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
427337e8e320f9814a5a900e078339cd0ecf905a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/function/lp_space.lean
8192c83bd44253b15338938e514c736d098f6e03
[ "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
74,066
lean
/- Copyright (c) 2020 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Sébastien Gouëzel -/ import analysis.normed.group.hom import measure_theory.function.lp_seminorm import topology.continuous_function.compact /-! # Lp space > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides the space `Lp E p μ` as the subtype of elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. For `1 ≤ p`, `snorm` defines a norm and `Lp` is a complete metric space. ## Main definitions * `Lp E p μ` : elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. Defined as an `add_subgroup` of `α →ₘ[μ] E`. Lipschitz functions vanishing at zero act by composition on `Lp`. We define this action, and prove that it is continuous. In particular, * `continuous_linear_map.comp_Lp` defines the action on `Lp` of a continuous linear map. * `Lp.pos_part` is the positive part of an `Lp` function. * `Lp.neg_part` is the negative part of an `Lp` function. When `α` is a topological space equipped with a finite Borel measure, there is a bounded linear map from the normed space of bounded continuous functions (`α →ᵇ E`) to `Lp E p μ`. We construct this as `bounded_continuous_function.to_Lp`. ## Notations * `α →₁[μ] E` : the type `Lp E 1 μ`. * `α →₂[μ] E` : the type `Lp E 2 μ`. ## Implementation Since `Lp` is defined as an `add_subgroup`, dot notation does not work. Use `Lp.measurable f` to say that the coercion of `f` to a genuine function is measurable, instead of the non-working `f.measurable`. To prove that two `Lp` elements are equal, it suffices to show that their coercions to functions coincide almost everywhere (this is registered as an `ext` rule). This can often be done using `filter_upwards`. For instance, a proof from first principles that `f + (g + h) = (f + g) + h` could read (in the `Lp` namespace) ``` example (f g h : Lp E p μ) : (f + g) + h = f + (g + h) := begin ext1, filter_upwards [coe_fn_add (f + g) h, coe_fn_add f g, coe_fn_add f (g + h), coe_fn_add g h] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, add_assoc], end ``` The lemma `coe_fn_add` states that the coercion of `f + g` coincides almost everywhere with the sum of the coercions of `f` and `g`. All such lemmas use `coe_fn` in their name, to distinguish the function coercion from the coercion to almost everywhere defined functions. -/ noncomputable theory open topological_space measure_theory filter open_locale nnreal ennreal big_operators topology measure_theory variables {α E F G : Type*} {m m0 : measurable_space α} {p : ℝ≥0∞} {q : ℝ} {μ ν : measure α} [normed_add_comm_group E] [normed_add_comm_group F] [normed_add_comm_group G] namespace measure_theory /-! ### Lp space The space of equivalence classes of measurable functions for which `snorm f p μ < ∞`. -/ @[simp] lemma snorm_ae_eq_fun {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) : snorm (ae_eq_fun.mk f hf) p μ = snorm f p μ := snorm_congr_ae (ae_eq_fun.coe_fn_mk _ _) lemma mem_ℒp.snorm_mk_lt_top {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hfp : mem_ℒp f p μ) : snorm (ae_eq_fun.mk f hfp.1) p μ < ∞ := by simp [hfp.2] /-- Lp space -/ def Lp {α} (E : Type*) {m : measurable_space α} [normed_add_comm_group E] (p : ℝ≥0∞) (μ : measure α . volume_tac) : add_subgroup (α →ₘ[μ] E) := { carrier := {f | snorm f p μ < ∞}, zero_mem' := by simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero], add_mem' := λ f g hf hg, by simp [snorm_congr_ae (ae_eq_fun.coe_fn_add _ _), snorm_add_lt_top ⟨f.ae_strongly_measurable, hf⟩ ⟨g.ae_strongly_measurable, hg⟩], neg_mem' := λ f hf, by rwa [set.mem_set_of_eq, snorm_congr_ae (ae_eq_fun.coe_fn_neg _), snorm_neg] } localized "notation (name := measure_theory.L1) α ` →₁[`:25 μ `] ` E := measure_theory.Lp E 1 μ" in measure_theory localized "notation (name := measure_theory.L2) α ` →₂[`:25 μ `] ` E := measure_theory.Lp E 2 μ" in measure_theory namespace mem_ℒp /-- make an element of Lp from a function verifying `mem_ℒp` -/ def to_Lp (f : α → E) (h_mem_ℒp : mem_ℒp f p μ) : Lp E p μ := ⟨ae_eq_fun.mk f h_mem_ℒp.1, h_mem_ℒp.snorm_mk_lt_top⟩ lemma coe_fn_to_Lp {f : α → E} (hf : mem_ℒp f p μ) : hf.to_Lp f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk _ _ lemma to_Lp_congr {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) (hfg : f =ᵐ[μ] g) : hf.to_Lp f = hg.to_Lp g := by simp [to_Lp, hfg] @[simp] lemma to_Lp_eq_to_Lp_iff {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : hf.to_Lp f = hg.to_Lp g ↔ f =ᵐ[μ] g := by simp [to_Lp] @[simp] lemma to_Lp_zero (h : mem_ℒp (0 : α → E) p μ) : h.to_Lp 0 = 0 := rfl lemma to_Lp_add {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.add hg).to_Lp (f + g) = hf.to_Lp f + hg.to_Lp g := rfl lemma to_Lp_neg {f : α → E} (hf : mem_ℒp f p μ) : hf.neg.to_Lp (-f) = - hf.to_Lp f := rfl lemma to_Lp_sub {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.sub hg).to_Lp (f - g) = hf.to_Lp f - hg.to_Lp g := rfl end mem_ℒp namespace Lp instance : has_coe_to_fun (Lp E p μ) (λ _, α → E) := ⟨λ f, ((f : α →ₘ[μ] E) : α → E)⟩ @[ext] lemma ext {f g : Lp E p μ} (h : f =ᵐ[μ] g) : f = g := begin cases f, cases g, simp only [subtype.mk_eq_mk], exact ae_eq_fun.ext h end lemma ext_iff {f g : Lp E p μ} : f = g ↔ f =ᵐ[μ] g := ⟨λ h, by rw h, λ h, ext h⟩ lemma mem_Lp_iff_snorm_lt_top {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ snorm f p μ < ∞ := iff.refl _ lemma mem_Lp_iff_mem_ℒp {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ mem_ℒp f p μ := by simp [mem_Lp_iff_snorm_lt_top, mem_ℒp, f.strongly_measurable.ae_strongly_measurable] protected lemma antitone [is_finite_measure μ] {p q : ℝ≥0∞} (hpq : p ≤ q) : Lp E q μ ≤ Lp E p μ := λ f hf, (mem_ℒp.mem_ℒp_of_exponent_le ⟨f.ae_strongly_measurable, hf⟩ hpq).2 @[simp] lemma coe_fn_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α → E) = f := rfl @[simp] lemma coe_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α →ₘ[μ] E) = f := rfl @[simp] lemma to_Lp_coe_fn (f : Lp E p μ) (hf : mem_ℒp f p μ) : hf.to_Lp f = f := by { cases f, simp [mem_ℒp.to_Lp] } lemma snorm_lt_top (f : Lp E p μ) : snorm f p μ < ∞ := f.prop lemma snorm_ne_top (f : Lp E p μ) : snorm f p μ ≠ ∞ := (snorm_lt_top f).ne @[measurability] protected lemma strongly_measurable (f : Lp E p μ) : strongly_measurable f := f.val.strongly_measurable @[measurability] protected lemma ae_strongly_measurable (f : Lp E p μ) : ae_strongly_measurable f μ := f.val.ae_strongly_measurable protected lemma mem_ℒp (f : Lp E p μ) : mem_ℒp f p μ := ⟨Lp.ae_strongly_measurable f, f.prop⟩ variables (E p μ) lemma coe_fn_zero : ⇑(0 : Lp E p μ) =ᵐ[μ] 0 := ae_eq_fun.coe_fn_zero variables {E p μ} lemma coe_fn_neg (f : Lp E p μ) : ⇑(-f) =ᵐ[μ] -f := ae_eq_fun.coe_fn_neg _ lemma coe_fn_add (f g : Lp E p μ) : ⇑(f + g) =ᵐ[μ] f + g := ae_eq_fun.coe_fn_add _ _ lemma coe_fn_sub (f g : Lp E p μ) : ⇑(f - g) =ᵐ[μ] f - g := ae_eq_fun.coe_fn_sub _ _ lemma mem_Lp_const (α) {m : measurable_space α} (μ : measure α) (c : E) [is_finite_measure μ] : @ae_eq_fun.const α _ _ μ _ c ∈ Lp E p μ := (mem_ℒp_const c).snorm_mk_lt_top instance : has_norm (Lp E p μ) := { norm := λ f, ennreal.to_real (snorm f p μ) } -- note: we need this to be defeq to the instance from `seminormed_add_group.to_has_nnnorm`, so -- can't use `ennreal.to_nnreal (snorm f p μ)` instance : has_nnnorm (Lp E p μ) := { nnnorm := λ f, ⟨‖f‖, ennreal.to_real_nonneg⟩ } instance : has_dist (Lp E p μ) := { dist := λ f g, ‖f - g‖} instance : has_edist (Lp E p μ) := { edist := λ f g, snorm (f - g) p μ } lemma norm_def (f : Lp E p μ) : ‖f‖ = ennreal.to_real (snorm f p μ) := rfl lemma nnnorm_def (f : Lp E p μ) : ‖f‖₊ = ennreal.to_nnreal (snorm f p μ) := subtype.eta _ _ @[simp, norm_cast] protected lemma coe_nnnorm (f : Lp E p μ) : (‖f‖₊ : ℝ) = ‖f‖ := rfl @[simp] lemma norm_to_Lp (f : α → E) (hf : mem_ℒp f p μ) : ‖hf.to_Lp f‖ = ennreal.to_real (snorm f p μ) := by rw [norm_def, snorm_congr_ae (mem_ℒp.coe_fn_to_Lp hf)] @[simp] lemma nnnorm_to_Lp (f : α → E) (hf : mem_ℒp f p μ) : ‖hf.to_Lp f‖₊ = ennreal.to_nnreal (snorm f p μ) := nnreal.eq $ norm_to_Lp f hf lemma dist_def (f g : Lp E p μ) : dist f g = (snorm (f - g) p μ).to_real := begin simp_rw [dist, norm_def], congr' 1, apply snorm_congr_ae (coe_fn_sub _ _), end lemma edist_def (f g : Lp E p μ) : edist f g = snorm (f - g) p μ := rfl @[simp] lemma edist_to_Lp_to_Lp (f g : α → E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : edist (hf.to_Lp f) (hg.to_Lp g) = snorm (f - g) p μ := by { rw edist_def, exact snorm_congr_ae (hf.coe_fn_to_Lp.sub hg.coe_fn_to_Lp) } @[simp] lemma edist_to_Lp_zero (f : α → E) (hf : mem_ℒp f p μ) : edist (hf.to_Lp f) 0 = snorm f p μ := by { convert edist_to_Lp_to_Lp f 0 hf zero_mem_ℒp, simp } @[simp] lemma nnnorm_zero : ‖(0 : Lp E p μ)‖₊ = 0 := begin rw [nnnorm_def], change (snorm ⇑(0 : α →ₘ[μ] E) p μ).to_nnreal = 0, simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero] end @[simp] lemma norm_zero : ‖(0 : Lp E p μ)‖ = 0 := congr_arg coe nnnorm_zero lemma nnnorm_eq_zero_iff {f : Lp E p μ} (hp : 0 < p) : ‖f‖₊ = 0 ↔ f = 0 := begin refine ⟨λ hf, _, λ hf, by simp [hf]⟩, rw [nnnorm_def, ennreal.to_nnreal_eq_zero_iff] at hf, cases hf, { rw snorm_eq_zero_iff (Lp.ae_strongly_measurable f) hp.ne.symm at hf, exact subtype.eq (ae_eq_fun.ext (hf.trans ae_eq_fun.coe_fn_zero.symm)), }, { exact absurd hf (snorm_ne_top f), }, end lemma norm_eq_zero_iff {f : Lp E p μ} (hp : 0 < p) : ‖f‖ = 0 ↔ f = 0 := iff.symm $ (nnnorm_eq_zero_iff hp).symm.trans $ (nnreal.coe_eq_zero _).symm lemma eq_zero_iff_ae_eq_zero {f : Lp E p μ} : f = 0 ↔ f =ᵐ[μ] 0 := begin split, { assume h, rw h, exact ae_eq_fun.coe_fn_const _ _ }, { assume h, ext1, filter_upwards [h, ae_eq_fun.coe_fn_const α (0 : E)] with _ ha h'a, rw ha, exact h'a.symm, }, end @[simp] lemma nnnorm_neg (f : Lp E p μ) : ‖-f‖₊ = ‖f‖₊ := by rw [nnnorm_def, nnnorm_def, snorm_congr_ae (coe_fn_neg _), snorm_neg] @[simp] lemma norm_neg (f : Lp E p μ) : ‖-f‖ = ‖f‖ := (congr_arg (coe : ℝ≥0 → ℝ) (nnnorm_neg f) : _) lemma nnnorm_le_mul_nnnorm_of_ae_le_mul {c : ℝ≥0} {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖₊ ≤ c * ‖g x‖₊ ) : ‖f‖₊ ≤ c * ‖g‖₊ := begin simp only [nnnorm_def], have := snorm_le_nnreal_smul_snorm_of_ae_le_mul h p, rwa [← ennreal.to_nnreal_le_to_nnreal, ennreal.smul_def, smul_eq_mul, ennreal.to_nnreal_mul, ennreal.to_nnreal_coe] at this, { exact (Lp.mem_ℒp _).snorm_ne_top }, { exact ennreal.mul_ne_top ennreal.coe_ne_top (Lp.mem_ℒp _).snorm_ne_top }, end lemma norm_le_mul_norm_of_ae_le_mul {c : ℝ} {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) : ‖f‖ ≤ c * ‖g‖ := begin cases le_or_lt 0 c with hc hc, { lift c to ℝ≥0 using hc, exact nnreal.coe_le_coe.mpr (nnnorm_le_mul_nnnorm_of_ae_le_mul h) }, { simp only [norm_def], have := snorm_eq_zero_and_zero_of_ae_le_mul_neg h hc p, simp [this] } end lemma norm_le_norm_of_ae_le {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : ‖f‖ ≤ ‖g‖ := begin rw [norm_def, norm_def, ennreal.to_real_le_to_real (snorm_ne_top _) (snorm_ne_top _)], exact snorm_mono_ae h end lemma mem_Lp_of_nnnorm_ae_le_mul {c : ℝ≥0} {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖₊ ≤ c * ‖g x‖₊) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_nnnorm_le_mul (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_le_mul {c : ℝ} {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le_mul (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_nnnorm_ae_le {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖₊ ≤ ‖g x‖₊) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_le {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : f ∈ Lp E p μ := mem_Lp_of_nnnorm_ae_le h lemma mem_Lp_of_ae_nnnorm_bound [is_finite_measure μ] {f : α →ₘ[μ] E} (C : ℝ≥0) (hfC : ∀ᵐ x ∂μ, ‖f x‖₊ ≤ C) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_bound f.ae_strongly_measurable _ hfC lemma mem_Lp_of_ae_bound [is_finite_measure μ] {f : α →ₘ[μ] E} (C : ℝ) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_bound f.ae_strongly_measurable _ hfC lemma nnnorm_le_of_ae_bound [is_finite_measure μ] {f : Lp E p μ} {C : ℝ≥0} (hfC : ∀ᵐ x ∂μ, ‖f x‖₊ ≤ C) : ‖f‖₊ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * C := begin by_cases hμ : μ = 0, { by_cases hp : p.to_real⁻¹ = 0, { simp [hp, hμ, nnnorm_def] }, { simp [hμ, nnnorm_def, real.zero_rpow hp] } }, rw [←ennreal.coe_le_coe, nnnorm_def, ennreal.coe_to_nnreal (snorm_ne_top _)], refine (snorm_le_of_ae_nnnorm_bound hfC).trans_eq _, rw [← coe_measure_univ_nnreal μ, ennreal.coe_rpow_of_ne_zero (measure_univ_nnreal_pos hμ).ne', ennreal.coe_mul, mul_comm, ennreal.smul_def, smul_eq_mul], end lemma norm_le_of_ae_bound [is_finite_measure μ] {f : Lp E p μ} {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : ‖f‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * C := begin lift C to ℝ≥0 using hC, have := nnnorm_le_of_ae_bound hfC, rwa [←nnreal.coe_le_coe, nnreal.coe_mul, nnreal.coe_rpow] at this, end instance [hp : fact (1 ≤ p)] : normed_add_comm_group (Lp E p μ) := { edist := edist, edist_dist := λ f g, by rw [edist_def, dist_def, ←snorm_congr_ae (coe_fn_sub _ _), ennreal.of_real_to_real (snorm_ne_top (f - g))], ..add_group_norm.to_normed_add_comm_group { to_fun := (norm : Lp E p μ → ℝ), map_zero' := norm_zero, neg' := by simp, add_le' := λ f g, begin simp only [norm_def], rw ← ennreal.to_real_add (snorm_ne_top f) (snorm_ne_top g), suffices h_snorm : snorm ⇑(f + g) p μ ≤ snorm ⇑f p μ + snorm ⇑g p μ, { rwa ennreal.to_real_le_to_real (snorm_ne_top (f + g)), exact ennreal.add_ne_top.mpr ⟨snorm_ne_top f, snorm_ne_top g⟩, }, rw [snorm_congr_ae (coe_fn_add _ _)], exact snorm_add_le (Lp.ae_strongly_measurable f) (Lp.ae_strongly_measurable g) hp.1, end, eq_zero_of_map_eq_zero' := λ f, (norm_eq_zero_iff $ zero_lt_one.trans_le hp.1).1 } } -- check no diamond is created example [fact (1 ≤ p)] : pseudo_emetric_space.to_has_edist = (Lp.has_edist : has_edist (Lp E p μ)) := rfl example [fact (1 ≤ p)] : seminormed_add_group.to_has_nnnorm = (Lp.has_nnnorm : has_nnnorm (Lp E p μ)) := rfl section has_bounded_smul variables {𝕜 𝕜' : Type*} variables [normed_ring 𝕜] [normed_ring 𝕜'] [module 𝕜 E] [module 𝕜' E] variables [has_bounded_smul 𝕜 E] [has_bounded_smul 𝕜' E] lemma mem_Lp_const_smul (c : 𝕜) (f : Lp E p μ) : c • ↑f ∈ Lp E p μ := begin rw [mem_Lp_iff_snorm_lt_top, snorm_congr_ae (ae_eq_fun.coe_fn_smul _ _)], refine (snorm_const_smul_le _ _).trans_lt _, rw [ennreal.smul_def, smul_eq_mul, ennreal.mul_lt_top_iff], exact or.inl ⟨ennreal.coe_lt_top, f.prop⟩, end variables (E p μ 𝕜) /-- The `𝕜`-submodule of elements of `α →ₘ[μ] E` whose `Lp` norm is finite. This is `Lp E p μ`, with extra structure. -/ def Lp_submodule : submodule 𝕜 (α →ₘ[μ] E) := { smul_mem' := λ c f hf, by simpa using mem_Lp_const_smul c ⟨f, hf⟩, .. Lp E p μ } variables {E p μ 𝕜} lemma coe_Lp_submodule : (Lp_submodule E p μ 𝕜).to_add_subgroup = Lp E p μ := rfl instance : module 𝕜 (Lp E p μ) := { .. (Lp_submodule E p μ 𝕜).module } lemma coe_fn_smul (c : 𝕜) (f : Lp E p μ) : ⇑(c • f) =ᵐ[μ] c • f := ae_eq_fun.coe_fn_smul _ _ instance [module 𝕜ᵐᵒᵖ E] [has_bounded_smul 𝕜ᵐᵒᵖ E] [is_central_scalar 𝕜 E] : is_central_scalar 𝕜 (Lp E p μ) := { op_smul_eq_smul := λ k f, subtype.ext $ op_smul_eq_smul k (f : α →ₘ[μ] E) } instance [smul_comm_class 𝕜 𝕜' E] : smul_comm_class 𝕜 𝕜' (Lp E p μ) := { smul_comm := λ k k' f, subtype.ext $ smul_comm k k' (f : α →ₘ[μ] E) } instance [has_smul 𝕜 𝕜'] [is_scalar_tower 𝕜 𝕜' E] : is_scalar_tower 𝕜 𝕜' (Lp E p μ) := { smul_assoc := λ k k' f, subtype.ext $ smul_assoc k k' (f : α →ₘ[μ] E) } instance [fact (1 ≤ p)] : has_bounded_smul 𝕜 (Lp E p μ) := -- TODO: add `has_bounded_smul.of_nnnorm_smul_le has_bounded_smul.of_norm_smul_le $ λ r f, begin suffices : (‖r • f‖₊ : ℝ≥0∞) ≤ ‖r‖₊ * ‖f‖₊, { exact_mod_cast this }, rw [nnnorm_def, nnnorm_def, ennreal.coe_to_nnreal (Lp.snorm_ne_top _), snorm_congr_ae (coe_fn_smul _ _), ennreal.coe_to_nnreal (Lp.snorm_ne_top _)], exact snorm_const_smul_le r f, end end has_bounded_smul section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] instance [fact (1 ≤ p)] : normed_space 𝕜 (Lp E p μ) := { norm_smul_le := λ _ _, norm_smul_le _ _ } end normed_space end Lp namespace mem_ℒp variables {𝕜 : Type*} [normed_ring 𝕜] [module 𝕜 E] [has_bounded_smul 𝕜 E] lemma to_Lp_const_smul {f : α → E} (c : 𝕜) (hf : mem_ℒp f p μ) : (hf.const_smul c).to_Lp (c • f) = c • hf.to_Lp f := rfl end mem_ℒp /-! ### Indicator of a set as an element of Lᵖ For a set `s` with `(hs : measurable_set s)` and `(hμs : μ s < ∞)`, we build `indicator_const_Lp p hs hμs c`, the element of `Lp` corresponding to `s.indicator (λ x, c)`. -/ section indicator variables {s : set α} {hs : measurable_set s} {c : E} {f : α → E} {hf : ae_strongly_measurable f μ} lemma snorm_ess_sup_indicator_le (s : set α) (f : α → G) : snorm_ess_sup (s.indicator f) μ ≤ snorm_ess_sup f μ := begin refine ess_sup_mono_ae (eventually_of_forall (λ x, _)), rw [ennreal.coe_le_coe, nnnorm_indicator_eq_indicator_nnnorm], exact set.indicator_le_self s _ x, end lemma snorm_ess_sup_indicator_const_le (s : set α) (c : G) : snorm_ess_sup (s.indicator (λ x : α , c)) μ ≤ ‖c‖₊ := begin by_cases hμ0 : μ = 0, { rw [hμ0, snorm_ess_sup_measure_zero], exact zero_le _ }, { exact (snorm_ess_sup_indicator_le s (λ x, c)).trans (snorm_ess_sup_const c hμ0).le, }, end lemma snorm_ess_sup_indicator_const_eq (s : set α) (c : G) (hμs : μ s ≠ 0) : snorm_ess_sup (s.indicator (λ x : α , c)) μ = ‖c‖₊ := begin refine le_antisymm (snorm_ess_sup_indicator_const_le s c) _, by_contra' h, have h' := ae_iff.mp (ae_lt_of_ess_sup_lt h), push_neg at h', refine hμs (measure_mono_null (λ x hx_mem, _) h'), rw [set.mem_set_of_eq, set.indicator_of_mem hx_mem], exact le_rfl, end variables (hs) lemma snorm_indicator_le {E : Type*} [normed_add_comm_group E] (f : α → E) : snorm (s.indicator f) p μ ≤ snorm f p μ := begin refine snorm_mono_ae (eventually_of_forall (λ x, _)), suffices : ‖s.indicator f x‖₊ ≤ ‖f x‖₊, { exact nnreal.coe_mono this }, rw nnnorm_indicator_eq_indicator_nnnorm, exact s.indicator_le_self _ x, end variables {hs} lemma snorm_indicator_const {c : G} (hs : measurable_set s) (hp : p ≠ 0) (hp_top : p ≠ ∞) : snorm (s.indicator (λ x, c)) p μ = ‖c‖₊ * (μ s) ^ (1 / p.to_real) := begin have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp hp_top, rw snorm_eq_lintegral_rpow_nnnorm hp hp_top, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_indicator_pow : (λ a : α, s.indicator (λ (x : α), (‖c‖₊ : ℝ≥0∞)) a ^ p.to_real) = s.indicator (λ (x : α), ↑‖c‖₊ ^ p.to_real), { rw set.comp_indicator_const (‖c‖₊ : ℝ≥0∞) (λ x, x ^ p.to_real) _, simp [hp_pos], }, rw [h_indicator_pow, lintegral_indicator _ hs, set_lintegral_const, ennreal.mul_rpow_of_nonneg], { rw [← ennreal.rpow_mul, mul_one_div_cancel hp_pos.ne.symm, ennreal.rpow_one], }, { simp [hp_pos.le], }, end lemma snorm_indicator_const' {c : G} (hs : measurable_set s) (hμs : μ s ≠ 0) (hp : p ≠ 0) : snorm (s.indicator (λ _, c)) p μ = ‖c‖₊ * (μ s) ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_indicator_const_eq s c hμs], }, { exact snorm_indicator_const hs hp hp_top, }, end lemma snorm_indicator_const_le (c : G) (p : ℝ≥0∞) : snorm (s.indicator (λ x, c)) p μ ≤ ‖c‖₊ * (μ s) ^ (1 / p.to_real) := begin rcases eq_or_ne p 0 with rfl|hp, { simp only [snorm_exponent_zero, zero_le'] }, rcases eq_or_ne p ∞ with rfl|h'p, { simp only [snorm_exponent_top, ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one], exact snorm_ess_sup_indicator_const_le _ _ }, let t := to_measurable μ s, calc snorm (s.indicator (λ x, c)) p μ ≤ snorm (t.indicator (λ x, c)) p μ : snorm_mono (norm_indicator_le_of_subset (subset_to_measurable _ _) _) ... = ‖c‖₊ * (μ t) ^ (1 / p.to_real) : snorm_indicator_const (measurable_set_to_measurable _ _) hp h'p ... = ‖c‖₊ * (μ s) ^ (1 / p.to_real) : by rw measure_to_measurable end lemma mem_ℒp.indicator (hs : measurable_set s) (hf : mem_ℒp f p μ) : mem_ℒp (s.indicator f) p μ := ⟨hf.ae_strongly_measurable.indicator hs, lt_of_le_of_lt (snorm_indicator_le f) hf.snorm_lt_top⟩ lemma snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict {f : α → F} (hs : measurable_set s) : snorm_ess_sup (s.indicator f) μ = snorm_ess_sup f (μ.restrict s) := begin simp_rw [snorm_ess_sup, nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], by_cases hs_null : μ s = 0, { rw measure.restrict_zero_set hs_null, simp only [ess_sup_measure_zero, ennreal.ess_sup_eq_zero_iff, ennreal.bot_eq_zero], have hs_empty : s =ᵐ[μ] (∅ : set α), by { rw ae_eq_set, simpa using hs_null, }, refine (indicator_ae_eq_of_ae_eq_set hs_empty).trans _, rw set.indicator_empty, refl, }, rw ess_sup_indicator_eq_ess_sup_restrict (eventually_of_forall (λ x, _)) hs hs_null, rw pi.zero_apply, exact zero_le _, end lemma snorm_indicator_eq_snorm_restrict {f : α → F} (hs : measurable_set s) : snorm (s.indicator f) p μ = snorm f p (μ.restrict s) := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top], exact snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict hs, }, simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, suffices : ∫⁻ x, ‖s.indicator f x‖₊ ^ p.to_real ∂μ = ∫⁻ x in s, ‖f x‖₊ ^ p.to_real ∂μ, by rw this, rw ← lintegral_indicator _ hs, congr, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_zero : (λ x, x ^ p.to_real) (0 : ℝ≥0∞) = 0, by simp [ennreal.to_real_pos hp_zero hp_top], exact (set.indicator_comp_of_zero h_zero).symm, end lemma mem_ℒp_indicator_iff_restrict (hs : measurable_set s) : mem_ℒp (s.indicator f) p μ ↔ mem_ℒp f p (μ.restrict s) := by simp [mem_ℒp, ae_strongly_measurable_indicator_iff hs, snorm_indicator_eq_snorm_restrict hs] lemma mem_ℒp_indicator_const (p : ℝ≥0∞) (hs : measurable_set s) (c : E) (hμsc : c = 0 ∨ μ s ≠ ∞) : mem_ℒp (s.indicator (λ _, c)) p μ := begin rw mem_ℒp_indicator_iff_restrict hs, by_cases hp_zero : p = 0, { rw hp_zero, exact mem_ℒp_zero_iff_ae_strongly_measurable.mpr ae_strongly_measurable_const, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_ℒp_top_of_bound ae_strongly_measurable_const (‖c‖) (eventually_of_forall (λ x, le_rfl)), }, rw [mem_ℒp_const_iff hp_zero hp_top, measure.restrict_apply_univ], cases hμsc, { exact or.inl hμsc, }, { exact or.inr hμsc.lt_top, }, end /-- The `ℒ^p` norm of the indicator of a set is uniformly small if the set itself has small measure, for any `p < ∞`. Given here as an existential `∀ ε > 0, ∃ η > 0, ...` to avoid later management of `ℝ≥0∞`-arithmetic. -/ lemma exists_snorm_indicator_le (hp : p ≠ ∞) (c : E) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ (η : ℝ≥0), 0 < η ∧ ∀ (s : set α), μ s ≤ η → snorm (s.indicator (λ x, c)) p μ ≤ ε := begin rcases eq_or_ne p 0 with rfl|h'p, { exact ⟨1, zero_lt_one, λ s hs, by simp⟩ }, have hp₀ : 0 < p := bot_lt_iff_ne_bot.2 h'p, have hp₀' : 0 ≤ 1 / p.to_real := div_nonneg zero_le_one ennreal.to_real_nonneg, have hp₀'' : 0 < p.to_real, { simpa [← ennreal.to_real_lt_to_real ennreal.zero_ne_top hp] using hp₀ }, obtain ⟨η, hη_pos, hη_le⟩ : ∃ (η : ℝ≥0), 0 < η ∧ (‖c‖₊ * η ^ (1 / p.to_real) : ℝ≥0∞) ≤ ε, { have : filter.tendsto (λ x : ℝ≥0, ((‖c‖₊ * x ^ (1 / p.to_real) : ℝ≥0) : ℝ≥0∞)) (𝓝 0) (𝓝 (0 : ℝ≥0)), { rw ennreal.tendsto_coe, convert ((nnreal.continuous_at_rpow_const (or.inr hp₀')).tendsto).const_mul _, simp [hp₀''.ne'] }, have hε' : 0 < ε := hε.bot_lt, obtain ⟨δ, hδ, hδε'⟩ := nnreal.nhds_zero_basis.eventually_iff.mp (eventually_le_of_tendsto_lt hε' this), obtain ⟨η, hη, hηδ⟩ := exists_between hδ, refine ⟨η, hη, _⟩, rw [ennreal.coe_rpow_of_nonneg _ hp₀', ← ennreal.coe_mul], exact hδε' hηδ }, refine ⟨η, hη_pos, λ s hs, _⟩, refine (snorm_indicator_const_le _ _).trans (le_trans _ hη_le), exact mul_le_mul_left' (ennreal.rpow_le_rpow hs hp₀') _, end end indicator section indicator_const_Lp open set function variables {s : set α} {hs : measurable_set s} {hμs : μ s ≠ ∞} {c : E} /-- Indicator of a set as an element of `Lp`. -/ def indicator_const_Lp (p : ℝ≥0∞) (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : Lp E p μ := mem_ℒp.to_Lp (s.indicator (λ _, c)) (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn : ⇑(indicator_const_Lp p hs hμs c) =ᵐ[μ] s.indicator (λ _, c) := mem_ℒp.coe_fn_to_Lp (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn_mem : ∀ᵐ (x : α) ∂μ, x ∈ s → indicator_const_Lp p hs hμs c x = c := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_mem hxs _)) lemma indicator_const_Lp_coe_fn_nmem : ∀ᵐ (x : α) ∂μ, x ∉ s → indicator_const_Lp p hs hμs c x = 0 := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_not_mem hxs _)) lemma norm_indicator_const_Lp (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : ‖indicator_const_Lp p hs hμs c‖ = ‖c‖ * (μ s).to_real ^ (1 / p.to_real) := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const hs hp_ne_zero hp_ne_top, ennreal.to_real_mul, ennreal.to_real_rpow, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp_top (hμs_ne_zero : μ s ≠ 0) : ‖indicator_const_Lp ∞ hs hμs c‖ = ‖c‖ := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const' hs hμs_ne_zero ennreal.top_ne_zero, ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp' (hp_pos : p ≠ 0) (hμs_pos : μ s ≠ 0) : ‖indicator_const_Lp p hs hμs c‖ = ‖c‖ * (μ s).to_real ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { rw [hp_top, ennreal.top_to_real, div_zero, real.rpow_zero, mul_one], exact norm_indicator_const_Lp_top hμs_pos, }, { exact norm_indicator_const_Lp hp_pos hp_top, }, end @[simp] lemma indicator_const_empty : indicator_const_Lp p measurable_set.empty (by simp : μ ∅ ≠ ∞) c = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, convert indicator_const_Lp_coe_fn, simp [set.indicator_empty'], end lemma mem_ℒp_add_of_disjoint {f g : α → E} (h : disjoint (support f) (support g)) (hf : strongly_measurable f) (hg : strongly_measurable g) : mem_ℒp (f + g) p μ ↔ mem_ℒp f p μ ∧ mem_ℒp g p μ := begin borelize E, refine ⟨λ hfg, ⟨_, _⟩, λ h, h.1.add h.2⟩, { rw ← indicator_add_eq_left h, exact hfg.indicator (measurable_set_support hf.measurable) }, { rw ← indicator_add_eq_right h, exact hfg.indicator (measurable_set_support hg.measurable) } end /-- The indicator of a disjoint union of two sets is the sum of the indicators of the sets. -/ lemma indicator_const_Lp_disjoint_union {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (c : E) : (indicator_const_Lp p (hs.union ht) ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne c) = indicator_const_Lp p hs hμs c + indicator_const_Lp p ht hμt c := begin ext1, refine indicator_const_Lp_coe_fn.trans (eventually_eq.trans _ (Lp.coe_fn_add _ _).symm), refine eventually_eq.trans _ (eventually_eq.add indicator_const_Lp_coe_fn.symm indicator_const_Lp_coe_fn.symm), rw set.indicator_union_of_disjoint (set.disjoint_iff_inter_eq_empty.mpr hst) _, end end indicator_const_Lp lemma mem_ℒp.norm_rpow_div {f : α → E} (hf : mem_ℒp f p μ) (q : ℝ≥0∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ q.to_real) (p/q) μ := begin refine ⟨(hf.1.norm.ae_measurable.pow_const q.to_real).ae_strongly_measurable, _⟩, by_cases q_top : q = ∞, { simp [q_top] }, by_cases q_zero : q = 0, { simp [q_zero], by_cases p_zero : p = 0, { simp [p_zero] }, rw ennreal.div_zero p_zero, exact (mem_ℒp_top_const (1 : ℝ)).2 }, rw snorm_norm_rpow _ (ennreal.to_real_pos q_zero q_top), apply ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg, rw [ennreal.of_real_to_real q_top, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one], exact hf.2.ne end lemma mem_ℒp_norm_rpow_iff {q : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) (q_zero : q ≠ 0) (q_top : q ≠ ∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ q.to_real) (p/q) μ ↔ mem_ℒp f p μ := begin refine ⟨λ h, _, λ h, h.norm_rpow_div q⟩, apply (mem_ℒp_norm_iff hf).1, convert h.norm_rpow_div (q⁻¹), { ext x, rw [real.norm_eq_abs, real.abs_rpow_of_nonneg (norm_nonneg _), ← real.rpow_mul (abs_nonneg _), ennreal.to_real_inv, mul_inv_cancel, abs_of_nonneg (norm_nonneg _), real.rpow_one], simp [ennreal.to_real_eq_zero_iff, not_or_distrib, q_zero, q_top] }, { rw [div_eq_mul_inv, inv_inv, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one] } end lemma mem_ℒp.norm_rpow {f : α → E} (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ p.to_real) 1 μ := begin convert hf.norm_rpow_div p, rw [div_eq_mul_inv, ennreal.mul_inv_cancel hp_ne_zero hp_ne_top], end end measure_theory open measure_theory /-! ### Composition on `L^p` We show that Lipschitz functions vanishing at zero act by composition on `L^p`, and specialize this to the composition with continuous linear maps, and to the definition of the positive part of an `L^p` function. -/ section composition variables {g : E → F} {c : ℝ≥0} lemma lipschitz_with.comp_mem_ℒp {α E F} {K} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (g0 : g 0 = 0) (hL : mem_ℒp f p μ) : mem_ℒp (g ∘ f) p μ := begin have : ∀ x, ‖g (f x)‖ ≤ K * ‖f x‖, { intro a, -- TODO: add `lipschitz_with.nnnorm_sub_le` and `lipschitz_with.nnnorm_le` simpa [g0] using hg.norm_sub_le (f a) 0, }, exact hL.of_le_mul (hg.continuous.comp_ae_strongly_measurable hL.1) (eventually_of_forall this), end lemma measure_theory.mem_ℒp.of_comp_antilipschitz_with {α E F} {K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hL : mem_ℒp (g ∘ f) p μ) (hg : uniform_continuous g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp f p μ := begin have A : ∀ x, ‖f x‖ ≤ K' * ‖g (f x)‖, { intro x, -- TODO: add `antilipschitz_with.le_mul_nnnorm_sub` and `antilipschitz_with.le_mul_norm` rw [← dist_zero_right, ← dist_zero_right, ← g0], apply hg'.le_mul_dist }, have B : ae_strongly_measurable f μ := ((hg'.uniform_embedding hg).embedding.ae_strongly_measurable_comp_iff.1 hL.1), exact hL.of_le_mul B (filter.eventually_of_forall A), end namespace lipschitz_with lemma mem_ℒp_comp_iff_of_antilipschitz {α E F} {K K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp (g ∘ f) p μ ↔ mem_ℒp f p μ := ⟨λ h, h.of_comp_antilipschitz_with hg.uniform_continuous hg' g0, λ h, hg.comp_mem_ℒp g0 h⟩ /-- When `g` is a Lipschitz function sending `0` to `0` and `f` is in `Lp`, then `g ∘ f` is well defined as an element of `Lp`. -/ def comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : Lp F p μ := ⟨ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E), begin suffices : ∀ᵐ x ∂μ, ‖ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E) x‖ ≤ c * ‖f x‖, { exact Lp.mem_Lp_of_ae_le_mul this }, filter_upwards [ae_eq_fun.coe_fn_comp g hg.continuous (f : α →ₘ[μ] E)] with a ha, simp only [ha], rw [← dist_zero_right, ← dist_zero_right, ← g0], exact hg.dist_le_mul (f a) 0, end⟩ lemma coe_fn_comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : hg.comp_Lp g0 f =ᵐ[μ] g ∘ f := ae_eq_fun.coe_fn_comp _ _ _ @[simp] lemma comp_Lp_zero (hg : lipschitz_with c g) (g0 : g 0 = 0) : hg.comp_Lp g0 (0 : Lp E p μ) = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, apply (coe_fn_comp_Lp _ _ _).trans, filter_upwards [Lp.coe_fn_zero E p μ] with _ ha, simp [ha, g0], end lemma norm_comp_Lp_sub_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f f' : Lp E p μ) : ‖hg.comp_Lp g0 f - hg.comp_Lp g0 f'‖ ≤ c * ‖f - f'‖ := begin apply Lp.norm_le_mul_norm_of_ae_le_mul, filter_upwards [hg.coe_fn_comp_Lp g0 f, hg.coe_fn_comp_Lp g0 f', Lp.coe_fn_sub (hg.comp_Lp g0 f) (hg.comp_Lp g0 f'), Lp.coe_fn_sub f f'] with a ha1 ha2 ha3 ha4, simp [ha1, ha2, ha3, ha4, ← dist_eq_norm], exact hg.dist_le_mul (f a) (f' a) end lemma norm_comp_Lp_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : ‖hg.comp_Lp g0 f‖ ≤ c * ‖f‖ := by simpa using hg.norm_comp_Lp_sub_le g0 f 0 lemma lipschitz_with_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : lipschitz_with c (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := lipschitz_with.of_dist_le_mul $ λ f g, by simp [dist_eq_norm, norm_comp_Lp_sub_le] lemma continuous_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : continuous (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := (lipschitz_with_comp_Lp hg g0).continuous end lipschitz_with namespace continuous_linear_map variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] /-- Composing `f : Lp ` with `L : E →L[𝕜] F`. -/ def comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : Lp F p μ := L.lipschitz.comp_Lp (map_zero L) f lemma coe_fn_comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : ∀ᵐ a ∂μ, (L.comp_Lp f) a = L (f a) := lipschitz_with.coe_fn_comp_Lp _ _ _ lemma coe_fn_comp_Lp' (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_Lp f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma comp_mem_ℒp (L : E →L[𝕜] F) (f : Lp E p μ) : mem_ℒp (L ∘ f) p μ := (Lp.mem_ℒp (L.comp_Lp f)).ae_eq (L.coe_fn_comp_Lp' f) lemma comp_mem_ℒp' (L : E →L[𝕜] F) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (L ∘ f) p μ := (L.comp_mem_ℒp (hf.to_Lp f)).ae_eq (eventually_eq.fun_comp (hf.coe_fn_to_Lp) _) section is_R_or_C variables {K : Type*} [is_R_or_C K] lemma _root_.measure_theory.mem_ℒp.of_real {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, (f x : K)) p μ := (@is_R_or_C.of_real_clm K _).comp_mem_ℒp' hf lemma _root_.measure_theory.mem_ℒp_re_im_iff {f : α → K} : mem_ℒp (λ x, is_R_or_C.re (f x)) p μ ∧ mem_ℒp (λ x, is_R_or_C.im (f x)) p μ ↔ mem_ℒp f p μ := begin refine ⟨_, λ hf, ⟨hf.re, hf.im⟩⟩, rintro ⟨hre, him⟩, convert hre.of_real.add (him.of_real.const_mul is_R_or_C.I), { ext1 x, rw [pi.add_apply, mul_comm, is_R_or_C.re_add_im] }, all_goals { apply_instance } end end is_R_or_C lemma add_comp_Lp (L L' : E →L[𝕜] F) (f : Lp E p μ) : (L + L').comp_Lp f = L.comp_Lp f + L'.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (L + L') f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine eventually_eq.trans _ (eventually_eq.add (L.coe_fn_comp_Lp' f).symm (L'.coe_fn_comp_Lp' f).symm), refine eventually_of_forall (λ x, _), refl, end lemma smul_comp_Lp {𝕜'} [normed_ring 𝕜'] [module 𝕜' F] [has_bounded_smul 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) (f : Lp E p μ) : (c • L).comp_Lp f = c • L.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (c • L) f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine (L.coe_fn_comp_Lp' f).mono (λ x hx, _), rw [pi.smul_apply, hx], refl, end lemma norm_comp_Lp_le (L : E →L[𝕜] F) (f : Lp E p μ) : ‖L.comp_Lp f‖ ≤ ‖L‖ * ‖f‖ := lipschitz_with.norm_comp_Lp_le _ _ _ variables (μ p) /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a `𝕜`-linear map on `Lp E p μ`. -/ def comp_Lpₗ (L : E →L[𝕜] F) : (Lp E p μ) →ₗ[𝕜] (Lp F p μ) := { to_fun := λ f, L.comp_Lp f, map_add' := begin intros f g, ext1, filter_upwards [Lp.coe_fn_add f g, coe_fn_comp_Lp L (f + g), coe_fn_comp_Lp L f, coe_fn_comp_Lp L g, Lp.coe_fn_add (L.comp_Lp f) (L.comp_Lp g)], assume a ha1 ha2 ha3 ha4 ha5, simp only [ha1, ha2, ha3, ha4, ha5, map_add, pi.add_apply], end, map_smul' := begin intros c f, dsimp, ext1, filter_upwards [Lp.coe_fn_smul c f, coe_fn_comp_Lp L (c • f), Lp.coe_fn_smul c (L.comp_Lp f), coe_fn_comp_Lp L f] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, smul_hom_class.map_smul, pi.smul_apply], end } /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a continuous `𝕜`-linear map on `Lp E p μ`. See also the similar * `linear_map.comp_left` for functions, * `continuous_linear_map.comp_left_continuous` for continuous functions, * `continuous_linear_map.comp_left_continuous_bounded` for bounded continuous functions, * `continuous_linear_map.comp_left_continuous_compact` for continuous functions on compact spaces. -/ def comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) : (Lp E p μ) →L[𝕜] (Lp F p μ) := linear_map.mk_continuous (L.comp_Lpₗ p μ) ‖L‖ L.norm_comp_Lp_le variables {μ p} lemma coe_fn_comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_LpL p μ f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma add_comp_LpL [fact (1 ≤ p)] (L L' : E →L[𝕜] F) : (L + L').comp_LpL p μ = L.comp_LpL p μ + L'.comp_LpL p μ := by { ext1 f, exact add_comp_Lp L L' f } lemma smul_comp_LpL [fact (1 ≤ p)] {𝕜'} [normed_ring 𝕜'] [module 𝕜' F] [has_bounded_smul 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) : (c • L).comp_LpL p μ = c • L.comp_LpL p μ := by { ext1 f, exact smul_comp_Lp c L f } lemma norm_compLpL_le [fact (1 ≤ p)] (L : E →L[𝕜] F) : ‖L.comp_LpL p μ‖ ≤ ‖L‖ := linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end continuous_linear_map namespace measure_theory lemma indicator_const_Lp_eq_to_span_singleton_comp_Lp {s : set α} [normed_space ℝ F] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : F) : indicator_const_Lp 2 hs hμs x = (continuous_linear_map.to_span_singleton ℝ x).comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)) := begin ext1, refine indicator_const_Lp_coe_fn.trans _, have h_comp_Lp := (continuous_linear_map.to_span_singleton ℝ x).coe_fn_comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)), rw ← eventually_eq at h_comp_Lp, refine eventually_eq.trans _ h_comp_Lp.symm, refine (@indicator_const_Lp_coe_fn _ _ _ 2 μ _ s hs hμs (1 : ℝ)).mono (λ y hy, _), dsimp only, rw hy, simp_rw [continuous_linear_map.to_span_singleton_apply], by_cases hy_mem : y ∈ s; simp [hy_mem, continuous_linear_map.lsmul_apply], end namespace Lp section pos_part lemma lipschitz_with_pos_part : lipschitz_with 1 (λ (x : ℝ), max x 0) := lipschitz_with.of_dist_le_mul $ λ x y, by simp [real.dist_eq, abs_max_sub_max_le_abs] lemma _root_.measure_theory.mem_ℒp.pos_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf lemma _root_.measure_theory.mem_ℒp.neg_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (-f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf.neg /-- Positive part of a function in `L^p`. -/ def pos_part (f : Lp ℝ p μ) : Lp ℝ p μ := lipschitz_with_pos_part.comp_Lp (max_eq_right le_rfl) f /-- Negative part of a function in `L^p`. -/ def neg_part (f : Lp ℝ p μ) : Lp ℝ p μ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : Lp ℝ p μ) : (pos_part f : α →ₘ[μ] ℝ) = (f : α →ₘ[μ] ℝ).pos_part := rfl lemma coe_fn_pos_part (f : Lp ℝ p μ) : ⇑(pos_part f) =ᵐ[μ] λ a, max (f a) 0 := ae_eq_fun.coe_fn_pos_part _ lemma coe_fn_neg_part_eq_max (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = max (- f a) 0 := begin rw neg_part, filter_upwards [coe_fn_pos_part (-f), coe_fn_neg f] with _ h₁ h₂, rw [h₁, h₂, pi.neg_apply], end lemma coe_fn_neg_part (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = - min (f a) 0 := (coe_fn_neg_part_eq_max f).mono $ assume a h, by rw [h, ← max_neg_neg, neg_zero] lemma continuous_pos_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, pos_part f) := lipschitz_with.continuous_comp_Lp _ _ lemma continuous_neg_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, neg_part f) := have eq : (λf : Lp ℝ p μ, neg_part f) = (λf : Lp ℝ p μ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part end Lp end measure_theory end composition /-! ## `L^p` is a complete space We show that `L^p` is a complete space for `1 ≤ p`. -/ section complete_space namespace measure_theory namespace Lp lemma snorm'_lim_eq_lintegral_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {p : ℝ} (hp_nonneg : 0 ≤ p) {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ = (∫⁻ a, at_top.liminf (λ m, (‖f m a‖₊ : ℝ≥0∞)^p) ∂μ) ^ (1/p) := begin suffices h_no_pow : (∫⁻ a, ‖f_lim a‖₊ ^ p ∂μ) = (∫⁻ a, at_top.liminf (λ m, (‖f m a‖₊ : ℝ≥0∞)^p) ∂μ), { rw [snorm', h_no_pow], }, refine lintegral_congr_ae (h_lim.mono (λ a ha, _)), rw tendsto.liminf_eq, simp_rw [ennreal.coe_rpow_of_nonneg _ hp_nonneg, ennreal.tendsto_coe], refine ((nnreal.continuous_rpow_const hp_nonneg).tendsto (‖f_lim a‖₊)).comp _, exact (continuous_nnnorm.tendsto (f_lim a)).comp ha, end lemma snorm'_lim_le_liminf_snorm' {E} [normed_add_comm_group E] {f : ℕ → α → E} {p : ℝ} (hp_pos : 0 < p) (hf : ∀ n, ae_strongly_measurable (f n) μ) {f_lim : α → E} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ ≤ at_top.liminf (λ n, snorm' (f n) p μ) := begin rw snorm'_lim_eq_lintegral_liminf hp_pos.le h_lim, rw [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], refine (lintegral_liminf_le' (λ m, ((hf m).ennnorm.pow_const _))).trans_eq _, have h_pow_liminf : at_top.liminf (λ n, snorm' (f n) p μ) ^ p = at_top.liminf (λ n, (snorm' (f n) p μ) ^ p), { have h_rpow_mono := ennreal.strict_mono_rpow_of_pos hp_pos, have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_pow_liminf, simp_rw [snorm', ← ennreal.rpow_mul, one_div, inv_mul_cancel hp_pos.ne.symm, ennreal.rpow_one], end lemma snorm_exponent_top_lim_eq_ess_sup_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ = ess_sup (λ x, at_top.liminf (λ m, (‖f m x‖₊ : ℝ≥0∞))) μ := begin rw [snorm_exponent_top, snorm_ess_sup], refine ess_sup_congr_ae (h_lim.mono (λ x hx, _)), rw tendsto.liminf_eq, rw ennreal.tendsto_coe, exact (continuous_nnnorm.tendsto (f_lim x)).comp hx, end lemma snorm_exponent_top_lim_le_liminf_snorm_exponent_top {ι} [nonempty ι] [countable ι] [linear_order ι] {f : ι → α → F} {f_lim : α → F} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ ≤ at_top.liminf (λ n, snorm (f n) ∞ μ) := begin rw snorm_exponent_top_lim_eq_ess_sup_liminf h_lim, simp_rw [snorm_exponent_top, snorm_ess_sup], exact ennreal.ess_sup_liminf_le (λ n, (λ x, (‖f n x‖₊ : ℝ≥0∞))), end lemma snorm_lim_le_liminf_snorm {E} [normed_add_comm_group E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim p μ ≤ at_top.liminf (λ n, snorm (f n) p μ) := begin by_cases hp0 : p = 0, { simp [hp0], }, rw ← ne.def at hp0, by_cases hp_top : p = ∞, { simp_rw [hp_top], exact snorm_exponent_top_lim_le_liminf_snorm_exponent_top h_lim, }, simp_rw snorm_eq_snorm' hp0 hp_top, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0 hp_top, exact snorm'_lim_le_liminf_snorm' hp_pos hf h_lim, end /-! ### `Lp` is complete iff Cauchy sequences of `ℒp` have limits in `ℒp` -/ lemma tendsto_Lp_iff_tendsto_ℒp' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : Lp E p μ) : fi.tendsto f (𝓝 f_lim) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_iff_dist_tendsto_zero, simp_rw dist_def, rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end lemma tendsto_Lp_iff_tendsto_ℒp {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_Lp_iff_tendsto_ℒp', suffices h_eq : (λ n, snorm (f n - mem_ℒp.to_Lp f_lim f_lim_ℒp) p μ) = (λ n, snorm (f n - f_lim) p μ), by rw h_eq, exact funext (λ n, snorm_congr_ae (eventually_eq.rfl.sub (mem_ℒp.coe_fn_to_Lp f_lim_ℒp))), end lemma tendsto_Lp_iff_tendsto_ℒp'' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → α → E) (f_ℒp : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto (λ n, (f_ℒp n).to_Lp (f n)) (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin convert Lp.tendsto_Lp_iff_tendsto_ℒp' _ _, ext1 n, apply snorm_congr_ae, filter_upwards [((f_ℒp n).sub f_lim_ℒp).coe_fn_to_Lp, Lp.coe_fn_sub ((f_ℒp n).to_Lp (f n)) (f_lim_ℒp.to_Lp f_lim)] with _ hx₁ hx₂, rw ← hx₂, exact hx₁.symm, end lemma tendsto_Lp_of_tendsto_ℒp {ι} {fi : filter ι} [hp : fact (1 ≤ p)] {f : ι → Lp E p μ} (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) (h_tendsto : fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) := (tendsto_Lp_iff_tendsto_ℒp f f_lim f_lim_ℒp).mpr h_tendsto lemma cauchy_seq_Lp_iff_cauchy_seq_ℒp {ι} [nonempty ι] [semilattice_sup ι] [hp : fact (1 ≤ p)] (f : ι → Lp E p μ) : cauchy_seq f ↔ tendsto (λ (n : ι × ι), snorm (f n.fst - f n.snd) p μ) at_top (𝓝 0) := begin simp_rw [cauchy_seq_iff_tendsto_dist_at_top_0, dist_def], rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact snorm_ne_top _, end lemma complete_space_Lp_of_cauchy_complete_ℒp [hp : fact (1 ≤ p)] (H : ∀ (f : ℕ → α → E) (hf : ∀ n, mem_ℒp (f n) p μ) (B : ℕ → ℝ≥0∞) (hB : ∑' i, B i < ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N), ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : complete_space (Lp E p μ) := begin let B := λ n : ℕ, ((1:ℝ) / 2) ^ n, have hB_pos : ∀ n, 0 < B n, from λ n, pow_pos (div_pos zero_lt_one zero_lt_two) n, refine metric.complete_of_convergent_controlled_sequences B hB_pos (λ f hf, _), rsuffices ⟨f_lim, hf_lim_meas, h_tendsto⟩ : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), { exact ⟨hf_lim_meas.to_Lp f_lim, tendsto_Lp_of_tendsto_ℒp f_lim hf_lim_meas h_tendsto⟩, }, have hB : summable B, from summable_geometric_two, cases hB with M hB, let B1 := λ n, ennreal.of_real (B n), have hB1_has : has_sum B1 (ennreal.of_real M), { have h_tsum_B1 : ∑' i, B1 i = (ennreal.of_real M), { change (∑' (n : ℕ), ennreal.of_real (B n)) = ennreal.of_real M, rw ←hB.tsum_eq, exact (ennreal.of_real_tsum_of_nonneg (λ n, le_of_lt (hB_pos n)) hB.summable).symm, }, have h_sum := (@ennreal.summable _ B1).has_sum, rwa h_tsum_B1 at h_sum, }, have hB1 : ∑' i, B1 i < ∞, by {rw hB1_has.tsum_eq, exact ennreal.of_real_lt_top, }, let f1 : ℕ → α → E := λ n, f n, refine H f1 (λ n, Lp.mem_ℒp (f n)) B1 hB1 (λ N n m hn hm, _), specialize hf N n m hn hm, rw dist_def at hf, simp_rw [f1, B1], rwa ennreal.lt_of_real_iff_to_real_lt, rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end /-! ### Prove that controlled Cauchy sequences of `ℒp` have limits in `ℒp` -/ private lemma snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) (n : ℕ) : snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i := begin let f_norm_diff := λ i x, ‖f (i + 1) x - f i x‖, have hgf_norm_diff : ∀ n, (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) = ∑ i in finset.range (n + 1), f_norm_diff i, from λ n, funext (λ x, by simp [f_norm_diff]), rw hgf_norm_diff, refine (snorm'_sum_le (λ i _, ((hf (i+1)).sub (hf i)).norm) hp1).trans _, simp_rw [←pi.sub_apply, snorm'_norm], refine (finset.sum_le_sum _).trans (sum_le_tsum _ (λ m _, zero_le _) ennreal.summable), exact λ m _, (h_cau m (m + 1) m (nat.le_succ m) (le_refl m)).le, end private lemma lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (n : ℕ) (hn : snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i) : ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, rw [←one_div_one_div p, @ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [hp_pos]), one_div_one_div p], simp_rw snorm' at hn, have h_nnnorm_nonneg : (λ a, (‖∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖‖₊ : ℝ≥0∞) ^ p) = λ a, (∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)) ^ p, { ext1 a, congr, simp_rw ←of_real_norm_eq_coe_nnnorm, rw ←ennreal.of_real_sum_of_nonneg, { rw real.norm_of_nonneg _, exact finset.sum_nonneg (λ x hx, norm_nonneg _), }, { exact λ x hx, norm_nonneg _, }, }, change (∫⁻ a, (λ x, ↑‖∑ i in finset.range (n + 1), ‖f (i+1) x - f i x‖‖₊^p) a ∂μ)^(1/p) ≤ ∑' i, B i at hn, rwa h_nnnorm_nonneg at hn, end private lemma lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p) : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, suffices h_pow : ∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, by rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], have h_tsum_1 : ∀ g : ℕ → ℝ≥0∞, ∑' i, g i = at_top.liminf (λ n, ∑ i in finset.range (n + 1), g i), by { intro g, rw [ennreal.tsum_eq_liminf_sum_nat, ← liminf_nat_add _ 1], }, simp_rw h_tsum_1 _, rw ← h_tsum_1, have h_liminf_pow : ∫⁻ a, at_top.liminf (λ n, ∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊))^p ∂μ = ∫⁻ a, at_top.liminf (λ n, (∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊))^p) ∂μ, { refine lintegral_congr (λ x, _), have h_rpow_mono := ennreal.strict_mono_rpow_of_pos (zero_lt_one.trans_le hp1), have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_liminf_pow, refine (lintegral_liminf_le' _).trans _, { exact λ n, (finset.ae_measurable_sum (finset.range (n+1)) (λ i _, ((hf (i+1)).sub (hf i)).ennnorm)).pow_const _, }, { exact liminf_le_of_frequently_le' (frequently_of_forall h), }, end private lemma tsum_nnnorm_sub_ae_lt_top {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i) : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞) < ∞ := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, have h_integral : ∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ < ∞, { have h_tsum_lt_top : (∑' i, B i) ^ p < ∞, from ennreal.rpow_lt_top_of_nonneg hp_pos.le hB, refine lt_of_le_of_lt _ h_tsum_lt_top, rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div] at h, }, have rpow_ae_lt_top : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞)^p < ∞, { refine ae_lt_top' (ae_measurable.pow_const _ _) h_integral.ne, exact ae_measurable.ennreal_tsum (λ n, ((hf (n+1)).sub (hf n)).ennnorm), }, refine rpow_ae_lt_top.mono (λ x hx, _), rwa [←ennreal.lt_rpow_one_div_iff hp_pos, ennreal.top_rpow_of_pos (by simp [hp_pos] : 0 < 1 / p)] at hx, end lemma ae_tendsto_of_cauchy_snorm' [complete_space E] {f : ℕ → α → E} {p : ℝ} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin have h_summable : ∀ᵐ x ∂μ, summable (λ (i : ℕ), f (i + 1) x - f i x), { have h1 : ∀ n, snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i, from snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' hf hp1 h_cau, have h2 : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, from λ n, lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum hf hp1 n (h1 n), have h3 : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i, from lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum hf hp1 h2, have h4 : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞) < ∞, from tsum_nnnorm_sub_ae_lt_top hf hp1 hB h3, exact h4.mono (λ x hx, summable_of_summable_nnnorm (ennreal.tsum_coe_ne_top_iff_summable.mp (lt_top_iff_ne_top.mp hx))), }, have h : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) (𝓝 l), { refine h_summable.mono (λ x hx, _), let hx_sum := hx.has_sum.tendsto_sum_nat, exact ⟨∑' i, (f (i + 1) x - f i x), hx_sum⟩, }, refine h.mono (λ x hx, _), cases hx with l hx, have h_rw_sum : (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) = λ n, f n x - f 0 x, { ext1 n, change ∑ (i : ℕ) in finset.range n, ((λ m, f m x) (i + 1) - (λ m, f m x) i) = f n x - f 0 x, rw finset.sum_range_sub, }, rw h_rw_sum at hx, have hf_rw : (λ n, f n x) = λ n, f n x - f 0 x + f 0 x, by { ext1 n, abel, }, rw hf_rw, exact ⟨l + f 0 x, tendsto.add_const _ hx⟩, end lemma ae_tendsto_of_cauchy_snorm [complete_space E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin by_cases hp_top : p = ∞, { simp_rw [hp_top] at *, have h_cau_ae : ∀ᵐ x ∂μ, ∀ N n m, N ≤ n → N ≤ m → (‖(f n - f m) x‖₊ : ℝ≥0∞) < B N, { simp_rw ae_all_iff, exact λ N n m hnN hmN, ae_lt_of_ess_sup_lt (h_cau N n m hnN hmN), }, simp_rw [snorm_exponent_top, snorm_ess_sup] at h_cau, refine h_cau_ae.mono (λ x hx, cauchy_seq_tendsto_of_complete _), refine cauchy_seq_of_le_tendsto_0 (λ n, (B n).to_real) _ _, { intros n m N hnN hmN, specialize hx N n m hnN hmN, rw [dist_eq_norm, ←ennreal.to_real_of_real (norm_nonneg _), ennreal.to_real_le_to_real ennreal.of_real_ne_top (ennreal.ne_top_of_tsum_ne_top hB N)], rw ←of_real_norm_eq_coe_nnnorm at hx, exact hx.le, }, { rw ← ennreal.zero_to_real, exact tendsto.comp (ennreal.tendsto_to_real ennreal.zero_ne_top) (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB), }, }, have hp1 : 1 ≤ p.to_real, { rw [← ennreal.of_real_le_iff_le_to_real hp_top, ennreal.of_real_one], exact hp, }, have h_cau' : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) (p.to_real) μ < B N, { intros N n m hn hm, specialize h_cau N n m hn hm, rwa snorm_eq_snorm' (zero_lt_one.trans_le hp).ne.symm hp_top at h_cau, }, exact ae_tendsto_of_cauchy_snorm' hf hp1 hB h_cau', end lemma cauchy_tendsto_of_tendsto {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw ennreal.tendsto_at_top_zero, intros ε hε, have h_B : ∃ (N : ℕ), B N ≤ ε, { suffices h_tendsto_zero : ∃ (N : ℕ), ∀ n : ℕ, N ≤ n → B n ≤ ε, from ⟨h_tendsto_zero.some, h_tendsto_zero.some_spec _ le_rfl⟩, exact (ennreal.tendsto_at_top_zero.mp (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB)) ε hε, }, cases h_B with N h_B, refine ⟨N, λ n hn, _⟩, have h_sub : snorm (f n - f_lim) p μ ≤ at_top.liminf (λ m, snorm (f n - f m) p μ), { refine snorm_lim_le_liminf_snorm (λ m, (hf n).sub (hf m)) (f n - f_lim) _, refine h_lim.mono (λ x hx, _), simp_rw sub_eq_add_neg, exact tendsto.add tendsto_const_nhds (tendsto.neg hx), }, refine h_sub.trans _, refine liminf_le_of_frequently_le' (frequently_at_top.mpr _), refine λ N1, ⟨max N N1, le_max_right _ _, _⟩, exact (h_cau N n (max N N1) hn (le_max_left _ _)).le.trans h_B, end lemma mem_ℒp_of_cauchy_tendsto (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (h_lim_meas : ae_strongly_measurable f_lim μ) (h_tendsto : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : mem_ℒp f_lim p μ := begin refine ⟨h_lim_meas, _⟩, rw ennreal.tendsto_at_top_zero at h_tendsto, cases (h_tendsto 1 zero_lt_one) with N h_tendsto_1, specialize h_tendsto_1 N (le_refl N), have h_add : f_lim = f_lim - f N + f N, by abel, rw h_add, refine lt_of_le_of_lt (snorm_add_le (h_lim_meas.sub (hf N).1) (hf N).1 hp) _, rw ennreal.add_lt_top, split, { refine lt_of_le_of_lt _ ennreal.one_lt_top, have h_neg : f_lim - f N = -(f N - f_lim), by simp, rwa [h_neg, snorm_neg], }, { exact (hf N).2, }, end lemma cauchy_complete_ℒp [complete_space E] (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin obtain ⟨f_lim, h_f_lim_meas, h_lim⟩ : ∃ (f_lim : α → E) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (nhds (f_lim x)), from exists_strongly_measurable_limit_of_tendsto_ae (λ n, (hf n).1) (ae_tendsto_of_cauchy_snorm (λ n, (hf n).1) hp hB h_cau), have h_tendsto' : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), from cauchy_tendsto_of_tendsto (λ m, (hf m).1) f_lim hB h_cau h_lim, have h_ℒp_lim : mem_ℒp f_lim p μ, from mem_ℒp_of_cauchy_tendsto hp hf f_lim h_f_lim_meas.ae_strongly_measurable h_tendsto', exact ⟨f_lim, h_ℒp_lim, h_tendsto'⟩, end /-! ### `Lp` is complete for `1 ≤ p` -/ instance [complete_space E] [hp : fact (1 ≤ p)] : complete_space (Lp E p μ) := complete_space_Lp_of_cauchy_complete_ℒp $ λ f hf B hB h_cau, cauchy_complete_ℒp hp.elim hf hB.ne h_cau end Lp end measure_theory end complete_space /-! ### Continuous functions in `Lp` -/ open_locale bounded_continuous_function open bounded_continuous_function section variables [topological_space α] [borel_space α] [second_countable_topology_either α E] variables (E p μ) /-- An additive subgroup of `Lp E p μ`, consisting of the equivalence classes which contain a bounded continuous representative. -/ def measure_theory.Lp.bounded_continuous_function : add_subgroup (Lp E p μ) := add_subgroup.add_subgroup_of ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)).range (Lp E p μ) variables {E p μ} /-- By definition, the elements of `Lp.bounded_continuous_function E p μ` are the elements of `Lp E p μ` which contain a bounded continuous representative. -/ lemma measure_theory.Lp.mem_bounded_continuous_function_iff {f : (Lp E p μ)} : f ∈ measure_theory.Lp.bounded_continuous_function E p μ ↔ ∃ f₀ : (α →ᵇ E), f₀.to_continuous_map.to_ae_eq_fun μ = (f : α →ₘ[μ] E) := add_subgroup.mem_add_subgroup_of namespace bounded_continuous_function variables [is_finite_measure μ] /-- A bounded continuous function on a finite-measure space is in `Lp`. -/ lemma mem_Lp (f : α →ᵇ E) : f.to_continuous_map.to_ae_eq_fun μ ∈ Lp E p μ := begin refine Lp.mem_Lp_of_ae_bound (‖f‖) _, filter_upwards [f.to_continuous_map.coe_fn_to_ae_eq_fun μ] with x _, convert f.norm_coe_le_norm x end /-- The `Lp`-norm of a bounded continuous function is at most a constant (depending on the measure of the whole space) times its sup-norm. -/ lemma Lp_nnnorm_le (f : α →ᵇ E) : ‖(⟨f.to_continuous_map.to_ae_eq_fun μ, mem_Lp f⟩ : Lp E p μ)‖₊ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ‖f‖₊ := begin apply Lp.nnnorm_le_of_ae_bound, refine (f.to_continuous_map.coe_fn_to_ae_eq_fun μ).mono _, intros x hx, rw [←nnreal.coe_le_coe, coe_nnnorm, coe_nnnorm], convert f.norm_coe_le_norm x, end /-- The `Lp`-norm of a bounded continuous function is at most a constant (depending on the measure of the whole space) times its sup-norm. -/ lemma Lp_norm_le (f : α →ᵇ E) : ‖(⟨f.to_continuous_map.to_ae_eq_fun μ, mem_Lp f⟩ : Lp E p μ)‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ‖f‖ := Lp_nnnorm_le f variables (p μ) /-- The normed group homomorphism of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp_hom [fact (1 ≤ p)] : normed_add_group_hom (α →ᵇ E) (Lp E p μ) := { bound' := ⟨_, Lp_norm_le⟩, .. add_monoid_hom.cod_restrict ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (Lp E p μ) mem_Lp } lemma range_to_Lp_hom [fact (1 ≤ p)] : ((to_Lp_hom p μ).range : add_subgroup (Lp E p μ)) = measure_theory.Lp.bounded_continuous_function E p μ := begin symmetry, convert add_monoid_hom.add_subgroup_of_range_eq_of_le ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (by { rintros - ⟨f, rfl⟩, exact mem_Lp f } : _ ≤ Lp E p μ), end variables (𝕜 : Type*) [fact (1 ≤ p)] /-- The bounded linear map of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : (α →ᵇ E) →L[𝕜] (Lp E p μ) := linear_map.mk_continuous (linear_map.cod_restrict (Lp.Lp_submodule E p μ 𝕜) ((continuous_map.to_ae_eq_fun_linear_map μ).comp (to_continuous_map_linear_map α E 𝕜)) mem_Lp) _ Lp_norm_le lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : α →ᵇ E) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : ((linear_map.range (to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] Lp E p μ)).to_add_subgroup) = measure_theory.Lp.bounded_continuous_function E p μ := range_to_Lp_hom p μ variables {p} lemma to_Lp_norm_le [nontrivially_normed_field 𝕜] [normed_space 𝕜 E]: ‖(to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := linear_map.mk_continuous_norm_le _ ((measure_univ_nnreal μ) ^ (p.to_real)⁻¹).coe_nonneg _ lemma to_Lp_inj {f g : α →ᵇ E} [μ.is_open_pos_measure] [normed_field 𝕜] [normed_space 𝕜 E] : to_Lp p μ 𝕜 f = to_Lp p μ 𝕜 g ↔ f = g := begin refine ⟨λ h, _, by tauto⟩, rw [←fun_like.coe_fn_eq, ←(map_continuous f).ae_eq_iff_eq μ (map_continuous g)], refine (coe_fn_to_Lp p μ 𝕜 f).symm.trans (eventually_eq.trans _ $ coe_fn_to_Lp p μ 𝕜 g), rw h, end lemma to_Lp_injective [μ.is_open_pos_measure] [normed_field 𝕜] [normed_space 𝕜 E] : function.injective ⇑(to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ)) := λ f g hfg, (to_Lp_inj μ).mp hfg end bounded_continuous_function namespace continuous_map variables [compact_space α] [is_finite_measure μ] variables (𝕜 : Type*) (p μ) [fact (1 ≤ p)] /-- The bounded linear map of considering a continuous function on a compact finite-measure space `α` as an element of `Lp`. By definition, the norm on `C(α, E)` is the sup-norm, transferred from the space `α →ᵇ E` of bounded continuous functions, so this construction is just a matter of transferring the structure from `bounded_continuous_function.to_Lp` along the isometry. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : C(α, E) →L[𝕜] (Lp E p μ) := (bounded_continuous_function.to_Lp p μ 𝕜).comp (linear_isometry_bounded_of_compact α E 𝕜).to_linear_isometry.to_continuous_linear_map variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : (linear_map.range (to_Lp p μ 𝕜 : C(α, E) →L[𝕜] Lp E p μ)).to_add_subgroup = measure_theory.Lp.bounded_continuous_function E p μ := begin refine set_like.ext' _, have := (linear_isometry_bounded_of_compact α E 𝕜).surjective, convert function.surjective.range_comp this (bounded_continuous_function.to_Lp p μ 𝕜), rw ←bounded_continuous_function.range_to_Lp p μ, refl, end variables {p} lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ lemma to_Lp_def [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f = bounded_continuous_function.to_Lp p μ 𝕜 (linear_isometry_bounded_of_compact α E 𝕜 f) := rfl @[simp] lemma to_Lp_comp_to_continuous_map [normed_field 𝕜] [normed_space 𝕜 E] (f : α →ᵇ E) : to_Lp p μ 𝕜 f.to_continuous_map = bounded_continuous_function.to_Lp p μ 𝕜 f := rfl @[simp] lemma coe_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : (to_Lp p μ 𝕜 f : α →ₘ[μ] E) = f.to_ae_eq_fun μ := rfl lemma to_Lp_injective [μ.is_open_pos_measure] [normed_field 𝕜] [normed_space 𝕜 E] : function.injective ⇑(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ)) := (bounded_continuous_function.to_Lp_injective _).comp (linear_isometry_bounded_of_compact α E 𝕜).injective lemma to_Lp_inj {f g : C(α, E)} [μ.is_open_pos_measure] [normed_field 𝕜] [normed_space 𝕜 E] : to_Lp p μ 𝕜 f = to_Lp p μ 𝕜 g ↔ f = g := (to_Lp_injective μ).eq_iff variables {μ} /-- If a sum of continuous functions `g n` is convergent, and the same sum converges in `Lᵖ` to `h`, then in fact `g n` converges uniformly to `h`. -/ lemma has_sum_of_has_sum_Lp {β : Type*} [μ.is_open_pos_measure] [normed_field 𝕜] [normed_space 𝕜 E] {g : β → C(α, E)} {f : C(α, E)} (hg : summable g) (hg2 : has_sum (to_Lp p μ 𝕜 ∘ g) (to_Lp p μ 𝕜 f)) : has_sum g f := begin convert summable.has_sum hg, exact to_Lp_injective μ (hg2.unique ((to_Lp p μ 𝕜).has_sum $ summable.has_sum hg)), end variables (μ) [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_norm_eq_to_Lp_norm_coe : ‖(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))‖ = ‖(bounded_continuous_function.to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))‖ := continuous_linear_map.op_norm_comp_linear_isometry_equiv _ _ /-- Bound for the operator norm of `continuous_map.to_Lp`. -/ lemma to_Lp_norm_le : ‖(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := by { rw to_Lp_norm_eq_to_Lp_norm_coe, exact bounded_continuous_function.to_Lp_norm_le μ } end continuous_map end namespace measure_theory namespace Lp lemma pow_mul_meas_ge_le_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : (ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real}) ^ (1 / p.to_real) ≤ (ennreal.of_real ‖f‖) := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ pow_mul_meas_ge_le_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma mul_meas_ge_le_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real} ≤ (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε /-- A version of Markov's inequality with elements of Lp. -/ lemma mul_meas_ge_le_pow_norm' (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε ^ p.to_real * μ {x | ε ≤ ‖f x‖₊} ≤ (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma meas_ge_le_mul_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : μ {x | ε ≤ ‖f x‖₊} ≤ ε⁻¹ ^ p.to_real * (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ meas_ge_le_mul_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) hε end Lp end measure_theory
feffafc772c4fe65eee3ad608909edd9f6680a3f
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/category_theory/equivalence.lean
2a613441008c2b2d5c3fa89259ee43ff14cccf9b
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,539
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.fully_faithful import category_theory.whiskering import tactic.slice /-! # Equivalence of categories An equivalence of categories `C` and `D` is a pair of functors `F : C ⥤ D` and `G : D ⥤ C` such that `η : 𝟭 C ≅ F ⋙ G` and `ε : G ⋙ F ≅ 𝟭 D`. In many situations, equivalences are a better notion of "sameness" of categories than the stricter isomorphims of categories. Recall that one way to express that two functors `F : C ⥤ D` and `G : D ⥤ C` are adjoint is using two natural transformations `η : 𝟭 C ⟶ F ⋙ G` and `ε : G ⋙ F ⟶ 𝟭 D`, called the unit and the counit, such that the compositions `F ⟶ FGF ⟶ F` and `G ⟶ GFG ⟶ G` are the identity. Unfortunately, it is not the case that the natural isomorphisms `η` and `ε` in the definition of an equivalence automatically give an adjunction. However, it is true that * if one of the two compositions is the identity, then so is the other, and * given an equivalence of categories, it is always possible to refine `η` in such a way that the identities are satisfied. For this reason, in mathlib we define an equivalence to be a "half-adjoint equivalence", which is a tuple `(F, G, η, ε)` as in the first paragraph such that the composite `F ⟶ FGF ⟶ F` is the identity. By the remark above, this already implies that the tuple is an "adjoint equivalence", i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. We also define essentially surjective functors and show that a functor is an equivalence if and only if it is full, faithful and essentially surjective. ## Main definitions * `equivalence`: bundled (half-)adjoint equivalences of categories * `is_equivalence`: type class on a functor `F` containing the data of the inverse `G` as well as the natural isomorphisms `η` and `ε`. * `ess_surj`: type class on a functor `F` containing the data of the preimages and the isomorphisms `F.obj (preimage d) ≅ d`. ## Main results * `equivalence.mk`: upgrade an equivalence to a (half-)adjoint equivalence * `equivalence_of_fully_faithfully_ess_surj`: a fully faithful essentially surjective functor is an equivalence. ## Notations We write `C ≌ D` (`\backcong`, not to be confused with `≅`/`\cong`) for a bundled equivalence. -/ namespace category_theory open category_theory.functor nat_iso category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. In `unit_inverse_comp`, we show that this is actually an adjoint equivalence, i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. See https://stacks.math.columbia.edu/tag/001J -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace equivalence /-- The unit of an equivalence of categories. -/ abbreviation unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom /-- The counit of an equivalence of categories. -/ abbreviation counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom /-- The inverse of the unit of an equivalence of categories. -/ abbreviation unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv /-- The inverse of the counit of an equivalence of categories. -/ abbreviation counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv /- While these abbreviations are convenient, they also cause some trouble, preventing structure projections from unfolding. -/ @[simp] lemma equivalence_mk'_unit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit = unit_iso.hom := rfl @[simp] lemma equivalence_mk'_counit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit = counit_iso.hom := rfl @[simp] lemma equivalence_mk'_unit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit_inv = unit_iso.inv := rfl @[simp] lemma equivalence_mk'_counit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit_inv = counit_iso.inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma counit_inv_app_functor (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) = e.functor.map (e.unit.app X) := by { symmetry, erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_app_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_app_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma unit_inv_app_inverse (e : C ≌ D) (Y : D) : e.unit_inv.app (e.inverse.obj Y) = e.inverse.map (e.counit.app Y) := by { symmetry, erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) /-- If `η : 𝟭 C ≅ F ⋙ G` is part of a (not necessarily half-adjoint) equivalence, we can upgrade it to a refined natural isomorphism `adjointify_η η : 𝟭 C ≅ F ⋙ G` which exhibits the properties required for a half-adjoint equivalence. See `equivalence.mk`. -/ def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end /-- Every equivalence of categories consisting of functors `F` and `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors can be transformed into a half-adjoint equivalence without changing `F` or `G`. -/ protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ /-- Equivalence of categories is reflexive. -/ @[refl, simps] def refl : C ≌ C := ⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩ instance : inhabited (C ≌ C) := ⟨refl⟩ /-- Equivalence of categories is symmetric. -/ @[symm, simps] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [category.{v₃} E] /-- Equivalence of categories is transitive. -/ @[trans, simps] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := { functor := e.functor ⋙ f.functor, inverse := f.inverse ⋙ e.inverse, unit_iso := begin refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) , end, counit_iso := begin refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) end, -- We wouldn't have needed to give this proof if we'd used `equivalence.mk`, -- but we choose to avoid using that here, for the sake of good structure projection `simp` lemmas. functor_unit_iso_comp' := λ X, begin dsimp, rw [← f.functor.map_comp_assoc, e.functor.map_comp, ←counit_inv_app_functor, fun_inv_map, iso.inv_hom_id_app_assoc, assoc, iso.inv_hom_id_app, counit_app_functor, ← functor.map_comp], erw [comp_id, iso.hom_inv_id_app, functor.map_id], end } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } /-- If `C` is equivalent to `D`, then `C ⥤ E` is equivalent to `D ⥤ E`. -/ @[simps {rhs_md:=semireducible}] def congr_left (e : C ≌ D) : (C ⥤ E) ≌ (D ⥤ E) := equivalence.mk ((whiskering_left _ _ _).obj e.inverse) ((whiskering_left _ _ _).obj e.functor) (nat_iso.of_components (λ F, (e.fun_inv_id_assoc F).symm) (by tidy)) (nat_iso.of_components (λ F, e.inv_fun_id_assoc F) (by tidy)) /-- If `C` is equivalent to `D`, then `E ⥤ C` is equivalent to `E ⥤ D`. -/ @[simps {rhs_md:=semireducible}] def congr_right (e : C ≌ D) : (E ⥤ C) ≌ (E ⥤ D) := equivalence.mk ((whiskering_right _ _ _).obj e.functor) ((whiskering_right _ _ _).obj e.inverse) (nat_iso.of_components (λ F, F.right_unitor.symm ≪≫ iso_whisker_left F e.unit_iso ≪≫ functor.associator _ _ _) (by tidy)) (nat_iso.of_components (λ F, functor.associator _ _ _ ≪≫ iso_whisker_left F e.counit_iso ≪≫ F.right_unitor) (by tidy)) section cancellation_lemmas variables (e : C ≌ D) -- We need special forms of `cancel_nat_iso_hom_right(_assoc)` and `cancel_nat_iso_inv_right(_assoc)` -- for units and counits, because neither `simp` or `rw` will apply those lemmas in this -- setting without providing `e.unit_iso` (or similar) as an explicit argument. -- We also provide the lemmas for length four compositions, since they're occasionally useful. -- (e.g. in proving that equivalences take monos to monos) @[simp] lemma cancel_unit_right {X Y : C} (f f' : X ⟶ Y) : f ≫ e.unit.app Y = f' ≫ e.unit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_inv_right {X Y : C} (f f' : X ⟶ e.inverse.obj (e.functor.obj Y)) : f ≫ e.unit_inv.app Y = f' ≫ e.unit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_right {X Y : D} (f f' : X ⟶ e.functor.obj (e.inverse.obj Y)) : f ≫ e.counit.app Y = f' ≫ e.counit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_inv_right {X Y : D} (f f' : X ⟶ Y) : f ≫ e.counit_inv.app Y = f' ≫ e.counit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_right_assoc {W X X' Y : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.unit.app Y = f' ≫ g' ≫ e.unit.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc {W X X' Y : D} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.counit_inv.app Y = f' ≫ g' ≫ e.counit_inv.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_unit_right_assoc' {W X X' Y Y' Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.unit.app Z = f' ≫ g' ≫ h' ≫ e.unit.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc' {W X X' Y Y' Z : D} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.counit_inv.app Z = f' ≫ g' ≫ h' ≫ e.counit_inv.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] end cancellation_lemmas section -- There's of course a monoid structure on `C ≌ C`, -- but let's not encourage using it. -- The power structure is nevertheless useful. /-- Powers of an auto-equivalence. -/ def pow (e : C ≌ C) : ℤ → (C ≌ C) | (int.of_nat 0) := equivalence.refl | (int.of_nat 1) := e | (int.of_nat (n+2)) := e.trans (pow (int.of_nat (n+1))) | (int.neg_succ_of_nat 0) := e.symm | (int.neg_succ_of_nat (n+1)) := e.symm.trans (pow (int.neg_succ_of_nat n)) instance : has_pow (C ≌ C) ℤ := ⟨pow⟩ @[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl @[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl @[simp] lemma pow_minus_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl -- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`. -- At this point, we haven't even defined the category of equivalences. end end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence /-- To see that a functor is an equivalence, it suffices to provide an inverse functor `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors. -/ protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor /-- Interpret a functor that is an equivalence as an equivalence. -/ def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso, is_equivalence.functor_unit_iso_comp⟩ instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl /-- The inverse functor of a functor that is an equivalence. -/ def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm @[simp] lemma as_equivalence_functor (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.functor = F := rfl @[simp] lemma as_equivalence_inverse (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.inverse = inv F := rfl @[simp] lemma inv_inv (F : C ⥤ D) [is_equivalence F] : inv (inv F) = F := rfl /-- The composition of functor that is an equivalence with its inverse is naturally isomorphic to the identity functor. -/ def fun_inv_id (F : C ⥤ D) [is_equivalence F] : F ⋙ F.inv ≅ 𝟭 C := is_equivalence.unit_iso.symm /-- The composition of functor that is an equivalence with its inverse is naturally isomorphic to the identity functor. -/ def inv_fun_id (F : C ⥤ D) [is_equivalence F] : F.inv ⋙ F ≅ 𝟭 D := is_equivalence.counit_iso variables {E : Type u₃} [category.{v₃} E] instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace equivalence @[simp] lemma functor_inv (E : C ≌ D) : E.functor.inv = E.inverse := rfl @[simp] lemma inverse_inv (E : C ≌ D) : E.inverse.inv = E.functor := rfl @[simp] lemma functor_as_equivalence (E : C ≌ D) : E.functor.as_equivalence = E := by { cases E, congr, } @[simp] lemma inverse_as_equivalence (E : C ≌ D) : E.inverse.as_equivalence = E.symm := by { cases E, congr, } end equivalence namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = F.inv_fun_id.hom.app X ≫ f ≫ F.inv_fun_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = F.fun_inv_id.hom.app X ≫ f ≫ F.fun_inv_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end -- We should probably restate many of the lemmas about `equivalence` for `is_equivalence`, -- but these are the only ones I need for now. @[simp] lemma functor_unit_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.map (E.fun_inv_id.inv.app Y) ≫ E.inv_fun_id.hom.app (E.obj Y) = 𝟙 _ := equivalence.functor_unit_comp E.as_equivalence Y @[simp] lemma inv_fun_id_inv_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.inv_fun_id.inv.app (E.obj Y) ≫ E.map (E.fun_inv_id.hom.app Y) = 𝟙 _ := eq_of_inv_eq_inv (functor_unit_comp _ _) end is_equivalence /-- A functor `F : C ⥤ D` is essentially surjective if for every `d : D`, there is some `c : C` so `F.obj c ≅ D`. See https://stacks.math.columbia.edu/tag/001C. -/ -- TODO should we make this a `Prop` that merely asserts the existence of a preimage, -- rather than choosing one? class ess_surj (F : C ⥤ D) := (obj_preimage (d : D) : C) (iso' (d : D) : F.obj (obj_preimage d) ≅ d . obviously) restate_axiom ess_surj.iso' /-- Applying an essentially surjective functor to a preimage of `d` yields an object that is isomorphic to `d`. -/ add_decl_doc ess_surj.iso namespace functor /-- Given an essentially surjective functor, we can find a preimage for every object `d` in the codomain. Applying the functor to this preimage will yield an object isomorphic to `d`, see `fun_obj_preimage_iso`. -/ def obj_preimage (F : C ⥤ D) [ess_surj F] (d : D) : C := ess_surj.obj_preimage.{v₁ v₂} F d /-- Applying an essentially surjective functor to a preimage of `d` yields an object that is isomorphic to `d`. -/ def fun_obj_preimage_iso (F : C ⥤ D) [ess_surj F] (d : D) : F.obj (F.obj_preimage d) ≅ d := ess_surj.iso d end functor namespace equivalence /-- An equivalence is essentially surjective. See https://stacks.math.columbia.edu/tag/02C3. -/ def ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨ λ Y : D, F.inv.obj Y, λ Y : D, (F.inv_fun_id.app Y) ⟩ /-- An equivalence is faithful. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { map_injective' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. /-- An equivalence is full. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, F.fun_inv_id.inv.app X ≫ F.inv.map f ≫ F.fun_inv_id.hom.app Y, witness' := λ X Y f, F.inv.map_injective (by simpa only [is_equivalence.inv_fun_map, assoc, iso.hom_inv_id_app_assoc, iso.hom_inv_id_app] using comp_id _) } @[simp] private def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.fun_obj_preimage_iso X).hom ≫ f ≫ (F.fun_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.map_injective, tidy end, map_comp' := λ X Y Z f g, by apply F.map_injective; simp } /-- A functor which is full, faithful, and essentially surjective is an equivalence. See https://stacks.math.columbia.edu/tag/02C3. -/ def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.fun_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.map_injective, obviously })) (nat_iso.of_components (λ Y, F.fun_obj_preimage_iso Y) (by obviously)) @[simp] lemma functor_map_inj_iff (e : C ≌ D) {X Y : C} (f g : X ⟶ Y) : e.functor.map f = e.functor.map g ↔ f = g := begin split, { intro w, apply e.functor.map_injective, exact w, }, { rintro ⟨rfl⟩, refl, } end @[simp] lemma inverse_map_inj_iff (e : C ≌ D) {X Y : D} (f g : X ⟶ Y) : e.inverse.map f = e.inverse.map g ↔ f = g := begin split, { intro w, apply e.inverse.map_injective, exact w, }, { rintro ⟨rfl⟩, refl, } end end equivalence end category_theory
790ba26b53c0a0cc211350d4e6dbc816814ec49c
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/topology/metric_space/gluing.lean
30b5b1d7e2ba28adc1d5f0082a658fb88c4aed34
[ "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
23,863
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Gluing metric spaces Authors: Sébastien Gouëzel -/ import topology.metric_space.isometry import topology.metric_space.premetric_space /-! # Metric space gluing Gluing two metric spaces along a common subset. Formally, we are given ``` Φ γ ---> α | |Ψ v β ``` where `hΦ : isometry Φ` and `hΨ : isometry Ψ`. We want to complete the square by a space `glue_space hΦ hΨ` and two isometries `to_glue_l hΦ hΨ` and `to_glue_r hΦ hΨ` that make the square commute. We start by defining a predistance on the disjoint union `α ⊕ β`, for which points `Φ p` and `Ψ p` are at distance 0. The (quotient) metric space associated to this predistance is the desired space. This is an instance of a more general construction, where `Φ` and `Ψ` do not have to be isometries, but the distances in the image almost coincide, up to `2ε` say. Then one can almost glue the two spaces so that the images of a point under `Φ` and `Ψ` are ε-close. If `ε > 0`, this yields a metric space structure on `α ⊕ β`, without the need to take a quotient. In particular, when `α` and `β` are inhabited, this gives a natural metric space structure on `α ⊕ β`, where the basepoints are at distance 1, say, and the distances between other points are obtained by going through the two basepoints. We also define the inductive limit of metric spaces. Given ``` f 0 f 1 f 2 f 3 X 0 -----> X 1 -----> X 2 -----> X 3 -----> ... ``` where the `X n` are metric spaces and `f n` isometric embeddings, we define the inductive limit of the `X n`, also known as the increasing union of the `X n` in this context, if we identify `X n` and `X (n+1)` through `f n`. This is a metric space in which all `X n` embed isometrically and in a way compatible with `f n`. -/ noncomputable theory universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function set premetric open_locale uniformity namespace metric section approx_gluing variables [metric_space α] [metric_space β] {Φ : γ → α} {Ψ : γ → β} {ε : ℝ} open sum (inl inr) /-- Define a predistance on α ⊕ β, for which Φ p and Ψ p are at distance ε -/ def glue_dist (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : α ⊕ β → α ⊕ β → ℝ | (inl x) (inl y) := dist x y | (inr x) (inr y) := dist x y | (inl x) (inr y) := infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε | (inr x) (inl y) := infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε private lemma glue_dist_self (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : ∀x, glue_dist Φ Ψ ε x x = 0 | (inl x) := dist_self _ | (inr x) := dist_self _ lemma glue_dist_glued_points [nonempty γ] (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (p : γ) : glue_dist Φ Ψ ε (inl (Φ p)) (inr (Ψ p)) = ε := begin have : infi (λq, dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q)) = 0, { have A : ∀q, 0 ≤ dist (Φ p) (Φ q) + dist (Ψ p) (Ψ q) := λq, by rw ← add_zero (0 : ℝ); exact add_le_add dist_nonneg dist_nonneg, refine le_antisymm _ (le_cinfi A), have : 0 = dist (Φ p) (Φ p) + dist (Ψ p) (Ψ p), by simp, rw this, exact cinfi_le ⟨0, forall_range_iff.2 A⟩ p }, rw [glue_dist, this, zero_add] end private lemma glue_dist_comm (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) : ∀x y, glue_dist Φ Ψ ε x y = glue_dist Φ Ψ ε y x | (inl x) (inl y) := dist_comm _ _ | (inr x) (inr y) := dist_comm _ _ | (inl x) (inr y) := rfl | (inr x) (inl y) := rfl variable [nonempty γ] private lemma glue_dist_triangle (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) : ∀x y z, glue_dist Φ Ψ ε x z ≤ glue_dist Φ Ψ ε x y + glue_dist Φ Ψ ε y z | (inl x) (inl y) (inl z) := dist_triangle _ _ _ | (inr x) (inr y) (inr z) := dist_triangle _ _ _ | (inr x) (inl y) (inl z) := begin have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) := λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩, unfold glue_dist, have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z, { have : infi (λp, dist y (Φ p) + dist x (Ψ p)) + dist y z = infi ((λt, t + dist y z) ∘ (λp, dist y (Φ p) + dist x (Ψ p))), { refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ (B _ _), intros x y hx, simpa }, rw [this, comp], refine cinfi_le_cinfi (B _ _) (λp, _), calc dist z (Φ p) + dist x (Ψ p) ≤ (dist y z + dist y (Φ p)) + dist x (Ψ p) : add_le_add (dist_triangle_left _ _ _) (le_refl _) ... = dist y (Φ p) + dist x (Ψ p) + dist y z : by ring }, linarith end | (inr x) (inr y) (inl z) := begin have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) := λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩, unfold glue_dist, have : infi (λp, dist z (Φ p) + dist x (Ψ p)) ≤ dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)), { have : dist x y + infi (λp, dist z (Φ p) + dist y (Ψ p)) = infi ((λt, dist x y + t) ∘ (λp, dist z (Φ p) + dist y (Ψ p))), { refine map_cinfi_of_continuous_at_of_monotone (continuous_at_const.add continuous_at_id) _ (B _ _), intros x y hx, simpa }, rw [this, comp], refine cinfi_le_cinfi (B _ _) (λp, _), calc dist z (Φ p) + dist x (Ψ p) ≤ dist z (Φ p) + (dist x y + dist y (Ψ p)) : add_le_add (le_refl _) (dist_triangle _ _ _) ... = dist x y + (dist z (Φ p) + dist y (Ψ p)) : by ring }, linarith end | (inl x) (inl y) (inr z) := begin have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) := λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩, unfold glue_dist, have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)), { have : dist x y + infi (λp, dist y (Φ p) + dist z (Ψ p)) = infi ((λt, dist x y + t) ∘ (λp, dist y (Φ p) + dist z (Ψ p))), { refine map_cinfi_of_continuous_at_of_monotone (continuous_at_const.add continuous_at_id) _ (B _ _), intros x y hx, simpa }, rw [this, comp], refine cinfi_le_cinfi (B _ _) (λp, _), calc dist x (Φ p) + dist z (Ψ p) ≤ (dist x y + dist y (Φ p)) + dist z (Ψ p) : add_le_add (dist_triangle _ _ _) (le_refl _) ... = dist x y + (dist y (Φ p) + dist z (Ψ p)) : by ring }, linarith end | (inl x) (inr y) (inr z) := begin have B : ∀a b, bdd_below (range (λ (p : γ), dist a (Φ p) + dist b (Ψ p))) := λa b, ⟨0, forall_range_iff.2 (λp, add_nonneg dist_nonneg dist_nonneg)⟩, unfold glue_dist, have : infi (λp, dist x (Φ p) + dist z (Ψ p)) ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z, { have : infi (λp, dist x (Φ p) + dist y (Ψ p)) + dist y z = infi ((λt, t + dist y z) ∘ (λp, dist x (Φ p) + dist y (Ψ p))), { refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ (B _ _), intros x y hx, simpa }, rw [this, comp], refine cinfi_le_cinfi (B _ _) (λp, _), calc dist x (Φ p) + dist z (Ψ p) ≤ dist x (Φ p) + (dist y z + dist y (Ψ p)) : add_le_add (le_refl _) (dist_triangle_left _ _ _) ... = dist x (Φ p) + dist y (Ψ p) + dist y z : by ring }, linarith end | (inl x) (inr y) (inl z) := real.le_of_forall_epsilon_le $ λδ δpos, begin have : ∃a ∈ range (λp, dist x (Φ p) + dist y (Ψ p)), a < infi (λp, dist x (Φ p) + dist y (Ψ p)) + δ/2 := exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith), rcases this with ⟨a, arange, ha⟩, rcases mem_range.1 arange with ⟨p, pa⟩, rw ← pa at ha, have : ∃b ∈ range (λp, dist z (Φ p) + dist y (Ψ p)), b < infi (λp, dist z (Φ p) + dist y (Ψ p)) + δ/2 := exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith), rcases this with ⟨b, brange, hb⟩, rcases mem_range.1 brange with ⟨q, qb⟩, rw ← qb at hb, have : dist (Φ p) (Φ q) ≤ dist (Ψ p) (Ψ q) + 2 * ε, { have := le_trans (le_abs_self _) (H p q), by linarith }, calc dist x z ≤ dist x (Φ p) + dist (Φ p) (Φ q) + dist (Φ q) z : dist_triangle4 _ _ _ _ ... ≤ dist x (Φ p) + dist (Ψ p) (Ψ q) + dist z (Φ q) + 2 * ε : by rw [dist_comm z]; linarith ... ≤ dist x (Φ p) + (dist y (Ψ p) + dist y (Ψ q)) + dist z (Φ q) + 2 * ε : add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _) ... ≤ (infi (λp, dist x (Φ p) + dist y (Ψ p)) + ε) + (infi (λp, dist z (Φ p) + dist y (Ψ p)) + ε) + δ : by linarith end | (inr x) (inl y) (inr z) := real.le_of_forall_epsilon_le $ λδ δpos, begin have : ∃a ∈ range (λp, dist y (Φ p) + dist x (Ψ p)), a < infi (λp, dist y (Φ p) + dist x (Ψ p)) + δ/2 := exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith), rcases this with ⟨a, arange, ha⟩, rcases mem_range.1 arange with ⟨p, pa⟩, rw ← pa at ha, have : ∃b ∈ range (λp, dist y (Φ p) + dist z (Ψ p)), b < infi (λp, dist y (Φ p) + dist z (Ψ p)) + δ/2 := exists_lt_of_cInf_lt (range_nonempty _) (by rw [infi]; linarith), rcases this with ⟨b, brange, hb⟩, rcases mem_range.1 brange with ⟨q, qb⟩, rw ← qb at hb, have : dist (Ψ p) (Ψ q) ≤ dist (Φ p) (Φ q) + 2 * ε, { have := le_trans (neg_le_abs_self _) (H p q), by linarith }, calc dist x z ≤ dist x (Ψ p) + dist (Ψ p) (Ψ q) + dist (Ψ q) z : dist_triangle4 _ _ _ _ ... ≤ dist x (Ψ p) + dist (Φ p) (Φ q) + dist z (Ψ q) + 2 * ε : by rw [dist_comm z]; linarith ... ≤ dist x (Ψ p) + (dist y (Φ p) + dist y (Φ q)) + dist z (Ψ q) + 2 * ε : add_le_add (add_le_add (add_le_add (le_refl _) (dist_triangle_left _ _ _)) (le_refl _)) (le_refl _) ... ≤ (infi (λp, dist y (Φ p) + dist x (Ψ p)) + ε) + (infi (λp, dist y (Φ p) + dist z (Ψ p)) + ε) + δ : by linarith end private lemma glue_eq_of_dist_eq_zero (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε) : ∀p q : α ⊕ β, glue_dist Φ Ψ ε p q = 0 → p = q | (inl x) (inl y) h := by rw eq_of_dist_eq_zero h | (inl x) (inr y) h := begin have : 0 ≤ infi (λp, dist x (Φ p) + dist y (Ψ p)) := le_cinfi (λp, by simpa using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)), have : 0 + ε ≤ glue_dist Φ Ψ ε (inl x) (inr y) := add_le_add this (le_refl ε), exfalso, linarith end | (inr x) (inl y) h := begin have : 0 ≤ infi (λp, dist y (Φ p) + dist x (Ψ p)) := le_cinfi (λp, by simpa [add_comm] using add_le_add (@dist_nonneg _ _ x _) (@dist_nonneg _ _ y _)), have : 0 + ε ≤ glue_dist Φ Ψ ε (inr x) (inl y) := add_le_add this (le_refl ε), exfalso, linarith end | (inr x) (inr y) h := by rw eq_of_dist_eq_zero h /-- Given two maps Φ and Ψ intro metric spaces α and β such that the distances between Φ p and Φ q, and between Ψ p and Ψ q, coincide up to 2 ε where ε > 0, one can almost glue the two spaces α and β along the images of Φ and Ψ, so that Φ p and Ψ p are at distance ε. -/ def glue_metric_approx (Φ : γ → α) (Ψ : γ → β) (ε : ℝ) (ε0 : 0 < ε) (H : ∀p q, abs (dist (Φ p) (Φ q) - dist (Ψ p) (Ψ q)) ≤ 2 * ε) : metric_space (α ⊕ β) := { dist := glue_dist Φ Ψ ε, dist_self := glue_dist_self Φ Ψ ε, dist_comm := glue_dist_comm Φ Ψ ε, dist_triangle := glue_dist_triangle Φ Ψ ε H, eq_of_dist_eq_zero := glue_eq_of_dist_eq_zero Φ Ψ ε ε0 } end approx_gluing section sum /- A particular case of the previous construction is when one uses basepoints in α and β and one glues only along the basepoints, putting them at distance 1. We give a direct definition of the distance, without infi, as it is easier to use in applications, and show that it is equal to the gluing distance defined above to take advantage of the lemmas we have already proved. -/ variables [metric_space α] [metric_space β] [inhabited α] [inhabited β] open sum (inl inr) /- Distance on a disjoint union. There are many (noncanonical) ways to put a distance compatible with each factor. If the two spaces are bounded, one can say for instance that each point in the first is at distance `diam α + diam β + 1` of each point in the second. Instead, we choose a construction that works for unbounded spaces, but requires basepoints. We embed isometrically each factor, set the basepoints at distance 1, arbitrarily, and say that the distance from `a` to `b` is the sum of the distances of `a` and `b` to their respective basepoints, plus the distance 1 between the basepoints. Since there is an arbitrary choice in this construction, it is not an instance by default. -/ def sum.dist : α ⊕ β → α ⊕ β → ℝ | (inl a) (inl a') := dist a a' | (inr b) (inr b') := dist b b' | (inl a) (inr b) := dist a (default α) + 1 + dist (default β) b | (inr b) (inl a) := dist b (default β) + 1 + dist (default α) a lemma sum.dist_eq_glue_dist {p q : α ⊕ β} : sum.dist p q = glue_dist (λ_ : unit, default α) (λ_ : unit, default β) 1 p q := by cases p; cases q; refl <|> simp [sum.dist, glue_dist, dist_comm, add_comm, add_left_comm] private lemma sum.dist_comm (x y : α ⊕ β) : sum.dist x y = sum.dist y x := by cases x; cases y; simp only [sum.dist, dist_comm, add_comm, add_left_comm] lemma sum.one_dist_le {x : α} {y : β} : 1 ≤ sum.dist (inl x) (inr y) := le_trans (le_add_of_nonneg_right dist_nonneg) $ add_le_add_right (le_add_of_nonneg_left dist_nonneg) _ lemma sum.one_dist_le' {x : α} {y : β} : 1 ≤ sum.dist (inr y) (inl x) := by rw sum.dist_comm; exact sum.one_dist_le private lemma sum.mem_uniformity (s : set ((α ⊕ β) × (α ⊕ β))) : s ∈ 𝓤 (α ⊕ β) ↔ ∃ ε > 0, ∀ a b, sum.dist a b < ε → (a, b) ∈ s := begin split, { rintro ⟨hsα, hsβ⟩, rcases mem_uniformity_dist.1 hsα with ⟨εα, εα0, hα⟩, rcases mem_uniformity_dist.1 hsβ with ⟨εβ, εβ0, hβ⟩, refine ⟨min (min εα εβ) 1, lt_min (lt_min εα0 εβ0) zero_lt_one, _⟩, rintro (a|a) (b|b) h, { exact hα (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_left _ _))) }, { cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le }, { cases not_le_of_lt (lt_of_lt_of_le h (min_le_right _ _)) sum.one_dist_le' }, { exact hβ (lt_of_lt_of_le h (le_trans (min_le_left _ _) (min_le_right _ _))) } }, { rintro ⟨ε, ε0, H⟩, split; rw [filter.mem_sets, filter.mem_map, mem_uniformity_dist]; exact ⟨ε, ε0, λ x y h, H _ _ (by exact h)⟩ } end /-- The distance on the disjoint union indeed defines a metric space. All the distance properties follow from our choice of the distance. The harder work is to show that the uniform structure defined by the distance coincides with the disjoint union uniform structure. -/ def metric_space_sum : metric_space (α ⊕ β) := { dist := sum.dist, dist_self := λx, by cases x; simp only [sum.dist, dist_self], dist_comm := sum.dist_comm, dist_triangle := λp q r, by simp only [dist, sum.dist_eq_glue_dist]; exact glue_dist_triangle _ _ _ (by simp; norm_num) _ _ _, eq_of_dist_eq_zero := λp q, by simp only [dist, sum.dist_eq_glue_dist]; exact glue_eq_of_dist_eq_zero _ _ _ zero_lt_one _ _, to_uniform_space := sum.uniform_space, uniformity_dist := uniformity_dist_of_mem_uniformity _ _ sum.mem_uniformity } local attribute [instance] metric_space_sum lemma sum.dist_eq {x y : α ⊕ β} : dist x y = sum.dist x y := rfl /-- The left injection of a space in a disjoint union in an isometry -/ lemma isometry_on_inl : isometry (sum.inl : α → (α ⊕ β)) := isometry_emetric_iff_metric.2 $ λx y, rfl /-- The right injection of a space in a disjoint union in an isometry -/ lemma isometry_on_inr : isometry (sum.inr : β → (α ⊕ β)) := isometry_emetric_iff_metric.2 $ λx y, rfl end sum section gluing /- Exact gluing of two metric spaces along isometric subsets. -/ variables [nonempty γ] [metric_space γ] [metric_space α] [metric_space β] {Φ : γ → α} {Ψ : γ → β} {ε : ℝ} open sum (inl inr) local attribute [instance] premetric.dist_setoid def glue_premetric (hΦ : isometry Φ) (hΨ : isometry Ψ) : premetric_space (α ⊕ β) := { dist := glue_dist Φ Ψ 0, dist_self := glue_dist_self Φ Ψ 0, dist_comm := glue_dist_comm Φ Ψ 0, dist_triangle := glue_dist_triangle Φ Ψ 0 $ λp q, by rw [hΦ.dist_eq, hΨ.dist_eq]; simp } def glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) : Type* := @metric_quot _ (glue_premetric hΦ hΨ) instance metric_space_glue_space (hΦ : isometry Φ) (hΨ : isometry Ψ) : metric_space (glue_space hΦ hΨ) := @premetric.metric_space_quot _ (glue_premetric hΦ hΨ) def to_glue_l (hΦ : isometry Φ) (hΨ : isometry Ψ) (x : α) : glue_space hΦ hΨ := by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inl x⟧ def to_glue_r (hΦ : isometry Φ) (hΨ : isometry Ψ) (y : β) : glue_space hΦ hΨ := by letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ; exact ⟦inr y⟧ instance inhabited_left (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited α] : inhabited (glue_space hΦ hΨ) := ⟨to_glue_l _ _ (default _)⟩ instance inhabited_right (hΦ : isometry Φ) (hΨ : isometry Ψ) [inhabited β] : inhabited (glue_space hΦ hΨ) := ⟨to_glue_r _ _ (default _)⟩ lemma to_glue_commute (hΦ : isometry Φ) (hΨ : isometry Ψ) : (to_glue_l hΦ hΨ) ∘ Φ = (to_glue_r hΦ hΨ) ∘ Ψ := begin letI : premetric_space (α ⊕ β) := glue_premetric hΦ hΨ, funext, simp only [comp, to_glue_l, to_glue_r, quotient.eq], exact glue_dist_glued_points Φ Ψ 0 x end lemma to_glue_l_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_l hΦ hΨ) := isometry_emetric_iff_metric.2 $ λ_ _, rfl lemma to_glue_r_isometry (hΦ : isometry Φ) (hΨ : isometry Ψ) : isometry (to_glue_r hΦ hΨ) := isometry_emetric_iff_metric.2 $ λ_ _, rfl end gluing --section section inductive_limit /- In this section, we define the inductive limit of f 0 f 1 f 2 f 3 X 0 -----> X 1 -----> X 2 -----> X 3 -----> ... where the X n are metric spaces and f n isometric embeddings. We do it by defining a premetric space structure on Σn, X n, where the predistance dist x y is obtained by pushing x and y in a common X k using composition by the f n, and taking the distance there. This does not depend on the choice of k as the f n are isometries. The metric space associated to this premetric space is the desired inductive limit.-/ open nat variables {X : ℕ → Type u} [∀n, metric_space (X n)] {f : Πn, X n → X (n+1)} /-- Predistance on the disjoint union Σn, X n. -/ def inductive_limit_dist (f : Πn, X n → X (n+1)) (x y : Σn, X n) : ℝ := dist (le_rec_on (le_max_left x.1 y.1) f x.2 : X (max x.1 y.1)) (le_rec_on (le_max_right x.1 y.1) f y.2 : X (max x.1 y.1)) /-- The predistance on the disjoint union Σn, X n can be computed in any X k for large enough k.-/ lemma inductive_limit_dist_eq_dist (I : ∀n, isometry (f n)) (x y : Σn, X n) (m : ℕ) : ∀hx : x.1 ≤ m, ∀hy : y.1 ≤ m, inductive_limit_dist f x y = dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m) := begin induction m with m hm, { assume hx hy, have A : max x.1 y.1 = 0, { rw [nonpos_iff_eq_zero.1 hx, nonpos_iff_eq_zero.1 hy], simp }, unfold inductive_limit_dist, congr; simp only [A] }, { assume hx hy, by_cases h : max x.1 y.1 = m.succ, { unfold inductive_limit_dist, congr; simp only [h] }, { have : max x.1 y.1 ≤ succ m := by simp [hx, hy], have : max x.1 y.1 ≤ m := by simpa [h] using of_le_succ this, have xm : x.1 ≤ m := le_trans (le_max_left _ _) this, have ym : y.1 ≤ m := le_trans (le_max_right _ _) this, rw [le_rec_on_succ xm, le_rec_on_succ ym, (I m).dist_eq], exact hm xm ym }} end /-- Premetric space structure on Σn, X n.-/ def inductive_premetric (I : ∀n, isometry (f n)) : premetric_space (Σn, X n) := { dist := inductive_limit_dist f, dist_self := λx, by simp [dist, inductive_limit_dist], dist_comm := λx y, begin let m := max x.1 y.1, have hx : x.1 ≤ m := le_max_left _ _, have hy : y.1 ≤ m := le_max_right _ _, unfold dist, rw [inductive_limit_dist_eq_dist I x y m hx hy, inductive_limit_dist_eq_dist I y x m hy hx, dist_comm] end, dist_triangle := λx y z, begin let m := max (max x.1 y.1) z.1, have hx : x.1 ≤ m := le_trans (le_max_left _ _) (le_max_left _ _), have hy : y.1 ≤ m := le_trans (le_max_right _ _) (le_max_left _ _), have hz : z.1 ≤ m := le_max_right _ _, calc inductive_limit_dist f x z = dist (le_rec_on hx f x.2 : X m) (le_rec_on hz f z.2 : X m) : inductive_limit_dist_eq_dist I x z m hx hz ... ≤ dist (le_rec_on hx f x.2 : X m) (le_rec_on hy f y.2 : X m) + dist (le_rec_on hy f y.2 : X m) (le_rec_on hz f z.2 : X m) : dist_triangle _ _ _ ... = inductive_limit_dist f x y + inductive_limit_dist f y z : by rw [inductive_limit_dist_eq_dist I x y m hx hy, inductive_limit_dist_eq_dist I y z m hy hz] end } local attribute [instance] inductive_premetric premetric.dist_setoid /-- The type giving the inductive limit in a metric space context. -/ def inductive_limit (I : ∀n, isometry (f n)) : Type* := @metric_quot _ (inductive_premetric I) /-- Metric space structure on the inductive limit. -/ instance metric_space_inductive_limit (I : ∀n, isometry (f n)) : metric_space (inductive_limit I) := @premetric.metric_space_quot _ (inductive_premetric I) /-- Mapping each `X n` to the inductive limit. -/ def to_inductive_limit (I : ∀n, isometry (f n)) (n : ℕ) (x : X n) : metric.inductive_limit I := by letI : premetric_space (Σn, X n) := inductive_premetric I; exact ⟦sigma.mk n x⟧ instance (I : ∀ n, isometry (f n)) [inhabited (X 0)] : inhabited (inductive_limit I) := ⟨to_inductive_limit _ 0 (default _)⟩ /-- The map `to_inductive_limit n` mapping `X n` to the inductive limit is an isometry. -/ lemma to_inductive_limit_isometry (I : ∀n, isometry (f n)) (n : ℕ) : isometry (to_inductive_limit I n) := isometry_emetric_iff_metric.2 $ λx y, begin change inductive_limit_dist f ⟨n, x⟩ ⟨n, y⟩ = dist x y, rw [inductive_limit_dist_eq_dist I ⟨n, x⟩ ⟨n, y⟩ n (le_refl n) (le_refl n), le_rec_on_self, le_rec_on_self] end /-- The maps `to_inductive_limit n` are compatible with the maps `f n`. -/ lemma to_inductive_limit_commute (I : ∀n, isometry (f n)) (n : ℕ) : (to_inductive_limit I n.succ) ∘ (f n) = to_inductive_limit I n := begin funext, simp only [comp, to_inductive_limit, quotient.eq], show inductive_limit_dist f ⟨n.succ, f n x⟩ ⟨n, x⟩ = 0, { rw [inductive_limit_dist_eq_dist I ⟨n.succ, f n x⟩ ⟨n, x⟩ n.succ, le_rec_on_self, le_rec_on_succ, le_rec_on_self, dist_self], exact le_refl _, exact le_refl _, exact le_succ _ } end end inductive_limit --section end metric --namespace
5e37dcfd2b33887b06654bc65e1f8d965c403513
bb31430994044506fa42fd667e2d556327e18dfe
/src/logic/basic.lean
bd88f41f2a834902c59a5e6bca07ee52b224c8ee
[ "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
68,717
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import tactic.doc_commands import tactic.reserved_notation /-! # Basic logic properties > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". In the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ open function local attribute [instance, priority 10] classical.prop_decidable section miscellany /- We add the `inline` attribute to optimize VM computation using these declarations. For example, `if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/ attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable decidable.true implies.decidable not.decidable ne.decidable bool.decidable_eq decidable.to_bool attribute [simp] cast_eq cast_heq variables {α : Type*} {β : Type*} /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ @[reducible] def hidden {α : Sort*} {a : α} := a /-- Ex falso, the nondependent eliminator for the `empty` type. -/ def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) := ⟨by { intros a b, cases a, cases b, congr, }⟩ instance : decidable_eq empty := λa, a.elim instance sort.inhabited : inhabited Sort* := ⟨punit⟩ instance sort.inhabited' : inhabited default := ⟨punit.star⟩ instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl default⟩ instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr default⟩ @[priority 10] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) @[simp] lemma eq_iff_true_of_subsingleton {α : Sort*} [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- If all points are equal to a given point `x`, then `α` is a subsingleton. -/ lemma subsingleton_of_forall_eq {α : Sort*} (x : α) (h : ∀ y, y = x) : subsingleton α := ⟨λ a b, (h a).symm ▸ (h b).symm ▸ rfl⟩ lemma subsingleton_iff_forall_eq {α : Sort*} (x : α) : subsingleton α ↔ ∀ y, y = x := ⟨λ h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x⟩ instance subtype.subsingleton (α : Sort*) [subsingleton α] (p : α → Prop) : subsingleton (subtype p) := ⟨λ ⟨x,_⟩ ⟨y,_⟩, have x = y, from subsingleton.elim _ _, by { cases this, refl }⟩ /-- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl theorem coe_fn_coe_trans {α β γ δ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ δ] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl /-- Non-dependent version of `coe_fn_coe_trans`, helps `rw` figure out the argument. -/ theorem coe_fn_coe_trans' {α β γ} {δ : out_param $ _} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ (λ _, δ)] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl @[simp] theorem coe_fn_coe_base {α β γ} [has_coe α β] [has_coe_to_fun β γ] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl /-- Non-dependent version of `coe_fn_coe_base`, helps `rw` figure out the argument. -/ theorem coe_fn_coe_base' {α β} {γ : out_param $ _} [has_coe α β] [has_coe_to_fun β (λ _, γ)] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl -- This instance should have low priority, to ensure we follow the chain -- `set_like → has_coe_to_sort` attribute [instance, priority 10] coe_sort_trans theorem coe_sort_coe_trans {α β γ δ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ δ] (x : α) : @coe_sort α _ _ x = @coe_sort β _ _ x := rfl /-- Many structures such as bundled morphisms coerce to functions so that you can transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α` then you can write `e a` and this is elaborated as `⇑e a`. This type of coercion is implemented using the `has_coe_to_fun` type class. There is one important consideration: If a type coerces to another type which in turn coerces to a function, then it **must** implement `has_coe_to_fun` directly: ```lean structure sparkling_equiv (α β) extends α ≃ β -- if we add a `has_coe` instance, instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) := ⟨sparkling_equiv.to_equiv⟩ -- then a `has_coe_to_fun` instance **must** be added as well: instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ ``` (Rationale: if we do not declare the direct coercion, then `⇑e a` is not in simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This often causes loops in the simplifier.) -/ library_note "function coercion" @[simp] theorem coe_sort_coe_base {α β γ} [has_coe α β] [has_coe_to_sort β γ] (x : α) : @coe_sort α _ _ x = @coe_sort β _ _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u /-- Ex falso, the nondependent eliminator for the `pempty` type. -/ def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ @[simp] lemma not_nonempty_pempty : ¬ nonempty pempty := assume ⟨h⟩, h.elim lemma congr_heq {α β γ : Sort*} {f : α → γ} {g : β → γ} {x : α} {y : β} (h₁ : f == g) (h₂ : x == y) : f x = g y := by { cases h₂, cases h₁, refl } lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma ulift.down_injective {α : Sort*} : function.injective (@ulift.down α) | ⟨a⟩ ⟨b⟩ rfl := rfl @[simp] lemma ulift.down_inj {α : Sort*} {a b : ulift α} : a.down = b.down ↔ a = b := ⟨λ h, ulift.down_injective h, λ h, by rw h⟩ lemma plift.down_injective {α : Sort*} : function.injective (@plift.down α) | ⟨a⟩ ⟨b⟩ rfl := rfl @[simp] lemma plift.down_inj {α : Sort*} {a b : plift α} : a.down = b.down ↔ a = b := ⟨λ h, plift.down_injective h, λ h, by rw h⟩ -- missing [symm] attribute for ne in core. attribute [symm] ne.symm lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩ @[simp] lemma eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ (b = c) := ⟨λ h, by rw [← h], λ h a, by rw h⟩ @[simp] lemma eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ (a = b) := ⟨λ h, by rw h, λ h a, by rw h⟩ /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `zmod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `nat.prime` a class is desirable at all. The compromise is to add the assumption `[fact p.prime]` to `zmod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ class fact (p : Prop) : Prop := (out [] : p) /-- In most cases, we should not have global instances of `fact`; typeclass search only reads the head symbol and then tries any instances, which means that adding any such instance will cause slowdowns everywhere. We instead make them as lemmata and make them local instances as required. -/ library_note "fact non-instances" lemma fact.elim {p : Prop} (h : fact p) : p := h.1 lemma fact_iff {p : Prop} : fact p ↔ p := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- Swaps two pairs of arguments to a function. -/ @[reducible] def function.swap₂ {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} {φ : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → Sort*} (f : Π i₁ j₁ i₂ j₂, φ i₁ j₁ i₂ j₂) : Π i₂ j₂ i₁ j₁, φ i₁ j₁ i₂ j₂ := λ i₂ j₂ i₁ j₁, f i₁ j₁ i₂ j₂ /-- If `x : α . tac_name` then `x.out : α`. These are definitionally equal, but this can nevertheless be useful for various reasons, e.g. to apply further projection notation or in an argument to `simp`. -/ def auto_param.out {α : Sort*} {n : name} (x : auto_param α n) : α := x /-- If `x : α := d` then `x.out : α`. These are definitionally equal, but this can nevertheless be useful for various reasons, e.g. to apply further projection notation or in an argument to `simp`. -/ def opt_param.out {α : Sort*} {d : α} (x : α := d) : α := x end miscellany open function /-! ### Declarations about propositional connectives -/ theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial theorem eq_true_iff {a : Prop} : (a = true) = a := have (a ↔ true) = a, from propext (iff_true a), eq.subst (@iff_eq_eq a true) this section propositional variables {a b c d e f : Prop} /-! ### Declarations about `implies` -/ instance : is_refl Prop iff := ⟨iff.refl⟩ instance : is_trans Prop iff := ⟨λ _ _ _, iff.trans⟩ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id lemma iff.imp (h₁ : a ↔ b) (h₂ : c ↔ d) : (a → c) ↔ (b → d) := imp_congr h₁ h₂ @[simp] lemma eq_true_eq_id : eq true = id := by { funext, simp only [true_iff, id.def, iff_self, eq_iff_iff], } theorem imp_intro {α β : Prop} (h : α) : β → α := λ _, h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ lemma imp_iff_not (hb : ¬ b) : a → b ↔ ¬ a := imp_congr_right $ λ _, iff_false_intro hb theorem decidable.imp_iff_right_iff [decidable a] : ((a → b) ↔ b) ↔ (a ∨ b) := ⟨λ H, (decidable.em a).imp_right $ λ ha', H.1 $ λ ha, (ha' ha).elim, λ H, H.elim imp_iff_right $ λ hb, ⟨λ hab, hb, λ _ _, hb⟩⟩ @[simp] theorem imp_iff_right_iff : ((a → b) ↔ b) ↔ (a ∨ b) := decidable.imp_iff_right_iff lemma decidable.and_or_imp [decidable a] : (a ∧ b) ∨ (a → c) ↔ a → (b ∨ c) := if ha : a then by simp only [ha, true_and, true_implies_iff] else by simp only [ha, false_or, false_and, false_implies_iff] @[simp] theorem and_or_imp : (a ∧ b) ∨ (a → c) ↔ a → (b ∨ c) := decidable.and_or_imp /-- Provide modus tollens (`mt`) as dot notation for implications. -/ protected lemma function.mt : (a → b) → ¬ b → ¬ a := mt /-! ### Declarations about `not` -/ /-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/ def not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {a : Prop} : ¬(a → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem dec_em' (p : Prop) [decidable p] : ¬p ∨ p := (dec_em p).swap theorem em (p : Prop) : p ∨ ¬p := classical.em _ theorem em' (p : Prop) : ¬p ∨ p := (em p).swap theorem or_not {p : Prop} : p ∨ ¬p := em _ section eq_or_ne variables {α : Sort*} (x y : α) theorem decidable.eq_or_ne [decidable (x = y)] : x = y ∨ x ≠ y := dec_em $ x = y theorem decidable.ne_or_eq [decidable (x = y)] : x ≠ y ∨ x = y := dec_em' $ x = y theorem eq_or_ne : x = y ∨ x ≠ y := em $ x = y theorem ne_or_eq : x ≠ y ∨ x = y := em' $ x = y end eq_or_ne theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction -- alias by_contradiction ← by_contra theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction /-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `classical.choice` appears in the list. -/ library_note "decidable namespace" /-- As mathlib is primarily classical, if the type signature of a `def` or `lemma` does not require any `decidable` instances to state, it is preferable not to introduce any `decidable` instances that are needed in the proof as arguments, but rather to use the `classical` tactic as needed. In the other direction, when `decidable` instances do appear in the type signature, it is better to use explicitly introduced ones rather than allowing Lean to automatically infer classical ones, as these may cause instance mismatch errors later. -/ library_note "decidable arguments" -- See Note [decidable namespace] protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro /-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`. The left-to-right direction, double negation elimination (DNE), is classically true but not constructively. -/ @[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not theorem of_not_not : ¬¬a → a := by_contra lemma not_ne_iff {α : Sort*} {a b : α} : ¬ a ≠ b ↔ a = b := not_not -- See Note [decidable namespace] protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_imp h) theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp -- See Note [decidable namespace] protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := decidable.by_contradiction $ hb ∘ h theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm -- See Note [decidable namespace] protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.decidable_imp_symm, not.decidable_imp_symm⟩ theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm @[simp] theorem imp_not_self : (a → ¬a) ↔ ¬a := ⟨λ h ha, h ha ha, λ h _, h⟩ theorem decidable.not_imp_self [decidable a] : (¬a → a) ↔ a := by { have := @imp_not_self (¬a), rwa decidable.not_not at this } @[simp] theorem not_imp_self : (¬a → a) ↔ a := decidable.not_imp_self theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨swap, swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap lemma iff.not (h : a ↔ b) : ¬ a ↔ ¬ b := not_congr h lemma iff.not_left (h : a ↔ ¬ b) : ¬ a ↔ b := h.not.trans not_not lemma iff.not_right (h : ¬ a ↔ b) : a ↔ ¬ b := not_not.symm.trans h.not /-! ### Declarations about `xor` -/ @[simp] theorem xor_true : xor true = not := funext $ λ a, by simp [xor] @[simp] theorem xor_false : xor false = id := funext $ λ a, by simp [xor] theorem xor_comm (a b) : xor a b ↔ xor b a := or_comm _ _ instance : is_commutative Prop xor := ⟨λ a b, propext $ xor_comm a b⟩ @[simp] theorem xor_self (a : Prop) : xor a a = false := by simp [xor] @[simp] theorem xor_not_left : xor (¬a) b ↔ (a ↔ b) := by by_cases a; simp * @[simp] theorem xor_not_right : xor a (¬b) ↔ (a ↔ b) := by by_cases a; simp * theorem xor_not_not : xor (¬a) (¬b) ↔ xor a b := by simp [xor, or_comm, and_comm] protected theorem xor.or (h : xor a b) : a ∨ b := h.imp and.left and.left /-! ### Declarations about `and` -/ lemma iff.and (h₁ : a ↔ b) (h₂ : c ↔ d) : a ∧ c ↔ b ∧ d := and_congr h₁ h₂ theorem and_congr_left (h : c → (a ↔ b)) : a ∧ c ↔ b ∧ c := and.comm.trans $ (and_congr_right h).trans and.comm theorem and_congr_left' (h : a ↔ b) : a ∧ c ↔ b ∧ c := h.and iff.rfl theorem and_congr_right' (h : b ↔ c) : a ∧ b ↔ a ∧ c := iff.rfl.and h theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp only [and.left_comm, and.comm] lemma and_and_and_comm (a b c d : Prop) : (a ∧ b) ∧ c ∧ d ↔ (a ∧ c) ∧ b ∧ d := by rw [←and_assoc, @and.right_comm a, and_assoc] lemma and_and_distrib_left (a b c : Prop) : a ∧ (b ∧ c) ↔ (a ∧ b) ∧ (a ∧ c) := by rw [and_and_and_comm, and_self] lemma and_and_distrib_right (a b c : Prop) : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ (b ∧ c) := by rw [and_and_and_comm, and_self] lemma and_rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp only [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c → b ∧ c ∧ a := and_rotate.1 theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) lemma ne_and_eq_iff_right {α : Sort*} {a b c : α} (h : b ≠ c) : a ≠ b ∧ a = c ↔ a = c := and_iff_right_of_imp (λ h2, h2.symm ▸ h.symm) @[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) := ⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩ @[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) := ⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩ @[simp] lemma iff_self_and {p q : Prop} : (p ↔ p ∧ q) ↔ (p → q) := by rw [@iff.comm p, and_iff_left_iff_imp] @[simp] lemma iff_and_self {p q : Prop} : (p ↔ q ∧ p) ↔ (p → q) := by rw [and_comm, iff_self_and] @[simp] lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[simp] lemma and.congr_left_iff : (a ∧ c ↔ b ∧ c) ↔ c → (a ↔ b) := by simp only [and.comm, ← and.congr_right_iff] @[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩ @[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩ /-! ### Declarations about `or` -/ lemma iff.or (h₁ : a ↔ b) (h₂ : c ↔ d) : a ∨ c ↔ b ∨ d := or_congr h₁ h₂ lemma or_congr_left' (h : a ↔ b) : a ∨ c ↔ b ∨ c := h.or iff.rfl lemma or_congr_right' (h : b ↔ c) : a ∨ b ↔ a ∨ c := iff.rfl.or h theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b] lemma or_or_or_comm (a b c d : Prop) : (a ∨ b) ∨ c ∨ d ↔ (a ∨ c) ∨ b ∨ d := by rw [←or_assoc, @or.right_comm a, or_assoc] lemma or_or_distrib_left (a b c : Prop) : a ∨ (b ∨ c) ↔ (a ∨ b) ∨ (a ∨ c) := by rw [or_or_or_comm, or_self] lemma or_or_distrib_right (a b c : Prop) : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ (b ∨ c) := by rw [or_or_or_comm, or_self] lemma or_rotate : a ∨ b ∨ c ↔ b ∨ c ∨ a := by simp only [or.left_comm, or.comm] lemma or.rotate : a ∨ b ∨ c → b ∨ c ∨ a := or_rotate.1 theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) lemma or.imp3 (had : a → d) (hbe : b → e) (hcf : c → f) : a ∨ b ∨ c → d ∨ e ∨ f := or.imp had $ or.imp hbe hcf theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans decidable.or_iff_not_imp_left theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right -- See Note [decidable namespace] protected lemma decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := dite _ (or.inr ∘ h) or.inl lemma not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp -- See Note [decidable namespace] protected lemma decidable.or_not_of_imp [decidable a] (h : a → b) : b ∨ ¬ a := dite _ (or.inl ∘ h) or.inr lemma or_not_of_imp : (a → b) → b ∨ ¬ a := decidable.or_not_of_imp -- See Note [decidable namespace] protected lemma decidable.imp_iff_not_or [decidable a] : a → b ↔ ¬ a ∨ b := ⟨decidable.not_or_of_imp, or.neg_resolve_left⟩ lemma imp_iff_not_or : a → b ↔ ¬ a ∨ b := decidable.imp_iff_not_or -- See Note [decidable namespace] protected lemma decidable.imp_iff_or_not [decidable b] : b → a ↔ a ∨ ¬ b := decidable.imp_iff_not_or.trans or.comm lemma imp_iff_or_not : b → a ↔ a ∨ ¬ b := decidable.imp_iff_or_not -- See Note [decidable namespace] protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩ theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not /-- Provide the reverse of modus tollens (`mt`) as dot notation for implications. -/ protected theorem function.mtr : (¬ a → ¬ b) → (b → a) := not_imp_not.mp -- See Note [decidable namespace] protected lemma decidable.or_congr_left [decidable c] (h : ¬ c → (a ↔ b)) : a ∨ c ↔ b ∨ c := by { rw [decidable.or_iff_not_imp_right, decidable.or_iff_not_imp_right], exact imp_congr_right h } lemma or_congr_left (h : ¬ c → (a ↔ b)) : a ∨ c ↔ b ∨ c := decidable.or_congr_left h -- See Note [decidable namespace] protected lemma decidable.or_congr_right [decidable a] (h : ¬ a → (b ↔ c)) : a ∨ b ↔ a ∨ c := by { rw [decidable.or_iff_not_imp_left, decidable.or_iff_not_imp_left], exact imp_congr_right h } lemma or_congr_right (h : ¬ a → (b ↔ c)) : a ∨ b ↔ a ∨ c := decidable.or_congr_right h @[simp] theorem or_iff_left_iff_imp : (a ∨ b ↔ a) ↔ (b → a) := ⟨λ h hb, h.1 (or.inr hb), or_iff_left_of_imp⟩ @[simp] theorem or_iff_right_iff_imp : (a ∨ b ↔ b) ↔ (a → b) := by rw [or_comm, or_iff_left_iff_imp] lemma or_iff_left (hb : ¬ b) : a ∨ b ↔ a := ⟨λ h, h.resolve_right hb, or.inl⟩ lemma or_iff_right (ha : ¬ a) : a ∨ b ↔ b := ⟨λ h, h.resolve_left ha, or.inr⟩ /-! ### Declarations about distributivity -/ /-- `∧` distributes over `∨` (on the left). -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ /-- `∧` distributes over `∨` (on the right). -/ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (and.comm.or and.comm) /-- `∨` distributes over `∧` (on the left). -/ theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ /-- `∨` distributes over `∧` (on the right). -/ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (or.comm.and or.comm) @[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b := ⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩ @[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b := ⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩ /-! Declarations about `iff` -/ lemma iff.iff (h₁ : a ↔ b) (h₂ : c ↔ d) : (a ↔ c) ↔ (b ↔ d) := iff_congr h₁ h₂ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) @[simp] lemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [decidable.imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib' theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha -- See Note [decidable namespace] protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ -- See Note [decidable namespace] protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _ theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id -- See Note [decidable namespace] protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact decidable.not_imp_not.and decidable.not_imp_not theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not -- See Note [decidable namespace] protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact decidable.not_imp_comm.and imp_not_comm theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm -- See Note [decidable namespace] protected theorem decidable.not_iff : ∀ [decidable b], ¬ (a ↔ b) ↔ (¬ a ↔ b) := by intro h; cases h; simp only [h, iff_true, iff_false] theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff -- See Note [decidable namespace] protected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact imp_not_comm.and decidable.not_imp_comm theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm -- See Note [decidable namespace] protected theorem decidable.iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := decidable.iff_iff_and_or_not_and_not lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [decidable.imp_iff_not_or, or.comm] end lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := decidable.iff_iff_not_or_and_or_not -- See Note [decidable namespace] protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right /-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent. **Important**: this function should be used instead of `rw` on `decidable b`, because the kernel will get stuck reducing the usage of `propext` otherwise, and `dec_trivial` will not work. -/ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h /-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent. This is the same as `decidable_of_iff` but the iff is flipped. -/ @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`. (This is sometimes taken as an alternate definition of decidability.) -/ def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /-! ### De Morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) -- See Note [decidable namespace] protected theorem decidable.not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ -- See Note [decidable namespace] protected theorem decidable.not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ /-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the disjunction of the negations. -/ theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm /-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the conjunction of the negations. -/ theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := or_imp_distrib -- See Note [decidable namespace] protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, decidable.not_not] theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not -- See Note [decidable namespace] protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← decidable.not_and_distrib, decidable.not_not] theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not @[simp] theorem not_xor (P Q : Prop) : ¬ xor P Q ↔ (P ↔ Q) := by simp only [not_and, xor, not_or_distrib, not_not, ← iff_iff_implies_and_implies] theorem xor_iff_not_iff (P Q : Prop) : xor P Q ↔ ¬ (P ↔ Q) := (not_xor P Q).not_right theorem xor_iff_iff_not : xor a b ↔ (a ↔ ¬b) := by simp only [← @xor_not_right a, not_not] theorem xor_iff_not_iff' : xor a b ↔ (¬a ↔ b) := by simp only [← @xor_not_left _ b, not_not] end propositional /-! ### Declarations about equality -/ section mem variables {α β : Type*} [has_mem α β] {s t : β} {a b : α} lemma ne_of_mem_of_not_mem (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h lemma ne_of_mem_of_not_mem' (h : a ∈ s) : a ∉ t → s ≠ t := mt $ λ e, e ▸ h /-- **Alias** of `ne_of_mem_of_not_mem`. -/ lemma has_mem.mem.ne_of_not_mem : a ∈ s → b ∉ s → a ≠ b := ne_of_mem_of_not_mem /-- **Alias** of `ne_of_mem_of_not_mem'`. -/ lemma has_mem.mem.ne_of_not_mem' : a ∈ s → a ∉ t → s ≠ t := ne_of_mem_of_not_mem' end mem section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl -- todo: change name lemma ball_cond_comm {α} {s : α → Prop} {p : α → α → Prop} : (∀ a, s a → ∀ b, s b → p a b) ↔ (∀ a b, s a → s b → p a b) := ⟨λ h a b ha hb, h a ha b hb, λ h a ha b hb, h a b ha hb⟩ lemma ball_mem_comm {α β} [has_mem α β] {s : β} {p : α → α → Prop} : (∀ a b ∈ s, p a b) ↔ (∀ a b, a ∈ s → b ∈ s → p a b) := ball_cond_comm lemma ne_of_apply_ne {α β : Sort*} (f : α → β) {x y : α} (h : f x ≠ f y) : x ≠ y := λ (w : x = y), h (congr_arg f w) theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ /-- Transport through trivial families is the identity. -/ @[simp] lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') : (@eq.rec α a (λ a, β) y a' h) = y := by { cases h, refl, } @[simp] lemma eq_mp_eq_cast {α β : Sort*} (h : α = β) : eq.mp h = cast h := rfl @[simp] lemma eq_mpr_eq_cast {α β : Sort*} (h : α = β) : eq.mpr h = cast h.symm := rfl @[simp] lemma cast_cast : ∀ {α β γ : Sort*} (ha : α = β) (hb : β = γ) (a : α), cast hb (cast ha a) = cast (ha.trans hb) a | _ _ _ rfl rfl a := rfl @[simp] lemma congr_refl_left {α β : Sort*} (f : α → β) {a b : α} (h : a = b) : congr (eq.refl f) h = congr_arg f h := rfl @[simp] lemma congr_refl_right {α β : Sort*} {f g : α → β} (h : f = g) (a : α) : congr h (eq.refl a) = congr_fun h a := rfl @[simp] lemma congr_arg_refl {α β : Sort*} (f : α → β) (a : α) : congr_arg f (eq.refl a) = eq.refl (f a) := rfl @[simp] lemma congr_fun_rfl {α β : Sort*} (f : α → β) (a : α) : congr_fun (eq.refl f) a = eq.refl (f a) := rfl @[simp] lemma congr_fun_congr_arg {α β γ : Sort*} (f : α → β → γ) {a a' : α} (p : a = a') (b : β) : congr_fun (congr_arg f p) b = congr_arg (λ a, f a b) p := rfl lemma heq_of_cast_eq : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : cast e a = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma cast_eq_iff_heq {α β : Sort*} {a : α} {a' : β} {e : α = β} : cast e a = a' ↔ a == a' := ⟨heq_of_cast_eq _, λ h, by cases h; refl⟩ lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : (x₁ = x₂) ↔ (y₁ = y₂) := by { subst h₁, subst h₂ } lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h] lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h] lemma congr_arg2 {α β γ : Sort*} (f : α → β → γ) {x x' : α} {y y' : β} (hx : x = x') (hy : y = y') : f x y = f x' y' := by { subst hx, subst hy } variables {β : α → Sort*} {γ : Π a, β a → Sort*} {δ : Π a b, γ a b → Sort*} lemma congr_fun₂ {f g : Π a b, γ a b} (h : f = g) (a : α) (b : β a) : f a b = g a b := congr_fun (congr_fun h _) _ lemma congr_fun₃ {f g : Π a b c, δ a b c} (h : f = g) (a : α) (b : β a) (c : γ a b) : f a b c = g a b c := congr_fun₂ (congr_fun h _) _ _ lemma funext₂ {f g : Π a b, γ a b} (h : ∀ a b, f a b = g a b) : f = g := funext $ λ _, funext $ h _ lemma funext₃ {f g : Π a b c, δ a b c} (h : ∀ a b c, f a b c = g a b c) : f = g := funext $ λ _, funext₂ $ h _ end equality /-! ### Declarations about quantifiers -/ section quantifiers variables {α : Sort*} section dependent variables {β : α → Sort*} {γ : Π a, β a → Sort*} {δ : Π a b, γ a b → Sort*} {ε : Π a b c, δ a b c → Sort*} lemma pi_congr {β' : α → Sort*} (h : ∀ a, β a = β' a) : (Π a, β a) = Π a, β' a := (funext h : β = β') ▸ rfl lemma forall₂_congr {p q : Π a, β a → Prop} (h : ∀ a b, p a b ↔ q a b) : (∀ a b, p a b) ↔ ∀ a b, q a b := forall_congr $ λ a, forall_congr $ h a lemma forall₃_congr {p q : Π a b, γ a b → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∀ a b c, p a b c) ↔ ∀ a b c, q a b c := forall_congr $ λ a, forall₂_congr $ h a lemma forall₄_congr {p q : Π a b c, δ a b c → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∀ a b c d, p a b c d) ↔ ∀ a b c d, q a b c d := forall_congr $ λ a, forall₃_congr $ h a lemma forall₅_congr {p q : Π a b c d, ε a b c d → Prop} (h : ∀ a b c d e, p a b c d e ↔ q a b c d e) : (∀ a b c d e, p a b c d e) ↔ ∀ a b c d e, q a b c d e := forall_congr $ λ a, forall₄_congr $ h a lemma exists₂_congr {p q : Π a, β a → Prop} (h : ∀ a b, p a b ↔ q a b) : (∃ a b, p a b) ↔ ∃ a b, q a b := exists_congr $ λ a, exists_congr $ h a lemma exists₃_congr {p q : Π a b, γ a b → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∃ a b c, p a b c) ↔ ∃ a b c, q a b c := exists_congr $ λ a, exists₂_congr $ h a lemma exists₄_congr {p q : Π a b c, δ a b c → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∃ a b c d, p a b c d) ↔ ∃ a b c d, q a b c d := exists_congr $ λ a, exists₃_congr $ h a lemma exists₅_congr {p q : Π a b c d, ε a b c d → Prop} (h : ∀ a b c d e, p a b c d e ↔ q a b c d e) : (∃ a b c d e, p a b c d e) ↔ ∃ a b c d e, q a b c d e := exists_congr $ λ a, exists₄_congr $ h a lemma forall_imp {p q : α → Prop} (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a := λ h' a, h a (h' a) lemma forall₂_imp {p q : Π a, β a → Prop} (h : ∀ a b, p a b → q a b) : (∀ a b, p a b) → ∀ a b, q a b := forall_imp $ λ i, forall_imp $ h i lemma forall₃_imp {p q : Π a b, γ a b → Prop} (h : ∀ a b c, p a b c → q a b c) : (∀ a b c, p a b c) → ∀ a b c, q a b c := forall_imp $ λ a, forall₂_imp $ h a lemma Exists.imp {p q : α → Prop} (h : ∀ a, (p a → q a)) : (∃ a, p a) → ∃ a, q a := exists_imp_exists h lemma Exists₂.imp {p q : Π a, β a → Prop} (h : ∀ a b, p a b → q a b) : (∃ a b, p a b) → ∃ a b, q a b := Exists.imp $ λ a, Exists.imp $ h a lemma Exists₃.imp {p q : Π a b, γ a b → Prop} (h : ∀ a b c, p a b c → q a b c) : (∃ a b c, p a b c) → ∃ a b c, q a b c := Exists.imp $ λ a, Exists₂.imp $ h a end dependent variables {ι β : Sort*} {κ : ι → Sort*} {p q : α → Prop} {b : Prop} lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a)) (hp : ∃ a, p a) : ∃ b, q b := exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩) theorem forall_swap {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨swap, swap⟩ lemma forall₂_swap {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} {p : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → Prop} : (∀ i₁ j₁ i₂ j₂, p i₁ j₁ i₂ j₂) ↔ ∀ i₂ j₂ i₁ j₁, p i₁ j₁ i₂ j₂ := ⟨swap₂, swap₂⟩ /-- We intentionally restrict the type of `α` in this lemma so that this is a safer to use in simp than `forall_swap`. -/ lemma imp_forall_iff {α : Type*} {p : Prop} {q : α → Prop} : (p → ∀ x, q x) ↔ (∀ x, p → q x) := forall_swap theorem exists_swap {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem forall_exists_index {q : (∃ x, p x) → Prop} : (∀ h, q h) ↔ ∀ x (h : p x), q ⟨x, h⟩ := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := forall_exists_index /-- Extract an element from a existential statement, using `classical.some`. -/ -- This enables projection notation. @[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P /-- Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`. -/ lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) -- See Note [decidable namespace] protected theorem decidable.not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall -- See Note [decidable namespace] protected theorem decidable.not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not -- See Note [decidable namespace] protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [decidable.not_not] @[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not theorem forall_imp_iff_exists_imp [ha : nonempty α] : ((∀ x, p x) → b) ↔ ∃ x, p x → b := let ⟨a⟩ := ha in ⟨λ h, not_forall_not.1 $ λ h', classical.by_cases (λ hb : b, h' a $ λ _, hb) (λ hb, hb $ h $ λ x, (not_imp.1 (h' x)).1), λ ⟨x, hx⟩ h, hx (h x)⟩ -- TODO: duplicate of a lemma in core theorem forall_true_iff : (α → true) ↔ true := implies_true_iff α -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) @[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} : (∃! x, p x) ↔ ∃ x, p x := ⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩ @[simp] theorem forall_const (α : Sort*) [i : nonempty α] : (α → b) ↔ b := ⟨i.elim, λ hb x, hb⟩ /-- For some reason simp doesn't use `forall_const` to simplify in this case. -/ @[simp] lemma forall_forall_const {α β : Type*} (p : β → Prop) [nonempty α] : (∀ x, α → p x) ↔ ∀ x, p x := forall_congr $ λ x, forall_const α @[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩ theorem exists_unique_const (α : Sort*) [i : nonempty α] [subsingleton α] : (∃! x : α, b) ↔ b := by simp theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] theorem and_forall_ne (a : α) : (p a ∧ ∀ b ≠ a, p b) ↔ ∀ b, p b := by simp only [← @forall_eq _ p a, ← forall_and_distrib, ← or_imp_distrib, classical.em, forall_const] -- this lemma is needed to simplify the output of `list.mem_cons_iff` @[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] lemma ne.ne_or_ne {x y : α} (z : α) (h : x ≠ y) : x ≠ z ∨ y ≠ z := not_and_distrib.1 $ mt (and_imp.2 eq.substr) h.symm theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq' {a' : α} : ∃ a, a' = a := ⟨_, rfl⟩ @[simp] theorem exists_unique_eq {a' : α} : ∃! a, a = a' := by simp only [eq_comm, exists_unique, and_self, forall_eq', exists_eq'] @[simp] theorem exists_unique_eq' {a' : α} : ∃! a, a' = a := by simp only [exists_unique, and_self, forall_eq', exists_eq'] @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem exists_eq_right_right {a' : α} : (∃ (a : α), p a ∧ q a ∧ a = a') ↔ p a' ∧ q a' := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_eq_right_right' {a' : α} : (∃ (a : α), p a ∧ q a ∧ a' = a) ↔ p a' ∧ q a' := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_apply_eq_apply (f : α → β) (a' : α) : ∃ a, f a = f a' := ⟨a', rfl⟩ @[simp] theorem exists_apply_eq_apply' (f : α → β) (a' : α) : ∃ a, f a' = f a := ⟨a', rfl⟩ @[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} : (∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) := ⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩ @[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} : (∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) := ⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩ @[simp] lemma exists_or_eq_left (y : α) (p : α → Prop) : ∃ (x : α), x = y ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ x = y := ⟨y, or.inr rfl⟩ @[simp] lemma exists_or_eq_left' (y : α) (p : α → Prop) : ∃ (x : α), y = x ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right' (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ y = x := ⟨y, or.inr rfl⟩ @[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) := ⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩ @[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) := by simp [@eq_comm _ _ (f _)] @[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_apply_eq_imp_iff₂ {f : α → β} {p : α → Prop} {q : β → Prop} : (∀ b, ∀ a, p a → f a = b → q b) ↔ ∀ a, p a → q (f a) := ⟨λ h a ha, h (f a) a ha rfl, λ h b a ha hb, hb ▸ h a ha⟩ @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b := ⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩ lemma exists₂_comm {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} {p : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → Prop} : (∃ i₁ j₁ i₂ j₂, p i₁ j₁ i₂ j₂) ↔ ∃ i₂ j₂ i₁ j₁, p i₁ j₁ i₂ j₂ := by simp only [@exists_comm (κ₁ _), @exists_comm ι₁] theorem and.exists {p q : Prop} {f : p ∧ q → Prop} : (∃ h, f h) ↔ ∃ hp hq, f ⟨hp, hq⟩ := ⟨λ ⟨h, H⟩, ⟨h.1, h.2, H⟩, λ ⟨hp, hq, H⟩, ⟨⟨hp, hq⟩, H⟩⟩ theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, decidable.forall_or_distrib_left] theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ theorem exists_unique_prop {p q : Prop} : (∃! h : p, q) ↔ p ∧ q := by simp @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h @[simp] lemma exists_unique_false : ¬ (∃! (a : α), false) := assume ⟨a, h, h'⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ lemma exists_iff_of_forall {p : Prop} {q : p → Prop} (h : ∀ h, q h) : (∃ h, q h) ↔ p := ⟨Exists.fst, λ H, ⟨H, h H⟩⟩ theorem exists_unique_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃! h' : p, q h') ↔ q h := @exists_unique_const (q h) p ⟨h⟩ _ theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst @[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q ↔ ∃ h : p', q' (hp.2 h) := ⟨λ ⟨_, _⟩, ⟨hp.1 ‹_›, (hq _).1 ‹_›⟩, λ ⟨_, _⟩, ⟨_, (hq _).2 ‹_›⟩⟩ @[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q = ∃ h : p', q' (hp.2 h) := propext (exists_prop_congr hq _) /-- See `is_empty.exists_iff` for the `false` version. -/ @[simp] lemma exists_true_left (p : true → Prop) : (∃ x, p x) ↔ p true.intro := exists_prop_of_true _ lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := unique_of_exists_unique h py₁ py₂ @[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) ↔ ∀ h : p', q' (hp.2 h) := ⟨λ h1 h2, (hq _).1 (h1 (hp.2 _)), λ h1 h2, (hq _).2 (h1 (hp.1 h2))⟩ @[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) = ∀ h : p', q' (hp.2 h) := propext (forall_prop_congr hq _) /-- See `is_empty.forall_iff` for the `false` version. -/ @[simp] lemma forall_true_left (p : true → Prop) : (∀ x, p x) ↔ p true.intro := forall_prop_of_true _ lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h) (h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b := begin simp only [exists_unique_iff_exists] at h₂, apply h₂.elim, exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩) end lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp) (H : ∀ y (hy : p y), q y hy → y = w) : ∃! x (hx : p x), q x hx := begin simp only [exists_unique_iff_exists], exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq) end lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop} (h : ∃! x (hx : p x), q x hx) : ∃ x (hx : p x), q x hx := h.exists.imp (λ x hx, hx.exists) lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx) {y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁) (hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ := begin simp only [exists_unique_iff_exists] at h, exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩ end end quantifiers /-! ### Classical lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 /- use shortened names to avoid conflict when classical namespace is open. -/ /-- Any prop `p` is decidable classically. A shorthand for `classical.prop_decidable`. -/ noncomputable def dec (p : Prop) : decidable p := by apply_instance /-- Any predicate `p` is decidable classically. -/ noncomputable def dec_pred (p : α → Prop) : decidable_pred p := by apply_instance /-- Any relation `p` is decidable classically. -/ noncomputable def dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance /-- Any type `α` has decidable equality classically. -/ noncomputable def dec_eq (α : Sort*) : decidable_eq α := by apply_instance /-- Construct a function from a default value `H0`, and a function to use if there exists a value satisfying the predicate. -/ @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Sort*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ /-- A version of classical.indefinite_description which is definitionally equal to a pair -/ noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} := ⟨classical.some h, classical.some_spec h⟩ /-- A version of `by_contradiction` that uses types instead of propositions. -/ protected noncomputable def by_contradiction' {α : Sort*} (H : ¬ (α → false)) : α := classical.choice $ peirce _ false $ λ h, (H $ λ a, h ⟨a⟩).elim /-- `classical.by_contradiction'` is equivalent to lean's axiom `classical.choice`. -/ def choice_of_by_contradiction' {α : Sort*} (contra : ¬ (α → false) → α) : nonempty α → α := λ H, contra H.elim end classical /-- This function has the same type as `exists.rec_on`, and can be used to case on an equality, but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe using the axiom of choice. -/ @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /-! ### Declarations about bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem bex_eq_left {a : α} : (∃ x (_ : x = a), p x) ↔ p a := by simp only [exists_prop, exists_eq_left] theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h -- See Note [decidable namespace] protected theorem decidable.not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib theorem ball_or_left_distrib : (∀ x, p x ∨ q x → r x) ↔ (∀ x, p x → r x) ∧ (∀ x, q x → r x) := iff.trans (forall_congr $ λ x, or_imp_distrib) forall_and_distrib theorem bex_or_left_distrib : (∃ x (_ : p x ∨ q x), r x) ↔ (∃ x (_ : p x), r x) ∨ (∃ x (_ : q x), r x) := by simp only [exists_prop]; exact iff.trans (exists_congr $ λ x, or_and_distrib_right) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical section ite variables {α β γ : Sort*} {σ : α → Sort*} (f : α → β) {P Q : Prop} [decidable P] [decidable Q] {a b c : α} {A : P → α} {B : ¬ P → α} lemma dite_eq_iff : dite P A B = c ↔ (∃ h, A h = c) ∨ ∃ h, B h = c := by by_cases P; simp [*, exists_prop_of_false not_false] lemma ite_eq_iff : ite P a b = c ↔ P ∧ a = c ∨ ¬ P ∧ b = c := dite_eq_iff.trans $ by rw [exists_prop, exists_prop] lemma dite_eq_iff' : dite P A B = c ↔ (∀ h, A h = c) ∧ (∀ h, B h = c) := ⟨λ he, ⟨λ h, (dif_pos h).symm.trans he, λ h, (dif_neg h).symm.trans he⟩, λ he, (em P).elim (λ h, (dif_pos h).trans $ he.1 h) (λ h, (dif_neg h).trans $ he.2 h)⟩ lemma ite_eq_iff' : ite P a b = c ↔ (P → a = c) ∧ (¬ P → b = c) := dite_eq_iff' @[simp] lemma dite_eq_left_iff : dite P (λ _, a) B = a ↔ ∀ h, B h = a := by by_cases P; simp [*, forall_prop_of_false not_false] @[simp] lemma dite_eq_right_iff : dite P A (λ _, b) = b ↔ ∀ h, A h = b := by by_cases P; simp [*, forall_prop_of_false not_false] @[simp] lemma ite_eq_left_iff : ite P a b = a ↔ (¬ P → b = a) := dite_eq_left_iff @[simp] lemma ite_eq_right_iff : ite P a b = b ↔ (P → a = b) := dite_eq_right_iff lemma dite_ne_left_iff : dite P (λ _, a) B ≠ a ↔ ∃ h, a ≠ B h := by { rw [ne.def, dite_eq_left_iff, not_forall], exact exists_congr (λ h, by rw ne_comm) } lemma dite_ne_right_iff : dite P A (λ _, b) ≠ b ↔ ∃ h, A h ≠ b := by simp only [ne.def, dite_eq_right_iff, not_forall] lemma ite_ne_left_iff : ite P a b ≠ a ↔ ¬ P ∧ a ≠ b := dite_ne_left_iff.trans $ by rw exists_prop lemma ite_ne_right_iff : ite P a b ≠ b ↔ P ∧ a ≠ b := dite_ne_right_iff.trans $ by rw exists_prop protected lemma ne.dite_eq_left_iff (h : ∀ h, a ≠ B h) : dite P (λ _, a) B = a ↔ P := dite_eq_left_iff.trans $ ⟨λ H, of_not_not $ λ h', h h' (H h').symm, λ h H, (H h).elim⟩ protected lemma ne.dite_eq_right_iff (h : ∀ h, A h ≠ b) : dite P A (λ _, b) = b ↔ ¬ P := dite_eq_right_iff.trans $ ⟨λ H h', h h' (H h'), λ h' H, (h' H).elim⟩ protected lemma ne.ite_eq_left_iff (h : a ≠ b) : ite P a b = a ↔ P := ne.dite_eq_left_iff $ λ _, h protected lemma ne.ite_eq_right_iff (h : a ≠ b) : ite P a b = b ↔ ¬ P := ne.dite_eq_right_iff $ λ _, h protected lemma ne.dite_ne_left_iff (h : ∀ h, a ≠ B h) : dite P (λ _, a) B ≠ a ↔ ¬ P := dite_ne_left_iff.trans $ exists_iff_of_forall h protected lemma ne.dite_ne_right_iff (h : ∀ h, A h ≠ b) : dite P A (λ _, b) ≠ b ↔ P := dite_ne_right_iff.trans $ exists_iff_of_forall h protected lemma ne.ite_ne_left_iff (h : a ≠ b) : ite P a b ≠ a ↔ ¬ P := ne.dite_ne_left_iff $ λ _, h protected lemma ne.ite_ne_right_iff (h : a ≠ b) : ite P a b ≠ b ↔ P := ne.dite_ne_right_iff $ λ _, h variables (P Q) (a b) /-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/ @[simp] lemma dite_eq_ite : dite P (λ h, a) (λ h, b) = ite P a b := rfl lemma dite_eq_or_eq : (∃ h, dite P A B = A h) ∨ ∃ h, dite P A B = B h := decidable.by_cases (λ h, or.inl ⟨h, dif_pos h⟩) (λ h, or.inr ⟨h, dif_neg h⟩) lemma ite_eq_or_eq : ite P a b = a ∨ ite P a b = b := decidable.by_cases (λ h, or.inl (if_pos h)) (λ h, or.inr (if_neg h)) /-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/ lemma apply_dite (x : P → α) (y : ¬P → α) : f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) := by by_cases h : P; simp [h] /-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/ lemma apply_ite : f (ite P a b) = ite P (f a) (f b) := apply_dite f P (λ _, a) (λ _, b) /-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function applied to each of the branches. -/ lemma apply_dite2 (f : α → β → γ) (P : Prop) [decidable P] (a : P → α) (b : ¬P → α) (c : P → β) (d : ¬P → β) : f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) := by by_cases h : P; simp [h] /-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function applied to each of the branches. -/ lemma apply_ite2 (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) : f (ite P a b) (ite P c d) = ite P (f a c) (f b d) := apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d) /-- A 'dite' producing a `Pi` type `Π a, σ a`, applied to a value `a : α` is a `dite` that applies either branch to `a`. -/ lemma dite_apply (f : P → Π a, σ a) (g : ¬ P → Π a, σ a) (a : α) : (dite P f g) a = dite P (λ h, f h a) (λ h, g h a) := by by_cases h : P; simp [h] /-- A 'ite' producing a `Pi` type `Π a, σ a`, applied to a value `a : α` is a `ite` that applies either branch to `a`. -/ lemma ite_apply (f g : Π a, σ a) (a : α) : (ite P f g) a = ite P (f a) (g a) := dite_apply P (λ _, f) (λ _, g) a /-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/ @[simp] lemma dite_not (x : ¬ P → α) (y : ¬¬ P → α) : dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x := by by_cases h : P; simp [h] /-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/ @[simp] lemma ite_not : ite (¬ P) a b = ite P b a := dite_not P (λ _, a) (λ _, b) lemma ite_and : ite (P ∧ Q) a b = ite P (ite Q a b) b := by by_cases hp : P; by_cases hq : Q; simp [hp, hq] lemma dite_dite_comm {B : Q → α} {C : ¬P → ¬Q → α} (h : P → ¬Q) : (if p : P then A p else if q : Q then B q else C p q) = (if q : Q then B q else if p : P then A p else C p q) := dite_eq_iff'.2 ⟨λ p, by rw [dif_neg (h p), dif_pos p], λ np, by { congr, funext, rw dif_neg np }⟩ lemma ite_ite_comm (h : P → ¬Q) : (if P then a else if Q then b else c) = (if Q then b else if P then a else c) := dite_dite_comm P Q h end ite
55cb1f574d320b2dace4163eabd0c18e6e74f263
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/analysis/normed_space/operator_norm.lean
51a24161f12013d6c3bd57cf2bb76ec4f0f42ae6
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
10,671
lean
/- Copyright (c) 2019 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo Operator norm on the space of continuous linear maps Define the operator norm on the space of continuous linear maps between normed spaces, and prove its basic properties. In particular, show that this space is itself a normed space. -/ import topology.metric_space.lipschitz import analysis.asymptotics noncomputable theory local attribute [instance] classical.prop_decidable set_option class.instance_max_depth 70 variables {k : Type*} {E : Type*} {F : Type*} {G : Type*} [nondiscrete_normed_field k] [normed_group E] [normed_space k E] [normed_group F] [normed_space k F] [normed_group G] [normed_space k G] (c : k) (f g : E →L[k] F) (h : F →L[k] G) (x y z : E) include k open metric continuous_linear_map lemma exists_pos_bound_of_bound {f : E → F} (M : ℝ) (h : ∀x, ∥f x∥ ≤ M * ∥x∥) : ∃ N > 0, ∀x, ∥f x∥ ≤ N * ∥x∥ := ⟨max M 1, lt_of_lt_of_le zero_lt_one (le_max_right _ _), λx, calc ∥f x∥ ≤ M * ∥x∥ : h x ... ≤ max M 1 * ∥x∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) ⟩ lemma linear_map.continuous_of_bound (f : E →ₗ[k] F) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : continuous f := begin have : ∀ (x y : E), dist (f x) (f y) ≤ C * dist x y := λx y, calc dist (f x) (f y) = ∥f x - f y∥ : by rw dist_eq_norm ... = ∥f (x - y)∥ : by simp ... ≤ C * ∥x - y∥ : h _ ... = C * dist x y : by rw dist_eq_norm, exact continuous_of_lipschitz this end def linear_map.with_bound (f : E →ₗ[k] F) (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) : E →L[k] F := ⟨f, let ⟨C, hC⟩ := h in linear_map.continuous_of_bound f C hC⟩ @[simp, elim_cast] lemma linear_map_with_bound_coe (f : E →ₗ[k] F) (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) : ((f.with_bound h) : E →ₗ[k] F) = f := rfl @[simp] lemma linear_map_with_bound_apply (f : E →ₗ[k] F) (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) (x : E) : f.with_bound h x = f x := rfl namespace continuous_linear_map /-- A continuous linear map between normed spaces is bounded when the field is nondiscrete. The continuity ensures boundedness on a ball of some radius δ. The nondiscreteness is then used to rescale any element into an element of norm in [δ/C, δ], whose image has a controlled norm. The norm control for the original element follows by rescaling. -/ theorem bound : ∃ C > 0, ∀ x : E, ∥f x∥ ≤ C * ∥x∥ := begin have : continuous_at f 0 := continuous_iff_continuous_at.1 f.2 _, rcases metric.tendsto_nhds_nhds.1 this 1 zero_lt_one with ⟨ε, ε_pos, hε⟩, let δ := ε/2, have δ_pos : δ > 0 := half_pos ε_pos, have H : ∀{a}, ∥a∥ ≤ δ → ∥f a∥ ≤ 1, { assume a ha, have : dist (f a) (f 0) ≤ 1, { apply le_of_lt (hε _), rw [dist_eq_norm, sub_zero], exact lt_of_le_of_lt ha (half_lt_self ε_pos) }, simpa using this }, rcases exists_one_lt_norm k with ⟨c, hc⟩, refine ⟨δ⁻¹ * ∥c∥, mul_pos (inv_pos δ_pos) (lt_trans zero_lt_one hc), (λx, _)⟩, by_cases h : x = 0, { simp only [h, norm_zero, mul_zero, continuous_linear_map.map_zero], }, { rcases rescale_to_shell hc δ_pos h with ⟨d, hd, dxle, ledx, dinv⟩, calc ∥f x∥ = ∥f ((d⁻¹ * d) • x)∥ : by rwa [inv_mul_cancel, one_smul] ... = ∥d∥⁻¹ * ∥f (d • x)∥ : by rw [mul_smul, map_smul, norm_smul, norm_inv] ... ≤ ∥d∥⁻¹ * 1 : mul_le_mul_of_nonneg_left (H dxle) (by { rw ← norm_inv, exact norm_nonneg _ }) ... ≤ δ⁻¹ * ∥c∥ * ∥x∥ : by { rw mul_one, exact dinv } } end section open asymptotics filter theorem is_O_id (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := f.bound in ⟨M, hMp, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp (g : F →L[k] G) (f : E → F) (l : filter E) : is_O (λ x', g (f x')) f l := ((g.is_O_id ⊤).comp _).mono (map_le_iff_le_comap.mp lattice.le_top) theorem is_O_sub (f : E →L[k] F) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp f _ l end section op_norm open set real set_option class.instance_max_depth 100 /-- The operator norm of a continuous linear map is the inf of all its bounds. -/ def op_norm := Inf { c | c ≥ 0 ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } instance has_op_norm : has_norm (E →L[k] F) := ⟨op_norm⟩ -- So that invocations of real.Inf_le make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : E →L[k] F} : ∃ c, c ∈ { c | c ≥ 0 ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : E →L[k] F} : bdd_below { c | c ≥ 0 ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := lb_le_Inf _ bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm: ∥f x∥ ≤ ∥f∥ * ∥x∥. -/ theorem le_op_norm : ∥f x∥ ≤ ∥f∥ * ∥x∥ := classical.by_cases (λ heq : x = 0, by { rw heq, simp }) (λ hne, have hlt : 0 < ∥x∥, from (norm_pos_iff _).2 hne, le_mul_of_div_le hlt ((le_Inf _ bounds_nonempty bounds_bdd_below).2 (λ c ⟨_, hc⟩, div_le_of_le_mul hlt (by { rw mul_comm, apply hc })))) lemma ratio_le_op_norm : ∥f x∥ / ∥x∥ ≤ ∥f∥ := (or.elim (lt_or_eq_of_le (norm_nonneg _)) (λ hlt, div_le_of_le_mul hlt (by { rw mul_comm, apply le_op_norm })) (λ heq, by { rw [←heq, div_zero], apply op_norm_nonneg })) /-- The image of the unit ball under a continuous linear map is bounded. -/ lemma unit_le_op_norm : ∥x∥ ≤ 1 → ∥f x∥ ≤ ∥f∥ := λ hx, begin rw [←(mul_one ∥f∥)], calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... ≤ _ : mul_le_mul_of_nonneg_left hx (op_norm_nonneg _) end /-- If one controls the norm of every A x, then one controls the norm of A. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ x, ∥f x∥ ≤ M * ∥x∥) : ∥f∥ ≤ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_triangle : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := Inf_le _ bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul, calc _ ≤ ∥f x∥ + ∥g x∥ : norm_triangle _ _ ... ≤ _ : add_le_add (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- An operator is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 := iff.intro (λ hn, continuous_linear_map.ext (λ x, (norm_le_zero_iff _).1 (calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... = _ : by rw [hn, zero_mul]))) (λ hf, le_antisymm (Inf_le _ bounds_bdd_below ⟨ge_of_eq rfl, λ _, le_of_eq (by { rw [zero_mul, hf], exact norm_zero })⟩) (op_norm_nonneg _)) @[simp] lemma norm_zero : ∥(0 : E →L[k] F)∥ = 0 := by rw op_norm_zero_iff /-- The norm of the identity is at most 1. It is in fact 1, except when the space is trivial where it is 0. It means that one can not do better than an inequality in general. -/ lemma norm_id : ∥(id : E →L[k] E)∥ ≤ 1 := op_norm_le_bound _ zero_le_one (λx, by simp) /-- The operator norm is homogeneous. -/ lemma op_norm_smul : ∥c • f∥ = ∥c∥ * ∥f∥ := le_antisymm (Inf_le _ bounds_bdd_below ⟨mul_nonneg (norm_nonneg _) (op_norm_nonneg _), λ _, begin erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end⟩) (lb_le_Inf _ bounds_nonempty (λ _ ⟨hn, hc⟩, (or.elim (lt_or_eq_of_le (norm_nonneg c)) (λ hlt, begin rw mul_comm, exact mul_le_of_le_div hlt (Inf_le _ bounds_bdd_below ⟨div_nonneg hn hlt, λ _, (by { rw div_mul_eq_mul_div, exact le_div_of_mul_le hlt (by { rw [ mul_comm, ←norm_smul ], exact hc _ }) })⟩) end) (λ heq, by { rw [←heq, zero_mul], exact hn })))) lemma op_norm_neg : ∥-f∥ = ∥f∥ := calc ∥-f∥ = ∥(-1:k) • f∥ : by rw neg_one_smul ... = ∥(-1:k)∥ * ∥f∥ : by rw op_norm_smul ... = ∥f∥ : by simp /-- Continuous linear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (E →L[k] F) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_triangle, op_norm_neg⟩ /- The next instance should be found automatically, but it is not. TODO: fix me -/ instance to_normed_group_prod : normed_group (E →L[k] (F × G)) := continuous_linear_map.to_normed_group instance to_normed_space : normed_space k (E →L[k] F) := ⟨op_norm_smul⟩ /-- The operator norm is submultiplicative. -/ lemma op_norm_comp_le : ∥comp h f∥ ≤ ∥h∥ * ∥f∥ := (Inf_le _ bounds_bdd_below ⟨mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, begin rw mul_assoc, calc _ ≤ ∥h∥ * ∥f x∥: le_op_norm _ _ ... ≤ _ : mul_le_mul_of_nonneg_left (le_op_norm _ _) (op_norm_nonneg _) end⟩) /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : lipschitz_with ∥f∥ f := ⟨op_norm_nonneg _, λ x y, by { rw [dist_eq_norm, dist_eq_norm, ←map_sub], apply le_op_norm }⟩ end op_norm /-- The norm of the tensor product of a scalar linear map and of an element of a normed space is the product of the norms. -/ @[simp] lemma scalar_prod_space_iso_norm {c : E →L[k] k} {f : F} : ∥scalar_prod_space_iso c f∥ = ∥c∥ * ∥f∥ := begin refine le_antisymm _ _, { apply op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) (λx, _), calc ∥(c x) • f∥ = ∥c x∥ * ∥f∥ : norm_smul _ _ ... ≤ (∥c∥ * ∥x∥) * ∥f∥ : mul_le_mul_of_nonneg_right (le_op_norm _ _) (norm_nonneg _) ... = ∥c∥ * ∥f∥ * ∥x∥ : by ring }, { by_cases h : ∥f∥ = 0, { rw h, simp [norm_nonneg] }, { have : 0 < ∥f∥ := lt_of_le_of_ne (norm_nonneg _) (ne.symm h), rw ← le_div_iff this, apply op_norm_le_bound _ (div_nonneg (norm_nonneg _) this) (λx, _), rw [div_mul_eq_mul_div, le_div_iff this], calc ∥c x∥ * ∥f∥ = ∥c x • f∥ : (norm_smul _ _).symm ... = ∥((scalar_prod_space_iso c f) : E → F) x∥ : rfl ... ≤ ∥scalar_prod_space_iso c f∥ * ∥x∥ : le_op_norm _ _ } }, end end continuous_linear_map
13769cb5aa490f7c9fb0bc3f6dfde760f36ae27c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Init/Coe.lean
3689d5a24850cebc70ff00170083a0754c6b014d
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
11,932
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, Mario Carneiro -/ prelude import Init.Prelude set_option linter.missingDocs true -- keep it documented /-! # Coercion Lean uses a somewhat elaborate system of typeclasses to drive the coercion system. Here a *coercion* means an invisible function that is automatically inserted to fix what would otherwise be a type error. For example, if we have: ``` def f (x : Nat) : Int := x ``` then this is clearly not type correct as is, because `x` has type `Nat` but type `Int` is expected, and normally you will get an error message saying exactly that. But before it shows that message, it will attempt to synthesize an instance of `CoeT Nat x Int`, which will end up going through all the other typeclasses defined below, to discover that there is an instance of `Coe Nat Int` defined. This instance is defined as: ``` instance : Coe Nat Int := ⟨Int.ofNat⟩ ``` so Lean will elaborate the original function `f` as if it said: ``` def f (x : Nat) : Int := Int.ofNat x ``` which is not a type error anymore. You can also use the `↑` operator to explicitly indicate a coercion. Using `↑x` instead of `x` in the example will result in the same output. Because there are many polymorphic functions in Lean, it is often ambiguous where the coercion can go. For example: ``` def f (x y : Nat) : Int := x + y ``` This could be either `↑x + ↑y` where `+` is the addition on `Int`, or `↑(x + y)` where `+` is addition on `Nat`, or even `x + y` using a heterogeneous addition with the type `Nat → Nat → Int`. You can use the `↑` operator to disambiguate between these possibilities, but generally Lean will elaborate working from the "outside in", meaning that it will first look at the expression `_ + _ : Int` and assign the `+` to be the one for `Int`, and then need to insert coercions for the subterms `↑x : Int` and `↑y : Int`, resulting in the `↑x + ↑y` version. ## Important typeclasses * `Coe α β` is the most basic class, and the usual one you will want to use when implementing a coercion for your own types. * `CoeDep α (x : α) β` allows `β` to depend not only on `α` but on the value `x : α` itself. This is useful when the coercion function is dependent. An example of a dependent coercion is the instance for `Prop → Bool`, because it only holds for `Decidable` propositions. It is defined as: ``` instance (p : Prop) [Decidable p] : CoeDep Prop p Bool := ... ``` * `CoeFun α (γ : α → Sort v)` is a coercion to a function. `γ a` should be a (coercion-to-)function type, and this is triggered whenever an element `f : α` appears in an application like `f x` which would not make sense since `f` does not have a function type. This is automatically turned into `CoeFun.coe f x`. * `CoeSort α β` is a coercion to a sort. `β` must be a universe, and if `a : α` appears in a place where a type is expected, like `(x : a)` or `a → a`, then it will be turned into `(x : CoeSort.coe a)`. * `CoeHead` is like `Coe`, but while `Coe` can be transitively chained in the `CoeT` class, `CoeHead` can only appear once and only at the start of such a chain. This is useful when the transitive instances are not well behaved. * `CoeTail` is similar: it can only appear at the end of a chain of coercions. * `CoeT α (x : α) β` itself is the combination of all the aforementioned classes (except `CoeSort` and `CoeFun` which have different triggers). You can implement `CoeT` if you do not want this coercion to be transitively composed with any other coercions. Note that unlike most operators like `+`, `↑` is always eagerly unfolded at parse time into its definition. So if we look at the definition of `f` from before, we see no trace of the `CoeT.coe` function: ``` def f (x : Nat) : Int := x #print f -- def f : Nat → Int := -- fun (x : Nat) => Int.ofNat x ``` -/ universe u v w w' /-- `Coe α β` is the typeclass for coercions from `α` to `β`. It can be transitively chained with other `Coe` instances, and coercion is automatically used when `x` has type `α` but it is used in a context where `β` is expected. You can use the `↑x` operator to explicitly trigger coercion. -/ class Coe (α : Sort u) (β : Sort v) where /-- Coerces a value of type `α` to type `β`. Accessible by the notation `↑x`, or by double type ascription `((x : α) : β)`. -/ coe : α → β /-- Auxiliary class that contains the transitive closure of `Coe`. Users should generally not implement this directly. -/ class CoeTC (α : Sort u) (β : Sort v) where /-- Coerces a value of type `α` to type `β`. Accessible by the notation `↑x`, or by double type ascription `((x : α) : β)`. -/ coe : α → β /-- `CoeHead α β` is for coercions that can only appear at the beginning of a sequence of coercions. That is, `β` can be further coerced via `Coe β γ` and `CoeTail γ δ` instances but `α` will only be the inferred type of the input. -/ class CoeHead (α : Sort u) (β : Sort v) where /-- Coerces a value of type `α` to type `β`. Accessible by the notation `↑x`, or by double type ascription `((x : α) : β)`. -/ coe : α → β /-- `CoeTail α β` is for coercions that can only appear at the end of a sequence of coercions. That is, `α` can be further coerced via `Coe σ α` and `CoeHead τ σ` instances but `β` will only be the expected type of the expression. -/ class CoeTail (α : Sort u) (β : Sort v) where /-- Coerces a value of type `α` to type `β`. Accessible by the notation `↑x`, or by double type ascription `((x : α) : β)`. -/ coe : α → β /-- Auxiliary class that contains `CoeHead` + `CoeTC` + `CoeTail`. A `CoeHTCT` chain has the "grammar" `(CoeHead)? (Coe)* (CoeTail)?`, except that the empty sequence is not allowed. -/ class CoeHTCT (α : Sort u) (β : Sort v) where /-- Coerces a value of type `α` to type `β`. Accessible by the notation `↑x`, or by double type ascription `((x : α) : β)`. -/ coe : α → β /-- `CoeDep α (x : α) β` is a typeclass for dependent coercions, that is, the type `β` can depend on `x` (or rather, the value of `x` is available to typeclass search so an instance that relates `β` to `x` is allowed). Dependent coercions do not participate in the transitive chaining process of regular coercions: they must exactly match the type mismatch on both sides. -/ class CoeDep (α : Sort u) (_ : α) (β : Sort v) where /-- The resulting value of type `β`. The input `x : α` is a parameter to the type class, so the value of type `β` may possibly depend on additional typeclasses on `x`. -/ coe : β /-- `CoeT` is the core typeclass which is invoked by Lean to resolve a type error. It can also be triggered explicitly with the notation `↑x` or by double type ascription `((x : α) : β)`. A `CoeT` chain has the "grammar" `(CoeHead)? (Coe)* (CoeTail)? | CoeDep`, except that the empty sequence is not allowed (identity coercions don't need the coercion system at all). -/ class CoeT (α : Sort u) (_ : α) (β : Sort v) where /-- The resulting value of type `β`. The input `x : α` is a parameter to the type class, so the value of type `β` may possibly depend on additional typeclasses on `x`. -/ coe : β /-- `CoeFun α (γ : α → Sort v)` is a coercion to a function. `γ a` should be a (coercion-to-)function type, and this is triggered whenever an element `f : α` appears in an application like `f x` which would not make sense since `f` does not have a function type. This is automatically turned into `CoeFun.coe f x`. -/ class CoeFun (α : Sort u) (γ : outParam (α → Sort v)) where /-- Coerces a value `f : α` to type `γ f`, which should be either be a function type or another `CoeFun` type, in order to resolve a mistyped application `f x`. -/ coe : (f : α) → γ f /-- `CoeSort α β` is a coercion to a sort. `β` must be a universe, and if `a : α` appears in a place where a type is expected, like `(x : a)` or `a → a`, then it will be turned into `(x : CoeSort.coe a)`. -/ class CoeSort (α : Sort u) (β : outParam (Sort v)) where /-- Coerces a value of type `α` to `β`, which must be a universe. -/ coe : α → β /-- `↑x` represents a coercion, which converts `x` of type `α` to type `β`, using typeclasses to resolve a suitable conversion function. You can often leave the `↑` off entirely, since coercion is triggered implicitly whenever there is a type error, but in ambiguous cases it can be useful to use `↑` to disambiguate between e.g. `↑x + ↑y` and `↑(x + y)`. -/ syntax:1024 (name := coeNotation) "↑" term:1024 : term instance coeTrans {α : Sort u} {β : Sort v} {δ : Sort w} [Coe β δ] [CoeTC α β] : CoeTC α δ where coe a := Coe.coe (CoeTC.coe a : β) instance coeBase {α : Sort u} {β : Sort v} [Coe α β] : CoeTC α β where coe a := Coe.coe a instance coeOfHeafOfTCOfTail {α : Sort u} {β : Sort v} {δ : Sort w} {γ : Sort w'} [CoeHead α β] [CoeTail δ γ] [CoeTC β δ] : CoeHTCT α γ where coe a := CoeTail.coe (CoeTC.coe (CoeHead.coe a : β) : δ) instance coeOfHeadOfTC {α : Sort u} {β : Sort v} {δ : Sort w} [CoeHead α β] [CoeTC β δ] : CoeHTCT α δ where coe a := CoeTC.coe (CoeHead.coe a : β) instance coeOfTCOfTail {α : Sort u} {β : Sort v} {δ : Sort w} [CoeTail β δ] [CoeTC α β] : CoeHTCT α δ where coe a := CoeTail.coe (CoeTC.coe a : β) instance coeOfHeadOfTail {α : Sort u} {β : Sort v} {γ : Sort w} [CoeHead α β] [CoeTail β γ] : CoeHTCT α γ where coe a := CoeTail.coe (CoeHead.coe a : β) instance coeOfHead {α : Sort u} {β : Sort v} [CoeHead α β] : CoeHTCT α β where coe a := CoeHead.coe a instance coeOfTail {α : Sort u} {β : Sort v} [CoeTail α β] : CoeHTCT α β where coe a := CoeTail.coe a instance coeOfTC {α : Sort u} {β : Sort v} [CoeTC α β] : CoeHTCT α β where coe a := CoeTC.coe a instance coeOfHTCT {α : Sort u} {β : Sort v} [CoeHTCT α β] (a : α) : CoeT α a β where coe := CoeHTCT.coe a instance coeOfDep {α : Sort u} {β : Sort v} (a : α) [CoeDep α a β] : CoeT α a β where coe := CoeDep.coe a instance coeId {α : Sort u} (a : α) : CoeT α a α where coe := a instance coeSortToCoeTail [inst : CoeSort α β] : CoeTail α β where coe := inst.coe /-! # Basic instances -/ @[inline] instance boolToProp : Coe Bool Prop where coe b := Eq b true instance boolToSort : CoeSort Bool Prop where coe b := Eq b true instance decPropToBool (p : Prop) [Decidable p] : CoeDep Prop p Bool where coe := decide p instance optionCoe {α : Type u} : CoeTail α (Option α) where coe := some instance subtypeCoe {α : Sort u} {p : α → Prop} : CoeHead (Subtype p) α where coe v := v.val /-! # Coe bridge -/ /-- Helper definition used by the elaborator. It is not meant to be used directly by users. This is used for coercions between monads, in the case where we want to apply a monad lift and a coercion on the result type at the same time. -/ @[inline] def Lean.Internal.liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β := do let a ← liftM x pure (CoeT.coe a) /-- Helper definition used by the elaborator. It is not meant to be used directly by users. This is used for coercing the result type under a monad. -/ @[inline] def Lean.Internal.coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β := do let a ← x pure (CoeT.coe a) instance [CoeFun α β] (a : α) : CoeDep α a (β a) where coe := CoeFun.coe a instance [CoeFun α (fun _ => β)] : CoeTail α β where coe a := CoeFun.coe a instance [CoeSort α β] : CoeTail α β where coe a := CoeSort.coe a
f0be1410ca2f942f9b0315e62e1fd0c24821cdff
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/inequality/02.lean
02458a4c7fd2513354c4002e24c545bbd901b44d
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
66
lean
lemma le_refl (x : mynat) : x ≤ x := begin use 0, ring, end
f9fb3edac41d4eeb148d18bd4c42a0fdda353dda
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/model_theory/finitely_generated.lean
2eee09bdd08a29bdcfa0a59588f266aba4a46bfc
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
9,441
lean
/- Copyright (c) 2022 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import model_theory.substructures /-! # Finitely Generated First-Order Structures > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines what it means for a first-order (sub)structure to be finitely or countably generated, similarly to other finitely-generated objects in the algebra library. ## Main Definitions * `first_order.language.substructure.fg` indicates that a substructure is finitely generated. * `first_order.language.Structure.fg` indicates that a structure is finitely generated. * `first_order.language.substructure.cg` indicates that a substructure is countably generated. * `first_order.language.Structure.cg` indicates that a structure is countably generated. ## TODO Develop a more unified definition of finite generation using the theory of closure operators, or use this definition of finite generation to define the others. -/ open_locale first_order open set namespace first_order namespace language open Structure variables {L : language} {M : Type*} [L.Structure M] namespace substructure /-- A substructure of `M` is finitely generated if it is the closure of a finite subset of `M`. -/ def fg (N : L.substructure M) : Prop := ∃ S : finset M, closure L ↑S = N theorem fg_def {N : L.substructure M} : N.fg ↔ ∃ S : set M, S.finite ∧ closure L S = N := ⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin rintro ⟨t', h, rfl⟩, rcases finite.exists_finset_coe h with ⟨t, rfl⟩, exact ⟨t, rfl⟩ end⟩ lemma fg_iff_exists_fin_generating_family {N : L.substructure M} : N.fg ↔ ∃ (n : ℕ) (s : fin n → M), closure L (range s) = N := begin rw fg_def, split, { rintros ⟨S, Sfin, hS⟩, obtain ⟨n, f, rfl⟩ := Sfin.fin_embedding, exact ⟨n, f, hS⟩, }, { rintros ⟨n, s, hs⟩, refine ⟨range s, finite_range s, hs⟩ }, end theorem fg_bot : (⊥ : L.substructure M).fg := ⟨∅, by rw [finset.coe_empty, closure_empty]⟩ theorem fg_closure {s : set M} (hs : s.finite) : fg (closure L s) := ⟨hs.to_finset, by rw [hs.coe_to_finset]⟩ theorem fg_closure_singleton (x : M) : fg (closure L ({x} : set M)) := fg_closure (finite_singleton x) theorem fg.sup {N₁ N₂ : L.substructure M} (hN₁ : N₁.fg) (hN₂ : N₂.fg) : (N₁ ⊔ N₂).fg := let ⟨t₁, ht₁⟩ := fg_def.1 hN₁, ⟨t₂, ht₂⟩ := fg_def.1 hN₂ in fg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [closure_union, ht₁.2, ht₂.2]⟩ theorem fg.map {N : Type*} [L.Structure N] (f : M →[L] N) {s : L.substructure M} (hs : s.fg) : (s.map f).fg := let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, ht.1.image _, by rw [closure_image, ht.2]⟩ theorem fg.of_map_embedding {N : Type*} [L.Structure N] (f : M ↪[L] N) {s : L.substructure M} (hs : (s.map f.to_hom).fg) : s.fg := begin rcases hs with ⟨t, h⟩, rw fg_def, refine ⟨f ⁻¹' t, t.finite_to_set.preimage (f.injective.inj_on _), _⟩, have hf : function.injective f.to_hom := f.injective, refine map_injective_of_injective hf _, rw [← h, map_closure, embedding.coe_to_hom, image_preimage_eq_of_subset], intros x hx, have h' := subset_closure hx, rw h at h', exact hom.map_le_range h' end /-- A substructure of `M` is countably generated if it is the closure of a countable subset of `M`. -/ def cg (N : L.substructure M) : Prop := ∃ S : set M, S.countable ∧ closure L S = N theorem cg_def {N : L.substructure M} : N.cg ↔ ∃ S : set M, S.countable ∧ closure L S = N := iff.refl _ theorem fg.cg {N : L.substructure M} (h : N.fg) : N.cg := begin obtain ⟨s, hf, rfl⟩ := fg_def.1 h, refine ⟨s, hf.countable, rfl⟩, end lemma cg_iff_empty_or_exists_nat_generating_family {N : L.substructure M} : N.cg ↔ (↑N = (∅ : set M)) ∨ ∃ (s : ℕ → M), closure L (range s) = N := begin rw cg_def, split, { rintros ⟨S, Scount, hS⟩, cases eq_empty_or_nonempty ↑N with h h, { exact or.intro_left _ h }, obtain ⟨f, h'⟩ := (Scount.union (set.countable_singleton h.some)).exists_eq_range (singleton_nonempty h.some).inr, refine or.intro_right _ ⟨f, _⟩, rw [← h', closure_union, hS, sup_eq_left, closure_le], exact singleton_subset_iff.2 h.some_mem }, { intro h, cases h with h h, { refine ⟨∅, countable_empty, closure_eq_of_le (empty_subset _) _⟩, rw [← set_like.coe_subset_coe, h], exact empty_subset _ }, { obtain ⟨f, rfl⟩ := h, exact ⟨range f, countable_range _, rfl⟩ } }, end theorem cg_bot : (⊥ : L.substructure M).cg := fg_bot.cg theorem cg_closure {s : set M} (hs : s.countable) : cg (closure L s) := ⟨s, hs, rfl⟩ theorem cg_closure_singleton (x : M) : cg (closure L ({x} : set M)) := (fg_closure_singleton x).cg theorem cg.sup {N₁ N₂ : L.substructure M} (hN₁ : N₁.cg) (hN₂ : N₂.cg) : (N₁ ⊔ N₂).cg := let ⟨t₁, ht₁⟩ := cg_def.1 hN₁, ⟨t₂, ht₂⟩ := cg_def.1 hN₂ in cg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [closure_union, ht₁.2, ht₂.2]⟩ theorem cg.map {N : Type*} [L.Structure N] (f : M →[L] N) {s : L.substructure M} (hs : s.cg) : (s.map f).cg := let ⟨t, ht⟩ := cg_def.1 hs in cg_def.2 ⟨f '' t, ht.1.image _, by rw [closure_image, ht.2]⟩ theorem cg.of_map_embedding {N : Type*} [L.Structure N] (f : M ↪[L] N) {s : L.substructure M} (hs : (s.map f.to_hom).cg) : s.cg := begin rcases hs with ⟨t, h1, h2⟩, rw cg_def, refine ⟨f ⁻¹' t, h1.preimage f.injective, _⟩, have hf : function.injective f.to_hom := f.injective, refine map_injective_of_injective hf _, rw [← h2, map_closure, embedding.coe_to_hom, image_preimage_eq_of_subset], intros x hx, have h' := subset_closure hx, rw h2 at h', exact hom.map_le_range h' end theorem cg_iff_countable [countable (Σl, L.functions l)] {s : L.substructure M} : s.cg ↔ countable s := begin refine ⟨_, λ h, ⟨s, h.to_set, s.closure_eq⟩⟩, rintro ⟨s, h, rfl⟩, exact h.substructure_closure L end end substructure open substructure namespace Structure variables (L) (M) /-- A structure is finitely generated if it is the closure of a finite subset. -/ class fg : Prop := (out : (⊤ : L.substructure M).fg) /-- A structure is countably generated if it is the closure of a countable subset. -/ class cg : Prop := (out : (⊤ : L.substructure M).cg) variables {L M} lemma fg_def : fg L M ↔ (⊤ : L.substructure M).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `Structure.fg` in terms of `set.finite` instead of `finset`. -/ lemma fg_iff : fg L M ↔ ∃ S : set M, S.finite ∧ closure L S = (⊤ : L.substructure M) := by rw [fg_def, substructure.fg_def] lemma fg.range {N : Type*} [L.Structure N] (h : fg L M) (f : M →[L] N) : f.range.fg := begin rw [hom.range_eq_map], exact (fg_def.1 h).map f, end lemma fg.map_of_surjective {N : Type*} [L.Structure N] (h : fg L M) (f : M →[L] N) (hs : function.surjective f) : fg L N := begin rw ← hom.range_eq_top at hs, rw [fg_def, ← hs], exact h.range f, end lemma cg_def : cg L M ↔ (⊤ : L.substructure M).cg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `Structure.cg`. -/ lemma cg_iff : cg L M ↔ ∃ S : set M, S.countable ∧ closure L S = (⊤ : L.substructure M) := by rw [cg_def, substructure.cg_def] lemma cg.range {N : Type*} [L.Structure N] (h : cg L M) (f : M →[L] N) : f.range.cg := begin rw [hom.range_eq_map], exact (cg_def.1 h).map f, end lemma cg.map_of_surjective {N : Type*} [L.Structure N] (h : cg L M) (f : M →[L] N) (hs : function.surjective f) : cg L N := begin rw ← hom.range_eq_top at hs, rw [cg_def, ← hs], exact h.range f, end lemma cg_iff_countable [countable (Σl, L.functions l)] : cg L M ↔ countable M := by rw [cg_def, cg_iff_countable, top_equiv.to_equiv.countable_iff] lemma fg.cg (h : fg L M) : cg L M := cg_def.2 (fg_def.1 h).cg @[priority 100] instance cg_of_fg [h : fg L M] : cg L M := h.cg end Structure lemma equiv.fg_iff {N : Type*} [L.Structure N] (f : M ≃[L] N) : Structure.fg L M ↔ Structure.fg L N := ⟨λ h, h.map_of_surjective f.to_hom f.to_equiv.surjective, λ h, h.map_of_surjective f.symm.to_hom f.to_equiv.symm.surjective⟩ lemma substructure.fg_iff_Structure_fg (S : L.substructure M) : S.fg ↔ Structure.fg L S := begin rw Structure.fg_def, refine ⟨λ h, fg.of_map_embedding S.subtype _, λ h, _⟩, { rw [← hom.range_eq_map, range_subtype], exact h }, { have h := h.map S.subtype.to_hom, rw [← hom.range_eq_map, range_subtype] at h, exact h } end lemma equiv.cg_iff {N : Type*} [L.Structure N] (f : M ≃[L] N) : Structure.cg L M ↔ Structure.cg L N := ⟨λ h, h.map_of_surjective f.to_hom f.to_equiv.surjective, λ h, h.map_of_surjective f.symm.to_hom f.to_equiv.symm.surjective⟩ lemma substructure.cg_iff_Structure_cg (S : L.substructure M) : S.cg ↔ Structure.cg L S := begin rw Structure.cg_def, refine ⟨λ h, cg.of_map_embedding S.subtype _, λ h, _⟩, { rw [← hom.range_eq_map, range_subtype], exact h }, { have h := h.map S.subtype.to_hom, rw [← hom.range_eq_map, range_subtype] at h, exact h } end end language end first_order
833af7e0a4a87efe4b05418d0782584353c1e820
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/small.lean
20639dc5bfedded872c1df0fa62d934416a612a5
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
754
lean
/- Categories which are small relative to a cardinal κ. κ-filtered categories. Normally we care about these concepts for categories which are used to index (co)limits, so we work with small_categories. -/ import category_theory.category import category_theory.functor import category_theory.limits.shape -- for cocone import set_theory.cardinal universe u namespace category_theory variables (κ : cardinal.{u}) def is_kappa_small (I : Type u) [small_category.{u} I] : Prop := cardinal.mk (Σ (a b : I), a ⟶ b) < κ structure kappa_filtered (C : Type u) [small_category.{u} C] : Prop := (has_cocones : ∀ (I : Type u) [small_category.{u} I] (hI : is_kappa_small κ I) (F : I ⥤ C), nonempty (limits.cocone F)) end category_theory
c483491172c02c88b3a5283f99bcf741ac73b514
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/analysis/special_functions/integrals.lean
caa061c3cde04a147c6432ac2a9d118c63340854
[ "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
8,978
lean
/- Copyright (c) 2021 Benjamin Davidson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Benjamin Davidson -/ import measure_theory.interval_integral /-! # Integration of specific interval integrals This file contains proofs of the integrals of various simple functions, including `pow`, `exp`, `inv`/`one_div`, `sin`, `cos`, and `λ x, 1 / (1 + x^2)`. There are also facts about more complicated integrals: * `sin x ^ n`: We prove a recursive formula for `sin x ^ (n + 2)` in terms of `sin x ^ n`, along with explicit product formulas for even and odd `n`. With these lemmas, many simple integrals can be computed by `simp` or `norm_num`. See `test/integration.lean` for specific examples. This file is still being developed. -/ open real set open_locale real big_operators variables {a b : ℝ} namespace interval_integral open measure_theory variables {f : ℝ → ℝ} {μ ν : measure ℝ} [locally_finite_measure μ] @[simp] lemma integral_const_mul (c : ℝ) : ∫ x in a..b, c * f x = c * ∫ x in a..b, f x := integral_smul c @[simp] lemma integral_mul_const (c : ℝ) : ∫ x in a..b, f x * c = (∫ x in a..b, f x) * c := by simp only [mul_comm, integral_const_mul] @[simp] lemma integral_div (c : ℝ) : ∫ x in a..b, f x / c = (∫ x in a..b, f x) / c := integral_mul_const c⁻¹ @[simp] lemma interval_integrable_pow (n : ℕ) : interval_integrable (λ x, x^n) μ a b := (continuous_pow n).interval_integrable a b @[simp] lemma interval_integrable_id : interval_integrable (λ x, x) μ a b := continuous_id.interval_integrable a b @[simp] lemma interval_integrable_const (c : ℝ) : interval_integrable (λ x, c) μ a b := continuous_const.interval_integrable a b @[simp] lemma interval_integrable.const_mul (c : ℝ) (h : interval_integrable f ν a b) : interval_integrable (λ x, c * f x) ν a b := by convert h.smul c @[simp] lemma interval_integrable.mul_const (c : ℝ) (h : interval_integrable f ν a b) : interval_integrable (λ x, f x * c) ν a b := by simp only [mul_comm, interval_integrable.const_mul c h] @[simp] lemma interval_integrable.div (c : ℝ) (h : interval_integrable f ν a b) : interval_integrable (λ x, f x / c) ν a b := interval_integrable.mul_const c⁻¹ h lemma interval_integrable_one_div (h : ∀ x : ℝ, x ∈ interval a b → f x ≠ 0) (hf : continuous_on f (interval a b)) : interval_integrable (λ x, 1 / f x) μ a b := (continuous_on_const.div hf h).interval_integrable @[simp] lemma interval_integrable_inv (h : ∀ x : ℝ, x ∈ interval a b → f x ≠ 0) (hf : continuous_on f (interval a b)) : interval_integrable (λ x, (f x)⁻¹) μ a b := by simpa only [one_div] using interval_integrable_one_div h hf @[simp] lemma interval_integrable_exp : interval_integrable exp μ a b := continuous_exp.interval_integrable a b @[simp] lemma interval_integrable_sin : interval_integrable sin μ a b := continuous_sin.interval_integrable a b @[simp] lemma interval_integrable_cos : interval_integrable cos μ a b := continuous_cos.interval_integrable a b lemma interval_integrable_one_div_one_add_sq : interval_integrable (λ x:ℝ, 1 / (1 + x^2)) μ a b := begin refine (continuous_const.div _ (λ x, _)).interval_integrable a b, { continuity }, { nlinarith }, end @[simp] lemma interval_integrable_inv_one_add_sq : interval_integrable (λ x:ℝ, (1 + x^2)⁻¹) μ a b := by simpa only [one_div] using interval_integrable_one_div_one_add_sq end interval_integral open interval_integral @[simp] lemma integral_pow (n : ℕ) : ∫ x in a..b, x ^ n = (b^(n+1) - a^(n+1)) / (n + 1) := begin have hderiv : deriv (λ x : ℝ, x^(n + 1) / (n + 1)) = λ x, x ^ n, { have hne : (n + 1 : ℝ) ≠ 0 := by exact_mod_cast nat.succ_ne_zero n, ext, simp [mul_div_assoc, mul_div_cancel' _ hne] }, rw integral_deriv_eq_sub' _ hderiv; norm_num [div_sub_div_same, (continuous_pow n).continuous_on], end @[simp] lemma integral_id : ∫ x in a..b, x = (b^2 - a^2) / 2 := by simpa using integral_pow 1 @[simp] lemma integral_one : ∫ x in a..b, (1:ℝ) = b - a := by simp @[simp] lemma integral_exp : ∫ x in a..b, exp x = exp b - exp a := by rw integral_deriv_eq_sub'; norm_num [continuous_exp.continuous_on] @[simp] lemma integral_inv (h : (0:ℝ) ∉ interval a b) : ∫ x in a..b, x⁻¹ = log (b / a) := begin have h' := λ x hx, ne_of_mem_of_not_mem hx h, rw [integral_deriv_eq_sub' _ deriv_log' (λ x hx, differentiable_at_log (h' x hx)) (continuous_on_inv'.mono (subset_compl_singleton_iff.mpr h)), log_div (h' b right_mem_interval) (h' a left_mem_interval)], end @[simp] lemma integral_inv_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_interval_of_lt ha hb @[simp] lemma integral_inv_of_neg (ha : a < 0) (hb : b < 0) : ∫ x in a..b, x⁻¹ = log (b / a) := integral_inv $ not_mem_interval_of_gt ha hb lemma integral_one_div (h : (0:ℝ) ∉ interval a b) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv h] lemma integral_one_div_of_pos (ha : 0 < a) (hb : 0 < b) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_pos ha hb] lemma integral_one_div_of_neg (ha : a < 0) (hb : b < 0) : ∫ x : ℝ in a..b, 1/x = log (b / a) := by simp only [one_div, integral_inv_of_neg ha hb] @[simp] lemma integral_sin : ∫ x in a..b, sin x = cos a - cos b := by rw integral_deriv_eq_sub' (λ x, -cos x); norm_num [continuous_on_sin] lemma integral_sin_pow_aux (n : ℕ) : ∫ x in 0..π, sin x ^ (n + 2) = ((n + 1) * ∫ x in 0..π, sin x ^ n) - (n + 1) * ∫ x in 0..π, sin x ^ (n + 2) := begin have hv : ∀ x ∈ interval 0 π, has_deriv_at (-cos) (sin x) x, { intros, convert (has_deriv_at_cos x).neg, rw neg_neg }, have hu : ∀ x ∈ interval 0 π, has_deriv_at (λ x, sin x ^ (n + 1)) ((n + 1) * cos x * sin x ^ n) x, { intros, convert (has_deriv_at_pow (n + 1) (sin x)).comp x (has_deriv_at_sin x) using 1, simp [mul_right_comm], }, calc ∫ (x : ℝ) in 0..π, sin x ^ (n + 2) = ∫ (x : ℝ) in 0..π, sin x ^ (n + 1) * sin x : by { congr, ext, ring_nf } ... = ∫ (x : ℝ) in 0..π, cos x * (λ (x : ℝ), (↑n + 1) * cos x * sin x ^ n) x : by { simp [integral_mul_deriv_eq_deriv_mul hu hv (by continuity : continuous _).continuous_on (by continuity : continuous _).continuous_on] } ... = (↑n + 1) * ∫ (x : ℝ) in 0..π, cos x ^ 2 * sin x ^ n : by { rw ← integral_const_mul, congr, ext, simp only, ring } ... = (↑n + 1) * ∫ (x : ℝ) in 0..π, sin x ^ n - sin x ^ (n + 2) : by { simp [integral_const_mul, cos_square', sub_mul, ← pow_add, add_comm] } ... = _ - _ : by { rw [integral_sub, mul_sub], { exact ((continuous_pow n).comp continuous_sin).interval_integrable 0 π }, { exact ((continuous_pow (n + 2)).comp continuous_sin).interval_integrable 0 π } }, end lemma integral_sin_pow_succ_succ (n : ℕ) : ∫ x in 0..π, sin x ^ (n + 2) = (n + 1) / (n + 2) * ∫ x in 0..π, sin x ^ n := begin have : (n:ℝ) + 2 ≠ 0 := by exact_mod_cast nat.succ_ne_zero n.succ, field_simp, convert eq_sub_iff_add_eq.mp (integral_sin_pow_aux n), ring, end theorem integral_sin_pow_odd (n : ℕ) : ∫ x in 0..π, sin x ^ (2 * n + 1) = 2 * ∏ i in finset.range n, (2 * i + 2) / (2 * i + 3) := begin induction n with k ih, { norm_num }, rw [finset.prod_range_succ, ← mul_assoc, mul_comm (2:ℝ) ((2 * k + 2) / (2 * k + 3)), mul_assoc, ← ih], have h₁ : 2 * k.succ + 1 = 2 * k + 1 + 2, { ring }, have h₂ : (2:ℝ) * k + 1 + 1 = 2 * k + 2, { norm_cast }, have h₃ : (2:ℝ) * k + 1 + 2 = 2 * k + 3, { norm_cast }, simp [h₁, h₂, h₃, integral_sin_pow_succ_succ (2 * k + 1)], end theorem integral_sin_pow_even (n : ℕ) : ∫ x in 0..π, sin x ^ (2 * n) = π * ∏ i in finset.range n, (2 * i + 1) / (2 * i + 2) := begin induction n with k ih, { norm_num }, rw [finset.prod_range_succ, ← mul_assoc, mul_comm π ((2 * k + 1) / (2 * k + 2)), mul_assoc, ← ih], simp [nat.succ_eq_add_one, mul_add, mul_one, integral_sin_pow_succ_succ _], end lemma integral_sin_pow_pos (n : ℕ) : 0 < ∫ x in 0..π, sin x ^ n := begin rcases nat.even_or_odd' n with ⟨k, h, h⟩; simp only [h, integral_sin_pow_even, integral_sin_pow_odd]; refine mul_pos (by norm_num [pi_pos]) (finset.prod_pos (λ n hn, div_pos _ _)); norm_cast; linarith, end @[simp] lemma integral_cos : ∫ x in a..b, cos x = sin b - sin a := by rw integral_deriv_eq_sub'; norm_num [continuous_on_cos] @[simp] lemma integral_inv_one_add_sq : ∫ x : ℝ in a..b, (1 + x^2)⁻¹ = arctan b - arctan a := begin simp only [← one_div], refine integral_deriv_eq_sub' _ _ _ (continuous_const.div _ (λ x, _)).continuous_on, { norm_num }, { norm_num }, { continuity }, { nlinarith }, end lemma integral_one_div_one_add_sq : ∫ x : ℝ in a..b, 1 / (1 + x^2) = arctan b - arctan a := by simp
28d05f13006d64cde5e69e4e5fcdf450b6dbc955
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/linear_algebra/matrix/transvection.lean
257fefad9b4d0972fd1c49dccbd1ad988fa9c7a9
[ "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
33,163
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 linear_algebra.matrix.determinant import linear_algebra.matrix.trace import linear_algebra.matrix.reindex import tactic.field_simp import data.matrix.basis /-! # Transvections Transvections are matrices of the form `1 + std_basis_matrix i j c`, where `std_basis_matrix i j c` is the basic matrix with a `1` at position `(i, j)`. Multiplying by such a transvection on the left (resp. on the right) amounts to adding `c` times the `j`-th row to to the `i`-th row (resp `c` times the `i`-th column to the `j`-th column). Therefore, they are useful to present algorithms operating on rows and columns. Transvections are a special case of *elementary matrices* (according to most references, these also contain the matrices exchanging rows, and the matrices multiplying a row by a constant). We show that, over a field, any matrix can be written as `L ⬝ D ⬝ L'`, where `L` and `L'` are products of transvections and `D` is diagonal. In other words, one can reduce a matrix to diagonal form by operations on its rows and columns, a variant of Gauss' pivot algorithm. ## Main definitions and results * `transvection i j c` is the matrix equal to `1 + std_basis_matrix i j c`. * `transvection_struct n R` is a structure containing the data of `i, j, c` and a proof that `i ≠ j`. These are often easier to manipulate than straight matrices, especially in inductive arguments. * `exists_list_transvec_mul_diagonal_mul_list_transvec` states that any matrix `M` over a field can be written in the form `t_1 ⬝ ... ⬝ t_k ⬝ D ⬝ t'_1 ⬝ ... ⬝ t'_l`, where `D` is diagonal and the `t_i`, `t'_j` are transvections. * `diagonal_transvection_induction` shows that a property which is true for diagonal matrices and transvections, and invariant under product, is true for all matrices. * `diagonal_transvection_induction_of_det_ne_zero` is the same statement over invertible matrices. ## Implementation details The proof of the reduction results is done inductively on the size of the matrices, reducing an `(r + 1) × (r + 1)` matrix to a matrix whose last row and column are zeroes, except possibly for the last diagonal entry. This step is done as follows. If all the coefficients on the last row and column are zero, there is nothing to do. Otherwise, one can put a nonzero coefficient in the last diagonal entry by a row or column operation, and then subtract this last diagonal entry from the other entries in the last row and column to make them vanish. This step is done in the type `fin r ⊕ unit`, where `fin r` is useful to choose arbitrarily some order in which we cancel the coefficients, and the sum structure is useful to use the formalism of block matrices. To proceed with the induction, we reindex our matrices to reduce to the above situation. -/ universes u₁ u₂ namespace matrix open_locale matrix variables (n p : Type*) (R : Type u₂) {𝕜 : Type*} [field 𝕜] variables [decidable_eq n] [decidable_eq p] variables [comm_ring R] section transvection variables {R n} (i j : n) /-- The transvection matrix `transvection i j c` is equal to the identity plus `c` at position `(i, j)`. Multiplying by it on the left (as in `transvection i j c ⬝ M`) corresponds to adding `c` times the `j`-th line of `M` to its `i`-th line. Multiplying by it on the right corresponds to adding `c` times the `i`-th column to the `j`-th column. -/ def transvection (c : R) : matrix n n R := 1 + matrix.std_basis_matrix i j c @[simp] lemma transvection_zero : transvection i j (0 : R) = 1 := by simp [transvection] section variable [fintype n] /-- A transvection matrix is obtained from the identity by adding `c` times the `j`-th row to the `i`-th row. -/ lemma update_row_eq_transvection (c : R) : update_row (1 : matrix n n R) i (((1 : matrix n n R)) i + c • (1 : matrix n n R) j) = transvection i j c := begin ext a b, by_cases ha : i = a; by_cases hb : j = b, { simp only [update_row, transvection, ha, hb, function.update_same, std_basis_matrix.apply_same, pi.add_apply, one_apply_eq, pi.smul_apply, mul_one, algebra.id.smul_eq_mul], }, { simp only [update_row, transvection, ha, hb, std_basis_matrix.apply_of_ne, function.update_same, pi.add_apply, ne.def, not_false_iff, pi.smul_apply, and_false, one_apply_ne, algebra.id.smul_eq_mul, mul_zero] }, { simp only [update_row, transvection, ha, ne.symm ha, std_basis_matrix.apply_of_ne, add_zero, algebra.id.smul_eq_mul, function.update_noteq, ne.def, not_false_iff, dmatrix.add_apply, pi.smul_apply, mul_zero, false_and] }, { simp only [update_row, transvection, ha, hb, ne.symm ha, std_basis_matrix.apply_of_ne, add_zero, algebra.id.smul_eq_mul, function.update_noteq, ne.def, not_false_iff, and_self, dmatrix.add_apply, pi.smul_apply, mul_zero] } end lemma transvection_mul_transvection_same (h : i ≠ j) (c d : R) : transvection i j c ⬝ transvection i j d = transvection i j (c + d) := by simp [transvection, matrix.add_mul, matrix.mul_add, h, h.symm, add_smul, add_assoc, std_basis_matrix_add] @[simp] lemma transvection_mul_apply_same (b : n) (c : R) (M : matrix n n R) : (transvection i j c ⬝ M) i b = M i b + c * M j b := by simp [transvection, matrix.add_mul] @[simp] lemma mul_transvection_apply_same (a : n) (c : R) (M : matrix n n R) : (M ⬝ transvection i j c) a j = M a j + c * M a i := by simp [transvection, matrix.mul_add, mul_comm] @[simp] lemma transvection_mul_apply_of_ne (a b : n) (ha : a ≠ i) (c : R) (M : matrix n n R) : (transvection i j c ⬝ M) a b = M a b := by simp [transvection, matrix.add_mul, ha] @[simp] lemma mul_transvection_apply_of_ne (a b : n) (hb : b ≠ j) (c : R) (M : matrix n n R) : (M ⬝ transvection i j c) a b = M a b := by simp [transvection, matrix.mul_add, hb] @[simp] lemma det_transvection_of_ne (h : i ≠ j) (c : R) : det (transvection i j c) = 1 := by rw [← update_row_eq_transvection i j, det_update_row_add_smul_self _ h, det_one] end variables (R n) /-- A structure containing all the information from which one can build a nontrivial transvection. This structure is easier to manipulate than transvections as one has a direct access to all the relevant fields. -/ @[nolint has_inhabited_instance] structure transvection_struct := (i j : n) (hij : i ≠ j) (c : R) instance [nontrivial n] : nonempty (transvection_struct n R) := by { choose x y hxy using exists_pair_ne n, exact ⟨⟨x, y, hxy, 0⟩⟩ } namespace transvection_struct variables {R n} /-- Associating to a `transvection_struct` the corresponding transvection matrix. -/ def to_matrix (t : transvection_struct n R) : matrix n n R := transvection t.i t.j t.c @[simp] lemma to_matrix_mk (i j : n) (hij : i ≠ j) (c : R) : transvection_struct.to_matrix ⟨i, j, hij, c⟩ = transvection i j c := rfl @[simp] protected lemma det [fintype n] (t : transvection_struct n R) : det t.to_matrix = 1 := det_transvection_of_ne _ _ t.hij _ @[simp] lemma det_to_matrix_prod [fintype n] (L : list (transvection_struct n 𝕜)) : det ((L.map to_matrix).prod) = 1 := begin induction L with t L IH, { simp }, { simp [IH], } end /-- The inverse of a `transvection_struct`, designed so that `t.inv.to_matrix` is the inverse of `t.to_matrix`. -/ @[simps] protected def inv (t : transvection_struct n R) : transvection_struct n R := { i := t.i, j := t.j, hij := t.hij, c := - t.c } section variable [fintype n] lemma inv_mul (t : transvection_struct n R) : t.inv.to_matrix ⬝ t.to_matrix = 1 := by { rcases t, simp [to_matrix, transvection_mul_transvection_same, t_hij] } lemma mul_inv (t : transvection_struct n R) : t.to_matrix ⬝ t.inv.to_matrix = 1 := by { rcases t, simp [to_matrix, transvection_mul_transvection_same, t_hij] } lemma reverse_inv_prod_mul_prod (L : list (transvection_struct n R)) : (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝ (L.map to_matrix).prod = 1 := begin induction L with t L IH, { simp }, { suffices : (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝ (t.inv.to_matrix ⬝ t.to_matrix) ⬝ (L.map to_matrix).prod = 1, by simpa [matrix.mul_assoc], simpa [inv_mul] using IH, } end lemma prod_mul_reverse_inv_prod (L : list (transvection_struct n R)) : (L.map to_matrix).prod ⬝ (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod = 1 := begin induction L with t L IH, { simp }, { suffices : t.to_matrix ⬝ ((L.map to_matrix).prod ⬝ (L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod) ⬝ t.inv.to_matrix = 1, by simpa [matrix.mul_assoc], simp_rw [IH, matrix.mul_one, t.mul_inv], } end end variables (p) open sum /-- Given a `transvection_struct` on `n`, define the corresponding `transvection_struct` on `n ⊕ p` using the identity on `p`. -/ def sum_inl (t : transvection_struct n R) : transvection_struct (n ⊕ p) R := { i := inl t.i, j := inl t.j, hij := by simp [t.hij], c := t.c } lemma to_matrix_sum_inl (t : transvection_struct n R) : (t.sum_inl p).to_matrix = from_blocks t.to_matrix 0 0 1 := begin cases t, ext a b, cases a; cases b, { by_cases h : a = b; simp [transvection_struct.sum_inl, transvection, h, std_basis_matrix], }, { simp [transvection_struct.sum_inl, transvection] }, { simp [transvection_struct.sum_inl, transvection] }, { by_cases h : a = b; simp [transvection_struct.sum_inl, transvection, h] }, end @[simp] lemma sum_inl_to_matrix_prod_mul [fintype n] [fintype p] (M : matrix n n R) (L : list (transvection_struct n R)) (N : matrix p p R) : (L.map (to_matrix ∘ sum_inl p)).prod ⬝ from_blocks M 0 0 N = from_blocks ((L.map to_matrix).prod ⬝ M) 0 0 N := begin induction L with t L IH, { simp }, { simp [matrix.mul_assoc, IH, to_matrix_sum_inl, from_blocks_multiply], }, end @[simp] lemma mul_sum_inl_to_matrix_prod [fintype n] [fintype p] (M : matrix n n R) (L : list (transvection_struct n R)) (N : matrix p p R) : (from_blocks M 0 0 N) ⬝ (L.map (to_matrix ∘ sum_inl p)).prod = from_blocks (M ⬝ (L.map to_matrix).prod) 0 0 N := begin induction L with t L IH generalizing M N, { simp }, { simp [IH, to_matrix_sum_inl, from_blocks_multiply], }, end variable {p} /-- Given a `transvection_struct` on `n` and an equivalence between `n` and `p`, define the corresponding `transvection_struct` on `p`. -/ def reindex_equiv (e : n ≃ p) (t : transvection_struct n R) : transvection_struct p R := { i := e t.i, j := e t.j, hij := by simp [t.hij], c := t.c } variables [fintype n] [fintype p] lemma to_matrix_reindex_equiv (e : n ≃ p) (t : transvection_struct n R) : (t.reindex_equiv e).to_matrix = reindex_alg_equiv R e t.to_matrix := begin cases t, ext a b, simp only [reindex_equiv, transvection, mul_boole, algebra.id.smul_eq_mul, to_matrix_mk, minor_apply, reindex_apply, dmatrix.add_apply, pi.smul_apply, reindex_alg_equiv_apply], by_cases ha : e t_i = a; by_cases hb : e t_j = b; by_cases hab : a = b; simp [ha, hb, hab, ← e.apply_eq_iff_eq_symm_apply, std_basis_matrix] end lemma to_matrix_reindex_equiv_prod (e : n ≃ p) (L : list (transvection_struct n R)) : (L.map (to_matrix ∘ (reindex_equiv e))).prod = reindex_alg_equiv R e (L.map to_matrix).prod := begin induction L with t L IH, { simp }, { simp only [to_matrix_reindex_equiv, IH, function.comp_app, list.prod_cons, mul_eq_mul, reindex_alg_equiv_apply, list.map], exact (reindex_alg_equiv_mul _ _ _ _).symm } end end transvection_struct end transvection /-! # Reducing matrices by left and right multiplication by transvections In this section, we show that any matrix can be reduced to diagonal form by left and right multiplication by transvections (or, equivalently, by elementary operations on lines and columns). The main step is to kill the last row and column of a matrix in `fin r ⊕ unit` with nonzero last coefficient, by subtracting this coefficient from the other ones. The list of these operations is recorded in `list_transvec_col M` and `list_transvec_row M`. We have to analyze inductively how these operations affect the coefficients in the last row and the last column to conclude that they have the desired effect. Once this is done, one concludes the reduction by induction on the size of the matrices, through a suitable reindexing to identify any fintype with `fin r ⊕ unit`. -/ namespace pivot variables {R} {r : ℕ} (M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) open sum unit fin transvection_struct /-- A list of transvections such that multiplying on the left with these transvections will replace the last column with zeroes. -/ def list_transvec_col : list (matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) := list.of_fn $ λ i : fin r, transvection (inl i) (inr star) $ -M (inl i) (inr star) / M (inr star) (inr star) /-- A list of transvections such that multiplying on the right with these transvections will replace the last row with zeroes. -/ def list_transvec_row : list (matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) := list.of_fn $ λ i : fin r, transvection (inr star) (inl i) $ -M (inr star) (inl i) / M (inr star) (inr star) /-- Multiplying by some of the matrices in `list_transvec_col M` does not change the last row. -/ lemma list_transvec_col_mul_last_row_drop (i : fin r ⊕ unit) {k : ℕ} (hk : k ≤ r) : (((list_transvec_col M).drop k).prod ⬝ M) (inr star) i = M (inr star) i := begin apply nat.decreasing_induction' _ hk, { simp only [list_transvec_col, list.length_of_fn, matrix.one_mul, list.drop_eq_nil_of_le, list.prod_nil], }, { assume n hn hk IH, have hn' : n < (list_transvec_col M).length, by simpa [list_transvec_col] using hn, rw ← list.cons_nth_le_drop_succ hn', simpa [list_transvec_col, matrix.mul_assoc] } end /-- Multiplying by all the matrices in `list_transvec_col M` does not change the last row. -/ lemma list_transvec_col_mul_last_row (i : fin r ⊕ unit) : ((list_transvec_col M).prod ⬝ M) (inr star) i = M (inr star) i := by simpa using list_transvec_col_mul_last_row_drop M i (zero_le _) /-- Multiplying by all the matrices in `list_transvec_col M` kills all the coefficients in the last column but the last one. -/ lemma list_transvec_col_mul_last_col (hM : M (inr star) (inr star) ≠ 0) (i : fin r) : ((list_transvec_col M).prod ⬝ M) (inl i) (inr star) = 0 := begin suffices H : ∀ (k : ℕ), k ≤ r → (((list_transvec_col M).drop k).prod ⬝ M) (inl i) (inr star) = if k ≤ i then 0 else M (inl i) (inr star), by simpa only [if_true, list.drop.equations._eqn_1] using H 0 (zero_le _), assume k hk, apply nat.decreasing_induction' _ hk, { simp only [list_transvec_col, list.length_of_fn, matrix.one_mul, list.drop_eq_nil_of_le, list.prod_nil], rw if_neg, simpa only [not_le] using i.2 }, { assume n hn hk IH, have hn' : n < (list_transvec_col M).length, by simpa [list_transvec_col] using hn, let n' : fin r := ⟨n, hn⟩, rw ← list.cons_nth_le_drop_succ hn', have A : (list_transvec_col M).nth_le n hn' = transvection (inl n') (inr star) (-M (inl n') (inr star) / M (inr star) (inr star)), by simp [list_transvec_col], simp only [matrix.mul_assoc, A, matrix.mul_eq_mul, list.prod_cons], by_cases h : n' = i, { have hni : n = i, { cases i, simp only [subtype.mk_eq_mk] at h, simp [h] }, rw [h, transvection_mul_apply_same, IH, list_transvec_col_mul_last_row_drop _ _ hn, ← hni], field_simp [hM] }, { have hni : n ≠ i, { rintros rfl, cases i, simpa using h }, simp only [transvection_mul_apply_of_ne, ne.def, not_false_iff, ne.symm h], rw IH, rcases le_or_lt (n+1) i with hi|hi, { simp only [hi, n.le_succ.trans hi, if_true] }, { rw [if_neg, if_neg], { simpa only [hni.symm, not_le, or_false] using nat.lt_succ_iff_lt_or_eq.1 hi }, { simpa only [not_le] using hi } } } } end /-- Multiplying by some of the matrices in `list_transvec_row M` does not change the last column. -/ lemma mul_list_transvec_row_last_col_take (i : fin r ⊕ unit) {k : ℕ} (hk : k ≤ r) : (M ⬝ ((list_transvec_row M).take k).prod) i (inr star) = M i (inr star) := begin induction k with k IH, { simp only [matrix.mul_one, list.take_zero, list.prod_nil], }, { have hkr : k < r := hk, let k' : fin r := ⟨k, hkr⟩, have : (list_transvec_row M).nth k = ↑(transvection (inr unit.star) (inl k') (-M (inr unit.star) (inl k') / M (inr unit.star) (inr unit.star))), { simp only [list_transvec_row, list.of_fn_nth_val, hkr, dif_pos, list.nth_of_fn], refl }, simp only [list.take_succ, ← matrix.mul_assoc, this, list.prod_append, matrix.mul_one, matrix.mul_eq_mul, list.prod_cons, list.prod_nil, option.to_list_some], rw [mul_transvection_apply_of_ne, IH hkr.le], simp only [ne.def, not_false_iff], } end /-- Multiplying by all the matrices in `list_transvec_row M` does not change the last column. -/ lemma mul_list_transvec_row_last_col (i : fin r ⊕ unit) : (M ⬝ (list_transvec_row M).prod) i (inr star) = M i (inr star) := begin have A : (list_transvec_row M).length = r, by simp [list_transvec_row], rw [← list.take_length (list_transvec_row M), A], simpa using mul_list_transvec_row_last_col_take M i le_rfl, end /-- Multiplying by all the matrices in `list_transvec_row M` kills all the coefficients in the last row but the last one. -/ lemma mul_list_transvec_row_last_row (hM : M (inr star) (inr star) ≠ 0) (i : fin r) : (M ⬝ (list_transvec_row M).prod) (inr star) (inl i) = 0 := begin suffices H : ∀ (k : ℕ), k ≤ r → (M ⬝ ((list_transvec_row M).take k).prod) (inr star) (inl i) = if k ≤ i then M (inr star) (inl i) else 0, { have A : (list_transvec_row M).length = r, by simp [list_transvec_row], rw [← list.take_length (list_transvec_row M), A], have : ¬ (r ≤ i), by simpa using i.2, simpa only [this, ite_eq_right_iff] using H r le_rfl }, assume k hk, induction k with n IH, { simp only [if_true, matrix.mul_one, list.take_zero, zero_le', list.prod_nil] }, { have hnr : n < r := hk, let n' : fin r := ⟨n, hnr⟩, have A : (list_transvec_row M).nth n = ↑(transvection (inr unit.star) (inl n') (-M (inr unit.star) (inl n') / M (inr unit.star) (inr unit.star))), { simp only [list_transvec_row, list.of_fn_nth_val, hnr, dif_pos, list.nth_of_fn], refl }, simp only [list.take_succ, A, ← matrix.mul_assoc, list.prod_append, matrix.mul_one, matrix.mul_eq_mul, list.prod_cons, list.prod_nil, option.to_list_some], by_cases h : n' = i, { have hni : n = i, { cases i, simp only [subtype.mk_eq_mk] at h, simp only [h, coe_mk] }, have : ¬ (n.succ ≤ i), by simp only [← hni, n.lt_succ_self, not_le], simp only [h, mul_transvection_apply_same, list.take, if_false, mul_list_transvec_row_last_col_take _ _ hnr.le, hni.le, this, if_true, IH hnr.le], field_simp [hM] }, { have hni : n ≠ i, { rintros rfl, cases i, simpa using h }, simp only [IH hnr.le, ne.def, mul_transvection_apply_of_ne, not_false_iff, ne.symm h], rcases le_or_lt (n+1) i with hi|hi, { simp [hi, n.le_succ.trans hi, if_true], }, { rw [if_neg, if_neg], { simpa only [not_le] using hi }, { simpa only [hni.symm, not_le, or_false] using nat.lt_succ_iff_lt_or_eq.1 hi } } } } end /-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` kills all the coefficients in the last row but the last one. -/ lemma list_transvec_col_mul_mul_list_transvec_row_last_col (hM : M (inr star) (inr star) ≠ 0) (i : fin r) : ((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) (inr star) (inl i) = 0 := begin have : list_transvec_row M = list_transvec_row ((list_transvec_col M).prod ⬝ M), by simp [list_transvec_row, list_transvec_col_mul_last_row], rw this, apply mul_list_transvec_row_last_row, simpa [list_transvec_col_mul_last_row] using hM end /-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` kills all the coefficients in the last column but the last one. -/ lemma list_transvec_col_mul_mul_list_transvec_row_last_row (hM : M (inr star) (inr star) ≠ 0) (i : fin r) : ((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) (inl i) (inr star) = 0 := begin have : list_transvec_col M = list_transvec_col (M ⬝ (list_transvec_row M).prod), by simp [list_transvec_col, mul_list_transvec_row_last_col], rw [this, matrix.mul_assoc], apply list_transvec_col_mul_last_col, simpa [mul_list_transvec_row_last_col] using hM end /-- Multiplying by all the matrices either in `list_transvec_col M` and `list_transvec_row M` turns the matrix in block-diagonal form. -/ lemma is_two_block_diagonal_list_transvec_col_mul_mul_list_transvec_row (hM : M (inr star) (inr star) ≠ 0) : is_two_block_diagonal ((list_transvec_col M).prod ⬝ M ⬝ (list_transvec_row M).prod) := begin split, { ext i j, have : j = star, by simp only [eq_iff_true_of_subsingleton], simp [to_blocks₁₂, this, list_transvec_col_mul_mul_list_transvec_row_last_row M hM] }, { ext i j, have : i = star, by simp only [eq_iff_true_of_subsingleton], simp [to_blocks₂₁, this, list_transvec_col_mul_mul_list_transvec_row_last_col M hM] }, end /-- There exist two lists of `transvection_struct` such that multiplying by them on the left and on the right makes a matrix block-diagonal, when the last coefficient is nonzero. -/ lemma exists_is_two_block_diagonal_of_ne_zero (hM : M (inr star) (inr star) ≠ 0) : ∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)), is_two_block_diagonal ((L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod) := begin let L : list (transvection_struct (fin r ⊕ unit) 𝕜) := list.of_fn (λ i : fin r, ⟨inl i, inr star, by simp, -M (inl i) (inr star) / M (inr star) (inr star)⟩), let L' : list (transvection_struct (fin r ⊕ unit) 𝕜) := list.of_fn (λ i : fin r, ⟨inr star, inl i, by simp, -M (inr star) (inl i) / M (inr star) (inr star)⟩), refine ⟨L, L', _⟩, have A : L.map to_matrix = list_transvec_col M, by simp [L, list_transvec_col, (∘)], have B : L'.map to_matrix = list_transvec_row M, by simp [L, list_transvec_row, (∘)], rw [A, B], exact is_two_block_diagonal_list_transvec_col_mul_mul_list_transvec_row M hM end /-- There exist two lists of `transvection_struct` such that multiplying by them on the left and on the right makes a matrix block-diagonal. -/ lemma exists_is_two_block_diagonal_list_transvec_mul_mul_list_transvec (M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) : ∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)), is_two_block_diagonal ((L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod) := begin by_cases H : is_two_block_diagonal M, { refine ⟨list.nil, list.nil, by simpa using H⟩ }, -- we have already proved this when the last coefficient is nonzero by_cases hM : M (inr star) (inr star) ≠ 0, { exact exists_is_two_block_diagonal_of_ne_zero M hM }, -- when the last coefficient is zero but there is a nonzero coefficient on the last row or the -- last column, we will first put this nonzero coefficient in last position, and then argue as -- above. push_neg at hM, simp [not_and_distrib, is_two_block_diagonal, to_blocks₁₂, to_blocks₂₁] at H, have : ∃ (i : fin r), M (inl i) (inr star) ≠ 0 ∨ M (inr star) (inl i) ≠ 0, { cases H, { contrapose! H, ext i j, convert (H i).1, simp only [eq_iff_true_of_subsingleton] }, { contrapose! H, ext i j, convert (H j).2, simp only [eq_iff_true_of_subsingleton] } }, rcases this with ⟨i, h|h⟩, { let M' := transvection (inr unit.star) (inl i) 1 ⬝ M, have hM' : M' (inr star) (inr star) ≠ 0, by simpa [M', hM], rcases exists_is_two_block_diagonal_of_ne_zero M' hM' with ⟨L, L', hLL'⟩, rw matrix.mul_assoc at hLL', refine ⟨L ++ [⟨inr star, inl i, by simp, 1⟩], L', _⟩, simp only [list.map_append, list.prod_append, matrix.mul_one, to_matrix_mk, list.prod_cons, list.prod_nil, mul_eq_mul, list.map, matrix.mul_assoc (L.map to_matrix).prod], exact hLL' }, { let M' := M ⬝ transvection (inl i) (inr star) 1, have hM' : M' (inr star) (inr star) ≠ 0, by simpa [M', hM], rcases exists_is_two_block_diagonal_of_ne_zero M' hM' with ⟨L, L', hLL'⟩, refine ⟨L, ⟨inl i, inr star, by simp, 1⟩ :: L', _⟩, simp only [←matrix.mul_assoc, to_matrix_mk, list.prod_cons, mul_eq_mul, list.map], rw [matrix.mul_assoc (L.map to_matrix).prod], exact hLL' } end /-- Inductive step for the reduction: if one knows that any size `r` matrix can be reduced to diagonal form by elementary operations, then one deduces it for matrices over `fin r ⊕ unit`. -/ lemma exists_list_transvec_mul_mul_list_transvec_eq_diagonal_induction (IH : ∀ (M : matrix (fin r) (fin r) 𝕜), ∃ (L₀ L₀' : list (transvection_struct (fin r) 𝕜)) (D₀ : (fin r) → 𝕜), (L₀.map to_matrix).prod ⬝ M ⬝ (L₀'.map to_matrix).prod = diagonal D₀) (M : matrix (fin r ⊕ unit) (fin r ⊕ unit) 𝕜) : ∃ (L L' : list (transvection_struct (fin r ⊕ unit) 𝕜)) (D : fin r ⊕ unit → 𝕜), (L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D := begin rcases exists_is_two_block_diagonal_list_transvec_mul_mul_list_transvec M with ⟨L₁, L₁', hM⟩, let M' := (L₁.map to_matrix).prod ⬝ M ⬝ (L₁'.map to_matrix).prod, let M'' := to_blocks₁₁ M', rcases IH M'' with ⟨L₀, L₀', D₀, h₀⟩, set c := M' (inr star) (inr star) with hc, refine ⟨L₀.map (sum_inl unit) ++ L₁, L₁' ++ L₀'.map (sum_inl unit), sum.elim D₀ (λ _, M' (inr star) (inr star)), _⟩, suffices : (L₀.map (to_matrix ∘ sum_inl unit)).prod ⬝ M' ⬝ (L₀'.map (to_matrix ∘ sum_inl unit)).prod = diagonal (sum.elim D₀ (λ _, c)), by simpa [M', matrix.mul_assoc, c], have : M' = from_blocks M'' 0 0 (diagonal (λ _, c)), { rw ← from_blocks_to_blocks M', congr, { exact hM.1 }, { exact hM.2 }, { ext i j, rw [hc, to_blocks₂₂], congr } }, rw this, simp [h₀], end variables {n p} [fintype n] [fintype p] /-- Reduction to diagonal form by elementary operations is invariant under reindexing. -/ lemma reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal (M : matrix p p 𝕜) (e : p ≃ n) (H : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜), (L.map to_matrix).prod ⬝ (matrix.reindex_alg_equiv 𝕜 e M) ⬝ (L'.map to_matrix).prod = diagonal D) : ∃ (L L' : list (transvection_struct p 𝕜)) (D : p → 𝕜), (L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D := begin rcases H with ⟨L₀, L₀', D₀, h₀⟩, refine ⟨L₀.map (reindex_equiv e.symm), L₀'.map (reindex_equiv e.symm), D₀ ∘ e, _⟩, have : M = reindex_alg_equiv 𝕜 e.symm (reindex_alg_equiv 𝕜 e M), by simp only [equiv.symm_symm, minor_minor, reindex_apply, minor_id_id, equiv.symm_comp_self, reindex_alg_equiv_apply], rw this, simp only [to_matrix_reindex_equiv_prod, list.map_map, reindex_alg_equiv_apply], simp only [← reindex_alg_equiv_apply, ← reindex_alg_equiv_mul, h₀], simp only [equiv.symm_symm, reindex_apply, minor_diagonal_equiv, reindex_alg_equiv_apply], end /-- Any matrix can be reduced to diagonal form by elementary operations. Formulated here on `Type 0` because we will make an induction using `fin r`. See `exists_list_transvec_mul_mul_list_transvec_eq_diagonal` for the general version (which follows from this one and reindexing). -/ lemma exists_list_transvec_mul_mul_list_transvec_eq_diagonal_aux (n : Type) [fintype n] [decidable_eq n] (M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜), (L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D := begin unfreezingI { induction hn : fintype.card n with r IH generalizing n M }, { refine ⟨list.nil, list.nil, λ _, 1, _⟩, ext i j, rw fintype.card_eq_zero_iff at hn, exact hn.elim' i }, { have e : n ≃ fin r ⊕ unit, { refine fintype.equiv_of_card_eq _, rw hn, convert (@fintype.card_sum (fin r) unit _ _).symm, simp }, apply reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal M e, apply exists_list_transvec_mul_mul_list_transvec_eq_diagonal_induction (λ N, IH (fin r) N (by simp)) } end /-- Any matrix can be reduced to diagonal form by elementary operations. -/ theorem exists_list_transvec_mul_mul_list_transvec_eq_diagonal (M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜), (L.map to_matrix).prod ⬝ M ⬝ (L'.map to_matrix).prod = diagonal D := begin have e : n ≃ fin (fintype.card n) := fintype.equiv_of_card_eq (by simp), apply reindex_exists_list_transvec_mul_mul_list_transvec_eq_diagonal M e, apply exists_list_transvec_mul_mul_list_transvec_eq_diagonal_aux end /-- Any matrix can be written as the product of transvections, a diagonal matrix, and transvections.-/ theorem exists_list_transvec_mul_diagonal_mul_list_transvec (M : matrix n n 𝕜) : ∃ (L L' : list (transvection_struct n 𝕜)) (D : n → 𝕜), M = (L.map to_matrix).prod ⬝ diagonal D ⬝ (L'.map to_matrix).prod := begin rcases exists_list_transvec_mul_mul_list_transvec_eq_diagonal M with ⟨L, L', D, h⟩, refine ⟨L.reverse.map transvection_struct.inv, L'.reverse.map transvection_struct.inv, D, _⟩, suffices : M = ((L.reverse.map (to_matrix ∘ transvection_struct.inv)).prod ⬝ (L.map to_matrix).prod) ⬝ M ⬝ ((L'.map to_matrix).prod ⬝ (L'.reverse.map (to_matrix ∘ transvection_struct.inv)).prod), by simpa [← h, matrix.mul_assoc], rw [reverse_inv_prod_mul_prod, prod_mul_reverse_inv_prod, matrix.one_mul, matrix.mul_one], end end pivot open pivot transvection_struct variables {n} [fintype n] /-- Induction principle for matrices based on transvections: if a property is true for all diagonal matrices, all transvections, and is stable under product, then it is true for all matrices. This is the useful way to say that matrices are generated by diagonal matrices and transvections. We state a slightly more general version: to prove a property for a matrix `M`, it suffices to assume that the diagonal matrices we consider have the same determinant as `M`. This is useful to obtain similar principles for `SLₙ` or `GLₙ`. -/ lemma diagonal_transvection_induction (P : matrix n n 𝕜 → Prop) (M : matrix n n 𝕜) (hdiag : ∀ D : n → 𝕜, det (diagonal D) = det M → P (diagonal D)) (htransvec : ∀ (t : transvection_struct n 𝕜), P t.to_matrix) (hmul : ∀ A B, P A → P B → P (A ⬝ B)) : P M := begin rcases exists_list_transvec_mul_diagonal_mul_list_transvec M with ⟨L, L', D, h⟩, have PD : P (diagonal D) := hdiag D (by simp [h]), suffices H : ∀ (L₁ L₂ : list (transvection_struct n 𝕜)) (E : matrix n n 𝕜), P E → P ((L₁.map to_matrix).prod ⬝ E ⬝ (L₂.map to_matrix).prod), by { rw h, apply H L L', exact PD }, assume L₁ L₂ E PE, induction L₁ with t L₁ IH, { simp only [matrix.one_mul, list.prod_nil, list.map], induction L₂ with t L₂ IH generalizing E, { simpa }, { simp only [←matrix.mul_assoc, list.prod_cons, mul_eq_mul, list.map], apply IH, exact hmul _ _ PE (htransvec _) } }, { simp only [matrix.mul_assoc, list.prod_cons, mul_eq_mul, list.map] at ⊢ IH, exact hmul _ _ (htransvec _) IH } end /-- Induction principle for invertible matrices based on transvections: if a property is true for all invertible diagonal matrices, all transvections, and is stable under product of invertible matrices, then it is true for all invertible matrices. This is the useful way to say that invertible matrices are generated by invertible diagonal matrices and transvections. -/ lemma diagonal_transvection_induction_of_det_ne_zero (P : matrix n n 𝕜 → Prop) (M : matrix n n 𝕜) (hMdet : det M ≠ 0) (hdiag : ∀ D : n → 𝕜, det (diagonal D) ≠ 0 → P (diagonal D)) (htransvec : ∀ (t : transvection_struct n 𝕜), P t.to_matrix) (hmul : ∀ A B, det A ≠ 0 → det B ≠ 0 → P A → P B → P (A ⬝ B)) : P M := begin let Q : matrix n n 𝕜 → Prop := λ N, det N ≠ 0 ∧ P N, have : Q M, { apply diagonal_transvection_induction Q M, { assume D hD, have detD : det (diagonal D) ≠ 0, by { rw hD, exact hMdet }, exact ⟨detD, hdiag _ detD⟩ }, { assume t, exact ⟨by simp, htransvec t⟩ }, { assume A B QA QB, exact ⟨by simp [QA.1, QB.1], hmul A B QA.1 QB.1 QA.2 QB.2⟩ } }, exact this.2 end end matrix
facd04c0d63d076e86e0109dbc94f7cc89f9abd5
26ac254ecb57ffcb886ff709cf018390161a9225
/src/control/functor/multivariate.lean
0cc94dbebda24eb93a6553f40446de9092195706
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
7,629
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Mario Carneiro, Simon Hudon -/ import data.fin2 import data.typevec import logic.function.basic import tactic.basic /-! Functors between the category of tuples of types, and the category Type Features: `mvfunctor n` : the type class of multivariate functors `f <$$> x` : notation for map -/ universes u v w open_locale mvfunctor /-- multivariate functors, i.e. functor between the category of type vectors and the category of Type -/ class mvfunctor {n : ℕ} (F : typevec n → Type*) := (map : Π {α β : typevec n}, (α ⟹ β) → (F α → F β)) localized "infixr ` <$$> `:100 := mvfunctor.map" in mvfunctor variables {n : ℕ} namespace mvfunctor variables {α β γ : typevec.{u} n} {F : typevec.{u} n → Type v} [mvfunctor F] /-- predicate lifting over multivariate functors -/ def liftp {α : typevec n} (p : Π i, α i → Prop) (x : F α) : Prop := ∃ u : F (λ i, subtype (p i)), (λ i, @subtype.val _ (p i)) <$$> u = x /-- relational lifting over multivariate functors -/ def liftr {α : typevec n} (r : Π {i}, α i → α i → Prop) (x y : F α) : Prop := ∃ u : F (λ i, {p : α i × α i // r p.fst p.snd}), (λ i (t : {p : α i × α i // r p.fst p.snd}), t.val.fst) <$$> u = x ∧ (λ i (t : {p : α i × α i // r p.fst p.snd}), t.val.snd) <$$> u = y /-- given `x : F α` and a projection `i` of type vector `α`, `supp x i` is the set of `α.i` contained in `x` -/ def supp {α : typevec n} (x : F α) (i : fin2 n) : set (α i) := { y : α i | ∀ {p}, liftp p x → p i y } theorem of_mem_supp {α : typevec n} {x : F α} {p : Π ⦃i⦄, α i → Prop} (h : liftp p x) (i : fin2 n): ∀ y ∈ supp x i, p y := λ y hy, hy h end mvfunctor /-- laws for `mvfunctor` -/ class is_lawful_mvfunctor {n : ℕ} (F : typevec n → Type*) [mvfunctor F] : Prop := (id_map : ∀ {α : typevec n} (x : F α), typevec.id <$$> x = x) (comp_map : ∀ {α β γ : typevec n} (g : α ⟹ β) (h : β ⟹ γ) (x : F α), (h ⊚ g) <$$> x = h <$$> g <$$> x) open nat typevec namespace mvfunctor export is_lawful_mvfunctor (comp_map) open is_lawful_mvfunctor variables {α β γ : typevec.{u} n} variables {F : typevec.{u} n → Type v} [mvfunctor F] variables (p : α ⟹ repeat n Prop) (r : α ⊗ α ⟹ repeat n Prop) /-- adapt `mvfunctor.liftp` to accept predicates as arrows -/ def liftp' : F α → Prop := mvfunctor.liftp $ λ i x, of_repeat $ p i x /-- adapt `mvfunctor.liftp` to accept relations as arrows -/ def liftr' : F α → F α → Prop := mvfunctor.liftr $ λ i x y, of_repeat $ r i $ typevec.prod.mk _ x y variables [is_lawful_mvfunctor F] @[simp] lemma id_map (x : F α) : typevec.id <$$> x = x := id_map x @[simp] lemma id_map' (x : F α) : (λ i a, a) <$$> x = x := id_map x lemma map_map (g : α ⟹ β) (h : β ⟹ γ) (x : F α) : h <$$> g <$$> x = (h ⊚ g) <$$> x := eq.symm $ comp_map _ _ _ section liftp' variables (F) lemma exists_iff_exists_of_mono {p : F α → Prop} {q : F β → Prop} (f : α ⟹ β) (g : β ⟹ α) (h₀ : f ⊚ g = id) (h₁ : ∀ u : F α, p u ↔ q (f <$$> u)) : (∃ u : F α, p u) ↔ (∃ u : F β, q u) := begin split; rintro ⟨u,h₂⟩; [ use f <$$> u, use g <$$> u ], { apply (h₁ u).mp h₂ }, { apply (h₁ _).mpr _, simp only [mvfunctor.map_map,h₀,is_lawful_mvfunctor.id_map,h₂] }, end variables {F} lemma liftp_def (x : F α) : liftp' p x ↔ ∃ u : F (subtype_ p), subtype_val p <$$> u = x := begin dsimp only [liftp', mvfunctor.liftp], apply exists_iff_exists_of_mono F (to_subtype p) (of_subtype p), { clear x _inst_2 _inst_1 F, dsimp only [comp], ext i x; induction i, { refl }, -- Lean 3.11.0 problem rw [of_subtype,@to_subtype.equations._eqn_2 i_n _ p i_a,i_ih], refl, }, { intro, rw [mvfunctor.map_map,(⊚)], congr', clear x u _inst_2 _inst_1 F, ext i ⟨ x,_ ⟩, induction i; dsimp only [to_subtype, subtype_val], { refl }, { apply i_ih }, } end lemma liftr_def (x y : F α) : liftr' r x y ↔ ∃ u : F (subtype_ r), (typevec.prod.fst ⊚ subtype_val r) <$$> u = x ∧ (typevec.prod.snd ⊚ subtype_val r) <$$> u = y := begin dsimp only [liftr', mvfunctor.liftr], apply exists_iff_exists_of_mono F (to_subtype' r) (of_subtype' r), { clear x y _inst_2 _inst_1 F, dsimp only [comp], ext i x; induction i, { dsimp only [typevec.id], cases x, refl }, -- Lean 3.11.0 problem { rw [of_subtype',@to_subtype'.equations._eqn_2 i_n _ r i_a, i_ih], refl } }, { intro, rw [mvfunctor.map_map,(⊚),mvfunctor.map_map,(⊚)], congr'; clear x y u _inst_2 _inst_1 F, { ext i ⟨ x,_ ⟩, induction i; dsimp only [subtype_val], { refl }, apply i_ih, }, { ext i ⟨x,_⟩, { induction i, { refl }, { rw i_ih (drop_fun r), refl }, } } } end end liftp' end mvfunctor open nat namespace mvfunctor open typevec section liftp_last_pred_iff variables {F : typevec.{u} (n+1) → Type*} [mvfunctor F] [is_lawful_mvfunctor F] {α : typevec.{u} n} variables (p : α ⟹ repeat n Prop) (r : α ⊗ α ⟹ repeat n Prop) open mvfunctor variables {β : Type u} variables (pp : β → Prop) private def f : Π (n α), (λ (i : fin2 (n + 1)), {p_1 // of_repeat (pred_last' α pp i p_1)}) ⟹ λ (i : fin2 (n + 1)), {p_1 : (α ::: β) i // pred_last α pp p_1} | _ α (fin2.fs i) x := ⟨ x.val, cast (by simp only [pred_last]; erw const_iff_true) x.property ⟩ | _ α fin2.fz x := ⟨ x.val, x.property ⟩ private def g : Π (n α), (λ (i : fin2 (n + 1)), {p_1 : (α ::: β) i // pred_last α pp p_1}) ⟹ (λ (i : fin2 (n + 1)), {p_1 // of_repeat (pred_last' α pp i p_1)}) | _ α (fin2.fs i) x := ⟨ x.val, cast (by simp only [pred_last]; erw const_iff_true) x.property ⟩ | _ α fin2.fz x := ⟨ x.val, x.property ⟩ lemma liftp_last_pred_iff {β} (p : β → Prop) (x : F (α ::: β)) : liftp' (pred_last' _ p) x ↔ liftp (pred_last _ p) x := begin dsimp only [liftp,liftp'], apply exists_iff_exists_of_mono F (f _ n α) (g _ n α), { clear x _inst_2 _inst_1 F, ext i ⟨x,_⟩, cases i; refl }, { intros, rw [mvfunctor.map_map,(⊚)], congr'; ext i ⟨x,_⟩; cases i; refl } end open function variables (rr : β → β → Prop) private def f : Π (n α), (λ (i : fin2 (n + 1)), {p_1 : _ × _ // of_repeat (rel_last' α rr i (typevec.prod.mk _ p_1.fst p_1.snd))}) ⟹ λ (i : fin2 (n + 1)), {p_1 : (α ::: β) i × _ // rel_last α rr (p_1.fst) (p_1.snd)} | _ α (fin2.fs i) x := ⟨ x.val, cast (by simp only [rel_last]; erw repeat_eq_iff_eq) x.property ⟩ | _ α fin2.fz x := ⟨ x.val, x.property ⟩ private def g : Π (n α), (λ (i : fin2 (n + 1)), {p_1 : (α ::: β) i × _ // rel_last α rr (p_1.fst) (p_1.snd)}) ⟹ (λ (i : fin2 (n + 1)), {p_1 : _ × _ // of_repeat (rel_last' α rr i (typevec.prod.mk _ p_1.fst p_1.snd))}) | _ α (fin2.fs i) x := ⟨ x.val, cast (by simp only [rel_last]; erw repeat_eq_iff_eq) x.property ⟩ | _ α fin2.fz x := ⟨ x.val, x.property ⟩ lemma liftr_last_rel_iff (x y : F (α ::: β)) : liftr' (rel_last' _ rr) x y ↔ liftr (rel_last _ rr) x y := begin dsimp only [liftr,liftr'], apply exists_iff_exists_of_mono F (f rr _ _) (g rr _ _), { clear x y _inst_2 _inst_1 F, ext i ⟨x,_⟩ : 2, cases i; refl, }, { intros, rw [mvfunctor.map_map,mvfunctor.map_map,(⊚),(⊚)], congr'; ext i ⟨x,_⟩; cases i; refl } end end liftp_last_pred_iff end mvfunctor
a1baaf772f1145707914baa1139ac6108be130d3
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Data/List/Control.lean
78a499d9b749f67ab2a992b94baa3f55365cec20
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
5,636
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Control.Monad import Init.Control.Alternative import Init.Data.List.Basic namespace List universes u v w u₁ u₂ /- Remark: we can define `mapM`, `mapM₂` and `forM` using `Applicative` instead of `Monad`. Example: ``` def mapM {m : Type u → Type v} [Applicative m] {α : Type w} {β : Type u} (f : α → m β) : List α → m (List β) | [] => pure [] | a::as => List.cons <$> (f a) <*> mapM as ``` However, we consider `f <$> a <*> b` an anti-idiom because the generated code may produce unnecessary closure allocations. Suppose `m` is a `Monad`, and it uses the default implementation for `Applicative.seq`. Then, the compiler expands `f <$> a <*> b <*> c` into something equivalent to ``` (Functor.map f a >>= fun g_1 => Functor.map g_1 b) >>= fun g_2 => Functor.map g_2 c ``` In an ideal world, the compiler may eliminate the temporary closures `g_1` and `g_2` after it inlines `Functor.map` and `Monad.bind`. However, this can easily fail. For example, suppose `Functor.map f a >>= fun g_1 => Functor.map g_1 b` expanded into a match-expression. This is not unreasonable and can happen in many different ways, e.g., we are using a monad that may throw exceptions. Then, the compiler has to decide whether it will create a join-point for the continuation of the match or float it. If the compiler decides to float, then it will be able to eliminate the closures, but it may not be feasible since floating match expressions may produce exponential blowup in the code size. Finally, we rarely use `mapM` with something that is not a `Monad`. Users that want to use `mapM` with `Applicative` should use `mapA` instead. -/ @[specialize] def mapM {m : Type u → Type v} [Monad m] {α : Type w} {β : Type u} (f : α → m β) : List α → m (List β) | [] => pure [] | a::as => do b ← f a; bs ← mapM as; pure (b :: bs) @[specialize] def mapM₂ {m : Type u → Type v} [Monad m] {α : Type u₁} {β : Type u₂} {γ : Type u} (f : α → β → m γ) : List α → List β → m (List γ) | a::as, b::bs => do c ← f a b; cs ← mapM₂ as bs; pure (c :: cs) | _, _ => pure [] @[specialize] def mapA {m : Type u → Type v} [Applicative m] {α : Type w} {β : Type u} (f : α → m β) : List α → m (List β) | [] => pure [] | a::as => List.cons <$> f a <*> mapA as @[specialize] def mapA₂ {m : Type u → Type v} [Applicative m] {α : Type u₁} {β : Type u₂} {γ : Type u} (f : α → β → m γ) : List α → List β → m (List γ) | a::as, b::bs => List.cons <$> f a b <*> mapA₂ as bs | _, _ => pure [] @[specialize] def forM {m : Type u → Type v} [Monad m] {α : Type w} {β : Type u} (f : α → m β) : List α → m PUnit | [] => pure ⟨⟩ | h :: t => do f h; forM t @[specialize] def forM₂ {m : Type u → Type v} [Monad m] {α : Type u₁} {β : Type u₂} {γ : Type u} (f : α → β → m γ) : List α → List β → m PUnit | a::as, b::bs => do f a b; forM₂ as bs | _, _ => pure ⟨⟩ @[specialize] def forA {m : Type u → Type v} [Applicative m] {α : Type w} {β : Type u} (f : α → m β) : List α → m PUnit | [] => pure ⟨⟩ | h :: t => f h *> forA t @[specialize] def forA₂ {m : Type u → Type v} [Applicative m] {α : Type u₁} {β : Type u₂} {γ : Type u} (f : α → β → m γ) : List α → List β → m PUnit | a::as, b::bs => f a b *> forA₂ as bs | _, _ => pure ⟨⟩ @[specialize] def filterAuxM {m : Type → Type v} [Monad m] {α : Type} (f : α → m Bool) : List α → List α → m (List α) | [], acc => pure acc | h :: t, acc => do b ← f h; filterAuxM t (cond b (h :: acc) acc) @[inline] def filterM {m : Type → Type v} [Monad m] {α : Type} (f : α → m Bool) (as : List α) : m (List α) := do as ← filterAuxM f as []; pure as.reverse @[inline] def filterRevM {m : Type → Type v} [Monad m] {α : Type} (f : α → m Bool) (as : List α) : m (List α) := filterAuxM f as.reverse [] @[specialize] def foldlM {m : Type u → Type v} [Monad m] {s : Type u} {α : Type w} : (s → α → m s) → s → List α → m s | f, s, [] => pure s | f, s, h :: r => do s' ← f s h; foldlM f s' r @[specialize] def foldrM {m : Type u → Type v} [Monad m] {s : Type u} {α : Type w} : (α → s → m s) → s → List α → m s | f, s, [] => pure s | f, s, h :: r => do s' ← foldrM f s r; f h s' @[specialize] def firstM {m : Type u → Type v} [Monad m] [Alternative m] {α : Type w} {β : Type u} (f : α → m β) : List α → m β | [] => failure | a::as => f a <|> firstM as @[specialize] def anyM {m : Type → Type u} [Monad m] {α : Type v} (f : α → m Bool) : List α → m Bool | [] => pure false | a::as => do b ← f a; match b with | true => pure true | false => anyM as @[specialize] def allM {m : Type → Type u} [Monad m] {α : Type v} (f : α → m Bool) : List α → m Bool | [] => pure true | a::as => do b ← f a; match b with | true => allM as | false => pure false @[specialize] def findM? {m : Type → Type u} [Monad m] {α : Type} (p : α → m Bool) : List α → m (Option α) | [] => pure none | a::as => condM (p a) (pure (some a)) (findM? as) @[specialize] def findSomeM? {m : Type u → Type v} [Monad m] {α : Type w} {β : Type u} (f : α → m (Option β)) : List α → m (Option β) | [] => pure none | a::as => do b? ← f a; match b? with | some b => pure b | none => findSomeM? as end List
1439d95b937a40d4a9e2c43939ff0b005f27f735
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/variables_typestar.lean
8153a194897ae0802c897e3b9618b0605a1e1869
[ "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
23
lean
variables (A B : Type*)
d658b6f72f69872cf92e50c7385712815769d11f
9cba98daa30c0804090f963f9024147a50292fa0
/old/src/classical_lumens.lean
3ce3b7ac1ee18e364d78b6b5ca7936daa6296ed3
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
1,769
lean
import .....math.affine.affine_coordinate_framed_space_lib import .....math.affine.affine_coordinate_transform import ..metrology.dimensions import ..metrology.measurement import data.real.basic noncomputable theory --open real_lib open measurementSystem open aff_fr open aff_lib open aff_trans /- add in a frame here...measurement system attached to quantity or frame? -/ structure classicalHertz : Type := mk :: (sp : aff_lib.affine_coord_space.standard_space ℝ 1) (id : ℕ) -- id serves as unique ID for a given geometric space attribute [reducible] def classicalHertz.build (id : ℕ) : classicalHertz := ⟨aff_lib.affine_coord_space.mk_with_standard ℝ 1, id⟩ noncomputable def classicalHertz.algebra : classicalHertz → aff_lib.affine_coord_space.standard_space ℝ 1 | (classicalHertz.mk sp n) := sp structure classicalHertzQuantity := mk :: (sp : classicalHertz) (val : ℝ) attribute [reducible] def classicalHertzQuantity.build (sp : classicalHertz) (val : vector ℝ 1) := classicalHertzQuantity.mk sp (val.nth 1) attribute [reducible] def classicalHertzQuantity.algebra (s : classicalHertzQuantity) := s.val structure classicalHertzVector := mk :: (sp : classicalHertz) (coords : vector ℝ 1) attribute [reducible] def classicalHertzVector.build (sp : classicalHertz) (coords : vector ℝ 1) := classicalHertzVector.mk sp --⟨[coord], by refl⟩ coords attribute [reducible] def classicalHertzVector.algebra (v : classicalHertzVector) := (aff_lib.affine_coord_space.mk_coord_vec (classicalHertz.algebra v.sp) v.coords) abbreviation classicalHertzBasis := (fin 1) → classicalHertzVector
fe0c2605e41ff72594d0957b536abaf9aa6f415c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/category/CompHaus/basic.lean
44100f91149c3c54b45f06a383fd8fb30529a426
[ "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
9,615
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Bhavik Mehta -/ import category_theory.adjunction.reflective import topology.stone_cech import category_theory.monad.limits import topology.urysohns_lemma import topology.category.Top.limits /-! # The category of Compact Hausdorff Spaces We construct the category of compact Hausdorff spaces. The type of compact Hausdorff spaces is denoted `CompHaus`, and it is endowed with a category instance making it a full subcategory of `Top`. The fully faithful functor `CompHaus ⥤ Top` is denoted `CompHaus_to_Top`. **Note:** The file `topology/category/Compactum.lean` provides the equivalence between `Compactum`, which is defined as the category of algebras for the ultrafilter monad, and `CompHaus`. `Compactum_to_CompHaus` is the functor from `Compactum` to `CompHaus` which is proven to be an equivalence of categories in `Compactum_to_CompHaus.is_equivalence`. See `topology/category/Compactum.lean` for a more detailed discussion where these definitions are introduced. -/ universes v u open category_theory /-- The type of Compact Hausdorff topological spaces. -/ structure CompHaus := (to_Top : Top) [is_compact : compact_space to_Top] [is_hausdorff : t2_space to_Top] namespace CompHaus instance : inhabited CompHaus := ⟨{to_Top := { α := pempty }}⟩ instance : has_coe_to_sort CompHaus Type* := ⟨λ X, X.to_Top⟩ instance {X : CompHaus} : compact_space X := X.is_compact instance {X : CompHaus} : t2_space X := X.is_hausdorff instance category : category CompHaus := induced_category.category to_Top instance concrete_category : concrete_category CompHaus := induced_category.concrete_category _ @[simp] lemma coe_to_Top {X : CompHaus} : (X.to_Top : Type*) = X := rfl variables (X : Type*) [topological_space X] [compact_space X] [t2_space X] /-- A constructor for objects of the category `CompHaus`, taking a type, and bundling the compact Hausdorff topology found by typeclass inference. -/ def of : CompHaus := { to_Top := Top.of X, is_compact := ‹_›, is_hausdorff := ‹_› } @[simp] lemma coe_of : (CompHaus.of X : Type _) = X := rfl /-- Any continuous function on compact Hausdorff spaces is a closed map. -/ lemma is_closed_map {X Y : CompHaus.{u}} (f : X ⟶ Y) : is_closed_map f := λ C hC, (hC.is_compact.image f.continuous).is_closed /-- Any continuous bijection of compact Hausdorff spaces is an isomorphism. -/ lemma is_iso_of_bijective {X Y : CompHaus.{u}} (f : X ⟶ Y) (bij : function.bijective f) : is_iso f := begin let E := equiv.of_bijective _ bij, have hE : continuous E.symm, { rw continuous_iff_is_closed, intros S hS, rw ← E.image_eq_preimage, exact is_closed_map f S hS }, refine ⟨⟨⟨E.symm, hE⟩, _, _⟩⟩, { ext x, apply E.symm_apply_apply }, { ext x, apply E.apply_symm_apply } end /-- Any continuous bijection of compact Hausdorff spaces induces an isomorphism. -/ noncomputable def iso_of_bijective {X Y : CompHaus.{u}} (f : X ⟶ Y) (bij : function.bijective f) : X ≅ Y := by letI := is_iso_of_bijective _ bij; exact as_iso f end CompHaus /-- The fully faithful embedding of `CompHaus` in `Top`. -/ @[simps {rhs_md := semireducible}, derive [full, faithful]] def CompHaus_to_Top : CompHaus.{u} ⥤ Top.{u} := induced_functor _ instance CompHaus.forget_reflects_isomorphisms : reflects_isomorphisms (forget CompHaus.{u}) := ⟨by introsI A B f hf; exact CompHaus.is_iso_of_bijective _ ((is_iso_iff_bijective f).mp hf)⟩ /-- (Implementation) The object part of the compactification functor from topological spaces to compact Hausdorff spaces. -/ @[simps] def StoneCech_obj (X : Top) : CompHaus := CompHaus.of (stone_cech X) /-- (Implementation) The bijection of homsets to establish the reflective adjunction of compact Hausdorff spaces in topological spaces. -/ noncomputable def stone_cech_equivalence (X : Top.{u}) (Y : CompHaus.{u}) : (StoneCech_obj X ⟶ Y) ≃ (X ⟶ CompHaus_to_Top.obj Y) := { to_fun := λ f, { to_fun := f ∘ stone_cech_unit, continuous_to_fun := f.2.comp (@continuous_stone_cech_unit X _) }, inv_fun := λ f, { to_fun := stone_cech_extend f.2, continuous_to_fun := continuous_stone_cech_extend f.2 }, left_inv := begin rintro ⟨f : stone_cech X ⟶ Y, hf : continuous f⟩, ext (x : stone_cech X), refine congr_fun _ x, apply continuous.ext_on dense_range_stone_cech_unit (continuous_stone_cech_extend _) hf, rintro _ ⟨y, rfl⟩, apply congr_fun (stone_cech_extend_extends (hf.comp _)) y, end, right_inv := begin rintro ⟨f : (X : Type*) ⟶ Y, hf : continuous f⟩, ext, exact congr_fun (stone_cech_extend_extends hf) _, end } /-- The Stone-Cech compactification functor from topological spaces to compact Hausdorff spaces, left adjoint to the inclusion functor. -/ noncomputable def Top_to_CompHaus : Top.{u} ⥤ CompHaus.{u} := adjunction.left_adjoint_of_equiv stone_cech_equivalence.{u} (λ _ _ _ _ _, rfl) lemma Top_to_CompHaus_obj (X : Top) : ↥(Top_to_CompHaus.obj X) = stone_cech X := rfl /-- The category of compact Hausdorff spaces is reflective in the category of topological spaces. -/ noncomputable instance CompHaus_to_Top.reflective : reflective CompHaus_to_Top := { to_is_right_adjoint := ⟨Top_to_CompHaus, adjunction.adjunction_of_equiv_left _ _⟩ } noncomputable instance CompHaus_to_Top.creates_limits : creates_limits CompHaus_to_Top := monadic_creates_limits _ instance CompHaus.has_limits : limits.has_limits CompHaus := has_limits_of_has_limits_creates_limits CompHaus_to_Top instance CompHaus.has_colimits : limits.has_colimits CompHaus := has_colimits_of_reflective CompHaus_to_Top namespace CompHaus /-- An explicit limit cone for a functor `F : J ⥤ CompHaus`, defined in terms of `Top.limit_cone`. -/ def limit_cone {J : Type v} [small_category J] (F : J ⥤ CompHaus.{max v u}) : limits.cone F := { X := { to_Top := (Top.limit_cone (F ⋙ CompHaus_to_Top)).X, is_compact := begin show compact_space ↥{u : Π j, (F.obj j) | ∀ {i j : J} (f : i ⟶ j), (F.map f) (u i) = u j}, rw ← is_compact_iff_compact_space, apply is_closed.is_compact, have : {u : Π j, F.obj j | ∀ {i j : J} (f : i ⟶ j), F.map f (u i) = u j} = ⋂ (i j : J) (f : i ⟶ j), {u | F.map f (u i) = u j}, { ext1, simp only [set.mem_Inter, set.mem_set_of_eq], }, rw this, apply is_closed_Inter, intros i, apply is_closed_Inter, intros j, apply is_closed_Inter, intros f, apply is_closed_eq, { exact (continuous_map.continuous (F.map f)).comp (continuous_apply i), }, { exact continuous_apply j, } end, is_hausdorff := show t2_space ↥{u : Π j, (F.obj j) | ∀ {i j : J} (f : i ⟶ j), (F.map f) (u i) = u j}, from infer_instance }, π := { app := λ j, (Top.limit_cone (F ⋙ CompHaus_to_Top)).π.app j, naturality' := by { intros _ _ _, ext ⟨x, hx⟩, simp only [comp_apply, functor.const_obj_map, id_apply], exact (hx f).symm, } } } /-- The limit cone `CompHaus.limit_cone F` is indeed a limit cone. -/ def limit_cone_is_limit {J : Type v} [small_category J] (F : J ⥤ CompHaus.{max v u}) : limits.is_limit (limit_cone F) := { lift := λ S, (Top.limit_cone_is_limit (F ⋙ CompHaus_to_Top)).lift (CompHaus_to_Top.map_cone S), uniq' := λ S m h, (Top.limit_cone_is_limit _).uniq (CompHaus_to_Top.map_cone S) _ h } lemma epi_iff_surjective {X Y : CompHaus.{u}} (f : X ⟶ Y) : epi f ↔ function.surjective f := begin split, { contrapose!, rintros ⟨y, hy⟩ hf, let C := set.range f, have hC : is_closed C := (is_compact_range f.continuous).is_closed, let D := {y}, have hD : is_closed D := is_closed_singleton, have hCD : disjoint C D, { rw set.disjoint_singleton_right, rintro ⟨y', hy'⟩, exact hy y' hy' }, haveI : normal_space ↥(Y.to_Top) := normal_of_compact_t2, obtain ⟨φ, hφ0, hφ1, hφ01⟩ := exists_continuous_zero_one_of_closed hC hD hCD, haveI : compact_space (ulift.{u} $ set.Icc (0:ℝ) 1) := homeomorph.ulift.symm.compact_space, haveI : t2_space (ulift.{u} $ set.Icc (0:ℝ) 1) := homeomorph.ulift.symm.t2_space, let Z := of (ulift.{u} $ set.Icc (0:ℝ) 1), let g : Y ⟶ Z := ⟨λ y', ⟨⟨φ y', hφ01 y'⟩⟩, continuous_ulift_up.comp (φ.continuous.subtype_mk (λ y', hφ01 y'))⟩, let h : Y ⟶ Z := ⟨λ _, ⟨⟨0, set.left_mem_Icc.mpr zero_le_one⟩⟩, continuous_const⟩, have H : h = g, { rw ← cancel_epi f, ext x, dsimp, simp only [comp_apply, continuous_map.coe_mk, subtype.coe_mk, hφ0 (set.mem_range_self x), pi.zero_apply], }, apply_fun (λ e, (e y).down) at H, dsimp at H, simp only [subtype.mk_eq_mk, hφ1 (set.mem_singleton y), pi.one_apply] at H, exact zero_ne_one H, }, { rw ← category_theory.epi_iff_surjective, apply (forget CompHaus).epi_of_epi_map } end lemma mono_iff_injective {X Y : CompHaus.{u}} (f : X ⟶ Y) : mono f ↔ function.injective f := begin split, { introsI hf x₁ x₂ h, let g₁ : of punit ⟶ X := ⟨λ _, x₁, continuous_of_discrete_topology⟩, let g₂ : of punit ⟶ X := ⟨λ _, x₂, continuous_of_discrete_topology⟩, have : g₁ ≫ f = g₂ ≫ f, by { ext, exact h }, rw cancel_mono at this, apply_fun (λ e, e punit.star) at this, exact this }, { rw ← category_theory.mono_iff_injective, apply (forget CompHaus).mono_of_mono_map } end end CompHaus
e6537a2bf1ee7a05185c862e731e9d686a0b54b7
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/adjunction/basic.lean
af4a1777aed1142ad2af331db3a1a580b6c8d5d4
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
18,155
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 import data.equiv.basic 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} @[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_hom_equiv`. 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 := comp _ _ Fl.adj 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 := comp _ _ Gr.adj 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 _, }⟩ 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
00d4663dd0c15bb6f2cb45d42f0be4eef878d7da
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/normed_space/banach_steinhaus.lean
560c7f692aca9da41ba2c345e8d579f169ae8e7c
[ "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
6,362
lean
/- Copyright (c) 2021 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import analysis.normed_space.operator_norm import topology.metric_space.baire import topology.algebra.module.basic /-! # The Banach-Steinhaus theorem: Uniform Boundedness Principle Herein we prove the Banach-Steinhaus theorem: any collection of bounded linear maps from a Banach space into a normed space which is pointwise bounded is uniformly bounded. ## TODO For now, we only prove the standard version by appeal to the Baire category theorem. Much more general versions exist (in particular, for maps from barrelled spaces to locally convex spaces), but these are not yet in `mathlib`. -/ open set variables {E F 𝕜 𝕜₂ : Type*} [semi_normed_group E] [semi_normed_group F] [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [normed_space 𝕜 E] [normed_space 𝕜₂ F] {σ₁₂ : 𝕜 →+* 𝕜₂} [ring_hom_isometric σ₁₂] /-- This is the standard Banach-Steinhaus theorem, or Uniform Boundedness Principle. If a family of continuous linear maps from a Banach space into a normed space is pointwise bounded, then the norms of these linear maps are uniformly bounded. -/ theorem banach_steinhaus {ι : Type*} [complete_space E] {g : ι → E →SL[σ₁₂] F} (h : ∀ x, ∃ C, ∀ i, ∥g i x∥ ≤ C) : ∃ C', ∀ i, ∥g i∥ ≤ C' := begin /- sequence of subsets consisting of those `x : E` with norms `∥g i x∥` bounded by `n` -/ let e : ℕ → set E := λ n, (⋂ i : ι, { x : E | ∥g i x∥ ≤ n }), /- each of these sets is closed -/ have hc : ∀ n : ℕ, is_closed (e n), from λ i, is_closed_Inter (λ i, is_closed_le (continuous.norm (g i).cont) continuous_const), /- the union is the entire space; this is where we use `h` -/ have hU : (⋃ n : ℕ, e n) = univ, { refine eq_univ_of_forall (λ x, _), cases h x with C hC, obtain ⟨m, hm⟩ := exists_nat_ge C, exact ⟨e m, mem_range_self m, mem_Inter.mpr (λ i, le_trans (hC i) hm)⟩ }, /- apply the Baire category theorem to conclude that for some `m : ℕ`, `e m` contains some `x` -/ rcases nonempty_interior_of_Union_of_closed hc hU with ⟨m, x, hx⟩, rcases metric.is_open_iff.mp is_open_interior x hx with ⟨ε, ε_pos, hε⟩, obtain ⟨k, hk⟩ := normed_field.exists_one_lt_norm 𝕜, /- show all elements in the ball have norm bounded by `m` after applying any `g i` -/ have real_norm_le : ∀ z : E, z ∈ metric.ball x ε → ∀ i : ι, ∥g i z∥ ≤ m, { intros z hz i, replace hz := mem_Inter.mp (interior_Inter_subset _ (hε hz)) i, apply interior_subset hz }, have εk_pos : 0 < ε / ∥k∥ := div_pos ε_pos (zero_lt_one.trans hk), refine ⟨(m + m : ℕ) / (ε / ∥k∥), λ i, continuous_linear_map.op_norm_le_of_shell ε_pos _ hk _⟩, { exact div_nonneg (nat.cast_nonneg _) εk_pos.le }, intros y le_y y_lt, calc ∥g i y∥ = ∥g i (y + x) - g i x∥ : by rw [continuous_linear_map.map_add, add_sub_cancel] ... ≤ ∥g i (y + x)∥ + ∥g i x∥ : norm_sub_le _ _ ... ≤ m + m : add_le_add (real_norm_le (y + x) (by rwa [add_comm, add_mem_ball_iff_norm]) i) (real_norm_le x (metric.mem_ball_self ε_pos) i) ... = (m + m : ℕ) : (m.cast_add m).symm ... ≤ (m + m : ℕ) * (∥y∥ / (ε / ∥k∥)) : le_mul_of_one_le_right (nat.cast_nonneg _) ((one_le_div $ div_pos ε_pos (zero_lt_one.trans hk)).2 le_y) ... = (m + m : ℕ) / (ε / ∥k∥) * ∥y∥ : (mul_comm_div' _ _ _).symm, end open_locale ennreal open ennreal /-- This version of Banach-Steinhaus is stated in terms of suprema of `↑∥⬝∥₊ : ℝ≥0∞` for convenience. -/ theorem banach_steinhaus_supr_nnnorm {ι : Type*} [complete_space E] {g : ι → E →SL[σ₁₂] F} (h : ∀ x, (⨆ i, ↑∥g i x∥₊) < ∞) : (⨆ i, ↑∥g i∥₊) < ∞ := begin have h' : ∀ x : E, ∃ C : ℝ, ∀ i : ι, ∥g i x∥ ≤ C, { intro x, rcases lt_iff_exists_coe.mp (h x) with ⟨p, hp₁, _⟩, refine ⟨p, (λ i, _)⟩, exact_mod_cast calc (∥g i x∥₊ : ℝ≥0∞) ≤ ⨆ j, ∥g j x∥₊ : le_supr _ i ... = p : hp₁ }, cases banach_steinhaus h' with C' hC', refine (supr_le $ λ i, _).trans_lt (@coe_lt_top C'.to_nnreal), rw ←norm_to_nnreal, exact coe_mono (real.to_nnreal_le_to_nnreal $ hC' i), end open_locale topological_space open filter /-- Given a *sequence* of continuous linear maps which converges pointwise and for which the domain is complete, the Banach-Steinhaus theorem is used to guarantee that the limit map is a *continuous* linear map as well. -/ def continuous_linear_map_of_tendsto [complete_space E] [t2_space F] (g : ℕ → E →SL[σ₁₂] F) {f : E → F} (h : tendsto (λ n x, g n x) at_top (𝓝 f)) : E →SL[σ₁₂] F := { to_fun := f, map_add' := (linear_map_of_tendsto _ _ h).map_add', map_smul' := (linear_map_of_tendsto _ _ h).map_smul', cont := begin /- show that the maps are pointwise bounded and apply `banach_steinhaus`-/ have h_point_bdd : ∀ x : E, ∃ C : ℝ, ∀ n : ℕ, ∥g n x∥ ≤ C, { intro x, rcases cauchy_seq_bdd (tendsto_pi_nhds.mp h x).cauchy_seq with ⟨C, C_pos, hC⟩, refine ⟨C + ∥g 0 x∥, (λ n, _)⟩, simp_rw dist_eq_norm at hC, calc ∥g n x∥ ≤ ∥g 0 x∥ + ∥g n x - g 0 x∥ : norm_le_insert' _ _ ... ≤ C + ∥g 0 x∥ : by linarith [hC n 0] }, cases banach_steinhaus h_point_bdd with C' hC', /- show the uniform bound from `banach_steinhaus` is a norm bound of the limit map by allowing "an `ε` of room." -/ refine linear_map.continuous_of_bound (linear_map_of_tendsto _ _ h) C' (λ x, le_of_forall_pos_lt_add (λ ε ε_pos, _)), cases metric.tendsto_at_top.mp (tendsto_pi_nhds.mp h x) ε ε_pos with n hn, have lt_ε : ∥g n x - f x∥ < ε, by {rw ←dist_eq_norm, exact hn n (le_refl n)}, calc ∥f x∥ ≤ ∥g n x∥ + ∥g n x - f x∥ : norm_le_insert _ _ ... < ∥g n∥ * ∥x∥ + ε : by linarith [lt_ε, (g n).le_op_norm x] ... ≤ C' * ∥x∥ + ε : by nlinarith [hC' n, norm_nonneg x], end }
6ec0555b0106522390a9360a57050fa7912a9d24
9c2e8d73b5c5932ceb1333265f17febc6a2f0a39
/src/defs.lean
bd36bcb805b006f108ba7067e3671fbc94bb28c0
[ "MIT" ]
permissive
minchaowu/ModalTab
2150392108dfdcaffc620ff280a8b55fe13c187f
9bb0bf17faf0554d907ef7bdd639648742889178
refs/heads/master
1,626,266,863,244
1,592,056,874,000
1,592,056,874,000
153,314,364
12
1
null
null
null
null
UTF-8
Lean
false
false
12,591
lean
/- Copyright (c) 2018-2019 Minchao Wu. All rights reserved. Released under MIT license as described in the file LICENSE. Author: Minchao Wu -/ import data.list open nat tactic subtype inductive nnf : Type | var (n : nat) | neg (n : nat) | and (φ ψ : nnf) | or (φ ψ : nnf) | box (φ : nnf) | dia (φ : nnf) open nnf instance : inhabited nnf := ⟨nnf.var 0⟩ class no_literals (Γ : list nnf) := (no_var : ∀ {n}, var n ∉ Γ) (no_neg : ∀ {n}, neg n ∉ Γ) class saturated (Γ : list nnf) := (no_and : ∀ {φ ψ}, nnf.and φ ψ ∉ Γ) (no_or : ∀ {φ ψ}, nnf.or φ ψ ∉ Γ) class box_only (Γ : list nnf) extends no_literals Γ, saturated Γ := (no_dia : ∀ {φ}, nnf.dia φ ∉ Γ) def box_only_nil : box_only [] := {no_var := λ n hn, absurd hn $ list.not_mem_nil _, no_neg := λ n hn, absurd hn $ list.not_mem_nil _, no_and := λ φ ψ hn, absurd hn $ list.not_mem_nil _, no_or := λ φ ψ hn, absurd hn $ list.not_mem_nil _, no_dia := λ n hn, absurd hn $ list.not_mem_nil _} def cons_box_only {φ Γ} (h : box_only Γ) : box_only (box φ :: Γ) := {no_var := begin intros n hn, cases hn, contradiction, apply h.no_var, exact hn end, no_neg := begin intros n hn, cases hn, contradiction, apply h.no_neg, exact hn end, no_and := begin intros ψ₁ ψ₂ hψ, cases hψ, contradiction, apply h.no_and, exact hψ end, no_or := begin intros ψ₁ ψ₂ hψ, cases hψ, contradiction, apply h.no_or, exact hψ end, no_dia := begin intros n hn, cases hn, contradiction, apply h.no_dia, exact hn end} def box_only_ex : Π {φ Γ} (h₁ : box_only Γ) (h₂ : φ ∈ Γ), ∃ ψ, φ = box ψ | (var n) Γ h₁ h₂ := begin exfalso, apply h₁.no_var, exact h₂ end | (neg n) Γ h₁ h₂ := begin exfalso, apply h₁.no_neg, exact h₂ end | (and φ ψ) Γ h₁ h₂ := begin exfalso, apply h₁.no_and, exact h₂ end | (or φ ψ) Γ h₁ h₂ := begin exfalso, apply h₁.no_or, exact h₂ end | (box φ) Γ h₁ h₂ := ⟨φ, rfl⟩ | (dia φ) Γ h₁ h₂ := begin exfalso, apply h₁.no_dia, exact h₂ end def nnf.to_string : nnf → string | (var n) := "P" ++ n.repr | (neg n) := "¬P" ++ n.repr | (and φ ψ) := nnf.to_string φ ++ "∧" ++ nnf.to_string ψ | (or φ ψ) := nnf.to_string φ ++ "∨" ++ nnf.to_string ψ | (box φ) := "□" ++ "(" ++ nnf.to_string φ ++ ")" | (dia φ) := "◇" ++ "(" ++ nnf.to_string φ ++ ")" instance nnf_repr : has_repr nnf := ⟨nnf.to_string⟩ instance dec_eq_nnf : decidable_eq nnf := by mk_dec_eq_instance structure kripke (states : Type) := (val : ℕ → states → Prop) (rel : states → states → Prop) instance inhabited_kripke : inhabited (kripke ℕ) := ⟨{ val := λ a b, tt, rel := λ a b, tt }⟩ open nnf /- Do not use map -/ @[simp] def node_size : list nnf → ℕ | [] := 0 | (hd :: tl) := sizeof hd + node_size tl inductive close_instance (Γ : list nnf) | cons : Π {n}, var n ∈ Γ → neg n ∈ Γ → close_instance inductive and_instance (Γ : list nnf) : list nnf → Type | cons : Π {φ ψ}, nnf.and φ ψ ∈ Γ → and_instance (φ :: ψ :: Γ.erase (nnf.and φ ψ)) inductive or_instance (Γ : list nnf) : list nnf → list nnf → Type | cons : Π {φ ψ}, nnf.or φ ψ ∈ Γ → or_instance (φ :: Γ.erase (nnf.or φ ψ)) (ψ :: Γ.erase (nnf.or φ ψ)) def left_prcp : Π {Γ₁ Γ₂ Δ : list nnf} (i : or_instance Δ Γ₁ Γ₂), nnf | Γ₁ Γ₂ Δ (@or_instance.cons _ φ ψ _) := φ @[simp] def sub_nnf : nnf → list nnf | (var n) := [var n] | (neg n) := [neg n] | (and φ ψ) := and φ ψ :: (sub_nnf φ ++ sub_nnf ψ) | (or φ ψ) := or φ ψ :: (sub_nnf φ ++ sub_nnf ψ) | (box φ) := box φ :: sub_nnf φ | (dia φ) := dia φ :: sub_nnf φ theorem mem_sub_nnf_self : Π φ : nnf, φ ∈ sub_nnf φ := begin intro φ, cases φ, all_goals {simp} end theorem mem_sub_nnf_and {φ ψ : nnf} : Π γ : nnf, and φ ψ ∈ sub_nnf γ → φ ∈ sub_nnf γ ∧ ψ ∈ sub_nnf γ | (var n) h := begin simp at h, contradiction end | (neg n) h := begin simp at h, contradiction end | (and φ₁ ψ₁) h := begin simp only [sub_nnf] at h, cases h, {injection h with eq₁ eq₂, rw eq₁, rw eq₂, simp only [sub_nnf], split, {simp, right, left, apply mem_sub_nnf_self}, {simp, right, right, apply mem_sub_nnf_self}}, {have := (@list.mem_append _ _ _ _).1 h, cases this, {split, {simp, right, left, apply (mem_sub_nnf_and _ _).1, exact this}, {simp, right, left, apply (mem_sub_nnf_and _ _).2, exact this}}, {split, {simp, right, right, apply (mem_sub_nnf_and _ _).1, exact this}, {simp, right, right, apply (mem_sub_nnf_and _ _).2, exact this}}} end | (or φ₁ ψ₁) h := begin simp at h, cases h, {split, {simp, right, left, apply (mem_sub_nnf_and _ _).1, exact h}, {simp, right, left, apply (mem_sub_nnf_and _ _).2, exact h}}, {split, {simp, right, right, apply (mem_sub_nnf_and _ _).1, exact h}, {simp, right, right, apply (mem_sub_nnf_and _ _).2, exact h}} end | (box φ₁) h := begin simp at h, {split, {simp, right, apply (mem_sub_nnf_and _ _).1, exact h}, {simp, right, apply (mem_sub_nnf_and _ _).2, exact h}} end | (dia φ₁) h := begin simp at h, {split, {simp, right, apply (mem_sub_nnf_and _ _).1, exact h}, {simp, right, apply (mem_sub_nnf_and _ _).2, exact h}} end theorem mem_sub_nnf_or {φ ψ : nnf} : Π γ : nnf, or φ ψ ∈ sub_nnf γ → φ ∈ sub_nnf γ ∧ ψ ∈ sub_nnf γ | (var n) h := begin simp at h, contradiction end | (neg n) h := begin simp at h, contradiction end | (and φ₁ ψ₁) h := begin simp at h, cases h, {split, {simp, right, left, apply (mem_sub_nnf_or _ _).1, exact h}, {simp, right, left, apply (mem_sub_nnf_or _ _).2, exact h}}, {split, {simp, right, right, apply (mem_sub_nnf_or _ _).1, exact h}, {simp, right, right, apply (mem_sub_nnf_or _ _).2, exact h}} end | (or φ₁ ψ₁) h := begin simp only [sub_nnf] at h, cases h, {injection h with eq₁ eq₂, rw eq₁, rw eq₂, simp only [sub_nnf], split, {simp, right, left, apply mem_sub_nnf_self}, {simp, right, right, apply mem_sub_nnf_self}}, {have := (@list.mem_append _ _ _ _).1 h, cases this, {split, {simp, right, left, apply (mem_sub_nnf_or _ _).1, exact this}, {simp, right, left, apply (mem_sub_nnf_or _ _).2, exact this}}, {split, {simp, right, right, apply (mem_sub_nnf_or _ _).1, exact this}, {simp, right, right, apply (mem_sub_nnf_or _ _).2, exact this}}} end | (box φ₁) h := begin simp at h, {split, {simp, right, apply (mem_sub_nnf_or _ _).1, exact h}, {simp, right, apply (mem_sub_nnf_or _ _).2, exact h}} end | (dia φ₁) h := begin simp at h, {split, {simp, right, apply (mem_sub_nnf_or _ _).1, exact h}, {simp, right, apply (mem_sub_nnf_or _ _).2, exact h}} end theorem mem_sub_nnf_box {φ : nnf} : Π γ : nnf, box φ ∈ sub_nnf γ → φ ∈ sub_nnf γ | (var n) h := begin simp at h, contradiction end | (neg n) h := begin simp at h, contradiction end | (and φ₁ ψ₁) h := begin simp at h, cases h, {simp, right, left, apply mem_sub_nnf_box, exact h}, {simp, right, right, apply mem_sub_nnf_box, exact h} end | (or φ₁ ψ₁) h := begin simp at h, cases h, {simp, right, left, apply mem_sub_nnf_box, exact h}, {simp, right, right, apply mem_sub_nnf_box, exact h} end | (box φ₁) h := begin simp at h, cases h, {rw h, simp, right, apply mem_sub_nnf_self}, {simp, right, apply mem_sub_nnf_box, exact h} end | (dia φ₁) h := begin simp at h, simp, right, apply mem_sub_nnf_box, exact h end theorem mem_sub_nnf_dia {φ : nnf} : Π γ : nnf, dia φ ∈ sub_nnf γ → φ ∈ sub_nnf γ | (var n) h := begin simp at h, contradiction end | (neg n) h := begin simp at h, contradiction end | (and φ₁ ψ₁) h := begin simp at h, cases h, {simp, right, left, apply mem_sub_nnf_dia, exact h}, {simp, right, right, apply mem_sub_nnf_dia, exact h} end | (or φ₁ ψ₁) h := begin simp at h, cases h, {simp, right, left, apply mem_sub_nnf_dia, exact h}, {simp, right, right, apply mem_sub_nnf_dia, exact h} end | (box φ₁) h := begin simp at h, simp, right, apply mem_sub_nnf_dia, exact h end | (dia φ₁) h := begin simp at h, cases h, {rw h, simp, right, apply mem_sub_nnf_self}, {simp, right, apply mem_sub_nnf_dia, exact h} end @[simp] def nnf.closure : list nnf → list nnf | [] := [] | (hd::tl) := sub_nnf hd ++ nnf.closure tl theorem mem_closure_self : Π Γ : list nnf, Γ ⊆ nnf.closure Γ | [] := λ x h, absurd h $ list.not_mem_nil _ | (hd::tl) := begin intros x hx, cases hx, {rw hx, simp, left, apply mem_sub_nnf_self}, {simp, right, apply mem_closure_self, exact hx} end theorem mem_closure_and {φ ψ : nnf} : Π Γ : list nnf, and φ ψ ∈ nnf.closure Γ → φ ∈ nnf.closure Γ ∧ ψ ∈ nnf.closure Γ | [] h := absurd h $ list.not_mem_nil _ | (hd::tl) h := begin simp at h, cases h, {split, {simp, left, apply (mem_sub_nnf_and hd _).1, swap, exact h}, {simp, left, apply (mem_sub_nnf_and hd _).2, swap, exact h}}, {split, {simp, right, apply (mem_closure_and _ _).1, exact h}, {simp, right, apply (mem_closure_and _ _).2, exact h} } end theorem mem_closure_or {φ ψ : nnf} : Π Γ : list nnf, or φ ψ ∈ nnf.closure Γ → φ ∈ nnf.closure Γ ∧ ψ ∈ nnf.closure Γ | [] h := absurd h $ list.not_mem_nil _ | (hd::tl) h := begin simp at h, cases h, {split, {simp, left, apply (mem_sub_nnf_or hd _).1, swap, exact h}, {simp, left, apply (mem_sub_nnf_or hd _).2, swap, exact h}}, {split, {simp, right, apply (mem_closure_or _ _).1, exact h}, {simp, right, apply (mem_closure_or _ _).2, exact h} } end theorem mem_closure_box {φ : nnf} : Π Γ : list nnf, box φ ∈ nnf.closure Γ → φ ∈ nnf.closure Γ | [] h := absurd h $ list.not_mem_nil _ | (hd::tl) h := begin simp at h, cases h, {simp, left, apply mem_sub_nnf_box _ h}, {simp, right, apply mem_closure_box _ h} end theorem mem_closure_dia {φ : nnf} : Π Γ : list nnf, dia φ ∈ nnf.closure Γ → φ ∈ nnf.closure Γ | [] h := absurd h $ list.not_mem_nil _ | (hd::tl) h := begin simp at h, cases h, {simp, left, apply mem_sub_nnf_dia _ h}, {simp, right, apply mem_closure_dia _ h} end @[simp] def filter_undia (l : list nnf) : list nnf → list nnf | [] := [] | (dia φ::tl) := if φ ∈ l then filter_undia tl else φ :: filter_undia tl | (e::tl) := filter_undia tl theorem mem_filter_undia_left : Π l Γ φ (h₁ : dia φ ∈ Γ) (h₂ : φ ∉ l), φ ∈ filter_undia l Γ | l [] φ h₁ h₂ := absurd h₁ $ list.not_mem_nil _ | l (hd :: tl) φ h₁ h₂ := begin cases h : hd, case nnf.dia : ψ {rw h at h₁, simp, by_cases hc : ψ ∈ l, {rw if_pos, apply mem_filter_undia_left, cases h₁, {injection h₁ with h₁', rw ←h₁' at hc, contradiction}, {exact h₁}, repeat {assumption}}, {rw if_neg, cases h₁, {injection h₁ with h₁', rw h₁', simp}, {right, apply mem_filter_undia_left, exact h₁, exact h₂}, exact hc}}, all_goals {simp, rw h at h₁, cases h₁, contradiction, apply mem_filter_undia_left, repeat {assumption}} end theorem mem_filter_dia_right_aux : Π l Γ φ, φ ∈ filter_undia l Γ → dia φ ∈ Γ | l [] φ h := absurd h $ list.not_mem_nil _ | l (hd :: tl) φ h := begin cases h₁ : hd, case nnf.dia : ψ {rw h₁ at h, dsimp at h, by_cases hc : ψ ∈ l, {rw if_pos at h, have := mem_filter_dia_right_aux l tl φ h, right, exact this, exact hc}, {rw if_neg at h, cases h, {rw h, simp}, {have := mem_filter_dia_right_aux l tl φ h, right, exact this}, exact hc}}, all_goals {rw h₁ at h, simp at h, right, apply mem_filter_dia_right_aux, exact h} end theorem mem_filter_dia_right : Π l Γ φ (h₂ : φ ∈ filter_undia l Γ), φ ∉ l | l [] φ h := absurd h $ list.not_mem_nil _ | l (hd :: tl) φ h := begin cases h₁ : hd, case nnf.dia : ψ {rw h₁ at h, dsimp at h, by_cases hc : ψ ∈ l, {rw if_pos at h, have := mem_filter_dia_right l tl φ h, exact this, exact hc}, {rw if_neg at h, cases h, {rw h, exact hc}, {have := mem_filter_dia_right l tl φ h, exact this}, exact hc}}, all_goals {rw h₁ at h, simp at h, apply mem_filter_dia_right, exact h} end structure hintikka (Γ : list nnf) : Prop := (hno_contra : ∀ {n}, var n ∈ Γ → neg n ∉ Γ) (hand_left : ∀ {φ ψ}, nnf.and φ ψ ∈ Γ → φ ∈ Γ) (hand_right : ∀ {φ ψ}, nnf.and φ ψ ∈ Γ → ψ ∈ Γ) (hor : ∀ {φ ψ}, nnf.or φ ψ ∈ Γ → φ ∈ Γ ∨ ψ ∈ Γ) (hbox : ∀ {φ}, nnf.box φ ∈ Γ → φ ∈ Γ)
7cad408c4e8362fe4f253674200c794aca0be517
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/complex/abs_max.lean
d99498d5098f4f6ecae23138bd9555b5a3890682
[ "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
22,743
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.cauchy_integral import analysis.normed_space.completion import analysis.normed_space.extr import topology.algebra.order.extr_closure /-! # Maximum modulus principle In this file we prove several versions of the maximum modulus principle. There are several statements that can be called "the maximum modulus principle" for maps between normed complex spaces. They differ by assumptions on the domain (any space, a nontrivial space, a finite dimensional space), assumptions on the codomain (any space, a strictly convex space), and by conclusion (either equality of norms or of the values of the function). ## Main results ### Theorems for any codomain Consider a function `f : E → F` that is complex differentiable on a set `s`, is continuous on its closure, and `‖f x‖` has a maximum on `s` at `c`. We prove the following theorems. - `complex.norm_eq_on_closed_ball_of_is_max_on`: if `s = metric.ball c r`, then `‖f x‖ = ‖f c‖` for any `x` from the corresponding closed ball; - `complex.norm_eq_norm_of_is_max_on_of_ball_subset`: if `metric.ball c (dist w c) ⊆ s`, then `‖f w‖ = ‖f c‖`; - `complex.norm_eq_on_of_is_preconnected_of_is_max_on`: if `U` is an open (pre)connected set, `f` is complex differentiable on `U`, and `‖f x‖` has a maximum on `U` at `c ∈ U`, then `‖f x‖ = ‖f c‖` for all `x ∈ U`; - `complex.norm_eq_on_closure_of_is_preconnected_of_is_max_on`: if `s` is open and (pre)connected and `c ∈ s`, then `‖f x‖ = ‖f c‖` for all `x ∈ closure s`; - `complex.norm_eventually_eq_of_is_local_max`: if `f` is complex differentiable in a neighborhood of `c` and `‖f x‖` has a local maximum at `c`, then `‖f x‖` is locally a constant in a neighborhood of `c`. ### Theorems for a strictly convex codomain If the codomain `F` is a strictly convex space, then in the lemmas from the previous section we can prove `f w = f c` instead of `‖f w‖ = ‖f c‖`, see `complex.eq_on_of_is_preconnected_of_is_max_on_norm`, `complex.eq_on_closure_of_is_preconnected_of_is_max_on_norm`, `complex.eq_of_is_max_on_of_ball_subset`, `complex.eq_on_closed_ball_of_is_max_on_norm`, and `complex.eventually_eq_of_is_local_max_norm`. ### Values on the frontier Finally, we prove some corollaries that relate the (norm of the) values of a function on a set to its values on the frontier of the set. All these lemmas assume that `E` is a nontrivial space. In this section `f g : E → F` are functions that are complex differentiable on a bounded set `s` and are continuous on its closure. We prove the following theorems. - `complex.exists_mem_frontier_is_max_on_norm`: If `E` is a finite dimensional space and `s` is a nonempty bounded set, then there exists a point `z ∈ frontier s` such that `λ z, ‖f z‖` takes it maximum value on `closure s` at `z`. - `complex.norm_le_of_forall_mem_frontier_norm_le`: if `‖f z‖ ≤ C` for all `z ∈ frontier s`, then `‖f z‖ ≤ C` for all `z ∈ s`; note that this theorem does not require `E` to be a finite dimensional space. - `complex.eq_on_closure_of_eq_on_frontier`: if `f x = g x` on the frontier of `s`, then `f x = g x` on `closure s`; - `complex.eq_on_of_eq_on_frontier`: if `f x = g x` on the frontier of `s`, then `f x = g x` on `s`. ## Tags maximum modulus principle, complex analysis -/ open topological_space metric set filter asymptotics function measure_theory affine_map open_locale topology filter nnreal real universes u v w variables {E : Type u} [normed_add_comm_group E] [normed_space ℂ E] {F : Type v} [normed_add_comm_group F] [normed_space ℂ F] local postfix `̂`:100 := uniform_space.completion namespace complex /-! ### Auxiliary lemmas We split the proof into a series of lemmas. First we prove the principle for a function `f : ℂ → F` with an additional assumption that `F` is a complete space, then drop unneeded assumptions one by one. The lemmas with names `*_auxₙ` are considered to be private and should not be used outside of this file. -/ lemma norm_max_aux₁ [complete_space F] {f : ℂ → F} {z w : ℂ} (hd : diff_cont_on_cl ℂ f (ball z (dist w z))) (hz : is_max_on (norm ∘ f) (closed_ball z (dist w z)) z) : ‖f w‖ = ‖f z‖ := begin /- Consider a circle of radius `r = dist w z`. -/ set r : ℝ := dist w z, have hw : w ∈ closed_ball z r, from mem_closed_ball.2 le_rfl, /- Assume the converse. Since `‖f w‖ ≤ ‖f z‖`, we have `‖f w‖ < ‖f z‖`. -/ refine (is_max_on_iff.1 hz _ hw).antisymm (not_lt.1 _), rintro hw_lt : ‖f w‖ < ‖f z‖, have hr : 0 < r, from dist_pos.2 (ne_of_apply_ne (norm ∘ f) hw_lt.ne), /- Due to Cauchy integral formula, it suffices to prove the following inequality. -/ suffices : ‖∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ‖ < 2 * π * ‖f z‖, { refine this.ne _, have A : ∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ = (2 * π * I : ℂ) • f z := hd.circle_integral_sub_inv_smul (mem_ball_self hr), simp [A, norm_smul, real.pi_pos.le] }, suffices : ‖∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ‖ < 2 * π * r * (‖f z‖ / r), by rwa [mul_assoc, mul_div_cancel' _ hr.ne'] at this, /- This inequality is true because `‖(ζ - z)⁻¹ • f ζ‖ ≤ ‖f z‖ / r` for all `ζ` on the circle and this inequality is strict at `ζ = w`. -/ have hsub : sphere z r ⊆ closed_ball z r, from sphere_subset_closed_ball, refine circle_integral.norm_integral_lt_of_norm_le_const_of_lt hr _ _ ⟨w, rfl, _⟩, show continuous_on (λ (ζ : ℂ), (ζ - z)⁻¹ • f ζ) (sphere z r), { refine ((continuous_on_id.sub continuous_on_const).inv₀ _).smul (hd.continuous_on_ball.mono hsub), exact λ ζ hζ, sub_ne_zero.2 (ne_of_mem_sphere hζ hr.ne') }, show ∀ ζ ∈ sphere z r, ‖(ζ - z)⁻¹ • f ζ‖ ≤ ‖f z‖ / r, { rintros ζ (hζ : abs (ζ - z) = r), rw [le_div_iff hr, norm_smul, norm_inv, norm_eq_abs, hζ, mul_comm, mul_inv_cancel_left₀ hr.ne'], exact hz (hsub hζ) }, show ‖(w - z)⁻¹ • f w‖ < ‖f z‖ / r, { rw [norm_smul, norm_inv, norm_eq_abs, ← div_eq_inv_mul], exact (div_lt_div_right hr).2 hw_lt } end /-! Now we drop the assumption `complete_space F` by embedding `F` into its completion. -/ lemma norm_max_aux₂ {f : ℂ → F} {z w : ℂ} (hd : diff_cont_on_cl ℂ f (ball z (dist w z))) (hz : is_max_on (norm ∘ f) (closed_ball z (dist w z)) z) : ‖f w‖ = ‖f z‖ := begin set e : F →L[ℂ] F̂ := uniform_space.completion.to_complL, have he : ∀ x, ‖e x‖ = ‖x‖, from uniform_space.completion.norm_coe, replace hz : is_max_on (norm ∘ (e ∘ f)) (closed_ball z (dist w z)) z, by simpa only [is_max_on, (∘), he] using hz, simpa only [he] using norm_max_aux₁ (e.differentiable.comp_diff_cont_on_cl hd) hz end /-! Then we replace the assumption `is_max_on (norm ∘ f) (closed_ball z r) z` with a seemingly weaker assumption `is_max_on (norm ∘ f) (ball z r) z`. -/ lemma norm_max_aux₃ {f : ℂ → F} {z w : ℂ} {r : ℝ} (hr : dist w z = r) (hd : diff_cont_on_cl ℂ f (ball z r)) (hz : is_max_on (norm ∘ f) (ball z r) z) : ‖f w‖ = ‖f z‖ := begin subst r, rcases eq_or_ne w z with rfl|hne, { refl }, rw ← dist_ne_zero at hne, exact norm_max_aux₂ hd (closure_ball z hne ▸ hz.closure hd.continuous_on.norm) end /-! ### Maximum modulus principle for any codomain If we do not assume that the codomain is a strictly convex space, then we can only claim that the **norm** `‖f x‖` is locally constant. -/ /-! Finally, we generalize the theorem from a disk in `ℂ` to a closed ball in any normed space. -/ /-- **Maximum modulus principle** on a closed ball: if `f : E → F` is continuous on a closed ball, is complex differentiable on the corresponding open ball, and the norm `‖f w‖` takes its maximum value on the open ball at its center, then the norm `‖f w‖` is constant on the closed ball. -/ lemma norm_eq_on_closed_ball_of_is_max_on {f : E → F} {z : E} {r : ℝ} (hd : diff_cont_on_cl ℂ f (ball z r)) (hz : is_max_on (norm ∘ f) (ball z r) z) : eq_on (norm ∘ f) (const E ‖f z‖) (closed_ball z r) := begin intros w hw, rw [mem_closed_ball, dist_comm] at hw, rcases eq_or_ne z w with rfl|hne, { refl }, set e : ℂ → E := line_map z w, have hde : differentiable ℂ e := (differentiable_id.smul_const (w - z)).add_const z, suffices : ‖(f ∘ e) (1 : ℂ)‖ = ‖(f ∘ e) (0 : ℂ)‖, by simpa [e], have hr : dist (1 : ℂ) 0 = 1, by simp, have hball : maps_to e (ball 0 1) (ball z r), { refine ((lipschitz_with_line_map z w).maps_to_ball (mt nndist_eq_zero.1 hne) 0 1).mono subset.rfl _, simpa only [line_map_apply_zero, mul_one, coe_nndist] using ball_subset_ball hw }, exact norm_max_aux₃ hr (hd.comp hde.diff_cont_on_cl hball) (hz.comp_maps_to hball (line_map_apply_zero z w)) end /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a set `s`, the norm of `f` takes it maximum on `s` at `z`, and `w` is a point such that the closed ball with center `z` and radius `dist w z` is included in `s`, then `‖f w‖ = ‖f z‖`. -/ lemma norm_eq_norm_of_is_max_on_of_ball_subset {f : E → F} {s : set E} {z w : E} (hd : diff_cont_on_cl ℂ f s) (hz : is_max_on (norm ∘ f) s z) (hsub : ball z (dist w z) ⊆ s) : ‖f w‖ = ‖f z‖ := norm_eq_on_closed_ball_of_is_max_on (hd.mono hsub) (hz.on_subset hsub) (mem_closed_ball.2 le_rfl) /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable in a neighborhood of `c` and the norm `‖f z‖` has a local maximum at `c`, then `‖f z‖` is locally constant in a neighborhood of `c`. -/ lemma norm_eventually_eq_of_is_local_max {f : E → F} {c : E} (hd : ∀ᶠ z in 𝓝 c, differentiable_at ℂ f z) (hc : is_local_max (norm ∘ f) c) : ∀ᶠ y in 𝓝 c, ‖f y‖ = ‖f c‖ := begin rcases nhds_basis_closed_ball.eventually_iff.1 (hd.and hc) with ⟨r, hr₀, hr⟩, exact nhds_basis_closed_ball.eventually_iff.2 ⟨r, hr₀, norm_eq_on_closed_ball_of_is_max_on (differentiable_on.diff_cont_on_cl $ λ x hx, (hr $ closure_ball_subset_closed_ball hx).1.differentiable_within_at) (λ x hx, (hr $ ball_subset_closed_ball hx).2)⟩ end lemma is_open_set_of_mem_nhds_and_is_max_on_norm {f : E → F} {s : set E} (hd : differentiable_on ℂ f s) : is_open {z | s ∈ 𝓝 z ∧ is_max_on (norm ∘ f) s z} := begin refine is_open_iff_mem_nhds.2 (λ z hz, (eventually_eventually_nhds.2 hz.1).and _), replace hd : ∀ᶠ w in 𝓝 z, differentiable_at ℂ f w, from hd.eventually_differentiable_at hz.1, exact (norm_eventually_eq_of_is_local_max hd $ (hz.2.is_local_max hz.1)).mono (λ x hx y hy, le_trans (hz.2 hy) hx.ge) end /-- **Maximum modulus principle** on a connected set. Let `U` be a (pre)connected open set in a complex normed space. Let `f : E → F` be a function that is complex differentiable on `U`. Suppose that `‖f x‖` takes its maximum value on `U` at `c ∈ U`. Then `‖f x‖ = ‖f c‖` for all `x ∈ U`. -/ lemma norm_eq_on_of_is_preconnected_of_is_max_on {f : E → F} {U : set E} {c : E} (hc : is_preconnected U) (ho : is_open U) (hd : differentiable_on ℂ f U) (hcU : c ∈ U) (hm : is_max_on (norm ∘ f) U c) : eq_on (norm ∘ f) (const E ‖f c‖) U := begin set V := U ∩ {z | is_max_on (norm ∘ f) U z}, have hV : ∀ x ∈ V, ‖f x‖ = ‖f c‖, from λ x hx, le_antisymm (hm hx.1) (hx.2 hcU), suffices : U ⊆ V, from λ x hx, hV x (this hx), have hVo : is_open V, { simpa only [ho.mem_nhds_iff, set_of_and, set_of_mem_eq] using is_open_set_of_mem_nhds_and_is_max_on_norm hd }, have hVne : (U ∩ V).nonempty := ⟨c, hcU, hcU, hm⟩, set W := U ∩ {z | ‖f z‖ ≠ ‖f c‖}, have hWo : is_open W, from hd.continuous_on.norm.preimage_open_of_open ho is_open_ne, have hdVW : disjoint V W, from disjoint_left.mpr (λ x hxV hxW, hxW.2 (hV x hxV)), have hUVW : U ⊆ V ∪ W, from λ x hx, (eq_or_ne (‖f x‖) (‖f c‖)).imp (λ h, ⟨hx, λ y hy, (hm hy).out.trans_eq h.symm⟩) (and.intro hx), exact hc.subset_left_of_subset_union hVo hWo hdVW hUVW hVne, end /-- **Maximum modulus principle** on a connected set. Let `U` be a (pre)connected open set in a complex normed space. Let `f : E → F` be a function that is complex differentiable on `U` and is continuous on its closure. Suppose that `‖f x‖` takes its maximum value on `U` at `c ∈ U`. Then `‖f x‖ = ‖f c‖` for all `x ∈ closure U`. -/ lemma norm_eq_on_closure_of_is_preconnected_of_is_max_on {f : E → F} {U : set E} {c : E} (hc : is_preconnected U) (ho : is_open U) (hd : diff_cont_on_cl ℂ f U) (hcU : c ∈ U) (hm : is_max_on (norm ∘ f) U c) : eq_on (norm ∘ f) (const E ‖f c‖) (closure U) := (norm_eq_on_of_is_preconnected_of_is_max_on hc ho hd.differentiable_on hcU hm).of_subset_closure hd.continuous_on.norm continuous_on_const subset_closure subset.rfl section strict_convex /-! ### The case of a strictly convex codomain If the codomain `F` is a strictly convex space, then we can claim equalities like `f w = f z` instead of `‖f w‖ = ‖f z‖`. Instead of repeating the proof starting with lemmas about integrals, we apply a corresponding lemma above twice: for `f` and for `λ x, f x + f c`. Then we have `‖f w‖ = ‖f z‖` and `‖f w + f z‖ = ‖f z + f z‖`, thus `‖f w + f z‖ = ‖f w‖ + ‖f z‖`. This is only possible if `f w = f z`, see `eq_of_norm_eq_of_norm_add_eq`. -/ variables [strict_convex_space ℝ F] /-- **Maximum modulus principle** on a connected set. Let `U` be a (pre)connected open set in a complex normed space. Let `f : E → F` be a function that is complex differentiable on `U`. Suppose that `‖f x‖` takes its maximum value on `U` at `c ∈ U`. Then `f x = f c` for all `x ∈ U`. TODO: change assumption from `is_max_on` to `is_local_max`. -/ lemma eq_on_of_is_preconnected_of_is_max_on_norm {f : E → F} {U : set E} {c : E} (hc : is_preconnected U) (ho : is_open U) (hd : differentiable_on ℂ f U) (hcU : c ∈ U) (hm : is_max_on (norm ∘ f) U c) : eq_on f (const E (f c)) U := λ x hx, have H₁ : ‖f x‖ = ‖f c‖, from norm_eq_on_of_is_preconnected_of_is_max_on hc ho hd hcU hm hx, have H₂ : ‖f x + f c‖ = ‖f c + f c‖, from norm_eq_on_of_is_preconnected_of_is_max_on hc ho (hd.add_const _) hcU hm.norm_add_self hx, eq_of_norm_eq_of_norm_add_eq H₁ $ by simp only [H₂, same_ray.rfl.norm_add, H₁] /-- **Maximum modulus principle** on a connected set. Let `U` be a (pre)connected open set in a complex normed space. Let `f : E → F` be a function that is complex differentiable on `U` and is continuous on its closure. Suppose that `‖f x‖` takes its maximum value on `U` at `c ∈ U`. Then `f x = f c` for all `x ∈ closure U`. -/ lemma eq_on_closure_of_is_preconnected_of_is_max_on_norm {f : E → F} {U : set E} {c : E} (hc : is_preconnected U) (ho : is_open U) (hd : diff_cont_on_cl ℂ f U) (hcU : c ∈ U) (hm : is_max_on (norm ∘ f) U c) : eq_on f (const E (f c)) (closure U) := (eq_on_of_is_preconnected_of_is_max_on_norm hc ho hd.differentiable_on hcU hm).of_subset_closure hd.continuous_on continuous_on_const subset_closure subset.rfl /-- **Maximum modulus principle**. Let `f : E → F` be a function between complex normed spaces. Suppose that the codomain `F` is a strictly convex space, `f` is complex differentiable on a set `s`, `f` is continuous on the closure of `s`, the norm of `f` takes it maximum on `s` at `z`, and `w` is a point such that the closed ball with center `z` and radius `dist w z` is included in `s`, then `f w = f z`. -/ lemma eq_of_is_max_on_of_ball_subset {f : E → F} {s : set E} {z w : E} (hd : diff_cont_on_cl ℂ f s) (hz : is_max_on (norm ∘ f) s z) (hsub : ball z (dist w z) ⊆ s) : f w = f z := have H₁ : ‖f w‖ = ‖f z‖, from norm_eq_norm_of_is_max_on_of_ball_subset hd hz hsub, have H₂ : ‖f w + f z‖ = ‖f z + f z‖, from norm_eq_norm_of_is_max_on_of_ball_subset (hd.add_const _) hz.norm_add_self hsub, eq_of_norm_eq_of_norm_add_eq H₁ $ by simp only [H₂, same_ray.rfl.norm_add, H₁] /-- **Maximum modulus principle** on a closed ball. Suppose that a function `f : E → F` from a normed complex space to a strictly convex normed complex space has the following properties: - it is continuous on a closed ball `metric.closed_ball z r`, - it is complex differentiable on the corresponding open ball; - the norm `‖f w‖` takes its maximum value on the open ball at its center. Then `f` is a constant on the closed ball. -/ lemma eq_on_closed_ball_of_is_max_on_norm {f : E → F} {z : E} {r : ℝ} (hd : diff_cont_on_cl ℂ f (ball z r)) (hz : is_max_on (norm ∘ f) (ball z r) z) : eq_on f (const E (f z)) (closed_ball z r) := λ x hx, eq_of_is_max_on_of_ball_subset hd hz $ ball_subset_ball hx /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable in a neighborhood of `c` and the norm `‖f z‖` has a local maximum at `c`, then `f` is locally constant in a neighborhood of `c`. -/ lemma eventually_eq_of_is_local_max_norm {f : E → F} {c : E} (hd : ∀ᶠ z in 𝓝 c, differentiable_at ℂ f z) (hc : is_local_max (norm ∘ f) c) : ∀ᶠ y in 𝓝 c, f y = f c := begin rcases nhds_basis_closed_ball.eventually_iff.1 (hd.and hc) with ⟨r, hr₀, hr⟩, exact nhds_basis_closed_ball.eventually_iff.2 ⟨r, hr₀, eq_on_closed_ball_of_is_max_on_norm (differentiable_on.diff_cont_on_cl $ λ x hx, (hr $ closure_ball_subset_closed_ball hx).1.differentiable_within_at) (λ x hx, (hr $ ball_subset_closed_ball hx).2)⟩ end lemma eventually_eq_or_eq_zero_of_is_local_min_norm {f : E → ℂ} {c : E} (hf : ∀ᶠ z in 𝓝 c, differentiable_at ℂ f z) (hc : is_local_min (norm ∘ f) c) : (∀ᶠ z in 𝓝 c, f z = f c) ∨ (f c = 0) := begin refine or_iff_not_imp_right.mpr (λ h, _), have h1 : ∀ᶠ z in 𝓝 c, f z ≠ 0 := hf.self_of_nhds.continuous_at.eventually_ne h, have h2 : is_local_max (norm ∘ f)⁻¹ c := hc.inv (h1.mono (λ z, norm_pos_iff.mpr)), have h3 : is_local_max (norm ∘ f⁻¹) c := by { refine h2.congr (eventually_of_forall _); simp }, have h4 : ∀ᶠ z in 𝓝 c, differentiable_at ℂ f⁻¹ z, by filter_upwards [hf, h1] with z h using h.inv, filter_upwards [eventually_eq_of_is_local_max_norm h4 h3] with z using inv_inj.mp end end strict_convex /-! ### Maximum on a set vs maximum on its frontier In this section we prove corollaries of the maximum modulus principle that relate the values of a function on a set to its values on the frontier of this set. -/ variable [nontrivial E] /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a nonempty bounded set `U` and is continuous on its closure, then there exists a point `z ∈ frontier U` such that `λ z, ‖f z‖` takes it maximum value on `closure U` at `z`. -/ lemma exists_mem_frontier_is_max_on_norm [finite_dimensional ℂ E] {f : E → F} {U : set E} (hb : bounded U) (hne : U.nonempty) (hd : diff_cont_on_cl ℂ f U) : ∃ z ∈ frontier U, is_max_on (norm ∘ f) (closure U) z := begin have hc : is_compact (closure U), from hb.is_compact_closure, obtain ⟨w, hwU, hle⟩ : ∃ w ∈ closure U, is_max_on (norm ∘ f) (closure U) w, from hc.exists_forall_ge hne.closure hd.continuous_on.norm, rw [closure_eq_interior_union_frontier, mem_union] at hwU, cases hwU, rotate, { exact ⟨w, hwU, hle⟩ }, have : interior U ≠ univ, from ne_top_of_le_ne_top hc.ne_univ interior_subset_closure, rcases exists_mem_frontier_inf_dist_compl_eq_dist hwU this with ⟨z, hzU, hzw⟩, refine ⟨z, frontier_interior_subset hzU, λ x hx, (mem_set_of_eq.mp $ hle hx).trans_eq _⟩, refine (norm_eq_norm_of_is_max_on_of_ball_subset hd (hle.on_subset subset_closure) _).symm, rw [dist_comm, ← hzw], exact ball_inf_dist_compl_subset.trans interior_subset end /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a bounded set `U` and `‖f z‖ ≤ C` for any `z ∈ frontier U`, then the same is true for any `z ∈ closure U`. -/ lemma norm_le_of_forall_mem_frontier_norm_le {f : E → F} {U : set E} (hU : bounded U) (hd : diff_cont_on_cl ℂ f U) {C : ℝ} (hC : ∀ z ∈ frontier U, ‖f z‖ ≤ C) {z : E} (hz : z ∈ closure U) : ‖f z‖ ≤ C := begin rw [closure_eq_self_union_frontier, union_comm, mem_union] at hz, cases hz, { exact hC z hz }, /- In case of a finite dimensional domain, one can just apply `complex.exists_mem_frontier_is_max_on_norm`. To make it work in any Banach space, we restrict the function to a line first. -/ rcases exists_ne z with ⟨w, hne⟩, set e : ℂ → E := line_map z w, have hde : differentiable ℂ e := (differentiable_id.smul_const (w - z)).add_const z, have hL : antilipschitz_with (nndist z w)⁻¹ e, from antilipschitz_with_line_map hne.symm, replace hd : diff_cont_on_cl ℂ (f ∘ e) (e ⁻¹' U), from hd.comp hde.diff_cont_on_cl (maps_to_preimage _ _), have h₀ : (0 : ℂ) ∈ e ⁻¹' U, by simpa only [e, mem_preimage, line_map_apply_zero], rcases exists_mem_frontier_is_max_on_norm (hL.bounded_preimage hU) ⟨0, h₀⟩ hd with ⟨ζ, hζU, hζ⟩, calc ‖f z‖ = ‖f (e 0)‖ : by simp only [e, line_map_apply_zero] ... ≤ ‖f (e ζ)‖ : hζ (subset_closure h₀) ... ≤ C : hC _ (hde.continuous.frontier_preimage_subset _ hζU) end /-- If two complex differentiable functions `f g : E → F` are equal on the boundary of a bounded set `U`, then they are equal on `closure U`. -/ lemma eq_on_closure_of_eq_on_frontier {f g : E → F} {U : set E} (hU : bounded U) (hf : diff_cont_on_cl ℂ f U) (hg : diff_cont_on_cl ℂ g U) (hfg : eq_on f g (frontier U)) : eq_on f g (closure U) := begin suffices H : ∀ z ∈ closure U, ‖(f - g) z‖ ≤ 0, by simpa [sub_eq_zero] using H, refine λ z hz, norm_le_of_forall_mem_frontier_norm_le hU (hf.sub hg) (λ w hw, _) hz, simp [hfg hw] end /-- If two complex differentiable functions `f g : E → F` are equal on the boundary of a bounded set `U`, then they are equal on `U`. -/ lemma eq_on_of_eq_on_frontier {f g : E → F} {U : set E} (hU : bounded U) (hf : diff_cont_on_cl ℂ f U) (hg : diff_cont_on_cl ℂ g U) (hfg : eq_on f g (frontier U)) : eq_on f g U := (eq_on_closure_of_eq_on_frontier hU hf hg hfg).mono subset_closure end complex
64afe314a0e2db5cd93ac899212ebfed43704e95
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Init/Data/List/Basic.lean
1e0c40708fa3d0c3bcf3c3eae13b3b992eb75f21
[ "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
14,811
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.SimpLemmas import Init.Data.Nat.Basic open Decidable List universe u v w variable {α : Type u} {β : Type v} {γ : Type w} namespace List theorem length_add_eq_lengthTRAux (as : List α) (n : Nat) : as.length + n = as.lengthTRAux n := by induction as generalizing n with | nil => simp [length, lengthTRAux] | cons a as ih => simp [length, lengthTRAux, ← ih, Nat.succ_add] rfl @[csimp] theorem length_eq_lengthTR : @List.length = @List.lengthTR := by apply funext; intro α; apply funext; intro as simp [lengthTR, ← length_add_eq_lengthTRAux] @[simp] theorem length_nil : length ([] : List α) = 0 := rfl def reverseAux : List α → List α → List α | [], r => r | a::l, r => reverseAux l (a::r) def reverse (as : List α) :List α := reverseAux as [] theorem reverseAux_reverseAux_nil (as bs : List α) : reverseAux (reverseAux as bs) [] = reverseAux bs as := by induction as generalizing bs with | nil => rfl | cons a as ih => simp [reverseAux, ih] theorem reverseAux_reverseAux (as bs cs : List α) : reverseAux (reverseAux as bs) cs = reverseAux bs (reverseAux (reverseAux as []) cs) := by induction as generalizing bs cs with | nil => rfl | cons a as ih => simp [reverseAux, ih (a::bs), ih [a]] @[simp] theorem reverse_reverse (as : List α) : as.reverse.reverse = as := by simp [reverse]; rw [reverseAux_reverseAux_nil]; rfl protected def append : List α → List α → List α | [], bs => bs | a::as, bs => a :: List.append as bs def appendTR (as bs : List α) : List α := reverseAux as.reverse bs @[csimp] theorem append_eq_appendTR : @List.append = @appendTR := by apply funext; intro α; apply funext; intro as; apply funext; intro bs simp [appendTR, reverse] induction as with | nil => rfl | cons a as ih => simp [reverseAux, List.append, ih, reverseAux_reverseAux] instance : Append (List α) := ⟨List.append⟩ @[simp] theorem nil_append (as : List α) : [] ++ as = as := rfl @[simp] theorem append_nil (as : List α) : as ++ [] = as := by induction as with | nil => rfl | cons a as ih => simp_all [HAppend.hAppend, Append.append, List.append] @[simp] theorem cons_append (a : α) (as bs : List α) : (a::as) ++ bs = a::(as ++ bs) := rfl theorem append_assoc (as bs cs : List α) : (as ++ bs) ++ cs = as ++ (bs ++ cs) := by induction as with | nil => rfl | cons a as ih => simp [ih] instance : EmptyCollection (List α) := ⟨List.nil⟩ protected def erase {α} [BEq α] : List α → α → List α | [], b => [] | a::as, b => match a == b with | true => as | false => a :: List.erase as b def eraseIdx : List α → Nat → List α | [], _ => [] | a::as, 0 => as | a::as, n+1 => a :: eraseIdx as n def isEmpty : List α → Bool | [] => true | _ :: _ => false @[specialize] def map (f : α → β) : List α → List β | [] => [] | a::as => f a :: map f as @[specialize] def mapTRAux (f : α → β) : List α → List β → List β | [], bs => bs.reverse | a::as, bs => mapTRAux f as (f a :: bs) @[inline] def mapTR (f : α → β) (as : List α) : List β := mapTRAux f as [] theorem reverseAux_eq_append (as bs : List α) : reverseAux as bs = reverseAux as [] ++ bs := by induction as generalizing bs with | nil => simp [reverseAux] | cons a as ih => simp [reverseAux] rw [ih (a :: bs), ih [a], append_assoc] rfl @[simp] theorem reverse_nil : reverse ([] : List α) = [] := rfl @[simp] theorem reverse_cons (a : α) (as : List α) : reverse (a :: as) = reverse as ++ [a] := by simp [reverse, reverseAux] rw [← reverseAux_eq_append] @[simp] theorem reverse_append (as bs : List α) : (as ++ bs).reverse = bs.reverse ++ as.reverse := by induction as generalizing bs with | nil => simp | cons a as ih => simp [ih]; rw [append_assoc] theorem mapTRAux_eq (f : α → β) (as : List α) (bs : List β) : mapTRAux f as bs = bs.reverse ++ map f as := by induction as generalizing bs with | nil => simp [mapTRAux, map] | cons a as ih => simp [mapTRAux, map] rw [ih (f a :: bs), reverse_cons, append_assoc] rfl @[csimp] theorem map_eq_mapTR : @map = @mapTR := by apply funext; intro α; apply funext; intro β; apply funext; intro f; apply funext; intro as simp [mapTR, mapTRAux_eq] @[specialize] def map₂ (f : α → β → γ) : List α → List β → List γ | [], _ => [] | _, [] => [] | a::as, b::bs => f a b :: map₂ f as bs def join : List (List α) → List α | [] => [] | a :: as => a ++ join as @[specialize] def filterMap (f : α → Option β) : List α → List β | [] => [] | a::as => match f a with | none => filterMap f as | some b => b :: filterMap f as @[specialize] def filterAux (p : α → Bool) : List α → List α → List α | [], rs => rs.reverse | a::as, rs => match p a with | true => filterAux p as (a::rs) | false => filterAux p as rs @[inline] def filter (p : α → Bool) (as : List α) : List α := filterAux p as [] @[specialize] def partitionAux (p : α → Bool) : List α → List α × List α → List α × List α | [], (bs, cs) => (bs.reverse, cs.reverse) | a::as, (bs, cs) => match p a with | true => partitionAux p as (a::bs, cs) | false => partitionAux p as (bs, a::cs) @[inline] def partition (p : α → Bool) (as : List α) : List α × List α := partitionAux p as ([], []) def dropWhile (p : α → Bool) : List α → List α | [] => [] | a::l => match p a with | true => dropWhile p l | false => a::l def find? (p : α → Bool) : List α → Option α | [] => none | a::as => match p a with | true => some a | false => find? p as def findSome? (f : α → Option β) : List α → Option β | [] => none | a::as => match f a with | some b => some b | none => findSome? f as def replace [BEq α] : List α → α → α → List α | [], _, _ => [] | a::as, b, c => match a == b with | true => c::as | false => a :: (replace as b c) def elem [BEq α] (a : α) : List α → Bool | [] => false | b::bs => match a == b with | true => true | false => elem a bs def notElem [BEq α] (a : α) (as : List α) : Bool := !(as.elem a) abbrev contains [BEq α] (as : List α) (a : α) : Bool := elem a as def eraseDupsAux {α} [BEq α] : List α → List α → List α | [], bs => bs.reverse | a::as, bs => match bs.elem a with | true => eraseDupsAux as bs | false => eraseDupsAux as (a::bs) def eraseDups {α} [BEq α] (as : List α) : List α := eraseDupsAux as [] def eraseRepsAux {α} [BEq α] : α → List α → List α → List α | a, [], rs => (a::rs).reverse | a, a'::as, rs => match a == a' with | true => eraseRepsAux a as rs | false => eraseRepsAux a' as (a::rs) /-- Erase repeated adjacent elements. -/ def eraseReps {α} [BEq α] : List α → List α | [] => [] | a::as => eraseRepsAux a as [] @[specialize] def spanAux (p : α → Bool) : List α → List α → List α × List α | [], rs => (rs.reverse, []) | a::as, rs => match p a with | true => spanAux p as (a::rs) | false => (rs.reverse, a::as) @[inline] def span (p : α → Bool) (as : List α) : List α × List α := spanAux p as [] @[specialize] def groupByAux (eq : α → α → Bool) : List α → List (List α) → List (List α) | a::as, (ag::g)::gs => match eq a ag with | true => groupByAux eq as ((a::ag::g)::gs) | false => groupByAux eq as ([a]::(ag::g).reverse::gs) | _, gs => gs.reverse @[specialize] def groupBy (p : α → α → Bool) : List α → List (List α) | [] => [] | a::as => groupByAux p as [[a]] def lookup [BEq α] : α → List (α × β) → Option β | _, [] => none | a, (k,b)::es => match a == k with | true => some b | false => lookup a es def removeAll [BEq α] (xs ys : List α) : List α := xs.filter (fun x => ys.notElem x) def drop : Nat → List α → List α | 0, a => a | n+1, [] => [] | n+1, a::as => drop n as def take : Nat → List α → List α | 0, a => [] | n+1, [] => [] | n+1, a::as => a :: take n as def takeWhile (p : α → Bool) : List α → List α | [] => [] | hd :: tl => match p hd with | true => hd :: takeWhile p tl | false => [] @[specialize] def foldr (f : α → β → β) (init : β) : List α → β | [] => init | a :: l => f a (foldr f init l) @[inline] def any (l : List α) (p : α → Bool) : Bool := foldr (fun a r => p a || r) false l @[inline] def all (l : List α) (p : α → Bool) : Bool := foldr (fun a r => p a && r) true l def or (bs : List Bool) : Bool := bs.any id def and (bs : List Bool) : Bool := bs.all id def zipWith (f : α → β → γ) : List α → List β → List γ | x::xs, y::ys => f x y :: zipWith f xs ys | _, _ => [] def zip : List α → List β → List (Prod α β) := zipWith Prod.mk def unzip : List (α × β) → List α × List β | [] => ([], []) | (a, b) :: t => match unzip t with | (al, bl) => (a::al, b::bl) def rangeAux : Nat → List Nat → List Nat | 0, ns => ns | n+1, ns => rangeAux n (n::ns) def range (n : Nat) : List Nat := rangeAux n [] def iota : Nat → List Nat | 0 => [] | m@(n+1) => m :: iota n def enumFrom : Nat → List α → List (Nat × α) | n, [] => nil | n, x :: xs => (n, x) :: enumFrom (n + 1) xs def enum : List α → List (Nat × α) := enumFrom 0 def init : List α → List α | [] => [] | [a] => [] | a::l => a::init l def intersperse (sep : α) : List α → List α | [] => [] | [x] => [x] | x::xs => x :: sep :: intersperse sep xs def intercalate (sep : List α) (xs : List (List α)) : List α := join (intersperse sep xs) @[inline] protected def bind {α : Type u} {β : Type v} (a : List α) (b : α → List β) : List β := join (map b a) @[inline] protected def pure {α : Type u} (a : α) : List α := [a] inductive lt [LT α] : List α → List α → Prop where | nil (b : α) (bs : List α) : lt [] (b::bs) | head {a : α} (as : List α) {b : α} (bs : List α) : a < b → lt (a::as) (b::bs) | tail {a : α} {as : List α} {b : α} {bs : List α} : ¬ a < b → ¬ b < a → lt as bs → lt (a::as) (b::bs) instance [LT α] : LT (List α) := ⟨List.lt⟩ instance hasDecidableLt [LT α] [h : DecidableRel (α:=α) (·<·)] : (l₁ l₂ : List α) → Decidable (l₁ < l₂) | [], [] => isFalse (fun h => nomatch h) | [], b::bs => isTrue (List.lt.nil _ _) | a::as, [] => isFalse (fun h => nomatch h) | a::as, b::bs => match h a b with | isTrue h₁ => isTrue (List.lt.head _ _ h₁) | isFalse h₁ => match h b a with | isTrue h₂ => isFalse (fun h => match h with | List.lt.head _ _ h₁' => absurd h₁' h₁ | List.lt.tail _ h₂' _ => absurd h₂ h₂') | isFalse h₂ => match hasDecidableLt as bs with | isTrue h₃ => isTrue (List.lt.tail h₁ h₂ h₃) | isFalse h₃ => isFalse (fun h => match h with | List.lt.head _ _ h₁' => absurd h₁' h₁ | List.lt.tail _ _ h₃' => absurd h₃' h₃) @[reducible] protected def le [LT α] (a b : List α) : Prop := ¬ b < a instance [LT α] : LE (List α) := ⟨List.le⟩ instance [LT α] [h : DecidableRel ((· < ·) : α → α → Prop)] : (l₁ l₂ : List α) → Decidable (l₁ ≤ l₂) := fun a b => inferInstanceAs (Decidable (Not _)) /-- `isPrefixOf l₁ l₂` returns `true` Iff `l₁` is a prefix of `l₂`. -/ def isPrefixOf [BEq α] : List α → List α → Bool | [], _ => true | _, [] => false | a::as, b::bs => a == b && isPrefixOf as bs /-- `isSuffixOf l₁ l₂` returns `true` Iff `l₁` is a suffix of `l₂`. -/ def isSuffixOf [BEq α] (l₁ l₂ : List α) : Bool := isPrefixOf l₁.reverse l₂.reverse @[specialize] def isEqv : List α → List α → (α → α → Bool) → Bool | [], [], _ => true | a::as, b::bs, eqv => eqv a b && isEqv as bs eqv | _, _, eqv => false protected def beq [BEq α] : List α → List α → Bool | [], [] => true | a::as, b::bs => a == b && List.beq as bs | _, _ => false instance [BEq α] : BEq (List α) := ⟨List.beq⟩ @[simp] def replicate : (n : Nat) → (a : α) → List α | 0, a => [] | n+1, a => a :: replicate n a def replicateTR {α : Type u} (n : Nat) (a : α) : List α := let rec loop : Nat → List α → List α | 0, as => as | n+1, as => loop n (a::as) loop n [] theorem replicateTR_loop_replicate_eq (a : α) (m n : Nat) : replicateTR.loop a n (replicate m a) = replicate (n + m) a := by induction n generalizing m with simp [replicateTR.loop] | succ n ih => simp [Nat.succ_add]; exact ih (m+1) @[csimp] theorem replicate_eq_replicateTR : @List.replicate = @List.replicateTR := by apply funext; intro α; apply funext; intro n; apply funext; intro a exact (replicateTR_loop_replicate_eq _ 0 n).symm def dropLast {α} : List α → List α | [] => [] | [a] => [] | a::as => a :: dropLast as @[simp] theorem length_replicate (n : Nat) (a : α) : (replicate n a).length = n := by induction n <;> simp_all @[simp] theorem length_concat (as : List α) (a : α) : (concat as a).length = as.length + 1 := by induction as with | nil => rfl | cons x xs ih => simp [concat, ih] @[simp] theorem length_set (as : List α) (i : Nat) (a : α) : (as.set i a).length = as.length := by induction as generalizing i with | nil => rfl | cons x xs ih => cases i with | zero => rfl | succ i => simp [set, ih] @[simp] theorem length_dropLast (as : List α) : as.dropLast.length = as.length - 1 := by match as with | [] => rfl | [a] => rfl | a::b::as => have ih := length_dropLast (b::as) simp[dropLast, ih] rfl @[simp] theorem length_append (as bs : List α) : (as ++ bs).length = as.length + bs.length := by induction as with | nil => simp | cons a as ih => simp [ih, Nat.succ_add] @[simp] theorem length_reverse (as : List α) : (as.reverse).length = as.length := by induction as with | nil => rfl | cons a as ih => simp [ih] def maximum? [LT α] [DecidableRel (@LT.lt α _)] : List α → Option α | [] => none | a::as => some <| as.foldl max a def minimum? [LE α] [DecidableRel (@LE.le α _)] : List α → Option α | [] => none | a::as => some <| as.foldl min a end List
133b72e4a4a9af8e05a2b991818b3b3608e597a5
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/types/nat/order.hlean
0d33b8934693d0256b498a5c03e322a32d1ec865
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
18,648
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad The order relation on the natural numbers. -/ import .basic algebra.ordered_ring open eq eq.ops algebra algebra namespace nat /- lt and le -/ protected theorem le_of_lt_sum_eq {m n : ℕ} (H : m < n ⊎ m = n) : m ≤ n := nat.le_of_eq_sum_lt (sum.swap H) protected theorem lt_sum_eq_of_le {m n : ℕ} (H : m ≤ n) : m < n ⊎ m = n := sum.swap (nat.eq_sum_lt_of_le H) protected theorem le_iff_lt_sum_eq (m n : ℕ) : m ≤ n ↔ m < n ⊎ m = n := iff.intro nat.lt_sum_eq_of_le nat.le_of_lt_sum_eq protected theorem lt_of_le_prod_ne {m n : ℕ} (H1 : m ≤ n) : m ≠ n → m < n := sum_resolve_right (nat.eq_sum_lt_of_le H1) protected theorem lt_iff_le_prod_ne (m n : ℕ) : m < n ↔ m ≤ n × m ≠ n := iff.intro (take H, pair (nat.le_of_lt H) (take H1, !nat.lt_irrefl (H1 ▸ H))) (prod.rec nat.lt_of_le_prod_ne) theorem le_add_right (n k : ℕ) : n ≤ n + k := nat.rec !nat.le_refl (λ k, le_succ_of_le) k theorem le_add_left (n m : ℕ): n ≤ m + n := !add.comm ▸ !le_add_right theorem le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m := h ▸ !le_add_right theorem le.elim {n m : ℕ} : n ≤ m → Σ k, n + k = m := le.rec (sigma.mk 0 rfl) (λm h, sigma.rec (λ k H, sigma.mk (succ k) (H ▸ rfl))) protected theorem le_total {m n : ℕ} : m ≤ n ⊎ n ≤ m := sum.imp_left nat.le_of_lt !nat.lt_sum_ge /- addition -/ protected theorem add_le_add_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m := obtain l Hl, from le.elim H, le.intro (Hl ▸ !add.assoc) protected theorem add_le_add_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k := !add.comm ▸ !add.comm ▸ nat.add_le_add_left H k protected theorem le_of_add_le_add_left {k n m : ℕ} (H : k + n ≤ k + m) : n ≤ m := obtain l Hl, from le.elim H, le.intro (nat.add_left_cancel (!add.assoc⁻¹ ⬝ Hl)) protected theorem lt_of_add_lt_add_left {k n m : ℕ} (H : k + n < k + m) : n < m := let H' := nat.le_of_lt H in nat.lt_of_le_prod_ne (nat.le_of_add_le_add_left H') (assume Heq, !nat.lt_irrefl (Heq ▸ H)) protected theorem add_lt_add_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m := lt_of_succ_le (!add_succ ▸ nat.add_le_add_left (succ_le_of_lt H) k) protected theorem add_lt_add_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k := !add.comm ▸ !add.comm ▸ nat.add_lt_add_left H k protected theorem lt_add_of_pos_right {n k : ℕ} (H : k > 0) : n < n + k := !add_zero ▸ nat.add_lt_add_left H n /- multiplication -/ theorem mul_le_mul_left {n m : ℕ} (k : ℕ) (H : n ≤ m) : k * n ≤ k * m := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, have k * n + k * l = k * m, by rewrite [-left_distrib, Hl], le.intro this theorem mul_le_mul_right {n m : ℕ} (k : ℕ) (H : n ≤ m) : n * k ≤ m * k := !mul.comm ▸ !mul.comm ▸ !mul_le_mul_left H protected theorem mul_le_mul {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l := nat.le_trans (!nat.mul_le_mul_right H1) (!nat.mul_le_mul_left H2) protected theorem mul_lt_mul_of_pos_left {n m k : ℕ} (H : n < m) (Hk : k > 0) : k * n < k * m := nat.lt_of_lt_of_le (nat.lt_add_of_pos_right Hk) (!mul_succ ▸ nat.mul_le_mul_left k (succ_le_of_lt H)) protected theorem mul_lt_mul_of_pos_right {n m k : ℕ} (H : n < m) (Hk : k > 0) : n * k < m * k := !mul.comm ▸ !mul.comm ▸ nat.mul_lt_mul_of_pos_left H Hk /- nat is an instance of a linearly ordered semiring and a lattice -/ protected definition decidable_linear_ordered_semiring [trans_instance] : decidable_linear_ordered_semiring nat := ⦃ decidable_linear_ordered_semiring, nat.comm_semiring, add_left_cancel := @nat.add_left_cancel, add_right_cancel := @nat.add_right_cancel, lt := nat.lt, le := nat.le, le_refl := nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, le_iff_lt_sum_eq := @nat.le_iff_lt_sum_eq, le_of_lt := @nat.le_of_lt, lt_irrefl := @nat.lt_irrefl, lt_of_lt_of_le := @nat.lt_of_lt_of_le, lt_of_le_of_lt := @nat.lt_of_le_of_lt, lt_of_add_lt_add_left := @nat.lt_of_add_lt_add_left, add_lt_add_left := @nat.add_lt_add_left, add_le_add_left := @nat.add_le_add_left, le_of_add_le_add_left := @nat.le_of_add_le_add_left, zero_lt_one := zero_lt_succ 0, mul_le_mul_of_nonneg_left := (take a b c H1 H2, nat.mul_le_mul_left c H1), mul_le_mul_of_nonneg_right := (take a b c H1 H2, nat.mul_le_mul_right c H1), mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right, decidable_lt := nat.decidable_lt ⦄ definition nat_has_dvd [instance] [priority nat.prio] : has_dvd nat := has_dvd.mk has_dvd.dvd theorem add_pos_left {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < a + b := @add_pos_of_pos_of_nonneg _ _ a b H !zero_le theorem add_pos_right {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < b + a := by rewrite add.comm; apply add_pos_left H b theorem add_eq_zero_iff_eq_zero_prod_eq_zero {a b : ℕ} : a + b = 0 ↔ a = 0 × b = 0 := @add_eq_zero_iff_eq_zero_prod_eq_zero_of_nonneg_of_nonneg _ _ a b !zero_le !zero_le theorem le_add_of_le_left {a b c : ℕ} (H : b ≤ c) : b ≤ a + c := @le_add_of_nonneg_of_le _ _ a b c !zero_le H theorem le_add_of_le_right {a b c : ℕ} (H : b ≤ c) : b ≤ c + a := @le_add_of_le_of_nonneg _ _ a b c H !zero_le theorem lt_add_of_lt_left {b c : ℕ} (H : b < c) (a : ℕ) : b < a + c := @lt_add_of_nonneg_of_lt _ _ a b c !zero_le H theorem lt_add_of_lt_right {b c : ℕ} (H : b < c) (a : ℕ) : b < c + a := @lt_add_of_lt_of_nonneg _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_left {a b c : ℕ} (H : c * a < c * b) : a < b := @lt_of_mul_lt_mul_left _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_right {a b c : ℕ} (H : a * c < b * c) : a < b := @lt_of_mul_lt_mul_right _ _ a b c H !zero_le theorem pos_of_mul_pos_left {a b : ℕ} (H : 0 < a * b) : 0 < b := @pos_of_mul_pos_left _ _ a b H !zero_le theorem pos_of_mul_pos_right {a b : ℕ} (H : 0 < a * b) : 0 < a := @pos_of_mul_pos_right _ _ a b H !zero_le theorem zero_le_one : (0:nat) ≤ 1 := dec_star /- properties specific to nat -/ theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m := lt_of_succ_le (le.intro H) theorem lt_elim {n m : ℕ} (H : n < m) : Σk, succ n + k = m := le.elim (succ_le_of_lt H) theorem lt_add_succ (n m : ℕ) : n < n + succ m := lt_intro !succ_add_eq_succ_add theorem eq_zero_of_le_zero {n : ℕ} (H : n ≤ 0) : n = 0 := obtain (k : ℕ) (Hk : n + k = 0), from le.elim H, eq_zero_of_add_eq_zero_right Hk /- succ and pred -/ theorem le_of_lt_succ {m n : nat} : m < succ n → m ≤ n := le_of_succ_le_succ theorem lt_iff_succ_le (m n : nat) : m < n ↔ succ m ≤ n := iff.rfl theorem lt_succ_iff_le (m n : nat) : m < succ n ↔ m ≤ n := iff.intro le_of_lt_succ lt_succ_of_le theorem self_le_succ (n : ℕ) : n ≤ succ n := le.intro !add_one theorem succ_le_sum_eq_of_le {n m : ℕ} : n ≤ m → succ n ≤ m ⊎ n = m := lt_sum_eq_of_le theorem pred_le_of_le_succ {n m : ℕ} : n ≤ succ m → pred n ≤ m := pred_le_pred theorem succ_le_of_le_pred {n m : ℕ} : succ n ≤ m → n ≤ pred m := pred_le_pred theorem pred_le_pred_of_le {n m : ℕ} : n ≤ m → pred n ≤ pred m := pred_le_pred theorem pre_lt_of_lt {n m : ℕ} : n < m → pred n < m := lt_of_le_of_lt !pred_le theorem lt_of_pred_lt_pred {n m : ℕ} (H : pred n < pred m) : n < m := lt_of_not_ge (suppose m ≤ n, not_lt_of_ge (pred_le_pred_of_le this) H) theorem le_sum_eq_succ_of_le_succ {n m : ℕ} (H : n ≤ succ m) : n ≤ m ⊎ n = succ m := sum.imp_left le_of_succ_le_succ (succ_le_sum_eq_of_le H) theorem le_pred_self (n : ℕ) : pred n ≤ n := !pred_le theorem succ_pos (n : ℕ) : 0 < succ n := !zero_lt_succ theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n := (sum_resolve_right (eq_zero_sum_eq_succ_pred n) (ne.symm (ne_of_lt H)))⁻¹ theorem exists_eq_succ_of_lt {n : ℕ} : Π {m : ℕ}, n < m → Σk, m = succ k | 0 H := absurd H !not_lt_zero | (succ k) H := sigma.mk k rfl theorem lt_succ_self (n : ℕ) : n < succ n := lt.base n lemma lt_succ_of_lt {i j : nat} : i < j → i < succ j := assume Plt, lt.trans Plt (self_lt_succ j) lemma one_le_succ (n : ℕ) : 1 ≤ succ n := nat.succ_le_succ !zero_le lemma two_le_succ_succ (n : ℕ) : 2 ≤ succ (succ n) := nat.succ_le_succ !one_le_succ /- other forms of induction -/ protected definition strong_rec_on {P : nat → Type} (n : ℕ) (H : Πn, (Πm, m < n → P m) → P n) : P n := nat.rec (λm h, absurd h !not_lt_zero) (λn' (IH : Π {m : ℕ}, m < n' → P m) m l, sum.elim (lt_sum_eq_of_le (le_of_lt_succ l)) IH (λ e, eq.rec (H n' @IH) e⁻¹)) (succ n) n !lt_succ_self protected theorem case_strong_rec_on {P : nat → Type} (a : nat) (H0 : P 0) (Hind : Π(n : nat), (Πm, m ≤ n → P m) → P (succ n)) : P a := nat.strong_rec_on a (take n, show (Π m, m < n → P m) → P n, from nat.cases_on n (suppose (Π m, m < 0 → P m), show P 0, from H0) (take n, suppose (Π m, m < succ n → P m), show P (succ n), from Hind n (take m, assume H1 : m ≤ n, this _ (lt_succ_of_le H1)))) /- pos -/ theorem by_cases_zero_pos {P : ℕ → Type} (y : ℕ) (H0 : P 0) (H1 : Π {y : nat}, y > 0 → P y) : P y := nat.cases_on y H0 (take y, H1 !succ_pos) theorem eq_zero_sum_pos (n : ℕ) : n = 0 ⊎ n > 0 := sum_of_sum_of_imp_left (sum.swap (lt_sum_eq_of_le !zero_le)) (suppose 0 = n, by subst n) theorem pos_of_ne_zero {n : ℕ} (H : n ≠ 0) : n > 0 := sum.elim !eq_zero_sum_pos (take H2 : n = 0, by contradiction) (take H2 : n > 0, H2) theorem ne_zero_of_pos {n : ℕ} (H : n > 0) : n ≠ 0 := ne.symm (ne_of_lt H) theorem exists_eq_succ_of_pos {n : ℕ} (H : n > 0) : Σl, n = succ l := exists_eq_succ_of_lt H theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 := pos_of_ne_zero (suppose m = 0, have n = 0, from eq_zero_of_zero_dvd (this ▸ H1), ne_of_lt H2 (by subst n)) /- multiplication -/ theorem mul_lt_mul_of_le_of_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) : n * m < k * l := lt_of_le_of_lt (mul_le_mul_right m H1) (mul_lt_mul_of_pos_left H2 Hk) theorem mul_lt_mul_of_lt_of_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) : n * m < k * l := lt_of_le_of_lt (mul_le_mul_left n H2) (mul_lt_mul_of_pos_right H1 Hl) theorem mul_lt_mul_of_le_of_le {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l := have H3 : n * m ≤ k * m, from mul_le_mul_right m (le_of_lt H1), have H4 : k * m < k * l, from mul_lt_mul_of_pos_left H2 (lt_of_le_of_lt !zero_le H1), lt_of_le_of_lt H3 H4 theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := have n * m ≤ n * k, by rewrite H, have m ≤ k, from le_of_mul_le_mul_left this Hn, have n * k ≤ n * m, by rewrite H, have k ≤ m, from le_of_mul_le_mul_left this Hn, le.antisymm `m ≤ k` this theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := eq_of_mul_eq_mul_left Hm (!mul.comm ▸ !mul.comm ▸ H) theorem eq_zero_sum_eq_of_mul_eq_mul_left {n m k : ℕ} (H : n * m = n * k) : n = 0 ⊎ m = k := sum_of_sum_of_imp_right !eq_zero_sum_pos (assume Hn : n > 0, eq_of_mul_eq_mul_left Hn H) theorem eq_zero_sum_eq_of_mul_eq_mul_right {n m k : ℕ} (H : n * m = k * m) : m = 0 ⊎ n = k := eq_zero_sum_eq_of_mul_eq_mul_left (!mul.comm ▸ !mul.comm ▸ H) theorem eq_one_of_mul_eq_one_right {n m : ℕ} (H : n * m = 1) : n = 1 := have H2 : n * m > 0, by rewrite H; apply succ_pos, sum.elim (le_sum_gt n 1) (suppose n ≤ 1, have n > 0, from pos_of_mul_pos_right H2, show n = 1, from le.antisymm `n ≤ 1` (succ_le_of_lt this)) (suppose n > 1, have m > 0, from pos_of_mul_pos_left H2, have n * m ≥ 2 * 1, from nat.mul_le_mul (succ_le_of_lt `n > 1`) (succ_le_of_lt this), have 1 ≥ 2, from !mul_one ▸ H ▸ this, absurd !lt_succ_self (not_lt_of_ge this)) theorem eq_one_of_mul_eq_one_left {n m : ℕ} (H : n * m = 1) : m = 1 := eq_one_of_mul_eq_one_right (!mul.comm ▸ H) theorem eq_one_of_mul_eq_self_left {n m : ℕ} (Hpos : n > 0) (H : m * n = n) : m = 1 := eq_of_mul_eq_mul_right Hpos (H ⬝ !one_mul⁻¹) theorem eq_one_of_mul_eq_self_right {n m : ℕ} (Hpos : m > 0) (H : m * n = m) : n = 1 := eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H) theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 := dvd.elim H (take m, suppose 1 = n * m, eq_one_of_mul_eq_one_right this⁻¹) /- min and max -/ open decidable theorem min_zero [simp] (a : ℕ) : min a 0 = 0 := by rewrite [min_eq_right !zero_le] theorem zero_min [simp] (a : ℕ) : min 0 a = 0 := by rewrite [min_eq_left !zero_le] theorem max_zero [simp] (a : ℕ) : max a 0 = a := by rewrite [max_eq_left !zero_le] theorem zero_max [simp] (a : ℕ) : max 0 a = a := by rewrite [max_eq_right !zero_le] theorem min_succ_succ [simp] (a b : ℕ) : min (succ a) (succ b) = succ (min a b) := sum.elim !lt_sum_ge (suppose a < b, by rewrite [min_eq_left_of_lt this, min_eq_left_of_lt (succ_lt_succ this)]) (suppose a ≥ b, by rewrite [min_eq_right this, min_eq_right (succ_le_succ this)]) theorem max_succ_succ [simp] (a b : ℕ) : max (succ a) (succ b) = succ (max a b) := sum.elim !lt_sum_ge (suppose a < b, by rewrite [max_eq_right_of_lt this, max_eq_right_of_lt (succ_lt_succ this)]) (suppose a ≥ b, by rewrite [max_eq_left this, max_eq_left (succ_le_succ this)]) /- In algebra.ordered_group, these next four are only proved for additive groups, not additive semigroups. -/ protected theorem min_add_add_left (a b c : ℕ) : min (a + b) (a + c) = a + min b c := decidable.by_cases (suppose b ≤ c, have a + b ≤ a + c, from add_le_add_left this _, by rewrite [min_eq_left `b ≤ c`, min_eq_left this]) (suppose ¬ b ≤ c, have c ≤ b, from le_of_lt (lt_of_not_ge this), have a + c ≤ a + b, from add_le_add_left this _, by rewrite [min_eq_right `c ≤ b`, min_eq_right this]) protected theorem min_add_add_right (a b c : ℕ) : min (a + c) (b + c) = min a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.min_add_add_left protected theorem max_add_add_left (a b c : ℕ) : max (a + b) (a + c) = a + max b c := decidable.by_cases (suppose b ≤ c, have a + b ≤ a + c, from add_le_add_left this _, by rewrite [max_eq_right `b ≤ c`, max_eq_right this]) (suppose ¬ b ≤ c, have c ≤ b, from le_of_lt (lt_of_not_ge this), have a + c ≤ a + b, from add_le_add_left this _, by rewrite [max_eq_left `c ≤ b`, max_eq_left this]) protected theorem max_add_add_right (a b c : ℕ) : max (a + c) (b + c) = max a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.max_add_add_left /- least and greatest -/ section least_prod_greatest variable (P : ℕ → Type) variable [decP : Π n, decidable (P n)] include decP -- returns the least i < n satisfying P, sum n if there is none definition least : ℕ → ℕ | 0 := 0 | (succ n) := if P (least n) then least n else succ n theorem least_of_bound {n : ℕ} (H : P n) : P (least P n) := begin induction n with [m, ih], rewrite ↑least, apply H, rewrite ↑least, cases decidable.em (P (least P m)) with [Hlp, Hlp], rewrite [if_pos Hlp], apply Hlp, rewrite [if_neg Hlp], apply H end theorem least_le (n : ℕ) : least P n ≤ n:= begin induction n with [m, ih], {rewrite ↑least}, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], apply le.trans ih !le_succ, rewrite [if_neg Pnsm] end theorem least_of_lt {i n : ℕ} (ltin : i < n) (H : P i) : P (least P n) := begin induction n with [m, ih], exact absurd ltin !not_lt_zero, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], apply Psm, rewrite [if_neg Pnsm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], exact absurd (ih Hlt) Pnsm, rewrite Heq at H, exact absurd (least_of_bound P H) Pnsm end theorem ge_least_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≥ least P n := begin induction n with [m, ih], exact absurd ltin !not_lt_zero, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], apply ih Hlt, rewrite Heq, apply least_le, rewrite [if_neg Pnsm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], apply absurd (least_of_lt P Hlt Hi) Pnsm, rewrite Heq at Hi, apply absurd (least_of_bound P Hi) Pnsm end theorem least_lt {n i : ℕ} (ltin : i < n) (Hi : P i) : least P n < n := lt_of_le_of_lt (ge_least_of_lt P ltin Hi) ltin -- returns the largest i < n satisfying P, sum n if there is none. definition greatest : ℕ → ℕ | 0 := 0 | (succ n) := if P n then n else greatest n theorem greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : P (greatest P n) := begin induction n with [m, ih], {exact absurd ltin !not_lt_zero}, {cases (decidable.em (P m)) with [Psm, Pnsm], {rewrite [↑greatest, if_pos Psm]; exact Psm}, {rewrite [↑greatest, if_neg Pnsm], have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm, have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim, apply ih ltim}} end theorem le_greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≤ greatest P n := begin induction n with [m, ih], {exact absurd ltin !not_lt_zero}, {cases (decidable.em (P m)) with [Psm, Pnsm], {rewrite [↑greatest, if_pos Psm], apply le_of_lt_succ ltin}, {rewrite [↑greatest, if_neg Pnsm], have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm, have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim, apply ih ltim}} end end least_prod_greatest end nat
195cee7c1df4f351bb261b1c4db0c551a50e45b1
43390109ab88557e6090f3245c47479c123ee500
/src/M1P2/sheet_9.lean
41f008586e3b2a6531114e042e78ba497c0ff1d6
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,861
lean
import algebra.euclidean_domain data.set data.complex.basic data.int.modeq variables {R : Type*} [euclidean_domain R] variables a b : ℤ def gaussian_ints : ℂ := ⟨a,b⟩ --def gaussian_ints_val : → ℕ := a^2 + b^2 --def valuation : def poly_coeff_rat := list rat -- could also use finsupp def ints_adj_sqrt (n : ℤ) := set.range (λ x : ℤ × ℤ, x.1 + x.2 * nat.sqrt n.nat_abs) -- 1. Let d ∈ Z be such that ℤ ∌ √d ,and let x,y ∈ Z[√d]. Verify the identity N(xy) = N(x)N(y), where N(r+s√d)=r^2−ds^2. theorem sheet09_q1 (d : ℤ) : (d ≡ 2 [ZMOD 4] ∨ d ≡ 3 [ZMOD 4]) := sorry -- 2. Let R be a ring with unity, and let u be a unit in R. Show that u√n us a unit for any n ∈ Z. Hence or otherwise, show that the the ring Z[√2] has infinitely many units. --theorem sheet09_q2 -- 3∗. Suppose R is a finite integral domain. Prove that R is a field. Look carefully at your proof. To what extent does your proof make full use of the hypotheses? Formulate and prove a more general result about finite commutative rings with unity. --theorem sheet09_q3 -- 4. Which of the following elements are irreducible in the rings specified? -- (a) 2 ∈ ℤ[i]. -- (b) 1+2√2 ∈ ℤ[√2]. -- (c) 1+2√−2 ∈ ℤ[√−2]. -- (d) 5 ∈ ℤ[i]. -- (e) 7 ∈ ℤ[i]. -- (f) x^2+3x+1 ∈ ℚ[x]. -- (g) x^2−x−6 ∈ ℚ[x]. -- (h) x^4+x^2+1 ∈ ℚ[x]. -- (i) x^3+3x+2 ∈ ℤ_7[x]. -- 5. Factorize the following ring elements into irreducibles. -- (a) 3 + 5i ∈ ℤ[i]. --e.g 1+i,4+i -- (b) 4+2√−2 ∈ ℤ[√−2] -- -- (c) x^6−1 ∈ ℝ[x]. -- 6. Use the Euclidean algorithm to find the highest common factor for each of the following pairs. -- (a) 3+√3 and 12 ∈ ℤ[√3]. -- (b) 7+i and 5+3i ∈ ℤ[i]. -- (c) x^6 and x^8+x^7+1 ∈ ℂ[x]. -- (d) x^3+x+1 and x5+1 ∈ ℝ[x].
4ecec8edc7b6171abf0e6a89ed1b5d489b541cdb
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/continued_fractions/convergents_equiv.lean
e1ac8e348006f988514fd94774ce79e0c7630ce4
[ "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
20,538
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.continuants_recurrence import algebra.continued_fractions.terminated_stable import tactic.linarith import tactic.field_simp /-! # Equivalence of Recursive and Direct Computations of `gcf` Convergents ## Summary We show the equivalence of two computations of convergents (recurrence relation (`convergents`) vs. direct evaluation (`convergents'`)) for `gcf`s on linear ordered fields. We follow the proof from [hardy2008introduction], Chapter 10. Here's a sketch: Let `c` be a continued fraction `[h; (a₀, b₀), (a₁, b₁), (a₂, b₂),...]`, visually: a₀ h + --------------------------- a₁ b₀ + -------------------- a₂ b₁ + -------------- a₃ b₂ + -------- b₃ + ... One can compute the convergents of `c` in two ways: 1. Directly evaluating the fraction described by `c` up to a given `n` (`convergents'`) 2. Using the recurrence (`convergents`): - `A₋₁ = 1, A₀ = h, Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, and - `B₋₁ = 0, B₀ = 1, Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂`. To show the equivalence of the computations in the main theorem of this file `convergents_eq_convergents'`, we proceed by induction. The case `n = 0` is trivial. For `n + 1`, we first "squash" the `n + 1`th position of `c` into the `n`th position to obtain another continued fraction `c' := [h; (a₀, b₀),..., (aₙ-₁, bₙ-₁), (aₙ, bₙ + aₙ₊₁ / bₙ₊₁), (aₙ₊₁, bₙ₊₁),...]`. This squashing process is formalised in section `squash`. Note that directly evaluating `c` up to position `n + 1` is equal to evaluating `c'` up to `n`. This is shown in lemma `succ_nth_convergent'_eq_squash_gcf_nth_convergent'`. By the inductive hypothesis, the two computations for the `n`th convergent of `c` coincide. So all that is left to show is that the recurrence relation for `c` at `n + 1` and and `c'` at `n` coincide. This can be shown by another induction. The corresponding lemma in this file is `succ_nth_convergent_eq_squash_gcf_nth_convergent`. ## Main Theorems - `generalized_continued_fraction.convergents_eq_convergents'` shows the equivalence under a strict positivity restriction on the sequence. - `continued_fractions.convergents_eq_convergents'` shows the equivalence for (regular) continued fractions. ## References - https://en.wikipedia.org/wiki/Generalized_continued_fraction - [*Hardy, GH and Wright, EM and Heath-Brown, Roger and Silverman, Joseph*][hardy2008introduction] ## Tags fractions, recurrence, equivalence -/ variables {K : Type*} {n : ℕ} namespace generalized_continued_fraction open generalized_continued_fraction as gcf variables {g : gcf K} {s : seq $ gcf.pair K} section squash /-! We will show the equivalence of the computations by induction. To make the induction work, we need to be able to *squash* the nth and (n + 1)th value of a sequence. This squashing itself and the lemmas about it are not very interesting. As a reader, you hence might want to skip this section. -/ section with_division_ring variable [division_ring K] /-- Given a sequence of gcf.pairs `s = [(a₀, bₒ), (a₁, b₁), ...]`, `squash_seq s n` combines `⟨aₙ, bₙ⟩` and `⟨aₙ₊₁, bₙ₊₁⟩` at position `n` to `⟨aₙ, bₙ + aₙ₊₁ / bₙ₊₁⟩`. For example, `squash_seq s 0 = [(a₀, bₒ + a₁ / b₁), (a₁, b₁),...]`. If `s.terminated_at (n + 1)`, then `squash_seq s n = s`. -/ def squash_seq (s : seq $ gcf.pair K) (n : ℕ) : seq (gcf.pair K) := match prod.mk (s.nth n) (s.nth (n + 1)) with | ⟨some gp_n, some gp_succ_n⟩ := seq.nats.zip_with -- return the squashed value at position `n`; otherwise, do nothing. (λ n' gp, if n' = n then ⟨gp_n.a, gp_n.b + gp_succ_n.a / gp_succ_n.b⟩ else gp) s | _ := s end /-! We now prove some simple lemmas about the squashed sequence -/ /-- If the sequence already terminated at position `n + 1`, nothing gets squashed. -/ lemma squash_seq_eq_self_of_terminated (terminated_at_succ_n : s.terminated_at (n + 1)) : squash_seq s n = s := begin change s.nth (n + 1) = none at terminated_at_succ_n, cases s_nth_eq : (s.nth n); simp only [*, squash_seq] end /-- If the sequence has not terminated before position `n + 1`, the value at `n + 1` gets squashed into position `n`. -/ lemma squash_seq_nth_of_not_terminated {gp_n gp_succ_n : gcf.pair K} (s_nth_eq : s.nth n = some gp_n) (s_succ_nth_eq : s.nth (n + 1) = some gp_succ_n) : (squash_seq s n).nth n = some ⟨gp_n.a, gp_n.b + gp_succ_n.a / gp_succ_n.b⟩ := by simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth n) s_nth_eq _)] /-- The values before the squashed position stay the same. -/ lemma squash_seq_nth_of_lt {m : ℕ} (m_lt_n : m < n) : (squash_seq s n).nth m = s.nth m := begin cases s_succ_nth_eq : s.nth (n + 1), case option.none { rw (squash_seq_eq_self_of_terminated s_succ_nth_eq) }, case option.some { obtain ⟨gp_n, s_nth_eq⟩ : ∃ gp_n, s.nth n = some gp_n, from s.ge_stable n.le_succ s_succ_nth_eq, obtain ⟨gp_m, s_mth_eq⟩ : ∃ gp_m, s.nth m = some gp_m, from s.ge_stable (le_of_lt m_lt_n) s_nth_eq, simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth m) s_mth_eq _), (ne_of_lt m_lt_n)] } end /-- Squashing at position `n + 1` and taking the tail is the same as squashing the tail of the sequence at position `n`. -/ lemma squash_seq_succ_n_tail_eq_squash_seq_tail_n : (squash_seq s (n + 1)).tail = squash_seq s.tail n := begin cases s_succ_succ_nth_eq : s.nth (n + 2) with gp_succ_succ_n, case option.none { have : squash_seq s (n + 1) = s, from squash_seq_eq_self_of_terminated s_succ_succ_nth_eq, cases s_succ_nth_eq : (s.nth (n + 1)); simp only [squash_seq, seq.nth_tail, s_succ_nth_eq, s_succ_succ_nth_eq] }, case option.some { obtain ⟨gp_succ_n, s_succ_nth_eq⟩ : ∃ gp_succ_n, s.nth (n + 1) = some gp_succ_n, from s.ge_stable (n + 1).le_succ s_succ_succ_nth_eq, -- apply extensionality with `m` and continue by cases `m = n`. ext m, cases decidable.em (m = n) with m_eq_n m_ne_n, { have : s.tail.nth n = some gp_succ_n, from (s.nth_tail n).trans s_succ_nth_eq, simp [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_some (seq.nats_nth n) this), (seq.zip_with_nth_some (seq.nats_nth (n + 1)) s_succ_nth_eq)] }, { have : s.tail.nth m = s.nth (m + 1), from s.nth_tail m, cases s_succ_mth_eq : s.nth (m + 1), all_goals { have s_tail_mth_eq, from this.trans s_succ_mth_eq }, { simp only [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_none' s_succ_mth_eq), (seq.zip_with_nth_none' s_tail_mth_eq)] }, { simp [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_some (seq.nats_nth (m + 1)) s_succ_mth_eq), (seq.zip_with_nth_some (seq.nats_nth m) s_tail_mth_eq)] } } } end /-- The auxiliary function `convergents'_aux` returns the same value for a sequence and the corresponding squashed sequence at the squashed position. -/ lemma succ_succ_nth_convergent'_aux_eq_succ_nth_convergent'_aux_squash_seq : convergents'_aux s (n + 2) = convergents'_aux (squash_seq s n) (n + 1) := begin cases s_succ_nth_eq : (s.nth $ n + 1) with gp_succ_n, case option.none { rw [(squash_seq_eq_self_of_terminated s_succ_nth_eq), (convergents'_aux_stable_step_of_terminated s_succ_nth_eq)] }, case option.some { induction n with m IH generalizing s gp_succ_n, case nat.zero { obtain ⟨gp_head, s_head_eq⟩ : ∃ gp_head, s.head = some gp_head, from s.ge_stable zero_le_one s_succ_nth_eq, have : (squash_seq s 0).head = some ⟨gp_head.a, gp_head.b + gp_succ_n.a / gp_succ_n.b⟩, from squash_seq_nth_of_not_terminated s_head_eq s_succ_nth_eq, simp [*, convergents'_aux, seq.head, seq.nth_tail] }, case nat.succ { obtain ⟨gp_head, s_head_eq⟩ : ∃ gp_head, s.head = some gp_head, from s.ge_stable (m + 2).zero_le s_succ_nth_eq, suffices : gp_head.a / (gp_head.b + convergents'_aux s.tail (m + 2)) = convergents'_aux (squash_seq s (m + 1)) (m + 2), by simpa only [convergents'_aux, s_head_eq], have : convergents'_aux s.tail (m + 2) = convergents'_aux (squash_seq s.tail m) (m + 1), by { have : s.tail.nth (m + 1) = some gp_succ_n, by simpa [seq.nth_tail] using s_succ_nth_eq, exact (IH _ this) }, have : (squash_seq s (m + 1)).head = some gp_head, from (squash_seq_nth_of_lt m.succ_pos).trans s_head_eq, simp only [*, convergents'_aux, squash_seq_succ_n_tail_eq_squash_seq_tail_n] } } end /-! Let us now lift the squashing operation to gcfs. -/ /-- Given a gcf `g = [h; (a₀, bₒ), (a₁, b₁), ...]`, we have - `squash_nth.gcf g 0 = [h + a₀ / b₀); (a₀, bₒ), ...]`, - `squash_nth.gcf g (n + 1) = ⟨g.h, squash_seq g.s n⟩` -/ def squash_gcf (g : gcf K) : ℕ → gcf K | 0 := match g.s.nth 0 with | none := g | some gp := ⟨g.h + gp.a / gp.b, g.s⟩ end | (n + 1) := ⟨g.h, squash_seq g.s n⟩ /-! Again, we derive some simple lemmas that are not really of interest. This time for the squashed gcf. -/ /-- If the gcf already terminated at position `n`, nothing gets squashed. -/ lemma squash_gcf_eq_self_of_terminated (terminated_at_n : terminated_at g n) : squash_gcf g n = g := begin cases n, case nat.zero { change g.s.nth 0 = none at terminated_at_n, simp only [convergents', squash_gcf, convergents'_aux, terminated_at_n] }, case nat.succ { cases g, simp [(squash_seq_eq_self_of_terminated terminated_at_n), squash_gcf] } end /-- The values before the squashed position stay the same. -/ lemma squash_gcf_nth_of_lt {m : ℕ} (m_lt_n : m < n) : (squash_gcf g (n + 1)).s.nth m = g.s.nth m := by simp only [squash_gcf, (squash_seq_nth_of_lt m_lt_n)] /-- `convergents'` returns the same value for a gcf and the corresponding squashed gcf at the squashed position. -/ lemma succ_nth_convergent'_eq_squash_gcf_nth_convergent' : g.convergents' (n + 1) = (squash_gcf g n).convergents' n := begin cases n, case nat.zero { cases g_s_head_eq : (g.s.nth 0); simp [g_s_head_eq, squash_gcf, convergents', convergents'_aux, seq.head] }, case nat.succ { simp only [succ_succ_nth_convergent'_aux_eq_succ_nth_convergent'_aux_squash_seq, convergents', squash_gcf] } end /-- The auxiliary continuants before the squashed position stay the same. -/ lemma continuants_aux_eq_continuants_aux_squash_gcf_of_le {m : ℕ} : m ≤ n → continuants_aux g m = (squash_gcf g n).continuants_aux m := nat.strong_induction_on m (begin clear m, assume m IH m_le_n, cases m with m', { refl }, { cases n with n', { have : false, from m'.not_succ_le_zero m_le_n, contradiction }, -- 1 ≰ 0 { cases m' with m'', { refl }, { -- get some inequalities to instantiate the IH for m'' and m'' + 1 have m'_lt_n : m'' + 1 < n' + 1, from m_le_n, have : m'' + 1 < m'' + 2, by linarith, have succ_m''th_conts_aux_eq := IH (m'' + 1) this (le_of_lt m'_lt_n), have : m'' < m'' + 2, by linarith, have m''th_conts_aux_eq := IH m'' this (le_of_lt $ lt_of_lt_of_le (by linarith) n'.le_succ), have : (squash_gcf g (n' + 1)).s.nth m'' = g.s.nth m'', from squash_gcf_nth_of_lt (by linarith), simp [continuants_aux, succ_m''th_conts_aux_eq, m''th_conts_aux_eq, this] } } } end) end with_division_ring /-- The convergents coincide in the expected way at the squashed position if the partial denominator at the squashed position is not zero. -/ lemma succ_nth_convergent_eq_squash_gcf_nth_convergent [field K] (nth_part_denom_ne_zero : ∀ {b : K}, g.partial_denominators.nth n = some b → b ≠ 0) : g.convergents (n + 1) = (squash_gcf g n).convergents n := begin cases decidable.em (g.terminated_at n) with terminated_at_n not_terminated_at_n, { have : squash_gcf g n = g, from squash_gcf_eq_self_of_terminated terminated_at_n, simp only [this, (convergents_stable_of_terminated n.le_succ terminated_at_n)] }, { obtain ⟨⟨a, b⟩, s_nth_eq⟩ : ∃ gp_n, g.s.nth n = some gp_n, from option.ne_none_iff_exists'.mp not_terminated_at_n, have b_ne_zero : b ≠ 0, from nth_part_denom_ne_zero (part_denom_eq_s_b s_nth_eq), cases n with n', case nat.zero { suffices : (b * g.h + a) / b = g.h + a / b, by simpa [squash_gcf, s_nth_eq, convergent_eq_conts_a_div_conts_b, (continuants_recurrence_aux s_nth_eq zeroth_continuant_aux_eq_one_zero first_continuant_aux_eq_h_one)], calc (b * g.h + a) / b = b * g.h / b + a / b : by ring -- requires `field`, not `division_ring` ... = g.h + a / b : by rw (mul_div_cancel_left _ b_ne_zero) }, case nat.succ { obtain ⟨⟨pa, pb⟩, s_n'th_eq⟩ : ∃ gp_n', g.s.nth n' = some gp_n' := g.s.ge_stable n'.le_succ s_nth_eq, -- Notations let g' := squash_gcf g (n' + 1), set pred_conts := g.continuants_aux (n' + 1) with succ_n'th_conts_aux_eq, set ppred_conts := g.continuants_aux n' with n'th_conts_aux_eq, let pA := pred_conts.a, let pB := pred_conts.b, let ppA := ppred_conts.a, let ppB := ppred_conts.b, set pred_conts' := g'.continuants_aux (n' + 1) with succ_n'th_conts_aux_eq', set ppred_conts' := g'.continuants_aux n' with n'th_conts_aux_eq', let pA' := pred_conts'.a, let pB' := pred_conts'.b, let ppA' := ppred_conts'.a, let ppB' := ppred_conts'.b, -- first compute the convergent of the squashed gcf have : g'.convergents (n' + 1) = ((pb + a / b) * pA' + pa * ppA') / ((pb + a / b) * pB' + pa * ppB'), { have : g'.s.nth n' = some ⟨pa, pb + a / b⟩ := squash_seq_nth_of_not_terminated s_n'th_eq s_nth_eq, rw [convergent_eq_conts_a_div_conts_b, continuants_recurrence_aux this n'th_conts_aux_eq'.symm succ_n'th_conts_aux_eq'.symm], }, rw this, -- then compute the convergent of the original gcf by recursively unfolding the continuants -- computation twice have : g.convergents (n' + 2) = (b * (pb * pA + pa * ppA) + a * pA) / (b * (pb * pB + pa * ppB) + a * pB), { -- use the recurrence once have : g.continuants_aux (n' + 2) = ⟨pb * pA + pa * ppA, pb * pB + pa * ppB⟩ := continuants_aux_recurrence s_n'th_eq n'th_conts_aux_eq.symm succ_n'th_conts_aux_eq.symm, -- and a second time rw [convergent_eq_conts_a_div_conts_b, continuants_recurrence_aux s_nth_eq succ_n'th_conts_aux_eq.symm this] }, rw this, suffices : ((pb + a / b) * pA + pa * ppA) / ((pb + a / b) * pB + pa * ppB) = (b * (pb * pA + pa * ppA) + a * pA) / (b * (pb * pB + pa * ppB) + a * pB), { obtain ⟨eq1, eq2, eq3, eq4⟩ : pA' = pA ∧ pB' = pB ∧ ppA' = ppA ∧ ppB' = ppB, { simp [*, (continuants_aux_eq_continuants_aux_squash_gcf_of_le $ le_refl $ n' + 1).symm, (continuants_aux_eq_continuants_aux_squash_gcf_of_le n'.le_succ).symm] }, symmetry, simpa only [eq1, eq2, eq3, eq4, mul_div_cancel _ b_ne_zero] }, field_simp, congr' 1; ring } } end end squash /-- Shows that the recurrence relation (`convergents`) and direct evaluation (`convergents'`) of the gcf coincide at position `n` if the sequence of fractions contains strictly positive values only. Requiring positivity of all values is just one possible condition to obtain this result. For example, the dual - sequences with strictly negative values only - would also work. In practice, one most commonly deals with (regular) continued fractions, which satisfy the positivity criterion required here. The analogous result for them (see `continued_fractions.convergents_eq_convergents`) hence follows directly from this theorem. -/ theorem convergents_eq_convergents' [linear_ordered_field K] (s_pos : ∀ {gp : gcf.pair K} {m : ℕ}, m < n → g.s.nth m = some gp → 0 < gp.a ∧ 0 < gp.b) : g.convergents n = g.convergents' n := begin induction n with n IH generalizing g, case nat.zero { simp }, case nat.succ { let g' := squash_gcf g n, -- first replace the rhs with the squashed computation suffices : g.convergents (n + 1) = g'.convergents' n, by rwa [succ_nth_convergent'_eq_squash_gcf_nth_convergent'], cases decidable.em (terminated_at g n) with terminated_at_n not_terminated_at_n, { have g'_eq_g : g' = g, from squash_gcf_eq_self_of_terminated terminated_at_n, have : ∀ ⦃gp m⦄, m < n → g.s.nth m = some gp → 0 < gp.a ∧ 0 < gp.b, by { assume _ _ m_lt_n s_mth_eq, exact (s_pos (nat.lt.step m_lt_n) s_mth_eq) }, rw [(convergents_stable_of_terminated n.le_succ terminated_at_n), g'_eq_g, (IH this)] }, { suffices : g.convergents (n + 1) = g'.convergents n, by -- invoke the IH for the squashed gcf { have : ∀ ⦃gp' m⦄, m < n → g'.s.nth m = some gp' → 0 < gp'.a ∧ 0 < gp'.b, by { assume gp' m m_lt_n s_mth_eq', -- case distinction on m + 1 = n or m + 1 < n cases m_lt_n with n succ_m_lt_n, { -- the difficult case at the squashed position: we first obtain the values from -- the sequence obtain ⟨gp_succ_m, s_succ_mth_eq⟩ : ∃ gp_succ_m, g.s.nth (m + 1) = some gp_succ_m, from option.ne_none_iff_exists'.mp not_terminated_at_n, obtain ⟨gp_m, mth_s_eq⟩ : ∃ gp_m, g.s.nth m = some gp_m, from g.s.ge_stable m.le_succ s_succ_mth_eq, -- we then plug them into the recurrence suffices : 0 < gp_m.a ∧ 0 < gp_m.b + gp_succ_m.a / gp_succ_m.b, by { have : g'.s.nth m = some ⟨gp_m.a, gp_m.b + gp_succ_m.a / gp_succ_m.b⟩, from squash_seq_nth_of_not_terminated mth_s_eq s_succ_mth_eq, have : gp' = ⟨gp_m.a, gp_m.b + gp_succ_m.a / gp_succ_m.b⟩, by cc, rwa this }, split, { exact (s_pos (nat.lt.step m_lt_n) mth_s_eq).left }, { have : 0 < gp_m.b, from (s_pos (nat.lt.step m_lt_n) mth_s_eq).right, have : 0 < gp_succ_m.a / gp_succ_m.b, by { have : 0 < gp_succ_m.a ∧ 0 < gp_succ_m.b, from s_pos (lt_add_one $ m + 1) s_succ_mth_eq, exact (div_pos this.left this.right) }, linarith } }, { -- the easy case: before the squashed position, nothing changes have : g.s.nth m = some gp', by { have : g'.s.nth m = g.s.nth m, from squash_gcf_nth_of_lt succ_m_lt_n, rwa this at s_mth_eq' }, exact s_pos (nat.lt.step $ nat.lt.step succ_m_lt_n) this } }, rwa [(IH this).symm] }, -- now the result follows from the fact that the convergents coincide at the squashed position -- as established in `succ_nth_convergent_eq_squash_gcf_nth_convergent`. have : ∀ ⦃b⦄, g.partial_denominators.nth n = some b → b ≠ 0, by { assume b nth_part_denom_eq, obtain ⟨gp, s_nth_eq, ⟨refl⟩⟩ : ∃ gp, g.s.nth n = some gp ∧ gp.b = b, from exists_s_b_of_part_denom nth_part_denom_eq, exact (ne_of_lt (s_pos (lt_add_one n) s_nth_eq).right).symm }, exact succ_nth_convergent_eq_squash_gcf_nth_convergent this } } end end generalized_continued_fraction namespace continued_fraction open generalized_continued_fraction as gcf open simple_continued_fraction as scf open continued_fraction as cf /-- Shows that the recurrence relation (`convergents`) and direct evaluation (`convergents'`) of a (regular) continued fraction coincide. -/ theorem convergents_eq_convergents' [linear_ordered_field K] {c : cf K} : (↑c : gcf K).convergents = (↑c : gcf K).convergents' := begin ext n, apply gcf.convergents_eq_convergents', assume gp m m_lt_n s_nth_eq, split, { have : gp.a = 1, from (c : scf K).property m gp.a (gcf.part_num_eq_s_a s_nth_eq), simp only [zero_lt_one, this] }, { exact (c.property m gp.b $ gcf.part_denom_eq_s_b s_nth_eq) } end end continued_fraction
0cb072af156b836168068fecd1bb0057eec58321
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/termParserAttr.lean
aa63738d01fa6a3d0b20d94cb01b32091270b4ea
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
2,095
lean
import Lean open Lean open Lean.Elab def runCore (input : String) (failIff : Bool := true) : CoreM Unit := do let env ← getEnv; let opts ← getOptions; let (env, messages) ← process input env opts; messages.toList.forM fun msg => do IO.println (← msg.toString) if failIff && messages.hasErrors then throwError "errors have been found"; if !failIff && !messages.hasErrors then throwError "there are no errors"; pure () open Lean.Parser @[term_parser] def tst := leading_parser "(|" >> termParser >> Parser.optional (symbol ", " >> termParser) >> "|)" def tst2 : Parser := symbol "(||" >> termParser >> symbol "||)" @[term_parser] def boo : ParserDescr := ParserDescr.node `boo 10 (ParserDescr.binary `andthen (ParserDescr.symbol "[|") (ParserDescr.binary `andthen (ParserDescr.cat `term 0) (ParserDescr.symbol "|]"))) @[term_parser] def boo2 : ParserDescr := ParserDescr.node `boo2 10 (ParserDescr.parser `tst2) open Lean.Elab.Term @[term_elab tst] def elabTst : TermElab := adaptExpander $ fun stx => match stx with | `((| $e |)) => pure e | _ => throwUnsupportedSyntax @[term_elab boo] def elabBoo : TermElab := fun stx expected? => elabTerm (stx.getArg 1) expected? @[term_elab boo2] def elabBool2 : TermElab := adaptExpander $ fun stx => match stx with | `((|| $e ||)) => `($e + 1) | _ => throwUnsupportedSyntax #eval runCore "#check [| @id.{1} Nat |]" #eval runCore "#check (| id 1 |)" #eval runCore "#check (|| id 1 ||)" -- #eval run "#check (| id 1, id 1 |)" -- it will fail @[term_elab tst] def elabTst2 : TermElab := adaptExpander $ fun stx => match stx with | `((| $e1, $e2 |)) => `(($e1, $e2)) | _ => throwUnsupportedSyntax -- Now both work #eval runCore "#check (| id 1 |)" #eval runCore "#check (| id 1, id 2 |)" declare_syntax_cat foo syntax "⟨|" term "|⟩" : foo syntax term : foo syntax term ">>>" term : foo syntax (name := tst3) "FOO " foo : term macro_rules | `(FOO ⟨| $t |⟩) => `($t+1) | `(FOO $t:term) => `($t) #check FOO ⟨| id 1 |⟩ #check FOO 1 #check FOO 1 >>> 2
76273b3e87ce32e6b0340e67d9e99b0e8c6d3ac3
9028d228ac200bbefe3a711342514dd4e4458bff
/src/topology/metric_space/isometry.lean
1a016be9e4b6c82b0173c113e4780493a411bbe5
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,690
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Isometries of emetric and metric spaces Authors: Sébastien Gouëzel -/ import topology.bounded_continuous_function import topology.compacts /-! # Isometries We define isometries, i.e., maps between emetric spaces that preserve the edistance (on metric spaces, these are exactly the maps that preserve distances), and prove their basic properties. We also introduce isometric bijections. -/ noncomputable theory universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function set /-- An isometry (also known as isometric embedding) is a map preserving the edistance between emetric spaces, or equivalently the distance between metric space. -/ def isometry [emetric_space α] [emetric_space β] (f : α → β) : Prop := ∀x1 x2 : α, edist (f x1) (f x2) = edist x1 x2 /-- On metric spaces, a map is an isometry if and only if it preserves distances. -/ lemma isometry_emetric_iff_metric [metric_space α] [metric_space β] {f : α → β} : isometry f ↔ (∀x y, dist (f x) (f y) = dist x y) := ⟨assume H x y, by simp [dist_edist, H x y], assume H x y, by simp [edist_dist, H x y]⟩ /-- An isometry preserves edistances. -/ theorem isometry.edist_eq [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) (x y : α) : edist (f x) (f y) = edist x y := hf x y /-- An isometry preserves distances. -/ theorem isometry.dist_eq [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (x y : α) : dist (f x) (f y) = dist x y := by rw [dist_edist, dist_edist, hf] section emetric_isometry variables [emetric_space α] [emetric_space β] [emetric_space γ] variables {f : α → β} {x y z : α} {s : set α} lemma isometry.lipschitz (h : isometry f) : lipschitz_with 1 f := lipschitz_with.of_edist_le $ λ x y, le_of_eq (h x y) lemma isometry.antilipschitz (h : isometry f) : antilipschitz_with 1 f := λ x y, by simp only [h x y, ennreal.coe_one, one_mul, le_refl] /-- An isometry is injective -/ lemma isometry.injective (h : isometry f) : injective f := h.antilipschitz.injective /-- Any map on a subsingleton is an isometry -/ theorem isometry_subsingleton [subsingleton α] : isometry f := λx y, by rw subsingleton.elim x y; simp /-- The identity is an isometry -/ lemma isometry_id : isometry (id : α → α) := λx y, rfl /-- The composition of isometries is an isometry -/ theorem isometry.comp {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) : isometry (g ∘ f) := assume x y, calc edist ((g ∘ f) x) ((g ∘ f) y) = edist (f x) (f y) : hg _ _ ... = edist x y : hf _ _ /-- An isometry is an embedding -/ theorem isometry.uniform_embedding (hf : isometry f) : uniform_embedding f := hf.antilipschitz.uniform_embedding hf.lipschitz.uniform_continuous /-- An isometry is continuous. -/ lemma isometry.continuous (hf : isometry f) : continuous f := hf.lipschitz.continuous /-- The right inverse of an isometry is an isometry. -/ lemma isometry.right_inv {f : α → β} {g : β → α} (h : isometry f) (hg : right_inverse g f) : isometry g := λ x y, by rw [← h, hg _, hg _] /-- Isometries preserve the diameter in emetric spaces. -/ lemma isometry.ediam_image (hf : isometry f) (s : set α) : emetric.diam (f '' s) = emetric.diam s := eq_of_forall_ge_iff $ λ d, by simp only [emetric.diam_le_iff_forall_edist_le, ball_image_iff, hf.edist_eq] lemma isometry.ediam_range (hf : isometry f) : emetric.diam (range f) = emetric.diam (univ : set α) := by { rw ← image_univ, exact hf.ediam_image univ } /-- The injection from a subtype is an isometry -/ lemma isometry_subtype_coe {s : set α} : isometry (coe : s → α) := λx y, rfl end emetric_isometry --section /-- An isometry preserves the diameter in metric spaces. -/ lemma isometry.diam_image [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s := by rw [metric.diam, metric.diam, hf.ediam_image] lemma isometry.diam_range [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) : metric.diam (range f) = metric.diam (univ : set α) := by { rw ← image_univ, exact hf.diam_image univ } /-- `α` and `β` are isometric if there is an isometric bijection between them. -/ @[nolint has_inhabited_instance] -- such a bijection need not exist structure isometric (α : Type*) (β : Type*) [emetric_space α] [emetric_space β] extends α ≃ β := (isometry_to_fun : isometry to_fun) infix ` ≃ᵢ `:25 := isometric namespace isometric variables [emetric_space α] [emetric_space β] [emetric_space γ] instance : has_coe_to_fun (α ≃ᵢ β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : α ≃ᵢ β) (a : α) : h a = h.to_equiv a := rfl protected lemma isometry (h : α ≃ᵢ β) : isometry h := h.isometry_to_fun protected lemma edist_eq (h : α ≃ᵢ β) (x y : α) : edist (h x) (h y) = edist x y := h.isometry.edist_eq x y protected lemma dist_eq {α β : Type*} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (x y : α) : dist (h x) (h y) = dist x y := h.isometry.dist_eq x y protected lemma continuous (h : α ≃ᵢ β) : continuous h := h.isometry.continuous lemma to_equiv_inj : ∀ ⦃h₁ h₂ : α ≃ᵢ β⦄, (h₁.to_equiv = h₂.to_equiv) → h₁ = h₂ | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ H := by { dsimp at H, subst e₁ } @[ext] lemma ext ⦃h₁ h₂ : α ≃ᵢ β⦄ (H : ∀ x, h₁ x = h₂ x) : h₁ = h₂ := to_equiv_inj $ equiv.ext H /-- Alternative constructor for isometric bijections, taking as input an isometry, and a right inverse. -/ def mk' (f : α → β) (g : β → α) (hfg : ∀ x, f (g x) = x) (hf : isometry f) : α ≃ᵢ β := { to_fun := f, inv_fun := g, left_inv := λ x, hf.injective $ hfg _, right_inv := hfg, isometry_to_fun := hf } /-- The identity isometry of a space. -/ protected def refl (α : Type*) [emetric_space α] : α ≃ᵢ α := { isometry_to_fun := isometry_id, .. equiv.refl α } /-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/ protected def trans (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ := { isometry_to_fun := h₂.isometry_to_fun.comp h₁.isometry_to_fun, .. equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : α) : h₁.trans h₂ x = h₂ (h₁ x) := rfl /-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/ protected def symm (h : α ≃ᵢ β) : β ≃ᵢ α := { isometry_to_fun := h.isometry.right_inv h.right_inv, to_equiv := h.to_equiv.symm } @[simp] lemma symm_symm (h : α ≃ᵢ β) : h.symm.symm = h := to_equiv_inj h.to_equiv.symm_symm @[simp] lemma apply_symm_apply (h : α ≃ᵢ β) (y : β) : h (h.symm y) = y := h.to_equiv.apply_symm_apply y @[simp] lemma symm_apply_apply (h : α ≃ᵢ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x lemma symm_apply_eq (h : α ≃ᵢ β) {x : α} {y : β} : h.symm y = x ↔ y = h x := h.to_equiv.symm_apply_eq lemma eq_symm_apply (h : α ≃ᵢ β) {x : α} {y : β} : x = h.symm y ↔ h x = y := h.to_equiv.eq_symm_apply lemma symm_comp_self (h : α ≃ᵢ β) : ⇑h.symm ∘ ⇑h = id := funext $ assume a, h.to_equiv.left_inv a lemma self_comp_symm (h : α ≃ᵢ β) : ⇑h ∘ ⇑h.symm = id := funext $ assume a, h.to_equiv.right_inv a lemma range_coe (h : α ≃ᵢ β) : range h = univ := eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩ lemma image_symm (h : α ≃ᵢ β) : image h.symm = preimage h := image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv lemma preimage_symm (h : α ≃ᵢ β) : preimage h.symm = image h := (image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm @[simp] lemma symm_trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : γ) : (h₁.trans h₂).symm x = h₁.symm (h₂.symm x) := rfl /-- The (bundled) homeomorphism associated to an isometric isomorphism. -/ protected def to_homeomorph (h : α ≃ᵢ β) : α ≃ₜ β := { continuous_to_fun := h.continuous, continuous_inv_fun := h.symm.continuous, .. h } @[simp] lemma coe_to_homeomorph (h : α ≃ᵢ β) : ⇑(h.to_homeomorph) = h := rfl @[simp] lemma to_homeomorph_to_equiv (h : α ≃ᵢ β) : h.to_homeomorph.to_equiv = h.to_equiv := rfl /-- The group of isometries. -/ instance : group (α ≃ᵢ α) := { one := isometric.refl _, mul := λ e₁ e₂, e₁.trans e₂, inv := isometric.symm, mul_assoc := λ e₁ e₂ e₃, rfl, one_mul := λ e, ext $ λ _, rfl, mul_one := λ e, ext $ λ _, rfl, mul_left_inv := λ e, ext e.apply_symm_apply } @[simp] lemma coe_one : ⇑(1 : α ≃ᵢ α) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : α ≃ᵢ α) : ⇑(e₁ * e₂) = e₂ ∘ e₁ := rfl lemma mul_apply (e₁ e₂ : α ≃ᵢ α) (x : α) : (e₁ * e₂) x = e₂ (e₁ x) := rfl @[simp] lemma inv_apply_self (e : α ≃ᵢ α) (x: α) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : α ≃ᵢ α) (x: α) : e (e⁻¹ x) = x := e.apply_symm_apply x section normed_group variables {G : Type*} [normed_group G] /-- Addition `y ↦ y + x` as an `isometry`. -/ protected def add_right (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _, .. equiv.add_right x } @[simp] lemma add_right_to_equiv (x : G) : (isometric.add_right x).to_equiv = equiv.add_right x := rfl @[simp] lemma coe_add_right (x : G) : (isometric.add_right x : G → G) = λ y, y + x := rfl lemma add_right_apply (x y : G) : (isometric.add_right x : G → G) y = y + x := rfl @[simp] lemma add_right_symm (x : G) : (isometric.add_right x).symm = isometric.add_right (-x) := ext $ λ y, rfl /-- Addition `y ↦ x + y` as an `isometry`. -/ protected def add_left (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _, to_equiv := equiv.add_left x } @[simp] lemma add_left_to_equiv (x : G) : (isometric.add_left x).to_equiv = equiv.add_left x := rfl @[simp] lemma coe_add_left (x : G) : ⇑(isometric.add_left x) = (+) x := rfl @[simp] lemma add_left_symm (x : G) : (isometric.add_left x).symm = isometric.add_left (-x) := ext $ λ y, rfl variable (G) /-- Negation `x ↦ -x` as an `isometry`. -/ protected def neg : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _, to_equiv := equiv.neg G } variable {G} @[simp] lemma neg_symm : (isometric.neg G).symm = isometric.neg G := rfl @[simp] lemma neg_to_equiv : (isometric.neg G).to_equiv = equiv.neg G := rfl @[simp] lemma coe_neg : ⇑(isometric.neg G) = has_neg.neg := rfl end normed_group end isometric /-- An isometry induces an isometric isomorphism between the source space and the range of the isometry. -/ def isometry.isometric_on_range [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : α ≃ᵢ range f := { isometry_to_fun := λx y, by simpa [subtype.edist_eq] using h x y, .. equiv.set.range f h.injective } @[simp] lemma isometry.isometric_on_range_apply [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) (x : α) : h.isometric_on_range x = ⟨f x, mem_range_self _⟩ := rfl /-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/ lemma algebra_map_isometry (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : isometry (algebra_map 𝕜 𝕜') := begin refine isometry_emetric_iff_metric.2 (λx y, _), rw [dist_eq_norm, dist_eq_norm, ← ring_hom.map_sub, norm_algebra_map_eq], end /-- The space of bounded sequences, with its sup norm -/ @[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ open bounded_continuous_function metric topological_space namespace Kuratowski_embedding /-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α) /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in the next definition, without density assumptions. -/ def embedding_of_subset : ℓ_infty_ℝ := of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _) lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ lemma embedding_of_subset_dist_le (a b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := begin refine (dist_le dist_nonneg).2 (λn, _), simp only [embedding_of_subset_coe, real.dist_eq], convert abs_dist_sub_le a b (x n) using 2, ring end /-- When the reference set is dense, the embedding map is an isometry on its image. -/ lemma embedding_of_subset_isometry (H : closure (range x) = univ) : isometry (embedding_of_subset x) := begin refine isometry_emetric_iff_metric.2 (λa b, _), refine le_antisymm (embedding_of_subset_dist_le x a b) (real.le_of_forall_epsilon_le (λe epos, _)), /- First step: find n with dist a (x n) < e -/ have A : a ∈ closure (range x), by { have B := mem_univ a, rwa [← H] at B }, rcases metric.mem_closure_range_iff.1 A (e/2) (half_pos epos) with ⟨n, hn⟩, /- Second step: use the norm control at index n to conclude -/ have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n := by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] }, have := calc dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _ ... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring } ... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) : by apply_rules [add_le_add_left, le_abs_self] ... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) : begin rw [C], apply_rules [add_le_add, mul_le_mul_of_nonneg_left, le_of_lt hn, le_refl], norm_num end ... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) : begin rw [← sub_apply], apply add_le_add_left, rw [sub_apply, ←real.dist_eq], apply dist_coe_le_dist end ... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring, simpa [dist_comm] using this end /-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] : ∃(f : α → ℓ_infty_ℝ), isometry f := begin cases (univ : set α).eq_empty_or_nonempty with h h, { use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) }, { /- We construct a map x : ℕ → α with dense image -/ rcases h with ⟨basepoint⟩, haveI : inhabited α := ⟨basepoint⟩, have : ∃s:set α, countable s ∧ closure s = univ := separable_space.exists_countable_closure_eq_univ, rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩, rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩, have : closure (range x) = univ := univ_subset_iff.1 (by { rw [← S_dense], apply closure_mono, assumption }), /- Use embedding_of_subset to construct the desired isometry -/ exact ⟨embedding_of_subset x, embedding_of_subset_isometry x this⟩ } end end Kuratowski_embedding open topological_space Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/ def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ := classical.some (Kuratowski_embedding.exists_isometric_embedding α) /-- The Kuratowski embedding is an isometry -/ protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] : nonempty_compacts ℓ_infty_ℝ := ⟨range (Kuratowski_embedding α), range_nonempty _, compact_range (Kuratowski_embedding.isometry α).continuous⟩
9974b8ce6865ac025c583c3e35f565566b2a6b8d
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/ReducibilityAttrs.lean
a0804bb1b54b5d229fe19cc2a583a7ce6bcab6cb
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,495
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.Attributes namespace Lean inductive ReducibilityStatus | reducible | semireducible | irreducible instance ReducibilityStatus.inhabited : Inhabited ReducibilityStatus := ⟨ReducibilityStatus.semireducible⟩ def mkReducibilityAttrs : IO (EnumAttributes ReducibilityStatus) := registerEnumAttributes `reducibility [(`reducible, "reducible", ReducibilityStatus.reducible), (`semireducible, "semireducible", ReducibilityStatus.semireducible), (`irreducible, "irreducible", ReducibilityStatus.irreducible)] @[init mkReducibilityAttrs] constant reducibilityAttrs : EnumAttributes ReducibilityStatus := arbitrary _ @[export lean_get_reducibility_status] def getReducibilityStatus (env : Environment) (n : Name) : ReducibilityStatus := match reducibilityAttrs.getValue env n with | some s => s | none => ReducibilityStatus.semireducible @[export lean_set_reducibility_status] def setReducibilityStatus (env : Environment) (n : Name) (s : ReducibilityStatus) : Environment := match reducibilityAttrs.setValue env n s with | Except.ok env => env | _ => env -- TODO(Leo): we should extend EnumAttributes.setValue in the future and ensure it never fails def isReducible (env : Environment) (n : Name) : Bool := match getReducibilityStatus env n with | ReducibilityStatus.reducible => true | _ => false end Lean
b144885f63ab28e7834a8c004fa0c98b2addc493
c09f5945267fd905e23a77be83d9a78580e04a4a
/src/topology/uniform_space/cauchy.lean
78fb5c12227ee2b05288ca979261b05b35690759
[ "Apache-2.0" ]
permissive
OHIHIYA20/mathlib
023a6df35355b5b6eb931c404f7dd7535dccfa89
1ec0a1f49db97d45e8666a3bf33217ff79ca1d87
refs/heads/master
1,587,964,529,965
1,551,819,319,000
1,551,819,319,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,173
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 /-- 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 ≤ uniformity def is_complete (s : set α) := ∀f, cauchy f → f ≤ principal s → ∃x∈s, f ≤ nhds x lemma cauchy_iff [uniform_space α] {f : filter α} : cauchy f ↔ (f ≠ ⊥ ∧ (∀s∈(@uniformity α _).sets, ∃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 [uniform_space α] {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (l ≠ ⊥ ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l.prod l) uniformity) := 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) = 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) (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 ... ≤ 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_comap [uniform_space β] {f : filter β} {m : α → β} (hm : comap (λp:α×α, (m p.1, m p.2)) uniformity ≤ uniformity) (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)) uniformity : comap_mono hf.right ... ≤ uniformity : 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 ≤ uniformity := 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_β ≤ uniformity.comap p_α ⊓ uniformity.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 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 ∈ (@uniformity α _).sets, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ (@uniformity α _).sets, ∃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_Union 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} ∈ (@uniformity α _).sets, 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⟩
89ff27750e3d5c30a17c0a8bf6dc8d15103a0d30
379500fc55253ad8be47265634a6014d01a4782d
/src/solutions/friday/manifolds.lean
0272c96d9949fd643e20fbaa1fc50d47eef99de5
[]
no_license
dgvirtz/lftcm2020
b0a2db6ebfd9f14e04b1931116afc77b277b5c2a
3d84c76608cb41d4b59b985e8be2df7d8cd0cb10
refs/heads/master
1,668,684,436,232
1,594,989,081,000
1,594,989,081,000
280,418,622
0
0
null
1,594,988,947,000
1,594,988,946,000
null
UTF-8
Lean
false
false
41,766
lean
import for_mathlib.manifolds noncomputable theory open_locale manifold classical big_operators open set universe u /-! ## Reminder on updating the exercises These instructions are now available at: https://leanprover-community.github.io/lftcm2020/exercises.html To get a new copy of the exercises, run the following commands in your terminal: ``` leanproject get lftcm2020 cp -r lftcm2020/src/exercises_sources/ lftcm2020/src/my_exercises code lftcm2020 ``` To update your exercise files, run the following commands: ``` cd /path/to/lftcm2020 git pull leanproject get-mathlib-cache ``` Don’t forget to copy the updated files to `src/my_exercises`. -/ /-! ## An overview of manifolds in Lean, discussing design decisions Warning: there are sorries in this section, they are not supposed to be filled! The exercises section start later, and there you will have plenty of sorries to fill. What is a manifold? 1) allow field other than `ℝ` or `ℂ`? 2) allow infinite dimension? 3) allow boundary? 4) allow model space depending on the point of the manifold? Bourbaki: 2, 4 (and just definitions and statements, no proofs!) Lean: 1, 2, 3 Perelman geometrization theorem : any compact connected irreducible 3-manifold can be cut along tori into finitely many pieces, each of which has a _geometric structure_ of finite volume, i.e., it is locally like a model space, with changes of coordinates given locally by the action of a Lie group Typical dynamics theorem : let `M` be a compact manifold, and `f : M → M` a map with property such and such. Then ... Or : Consider a hyperbolic surface of genus `g`, and a random geodesic of length `T`. How many times does it typically self-intersect? Manifold in Lean: * charted space structure, i.e., set of local homeos to a model space. This is data, fixed once and for all (and a typeclass) * compatibility condition, i.e., the change of coordinates should belong to some subgroup of the group of local homeos of the model space. This is Prop (and a typeclass). The same manifold can be at the same time an analytic manifold, a smooth manifold and a topological manifold (with the same fixed atlas). * A charted space is a smooth manifold (with corners) if it is compatible with the smooth groupoid on the model space. To cover uniformly both situations with and without boundary, the smooth groupoid is with respect to a map `I : H → E` (think of `H` as the half-space and `E` the full space), which is the identity in the boundaryless situation, the inclusion in the half-space situation. This map `I` is called a _model with corners_. The most standard ones (identity in `ℝ^n` and inclusion of half-space in `ℝ^n`) have dedicated notations: `𝓡 n` and `𝓡∂ n`. -/ #check charted_space (euclidean_half_space 1) (Icc (0 : ℝ) 1) #check has_groupoid (Icc (0 : ℝ) 1) (times_cont_diff_groupoid ∞ (𝓡∂ 1)) #check smooth_manifold_with_corners (𝓡∂ 1) (Icc (0 : ℝ) 1) -- atlases are not maximal in general #check (times_cont_diff_groupoid ∞ (𝓡∂ 1)).maximal_atlas (Icc (0 : ℝ) 1) -- let's try to put a smooth manifold structure on the sphere -- (we don't have submanifolds yet, but it's coming in the near future) @[derive topological_space] definition sphere (n : ℕ) : Type := metric.sphere (0 : euclidean_space (fin (n+1))) 1 instance (n : ℕ) : has_coe (sphere n) (euclidean_space (fin (n+1))) := ⟨subtype.val⟩ instance (n : ℕ) : charted_space (euclidean_space (fin n)) (sphere n) := { atlas := begin sorry end, chart_at := begin sorry end, mem_chart_source := begin sorry end, chart_mem_atlas := begin sorry end } instance (n : ℕ) : smooth_manifold_with_corners (𝓡 n) (sphere n) := { compatible := begin assume e e' he he', sorry end } -- smooth functions def inc (n : ℕ) : sphere n → euclidean_space (fin (n+1)) := λ p : sphere n, (p : euclidean_space (fin (n+1))) lemma inc_smooth (n : ℕ) : times_cont_mdiff (𝓡 n) (𝓡 (n+1)) ∞ (inc n) := begin rw times_cont_mdiff_iff, split, { exact continuous_subtype_coe, }, { assume x y, sorry } end lemma inc_continuous (n : ℕ) : continuous (inc n) := (inc_smooth n).continuous lemma inc_mdifferentiable (n : ℕ) : mdifferentiable (𝓡 n) (𝓡 (n+1)) (inc n) := (inc_smooth n).mdifferentiable le_top -- tangent space and tangent bundles example (n : ℕ) (p : sphere n) (v : tangent_space (𝓡 n) p) : tangent_bundle (𝓡 n) (sphere n) := ⟨p, v⟩ -- tangent map, derivatives example (n : ℕ) : times_cont_mdiff ((𝓡 n).prod (𝓡 n)) ((𝓡 (n+1)).prod (𝓡 (n+1))) ∞ (tangent_map (𝓡 n) (𝓡 (n+1)) (inc n)) := (inc_smooth n).times_cont_mdiff_tangent_map le_top example (n : ℕ) (f : sphere n → sphere (n^2)) (p : sphere n) (v : tangent_space (𝓡 n) p) : mfderiv (𝓡 n) (𝓡 (n^2)) f p v = (tangent_map (𝓡 n) (𝓡 (n^2)) f ⟨p, v⟩).2 := rfl /- Can you express the sphere eversion theorem, i.e., the fact that there is a smooth isotopy of immersions between the canonical embedding of the sphere `S^2` and `ℝ^3`, and the antipodal embedding? Note that we haven't defined immersions in mathlib, but you can jut require that the fiber derivative is injective everywhere, which is easy to express if you know that the derivative of a function `f` from a manifold of dimension `2` to a manifold of dimension `3` at a point `x` is `mfderiv (𝓡 2) (𝓡 3) f x`. Don't forget to require the global smoothness of the map! You may need to know that the interval `[0,1]`, called `Icc (0 : ℝ) 1` in Lean, already has a manifold (with boundary!) structure, where the corresponding model with corners is called `𝓡∂ 1`. -/ theorem sphere_eversion : ∃ f : (Icc (0 : ℝ) 1) × sphere 2 → euclidean_space (fin 3), times_cont_mdiff ((𝓡∂ 1).prod (𝓡 2)) (𝓡 3) ∞ f ∧ ∀ (t : (Icc (0 : ℝ) 1)), ∀ (p : sphere 2), function.injective (mfderiv (𝓡 2) (𝓡 3) (f ∘ λ y, (t, y)) p) ∧ ∀ (p : sphere 2), f (0, p) = p ∧ ∀ (p : sphere 2), f (1, p) = - p := sorry /- Dicussing three (controversial?) design decisions #### Local homeos What is a local homeo `f` between an open subset of `E` and an open subset of `F`? 1) a map defined on a subtype: `f x` only makes sense for `x : f.source` 2) a map defined on the whole space `E`, but taking values in `option F = F ∪ {junk}`, with `f x = junk` when `x ∉ f.source` 3) a map defined on the whole space `E`, taking values in `F`, and we don't care about its values outside of `f.source`. Just like division by zero! But worse: * issue with 1): you keep intersecting chart domains. But the subtype `u ∩ v` is not the same as the subtype `v ∩ u`, so you keep adding casts everywhere * issue with 2): if you want to say that a chart is smooth, then you define to define smooth functions between `option E` and `option F` when `E` and `F` are vector spaces. All notions need to be redefined with `option`. * issue with 3): it works perfectly well, but it makes mathematicians unhappy/uneasy (and it is *not* equivalent to 1) or 2) when one of the spaces is empty) I picked 3) #### Tangent vectors What is a tangent vector (for a manifold `M` modelled on a vector space `E`)? 1) An equivalence class of germs of curves 2) A derivation 3) Physicist point of view: I don't know what a tangent vector is, but I know in charts. Mathematician's interpretation: equivalence class of `(e, v)` where `e` is a chart at `x`, `v` a vector in the vector space, and `(e, v) ∼ (e', v')` if `D(e' ∘ e ⁻¹) v = v'` 4) ... Issues: 1) Pictures are pretty, but this doesn't bring anything compared to 3) when you go down to details. And what about boundaries, where you can only have a half-curve 2) Need partitions of unity to show that this is local and coincides with the usual point of view. Doesn't work well in finite smoothness, nor in complex manifolds 3) Fine, works in all situations, but requires a lot of work to define the equivalence classes, the topology, check that the topology is compatible with the vector space structure, and so on. In a vector space, the tangent space is not defeq to the vector space itself 4) Pick one favorite chart at `x`, say `e_x`, and *define* the tangent space at `x` to be `E`, but "seen" in the chart `e_x` (this will show up in the definition of the derivative : the derivative of `f : M → M'` at `x` is defined to be the derivative of the map `e_{f x} ∘ f ∘ e_x⁻¹`). Works perfectly fine, but makes mathematicians unhappy/uneasy. (Axiom of choice? In fact we put the choice of `e_x` in the *definition* of charted spaces, so not further choice) I picked 4) #### Smooth functions in manifolds with boundary Usual definition of smooth functions in a half space: extend to a smooth function a little bit beyond the boundary, so one only really needs to speak of smooth functions in open subsets of vector spaces. When you define the derivative, you will need to check that it does not depend on the choice of the extension. Even worse when you want to define the tangent bundle: choose an open extension of your manifold with boundary, and then check that the restriction of the tangent bundle does not depend on the choice of the extension. Very easy when handwaving, nightmare to formalize. (What is the extension of the manifold with boundary? Another type?) Instead, if you define derivatives in (non-open) domains, you can talk of smooth functions in domains, and do everything without extending. Need to know this early enough: when starting to define derivatives, you should already think of manifolds with boundaries! That's what we did in mathlib. Difficulty: if a domain `s` is too small (think `s = ℝ ⊆ ℝ^2`), the values of `f` on `s` do not prescribe uniquely a derivative, so `fderiv_within_at ℝ f s x` may behave badly: derivative of a sum might be different from sum of derivatives, as there is an arbitrary choice to be made. This does not happen with the half-space, as it is large enough: derivatives within domains only work well if the tangent directions span the whole space. Predicate `unique_diff_on` for sets in vector spaces. You won't find this in books! -/ /-! ## Exercises -/ /-! ### Local homeomorphisms Local homeomorphisms are globally defined maps with a globally defined "inverse", but the only relevant set is the *source*, which should be mapped homeomorphically to the *target*. -/ /- Define a local homeomorphism from `ℝ` to `ℝ` which is just `x ↦ -x`, but on `(-1, 1)`. In Lean, the interval `(-1, 1)` is denoted by `Ioo (-1 : ℝ) 1` (where `o` stands for _open_). -/ -- set up a simple helper simp lemma to simplify our life later. @[simp] lemma neg_mem_Ioo_minus_one_one (x : ℝ) : -x ∈ Ioo (-1 : ℝ) 1 ↔ x ∈ Ioo (-1 : ℝ) 1 := begin -- sorry simp [neg_lt, and_comm], -- sorry end def my_first_local_homeo : local_homeomorph ℝ ℝ := { to_fun := λ x, -x, inv_fun := λ x, -x, source := Ioo (-1) 1, target := /- inline sorry -/Ioo (-1) 1/- inline sorry -/, map_source' := begin -- sorry assume x hx, simp [hx], -- sorry end, map_target' := begin -- sorry assume x hx, simp [hx], -- sorry end, left_inv' := begin -- sorry simp, -- sorry end, right_inv' := begin -- sorry simp, -- sorry end, open_source := /- inline sorry -/is_open_Ioo/- inline sorry -/, open_target := /- inline sorry -/is_open_Ioo/- inline sorry -/, continuous_to_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/, continuous_inv_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/ } /- Two simple lemmas that will prove useful below. You can leave them sorried if you like. -/ lemma ne_3_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-1 : ℝ) 1) : x ≠ 3 := begin -- sorry exact ne_of_lt (lt_trans h.2 (by norm_num)) -- sorry end lemma neg_ne_3_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-1 : ℝ) 1) : -x ≠ 3 := begin -- sorry assume h', simp at h, linarith, -- sorry end /- Now, define a second local homeomorphism which is almost like the previous one. You may find the following lemma useful for `continuous_to_fun`: -/ #check continuous_on.congr def my_second_local_homeo : local_homeomorph ℝ ℝ := { to_fun := λ x, if x = 3 then 0 else - x, inv_fun := λ x, -x, source := Ioo (-1) 1, target := /- inline sorry -/Ioo (-1) 1/- inline sorry -/, map_source' := /- inline sorry -/λ x hx, by simp [hx, ne_3_of_mem_Ioo hx]/- inline sorry -/, map_target' := /- inline sorry -/λ x hx, by simp [hx]/- inline sorry -/, left_inv' := /- inline sorry -/λ x hx, by simp [hx, ne_3_of_mem_Ioo hx]/- inline sorry -/, right_inv' := /- inline sorry -/λ x hx, by simp [hx, neg_ne_3_of_mem_Ioo hx]/- inline sorry -/, open_source := /- inline sorry -/is_open_Ioo/- inline sorry -/, open_target := /- inline sorry -/is_open_Ioo/- inline sorry -/, continuous_to_fun := begin -- sorry refine continuous_neg.continuous_on.congr (λ x hx, _), simp [hx, ne_3_of_mem_Ioo hx], -- sorry end, continuous_inv_fun := /- inline sorry -/continuous_neg.continuous_on/- inline sorry -/ } /- Although the two above local homeos are the same for all practical purposes as they coincide where relevant, they are not *equal*: -/ lemma my_first_local_homeo_ne_my_second_local_homeo : my_first_local_homeo ≠ my_second_local_homeo := begin -- sorry assume h, have : my_first_local_homeo 3 = my_second_local_homeo 3, by rw h, simp [my_first_local_homeo, my_second_local_homeo] at this, linarith, -- sorry end /- The right equivalence relation for local homeos is not equality, but `eq_on_source`. Indeed, the two local homeos we have defined above coincide from this point of view. -/ #check local_homeomorph.eq_on_source lemma eq_on_source_my_first_local_homeo_my_second_local_homeo : local_homeomorph.eq_on_source my_first_local_homeo my_second_local_homeo := begin -- sorry refine ⟨rfl, λ x hx, _⟩, simp [my_first_local_homeo, my_second_local_homeo, ne_3_of_mem_Ioo hx], -- sorry end /-! ### An example of a charted space structure on `ℝ` A charted space is a topological space together with a set of local homeomorphisms to a model space, whose sources cover the whole space. For instance, `ℝ` is already endowed with a charted space structure with model space `ℝ`, where the unique chart is the identity: -/ #check charted_space_self ℝ /- For educational purposes only, we will put another charted space structure on `ℝ` using the local homeomorphisms we have constructed above. To avoid using too much structure of `ℝ` (and to avoid confusing Lean), we will work with a copy of `ℝ`, on which we will only register the topology. -/ @[derive topological_space] def myℝ : Type := ℝ instance : charted_space ℝ myℝ := { atlas := { local_homeomorph.refl ℝ, my_first_local_homeo }, chart_at := λ x, if x ∈ Ioo (-1 : ℝ) 1 then my_first_local_homeo else local_homeomorph.refl ℝ, mem_chart_source := begin -- sorry assume x, split_ifs, { exact h }, { exact mem_univ _ } -- sorry end, chart_mem_atlas := begin -- sorry assume x, split_ifs; simp, -- sorry end } /- Now come more interesting bits. We have endowed `myℝ` with a charted space structure, with charts taking values in `ℝ`. We want to say that this is a smooth structure, i.e., the changes of coordinates are smooth. In Lean, this is written with `has_groupoid`. A groupoid is a set of local homeomorphisms of the model space (for example, local homeos that are smooth on their domain). A charted space admits the groupoid as a structure groupoid if all the changes of coordinates belong to the groupoid. There is a difficulty that the definitions are set up to be able to also speak of smooth manifolds with boundary or with corners, so the name of the smooth groupoid on `ℝ` has the slightly strange name `times_cont_diff_groupoid ∞ (model_with_corners_self ℝ ℝ)`. To avoid typing again and again `model_with_corners_self ℝ ℝ`, let us introduce a shortcut -/ abbreviation 𝓡1 := model_with_corners_self ℝ ℝ /- In the library, there are such shortcuts for manifolds modelled on `ℝ^n`, denoted with `𝓡 n`, but for `n = 1` this does not coincide with the above one, as `ℝ^1` (a.k.a. `fin 1 → ℝ`) is not the same as `ℝ`! Still, since they are of the same nature, the notation we have just introduced is very close, compare `𝓡1` with `𝓡 1` (and try not to get confused): -/ instance : has_groupoid myℝ (times_cont_diff_groupoid ∞ 𝓡1) := begin -- in theory, we should prove that all compositions of charts are diffeos, i.e., they are smooth -- and their inverse are smooth. For symmetry reasons, it suffices to check one direction apply has_groupoid_of_pregroupoid, -- take two charts `e` and `e'` assume e e' he he', -- if next line is a little bit slow for your taste, you can replace `simp` with `squeeze_simp` -- and then follow the advice simp [atlas] at he he', dsimp, -- to continue, some hints: -- (1) don't hesitate to use the fact that the restriction of a smooth function to a -- subset is still smooth there (`times_cont_diff.times_cont_diff_on`) -- (2) hopefully, there is a theorem saying that the negation function is smooth. -- you can either try to guess its name, or hope that `suggest` will help you there. -- sorry rcases he with rfl|rfl; rcases he' with rfl|rfl, { exact times_cont_diff_id.times_cont_diff_on }, { exact times_cont_diff_id.neg.times_cont_diff_on }, { exact times_cont_diff_id.neg.times_cont_diff_on }, { convert times_cont_diff_id.times_cont_diff_on, ext x, simp [my_first_local_homeo], }, -- sorry end /- The statement of the previous instance is not very readable. There is a shortcut notation: -/ instance : smooth_manifold_with_corners 𝓡1 myℝ := {} /- We will now study a very simple map from `myℝ` to `ℝ`, the identity. -/ def my_map : myℝ → ℝ := λ x, x /- The map `my_map` is a map going from the type `myℝ` to the type `ℝ`. From the point of view of the kernel of Lean, it is just the identity, but from the point of view of structures on `myℝ` and `ℝ` it might not be trivial, as we have registered different instances on these two types. -/ /- The continuity should be trivial, as the topologies on `myℝ` and `ℝ` are definitionally the same. So `continuous_id` might help. -/ lemma continuous_my_map : continuous my_map := -- sorry continuous_id -- sorry /- Smoothness should not be obvious, though, as the manifold structures are not the same: the atlas on `myℝ` has two elements, while the atlas on `ℝ` has one single element. Note that `myℝ` is not a vector space, nor a normed space, so one can not ask whether `my_map` is smooth in the usual sense (as a map between vector spaces): -/ -- lemma times_cont_diff_my_map : times_cont_diff ℝ ∞ my_map := sorry /- does not make sense (try uncommenting it!) However, we can ask whether `my_map` is a smooth map between manifolds, i.e., whether it is smooth when read in the charts. When we mention the smoothness of a map, we should always specify explicitly the model with corners we are using, because there might be several around (think of a complex manifold that you may want to consider as a real manifold, to talk about functions which are real-smooth but not holomorphic) -/ lemma times_cont_mdiff_my_map : times_cont_mdiff 𝓡1 𝓡1 ∞ my_map := begin -- put things in a nicer form. The simpset `mfld_simps` registers many simplification rules for -- manifolds. `simp` is used heavily in manifold files to bring everything into manageable form. rw times_cont_mdiff_iff, simp only [continuous_my_map] with mfld_simps, -- simp has erased the chart in the target, as it knows that the only chart in the manifold `ℝ` -- is the identity. assume x y, -- sorry simp [my_map, (∘), chart_at], split_ifs, { exact times_cont_diff_id.neg.times_cont_diff_on }, { exact times_cont_diff_id.times_cont_diff_on }, -- sorry end /- Now, let's go to tangent bundles. We have a smooth manifold, so its tangent bundle should also be a smooth manifold. -/ -- the type `tangent_bundle I myℝ` makes sense #check tangent_bundle 𝓡1 myℝ /- The tangent space above a point of `myℝ` is just a one-dimensional vector space (identified with `ℝ`). So, one can prescribe an element of the tangent bundle as a pair (more on this below) -/ example : tangent_bundle 𝓡1 myℝ := ((4 : ℝ), 0) /- Construct the smooth manifold structure on the tangent bundle. Hint: the answer is a one-liner, and this instance is not really needed. -/ instance tangent_bundle_myℝ : smooth_manifold_with_corners (𝓡1.prod 𝓡1) (tangent_bundle 𝓡1 myℝ) := -- sorry by apply_instance -- sorry /- NB: the model space for the tangent bundle to a product manifold or a tangent space is not `ℝ × ℝ`, but a copy called `model_prod ℝ ℝ`. Otherwise, `ℝ × ℝ` would have two charted space structures with model `ℝ × ℝ`, the identity one and the product one, which are not definitionally equal. And this would be bad. -/ #check tangent_bundle.charted_space 𝓡1 myℝ /- A smooth map between manifolds induces a map between their tangent bundles. In `mathlib` this is called the `tangent_map` (you might instead know it as the "differential" or "pushforward" of the map). Let us check that the `tangent_map` of `my_map` is smooth. -/ lemma times_cont_mdiff_tangent_map_my_map : times_cont_mdiff (𝓡1.prod 𝓡1) (𝓡1.prod 𝓡1) ∞ (tangent_map 𝓡1 𝓡1 my_map) := begin -- hopefully, there is a theorem providing the general result, i.e. the tangent map to a smooth -- map is smooth. -- you can either try to guess its name, or hope that `suggest` will help you there. -- sorry exact times_cont_mdiff_my_map.times_cont_mdiff_tangent_map le_top, -- sorry end /- (Harder question) Can you show that this tangent bundle is homeomorphic to `ℝ × ℝ`? You could try to build the homeomorphism by hand, using `tangent_map I I my_map` in one direction and a similar map in the other direction, but it is probably more efficient to use one of the charts of the tangent bundle. Remember, the model space for `tangent_bundle I myℝ` is `model_prod ℝ ℝ`, not `ℝ × ℝ`. But the topologies on `model_prod ℝ ℝ` and `ℝ × ℝ` are the same, so it is by definition good enough to construct a homeomorphism with `model_prod ℝ ℝ`. -/ def my_homeo : tangent_bundle 𝓡1 myℝ ≃ₜ (ℝ × ℝ) := begin -- sorry let p : tangent_bundle 𝓡1 myℝ := ((4 : ℝ), 0), let F := chart_at (model_prod ℝ ℝ) p, have A : ¬ ((4 : ℝ) < 1), by norm_num, have S : F.source = univ, by simp [F, chart_at, A, @local_homeomorph.refl_source ℝ _], have T : F.target = univ, by simp [F, chart_at, A, @local_homeomorph.refl_target ℝ _], exact F.to_homeomorph_of_source_eq_univ_target_eq_univ S T, -- sorry end /- Up to now, we have never used the definition of the tangent bundle, and this corresponds to the usual mathematical practice: one doesn't care if the tangent space is defined using germs of curves, or spaces of derivations, or whatever equivalent definition. Instead, one relies all the time on functoriality (i.e., a smooth map has a well defined derivative, and they compose well, together with the fact that the tangent bundle to a vector space is the product). If you want to know more about the internals of the tangent bundle in mathlib, you can browse through the next section, but it is maybe wiser to skip it on first reading, as it is not needed to use the library -/ section you_should_probably_skip_this /- If `M` is a manifold modelled on a vector space `E`, then the underlying type for the tangent bundle is just `M × E` -/ lemma tangent_bundle_myℝ_is_prod : tangent_bundle 𝓡1 myℝ = (myℝ × ℝ) := /- inline sorry -/rfl/- inline sorry -/ /- This means that you can specify a point in the tangent bundle as a pair `(x, y)`. However, in general, a tangent bundle is not trivial: the topology on `tangent_bundle I myℝ` is *not* the product topology. Instead, the tangent space at a point `x` is identified with `ℝ` through some preferred chart at `x`, called `chart_at ℝ x`, but the way they are glued together depends on the manifold and the charts. In vector spaces, the tangent space is canonically the product space, with the same topology, as there is only one chart so there is no strange gluing at play. The equality of the topologies is given in `tangent_bundle_model_space_topology_eq_prod`, but they are not definitionally equal so one can get strange behavior if abusing identifications. Let us register the identification explicitly, as a homeomorphism: -/ def tangent_bundle_vector_space_triv (E : Type u) [normed_group E] [normed_space ℝ E] : tangent_bundle (model_with_corners_self ℝ E) E ≃ₜ E × E := { to_fun := id, inv_fun := id, left_inv := /- inline sorry -/λ x, rfl/- inline sorry -/, right_inv := /- inline sorry -/λ x, rfl/- inline sorry -/, continuous_to_fun := begin -- if you think that `continuous_id` should work but `exact continuous_id` fails, you -- can try `convert continuous_id`: it might show you what doesn't match and let you -- fix it afterwards. -- sorry convert continuous_id, exact (tangent_bundle_model_space_topology_eq_prod _ _).symm -- sorry end, continuous_inv_fun := begin -- sorry convert continuous_id, exact (tangent_bundle_model_space_topology_eq_prod _ _) -- sorry end } /- Even though the tangent bundle to `myℝ` is trivial abstractly, with this construction the tangent bundle is *not* the product space with the product topology, as we have used various charts so the gluing is not trivial. The following exercise unfolds the definition to see what is going on. It is not a reasonable exercise, in the sense that one should never ever do this when working with a manifold! -/ lemma crazy_formula_after_identifications (x : ℝ) (v : ℝ) : let p : tangent_bundle 𝓡1 myℝ := ((3 : ℝ), 0) in chart_at (model_prod ℝ ℝ) p (x, v) = if x ∈ Ioo (-1 : ℝ) 1 then (x, -v) else (x, v) := begin -- this exercise is not easy (and shouldn't be: you are not supposed to use the library like this!) -- if you really want to do this, you should unfold as much as you can using simp and dsimp, until you -- are left with a statement speaking of derivatives of real functions, without any manifold code left. -- sorry have : ¬ ((3 : ℝ) < 1), by norm_num, simp only [chart_at, this, mem_Ioo, if_false, and_false], dsimp [tangent_bundle_core, basic_smooth_bundle_core.chart, topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv', topological_fiber_bundle_core.index_at, basic_smooth_bundle_core.to_topological_fiber_bundle_core], split_ifs, { simp only [chart_at, h, my_first_local_homeo, if_true, fderiv_within_univ, prod.mk.inj_iff, mem_Ioo, fderiv_neg differentiable_at_id', fderiv_id', id.def, continuous_linear_map.coe_id', continuous_linear_map.neg_apply] with mfld_simps }, { simp only [chart_at, h, fderiv_within_univ, mem_Ioo, if_false, @local_homeomorph.refl_symm ℝ, fderiv_id, continuous_linear_map.coe_id'] with mfld_simps } -- sorry end end you_should_probably_skip_this /-! ### The language of manifolds In this paragraph, we will try to write down interesting statements of theorems, without proving them. The goal here is that Lean should not complain on the statement, but the proof should be sorried. -/ /- Here is a first example, already filled up, to show you how diffeomorphisms are currently named (we will probably introduce an abbreviation, but this hasn't been done yet). Don't try to fill the sorried proof! -/ /-- Two zero-dimensional connected manifolds are diffeomorphic. -/ theorem diffeomorph_of_zero_dim_connected (M M' : Type*) [topological_space M] [topological_space M'] [charted_space (euclidean_space (fin 0)) M] [charted_space (euclidean_space (fin 0)) M'] [connected_space M] [connected_space M'] : nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 0)) M M') := sorry /- Do you think that this statement is correct? (note that we have not assumed that our manifolds are smooth, nor that they are separated, but this is maybe automatic in zero dimension). Now, write down a version of this theorem in dimension 1, replacing the first sorry with meaningful content (and adding what is needed before the colon). Don't try to fill the sorried proof! -/ /-- Two one-dimensional smooth compact connected manifolds are diffeomorphic. -/ theorem diffeomorph_of_one_dim_compact_connected -- omit (M M' : Type*) [topological_space M] [topological_space M'] [charted_space (euclidean_space (fin 1)) M] [charted_space (euclidean_space (fin 1)) M'] [connected_space M] [connected_space M'] [compact_space M] [compact_space M'] [t2_space M] [t2_space M'] [smooth_manifold_with_corners (𝓡 1) M] [smooth_manifold_with_corners (𝓡 1) M'] -- omit : -- sorry nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 1)) M M') -- sorry := sorry /- You will definitely need to require smoothness and separation in this case, as it is wrong otherwise. Note that Lean won't complain if you don't put these assumptions, as the theorem would still make sense, but it would just turn out to be wrong. The previous statement is not really satisfactory: we would instead like to express that any such manifold is diffeomorphic to the circle. The trouble is that we don't have the circle as a smooth manifold yet. Since we have cheated and introduced it (with sorries) at the beginning of the tutorial, let's cheat again and use it to reformulate the previous statement. -/ -- the next result is not trivial, leave it sorried (but you can work on it if you don't like -- manifolds and prefer topology -- then please PR it to mathlib!). instance connected_sphere (n : ℕ) : connected_space (sphere (n+1)) := sorry /- The next two instances are easier to prove, you can prove them or leave them sorried as you like. For the second one, you may need to use facts of the library such as -/ #check compact_iff_compact_space #check metric.compact_iff_closed_bounded instance (n : ℕ) : t2_space (sphere n) := begin -- sorry dunfold sphere, apply_instance -- sorry end instance (n : ℕ) : compact_space (sphere n) := begin -- sorry dunfold sphere, apply compact_iff_compact_space.1, rw metric.compact_iff_closed_bounded, split, { exact metric.is_closed_sphere }, { rw metric.bounded_iff_subset_ball (0 : euclidean_space (fin (n+1))), exact ⟨1, metric.sphere_subset_closed_ball⟩ } -- sorry end /- Now, you can prove that any one-dimensional compact connected manifold is diffeomorphic to the circle -/ theorem diffeomorph_circle_of_one_dim_compact_connected (M : Type*) [topological_space M] [charted_space (euclidean_space (fin 1)) M] [connected_space M] [compact_space M] [t2_space M] [smooth_manifold_with_corners (𝓡 1) M] : nonempty (structomorph (times_cont_diff_groupoid ∞ (𝓡 1)) M (sphere 1)) := -- sorry diffeomorph_of_one_dim_compact_connected M (sphere 1) -- sorry /- What about trying to say that there are uncountably many different smooth structures on `ℝ⁴`? (see https://en.wikipedia.org/wiki/Exotic_R4). The library is not really designed with this in mind, as in general we only work with one differentiable structure on a space, but it is perfectly capable of expressing this fact if one uses the `@` version of some definitions. Don't try to fill the sorried proof! -/ theorem exotic_ℝ4 : -- sorry let E := (euclidean_space (fin 4)) in ∃ f : ℝ → charted_space E E, ∀ i, @has_groupoid E _ E _ (f i) (times_cont_diff_groupoid ∞ (𝓡 4)) ∧ ∀ i j, nonempty (@structomorph _ _ (times_cont_diff_groupoid ∞ (𝓡 4)) E E _ _ (f i) (f j)) → i = j -- sorry := sorry /-! ### Smooth functions on `[0, 1]` In this paragraph, you will prove several (math-trivial but Lean-nontrivial) statements on the smooth structure of `[0,1]`. These facts should be Lean-trivial, but they are not (yet) since there is essentially nothing in this direction for now in the library. The goal is as much to be able to write the statements as to prove them. Most of the necessary vocabulary has been introduced above, so don't hesitate to browse the file if you are stuck. Additionally, you will need the notion of a smooth function on a subset: it is `times_cont_diff_on` for functions between vector spaces and `times_cont_mdiff_on` for functions between manifolds. Lemma times_cont_mdiff_g : the inclusion `g` of `[0, 1]` in `ℝ` is smooth. Lemma msmooth_of_smooth : Consider a function `f : ℝ → [0, 1]`, which is smooth in the usual sense as a function from `ℝ` to `ℝ` on a set `s`. Then it is manifold-smooth on `s`. Definition : construct a function `f` from `ℝ` to `[0,1]` which is the identity on `[0, 1]`. Theorem : the tangent bundle to `[0, 1]` is homeomorphic to `[0, 1] × ℝ` Hint for Theorem 4: don't try to unfold the definition of the tangent bundle, it will only get you into trouble. Instead, use the derivatives of the maps `f` and `g`, and rely on functoriality to check that they are inverse to each other. (This advice is slightly misleading as these derivatives do not go between the right spaces, so you will need to massage them a little bit). A global advice: don't hesitate to use and abuse `simp`, it is the main workhorse in this area of mathlib. -/ /- After doing the exercise myself, I realized it was (way!) too hard. So I will give at least the statements of the lemmas, to guide you a little bit more. To let you try the original version if you want, I have left a big blank space to avoid spoilers. -/ def g : Icc (0 : ℝ) 1 → ℝ := subtype.val -- smoothness results for `euclidean_space` are expressed for general `L^p` spaces -- (as `euclidean_space` has the `L^2` norm), in: #check pi_Lp.times_cont_diff_coord #check pi_Lp.times_cont_diff_on_iff_coord lemma times_cont_mdiff_g : times_cont_mdiff (𝓡∂ 1) 𝓡1 ∞ g := begin -- sorry rw times_cont_mdiff_iff, refine ⟨continuous_subtype_val, λ x y, _⟩, by_cases h : (x : ℝ) < 1, { simp only [g, chart_at, h, Icc_left_chart, function.comp, model_with_corners_euclidean_half_space, add_zero, dif_pos, if_true, max_lt_iff, preimage_set_of_eq, sub_zero, subtype.range_coe_subtype, subtype.coe_mk, subtype.val_eq_coe] with mfld_simps, refine (pi_Lp.times_cont_diff_coord 0).times_cont_diff_on.congr (λ x hx, _), simp only [mem_inter_eq, mem_set_of_eq] at hx, simp only [hx, le_of_lt hx.right.left, min_eq_left, max_eq_left] }, { simp only [chart_at, h, Icc_right_chart, function.comp, model_with_corners_euclidean_half_space, dif_pos, max_lt_iff, preimage_set_of_eq, sub_zero, subtype.range_coe_subtype, if_false, subtype.coe_mk, subtype.val_eq_coe, g] with mfld_simps, have : times_cont_diff ℝ ⊤ (λ (x : euclidean_space (fin 1)), 1 - x 0) := times_cont_diff_const.sub (pi_Lp.times_cont_diff_coord 0), apply this.times_cont_diff_on.congr (λ x hx, _), simp only [mem_inter_eq, mem_set_of_eq] at hx, have : 0 ≤ 1 - x 0, by linarith, simp only [hx, this, max_eq_left] } -- sorry end lemma msmooth_of_smooth {f : ℝ → Icc (0 : ℝ) 1} {s : set ℝ} (h : times_cont_diff_on ℝ ∞ (λ x, (f x : ℝ)) s) : times_cont_mdiff_on 𝓡1 (𝓡∂ 1) ∞ f s := begin -- sorry rw times_cont_mdiff_on_iff, split, { have : embedding (subtype.val : Icc (0 : ℝ) 1 → ℝ) := embedding_subtype_coe, exact (embedding.continuous_on_iff this).2 h.continuous_on }, simp only with mfld_simps, assume y, by_cases hy : (y : ℝ) < 1, { simp [chart_at, model_with_corners_euclidean_half_space, (∘), hy, Icc_left_chart, pi_Lp.times_cont_diff_on_iff_coord], apply h.mono (inter_subset_left _ _) }, { simp [chart_at, model_with_corners_euclidean_half_space, (∘), hy, Icc_right_chart, pi_Lp.times_cont_diff_on_iff_coord], assume i, apply (times_cont_diff_on_const.sub h).mono (inter_subset_left _ _) } -- sorry end /- A function from `ℝ` to `[0,1]` which is the identity on `[0,1]`. -/ def f : ℝ → Icc (0 : ℝ) 1 := λ x, ⟨max (min x 1) 0, by simp [le_refl, zero_le_one]⟩ lemma times_cont_mdiff_on_f : times_cont_mdiff_on 𝓡1 (𝓡∂ 1) ∞ f (Icc 0 1) := begin -- sorry apply msmooth_of_smooth, apply times_cont_diff_id.times_cont_diff_on.congr, assume x hx, simp at hx, simp [f, hx], -- sorry end lemma fog : f ∘ g = id := begin -- sorry ext x, rcases x with ⟨x', h'⟩, simp at h', simp [f, g, h'], -- sorry end lemma gof : ∀ x ∈ Icc (0 : ℝ) 1, g (f x) = x := begin -- sorry assume x hx, simp at hx, simp [g, f], simp [hx], -- sorry end def G : tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) → (Icc (0 : ℝ) 1) × ℝ := λ p, (p.1, (tangent_map (𝓡∂ 1) 𝓡1 g p).2) lemma continuous_G : continuous G := begin -- sorry apply continuous.prod_mk (tangent_bundle_proj_continuous _ _), refine continuous_snd.comp _, have Z := times_cont_mdiff_g.continuous_tangent_map le_top, convert Z, exact (tangent_bundle_model_space_topology_eq_prod ℝ 𝓡1).symm -- sorry end /- in the definition of `F`, we use the map `tangent_bundle_vector_space_triv` (which is just the identity pointwise) to make sure that Lean is not lost between the different topologies. -/ def F : (Icc (0 : ℝ) 1) × ℝ → tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) := λ p, tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1) ((tangent_bundle_vector_space_triv ℝ).symm (p.1, p.2)) lemma continuous_F : continuous F := begin -- sorry rw continuous_iff_continuous_on_univ, apply (times_cont_mdiff_on_f.continuous_on_tangent_map_within le_top _).comp, { apply ((tangent_bundle_vector_space_triv ℝ).symm.continuous.comp _).continuous_on, apply (continuous_subtype_coe.comp continuous_fst).prod_mk continuous_snd }, { rintros ⟨⟨x, hx⟩, v⟩ _, simp [tangent_bundle_vector_space_triv], exact hx }, { rw unique_mdiff_on_iff_unique_diff_on, exact unique_diff_on_Icc_zero_one } -- sorry end lemma FoG : F ∘ G = id := begin -- sorry ext1 p, rcases p with ⟨x, v⟩, simp [F, G, tangent_map_within, tangent_bundle_vector_space_triv, f], dsimp, split, { rcases x with ⟨x', h'⟩, simp at h', simp [h'] }, { change (tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1) (tangent_map (𝓡∂ 1) 𝓡1 g (x, v))).snd = v, rw [← tangent_map_within_univ, ← tangent_map_within_comp_at, fog, tangent_map_within_univ, tangent_map_id], { refl }, { apply times_cont_mdiff_on_f.mdifferentiable_on le_top, simpa [g] using x.2 }, { apply (times_cont_mdiff_g.times_cont_mdiff_at.mdifferentiable_at le_top).mdifferentiable_within_at }, { assume z hz, simpa [g] using z.2 }, { apply unique_mdiff_on_univ _ (mem_univ _) } } -- sorry end lemma GoF : G ∘ F = id := begin -- sorry ext1 p, rcases p with ⟨x, v⟩, simp [F, G, tangent_map_within, tangent_bundle_vector_space_triv, f], dsimp, split, { rcases x with ⟨x', h'⟩, simp at h', simp [h'] }, { have A : unique_mdiff_within_at 𝓡1 (Icc 0 1) ((x : ℝ), v).fst, { rw unique_mdiff_within_at_iff_unique_diff_within_at, apply unique_diff_on_Icc_zero_one _ x.2 }, change (tangent_map (𝓡∂ 1) 𝓡1 g (tangent_map_within 𝓡1 (𝓡∂ 1) f (Icc 0 1) (x, v))).snd = v, rw [← tangent_map_within_univ, ← tangent_map_within_comp_at _ _ _ _ A], { have : tangent_map_within 𝓡1 𝓡1 (g ∘ f) (Icc 0 1) (x, v) = tangent_map_within 𝓡1 𝓡1 id (Icc 0 1) (x, v) := tangent_map_within_congr gof _ x.2 A, rw [this, tangent_map_within_id A] }, { apply times_cont_mdiff_g.times_cont_mdiff_on.mdifferentiable_on le_top _ (mem_univ _) }, { apply times_cont_mdiff_on_f.mdifferentiable_on le_top _ x.2 }, { simp only [preimage_univ, subset_univ], } } -- sorry end def my_tangent_homeo : tangent_bundle (𝓡∂ 1) (Icc (0 : ℝ) 1) ≃ₜ (Icc (0 : ℝ) 1) × ℝ := -- sorry { to_fun := G, inv_fun := F, continuous_to_fun := continuous_G, continuous_inv_fun := continuous_F, left_inv := λ p, show (F ∘ G) p = id p, by rw FoG, right_inv := λ p, show (G ∘ F) p = id p, by rw GoF } -- sorry /-! ### Further things to do 1) can you prove `diffeomorph_of_zero_dim_connected` or `connected_sphere`? 2) Try to express and then prove the local inverse theorem in real manifolds: if a map between real manifolds (without boundary, modelled on a complete vector space) is smooth, then it is a local homeomorphism around each point. We already have versions of this statement in mathlib for functions between vector spaces, but this is very much a work in progress. 3) What about trying to prove `diffeomorph_of_one_dim_compact_connected`? (I am not sure mathlib is ready for this, as the proofs I am thinking of are currently a little bit too high-powered. If you manage to do it, you should absolutely PR it!) 4) Why not contribute to the proof of `sphere_eversion`? You can have a look at https://leanprover-community.github.io/sphere-eversion/ to learn more about this project by Patrick Massot. -/
3d28a29aa6ce92915065be7d7bfe7d6cb0b90166
4fa161becb8ce7378a709f5992a594764699e268
/src/ring_theory/coprime.lean
47b4ddbef39928dfe2db42478add2b9f3095a9ac
[ "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
7,576
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Ken Lee, Chris Hughes -/ import algebra.big_operators import data.fintype.basic import data.int.gcd import data.set.disjointed /-! # Coprime elements of a ring ## Main definitions * `is_coprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/ open_locale classical big_operators universes u v variables {R : Type u} [comm_semiring R] (x y z : R) /-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `x₂` are not coprime. -/ @[simp] def is_coprime : Prop := ∃ a b, a * x + b * y = 1 theorem nat.is_coprime_iff_coprime {m n : ℕ} : is_coprime (m : ℤ) n ↔ nat.coprime m n := ⟨λ ⟨a, b, H⟩, nat.eq_one_of_dvd_one $ int.coe_nat_dvd.1 $ by { rw [int.coe_nat_one, ← H], exact dvd_add (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_left m n) _) (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_right m n) _) }, λ H, ⟨nat.gcd_a m n, nat.gcd_b m n, by rw [mul_comm _ (m : ℤ), mul_comm _ (n : ℤ), ← nat.gcd_eq_gcd_ab, show _ = _, from H, int.coe_nat_one]⟩⟩ variables {x y z} theorem is_coprime.symm (H : is_coprime x y) : is_coprime y x := let ⟨a, b, H⟩ := H in ⟨b, a, by rw [add_comm, H]⟩ theorem is_coprime_comm : is_coprime x y ↔ is_coprime y x := ⟨is_coprime.symm, is_coprime.symm⟩ theorem is_coprime_self : is_coprime x x ↔ is_unit x := ⟨λ ⟨a, b, h⟩, is_unit_of_mul_eq_one x (a + b) $ by rwa [mul_comm, add_mul], λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b, 0, by rwa [zero_mul, add_zero]⟩⟩ theorem is_coprime_zero_left : is_coprime 0 x ↔ is_unit x := ⟨λ ⟨a, b, H⟩, is_unit_of_mul_eq_one x b $ by rwa [mul_zero, zero_add, mul_comm] at H, λ H, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 H in ⟨1, b, by rwa [one_mul, zero_add]⟩⟩ theorem is_coprime_zero_right : is_coprime x 0 ↔ is_unit x := is_coprime_comm.trans is_coprime_zero_left theorem is_coprime_one_left : is_coprime 1 x := ⟨1, 0, by rw [one_mul, zero_mul, add_zero]⟩ theorem is_coprime_one_right : is_coprime x 1 := ⟨0, 1, by rw [one_mul, zero_mul, zero_add]⟩ theorem is_coprime.dvd_of_dvd_mul_right (H1 : is_coprime x z) (H2 : x ∣ y * z) : x ∣ y := let ⟨a, b, H⟩ := H1 in by { rw [← mul_one y, ← H, mul_add, ← mul_assoc, mul_left_comm], exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) } theorem is_coprime.dvd_of_dvd_mul_left (H1 : is_coprime x y) (H2 : x ∣ y * z) : x ∣ z := let ⟨a, b, H⟩ := H1 in by { rw [← one_mul z, ← H, add_mul, mul_right_comm, mul_assoc b], exact dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_right H2 _) } theorem is_coprime.mul_left (H1 : is_coprime x z) (H2 : is_coprime y z) : is_coprime (x * y) z := let ⟨a, b, h1⟩ := H1, ⟨c, d, h2⟩ := H2 in ⟨a * c, a * x * d + b * c * y + b * d * z, calc a * c * (x * y) + (a * x * d + b * c * y + b * d * z) * z = (a * x + b * z) * (c * y + d * z) : by ring ... = 1 : by rw [h1, h2, mul_one]⟩ theorem is_coprime.mul_right (H1 : is_coprime x y) (H2 : is_coprime x z) : is_coprime x (y * z) := by { rw is_coprime_comm at H1 H2 ⊢, exact H1.mul_left H2 } variables {I : Type v} {s : I → R} {t : finset I} theorem is_coprime.prod_left : (∀ i ∈ t, is_coprime (s i) x) → is_coprime (∏ i in t, s i) x := finset.induction_on t (λ _, is_coprime_one_left) $ λ b t hbt ih H, by { rw finset.prod_insert hbt, rw finset.forall_mem_insert at H, exact H.1.mul_left (ih H.2) } theorem is_coprime.prod_right : (∀ i ∈ t, is_coprime x (s i)) → is_coprime x (∏ i in t, s i) := by simpa only [is_coprime_comm] using is_coprime.prod_left theorem is_coprime.mul_dvd (H : is_coprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z := begin obtain ⟨a, b, h⟩ := H, rw [← mul_one z, ← h, mul_add], apply dvd_add, { rw [mul_comm z, mul_assoc], exact dvd_mul_of_dvd_right (mul_dvd_mul_left _ H2) _ }, { rw [mul_comm b, ← mul_assoc], exact dvd_mul_of_dvd_left (mul_dvd_mul_right H1 _) _ } end theorem finset.prod_dvd_of_coprime (Hs : pairwise (is_coprime on s)) (Hs1 : ∀ i, s i ∣ z) : ∏ x in t, s x ∣ z := finset.induction_on t (one_dvd z) (λ a r har ih, by { rw finset.prod_insert har, exact (is_coprime.prod_right $ λ i hir, Hs a i $ λ hai, har $ hai.symm ▸ hir).mul_dvd (Hs1 a) ih }) theorem fintype.prod_dvd_of_coprime [fintype I] (Hs : pairwise (is_coprime on s)) (Hs1 : ∀ i, s i ∣ z) : ∏ x, s x ∣ z := finset.prod_dvd_of_coprime Hs Hs1 theorem is_coprime.of_mul_left_left (H : is_coprime (x * y) z) : is_coprime x z := let ⟨a, b, h⟩ := H in ⟨a * y, b, by rwa [mul_right_comm, mul_assoc]⟩ theorem is_coprime.of_mul_left_right (H : is_coprime (x * y) z) : is_coprime y z := by { rw mul_comm at H, exact H.of_mul_left_left } theorem is_coprime.of_mul_right_left (H : is_coprime x (y * z)) : is_coprime x y := by { rw is_coprime_comm at H ⊢, exact H.of_mul_left_left } theorem is_coprime.of_mul_right_right (H : is_coprime x (y * z)) : is_coprime x z := by { rw mul_comm at H, exact H.of_mul_right_left } theorem is_coprime.mul_left_iff : is_coprime (x * y) z ↔ is_coprime x z ∧ is_coprime y z := ⟨λ H, ⟨H.of_mul_left_left, H.of_mul_left_right⟩, λ ⟨H1, H2⟩, H1.mul_left H2⟩ theorem is_coprime.mul_right_iff : is_coprime x (y * z) ↔ is_coprime x y ∧ is_coprime x z := by rw [is_coprime_comm, is_coprime.mul_left_iff, is_coprime_comm, @is_coprime_comm _ _ z] theorem is_coprime.prod_left_iff : is_coprime (∏ i in t, s i) x ↔ ∀ i ∈ t, is_coprime (s i) x := finset.induction_on t (iff_of_true is_coprime_one_left $ λ _, false.elim) $ λ b t hbt ih, by rw [finset.prod_insert hbt, is_coprime.mul_left_iff, ih, finset.forall_mem_insert] theorem is_coprime.prod_right_iff : is_coprime x (∏ i in t, s i) ↔ ∀ i ∈ t, is_coprime x (s i) := by simpa only [is_coprime_comm] using is_coprime.prod_left_iff theorem is_coprime.of_prod_left (H1 : is_coprime (∏ i in t, s i) x) (i : I) (hit : i ∈ t) : is_coprime (s i) x := is_coprime.prod_left_iff.1 H1 i hit theorem is_coprime.of_prod_right (H1 : is_coprime x (∏ i in t, s i)) (i : I) (hit : i ∈ t) : is_coprime x (s i) := is_coprime.prod_right_iff.1 H1 i hit variables {m n : ℕ} theorem is_coprime.pow_left (H : is_coprime x y) : is_coprime (x ^ m) y := by { rw [← finset.card_range m, ← finset.prod_const], exact is_coprime.prod_left (λ _ _, H) } theorem is_coprime.pow_right (H : is_coprime x y) : is_coprime x (y ^ n) := by { rw [← finset.card_range n, ← finset.prod_const], exact is_coprime.prod_right (λ _ _, H) } theorem is_coprime.pow (H : is_coprime x y) : is_coprime (x ^ m) (y ^ n) := H.pow_left.pow_right theorem is_coprime.is_unit_of_dvd (H : is_coprime x y) (d : x ∣ y) : is_unit x := let ⟨k, hk⟩ := d in is_coprime_self.1 $ is_coprime.of_mul_right_left $ show is_coprime x (x * k), from hk ▸ H theorem is_coprime.map (H : is_coprime x y) {S : Type v} [comm_semiring S] (f : R →+* S) : is_coprime (f x) (f y) := let ⟨a, b, h⟩ := H in ⟨f a, f b, by rw [← f.map_mul, ← f.map_mul, ← f.map_add, h, f.map_one]⟩
97b6a0c108079b01ae9e205cf8bb6abeb84959d4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/general_recursion.lean
ca29ed91a7ddfe036c67bbfd1d533dae3b3bab2c
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
10,708
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.norm_num import tactic.linarith import tactic.omega import control.lawful_fix import order.category.omega_complete_partial_order import data.nat.basic universes u_1 u_2 namespace roption.examples open function has_fix omega_complete_partial_order /-! `easy` is a trivial, non-recursive example -/ def easy.intl (easy : ℕ → ℕ → roption ℕ) : ℕ → ℕ → roption ℕ | x y := pure x def easy := fix easy.intl -- automation coming soon theorem easy.cont : continuous' easy.intl := pi.omega_complete_partial_order.flip₂_continuous' easy.intl (λ x, pi.omega_complete_partial_order.flip₂_continuous' _ (λ x_1, const_continuous' (pure x))) -- automation coming soon theorem easy.equations.eqn_1 (x y : ℕ) : easy x y = pure x := by rw [easy, lawful_fix.fix_eq' easy.cont]; refl /-! division on natural numbers -/ def div.intl (div : ℕ → ℕ → roption ℕ) : ℕ → ℕ → roption ℕ | x y := if y ≤ x ∧ y > 0 then div (x - y) y else pure x def div : ℕ → ℕ → roption ℕ := fix div.intl -- automation coming soon theorem div.cont : continuous' div.intl := pi.omega_complete_partial_order.flip₂_continuous' div.intl (λ (x : ℕ), pi.omega_complete_partial_order.flip₂_continuous' (λ (g : ℕ → ℕ → roption ℕ), div.intl g x) (λ (x_1 : ℕ), (continuous_hom.ite_continuous' (λ (x_2 : ℕ → ℕ → roption ℕ), x_2 (x - x_1) x_1) (λ (x_1 : ℕ → ℕ → roption ℕ), pure x) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v_1 : ℕ) (x_2 : ℕ → ℕ → roption ℕ), x_2 (x - x_1) v_1) _ $ pi.omega_complete_partial_order.flip₁_continuous' (λ (v : ℕ) (g : ℕ → ℕ → roption ℕ) (x : ℕ), g v x) _ id_continuous') (const_continuous' (pure x))))) -- automation coming soon theorem div.equations.eqn_1 (x y : ℕ) : div x y = if y ≤ x ∧ y > 0 then div (x - y) y else pure x := by conv_lhs { rw [div, lawful_fix.fix_eq' div.cont] }; refl inductive tree (α : Type*) | nil {} : tree | node (x : α) : tree → tree → tree open roption.examples.tree /-! `map` on a `tree` using monadic notation -/ def tree_map.intl {α β : Type*} (f : α → β) (tree_map : tree α → roption (tree β)) : tree α → roption (tree β) | nil := pure nil | (node x t₀ t₁) := do tt₀ ← tree_map t₀, tt₁ ← tree_map t₁, pure $ node (f x) tt₀ tt₁ -- automation coming soon def tree_map {α : Type u_1} {β : Type u_2} (f : α → β) : tree α → roption (tree β) := fix (tree_map.intl f) -- automation coming soon theorem tree_map.cont : ∀ {α : Type u_1} {β : Type u_2} (f : α → β), continuous' (tree_map.intl f) := λ {α : Type u_1} {β : Type u_2} (f : α → β), pi.omega_complete_partial_order.flip₂_continuous' (tree_map.intl f) (λ (x : tree α), tree.cases_on x (id (const_continuous' (pure nil))) (λ (x_x : α) (x_a x_a_1 : tree α), (continuous_hom.bind_continuous' (λ (x : tree α → roption (tree β)), x x_a) (λ (x : tree α → roption (tree β)) (tt₀ : tree β), x x_a_1 >>= λ (tt₁ : tree β), pure (node (f x_x) tt₀ tt₁)) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → roption (tree β)), x v) x_a id_continuous') (pi.omega_complete_partial_order.flip₂_continuous' (λ (x : tree α → roption (tree β)) (tt₀ : tree β), x x_a_1 >>= λ (tt₁ : tree β), pure (node (f x_x) tt₀ tt₁)) (λ (x : tree β), continuous_hom.bind_continuous' (λ (x : tree α → roption (tree β)), x x_a_1) (λ (x_1 : tree α → roption (tree β)) (tt₁ : tree β), pure (node (f x_x) x tt₁)) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → roption (tree β)), x v) x_a_1 id_continuous') (pi.omega_complete_partial_order.flip₂_continuous' (λ (x_1 : tree α → roption (tree β)) (tt₁ : tree β), pure (node (f x_x) x tt₁)) (λ (x_1 : tree β), const_continuous' (pure (node (f x_x) x x_1))))))))) -- automation coming soon theorem tree_map.equations.eqn_1 {α : Type u_1} {β : Type u_2} (f : α → β) : tree_map f nil = pure nil := by rw [tree_map,lawful_fix.fix_eq' (tree_map.cont f)]; refl -- automation coming soon theorem tree_map.equations.eqn_2 {α : Type u_1} {β : Type u_2} (f : α → β) (x : α) (t₀ t₁ : tree α) : tree_map f (node x t₀ t₁) = tree_map f t₀ >>= λ (tt₀ : tree β), tree_map f t₁ >>= λ (tt₁ : tree β), pure (node (f x) tt₀ tt₁) := by conv_lhs { rw [tree_map,lawful_fix.fix_eq' (tree_map.cont f)] }; refl /-! `map` on a `tree` using applicative notation -/ def tree_map'.intl {α β} (f : α → β) (tree_map : tree α → roption (tree β)) : tree α → roption (tree β) | nil := pure nil | (node x t₀ t₁) := node (f x) <$> tree_map t₀ <*> tree_map t₁ -- automation coming soon def tree_map' {α : Type u_1} {β : Type u_2} (f : α → β) : tree α → roption (tree β) := fix (tree_map'.intl f) -- automation coming soon theorem tree_map'.cont : ∀ {α : Type u_1} {β : Type u_2} (f : α → β), continuous' (tree_map'.intl f) := λ {α : Type u_1} {β : Type u_2} (f : α → β), pi.omega_complete_partial_order.flip₂_continuous' (tree_map'.intl f) (λ (x : tree α), tree.cases_on x (id (const_continuous' (pure nil))) (λ (x_x : α) (x_a x_a_1 : tree α), (continuous_hom.seq_continuous' (λ (x : tree α → roption (tree β)), node (f x_x) <$> x x_a) (λ (x : tree α → roption (tree β)), x x_a_1) (continuous_hom.map_continuous' (node (f x_x)) (λ (x : tree α → roption (tree β)), x x_a) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → roption (tree β)), x v) x_a id_continuous')) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v : tree α) (x : tree α → roption (tree β)), x v) x_a_1 id_continuous')))) -- automation coming soon theorem tree_map'.equations.eqn_1 {α : Type u_1} {β : Type u_2} (f : α → β) : tree_map' f nil = pure nil := by rw [tree_map',lawful_fix.fix_eq' (tree_map'.cont f)]; refl -- automation coming soon theorem tree_map'.equations.eqn_2 {α : Type u_1} {β : Type u_2} (f : α → β) (x : α) (t₀ t₁ : tree α) : tree_map' f (node x t₀ t₁) = node (f x) <$> tree_map' f t₀ <*> tree_map' f t₁ := by conv_lhs { rw [tree_map',lawful_fix.fix_eq' (tree_map'.cont f)] }; refl /-! f91 is a function whose proof of termination cannot rely on the structural ordering of its arguments and does not use the usual well-founded order on natural numbers. It is an interesting candidate to show that `fix` lets us disentangle the issue of termination from the definition of the function. -/ def f91.intl (f91 : ℕ → roption ℕ) (n : ℕ) : roption ℕ := if n > 100 then pure $ n - 10 else f91 (n + 11) >>= f91 -- automation coming soon def f91 : ℕ → roption ℕ := fix f91.intl -- automation coming soon lemma f91.cont : continuous' f91.intl := pi.omega_complete_partial_order.flip₂_continuous' f91.intl (λ (x : ℕ), id (continuous_hom.ite_continuous' (λ (x_1 : ℕ → roption ℕ), pure (x - 10)) (λ (x_1 : ℕ → roption ℕ), x_1 (x + 11) >>= x_1) (const_continuous' (pure (x - 10))) (continuous_hom.bind_continuous' (λ (x_1 : ℕ → roption ℕ), x_1 (x + 11)) (λ (x : ℕ → roption ℕ), x) (pi.omega_complete_partial_order.flip₁_continuous' (λ (v : ℕ) (x : ℕ → roption ℕ), x v) (x + 11) id_continuous') (pi.omega_complete_partial_order.flip₂_continuous' (λ (x : ℕ → roption ℕ), x) (λ (x_1 : ℕ), pi.omega_complete_partial_order.flip₁_continuous' (λ (v : ℕ) (g : ℕ → roption ℕ), g v) x_1 id_continuous'))))) . -- automation coming soon theorem f91.equations.eqn_1 (n : ℕ) : f91 n = ite (n > 100) (pure (n - 10)) (f91 (n + 11) >>= f91) := by conv_lhs { rw [f91, lawful_fix.fix_eq' f91.cont] }; refl lemma f91_spec (n : ℕ) : (∃ n', n < n' + 11 ∧ n' ∈ f91 n) := begin apply well_founded.induction (measure_wf $ λ n, 101 - n) n, clear n, dsimp [measure,inv_image], intros n ih, by_cases h' : n > 100, { rw [roption.examples.f91.equations.eqn_1,if_pos h'], existsi n - 10, rw nat.sub_add_eq_add_sub, norm_num [pure], apply le_of_lt, transitivity 100, norm_num, exact h' }, { rw [roption.examples.f91.equations.eqn_1,if_neg h'], simp, rcases ih (n + 11) _ with ⟨n',hn₀,hn₁⟩, rcases ih (n') _ with ⟨n'',hn'₀,hn'₁⟩, refine ⟨n'',_,_,hn₁,hn'₁⟩, { clear ih hn₁ hn'₁, omega }, { clear ih hn₁, omega }, { clear ih, omega } }, end lemma f91_dom (n : ℕ) : (f91 n).dom := by rw roption.dom_iff_mem; apply exists_imp_exists _ (f91_spec n); simp def f91' (n : ℕ) : ℕ := (f91 n).get (f91_dom n) run_cmd guard (f91' 109 = 99) lemma f91_spec' (n : ℕ) : f91' n = if n > 100 then n - 10 else 91 := begin suffices : (∃ n', n' ∈ f91 n ∧ n' = if n > 100 then n - 10 else 91), { dsimp [f91'], rw roption.get_eq_of_mem, rcases this with ⟨n,_,_⟩, subst n, assumption }, apply well_founded.induction (measure_wf $ λ n, 101 - n) n, clear n, dsimp [measure,inv_image], intros n ih, by_cases h' : n > 100, { rw [roption.examples.f91.equations.eqn_1,if_pos h',if_pos h'], simp [pure] }, { rw [roption.examples.f91.equations.eqn_1,if_neg h',if_neg h'], simp, rcases ih (n + 11) _ with ⟨n',hn'₀,hn'₁⟩, split_ifs at hn'₁, { subst hn'₁, norm_num at hn'₀, refine ⟨_,hn'₀,_⟩, rcases ih (n+1) _ with ⟨n',hn'₀,hn'₁⟩, split_ifs at hn'₁, { subst n', convert hn'₀, clear hn'₀ hn'₀ ih, omega }, { subst n', exact hn'₀ }, { clear ih hn'₀, omega } }, { refine ⟨_,hn'₀,_⟩, subst n', rcases ih 91 _ with ⟨n',hn'₀,hn'₁⟩, rw if_neg at hn'₁, subst n', exact hn'₀, { clear ih hn'₀ hn'₀, omega, }, { clear ih hn'₀, omega, } }, { clear ih, omega } } end end roption.examples
600856b55a6c05552babe33140468a36103e166c
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/macro_macro.lean
0d2758242c76aa62a295c86822407cf9ba1d8ea4
[ "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
313
lean
macro "mk_m " id:ident v:str n:num c:char : command => let tk : Lean.Syntax := Lean.Syntax.mkStrLit id.getId.toString `(macro $tk:strLit : term => `(fun (x : String) => x ++ $v ++ toString $n ++ toString $c)) #print " ---- " mk_m foo "bla" 10 'a' mk_m boo "hello" 3 'b' #check foo "world" #check boo "boo"
53792219e5bbb31a0b41bf7fb199fca4c88c0d0e
ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f
/qp/p0_stdlib/c4_algebra.lean
c75018e9ad0d3c83dc025cdb9390d539856e2897
[]
no_license
intoverflow/qvr
34b9ef23604738381ca20b7d622fd0399d88f2dd
0cfcd33fe4bf8d93851a00cec5bfd21e77105d74
refs/heads/master
1,616,591,570,371
1,492,575,772,000
1,492,575,772,000
80,061,627
0
0
null
null
null
null
UTF-8
Lean
false
false
6,727
lean
/- ----------------------------------------------------------------------- Facts about algebraic structures. ----------------------------------------------------------------------- -/ namespace qp namespace stdaux universe variables ℓ₁ ℓ₂ ℓ₃ /-! #brief A semigroup homomorphism. -/ structure semigroup.hom {A : Type ℓ₁} [A_semigroup : semigroup A] {B : Type ℓ₂} [B_semigroup : semigroup B] (f : A → B) : Prop := (dist : ∀ (a₁ a₂ : A), f (a₁ * a₂) = f a₁ * f a₂) /-! #brief A commutative semigroup homomorphism. -/ structure comm_semigroup.hom {A : Type ℓ₁} [A_comm_semigroup : comm_semigroup A] {B : Type ℓ₂} [B_comm_semigroup : comm_semigroup B] (f : A → B) extends semigroup.hom f : Prop /-! #brief Semigroup homomorphisms are closed under composition. -/ definition semigroup.hom_comp {A : Type ℓ₁} [A_semigroup : semigroup A] {B : Type ℓ₂} [B_semigroup : semigroup B] {C : Type ℓ₃} [C_semigroup : semigroup C] {g : B → C} (g_hom : semigroup.hom g) {f : A → B} (f_hom : semigroup.hom f) : semigroup.hom (function.comp g f) := { dist := λ a₁ a₂, by rw [-g_hom^.dist, -f_hom^.dist] } /-! #brief The identity function is a semigroup homomorphism. -/ definition semigroup.hom_id (A : Type ℓ₁) [A_semigroup : semigroup A] : semigroup.hom (@id A) := { dist := λ a₁ a₂, rfl } /-! #brief A monoid homomorphism. -/ structure monoid.hom {A : Type ℓ₁} [A_monoid : monoid A] {B : Type ℓ₂} [B_monoid : monoid B] (f : A → B) extends semigroup.hom f : Prop := (id : f 1 = 1) /-! #brief A commutative monoid homomorphism. -/ structure comm_monoid.hom {A : Type ℓ₁} [A_comm_monoid : comm_monoid A] {B : Type ℓ₂} [B_comm_monoid : comm_monoid B] (f : A → B) extends monoid.hom f : Prop /-! #brief Monoid homomorphisms are closed under composition. -/ definition monoid.hom_comp {A : Type ℓ₁} [A_monoid : monoid A] {B : Type ℓ₂} [B_monoid : monoid B] {C : Type ℓ₃} [C_monoid : monoid C] {g : B → C} (g_hom : monoid.hom g) {f : A → B} (f_hom : monoid.hom f) : monoid.hom (function.comp g f) := { dist := λ a₁ a₂, by rw [-g_hom^.dist, -f_hom^.dist] , id := by rw [-g_hom^.id, -f_hom^.id] } /-! #brief The identity function is a monoid homomorphism. -/ definition monoid.hom_id (A : Type ℓ₁) [A_monoid : monoid A] : monoid.hom (@id A) := { dist := (semigroup.hom_id A)^.dist , id := rfl } /-! #brief A group homomorphism. -/ structure group.hom {A : Type ℓ₁} [A_group : group A] {B : Type ℓ₂} [B_group : group B] (f : A → B) extends semigroup.hom f : Prop /-! #brief A commutative group homomorphism. -/ structure comm_group.hom {A : Type ℓ₁} [A_comm_group : comm_group A] {B : Type ℓ₂} [B_comm_group : comm_group B] (f : A → B) extends group.hom f : Prop /-! #brief Group homomorphisms are closed under composition. -/ definition group.hom_comp {A : Type ℓ₁} [A_group : group A] {B : Type ℓ₂} [B_group : group B] {C : Type ℓ₃} [C_group : group C] {g : B → C} (g_hom : group.hom g) {f : A → B} (f_hom : group.hom f) : group.hom (function.comp g f) := { dist := (semigroup.hom_comp (group.hom.to_hom g_hom) (group.hom.to_hom f_hom))^.dist } /-! #brief The identity function is a group homomorphism. -/ definition group.hom_id (A : Type ℓ₁) [A_group : group A] : group.hom (@id A) := { dist := (semigroup.hom_id A)^.dist } /-! #brief Idempotents in groups are trivial. -/ theorem group.idempotent {A : Type ℓ₁} [A_group : group A] {a : A} (ω : a = a * a) : a = 1 := by calc a = a * 1 : by rw mul_one ... = a * (a * a⁻¹) : by rw mul_right_inv ... = (a * a) * a⁻¹ : by rw mul_assoc ... = a * a⁻¹ : by rw -ω ... = 1 : by rw mul_right_inv /-! #brief Inverses in a group are unique. -/ theorem group.inv_uniq {A : Type ℓ₁} [A_group : group A] {a b : A} (ω : a * b = 1) : b = a⁻¹ := by calc b = 1 * b : by rw one_mul ... = (a⁻¹ * a) * b : by rw mul_left_inv ... = a⁻¹ * (a * b) : by rw mul_assoc ... = a⁻¹ * 1 : by rw ω ... = a⁻¹ : by rw mul_one /-! #brief Group homomorphisms preserve identities. -/ theorem group.hom_one {A : Type ℓ₁} [A_group : group A] {B : Type ℓ₂} [B_group : group B] {f : A → B} (f_hom : group.hom f) : f 1 = 1 := group.idempotent (by calc f 1 = f (1 * 1) : by rw mul_one ... = f 1 * f 1 : by rw f_hom^.dist) /-! #brief Group homomorphisms preserve inverses. -/ theorem group.hom_inv {A : Type ℓ₁} [A_group : group A] {B : Type ℓ₂} [B_group : group B] {f : A → B} (f_hom : group.hom f) (a : A) : f (a⁻¹) = (f a)⁻¹ := group.inv_uniq (by calc f a * f (a⁻¹) = f (a * a⁻¹) : by rw f_hom^.dist ... = f 1 : by rw mul_right_inv ... = 1 : by rw group.hom_one f_hom) /-! #brief Every group homomorphism is a monoid homomorphism. -/ definition group.hom.to_monoid_hom {A : Type ℓ₁} [A_group : group A] {B : Type ℓ₂} [B_group : group B] {f : A → B} (f_hom : group.hom f) : monoid.hom f := { dist := f_hom^.dist , id := group.hom_one f_hom } /-! #brief A homomorphism of commutative rings. -/ structure comm_ring.hom {A : Type ℓ₁} [A_comm_ring : comm_ring A] {B : Type ℓ₂} [B_comm_ring : comm_ring B] (f : A → B) : Prop := (add : ∀ (a₁ a₂ : A), f (a₁ + a₂) = f a₁ + f a₂) (mul : ∀ (a₁ a₂ : A), f (a₁ * a₂) = f a₁ * f a₂) (one : f 1 = 1) /-! #brief The identity function is a commutative ring homomorphism. -/ definition comm_ring.hom_id {A : Type ℓ₁} [A_comm_ring : comm_ring A] : comm_ring.hom (@id A) := { add := λ a₁ a₂, rfl , mul := λ a₁ a₂, rfl , one := rfl } /-! #brief Commutative ring homomorphisms are closed under composition. -/ definition comm_ring.hom_comp {A : Type ℓ₁} [A_comm_ring : comm_ring A] {B : Type ℓ₂} [B_comm_ring : comm_ring B] {C : Type ℓ₃} [C_comm_ring : comm_ring C] {g : B → C} (g_hom : comm_ring.hom g) {f : A → B} (f_hom : comm_ring.hom f) : comm_ring.hom (function.comp g f) := { add := λ a₁ a₂, by rw [-g_hom^.add, -f_hom^.add] , mul := λ a₁ a₂, by rw [-g_hom^.mul, -f_hom^.mul] , one := by rw [-g_hom^.one, -f_hom^.one] } end stdaux end qp
a8ebf2a35ceb8ff1a5b1cf7715b7eba787726770
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/choose/central.lean
36fa04c9d5c6e5793108bc893b70bea154ba549c
[ "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
4,185
lean
/- Copyright (c) 2021 Patrick Stevens. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Stevens, Thomas Browning -/ import data.nat.choose.basic import tactic.norm_num import tactic.linarith /-! # Central binomial coefficients This file proves properties of the central binomial coefficients (that is, `nat.choose (2 * n) n`). ## Main definition and results * `nat.central_binom`: the central binomial coefficient, `(2 * n).choose n`. * `nat.succ_mul_central_binom_succ`: the inductive relationship between successive central binomial coefficients. * `nat.four_pow_lt_mul_central_binom`: an exponential lower bound on the central binomial coefficient. -/ namespace nat /-- The central binomial coefficient, `nat.choose (2 * n) n`. -/ def central_binom (n : ℕ) := (2 * n).choose n lemma central_binom_eq_two_mul_choose (n : ℕ) : central_binom n = (2 * n).choose n := rfl lemma central_binom_pos (n : ℕ) : 0 < central_binom n := choose_pos (nat.le_mul_of_pos_left zero_lt_two) lemma central_binom_ne_zero (n : ℕ) : central_binom n ≠ 0 := (central_binom_pos n).ne' @[simp] lemma central_binom_zero : central_binom 0 = 1 := choose_zero_right _ /-- The central binomial coefficient is the largest binomial coefficient. -/ lemma choose_le_central_binom (r n : ℕ) : choose (2 * n) r ≤ central_binom n := calc (2 * n).choose r ≤ (2 * n).choose (2 * n / 2) : choose_le_middle r (2 * n) ... = (2 * n).choose n : by rw nat.mul_div_cancel_left n zero_lt_two lemma two_le_central_binom (n : ℕ) (n_pos : 0 < n) : 2 ≤ central_binom n := calc 2 ≤ 2 * n : le_mul_of_pos_right n_pos ... = (2 * n).choose 1 : (choose_one_right (2 * n)).symm ... ≤ central_binom n : choose_le_central_binom 1 n /-- An inductive property of the central binomial coefficient. -/ lemma succ_mul_central_binom_succ (n : ℕ) : (n + 1) * central_binom (n + 1) = 2 * (2 * n + 1) * central_binom n := calc (n + 1) * (2 * (n + 1)).choose (n + 1) = (2 * n + 2).choose (n + 1) * (n + 1) : mul_comm _ _ ... = (2 * n + 1).choose n * (2 * n + 2) : by rw [choose_succ_right_eq, choose_mul_succ_eq] ... = 2 * ((2 * n + 1).choose n * (n + 1)) : by ring ... = 2 * ((2 * n + 1).choose n * ((2 * n + 1) - n)) : by rw [two_mul n, add_assoc, nat.add_sub_cancel_left] ... = 2 * ((2 * n).choose n * (2 * n + 1)) : by rw choose_mul_succ_eq ... = (2 * (2 * n + 1)) * (2 * n).choose n : by rw [mul_assoc, mul_comm (2 * n + 1)] /-- An exponential lower bound on the central binomial coefficient. This bound is of interest because it appears in [Tochiori's refinement of Erdős's proof of Bertrand's postulate](https://en.wikipedia.org/w/index.php?title=Proof_of_Bertrand%27s_postulate&oldid=859165151#Proof_by_Shigenori_Tochiori). -/ lemma four_pow_lt_mul_central_binom (n : ℕ) (n_big : 4 ≤ n) : 4 ^ n < n * central_binom n := begin induction n using nat.strong_induction_on with n IH, rcases lt_trichotomy n 4 with (hn|rfl|hn), { clear IH, dec_trivial! }, { norm_num [central_binom, choose] }, obtain ⟨n, rfl⟩ : ∃ m, n = m + 1 := nat.exists_eq_succ_of_ne_zero (zero_lt_four.trans hn).ne', calc 4 ^ (n + 1) < 4 * (n * central_binom n) : (mul_lt_mul_left zero_lt_four).mpr (IH n n.lt_succ_self (nat.le_of_lt_succ hn)) ... ≤ 2 * (2 * n + 1) * central_binom n : by { rw ← mul_assoc, linarith } ... = (n + 1) * central_binom (n + 1) : (succ_mul_central_binom_succ n).symm, end /-- An exponential lower bound on the central binomial coefficient. This bound is weaker than `four_pow_n_lt_n_mul_central_binom`, but it is of historical interest because it appears in Erdős's proof of Bertrand's postulate. -/ lemma four_pow_le_two_mul_self_mul_central_binom : ∀ (n : ℕ) (n_pos : 0 < n), 4 ^ n ≤ (2 * n) * central_binom n | 0 pr := (nat.not_lt_zero _ pr).elim | 1 pr := by norm_num [central_binom, choose] | 2 pr := by norm_num [central_binom, choose] | 3 pr := by norm_num [central_binom, choose] | n@(m + 4) _ := calc 4 ^ n ≤ n * central_binom n : (four_pow_lt_mul_central_binom _ le_add_self).le ... ≤ 2 * n * central_binom n : by { rw [mul_assoc], refine le_mul_of_pos_left zero_lt_two } end nat
3eff549a15ed9bc9a23a4d48e932cae8b43bb1cc
bb31430994044506fa42fd667e2d556327e18dfe
/src/order/game_add.lean
f7207da171cae7f38e783f0caec316b52107010b
[ "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
2,992
lean
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import order.basic import logic.relation /-! # Game addition relation > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines, given relations `rα : α → α → Prop` and `rβ : β → β → Prop`, a relation `prod.game_add` on pairs, such that `game_add rα rβ x y` iff `x` can be reached from `y` by decreasing either entry (with respect to `rα` and `rβ`). It is so called since it models the subsequency relation on the addition of combinatorial games. ## Main definitions and results - `prod.game_add`: the game addition relation on ordered pairs. - `well_founded.prod_game_add`: formalizes induction on ordered pairs, where exactly one entry decreases at a time. ## Todo - Add custom `induction` and `fix` lemmas. - Define `sym2.game_add`. -/ variables {α β : Type*} (rα : α → α → Prop) (rβ : β → β → Prop) namespace prod /-- The "addition of games" relation in combinatorial game theory, on the product type: if `rα a' a` means that `a ⟶ a'` is a valid move in game `α`, and `rβ b' b` means that `b ⟶ b'` is a valid move in game `β`, then `game_add rα rβ` specifies the valid moves in the juxtaposition of `α` and `β`: the player is free to choose one of the games and make a move in it, while leaving the other game unchanged. -/ inductive game_add : α × β → α × β → Prop | fst {a' a b} : rα a' a → game_add (a',b) (a,b) | snd {a b' b} : rβ b' b → game_add (a,b') (a,b) /-- `game_add` is a `subrelation` of `prod.lex`. -/ lemma game_add_le_lex : game_add rα rβ ≤ prod.lex rα rβ := λ _ _ h, h.rec (λ _ _ b, prod.lex.left b b) (λ a _ _, prod.lex.right a) /-- `prod.rprod` is a subrelation of the transitive closure of `game_add`. -/ lemma rprod_le_trans_gen_game_add : prod.rprod rα rβ ≤ relation.trans_gen (game_add rα rβ) := λ _ _ h, h.rec begin intros _ _ _ _ hα hβ, exact relation.trans_gen.tail (relation.trans_gen.single $ game_add.fst hα) (game_add.snd hβ), end end prod variables {rα rβ} /-- If `a` is accessible under `rα` and `b` is accessible under `rβ`, then `(a, b)` is accessible under `prod.game_add rα rβ`. Notice that `prod.lex_accessible` requires the stronger condition `∀ b, acc rβ b`. -/ lemma acc.prod_game_add {a b} (ha : acc rα a) (hb : acc rβ b) : acc (prod.game_add rα rβ) (a, b) := begin induction ha with a ha iha generalizing b, induction hb with b hb ihb, refine acc.intro _ (λ h, _), rintro (⟨ra⟩ | ⟨rb⟩), exacts [iha _ ra (acc.intro b hb), ihb _ rb], end /-- The sum of two well-founded games is well-founded. -/ lemma well_founded.prod_game_add (hα : well_founded rα) (hβ : well_founded rβ) : well_founded (prod.game_add rα rβ) := ⟨λ ⟨a,b⟩, (hα.apply a).prod_game_add (hβ.apply b)⟩
ee33ea8071a47bf5e6c73118e7d6ec40a8756f92
618003631150032a5676f229d13a079ac875ff77
/src/data/list/min_max.lean
eac15f81078f2d9a036a7b963133c8b6659c55b3
[ "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
11,225
lean
/- Copyright (c) 2019 Minchao Wu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Minchao Wu, Chris Hughes -/ import data.list.basic /- # Minimum and maximum of lists ## Main definitions The main definitions are `argmax`, `argmin`, `minimum` and `maximum` for lists. `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ namespace list variables {α : Type*} {β : Type*} [decidable_linear_order β] /-- Auxiliary definition to define `argmax` -/ def argmax₂ (f : α → β) (a : option α) (b : α) : option α := option.cases_on a (some b) (λ c, if f b ≤ f c then some c else some b) /-- `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` -/ def argmax (f : α → β) (l : list α) : option α := l.foldl (argmax₂ f) none /-- `argmin f l` returns `some a`, where `a` of `l` that minimises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmin f []` = none` -/ def argmin (f : α → β) (l : list α) := @argmax _ (order_dual β) _ f l @[simp] lemma argmax_two_self (f : α → β) (a : α) : argmax₂ f (some a) a = a := if_pos (le_refl _) @[simp] lemma argmax_nil (f : α → β) : argmax f [] = none := rfl @[simp] lemma argmin_nil (f : α → β) : argmin f [] = none := rfl @[simp] lemma argmax_singleton {f : α → β} {a : α} : argmax f [a] = some a := rfl @[simp] lemma argmin_singleton {f : α → β} {a : α} : argmin f [a] = a := rfl @[simp] lemma foldl_argmax₂_eq_none {f : α → β} {l : list α} {o : option α} : l.foldl (argmax₂ f) o = none ↔ l = [] ∧ o = none := list.reverse_rec_on l (by simp) $ (assume tl hd, by simp [argmax₂]; cases foldl (argmax₂ f) o tl; simp; try {split_ifs}; simp) private theorem le_of_foldl_argmax₂ {f : α → β} {l} : Π {a m : α} {o : option α}, a ∈ l → m ∈ foldl (argmax₂ f) o l → f a ≤ f m := list.reverse_rec_on l (λ _ _ _ h, absurd h $ not_mem_nil _) begin intros tl _ ih _ _ _ h ho, rw [foldl_append, foldl_cons, foldl_nil, argmax₂] at ho, cases hf : foldl (argmax₂ f) o tl, { rw [hf] at ho, rw [foldl_argmax₂_eq_none] at hf, simp [hf.1, hf.2, *] at * }, rw [hf, option.mem_def] at ho, dsimp only at ho, cases mem_append.1 h with h h, { refine le_trans (ih h hf) _, have := @le_of_lt _ _ (f val) (f m), split_ifs at ho; simp * at * }, { split_ifs at ho; simp * at * } end private theorem foldl_argmax₂_mem (f : α → β) (l) : Π (a m : α), m ∈ foldl (argmax₂ f) (some a) l → m ∈ a :: l := list.reverse_rec_on l (by simp [eq_comm]) begin assume tl hd ih a m, simp only [foldl_append, foldl_cons, foldl_nil, argmax₂], cases hf : foldl (argmax₂ f) (some a) tl, { simp {contextual := tt} }, { dsimp only, split_ifs, { finish [ih _ _ hf] }, { simp {contextual := tt} } } end theorem argmax_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → m ∈ l | [] m := by simp | (hd::tl) m := by simpa [argmax, argmax₂] using foldl_argmax₂_mem f tl hd m theorem argmin_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → m ∈ l := @argmax_mem _ (order_dual β) _ _ @[simp] theorem argmax_eq_none {f : α → β} {l : list α} : l.argmax f = none ↔ l = [] := by simp [argmax] @[simp] theorem argmin_eq_none {f : α → β} {l : list α} : l.argmin f = none ↔ l = [] := @argmax_eq_none _ (order_dual β) _ _ _ theorem le_argmax_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmax f l → f a ≤ f m := le_of_foldl_argmax₂ theorem argmin_le_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmin f l → f m ≤ f a:= @le_argmax_of_mem _ (order_dual β) _ _ _ _ _ theorem argmax_concat (f : α → β) (a : α) (l : list α) : argmax f (l ++ [a]) = option.cases_on (argmax f l) (some a) (λ c, if f a ≤ f c then some c else some a) := by rw [argmax, argmax]; simp [argmax₂] theorem argmin_concat (f : α → β) (a : α) (l : list α) : argmin f (l ++ [a]) = option.cases_on (argmin f l) (some a) (λ c, if f c ≤ f a then some c else some a) := @argmax_concat _ (order_dual β) _ _ _ _ theorem argmax_cons (f : α → β) (a : α) (l : list α) : argmax f (a :: l) = option.cases_on (argmax f l) (some a) (λ c, if f c ≤ f a then some a else some c) := list.reverse_rec_on l rfl $ assume hd tl ih, begin rw [← cons_append, argmax_concat, ih, argmax_concat], cases h : argmax f hd with m, { simp [h] }, { simp [h], dsimp, by_cases ham : f m ≤ f a, { rw if_pos ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw [if_pos (le_trans htlm ham), if_pos ham] }, { rw if_neg htlm } }, { rw if_neg ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw if_neg ham }, { rw if_neg htlm, dsimp, rw [if_neg (not_le_of_gt (lt_trans (lt_of_not_ge ham) (lt_of_not_ge htlm)))] } } } end theorem argmin_cons (f : α → β) (a : α) (l : list α) : argmin f (a :: l) = option.cases_on (argmin f l) (some a) (λ c, if f a ≤ f c then some a else some c) := @argmax_cons _ (order_dual β) _ _ _ _ theorem index_of_argmax [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → ∀ {a}, a ∈ l → f m ≤ f a → l.index_of m ≤ l.index_of a | [] m _ _ _ _ := by simp | (hd::tl) m hm a ha ham := begin simp only [index_of_cons, argmax_cons, option.mem_def] at ⊢ hm, cases h : argmax f tl, { rw h at hm, simp * at * }, { rw h at hm, dsimp only at hm, cases ha with hahd hatl, { clear index_of_argmax, subst hahd, split_ifs at hm, { subst hm }, { subst hm, contradiction } }, { have := index_of_argmax h hatl, clear index_of_argmax, split_ifs at *; refl <|> exact nat.zero_le _ <|> simp [*, nat.succ_le_succ_iff, -not_le] at * } } end theorem index_of_argmin [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → ∀ {a}, a ∈ l → f a ≤ f m → l.index_of m ≤ l.index_of a := @index_of_argmax _ (order_dual β) _ _ _ theorem mem_argmax_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmax f l ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := ⟨λ hm, ⟨argmax_mem hm, λ a ha, le_argmax_of_mem ha hm, λ _, index_of_argmax hm⟩, begin rintros ⟨hml, ham, hma⟩, cases harg : argmax f l with n, { simp * at * }, { have := le_antisymm (hma n (argmax_mem harg) (le_argmax_of_mem hml harg)) (index_of_argmax harg hml (ham _ (argmax_mem harg))), rw [(index_of_inj hml (argmax_mem harg)).1 this, option.mem_def] } end⟩ theorem argmax_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmax f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := mem_argmax_iff theorem mem_argmin_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmin f l ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := @mem_argmax_iff _ (order_dual β) _ _ _ _ _ theorem argmin_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmin f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := mem_argmin_iff variable [decidable_linear_order α] /-- `maximum l` returns an `with_bot α`, the largest element of `l` for nonempty lists, and `⊥` for `[]` -/ def maximum (l : list α) : with_bot α := argmax id l /-- `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ def minimum (l : list α) : with_top α := argmin id l @[simp] lemma maximum_nil : maximum ([] : list α) = ⊥ := rfl @[simp] lemma minimum_nil : minimum ([] : list α) = ⊤ := rfl @[simp] lemma maximum_singleton (a : α) : maximum [a] = a := rfl @[simp] lemma minimum_singleton (a : α) : minimum [a] = a := rfl theorem maximum_mem {l : list α} {m : α} : (maximum l : with_top α) = m → m ∈ l := argmax_mem theorem minimum_mem {l : list α} {m : α} : (minimum l : with_bot α) = m → m ∈ l := argmin_mem @[simp] theorem maximum_eq_none {l : list α} : l.maximum = none ↔ l = [] := argmax_eq_none @[simp] theorem minimum_eq_none {l : list α} : l.minimum = none ↔ l = [] := argmin_eq_none theorem le_maximum_of_mem {a m : α} {l : list α} : a ∈ l → (maximum l : with_bot α) = m → a ≤ m := le_argmax_of_mem theorem minimum_le_of_mem {a m : α} {l : list α} : a ∈ l → (minimum l : with_top α) = m → m ≤ a := argmin_le_of_mem theorem le_maximum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : (a : with_bot α) ≤ maximum l := option.cases_on (maximum l) (λ _ h, absurd ha ((h rfl).symm ▸ not_mem_nil _)) (λ m hm _, with_bot.coe_le_coe.2 $ hm _ rfl) (λ m, @le_maximum_of_mem _ _ _ m _ ha) (@maximum_eq_none _ _ l).1 theorem le_minimum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : minimum l ≤ (a : with_top α) := @le_maximum_of_mem' (order_dual α) _ _ _ ha theorem maximum_concat (a : α) (l : list α) : maximum (l ++ [a]) = max (maximum l) a := begin rw max_comm, simp only [maximum, argmax_concat, id, max], cases h : argmax id l, { rw [if_neg], refl, exact not_le_of_gt (with_bot.bot_lt_some _) }, change (coe : α → with_bot α) with some, simp end theorem minimum_concat (a : α) (l : list α) : minimum (l ++ [a]) = min (minimum l) a := by simp only [min_comm _ (a : with_top α)]; exact @maximum_concat (order_dual α) _ _ _ theorem maximum_cons (a : α) (l : list α) : maximum (a :: l) = max a (maximum l) := list.reverse_rec_on l (by simp [@max_eq_left (with_bot α) _ _ _ bot_le]) (λ tl hd ih, by rw [← cons_append, maximum_concat, ih, maximum_concat, max_assoc]) theorem minimum_cons (a : α) (l : list α) : minimum (a :: l) = min a (minimum l) := min_comm (minimum l) a ▸ @maximum_cons (order_dual α) _ _ _ theorem maximum_eq_coe_iff {m : α} {l : list α} : maximum l = m ↔ m ∈ l ∧ (∀ a ∈ l, a ≤ m) := begin unfold_coes, simp only [maximum, argmax_eq_some_iff, id], split, { simp only [true_and, forall_true_iff] {contextual := tt} }, { simp only [true_and, forall_true_iff] {contextual := tt}, intros h a hal hma, rw [le_antisymm hma (h.2 a hal)] } end theorem minimum_eq_coe_iff {m : α} {l : list α} : minimum l = m ↔ m ∈ l ∧ (∀ a ∈ l, m ≤ a) := @maximum_eq_coe_iff (order_dual α) _ _ _ end list
bac124f2a8b0b7b218f7f068d9eb36d2153aa280
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/graded_algebra/basic.lean
cb47f95c02b29f7c05ae79247ac5a8fd06d26ecb
[ "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
9,154
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Kevin Buzzard, Jujian Zhang -/ import algebra.direct_sum.algebra import algebra.direct_sum.internal import algebra.direct_sum.ring import group_theory.subgroup.basic /-! # Internally-graded algebras This file defines the typeclass `graded_algebra 𝒜`, for working with an algebra `A` that is internally graded by a collection of submodules `𝒜 : ι → submodule R A`. See the docstring of that typeclass for more information. ## Main definitions * `graded_algebra 𝒜`: the typeclass, which is a combination of `set_like.graded_monoid`, and a constructive version of `direct_sum.submodule_is_internal 𝒜`. * `graded_algebra.decompose : A ≃ₐ[R] ⨁ i, 𝒜 i`, which breaks apart an element of the algebra into its constituent pieces. * `graded_algebra.proj 𝒜 i` is the linear map from `A` to its degree `i : ι` component, such that `proj 𝒜 i x = decompose 𝒜 x i`. * `graded_algebra.support 𝒜 r` is the `finset ι` containing the `i : ι` such that the degree `i` component of `r` is not zero. ## Implementation notes For now, we do not have internally-graded semirings and internally-graded rings; these can be represented with `𝒜 : ι → submodule ℕ A` and `𝒜 : ι → submodule ℤ A` respectively, since all `semiring`s are ℕ-algebras via `algebra_nat`, and all `ring`s are `ℤ`-algebras via `algebra_int`. ## Tags graded algebra, graded ring, graded semiring, decomposition -/ open_locale direct_sum big_operators section graded_algebra variables {ι R A : Type*} variables [decidable_eq ι] [add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] variables (𝒜 : ι → submodule R A) /-- An internally-graded `R`-algebra `A` is one that can be decomposed into a collection of `submodule R A`s indexed by `ι` such that the canonical map `A → ⨁ i, 𝒜 i` is bijective and respects multiplication, i.e. the product of an element of degree `i` and an element of degree `j` is an element of degree `i + j`. Note that the fact that `A` is internally-graded, `graded_algebra 𝒜`, implies an externally-graded algebra structure `direct_sum.galgebra R (λ i, ↥(𝒜 i))`, which in turn makes available an `algebra R (⨁ i, 𝒜 i)` instance. -/ class graded_algebra extends set_like.graded_monoid 𝒜 := (decompose' : A → ⨁ i, 𝒜 i) (left_inv : function.left_inverse decompose' (direct_sum.submodule_coe 𝒜)) (right_inv : function.right_inverse decompose' (direct_sum.submodule_coe 𝒜)) lemma graded_algebra.is_internal [graded_algebra 𝒜] : direct_sum.submodule_is_internal 𝒜 := ⟨graded_algebra.left_inv.injective, graded_algebra.right_inv.surjective⟩ /-- A helper to construct a `graded_algebra` when the `set_like.graded_monoid` structure is already available. This makes the `left_inv` condition easier to prove, and phrases the `right_inv` condition in a way that allows custom `@[ext]` lemmas to apply. See note [reducible non-instances]. -/ @[reducible] def graded_algebra.of_alg_hom [set_like.graded_monoid 𝒜] (decompose : A →ₐ[R] ⨁ i, 𝒜 i) (right_inv : (direct_sum.submodule_coe_alg_hom 𝒜).comp decompose = alg_hom.id R A) (left_inv : ∀ i (x : 𝒜 i), decompose (x : A) = direct_sum.of (λ i, ↥(𝒜 i)) i x) : graded_algebra 𝒜 := { decompose' := decompose, right_inv := alg_hom.congr_fun right_inv, left_inv := begin suffices : decompose.comp (direct_sum.submodule_coe_alg_hom 𝒜) = alg_hom.id _ _, from alg_hom.congr_fun this, ext i x : 2, exact (decompose.congr_arg $ direct_sum.submodule_coe_alg_hom_of _ _ _).trans (left_inv i x), end} variable [graded_algebra 𝒜] /-- If `A` is graded by `ι` with degree `i` component `𝒜 i`, then it is isomorphic as an algebra to a direct sum of components. -/ def graded_algebra.decompose : A ≃ₐ[R] ⨁ i, 𝒜 i := alg_equiv.symm { to_fun := direct_sum.submodule_coe_alg_hom 𝒜, inv_fun := graded_algebra.decompose', left_inv := graded_algebra.left_inv, right_inv := graded_algebra.right_inv, map_mul' := alg_hom.map_mul _, map_add' := alg_hom.map_add _, commutes' := alg_hom.commutes _ } @[simp] lemma graded_algebra.decompose'_def : graded_algebra.decompose' = graded_algebra.decompose 𝒜 := rfl @[simp] lemma graded_algebra.decompose_symm_of {i : ι} (x : 𝒜 i) : (graded_algebra.decompose 𝒜).symm (direct_sum.of _ i x) = x := direct_sum.submodule_coe_alg_hom_of 𝒜 _ _ /-- The projection maps of graded algebra-/ def graded_algebra.proj (𝒜 : ι → submodule R A) [graded_algebra 𝒜] (i : ι) : A →ₗ[R] A := (𝒜 i).subtype.comp $ (dfinsupp.lapply i).comp $ (graded_algebra.decompose 𝒜).to_alg_hom.to_linear_map @[simp] lemma graded_algebra.proj_apply (i : ι) (r : A) : graded_algebra.proj 𝒜 i r = (graded_algebra.decompose 𝒜 r : ⨁ i, 𝒜 i) i := rfl /-- The support of `r` is the `finset` where `proj R A i r ≠ 0 ↔ i ∈ r.support`-/ def graded_algebra.support [Π (i : ι) (x : 𝒜 i), decidable (x ≠ 0)] (r : A) : finset ι := (graded_algebra.decompose 𝒜 r).support lemma graded_algebra.proj_recompose (a : ⨁ i, 𝒜 i) (i : ι) : graded_algebra.proj 𝒜 i ((graded_algebra.decompose 𝒜).symm a) = (graded_algebra.decompose 𝒜).symm (direct_sum.of _ i (a i)) := by rw [graded_algebra.proj_apply, graded_algebra.decompose_symm_of, alg_equiv.apply_symm_apply] @[simp] lemma graded_algebra.decompose_coe {i : ι} (x : 𝒜 i) : graded_algebra.decompose 𝒜 x = direct_sum.of _ i x := by rw [←graded_algebra.decompose_symm_of, alg_equiv.apply_symm_apply] lemma graded_algebra.decompose_of_mem {x : A} {i : ι} (hx : x ∈ 𝒜 i) : graded_algebra.decompose 𝒜 x = direct_sum.of _ i (⟨x, hx⟩ : 𝒜 i) := graded_algebra.decompose_coe _ ⟨x, hx⟩ lemma graded_algebra.decompose_of_mem_same {x : A} {i : ι} (hx : x ∈ 𝒜 i) : (graded_algebra.decompose 𝒜 x i : A) = x := by rw [graded_algebra.decompose_of_mem _ hx, direct_sum.of_eq_same, subtype.coe_mk] lemma graded_algebra.decompose_of_mem_ne {x : A} {i j : ι} (hx : x ∈ 𝒜 i) (hij : i ≠ j): (graded_algebra.decompose 𝒜 x j : A) = 0 := by rw [graded_algebra.decompose_of_mem _ hx, direct_sum.of_eq_of_ne _ _ _ _ hij, submodule.coe_zero] variable [Π (i : ι) (x : 𝒜 i), decidable (x ≠ 0)] lemma graded_algebra.mem_support_iff (r : A) (i : ι) : i ∈ graded_algebra.support 𝒜 r ↔ graded_algebra.proj 𝒜 i r ≠ 0 := begin rw [graded_algebra.support, dfinsupp.mem_support_iff, graded_algebra.proj_apply], simp only [ne.def, submodule.coe_eq_zero], end lemma graded_algebra.sum_support_decompose (r : A) : ∑ i in graded_algebra.support 𝒜 r, (graded_algebra.decompose 𝒜 r i : A) = r := begin conv_rhs { rw [←(graded_algebra.decompose 𝒜).symm_apply_apply r, ←direct_sum.sum_support_of _ (graded_algebra.decompose 𝒜 r)] }, rw [alg_equiv.map_sum, graded_algebra.support], simp_rw graded_algebra.decompose_symm_of, end end graded_algebra section canonical_order open graded_algebra set_like.graded_monoid direct_sum variables {ι R A : Type*} variables [comm_semiring R] [semiring A] variables [algebra R A] [decidable_eq ι] variables [canonically_ordered_add_monoid ι] variables (𝒜 : ι → submodule R A) [graded_algebra 𝒜] /-- If `A` is graded by a canonically ordered add monoid, then the projection map `x ↦ x₀` is a ring homomorphism. -/ @[simps] def graded_algebra.proj_zero_ring_hom : A →+* A := { to_fun := λ a, decompose 𝒜 a 0, map_one' := decompose_of_mem_same 𝒜 one_mem, map_zero' := by simp only [subtype.ext_iff_val, map_zero, zero_apply, submodule.coe_zero], map_add' := λ _ _, by simp [subtype.ext_iff_val, map_add, add_apply, submodule.coe_add], map_mul' := λ x y, have m : ∀ x, x ∈ supr 𝒜, from λ x, (is_internal 𝒜).supr_eq_top.symm ▸ submodule.mem_top, begin refine submodule.supr_induction 𝒜 (m x) (λ i c hc, _) _ _, { refine submodule.supr_induction 𝒜 (m y) (λ j c' hc', _) _ _, { by_cases h : i + j = 0, { rw [decompose_of_mem_same 𝒜 (show c * c' ∈ 𝒜 0, from h ▸ mul_mem hc hc'), decompose_of_mem_same 𝒜 (show c ∈ 𝒜 0, from (add_eq_zero_iff.mp h).1 ▸ hc), decompose_of_mem_same 𝒜 (show c' ∈ 𝒜 0, from (add_eq_zero_iff.mp h).2 ▸ hc')] }, { rw [decompose_of_mem_ne 𝒜 (mul_mem hc hc') h], cases (show i ≠ 0 ∨ j ≠ 0, by rwa [add_eq_zero_iff, not_and_distrib] at h) with h' h', { simp only [decompose_of_mem_ne 𝒜 hc h', zero_mul] }, { simp only [decompose_of_mem_ne 𝒜 hc' h', mul_zero] } } }, { simp only [map_zero, zero_apply, submodule.coe_zero, mul_zero] }, { intros _ _ hd he, simp only [mul_add, map_add, add_apply, submodule.coe_add, hd, he] } }, { simp only [map_zero, zero_apply, submodule.coe_zero, zero_mul] }, { rintros _ _ ha hb, simp only [add_mul, map_add, add_apply, submodule.coe_add, ha, hb] }, end } end canonical_order
271b36753e962af154c61e8a6f7af0fd750e4c82
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/functor/inv_isos.lean
0dc3e4d9e6f97efdee085426a6b66827b2d2b461
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
1,898
lean
/- Copyright (c) 2022 Antoine Labelle. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Labelle -/ import category_theory.eq_to_hom /-! # Natural isomorphisms with composition with inverses > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Definition of useful natural isomorphisms involving inverses of functors. These definitions cannot go in `category_theory/equivalence` because they require `eq_to_hom`. -/ namespace category_theory open category_theory.functor universes u₁ u₂ u₃ v₁ v₂ v₃ variables {A : Type u₁} [category.{v₁} A] {B : Type u₂} [category.{v₂} B] {C : Type u₃} [category.{v₃} C] variables {F : A ⥤ C} {G : A ⥤ B} {H : B ⥤ C} /-- Construct an isomorphism `F ⋙ H.inv ≅ G` from an isomorphism `F ≅ G ⋙ H`. -/ @[simps] def comp_inv_iso [h : is_equivalence H] (i : F ≅ G ⋙ H) : F ⋙ H.inv ≅ G := iso_whisker_right i H.inv ≪≫ (associator G H H.inv) ≪≫ iso_whisker_left G h.unit_iso.symm ≪≫ eq_to_iso (functor.comp_id G) /-- Construct an isomorphism `G ≅ F ⋙ H.inv` from an isomorphism `G ⋙ H ≅ F`. -/ @[simps] def iso_comp_inv [h : is_equivalence H] (i : G ⋙ H ≅ F) : G ≅ F ⋙ H.inv := (comp_inv_iso i.symm).symm /-- Construct an isomorphism `G.inv ⋙ F ≅ H` from an isomorphism `F ≅ G ⋙ H`. -/ @[simps] def inv_comp_iso [h : is_equivalence G] (i : F ≅ G ⋙ H) : G.inv ⋙ F ≅ H := iso_whisker_left G.inv i ≪≫ (associator G.inv G H).symm ≪≫ iso_whisker_right h.counit_iso H ≪≫ eq_to_iso (functor.id_comp H) /-- Construct an isomorphism `H ≅ G.inv ⋙ F` from an isomorphism `G ⋙ H ≅ F`. -/ @[simps] def iso_inv_comp [h : is_equivalence G] (i : G ⋙ H ≅ F) : H ≅ G.inv ⋙ F := (inv_comp_iso i.symm).symm end category_theory
5bb7ce20ebe362669a24355f15cc6992c082fe0b
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/addition1.lean
a816e7349b6bc64bbf907125adc35f720bb3510b
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
7,319
lean
local attribute [instance] decidable_inhabited prop_decidable inductive xnat | zero : xnat | succ : xnat → xnat open xnat definition one := succ zero definition two := succ one definition add :xnat → xnat → xnat | n zero := n | n (succ p) := succ (add n p) notation a + b := add a b theorem one_add_one_equals_two : one + one = two := begin unfold two, unfold one, unfold add, end theorem add_zerox (n:xnat): n+zero=n:= begin unfold add, end theorem zero_addx (n:xnat):zero+n=n:= begin induction n with k H, unfold add, unfold add, rw[H], end theorem add_assocx (a b c:xnat):(a+b)+c=a+(b+c):= begin induction c with k H, unfold add, unfold add, rw[H], end theorem zero_add_eq_ad_zerox (n:xnat) : zero+n=n+zero:= begin rw[zero_addx,add_zerox], end theorem add_one_eq_succx (n:xnat) : n + one = succ n:= begin unfold one add, end theorem one_add_eq_succx (n : xnat) : one+n=succ n:= begin induction n with k H, unfold one add, unfold one add, rw[←H], unfold one, end theorem add_commx (a b:xnat) : a+b = b+a:= begin induction b with k H, rw[zero_add_eq_ad_zerox], unfold add, rw[H,←add_one_eq_succx,←add_one_eq_succx,add_assocx,add_assocx,add_one_eq_succx,one_add_eq_succx], end theorem eq_iff_succ_eq_succ (a b : xnat) : succ a = succ b ↔ a = b := begin split, exact succ.inj, assume H : a = b, rw [H], end theorem add_cancel_right (a b t : xnat) : a = b ↔ a+t = b+t := begin split, assume H, rw[H], induction t with k H, rw[add_zerox,add_zerox], assume H1, exact H1, unfold add, rw[eq_iff_succ_eq_succ (a+k) (b+k)], exact H, end definition mul:xnat→xnat→xnat | n zero:=zero | n (succ p):= mul n p + n notation a * b := mul a b theorem mul_zerox (a : xnat) : a * zero = zero := begin trivial, end theorem zero_mulx (a : xnat) : zero * a = zero := begin induction a with k H, unfold mul, unfold mul add, rw[H], end theorem mul_onex (a : xnat) : a * one = a := begin unfold one mul, rw[zero_addx], end theorem one_mulx (a : xnat) : one * a = a := begin induction a with k H, unfold mul, unfold mul, rw[add_one_eq_succx, H], end theorem right_distribx (a b c : xnat) : a * (b + c) = a* b + a * c := begin induction c with k H, rw[mul_zerox,add_zerox,add_zerox], unfold add mul, rw[H, add_assocx], end theorem left_distribx (a b c : xnat) : (a + b) * c = a * c + b * c := begin induction c with n Hn, unfold mul, refl, rw [←add_one_eq_succx,right_distribx,Hn,right_distribx,right_distribx], rw [mul_onex,mul_onex,mul_onex], rw [add_assocx,←add_assocx (b*n),add_commx (b*n),←add_assocx,←add_assocx,←add_assocx], end theorem mul_assocx (a b c : xnat) : (a * b) * c = a * (b * c) := begin induction c with k H, rw[mul_zerox,mul_zerox,mul_zerox], unfold mul, rw[right_distribx,H] end theorem mul_commx (a b : xnat) : a * b = b * a := begin induction b with k H, rw[mul_zerox,zero_mulx], unfold mul, rw[H], exact calc k * a + a = k * a + one * a: by rw[one_mulx] ...=(k + one) * a: by rw[left_distribx] ...=succ k * a: by rw[add_one_eq_succx], end definition lt : xnat → xnat → Prop | zero zero := false | (succ m) zero := false | zero (succ p) := true | (succ m) (succ p) := lt m p notation a < b := lt a b theorem subtraction (a b:xnat) (Hab:a<b): ∃(c:xnat), succ c+a=b:=begin revert a, induction b with b1 Hib, assume a1 H1,exfalso,revert H1, cases a1 with a2, unfold lt,trivial, unfold lt,trivial, assume a1, cases a1 with a2, assume H1,existsi b1,unfold add, unfold lt, have H1:∀d:xnat,succ d+a2=b1→∃c,succ c+succ a2=succ b1:=begin unfold add,assume d H1,existsi d, rw H1, end, assume H2, exact exists.elim (Hib a2 H2) H1, end theorem not_lt_itself (x:xnat):¬x<x:=begin induction x with k H, unfold lt, assume H,trivial, unfold lt, exact H, end theorem inequality_A1 (a b t : xnat) : a < b ↔ a + t < b + t := begin apply iff.intro, induction t with k H, rw[add_zerox,add_zerox], assume H1,exact H1, unfold add,unfold lt,exact H, induction t with n H, rw[add_zerox,add_zerox], assume H1, exact H1, unfold add lt, exact H, end theorem inequality_A2 (a b c:xnat):a<b→b<c→a<c:=begin revert a b, induction c with c1 Hic, assume a b, cases b with b1, unfold lt,assume H,trivial, unfold lt,assume H,trivial, assume a b, cases a with a1, unfold lt,assume H1 H2,trivial, cases b with b1, unfold lt,trivial, unfold lt,exact Hic a1 b1, end theorem inequality_A3 (g b : xnat) : (g < b ∨ g = b ∨ b < g) ∧ (g < b → ¬ (g = b)) ∧ (g < b → ¬ (b < g))∧ ((g = b) → ¬ (b < g)):=begin apply and.intro, tactic.swap, apply and.intro, have H1:∀c,succ c+g=b→¬g=b:=begin assume c H1, rw ←H1, have H4:¬zero=succ c:=begin assume H, have H1:zero<zero:= by cc,revert H1,unfold lt, trivial, end, revert H4, rw[add_cancel_right],tactic.swap,exact g, rw zero_addx,assume H3,assumption, end, assume H2, exact exists.elim (subtraction g b H2) H1, apply and.intro, tactic.swap, assume H1, rw H1, exact not_lt_itself b, assume H1, have H2:∀c,succ c+g=b→¬b < g:=begin assume c1 H2,rw ←H2, have H3:¬succ c1 < zero:=begin unfold lt,assume H3,assumption, end,assume H4,revert H3,rw[inequality_A1 (succ c1) zero g,zero_addx], assume H5,exact H5 H4, end, exact exists.elim (subtraction g b H1) H2, have H1:∀ x y:xnat, ¬x<y→¬y<x→x=y:=begin assume x y H1 H2,revert y, induction x with x1 H, assume y1 H1 H2,clear H2, cases y1 with y1, trivial, exfalso, have H2:zero<succ y1:= by unfold lt, exact H1 H2, assume y1, cases y1 with y1, assume H1 H2,exfalso,have H3:zero<succ x1:=by unfold lt, exact H2 H3, unfold lt,rw eq_iff_succ_eq_succ, exact H y1, end, rw [or.comm,or.assoc], have H2:¬g < b → ¬b < g → g = b:=H1 g b,clear H1, cases classical.em (g<b) with A B, right,right,assumption, have H:¬b < g → g = b:= H2 B,clear H2 B, cases classical.em (b<g) with A B, right,left,assumption,left, exact H B, end theorem inequality_A4 (g b : xnat) : zero<g → zero<b → zero<g*b :=begin cases g with g1, assume H1,exfalso,revert H1, unfold lt,trivial, cases b with b1, assume H1 H2,exfalso,revert H2, unfold lt,trivial, assume H1 H2, rw[←one_add_eq_succx,←one_add_eq_succx, right_distribx, left_distribx,left_distribx], have H3:one*one=one:=begin unfold one,unfold mul,rw zero_addx, end, rw[H3, add_assocx, one_add_eq_succx],unfold lt, end
0908190cc2bfccae2865b211181b99fcc3c4f155
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/injection_tactic.lean
dde152855d408e90327a1e3ad0eff036aa216692
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
4,752
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jannis Limperg -/ prelude import init.meta.tactic namespace tactic open expr /- Given a local constant `H : C x₁ ... xₙ = D y₁ ... yₘ`, where `C` and `D` are fully applied constructors, `injection_with H ns base offset` does the following: - If `C ≠ D`, it solves the goal (using the no-confusion rule). - If `C = D` (and thus `n = m`), it adds hypotheses `h₁ : x₁ = y₁, ..., hₙ : xₙ = yₙ` to the local context. Names for the `hᵢ` are taken from `ns`. If `ns` does not contain enough names, then the names are derived from `base` and `offset` (by default `h_1`, `h_2` etc.; see `intro_fresh`). - Special case: if `C = D` and `n = 0` (i.e. the constructors have no arguments), the hypothesis `h : true` is added to the context. `injection_with` returns the new hypotheses and the leftover names from `ns` (i.e. those names that were not used to name the new hypotheses). If (and only if) the goal was solved, the list of new hypotheses is empty. -/ meta def injection_with (h : expr) (ns : list name) (base := `h) (offset := some 1) : tactic (list expr × list name) := do H ← infer_type h, (lhs, rhs, constructor_left, constructor_right, inj_name) ← do { (lhs, rhs) ← match_eq H, lhs ← whnf_ginductive lhs, rhs ← whnf_ginductive rhs, env ← get_env, (const constructor_left _) ← pure $ get_app_fn lhs, (const constructor_right _) ← pure $ get_app_fn rhs, inj_name ← resolve_constant $ constructor_left ++ "inj_arrow", pure (lhs, rhs, constructor_left, constructor_right, inj_name) } <|> fail "injection tactic failed, argument must be an equality proof where lhs and rhs are of the form (c ...), where c is a constructor", if constructor_left = constructor_right then do -- C.inj_arrow, for a given constructor C of datatype D, has type -- -- ∀ (A₁ ... Aₙ) (x₁ ... xₘ) (y₁ ... yₘ), C x₁ ... xₘ = C y₁ ... yₘ -- → ∀ ⦃P : Sort u⦄, (x₁ = y₁ → ... → yₖ = yₖ → P) → P -- -- where the Aᵢ are parameters of D and the xᵢ/yᵢ are arguments of C. -- Note that if xᵢ/yᵢ are propositions, no equation is generated, so the -- number of equations is not necessarily the constructor arity. -- First, we find out how many equations we need to intro later. inj ← mk_const inj_name, inj_type ← infer_type inj, inj_arity ← get_pi_arity inj_type, let num_equations := (inj_type.nth_binding_body (inj_arity - 1)).binding_domain.pi_arity, -- Now we generate the actual proof of the target. tgt ← target, proof ← mk_mapp inj_name (list.repeat none (inj_arity - 3) ++ [some h, some tgt]), eapply proof, intron_with num_equations ns base offset else do tgt ← target, let inductive_name := constructor_left.get_prefix, pr ← mk_app (inductive_name <.> "no_confusion") [tgt, lhs, rhs, h], exact pr, return ([], ns) /-- Simplify the equation `h` using injectivity of constructors. See `injection_with`. Returns the hypotheses that were added to the context, or an empty list if the goal was solved by contradiction. -/ meta def injection (h : expr) (base := `h) (offset := some 1) : tactic (list expr) := prod.fst <$> injection_with h [] base offset private meta def injections_with_inner (base : name) (offset : option ℕ) : ℕ → list expr → list name → tactic unit | 0 lc ns := fail "recursion depth exceeded" | (n+1) [] ns := skip | (n+1) (h :: lc) ns := do o ← try_core (injection_with h ns base offset), match o with | none := injections_with_inner (n+1) lc ns | some ([], _) := skip -- This means that the contradiction part was triggered and the goal is done | some (t, ns') := injections_with_inner n (t ++ lc) ns' end /-- Simplifies equations in the context using injectivity of constructors. For each equation `h : C x₁ ... xₙ = D y₁ ... yₘ` in the context, where `C` and `D` are constructors of the same data type, `injections_with` does the following: - If `C = D`, it adds equations `x₁ = y₁`, ..., `xₙ = yₙ`. - If `C ≠ D`, it solves the goal by contradiction. See `injection_with` for details, including information about `base` and `offset`. `injections_with` also recurses into the new equations `xᵢ = yᵢ`. If it has to recurse more than five times, it fails. -/ meta def injections_with (ns : list name) (base := `h) (offset := some 1) : tactic unit := do lc ← local_context, injections_with_inner base offset 5 lc ns end tactic
962a9dcc6e654ea51ea574049c942665ab3a2c51
0179bcdbf094a112437450a02dc2bdc8b2f921d4
/folklore/real_axiom.lean
a7edcd473860052ab032f042e6f73f0e18875620
[ "CC-BY-4.0" ]
permissive
haselwarter/formalabstracts
cf0c129fc086526cef1bd65d95c6f95a9a7ec5c8
eab6d94850308df9f09d23f3a9a2f7b5ac5c6c7a
refs/heads/master
1,609,515,992,248
1,500,418,815,000
1,500,418,815,000
97,696,544
0
0
null
1,500,455,350,000
1,500,455,350,000
null
UTF-8
Lean
false
false
3,207
lean
/- axiomatic development of the complete ordered field of real numbers in classical logic. At some point this should be replaced with an actual construction. T.Hales, July 15, 2017 -/ import meta_data import data.list data.vector noncomputable theory namespace real_axiom open classical nat int list vector universe u -- TODO: This definition used to be universe-polymorphic, but it looks like unfinished -- does not support parameters so the universe u is unavailable, fix it. unfinished ℝ : Type := { description := "the type real numbers" } unfinished real_of_int : ℤ → ℝ := { description := "the canonical embedding of integers into reals" } -- TODO: properly treat unbounded and empty sets unfinished sup : (set ℝ) → ℝ := { description := "the supremum of a subset of real numbers" } instance real_of_nat_coe : has_coe ℤ ℝ := ⟨ real_of_int ⟩ @[instance] constant real_ordered_field : linear_ordered_field ℝ -- It is not clear why this line is required; but it is. attribute [instance] real_ordered_field def is_upper_bound {α : Type u} [R : linear_ordered_ring α] (S : set α) (r : α) : Prop := (∀ (s : α), s ∈ S → s ≤ r) def has_upper_bound {α : Type u} [R : linear_ordered_ring α] (S : set α) : Prop := (∃ r : α, is_upper_bound S r) def is_least_upper_bound {α : Type u} [R : linear_ordered_ring α] (S : set α) (s : α) := (is_upper_bound S s ∧ (∀ r, is_upper_bound S r → r ≤ s)) class complete_ordered_field (α : Type u) extends linear_ordered_field α := (sup : (set α) → α) (dedekind_completeness : (∀ (S : set α), (S ≠ ∅) → has_upper_bound S → is_least_upper_bound S (sup S))) unfinished real_dedekind_completeness : (∀ (S : set ℝ), (S ≠ ∅) → has_upper_bound S → is_least_upper_bound S (sup S)) := { description := "the real numbers are Dedekind complete" } instance real_complete_ordered_field : complete_ordered_field ℝ := { real_axiom.real_ordered_field with sup := sup, dedekind_completeness := real_dedekind_completeness } unfinished real_archimedean : (∀ x y, x > 0 → y > 0 → ∃ (n : ℕ), y < n*x) := { description := "the real numbers are an archimedean field" } -- why does -x ∈ S fail? -- why does {x | ... } fail? def real_inf (S : set ℝ) : ℝ := - (sup (λ x, S( -x ))) -- extension by zero when x < 0. def real_sqrt (x : ℝ) : ℝ := sup (λ (t : ℝ), (t = 0) ∨ t*t = x) def real_abs (x : ℝ) : ℝ := sup (λ (t : ℝ), t = x ∨ t = -x) def real_max (x : ℝ) y : ℝ := sup (λ (t : ℝ), t = x ∨ t = y) def real_min (x : ℝ) y : ℝ := real_inf (λ (t : ℝ), t = x ∨ t = y) def real_pow : ℝ → ℕ → ℝ | x 0 := (1 : ℝ) | x (succ n) := x * (real_pow x n) local infix `^` := real_pow unfinished pi : ℝ := { description := "the ratio of the circumference and the diameter of a circle" } -- one possible specification of pi unfinished pi_def : (∀ (n : nat), let iota := list.iota n, terms := list.map (λ k, (-1)^(k+1) / (2*k-1)) iota in (real_abs (pi/ 4 - list.sum terms) < 1 / (2*n + 3))) := { description := "alternating series for pi/4 = 1 - 1/3 + 1/5 -..." } end real_axiom
e1dae15a093775cc641ae8acbd6af4472e8be192
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/liminf_limsup.lean
8e2573f101b5a34ed2c4579b059f8435f0a257b9
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
23,892
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.partial import order.filter.at_top_bot /-! # 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_sets_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.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 [nonempty α] [semilattice_sup α] [preorder β] [no_top_order β] {f : α → β} (hf : tendsto f at_top at_top) : ¬ is_bounded_under (≤) at_top f := begin rintro ⟨b, hb⟩, rw eventually_map at hb, obtain ⟨b', h⟩ := no_top 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 at_top.empty_nmem_sets (this ▸ filter.inter_mem_sets hb hb' : ∅ ∈ (at_top : filter α)), end lemma not_is_bounded_under_of_tendsto_at_bot [nonempty α] [semilattice_sup α] [preorder β] [no_bot_order β] {f : α → β} (hf : tendsto f at_top at_bot) : ¬ is_bounded_under (≥) at_top f := begin rintro ⟨b, hb⟩, rw eventually_map at hb, obtain ⟨b', h⟩ := no_bot b, have hb' := (tendsto_at_bot.mp hf) b', have : {x : α | b ≤ f x} ∩ {x : α | f x ≤ b'} = ∅ := eq_empty_of_subset_empty (λ x hx, (not_le_of_lt h) (le_trans hx.1 hx.2)), exact at_top.empty_nmem_sets (this ▸ filter.inter_mem_sets hb hb' : ∅ ∈ (at_top : filter α)), end /-- `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_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 [order_bot α] {f : filter α} : f.is_cobounded (≤) := ⟨⊥, assume a h, bot_le⟩ lemma is_cobounded_ge_of_top [order_top α] {f : filter α} : f.is_cobounded (≥) := ⟨⊤, assume a h, le_top⟩ lemma is_bounded_le_of_top [order_top α] {f : filter α} : f.is_bounded (≤) := ⟨⊤, eventually_of_forall $ λ _, le_top⟩ lemma is_bounded_ge_of_bot [order_bot α] {f : filter α} : f.is_bounded (≥) := ⟨⊥, eventually_of_forall $ λ _, bot_le⟩ 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] assume x, sup_le_sup⟩ 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) | ⟨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] assume x, inf_le_inf⟩ /-- 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 (order_dual β) α _ _ _ _ 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 (order_dual α) _ 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 (order_dual β) _ _ _ _ _ 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 (order_dual β) α _ f _ b 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_refl _)), 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 (order_dual α) β _ _ lemma liminf_le_limsup {f : filter β} [ne_bot f] {u : β → α} : liminf f u ≤ limsup f u := Liminf_le_Limsup is_bounded_le_of_top is_bounded_ge_of_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_binfi $ λ 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 _ $ 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 (order_dual α) _ _ _ _ _ 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 (order_dual α) _ _ /-- 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 (order_dual α) β _ _ _ lemma liminf_eq_supr_infi_of_nat {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i ≥ n, u i := @limsup_eq_infi_supr_of_nat (order_dual α) _ u lemma liminf_eq_supr_infi_of_nat' {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i : ℕ, u (i + n) := @limsup_eq_infi_supr_of_nat' (order_dual α) _ _ 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 (order_dual α) _ _ _ _ _ _ _ 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 (order_dual α) _ 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' _ (order_dual β) _ _ _ _ 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 _ (order_dual β) _ _ _ _ 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 _ (order_dual β) _ f u b hu_le hu end conditionally_complete_linear_order end filter section order open filter 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 α (order_dual β) (order_dual γ) _ _ f u g.dual hu hu_co hgu hgu_co end order
a74d9d856831cf3c8dd5beb7484a8aa5c83f6c5c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/test/instance_diamonds.lean
9e0db87bd64862d4cbec7f3970ef2a14b8b88a90
[ "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
8,493
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.module.pi import data.polynomial.basic import group_theory.group_action.prod import group_theory.group_action.units import data.complex.module import ring_theory.algebraic import data.zmod.basic import ring_theory.tensor_product /-! # Tests that instances do not form diamonds -/ /-! ## Scalar action instances -/ section has_smul open_locale polynomial example : (sub_neg_monoid.has_smul_int : has_smul ℤ ℂ) = (complex.has_smul : has_smul ℤ ℂ) := rfl example : restrict_scalars.module ℝ ℂ ℂ = complex.module := rfl example : restrict_scalars.algebra ℝ ℂ ℂ = complex.algebra := rfl example (α β : Type*) [add_monoid α] [add_monoid β] : (prod.has_smul : has_smul ℕ (α × β)) = add_monoid.has_smul_nat := rfl example (α β : Type*) [sub_neg_monoid α] [sub_neg_monoid β] : (prod.has_smul : has_smul ℤ (α × β)) = sub_neg_monoid.has_smul_int := rfl example (α : Type*) (β : α → Type*) [Π a, add_monoid (β a)] : (pi.has_smul : has_smul ℕ (Π a, β a)) = add_monoid.has_smul_nat := rfl example (α : Type*) (β : α → Type*) [Π a, sub_neg_monoid (β a)] : (pi.has_smul : has_smul ℤ (Π a, β a)) = sub_neg_monoid.has_smul_int := rfl namespace tensor_product open_locale tensor_product open complex /-! The `example` below times out. TODO Fix it! /- `tensor_product.algebra.module` forms a diamond with `has_mul.to_has_smul` and `algebra.tensor_product.tensor_product.semiring`. Given a commutative semiring `A` over a commutative semiring `R`, we get two mathematically different scalar actions of `A ⊗[R] A` on itself. -/ def f : ℂ ⊗[ℝ] ℂ →ₗ[ℝ] ℝ := tensor_product.lift { to_fun := λ z, z.re • re_lm, map_add' := λ z w, by simp [add_smul], map_smul' := λ r z, by simp [mul_smul], } @[simp] lemma f_apply (z w : ℂ) : f (z ⊗ₜ[ℝ] w) = z.re * w.re := by simp [f] /- `tensor_product.algebra.module` forms a diamond with `has_mul.to_has_smul` and `algebra.tensor_product.tensor_product.semiring`. Given a commutative semiring `A` over a commutative semiring `R`, we get two mathematically different scalar actions of `A ⊗[R] A` on itself. -/ example : has_mul.to_has_smul (ℂ ⊗[ℝ] ℂ) ≠ (@tensor_product.algebra.module ℝ ℂ ℂ (ℂ ⊗[ℝ] ℂ) _ _ _ _ _ _ _ _ _ _ _ _).to_has_smul := begin have contra : I ⊗ₜ[ℝ] I ≠ (-1) ⊗ₜ[ℝ] 1 := λ c, by simpa using congr_arg f c, contrapose! contra, rw has_smul.ext_iff at contra, replace contra := congr_fun (congr_fun contra (1 ⊗ₜ I)) (I ⊗ₜ 1), rw @tensor_product.algebra.smul_def ℝ ℂ ℂ (ℂ ⊗[ℝ] ℂ) _ _ _ _ _ _ _ _ _ _ _ _ (1 : ℂ) I (I ⊗ₜ[ℝ] (1 : ℂ)) at contra, simpa only [algebra.id.smul_eq_mul, algebra.tensor_product.tmul_mul_tmul, one_mul, mul_one, one_smul, tensor_product.smul_tmul', I_mul_I] using contra, end -/ end tensor_product section units example (α : Type*) [monoid α] : (units.mul_action : mul_action αˣ (α × α)) = prod.mul_action := rfl example (R α : Type*) (β : α → Type*) [monoid R] [Π i, mul_action R (β i)] : (units.mul_action : mul_action Rˣ (Π i, β i)) = pi.mul_action _ := rfl example (R α : Type*) (β : α → Type*) [monoid R] [semiring α] [distrib_mul_action R α] : (units.distrib_mul_action : distrib_mul_action Rˣ α[X]) = polynomial.distrib_mul_action := rfl /-! TODO: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/units.2Emul_action'.20diamond/near/246402813 ```lean example {α : Type*} [comm_monoid α] : (units.mul_action' : mul_action αˣ αˣ) = monoid.to_mul_action _ := rfl -- fails ``` -/ end units end has_smul /-! ## `with_top` (Type with point at infinity) instances -/ section with_top example (R : Type*) [h : strict_ordered_semiring R] : (@with_top.add_comm_monoid R (@non_unital_non_assoc_semiring.to_add_comm_monoid R (@non_assoc_semiring.to_non_unital_non_assoc_semiring R (@semiring.to_non_assoc_semiring R (@strict_ordered_semiring.to_semiring R h))))) = (@ordered_add_comm_monoid.to_add_comm_monoid (with_top R) (@with_top.ordered_add_comm_monoid R (@ordered_cancel_add_comm_monoid.to_ordered_add_comm_monoid R (@strict_ordered_semiring.to_ordered_cancel_add_comm_monoid R h)))) := rfl end with_top /-! ## `multiplicative` instances -/ section multiplicative example : @monoid.to_mul_one_class (multiplicative ℕ) (comm_monoid.to_monoid _) = multiplicative.mul_one_class := rfl -- `dunfold` can still break unification, but it's better to have `dunfold` break it than have the -- above example fail. example : @monoid.to_mul_one_class (multiplicative ℕ) (comm_monoid.to_monoid _) = multiplicative.mul_one_class := begin dunfold has_one.one multiplicative.mul_one_class, success_if_fail { refl, }, ext, refl end end multiplicative /-! ## `finsupp` instances-/ section finsupp open finsupp /-- `finsupp.comap_has_smul` can form a non-equal diamond with `finsupp.smul_zero_class` -/ example {k : Type*} [semiring k] [nontrivial k] : (finsupp.comap_has_smul : has_smul k (k →₀ k)) ≠ finsupp.smul_zero_class.to_has_smul := begin obtain ⟨u : k, hu⟩ := exists_ne (1 : k), intro h, simp only [has_smul.ext_iff, function.funext_iff, finsupp.ext_iff] at h, replace h := h u (finsupp.single 1 1) u, classical, rw [comap_smul_single, smul_apply, smul_eq_mul, mul_one, single_eq_same, smul_eq_mul, single_eq_of_ne hu.symm, mul_zero] at h, exact one_ne_zero h, end /-- `finsupp.comap_has_smul` can form a non-equal diamond with `finsupp.smul_zero_class` even when the domain is a group. -/ example {k : Type*} [semiring k] [nontrivial kˣ] : (finsupp.comap_has_smul : has_smul kˣ (kˣ →₀ k)) ≠ finsupp.smul_zero_class.to_has_smul := begin obtain ⟨u : kˣ, hu⟩ := exists_ne (1 : kˣ), haveI : nontrivial k := ⟨⟨u, 1, units.ext.ne hu⟩⟩, intro h, simp only [has_smul.ext_iff, function.funext_iff, finsupp.ext_iff] at h, replace h := h u (finsupp.single 1 1) u, classical, rw [comap_smul_single, smul_apply, units.smul_def, smul_eq_mul, mul_one, single_eq_same, smul_eq_mul, single_eq_of_ne hu.symm, mul_zero] at h, exact one_ne_zero h, end end finsupp /-! ## `polynomial` instances -/ section polynomial variables (R A : Type*) open_locale polynomial open polynomial /-- `polynomial.has_smul_pi` forms a diamond with `pi.has_smul`. -/ example [semiring R] [nontrivial R] : polynomial.has_smul_pi _ _ ≠ (pi.has_smul : has_smul R[X] (R → R[X])) := begin intro h, simp_rw [has_smul.ext_iff, function.funext_iff, polynomial.ext_iff] at h, simpa using h X 1 1 0, end /-- `polynomial.has_smul_pi'` forms a diamond with `pi.has_smul`. -/ example [comm_semiring R] [nontrivial R] : polynomial.has_smul_pi' _ _ _ ≠ (pi.has_smul : has_smul R[X] (R → R[X])) := begin intro h, simp_rw [has_smul.ext_iff, function.funext_iff, polynomial.ext_iff] at h, simpa using h X 1 1 0, end /-- `polynomial.has_smul_pi'` is consistent with `polynomial.has_smul_pi`. -/ example [comm_semiring R] [nontrivial R] : polynomial.has_smul_pi' _ _ _ = (polynomial.has_smul_pi _ _ : has_smul R[X] (R → R[X])) := rfl /-- `polynomial.algebra_of_algebra` is consistent with `algebra_nat`. -/ example [semiring R] : (polynomial.algebra_of_algebra : algebra ℕ R[X]) = algebra_nat := rfl /-- `polynomial.algebra_of_algebra` is consistent with `algebra_int`. -/ example [ring R] : (polynomial.algebra_of_algebra : algebra ℤ R[X]) = algebra_int _ := rfl end polynomial /-! ## `subtype` instances -/ section subtype -- this diamond is the reason that `fintype.to_locally_finite_order` is not an instance example {α} [preorder α] [locally_finite_order α] [fintype α] [@decidable_rel α (<)] [@decidable_rel α (≤)] (p : α → Prop) [decidable_pred p] : subtype.locally_finite_order p = fintype.to_locally_finite_order := begin success_if_fail { refl, }, exact subsingleton.elim _ _ end end subtype /-! ## `zmod` instances -/ section zmod variables {p : ℕ} [fact p.prime] example : @euclidean_domain.to_comm_ring _ (@field.to_euclidean_domain _ (zmod.field p)) = zmod.comm_ring p := rfl example (n : ℕ) : zmod.comm_ring (n + 1) = fin.comm_ring n := rfl example : zmod.comm_ring 0 = int.comm_ring := rfl end zmod
54cfc9ba97c582d113e9b117ebd40173d51a7ab9
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/data/unit.lean
de2d0132fc9f207f08e8b13253a624824a85ffd8
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
480
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.logic lemma unit_eq (a b : unit) : a = b := unit.rec_on a (unit.rec_on b rfl) lemma unit_eq_unit (a : unit) : a = () := unit_eq a () instance : subsingleton unit := subsingleton.intro unit_eq instance : inhabited unit := ⟨()⟩ instance : decidable_eq unit := λ a b, is_true (unit_eq a b)
9a80afd459faa45f6856592d6ce260dfecd066f2
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/complex/abs_max.lean
c00c4fa95b66bd02718375f1f8bb04ca3645caf0
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
12,540
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.cauchy_integral import analysis.convex.integral import analysis.normed_space.completion import topology.algebra.order.extr_closure /-! # Maximum modulus principle In this file we prove several versions of the maximum modulus principle. There are several statements that can be called "the maximum modulus principle" for maps between normed complex spaces. In the most general case, see `complex.norm_eventually_eq_of_is_local_max`, we can only say that for a differentiable function `f : E → F`, if the norm has a local maximum at `z`, then *the norm* is constant in a neighborhood of `z`. If the domain is a nontrivial finite dimensional space, then this implies the following version of the maximum modulus principle, see `complex.exists_mem_frontier_is_max_on_norm`. If `f : E → F` is complex differentiable on a nonempty compact set `K`, then there exists a point `z ∈ frontier K` such that `λ z, ∥f z∥` takes it maximum value on `K` at `z`. Finally, if the codomain is a strictly convex space, then the function cannot have a local maximum of the norm unless the function (not only its norm) is a constant. This version is not formalized yet. -/ open topological_space metric set filter asymptotics function measure_theory affine_map open_locale topological_space filter nnreal real universes u v w variables {E : Type u} [normed_group E] [normed_space ℂ E] {F : Type v} [normed_group F] [normed_space ℂ F] local postfix `̂`:100 := uniform_space.completion namespace complex /-! ### Auxiliary lemmas We split the proof into a series of lemmas. First we prove the principle for a function `f : ℂ → F` with an additional assumption that `F` is a complete space, then drop unneeded assumptions one by one. The only "public API" lemmas in this section are TODO and `complex.norm_eq_norm_of_is_max_on_of_closed_ball_subset`. -/ lemma norm_max_aux₁ [complete_space F] {f : ℂ → F} {z w : ℂ} (hd : diff_cont_on_cl ℂ f (ball z (dist w z))) (hz : is_max_on (norm ∘ f) (closed_ball z (dist w z)) z) : ∥f w∥ = ∥f z∥ := begin /- Consider a circle of radius `r = dist w z`. -/ set r : ℝ := dist w z, have hw : w ∈ closed_ball z r, from mem_closed_ball.2 le_rfl, /- Assume the converse. Since `∥f w∥ ≤ ∥f z∥`, we have `∥f w∥ < ∥f z∥`. -/ refine (is_max_on_iff.1 hz _ hw).antisymm (not_lt.1 _), rintro hw_lt : ∥f w∥ < ∥f z∥, have hr : 0 < r, from dist_pos.2 (ne_of_apply_ne (norm ∘ f) hw_lt.ne), /- Due to Cauchy integral formula, it suffices to prove the following inequality. -/ suffices : ∥∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ∥ < 2 * π * ∥f z∥, { refine this.ne _, have A : ∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ = (2 * π * I : ℂ) • f z := hd.circle_integral_sub_inv_smul (mem_ball_self hr), simp [A, norm_smul, real.pi_pos.le] }, suffices : ∥∮ ζ in C(z, r), (ζ - z)⁻¹ • f ζ∥ < 2 * π * r * (∥f z∥ / r), by rwa [mul_assoc, mul_div_cancel' _ hr.ne'] at this, /- This inequality is true because `∥(ζ - z)⁻¹ • f ζ∥ ≤ ∥f z∥ / r` for all `ζ` on the circle and this inequality is strict at `ζ = w`. -/ have hsub : sphere z r ⊆ closed_ball z r, from sphere_subset_closed_ball, refine circle_integral.norm_integral_lt_of_norm_le_const_of_lt hr _ _ ⟨w, rfl, _⟩, show continuous_on (λ (ζ : ℂ), (ζ - z)⁻¹ • f ζ) (sphere z r), { refine ((continuous_on_id.sub continuous_on_const).inv₀ _).smul (hd.continuous_on_ball.mono hsub), exact λ ζ hζ, sub_ne_zero.2 (ne_of_mem_sphere hζ hr.ne') }, show ∀ ζ ∈ sphere z r, ∥(ζ - z)⁻¹ • f ζ∥ ≤ ∥f z∥ / r, { rintros ζ (hζ : abs (ζ - z) = r), rw [le_div_iff hr, norm_smul, norm_inv, norm_eq_abs, hζ, mul_comm, mul_inv_cancel_left₀ hr.ne'], exact hz (hsub hζ) }, show ∥(w - z)⁻¹ • f w∥ < ∥f z∥ / r, { rw [norm_smul, norm_inv, norm_eq_abs, ← div_eq_inv_mul], exact (div_lt_div_right hr).2 hw_lt } end /-! Now we drop the assumption `complete_space F` by embedding `F` into its completion. -/ lemma norm_max_aux₂ {f : ℂ → F} {z w : ℂ} (hd : diff_cont_on_cl ℂ f (ball z (dist w z))) (hz : is_max_on (norm ∘ f) (closed_ball z (dist w z)) z) : ∥f w∥ = ∥f z∥ := begin set e : F →L[ℂ] F̂ := uniform_space.completion.to_complL, have he : ∀ x, ∥e x∥ = ∥x∥, from uniform_space.completion.norm_coe, replace hz : is_max_on (norm ∘ (e ∘ f)) (closed_ball z (dist w z)) z, by simpa only [is_max_on, (∘), he] using hz, simpa only [he] using norm_max_aux₁ (e.differentiable.comp_diff_cont_on_cl hd) hz end /-! Then we replace the assumption `is_max_on (norm ∘ f) (closed_ball z r) z` with a seemingly weaker assumption `is_max_on (norm ∘ f) (ball z r) z`. -/ lemma norm_max_aux₃ {f : ℂ → F} {z w : ℂ} {r : ℝ} (hr : dist w z = r) (hd : diff_cont_on_cl ℂ f (ball z r)) (hz : is_max_on (norm ∘ f) (ball z r) z) : ∥f w∥ = ∥f z∥ := begin subst r, rcases eq_or_ne w z with rfl|hne, { refl }, rw ← dist_ne_zero at hne, exact norm_max_aux₂ hd (closure_ball z hne ▸ hz.closure hd.continuous_on.norm) end /-! Finally, we generalize the theorem from a disk in `ℂ` to a closed ball in any normed space. -/ /-- **Maximum modulus principle** on a closed ball: if `f : E → F` is continuous on a closed ball, is complex differentiable on the corresponding open ball, and the norm `∥f w∥` takes its maximum value on the open ball at its center, then the norm `∥f w∥` is constant on the closed ball. -/ lemma norm_eq_on_closed_ball_of_is_max_on {f : E → F} {z : E} {r : ℝ} (hd : diff_cont_on_cl ℂ f (ball z r)) (hz : is_max_on (norm ∘ f) (ball z r) z) : eq_on (norm ∘ f) (const E ∥f z∥) (closed_ball z r) := begin intros w hw, rw [mem_closed_ball, dist_comm] at hw, rcases eq_or_ne z w with rfl|hne, { refl }, set e : ℂ → E := line_map z w, have hde : differentiable ℂ e := (differentiable_id.smul_const (w - z)).add_const z, suffices : ∥(f ∘ e) (1 : ℂ)∥ = ∥(f ∘ e) (0 : ℂ)∥, by simpa [e], have hr : dist (1 : ℂ) 0 = 1, by simp, have hball : maps_to e (ball 0 1) (ball z r), { refine ((lipschitz_with_line_map z w).maps_to_ball (mt nndist_eq_zero.1 hne) 0 1).mono subset.rfl _, simpa only [line_map_apply_zero, mul_one, coe_nndist] using ball_subset_ball hw }, exact norm_max_aux₃ hr (hd.comp hde.diff_cont_on_cl hball) (hz.comp_maps_to hball (line_map_apply_zero z w)) end /-! ### Different forms of the maximum modulus principle -/ /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a set `s`, the norm of `f` takes it maximum on `s` at `z` and `w` is a point such that the closed ball with center `z` and radius `dist w z` is included in `s`, then `∥f w∥ = ∥f z∥`. -/ lemma norm_eq_norm_of_is_max_on_of_closed_ball_subset {f : E → F} {s : set E} {z w : E} (hd : diff_cont_on_cl ℂ f s) (hz : is_max_on (norm ∘ f) s z) (hsub : ball z (dist w z) ⊆ s) : ∥f w∥ = ∥f z∥ := norm_eq_on_closed_ball_of_is_max_on (hd.mono hsub) (hz.on_subset hsub) (mem_closed_ball.2 le_rfl) /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable in a neighborhood of `c` and the norm `∥f z∥` has a local maximum at `c`, then `∥f z∥` is locally constant in a neighborhood of `c`. -/ lemma norm_eventually_eq_of_is_local_max {f : E → F} {c : E} (hd : ∀ᶠ z in 𝓝 c, differentiable_at ℂ f z) (hc : is_local_max (norm ∘ f) c) : ∀ᶠ y in 𝓝 c, ∥f y∥ = ∥f c∥ := begin rcases nhds_basis_closed_ball.eventually_iff.1 (hd.and hc) with ⟨r, hr₀, hr⟩, exact nhds_basis_closed_ball.eventually_iff.2 ⟨r, hr₀, norm_eq_on_closed_ball_of_is_max_on (differentiable_on.diff_cont_on_cl $ λ x hx, (hr $ closure_ball_subset_closed_ball hx).1.differentiable_within_at) (λ x hx, (hr $ ball_subset_closed_ball hx).2)⟩ end lemma is_open_set_of_mem_nhds_and_is_max_on_norm {f : E → F} {s : set E} (hd : differentiable_on ℂ f s) : is_open {z | s ∈ 𝓝 z ∧ is_max_on (norm ∘ f) s z} := begin refine is_open_iff_mem_nhds.2 (λ z hz, (eventually_eventually_nhds.2 hz.1).and _), replace hd : ∀ᶠ w in 𝓝 z, differentiable_at ℂ f w, from hd.eventually_differentiable_at hz.1, exact (norm_eventually_eq_of_is_local_max hd $ (hz.2.is_local_max hz.1)).mono (λ x hx y hy, le_trans (hz.2 hy) hx.ge) end /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a nonempty bounded set `U` and is continuous on its closure, then there exists a point `z ∈ frontier U` such that `λ z, ∥f z∥` takes it maximum value on `closure U` at `z`. -/ lemma exists_mem_frontier_is_max_on_norm [nontrivial E] [finite_dimensional ℂ E] {f : E → F} {U : set E} (hb : bounded U) (hne : U.nonempty) (hd : diff_cont_on_cl ℂ f U) : ∃ z ∈ frontier U, is_max_on (norm ∘ f) (closure U) z := begin have hc : is_compact (closure U), from hb.is_compact_closure, obtain ⟨w, hwU, hle⟩ : ∃ w ∈ closure U, is_max_on (norm ∘ f) (closure U) w, from hc.exists_forall_ge hne.closure hd.continuous_on.norm, rw [closure_eq_interior_union_frontier, mem_union_eq] at hwU, cases hwU, rotate, { exact ⟨w, hwU, hle⟩ }, have : interior U ≠ univ, from ne_top_of_le_ne_top hc.ne_univ interior_subset_closure, rcases exists_mem_frontier_inf_dist_compl_eq_dist hwU this with ⟨z, hzU, hzw⟩, refine ⟨z, frontier_interior_subset hzU, λ x hx, (mem_set_of_eq.mp $ hle hx).trans_eq _⟩, refine (norm_eq_norm_of_is_max_on_of_closed_ball_subset hd (hle.on_subset subset_closure) _).symm, rw [dist_comm, ← hzw], exact ball_inf_dist_compl_subset.trans interior_subset end /-- **Maximum modulus principle**: if `f : E → F` is complex differentiable on a bounded set `U` and `∥f z∥ ≤ C` for any `z ∈ frontier U`, then the same is true for any `z ∈ closure U`. -/ lemma norm_le_of_forall_mem_frontier_norm_le [nontrivial E] {f : E → F} {U : set E} (hU : bounded U) (hd : diff_cont_on_cl ℂ f U) {C : ℝ} (hC : ∀ z ∈ frontier U, ∥f z∥ ≤ C) {z : E} (hz : z ∈ closure U) : ∥f z∥ ≤ C := begin rw [closure_eq_self_union_frontier, union_comm, mem_union_eq] at hz, cases hz, { exact hC z hz }, /- In case of a finite dimensional domain, one can just apply `complex.exists_mem_frontier_is_max_on_norm`. To make it work in any Banach space, we restrict the function to a line first. -/ rcases exists_ne z with ⟨w, hne⟩, set e : ℂ → E := line_map z w, have hde : differentiable ℂ e := (differentiable_id.smul_const (w - z)).add_const z, have hL : antilipschitz_with (nndist z w)⁻¹ e, from antilipschitz_with_line_map hne.symm, replace hd : diff_cont_on_cl ℂ (f ∘ e) (e ⁻¹' U), from hd.comp hde.diff_cont_on_cl (maps_to_preimage _ _), have h₀ : (0 : ℂ) ∈ e ⁻¹' U, by simpa only [e, mem_preimage, line_map_apply_zero], rcases exists_mem_frontier_is_max_on_norm (hL.bounded_preimage hU) ⟨0, h₀⟩ hd with ⟨ζ, hζU, hζ⟩, calc ∥f z∥ = ∥f (e 0)∥ : by simp only [e, line_map_apply_zero] ... ≤ ∥f (e ζ)∥ : hζ (subset_closure h₀) ... ≤ C : hC _ (hde.continuous.frontier_preimage_subset _ hζU) end /-- If two complex differentiable functions `f g : E → F` are equal on the boundary of a bounded set `U`, then they are equal on `closure U`. -/ lemma eq_on_closure_of_eq_on_frontier [nontrivial E] {f g : E → F} {U : set E} (hU : bounded U) (hf : diff_cont_on_cl ℂ f U) (hg : diff_cont_on_cl ℂ g U) (hfg : eq_on f g (frontier U)) : eq_on f g (closure U) := begin suffices H : ∀ z ∈ closure U, ∥(f - g) z∥ ≤ 0, by simpa [sub_eq_zero] using H, refine λ z hz, norm_le_of_forall_mem_frontier_norm_le hU (hf.sub hg) (λ w hw, _) hz, simp [hfg hw] end /-- If two complex differentiable functions `f g : E → F` are equal on the boundary of a bounded set `U`, then they are equal on `U`. -/ lemma eq_on_of_eq_on_frontier [nontrivial E] {f g : E → F} {U : set E} (hU : bounded U) (hf : diff_cont_on_cl ℂ f U) (hg : diff_cont_on_cl ℂ g U) (hfg : eq_on f g (frontier U)) : eq_on f g U := (eq_on_closure_of_eq_on_frontier hU hf hg hfg).mono subset_closure end complex
1579e2c2925ca4756fa658cd3d31ac3f579ee96c
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/measure_theory/set_integral.lean
938fde42e3f5d74e8ede65183365a3a34551aa8f
[ "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,408
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import measure_theory.bochner_integration import analysis.normed_space.indicator_function /-! # Set integral In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable function `f` and a measurable set `s` this definition coincides with another natural definition: `∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s` and is zero otherwise. Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ` directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g. `integral_union`, `integral_empty`, `integral_univ`. We also define `integrable_on f s μ := integrable f (μ.restrict s)` and prove theorems like `integrable_on_union : integrable_on f (s ∪ t) μ ↔ integrable_on f s μ ∧ integrable_on f t μ`. Next we define a predicate `integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)` saying that `f` is integrable at some set `s ∈ l` and prove that a measurable function is integrable at `l` with respect to `μ` provided that `f` is bounded above at `l ⊓ μ.ae` and `μ` is finite at `l`. Finally, we prove a version of the [Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus) for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries. Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)` as `s` tends to `l.lift' powerset`, i.e. for any `ε>0` there exists `t ∈ l` such that `∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`. ## Notation `∫ a in s, f a` is `measure_theory.integral (s.indicator f)` ## TODO The file ends with over a hundred lines of commented out code. This is the old contents of this file using the `indicator` approach to the definition of `∫ x in s, f x ∂μ`. This code should be migrated to the new definition. -/ noncomputable theory open set filter topological_space measure_theory function open_locale classical topological_space interval big_operators filter variables {α β E F : Type*} [measurable_space α] section piecewise variables {μ : measure α} {s : set α} {f g : α → β} lemma piecewise_ae_eq_restrict (hs : is_measurable s) : piecewise s f g =ᵐ[μ.restrict s] f := begin rw [ae_restrict_eq hs], exact (piecewise_eq_on s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_restrict_compl (hs : is_measurable s) : piecewise s f g =ᵐ[μ.restrict sᶜ] g := begin rw [ae_restrict_eq hs.compl], exact (piecewise_eq_on_compl s f g).eventually_eq.filter_mono inf_le_right end end piecewise section indicator_function variables [has_zero β] {μ : measure α} {s : set α} {f : α → β} lemma indicator_ae_eq_restrict (hs : is_measurable s) : indicator s f =ᵐ[μ.restrict s] f := piecewise_ae_eq_restrict hs lemma indicator_ae_eq_restrict_compl (hs : is_measurable s) : indicator s f =ᵐ[μ.restrict sᶜ] 0 := piecewise_ae_eq_restrict_compl hs end indicator_function namespace measure_theory section normed_group lemma has_finite_integral_restrict_of_bounded [normed_group E] {f : α → E} {s : set α} {μ : measure α} {C} (hs : μ s < ⊤) (hf : ∀ᵐ x ∂(μ.restrict s), ∥f x∥ ≤ C) : has_finite_integral f (μ.restrict s) := by haveI : finite_measure (μ.restrict s) := ⟨by rwa [measure.restrict_apply_univ]⟩; exact has_finite_integral_of_bounded hf variables [normed_group E] [measurable_space E] {f : α → E} {s t : set α} {μ ν : measure α} /-- A function is `integrable_on` a set `s` if it is a measurable function and if the integral of its pointwise norm over `s` is less than infinity. -/ def integrable_on (f : α → E) (s : set α) (μ : measure α . volume_tac) : Prop := integrable f (μ.restrict s) lemma integrable_on.integrable (h : integrable_on f s μ) : integrable f (μ.restrict s) := h @[simp] lemma integrable_on_empty (hf : measurable f) : integrable_on f ∅ μ := by simp [integrable_on, measurable.integrable_zero hf] @[simp] lemma integrable_on_univ : integrable_on f univ μ ↔ integrable f μ := by rw [integrable_on, measure.restrict_univ] lemma integrable_on_zero : integrable_on (λ _, (0:E)) s μ := integrable_zero _ _ _ lemma integrable_on_const {C : E} : integrable_on (λ _, C) s μ ↔ C = 0 ∨ μ s < ⊤ := integrable_const_iff.trans $ by rw [measure.restrict_apply_univ] lemma integrable_on.mono (h : integrable_on f t ν) (hs : s ⊆ t) (hμ : μ ≤ ν) : integrable_on f s μ := h.mono_measure $ measure.restrict_mono hs hμ lemma integrable_on.mono_set (h : integrable_on f t μ) (hst : s ⊆ t) : integrable_on f s μ := h.mono hst (le_refl _) lemma integrable_on.mono_measure (h : integrable_on f s ν) (hμ : μ ≤ ν) : integrable_on f s μ := h.mono (subset.refl _) hμ lemma integrable_on.mono_set_ae (h : integrable_on f t μ) (hst : s ≤ᵐ[μ] t) : integrable_on f s μ := h.integrable.mono_measure $ restrict_mono_ae hst lemma integrable.integrable_on (h : integrable f μ) : integrable_on f s μ := h.mono_measure $ measure.restrict_le_self lemma integrable.integrable_on' (h : integrable f (μ.restrict s)) : integrable_on f s μ := h lemma integrable_on.left_of_union (h : integrable_on f (s ∪ t) μ) : integrable_on f s μ := h.mono_set $ subset_union_left _ _ lemma integrable_on.right_of_union (h : integrable_on f (s ∪ t) μ) : integrable_on f t μ := h.mono_set $ subset_union_right _ _ lemma integrable_on.union (hs : integrable_on f s μ) (ht : integrable_on f t μ) : integrable_on f (s ∪ t) μ := (hs.add_measure ht).mono_measure $ measure.restrict_union_le _ _ @[simp] lemma integrable_on_union : integrable_on f (s ∪ t) μ ↔ integrable_on f s μ ∧ integrable_on f t μ := ⟨λ h, ⟨h.left_of_union, h.right_of_union⟩, λ h, h.1.union h.2⟩ @[simp] lemma integrable_on_finite_union (hf : measurable f) {s : set β} (hs : finite s) {t : β → set α} : integrable_on f (⋃ i ∈ s, t i) μ ↔ ∀ i ∈ s, integrable_on f (t i) μ := begin apply hs.induction_on, { simp [hf] }, { intros a s ha hs hf, simp [hf, or_imp_distrib, forall_and_distrib] } end @[simp] lemma integrable_on_finset_union (hf : measurable f) {s : finset β} {t : β → set α} : integrable_on f (⋃ i ∈ s, t i) μ ↔ ∀ i ∈ s, integrable_on f (t i) μ := integrable_on_finite_union hf s.finite_to_set lemma integrable_on.add_measure (hμ : integrable_on f s μ) (hν : integrable_on f s ν) : integrable_on f s (μ + ν) := by { delta integrable_on, rw measure.restrict_add, exact hμ.integrable.add_measure hν } @[simp] lemma integrable_on_add_measure : integrable_on f s (μ + ν) ↔ integrable_on f s μ ∧ integrable_on f s ν := ⟨λ h, ⟨h.mono_measure (measure.le_add_right (le_refl _)), h.mono_measure (measure.le_add_left (le_refl _))⟩, λ h, h.1.add_measure h.2⟩ lemma integrable_indicator_iff (hf : measurable f) (hs : is_measurable s) : integrable (indicator s f) μ ↔ integrable_on f s μ := by simp only [integrable_on, integrable, has_finite_integral, nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator, lintegral_indicator _ hs, hf, hf.indicator hs] lemma integrable_on.indicator (h : integrable_on f s μ) (hs : is_measurable s) : integrable (indicator s f) μ := (integrable_indicator_iff h.measurable hs).2 h /-- We say that a function `f` is *integrable at filter* `l` if it is integrable on some set `s ∈ l`. Equivalently, it is eventually integrable on `s` in `l.lift' powerset`. -/ def integrable_at_filter (f : α → E) (l : filter α) (μ : measure α . volume_tac) := ∃ s ∈ l, integrable_on f s μ variables {l l' : filter α} protected lemma integrable_at_filter.eventually (h : integrable_at_filter f l μ) : ∀ᶠ s in l.lift' powerset, integrable_on f s μ := by { refine (eventually_lift'_powerset' $ λ s t hst ht, _).2 h, exact ht.mono_set hst } lemma integrable_at_filter.filter_mono (hl : l ≤ l') (hl' : integrable_at_filter f l' μ) : integrable_at_filter f l μ := let ⟨s, hs, hsf⟩ := hl' in ⟨s, hl hs, hsf⟩ lemma integrable_at_filter.inf_of_left (hl : integrable_at_filter f l μ) : integrable_at_filter f (l ⊓ l') μ := hl.filter_mono inf_le_left lemma integrable_at_filter.inf_of_right (hl : integrable_at_filter f l μ) : integrable_at_filter f (l' ⊓ l) μ := hl.filter_mono inf_le_right @[simp] lemma integrable_at_filter.inf_ae_iff {l : filter α} : integrable_at_filter f (l ⊓ μ.ae) μ ↔ integrable_at_filter f l μ := begin refine ⟨_, λ h, h.filter_mono inf_le_left⟩, rintros ⟨s, ⟨t, ht, u, hu, hs⟩, hf⟩, refine ⟨t, ht, _⟩, refine hf.integrable.mono_measure (λ v hv, _), simp only [measure.restrict_apply hv], refine measure_mono_ae (mem_sets_of_superset hu $ λ x hx, _), exact λ ⟨hv, ht⟩, ⟨hv, hs ⟨ht, hx⟩⟩ end alias integrable_at_filter.inf_ae_iff ↔ measure_theory.integrable_at_filter.of_inf_ae _ /-- If `μ` is a measure finite at filter `l` and `f` is a function such that its norm is bounded above at `l`, then `f` is integrable at `l`. -/ lemma measure.finite_at_filter.integrable_at_filter (hfm : measurable f) {l : filter α} [is_measurably_generated l] (hμ : μ.finite_at_filter l) (hf : l.is_bounded_under (≤) (norm ∘ f)) : integrable_at_filter f l μ := begin rcases hμ with ⟨s, hsl, hsμ⟩, rcases hf with ⟨C, hC⟩, simp only [eventually_map] at hC, rcases hC.exists_measurable_mem with ⟨t, htl, htm, hC⟩, refine ⟨t ∩ s, inter_mem_sets htl hsl, _⟩, refine ⟨hfm, has_finite_integral_restrict_of_bounded (lt_of_le_of_lt (measure_mono $ inter_subset_right _ _) hsμ) _⟩, exact C, suffices : ∀ᵐ x ∂μ.restrict t, ∥f x∥ ≤ C, from ae_mono (measure.restrict_mono (inter_subset_left _ _) (le_refl _)) this, rw [ae_restrict_eq htm, eventually_inf_principal], exact eventually_of_forall hC end lemma measure.finite_at_filter.integrable_at_filter_of_tendsto_ae (hfm : measurable f) {l : filter α} [is_measurably_generated l] (hμ : μ.finite_at_filter l) {b} (hf : tendsto f (l ⊓ μ.ae) (𝓝 b)) : integrable_at_filter f l μ := (hμ.inf_of_left.integrable_at_filter hfm hf.norm.is_bounded_under_le).of_inf_ae alias measure.finite_at_filter.integrable_at_filter_of_tendsto_ae ← filter.tendsto.integrable_at_filter_ae lemma measure.finite_at_filter.integrable_at_filter_of_tendsto (hfm : measurable f) {l : filter α} [is_measurably_generated l] (hμ : μ.finite_at_filter l) {b} (hf : tendsto f l (𝓝 b)) : integrable_at_filter f l μ := hμ.integrable_at_filter hfm hf.norm.is_bounded_under_le alias measure.finite_at_filter.integrable_at_filter_of_tendsto ← filter.tendsto.integrable_at_filter variables [borel_space E] [second_countable_topology E] lemma integrable_add [opens_measurable_space E] {f g : α → E} (h : univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0}) (hf : measurable f) (hg : measurable g) : integrable (f + g) μ ↔ integrable f μ ∧ integrable g μ := begin refine ⟨λ hfg, _, λ h, h.1.add h.2⟩, rw [← indicator_add_eq_left h], conv { congr, skip, rw [← indicator_add_eq_right h] }, rw [integrable_indicator_iff _ (hf (is_measurable_singleton 0)).compl], rw [integrable_indicator_iff _ (hg (is_measurable_singleton 0)).compl], exact ⟨hfg.integrable_on, hfg.integrable_on⟩, exact hf.add hg, exact hf.add hg end /-- To prove something for an arbitrary measurable + integrable function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `L¹` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_sum` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma integrable.induction (P : (α → E) → Prop) (h_ind : ∀ (c : E) ⦃s⦄, is_measurable s → μ s < ⊤ → P (s.indicator (λ _, c))) (h_sum : ∀ ⦃f g : α → E⦄, set.univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0} → integrable f μ → integrable g μ → P f → P g → P (f + g)) (h_closed : is_closed {f : α →₁[μ] E | P f} ) (h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → integrable f μ → measurable g → P f → P g) : ∀ ⦃f : α → E⦄ (hf : integrable f μ), P f := begin have : ∀ (f : simple_func α E), integrable f μ → P f, { refine simple_func.induction _ _, { intros c s hs h, dsimp only [simple_func.coe_const, simple_func.const_zero, piecewise_eq_indicator, simple_func.coe_zero, simple_func.coe_piecewise] at h ⊢, by_cases hc : c = 0, { subst hc, convert h_ind 0 is_measurable.empty (by simp) using 1, simp [const] }, apply h_ind c hs, have : (nnnorm c : ennreal) * μ s < ⊤, { have := @comp_indicator _ _ _ _ (λ x : E, (nnnorm x : ennreal)) (const α c) s, dsimp only at this, have h' := h.has_finite_integral, simpa [has_finite_integral, this, lintegral_indicator, hs] using h' }, exact ennreal.lt_top_of_mul_lt_top_right this (by simp [hc]) }, { intros f g hfg hf hg int_fg, rw [simple_func.coe_add, integrable_add hfg f.measurable g.measurable] at int_fg, refine h_sum hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } }, have : ∀ (f : α →₁ₛ[μ] E), P f, { intro f, exact h_ae f.to_simple_func_eq_to_fun f.integrable (l1.measurable _) (this f.to_simple_func f.integrable) }, have : ∀ (f : α →₁[μ] E), P f := λ f, l1.simple_func.dense_range.induction_on f h_closed this, exact λ f hf, h_ae (l1.to_fun_of_fun f hf) (l1.integrable _) hf.measurable (this (l1.of_fun f hf)) end variables [complete_space E] [normed_space ℝ E] lemma integral_union (hst : disjoint s t) (hs : is_measurable s) (ht : is_measurable t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := by simp only [integrable_on, measure.restrict_union hst hs ht, integral_add_measure hfs hft] lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure] lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ] lemma integral_add_compl (hs : is_measurable s) (hfi : integrable f μ) : ∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ := by rw [← integral_union (disjoint_compl_right s) hs hs.compl hfi.integrable_on hfi.integrable_on, union_compl_self, integral_univ] /-- For a measurable function `f` and a measurable set `s`, the integral of `indicator s f` over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/ lemma integral_indicator (hfm : measurable f) (hs : is_measurable s) : ∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ := have hfms : measurable (indicator s f) := hfm.indicator hs, if hfi : integrable_on f s μ then calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ : (integral_add_compl hs (hfi.indicator hs)).symm ... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ : congr_arg2 (+) (integral_congr_ae hfms hfm (indicator_ae_eq_restrict hs)) (integral_congr_ae hfms measurable_const (indicator_ae_eq_restrict_compl hs)) ... = ∫ x in s, f x ∂μ : by simp else by { rwa [integral_undef, integral_undef], rwa integrable_indicator_iff hfm hs } lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c := by rw [integral_const, measure.restrict_apply_univ] @[simp] lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : is_measurable s) : ∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e := by rw [integral_indicator measurable_const s_meas, ← set_integral_const] lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β} (hs : is_measurable s) (hf : measurable f) (hg : measurable g) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := by rw [measure.restrict_map hg hs, integral_map_measure hg hf] lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ⊤) (hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin rw ← measure.restrict_apply_univ at *, haveI : finite_measure (μ.restrict s) := ⟨‹_›⟩, exact norm_integral_le_of_norm_le_const hC end lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ⊤) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : measurable f) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae hs $ (ae_restrict_iff $ hfm.norm is_measurable_Iic).2 hC lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ⊤) (hsm : is_measurable s) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal] lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ⊤) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : measurable f) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ⊤) (hsm : is_measurable s) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC end normed_group end measure_theory open measure_theory asymptotics metric variables [measurable_space E] [normed_group E] /-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in s, f x ∂μ = μ s • b + o(μ s)` as `s` tends to `l.lift' powerset`. Since `μ s` is an `ennreal` number, we use `(μ s).to_real` in the actual statement. -/ lemma filter.tendsto.integral_sub_linear_is_o_ae [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} {l : filter α} [l.is_measurably_generated] {f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b)) (hfm : measurable f) (hμ : μ.finite_at_filter l) : is_o (λ s : set α, ∫ x in s, f x ∂μ - (μ s).to_real • b) (λ s, (μ s).to_real) (l.lift' powerset) := begin simp only [is_o_iff], intros ε ε₀, have : ∀ᶠ s in l.lift' powerset, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε := eventually_lift'_powerset_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀), refine hμ.eventually.mp ((h.integrable_at_filter_ae hfm hμ).eventually.mp (this.mono _)), simp only [mem_closed_ball, dist_eq_norm], intros s h_norm h_integrable hμs, rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs), real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub measurable_const) end /-- If a function is integrable at `𝓝[s] x` for each point `x` of a compact set `s`, then it is integrable on `s`. -/ lemma is_compact.integrable_on_of_nhds_within [topological_space α] {μ : measure α} {s : set α} (hs : is_compact s) {f : α → E} (hfm : measurable f) (hf : ∀ x ∈ s, integrable_at_filter f (𝓝[s] x) μ) : integrable_on f s μ := is_compact.induction_on hs (integrable_on_empty hfm) (λ s t hst ht, ht.mono_set hst) (λ s t hs ht, hs.union ht) hf /-- A function `f` continuous on a compact set `s` is integrable on this set with respect to any locally finite measure. -/ lemma continuous_on.integrable_on_compact [topological_space α] [opens_measurable_space α] [t2_space α] {μ : measure α} [locally_finite_measure μ] {s : set α} (hs : is_compact s) {f : α → E} (hfm : measurable f) (hf : continuous_on f s) : integrable_on f s μ := hs.integrable_on_of_nhds_within hfm $ λ x hx, by haveI := hs.is_measurable.nhds_within_is_measurably_generated; exact (hf x hx).integrable_at_filter hfm (μ.finite_at_nhds_within _ _) /-- A continuous function `f` is integrable on any compact set with respect to any locally finite measure. -/ lemma continuous.integrable_on_compact [topological_space α] [opens_measurable_space α] [t2_space α] [borel_space E] {μ : measure α} [locally_finite_measure μ] {s : set α} (hs : is_compact s) {f : α → E} (hf : continuous f) : integrable_on f s μ := hf.continuous_on.integrable_on_compact hs hf.measurable /-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite measure that and `f` is a measurable function that is continuous at a point `a`, then `∫ x in s, f x ∂μ = μ s • f a + o(μ s)` as `s` tends to `(𝓝 a).lift' powerset`. Since `μ s` is an `ennreal` number, we use `(μ s).to_real` in the actual statement. -/ lemma continuous_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} [locally_finite_measure μ] {a : α} {f : α → E} (ha : continuous_at f a) (hfm : measurable f) : is_o (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • f a) (λ s, (μ s).to_real) ((𝓝 a).lift' powerset) := (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) section /-! ### Continuous linear maps composed with integration The goal of this section is to prove that integration commutes with continuous linear maps. The first step is to prove that, given a function `φ : α → E` which is measurable and integrable, and a continuous linear map `L : E →L[ℝ] F`, the function `λ a, L(φ a)` is also measurable and integrable. Note we cannot write this as `L ∘ φ` since the type of `L` is not an actual function type. The next step is translate this to `l1`, replacing the function `φ` by a term with type `α →₁[μ] E` (an equivalence class of integrable functions). The corresponding "composition" is `L.comp_l1 φ : α →₁[μ] F`. This is then upgraded to a linear map `L.comp_l1ₗ : (α →₁[μ] E) →ₗ[ℝ] (α →₁[μ] F)` and a continuous linear map `L.comp_l1L : (α →₁[μ] E) →L[ℝ] (α →₁[μ] F)`. Then we can prove the commutation result using continuity of all relevant operations and the result on simple functions. -/ variables {μ : measure α} [normed_space ℝ E] variables [normed_group F] [normed_space ℝ F] namespace continuous_linear_map lemma norm_comp_l1_apply_le [opens_measurable_space E] [second_countable_topology E] (φ : α →₁[μ] E) (L : E →L[ℝ] F) : ∀ᵐ a ∂μ, ∥L (φ a)∥ ≤ ∥L∥ * ∥φ a∥ := eventually_of_forall (λ a, L.le_op_norm (φ a)) variables [measurable_space F] [borel_space F] lemma integrable_comp [opens_measurable_space E] {φ : α → E} (L : E →L[ℝ] F) (φ_int : integrable φ μ) : integrable (λ (a : α), L (φ a)) μ := ((integrable.norm φ_int).const_mul ∥L∥).mono' (L.measurable.comp φ_int.measurable) (eventually_of_forall $ λ a, L.le_op_norm (φ a)) variables [borel_space E] [second_countable_topology E] /-- Composing `φ : α →₁[μ] E` with `L : E →L[ℝ] F`. -/ def comp_l1 [second_countable_topology F] (L : E →L[ℝ] F) (φ : α →₁[μ] E) : α →₁[μ] F := l1.of_fun (λ a, L (φ a)) (L.integrable_comp φ.integrable) lemma comp_l1_apply [second_countable_topology F] (L : E →L[ℝ] F) (φ : α →₁[μ] E) : ∀ᵐ a ∂μ, (L.comp_l1 φ) a = L (φ a) := l1.to_fun_of_fun _ _ lemma integrable_comp_l1 (L : E →L[ℝ] F) (φ : α →₁[μ] E) : integrable (λ a, L (φ a)) μ := L.integrable_comp φ.integrable lemma measurable_comp_l1 (L : E →L[ℝ] F) (φ : α →₁[μ] E) : measurable (λ a, L (φ a)) := L.measurable.comp φ.measurable variables [second_countable_topology F] lemma integral_comp_l1 [complete_space F] (L : E →L[ℝ] F) (φ : α →₁[μ] E) : ∫ a, (L.comp_l1 φ) a ∂μ = ∫ a, L (φ a) ∂μ := by simp [comp_l1] /-- Composing `φ : α →₁[μ] E` with `L : E →L[ℝ] F`, seen as a `ℝ`-linear map on `α →₁[μ] E`. -/ def comp_l1ₗ (L : E →L[ℝ] F) : (α →₁[μ] E) →ₗ[ℝ] (α →₁[μ] F) := { to_fun := λ φ, L.comp_l1 φ, map_add' := begin intros f g, dsimp [comp_l1], rw [← l1.of_fun_add, l1.of_fun_eq_of_fun], apply (l1.add_to_fun f g).mono, intros a ha, simp only [ha, pi.add_apply, L.map_add] end, map_smul' := begin intros c f, dsimp [comp_l1], rw [← l1.of_fun_smul, l1.of_fun_eq_of_fun], apply (l1.smul_to_fun c f).mono, intros a ha, simp only [ha, pi.smul_apply, continuous_linear_map.map_smul] end } lemma norm_comp_l1_le (φ : α →₁[μ] E) (L : E →L[ℝ] F) : ∥L.comp_l1 φ∥ ≤ ∥L∥*∥φ∥ := begin erw l1.norm_of_fun_eq_integral_norm, calc ∫ a, ∥L (φ a)∥ ∂μ ≤ ∫ a, ∥L∥ *∥φ a∥ ∂μ : integral_mono_ae (L.integrable_comp_l1 φ).norm (φ.integrable_norm.const_mul $ ∥L∥) (L.norm_comp_l1_apply_le φ) ... = ∥L∥ * ∥φ∥ : by rw [integral_mul_left, φ.norm_eq_integral_norm] end /-- Composing `φ : α →₁[μ] E` with `L : E →L[ℝ] F`, seen as a continuous `ℝ`-linear map on `α →₁[μ] E`. -/ def comp_l1L (L : E →L[ℝ] F) : (α →₁[μ] E) →L[ℝ] (α →₁[μ] F) := linear_map.mk_continuous L.comp_l1ₗ (∥L∥) (λ φ, L.norm_comp_l1_le φ) lemma norm_compl1L_le (L : E →L[ℝ] F) : ∥(L.comp_l1L : (α →₁[μ] E) →L[ℝ] (α →₁[μ] F))∥ ≤ ∥L∥ := op_norm_le_bound _ (norm_nonneg _) (λ φ, L.norm_comp_l1_le φ) variables [complete_space F] lemma continuous_integral_comp_l1 (L : E →L[ℝ] F) : continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) := begin rw ← funext L.integral_comp_l1, exact continuous_integral.comp L.comp_l1L.continuous end variables [complete_space E] lemma integral_comp_comm (L : E →L[ℝ] F) {φ : α → E} (φ_int : integrable φ μ) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)), { intros e s s_meas s_finite, rw [integral_indicator_const e s_meas, continuous_linear_map.map_smul, ← integral_indicator_const (L e) s_meas], congr' 1 with a, rw set.indicator_comp_of_zero L.map_zero }, { intros f g H f_int g_int hf hg, simp [L.map_add, integral_add f_int g_int, integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] }, { exact is_closed_eq L.continuous_integral_comp_l1 (L.continuous.comp continuous_integral) }, { intros f g hfg f_int g_meas hf, convert hf using 1 ; clear hf, { exact integral_congr_ae (L.measurable.comp g_meas) (L.measurable.comp f_int.measurable) (hfg.fun_comp L).symm }, { rw integral_congr_ae g_meas f_int.measurable hfg.symm } }, all_goals { assumption } end lemma integral_comp_l1_comm (L : E →L[ℝ] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.integral_comp_comm φ.integrable end continuous_linear_map end /- namespace integrable variables [measurable_space α] [measurable_space β] [normed_group E] protected lemma measure_mono end integrable end measure_theory section integral_on variables [measurable_space α] [normed_group β] [second_countable_topology β] [normed_space ℝ β] [complete_space β] [measurable_space β] [borel_space β] {s t : set α} {f g : α → β} {μ : measure α} open set lemma integral_on_congr (hf : measurable f) (hg : measurable g) (hs : is_measurable s) (h : ∀ᵐ a ∂μ, a ∈ s → f a = g a) : ∫ a in s, f a ∂μ = ∫ a in s, g a ∂μ := integral_congr_ae hf hg $ _ lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f) (h : ∀ᵐ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) := integral_congr_ae hsm htm $ indicator_congr_of_set h lemma integral_on_add {s : set α} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) := by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi } lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) := by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi } lemma integral_on_le_integral_on_ae {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ᵐ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := begin apply integral_le_integral_ae hfm hfi hgm hgi, apply indicator_le_indicator_ae, exact h end lemma integral_on_le_integral_on {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] } lemma integral_on_union_ae (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : ∀ᵐ a, a ∉ s ∩ t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := begin have := integral_congr_ae _ _ (indicator_union_ae h f), rw [this, integral_add hsm hsi htm hti], { exact hsm.union hs ht htm }, { exact measurable.add hsm htm } end lemma integral_on_nonneg_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_nonneg_of_ae $ by { filter_upwards [hf] λ a h, indicator_nonneg' h } lemma integral_on_nonneg {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_on_nonneg_of_ae $ univ_mem_sets' hf lemma integral_on_nonpos_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] λ a h, indicator_nonpos' h } lemma integral_on_nonpos {f : α → ℝ} (hf : ∀ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_on_nonpos_of_ae $ univ_mem_sets' hf lemma tendsto_integral_on_of_monotone {s : ℕ → set α} {f : α → β} (hsm : ∀i, is_measurable (s i)) (h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) := let bound : α → ℝ := indicator (Union s) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, exact hfm.subset (hsm i) (subset_Union _ _) }, { assumption }, { show integrable_on (Union s) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], exact indicator_le_indicator_of_subset (subset_Union _ _) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) } end lemma tendsto_integral_on_of_antimono (s : ℕ → set α) (f : α → β) (hsm : ∀i, is_measurable (s i)) (h_mono : ∀i j, i ≤ j → s j ⊆ s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) := let bound : α → ℝ := indicator (s 0) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) }, { exact hfm.subset (is_measurable.Inter hsm) (Inter_subset _ _) }, { show integrable_on (s 0) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) } end -- TODO : prove this for an encodable type -- by proving an encodable version of `filter.is_countably_generated_at_top_finset_nat ` lemma integral_on_Union (s : ℕ → set α) (f : α → β) (hm : ∀i, is_measurable (s i)) (hd : ∀ i j, i ≠ j → s i ∩ s j = ∅) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : (∫ a in (Union s), f a) = ∑'i, ∫ a in s i, f a := suffices h : tendsto (λn:finset ℕ, ∑ i in n, ∫ a in s i, f a) at_top (𝓝 $ (∫ a in (Union s), f a)), by { rwa has_sum.tsum_eq }, begin have : (λn:finset ℕ, ∑ i in n, ∫ a in s i, f a) = λn:finset ℕ, ∫ a in (⋃i∈n, s i), f a, { funext, rw [← integral_finset_sum, indicator_finset_bUnion], { assume i hi j hj hij, exact hd i j hij }, { assume i, refine hfm.subset (hm _) (subset_Union _ _) }, { assume i, refine hfi.subset (subset_Union _ _) } }, rw this, refine tendsto_integral_filter_of_dominated_convergence _ _ _ _ _ _ _, { exact indicator (Union s) (λ a, ∥f a∥) }, { exact is_countably_generated_at_top_finset_nat }, { refine univ_mem_sets' (λ n, _), simp only [mem_set_of_eq], refine hfm.subset (is_measurable.Union (λ i, is_measurable.Union_Prop (λh, hm _))) (bUnion_subset_Union _ _), }, { assumption }, { refine univ_mem_sets' (λ n, univ_mem_sets' $ _), simp only [mem_set_of_eq], assume a, rw ← norm_indicator_eq_indicator_norm, refine norm_indicator_le_of_subset (bUnion_subset_Union _ _) _ _ }, { rw [← integrable_on, integrable_on_norm_iff], assumption }, { filter_upwards [] λa, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) } end end integral_on -/
bd5c837ef2f5adcb6ba61ad8700730e69855afb3
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/normed_space/multilinear.lean
d8c2513325806ead9380bb5e4a8bc15e8bc2f989
[ "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
72,380
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.operator_norm import topology.algebra.module.multilinear /-! # Operator norm on the space of continuous multilinear maps When `f` is a continuous multilinear map in finitely many variables, we define its norm `∥f∥` as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that it is indeed a norm, and prove its basic properties. ## Main results Let `f` be a multilinear map in finitely many variables. * `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0` with `∥f m∥ ≤ C * ∏ i, ∥m i∥` for all `m`. * `continuous_of_bound`, conversely, asserts that this bound implies continuity. * `mk_continuous` constructs the associated continuous multilinear map. Let `f` be a continuous multilinear map in finitely many variables. * `∥f∥` is its norm, i.e., the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. * `le_op_norm f m` asserts the fundamental inequality `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥`. * `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of `∥f∥` and `∥m₁ - m₂∥`. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a continuous multilinear function `f` in `n+1` variables into a continuous linear function taking values in continuous multilinear functions in `n` variables, and also into a continuous multilinear function in `n` variables taking values in continuous linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). They induce continuous linear equivalences between spaces of continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n` variables taking values in continuous linear functions), called respectively `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. ## Implementation notes We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity that we should deal with multilinear maps in several variables. The currying/uncurrying constructions are based on those in `multilinear.lean`. From the mathematical point of view, all the results follow from the results on operator norm in one variable, by applying them to one variable after the other through currying. However, this is only well defined when there is an order on the variables (for instance on `fin n`) although the final result is independent of the order. While everything could be done following this approach, it turns out that direct proofs are easier and more efficient. -/ noncomputable theory open_locale classical big_operators nnreal open finset metric local attribute [instance, priority 1001] add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_module' -- hack to speed up simp when dealing with complicated types local attribute [-instance] unique.subsingleton pi.subsingleton /-! ### Type variables We use the following type variables in this file: * `𝕜` : a `nondiscrete_normed_field`; * `ι`, `ι'` : finite index types with decidable equality; * `E`, `E₁` : families of normed vector spaces over `𝕜` indexed by `i : ι`; * `E'` : a family of normed vector spaces over `𝕜` indexed by `i' : ι'`; * `Ei` : a family of normed vector spaces over `𝕜` indexed by `i : fin (nat.succ n)`; * `G`, `G'` : normed vector spaces over `𝕜`. -/ universes u v v' wE wE₁ wE' wEi wG wG' variables {𝕜 : Type u} {ι : Type v} {ι' : Type v'} {n : ℕ} {E : ι → Type wE} {E₁ : ι → Type wE₁} {E' : ι' → Type wE'} {Ei : fin n.succ → Type wEi} {G : Type wG} {G' : Type wG'} [decidable_eq ι] [fintype ι] [decidable_eq ι'] [fintype ι'] [nondiscrete_normed_field 𝕜] [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] [Π i, normed_group (E₁ i)] [Π i, normed_space 𝕜 (E₁ i)] [Π i, normed_group (E' i)] [Π i, normed_space 𝕜 (E' i)] [Π i, normed_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)] [normed_group G] [normed_space 𝕜 G] [normed_group G'] [normed_space 𝕜 G'] /-! ### Continuity properties of multilinear maps We relate continuity of multilinear maps to the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, in both directions. Along the way, we prove useful bounds on the difference `∥f m₁ - f m₂∥`. -/ namespace multilinear_map variable (f : multilinear_map 𝕜 E G) /-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥` on a shell `ε i / ∥c i∥ < ∥m i∥ < ε i` for some positive numbers `ε i` and elements `c i : 𝕜`, `1 < ∥c i∥`, then it satisfies this inequality for all `m`. -/ lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ∥c i∥) (hf : ∀ m : Π i, E i, (∀ i, ε i / ∥c i∥ ≤ ∥m i∥) → (∀ i, ∥m i∥ < ε i) → ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m : Π i, E i) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ := begin rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm], { simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] }, choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i), have hδ0 : 0 < ∏ i, ∥δ i∥, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)), simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0] using hf (λ i, δ i • m i) hle_δm hδm_lt, end /-- If a multilinear map in finitely many variables on normed spaces is continuous, then it satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, for some `C` which can be chosen to be positive. -/ theorem exists_bound_of_continuous (hf : continuous f) : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := begin casesI is_empty_or_nonempty ι, { refine ⟨∥f 0∥ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩, obtain rfl : m = 0, from funext (is_empty.elim ‹_›), simp [univ_eq_empty, zero_le_one] }, obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E i, ∥m - 0∥ < ε → ∥f m - f 0∥ < 1⟩ := normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one, simp only [sub_zero, f.map_zero] at hε, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have : 0 < (∥c∥ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _, refine ⟨_, this, _⟩, refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _), refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _, rw [← div_le_iff' this, one_div, ← inv_pow₀, inv_div, fintype.card, ← prod_const], exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i) end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a precise but hard to use version. See `norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads `∥f m - f m'∥ ≤ C * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`. -/ lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := begin have A : ∀(s : finset ι), ∥f m₁ - f (s.piecewise m₂ m₁)∥ ≤ C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { refine finset.induction (by simp) _, assume i s his Hrec, have I : ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ ≤ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { have A : ((insert i s).piecewise m₂ m₁) = function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _, have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i), { ext j, by_cases h : j = i, { rw h, simp [his] }, { simp [h] } }, rw [B, A, ← f.map_sub], apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC), refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _), by_cases h : j = i, { rw h, simp }, { by_cases h' : j ∈ s; simp [h', h, le_refl] } }, calc ∥f m₁ - f ((insert i s).piecewise m₂ m₁)∥ ≤ ∥f m₁ - f (s.piecewise m₂ m₁)∥ + ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ : by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ } ... ≤ (C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) + C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : add_le_add Hrec I ... = C * ∑ i in insert i s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : by simp [his, add_comm, left_distrib] }, convert A univ, simp end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a usable but not very precise version. See `norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is `∥f m - f m'∥ ≤ C * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`. -/ lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := begin have A : ∀ (i : ι), ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1), { assume i, calc ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∏ j : ι, function.update (λ j, max ∥m₁∥ ∥m₂∥) i (∥m₁ - m₂∥) j : begin apply prod_le_prod, { assume j hj, by_cases h : j = i; simp [h, norm_nonneg] }, { assume j hj, by_cases h : j = i, { rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i }, { simp [h, max_le_max, norm_le_pi_norm (_ : Π i, E i)] } } end ... = ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } }, calc ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : f.norm_image_sub_le_of_bound' hC H m₁ m₂ ... ≤ C * ∑ i, ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC ... = C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ : by { rw [sum_const, card_univ, nsmul_eq_mul], ring } end /-- If a multilinear map satisfies an inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, then it is continuous. -/ theorem continuous_of_bound (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous f := begin let D := max C 1, have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _), replace H : ∀ m, ∥f m∥ ≤ D * ∏ i, ∥m i∥, { assume m, apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _), exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) }, refine continuous_iff_continuous_at.2 (λm, _), refine continuous_at_of_locally_lipschitz zero_lt_one (D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1)) (λm' h', _), rw [dist_eq_norm, dist_eq_norm], have : 0 ≤ (max ∥m'∥ ∥m∥), by simp, have : (max ∥m'∥ ∥m∥) ≤ ∥m∥ + 1, by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm], calc ∥f m' - f m∥ ≤ D * (fintype.card ι) * (max ∥m'∥ ∥m∥) ^ (fintype.card ι - 1) * ∥m' - m∥ : f.norm_image_sub_le_of_bound D_pos H m' m ... ≤ D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1) * ∥m' - m∥ : by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left] end /-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness condition. -/ def mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous_multilinear_map 𝕜 E G := { cont := f.continuous_of_bound C H, ..f } @[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ⇑(f.mk_continuous C H) = f := rfl /-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on the other coordinates, then the resulting restricted function satisfies an inequality `∥f.restr v∥ ≤ C * ∥z∥^(n-k) * Π ∥v i∥` if the original function satisfies `∥f v∥ ≤ C * Π ∥v i∥`. -/ lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ} (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (v : fin k → G) : ∥f.restr s hk z v∥ ≤ C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ := begin rw [mul_right_comm, mul_assoc], convert H _ using 2, simp only [apply_dite norm, fintype.prod_dite, prod_const (∥z∥), finset.card_univ, fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe, ← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ∥v x∥)], refl end end multilinear_map /-! ### Continuous multilinear maps We define the norm `∥f∥` of a continuous multilinear map `f` in finitely many variables as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that this defines a normed space structure on `continuous_multilinear_map 𝕜 E G`. -/ namespace continuous_multilinear_map variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E G) (m : Πi, E i) theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := f.to_multilinear_map.exists_bound_of_continuous f.2 open real /-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/ def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E G) := ⟨op_norm⟩ lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl -- So that invocations of `le_cInf` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E G} : ∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E G} : bdd_below {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm of a continuous multilinear map: `∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`. -/ theorem le_op_norm : ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := begin have A : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _), cases A.eq_or_lt with h hlt, { rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩, rw norm_eq_zero at hi, have : f m = 0 := f.map_coord_zero i hi, rw [this, norm_zero], exact mul_nonneg (op_norm_nonneg f) A }, { rw [← div_le_iff hlt], apply le_cInf bounds_nonempty, rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc } end theorem le_of_op_norm_le {C : ℝ} (h : ∥f∥ ≤ C) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ := (f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ λ i _, norm_nonneg (m i)) lemma ratio_le_op_norm : ∥f m∥ / ∏ i, ∥m i∥ ≤ ∥f∥ := div_le_of_nonneg_of_le_mul (prod_nonneg $ λ i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m) /-- The image of the unit ball under a continuous multilinear map is bounded. -/ lemma unit_le_op_norm (h : ∥m∥ ≤ 1) : ∥f m∥ ≤ ∥f∥ := calc ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ : f.le_op_norm m ... ≤ ∥f∥ * ∏ i : ι, 1 : mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _) (λi hi, le_trans (norm_le_pi_norm (_ : Π i, E i) _) h)) (op_norm_nonneg f) ... = ∥f∥ : by simp /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ∥f m∥ ≤ M * ∏ i, ∥m i∥) : ∥f∥ ≤ M := cInf_le bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := cInf_le bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul, exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- A continuous linear map is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 := begin split, { assume h, ext m, simpa [h] using f.le_op_norm m }, { rintro rfl, apply le_antisymm (op_norm_le_bound 0 le_rfl (λm, _)) (op_norm_nonneg _), simp } end variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜] [normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 G] lemma op_norm_smul_le (c : 𝕜') : ∥c • f∥ ≤ ∥c∥ * ∥f∥ := (c • f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) begin intro m, erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end lemma op_norm_neg : ∥-f∥ = ∥f∥ := by { rw norm_def, apply congr_arg, ext, simp } /-- Continuous multilinear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (continuous_multilinear_map 𝕜 E G) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ instance to_normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) := ⟨λ c f, f.op_norm_smul_le c⟩ theorem le_op_norm_mul_prod_of_le {b : ι → ℝ} (hm : ∀ i, ∥m i∥ ≤ b i) : ∥f m∥ ≤ ∥f∥ * ∏ i, b i := (f.le_op_norm m).trans $ mul_le_mul_of_nonneg_left (prod_le_prod (λ _ _, norm_nonneg _) (λ i _, hm i)) (norm_nonneg f) theorem le_op_norm_mul_pow_card_of_le {b : ℝ} (hm : ∀ i, ∥m i∥ ≤ b) : ∥f m∥ ≤ ∥f∥ * b ^ fintype.card ι := by simpa only [prod_const] using f.le_op_norm_mul_prod_of_le m hm theorem le_op_norm_mul_pow_of_le {Ei : fin n → Type*} [Π i, normed_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)] (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i, Ei i) {b : ℝ} (hm : ∥m∥ ≤ b) : ∥f m∥ ≤ ∥f∥ * b ^ n := by simpa only [fintype.card_fin] using f.le_op_norm_mul_pow_card_of_le m (λ i, (norm_le_pi_norm m i).trans hm) /-- The fundamental property of the operator norm of a continuous multilinear map: `∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`, `nnnorm` version. -/ theorem le_op_nnnorm : nnnorm (f m) ≤ nnnorm f * ∏ i, nnnorm (m i) := nnreal.coe_le_coe.1 $ by { push_cast, exact f.le_op_norm m } theorem le_of_op_nnnorm_le {C : ℝ≥0} (h : nnnorm f ≤ C) : nnnorm (f m) ≤ C * ∏ i, nnnorm (m i) := (f.le_op_nnnorm m).trans $ mul_le_mul' h le_rfl lemma op_norm_prod (f : continuous_multilinear_map 𝕜 E G) (g : continuous_multilinear_map 𝕜 E G') : ∥f.prod g∥ = max (∥f∥) (∥g∥) := le_antisymm (op_norm_le_bound _ (norm_nonneg (f, g)) (λ m, have H : 0 ≤ ∏ i, ∥m i∥, from prod_nonneg $ λ _ _, norm_nonneg _, by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H] using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $ max_le (f.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_left _ _).trans ((f.prod g).le_op_norm _)) (g.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_right _ _).trans ((f.prod g).le_op_norm _)) lemma norm_pi {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')] [Π i', normed_space 𝕜 (E' i')] (f : Π i', continuous_multilinear_map 𝕜 E (E' i')) : ∥pi f∥ = ∥f∥ := begin apply le_antisymm, { refine (op_norm_le_bound _ (norm_nonneg f) (λ m, _)), dsimp, rw pi_norm_le_iff, exacts [λ i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i), mul_nonneg (norm_nonneg f) (prod_nonneg $ λ _ _, norm_nonneg _)] }, { refine (pi_norm_le_iff (norm_nonneg _)).2 (λ i, _), refine (op_norm_le_bound _ (norm_nonneg _) (λ m, _)), refine le_trans _ ((pi f).le_op_norm m), convert norm_le_pi_norm (λ j, f j m) i } end section variables (𝕜 E E' G G') /-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/ def prodL : (continuous_multilinear_map 𝕜 E G) × (continuous_multilinear_map 𝕜 E G') ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 E (G × G') := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ((continuous_linear_map.fst 𝕜 G G').comp_continuous_multilinear_map f, (continuous_linear_map.snd 𝕜 G G').comp_continuous_multilinear_map f), map_add' := λ f g, rfl, map_smul' := λ c f, rfl, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, norm_map' := λ f, op_norm_prod f.1 f.2 } /-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/ def piₗᵢ {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')] [Π i', normed_space 𝕜 (E' i')] : @linear_isometry_equiv 𝕜 𝕜 _ _ (ring_hom.id 𝕜) _ _ _ (Π i', continuous_multilinear_map 𝕜 E (E' i')) (continuous_multilinear_map 𝕜 E (Π i, E' i)) _ _ (@pi.module ι' _ 𝕜 _ _ (λ i', infer_instance)) _ := { to_linear_equiv := -- note: `pi_linear_equiv` does not unify correctly here, presumably due to issues with dependent -- typeclass arguments. { map_add' := λ f g, rfl, map_smul' := λ c f, rfl, .. pi_equiv, }, norm_map' := norm_pi } end section restrict_scalars variables [Π i, normed_space 𝕜' (E i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E i)] @[simp] lemma norm_restrict_scalars : ∥f.restrict_scalars 𝕜'∥ = ∥f∥ := by simp only [norm_def, coe_restrict_scalars] variable (𝕜') /-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/ def restrict_scalars_linear : continuous_multilinear_map 𝕜 E G →L[𝕜'] continuous_multilinear_map 𝕜' E G := linear_map.mk_continuous { to_fun := restrict_scalars 𝕜', map_add' := λ m₁ m₂, rfl, map_smul' := λ c m, rfl } 1 $ λ f, by simp variable {𝕜'} lemma continuous_restrict_scalars : continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E G → continuous_multilinear_map 𝕜' E G) := (restrict_scalars_linear 𝕜').continuous end restrict_scalars /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, precise version. For a less precise but more usable version, see `norm_image_sub_le`. The bound reads `∥f m - f m'∥ ≤ ∥f∥ * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`.-/ lemma norm_image_sub_le' (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _ /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, less precise version. For a more precise but less usable version, see `norm_image_sub_le'`. The bound is `∥f m - f m'∥ ≤ ∥f∥ * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`.-/ lemma norm_image_sub_le (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _ /-- Applying a multilinear map to a vector is continuous in both coordinates. -/ lemma continuous_eval : continuous (λ p : continuous_multilinear_map 𝕜 E G × Π i, E i, p.1 p.2) := begin apply continuous_iff_continuous_at.2 (λp, _), apply continuous_at_of_locally_lipschitz zero_lt_one ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + ∏ i, ∥p.2 i∥) (λq hq, _), have : 0 ≤ (max ∥q.2∥ ∥p.2∥), by simp, have : 0 ≤ ∥p∥ + 1, by simp [le_trans zero_le_one], have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq), have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 := le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (by simp [A, -add_comm, zero_le_one]), have : ∀ (i : ι), i ∈ univ → 0 ≤ ∥p.2 i∥ := λ i hi, norm_nonneg _, calc dist (q.1 q.2) (p.1 p.2) ≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _ ... = ∥q.1 q.2 - q.1 p.2∥ + ∥q.1 p.2 - p.1 p.2∥ : by rw [dist_eq_norm, dist_eq_norm] ... ≤ ∥q.1∥ * (fintype.card ι) * (max ∥q.2∥ ∥p.2∥) ^ (fintype.card ι - 1) * ∥q.2 - p.2∥ + ∥q.1 - p.1∥ * ∏ i, ∥p.2 i∥ : add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2) ... ≤ (∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) * ∥q - p∥ + ∥q - p∥ * ∏ i, ∥p.2 i∥ : by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg, mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg, norm_fst_le (q - p), prod_nonneg] ... = ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + (∏ i, ∥p.2 i∥)) * dist q p : by { rw dist_eq_norm, ring } end lemma continuous_eval_left (m : Π i, E i) : continuous (λ p : continuous_multilinear_map 𝕜 E G, p m) := continuous_eval.comp (continuous_id.prod_mk continuous_const) lemma has_sum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} {q : continuous_multilinear_map 𝕜 E G} (h : has_sum p q) (m : Π i, E i) : has_sum (λ a, p a m) (q m) := begin dsimp [has_sum] at h ⊢, convert ((continuous_eval_left m).tendsto _).comp h, ext s, simp end lemma tsum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} (hp : summable p) (m : Π i, E i) : (∑' a, p a) m = ∑' a, p a m := (has_sum_eval hp.has_sum m).tsum_eq.symm open_locale topological_space open filter /-- If the target space is complete, the space of continuous multilinear maps with its norm is also complete. The proof is essentially the same as for the space of continuous linear maps (modulo the addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear case from the multilinear case via a currying isomorphism. However, this would mess up imports, and it is more satisfactory to have the simplest case as a standalone proof. -/ instance [complete_space G] : complete_space (continuous_multilinear_map 𝕜 E G) := begin have nonneg : ∀ (v : Π i, E i), 0 ≤ ∏ i, ∥v i∥ := λ v, finset.prod_nonneg (λ i hi, norm_nonneg _), -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _), -- We now expand out the definition of a Cauchy sequence, rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, -- and establish that the evaluation at any point `v : Π i, E i` is Cauchy. have cau : ∀ v, cauchy_seq (λ n, f n v), { assume v, apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ∥v i∥, λ n, _, _, _⟩, { exact mul_nonneg (b0 n) (nonneg v) }, { assume n m N hn hm, rw dist_eq_norm, apply le_trans ((f n - f m).le_op_norm v) _, exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) }, { simpa using b_lim.mul tendsto_const_nhds } }, -- We assemble the limits points of those Cauchy sequences -- (which exist as `G` is complete) -- into a function which we call `F`. choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `F` is multilinear, let Fmult : multilinear_map 𝕜 E G := { to_fun := F, map_add' := λ v i x y, begin have A := hF (function.update v i (x + y)), have B := (hF (function.update v i x)).add (hF (function.update v i y)), simp at A B, exact tendsto_nhds_unique A B end, map_smul' := λ v i c x, begin have A := hF (function.update v i (c • x)), have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)), simp at A B, exact tendsto_nhds_unique A B end }, -- and that `F` has norm at most `(b 0 + ∥f 0∥)`. have Fnorm : ∀ v, ∥F v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume v, have A : ∀ n, ∥f n v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume n, apply le_trans ((f n).le_op_norm _) _, apply mul_le_mul_of_nonneg_right _ (nonneg v), calc ∥f n∥ = ∥(f n - f 0) + f 0∥ : by { congr' 1, abel } ... ≤ ∥f n - f 0∥ + ∥f 0∥ : norm_add_le _ _ ... ≤ b 0 + ∥f 0∥ : begin apply add_le_add_right, simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _) end }, exact le_of_tendsto (hF v).norm (eventually_of_forall A) }, -- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence. let Fcont := Fmult.mk_continuous _ Fnorm, use Fcont, -- Our last task is to establish convergence to `F` in norm. have : ∀ n, ∥f n - Fcont∥ ≤ b n, { assume n, apply op_norm_le_bound _ (b0 n) (λ v, _), have A : ∀ᶠ m in at_top, ∥(f n - f m) v∥ ≤ b n * ∏ i, ∥v i∥, { refine eventually_at_top.2 ⟨n, λ m hm, _⟩, apply le_trans ((f n - f m).le_op_norm _) _, exact mul_le_mul_of_nonneg_right (b_bound n m n le_rfl hm) (nonneg v) }, have B : tendsto (λ m, ∥(f n - f m) v∥) at_top (𝓝 (∥(f n - Fcont) v∥)) := tendsto.norm (tendsto_const_nhds.sub (hF v)), exact le_of_tendsto B A }, erw tendsto_iff_norm_tendsto_zero, exact squeeze_zero (λ n, norm_nonneg _) this b_lim, end end continuous_multilinear_map /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E G) {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ C := continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m) /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map 𝕜 E G) {C : ℝ} (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ max C 0 := continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $ λ m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _) (prod_nonneg $ λ _ _, norm_nonneg _) namespace continuous_multilinear_map /-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : ℕ} (f : (G [×n]→L[𝕜] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) : G [×k]→L[𝕜] G' := (f.to_multilinear_map.restr s hk z).mk_continuous (∥f∥ * ∥z∥^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _ lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] G') (s : finset (fin n)) (hk : s.card = k) (z : G) : ∥f.restr s hk z∥ ≤ ∥f∥ * ∥z∥ ^ (n - k) := begin apply multilinear_map.mk_continuous_norm_le, exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _) end section variables (𝕜 ι) (A : Type*) [normed_comm_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^ι`, where `A` is a normed commutative algebra over `𝕜`, associating to `m` the product of all the `m i`. See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/ protected def mk_pi_algebra : continuous_multilinear_map 𝕜 (λ i : ι, A) A := multilinear_map.mk_continuous (multilinear_map.mk_pi_algebra 𝕜 ι A) (if nonempty ι then 1 else ∥(1 : A)∥) $ begin intro m, casesI is_empty_or_nonempty ι with hι hι, { simp [eq_empty_of_is_empty univ, not_nonempty_iff.2 hι] }, { simp [norm_prod_le' univ univ_nonempty, hι] } end variables {A 𝕜 ι} @[simp] lemma mk_pi_algebra_apply (m : ι → A) : continuous_multilinear_map.mk_pi_algebra 𝕜 ι A m = ∏ i, m i := rfl lemma norm_mk_pi_algebra_le [nonempty ι] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ 1 := calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = _ : if_pos ‹_› lemma norm_mk_pi_algebra_of_empty [is_empty ι] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = ∥(1 : A)∥ := begin apply le_antisymm, calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = ∥(1 : A)∥ : if_neg (not_nonempty_iff.mpr ‹_›), convert ratio_le_op_norm _ (λ _, (1 : A)), simp [eq_empty_of_is_empty (univ : finset ι)], end @[simp] lemma norm_mk_pi_algebra [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = 1 := begin casesI is_empty_or_nonempty ι, { simp [norm_mk_pi_algebra_of_empty] }, { refine le_antisymm norm_mk_pi_algebra_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp }, end end section variables (𝕜 n) (A : Type*) [normed_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `𝕜`, associating to `m` the product of all the `m i`. See also: `multilinear_map.mk_pi_algebra`. -/ protected def mk_pi_algebra_fin : continuous_multilinear_map 𝕜 (λ i : fin n, A) A := multilinear_map.mk_continuous (multilinear_map.mk_pi_algebra_fin 𝕜 n A) (nat.cases_on n ∥(1 : A)∥ (λ _, 1)) $ begin intro m, cases n, { simp }, { have : @list.of_fn A n.succ m ≠ [] := by simp, simpa [← fin.prod_of_fn] using list.norm_prod_le' this } end variables {A 𝕜 n} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) : continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A m = (list.of_fn m).prod := rfl lemma norm_mk_pi_algebra_fin_succ_le : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A∥ ≤ 1 := multilinear_map.mk_continuous_norm_le _ zero_le_one _ lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ ≤ 1 := by cases n; [exact hn.false.elim, exact norm_mk_pi_algebra_fin_succ_le] lemma norm_mk_pi_algebra_fin_zero : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A∥ = ∥(1 : A)∥ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, convert ratio_le_op_norm _ (λ _, 1); [simp, apply_instance] end @[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ = 1 := begin cases n, { simp [norm_mk_pi_algebra_fin_zero] }, { refine le_antisymm norm_mk_pi_algebra_fin_succ_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp } end end variables (𝕜 ι) /-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_field (z : G) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G := multilinear_map.mk_continuous (multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥) (λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.norm_prod, mul_comm]) variables {𝕜 ι} @[simp] lemma mk_pi_field_apply (z : G) (m : ι → 𝕜) : (continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → G) m = (∏ i, m i) • z := rfl lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) : continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f := to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self @[simp] lemma norm_mk_pi_field (z : G) : ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ = ∥z∥ := (multilinear_map.mk_continuous_norm_le _ (norm_nonneg z) _).antisymm $ by simpa using (continuous_multilinear_map.mk_pi_field 𝕜 ι z).le_op_norm (λ _, 1) variables (𝕜 ι G) /-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear isometry in `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv : G ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) := { to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z, inv_fun := λ f, f (λi, 1), map_add' := λ z z', by { ext m, simp [smul_add] }, map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_field_apply_one_eq_self, norm_map' := norm_mk_pi_field } end continuous_multilinear_map namespace continuous_linear_map lemma norm_comp_continuous_multilinear_map_le (g : G →L[𝕜] G') (f : continuous_multilinear_map 𝕜 E G) : ∥g.comp_continuous_multilinear_map f∥ ≤ ∥g∥ * ∥f∥ := continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ λ m, calc ∥g (f m)∥ ≤ ∥g∥ * (∥f∥ * ∏ i, ∥m i∥) : g.le_op_norm_of_le $ f.le_op_norm _ ... = _ : (mul_assoc _ _ _).symm /-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/ def comp_continuous_multilinear_mapL : (G →L[𝕜] G') →L[𝕜] continuous_multilinear_map 𝕜 E G →L[𝕜] continuous_multilinear_map 𝕜 E G' := linear_map.mk_continuous₂ (linear_map.mk₂ 𝕜 comp_continuous_multilinear_map (λ f₁ f₂ g, rfl) (λ c f g, rfl) (λ f g₁ g₂, by { ext1, apply f.map_add }) (λ c f g, by { ext1, simp })) 1 $ λ f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g } /-- Flip arguments in `f : G →L[𝕜] continuous_multilinear_map 𝕜 E G'` to get `continuous_multilinear_map 𝕜 E (G →L[𝕜] G')` -/ def flip_multilinear (f : G →L[𝕜] continuous_multilinear_map 𝕜 E G') : continuous_multilinear_map 𝕜 E (G →L[𝕜] G') := multilinear_map.mk_continuous { to_fun := λ m, linear_map.mk_continuous { to_fun := λ x, f x m, map_add' := λ x y, by simp only [map_add, continuous_multilinear_map.add_apply], map_smul' := λ c x, by simp only [continuous_multilinear_map.smul_apply, map_smul, ring_hom.id_apply] } (∥f∥ * ∏ i, ∥m i∥) $ λ x, by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) }, map_add' := λ m i x y, by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk, linear_map.mk_continuous_apply]}, map_smul' := λ m i c x, by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk, linear_map.mk_continuous_apply, pi.smul_apply]} } ∥f∥ $ λ m, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg f) (prod_nonneg $ λ i hi, norm_nonneg (m i))) _ end continuous_linear_map open continuous_multilinear_map namespace multilinear_map /-- Given a map `f : G →ₗ[𝕜] multilinear_map 𝕜 E G'` and an estimate `H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥`, construct a continuous linear map from `G` to `continuous_multilinear_map 𝕜 E G'`. In order to lift, e.g., a map `f : (multilinear_map 𝕜 E G) →ₗ[𝕜] multilinear_map 𝕜 E' G'` to a map `(continuous_multilinear_map 𝕜 E G) →L[𝕜] continuous_multilinear_map 𝕜 E' G'`, one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear` which is a linear map from `continuous_multilinear_map 𝕜 E G` to `multilinear_map 𝕜 E' G'`. -/ def mk_continuous_linear (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : G →L[𝕜] continuous_multilinear_map 𝕜 E G' := linear_map.mk_continuous { to_fun := λ x, (f x).mk_continuous (C * ∥x∥) $ H x, map_add' := λ x y, by { ext1, simp }, map_smul' := λ c x, by { ext1, simp } } (max C 0) $ λ x, ((f x).mk_continuous_norm_le' _).trans_eq $ by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul] lemma mk_continuous_linear_norm_le' (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : ∥mk_continuous_linear f C H∥ ≤ max C 0 := begin dunfold mk_continuous_linear, exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_linear_norm_le (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') {C : ℝ} (hC : 0 ≤ C) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : ∥mk_continuous_linear f C H∥ ≤ C := (mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC) /-- Given a map `f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)` and an estimate `H : ∀ m m', ∥f m m'∥ ≤ C * ∏ i, ∥m i∥ * ∏ i, ∥m' i∥`, upgrade all `multilinear_map`s in the type to `continuous_multilinear_map`s. -/ def mk_continuous_multilinear (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : continuous_multilinear_map 𝕜 E (continuous_multilinear_map 𝕜 E' G) := mk_continuous { to_fun := λ m, mk_continuous (f m) (C * ∏ i, ∥m i∥) $ H m, map_add' := λ m i x y, by { ext1, simp }, map_smul' := λ m i c x, by { ext1, simp } } (max C 0) $ λ m, ((f m).mk_continuous_norm_le' _).trans_eq $ by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (λ _ _, norm_nonneg _) } @[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ} (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) (m : Π i, E i) : ⇑(mk_continuous_multilinear f C H m) = f m := rfl lemma mk_continuous_multilinear_norm_le' (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : ∥mk_continuous_multilinear f C H∥ ≤ max C 0 := begin dunfold mk_continuous_multilinear, exact mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_multilinear_norm_le (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ} (hC : 0 ≤ C) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : ∥mk_continuous_multilinear f C H∥ ≤ C := (mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC) end multilinear_map namespace continuous_multilinear_map lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i →L[𝕜] E₁ i) : ∥g.comp_continuous_linear_map f∥ ≤ ∥g∥ * ∏ i, ∥f i∥ := op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ λ i hi, norm_nonneg _) $ λ m, calc ∥g (λ i, f i (m i))∥ ≤ ∥g∥ * ∏ i, ∥f i (m i)∥ : g.le_op_norm _ ... ≤ ∥g∥ * ∏ i, (∥f i∥ * ∥m i∥) : mul_le_mul_of_nonneg_left (prod_le_prod (λ _ _, norm_nonneg _) (λ i hi, (f i).le_op_norm (m i))) (norm_nonneg g) ... = (∥g∥ * ∏ i, ∥f i∥) * ∏ i, ∥m i∥ : by rw [prod_mul_distrib, mul_assoc] /-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map. This implementation fixes `f : Π i, E i →L[𝕜] E₁ i`. TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of issues with class instances. -/ def comp_continuous_linear_mapL (f : Π i, E i →L[𝕜] E₁ i) : continuous_multilinear_map 𝕜 E₁ G →L[𝕜] continuous_multilinear_map 𝕜 E G := linear_map.mk_continuous { to_fun := λ g, g.comp_continuous_linear_map f, map_add' := λ g₁ g₂, rfl, map_smul' := λ c g, rfl } (∏ i, ∥f i∥) $ λ g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _) @[simp] lemma comp_continuous_linear_mapL_apply (g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i →L[𝕜] E₁ i) : comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f := rfl lemma norm_comp_continuous_linear_mapL_le (f : Π i, E i →L[𝕜] E₁ i) : ∥@comp_continuous_linear_mapL 𝕜 ι E E₁ G _ _ _ _ _ _ _ _ _ f∥ ≤ (∏ i, ∥f i∥) := linear_map.mk_continuous_norm_le _ (prod_nonneg $ λ i _, norm_nonneg _) _ end continuous_multilinear_map section currying /-! ### Currying We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`). The inverse operations are called `uncurry_left` and `uncurry_right`. We also register continuous linear equiv versions of these correspondences, in `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. -/ open fin function lemma continuous_linear_map.norm_map_tail_le (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) : ∥f (m 0) (tail m)∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (m 0) (tail m)∥ ≤ ∥f (m 0)∥ * ∏ i, ∥(tail m) i∥ : (f (m 0)).le_op_norm _ ... ≤ (∥f∥ * ∥m 0∥) * ∏ i, ∥(tail m) i∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _)) ... = ∥f∥ * (∥m 0∥ * ∏ i, ∥(tail m) i∥) : by ring ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_succ, refl } lemma continuous_multilinear_map.norm_map_init_le (f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) (m : Πi, Ei i) : ∥f (init m) (m (last n))∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (init m) (m (last n))∥ ≤ ∥f (init m)∥ * ∥m (last n)∥ : (f (init m)).le_op_norm _ ... ≤ (∥f∥ * (∏ i, ∥(init m) i∥)) * ∥m (last n)∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _) ... = ∥f∥ * ((∏ i, ∥(init m) i∥) * ∥m (last n)∥) : mul_assoc _ _ _ ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_cast_succ, refl } lemma continuous_multilinear_map.norm_map_cons_le (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) : ∥f (cons x m)∥ ≤ ∥f∥ * ∥x∥ * ∏ i, ∥m i∥ := calc ∥f (cons x m)∥ ≤ ∥f∥ * ∏ i, ∥cons x m i∥ : f.le_op_norm _ ... = (∥f∥ * ∥x∥) * ∏ i, ∥m i∥ : by { rw prod_univ_succ, simp [mul_assoc] } lemma continuous_multilinear_map.norm_map_snoc_le (f : continuous_multilinear_map 𝕜 Ei G) (m : Π(i : fin n), Ei i.cast_succ) (x : Ei (last n)) : ∥f (snoc m x)∥ ≤ ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ := calc ∥f (snoc m x)∥ ≤ ∥f∥ * ∏ i, ∥snoc m x i∥ : f.le_op_norm _ ... = ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ : by { rw prod_univ_cast_succ, simp [mul_assoc] } /-! #### Left currying -/ /-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def continuous_linear_map.uncurry_left (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : continuous_multilinear_map 𝕜 Ei G := (@linear_map.uncurry_left 𝕜 n Ei G _ _ _ _ _ (continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous (∥f∥) (λm, continuous_linear_map.norm_map_tail_le f m) @[simp] lemma continuous_linear_map.uncurry_left_apply (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain a continuous linear map into continuous multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def continuous_multilinear_map.curry_left (f : continuous_multilinear_map 𝕜 Ei G) : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G) := linear_map.mk_continuous { -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear -- map to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous (∥f∥ * ∥x∥) (f.norm_map_cons_le x), map_add' := λx y, by { ext m, exact f.cons_add m x y }, map_smul' := λc x, by { ext m, exact f.cons_smul m c x } } -- then register its continuity thanks to its boundedness properties. (∥f∥) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _) @[simp] lemma continuous_multilinear_map.curry_left_apply (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma continuous_linear_map.curry_uncurry_left (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, continuous_linear_map.uncurry_left_apply, continuous_multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma continuous_multilinear_map.uncurry_curry_left (f : continuous_multilinear_map 𝕜 Ei G) : f.curry_left.uncurry_left = f := continuous_multilinear_map.to_multilinear_map_inj $ f.to_multilinear_map.uncurry_curry_left variables (𝕜 Ei G) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on `Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in `continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given in `multilinear_curry_left_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_left_equiv : (Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_linear_map.uncurry_left, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_left, left_inv := continuous_linear_map.curry_uncurry_left, right_inv := continuous_multilinear_map.uncurry_curry_left } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {𝕜 Ei G} @[simp] lemma continuous_multilinear_curry_left_equiv_apply (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.succ) G)) (v : Π i, Ei i) : continuous_multilinear_curry_left_equiv 𝕜 Ei G f v = f (v 0) (tail v) := rfl @[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (v : Π i : fin n, Ei i.succ) : (continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm f x v = f (cons x v) := rfl @[simp] lemma continuous_multilinear_map.curry_left_norm (f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_left∥ = ∥f∥ := (continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm.norm_map f @[simp] lemma continuous_linear_map.uncurry_left_norm (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : ∥f.uncurry_left∥ = ∥f∥ := (continuous_multilinear_curry_left_equiv 𝕜 Ei G).norm_map f /-! #### Right currying -/ /-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/ def continuous_multilinear_map.uncurry_right (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : continuous_multilinear_map 𝕜 Ei G := let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →ₗ[𝕜] G) := { to_fun := λ m, (f m).to_linear_map, map_add' := λ m i x y, by simp, map_smul' := λ m i c x, by simp } in (@multilinear_map.uncurry_right 𝕜 n Ei G _ _ _ _ _ f').mk_continuous (∥f∥) (λm, f.norm_map_init_le m) @[simp] lemma continuous_multilinear_map.uncurry_right_apply (f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) (m : Πi, Ei i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain a continuous multilinear map in `n` variables into continuous linear maps, given by `m ↦ (x ↦ f (snoc m x))`. -/ def continuous_multilinear_map.curry_right (f : continuous_multilinear_map 𝕜 Ei G) : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G) := let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G) := { to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous (∥f∥ * ∏ i, ∥m i∥) $ λx, f.norm_map_snoc_le m x, map_add' := λ m i x y, by { simp, refl }, map_smul' := λ m i c x, by { simp, refl } } in f'.mk_continuous (∥f∥) (λm, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _) @[simp] lemma continuous_multilinear_map.curry_right_apply (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i : fin n, Ei i.cast_succ) (x : Ei (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma continuous_multilinear_map.curry_uncurry_right (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, continuous_multilinear_map.curry_right_apply, continuous_multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma continuous_multilinear_map.uncurry_curry_right (f : continuous_multilinear_map 𝕜 Ei G) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (𝕜 Ei G) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), Ei i` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), Ei i.cast_succ` with values in the space of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 Ei G`. The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 Ei G`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_multilinear_map.uncurry_right, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_right, left_inv := continuous_multilinear_map.curry_uncurry_right, right_inv := continuous_multilinear_map.uncurry_curry_right } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables (n G') /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space of continuous linear maps on `G`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G G'`. For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there are no dependent types, use the primed version as it helps Lean a lot for unification. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv' : (G [×n]→L[𝕜] (G →L[𝕜] G')) ≃ₗᵢ[𝕜] (G [×n.succ]→L[𝕜] G') := continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) G' variables {n 𝕜 G Ei G'} @[simp] lemma continuous_multilinear_curry_right_equiv_apply (f : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))) (v : Π i, Ei i) : (continuous_multilinear_curry_right_equiv 𝕜 Ei G) f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply (f : continuous_multilinear_map 𝕜 Ei G) (v : Π (i : fin n), Ei i.cast_succ) (x : Ei (last n)) : (continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_apply' (f : G [×n]→L[𝕜] (G →L[𝕜] G')) (v : Π (i : fin n.succ), G) : continuous_multilinear_curry_right_equiv' 𝕜 n G G' f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply' (f : G [×n.succ]→L[𝕜] G') (v : Π (i : fin n), G) (x : G) : (continuous_multilinear_curry_right_equiv' 𝕜 n G G').symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_map.curry_right_norm (f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_right∥ = ∥f∥ := (continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm.norm_map f @[simp] lemma continuous_multilinear_map.uncurry_right_norm (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : ∥f.uncurry_right∥ = ∥f∥ := (continuous_multilinear_curry_right_equiv 𝕜 Ei G).norm_map f /-! #### Currying with `0` variables The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!). Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated derivatives, we register this isomorphism. -/ section local attribute [instance] unique.subsingleton variables {𝕜 G G'} /-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/ def continuous_multilinear_map.uncurry0 (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) G') : G' := f 0 variables (𝕜 G) /-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0` variables taking the (unique) value `x` -/ def continuous_multilinear_map.curry0 (x : G') : G [×0]→L[𝕜] G' := { to_fun := λm, x, map_add' := λ m i, fin.elim0 i, map_smul' := λ m i, fin.elim0 i, cont := continuous_const } variable {G} @[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) → G) : continuous_multilinear_map.curry0 𝕜 G x m = x := rfl variable {𝕜} @[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [×0]→L[𝕜] G') : f.uncurry0 = f 0 := rfl @[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [×0]→L[𝕜] G') {x : fin 0 → G} : continuous_multilinear_map.curry0 𝕜 G (f x) = f := by { ext m, simp [(subsingleton.elim _ _ : x = m)] } lemma continuous_multilinear_map.uncurry0_curry0 (f : G [×0]→L[𝕜] G') : continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f := by simp variables (𝕜 G) @[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') : (continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl @[simp] lemma continuous_multilinear_map.curry0_norm (x : G') : ∥continuous_multilinear_map.curry0 𝕜 G x∥ = ∥x∥ := begin apply le_antisymm, { exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) }, { simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 } end variables {𝕜 G} @[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [×0]→L[𝕜] G') {x : fin 0 → G} : ∥f x∥ = ∥f∥ := begin have : x = 0 := subsingleton.elim _ _, subst this, refine le_antisymm (by simpa using f.le_op_norm 0) _, have : ∥continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)∥ ≤ ∥f.uncurry0∥ := continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp [-continuous_multilinear_map.apply_zero_curry0]), simpa end lemma continuous_multilinear_map.uncurry0_norm (f : G [×0]→L[𝕜] G') : ∥f.uncurry0∥ = ∥f∥ := by simp variables (𝕜 G G') /-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear maps in `0` variables with values in this normed space. The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_fin0 : (G [×0]→L[𝕜] G') ≃ₗᵢ[𝕜] G' := { to_fun := λf, continuous_multilinear_map.uncurry0 f, inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f, map_add' := λf g, rfl, map_smul' := λc f, rfl, left_inv := continuous_multilinear_map.uncurry0_curry0, right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G, norm_map' := continuous_multilinear_map.uncurry0_norm } variables {𝕜 G G'} @[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [×0]→L[𝕜] G') : continuous_multilinear_curry_fin0 𝕜 G G' f = f 0 := rfl @[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) → G) : (continuous_multilinear_curry_fin0 𝕜 G G').symm x v = x := rfl end /-! #### With 1 variable -/ variables (𝕜 G G') /-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from `G` to `G'`. -/ def continuous_multilinear_curry_fin1 : (G [×1]→L[𝕜] G') ≃ₗᵢ[𝕜] (G →L[𝕜] G') := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) G').symm.trans (continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] G')) variables {𝕜 G G'} @[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [×1]→L[𝕜] G') (x : G) : continuous_multilinear_curry_fin1 𝕜 G G' f x = f (fin.snoc 0 x) := rfl @[simp] lemma continuous_multilinear_curry_fin1_symm_apply (f : G →L[𝕜] G') (v : (fin 1) → G) : (continuous_multilinear_curry_fin1 𝕜 G G').symm f v = f (v 0) := rfl namespace continuous_multilinear_map variables (𝕜 G G') /-- An equivalence of the index set defines a linear isometric equivalence between the spaces of multilinear maps. -/ def dom_dom_congr (σ : ι ≃ ι') : continuous_multilinear_map 𝕜 (λ _ : ι, G) G' ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 (λ _ : ι', G) G' := linear_isometry_equiv.of_bounds { to_fun := λ f, (multilinear_map.dom_dom_congr σ f.to_multilinear_map).mk_continuous ∥f∥ $ λ m, (f.le_op_norm (λ i, m (σ i))).trans_eq $ by rw [← σ.prod_comp], inv_fun := λ f, (multilinear_map.dom_dom_congr σ.symm f.to_multilinear_map).mk_continuous ∥f∥ $ λ m, (f.le_op_norm (λ i, m (σ.symm i))).trans_eq $ by rw [← σ.symm.prod_comp], left_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.symm_apply_apply], right_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.apply_symm_apply], map_add' := λ f g, rfl, map_smul' := λ c f, rfl } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {𝕜 G G'} section variable [decidable_eq (ι ⊕ ι')] /-- A continuous multilinear map with variables indexed by `ι ⊕ ι'` defines a continuous multilinear map with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'`. -/ def curry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') := multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (∥f∥) $ λ m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m') @[simp] lemma curry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') (m : ι → G) (m' : ι' → G) : f.curry_sum m m' = f (sum.elim m m') := rfl /-- A continuous multilinear map with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'` defines a continuous multilinear map with variables indexed by `ι ⊕ ι'`. -/ def uncurry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' := multilinear_map.mk_continuous (to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (∥f∥) $ λ m, by simpa [fintype.prod_sum_type, mul_assoc] using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _) @[simp] lemma uncurry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) (m : ι ⊕ ι' → G) : f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) := rfl variables (𝕜 ι ι' G G') /-- Linear isometric equivalence between the space of continuous multilinear maps with variables indexed by `ι ⊕ ι'` and the space of continuous multilinear maps with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'`. The forward and inverse functions are `continuous_multilinear_map.curry_sum` and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need some properties of `linear_isometry_equiv`. -/ def curry_sum_equiv : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') := linear_isometry_equiv.of_bounds { to_fun := curry_sum, inv_fun := uncurry_sum, map_add' := λ f g, by { ext, refl }, map_smul' := λ c f, by { ext, refl }, left_inv := λ f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) }, right_inv := λ f, by { ext m₁ m₂, change f _ _ = f _ _, rw [sum.elim_comp_inl, sum.elim_comp_inr] } } (λ f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) end section variables (𝕜 G G') {k l : ℕ} {s : finset (fin n)} /-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality `l`, then the space of continuous multilinear maps `G [×n]→L[𝕜] G'` of `n` variables is isomorphic to the space of continuous multilinear maps `G [×k]→L[𝕜] G [×l]→L[𝕜] G'` of `k` variables taking values in the space of continuous multilinear maps of `l` variables. -/ def curry_fin_finset {k l n : ℕ} {s : finset (fin n)} (hk : s.card = k) (hl : sᶜ.card = l) : (G [×n]→L[𝕜] G') ≃ₗᵢ[𝕜] (G [×k]→L[𝕜] G [×l]→L[𝕜] G') := (dom_dom_congr 𝕜 G G' (fin_sum_equiv_of_finset hk hl).symm).trans (curry_sum_equiv 𝕜 (fin k) (fin l) G G') variables {𝕜 G G'} @[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×n]→L[𝕜] G') (mk : fin k → G) (ml : fin l → G) : curry_fin_finset 𝕜 G G' hk hl f mk ml = f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) := rfl @[simp] lemma curry_fin_finset_symm_apply (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (m : fin n → G) : (curry_fin_finset 𝕜 G G' hk hl).symm f m = f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i)) (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) := rfl @[simp] lemma curry_fin_finset_symm_apply_piecewise_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x y : G) : (curry_fin_finset 𝕜 G G' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) := multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y @[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x : G) : (curry_fin_finset 𝕜 G G' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) := rfl @[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×n]→L[𝕜] G') (x y : G) : curry_fin_finset 𝕜 G G' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) := begin refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails rw linear_isometry_equiv.symm_apply_apply end end end continuous_multilinear_map end currying
21e74ddbf0a053136864653af66df23fb6950f1b
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/tactic_paper_supplemental_material/monoid_cancellation/simp.lean
37d8b48b487e8fab60717ea5e557919f2ab743e5
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
244
lean
import .monoid local infix * := star attribute [simp] star_comm star_assoc one_star @[simp] lemma star_left_comm : ∀ a b c, a * (b * c) = b * (a * c) := left_comm star star_comm star_assoc @[simp] lemma star_one (a) : a * o = a := by simp
6098e2958c73cfbc46afc7d4fabf81a78cec9e2c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/subtype/default.lean
196786d79de8d7c2873dec17ab852bca9f0fd77d
[]
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
323
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.subtype.basic import Mathlib.Lean3Lib.init.data.subtype.instances namespace Mathlib
f46b8303dfc1535d21b5d2c9f912bcf1775976d3
4f065978c49388d188224610d9984673079f7d91
/sheaf_fundamental.lean
92d36f3695cace035f3b02151a0ed0450fd1fd41
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
18,027
lean
import topology.opens universes v u open topological_space lattice variables (X : Type u) [topological_space X] set_option old_structure_cmd true structure presheaf : Type (max u (v+1)) := (F : ∀ U : opens X, Type v) (res : Π U V : opens X, V ≤ U → F U → F V) (res_self : ∀ U x, res U U (le_refl U) x = x) (res_res : ∀ U V W HWV HVU x, res V W HWV (res U V HVU x) = res U W (le_trans HWV HVU) x) namespace presheaf instance : has_coe_to_fun (presheaf.{v} X) := ⟨_, presheaf.F⟩ end presheaf variables {X} structure covering (U : opens X) : Type (u+1) := (ι : Type u) (map : ι → opens X) (map_le : ∀ i, map i ≤ U) (exists_of_mem : ∀ x ∈ U, ∃ i, x ∈ map i) variables (X) structure sheaf extends presheaf.{v} X : Type (max u (v+1)) := (locality : ∀ U : opens X, ∀ s t : F U, ∀ OC : covering U, (∀ i : OC.ι, res U (OC.map i) (OC.map_le i) s = res U (OC.map i) (OC.map_le i) t) → s = t) (gluing : ∀ U : opens X, ∀ OC : covering U, ∀ S : Π i : OC.ι, F (OC.map i), (∀ i j : OC.ι, res (OC.map i) (OC.map i ⊓ OC.map j) inf_le_left (S i) = res (OC.map j) (OC.map i ⊓ OC.map j) inf_le_right (S j)) → ∃ s : F U, ∀ i : OC.ι, res U (OC.map i) (OC.map_le i) s = S i) def Func (Y : Type v) : sheaf.{(max u v) u} X := { F := λ U, U → Y, res := λ U V HVU s v, s ⟨v.1, HVU v.2⟩, res_self := λ _ _, funext $ λ ⟨_, _⟩, rfl, res_res := λ _ _ _ _ _ _, rfl, locality := λ U s t OC H, funext $ λ ⟨u, hu⟩, let ⟨i, hui⟩ := OC.exists_of_mem u hu in have _ := congr_fun (H i) ⟨u, hui⟩, this, gluing := λ U OC S H, ⟨λ u, S (classical.some $ OC.exists_of_mem u.1 u.2) ⟨u.1, classical.some_spec $ OC.exists_of_mem u.1 u.2⟩, λ i, funext $ λ ⟨u, hu⟩, have _ := congr_fun (H i $ classical.some $ OC.exists_of_mem u $ OC.map_le i hu) ⟨u, hu, classical.some_spec $ OC.exists_of_mem u $ OC.map_le i hu⟩, this.symm⟩ } variables {X} namespace opens def covering_res (U V : opens X) (H : V ⊆ U) (OC : covering U) : covering V := { ι := OC.ι, map := λ i : OC.ι, V ⊓ OC.map i, map_le := λ _, inf_le_left, exists_of_mem := λ x hxV, let ⟨i, hi⟩ := OC.exists_of_mem _ (H hxV) in ⟨i, hxV, hi⟩ } end opens structure subpresheaf (F : presheaf.{v} X) : Type (max u v) := (to_set : Π U : opens X, set (F U)) (res_mem_to_set : ∀ {U V : opens X} (HVU : V ⊆ U) {s : F U}, s ∈ to_set U → F.res U V HVU s ∈ to_set V) namespace subpresheaf instance (F : presheaf.{v} X) : has_coe_to_fun (subpresheaf F) := ⟨_, to_set⟩ instance (F : presheaf.{v} X) : partial_order (subpresheaf F) := partial_order.lift to_set (λ ⟨x, hx⟩ ⟨y, hy⟩, mk.inj_eq.mpr) infer_instance def to_subsheaf {F : presheaf.{v} X} (S : subpresheaf F) : subpresheaf F := { to_set := λ U, { x | ∃ OC : covering.{u} U, ∀ i : OC.ι, F.res U (OC.map i) (OC.map_le i) x ∈ S (OC.map i) }, res_mem_to_set := λ U V HVU x ⟨OC, hx⟩, ⟨opens.covering_res U V HVU OC, λ i, have _ ∈ S ((opens.covering_res U V HVU OC).map i) := S.res_mem_to_set (set.inter_subset_right _ _) (hx i), by rwa F.res_res at this ⊢⟩ } theorem le_to_subsheaf {F : presheaf.{v} X} (S : subpresheaf F) : S ≤ S.to_subsheaf := λ U x hx, ⟨{ ι := punit, map := λ _, U, map_le := λ _, le_refl U, exists_of_mem := λ x hxU, ⟨punit.star, hxU⟩ }, λ i, by rwa F.res_self⟩ def to_presheaf {F : presheaf.{v} X} (S : subpresheaf F) : presheaf X := { F := λ U, S U, res := λ U V HVU x, ⟨F.res U V HVU x.1, S.2 HVU x.2⟩, res_self := λ U x, subtype.eq $ F.res_self U x.1, res_res := λ U V W HWV HVU x, subtype.eq $ F.res_res U V W HWV HVU x.1 } end subpresheaf structure subsheaf (F : presheaf.{v} X) extends subpresheaf F := (mem_of_res_mem : ∀ {U : opens X}, ∀ {s : F U}, ∀ OC : covering.{u} U, (∀ i : OC.ι, F.res U (OC.map i) (OC.map_le i) s ∈ to_set (OC.map i)) → s ∈ to_set U) namespace subsheaf def to_sheaf {F : sheaf.{v} X} (S : subsheaf F.to_presheaf) : sheaf X := { locality := λ U ⟨s, hs⟩ ⟨t, ht⟩ OC H, subtype.eq $ F.locality U s t OC $ λ i, have _ := congr_arg subtype.val (H i), this, gluing := λ U OC ss H, let ⟨s, hs⟩ := F.gluing U OC (λ i, (ss i).1) (λ i j, have _ := congr_arg subtype.val (H i j), this) in ⟨⟨s, S.mem_of_res_mem OC (λ i, show F.res _ _ _ _ ∈ _, by rw hs; exact (ss i).2)⟩, λ i, subtype.eq $ (hs i)⟩, .. S.to_subpresheaf.to_presheaf } end subsheaf def continuous_subsheaf (Y : Type v) [topological_space Y] : subsheaf (Func X Y).to_presheaf := { to_set := λ U, { f | continuous f }, res_mem_to_set := λ U V HVU s hs, hs.comp $ continuous_induced_rng continuous_induced_dom, mem_of_res_mem := λ U s OC H, continuous_iff_continuous_at.2 $ λ ⟨x, hxU⟩, let ⟨i, hi⟩ := OC.exists_of_mem x hxU in λ V HV, let ⟨t, htV, ⟨u, hu, hut⟩, hxt⟩ := mem_nhds_sets_iff.1 (continuous_iff_continuous_at.1 (H i) ⟨x, hi⟩ HV) in mem_nhds_sets_iff.2 ⟨subtype.val ⁻¹' (subtype.val '' t), by rintros ⟨y, hy⟩ ⟨z, hzt, hzy⟩; dsimp only at hzy; subst hzy; exact htV hzt, ⟨u ∩ OC.map i, is_open_inter hu (OC.map i).2, by rw [← hut, subtype.image_preimage_val]; refl⟩, ⟨x, hi⟩, hxt, rfl⟩ } namespace subpresheaf variables {X} (F : presheaf X) instance : has_sup (subpresheaf F) := ⟨λ S T, ⟨λ U, S U ∪ T U, λ U V HVU s, or.imp (S.2 HVU) (T.2 HVU)⟩⟩ instance : has_inf (subpresheaf F) := ⟨λ S T, ⟨λ U, S U ∩ T U, λ U V HVU s, and.imp (S.2 HVU) (T.2 HVU)⟩⟩ instance : has_Sup (subpresheaf F) := ⟨λ SS, ⟨λ U, ⋃ S ∈ SS, to_set S U, λ U V HVU s hs, let ⟨S, HS, hsS⟩ := set.mem_bUnion_iff.1 hs in set.mem_bUnion_iff.2 ⟨S, HS, S.2 HVU hsS⟩⟩⟩ instance : has_Inf (subpresheaf F) := ⟨λ SS, ⟨λ U, ⋂ S ∈ SS, to_set S U, λ U V HVU s hs, set.mem_bInter $ λ S HS, S.2 HVU $ set.mem_bInter_iff.1 hs S HS⟩⟩ instance : has_top (subpresheaf F) := ⟨⟨λ U, set.univ, λ U V HVU s _, trivial⟩⟩ instance : has_bot (subpresheaf F) := ⟨⟨λ U, ∅, λ U V HVU s, false.elim⟩⟩ instance subpresheaf.complete_lattice (F : presheaf X) : complete_lattice (subpresheaf F) := { le_sup_left := λ S T U, set.subset_union_left _ _, le_sup_right := λ S T U, set.subset_union_right _ _, sup_le := λ S1 S2 S3 H13 H23 U, set.union_subset (H13 U) (H23 U), inf_le_left := λ S T U, set.inter_subset_left _ _, inf_le_right := λ S T U, set.inter_subset_right _ _, le_inf := λ S1 S2 S3 H12 H13 U, set.subset_inter (H12 U) (H13 U), le_top := λ S U, set.subset_univ _, bot_le := λ S U, set.empty_subset _, le_Sup := λ SS S HS U, set.subset_bUnion_of_mem HS, Sup_le := λ SS S HS U, set.bUnion_subset $ λ T HT, HS T HT U, Inf_le := λ SS S HS U, set.bInter_subset_of_mem HS, le_Inf := λ SS S HS U, set.subset_bInter $ λ T HT, HS T HT U, .. subpresheaf.partial_order F, .. subpresheaf.lattice.has_sup F, .. subpresheaf.lattice.has_inf F, .. subpresheaf.lattice.has_Sup F, .. subpresheaf.lattice.has_Inf F, .. subpresheaf.lattice.has_top F, .. subpresheaf.lattice.has_bot F } end subpresheaf namespace subsheaf variables {X} (F : presheaf X) instance (F : presheaf.{v} X) : has_coe_to_fun (subsheaf F) := ⟨_, to_set⟩ instance (F : presheaf.{v} X) : partial_order (subsheaf F) := partial_order.lift to_set (λ ⟨x, hx1, hx2⟩ ⟨y, hy1, hy2⟩, mk.inj_eq.mpr) infer_instance instance : has_inf (subsheaf F) := ⟨λ S T, ⟨λ U, S U ∩ T U, λ U V HVU s, and.imp (S.2 HVU) (T.2 HVU), λ U s OC H, ⟨S.3 OC $ λ i, (H i).1, T.3 OC $ λ i, (H i).2⟩⟩⟩ instance : has_Inf (subsheaf F) := ⟨λ SS, ⟨λ U, ⋂ S ∈ SS, to_set S U, λ U V HVU s hs, set.mem_bInter $ λ S HS, S.2 HVU $ set.mem_bInter_iff.1 hs S HS, λ U s OC H, set.mem_bInter $ λ S HS, S.3 OC $ λ i, set.mem_bInter_iff.1 (H i) S HS⟩⟩ instance : has_top (subsheaf F) := ⟨⟨λ U, set.univ, λ U V HVU s _, trivial, λ _ _ _ _, trivial⟩⟩ instance subsheaf.semilattice_inf_top (F : presheaf X) : semilattice_inf_top (subsheaf F) := { inf_le_left := λ S T U, set.inter_subset_left _ _, inf_le_right := λ S T U, set.inter_subset_right _ _, le_inf := λ S1 S2 S3 H12 H13 U, set.subset_inter (H12 U) (H13 U), le_top := λ S U, set.subset_univ _, .. subsheaf.partial_order F, .. subsheaf.lattice.has_inf F, .. subsheaf.lattice.has_Inf F, .. subsheaf.lattice.has_top F } theorem Inf_le (SS : set (subsheaf F)) (S : subsheaf F) (HS : S ∈ SS) : Inf SS ≤ S := λ U, set.bInter_subset_of_mem HS theorem le_Inf (SS : set (subsheaf F)) (S : subsheaf F) (HS : ∀ b ∈ SS, S ≤ b) : S ≤ Inf SS := λ U, set.subset_bInter $ λ T HT, HS T HT U end subsheaf def section_subsheaf (Y : Type v) (π : Y → X) : subsheaf (Func X Y).to_presheaf := { to_set := λ U, { s | ∀ u : U, π (s u) = u.1 }, res_mem_to_set := λ U V HVU s hs ⟨v, hv⟩, hs ⟨v, _⟩, mem_of_res_mem := λ U s OC H ⟨u, hu⟩, let ⟨i, hi⟩ := OC.exists_of_mem u hu in H i ⟨u, hi⟩ } def continuous_section_subsheaf (Y : Type v) [topological_space Y] (π : Y → X) : subsheaf (Func X Y).to_presheaf := continuous_subsheaf Y ⊓ section_subsheaf Y π structure germ (F : presheaf X) (x : X) := (U : opens X) (hxU : x ∈ U) (s : F U) namespace germ variables (F : presheaf X) (x : X) instance : setoid (germ F x) := { r := λ g1 g2, ∃ U : opens X, x ∈ U ∧ ∃ H1 : U ≤ g1.U, ∃ H2 : U ≤ g2.U, F.res g1.U U H1 g1.s = F.res g2.U U H2 g2.s, iseqv := ⟨λ g1, ⟨g1.U, g1.2, le_refl _, le_refl _, rfl⟩, λ g1 g2 ⟨U, hx, H1, H2, H3⟩, ⟨U, hx, H2, H1, H3.symm⟩, λ g1 g2 g3 ⟨U, hxU, H1, H2, H3⟩ ⟨V, hxV, H4, H5, H6⟩, ⟨U ⊓ V, ⟨hxU, hxV⟩, le_trans inf_le_left H1, le_trans inf_le_right H5, calc F.res g1.U (U ⊓ V) (le_trans inf_le_left H1) g1.s = F.res U (U ⊓ V) inf_le_left (F.res g1.U U H1 g1.s) : by rw F.res_res ... = F.res U (U ⊓ V) inf_le_left (F.res g2.U U H2 g2.s) : by rw H3 ... = F.res V (U ⊓ V) inf_le_right (F.res g2.U V H4 g2.s) : by rw [F.res_res, F.res_res] ... = F.res V (U ⊓ V) inf_le_right (F.res g3.U V H5 g3.s) : by rw H6 ... = F.res g3.U (U ⊓ V) (le_trans inf_le_right H5) g3.s : by rw F.res_res⟩⟩ } end germ def stalk (F : presheaf X) (x : X) := quotient (germ.setoid F x) def to_stalk (F : presheaf X) (x : X) (U : opens X) (hxU : x ∈ U) (s : F U) : stalk F x := ⟦⟨U, hxU, s⟩⟧ theorem to_stalk_res (F : presheaf X) (x : X) (U V : opens X) (hxV : x ∈ V) (HVU : V ≤ U) (s : F U) : to_stalk F x V hxV (F.res U V HVU s) = to_stalk F x U (HVU hxV) s := quotient.sound ⟨V, hxV, le_refl V, HVU, F.res_res _ _ _ _ _ _⟩ def espace_etale (F : presheaf X) : Type (max u v) := Σ x : X, stalk F x def of_espace_etale (F : presheaf X) (x : espace_etale F) : X := x.1 def to_espace_etale (F : presheaf X) (U : opens X) (s : F U) (p : U) : espace_etale F := ⟨p.1, to_stalk F p.1 U p.2 s⟩ instance (F : presheaf X) : topological_space (espace_etale F) := { is_open := λ S, ∀ x ∈ S, ∃ U : opens X, ∃ hxU : sigma.fst x ∈ U, ∃ s : F U, to_stalk F x.1 U hxU s = x.2 ∧ ∀ p : X, ∀ hpU : p ∈ U, (⟨p, to_stalk F p U hpU s⟩ : espace_etale F) ∈ S, is_open_univ := λ ⟨p, g⟩ _, quotient.induction_on g $ λ ⟨U, hpU, s⟩, ⟨U, hpU, s, rfl, λ _ _, trivial⟩, is_open_inter := λ S T HS HT x hxST, let ⟨U, hxU, s, hsx, hs⟩ := HS x hxST.1, ⟨V, hxV, t, htx, ht⟩ := HT x hxST.2, ⟨W, hxW, HWU, HWV, h⟩ := quotient.exact (hsx.trans htx.symm) in ⟨W, hxW, F.res U W HWU s, by rw to_stalk_res; exact hsx, λ q hqW, ⟨by rw to_stalk_res; apply hs, by rw [h, to_stalk_res]; apply ht⟩⟩, is_open_sUnion := λ SS H x hx, let ⟨t, htSS, hxt⟩ := set.mem_sUnion.1 hx, ⟨U, hxU, s, hsx, hs⟩ := H t htSS x hxt in ⟨U, hxU, s, hsx, λ p hpU, set.mem_sUnion_of_mem (hs p hpU) htSS⟩ } theorem continuous_of_espace_etale (F : presheaf X) : continuous (of_espace_etale F) := λ U HU ⟨p, g⟩ hpU, quotient.induction_on g $ λ ⟨V, hpV, s⟩, ⟨⟨U, HU⟩ ⊓ V, ⟨hpU, hpV⟩, F.res V _ inf_le_right s, to_stalk_res _ _ _ _ _ _ _, λ q hqUV, hqUV.1⟩ theorem continuous_to_espace_etale (F : presheaf X) (U : opens X) (s : F U) : continuous (to_espace_etale F U s) := λ S HS, is_open_iff_forall_mem_open.2 $ λ q hq, let ⟨V, hqV, t, hts, ht⟩ := HS _ hq, ⟨W, hqW, HWV, HWU, HW⟩ := quotient.exact hts in ⟨subtype.val ⁻¹' W.1, λ p hpW, show (⟨_, _⟩ : espace_etale F) ∈ S, by erw [← to_stalk_res F p.1 U W hpW HWU, ← HW, to_stalk_res]; apply ht, continuous_subtype_val _ W.2, hqW⟩ def espace_etale.basic (F : presheaf X) (U : opens X) (s : F U) : opens (espace_etale F) := ⟨{ x | ∃ hxU : x.1 ∈ U, to_stalk F x.1 U hxU s = x.2 }, λ x ⟨hxU, hx⟩, ⟨U, hxU, s, hx, λ p hpU, ⟨hpU, rfl⟩⟩⟩ structure presheaf.hom (F : presheaf X) (G : presheaf X) := (to_fun : Π U : opens X, F U → G U) (to_fun_res : ∀ U V : opens X, ∀ HVU : V ≤ U, ∀ s : F U, to_fun V (F.res U V HVU s) = G.res U V HVU (to_fun U s)) structure presheaf.equiv (F : presheaf X) (G : presheaf X) := (to_fun : Π U : opens X, F U → G U) (inv_fun : Π U : opens X, G U → F U) (left_inv : ∀ U : opens X, ∀ s : F U, inv_fun U (to_fun U s) = s) (right_inv : ∀ U : opens X, ∀ s : G U, to_fun U (inv_fun U s) = s) (to_fun_res : ∀ U V : opens X, ∀ HVU : V ≤ U, ∀ s : F U, to_fun V (F.res U V HVU s) = G.res U V HVU (to_fun U s)) theorem sheaf.locality' (F : sheaf X) {U : opens X} {s t : F.to_presheaf U} (H : ∀ x ∈ U, ∃ V : opens X, ∃ HVU : V ≤ U, x ∈ V ∧ F.res U V HVU s = F.res U V HVU t) : s = t := F.locality U s t ⟨U, λ p, classical.some $ H p.1 p.2, λ p, classical.some $ classical.some_spec $ H p.1 p.2, λ p hp, ⟨⟨p, hp⟩, (classical.some_spec $ classical.some_spec $ H p hp).1⟩⟩ $ λ p, (classical.some_spec $ classical.some_spec $ H p.1 p.2).2 theorem sheaf.locality'' (F : sheaf X) {U : opens X} {s t : F.to_presheaf U} (H : ∀ x ∈ U, to_stalk F.to_presheaf x U H s = to_stalk F.to_presheaf x U H t) : s = t := F.locality' $ λ x hxU, let ⟨V, hxV, HVU, HVU', hv⟩ := quotient.exact (H x hxU) in ⟨V, HVU, hxV, hv⟩ theorem germ.eta (F : presheaf X) (x : X) (g : germ F x) (H) : (⟨g.1, H, g.3⟩ : germ F x) = g := by cases g; refl noncomputable def sheaf.glue (F : sheaf X) (U : opens X) (OC : covering U) (S : Π i : OC.ι, F.to_presheaf (OC.map i)) (H : ∀ i j : OC.ι, F.res (OC.map i) (OC.map i ⊓ OC.map j) inf_le_left (S i) = F.res (OC.map j) (OC.map i ⊓ OC.map j) inf_le_right (S j)) : F.to_presheaf U := classical.some $ F.gluing U OC S H theorem res_glue (F : sheaf X) (U : opens X) (OC : covering U) (S H i) : F.res U (OC.map i) (OC.map_le i) (F.glue U OC S H) = S i := classical.some_spec (F.gluing U OC S H) i theorem glue_eq (F : sheaf X) (U : opens X) (OC : covering U) (S : Π i : OC.ι, F.to_presheaf (OC.map i)) (H s) (H2 : ∀ i, F.res U (OC.map i) (OC.map_le i) s = S i) : F.glue U OC S H = s := F.locality _ _ _ OC $ λ i, by rw [res_glue, H2] theorem to_stalk_glue (F : sheaf X) {U : opens X} {OC : covering U} {S : Π i : OC.ι, F.to_presheaf (OC.map i)} {H p} (i) (H2 : p ∈ OC.map i) : to_stalk F.to_presheaf p U (OC.map_le i H2) (F.glue U OC S H) = to_stalk F.to_presheaf p (OC.map i) H2 (S i) := by rw ← to_stalk_res; congr' 1; apply res_glue noncomputable def equiv_continuous_section_espace_etale (F : sheaf X) : F.to_presheaf.equiv (continuous_section_subsheaf (espace_etale F.to_presheaf) (of_espace_etale F.to_presheaf)).to_sheaf.to_presheaf := { to_fun := λ U s, ⟨to_espace_etale F.to_presheaf U s, continuous_to_espace_etale F.to_presheaf U s, λ p, rfl⟩, inv_fun := λ U s, F.glue U ⟨U, λ p, ⟨subtype.val '' (s.1 ⁻¹' (espace_etale.basic F.to_presheaf (quotient.out (s.1 p).2).1 (quotient.out (s.1 p).2).3).1), let ⟨V, hv1, hv2⟩ := s.2.1 (espace_etale.basic F.to_presheaf (quotient.out (s.1 p).2).1 (quotient.out (s.1 p).2).3).1 (espace_etale.basic F.to_presheaf (quotient.out (s.1 p).2).1 (quotient.out (s.1 p).2).3).2 in by rw [← hv2, subtype.image_preimage_val]; exact is_open_inter hv1 U.2⟩, λ p q ⟨r, hr, hrq⟩, hrq ▸ r.2, λ p hpU, ⟨⟨p, hpU⟩, ⟨p, hpU⟩, ⟨(quotient.out (s.1 ⟨p, hpU⟩).2).2, by dsimp only [to_stalk]; rw germ.eta; exact quotient.out_eq _⟩, rfl⟩⟩ (λ p, F.res (quotient.out (s.1 p).2).1 _ (λ q ⟨r, ⟨hsr1, hsr2⟩, hrq⟩, hrq ▸ s.2.2 r ▸ hsr1) (quotient.out (s.1 p).2).3) (λ p q, F.locality'' $ λ r ⟨⟨u, ⟨hsu1, hsu2⟩, hur⟩, ⟨v, ⟨hsv1, hsv2⟩, hvr⟩⟩, have huv : u = v, from subtype.eq $ hur.trans hvr.symm, have hsu : (s.1 u).1 = u.1, from s.2.2 u, begin clear_, iterate 4 { erw to_stalk_res }; substs huv hur; cases u with u hu; dsimp only at *, generalize : equiv_continuous_section_espace_etale._match_3 F U s p u _ = h1, generalize : equiv_continuous_section_espace_etale._match_3 F U s q u _ = h2, revert h1 h2, rw ← hsu, intros, erw [hsu2, hsv2] end), left_inv := λ U s, glue_eq _ _ _ _ _ _ $ λ p, F.locality'' $ λ q ⟨u, ⟨hsu1, hsu2⟩, huq⟩, by clear_; erw [to_stalk_res, to_stalk_res]; subst huq; exact hsu2.symm, right_inv := λ U s, subtype.eq $ funext $ λ p, sigma.eq (s.2.2 p).symm $ begin dsimp only [to_espace_etale], generalize : to_espace_etale._proof_1 U p = h1, generalize : (s.2.2 p).symm = h2, revert h1 h2, rw ← s.2.2 p, intros, dsimp only, erw to_stalk_glue F, swap 3, exact p, swap, { refine ⟨p, ⟨(quotient.out (s.1 p).2).2, _⟩, (s.2.2 p).symm⟩, dsimp only [to_stalk], rw [germ.eta, quotient.out_eq] }, erw to_stalk_res, dsimp only [to_stalk], rw [germ.eta, quotient.out_eq] end, to_fun_res := λ U V HVU s, subtype.eq $ funext $ λ p, sigma.eq rfl $ to_stalk_res _ _ _ _ _ _ _ }
07e737f27beab1de3eeec103423cd5d29c497c3c
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/sum.lean
46920ee8337bc8a2e09365f11bf38671c3447c8b
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
5,610
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury G. Kudryashov -/ import tactic.lint /-! # More theorems about the sum type -/ universes u v w x variables {α : Type u} {α' : Type w} {β : Type v} {β' : Type x} open sum attribute [derive decidable_eq] sum @[simp] theorem sum.forall {p : α ⊕ β → Prop} : (∀ x, p x) ↔ (∀ a, p (inl a)) ∧ (∀ b, p (inr b)) := ⟨λ h, ⟨λ a, h _, λ b, h _⟩, λ ⟨h₁, h₂⟩, sum.rec h₁ h₂⟩ @[simp] theorem sum.exists {p : α ⊕ β → Prop} : (∃ x, p x) ↔ (∃ a, p (inl a)) ∨ ∃ b, p (inr b) := ⟨λ h, match h with | ⟨inl a, h⟩ := or.inl ⟨a, h⟩ | ⟨inr b, h⟩ := or.inr ⟨b, h⟩ end, λ h, match h with | or.inl ⟨a, h⟩ := ⟨inl a, h⟩ | or.inr ⟨b, h⟩ := ⟨inr b, h⟩ end⟩ namespace sum /-- Map `α ⊕ β` to `α' ⊕ β'` sending `α` to `α'` and `β` to `β'`. -/ protected def map (f : α → α') (g : β → β') : α ⊕ β → α' ⊕ β' | (sum.inl x) := sum.inl (f x) | (sum.inr x) := sum.inr (g x) @[simp] lemma map_inl (f : α → α') (g : β → β') (x : α) : (inl x).map f g = inl (f x) := rfl @[simp] lemma map_inr (f : α → α') (g : β → β') (x : β) : (inr x).map f g = inr (g x) := rfl @[simp] lemma map_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') : ∀ x : α ⊕ β, (x.map f g).map f' g' = x.map (f' ∘ f) (g' ∘ g) | (inl a) := rfl | (inr b) := rfl @[simp] lemma map_comp_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') : (sum.map f' g') ∘ (sum.map f g) = sum.map (f' ∘ f) (g' ∘ g) := funext $ map_map f' g' f g @[simp] lemma map_id_id (α β) : sum.map (@id α) (@id β) = id := funext $ λ x, sum.rec_on x (λ _, rfl) (λ _, rfl) theorem inl.inj_iff {a b} : (inl a : α ⊕ β) = inl b ↔ a = b := ⟨inl.inj, congr_arg _⟩ theorem inr.inj_iff {a b} : (inr a : α ⊕ β) = inr b ↔ a = b := ⟨inr.inj, congr_arg _⟩ theorem inl_ne_inr {a : α} {b : β} : inl a ≠ inr b. theorem inr_ne_inl {a : α} {b : β} : inr b ≠ inl a. /-- Define a function on `α ⊕ β` by giving separate definitions on `α` and `β`. -/ protected def elim {α β γ : Sort*} (f : α → γ) (g : β → γ) : α ⊕ β → γ := λ x, sum.rec_on x f g @[simp] lemma elim_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : α) : sum.elim f g (inl x) = f x := rfl @[simp] lemma elim_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : β) : sum.elim f g (inr x) = g x := rfl lemma elim_injective {α β γ : Sort*} {f : α → γ} {g : β → γ} (hf : function.injective f) (hg : function.injective g) (hfg : ∀ a b, f a ≠ g b) : function.injective (sum.elim f g) := λ x y, sum.rec_on x (sum.rec_on y (λ x y hxy, by rw hf hxy) (λ x y hxy, false.elim $ hfg _ _ hxy)) (sum.rec_on y (λ x y hxy, false.elim $ hfg x y hxy.symm) (λ x y hxy, by rw hg hxy)) section variables (ra : α → α → Prop) (rb : β → β → Prop) /-- Lexicographic order for sum. Sort all the `inl a` before the `inr b`, otherwise use the respective order on `α` or `β`. -/ inductive lex : α ⊕ β → α ⊕ β → Prop | inl {a₁ a₂} (h : ra a₁ a₂) : lex (inl a₁) (inl a₂) | inr {b₁ b₂} (h : rb b₁ b₂) : lex (inr b₁) (inr b₂) | sep (a b) : lex (inl a) (inr b) variables {ra rb} @[simp] theorem lex_inl_inl {a₁ a₂} : lex ra rb (inl a₁) (inl a₂) ↔ ra a₁ a₂ := ⟨λ h, by cases h; assumption, lex.inl⟩ @[simp] theorem lex_inr_inr {b₁ b₂} : lex ra rb (inr b₁) (inr b₂) ↔ rb b₁ b₂ := ⟨λ h, by cases h; assumption, lex.inr⟩ @[simp] theorem lex_inr_inl {b a} : ¬ lex ra rb (inr b) (inl a) := λ h, by cases h attribute [simp] lex.sep theorem lex_acc_inl {a} (aca : acc ra a) : acc (lex ra rb) (inl a) := begin induction aca with a H IH, constructor, intros y h, cases h with a' _ h', exact IH _ h' end theorem lex_acc_inr (aca : ∀ a, acc (lex ra rb) (inl a)) {b} (acb : acc rb b) : acc (lex ra rb) (inr b) := begin induction acb with b H IH, constructor, intros y h, cases h with _ _ _ b' _ h' a, { exact IH _ h' }, { exact aca _ } end theorem lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := have aca : ∀ a, acc (lex ra rb) (inl a), from λ a, lex_acc_inl (ha.apply a), ⟨λ x, sum.rec_on x aca (λ b, lex_acc_inr aca (hb.apply b))⟩ end /-- Swap the factors of a sum type -/ @[simp] def swap : α ⊕ β → β ⊕ α | (inl a) := inr a | (inr b) := inl b @[simp] lemma swap_swap (x : α ⊕ β) : swap (swap x) = x := by cases x; refl @[simp] lemma swap_swap_eq : swap ∘ swap = @id (α ⊕ β) := funext $ swap_swap @[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap := swap_swap @[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap := swap_swap end sum namespace function open sum lemma injective.sum_map {f : α → β} {g : α' → β'} (hf : injective f) (hg : injective g) : injective (sum.map f g) | (inl x) (inl y) h := congr_arg inl $ hf $ inl.inj h | (inr x) (inr y) h := congr_arg inr $ hg $ inr.inj h lemma surjective.sum_map {f : α → β} {g : α' → β'} (hf : surjective f) (hg : surjective g) : surjective (sum.map f g) | (inl y) := let ⟨x, hx⟩ := hf y in ⟨inl x, congr_arg inl hx⟩ | (inr y) := let ⟨x, hx⟩ := hg y in ⟨inr x, congr_arg inr hx⟩ end function
b7b15491474accb751039ce0a37eec88ebd6c90b
2c41ae31b2b771ad5646ad880201393f5269a7f0
/Lean/Qualities/Dependable.lean
79af3e140bda8bc5c58b3902a6952b6e42f0bc3e
[]
no_license
kevinsullivan/Boehm
926f25bc6f1a8b6bd47d333d936fdfc278228312
55208395bff20d48a598b7fa33a4d55a2447a9cf
refs/heads/master
1,586,127,134,302
1,488,252,326,000
1,488,252,326,000
32,836,930
0
0
null
null
null
null
UTF-8
Lean
false
false
782
lean
import SystemModel.System import Qualities.Secure import Qualities.Safe import Qualities.Reliable import Qualities.Maintainable import Qualities.Available import Qualities.Survivable import Qualities.Robust /- Dependable -/ /- [Dependable] is a composite attribute of [Security], [Safety], [Reliability], ..., and [Robustness]. Informally, An instance of type [SystemType] is deemed [Dependable] if and only if all the requirements of its sub-attributes are satisfied given the same conditions. -/ inductive Dependable (sys_type: SystemType) : Prop | intro : Secure sys_type -> Safe sys_type -> Reliable sys_type -> Maintainable sys_type -> Available sys_type -> Survivable sys_type -> Robust sys_type -> Dependable
858e5d80a0a6b44676f8c87eb13735b72db74107
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/stream.lean
4638c89b053aad43012ff982bf4970488854b3a6
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
22,532
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 -/ import data.nat data.list data.equiv open nat function option definition stream (A : Type) := nat → A namespace stream variables {A B C : Type} definition cons (a : A) (s : stream A) : stream A := λ i, match i with | 0 := a | succ n := s n end notation h :: t := cons h t definition head [reducible] (s : stream A) : A := s 0 definition tail (s : stream A) : stream A := λ i, s (i+1) definition drop (n : nat) (s : stream A) : stream A := λ i, s (i+n) definition nth [reducible] (n : nat) (s : stream A) : A := s n protected theorem eta (s : stream A) : head s :: tail s = s := funext (λ i, begin cases i, repeat reflexivity end) theorem nth_zero_cons (a : A) (s : stream A) : nth 0 (a :: s) = a := rfl theorem head_cons (a : A) (s : stream A) : head (a :: s) = a := rfl theorem tail_cons (a : A) (s : stream A) : tail (a :: s) = s := rfl theorem tail_drop (n : nat) (s : stream A) : tail (drop n s) = drop n (tail s) := funext (λ i, begin esimp [tail, drop], congruence, rewrite add.right_comm end) theorem nth_drop (n m : nat) (s : stream A) : nth n (drop m s) = nth (n+m) s := rfl theorem tail_eq_drop (s : stream A) : tail s = drop 1 s := rfl theorem drop_drop (n m : nat) (s : stream A) : drop n (drop m s) = drop (n+m) s := funext (λ i, begin esimp [drop], rewrite add.assoc end) theorem nth_succ (n : nat) (s : stream A) : nth (succ n) s = nth n (tail s) := rfl theorem drop_succ (n : nat) (s : stream A) : drop (succ n) s = drop n (tail s) := rfl protected theorem ext {s₁ s₂ : stream A} : (∀ n, nth n s₁ = nth n s₂) → s₁ = s₂ := assume h, funext h definition all (p : A → Prop) (s : stream A) := ∀ n, p (nth n s) definition any (p : A → Prop) (s : stream A) := ∃ n, p (nth n s) theorem all_def (p : A → Prop) (s : stream A) : all p s = ∀ n, p (nth n s) := rfl theorem any_def (p : A → Prop) (s : stream A) : any p s = ∃ n, p (nth n s) := rfl definition mem (a : A) (s : stream A) := any (λ b, a = b) s notation e ∈ s := mem e s theorem mem_cons (a : A) (s : stream A) : a ∈ (a::s) := exists.intro 0 rfl theorem mem_cons_of_mem {a : A} {s : stream A} (b : A) : a ∈ s → a ∈ b :: s := assume ains, obtain n (h : a = nth n s), from ains, exists.intro (succ n) (by rewrite [nth_succ, tail_cons, h]) theorem eq_or_mem_of_mem_cons {a b : A} {s : stream A} : a ∈ b::s → a = b ∨ a ∈ s := assume ainbs, obtain n (h : a = nth n (b::s)), from ainbs, begin cases n with n', {left, exact h}, {right, rewrite [nth_succ at h, tail_cons at h], existsi n', exact h} end theorem mem_of_nth_eq {n : nat} {s : stream A} {a : A} : a = nth n s → a ∈ s := assume h, exists.intro n h section map variable (f : A → B) definition map (s : stream A) : stream B := λ n, f (nth n s) theorem drop_map (n : nat) (s : stream A) : drop n (map f s) = map f (drop n s) := stream.ext (λ i, rfl) theorem nth_map (n : nat) (s : stream A) : nth n (map f s) = f (nth n s) := rfl theorem tail_map (s : stream A) : tail (map f s) = map f (tail s) := begin rewrite tail_eq_drop end theorem head_map (s : stream A) : head (map f s) = f (head s) := rfl theorem map_eq (s : stream A) : map f s = f (head s) :: map f (tail s) := by rewrite [-stream.eta, tail_map, head_map] theorem map_cons (a : A) (s : stream A) : map f (a :: s) = f a :: map f s := by rewrite [-stream.eta, map_eq] theorem map_id (s : stream A) : map id s = s := rfl theorem map_map (g : B → C) (f : A → B) (s : stream A) : map g (map f s) = map (g ∘ f) s := rfl theorem mem_map {a : A} {s : stream A} : a ∈ s → f a ∈ map f s := assume ains, obtain n (h : a = nth n s), from ains, exists.intro n (by rewrite [nth_map, h]) end map section zip variable (f : A → B → C) definition zip (s₁ : stream A) (s₂ : stream B) : stream C := λ n, f (nth n s₁) (nth n s₂) theorem drop_zip (n : nat) (s₁ : stream A) (s₂ : stream B) : drop n (zip f s₁ s₂) = zip f (drop n s₁) (drop n s₂) := stream.ext (λ i, rfl) theorem nth_zip (n : nat) (s₁ : stream A) (s₂ : stream B) : nth n (zip f s₁ s₂) = f (nth n s₁) (nth n s₂) := rfl theorem head_zip (s₁ : stream A) (s₂ : stream B) : head (zip f s₁ s₂) = f (head s₁) (head s₂) := rfl theorem tail_zip (s₁ : stream A) (s₂ : stream B) : tail (zip f s₁ s₂) = zip f (tail s₁) (tail s₂) := rfl theorem zip_eq (s₁ : stream A) (s₂ : stream B) : zip f s₁ s₂ = f (head s₁) (head s₂) :: zip f (tail s₁) (tail s₂) := by rewrite [-stream.eta] end zip definition const (a : A) : stream A := λ n, a theorem mem_const (a : A) : a ∈ const a := exists.intro 0 rfl theorem const_eq (a : A) : const a = a :: const a := begin apply stream.ext, intro n, cases n, repeat reflexivity end theorem tail_const (a : A) : tail (const a) = const a := by rewrite [const_eq at {1}] theorem map_const (f : A → B) (a : A) : map f (const a) = const (f a) := rfl theorem nth_const (n : nat) (a : A) : nth n (const a) = a := rfl theorem drop_const (n : nat) (a : A) : drop n (const a) = const a := stream.ext (λ i, rfl) definition iterate (f : A → A) (a : A) : stream A := λ n, nat.rec_on n a (λ n r, f r) theorem head_iterate (f : A → A) (a : A) : head (iterate f a) = a := rfl theorem tail_iterate (f : A → A) (a : A) : tail (iterate f a) = iterate f (f a) := begin apply funext, intro n, induction n with n' IH, {reflexivity}, {esimp [tail, iterate] at *, rewrite add_one at *, esimp at *, rewrite IH} end theorem iterate_eq (f : A → A) (a : A) : iterate f a = a :: iterate f (f a) := begin rewrite [-stream.eta], congruence, exact !tail_iterate end theorem nth_zero_iterate (f : A → A) (a : A) : nth 0 (iterate f a) = a := rfl theorem nth_succ_iterate (n : nat) (f : A → A) (a : A) : nth (succ n) (iterate f a) = nth n (iterate f (f a)) := by rewrite [nth_succ, tail_iterate] section bisim variable (R : stream A → stream A → Prop) local infix ~ := R definition is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → head s₁ = head s₂ ∧ tail s₁ ~ tail s₂ lemma nth_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂} n, s₁ ~ s₂ → nth n s₁ = nth n s₂ ∧ drop (n+1) s₁ ~ drop (n+1) s₂ | s₁ s₂ 0 h := bisim h | s₁ s₂ (n+1) h := obtain h₁ (trel : tail s₁ ~ tail s₂), from bisim h, nth_of_bisim n trel -- If two streams are bisimilar, then they are equal theorem eq_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂}, s₁ ~ s₂ → s₁ = s₂ := λ s₁ s₂ r, stream.ext (λ n, and.elim_left (nth_of_bisim R bisim n r)) end bisim theorem bisim_simple (s₁ s₂ : stream A) : head s₁ = head s₂ → s₁ = tail s₁ → s₂ = tail s₂ → s₁ = s₂ := assume hh ht₁ ht₂, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ s₁ = tail s₁ ∧ s₂ = tail s₂) (λ s₁ s₂ h, obtain h₁ h₂ h₃, from h, begin constructor, exact h₁, rewrite [-h₂, -h₃], exact h end) (and.intro hh (and.intro ht₁ ht₂)) -- AKA coinduction freeze theorem coinduction.{l} {A : Type.{l}} {s₁ s₂ : stream A} : head s₁ = head s₂ → (∀ (B : Type.{l}) (fr : stream A → B), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂ := assume hh ht, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ ∀ (B : Type) (fr : stream A → B), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) (λ s₁ s₂ h, have h₁ : head s₁ = head s₂, from and.elim_left h, have h₂ : head (tail s₁) = head (tail s₂), from and.elim_right h A (@head A) h₁, have h₃ : ∀ (B : Type) (fr : stream A → B), fr (tail s₁) = fr (tail s₂) → fr (tail (tail s₁)) = fr (tail (tail s₂)), from λ B fr, and.elim_right h B (λ s, fr (tail s)), and.intro h₁ (and.intro h₂ h₃)) (and.intro hh ht) theorem iterate_id (a : A) : iterate id a = const a := coinduction rfl (λ B fr ch, by rewrite [tail_iterate, tail_const]; exact ch) local attribute stream [reducible] theorem map_iterate (f : A → A) (a : A) : iterate f (f a) = map f (iterate f a) := begin apply funext, intro n, induction n with n' IH, {reflexivity}, { esimp [map, iterate, nth] at *, rewrite IH } end section corec definition corec (f : A → B) (g : A → A) : A → stream B := λ a, map f (iterate g a) theorem corec_def (f : A → B) (g : A → A) (a : A) : corec f g a = map f (iterate g a) := rfl theorem corec_eq (f : A → B) (g : A → A) (a : A) : corec f g a = f a :: corec f g (g a) := by rewrite [corec_def, map_eq, head_iterate, tail_iterate] theorem corec_id_id_eq_const (a : A) : corec id id a = const a := by rewrite [corec_def, map_id, iterate_id] theorem corec_id_f_eq_iterate (f : A → A) (a : A) : corec id f a = iterate f a := rfl end corec -- corec is also known as unfold definition unfolds (g : A → B) (f : A → A) (a : A) : stream B := corec g f a theorem unfolds_eq (g : A → B) (f : A → A) (a : A) : unfolds g f a = g a :: unfolds g f (f a) := by esimp [ unfolds ]; rewrite [corec_eq] theorem nth_unfolds_head_tail : ∀ (n : nat) (s : stream A), nth n (unfolds head tail s) = nth n s := begin intro n, induction n with n' ih, {intro s, reflexivity}, {intro s, rewrite [*nth_succ, unfolds_eq, tail_cons, ih]} end theorem unfolds_head_eq : ∀ (s : stream A), unfolds head tail s = s := λ s, stream.ext (λ n, nth_unfolds_head_tail n s) definition interleave (s₁ s₂ : stream A) : stream A := corec (λ p, obtain s₁ s₂, from p, head s₁) (λ p, obtain s₁ s₂, from p, (s₂, tail s₁)) (s₁, s₂) infix `⋈`:65 := interleave theorem interleave_eq (s₁ s₂ : stream A) : s₁ ⋈ s₂ = head s₁ :: head s₂ :: (tail s₁ ⋈ tail s₂) := begin esimp [interleave], rewrite corec_eq, esimp, congruence, rewrite corec_eq end theorem tail_interleave (s₁ s₂ : stream A) : tail (s₁ ⋈ s₂) = s₂ ⋈ (tail s₁) := by esimp [interleave]; rewrite corec_eq theorem interleave_tail_tail (s₁ s₂ : stream A) : tail s₁ ⋈ tail s₂ = tail (tail (s₁ ⋈ s₂)) := by rewrite [interleave_eq s₁ s₂] theorem nth_interleave_left : ∀ (n : nat) (s₁ s₂ : stream A), nth (2*n) (s₁ ⋈ s₂) = nth n s₁ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n))) (s₁ ⋈ s₂) = nth (succ n) s₁, rewrite [*nth_succ, interleave_eq, *tail_cons, nth_interleave_left] end theorem nth_interleave_right : ∀ (n : nat) (s₁ s₂ : stream A), nth (2*n+1) (s₁ ⋈ s₂) = nth n s₂ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n+1))) (s₁ ⋈ s₂) = nth (succ n) s₂, rewrite [*nth_succ, interleave_eq, *tail_cons, nth_interleave_right] end theorem mem_interleave_left {a : A} {s₁ : stream A} (s₂ : stream A) : a ∈ s₁ → a ∈ s₁ ⋈ s₂ := assume ains₁, obtain n h, from ains₁, exists.intro (2*n) (by rewrite [h, nth_interleave_left]) theorem mem_interleave_right {a : A} {s₁ : stream A} (s₂ : stream A) : a ∈ s₂ → a ∈ s₁ ⋈ s₂ := assume ains₂, obtain n h, from ains₂, exists.intro (2*n+1) (by rewrite [h, nth_interleave_right]) definition even (s : stream A) : stream A := corec (λ s, head s) (λ s, tail (tail s)) s definition odd (s : stream A) : stream A := even (tail s) theorem odd_eq (s : stream A) : odd s = even (tail s) := rfl theorem head_even (s : stream A) : head (even s) = head s := rfl theorem tail_even (s : stream A) : tail (even s) = even (tail (tail s)) := by esimp [even]; rewrite corec_eq theorem even_cons_cons (a₁ a₂ : A) (s : stream A) : even (a₁ :: a₂ :: s) = a₁ :: even s := by esimp [even]; rewrite corec_eq theorem even_tail (s : stream A) : even (tail s) = odd s := rfl theorem even_interleave (s₁ s₂ : stream A) : even (s₁ ⋈ s₂) = s₁ := eq_of_bisim (λ s₁' s₁, ∃ s₂, s₁' = even (s₁ ⋈ s₂)) (λ s₁' s₁ h, obtain s₂ (h₁ : s₁' = even (s₁ ⋈ s₂)), from h, begin rewrite h₁, constructor, {reflexivity}, {existsi (tail s₂), rewrite [interleave_eq, even_cons_cons, tail_cons]} end) (exists.intro s₂ rfl) theorem interleave_even_odd (s₁ : stream A) : even s₁ ⋈ odd s₁ = s₁ := eq_of_bisim (λ s' s, s' = even s ⋈ odd s) (λ s' s (h : s' = even s ⋈ odd s), begin rewrite h, constructor, {reflexivity}, {esimp, rewrite [*odd_eq, tail_interleave, tail_even]} end) rfl theorem nth_even : ∀ (n : nat) (s : stream A), nth n (even s) = nth (2*n) s | 0 s := rfl | (succ n) s := begin change nth (succ n) (even s) = nth (succ (succ (2 * n))) s, rewrite [+nth_succ, tail_even, nth_even] end theorem nth_odd : ∀ (n : nat) (s : stream A), nth n (odd s) = nth (2*n + 1) s := λ n s, by rewrite [odd_eq, nth_even] theorem mem_of_mem_even (a : A) (s : stream A) : a ∈ even s → a ∈ s := assume aines, obtain n h, from aines, exists.intro (2*n) (by rewrite [h, nth_even]) theorem mem_of_mem_odd (a : A) (s : stream A) : a ∈ odd s → a ∈ s := assume ainos, obtain n h, from ainos, exists.intro (2*n+1) (by rewrite [h, nth_odd]) open list definition append : list A → stream A → stream A | [] s := s | (a::l) s := a :: append l s theorem nil_append (s : stream A) : append [] s = s := rfl theorem cons_append (a : A) (l : list A) (s : stream A) : append (a::l) s = a :: append l s := rfl infix ++ := append -- the following local notation is used just to make the following theorem clear local infix `++ₛ`:65 := append theorem append_append : ∀ (l₁ l₂ : list A) (s : stream A), (l₁ ++ l₂) ++ₛ s = l₁ ++ (l₂ ++ₛ s) | [] l₂ s := rfl | (a::l₁) l₂ s := by rewrite [list.append_cons, *cons_append, append_append] theorem map_append (f : A → B) : ∀ (l : list A) (s : stream A), map f (l ++ s) = list.map f l ++ map f s | [] s := rfl | (a::l) s := by rewrite [cons_append, list.map_cons, map_cons, cons_append, map_append] theorem drop_append : ∀ (l : list A) (s : stream A), drop (length l) (l ++ s) = s | [] s := by esimp | (a::l) s := by rewrite [length_cons, add_one, drop_succ, cons_append, tail_cons, drop_append] theorem append_head_tail (s : stream A) : [head s] ++ tail s = s := by rewrite [cons_append, nil_append, stream.eta] theorem mem_append_right : ∀ {a : A} (l : list A) {s : stream A}, a ∈ s → a ∈ l ++ s | a [] s h := h | a (b::l) s h := have ih : a ∈ l ++ s, from mem_append_right l h, !mem_cons_of_mem ih theorem mem_append_left : ∀ {a : A} {l : list A} (s : stream A), a ∈ l → a ∈ l ++ s | a [] s h := absurd h !not_mem_nil | a (b::l) s h := or.elim (list.eq_or_mem_of_mem_cons h) (λ (aeqb : a = b), exists.intro 0 aeqb) (λ (ainl : a ∈ l), mem_cons_of_mem b (mem_append_left s ainl)) definition approx : nat → stream A → list A | 0 s := [] | (n+1) s := head s :: approx n (tail s) theorem approx_zero (s : stream A) : approx 0 s = [] := rfl theorem approx_succ (n : nat) (s : stream A) : approx (succ n) s = head s :: approx n (tail s) := rfl theorem nth_approx : ∀ (n : nat) (s : stream A), list.nth (approx (succ n) s) n = some (nth n s) | 0 s := rfl | (n+1) s := begin rewrite [approx_succ, add_one, list.nth_succ, nth_approx] end theorem append_approx_drop : ∀ (n : nat) (s : stream A), append (approx n s) (drop n s) = s := begin intro n, induction n with n' ih, {intro s, reflexivity}, {intro s, rewrite [approx_succ, drop_succ, cons_append, ih (tail s), stream.eta]} end -- Take lemma reduces a proof of equality of infinite streams to an -- induction over all their finite approximations. theorem take_lemma (s₁ s₂ : stream A) : (∀ (n : nat), approx n s₁ = approx n s₂) → s₁ = s₂ := begin intro h, apply stream.ext, intro n, induction n with n ih, {injection (h 1) with aux, exact aux}, {have h₁ : some (nth (succ n) s₁) = some (nth (succ n) s₂), by rewrite [-*nth_approx, h (succ (succ n))], injection h₁, assumption} end -- auxiliary definition for cycle corecursive definition private definition cycle_f : A × list A × A × list A → A | (v, _, _, _) := v -- auxiliary definition for cycle corecursive definition private definition cycle_g : A × list A × A × list A → A × list A × A × list A | (v₁, [], v₀, l₀) := (v₀, l₀, v₀, l₀) | (v₁, v₂::l₂, v₀, l₀) := (v₂, l₂, v₀, l₀) private lemma cycle_g_cons (a : A) (a₁ : A) (l₁ : list A) (a₀ : A) (l₀ : list A) : cycle_g (a, a₁::l₁, a₀, l₀) = (a₁, l₁, a₀, l₀) := rfl definition cycle : Π (l : list A), l ≠ nil → stream A | [] h := absurd rfl h | (a::l) h := corec cycle_f cycle_g (a, l, a, l) theorem cycle_eq : ∀ (l : list A) (h : l ≠ nil), cycle l h = l ++ cycle l h | [] h := absurd rfl h | (a::l) h := have gen : ∀ l' a', corec cycle_f cycle_g (a', l', a, l) = (a' :: l') ++ₛ corec cycle_f cycle_g (a, l, a, l), begin intro l', induction l' with a₁ l₁ ih, {intro a', rewrite [corec_eq]}, {intro a', rewrite [corec_eq, cycle_g_cons, ih a₁]} end, gen l a theorem mem_cycle {a : A} {l : list A} : ∀ (h : l ≠ []), a ∈ l → a ∈ cycle l h := assume h ainl, by rewrite [cycle_eq]; exact !mem_append_left ainl theorem cycle_singleton (a : A) (h : [a] ≠ nil) : cycle [a] h = const a := coinduction rfl (λ B fr ch, by rewrite [cycle_eq, const_eq]; exact ch) definition tails (s : stream A) : stream (stream A) := corec id tail (tail s) theorem tails_eq (s : stream A) : tails s = tail s :: tails (tail s) := by esimp [tails]; rewrite [corec_eq] theorem nth_tails : ∀ (n : nat) (s : stream A), nth n (tails s) = drop n (tail s) := begin intro n, induction n with n' ih, {intros, reflexivity}, {intro s, rewrite [nth_succ, drop_succ, tails_eq, tail_cons, ih]} end theorem tails_eq_iterate (s : stream A) : tails s = iterate tail (tail s) := rfl definition inits_core (l : list A) (s : stream A) : stream (list A) := corec prod.pr1 (λ p, match p with (l', s') := (l' ++ [head s'], tail s') end) (l, s) definition inits (s : stream A) : stream (list A) := inits_core [head s] (tail s) theorem inits_core_eq (l : list A) (s : stream A) : inits_core l s = l :: inits_core (l ++ [head s]) (tail s) := by esimp [inits_core]; rewrite [corec_eq] theorem tail_inits (s : stream A) : tail (inits s) = inits_core [head s, head (tail s)] (tail (tail s)) := by esimp [inits]; rewrite inits_core_eq theorem inits_tail (s : stream A) : inits (tail s) = inits_core [head (tail s)] (tail (tail s)) := rfl theorem cons_nth_inits_core : ∀ (a : A) (n : nat) (l : list A) (s : stream A), a :: nth n (inits_core l s) = nth n (inits_core (a::l) s) := begin intro a n, induction n with n' ih, {intros, reflexivity}, {intro l s, rewrite [*nth_succ, inits_core_eq, +tail_cons, ih, inits_core_eq (a::l) s] } end theorem nth_inits : ∀ (n : nat) (s : stream A), nth n (inits s) = approx (succ n) s := begin intro n, induction n with n' ih, {intros, reflexivity}, {intros, rewrite [nth_succ, approx_succ, -ih, tail_inits, inits_tail, cons_nth_inits_core]} end theorem inits_eq (s : stream A) : inits s = [head s] :: map (list.cons (head s)) (inits (tail s)) := begin apply stream.ext, intro n, cases n, {reflexivity}, {rewrite [nth_inits, nth_succ, tail_cons, nth_map, nth_inits]} end theorem zip_inits_tails (s : stream A) : zip append (inits s) (tails s) = const s := begin apply stream.ext, intro n, rewrite [nth_zip, nth_inits, nth_tails, nth_const, approx_succ, cons_append, append_approx_drop, stream.eta] end definition pure (a : A) : stream A := const a definition apply (f : stream (A → B)) (s : stream A) : stream B := λ n, (nth n f) (nth n s) infix `⊛`:75 := apply -- input as \o* theorem identity (s : stream A) : pure id ⊛ s = s := rfl theorem composition (g : stream (B → C)) (f : stream (A → B)) (s : stream A) : pure compose ⊛ g ⊛ f ⊛ s = g ⊛ (f ⊛ s) := rfl theorem homomorphism (f : A → B) (a : A) : pure f ⊛ pure a = pure (f a) := rfl theorem interchange (fs : stream (A → B)) (a : A) : fs ⊛ pure a = pure (λ f, f a) ⊛ fs := rfl theorem map_eq_apply (f : A → B) (s : stream A) : map f s = pure f ⊛ s := rfl definition nats : stream nat := λ n, n theorem nth_nats (n : nat) : nth n nats = n := rfl theorem nats_eq : nats = 0 :: map succ nats := begin apply stream.ext, intro n, cases n, reflexivity, rewrite [nth_succ] end section open equiv lemma stream_equiv_of_equiv {A B : Type} : A ≃ B → stream A ≃ stream B | (mk f g l r) := mk (map f) (map g) begin intros, rewrite [map_map, id_of_left_inverse l, map_id] end begin intros, rewrite [map_map, id_of_right_inverse r, map_id] end end definition lex (rel : A → A → Prop) (s₁ s₂ : stream A) : Prop := ∃ i, rel (nth i s₁) (nth i s₂) ∧ ∀ j, j < i → nth j s₁ = nth j s₂ definition lex.trans {s₁ s₂ s₃} {rel : A → A → Prop} : transitive rel → lex rel s₁ s₂ → lex rel s₂ s₃ → lex rel s₁ s₃ := assume htrans h₁ h₂, obtain (i₁ : nat) hlt₁ he₁, from h₁, obtain (i₂ : nat) hlt₂ he₂, from h₂, lt.by_cases (λ i₁lti₂ : i₁ < i₂, assert aux : nth i₁ s₂ = nth i₁ s₃, from he₂ _ i₁lti₂, begin existsi i₁, split, {rewrite -aux, exact hlt₁}, {intro j jlti₁, transitivity nth j s₂, exact !he₁ jlti₁, exact !he₂ (lt.trans jlti₁ i₁lti₂)} end) (λ i₁eqi₂ : i₁ = i₂, begin subst i₂, existsi i₁, split, exact htrans hlt₁ hlt₂, intro j jlti₁, transitivity nth j s₂, exact !he₁ jlti₁; exact !he₂ jlti₁ end) (λ i₂lti₁ : i₂ < i₁, assert nth i₂ s₁ = nth i₂ s₂, from he₁ _ i₂lti₁, begin existsi i₂, split, {rewrite this, exact hlt₂}, {intro j jlti₂, transitivity nth j s₂, exact !he₁ (lt.trans jlti₂ i₂lti₁), exact !he₂ jlti₂} end) end stream
6a060d00ef25183ec24cf842d6dd06844a8e84a8
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/number_theory/bernoulli.lean
c9be0f82df9792e0e88f700329f494d5ec56ca7b
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,968
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kevin Buzzard -/ import data.rat import data.fintype.card import algebra.big_operators.nat_antidiagonal import ring_theory.power_series.well_known /-! # Bernoulli numbers The Bernoulli numbers are a sequence of rational numbers that frequently show up in number theory. ## Mathematical overview The Bernoulli numbers $(B_0, B_1, B_2, \ldots)=(1, -1/2, 1/6, 0, -1/30, \ldots)$ are a sequence of rational numbers. They show up in the formula for the sums of $k$th powers. They are related to the Taylor series expansions of $x/\tan(x)$ and of $\coth(x)$, and also show up in the values that the Riemann Zeta function takes both at both negative and positive integers (and hence in the theory of modular forms). For example, if $1 \leq n$ is even then $$\zeta(2n)=\sum_{t\geq1}t^{-2n}=(-1)^{n+1}\frac{(2\pi)^{2n}B_{2n}}{2(2n)!}.$$ Note however that this result is not yet formalised in Lean. The Bernoulli numbers can be formally defined using the power series $$\sum B_n\frac{t^n}{n!}=\frac{t}{1-e^{-t}}$$ although that happens to not be the definition in mathlib (this is an *implementation detail* and need not concern the mathematician). Note that $B_1=-1/2$, meaning that we are using the $B_n^-$ of [from Wikipedia](https://en.wikipedia.org/wiki/Bernoulli_number). ## Implementation detail The Bernoulli numbers are defined using well-founded induction, by the formula $$B_n=1-\sum_{k\lt n}\frac{\binom{n}{k}}{n-k+1}B_k.$$ This formula is true for all $n$ and in particular $B_0=1$. Note that this is the definition for positive Bernoulli numbers, which we call `bernoulli'`. The negative Bernoulli numbers are then defined as `bernoulli := (-1)^n * bernoulli'`. ## Main theorems `sum_bernoulli : ∑ k in finset.range n, (n.choose k : ℚ) * bernoulli k = 0` -/ open_locale nat big_operators open finset nat finset.nat power_series variables (A : Type*) [comm_ring A] [algebra ℚ A] /-! ### Definitions -/ /-- The Bernoulli numbers: the $n$-th Bernoulli number $B_n$ is defined recursively via $$B_n = 1 - \sum_{k < n} \binom{n}{k}\frac{B_k}{n+1-k}$$ -/ def bernoulli' : ℕ → ℚ := well_founded.fix lt_wf $ λ n bernoulli', 1 - ∑ k : fin n, n.choose k / (n - k + 1) * bernoulli' k k.2 lemma bernoulli'_def' (n : ℕ) : bernoulli' n = 1 - ∑ k : fin n, n.choose k / (n - k + 1) * bernoulli' k := well_founded.fix_eq _ _ _ lemma bernoulli'_def (n : ℕ) : bernoulli' n = 1 - ∑ k in range n, n.choose k / (n - k + 1) * bernoulli' k := by { rw [bernoulli'_def', ← fin.sum_univ_eq_sum_range], refl } lemma bernoulli'_spec (n : ℕ) : ∑ k in range n.succ, (n.choose (n - k) : ℚ) / (n - k + 1) * bernoulli' k = 1 := begin rw [sum_range_succ_comm, bernoulli'_def n, nat.sub_self], conv in (n.choose (_ - _)) { rw choose_symm (mem_range.1 H).le }, simp only [one_mul, cast_one, sub_self, sub_add_cancel, choose_zero_right, zero_add, div_one], end lemma bernoulli'_spec' (n : ℕ) : ∑ k in antidiagonal n, ((k.1 + k.2).choose k.2 : ℚ) / (k.2 + 1) * bernoulli' k.1 = 1 := begin refine ((sum_antidiagonal_eq_sum_range_succ_mk _ n).trans _).trans (bernoulli'_spec n), refine sum_congr rfl (λ x hx, _), simp only [nat.add_sub_cancel', mem_range_succ_iff.mp hx, cast_sub], end /-! ### Examples -/ section examples @[simp] lemma bernoulli'_zero : bernoulli' 0 = 1 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_one : bernoulli' 1 = 1/2 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_two : bernoulli' 2 = 1/6 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_three : bernoulli' 3 = 0 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_four : bernoulli' 4 = -1/30 := have nat.choose 4 2 = 6 := dec_trivial, -- shrug by { rw bernoulli'_def, norm_num [sum_range_succ, this] } end examples @[simp] lemma sum_bernoulli' (n : ℕ) : ∑ k in range n, (n.choose k : ℚ) * bernoulli' k = n := begin cases n, { simp }, suffices : (n + 1 : ℚ) * ∑ k in range n, ↑(n.choose k) / (n - k + 1) * bernoulli' k = ∑ x in range n, ↑(n.succ.choose x) * bernoulli' x, { rw_mod_cast [sum_range_succ, bernoulli'_def, ← this, choose_succ_self_right], ring }, simp_rw [mul_sum, ← mul_assoc], refine sum_congr rfl (λ k hk, _), congr', have : ((n - k : ℕ) : ℚ) + 1 ≠ 0 := by apply_mod_cast succ_ne_zero, field_simp [← cast_sub (mem_range.1 hk).le, mul_comm], rw_mod_cast [nat.sub_add_eq_add_sub (mem_range.1 hk).le, choose_mul_succ_eq], end /-- The exponential generating function for the Bernoulli numbers `bernoulli' n`. -/ def bernoulli'_power_series := mk $ λ n, algebra_map ℚ A (bernoulli' n / n!) theorem bernoulli'_power_series_mul_exp_sub_one : bernoulli'_power_series A * (exp A - 1) = X * exp A := begin ext n, -- constant coefficient is a special case cases n, { simp }, rw [bernoulli'_power_series, coeff_mul, mul_comm X, sum_antidiagonal_succ'], suffices : ∑ p in antidiagonal n, (bernoulli' p.1 / p.1!) * ((p.2 + 1) * p.2!)⁻¹ = n!⁻¹, { simpa [ring_hom.map_sum] using congr_arg (algebra_map ℚ A) this }, apply eq_inv_of_mul_left_eq_one, rw sum_mul, convert bernoulli'_spec' n using 1, apply sum_congr rfl, simp_rw [mem_antidiagonal], rintro ⟨i, j⟩ rfl, have : (j + 1 : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero j, have : (j + 1 : ℚ) * j! * i! ≠ 0 := by simpa [factorial_ne_zero], have := factorial_mul_factorial_dvd_factorial_add i j, field_simp [mul_comm _ (bernoulli' i), mul_assoc, add_choose], rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc], rw [cast_mul, cast_mul, mul_div_mul_right, cast_dvd_char_zero, cast_mul], assumption', end /-- Odd Bernoulli numbers (greater than 1) are zero. -/ theorem bernoulli'_odd_eq_zero {n : ℕ} (h_odd : odd n) (hlt : 1 < n) : bernoulli' n = 0 := begin let B := mk (λ n, bernoulli' n / n!), suffices : (B - eval_neg_hom B) * (exp ℚ - 1) = X * (exp ℚ - 1), { cases mul_eq_mul_right_iff.mp this; simp only [power_series.ext_iff, eval_neg_hom, coeff_X] at h, { apply eq_zero_of_neg_eq, specialize h n, split_ifs at h; simp [neg_one_pow_of_odd h_odd, factorial_ne_zero, *] at * }, { simpa using h 1 } }, have h : B * (exp ℚ - 1) = X * exp ℚ, { simpa [bernoulli'_power_series] using bernoulli'_power_series_mul_exp_sub_one ℚ }, rw [sub_mul, h, mul_sub X, sub_right_inj, ← neg_sub, ← neg_mul_eq_mul_neg, neg_eq_iff_neg_eq], suffices : eval_neg_hom (B * (exp ℚ - 1)) * exp ℚ = eval_neg_hom (X * exp ℚ) * exp ℚ, { simpa [mul_assoc, sub_mul, mul_comm (eval_neg_hom (exp ℚ)), exp_mul_exp_neg_eq_one, eq_comm] }, congr', end /-- The Bernoulli numbers are defined to be `bernoulli'` with a parity sign. -/ def bernoulli (n : ℕ) : ℚ := (-1)^n * bernoulli' n lemma bernoulli'_eq_bernoulli (n : ℕ) : bernoulli' n = (-1)^n * bernoulli n := by simp [bernoulli, ← mul_assoc, ← sq, ← pow_mul, mul_comm n 2, pow_mul] @[simp] lemma bernoulli_zero : bernoulli 0 = 1 := by simp [bernoulli] @[simp] lemma bernoulli_one : bernoulli 1 = -1/2 := by norm_num [bernoulli] theorem bernoulli_eq_bernoulli'_of_ne_one {n : ℕ} (hn : n ≠ 1) : bernoulli n = bernoulli' n := begin by_cases h0 : n = 0, { simp [h0] }, rw [bernoulli, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one n, { simp [h] }, simp [bernoulli'_odd_eq_zero (odd_iff.mpr h) (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, hn⟩)], end @[simp] theorem sum_bernoulli (n : ℕ): ∑ k in range n, (n.choose k : ℚ) * bernoulli k = if n = 1 then 1 else 0 := begin cases n, { simp }, cases n, { simp }, suffices : ∑ i in range n, ↑((n + 2).choose (i + 2)) * bernoulli (i + 2) = n / 2, { simp only [this, sum_range_succ', cast_succ, bernoulli_one, bernoulli_zero, choose_one_right, mul_one, choose_zero_right, cast_zero, if_false, zero_add, succ_succ_ne_one], ring }, have f := sum_bernoulli' n.succ.succ, simp_rw [sum_range_succ', bernoulli'_one, choose_one_right, cast_succ, ← eq_sub_iff_add_eq] at f, convert f, { ext x, rw bernoulli_eq_bernoulli'_of_ne_one (succ_ne_zero x ∘ succ.inj) }, { simp only [one_div, mul_one, bernoulli'_zero, cast_one, choose_zero_right, add_sub_cancel], ring }, end lemma bernoulli_spec' (n : ℕ) : ∑ k in antidiagonal n, ((k.1 + k.2).choose k.2 : ℚ) / (k.2 + 1) * bernoulli k.1 = if n = 0 then 1 else 0 := begin cases n, { simp }, rw if_neg (succ_ne_zero _), -- algebra facts have h₁ : (1, n) ∈ antidiagonal n.succ := by simp [mem_antidiagonal, add_comm], have h₂ : (n : ℚ) + 1 ≠ 0 := by apply_mod_cast succ_ne_zero, have h₃ : (1 + n).choose n = n + 1 := by simp [add_comm], -- key equation: the corresponding fact for `bernoulli'` have H := bernoulli'_spec' n.succ, -- massage it to match the structure of the goal, then convert piece by piece rw sum_eq_add_sum_diff_singleton h₁ at H ⊢, apply add_eq_of_eq_sub', convert eq_sub_of_add_eq' H using 1, { refine sum_congr rfl (λ p h, _), obtain ⟨h', h''⟩ : p ∈ _ ∧ p ≠ _ := by rwa [mem_sdiff, mem_singleton] at h, simp [bernoulli_eq_bernoulli'_of_ne_one ((not_congr (antidiagonal_congr h' h₁)).mp h'')] }, { field_simp [h₃], norm_num }, end /-- The exponential generating function for the Bernoulli numbers `bernoulli n`. -/ def bernoulli_power_series := mk $ λ n, algebra_map ℚ A (bernoulli n / n!) theorem bernoulli_power_series_mul_exp_sub_one : bernoulli_power_series A * (exp A - 1) = X := begin ext n, -- constant coefficient is a special case cases n, { simp }, simp only [bernoulli_power_series, coeff_mul, coeff_X, sum_antidiagonal_succ', one_div, coeff_mk, coeff_one, coeff_exp, linear_map.map_sub, factorial, if_pos, cast_succ, cast_one, cast_mul, sub_zero, ring_hom.map_one, add_eq_zero_iff, if_false, inv_one, zero_add, one_ne_zero, mul_zero, and_false, sub_self, ← ring_hom.map_mul, ← ring_hom.map_sum], suffices : ∑ x in antidiagonal n, bernoulli x.1 / x.1! * ((x.2 + 1) * x.2!)⁻¹ = if n.succ = 1 then 1 else 0, { split_ifs; simp [h, this] }, cases n, { simp }, have hfact : ∀ m, (m! : ℚ) ≠ 0 := λ m, by exact_mod_cast factorial_ne_zero m, have hite1 : ite (n.succ.succ = 1) 1 0 = (0 / n.succ! : ℚ) := by simp, have hite2 : ite (n.succ = 0) 1 0 = (0 : ℚ) := by simp [succ_ne_zero], rw [hite1, eq_div_iff (hfact n.succ), ← hite2, ← bernoulli_spec', sum_mul], apply sum_congr rfl, rintro ⟨i, j⟩ h, rw mem_antidiagonal at h, have hj : (j.succ : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero j, field_simp [← h, mul_ne_zero hj (hfact j), hfact i, mul_comm _ (bernoulli i), mul_assoc], rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc], rw [cast_mul, cast_mul, mul_div_mul_right _ _ hj, add_choose, cast_dvd_char_zero], apply factorial_mul_factorial_dvd_factorial_add, end section faulhaber /-- **Faulhaber's theorem** relating the **sum of of p-th powers** to the Bernoulli numbers: $$\sum_{k=0}^{n-1} k^p = \sum_{i=0}^p B_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ See https://proofwiki.org/wiki/Faulhaber%27s_Formula and [orosi2018faulhaber] for the proof provided here. -/ theorem sum_range_pow (n p : ℕ) : ∑ k in range n, (k : ℚ) ^ p = ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin have hne : ∀ m : ℕ, (m! : ℚ) ≠ 0 := λ m, by exact_mod_cast factorial_ne_zero m, -- compute the Cauchy product of two power series have h_cauchy : mk (λ p, bernoulli p / p!) * mk (λ q, coeff ℚ (q + 1) (exp ℚ ^ n)) = mk (λ p, ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { ext q, let f := λ a b, bernoulli a / a! * coeff ℚ (b + 1) (exp ℚ ^ n), -- key step: use `power_series.coeff_mul` and then rewrite sums simp only [coeff_mul, coeff_mk, cast_mul, sum_antidiagonal_eq_sum_range_succ f], apply sum_congr rfl, simp_intros m h only [finset.mem_range], simp only [f, exp_pow_eq_rescale_exp, rescale, one_div, coeff_mk, ring_hom.coe_mk, coeff_exp, ring_hom.id_apply, cast_mul, rat.algebra_map_rat_rat], -- manipulate factorials and binomial coefficients rw [choose_eq_factorial_div_factorial h.le, eq_comm, div_eq_iff (hne q.succ), succ_eq_add_one, mul_assoc _ _ ↑q.succ!, mul_comm _ ↑q.succ!, ← mul_assoc, div_mul_eq_mul_div, mul_comm (↑n ^ (q - m + 1)), ← mul_assoc _ _ (↑n ^ (q - m + 1)), ← one_div, mul_one_div, div_div_eq_div_mul, ← nat.sub_add_comm (le_of_lt_succ h), cast_dvd, cast_mul], { ring }, { exact factorial_mul_factorial_dvd_factorial h.le }, { simp [hne] } }, -- same as our goal except we pull out `p!` for convenience have hps : ∑ k in range n, ↑k ^ p = (∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!) * p!, { suffices : mk (λ p, ∑ k in range n, ↑k ^ p * algebra_map ℚ ℚ p!⁻¹) = mk (λ p, ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { rw [← div_eq_iff (hne p), div_eq_mul_inv, sum_mul], rw power_series.ext_iff at this, simpa using this p }, -- the power series `exp ℚ - 1` is non-zero, a fact we need in order to use `mul_right_inj'` have hexp : exp ℚ - 1 ≠ 0, { simp only [exp, power_series.ext_iff, ne, not_forall], use 1, simp }, have h_r : exp ℚ ^ n - 1 = X * mk (λ p, coeff ℚ (p + 1) (exp ℚ ^ n)), { have h_const : C ℚ (constant_coeff ℚ (exp ℚ ^ n)) = 1 := by simp, rw [← h_const, sub_const_eq_X_mul_shift] }, -- key step: a chain of equalities of power series rw [← mul_right_inj' hexp, mul_comm, ← exp_pow_sum, ← geom_sum_def, geom_sum_mul, h_r, ← bernoulli_power_series_mul_exp_sub_one, bernoulli_power_series, mul_right_comm], simp [h_cauchy, mul_comm] }, -- massage `hps` into our goal rw [hps, sum_mul], refine sum_congr rfl (λ x hx, _), field_simp [mul_right_comm _ ↑p!, ← mul_assoc _ _ ↑p!, cast_add_one_ne_zero, hne], end /-- Alternate form of **Faulhaber's theorem**, relating the sum of p-th powers to the Bernoulli numbers: $$\sum_{k=1}^{n} k^p = \sum_{i=0}^p (-1)^iB_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ Deduced from `sum_range_pow`. -/ theorem sum_Ico_pow (n p : ℕ) : ∑ k in Ico 1 (n + 1), (k : ℚ) ^ p = ∑ i in range (p + 1), bernoulli' i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin -- dispose of the trivial case cases p, { simp }, let f := λ i, bernoulli i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, let f' := λ i, bernoulli' i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, suffices : ∑ k in Ico 1 n.succ, ↑k ^ p.succ = ∑ i in range p.succ.succ, f' i, { convert this }, -- prove some algebraic facts that will make things easier for us later on have hle := nat.le_add_left 1 n, have hne : (p + 1 + 1 : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero p.succ, have h1 : ∀ r : ℚ, r * (p + 1 + 1) * n ^ p.succ / (p + 1 + 1 : ℚ) = r * n ^ p.succ := λ r, by rw [mul_div_right_comm, mul_div_cancel _ hne], have h2 : f 1 + n ^ p.succ = 1 / 2 * n ^ p.succ, { simp_rw [f, bernoulli_one, choose_one_right, succ_sub_succ_eq_sub, cast_succ, nat.sub_zero, h1], ring }, have : ∑ i in range p, bernoulli (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) = ∑ i in range p, bernoulli' (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) := sum_congr rfl (λ i h, by rw bernoulli_eq_bernoulli'_of_ne_one (succ_succ_ne_one i)), calc ∑ k in Ico 1 n.succ, ↑k ^ p.succ -- replace sum over `Ico` with sum over `range` and simplify = ∑ k in range n.succ, ↑k ^ p.succ : by simp [sum_Ico_eq_sub _ hle, succ_ne_zero] -- extract the last term of the sum ... = ∑ k in range n, (k : ℚ) ^ p.succ + n ^ p.succ : by rw sum_range_succ -- apply the key lemma, `sum_range_pow` ... = ∑ i in range p.succ.succ, f i + n ^ p.succ : by simp [f, sum_range_pow] -- extract the first two terms of the sum ... = ∑ i in range p, f i.succ.succ + f 1 + f 0 + n ^ p.succ : by simp_rw [sum_range_succ'] ... = ∑ i in range p, f i.succ.succ + (f 1 + n ^ p.succ) + f 0 : by ring ... = ∑ i in range p, f i.succ.succ + 1 / 2 * n ^ p.succ + f 0 : by rw h2 -- convert from `bernoulli` to `bernoulli'` ... = ∑ i in range p, f' i.succ.succ + f' 1 + f' 0 : by { simp only [f, f'], simpa [h1] } -- rejoin the first two terms of the sum ... = ∑ i in range p.succ.succ, f' i : by simp_rw [sum_range_succ'], end end faulhaber
1bf801d853ad44dd33512f175325d15e286b1192
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/714.lean
51d035b5d0e38b4752e63276c90fc5cc612c10e0
[ "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
53
lean
def test {α : Type} [LT α] := List α #check test
e6cf2d2f238c81c88d267568a20fd54d26839116
618003631150032a5676f229d13a079ac875ff77
/src/data/sum.lean
4d55582b13240cd5ddfa5c5cb0366afc8b85f2d0
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,064
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury G. Kudryashov -/ import tactic.lint /-! # More theorems about the sum type -/ universes u v w x variables {α : Type u} {α' : Type w} {β : Type v} {β' : Type x} open sum attribute [derive decidable_eq] sum @[simp] theorem sum.forall {p : α ⊕ β → Prop} : (∀ x, p x) ↔ (∀ a, p (inl a)) ∧ (∀ b, p (inr b)) := ⟨λ h, ⟨λ a, h _, λ b, h _⟩, λ ⟨h₁, h₂⟩, sum.rec h₁ h₂⟩ @[simp] theorem sum.exists {p : α ⊕ β → Prop} : (∃ x, p x) ↔ (∃ a, p (inl a)) ∨ ∃ b, p (inr b) := ⟨λ h, match h with | ⟨inl a, h⟩ := or.inl ⟨a, h⟩ | ⟨inr b, h⟩ := or.inr ⟨b, h⟩ end, λ h, match h with | or.inl ⟨a, h⟩ := ⟨inl a, h⟩ | or.inr ⟨b, h⟩ := ⟨inr b, h⟩ end⟩ namespace sum /-- Map `α ⊕ β` to `α' ⊕ β'` sending `α` to `α'` and `β` to `β'`. -/ protected def map (f : α → α') (g : β → β') : α ⊕ β → α' ⊕ β' | (sum.inl x) := sum.inl (f x) | (sum.inr x) := sum.inr (g x) @[simp] lemma map_inl (f : α → α') (g : β → β') (x : α) : (inl x).map f g = inl (f x) := rfl @[simp] lemma map_inr (f : α → α') (g : β → β') (x : β) : (inr x).map f g = inr (g x) := rfl @[simp] lemma map_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') : ∀ x : α ⊕ β, (x.map f g).map f' g' = x.map (f' ∘ f) (g' ∘ g) | (inl a) := rfl | (inr b) := rfl @[simp] lemma map_comp_map {α'' β''} (f' : α' → α'') (g' : β' → β'') (f : α → α') (g : β → β') : (sum.map f' g') ∘ (sum.map f g) = sum.map (f' ∘ f) (g' ∘ g) := funext $ map_map f' g' f g @[simp] lemma map_id_id (α β) : sum.map (@id α) (@id β) = id := funext $ λ x, sum.rec_on x (λ _, rfl) (λ _, rfl) theorem inl.inj_iff {a b} : (inl a : α ⊕ β) = inl b ↔ a = b := ⟨inl.inj, congr_arg _⟩ theorem inr.inj_iff {a b} : (inr a : α ⊕ β) = inr b ↔ a = b := ⟨inr.inj, congr_arg _⟩ theorem inl_ne_inr {a : α} {b : β} : inl a ≠ inr b. theorem inr_ne_inl {a : α} {b : β} : inr b ≠ inl a. /-- Define a function on `α ⊕ β` by giving separate definitions on `α` and `β`. -/ protected def elim {α β γ : Sort*} (f : α → γ) (g : β → γ) : α ⊕ β → γ := λ x, sum.rec_on x f g @[simp] lemma elim_inl {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : α) : sum.elim f g (inl x) = f x := rfl @[simp] lemma elim_inr {α β γ : Sort*} (f : α → γ) (g : β → γ) (x : β) : sum.elim f g (inr x) = g x := rfl lemma elim_injective {α β γ : Sort*} {f : α → γ} {g : β → γ} (hf : function.injective f) (hg : function.injective g) (hfg : ∀ a b, f a ≠ g b) : function.injective (sum.elim f g) := λ x y, sum.rec_on x (sum.rec_on y (λ x y hxy, by rw hf hxy) (λ x y hxy, false.elim $ hfg _ _ hxy)) (sum.rec_on y (λ x y hxy, false.elim $ hfg x y hxy.symm) (λ x y hxy, by rw hg hxy)) section variables (ra : α → α → Prop) (rb : β → β → Prop) /-- Lexicographic order for sum. Sort all the `inl a` before the `inr b`, otherwise use the respective order on `α` or `β`. -/ inductive lex : α ⊕ β → α ⊕ β → Prop | inl {a₁ a₂} (h : ra a₁ a₂) : lex (inl a₁) (inl a₂) | inr {b₁ b₂} (h : rb b₁ b₂) : lex (inr b₁) (inr b₂) | sep (a b) : lex (inl a) (inr b) variables {ra rb} @[simp] theorem lex_inl_inl {a₁ a₂} : lex ra rb (inl a₁) (inl a₂) ↔ ra a₁ a₂ := ⟨λ h, by cases h; assumption, lex.inl⟩ @[simp] theorem lex_inr_inr {b₁ b₂} : lex ra rb (inr b₁) (inr b₂) ↔ rb b₁ b₂ := ⟨λ h, by cases h; assumption, lex.inr⟩ @[simp] theorem lex_inr_inl {b a} : ¬ lex ra rb (inr b) (inl a) := λ h, by cases h attribute [simp] lex.sep theorem lex_acc_inl {a} (aca : acc ra a) : acc (lex ra rb) (inl a) := begin induction aca with a H IH, constructor, intros y h, cases h with a' _ h', exact IH _ h' end theorem lex_acc_inr (aca : ∀ a, acc (lex ra rb) (inl a)) {b} (acb : acc rb b) : acc (lex ra rb) (inr b) := begin induction acb with b H IH, constructor, intros y h, cases h with _ _ _ b' _ h' a, { exact IH _ h' }, { exact aca _ } end theorem lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := have aca : ∀ a, acc (lex ra rb) (inl a), from λ a, lex_acc_inl (ha.apply a), ⟨λ x, sum.rec_on x aca (λ b, lex_acc_inr aca (hb.apply b))⟩ end /-- Swap the factors of a sum type -/ @[simp] def swap : α ⊕ β → β ⊕ α | (inl a) := inr a | (inr b) := inl b @[simp] lemma swap_swap (x : α ⊕ β) : swap (swap x) = x := by cases x; refl @[simp] lemma swap_swap_eq : swap ∘ swap = @id (α ⊕ β) := funext $ swap_swap @[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap := swap_swap @[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap := swap_swap end sum
ed46a12cae7cac919029e6f582266077aa4f9689
1dd482be3f611941db7801003235dc84147ec60a
/src/topology/bounded_continuous_function.lean
ee27818811b9a9e5bac6da82522fbd034acfff44
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
23,576
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, Mario Carneiro Type of bounded continuous functions taking values in a metric space, with the uniform distance. -/ import analysis.normed_space.basic data.real.cau_seq_filter topology.metric_space.lipschitz noncomputable theory local attribute [instance] classical.decidable_inhabited classical.prop_decidable open set lattice filter metric universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- A locally uniform limit of continuous functions is continuous -/ lemma continuous_of_locally_uniform_limit_of_continuous [topological_space α] [metric_space β] {F : ℕ → α → β} {f : α → β} (L : ∀x:α, ∃s ∈ (nhds x).sets, ∀ε>(0:ℝ), ∃n, ∀y∈s, dist (F n y) (f y) ≤ ε) (C : ∀ n, continuous (F n)) : continuous f := continuous_iff'.2 $ λ x ε ε0, begin rcases L x with ⟨r, rx, hr⟩, rcases hr (ε/2/2) (half_pos $ half_pos ε0) with ⟨n, hn⟩, rcases continuous_iff'.1 (C n) x (ε/2) (half_pos ε0) with ⟨s, sx, hs⟩, refine ⟨_, (nhds x).inter_sets rx sx, _⟩, rintro y ⟨yr, ys⟩, calc dist (f y) (f x) ≤ dist (F n y) (F n x) + (dist (F n y) (f y) + dist (F n x) (f x)) : dist_triangle4_left _ _ _ _ ... < ε/2 + (ε/2/2 + ε/2/2) : add_lt_add_of_lt_of_le (hs _ ys) (add_le_add (hn _ yr) (hn _ (mem_of_nhds rx))) ... = ε : by rw [add_halves, add_halves] end /-- A uniform limit of continuous functions is continuous -/ lemma continuous_of_uniform_limit_of_continuous [topological_space α] {β : Type v} [metric_space β] {F : ℕ → α → β} {f : α → β} (L : ∀ε>(0:ℝ), ∃N, ∀y, dist (F N y) (f y) ≤ ε) : (∀ n, continuous (F n)) → continuous f := continuous_of_locally_uniform_limit_of_continuous $ λx, ⟨univ, by simpa [filter.univ_mem_sets] using L⟩ /-- The type of bounded continuous functions from a topological space to a metric space -/ def bounded_continuous_function (α : Type u) (β : Type v) [topological_space α] [metric_space β] : Type (max u v) := {f : α → β // continuous f ∧ ∃C, ∀x y:α, dist (f x) (f y) ≤ C} local infixr ` →ᵇ `:25 := bounded_continuous_function namespace bounded_continuous_function section basics variables [topological_space α] [metric_space β] [metric_space γ] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_coe_to_fun (α →ᵇ β) := ⟨_, subtype.val⟩ lemma bounded_range : bounded (range f) := bounded_range_iff.2 f.2.2 /-- If a function is continuous on a compact space, it is automatically bounded, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_compact [compact_space α] (f : α → β) (hf : continuous f) : α →ᵇ β := ⟨f, hf, bounded_range_iff.1 $ by rw ← image_univ; exact bounded_of_compact (compact_image compact_univ hf)⟩ /-- If a function is bounded on a discrete space, it is automatically continuous, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_discrete [discrete_topology α] (f : α → β) (hf : ∃C, ∀x y, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨f, continuous_of_discrete_topology, hf⟩ /-- The uniform distance between two bounded continuous functions -/ instance : has_dist (α →ᵇ β) := ⟨λf g, Inf {C | C ≥ 0 ∧ ∀ x : α, dist (f x) (g x) ≤ C}⟩ lemma dist_eq : dist f g = Inf {C | C ≥ 0 ∧ ∀ x : α, dist (f x) (g x) ≤ C} := rfl lemma dist_set_exists : ∃ C, C ≥ 0 ∧ ∀ x : α, dist (f x) (g x) ≤ C := begin refine if h : nonempty α then _ else ⟨0, le_refl _, λ x, h.elim ⟨x⟩⟩, cases h with x, rcases f.2 with ⟨_, Cf, hCf⟩, /- hCf : ∀ (x y : α), dist (f.val x) (f.val y) ≤ Cf -/ rcases g.2 with ⟨_, Cg, hCg⟩, /- hCg : ∀ (x y : α), dist (g.val x) (g.val y) ≤ Cg -/ let C := max 0 (dist (f x) (g x) + (Cf + Cg)), exact ⟨C, le_max_left _ _, λ y, calc dist (f y) (g y) ≤ dist (f x) (g x) + (dist (f x) (f y) + dist (g x) (g y)) : dist_triangle4_left _ _ _ _ ... ≤ dist (f x) (g x) + (Cf + Cg) : add_le_add_left (add_le_add (hCf _ _) (hCg _ _)) _ ... ≤ C : le_max_right _ _⟩ end /-- The pointwise distance is controlled by the distance between functions, by definition -/ lemma dist_coe_le_dist (x : α) : dist (f x) (g x) ≤ dist f g := le_cInf (ne_empty_iff_exists_mem.2 dist_set_exists) $ λb hb, hb.2 x @[extensionality] lemma ext (H : ∀x, f x = g x) : f = g := subtype.eq $ by ext; apply H /- This lemma will be needed in the proof of the metric space instance, but it will become useless afterwards as it will be superceded by the general result that the distance is nonnegative is metric spaces. -/ private lemma dist_nonneg' : 0 ≤ dist f g := le_cInf (ne_empty_iff_exists_mem.2 dist_set_exists) (λ C, and.left) /-- The distance between two functions is controlled by the supremum of the pointwise distances -/ lemma dist_le (C0 : (0 : ℝ) ≤ C) : dist f g ≤ C ↔ ∀x:α, dist (f x) (g x) ≤ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ H, cInf_le ⟨0, λ C, and.left⟩ ⟨C0, H⟩⟩ /-- On an empty space, bounded continuous functions are at distance 0 -/ lemma dist_zero_of_empty (e : ¬ nonempty α) : dist f g = 0 := le_antisymm ((dist_le (le_refl _)).2 $ λ x, e.elim ⟨x⟩) dist_nonneg' /-- The type of bounded continuous functions, with the uniform distance, is a metric space. -/ instance : metric_space (α →ᵇ β) := { dist_self := λ f, le_antisymm ((dist_le (le_refl _)).2 $ λ x, by simp) dist_nonneg', eq_of_dist_eq_zero := λ f g hfg, by ext x; exact eq_of_dist_eq_zero (le_antisymm (hfg ▸ dist_coe_le_dist _) dist_nonneg), dist_comm := λ f g, by simp [dist_eq, dist_comm], dist_triangle := λ f g h, (dist_le (add_nonneg dist_nonneg' dist_nonneg')).2 $ λ x, le_trans (dist_triangle _ _ _) (add_le_add (dist_coe_le_dist _) (dist_coe_le_dist _)) } def const (b : β) : α →ᵇ β := ⟨λx, b, continuous_const, 0, by simp [le_refl]⟩ /-- If the target space is inhabited, so is the space of bounded continuous functions -/ instance [inhabited β] : inhabited (α →ᵇ β) := ⟨const (default β)⟩ /-- The evaluation map is continuous, as a joint function of `u` and `x` -/ theorem continuous_eval : continuous (λ p : (α →ᵇ β) × α, p.1 p.2) := continuous_iff'.2 $ λ ⟨f, x⟩ ε ε0, /- use the continuity of `f` to find a neighborhood of `x` where it varies at most by ε/2 -/ let ⟨s, sx, Hs⟩ := continuous_iff'.1 f.2.1 x (ε/2) (half_pos ε0) in /- s : set α, sx : s ∈ (nhds x).sets, Hs : ∀ (b : α), b ∈ s → dist (f.val b) (f.val x) < ε / 2 -/ ⟨set.prod (ball f (ε/2)) s, prod_mem_nhds_sets (ball_mem_nhds _ (half_pos ε0)) sx, λ ⟨g, y⟩ ⟨hg, hy⟩, calc dist (g y) (f x) ≤ dist (g y) (f y) + dist (f y) (f x) : dist_triangle _ _ _ ... < ε/2 + ε/2 : add_lt_add (lt_of_le_of_lt (dist_coe_le_dist _) hg) (Hs _ hy) ... = ε : add_halves _⟩ /-- In particular, when `x` is fixed, `f → f x` is continuous -/ theorem continuous_evalx {x : α} : continuous (λ f : α →ᵇ β, f x) := (continuous_id.prod_mk continuous_const).comp continuous_eval /-- When `f` is fixed, `x → f x` is also continuous, by definition -/ theorem continuous_evalf {f : α →ᵇ β} : continuous f := f.2.1 /-- Bounded continuous functions taking values in a complete space form a complete space. -/ instance [complete_space β] : complete_space (α →ᵇ β) := complete_of_cauchy_seq_tendsto $ λ (f : ℕ → α →ᵇ β) (hf : cauchy_seq f), begin /- We have to show that `f n` converges to a bounded continuous function. For this, we prove pointwise convergence to define the limit, then check it is a continuous bounded function, and then check the norm convergence. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, have f_bdd := λx n m N hn hm, le_trans (dist_coe_le_dist x) (b_bound n m N hn hm), have fx_cau : ∀x, cauchy_seq (λn, f n x) := λx, cauchy_seq_iff_le_tendsto_0.2 ⟨b, b0, f_bdd x, b_lim⟩, choose F hF using λx, cauchy_seq_tendsto_of_complete (fx_cau x), /- F : α → β, hF : ∀ (x : α), tendsto (λ (n : ℕ), f n x) at_top (nhds (F x)) `F` is the desired limit function. Check that it is uniformly approximated by `f N` -/ have fF_bdd : ∀x N, dist (f N x) (F x) ≤ b N := λ x N, le_of_tendsto (by simp) (tendsto_dist tendsto_const_nhds (hF x)) (filter.mem_at_top_sets.2 ⟨N, λn hn, f_bdd x N n N (le_refl N) hn⟩), refine ⟨⟨F, _, _⟩, _⟩, { /- Check that `F` is continuous -/ refine continuous_of_uniform_limit_of_continuous (λ ε ε0, _) (λN, (f N).2.1), rcases metric.tendsto_at_top.1 b_lim ε ε0 with ⟨N, hN⟩, exact ⟨N, λy, calc dist (f N y) (F y) ≤ b N : fF_bdd y N ... ≤ dist (b N) 0 : begin simp, show b N ≤ abs(b N), from le_abs_self _ end ... ≤ ε : le_of_lt (hN N (le_refl N))⟩ }, { /- Check that `F` is bounded -/ rcases (f 0).2.2 with ⟨C, hC⟩, exact ⟨C + (b 0 + b 0), λ x y, calc dist (F x) (F y) ≤ dist (f 0 x) (f 0 y) + (dist (f 0 x) (F x) + dist (f 0 y) (F y)) : dist_triangle4_left _ _ _ _ ... ≤ C + (b 0 + b 0) : add_le_add (hC x y) (add_le_add (fF_bdd x 0) (fF_bdd y 0))⟩ }, { /- Check that `F` is close to `f N` in distance terms -/ refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero (λ _, dist_nonneg) _ b_lim), exact λ N, (dist_le (b0 _)).2 (λx, fF_bdd x N) } end /-- Composition (in the target) of a bounded continuous function with a Lipschitz map again gives a bounded continuous function -/ def comp (G : β → γ) (H : ∀x y, dist (G x) (G y) ≤ C * dist x y) (f : α →ᵇ β) : α →ᵇ γ := ⟨λx, G (f x), f.2.1.comp (continuous_of_lipschitz H), let ⟨D, hD⟩ := f.2.2 in ⟨max C 0 * D, λ x y, calc dist (G (f x)) (G (f y)) ≤ C * dist (f x) (f y) : H _ _ ... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left C 0) dist_nonneg ... ≤ max C 0 * D : mul_le_mul_of_nonneg_left (hD _ _) (le_max_right C 0)⟩⟩ /-- The composition operator (in the target) with a Lipschitz map is continuous -/ lemma continuous_comp {G : β → γ} (H : ∀x y, dist (G x) (G y) ≤ C * dist x y) : continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := continuous_of_lipschitz $ λ f g, (dist_le (mul_nonneg (le_max_right C 0) dist_nonneg)).2 $ λ x, calc dist (G (f x)) (G (g x)) ≤ C * dist (f x) (g x) : H _ _ ... ≤ max C 0 * dist (f x) (g x) : mul_le_mul_of_nonneg_right (le_max_left C 0) (dist_nonneg) ... ≤ max C 0 * dist f g : mul_le_mul_of_nonneg_left (dist_coe_le_dist _) (le_max_right C 0) /-- Restriction (in the target) of a bounded continuous function taking values in a subset -/ def cod_restrict (s : set β) (f : α →ᵇ β) (H : ∀x, f x ∈ s) : α →ᵇ s := ⟨λx, ⟨f x, H x⟩, continuous_subtype_mk _ f.2.1, f.2.2⟩ end basics section arzela_ascoli variables [topological_space α] [compact_space α] [metric_space β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} /- Arzela-Ascoli theorem asserts that, on a compact space, a set of functions sharing a common modulus of continuity and taking values in a compact set forms a compact subset for the topology of uniform convergence. In this section, we prove this theorem and several useful variations around it. -/ /-- First version, with pointwise equicontinuity and range in a compact space -/ theorem arzela_ascoli₁ [compact_space β] (A : set (α →ᵇ β)) (closed : is_closed A) (H : ∀ (x:α) (ε > 0), ∃U ∈ (nhds x).sets, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : compact A := begin refine compact_of_totally_bounded_is_closed _ closed, refine totally_bounded_of_finite_discretization (λ ε ε0, _), rcases dense ε0 with ⟨ε₁, ε₁0, εε₁⟩, let ε₂ := ε₁/2/2, /- We have to find a finite discretization of `u`, i.e., finite information that is sufficient to reconstruct `u` up to ε. This information will be provided by the values of `u` on a sufficiently dense set tα, slightly translated to fit in a finite ε₂-dense set tβ in the image. Such sets exist by compactness of the source and range. Then, to check that these data determine the function up to ε, one uses the control on the modulus of continuity to extend the closeness on tα to closeness everywhere. -/ have ε₂0 : ε₂ > 0 := half_pos (half_pos ε₁0), have : ∀x:α, ∃U, x ∈ U ∧ is_open U ∧ ∀ (y z ∈ U) {f : α →ᵇ β}, f ∈ A → dist (f y) (f z) < ε₂ := λ x, let ⟨U, nhdsU, hU⟩ := H x _ ε₂0, ⟨V, VU, openV, xV⟩ := mem_nhds_sets_iff.1 nhdsU in ⟨V, xV, openV, λy z hy hz f hf, hU y z (VU hy) (VU hz) f hf⟩, choose U hU using this, /- For all x, the set hU x is an open set containing x on which the elements of A fluctuate by at most ε₂. We extract finitely many of these sets that cover the whole space, by compactness -/ rcases compact_elim_finite_subcover_image compact_univ (λx _, (hU x).2.1) (λx hx, mem_bUnion (mem_univ _) (hU x).1) with ⟨tα, _, ⟨_⟩, htα⟩, /- tα : set α, htα : univ ⊆ ⋃x ∈ tα, U x -/ rcases @finite_cover_balls_of_compact β _ _ compact_univ _ ε₂0 with ⟨tβ, _, ⟨_⟩, htβ⟩, resetI, /- tβ : set β, htβ : univ ⊆ ⋃y ∈ tβ, ball y ε₂ -/ /- Associate to every point `y` in the space a nearby point `F y` in tβ -/ choose F hF using λy, show ∃z∈tβ, dist y z < ε₂, by simpa using htβ (mem_univ y), /- F : β → β, hF : ∀ (y : β), F y ∈ tβ ∧ dist y (F y) < ε₂ -/ /- Associate to every function a discrete approximation, mapping each point in `tα` to a point in `tβ` close to its true image by the function. -/ refine ⟨tα → tβ, by apply_instance, λ f a, ⟨F (f a), (hF (f a)).fst⟩, _⟩, rintro ⟨f, hf⟩ ⟨g, hg⟩ f_eq_g, /- If two functions have the same approximation, then they are within distance ε -/ refine lt_of_le_of_lt ((dist_le $ le_of_lt ε₁0).2 (λ x, _)) εε₁, have : ∃x', x' ∈ tα ∧ x ∈ U x' := mem_bUnion_iff.1 (htα (mem_univ x)), rcases this with ⟨x', x'tα, hx'⟩, refine calc dist (f x) (g x) ≤ dist (f x) (f x') + dist (g x) (g x') + dist (f x') (g x') : dist_triangle4_right _ _ _ _ ... ≤ ε₂ + ε₂ + ε₁/2 : le_of_lt (add_lt_add (add_lt_add _ _) _) ... = ε₁ : by rw [add_halves, add_halves], { exact (hU x').2.2 _ _ hx' ((hU x').1) hf }, { exact (hU x').2.2 _ _ hx' ((hU x').1) hg }, { have F_f_g : F (f x') = F (g x') := (congr_arg (λ f:tα → tβ, (f ⟨x', x'tα⟩ : β)) f_eq_g : _), calc dist (f x') (g x') ≤ dist (f x') (F (f x')) + dist (g x') (F (f x')) : dist_triangle_right _ _ _ ... = dist (f x') (F (f x')) + dist (g x') (F (g x')) : by rw F_f_g ... < ε₂ + ε₂ : add_lt_add (hF (f x')).snd (hF (g x')).snd ... = ε₁/2 : add_halves _ } end /-- Second version, with pointwise equicontinuity and range in a compact subset -/ theorem arzela_ascoli₂ (s : set β) (hs : compact s) (A : set (α →ᵇ β)) (closed : is_closed A) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ (nhds x).sets, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : compact A := /- This version is deduced from the previous one by restricting to the compact type in the target, using compactness there and then lifting everything to the original space. -/ begin have M : ∀x y : s, dist (x : β) y ≤ 1 * dist x y := λ x y, ge_of_eq (one_mul _), let F : (α →ᵇ s) → α →ᵇ β := comp coe M, refine compact_of_is_closed_subset (compact_image (_ : compact (F ⁻¹' A)) (continuous_comp M)) closed (λ f hf, _), { haveI : compact_space s := compact_iff_compact_space.1 hs, refine arzela_ascoli₁ _ (continuous_iff_is_closed.1 (continuous_comp M) _ closed) (λ x ε ε0, bex.imp_right (λ U U_nhds hU y z hy hz f hf, _) (H x ε ε0)), calc dist (f y) (f z) = dist (F f y) (F f z) : rfl ... < ε : hU y z hy hz (F f) hf }, { let g := cod_restrict s f (λx, in_s f x hf), rw [show f = F g, by ext; refl] at hf ⊢, exact ⟨g, hf, rfl⟩ } end /-- Third (main) version, with pointwise equicontinuity and range in a compact subset, but without closedness. The closure is then compact -/ theorem arzela_ascoli (s : set β) (hs : compact s) (A : set (α →ᵇ β)) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ (nhds x).sets, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : compact (closure A) := /- This version is deduced from the previous one by checking that the closure of A, in addition to being closed, still satisfies the properties of compact range and equicontinuity -/ arzela_ascoli₂ s hs (closure A) is_closed_closure (λ f x hf, (mem_of_closed' (closed_of_compact _ hs)).2 $ λ ε ε0, let ⟨g, gA, dist_fg⟩ := mem_closure_iff'.1 hf ε ε0 in ⟨g x, in_s g x gA, lt_of_le_of_lt (dist_coe_le_dist _) dist_fg⟩) (λ x ε ε0, show ∃ U ∈ (nhds x).sets, ∀ y z ∈ U, ∀ (f : α →ᵇ β), f ∈ closure A → dist (f y) (f z) < ε, begin refine bex.imp_right (λ U U_set hU y z hy hz f hf, _) (H x (ε/2) (half_pos ε0)), rcases mem_closure_iff'.1 hf (ε/2/2) (half_pos (half_pos ε0)) with ⟨g, gA, dist_fg⟩, replace dist_fg := λ x, lt_of_le_of_lt (dist_coe_le_dist x) dist_fg, calc dist (f y) (f z) ≤ dist (f y) (g y) + dist (f z) (g z) + dist (g y) (g z) : dist_triangle4_right _ _ _ _ ... < ε/2/2 + ε/2/2 + ε/2 : add_lt_add (add_lt_add (dist_fg y) (dist_fg z)) (hU y z hy hz g gA) ... = ε : by rw [add_halves, add_halves] end) /- To apply the previous theorems, one needs to check the equicontinuity. An important instance is when the source space is a metric space, and there is a fixed modulus of continuity for all the functions in the set A -/ lemma equicontinuous_of_continuity_modulus {α : Type u} [metric_space α] (b : ℝ → ℝ) (b_lim : tendsto b (nhds 0) (nhds 0)) (A : set (α →ᵇ β)) (H : ∀(x y:α) (f : α →ᵇ β), f ∈ A → dist (f x) (f y) ≤ b (dist x y)) (x:α) (ε : ℝ) (ε0 : ε > 0) : ∃U ∈ (nhds x).sets, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε := begin rcases tendsto_nhds_nhds.1 b_lim ε ε0 with ⟨δ, δ0, hδ⟩, refine ⟨ball x (δ/2), ball_mem_nhds x (half_pos δ0), λ y z hy hz f hf, _⟩, have : dist y z < δ := calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... < δ/2 + δ/2 : add_lt_add hy hz ... = δ : add_halves _, calc dist (f y) (f z) ≤ b (dist y z) : H y z f hf ... ≤ abs (b (dist y z)) : le_abs_self _ ... = dist (b (dist y z)) 0 : by simp [real.dist_eq] ... < ε : hδ (by simpa [real.dist_eq] using this), end end arzela_ascoli section normed_group /- In this section, if β is a normed group, then we show that the space of bounded continuous functions from α to β inherits a normed group structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] [normed_group β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_zero (α →ᵇ β) := ⟨const 0⟩ @[simp] lemma coe_zero : (0 : α →ᵇ β) x = 0 := rfl instance : has_norm (α →ᵇ β) := ⟨λu, dist u 0⟩ lemma norm_def : ∥f∥ = dist f 0 := rfl lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ := calc ∥f x∥ = dist (f x) ((0 : α →ᵇ β) x) : by simp [dist_zero_right] ... ≤ ∥f∥ : dist_coe_le_dist _ /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C := by simpa only [coe_zero, dist_zero_right] using @dist_le _ _ _ _ f 0 _ C0 /-- The pointwise sum of two bounded continuous functions is again bounded continuous. -/ instance : has_add (α →ᵇ β) := ⟨λf g, ⟨λx, f x + g x, continuous_add f.2.1 g.2.1, (∥f∥ + ∥g∥) + (∥f∥ + ∥g∥), λ x y, have ∀x, dist (f x + g x) 0 ≤ ∥f∥ + ∥g∥ := λx, calc dist (f x + g x) 0 = ∥f x + g x∥ : dist_zero_right _ ... ≤ ∥f x∥ + ∥g x∥ : norm_triangle _ _ ... ≤ ∥f∥ + ∥g∥ : add_le_add (norm_coe_le_norm _) (norm_coe_le_norm _), calc dist (f x + g x) (f y + g y) ≤ dist (f x + g x) 0 + dist (f y + g y) 0 : dist_triangle_right _ _ _ ... ≤ (∥f∥ + ∥g∥) + (∥f∥ + ∥g∥) : add_le_add (this x) (this y) ⟩⟩ /-- The pointwise opposite of a bounded continuous function is again bounded continuous. -/ instance : has_neg (α →ᵇ β) := ⟨λf, ⟨λx, -f x, continuous_neg f.2.1, begin have dn : ∀a b : β, dist (-a) (-b) = dist a b := λ a b, by rw [dist_eq_norm, neg_sub_neg, ← dist_eq_norm, dist_comm], simpa only [dn] using f.2.2 end⟩⟩ @[simp] lemma coe_add : (f + g) x = f x + g x := rfl @[simp] lemma coe_neg : (-f) x = - (f x) := rfl lemma forall_coe_zero_iff_zero : (∀x, f x = 0) ↔ f = 0 := ⟨@ext _ _ _ _ f 0, by rintro rfl _; refl⟩ instance : add_comm_group (α →ᵇ β) := { add_assoc := assume f g h, by ext; simp, zero_add := assume f, by ext; simp, add_zero := assume f, by ext; simp, add_left_neg := assume f, by ext; simp, add_comm := assume f g, by ext; simp, ..bounded_continuous_function.has_add, ..bounded_continuous_function.has_neg, ..bounded_continuous_function.has_zero } @[simp] lemma coe_diff : (f - g) x = f x - g x := rfl instance : normed_group (α →ᵇ β) := normed_group.of_add_dist (λ _, rfl) $ λ f g h, (dist_le dist_nonneg).2 $ λ x, le_trans (by rw [dist_eq_norm, dist_eq_norm, coe_add, coe_add, add_sub_add_right_eq_sub]) (dist_coe_le_dist x) lemma abs_diff_coe_le_dist : norm (f x - g x) ≤ dist f g := by rw normed_group.dist_eq; exact @norm_coe_le_norm _ _ _ _ (f-g) x lemma coe_le_coe_add_dist {f g : α →ᵇ ℝ} : f x ≤ g x + dist f g := sub_le_iff_le_add'.1 $ (abs_le.1 $ @dist_coe_le_dist _ _ _ _ f g x).2 /-- Constructing a bounded continuous function from a uniformly bounded continuous function taking values in a normed group. -/ def of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, norm (f x) ≤ C) (Hf : continuous f) : α →ᵇ β := ⟨λn, f n, ⟨Hf, ⟨C + C, λ m n, calc dist (f m) (f n) ≤ dist (f m) 0 + dist (f n) 0 : dist_triangle_right _ _ _ ... = norm (f m) + norm (f n) : by simp ... ≤ C + C : add_le_add (H m) (H n)⟩⟩⟩ /-- Constructing a bounded continuous function from a uniformly bounded function on a discrete space, taking values in a normed group -/ def of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, norm (f x) ≤ C) : α →ᵇ β := of_normed_group f C H continuous_of_discrete_topology end normed_group end bounded_continuous_function
6e1c512ac3010c68a942d0430056c086eb61240d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/list/lemmas.lean
0c3feaba9a113e5ad094e4b597610a4f85acfd48
[]
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
9,875
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.basic import Mathlib.Lean3Lib.init.function import Mathlib.Lean3Lib.init.meta.default import Mathlib.Lean3Lib.init.data.nat.lemmas import Mathlib.Lean3Lib.init.meta.interactive import Mathlib.Lean3Lib.init.meta.smt.rsimp universes u v w w₂ w₁ namespace Mathlib namespace list /- append -/ @[simp] theorem nil_append {α : Type u} (s : List α) : [] ++ s = s := rfl @[simp] theorem cons_append {α : Type u} (x : α) (s : List α) (t : List α) : x :: s ++ t = x :: (s ++ t) := rfl @[simp] theorem append_nil {α : Type u} (t : List α) : t ++ [] = t := sorry @[simp] theorem append_assoc {α : Type u} (s : List α) (t : List α) (u : List α) : s ++ t ++ u = s ++ (t ++ u) := sorry /- length -/ theorem length_cons {α : Type u} (a : α) (l : List α) : length (a :: l) = length l + 1 := rfl @[simp] theorem length_append {α : Type u} (s : List α) (t : List α) : length (s ++ t) = length s + length t := sorry @[simp] theorem length_repeat {α : Type u} (a : α) (n : ℕ) : length (repeat a n) = n := sorry @[simp] theorem length_tail {α : Type u} (l : List α) : length (tail l) = length l - 1 := list.cases_on l (Eq.refl (length (tail []))) fun (l_hd : α) (l_tl : List α) => Eq.refl (length (tail (l_hd :: l_tl))) -- TODO(Leo): cleanup proof after arith dec proc @[simp] theorem length_drop {α : Type u} (i : ℕ) (l : List α) : length (drop i l) = length l - i := sorry /- map -/ theorem map_cons {α : Type u} {β : Type v} (f : α → β) (a : α) (l : List α) : map f (a :: l) = f a :: map f l := rfl @[simp] theorem map_append {α : Type u} {β : Type v} (f : α → β) (l₁ : List α) (l₂ : List α) : map f (l₁ ++ l₂) = map f l₁ ++ map f l₂ := sorry theorem map_singleton {α : Type u} {β : Type v} (f : α → β) (a : α) : map f [a] = [f a] := rfl @[simp] theorem map_id {α : Type u} (l : List α) : map id l = l := sorry @[simp] theorem map_map {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : α → β) (l : List α) : map g (map f l) = map (g ∘ f) l := sorry @[simp] theorem length_map {α : Type u} {β : Type v} (f : α → β) (l : List α) : length (map f l) = length l := sorry /- bind -/ @[simp] theorem nil_bind {α : Type u} {β : Type v} (f : α → List β) : list.bind [] f = [] := sorry @[simp] theorem cons_bind {α : Type u} {β : Type v} (x : α) (xs : List α) (f : α → List β) : list.bind (x :: xs) f = f x ++ list.bind xs f := sorry @[simp] theorem append_bind {α : Type u} {β : Type v} (xs : List α) (ys : List α) (f : α → List β) : list.bind (xs ++ ys) f = list.bind xs f ++ list.bind ys f := sorry /- mem -/ @[simp] theorem mem_nil_iff {α : Type u} (a : α) : a ∈ [] ↔ False := iff.rfl @[simp] theorem not_mem_nil {α : Type u} (a : α) : ¬a ∈ [] := iff.mp (mem_nil_iff a) @[simp] theorem mem_cons_self {α : Type u} (a : α) (l : List α) : a ∈ a :: l := Or.inl rfl @[simp] theorem mem_cons_iff {α : Type u} (a : α) (y : α) (l : List α) : a ∈ y :: l ↔ a = y ∨ a ∈ l := iff.rfl theorem mem_cons_eq {α : Type u} (a : α) (y : α) (l : List α) : a ∈ y :: l = (a = y ∨ a ∈ l) := rfl theorem mem_cons_of_mem {α : Type u} (y : α) {a : α} {l : List α} : a ∈ l → a ∈ y :: l := fun (H : a ∈ l) => Or.inr H theorem eq_or_mem_of_mem_cons {α : Type u} {a : α} {y : α} {l : List α} : a ∈ y :: l → a = y ∨ a ∈ l := fun (h : a ∈ y :: l) => h @[simp] theorem mem_append {α : Type u} {a : α} {s : List α} {t : List α} : a ∈ s ++ t ↔ a ∈ s ∨ a ∈ t := sorry theorem mem_append_eq {α : Type u} (a : α) (s : List α) (t : List α) : a ∈ s ++ t = (a ∈ s ∨ a ∈ t) := propext mem_append theorem mem_append_left {α : Type u} {a : α} {l₁ : List α} (l₂ : List α) (h : a ∈ l₁) : a ∈ l₁ ++ l₂ := iff.mpr mem_append (Or.inl h) theorem mem_append_right {α : Type u} {a : α} (l₁ : List α) {l₂ : List α} (h : a ∈ l₂) : a ∈ l₁ ++ l₂ := iff.mpr mem_append (Or.inr h) @[simp] theorem not_bex_nil {α : Type u} (p : α → Prop) : ¬∃ (x : α), ∃ (H : x ∈ []), p x := sorry @[simp] theorem ball_nil {α : Type u} (p : α → Prop) (x : α) (H : x ∈ []) : p x := false.elim @[simp] theorem bex_cons {α : Type u} (p : α → Prop) (a : α) (l : List α) : (∃ (x : α), ∃ (H : x ∈ a :: l), p x) ↔ p a ∨ ∃ (x : α), ∃ (H : x ∈ l), p x := sorry @[simp] theorem ball_cons {α : Type u} (p : α → Prop) (a : α) (l : List α) : (∀ (x : α), x ∈ a :: l → p x) ↔ p a ∧ ∀ (x : α), x ∈ l → p x := sorry /- list subset -/ protected def subset {α : Type u} (l₁ : List α) (l₂ : List α) := ∀ {a : α}, a ∈ l₁ → a ∈ l₂ protected instance has_subset {α : Type u} : has_subset (List α) := has_subset.mk list.subset @[simp] theorem nil_subset {α : Type u} (l : List α) : [] ⊆ l := fun (b : α) (i : b ∈ []) => false.elim (iff.mp (mem_nil_iff b) i) @[simp] theorem subset.refl {α : Type u} (l : List α) : l ⊆ l := fun (b : α) (i : b ∈ l) => i theorem subset.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ ⊆ l₂) (h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := fun (b : α) (i : b ∈ l₁) => h₂ (h₁ i) @[simp] theorem subset_cons {α : Type u} (a : α) (l : List α) : l ⊆ a :: l := fun (b : α) (i : b ∈ l) => Or.inr i theorem subset_of_cons_subset {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : a :: l₁ ⊆ l₂ → l₁ ⊆ l₂ := fun (s : a :: l₁ ⊆ l₂) (b : α) (i : b ∈ l₁) => s (mem_cons_of_mem a i) theorem cons_subset_cons {α : Type u} {l₁ : List α} {l₂ : List α} (a : α) (s : l₁ ⊆ l₂) : a :: l₁ ⊆ a :: l₂ := fun (b : α) (hin : b ∈ a :: l₁) => or.elim (eq_or_mem_of_mem_cons hin) (fun (e : b = a) => Or.inl e) fun (i : b ∈ l₁) => Or.inr (s i) @[simp] theorem subset_append_left {α : Type u} (l₁ : List α) (l₂ : List α) : l₁ ⊆ l₁ ++ l₂ := fun (b : α) => mem_append_left l₂ @[simp] theorem subset_append_right {α : Type u} (l₁ : List α) (l₂ : List α) : l₂ ⊆ l₁ ++ l₂ := fun (b : α) => mem_append_right l₁ theorem subset_cons_of_subset {α : Type u} (a : α) {l₁ : List α} {l₂ : List α} : l₁ ⊆ l₂ → l₁ ⊆ a :: l₂ := fun (s : l₁ ⊆ l₂) (a_1 : α) (i : a_1 ∈ l₁) => Or.inr (s i) theorem eq_nil_of_length_eq_zero {α : Type u} {l : List α} : length l = 0 → l = [] := sorry theorem ne_nil_of_length_eq_succ {α : Type u} {l : List α} {n : ℕ} : length l = Nat.succ n → l ≠ [] := sorry @[simp] theorem length_map₂ {α : Type u} {β : Type v} {γ : Type w} (f : α → β → γ) (l₁ : List α) (l₂ : List β) : length (map₂ f l₁ l₂) = min (length l₁) (length l₂) := sorry @[simp] theorem length_take {α : Type u} (i : ℕ) (l : List α) : length (take i l) = min i (length l) := sorry theorem length_take_le {α : Type u} (n : ℕ) (l : List α) : length (take n l) ≤ n := sorry theorem length_remove_nth {α : Type u} (l : List α) (i : ℕ) : i < length l → length (remove_nth l i) = length l - 1 := sorry @[simp] theorem partition_eq_filter_filter {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : partition p l = (filter p l, filter (Not ∘ p) l) := sorry /- sublists -/ inductive sublist {α : Type u} : List α → List α → Prop where | slnil : sublist [] [] | cons : ∀ (l₁ l₂ : List α) (a : α), sublist l₁ l₂ → sublist l₁ (a :: l₂) | cons2 : ∀ (l₁ l₂ : List α) (a : α), sublist l₁ l₂ → sublist (a :: l₁) (a :: l₂) infixl:50 " <+ " => Mathlib.list.sublist theorem length_le_of_sublist {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → length l₁ ≤ length l₂ := sorry /- filter -/ @[simp] theorem filter_nil {α : Type u} (p : α → Prop) [h : decidable_pred p] : filter p [] = [] := rfl @[simp] theorem filter_cons_of_pos {α : Type u} {p : α → Prop} [h : decidable_pred p] {a : α} (l : List α) : p a → filter p (a :: l) = a :: filter p l := fun (pa : p a) => if_pos pa @[simp] theorem filter_cons_of_neg {α : Type u} {p : α → Prop} [h : decidable_pred p] {a : α} (l : List α) : ¬p a → filter p (a :: l) = filter p l := fun (pa : ¬p a) => if_neg pa @[simp] theorem filter_append {α : Type u} {p : α → Prop} [h : decidable_pred p] (l₁ : List α) (l₂ : List α) : filter p (l₁ ++ l₂) = filter p l₁ ++ filter p l₂ := sorry @[simp] theorem filter_sublist {α : Type u} {p : α → Prop} [h : decidable_pred p] (l : List α) : filter p l <+ l := sorry /- map_accumr -/ -- This runs a function over a list returning the intermediate results and a -- a final result. def map_accumr {α : Type u} {β : Type v} {σ : Type w₂} (f : α → σ → σ × β) : List α → σ → σ × List β := sorry @[simp] theorem length_map_accumr {α : Type u} {β : Type v} {σ : Type w₂} (f : α → σ → σ × β) (x : List α) (s : σ) : length (prod.snd (map_accumr f x s)) = length x := sorry -- This runs a function over two lists returning the intermediate results and a -- a final result. def map_accumr₂ {α : Type u} {β : Type v} {φ : Type w₁} {σ : Type w₂} (f : α → β → σ → σ × φ) : List α → List β → σ → σ × List φ := sorry @[simp] theorem length_map_accumr₂ {α : Type u} {β : Type v} {φ : Type w₁} {σ : Type w₂} (f : α → β → σ → σ × φ) (x : List α) (y : List β) (c : σ) : length (prod.snd (map_accumr₂ f x y c)) = min (length x) (length y) := sorry
15e5c71436107092d975d9832aab7b95986fe0d7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/pureCoeIssue.lean
8d6908c32ed509a7826754708b2b3eaf5472c5da
[ "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
240
lean
def f1 (x : Nat) : IO Unit := do IO.println x return () def g1 : IO Unit := do f1 -- Error pure () def f2 (x : Nat) (y : Nat) : IO Unit := do IO.println s!"{x} {y}" return () def g2 : IO Unit := do f2 10 -- Error pure ()
773dcfe2cd56136a2390acefaf5d3178e96f83bc
f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58
/data/finset.lean
0d8bdc7c7a7a818ccad0967aae75fd6bd0bb73dc
[ "Apache-2.0" ]
permissive
semorrison/mathlib
1be6f11086e0d24180fec4b9696d3ec58b439d10
20b4143976dad48e664c4847b75a85237dca0a89
refs/heads/master
1,583,799,212,170
1,535,634,130,000
1,535,730,505,000
129,076,205
0
0
Apache-2.0
1,551,697,998,000
1,523,442,265,000
Lean
UTF-8
Lean
false
false
59,209
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro Finite sets. -/ import logic.embedding order.boolean_algebra algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ h := by congr; assumption @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 end finset namespace finset instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /- set coercion -/ /-- Convert a finset to a set in the natural way. -/ def to_set (s : finset α) : set α := {x | x ∈ s} instance : has_lift (finset α) (set α) := ⟨to_set⟩ @[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl /- extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[extensionality] theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext.2 @[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := (set.ext_iff _ _).trans ext.symm /- subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp [set.ssubset_iff_subset_not_subset] {contextual := tt} @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff /- empty -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ | e := not_mem_empty a $ e ▸ h @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨λ h, by simp [h], λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := by simp [set.ext_iff] /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩ local prefix `ι`:90 := singleton @[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := by simp [singleton] theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := by simp theorem mem_singleton_self (a : α) : a ∈ ι a := by simp theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := by simp [set.ext_iff] /- insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ @[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by simp [erase_dup_cons] theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := by simp theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := by simp * theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := by simp [set.ext_iff] @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext.2 $ by simp [or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext.2 $ by simp @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem subset_insert [h : decidable_eq α] (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume ⟨h₁, h₂⟩, have ∃a, a ∈ t ∧ a ∉ s, by simpa [finset.subset_iff, classical.not_forall] using h₂, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s @[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl @[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a := by simp [singleton] /- union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := by simp * theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := by simp * theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by simp [not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := by simp [set.ext_iff] theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left {s₁ s₂ : finset α} : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right {s₁ s₂ : finset α} : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ @[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := by simp [ext, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := by simp [ext, or_comm, or.left_comm] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ by simp instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext.2 $ by simp [or_comm, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by simp @[simp] theorem union_self (s : finset α) : s ∪ s = s := by simp @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := by simp [ext] @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := by simp [ext] theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := by simp [ext, or_comm, or.left_comm] @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp [ext, or_comm, or.left_comm] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp [ext, or.left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp [ext] /- inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp [subset_iff] {contextual:=tt}; finish @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := by simp [set.ext_iff] @[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ by simp [and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ by simp [and_comm, and.left_comm] @[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ by simp @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ by simp @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ by simp @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext.2 $ by simp; intro x; constructor; finish @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext.2 $ assume a', by by_cases h' : a' = a; simp [mem_inter, mem_insert, h, h', and_comm] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} : a ∈ s → ι a ∩ s = ι a := show a ∈ s → insert a ∅ ∩ s = insert a ∅, by simp {contextual := tt} @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} : a ∉ s → ι a ∩ s = ∅ := show a ∉ s → insert a ∅ ∩ s = ∅, by simp {contextual := tt} @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] /- lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := assume a b, subset_union_left, le_sup_right := assume a b, subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := assume a b, inter_subset_left, inf_le_right := assume a b, inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp [subset_iff, and_imp, or_imp_distrib] {contextual:=tt}, ..finset.lattice.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish /- erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := by simp @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp {contextual:=tt} theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp {contextual:=tt} theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext.2 $ assume x, by simp; constructor; finish theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext.2 $ assume x, by simp; constructor; finish theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := by simp [set.ext_iff, and_comm] lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp [subset_iff, or_iff_not_imp_left]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /- sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 @[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext.2 $ λ a, by simpa [or_and_distrib_left, dec_em] using or_iff_right_of_imp (@h a) @[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := ext.2 $ by simp {contextual := tt} @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := by simp theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa [subset_iff] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := by simp [set.ext_iff] end decidable_eq /- attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /- filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext.2 $ assume a, by simp [and_comm, and.left_comm] @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext.2 $ assume a, by simp lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext.2 $ by simp [or_and_distrib_right] theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext.2 $ by simp [and_or_distrib_left] theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ by simp [and_comm, and.left_comm] theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext.2 $ by simpa [and_comm] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ by simp theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp [filter_not] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp [filter_not] @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := by simp [set.ext_iff] end filter /- range -/ section range variables {n m l : ℕ} /-- `range n` is the set of integers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ by simp @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := finset.induction_on s ⟨0, by simp⟩ $ λ a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa using le_max_left (a+1) n, subset.trans hn (by simp [le_max_right])⟩⟩ end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp [or_and_distrib_right, exists_or_distrib] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := by simp theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp [or_imp_distrib, forall_and_distrib] end finset /- erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset section map open function def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) (s : finset α) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.map f := mem_map.2 ⟨_, h, rfl⟩ theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ by simp theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ by simp [embedding.refl] theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem map_subset_map {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.map f ⊆ s₂.map f := by simp [subset_def, map_subset_map h] theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext.2 $ λ b, by simp; rw ← exists_and_distrib_right; refine exists_congr (λ a, (and_congr_right $ λ e, _).trans and.right_comm); simp [e.2.symm] theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext.2 $ by simp [mem_map, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext.2 $ by simp [mem_map]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, f.2 (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) := ext.2 $ by simp [mem_map, eq_comm] @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp [insert_eq, map_union] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by simp [embedding.subtype]; rw attach_val; simp [multiset.attach_map_val] end map section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := by simp [set.ext_iff] theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ by simp @[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ by simp theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp [subset_def, multiset.map_subset_map h] theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ λ b, by simp [and_comm]; rw ← exists_and_distrib_left; exact exists_congr (λ a, and.left_comm.trans $ and_congr_right $ λ e, by simp [e.symm]) theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext.2 $ by simp [mem_image, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext.2 $ by simp [mem_image]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (f a) := ext.2 $ by simp [mem_image, eq_comm] @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp [insert_eq, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by simp [multiset.attach_map_val] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := begin apply eq_of_veq, dsimp, rw [attach_ndinsert, multiset.erase_dup_eq_self.2], { refl }, apply nodup_map_on, exact assume ⟨a', _⟩ _ ⟨b', _⟩ _ h, by simp at h; simp [h], exact multiset.nodup_attach.2 s.2 end theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const [decidable_eq β] {s : finset α} (h : s ≠ ∅) (b : β) : s.image (λa, b) = singleton b := ext.2 $ assume b', by simp [exists_mem_of_ne_empty h, eq_comm] end image /- card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ := pos_iff_ne_zero.trans $ not_congr card_eq_zero @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa [card] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; simp [h, nat.le_add_right] @[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_range (n : ℕ) : card (range n) = n := card_range n theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp [card, image_val_of_inj_on H] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa [ext], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n lemma card_eq_succ [decidable_eq α] {s : finset α} {a : α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have card s > 0, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp [eq, card_erase_of_mem has]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card [decidable_eq α] {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on [decidable_eq α] [decidable_eq β] {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simp; assumption) (by simp; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) @[elab_as_eliminator] lemma strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) end card section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp [mem_def] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext.2 $ by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a := show (insert a ∅ : finset α).bind t = t a, by simp theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp {contextual := tt}) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [image_union] {contextual := tt}) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext.2 $ by simp lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa [finset.subset_iff] lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := finset.ext.mpr $ by simp [eq_comm] end bind section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext.2 $ by simp [and.left_comm] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} : s₁ ⊆ s₂ → (∀a, t₁ a ⊆ t₂ a) → s₁.sigma t₁ ⊆ s₂.sigma t₂ := by simp [subset_iff, mem_sigma] {contextual := tt} theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext.2 $ by simp [and.left_comm] end sigma section pi variables {δ : α → Type*} [decidable_eq α] def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa using h) = pi.cons s a b e₂ e (by simpa using h), by rw [eq], this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end end pi section powerset def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp [powerset]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) end powerset section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by simp [fold, ndinsert_of_not_mem h] @[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b := by simp [fold] @[simp] theorem fold_image [decidable_eq α] [decidable_eq γ] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp [fold, image_val_of_inj_on H, map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp [fold] end fold section sup variables [semilattice_sup_bot α] /-- 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 : by simp lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp [-sup_le_iff, sup_le_sup] {contextual := tt}) lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := by letI := classical.dec_eq β; from calc f b ≤ f b ⊔ s.sup f : le_sup_left ... = (insert b s).sup f : by simp ... = s.sup f : by simp [hb] lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) 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) end sup 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 : by simp lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp [inf_le_inf] {contextual := tt}) lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := by letI := classical.dec_eq β; from calc f b ≥ f b ⊓ s.inf f : inf_le_left ... = (insert b s).inf f : by simp ... = s.inf f : by simp [hb] lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) 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) end inf /- max and min of finite sets -/ section max_min variables [decidable_linear_order α] protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := by simp [finset.max] @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := by simp [finset.max, fold_insert_idem] @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by simp [finset.max, option.lift_or_get] @[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_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := max_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (by simp) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := by simp [finset.min] @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := by simp [finset.min, fold_insert_idem] @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by simp [finset.min, option.lift_or_get] theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := min_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (by simp) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end theorem le_min_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption end max_min section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ end sort section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp [_root_.disjoint, subset_iff]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp [disjoint_left]; refl @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := by rw disjoint.comm; simp @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp [disjoint_left, or_imp_distrib, forall_and_distrib]; refl @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint_left, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint_right, or_imp_distrib, forall_and_distrib] @[simp] theorem card_disjoint_union {s t : finset α} : disjoint s t → card (s ∪ t) = card s + card t := finset.induction_on s (by simp) $ by simp {contextual := tt} end disjoint theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ end finset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} : l.nodup → l.to_finset.card = l.length := begin induction l, case list.nil { simp }, case list.cons : _ _ ih { intros nd, simp at nd, simp [finset.card_insert_of_not_mem ((not_iff_not_of_iff mem_to_finset).mpr nd.1), ih nd.2] } end end list
261b296e8697f9292e3b1a21bb1459830fb9a617
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/doc/examples/NFM2022/nfm19.lean
2246d4363250b75d91fc32362ba9a8bd8b279843
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
313
lean
/- Tactic combinators -/ example : p → q → r → p ∧ ((p ∧ q) ∧ r) ∧ (q ∧ r ∧ p) := by intros repeat (any_goals constructor) all_goals assumption example : p → q → r → p ∧ ((p ∧ q) ∧ r) ∧ (q ∧ r ∧ p) := by intros repeat (any_goals (first | assumption | constructor))
87795fde8788f5dd069302f9eb7b15ffc4b78570
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/11_Tactic-Style_Proofs.org.2.lean
896c2e2ad9a54a91f0f0ef6f56ffc28b0b06bdea
[]
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
168
lean
import standard -- BEGIN theorem test (p q : Prop) (Hp : p) (Hq : q) : p ∧ q ∧ p := begin apply and.intro, exact Hp, apply and.intro, exact Hq, exact Hp end -- END
a056db40abcd2402c4bc467f31a92369ecaa1365
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Lean/Elab/Tactic/Induction.lean
fabe1f136e439ebd1cd6e7af4104eda3e06463e0
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,275
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Lean.Meta.RecursorInfo import Init.Lean.Meta.Tactic.Induction import Init.Lean.Meta.Tactic.Cases import Init.Lean.Elab.Tactic.ElabTerm import Init.Lean.Elab.Tactic.Generalize namespace Lean namespace Elab namespace Tactic -- Recall that -- majorPremise := parser! optional (try (ident >> " : ")) >> termParser private def getAuxHypothesisName (stx : Syntax) : Option Name := if ((stx.getArg 1).getArg 0).isNone then none else some (((stx.getArg 1).getArg 0).getIdAt 0) private def getMajor (stx : Syntax) : Syntax := (stx.getArg 1).getArg 1 private def elabMajor (ref : Syntax) (h? : Option Name) (major : Syntax) : TacticM Expr := do match h? with | none => withMainMVarContext ref $ elabTerm major none | some h => withMainMVarContext ref $ do lctx ← getLCtx; let x := lctx.getUnusedName `x; major ← elabTerm major none; evalGeneralizeAux ref h? major x; withMainMVarContext ref $ do lctx ← getLCtx; match lctx.findFromUserName? x with | some decl => pure decl.toExpr | none => throwError ref "failed to generalize" private def generalizeMajor (ref : Syntax) (major : Expr) : TacticM Expr := do match major with | Expr.fvar _ _ => pure major | _ => do liftMetaTacticAux ref $ fun mvarId => do mvarId ← Meta.generalize mvarId major `x; (fvarId, mvarId) ← Meta.intro1 mvarId; pure (mkFVar fvarId, [mvarId]) /- Recall that ``` generalizingVars := optional (" generalizing " >> many1 ident) «induction» := parser! nonReservedSymbol "induction " >> majorPremise >> usingRec >> generalizingVars >> withAlts ``` `stx` is syntax for `induction`. -/ private def getGeneralizingFVarIds (stx : Syntax) : TacticM (Array FVarId) := let generalizingStx := stx.getArg 3; if generalizingStx.isNone then pure #[] else withMainMVarContext stx $ do trace `Elab.induction stx $ fun _ => generalizingStx; let vars := (generalizingStx.getArg 1).getArgs; getFVarIds vars -- process `generalizingVars` subterm of induction Syntax `stx`. private def generalizeVars (stx : Syntax) (major : Expr) : TacticM Nat := do fvarIds ← getGeneralizingFVarIds stx; liftMetaTacticAux stx $ fun mvarId => do (fvarIds, mvarId') ← Meta.revert mvarId fvarIds; when (fvarIds.contains major.fvarId!) $ Meta.throwTacticEx `induction mvarId "major premise depends on variable being generalized"; pure (fvarIds.size, [mvarId']) private def getAlts (withAlts : Syntax) : Array Syntax := (withAlts.getArg 2).getArgs.getSepElems /- Given an `inductionAlt` of the form ``` nodeWithAntiquot "inductionAlt" `Lean.Parser.Tactic.inductionAlt $ ident' >> many ident' >> darrow >> termParser ``` -/ private def getAltName (alt : Syntax) : Name := (alt.getArg 0).getId.eraseMacroScopes private def getAltVarNames (alt : Syntax) : Array Name := (alt.getArg 1).getArgs.map Syntax.getId private def getAltRHS (alt : Syntax) : Syntax := alt.getArg 3 /- Given alts of the form ``` nodeWithAntiquot "inductionAlt" `Lean.Parser.Tactic.inductionAlt $ ident' >> many ident' >> darrow >> termParser ``` esnure the first `ident'` is `_` or a constructor name. -/ private def checkAltCtorNames (alts : Array Syntax) (ctorNames : List Name) : TacticM Unit := alts.forM $ fun alt => do let n := getAltName alt; trace `Elab.checkAlt alt $ fun _ => n ++ ", " ++ alt; unless (n == `_ || ctorNames.any (fun ctorName => n.isSuffixOf ctorName)) $ throwError (alt.getArg 0) ("invalid constructor name '" ++ toString n ++ "'") structure RecInfo := (recName : Name) (altVars : Array (List Name) := #[]) -- new variable names for each minor premise (altRHSs : Array Syntax := #[]) -- RHS for each minor premise def getInductiveValFromMajor (ref : Syntax) (major : Expr) : TacticM InductiveVal := liftMetaMAtMain ref $ fun mvarId => do majorType ← Meta.inferType major; majorType ← Meta.whnf majorType; match majorType.getAppFn with | Expr.const n _ _ => do env ← Meta.getEnv; match env.find? n with | ConstantInfo.inductInfo val => pure val | _ => Meta.throwTacticEx `induction mvarId ("major premise type is not an inductive type " ++ indentExpr majorType) | _ => Meta.throwTacticEx `induction mvarId ("major premise type is not an inductive type " ++ indentExpr majorType) private partial def getRecFromUsingLoop (ref : Syntax) (baseRecName : Name) : Expr → TacticM (Option Meta.RecursorInfo) | majorType => do let continue (majorType : Expr) : TacticM (Option Meta.RecursorInfo) := do { majorType? ← unfoldDefinition? ref majorType; match majorType? with | some majorType => withIncRecDepth ref $ getRecFromUsingLoop majorType | none => pure none }; majorType ← whnfCore ref majorType; match majorType.getAppFn with | Expr.const name _ _ => do let candidate := name ++ baseRecName; env ← getEnv; match env.find? candidate with | some _ => catch (liftMetaMAtMain ref $ fun _ => do info ← Meta.mkRecursorInfo candidate; pure (some info)) (fun _ => continue majorType) | none => continue majorType | _ => continue majorType def getRecFromUsing (ref : Syntax) (major : Expr) (baseRecName : Name) : TacticM Meta.RecursorInfo := do majorType ← inferType ref major; recInfo? ← getRecFromUsingLoop ref baseRecName majorType; match recInfo? with | some recInfo => pure recInfo | none => do result ← resolveGlobalName baseRecName; match result with | _::_::_ => throwError ref ("ambiguous recursor name '" ++ baseRecName ++ "', " ++ toString (result.map Prod.fst)) | [(recName, [])] => do catch (liftMetaMAtMain ref $ fun _ => Meta.mkRecursorInfo recName) (fun _ => throwError ref ("invalid recursor name '" ++ baseRecName ++ "'")) | _ => throwError ref ("invalid recursor name '" ++ baseRecName ++ "'") /- Create `RecInfo` assuming builtin recursor -/ private def getRecInfoDefault (ref : Syntax) (major : Expr) (withAlts : Syntax) (allowMissingAlts : Bool) : TacticM (RecInfo × Array Name) := do indVal ← getInductiveValFromMajor ref major; let recName := mkRecFor indVal.name; if withAlts.isNone then pure ({ recName := recName }, #[]) else do let ctorNames := indVal.ctors; let alts := getAlts withAlts; checkAltCtorNames alts ctorNames; (altVars, altRHSs, remainingAlts, _) ← ctorNames.foldlM (fun (result : Array (List Name) × Array Syntax × Array Syntax × Option Syntax) (ctorName : Name) => do let (altVars, altRHSs, remainingAlts, prevAnonymousAlt?) := result; match remainingAlts.findIdx? (fun alt => (getAltName alt).isSuffixOf ctorName) with | some idx => let newAlt := remainingAlts.get! idx; pure (altVars.push (getAltVarNames newAlt).toList, altRHSs.push (getAltRHS newAlt), remainingAlts.eraseIdx idx, prevAnonymousAlt?) | none => match remainingAlts.findIdx? (fun alt => getAltName alt == `_) with | some idx => let newAlt := remainingAlts.get! idx; pure (altVars.push (getAltVarNames newAlt).toList, altRHSs.push (getAltRHS newAlt), remainingAlts.eraseIdx idx, some newAlt) | none => match prevAnonymousAlt? with | some alt => pure (altVars.push (getAltVarNames alt).toList, altRHSs.push (getAltRHS alt), remainingAlts, prevAnonymousAlt?) | none => if allowMissingAlts then pure (altVars.push [], altRHSs.push Syntax.missing, remainingAlts, prevAnonymousAlt?) else throwError ref ("alternative for constructor '" ++ toString ctorName ++ "' is missing")) (#[], #[], alts, none); unless remainingAlts.isEmpty $ throwError (remainingAlts.get! 0) "unused alternative"; pure ({ recName := recName, altVars := altVars, altRHSs := altRHSs }, ctorNames.toArray) /- Recall that ``` inductionAlt : Parser := nodeWithAntiquot "inductionAlt" `Lean.Parser.Tactic.inductionAlt $ ident' >> many ident' >> darrow >> (hole <|> namedHole <|> tacticParser) inductionAlts : Parser := withPosition $ fun pos => "|" >> sepBy1 inductionAlt (checkColGe pos.column "alternatives must be indented" >> "|") withAlts : Parser := optional (" with " >> inductionAlts) usingRec : Parser := optional (" using " >> ident) ``` -/ private def getRecInfo (stx : Syntax) (major : Expr) : TacticM RecInfo := let ref := stx; let usingRecStx := stx.getArg 2; let withAlts := stx.getArg 4; if usingRecStx.isNone then do (rinfo, _) ← getRecInfoDefault ref major withAlts false; pure rinfo else do let baseRecName := (usingRecStx.getIdAt 1).eraseMacroScopes; recInfo ← getRecFromUsing ref major baseRecName; let recName := recInfo.recursorName; if withAlts.isNone then pure { recName := recName } else do let alts := getAlts withAlts; paramNames ← liftMetaMAtMain ref $ fun _ => Meta.getParamNames recInfo.recursorName; (altVars, altRHSs, remainingAlts, _) ← paramNames.size.foldM (fun (i : Nat) (result : Array (List Name) × Array Syntax × Array Syntax × Option Syntax) => if recInfo.isMinor i then let paramName := paramNames.get! i; let (altVars, altRHSs, remainingAlts, prevAnonymousAlt?) := result; match remainingAlts.findIdx? (fun alt => getAltName alt == paramName) with | some idx => let newAlt := remainingAlts.get! idx; pure (altVars.push (getAltVarNames newAlt).toList, altRHSs.push (getAltRHS newAlt), remainingAlts.eraseIdx idx, prevAnonymousAlt?) | none => match remainingAlts.findIdx? (fun alt => getAltName alt == `_) with | some idx => let newAlt := remainingAlts.get! idx; pure (altVars.push (getAltVarNames newAlt).toList, altRHSs.push (getAltRHS newAlt), remainingAlts.eraseIdx idx, some newAlt) | none => match prevAnonymousAlt? with | some alt => pure (altVars.push (getAltVarNames alt).toList, altRHSs.push (getAltRHS alt), remainingAlts, prevAnonymousAlt?) | none => throwError ref ("alternative for minor premise '" ++ toString paramName ++ "' is missing") else pure result) (#[], #[], alts, none); unless remainingAlts.isEmpty $ throwError (remainingAlts.get! 0) "unused alternative"; pure { recName := recName, altVars := altVars, altRHSs := altRHSs } -- Return true if `stx` is a term occurring in the RHS of the induction/cases tactic private def isTermRHS (rhs : Syntax) : Bool := rhs.isOfKind `Lean.Parser.Term.namedHole || rhs.isOfKind `Lean.Parser.Term.hole private def processResult (ref : Syntax) (altRHSs : Array Syntax) (result : Array Meta.InductionSubgoal) : TacticM Unit := do if altRHSs.isEmpty then setGoals $ result.toList.map $ fun s => s.mvarId else do unless (altRHSs.size == result.size) $ throwError ref ("mistmatch on the number of subgoals produced (" ++ toString result.size ++ ") and " ++ "alternatives provided (" ++ toString altRHSs.size ++ ")"); gs ← result.size.foldM (fun i gs => do let subgoal := result.get! i; let rhs := altRHSs.get! i; let ref := rhs; let mvarId := subgoal.mvarId; if isTermRHS rhs then withMVarContext mvarId $ do mvarDecl ← getMVarDecl mvarId; val ← elabTerm rhs mvarDecl.type; val ← ensureHasType rhs mvarDecl.type val; assignExprMVar mvarId val; gs' ← collectMVars rhs val; tagUntaggedGoals mvarDecl.userName `induction gs'; pure (gs ++ gs') else do setGoals [mvarId]; evalTactic rhs; done ref; pure gs) []; setGoals gs @[builtinTactic «induction»] def evalInduction : Tactic := fun stx => focusAux stx $ do let h? := getAuxHypothesisName stx; major ← elabMajor stx h? (getMajor stx); major ← generalizeMajor stx major; n ← generalizeVars stx major; recInfo ← getRecInfo stx major; (mvarId, _) ← getMainGoal stx; result ← liftMetaM stx $ Meta.induction mvarId major.fvarId! recInfo.recName recInfo.altVars; processResult stx recInfo.altRHSs result private partial def checkCasesResultAux (ref : Syntax) (casesResult : Array Meta.CasesSubgoal) (ctorNames : Array Name) (altRHSs : Array Syntax) : Nat → Nat → TacticM Unit | i, j => if h : j < altRHSs.size then do let altRHS := altRHSs.get ⟨j, h⟩; if altRHS.isMissing then checkCasesResultAux i (j+1) else let ctorName := ctorNames.get! j; if h : i < casesResult.size then let subgoal := casesResult.get ⟨i, h⟩; if ctorName == subgoal.ctorName then checkCasesResultAux (i+1) (j+1) else throwError ref ("alternative for '" ++ subgoal.ctorName ++ "' has not been provided") else throwError ref ("alternative for '" ++ ctorName ++ "' is not needed") else if h : i < casesResult.size then let subgoal := casesResult.get ⟨i, h⟩; throwError ref ("alternative for '" ++ subgoal.ctorName ++ "' has not been provided") else pure () private def checkCasesResult (ref : Syntax) (casesResult : Array Meta.CasesSubgoal) (ctorNames : Array Name) (altRHSs : Array Syntax) : TacticM Unit := unless altRHSs.isEmpty $ checkCasesResultAux ref casesResult ctorNames altRHSs 0 0 @[builtinTactic «cases»] def evalCases : Tactic := fun stx => focusAux stx $ do -- parser! nonReservedSymbol "cases " >> majorPremise >> withAlts let h? := getAuxHypothesisName stx; major ← elabMajor stx h? (getMajor stx); major ← generalizeMajor stx major; (mvarId, _) ← getMainGoal stx; let withAlts := stx.getArg 2; (recInfo, ctorNames) ← getRecInfoDefault stx major withAlts true; result ← liftMetaM stx $ Meta.cases mvarId major.fvarId! recInfo.altVars; checkCasesResult stx result ctorNames recInfo.altRHSs; let result := result.map (fun s => s.toInductionSubgoal); let altRHSs := recInfo.altRHSs.filter $ fun stx => !stx.isMissing; processResult stx altRHSs result end Tactic end Elab end Lean
daa6df28d0165c0c77e93d9c482d1e4730428eb0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/stateRef.lean
37820021b31f51e3d2a59108a14679cbdd3f1a48
[ "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
1,570
lean
def f (v : Nat) : StateRefT Nat IO Nat := do IO.println "hello" modify fun s => s - v get def g : IO Nat := f 5 |>.run' 20 #eval (f 5).run' 20 #eval (do set 100; f 5 : StateRefT Nat IO Nat).run' 0 def f2 : ReaderT Nat (StateRefT Nat IO) Nat := do let v ← read IO.println $ "context " ++ toString v modify fun s => s + v get #eval (f2.run 10).run' 20 def f3 : StateT String (StateRefT Nat IO) Nat := do let s ← get let n ← getThe Nat set $ s ++ ", " ++ toString n let s ← get IO.println s set (n+1) getThe Nat #eval (f3.run' "test").run' 10 structure Label {β : Type} (v : β) (α : Type) := (val : α) class HasGetAt {β : Type} (v : β) (α : outParam Type) (m : Type → Type) := (getAt : m α) instance monadState.hasGetAt (β : Type) (v : β) (α : Type) (m : Type → Type) [Monad m] [MonadStateOf (Label v α) m] : HasGetAt v α m := { getAt := do let a ← getThe (Label v α); pure a.val } export HasGetAt (getAt) abbrev M := StateRefT (Label 0 Nat) $ StateRefT (Label 1 Nat) $ StateRefT (Label 2 Nat) IO def f4 : M Nat := do let a0 : Nat ← getAt 0 let a1 ← getAt 1 let a2 ← getAt 2 IO.println $ "state0 " ++ toString a0 IO.println $ "state1 " ++ toString a1 IO.println $ "state1 " ++ toString a2 pure (a0 + a1 + a2) #eval f4.run' ⟨10⟩ |>.run' ⟨20⟩ |>.run' ⟨30⟩ abbrev S (ω : Type) := StateRefT Nat $ StateRefT String $ ST ω def f5 {ω} : S ω Unit := do let s ← getThe String modify fun n => n + s.length pure () def f5Pure (n : Nat) (s : String) := runST fun _ => f5.run n |>.run s #eval f5Pure 10 "hello world"
e5a0d034ce4e2786717cf39eef466a7e21a7398a
e9dbaaae490bc072444e3021634bf73664003760
/src/Problems/2002/IMO_2002_P2.lean
4568dc9d724a383fe81dd096c1c66bae51f3900c
[ "Apache-2.0" ]
permissive
liaofei1128/geometry
566d8bfe095ce0c0113d36df90635306c60e975b
3dd128e4eec8008764bb94e18b932f9ffd66e6b3
refs/heads/master
1,678,996,510,399
1,581,454,543,000
1,583,337,839,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
533
lean
import Geo.Geo.Core namespace Geo open Analytic Arc Circle Triangle def IMO_2002_P2 : Prop := ∀ (B C O : Point) (Γ : Circle), isOrigin O Γ → isDiameter B C Γ → ∀ (A : Point), on A Γ → deg2π 60 < uangle ⟨A, O, C⟩ → ∀ (E F : Point), on E Γ → -- ryankrue: could have isChord (p₁ p₂ : Point) (Γ : Circle) on F Γ → isPerpBis ⟨E, F⟩ ⟨A, O⟩ → let D := (buildMinor Γ A B).midp; ∀ (J : Point), intersectAt (Line.buildPara O ⟨A, D⟩) (Line.mk A C) J → isIncenter J ⟨C, E, F⟩ end Geo
6fc1f8f2c97681f6fe08d1b926a005befd37a002
32a2d1642d7519c99693bc1d3b24069e4853dd1f
/tactic/interactive.lean
2f12506a0febdcae8a2279fd134d6f5aa4310e7a
[ "Apache-2.0" ]
permissive
Cedric0099/mathlib
7edb81d5d68e280b4d21f6c0377dad1f9b8c0d71
a97101d2df5d186848075a2d0452f6a04d8a13eb
refs/heads/master
1,584,201,847,599
1,524,979,632,000
1,524,979,632,000
131,690,350
0
0
null
1,525,162,341,000
1,525,162,341,000
null
UTF-8
Lean
false
false
17,119
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.dlist tactic.basic tactic.rcases tactic.generalize_proofs meta.expr open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic namespace interactive open interactive interactive.types expr meta def rcases_parse : parser (list rcases_patt) := with_desc "patt" $ let p := (rcases_patt.one <$> ident_) <|> (rcases_patt.many <$> brackets "⟨" "⟩" (sep_by (tk ",") rcases_parse)) in list.cons <$> p <*> (tk "|" *> p)* meta def rcases_parse.invert : list rcases_patt → list (list rcases_patt) := let invert' (l : list rcases_patt) : rcases_patt := match l with | [k] := k | _ := rcases_patt.many (rcases_parse.invert l) end in list.map $ λ p, match p with | rcases_patt.one n := [rcases_patt.one n] | rcases_patt.many l := invert' <$> l end /-- 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. -/ meta def rcases (p : parse texpr) (ids : parse (tk "with" *> rcases_parse)?) : tactic unit := tactic.rcases p $ rcases_parse.invert $ ids.get_or_else [default _] /-- This is a "finishing" tactic modification of `simp`. The tactic `simpa [rules, ...] using e` will simplify the hypothesis `e` using `rules`, then simplify the goal using `rules`, and try to close the goal using `assumption`. If `e` is a term instead of a local constant, it is first added to the local context using `have`. -/ meta def simpa (use_iota_eqn : parse $ (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (tgt : parse (tk "using" *> texpr)?) (cfg : simp_config_ext := {}) : tactic unit := let simp_at (lc) := simp use_iota_eqn no_dflt hs attr_names (loc.ns lc) cfg >> try (assumption <|> trivial) in match tgt with | none := get_local `this >> simp_at [some `this, none] <|> simp_at [none] | some e := (do e ← i_to_expr e, match e with | local_const _ lc _ _ := simp_at [some lc, none] | e := do t ← infer_type e, assertv `this t e >> simp_at [some `this, none] end) <|> (do simp_at [none], ty ← target, e ← i_to_expr_strict ``(%%e : %%ty), -- for positional error messages, don't care about the result pty ← pp ty, ptgt ← pp e, -- Fail deliberately, to advise regarding `simp; exact` usage fail ("simpa failed, 'using' expression type not directly " ++ "inferrable. Try:\n\nsimpa ... using\nshow " ++ to_fmt pty ++ ",\nfrom " ++ ptgt : format)) end /-- `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, tactic.try_for max tac <|> (tactic.trace "try_for timeout, using sorry" >> admit) /-- 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) /-- Unfold coercion-related definitions -/ meta def unfold_coes (loc : parse location) : tactic unit := unfold [``coe,``lift_t,``has_lift_t.lift,``coe_t,``has_coe_t.coe,``coe_b,``has_coe.coe, ``coe_fn, ``has_coe_to_fun.coe, ``coe_sort, ``has_coe_to_sort.coe] loc /-- 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 := do r ← tactic.result, tactic.set_goals $ r.fold [] $ λ e _ l, match e with | expr.mvar _ _ _ := insert e l | _ := l end /-- 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 := if n = 2 then tactic.swap else tactic.rotate (n-1) /-- 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 /-- Same as the `congr` tactic, but only works up to depth `n`. This is useful when the `congr` tactic 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_n 2` produces the intended `⊢ x + y = y + x`. -/ meta def congr_n : nat → tactic unit | 0 := failed | (n+1) := focus1 (try assumption >> congr_core >> all_goals (try reflexivity >> try (congr_n n))) /-- 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 /-- Unfreeze local instances, which allows us to revert instances in the context. -/ meta def unfreezeI := tactic.unfreeze_local_instances /-- Reset the instance cache. This allows any new instances added to the context to be used in typeclass inference. -/ meta def resetI := reset_instance_cache /-- Like `intro`, but uses the introduced variable in typeclass inference. -/ meta def introI (p : parse ident_?) : tactic unit := intro p >> reset_instance_cache /-- Like `intros`, but uses the introduced variable(s) in typeclass inference. -/ meta def introsI (p : parse ident_*) : tactic unit := intros p >> reset_instance_cache /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `have`, but the proof-omitted version is not supported. For this one must write `have : t, { <proof> }, resetI, <proof>`. -/ meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse (tk ":=" *> texpr)) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «have» (some h) q₁ (some q₂), match q₁ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `let`. -/ meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «let» (some h) q₁ q₂, match q₁ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Like `exact`, but uses all variables in the context for typeclass inference. -/ meta def exactI (q : parse texpr) : tactic unit := reset_instance_cache >> exact q /-- `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 : option (list expr) := none) (tac : tactic unit := return ()) : tactic unit := do { ctx ← asms.to_monad <|> local_context, t ← target, hs ← find_matching_head t ctx, hs.any_of (λ H, () <$ tactic.apply H ; tac) } <|> do { exfalso, ctx ← asms.to_monad <|> local_context, t ← target, hs ← find_matching_head t ctx, hs.any_of (λ H, () <$ tactic.apply H ; tac) } <|> fail "assumption tactic failed" open nat /-- `solve_by_elim` calls `apply_assumption` on the main goal to find an assumption whose head matches and repeated calls `apply_assumption` on the generated subgoals until no subgoals remains or up to `depth` times. `solve_by_elim` discharges the current goal or fails `solve_by_elim` does some back-tracking if `apply_assumption` chooses an unproductive assumption optional arguments: - discharger: a subsidiary tactic to try at each step (`cc` is often helpful) - asms: list of assumptions / rules to consider instead of local constants - depth: number of attempts at discharging generated sub-goals -/ meta def solve_by_elim (discharger : tactic unit := done) (asms : option (list expr) := none) : opt_param ℕ 3 → tactic unit | 0 := done | (succ n) := discharger <|> (apply_assumption asms $ solve_by_elim n) /-- `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 : tactic unit := repeat (do gs ← get_goals, () <$ tactic.intros ; casesm (some ()) [``(_ ∧ _),``(_ ∨ _),``(Exists _)] ; constructor_matching (some ()) [``(_ ∧ _),``(_ ↔ _)], gs' ← get_goals, guard (gs ≠ gs') ) ; repeat (reflexivity <|> solve_by_elim <|> constructor_matching none [``(_ ∧ _),``(_ ↔ _),``(Exists _)]) ; done /-- Shorter name for the tactic `tautology`. -/ meta def tauto := tautology /-- `wlog h : i ≤ j using i j`: without loss of generality, let us assume `h : i ≤ j` If `using i j` is omitted, the last two free variables found in `i ≤ j` will be used. `wlog : R x y` (synonymous with `wlog : R x y using x y`) adds `R x y` to the assumptions and the goal `⊢ R x y ∨ R y x`. A special case is made for total order relations `≤` where `⊢ R x y ∨ R y x` is discharged automatically. TODO(Simon): Generalize to multiple pairs of variables -/ meta def wlog (h : parse ident?) (p : parse (tk ":" *> texpr)) (xy : parse (tk "using" *> monad.sequence [ident,ident])?) : tactic unit := do p' ← to_expr p, h_asm ← get_unused_name (h.get_or_else `a), (x :: y :: _) ← xy.to_monad >>= mmap get_local <|> pure p'.list_local_const, n ← tactic.revert_lst [x,y], x ← intro1, y ← intro1, p ← to_expr p, when (¬ x.occurs p ∧ ¬ x.occurs p) (do p ← pp p, fail format!"{p} should reference {x} and {y}"), let p' := subst_locals [(x,y),(y,x)] p, t ← target, asm ← mk_local_def h_asm p, g ← tactic.pis [x,y,asm] t, h_this₀ ← get_unused_name `this, (this,gs) ← local_proof h_this₀ (set_binder g [binder_info.default,binder_info.default]) (do tactic.clear x, tactic.clear y, intron 2, intro $ h_asm, intron (n-2)), intron (n-2), p_or_p' ← to_expr ``(%%p ∨ %%p'), h_this ← get_unused_name `this, (h',gs') ← local_proof h_this p_or_p' (do tactic.clear this, try $ assumption <|> `[exact le_total _ _]), (() <$ tactic.cases h' [h_asm,h_asm]) ; [ (do h ← get_local h_asm, specialize ```(%%this %%x %%y %%h) <|> fail "spec A"), do h ← get_local h_asm, specialize ```(%%this %%y %%x %%h) <|> fail "spec B" ] ; try (solve_by_elim <|> tauto <|> (tactic.intros >> cc)), gs'' ← get_goals, set_goals $ gs' ++ gs'' ++ gs, return () /-- Tag lemmas of the form: ``` lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` -/ @[user_attribute] meta def extensional_attribute : user_attribute := { name := `extensionality, descr := "lemmas usable by `ext` tactic" } attribute [extensionality] funext array.ext /-- `ext1 id` selects and apply one extensionality lemma (with attribute `extensionality`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. -/ meta def ext1 (x : parse ident_ ?) : tactic unit := do ls ← attribute.get_instances `extensionality, ls.any_of (λ l, applyc l) <|> fail "no applicable extensionality rule found", interactive.intro x /-- - `ext` applies as many extensionality lemmas as possible; - `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them until it runs out of identifiers in `ids` to name the local constants. When trying to prove: ``` α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ``` α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ f x ``` by applying functional extensionality and set extensionality. -/ meta def ext : parse ident_ * → tactic unit | [] := repeat (ext1 none) | xs := xs.mmap' (ext1 ∘ some) 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], (option.cases_on n congr congr_n : tactic unit), gs' ← get_goals, set_goals $ gs' ++ gs meta def clean_ids : list name := [``id, ``id_rhs, ``id_delta] /-- 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) end interactive end tactic
f635cc11357767ae1ea860a454ecd2431033fc3d
c777c32c8e484e195053731103c5e52af26a25d1
/src/probability/process/filtration.lean
7a3c14f17260bfc878c42f6e6b96b2ee7d61598e
[ "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
13,072
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Rémy Degenne -/ import measure_theory.function.conditional_expectation.real /-! # Filtrations This file defines filtrations of a measurable space and σ-finite filtrations. ## Main definitions * `measure_theory.filtration`: a filtration on a measurable space. That is, a monotone sequence of sub-σ-algebras. * `measure_theory.sigma_finite_filtration`: a filtration `f` is σ-finite with respect to a measure `μ` if for all `i`, `μ.trim (f.le i)` is σ-finite. * `measure_theory.filtration.natural`: the smallest filtration that makes a process adapted. That notion `adapted` is not defined yet in this file. See `measure_theory.adapted`. ## Main results * `measure_theory.filtration.complete_lattice`: filtrations are a complete lattice. ## Tags filtration, stochastic process -/ open filter order topological_space open_locale classical measure_theory nnreal ennreal topology big_operators namespace measure_theory /-- A `filtration` on a measurable space `Ω` with σ-algebra `m` is a monotone sequence of sub-σ-algebras of `m`. -/ structure filtration {Ω : Type*} (ι : Type*) [preorder ι] (m : measurable_space Ω) := (seq : ι → measurable_space Ω) (mono' : monotone seq) (le' : ∀ i : ι, seq i ≤ m) variables {Ω β ι : Type*} {m : measurable_space Ω} instance [preorder ι] : has_coe_to_fun (filtration ι m) (λ _, ι → measurable_space Ω) := ⟨λ f, f.seq⟩ namespace filtration variables [preorder ι] protected lemma mono {i j : ι} (f : filtration ι m) (hij : i ≤ j) : f i ≤ f j := f.mono' hij protected lemma le (f : filtration ι m) (i : ι) : f i ≤ m := f.le' i @[ext] protected lemma ext {f g : filtration ι m} (h : (f : ι → measurable_space Ω) = g) : f = g := by { cases f, cases g, simp only, exact h, } variable (ι) /-- The constant filtration which is equal to `m` for all `i : ι`. -/ def const (m' : measurable_space Ω) (hm' : m' ≤ m) : filtration ι m := ⟨λ _, m', monotone_const, λ _, hm'⟩ variable {ι} @[simp] lemma const_apply {m' : measurable_space Ω} {hm' : m' ≤ m} (i : ι) : const ι m' hm' i = m' := rfl instance : inhabited (filtration ι m) := ⟨const ι m le_rfl⟩ instance : has_le (filtration ι m) := ⟨λ f g, ∀ i, f i ≤ g i⟩ instance : has_bot (filtration ι m) := ⟨const ι ⊥ bot_le⟩ instance : has_top (filtration ι m) := ⟨const ι m le_rfl⟩ instance : has_sup (filtration ι m) := ⟨λ f g, { seq := λ i, f i ⊔ g i, mono' := λ i j hij, sup_le ((f.mono hij).trans le_sup_left) ((g.mono hij).trans le_sup_right), le' := λ i, sup_le (f.le i) (g.le i) }⟩ @[norm_cast] lemma coe_fn_sup {f g : filtration ι m} : ⇑(f ⊔ g) = f ⊔ g := rfl instance : has_inf (filtration ι m) := ⟨λ f g, { seq := λ i, f i ⊓ g i, mono' := λ i j hij, le_inf (inf_le_left.trans (f.mono hij)) (inf_le_right.trans (g.mono hij)), le' := λ i, inf_le_left.trans (f.le i) }⟩ @[norm_cast] lemma coe_fn_inf {f g : filtration ι m} : ⇑(f ⊓ g) = f ⊓ g := rfl instance : has_Sup (filtration ι m) := ⟨λ s, { seq := λ i, Sup ((λ f : filtration ι m, f i) '' s), mono' := λ i j hij, begin refine Sup_le (λ m' hm', _), rw [set.mem_image] at hm', obtain ⟨f, hf_mem, hfm'⟩ := hm', rw ← hfm', refine (f.mono hij).trans _, have hfj_mem : f j ∈ ((λ g : filtration ι m, g j) '' s), from ⟨f, hf_mem, rfl⟩, exact le_Sup hfj_mem, end, le' := λ i, begin refine Sup_le (λ m' hm', _), rw [set.mem_image] at hm', obtain ⟨f, hf_mem, hfm'⟩ := hm', rw ← hfm', exact f.le i, end, }⟩ lemma Sup_def (s : set (filtration ι m)) (i : ι) : Sup s i = Sup ((λ f : filtration ι m, f i) '' s) := rfl noncomputable instance : has_Inf (filtration ι m) := ⟨λ s, { seq := λ i, if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m, mono' := λ i j hij, begin by_cases h_nonempty : set.nonempty s, swap, { simp only [h_nonempty, set.nonempty_image_iff, if_false, le_refl], }, simp only [h_nonempty, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂], refine λ f hf_mem, le_trans _ (f.mono hij), have hfi_mem : f i ∈ ((λ g : filtration ι m, g i) '' s), from ⟨f, hf_mem, rfl⟩, exact Inf_le hfi_mem, end, le' := λ i, begin by_cases h_nonempty : set.nonempty s, swap, { simp only [h_nonempty, if_false, le_refl], }, simp only [h_nonempty, if_true], obtain ⟨f, hf_mem⟩ := h_nonempty, exact le_trans (Inf_le ⟨f, hf_mem, rfl⟩) (f.le i), end, }⟩ lemma Inf_def (s : set (filtration ι m)) (i : ι) : Inf s i = if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m := rfl noncomputable instance : complete_lattice (filtration ι m) := { le := (≤), le_refl := λ f i, le_rfl, le_trans := λ f g h h_fg h_gh i, (h_fg i).trans (h_gh i), le_antisymm := λ f g h_fg h_gf, filtration.ext $ funext $ λ i, (h_fg i).antisymm (h_gf i), sup := (⊔), le_sup_left := λ f g i, le_sup_left, le_sup_right := λ f g i, le_sup_right, sup_le := λ f g h h_fh h_gh i, sup_le (h_fh i) (h_gh _), inf := (⊓), inf_le_left := λ f g i, inf_le_left, inf_le_right := λ f g i, inf_le_right, le_inf := λ f g h h_fg h_fh i, le_inf (h_fg i) (h_fh i), Sup := Sup, le_Sup := λ s f hf_mem i, le_Sup ⟨f, hf_mem, rfl⟩, Sup_le := λ s f h_forall i, Sup_le $ λ m' hm', begin obtain ⟨g, hg_mem, hfm'⟩ := hm', rw ← hfm', exact h_forall g hg_mem i, end, Inf := Inf, Inf_le := λ s f hf_mem i, begin have hs : s.nonempty := ⟨f, hf_mem⟩, simp only [Inf_def, hs, if_true], exact Inf_le ⟨f, hf_mem, rfl⟩, end, le_Inf := λ s f h_forall i, begin by_cases hs : s.nonempty, swap, { simp only [Inf_def, hs, if_false], exact f.le i, }, simp only [Inf_def, hs, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂], exact λ g hg_mem, h_forall g hg_mem i, end, top := ⊤, bot := ⊥, le_top := λ f i, f.le' i, bot_le := λ f i, bot_le, } end filtration lemma measurable_set_of_filtration [preorder ι] {f : filtration ι m} {s : set Ω} {i : ι} (hs : measurable_set[f i] s) : measurable_set[m] s := f.le i s hs /-- A measure is σ-finite with respect to filtration if it is σ-finite with respect to all the sub-σ-algebra of the filtration. -/ class sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) : Prop := (sigma_finite : ∀ i : ι, sigma_finite (μ.trim (f.le i))) instance sigma_finite_of_sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) [hf : sigma_finite_filtration μ f] (i : ι) : sigma_finite (μ.trim (f.le i)) := by apply hf.sigma_finite -- can't exact here @[priority 100] instance is_finite_measure.sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) [is_finite_measure μ] : sigma_finite_filtration μ f := ⟨λ n, by apply_instance⟩ /-- Given a integrable function `g`, the conditional expectations of `g` with respect to a filtration is uniformly integrable. -/ lemma integrable.uniform_integrable_condexp_filtration [preorder ι] {μ : measure Ω} [is_finite_measure μ] {f : filtration ι m} {g : Ω → ℝ} (hg : integrable g μ) : uniform_integrable (λ i, μ[g | f i]) 1 μ := hg.uniform_integrable_condexp f.le section of_set variables [preorder ι] /-- Given a sequence of measurable sets `(sₙ)`, `filtration_of_set` is the smallest filtration such that `sₙ` is measurable with respect to the `n`-the sub-σ-algebra in `filtration_of_set`. -/ def filtration_of_set {s : ι → set Ω} (hsm : ∀ i, measurable_set (s i)) : filtration ι m := { seq := λ i, measurable_space.generate_from {t | ∃ j ≤ i, s j = t}, mono' := λ n m hnm, measurable_space.generate_from_mono (λ t ⟨k, hk₁, hk₂⟩, ⟨k, hk₁.trans hnm, hk₂⟩), le' := λ n, measurable_space.generate_from_le (λ t ⟨k, hk₁, hk₂⟩, hk₂ ▸ hsm k) } lemma measurable_set_filtration_of_set {s : ι → set Ω} (hsm : ∀ i, measurable_set[m] (s i)) (i : ι) {j : ι} (hj : j ≤ i) : measurable_set[filtration_of_set hsm i] (s j) := measurable_space.measurable_set_generate_from ⟨j, hj, rfl⟩ lemma measurable_set_filtration_of_set' {s : ι → set Ω} (hsm : ∀ n, measurable_set[m] (s n)) (i : ι) : measurable_set[filtration_of_set hsm i] (s i) := measurable_set_filtration_of_set hsm i le_rfl end of_set namespace filtration variables [topological_space β] [metrizable_space β] [mβ : measurable_space β] [borel_space β] [preorder ι] include mβ /-- Given a sequence of functions, the natural filtration is the smallest sequence of σ-algebras such that that sequence of functions is measurable with respect to the filtration. -/ def natural (u : ι → Ω → β) (hum : ∀ i, strongly_measurable (u i)) : filtration ι m := { seq := λ i, ⨆ j ≤ i, measurable_space.comap (u j) mβ, mono' := λ i j hij, bsupr_mono $ λ k, ge_trans hij, le' := λ i, begin refine supr₂_le _, rintros j hj s ⟨t, ht, rfl⟩, exact (hum j).measurable ht, end } section open measurable_space lemma filtration_of_set_eq_natural [mul_zero_one_class β] [nontrivial β] {s : ι → set Ω} (hsm : ∀ i, measurable_set[m] (s i)) : filtration_of_set hsm = natural (λ i, (s i).indicator (λ ω, 1 : Ω → β)) (λ i, strongly_measurable_one.indicator (hsm i)) := begin simp only [natural, filtration_of_set, measurable_space_supr_eq], ext1 i, refine le_antisymm (generate_from_le _) (generate_from_le _), { rintro _ ⟨j, hij, rfl⟩, refine measurable_set_generate_from ⟨j, measurable_set_generate_from ⟨hij, _⟩⟩, rw comap_eq_generate_from, refine measurable_set_generate_from ⟨{1}, measurable_set_singleton 1, _⟩, ext x, simp [set.indicator_const_preimage_eq_union] }, { rintro t ⟨n, ht⟩, suffices : measurable_space.generate_from {t | ∃ (H : n ≤ i), measurable_set[(measurable_space.comap ((s n).indicator (λ ω, 1 : Ω → β)) mβ)] t} ≤ generate_from {t | ∃ (j : ι) (H : j ≤ i), s j = t}, { exact this _ ht }, refine generate_from_le _, rintro t ⟨hn, u, hu, hu'⟩, obtain heq | heq | heq | heq := set.indicator_const_preimage (s n) u (1 : β), swap 4, rw set.mem_singleton_iff at heq, all_goals { rw heq at hu', rw ← hu' }, exacts [measurable_set_empty _, measurable_set.univ, measurable_set_generate_from ⟨n, hn, rfl⟩, measurable_set.compl (measurable_set_generate_from ⟨n, hn, rfl⟩)] } end end section limit omit mβ variables {E : Type*} [has_zero E] [topological_space E] {ℱ : filtration ι m} {f : ι → Ω → E} {μ : measure Ω} /-- Given a process `f` and a filtration `ℱ`, if `f` converges to some `g` almost everywhere and `g` is `⨆ n, ℱ n`-measurable, then `limit_process f ℱ μ` chooses said `g`, else it returns 0. This definition is used to phrase the a.e. martingale convergence theorem `submartingale.ae_tendsto_limit_process` where an L¹-bounded submartingale `f` adapted to `ℱ` converges to `limit_process f ℱ μ` `μ`-almost everywhere. -/ noncomputable def limit_process (f : ι → Ω → E) (ℱ : filtration ι m) (μ : measure Ω . volume_tac) := if h : ∃ g : Ω → E, strongly_measurable[⨆ n, ℱ n] g ∧ ∀ᵐ ω ∂μ, tendsto (λ n, f n ω) at_top (𝓝 (g ω)) then classical.some h else 0 lemma strongly_measurable_limit_process : strongly_measurable[⨆ n, ℱ n] (limit_process f ℱ μ) := begin rw limit_process, split_ifs with h h, exacts [(classical.some_spec h).1, strongly_measurable_zero] end lemma strongly_measurable_limit_process' : strongly_measurable[m] (limit_process f ℱ μ) := strongly_measurable_limit_process.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _)) lemma mem_ℒp_limit_process_of_snorm_bdd {R : ℝ≥0} {p : ℝ≥0∞} {F : Type*} [normed_add_comm_group F] {ℱ : filtration ℕ m} {f : ℕ → Ω → F} (hfm : ∀ n, ae_strongly_measurable (f n) μ) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : mem_ℒp (limit_process f ℱ μ) p μ := begin rw limit_process, split_ifs with h, { refine ⟨strongly_measurable.ae_strongly_measurable ((classical.some_spec h).1.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _))), lt_of_le_of_lt (Lp.snorm_lim_le_liminf_snorm hfm _ (classical.some_spec h).2) (lt_of_le_of_lt _ (ennreal.coe_lt_top : ↑R < ∞))⟩, simp_rw [liminf_eq, eventually_at_top], exact Sup_le (λ b ⟨a, ha⟩, (ha a le_rfl).trans (hbdd _)) }, { exact zero_mem_ℒp } end end limit end filtration end measure_theory
710dad1ea560f7d5fc91499632e82ab23f8cb119
fb3b29254af2d298d934a2ec1bddbb387e5ee67d
/src/misc.lean
8fc06a7d6bdaa745bf1a39d17c869b02bda4d9ff
[]
no_license
th-char/cayleys_theorem
cdbc48c543184320ae474b27e6d99e4b1c931e69
c4862adbe1e6a8892fd607217c803f260ee74be2
refs/heads/master
1,668,812,911,278
1,595,171,637,000
1,595,175,437,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,352
lean
import algebra.group.basic import data.zmod.basic import data.equiv.basic import tactic import data.set.basic import .cayleys variables {G₁ : Type*} {G₂ : Type*} [group G₁] [group G₂] open equiv function set def hom_induces_group (f : G₁ →* G₂) : group (range f) := { mul := has_mul.mul, mul_assoc := mul_assoc, one := has_one.one, one_mul := one_mul, mul_one := mul_one, inv := has_inv.inv, mul_left_inv := mul_left_inv, } def ψ (h : G₁ ≃* G₂): perm G₁ → perm G₂ := λ θ, { to_fun := h.1 ∘ θ.1 ∘ h.2, inv_fun := h.1 ∘ θ.2 ∘ h.2, left_inv := begin intro x, calc (h.to_fun ∘ θ.inv_fun ∘ h.inv_fun) ((h.to_fun ∘ θ.to_fun ∘ h.inv_fun) x) = (h.to_fun ∘ θ.inv_fun ∘ (h.inv_fun ∘ h.to_fun) ∘ θ.to_fun ∘ h.inv_fun) x : rfl ... = (h.to_fun ∘ θ.inv_fun ∘ id ∘ θ.to_fun ∘ h.inv_fun) x : by rw left_inverse.id h.3 ... = (h.to_fun ∘ (θ.inv_fun ∘ θ.to_fun) ∘ h.inv_fun) x : rfl ... = (h.to_fun ∘ id ∘ h.inv_fun) x : by rw left_inverse.id θ.3 ... = (h.to_fun ∘ h.inv_fun) x : rfl ... = id x : by rw right_inverse.id h.4, end, right_inv := begin intro x, calc (h.to_fun ∘ θ.to_fun ∘ h.inv_fun) ((h.to_fun ∘ θ.inv_fun ∘ h.inv_fun) x) = (h.to_fun ∘ θ.to_fun ∘ (h.inv_fun ∘ h.to_fun) ∘ θ.inv_fun ∘ h.inv_fun) x : rfl ... = (h.to_fun ∘ θ.to_fun ∘ id ∘ θ.inv_fun ∘ h.inv_fun) x : by rw left_inverse.id h.3 ... = (h.to_fun ∘ (θ.to_fun ∘ θ.inv_fun) ∘ h.inv_fun) x : rfl ... = (h.to_fun ∘ id ∘ h.inv_fun) x : by rw right_inverse.id θ.4 ... = (h.to_fun ∘ h.inv_fun) x : rfl ... = id x : by rw right_inverse.id h.4, end, } lemma embedding_diagram_com (h : G₁ ≃* G₂) : (ψ h) ∘ (lift_to_perm G₁).1 = (lift_to_perm G₂).1 ∘ h.1 := begin ext g x, have H₁ : ((ψ h ∘ (lift_to_perm G₁).to_fun) g) x = (h.1 g) * x, calc ((ψ h ∘ (lift_to_perm G₁).to_fun) g) x = h.1 (g * (h.2 x)) : rfl ... = (h.1 g) * (h.1 (h.2 x)) : mul_equiv.map_mul h g (h.inv_fun x) ... = (h.1 g) * ((h.1 ∘ h.2) x) : rfl ... = (h.1 g) * (id x) : by rw right_inverse.id h.4, have H₂ : (((lift_to_perm G₂).to_fun ∘ h.1) g) x = (h.1 g) * x := rfl, rw [H₁, H₂], end
f10a936908124a416b751e795ccbbd7a44317c19
390c7d44c0470f935f1ff06637607521791400da
/test1.lean
08145739c5cfa5a73f901ad250aac563205a643f
[]
no_license
avigad/auto
32baee2744dd3ae53f1cffd3f04523b953e5a9eb
1aa4ebf60f900e8d61bb423105592a4010fa6ec5
refs/heads/master
1,594,791,920,875
1,475,608,824,000
1,475,608,824,000
67,415,120
2
0
null
null
null
null
UTF-8
Lean
false
false
3,008
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Examples to test the tableau provers. -/ import .auto open tactic -- useful for debugging -- set_option pp.beta false -- set_option pp.delayed_abstraction true -- set_option pp.universes true -- set_option trace.type_context.is_def_eq true -- set_option trace.type_context.is_def_eq_detail true /- -- Quantifier-free examples -/ section variables A B C D : Prop /- clarify -/ example (H : ¬ A) (H' : A) : C := by clarify' example (H₁ : A ∧ A ∧ B) (H₂ : A ∧ C ∧ B) : A := by clarify' /- safe -/ example (H : A) (H₁ : ¬ B) : ¬ (A → B) := by safe' example : A ∨ B → B ∨ A := by safe' example : A ∧ B → B ∧ A := by safe' example (H : A) (H₁ : A → B) (H₂ : B → C) : C := by safe' example (H₁ : A ∧ B) (H₂ : C ∧ B) : C := by safe' example (HA : A) (HB : B) (HC : C) (HD : D) : (A ∧ B) ∧ (C ∧ D) := by safe' example (H₁ : A ∧ B) (H₂ : B ∧ ¬ C) : A ∨ C := by safe' example : (A → B) ∧ (B → C) → A → C := by safe' example : (A → B) ∨ (B → A) := by safe' example : ((A → B) → A) → A := by safe' /- iauto -/ example (H : A) (H₁ : ¬ B) : ¬ (A → B) := by iauto' example : ¬ (A ↔ ¬ A) := by iauto' example (H : A ↔ B) (H₁ : A ∧ B → C) (H₂ : ¬ A ∧ ¬ B → C) : C := by safe' example (H : A ↔ B) (H₁ : (A ∧ ¬ B) ∨ (¬ A ∧ B)) : C := by safe' example (H : A → B) (H₁ : A) : B := by iauto' example (H : A → B) (H₁ : B → C) : A → C := by iauto' example (H : A → B ∨ C) (H₁ : B → D) (H₂ : C → D) : A → D := by iauto' example : A ∨ B → B ∨ A := by iauto' /- using injectivity -/ section open nat /- using injectivity -/ example (x y : ℕ) : succ x = succ y → x = y ∨ x = succ y := by safe' -- example (x y z : ℕ) : succ (succ x) = succ y ∧ y = succ z → -- y = succ x ∧ succ x = succ z := by ssafe' end /- -- Examples with quantifiers -/ section variables (X : Type) (P Q R : X → Prop) (T : X → X → Prop) (a b : X) /- auto -/ example (H : ∀ x, P x → Q x) (H₁ : ∀ x, P x) : Q a := by auto' example (H : ∀ x, P x → Q x) (H₁ : P a) : Q a := by auto' /- iauto -/ example (H₁ : P a) (H₂ : P b) : ∃ x, P x := by iauto' example (H₁ : P a) (H₂ : P b) (H₃ : Q b) : ∃ x, P x ∧ Q x := by iauto' example (H₁ : P b) (H₂ : Q b) (H₃ : P a) : ∃ x, P x ∧ Q x := by iauto' example (H : ∀ x, P x → Q x ∧ R x) (a : X) (H₁ : P a) : R a ∧ Q a := by auto' example (H : ∃ x, P x ∧ Q x) : ∃ x, Q x ∧ P x := by iauto' -- not valid in dependent type theory! -- example : ∃ x, ((∃ y, P y) → P x) := -- by auto' -- but this would require more intelligent handling of quantifiers -- example (H : ∃ x : X, x = x) : ∃ x, ((∃ y, P y) → P x) := -- by auto' example : (∃ x, ∀ y, T x y) → ∀ y, ∃ x, T x y := by iauto' end end
71d11b36596170b7208f006d721d49e9791af204
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/quadratic_form.lean
c6a2d4ad532f2f1bc44f2686c7f0c7ef2a970ab9
[ "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,521
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen -/ import algebra.invertible import linear_algebra.bilinear_form import linear_algebra.determinant import linear_algebra.special_linear_group /-! # Quadratic forms This file defines quadratic forms over a `R`-module `M`. A quadratic form is a map `Q : M → R` such that (`to_fun_smul`) `Q (a • x) = a * a * Q x` (`polar_...`) The map `polar Q := λ x y, Q (x + y) - Q x - Q y` is bilinear. They come with a scalar multiplication, `(a • Q) x = Q (a • x) = a * a * Q x`, and composition with linear maps `f`, `Q.comp f x = Q (f x)`. ## Main definitions * `quadratic_form.associated`: associated bilinear form * `quadratic_form.pos_def`: positive definite quadratic forms * `quadratic_form.anisotropic`: anisotropic quadratic forms * `quadratic_form.discr`: discriminant of a quadratic form ## Main statements * `quadratic_form.associated_left_inverse`, * `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has an inverse, there is a correspondence between quadratic forms and symmetric bilinear forms * `bilin_form.exists_orthogonal_basis`: There exists an orthogonal basis with respect to any nondegenerate, symmetric bilinear form `B`. ## Notation In this file, the variable `R` is used when a `ring` structure is sufficient and `R₁` is used when specifically a `comm_ring` is required. This allows us to keep `[module R M]` and `[module R₁ M]` assumptions in the variables without confusion between `*` from `ring` and `*` from `comm_ring`. The variable `S` is used when `R` itself has a `•` action. ## References * https://en.wikipedia.org/wiki/Quadratic_form * https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms ## Tags quadratic form, homogeneous polynomial, quadratic polynomial -/ universes u v w variables {S : Type*} variables {R : Type*} {M : Type*} [add_comm_group M] [ring R] variables {R₁ : Type*} [comm_ring R₁] namespace quadratic_form /-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`.d Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization -/ def polar (f : M → R) (x y : M) := f (x + y) - f x - f y lemma polar_add (f g : M → R) (x y : M) : polar (f + g) x y = polar f x y + polar g x y := by { simp only [polar, pi.add_apply], abel } lemma polar_neg (f : M → R) (x y : M) : polar (-f) x y = - polar f x y := by { simp only [polar, pi.neg_apply, sub_eq_add_neg, neg_add] } lemma polar_smul [monoid S] [distrib_mul_action S R] (f : M → R) (s : S) (x y : M) : polar (s • f) x y = s • polar f x y := by { simp only [polar, pi.smul_apply, smul_sub] } lemma polar_comm (f : M → R) (x y : M) : polar f x y = polar f y x := by rw [polar, polar, add_comm, sub_sub, sub_sub, add_comm (f x) (f y)] end quadratic_form variables [module R M] [module R₁ M] open quadratic_form /-- A quadratic form over a module. -/ structure quadratic_form (R : Type u) (M : Type v) [ring R] [add_comm_group M] [module R M] := (to_fun : M → R) (to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x) (polar_add_left' : ∀ (x x' y : M), polar to_fun (x + x') y = polar to_fun x y + polar to_fun x' y) (polar_smul_left' : ∀ (a : R) (x y : M), polar to_fun (a • x) y = a • polar to_fun x y) (polar_add_right' : ∀ (x y y' : M), polar to_fun x (y + y') = polar to_fun x y + polar to_fun x y') (polar_smul_right' : ∀ (a : R) (x y : M), polar to_fun x (a • y) = a • polar to_fun x y) namespace quadratic_form variables {Q : quadratic_form R M} instance : has_coe_to_fun (quadratic_form R M) := ⟨_, λ B, B.to_fun⟩ /-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/ @[simp] lemma to_fun_eq_apply : Q.to_fun = ⇑ Q := rfl lemma map_smul (a : R) (x : M) : Q (a • x) = a * a * Q x := Q.to_fun_smul a x lemma map_add_self (x : M) : Q (x + x) = 4 * Q x := by { rw [←one_smul R x, ←add_smul, map_smul], norm_num } @[simp] lemma map_zero : Q 0 = 0 := by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul] @[simp] lemma map_neg (x : M) : Q (-x) = Q x := by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul] lemma map_sub (x y : M) : Q (x - y) = Q (y - x) := by rw [←neg_sub, map_neg] @[simp] lemma polar_zero_left (y : M) : polar Q 0 y = 0 := by simp [polar] @[simp] lemma polar_add_left (x x' y : M) : polar Q (x + x') y = polar Q x y + polar Q x' y := Q.polar_add_left' x x' y @[simp] lemma polar_smul_left (a : R) (x y : M) : polar Q (a • x) y = a * polar Q x y := Q.polar_smul_left' a x y @[simp] lemma polar_neg_left (x y : M) : polar Q (-x) y = -polar Q x y := by rw [←neg_one_smul R x, polar_smul_left, neg_one_mul] @[simp] lemma polar_sub_left (x x' y : M) : polar Q (x - x') y = polar Q x y - polar Q x' y := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_left, polar_neg_left] @[simp] lemma polar_zero_right (y : M) : polar Q y 0 = 0 := by simp [polar] @[simp] lemma polar_add_right (x y y' : M) : polar Q x (y + y') = polar Q x y + polar Q x y' := Q.polar_add_right' x y y' @[simp] lemma polar_smul_right (a : R) (x y : M) : polar Q x (a • y) = a * polar Q x y := Q.polar_smul_right' a x y @[simp] lemma polar_neg_right (x y : M) : polar Q x (-y) = -polar Q x y := by rw [←neg_one_smul R y, polar_smul_right, neg_one_mul] @[simp] lemma polar_sub_right (x y y' : M) : polar Q x (y - y') = polar Q x y - polar Q x y' := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_right, polar_neg_right] @[simp] lemma polar_self (x : M) : polar Q x x = 2 * Q x := begin rw [polar, map_add_self, sub_sub, sub_eq_iff_eq_add, ←two_mul, ←two_mul, ←mul_assoc], norm_num end section of_tower variables [comm_semiring S] [algebra S R] [semimodule S M] [is_scalar_tower S R M] @[simp] lemma polar_smul_left_of_tower (a : S) (x y : M) : polar Q (a • x) y = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a x, polar_smul_left, algebra.smul_def] @[simp] lemma polar_smul_right_of_tower (a : S) (x y : M) : polar Q x (a • y) = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a y, polar_smul_right, algebra.smul_def] end of_tower variable {Q' : quadratic_form R M} @[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' := by { cases Q, cases Q', congr, funext, apply H } instance : has_zero (quadratic_form R M) := ⟨ { to_fun := λ x, 0, to_fun_smul := λ a x, by simp, polar_add_left' := λ x x' y, by simp [polar], polar_smul_left' := λ a x y, by simp [polar], polar_add_right' := λ x y y', by simp [polar], polar_smul_right' := λ a x y, by simp [polar] } ⟩ @[simp] lemma zero_apply (x : M) : (0 : quadratic_form R M) x = 0 := rfl instance : inhabited (quadratic_form R M) := ⟨0⟩ instance : has_add (quadratic_form R M) := ⟨ λ Q Q', { to_fun := Q + Q', to_fun_smul := λ a x, by simp only [pi.add_apply, map_smul, mul_add], polar_add_left' := λ x x' y, by simp only [polar_add, polar_add_left, add_assoc, add_left_comm], polar_smul_left' := λ a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_left], polar_add_right' := λ x y y', by simp only [polar_add, polar_add_right, add_assoc, add_left_comm], polar_smul_right' := λ a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_right] } ⟩ @[simp] lemma coe_fn_add (Q Q' : quadratic_form R M) : ⇑(Q + Q') = Q + Q' := rfl @[simp] lemma add_apply (Q Q' : quadratic_form R M) (x : M) : (Q + Q') x = Q x + Q' x := rfl instance : has_neg (quadratic_form R M) := ⟨ λ Q, { to_fun := -Q, to_fun_smul := λ a x, by simp only [pi.neg_apply, map_smul, mul_neg_eq_neg_mul_symm], polar_add_left' := λ x x' y, by simp only [polar_neg, polar_add_left, neg_add], polar_smul_left' := λ a x y, by simp only [polar_neg, polar_smul_left, mul_neg_eq_neg_mul_symm, smul_eq_mul], polar_add_right' := λ x y y', by simp only [polar_neg, polar_add_right, neg_add], polar_smul_right' := λ a x y, by simp only [polar_neg, polar_smul_right, mul_neg_eq_neg_mul_symm, smul_eq_mul] } ⟩ @[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl @[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl instance : add_comm_group (quadratic_form R M) := { add := (+), zero := 0, neg := has_neg.neg, add_comm := λ Q Q', by { ext, simp only [add_apply, add_comm] }, add_assoc := λ Q Q' Q'', by { ext, simp only [add_apply, add_assoc] }, add_left_neg := λ Q, by { ext, simp only [add_apply, neg_apply, zero_apply, add_left_neg] }, add_zero := λ Q, by { ext, simp only [zero_apply, add_apply, add_zero] }, zero_add := λ Q, by { ext, simp only [zero_apply, add_apply, zero_add] } } @[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := by simp [sub_eq_add_neg] @[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := by simp [sub_eq_add_neg] section has_scalar variables [comm_semiring S] [algebra S R] /-- `quadratic_form R M` inherits the scalar action from any algebra over `R`. When `R` is commutative, this provides an `R`-action via `algebra.id`. -/ instance : has_scalar S (quadratic_form R M) := ⟨ λ a Q, { to_fun := a • Q, to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, algebra.mul_smul_comm], polar_add_left' := λ x x' y, by simp only [polar_smul, polar_add_left, smul_add], polar_smul_left' := λ b x y, begin simp only [polar_smul, polar_smul_left, ←algebra.mul_smul_comm, smul_eq_mul], end, polar_add_right' := λ x y y', by simp only [polar_smul, polar_add_right, smul_add], polar_smul_right' := λ b x y, begin simp only [polar_smul, polar_smul_right, ←algebra.mul_smul_comm, smul_eq_mul], end } ⟩ @[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl @[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) : (a • Q) x = a • Q x := rfl instance : semimodule S (quadratic_form R M) := { mul_smul := λ a b Q, ext (λ x, by simp only [smul_apply, mul_left_comm, ←smul_eq_mul, smul_assoc]), one_smul := λ Q, ext (λ x, by simp), smul_add := λ a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] }, smul_zero := λ a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, zero_smul := λ Q, by { ext, simp only [zero_apply, smul_apply, zero_smul] }, add_smul := λ a b Q, by { ext, simp only [add_apply, smul_apply, add_smul] } } end has_scalar section comp variables {N : Type v} [add_comm_group N] [module R N] /-- Compose the quadratic form with a linear function. -/ def comp (Q : quadratic_form R N) (f : M →ₗ[R] N) : quadratic_form R M := { to_fun := λ x, Q (f x), to_fun_smul := λ a x, by simp only [map_smul, f.map_smul], polar_add_left' := λ x x' y, by convert polar_add_left (f x) (f x') (f y) using 1; simp only [polar, f.map_add], polar_smul_left' := λ a x y, by convert polar_smul_left a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul], polar_add_right' := λ x y y', by convert polar_add_right (f x) (f y) (f y') using 1; simp only [polar, f.map_add], polar_smul_right' := λ a x y, by convert polar_smul_right a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul] } @[simp] lemma comp_apply (Q : quadratic_form R N) (f : M →ₗ[R] N) (x : M) : (Q.comp f) x = Q (f x) := rfl end comp section comm_ring /-- Create a quadratic form in a commutative ring by proving only one side of the bilinearity. -/ def mk_left (f : M → R₁) (to_fun_smul : ∀ a x, f (a • x) = a * a * f x) (polar_add_left : ∀ x x' y, polar f (x + x') y = polar f x y + polar f x' y) (polar_smul_left : ∀ a x y, polar f (a • x) y = a * polar f x y) : quadratic_form R₁ M := { to_fun := f, to_fun_smul := to_fun_smul, polar_add_left' := polar_add_left, polar_smul_left' := polar_smul_left, polar_add_right' := λ x y y', by rw [polar_comm, polar_add_left, polar_comm f y x, polar_comm f y' x], polar_smul_right' := λ a x y, by rw [polar_comm, polar_smul_left, polar_comm f y x, smul_eq_mul] } /-- The product of linear forms is a quadratic form. -/ def lin_mul_lin (f g : M →ₗ[R₁] R₁) : quadratic_form R₁ M := mk_left (f * g) (λ a x, by { simp, ring }) (λ x x' y, by { simp [polar], ring }) (λ a x y, by { simp [polar], ring }) @[simp] lemma lin_mul_lin_apply (f g : M →ₗ[R₁] R₁) (x) : lin_mul_lin f g x = f x * g x := rfl @[simp] lemma add_lin_mul_lin (f g h : M →ₗ[R₁] R₁) : lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h := ext (λ x, add_mul _ _ _) @[simp] lemma lin_mul_lin_add (f g h : M →ₗ[R₁] R₁) : lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h := ext (λ x, mul_add _ _ _) variables {N : Type v} [add_comm_group N] [module R₁ N] @[simp] lemma lin_mul_lin_comp (f g : M →ₗ[R₁] R₁) (h : N →ₗ[R₁] M) : (lin_mul_lin f g).comp h = lin_mul_lin (f.comp h) (g.comp h) := rfl variables {n : Type*} /-- `proj i j` is the quadratic form mapping the vector `x : n → R₁` to `x i * x j` -/ def proj (i j : n) : quadratic_form R₁ (n → R₁) := lin_mul_lin (@linear_map.proj _ _ _ (λ _, R₁) _ _ i) (@linear_map.proj _ _ _ (λ _, R₁) _ _ j) @[simp] lemma proj_apply (i j : n) (x : n → R₁) : proj i j x = x i * x j := rfl end comm_ring end quadratic_form /-! ### Associated bilinear forms Over a commutative ring with an inverse of 2, the theory of quadratic forms is basically identical to that of symmetric bilinear forms. The map from quadratic forms to bilinear forms giving this identification is called the `associated` quadratic form. -/ variables {B : bilin_form R M} namespace bilin_form open quadratic_form lemma polar_to_quadratic_form (x y : M) : polar (λ x, B x x) x y = B x y + B y x := by simp [polar, add_left, add_right, sub_eq_add_neg _ (B y y), add_comm (B y x) _, add_assoc] /-- A bilinear form gives a quadratic form by applying the argument twice. -/ def to_quadratic_form (B : bilin_form R M) : quadratic_form R M := ⟨ λ x, B x x, λ a x, by simp [smul_left, smul_right, mul_assoc], λ x x' y, by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], λ a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add], λ x y y', by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], λ a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add] ⟩ @[simp] lemma to_quadratic_form_apply (B : bilin_form R M) (x : M) : B.to_quadratic_form x = B x x := rfl end bilin_form namespace quadratic_form open bilin_form sym_bilin_form section associated variables [invertible (2 : R₁)] {B₁ : bilin_form R₁ M} /-- `associated` is the linear map that sends a quadratic form to its associated symmetric bilinear form -/ def associated : quadratic_form R₁ M →ₗ[R₁] bilin_form R₁ M := { to_fun := λ Q, { bilin := λ x y, ⅟2 * polar Q x y, bilin_add_left := λ x y z, by rw [← mul_add, polar_add_left], bilin_smul_left := λ x y z, by rw [← mul_left_comm, polar_smul_left], bilin_add_right := λ x y z, by rw [← mul_add, polar_add_right], bilin_smul_right := λ x y z, by rw [← mul_left_comm, polar_smul_right] }, map_add' := λ Q Q', by { ext, simp [bilin_form.add_apply, polar_add, mul_add] }, map_smul' := λ a Q, by { ext, simp [bilin_form.smul_apply, polar_smul, mul_left_comm] } } variables {Q : quadratic_form R₁ M} @[simp] lemma associated_apply (x y : M) : associated Q x y = ⅟2 * (Q (x + y) - Q x - Q y) := rfl lemma associated_is_sym : is_sym Q.associated := λ x y, by simp only [associated_apply, add_comm, add_left_comm, sub_eq_add_neg] @[simp] lemma associated_comp {N : Type v} [add_comm_group N] [module R₁ N] (f : N →ₗ[R₁] M) : (Q.comp f).associated = Q.associated.comp f f := by { ext, simp } @[simp] lemma associated_lin_mul_lin (f g : M →ₗ[R₁] R₁) : (lin_mul_lin f g).associated = ⅟(2 : R₁) • (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) := by { ext, simp [bilin_form.add_apply, bilin_form.smul_apply], ring } lemma associated_to_quadratic_form (B : bilin_form R₁ M) (x y : M) : associated B.to_quadratic_form x y = ⅟2 * (B x y + B y x) := by simp [associated_apply, ←polar_to_quadratic_form, polar] lemma associated_left_inverse (h : is_sym B₁) : B₁.to_quadratic_form.associated = B₁ := bilin_form.ext $ λ x y, by rw [associated_to_quadratic_form, sym h x y, ←two_mul, ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_right_inverse : Q.associated.to_quadratic_form = Q := quadratic_form.ext $ λ x, calc Q.associated.to_quadratic_form x = ⅟2 * (Q x + Q x) : by simp [map_add_self, bit0, add_mul, add_assoc] ... = Q x : by rw [← two_mul (Q x), ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_eq_self_apply (x : M) : associated Q x x = Q x := begin rw [associated_apply, map_add_self], suffices : 2 * Q x * ⅟ 2 = Q x, { ring, assumption }, rw [mul_right_comm, mul_inv_of_self, one_mul], end -- Change to module over rings once #6585 is merged /-- There exists a non-null vector with respect to any quadratic form `Q` whose associated bilinear form is non-degenerate, i.e. there exists `x` such that `Q x ≠ 0`. -/ lemma exists_quadratic_form_neq_zero [nontrivial M] {Q : quadratic_form R₁ M} (hB₁ : Q.associated.nondegenerate) : ∃ x, Q x ≠ 0 := begin rw nondegenerate at hB₁, contrapose! hB₁, obtain ⟨x, hx⟩ := exists_ne (0 : M), refine ⟨x, λ y, _, hx⟩, have : Q = 0 := quadratic_form.ext hB₁, simpa [this, quadratic_form.associated_apply], end end associated section anisotropic /-- An anisotropic quadratic form is zero only on zero vectors. -/ def anisotropic (Q : quadratic_form R M) : Prop := ∀ x, Q x = 0 → x = 0 lemma not_anisotropic_iff_exists (Q : quadratic_form R M) : ¬anisotropic Q ↔ ∃ x ≠ 0, Q x = 0 := by simp only [anisotropic, not_forall, exists_prop, and_comm] end anisotropic section pos_def variables {R₂ : Type u} [ordered_ring R₂] [module R₂ M] {Q₂ : quadratic_form R₂ M} /-- A positive definite quadratic form is positive on nonzero vectors. -/ def pos_def (Q₂ : quadratic_form R₂ M) : Prop := ∀ x ≠ 0, 0 < Q₂ x lemma pos_def.smul {R} [linear_ordered_comm_ring R] [module R M] {Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a • Q) := λ x hx, mul_pos a_pos (h x hx) variables {n : Type*} lemma pos_def.add (Q Q' : quadratic_form R₂ M) (hQ : pos_def Q) (hQ' : pos_def Q') : pos_def (Q + Q') := λ x hx, add_pos (hQ x hx) (hQ' x hx) lemma lin_mul_lin_self_pos_def {R} [linear_ordered_comm_ring R] [module R M] (f : M →ₗ[R] R) (hf : linear_map.ker f = ⊥) : pos_def (lin_mul_lin f f) := λ x hx, mul_self_pos (λ h, hx (linear_map.ker_eq_bot.mp hf (by rw [h, linear_map.map_zero]))) end pos_def end quadratic_form section /-! ### Quadratic forms and matrices Connect quadratic forms and matrices, in order to explicitly compute with them. The convention is twos out, so there might be a factor 2⁻¹ in the entries of the matrix. The determinant of the matrix is the discriminant of the quadratic form. -/ variables {n : Type w} [fintype n] [decidable_eq n] /-- `M.to_quadratic_form` is the map `λ x, col x ⬝ M ⬝ row x` as a quadratic form. -/ def matrix.to_quadratic_form' (M : matrix n n R₁) : quadratic_form R₁ (n → R₁) := M.to_bilin'.to_quadratic_form variables [invertible (2 : R₁)] /-- A matrix representation of the quadratic form. -/ def quadratic_form.to_matrix' (Q : quadratic_form R₁ (n → R₁)) : matrix n n R₁ := Q.associated.to_matrix' open quadratic_form lemma quadratic_form.to_matrix'_smul (a : R₁) (Q : quadratic_form R₁ (n → R₁)) : (a • Q).to_matrix' = a • Q.to_matrix' := by simp only [to_matrix', linear_equiv.map_smul, linear_map.map_smul] end namespace quadratic_form variables {n : Type w} [fintype n] variables [decidable_eq n] [invertible (2 : R₁)] variables {m : Type w} [decidable_eq m] [fintype m] open_locale matrix @[simp] lemma to_matrix'_comp (Q : quadratic_form R₁ (m → R₁)) (f : (n → R₁) →ₗ[R₁] (m → R₁)) : (Q.comp f).to_matrix' = f.to_matrix'ᵀ ⬝ Q.to_matrix' ⬝ f.to_matrix' := by { ext, simp [to_matrix', bilin_form.to_matrix'_comp] } section discriminant variables {Q : quadratic_form R₁ (n → R₁)} /-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/ def discr (Q : quadratic_form R₁ (n → R₁)) : R₁ := Q.to_matrix'.det lemma discr_smul (a : R₁) : (a • Q).discr = a ^ fintype.card n * Q.discr := by simp only [discr, to_matrix'_smul, matrix.det_smul] lemma discr_comp (f : (n → R₁) →ₗ[R₁] (n → R₁)) : (Q.comp f).discr = f.to_matrix'.det * f.to_matrix'.det * Q.discr := by simp [discr, mul_left_comm, mul_comm] end discriminant end quadratic_form namespace quadratic_form variables {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₃] variables [module R M₁] [module R M₂] [module R M₃] /-- An isometry between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`, is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/ @[nolint has_inhabited_instance] structure isometry (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) extends M₁ ≃ₗ[R] M₂ := (map_app' : ∀ m, Q₂ (to_fun m) = Q₁ m) /-- Two quadratic forms over a ring `R` are equivalent if there exists an isometry between them: a linear equivalence that transforms one quadratic form into the other. -/ def equivalent (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) := nonempty (Q₁.isometry Q₂) namespace isometry variables {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} instance : has_coe (Q₁.isometry Q₂) (M₁ ≃ₗ[R] M₂) := ⟨isometry.to_linear_equiv⟩ instance : has_coe_to_fun (Q₁.isometry Q₂) := { F := λ _, M₁ → M₂, coe := λ f, ⇑(f : M₁ ≃ₗ[R] M₂) } @[simp] lemma map_app (f : Q₁.isometry Q₂) (m : M₁) : Q₂ (f m) = Q₁ m := f.map_app' m /-- The identity isometry from a quadratic form to itself. -/ @[refl] def refl (Q : quadratic_form R M) : Q.isometry Q := { map_app' := λ m, rfl, .. linear_equiv.refl R M } /-- The inverse isometry of an isometry between two quadratic forms. -/ @[symm] def symm (f : Q₁.isometry Q₂) : Q₂.isometry Q₁ := { map_app' := by { intro m, rw ← f.map_app, congr, exact f.to_linear_equiv.apply_symm_apply m }, .. (f : M₁ ≃ₗ[R] M₂).symm } /-- The composition of two isometries between quadratic forms. -/ @[trans] def trans (f : Q₁.isometry Q₂) (g : Q₂.isometry Q₃) : Q₁.isometry Q₃ := { map_app' := by { intro m, rw [← f.map_app, ← g.map_app], refl }, .. (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) } end isometry namespace equivalent variables {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} @[refl] lemma refl (Q : quadratic_form R M) : Q.equivalent Q := ⟨isometry.refl Q⟩ @[symm] lemma symm (h : Q₁.equivalent Q₂) : Q₂.equivalent Q₁ := h.elim $ λ f, ⟨f.symm⟩ @[trans] lemma trans (h : Q₁.equivalent Q₂) (h' : Q₂.equivalent Q₃) : Q₁.equivalent Q₃ := h'.elim $ h.elim $ λ f g, ⟨f.trans g⟩ end equivalent end quadratic_form namespace bilin_form /-- There exists a non-null vector with respect to any symmetric, nondegenerate bilinear form `B` on a nontrivial module `M` over the commring `R` with invertible `2`, i.e. there exists some `x : M` such that `B x x ≠ 0`. -/ lemma exists_bilin_form_self_neq_zero [htwo : invertible (2 : R₁)] [nontrivial M] {B : bilin_form R₁ M} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ x, ¬ B.is_ortho x x := begin have : B.to_quadratic_form.associated.nondegenerate, refine (quadratic_form.associated_left_inverse hB₂).symm ▸ hB₁, obtain ⟨x, hx⟩ := quadratic_form.exists_quadratic_form_neq_zero this, refine ⟨x, λ h, hx (B.to_quadratic_form_apply x ▸ h)⟩, end open finite_dimensional variables {V : Type u} {K : Type v} [field K] [add_comm_group V] [vector_space K V] variable [finite_dimensional K V] -- We start proving that symmetric nondegenerate bilinear forms are diagonalisable, or equivalently -- there exists a orthogonal basis with respect to any symmetric nondegenerate bilinear form. lemma exists_orthogonal_basis' [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ v : fin (findim K V) → V, B.is_Ortho v ∧ is_basis K v ∧ ∀ i, B (v i) (v i) ≠ 0 := begin tactic.unfreeze_local_instances, induction hd : findim K V with d ih generalizing V, { exact ⟨λ _, 0, λ _ _ _, zero_left _, is_basis_of_findim_zero' hd, fin.elim0⟩ }, { haveI := findim_pos_iff.1 (hd.symm ▸ nat.succ_pos d : 0 < findim K V), cases exists_bilin_form_self_neq_zero hB₁ hB₂ with x hx, { have hd' := hd, rw [← submodule.findim_add_eq_of_is_compl (is_compl_span_singleton_orthogonal hx).symm, findim_span_singleton (ne_zero_of_not_is_ortho_self x hx)] at hd, rcases @ih (B.orthogonal $ K ∙ x) _ _ _ (B.restrict _) (B.restrict_orthogonal_span_singleton_nondegenerate hB₁ hB₂ hx) (B.restrict_sym hB₂ _) (nat.succ.inj hd) with ⟨v', hv₁, hv₂, hv₃⟩, refine ⟨λ i, if h : i ≠ 0 then coe (v' (i.pred h)) else x, λ i j hij, _, _, _⟩, { by_cases hi : i = 0, { subst i, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw [dif_neg hij.symm, is_ortho, hB₂], exact (v' (j.pred hij.symm)).2 _ (submodule.mem_span_singleton_self x) }, by_cases hj : j = 0, { subst j, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw dif_neg hi, exact (v' (i.pred hi)).2 _ (submodule.mem_span_singleton_self x) }, { simp_rw [dif_pos hi, dif_pos hj], rw [is_ortho, hB₂], exact hv₁ (j.pred hj) (i.pred hi) (by simpa using hij.symm) } }, { refine is_basis_of_linear_independent_of_card_eq_findim (@linear_independent_of_is_Ortho _ _ _ _ _ _ B _ _ _) (by rw [hd', fintype.card_fin]), { intros i j hij, by_cases hi : i = 0, { subst hi, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw [dif_neg hij.symm, is_ortho, hB₂], exact (v' (j.pred hij.symm)).2 _ (submodule.mem_span_singleton_self x) }, by_cases hj : j = 0, { subst j, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw dif_neg hi, exact (v' (i.pred hi)).2 _ (submodule.mem_span_singleton_self x) }, { simp_rw [dif_pos hi, dif_pos hj], rw [is_ortho, hB₂], exact hv₁ (j.pred hj) (i.pred hi) (by simpa using hij.symm) } }, { intro i, by_cases hi : i ≠ 0, { rw dif_pos hi, exact hv₃ (i.pred hi) }, { rw dif_neg hi, exact hx } } }, { intro i, by_cases hi : i ≠ 0, { rw dif_pos hi, exact hv₃ (i.pred hi) }, { rw dif_neg hi, exact hx } } } } end . /-- Given a nondegenerate symmetric bilinear form `B` on some vector space `V` over the field `K` with invertible `2`, there exists an orthogonal basis with respect to `B`. -/ theorem exists_orthogonal_basis [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ v : fin (findim K V) → V, B.is_Ortho v ∧ is_basis K v := let ⟨v, hv₁, hv₂, _⟩ := exists_orthogonal_basis' hB₁ hB₂ in ⟨v, hv₁, hv₂⟩ end bilin_form
82e4505f043ac227fe170c861a29c892e26b9332
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/typeclass_metas_internal_goals4.lean
25b7e188cdeb6cf77c3e6a9c6e563f5f18273a64
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
636
lean
class Foo (α β γ : Type) := (u:Unit) class Bar (α β γ : Type) := (u:Unit) class Top := (u:Unit) instance FooBarToTop (α β γ : Type) [Foo α β γ] [Bar α β γ] : Top := {u:=()} instance Foo₁ (β γ : Type) : Foo Unit β γ := {u:=()} instance Foo₂ (α γ : Type) : Foo α Unit γ := {u:=()} instance Foo₃ (α β : Type) : Foo α β Unit := {u:=()} instance Foo₁₂ (γ : Type) : Foo Unit Nat γ := {u:=()} instance Foo₂₃ (α : Type) : Foo α Unit Nat := {u:=()} instance Foo₃₁ (β : Type) : Foo Nat β Unit := {u:=()} instance Bar0 : Bar Unit Int (List Int) := {u:=()} set_option pp.all true #synth Top
544899b7a343bac2dc0e60404eca60e4b0532147
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/compiler/float_cases_bug.lean
af8e76fbe897376506b56352a4c62c3e4187c6f7
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
959
lean
inductive Term : Type | const : Nat -> Term | app : List Term -> Term namespace Term instance : Inhabited Term := ⟨Term.const 0⟩ partial def hasToString : Term -> String | const n => "CONST(" ++ toString n ++ ")" | app ts => "APP" instance : HasToString Term := ⟨hasToString⟩ end Term open Term structure MyState : Type := (ts : List Term) def emit (t : Term) : StateM MyState Unit := modify (λ ms => ⟨t::ms.ts⟩) partial def foo : MyState -> Term -> Term -> List Term | ms₀, t, u => let stateT : StateM MyState Unit := do { match t with | const _ => pure () | app _ => emit (const 1) *> pure () ; match t, u with | app _, app _ => emit (app []) *> pure () | _, _ => pure () ; match t, u with | app _, app _ => emit (app []) *> pure () | _, _ => emit (const 2) *> pure () } ; (stateT.run ⟨[]⟩).2.ts.reverse def main : IO Unit := IO.println $ foo ⟨[]⟩ (app []) (app [])
9342b51fe492e211d5b3c50e118fb8fce9f0382e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/valuation/basic.lean
d327f50e5ac174e7bad67c76e073ffa2b7072320
[ "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
24,963
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Johan Commelin, Patrick Massot -/ import algebra.order.with_zero import algebra.punit_instances import ring_theory.ideal.operations /-! # The basics of valuation theory. The basic theory of valuations (non-archimedean norms) on a commutative ring, following T. Wedhorn's unpublished notes “Adic Spaces” ([wedhorn_adic]). The definition of a valuation we use here is Definition 1.22 of [wedhorn_adic]. A valuation on a ring `R` is a monoid homomorphism `v` to a linearly ordered commutative monoid with zero, that in addition satisfies the following two axioms: * `v 0 = 0` * `∀ x y, v (x + y) ≤ max (v x) (v y)` `valuation R Γ₀`is the type of valuations `R → Γ₀`, with a coercion to the underlying function. If `v` is a valuation from `R` to `Γ₀` then the induced group homomorphism `units(R) → Γ₀` is called `unit_map v`. The equivalence "relation" `is_equiv v₁ v₂ : Prop` defined in 1.27 of [wedhorn_adic] is not strictly speaking a relation, because `v₁ : valuation R Γ₁` and `v₂ : valuation R Γ₂` might not have the same type. This corresponds in ZFC to the set-theoretic difficulty that the class of all valuations (as `Γ₀` varies) on a ring `R` is not a set. The "relation" is however reflexive, symmetric and transitive in the obvious sense. Note that we use 1.27(iii) of [wedhorn_adic] as the definition of equivalence. The support of a valuation `v : valuation R Γ₀` is `supp v`. If `J` is an ideal of `R` with `h : J ⊆ supp v` then the induced valuation on R / J = `ideal.quotient J` is `on_quot v h`. ## Main definitions * `valuation R Γ₀`, the type of valuations on `R` with values in `Γ₀` * `valuation.is_equiv`, the heterogeneous equivalence relation on valuations * `valuation.supp`, the support of a valuation * `add_valuation R Γ₀`, the type of additive valuations on `R` with values in a linearly ordered additive commutative group with a top element, `Γ₀`. ## Implementation Details `add_valuation R Γ₀` is implemented as `valuation R (multiplicative (order_dual Γ₀))`. -/ open_locale classical big_operators noncomputable theory open function ideal variables {R : Type*} -- This will be a ring, assumed commutative in some sections section variables (R) (Γ₀ : Type*) [linear_ordered_comm_monoid_with_zero Γ₀] [ring R] /-- The type of `Γ₀`-valued valuations on `R`. -/ @[nolint has_inhabited_instance] structure valuation extends R →*₀ Γ₀ := (map_add' : ∀ x y, to_fun (x + y) ≤ max (to_fun x) (to_fun y)) /-- The `monoid_with_zero_hom` underlying a valuation. -/ add_decl_doc valuation.to_monoid_with_zero_hom end namespace valuation variables {Γ₀ : Type*} variables {Γ'₀ : Type*} variables {Γ''₀ : Type*} [linear_ordered_comm_monoid_with_zero Γ''₀] section basic variables (R) (Γ₀) [ring R] section monoid variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] /-- A valuation is coerced to the underlying function `R → Γ₀`. -/ instance : has_coe_to_fun (valuation R Γ₀) (λ _, R → Γ₀) := { coe := λ v, v.to_monoid_with_zero_hom.to_fun } /-- A valuation is coerced to a monoid morphism R → Γ₀. -/ instance : has_coe (valuation R Γ₀) (R →*₀ Γ₀) := ⟨valuation.to_monoid_with_zero_hom⟩ variables {R} {Γ₀} (v : valuation R Γ₀) {x y z : R} @[simp, norm_cast] lemma coe_coe : ((v : R →*₀ Γ₀) : R → Γ₀) = v := rfl @[simp] lemma map_zero : v 0 = 0 := v.map_zero' @[simp] lemma map_one : v 1 = 1 := v.map_one' @[simp] lemma map_mul : ∀ x y, v (x * y) = v x * v y := v.map_mul' @[simp] lemma map_add : ∀ x y, v (x + y) ≤ max (v x) (v y) := v.map_add' lemma map_add_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x + y) ≤ g := le_trans (v.map_add x y) $ max_le hx hy lemma map_add_lt {x y g} (hx : v x < g) (hy : v y < g) : v (x + y) < g := lt_of_le_of_lt (v.map_add x y) $ max_lt hx hy lemma map_sum_le {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, v (f i) ≤ g) : v (∑ i in s, f i) ≤ g := begin refine finset.induction_on s (λ _, trans_rel_right (≤) v.map_zero zero_le') (λ a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_le hf.1 (ih hf.2) end lemma map_sum_lt {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ 0) (hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g := begin refine finset.induction_on s (λ _, trans_rel_right (<) v.map_zero (zero_lt_iff.2 hg)) (λ a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_lt hf.1 (ih hf.2) end lemma map_sum_lt' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : 0 < g) (hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g := v.map_sum_lt (ne_of_gt hg) hf @[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = (v x)^n := v.to_monoid_with_zero_hom.to_monoid_hom.map_pow @[ext] lemma ext {v₁ v₂ : valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ := by { rcases v₁ with ⟨⟨⟩⟩, rcases v₂ with ⟨⟨⟩⟩, congr, funext r, exact h r } lemma ext_iff {v₁ v₂ : valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r := ⟨λ h r, congr_arg _ h, ext⟩ -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder : preorder R := preorder.lift v /-- If `v` is a valuation on a division ring then `v(x) = 0` iff `x = 0`. -/ @[simp] lemma zero_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x = 0 ↔ x = 0 := v.to_monoid_with_zero_hom.map_eq_zero lemma ne_zero_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x ≠ 0 ↔ x ≠ 0 := v.to_monoid_with_zero_hom.map_ne_zero theorem unit_map_eq (u : Rˣ) : (units.map (v : R →* Γ₀) u : Γ₀) = v u := rfl /-- A ring homomorphism `S → R` induces a map `valuation R Γ₀ → valuation S Γ₀`. -/ def comap {S : Type*} [ring S] (f : S →+* R) (v : valuation R Γ₀) : valuation S Γ₀ := { to_fun := v ∘ f, map_add' := λ x y, by simp only [comp_app, map_add, f.map_add], .. v.to_monoid_with_zero_hom.comp f.to_monoid_with_zero_hom, } @[simp] lemma comap_id : v.comap (ring_hom.id R) = v := ext $ λ r, rfl lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) : v.comap (g.comp f) = (v.comap g).comap f := ext $ λ r, rfl /-- A `≤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map `valuation R Γ₀ → valuation R Γ'₀`. -/ def map (f : Γ₀ →*₀ Γ'₀) (hf : monotone f) (v : valuation R Γ₀) : valuation R Γ'₀ := { to_fun := f ∘ v, map_add' := λ r s, calc f (v (r + s)) ≤ f (max (v r) (v s)) : hf (v.map_add r s) ... = max (f (v r)) (f (v s)) : hf.map_max, .. monoid_with_zero_hom.comp f v.to_monoid_with_zero_hom } /-- Two valuations on `R` are defined to be equivalent if they induce the same preorder on `R`. -/ def is_equiv (v₁ : valuation R Γ₀) (v₂ : valuation R Γ'₀) : Prop := ∀ r s, v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s end monoid section group variables [linear_ordered_comm_group_with_zero Γ₀] {R} {Γ₀} (v : valuation R Γ₀) {x y z : R} @[simp] lemma map_inv {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x⁻¹ = (v x)⁻¹ := v.to_monoid_with_zero_hom.map_inv x @[simp] lemma map_zpow {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} {n : ℤ} : v (x^n) = (v x)^n := v.to_monoid_with_zero_hom.map_zpow x n lemma map_units_inv (x : Rˣ) : v (x⁻¹ : Rˣ) = (v x)⁻¹ := v.to_monoid_with_zero_hom.to_monoid_hom.map_units_inv x @[simp] lemma map_neg (x : R) : v (-x) = v x := v.to_monoid_with_zero_hom.to_monoid_hom.map_neg x lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) := v.to_monoid_with_zero_hom.to_monoid_hom.map_sub_swap x y lemma map_sub (x y : R) : v (x - y) ≤ max (v x) (v y) := calc v (x - y) = v (x + -y) : by rw [sub_eq_add_neg] ... ≤ max (v x) (v $ -y) : v.map_add _ _ ... = max (v x) (v y) : by rw map_neg lemma map_sub_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x - y) ≤ g := begin rw sub_eq_add_neg, exact v.map_add_le hx (le_trans (le_of_eq (v.map_neg y)) hy) end lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = max (v x) (v y) := begin suffices : ¬v (x + y) < max (v x) (v y), from or_iff_not_imp_right.1 (le_iff_eq_or_lt.1 (v.map_add x y)) this, intro h', wlog vyx : v y < v x using x y, { apply lt_or_gt_of_ne h.symm }, { rw max_eq_left_of_lt vyx at h', apply lt_irrefl (v x), calc v x = v ((x+y) - y) : by simp ... ≤ max (v $ x + y) (v y) : map_sub _ _ _ ... < v x : max_lt h' vyx }, { apply this h.symm, rwa [add_comm, max_comm] at h' } end lemma map_eq_of_sub_lt (h : v (y - x) < v x) : v y = v x := begin have := valuation.map_add_of_distinct_val v (ne_of_gt h).symm, rw max_eq_right (le_of_lt h) at this, simpa using this end /-- The subgroup of elements whose valuation is less than a certain unit.-/ def lt_add_subgroup (v : valuation R Γ₀) (γ : Γ₀ˣ) : add_subgroup R := { carrier := {x | v x < γ}, zero_mem' := by { have h := units.ne_zero γ, contrapose! h, simpa using h }, add_mem' := λ x y x_in y_in, lt_of_le_of_lt (v.map_add x y) (max_lt x_in y_in), neg_mem' := λ x x_in, by rwa [set.mem_set_of_eq, map_neg] } end group end basic -- end of section namespace is_equiv variables [ring R] variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] variables {v : valuation R Γ₀} variables {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} {v₃ : valuation R Γ''₀} @[refl] lemma refl : v.is_equiv v := λ _ _, iff.refl _ @[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ := λ _ _, iff.symm (h _ _) @[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ := λ _ _, iff.trans (h₁₂ _ _) (h₂₃ _ _) lemma of_eq {v' : valuation R Γ₀} (h : v = v') : v.is_equiv v' := by { subst h } lemma map {v' : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀) (hf : monotone f) (inf : injective f) (h : v.is_equiv v') : (v.map f hf).is_equiv (v'.map f hf) := let H : strict_mono f := hf.strict_mono_of_injective inf in λ r s, calc f (v r) ≤ f (v s) ↔ v r ≤ v s : by rw H.le_iff_le ... ↔ v' r ≤ v' s : h r s ... ↔ f (v' r) ≤ f (v' s) : by rw H.le_iff_le /-- `comap` preserves equivalence. -/ lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) : (v₁.comap f).is_equiv (v₂.comap f) := λ r s, h (f r) (f s) lemma val_eq (h : v₁.is_equiv v₂) {r s : R} : v₁ r = v₁ s ↔ v₂ r = v₂ s := by simpa only [le_antisymm_iff] using and_congr (h r s) (h s r) lemma ne_zero (h : v₁.is_equiv v₂) {r : R} : v₁ r ≠ 0 ↔ v₂ r ≠ 0 := begin have : v₁ r ≠ v₁ 0 ↔ v₂ r ≠ v₂ 0 := not_iff_not_of_iff h.val_eq, rwa [v₁.map_zero, v₂.map_zero] at this, end end is_equiv -- end of namespace section lemma is_equiv_of_map_strict_mono [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] [ring R] {v : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀) (H : strict_mono f) : is_equiv (v.map f (H.monotone)) v := λ x y, ⟨H.le_iff_le.mp, λ h, H.monotone h⟩ lemma is_equiv_of_val_le_one [linear_ordered_comm_group_with_zero Γ₀] [linear_ordered_comm_group_with_zero Γ'₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) (v' : valuation K Γ'₀) (h : ∀ {x:K}, v x ≤ 1 ↔ v' x ≤ 1) : v.is_equiv v' := begin intros x y, by_cases hy : y = 0, { simp [hy, zero_iff], }, rw show y = 1 * y, by rw one_mul, rw [← (inv_mul_cancel_right₀ hy x)], iterate 2 {rw [v.map_mul _ y, v'.map_mul _ y]}, rw [v.map_one, v'.map_one], split; intro H, { apply mul_le_mul_right', replace hy := v.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h at H, }, { apply mul_le_mul_right', replace hy := v'.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h, }, end end section supp variables [comm_ring R] variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] variables (v : valuation R Γ₀) /-- The support of a valuation `v : R → Γ₀` is the ideal of `R` where `v` vanishes. -/ def supp : ideal R := { carrier := {x | v x = 0}, zero_mem' := map_zero v, add_mem' := λ x y hx hy, le_zero_iff.mp $ calc v (x + y) ≤ max (v x) (v y) : v.map_add x y ... ≤ 0 : max_le (le_zero_iff.mpr hx) (le_zero_iff.mpr hy), smul_mem' := λ c x hx, calc v (c * x) = v c * v x : map_mul v c x ... = v c * 0 : congr_arg _ hx ... = 0 : mul_zero _ } @[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = 0 := iff.rfl -- @[simp] lemma mem_supp_iff' (x : R) : x ∈ (supp v : set R) ↔ v x = 0 := iff.rfl /-- The support of a valuation is a prime ideal. -/ instance [nontrivial Γ₀] [no_zero_divisors Γ₀] : ideal.is_prime (supp v) := ⟨λ (h : v.supp = ⊤), one_ne_zero $ show (1 : Γ₀) = 0, from calc 1 = v 1 : v.map_one.symm ... = 0 : show (1:R) ∈ supp v, by { rw h, trivial }, λ x y hxy, begin show v x = 0 ∨ v y = 0, change v (x * y) = 0 at hxy, rw [v.map_mul x y] at hxy, exact eq_zero_or_eq_zero_of_mul_eq_zero hxy end⟩ lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a := begin have aux : ∀ a s, v s = 0 → v (a + s) ≤ v a, { intros a' s' h', refine le_trans (v.map_add a' s') (max_le le_rfl _), simp [h'], }, apply le_antisymm (aux a s h), calc v a = v (a + s + -s) : by simp ... ≤ v (a + s) : aux (a + s) (-s) (by rwa ←ideal.neg_mem_iff at h) end /-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def on_quot_val {J : ideal R} (hJ : J ≤ supp v) : R ⧸ J → Γ₀ := λ q, quotient.lift_on' q v $ λ a b h, calc v a = v (b + (a - b)) : by simp ... = v b : v.map_add_supp b (hJ h) /-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/ def on_quot {J : ideal R} (hJ : J ≤ supp v) : valuation (R ⧸ J) Γ₀ := { to_fun := v.on_quot_val hJ, map_zero' := v.map_zero, map_one' := v.map_one, map_mul' := λ xbar ybar, quotient.ind₂' v.map_mul xbar ybar, map_add' := λ xbar ybar, quotient.ind₂' v.map_add xbar ybar } @[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) : (v.on_quot hJ).comap (ideal.quotient.mk J) = v := ext $ λ r, begin refine @quotient.lift_on_mk _ _ (J.quotient_rel) v (λ a b h, _) _, calc v a = v (b + (a - b)) : by simp ... = v b : v.map_add_supp b (hJ h) end lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) : supp (v.comap f) = ideal.comap f v.supp := ideal.ext $ λ x, begin rw [mem_supp_iff, ideal.mem_comap, mem_supp_iff], refl, end lemma self_le_supp_comap (J : ideal R) (v : valuation (R ⧸ J) Γ₀) : J ≤ (v.comap (ideal.quotient.mk J)).supp := by { rw [comap_supp, ← ideal.map_le_iff_le_comap], simp } @[simp] lemma comap_on_quot_eq (J : ideal R) (v : valuation (R ⧸ J) Γ₀) : (v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v := ext $ by { rintro ⟨x⟩, refl } /-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/ lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) : supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) := begin apply le_antisymm, { rintro ⟨x⟩ hx, apply ideal.subset_span, exact ⟨x, hx, rfl⟩ }, { rw ideal.map_le_iff_le_comap, intros x hx, exact hx } end lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 := by { rw supp_quot, exact ideal.map_quotient_self _ } end supp -- end of section end valuation section add_monoid variables (R) [ring R] (Γ₀ : Type*) [linear_ordered_add_comm_monoid_with_top Γ₀] /-- The type of `Γ₀`-valued additive valuations on `R`. -/ @[nolint has_inhabited_instance] def add_valuation := valuation R (multiplicative (order_dual Γ₀)) end add_monoid namespace add_valuation variables {Γ₀ : Type*} {Γ'₀ : Type*} section basic section monoid variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables (R) (Γ₀) [ring R] /-- A valuation is coerced to the underlying function `R → Γ₀`. -/ instance : has_coe_to_fun (add_valuation R Γ₀) (λ _, R → Γ₀) := { coe := λ v, v.to_monoid_with_zero_hom.to_fun } variables {R} {Γ₀} (v : add_valuation R Γ₀) {x y z : R} section variables (f : R → Γ₀) (h0 : f 0 = ⊤) (h1 : f 1 = 0) variables (hadd : ∀ x y, min (f x) (f y) ≤ f (x + y)) (hmul : ∀ x y, f (x * y) = f x + f y) /-- An alternate constructor of `add_valuation`, that doesn't reference `multiplicative (order_dual Γ₀)` -/ def of : add_valuation R Γ₀ := { to_fun := f, map_one' := h1, map_zero' := h0, map_add' := hadd, map_mul' := hmul } variables {h0} {h1} {hadd} {hmul} {r : R} @[simp] theorem of_apply : (of f h0 h1 hadd hmul) r = f r := rfl end @[simp] lemma map_zero : v 0 = ⊤ := v.map_zero @[simp] lemma map_one : v 1 = 0 := v.map_one @[simp] lemma map_mul : ∀ x y, v (x * y) = v x + v y := v.map_mul @[simp] lemma map_add : ∀ x y, min (v x) (v y) ≤ v (x + y) := v.map_add lemma map_le_add {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x + y) := v.map_add_le hx hy lemma map_lt_add {x y g} (hx : g < v x) (hy : g < v y) : g < v (x + y) := v.map_add_lt hx hy lemma map_le_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, g ≤ v (f i)) : g ≤ v (∑ i in s, f i) := v.map_sum_le hf lemma map_lt_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ ⊤) (hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt hg hf lemma map_lt_sum' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g < ⊤) (hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt' hg hf @[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = n • (v x) := v.map_pow @[ext] lemma ext {v₁ v₂ : add_valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ := valuation.ext h lemma ext_iff {v₁ v₂ : add_valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r := valuation.ext_iff -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder : preorder R := preorder.lift v /-- If `v` is an additive valuation on a division ring then `v(x) = ⊤` iff `x = 0`. -/ @[simp] lemma top_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x = ⊤ ↔ x = 0 := v.zero_iff lemma ne_top_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x ≠ ⊤ ↔ x ≠ 0 := v.ne_zero_iff /-- A ring homomorphism `S → R` induces a map `add_valuation R Γ₀ → add_valuation S Γ₀`. -/ def comap {S : Type*} [ring S] (f : S →+* R) (v : add_valuation R Γ₀) : add_valuation S Γ₀ := v.comap f @[simp] lemma comap_id : v.comap (ring_hom.id R) = v := v.comap_id lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) : v.comap (g.comp f) = (v.comap g).comap f := v.comap_comp f g /-- A `≤`-preserving, `⊤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map `add_valuation R Γ₀ → add_valuation R Γ'₀`. -/ def map (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f) (v : add_valuation R Γ₀) : add_valuation R Γ'₀ := v.map { to_fun := f, map_mul' := f.map_add, map_one' := f.map_zero, map_zero' := ht } (λ x y h, hf h) /-- Two additive valuations on `R` are defined to be equivalent if they induce the same preorder on `R`. -/ def is_equiv (v₁ : add_valuation R Γ₀) (v₂ : add_valuation R Γ'₀) : Prop := v₁.is_equiv v₂ end monoid section group variables [linear_ordered_add_comm_group_with_top Γ₀] [ring R] (v : add_valuation R Γ₀) {x y z : R} @[simp] lemma map_inv {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x⁻¹ = - (v x) := v.map_inv lemma map_units_inv (x : Rˣ) : v (x⁻¹ : Rˣ) = - (v x) := v.map_units_inv x @[simp] lemma map_neg (x : R) : v (-x) = v x := v.map_neg x lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) := v.map_sub_swap x y lemma map_sub (x y : R) : min (v x) (v y) ≤ v (x - y) := v.map_sub x y lemma map_le_sub {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x - y) := v.map_sub_le hx hy lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = min (v x) (v y) := v.map_add_of_distinct_val h lemma map_eq_of_lt_sub (h : v x < v (y - x)) : v y = v x := v.map_eq_of_sub_lt h end group end basic namespace is_equiv variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables [ring R] variables {Γ''₀ : Type*} [linear_ordered_add_comm_monoid_with_top Γ''₀] variables {v : add_valuation R Γ₀} variables {v₁ : add_valuation R Γ₀} {v₂ : add_valuation R Γ'₀} {v₃ : add_valuation R Γ''₀} @[refl] lemma refl : v.is_equiv v := valuation.is_equiv.refl @[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ := h.symm @[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ := h₁₂.trans h₂₃ lemma of_eq {v' : add_valuation R Γ₀} (h : v = v') : v.is_equiv v' := valuation.is_equiv.of_eq h lemma map {v' : add_valuation R Γ₀} (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f) (inf : injective f) (h : v.is_equiv v') : (v.map f ht hf).is_equiv (v'.map f ht hf) := h.map { to_fun := f, map_mul' := f.map_add, map_one' := f.map_zero, map_zero' := ht } (λ x y h, hf h) inf /-- `comap` preserves equivalence. -/ lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) : (v₁.comap f).is_equiv (v₂.comap f) := h.comap f lemma val_eq (h : v₁.is_equiv v₂) {r s : R} : v₁ r = v₁ s ↔ v₂ r = v₂ s := h.val_eq lemma ne_top (h : v₁.is_equiv v₂) {r : R} : v₁ r ≠ ⊤ ↔ v₂ r ≠ ⊤ := h.ne_zero end is_equiv section supp variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables [comm_ring R] variables (v : add_valuation R Γ₀) /-- The support of an additive valuation `v : R → Γ₀` is the ideal of `R` where `v x = ⊤` -/ def supp : ideal R := v.supp @[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = ⊤ := v.mem_supp_iff x lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a := v.map_add_supp a h /-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def on_quot_val {J : ideal R} (hJ : J ≤ supp v) : (R ⧸ J) → Γ₀ := v.on_quot_val hJ /-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/ def on_quot {J : ideal R} (hJ : J ≤ supp v) : add_valuation (R ⧸ J) Γ₀ := v.on_quot hJ @[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) : (v.on_quot hJ).comap (ideal.quotient.mk J) = v := v.on_quot_comap_eq hJ lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) : supp (v.comap f) = ideal.comap f v.supp := v.comap_supp f lemma self_le_supp_comap (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) : J ≤ (v.comap (ideal.quotient.mk J)).supp := v.self_le_supp_comap J @[simp] lemma comap_on_quot_eq (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) : (v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v := v.comap_on_quot_eq J /-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/ lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) : supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) := v.supp_quot hJ lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 := v.supp_quot_supp end supp -- end of section attribute [irreducible] add_valuation end add_valuation
7d6b30b56d6600be9c8fd2bc63baf486c6c17e71
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world5/level4.lean
dc4b0c28b8989e58aba613f953bf8af3dba97d56
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
2,040
lean
/- Tactic : apply ## Summary If `h : P → Q` is a hypothesis, and the goal is `⊢ Q` then `apply h` changes the goal to `⊢ P`. ## Details If you have a function `h : P → Q` and your goal is `⊢ Q` then `apply h` changes the goal to `⊢ P`. The logic is simple: if you are trying to create a term of type `Q`, but `h` is a function which turns terms of type `P` into terms of type `Q`, then it will suffice to construct a term of type `P`. A mathematician might say: "we need to construct an element of $Q$, but we have a function $h:P\to Q$ so it suffices to construct an element of $P$". Or alternatively "we need to prove $Q$, but we have a proof $h$ that $P\implies Q$ so it suffices to prove $P$". -/ /- # Function world. ## Level 4: the `apply` tactic. Let's do the same level again: ![diagram](https://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_game_images/function_diag.jpg) We are given $p \in P$ and our goal is to find an element of $U$, or in other words to find a path through the maze that links $P$ to $U$. In level 3 we solved this by using `have`s to move forward, from $P$ to $Q$ to $T$ to $U$. Using the `apply` tactic we can instead construct the path backwards, moving from $U$ to $T$ to $Q$ to $P$. Our goal is to construct an element of the set $U$. But $l:T\to U$ is a function, so it would suffice to construct an element of $T$. Tell Lean this by starting the proof below with `apply l,` and notice that our assumptions don't change but *the goal changes* from `⊢ U` to `⊢ T`. Keep `apply`ing functions until your goal is `P`, and try not to get lost! Now solve this goal with `exact p`. Note: you will need to learn the difference between `exact p` (which works) and `exact P` (which doesn't, because $P$ is not an element of $P$). -/ /- Definition Given an element of $P$ we can define an element of $U$. -/ example (P Q R S T U: Type) (p : P) (h : P → Q) (i : Q → R) (j : Q → T) (k : S → T) (l : T → U) : U := begin apply l, apply j, apply h, exact p, end
a98ce978e3f6cef1adf15505ba95cab2b689525b
037dba89703a79cd4a4aec5e959818147f97635d
/src/2020/logic/questions.lean
b8eda0bc3a0f386e0d3b374ec2c169ac17bd99b1
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
14,290
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard -/ import tactic /-! # Logic A Lean companion to the "Logic" part of the intro module. We develop the basic theory of the five symbols →, ¬, ∧, ↔, ∨ # Background It is hard to ask you difficult questions about the basic theory of these logical operators, because every question can be proved by "check all the cases". However, there is this cool theorem, that says that if a theorem in the basic theory of logical propositions can be proved by "check all the cases", then it can be proved in the Lean theorem prover using only the eight constructive tactics `intro`, `apply`, `assumption`, `exfalso`, `split`, `cases`, `have`, `left` and `right`, as well as one extra rule called the Law of the Excluded Middle, which in Lean is the tactic `by_cases`. Note that the tactic `finish` is a general "check all the cases" tactic, and it uses `by_cases`. ## Reference * The first half of section 1 of the M40001/40009 course notes. -/ namespace xena variables (P Q R : Prop) /- ## Level 1 : implies In Lean, `P → Q` is the notation for `P ⇒ Q` . Let's start by learning how to control implications. We will learn the three tactics `intro`, `apply` and `exact`. -/ /-- Every proposition implies itself. -/ def id : P → P := begin /- Click here! See that `⊢ P → P` on the top right? That funny symbol `⊢ X` means "you have to prove `X`". So we have to prove that `P` implies `P`. How do we prove that `X` implies `Y`? We assume `X`, and try and deduce `Y`. -/ -- assume P is true. Call this hypothesis hP. intro hP, -- goal now `⊢ P` and we also have hypothesis `hP: P` -- So we know that P is true, by hypothesis hP. exact hP, end -- implication isn't associative! -- Try it when P, Q, R are all false. -- `false → (false → false)` is `true`, -- and -- `(false → false) → false` is `false`. -- in Lean, `P → Q → R` is _defined_ to be `P → (Q → R)` -- Here's a proof of what I just said. example : (P → Q → R) ↔ (P → (Q → R)) := begin -- ⊢ P → Q → R ↔ P → Q → R refl -- that closes goals of the form X = X and X ↔ X. end -- Another way to see it is just to uncomment out the line below: -- #check P → (Q → R) -- output is `P → Q → R : Prop` example : P → Q → P := begin -- remember that by definition the goal is P → (Q → P), -- so it's P implies something, so let's assume -- that P is true and call this hypothesis hP. intro hP, -- Now we have to prove that Q implies P, so let's -- assume that Q is true, and let's call this hypothesis hQ intro hQ, -- We now have to prove that P is true. -- But this is exactly our hypothesis hP. exact hP, end /-- If we know `P`, and we also know `P → Q`, we can deduce `Q`. -/ lemma modus_ponens : P → (P → Q) → Q := begin -- remember this means "P implies that ((P implies Q) implies Q)" -- so let's assume P is true intro hP, -- and let's assume hypothesis hPQ, that P implies Q intro hPQ, -- now `hPQ` says `P → Q` and we're trying to prove `Q`! -- So by applying the hypothesis `hPQ`, we can reduce -- this puzzle to proving `P`. apply hPQ, -- Now we have to prove `P`. But this is just an assumption exact hP, -- or `assumption` end -- See if you can do this one yourself. Replace the `sorry` with a proof. lemma transitivity : (P → Q) → (Q → R) → (P → R) := begin sorry, end -- Of course you can always cheat with the `finish` tactic example : (P → Q) → (Q → R) → (P → R) := begin finish, end -- finish just checks all the cases. It's slower than a constructive proof. -- constructivists regard it as cheating. -- This one is a "relative modus ponens" -- in the -- presence of P, if Q -> R and Q then R. -- Something fun happens in this one. I'll start you off. example : (P → Q → R) → (P → Q) → (P → R) := begin -- Let `hPQR` be the hypothesis that `P → Q → R`. intro hPQR, -- We now need to prove that `(P → Q)` implies something. -- So let `hPQ` be hypothesis that `P → Q` intro hPQ, -- We now need to prove that `P` implies something, so -- let `hP` be the hypothesis that `P` is true. intro hP, -- We now have to prove `R`. -- We know the hypothesis `hPQR : P → (Q → R)`. -- Can we apply it? apply hPQR, -- exercise: what just happened? sorry, sorry end /- ### Level 2 : not `not P`, with notation `¬ P`, is defined to mean `P → false` in Lean, i.e., the proposition that P implies false. Note that `true → false` is `false`, and `false → false` is `true`, so `P → false` is indeed equivalent to `¬ P`. But we need to remember the fact that in Lean, `¬ P` was *defined* to mean `P → false` and not in any other way. We develop a basic interface for `¬`. -/ theorem not_not_intro : P → ¬ (¬ P) := begin -- we have to prove that P implies (not (not P)), -- so let's assume P is true, and let's call this assumption hP intro hP, -- now we have to prove `not (not P)`, a.k.a. `¬ (¬ P)`, and -- by definition this means we have to prove `(¬ P) → false` -- In fact we can `change` our goal to this change ¬ P → false, -- The `change` tactic will make changes to the goal, as long -- as they are true *by definition*. -- So let's let hnP be the hypothesis that `¬ P` is true. intro hnP, -- and now we have to prove `false`! -- Sometimes this can be difficult, but it's OK if you have -- *contradictory hypotheses*, because with contradictory -- assumptions you can prove false conclusions, and once you've -- proved one false thing you've proved all false things because -- you've made mathematics collapse. -- How are we going to use hypothesis `hnP : ¬ P`? -- Well, what does it _mean_? It means `P → false`, -- We could `change` `hnP` to remind us of this: change P → false at hnP, -- Now our _goal_ is false, so why don't we apply -- hypothesis hnP, which will reduce our problem -- to proving `P`. apply hnP, -- now our goal is `P`, and this is an assumption! exact hP end -- What do you think of this proof? theorem not_not_intro'' : P → ¬ (¬ P) := begin apply modus_ponens, -- Go back and look at modus ponens. Can you see how this proof worked? end -- If you're into lambda calculus or functional programming, -- here's a functional proof theorem not_not_intro' : P → ¬ (¬ P) := λ hP hnP, hnP hP -- This one is straightforward -- give it a go: theorem contra1 : (P → Q) → (¬ Q → ¬ P) := begin sorry end -- This way is impossible using constructive logic -- you have to use -- a classical tactic like `finish` or check manually on cases. theorem contra2 : (¬ Q → ¬ P) → (P → Q) := begin intro h, intro hP, -- stuck finish, end /-! ### Level 3 : and The hypothesis `hPaQ : P ∧ Q` in Lean, is equivalent to hypotheses `hP : P` and `hQ : Q`. If you have `hPaQ` as a hypothesis, and you want to get to `hP` and `hQ`, you can use the `cases` tactic. If you have `⊢ P ∧ Q` as a goal, and want to turn the goal into two goals `⊢ P` and `⊢ Q`, then use the `split` tactic. Note that after `split` it's good etiquette to use indentation or brackets, because you have two goals. Example: example (hP : P) (hQ : Q) : P ∧ Q := begin split, exact hP, -- we had two goals here exact hQ -- we are back to one goal end or example (hP : P) (hQ : Q) : P ∧ Q := begin split, { exact hP }, { exact hQ } end -/ theorem and.elim_left : P ∧ Q → P := begin intro hPaQ, -- You can use the `cases` tactic on an `and` hypothesis cases hPaQ with hP hQ, exact hP, end -- try this one theorem and.elim_right : P ∧ Q → Q := begin sorry end -- functional proof theorem and.elim_right' : P ∧ Q → Q := λ hPaQ, hPaQ.2 -- Can you construct the full eliminator for `and`? theorem and.elim : P ∧ Q → (P → Q → R) → R := begin sorry end -- Here's how to solve `and` goals. theorem and.intro : P → Q → P ∧ Q := begin intro hP, intro hQ, -- use `split` on an and goal; you'll get two goals. split, assumption, -- just means "the goal is one of the hypotheses" assumption, end -- there's a two-line proof of this which starts -- `apply function.swap`, but you don't need to do this theorem and.rec : (P → Q → R) → P ∧ Q → R := begin sorry end theorem and.symm : P ∧ Q → Q ∧ P := begin sorry end theorem and.trans : (P ∧ Q) → (Q ∧ R) → (P ∧ R) := begin sorry, end /- Extra credit Recall that the convention for the implies sign → is that it is _right associative_, by which I mean that `P → Q → R` means `P → (Q → R)` by definition. This does actually simplify! If `P` implies `Q → R` then this means that `P` and `Q` together, imply `R`, so `P → Q → R` is logically equivalent to `(P ∧ Q) → R`. We proved that `P → Q → R` implied `(P ∧ Q) → R`; this was `and.rec`. Let's go the other way. -/ example : ((P ∧ Q) → R) → (P → Q → R) := begin sorry end /-! ### Level 4 : iff The basic theory of `iff`. In Lean, `P ↔ Q` is *defined to mean* `(P → Q) ∧ (Q → P)`. It is _not_ defined by a truth table. You can attack a `P ↔ Q` goal with the `split` tactic, because it is really an `∧` statement. -/ /-- `P ↔ P` is true for all propositions `P`. -/ def iff.refl : P ↔ P := begin -- By Lean's definition I need to prove (P → P) ∧ (P → P) split, -- need to prove P → P -- We proved that a long time ago and called it `id`. apply id, -- need to prove P → P apply id end -- If you get stuck, there is always the "truth table" tactic `finish` def iff.refl' : P ↔ P := begin finish, end -- The refl tactic also works def iff.refl'' : P ↔ P := begin refl end def iff.symm : (P ↔ Q) → (Q ↔ P) := begin -- Try this one using `cases` and `split`. sorry end -- I'll now show you a better way: the `rewrite` tactic. def iff.symm' : (P ↔ Q) → (Q ↔ P) := begin intro h, -- `h : P ↔ Q` -- The `rw h` tactic will change all P's in the goal to Q's. -- And then it will try `refl`, just for luck rw h, -- finished! Goal becamse `Q ↔ Q` and then `refl` finished it. end def iff.comm : (P ↔ Q) ↔ (Q ↔ P) := begin sorry, end -- without rw or cc this is ugly def iff.trans : (P ↔ Q) → (Q ↔ R) → (P ↔ R) := begin sorry end -- This is a cute question. Can you prove it constructively, -- using only `intro`, `cases`, `have`, `apply`, and `assumption`? def iff.boss : ¬ (P ↔ ¬ P) := begin sorry end -- Now we have iff we can go back to and. /-! ### iff epilogue: ↔ and ∧ -/ theorem and.comm : P ∧ Q ↔ Q ∧ P := begin sorry end -- ∧ is "right associative" in Lean, which means -- that `P ∧ Q ∧ R` is _defined to mean_ `P ∧ (Q ∧ R)`. -- Associativity can hence be written like this: theorem and_assoc : ((P ∧ Q) ∧ R) ↔ (P ∧ Q ∧ R) := begin sorry, end /-! ## Level 5 (final level) : Or `P ∨ Q` is true when at least one of `P` and `Q` are true. Here is how to work with `∨` in Lean. If you have a _hypothesis_ `hPoQ : P ∨ Q` then you can break into the two cases `hP : P` and `hQ : Q` using `cases hPoQ with hP hQ` If you have a _goal_ of the form `⊢ P ∨ Q` then you need to decide whether you're going to prove `P` or `Q`. If you want to prove `P` then use the `left` tactic, and if you want to prove `Q` then use the `right` tactic. Don't get lost! You can't go back. -/ -- recall that P, Q, R are Propositions. We'll need S for this one. variable (S : Prop) -- use the `left` tactic to reduce from `⊢ P ∨ Q` to `⊢ P` theorem or.intro_left : P → P ∨ Q := begin intro hP, -- ⊢ P ∨ Q left, -- ⊢ P exact hP end -- use the `right` tactic to reduce from `⊢ P ∨ Q` theorem or.intro_right : Q → P ∨ Q := begin sorry, end theorem or.elim : P ∨ Q → (P → R) → (Q → R) → R := begin intro hPoQ, intros hpq hqr, -- use the `cases h` tactic if `h : X ∨ Y` cases hPoQ with hP hQ, sorry, sorry end theorem or.symm : P ∨ Q → Q ∨ P := begin sorry end theorem or.comm : P ∨ Q ↔ Q ∨ P := begin sorry end theorem or.assoc : (P ∨ Q) ∨ R ↔ P ∨ Q ∨ R := begin sorry, end theorem or.cases_on : P ∨ Q → (P → R) → (Q → R) → R := begin sorry, end theorem or.imp : (P → R) → (Q → S) → P ∨ Q → R ∨ S := begin sorry, end theorem or.imp_left : (P → Q) → P ∨ R → Q ∨ R := begin sorry, end theorem or.imp_right : (P → Q) → R ∨ P → R ∨ Q := begin sorry, end theorem or.left_comm : P ∨ Q ∨ R ↔ Q ∨ P ∨ R := begin sorry, end theorem or.rec : (P → R) → (Q → R) → P ∨ Q → R := begin sorry end theorem or.resolve_left : P ∨ Q → ¬P → Q := begin sorry, end theorem or_congr : (P ↔ R) → (Q ↔ S) → (P ∨ Q ↔ R ∨ S) := begin sorry, end /-! # Appendix: `exfalso` and classical logic -/ -- useful lemma about false theorem false.elim' : false → P := begin -- Let's assume that a false proposition is true. Let's -- call this assumption h. intro h, -- We now have to prove P. -- The `exfalso` tactic changes any goal to `false`. exfalso, -- Now our goal is an assumption! It's exactly `h`. exact h, end -- Is that confusing? What about this proof? theorem false.elim'' : false → P := begin -- Let's assume that a false proposition is true. Let's -- call this assumption h. intro h, -- Now let's deal with all the cases. cases h, -- There are no cases. end -- This next one cannot be proved using the tactics we know -- which are constructive. This one needs the assumption -- that every statement is true or false. -- We give a "by cases" proof explicitly -- `finish` just does the -- job immediately. theorem double_negation_elimination : ¬ (¬ P) → P := begin -- `finish` works classical, intro hnnP, by_cases hP : P, -- hypothesis hP : P assumption, -- hypothesis hP : ¬ P -- `contradiction` works from here exfalso, apply hnnP, exact hP, end end xena
6e30cd0bf36712ec7f46a1b198f4951e90c6d2f3
367134ba5a65885e863bdc4507601606690974c1
/src/order/zorn.lean
6ee69a190426c0f5a42496f3b814fa923b68e8ae
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
13,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 Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import data.set.lattice noncomputable theory universes u open set classical open_locale classical namespace zorn section chain parameters {α : Type u} (r : α → α → Prop) local infix ` ≺ `:50 := r /-- A chain is a subset `c` satisfying `x ≺ y ∨ x = y ∨ y ≺ x` for all `x y ∈ c`. -/ def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) parameters {r} theorem chain.total_of_refl [is_refl α r] {c} (H : chain c) {x y} (hx : x ∈ c) (hy : y ∈ c) : x ≺ y ∨ y ≺ x := if e : x = y then or.inl (e ▸ refl _) else H _ hx _ hy e theorem chain.mono {c c'} : c' ⊆ c → chain c → chain c' := pairwise_on.mono theorem chain.directed_on [is_refl α r] {c} (H : chain c) : directed_on (≺) c := assume x hx y hy, match H.total_of_refl hx hy with | or.inl h := ⟨y, hy, h, refl _⟩ | or.inr h := ⟨x, hx, refl _, h⟩ end theorem chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (assume x hx, forall_insert_of_forall (hc x hx) (assume hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (assume x hx hneq, ha x hx $ assume h', hneq h'.symm) (assume h, (h rfl).rec _)) /-- `super_chain c₁ c₂` means that `c₂ is a chain that strictly includes `c₁`. -/ def super_chain (c₁ c₂ : set α) : Prop := chain c₂ ∧ c₁ ⊂ c₂ /-- A chain `c` is a maximal chain if there does not exists a chain strictly including `c`. -/ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') /-- Given a set `c`, if there exists a chain `c'` strictly including `c`, then `succ_chain c` is one of these chains. Otherwise it is `c`. -/ def succ_chain (c : set α) : set α := if h : ∃c', chain c ∧ super_chain c c' then some h else c theorem succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] theorem chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc theorem super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_distrib, not_forall_not] at hc₂, cases hc₂.neg_resolve_left hc₁ with c' hc', exact succ_spec ⟨c', hc₁, hc'⟩ end theorem succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] /-- Set of sets reachable from `∅` using `succ_chain` and `⋃₀`. -/ inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) theorem chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ assume a h, h.rec _, by simp at this; assumption theorem chain_closure_closure : chain_closure (⋃₀ chain_closure) := chain_closure.union $ assume s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case succ : c₃ hc₃ ih { cases ih with ih ih, { have h := h hc₃ ih, cases h with h h, { exact or.inr (h ▸ subset.refl _) }, { exact or.inl h } }, { exact or.inr (subset.trans ih succ_increasing) } }, case union : s hs ih { refine (or_iff_not_imp_right.2 $ λ hn, sUnion_subset $ λ a ha, _), apply (ih a ha).resolve_right, apply mt (λ h, _) hn, exact subset.trans h (subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case succ : c₂ hc₂ ih { have h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ assume c₁, ih), cases h₁ with h₁ h₁, { have h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case union : s hs ih { apply or.imp_left (assume h', subset.antisymm h' h), apply classical.by_contradiction, simp [not_or_distrib, sUnion_subset_iff, not_forall], intros c₃ hc₃ h₁ h₂, have h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (assume c₄, ih _ hc₃), cases h with h h, { have h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₁ $ h' ▸ subset.refl _) }, { exact (h₂ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₁ $ subset.trans succ_increasing h) } } end theorem chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ assume c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (assume : succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this theorem chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case succ : c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (assume h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case union : s hs ih { exact (sUnion_subset $ assume c₁ hc₁, ih c₁ hc₁) } end theorem chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ chain_closure := ⟨assume h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), assume : c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ theorem chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case succ : c hc h { exact chain_succ h }, case union : s hs h { have h : ∀c∈s, zorn.chain c := h, exact assume c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (assume : t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (assume : t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end /-- `max_chain` is the union of all sets in the chain closure. -/ def max_chain := ⋃₀ chain_closure /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ theorem max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ assume : ¬ is_max_chain (⋃₀ chain_closure), have super_chain (⋃₀ chain_closure) (succ_chain (⋃₀ chain_closure)), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, H⟩ := this, ⟨h₂, (h₃ : (⋃₀ chain_closure) ≠ succ_chain (⋃₀ chain_closure))⟩ := ssubset_iff_subset_ne.1 H in have succ_chain (⋃₀ chain_closure) = (⋃₀ chain_closure), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ theorem exists_maximal_of_chains_bounded (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, assume a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ assume b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ /-- If every nonempty chain of a nonempty type has an upper bound, then there is a maximal element. (A variant of Zorn's lemma.) -/ theorem exists_maximal_of_nonempty_chains_bounded [nonempty α] (h : ∀c, chain c → c.nonempty → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := exists_maximal_of_chains_bounded (λ c hc, (eq_empty_or_nonempty c).elim (λ h, ⟨classical.arbitrary α, λ x hx, (h ▸ hx : x ∈ (∅ : set α)).elim⟩) (h c hc)) (λ a b c, trans) end chain theorem zorn_partial_order {α : Type u} [partial_order α] (h : ∀c:set α, chain (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @exists_maximal_of_chains_bounded α (≤) h (assume a b c, le_trans) in ⟨m, assume a ha, le_antisymm (hm a ha) ha⟩ theorem zorn_nonempty_partial_order {α : Type u} [partial_order α] [nonempty α] (h : ∀ (c : set α), chain (≤) c → c.nonempty → ∃ub, ∀ a ∈ c, a ≤ ub) : ∃ (m : α), ∀ a, m ≤ a → a = m := let ⟨m, hm⟩ := @exists_maximal_of_nonempty_chains_bounded α (≤) _ h (λ a b c, le_trans) in ⟨m, λ a ha, le_antisymm (hm a ha) ha⟩ theorem zorn_partial_order₀ {α : Type u} [partial_order α] (s : set α) (ih : ∀ c ⊆ s, chain (≤) c → ∀ y ∈ c, ∃ ub ∈ s, ∀ z ∈ c, z ≤ ub) (x : α) (hxs : x ∈ s) : ∃ m ∈ s, x ≤ m ∧ ∀ z ∈ s, m ≤ z → z = m := let ⟨⟨m, hms, hxm⟩, h⟩ := @zorn_partial_order {m // m ∈ s ∧ x ≤ m} _ (λ c hc, c.eq_empty_or_nonempty.elim (assume hce, hce.symm ▸ ⟨⟨x, hxs, le_refl _⟩, λ _, false.elim⟩) (assume ⟨m, hmc⟩, let ⟨ub, hubs, hub⟩ := ih (subtype.val '' c) (image_subset_iff.2 $ λ z hzc, z.2.1) (by rintro _ ⟨p, hpc, rfl⟩ _ ⟨q, hqc, rfl⟩ hpq; exact hc p hpc q hqc (mt (by rintro rfl; refl) hpq)) m.1 (mem_image_of_mem _ hmc) in ⟨⟨ub, hubs, le_trans m.2.2 $ hub m.1 $ mem_image_of_mem _ hmc⟩, λ a hac, hub a.1 ⟨a, hac, rfl⟩⟩)) in ⟨m, hms, hxm, λ z hzs hmz, congr_arg subtype.val $ h ⟨z, hzs, le_trans hxm hmz⟩ hmz⟩ theorem zorn_subset {α : Type u} (S : set (set α)) (h : ∀c ⊆ S, chain (⊆) c → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) : ∃ m ∈ S, ∀a ∈ S, m ⊆ a → a = m := begin letI : partial_order S := partial_order.lift subtype.val (λ _ _, subtype.ext_val), have : ∀c:set S, @chain S (≤) c → ∃ub, ∀a∈c, a ≤ ub, { intros c hc, rcases h (subtype.val '' c) (image_subset_iff.2 _) _ with ⟨s, sS, hs⟩, { exact ⟨⟨s, sS⟩, λ ⟨x, hx⟩ H, hs _ (mem_image_of_mem _ H)⟩ }, { rintro ⟨x, hx⟩ _, exact hx }, { rintro _ ⟨x, cx, rfl⟩ _ ⟨y, cy, rfl⟩ xy, exact hc x cx y cy (mt (congr_arg _) xy) } }, rcases zorn_partial_order this with ⟨⟨m, mS⟩, hm⟩, exact ⟨m, mS, λ a aS ha, congr_arg subtype.val (hm ⟨a, aS⟩ ha)⟩ end theorem zorn_subset₀ {α : Type u} (S : set (set α)) (H : ∀c ⊆ S, chain (⊆) c → c.nonempty → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) (x) (hx : x ∈ S) : ∃ m ∈ S, x ⊆ m ∧ ∀a ∈ S, m ⊆ a → a = m := begin let T := {s ∈ S | x ⊆ s}, rcases zorn_subset T _ with ⟨m, ⟨mS, mx⟩, hm⟩, { exact ⟨m, mS, mx, λ a ha ha', hm a ⟨ha, subset.trans mx ha'⟩ ha'⟩ }, { intros c cT hc, cases c.eq_empty_or_nonempty with c0 c0, { rw c0, exact ⟨x, ⟨hx, subset.refl _⟩, λ _, false.elim⟩ }, { rcases H _ (subset.trans cT (sep_subset _ _)) hc c0 with ⟨ub, us, h⟩, refine ⟨ub, ⟨us, _⟩, h⟩, rcases c0 with ⟨s, hs⟩, exact subset.trans (cT hs).2 (h _ hs) } } end theorem chain.total {α : Type u} [preorder α] {c : set α} (H : chain (≤) c) : ∀ {x y}, x ∈ c → y ∈ c → x ≤ y ∨ y ≤ x := λ x y, H.total_of_refl theorem chain.image {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) (f : α → β) (h : ∀ x y, r x y → s (f x) (f y)) {c : set α} (hrc : chain r c) : chain s (f '' c) := λ x ⟨a, ha₁, ha₂⟩ y ⟨b, hb₁, hb₂⟩, ha₂ ▸ hb₂ ▸ λ hxy, (hrc a ha₁ b hb₁ (mt (congr_arg f) $ hxy)).elim (or.inl ∘ h _ _) (or.inr ∘ h _ _) end zorn theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α} (h : zorn.chain (f ⁻¹'o r) c) : directed r (λx:{a:α // a ∈ c}, f x) := assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases (assume : a = b, by simp only [this, exists_prop, and_self, subtype.exists]; exact ⟨b, hb, refl _⟩) (assume : a ≠ b, (h a ha b hb this).elim (λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩) (λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩))
b4cac92593a1f31df39e119a80faa4216b594034
94e33a31faa76775069b071adea97e86e218a8ee
/src/order/initial_seg.lean
a6ce2466a5320d65e96873d80578740aea249bab
[ "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
16,984
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import order.rel_iso import order.well_founded /-! # Initial and principal segments This file defines initial and principal segments. ## Main definitions * `initial_seg r s`: type of order embeddings of `r` into `s` for which the range is an initial segment (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). It is denoted by `r ≼i s`. * `principal_seg r s`: Type of order embeddings of `r` into `s` for which the range is a principal segment, i.e., an interval of the form `(-∞, top)` for some element `top`. It is denoted by `r ≺i s`. ## Notations These notations belong to the `initial_seg` locale. * `r ≼i s`: the type of initial segment embeddings of `r` into `s`. * `r ≺i s`: the type of principal segment embeddings of `r` into `s`. -/ /-! ### Initial segments Order embeddings whose range is an initial segment of `s` (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). The type of these embeddings from `r` to `s` is called `initial_seg r s`, and denoted by `r ≼i s`. -/ variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} open function /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≼i s` is an order embedding whose range is an initial segment. That is, whenever `b < f a` in `β` then `b` is in the range of `f`. -/ @[nolint has_inhabited_instance] structure initial_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (init : ∀ a b, s b (to_rel_embedding a) → ∃ a', to_rel_embedding a' = b) localized "infix ` ≼i `:25 := initial_seg" in initial_seg namespace initial_seg instance : has_coe (r ≼i s) (r ↪r s) := ⟨initial_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≼i s) (λ _, α → β) := ⟨λ f x, (f : r ↪r s) x⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (o) : (@initial_seg.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≼i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≼i s) : ((f : r ↪r s) : α → β) = f := rfl theorem init' (f : r ≼i s) {a : α} {b : β} : s b (f a) → ∃ a', f a' = b := f.init _ _ theorem init_iff (f : r ≼i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := ⟨λ h, let ⟨a', e⟩ := f.init' h in ⟨a', e, (f : r ↪r s).map_rel_iff.1 (e.symm ▸ h)⟩, λ ⟨a', e, h⟩, e ▸ (f : r ↪r s).map_rel_iff.2 h⟩ /-- An order isomorphism is an initial segment -/ def of_iso (f : r ≃r s) : r ≼i s := ⟨f, λ a b h, ⟨f.symm b, rel_iso.apply_symm_apply f _⟩⟩ /-- The identity function shows that `≼i` is reflexive -/ @[refl] protected def refl (r : α → α → Prop) : r ≼i r := ⟨rel_embedding.refl _, λ a b h, ⟨_, rfl⟩⟩ /-- Composition of functions shows that `≼i` is transitive -/ @[trans] protected def trans (f : r ≼i s) (g : s ≼i t) : r ≼i t := ⟨f.1.trans g.1, λ a c h, begin simp at h ⊢, rcases g.2 _ _ h with ⟨b, rfl⟩, have h := g.1.map_rel_iff.1 h, rcases f.2 _ _ h with ⟨a', rfl⟩, exact ⟨a', rfl⟩ end⟩ @[simp] theorem refl_apply (x : α) : initial_seg.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼i s) (g : s ≼i t) (a : α) : (f.trans g) a = g (f a) := rfl theorem unique_of_extensional [is_extensional β s] : well_founded r → subsingleton (r ≼i s) | ⟨h⟩ := ⟨λ f g, begin suffices : (f : α → β) = g, { cases f, cases g, congr, exact rel_embedding.coe_fn_injective this }, funext a, have := h a, induction this with a H IH, refine @is_extensional.ext _ s _ _ _ (λ x, ⟨λ h, _, λ h, _⟩), { rcases f.init_iff.1 h with ⟨y, rfl, h'⟩, rw IH _ h', exact (g : r ↪r s).map_rel_iff.2 h' }, { rcases g.init_iff.1 h with ⟨y, rfl, h'⟩, rw ← IH _ h', exact (f : r ↪r s).map_rel_iff.2 h' } end⟩ instance [is_well_order β s] : subsingleton (r ≼i s) := ⟨λ a, @subsingleton.elim _ (unique_of_extensional (@rel_embedding.well_founded _ _ r s a is_well_order.wf)) a⟩ protected theorem eq [is_well_order β s] (f g : r ≼i s) (a) : f a = g a := by rw subsingleton.elim f g theorem antisymm.aux [is_well_order α r] (f : r ≼i s) (g : s ≼i r) : left_inverse g f := initial_seg.eq (f.trans g) (initial_seg.refl _) /-- If we have order embeddings between `α` and `β` whose images are initial segments, and `β` is a well-order then `α` and `β` are order-isomorphic. -/ def antisymm [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : r ≃r s := by haveI := f.to_rel_embedding.is_well_order; exact ⟨⟨f, g, antisymm.aux f g, antisymm.aux g f⟩, f.map_rel_iff'⟩ @[simp] theorem antisymm_to_fun [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g : α → β) = f := rfl @[simp] theorem antisymm_symm [is_well_order α r] [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g).symm = antisymm g f := rel_iso.coe_fn_injective rfl theorem eq_or_principal [is_well_order β s] (f : r ≼i s) : surjective f ∨ ∃ b, ∀ x, s x b ↔ ∃ y, f y = x := or_iff_not_imp_right.2 $ λ h b, acc.rec_on (is_well_order.wf.apply b : acc s b) $ λ x H IH, not_forall_not.1 $ λ hn, h ⟨x, λ y, ⟨(IH _), λ ⟨a, e⟩, by rw ← e; exact (trichotomous _ _).resolve_right (not_or (hn a) (λ hl, not_exists.2 hn (f.init' hl)))⟩⟩ /-- Restrict the codomain of an initial segment -/ def cod_restrict (p : set β) (f : r ≼i s) (H : ∀ a, f a ∈ p) : r ≼i subrel s p := ⟨rel_embedding.cod_restrict p f H, λ a ⟨b, m⟩ (h : s b (f a)), let ⟨a', e⟩ := f.init' h in ⟨a', by clear _let_match; subst e; refl⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≼i s) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- Initial segment embedding of an order `r` into the disjoint union of `r` and `s`. -/ def le_add (r : α → α → Prop) (s : β → β → Prop) : r ≼i sum.lex r s := ⟨⟨⟨sum.inl, λ _ _, sum.inl.inj⟩, λ a b, sum.lex_inl_inl⟩, λ a b, by cases b; [exact λ _, ⟨_, rfl⟩, exact false.elim ∘ sum.lex_inr_inl]⟩ @[simp] theorem le_add_apply (r : α → α → Prop) (s : β → β → Prop) (a) : le_add r s a = sum.inl a := rfl end initial_seg /-! ### Principal segments Order embeddings whose range is a principal segment of `s` (i.e., an interval of the form `(-∞, top)` for some element `top` of `β`). The type of these embeddings from `r` to `s` is called `principal_seg r s`, and denoted by `r ≺i s`. Principal segments are in particular initial segments. -/ /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≺i s` is an order embedding whose range is an open interval `(-∞, top)` for some element `top` of `β`. Such order embeddings are called principal segments -/ @[nolint has_inhabited_instance] structure principal_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (top : β) (down : ∀ b, s b top ↔ ∃ a, to_rel_embedding a = b) localized "infix ` ≺i `:25 := principal_seg" in initial_seg namespace principal_seg instance : has_coe (r ≺i s) (r ↪r s) := ⟨principal_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≺i s) (λ _, α → β) := ⟨λ f, f⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (t o) : (@principal_seg.mk _ _ r s f t o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≺i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≺i s) : ((f : r ↪r s) : α → β) = f := rfl theorem down' (f : r ≺i s) {b : β} : s b f.top ↔ ∃ a, f a = b := f.down _ theorem lt_top (f : r ≺i s) (a : α) : s (f a) f.top := f.down'.2 ⟨_, rfl⟩ theorem init [is_trans β s] (f : r ≺i s) {a : α} {b : β} (h : s b (f a)) : ∃ a', f a' = b := f.down'.1 $ trans h $ f.lt_top _ /-- A principal segment is in particular an initial segment. -/ instance has_coe_initial_seg [is_trans β s] : has_coe (r ≺i s) (r ≼i s) := ⟨λ f, ⟨f.to_rel_embedding, λ a b, f.init⟩⟩ theorem coe_coe_fn' [is_trans β s] (f : r ≺i s) : ((f : r ≼i s) : α → β) = f := rfl theorem init_iff [is_trans β s] (f : r ≺i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := @initial_seg.init_iff α β r s f a b theorem irrefl (r : α → α → Prop) [is_well_order α r] (f : r ≺i r) : false := begin have := f.lt_top f.top, rw [show f f.top = f.top, from initial_seg.eq ↑f (initial_seg.refl r) f.top] at this, exact irrefl _ this end /-- Composition of a principal segment with an initial segment, as a principal segment -/ def lt_le (f : r ≺i s) (g : s ≼i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, λ a, by simp only [g.init_iff, f.down', exists_and_distrib_left.symm, exists_swap, rel_embedding.trans_apply, exists_eq_right']; refl⟩ @[simp] theorem lt_le_apply (f : r ≺i s) (g : s ≼i t) (a : α) : (f.lt_le g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem lt_le_top (f : r ≺i s) (g : s ≼i t) : (f.lt_le g).top = g f.top := rfl /-- Composition of two principal segments as a principal segment -/ @[trans] protected def trans [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : r ≺i t := lt_le f g @[simp] theorem trans_apply [is_trans γ t] (f : r ≺i s) (g : s ≺i t) (a : α) : (f.trans g) a = g (f a) := lt_le_apply _ _ _ @[simp] theorem trans_top [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : (f.trans g).top = g f.top := rfl /-- Composition of an order isomorphism with a principal segment, as a principal segment -/ def equiv_lt (f : r ≃r s) (g : s ≺i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g.top, λ c, suffices (∃ (a : β), g a = c) ↔ ∃ (a : α), g (f a) = c, by simpa [g.down], ⟨λ ⟨b, h⟩, ⟨f.symm b, by simp only [h, rel_iso.apply_symm_apply, rel_iso.coe_coe_fn]⟩, λ ⟨a, h⟩, ⟨f a, h⟩⟩⟩ /-- Composition of a principal segment with an order isomorphism, as a principal segment -/ def lt_equiv {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} (f : principal_seg r s) (g : s ≃r t) : principal_seg r t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, begin intro x, rw [← g.apply_symm_apply x, g.map_rel_iff, f.down', exists_congr], intro y, exact ⟨congr_arg g, λ h, g.to_equiv.bijective.1 h⟩ end⟩ @[simp] theorem equiv_lt_apply (f : r ≃r s) (g : s ≺i t) (a : α) : (equiv_lt f g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem equiv_lt_top (f : r ≃r s) (g : s ≺i t) : (equiv_lt f g).top = g.top := rfl /-- Given a well order `s`, there is a most one principal segment embedding of `r` into `s`. -/ instance [is_well_order β s] : subsingleton (r ≺i s) := ⟨λ f g, begin have ef : (f : α → β) = g, { show ((f : r ≼i s) : α → β) = g, rw @subsingleton.elim _ _ (f : r ≼i s) g, refl }, have et : f.top = g.top, { refine @is_extensional.ext _ s _ _ _ (λ x, _), simp only [f.down, g.down, ef, coe_fn_to_rel_embedding] }, cases f, cases g, have := rel_embedding.coe_fn_injective ef; congr' end⟩ theorem top_eq [is_well_order γ t] (e : r ≃r s) (f : r ≺i t) (g : s ≺i t) : f.top = g.top := by rw subsingleton.elim f (principal_seg.equiv_lt e g); refl lemma top_lt_top {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} [is_well_order γ t] (f : principal_seg r s) (g : principal_seg s t) (h : principal_seg r t) : t h.top g.top := by { rw [subsingleton.elim h (f.trans g)], apply principal_seg.lt_top } /-- Any element of a well order yields a principal segment -/ def of_element {α : Type*} (r : α → α → Prop) (a : α) : subrel r {b | r b a} ≺i r := ⟨subrel.rel_embedding _ _, a, λ b, ⟨λ h, ⟨⟨_, h⟩, rfl⟩, λ ⟨⟨_, h⟩, rfl⟩, h⟩⟩ @[simp] theorem of_element_apply {α : Type*} (r : α → α → Prop) (a : α) (b) : of_element r a b = b.1 := rfl @[simp] theorem of_element_top {α : Type*} (r : α → α → Prop) (a : α) : (of_element r a).top = a := rfl /-- Restrict the codomain of a principal segment -/ def cod_restrict (p : set β) (f : r ≺i s) (H : ∀ a, f a ∈ p) (H₂ : f.top ∈ p) : r ≺i subrel s p := ⟨rel_embedding.cod_restrict p f H, ⟨f.top, H₂⟩, λ ⟨b, h⟩, f.down'.trans $ exists_congr $ λ a, show (⟨f a, H a⟩ : p).1 = _ ↔ _, from ⟨subtype.eq, congr_arg _⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≺i s) (H H₂ a) : cod_restrict p f H H₂ a = ⟨f a, H a⟩ := rfl @[simp] theorem cod_restrict_top (p) (f : r ≺i s) (H H₂) : (cod_restrict p f H H₂).top = ⟨f.top, H₂⟩ := rfl end principal_seg /-! ### Properties of initial and principal segments -/ /-- To an initial segment taking values in a well order, one can associate either a principal segment (if the range is not everything, hence one can take as top the minimum of the complement of the range) or an order isomorphism (if the range is everything). -/ noncomputable def initial_seg.lt_or_eq [is_well_order β s] (f : r ≼i s) : (r ≺i s) ⊕ (r ≃r s) := begin by_cases h : surjective f, { exact sum.inr (rel_iso.of_surjective f h) }, { have h' : _, from (initial_seg.eq_or_principal f).resolve_left h, exact sum.inl ⟨f, classical.some h', classical.some_spec h'⟩ } end theorem initial_seg.lt_or_eq_apply_left [is_well_order β s] (f : r ≼i s) (g : r ≺i s) (a : α) : g a = f a := @initial_seg.eq α β r s _ g f a theorem initial_seg.lt_or_eq_apply_right [is_well_order β s] (f : r ≼i s) (g : r ≃r s) (a : α) : g a = f a := initial_seg.eq (initial_seg.of_iso g) f a /-- Composition of an initial segment taking values in a well order and a principal segment. -/ noncomputable def initial_seg.le_lt [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) : r ≺i t := match f.lt_or_eq with | sum.inl f' := f'.trans g | sum.inr f' := principal_seg.equiv_lt f' g end @[simp] theorem initial_seg.le_lt_apply [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) (a : α) : (f.le_lt g) a = g (f a) := begin delta initial_seg.le_lt, cases h : f.lt_or_eq with f' f', { simp only [principal_seg.trans_apply, f.lt_or_eq_apply_left] }, { simp only [principal_seg.equiv_lt_apply, f.lt_or_eq_apply_right] } end namespace rel_embedding /-- Given an order embedding into a well order, collapse the order embedding by filling the gaps, to obtain an initial segment. Here, we construct the collapsed order embedding pointwise, but the proof of the fact that it is an initial segment will be given in `collapse`. -/ noncomputable def collapse_F [is_well_order β s] (f : r ↪r s) : Π a, {b // ¬ s (f a) b} := (rel_embedding.well_founded f $ is_well_order.wf).fix $ λ a IH, begin let S := {b | ∀ a h, s (IH a h).1 b}, have : f a ∈ S, from λ a' h, ((trichotomous _ _) .resolve_left $ λ h', (IH a' h).2 $ trans (f.map_rel_iff.2 h) h') .resolve_left $ λ h', (IH a' h).2 $ h' ▸ f.map_rel_iff.2 h, exact ⟨is_well_order.wf.min S ⟨_, this⟩, is_well_order.wf.not_lt_min _ _ this⟩ end theorem collapse_F.lt [is_well_order β s] (f : r ↪r s) {a : α} : ∀ {a'}, r a' a → s (collapse_F f a').1 (collapse_F f a).1 := show (collapse_F f a).1 ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, begin unfold collapse_F, rw well_founded.fix_eq, apply well_founded.min_mem _ _ end theorem collapse_F.not_lt [is_well_order β s] (f : r ↪r s) (a : α) {b} (h : ∀ a' (h : r a' a), s (collapse_F f a').1 b) : ¬ s b (collapse_F f a).1 := begin unfold collapse_F, rw well_founded.fix_eq, exact well_founded.not_lt_min _ _ _ (show b ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, from h) end /-- Construct an initial segment from an order embedding into a well order, by collapsing it to fill the gaps. -/ noncomputable def collapse [is_well_order β s] (f : r ↪r s) : r ≼i s := by haveI := rel_embedding.is_well_order f; exact ⟨rel_embedding.of_monotone (λ a, (collapse_F f a).1) (λ a b, collapse_F.lt f), λ a b, acc.rec_on (is_well_order.wf.apply b : acc s b) (λ b H IH a h, begin let S := {a | ¬ s (collapse_F f a).1 b}, have : S.nonempty := ⟨_, asymm h⟩, existsi (is_well_order.wf : well_founded r).min S this, refine ((@trichotomous _ s _ _ _).resolve_left _).resolve_right _, { exact (is_well_order.wf : well_founded r).min_mem S this }, { refine collapse_F.not_lt f _ (λ a' h', _), by_contradiction hn, exact is_well_order.wf.not_lt_min S this hn h' } end) a⟩ theorem collapse_apply [is_well_order β s] (f : r ↪r s) (a) : collapse f a = (collapse_F f a).1 := rfl end rel_embedding
cb70a44a2704860ecb64591d8d243f462a571f45
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/set_theory/lists.lean
687e463b153e190603351d6dcc1eacd5481aad57
[ "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
14,886
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.basic /-! # A computable model of ZFA without infinity In this file we define finite hereditary lists. This is useful for calculations in naive set theory. We distinguish two kinds of ZFA lists: * Atoms. Directly correspond to an element of the original type. * Proper ZFA lists. Can thought of (but aren't implemented) as a list of ZFA lists (not necessarily proper). For example, `lists ℕ` contains stuff like `23`, `[]`, `[37]`, `[1, [[2], 3], 4]`. ## Implementation note As we want to be able to append both atoms and proper ZFA lists to proper ZFA lists, it's handy that atoms and proper ZFA lists belong to the same type, even though atoms of `α` could be modelled as `α` directly. But we don't want to be able to append anything to atoms. This calls for a two-steps definition of ZFA lists: * First, define ZFA prelists as atoms and proper ZFA prelists. Those proper ZFA prelists are defined by inductive appending of (not necessarily proper) ZFA lists. * Second, define ZFA lists by rubbing out the distinction between atoms and proper lists. ## Main declarations * `lists' α ff`: Atoms as ZFA prelists. Basically a copy of `α`. * `lists' α tt`: Proper ZFA prelists. Defined inductively from the empty ZFA prelist (`lists'.nil`) and from appending a ZFA prelist to a proper ZFA prelist (`lists'.cons a l`). * `lists α`: ZFA lists. Sum of the atoms and proper ZFA prelists. ## TODO The next step is to define ZFA sets as lists quotiented by `lists.equiv`. (-/ variables {α : Type*} /-- Prelists, helper type to define `lists`. `lists' α ff` are the "atoms", a copy of `α`. `lists' α tt` are the "proper" ZFA prelists, inductively defined from the empty ZFA prelist and from appending a ZFA prelist to a proper ZFA prelist. It is made so that you can't append anything to an atom while having only one appending function for appending both atoms and proper ZFC prelists to a proper ZFA prelist. -/ @[derive decidable_eq] inductive {u} lists' (α : Type u) : bool → Type u | atom : α → lists' ff | nil : lists' tt | cons' {b} : lists' b → lists' tt → lists' tt /-- Hereditarily finite list, aka ZFA list. A ZFA list is either an "atom" (`b = ff`), corresponding to an element of `α`, or a "proper" ZFA list, inductively defined from the empty ZFA list and from appending a ZFA list to a proper ZFA list. -/ def lists (α : Type*) := Σ b, lists' α b namespace lists' instance [inhabited α] : ∀ b, inhabited (lists' α b) | tt := ⟨nil⟩ | ff := ⟨atom (default _)⟩ /-- Appending a ZFA list to a proper ZFA prelist. -/ def cons : lists α → lists' α tt → lists' α tt | ⟨b, a⟩ l := cons' a l /-- Converts a ZFA prelist to a `list` of ZFA lists. Atoms are sent to `[]`. -/ @[simp] def to_list : ∀ {b}, lists' α b → list (lists α) | _ (atom a) := [] | _ nil := [] | _ (cons' a l) := ⟨_, a⟩ :: l.to_list @[simp] theorem to_list_cons (a : lists α) (l) : to_list (cons a l) = a :: l.to_list := by cases a; simp [cons] /-- Converts a `list` of ZFA lists to a proper ZFA prelist. -/ @[simp] def of_list : list (lists α) → lists' α tt | [] := nil | (a :: l) := cons a (of_list l) @[simp] theorem to_of_list (l : list (lists α)) : to_list (of_list l) = l := by induction l; simp * @[simp] theorem of_to_list : ∀ (l : lists' α tt), of_list (to_list l) = l := suffices ∀ b (h : tt = b) (l : lists' α b), let l' : lists' α tt := by rw h; exact l in of_list (to_list l') = l', from this _ rfl, λ b h l, begin induction l, {cases h}, {exact rfl}, case lists'.cons' : b a l IH₁ IH₂ { intro, change l' with cons' a l, simpa [cons] using IH₂ rfl } end end lists' mutual inductive lists.equiv, lists'.subset with lists.equiv : lists α → lists α → Prop | refl (l) : lists.equiv l l | antisymm {l₁ l₂ : lists' α tt} : lists'.subset l₁ l₂ → lists'.subset l₂ l₁ → lists.equiv ⟨_, l₁⟩ ⟨_, l₂⟩ with lists'.subset : lists' α tt → lists' α tt → Prop | nil {l} : lists'.subset lists'.nil l | cons {a a' l l'} : lists.equiv a a' → a' ∈ lists'.to_list l' → lists'.subset l l' → lists'.subset (lists'.cons a l) l' local infix ` ~ `:50 := lists.equiv /-- Equivalence of ZFA lists. Defined inductively. -/ add_decl_doc lists.equiv /-- Subset relation for ZFA lists. Defined inductively. -/ add_decl_doc lists'.subset namespace lists' instance : has_subset (lists' α tt) := ⟨lists'.subset⟩ /-- ZFA prelist membership. A ZFA list is in a ZFA prelist if some element of this ZFA prelist is equivalent as a ZFA list to this ZFA list. -/ instance {b} : has_mem (lists α) (lists' α b) := ⟨λ a l, ∃ a' ∈ l.to_list, a ~ a'⟩ theorem mem_def {b a} {l : lists' α b} : a ∈ l ↔ ∃ a' ∈ l.to_list, a ~ a' := iff.rfl @[simp] theorem mem_cons {a y l} : a ∈ @cons α y l ↔ a ~ y ∨ a ∈ l := by simp [mem_def, or_and_distrib_right, exists_or_distrib] theorem cons_subset {a} {l₁ l₂ : lists' α tt} : lists'.cons a l₁ ⊆ l₂ ↔ a ∈ l₂ ∧ l₁ ⊆ l₂ := begin refine ⟨λ h, _, λ ⟨⟨a', m, e⟩, s⟩, subset.cons e m s⟩, generalize_hyp h' : lists'.cons a l₁ = l₁' at h, cases h with l a' a'' l l' e m s, {cases a, cases h'}, cases a, cases a', cases h', exact ⟨⟨_, m, e⟩, s⟩ end theorem of_list_subset {l₁ l₂ : list (lists α)} (h : l₁ ⊆ l₂) : lists'.of_list l₁ ⊆ lists'.of_list l₂ := begin induction l₁, {exact subset.nil}, refine subset.cons (lists.equiv.refl _) _ (l₁_ih (list.subset_of_cons_subset h)), simp at h, simp [h] end @[refl] theorem subset.refl {l : lists' α tt} : l ⊆ l := by rw ← lists'.of_to_list l; exact of_list_subset (list.subset.refl _) theorem subset_nil {l : lists' α tt} : l ⊆ lists'.nil → l = lists'.nil := begin rw ← of_to_list l, induction to_list l; intro h, {refl}, rcases cons_subset.1 h with ⟨⟨_, ⟨⟩, _⟩, _⟩ end theorem mem_of_subset' {a} {l₁ l₂ : lists' α tt} (s : l₁ ⊆ l₂) (h : a ∈ l₁.to_list) : a ∈ l₂ := begin induction s with _ a a' l l' e m s IH, {cases h}, simp at h, rcases h with rfl|h, exacts [⟨_, m, e⟩, IH h] end theorem subset_def {l₁ l₂ : lists' α tt} : l₁ ⊆ l₂ ↔ ∀ a ∈ l₁.to_list, a ∈ l₂ := ⟨λ H a, mem_of_subset' H, λ H, begin rw ← of_to_list l₁, revert H, induction to_list l₁; intro, { exact subset.nil }, { simp at H, exact cons_subset.2 ⟨H.1, ih H.2⟩ } end⟩ end lists' namespace lists /-- Sends `a : α` to the corresponding atom in `lists α`. -/ @[pattern] def atom (a : α) : lists α := ⟨_, lists'.atom a⟩ /-- Converts a proper ZFA prelist to a ZFA list. -/ @[pattern] def of' (l : lists' α tt) : lists α := ⟨_, l⟩ /-- Converts a ZFA list to a `list` of ZFA lists. Atoms are sent to `[]`. -/ @[simp] def to_list : lists α → list (lists α) | ⟨b, l⟩ := l.to_list /-- Predicate stating that a ZFA list is proper. -/ def is_list (l : lists α) : Prop := l.1 /-- Converts a `list` of ZFA lists to a ZFA list. -/ def of_list (l : list (lists α)) : lists α := of' (lists'.of_list l) theorem is_list_to_list (l : list (lists α)) : is_list (of_list l) := eq.refl _ theorem to_of_list (l : list (lists α)) : to_list (of_list l) = l := by simp [of_list, of'] theorem of_to_list : ∀ {l : lists α}, is_list l → of_list (to_list l) = l | ⟨tt, l⟩ _ := by simp [of_list, of'] instance : inhabited (lists α) := ⟨of' lists'.nil⟩ instance [decidable_eq α] : decidable_eq (lists α) := by unfold lists; apply_instance instance [has_sizeof α] : has_sizeof (lists α) := by unfold lists; apply_instance /-- A recursion principle for pairs of ZFA lists and proper ZFA prelists. -/ def induction_mut (C : lists α → Sort*) (D : lists' α tt → Sort*) (C0 : ∀ a, C (atom a)) (C1 : ∀ l, D l → C (of' l)) (D0 : D lists'.nil) (D1 : ∀ a l, C a → D l → D (lists'.cons a l)) : pprod (∀ l, C l) (∀ l, D l) := begin suffices : ∀ {b} (l : lists' α b), pprod (C ⟨_, l⟩) (match b, l with | tt, l := D l | ff, l := punit end), { exact ⟨λ ⟨b, l⟩, (this _).1, λ l, (this l).2⟩ }, intros, induction l with a b a l IH₁ IH₂, { exact ⟨C0 _, ⟨⟩⟩ }, { exact ⟨C1 _ D0, D0⟩ }, { suffices, {exact ⟨C1 _ this, this⟩}, exact D1 ⟨_, _⟩ _ IH₁.1 IH₂.2 } end /-- Membership of ZFA list. A ZFA list belongs to a proper ZFA list if it belongs to the latter as a proper ZFA prelist. An atom has no members. -/ def mem (a : lists α) : lists α → Prop | ⟨ff, l⟩ := false | ⟨tt, l⟩ := a ∈ l instance : has_mem (lists α) (lists α) := ⟨mem⟩ theorem is_list_of_mem {a : lists α} : ∀ {l : lists α}, a ∈ l → is_list l | ⟨_, lists'.nil⟩ _ := rfl | ⟨_, lists'.cons' _ _⟩ _ := rfl theorem equiv.antisymm_iff {l₁ l₂ : lists' α tt} : of' l₁ ~ of' l₂ ↔ l₁ ⊆ l₂ ∧ l₂ ⊆ l₁ := begin refine ⟨λ h, _, λ ⟨h₁, h₂⟩, equiv.antisymm h₁ h₂⟩, cases h with _ _ _ h₁ h₂, { simp [lists'.subset.refl] }, { exact ⟨h₁, h₂⟩ } end attribute [refl] equiv.refl theorem equiv_atom {a} {l : lists α} : atom a ~ l ↔ atom a = l := ⟨λ h, by cases h; refl, λ h, h ▸ equiv.refl _⟩ theorem equiv.symm {l₁ l₂ : lists α} (h : l₁ ~ l₂) : l₂ ~ l₁ := by cases h with _ _ _ h₁ h₂; [refl, exact equiv.antisymm h₂ h₁] theorem equiv.trans : ∀ {l₁ l₂ l₃ : lists α}, l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃ := begin let trans := λ (l₁ : lists α), ∀ ⦃l₂ l₃⦄, l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃, suffices : pprod (∀ l₁, trans l₁) (∀ (l : lists' α tt) (l' ∈ l.to_list), trans l'), {exact this.1}, apply induction_mut, { intros a l₂ l₃ h₁ h₂, rwa ← equiv_atom.1 h₁ at h₂ }, { intros l₁ IH l₂ l₃ h₁ h₂, cases h₁ with _ _ l₂, {exact h₂}, cases h₂ with _ _ l₃, {exact h₁}, cases equiv.antisymm_iff.1 h₁ with hl₁ hr₁, cases equiv.antisymm_iff.1 h₂ with hl₂ hr₂, apply equiv.antisymm_iff.2; split; apply lists'.subset_def.2, { intros a₁ m₁, rcases lists'.mem_of_subset' hl₁ m₁ with ⟨a₂, m₂, e₁₂⟩, rcases lists'.mem_of_subset' hl₂ m₂ with ⟨a₃, m₃, e₂₃⟩, exact ⟨a₃, m₃, IH _ m₁ e₁₂ e₂₃⟩ }, { intros a₃ m₃, rcases lists'.mem_of_subset' hr₂ m₃ with ⟨a₂, m₂, e₃₂⟩, rcases lists'.mem_of_subset' hr₁ m₂ with ⟨a₁, m₁, e₂₁⟩, exact ⟨a₁, m₁, (IH _ m₁ e₂₁.symm e₃₂.symm).symm⟩ } }, { rintro _ ⟨⟩ }, { intros a l IH₁ IH₂, simpa [IH₁] using IH₂ } end instance : setoid (lists α) := ⟨(~), equiv.refl, @equiv.symm _, @equiv.trans _⟩ section decidable @[simp] def equiv.decidable_meas : (psum (Σ' (l₁ : lists α), lists α) $ psum (Σ' (l₁ : lists' α tt), lists' α tt) Σ' (a : lists α), lists' α tt) → ℕ | (psum.inl ⟨l₁, l₂⟩) := sizeof l₁ + sizeof l₂ | (psum.inr $ psum.inl ⟨l₁, l₂⟩) := sizeof l₁ + sizeof l₂ | (psum.inr $ psum.inr ⟨l₁, l₂⟩) := sizeof l₁ + sizeof l₂ open well_founded_tactics theorem sizeof_pos {b} (l : lists' α b) : 0 < sizeof l := by cases l; unfold_sizeof; trivial_nat_lt theorem lt_sizeof_cons' {b} (a : lists' α b) (l) : sizeof (⟨b, a⟩ : lists α) < sizeof (lists'.cons' a l) := by {unfold_sizeof, apply sizeof_pos} @[instance] mutual def equiv.decidable, subset.decidable, mem.decidable [decidable_eq α] with equiv.decidable : ∀ l₁ l₂ : lists α, decidable (l₁ ~ l₂) | ⟨ff, l₁⟩ ⟨ff, l₂⟩ := decidable_of_iff' (l₁ = l₂) $ by cases l₁; refine equiv_atom.trans (by simp [atom]) | ⟨ff, l₁⟩ ⟨tt, l₂⟩ := is_false $ by rintro ⟨⟩ | ⟨tt, l₁⟩ ⟨ff, l₂⟩ := is_false $ by rintro ⟨⟩ | ⟨tt, l₁⟩ ⟨tt, l₂⟩ := begin haveI := have sizeof l₁ + sizeof l₂ < sizeof (⟨tt, l₁⟩ : lists α) + sizeof (⟨tt, l₂⟩ : lists α), by default_dec_tac, subset.decidable l₁ l₂, haveI := have sizeof l₂ + sizeof l₁ < sizeof (⟨tt, l₁⟩ : lists α) + sizeof (⟨tt, l₂⟩ : lists α), by default_dec_tac, subset.decidable l₂ l₁, exact decidable_of_iff' _ equiv.antisymm_iff, end with subset.decidable : ∀ l₁ l₂ : lists' α tt, decidable (l₁ ⊆ l₂) | lists'.nil l₂ := is_true subset.nil | (@lists'.cons' _ b a l₁) l₂ := begin haveI := have sizeof (⟨b, a⟩ : lists α) + sizeof l₂ < sizeof (lists'.cons' a l₁) + sizeof l₂, from add_lt_add_right (lt_sizeof_cons' _ _) _, mem.decidable ⟨b, a⟩ l₂, haveI := have sizeof l₁ + sizeof l₂ < sizeof (lists'.cons' a l₁) + sizeof l₂, by default_dec_tac, subset.decidable l₁ l₂, exact decidable_of_iff' _ (@lists'.cons_subset _ ⟨_, _⟩ _ _) end with mem.decidable : ∀ (a : lists α) (l : lists' α tt), decidable (a ∈ l) | a lists'.nil := is_false $ by rintro ⟨_, ⟨⟩, _⟩ | a (lists'.cons' b l₂) := begin haveI := have sizeof a + sizeof (⟨_, b⟩ : lists α) < sizeof a + sizeof (lists'.cons' b l₂), from add_lt_add_left (lt_sizeof_cons' _ _) _, equiv.decidable a ⟨_, b⟩, haveI := have sizeof a + sizeof l₂ < sizeof a + sizeof (lists'.cons' b l₂), by default_dec_tac, mem.decidable a l₂, refine decidable_of_iff' (a ~ ⟨_, b⟩ ∨ a ∈ l₂) _, rw ← lists'.mem_cons, refl end using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf equiv.decidable_meas⟩], dec_tac := `[assumption] } end decidable end lists namespace lists' theorem mem_equiv_left {l : lists' α tt} : ∀ {a a'}, a ~ a' → (a ∈ l ↔ a' ∈ l) := suffices ∀ {a a'}, a ~ a' → a ∈ l → a' ∈ l, from λ a a' e, ⟨this e, this e.symm⟩, λ a₁ a₂ e₁ ⟨a₃, m₃, e₂⟩, ⟨_, m₃, e₁.symm.trans e₂⟩ theorem mem_of_subset {a} {l₁ l₂ : lists' α tt} (s : l₁ ⊆ l₂) : a ∈ l₁ → a ∈ l₂ | ⟨a', m, e⟩ := (mem_equiv_left e).2 (mem_of_subset' s m) theorem subset.trans {l₁ l₂ l₃ : lists' α tt} (h₁ : l₁ ⊆ l₂) (h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := subset_def.2 $ λ a₁ m₁, mem_of_subset h₂ $ mem_of_subset' h₁ m₁ end lists' def finsets (α : Type*) := quotient (@lists.setoid α) namespace finsets instance : has_emptyc (finsets α) := ⟨⟦lists.of' lists'.nil⟧⟩ instance : inhabited (finsets α) := ⟨∅⟩ instance [decidable_eq α] : decidable_eq (finsets α) := by unfold finsets; apply_instance end finsets
1b778b065cd197e44d38f5b5d8900066c2cc6d3a
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/tests/lean/run/all_goals.lean
54b9df9be0962cde27f8d7819f4123026a70de00
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
245
lean
import data.nat open nat attribute nat.add [unfold-c 2] attribute nat.rec_on [unfold-c 2] example (a b c : nat) : a + 0 = 0 + a ∧ b + 0 = 0 + b := begin apply and.intro, all_goals esimp{of_num}, all_goals (state; rewrite zero_add) end
b596d2cfd60e3a3ef0b2f83ae261b1181dd09beb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/int/gcd.lean
e05c44e65c5e46dec1182932b1f79db311c1f354
[ "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
25,801
lean
/- Copyright (c) 2018 Guy Leroy. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes Hölzl, Mario Carneiro -/ import data.nat.gcd.basic import tactic.norm_num /-! # Extended GCD and divisibility over ℤ > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * Given `x y : ℕ`, `xgcd x y` computes the pair of integers `(a, b)` such that `gcd x y = x * a + y * b`. `gcd_a x y` and `gcd_b x y` are defined to be `a` and `b`, respectively. ## Main statements * `gcd_eq_gcd_ab`: Bézout's lemma, given `x y : ℕ`, `gcd x y = x * gcd_a x y + y * gcd_b x y`. ## Tags Bézout's lemma, Bezout's lemma -/ /-! ### Extended Euclidean algorithm -/ namespace nat /-- Helper function for the extended GCD algorithm (`nat.xgcd`). -/ def xgcd_aux : ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ | 0 s t r' s' t' := (r', s', t') | r@(succ _) s t r' s' t' := have r' % r < r, from mod_lt _ $ succ_pos _, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t @[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by simp [xgcd_aux] theorem xgcd_aux_rec {r s t r' s' t'} (h : 0 < r) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by cases r; [exact absurd h (lt_irrefl _), {simp only [xgcd_aux], refl}] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : ℕ) : ℤ × ℤ := (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 : ℕ) : ℤ := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : ℕ) : ℤ := (xgcd x y).2 @[simp] theorem gcd_a_zero_left {s : ℕ} : gcd_a 0 s = 0 := by { unfold gcd_a, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_b_zero_left {s : ℕ} : gcd_b 0 s = 1 := by { unfold gcd_b, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_a_zero_right {s : ℕ} (h : s ≠ 0) : gcd_a s 0 = 1 := begin unfold gcd_a xgcd, induction s, { exact absurd rfl h, }, { simp [xgcd_aux], } end @[simp] theorem gcd_b_zero_right {s : ℕ} (h : s ≠ 0) : gcd_b s 0 = 0 := begin unfold gcd_b xgcd, induction s, { exact absurd rfl h, }, { simp [xgcd_aux], } end @[simp] theorem xgcd_aux_fst (x y) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by simp) (λ x y h IH s t s' t', by simp [xgcd_aux_rec, h, IH]; rw ← gcd_rec) theorem xgcd_aux_val (x y) : 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]; cases xgcd_aux x 1 0 y 0 1; refl theorem xgcd_val (x y) : xgcd x y = (gcd_a x y, gcd_b x y) := by unfold gcd_a gcd_b; cases xgcd x y; refl section parameters (x y : ℕ) private def P : ℕ × ℤ × ℤ → Prop | (r, s, t) := (r : ℤ) = x * s + y * t theorem xgcd_aux_P {r r'} : ∀ {s t s' t'}, P (r, s, t) → P (r', s', t') → P (xgcd_aux r s t r' s' t') := gcd.induction r r' (by simp) $ λ a b h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *, rw [int.mod_def], generalize : (b / a : ℤ) = k, rw [p, p'], simp [mul_add, mul_comm, mul_left_comm, add_comm, add_left_comm, sub_eq_neg_add, mul_assoc] end /-- **Bézout's lemma**: given `x y : ℕ`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and `b = gcd_b x y` are computed by the extended Euclidean algorithm. -/ theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y := by have := @xgcd_aux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]); rwa [xgcd_aux_val, xgcd_val] at this end lemma exists_mul_mod_eq_gcd {k n : ℕ} (hk : gcd n k < k) : ∃ m, n * m % k = gcd n k := begin have hk' := int.coe_nat_ne_zero.mpr (ne_of_gt (lt_of_le_of_lt (zero_le (gcd n k)) hk)), have key := congr_arg (λ m, int.nat_mod m k) (gcd_eq_gcd_ab n k), simp_rw int.nat_mod at key, rw [int.add_mul_mod_self_left, ←int.coe_nat_mod, int.to_nat_coe_nat, mod_eq_of_lt hk] at key, refine ⟨(n.gcd_a k % k).to_nat, eq.trans (int.coe_nat_inj _) key.symm⟩, rw [int.coe_nat_mod, int.coe_nat_mul, int.to_nat_of_nonneg (int.mod_nonneg _ hk'), int.to_nat_of_nonneg (int.mod_nonneg _ hk'), int.mul_mod, int.mod_mod, ←int.mul_mod], end lemma exists_mul_mod_eq_one_of_coprime {k n : ℕ} (hkn : coprime n k) (hk : 1 < k) : ∃ m, n * m % k = 1 := Exists.cases_on (exists_mul_mod_eq_gcd (lt_of_le_of_lt (le_of_eq hkn) hk)) (λ m hm, ⟨m, hm.trans hkn⟩) end nat /-! ### Divisibility over ℤ -/ namespace int protected lemma coe_nat_gcd (m n : ℕ) : int.gcd ↑m ↑n = nat.gcd m n := rfl /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a : ℤ → ℤ → ℤ | (of_nat m) n := m.gcd_a n.nat_abs | -[1+ m] n := -m.succ.gcd_a n.nat_abs /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b : ℤ → ℤ → ℤ | m (of_nat n) := m.nat_abs.gcd_b n | m -[1+ n] := -m.nat_abs.gcd_b n.succ /-- **Bézout's lemma** -/ theorem gcd_eq_gcd_ab : ∀ x y : ℤ, (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y | (m : ℕ) (n : ℕ) := nat.gcd_eq_gcd_ab _ _ | (m : ℕ) -[1+ n] := show (_ : ℤ) = _ + -(n+1) * -_, by rw neg_mul_neg; apply nat.gcd_eq_gcd_ab | -[1+ m] (n : ℕ) := show (_ : ℤ) = -(m+1) * -_ + _ , by rw neg_mul_neg; apply nat.gcd_eq_gcd_ab | -[1+ m] -[1+ n] := show (_ : ℤ) = -(m+1) * -_ + -(n+1) * -_, by { rw [neg_mul_neg, neg_mul_neg], apply nat.gcd_eq_gcd_ab } theorem nat_abs_div (a b : ℤ) (H : b ∣ a) : nat_abs (a / b) = (nat_abs a) / (nat_abs b) := begin cases (nat.eq_zero_or_pos (nat_abs b)), {rw eq_zero_of_nat_abs_eq_zero h, simp [int.div_zero]}, calc nat_abs (a / b) = nat_abs (a / b) * 1 : by rw mul_one ... = nat_abs (a / b) * (nat_abs b / nat_abs b) : by rw nat.div_self h ... = nat_abs (a / b) * nat_abs b / nat_abs b : by rw (nat.mul_div_assoc _ dvd_rfl) ... = nat_abs (a / b * b) / nat_abs b : by rw (nat_abs_mul (a / b) b) ... = nat_abs a / nat_abs b : by rw int.div_mul_cancel H, end theorem dvd_of_mul_dvd_mul_left {i j k : ℤ} (k_non_zero : k ≠ 0) (H : k * i ∣ k * j) : i ∣ j := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, mul_left_cancel₀ k_non_zero H1⟩) theorem dvd_of_mul_dvd_mul_right {i j k : ℤ} (k_non_zero : k ≠ 0) (H : i * k ∣ j * k) : i ∣ j := by rw [mul_comm i k, mul_comm j k] at H; exact dvd_of_mul_dvd_mul_left k_non_zero H /-- ℤ specific version of least common multiple. -/ def lcm (i j : ℤ) : ℕ := nat.lcm (nat_abs i) (nat_abs j) theorem lcm_def (i j : ℤ) : lcm i j = nat.lcm (nat_abs i) (nat_abs j) := rfl protected lemma coe_nat_lcm (m n : ℕ) : int.lcm ↑m ↑n = nat.lcm m n := rfl theorem gcd_dvd_left (i j : ℤ) : (gcd i j : ℤ) ∣ i := dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_left _ _ theorem gcd_dvd_right (i j : ℤ) : (gcd i j : ℤ) ∣ j := dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_right _ _ theorem dvd_gcd {i j k : ℤ} (h1 : k ∣ i) (h2 : k ∣ j) : k ∣ gcd i j := nat_abs_dvd.1 $ coe_nat_dvd.2 $ nat.dvd_gcd (nat_abs_dvd_iff_dvd.2 h1) (nat_abs_dvd_iff_dvd.2 h2) theorem gcd_mul_lcm (i j : ℤ) : gcd i j * lcm i j = nat_abs (i * j) := by rw [int.gcd, int.lcm, nat.gcd_mul_lcm, nat_abs_mul] theorem gcd_comm (i j : ℤ) : gcd i j = gcd j i := nat.gcd_comm _ _ theorem gcd_assoc (i j k : ℤ) : gcd (gcd i j) k = gcd i (gcd j k) := nat.gcd_assoc _ _ _ @[simp] theorem gcd_self (i : ℤ) : gcd i i = nat_abs i := by simp [gcd] @[simp] theorem gcd_zero_left (i : ℤ) : gcd 0 i = nat_abs i := by simp [gcd] @[simp] theorem gcd_zero_right (i : ℤ) : gcd i 0 = nat_abs i := by simp [gcd] @[simp] theorem gcd_one_left (i : ℤ) : gcd 1 i = 1 := nat.gcd_one_left _ @[simp] theorem gcd_one_right (i : ℤ) : gcd i 1 = 1 := nat.gcd_one_right _ @[simp] lemma gcd_neg_right {x y : ℤ} : gcd x (-y) = gcd x y := by rw [int.gcd, int.gcd, nat_abs_neg] @[simp] lemma gcd_neg_left {x y : ℤ} : gcd (-x) y = gcd x y := by rw [int.gcd, int.gcd, nat_abs_neg] theorem gcd_mul_left (i j k : ℤ) : gcd (i * j) (i * k) = nat_abs i * gcd j k := by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_left } theorem gcd_mul_right (i j k : ℤ) : gcd (i * j) (k * j) = gcd i k * nat_abs j := by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_right } theorem gcd_pos_of_ne_zero_left {i : ℤ} (j : ℤ) (hi : i ≠ 0) : 0 < gcd i j := nat.gcd_pos_of_pos_left _ $ nat_abs_pos_of_ne_zero hi theorem gcd_pos_of_ne_zero_right (i : ℤ) {j : ℤ} (hj : j ≠ 0) : 0 < gcd i j := nat.gcd_pos_of_pos_right _ $ nat_abs_pos_of_ne_zero hj theorem gcd_eq_zero_iff {i j : ℤ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin rw int.gcd, split, { intro h, exact ⟨nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_left h), nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_right h)⟩ }, { intro h, rw [nat_abs_eq_zero.mpr h.left, nat_abs_eq_zero.mpr h.right], apply nat.gcd_zero_left } end theorem gcd_pos_iff {i j : ℤ} : 0 < gcd i j ↔ i ≠ 0 ∨ j ≠ 0 := pos_iff_ne_zero.trans $ gcd_eq_zero_iff.not.trans not_and_distrib theorem gcd_div {i j k : ℤ} (H1 : k ∣ i) (H2 : k ∣ j) : gcd (i / k) (j / k) = gcd i j / nat_abs k := by rw [gcd, nat_abs_div i k H1, nat_abs_div j k H2]; exact nat.gcd_div (nat_abs_dvd_iff_dvd.mpr H1) (nat_abs_dvd_iff_dvd.mpr H2) theorem gcd_div_gcd_div_gcd {i j : ℤ} (H : 0 < gcd i j) : gcd (i / gcd i j) (j / gcd i j) = 1 := begin rw [gcd_div (gcd_dvd_left i j) (gcd_dvd_right i j)], rw [nat_abs_of_nat, nat.div_self H] end theorem gcd_dvd_gcd_of_dvd_left {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd i j ∣ gcd k j := int.coe_nat_dvd.1 $ dvd_gcd ((gcd_dvd_left i j).trans H) (gcd_dvd_right i j) theorem gcd_dvd_gcd_of_dvd_right {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd j i ∣ gcd j k := int.coe_nat_dvd.1 $ dvd_gcd (gcd_dvd_left j i) ((gcd_dvd_right j i).trans H) theorem gcd_dvd_gcd_mul_left (i j k : ℤ) : gcd i j ∣ gcd (k * i) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (i j k : ℤ) : gcd i j ∣ gcd (i * k) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (i j k : ℤ) : gcd i j ∣ gcd i (k * j) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (i j k : ℤ) : gcd i j ∣ gcd i (j * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {i j : ℤ} (H : i ∣ j) : gcd i j = nat_abs i := nat.dvd_antisymm (by unfold gcd; exact nat.gcd_dvd_left _ _) (by unfold gcd; exact nat.dvd_gcd dvd_rfl (nat_abs_dvd_iff_dvd.mpr H)) theorem gcd_eq_right {i j : ℤ} (H : j ∣ i) : gcd i j = nat_abs j := by rw [gcd_comm, gcd_eq_left H] theorem ne_zero_of_gcd {x y : ℤ} (hc : gcd x y ≠ 0) : x ≠ 0 ∨ y ≠ 0 := begin contrapose! hc, rw [hc.left, hc.right, gcd_zero_right, nat_abs_zero] end theorem exists_gcd_one {m n : ℤ} (H : 0 < gcd m n) : ∃ (m' n' : ℤ), gcd m' n' = 1 ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, gcd_div_gcd_div_gcd H, (int.div_mul_cancel (gcd_dvd_left m n)).symm, (int.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_gcd_one' {m n : ℤ} (H : 0 < gcd m n) : ∃ (g : ℕ) (m' n' : ℤ), 0 < g ∧ gcd m' n' = 1 ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_gcd_one H in ⟨_, m', n', H, h⟩ theorem pow_dvd_pow_iff {m n : ℤ} {k : ℕ} (k0 : 0 < k) : m ^ k ∣ n ^ k ↔ m ∣ n := begin refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩, apply int.nat_abs_dvd_iff_dvd.mp, apply (nat.pow_dvd_pow_iff k0).mp, rw [← int.nat_abs_pow, ← int.nat_abs_pow], exact int.nat_abs_dvd_iff_dvd.mpr h end lemma gcd_dvd_iff {a b : ℤ} {n : ℕ} : gcd a b ∣ n ↔ ∃ x y : ℤ, ↑n = a * x + b * y := begin split, { intro h, rw [← nat.mul_div_cancel' h, int.coe_nat_mul, gcd_eq_gcd_ab, add_mul, mul_assoc, mul_assoc], refine ⟨_, _, rfl⟩, }, { rintro ⟨x, y, h⟩, rw [←int.coe_nat_dvd, h], exact dvd_add (dvd_mul_of_dvd_left (gcd_dvd_left a b) _) (dvd_mul_of_dvd_left (gcd_dvd_right a b) y) } end lemma gcd_greatest {a b d : ℤ} (hd_pos : 0 ≤ d) (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : ℤ, e ∣ a → e ∣ b → e ∣ d) : d = gcd a b := dvd_antisymm hd_pos (coe_zero_le (gcd a b)) (dvd_gcd hda hdb) (hd _ (gcd_dvd_left a b) (gcd_dvd_right a b)) /-- Euclid's lemma: if `a ∣ b * c` and `gcd a c = 1` then `a ∣ b`. Compare with `is_coprime.dvd_of_dvd_mul_left` and `unique_factorization_monoid.dvd_of_dvd_mul_left_of_no_prime_factors` -/ lemma dvd_of_dvd_mul_left_of_gcd_one {a b c : ℤ} (habc : a ∣ b * c) (hab : gcd a c = 1) : a ∣ b := begin have := gcd_eq_gcd_ab a c, simp only [hab, int.coe_nat_zero, int.coe_nat_succ, zero_add] at this, have : b * a * gcd_a a c + b * c * gcd_b a c = b, { simp [mul_assoc, ←mul_add, ←this] }, rw ←this, exact dvd_add (dvd_mul_of_dvd_left (dvd_mul_left a b) _) (dvd_mul_of_dvd_left habc _), end /-- Euclid's lemma: if `a ∣ b * c` and `gcd a b = 1` then `a ∣ c`. Compare with `is_coprime.dvd_of_dvd_mul_right` and `unique_factorization_monoid.dvd_of_dvd_mul_right_of_no_prime_factors` -/ lemma dvd_of_dvd_mul_right_of_gcd_one {a b c : ℤ} (habc : a ∣ b * c) (hab : gcd a b = 1) : a ∣ c := by { rw mul_comm at habc, exact dvd_of_dvd_mul_left_of_gcd_one habc hab } /-- For nonzero integers `a` and `b`, `gcd a b` is the smallest positive natural number that can be written in the form `a * x + b * y` for some pair of integers `x` and `y` -/ theorem gcd_least_linear {a b : ℤ} (ha : a ≠ 0) : is_least { n : ℕ | 0 < n ∧ ∃ x y : ℤ, ↑n = a * x + b * y } (a.gcd b) := begin simp_rw ←gcd_dvd_iff, split, { simpa [and_true, dvd_refl, set.mem_set_of_eq] using gcd_pos_of_ne_zero_left b ha }, { simp only [lower_bounds, and_imp, set.mem_set_of_eq], exact λ n hn_pos hn, nat.le_of_dvd hn_pos hn }, end /-! ### lcm -/ theorem lcm_comm (i j : ℤ) : lcm i j = lcm j i := by { rw [int.lcm, int.lcm], exact nat.lcm_comm _ _ } theorem lcm_assoc (i j k : ℤ) : lcm (lcm i j) k = lcm i (lcm j k) := by { rw [int.lcm, int.lcm, int.lcm, int.lcm, nat_abs_of_nat, nat_abs_of_nat], apply nat.lcm_assoc } @[simp] theorem lcm_zero_left (i : ℤ) : lcm 0 i = 0 := by { rw [int.lcm], apply nat.lcm_zero_left } @[simp] theorem lcm_zero_right (i : ℤ) : lcm i 0 = 0 := by { rw [int.lcm], apply nat.lcm_zero_right } @[simp] theorem lcm_one_left (i : ℤ) : lcm 1 i = nat_abs i := by { rw int.lcm, apply nat.lcm_one_left } @[simp] theorem lcm_one_right (i : ℤ) : lcm i 1 = nat_abs i := by { rw int.lcm, apply nat.lcm_one_right } @[simp] theorem lcm_self (i : ℤ) : lcm i i = nat_abs i := by { rw int.lcm, apply nat.lcm_self } theorem dvd_lcm_left (i j : ℤ) : i ∣ lcm i j := by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_left } theorem dvd_lcm_right (i j : ℤ) : j ∣ lcm i j := by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_right } theorem lcm_dvd {i j k : ℤ} : i ∣ k → j ∣ k → (lcm i j : ℤ) ∣ k := begin rw int.lcm, intros hi hj, exact coe_nat_dvd_left.mpr (nat.lcm_dvd (nat_abs_dvd_iff_dvd.mpr hi) (nat_abs_dvd_iff_dvd.mpr hj)) end end int lemma pow_gcd_eq_one {M : Type*} [monoid M] (x : M) {m n : ℕ} (hm : x ^ m = 1) (hn : x ^ n = 1) : x ^ m.gcd n = 1 := begin cases m, { simp only [hn, nat.gcd_zero_left] }, lift x to Mˣ using is_unit_of_pow_eq_one hm m.succ_ne_zero, simp only [← units.coe_pow] at *, rw [← units.coe_one, ← zpow_coe_nat, ← units.ext_iff] at *, simp only [nat.gcd_eq_gcd_ab, zpow_add, zpow_mul, hm, hn, one_zpow, one_mul] end lemma gcd_nsmul_eq_zero {M : Type*} [add_monoid M] (x : M) {m n : ℕ} (hm : m • x = 0) (hn : n • x = 0) : (m.gcd n) • x = 0 := begin apply multiplicative.of_add.injective, rw [of_add_nsmul, of_add_zero, pow_gcd_eq_one]; rwa [←of_add_nsmul, ←of_add_zero, equiv.apply_eq_iff_eq] end attribute [to_additive gcd_nsmul_eq_zero] pow_gcd_eq_one /-! ### GCD prover -/ open norm_num namespace tactic namespace norm_num lemma int_gcd_helper' {d : ℕ} {x y a b : ℤ} (h₁ : (d:ℤ) ∣ x) (h₂ : (d:ℤ) ∣ y) (h₃ : x * a + y * b = d) : int.gcd x y = d := begin refine nat.dvd_antisymm _ (int.coe_nat_dvd.1 (int.dvd_gcd h₁ h₂)), rw [← int.coe_nat_dvd, ← h₃], apply dvd_add, { exact (int.gcd_dvd_left _ _).mul_right _ }, { exact (int.gcd_dvd_right _ _).mul_right _ } end lemma nat_gcd_helper_dvd_left (x y a : ℕ) (h : x * a = y) : nat.gcd x y = x := nat.gcd_eq_left ⟨a, h.symm⟩ lemma nat_gcd_helper_dvd_right (x y a : ℕ) (h : y * a = x) : nat.gcd x y = y := nat.gcd_eq_right ⟨a, h.symm⟩ lemma nat_gcd_helper_2 (d x y a b u v tx ty : ℕ) (hu : d * u = x) (hv : d * v = y) (hx : x * a = tx) (hy : y * b = ty) (h : ty + d = tx) : nat.gcd x y = d := begin rw ← int.coe_nat_gcd, apply @int_gcd_helper' _ _ _ a (-b) (int.coe_nat_dvd.2 ⟨_, hu.symm⟩) (int.coe_nat_dvd.2 ⟨_, hv.symm⟩), rw [mul_neg, ← sub_eq_add_neg, sub_eq_iff_eq_add'], norm_cast, rw [hx, hy, h] end lemma nat_gcd_helper_1 (d x y a b u v tx ty : ℕ) (hu : d * u = x) (hv : d * v = y) (hx : x * a = tx) (hy : y * b = ty) (h : tx + d = ty) : nat.gcd x y = d := (nat.gcd_comm _ _).trans $ nat_gcd_helper_2 _ _ _ _ _ _ _ _ _ hv hu hy hx h lemma nat_lcm_helper (x y d m n : ℕ) (hd : nat.gcd x y = d) (d0 : 0 < d) (xy : x * y = n) (dm : d * m = n) : nat.lcm x y = m := mul_right_injective₀ d0.ne' $ by rw [dm, ← xy, ← hd, nat.gcd_mul_lcm] lemma nat_coprime_helper_zero_left (x : ℕ) (h : 1 < x) : ¬ nat.coprime 0 x := mt (nat.coprime_zero_left _).1 $ ne_of_gt h lemma nat_coprime_helper_zero_right (x : ℕ) (h : 1 < x) : ¬ nat.coprime x 0 := mt (nat.coprime_zero_right _).1 $ ne_of_gt h lemma nat_coprime_helper_1 (x y a b tx ty : ℕ) (hx : x * a = tx) (hy : y * b = ty) (h : tx + 1 = ty) : nat.coprime x y := nat_gcd_helper_1 _ _ _ _ _ _ _ _ _ (one_mul _) (one_mul _) hx hy h lemma nat_coprime_helper_2 (x y a b tx ty : ℕ) (hx : x * a = tx) (hy : y * b = ty) (h : ty + 1 = tx) : nat.coprime x y := nat_gcd_helper_2 _ _ _ _ _ _ _ _ _ (one_mul _) (one_mul _) hx hy h lemma nat_not_coprime_helper (d x y u v : ℕ) (hu : d * u = x) (hv : d * v = y) (h : 1 < d) : ¬ nat.coprime x y := nat.not_coprime_of_dvd_of_dvd h ⟨_, hu.symm⟩ ⟨_, hv.symm⟩ lemma int_gcd_helper (x y : ℤ) (nx ny d : ℕ) (hx : (nx:ℤ) = x) (hy : (ny:ℤ) = y) (h : nat.gcd nx ny = d) : int.gcd x y = d := by rwa [← hx, ← hy, int.coe_nat_gcd] lemma int_gcd_helper_neg_left (x y : ℤ) (d : ℕ) (h : int.gcd x y = d) : int.gcd (-x) y = d := by rw int.gcd at h ⊢; rwa int.nat_abs_neg lemma int_gcd_helper_neg_right (x y : ℤ) (d : ℕ) (h : int.gcd x y = d) : int.gcd x (-y) = d := by rw int.gcd at h ⊢; rwa int.nat_abs_neg lemma int_lcm_helper (x y : ℤ) (nx ny d : ℕ) (hx : (nx:ℤ) = x) (hy : (ny:ℤ) = y) (h : nat.lcm nx ny = d) : int.lcm x y = d := by rwa [← hx, ← hy, int.coe_nat_lcm] lemma int_lcm_helper_neg_left (x y : ℤ) (d : ℕ) (h : int.lcm x y = d) : int.lcm (-x) y = d := by rw int.lcm at h ⊢; rwa int.nat_abs_neg lemma int_lcm_helper_neg_right (x y : ℤ) (d : ℕ) (h : int.lcm x y = d) : int.lcm x (-y) = d := by rw int.lcm at h ⊢; rwa int.nat_abs_neg /-- Evaluates the `nat.gcd` function. -/ meta def prove_gcd_nat (c : instance_cache) (ex ey : expr) : tactic (instance_cache × expr × expr) := do x ← ex.to_nat, y ← ey.to_nat, match x, y with | 0, _ := pure (c, ey, `(nat.gcd_zero_left).mk_app [ey]) | _, 0 := pure (c, ex, `(nat.gcd_zero_right).mk_app [ex]) | 1, _ := pure (c, `(1:ℕ), `(nat.gcd_one_left).mk_app [ey]) | _, 1 := pure (c, `(1:ℕ), `(nat.gcd_one_right).mk_app [ex]) | _, _ := do let (d, a, b) := nat.xgcd_aux x 1 0 y 0 1, if d = x then do (c, ea) ← c.of_nat (y / x), (c, _, p) ← prove_mul_nat c ex ea, pure (c, ex, `(nat_gcd_helper_dvd_left).mk_app [ex, ey, ea, p]) else if d = y then do (c, ea) ← c.of_nat (x / y), (c, _, p) ← prove_mul_nat c ey ea, pure (c, ey, `(nat_gcd_helper_dvd_right).mk_app [ex, ey, ea, p]) else do (c, ed) ← c.of_nat d, (c, ea) ← c.of_nat a.nat_abs, (c, eb) ← c.of_nat b.nat_abs, (c, eu) ← c.of_nat (x / d), (c, ev) ← c.of_nat (y / d), (c, _, pu) ← prove_mul_nat c ed eu, (c, _, pv) ← prove_mul_nat c ed ev, (c, etx, px) ← prove_mul_nat c ex ea, (c, ety, py) ← prove_mul_nat c ey eb, (c, p) ← if a ≥ 0 then prove_add_nat c ety ed etx else prove_add_nat c etx ed ety, let pf : expr := if a ≥ 0 then `(nat_gcd_helper_2) else `(nat_gcd_helper_1), pure (c, ed, pf.mk_app [ed, ex, ey, ea, eb, eu, ev, etx, ety, pu, pv, px, py, p]) end /-- Evaluates the `nat.lcm` function. -/ meta def prove_lcm_nat (c : instance_cache) (ex ey : expr) : tactic (instance_cache × expr × expr) := do x ← ex.to_nat, y ← ey.to_nat, match x, y with | 0, _ := pure (c, `(0:ℕ), `(nat.lcm_zero_left).mk_app [ey]) | _, 0 := pure (c, `(0:ℕ), `(nat.lcm_zero_right).mk_app [ex]) | 1, _ := pure (c, ey, `(nat.lcm_one_left).mk_app [ey]) | _, 1 := pure (c, ex, `(nat.lcm_one_right).mk_app [ex]) | _, _ := do (c, ed, pd) ← prove_gcd_nat c ex ey, (c, p0) ← prove_pos c ed, (c, en, xy) ← prove_mul_nat c ex ey, d ← ed.to_nat, (c, em) ← c.of_nat ((x * y) / d), (c, _, dm) ← prove_mul_nat c ed em, pure (c, em, `(nat_lcm_helper).mk_app [ex, ey, ed, em, en, pd, p0, xy, dm]) end /-- Evaluates the `int.gcd` function. -/ meta def prove_gcd_int (zc nc : instance_cache) : expr → expr → tactic (instance_cache × instance_cache × expr × expr) | x y := match match_neg x with | some x := do (zc, nc, d, p) ← prove_gcd_int x y, pure (zc, nc, d, `(int_gcd_helper_neg_left).mk_app [x, y, d, p]) | none := match match_neg y with | some y := do (zc, nc, d, p) ← prove_gcd_int x y, pure (zc, nc, d, `(int_gcd_helper_neg_right).mk_app [x, y, d, p]) | none := do (zc, nc, nx, px) ← prove_nat_uncast zc nc x, (zc, nc, ny, py) ← prove_nat_uncast zc nc y, (nc, d, p) ← prove_gcd_nat nc nx ny, pure (zc, nc, d, `(int_gcd_helper).mk_app [x, y, nx, ny, d, px, py, p]) end end /-- Evaluates the `int.lcm` function. -/ meta def prove_lcm_int (zc nc : instance_cache) : expr → expr → tactic (instance_cache × instance_cache × expr × expr) | x y := match match_neg x with | some x := do (zc, nc, d, p) ← prove_lcm_int x y, pure (zc, nc, d, `(int_lcm_helper_neg_left).mk_app [x, y, d, p]) | none := match match_neg y with | some y := do (zc, nc, d, p) ← prove_lcm_int x y, pure (zc, nc, d, `(int_lcm_helper_neg_right).mk_app [x, y, d, p]) | none := do (zc, nc, nx, px) ← prove_nat_uncast zc nc x, (zc, nc, ny, py) ← prove_nat_uncast zc nc y, (nc, d, p) ← prove_lcm_nat nc nx ny, pure (zc, nc, d, `(int_lcm_helper).mk_app [x, y, nx, ny, d, px, py, p]) end end /-- Evaluates the `nat.coprime` function. -/ meta def prove_coprime_nat (c : instance_cache) (ex ey : expr) : tactic (instance_cache × (expr ⊕ expr)) := do x ← ex.to_nat, y ← ey.to_nat, match x, y with | 1, _ := pure (c, sum.inl $ `(nat.coprime_one_left).mk_app [ey]) | _, 1 := pure (c, sum.inl $ `(nat.coprime_one_right).mk_app [ex]) | 0, 0 := pure (c, sum.inr `(nat.not_coprime_zero_zero)) | 0, _ := do c ← mk_instance_cache `(ℕ), (c, p) ← prove_lt_nat c `(1) ey, pure (c, sum.inr $ `(nat_coprime_helper_zero_left).mk_app [ey, p]) | _, 0 := do c ← mk_instance_cache `(ℕ), (c, p) ← prove_lt_nat c `(1) ex, pure (c, sum.inr $ `(nat_coprime_helper_zero_right).mk_app [ex, p]) | _, _ := do c ← mk_instance_cache `(ℕ), let (d, a, b) := nat.xgcd_aux x 1 0 y 0 1, if d = 1 then do (c, ea) ← c.of_nat a.nat_abs, (c, eb) ← c.of_nat b.nat_abs, (c, etx, px) ← prove_mul_nat c ex ea, (c, ety, py) ← prove_mul_nat c ey eb, (c, p) ← if a ≥ 0 then prove_add_nat c ety `(1) etx else prove_add_nat c etx `(1) ety, let pf : expr := if a ≥ 0 then `(nat_coprime_helper_2) else `(nat_coprime_helper_1), pure (c, sum.inl $ pf.mk_app [ex, ey, ea, eb, etx, ety, px, py, p]) else do (c, ed) ← c.of_nat d, (c, eu) ← c.of_nat (x / d), (c, ev) ← c.of_nat (y / d), (c, _, pu) ← prove_mul_nat c ed eu, (c, _, pv) ← prove_mul_nat c ed ev, (c, p) ← prove_lt_nat c `(1) ed, pure (c, sum.inr $ `(nat_not_coprime_helper).mk_app [ed, ex, ey, eu, ev, pu, pv, p]) end /-- Evaluates the `gcd`, `lcm`, and `coprime` functions. -/ @[norm_num] meta def eval_gcd : expr → tactic (expr × expr) | `(nat.gcd %%ex %%ey) := do c ← mk_instance_cache `(ℕ), prod.snd <$> prove_gcd_nat c ex ey | `(nat.lcm %%ex %%ey) := do c ← mk_instance_cache `(ℕ), prod.snd <$> prove_lcm_nat c ex ey | `(nat.coprime %%ex %%ey) := do c ← mk_instance_cache `(ℕ), prove_coprime_nat c ex ey >>= sum.elim true_intro false_intro ∘ prod.snd | `(int.gcd %%ex %%ey) := do zc ← mk_instance_cache `(ℤ), nc ← mk_instance_cache `(ℕ), (prod.snd ∘ prod.snd) <$> prove_gcd_int zc nc ex ey | `(int.lcm %%ex %%ey) := do zc ← mk_instance_cache `(ℤ), nc ← mk_instance_cache `(ℕ), (prod.snd ∘ prod.snd) <$> prove_lcm_int zc nc ex ey | _ := failed end norm_num end tactic
d7a923d8be70c537ab26004d69302d33bddf2901
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/sym/basic.lean
e2e73ba74c7687bc24990fcc45ba950b2c64a36a
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
13,903
lean
/- Copyright (c) 2020 Kyle Miller All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import data.multiset.basic import data.vector.basic import data.setoid.basic import tactic.apply_fun /-! # Symmetric powers This file defines symmetric powers of a type. The nth symmetric power consists of homogeneous n-tuples modulo permutations by the symmetric group. The special case of 2-tuples is called the symmetric square, which is addressed in more detail in `data.sym.sym2`. TODO: This was created as supporting material for `sym2`; it needs a fleshed-out interface. ## Tags symmetric powers -/ open function /-- The nth symmetric power is n-tuples up to permutation. We define it as a subtype of `multiset` since these are well developed in the library. We also give a definition `sym.sym'` in terms of vectors, and we show these are equivalent in `sym.sym_equiv_sym'`. -/ def sym (α : Type*) (n : ℕ) := {s : multiset α // s.card = n} instance sym.has_coe (α : Type*) (n : ℕ) : has_coe (sym α n) (multiset α) := coe_subtype /-- This is the `list.perm` setoid lifted to `vector`. See note [reducible non-instances]. -/ @[reducible] def vector.perm.is_setoid (α : Type*) (n : ℕ) : setoid (vector α n) := (list.is_setoid α).comap subtype.val local attribute [instance] vector.perm.is_setoid namespace sym variables {α β : Type*} {n : ℕ} {s : sym α n} {a b : α} lemma coe_injective : injective (coe : sym α n → multiset α) := subtype.coe_injective @[simp, norm_cast] lemma coe_inj {s₁ s₂ : sym α n} : (s₁ : multiset α) = s₂ ↔ s₁ = s₂ := coe_injective.eq_iff /-- Construct an element of the `n`th symmetric power from a multiset of cardinality `n`. -/ @[simps, pattern] abbreviation mk (m : multiset α) (h : m.card = n) : sym α n := ⟨m, h⟩ /-- The unique element in `sym α 0`. -/ @[pattern] def nil : sym α 0 := ⟨0, multiset.card_zero⟩ /-- Inserts an element into the term of `sym α n`, increasing the length by one. -/ @[pattern] def cons (a : α) (s : sym α n) : sym α n.succ := ⟨a ::ₘ s.1, by rw [multiset.card_cons, s.2]⟩ infixr ` ::ₛ `:67 := cons @[simp] lemma cons_inj_right (a : α) (s s' : sym α n) : a ::ₛ s = a ::ₛ s' ↔ s = s' := subtype.ext_iff.trans $ (multiset.cons_inj_right _).trans subtype.ext_iff.symm @[simp] lemma cons_inj_left (a a' : α) (s : sym α n) : a ::ₛ s = a' ::ₛ s ↔ a = a' := subtype.ext_iff.trans $ multiset.cons_inj_left _ lemma cons_swap (a b : α) (s : sym α n) : a ::ₛ b ::ₛ s = b ::ₛ a ::ₛ s := subtype.ext $ multiset.cons_swap a b s.1 lemma coe_cons (s : sym α n) (a : α) : (a ::ₛ s : multiset α) = a ::ₘ s := rfl /-- This is the quotient map that takes a list of n elements as an n-tuple and produces an nth symmetric power. -/ instance : has_lift (vector α n) (sym α n) := { lift := λ x, ⟨↑x.val, (multiset.coe_card _).trans x.2⟩ } @[simp] lemma of_vector_nil : ↑(vector.nil : vector α 0) = (sym.nil : sym α 0) := rfl @[simp] lemma of_vector_cons (a : α) (v : vector α n) : ↑(vector.cons a v) = a ::ₛ (↑v : sym α n) := by { cases v, refl } /-- `α ∈ s` means that `a` appears as one of the factors in `s`. -/ instance : has_mem α (sym α n) := ⟨λ a s, a ∈ s.1⟩ instance decidable_mem [decidable_eq α] (a : α) (s : sym α n) : decidable (a ∈ s) := s.1.decidable_mem _ @[simp] lemma mem_mk (a : α) (s : multiset α) (h : s.card = n) : a ∈ mk s h ↔ a ∈ s := iff.rfl @[simp] lemma mem_cons {a b : α} {s : sym α n} : a ∈ b ::ₛ s ↔ a = b ∨ a ∈ s := multiset.mem_cons lemma mem_cons_of_mem {a b : α} {s : sym α n} (h : a ∈ s) : a ∈ b ::ₛ s := multiset.mem_cons_of_mem h @[simp] lemma mem_cons_self (a : α) (s : sym α n) : a ∈ a ::ₛ s := multiset.mem_cons_self a s.1 lemma cons_of_coe_eq (a : α) (v : vector α n) : a ::ₛ (↑v : sym α n) = ↑(a ::ᵥ v) := subtype.ext $ by { cases v, refl } lemma sound {a b : vector α n} (h : a.val ~ b.val) : (↑a : sym α n) = ↑b := subtype.ext $ quotient.sound h /-- `erase s a h` is the sym that subtracts 1 from the multiplicity of `a` if a is present in the sym. -/ def erase [decidable_eq α] (s : sym α (n + 1)) (a : α) (h : a ∈ s) : sym α n := ⟨s.val.erase a, (multiset.card_erase_of_mem h).trans $ s.property.symm ▸ n.pred_succ⟩ @[simp] lemma erase_mk [decidable_eq α] (m : multiset α) (hc : m.card = n + 1) (a : α) (h : a ∈ m) : (mk m hc).erase a h = mk (m.erase a) (by { rw [multiset.card_erase_of_mem h, hc], refl }) := rfl @[simp] lemma coe_erase [decidable_eq α] {s : sym α n.succ} {a : α} (h : a ∈ s) : (s.erase a h : multiset α) = multiset.erase s a := rfl @[simp] lemma cons_erase [decidable_eq α] {s : sym α n.succ} {a : α} (h : a ∈ s) : a ::ₛ s.erase a h = s := coe_injective $ multiset.cons_erase h @[simp] lemma erase_cons_head [decidable_eq α] (s : sym α n) (a : α) (h : a ∈ a ::ₛ s := mem_cons_self a s) : (a ::ₛ s).erase a h = s := coe_injective $ multiset.erase_cons_head a s.1 /-- Another definition of the nth symmetric power, using vectors modulo permutations. (See `sym`.) -/ def sym' (α : Type*) (n : ℕ) := quotient (vector.perm.is_setoid α n) /-- This is `cons` but for the alternative `sym'` definition. -/ def cons' {α : Type*} {n : ℕ} : α → sym' α n → sym' α (nat.succ n) := λ a, quotient.map (vector.cons a) (λ ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ h, list.perm.cons _ h) notation a :: b := cons' a b /-- Multisets of cardinality n are equivalent to length-n vectors up to permutations. -/ def sym_equiv_sym' {α : Type*} {n : ℕ} : sym α n ≃ sym' α n := equiv.subtype_quotient_equiv_quotient_subtype _ _ (λ _, by refl) (λ _ _, by refl) lemma cons_equiv_eq_equiv_cons (α : Type*) (n : ℕ) (a : α) (s : sym α n) : a :: sym_equiv_sym' s = sym_equiv_sym' (a ::ₛ s) := by { rcases s with ⟨⟨l⟩, _⟩, refl, } instance : has_zero (sym α 0) := ⟨⟨0, rfl⟩⟩ instance : has_emptyc (sym α 0) := ⟨0⟩ lemma eq_nil_of_card_zero (s : sym α 0) : s = nil := subtype.ext $ multiset.card_eq_zero.1 s.2 instance unique_zero : unique (sym α 0) := ⟨⟨nil⟩, eq_nil_of_card_zero⟩ /-- `repeat a n` is the sym containing only `a` with multiplicity `n`. -/ def repeat (a : α) (n : ℕ) : sym α n := ⟨multiset.repeat a n, multiset.card_repeat _ _⟩ lemma repeat_succ {a : α} {n : ℕ} : repeat a n.succ = a ::ₛ repeat a n := rfl lemma coe_repeat : (repeat a n : multiset α) = multiset.repeat a n := rfl @[simp] lemma mem_repeat : b ∈ repeat a n ↔ n ≠ 0 ∧ b = a := multiset.mem_repeat lemma eq_repeat_iff : s = repeat a n ↔ ∀ b ∈ s, b = a := begin rw [subtype.ext_iff, coe_repeat], convert multiset.eq_repeat', exact s.2.symm, end lemma exists_mem (s : sym α n.succ) : ∃ a, a ∈ s := multiset.card_pos_iff_exists_mem.1 $ s.2.symm ▸ n.succ_pos lemma exists_eq_cons_of_succ (s : sym α n.succ) : ∃ (a : α) (s' : sym α n), s = a ::ₛ s' := begin obtain ⟨a, ha⟩ := exists_mem s, classical, exact ⟨a, s.erase a ha, (cons_erase ha).symm⟩, end lemma eq_repeat {a : α} {n : ℕ} {s : sym α n} : s = repeat a n ↔ ∀ b ∈ s, b = a := subtype.ext_iff.trans $ multiset.eq_repeat.trans $ and_iff_right s.prop lemma eq_repeat_of_subsingleton [subsingleton α] (a : α) {n : ℕ} (s : sym α n) : s = repeat a n := eq_repeat.2 $ λ b hb, subsingleton.elim _ _ instance [subsingleton α] (n : ℕ) : subsingleton (sym α n) := ⟨begin cases n, { simp, }, { intros s s', obtain ⟨b, -⟩ := exists_mem s, rw [eq_repeat_of_subsingleton b s', eq_repeat_of_subsingleton b s], }, end⟩ instance inhabited_sym [inhabited α] (n : ℕ) : inhabited (sym α n) := ⟨repeat default n⟩ instance inhabited_sym' [inhabited α] (n : ℕ) : inhabited (sym' α n) := ⟨quotient.mk' (vector.repeat default n)⟩ instance (n : ℕ) [is_empty α] : is_empty (sym α n.succ) := ⟨λ s, by { obtain ⟨a, -⟩ := exists_mem s, exact is_empty_elim a }⟩ instance (n : ℕ) [unique α] : unique (sym α n) := unique.mk' _ lemma repeat_left_inj {a b : α} {n : ℕ} (h : n ≠ 0) : repeat a n = repeat b n ↔ a = b := subtype.ext_iff.trans (multiset.repeat_left_inj h) lemma repeat_left_injective {n : ℕ} (h : n ≠ 0) : function.injective (λ x : α, repeat x n) := λ a b, (repeat_left_inj h).1 instance (n : ℕ) [nontrivial α] : nontrivial (sym α (n + 1)) := (repeat_left_injective n.succ_ne_zero).nontrivial /-- A function `α → β` induces a function `sym α n → sym β n` by applying it to every element of the underlying `n`-tuple. -/ def map {n : ℕ} (f : α → β) (x : sym α n) : sym β n := ⟨x.val.map f, by simpa [multiset.card_map] using x.property⟩ @[simp] lemma mem_map {n : ℕ} {f : α → β} {b : β} {l : sym α n} : b ∈ sym.map f l ↔ ∃ a, a ∈ l ∧ f a = b := multiset.mem_map /-- Note: `sym.map_id` is not simp-normal, as simp ends up unfolding `id` with `sym.map_congr` -/ @[simp] lemma map_id' {α : Type*} {n : ℕ} (s : sym α n) : sym.map (λ (x : α), x) s = s := by simp [sym.map] lemma map_id {α : Type*} {n : ℕ} (s : sym α n) : sym.map id s = s := by simp [sym.map] @[simp] lemma map_map {α β γ : Type*} {n : ℕ} (g : β → γ) (f : α → β) (s : sym α n) : sym.map g (sym.map f s) = sym.map (g ∘ f) s := by simp [sym.map] @[simp] lemma map_zero (f : α → β) : sym.map f (0 : sym α 0) = (0 : sym β 0) := rfl @[simp] lemma map_cons {n : ℕ} (f : α → β) (a : α) (s : sym α n) : (a ::ₛ s).map f = (f a) ::ₛ s.map f := by simp [map, cons] @[congr] lemma map_congr {f g : α → β} {s : sym α n} (h : ∀ x ∈ s, f x = g x) : map f s = map g s := subtype.ext $ multiset.map_congr rfl h @[simp] lemma map_mk {f : α → β} {m : multiset α} {hc : m.card = n} : map f (mk m hc) = mk (m.map f) (by simp [hc]) := rfl @[simp] lemma coe_map (s : sym α n) (f : α → β) : ↑(s.map f) = multiset.map f s := rfl lemma map_injective {f : α → β} (hf : injective f) (n : ℕ) : injective (map f : sym α n → sym β n) := λ s t h, coe_injective $ multiset.map_injective hf $ coe_inj.2 h /-- Mapping an equivalence `α ≃ β` using `sym.map` gives an equivalence between `sym α n` and `sym β n`. -/ @[simps] def equiv_congr (e : α ≃ β) : sym α n ≃ sym β n := { to_fun := map e, inv_fun := map e.symm, left_inv := λ x, by rw [map_map, equiv.symm_comp_self, map_id], right_inv := λ x, by rw [map_map, equiv.self_comp_symm, map_id] } /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce an element of the symmetric power on `{x // x ∈ s}`. -/ def attach (s : sym α n) : sym {x // x ∈ s} n := ⟨s.val.attach, by rw [multiset.card_attach, s.2]⟩ @[simp] lemma attach_mk {m : multiset α} {hc : m.card = n} : attach (mk m hc) = mk m.attach (multiset.card_attach.trans hc) := rfl @[simp] lemma coe_attach (s : sym α n) : (s.attach : multiset {a // a ∈ s}) = multiset.attach s := rfl lemma attach_map_coe (s : sym α n) : s.attach.map coe = s := coe_injective $ multiset.attach_map_val _ @[simp] lemma mem_attach (s : sym α n) (x : {x // x ∈ s}) : x ∈ s.attach := multiset.mem_attach _ _ @[simp] lemma attach_nil : (nil : sym α 0).attach = nil := rfl @[simp] lemma attach_cons (x : α) (s : sym α n) : (cons x s).attach = cons ⟨x, mem_cons_self _ _⟩ (s.attach.map (λ x, ⟨x, mem_cons_of_mem x.prop⟩)) := coe_injective $ multiset.attach_cons _ _ end sym section equiv /-! ### Combinatorial equivalences -/ variables {α : Type*} {n : ℕ} open sym namespace sym_option_succ_equiv /-- Function from the symmetric product over `option` splitting on whether or not it contains a `none`. -/ def encode [decidable_eq α] (s : sym (option α) n.succ) : sym (option α) n ⊕ sym α n.succ := if h : none ∈ s then sum.inl (s.erase none h) else sum.inr (s.attach.map $ λ o, option.get $ option.ne_none_iff_is_some.1 $ ne_of_mem_of_not_mem o.2 h) @[simp] lemma encode_of_none_mem [decidable_eq α] (s : sym (option α) n.succ) (h : none ∈ s) : encode s = sum.inl (s.erase none h) := dif_pos h @[simp] lemma encode_of_not_none_mem [decidable_eq α] (s : sym (option α) n.succ) (h : ¬ none ∈ s) : encode s = sum.inr (s.attach.map $ λ o, option.get $ option.ne_none_iff_is_some.1 $ ne_of_mem_of_not_mem o.2 h) := dif_neg h /-- Inverse of `sym_option_succ_equiv.decode`. -/ @[simp] def decode : sym (option α) n ⊕ sym α n.succ → sym (option α) n.succ | (sum.inl s) := none ::ₛ s | (sum.inr s) := s.map embedding.coe_option @[simp] lemma decode_encode [decidable_eq α] (s : sym (option α) n.succ) : decode (encode s) = s := begin by_cases h : none ∈ s, { simp [h] }, { simp only [h, decode, not_false_iff, subtype.val_eq_coe, encode_of_not_none_mem, embedding.coe_option_apply, map_map, comp_app, option.coe_get], convert s.attach_map_coe } end @[simp] lemma encode_decode [decidable_eq α] (s : sym (option α) n ⊕ sym α n.succ) : encode (decode s) = s := begin obtain (s | s) := s, { simp }, { unfold sym_option_succ_equiv.encode, split_ifs, { obtain ⟨a, _, ha⟩ := multiset.mem_map.mp h, exact option.some_ne_none _ ha }, { refine map_injective (option.some_injective _) _ _, convert eq.trans _ (sym_option_succ_equiv.decode (sum.inr s)).attach_map_coe, simp } } end end sym_option_succ_equiv /-- The symmetric product over `option` is a disjoint union over simpler symmetric products. -/ @[simps] def sym_option_succ_equiv [decidable_eq α] : sym (option α) n.succ ≃ sym (option α) n ⊕ sym α n.succ := { to_fun := sym_option_succ_equiv.encode, inv_fun := sym_option_succ_equiv.decode, left_inv := sym_option_succ_equiv.decode_encode, right_inv := sym_option_succ_equiv.encode_decode } end equiv
59aa5afe0de69bac8c1c76f6007f0f8d77d16ce7
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/bernoulli_polynomials.lean
d93e8eb33cafc00cf21c200dab206d48d5fb1926
[ "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
7,093
lean
/- Copyright (c) 2021 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan -/ import data.nat.choose.cast import number_theory.bernoulli /-! # Bernoulli polynomials The Bernoulli polynomials (defined here : https://en.wikipedia.org/wiki/Bernoulli_polynomials) are an important tool obtained from Bernoulli numbers. ## Mathematical overview The $n$-th Bernoulli polynomial is defined as $$ B_n(X) = ∑_{k = 0}^n {n \choose k} (-1)^k * B_k * X^{n - k} $$ where $B_k$ is the $k$-th Bernoulli number. The Bernoulli polynomials are generating functions, $$ t * e^{tX} / (e^t - 1) = ∑_{n = 0}^{\infty} B_n(X) * \frac{t^n}{n!} $$ ## Implementation detail Bernoulli polynomials are defined using `bernoulli`, the Bernoulli numbers. ## Main theorems - `sum_bernoulli_poly`: The sum of the $k^\mathrm{th}$ Bernoulli polynomial with binomial coefficients up to n is `(n + 1) * X^n`. - `exp_bernoulli_poly`: The Bernoulli polynomials act as generating functions for the exponential. ## TODO - `bernoulli_poly_eval_one_neg` : $$ B_n(1 - x) = (-1)^n*B_n(x) $$ - ``bernoulli_poly_eval_one` : Follows as a consequence of `bernoulli_poly_eval_one_neg`. -/ noncomputable theory open_locale big_operators open_locale nat open nat finset /-- The Bernoulli polynomials are defined in terms of the negative Bernoulli numbers. -/ def bernoulli_poly (n : ℕ) : polynomial ℚ := ∑ i in range (n + 1), polynomial.monomial (n - i) ((bernoulli i) * (choose n i)) lemma bernoulli_poly_def (n : ℕ) : bernoulli_poly n = ∑ i in range (n + 1), polynomial.monomial i ((bernoulli (n - i)) * (choose n i)) := begin rw [←sum_range_reflect, add_succ_sub_one, add_zero, bernoulli_poly], apply sum_congr rfl, rintros x hx, rw mem_range_succ_iff at hx, rw [choose_symm hx, tsub_tsub_cancel_of_le hx], end namespace bernoulli_poly /- ### examples -/ section examples @[simp] lemma bernoulli_poly_zero : bernoulli_poly 0 = 1 := by simp [bernoulli_poly] @[simp] lemma bernoulli_poly_eval_zero (n : ℕ) : (bernoulli_poly n).eval 0 = bernoulli n := begin rw [bernoulli_poly, polynomial.eval_finset_sum, sum_range_succ], have : ∑ (x : ℕ) in range n, bernoulli x * (n.choose x) * 0 ^ (n - x) = 0, { apply sum_eq_zero (λ x hx, _), have h : 0 < n - x := tsub_pos_of_lt (mem_range.1 hx), simp [h] }, simp [this], end @[simp] lemma bernoulli_poly_eval_one (n : ℕ) : (bernoulli_poly n).eval 1 = bernoulli' n := begin simp only [bernoulli_poly, polynomial.eval_finset_sum], simp only [←succ_eq_add_one, sum_range_succ, mul_one, cast_one, choose_self, (bernoulli _).mul_comm, sum_bernoulli, one_pow, mul_one, polynomial.eval_C, polynomial.eval_monomial], by_cases h : n = 1, { norm_num [h], }, { simp [h], exact bernoulli_eq_bernoulli'_of_ne_one h, } end end examples @[simp] theorem sum_bernoulli_poly (n : ℕ) : ∑ k in range (n + 1), ((n + 1).choose k : ℚ) • bernoulli_poly k = polynomial.monomial n (n + 1 : ℚ) := begin simp_rw [bernoulli_poly_def, finset.smul_sum, finset.range_eq_Ico, ←finset.sum_Ico_Ico_comm, finset.sum_Ico_eq_sum_range], simp only [cast_succ, add_tsub_cancel_left, tsub_zero, zero_add, linear_map.map_add], simp_rw [polynomial.smul_monomial, mul_comm (bernoulli _) _, smul_eq_mul, ←mul_assoc], conv_lhs { apply_congr, skip, conv { apply_congr, skip, rw [← nat.cast_mul, choose_mul ((le_tsub_iff_left $ mem_range_le H).1 $ mem_range_le H_1) (le.intro rfl), nat.cast_mul, add_comm x x_1, add_tsub_cancel_right, mul_assoc, mul_comm, ←smul_eq_mul, ←polynomial.smul_monomial] }, rw [←sum_smul], }, rw [sum_range_succ_comm], simp only [add_right_eq_self, cast_succ, mul_one, cast_one, cast_add, add_tsub_cancel_left, choose_succ_self_right, one_smul, bernoulli_zero, sum_singleton, zero_add, linear_map.map_add, range_one], apply sum_eq_zero (λ x hx, _), have f : ∀ x ∈ range n, ¬ n + 1 - x = 1, { rintros x H, rw [mem_range] at H, rw [eq_comm], exact ne_of_lt (nat.lt_of_lt_of_le one_lt_two (le_tsub_of_add_le_left (succ_le_succ H))) }, rw [sum_bernoulli], have g : (ite (n + 1 - x = 1) (1 : ℚ) 0) = 0, { simp only [ite_eq_right_iff, one_ne_zero], intro h₁, exact (f x hx) h₁, }, rw [g, zero_smul], end open power_series open polynomial (aeval) variables {A : Type*} [comm_ring A] [algebra ℚ A] -- TODO: define exponential generating functions, and use them here -- This name should probably be updated afterwards /-- The theorem that `∑ Bₙ(t)X^n/n!)(e^X-1)=Xe^{tX}` -/ theorem exp_bernoulli_poly' (t : A) : mk (λ n, aeval t ((1 / n! : ℚ) • bernoulli_poly n)) * (exp A - 1) = X * rescale t (exp A) := begin -- check equality of power series by checking coefficients of X^n ext n, -- n = 0 case solved by `simp` cases n, { simp }, -- n ≥ 1, the coefficients is a sum to n+2, so use `sum_range_succ` to write as -- last term plus sum to n+1 rw [coeff_succ_X_mul, coeff_rescale, coeff_exp, coeff_mul, nat.sum_antidiagonal_eq_sum_range_succ_mk, sum_range_succ], -- last term is zero so kill with `add_zero` simp only [ring_hom.map_sub, tsub_self, constant_coeff_one, constant_coeff_exp, coeff_zero_eq_constant_coeff, mul_zero, sub_self, add_zero], -- Let's multiply both sides by (n+1)! (OK because it's a unit) set u : units ℚ := ⟨(n+1)!, (n+1)!⁻¹, mul_inv_cancel (by exact_mod_cast factorial_ne_zero (n+1)), inv_mul_cancel (by exact_mod_cast factorial_ne_zero (n+1))⟩ with hu, rw ←units.mul_right_inj (units.map (algebra_map ℚ A).to_monoid_hom u), -- now tidy up unit mess and generally do trivial rearrangements -- to make RHS (n+1)*t^n rw [units.coe_map, mul_left_comm, ring_hom.to_monoid_hom_eq_coe, ring_hom.coe_monoid_hom, ←ring_hom.map_mul, hu, units.coe_mk], change _ = t^n * algebra_map ℚ A (((n+1)*n! : ℕ)*(1/n!)), rw [cast_mul, mul_assoc, mul_one_div_cancel (show (n! : ℚ) ≠ 0, from cast_ne_zero.2 (factorial_ne_zero n)), mul_one, mul_comm (t^n), ← polynomial.aeval_monomial, cast_add, cast_one], -- But this is the RHS of `sum_bernoulli_poly` rw [← sum_bernoulli_poly, finset.mul_sum, alg_hom.map_sum], -- and now we have to prove a sum is a sum, but all the terms are equal. apply finset.sum_congr rfl, -- The rest is just trivialities, hampered by the fact that we're coercing -- factorials and binomial coefficients between ℕ and ℚ and A. intros i hi, -- deal with coefficients of e^X-1 simp only [nat.cast_choose ℚ (mem_range_le hi), coeff_mk, if_neg (mem_range_sub_ne_zero hi), one_div, alg_hom.map_smul, coeff_one, units.coe_mk, coeff_exp, sub_zero, linear_map.map_sub, algebra.smul_mul_assoc, algebra.smul_def, mul_right_comm _ ((aeval t) _), ←mul_assoc, ← ring_hom.map_mul, succ_eq_add_one], -- finally cancel the Bernoulli polynomial and the algebra_map congr', apply congr_arg, rw [mul_assoc, div_eq_mul_inv, ← mul_inv₀], end end bernoulli_poly
71e01bf1e4aa001021dcfb1762a93229cdd78338
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/measure_theory/function/ae_eq_of_integral.lean
78b174d24e70dfaff3f14f6caefb2123f548ee05
[ "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
24,149
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import analysis.normed_space.dual import measure_theory.function.strongly_measurable import measure_theory.integral.set_integral /-! # From equality of integrals to equality of functions This file provides various statements of the general form "if two functions have the same integral on all sets, then they are equal almost everywhere". The different lemmas use various hypotheses on the class of functions, on the target space or on the possible finiteness of the measure. ## Main statements All results listed below apply to two functions `f,g`, together with two main hypotheses, * `f` and `g` are integrable on all measurable sets with finite measure, * for all measurable sets `s` with finite measure, `∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ`. The conclusion is then `f =ᵐ[μ] g`. The main lemmas are: * `ae_eq_of_forall_set_integral_eq_of_sigma_finite`: case of a sigma-finite measure. * `ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq`: for functions which are `ae_fin_strongly_measurable`. * `Lp.ae_eq_of_forall_set_integral_eq`: for elements of `Lp`, for `0 < p < ∞`. * `integrable.ae_eq_of_forall_set_integral_eq`: for integrable functions. For each of these results, we also provide a lemma about the equality of one function and 0. For example, `Lp.ae_eq_zero_of_forall_set_integral_eq_zero`. Generally useful lemmas which are not related to integrals: * `ae_eq_zero_of_forall_inner`: if for all constants `c`, `λ x, inner c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. * `ae_eq_zero_of_forall_dual`: if for all constants `c` in the dual space, `λ x, c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. -/ open measure_theory topological_space normed_space filter open_locale ennreal nnreal measure_theory namespace measure_theory section ae_eq_of_forall variables {α E 𝕜 : Type*} {m : measurable_space α} {μ : measure α} [is_R_or_C 𝕜] lemma ae_eq_zero_of_forall_inner [inner_product_space 𝕜 E] [second_countable_topology E] {f : α → E} (hf : ∀ c : E, (λ x, (inner c (f x) : 𝕜)) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := begin let s := dense_seq E, have hs : dense_range s := dense_range_dense_seq E, have hf' : ∀ᵐ x ∂μ, ∀ n : ℕ, inner (s n) (f x) = (0 : 𝕜), from ae_all_iff.mpr (λ n, hf (s n)), refine hf'.mono (λ x hx, _), rw [pi.zero_apply, ← inner_self_eq_zero], have h_closed : is_closed {c : E | inner c (f x) = (0 : 𝕜)}, from is_closed_eq (continuous_id.inner continuous_const) continuous_const, exact @is_closed_property ℕ E _ s (λ c, inner c (f x) = (0 : 𝕜)) hs h_closed (λ n, hx n) _, end local notation `⟪`x`, `y`⟫` := y x variables (𝕜) lemma ae_eq_zero_of_forall_dual [normed_group E] [normed_space 𝕜 E] [second_countable_topology E] {f : α → E} (hf : ∀ c : dual 𝕜 E, (λ x, ⟪f x, c⟫) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := begin let u := dense_seq E, have hu : dense_range u := dense_range_dense_seq _, have : ∀ n, ∃ g : E →L[𝕜] 𝕜, ∥g∥ ≤ 1 ∧ g (u n) = norm' 𝕜 (u n) := λ n, exists_dual_vector'' 𝕜 (u n), choose s hs using this, have A : ∀ (a : E), (∀ n, ⟪a, s n⟫ = (0 : 𝕜)) → a = 0, { assume a ha, contrapose! ha, have a_pos : 0 < ∥a∥, by simp only [ha, norm_pos_iff, ne.def, not_false_iff], have a_mem : a ∈ closure (set.range u), by simp [hu.closure_range], obtain ⟨n, hn⟩ : ∃ (n : ℕ), dist a (u n) < ∥a∥ / 2 := metric.mem_closure_range_iff.1 a_mem (∥a∥/2) (half_pos a_pos), use n, have I : ∥a∥/2 < ∥u n∥, { have : ∥a∥ ≤ ∥u n∥ + ∥a - u n∥ := norm_le_insert' _ _, have : ∥a - u n∥ < ∥a∥/2, by rwa dist_eq_norm at hn, linarith }, assume h, apply lt_irrefl (∥s n (u n)∥), calc ∥s n (u n)∥ = ∥s n (u n - a)∥ : by simp only [h, sub_zero, continuous_linear_map.map_sub] ... ≤ 1 * ∥u n - a∥ : continuous_linear_map.le_of_op_norm_le _ (hs n).1 _ ... < ∥a∥ / 2 : by { rw [one_mul], rwa dist_eq_norm' at hn } ... < ∥u n∥ : I ... = ∥s n (u n)∥ : by rw [(hs n).2, norm_norm'] }, have hfs : ∀ n : ℕ, ∀ᵐ x ∂μ, ⟪f x, s n⟫ = (0 : 𝕜), from λ n, hf (s n), have hf' : ∀ᵐ x ∂μ, ∀ n : ℕ, ⟪f x, s n⟫ = (0 : 𝕜), by rwa ae_all_iff, exact hf'.mono (λ x hx, A (f x) hx), end variables {𝕜} end ae_eq_of_forall variables {α E : Type*} {m m0 : measurable_space α} {μ : measure α} {s t : set α} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [second_countable_topology E] [complete_space E] {p : ℝ≥0∞} section ae_eq_of_forall_set_integral_eq lemma ae_const_le_iff_forall_lt_measure_zero {β} [linear_order β] [topological_space β] [order_topology β] [first_countable_topology β] (f : α → β) (c : β) : (∀ᵐ x ∂μ, c ≤ f x) ↔ ∀ b < c, μ {x | f x ≤ b} = 0 := begin rw ae_iff, push_neg, split, { assume h b hb, exact measure_mono_null (λ y hy, (lt_of_le_of_lt hy hb : _)) h }, assume hc, by_cases h : ∀ b, c ≤ b, { have : {a : α | f a < c} = ∅, { apply set.eq_empty_iff_forall_not_mem.2 (λ x hx, _), exact (lt_irrefl _ (lt_of_lt_of_le hx (h (f x)))).elim }, simp [this] }, by_cases H : ¬ (is_lub (set.Iio c) c), { have : c ∈ upper_bounds (set.Iio c) := λ y hy, le_of_lt hy, obtain ⟨b, b_up, bc⟩ : ∃ (b : β), b ∈ upper_bounds (set.Iio c) ∧ b < c, by simpa [is_lub, is_least, this, lower_bounds] using H, exact measure_mono_null (λ x hx, b_up hx) (hc b bc) }, push_neg at H h, obtain ⟨u, u_mono, u_lt, u_lim, -⟩ : ∃ (u : ℕ → β), strict_mono u ∧ (∀ (n : ℕ), u n < c) ∧ tendsto u at_top (nhds c) ∧ ∀ (n : ℕ), u n ∈ set.Iio c := H.exists_seq_strict_mono_tendsto_of_not_mem (lt_irrefl c) h, have h_Union : {x | f x < c} = ⋃ (n : ℕ), {x | f x ≤ u n}, { ext1 x, simp_rw [set.mem_Union, set.mem_set_of_eq], split; intro h, { obtain ⟨n, hn⟩ := ((tendsto_order.1 u_lim).1 _ h).exists, exact ⟨n, hn.le⟩ }, { obtain ⟨n, hn⟩ := h, exact hn.trans_lt (u_lt _), }, }, rw [h_Union, measure_Union_null_iff], assume n, exact hc _ (u_lt n), end section real section real_finite_measure variables [is_finite_measure μ] {f : α → ℝ} /-- Don't use this lemma. Use `ae_nonneg_of_forall_set_integral_nonneg_of_finite_measure`. -/ lemma ae_nonneg_of_forall_set_integral_nonneg_of_finite_measure_of_measurable (hfm : measurable f) (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin simp_rw [eventually_le, pi.zero_apply], rw ae_const_le_iff_forall_lt_measure_zero, intros b hb_neg, let s := {x | f x ≤ b}, have hs : measurable_set s, from measurable_set_le hfm measurable_const, have h_int_gt : ∫ x in s, f x ∂μ ≤ b * (μ s).to_real, { have h_const_le : ∫ x in s, f x ∂μ ≤ ∫ x in s, b ∂μ, { refine set_integral_mono_ae_restrict hf.integrable_on (integrable_on_const.mpr (or.inr (measure_lt_top μ s))) _, rw [eventually_le, ae_restrict_iff hs], exact eventually_of_forall (λ x hxs, hxs), }, rwa [set_integral_const, smul_eq_mul, mul_comm] at h_const_le, }, by_contra, refine (lt_self_iff_false (∫ x in s, f x ∂μ)).mp (h_int_gt.trans_lt _), refine (mul_neg_iff.mpr (or.inr ⟨hb_neg, _⟩)).trans_le _, swap, { simp_rw measure.restrict_restrict hs, exact hf_zero s hs, }, refine (ennreal.to_real_nonneg).lt_of_ne (λ h_eq, h _), cases (ennreal.to_real_eq_zero_iff _).mp h_eq.symm with hμs_eq_zero hμs_eq_top, { exact hμs_eq_zero, }, { exact absurd hμs_eq_top (measure_lt_top μ s).ne, }, end lemma ae_nonneg_of_forall_set_integral_nonneg_of_finite_measure (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin rcases hf.1 with ⟨f', hf'_meas, hf_ae⟩, have hf'_integrable : integrable f' μ, from integrable.congr hf hf_ae, have hf'_zero : ∀ s, measurable_set s → 0 ≤ ∫ x in s, f' x ∂μ, { intros s hs, rw set_integral_congr_ae hs (hf_ae.mono (λ x hx hxs, hx.symm)), exact hf_zero s hs, }, exact (ae_nonneg_of_forall_set_integral_nonneg_of_finite_measure_of_measurable hf'_meas hf'_integrable hf'_zero).trans hf_ae.symm.le, end end real_finite_measure lemma ae_nonneg_restrict_of_forall_set_integral_nonneg_inter {f : α → ℝ} {t : set α} (hμt : μ t ≠ ∞) (hf : integrable_on f t μ) (hf_zero : ∀ s, measurable_set s → 0 ≤ ∫ x in (s ∩ t), f x ∂μ) : 0 ≤ᵐ[μ.restrict t] f := begin haveI : fact (μ t < ∞) := ⟨lt_top_iff_ne_top.mpr hμt⟩, refine ae_nonneg_of_forall_set_integral_nonneg_of_finite_measure hf (λ s hs, _), simp_rw measure.restrict_restrict hs, exact hf_zero s hs, end lemma ae_nonneg_of_forall_set_integral_nonneg_of_sigma_finite [sigma_finite μ] {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin let S := spanning_sets μ, rw [← @measure.restrict_univ _ _ μ, ← Union_spanning_sets μ, eventually_le, ae_iff, measure.restrict_apply'], swap, { exact measurable_set.Union (measurable_spanning_sets μ), }, rw [set.inter_Union, measure_Union_null_iff], intro n, have h_meas_n : measurable_set (S n), from (measurable_spanning_sets μ n), have hμn : μ (S n) < ∞, from measure_spanning_sets_lt_top μ n, rw ← measure.restrict_apply' h_meas_n, refine ae_nonneg_restrict_of_forall_set_integral_nonneg_inter hμn.ne (hf_int_finite (S n) h_meas_n hμn) (λ s hs, _), exact hf_zero (s ∩ S n) (hs.inter h_meas_n) ((measure_mono (set.inter_subset_right _ _)).trans_lt hμn), end lemma ae_fin_strongly_measurable.ae_nonneg_of_forall_set_integral_nonneg {f : α → ℝ} (hf : ae_fin_strongly_measurable f μ) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin let t := hf.sigma_finite_set, suffices : 0 ≤ᵐ[μ.restrict t] f, from ae_of_ae_restrict_of_ae_restrict_compl hf.measurable_set this hf.ae_eq_zero_compl.symm.le, haveI : sigma_finite (μ.restrict t) := hf.sigma_finite_restrict, refine ae_nonneg_of_forall_set_integral_nonneg_of_sigma_finite (λ s hs hμts, _) (λ s hs hμts, _), { rw [integrable_on, measure.restrict_restrict hs], rw measure.restrict_apply hs at hμts, exact hf_int_finite (s ∩ t) (hs.inter hf.measurable_set) hμts, }, { rw measure.restrict_restrict hs, rw measure.restrict_apply hs at hμts, exact hf_zero (s ∩ t) (hs.inter hf.measurable_set) hμts, }, end lemma integrable.ae_nonneg_of_forall_set_integral_nonneg {f : α → ℝ} (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := ae_fin_strongly_measurable.ae_nonneg_of_forall_set_integral_nonneg hf.ae_fin_strongly_measurable (λ s hs hμs, hf.integrable_on) hf_zero lemma ae_nonneg_restrict_of_forall_set_integral_nonneg {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : 0 ≤ᵐ[μ.restrict t] f := begin refine ae_nonneg_restrict_of_forall_set_integral_nonneg_inter hμt (hf_int_finite t ht (lt_top_iff_ne_top.mpr hμt)) (λ s hs, _), refine (hf_zero (s ∩ t) (hs.inter ht) _), exact (measure_mono (set.inter_subset_right s t)).trans_lt (lt_top_iff_ne_top.mpr hμt), end lemma ae_eq_zero_restrict_of_forall_set_integral_eq_zero_real {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := begin suffices h_and : f ≤ᵐ[μ.restrict t] 0 ∧ 0 ≤ᵐ[μ.restrict t] f, from h_and.1.mp (h_and.2.mono (λ x hx1 hx2, le_antisymm hx2 hx1)), refine ⟨_, ae_nonneg_restrict_of_forall_set_integral_nonneg hf_int_finite (λ s hs hμs, (hf_zero s hs hμs).symm.le) ht hμt⟩, suffices h_neg : 0 ≤ᵐ[μ.restrict t] -f, { refine h_neg.mono (λ x hx, _), rw pi.neg_apply at hx, simpa using hx, }, refine ae_nonneg_restrict_of_forall_set_integral_nonneg (λ s hs hμs, (hf_int_finite s hs hμs).neg) (λ s hs hμs, _) ht hμt, simp_rw pi.neg_apply, rw [integral_neg, neg_nonneg], exact (hf_zero s hs hμs).le, end end real lemma ae_eq_zero_restrict_of_forall_set_integral_eq_zero {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := begin refine ae_eq_zero_of_forall_dual ℝ (λ c, _), refine ae_eq_zero_restrict_of_forall_set_integral_eq_zero_real _ _ ht hμt, { assume s hs hμs, exact continuous_linear_map.integrable_comp c (hf_int_finite s hs hμs) }, { assume s hs hμs, rw [continuous_linear_map.integral_comp_comm c (hf_int_finite s hs hμs), hf_zero s hs hμs], exact continuous_linear_map.map_zero _ } end lemma ae_eq_restrict_of_forall_set_integral_eq {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] g := begin rw ← sub_ae_eq_zero, have hfg' : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), exact sub_eq_zero.mpr (hfg_zero s hs hμs), }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact ae_eq_zero_restrict_of_forall_set_integral_eq_zero hfg_int hfg' ht hμt, end lemma ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite [sigma_finite μ] {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := begin let S := spanning_sets μ, rw [← @measure.restrict_univ _ _ μ, ← Union_spanning_sets μ, eventually_eq, ae_iff, measure.restrict_apply' (measurable_set.Union (measurable_spanning_sets μ))], rw [set.inter_Union, measure_Union_null_iff], intro n, have h_meas_n : measurable_set (S n), from (measurable_spanning_sets μ n), have hμn : μ (S n) < ∞, from measure_spanning_sets_lt_top μ n, rw ← measure.restrict_apply' h_meas_n, exact ae_eq_zero_restrict_of_forall_set_integral_eq_zero hf_int_finite hf_zero h_meas_n hμn.ne, end lemma ae_eq_of_forall_set_integral_eq_of_sigma_finite [sigma_finite μ] {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_eq : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)], }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite hfg_int hfg, end lemma ae_fin_strongly_measurable.ae_eq_zero_of_forall_set_integral_eq_zero {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ] 0 := begin let t := hf.sigma_finite_set, suffices : f =ᵐ[μ.restrict t] 0, from ae_of_ae_restrict_of_ae_restrict_compl hf.measurable_set this hf.ae_eq_zero_compl, haveI : sigma_finite (μ.restrict t) := hf.sigma_finite_restrict, refine ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite _ _, { intros s hs hμs, rw [integrable_on, measure.restrict_restrict hs], rw [measure.restrict_apply hs] at hμs, exact hf_int_finite _ (hs.inter hf.measurable_set) hμs, }, { intros s hs hμs, rw [measure.restrict_restrict hs], rw [measure.restrict_apply hs] at hμs, exact hf_zero _ (hs.inter hf.measurable_set) hμs, }, end lemma ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_eq : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)], }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact (hf.sub hg).ae_eq_zero_of_forall_set_integral_eq_zero hfg_int hfg, end lemma Lp.ae_eq_zero_of_forall_set_integral_eq_zero (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := ae_fin_strongly_measurable.ae_eq_zero_of_forall_set_integral_eq_zero hf_int_finite hf_zero (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable lemma Lp.ae_eq_of_forall_set_integral_eq (f g : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq hf_int_finite hg_int_finite hfg (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable lemma ae_eq_zero_of_forall_set_integral_eq_of_fin_strongly_measurable_trim (hm : m ≤ m0) {f : α → E} (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : fin_strongly_measurable f (μ.trim hm)) : f =ᵐ[μ] 0 := begin obtain ⟨t, ht_meas, htf_zero, htμ⟩ := hf.exists_set_sigma_finite, haveI : sigma_finite ((μ.restrict t).trim hm) := by rwa restrict_trim hm μ ht_meas at htμ, have htf_zero : f =ᵐ[μ.restrict tᶜ] 0, { rw [eventually_eq, ae_restrict_iff' (measurable_set.compl (hm _ ht_meas))], exact eventually_of_forall htf_zero, }, have hf_meas_m : @measurable _ _ m _ f, from hf.measurable, suffices : f =ᵐ[μ.restrict t] 0, from ae_of_ae_restrict_of_ae_restrict_compl (hm t ht_meas) this htf_zero, refine measure_eq_zero_of_trim_eq_zero hm _, refine ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite _ _, { intros s hs hμs, rw [integrable_on, restrict_trim hm (μ.restrict t) hs, measure.restrict_restrict (hm s hs)], rw [← restrict_trim hm μ ht_meas, measure.restrict_apply hs, trim_measurable_set_eq hm (@measurable_set.inter _ m _ _ hs ht_meas)] at hμs, refine integrable.trim hm _ hf_meas_m, exact hf_int_finite _ (@measurable_set.inter _ m _ _ hs ht_meas) hμs, }, { intros s hs hμs, rw [restrict_trim hm (μ.restrict t) hs, measure.restrict_restrict (hm s hs)], rw [← restrict_trim hm μ ht_meas, measure.restrict_apply hs, trim_measurable_set_eq hm (@measurable_set.inter _ m _ _ hs ht_meas)] at hμs, rw ← integral_trim hm hf_meas_m, exact hf_zero _ (@measurable_set.inter _ m _ _ hs ht_meas) hμs, }, end lemma integrable.ae_eq_zero_of_forall_set_integral_eq_zero {f : α → E} (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := begin have hf_Lp : mem_ℒp f 1 μ, from mem_ℒp_one_iff_integrable.mpr hf, let f_Lp := hf_Lp.to_Lp f, have hf_f_Lp : f =ᵐ[μ] f_Lp, from (mem_ℒp.coe_fn_to_Lp hf_Lp).symm, refine hf_f_Lp.trans _, refine Lp.ae_eq_zero_of_forall_set_integral_eq_zero f_Lp one_ne_zero ennreal.coe_ne_top _ _, { exact λ s hs hμs, integrable.integrable_on (L1.integrable_coe_fn _), }, { intros s hs hμs, rw integral_congr_ae (ae_restrict_of_ae hf_f_Lp.symm), exact hf_zero s hs hμs, }, end lemma integrable.ae_eq_of_forall_set_integral_eq (f g : α → E) (hf : integrable f μ) (hg : integrable g μ) (hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg' : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw integral_sub' hf.integrable_on hg.integrable_on, exact sub_eq_zero.mpr (hfg s hs hμs), }, exact integrable.ae_eq_zero_of_forall_set_integral_eq_zero (hf.sub hg) hfg', end end ae_eq_of_forall_set_integral_eq section lintegral lemma ae_measurable.ae_eq_of_forall_set_lintegral_eq {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (hgi : ∫⁻ x, g x ∂μ ≠ ∞) (hfg : ∀ ⦃s⦄, measurable_set s → μ s < ∞ → ∫⁻ x in s, f x ∂μ = ∫⁻ x in s, g x ∂μ) : f =ᵐ[μ] g := begin refine ennreal.eventually_eq_of_to_real_eventually_eq (ae_lt_top' hf hfi).ne_of_lt (ae_lt_top' hg hgi).ne_of_lt (integrable.ae_eq_of_forall_set_integral_eq _ _ (integrable_to_real_of_lintegral_ne_top hf hfi) (integrable_to_real_of_lintegral_ne_top hg hgi) (λ s hs hs', _)), rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { congr' 1, rw [lintegral_congr_ae (of_real_to_real_ae_eq _), lintegral_congr_ae (of_real_to_real_ae_eq _)], { exact hfg hs hs' }, { refine (ae_lt_top' hg.restrict (ne_of_lt (lt_of_le_of_lt _ hgi.lt_top))), exact @set_lintegral_univ α _ μ g ▸ lintegral_mono_set (set.subset_univ _) }, { refine (ae_lt_top' hf.restrict (ne_of_lt (lt_of_le_of_lt _ hfi.lt_top))), exact @set_lintegral_univ α _ μ f ▸ lintegral_mono_set (set.subset_univ _) } }, -- putting the proofs where they are used is extremely slow exacts [ae_of_all _ (λ x, ennreal.to_real_nonneg), hg.ennreal_to_real.restrict, ae_of_all _ (λ x, ennreal.to_real_nonneg), hf.ennreal_to_real.restrict] end end lintegral end measure_theory
c6ca91c3c2ba28d54014b19041cdc18430a09111
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Init/Control/Option.lean
0f0111011e59eca0d2aaefd308ffedecf9432713
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,853
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Control.Basic import Init.Control.Except universes u v instance {α} : ToBool (Option α) := ⟨Option.toBool⟩ def OptionT (m : Type u → Type v) (α : Type u) : Type v := m (Option α) @[inline] def OptionT.run {m : Type u → Type v} {α : Type u} (x : OptionT m α) : m (Option α) := x namespace OptionT variables {m : Type u → Type v} [Monad m] {α β : Type u} @[inline] protected def bind (x : OptionT m α) (f : α → OptionT m β) : OptionT m β := id (α := m (Option β)) do match (← x) with | some a => f a | none => pure none @[inline] protected def pure (a : α) : OptionT m α := id (α := m (Option α)) do pure (some a) instance : Monad (OptionT m) := { pure := OptionT.pure, bind := OptionT.bind } @[inline] protected def orElse (x : OptionT m α) (y : OptionT m α) : OptionT m α := id (α := m (Option α)) do match (← x) with | some a => pure (some a) | _ => y @[inline] protected def fail : OptionT m α := id (α := m (Option α)) do pure none instance : Alternative (OptionT m) := { failure := OptionT.fail, orElse := OptionT.orElse } @[inline] protected def lift (x : m α) : OptionT m α := id (α := m (Option α)) do return some (← x) instance : MonadLift m (OptionT m) := ⟨OptionT.lift⟩ instance : MonadFunctor m (OptionT m) := ⟨fun f x => f x⟩ @[inline] protected def tryCatch (x : OptionT m α) (handle : Unit → OptionT m α) : OptionT m α := id (α := m (Option α)) do let some a ← x | handle () pure a instance : MonadExceptOf Unit (OptionT m) := { throw := fun _ => OptionT.fail, tryCatch := OptionT.tryCatch } end OptionT
a47893dbe9e2e46b23ad15ff4ce946ecc08acb7a
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/measure_theory/measure/outer_measure.lean
cc6f7a90e88b881ed63b7f99f7b1d8659c0a5345
[ "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
61,611
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.pi_system import data.matrix.notation import topology.algebra.infinite_sum /-! # Outer Measures An outer measure is a function `μ : set α → ℝ≥0∞`, 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 α → ℝ≥0∞` 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.bounded_by` is the greatest outer measure that is at most the given function. If you know that the given functions sends `∅` to `0`, then `outer_measure.of_function` is a special case. * `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 nnreal topological_space ennreal namespace measure_theory /-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/ structure outer_measure (α : Type*) := (measure_of : set α → ℝ≥0∞) (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₂ /-- If `s : ι → set α` is a sequence of sets, `S = ⋃ n, s n`, and `m (S \ s n)` tends to zero along some nontrivial filter (usually `at_top` on `α = ℕ`), then `m S = ⨆ n, m (s n)`. -/ lemma Union_of_tendsto_zero {ι} (m : outer_measure α) {s : ι → set α} (l : filter ι) [ne_bot l] (h0 : tendsto (λ k, m ((⋃ n, s n) \ s k)) l (𝓝 0)) : m (⋃ n, s n) = ⨆ n, m (s n) := begin set S := ⋃ n, s n, set M := ⨆ n, m (s n), have hsS : ∀ {k}, s k ⊆ S, from λ k, subset_Union _ _, refine le_antisymm _ (supr_le $ λ n, m.mono hsS), have A : ∀ k, m S ≤ M + m (S \ s k), from λ k, calc m S = m (s k ∪ S \ s k) : by rw [union_diff_self, union_eq_self_of_subset_left hsS] ... ≤ m (s k) + m (S \ s k) : m.union _ _ ... ≤ M + m (S \ s k) : add_le_add_right (le_supr _ k) _, have B : tendsto (λ k, M + m (S \ s k)) l (𝓝 (M + 0)), from tendsto_const_nhds.add h0, rw add_zero at B, exact ge_of_tendsto' B A end /-- If `s : ℕ → set α` is a monotone sequence of sets such that `∑' k, m (s (k + 1) \ s k) ≠ ∞`, then `m (⋃ n, s n) = ⨆ n, m (s n)`. -/ lemma Union_nat_of_monotone_of_tsum_ne_top (m : outer_measure α) {s : ℕ → set α} (h_mono : ∀ n, s n ⊆ s (n + 1)) (h0 : ∑' k, m (s (k + 1) \ s k) ≠ ∞) : m (⋃ n, s n) = ⨆ n, m (s n) := begin refine m.Union_of_tendsto_zero at_top _, refine tendsto_nhds_bot_mono' (ennreal.tendsto_sum_nat_add _ h0) (λ n, _), refine (m.mono _).trans (m.Union _), /- Current goal: `(⋃ k, s k) \ s n ⊆ ⋃ k, s (k + n + 1) \ s (k + n)` -/ have h' : monotone s := @monotone_nat_of_le_succ (set α) _ _ h_mono, simp only [diff_subset_iff, Union_subset_iff], intros i x hx, rcases nat.find_x ⟨i, hx⟩ with ⟨j, hj, hlt⟩, clear hx i, cases le_or_lt j n with hjn hnj, { exact or.inl (h' hjn hj) }, have : j - (n + 1) + n + 1 = j, by rw [add_assoc, nat.sub_add_cancel hnj], refine or.inr (mem_Union.2 ⟨j - (n + 1), _, hlt _ _⟩), { rwa this }, { rw [← nat.succ_le_iff, nat.succ_eq_add_one, this] } end 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 : injective (λ (μ : outer_measure α) (s : set α), μ s) := λ μ₁ μ₂ h, by { cases μ₁, cases μ₂, congr, exact h } @[ext] lemma ext {μ₁ μ₂ : outer_measure α} (h : ∀ s, μ₁ s = μ₂ s) : μ₁ = μ₂ := coe_fn_injective $ funext h /-- A version of `measure_theory.outer_measure.ext` that assumes `μ₁ s = μ₂ s` on all *nonempty* sets `s`, and gets `μ₁ ∅ = μ₂ ∅` from `measure_theory.outer_measure.empty'`. -/ lemma ext_nonempty {μ₁ μ₂ : outer_measure α} (h : ∀ s : set α, s.nonempty → μ₁ s = μ₂ s) : μ₁ = μ₂ := ext $ λ s, s.eq_empty_or_nonempty.elim (λ he, by rw [he, empty', empty']) (h s) 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 α → ℝ≥0∞, from coe_fn) coe_fn_injective rfl (λ _ _, rfl) } instance : has_scalar ℝ≥0∞ (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 : ℝ≥0∞) (m : outer_measure α) : ⇑(c • m) = c • m := rfl lemma smul_apply (c : ℝ≥0∞) (m : outer_measure α) (s : set α) : (c • m) s = c * m s := rfl instance : module ℝ≥0∞ (outer_measure α) := { smul := (•), .. injective.module ℝ≥0∞ ⟨show outer_measure α → set α → ℝ≥0∞, 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 := nonpos_iff_eq_zero.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 theorem smul_supr {ι} (f : ι → outer_measure α) (c : ℝ≥0∞) : c • (⨆ i, f i) = ⨆ i, c • f i := ext $ λ s, by simp only [smul_apply, supr_apply, ennreal.mul_supr] end supremum @[mono] lemma mono'' {m₁ m₂ : outer_measure α} {s₁ s₂ : set α} (hm : m₁ ≤ m₂) (hs : s₁ ⊆ s₂) : m₁ s₁ ≤ m₂ s₂ := (hm s₁).trans (m₂.mono hs) /-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/ def map {β} (f : α → β) : outer_measure α →ₗ[ℝ≥0∞] 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 @[mono] theorem map_mono {β} (f : α → β) : monotone (map f) := λ m m' h s, h _ @[simp] theorem map_sup {β} (f : α → β) (m m' : outer_measure α) : map f (m ⊔ m') = map f m ⊔ map f m' := ext $ λ s, by simp only [map_apply, sup_apply] @[simp] theorem map_supr {β ι} (f : α → β) (m : ι → outer_measure α) : map f (⨆ i, m i) = ⨆ i, map f (m i) := ext $ λ s, by simp only [map_apply, supr_apply] 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, indicator s (λ _, 1) a, empty := by simp, mono := λ s t h, indicator_le_indicator_of_subset h (λ _, zero_le _) a, Union_nat := λ s, if hs : a ∈ ⋃ n, s n then let ⟨i, hi⟩ := mem_Union.1 hs in calc indicator (⋃ n, s n) (λ _, (1 : ℝ≥0∞)) a = 1 : indicator_of_mem hs _ ... = indicator (s i) (λ _, 1) a : (indicator_of_mem hi _).symm ... ≤ ∑' n, indicator (s n) (λ _, 1) a : ennreal.le_tsum _ else by simp only [indicator_of_not_mem hs, zero_le]} @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = indicator s (λ _, 1) a := 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 : ℝ≥0∞) (b : α) (s : set α) : (a • dirac b) s = indicator s (λ _, a) b := by simp only [smul_apply, dirac_apply, ← indicator_mul_right _ (λ _, a), mul_one] /-- Pullback of an `outer_measure`: `comap f μ s = μ (f '' s)`. -/ def comap {β} (f : α → β) : outer_measure β →ₗ[ℝ≥0∞] 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 @[mono] lemma comap_mono {β} (f : α → β) : monotone (comap f) := λ m m' h s, h _ @[simp] theorem comap_supr {β ι} (f : α → β) (m : ι → outer_measure β) : comap f (⨆ i, m i) = ⨆ i, comap f (m i) := ext $ λ s, by simp only [comap_apply, supr_apply] /-- Restrict an `outer_measure` to a set. -/ def restrict (s : set α) : outer_measure α →ₗ[ℝ≥0∞] 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] @[mono] lemma restrict_mono {s t : set α} (h : s ⊆ t) {m m' : outer_measure α} (hm : m ≤ m') : restrict s m ≤ restrict t m' := λ u, by { simp only [restrict_apply], exact (hm _).trans (m'.mono $ inter_subset_inter_right _ h) } @[simp] lemma restrict_univ (m : outer_measure α) : restrict univ m = m := ext $ λ s, by simp @[simp] lemma restrict_empty (m : outer_measure α) : restrict ∅ m = 0 := ext $ λ s, by simp @[simp] lemma restrict_supr {ι} (s : set α) (m : ι → outer_measure α) : restrict s (⨆ i, m i) = ⨆ i, restrict s (m i) := by simp [restrict] lemma map_comap {β} (f : α → β) (m : outer_measure β) : map f (comap f m) = restrict (range f) m := ext $ λ s, congr_arg m $ by simp only [image_preimage_eq_inter_range, subtype.range_coe] lemma map_comap_le {β} (f : α → β) (m : outer_measure β) : map f (comap f m) ≤ m := λ s, m.mono $ image_preimage_subset _ _ lemma restrict_le_self (m : outer_measure α) (s : set α) : restrict s m ≤ m := map_comap_le _ _ @[simp] lemma map_le_restrict_range {β} {ma : outer_measure α} {mb : outer_measure β} {f : α → β} : map f ma ≤ restrict (range f) mb ↔ map f ma ≤ mb := ⟨λ h, h.trans (restrict_le_self _ _), λ h s, by simpa using h (s ∩ range f)⟩ lemma map_comap_of_surjective {β} {f : α → β} (hf : surjective f) (m : outer_measure β) : map f (comap f m) = m := ext $ λ s, by rw [map_apply, comap_apply, hf.image_preimage] lemma le_comap_map {β} (f : α → β) (m : outer_measure α) : m ≤ comap f (map f m) := λ s, m.mono $ subset_preimage_image _ _ lemma comap_map {β} {f : α → β} (hf : injective f) (m : outer_measure α) : comap f (map f m) = m := ext $ λ s, by rw [comap_apply, map_apply, hf.preimage_image] @[simp] 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 (∞ • dirac a) trivial) theorem top_apply' (s : set α) : (⊤ : outer_measure α) s = ⨅ (h : s = ∅), 0 := s.eq_empty_or_nonempty.elim (λ h, by simp [h]) (λ h, by simp [h, h.ne_empty]) @[simp] theorem comap_top (f : α → β) : comap f ⊤ = ⊤ := ext_nonempty $ λ s hs, by rw [comap_apply, top_apply hs, top_apply (hs.image _)] theorem map_top (f : α → β) : map f ⊤ = restrict (range f) ⊤ := ext $ λ s, by rw [map_apply, restrict_apply, ← image_preimage_eq_inter_range, top_apply', top_apply', set.image_eq_empty] theorem map_top_of_surjective (f : α → β) (hf : surjective f) : map f ⊤ = ⊤ := by rw [map_top, hf.range_eq, restrict_univ] end basic section of_function set_option eqn_compiler.zeta true variables {α : Type*} (m : set α → ℝ≥0∞) (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_pos_le_add $ 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 (ne_top_of_le_ne_top hb.ne $ ennreal.le_tsum _) (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 } lemma of_function_apply (s : set α) : outer_measure.of_function m m_empty s = (⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, m (t n)) := rfl 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.cases_on i s (λ _, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ tsum_eq_single 0 $ by rintro (_|i); simp [f, m_empty] 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 _⟩ lemma is_greatest_of_function : is_greatest {μ : outer_measure α | ∀ s, μ s ≤ m s} (outer_measure.of_function m m_empty) := ⟨λ s, of_function_le _, λ μ, le_of_function.2⟩ lemma of_function_eq_Sup : outer_measure.of_function m m_empty = Sup {μ | ∀ s, μ s ≤ m s} := (@is_greatest_of_function α m m_empty).is_lub.Sup_eq.symm /-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.of_function m m_empty`. E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma of_function_union_of_top_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) : outer_measure.of_function m m_empty (s ∪ t) = outer_measure.of_function m m_empty s + outer_measure.of_function m m_empty t := begin refine le_antisymm (outer_measure.union _ _ _) (le_infi $ λ f, le_infi $ λ hf, _), set μ := outer_measure.of_function m m_empty, rcases em (∃ i, (s ∩ f i).nonempty ∧ (t ∩ f i).nonempty) with ⟨i, hs, ht⟩|he, { calc μ s + μ t ≤ ∞ : le_top ... = m (f i) : (h (f i) hs ht).symm ... ≤ ∑' i, m (f i) : ennreal.le_tsum i }, set I := λ s, {i : ℕ | (s ∩ f i).nonempty}, have hd : disjoint (I s) (I t), from λ i hi, he ⟨i, hi⟩, have hI : ∀ u ⊆ s ∪ t, μ u ≤ ∑' i : I u, μ (f i), from λ u hu, calc μ u ≤ μ (⋃ i : I u, f i) : μ.mono (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hf (hu hx)) in mem_Union.2 ⟨⟨i, ⟨x, hx, hi⟩⟩, hi⟩) ... ≤ ∑' i : I u, μ (f i) : μ.Union _, calc μ s + μ t ≤ (∑' i : I s, μ (f i)) + (∑' i : I t, μ (f i)) : add_le_add (hI _ $ subset_union_left _ _) (hI _ $ subset_union_right _ _) ... = ∑' i : I s ∪ I t, μ (f i) : (@tsum_union_disjoint _ _ _ _ _ (λ i, μ (f i)) _ _ _ hd ennreal.summable ennreal.summable).symm ... ≤ ∑' i, μ (f i) : tsum_le_tsum_of_inj coe subtype.coe_injective (λ _ _, zero_le _) (λ _, le_rfl) ennreal.summable ennreal.summable ... ≤ ∑' i, m (f i) : ennreal.tsum_le_tsum (λ i, of_function_le _) end lemma comap_of_function {β} (f : β → α) (h : monotone m ∨ surjective f) : comap f (outer_measure.of_function m m_empty) = outer_measure.of_function (λ s, m (f '' s)) (by rwa set.image_empty) := begin refine le_antisymm (le_of_function.2 $ λ s, _) (λ s, _), { rw comap_apply, apply of_function_le }, { rw [comap_apply, of_function_apply, of_function_apply], refine infi_le_infi2 (λ t, ⟨λ k, f ⁻¹' (t k), _⟩), refine infi_le_infi2 (λ ht, _), rw [set.image_subset_iff, preimage_Union] at ht, refine ⟨ht, ennreal.tsum_le_tsum $ λ n, _⟩, cases h, exacts [h (image_preimage_subset _ _), (congr_arg m (h.image_preimage (t n))).le] } end lemma map_of_function_le {β} (f : α → β) : map f (outer_measure.of_function m m_empty) ≤ outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty := le_of_function.2 $ λ s, by { rw map_apply, apply of_function_le } lemma map_of_function {β} {f : α → β} (hf : injective f) : map f (outer_measure.of_function m m_empty) = outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty := begin refine (map_of_function_le _).antisymm (λ s, _), simp only [of_function_apply, map_apply, le_infi_iff], intros t ht, refine infi_le_of_le (λ n, (range f)ᶜ ∪ f '' (t n)) (infi_le_of_le _ _), { rw [← union_Union, ← inter_subset, ← image_preimage_eq_inter_range, ← image_Union], exact image_subset _ ht }, { refine ennreal.tsum_le_tsum (λ n, le_of_eq _), simp [hf.preimage_image] } end lemma restrict_of_function (s : set α) (hm : monotone m) : restrict s (outer_measure.of_function m m_empty) = outer_measure.of_function (λ t, m (t ∩ s)) (by rwa set.empty_inter) := by simp only [restrict, linear_map.comp_apply, comap_of_function _ (or.inl hm), map_of_function subtype.coe_injective, subtype.image_preimage_coe] lemma smul_of_function {c : ℝ≥0∞} (hc : c ≠ ∞) : c • outer_measure.of_function m m_empty = outer_measure.of_function (c • m) (by simp [m_empty]) := begin ext1 s, haveI : nonempty {t : ℕ → set α // s ⊆ ⋃ i, t i} := ⟨⟨λ _, s, subset_Union (λ _, s) 0⟩⟩, simp only [smul_apply, of_function_apply, ennreal.tsum_mul_left, pi.smul_apply, smul_eq_mul, infi_subtype', ennreal.infi_mul_left (λ h, (hc h).elim)], end end of_function section bounded_by variables {α : Type*} (m : set α → ℝ≥0∞) /-- Given any function `m` assigning measures to sets, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. This is the same as `outer_measure.of_function`, except that it doesn't require `m ∅ = 0`. -/ def bounded_by : outer_measure α := outer_measure.of_function (λ s, ⨆ (h : s.nonempty), m s) (by simp [empty_not_nonempty]) variables {m} theorem bounded_by_le (s : set α) : bounded_by m s ≤ m s := (of_function_le _).trans supr_const_le theorem bounded_by_eq_of_function (m_empty : m ∅ = 0) (s : set α) : bounded_by m s = outer_measure.of_function m m_empty s := begin have : (λ s : set α, ⨆ (h : s.nonempty), m s) = m, { ext1 t, cases t.eq_empty_or_nonempty with h h; simp [h, empty_not_nonempty, m_empty] }, simp [bounded_by, this] end theorem bounded_by_apply (s : set α) : bounded_by m s = ⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, ⨆ (h : (t n).nonempty), m (t n) := by simp [bounded_by, of_function_apply] theorem bounded_by_eq (s : set α) (m_empty : m ∅ = 0) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ ∑'i, m (s i)) : bounded_by m s = m s := by rw [bounded_by_eq_of_function m_empty, of_function_eq s m_mono m_subadd] theorem le_bounded_by {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ s, μ s ≤ m s := begin rw [bounded_by, le_of_function, forall_congr], intro s, cases s.eq_empty_or_nonempty with h h; simp [h, empty_not_nonempty] end theorem le_bounded_by' {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ s : set α, s.nonempty → μ s ≤ m s := by { rw [le_bounded_by, forall_congr], intro s, cases s.eq_empty_or_nonempty with h h; simp [h] } lemma smul_bounded_by {c : ℝ≥0∞} (hc : c ≠ ∞) : c • bounded_by m = bounded_by (c • m) := begin simp only [bounded_by, smul_of_function hc], congr' 1 with s : 1, rcases s.eq_empty_or_nonempty with rfl|hs; simp * end lemma comap_bounded_by {β} (f : β → α) (h : monotone (λ s : {s : set α // s.nonempty}, m s) ∨ surjective f) : comap f (bounded_by m) = bounded_by (λ s, m (f '' s)) := begin refine (comap_of_function _ _).trans _, { refine h.imp (λ H s t hst, supr_le $ λ hs, _) id, have ht : t.nonempty := hs.mono hst, exact (@H ⟨s, hs⟩ ⟨t, ht⟩ hst).trans (le_supr (λ h : t.nonempty, m t) ht) }, { dunfold bounded_by, congr' with s : 1, rw nonempty_image_iff } end /-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.bounded_by m`. E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma bounded_by_union_of_top_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) : bounded_by m (s ∪ t) = bounded_by m s + bounded_by m t := of_function_union_of_top_of_nonempty_inter $ λ u hs ht, top_unique $ (h u hs ht).ge.trans $ le_supr (λ h, m u) (hs.mono $ inter_subset_right s u) end bounded_by 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 bUnion_lt_succ; exact is_caratheodory_union m (is_caratheodory_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) (h n (le_refl (n + 1))) 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 rw [bUnion_lt_succ, finset.sum_range_succ, set.union_comm, is_caratheodory_sum, m.measure_inter_union _ (h n), add_comm], intro a, simpa using λ (h₁ : a ∈ s n) i (hi : i < n) 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.measurable_set' s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_iff_le {s : set α} : caratheodory.measurable_set' 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.measurable_set' (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 α → ℝ≥0∞} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.measurable_set' 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 lemma bounded_by_caratheodory {m : set α → ℝ≥0∞} {s : set α} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (bounded_by m).caratheodory.measurable_set' s := begin apply of_function_caratheodory, intro t, cases t.eq_empty_or_nonempty with h h, { simp [h, empty_not_nonempty] }, { convert le_trans _ (hs t), { simp [h] }, exact add_le_add supr_const_le supr_const_le } 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.measurable_set_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ℝ≥0∞) (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 ht : a ∈ t, swap, by simp [ht], by_cases hs : a ∈ s; simp* 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 α) : ℝ≥0∞ := ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s lemma Inf_gen_def (m : set (outer_measure α)) (t : set α) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := rfl lemma Inf_eq_bounded_by_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.bounded_by (Inf_gen m) := begin refine le_antisymm _ _, { refine (le_bounded_by.2 $ λ s, _), refine le_binfi _, intros μ hμ, refine (show Inf m ≤ μ, from Inf_le hμ) s }, { refine le_Inf _, intros μ hμ t, refine le_trans (bounded_by_le t) (binfi_le μ hμ) } end lemma supr_Inf_gen_nonempty {m : set (outer_measure α)} (h : m.nonempty) (t : set α) : (⨆ (h : t.nonempty), Inf_gen m t) = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin rcases t.eq_empty_or_nonempty with rfl|ht, { rcases h with ⟨μ, hμ⟩, rw [eq_false_intro empty_not_nonempty, supr_false, eq_comm], simp_rw [empty'], apply bot_unique, refine infi_le_of_le μ (infi_le _ hμ) }, { simp [ht, Inf_gen_def] } end /-- The value of the Infimum of a nonempty set of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma Inf_apply {m : set (outer_measure α)} {s : set α} (h : m.nonempty) : Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) := by simp_rw [Inf_eq_bounded_by_Inf_gen, bounded_by_apply, supr_Inf_gen_nonempty h] /-- The value of the Infimum of a set of outer measures on a nonempty set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma Inf_apply' {m : set (outer_measure α)} {s : set α} (h : s.nonempty) : Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) := m.eq_empty_or_nonempty.elim (λ hm, by simp [hm, h]) Inf_apply /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma infi_apply {ι} [nonempty ι] (m : ι → outer_measure α) (s : set α) : (⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) := by { rw [infi, Inf_apply (range_nonempty m)], simp only [infi_range] } /-- The value of the Infimum of a family of outer measures on a nonempty set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma infi_apply' {ι} (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) : (⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) := by { rw [infi, Inf_apply' hs], simp only [infi_range] } /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma binfi_apply {ι} {I : set ι} (hI : I.nonempty) (m : ι → outer_measure α) (s : set α) : (⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) := by { haveI := hI.to_subtype, simp only [← infi_subtype'', infi_apply] } /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma binfi_apply' {ι} (I : set ι) (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) : (⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) := by { simp only [← infi_subtype'', infi_apply' _ hs] } lemma map_infi_le {ι β} (f : α → β) (m : ι → outer_measure α) : map f (⨅ i, m i) ≤ ⨅ i, map f (m i) := (map_mono f).map_infi_le lemma comap_infi {ι β} (f : α → β) (m : ι → outer_measure β) : comap f (⨅ i, m i) = ⨅ i, comap f (m i) := begin refine ext_nonempty (λ s hs, _), refine ((comap_mono f).map_infi_le s).antisymm _, simp only [comap_apply, infi_apply' _ hs, infi_apply' _ (hs.image _), le_infi_iff, set.image_subset_iff, preimage_Union], refine λ t ht, infi_le_of_le _ (infi_le_of_le ht $ ennreal.tsum_le_tsum $ λ k, _), exact infi_le_infi (λ i, (m i).mono (image_preimage_subset _ _)) end lemma map_infi {ι β} {f : α → β} (hf : injective f) (m : ι → outer_measure α) : map f (⨅ i, m i) = restrict (range f) (⨅ i, map f (m i)) := begin refine eq.trans _ (map_comap _ _), simp only [comap_infi, comap_map hf] end lemma map_infi_comap {ι β} [nonempty ι] {f : α → β} (m : ι → outer_measure β) : map f (⨅ i, comap f (m i)) = ⨅ i, map f (comap f (m i)) := begin refine (map_infi_le _ _).antisymm (λ s, _), simp only [map_apply, comap_apply, infi_apply, le_infi_iff], refine λ t ht, infi_le_of_le (λ n, f '' (t n) ∪ (range f)ᶜ) (infi_le_of_le _ _), { rw [← Union_union, set.union_comm, ← inter_subset, ← image_Union, ← image_preimage_eq_inter_range], exact image_subset _ ht }, { refine ennreal.tsum_le_tsum (λ n, infi_le_infi (λ i, (m i).mono _)), simp } end lemma map_binfi_comap {ι β} {I : set ι} (hI : I.nonempty) {f : α → β} (m : ι → outer_measure β) : map f (⨅ i ∈ I, comap f (m i)) = ⨅ i ∈ I, map f (comap f (m i)) := by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact map_infi_comap _ } lemma restrict_infi_restrict {ι} (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i, restrict s (m i)) = restrict s (⨅ i, m i) := calc restrict s (⨅ i, restrict s (m i)) = restrict (range (coe : s → α)) (⨅ i, restrict s (m i)) : by rw [subtype.range_coe] ... = map (coe : s → α) (⨅ i, comap coe (m i)) : (map_infi subtype.coe_injective _).symm ... = restrict s (⨅ i, m i) : congr_arg (map coe) (comap_infi _ _).symm lemma restrict_infi {ι} [nonempty ι] (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i, m i) = ⨅ i, restrict s (m i) := (congr_arg (map coe) (comap_infi _ _)).trans (map_infi_comap _) lemma restrict_binfi {ι} {I : set ι} (hI : I.nonempty) (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i ∈ I, m i) = ⨅ i ∈ I, restrict s (m i) := by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact restrict_infi _ _ } /-- This proves that Inf and restrict commute for outer measures, so long as the set of outer measures is nonempty. -/ lemma restrict_Inf_eq_Inf_restrict (m : set (outer_measure α)) {s : set α} (hm : m.nonempty) : restrict s (Inf m) = Inf ((restrict s) '' m) := by simp only [Inf_eq_infi, restrict_binfi, hm, infi_image] 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 = measurable_set`). 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 → ℝ≥0∞) /-- We can trivially extend a function defined on a subclass of objects (with codomain `ℝ≥0∞`) to all objects by defining it to be `∞` on the objects not in the class. -/ def extend (s : α) : ℝ≥0∞ := ⨅ h : P s, m s h lemma extend_eq {s : α} (h : P s) : extend m s = m s h := by simp [extend, h] lemma extend_eq_top {s : α} (h : ¬P s) : extend m s = ∞ := 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' } -- TODO: why this is a bad `congr` lemma? lemma extend_congr {β : Type*} {Pb : β → Prop} {mb : Π s : β, Pb s → ℝ≥0∞} {sa : α} {sb : β} (hP : P sa ↔ Pb sb) (hm : ∀ (ha : P sa) (hb : Pb sb), m sa ha = mb sb hb) : extend m sa = extend mb sb := infi_congr_Prop hP (λ h, hm _ _) end extend section extend_set variables {α : Type*} {P : set α → Prop} variables {m : Π (s : set α), P s → ℝ≥0∞} 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_decode₂, ← tsum_Union_decode₂], { exact extend_Union_nat PU (λ n, encodable.Union_decode₂_cases P0 hm) (mU _ (encodable.Union_decode₂_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} lemma le_induced_outer_measure {μ : outer_measure α} : μ ≤ induced_outer_measure m P0 m0 ↔ ∀ s (hs : P s), μ s ≤ m s hs := le_of_function.trans $ forall_congr $ λ s, le_infi_iff /-- If `P u` is `false` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = induced_outer_measure m P0 m0`. E.g., if `α` is an (e)metric space and `P u = diam u < r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma induced_outer_measure_union_of_false_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → ¬P u) : induced_outer_measure m P0 m0 (s ∪ t) = induced_outer_measure m P0 m0 s + induced_outer_measure m P0 m0 t := of_function_union_of_top_of_nonempty_inter $ λ u hsu htu, @infi_of_empty _ _ _ ⟨h u hsu htu⟩ _ 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 ≠ ∞) {ε : ℝ≥0} (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.measurable_set' 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 `measurable_set` 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 α), measurable_set s → ℝ≥0∞} variables (m0 : m ∅ measurable_set.empty = 0) variable (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃i, f i) (measurable_set.Union hm) = ∑'i, m (f i) (hm i)) include m0 mU lemma extend_mono {s₁ s₂ : set α} (h₁ : measurable_set s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := begin refine le_infi _, intro h₂, have := extend_union measurable_set.empty m0 measurable_set.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' measurable_set.Union _, intros f h, simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (measurable_set.disjointed h) (disjoint_disjointed _)], refine ennreal.tsum_le_tsum (λ i, _), rw [← extend_eq m, ← extend_eq m], exact extend_mono m0 mU (measurable_set.disjointed h _) (disjointed_le f _), end lemma induced_outer_measure_eq_extend {s : set α} (hs : measurable_set s) : induced_outer_measure m measurable_set.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 : measurable_set s) : induced_outer_measure m measurable_set.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) measurable_set.empty m.empty theorem le_trim : m ≤ m.trim := le_of_function.mpr $ λ s, le_infi $ λ _, le_refl _ theorem trim_eq {s : set α} (hs : measurable_set s) : m.trim s = m s := induced_outer_measure_eq' measurable_set.Union (λ f hf, m.Union_nat f) (λ _ _ _ _ h, m.mono h) hs theorem trim_congr {m₁ m₂ : outer_measure α} (H : ∀ {s : set α}, measurable_set s → m₁ s = m₂ s) : m₁.trim = m₂.trim := by { unfold trim, congr, funext s hs, exact H hs } @[mono] theorem trim_mono : monotone (trim : outer_measure α → outer_measure α) := λ m₁ m₂ H 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, measurable_set s → m₁ s ≤ m₂ s := le_of_function.trans $ forall_congr $ λ s, le_infi_iff theorem trim_le_trim_iff {m₁ m₂ : outer_measure α} : m₁.trim ≤ m₂.trim ↔ ∀ s, measurable_set s → m₁ s ≤ m₂ s := le_trim_iff.trans $ forall_congr $ λ s, forall_congr $ λ hs, by rw [trim_eq _ hs] theorem trim_eq_trim_iff {m₁ m₂ : outer_measure α} : m₁.trim = m₂.trim ↔ ∀ s, measurable_set s → m₁ s = m₂ s := by simp only [le_antisymm_iff, trim_le_trim_iff, forall_and_distrib] theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), m t := by { simp only [infi_comm] {single_pass := tt}, exact induced_outer_measure_eq_infi measurable_set.Union (λ f _, m.Union_nat f) (λ _ _ _ _ h, m.mono h) s } theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ measurable_set t}, m t := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim := trim_eq_trim_iff.2 $ λ s, m.trim_eq @[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 _ measurable_set.univ) (zero_le _) 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_measurable_superset_eq_trim (m : outer_measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ m t = m.trim s := begin simp only [trim_eq_infi], set ms := ⨅ (t : set α) (st : s ⊆ t) (ht : measurable_set t), m t, by_cases hs : ms = ∞, { simp only [hs], simp only [infi_eq_top] at hs, exact ⟨univ, subset_univ s, measurable_set.univ, hs _ (subset_univ s) measurable_set.univ⟩ }, { have : ∀ r > ms, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < r, { intros r hs, simpa [infi_lt_iff] using hs }, have : ∀ n : ℕ, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < ms + n⁻¹, { assume n, refine this _ (ennreal.lt_add_right hs _), exact (ennreal.inv_pos.2 $ ennreal.nat_ne_top _) }, choose t hsub hm hm', refine ⟨⋂ n, t n, subset_Inter hsub, measurable_set.Inter hm, _⟩, have : tendsto (λ n : ℕ, ms + n⁻¹) at_top (𝓝 (ms + 0)), from tendsto_const_nhds.add ennreal.tendsto_inv_nat_nhds_zero, rw add_zero at this, refine le_antisymm (ge_of_tendsto' this $ λ n, _) _, { exact le_trans (m.mono' $ Inter_subset t n) (hm' n).le }, { refine infi_le_of_le (⋂ n, t n) _, refine infi_le_of_le (subset_Inter hsub) _, refine infi_le _ (measurable_set.Inter hm) } } end lemma exists_measurable_superset_of_trim_eq_zero {m : outer_measure α} {s : set α} (h : m.trim s = 0) : ∃t, s ⊆ t ∧ measurable_set t ∧ m t = 0 := begin rcases exists_measurable_superset_eq_trim m s with ⟨t, hst, ht, hm⟩, exact ⟨t, hst, ht, h ▸ hm⟩ end /-- If `μ i` is a countable family of outer measures, then for every set `s` there exists a measurable set `t ⊇ s` such that `μ i t = (μ i).trim s` for all `i`. -/ lemma exists_measurable_superset_forall_eq_trim {ι} [encodable ι] (μ : ι → outer_measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = (μ i).trim s := begin choose t hst ht hμt using λ i, (μ i).exists_measurable_superset_eq_trim s, replace hst := subset_Inter hst, replace ht := measurable_set.Inter ht, refine ⟨⋂ i, t i, hst, ht, λ i, le_antisymm _ _⟩, exacts [hμt i ▸ (μ i).mono (Inter_subset _ _), (mono' _ hst).trans_eq ((μ i).trim_eq ht)] end /-- If `m₁ s = op (m₂ s) (m₃ s)` for all `s`, then the same is true for `m₁.trim`, `m₂.trim`, and `m₃ s`. -/ theorem trim_binop {m₁ m₂ m₃ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞ → ℝ≥0∞} (h : ∀ s, m₁ s = op (m₂ s) (m₃ s)) (s : set α) : m₁.trim s = op (m₂.trim s) (m₃.trim s) := begin rcases exists_measurable_superset_forall_eq_trim (![m₁, m₂, m₃]) s with ⟨t, hst, ht, htm⟩, simp only [fin.forall_fin_succ, matrix.cons_val_zero, matrix.cons_val_succ] at htm, rw [← htm.1, ← htm.2.1, ← htm.2.2.1, h] end /-- If `m₁ s = op (m₂ s)` for all `s`, then the same is true for `m₁.trim` and `m₂.trim`. -/ theorem trim_op {m₁ m₂ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞} (h : ∀ s, m₁ s = op (m₂ s)) (s : set α) : m₁.trim s = op (m₂.trim s) := @trim_binop α _ m₁ m₂ 0 (λ a b, op a) h s /-- `trim` is additive. -/ theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim := ext $ trim_binop (add_apply m₁ m₂) /-- `trim` respects scalar multiplication. -/ theorem trim_smul (c : ℝ≥0∞) (m : outer_measure α) : (c • m).trim = c • m.trim := ext $ trim_op (smul_apply c m) /-- `trim` sends the supremum of two outer measures to the supremum of the trimmed measures. -/ theorem trim_sup (m₁ m₂ : outer_measure α) : (m₁ ⊔ m₂).trim = m₁.trim ⊔ m₂.trim := ext $ λ s, (trim_binop (sup_apply m₁ m₂) s).trans (sup_apply _ _ _).symm /-- `trim` sends the supremum of a countable family of outer measures to the supremum of the trimmed measures. -/ lemma trim_supr {ι} [encodable ι] (μ : ι → outer_measure α) : trim (⨆ i, μ i) = ⨆ i, trim (μ i) := begin ext1 s, rcases exists_measurable_superset_forall_eq_trim (λ o, option.elim o (supr μ) μ) s with ⟨t, hst, ht, hμt⟩, simp only [option.forall, option.elim] at hμt, simp only [supr_apply, ← hμt.1, ← hμt.2] end /-- The trimmed property of a measure μ states that `μ.to_outer_measure.trim = μ.to_outer_measure`. This theorem shows that a restricted trimmed outer measure is a trimmed outer measure. -/ lemma restrict_trim {μ : outer_measure α} {s : set α} (hs : measurable_set s) : (restrict s μ).trim = restrict s μ.trim := begin refine le_antisymm (λ t, _) (le_trim_iff.2 $ λ t ht, _), { rw restrict_apply, rcases μ.exists_measurable_superset_eq_trim (t ∩ s) with ⟨t', htt', ht', hμt'⟩, rw [← hμt'], rw inter_subset at htt', refine (mono' _ htt').trans _, rw [trim_eq _ (hs.compl.union ht'), restrict_apply, union_inter_distrib_right, compl_inter_self, set.empty_union], exact μ.mono' (inter_subset_left _ _) }, { rw [restrict_apply, trim_eq _ (ht.inter hs), restrict_apply], exact le_rfl } end end outer_measure end measure_theory
08c3f5ce31011af0d858d5b3717d34744f88681f
d4f8579bd5c4a56a88575ff0f1957193d83cf965
/src/steele.lean
9293fedc62d6955b0c4be4777361d789d640ba08
[]
no_license
ldct/lean-textbooks
5b14efc448c6c0ee6b2a89d1c2243c5e343ea88c
6550da16cd3f4c31145d3eb759a9326740a7dcd6
refs/heads/master
1,668,538,459,925
1,594,208,495,000
1,594,208,495,000
271,553,644
0
0
null
null
null
null
UTF-8
Lean
false
false
661
lean
-- Graham, Knuth, Patashnik -- Concrete Mathematics import tactic import data.real.basic import data.num.basic lemma l1 (x y : ℝ) : 0 ≤ x^2-2*x*y + y^2 := begin have h1 : 0 ≤ (x - y)^2, exact pow_two_nonneg (x - y), ring SOP at h1, ring at h1, ring, exact h1, end example (a b : ℝ) : a ≤ b → 0 ≤ b - a := begin exact sub_nonneg.mpr, end -- CSI, two variable case -- todo: use sqrt? theorem csi_two_variable (a1 a2 b1 b2 : ℝ) : (a1*b1 + a2*b2)^2 ≤ (a1^2 + a2^2)*(b1^2 + b2^2) := begin have h1 := l1 (a1*b2) (a2*b1), rw ← sub_nonneg, ring SOP at h1, ring SOP at h1, ring SOP, ring SOP, ring SOP, exact h1, end
3f2fe55c1c26fdd1d42d4be0ac50cbafa799c5d3
d744d97b07fc1e61b0ebea192b2d624125898128
/test/tree_test.lean
9edba72df8373532645a508589c1af1095c3ab33
[ "MIT" ]
permissive
parkersullivan/leanstuff
019c757848dd8f5db185e6973969141aee6fef7c
3da132c44d365ecd933de64fa04ba66b9e620683
refs/heads/master
1,599,036,040,845
1,573,316,625,000
1,573,316,625,000
219,181,711
0
0
null
null
null
null
UTF-8
Lean
false
false
967
lean
import ..src.tree open my_tree /-defining some tree instances-/ def t0 : (tree ℕ ) := @tree.empty ℕ def t1 : (tree ℕ) := tree.node 5 t0 t0 def t2 : (tree nat) := tree.node 2 t1 t0 def t3 : (tree nat) := tree.node 7 t0 t1 def t4 : (tree nat) := tree.node 12 t2 t1 /-testing out the sum_node function-/ #eval sum_node t0 #eval sum_node t1 #eval sum_node t2 #eval sum_node t3 #eval sum_node t4 /-testing out the tree_map function-/ #reduce tree_map (λ (n: ℕ), (n + 1)) t0 #reduce tree_map (λ (n: ℕ), (n + 1)) t1 #reduce tree_map (λ (n: ℕ), (n + 1)) t2 #reduce tree_map (λ (n: ℕ), (n + 1)) t3 #reduce tree_map (λ (n: ℕ), (n + 1)) t4 theorem five_eq_five : eq (2 + 3) 5 := (eq.refl 5) theorem zero_eq_zero : 0 = 0 := eq.refl 0 theorem zez_and_5eq5 : (0 = 0) ∧ (5 = 5) := and.intro (eq.refl 0) (eq.refl 5) theorem all_n_self_eq : ∀ (n : ℕ), n = n := λ (x : ℕ), (eq.refl x) #check all_n_self_eq #reduce all_n_self_eq 7
1498c927d13268a36a6e05fdc36b317969838e57
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/types/equiv.hlean
2fce2cfa211f343ef07e05da3c1b008ae0051fcc
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,364
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 open eq is_trunc sigma sigma.ops pi fiber function equiv equiv.ops 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 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.refl 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 end equiv
a10733ac6d86a6f09febc3d782e4880086203788
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/nested_rec.lean
f0b14664735c988675ec31db4557f83f9ec72945
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,317
lean
open nat prod sigma -- We will define the following example by well-foudned recursion -- g 0 := 0 -- g (succ x) := g (g x) definition g.F (x : nat) : (Π y, y < x → Σ r : nat, r ≤ y) → Σ r : nat, r ≤ x := nat.cases_on x (λ f, ⟨zero, le.refl zero⟩) (λ x₁ (f : Π y, y < succ x₁ → Σ r : nat, r ≤ y), let p₁ := f x₁ (lt.base x₁) in let gx₁ := pr₁ p₁ in let p₂ := f gx₁ (lt_of_le_of_lt (pr₂ p₁) (lt.base x₁)) in let ggx₁ := pr₁ p₂ in ⟨ggx₁, le_succ_of_le (le.trans (pr₂ p₂) (pr₂ p₁))⟩) definition g (x : nat) : nat := pr₁ (well_founded.fix g.F x) example : g 3 = 0 := rfl example : g 6 = 0 := rfl theorem g_zero : g 0 = 0 := rfl theorem g_succ (a : nat) : g (succ a) = g (g a) := have aux : well_founded.fix g.F (succ a) = sigma.mk (g (g a)) _, from well_founded.fix_eq g.F (succ a), calc g (succ a) = pr₁ (well_founded.fix g.F (succ a)) : rfl ... = pr₁ (sigma.mk (g (g a)) _) : {aux} ... = g (g a) : rfl theorem g_all_zero (a : nat) : g a = zero := nat.induction_on a g_zero (λ a₁ (ih : g a₁ = zero), calc g (succ a₁) = g (g a₁) : g_succ ... = g 0 : ih ... = 0 : g_zero)
3963f09a0ae8ad1edccd9ece76d26912704c5e7f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/limits/preserves/shapes/kernels.lean
a174f04aa2e1cb08718072aa263c23e72153a3e8
[ "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
8,639
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.kernels import category_theory.limits.preserves.shapes.zero /-! # Preserving (co)kernels Constructions to relate the notions of preserving (co)kernels and reflecting (co)kernels to concrete (co)forks. In particular, we show that `kernel_comparison f g G` is an isomorphism iff `G` preserves the limit of the parallel pair `f,0`, as well as the dual result. -/ noncomputable theory universes v₁ v₂ u₁ u₂ open category_theory category_theory.category category_theory.limits variables {C : Type u₁} [category.{v₁} C] [has_zero_morphisms C] variables {D : Type u₂} [category.{v₂} D] [has_zero_morphisms D] variables (G : C ⥤ D) [functor.preserves_zero_morphisms G] namespace category_theory.limits section kernels variables {X Y Z : C} {f : X ⟶ Y} {h : Z ⟶ X} (w : h ≫ f = 0) /-- The map of a kernel fork is a limit iff the kernel fork consisting of the mapped morphisms is a limit. This essentially lets us commute `kernel_fork.of_ι` with `functor.map_cone`. This is a variant of `is_limit_map_cone_fork_equiv` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_limit_map_cone_fork_equiv' : is_limit (G.map_cone (kernel_fork.of_ι h w)) ≃ is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : fork (G.map f) 0) := begin refine (is_limit.postcompose_hom_equiv _ _).symm.trans (is_limit.equiv_iso_limit _), refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp, refine fork.ext (iso.refl _) _, simp [fork.ι] end /-- The property of preserving kernels expressed in terms of kernel forks. This is a variant of `is_limit_fork_map_of_is_limit` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_limit_fork_map_of_is_limit' [preserves_limit (parallel_pair f 0) G] (l : is_limit (kernel_fork.of_ι h w)) : is_limit (kernel_fork.of_ι (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : fork (G.map f) 0) := is_limit_map_cone_fork_equiv' G w (preserves_limit.preserves l) variables (f) [has_kernel f] /-- If `G` preserves kernels and `C` has them, then the fork constructed of the mapped morphisms of a kernel fork is a limit. -/ def is_limit_of_has_kernel_of_preserves_limit [preserves_limit (parallel_pair f 0) G] : is_limit (fork.of_ι (G.map (kernel.ι f)) (by simp only [←G.map_comp, equalizer.condition, comp_zero, functor.map_zero]) : fork (G.map f) 0) := is_limit_fork_map_of_is_limit' G (kernel.condition f) (kernel_is_kernel f) instance [preserves_limit (parallel_pair f 0) G] : has_kernel (G.map f) := { exists_limit := ⟨⟨_, is_limit_of_has_kernel_of_preserves_limit G f⟩⟩, } variables [has_kernel (G.map f)] /-- If the kernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the kernel of `f`. -/ def preserves_kernel.of_iso_comparison [i : is_iso (kernel_comparison f G)] : preserves_limit (parallel_pair f 0) G := begin apply preserves_limit_of_preserves_limit_cone (kernel_is_kernel f), apply (is_limit_map_cone_fork_equiv' G (kernel.condition f)).symm _, apply is_limit.of_point_iso (kernel_is_kernel (G.map f)), exact i, end variables [preserves_limit (parallel_pair f 0) G] /-- If `G` preserves the kernel of `f`, then the kernel comparison map for `G` at `f` is an isomorphism. -/ def preserves_kernel.iso : G.obj (kernel f) ≅ kernel (G.map f) := is_limit.cone_point_unique_up_to_iso (is_limit_of_has_kernel_of_preserves_limit G f) (kernel_is_kernel _) @[simp] lemma preserves_kernel.iso_hom : (preserves_kernel.iso G f).hom = kernel_comparison f G := rfl instance : is_iso (kernel_comparison f G) := begin rw ← preserves_kernel.iso_hom, apply_instance end @[reassoc] lemma kernel_map_comp_preserves_kernel_iso_inv {X' Y' : C} (g : X' ⟶ Y') [has_kernel g] [has_kernel (G.map g)] [preserves_limit (parallel_pair g 0) G] (p : X ⟶ X') (q : Y ⟶ Y') (hpq : f ≫ q = p ≫ g) : kernel.map (G.map f) (G.map g) (G.map p) (G.map q) (by rw [←G.map_comp, hpq, G.map_comp]) ≫ (preserves_kernel.iso G _).inv = (preserves_kernel.iso G _).inv ≫ G.map (kernel.map f g p q hpq) := begin rw [iso.comp_inv_eq, category.assoc, preserves_kernel.iso_hom, iso.eq_inv_comp], exact kernel_comparison_comp_kernel_map _ _ _ _ _ _, end end kernels section cokernels variables {X Y Z : C} {f : X ⟶ Y} {h : Y ⟶ Z} (w : f ≫ h = 0) /-- The map of a cokernel cofork is a colimit iff the cokernel cofork consisting of the mapped morphisms is a colimit. This essentially lets us commute `cokernel_cofork.of_π` with `functor.map_cocone`. This is a variant of `is_colimit_map_cocone_cofork_equiv` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_colimit_map_cocone_cofork_equiv' : is_colimit (G.map_cocone (cokernel_cofork.of_π h w)) ≃ is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : cofork (G.map f) 0) := begin refine (is_colimit.precompose_hom_equiv _ _).symm.trans (is_colimit.equiv_iso_colimit _), refine parallel_pair.ext (iso.refl _) (iso.refl _) _ _; simp, refine cofork.ext (iso.refl _) _, simp only [cofork.π, iso.refl_hom, id_comp, cocones.precompose_obj_ι, nat_trans.comp_app, parallel_pair.ext_hom_app, functor.map_cocone_ι_app, cofork.of_π_ι_app], apply category.comp_id end /-- The property of preserving cokernels expressed in terms of cokernel coforks. This is a variant of `is_colimit_cofork_map_of_is_colimit` for equalizers, which we can't use directly between `G.map 0 = 0` does not hold definitionally. -/ def is_colimit_cofork_map_of_is_colimit' [preserves_colimit (parallel_pair f 0) G] (l : is_colimit (cokernel_cofork.of_π h w)) : is_colimit (cokernel_cofork.of_π (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) : cofork (G.map f) 0) := is_colimit_map_cocone_cofork_equiv' G w (preserves_colimit.preserves l) variables (f) [has_cokernel f] /-- If `G` preserves cokernels and `C` has them, then the cofork constructed of the mapped morphisms of a cokernel cofork is a colimit. -/ def is_colimit_of_has_cokernel_of_preserves_colimit [preserves_colimit (parallel_pair f 0) G] : is_colimit (cofork.of_π (G.map (cokernel.π f)) (by simp only [←G.map_comp, coequalizer.condition, zero_comp, functor.map_zero]) : cofork (G.map f) 0) := is_colimit_cofork_map_of_is_colimit' G (cokernel.condition f) (cokernel_is_cokernel f) instance [preserves_colimit (parallel_pair f 0) G] : has_cokernel (G.map f) := { exists_colimit := ⟨⟨_, is_colimit_of_has_cokernel_of_preserves_colimit G f⟩⟩, } variables [has_cokernel (G.map f)] /-- If the cokernel comparison map for `G` at `f` is an isomorphism, then `G` preserves the cokernel of `f`. -/ def preserves_cokernel.of_iso_comparison [i : is_iso (cokernel_comparison f G)] : preserves_colimit (parallel_pair f 0) G := begin apply preserves_colimit_of_preserves_colimit_cocone (cokernel_is_cokernel f), apply (is_colimit_map_cocone_cofork_equiv' G (cokernel.condition f)).symm _, apply is_colimit.of_point_iso (cokernel_is_cokernel (G.map f)), exact i, end variables [preserves_colimit (parallel_pair f 0) G] /-- If `G` preserves the cokernel of `f`, then the cokernel comparison map for `G` at `f` is an isomorphism. -/ def preserves_cokernel.iso : G.obj (cokernel f) ≅ cokernel (G.map f) := is_colimit.cocone_point_unique_up_to_iso (is_colimit_of_has_cokernel_of_preserves_colimit G f) (cokernel_is_cokernel _) @[simp] lemma preserves_cokernel.iso_inv : (preserves_cokernel.iso G f).inv = cokernel_comparison f G := rfl instance : is_iso (cokernel_comparison f G) := begin rw ← preserves_cokernel.iso_inv, apply_instance end @[reassoc] lemma preserves_cokernel_iso_comp_cokernel_map {X' Y' : C} (g : X' ⟶ Y') [has_cokernel g] [has_cokernel (G.map g)] [preserves_colimit (parallel_pair g 0) G] (p : X ⟶ X') (q : Y ⟶ Y') (hpq : f ≫ q = p ≫ g) : (preserves_cokernel.iso G _).hom ≫ cokernel.map (G.map f) (G.map g) (G.map p) (G.map q) (by rw [←G.map_comp, hpq, G.map_comp]) = G.map (cokernel.map f g p q hpq) ≫ (preserves_cokernel.iso G _).hom := begin rw [←iso.comp_inv_eq, category.assoc, ←iso.eq_inv_comp], exact cokernel_map_comp_cokernel_comparison _ _ _ _ _ _, end end cokernels end category_theory.limits
bef3ad48efed3bd89166f12b0d31be8bddd9e810
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/array.lean
5713f57100a856cc754f3399f0a2373725fd41da
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
4,903
lean
import data.list import data.list.basic import data.array.lemmas import util.data.fin import util.data.list import util.data.order import util.meta.tactic universe variables u₀ u₁ u₂ variables {α : Type u₀} {β β' : Type u₁} {γ : Type u₂} open nat namespace array def push_aux {n : ℕ} (x : α) (ar : fin n → α) : fin (succ n) → α | ⟨0,_⟩ := x | ⟨succ i,P⟩ := ar ⟨i,lt_of_succ_lt_succ P⟩ def push {n : ℕ} (x : α) : array n α → array (succ n) α | ⟨ ar ⟩ := ⟨ push_aux x ar ⟩ def pop {n : ℕ} : array (succ n) α → array n α | ⟨ ar ⟩ := ⟨ ar ∘ fin.succ ⟩ def head {n : ℕ} : array (succ n) α → α | ⟨ ar ⟩ := ar 0 lemma push_head_pop {n : ℕ} (xs : array (succ n) α) : push (head xs) (pop xs) = xs := sorry open d_array -- (rev_iterate_aux rev_iterate) lemma rev_iterate_push {n : ℕ} (x : α) (xs : array n α) (i : β) (f : fin (succ n) → α → β → β) : rev_iterate (push x xs) i f = f 0 x (rev_iterate xs i (f ∘ fin.succ)) := begin revert x xs, induction n with n IH ; intros x xs, { cases xs with xs, refl, }, { rw [← push_head_pop xs,IH], unfold function.comp, rw [push_head_pop xs], admit } end def maximum_aux {n : ℕ} (a : array n ℕ) (i : ℕ) (H : i ≤ n) : ℕ := d_array.iterate_aux a (λ _, max) i H 0 lemma le_maximum_aux {n : ℕ} (a : array n ℕ) (k : ℕ) (Hk : k ≤ n) (i : fin n) (Hi : i.val < k) : a.read i ≤ maximum_aux a k Hk := begin induction k, case nat.zero { admit }, case nat.succ : k { unfold maximum_aux iterate_aux, simp, admit }, end def maximum {n : ℕ} (a : array n ℕ) : ℕ := maximum_aux a n (by refl) lemma le_maximum {n : ℕ} (a : array n ℕ) (i : fin n) : a.read i ≤ maximum a := begin apply le_maximum_aux, apply i.is_lt end lemma rev_list_eq_nil (ar : array 0 α) : ar.rev_list = [] := by refl lemma to_list_eq_nil (ar : array 0 α) : ar.to_list = [] := by refl lemma iterate_aux_pop {n : ℕ} (ar : array (succ n) α) (f : fin (succ n) → α → β → β) (x : β) {k : ℕ} (Hk : k ≤ n) : iterate_aux ar f (succ k) (succ_le_succ Hk) x = iterate_aux (pop ar) (f ∘ fin.succ) k Hk (f 0 (ar.read 0) x) := begin induction k, case zero { refl }, case succ : k { rw d_array.iterate_aux.equations._eqn_2 (pop ar), simp, rw ← k_ih, unfold iterate_aux, simp, apply congr_fun, cases ar, refl } end lemma rev_list_eq_cons_aux {n k : ℕ} (ar : array n α) (xs : list α) (H : k ≤ n) : iterate_aux ar (λ _, list.cons) k H xs = iterate_aux ar (λ _, list.cons) k H [] ++ xs := begin induction k, { unfold iterate_aux, simp }, { unfold iterate_aux, simp [k_ih], }, end lemma rev_list_eq_cons {n : ℕ} (ar : array (succ n) α) : ar.rev_list = ar.pop.rev_list ++ [ar.read 0] := begin unfold rev_list foldl iterate d_array.iterate, rw [iterate_aux_pop _ _ _ (le_refl n),rev_list_eq_cons_aux], end lemma iterate_aux_pop_back {n : ℕ} (ar : array (succ n) α) (f : fin (succ n) → α → β → β) (x : β) (k : ℕ) (Hk : k ≤ n) : iterate_aux ar f k (le_succ_of_le Hk) x = iterate_aux (pop_back ar) (restr f) k Hk x := begin induction k, case zero { refl }, case succ : k { unfold iterate_aux, simp, apply congr_arg, apply k_ih }, end lemma rev_list_eq_append {n : ℕ} (ar : array (succ n) α) : ar.rev_list = ar.read fin.max :: ar.pop_back.rev_list := begin unfold rev_list foldl iterate d_array.iterate iterate_aux, simp, split, refl, apply iterate_aux_pop_back, end end array lemma fin.sum_zero [has_add α] [has_zero α] (f : fin 0 → α) : fin.sum 0 f = 0 := rfl open array lemma fin.sum_succ {n : ℕ} [add_comm_monoid α] (f : fin (succ n) → α) : fin.sum (succ n) f = fin.sum n (f ∘ fin.succ) + f 0 := begin unfold fin.sum fin.foldl, repeat { rw [← rev_list_foldr] }, simp! [d_array.foldl,array.rev_list_eq_cons,d_array.iterate,d_array.iterate_aux ,d_array.read,d_array.data,array.pop], symmetry, simp [list.foldr_hom (has_add.add (f 0)) _ has_add.add], refl, end @[simp] lemma array_widen_eq_pop_back {n : ℕ} (f : fin (succ n) → α) : d_array.mk (f ∘ widen) = array.pop_back ⟨f⟩ := begin ext1, cases i, refl, end attribute [extensionality] d_array.ext' lemma push_pop_back {n : ℕ} (ar : array n.succ α) : push_back (pop_back ar) (ar.read fin.max) = ar := by { ext ⟨i,h⟩, cases ar, simp! only [d_array.read,array.read,push_back,pop_back], split_ifs; [subst i, skip]; refl } open list lemma fin.sum_succ' {n : ℕ} [add_comm_monoid α] (f : fin (succ n) → α) : fin.sum (succ n) f = fin.sum n (f ∘ widen) + f fin.max := begin unfold fin.sum fin.foldl, rw [← to_list_foldl,← to_list_foldl, ← push_pop_back ⟨f⟩, push_back_to_list, ← array_widen_eq_pop_back], simp [array.read,d_array.read], end
c9dad7f0d216d4596c15860dd4fc71dcce47804f
f23fc72f73d16cfbf5f27a7250cb58a06dc1c518
/complex.lean
5096cab8929522cf47f27a2bfdfb936a950ae3e9
[ "BSD-3-Clause" ]
permissive
emc2/leanstuff
2e7a9bc05b082c5fefd39c66420ca35b1d50e6ae
609efb86cec168eae73a799f8dff7993eec0ee45
refs/heads/master
1,473,171,453,604
1,439,952,548,000
1,439,952,548,000
40,196,136
0
0
null
null
null
null
UTF-8
Lean
false
false
21,519
lean
import algebra.group import algebra.ring import algebra.field import data.prod namespace algebra open prod ring definition complex_ty (A : Type) : Type := A × A structure complex [class] (A : Type) := (ℂ : Type) (real : A -> ℂ) (i : ℂ) (complex_conj : ℂ → ℂ) (abs : ℂ -> A) namespace complex_ty definition mk_complex_impl {A : Type} (real : A) (imag : A) := (real, imag) definition complex_conj_impl {A : Type} [hn : has_neg A] : complex_ty A → complex_ty A | (rval, ival) := (rval, -ival) definition complex_zero {A : Type} [hz : has_zero A] : complex_ty A := (0, 0) definition complex_has_zero [instance] {A : Type} [hz : has_zero A] : has_zero (complex_ty A) := ⦃ has_zero, zero := complex_zero ⦄ definition complex_one {A : Type} [hz : has_zero A] [ho : has_one A] : complex_ty A := (1, 0) definition complex_has_one [instance] {A : Type} [hz : has_zero A] [hz : has_one A] : has_one (complex_ty A) := ⦃ has_one, one := complex_one ⦄ definition complex_i {A : Type} [hz : has_zero A] [hz : has_one A] : complex_ty A := (0, 1) definition complex_add {A : Type} [ha : has_add A] (v1 : complex_ty A) (v2 : complex_ty A) : complex_ty A := let r1 : A := pr1 v1, r2 : A := pr1 v2, i1 : A := pr2 v1, i2 : A := pr2 v2 in (r1 + r2, i1 + i2) definition complex_has_add [instance] {A : Type} [ha : has_add A] : has_add (complex_ty A) := ⦃ has_add, add := complex_add ⦄ definition complex_mul {A : Type} [ag : add_group A] [hm : has_mul A] (v1 : complex_ty A) (v2 : complex_ty A) : complex_ty A := let r1 := pr1 v1, r2 := pr1 v2, i1 := pr2 v1, i2 := pr2 v2 in ((r1 * r2) - (i1 * i2), (r1 * i2) + (i1 * r2)) definition complex_has_mul [instance] {A : Type} [ag : add_group A] [hm : has_mul A] : has_mul (complex_ty A) := ⦃ has_mul, mul := complex_mul ⦄ definition complex_neg {A : Type} [hn : has_neg A] (c : complex_ty A) : complex_ty A := let r := pr1 c, i := pr2 c in (-r, -i) definition complex_has_neg [instance] {A : Type} [hn : has_neg A] : has_neg (complex_ty A) := ⦃ has_neg, neg := complex_neg ⦄ definition complex_inv {A : Type} [hi : has_inv A] [ha : has_add A] [hm : has_mul A] [hn : has_neg A] (c : complex_ty A) : complex_ty A := let r := pr1 c, i := pr2 c in (r * ((r * r) + (i * i))⁻¹, -(i * ((r * r) + (i * i))⁻¹)) theorem complex_add_assoc {A : Type} [asg : add_semigroup A] : ∀ (c1 c2 c3 : complex_ty A), complex_add (complex_add c1 c2) c3 = complex_add c1 (complex_add c2 c3) := take c1 : complex_ty A, take c2 : complex_ty A, take c3 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, r3 := pr1 c3, i1 := pr2 c1, i2 := pr2 c2, i3 := pr2 c3 in show (r1 + r2 + r3, i1 + i2 + i3) = (r1 + (r2 + r3), i1 + (i2 + i3)), from pair_eq (by rewrite add.assoc) (by rewrite add.assoc) definition complex_add_semigroup [instance] {A : Type} [asg : add_semigroup A] : add_semigroup (complex_ty A) := ⦃ add_semigroup, add_assoc := complex_add_assoc ⦄ theorem complex_zero_add {A : Type} [am : add_monoid A] : ∀ (c : complex_ty A), complex_add complex_zero c = c := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (0 + r, 0 + i) = c, from prod.eq (calc pr₁ (0 + r, 0 + i) = 0 + r : by esimp ... = r : by rewrite zero_add) (calc pr₂ (0 + r, 0 + i) = 0 + i : by esimp ... = i : by rewrite zero_add) theorem complex_add_zero {A : Type} [am : add_monoid A] : ∀ (c : complex_ty A), complex_add c complex_zero = c := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (r + 0, i + 0) = c, from prod.eq (calc pr₁ (r + 0, i + 0) = r + 0 : by esimp ... = r : by rewrite add_zero) (calc pr₂ (r + 0, i + 0) = i + 0 : by esimp ... = i : by rewrite add_zero) definition complex_add_monoid [instance] {A : Type} [am : add_monoid A] : add_monoid (complex_ty A) := ⦃ add_monoid, add_assoc := complex_add_assoc, zero_add := complex_zero_add, add_zero := complex_add_zero ⦄ theorem complex_add_comm {A : Type} [acsg : add_comm_semigroup A] : ∀ (c1 c2 : complex_ty A), complex_add c1 c2 = complex_add c2 c1 := take c1 : complex_ty A, take c2 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, i1 := pr2 c1, i2 := pr2 c2 in show (r1 + r2, i1 + i2) = (r2 + r1, i2 + i1), from pair_eq (by rewrite add.comm) (by rewrite add.comm) definition complex_add_comm_semigroup [instance] {A : Type} [asg : add_comm_semigroup A] : add_comm_semigroup (complex_ty A) := ⦃ add_comm_semigroup, add_assoc := complex_add_assoc, add_comm := complex_add_comm ⦄ definition complex_add_comm_monoid [instance] {A : Type} [acm : add_comm_monoid A] : add_comm_monoid (complex_ty A) := ⦃ add_comm_monoid, add_assoc := complex_add_assoc, zero_add := complex_zero_add, add_zero := complex_add_zero, add_comm := complex_add_comm ⦄ theorem complex_add_left_inv {A : Type} [ag : add_group A] : ∀ (c : complex_ty A), complex_add (complex_neg c) c = complex_zero := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (-r + r, -i + i) = (0, 0), from pair_eq (add_group.add_left_inv r) (add_group.add_left_inv i) definition complex_add_group [instance] {A : Type} [ag : add_group A] : add_group (complex_ty A) := ⦃ add_group, add_assoc := complex_add_assoc, zero_add := complex_zero_add, add_zero := complex_add_zero, add_left_inv := complex_add_left_inv ⦄ definition complex_add_comm_group [instance] {A : Type} [acg : add_comm_group A] : add_comm_group (complex_ty A) := ⦃ add_comm_group, add_assoc := complex_add_assoc, add_comm := complex_add_comm, zero_add := complex_zero_add, add_zero := complex_add_zero, add_left_inv := complex_add_left_inv ⦄ theorem complex_mul_assoc {A : Type} [r : ring A] : ∀ (c1 c2 c3 : complex_ty A), complex_mul (complex_mul c1 c2) c3 = complex_mul c1 (complex_mul c2 c3) := take c1 : complex_ty A, take c2 : complex_ty A, take c3 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, r3 := pr1 c3, i1 := pr2 c1, i2 := pr2 c2, i3 := pr2 c3 in show ((r1 * r2 - i1 * i2) * r3 - (r1 * i2 + i1 * r2) * i3, (r1 * r2 - i1 * i2) * i3 + (r1 * i2 + i1 * r2) * r3) = (r1 * (r2 * r3 - i2 * i3) - i1 * (r2 * i3 + i2 * r3), pr₁ c1 * (r2 * i3 + i2 * r3) + i1 * (r2 * r3 - i2 * i3)), from pair_eq (calc (r1 * r2 - i1 * i2) * r3 - (r1 * i2 + i1 * r2) * i3 = r1 * r2 * r3 - i1 * i2 * r3 - (r1 * i2 + i1 * r2) * i3 : by rewrite mul_sub_right_distrib ... = r1 * r2 * r3 - i1 * i2 * r3 - (r1 * i2 * i3 + i1 * r2 * i3) : by rewrite right_distrib ... = r1 * r2 * r3 - i1 * i2 * r3 - i1 * r2 * i3 - r1 * i2 * i3 : by rewrite sub_add_eq_sub_sub_swap ... = -(r1 * i2 * i3) + (r1 * r2 * r3 + (-(i1 * i2 * r3) - i1 * r2 * i3)) : by rewrite [add.comm, add.assoc] ... = -(r1 * i2 * i3) + (r1 * r2 * r3 - (i1 * r2 * i3 + i1 * i2 * r3)) : by rewrite -neg_add_rev ... = -(r1 * i2 * i3) + (r1 * r2 * r3 - (i1 * (r2 * i3) + i1 * (i2 * r3))) : by rewrite -*mul.assoc ... = -(r1 * i2 * i3) + (r1 * r2 * r3 - i1 * (r2 * i3 + i2 * r3)) : by rewrite -left_distrib ... = (-(r1 * i2 * i3) + r1 * r2 * r3) - i1 * (r2 * i3 + i2 * r3) : by rewrite add.assoc ... = (r1 * r2 * r3 - r1 * i2 * i3) - i1 * (r2 * i3 + i2 * r3) : by krewrite add.comm ... = (r1 * (r2 * r3) - r1 * (i2 * i3)) - i1 * (r2 * i3 + i2 * r3) : by rewrite *mul.assoc ... = (r1 * (r2 * r3) + r1 * -(i2 * i3)) - i1 * (r2 * i3 + i2 * r3) : by rewrite -neg_mul_eq_mul_neg ... = r1 * (r2 * r3 -i2 * i3) - i1 * (r2 * i3 + i2 * r3) : by rewrite -left_distrib) (calc (r1 * r2 - i1 * i2) * i3 + (r1 * i2 + i1 * r2) * r3 = r1 * r2 * i3 - i1 * i2 * i3 + (r1 * i2 + i1 * r2) * r3 : by rewrite mul_sub_right_distrib ... = r1 * r2 * i3 - i1 * i2 * i3 + (r1 * i2 * r3 + i1 * r2 * r3) : by rewrite right_distrib ... = (r1 * r2 * i3 - i1 * i2 * i3) + r1 * i2 * r3 + i1 * r2 * r3 : by rewrite *add.assoc ... = (-(i1 * i2 * i3) + r1 * r2 * i3) + r1 * i2 * r3 + i1 * r2 * r3 : by rewrite {_ + r1 * r2 * i3}add.comm ... = -(i1 * i2 * i3) + (r1 * r2 * i3 + (r1 * i2 * r3 + i1 * r2 * r3)) : by rewrite *add.assoc ... = (r1 * r2 * i3 + (r1 * i2 * r3 + i1 * r2 * r3)) -(i1 * i2 * i3) : by rewrite add.comm ... = r1 * r2 * i3 + r1 * i2 * r3 + (i1 * r2 * r3 - i1 * i2 * i3) : by rewrite *add.assoc ... = r1 * (r2 * i3) + r1 * (i2 * r3) + (i1 * (r2 * r3) - i1 * (i2 * i3)) : by rewrite *mul.assoc ... = r1 * (r2 * i3 + i2 * r3) + (i1 * (r2 * r3) - i1 * (i2 * i3)) : by rewrite -left_distrib ... = r1 * (r2 * i3 + i2 * r3) + (i1 * (r2 * r3) + i1 * -(i2 * i3)) : by rewrite -neg_mul_eq_mul_neg ... = r1 * (r2 * i3 + i2 * r3) + i1 * (r2 * r3 - i2 * i3) : by rewrite -left_distrib) definition complex_semigroup [instance] {A : Type} [r : ring A] : semigroup (complex_ty A) := ⦃ semigroup, mul_assoc := complex_mul_assoc ⦄ theorem complex_one_mul {A : Type} [r : ring A] : ∀ (c : complex_ty A), complex_mul complex_one c = c := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (1 * r - 0 * i, 1 * i + 0 * r) = c, from prod.eq (calc pr₁ (1 * r - 0 * i, 1 * i + 0 * r) = 1 * r - 0 * i : by esimp ... = r : by rewrite [one_mul, zero_mul, sub_zero]) (calc pr₂ (1 * r - 0 * i, 1 * i + 0 * r) = 1 * i + 0 * r : by esimp ... = i : by rewrite [one_mul, zero_mul, add_zero]) theorem complex_mul_one {A : Type} [r : ring A] : ∀ (c : complex_ty A), complex_mul c complex_one = c := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (r * 1 - i * 0, r * 0 + i * 1) = c, from prod.eq (calc pr₁ (r * 1 - i * 0, r * 0 + i * 1) = r * 1 - i * 0 : by esimp ... = r : by rewrite [mul_one, mul_zero, sub_zero]) (calc pr₂ (r * 1 - i * 0, r * 0 + i * 1) = r * 0 + i * 1 : by esimp ... = i : by rewrite [mul_one, mul_zero, zero_add]) definition complex_monoid [instance] {A : Type} [r : ring A] : monoid (complex_ty A) := ⦃ monoid, mul_assoc := complex_mul_assoc, one_mul := complex_one_mul, mul_one := complex_mul_one ⦄ theorem complex_mul_comm {A : Type} [r : comm_ring A] : ∀ (c1 c2 : complex_ty A), complex_mul c1 c2 = complex_mul c2 c1 := take c1 : complex_ty A, take c2 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, i1 := pr2 c1, i2 := pr2 c2 in show (r1 * r2 - i1 * i2, r1 * i2 + i1 * r2) = (r2 * r1 - i2 * i1, r2 * i1 + i2 * r1), from pair_eq (calc r1 * r2 - i1 * i2 = r2 * r1 - i1 * i2 : by rewrite mul.comm ... = r2 * r1 - i2 * i1 : by rewrite {_ * i1}mul.comm) (calc r1 * i2 + i1 * r2 = i1 * r2 + r1 * i2 : by rewrite add.comm ... = r2 * i1 + r1 * i2 : by rewrite mul.comm ... = r2 * i1 + i2 * r1 : by rewrite {_ * r1}mul.comm) definition complex_comm_semigroup [instance] {A : Type} [r : comm_ring A] : comm_semigroup (complex_ty A) := ⦃ comm_semigroup, mul_assoc := complex_mul_assoc, mul_comm := complex_mul_comm ⦄ definition complex_comm_monoid [instance] {A : Type} [r : comm_ring A] : comm_monoid (complex_ty A) := ⦃ comm_monoid, mul_assoc := complex_mul_assoc, mul_comm := complex_mul_comm, one_mul := complex_one_mul, mul_one := complex_mul_one ⦄ theorem complex_left_distrib {A : Type} [cr : comm_ring A] : ∀ (c1 c2 c3 : complex_ty A), complex_mul c1 (complex_add c2 c3) = complex_add (complex_mul c1 c2) (complex_mul c1 c3) := take c1 : complex_ty A, take c2 : complex_ty A, take c3 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, r3 := pr1 c3, i1 := pr2 c1, i2 := pr2 c2, i3 := pr2 c3 in show (r1 * (r2 + r3) - i1 * (i2 + i3), r1 * (i2 + i3) + i1 * (r2 + r3)) = (r1 * r2 - i1 * i2 + (r1 * r3 - i1 * i3), r1 * i2 + i1 * r2 + (r1 * i3 + i1 * r3)), from pair_eq (calc r1 * (r2 + r3) - i1 * (i2 + i3) = r1 * r2 + r1 * r3 - (i1 * (i2 + i3)) : by rewrite left_distrib ... = r1 * r2 + r1 * r3 - (i1 * i2 + i1 * i3) : by rewrite left_distrib ... = r1 * r2 + r1 * r3 - i1 * i2 - i1 * i3 : by rewrite sub_add_eq_sub_sub ... = r1 * r2 + (r1 * r3 - i1 * i2) - i1 * i3 : by rewrite *add.assoc ... = r1 * r2 + (-(i1 * i2) + r1 * r3) - i1 * i3 : by rewrite -{_ - i1 * i2}neg_add_eq_sub ... = r1 * r2 - i1 * i2 + (r1 * r3 - i1 * i3) : by rewrite -*add.assoc) (calc r1 * (i2 + i3) + i1 * (r2 + r3) = r1 * i2 + r1 * i3 + (i1 * (r2 + r3)) : by rewrite left_distrib ... = r1 * i2 + r1 * i3 + (i1 * r2 + i1 * r3) : by rewrite left_distrib ... = r1 * i2 + (r1 * i3 + i1 * r2 + i1 * r3) : by rewrite *add.assoc ... = r1 * i2 + (i1 * r2 + r1 * i3 + i1 * r3) : by rewrite {i1 * r2 + _}add.comm ... = r1 * i2 + i1 * r2 + (r1 * i3 + i1 * r3) : by rewrite -*add.assoc) theorem complex_right_distrib {A : Type} [cr : comm_ring A] : ∀ (c1 c2 c3 : complex_ty A), complex_mul (complex_add c1 c2) c3 = complex_add (complex_mul c1 c3) (complex_mul c2 c3) := take c1 : complex_ty A, take c2 : complex_ty A, take c3 : complex_ty A, let r1 := pr1 c1, r2 := pr1 c2, r3 := pr1 c3, i1 := pr2 c1, i2 := pr2 c2, i3 := pr2 c3 in show ((r1 + r2) * r3 - (i1 + i2) * i3, (r1 + r2) * i3 + (i1 + i2) * r3) = (r1 * r3 - i1 * i3 + (r2 * r3 - i2 * i3), r1 * i3 + i1 * r3 + (r2 * i3 + i2 * r3)), from pair_eq (calc (r1 + r2) * r3 - (i1 + i2) * i3 = r1 * r3 + r2 * r3 - (i1 + i2) * i3 : by rewrite right_distrib ... = r1 * r3 + r2 * r3 - (i1 * i3 + i2 * i3) : by rewrite right_distrib ... = r1 * r3 + r2 * r3 - i1 * i3 - i2 * i3 : by rewrite sub_add_eq_sub_sub ... = r1 * r3 + (r2 * r3 - i1 * i3) - i2 * i3 : by rewrite *add.assoc ... = r1 * r3 + (-(i1 * i3) + r2 * r3) - i2 * i3 : by rewrite -{_ - i1 * i3}neg_add_eq_sub ... = r1 * r3 - i1 * i3 + (r2 * r3 - i2 * i3) : by rewrite -*add.assoc) (calc (r1 + r2) * i3 + (i1 + i2) * r3 = r1 * i3 + r2 * i3 + (i1 + i2) * r3 : by rewrite right_distrib ... = r1 * i3 + r2 * i3 + (i1 * r3 + i2 * r3) : by rewrite right_distrib ... = r1 * i3 + (r2 * i3 + i1 * r3 + i2 * r3) : by rewrite *add.assoc ... = r1 * i3 + (i1 * r3 + r2 * i3 + i2 * r3) : by rewrite {i1 * r3 + _}add.comm ... = r1 * i3 + i1 * r3 + (r2 * i3 + i2 * r3) : by rewrite -*add.assoc) definition complex_distrib [instance] {A : Type} [r : comm_ring A] : distrib (complex_ty A) := ⦃ distrib, left_distrib := complex_left_distrib, right_distrib := complex_right_distrib ⦄ theorem complex_zero_mul {A : Type} [r : ring A] : ∀ (c : complex_ty A), complex_mul complex_zero c = complex_zero := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (0 * r - 0 * i, 0 * i + 0 * r) = (0, 0), from prod.eq (calc pr₁ (0 * r - 0 * i, 0 * i + 0 * r) = 0 * r - 0 * i : by esimp ... = 0 : by rewrite [*zero_mul, sub_zero]) (calc pr₂ (0 * r - 0 * i, 0 * i + 0 * r) = 0 * i + 0 * r : by esimp ... = 0 : by rewrite [*zero_mul, add_zero]) theorem complex_mul_zero {A : Type} [r : ring A] : ∀ (c : complex_ty A), complex_mul c complex_zero = complex_zero := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (r * 0 - i * 0, r * 0 + i * 0) = (0, 0), from prod.eq (calc pr₁ (r * 0 - i * 0, r * 0 + i * 0) = r * 0 - i * 0 : by esimp ... = 0 : by rewrite [*mul_zero, sub_zero]) (calc pr₂ (r * 0 - i * 0, r * 0 + i * 0) = r * 0 + i * 0 : by esimp ... = 0 : by rewrite [*mul_zero, zero_add]) definition complex_mul_zero_class [instance] {A : Type} [r : ring A] : mul_zero_class (complex_ty A) := ⦃ mul_zero_class, zero_mul := complex_zero_mul, mul_zero := complex_mul_zero ⦄ /- theorem complex_mul_left_inv {A : Type} [f : field A] [g : group A] : ∀ (c : complex_ty A), complex_mul (complex_inv c) c = complex_one := take c : complex_ty A, let r : A := pr1 c, i : A := pr2 c in show (r * (r * r + i * i)⁻¹ * r - -(i * (r * r + i * i)⁻¹) * i, r * (r * r + i * i)⁻¹ * i + -(i * (r * r + i * i)⁻¹) * r) = (1, 0), from pair_eq (calc r * (r * r + i * i)⁻¹ * r - -(i * (r * r + i * i)⁻¹) * i = r * (r * r + i * i)⁻¹ * r + i * (r * r + i * i)⁻¹ * i : by rewrite [-neg_mul_eq_neg_mul, sub_neg_eq_add] ... = r * r * (r * r + i * i)⁻¹ + i * (r * r + i * i)⁻¹ * i : by rewrite [mul.comm, -mul.assoc] ... = r * r * (r * r + i * i)⁻¹ + i * i * (r * r + i * i)⁻¹ : by rewrite [{_ * _ * i}mul.comm, -mul.assoc] ... = (r * r + i * i) * (r * r + i * i)⁻¹ : by rewrite right_distrib ... = (r * r + i * i)⁻¹ * (r * r + i * i) : by rewrite mul.comm ... = 1 : by rewrite mul.left_inv) _ -/ end complex_ty end algebra
9a1d8088dc2bc3d3662d306807851965633a7b11
367134ba5a65885e863bdc4507601606690974c1
/src/logic/embedding.lean
250670b5e79f1869a98c70862f6625d715af6598
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,790
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.equiv.basic import data.sigma.basic import algebra.group.defs /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ @[nolint has_inhabited_instance] -- depending on cardinalities, an injective function may not exist structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ initialize_simps_projections embedding (to_fun → apply) end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ @[simps] protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ namespace function namespace embedding @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; simpa using funext h lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ @[simps] def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ @[simps] def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps apply] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps apply] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps apply] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ lemma swap_apply {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := f.injective.swap_apply x y z lemma swap_comp {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := f.injective.swap_comp x y end embedding end function namespace equiv @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps apply] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set -- TODO: these two definitions probably belong somewhere else, so that we can remove the -- `algebra.group.defs` import. /-- The embedding of a left cancellative semigroup into itself by left multiplication by a fixed element. -/ @[to_additive "The embedding of a left cancellative additive semigroup into itself by left translation by a fixed element.", simps] def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, g * h, inj' := mul_right_injective g } /-- The embedding of a right cancellative semigroup into itself by right multiplication by a fixed element. -/ @[to_additive "The embedding of a right cancellative additive semigroup into itself by right translation by a fixed element.", simps] def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, h * g, inj' := mul_left_injective g }
d28b29d37401699160099e2bf849837b5dc92081
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/topology/uniform_space/basic.lean
7b2371c620211b9ea492ade82e2f5f21ee4fb23f
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
69,125
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import order.filter.lift import topology.separation /-! # Uniform spaces Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * uniform continuity (in this file) * completeness (in `cauchy.lean`) * extension of uniform continuous functions to complete spaces (in `uniform_embedding.lean`) * totally bounded sets (in `cauchy.lean`) * totally bounded complete sets are compact (in `cauchy.lean`) A uniform structure on a type `X` is a filter `𝓤 X` on `X × X` satisfying some conditions which makes it reasonable to say that `∀ᶠ (p : X × X) in 𝓤 X, ...` means "for all p.1 and p.2 in X close enough, ...". Elements of this filter are called entourages of `X`. The two main examples are: * If `X` is a metric space, `V ∈ 𝓤 X ↔ ∃ ε > 0, { p | dist p.1 p.2 < ε } ⊆ V` * If `G` is an additive topological group, `V ∈ 𝓤 G ↔ ∃ U ∈ 𝓝 (0 : G), {p | p.2 - p.1 ∈ U} ⊆ V` Those examples are generalizations in two different directions of the elementary example where `X = ℝ` and `V ∈ 𝓤 ℝ ↔ ∃ ε > 0, { p | |p.2 - p.1| < ε } ⊆ V` which features both the topological group structure on `ℝ` and its metric space structure. Each uniform structure on `X` induces a topology on `X` characterized by > `nhds_eq_comap_uniformity : ∀ {x : X}, 𝓝 x = comap (prod.mk x) (𝓤 X)` where `prod.mk x : X → X × X := (λ y, (x, y))` is the partial evaluation of the product constructor. The dictionary with metric spaces includes: * an upper bound for `dist x y` translates into `(x, y) ∈ V` for some `V ∈ 𝓤 X` * a ball `ball x r` roughly corresponds to `uniform_space.ball x V := {y | (x, y) ∈ V}` for some `V ∈ 𝓤 X`, but the later is more general (it includes in particular both open and closed balls for suitable `V`). In particular we have: `is_open_iff_ball_subset {s : set X} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 X, ball x V ⊆ s` The triangle inequality is abstracted to a statement involving the composition of relations in `X`. First note that the triangle inequality in a metric space is equivalent to `∀ (x y z : X) (r r' : ℝ), dist x y ≤ r → dist y z ≤ r' → dist x z ≤ r + r'`. Then, for any `V` and `W` with type `set (X × X)`, the composition `V ○ W : set (X × X)` is defined as `{ p : X × X | ∃ z, (p.1, z) ∈ V ∧ (z, p.2) ∈ W }`. In the metric space case, if `V = { p | dist p.1 p.2 ≤ r }` and `W = { p | dist p.1 p.2 ≤ r' }` then the triangle inequality, as reformulated above, says `V ○ W` is contained in `{p | dist p.1 p.2 ≤ r + r'}` which is the entourage associated to the radius `r + r'`. In general we have `mem_ball_comp (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W)`. Note that this discussion does not depend on any axiom imposed on the uniformity filter, it is simply captured by the definition of composition. The uniform space axioms ask the filter `𝓤 X` to satisfy the following: * every `V ∈ 𝓤 X` contains the diagonal `id_rel = { p | p.1 = p.2 }`. This abstracts the fact that `dist x x ≤ r` for every non-negative radius `r` in the metric space case and also that `x - x` belongs to every neighborhood of zero in the topological group case. * `V ∈ 𝓤 X → prod.swap '' V ∈ 𝓤 X`. This is tightly related the fact that `dist x y = dist y x` in a metric space, and to continuity of negation in the topological group case. * `∀ V ∈ 𝓤 X, ∃ W ∈ 𝓤 X, W ○ W ⊆ V`. In the metric space case, it corresponds to cutting the radius of a ball in half and applying the triangle inequality. In the topological group case, it comes from continuity of addition at `(0, 0)`. These three axioms are stated more abstractly in the definition below, in terms of operations on filters, without directly manipulating entourages. ## Main definitions * `uniform_space X` is a uniform space structure on a type `X` * `uniform_continuous f` is a predicate saying a function `f : α → β` between uniform spaces is uniformly continuous : `∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r` In this file we also define a complete lattice structure on the type `uniform_space X` of uniform structures on `X`, as well as the pullback (`uniform_space.comap`) of uniform structures coming from the pullback of filters. Like distance functions, uniform structures cannot be pushed forward in general. ## Notations Localized in `uniformity`, we have the notation `𝓤 X` for the uniformity on a uniform space `X`, and `○` for composition of relations, seen as terms with type `set (X × X)`. ## Implementation notes There is already a theory of relations in `data/rel.lean` where the main definition is `def rel (α β : Type*) := α → β → Prop`. The relations used in the current file involve only one type, but this is not the reason why we don't reuse `data/rel.lean`. We use `set (α × α)` instead of `rel α α` because we really need sets to use the filter library, and elements of filters on `α × α` have type `set (α × α)`. The structure `uniform_space X` bundles a uniform structure on `X`, a topology on `X` and an assumption saying those are compatible. This may not seem mathematically reasonable at first, but is in fact an instance of the forgetful inheritance pattern. See Note [forgetful inheritance] below. ## References The formalization uses the books: * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] But it makes a more systematic use of the filter library. -/ open set filter classical open_locale classical topological_space filter set_option eqn_compiler.zeta true universes u /-! ### Relations, seen as `set (α × α)` -/ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {α : Type*} := {p : α × α | p.1 = p.2} @[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (λ a, by simp) /-- The composition of relations -/ def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} localized "infix ` ○ `:55 := comp_rel" in uniformity @[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)} {x y : α} : (x, y) ∈ r₁ ○ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ○ (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ @[mono] lemma comp_rel_mono {f g h k: set (α×α)} (h₁ : f ⊆ h) (h₂ : g ⊆ k) : f ○ g ⊆ h ○ k := λ ⟨x, y⟩ ⟨z, h, h'⟩, ⟨z, h₁ h, h₂ h'⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ s ○ t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : id_rel ○ r = r := set.ext $ assume ⟨a, b⟩, by simp lemma comp_rel_assoc {r s t : set (α×α)} : (r ○ s) ○ t = r ○ (s ○ t) := by ext p; cases p; simp only [mem_comp_rel]; tauto lemma subset_comp_self {α : Type*} {s : set (α × α)} (h : id_rel ⊆ s) : s ⊆ s ○ s := λ ⟨x, y⟩ xy_in, ⟨x, h (by rw mem_id_rel), xy_in⟩ /-- The relation is invariant under swapping factors. -/ def symmetric_rel (V : set (α × α)) : Prop := prod.swap ⁻¹' V = V /-- The maximal symmetric relation contained in a given relation. -/ def symmetrize_rel (V : set (α × α)) : set (α × α) := V ∩ prod.swap ⁻¹' V lemma symmetric_symmetrize_rel (V : set (α × α)) : symmetric_rel (symmetrize_rel V) := by simp [symmetric_rel, symmetrize_rel, preimage_inter, inter_comm, ← preimage_comp] lemma symmetrize_rel_subset_self (V : set (α × α)) : symmetrize_rel V ⊆ V := sep_subset _ _ @[mono] lemma symmetrize_mono {V W: set (α × α)} (h : V ⊆ W) : symmetrize_rel V ⊆ symmetrize_rel W := inter_subset_inter h $ preimage_mono h lemma symmetric_rel_inter {U V : set (α × α)} (hU : symmetric_rel U) (hV : symmetric_rel V) : symmetric_rel (U ∩ V) := begin unfold symmetric_rel at *, rw [preimage_inter, hU, hV], end /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : 𝓟 id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, s ○ s) ≤ uniformity) /-- An alternative constructor for `uniform_space.core`. This version unfolds various `filter`-related definitions. -/ def uniform_space.core.mk' {α : Type u} (U : filter (α × α)) (refl : ∀ (r ∈ U) x, (x, x) ∈ r) (symm : ∀ r ∈ U, prod.swap ⁻¹' r ∈ U) (comp : ∀ r ∈ U, ∃ t ∈ U, t ○ t ⊆ r) : uniform_space.core α := ⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*] section prio /-- Suppose that one can put two mathematical structures on a type, a rich one `R` and a poor one `P`, and that one can deduce the poor structure from the rich structure through a map `F` (called a forgetful functor) (think `R = metric_space` and `P = topological_space`). A possible implementation would be to have a type class `rich` containing a field `R`, a type class `poor` containing a field `P`, and an instance from `rich` to `poor`. However, this creates diamond problems, and a better approach is to let `rich` extend `poor` and have a field saying that `F R = P`. To illustrate this, consider the pair `metric_space` / `topological_space`. Consider the topology on a product of two metric spaces. With the first approach, it could be obtained by going first from each metric space to its topology, and then taking the product topology. But it could also be obtained by considering the product metric space (with its sup distance) and then the topology coming from this distance. These would be the same topology, but not definitionally, which means that from the point of view of Lean's kernel, there would be two different `topological_space` instances on the product. This is not compatible with the way instances are designed and used: there should be at most one instance of a kind on each type. This approach has created an instance diamond that does not commute definitionally. The second approach solves this issue. Now, a metric space contains both a distance, a topology, and a proof that the topology coincides with the one coming from the distance. When one defines the product of two metric spaces, one uses the sup distance and the product topology, and one has to give the proof that the sup distance induces the product topology. Following both sides of the instance diamond then gives rise (definitionally) to the product topology on the product space. Another approach would be to have the rich type class take the poor type class as an instance parameter. It would solve the diamond problem, but it would lead to a blow up of the number of type classes one would need to declare to work with complicated classes, say a real inner product space, and would create exponential complexity when working with products of such complicated spaces, that are avoided by bundling things carefully as above. Note that this description of this specific case of the product of metric spaces is oversimplified compared to mathlib, as there is an intermediate typeclass between `metric_space` and `topological_space` called `uniform_space`. The above scheme is used at both levels, embedding a topology in the uniform space structure, and a uniform structure in the metric space structure. Note also that, when `P` is a proposition, there is no such issue as any two proofs of `P` are definitionally equivalent in Lean. To avoid boilerplate, there are some designs that can automatically fill the poor fields when creating a rich structure if one doesn't want to do something special about them. For instance, in the definition of metric spaces, default tactics fill the uniform space fields if they are not given explicitly. One can also have a helper function creating the rich structure from a structure with less fields, where the helper function fills the remaining fields. See for instance `uniform_space.of_core` or `real_inner_product.of_core`. For more details on this question, called the forgetful inheritance pattern, see [Competing inheritance paths in dependent type theory: a case study in functional analysis](https://hal.inria.fr/hal-02463336). -/ library_note "forgetful inheritance" set_option default_priority 100 -- see Note [default priority] /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `α × α` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity)) end prio /-- Alternative constructor for `uniform_space α` when a topology is already given. -/ @[pattern] def uniform_space.mk' {α} (t : topological_space α) (c : uniform_space.core α) (is_open_uniformity : ∀s:set α, t.is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) : uniform_space α := ⟨c, is_open_uniformity⟩ /-- Construct a `uniform_space` from a `uniform_space.core`. -/ def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.rfl } /-- Construct a `uniform_space` from a `u : uniform_space.core` and a `topological_space` structure that is equal to `u.to_topological_space`. -/ def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.rfl } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] @[ext] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space α] /-- The uniformity is a filter on α × α (inferred from an ambient uniform space structure on α). -/ def uniformity (α : Type u) [uniform_space α] : filter (α × α) := (@uniform_space.to_core α _).uniformity localized "notation `𝓤` := uniformity" in uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : 𝓟 id_rel ≤ 𝓤 α := (@uniform_space.to_core α _).refl lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), s ○ s) ≤ 𝓤 α := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) := symm_le_uniformity lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, t ○ t ⊆ s := have s ∈ (𝓤 α).lift' (λt:set (α×α), t ○ t), from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is transitive. -/ lemma filter.tendsto.uniformity_trans {l : filter β} {f₁ f₂ f₃ : β → α} (h₁₂ : tendsto (λ x, (f₁ x, f₂ x)) l (𝓤 α)) (h₂₃ : tendsto (λ x, (f₂ x, f₃ x)) l (𝓤 α)) : tendsto (λ x, (f₁ x, f₃ x)) l (𝓤 α) := begin refine le_trans (le_lift' $ λ s hs, mem_map.2 _) comp_le_uniformity, filter_upwards [h₁₂ hs, h₂₃ hs], exact λ x hx₁₂ hx₂₃, ⟨_, hx₁₂, hx₂₃⟩ end /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is symmetric -/ lemma filter.tendsto.uniformity_symm {l : filter β} {f : β → α × α} (h : tendsto f l (𝓤 α)) : tendsto (λ x, ((f x).2, (f x).1)) l (𝓤 α) := tendsto_swap_uniformity.comp h /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is reflexive. -/ lemma tendsto_diag_uniformity (f : β → α) (l : filter β) : tendsto (λ x, (f x, f x)) l (𝓤 α) := assume s hs, mem_map.2 $ univ_mem_sets' $ λ x, refl_mem_uniformity hs lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) := tendsto_diag_uniformity (λ _, a) f lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ t ○ t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α := by rw [map_swap_eq_comap_swap]; from map_le_iff_le_comap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α := le_antisymm uniformity_le_symm symm_le_uniformity lemma symmetrize_mem_uniformity {V : set (α × α)} (h : V ∈ 𝓤 α) : symmetrize_rel V ∈ 𝓤 α := begin apply (𝓤 α).inter_sets h, rw [← image_swap_eq_preimage_swap, uniformity_eq_symm], exact image_mem_map h, end theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f := calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g : lift_mono uniformity_le_symm (le_refl _) ... ≤ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) : (𝓤 α).lift (λs, f (s ○ s)) ≤ (𝓤 α).lift f := calc (𝓤 α).lift (λs, f (s ○ s)) = ((𝓤 α).lift' (λs:set (α×α), s ○ s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : (𝓤 α).lift' (λs:set (α×α), s ○ (s ○ s)) ≤ (𝓤 α) := calc (𝓤 α).lift' (λd, d ○ (d ○ d)) = (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ (t ○ t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (𝓟 ∘ (○) s) $ monotone_principal.comp (monotone_comp_rel monotone_const monotone_id) ... = (𝓤 α).lift' (λs:set(α×α), s ○ s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ (𝓤 α) : comp_le_uniformity lemma comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ⊆ s := begin obtain ⟨w, w_in, w_sub⟩ : ∃ w ∈ 𝓤 α, w ○ w ⊆ s := comp_mem_uniformity_sets hs, use [symmetrize_rel w, symmetrize_mem_uniformity w_in, symmetric_symmetrize_rel w], have : symmetrize_rel w ⊆ w := symmetrize_rel_subset_self w, calc symmetrize_rel w ○ symmetrize_rel w ⊆ w ○ w : by mono ... ⊆ s : w_sub, end lemma subset_comp_self_of_mem_uniformity {s : set (α × α)} (h : s ∈ 𝓤 α) : s ⊆ s ○ s := subset_comp_self (refl_le_uniformity h) lemma comp_comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ○ t ⊆ s := begin rcases comp_symm_mem_uniformity_sets hs with ⟨w, w_in, w_symm, w_sub⟩, rcases comp_symm_mem_uniformity_sets w_in with ⟨t, t_in, t_symm, t_sub⟩, use [t, t_in, t_symm], have : t ⊆ t ○ t := subset_comp_self_of_mem_uniformity t_in, calc t ○ t ○ t ⊆ w ○ t : by mono ... ⊆ w ○ (t ○ t) : by mono ... ⊆ w ○ w : by mono ... ⊆ s : w_sub, end /-! ### Balls in uniform spaces -/ /-- The ball around `(x : β)` with respect to `(V : set (β × β))`. Intended to be used for `V ∈ 𝓤 β`, but this is not needed for the definition. Recovers the notions of metric space ball when `V = {p | dist p.1 p.2 < r }`. -/ def uniform_space.ball (x : β) (V : set (β × β)) : set β := (prod.mk x) ⁻¹' V open uniform_space (ball) /-- The triangle inequality for `uniform_space.ball` -/ lemma mem_ball_comp {V W : set (β × β)} {x y z} (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W) := prod_mk_mem_comp_rel h h' lemma ball_subset_of_comp_subset {V W : set (β × β)} {x y} (h : x ∈ ball y W) (h' : W ○ W ⊆ V) : ball x W ⊆ ball y V := λ z z_in, h' (mem_ball_comp h z_in) lemma ball_mono {V W : set (β × β)} (h : V ⊆ W) (x : β) : ball x V ⊆ ball x W := by tauto lemma mem_ball_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x y} : x ∈ ball y V ↔ y ∈ ball x V := show (x, y) ∈ prod.swap ⁻¹' V ↔ (x, y) ∈ V, by { unfold symmetric_rel at hV, rw hV } lemma ball_eq_of_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x} : ball x V = {y | (y, x) ∈ V} := by { ext y, rw mem_ball_symmetry hV, exact iff.rfl } lemma mem_comp_of_mem_ball {V W : set (β × β)} {x y z : β} (hV : symmetric_rel V) (hx : x ∈ ball z V) (hy : y ∈ ball z W) : (x, y) ∈ V ○ W := begin rw mem_ball_symmetry hV at hx, exact ⟨z, hx, hy⟩ end lemma mem_comp_comp {V W M : set (β × β)} (hW' : symmetric_rel W) {p : β × β} : p ∈ V ○ M ○ W ↔ ((ball p.1 V).prod (ball p.2 W) ∩ M).nonempty := begin cases p with x y, split, { rintros ⟨z, ⟨w, hpw, hwz⟩, hzy⟩, exact ⟨(w, z), ⟨hpw, by rwa mem_ball_symmetry hW'⟩, hwz⟩, }, { rintro ⟨⟨w, z⟩, ⟨w_in, z_in⟩, hwz⟩, rwa mem_ball_symmetry hW' at z_in, use [z, w] ; tauto }, end /-! ### Neighborhoods in uniform spaces -/ lemma mem_nhds_uniformity_iff_right {x : α} {s : set α} : s ∈ 𝓝 x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α := ⟨ begin simp only [mem_nhds_sets_iff, is_open_uniformity, and_imp, exists_imp_distrib], exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α}, assume x' hx', refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'), by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b), have hp : (x', b) ∈ t, from hax' ▸ hp', have (b, b') ∈ t, from hab ▸ hp'', have (x', b') ∈ t ○ t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma mem_nhds_uniformity_iff_left {x : α} {s : set α} : s ∈ 𝓝 x ↔ {p : α × α | p.2 = x → p.1 ∈ s} ∈ 𝓤 α := by { rw [uniformity_eq_symm, mem_nhds_uniformity_iff_right], refl } lemma nhds_eq_comap_uniformity_aux {α : Type u} {x : α} {s : set α} {F : filter (α × α)} : {p : α × α | p.fst = x → p.snd ∈ s} ∈ F ↔ s ∈ comap (prod.mk x) F := by rw mem_comap_sets ; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, F.sets_of_superset h $ assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_comap_uniformity {x : α} : 𝓝 x = (𝓤 α).comap (prod.mk x) := by { ext s, rw [mem_nhds_uniformity_iff_right], exact nhds_eq_comap_uniformity_aux } lemma is_open_iff_ball_subset {s : set α} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 α, ball x V ⊆ s := begin simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity], exact iff.rfl, end lemma nhds_basis_uniformity' {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} : (𝓝 x).has_basis p (λ i, {y | (x, y) ∈ s i}) := by { rw [nhds_eq_comap_uniformity], exact h.comap (prod.mk x) } lemma nhds_basis_uniformity {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} : (𝓝 x).has_basis p (λ i, {y | (y, x) ∈ s i}) := begin replace h := h.comap prod.swap, rw [← map_swap_eq_comap_swap, ← uniformity_eq_symm] at h, exact nhds_basis_uniformity' h end lemma uniform_space.mem_nhds_iff {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, ball x V ⊆ s := begin rw [nhds_eq_comap_uniformity, mem_comap_sets], exact iff.rfl, end lemma uniform_space.ball_mem_nhds (x : α) ⦃V : set (α × α)⦄ (V_in : V ∈ 𝓤 α) : ball x V ∈ 𝓝 x := begin rw uniform_space.mem_nhds_iff, exact ⟨V, V_in, subset.refl _⟩ end lemma uniform_space.mem_nhds_iff_symm {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, symmetric_rel V ∧ ball x V ⊆ s := begin rw uniform_space.mem_nhds_iff, split, { rintros ⟨V, V_in, V_sub⟩, use [symmetrize_rel V, symmetrize_mem_uniformity V_in, symmetric_symmetrize_rel V], exact subset.trans (ball_mono (symmetrize_rel_subset_self V) x) V_sub }, { rintros ⟨V, V_in, V_symm, V_sub⟩, exact ⟨V, V_in, V_sub⟩ } end lemma uniform_space.has_basis_nhds (x : α) : has_basis (𝓝 x) (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) (λ s, ball x s) := ⟨λ t, by simp [uniform_space.mem_nhds_iff_symm, and_assoc]⟩ open uniform_space lemma uniform_space.has_basis_nhds_prod (x y : α) : has_basis (𝓝 (x, y)) (λ s, s ∈ 𝓤 α ∧ symmetric_rel s) $ λ s, (ball x s).prod (ball y s) := begin rw nhds_prod_eq, apply (has_basis_nhds x).prod' (has_basis_nhds y), rintro U V ⟨U_in, U_symm⟩ ⟨V_in, V_symm⟩, exact ⟨U ∩ V, ⟨(𝓤 α).inter_sets U_in V_in, symmetric_rel_inter U_symm V_symm⟩, ball_mono (inter_subset_left U V) x, ball_mono (inter_subset_right U V) y⟩, end lemma nhds_eq_uniformity {x : α} : 𝓝 x = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) := (nhds_basis_uniformity' (𝓤 α).basis_sets).eq_binfi lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {y : α | (x, y) ∈ s} ∈ 𝓝 x := (nhds_basis_uniformity' (𝓤 α).basis_sets).mem_of_mem h lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {x : α | (x, y) ∈ s} ∈ 𝓝 y := mem_nhds_left _ (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (𝓝 a) (𝓤 α) := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (𝓝 a) (𝓤 α) := assume s, mem_nhds_left a lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage ) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ hg.comp monotone_preimage ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : filter.prod (𝓝 a) (𝓝 b) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) := begin rw [prod_def], show (𝓝 a).lift (λs:set α, (𝓝 b).lift (λt:set α, 𝓟 (set.prod s t))) = _, rw [lift_nhds_right], apply congr_arg, funext s, rw [lift_nhds_left], refl, exact monotone_principal.comp (monotone_prod monotone_const monotone_id), exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : α} : 𝓝 (a, b) = (𝓤 α).lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ 𝓝 (x, y), begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end /-- Entourages are neighborhoods of the diagonal. -/ lemma nhds_le_uniformity : (⨆ x : α, 𝓝 (x, x)) ≤ 𝓤 α := begin apply supr_le _, intros x V V_in, rcases comp_symm_mem_uniformity_sets V_in with ⟨w, w_in, w_symm, w_sub⟩, have : (ball x w).prod (ball x w) ∈ 𝓝 (x, x), { rw nhds_prod_eq, exact prod_mem_prod (ball_mem_nhds x w_in) (ball_mem_nhds x w_in) }, apply mem_sets_of_superset this, rintros ⟨u, v⟩ ⟨u_in, v_in⟩, exact w_sub (mem_comp_of_mem_ball w_symm u_in v_in) end /-! ### Closure and interior in uniform spaces -/ lemma closure_eq_uniformity (s : set $ α × α) : closure s = ⋂ V ∈ {V | V ∈ 𝓤 α ∧ symmetric_rel V}, V ○ s ○ V := begin ext ⟨x, y⟩, simp_rw [mem_closure_iff_nhds_basis (uniform_space.has_basis_nhds_prod x y), mem_Inter, mem_set_of_eq], apply forall_congr, intro V, apply forall_congr, rintros ⟨V_in, V_symm⟩, simp_rw [mem_comp_comp V_symm, inter_comm, exists_prop], exact iff.rfl, end lemma uniformity_has_basis_closed : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_closed V) id := begin rw filter.has_basis_self, intro t, split, { intro h, rcases comp_comp_symm_mem_uniformity_sets h with ⟨w, w_in, w_symm, r⟩, refine ⟨closure w, _, is_closed_closure, _⟩, apply mem_sets_of_superset w_in subset_closure, refine subset.trans _ r, rw closure_eq_uniformity, apply Inter_subset_of_subset, apply Inter_subset, exact ⟨w_in, w_symm⟩ }, { rintros ⟨r, r_in, r_closed, r_sub⟩, exact mem_sets_of_superset r_in r_sub, } end /-- Closed entourages form a basis of the uniformity filter. -/ lemma uniformity_has_basis_closure : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α) closure := ⟨begin intro t, rw uniformity_has_basis_closed.mem_iff, split, { rintros ⟨r, ⟨r_in, r_closed⟩, r_sub⟩, use [r, r_in], convert r_sub, rw r_closed.closure_eq, refl }, { rintros ⟨r, r_in, r_sub⟩, exact ⟨closure r, ⟨mem_sets_of_superset r_in subset_closure, is_closed_closure⟩, r_sub⟩ } end⟩ lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d ∈ 𝓤 α, d ○ (t ○ d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (𝓝 (a, b) ⊓ 𝓟 t ≠ ⊥) : by simp [closure_eq_cluster_pts, cluster_pt] ... ↔ (((@prod.swap α α) <$> 𝓤 α).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap α α) (𝓤 α)).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) : by refl ... ↔ ((𝓤 α).lift' (λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ 𝓟 t ≠ ⊥) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (∀s ∈ 𝓤 α, (set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t).nonempty) : begin rw [lift'_inf_principal_eq, lift'_ne_bot_iff], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (∀ s ∈ 𝓤 α, (a, b) ∈ s ○ (t ○ s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure) (calc (𝓤 α).lift' closure ≤ (𝓤 α).lift' (λd, d ○ (d ○ d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≤ (𝓤 α) : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ 𝓤 α, by filter_upwards [hs] this, by simp [this]) (assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : interior s ∈ 𝓤 α := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) : ∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s := have s ∈ (𝓤 α).lift' closure, by rwa [uniformity_eq_uniformity_closure] at h, have ∃ t ∈ 𝓤 α, closure t ⊆ s, by rwa [mem_lift'_sets] at this; apply closure_mono, let ⟨t, ht, hst⟩ := this in ⟨closure t, (𝓤 α).sets_of_superset ht subset_closure, is_closed_closure, hst⟩ /-! ### Uniformity bases -/ lemma filter.has_basis.mem_uniformity_iff {p : β → Prop} {s : β → set (α×α)} (h : (𝓤 α).has_basis p s) {t : set (α × α)} : t ∈ 𝓤 α ↔ ∃ i (hi : p i), ∀ a b, (a, b) ∈ s i → (a, b) ∈ t := h.mem_iff.trans $ by simp only [prod.forall, subset_def] /-- Symmetric entourages form a basis of `𝓤 α` -/ lemma uniform_space.has_basis_symmetric : (𝓤 α).has_basis (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) id := ⟨λ t, ⟨λ t_in, ⟨symmetrize_rel t, ⟨⟨symmetrize_mem_uniformity t_in, symmetric_symmetrize_rel t⟩, symmetrize_rel_subset_self _⟩⟩, λ ⟨s, ⟨s_in, h⟩, hst⟩, mem_sets_of_superset s_in hst⟩⟩ lemma uniform_space.has_seq_basis (h : is_countably_generated $ 𝓤 α) : ∃ V : ℕ → set (α × α), has_antimono_basis (𝓤 α) (λ _, true) V ∧ ∀ n, symmetric_rel (V n) := begin rcases h.has_antimono_basis with ⟨U, hbasis, hdec, monotrue⟩, clear monotrue, simp only [forall_prop_of_true] at hdec, use λ n, symmetrize_rel (U n), refine ⟨⟨⟨_⟩, by intros ; mono, by tauto⟩, λ n, symmetric_symmetrize_rel _⟩, intros t, rw hbasis.mem_iff, split, { rintro ⟨i, _, hi⟩, exact ⟨i, trivial, subset.trans (inter_subset_left _ _) hi⟩ }, { rintro ⟨i, _, hi⟩, rcases hbasis.mem_iff.mp (symmetrize_mem_uniformity $ hbasis.mem_of_mem trivial) with ⟨j, _, hj⟩, use j, tauto } end /-! ### Uniform continuity -/ /-- A function `f : α → β` is *uniformly continuous* if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `α`. -/ def uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β) /-- A function `f : α → β` is *uniformly continuous* on `s : set α` if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal while remaining in `s.prod s`. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `s`.-/ def uniform_continuous_on [uniform_space β] (f : α → β) (s : set α) : Prop := tendsto (λ x : α × α, (f x.1, f x.2)) (𝓤 α ⊓ principal (s.prod s)) (𝓤 β) theorem uniform_continuous_def [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, { x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α := iff.rfl theorem uniform_continuous_iff_eventually [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r := iff.rfl lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) : uniform_continuous c := have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b, le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity lemma uniform_continuous_id : uniform_continuous (@id α) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := uniform_continuous_of_const $ λ _ _, rfl lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) := hg.comp hf lemma filter.has_basis.uniform_continuous_iff [uniform_space β] {p : γ → Prop} {s : γ → set (α×α)} (ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)} (hb : (𝓤 β).has_basis q t) {f : α → β} : uniform_continuous f ↔ ∀ i (hi : q i), ∃ j (hj : p j), ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ t i := (ha.tendsto_iff hb).trans $ by simp only [prod.forall] end uniform_space open_locale uniformity section constructions instance : partial_order (uniform_space α) := { le := λt s, t.uniformity ≤ s.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ } instance : has_Inf (uniform_space α) := ⟨assume s, uniform_space.of_core { uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : Inf tt ≤ t := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_top (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_bot (uniform_space α) := ⟨{ to_topological_space := ⊥, uniformity := 𝓟 id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩ instance : complete_lattice (uniform_space α) := { sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h), le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h), sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩, inf := λ a b, Inf {a, b}, le_inf := λ a b c h₁ h₂, le_Inf (λ u h, by { cases h, exact h.symm ▸ h₁, exact (mem_singleton_iff.1 h).symm ▸ h₂ }), inf_le_left := λ a b, Inf_le (by simp), inf_le_right := λ a b, Inf_le (by simp), top := ⊤, le_top := λ a, show a.uniformity ≤ ⊤, from le_top, bot := ⊥, bot_le := λ u, u.refl, Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t}, le_Sup := λ s u h, le_Inf (λ u' h', h' u h), Sup_le := λ s u h, Inf_le h, Inf := Inf, le_Inf := λ s a hs, le_Inf hs, Inf_le := λ s a ha, Inf_le ha, ..uniform_space.partial_order } lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} : (infi u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _) lemma inf_uniformity {u v : uniform_space α} : (u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity := have (u ⊓ v) = (⨅i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq], calc (u ⊓ v).uniformity = ((⨅i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this] ... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩ instance inhabited_uniform_space_core : inhabited (uniform_space.core α) := ⟨@uniform_space.to_core _ (default _)⟩ /-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `α × α → β × β`. -/ def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl), symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap, comp := le_trans begin rw [comap_lift'_eq, comap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), repeat { exact monotone_comp_rel monotone_id monotone_id } end (comap_mono u.comp), is_open_uniformity := λ s, begin change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff_right, filter.comap, and_comm], refine ball_congr (λ x hx, ⟨_, _⟩), { rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩, rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) }, { rintro ⟨t, ht, hts⟩, exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl, mem_nhds_uniformity_iff_right.1 $ mem_nhds_left _ ht⟩ } end } lemma uniformity_comap [uniform_space α] [uniform_space β] {f : α → β} (h : ‹uniform_space α› = uniform_space.comap f ‹uniform_space β›) : 𝓤 α = comap (prod.map f f) (𝓤 β) := by { rw h, refl } lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id := by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id] lemma uniform_space.comap_comap {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} : uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) := by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap lemma uniform_continuous_iff {α β} [uα : uniform_space α] [uβ : uniform_space β] {f : α → β} : uniform_continuous f ↔ uα ≤ uβ.comap f := filter.map_le_iff_le_comap lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.comap f u) u f := tendsto_comap theorem to_topological_space_comap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.comap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := rfl lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g := tendsto_comap_iff.2 h lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _) lemma uniform_continuous.continuous [uniform_space α] [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := top_unique $ assume s hs, s.eq_empty_or_nonempty.elim (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤) (assume ⟨x, hx⟩, have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl, this.symm ▸ @is_open_univ _ ⊤) lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} : (infi u).to_topological_space = ⨅i, (u i).to_topological_space := classical.by_cases (assume h : nonempty ι, eq_of_nhds_eq_nhds $ assume a, begin rw [nhds_infi, nhds_eq_uniformity], change (infi u).uniformity.lift' (preimage $ prod.mk a) = _, begin rw [infi_uniformity, lift'_infi], exact (congr_arg _ $ funext $ assume i, (@nhds_eq_uniformity α (u i) a).symm), exact h, exact assume a b, rfl end end) (assume : ¬ nonempty ι, le_antisymm (le_infi $ assume i, to_topological_space_mono $ infi_le _ _) (have infi u = ⊤, from top_unique $ le_infi $ assume i, (this ⟨i⟩).elim, have @uniform_space.to_topological_space _ (infi u) = ⊤, from this.symm ▸ to_topological_space_top, this.symm ▸ le_top)) lemma to_topological_space_Inf {s : set (uniform_space α)} : (Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) := begin rw [Inf_eq_infi, to_topological_space_infi], apply congr rfl, funext x, exact to_topological_space_infi end lemma to_topological_space_inf {u v : uniform_space α} : (u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space := by rw [to_topological_space_Inf, infi_pair] instance : uniform_space empty := ⊥ instance : uniform_space unit := ⊥ instance : uniform_space bool := ⊥ instance : uniform_space ℕ := ⊥ instance : uniform_space ℤ := ⊥ instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.comap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : 𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_comap lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_comap' hf lemma uniform_continuous_on_iff_restrict [uniform_space α] [uniform_space β] (f : α → β) (s : set α) : uniform_continuous_on f s ↔ uniform_continuous (s.restrict f) := begin unfold uniform_continuous_on set.restrict uniform_continuous tendsto, rw [show (λ x : s × s, (f x.1, f x.2)) = prod.map f f ∘ coe, by ext x; cases x; refl, uniformity_comap rfl, show prod.map subtype.val subtype.val = (coe : s × s → α × α), by ext x; cases x; refl], conv in (map _ (comap _ _)) { rw ← filter.map_map }, rw subtype_coe_map_comap_prod, refl, end lemma tendsto_of_uniform_continuous_subtype [uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α} (hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ 𝓝 a) : tendsto f (𝓝 a) (𝓝 (f a)) := by rw [(@map_nhds_subtype_coe_eq α _ s a (mem_of_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _) section prod /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := uniform_space.of_core_eq (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_topological_space : by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = (𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓ (𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) := inf_uniformity lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] : 𝓤 (α×β) = map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (filter.prod (𝓤 α) (𝓤 β)) := have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) = comap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))), from funext $ assume f, map_eq_comap_of_inverse (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl), by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap, comap_comap] lemma mem_map_sets_iff' {α : Type*} {β : Type*} {f : filter α} {m : α → β} {t : set β} : t ∈ (map m f).sets ↔ (∃s∈f, m '' s ⊆ t) := mem_map_sets_iff lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space α] {s:set (α×α)} {f : α → α → α} (hf : uniform_continuous (λp:α×α, f p.1 p.2)) (hs : s ∈ 𝓤 α) : ∃u∈𝓤 α, ∀a b c, (a, b) ∈ u → (f a c, f b c) ∈ s := begin rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf, rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf, rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht, refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩, exact hab, exact refl_mem_uniformity hv, refl end lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) := le_trans (map_mono (@inf_le_left (uniform_space (α×β)) _ _ _)) map_comap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) := le_trans (map_mono (@inf_le_right (uniform_space (α×β)) _ _ _)) map_comap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd variables [uniform_space α] [uniform_space β] [uniform_space γ] lemma uniform_continuous.prod_mk {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) : uniform_continuous (λ a, f (a,b)) := h.comp (uniform_continuous_id.prod_mk uniform_continuous_const) lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) : uniform_continuous (λ b, f (a,b)) := h.comp (uniform_continuous_const.prod_mk uniform_continuous_id) lemma uniform_continuous.prod_map [uniform_space δ] {f : α → γ} {g : β → δ} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (prod.map f g) := (hf.comp uniform_continuous_fst).prod_mk (hg.comp uniform_continuous_snd) lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl end prod section open uniform_space function variables {δ' : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] [uniform_space δ'] local notation f `∘₂` g := function.bicompr f g /-- Uniform continuity for functions of two variables. -/ def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry f) lemma uniform_continuous₂_def (f : α → β → γ) : uniform_continuous₂ f ↔ uniform_continuous (uncurry f) := iff.rfl lemma uniform_continuous₂.uniform_continuous {f : α → β → γ} (h : uniform_continuous₂ f) : uniform_continuous (uncurry f) := h lemma uniform_continuous₂_curry (f : α × β → γ) : uniform_continuous₂ (function.curry f) ↔ uniform_continuous f := by rw [uniform_continuous₂, uncurry_curry] lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ} (hg : uniform_continuous g) (hf : uniform_continuous₂ f) : uniform_continuous₂ (g ∘₂ f) := hg.comp hf lemma uniform_continuous₂.bicompl {f : α → β → γ} {ga : δ → α} {gb : δ' → β} (hf : uniform_continuous₂ f) (hga : uniform_continuous ga) (hgb : uniform_continuous gb) : uniform_continuous₂ (bicompl f ga gb) := hf.uniform_continuous.comp (hga.prod_map hgb) end lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl section sum variables [uniform_space α] [uniform_space β] open sum /-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained by taking independently an entourage of the diagonal in the first part, and an entourage of the diagonal in the second part. -/ def uniform_space.core.sum : uniform_space.core (α ⊕ β) := uniform_space.core.mk' (map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β)) (λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂]) (λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩) (λ r ⟨Hrα, Hrβ⟩, begin rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩, rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩, refine ⟨_, ⟨mem_map_sets_iff.2 ⟨tα, htα, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩, rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩, ⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩, { have A : (a, c) ∈ tα ○ tα := ⟨b, hab, hbc⟩, exact Htα A }, { have A : (a, c) ∈ tβ ○ tβ := ⟨b, hab, hbc⟩, exact Htβ A } end) /-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage of the diagonal. -/ lemma union_mem_uniformity_sum {a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) : ((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈ (@uniform_space.core.sum α β _ _).uniformity := ⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩ /- To prove that the topology defined by the uniform structure on the disjoint union coincides with the disjoint union topology, we need two lemmas saying that open sets can be characterized by the uniform structure -/ lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) : { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity := begin cases x, { refine mem_sets_of_superset (union_mem_uniformity_sum (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.1 xs)) univ_mem_sets) (union_subset _ _); rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, { refine mem_sets_of_superset (union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff_right.1 (mem_nhds_sets hs.2 xs))) (union_subset _ _); rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, end lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)} (hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity) : is_open s := begin split, { refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }, { refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl } end /- We can now define the uniform structure on the disjoint union -/ instance sum.uniform_space : uniform_space (α ⊕ β) := { to_core := uniform_space.core.sum, is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ } lemma sum.uniformity : 𝓤 (α ⊕ β) = map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl end sum end constructions -- For a version of the Lebesgue number lemma assuming only a sequentially compact space, -- see topology/sequences.lean lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α} (hs : is_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i := begin let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ m ○ n} ⊆ c i}, have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n), { refine λ n hn, is_open_uniformity.2 _, rintro x ⟨i, m, hm, h⟩, rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩, apply (𝓤 α).sets_of_superset hm', rintros ⟨x, y⟩ hp rfl, refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩, dsimp at hz ⊢, rw comp_rel_assoc, exact ⟨y, hp, hz⟩ }, have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n, { intros x hx, rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩, rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩, exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ }, rcases hs.elim_finite_subcover_image hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩, refine ⟨_, Inter_mem_sets b_fin bu, λ x hx, _⟩, rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩, refine ⟨i, λ y hy, h _⟩, exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy) end lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)} (hs : is_compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma hs (by simpa) hc₂ /-! ### Expressing continuity properties in uniform spaces We reformulate the various continuity properties of functions taking values in a uniform space in terms of the uniformity in the target. Since the same lemmas (essentially with the same names) also exist for metric spaces and emetric spaces (reformulating things in terms of the distance or the edistance in the target), we put them in a namespace `uniform` here. In the metric and emetric space setting, there are also similar lemmas where one assumes that both the source and the target are metric spaces, reformulating things in terms of the distance on both sides. These lemmas are generally written without primes, and the versions where only the target is a metric space is primed. We follow the same convention here, thus giving lemmas with primes. -/ namespace uniform variables [uniform_space α] theorem tendsto_nhds_right {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ tendsto (λ x, (a, u x)) f (𝓤 α) := ⟨λ H, tendsto_left_nhds_uniformity.comp H, λ H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_right.1 hs)⟩ theorem tendsto_nhds_left {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ tendsto (λ x, (u x, a)) f (𝓤 α) := ⟨λ H, tendsto_right_nhds_uniformity.comp H, λ H s hs, by simpa [mem_of_nhds hs] using H (mem_nhds_uniformity_iff_left.1 hs)⟩ theorem continuous_at_iff'_right [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) := by rw [continuous_at, tendsto_nhds_right] theorem continuous_at_iff'_left [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) := by rw [continuous_at, tendsto_nhds_left] theorem continuous_within_at_iff'_right [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔ tendsto (λ x, (f b, f x)) (nhds_within b s) (𝓤 α) := by rw [continuous_within_at, tendsto_nhds_right] theorem continuous_within_at_iff'_left [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔ tendsto (λ x, (f x, f b)) (nhds_within b s) (𝓤 α) := by rw [continuous_within_at, tendsto_nhds_left] theorem continuous_on_iff'_right [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f b, f x)) (nhds_within b s) (𝓤 α) := by simp [continuous_on, continuous_within_at_iff'_right] theorem continuous_on_iff'_left [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f x, f b)) (nhds_within b s) (𝓤 α) := by simp [continuous_on, continuous_within_at_iff'_left] theorem continuous_iff'_right [topological_space β] {f : β → α} : continuous f ↔ ∀ b, tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_right theorem continuous_iff'_left [topological_space β] {f : β → α} : continuous f ↔ ∀ b, tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_left end uniform lemma filter.tendsto.congr_uniformity {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β} (hf : tendsto f l (𝓝 b)) (hg : tendsto (λ x, (f x, g x)) l (𝓤 β)) : tendsto g l (𝓝 b) := uniform.tendsto_nhds_right.2 $ (uniform.tendsto_nhds_right.1 hf).uniformity_trans hg lemma uniform.tendsto_congr {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β} (hfg : tendsto (λ x, (f x, g x)) l (𝓤 β)) : tendsto f l (𝓝 b) ↔ tendsto g l (𝓝 b) := ⟨λ h, h.congr_uniformity hfg, λ h, h.congr_uniformity hfg.uniformity_symm⟩
b442c3cd2d8850a3ea610f84a6dd3fc586ca12c6
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/real/hyperreal.lean
160d4155dbda5b268fdf5a813eaa0fe78a2e0504
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
37,164
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir -/ import order.filter.filter_product import analysis.specific_limits /-! # Construction of the hyperreal numbers as an ultraproduct of real sequences. -/ open filter filter.germ open_locale topological_space classical /-- Hyperreal numbers on the ultrafilter extending the cofinite filter -/ def hyperreal : Type := germ (@hyperfilter ℕ) ℝ namespace hyperreal notation `ℝ*` := hyperreal private def U : is_ultrafilter (@hyperfilter ℕ) := is_ultrafilter_hyperfilter noncomputable instance : discrete_linear_ordered_field ℝ* := germ.discrete_linear_ordered_field U noncomputable instance : inhabited ℝ* := ⟨0⟩ noncomputable instance : has_coe_t ℝ ℝ* := ⟨λ x, (↑x : germ _ _)⟩ @[simp, norm_cast] lemma coe_eq_coe {x y : ℝ} : (x : ℝ*) = y ↔ x = y := germ.const_inj @[simp, norm_cast] lemma coe_eq_zero {x : ℝ} : (x : ℝ*) = 0 ↔ x = 0 := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one {x : ℝ} : (x : ℝ*) = 1 ↔ x = 1 := coe_eq_coe @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ) = (1 : ℝ*) := rfl @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ) = (0 : ℝ*) := rfl @[simp, norm_cast] lemma coe_inv (x : ℝ) : ↑(x⁻¹) = (x⁻¹ : ℝ*) := rfl @[simp, norm_cast] lemma coe_neg (x : ℝ) : ↑(-x) = (-x : ℝ*) := rfl @[simp, norm_cast] lemma coe_add (x y : ℝ) : ↑(x + y) = (x + y : ℝ*) := rfl @[simp, norm_cast] lemma coe_bit0 (x : ℝ) : ↑(bit0 x) = (bit0 x : ℝ*) := rfl @[simp, norm_cast] lemma coe_bit1 (x : ℝ) : ↑(bit1 x) = (bit1 x : ℝ*) := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ) : ↑(x * y) = (x * y : ℝ*) := rfl @[simp, norm_cast] lemma coe_div (x y : ℝ) : ↑(x / y) = (x / y : ℝ*) := rfl @[simp, norm_cast] lemma coe_sub (x y : ℝ) : ↑(x - y) = (x - y : ℝ*) := rfl @[simp, norm_cast] lemma coe_lt_coe {x y : ℝ} : (x : ℝ*) < y ↔ x < y := germ.const_lt U @[simp, norm_cast] lemma coe_pos {x : ℝ} : 0 < (x : ℝ*) ↔ 0 < x := coe_lt_coe @[simp, norm_cast] lemma coe_le_coe {x y : ℝ} : (x : ℝ*) ≤ y ↔ x ≤ y := germ.const_le_iff @[simp, norm_cast] lemma coe_abs (x : ℝ) : ((abs x : ℝ) : ℝ*) = abs x := germ.const_abs _ _ @[simp, norm_cast] lemma coe_max (x y : ℝ) : ((max x y : ℝ) : ℝ*) = max x y := germ.const_max _ _ _ @[simp, norm_cast] lemma coe_min (x y : ℝ) : ((min x y : ℝ) : ℝ*) = min x y := germ.const_min _ _ _ /-- Construct a hyperreal number from a sequence of real numbers. -/ noncomputable def of_seq (f : ℕ → ℝ) : ℝ* := (↑f : germ (@hyperfilter ℕ) ℝ) /-- A sample infinitesimal hyperreal-/ noncomputable def epsilon : ℝ* := of_seq $ λ n, n⁻¹ /-- A sample infinite hyperreal-/ noncomputable def omega : ℝ* := of_seq coe localized "notation `ε` := hyperreal.epsilon" in hyperreal localized "notation `ω` := hyperreal.omega" in hyperreal lemma epsilon_eq_inv_omega : ε = ω⁻¹ := rfl lemma inv_epsilon_eq_omega : ε⁻¹ = ω := @inv_inv' _ _ ω lemma epsilon_pos : 0 < ε := suffices ∀ᶠ i in hyperfilter, (0 : ℝ) < (i : ℕ)⁻¹, by rwa lt_def U, have h0' : {n : ℕ | ¬ 0 < n} = {0} := by simp only [not_lt, (set.set_of_eq_eq_singleton).symm]; ext; exact nat.le_zero_iff, begin simp only [inv_pos, nat.cast_pos], exact mem_hyperfilter_of_finite_compl (by convert set.finite_singleton _), end lemma epsilon_ne_zero : ε ≠ 0 := ne_of_gt epsilon_pos lemma omega_pos : 0 < ω := by rw ←inv_epsilon_eq_omega; exact inv_pos.2 epsilon_pos lemma omega_ne_zero : ω ≠ 0 := ne_of_gt omega_pos theorem epsilon_mul_omega : ε * ω = 1 := @inv_mul_cancel _ _ ω omega_ne_zero lemma lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, 0 < r → of_seq f < (r : ℝ*) := begin simp only [metric.tendsto_at_top, dist_zero_right, norm, lt_def U] at hf ⊢, intros r hr, cases hf r hr with N hf', have hs : {i : ℕ | f i < r}ᶜ ⊆ {i : ℕ | i ≤ N} := λ i hi1, le_of_lt (by simp only [lt_iff_not_ge]; exact λ hi2, hi1 (lt_of_le_of_lt (le_abs_self _) (hf' i hi2)) : i < N), exact mem_hyperfilter_of_finite_compl ((set.finite_le_nat N).subset hs) end lemma neg_lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, 0 < r → (-r : ℝ*) < of_seq f := λ r hr, have hg : _ := hf.neg, neg_lt_of_neg_lt (by rw [neg_zero] at hg; exact lt_of_tendsto_zero_of_pos hg hr) lemma gt_of_tendsto_zero_of_neg {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, r < 0 → (r : ℝ*) < of_seq f := λ r hr, by rw [←neg_neg r, coe_neg]; exact neg_lt_of_tendsto_zero_of_pos hf (neg_pos.mpr hr) lemma epsilon_lt_pos (x : ℝ) : 0 < x → ε < x := lt_of_tendsto_zero_of_pos tendsto_inverse_at_top_nhds_0_nat /-- Standard part predicate -/ def is_st (x : ℝ*) (r : ℝ) := ∀ δ : ℝ, 0 < δ → (r - δ : ℝ*) < x ∧ x < r + δ /-- Standard part function: like a "round" to ℝ instead of ℤ -/ noncomputable def st : ℝ* → ℝ := λ x, if h : ∃ r, is_st x r then classical.some h else 0 /-- A hyperreal number is infinitesimal if its standard part is 0 -/ def infinitesimal (x : ℝ*) := is_st x 0 /-- A hyperreal number is positive infinite if it is larger than all real numbers -/ def infinite_pos (x : ℝ*) := ∀ r : ℝ, ↑r < x /-- A hyperreal number is negative infinite if it is smaller than all real numbers -/ def infinite_neg (x : ℝ*) := ∀ r : ℝ, x < r /-- A hyperreal number is infinite if it is infinite positive or infinite negative -/ def infinite (x : ℝ*) := infinite_pos x ∨ infinite_neg x /-! ### Some facts about `st` -/ private lemma is_st_unique' (x : ℝ*) (r s : ℝ) (hr : is_st x r) (hs : is_st x s) (hrs : r < s) : false := have hrs' : _ := half_pos $ sub_pos_of_lt hrs, have hr' : _ := (hr _ hrs').2, have hs' : _ := (hs _ hrs').1, have h : s - ((s - r) / 2) = r + (s - r) / 2 := by linarith, begin norm_cast at *, rw h at hs', exact not_lt_of_lt hs' hr' end theorem is_st_unique {x : ℝ*} {r s : ℝ} (hr : is_st x r) (hs : is_st x s) : r = s := begin rcases lt_trichotomy r s with h | h | h, { exact false.elim (is_st_unique' x r s hr hs h) }, { exact h }, { exact false.elim (is_st_unique' x s r hs hr h) } end theorem not_infinite_of_exists_st {x : ℝ*} : (∃ r : ℝ, is_st x r) → ¬ infinite x := λ he hi, Exists.dcases_on he $ λ r hr, hi.elim (λ hip, not_lt_of_lt (hr 2 zero_lt_two).2 (hip $ r + 2)) (λ hin, not_lt_of_lt (hr 2 zero_lt_two).1 (hin $ r - 2)) theorem is_st_Sup {x : ℝ*} (hni : ¬ infinite x) : is_st x (Sup {y : ℝ | (y : ℝ*) < x}) := let S : set ℝ := {y : ℝ | (y : ℝ*) < x} in let R : _ := Sup S in have hnile : _ := not_forall.mp (not_or_distrib.mp hni).1, have hnige : _ := not_forall.mp (not_or_distrib.mp hni).2, Exists.dcases_on hnile $ Exists.dcases_on hnige $ λ r₁ hr₁ r₂ hr₂, have HR₁ : ∃ y : ℝ, y ∈ S := ⟨r₁ - 1, lt_of_lt_of_le (coe_lt_coe.2 $ sub_one_lt _) (not_lt.mp hr₁) ⟩, have HR₂ : ∃ z : ℝ, ∀ y ∈ S, y ≤ z := ⟨ r₂, λ y hy, le_of_lt (coe_lt_coe.1 (lt_of_lt_of_le hy (not_lt.mp hr₂))) ⟩, λ δ hδ, ⟨ lt_of_not_ge' $ λ c, have hc : ∀ y ∈ S, y ≤ R - δ := λ y hy, coe_le_coe.1 $ le_of_lt $ lt_of_lt_of_le hy c, not_lt_of_le ((real.Sup_le _ HR₁ HR₂).mpr hc) $ sub_lt_self R hδ, lt_of_not_ge' $ λ c, have hc : ↑(R + δ / 2) < x := lt_of_lt_of_le (add_lt_add_left (coe_lt_coe.2 (half_lt_self hδ)) R) c, not_lt_of_le (real.le_Sup _ HR₂ hc) $ (lt_add_iff_pos_right _).mpr $ half_pos hδ⟩ theorem exists_st_of_not_infinite {x : ℝ*} (hni : ¬ infinite x) : ∃ r : ℝ, is_st x r := ⟨Sup {y : ℝ | (y : ℝ*) < x}, is_st_Sup hni⟩ theorem st_eq_Sup {x : ℝ*} : st x = Sup {y : ℝ | (y : ℝ*) < x} := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) (is_st_Sup (not_infinite_of_exists_st h)) }, { cases not_imp_comm.mp exists_st_of_not_infinite h with H H, { rw (set.ext (λ i, ⟨λ hi, set.mem_univ i, λ hi, H i⟩) : {y : ℝ | (y : ℝ*) < x} = set.univ), exact (real.Sup_univ).symm }, { rw (set.ext (λ i, ⟨λ hi, false.elim (not_lt_of_lt (H i) hi), λ hi, false.elim (set.not_mem_empty i hi)⟩) : {y : ℝ | (y : ℝ*) < x} = ∅), exact (real.Sup_empty).symm } } end theorem exists_st_iff_not_infinite {x : ℝ*} : (∃ r : ℝ, is_st x r) ↔ ¬ infinite x := ⟨ not_infinite_of_exists_st, exists_st_of_not_infinite ⟩ theorem infinite_iff_not_exists_st {x : ℝ*} : infinite x ↔ ¬ ∃ r : ℝ, is_st x r := iff_not_comm.mp exists_st_iff_not_infinite theorem st_infinite {x : ℝ*} (hi : infinite x) : st x = 0 := begin unfold st, split_ifs, { exact false.elim ((infinite_iff_not_exists_st.mp hi) h) }, { refl } end lemma st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : st x = r := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) hxr }, { exact false.elim (h ⟨r, hxr⟩) } end lemma is_st_st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st x (st x) := by rwa [st_of_is_st hxr] lemma is_st_st_of_exists_st {x : ℝ*} (hx : ∃ r : ℝ, is_st x r) : is_st x (st x) := Exists.dcases_on hx (λ r, is_st_st_of_is_st) lemma is_st_st {x : ℝ*} (hx : st x ≠ 0) : is_st x (st x) := begin unfold st, split_ifs, { exact classical.some_spec h }, { exact false.elim (hx (by unfold st; split_ifs; refl)) } end lemma is_st_st' {x : ℝ*} (hx : ¬ infinite x) : is_st x (st x) := is_st_st_of_exists_st $ exists_st_of_not_infinite hx lemma is_st_refl_real (r : ℝ) : is_st r r := λ δ hδ, ⟨ sub_lt_self _ (coe_lt_coe.2 hδ), (lt_add_of_pos_right _ (coe_lt_coe.2 hδ)) ⟩ lemma st_id_real (r : ℝ) : st r = r := st_of_is_st (is_st_refl_real r) lemma eq_of_is_st_real {r s : ℝ} : is_st r s → r = s := is_st_unique (is_st_refl_real r) lemma is_st_real_iff_eq {r s : ℝ} : is_st r s ↔ r = s := ⟨eq_of_is_st_real, λ hrs, by rw [hrs]; exact is_st_refl_real s⟩ lemma is_st_symm_real {r s : ℝ} : is_st r s ↔ is_st s r := by rw [is_st_real_iff_eq, is_st_real_iff_eq, eq_comm] lemma is_st_trans_real {r s t : ℝ} : is_st r s → is_st s t → is_st r t := by rw [is_st_real_iff_eq, is_st_real_iff_eq, is_st_real_iff_eq]; exact eq.trans lemma is_st_inj_real {r₁ r₂ s : ℝ} (h1 : is_st r₁ s) (h2 : is_st r₂ s) : r₁ = r₂ := eq.trans (eq_of_is_st_real h1) (eq_of_is_st_real h2).symm lemma is_st_iff_abs_sub_lt_delta {x : ℝ*} {r : ℝ} : is_st x r ↔ ∀ (δ : ℝ), 0 < δ → abs (x - r) < δ := by simp only [abs_sub_lt_iff, @sub_lt _ _ (r : ℝ*) x _, @sub_lt_iff_lt_add' _ _ x (r : ℝ*) _, and_comm]; refl lemma is_st_add {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x + y) (r + s) := λ hxr hys d hd, have hxr' : _ := hxr (d / 2) (half_pos hd), have hys' : _ := hys (d / 2) (half_pos hd), ⟨by convert add_lt_add hxr'.1 hys'.1 using 1; norm_cast; linarith, by convert add_lt_add hxr'.2 hys'.2 using 1; norm_cast; linarith⟩ lemma is_st_neg {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st (-x) (-r) := λ d hd, by show -(r : ℝ*) - d < -x ∧ -x < -r + d; cases (hxr d hd); split; linarith lemma is_st_sub {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x - y) (r - s) := λ hxr hys, by rw [sub_eq_add_neg, sub_eq_add_neg]; exact is_st_add hxr (is_st_neg hys) /- (st x < st y) → (x < y) → (x ≤ y) → (st x ≤ st y) -/ lemma lt_of_is_st_lt {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : r < s → x < y := λ hrs, have hrs' : 0 < (s - r) / 2 := half_pos (sub_pos.mpr hrs), have hxr' : _ := (hxr _ hrs').2, have hys' : _ := (hys _ hrs').1, have H1 : r + ((s - r) / 2) = (r + s) / 2 := by linarith, have H2 : s - ((s - r) / 2) = (r + s) / 2 := by linarith, begin norm_cast at *, rw H1 at hxr', rw H2 at hys', exact lt_trans hxr' hys' end lemma is_st_le_of_le {x y : ℝ*} {r s : ℝ} (hrx : is_st x r) (hsy : is_st y s) : x ≤ y → r ≤ s := by rw [←not_lt, ←not_lt, not_imp_not]; exact lt_of_is_st_lt hsy hrx lemma st_le_of_le {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : x ≤ y → st x ≤ st y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, is_st_le_of_le hx' hy' lemma lt_of_st_lt {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : st x < st y → x < y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, lt_of_is_st_lt hx' hy' /-! ### Basic lemmas about infinite -/ lemma infinite_pos_def {x : ℝ*} : infinite_pos x ↔ ∀ r : ℝ, ↑r < x := by rw iff_eq_eq; refl lemma infinite_neg_def {x : ℝ*} : infinite_neg x ↔ ∀ r : ℝ, x < r := by rw iff_eq_eq; refl lemma ne_zero_of_infinite {x : ℝ*} : infinite x → x ≠ 0 := λ hI h0, or.cases_on hI (λ hip, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_pos 0) 0)) (λ hin, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_neg 0) 0)) lemma not_infinite_zero : ¬ infinite 0 := λ hI, ne_zero_of_infinite hI rfl lemma pos_of_infinite_pos {x : ℝ*} : infinite_pos x → 0 < x := λ hip, hip 0 lemma neg_of_infinite_neg {x : ℝ*} : infinite_neg x → x < 0 := λ hin, hin 0 lemma not_infinite_pos_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinite_pos x := λ hn hp, not_lt_of_lt (hn 1) (hp 1) lemma not_infinite_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinite_neg x := imp_not_comm.mp not_infinite_pos_of_infinite_neg lemma infinite_neg_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → infinite_neg (-x) := λ hp r, neg_lt.mp (hp (-r)) lemma infinite_pos_neg_of_infinite_neg {x : ℝ*} : infinite_neg x → infinite_pos (-x) := λ hp r, lt_neg.mp (hp (-r)) lemma infinite_pos_iff_infinite_neg_neg {x : ℝ*} : infinite_pos x ↔ infinite_neg (-x) := ⟨ infinite_neg_neg_of_infinite_pos, λ hin, neg_neg x ▸ infinite_pos_neg_of_infinite_neg hin ⟩ lemma infinite_neg_iff_infinite_pos_neg {x : ℝ*} : infinite_neg x ↔ infinite_pos (-x) := ⟨ infinite_pos_neg_of_infinite_neg, λ hin, neg_neg x ▸ infinite_neg_neg_of_infinite_pos hin ⟩ lemma infinite_iff_infinite_neg {x : ℝ*} : infinite x ↔ infinite (-x) := ⟨ λ hi, or.cases_on hi (λ hip, or.inr (infinite_neg_neg_of_infinite_pos hip)) (λ hin, or.inl (infinite_pos_neg_of_infinite_neg hin)), λ hi, or.cases_on hi (λ hipn, or.inr (infinite_neg_iff_infinite_pos_neg.mpr hipn)) (λ hinp, or.inl (infinite_pos_iff_infinite_neg_neg.mpr hinp))⟩ lemma not_infinite_of_infinitesimal {x : ℝ*} : infinitesimal x → ¬ infinite x := λ hi hI, have hi' : _ := (hi 2 zero_lt_two), or.dcases_on hI (λ hip, have hip' : _ := hip 2, not_lt_of_lt hip' (by convert hi'.2; exact (zero_add 2).symm)) (λ hin, have hin' : _ := hin (-2), not_lt_of_lt hin' (by convert hi'.1; exact (zero_sub 2).symm)) lemma not_infinitesimal_of_infinite {x : ℝ*} : infinite x → ¬ infinitesimal x := imp_not_comm.mp not_infinite_of_infinitesimal lemma not_infinitesimal_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinitesimal x := λ hp, not_infinitesimal_of_infinite (or.inl hp) lemma not_infinitesimal_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinitesimal x := λ hn, not_infinitesimal_of_infinite (or.inr hn) lemma infinite_pos_iff_infinite_and_pos {x : ℝ*} : infinite_pos x ↔ (infinite x ∧ 0 < x) := ⟨ λ hip, ⟨or.inl hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hip, hip) (λ hin, false.elim (not_lt_of_lt hp (hin 0))) ⟩ lemma infinite_neg_iff_infinite_and_neg {x : ℝ*} : infinite_neg x ↔ (infinite x ∧ x < 0) := ⟨ λ hip, ⟨or.inr hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hin, false.elim (not_lt_of_lt hp (hin 0))) (λ hip, hip) ⟩ lemma infinite_pos_iff_infinite_of_pos {x : ℝ*} (hp : 0 < x) : infinite_pos x ↔ infinite x := by rw [infinite_pos_iff_infinite_and_pos]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hp⟩⟩ lemma infinite_pos_iff_infinite_of_nonneg {x : ℝ*} (hp : 0 ≤ x) : infinite_pos x ↔ infinite x := or.cases_on (lt_or_eq_of_le hp) (infinite_pos_iff_infinite_of_pos) (λ h, by rw h.symm; exact ⟨λ hIP, false.elim (not_infinite_zero (or.inl hIP)), λ hI, false.elim (not_infinite_zero hI)⟩) lemma infinite_neg_iff_infinite_of_neg {x : ℝ*} (hn : x < 0) : infinite_neg x ↔ infinite x := by rw [infinite_neg_iff_infinite_and_neg]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hn⟩⟩ lemma infinite_pos_abs_iff_infinite_abs {x : ℝ*} : infinite_pos (abs x) ↔ infinite (abs x) := infinite_pos_iff_infinite_of_nonneg (abs_nonneg _) lemma infinite_iff_infinite_pos_abs {x : ℝ*} : infinite x ↔ infinite_pos (abs x) := ⟨ λ hi d, or.cases_on hi (λ hip, by rw [abs_of_pos (hip 0)]; exact hip d) (λ hin, by rw [abs_of_neg (hin 0)]; exact lt_neg.mp (hin (-d))), λ hipa, by { rcases (lt_trichotomy x 0) with h | h | h, { exact or.inr (infinite_neg_iff_infinite_pos_neg.mpr (by rwa abs_of_neg h at hipa)) }, { exact false.elim (ne_zero_of_infinite (or.inl (by rw [h]; rwa [h, abs_zero] at hipa)) h) }, { exact or.inl (by rwa abs_of_pos h at hipa) } } ⟩ lemma infinite_iff_infinite_abs {x : ℝ*} : infinite x ↔ infinite (abs x) := by rw [←infinite_pos_iff_infinite_of_nonneg (abs_nonneg _), infinite_iff_infinite_pos_abs] lemma infinite_iff_abs_lt_abs {x : ℝ*} : infinite x ↔ ∀ r : ℝ, (abs r : ℝ*) < abs x := ⟨ λ hI r, (coe_abs r) ▸ infinite_iff_infinite_pos_abs.mp hI (abs r), λ hR, or.cases_on (max_choice x (-x)) (λ h, or.inl $ λ r, lt_of_le_of_lt (le_abs_self _) (h ▸ (hR r))) (λ h, or.inr $ λ r, neg_lt_neg_iff.mp $ lt_of_le_of_lt (neg_le_abs_self _) (h ▸ (hR r)))⟩ lemma infinite_pos_add_not_infinite_neg {x y : ℝ*} : infinite_pos x → ¬ infinite_neg y → infinite_pos (x + y) := begin intros hip hnin r, cases not_forall.mp hnin with r₂ hr₂, convert add_lt_add_of_lt_of_le (hip (r + -r₂)) (not_lt.mp hr₂) using 1, simp end lemma not_infinite_neg_add_infinite_pos {x y : ℝ*} : ¬ infinite_neg x → infinite_pos y → infinite_pos (x + y) := λ hx hy, by rw [add_comm]; exact infinite_pos_add_not_infinite_neg hy hx lemma infinite_neg_add_not_infinite_pos {x y : ℝ*} : infinite_neg x → ¬ infinite_pos y → infinite_neg (x + y) := by rw [@infinite_neg_iff_infinite_pos_neg x, @infinite_pos_iff_infinite_neg_neg y, @infinite_neg_iff_infinite_pos_neg (x + y), neg_add]; exact infinite_pos_add_not_infinite_neg lemma not_infinite_pos_add_infinite_neg {x y : ℝ*} : ¬ infinite_pos x → infinite_neg y → infinite_neg (x + y) := λ hx hy, by rw [add_comm]; exact infinite_neg_add_not_infinite_pos hy hx lemma infinite_pos_add_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_infinite_neg_of_infinite_pos hy) lemma infinite_neg_add_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_infinite_pos_of_infinite_neg hy) lemma infinite_pos_add_not_infinite {x y : ℝ*} : infinite_pos x → ¬ infinite y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_or_distrib.mp hy).2 lemma infinite_neg_add_not_infinite {x y : ℝ*} : infinite_neg x → ¬ infinite y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_or_distrib.mp hy).1 theorem infinite_pos_of_tendsto_top {f : ℕ → ℝ} (hf : tendsto f at_top at_top) : infinite_pos (of_seq f) := λ r, have hf' : _ := (tendsto_at_top_at_top _).mp hf, Exists.cases_on (hf' (r + 1)) $ λ i hi, have hi' : ∀ (a : ℕ), f a < (r + 1) → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : {a : ℕ | r < f a}ᶜ ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_le_of_lt har (lt_add_one _))), (germ.coe_lt U).2 $ mem_hyperfilter_of_finite_compl $ (set.finite_le_nat _).subset hS theorem infinite_neg_of_tendsto_bot {f : ℕ → ℝ} (hf : tendsto f at_top at_bot) : infinite_neg (of_seq f) := λ r, have hf' : _ := (tendsto_at_top_at_bot _).mp hf, Exists.cases_on (hf' (r - 1)) $ λ i hi, have hi' : ∀ (a : ℕ), r - 1 < f a → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : {a : ℕ | f a < r}ᶜ ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_lt_of_le (sub_one_lt _) har)), (germ.coe_lt U).2 $ mem_hyperfilter_of_finite_compl $ (set.finite_le_nat _).subset hS lemma not_infinite_neg {x : ℝ*} : ¬ infinite x → ¬ infinite (-x) := not_imp_not.mpr infinite_iff_infinite_neg.mpr lemma not_infinite_add {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x + y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s + r, is_st_add hs hr⟩ theorem not_infinite_iff_exist_lt_gt {x : ℝ*} : ¬ infinite x ↔ ∃ r s : ℝ, (r : ℝ*) < x ∧ x < s := ⟨ λ hni, Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).1) $ Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).2) $ λ r hr s hs, by rw [not_lt] at hr hs; exact ⟨r - 1, s + 1, ⟨ lt_of_lt_of_le (by rw sub_eq_add_neg; norm_num) hr, lt_of_le_of_lt hs (by norm_num)⟩ ⟩, λ hrs, Exists.dcases_on hrs $ λ r hr, Exists.dcases_on hr $ λ s hs, not_or_distrib.mpr ⟨not_forall.mpr ⟨s, lt_asymm (hs.2)⟩, not_forall.mpr ⟨r, lt_asymm (hs.1) ⟩⟩⟩ theorem not_infinite_real (r : ℝ) : ¬ infinite r := by rw not_infinite_iff_exist_lt_gt; exact ⟨ r - 1, r + 1, coe_lt_coe.2 $ sub_one_lt r, coe_lt_coe.2 $ lt_add_one r⟩ theorem not_real_of_infinite {x : ℝ*} : infinite x → ∀ r : ℝ, x ≠ r := λ hi r hr, not_infinite_real r $ @eq.subst _ infinite _ _ hr hi /-! ### Facts about `st` that require some infinite machinery -/ private lemma is_st_mul' {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) (hs : s ≠ 0) : is_st (x * y) (r * s) := have hxr' : _ := is_st_iff_abs_sub_lt_delta.mp hxr, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys, have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, is_st_iff_abs_sub_lt_delta.mpr $ λ d hd, calc abs (x * y - r * s) = abs (x * (y - s) + (x - r) * s) : by rw [mul_sub, sub_mul, add_sub, sub_add_cancel] ... ≤ abs (x * (y - s)) + abs ((x - r) * s) : abs_add _ _ ... ≤ abs x * abs (y - s) + abs (x - r) * abs s : by simp only [abs_mul] ... ≤ abs x * ((d / t) / 2 : ℝ) + ((d / abs s) / 2 : ℝ) * abs s : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ hys' _ $ half_pos $ div_pos hd $ coe_pos.1 $ lt_of_le_of_lt (abs_nonneg x) ht) $ abs_nonneg _) (mul_le_mul_of_nonneg_right (le_of_lt $ hxr' _ $ half_pos $ div_pos hd $ abs_pos.2 hs) $ abs_nonneg _) ... = (d / 2 * (abs x / t) + d / 2 : ℝ*) : by { push_cast, have : (abs s : ℝ*) ≠ 0, by simpa, field_simp [this, @two_ne_zero ℝ* _, add_mul, mul_add, mul_assoc, mul_comm, mul_left_comm] } ... < (d / 2 * 1 + d / 2 : ℝ*) : add_lt_add_right (mul_lt_mul_of_pos_left ((div_lt_one $ lt_of_le_of_lt (abs_nonneg x) ht).mpr ht) $ half_pos $ coe_pos.2 hd) _ ... = (d : ℝ*) : by rw [mul_one, add_halves] lemma is_st_mul {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : is_st (x * y) (r * s) := have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, begin by_cases hs : s = 0, { apply is_st_iff_abs_sub_lt_delta.mpr, intros d hd, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys (d / t) (div_pos hd (coe_pos.1 (lt_of_le_of_lt (abs_nonneg x) ht))), rw [hs, coe_zero, sub_zero] at hys', rw [hs, mul_zero, coe_zero, sub_zero, abs_mul, mul_comm, ←div_mul_cancel (d : ℝ*) (ne_of_gt (lt_of_le_of_lt (abs_nonneg x) ht)), ←coe_div], exact mul_lt_mul'' hys' ht (abs_nonneg _) (abs_nonneg _) }, exact is_st_mul' hxr hys hs, end --AN INFINITE LEMMA THAT REQUIRES SOME MORE ST MACHINERY lemma not_infinite_mul {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x * y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s * r, is_st_mul hs hr⟩ --- lemma st_add {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x + y) = st x + st y := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_add hx hy), have hxy' : _ := is_st_add hx' hy', is_st_unique hxy hxy' lemma st_neg (x : ℝ*) : st (-x) = - st x := if h : infinite x then by rw [st_infinite h, st_infinite (infinite_iff_infinite_neg.mp h), neg_zero] else is_st_unique (is_st_st' (not_infinite_neg h)) (is_st_neg (is_st_st' h)) lemma st_mul {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x * y) = (st x) * (st y) := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_mul hx hy), have hxy' : _ := is_st_mul hx' hy', is_st_unique hxy hxy' /-! ### Basic lemmas about infinitesimal -/ theorem infinitesimal_def {x : ℝ*} : infinitesimal x ↔ (∀ r : ℝ, 0 < r → -(r : ℝ*) < x ∧ x < r) := ⟨ λ hi r hr, by { convert (hi r hr); simp }, λ hi d hd, by { convert (hi d hd); simp } ⟩ theorem lt_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, 0 < r → x < r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).2 theorem lt_neg_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, 0 < r → -↑r < x := λ hi r hr, ((infinitesimal_def.mp hi) r hr).1 theorem gt_of_neg_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r < 0 → ↑r < x := λ hi r hr, by convert ((infinitesimal_def.mp hi) (-r) (neg_pos.mpr hr)).1; exact (neg_neg ↑r).symm theorem abs_lt_real_iff_infinitesimal {x : ℝ*} : infinitesimal x ↔ ∀ r : ℝ, r ≠ 0 → abs x < abs r := ⟨ λ hi r hr, abs_lt.mpr (by rw ←coe_abs; exact infinitesimal_def.mp hi (abs r) (abs_pos.2 hr)), λ hR, infinitesimal_def.mpr $ λ r hr, abs_lt.mp $ (abs_of_pos $ coe_pos.2 hr) ▸ hR r $ ne_of_gt hr ⟩ lemma infinitesimal_zero : infinitesimal 0 := is_st_refl_real 0 lemma zero_of_infinitesimal_real {r : ℝ} : infinitesimal r → r = 0 := eq_of_is_st_real lemma zero_iff_infinitesimal_real {r : ℝ} : infinitesimal r ↔ r = 0 := ⟨zero_of_infinitesimal_real, λ hr, by rw hr; exact infinitesimal_zero⟩ lemma infinitesimal_add {x y : ℝ*} (hx : infinitesimal x) (hy : infinitesimal y) : infinitesimal (x + y) := by simpa only [add_zero] using is_st_add hx hy lemma infinitesimal_neg {x : ℝ*} (hx : infinitesimal x) : infinitesimal (-x) := by simpa only [neg_zero] using is_st_neg hx lemma infinitesimal_neg_iff {x : ℝ*} : infinitesimal x ↔ infinitesimal (-x) := ⟨infinitesimal_neg, λ h, (neg_neg x) ▸ @infinitesimal_neg (-x) h⟩ lemma infinitesimal_mul {x y : ℝ*} (hx : infinitesimal x) (hy : infinitesimal y) : infinitesimal (x * y) := by simpa only [mul_zero] using is_st_mul hx hy theorem infinitesimal_of_tendsto_zero {f : ℕ → ℝ} : tendsto f at_top (𝓝 0) → infinitesimal (of_seq f) := λ hf d hd, by rw [sub_eq_add_neg, ←coe_neg, ←coe_add, ←coe_add, zero_add, zero_add]; exact ⟨neg_lt_of_tendsto_zero_of_pos hf hd, lt_of_tendsto_zero_of_pos hf hd⟩ theorem infinitesimal_epsilon : infinitesimal ε := infinitesimal_of_tendsto_zero tendsto_inverse_at_top_nhds_0_nat lemma not_real_of_infinitesimal_ne_zero (x : ℝ*) : infinitesimal x → x ≠ 0 → ∀ r : ℝ, x ≠ r := λ hi hx r hr, hx $ hr.trans $ coe_eq_zero.2 $ is_st_unique (hr.symm ▸ is_st_refl_real r : is_st x r) hi theorem infinitesimal_sub_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : infinitesimal (x - r) := show is_st (x + -r) 0, by rw ←add_neg_self r; exact is_st_add hxr (is_st_refl_real (-r)) theorem infinitesimal_sub_st {x : ℝ*} (hx : ¬infinite x) : infinitesimal (x - st x) := infinitesimal_sub_is_st $ is_st_st' hx lemma infinite_pos_iff_infinitesimal_inv_pos {x : ℝ*} : infinite_pos x ↔ (infinitesimal x⁻¹ ∧ 0 < x⁻¹) := ⟨ λ hip, ⟨ infinitesimal_def.mpr $ λ r hr, ⟨ lt_trans (coe_lt_coe.2 (neg_neg_of_pos hr)) (inv_pos.2 (hip 0)), (inv_lt (coe_lt_coe.2 hr) (hip 0)).mp (by convert hip r⁻¹) ⟩, inv_pos.2 $ hip 0 ⟩, λ ⟨hi, hp⟩ r, @classical.by_cases (r = 0) (↑r < x) (λ h, eq.substr h (inv_pos.mp hp)) $ λ h, lt_of_le_of_lt (coe_le_coe.2 (le_abs_self r)) ((inv_lt_inv (inv_pos.mp hp) (coe_lt_coe.2 (abs_pos.2 h))).mp ((infinitesimal_def.mp hi) ((abs r)⁻¹) (inv_pos.2 (abs_pos.2 h))).2) ⟩ lemma infinite_neg_iff_infinitesimal_inv_neg {x : ℝ*} : infinite_neg x ↔ (infinitesimal x⁻¹ ∧ x⁻¹ < 0) := ⟨ λ hin, have hin' : _ := infinite_pos_iff_infinitesimal_inv_pos.mp (infinite_pos_neg_of_infinite_neg hin), by rwa [infinitesimal_neg_iff, ←neg_pos, neg_inv], λ hin, have h0 : x ≠ 0 := λ h0, (lt_irrefl (0 : ℝ*) (by convert hin.2; rw [h0, inv_zero])), by rwa [←neg_pos, infinitesimal_neg_iff, neg_inv, ←infinite_pos_iff_infinitesimal_inv_pos, ←infinite_neg_iff_infinite_pos_neg] at hin ⟩ theorem infinitesimal_inv_of_infinite {x : ℝ*} : infinite x → infinitesimal x⁻¹ := λ hi, or.cases_on hi (λ hip, (infinite_pos_iff_infinitesimal_inv_pos.mp hip).1) (λ hin, (infinite_neg_iff_infinitesimal_inv_neg.mp hin).1) theorem infinite_of_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) (hi : infinitesimal x⁻¹ ) : infinite x := begin cases (lt_or_gt_of_ne h0) with hn hp, { exact or.inr (infinite_neg_iff_infinitesimal_inv_neg.mpr ⟨hi, inv_lt_zero.mpr hn⟩) }, { exact or.inl (infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨hi, inv_pos.mpr hp⟩) } end theorem infinite_iff_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) : infinite x ↔ infinitesimal x⁻¹ := ⟨ infinitesimal_inv_of_infinite, infinite_of_infinitesimal_inv h0 ⟩ lemma infinitesimal_pos_iff_infinite_pos_inv {x : ℝ*} : infinite_pos x⁻¹ ↔ (infinitesimal x ∧ 0 < x) := by convert infinite_pos_iff_infinitesimal_inv_pos; simp only [inv_inv'] lemma infinitesimal_neg_iff_infinite_neg_inv {x : ℝ*} : infinite_neg x⁻¹ ↔ (infinitesimal x ∧ x < 0) := by convert infinite_neg_iff_infinitesimal_inv_neg; simp only [inv_inv'] theorem infinitesimal_iff_infinite_inv {x : ℝ*} (h : x ≠ 0) : infinitesimal x ↔ infinite x⁻¹ := by convert (infinite_iff_infinitesimal_inv (inv_ne_zero h)).symm; simp only [inv_inv'] /-! ### `st` stuff that requires infinitesimal machinery -/ theorem is_st_of_tendsto {f : ℕ → ℝ} {r : ℝ} (hf : tendsto f at_top (𝓝 r)) : is_st (of_seq f) r := have hg : tendsto (λ n, f n - r) at_top (𝓝 0) := (sub_self r) ▸ (hf.sub tendsto_const_nhds), by rw [←(zero_add r), ←(sub_add_cancel f (λ n, r))]; exact is_st_add (infinitesimal_of_tendsto_zero hg) (is_st_refl_real r) lemma is_st_inv {x : ℝ*} {r : ℝ} (hi : ¬ infinitesimal x) : is_st x r → is_st x⁻¹ r⁻¹ := λ hxr, have h : x ≠ 0 := (λ h, hi (h.symm ▸ infinitesimal_zero)), have H : _ := exists_st_of_not_infinite $ not_imp_not.mpr (infinitesimal_iff_infinite_inv h).mpr hi, Exists.cases_on H $ λ s hs, have H' : is_st 1 (r * s) := mul_inv_cancel h ▸ is_st_mul hxr hs, have H'' : s = r⁻¹ := one_div r ▸ eq_one_div_of_mul_eq_one (eq_of_is_st_real H').symm, H'' ▸ hs lemma st_inv (x : ℝ*) : st x⁻¹ = (st x)⁻¹ := begin by_cases h0 : x = 0, rw [h0, inv_zero, ←coe_zero, st_id_real, inv_zero], by_cases h1 : infinitesimal x, rw [st_infinite ((infinitesimal_iff_infinite_inv h0).mp h1), st_of_is_st h1, inv_zero], by_cases h2 : infinite x, rw [st_of_is_st (infinitesimal_inv_of_infinite h2), st_infinite h2, inv_zero], exact st_of_is_st (is_st_inv h1 (is_st_st' h2)), end /-! ### Infinite stuff that requires infinitesimal machinery -/ lemma infinite_pos_omega : infinite_pos ω := infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨infinitesimal_epsilon, epsilon_pos⟩ lemma infinite_omega : infinite ω := (infinite_iff_infinitesimal_inv omega_ne_zero).mpr infinitesimal_epsilon lemma infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → 0 < y → infinite_pos (x * y) := λ hx hy₁ hy₂ r, have hy₁' : _ := not_forall.mp (by rw infinitesimal_def at hy₁; exact hy₁), Exists.dcases_on hy₁' $ λ r₁ hy₁'', have hyr : _ := by rw [not_imp, ←abs_lt, not_lt, abs_of_pos hy₂] at hy₁''; exact hy₁'', by rw [←div_mul_cancel r (ne_of_gt hyr.1), coe_mul]; exact mul_lt_mul (hx (r / r₁)) hyr.2 (coe_lt_coe.2 hyr.1) (le_of_lt (hx 0)) lemma infinite_pos_mul_of_not_infinitesimal_pos_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → 0 < x → infinite_pos y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → y < 0 → infinite_pos (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, ←neg_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_pos_mul_of_not_infinitesimal_neg_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_neg y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → y < 0 → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, neg_mul_eq_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_neg_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_pos y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → 0 < y → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, infinite_neg_iff_infinite_pos_neg, neg_mul_eq_neg_mul]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_pos_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → 0 < x → infinite_neg y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_neg_mul_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_pos_mul_infinite_neg {x y : ℝ*} : infinite_pos x → infinite_neg y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_neg_mul_infinite_pos {x y : ℝ*} : infinite_neg x → infinite_pos y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_mul_of_infinite_not_infinitesimal {x y : ℝ*} : infinite x → ¬ infinitesimal y → infinite (x * y) := λ hx hy, have h0 : y < 0 ∨ 0 < y := lt_or_gt_of_ne (λ H0, hy (eq.substr H0 (is_st_refl_real 0))), or.dcases_on hx (or.dcases_on h0 (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos Hx hy H0))) (or.dcases_on h0 (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos Hx hy H0))) lemma infinite_mul_of_not_infinitesimal_infinite {x y : ℝ*} : ¬ infinitesimal x → infinite y → infinite (x * y) := λ hx hy, by rw [mul_comm]; exact infinite_mul_of_infinite_not_infinitesimal hy hx lemma infinite_mul_infinite {x y : ℝ*} : infinite x → infinite y → infinite (x * y) := λ hx hy, infinite_mul_of_infinite_not_infinitesimal hx (not_infinitesimal_of_infinite hy) end hyperreal
15bebcf7db5aa096879a09006a98d962e7f5ffea
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/rel_iso.lean
379cb9c255601346c8a1f443089723a78a15e670
[ "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
22,059
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.group.defs import data.equiv.set import data.fun_like.basic import logic.embedding import order.rel_classes /-! # Relation homomorphisms, embeddings, isomorphisms This file defines relation homomorphisms, embeddings, isomorphisms and order embeddings and isomorphisms. ## Main declarations * `rel_hom`: Relation homomorphism. A `rel_hom r s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. * `rel_embedding`: Relation embedding. A `rel_embedding r s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. * `rel_iso`: Relation isomorphism. A `rel_iso r s` is an equivalence `f : α ≃ β` such that `r a b ↔ s (f a) (f b)`. * `sum_lex_congr`, `prod_lex_congr`: Creates a relation homomorphism between two `sum_lex` or two `prod_lex` from relation homomorphisms between their arguments. ## Notation * `→r`: `rel_hom` * `↪r`: `rel_embedding` * `≃r`: `rel_iso` -/ open function universes u v w variables {α β γ : 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 /-- `rel_hom_class F r s` asserts that `F` is a type of functions such that all `f : F` satisfy `r a b → s (f a) (f b)`. The relations `r` and `s` are `out_param`s since figuring them out from a goal is a higher-order matching problem that Lean usually can't do unaided. -/ class rel_hom_class (F : Type*) {α β : out_param $ Type*} (r : out_param $ α → α → Prop) (s : out_param $ β → β → Prop) extends fun_like F α (λ _, β) := (map_rel : ∀ (f : F) {a b}, r a b → s (f a) (f b)) export rel_hom_class (map_rel) -- The free parameters `r` and `s` are `out_param`s so this is not dangerous. attribute [nolint dangerous_instance] rel_hom_class.to_fun_like namespace rel_hom_class variables {F : Type*} lemma map_inf [semilattice_inf α] [linear_order β] [rel_hom_class F ((<) : β → β → Prop) ((<) : α → α → Prop)] (a : F) (m n : β) : a (m ⊓ n) = a m ⊓ a n := (strict_mono.monotone $ λ x y, map_rel a).map_inf m n lemma map_sup [semilattice_sup α] [linear_order β] [rel_hom_class F ((>) : β → β → Prop) ((>) : α → α → Prop)] (a : F) (m n : β) : a (m ⊔ n) = a m ⊔ a n := @map_inf (order_dual α) (order_dual β) _ _ _ _ _ _ _ protected theorem is_irrefl [rel_hom_class F r s] (f : F) : ∀ [is_irrefl β s], is_irrefl α r | ⟨H⟩ := ⟨λ a h, H _ (map_rel f h)⟩ protected theorem is_asymm [rel_hom_class F r s] (f : F) : ∀ [is_asymm β s], is_asymm α r | ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (map_rel f h₁) (map_rel f h₂)⟩ protected theorem acc [rel_hom_class F r s] (f : F) (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') (map_rel f h) _ rfl⟩ end protected theorem well_founded [rel_hom_class F r s] (f : F) : ∀ (h : well_founded s), well_founded r | ⟨H⟩ := ⟨λ a, rel_hom_class.acc f _ (H _)⟩ end rel_hom_class namespace rel_hom instance : rel_hom_class (r →r s) r s := { coe := λ o, o.to_fun, coe_injective' := λ f g h, by { cases f, cases g, congr' }, map_rel := map_rel' } /-- Auxiliary instance if `rel_hom_class.to_fun_like.to_has_coe_to_fun` isn't found -/ instance : has_coe_to_fun (r →r s) (λ _, α → β) := ⟨λ o, o.to_fun⟩ initialize_simps_projections rel_hom (to_fun → apply) protected 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. -/ theorem coe_fn_injective : @function.injective (r →r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Identity map is a relation homomorphism. -/ @[refl, simps] protected def id (r : α → α → Prop) : r →r r := ⟨λ x, x, λ a b x, x⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans, simps] protected def comp (g : s →r t) (f : r →r s) : r →r t := ⟨λ x, g (f x), λ a b h, g.2 (f.2 h)⟩ /-- 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⟩ 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) -- TODO: define a `rel_iff_class` so we don't have to do all the `convert` trickery? 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 refine rel_hom_class.well_founded (rel_hom.mk _ _ : s →r r), { 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_class.well_founded (⟨f, λ _ _, o.1⟩ : r →r s)) /-- 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 /-- 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⟩ -- TODO: define and instantiate a `rel_embedding_class` when `embedding_like` is defined instance : rel_hom_class (r ↪r s) r s := { coe := coe_fn, coe_injective' := λ f g h, by { rcases f with ⟨⟨⟩⟩, rcases g with ⟨⟨⟩⟩, congr' }, map_rel := λ f a b, iff.mpr (map_rel_iff' f) } /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r ↪r s) : α → β := h initialize_simps_projections rel_embedding (to_embedding_to_fun → apply, -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. -/ theorem coe_fn_injective : @function.injective (r ↪r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext _ _ h theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Identity map is a relation embedding. -/ @[refl, simps] 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 _⟩ 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'} /-- To define an relation embedding from an antisymmetric relation `r` to a reflexive relation `s` it suffices to give a function together with a proof that it satisfies `s (f a) (f b) ↔ r a b`. -/ def of_map_rel_iff (f : α → β) [is_antisymm α r] [is_refl β s] (hf : ∀ a b, s (f a) (f b) ↔ r a b) : r ↪r s := { to_fun := f, inj' := λ x y h, antisymm ((hf _ _).1 (h ▸ refl _)) ((hf _ _).1 (h ▸ refl _)), map_rel_iff' := hf } @[simp] lemma of_map_rel_iff_coe (f : α → β) [is_antisymm α r] [is_refl β s] (hf : ∀ a b, s (f a) (f b) ↔ r a b) : ⇑(of_map_rel_iff f hf : r ↪r s) = f := rfl /-- 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 end rel_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 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'⟩ theorem to_equiv_injective : injective (to_equiv : (r ≃r s) → α ≃ β) | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h } 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⟩ -- TODO: define and instantiate a `rel_iso_class` when `equiv_like` is defined instance : rel_hom_class (r ≃r s) r s := { coe := coe_fn, coe_injective' := equiv.coe_fn_injective.comp to_equiv_injective, map_rel := λ f a b, iff.mpr (map_rel_iff' 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 /-- 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 coe_fn_injective : @function.injective (r ≃r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- 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]⟩ /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r ≃r s) : α → β := h /-- See Note [custom simps projection]. -/ def simps.symm_apply (h : r ≃r s) : β → α := h.symm initialize_simps_projections rel_iso (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) /-- Identity map is a relation isomorphism. -/ @[refl, simps apply] protected def refl (r : α → α → Prop) : r ≃r r := ⟨equiv.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans, simps apply] 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 = 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 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. -/ @[simps apply] 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⟩ /-- Given relation isomorphisms `r₁ ≃r s₁` and `r₂ ≃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₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) : sum.lex r₁ r₂ ≃r sum.lex s₁ 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 s₁` and `r₂ ≃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₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) : prod.lex r₁ r₂ ≃r prod.lex s₁ 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 /-- `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
07bce7148b363f00ce468d10cd7a661cf270a4bf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/quiver/connected_component.lean
7fe9512473a69b637edc82f70c0539a93edae832
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,167
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import combinatorics.quiver.subquiver import combinatorics.quiver.path import data.sum.basic /-! ## Weakly connected components For a quiver `V`, we build a quiver `symmetrify V` by adding a reversal of every edge. Informally, a path in `symmetrify V` corresponds to a 'zigzag' in `V`. This lets us define the type `weakly_connected_component V` as the quotient of `V` by the relation which identifies `a` with `b` if there is a path from `a` to `b` in `symmetrify V`. (These zigzags can be seen as a proof-relevant analogue of `eqv_gen`.) Strongly connected components have not yet been defined. -/ universes v u namespace quiver /-- A type synonym for the symmetrized quiver (with an arrow both ways for each original arrow). NB: this does not work for `Prop`-valued quivers. It requires `[quiver.{v+1} V]`. -/ @[nolint has_nonempty_instance] def symmetrify (V) : Type u := V instance symmetrify_quiver (V : Type u) [quiver V] : quiver (symmetrify V) := ⟨λ a b : V, (a ⟶ b) ⊕ (b ⟶ a)⟩ variables (V : Type u) [quiver.{v+1} V] /-- A quiver `has_reverse` if we can reverse an arrow `p` from `a` to `b` to get an arrow `p.reverse` from `b` to `a`.-/ class has_reverse := (reverse' : Π {a b : V}, (a ⟶ b) → (b ⟶ a)) /-- Reverse the direction of an arrow. -/ def reverse {V} [quiver.{v+1} V] [has_reverse V] {a b : V} : (a ⟶ b) → (b ⟶ a) := has_reverse.reverse' /-- A quiver `has_involutive_reverse` if reversing twice is the identity.`-/ class has_involutive_reverse extends has_reverse V := (inv' : Π {a b : V} (f : a ⟶ b), reverse (reverse f) = f) @[simp] lemma reverse_reverse {V} [quiver.{v+1} V] [h : has_involutive_reverse V] {a b : V} (f : a ⟶ b) : reverse (reverse f) = f := by apply h.inv' variables {V} instance : has_reverse (symmetrify V) := ⟨λ a b e, e.swap⟩ instance : has_involutive_reverse (symmetrify V) := { to_has_reverse := ⟨λ a b e, e.swap⟩, inv' := λ a b e, congr_fun sum.swap_swap_eq e } /-- Reverse the direction of a path. -/ @[simp] def path.reverse [has_reverse V] {a : V} : Π {b}, path a b → path b a | a path.nil := path.nil | b (path.cons p e) := (reverse e).to_path.comp p.reverse @[simp] lemma path.reverse_to_path [has_reverse V] {a b : V} (f : a ⟶ b) : f.to_path.reverse = (reverse f).to_path := rfl @[simp] lemma path.reverse_comp [has_reverse V] {a b c : V} (p : path a b) (q : path b c) : (p.comp q).reverse = q.reverse.comp p.reverse := by { induction q, { simp, }, { simp [q_ih], }, } @[simp] lemma path.reverse_reverse [h : has_involutive_reverse V] {a b : V} (p : path a b) : p.reverse.reverse = p := by { induction p, { simp, }, { simp only [path.reverse, path.reverse_comp, path.reverse_to_path, reverse_reverse, p_ih], refl, }, } /-- The inclusion of a quiver in its symmetrification -/ def symmetrify.of : prefunctor V (symmetrify V) := { obj := id, map := λ X Y f, sum.inl f } /-- Given a quiver `V'` with reversible arrows, a prefunctor to `V'` can be lifted to one from `symmetrify V` to `V'` -/ def symmetrify.lift {V' : Type*} [quiver V'] [has_reverse V'] (φ : prefunctor V V') : prefunctor (symmetrify V) V' := { obj := φ.obj, map := λ X Y f, sum.rec (λ fwd, φ.map fwd) (λ bwd, reverse (φ.map bwd)) f } lemma symmetrify.lift_spec (V' : Type*) [quiver V'] [has_reverse V'] (φ : prefunctor V V') : symmetrify.of.comp (symmetrify.lift φ) = φ := begin fapply prefunctor.ext, { rintro X, refl, }, { rintros X Y f, refl, }, end lemma symmetrify.lift_reverse (V' : Type*) [quiver V'] [h : has_involutive_reverse V'] (φ : prefunctor V V') {X Y : symmetrify V} (f : X ⟶ Y) : (symmetrify.lift φ).map (quiver.reverse f) = quiver.reverse ((symmetrify.lift φ).map f) := begin dsimp [symmetrify.lift], cases f, { simp only, refl, }, { simp only, rw h.inv', refl, } end /-- `lift φ` is the only prefunctor extending `φ` and preserving reverses. -/ lemma symmetrify.lift_unique (V' : Type*) [quiver V'] [has_reverse V'] (φ : prefunctor V V') (Φ : prefunctor (symmetrify V) V') (hΦ : symmetrify.of.comp Φ = φ) (hΦinv : ∀ {X Y : V} (f : X ⟶ Y), Φ.map (reverse f) = reverse (Φ.map f)) : Φ = symmetrify.lift φ := begin subst_vars, fapply prefunctor.ext, { rintro X, refl, }, { rintros X Y f, cases f, { refl, }, { dsimp [symmetrify.lift,symmetrify.of], convert hΦinv (sum.inl f), }, }, end variables (V) /-- Two vertices are related in the zigzag setoid if there is a zigzag of arrows from one to the other. -/ def zigzag_setoid : setoid V := ⟨λ a b, nonempty (@path (symmetrify V) _ a b), λ a, ⟨path.nil⟩, λ a b ⟨p⟩, ⟨p.reverse⟩, λ a b c ⟨p⟩ ⟨q⟩, ⟨p.comp q⟩⟩ /-- The type of weakly connected components of a directed graph. Two vertices are in the same weakly connected component if there is a zigzag of arrows from one to the other. -/ def weakly_connected_component : Type* := quotient (zigzag_setoid V) namespace weakly_connected_component variable {V} /-- The weakly connected component corresponding to a vertex. -/ protected def mk : V → weakly_connected_component V := quotient.mk' instance : has_coe_t V (weakly_connected_component V) := ⟨weakly_connected_component.mk⟩ instance [inhabited V] : inhabited (weakly_connected_component V) := ⟨show V, from default⟩ protected lemma eq (a b : V) : (a : weakly_connected_component V) = b ↔ nonempty (@path (symmetrify V) _ a b) := quotient.eq' end weakly_connected_component variable {V} /-- A wide subquiver `H` of `G.symmetrify` determines a wide subquiver of `G`, containing an an arrow `e` if either `e` or its reversal is in `H`. -/ -- Without the explicit universe level in `quiver.{v+1}` Lean comes up with -- `quiver.{max u_2 u_3 + 1}`. This causes problems elsewhere, so we write `quiver.{v+1}`. def wide_subquiver_symmetrify (H : wide_subquiver (symmetrify V)) : wide_subquiver V := λ a b, { e | sum.inl e ∈ H a b ∨ sum.inr e ∈ H b a } end quiver
557d472e8df64dd325256d2e804f5246264554cb
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/logic/axioms/examples/diaconescu.lean
fea750013d5505f8f7a6e671721b10723e8fc9f1
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,734
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 import logic.axioms.hilbert logic.axioms.funext open eq.ops nonempty inhabited -- Diaconescu’s theorem -- Show that Excluded middle follows from -- Hilbert's choice operator, function extensionality and Prop extensionality context hypothesis propext {a b : Prop} : (a → b) → (b → a) → a = b parameter p : Prop private definition u := epsilon (λx, x = true ∨ p) private definition v := epsilon (λx, x = false ∨ p) private lemma u_def : u = true ∨ p := epsilon_spec (exists_intro true (or.inl rfl)) private lemma v_def : v = false ∨ p := epsilon_spec (exists_intro false (or.inl rfl)) private lemma uv_implies_p : ¬(u = v) ∨ p := or.elim u_def (assume Hut : u = true, or.elim v_def (assume Hvf : v = false, have Hne : ¬(u = v), from Hvf⁻¹ ▸ Hut⁻¹ ▸ true_ne_false, or.inl Hne) (assume Hp : p, or.inr Hp)) (assume Hp : p, or.inr Hp) private lemma p_implies_uv : p → u = v := assume Hp : p, have Hpred : (λ x, x = true ∨ p) = (λ x, x = false ∨ p), from funext (take x : Prop, have Hl : (x = true ∨ p) → (x = false ∨ p), from assume A, or.inr Hp, have Hr : (x = false ∨ p) → (x = true ∨ p), from assume A, or.inr Hp, show (x = true ∨ p) = (x = false ∨ p), from propext Hl Hr), show u = v, from Hpred ▸ (eq.refl (epsilon (λ x, x = true ∨ p))) theorem em : p ∨ ¬p := have H : ¬(u = v) → ¬p, from mt p_implies_uv, or.elim uv_implies_p (assume Hne : ¬(u = v), or.inr (H Hne)) (assume Hp : p, or.inl Hp) end
e7ee78628be8a37ada4551091de6f1c6c3258c6d
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Compiler/IR/NormIds.lean
e9eafe8f3be6ca43def31011d7b8d7597a89d615
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
7,562
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.Compiler.IR.Basic namespace Lean.IR.UniqueIds abbrev M := StateT IndexSet Id def checkId (id : Index) : M Bool := modifyGet fun s => if s.contains id then (false, s) else (true, s.insert id) def checkParams (ps : Array Param) : M Bool := ps.allM fun p => checkId p.x.idx partial def checkFnBody : FnBody → M Bool | .vdecl x _ _ b => checkId x.idx <&&> checkFnBody b | .jdecl j ys _ b => checkId j.idx <&&> checkParams ys <&&> checkFnBody b | .case _ _ _ alts => alts.allM fun alt => checkFnBody alt.body | b => if b.isTerminal then pure true else checkFnBody b.body partial def checkDecl : Decl → M Bool | .fdecl (xs := xs) (body := b) .. => checkParams xs <&&> checkFnBody b | .extern (xs := xs) .. => checkParams xs end UniqueIds /-- Return true if variable, parameter and join point ids are unique -/ def Decl.uniqueIds (d : Decl) : Bool := (UniqueIds.checkDecl d).run' {} namespace NormalizeIds abbrev M := ReaderT IndexRenaming Id def normIndex (x : Index) : M Index := fun m => match m.find? x with | some y => y | none => x def normVar (x : VarId) : M VarId := VarId.mk <$> normIndex x.idx def normJP (x : JoinPointId) : M JoinPointId := JoinPointId.mk <$> normIndex x.idx def normArg : Arg → M Arg | Arg.var x => Arg.var <$> normVar x | other => pure other def normArgs (as : Array Arg) : M (Array Arg) := fun m => as.map fun a => normArg a m def normExpr : Expr → M Expr | Expr.ctor c ys, m => Expr.ctor c (normArgs ys m) | Expr.reset n x, m => Expr.reset n (normVar x m) | Expr.reuse x c u ys, m => Expr.reuse (normVar x m) c u (normArgs ys m) | Expr.proj i x, m => Expr.proj i (normVar x m) | Expr.uproj i x, m => Expr.uproj i (normVar x m) | Expr.sproj n o x, m => Expr.sproj n o (normVar x m) | Expr.fap c ys, m => Expr.fap c (normArgs ys m) | Expr.pap c ys, m => Expr.pap c (normArgs ys m) | Expr.ap x ys, m => Expr.ap (normVar x m) (normArgs ys m) | Expr.box t x, m => Expr.box t (normVar x m) | Expr.unbox x, m => Expr.unbox (normVar x m) | Expr.isShared x, m => Expr.isShared (normVar x m) | Expr.isTaggedPtr x, m => Expr.isTaggedPtr (normVar x m) | e@(Expr.lit _), _ => e abbrev N := ReaderT IndexRenaming (StateM Nat) @[inline] def withVar {α : Type} (x : VarId) (k : VarId → N α) : N α := fun m => do let n ← getModify (fun n => n + 1) k { idx := n } (m.insert x.idx n) @[inline] def withJP {α : Type} (x : JoinPointId) (k : JoinPointId → N α) : N α := fun m => do let n ← getModify (fun n => n + 1) k { idx := n } (m.insert x.idx n) @[inline] def withParams {α : Type} (ps : Array Param) (k : Array Param → N α) : N α := fun m => do let m ← ps.foldlM (init := m) fun m p => do let n ← getModify fun n => n + 1 return m.insert p.x.idx n let ps := ps.map fun p => { p with x := normVar p.x m } k ps m instance : MonadLift M N := ⟨fun x m => return x m⟩ partial def normFnBody : FnBody → N FnBody | FnBody.vdecl x t v b => do let v ← normExpr v; withVar x fun x => return FnBody.vdecl x t v (← normFnBody b) | FnBody.jdecl j ys v b => do let (ys, v) ← withParams ys fun ys => do let v ← normFnBody v; pure (ys, v) withJP j fun j => return FnBody.jdecl j ys v (← normFnBody b) | FnBody.set x i y b => return FnBody.set (← normVar x) i (← normArg y) (← normFnBody b) | FnBody.uset x i y b => return FnBody.uset (← normVar x) i (← normVar y) (← normFnBody b) | FnBody.sset x i o y t b => return FnBody.sset (← normVar x) i o (← normVar y) t (← normFnBody b) | FnBody.setTag x i b => return FnBody.setTag (← normVar x) i (← normFnBody b) | FnBody.inc x n c p b => return FnBody.inc (← normVar x) n c p (← normFnBody b) | FnBody.dec x n c p b => return FnBody.dec (← normVar x) n c p (← normFnBody b) | FnBody.del x b => return FnBody.del (← normVar x) (← normFnBody b) | FnBody.mdata d b => return FnBody.mdata d (← normFnBody b) | FnBody.case tid x xType alts => do let x ← normVar x let alts ← alts.mapM fun alt => alt.mmodifyBody normFnBody return FnBody.case tid x xType alts | FnBody.jmp j ys => return FnBody.jmp (← normJP j) (← normArgs ys) | FnBody.ret x => return FnBody.ret (← normArg x) | FnBody.unreachable => pure FnBody.unreachable def normDecl (d : Decl) : N Decl := match d with | Decl.fdecl (xs := xs) (body := b) .. => withParams xs fun _ => return d.updateBody! (← normFnBody b) | other => pure other end NormalizeIds /-- Create a declaration equivalent to `d` s.t. `d.normalizeIds.uniqueIds == true` -/ def Decl.normalizeIds (d : Decl) : Decl := (NormalizeIds.normDecl d {}).run' 1 /-! Apply a function `f : VarId → VarId` to variable occurrences. The following functions assume the IR code does not have variable shadowing. -/ namespace MapVars @[inline] def mapArg (f : VarId → VarId) : Arg → Arg | Arg.var x => Arg.var (f x) | a => a def mapArgs (f : VarId → VarId) (as : Array Arg) : Array Arg := as.map (mapArg f) def mapExpr (f : VarId → VarId) : Expr → Expr | Expr.ctor c ys => Expr.ctor c (mapArgs f ys) | Expr.reset n x => Expr.reset n (f x) | Expr.reuse x c u ys => Expr.reuse (f x) c u (mapArgs f ys) | Expr.proj i x => Expr.proj i (f x) | Expr.uproj i x => Expr.uproj i (f x) | Expr.sproj n o x => Expr.sproj n o (f x) | Expr.fap c ys => Expr.fap c (mapArgs f ys) | Expr.pap c ys => Expr.pap c (mapArgs f ys) | Expr.ap x ys => Expr.ap (f x) (mapArgs f ys) | Expr.box t x => Expr.box t (f x) | Expr.unbox x => Expr.unbox (f x) | Expr.isShared x => Expr.isShared (f x) | Expr.isTaggedPtr x => Expr.isTaggedPtr (f x) | e@(Expr.lit _) => e partial def mapFnBody (f : VarId → VarId) : FnBody → FnBody | FnBody.vdecl x t v b => FnBody.vdecl x t (mapExpr f v) (mapFnBody f b) | FnBody.jdecl j ys v b => FnBody.jdecl j ys (mapFnBody f v) (mapFnBody f b) | FnBody.set x i y b => FnBody.set (f x) i (mapArg f y) (mapFnBody f b) | FnBody.setTag x i b => FnBody.setTag (f x) i (mapFnBody f b) | FnBody.uset x i y b => FnBody.uset (f x) i (f y) (mapFnBody f b) | FnBody.sset x i o y t b => FnBody.sset (f x) i o (f y) t (mapFnBody f b) | FnBody.inc x n c p b => FnBody.inc (f x) n c p (mapFnBody f b) | FnBody.dec x n c p b => FnBody.dec (f x) n c p (mapFnBody f b) | FnBody.del x b => FnBody.del (f x) (mapFnBody f b) | FnBody.mdata d b => FnBody.mdata d (mapFnBody f b) | FnBody.case tid x xType alts => FnBody.case tid (f x) xType (alts.map fun alt => alt.modifyBody (mapFnBody f)) | FnBody.jmp j ys => FnBody.jmp j (mapArgs f ys) | FnBody.ret x => FnBody.ret (mapArg f x) | FnBody.unreachable => FnBody.unreachable end MapVars @[inline] def FnBody.mapVars (f : VarId → VarId) (b : FnBody) : FnBody := MapVars.mapFnBody f b /-- Replace `x` with `y` in `b`. This function assumes `b` does not shadow `x` -/ def FnBody.replaceVar (x y : VarId) (b : FnBody) : FnBody := b.mapVars fun z => if x == z then y else z end Lean.IR
4cc9e746df6a6e01aeffa05553f6c5119494c372
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/tactic/push_neg.lean
0295a10fe4f9e9e0378235feab1d042925cf41f0
[ "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
7,965
lean
/- Copyright (c) 2019 Patrick Massot All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Simon Hudon A tactic pushing negations into an expression -/ import logic.basic open tactic expr namespace push_neg section universe u variable {α : Sort u} variables (p q : Prop) variable (s : α → Prop) local attribute [instance, priority 10] classical.prop_decidable theorem not_not_eq : (¬ ¬ p) = p := propext not_not theorem not_and_eq : (¬ (p ∧ q)) = (p → ¬ q) := propext not_and theorem not_or_eq : (¬ (p ∨ q)) = (¬ p ∧ ¬ q) := propext not_or_distrib theorem not_forall_eq : (¬ ∀ x, s x) = (∃ x, ¬ s x) := propext not_forall theorem not_exists_eq : (¬ ∃ x, s x) = (∀ x, ¬ s x) := propext not_exists theorem not_implies_eq : (¬ (p → q)) = (p ∧ ¬ q) := propext not_imp theorem classical.implies_iff_not_or : (p → q) ↔ (¬ p ∨ q) := imp_iff_not_or theorem not_eq (a b : α) : (¬ a = b) ↔ (a ≠ b) := iff.rfl variable {β : Type u} variable [linear_order β] theorem not_le_eq (a b : β) : (¬ (a ≤ b)) = (b < a) := propext not_le theorem not_lt_eq (a b : β) : (¬ (a < b)) = (b ≤ a) := propext not_lt end meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible private meta def transform_negation_step (e : expr) : tactic (option (expr × expr)) := do e ← whnf_reducible e, match e with | `(¬ %%ne) := (do ne ← whnf_reducible ne, match ne with | `(¬ %%a) := do pr ← mk_app ``not_not_eq [a], return (some (a, pr)) | `(%%a ∧ %%b) := do pr ← mk_app ``not_and_eq [a, b], return (some (`((%%a : Prop) → ¬ %%b), pr)) | `(%%a ∨ %%b) := do pr ← mk_app ``not_or_eq [a, b], return (some (`(¬ %%a ∧ ¬ %%b), pr)) | `(%%a ≤ %%b) := do e ← to_expr ``(%%b < %%a), pr ← mk_app ``not_le_eq [a, b], return (some (e, pr)) | `(%%a < %%b) := do e ← to_expr ``(%%b ≤ %%a), pr ← mk_app ``not_lt_eq [a, b], return (some (e, pr)) | `(Exists %%p) := do pr ← mk_app ``not_exists_eq [p], e ← match p with | (lam n bi typ bo) := do body ← mk_app ``not [bo], return (pi n bi typ body) | _ := tactic.fail "Unexpected failure negating ∃" end, return (some (e, pr)) | (pi n bi d p) := if p.has_var then do pr ← mk_app ``not_forall_eq [lam n bi d p], body ← mk_app ``not [p], e ← mk_app ``Exists [lam n bi d body], return (some (e, pr)) else do pr ← mk_app ``not_implies_eq [d, p], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) | _ := return none end) | _ := return none end private meta def transform_negation : expr → tactic (option (expr × expr)) | e := do (some (e', pr)) ← transform_negation_step e | return none, (some (e'', pr')) ← transform_negation e' | return (some (e', pr)), pr'' ← mk_eq_trans pr pr', return (some (e'', pr'')) meta def normalize_negations (t : expr) : tactic (expr × expr) := do (_, e, pr) ← simplify_top_down () (λ _, λ e, do oepr ← transform_negation e, match oepr with | (some (e', pr)) := return ((), e', pr) | none := do pr ← mk_eq_refl e, return ((), e, pr) end) t { eta := ff }, return (e, pr) meta def push_neg_at_hyp (h : name) : tactic unit := do H ← get_local h, t ← infer_type H, (e, pr) ← normalize_negations t, replace_hyp H e pr, skip meta def push_neg_at_goal : tactic unit := do H ← target, (e, pr) ← normalize_negations H, replace_target e pr end push_neg open interactive (parse loc.ns loc.wildcard) open interactive.types (location texpr) open lean.parser (tk ident many) interactive.loc local postfix `?`:9001 := optional local postfix *:9001 := many open push_neg /-- Push negations in the goal of some assumption. For instance, a hypothesis `h : ¬ ∀ x, ∃ y, x ≤ y` will be transformed by `push_neg at h` into `h : ∃ x, ∀ y, y < x`. Variables names are conserved. This tactic pushes negations inside expressions. For instance, given an assumption ```lean h : ¬ ∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - y₀| ≤ ε) ``` writing `push_neg at h` will turn `h` into ```lean h : ∃ ε, ε > 0 ∧ ∀ δ, δ > 0 → (∃ x, |x - x₀| ≤ δ ∧ ε < |f x - y₀|), ``` (the pretty printer does *not* use the abreviations `∀ δ > 0` and `∃ ε > 0` but this issue has nothing to do with `push_neg`). Note that names are conserved by this tactic, contrary to what would happen with `simp` using the relevant lemmas. One can also use this tactic at the goal using `push_neg`, at every assumption and the goal using `push_neg at *` or at selected assumptions and the goal using say `push_neg at h h' ⊢` as usual. -/ meta def tactic.interactive.push_neg : parse location → tactic unit | (loc.ns loc_l) := loc_l.mmap' (λ l, match l with | some h := do push_neg_at_hyp h, try $ interactive.simp_core { eta := ff } failed tt [simp_arg_type.expr ``(push_neg.not_eq)] [] (interactive.loc.ns [some h]) | none := do push_neg_at_goal, try `[simp only [push_neg.not_eq] { eta := ff }] end) | loc.wildcard := do push_neg_at_goal, local_context >>= mmap' (λ h, push_neg_at_hyp (local_pp_name h)) , try `[simp only [push_neg.not_eq] at * { eta := ff }] add_tactic_doc { name := "push_neg", category := doc_category.tactic, decl_names := [`tactic.interactive.push_neg], tags := ["logic"] } lemma imp_of_not_imp_not (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := λ h hP, classical.by_contradiction (λ h', h h' hP) /-- Matches either an identifier "h" or a pair of identifiers "h with k" -/ meta def name_with_opt : lean.parser (name × option name) := prod.mk <$> ident <*> (some <$> (tk "with" >> ident) <|> return none) /-- Transforms the goal into its contrapositive. * `contrapose` turns a goal `P → Q` into `¬ Q → ¬ P` * `contrapose!` turns a goal `P → Q` into `¬ Q → ¬ P` and pushes negations inside `P` and `Q` using `push_neg` * `contrapose h` first reverts the local assumption `h`, and then uses `contrapose` and `intro h` * `contrapose! h` first reverts the local assumption `h`, and then uses `contrapose!` and `intro h` * `contrapose h with new_h` uses the name `new_h` for the introduced hypothesis -/ meta def tactic.interactive.contrapose (push : parse (tk "!" )?) : parse name_with_opt? → tactic unit | (some (h, h')) := get_local h >>= revert >> tactic.interactive.contrapose none >> intro (h'.get_or_else h) >> skip | none := do `(%%P → %%Q) ← target | fail "The goal is not an implication, and you didn't specify an assumption", cp ← mk_mapp ``imp_of_not_imp_not [P, Q] <|> fail "contrapose only applies to nondependent arrows between props", apply cp, when push.is_some $ try (tactic.interactive.push_neg (loc.ns [none])) add_tactic_doc { name := "contrapose", category := doc_category.tactic, decl_names := [`tactic.interactive.contrapose], tags := ["logic"] }