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
b774dc4eafcc593cd5e5f1875a323534ba83bc43
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/geometry/manifold/smooth_manifold_with_corners.lean
365b9662fc57c13380a7529672fde7659cd78ad0
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
32,916
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.times_cont_diff import geometry.manifold.charted_space /-! # Smooth manifolds (possibly with boundary or corners) A smooth manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a specific type class for `C^∞` manifolds as these are the most commonly used. ## Main definitions * `model_with_corners 𝕜 E H` : a structure containing informations on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a smooth manifold with model space `H`, and model vector space `E`. * `model_with_corners_self 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `times_cont_diff_groupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `smooth_manifold_with_corners I M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`. * `ext_chart_at I x`: in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`, but often we may want to use their `E`-valued version, obtained by composing the charts with `I`. Since the target is in general not open, we can not register them as local homeomorphisms, but we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`. As specific examples of models with corners, we define (in the file `real_instances.lean`) * `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary, one could use `variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M] [smooth_manifold_with_corners (𝓡 n) M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(euclidean_space (fin n)) × (euclidean_space (fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {I : model_with_corners ℝ E E} [I.boundaryless] {M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]` Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `model_with_corners_self`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`. We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that one could revisit later if needed. `C^k` manifolds are still available, but they should be called using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners. I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ noncomputable theory universes u v w u' v' w' open set localized "notation `∞` := (⊤ : with_top ℕ)" in manifold section model_with_corners /-! ### Models with corners. -/ /-- A structure containing informations on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a smooth manifold with model space `H`, and model vector space `E`. -/ @[nolint has_inhabited_instance] structure model_with_corners (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (H : Type*) [topological_space H] extends local_equiv H E := (source_eq : source = univ) (unique_diff' : unique_diff_on 𝕜 (range to_fun)) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') attribute [simp, mfld_simps] model_with_corners.source_eq /-- A vector space is a model with corners. -/ def model_with_corners_self (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E := { to_fun := id, inv_fun := id, source := univ, target := univ, source_eq := rfl, map_source' := λ_ _, mem_univ _, map_target' := λ_ _, mem_univ _, left_inv' := λ_ _, rfl, right_inv' := λ_ _, rfl, unique_diff' := by { rw range_id, exact unique_diff_on_univ }, continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id } section variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) instance : has_coe_to_fun (model_with_corners 𝕜 E H) := ⟨_, λ e, e.to_fun⟩ /-- The inverse to a model with corners, only registered as a local equiv. -/ protected def model_with_corners.symm : local_equiv E H := I.to_local_equiv.symm /- Register a few lemmas to make sure that `simp` puts expressions in normal form -/ @[simp, mfld_simps] lemma model_with_corners.to_local_equiv_coe : (I.to_local_equiv : H → E) = I := rfl @[simp, mfld_simps] lemma model_with_corners.mk_coe (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H) : H → E) = (e : H → E) := rfl @[simp, mfld_simps] lemma model_with_corners.to_local_equiv_coe_symm : (I.to_local_equiv.symm : E → H) = I.symm := rfl @[simp, mfld_simps] lemma model_with_corners.mk_coe_symm (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H).symm : E → H) = (e.symm : E → H) := rfl lemma model_with_corners.unique_diff : unique_diff_on 𝕜 (range I) := I.unique_diff' protected lemma model_with_corners.continuous : continuous I := I.continuous_to_fun lemma model_with_corners.continuous_symm : continuous I.symm := I.continuous_inv_fun section variables (𝕜 E) /-- In the trivial model with corners, the associated local equiv is the identity. -/ @[simp, mfld_simps] lemma model_with_corners_self_local_equiv : (model_with_corners_self 𝕜 E).to_local_equiv = local_equiv.refl E := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe : (model_with_corners_self 𝕜 E : E → E) = id := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe_symm : ((model_with_corners_self 𝕜 E).symm : E → E) = id := rfl end @[simp, mfld_simps] lemma model_with_corners.target : I.target = range (I : H → E) := by { rw [← image_univ, ← I.source_eq], exact (I.to_local_equiv.image_source_eq_target).symm } @[simp, mfld_simps] lemma model_with_corners.left_inv (x : H) : I.symm (I x) = x := by { convert I.left_inv' _, simp } @[simp, mfld_simps] lemma model_with_corners.left_inv' : I.symm ∘ I = id := by { ext x, exact model_with_corners.left_inv _ _ } @[simp, mfld_simps] lemma model_with_corners.right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x := by { apply I.right_inv', simp [hx] } lemma model_with_corners.image (s : set H) : I '' s = I.symm ⁻¹' s ∩ range I := begin ext x, simp only [mem_image, mem_inter_eq, mem_range, mem_preimage], split, { rintros ⟨y, ⟨ys, hy⟩⟩, rw ← hy, simp only [ys, true_and, model_with_corners.left_inv], exact ⟨y, rfl⟩ }, { rintros ⟨xs, ⟨y, yx⟩⟩, rw ← yx at xs, simp only [model_with_corners.left_inv] at xs, exact ⟨y, ⟨xs, yx⟩⟩ } end lemma model_with_corners.unique_diff_preimage {s : set H} (hs : is_open s) : unique_diff_on 𝕜 (I.symm ⁻¹' s ∩ range I) := by { rw inter_comm, exact I.unique_diff.inter (I.continuous_inv_fun _ hs) } lemma model_with_corners.unique_diff_preimage_source {β : Type*} [topological_space β] {e : local_homeomorph H β} : unique_diff_on 𝕜 (I.symm ⁻¹' (e.source) ∩ range I) := I.unique_diff_preimage e.open_source lemma model_with_corners.unique_diff_at_image {x : H} : unique_diff_within_at 𝕜 (range I) (I x) := I.unique_diff _ (mem_range_self _) end section model_with_corners_prod /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', H × H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. -/ def model_with_corners.prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') := { to_fun := λ p, (I p.1, I' p.2), inv_fun := λ p, (I.symm p.1, I'.symm p.2), source := (univ : set (H × H')), target := set.prod (range I) (range I'), map_source' := λ ⟨x, x'⟩ _, by simp [-mem_range, mem_range_self], map_target' := λ ⟨x, x'⟩ _, mem_univ _, left_inv' := λ ⟨x, x'⟩ _, by simp, right_inv' := λ ⟨x, x'⟩ ⟨hx, hx'⟩, by simp [hx, hx'], source_eq := rfl, unique_diff' := begin have : range (λ(p : model_prod H H'), (I p.1, I' p.2)) = set.prod (range I) (range I'), by { dsimp [model_prod], rw ← prod_range_range_eq }, rw this, exact unique_diff_on.prod I.unique_diff I'.unique_diff, end, continuous_to_fun := (continuous.comp I.continuous_to_fun continuous_fst).prod_mk (continuous.comp I'.continuous_to_fun continuous_snd), continuous_inv_fun := (continuous.comp I.continuous_inv_fun continuous_fst).prod_mk (continuous.comp I'.continuous_inv_fun continuous_snd) } /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ @[reducible] def model_with_corners.tangent {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) := I.prod (model_with_corners_self 𝕜 E) variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {G' : Type*} [topological_space G'] {I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G} @[simp, mfld_simps] lemma model_with_corners_prod_to_local_equiv : (I.prod J).to_local_equiv = (I.to_local_equiv).prod (J.to_local_equiv) := begin ext1 x, { refl, }, { intro x, refl, }, { simp only [set.univ_prod_univ, model_with_corners.source_eq, local_equiv.prod_source], } end @[simp, mfld_simps] lemma model_with_corners_prod_coe (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : (I.prod I' : _ × _ → _ × _) = prod.map I I' := rfl @[simp, mfld_simps] lemma model_with_corners_prod_coe_symm (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ((I.prod I').symm : _ × _ → _ × _) = prod.map I.symm I'.symm := rfl end model_with_corners_prod section boundaryless /-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/ class model_with_corners.boundaryless {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) : Prop := (range_eq_univ : range I = univ) /-- The trivial model with corners has no boundary -/ instance model_with_corners_self_boundaryless (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : (model_with_corners_self 𝕜 E).boundaryless := ⟨by simp⟩ /-- If two model with corners are boundaryless, their product also is -/ instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) [I.boundaryless] {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') [I'.boundaryless] : (I.prod I').boundaryless := begin split, dsimp [model_with_corners.prod, model_prod], rw [← prod_range_range_eq, model_with_corners.boundaryless.range_eq_univ, model_with_corners.boundaryless.range_eq_univ, univ_prod_univ] end end boundaryless section times_cont_diff_groupoid /-! ### Smooth functions on models with corners -/ variables {m n : with_top ℕ} {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] variable (n) /-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def times_cont_diff_groupoid : structure_groupoid H := pregroupoid.groupoid { property := λf s, times_cont_diff_on 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I), comp := λf g u v hf hg hu hv huv, begin have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ (I ∘ f ∘ I.symm), by { ext x, simp }, rw this, apply times_cont_diff_on.comp hg _, { rintros x ⟨hx1, hx2⟩, simp only with mfld_simps at ⊢ hx1, exact hx1.2 }, { refine hf.mono _, rintros x ⟨hx1, hx2⟩, exact ⟨hx1.1, hx2⟩ } end, id_mem := begin apply times_cont_diff_on.congr (times_cont_diff_id.times_cont_diff_on), rintros x ⟨hx1, hx2⟩, rcases mem_range.1 hx2 with ⟨y, hy⟩, rw ← hy, simp only with mfld_simps, end, locality := λf u hu H, begin apply times_cont_diff_on_of_locally_times_cont_diff_on, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rcases H x hy1 with ⟨v, v_open, xv, hv⟩, have : ((I.symm ⁻¹' (u ∩ v)) ∩ (range I)) = ((I.symm ⁻¹' u) ∩ (range I) ∩ I.symm ⁻¹' v), { rw [preimage_inter, inter_assoc, inter_assoc], congr' 1, rw inter_comm }, rw this at hv, exact ⟨I.symm ⁻¹' v, I.continuous_symm _ v_open, by simpa, hv⟩ end, congr := λf g u hu fg hf, begin apply hf.congr, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rw fg _ hy1 end } variable {n} /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ lemma times_cont_diff_groupoid_le (h : m ≤ n) : times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I := begin rw [times_cont_diff_groupoid, times_cont_diff_groupoid], apply groupoid_of_pregroupoid_le, assume f s hfs, exact times_cont_diff_on.of_le hfs h end /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all local homeomorphisms -/ lemma times_cont_diff_groupoid_zero_eq : times_cont_diff_groupoid 0 I = continuous_groupoid H := begin apply le_antisymm le_top, assume u hu, -- we have to check that every local homeomorphism belongs to `times_cont_diff_groupoid 0 I`, -- by unfolding its definition change u ∈ times_cont_diff_groupoid 0 I, rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], simp only [times_cont_diff_on_zero], split, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_to_fun I.continuous_symm.continuous_on (inter_subset_left _ _) }, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_inv_fun I.continuous_inv_fun.continuous_on (inter_subset_left _ _) }, end variable (n) /-- An identity local homeomorphism belongs to the `C^n` groupoid. -/ lemma of_set_mem_times_cont_diff_groupoid {s : set H} (hs : is_open s) : local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I := begin rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], suffices h : times_cont_diff_on 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I), by simp [h], have : times_cont_diff_on 𝕜 n id (univ : set E) := times_cont_diff_id.times_cont_diff_on, exact this.congr_mono (λ x hx, by simp [hx.2]) (subset_univ _) end /-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ lemma symm_trans_mem_times_cont_diff_groupoid (e : local_homeomorph M H) : e.symm.trans e ∈ times_cont_diff_groupoid n I := begin have : e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target := local_homeomorph.trans_symm_self _, exact structure_groupoid.eq_on_source _ (of_set_mem_times_cont_diff_groupoid n I e.open_target) this end variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] /-- The product of two smooth local homeomorphisms is smooth. -/ lemma times_cont_diff_groupoid_prod {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {e : local_homeomorph H H} {e' : local_homeomorph H' H'} (he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') : e.prod e' ∈ times_cont_diff_groupoid ⊤ (I.prod I') := begin cases he with he he_symm, cases he' with he' he'_symm, simp only at he he_symm he' he'_symm, split; simp only [local_equiv.prod_source, local_homeomorph.prod_to_local_equiv], { have h3 := times_cont_diff_on.prod_map he he', rw [← model_with_corners.image I _, ← model_with_corners.image I' _, set.prod_image_image_eq] at h3, rw ← model_with_corners.image (I.prod I') _, exact h3, }, { have h3 := times_cont_diff_on.prod_map he_symm he'_symm, rw [← model_with_corners.image I _, ← model_with_corners.image I' _, set.prod_image_image_eq] at h3, rw ← model_with_corners.image (I.prod I') _, exact h3, } end /-- The `C^n` groupoid is closed under restriction. -/ instance : closed_under_restriction (times_cont_diff_groupoid n I) := (closed_under_restriction_iff_id_le _).mpr begin apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, apply (times_cont_diff_groupoid n I).eq_on_source' _ _ _ hes, exact of_set_mem_times_cont_diff_groupoid n I hs, end end times_cont_diff_groupoid end model_with_corners /-! ### Smooth manifolds with corners -/ /-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/ class smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] extends has_groupoid M (times_cont_diff_groupoid ∞ I) : Prop /-- For any model with corners, the model space is a smooth manifold -/ instance model_space_smooth {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} : smooth_manifold_with_corners I H := {} namespace smooth_manifold_with_corners /- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `times_cont_diff_groupoid ∞ I` explicitly. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] /-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the model with corners `I`. -/ def maximal_atlas := (times_cont_diff_groupoid ∞ I).maximal_atlas M variable {M} lemma compatible [smooth_manifold_with_corners I M] {e e' : local_homeomorph M H} (he : e ∈ atlas H M) (he' : e' ∈ atlas H M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ he he' lemma mem_maximal_atlas_of_mem_atlas [smooth_manifold_with_corners I M] {e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ maximal_atlas I M := structure_groupoid.mem_maximal_atlas_of_mem_atlas _ he lemma chart_mem_maximal_atlas [smooth_manifold_with_corners I M] (x : M) : chart_at H x ∈ maximal_atlas I M := structure_groupoid.chart_mem_maximal_atlas _ x variable {I} lemma compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := structure_groupoid.compatible_of_mem_maximal_atlas he he' /-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/ instance prod_smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} (M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] : smooth_manifold_with_corners (I.prod I') (M×M') := { compatible := begin rintros f g ⟨f1, hf1, f2, hf2, hf⟩ ⟨g1, hg1, g2, hg2, hg⟩, rw [hf, hg, local_homeomorph.prod_symm, local_homeomorph.prod_trans], have h1 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) hf1 hg1, have h2 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I') hf2 hg2, exact times_cont_diff_groupoid_prod h1 h2, end } end smooth_manifold_with_corners section extended_charts open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] (x : M) {s t : set M} /-! ### Extended charts In a smooth manifold with corners, the model space is the space `H`. However, we will also need to use extended charts taking values in the model vector space `E`. These extended charts are not `local_homeomorph` as the target is not open in `E` in general, but we can still register them as `local_equiv`. -/ /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp, mfld_simps] def ext_chart_at (x : M) : local_equiv M E := (chart_at H x).to_local_equiv.trans I.to_local_equiv lemma ext_chart_at_source : (ext_chart_at I x).source = (chart_at H x).source := by rw [ext_chart_at, local_equiv.trans_source, I.source_eq, preimage_univ, inter_univ] lemma ext_chart_at_open_source : is_open (ext_chart_at I x).source := by { rw ext_chart_at_source, exact (chart_at H x).open_source } lemma mem_ext_chart_source : x ∈ (ext_chart_at I x).source := by simp only with mfld_simps lemma ext_chart_at_to_inv : (ext_chart_at I x).symm ((ext_chart_at I x) x) = x := by simp only with mfld_simps lemma ext_chart_at_source_mem_nhds : (ext_chart_at I x).source ∈ 𝓝 x := mem_nhds_sets (ext_chart_at_open_source I x) (mem_ext_chart_source I x) lemma ext_chart_at_continuous_on : continuous_on (ext_chart_at I x) (ext_chart_at I x).source := begin refine continuous_on.comp I.continuous.continuous_on _ subset_preimage_univ, rw ext_chart_at_source, exact (chart_at H x).continuous_on end lemma ext_chart_at_continuous_at : continuous_at (ext_chart_at I x) x := (ext_chart_at_continuous_on I x x (mem_ext_chart_source I x)).continuous_at (ext_chart_at_source_mem_nhds I x) lemma ext_chart_at_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := begin apply continuous_on.comp (chart_at H x).continuous_on_symm I.continuous_symm.continuous_on, simp [ext_chart_at, local_equiv.trans_target] end lemma ext_chart_at_target_mem_nhds_within : (ext_chart_at I x).target ∈ 𝓝[range I] ((ext_chart_at I x) x) := begin rw [ext_chart_at, local_equiv.trans_target], simp only [function.comp_app, local_equiv.coe_trans, model_with_corners.target], refine inter_mem_nhds_within _ (mem_nhds_sets (I.continuous_symm _ (chart_at H x).open_target) _), simp only with mfld_simps end lemma ext_chart_at_coe (p : M) : (ext_chart_at I x) p = I ((chart_at H x : M → H) p) := rfl lemma ext_chart_at_coe_symm (p : E) : (ext_chart_at I x).symm p = ((chart_at H x).symm : H → M) (I.symm p) := rfl lemma nhds_within_ext_chart_target_eq : 𝓝[(ext_chart_at I x).target] ((ext_chart_at I x) x) = 𝓝[range I] ((ext_chart_at I x) x) := begin apply le_antisymm, { apply nhds_within_mono, simp only with mfld_simps}, { apply nhds_within_le_of_mem (ext_chart_at_target_mem_nhds_within _ _) } end lemma ext_chart_continuous_at_symm' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x') := begin apply continuous_at.comp, { rw ext_chart_at_source at h, simp only with mfld_simps, exact ((chart_at H x).continuous_on_symm _ ((chart_at H x).map_source h)).continuous_at (mem_nhds_sets (chart_at H x).open_target ((chart_at H x).map_source h)) }, { exact I.continuous_symm.continuous_at } end lemma ext_chart_continuous_at_symm : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x) := ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x) /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) (ht : t ∈ 𝓝[s] x') : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') := begin apply (ext_chart_continuous_at_symm' I x h).continuous_within_at.tendsto_nhds_within_image, rw (ext_chart_at I x).left_inv h, apply nhds_within_mono _ _ ht, have : (ext_chart_at I x).symm '' ((ext_chart_at I x).symm ⁻¹' s) ⊆ s := image_preimage_subset _ _, exact subset.trans (image_subset _ (inter_subset_left _ _)) this end /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within (ht : t ∈ 𝓝[s] x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ lemma ext_chart_preimage_mem_nhds (ht : t ∈ 𝓝 x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝 ((ext_chart_at I x) x) := begin apply (ext_chart_continuous_at_symm I x).preimage_mem_nhds, rwa (ext_chart_at I x).left_inv (mem_ext_chart_source _ _) end /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ lemma ext_chart_preimage_inter_eq : ((ext_chart_at I x).symm ⁻¹' (s ∩ t) ∩ range I) = ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ∩ ((ext_chart_at I x).symm ⁻¹' t) := by mfld_set_tac end extended_charts /-- In the case of the manifold structure on a vector space, the extended charts are just the identity.-/ lemma ext_chart_model_space_eq_id (𝕜 : Type*) [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (x : E) : ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E := by simp only with mfld_simps
865a6426b28c4e4c7fe404fae8c6ca4d931e92c7
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/data/opposite.lean
22324fa71a400183ee4f8f4729bb986008f704d3
[ "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
3,280
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton, Simon Hudon, Kenny Lau Opposites. -/ import data.list.defs namespace opposite universes v u -- declare the `v` first; see `category_theory.category` for an explanation variable (α : Sort u) /-- The type of objects of the opposite of `α`; used to defined opposite category/group/... In order to avoid confusion between `α` and its opposite type, we set up the type of objects `opposite α` using the following pattern, which will be repeated later for the morphisms. 1. Define `opposite α := α`. 2. Define the isomorphisms `op : α → opposite α`, `unop : opposite α → α`. 3. Make the definition `opposite` irreducible. This has the following consequences. * `opposite α` and `α` are distinct types in the elaborator, so you must use `op` and `unop` explicitly to convert between them. * Both `unop (op X) = X` and `op (unop X) = X` are definitional equalities. Notably, every object of the opposite category is definitionally of the form `op X`, which greatly simplifies the definition of the structure of the opposite category, for example. (If Lean supported definitional eta equality for records, we could achieve the same goals using a structure with one field.) -/ def opposite : Sort u := α -- Use a high right binding power (like that of postfix ⁻¹) so that, for example, -- `presheaf Cᵒᵖ` parses as `presheaf (Cᵒᵖ)` and not `(presheaf C)ᵒᵖ`. notation α `ᵒᵖ`:std.prec.max_plus := opposite α variables {α} def op : α → αᵒᵖ := id def unop : αᵒᵖ → α := id lemma op_inj : function.injective (op : α → αᵒᵖ) := λ _ _, id lemma unop_inj : function.injective (unop : αᵒᵖ → α) := λ _ _, id @[simp] lemma op_inj_iff (x y : α) : op x = op y ↔ x = y := iff.refl _ @[simp] lemma unop_inj_iff (x y : αᵒᵖ) : unop x = unop y ↔ x = y := iff.refl _ @[simp] lemma op_unop (x : αᵒᵖ) : op (unop x) = x := rfl @[simp] lemma unop_op (x : α) : unop (op x) = x := rfl attribute [irreducible] opposite def op_induction {F : Π (X : αᵒᵖ), Sort v} (h : Π X, F (op X)) : Π X, F X := λ X, h (unop X) end opposite namespace tactic open opposite open interactive interactive.types lean.parser tactic local postfix `?`:9001 := optional namespace op_induction meta def is_opposite (e : expr) : tactic bool := do t ← infer_type e, `(opposite _) ← whnf t | return ff, return tt meta def find_opposite_hyp : tactic name := do lc ← local_context, h :: _ ← lc.mfilter $ is_opposite | fail "No hypotheses of the form Xᵒᵖ", return h.local_pp_name end op_induction open op_induction meta def op_induction (h : option name) : tactic unit := do h ← match h with | (some h) := pure h | none := find_opposite_hyp end, h' ← tactic.get_local h, revert_lst [h'], applyc `opposite.op_induction, tactic.intro h, skip -- For use with `local attribute [tidy] op_induction` meta def op_induction' := op_induction none namespace interactive meta def op_induction (h : parse ident?) : tactic unit := tactic.op_induction h end interactive end tactic
f6d67c23b5a89868e05ee343bc2272e0ce0ac5bc
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/limits/creates.lean
36f04d0ad70f685bbaada84974d5534ea3b32d69
[ "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
14,776
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.preserves open category_theory category_theory.limits namespace category_theory universes v u₁ u₂ u₃ variables {C : Type u₁} [category.{v} C] section creates variables {D : Type u₂} [category.{v} D] variables {J : Type v} [small_category J] {K : J ⥤ C} /-- Define the lift of a cone: For a cone `c` for `K ⋙ F`, give a cone for `K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`. We will then use this as part of the definition of creation of limits: every limit cone has a lift. Note this definition is really only useful when `c` is a limit already. -/ structure liftable_cone (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) := (lifted_cone : cone K) (valid_lift : F.map_cone lifted_cone ≅ c) /-- Define the lift of a cocone: For a cocone `c` for `K ⋙ F`, give a cocone for `K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`. We will then use this as part of the definition of creation of colimits: every limit cocone has a lift. Note this definition is really only useful when `c` is a colimit already. -/ structure liftable_cocone (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) := (lifted_cocone : cocone K) (valid_lift : F.map_cocone lifted_cocone ≅ c) set_option default_priority 100 /-- Definition 3.3.1 of [Riehl]. We say that `F` creates limits of `K` if, given any limit cone `c` for `K ⋙ F` (i.e. below) we can lift it to a cone "above", and further that `F` reflects limits for `K`. If `F` reflects isomorphisms, it suffices to show only that the lifted cone is a limit - see `creates_limit_of_reflects_iso`. -/ class creates_limit (K : J ⥤ C) (F : C ⥤ D) extends reflects_limit K F := (lifts : Π c, is_limit c → liftable_cone K F c) /-- `F` creates limits of shape `J` if `F` creates the limit of any diagram `K : J ⥤ C`. -/ class creates_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) := (creates_limit : Π {K : J ⥤ C}, creates_limit K F) /-- `F` creates limits if it creates limits of shape `J` for any small `J`. -/ class creates_limits (F : C ⥤ D) := (creates_limits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI creates_limits_of_shape J F) /-- Dual of definition 3.3.1 of [Riehl]. We say that `F` creates colimits of `K` if, given any limit cocone `c` for `K ⋙ F` (i.e. below) we can lift it to a cocone "above", and further that `F` reflects limits for `K`. If `F` reflects isomorphisms, it suffices to show only that the lifted cocone is a limit - see `creates_limit_of_reflects_iso`. -/ class creates_colimit (K : J ⥤ C) (F : C ⥤ D) extends reflects_colimit K F := (lifts : Π c, is_colimit c → liftable_cocone K F c) /-- `F` creates colimits of shape `J` if `F` creates the colimit of any diagram `K : J ⥤ C`. -/ class creates_colimits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) := (creates_colimit : Π {K : J ⥤ C}, creates_colimit K F) /-- `F` creates colimits if it creates colimits of shape `J` for any small `J`. -/ class creates_colimits (F : C ⥤ D) := (creates_colimits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI creates_colimits_of_shape J F) attribute [instance, priority 100] -- see Note [lower instance priority] creates_limits_of_shape.creates_limit creates_limits.creates_limits_of_shape creates_colimits_of_shape.creates_colimit creates_colimits.creates_colimits_of_shape /- Interface to the `creates_limit` class. -/ /-- `lift_limit t` is the cone for `K` given by lifting the limit `t` for `K ⋙ F`. -/ def lift_limit {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : cone K := (creates_limit.lifts c t).lifted_cone /-- The lifted cone has an image isomorphic to the original cone. -/ def lifted_limit_maps_to_original {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : F.map_cone (lift_limit t) ≅ c := (creates_limit.lifts c t).valid_lift /-- The lifted cone is a limit. -/ def lifted_limit_is_limit {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : is_limit (lift_limit t) := reflects_limit.reflects (is_limit.of_iso_limit t (lifted_limit_maps_to_original t).symm) /-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/ def has_limit_of_created (K : J ⥤ C) (F : C ⥤ D) [has_limit (K ⋙ F)] [creates_limit K F] : has_limit K := { cone := lift_limit (limit.is_limit (K ⋙ F)), is_limit := lifted_limit_is_limit _ } /- Interface to the `creates_colimit` class. -/ /-- `lift_colimit t` is the cocone for `K` given by lifting the colimit `t` for `K ⋙ F`. -/ def lift_colimit {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : cocone K := (creates_colimit.lifts c t).lifted_cocone /-- The lifted cocone has an image isomorphic to the original cocone. -/ def lifted_colimit_maps_to_original {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : F.map_cocone (lift_colimit t) ≅ c := (creates_colimit.lifts c t).valid_lift /-- The lifted cocone is a colimit. -/ def lifted_colimit_is_colimit {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : is_colimit (lift_colimit t) := reflects_colimit.reflects (is_colimit.of_iso_colimit t (lifted_colimit_maps_to_original t).symm) /-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/ def has_colimit_of_created (K : J ⥤ C) (F : C ⥤ D) [has_colimit (K ⋙ F)] [creates_colimit K F] : has_colimit K := { cocone := lift_colimit (colimit.is_colimit (K ⋙ F)), is_colimit := lifted_colimit_is_colimit _ } /-- A helper to show a functor creates limits. In particular, if we can show that for any limit cone `c` for `K ⋙ F`, there is a lift of it which is a limit and `F` reflects isomorphisms, then `F` creates limits. Usually, `F` creating limits says that _any_ lift of `c` is a limit, but here we only need to show that our particular lift of `c` is a limit. -/ structure lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) (t : is_limit c) extends liftable_cone K F c := (makes_limit : is_limit lifted_cone) /-- A helper to show a functor creates colimits. In particular, if we can show that for any limit cocone `c` for `K ⋙ F`, there is a lift of it which is a limit and `F` reflects isomorphisms, then `F` creates colimits. Usually, `F` creating colimits says that _any_ lift of `c` is a colimit, but here we only need to show that our particular lift of `c` is a colimit. -/ structure lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) (t : is_colimit c) extends liftable_cocone K F c := (makes_colimit : is_colimit lifted_cocone) /-- If `F` reflects isomorphisms and we can lift any limit cone to a limit cone, then `F` creates limits. In particular here we don't need to assume that F reflects limits. -/ def creates_limit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : Π c t, lifts_to_limit K F c t) : creates_limit K F := { lifts := λ c t, (h c t).to_liftable_cone, to_reflects_limit := { reflects := λ (d : cone K) (hd : is_limit (F.map_cone d)), begin let d' : cone K := (h (F.map_cone d) hd).to_liftable_cone.lifted_cone, let i : F.map_cone d' ≅ F.map_cone d := (h (F.map_cone d) hd).to_liftable_cone.valid_lift, let hd' : is_limit d' := (h (F.map_cone d) hd).makes_limit, let f : d ⟶ d' := hd'.lift_cone_morphism d, have : (cones.functoriality K F).map f = i.inv := (hd.of_iso_limit i.symm).uniq_cone_morphism, haveI : is_iso ((cones.functoriality K F).map f) := (by { rw this, apply_instance }), haveI : is_iso f := is_iso_of_reflects_iso f (cones.functoriality K F), exact is_limit.of_iso_limit hd' (as_iso f).symm, end } } /-- `F` preserves the limit of `K` if it creates the limit and `K ⋙ F` has the limit. -/ instance preserves_limit_of_creates_limit_and_has_limit (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] [has_limit (K ⋙ F)] : preserves_limit K F := { preserves := λ c t, is_limit.of_iso_limit (limit.is_limit _) ((lifted_limit_maps_to_original (limit.is_limit _)).symm ≪≫ ((cones.functoriality K F).map_iso ((lifted_limit_is_limit (limit.is_limit _)).unique_up_to_iso t))) } /-- `F` preserves the limit of shape `J` if it creates these limits and `D` has them. -/ instance preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape (F : C ⥤ D) [creates_limits_of_shape J F] [has_limits_of_shape J D] : preserves_limits_of_shape J F := { preserves_limit := λ K, category_theory.preserves_limit_of_creates_limit_and_has_limit K F } /-- `F` preserves limits if it creates limits and `D` has limits. -/ instance preserves_limits_of_creates_limits_and_has_limits (F : C ⥤ D) [creates_limits F] [has_limits D] : preserves_limits F := { preserves_limits_of_shape := λ J 𝒥, by exactI category_theory.preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape F } /-- If `F` reflects isomorphisms and we can lift any limit cocone to a limit cocone, then `F` creates colimits. In particular here we don't need to assume that F reflects colimits. -/ def creates_colimit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : Π c t, lifts_to_colimit K F c t) : creates_colimit K F := { lifts := λ c t, (h c t).to_liftable_cocone, to_reflects_colimit := { reflects := λ (d : cocone K) (hd : is_colimit (F.map_cocone d)), begin let d' : cocone K := (h (F.map_cocone d) hd).to_liftable_cocone.lifted_cocone, let i : F.map_cocone d' ≅ F.map_cocone d := (h (F.map_cocone d) hd).to_liftable_cocone.valid_lift, let hd' : is_colimit d' := (h (F.map_cocone d) hd).makes_colimit, let f : d' ⟶ d := hd'.desc_cocone_morphism d, have : (cocones.functoriality K F).map f = i.hom := (hd.of_iso_colimit i.symm).uniq_cocone_morphism, haveI : is_iso ((cocones.functoriality K F).map f) := (by { rw this, apply_instance }), haveI := is_iso_of_reflects_iso f (cocones.functoriality K F), exact is_colimit.of_iso_colimit hd' (as_iso f), end } } /-- `F` preserves the colimit of `K` if it creates the colimit and `K ⋙ F` has the colimit. -/ instance preserves_colimit_of_creates_colimit_and_has_colimit (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] [has_colimit (K ⋙ F)] : preserves_colimit K F := { preserves := λ c t, is_colimit.of_iso_colimit (colimit.is_colimit _) ((lifted_colimit_maps_to_original (colimit.is_colimit _)).symm ≪≫ ((cocones.functoriality K F).map_iso ((lifted_colimit_is_colimit (colimit.is_colimit _)).unique_up_to_iso t))) } /-- `F` preserves the colimit of shape `J` if it creates these colimits and `D` has them. -/ instance preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape (F : C ⥤ D) [creates_colimits_of_shape J F] [has_colimits_of_shape J D] : preserves_colimits_of_shape J F := { preserves_colimit := λ K, category_theory.preserves_colimit_of_creates_colimit_and_has_colimit K F } /-- `F` preserves limits if it creates limits and `D` has limits. -/ instance preserves_colimits_of_creates_colimits_and_has_colimits (F : C ⥤ D) [creates_colimits F] [has_colimits D] : preserves_colimits F := { preserves_colimits_of_shape := λ J 𝒥, by exactI category_theory.preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape F } -- For the inhabited linter later. /-- If F creates the limit of K, any cone lifts to a limit. -/ def lifts_to_limit_of_creates (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) : lifts_to_limit K F c t := { lifted_cone := lift_limit t, valid_lift := lifted_limit_maps_to_original t, makes_limit := lifted_limit_is_limit t } -- For the inhabited linter later. /-- If F creates the colimit of K, any cocone lifts to a colimit. -/ def lifts_to_colimit_of_creates (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) : lifts_to_colimit K F c t := { lifted_cocone := lift_colimit t, valid_lift := lifted_colimit_maps_to_original t, makes_colimit := lifted_colimit_is_colimit t } /-- Any cone lifts through the identity functor. -/ def id_lifts_cone (c : cone (K ⋙ 𝟭 C)) : liftable_cone K (𝟭 C) c := { lifted_cone := { X := c.X, π := c.π ≫ K.right_unitor.hom }, valid_lift := cones.ext (iso.refl _) (by tidy) } /-- The identity functor creates all limits. -/ instance id_creates_limits : creates_limits (𝟭 C) := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ F, { lifts := λ c t, id_lifts_cone c } } } /-- Any cocone lifts through the identity functor. -/ def id_lifts_cocone (c : cocone (K ⋙ 𝟭 C)) : liftable_cocone K (𝟭 C) c := { lifted_cocone := { X := c.X, ι := K.right_unitor.inv ≫ c.ι }, valid_lift := cocones.ext (iso.refl _) (by tidy) } /-- The identity functor creates all colimits. -/ instance id_creates_colimits : creates_colimits (𝟭 C) := { creates_colimits_of_shape := λ J 𝒥, by exactI { creates_colimit := λ F, { lifts := λ c t, id_lifts_cocone c } } } /-- Satisfy the inhabited linter -/ instance inhabited_liftable_cone (c : cone (K ⋙ 𝟭 C)) : inhabited (liftable_cone K (𝟭 C) c) := ⟨id_lifts_cone c⟩ instance inhabited_liftable_cocone (c : cocone (K ⋙ 𝟭 C)) : inhabited (liftable_cocone K (𝟭 C) c) := ⟨id_lifts_cocone c⟩ /-- Satisfy the inhabited linter -/ instance inhabited_lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) : inhabited (lifts_to_limit _ _ _ t) := ⟨lifts_to_limit_of_creates K F c t⟩ instance inhabited_lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) : inhabited (lifts_to_colimit _ _ _ t) := ⟨lifts_to_colimit_of_creates K F c t⟩ section comp variables {E : Type u₃} [ℰ : category.{v} E] variables (F : C ⥤ D) (G : D ⥤ E) instance comp_creates_limit [i₁ : creates_limit K F] [i₂ : creates_limit (K ⋙ F) G] : creates_limit K (F ⋙ G) := { lifts := λ c t, { lifted_cone := lift_limit (lifted_limit_is_limit t), valid_lift := (cones.functoriality (K ⋙ F) G).map_iso (lifted_limit_maps_to_original (lifted_limit_is_limit t)) ≪≫ (lifted_limit_maps_to_original t), } } end comp end creates end category_theory
06083fd13af6045f7ba3d5f48d073bf5a04f1250
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/elab3.lean
7363cc0a572337838d6af6d3b0e366325d0ef258
[ "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
90
lean
set_option pp.binder_types true axiom Sorry {A : Sort*} : A #check (Sorry : ∀ a, a > 0)
1796d66d83244781ec071a547d2b9c54791717d2
fcf3ffa92a3847189ca669cb18b34ef6b2ec2859
/src/world8/level10.lean
f0e49edc44d5f76e6377b814b71bb111b4757196
[ "Apache-2.0" ]
permissive
nomoid/lean-proofs
4a80a97888699dee42b092b7b959b22d9aa0c066
b9f03a24623d1a1d111d6c2bbf53c617e2596d6a
refs/heads/master
1,674,955,317,080
1,607,475,706,000
1,607,475,706,000
314,104,281
0
0
null
null
null
null
UTF-8
Lean
false
false
359
lean
import mynat.definition import mynat.add import world8.level6 import world8.level9 namespace mynat theorem add_left_eq_zero (a b : mynat) (h : a + b = 0) : b = 0 := begin [nat_num_game] cases b with d, { refl, }, { exfalso, rw add_succ at h, apply succ_ne_zero (a + d), exact h, }, end end mynat
b06e9911d31eb51b7251ff9bbf392be87d6c964f
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/vname/core.lean
b796ecf16d0a69736d38168ad18a6421ec830b84
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
2,690
lean
/- This files contains a collection of core definitions and properties for variable names. -/ import .type namespace acie ----------------------------------------------------------------- namespace vname ---------------------------------------------------------------- variables {V : Type} [decidable_eq V] -- Type of variable names variables {a b : V} -- Variable names variables {vs : Type → Type} [vset vs V] -- Type of variable name sets variables {X Y : vs V} -- Variable name sets -- Rewrite a constraint from `X` to `Y` if `X = Y`. @[reducible] theorem rw_constraint (p : X = Y) : a ∈ X → a ∈ Y := by rw [p]; intro; assumption -- Rewrite the free variable set from `X` to `Y` if `X = Y`. @[reducible] protected theorem rw (p : X = Y) (x : ν∈ X) : ν∈ Y := ⟨x.1, rw_constraint p x.2⟩ @[reducible] protected theorem eq {x₁ x₂ : ν∈ X} (h : x₁.1 = x₂.1) : x₁ = x₂ := psigma.eq h rfl @[reducible] protected def insert_self (a : V) (X : vs V) : ν∈ insert a X := ⟨a, vset.prop_insert_self a X⟩ -- Erase a variable from the free variable set if it is not equal to this -- variable. @[reducible] protected def erase (x : ν∈ insert a X) (p : x.1 ≠ a) : ν∈ X := ⟨x.1, vset.prop_rm_insert_if_ne x.2 p⟩ -- Insert a variable name into the free variable set. @[reducible] protected def insert (a : V) (x : ν∈ X) : ν∈ insert a X := ⟨x.1, vset.prop_insert a x.2⟩ -- Update a function on names with an extra argument and a matching result. @[reducible] protected def update (a b : V) (F : X →ν Y) (x : ν∈ insert a X) : ν∈ insert b Y := if P : x.1 = a then vname.insert_self b Y else vname.insert b (F (vname.erase x P)) -- Map the free variable set from `X` to `Y` if `x.1 ∈ Y`. @[reducible] def map_of_mem (x : ν∈ X) (p : x.1 ∈ Y) : ν∈ Y := ⟨x.1, p⟩ -- Map the free variable set from `X` to `Y` if `X ⊆ Y`. @[reducible] def map_of_subset (p : X ⊆ Y) (x : ν∈ X) : ν∈ Y := ⟨x.1, vset.prop_mem_of_subset p x.2⟩ theorem eq_of_erase_insert {a : V} (x : ν∈ X) (x_ne_a : x.1 ≠ a) : vname.erase (vname.insert a x) x_ne_a = x := vname.eq (by trivial) -- Variables of exclusive constraints are not equal theorem ne_if_mem_and_not_mem (x : ν∈ X) (x' : ν∉ X) : x.1 ≠ x'.1 := vset.prop_ne_if_mem_and_not_mem x.2 x'.2 theorem insert_mem_ne_not_mem {a : V} (x : ν∈ X) (x' : ν∉ X) : (vname.insert a x).1 ≠ x'.1 := ne_if_mem_and_not_mem (map_of_subset (vset.prop_subset_refl X) x) x' end /- namespace -/ vname ------------------------------------------------------ end /- namespace -/ acie -------------------------------------------------------
a0a1e306d6253606a2750793b2e86f135827d687
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/pi_patterns.lean
e14b7934a1dee468d6765d1d490b209106b54e4c
[ "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
331
lean
open tactic run_cmd do `(%%a → %%b) ← pure ↑`(nat → nat) | failed, skip run_cmd do `(%%a → %%b) ← pure ↑`(Π (b : nat), nat) | failed, skip run_cmd do `(λ a : %%a, (%%b : %%c)) ← pure ↑`(λ n, n + 1) | failed, skip run_cmd do `(let a := (%%a : %%c) in (%%b : %%d)) ← pure ↑`(let n := 1 in n) | failed, skip
be43c84eb8b2e77e48644b168d8cdc78030354aa
ac60dab17014edd769c9618cc1569ce8c960a6a5
/src/grid.lean
6f81fb53f2653025cf31fd803e95d1124841323a
[ "MIT" ]
permissive
frankSil/CAExtensions
20e1f856b3ad775d5e8dc8877614dbcd58c77901
f5c74fd9a806696c73497d9abd45b7315f45379f
refs/heads/master
1,608,706,941,607
1,586,193,337,000
1,586,193,337,000
237,077,434
0
0
null
null
null
null
UTF-8
Lean
false
false
104,480
lean
import utils import data.vector data.list data.int.basic tactic.omega data.fin tactic.linarith tactic.apply open utils section grids open list class relative_grid (α : Type*) := (carrier : Type) (rows : α → ℕ) (cols : α → ℕ) (nonempty : Πg, rows g * cols g > 0) (data : Πg, fin (rows g) → fin (cols g) → carrier) class grid (α : Type*) extends relative_grid α := (bl : α → point) section grid_defs variables {α : Type*} [grid α] (g : α) open grid relative_grid notation `|`:max x `|`:0 := int.nat_abs x def size := rows g * cols g attribute [simp] lemma size_eq_rows_mul_cols : size g = rows g * cols g := rfl def tr (bl : point) (r c : ℕ) : point := ⟨bl.x + c, bl.y + r⟩ attribute [simp] def grid_rows := rows g attribute [simp] def grid_cols := cols g attribute [simp] def gbl := bl g def gtr := tr (bl g) (rows g) (cols g) def tl : point := ⟨(bl g).x, (bl g).y + rows g⟩ def br : point := ⟨(bl g).x + cols g, (bl g).y⟩ lemma expand_gbl : gbl g = bl g := by simp lemma expand_gtr : gtr g = ⟨(bl g).x + cols g, (bl g).y + rows g⟩ := by simp [gtr, tr] lemma blx_eq_tlx {g : α} : (bl g).x = (tl g).x := by simp [bl, tl] lemma brx_eq_trx {g : α} : (br g).x = (gtr g).x := by simp [br, expand_gtr] lemma bly_eq_bry {g : α} : (bl g).y = (br g).y := by simp [br] lemma tly_eq_try {g : α} : (tl g).y = (gtr g).y := by simp [expand_gtr, tl] structure bounding_box := (p₁ : point) (p₂ : point) (h : p₁ ↗ p₂) def bbox_str : bounding_box → string | ⟨p₁, p₂, _⟩ := "<(" ++ to_string p₁ ++ ", " ++ to_string p₂ ++ ")>" instance : has_to_string bounding_box := ⟨bbox_str⟩ instance : has_repr bounding_box := ⟨bbox_str⟩ def bb_eq (bb₁ bb₂ : bounding_box) := bb₁.p₁ = bb₂.p₂ ∧ bb₁.p₂ = bb₂.p₂ instance dec_eq_bb {bb₁ bb₂} : decidable (bb_eq bb₁ bb₂) := by simp [bb_eq]; apply_instance instance : decidable_eq bounding_box := λbb₁ bb₂, begin cases bb₁, cases bb₂, simp, apply_instance end def points_of_box (bb : bounding_box) : point × point := ⟨bb.p₁, bb.p₂⟩ def rows_of_box (bb : bounding_box) : ℕ := |bb.p₂.y - bb.p₁.y| def cols_of_box (bb : bounding_box) : ℕ := |bb.p₂.x - bb.p₁.x| def bb_size (bb : bounding_box) := rows_of_box bb * cols_of_box bb private def data_option (g : α) (x y : ℕ) := if h : y < cols g then if h₁ : x < rows g then some $ data g ⟨x, h₁⟩ ⟨y, h⟩ else none else none end grid_defs section grid_lemmas open grid relative_grid function variables {α : Type*} [grid α] {g : α} private theorem data_data_option {x y : ℕ} (h₁ : y < rows g) (h₂ : x < cols g) : some (data g ⟨y, h₁⟩ ⟨x, h₂⟩) = data_option g y x := by unfold data_option; repeat { rw dif_pos; try { simp [is_bounded, h.2] } }; simpa lemma rows_of_box_pos {bb : bounding_box} : rows_of_box bb > 0 := let ⟨⟨_, y₁⟩, ⟨_, y₂⟩, h⟩ := bb in begin simp only [rows_of_box, gt_from_lt], simp [grid_bounded_iff] at h, rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg]; omega end lemma cols_of_box_pos {bb : bounding_box} : cols_of_box bb > 0 := let ⟨⟨x₁, _⟩, ⟨x₂, _⟩, h⟩ := bb in begin simp only [cols_of_box, gt_from_lt], simp [grid_bounded_iff] at h, rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg]; omega end lemma rows_pos : 0 < rows g := (gt_and_gt_of_mul_gt (nonempty g)).1 lemma cols_pos : 0 < cols g := (gt_and_gt_of_mul_gt (nonempty g)).2 lemma abs_rows_pos : 0 < |rows g| := rows_pos lemma abs_cols_pos : 0 < |cols g| := cols_pos lemma coe_rows_pos : (0 : ℤ) < ↑(rows g) := by simp [rows_pos] lemma coe_cols_pos {g : α} : (0 : ℤ) < ↑(cols g) := by simp [cols_pos] lemma idx_div_cols_bounded {n} (h : n < size g) : (bl g).y + ↑n / ↑(cols g) < (gtr g).y := begin simp [expand_gtr, gt_from_lt] at *, norm_cast, rw mul_comm at h, replace h := nat.div_lt_of_lt_mul h, linarith end lemma idx_mod_cols_bounded {n : ℕ} : (bl g).x + ↑n % ↑(cols g) < (gtr g).x := by simp [expand_gtr]; exact int.mod_lt_of_pos _ coe_cols_pos lemma grid_is_bounding_box : bl g ↗ gtr g := let ⟨h₁, h₂⟩ := gt_and_gt_of_mul_gt (nonempty g) in grid_bounded_iff.2 ⟨ by simpa [expand_gtr], by simpa [expand_gtr] ⟩ structure relative_point (g : α) := (x : fin (rows g)) (y : fin (cols g)) def relative_point_str (g : α) : relative_point g → string | ⟨x, y⟩ := "[" ++ to_string x ++ ", " ++ to_string y ++ "]" instance : has_to_string (relative_point g) := ⟨relative_point_str g⟩ instance : has_repr (relative_point g) := ⟨relative_point_str g⟩ structure grid_point (g : α) := (y : bounded (bl g).y (gtr g).y) (x : bounded (bl g).x (gtr g).x) def grid_point_str (g : α) : grid_point g → string | ⟨x, y⟩ := "[" ++ to_string x ++ ", " ++ to_string y ++ "] - " ++ to_string (bl g) instance : has_to_string (grid_point g) := ⟨grid_point_str g⟩ instance : has_repr (grid_point g) := ⟨grid_point_str g⟩ lemma blgy_lt_trgy : (bl g).y < (gtr g).y := by simp [expand_gtr, rows_pos] lemma gblx_lt_gtrx : (gbl g).x < (gtr g).x := expand_gtr g ▸ expand_gbl g ▸ lt_add_of_pos_right _ coe_cols_pos private lemma grid_rows_eq_try_sub_bly : grid_rows g = |(gtr g).y - (bl g).y| := by simp [expand_gtr] lemma rows_eq_try_sub_bly : rows g = |(gtr g).y - (bl g).y| := grid_rows_eq_try_sub_bly lemma rows_eq_try_sub_bly' : ↑(rows g) = (gtr g).y - (bl g).y := by simp [gtr, tr] private lemma grid_cols_eq_trx_sub_blx : grid_cols g = |((gtr g).x - (bl g).x)| := by simp [expand_gtr] lemma cols_eq_trx_sub_blx : cols g = |((gtr g).x - (bl g).x)| := grid_cols_eq_trx_sub_blx def relpoint_of_gpoint {g : α} (p : grid_point g) : relative_point g := ⟨ ⟨|p.y.1 - (bl g).y|, begin rcases p with ⟨⟨x, ⟨xl, xu⟩⟩, ⟨y, ⟨yl, yu⟩⟩⟩, simp, have eq₁ : x + -(bl g).y ≥ 0, by linarith, have eq₂ : (gtr g).y - (bl g).y ≥ 0, by simp [expand_gtr]; linarith, rw [ ← int.coe_nat_lt_coe_nat_iff, rows_eq_try_sub_bly, int.nat_abs_of_nonneg eq₁, int.nat_abs_of_nonneg eq₂ ], linarith end ⟩, ⟨|p.x.1 - (bl g).x|, have h : p.x.1 - (tl g).x ≥ 0, from le_sub_iff_add_le.2 (by simp [tl, p.x.2.1]), ((int.coe_nat_lt_coe_nat_iff _ _).1 $ (int.nat_abs_of_nonneg h).symm ▸ begin let uby := p.x.2.2, simp only [expand_gtr] at uby, simp only [tl], linarith end) ⟩ ⟩ def gpoint_of_relpoint {g : α} (p : relative_point g) : grid_point g := ⟨ ⟨(bl g).y + p.x.1, ⟨ by simp [tl, expand_gtr], by rcases p with ⟨⟨_, h⟩, _⟩; simp only [tl, expand_gtr, h]; linarith ⟩ ⟩, ⟨(bl g).x + p.y.1, ⟨ by simp [tl], by rcases p with ⟨⟨_, _⟩, ⟨_, h⟩⟩; simp only [tl, expand_gtr, h]; linarith ⟩ ⟩ ⟩ lemma relpoint_gpoint_id {g : α} {p : grid_point g} : gpoint_of_relpoint (relpoint_of_gpoint p) = p := begin rcases p with ⟨⟨x, ⟨hx₁, _⟩⟩, ⟨y, ⟨hy₁, _⟩⟩⟩, simp [relpoint_of_gpoint, gpoint_of_relpoint, -sub_eq_add_neg], have : x - (bl g).y ≥ 0, by linarith, have : y - (bl g).x ≥ 0, by linarith, split; rw int.nat_abs_of_nonneg; try { simp }; assumption end lemma gpoint_relpoint_id {g : α} {p : relative_point g} : relpoint_of_gpoint (gpoint_of_relpoint p) = p := by cases p with x y; simp [gpoint_of_relpoint, relpoint_of_gpoint] def prod_of_rel_point {g : α} (rp : relative_point g) := (rp.x, rp.y) def prod_of_grid_point {g : α} (ap : grid_point g) := (ap.x, ap.y) def grid_point_of_prod {g : α} (p : bounded (bl g).x (gtr g).x × bounded (bl g).y (gtr g).y) : grid_point g := ⟨p.snd, p.fst⟩ def grid_point_of_prod' {g : α} (p : bounded (bl g).y (gtr g).y × bounded (bl g).x (gtr g).x) : grid_point g := ⟨p.fst, p.snd⟩ def abs_data (g : α) (gp : grid_point g) := let rp := relpoint_of_gpoint gp in (data g) rp.x rp.y lemma try_lt_bly : (gbl g).y < (gtr g).y := (grid_bounded_iff.1 grid_is_bounding_box).2 private lemma bounded_establishes_bounds {a b : ℤ} (h : a < b) (x : bounded 0 ( |b - a| )) : a ≤ a + ↑x ∧ a + ↑x < b := have xpos : ↑x ≥ 0, from positive_bounded _, have xmax : ↑x < |b - a|, from bounded_lt _, ⟨ by apply le_add_of_nonneg_right; unfold coe, begin unfold_coes at *, rw add_comm, rw [← int.coe_nat_lt, int.nat_abs_of_nonneg, lt_sub_iff_add_lt] at xmax, exact xmax, { simp [ge_from_le], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right a, zero_add, sub_add_cancel ], exact int.le_of_lt h, } end ⟩ end grid_lemmas end grids section grid_impls structure vec_grid (α : Type) := (r : ℕ) (c : ℕ) (h : r * c > 0) (data : vector α (r * c)) structure vec_grid₀ (α : Type) extends vec_grid α := (o : point) structure fgrid₀ (α : Type) := (r : ℕ) (c : ℕ) (h : r * c > 0) (o : point) (data : bounded o.y (o.y + r) → bounded o.x (o.x + c) → α) end grid_impls section grid_instances open relative_grid grid lemma data_not_empty {α : Type} {g : vec_grid₀ α} : ¬empty_list g.data.to_list := assume contra, begin simp [empty_list] at contra, have contra₁ := contra.symm, rw [list_empty_iff_len, vector.to_list_length] at contra₁, rcases g with ⟨⟨_, _, h,_⟩, _⟩, linarith end lemma linearize_array {x y r c : ℕ} (xb : x < c) (yb : y < r) : y * c + x < r * c := have h₁ : y * c < r * c, by apply mul_lt_mul yb; omega, have h₂ : ∃n, nat.succ y + n = r, from nat_le_dest yb, let ⟨n, h₂⟩ := h₂ in by rw [← h₂, right_distrib, nat.succ_mul, add_assoc]; linarith def rel_point_to_fin {α : Type} [grid α] {g : α} (p : relative_point g) : fin (size g) := ⟨p.x * cols g + p.y, linearize_array p.y.2 p.x.2⟩ def grid_point_to_fin {α : Type} [grid α] {g : α} (p : grid_point g) : fin (size g) := rel_point_to_fin (relpoint_of_gpoint p) lemma expand_grid_point_to_fin {α : Type} [grid α] {g : α} (p : grid_point g) : grid_point_to_fin p = ⟨|p.y.1 - (bl g).y| * cols g + |p.x.1 - (bl g).x|, linearize_array begin rcases p with ⟨_, ⟨y, ⟨_, yu⟩⟩⟩, simp only [tl], rw ← int.coe_nat_lt_coe_nat_iff, have : y - (grid.bl g).x ≥ 0, by rw [ge_from_le]; linarith, rw int.nat_abs_of_nonneg this, simp [expand_gtr] at yu, linarith end begin rcases p with ⟨⟨x, ⟨xl, xu⟩⟩, _⟩, simp only [tl], rw ← int.coe_nat_lt_coe_nat_iff, have : x - (bl g).y ≥ 0, by linarith, rw [int.nat_abs_of_nonneg this, rows_eq_try_sub_bly'], linarith end⟩ := by simp [grid_point_to_fin, relpoint_of_gpoint, rel_point_to_fin]; unfold_coes instance rg_vec_grid {α : Type} : relative_grid (vec_grid α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, nonempty := λg, g.h, data := λg y x, g.data.nth ⟨ y.1 * g.c + x.1, linearize_array x.2 y.2 ⟩ } instance rg_vec_grid₀ {α : Type} : relative_grid (vec_grid₀ α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, nonempty := λg, g.h, data := λg y x, g.data.nth ⟨ y.1 * g.c + x.1, linearize_array x.2 y.2 ⟩ } private lemma absolute_bounds {o : ℤ} {r : ℕ} (x : fin r) : o + ↑x < o + ↑r := by simp; cases x; unfold_coes; simpa instance rg_fgrid₀ {α : Type} : relative_grid (fgrid₀ α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, nonempty := λg, g.h, data := λg y x, g.data ⟨g.o.y + y, ⟨by simp, absolute_bounds _⟩⟩ ⟨g.o.x + x, ⟨by simp, absolute_bounds _⟩⟩ } instance ag_vec_agrid₀ {α : Type} : grid (vec_grid₀ α) := { bl := λg, g.o } instance ag_fgrid₀ {α : Type} : grid (fgrid₀ α) := { bl := λg, g.o } def point_of_grid_point {α : Type*} [grid α] {g : α} : grid_point g → point | ⟨b₁, b₂⟩ := ⟨b₂, b₁⟩ instance point_grid_point_coe {α : Type*} [grid α] (g : α) : has_coe (grid_point g) point := ⟨point_of_grid_point⟩ end grid_instances section finite_grid open list int function section spec open grid relative_grid variables {α : Type} {ag : vec_grid₀ α} {fg : fgrid₀ α} lemma coe_rows_pos_a : ↑ag.r > (0 : ℤ) := by change ag.r with (rows ag); simp [gt_from_lt, rows_pos] lemma coe_rows_pos_f : ↑fg.r > (0 : ℤ) := by change fg.r with (rows fg); simp [gt_from_lt, rows_pos] lemma coe_cols_pos_a : ↑ag.c > (0 : ℤ) := by change ag.c with (cols ag); simp [gt_from_lt, cols_pos] lemma coe_cols_pos_f : ↑fg.c > (0 : ℤ) := by change fg.c with (cols fg); simp [gt_from_lt, cols_pos] end spec variables {α : Type*} [grid α] (g : α) def grp (a b row : ℤ) : list point := map (uncurry point.mk) $ zip (range_pure a b) (repeat row ( |b - a| )) private lemma expand_grp {a b r} (h : a < b) : grp a b r = ⟨a, r⟩ :: grp (a + 1) b r := begin conv_lhs { simp only [grp] }, rw range_pure_next h, have : |b - a| ≥ 1, from nat_abs_ge_one_of_lt h, rw repeat_more this, simp [-sub_eq_add_neg], exact ⟨ by simp [uncurry], by simp [grp, -sub_eq_add_neg, abs_minus_plus h] ⟩ end private lemma expand_grp_g {g : α} : grp (gbl g).x (gtr g).x (gtr g).y = ⟨(gbl g).x, (gtr g).y⟩ :: grp ((gbl g).x + 1) (gtr g).x (gtr g).y := begin simp only [grp], have h : range_pure ((gbl g).x) ((gtr g).x) = (gbl g).x :: range_pure (((gbl g).x) + 1) ((gtr g).x), from range_pure_next (grid_bounded_iff.1 grid_is_bounding_box).1, rw h, have h₁ : repeat ((gtr g).y) ( |(gtr g).x - (gbl g).x| ) = (gtr g).y :: repeat (gtr g).y ( |(gtr g).x - (gbl g).x| - 1), { simp only [expand_gbl], apply repeat_more, rw ← cols_eq_trx_sub_blx, exact abs_cols_pos }, simp only [map, h₁, zip_cons_cons], exact ⟨ by simp [uncurry], by rw abs_minus_plus; exact (grid_bounded_iff.1 grid_is_bounding_box).1 ⟩ end private lemma grp_empty_iff {a b r} : empty_list (grp a b r) ↔ b ≤ a := ⟨ assume h, begin by_cases contra : a < b, {rw expand_grp at h, cases h, exact contra}, {exact le_of_not_lt contra} end, assume h, begin unfold grp, have : range_pure a b = [], by unfold1 range_pure; exact if_neg (not_lt_of_le h), simp [zip_nil_left, empty_list, this] end ⟩ lemma grp_empty_iff' {a b r} : grp a b r = [] ↔ (b ≤ a) := begin split; intros h, {rw ← grp_empty_iff, simp [empty_list, h.symm]}, { have : empty_list (grp a b r), from grp_empty_iff.2 h, simp only [empty_list] at this, exact this.symm } end lemma grp_nil_iff {x y} : grp x x y = [] := have h : empty_list (grp x x y), from grp_empty_iff.2 (le_refl _), h.symm lemma in_grp_second {a b r} {p} (h₁ : p ∈ grp a b r) : p.y = r := begin cases p with px py, simp only [point.y] at *, revert h₁, induction eq : grp a b r with hd tl generalizing a; intros, {cases h₁}, { by_cases h : a < b, { rw expand_grp h at eq, injection eq with eq₁ eq₂, rw mem_cons_iff at h₁, cases h₁ with h₁ h₁, {cc}, { by_cases h₂ : a + 1 < b, {exact ih eq₂ h₁,}, { rw not_lt at h₂, rw grp_empty_iff'.2 h₂ at eq₂, subst eq₂, cases h₁ } } }, {rw not_lt at h, rw grp_empty_iff'.2 h at eq, cases eq} } end lemma in_grp_iff {a b r} {c : point} (h₀ : a < b) : c ∈ grp a b r ↔ is_bounded a b c.x ∧ c.y = r := begin split; intros h, { split, unfold grp at h, simp [-sub_eq_add_neg, uncurry] at h, rcases h with ⟨a₁, b₂, ⟨h₁, h₂⟩⟩, have : a₁ ∈ range_pure a b, from pair_in_zip_l h₁, rw in_range_pure_iff at this, cases c, cc, exact in_grp_second h }, { cases c with x y, simp only [point.x, point.y] at *, unfold is_bounded at h, rcases h with ⟨⟨h, h₁⟩, h₂⟩, simp [grp, -sub_eq_add_neg, uncurry], use x, use y, split, { apply in_zip_of_and, intros, subst h₂, exact eq_of_mem_repeat H, rw in_range_pure_iff, unfold is_bounded, exact ⟨h, h₁⟩, rw length_repeat, rw range_length_pure (int.le_of_lt h₀) }, {simp} } end lemma notin_grp_of_lt {p : point} {a b r : ℤ} (h : p.x < a) : p ∉ grp a b r := begin cases p with px py, simp only [point.x] at *, induction eq : grp a b r with hd tl generalizing a, {intros contra, cases contra}, { by_cases h₁ : a < b, { rw expand_grp h₁ at eq, injection eq with eq₁ eq₂, clear eq, intros contra, cases contra, {subst eq₁, subst eq₂, cases contra, linarith}, { by_cases h₂ : a + 1 < b, { specialize @ih (a + 1) (by linarith) eq₂, contradiction }, { rw not_lt at h₂, rw grp_empty_iff'.2 h₂ at eq₂, subst eq₂, cases contra } } }, {rw not_lt at h₁, rw grp_empty_iff'.2 h₁ at eq, cases eq} } end lemma nodup_grp {a b r} : nodup (grp a b r) := begin induction eq : grp a b r with hd tl generalizing a, {exact nodup_nil}, { by_cases h : a < b, { rw expand_grp h at eq, injection eq with eq₁ eq₂, rw nodup_cons, split, { rw [← eq₂, ← eq₁], apply notin_grp_of_lt, simp only [point.x], linarith }, {exact ih eq₂} }, {rw not_lt at h, rw grp_empty_iff'.2 h at eq, cases eq} } end open function private lemma grp_bounds {a b row : ℤ} : ∀{c : point}, c ∈ grp a b row → is_bounded a b c.x ∧ is_bounded row (row + 1) c.y := assume c h, begin simp [grp] at h, rcases h with ⟨a₁, ⟨b₁, ⟨h₂, h₃⟩⟩⟩, have h₄ : a₁ ∈ range_pure a b, from pair_in_zip_l h₂, have h₅ : b₁ ∈ repeat row ( |b + -a| ), from pair_in_zip_r h₂, rw ← h₃, split; split, {exact (range_pure_bounded h₄).1}, {exact (range_pure_bounded h₄).2}, {simp [repeat_bounded h₅, uncurry]}, {rw (repeat_bounded h₅), exact lt_add_succ _ _} end lemma length_grp {a b : ℤ} (h : a < b) {x : ℤ} : length (grp a b x) = |b - a| := have h₁ : length (range_pure a b) = |b - a|, from range_length_pure (int.le_of_lt h), by simp [grp, length_map, length_zip_left, length_repeat, h₁] lemma injective_grp {a b} (h : a < b) : injective (grp a b) := begin intros a₁ a₂, induction eq : grp a b a₁ with hd tl ih generalizing a; intros h₁, {rw expand_grp h at eq, cases eq}, { rw expand_grp h at eq h₁, injection eq with eq₁ eq₂, injection h₁ with h₂ h₃, by_cases alt : a + 1 < b, {exact ih alt eq₂ h₃}, {cc} } end lemma disjoint_grp_neq_row {a b r₁ r₂} (h₀ : r₁ ≠ r₂) : disjoint (grp a b r₁) (grp a b r₂) := begin by_cases h : a < b, { induction eq : grp a b r₁ with hd tl ih generalizing a, {rw expand_grp h at eq, cases eq}, { cases eq₁ : grp a b r₂ with hd₁ tl₁, {rw expand_grp h at eq₁, cases eq₁}, { rw expand_grp h at eq₁, rw expand_grp h at eq, injection eq₁ with eq₂ eq₃, injection eq with eq₄ eq₅, clear eq eq₁, simp only [mem_cons_iff, disjoint_cons_left, disjoint_cons_right], split, { intros contra, cases contra, {cc}, { rw ← eq₅ at contra, have : hd₁.y = r₁, from in_grp_second contra, clear contra, subst this, subst eq₄, subst eq₅, subst eq₂, cc } }, { split, { intros contra, rw ← eq₃ at contra, have : hd.y = r₂, from in_grp_second contra, clear contra, subst this, subst eq₄, subst eq₅, subst eq₂, cc }, { rw ← eq₃, by_cases h₁ : a + 1 < b, { exact ih h₁ eq₅ }, { rw not_lt at h₁, rw [grp_empty_iff'.2 h₁, disjoint_comm], exact disjoint_nil_left _, } } } } } }, {rw not_lt at h, simp [grp_empty_iff'.2 h]} end def gip (p₁ p₂ : point) : list point := join (map (grp p₁.x p₂.x) (range_pure p₁.y p₂.y)) lemma gip_no_dup {p₁ p₂} : nodup (gip p₁ p₂) := begin cases p₁ with p₁x p₁y, cases p₂ with p₂x p₂y, simp only [gip, nodup_join], split, { intros l hl, by_cases h : p₁x < p₂x, { have : nodup (map (grp p₁x p₂x) (range_pure p₁y p₂y)), from nodup_map (injective_grp h) (nodup_range_pure), induction l with hd tl ih generalizing p₁x, {exact nodup_nil}, { simp only [map, mem_map] at hl, rcases hl with ⟨l, ⟨c₁, c₂⟩⟩, rw ← c₂, exact nodup_grp } }, { rw not_lt at h, simp only [map, mem_map] at hl, rcases hl with ⟨l', ⟨c₁, c₂⟩⟩, rw grp_empty_iff'.2 h at c₂, simp [c₂.symm, nodup_nil] } }, { induction eq : range_pure p₁y p₂y with hd tl ih generalizing p₁y, {simp, constructor}, { by_cases h : p₁y < p₂y, { rw range_pure_next h at eq, injection eq with eq₁ eq₂, specialize @ih (p₁y + 1) eq₂, simp only [ and_imp, disjoint_comm, pairwise_cons, map, mem_map, exists_imp_distrib ], split; try { assumption }, intros l x h₁ h₂, subst h₂, have : x ≠ hd, { intros contra, subst contra, subst eq₁, rw ← eq₂ at h₁, have : p₁y ∉ range_pure (p₁y + 1) p₂y, from @notin_range_pure_of_lt (p₁y + 1) p₂y p₁y (by linarith), contradiction }, exact disjoint_grp_neq_row this }, { rw not_lt at h, rw range_pure_empty_iff.2 h at eq, cases eq } } } end open relative_grid grid def gip_g := gip (bl g) (gtr g) private lemma expand_gip {p₁ p₂} (h : p₁ ↗ p₂) : gip p₁ p₂ = ⟨p₁.x, p₁.y⟩ :: grp (p₁.x + 1) p₂.x p₁.y ++ gip ⟨p₁.x, p₁.y + 1⟩ p₂ := by simp [ gip, expand_grp (grid_bounded_iff.1 h).1, range_pure_next (grid_bounded_iff.1 h).2 ] private lemma expand_row_gip {p₁ p₂} (h : p₁ ↗ p₂) : gip p₁ p₂ = grp p₁.x p₂.x p₁.y ++ gip ⟨p₁.x, p₁.y + 1⟩ p₂ := by simp [gip, range_pure_next (grid_bounded_iff.1 h).2] private lemma expand_gip_g : (gip_g g) = grp (gbl g).x (gtr g).x (gbl g).y ++ gip ⟨(gbl g).x, (gbl g).y + 1⟩ ⟨(gtr g).x, ((gtr g).y)⟩ := begin generalize h : gip ⟨(gbl g).x, (gbl g).y⟩ ⟨(gtr g).x, ((gtr g).y + 1)⟩ = t, simp only [gip_g, gip], rw range_pure_next, dsimp, {apply congr_arg, simp [h.symm, gip]}, {exact try_lt_bly} end def is_in_grid' (xy : point) := is_bounded (gbl g).y (gtr g).y xy.y ∧ is_bounded (gbl g).x (gtr g).x xy.x def is_in_grid (bb : bounding_box) (xy : point) := is_bounded bb.p₁.y bb.p₂.y xy.y ∧ is_bounded bb.p₁.x bb.p₂.x xy.x attribute [reducible] instance has_mem_grid : has_mem point α := ⟨flip is_in_grid'⟩ attribute [reducible] instance has_mem_bb : has_mem point bounding_box := ⟨flip is_in_grid⟩ lemma gip_in_grid {p₁ p₂ : point} {h : p₁ ↗ p₂} : ∀{a}, a ∈ gip p₁ p₂ → a ∈ (⟨p₁, p₂, h⟩ : bounding_box) := assume a h, begin simp [gip] at h, cases a with al ar, rcases h with ⟨l, ⟨⟨a₁, ⟨h₂, h₃⟩⟩, h₁⟩⟩, have h₄ := range_pure_bounded h₂, rw ← h₃ at h₁, have h₅ := grp_bounds h₁, split; split, { simp [bounding_box.p₁], rcases h₅ with ⟨⟨h₅l₁, h₅l₂⟩, ⟨h₅r₁, h₅r₂⟩⟩, cases h₄, transitivity a₁; assumption }, {exact lt_of_le_of_lt (le_of_lt_add_one h₅.2.2) h₄.2}, {exact h₅.1.1}, {exact h₅.1.2} end def grid_bounds : bounding_box := ⟨gbl g, gtr g, grid_is_bounding_box⟩ lemma grid_bounds_p₁ : (grid_bounds g).p₁ = gbl g := rfl lemma grid_bounds_p₂ : (grid_bounds g).p₂ = gtr g := rfl lemma gip_g_in_grid {g : α} : ∀{a}, a ∈ gip_g g → a ∈ (grid_bounds g) := assume a h, gip_in_grid h def make_bounded_idx {g : α} {p : point} (h : p ∈ (grid_bounds g)) : bounded (bl g).x (gtr g).x × bounded (gbl g).y (gtr g).y := (make_bounded h.2, make_bounded h.1) private def make_bounded_indices (is : list point) (h : ∀p, p ∈ is → p ∈ (grid_bounds g)) : list ( bounded (bl g).x (gtr g).x × bounded (gbl g).y (gtr g).y ) := map (λp : {x // x ∈ is}, (⟨p.1.1, (h p.1 p.2).2⟩, ⟨p.1.2, (h p.1 p.2).1⟩)) (attach is) lemma is_bounded_y_of_grid_bounds {g : α} {p} (h : p ∈ (grid_bounds g)) : is_bounded (bl g).y (gtr g).y p.y := by simp [grid_bounds, (∈), flip, is_in_grid] at h; exact h.1 lemma is_bounded_x_of_grid_bounds {g : α} {p} (h : p ∈ (grid_bounds g)) : is_bounded (bl g).x (gtr g).x p.x := by simp [grid_bounds, (∈), flip, is_in_grid] at h; exact h.2 instance decidable_is_in_grid' {xy : point} : decidable (is_in_grid' g xy) := by simp [is_in_grid']; apply_instance instance decidable_is_in_grid (bb : bounding_box) {xy : point} : decidable (is_in_grid bb xy) := by simp [is_in_grid]; apply_instance instance decidable_is_in_grid'_op {xy : point} : decidable (xy ∈ g) := by simp [(∈), is_in_grid', flip]; apply_instance instance decidable_is_in_grid_op (bb : bounding_box) {xy : point} : decidable (xy ∈ bb) := by simp [is_in_grid, (∈), flip]; apply_instance def inject_into_bounded (p : {x // x ∈ gip_g g}) : bounded (bl g).x (gtr g).x × bounded (gbl g).y (gtr g).y := make_bounded_idx (gip_g_in_grid p.2) private def inject_row_into_bounded {a b r} (p : {x // x ∈ grp a b r}) : bounded a b × bounded r (r + 1) := ⟨⟨p.1.1, (grp_bounds p.2).1⟩, ⟨p.1.2, (grp_bounds p.2).2⟩⟩ private lemma blgx_trgx_of_mem {g : α} {x} {y} (h : point.mk x y ∈ g) : (bl g).x < (gtr g).x := by simp only [(∈), flip, is_in_grid'] at h; exact lt_of_le_of_lt h.2.1 h.2.2 theorem in_gip_g_of_in_g {α : Type*} [grid α] {g : α} {p} (h : p ∈ g) : p ∈ gip_g g := begin cases p with x y, simp [-gtr, gip_g, gip], have h₂ : y ∈ range_pure (gbl g).y (gtr g).y, by simp [(∈), flip, is_in_grid'] at h; exact in_range_iff.2 h.1, split, { split, {use y, exact ⟨h₂, by simp [grp]⟩}, { generalize h₂ : range_pure ((bl g).x) ((gtr g).x) = l₁, generalize h₃ : repeat y ( |(gtr g).x - (bl g).x| ) = l₂, rw point_in_zip_prod_iff, apply point_in_zip_repeat_right _ h₃ _, { simp [ h₂.symm, h₃.symm, range_length_pure, length_repeat, int.le_of_lt (blgx_trgx_of_mem h) ] }, { rw [← h₂, in_range_iff], simp only [(∈), flip, is_in_grid'] at h, exact h.2 } } } end theorem in_grid_iff_in_gip_g {p} {g : α} : p ∈ g ↔ p ∈ gip_g g := ⟨ in_gip_g_of_in_g, λh, by apply gip_in_grid h; exact grid_is_bounding_box ⟩ lemma in_grid_iff_in_grid_bounds {p} {g : α} : p ∈ grid_bounds g ↔ p ∈ g := by split; intros h; simp [(∈), flip, is_in_grid, grid_bounds, is_in_grid'] at *; exact h lemma is_bounded_rows_of_in_gip_g {p} {g : α} (h : p ∈ gip_g g) : is_bounded (gbl g).y ((gbl g).y + ↑(rows g)) p.y := (in_grid_iff_in_gip_g.2 h).1 lemma is_bounded_cols_of_in_gip_g {p} {g : α} (h : p ∈ gip_g g) : is_bounded (gbl g).x ((gbl g).x + ↑(cols g)) p.x := (in_grid_iff_in_gip_g.2 h).2 def grid_point_of_mem {p} (h : p ∈ g) : grid_point g := ⟨make_bounded h.1, make_bounded h.2⟩ def generate := map (abs_data g ∘ grid_point_of_prod ∘ inject_into_bounded g) (attach $ gip_g g) notation `℘` g:max := generate g section grid_instances instance vec_grid_functor : functor vec_grid := { map := λα β f g, {g with data := vector.map f g.data} } instance vec_grid_functor_law : is_lawful_functor vec_grid := { id_map := λα ⟨r, c, h, d⟩, by unfold functor.map; simp, comp_map := λα β γ f h ⟨r, c, h, d⟩, by simp [(<$>)] } instance vec_grid₀_functor : functor vec_grid₀ := { map := λα β f g, {g with data := vector.map f g.data} } instance vec_grid₀_functor_law : is_lawful_functor vec_grid₀ := { id_map := λα ⟨⟨r, c, h, d⟩, o⟩, by simp [(<$>)], comp_map := λα β γ f h ⟨⟨r, c, h, d⟩, o⟩, by simp [(<$>)] } instance fgrid₀_functor : functor fgrid₀ := { map := λα β f g, {g with data := λx y, f (g.data x y)} } instance fgrid₀_functor_law : is_lawful_functor fgrid₀ := { id_map := λα ⟨r, c, h, d, o⟩, by simp [(<$>)], comp_map := λα β γ f h ⟨r, c, h, d, o⟩, by simp [(<$>)] } end grid_instances attribute [simp] lemma vec_grid_fmap_r {α β : Type} {g : vec_grid α} {f : α → β} : (f <$> g).r = g.r := by simp [(<$>)] attribute [simp] lemma vec_grid_fmap_c {α β : Type} {g : vec_grid α} {f : α → β} : (f <$> g).c = g.c := by simp [(<$>)] attribute [simp] lemma vec_grid₀_fmap_r {α β : Type} {g : vec_grid₀ α} {f : α → β} : (f <$> g).r = g.r := by simp [(<$>)] attribute [simp] lemma vec_grid₀_fmap_c {α β : Type} {g : vec_grid₀ α} {f : α → β} : (f <$> g).c = g.c := by simp [(<$>)] attribute [simp] lemma fgrid₀_fmap_r {α β : Type} {g : fgrid₀ α} {f : α → β} : (f <$> g).r = g.r := by simp [(<$>)] attribute [simp] lemma fgrid₀_fmap_c {α β : Type} {g : fgrid₀ α} {f : α → β} : (f <$> g).c = g.c := by simp [(<$>)] def point_of_bounded_prod {a b c d : ℤ} : bounded a b × bounded c d → point | ⟨⟨a, _⟩, ⟨c, _⟩⟩ := ⟨a, c⟩ lemma gip_g_nonempty : ¬empty_list (gip_g g) := assume contra, begin simp [gip_g, gip] at contra, have c₁ : ¬empty_list ( range_pure (bl g).y (gtr g).y ), { simp only [empty_list], intros c₂, symmetry' at c₂, rw range_pure_empty_iff at c₂, have c₃ := @grid_is_bounding_box _ _ g, rw grid_bounded_iff at c₃, exact absurd (lt_of_le_of_lt c₂ c₃.2) (lt_irrefl _) }, have c₂ := @not_map_empty_of_not_empty _ _ _ (grp (bl g).x (tr (bl g) (rows g) (cols g)).x) c₁, have c₃ := not_join_empty_of_not_empty contra, cases c₃, {contradiction}, { revert c₃ contra c₂ c₁, generalize c₆ : bl g = bl, generalize c₅ : tr bl (rows g) (cols g) = tr', generalize c₄ : map (grp bl.x tr'.x) (range_pure bl.y (gtr g).y) = l, let h₃ := @grid_is_bounding_box _ _ g, rw grid_bounded_iff at h₃, simp [gtr, c₆, c₅] at h₃, intros, have c₅ : ∃z ∈ l, ¬empty_list z, { let h := grp bl.x tr'.x bl.y, have h₁ : h = grp bl.x tr'.x bl.y, by cc, use h, split, { rw h₁, revert c₄, generalize h₂ : range_pure bl.y tr'.y = l₁, intros, cases l₁ with w ws, { rw range_pure_empty_iff at h₂, exact absurd (lt_of_le_of_lt h₂ h₃.2) (lt_irrefl _) }, { have h₄ : w = bl.y, by unfold1 range_pure at h₂; rw if_pos h₃.2 at h₂; injection h₂ with h₃ _; rw h₃, have : bl.y < (gtr g).y, by subst c₅; simp [gtr, c₆]; exact h₃.2, simp [c₄.symm, range_pure_next this] }, }, { unfold1 grp at h₁, have h₂ : tr'.x > bl.x, from h₃.1, have h₄ : range_pure (bl.x) (tr'.x) = bl.x :: range_pure (bl.x + 1) (tr'.x), from range_pure_next h₂, rw h₄ at h₁, have : |tr'.x - bl.x| ≥ 1, begin apply nat.succ_le_of_lt (lt_of_coe_nat_lt_coe_nat _), rw [nat_abs_of_nonneg, lt_sub], simpa, linarith end, have h₅ : repeat bl.y ( |tr'.x - bl.x| ) = bl.y :: repeat bl.y (( |tr'.x - bl.x| ) - 1), from repeat_more this, rw [h₅, zip_cons_cons, map_cons] at h₁, rw h₁, apply not_empty_cons } }, rcases c₅ with ⟨c₅l, ⟨c₅₁, c₅₂⟩⟩, rw [← c₄, ← c₅] at c₅₁, simp only [gtr] at c₃, subst c₆, exact absurd (c₃ c₅l c₅₁) c₅₂ } end lemma length_gip {p₁ p₂ : point} (h : p₁ ↗ p₂) : length (gip p₁ p₂) = |p₂.y - p₁.y| * |p₂.x - p₁.x| := begin rw [← int.coe_nat_eq_coe_nat_iff, ← nat_abs_mul], rw grid_bounded_iff at h, have h₁ : (p₂.y - p₁.y) * (p₂.x - p₁.x) > 0, {cases p₁, cases p₂, apply mul_pos; omega}, simp [ -sub_eq_add_neg, gip, length_join, (∘), length_grp h.1, range_length_pure (int.le_of_lt h.2), nat_abs_of_nonneg (int.le_of_lt h₁) ], repeat {rw nat_abs_of_nonneg}; simp [-sub_eq_add_neg, ge_from_le]; apply int.le_of_lt; simp [h.1, h.2] end theorem length_gip_g : length (gip_g g) = rows g * cols g := by simp [ gip_g, length_gip, rows_eq_try_sub_bly, cols_eq_trx_sub_blx, grid_is_bounding_box ] private theorem length_generate {α : Type*} [grid α] (g : α) : length (℘ g) = grid_rows g * grid_cols g := by unfold generate gip_g; rw [ length_map, grid_rows_eq_try_sub_bly, grid_cols_eq_trx_sub_blx, length_attach, length_gip_g, rows_eq_try_sub_bly, cols_eq_trx_sub_blx ] lemma length_generate_eq_size : length (℘ g) = size g := by simp [size, length_generate] lemma map_generate_map_v₀ {α β : Type} {g : vec_grid₀ α} {f : α → β} : f <$> (℘ g) = ℘ (f <$> g) := by simpa [(<$>), generate, abs_data, data, vector.nth_map, (∘)] lemma map_generate_map_f₀ {α β : Type} (g : fgrid₀ α) {f : α → β} : f <$> (℘ g) = ℘ (f <$> g) := by simpa [(<$>), generate, abs_data, data] lemma dec_grid_len_eq_indices_len : length (℘ g) = length (gip_g g) := by simp [length_generate, length_gip_g] def vec_grid₀_of_fgrid₀ {α : Type} (g : fgrid₀ α) : vec_grid₀ α := {g with data := ⟨℘ g, length_generate_eq_size _⟩} def fgrid₀_of_vec_grid₀ {α : Type} (g : vec_grid₀ α) : fgrid₀ α := {g with data := λx y, abs_data g ⟨x, y⟩} instance f₀_v₀_coe {α : Type} : has_coe (fgrid₀ α) (vec_grid₀ α) := ⟨vec_grid₀_of_fgrid₀⟩ instance v₀_f₀_coe {α : Type} : has_coe (vec_grid₀ α) (fgrid₀ α) := ⟨fgrid₀_of_vec_grid₀⟩ attribute [simp] lemma vec_grid₀_of_fgrid₀_r {α : Type} {g : fgrid₀ α} : (vec_grid₀_of_fgrid₀ g).r = g.r := by simp [vec_grid₀_of_fgrid₀] attribute [simp] lemma vec_grid₀_of_fgrid₀_c {α : Type} {g : fgrid₀ α} : (vec_grid₀_of_fgrid₀ g).c = g.c := by simp [vec_grid₀_of_fgrid₀] attribute [simp] lemma vec_grid₀_of_fgrid₀_o {α : Type} {g : fgrid₀ α} : (vec_grid₀_of_fgrid₀ g).o = g.o := by simp [vec_grid₀_of_fgrid₀] attribute [simp] lemma fgrid₀_of_vec_grid₀_r {α : Type} {g : vec_grid₀ α} : (fgrid₀_of_vec_grid₀ g).r = g.r := by simp [fgrid₀_of_vec_grid₀] attribute [simp] lemma fgrid₀_of_vec_grid₀_c {α : Type} {g : vec_grid₀ α} : (fgrid₀_of_vec_grid₀ g).c = g.c := by simp [fgrid₀_of_vec_grid₀] attribute [simp] lemma fgrid₀_of_vec_grid₀_o {α : Type} {g : vec_grid₀ α} : (fgrid₀_of_vec_grid₀ g).o = g.o := by simp [fgrid₀_of_vec_grid₀] attribute [simp] lemma vec_grid₀_of_fgrid₀_gtr {α : Type} {g : fgrid₀ α} : gtr (vec_grid₀_of_fgrid₀ g) = gtr g := by simp [expand_gtr, bl, cols, rows, vec_grid₀_of_fgrid₀] attribute [simp] lemma fgrid₀_of_vec_grid₀_gtr {α : Type} {g : vec_grid₀ α} : gtr (fgrid₀_of_vec_grid₀ g) = gtr g := by simp [expand_gtr, bl, cols, rows, fgrid₀_of_vec_grid₀] private theorem nth_le_grp {n} {a b r : ℤ} (h : a < b) (H) : nth_le (grp a b r) n H = ⟨a + n, r⟩ := begin rw ← option.some_inj, rw ← nth_le_nth H, induction n with n ih generalizing a b, {simp [expand_grp h]}, { simp [expand_grp h], have : a + 1 < b, begin have : a + 1 ≠ b, assume contra, by simp [contra.symm, @length_grp a (a + 1) (by cc)] at H; clear contra h ih; omega, by_contradiction h₁, replace h₁ := le_of_not_lt h₁, rw le_iff_eq_or_lt at h₁, cases h₁; try { cc }, have : a = b, by linarith, rw [this, length_grp] at H, simp at H, cases H, linarith end, have lenok : n < length (grp (a + 1) b r), begin rw length_grp this, rw length_grp h at H, have eq₁ : b - (a + 1) ≥ 0, by linarith, have eq₂ : b - a ≥ 0, by linarith, rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg eq₁], rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg eq₂] at H, simp, simp at H, linarith end, specialize @ih (a + 1) b this lenok, simp [ih] } end lemma bl_g_in_g {α : Type*} [grid α] {g : α} : bl g ∈ g := begin simp only [has_mem.mem, flip, is_in_grid, is_in_grid', is_bounded, gbl], split; split; try { linarith }, exact blgy_lt_trgy, exact gblx_lt_gtrx end lemma bl_g_in_gip_g {α : Type*} [grid α] {g : α} : bl g ∈ gip_g g := in_grid_iff_in_gip_g.1 bl_g_in_g theorem nth_grp {n} {a b r : ℤ} (h : a < b) (H : n < length (grp a b r)) : nth (grp a b r) n = some ⟨a + n, r⟩ := by rw nth_le_nth H; exact congr_arg _ (nth_le_grp h _) theorem nth_le_gip {n} {p₁ p₂ : point} (h : p₁ ↗ p₂) (H) : nth_le (gip p₁ p₂) n H = ⟨p₁.x + n % |p₂.x - p₁.x|, p₁.y + n / |p₂.x - p₁.x|⟩ := begin cases p₁ with x₁ y₁, cases p₂ with x₂ y₂, have x₁x₂ : x₁ < x₂, from (grid_bounded_iff.1 h).1, have y₁y₂ : y₁ < y₂, from (grid_bounded_iff.1 h).2, rw [← option.some_inj, ← nth_le_nth H], rw length_gip h at H, repeat { rw nat_abs_of_nonneg (nonneg_of_lt x₁x₂) }, simp [-sub_eq_add_neg] at *, have : y₂ = y₁ + (y₂ - y₁), by linarith, rw this, clear this, have : y₂ - y₁ = ↑|y₂ - y₁|, by rw nat_abs_of_nonneg; exact nonneg_of_lt y₁y₂, rw this, clear this, generalize hrows : |y₂ - y₁| = rows, rw hrows at H, induction rows with rows ih generalizing y₁ y₂ n, {exfalso, simp at H, cases H}, { rw expand_row_gip _, { by_cases h₁ : n < |x₂ - x₁|, { simp [-sub_eq_add_neg], rw [nth_split, nth_grp]; try {simpa [length_grp x₁x₂]}, congr' 2; rw [ ← int.coe_nat_lt_coe_nat_iff, nat_abs_of_nonneg (nonneg_of_lt x₁x₂) ] at h₁, rw mod_eq_of_lt (coe_zero_le _) h₁, rw div_eq_zero_of_lt (coe_zero_le _) h₁, simp }, { generalize hcols : x₂ - x₁ = cols, have rowsnezero : rows ≠ 0, assume contra, by simp [contra, -sub_eq_add_neg] at H; contradiction, have colsnezero : cols ≠ 0, by linarith, have x₂x₁n : |x₂ - x₁| ≤ n, from not_lt.1 h₁, have lenok : ¬n < length (grp x₁ x₂ y₁), by simpa [length_grp x₁x₂, -sub_eq_add_neg], simp [-sub_eq_add_neg], rw nth_split_second lenok, by_cases h₂ : y₁ + 1 < y₂, { have h₃ : {x := x₁, y := y₁ + 1}↗{x := x₂, y := y₂}, from ⟨x₁x₂, h₂⟩, have lenok : n - length (grp x₁ x₂ y₁) < rows * |x₂ - x₁|, { rw nat.succ_mul at H, rw [ length_grp x₁x₂, ← int.coe_nat_lt_coe_nat_iff, int.coe_nat_sub x₂x₁n, int.coe_nat_mul, sub_lt_iff_lt_add, ← int.coe_nat_mul, ← int.coe_nat_add ], rwa int.coe_nat_lt_coe_nat_iff }, have rowsok : |y₂ - (y₁ + 1)| = rows, by rw [← abs_minus_plus y₁y₂, hrows, nat.succ_sub_one], rw [ ← add_assoc, @ih (y₁ + 1) y₂ (n - length (grp x₁ x₂ y₁)) h₃ h₂ rowsok lenok, length_grp x₁x₂ ], simp [-sub_eq_add_neg], exact ⟨ begin rw [ int.coe_nat_sub x₂x₁n, nat_abs_of_nonneg, ← hcols, mod_eq_mod_iff_mod_sub_eq_zero, mod_eq_zero_of_dvd ], simp, rw ← dvd_neg, simp, exact nonneg_of_lt x₁x₂ end, begin rw [ int.coe_nat_sub x₂x₁n, nat_abs_of_nonneg (nonneg_of_lt x₁x₂), hcols ], simp, have : -cols = cols * (-1 : ℤ), by simp, rw this, rw int.add_mul_div_left _ _ colsnezero, simp end ⟩ }, { have h₃ : y₁ + 1 = y₂, by linarith, have h₄ : |y₂ - y₁| = 1, by simp [h₃.symm, add_sub_cancel'], rw h₄ at hrows, injection hrows with contra, cc } } }, { exact ⟨ (grid_bounded_iff.1 h).1, begin simp [ sub_lt_iff_lt_add, lt_add_iff_pos_right, -sub_eq_add_neg ], exact nat.cases_on rows zero_lt_one (λ_, lt_trans zero_lt_one (lt_add_succ _ _)), end ⟩ } } end theorem nth_le_gip_g {n} (H) : nth_le (gip_g g) n H = ⟨(bl g).x + n % cols g, (bl g).y + n / cols g⟩ := begin rw cols_eq_trx_sub_blx, exact @nth_le_gip n (gbl g) (gtr g) grid_is_bounding_box H end theorem nth_gip {n} {p₁ p₂ : point} (h : p₁ ↗ p₂) (H : n < length (gip p₁ p₂)) : nth (gip p₁ p₂) n = some ⟨p₁.x + n % |p₂.x - p₁.x|, p₁.y + n / |p₂.x - p₁.x|⟩ := by simp [nth_le_nth H, nth_le_gip h] theorem nth_gip_g {n} (H : n < length (gip_g g)) : nth (gip_g g) n = some ⟨(bl g).x + n % cols g, (bl g).y + n / cols g⟩ := by simp [nth_le_nth H, nth_le_gip_g] theorem nth_generate {n} (H) : nth_le (℘ g) n H = abs_data g ⟨ ⟨(bl g).y + n / cols g, ⟨ by simp, idx_div_cols_bounded (by rwa length_generate_eq_size at H) ⟩⟩, ⟨(bl g).x + n % cols g, ⟨ by simp, idx_mod_cols_bounded⟩ ⟩⟩ := begin rw length_generate at H, rw [← option.some_inj, ← nth_le_nth], simp only [ abs_data, (∘), relpoint_of_gpoint, prod_of_rel_point, expand_gtr, generate, nth_map ], have : n < length (attach (gip_g g)), by simpa [length_attach, length_gip_g], simp [ nth_le_nth this, inject_into_bounded, make_bounded_idx, make_bounded, nth_le_gip_g, grid_point_of_prod, data_option ] end lemma gip_g_expand : gip_g g = bl g :: grp ((bl g).x + 1) (gtr g).x (bl g).y ++ gip ⟨(bl g).x, (bl g).y + 1⟩ (gtr g) := begin unfold gip_g gip, rw range_pure_next, simp, rw expand_grp, simp, cases (bl g), simp, exact gblx_lt_gtrx, exact blgy_lt_trgy end theorem nth_generate' {n} (h : n < length ℘ g) : nth (℘ g) n = some (abs_data g ⟨ ⟨(bl g).y + n / cols g, ⟨ by simp, idx_div_cols_bounded (by rwa length_generate_eq_size at h) ⟩⟩, ⟨(bl g).x + n % cols g, ⟨ by simp, idx_mod_cols_bounded⟩ ⟩⟩) := by simp [nth_le_nth h, congr_arg, nth_generate] lemma abs_data_eq_nth_v₀ {α : Type} {g : vec_grid₀ α} {p} : abs_data g p = vector.nth g.data (grid_point_to_fin p) := by simpa [ abs_data, (∘), relpoint_of_gpoint, prod_of_rel_point, data, grid_point_to_fin, rel_point_to_fin ] lemma abs_data_eq_nth_v₀' {α : Type} {g : vec_grid₀ α} {p} : abs_data g p = vector.nth g.data ⟨|p.y.1 - g.o.y| * g.c + |p.x.1 - g.o.x|, begin rcases p with ⟨⟨x, ⟨xl, xu⟩⟩, ⟨y, ⟨yl, yu⟩⟩⟩, simp [-sub_eq_add_neg], simp [-sub_eq_add_neg, expand_gtr, bl, rows, cols] at *, rw add_comm, have eq₁ : |y - g.o.x| < g.c, { have : y - (g.o).x ≥ 0, by linarith, rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg this], linarith }, have eq₂ : |x - g.o.y| < g.r, { have : x - g.o.y ≥ 0, by linarith, rw [← int.coe_nat_lt_coe_nat_iff, int.nat_abs_of_nonneg this], linarith }, exact linearize_array eq₁ eq₂ end⟩ := by simp [ abs_data, (∘), relpoint_of_gpoint, prod_of_rel_point, data, grid_point_to_fin, rel_point_to_fin, bl, rows, cols ] lemma abs_data_eq_nth_f₀ {α : Type} {g : fgrid₀ α} {p} : abs_data g p = g.data p.y p.x := begin rcases p with ⟨⟨x, ⟨xl, xu⟩⟩, ⟨y, ⟨yl, yu⟩⟩⟩, simp only [ abs_data, (∘), relpoint_of_gpoint, prod_of_rel_point, data ], unfold_coes, simp only [fin.val, of_nat_eq_coe], have h₁ : x - (bl g).y ≥ 0, by linarith, have h₂ : y - (bl g).x ≥ 0, by linarith, congr; rw int.nat_abs_of_nonneg; try { assumption }; simp only [bl]; linarith end lemma some_nth_le_generate_v₀ {α : Type} {g : vec_grid₀ α} {n} (H) : some (nth_le (℘ g) n H) = nth g.data.to_list ( |↑n % ↑g.c| + |↑n / ↑g.c| * g.c ) := begin rcases g with ⟨⟨r, c, h, ⟨d, hd⟩⟩, o⟩, rw [nth_le_nth, nth_generate], simp [abs_data_eq_nth_v₀', expand_gtr, bl, rows, cols, vector.nth, hd], rw mod_add_div_coe, simp [length_generate, rows, cols] at H, simp [H], simpa [hd] end lemma nth_generate_v₀ {α : Type} {g : vec_grid₀ α} {n} (H : n < length ℘ g): nth (℘ g) n = nth g.data.to_list ( |↑n % ↑g.c| + |↑n / ↑g.c| * g.c) := by simp [nth_le_nth, some_nth_le_generate_v₀, H] private lemma goy_add_n_div_c_lt_goy_add_r {α : Type} {g : fgrid₀ α} {n : ℕ} (h : n < length ℘ g) : g.o.y + ↑n / ↑g.c < g.o.y + ↑g.r := begin simp [-sub_eq_add_neg], norm_cast, rw [length_generate, nat.mul_comm] at h, exact nat.div_lt_of_lt_mul h end lemma some_nth_le_generate_f₀ {α : Type} {g : fgrid₀ α} {n} (H) : some (nth_le (℘ g) n H) = g.data ⟨g.o.y + ↑n / ↑g.c, ⟨by simp, goy_add_n_div_c_lt_goy_add_r H⟩⟩ ⟨g.o.x + ↑n % ↑g.c, ⟨by simp, by simp; exact mod_lt_of_pos _ coe_cols_pos_f⟩⟩ := by simpa [nth_generate, abs_data_eq_nth_f₀, expand_gtr] lemma nth_generate_f₀ {α : Type} {g : fgrid₀ α} {n} (H : n < length ℘ g) : nth (℘ g) n = g.data ⟨g.o.y + ↑n / ↑g.c, ⟨by simp, goy_add_n_div_c_lt_goy_add_r H⟩⟩ ⟨g.o.x + ↑n % ↑g.c, ⟨by simp, by simp; exact mod_lt_of_pos _ coe_cols_pos_f⟩⟩ := by simp [nth_le_nth H, some_nth_le_generate_f₀] lemma nth_le_generate_f₀ {α : Type} {g : fgrid₀ α} {n} (H) : nth_le (℘ g) n H = g.data ⟨g.o.y + ↑n / ↑g.c, ⟨by simp, goy_add_n_div_c_lt_goy_add_r H⟩⟩ ⟨g.o.x + ↑n % ↑g.c, ⟨by simp, by simp; exact mod_lt_of_pos _ coe_cols_pos_f⟩⟩ := by simpa [nth_generate, abs_data_eq_nth_f₀, expand_gtr] lemma generate_eq_data {α : Type} (g : vec_grid₀ α) : ℘ g = g.data.to_list := begin have h₁ : length (℘ g) = rows g * cols g, from length_generate _, have h₂ : length (g.data.to_list) = rows g * cols g, by simp [rows, cols], apply ext_le (eq.trans h₁ h₂.symm) (λi hi₁ hi₂, _), rw h₁ at hi₁, rw h₂ at hi₂, have : hi₁ = hi₂, from rfl, subst this, dedup, rw ← option.some_inj, repeat { rw ← nth_le_nth }, rename hi₁_1 hi, rcases g with ⟨⟨r, c, h, ⟨data, hd⟩⟩, o⟩, simp [-sub_eq_add_neg, rows, cols] at *, rw [nth_le_nth (by simpa [length_generate_eq_sizes]), some_nth_le_generate_v₀], rw nth_le_nth hi₂, simp, have : |↑i % ↑c| + |↑i / ↑c| * c = i, from mod_add_div_coe, repeat { rw ← nth_le_nth }, simp [this] end private theorem generate_inj_v₀_v₀ {α : Type} {g₁ g₂ : vec_grid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) (h : ℘ g₁ = ℘ g₂) : g₁ = g₂ := begin repeat { rw generate_eq_data at h }, rcases g₁ with ⟨⟨g₁r, g₁c, g₁h, g₁d⟩, g₁o⟩, rcases g₂ with ⟨⟨g₂r, g₂c, g₂h, g₂d⟩, g₂o⟩, dsimp at hrows hcols horig h, substs hrows hcols horig, congr, exact vector.to_list_inj h end lemma nth_le_attach_gip_in_gip_g {α : Type*} [grid α] {g : α} {i} {hi} : (nth_le (attach (gip_g g)) i hi).val ∈ gip_g g := begin rw [nth_le_attach, ← in_grid_iff_in_gip_g, nth_le_gip_g], simp only [(∈), flip, is_in_grid', is_bounded], split; split, { have : ↑i / ↑(cols g) ≥ 0, by norm_cast; linarith, simp [le_add_of_nonneg_right this] }, { simp [expand_gtr], norm_cast, apply nat.div_lt_of_lt_mul, simp [length_attach, length_gip_g] at hi, rw mul_comm at hi, exact hi }, { have : ↑i % ↑(cols g) ≥ 0, by norm_cast; linarith, simp [le_add_of_nonneg_right this] }, { simp [expand_gtr], exact mod_lt_of_pos _ coe_cols_pos } end theorem grid_eq_iff_v₀_v₀ {α : Type} {g₁ g₂ : vec_grid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : g₁ = g₂ ↔ ℘ g₁ = ℘ g₂ := ⟨λh, h ▸ rfl, generate_inj_v₀_v₀ hrows hcols horig⟩ lemma i_bounded {α : Type*} [grid α] {g : α} {p} (h : p ∈ g) : is_bounded 0 (size g) ( |p.y - (bl g).y| * cols g + |p.x - (bl g).x| ) := begin have h₁ : |p.x - (bl g).x| ≥ 0, by simp, have h₂ : |p.y - (bl g).y| ≥ 0, by simp, unfold is_bounded, split, {linarith}, { simp [(∈), flip, is_in_grid'] at h, rcases h with ⟨⟨h₃, h₄⟩, ⟨h₅, h₆⟩⟩, have eq₁ : |p.x - (bl g).x| < cols g, { rw ← int.coe_nat_lt_coe_nat_iff, rw cols_eq_trx_sub_blx, repeat { rw nat_abs_of_nonneg }, simpa, simp [expand_gtr], linarith }, have eq₂ : |p.y - (bl g).y| < rows g, { rw ← int.coe_nat_lt_coe_nat_iff, rw rows_eq_try_sub_bly, repeat { rw nat_abs_of_nonneg }, simpa, simp [expand_gtr], linarith }, exact linearize_array eq₁ eq₂ } end private theorem generate_inj_f₀_f₀ {α : Type} {g₁ g₂ : fgrid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) (h : ℘ g₁ = ℘ g₂) : g₁ = g₂ := begin have hl₁ : length (℘ g₁) = g₁.r * g₁.c, from length_generate _, have hl₂ : length (℘ g₂) = g₂.r * g₂.c, from length_generate _, cases g₁ with g₁r g₁c g₁h g₁o g₁d, cases g₂ with g₂r g₂c g₂h g₂o g₂d, dsimp at hrows hcols horig hl₁ hl₂, subst hrows, subst hcols, subst horig, congr, ext x y, rcases x with ⟨x, ⟨xl, xu⟩⟩, rcases y with ⟨y, ⟨yl, yu⟩⟩, have rowsnezero : g₁r ≠ 0, assume contra, by simp [contra] at g₁h; exact absurd g₁h (lt_irrefl _), have colsnezero : g₁c ≠ 0, assume contra, by simp [contra] at g₂h; exact absurd g₂h (lt_irrefl _), let i := |x - g₁o.y| * g₁c + |y - g₁o.x|, have hi : i = |x - g₁o.y| * g₁c + |y - g₁o.x|, refl, have r_nonneg : x - g₁o.y ≥ 0, by simp only [ge_from_le, le_sub_iff_add_le, zero_add]; exact xl, have c_nonneg : y - g₁o.x ≥ 0, by simp only [ge_from_le, le_sub_iff_add_le, zero_add]; exact yl, have i_nonneg : 0 ≤ i, by linarith, have i_bounded : i < g₁r * g₁c, { have yb : y - g₁o.x < ↑g₁c, from sub_lt_iff_lt_add'.2 yu, have xb : x - g₁o.y < ↑g₁r, from sub_lt_iff_lt_add'.2 xu, rw hi, apply linearize_array; try { rw ← int.coe_nat_lt_coe_nat_iff }; rw nat_abs_of_nonneg; try { assumption } }, have h₁ : ∀hh, list.nth_le (℘ ( {r := g₁r, c := g₁c, h := g₁h, o := g₁o, data := g₁d} : fgrid₀ α )) i hh = list.nth_le (℘ ( {r := g₁r, c := g₁c, h := g₂h, o := g₁o, data := g₂d} : fgrid₀ α )) i (hl₂.symm ▸ i_bounded), { rw h, intro, refl }, specialize h₁ (hl₁.symm ▸ i_bounded), simp [-sub_eq_add_neg, nth_le_generate_f₀] at h₁, have : g₁o.y + (↑|y - g₁o.x| + ↑|x - g₁o.y| * ↑g₁c) / ↑g₁c = x, { repeat { rw nat_abs_of_nonneg; try { assumption } }, rw @int.add_mul_div_right _ _ ↑g₁c (by simp [colsnezero]), rw div_eq_zero_of_lt c_nonneg (sub_lt_iff_lt_add'.2 yu), simp }, simp only [this] at h₁, have : g₁o.x + ↑|y - g₁o.x| % ↑g₁c = y, { repeat { rw nat_abs_of_nonneg; try { assumption } }, rw mod_eq_of_lt c_nonneg (sub_lt_iff_lt_add'.2 yu), simp }, simp only [this] at h₁, exact h₁ end theorem grid_eq_iff_f₀_f₀ {α : Type} {g₁ g₂ : fgrid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : g₁ = g₂ ↔ ℘ g₁ = ℘ g₂ := ⟨λh, h ▸ rfl, generate_inj_f₀_f₀ hrows hcols horig⟩ def row (n : fin (rows g)) : (fin (cols g)) → carrier α := data g n def col (n : fin (cols g)) : (fin (rows g)) → carrier α := flip (data g) n def top := row g ⟨ 0, and.elim_left (gt_and_gt_of_mul_gt (nonempty g)) ⟩ def bot := row g ⟨nat.pred (rows g), nat.pred_lt (ne_of_gt (gt_and_gt_of_mul_gt (nonempty g)).1) ⟩ def left := have h : cols g > 0, from (gt_and_gt_of_mul_gt (nonempty g)).2, col g ⟨0, h⟩ def right := have h : cols g > 0, from (gt_and_gt_of_mul_gt (nonempty g)).2, col g ⟨nat.pred (cols g), nat.pred_lt (ne_of_gt h)⟩ def overlaid_by (bb₁ bb₂ : bounding_box) := (bb₂.p₁.x ≤ bb₁.p₁.x ∧ bb₁.p₂.x ≤ bb₂.p₂.x) ∧ (bb₁.p₂.y ≤ bb₂.p₂.y ∧ bb₂.p₁.y ≤ bb₁.p₁.y) def in_grid_bounded (p : point) (h : is_in_grid' g p) := let ⟨left, right⟩ := h in (make_bounded left, make_bounded right) instance overlaid_decidable (p₁ p₂ : bounding_box) : decidable (overlaid_by p₁ p₂) := by simp [overlaid_by]; apply_instance lemma overlaid_by_refl (bb : bounding_box) : overlaid_by bb bb := by simp [overlaid_by]; repeat {split}; refl lemma overlaid_by_trans {bb₁ bb₂ bb₃ : bounding_box} (h : overlaid_by bb₁ bb₂) (h₁ : overlaid_by bb₂ bb₃) : overlaid_by bb₁ bb₃ := by simp [overlaid_by] at *; repeat {split}; transitivity; finish lemma overlaid_by_antisymm {bb₁ bb₂ : bounding_box} (h : overlaid_by bb₁ bb₂) (h₁ : overlaid_by bb₂ bb₁) : bb₁ = bb₂ := begin simp [overlaid_by] at *, rcases bb₁ with ⟨⟨_, _⟩, ⟨_, _⟩⟩, rcases bb₂ with ⟨⟨_, _⟩, ⟨_, _⟩⟩, safe end lemma overlaid_by_size_le {α : Type*} [grid α] {g : α} {bb : bounding_box} (h : overlaid_by bb (grid_bounds g)) : bb_size bb ≤ size g := begin unfold bb_size, rw [ size_eq_rows_mul_cols, rows_of_box, cols_of_box, rows_eq_try_sub_bly, cols_eq_trx_sub_blx ], simp [overlaid_by, grid_bounds] at h, rcases h with ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩, rw ← int.coe_nat_le_coe_nat_iff, repeat { rw int.coe_nat_mul }, cases bb with p₁ p₂ hbb, simp [-sub_eq_add_neg] at *, rw grid_bounded_iff at hbb, cases hbb with h₅ h₆, repeat { rw nat_abs_of_nonneg; try { linarith } }, apply mul_le_mul; linarith end lemma is_in_larger {bb₁ bb₂ : bounding_box} {xy : point} (h : xy ∈ bb₁) (h₁ : overlaid_by bb₁ bb₂) : xy ∈ bb₂ := ⟨⟨le_trans h₁.2.2 h.1.1, lt_of_lt_of_le h.1.2 h₁.2.1⟩, ⟨le_trans h₁.1.1 h.2.1, lt_of_lt_of_le h.2.2 h₁.1.2⟩⟩ private def bounded_prod_of_point {p : point} {g : α} (h : p ∈ g) : bounded (bl g).x (gtr g).x × bounded (bl g).y (gtr g).y := ⟨make_bounded h.2, make_bounded h.1⟩ open bounding_box def subgrid (bb : bounding_box) (h : overlaid_by bb (grid_bounds g)) : fgrid₀ (carrier α) := ⟨rows_of_box bb, cols_of_box bb, mul_pos rows_of_box_pos cols_of_box_pos, bb.p₁, λx y, abs_data g ⟨⟨x.1, begin unfold overlaid_by at h, cases x with x hx, simp, rw grid_bounds_p₁ at h, rw grid_bounds_p₂ at h, exact ⟨ le_trans h.2.2 hx.1, begin have : bb.p₁.y + ↑(rows_of_box bb) = bb.p₂.y, begin have : (bb.p₂).y - (bb.p₁).y ≥ 0, by simp [-sub_eq_add_neg, ge_from_le]; apply int.le_of_lt (grid_bounded_iff.1 bb.3).2, simp [-sub_eq_add_neg, rows_of_box], rw nat_abs_of_nonneg this, simp end, rw this at hx, exact lt_of_lt_of_le hx.2 h.2.1 end ⟩ end⟩, ⟨y.1, begin unfold overlaid_by at h, cases y with y hy, simp, rw grid_bounds_p₁ at h, rw grid_bounds_p₂ at h, have : (bb.p₁).x + ↑(cols_of_box bb) = bb.p₂.x, by simp [ -sub_eq_add_neg, bounding_box.p₁, bounding_box.p₂, cols_of_box, nat_abs_of_nonneg (nonneg_of_lt (grid_bounded_iff.1 bb.3).1), add_sub_cancel'_right ], rw this at hy, exact ⟨le_trans h.1.1 hy.1, lt_of_lt_of_le hy.2 h.1.2⟩ end⟩⟩⟩ theorem length_subgrid {bb} {H} : length ℘(subgrid g bb H) = bb_size bb := by simp [length_generate_eq_size, size, bb_size, subgrid, rows, cols] lemma gen_subgrid_self : ℘ (subgrid g (grid_bounds g) (overlaid_by_refl _)) = @generate _ _ g := begin have lenok : length ℘(subgrid g (grid_bounds g) (overlaid_by_refl _)) = length ℘ g, by simp [ length_subgrid, length_generate, bb_size, grid_bounds, subgrid, rows, cols, rows_of_box, cols_of_box, rows_eq_try_sub_bly, cols_eq_trx_sub_blx ], have eq₁ : rows_of_box (grid_bounds g) = rows g, by simp [grid_bounds, rows_of_box, rows_eq_try_sub_bly], have eq₂ : cols_of_box (grid_bounds g) = cols g, by simp [grid_bounds, cols_of_box, cols_eq_trx_sub_blx], have eq₃ : (grid_bounds g).p₁ = bl g, by simp [grid_bounds], unfold subgrid, apply list.ext_le lenok, intros, unfold subgrid, repeat { rw nth_generate }, simp [abs_data_eq_nth_f₀, bl, eq₃, cols, eq₁, eq₂] end lemma overlaid_by_subgrid_grid {α : Type*} [grid α] {g : α} {bb} {H} : overlaid_by (grid_bounds (subgrid g bb H)) (grid_bounds g) := begin unfold overlaid_by at H, rcases bb with ⟨p₁, p₂, hbb⟩, rw grid_bounded_iff at hbb, cases hbb with hbb₁ hbb₂, rcases H with ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩, simp [grid_bounds, overlaid_by, subgrid, expand_gtr] at *, split; split; try { simp [bl, expand_gtr, rows, cols, rows_of_box, cols_of_box, -sub_eq_add_neg] }, {assumption}, { have : p₂.x - p₁.x ≥ 0, by linarith, rw nat_abs_of_nonneg this, linarith }, { have : p₂.y - p₁.y ≥ 0, by linarith, rw nat_abs_of_nonneg this, linarith }, {assumption} end lemma bl_subgrid_g_bb_eq_bb_p₁ {α : Type*} [grid α] {g : α} {bb} {H} : bl (subgrid g bb H) = bb.p₁ := by simp [subgrid, bl] lemma tr_subgrid_g_bb_eq_bb_p₂ {α : Type*} [grid α] {g : α} {bb} {H} : gtr (subgrid g bb H) = bb.p₂ := begin simp [expand_gtr, subgrid, bl, rows, cols], have eq₁ : cols_of_box bb > 0, from cols_of_box_pos, have eq₂ : rows_of_box bb > 0, from rows_of_box_pos, rcases bb with ⟨⟨p₁x, p₁y⟩, ⟨p₂x, p₂y⟩, h⟩, rw grid_bounded_iff at h, simp [-sub_eq_add_neg, rows_of_box, cols_of_box], simp [-sub_eq_add_neg, cols_of_box] at eq₁, simp [-sub_eq_add_neg, rows_of_box] at eq₂, simp at h, cases h with hl hr, rw ← int.coe_nat_lt_coe_nat_iff at eq₁ eq₂, rw nat_abs_of_nonneg at eq₁ eq₂, all_goals { try { linarith } }, repeat { rw nat_abs_of_nonneg }, all_goals { try { linarith } }, split; linarith end lemma grid_bounds_subgrid_g {α : Type*} [grid α] {g : α} {bb} {H} : grid_bounds (subgrid g bb H) = bb := by simp [grid_bounds, bl_subgrid_g_bb_eq_bb_p₁, tr_subgrid_g_bb_eq_bb_p₂]; cases bb; refl lemma size_subgrid {bb} {H} : size (subgrid g bb H) = bb_size bb := by simp only [ size, bb_size, rows_of_box, cols_of_box, rows_eq_try_sub_bly, cols_eq_trx_sub_blx, tr_subgrid_g_bb_eq_bb_p₂, bl_subgrid_g_bb_eq_bb_p₁ ] lemma overlaid_by_subgrid_bb {α : Type*} [grid α] {g : α} {bb} {H} : overlaid_by bb (grid_bounds (subgrid g bb H)) := by rw grid_bounds_subgrid_g; exact overlaid_by_refl bb lemma p_in_bb_of_grid_bounds_subgrid_bb_iff {α : Type*} [grid α] {g : α} {bb} {H} {p} : p ∈ grid_bounds (subgrid g bb H) ↔ p ∈ bb := begin split; intros h, { simp [grid_bounds, flip, is_in_grid] at h, simp [(∈), flip, is_in_grid], rw [bl_subgrid_g_bb_eq_bb_p₁, tr_subgrid_g_bb_eq_bb_p₂] at h, exact h }, { simp [grid_bounds, flip, is_in_grid], simp [(∈), flip, is_in_grid] at h, rw [bl_subgrid_g_bb_eq_bb_p₁, tr_subgrid_g_bb_eq_bb_p₂], exact h } end def inject_filter_bounded {bb : bounding_box} (e : {x // x ∈ filter (λ (p : point), p ∉ bb) (gip_g g)}) : grid_point g := grid_point_of_mem _ $ in_grid_iff_in_gip_g.2 (mem_filter.1 e.2).1 def subgrid' (bb : bounding_box) (h : overlaid_by bb (grid_bounds g)) : list (carrier α) := map (abs_data g ∘ inject_filter_bounded _) (attach $ filter (λp, p ∉ bb) (gip_g g)) lemma countp_grp_x {p₁x p₂x a b row} : countp (λ (p : point), p.x < p₁x ∨ p₂x ≤ p.x) (grp a b row) = countp (λ x, x < p₁x ∨ p₂x ≤ x) (range_pure a b) := begin induction eq : range_pure a b with hd tl ih generalizing a, { rw range_pure_empty_iff at eq, rw grp_empty_iff'.2 eq, refl }, { have : a < b, { by_contradiction contra, rw not_lt at contra, rw range_pure_empty_iff.2 contra at eq, cases eq }, rw range_pure_next this at eq, injection eq with eq₁ eq₂, subst eq₁, specialize @ih (a + 1) eq₂, rw expand_grp this, simp [countp], by_cases eq₁ : a < p₁x ∨ p₂x ≤ a; simp [eq₁, ih] } end lemma countp_grp_single_row_l {α : Type} {l : list α} {P₁ P₂ : α → Prop} [decidable_pred P₁] [decidable_pred P₂] (h : ∀x ∈ l, ¬P₂ x) : countp P₁ l = countp (λ x, P₁ x ∨ P₂ x) l := begin induction l with hd tl ih, {simp}, { simp [countp], by_cases eq : (P₁ hd); simp [eq], {exact ih (λ_ h₀, h _ (mem_cons_of_mem _ h₀))}, { rw if_neg (h _ _), exact ih (λ_ h₀, h _ (mem_cons_of_mem _ h₀)), exact mem_cons_self _ _ } } end lemma countp_grp_single_row_r {α : Type} {l : list α} {P₁ P₂ : α → Prop} [decidable_pred P₁] [decidable_pred P₂] (h : ∀x ∈ l, ¬P₂ x) : countp P₁ l = countp (λ x, P₂ x ∨ P₁ x) l := begin induction l with hd tl ih, {simp}, { simp [countp], by_cases eq : (P₁ hd); simp [eq], {exact ih (λ_ h₀, h _ (mem_cons_of_mem _ h₀))}, { rw if_neg (h _ _), exact ih (λ_ h₀, h _ (mem_cons_of_mem _ h₀)), exact mem_cons_self _ _ } } end lemma countp_grp_row {p₁x p₁y p₂x p₂y : ℤ} {blx bly trx : ℤ} (h₀ : p₁y < p₂y) (hl : bly ≤ p₁y) : countp (λ (p : point), (p.y < p₁y ∨ p₂y ≤ p.y) ∨ p.x < p₁x ∨ p₂x ≤ p.x) (grp blx trx bly) = ite (p₁y = bly) (countp (λ (p : point), p.x < p₁x ∨ p₂x ≤ p.x) (grp blx trx bly)) (length (grp blx trx bly)) := begin by_cases h₁ : p₁y = bly, { have : ∀p : point, p ∈ grp blx trx bly → p.y = bly, from λp, in_grp_second, rw if_pos h₁, rw ← countp_grp_single_row_r, intros p h₂, rw not_or_distrib, subst h₁, by_cases h : blx < trx, { rw expand_grp h at this, have eq : p.y = p₁y, from in_grp_second h₂, subst eq, split; linarith }, { rw not_lt at h, rw grp_empty_iff'.2 h at h₂, cases h₂ } }, { rw if_neg h₁, have eq : ∀p : point, p ∈ grp blx trx bly → p.y = bly, from λp, in_grp_second, have eq₁ : bly < p₁y, { rw le_iff_eq_or_lt at hl, cases hl, subst hl, contradiction, exact hl }, induction iheq : grp blx trx bly with hd tl ih generalizing blx, {simp}, { have eq₂ : blx < trx, { by_contradiction contra, rw not_lt at contra, rw grp_empty_iff'.2 contra at iheq, cases iheq }, rw expand_grp eq₂ at iheq, injection iheq with i₁ i₂, simp [countp], subst i₁, simp only [point.x, point.y] at *, by_cases h₃ : (bly < p₁y ∨ p₂y ≤ bly) ∨ blx < p₁x ∨ p₂x ≤ blx; simp [h₃], { simp [nat.succ_eq_add_one], rw expand_grp eq₂ at eq, rw @ih (blx + 1) (λ_ hk, eq _ (mem_cons_of_mem _ hk)) i₂ }, { repeat { rw not_or_distrib at h₃ }, rcases h₃ with ⟨⟨a₁, a₂⟩, a₃, a₄⟩, linarith } } } end lemma in_join_grp_range_iff {blx trx bly try} {p : point} (h₀ : blx < trx) : p ∈ join (map (grp blx trx) (range_pure bly try)) ↔ is_bounded blx trx p.x ∧ is_bounded bly try p.y := begin split; intros h, { simp at h, rcases h with ⟨l, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩, rw in_range_pure_iff at h₁, rw [← h₂, in_grp_iff h₀] at h₃, cases h₃ with h₃ h₄, rw ← h₄ at h₁, exact ⟨h₃, h₁⟩ }, { cases h with h h₁, simp, use (grp blx trx p.y), use p.y, split, {exact in_range_pure_iff.2 h₁}, {refl}, rw in_grp_iff h₀, split, {exact h}, {refl} } end private lemma filter_any_ {l₁ l₂ : list ℤ} {p₁x p₂x} (h : length l₁ = length l₂) : length (filter (λ (x : point), x.x < p₁x ∨ p₂x ≤ x.x) (map (uncurry point.mk) (zip l₁ l₂))) = length (filter (λ (x : ℤ), x < p₁x ∨ p₂x ≤ x) l₁) := begin induction l₁ with hd tl ih generalizing l₂, {simp}, { cases l₂ with hd₂ tl₂, {simp at h, contradiction}, { simp [uncurry] at *, by_cases eq : hd < p₁x ∨ p₂x ≤ hd; simp [eq]; exact ih h } } end lemma countp_grp {p₁x p₂x a b r : ℤ} (h : p₁x < p₂x) (h₁ : a ≤ p₁x) (h₂ : p₂x ≤ b): countp (λ (x : point), x.x < p₁x ∨ p₂x ≤ x.x) (grp a b r) = nat_abs (p₁x - a) + nat_abs (b - p₂x) := begin unfold grp, rw countp_eq_length_filter, rw filter_any_, rw ← countp_eq_length_filter, rw countp_range_pure h h₁ h₂, rw range_length_pure, rw length_repeat, linarith end lemma map_length_grp_range {a b c d : ℤ} (h : c ≤ d) (h₁ : a < b) : length (join (map (grp a b) (range_pure c d))) = |d - c| * |b - a| := begin generalize eq : range_pure c d = l, rw le_iff_eq_or_lt at h, cases h with h h, { subst h, rw range_pure_same_empty at eq, subst eq, simp }, { induction l with hd tl ih generalizing c, { rw range_pure_empty_iff at eq, have : c = d, by linarith, simp [this] }, { rw range_pure_next h at eq, injection eq with eq eq₁, simp only [map, join, length_append], rw length_grp h₁, by_cases eq₂ : c + 1 < d, { rw @ih (c + 1) eq₁ eq₂, rw ← abs_minus_plus h, rw ← int.coe_nat_eq_coe_nat_iff, rw int.coe_nat_add, repeat { rw int.coe_nat_mul }, rw int.coe_nat_sub, repeat { rw int.nat_abs_of_nonneg }; try { linarith }, simp only [int.coe_nat_zero, int.coe_nat_succ, zero_add], ring, rw ← int.coe_nat_le_coe_nat_iff, rw int.nat_abs_of_nonneg; try { linarith }, simp, linarith }, { rw not_lt at eq₂, have : c + 1 = d, by linarith, subst this, rw range_pure_same_empty at eq₁, subst eq₁, simp } } } end lemma count_outside_join_map {a b c d p₁x p₂x} (h : c < d) (h₁ : p₁x < p₂x) (h₂ : a ≤ p₁x) (h₃ : p₂x ≤ b) : countp (λ (p : point), p.x < p₁x ∨ p₂x ≤ p.x) (join (map (grp a b) (range_pure c d))) = |p₁x - a| * |d - c| + |b - p₂x| * |d - c| := begin generalize eq : range_pure c d = l, induction l with hd tl ih generalizing c, {rw range_pure_empty_iff at eq, linarith}, { by_cases eq₁ : c + 1 < d, { rw range_pure_next h at eq, injection eq with eq eq₂, simp [-sub_eq_add_neg], rw countp_grp; try { linarith }, rw @ih (c + 1) eq₁ eq₂, repeat { rw ← abs_minus_plus h }, rw ← int.coe_nat_eq_coe_nat_iff, repeat { rw int.coe_nat_add }, repeat { rw int.coe_nat_mul }, repeat { rw int.coe_nat_sub }, simp only [int.coe_nat_zero, int.coe_nat_succ, zero_add], repeat { rw int.nat_abs_of_nonneg }; try { linarith }, ring, rw ← int.coe_nat_le_coe_nat_iff, rw int.nat_abs_of_nonneg, simp, linarith, linarith }, { rw not_lt at eq₁, rw range_pure_next h at eq, have : d = c + 1, by linarith, subst this, injection eq with eq eq₂, rw range_pure_same_empty at eq₂, subst eq₂, simp [-sub_eq_add_neg], rw countp_grp; try { linarith }, simp } } end lemma subgrid_smaller_ints {blx bly trx try p₁x p₁y p₂x p₂y} (h₀ : p₁x < p₂x) (h₁ : p₁y < p₂y) (h₂ : blx ≤ p₁x) (h₃ : p₂x ≤ trx) (h₄ : bly ≤ p₁y) (h₅ : p₂y ≤ try) : |p₂y - p₁y| * |p₂x - p₁x| ≤ |try - bly| * |trx - blx| := begin rw ← int.coe_nat_le_coe_nat_iff, repeat { rw int.coe_nat_mul }, repeat { rw int.nat_abs_of_nonneg }; try { linarith }, apply mul_le_mul; linarith end private lemma count_notin_ {p₁x p₁y p₂x p₂y : ℤ} {blx bly : ℤ} {c r : ℕ} (cpos : c > 0) (rpos : r > 0) (h₃ : blx ≤ p₁x) (h₄ : p₂x ≤ blx + c) (h₅ : bly ≤ p₁y) (h₆ : p₂y ≤ bly + r) (h₇ : p₁x < p₂x) (h₈ : p₁y < p₂y) : countp (λ (p : point), ((p.y < p₁y ∨ p₂y ≤ p.y) ∨ p.x < p₁x ∨ p₂x ≤ p.x)) (join (map (grp blx (blx + ↑c)) (range_pure (bly) (bly + ↑r)))) = r * c - |p₂y - p₁y| * |p₂x - p₁x| := begin generalize eq₁ : blx + ↑c = trx, generalize eq₂ : bly + ↑r = try, have eq₃ : ↑c = trx - blx, by linarith, have eq₄ : ↑r = try - bly, by linarith, rw ← @nat_abs_of_nonneg (trx - blx) at eq₃; try { linarith }, rw ← @nat_abs_of_nonneg (try - bly) at eq₄; try { linarith }, simp [-sub_eq_add_neg] at eq₃ eq₄, rw [eq₃, eq₄], generalize eq : range_pure bly try = l, have : ∃l₁ l₂, l = l₁ ++ l₂ ∧ l₁ = range_pure bly p₁y ∧ l₂ = range_pure p₁y try, from @range_pure_app bly try _ (by linarith) eq.symm _ h₅ (by linarith), rcases this with ⟨l₁, ⟨l₂, eq₂, eq₃, eq₄⟩⟩, have : ∃ (l₃ l₄ : list ℤ), l₂ = l₃ ++ l₄ ∧ l₃ = range_pure p₁y p₂y ∧ l₄ = range_pure p₂y try, from @range_pure_app p₁y try _ (by linarith) eq₄ _ (int.le_of_lt h₈) (by linarith), rcases this with ⟨l₃, ⟨l₄, eq₅, eq₆, eq₇⟩⟩, have eq₈ : l = l₁ ++ l₃ ++ l₄, by rw [eq₂, eq₅, append_assoc], rw eq₈, simp [-sub_eq_add_neg], rw [eq₃, eq₆, eq₇], rw countp_eq_length_filter, rw @filter_congr _ _ (λ_, true), swap 2, { intros, split; intros h₃, {trivial}, { rw in_join_grp_range_iff at H, unfold is_bounded at H, rcases H with ⟨_, ⟨_, c⟩⟩, left, left, exact c, linarith } }, rw filter_true, rw countp_eq_length_filter, rw @filter_congr _ _ (λp : point, p.x < p₁x ∨ p₂x ≤ p.x), swap 2, { intros, split; intros h₃, { rw in_join_grp_range_iff at H, unfold is_bounded at H, rcases H with ⟨⟨c₁, c₂⟩, ⟨c₃, c₄⟩⟩, cases h₃ with h₃ h₃; cases h₃ with h₃ h₃, {linarith}, {linarith}, {left, exact h₃}, {right, exact h₃}, {linarith} }, {right, exact h₃} }, rw ← countp_eq_length_filter, generalize c₁ : countp (λ (p : point), p.x < p₁x ∨ p₂x ≤ p.x) (join (map (grp blx trx) (range_pure p₁y p₂y))) = n₁, rw countp_eq_length_filter, rw @filter_congr _ _ (λ_, true), swap 2, { intros, split; intros h₃, {trivial}, { rw in_join_grp_range_iff at H, unfold is_bounded at H, rcases H with ⟨_, ⟨c, _⟩⟩, left, right, exact c, linarith } }, rw filter_true, repeat { rw map_length_grp_range }; try { linarith }, rw ← c₁, rw count_outside_join_map; try { linarith }, rw ← int.coe_nat_eq_coe_nat_iff, repeat { rw int.coe_nat_add }, repeat { rw int.coe_nat_mul }, repeat { rw int.coe_nat_sub }, repeat { rw int.coe_nat_mul }, repeat { rw int.nat_abs_of_nonneg }; try { linarith }, ring, apply subgrid_smaller_ints; linarith end lemma count_notin {α : Type*} [φ : grid α] {g : α} {bb} {H} : countp (λ (p : point), ¬is_in_grid bb p) (gip_g g) = size g - size (subgrid g bb H) := begin rcases bb with ⟨⟨p₁x, p₁y⟩, ⟨p₂x, p₂y⟩, p₃⟩, simp [grid_bounded_iff] at p₃, simp only [overlaid_by, grid_bounds, expand_gtr] at H, simp only [size_subgrid], simp only [is_in_grid, is_bounded, not_and_distrib, not_le, not_lt], simp only [gip_g, bb_size, rows_of_box, cols_of_box, expand_gtr, gip, size], rw ← @count_notin_ p₁x p₁y p₂x p₂y (bl g).x (bl g).y (cols g) (rows g) cols_pos rows_pos H.1.1 H.1.2 H.2.2 H.2.1 p₃.1 p₃.2, finish end lemma length_subgrid' {α : Type*} [grid α] {g : α} {bb} {H} : length (subgrid' g bb H) = size g - size (subgrid g bb H) := begin simp only [ subgrid', size_eq_rows_mul_cols, map, length, length_attach, length_map, flip ], rw ← countp_eq_length_filter, exact count_notin end -- lemma gip_g_subgrid'_eq_gip_g {α : Type*} [grid α] {g : α} {bb} {H} : -- gip_g (subgrid' g bb H) = gip_g g := by unfold gip_g; congr -- lemma grid_bounds_subgrid'_grid {α : Type*} [grid α] {g : α} {bb} {H} : -- grid_bounds (subgrid' g bb H) = grid_bounds g := -- by simp [subgrid', grid_bounds, expand_gbl, expand_gtr, bl, cols, rows] -- lemma bl_g_subgrid'_eq_bl_g_grid {α : Type*} [grid α] {g : α} {bb} {H} : -- bl (subgrid' g bb H) = bl g := -- let bounds := (@grid_bounds_subgrid'_grid _ _ g bb H) in -- by simp [grid_bounds] at bounds; exact bounds.1 -- lemma tr_g_subgrid'_eq_tr_g_grid {α : Type*} [grid α] {g : α} {bb} {H} : -- gtr (subgrid' g bb H) = gtr g := -- let bounds := (@grid_bounds_subgrid'_grid _ _ g bb H) in -- by simp [grid_bounds] at bounds; exact bounds.2 -- lemma overlaid_by_subgrid_subgrid' {α : Type*} [grid α] {g : α} {bb} {H} : -- overlaid_by (grid_bounds (subgrid g bb H)) (grid_bounds (subgrid' g bb H)) := -- by simp [grid_bounds_subgrid'_grid, overlaid_by_subgrid_grid] lemma p_in_g_iff_p_in_grid_bounds_g {α : Type*} [grid α] {g : α} {p} : p ∈ g ↔ p ∈ grid_bounds g := begin split; intros h, { simp [grid_bounds, flip, is_in_grid], simp [(∈), flip, is_in_grid'] at h, exact h }, { simp [(∈), flip, is_in_grid'], simp [grid_bounds, flip, is_in_grid] at h, exact h } end private lemma abs_data_none_of_subgrid_ {p} {bb} {H} (h : p ∈ gip_g (subgrid g bb H)) : p ∈ grid_bounds g := begin have overlaid : overlaid_by (grid_bounds (subgrid g bb H)) (grid_bounds g), from overlaid_by_subgrid_grid, rw [← in_grid_iff_in_gip_g, ← in_grid_iff_in_grid_bounds] at h, exact is_in_larger h overlaid end private def modify_vec {α : Type} {m} (v : vector α m) (n : ℕ) (x : α) : vector α m := ⟨update_nth v.to_list n x, by simp [update_nth_pres_len, *]⟩ def modify_at {α : Type} (p : point) (x : α) (g : vec_grid₀ α) : vec_grid₀ α := if h : p ∈ g then let ⟨r, c⟩ := relpoint_of_gpoint $ @grid_point.mk _ _ g ⟨p.y, by simp only [(∈)] at h; exact h.left⟩ ⟨p.x, by simp only [(∈)] at h; exact h.right⟩ in ⟨⟨g.r, g.c, g.h, modify_vec g.data (r * g.c + c) x⟩, g.o⟩ else g def modify_many {α : Type} (l : list (point × α)) (g : vec_grid₀ α) : vec_grid₀ α := foldr (uncurry modify_at) g l def count_grid {α : Type} [grid α] [decidable_eq (carrier α)] (g : α) (x : carrier α) := list.count x (℘ g) lemma gen_aof_eq_gen {α : Type} {g : fgrid₀ α} : ℘ (vec_grid₀_of_fgrid₀ g) = @generate _ ag_fgrid₀ g := by simp [vec_grid₀_of_fgrid₀, generate_eq_data] private theorem generate_inj_a_f {α : Type} {g₁ : vec_grid₀ α} {g₂ : fgrid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) (h : ℘ g₁ = @generate (fgrid₀ α) _ g₂) : g₁ = g₂ := begin have hl₁ : length (℘ g₁) = g₁.r * g₁.c, from length_generate _, have hl₂ : length (℘ g₂) = g₂.r * g₂.c, from length_generate _, rcases g₁ with ⟨⟨g₁r, g₁c, g₁h, ⟨g₁dv, g₁dh⟩⟩, g₁o⟩, cases g₂ with g₂r g₂c g₂h g₂o g₂d, dsimp at hrows hcols horig hl₁ hl₂, subst hrows, subst hcols, subst horig, unfold_coes, simp [vec_grid₀_of_fgrid₀, h.symm, generate_eq_data] end lemma gen_foa_eq_gen {α : Type} {g : vec_grid₀ α} : ℘ (fgrid₀_of_vec_grid₀ g) = @generate (vec_grid₀ α) _ g := begin have hl₁ : length (℘ g) = rows g * cols g, from length_generate _, have hl₂ : length (℘ (fgrid₀_of_vec_grid₀ g)) = rows g * cols g, from length_generate _, simp [fgrid₀_of_vec_grid₀] at *, apply list.ext_le (hl₂.trans hl₁.symm) (λi hi₁ hi₂, _), simp [ nth_le_generate_f₀, nth_generate, abs_data_eq_nth_v₀', abs_data_eq_nth_f₀, tl, bl, rows, cols, expand_gtr ] end private theorem generate_inj_f₀_v₀ {α : Type} {g₁ : fgrid₀ α} {g₂ : vec_grid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) (h : ℘ g₁ = @generate (fgrid₀ α) _ g₂) : g₁ = g₂ := generate_inj_f₀_f₀ hrows hcols horig h theorem grid_eq_iff_v₀_f₀ {α : Type} {g₁ : vec_grid₀ α} {g₂ : fgrid₀ α} (h₁ : g₁.r = g₂.r) (h₂ : g₁.c = g₂.c) (h₃ : g₁.o = g₂.o) : g₁ = g₂ ↔ ℘ g₁ = ℘ g₂ := ⟨λh, h ▸ rfl, λh, generate_inj_a_f h₁ h₂ h₃ $ by rwa gen_aof_eq_gen.symm⟩ theorem grid_eq_iff_f₀_v₀ {α : Type} {g₁ : fgrid₀ α} {g₂ : vec_grid₀ α} (h₁ : g₁.r = g₂.r) (h₂ : g₁.c = g₂.c) (h₃ : g₁.o = g₂.o) : g₁ = g₂ ↔ ℘ g₁ = ℘ g₂ := ⟨λh, h ▸ rfl, λh, generate_inj_f₀_v₀ h₁ h₂ h₃ h⟩ @[ext] theorem grid_eq_ext_v₀_v₀ {α : Type} {g₁ g₂ : vec_grid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : ℘ g₁ = ℘ g₂ → g₁ = g₂ := (grid_eq_iff_v₀_v₀ hrows hcols horig).2 @[ext] theorem grid_eq_ext_f₀_f₀ {α : Type} {g₁ g₂ : fgrid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : ℘ g₁ = ℘ g₂ → g₁ = g₂ := (grid_eq_iff_f₀_f₀ hrows hcols horig).2 @[ext] theorem grid_eq_ext_v₀_f₀ {α : Type} {g₁ : vec_grid₀ α} {g₂ : fgrid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : ℘ g₁ = ℘ g₂ → g₁ = g₂ := (grid_eq_iff_v₀_f₀ hrows hcols horig).2 @[ext] theorem grid_eq_ext_f₀_v₀ {α : Type} {g₁ : fgrid₀ α} {g₂ : vec_grid₀ α} (hrows : g₁.r = g₂.r) (hcols : g₁.c = g₂.c) (horig : g₁.o = g₂.o) : ℘ g₁ = ℘ g₂ → g₁ = g₂ := (grid_eq_iff_f₀_v₀ hrows hcols horig).2 lemma nth_vecgrid_of_fgrid {α : Type} {g : fgrid₀ α} {n} : list.nth (vec_grid₀_of_fgrid₀ g).data.val n = list.nth (℘ g) n := by delta vec_grid₀_of_fgrid₀; simp instance decidable_eq_v₀_v₀ {α : Type} [decidable_eq α] : decidable_eq (vec_grid₀ α) := λg₁ g₂, if h : g₁.r = g₂.r ∧ g₁.c = g₂.c ∧ g₁.o = g₂.o then by simp [grid_eq_iff_v₀_v₀, *]; apply_instance else is_false $ by finish instance decidable_eq_f₀_f₀ {α : Type} [decidable_eq α] : decidable_eq (fgrid₀ α) := λg₁ g₂, if h : g₁.r = g₂.r ∧ g₁.c = g₂.c ∧ g₁.o = g₂.o then by simp [grid_eq_iff_f₀_f₀, *]; apply_instance else is_false $ by finish instance decidable_eq_v₀_f₀ {α : Type} [decidable_eq α] {g₁ : vec_grid₀ α} {g₂ : fgrid₀ α} : decidable (g₁ = g₂) := if h : g₁.r = g₂.r ∧ g₁.c = g₂.c ∧ g₁.o = g₂.o then by simp [grid_eq_iff_v₀_f₀, *]; apply_instance else is_false $ by finish instance decidable_eq_f₀_v₀ {α : Type} [decidable_eq α] {g₁ : fgrid₀ α} {g₂ : vec_grid₀ α} : decidable (g₁ = g₂) := if h : g₁.r = g₂.r ∧ g₁.c = g₂.c ∧ g₁.o = g₂.o then by simp [grid_eq_iff_f₀_v₀, *]; apply_instance else is_false $ by finish lemma subgrid_self {α : Type} {g : vec_grid₀ α} {bb : bounding_box} (h : bb = {bounding_box. p₁ := bl g, p₂ := gtr g, h := grid_is_bounding_box }) : subgrid g bb begin unfold grid_bounds, rw h, exact overlaid_by_refl _ end = g := begin rcases g with ⟨⟨r, c, h, ⟨d, hd⟩⟩, o⟩, simp [h, subgrid], unfold_coes, rw grid_eq_iff_f₀_f₀; try { simp [cols_of_box, bl, expand_gtr, cols] }; try { simp }; try { simp [rows_of_box, bl, expand_gtr, rows] }, rw gen_foa_eq_gen, apply ext_le, { simp [ length_generate_eq_size, size, rows, cols, rows_of_box, cols_of_box, bl, expand_gtr ] }, { intros, rw nth_le_generate_f₀, simp only [ nth_generate, abs_data, data, expand_gtr, bl, (∘), relpoint_of_gpoint, prod_of_rel_point, rows, cols, tl, rows_of_box, cols_of_box ], simp } end lemma p_in_g_iff_v₀_f₀ {α : Type} {g₁ : vec_grid₀ α} {g₂ : fgrid₀ α} {p} (h₁ : g₁.r = g₂.r) (h₂ : g₁.c = g₂.c) (h₃ : g₁.o = g₂.o) : p ∈ g₁ ↔ p ∈ g₂ := begin rcases g₁ with ⟨⟨r₁, c₁, gh₁, d₁⟩, o₁⟩, rcases g₂ with ⟨r₂, c₂, gh₂, o₂, d₂⟩, simp [flip, is_in_grid'] at *, split; intros; unfold_projs at *; subst h₁; subst h₂; subst h₃; finish end lemma cols_subgrid {bb} {H} : cols (subgrid g bb H) = cols_of_box bb := rfl lemma rows_subgrid {bb} {H} : rows (subgrid g bb H) = rows_of_box bb := rfl private lemma expand_repeat_app {α : Type} {l₁ l₂ l₃ : list α} {r : ℤ} : repeat r (length (l₁ ++ l₂ ++ l₃)) = repeat r (length l₁) ++ repeat r (length l₂) ++ repeat r (length l₃) := by simp [repeat_add] private lemma expand_map_zip_repeat {l₁ l₂ l₃} {r : ℤ} : map (uncurry point.mk) (zip (l₁ ++ l₂ ++ l₃) (repeat r (length (l₁ ++ l₂ ++ l₃)))) = map (uncurry point.mk) (zip l₁ (repeat r (length l₁))) ++ map (uncurry point.mk) (zip l₂ (repeat r (length l₂))) ++ map (uncurry point.mk) (zip l₃ (repeat r (length l₃))) := begin repeat { rw ← map_append }, congr, rw expand_repeat_app, repeat { rw zip_append }, rw length_repeat, simp end lemma filter_cols_grp_range_pure {p₁x p₂x gbl gtr r} (h : p₁x < p₂x) (h₁ : gbl ≤ p₁x) (h₂ : p₂x ≤ gtr) : filter (λ (p : point), p₁x ≤ p.x ∧ p.x < p₂x) (grp gbl gtr r) = grp p₁x p₂x r := begin unfold grp, generalize eq : range_pure gbl gtr = l, have : ∃l₁ l₂, l = l₁ ++ l₂ ∧ l₁ = range_pure gbl p₁x ∧ l₂ = range_pure p₁x gtr, from @range_pure_app gbl gtr _ (by linarith) eq.symm _ h₁ (by linarith), rcases this with ⟨l₁, ⟨l₂, eq₁, eq₂, eq₃⟩⟩, have : ∃l₃ l₄, l₂ = l₃ ++ l₄ ∧ l₃ = range_pure p₁x p₂x ∧ l₄ = range_pure p₂x gtr, from @range_pure_app p₁x gtr _ (by linarith) eq₃ _ (int.le_of_lt h) h₂, rcases this with ⟨l₃, ⟨l₄, eq₄, eq₅, eq₆⟩⟩, rw [eq₁, eq₄], rw ← eq₅, rw ← append_assoc, have : length (l₁ ++ l₃ ++ l₄) = |gtr - gbl|, { have : l = l₁ ++ l₃ ++ l₄, by simp [append_assoc, *], rw ← this, rw ← eq, apply range_length_pure, linarith }, rw ← this, rw expand_map_zip_repeat, simp, rw eq₂, rw @filter_congr _ _ (λ_, false), swap 2, { intros, split; intros H₁, cases x with x y, have eq₇ : x ∈ range_pure gbl p₁x ∧ y ∈ repeat r (length (range_pure gbl p₁x)), from in_zip_of H, cases eq₇ with eq₇ eq₈, simp at H₁, rw in_range_pure_iff at eq₇, have : y = r, from eq_of_mem_repeat eq₈, subst this, unfold is_bounded at eq₇, cases H₁, cases eq₇, linarith, contradiction }, rw [filter_false, nil_append], rw eq₅, rw @filter_congr _ _ (λ_, true), swap 2, { intros, split; intros H₁, trivial, cases x with x y, have eq₇ : x ∈ range_pure p₁x p₂x ∧ y ∈ repeat r (length (range_pure p₁x p₂x)), from in_zip_of H, cases eq₇ with eq₇ eq₈, rw in_range_pure_iff at eq₇, have : y = r, from eq_of_mem_repeat eq₈, subst this, exact eq₇ }, rw filter_true, rw eq₆, rw @filter_congr _ _ (λ_, false), swap 2, { intros, split; intros H₁, cases x with x y, have eq₇ : x ∈ range_pure p₂x gtr ∧ y ∈ repeat r (length (range_pure p₂x gtr)), from in_zip_of H, cases eq₇ with eq₇ eq₈, simp at H₁, rw in_range_pure_iff at eq₇, have : y = r, from eq_of_mem_repeat eq₈, subst this, unfold is_bounded at eq₇, cases H₁, cases eq₇, linarith, contradiction }, rw [filter_false, append_nil], rw range_length_pure, simp, linarith end private lemma filter_bbox_gip {p₁x p₂x gbl gtr} {l} (h : p₁x < p₂x) (h₁ : gbl ≤ p₁x) (h₂ : p₂x ≤ gtr) : filter (λ (p : point), p₁x ≤ p.x ∧ p.x < p₂x) (join (map (grp gbl gtr) l)) = join (map (grp p₁x p₂x) l) := begin induction l with hd tl ih, {simp}, {simp, rw ih, rw filter_cols_grp_range_pure; linarith} end lemma filter_generate_subgrid {α : Type*} [grid α] {g : α} {bb} {H} : filter (λ (p : point), p ∈ bb) (gip_g g) = gip_g (subgrid g bb H) := begin rcases bb with ⟨p₁, p₂, hbb⟩, rw grid_bounded_iff at hbb, cases hbb with hbb₁ hbb₂, unfold gip_g gip, rw bl_subgrid_g_bb_eq_bb_p₁, rw tr_subgrid_g_bb_eq_bb_p₂, simp at *, generalize eq : range_pure p₁.y p₂.y = l, generalize eq₁ : range_pure (bl g).y (gtr g).y = l₁, unfold overlaid_by at H, rcases H with ⟨⟨c₁, c₂⟩, c₃, c₄⟩, simp [grid_bounds] at *, have : ∃l₂ l₃, l₁ = l₂ ++ l₃ ∧ l₂ = range_pure (bl g).y p₁.y ∧ l₃ = range_pure p₁.y (gtr g).y, from @range_pure_app (bl g).y (gtr g).y _ blgy_lt_trgy eq₁.symm _ c₄ (by linarith), rcases this with ⟨l₂, ⟨l₃, eq₂, eq₃, eq₄⟩⟩, have : ∃l₄ l₅, l₃ = l₄ ++ l₅ ∧ l₄ = range_pure p₁.y p₂.y ∧ l₅ = range_pure p₂.y (gtr g).y, from @range_pure_app p₁.y (gtr g).y _ (lt_of_lt_of_le hbb₂ c₃) eq₄ _ (int.le_of_lt hbb₂) (by linarith), rcases this with ⟨l₄, ⟨l₅, eq₅, eq₆, eq₇⟩⟩, rw eq₂, rw eq₅, have eq₈ : l₄ = l, by cc, subst eq₈, simp only [flip, is_in_grid, is_bounded], simp, rw eq₃, rw eq₆, rw eq₇, rw @filter_congr _ _ (λ_, false), swap 2, intros, split; intros H₁, { rw in_join_grp_range_iff at H, unfold is_bounded at H, have contra₁ : p₁.y ≤ x.y, from H₁.1.1, have contra₂ : x.y < p₁.y, from H.2.2, linarith, exact gblx_lt_gtrx }, { contradiction }, rw [filter_false, nil_append], generalize protect : filter (λ (p : point), (p₁.y ≤ p.y ∧ p.y < p₂.y) ∧ p₁.x ≤ p.x ∧ p.x < p₂.x) (join (map (grp ((bl g).x) ((gtr g).x)) (range_pure (p₁.y) (p₂.y)))) = prot₁, rw @filter_congr _ _ (λ_, false), swap 2, intros, split; intros H₁, { rw in_join_grp_range_iff at H, unfold is_bounded at H, have contra₁ : x.y < p₂.y, from H₁.1.2, have contra₂ : p₂.y ≤ x.y, from H.2.1, linarith, exact gblx_lt_gtrx }, { contradiction }, rw [filter_false, append_nil], rw ← protect, rw @filter_congr _ _ (λ (p : point), p₁.x ≤ p.x ∧ p.x < p₂.x), swap 2, { intros, split; intros H₁, exact H₁.right, rw in_join_grp_range_iff at H, unfold is_bounded at H, exact ⟨H.2, H₁⟩, exact gblx_lt_gtrx }, { rw filter_bbox_gip; linarith } end lemma nth_le_filter_mem_gip {n} {bb : bounding_box} {H} (h : overlaid_by bb (grid_bounds g)) : nth_le (filter (λ (p : point), p ∈ bb) (gip_g g)) n H = {x := bb.p₁.x + ↑n % ↑(cols_of_box bb), y := bb.p₁.y + ↑n / ↑(cols_of_box bb)} := begin rw ← option.some_inj, rw ← nth_le_nth, rw @filter_generate_subgrid _ _ _ _ h, rw nth_gip_g, rw bl_subgrid_g_bb_eq_bb_p₁, refl, rw @filter_generate_subgrid _ _ _ _ h at H, exact H end lemma rows_of_overlaid_le {α : Type*} [grid α] {g : α} {bb} (H : overlaid_by bb (grid_bounds g)) : rows_of_box bb ≤ rows g := begin unfold rows_of_box, unfold overlaid_by grid_bounds at H, simp at H, rcases H with ⟨⟨h₁, h₂⟩, h₃, h₄⟩, rw rows_eq_try_sub_bly, rw ← int.coe_nat_le_coe_nat_iff, repeat { rw int.nat_abs_of_nonneg }, linarith, rw expand_gtr, simp, cases bb, simp at *, rw grid_bounded_iff at bb_h, cases bb_h, linarith end lemma cols_of_overlaid_le {α : Type*} [grid α] {g : α} {bb} (H : overlaid_by bb (grid_bounds g)) : cols_of_box bb ≤ cols g := begin unfold cols_of_box, unfold overlaid_by grid_bounds at H, simp at H, rcases H with ⟨⟨h₁, h₂⟩, h₃, h₄⟩, rw cols_eq_trx_sub_blx, rw ← int.coe_nat_le_coe_nat_iff, repeat { rw int.nat_abs_of_nonneg }, linarith, rw expand_gtr, simp, cases bb, simp at *, rw grid_bounded_iff at bb_h, cases bb_h, linarith end lemma size_subgrid_le_grid {α : Type*} [grid α] {g : α} {bb} {H} : size (subgrid g bb H) ≤ size g := begin rw size_subgrid, unfold bb_size size, apply mul_le_mul, exact rows_of_overlaid_le H, exact cols_of_overlaid_le H, exact le_of_lt cols_of_box_pos, exact le_of_lt rows_pos end private lemma filter_congr_heq {α : Type} {l₁ l₂ : list α} (h : l₁ = l₂) {P₁ : {x : α | x ∈ l₁} → Prop} {P₂ : {x : α | x ∈ l₂} → Prop} [decidable_pred P₁] [decidable_pred P₂] (h₁ : ∀x : α, x ∈ l₁ → (P₁ ⟨x, by simp [a]⟩ ↔ P₂ ⟨x, begin simp, rw ← h, exact a end⟩)) : length (filter P₁ (attach l₁)) = length (filter P₂ (attach l₂)) := begin resetI, subst h, have : P₁ = P₂, { apply funext, intros, cases x with x hx, simp at hx, simp, split; intros h, { specialize h₁ x hx, rw ← h₁, exact h }, { specialize h₁ x hx, rw h₁, exact h } }, subst this, finish end private lemma filter_congr_heq' {α : Type} {l₁ l₂ : list α} (h : l₁ = l₂) {P₁ : {x : α // x ∈ l₁} → Prop} {P₂ : {x : α // x ∈ l₂} → Prop} [decidable_pred P₁] [decidable_pred P₂] (h₁ : ∀x : α, x ∈ l₁ → (P₁ ⟨x, by simp [a]⟩ ↔ P₂ ⟨x, begin rw ← h, exact a end⟩)) : length (filter P₁ (pmap subtype.mk l₁ (λ_, id))) = length (filter P₂ (pmap subtype.mk l₂ (λ_, id))) := filter_congr_heq h h₁ lemma count_subgrid_in_bb {α : Type*} [grid α] [decidable_eq (carrier α)] (g : α) {bb} {H₁} {elem : carrier α} : count elem (@generate _ _ (subgrid g bb H₁)) = countp (λ (x : {x // x ∈ filter (λ (p : point), p ∈ bb) (gip_g g)}), elem = abs_data g { x := ⟨(x.val).x, begin rcases x with ⟨⟨x, y⟩, hx⟩, simp, rw mem_filter at hx, cases hx with hx₁ hx₂, rw ← in_grid_iff_in_gip_g at hx₁, exact hx₁.2 end⟩, y := ⟨(x.val).y, begin rcases x with ⟨⟨x, y⟩, hx⟩, simp, rw mem_filter at hx, cases hx with hx₁ hx₂, rw ← in_grid_iff_in_gip_g at hx₁, exact hx₁.1 end⟩}) (attach (filter (λ (p : point), p ∈ bb) (gip_g g))) := begin simp only [count, generate], repeat { rw countp_eq_length_filter }, rw filter_of_map, rw length_map, simp only [ (∘), inject_into_bounded, grid_point_of_prod, make_bounded, make_bounded_idx ], let eq := @filter_generate_subgrid _ _ g bb H₁, symmetry' at eq, apply filter_congr_heq eq, rintros ⟨x, y⟩ p, split; intros h; simp [h]; rw abs_data_eq_nth_f₀; delta subgrid; simp; refl end lemma filter_partition_dependent' {α : Type} [decidable_eq α] {l : list α} {Q : α → Prop} {R : α → Prop} [decidable_pred Q] [decidable_pred R] {β} {P : β → Prop} [decidable_pred P] (h : ∀x, ¬Q x ↔ R x) (p : α → Prop) (f : ∀ a, p a → β) {β₁} (i₁ : β₁ → β) (p₁ : α → Prop) (f₁ : ∀ a, p₁ a → β₁) {β₂} (i₂ : β₂ → β) (p₂ : α → Prop) (f₂ : ∀ a, p₂ a → β₂) : ∀ (H : ∀ (a : α), a ∈ l → p a) (H₁ : ∀ (a : α), a ∈ filter Q l → p₁ a) (H₂ : ∀ (a : α), a ∈ filter R l → p₂ a) (heq : ∀x (H₃ : x ∈ l) (H₄ : Q x), P (f x (H x H₃)) → P (i₁ (f₁ x (H₁ x (by simp [H₃, H₄]))))) (heq₁ : ∀x (H₃ : x ∈ l) (H₄ : R x), P (f x (H x H₃)) → P (i₂ (f₂ x (H₂ x (by simp [H₃, H₄]))))) (heq₂ : ∀x (H₃ : x ∈ l) (H₄ : Q x), ¬P (f x (H x H₃)) → ¬P (i₁ (f₁ x (H₁ x (by simp [H₃, H₄]))))) (heq₃ : ∀x (H₃ : x ∈ l) (H₄ : R x), ¬P (f x (H x H₃)) → ¬P (i₂ (f₂ x (H₂ x (by simp [H₃, H₄]))))), length (filter P (pmap f l H)) = length (filter (λ x, P (i₁ x)) (pmap f₁ (filter Q l) H₁)) + length (filter (λ x, P (i₂ x)) (pmap f₂ (filter R l) H₂)) := begin induction l with hd tl ih; intros, {simp}, { by_cases h₁ : P (f hd (H _ (mem_cons_self _ _))), { unfold pmap, rw filter_cons_of_pos _ h₁, rw length_cons, by_cases h₂ : Q hd, { simp [filter_cons_of_pos _ h₂, -add_comm], have : ¬R hd, by finish, simp [filter_cons_of_neg _ this, -add_comm], have h₃ : P (i₁ (f₁ hd (H₁ _ (by simp [h₂])))), { apply heq, exact h₂, exact h₁ }, simp [h₃], rw add_comm, apply ih, intros, apply heq, exact H₄, exact a, right, exact H₃, intros, apply heq₁, exact H₄, exact a, right, exact H₃, intros, apply heq₂, exact H₄, exact a, right, exact H₃, intros, apply heq₃, exact H₄, exact a, right, exact H₃ }, { simp [filter_cons_of_neg _ h₂, -add_comm], have : R hd, by finish, simp [filter_cons_of_pos _ this, -add_comm], have h₃ : P (i₂ (f₂ hd (H₂ _ (by simp [this])))), { apply heq₁, exact this, exact h₁ }, simp [h₃], apply ih, intros, apply heq, exact H₄, exact a, right, exact H₃, intros, apply heq₁, exact H₄, exact a, right, exact H₃, intros, apply heq₂, exact H₄, exact a, right, exact H₃, intros, apply heq₃, exact H₄, exact a, right, exact H₃ } }, { unfold pmap, rw filter_cons_of_neg _ h₁, by_cases h₂ : Q hd, { simp [filter_cons_of_pos _ h₂, -add_comm], have : ¬R hd, by finish, simp [filter_cons_of_neg _ this, -add_comm], have h₃ : ¬P (i₁ (f₁ hd (H₁ _ (by simp [h₂])))), { apply heq₂, exact h₂, exact h₁ }, simp [h₃], rw add_comm, apply ih, intros, apply heq, exact H₄, exact a, right, exact H₃, intros, apply heq₁, exact H₄, exact a, right, exact H₃, intros, apply heq₂, exact H₄, exact a, right, exact H₃, intros, apply heq₃, exact H₄, exact a, right, exact H₃ }, { simp [filter_cons_of_neg _ h₂, -add_comm], have : R hd, by finish, simp [filter_cons_of_pos _ this, -add_comm], have h₃ : ¬P (i₂ (f₂ hd (H₂ _ (by simp [this])))), { apply heq₃, exact this, exact h₁ }, simp [h₃], apply ih, intros, apply heq, exact H₄, exact a, right, exact H₃, intros, apply heq₁, exact H₄, exact a, right, exact H₃, intros, apply heq₂, exact H₄, exact a, right, exact H₃, intros, apply heq₃, exact H₄, exact a, right, exact H₃ } } } end private lemma count_partition_over_filter {α : Type} {l : list α} [decidable_eq α] {P : α → Prop} [decidable_pred P] {P₃ : α → Prop} [decidable_pred P₃] {P₅ : {x // x ∈ l} → Prop} [decidable_pred P₅] (h : ∀x, ¬P x ↔ P₃ x) : length (filter (λ (x : {x // x ∈ l}), P₅ x) (attach l)) = length (filter (λ (x : {x // x ∈ filter P l}), P₅ ⟨x.1, in_l_of_in_filter x.2⟩) (attach (filter P l))) + length (filter (λ (x : {x // x ∈ filter P₃ l}), P₅ ⟨x.1, in_l_of_in_filter x.2⟩) (attach (filter P₃ l))) := begin apply filter_partition_dependent', exact h, intros, exact a, intros, exact a, intros, exact a, intros, exact a end lemma count_split {α : Type*} [grid α] [decidable_eq (carrier α)] (g : α) (bb) (H₁) {elem : carrier α} : count elem (℘ g) = count elem (@generate _ _ (subgrid g bb H₁)) + count elem (subgrid' g bb H₁) := begin rw count_subgrid_in_bb, simp only [ generate, subgrid', comp, grid_point_of_prod, inject_into_bounded, make_bounded, make_bounded_idx, gbl, map, inject_filter_bounded, grid_point_of_mem ], simp only [ count, countp_eq_length_filter, filter_of_map, gbl, map, length, length_map ], simp only [(∘)], apply count_partition_over_filter, finish end end finite_grid section grid_instances open relative_grid def split_rows_cols : ℕ → ℕ → list string → list string | cols 0 ls := [""] | cols (k + 1) ls := list.take cols ls ++ ["\n"] ++ split_rows_cols cols k (list.drop cols ls) def grid_str {α : Type*} [grid α] [has_to_string (carrier α)] (g : α) : string := let points := list.map to_string $ ℘ g in " " ++ (list.foldr append "" $ list.intersperse " " $ split_rows_cols (cols g) (rows g) points) instance grid_repr {α : Type*} [grid α] [has_to_string (carrier α)] : has_repr α := ⟨grid_str⟩ instance grid_to_string {α : Type*} [grid α] [has_to_string (carrier α)] : has_to_string α := ⟨grid_str⟩ -- def test_grid : vec_grid₀ ℕ := -- vec_grid₀.mk ⟨4, 5, dec_trivial, -- ⟨[1, 2, 3, 4, 5, -- 1, 2, 3, 4, 5, -- 1, 2, 3, 4, 5, -- 1, 2, 3, 4, 5], rfl⟩⟩ ⟨0, 0⟩ -- def testbb : bounding_box := ⟨⟨0, 0⟩, ⟨2, 3⟩, sorry⟩ -- #eval list.filter (λx, x ∈ testbb) (gip_g test_grid) -- #eval gip_g (subgrid test_grid testbb sorry) -- #eval list.count 4 (℘ test_grid) -- #eval list.count 4 (℘ (subgrid test_grid testbb sorry)) -- #eval list.count 4 ((subgrid' test_grid testbb sorry)) -- #eval (subgrid' test_grid testbb sorry) -- -- 16, 17, 18, 19, 20 -- -- 11, 12, 13, 14, 16, -- -- 6, 7, 8, 9, 10, -- -- 1, 2, 3, 4, 5, -- #eval grid_bounds test_grid -- #eval subgrid' test_grid ⟨⟨1, 1⟩, ⟨4, 3⟩, sorry⟩ sorry end grid_instances
99594f59805d05eefc60fb2cb60ef58d3c21a778
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/support.lean
ac0a48cd703035c45c6776abeddd529a86c076f3
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,757
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import data.set.basic algebra.field order.conditionally_complete_lattice algebra.big_operators /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. -/ universes u v w x y open set namespace function variables {α : Type u} {β : Type v} {ι : Sort w} {A : Type x} {B : Type y} /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} lemma nmem_support [has_zero A] {f : α → A} {x : α} : x ∉ support f ↔ f x = 0 := classical.not_not lemma support_binop_subset [has_zero A] (op : A → A → A) (op0 : op 0 0 = 0) (f g : α → A) : support (λ x, op (f x) (g x)) ⊆ support f ∪ support g := λ x hx, classical.by_cases (λ hf : f x = 0, or.inr $ λ hg, hx $ by simp only [hf, hg, op0]) or.inl lemma support_add [add_monoid A] (f g : α → A) : support (λ x, f x + g x) ⊆ support f ∪ support g := support_binop_subset (+) (zero_add _) f g @[simp] lemma support_neg [add_group A] (f : α → A) : support (λ x, -f x) = support f := set.ext $ λ x, not_congr neg_eq_zero lemma support_sub [add_group A] (f g : α → A) : support (λ x, f x - g x) ⊆ support f ∪ support g := support_binop_subset (has_sub.sub) (sub_self _) f g @[simp] lemma support_mul [domain A] (f g : α → A) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [support, ne.def, mul_eq_zero, mem_set_of_eq, mem_inter_iff, not_or_distrib] @[simp] lemma support_mul' {A} [group_with_zero A] (f g : α → A) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [support, ne.def, mul_eq_zero_iff', mem_set_of_eq, mem_inter_iff, not_or_distrib] @[simp] lemma support_inv [division_ring A] (f : α → A) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [division_ring A] (f g : α → A) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] lemma support_sup [has_zero A] [semilattice_sup A] (f g : α → A) : support (λ x, (f x) ⊔ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊔) sup_idem f g lemma support_inf [has_zero A] [semilattice_inf A] (f g : α → A) : support (λ x, (f x) ⊓ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊓) inf_idem f g lemma support_max [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, max (f x) (g x)) ⊆ support f ∪ support g := support_sup f g lemma support_min [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, min (f x) (g x)) ⊆ support f ∪ support g := support_inf f g lemma support_supr [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨆ i, f i x) ⊆ ⋃ i, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, simp only [hx, csupr_const] end lemma support_infi [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨅ i, f i x) ⊆ ⋃ i, support (f i) := @support_supr _ _ (order_dual A) ⟨(0:A)⟩ _ _ f lemma support_sum [add_comm_monoid A] (s : finset α) (f : α → β → A) : support (λ x, s.sum (λ i, f i x)) ⊆ ⋃ i ∈ s, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, exact finset.sum_eq_zero hx end -- TODO: Drop `classical` once #2332 is merged lemma support_prod [integral_domain A] (s : finset α) (f : α → β → A) : support (λ x, s.prod (λ i, f i x)) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by classical; simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma support_comp [has_zero A] [has_zero B] (g : A → B) (hg : g 0 = 0) (f : α → A) : support (g ∘ f) ⊆ support f := λ x, mt $ λ h, by simp [(∘), *] lemma support_comp' [has_zero A] [has_zero B] (g : A → B) (hg : g 0 = 0) (f : α → A) : support (λ x, g (f x)) ⊆ support f := support_comp g hg f lemma support_comp_eq [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (g ∘ f) = support f := set.ext $ λ x, not_congr hg lemma support_comp_eq' [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (λ x, g (f x)) = support f := support_comp_eq g @hg f end function
6b0c3751e148c0feb6e9359441004bc7bf458fe9
0845ae2ca02071debcfd4ac24be871236c01784f
/tests/playground/bytearray.lean
7282ecbb00f3b5e291b7c8ba6b780e2ad3fcf5e1
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
238
lean
def main (xs : List String) : IO Unit := do let bs := [1, 2, 3].toByteArray, IO.println bs, let bs := bs.push 4, let bs := bs.set 1 20, IO.println bs, let bs₁ := bs.set 2 30, IO.println bs₁, IO.println bs, IO.println bs.size, pure ()
bde32a11c45ca6b3c0f89ff9e077bade9b219ce9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/types.lean
fbf17a593ce36adac2b6324f9fbaf0f0b9036e4c
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,785
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.fully_faithful import Mathlib.data.equiv.basic import Mathlib.PostPort universes u v w u' u_1 u_2 namespace Mathlib /-! # The category `Type`. In this section we set up the theory so that Lean's types and functions between them can be viewed as a `large_category` in our framework. Lean can not transparently view a function as a morphism in this category, and needs a hint in order to be able to type check. We provide the abbreviation `as_hom f` to guide type checking, as well as a corresponding notation `↾ f`. (Entered as `\upr `.) We provide various simplification lemmas for functors and natural transformations valued in `Type`. We define `ulift_functor`, from `Type u` to `Type (max u v)`, and show that it is fully faithful (but not, of course, essentially surjective). We prove some basic facts about the category `Type`: * epimorphisms are surjections and monomorphisms are injections, * `iso` is both `iso` and `equiv` to `equiv` (at least within a fixed universe), * every type level `is_lawful_functor` gives a categorical functor `Type ⥤ Type` (the corresponding fact about monads is in `src/category_theory/monad/types.lean`). -/ namespace category_theory protected instance types : large_category (Type u) := category.mk theorem types_hom {α : Type u} {β : Type u} : (α ⟶ β) = (α → β) := rfl theorem types_id (X : Type u) : 𝟙 = id := rfl theorem types_comp {X : Type u} {Y : Type u} {Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g = g ∘ f := rfl @[simp] theorem types_id_apply (X : Type u) (x : X) : 𝟙 = x := rfl @[simp] theorem types_comp_apply {X : Type u} {Y : Type u} {Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : category_struct.comp f g x = g (f x) := rfl @[simp] theorem hom_inv_id_apply {X : Type u} {Y : Type u} (f : X ≅ Y) (x : X) : iso.inv f (iso.hom f x) = x := congr_fun (iso.hom_inv_id f) x @[simp] theorem inv_hom_id_apply {X : Type u} {Y : Type u} (f : X ≅ Y) (y : Y) : iso.hom f (iso.inv f y) = y := congr_fun (iso.inv_hom_id f) y /-- `as_hom f` helps Lean type check a function as a morphism in the category `Type`. -/ -- Unfortunately without this wrapper we can't use `category_theory` idioms, such as `is_iso f`. -- If you don't mind some notation you can use fewer keystrokes: def as_hom {α : Type u} {β : Type u} (f : α → β) : α ⟶ β := f prefix:200 "↾" => Mathlib.category_theory.as_hom namespace functor /-- The sections of a functor `J ⥤ Type` are the choices of a point `u j : F.obj j` for each `j`, such that `F.map f (u j) = u j` for every morphism `f : j ⟶ j'`. We later use these to define limits in `Type` and in many concrete categories. -/ def sections {J : Type u} [category J] (F : J ⥤ Type w) : set ((j : J) → obj F j) := set_of fun (u : (j : J) → obj F j) => ∀ {j j' : J} (f : j ⟶ j'), map F f (u j) = u j' end functor namespace functor_to_types @[simp] theorem map_comp_apply {C : Type u} [category C] (F : C ⥤ Type w) {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (a : functor.obj F X) : functor.map F (f ≫ g) a = functor.map F g (functor.map F f a) := sorry @[simp] theorem map_id_apply {C : Type u} [category C] (F : C ⥤ Type w) {X : C} (a : functor.obj F X) : functor.map F 𝟙 a = a := sorry theorem naturality {C : Type u} [category C] (F : C ⥤ Type w) (G : C ⥤ Type w) {X : C} {Y : C} (σ : F ⟶ G) (f : X ⟶ Y) (x : functor.obj F X) : nat_trans.app σ Y (functor.map F f x) = functor.map G f (nat_trans.app σ X x) := congr_fun (nat_trans.naturality σ f) x @[simp] theorem comp {C : Type u} [category C] (F : C ⥤ Type w) (G : C ⥤ Type w) (H : C ⥤ Type w) {X : C} (σ : F ⟶ G) (τ : G ⟶ H) (x : functor.obj F X) : nat_trans.app (σ ≫ τ) X x = nat_trans.app τ X (nat_trans.app σ X x) := rfl @[simp] theorem hcomp {C : Type u} [category C] (F : C ⥤ Type w) (G : C ⥤ Type w) (σ : F ⟶ G) {D : Type u'} [𝒟 : category D] (I : D ⥤ C) (J : D ⥤ C) (ρ : I ⟶ J) {W : D} (x : functor.obj (I ⋙ F) W) : nat_trans.app (ρ ◫ σ) W x = functor.map G (nat_trans.app ρ W) (nat_trans.app σ (functor.obj I W) x) := rfl @[simp] theorem map_inv_map_hom_apply {C : Type u} [category C] (F : C ⥤ Type w) {X : C} {Y : C} (f : X ≅ Y) (x : functor.obj F X) : functor.map F (iso.inv f) (functor.map F (iso.hom f) x) = x := congr_fun (iso.hom_inv_id (functor.map_iso F f)) x @[simp] theorem map_hom_map_inv_apply {C : Type u} [category C] (F : C ⥤ Type w) {X : C} {Y : C} (f : X ≅ Y) (y : functor.obj F Y) : functor.map F (iso.hom f) (functor.map F (iso.inv f) y) = y := congr_fun (iso.inv_hom_id (functor.map_iso F f)) y @[simp] theorem hom_inv_id_app_apply {C : Type u} [category C] (F : C ⥤ Type w) (G : C ⥤ Type w) (α : F ≅ G) (X : C) (x : functor.obj F X) : nat_trans.app (iso.inv α) X (nat_trans.app (iso.hom α) X x) = x := congr_fun (iso.hom_inv_id_app α X) x @[simp] theorem inv_hom_id_app_apply {C : Type u} [category C] (F : C ⥤ Type w) (G : C ⥤ Type w) (α : F ≅ G) (X : C) (x : functor.obj G X) : nat_trans.app (iso.hom α) X (nat_trans.app (iso.inv α) X x) = x := congr_fun (iso.inv_hom_id_app α X) x end functor_to_types /-- The isomorphism between a `Type` which has been `ulift`ed to the same universe, and the original type. -/ def ulift_trivial (V : Type u) : ulift V ≅ V := iso.mk (id fun (ᾰ : ulift V) => ulift.cases_on ᾰ fun (ᾰ : V) => ᾰ) ulift.up /-- The functor embedding `Type u` into `Type (max u v)`. Write this as `ulift_functor.{5 2}` to get `Type 2 ⥤ Type 5`. -/ def ulift_functor : Type u ⥤ Type (max u v) := functor.mk (fun (X : Type u) => ulift X) fun (X Y : Type u) (f : X ⟶ Y) (x : ulift X) => ulift.up (f (ulift.down x)) @[simp] theorem ulift_functor_map {X : Type u} {Y : Type u} (f : X ⟶ Y) (x : ulift X) : functor.map ulift_functor f x = ulift.up (f (ulift.down x)) := rfl protected instance ulift_functor_full : full ulift_functor := full.mk fun (X Y : Type u) (f : functor.obj ulift_functor X ⟶ functor.obj ulift_functor Y) (x : X) => ulift.down (f (ulift.up x)) protected instance ulift_functor_faithful : faithful ulift_functor := faithful.mk /-- Any term `x` of a type `X` corresponds to a morphism `punit ⟶ X`. -/ -- TODO We should connect this to a general story about concrete categories -- whose forgetful functor is representable. def hom_of_element {X : Type u} (x : X) : PUnit ⟶ X := fun (_x : PUnit) => x theorem hom_of_element_eq_iff {X : Type u} (x : X) (y : X) : hom_of_element x = hom_of_element y ↔ x = y := sorry /-- A morphism in `Type` is a monomorphism if and only if it is injective. See https://stacks.math.columbia.edu/tag/003C. -/ theorem mono_iff_injective {X : Type u} {Y : Type u} (f : X ⟶ Y) : mono f ↔ function.injective f := sorry /-- A morphism in `Type` is an epimorphism if and only if it is surjective. See https://stacks.math.columbia.edu/tag/003C. -/ theorem epi_iff_surjective {X : Type u} {Y : Type u} (f : X ⟶ Y) : epi f ↔ function.surjective f := sorry /-- `of_type_functor m` converts from Lean's `Type`-based `category` to `category_theory`. This allows us to use these functors in category theory. -/ def of_type_functor (m : Type u → Type v) [Functor m] [is_lawful_functor m] : Type u ⥤ Type v := functor.mk m fun (α β : Type u) => Functor.map @[simp] theorem of_type_functor_obj (m : Type u → Type v) [Functor m] [is_lawful_functor m] : functor.obj (of_type_functor m) = m := rfl @[simp] theorem of_type_functor_map (m : Type u → Type v) [Functor m] [is_lawful_functor m] {α : Type u} {β : Type u} (f : α → β) : functor.map (of_type_functor m) f = Functor.map f := rfl end category_theory -- Isomorphisms in Type and equivalences. namespace equiv /-- Any equivalence between types in the same universe gives a categorical isomorphism between those types. -/ def to_iso {X : Type u} {Y : Type u} (e : X ≃ Y) : X ≅ Y := category_theory.iso.mk (to_fun e) (inv_fun e) @[simp] theorem to_iso_hom {X : Type u} {Y : Type u} {e : X ≃ Y} : category_theory.iso.hom (to_iso e) = ⇑e := rfl @[simp] theorem to_iso_inv {X : Type u} {Y : Type u} {e : X ≃ Y} : category_theory.iso.inv (to_iso e) = ⇑(equiv.symm e) := rfl end equiv namespace category_theory.iso /-- Any isomorphism between types gives an equivalence. -/ def to_equiv {X : Type u} {Y : Type u} (i : X ≅ Y) : X ≃ Y := equiv.mk (hom i) (inv i) sorry sorry @[simp] theorem to_equiv_fun {X : Type u} {Y : Type u} (i : X ≅ Y) : ⇑(to_equiv i) = hom i := rfl @[simp] theorem to_equiv_symm_fun {X : Type u} {Y : Type u} (i : X ≅ Y) : ⇑(equiv.symm (to_equiv i)) = inv i := rfl @[simp] theorem to_equiv_id (X : Type u) : to_equiv (refl X) = equiv.refl X := rfl @[simp] theorem to_equiv_comp {X : Type u} {Y : Type u} {Z : Type u} (f : X ≅ Y) (g : Y ≅ Z) : to_equiv (f ≪≫ g) = equiv.trans (to_equiv f) (to_equiv g) := rfl end category_theory.iso namespace category_theory /-- A morphism in `Type u` is an isomorphism if and only if it is bijective. -/ def is_iso_equiv_bijective {X : Type u} {Y : Type u} (f : X ⟶ Y) : is_iso f ≃ function.bijective f := equiv_of_subsingleton_of_subsingleton sorry fun (b : function.bijective f) => is_iso.mk (iso.inv (equiv.to_iso (equiv.of_bijective f b))) end category_theory -- We prove `equiv_iso_iso` and then use that to sneakily construct `equiv_equiv_iso`. -- (In this order the proofs are handled by `obviously`.) /-- Equivalences (between types in the same universe) are the same as (isomorphic to) isomorphisms of types. -/ @[simp] theorem equiv_iso_iso_hom {X : Type u} {Y : Type u} (e : X ≃ Y) : category_theory.iso.hom equiv_iso_iso e = equiv.to_iso e := Eq.refl (category_theory.iso.hom equiv_iso_iso e) /-- Equivalences (between types in the same universe) are the same as (equivalent to) isomorphisms of types. -/ -- We leave `X` and `Y` as explicit arguments here, because the coercions from `equiv` to a function -- won't fire without them. -- TODO: is it still true? def equiv_equiv_iso (X : Type u) (Y : Type u) : X ≃ Y ≃ (X ≅ Y) := category_theory.iso.to_equiv equiv_iso_iso @[simp] theorem equiv_equiv_iso_hom {X : Type u} {Y : Type u} (e : X ≃ Y) : coe_fn (equiv_equiv_iso X Y) e = equiv.to_iso e := rfl @[simp] theorem equiv_equiv_iso_inv {X : Type u} {Y : Type u} (e : X ≅ Y) : coe_fn (equiv.symm (equiv_equiv_iso X Y)) e = category_theory.iso.to_equiv e := rfl
d9d0ea1b4ea6431e4a3a0cd3dddb64ef6fe98e09
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/finsupp/multiset.lean
120284c1e5881a0966a08ba19537d4142e73092d
[ "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,286
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.finsupp.order /-! # Equivalence between `multiset` and `ℕ`-valued finitely supported functions This defines `finsupp.to_multiset` the equivalence between `α →₀ ℕ` and `multiset α`, along with `multiset.to_finsupp` the reverse equivalence and `finsupp.order_iso_multiset` the equivalence promoted to an order isomorphism. -/ open finset open_locale big_operators classical noncomputable theory variables {α β ι : Type*} namespace finsupp /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. We define this function as an `add_equiv`. -/ def to_multiset : (α →₀ ℕ) ≃+ multiset α := { to_fun := λ f, f.sum (λa n, n • {a}), inv_fun := λ s, ⟨s.to_finset, λ a, s.count a, λ a, by simp⟩, left_inv := λ f, ext $ λ a, by { simp only [sum, multiset.count_sum', multiset.count_singleton, mul_boole, coe_mk, mem_support_iff, multiset.count_nsmul, finset.sum_ite_eq, ite_not, ite_eq_right_iff], exact eq.symm }, right_inv := λ s, by simp only [sum, coe_mk, multiset.to_finset_sum_count_nsmul_eq], map_add' := λ f g, sum_add_index (λ a, zero_nsmul _) (λ a, add_nsmul _) } lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := to_multiset.map_add m n lemma to_multiset_apply (f : α →₀ ℕ) : f.to_multiset = f.sum (λ a n, n • {a}) := rfl @[simp] lemma to_multiset_symm_apply [decidable_eq α] (s : multiset α) (x : α) : finsupp.to_multiset.symm s x = s.count x := by convert rfl @[simp] lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n • {a} := by rw [to_multiset_apply, sum_single_index]; apply zero_nsmul lemma to_multiset_sum {f : ι → α →₀ ℕ} (s : finset ι) : finsupp.to_multiset (∑ i in s, f i) = ∑ i in s, finsupp.to_multiset (f i) := add_equiv.map_sum _ _ _ lemma to_multiset_sum_single (s : finset ι) (n : ℕ) : finsupp.to_multiset (∑ i in s, single i n) = n • s.val := by simp_rw [to_multiset_sum, finsupp.to_multiset_single, sum_nsmul, sum_multiset_singleton] lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λ a, id) := by simp [to_multiset_apply, add_monoid_hom.map_finsupp_sum, function.id_def] lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, ← multiset.coe_map_add_monoid_hom, (multiset.map_add_monoid_hom g).map_nsmul], refl } end @[simp] lemma prod_to_multiset [comm_monoid α] (f : α →₀ ℕ) : f.to_multiset.prod = f.prod (λ a n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_nsmul, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end @[simp] lemma to_finset_to_multiset [decidable_eq α] (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.to_finset_singleton], refine disjoint.mono_left support_single_subset _, rwa [finset.disjoint_singleton_left] } end @[simp] lemma count_to_multiset [decidable_eq α] (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n • {x} : multiset α).count a) : (multiset.count_add_monoid_hom a).map_sum _ f.support ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_nsmul] ... = f a * ({a} : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_singleton, if_false, H.symm, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by rw [multiset.count_singleton_self, mul_one] @[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) : i ∈ f.to_multiset ↔ i ∈ f.support := by rw [←multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff] end finsupp namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp : multiset α ≃+ (α →₀ ℕ) := finsupp.to_multiset.symm @[simp] lemma to_finsupp_support [decidable_eq α] (s : multiset α) : s.to_finsupp.support = s.to_finset := by convert rfl @[simp] lemma to_finsupp_apply [decidable_eq α] (s : multiset α) (a : α) : to_finsupp s a = s.count a := by convert rfl lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := add_equiv.map_zero _ lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := to_finsupp.map_add s t @[simp] lemma to_finsupp_singleton (a : α) : to_finsupp ({a} : multiset α) = finsupp.single a 1 := finsupp.to_multiset.symm_apply_eq.2 $ by simp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := finsupp.to_multiset.apply_symm_apply s lemma to_finsupp_eq_iff {s : multiset α} {f : α →₀ ℕ} : s.to_finsupp = f ↔ s = f.to_multiset := finsupp.to_multiset.symm_apply_eq end multiset @[simp] lemma finsupp.to_multiset_to_finsupp (f : α →₀ ℕ) : f.to_multiset.to_finsupp = f := finsupp.to_multiset.symm_apply_apply f /-! ### As an order isomorphism -/ namespace finsupp /-- `finsupp.to_multiset` as an order isomorphism. -/ def order_iso_multiset : (ι →₀ ℕ) ≃o multiset ι := { to_equiv := to_multiset.to_equiv, map_rel_iff' := λ f g, by simp [multiset.le_iff_count, le_def] } @[simp] lemma coe_order_iso_multiset : ⇑(@order_iso_multiset ι) = to_multiset := rfl @[simp] lemma coe_order_iso_multiset_symm : ⇑(@order_iso_multiset ι).symm = multiset.to_finsupp := rfl lemma to_multiset_strict_mono : strict_mono (@to_multiset ι) := (@order_iso_multiset ι).strict_mono lemma sum_id_lt_of_lt (m n : ι →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [←card_to_multiset, ←card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (ι) /-- The order on `ι →₀ ℕ` is well-founded. -/ lemma lt_wf : well_founded (@has_lt.lt (ι →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf end finsupp lemma multiset.to_finsupp_strict_mono : strict_mono (@multiset.to_finsupp ι) := (@finsupp.order_iso_multiset ι).symm.strict_mono
e4e389829e63635f92679d385872559f858aa124
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world10/level12.lean
5b769e86446a72b0ac74d03baa60735f0c39fbac
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
444
lean
import game.world10.level11 -- hide namespace mynat -- hide /- # Inequality world. ## Level 12: `le_of_succ_le_succ` -/ /- Lemma For all naturals $a$ and $b$, $\operatorname{succ}(a)\le\operatorname{succ}(b)\implies a\le b.$ -/ theorem le_of_succ_le_succ (a b : mynat) : succ a ≤ succ b → a ≤ b := begin [less_leaky] intro h, cases h with c hc, use c, apply succ_inj, rw hc, exact succ_add a c, end end mynat -- hide
1f1a00553117a6bbf19d3fdbb8430f78981ba522
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/tests/lean/run/discrTreeSimp.lean
ff6dbdd32788c74f8b1e66d3af91725f2449e0b1
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
662
lean
prelude import Init.Data.List.Basic @[simp] theorem map_comp_map (f : α → β) (g : β → γ) : List.map g ∘ List.map f = List.map (g ∘ f) := sorry theorem map_map (f : α → β) (g : β → γ) (xs : List α) : (xs.map f |>.map g) = xs.map (g ∘ f) := sorry theorem ex1 (f : Nat → Nat) (xs : List Nat) : (xs.map f |>.map f) = xs.map (f ∘ f) := by simp simp [map_map] done theorem ex2 (f : Nat → Nat) : List.map f ∘ List.map f ∘ List.map f = List.map (f ∘ f ∘ f) := by simp attribute [simp] map_map theorem ex3 (f : Nat → Nat) (xs : List Nat) : (xs.map f |>.map f |>.map f) = xs.map (fun x => f (f (f x))) := by simp
20af673e04c8911b41e7dced81493beb6c96f4b5
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/sum/basic.lean
b21185edbad8c7fd238e0777b578056e29329807
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
541
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, Jeremy Avigad The sum type, aka disjoint union. -/ prelude import init.logic notation α ⊕ β := sum α β universe variables u v variables {α : Type u} {β : Type v} instance sum.inhabited_left [h : inhabited α] : inhabited (α ⊕ β) := ⟨sum.inl (default α)⟩ instance sum.inhabited_right [h : inhabited β] : inhabited (α ⊕ β) := ⟨sum.inr (default β)⟩
4989ce1c7725dc545c08a13c8a67a293176e4bdb
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/special_functions/trigonometric/bounds.lean
7e2e8244e4e0838120acc71b5eb8d798a45ecf1c
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
6,315
lean
/- Copyright (c) 2022 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import analysis.special_functions.trigonometric.arctan_deriv /-! # Polynomial bounds for trigonometric functions ## Main statements This file contains upper and lower bounds for real trigonometric functions in terms of polynomials. See `trigonometric.basic` for more elementary inequalities, establishing the ranges of these functions, and their monotonicity in suitable intervals. Here we prove the following: * `sin_lt`: for `x > 0` we have `sin x < x`. * `sin_gt_sub_cube`: For `0 < x ≤ 1` we have `x - x ^ 3 / 4 < sin x`. * `lt_tan`: for `0 < x < π/2` we have `x < tan x`. * `cos_le_one_div_sqrt_sq_add_one` and `cos_lt_one_div_sqrt_sq_add_one`: for `-3 * π / 2 ≤ x ≤ 3 * π / 2`, we have `cos x ≤ 1 / sqrt (x ^ 2 + 1)`, with strict inequality if `x ≠ 0`. (This bound is not quite optimal, but not far off) ## Tags sin, cos, tan, angle -/ noncomputable theory open set namespace real open_locale real /-- For 0 < x, we have sin x < x. -/ lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x := begin cases lt_or_le 1 x with h' h', { exact (sin_le_one x).trans_lt h' }, have hx : |x| = x := abs_of_nonneg h.le, have := le_of_abs_le (sin_bound $ show |x| ≤ 1, by rwa [hx]), rw [sub_le_iff_le_add', hx] at this, apply this.trans_lt, rw [sub_add, sub_lt_self_iff, sub_pos, div_eq_mul_inv (x ^ 3)], refine mul_lt_mul' _ (by norm_num) (by norm_num) (pow_pos h 3), apply pow_le_pow_of_le_one h.le h', norm_num end /-- For 0 < x ≤ 1 we have x - x ^ 3 / 4 < sin x. This is also true for x > 1, but it's nontrivial for x just above 1. This inequality is not tight; the tighter inequality is sin x > x - x ^ 3 / 6 for all x > 0, but this inequality has a simpler proof. -/ lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x := begin have hx : |x| = x := abs_of_nonneg h.le, have := neg_le_of_abs_le (sin_bound $ show |x| ≤ 1, by rwa [hx]), rw [le_sub_iff_add_le, hx] at this, refine lt_of_lt_of_le _ this, have : x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 * 12⁻¹ := by norm_num [div_eq_mul_inv, ← mul_sub], rw [add_comm, sub_add, sub_neg_eq_add, sub_lt_sub_iff_left, ←lt_sub_iff_add_lt', this], refine mul_lt_mul' _ (by norm_num) (by norm_num) (pow_pos h 3), apply pow_le_pow_of_le_one h.le h', norm_num end /-- The derivative of `tan x - x` is `1/(cos x)^2 - 1` away from the zeroes of cos. -/ lemma deriv_tan_sub_id (x : ℝ) (h : cos x ≠ 0) : deriv (λ y : ℝ, tan y - y) x = 1 / cos x ^ 2 - 1 := has_deriv_at.deriv $ by simpa using (has_deriv_at_tan h).add (has_deriv_at_id x).neg /-- For all `0 < x < π/2` we have `x < tan x`. This is proved by checking that the function `tan x - x` vanishes at zero and has non-negative derivative. -/ theorem lt_tan {x : ℝ} (h1 : 0 < x) (h2 : x < π / 2) : x < tan x := begin let U := Ico 0 (π / 2), have intU : interior U = Ioo 0 (π / 2) := interior_Ico, have half_pi_pos : 0 < π / 2 := div_pos pi_pos two_pos, have cos_pos : ∀ {y : ℝ}, y ∈ U → 0 < cos y, { intros y hy, exact cos_pos_of_mem_Ioo (Ico_subset_Ioo_left (neg_lt_zero.mpr half_pi_pos) hy) }, have sin_pos : ∀ {y : ℝ}, y ∈ interior U → 0 < sin y, { intros y hy, rw intU at hy, exact sin_pos_of_mem_Ioo (Ioo_subset_Ioo_right (div_le_self pi_pos.le one_le_two) hy) }, have tan_cts_U : continuous_on tan U, { apply continuous_on.mono continuous_on_tan, intros z hz, simp only [mem_set_of_eq], exact (cos_pos hz).ne' }, have tan_minus_id_cts : continuous_on (λ y : ℝ, tan y - y) U := tan_cts_U.sub continuous_on_id, have deriv_pos : ∀ y : ℝ, y ∈ interior U → 0 < deriv (λ y' : ℝ, tan y' - y') y, { intros y hy, have := cos_pos (interior_subset hy), simp only [deriv_tan_sub_id y this.ne', one_div, gt_iff_lt, sub_pos], have bd2 : cos y ^ 2 < 1, { apply lt_of_le_of_ne y.cos_sq_le_one, rw cos_sq', simpa only [ne.def, sub_eq_self, pow_eq_zero_iff, nat.succ_pos'] using (sin_pos hy).ne' }, rwa [lt_inv, inv_one], { exact zero_lt_one }, simpa only [sq, mul_self_pos] using this.ne' }, have mono := convex.strict_mono_on_of_deriv_pos (convex_Ico 0 (π / 2)) tan_minus_id_cts deriv_pos, have zero_in_U : (0 : ℝ) ∈ U, { rwa left_mem_Ico }, have x_in_U : x ∈ U := ⟨h1.le, h2⟩, simpa only [tan_zero, sub_zero, sub_pos] using mono zero_in_U x_in_U h1 end lemma le_tan {x : ℝ} (h1 : 0 ≤ x) (h2 : x < π / 2) : x ≤ tan x := begin rcases eq_or_lt_of_le h1 with rfl | h1', { rw tan_zero }, { exact le_of_lt (lt_tan h1' h2) } end lemma cos_lt_one_div_sqrt_sq_add_one {x : ℝ} (hx1 : -(3 * π / 2) ≤ x) (hx2 : x ≤ 3 * π / 2) (hx3 : x ≠ 0) : cos x < 1 / sqrt (x ^ 2 + 1) := begin suffices : ∀ {y : ℝ} (hy1 : 0 < y) (hy2 : y ≤ 3 * π / 2), cos y < 1 / sqrt (y ^ 2 + 1), { rcases lt_or_lt_iff_ne.mpr hx3.symm, { exact this h hx2 }, { convert this (by linarith : 0 < -x) (by linarith) using 1, { rw cos_neg }, { rw neg_sq } } }, intros y hy1 hy2, have hy3 : 0 < y ^ 2 + 1, by linarith [sq_nonneg y], rcases lt_or_le y (π / 2) with hy2' | hy1', { -- Main case : `0 < y < π / 2` have hy4 : 0 < cos y := cos_pos_of_mem_Ioo ⟨by linarith, hy2'⟩, rw [←abs_of_nonneg (cos_nonneg_of_mem_Icc ⟨by linarith, hy2'.le⟩), ←abs_of_nonneg (one_div_nonneg.mpr (sqrt_nonneg _)), ←sq_lt_sq, div_pow, one_pow, sq_sqrt hy3.le, lt_one_div (pow_pos hy4 _) hy3, ←inv_one_add_tan_sq hy4.ne', one_div, inv_inv, add_comm, add_lt_add_iff_left, sq_lt_sq, abs_of_pos hy1, abs_of_nonneg (tan_nonneg_of_nonneg_of_le_pi_div_two hy1.le hy2'.le)], exact real.lt_tan hy1 hy2' }, { -- Easy case : `π / 2 ≤ y ≤ 3 * π / 2` refine lt_of_le_of_lt _ (one_div_pos.mpr $ sqrt_pos_of_pos hy3), exact cos_nonpos_of_pi_div_two_le_of_le hy1' (by linarith [pi_pos]) } end lemma cos_le_one_div_sqrt_sq_add_one {x : ℝ} (hx1 : -(3 * π / 2) ≤ x) (hx2 : x ≤ 3 * π / 2) : cos x ≤ 1 / sqrt (x ^ 2 + 1) := begin rcases eq_or_ne x 0 with rfl | hx3, { simp }, { exact (cos_lt_one_div_sqrt_sq_add_one hx1 hx2 hx3).le } end end real
1ca706a2e79ce754711500886af98eb6f311aa1c
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/abelianization.lean
6c9fdf44175e592fd661a92406e46030e95e3069
[ "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
10,462
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Michael Howes -/ import data.finite.card import group_theory.commutator import group_theory.finiteness /-! # The abelianization of a group > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the commutator and the abelianization of a group. It furthermore prepares for the result that the abelianization is left adjoint to the forgetful functor from abelian groups to groups, which can be found in `algebra/category/Group/adjunctions`. ## Main definitions * `commutator`: defines the commutator of a group `G` as a subgroup of `G`. * `abelianization`: defines the abelianization of a group `G` as the quotient of a group by its commutator subgroup. * `abelianization.map`: lifts a group homomorphism to a homomorphism between the abelianizations * `mul_equiv.abelianization_congr`: Equivalent groups have equivalent abelianizations -/ universes u v w -- Let G be a group. variables (G : Type u) [group G] /-- The commutator subgroup of a group G is the normal subgroup generated by the commutators [p,q]=`p*q*p⁻¹*q⁻¹`. -/ @[derive subgroup.normal] def commutator : subgroup G := ⁅(⊤ : subgroup G), ⊤⁆ lemma commutator_def : commutator G = ⁅(⊤ : subgroup G), ⊤⁆ := rfl lemma commutator_eq_closure : commutator G = subgroup.closure (commutator_set G) := by simp [commutator, subgroup.commutator_def, commutator_set] lemma commutator_eq_normal_closure : commutator G = subgroup.normal_closure (commutator_set G) := by simp [commutator, subgroup.commutator_def', commutator_set] instance commutator_characteristic : (commutator G).characteristic := subgroup.commutator_characteristic ⊤ ⊤ instance [finite (commutator_set G)] : group.fg (commutator G) := begin rw commutator_eq_closure, apply group.closure_finite_fg, end lemma rank_commutator_le_card [finite (commutator_set G)] : group.rank (commutator G) ≤ nat.card (commutator_set G) := begin rw subgroup.rank_congr (commutator_eq_closure G), apply subgroup.rank_closure_finite_le_nat_card, end lemma commutator_centralizer_commutator_le_center : ⁅(commutator G).centralizer, (commutator G).centralizer⁆ ≤ subgroup.center G := begin rw [←subgroup.centralizer_top, ←subgroup.commutator_eq_bot_iff_le_centralizer], suffices : ⁅⁅⊤, (commutator G).centralizer⁆, (commutator G).centralizer⁆ = ⊥, { refine subgroup.commutator_commutator_eq_bot_of_rotate _ this, rwa subgroup.commutator_comm (commutator G).centralizer }, rw [subgroup.commutator_comm, subgroup.commutator_eq_bot_iff_le_centralizer], exact set.centralizer_subset (subgroup.commutator_mono le_top le_top), end /-- The abelianization of G is the quotient of G by its commutator subgroup. -/ def abelianization : Type u := G ⧸ (commutator G) namespace abelianization local attribute [instance] quotient_group.left_rel instance : comm_group (abelianization G) := { mul_comm := λ x y, quotient.induction_on₂' x y $ λ a b, quotient.sound' $ quotient_group.left_rel_apply.mpr $ subgroup.subset_closure ⟨b⁻¹, subgroup.mem_top b⁻¹, a⁻¹, subgroup.mem_top a⁻¹, by group⟩, .. quotient_group.quotient.group _ } instance : inhabited (abelianization G) := ⟨1⟩ instance [fintype G] [decidable_pred (∈ commutator G)] : fintype (abelianization G) := quotient_group.fintype (commutator G) instance [finite G] : finite (abelianization G) := quotient.finite _ variable {G} /-- `of` is the canonical projection from G to its abelianization. -/ def of : G →* abelianization G := { to_fun := quotient_group.mk, map_one' := rfl, map_mul' := λ x y, rfl } @[simp] lemma mk_eq_of (a : G) : quot.mk _ a = of a := rfl section lift -- So far we have built Gᵃᵇ and proved it's an abelian group. -- Furthremore we defined the canonical projection `of : G → Gᵃᵇ` -- Let `A` be an abelian group and let `f` be a group homomorphism from `G` to `A`. variables {A : Type v} [comm_group A] (f : G →* A) lemma commutator_subset_ker : commutator G ≤ f.ker := begin rw [commutator_eq_closure, subgroup.closure_le], rintros x ⟨p, q, rfl⟩, simp [monoid_hom.mem_ker, mul_right_comm (f p) (f q), commutator_element_def], end /-- If `f : G → A` is a group homomorphism to an abelian group, then `lift f` is the unique map from the abelianization of a `G` to `A` that factors through `f`. -/ def lift : (G →* A) ≃ (abelianization G →* A) := { to_fun := λ f, quotient_group.lift _ f (λ x h, f.mem_ker.2 $ commutator_subset_ker _ h), inv_fun := λ F, F.comp of, left_inv := λ f, monoid_hom.ext $ λ x, rfl, right_inv := λ F, monoid_hom.ext $ λ x, quotient_group.induction_on x $ λ z, rfl } @[simp] lemma lift.of (x : G) : lift f (of x) = f x := rfl theorem lift.unique (φ : abelianization G →* A) -- hφ : φ agrees with f on the image of G in Gᵃᵇ (hφ : ∀ (x : G), φ (of x) = f x) {x : abelianization G} : φ x = lift f x := quotient_group.induction_on x hφ @[simp] lemma lift_of : lift of = monoid_hom.id (abelianization G) := lift.apply_symm_apply $ monoid_hom.id _ end lift variables {A : Type v} [monoid A] /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext (φ ψ : abelianization G →* A) (h : φ.comp of = ψ.comp of) : φ = ψ := monoid_hom.ext $ λ x, quotient_group.induction_on x $ monoid_hom.congr_fun h section map variables {H : Type v} [group H] (f : G →* H) /-- The map operation of the `abelianization` functor -/ def map : abelianization G →* abelianization H := lift (of.comp f) @[simp] lemma map_of (x : G) : map f (of x) = of (f x) := rfl @[simp] lemma map_id : map (monoid_hom.id G) = monoid_hom.id (abelianization G) := hom_ext _ _ rfl @[simp] lemma map_comp {I : Type w} [group I] (g : H →* I) : (map g).comp (map f) = map (g.comp f) := hom_ext _ _ rfl @[simp] lemma map_map_apply {I : Type w} [group I] {g : H →* I} {x : abelianization G}: map g (map f x) = map (g.comp f) x := monoid_hom.congr_fun (map_comp _ _) x end map end abelianization section abelianization_congr variables {G} {H : Type v} [group H] (e : G ≃* H) /-- Equivalent groups have equivalent abelianizations -/ def mul_equiv.abelianization_congr : abelianization G ≃* abelianization H := { to_fun := abelianization.map e.to_monoid_hom, inv_fun := abelianization.map e.symm.to_monoid_hom, left_inv := by { rintros ⟨a⟩, simp }, right_inv := by { rintros ⟨a⟩, simp }, map_mul' := monoid_hom.map_mul _ } @[simp] lemma abelianization_congr_of (x : G) : (e.abelianization_congr) (abelianization.of x) = abelianization.of (e x) := rfl @[simp] lemma abelianization_congr_refl : (mul_equiv.refl G).abelianization_congr = mul_equiv.refl (abelianization G) := mul_equiv.to_monoid_hom_injective abelianization.lift_of @[simp] lemma abelianization_congr_symm : e.abelianization_congr.symm = e.symm.abelianization_congr := rfl @[simp] lemma abelianization_congr_trans {I : Type v} [group I] (e₂ : H ≃* I) : e.abelianization_congr.trans e₂.abelianization_congr = (e.trans e₂).abelianization_congr := mul_equiv.to_monoid_hom_injective (abelianization.hom_ext _ _ rfl) end abelianization_congr /-- An Abelian group is equivalent to its own abelianization. -/ @[simps] def abelianization.equiv_of_comm {H : Type*} [comm_group H] : H ≃* abelianization H := { to_fun := abelianization.of, inv_fun := abelianization.lift (monoid_hom.id H), left_inv := λ a, rfl, right_inv := by { rintros ⟨a⟩, refl, }, .. abelianization.of } section commutator_representatives open subgroup /-- Representatives `(g₁, g₂) : G × G` of commutator_set `⁅g₁, g₂⁆ ∈ G`. -/ def commutator_representatives : set (G × G) := set.range (λ g : commutator_set G, (g.2.some, g.2.some_spec.some)) instance [finite (commutator_set G)] : finite (commutator_representatives G) := set.finite_coe_iff.mpr (set.finite_range _) /-- Subgroup generated by representatives `g₁ g₂ : G` of commutators `⁅g₁, g₂⁆ ∈ G`. -/ def closure_commutator_representatives : subgroup G := closure (prod.fst '' commutator_representatives G ∪ prod.snd '' commutator_representatives G) instance closure_commutator_representatives_fg [finite (commutator_set G)] : group.fg (closure_commutator_representatives G) := group.closure_finite_fg _ lemma rank_closure_commutator_representations_le [finite (commutator_set G)] : group.rank (closure_commutator_representatives G) ≤ 2 * nat.card (commutator_set G) := begin rw two_mul, exact (subgroup.rank_closure_finite_le_nat_card _).trans ((set.card_union_le _ _).trans (add_le_add ((finite.card_image_le _).trans (finite.card_range_le _)) ((finite.card_image_le _).trans (finite.card_range_le _ )))), end lemma image_commutator_set_closure_commutator_representatives : (closure_commutator_representatives G).subtype '' (commutator_set (closure_commutator_representatives G)) = commutator_set G := begin apply set.subset.antisymm, { rintros - ⟨-, ⟨g₁, g₂, rfl⟩, rfl⟩, exact ⟨g₁, g₂, rfl⟩ }, { exact λ g hg, ⟨_, ⟨⟨_, subset_closure (or.inl ⟨_, ⟨⟨g, hg⟩, rfl⟩, rfl⟩)⟩, ⟨_, subset_closure (or.inr ⟨_, ⟨⟨g, hg⟩, rfl⟩, rfl⟩)⟩, rfl⟩, hg.some_spec.some_spec⟩ }, end lemma card_commutator_set_closure_commutator_representatives : nat.card (commutator_set (closure_commutator_representatives G)) = nat.card (commutator_set G) := begin rw ← image_commutator_set_closure_commutator_representatives G, exact nat.card_congr (equiv.set.image _ _ (subtype_injective _)), end lemma card_commutator_closure_commutator_representatives : nat.card (commutator (closure_commutator_representatives G)) = nat.card (commutator G) := begin rw [commutator_eq_closure G, ←image_commutator_set_closure_commutator_representatives, ←monoid_hom.map_closure, ←commutator_eq_closure], exact nat.card_congr (equiv.set.image _ _ (subtype_injective _)), end instance [finite (commutator_set G)] : finite (commutator_set (closure_commutator_representatives G)) := begin apply nat.finite_of_card_ne_zero, rw card_commutator_set_closure_commutator_representatives, exact finite.card_pos.ne', end end commutator_representatives
e781c5e667a6ff8c8ee7a3ccc1b8c851e6d43ca1
f7315930643edc12e76c229a742d5446dad77097
/library/data/nat/order.lean
72d7b84c58f148be98103144563c8980e768fcd7
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,888
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.nat.order Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad The order relation on the natural numbers. -/ import data.nat.basic algebra.ordered_ring open eq.ops namespace nat /- lt and le -/ theorem le_of_lt_or_eq {m n : ℕ} (H : m < n ∨ m = n) : m ≤ n := or.elim H (take H1, le_of_lt H1) (take H1, H1 ▸ !le.refl) theorem lt.by_cases {a b : ℕ} {P : Prop} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := or.elim !lt.trichotomy (assume H, H1 H) (assume H, or.elim H (assume H', H2 H') (assume H', H3 H')) theorem lt_or_eq_of_le {m n : ℕ} (H : m ≤ n) : m < n ∨ m = n := lt.by_cases (assume H1 : m < n, or.inl H1) (assume H1 : m = n, or.inr H1) (assume H1 : m > n, absurd (lt_of_le_of_lt H H1) !lt.irrefl) theorem le_iff_lt_or_eq (m n : ℕ) : m ≤ n ↔ m < n ∨ m = n := iff.intro lt_or_eq_of_le le_of_lt_or_eq theorem lt_of_le_and_ne {m n : ℕ} (H1 : m ≤ n) (H2 : m ≠ n) : m < n := or.elim (lt_or_eq_of_le H1) (take H3 : m < n, H3) (take H3 : m = n, absurd H3 H2) theorem lt_iff_le_and_ne (m n : ℕ) : m < n ↔ m ≤ n ∧ m ≠ n := iff.intro (take H, and.intro (le_of_lt H) (take H1, lt.irrefl _ (H1 ▸ H))) (take H, lt_of_le_and_ne (and.elim_left H) (and.elim_right H)) theorem le_add_right (n k : ℕ) : n ≤ n + k := nat.induction_on k (calc n ≤ n : le.refl n ... = n + zero : add_zero) (λ k (ih : n ≤ n + k), calc n ≤ succ (n + k) : le_succ_of_le ih ... = n + succ k : add_succ) 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 n k theorem le.elim {n m : ℕ} (h : n ≤ m) : ∃k, n + k = m := le.rec_on h (exists.intro 0 rfl) (λ m (h : n < m), lt.rec_on h (exists.intro 1 rfl) (λ b hlt (ih : ∃ (k : ℕ), n + k = b), obtain (k : ℕ) (h : n + k = b), from ih, exists.intro (succ k) (calc n + succ k = succ (n + k) : add_succ ... = succ b : h))) theorem le.total {m n : ℕ} : m ≤ n ∨ n ≤ m := lt.by_cases (assume H : m < n, or.inl (le_of_lt H)) (assume H : m = n, or.inl (H ▸ !le.refl)) (assume H : m > n, or.inr (le_of_lt H)) /- addition -/ theorem add_le_add_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, le.intro (calc k + n + l = k + (n + l) : !add.assoc ... = k + m : {Hl}) theorem add_le_add_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k := !add.comm ▸ !add.comm ▸ add_le_add_left H k theorem le_of_add_le_add_left {k n m : ℕ} (H : k + n ≤ k + m) : n ≤ m := obtain (l : ℕ) (Hl : k + n + l = k + m), from (le.elim H), le.intro (add.cancel_left (calc k + (n + l) = k + n + l : (!add.assoc)⁻¹ ... = k + m : Hl)) theorem add_lt_add_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m := lt_of_succ_le (!add_succ ▸ add_le_add_left (succ_le_of_lt H) k) theorem add_lt_add_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k := !add.comm ▸ !add.comm ▸ add_lt_add_left H k theorem lt_add_of_pos_right {n k : ℕ} (H : k > 0) : n < n + k := !add_zero ▸ add_lt_add_left H n /- multiplication -/ theorem mul_le_mul_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k * n ≤ k * m := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, have H2 : k * n + k * l = k * m, by rewrite [-mul.left_distrib, Hl], le.intro H2 theorem mul_le_mul_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n * k ≤ m * k := !mul.comm ▸ !mul.comm ▸ (mul_le_mul_left H k) theorem mul_le_mul {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l := le.trans (mul_le_mul_right H1 m) (mul_le_mul_left H2 k) theorem mul_lt_mul_of_pos_left {n m k : ℕ} (H : n < m) (Hk : k > 0) : k * n < k * m := have H2 : k * n < k * n + k, from lt_add_of_pos_right Hk, have H3 : k * n + k ≤ k * m, from !mul_succ ▸ mul_le_mul_left (succ_le_of_lt H) k, lt_of_lt_of_le H2 H3 theorem mul_lt_mul_of_pos_right {n m k : ℕ} (H : n < m) (Hk : k > 0) : n * k < m * k := !mul.comm ▸ !mul.comm ▸ mul_lt_mul_of_pos_left H Hk theorem le.antisymm {n m : ℕ} (H1 : n ≤ m) (H2 : m ≤ n) : n = m := obtain (k : ℕ) (Hk : n + k = m), from (le.elim H1), obtain (l : ℕ) (Hl : m + l = n), from (le.elim H2), have L1 : k + l = 0, from add.cancel_left (calc n + (k + l) = n + k + l : (!add.assoc)⁻¹ ... = m + l : {Hk} ... = n : Hl ... = n + 0 : (!add_zero)⁻¹), have L2 : k = 0, from eq_zero_of_add_eq_zero_right L1, calc n = n + 0 : (!add_zero)⁻¹ ... = n + k : {L2⁻¹} ... = m : Hk theorem zero_le (n : ℕ) : 0 ≤ n := le.intro !zero_add /- nat is an instance of a linearly ordered semiring -/ section open [classes] algebra protected definition linear_ordered_semiring [instance] [reducible] : algebra.linear_ordered_semiring nat := ⦃ algebra.linear_ordered_semiring, nat.comm_semiring, add_left_cancel := @add.cancel_left, add_right_cancel := @add.cancel_right, lt := lt, le := le, le_refl := le.refl, le_trans := @le.trans, le_antisymm := @le.antisymm, le_total := @le.total, le_iff_lt_or_eq := @le_iff_lt_or_eq, lt_iff_le_ne := lt_iff_le_and_ne, add_le_add_left := @add_le_add_left, le_of_add_le_add_left := @le_of_add_le_add_left, zero_ne_one := ne.symm (succ_ne_zero zero), mul_le_mul_of_nonneg_left := (take a b c H1 H2, mul_le_mul_left H1 c), mul_le_mul_of_nonneg_right := (take a b c H1 H2, mul_le_mul_right H1 c), mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right ⦄ migrate from algebra with nat replacing has_le.ge → ge, has_lt.gt → gt hiding pos_of_mul_pos_left, pos_of_mul_pos_right, lt_of_mul_lt_mul_left, lt_of_mul_lt_mul_right end section port_algebra open [classes] algebra theorem add_pos_left : ∀{a : ℕ}, 0 < a → ∀b : ℕ, 0 < a + b := take a H b, @algebra.add_pos_of_pos_of_nonneg _ _ a b H !zero_le theorem add_pos_right : ∀{a : ℕ}, 0 < a → ∀b : ℕ, 0 < b + a := take a H b, !add.comm ▸ add_pos_left H b theorem add_eq_zero_iff_eq_zero_and_eq_zero : ∀{a b : ℕ}, a + b = 0 ↔ a = 0 ∧ b = 0 := take a b : ℕ, @algebra.add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg _ _ a b !zero_le !zero_le theorem le_add_of_le_left : ∀{a b c : ℕ}, b ≤ c → b ≤ a + c := take a b c H, @algebra.le_add_of_nonneg_of_le _ _ a b c !zero_le H theorem le_add_of_le_right : ∀{a b c : ℕ}, b ≤ c → b ≤ c + a := take a b c H, @algebra.le_add_of_le_of_nonneg _ _ a b c H !zero_le theorem lt_add_of_lt_left : ∀{b c : ℕ}, b < c → ∀a, b < a + c := take b c H a, @algebra.lt_add_of_nonneg_of_lt _ _ a b c !zero_le H theorem lt_add_of_lt_right : ∀{b c : ℕ}, b < c → ∀a, b < c + a := take b c H a, @algebra.lt_add_of_lt_of_nonneg _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_left : ∀{a b c : ℕ}, c * a < c * b → a < b := take a b c H, @algebra.lt_of_mul_lt_mul_left _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_right : ∀{a b c : ℕ}, a * c < b * c → a < b := take a b c H, @algebra.lt_of_mul_lt_mul_right _ _ a b c H !zero_le theorem pos_of_mul_pos_left : ∀{a b : ℕ}, 0 < a * b → 0 < b := take a b H, @algebra.pos_of_mul_pos_left _ _ a b H !zero_le theorem pos_of_mul_pos_right : ∀{a b : ℕ}, 0 < a * b → 0 < a := take a b H, @algebra.pos_of_mul_pos_right _ _ a b H !zero_le end port_algebra theorem zero_le_one : 0 ≤ 1 := dec_trivial theorem zero_lt_one : 0 < 1 := dec_trivial /- 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 lt_iff_succ_le (m n : nat) : m < n ↔ succ m ≤ n := iff.intro succ_le_of_lt lt_of_succ_le theorem not_succ_le_zero (n : ℕ) : ¬ succ n ≤ 0 := (assume H : succ n ≤ 0, have H2 : succ n = 0, from eq_zero_of_le_zero H, absurd H2 !succ_ne_zero) theorem succ_le_succ {n m : ℕ} (H : n ≤ m) : succ n ≤ succ m := !add_one ▸ !add_one ▸ add_le_add_right H 1 theorem le_of_succ_le_succ {n m : ℕ} (H : succ n ≤ succ m) : n ≤ m := le_of_add_le_add_right ((!add_one)⁻¹ ▸ (!add_one)⁻¹ ▸ H) theorem self_le_succ (n : ℕ) : n ≤ succ n := le.intro !add_one theorem succ_le_or_eq_of_le {n m : ℕ} (H : n ≤ m) : succ n ≤ m ∨ n = m := or.elim (lt_or_eq_of_le H) (assume H1 : n < m, or.inl (succ_le_of_lt H1)) (assume H1 : n = m, or.inr H1) theorem le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := nat.cases_on n (assume H : pred 0 ≤ m, !zero_le) (take n', assume H : pred (succ n') ≤ m, have H1 : n' ≤ m, from pred_succ n' ▸ H, succ_le_succ H1) theorem pred_le_of_le_succ {n m : ℕ} : n ≤ succ m → pred n ≤ m := nat.cases_on n (assume H, !pred_zero⁻¹ ▸ zero_le m) (take n', assume H : succ n' ≤ succ m, have H1 : n' ≤ m, from le_of_succ_le_succ H, !pred_succ⁻¹ ▸ H1) theorem succ_le_of_le_pred {n m : ℕ} : succ n ≤ m → n ≤ pred m := nat.cases_on m (assume H, absurd H !not_succ_le_zero) (take m', assume H : succ n ≤ succ m', have H1 : n ≤ m', from le_of_succ_le_succ H, !pred_succ⁻¹ ▸ H1) theorem pred_le_pred_of_le {n m : ℕ} : n ≤ m → pred n ≤ pred m := nat.cases_on n (assume H, pred_zero⁻¹ ▸ zero_le (pred m)) (take n', assume H : succ n' ≤ m, !pred_succ⁻¹ ▸ succ_le_of_le_pred H) theorem lt_of_pred_lt_pred {n m : ℕ} (H : pred n < pred m) : n < m := lt_of_not_le (take H1 : m ≤ n, not_lt_of_le (pred_le_pred_of_le H1) H) theorem le_or_eq_succ_of_le_succ {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m := or_of_or_of_imp_left (succ_le_or_eq_of_le H) (take H2 : succ n ≤ succ m, show n ≤ m, from le_of_succ_le_succ H2) theorem le_pred_self (n : ℕ) : pred n ≤ n := nat.cases_on n (pred_zero⁻¹ ▸ !le.refl) (take k : ℕ, (!pred_succ)⁻¹ ▸ !self_le_succ) theorem succ_pos (n : ℕ) : 0 < succ n := !zero_lt_succ theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n := (or_resolve_right (eq_zero_or_eq_succ_pred n) (ne.symm (ne_of_lt H)))⁻¹ theorem exists_eq_succ_of_lt {n m : ℕ} (H : n < m) : exists k, m = succ k := discriminate (take (Hm : m = 0), absurd (Hm ▸ H) !not_lt_zero) (take (l : ℕ) (Hm : m = succ l), exists.intro l Hm) theorem lt_succ_self (n : ℕ) : n < succ n := lt.base n theorem le_of_lt_succ {n m : ℕ} (H : n < succ m) : n ≤ m := le_of_succ_le_succ (succ_le_of_lt H) /- other forms of induction -/ protected theorem strong_induction_on {P : nat → Prop} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) : P n := have H1 : ∀ {n m : nat}, m < n → P m, from take n, nat.induction_on n (show ∀m, m < 0 → P m, from take m H, absurd H !not_lt_zero) (take n', assume IH : ∀ {m : nat}, m < n' → P m, have H2: P n', from H n' @IH, show ∀m, m < succ n' → P m, from take m, assume H3 : m < succ n', or.elim (lt_or_eq_of_le (le_of_lt_succ H3)) (assume H4: m < n', IH H4) (assume H4: m = n', H4⁻¹ ▸ H2)), H1 !lt_succ_self protected theorem case_strong_induction_on {P : nat → Prop} (a : nat) (H0 : P 0) (Hind : ∀(n : nat), (∀m, m ≤ n → P m) → P (succ n)) : P a := strong_induction_on a ( take n, show (∀m, m < n → P m) → P n, from nat.cases_on n (assume H : (∀m, m < 0 → P m), show P 0, from H0) (take n, assume H : (∀m, m < succ n → P m), show P (succ n), from Hind n (take m, assume H1 : m ≤ n, H _ (lt_succ_of_le H1)))) /- pos -/ theorem by_cases_zero_pos {P : ℕ → Prop} (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_or_pos (n : ℕ) : n = 0 ∨ n > 0 := or_of_or_of_imp_left (or.swap (lt_or_eq_of_le !zero_le)) (take H : 0 = n, H⁻¹) theorem pos_of_ne_zero {n : ℕ} (H : n ≠ 0) : n > 0 := or.elim !eq_zero_or_pos (take H2 : n = 0, absurd H2 H) (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) : exists 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 (assume H3 : m = 0, have H4 : n = 0, from eq_zero_of_zero_dvd (H3 ▸ H1), ne_of_lt H2 H4⁻¹) /- 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 H1 m) (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 H2 n) (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 (le_of_lt H1) m, 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 H2 : n * m ≤ n * k, from H ▸ !le.refl, have H3 : n * k ≤ n * m, from H ▸ !le.refl, have H4 : m ≤ k, from le_of_mul_le_mul_left H2 Hn, have H5 : k ≤ m, from le_of_mul_le_mul_left H3 Hn, le.antisymm H4 H5 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_or_eq_of_mul_eq_mul_left {n m k : ℕ} (H : n * m = n * k) : n = 0 ∨ m = k := or_of_or_of_imp_right !eq_zero_or_pos (assume Hn : n > 0, eq_of_mul_eq_mul_left Hn H) theorem eq_zero_or_eq_of_mul_eq_mul_right {n m k : ℕ} (H : n * m = k * m) : m = 0 ∨ n = k := eq_zero_or_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, from H⁻¹ ▸ !succ_pos, have H3 : n > 0, from pos_of_mul_pos_right H2, have H4 : m > 0, from pos_of_mul_pos_left H2, or.elim (le_or_gt n 1) (assume H5 : n ≤ 1, show n = 1, from le.antisymm H5 (succ_le_of_lt H3)) (assume H5 : n > 1, have H6 : n * m ≥ 2 * 1, from mul_le_mul (succ_le_of_lt H5) (succ_le_of_lt H4), have H7 : 1 ≥ 2, from !mul_one ▸ H ▸ H6, absurd !lt_succ_self (not_lt_of_le H7)) 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, assume H1 : 1 = n * m, eq_one_of_mul_eq_one_right H1⁻¹) end nat
8aac9a70edc6c95c99a188827b401bb10996ee29
60bf3fa4185ec5075eaea4384181bfbc7e1dc319
/src/game/sup_inf/infSumConst.lean
13ed04921598a472d09b829245ded13d92de10a6
[ "Apache-2.0" ]
permissive
anrddh/real-number-game
660f1127d03a78fd35986c771d65c3132c5f4025
c708c4e02ec306c657e1ea67862177490db041b0
refs/heads/master
1,668,214,277,092
1,593,105,075,000
1,593,105,075,000
264,269,218
0
0
null
1,589,567,264,000
1,589,567,264,000
null
UTF-8
Lean
false
false
679
lean
import data.real.basic namespace xena -- hide /- # Chapter 3 : Sup and Inf ## Level 7 Another level that showcases the infimum. -/ def sum_set_const (A : set ℝ) (c : ℝ) := { x : ℝ | ∃ y ∈ A, x = y + c} /- Lemma If $A$ is a set of reals, then $$ \textrm{inf} (c + A) = c + \textrm{inf} (A)$$ -/ lemma inf_sum_set_const (A : set ℝ) (h1A : A.nonempty) (h2A : bdd_below A) (a : ℝ) (c : ℝ): (is_glb A a) → is_glb (sum_set_const A c) (c + a) := begin intro h, cases h with hA hB, split, -- prove that (c+a) is a lower bound intros x h0, cases h0 with y h1, cases h1 with yA h2, sorry, sorry, --needs being worked out end end xena -- hide
18918484eea5756471a21022ae9f1c3142fd9d90
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/idempotents/basic.lean
1efc764aa45332b406260c0cedfd78793369f005
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,724
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import category_theory.abelian.basic /-! # Idempotent complete categories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define the notion of idempotent complete categories (also known as Karoubian categories, or pseudoabelian in the case of preadditive categories). ## Main definitions - `is_idempotent_complete C` expresses that `C` is idempotent complete, i.e. all idempotents in `C` split. Other characterisations of idempotent completeness are given by `is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent` and `is_idempotent_complete_iff_idempotents_have_kernels`. - `is_idempotent_complete_of_abelian` expresses that abelian categories are idempotent complete. - `is_idempotent_complete_iff_of_equivalence` expresses that if two categories `C` and `D` are equivalent, then `C` is idempotent complete iff `D` is. - `is_idempotent_complete_iff_opposite` expresses that `Cᵒᵖ` is idempotent complete iff `C` is. ## References * [Stacks: Karoubian categories] https://stacks.math.columbia.edu/tag/09SF -/ open category_theory open category_theory.category open category_theory.limits open category_theory.preadditive open opposite namespace category_theory variables (C : Type*) [category C] /-- A category is idempotent complete iff all idempotent endomorphisms `p` split as a composition `p = e ≫ i` with `i ≫ e = 𝟙 _` -/ class is_idempotent_complete : Prop := (idempotents_split : ∀ (X : C) (p : X ⟶ X), p ≫ p = p → ∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) namespace idempotents /-- A category is idempotent complete iff for all idempotent endomorphisms, the equalizer of the identity and this idempotent exists. -/ lemma is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent : is_idempotent_complete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → has_equalizer (𝟙 X) p := begin split, { introI, intros X p hp, rcases is_idempotent_complete.idempotents_split X p hp with ⟨Y, i, e, ⟨h₁, h₂⟩⟩, exact ⟨nonempty.intro { cone := fork.of_ι i (show i ≫ 𝟙 X = i ≫ p, by rw [comp_id, ← h₂, ← assoc, h₁, id_comp]), is_limit := begin apply fork.is_limit.mk', intro s, refine ⟨s.ι ≫ e, _⟩, split, { erw [assoc, h₂, ← limits.fork.condition s, comp_id], }, { intros m hm, rw fork.ι_of_ι at hm, rw [← hm], simp only [← hm, assoc, h₁], exact (comp_id m).symm } end }⟩, }, { intro h, refine ⟨_⟩, intros X p hp, haveI := h X p hp, use equalizer (𝟙 X) p, use equalizer.ι (𝟙 X) p, use equalizer.lift p (show p ≫ 𝟙 X = p ≫ p, by rw [hp, comp_id]), split, { ext, rw [assoc, equalizer.lift_ι, id_comp], conv { to_rhs, erw [← comp_id (equalizer.ι (𝟙 X) p)], }, exact (limits.fork.condition (equalizer.fork (𝟙 X) p)).symm, }, { rw [equalizer.lift_ι], }, } end variables {C} /-- In a preadditive category, when `p : X ⟶ X` is idempotent, then `𝟙 X - p` is also idempotent. -/ lemma idem_of_id_sub_idem [preadditive C] {X : C} (p : X ⟶ X) (hp : p ≫ p = p) : (𝟙 _ - p) ≫ (𝟙 _ - p) = (𝟙 _ - p) := by simp only [comp_sub, sub_comp, id_comp, comp_id, hp, sub_self, sub_zero] variables (C) /-- A preadditive category is pseudoabelian iff all idempotent endomorphisms have a kernel. -/ lemma is_idempotent_complete_iff_idempotents_have_kernels [preadditive C] : is_idempotent_complete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → has_kernel p := begin rw is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent, split, { intros h X p hp, haveI := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp), convert has_kernel_of_has_equalizer (𝟙 X) (𝟙 X - p), rw [sub_sub_cancel], }, { intros h X p hp, haveI : has_kernel (𝟙 _ - p) := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp), apply preadditive.has_equalizer_of_has_kernel, }, end /-- An abelian category is idempotent complete. -/ @[priority 100] instance is_idempotent_complete_of_abelian (D : Type*) [category D] [abelian D] : is_idempotent_complete D := by { rw is_idempotent_complete_iff_idempotents_have_kernels, intros, apply_instance, } variables {C} lemma split_imp_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X') (hpp' : p ≫ φ.hom = φ.hom ≫ p') (h : ∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) : (∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p') := begin rcases h with ⟨Y, i, e, ⟨h₁, h₂⟩⟩, use [Y, i ≫ φ.hom, φ.inv ≫ e], split, { slice_lhs 2 3 { rw φ.hom_inv_id, }, rw [id_comp, h₁], }, { slice_lhs 2 3 { rw h₂, }, rw [hpp', ← assoc, φ.inv_hom_id, id_comp], } end lemma split_iff_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X') (hpp' : p ≫ φ.hom = φ.hom ≫ p') : (∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) ↔ (∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p') := begin split, { exact split_imp_of_iso φ p p' hpp', }, { apply split_imp_of_iso φ.symm p' p, rw [← comp_id p, ← φ.hom_inv_id], slice_rhs 2 3 { rw hpp', }, slice_rhs 1 2 { erw φ.inv_hom_id, }, simpa only [id_comp], }, end lemma equivalence.is_idempotent_complete {D : Type*} [category D] (ε : C ≌ D) (h : is_idempotent_complete C) : is_idempotent_complete D := begin refine ⟨_⟩, intros X' p hp, let φ := ε.counit_iso.symm.app X', erw split_iff_of_iso φ p (φ.inv ≫ p ≫ φ.hom) (by { slice_rhs 1 2 { rw φ.hom_inv_id, }, rw id_comp,}), rcases is_idempotent_complete.idempotents_split (ε.inverse.obj X') (ε.inverse.map p) (by rw [← ε.inverse.map_comp, hp]) with ⟨Y, i, e, ⟨h₁,h₂⟩⟩, use [ε.functor.obj Y, ε.functor.map i, ε.functor.map e], split, { rw [← ε.functor.map_comp, h₁, ε.functor.map_id], }, { simpa only [← ε.functor.map_comp, h₂, equivalence.fun_inv_map], }, end /-- If `C` and `D` are equivalent categories, that `C` is idempotent complete iff `D` is. -/ lemma is_idempotent_complete_iff_of_equivalence {D : Type*} [category D] (ε : C ≌ D) : is_idempotent_complete C ↔ is_idempotent_complete D := begin split, { exact equivalence.is_idempotent_complete ε, }, { exact equivalence.is_idempotent_complete ε.symm, }, end lemma is_idempotent_complete_of_is_idempotent_complete_opposite (h : is_idempotent_complete Cᵒᵖ) : is_idempotent_complete C := begin refine ⟨_⟩, intros X p hp, rcases is_idempotent_complete.idempotents_split (op X) p.op (by rw [← op_comp, hp]) with ⟨Y, i, e, ⟨h₁, h₂⟩⟩, use [Y.unop, e.unop, i.unop], split, { simpa only [← unop_comp, h₁], }, { simpa only [← unop_comp, h₂], }, end lemma is_idempotent_complete_iff_opposite : is_idempotent_complete Cᵒᵖ ↔ is_idempotent_complete C := begin split, { exact is_idempotent_complete_of_is_idempotent_complete_opposite, }, { intro h, apply is_idempotent_complete_of_is_idempotent_complete_opposite, rw is_idempotent_complete_iff_of_equivalence (op_op_equivalence C), exact h, }, end instance [is_idempotent_complete C] : is_idempotent_complete (Cᵒᵖ) := by rwa is_idempotent_complete_iff_opposite end idempotents end category_theory
bfe4603a7a3dbed43243487fa98af27ed85832c1
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/real/nnreal.lean
bb94e0d712686e0bd109d781ad59abec48a32576
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
15,503
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import data.real.basic order.lattice algebra.field noncomputable theory open lattice local attribute [instance] classical.prop_decidable def nnreal := {r : ℝ // 0 ≤ r} local notation ` ℝ≥0 ` := nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_div ℝ≥0 := ⟨λa b, ⟨a.1 / b.1, div_nonneg' a.2 b.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp] protected lemma coe_sub (r₁ r₂ : ℝ≥0) (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma zero_div (r : ℝ≥0) : 0 / r = 0 := nnreal.eq (zero_div _) @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := @nnreal.eq_iff r 0 instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero] } end instance : is_semiring_hom (coe : ℝ≥0 → ℝ) := by refine_struct {..}; intros; refl lemma sum_coe {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.sum f) = s.sum (λa, (f a : ℝ)) := eq.symm $ finset.sum_hom _ lemma prod_coe {α} {s : finset α} {f : α → ℝ≥0} : ↑(s.prod f) = s.prod (λa, (f a : ℝ)) := eq.symm $ finset.prod_hom _ lemma smul_coe (r : ℝ≥0) : ∀n, ↑(add_monoid.smul n r) = add_monoid.smul n (r:ℝ) | 0 := rfl | (n + 1) := by simp [add_monoid.add_smul, smul_coe n] @[simp] protected lemma coe_nat_cast : ∀(n : ℕ), (↑(↑n : ℝ≥0) : ℝ) = n | 0 := rfl | (n + 1) := by simp [coe_nat_cast n] instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective protected lemma coe_le {r₁ r₂ : ℝ≥0} : r₁ ≤ r₂ ↔ (r₁ : ℝ) ≤ r₂ := iff.rfl protected lemma coe_lt {r₁ r₂ : ℝ≥0} : r₁ < r₂ ↔ (r₁ : ℝ) < r₂ := iff.rfl protected lemma coe_pos {r : ℝ≥0} : 0 < r ↔ (0 : ℝ) < r := iff.rfl instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.lattice.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.lattice.order_bot, .. nnreal.lattice.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.lattice.order_bot, .. nnreal.lattice.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_le_mul_of_nonneg_left := assume a b c, @mul_le_mul_of_nonneg_left ℝ _ a b c, mul_le_mul_of_nonneg_right := assume a b c, @mul_le_mul_of_nonneg_right ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : canonically_ordered_comm_semiring ℝ≥0 := { zero_ne_one := assume h, @zero_ne_one ℝ _ $ congr_arg subtype.val $ h, mul_eq_zero_iff := assume a b, nnreal.eq_iff.symm.trans $ mul_eq_zero.trans $ by simp, .. nnreal.linear_ordered_semiring, .. nnreal.canonically_ordered_monoid, .. nnreal.comm_semiring } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb _ $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb _ $ hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin by_cases h : s = ∅, { simp [h, set.image_empty, real.Sup_empty] }, rcases set.ne_empty_iff_exists_mem.1 h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin by_cases h : s = ∅, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (by simp [h]) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h _ hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h _ hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice.lattice_of_decidable_linear_order, .. nnreal.lattice.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (add_monoid.smul n y : nnreal), by simp [*, smul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_coe {r : nnreal} : nnreal.of_real r = r := nnreal.eq $ by simp [nnreal.of_real] @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.coe_le.2 $ max_le_max h $ le_refl _ lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le.2 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact eq_of_mul_eq_mul_left (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end end mul section sub lemma sub_eq_zero {r p : nnreal} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le] using h @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [nnreal.coe_le, nnreal.coe_le, nnreal.coe_sub _ _ h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le, nnreal.coe_le, sub_eq_zero h] end lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub _ _ h, sub_add_cancel] end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := by rw [← nnreal.eq_iff, nnreal.coe_inv, nnreal.coe_zero, inv_eq_zero, ← nnreal.coe_zero, nnreal.eq_iff] @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq inv_inv' @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_if_le_inv {a b r : nnreal} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [nnreal.coe_lt, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) end inv end nnreal
004a211074e83712f559cf71b36b64c9bace90e9
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/src/o_minimal/o_minimal.lean
ec3e4db15a443d4bd7f458920eb42670103ce982
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
4,118
lean
import for_mathlib.boolean_subalgebra import for_mathlib.closure import for_mathlib.order import o_minimal.order import o_minimal.tame.basic /- Definition of an *o-minimal* structure. -/ namespace o_minimal set_option old_structure_cmd true -- TODO: Is this needed? variables {R : Type*} [DUNLO R] lemma interval_or_point.def_set {S : struc R} [definable_constants S] [is_definable_le S R] : ∀ {i : set R}, interval_or_point i → def_set S i | _ (interval_or_point.pt _) := def_val_const | _ interval_or_point.Iii := def_set_univ _ | _ (interval_or_point.Ioi a) := def_set.Ioi a | _ (interval_or_point.Iio b) := def_set.Iio b | _ (interval_or_point.Ioo a b _) := def_set.Ioo a b /-- Tame sets are definable. -/ lemma tame.def_set {S : struc R} [definable_constants S] [is_definable_le S R] {s : set R} (h : tame s) : def_set S s := tame.induction (def_set_empty _) (λ s _ i ih, i.def_set.union ih) h /-- An o-minimal structure is one in which every definable subset of R is tame. -/ def is_o_minimal {S : struc R} [definable_constants S] [is_definable_le S R] : Prop := ∀ (s : set R), def_set S s → tame s /-- An o-minimal structure on a DUNLO `R` is a structure `S` for which: 1. `S` has definable constants. 2. The `≤` relation on `R` is definable in `S`. 3. Every definable subset of `R` is tame (a union of points and open intervals). Below we verify that this definition agrees with the one of [vdD]. -/ class o_minimal (S : struc R) extends definable_constants S, is_definable_le S R : Prop := (tame_of_def : ∀ {s : set R}, def_set S s → tame s) /-- Version of `o_minimal.tame_of_def` with explicit structure argument. This is useful because otherwise `S` can only be inferred from the proof of definability, which we might want to produce automatically. -/ lemma tame_of_def (S : struc R) [o_minimal S] {s : set R} : def_set S s → tame s := o_minimal.tame_of_def /-- Our definition of an o-minimal structure is equivalent to the one in [vdD:1.3.2]. -/ lemma o_minimal_iff_vdD (S : struc R) : o_minimal S ↔ (def_set S {p : R × R | p.1 < p.2} ∧ ∀ (s : set R), def_set S s ↔ tame s) := ⟨λ h, by exactI ⟨definable_lt', λ s, ⟨o_minimal.tame_of_def, tame.def_set⟩⟩, λ ⟨h₁, h₂⟩, { definable_val := λ r, (h₂ _).mpr (tame_single (interval_or_point.pt r)), definable_le' := (is_definable_le_of_definable_lt h₁).1, tame_of_def := λ s, (h₂ s).mp }⟩ -- TODO: for_mathlib lemma function.const_injective {α β : Type*} [H : nonempty α] : function.injective (function.const α : β → α → β) := let ⟨a⟩ := H in λ b₁ b₂ h, congr_fun h a /-- An alternate constructor expressed in terms of low-level definability. -/ lemma o_minimal.mk' (S : struc R) (definable_lt : S.definable {x : finvec 2 R | x 0 < x 1}) (definable_const : ∀ (r : R), S.definable {x : finvec 1 R | x 0 = r}) (tame_of_definable : ∀ (s : set (finvec 1 R)), S.definable s → tame {r | (λ _, r : finvec 1 R) ∈ s}) : o_minimal S := { definable_val := begin intro r, unfold def_val def_set, convert definable_const r, apply set.ext, simp_rw [set.mem_image], change ∀ (x : fin 1 → R), _ ↔ x 0 = r, rw (equiv.fun_unique (fin 1) R).forall_congr_left', intro r', simp only [equiv.fun_unique, equiv.coe_fn_symm_mk, set.mem_singleton_iff, exists_eq_left, has_coordinates.self_coords, function.const_injective.eq_iff], exact eq_comm end, definable_le' := begin refine (is_definable_le_of_definable_lt _).1, unfold def_val def_set, convert definable_lt, apply set.ext, simp_rw [set.mem_image], change ∀ (x : finvec 2 R), _ ↔ x 0 < x 1, rw finvec.finvec_two_equiv_prod_self.forall_congr_left', rintro ⟨x₀, x₁⟩, change _ ↔ x₀ < x₁, simp [has_coordinates.prod_coords, finvec.finvec_two_equiv_prod_self, finvec.append.inj_iff, function.const_injective.eq_iff, ←and_assoc] end, tame_of_def := begin intros s hs, convert tame_of_definable _ hs, ext x, simp [function.const_injective.eq_iff] end } end o_minimal
f4b698e97840d314ea34ff0885cf6c49eeadc5ed
92b50235facfbc08dfe7f334827d47281471333b
/hott/types/trunc.hlean
62f191dec33453625519c98b2582b42ae2abc886
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
10,116
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Properties of is_trunc and trunctype -/ -- NOTE: the fact that (is_trunc n A) is a mere proposition is proved in .hprop_trunc import types.pi types.eq types.equiv .function open eq sigma sigma.ops pi function equiv is_trunc.trunctype is_equiv prod is_trunc.trunc_index pointed nat namespace is_trunc variables {A B : Type} {n : trunc_index} definition is_trunc_succ_of_imp_is_trunc_succ (H : A → is_trunc (n.+1) A) : is_trunc (n.+1) A := @is_trunc_succ_intro _ _ (λx y, @is_trunc_eq _ _ (H x) x y) definition is_trunc_of_imp_is_trunc_of_leq (Hn : -1 ≤ n) (H : A → is_trunc n A) : is_trunc n A := trunc_index.rec_on n (λHn H, empty.rec _ Hn) (λn IH Hn, is_trunc_succ_of_imp_is_trunc_succ) Hn H /- theorems about trunctype -/ protected definition trunctype.sigma_char.{l} (n : trunc_index) : (trunctype.{l} n) ≃ (Σ (A : Type.{l}), is_trunc n A) := begin fapply equiv.MK, { intro A, exact (⟨carrier A, struct A⟩)}, { intro S, exact (trunctype.mk S.1 S.2)}, { intro S, apply (sigma.rec_on S), intro S1 S2, apply idp}, { intro A, apply (trunctype.rec_on A), intro A1 A2, apply idp}, end definition trunctype_eq_equiv (n : trunc_index) (A B : n-Type) : (A = B) ≃ (carrier A = carrier B) := calc (A = B) ≃ (to_fun (trunctype.sigma_char n) A = to_fun (trunctype.sigma_char n) B) : eq_equiv_fn_eq_of_equiv ... ≃ ((to_fun (trunctype.sigma_char n) A).1 = (to_fun (trunctype.sigma_char n) B).1) : equiv.symm (!equiv_subtype) ... ≃ (carrier A = carrier B) : equiv.refl definition is_trunc_is_embedding_closed (f : A → B) [Hf : is_embedding f] [HB : is_trunc n B] (Hn : -1 ≤ n) : is_trunc n A := begin cases n with n, {exact !empty.elim Hn}, {apply is_trunc_succ_intro, intro a a', fapply @is_trunc_is_equiv_closed_rev _ _ n (ap f)} end definition is_trunc_is_retraction_closed (f : A → B) [Hf : is_retraction f] (n : trunc_index) [HA : is_trunc n A] : is_trunc n B := begin revert A B f Hf HA, eapply (trunc_index.rec_on n), { clear n, intro A B f Hf HA, cases Hf with g ε, fapply is_contr.mk, { exact f (center A)}, { intro b, apply concat, { apply (ap f), exact (center_eq (g b))}, { apply ε}}}, { clear n, intro n IH A B f Hf HA, cases Hf with g ε, apply is_trunc_succ_intro, intro b b', fapply (IH (g b = g b')), { intro q, exact ((ε b)⁻¹ ⬝ ap f q ⬝ ε b')}, { apply (is_retraction.mk (ap g)), { intro p, cases p, {rewrite [↑ap, con.left_inv]}}}, { apply is_trunc_eq}} end definition is_embedding_to_fun (A B : Type) : is_embedding (@to_fun A B) := is_embedding.mk (λf f', !is_equiv_ap_to_fun) definition is_trunc_trunctype [instance] (n : trunc_index) : is_trunc n.+1 (n-Type) := begin apply is_trunc_succ_intro, intro X Y, fapply is_trunc_equiv_closed, {apply equiv.symm, apply trunctype_eq_equiv}, fapply is_trunc_equiv_closed, {apply equiv.symm, apply eq_equiv_equiv}, cases n, {apply @is_contr_of_inhabited_hprop, {apply is_trunc_is_embedding_closed, {apply is_embedding_to_fun} , {exact unit.star}}, {apply equiv_of_is_contr_of_is_contr}}, {apply is_trunc_is_embedding_closed, {apply is_embedding_to_fun}, {exact unit.star}} end /- theorems about decidable equality and axiom K -/ definition is_hset_of_axiom_K {A : Type} (K : Π{a : A} (p : a = a), p = idp) : is_hset A := is_hset.mk _ (λa b p q, eq.rec_on q K p) theorem is_hset_of_relation.{u} {A : Type.{u}} (R : A → A → Type.{u}) (mere : Π(a b : A), is_hprop (R a b)) (refl : Π(a : A), R a a) (imp : Π{a b : A}, R a b → a = b) : is_hset A := is_hset_of_axiom_K (λa p, have H2 : transport (λx, R a x → a = x) p (@imp a a) = @imp a a, from !apd, have H3 : Π(r : R a a), transport (λx, a = x) p (imp r) = imp (transport (λx, R a x) p r), from to_fun (equiv.symm !heq_pi) H2, have H4 : imp (refl a) ⬝ p = imp (refl a), from calc imp (refl a) ⬝ p = transport (λx, a = x) p (imp (refl a)) : transport_eq_r ... = imp (transport (λx, R a x) p (refl a)) : H3 ... = imp (refl a) : is_hprop.elim, cancel_left H4) definition relation_equiv_eq {A : Type} (R : A → A → Type) (mere : Π(a b : A), is_hprop (R a b)) (refl : Π(a : A), R a a) (imp : Π{a b : A}, R a b → a = b) (a b : A) : R a b ≃ a = b := @equiv_of_is_hprop _ _ _ (@is_trunc_eq _ _ (is_hset_of_relation R mere refl @imp) a b) imp (λp, p ▸ refl a) local attribute not [reducible] definition is_hset_of_double_neg_elim {A : Type} (H : Π(a b : A), ¬¬a = b → a = b) : is_hset A := is_hset_of_relation (λa b, ¬¬a = b) _ (λa n, n idp) H section open decidable --this is proven differently in init.hedberg definition is_hset_of_decidable_eq (A : Type) [H : decidable_eq A] : is_hset A := is_hset_of_double_neg_elim (λa b, by_contradiction) end definition is_trunc_of_axiom_K_of_leq {A : Type} (n : trunc_index) (H : -1 ≤ n) (K : Π(a : A), is_trunc n (a = a)) : is_trunc (n.+1) A := @is_trunc_succ_intro _ _ (λa b, is_trunc_of_imp_is_trunc_of_leq H (λp, eq.rec_on p !K)) definition is_trunc_succ_of_is_trunc_loop (Hn : -1 ≤ n) (Hp : Π(a : A), is_trunc n (a = a)) : is_trunc (n.+1) A := begin apply is_trunc_succ_intro, intros a a', apply is_trunc_of_imp_is_trunc_of_leq Hn, intro p, cases p, apply Hp end definition is_trunc_succ_iff_is_trunc_loop (A : Type) (Hn : -1 ≤ n) : is_trunc (n.+1) A ↔ Π(a : A), is_trunc n (a = a) := iff.intro _ (is_trunc_succ_of_is_trunc_loop Hn) definition is_trunc_iff_is_contr_loop_succ (n : ℕ) (A : Type) : is_trunc n A ↔ Π(a : A), is_contr (Ω[succ n](Pointed.mk a)) := begin revert A, induction n with n IH, { intros, esimp [Iterated_loop_space], apply iff.intro, { intros H a, apply is_contr.mk idp, apply is_hprop.elim}, { intro H, apply is_hset_of_axiom_K, intros, apply is_hprop.elim}}, { intros, transitivity _, apply @is_trunc_succ_iff_is_trunc_loop n, constructor, apply iff.pi_iff_pi, intros, transitivity _, apply IH, assert H : Πp : a = a, Ω(Pointed.mk p) = Ω(Pointed.mk (idpath a)), { intros, fapply Pointed_eq, { esimp, transitivity _, apply eq_equiv_fn_eq_of_equiv (equiv_eq_closed_right _ p⁻¹), esimp, apply eq_equiv_eq_closed, apply con.right_inv, apply con.right_inv}, { esimp, apply con.left_inv}}, transitivity _, apply iff.pi_iff_pi, intro p, rewrite [↑Iterated_loop_space,H], apply iff.refl, apply iff.imp_iff, reflexivity} end definition is_trunc_iff_is_contr_loop (n : ℕ) (A : Type) : is_trunc (n.-2.+1) A ↔ (Π(a : A), is_contr (Ω[n](pointed.Mk a))) := begin cases n with n, { esimp [sub_two,Iterated_loop_space], apply iff.intro, intro H a, exact is_contr_of_inhabited_hprop a, intro H, apply is_hprop_of_imp_is_contr, exact H}, { apply is_trunc_iff_is_contr_loop_succ}, end end is_trunc open is_trunc namespace trunc variable {A : Type} protected definition code (n : trunc_index) (aa aa' : trunc n.+1 A) : n-Type := trunc.rec_on aa (λa, trunc.rec_on aa' (λa', trunctype.mk' n (trunc n (a = a')))) protected definition encode (n : trunc_index) (aa aa' : trunc n.+1 A) : aa = aa' → trunc.code n aa aa' := begin intro p, cases p, apply (trunc.rec_on aa), intro a, esimp [trunc.code,trunc.rec_on], exact (tr idp) end protected definition decode (n : trunc_index) (aa aa' : trunc n.+1 A) : trunc.code n aa aa' → aa = aa' := begin eapply (trunc.rec_on aa'), eapply (trunc.rec_on aa), intro a a' x, esimp [trunc.code, trunc.rec_on] at x, apply (trunc.rec_on x), intro p, exact (ap tr p) end definition trunc_eq_equiv (n : trunc_index) (aa aa' : trunc n.+1 A) : aa = aa' ≃ trunc.code n aa aa' := begin fapply equiv.MK, { apply trunc.encode}, { apply trunc.decode}, { eapply (trunc.rec_on aa'), eapply (trunc.rec_on aa), intro a a' x, esimp [trunc.code, trunc.rec_on] at x, apply (trunc.rec_on x), intro p, cases p, exact idp}, { intro p, cases p, apply (trunc.rec_on aa), intro a, exact idp}, end definition tr_eq_tr_equiv (n : trunc_index) (a a' : A) : (tr a = tr a' :> trunc n.+1 A) ≃ trunc n (a = a') := !trunc_eq_equiv definition is_trunc_trunc_of_is_trunc [instance] [priority 500] (A : Type) (n m : trunc_index) [H : is_trunc n A] : is_trunc n (trunc m A) := begin revert A m H, eapply (trunc_index.rec_on n), { clear n, intro A m H, apply is_contr_equiv_closed, { apply equiv_trunc, apply (@is_trunc_of_leq _ -2), exact unit.star} }, { clear n, intro n IH A m H, cases m with m, { apply (@is_trunc_of_leq _ -2), exact unit.star}, { apply is_trunc_succ_intro, intro aa aa', apply (@trunc.rec_on _ _ _ aa (λy, !is_trunc_succ_of_is_hprop)), eapply (@trunc.rec_on _ _ _ aa' (λy, !is_trunc_succ_of_is_hprop)), intro a a', apply (is_trunc_equiv_closed_rev), { apply tr_eq_tr_equiv}, { exact (IH _ _ _)}}} end end trunc open trunc namespace function variables {A B : Type} definition is_surjective_of_is_equiv [instance] (f : A → B) [H : is_equiv f] : is_surjective f := is_surjective.mk (λb, !center) definition is_equiv_equiv_is_embedding_times_is_surjective (f : A → B) : is_equiv f ≃ (is_embedding f × is_surjective f) := equiv_of_is_hprop (λH, (_, _)) (λP, prod.rec_on P (λH₁ H₂, !is_equiv_of_is_surjective_of_is_embedding)) end function
b03a0bad8055cf48cebdfcdff525de6e93c7281d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/ffi/lib/lean/FFI/Fn.lean
ec3d7d49600ae3654386746b3c5edc19ed3616e7
[ "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
52
lean
@[extern "my_lean_fun"] opaque myLeanFun : IO PUnit
8c187722c249995452fb38efe8be5ce13afb3126
cf39355caa609c0f33405126beee2739aa3cb77e
/library/data/buffer.lean
46bab575bfbc72e4dd871d8f7e366232a85054d4
[ "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
4,825
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 -/ universes u w def buffer (α : Type u) := Σ n, array n α def mk_buffer {α : Type u} : buffer α := ⟨0, {data := λ i, fin.elim0 i}⟩ def array.to_buffer {α : Type u} {n : nat} (a : array n α) : buffer α := ⟨n, a⟩ namespace buffer variables {α : Type u} {β : Type w} def nil : buffer α := mk_buffer def size (b : buffer α) : nat := b.1 def to_array (b : buffer α) : array (b.size) α := b.2 def push_back : buffer α → α → buffer α | ⟨n, a⟩ v := ⟨n+1, a.push_back v⟩ def pop_back : buffer α → buffer α | ⟨0, a⟩ := ⟨0, a⟩ | ⟨n+1, a⟩ := ⟨n, a.pop_back⟩ def read : Π (b : buffer α), fin b.size → α | ⟨n, a⟩ i := a.read i def write : Π (b : buffer α), fin b.size → α → buffer α | ⟨n, a⟩ i v := ⟨n, a.write i v⟩ def read' [inhabited α] : buffer α → nat → α | ⟨n, a⟩ i := a.read' i def write' : buffer α → nat → α → buffer α | ⟨n, a⟩ i v := ⟨n, a.write' i v⟩ lemma read_eq_read' [inhabited α] (b : buffer α) (i : nat) (h : i < b.size) : read b ⟨i, h⟩ = read' b i := by cases b; unfold read read'; simp [array.read_eq_read'] lemma write_eq_write' (b : buffer α) (i : nat) (h : i < b.size) (v : α) : write b ⟨i, h⟩ v = write' b i v := by cases b; unfold write write'; simp [array.write_eq_write'] def to_list (b : buffer α) : list α := b.to_array.to_list protected def to_string (b : buffer char) : string := b.to_array.to_list.as_string def append_list {α : Type u} : buffer α → list α → buffer α | b [] := b | b (v::vs) := append_list (b.push_back v) vs def append_string (b : buffer char) (s : string) : buffer char := b.append_list s.to_list lemma lt_aux_1 {a b c : nat} (h : a + c < b) : a < b := lt_of_le_of_lt (nat.le_add_right a c) h lemma lt_aux_2 {n : nat} (h : 0 < n) : n - 1 < n := nat.sub_lt h (nat.succ_pos 0) lemma lt_aux_3 {n i} (h : i + 1 < n) : n - 2 - i < n := have n > 0, from lt_trans (nat.zero_lt_succ i) h, have n - 2 < n, from nat.sub_lt this (dec_trivial), lt_of_le_of_lt (nat.sub_le _ _) this def append_array {α : Type u} {n : nat} (nz : 0 < n) : buffer α → array n α → ∀ i : nat, i < n → buffer α | ⟨m, b⟩ a 0 _ := let i : fin n := ⟨n - 1, lt_aux_2 nz⟩ in ⟨m+1, b.push_back (a.read i)⟩ | ⟨m, b⟩ a (j+1) h := let i : fin n := ⟨n - 2 - j, lt_aux_3 h⟩ in append_array ⟨m+1, b.push_back (a.read i)⟩ a j (lt_aux_1 h) protected def append {α : Type u} : buffer α → buffer α → buffer α | b ⟨0, a⟩ := b | b ⟨n+1, a⟩ := append_array (nat.zero_lt_succ _) b a n (nat.lt_succ_self _) def iterate : Π b : buffer α, β → (fin b.size → α → β → β) → β | ⟨_, a⟩ b f := a.iterate b f def foreach : Π b : buffer α, (fin b.size → α → α) → buffer α | ⟨n, a⟩ f := ⟨n, a.foreach f⟩ /-- Monadically map a function over the buffer. -/ @[inline] def mmap {m} [monad m] (b : buffer α) (f : α → m β) : m (buffer β) := do b' ← b.2.mmap f, return b'.to_buffer /-- Map a function over the buffer. -/ @[inline] def map : buffer α → (α → β) → buffer β | ⟨n, a⟩ f := ⟨n, a.map f⟩ def foldl : buffer α → β → (α → β → β) → β | ⟨_, a⟩ b f := a.foldl b f def rev_iterate : Π (b : buffer α), β → (fin b.size → α → β → β) → β | ⟨_, a⟩ b f := a.rev_iterate b f def take (b : buffer α) (n : nat) : buffer α := if h : n ≤ b.size then ⟨n, b.to_array.take n h⟩ else b def take_right (b : buffer α) (n : nat) : buffer α := if h : n ≤ b.size then ⟨n, b.to_array.take_right n h⟩ else b def drop (b : buffer α) (n : nat) : buffer α := if h : n ≤ b.size then ⟨_, b.to_array.drop n h⟩ else b def reverse (b : buffer α) : buffer α := ⟨b.size, b.to_array.reverse⟩ protected def mem (v : α) (a : buffer α) : Prop := ∃i, read a i = v instance : has_mem α (buffer α) := ⟨buffer.mem⟩ instance : has_append (buffer α) := ⟨buffer.append⟩ instance [has_repr α] : has_repr (buffer α) := ⟨repr ∘ to_list⟩ meta instance [has_to_format α] : has_to_format (buffer α) := ⟨to_fmt ∘ to_list⟩ meta instance [has_to_tactic_format α] : has_to_tactic_format (buffer α) := ⟨tactic.pp ∘ to_list⟩ end buffer def list.to_buffer {α : Type u} (l : list α) : buffer α := mk_buffer.append_list l @[reducible] def char_buffer := buffer char /-- Convert a format object into a character buffer with the provided formatting options. -/ meta constant format.to_buffer : format → options → buffer char def string.to_char_buffer (s : string) : char_buffer := buffer.nil.append_string s
c325e97b01a51257852f6d7bc7a12170a7be7a06
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/topology/sequences.lean
da68e7f987a297833d6e0984c7d74aed040533fb
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
10,529
lean
/- Copyright (c) 2018 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow Sequences in topological spaces. In this file we define sequences in topological spaces and show how they are related to filters and the topology. In particular, we * associate a filter with a sequence and prove equivalence of convergence of the two, * define the sequential closure of a set and prove that it's contained in the closure, * define a type class "sequential_space" in which closure and sequential closure agree, * define sequential continuity and show that it coincides with continuity in sequential spaces, * provide an instance that shows that every metric space is a sequential space. TODO: * There should be an instance that associates a sequential space with a first countable space. * Sequential compactness should be handled here. -/ import topology.basic topology.metric_space.basic import analysis.specific_limits open set filter variables {α : Type*} {β : Type*} local notation f `⟶` limit := tendsto f at_top (nhds limit) /- Statements about sequences in general topological spaces. -/ section topological_space variables [topological_space α] [topological_space β] /-- A sequence converges in the sence of topological spaces iff the associated statement for filter holds. -/ @[simp] lemma topological_space.seq_tendsto_iff {x : ℕ → α} {limit : α} : tendsto x at_top (nhds limit) ↔ ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U := iff.intro (assume ttol : tendsto x at_top (nhds limit), show ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, from assume U limitInU isOpenU, have {n | (x n) ∈ U} ∈ at_top := mem_map.mp $ le_def.mp ttol U $ mem_nhds_sets isOpenU limitInU, show ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, from mem_at_top_sets.mp this) (assume xtol : ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, suffices ∀ U, is_open U → limit ∈ U → x ⁻¹' U ∈ at_top, from tendsto_nhds.mpr this, assume U isOpenU limitInU, suffices ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, by simp [this], xtol U limitInU isOpenU) /-- The sequential closure of a subset M ⊆ α of a topological space α is the set of all p ∈ α which arise as limit of sequences in M. -/ def sequential_closure (M : set α) : set α := {p | ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ M) ∧ (x ⟶ p)} lemma subset_sequential_closure (M : set α) : M ⊆ sequential_closure M := assume p (_ : p ∈ M), show p ∈ sequential_closure M, from ⟨λ n, p, assume n, ‹p ∈ M›, tendsto_const_nhds⟩ def is_seq_closed (A : set α) : Prop := A = sequential_closure A /-- A convenience lemma for showing that a set is sequentially closed. -/ lemma is_seq_closed_of_def {A : set α} (h : ∀(x : ℕ → α) (p : α), (∀ n : ℕ, x n ∈ A) → (x ⟶ p) → p ∈ A) : is_seq_closed A := show A = sequential_closure A, from subset.antisymm (subset_sequential_closure A) (show ∀ p, p ∈ sequential_closure A → p ∈ A, from (assume p ⟨x, _, _⟩, show p ∈ A, from h x p ‹∀ n : ℕ, ((x n) ∈ A)› ‹(x ⟶ p)›)) /-- The sequential closure of a set is contained in the closure of that set. The converse is not true. -/ lemma sequential_closure_subset_closure (M : set α) : sequential_closure M ⊆ closure M := show ∀ p, p ∈ sequential_closure M → p ∈ closure M, from assume p, assume : ∃ x : ℕ → α, (∀ n : ℕ, ((x n) ∈ M)) ∧ (x ⟶ p), let ⟨x, ⟨_, _⟩⟩ := this in show p ∈ closure M, from -- we have to show that p is in the closure of M -- using mem_closure_iff, this is equivalent to proving that every open neighbourhood -- has nonempty intersection with M, but this is witnessed by our sequence x suffices ∀ O, is_open O → p ∈ O → O ∩ M ≠ ∅, from mem_closure_iff.mpr this, have ∀ (U : set α), p ∈ U → is_open U → (∃ n0, ∀ n, n ≥ n0 → x n ∈ U), by rwa[←topological_space.seq_tendsto_iff], assume O is_open_O p_in_O, let ⟨n0, _⟩ := this O ‹p ∈ O› ‹is_open O› in have (x n0) ∈ O, from ‹∀ n ≥ n0, x n ∈ O› n0 (show n0 ≥ n0, from le_refl n0), have (x n0) ∈ O ∩ M, from ⟨this, ‹∀n, x n ∈ M› n0⟩, set.ne_empty_of_mem this /-- A set is sequentially closed if it is closed. -/ lemma is_seq_closed_of_is_closed (M : set α) (_ : is_closed M) : is_seq_closed M := suffices sequential_closure M ⊆ M, from set.eq_of_subset_of_subset (subset_sequential_closure M) this, calc sequential_closure M ⊆ closure M : sequential_closure_subset_closure M ... = M : closure_eq_of_is_closed ‹is_closed M› /-- The limit of a convergent sequence in a sequentially closed set is in that set.-/ lemma mem_of_is_seq_closed {A : set α} (_ : is_seq_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : (x ⟶ limit)) : limit ∈ A := have limit ∈ sequential_closure A, from show ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ A) ∧ (x ⟶ limit), from ⟨x, ‹∀ n, x n ∈ A›, ‹(x ⟶ limit)›⟩, eq.subst (eq.symm ‹is_seq_closed A›) ‹limit ∈ sequential_closure A› /-- The limit of a convergent sequence in a closed set is in that set.-/ lemma mem_of_is_closed_sequential {A : set α} (_ : is_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : x ⟶ limit) : limit ∈ A := mem_of_is_seq_closed (is_seq_closed_of_is_closed A ‹is_closed A›) ‹∀ n, x n ∈ A› ‹(x ⟶ limit)› /-- A sequential space is a space in which 'sequences are enough to probe the topology'. This can be formalised by demanding that the sequential closure and the closure coincide. The following statements show that other topological properties can be deduced from sequences in sequential spaces. -/ class sequential_space (α : Type*) [topological_space α] : Prop := (sequential_closure_eq_closure : ∀ M : set α, sequential_closure M = closure M) /-- In a sequential space, a set is closed iff it's sequentially closed. -/ lemma is_seq_closed_iff_is_closed [sequential_space α] {M : set α} : is_seq_closed M ↔ is_closed M := iff.intro (assume _, closure_eq_iff_is_closed.mp (eq.symm (calc M = sequential_closure M : by assumption ... = closure M : sequential_space.sequential_closure_eq_closure M))) (is_seq_closed_of_is_closed M) /-- A function between topological spaces is sequentially continuous if it commutes with limit of convergent sequences. -/ def sequentially_continuous (f : α → β) : Prop := ∀ (x : ℕ → α), ∀ {limit : α}, (x ⟶ limit) → (f∘x ⟶ f limit) /- A continuous function is sequentially continuous. -/ lemma continuous.to_sequentially_continuous {f : α → β} (_ : continuous f) : sequentially_continuous f := assume x limit (_ : x ⟶ limit), have tendsto f (nhds limit) (nhds (f limit)), from continuous.tendsto ‹continuous f› limit, show (f ∘ x) ⟶ (f limit), from tendsto.comp ‹(x ⟶ limit)› this /-- In a sequential space, continuity and sequential continuity coincide. -/ lemma continuous_iff_sequentially_continuous {f : α → β} [sequential_space α] : continuous f ↔ sequentially_continuous f := iff.intro (assume _, ‹continuous f›.to_sequentially_continuous) (assume : sequentially_continuous f, show continuous f, from suffices h : ∀ {A : set β}, is_closed A → is_seq_closed (f ⁻¹' A), from continuous_iff_is_closed.mpr (assume A _, is_seq_closed_iff_is_closed.mp $ h ‹is_closed A›), assume A (_ : is_closed A), is_seq_closed_of_def $ assume (x : ℕ → α) p (_ : ∀ n, f (x n) ∈ A) (_ : x ⟶ p), have (f ∘ x) ⟶ (f p), from ‹sequentially_continuous f› x ‹(x ⟶ p)›, show f p ∈ A, from mem_of_is_closed_sequential ‹is_closed A› ‹∀ n, f (x n) ∈ A› ‹(f∘x ⟶ f p)›) end topological_space /- Statements about sequences in metric spaces -/ namespace metric variable [metric_space α] variables {ε : ℝ} -- necessary for the next instance set_option eqn_compiler.zeta true /-- Show that every metric space is sequential. -/ instance : sequential_space α := ⟨show ∀ M, sequential_closure M = closure M, from assume M, suffices closure M ⊆ sequential_closure M, from set.subset.antisymm (sequential_closure_subset_closure M) this, assume (p : α) (_ : p ∈ closure M), -- we construct a sequence in α, with values in M, that converges to p -- the first step is to use (p ∈ closure M) ↔ "all nhds of p contain elements of M" on metric -- balls have ∀ n : ℕ, ball p ((1:ℝ)/((n+1):ℝ)) ∩ M ≠ ∅ := assume n : ℕ, mem_closure_iff.mp ‹p ∈ (closure M)› (ball p ((1:ℝ)/((n+1):ℝ))) (is_open_ball) (mem_ball_self $ one_div_pos_of_pos $ add_pos_of_nonneg_of_pos (nat.cast_nonneg n) zero_lt_one), -- from this, construct a "sequence of hypothesis" h, (h n) := _ ∈ {x // x ∈ ball (1/n+1) p ∩ M} let h := λ n : ℕ, (classical.indefinite_description _ (set.exists_mem_of_ne_empty (this n))), -- and the actual sequence x := λ n : ℕ, (h n).val in -- now we construct the promised sequence and show the claim show ∃ x : ℕ → α, (∀ n : ℕ, ((x n) ∈ M)) ∧ (x ⟶ p), from ⟨x, assume n, have (x n) ∈ ball p ((1:ℝ)/((n+1):ℝ)) ∩ M := (h n).property, this.2, suffices ∀ ε > 0, ∃ n0 : ℕ, ∀ n ≥ n0, dist (x n) p < ε, by simpa only [metric.tendsto_at_top], assume ε _, -- we apply that 1/n converges to zero to the fact that (x n) ∈ ball p ε have ∀ ε > 0, ∃ n0 : ℕ, ∀ n ≥ n0, dist (1 / (↑n + 1)) (0:ℝ) < ε := metric.tendsto_at_top.mp tendsto_one_div_add_at_top_nhds_0_nat, let ⟨n0, hn0⟩ := this ε ‹ε > 0› in show ∃ n0 : ℕ, ∀ n ≥ n0, dist (x n) p < ε, from ⟨n0, assume n ngtn0, calc dist (x n) p < (1:ℝ)/↑(n+1) : (h n).property.1 ... = abs ((1:ℝ)/↑(n+1)) : eq.symm $ abs_of_nonneg $ div_nonneg' zero_le_one $ nat.cast_nonneg _ ... = abs ((1:ℝ)/↑(n+1) - 0) : by simp ... = dist ((1:ℝ)/↑(n+1)) 0 : eq.symm $ real.dist_eq ((1:ℝ)/↑(n+1)) 0 ... < ε : hn0 n ‹n ≥ n0›⟩⟩⟩ set_option eqn_compiler.zeta false end metric
aa8c867adf495ee536a0cfba29134cfb96efcbc0
d7189ea2ef694124821b033e533f18905b5e87ef
/galois/map/fmap_func.lean
e1a4393400506f4d336a0e81f7844ca37e47df4c
[ "Apache-2.0" ]
permissive
digama0/lean-protocol-support
eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59
cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda
refs/heads/master
1,625,421,450,627
1,506,035,462,000
1,506,035,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,959
lean
/- An implementation of key-value maps using functions Includes a dependent map, allowing for mappings into multiple types, as well as a non-dependent map, with only a single value tyep -/ universes u v w namespace option structure has_value {V : Type u} (x : option V) := (value : V) (value_ok : x = some value) def check_has_value {V : Type u} (x : option V) : option (has_value x) := begin destruct x; intros, { exact none }, { apply some, constructor, assumption } end end option /-- The type of Dependent maps. Takes a total mapping from keys to type and represents a partial mapping from keys of type K to values of type defined by V -/ def mapd {K : Type u} (V : K → Type v) : Type (max u v) := ∀ k, option (V k) /-- A non-dependent map type with a single value type. -/ @[reducible] def map (K : Type u) (V : Type v) : Type (max u v) := mapd (λ _ : K, V) namespace mapd section /- We parameterize the key type and require decidable equality on it -/ parameter {K : Type u} parameter [decidable_eq K] parameter {V : K → Type v} /-- An empty map, all lookups return none-/ def empty : mapd V := fun _, none /-- Function to make finds explicit.-/ def find (k : K) (m : mapd V) : option (V k) := m k /-- All empty lookups are none -/ lemma empty_char (k : K) : find k empty = none := rfl /-- Put an item into the map, shadowing any previous definitions-/ def insert (k : K) (v : V k) (m : mapd V) : mapd V := λ k' : K, if H : k = k' then some (eq.rec_on H v) else find k' m /-- Relationship between find and insert -/ lemma insert_char (k : K) (v : V k) (m : mapd V) (k' : K) : find k' (insert k v m) = if H : k = k' then some (eq.rec_on H v) else find k' m := rfl -- perhaps this is lazy, it should fail if not already there? def update := insert /-- Remove a mapping -/ def remove (k : K) (m : mapd V) : mapd V := λ k' : K, if H : k = k' then none else m k' /-- Relationship between remove and find-/ lemma remove_char (k : K) (m : mapd V) (k' : K) : find k' (remove k m) = if H : k = k' then none else find k' m := rfl /-- Extensionality for maps-/ lemma eq_rel (m m' : mapd V) (H : ∀ k, find k m = find k m') : m = m' := funext H /-- Map over all values contained in the map-/ def fmapd {V' : K → Type w} (f : ∀ k : K, V k → V' k) (m : mapd V) : mapd V' := λ k, option.map (f k) (find k m) end /-- Interaction between fmapd and find -/ lemma fmapd_char {K : Type u} [decidable_eq K] {V : K → Type v} {V' : K → Type w} (f : ∀ k : K, V k → V' k) (m : mapd V) (k : K) : find k (fmapd f m) = option.map (f k) (find k m) := rfl /-- Map over non-dependent maps-/ def fmap {K : Type u} [decidable_eq K] {V : Type v} {V' : Type w} (f : V → V') (m : map K V) : map K V' := λ k, option.map f (find k m) /-- Interaction between fmap and find -/ lemma fmap_char {K : Type u} [decidable_eq K] {V : Type v} {V' : Type w} (f : V → V') (m : map K V) (k : K) : find k (fmap f m) = option.map f (find k m) := rfl attribute [irreducible] mapd attribute [irreducible] find attribute [irreducible] insert attribute [irreducible] remove section parameter {K : Type u} parameter [decidable_eq K] parameter {V : K → Type v} /-- Update the value already contained at key k by function f. Has no effect if there is no existing mapping for k -/ def updatef (k : K) (f : V k → V k) (m : mapd V) : mapd V := match find k m with | none := m | some v := insert k (f v) m end /-- Definition of functions over maps that can be reordered, assuming they are about distinct keys -/ def permutable (f : ∀ k : K, V k → mapd V → mapd V) := ∀ (m : mapd V) (k k' : K) (v : V k) (v' : V k'), k ≠ k' → f k v (f k' v' m) = f k' v' (f k v m) lemma neq_symm {A : Type u} {x y : A} (H : x ≠ y) : y ≠ x := begin intros contra, apply H, symmetry, assumption end /-- We can reorder inserts over distinct keys -/ lemma insert_permutable : permutable insert := begin unfold permutable, intros m k k' v v' Hne, apply eq_rel, intros z, repeat {rw insert_char}, apply (if Hkz : k = z then _ else _), { rw (dif_pos Hkz), subst z, rw (dif_neg (neq_symm Hne)), rw (dif_pos (eq.refl k)) }, { repeat {rw (dif_neg Hkz)}, } end /-- Relationship between find and insert with the same key -/ lemma find_insert_refl (m : mapd V) (k : K) (v : V k) : find k (insert k v m) = some v := begin rw insert_char, rw (dif_pos (eq.refl k)) end lemma find_insert_neq {m : mapd V} {k k' : K} (H : k ≠ k') (v : V k) : find k' (insert k v m) = find k' m := begin rw insert_char, rw (dif_neg H) end /-- A membership structure to allow lookups that come with a proof of membership -/ structure member (m : mapd V) := (key : K) (value : V key) (in_map : find key m = some value) inductive is_member (k : K) (m : mapd V) : Prop | mk : ∀ value : V k, find k m = some value → is_member def has_value_to_member {m : mapd V} {k : K} (x : (find k m).has_value) : member m := { key := k, value := x.value, in_map := x.value_ok } /-- Get the member, if there is a member at k -/ def check_member (m : mapd V) (k : K) : option (member m) := option.map has_value_to_member (find k m).check_has_value def check_member_same_key {m : mapd V} {k : K} {v : m.member} (H : check_member m k = some v) : v.key = k := begin dsimp [check_member] at H, destruct ((find k m).check_has_value); intros, { rw a at H, contradiction }, { rw a_1 at H, simp [option.map, option.bind, function.comp] at H, injection H with H', clear H, subst v, simp [has_value_to_member], } end lemma member_eq_value {m : mapd V} {k : K} {v v' : V k} (H : v = v') {in1 : find k m = some v} {in2 : find k m = some v'} : member.mk k v in1 = member.mk k v' in2 := begin induction H, trivial end lemma check_member_same {m : mapd V} {x x' : m.member} (H : check_member m (x.key) = some x') : x = x' := begin dsimp [check_member] at H, destruct ((find x.key m).check_has_value); intros, { rw a at H, contradiction }, { rw a_1 at H, simp [option.map, option.bind, function.comp] at H, injection H with H', clear H, subst x', simp [has_value_to_member], have H2 := a.value_ok, have H3 := x.in_map, rw H2 at H3, clear H2, injection H3 with H, clear H3, induction x, dsimp, dsimp at H, apply member_eq_value, symmetry, assumption } end def mfind (k : K) (m : mapd V) (mem : is_member k m) : V k := begin destruct (find k m); intros, { exfalso, induction mem, rw a at a_1, contradiction }, { assumption } end instance member_decidable (m : mapd V) : decidable_eq (member m) := begin unfold decidable_eq, unfold decidable_rel, intros k k', cases k with k v kv, cases k' with k' v' kv', apply (if H : k = k' then _ else _), { apply decidable.is_true, induction H, rw kv at kv', injection kv' with h, induction h, apply congr_arg, apply proof_irrel, }, { apply decidable.is_false, intros contra, injection contra, contradiction } end end namespace is_member def value {K : Type u} [decidable_eq K] {V : K → Type v} {k : K} {m : mapd V} (H : is_member k m) : V k := begin destruct (mapd.find k m); intros, { exfalso, destruct H; intros, rw a at a_1, contradiction }, { assumption } end lemma update {K : Type u} [decidable_eq K] {V : K → Type v} {k : K} {m : mapd V} (H : is_member k m) (k' : K) (v' : V k') : is_member k (update k' v' m) := begin induction H, unfold update, apply (if H : k' = k then _ else _), { induction H, existsi v', rw find_insert_refl }, { apply (is_member.mk value), rw insert_char, rw (dif_neg H), assumption } end end is_member def member_unfmap {K : Type u} [decidable_eq K] {V : K → Type v} {V' : K → Type w} {f : ∀ k : K, V k → V' k} (m : mapd V) (x : member (fmapd f m)) : member m := begin cases x, destruct (find key m); intros, { unfold fmapd at in_map, unfold find at in_map, unfold find at a, unfold option.map at in_map, rw a at in_map, simp [option.bind] at in_map, contradiction }, { constructor, assumption } end def member_fmap {K : Type u} [decidable_eq K] {V : K → Type v} {V' : K → Type w} (m : mapd V) (x : member m) (f : ∀ k : K, V k → V' k) : member (fmapd f m) := begin constructor, rw fmapd_char, rw x.in_map, simp [option.map], simp [option.bind], end def depv {K : Type u} [decidable_eq K] {V : Type v} (m : map K V) (B : V → Type w) := ∀ x : m.member, B x.value namespace depv section parameters {K : Type u} [decidable_eq K] {V : Type v} {B : V → Type w} def lookup_empty : depv (empty : map K V) B := begin intros x, cases x, rw empty_char at in_map, contradiction end inductive liftProp (P : Prop) : Type | mk : P → liftProp @[reducible] def insert_member_invert {m : map K V} {k : K} {v : V} (x : (insert k v m).member) : (liftProp (k = x.key)) ⊕ ((find x.key m).has_value × liftProp (¬ k = x.key)) := begin apply (if H : k = x.key then _ else _), { apply sum.inl, constructor, assumption }, { apply sum.inr, constructor, constructor, have H' := x.in_map, rw insert_char at H', rw (dif_neg H) at H', assumption, constructor, assumption } end def lookup_insert {m : map K V} (k : K) (v : V) (b : B v) (f : depv m B) : depv (insert k v m) B := begin intros x, have H := insert_member_invert x, induction H with H H, { induction H with H, induction x, dsimp, dsimp at H, induction H, rw mapd.insert_char at in_map, rw (dif_pos (eq.refl k)) at in_map, injection in_map with h, clear in_map, subst h, apply b, }, { induction H with H H1, induction H1 with H1, induction x, let H' := member.mk key H.value H.value_ok, dsimp, dsimp at H, dsimp at H1, rw (find_insert_neq H1) at in_map, rename in_map in_map', have H' := H.value_ok, rw H' at in_map', injection in_map' with H2, clear H1 H' in_map', subst value, apply (f H'), } end end end depv end mapd
0e0ac6994103eca09132f3236e51a2be14d41209
618003631150032a5676f229d13a079ac875ff77
/src/control/traversable/equiv.lean
014bf1000cf5a799299c33947485abafd5b257dc
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
4,799
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Transferring `traversable` instances using isomorphisms. -/ import data.equiv.basic import control.traversable.lemmas universes u namespace equiv section functor parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [functor t] open functor protected def map {α β : Type u} (f : α → β) (x : t' α) : t' β := eqv β $ map f ((eqv α).symm x) protected def functor : functor t' := { map := @equiv.map _ } variables [is_lawful_functor t] protected lemma id_map {α : Type u} (x : t' α) : equiv.map id x = x := by simp [equiv.map, id_map] protected lemma comp_map {α β γ : Type u} (g : α → β) (h : β → γ) (x : t' α) : equiv.map (h ∘ g) x = equiv.map h (equiv.map g x) := by simp [equiv.map]; apply comp_map protected lemma is_lawful_functor : @is_lawful_functor _ equiv.functor := { id_map := @equiv.id_map _ _, comp_map := @equiv.comp_map _ _ } protected lemma is_lawful_functor' [F : _root_.functor t'] (h₀ : ∀ {α β} (f : α → β), _root_.functor.map f = equiv.map f) (h₁ : ∀ {α β} (f : β), _root_.functor.map_const f = (equiv.map ∘ function.const α) f) : _root_.is_lawful_functor t' := begin have : F = equiv.functor, { unfreezeI, cases F, dsimp [equiv.functor], congr; ext; [rw ← h₀, rw ← h₁] }, constructor; intros; haveI F' := equiv.is_lawful_functor, { simp, intros, ext, rw [h₁], rw ← this at F', have k := @map_const_eq t' _ _ α β, rw this at ⊢ k, rw ← k, refl }, { rw [h₀], rw ← this at F', have k := id_map x, rw this at k, apply k }, { rw [h₀], rw ← this at F', have k := comp_map g h x, revert k, rw this, exact id }, end end functor section traversable parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [traversable t] variables {m : Type u → Type u} [applicative m] variables {α β : Type u} protected def traverse (f : α → m β) (x : t' α) : m (t' β) := eqv β <$> traverse f ((eqv α).symm x) protected def traversable : traversable t' := { to_functor := equiv.functor eqv, traverse := @equiv.traverse _ } end traversable section equiv parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [traversable t] [is_lawful_traversable t] variables {F G : Type u → Type u} [applicative F] [applicative G] variables [is_lawful_applicative F] [is_lawful_applicative G] variables (η : applicative_transformation F G) variables {α β γ : Type u} open is_lawful_traversable functor protected lemma id_traverse (x : t' α) : equiv.traverse eqv id.mk x = x := by simp! [equiv.traverse,id_bind,id_traverse,functor.map] with functor_norm protected lemma traverse_eq_map_id (f : α → β) (x : t' α) : equiv.traverse eqv (id.mk ∘ f) x = id.mk (equiv.map eqv f x) := by simp [equiv.traverse, traverse_eq_map_id] with functor_norm; refl protected lemma comp_traverse (f : β → F γ) (g : α → G β) (x : t' α) : equiv.traverse eqv (comp.mk ∘ functor.map f ∘ g) x = comp.mk (equiv.traverse eqv f <$> equiv.traverse eqv g x) := by simp [equiv.traverse,comp_traverse] with functor_norm; congr; ext; simp protected lemma naturality (f : α → F β) (x : t' α) : η (equiv.traverse eqv f x) = equiv.traverse eqv (@η _ ∘ f) x := by simp only [equiv.traverse] with functor_norm protected def is_lawful_traversable : @is_lawful_traversable t' (equiv.traversable eqv) := { to_is_lawful_functor := @equiv.is_lawful_functor _ _ eqv _ _, id_traverse := @equiv.id_traverse _ _, comp_traverse := @equiv.comp_traverse _ _, traverse_eq_map_id := @equiv.traverse_eq_map_id _ _, naturality := @equiv.naturality _ _ } protected def is_lawful_traversable' [_i : traversable t'] (h₀ : ∀ {α β} (f : α → β), map f = equiv.map eqv f) (h₁ : ∀ {α β} (f : β), map_const f = (equiv.map eqv ∘ function.const α) f) (h₂ : ∀ {F : Type u → Type u} [applicative F] [is_lawful_applicative F] {α β} (f : α → F β), traverse f = equiv.traverse eqv f) : _root_.is_lawful_traversable t' := begin -- we can't use the same approach as for `is_lawful_functor'` because -- h₂ needs a `is_lawful_applicative` assumption refine {to_is_lawful_functor := equiv.is_lawful_functor' eqv @h₀ @h₁, ..}; intros; resetI, { rw [h₂, equiv.id_traverse], apply_instance }, { rw [h₂, equiv.comp_traverse f g x, h₂], congr, rw [h₂], all_goals { apply_instance } }, { rw [h₂, equiv.traverse_eq_map_id, h₀]; apply_instance }, { rw [h₂, equiv.naturality, h₂]; apply_instance } end end equiv end equiv
69c9ef7272db994a305f31614b021c2b9481768d
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/stage0/src/Lean/Meta/SynthInstance.lean
30f5ac7ff1b5b520596b55d781017ca2ab664007
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,605
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam, Leonardo de Moura Type class instance synthesizer using tabled resolution. -/ import Lean.Meta.Basic import Lean.Meta.Instances import Lean.Meta.LevelDefEq import Lean.Meta.AbstractMVars import Lean.Meta.WHNF import Lean.Util.Profile namespace Lean.Meta namespace SynthInstance open Std (HashMap) builtin_initialize inferTCGoalsRLAttr : TagAttribute ← registerTagAttribute `inferTCGoalsRL "instruct type class resolution procedure to solve goals from right to left for this instance" def hasInferTCGoalsRLAttribute (env : Environment) (constName : Name) : Bool := inferTCGoalsRLAttr.hasTag env constName structure GeneratorNode where mvar : Expr key : Expr mctx : MetavarContext instances : Array Expr currInstanceIdx : Nat deriving Inhabited structure ConsumerNode where mvar : Expr key : Expr mctx : MetavarContext subgoals : List Expr size : Nat -- instance size so far deriving Inhabited inductive Waiter where | consumerNode : ConsumerNode → Waiter | root : Waiter def Waiter.isRoot : Waiter → Bool | Waiter.consumerNode _ => false | Waiter.root => true /- In tabled resolution, we creating a mapping from goals (e.g., `Coe Nat ?x`) to answers and waiters. Waiters are consumer nodes that are waiting for answers for a particular node. We implement this mapping using a `HashMap` where the keys are normalized expressions. That is, we replace assignable metavariables with auxiliary free variables of the form `_tc.<idx>`. We do not declare these free variables in any local context, and we should view them as "normalized names" for metavariables. For example, the term `f ?m ?m ?n` is normalized as `f _tc.0 _tc.0 _tc.1`. This approach is structural, and we may visit the same goal more than once if the different occurrences are just definitionally equal, but not structurally equal. Remark: a metavariable is assignable only if its depth is equal to the metavar context depth. -/ namespace MkTableKey structure State where nextIdx : Nat := 0 lmap : HashMap MVarId Level := {} emap : HashMap MVarId Expr := {} abbrev M := ReaderT MetavarContext (StateM State) partial def normLevel (u : Level) : M Level := do if !u.hasMVar then pure u else match u with | Level.succ v _ => return u.updateSucc! (← normLevel v) | Level.max v w _ => return u.updateMax! (← normLevel v) (← normLevel w) | Level.imax v w _ => return u.updateIMax! (← normLevel v) (← normLevel w) | Level.mvar mvarId _ => let mctx ← read if !mctx.isLevelAssignable mvarId then pure u else let s ← get match s.lmap.find? mvarId with | some u' => pure u' | none => let u' := mkLevelParam $ Name.mkNum `_tc s.nextIdx modify fun s => { s with nextIdx := s.nextIdx + 1, lmap := s.lmap.insert mvarId u' } pure u' | u => pure u partial def normExpr (e : Expr) : M Expr := do if !e.hasMVar then pure e else match e with | Expr.const _ us _ => return e.updateConst! (← us.mapM normLevel) | Expr.sort u _ => return e.updateSort! (← normLevel u) | Expr.app f a _ => return e.updateApp! (← normExpr f) (← normExpr a) | Expr.letE _ t v b _ => return e.updateLet! (← normExpr t) (← normExpr v) (← normExpr b) | Expr.forallE _ d b _ => return e.updateForallE! (← normExpr d) (← normExpr b) | Expr.lam _ d b _ => return e.updateLambdaE! (← normExpr d) (← normExpr b) | Expr.mdata _ b _ => return e.updateMData! (← normExpr b) | Expr.proj _ _ b _ => return e.updateProj! (← normExpr b) | Expr.mvar mvarId _ => let mctx ← read if !mctx.isExprAssignable mvarId then pure e else let s ← get match s.emap.find? mvarId with | some e' => pure e' | none => do let e' := mkFVar $ Name.mkNum `_tc s.nextIdx modify fun s => { s with nextIdx := s.nextIdx + 1, emap := s.emap.insert mvarId e' } pure e' | _ => pure e end MkTableKey /- Remark: `mkTableKey` assumes `e` does not contain assigned metavariables. -/ def mkTableKey (mctx : MetavarContext) (e : Expr) : Expr := MkTableKey.normExpr e mctx |>.run' {} structure Answer where result : AbstractMVarsResult resultType : Expr size : Nat instance : Inhabited Answer where default := { result := arbitrary, resultType := arbitrary, size := 0 } structure TableEntry where waiters : Array Waiter answers : Array Answer := #[] structure Context where maxResultSize : Nat /- Remark: the SynthInstance.State is not really an extension of `Meta.State`. The field `postponed` is not needed, and the field `mctx` is misleading since `synthInstance` methods operate over different `MetavarContext`s simultaneously. That being said, we still use `extends` because it makes it simpler to move from `M` to `MetaM`. -/ structure State where result : Option Expr := none generatorStack : Array GeneratorNode := #[] resumeStack : Array (ConsumerNode × Answer) := #[] tableEntries : HashMap Expr TableEntry := {} abbrev SynthM := ReaderT Context $ StateRefT State MetaM @[inline] def mapMetaM (f : forall {α}, MetaM α → MetaM α) {α} : SynthM α → SynthM α := monadMap @f instance {α} : Inhabited (SynthM α) where default := fun _ _ => arbitrary /-- Return globals and locals instances that may unify with `type` -/ def getInstances (type : Expr) : MetaM (Array Expr) := do -- We must retrieve `localInstances` before we use `forallTelescopeReducing` because it will update the set of local instances let localInstances ← getLocalInstances forallTelescopeReducing type fun _ type => do let className? ← isClass? type match className? with | none => throwError $ "type class instance expected" ++ indentExpr type | some className => let globalInstances ← getGlobalInstancesIndex let result ← globalInstances.getUnify type -- Using insertion sort because it is stable and the array `result` should be mostly sorted. -- Most instances have default priority. let result := result.insertionSort fun e₁ e₂ => e₁.priority < e₂.priority let result ← result.mapM fun e => match e.val with | Expr.const constName us _ => return e.val.updateConst! (← us.mapM (fun _ => mkFreshLevelMVar)) | _ => panic! "global instance is not a constant" trace[Meta.synthInstance.globalInstances]! "{type}, {result}" let result := localInstances.foldl (init := result) fun (result : Array Expr) linst => if linst.className == className then result.push linst.fvar else result pure result def mkGeneratorNode? (key mvar : Expr) : MetaM (Option GeneratorNode) := do let mvarType ← inferType mvar let mvarType ← instantiateMVars mvarType let instances ← getInstances mvarType if instances.isEmpty then pure none else let mctx ← getMCtx pure $ some { mvar := mvar, key := key, mctx := mctx, instances := instances, currInstanceIdx := instances.size } /-- Create a new generator node for `mvar` and add `waiter` as its waiter. `key` must be `mkTableKey mctx mvarType`. -/ def newSubgoal (mctx : MetavarContext) (key : Expr) (mvar : Expr) (waiter : Waiter) : SynthM Unit := withMCtx mctx do trace[Meta.synthInstance.newSubgoal]! key match (← mkGeneratorNode? key mvar) with | none => pure () | some node => let entry : TableEntry := { waiters := #[waiter] } modify fun s => { s with generatorStack := s.generatorStack.push node, tableEntries := s.tableEntries.insert key entry } def findEntry? (key : Expr) : SynthM (Option TableEntry) := do return (← get).tableEntries.find? key def getEntry (key : Expr) : SynthM TableEntry := do match (← findEntry? key) with | none => panic! "invalid key at synthInstance" | some entry => pure entry /-- Create a `key` for the goal associated with the given metavariable. That is, we create a key for the type of the metavariable. We must instantiate assigned metavariables before we invoke `mkTableKey`. -/ def mkTableKeyFor (mctx : MetavarContext) (mvar : Expr) : SynthM Expr := withMCtx mctx do let mvarType ← inferType mvar let mvarType ← instantiateMVars mvarType return mkTableKey mctx mvarType /- See `getSubgoals` and `getSubgoalsAux` We use the parameter `j` to reduce the number of `instantiate*` invocations. It is the same approach we use at `forallTelescope` and `lambdaTelescope`. Given `getSubgoalsAux args j subgoals instVal type`, we have that `type.instantiateRevRange j args.size args` does not have loose bound variables. -/ structure SubgoalsResult where subgoals : List Expr instVal : Expr instTypeBody : Expr private partial def getSubgoalsAux (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) : Array Expr → Nat → List Expr → Expr → Expr → MetaM SubgoalsResult | args, j, subgoals, instVal, Expr.forallE n d b c => do let d := d.instantiateRevRange j args.size args let mvarType ← mkForallFVars xs d let mvar ← mkFreshExprMVarAt lctx localInsts mvarType let arg := mkAppN mvar xs let instVal := mkApp instVal arg let subgoals := if c.binderInfo.isInstImplicit then mvar::subgoals else subgoals let args := args.push (mkAppN mvar xs) getSubgoalsAux lctx localInsts xs args j subgoals instVal b | args, j, subgoals, instVal, type => do let type := type.instantiateRevRange j args.size args let type ← whnf type if type.isForall then getSubgoalsAux lctx localInsts xs args args.size subgoals instVal type else pure ⟨subgoals, instVal, type⟩ /-- `getSubgoals lctx localInsts xs inst` creates the subgoals for the instance `inst`. The subgoals are in the context of the free variables `xs`, and `(lctx, localInsts)` is the local context and instances before we added the free variables to it. This extra complication is required because 1- We want all metavariables created by `synthInstance` to share the same local context. 2- We want to ensure that applications such as `mvar xs` are higher order patterns. The method `getGoals` create a new metavariable for each parameter of `inst`. For example, suppose the type of `inst` is `forall (x_1 : A_1) ... (x_n : A_n), B x_1 ... x_n`. Then, we create the metavariables `?m_i : forall xs, A_i`, and return the subset of these metavariables that are instance implicit arguments, and the expressions: - `inst (?m_1 xs) ... (?m_n xs)` (aka `instVal`) - `B (?m_1 xs) ... (?m_n xs)` -/ def getSubgoals (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) (inst : Expr) : MetaM SubgoalsResult := do let instType ← inferType inst let result ← getSubgoalsAux lctx localInsts xs #[] 0 [] inst instType match inst.getAppFn with | Expr.const constName _ _ => let env ← getEnv if hasInferTCGoalsRLAttribute env constName then pure result else pure { result with subgoals := result.subgoals.reverse } | _ => pure result def tryResolveCore (mvar : Expr) (inst : Expr) : MetaM (Option (MetavarContext × List Expr)) := do let mvarType ← inferType mvar let lctx ← getLCtx let localInsts ← getLocalInstances forallTelescopeReducing mvarType fun xs mvarTypeBody => do let ⟨subgoals, instVal, instTypeBody⟩ ← getSubgoals lctx localInsts xs inst trace[Meta.synthInstance.tryResolve]! "{mvarTypeBody} =?= {instTypeBody}" if (← isDefEq mvarTypeBody instTypeBody) then let instVal ← mkLambdaFVars xs instVal if (← isDefEq mvar instVal) then trace[Meta.synthInstance.tryResolve]! "success" pure (some ((← getMCtx), subgoals)) else trace[Meta.synthInstance.tryResolve]! "failure assigning" pure none else trace[Meta.synthInstance.tryResolve]! "failure" pure none /-- Try to synthesize metavariable `mvar` using the instance `inst`. Remark: `mctx` contains `mvar`. If it succeeds, the result is a new updated metavariable context and a new list of subgoals. A subgoal is created for each instance implicit parameter of `inst`. -/ def tryResolve (mctx : MetavarContext) (mvar : Expr) (inst : Expr) : SynthM (Option (MetavarContext × List Expr)) := traceCtx `Meta.synthInstance.tryResolve <| withMCtx mctx <| tryResolveCore mvar inst /-- Assign a precomputed answer to `mvar`. If it succeeds, the result is a new updated metavariable context and a new list of subgoals. -/ def tryAnswer (mctx : MetavarContext) (mvar : Expr) (answer : Answer) : SynthM (Option MetavarContext) := withMCtx mctx do let (_, _, val) ← openAbstractMVarsResult answer.result if (← isDefEq mvar val) then pure (some (← getMCtx)) else pure none /-- Move waiters that are waiting for the given answer to the resume stack. -/ def wakeUp (answer : Answer) : Waiter → SynthM Unit | Waiter.root => do if answer.result.paramNames.isEmpty && answer.result.numMVars == 0 then modify fun s => { s with result := answer.result.expr } else let (_, _, answerExpr) ← openAbstractMVarsResult answer.result trace[Meta.synthInstance]! "skip answer containing metavariables {answerExpr}" pure () | Waiter.consumerNode cNode => modify fun s => { s with resumeStack := s.resumeStack.push (cNode, answer) } def isNewAnswer (oldAnswers : Array Answer) (answer : Answer) : Bool := oldAnswers.all fun oldAnswer => do -- Remark: isDefEq here is too expensive. TODO: if `==` is too imprecise, add some light normalization to `resultType` at `addAnswer` -- iseq ← isDefEq oldAnswer.resultType answer.resultType; pure (!iseq) oldAnswer.resultType != answer.resultType private def mkAnswer (cNode : ConsumerNode) : MetaM Answer := withMCtx cNode.mctx do traceM `Meta.synthInstance.newAnswer do m!"size: {cNode.size}, {← inferType cNode.mvar}" let val ← instantiateMVars cNode.mvar let result ← abstractMVars val -- assignable metavariables become parameters let resultType ← inferType result.expr pure { result := result, resultType := resultType, size := cNode.size + 1 } /-- Create a new answer after `cNode` resolved all subgoals. That is, `cNode.subgoals == []`. And then, store it in the tabled entries map, and wakeup waiters. -/ def addAnswer (cNode : ConsumerNode) : SynthM Unit := do if cNode.size ≥ (← read).maxResultSize then traceM `Meta.synthInstance.discarded do m!"size: {cNode.size} ≥ {(← read).maxResultSize}, {← inferType cNode.mvar}" return () else let answer ← mkAnswer cNode -- Remark: `answer` does not contain assignable or assigned metavariables. let key := cNode.key let entry ← getEntry key if isNewAnswer entry.answers answer then let newEntry := { entry with answers := entry.answers.push answer } modify fun s => { s with tableEntries := s.tableEntries.insert key newEntry } entry.waiters.forM (wakeUp answer) /-- Process the next subgoal in the given consumer node. -/ def consume (cNode : ConsumerNode) : SynthM Unit := match cNode.subgoals with | [] => addAnswer cNode | mvar::_ => do let waiter := Waiter.consumerNode cNode let key ← mkTableKeyFor cNode.mctx mvar let entry? ← findEntry? key match entry? with | none => newSubgoal cNode.mctx key mvar waiter | some entry => modify fun s => { s with resumeStack := entry.answers.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack, tableEntries := s.tableEntries.insert key { entry with waiters := entry.waiters.push waiter } } def getTop : SynthM GeneratorNode := do pure (← get).generatorStack.back @[inline] def modifyTop (f : GeneratorNode → GeneratorNode) : SynthM Unit := modify fun s => { s with generatorStack := s.generatorStack.modify (s.generatorStack.size - 1) f } /-- Try the next instance in the node on the top of the generator stack. -/ def generate : SynthM Unit := do let gNode ← getTop if gNode.currInstanceIdx == 0 then modify fun s => { s with generatorStack := s.generatorStack.pop } else do let key := gNode.key let idx := gNode.currInstanceIdx - 1 let inst := gNode.instances.get! idx let mctx := gNode.mctx let mvar := gNode.mvar trace[Meta.synthInstance.generate]! "instance {inst}" modifyTop fun gNode => { gNode with currInstanceIdx := idx } match (← tryResolve mctx mvar inst) with | none => pure () | some (mctx, subgoals) => consume { key := key, mvar := mvar, subgoals := subgoals, mctx := mctx, size := 0 } def getNextToResume : SynthM (ConsumerNode × Answer) := do let s ← get let r := s.resumeStack.back modify fun s => { s with resumeStack := s.resumeStack.pop } pure r /-- Given `(cNode, answer)` on the top of the resume stack, continue execution by using `answer` to solve the next subgoal. -/ def resume : SynthM Unit := do let (cNode, answer) ← getNextToResume match cNode.subgoals with | [] => panic! "resume found no remaining subgoals" | mvar::rest => match (← tryAnswer cNode.mctx mvar answer) with | none => pure () | some mctx => withMCtx mctx <| traceM `Meta.synthInstance.resume do let goal ← inferType cNode.mvar let subgoal ← inferType mvar pure m!"size: {cNode.size + answer.size}, {goal} <== {subgoal}" consume { key := cNode.key, mvar := cNode.mvar, subgoals := rest, mctx := mctx, size := cNode.size + answer.size } def step : SynthM Bool := do let s ← get if !s.resumeStack.isEmpty then resume pure true else if !s.generatorStack.isEmpty then generate pure true else pure false def getResult : SynthM (Option Expr) := do pure (← get).result partial def synth : SynthM (Option Expr) := do if (← step) then match (← getResult) with | none => synth | some result => pure result else trace[Meta.synthInstance]! "failed" pure none def main (type : Expr) (maxResultSize : Nat) : MetaM (Option Expr) := traceCtx `Meta.synthInstance do trace[Meta.synthInstance]! "main goal {type}" let mvar ← mkFreshExprMVar type let mctx ← getMCtx let key := mkTableKey mctx type let action : SynthM (Option Expr) := do newSubgoal mctx key mvar Waiter.root synth action.run { maxResultSize := maxResultSize } |>.run' {} end SynthInstance /- Type class parameters can be annotated with `outParam` annotations. Given `C a_1 ... a_n`, we replace `a_i` with a fresh metavariable `?m_i` IF `a_i` is an `outParam`. The result is type correct because we reject type class declarations IF it contains a regular parameter X that depends on an `out` parameter Y. Then, we execute type class resolution as usual. If it succeeds, and metavariables ?m_i have been assigned, we try to unify the original type `C a_1 ... a_n` witht the normalized one. -/ private def preprocess (type : Expr) : MetaM Expr := forallTelescopeReducing type fun xs type => do let type ← whnf type mkForallFVars xs type private def preprocessLevels (us : List Level) : MetaM (List Level) := do let mut r := [] for u in us do let u ← instantiateLevelMVars u if u.hasMVar then r := (← mkFreshLevelMVar)::r else r := u::r pure r.reverse private partial def preprocessArgs (type : Expr) (i : Nat) (args : Array Expr) : MetaM (Array Expr) := do if h : i < args.size then let type ← whnf type match type with | Expr.forallE _ d b _ => do let arg := args.get ⟨i, h⟩ let arg ← if isOutParam d then mkFreshExprMVar d else pure arg let args := args.set ⟨i, h⟩ arg preprocessArgs (b.instantiate1 arg) (i+1) args | _ => throwError "type class resolution failed, insufficient number of arguments" -- TODO improve error message else pure args private def preprocessOutParam (type : Expr) : MetaM Expr := forallTelescope type fun xs typeBody => do match typeBody.getAppFn with | c@(Expr.const constName us _) => let env ← getEnv if !hasOutParams env constName then pure type else do let args := typeBody.getAppArgs let us ← preprocessLevels us let c := mkConst constName us let cType ← inferType c let args ← preprocessArgs cType 0 args mkForallFVars xs (mkAppN c args) | _ => pure type def maxResultSizeDefault := 128 builtin_initialize registerOption `maxInstSize { defValue := maxResultSizeDefault, group := "", descr := "maximum number of instances used to construct a solution in the type class instance synthesis procedure" } private def getMaxSize (opts : Options) : Nat := opts.getNat `maxInstSize maxResultSizeDefault /- Remark: when `maxResultSize? == none`, the configuration option `synthInstance.maxResultSize` is used. Remark: we use a different option for controlling the maximum result size for coercions. -/ def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (Option Expr) := do profileitM Exception "typeclass inference" (← getOptions) ⟨0, 0⟩ do let opts ← getOptions let maxResultSize := maxResultSize?.getD (getMaxSize opts) let inputConfig ← getConfig withConfig (fun config => { config with isDefEqStuckEx := true, transparency := TransparencyMode.reducible, foApprox := true, ctxApprox := true, constApprox := false }) do let type ← instantiateMVars type let type ← preprocess type let s ← get match s.cache.synthInstance.find? type with | some result => pure result | none => let result? ← withNewMCtxDepth do let normType ← preprocessOutParam type trace[Meta.synthInstance]! "{type} ==> {normType}" match (← SynthInstance.main normType maxResultSize) with | none => pure none | some result => trace[Meta.synthInstance]! "FOUND result {result}" let result ← instantiateMVars result if (← hasAssignableMVar result) then trace[Meta.synthInstance]! "Failed has assignable mvar {result.setOption `pp.all true}" pure none else pure (some result) let result? ← match result? with | none => pure none | some result => do trace[Meta.synthInstance]! "result {result}" let resultType ← inferType result if (← withConfig (fun _ => inputConfig) <| isDefEq type resultType) then let result ← instantiateMVars result pure (some result) else trace[Meta.synthInstance]! "result type{indentExpr resultType}\nis not definitionally equal to{indentExpr type}" pure none if type.hasMVar then pure result? else do modify fun s => { s with cache := { s.cache with synthInstance := s.cache.synthInstance.insert type result? } } pure result? /-- Return `LOption.some r` if succeeded, `LOption.none` if it failed, and `LOption.undef` if instance cannot be synthesized right now because `type` contains metavariables. -/ def trySynthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (LOption Expr) := do catchInternalId isDefEqStuckExceptionId (toLOptionM <| synthInstance? type maxResultSize?) (fun _ => pure LOption.undef) def synthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM Expr := catchInternalId isDefEqStuckExceptionId (do let result? ← synthInstance? type maxResultSize? match result? with | some result => pure result | none => throwError! "failed to synthesize{indentExpr type}") (fun _ => throwError! "failed to synthesize{indentExpr type}") private def synthPendingImp (mvarId : MVarId) (maxResultSize? : Option Nat) : MetaM Bool := do let mvarDecl ← getMVarDecl mvarId match mvarDecl.kind with | MetavarKind.synthetic => match (← isClass? mvarDecl.type) with | none => pure false | some _ => do let val? ← catchInternalId isDefEqStuckExceptionId (synthInstance? mvarDecl.type maxResultSize?) (fun _ => pure none) match val? with | none => pure false | some val => if (← isExprMVarAssigned mvarId) then pure false else assignExprMVar mvarId val pure true | _ => pure false builtin_initialize synthPendingRef.set (synthPendingImp · none) builtin_initialize registerTraceClass `Meta.synthInstance registerTraceClass `Meta.synthInstance.globalInstances registerTraceClass `Meta.synthInstance.newSubgoal registerTraceClass `Meta.synthInstance.tryResolve registerTraceClass `Meta.synthInstance.resume registerTraceClass `Meta.synthInstance.generate end Lean.Meta
f63fca202e988e15675707bdeadf446e9f920e76
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/shift.lean
c61713108faab18d2d52aac4b9f96d65e030a4af
[ "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
18,215
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin, Andrew Yang -/ import category_theory.limits.preserves.shapes.zero import category_theory.monoidal.End import category_theory.monoidal.discrete /-! # Shift A `shift` on a category `C` indexed by a monoid `A` is nothing more than a monoidal functor from `A` to `C ⥤ C`. A typical example to keep in mind might be the category of complexes `⋯ → C_{n-1} → C_n → C_{n+1} → ⋯`. It has a shift indexed by `ℤ`, where we assign to each `n : ℤ` the functor `C ⥤ C` that re-indexes the terms, so the degree `i` term of `shift n C` would be the degree `i+n`-th term of `C`. ## Main definitions * `has_shift`: A typeclass asserting the existence of a shift functor. * `shift_equiv`: When the indexing monoid is a group, then the functor indexed by `n` and `-n` forms an self-equivalence of `C`. * `shift_comm`: When the indexing monoid is commutative, then shifts commute as well. ## Implementation Notes Many of the definitions in this file are marked as an `abbreviation` so that the simp lemmas in `category_theory/monoidal/End` can apply. -/ namespace category_theory noncomputable theory universes v u variables (C : Type u) (A : Type*) [category.{v} C] local attribute [instance] endofunctor_monoidal_category section eq_to_hom variables {A C} variables [add_monoid A] (F : monoidal_functor (discrete A) (C ⥤ C)) @[simp, reassoc] lemma eq_to_hom_μ_app {i j i' j' : A} (h₁ : i = i') (h₂ : j = j') (X : C) : eq_to_hom (by rw [h₁, h₂] : (F.obj ⟨i⟩ ⊗ F.obj ⟨j⟩).obj X = (F.obj ⟨i'⟩ ⊗ F.obj ⟨j'⟩).obj X) ≫ (F.μ ⟨i'⟩ ⟨j'⟩).app X = (F.μ ⟨i⟩ ⟨j⟩).app X ≫ eq_to_hom (by rw [h₁, h₂]) := by { cases h₁, cases h₂, rw [eq_to_hom_refl, eq_to_hom_refl, category.id_comp, category.comp_id] } @[simp, reassoc] lemma μ_inv_app_eq_to_hom {i j i' j' : A} (h₁ : i = i') (h₂ : j = j') (X : C) : inv ((F.μ ⟨i⟩ ⟨j⟩).app X) ≫ eq_to_hom (by rw [h₁, h₂]) = eq_to_hom (by rw [h₁, h₂]) ≫ inv ((F.μ ⟨i'⟩ ⟨j'⟩).app X) := by { cases h₁, cases h₂, rw [eq_to_hom_refl, eq_to_hom_refl, category.id_comp, category.comp_id] } end eq_to_hom variables {A C} /-- A monoidal functor from a group `A` into `C ⥤ C` induces a self-equivalence of `C` for each `n : A`. -/ @[simps functor inverse unit_iso_hom unit_iso_inv counit_iso_hom counit_iso_inv] def add_neg_equiv [add_group A] (F : monoidal_functor (discrete A) (C ⥤ C)) (n : A) : C ≌ C := equiv_of_tensor_iso_unit F ⟨n⟩ ⟨(-n : A)⟩ (discrete.eq_to_iso (add_neg_self n)) (discrete.eq_to_iso (neg_add_self n)) (subsingleton.elim _ _) section defs variables (A C) [add_monoid A] /-- A category has a shift indexed by an additive monoid `A` if there is a monoidal functor from `A` to `C ⥤ C`. -/ class has_shift (C : Type u) (A : Type*) [category.{v} C] [add_monoid A] := (shift : monoidal_functor (discrete A) (C ⥤ C)) /-- A helper structure to construct the shift functor `(discrete A) ⥤ (C ⥤ C)`. -/ @[nolint has_inhabited_instance] structure shift_mk_core := (F : A → (C ⥤ C)) (ε : 𝟭 C ≅ F 0) (μ : Π n m : A, F n ⋙ F m ≅ F (n + m)) (associativity : ∀ (m₁ m₂ m₃ : A) (X : C), (F m₃).map ((μ m₁ m₂).hom.app X) ≫ (μ (m₁ + m₂) m₃).hom.app X ≫ eq_to_hom (by { congr' 2, exact add_assoc _ _ _ }) = (μ m₂ m₃).hom.app ((F m₁).obj X) ≫ (μ m₁ (m₂ + m₃)).hom.app X . obviously) (left_unitality : ∀ (n : A) (X : C), (F n).map (ε.hom.app X) ≫ (μ 0 n).hom.app X = eq_to_hom (by { dsimp, rw zero_add }) . obviously) (right_unitality : ∀ (n : A) (X : C), ε.hom.app ((F n).obj X) ≫ (μ n 0).hom.app X = eq_to_hom (by { dsimp, rw add_zero }) . obviously) section local attribute [simp] eq_to_hom_map local attribute [reducible] endofunctor_monoidal_category discrete.add_monoidal /-- Constructs a `has_shift C A` instance from `shift_mk_core`. -/ @[simps] def has_shift_mk (h : shift_mk_core C A) : has_shift C A := ⟨{ ε := h.ε.hom, μ := λ m n, (h.μ m.as n.as).hom, μ_natural' := by { rintros ⟨X⟩ ⟨Y⟩ ⟨X'⟩ ⟨Y'⟩ ⟨⟨⟨rfl⟩⟩⟩ ⟨⟨⟨rfl⟩⟩⟩, ext, dsimp, simp, dsimp, simp }, associativity' := by { introv, ext, dsimp, simpa using h.associativity _ _ _ _, }, left_unitality' := by { rintro ⟨X⟩, ext, dsimp, rw [category.id_comp, ← category.assoc, h.left_unitality], simp }, right_unitality' := by { rintro ⟨X⟩, ext, dsimp, rw [functor.map_id, category.comp_id, ← category.assoc, h.right_unitality], simp }, ..(discrete.functor h.F) }⟩ end variables [has_shift C A] /-- The monoidal functor from `A` to `C ⥤ C` given a `has_shift` instance. -/ def shift_monoidal_functor : monoidal_functor (discrete A) (C ⥤ C) := has_shift.shift variable {A} /-- The shift autoequivalence, moving objects and morphisms 'up'. -/ abbreviation shift_functor (i : A) : C ⥤ C := (shift_monoidal_functor C A).obj ⟨i⟩ /-- Shifting by `i + j` is the same as shifting by `i` and then shifting by `j`. -/ abbreviation shift_functor_add (i j : A) : shift_functor C (i + j) ≅ shift_functor C i ⋙ shift_functor C j := ((shift_monoidal_functor C A).μ_iso ⟨i⟩ ⟨j⟩).symm variables (A) /-- Shifting by zero is the identity functor. -/ abbreviation shift_functor_zero : shift_functor C (0 : A) ≅ 𝟭 C := (shift_monoidal_functor C A).ε_iso.symm -- Any better notational suggestions? notation X`⟦`n`⟧`:20 := (shift_functor _ n).obj X notation f`⟦`n`⟧'`:80 := (shift_functor _ n).map f end defs section add_monoid variables {C A} [add_monoid A] [has_shift C A] (X Y : C) (f : X ⟶ Y) @[simp] lemma has_shift.shift_obj_obj (n : A) (X : C) : (has_shift.shift.obj ⟨n⟩).obj X = X⟦n⟧ := rfl /-- Shifting by `i + j` is the same as shifting by `i` and then shifting by `j`. -/ abbreviation shift_add (i j : A) : X⟦i + j⟧ ≅ X⟦i⟧⟦j⟧ := (shift_functor_add C i j).app _ @[reassoc] lemma shift_add_hom_comp_eq_to_hom₁ (i i' j : A) (h : i = i') : (shift_add X i j).hom ≫ eq_to_hom (by rw h) = eq_to_hom (by rw h) ≫ (shift_add X i' j).hom := by { cases h, rw [eq_to_hom_refl, eq_to_hom_refl, category.id_comp, category.comp_id] } @[reassoc] lemma shift_add_hom_comp_eq_to_hom₂ (i j j' : A) (h : j = j') : (shift_add X i j).hom ≫ eq_to_hom (by rw h) = eq_to_hom (by rw h) ≫ (shift_add X i j').hom := by { cases h, rw [eq_to_hom_refl, eq_to_hom_refl, category.id_comp, category.comp_id] } @[reassoc] lemma shift_add_hom_comp_eq_to_hom₁₂ (i j i' j' : A) (h₁ : i = i') (h₂ : j = j') : (shift_add X i j).hom ≫ eq_to_hom (by rw [h₁, h₂]) = eq_to_hom (by rw [h₁, h₂]) ≫ (shift_add X i' j').hom := by { cases h₁, cases h₂, rw [eq_to_hom_refl, eq_to_hom_refl, category.id_comp, category.comp_id] } @[reassoc] lemma eq_to_hom_comp_shift_add_inv₁ (i i' j : A) (h : i = i') : eq_to_hom (by rw h) ≫ (shift_add X i' j).inv = (shift_add X i j).inv ≫ eq_to_hom (by rw h) := by rw [iso.comp_inv_eq, category.assoc, iso.eq_inv_comp, shift_add_hom_comp_eq_to_hom₁] @[reassoc] lemma eq_to_hom_comp_shift_add_inv₂ (i j j' : A) (h : j = j') : eq_to_hom (by rw h) ≫ (shift_add X i j').inv = (shift_add X i j).inv ≫ eq_to_hom (by rw h) := by rw [iso.comp_inv_eq, category.assoc, iso.eq_inv_comp, shift_add_hom_comp_eq_to_hom₂] @[reassoc] lemma eq_to_hom_comp_shift_add_inv₁₂ (i j i' j' : A) (h₁ : i = i') (h₂ : j = j') : eq_to_hom (by rw [h₁, h₂]) ≫ (shift_add X i' j').inv = (shift_add X i j).inv ≫ eq_to_hom (by rw [h₁, h₂]) := by rw [iso.comp_inv_eq, category.assoc, iso.eq_inv_comp, shift_add_hom_comp_eq_to_hom₁₂] lemma shift_shift' (i j : A) : f⟦i⟧'⟦j⟧' = (shift_add X i j).inv ≫ f⟦i + j⟧' ≫ (shift_add Y i j).hom := by { symmetry, apply nat_iso.naturality_1 } variables (A) /-- Shifting by zero is the identity functor. -/ abbreviation shift_zero : X⟦0⟧ ≅ X := (shift_functor_zero C A).app _ lemma shift_zero' : f⟦(0 : A)⟧' = (shift_zero A X).hom ≫ f ≫ (shift_zero A Y).inv := by { symmetry, apply nat_iso.naturality_2 } end add_monoid section add_group variables (C) {A} [add_group A] [has_shift C A] variables (X Y : C) (f : X ⟶ Y) /-- Shifting by `i` is an equivalence. -/ instance (i : A) : is_equivalence (shift_functor C i) := begin change is_equivalence (add_neg_equiv (shift_monoidal_functor C A) i).functor, apply_instance, end @[simp] lemma shift_functor_inv (i : A) : (shift_functor C i).inv = shift_functor C (-i) := rfl /-- Shifting by `i` and then shifting by `-i` is the identity. -/ abbreviation shift_functor_comp_shift_functor_neg (i : A) : shift_functor C i ⋙ shift_functor C (-i) ≅ 𝟭 C := unit_of_tensor_iso_unit (shift_monoidal_functor C A) ⟨i⟩ ⟨(-i : A)⟩ (discrete.eq_to_iso (add_neg_self i)) /-- Shifting by `-i` and then shifting by `i` is the identity. -/ abbreviation shift_functor_neg_comp_shift_functor (i : A) : shift_functor C (-i) ⋙ shift_functor C i ≅ 𝟭 C := unit_of_tensor_iso_unit (shift_monoidal_functor C A) ⟨(-i : A)⟩ ⟨i⟩ (discrete.eq_to_iso (neg_add_self i)) section variables (C) /-- Shifting by `n` is a faithful functor. -/ instance shift_functor_faithful (i : A) : faithful (shift_functor C i) := faithful.of_comp_iso (shift_functor_comp_shift_functor_neg C i) /-- Shifting by `n` is a full functor. -/ instance shift_functor_full (i : A) : full (shift_functor C i) := begin haveI : full (shift_functor C i ⋙ shift_functor C (-i)) := full.of_iso (shift_functor_comp_shift_functor_neg C i).symm, exact full.of_comp_faithful _ (shift_functor C (-i)) end /-- Shifting by `n` is an essentially surjective functor. -/ instance shift_functor_ess_surj (i : A) : ess_surj (shift_functor C i) := { mem_ess_image := λ Y, ⟨Y⟦-i⟧, ⟨(shift_functor_neg_comp_shift_functor C i).app Y⟩⟩ } end variables {C} /-- Shifting by `i` and then shifting by `-i` is the identity. -/ abbreviation shift_shift_neg (i : A) : X⟦i⟧⟦-i⟧ ≅ X := (shift_functor_comp_shift_functor_neg C i).app _ /-- Shifting by `-i` and then shifting by `i` is the identity. -/ abbreviation shift_neg_shift (i : A) : X⟦-i⟧⟦i⟧ ≅ X := (shift_functor_neg_comp_shift_functor C i).app _ variables {X Y} lemma shift_shift_neg' (i : A) : f⟦i⟧'⟦-i⟧' = (shift_shift_neg X i).hom ≫ f ≫ (shift_shift_neg Y i).inv := by { symmetry, apply nat_iso.naturality_2 } lemma shift_neg_shift' (i : A) : f⟦-i⟧'⟦i⟧' = (shift_neg_shift X i).hom ≫ f ≫ (shift_neg_shift Y i).inv := by { symmetry, apply nat_iso.naturality_2 } lemma shift_equiv_triangle (n : A) (X : C) : (shift_shift_neg X n).inv⟦n⟧' ≫ (shift_neg_shift (X⟦n⟧) n).hom = 𝟙 (X⟦n⟧) := (add_neg_equiv (shift_monoidal_functor C A) n).functor_unit_iso_comp X section local attribute [reducible] discrete.add_monoidal lemma shift_shift_neg_hom_shift (n : A) (X : C) : (shift_shift_neg X n).hom ⟦n⟧' = (shift_neg_shift (X⟦n⟧) n).hom := begin -- This is just `simp, simp [eq_to_hom_map]`. simp only [iso.app_hom, unit_of_tensor_iso_unit_hom_app, eq_to_iso.hom, functor.map_comp, obj_μ_app, eq_to_iso.inv, obj_ε_inv_app, μ_naturalityₗ_assoc, category.assoc, μ_inv_hom_app_assoc, ε_inv_app_obj, μ_naturalityᵣ_assoc], simp only [eq_to_hom_map, eq_to_hom_app, eq_to_hom_trans], end end lemma shift_shift_neg_inv_shift (n : A) (X : C) : (shift_shift_neg X n).inv ⟦n⟧' = (shift_neg_shift (X⟦n⟧) n).inv := by { ext, rw [← shift_shift_neg_hom_shift, ← functor.map_comp, iso.hom_inv_id, functor.map_id] } @[simp] lemma shift_shift_neg_shift_eq (n : A) (X : C) : (shift_functor C n).map_iso (shift_shift_neg X n) = shift_neg_shift (X⟦n⟧) n := category_theory.iso.ext $ shift_shift_neg_hom_shift _ _ variables (C) /-- Shifting by `n` and shifting by `-n` forms an equivalence. -/ @[simps] def shift_equiv (n : A) : C ≌ C := { functor := shift_functor C n, inverse := shift_functor C (-n), ..(add_neg_equiv (shift_monoidal_functor C A) n) } variable {C} open category_theory.limits variables [has_zero_morphisms C] lemma shift_zero_eq_zero (X Y : C) (n : A) : (0 : X ⟶ Y)⟦n⟧' = (0 : X⟦n⟧ ⟶ Y⟦n⟧) := category_theory.functor.map_zero _ _ _ end add_group section add_comm_monoid variables {C A} [add_comm_monoid A] [has_shift C A] variables (X Y : C) (f : X ⟶ Y) /-- When shifts are indexed by an additive commutative monoid, then shifts commute. -/ def shift_comm (i j : A) : X⟦i⟧⟦j⟧ ≅ X⟦j⟧⟦i⟧ := (shift_add X i j).symm ≪≫ ((shift_monoidal_functor C A).to_functor.map_iso (discrete.eq_to_iso $ add_comm i j : (⟨i+j⟩ : discrete A) ≅ ⟨j+i⟩)).app X ≪≫ shift_add X j i @[simp] lemma shift_comm_symm (i j : A) : (shift_comm X i j).symm = shift_comm X j i := begin ext, dsimp [shift_comm], simpa [eq_to_hom_map], end variables {X Y} /-- When shifts are indexed by an additive commutative monoid, then shifts commute. -/ lemma shift_comm' (i j : A) : f⟦i⟧'⟦j⟧' = (shift_comm _ _ _).hom ≫ f⟦j⟧'⟦i⟧' ≫ (shift_comm _ _ _).hom := begin -- This is just `simp, simp [eq_to_hom_map]`. simp only [shift_comm, iso.trans_hom, iso.symm_hom, iso.app_inv, iso.symm_inv, monoidal_functor.μ_iso_hom, iso.app_hom, functor.map_iso_hom, eq_to_iso.hom, μ_naturality_assoc, nat_trans.naturality_assoc, nat_trans.naturality, functor.comp_map, category.assoc, μ_inv_hom_app_assoc], simp only [eq_to_hom_map, eq_to_hom_app, eq_to_hom_trans_assoc, eq_to_hom_refl, category.id_comp, μ_hom_inv_app_assoc], end @[reassoc] lemma shift_comm_hom_comp (i j : A) : (shift_comm X i j).hom ≫ f⟦j⟧'⟦i⟧' = f⟦i⟧'⟦j⟧' ≫ (shift_comm Y i j).hom := by rw [shift_comm', ← shift_comm_symm, iso.symm_hom, iso.inv_hom_id_assoc] end add_comm_monoid variables {D : Type*} [category D] [add_monoid A] [has_shift D A] variables (F : C ⥤ D) [full F] [faithful F] section local attribute [reducible] discrete.add_monoidal /-- Given a family of endomorphisms of `C` which are interwined by a fully faithful `F : C ⥤ D` with shift functors on `D`, we can promote that family to shift functors on `C`. -/ def has_shift_of_fully_faithful (s : A → C ⥤ C) (i : ∀ i, s i ⋙ F ≅ F ⋙ shift_functor D i) : has_shift C A := has_shift_mk C A { F := s, ε := nat_iso_of_comp_fully_faithful F (calc 𝟭 C ⋙ F ≅ F : functor.left_unitor _ ... ≅ F ⋙ 𝟭 D : (functor.right_unitor _).symm ... ≅ F ⋙ shift_functor D (0 : A) : iso_whisker_left F (shift_functor_zero D A).symm ... ≅ s 0 ⋙ F : (i 0).symm), μ := λ a b, nat_iso_of_comp_fully_faithful F (calc (s a ⋙ s b) ⋙ F ≅ s a ⋙ s b ⋙ F : functor.associator _ _ _ ... ≅ s a ⋙ F ⋙ shift_functor D b : iso_whisker_left _ (i b) ... ≅ (s a ⋙ F) ⋙ shift_functor D b : (functor.associator _ _ _).symm ... ≅ (F ⋙ shift_functor D a) ⋙ shift_functor D b : iso_whisker_right (i a) _ ... ≅ F ⋙ shift_functor D a ⋙ shift_functor D b : functor.associator _ _ _ ... ≅ F ⋙ shift_functor D (a + b) : iso_whisker_left _ (shift_functor_add D a b).symm ... ≅ s (a + b) ⋙ F : (i (a + b)).symm), associativity := begin intros, apply F.map_injective, dsimp, simp only [category.comp_id, category.id_comp, category.assoc, category_theory.functor.map_comp, functor.image_preimage, eq_to_hom_map, iso.inv_hom_id_app_assoc], erw (i m₃).hom.naturality_assoc, congr' 1, dsimp, simp only [eq_to_iso.inv, eq_to_hom_app, eq_to_hom_map, obj_μ_app, μ_naturality_assoc, category.assoc, category_theory.functor.map_comp, functor.image_preimage], congr' 3, dsimp, simp only [←(shift_functor D m₃).map_comp_assoc, iso.inv_hom_id_app], erw [(shift_functor D m₃).map_id, category.id_comp], erw [((shift_monoidal_functor D A).μ_iso ⟨m₁ + m₂⟩ ⟨m₃⟩).inv_hom_id_app_assoc], congr' 1, have := dcongr_arg (λ a, (i a).inv.app X) (add_assoc m₁ m₂ m₃), dsimp at this, simp [this], end, left_unitality := begin intros, apply F.map_injective, dsimp, simp only [category.comp_id, category.id_comp, category.assoc, category_theory.functor.map_comp, eq_to_hom_app, eq_to_hom_map, functor.image_preimage], erw (i n).hom.naturality_assoc, dsimp, simp only [eq_to_iso.inv, eq_to_hom_app, category.assoc, category_theory.functor.map_comp, eq_to_hom_map, obj_ε_app, functor.image_preimage], simp only [←(shift_functor D n).map_comp_assoc, iso.inv_hom_id_app], dsimp, simp only [category.id_comp, μ_inv_hom_app_assoc, category_theory.functor.map_id], have := dcongr_arg (λ a, (i a).inv.app X) (zero_add n), dsimp at this, simp [this], end, right_unitality := begin intros, apply F.map_injective, dsimp, simp only [category.comp_id, category.id_comp, category.assoc, iso.inv_hom_id_app_assoc, eq_to_iso.inv, eq_to_hom_app, eq_to_hom_map, category_theory.functor.map_comp, functor.image_preimage, obj_zero_map_μ_app, ε_hom_inv_app_assoc], have := dcongr_arg (λ a, (i a).inv.app X) (add_zero n), dsimp at this, simp [this], end, } end /-- When we construct shifts on a subcategory from shifts on the ambient category, the inclusion functor intertwines the shifts. -/ @[nolint unused_arguments] -- incorrectly reports that `[full F]` and `[faithful F]` are unused. def has_shift_of_fully_faithful_comm (s : A → C ⥤ C) (i : ∀ i, s i ⋙ F ≅ F ⋙ shift_functor D i) (m : A) : begin haveI := has_shift_of_fully_faithful F s i, exact (shift_functor C m) ⋙ F ≅ F ⋙ shift_functor D m end := i m end category_theory
526caa196a18c85535ebe9d94e1bbaa1d4880173
d5bef83c55d40cb88f9a01b755c882a93348a847
/tests/lean/local_notation_meta_bug.lean
8f7202158534fe045e1b078ffc56c6eb3fae5f8f
[ "Apache-2.0" ]
permissive
urkud/lean
587d78216e1f0c7f651566e9e92cf8ade285d58d
3526539070ea6268df5dd373deeb3ac8b9621952
refs/heads/master
1,660,171,634,921
1,657,873,466,000
1,657,873,466,000
249,789,456
0
0
Apache-2.0
1,585,075,263,000
1,585,075,263,000
null
UTF-8
Lean
false
false
202
lean
local infix ` + ` := nat.add @[class] local infix ` + ` := nat.add noncomputable local infix ` + ` := nat.add @[class] noncomputable local infix ` + ` := nat.add /-- foo -/ local infix ` + ` := nat.add
9cdde2d6f5b02cae74bef0e544ee2c0126324da6
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/data/mv_polynomial.lean
28f235b00b5bc39ce3d854ea78cee58f79cf18eb
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
42,144
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro Multivariate Polynomial -/ import algebra.ring import data.finsupp data.polynomial data.equiv.algebra open set function finsupp lattice universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `α` is the coefficient ring -/ def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := (σ →₀ ℕ) →₀ α namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} variables [decidable_eq σ] [decidable_eq α] section comm_semiring variables [comm_semiring α] {p q : mv_polynomial σ α} instance : decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq instance : has_zero (mv_polynomial σ α) := finsupp.has_zero instance : has_one (mv_polynomial σ α) := finsupp.has_one instance : has_add (mv_polynomial σ α) := finsupp.has_add instance : has_mul (mv_polynomial σ α) := finsupp.has_mul instance : comm_semiring (mv_polynomial σ α) := finsupp.comm_semiring /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a /-- `C a` is the constant polynomial with value `a` -/ def C (a : α) : mv_polynomial σ α := monomial 0 a /-- `X n` is the polynomial with value X_n -/ def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1 @[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl @[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by simp [C, monomial, single_mul_single] @[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add @[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm @[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n := by induction n; simp [pow_succ, *] instance : is_semiring_hom (C : α → mv_polynomial σ α) := { map_zero := C_0, map_one := C_1, map_add := λ a a', C_add, map_mul := λ a a', C_mul } lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n := by induction n; simp [nat.succ_eq_add_one, *] lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) := begin induction e, { simp [X], refl }, { simp [pow_succ, e_ih], simp [X, monomial, single_mul_single, nat.succ_eq_add_one] } end lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) := begin apply @finsupp.induction σ ℕ _ _ _ _ s, { simp [C, prod_zero_index]; exact (mul_one _).symm }, { assume n e s hns he ih, simp [prod_add_index, prod_single_index, pow_zero, pow_add, (mul_assoc _ _ _).symm, ih.symm, monomial_add_single] } end @[recursor 7] lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) : M p := have ∀s a, M (monomial s a), begin assume s a, apply @finsupp.induction σ ℕ _ _ _ _ s, { show M (monomial 0 a), from h_C a, }, { assume n e p hpn he ih, have : ∀e:ℕ, M (monomial p a * X n ^ e), { intro e, induction e, { simp [ih] }, { simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } }, simp [monomial_add_single, this] } end, finsupp.induction p (by have : M (C 0) := h_C 0; rwa [C_0] at this) (assume s a p hsp ha hp, h_add _ _ (this s a) hp) lemma hom_eq_hom [semiring γ] (f g : mv_polynomial σ α → γ) (hf : is_semiring_hom f) (hg : is_semiring_hom g) (hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) : f p = g p := mv_polynomial.induction_on p hC begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end lemma is_id (f : mv_polynomial σ α → mv_polynomial σ α) (hf : is_semiring_hom f) (hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) : f p = p := hom_eq_hom f id hf is_semiring_hom.id hC hX p section coeff def tmp.coe : has_coe_to_fun (mv_polynomial σ α) := by delta mv_polynomial; apply_instance local attribute [instance] tmp.coe def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m lemma ext (p q : mv_polynomial σ α) : (∀ m, coeff m p = coeff m q) → p = q := ext lemma ext_iff (p q : mv_polynomial σ α) : (∀ m, coeff m p = coeff m q) ↔ p = q := ⟨ext p q, λ h m, by rw h⟩ @[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) : coeff m (p + q) = coeff m p + coeff m q := add_apply @[simp] lemma coeff_zero (m : σ →₀ ℕ) : coeff m (0 : mv_polynomial σ α) = 0 := rfl @[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 := rfl instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) : is_add_monoid_hom (coeff m : mv_polynomial σ α → α) := { map_add := coeff_add m, map_zero := coeff_zero m } lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) : coeff m (s.sum f) = s.sum (λ x, coeff m (f x)) := (finset.sum_hom _).symm lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) := by simp [monomial_eq] @[simp] lemma coeff_monomial (m n) (a) : coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 := single_apply @[simp] lemma coeff_C (m) (a) : coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 := single_apply lemma coeff_X_pow (i : σ) (m) (k : ℕ) : coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 := begin have := coeff_monomial m (finsupp.single i k) (1:α), rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _ _ _, C_1, one_mul, finsupp.prod_single_index] at this, exact pow_zero _ end lemma coeff_X' (i : σ) (m) : coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 := by rw [← coeff_X_pow, pow_one] @[simp] lemma coeff_X (i : σ) : coeff (single i 1) (X i : mv_polynomial σ α) = 1 := by rw [coeff_X', if_pos rfl] @[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p := begin rw [mul_def, C, monomial], simp only [sum_single_index, zero_mul, single_zero, zero_add, sum_zero], convert sum_apply, simp only [single_apply, finsupp.sum], rw finset.sum_eq_single m, { rw if_pos rfl, refl }, { intros m' hm' H, apply if_neg, exact H }, { intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } end lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) : coeff n (p * q) = finset.sum (antidiagonal n).support (λ x, coeff x.1 p * coeff x.2 q) := begin rw mul_def, have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support) (λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0), convert this.symm using 1; clear this, { rw [coeff], repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only}, exact single_apply }, { have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆ (antidiagonal n).support := finset.filter_subset _, rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and, not_and, not_mem_support_iff] at hx, by_cases H : x.1 ∈ p.support, { rw [coeff, coeff, hx.2 hx.1 H, mul_zero] }, { rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } }, symmetry, rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx, rw if_neg, exact hx.2 hx.1 }, { apply finset.sum_bij, swap 5, { intros x hx, exact (x.1, x.2) }, { intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx, simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm }, { intros x hx, rw finset.mem_filter at hx, rw if_pos hx.2 }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro }, { rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance }, { rw [finset.mem_filter, mem_antidiagonal_support] at hij, simpa [finset.mem_filter, finset.mem_sigma] using hij.symm }, { refl } } }, all_goals { apply_instance } } end @[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) : coeff (m + single s 1) (p * X s) = coeff m p := begin have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl, rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), finset.sum_eq_zero, add_zero, coeff_X, mul_one], rintros ⟨i,j⟩ hij, rw [finset.mem_erase, mem_antidiagonal_support] at hij, by_cases H : single s 1 = j, { subst j, simpa using hij }, { rw [coeff_X', if_neg H, mul_zero] }, end lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) : coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 := begin split_ifs with h h, { conv_rhs {rw ← coeff_mul_X _ s}, congr' 1, ext t, by_cases hj : s = t, { subst t, simp only [nat_sub_apply, add_apply, single_eq_same], refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h }, { simp [single_eq_of_ne hj] } }, { delta coeff, rw ← not_mem_support_iff, intro hm, apply h, have H := support_mul _ _ hm, simp only [finset.mem_bind] at H, rcases H with ⟨j, hj, i', hi', H⟩, delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i', erw finset.mem_singleton at H, subst m, rw [mem_support_iff, add_apply, single_apply, if_pos rfl], intro H, rw [add_eq_zero_iff] at H, exact one_ne_zero H.2 } end end coeff section eval₂ variables [comm_semiring β] variables (f : α → β) (g : σ → β) /-- Evaluate a polynomial `p` given a valuation `g` of all the variables and a ring hom `f` from the scalar ring to the target -/ def eval₂ (p : mv_polynomial σ α) : β := p.sum (λs a, f a * s.prod (λn e, g n ^ e)) @[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 := finsupp.sum_zero_index variables [is_semiring_hom f] @[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := finsupp.sum_add_index (by simp [is_semiring_hom.map_zero f]) (by simp [add_mul, is_semiring_hom.map_add f]) @[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) := finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f]) @[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a := by simp [eval₂_monomial, C, prod_zero_index] @[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 := (eval₂_C _ _ _).trans (is_semiring_hom.map_one f) @[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n := by simp [eval₂_monomial, is_semiring_hom.map_one f, X, prod_single_index, pow_one] lemma eval₂_mul_monomial : ∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) := begin apply mv_polynomial.induction_on p, { assume a' s a, simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] }, { assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] }, { assume p n ih s a, from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g : by simp [monomial_single_add, -add_comm, pow_one, mul_assoc] ... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) : by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm, is_semiring_hom.map_one f, -add_comm] } end @[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := begin apply mv_polynomial.induction_on q, { simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] }, { simp [mul_add, eval₂_add] {contextual := tt} }, { simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} } end @[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n | 0 := eval₂_one _ _ | (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow] instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ p q, eval₂_add _ _, map_mul := λ p q, eval₂_mul _ _ } lemma eval₂_comp_left {γ} [comm_semiring γ] (k : β → γ) [is_semiring_hom k] (f : α → β) [is_semiring_hom f] (g : σ → β) (p) : k (eval₂ f g p) = eval₂ (k ∘ f) (k ∘ g) p := by apply mv_polynomial.induction_on p; simp [ eval₂_add, is_semiring_hom.map_add k, eval₂_mul, is_semiring_hom.map_mul k] {contextual := tt} @[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p := by apply mv_polynomial.induction_on p; simp [eval₂_add, eval₂_mul] {contextual := tt} lemma eval₂_congr (g₁ g₂ : σ → β) (h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) : p.eval₂ f g₁ = p.eval₂ f g₂ := begin apply finset.sum_congr rfl, intros c hc, dsimp, congr' 1, apply finset.prod_congr rfl, intros i hi, dsimp, congr' 1, apply h hi, rwa finsupp.mem_support_iff at hc end @[simp] lemma eval₂_prod [decidable_eq γ] (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (s.prod p) = s.prod (λ x, eval₂ f g $ p x) := (finset.prod_hom _).symm @[simp] lemma eval₂_sum [decidable_eq γ] (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (s.sum p) = s.sum (λ x, eval₂ f g $ p x) := (finset.sum_hom _).symm attribute [to_additive] eval₂_prod lemma eval₂_assoc [decidable_eq γ] (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) : eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) := by { rw eval₂_comp_left (eval₂ f g), congr, funext, simp } end eval₂ section eval variables {f : σ → α} /-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/ def eval (f : σ → α) : mv_polynomial σ α → α := eval₂ id f @[simp] lemma eval_zero : (0 : mv_polynomial σ α).eval f = 0 := eval₂_zero _ _ @[simp] lemma eval_add : (p + q).eval f = p.eval f + q.eval f := eval₂_add _ _ lemma eval_monomial : (monomial s a).eval f = a * s.prod (λn e, f n ^ e) := eval₂_monomial _ _ @[simp] lemma eval_C : ∀ a, (C a).eval f = a := eval₂_C _ _ @[simp] lemma eval_X : ∀ n, (X n).eval f = f n := eval₂_X _ _ @[simp] lemma eval_mul : (p * q).eval f = p.eval f * q.eval f := eval₂_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval f) := eval₂.is_semiring_hom _ _ theorem eval_assoc {τ} [decidable_eq τ] (f : σ → mv_polynomial τ α) (g : τ → α) (p : mv_polynomial σ α) : p.eval (eval g ∘ f) = (eval₂ C f p).eval g := begin rw eval₂_comp_left (eval g), unfold eval, congr; funext a; simp end end eval section map variables [comm_semiring β] [decidable_eq β] variables (f : α → β) [is_semiring_hom f] /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : mv_polynomial σ α → mv_polynomial σ β := eval₂ (C ∘ f) X @[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) := (eval₂_monomial _ _).trans monomial_eq.symm @[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _ @[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _ @[simp] theorem map_one : map f (1 : mv_polynomial σ α) = 1 := eval₂_one _ _ @[simp] theorem map_add (p q : mv_polynomial σ α) : map f (p + q) = map f p + map f q := eval₂_add _ _ @[simp] theorem map_mul (p q : mv_polynomial σ α) : map f (p * q) = map f p * map f q := eval₂_mul _ _ @[simp] lemma map_pow (p : mv_polynomial σ α) (n : ℕ) : map f (p^n) = (map f p)^n := eval₂_pow _ _ instance map.is_semiring_hom : is_semiring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_semiring_hom _ _ theorem map_id : ∀ (p : mv_polynomial σ α), map id p = p := eval₂_eta theorem map_map [comm_semiring γ] [decidable_eq γ] (g : β → γ) [is_semiring_hom g] (p : mv_polynomial σ α) : map g (map f p) = map (g ∘ f) p := (eval₂_comp_left (map g) (C ∘ f) X p).trans $ by congr; funext a; simp theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) : p.eval₂ f g = (map f p).eval g := begin unfold map eval, rw eval₂_comp_left (eval₂ id g), congr; funext a; simp end lemma eval₂_comp_right {γ} [comm_semiring γ] (k : β → γ) [is_semiring_hom k] (f : α → β) [is_semiring_hom f] (g : σ → β) (p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, is_semiring_hom.map_add k, map_add, eval₂_add, hp, hq] }, { intros p s hp, rw [eval₂_mul, is_semiring_hom.map_mul k, map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] } end lemma map_eval₂ [decidable_eq γ] [decidable_eq δ] (f : α → β) [is_semiring_hom f] (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) : map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, map_add, hp, hq, map_add, eval₂_add] }, { intros p s hp, rw [eval₂_mul, map_mul, hp, map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] } end lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (p.map f) = f (coeff m p) := begin apply mv_polynomial.induction_on p; clear p, { intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw is_semiring_hom.map_zero f }, { intros p q hp hq m, simp only [hp, hq, map_add, coeff_add], rw is_semiring_hom.map_add f }, { intros p i hp m, simp only [hp, map_mul, map_X], simp only [hp, mem_support_iff, coeff_mul_X'], split_ifs, {refl}, rw is_semiring_hom.map_zero f } end lemma map_injective (hf : function.injective f) : function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) := λ p q h, ext _ _ $ λ m, hf $ begin rw ← ext_iff at h, specialize h m, rw [coeff_map, coeff_map] at h, exact h end end map section degrees section comm_semiring def degrees (p : mv_polynomial σ α) : multiset σ := p.support.sup (λs:σ →₀ ℕ, s.to_multiset) lemma degrees_monomial (s : σ →₀ ℕ) (a : α) : degrees (monomial s a) ≤ s.to_multiset := finset.sup_le $ assume t h, begin have := finsupp.support_single_subset h, rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, rw this end lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : α) (ha : a ≠ 0) : degrees (monomial s a) = s.to_multiset := le_antisymm (degrees_monomial s a) $ finset.le_sup $ by rw [monomial, finsupp.support_single_ne_zero ha, finset.singleton_eq_singleton, finset.mem_singleton] lemma degrees_C (a : α) : degrees (C a : mv_polynomial σ α) = 0 := multiset.le_zero.1 $ degrees_monomial _ _ lemma degrees_X (n : σ) : degrees (X n : mv_polynomial σ α) ≤ {n} := le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _ lemma degrees_zero : degrees (0 : mv_polynomial σ α) = 0 := degrees_C 0 lemma degrees_one : degrees (1 : mv_polynomial σ α) = 0 := degrees_C 1 lemma degrees_add (p q : mv_polynomial σ α) : (p + q).degrees ≤ p.degrees ⊔ q.degrees := begin refine finset.sup_le (assume b hb, _), cases finset.mem_union.1 (finsupp.support_add hb), { exact le_sup_left_of_le (finset.le_sup h) }, { exact le_sup_right_of_le (finset.le_sup h) }, end lemma degrees_sum {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → mv_polynomial σ α) : (s.sum f).degrees ≤ s.sup (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ }, { assume i s his ih, rw [finset.sup_insert, finset.sum_insert his], exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) } end lemma degrees_mul (p q : mv_polynomial σ α) : (p * q).degrees ≤ p.degrees + q.degrees := begin refine finset.sup_le (assume b hb, _), have := support_mul p q hb, simp only [finset.mem_bind, finset.singleton_eq_singleton, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.to_multiset_add], exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) end lemma degrees_prod {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).degrees ≤ s.sum (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.prod_empty, finset.sum_empty, degrees_one] }, { assume i s his ih, rw [finset.prod_insert his, finset.sum_insert his], exact le_trans (degrees_mul _ _) (add_le_add_left ih _) } end lemma degrees_pow (p : mv_polynomial σ α) : ∀(n : ℕ), (p^n).degrees ≤ add_monoid.smul n p.degrees | 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end | (n + 1) := le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _) end comm_semiring end degrees section vars /-- `vars p` is the set of variables appearing in the polynomial `p` -/ def vars (p : mv_polynomial σ α) : finset σ := p.degrees.to_finset @[simp] lemma vars_0 : (0 : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_zero, multiset.to_finset_zero] @[simp] lemma vars_monomial (h : a ≠ 0) : (monomial s a).vars = s.support := by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset] @[simp] lemma vars_C : (C a : mv_polynomial σ α).vars = ∅ := by rw [vars, degrees_C, multiset.to_finset_zero] @[simp] lemma vars_X (h : 0 ≠ (1 : α)) : (X n : mv_polynomial σ α).vars = {n} := by rw [X, vars_monomial h.symm, finsupp.support_single_ne_zero zero_ne_one.symm] end vars section degree_of /-- `degree_of n p` gives the highest power of X_n that appears in `p` -/ def degree_of (n : σ) (p : mv_polynomial σ α) : ℕ := p.degrees.count n end degree_of section total_degree /-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/ def total_degree (p : mv_polynomial σ α) : ℕ := p.support.sup (λs, s.sum $ λn e, e) lemma total_degree_eq (p : mv_polynomial σ α) : p.total_degree = p.support.sup (λm, m.to_multiset.card) := begin rw [total_degree], congr, funext m, exact (finsupp.card_to_multiset _).symm end lemma total_degree_le_degrees_card (p : mv_polynomial σ α) : p.total_degree ≤ p.degrees.card := begin rw [total_degree_eq], exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs) end lemma total_degree_C (a : α) : (C a : mv_polynomial σ α).total_degree = 0 := nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn, have _ := finsupp.support_single_subset hn, begin rw [finset.singleton_eq_singleton, finset.mem_singleton] at this, subst this, exact le_refl _ end lemma total_degree_zero : (0 : mv_polynomial σ α).total_degree = 0 := by rw [← C_0]; exact total_degree_C (0 : α) lemma total_degree_one : (1 : mv_polynomial σ α).total_degree = 0 := total_degree_C (1 : α) lemma total_degree_add (a b : mv_polynomial σ α) : (a + b).total_degree ≤ max a.total_degree b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_add hn, begin rcases finset.mem_union.1 this, { exact le_max_left_of_le (finset.le_sup h) }, { exact le_max_right_of_le (finset.le_sup h) } end lemma total_degree_mul (a b : mv_polynomial σ α) : (a * b).total_degree ≤ a.total_degree + b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_mul a b hn, begin simp only [finset.mem_bind, finset.mem_singleton, finset.singleton_eq_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.sum_add_index], { exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) }, { assume a, refl }, { assume a b₁ b₂, refl } end lemma total_degree_list_prod : ∀(s : list (mv_polynomial σ α)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum | [] := by rw [@list.prod_nil (mv_polynomial σ α) _, total_degree_one]; refl | (p :: ps) := begin rw [@list.prod_cons (mv_polynomial σ α) _, list.map, list.sum_cons], exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _) end lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ α)) : s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum := begin refine quotient.induction_on s (assume l, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum], exact total_degree_list_prod l end lemma total_degree_finset_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) : (s.prod f).total_degree ≤ s.sum (λi, (f i).total_degree) := begin refine le_trans (total_degree_multiset_prod _) _, rw [multiset.map_map], refl end end total_degree end comm_semiring section comm_ring variable [comm_ring α] variables {p q : mv_polynomial σ α} instance : ring (mv_polynomial σ α) := finsupp.ring instance : comm_ring (mv_polynomial σ α) := finsupp.comm_ring instance : has_scalar α (mv_polynomial σ α) := finsupp.has_scalar instance : module α (mv_polynomial σ α) := finsupp.module _ α instance C.is_ring_hom : is_ring_hom (C : α → mv_polynomial σ α) := by apply is_ring_hom.of_semiring variables (σ a a') lemma C_sub : (C (a - a') : mv_polynomial σ α) = C a - C a' := is_ring_hom.map_sub _ @[simp] lemma C_neg : (C (-a) : mv_polynomial σ α) = -C a := is_ring_hom.map_neg _ @[simp] lemma coeff_sub (m : σ →₀ ℕ) (p q : mv_polynomial σ α) : coeff m (p - q) = coeff m p - coeff m q := finsupp.sub_apply instance coeff.is_add_group_hom (m : σ →₀ ℕ) : is_add_group_hom (coeff m : mv_polynomial σ α → α) := { map_add := coeff_add m } variables {σ} (p) theorem C_mul' : mv_polynomial.C a * p = a • p := begin apply finsupp.induction p, { exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm }, intros p b f haf hb0 ih, rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1, rw [finsupp.mul_def, finsupp.smul_single, mv_polynomial.C, mv_polynomial.monomial], rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add, smul_eq_mul], { rw [mul_zero, finsupp.single_zero] }, { rw finsupp.sum_single_index, all_goals { rw [zero_mul, finsupp.single_zero] } } end lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p := begin rw [← finsupp.sum_single p, @finsupp.smul_sum (σ →₀ ℕ) α α, finsupp.mul_sum], refine finset.sum_congr rfl (assume n _, _), simp only [finsupp.smul_single], exact C_mul_monomial.symm end @[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : (s • p).eval x = s * p.eval x := by rw [smul_eq_C_mul, eval_mul, eval_C] section degrees lemma degrees_neg [comm_ring α] (p : mv_polynomial σ α) : (- p).degrees = p.degrees := by rw [degrees, finsupp.support_neg]; refl lemma degrees_sub [comm_ring α] (p q : mv_polynomial σ α) : (p - q).degrees ≤ p.degrees ⊔ q.degrees := le_trans (degrees_add p (-q)) $ by rw [degrees_neg] end degrees section eval₂ variables [comm_ring β] variables (f : α → β) [is_ring_hom f] (g : σ → β) instance eval₂.is_ring_hom : is_ring_hom (eval₂ f g) := by apply is_ring_hom.of_semiring lemma eval₂_sub : (p - q).eval₂ f g = p.eval₂ f g - q.eval₂ f g := is_ring_hom.map_sub _ @[simp] lemma eval₂_neg : (-p).eval₂ f g = -(p.eval₂ f g) := is_ring_hom.map_neg _ lemma hom_C (f : mv_polynomial σ ℤ → β) [is_ring_hom f] (n : ℤ) : f (C n) = (n : β) := congr_fun (int.eq_cast' (f ∘ C)) n /-- A ring homomorphism f : Z[X_1, X_2, ...] → R is determined by the evaluations f(X_1), f(X_2), ... -/ @[simp] lemma eval₂_hom_X {α : Type u} [decidable_eq α] (c : ℤ → β) [is_ring_hom c] (f : mv_polynomial α ℤ → β) [is_ring_hom f] (x : mv_polynomial α ℤ) : eval₂ c (f ∘ X) x = f x := mv_polynomial.induction_on x (λ n, by { rw [hom_C f, eval₂_C, int.eq_cast' c], refl }) (λ p q hp hq, by { rw [eval₂_add, hp, hq], exact (is_ring_hom.map_add f).symm }) (λ p n hp, by { rw [eval₂_mul, eval₂_X, hp], exact (is_ring_hom.map_mul f).symm }) end eval₂ section eval variables (f : σ → α) instance eval.is_ring_hom : is_ring_hom (eval f) := eval₂.is_ring_hom _ _ lemma eval_sub : (p - q).eval f = p.eval f - q.eval f := is_ring_hom.map_sub _ @[simp] lemma eval_neg : (-p).eval f = -(p.eval f) := is_ring_hom.map_neg _ end eval section map variables [decidable_eq β] [comm_ring β] variables (f : α → β) [is_ring_hom f] instance map.is_ring_hom : is_ring_hom (map f : mv_polynomial σ α → mv_polynomial σ β) := eval₂.is_ring_hom _ _ lemma map_sub : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ end map end comm_ring section rename variables {α} [comm_semiring α] [decidable_eq α] [decidable_eq β] [decidable_eq γ] [decidable_eq δ] def rename (f : β → γ) : mv_polynomial β α → mv_polynomial γ α := eval₂ C (X ∘ f) instance rename.is_semiring_hom (f : β → γ) : is_semiring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := by unfold rename; apply_instance @[simp] lemma rename_C (f : β → γ) (a : α) : rename f (C a) = C a := eval₂_C _ _ _ @[simp] lemma rename_X (f : β → γ) (b : β) : rename f (X b : mv_polynomial β α) = X (f b) := eval₂_X _ _ _ @[simp] lemma rename_zero (f : β → γ) : rename f (0 : mv_polynomial β α) = 0 := eval₂_zero _ _ @[simp] lemma rename_one (f : β → γ) : rename f (1 : mv_polynomial β α) = 1 := eval₂_one _ _ @[simp] lemma rename_add (f : β → γ) (p q : mv_polynomial β α) : rename f (p + q) = rename f p + rename f q := eval₂_add _ _ @[simp] lemma rename_sub {α} [comm_ring α] [decidable_eq α] (f : β → γ) (p q : mv_polynomial β α) : rename f (p - q) = rename f p - rename f q := eval₂_sub _ _ _ @[simp] lemma rename_mul (f : β → γ) (p q : mv_polynomial β α) : rename f (p * q) = rename f p * rename f q := eval₂_mul _ _ @[simp] lemma rename_pow (f : β → γ) (p : mv_polynomial β α) (n : ℕ) : rename f (p^n) = (rename f p)^n := eval₂_pow _ _ lemma map_rename [comm_semiring β] (f : α → β) [is_semiring_hom f] (g : γ → δ) (p : mv_polynomial γ α) : map f (rename g p) = rename g (map f p) := mv_polynomial.induction_on p (λ a, by simp) (λ p q hp hq, by simp [hp, hq]) (λ p n hp, by simp [hp]) @[simp] lemma rename_rename (f : β → γ) (g : γ → δ) (p : mv_polynomial β α) : rename g (rename f p) = rename (g ∘ f) p := show rename g (eval₂ C (X ∘ f) p) = _, by simp only [eval₂_comp_left (rename g) C (X ∘ f) p, (∘), rename_C, rename_X]; refl @[simp] lemma rename_id (p : mv_polynomial β α) : rename id p = p := eval₂_eta p lemma rename_monomial (f : β → γ) (p : β →₀ ℕ) (a : α) : rename f (monomial p a) = monomial (p.map_domain f) a := begin rw [rename, eval₂_monomial, monomial_eq, finsupp.prod_map_domain_index], { exact assume n, pow_zero _ }, { exact assume n i₁ i₂, pow_add _ _ _ } end lemma rename_eq (f : β → γ) (p : mv_polynomial β α) : rename f p = finsupp.map_domain (finsupp.map_domain f) p := begin simp only [rename, eval₂, finsupp.map_domain], congr, ext s a : 2, rw [← monomial, monomial_eq, finsupp.prod_sum_index], congr, ext n i : 2, rw [finsupp.prod_single_index], exact pow_zero _, exact assume a, pow_zero _, exact assume a b c, pow_add _ _ _ end lemma injective_rename (f : β → γ) (hf : function.injective f) : function.injective (rename f : mv_polynomial β α → mv_polynomial γ α) := have (rename f : mv_polynomial β α → mv_polynomial γ α) = finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f), begin rw this, exact finsupp.injective_map_domain (finsupp.injective_map_domain hf) end lemma total_degree_rename_le (f : β → γ) (p : mv_polynomial β α) : (p.rename f).total_degree ≤ p.total_degree := finset.sup_le $ assume b, begin assume h, rw rename_eq at h, have h' := finsupp.map_domain_support h, rcases finset.mem_image.1 h' with ⟨s, hs, rfl⟩, rw finsupp.sum_map_domain_index, exact le_trans (le_refl _) (finset.le_sup hs), exact assume _, rfl, exact assume _ _ _, rfl end section variables [comm_semiring β] (f : α → β) [is_semiring_hom f] variables (k : γ → δ) (g : δ → β) (p : mv_polynomial γ α) lemma eval₂_rename : (p.rename k).eval₂ f g = p.eval₂ f (g ∘ k) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma rename_eval₂ (g : δ → mv_polynomial γ α) : (p.eval₂ C (g ∘ k)).rename k = (p.rename k).eval₂ C (rename k ∘ g) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma rename_prodmk_eval₂ (d : δ) (g : γ → mv_polynomial γ α) : (p.eval₂ C g).rename (prod.mk d) = p.eval₂ C (λ x, (g x).rename (prod.mk d)) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma eval₂_rename_prodmk (g : δ × γ → β) (d : δ) : (rename (prod.mk d) p).eval₂ f g = eval₂ f (λ i, g (d, i)) p := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma eval_rename_prodmk (g : δ × γ → α) (d : δ) : (rename (prod.mk d) p).eval g = eval (λ i, g (d, i)) p := eval₂_rename_prodmk id _ _ _ end end rename lemma eval₂_cast_comp {β : Type u} {γ : Type v} [decidable_eq β] [decidable_eq γ] (f : γ → β) {α : Type w} [comm_ring α] (c : ℤ → α) [is_ring_hom c] (g : β → α) (x : mv_polynomial γ ℤ) : eval₂ c (g ∘ f) x = eval₂ c g (rename f x) := mv_polynomial.induction_on x (λ n, by simp only [eval₂_C, rename_C]) (λ p q hp hq, by simp only [hp, hq, rename, eval₂_add]) (λ p n hp, by simp only [hp, rename, eval₂_X, eval₂_mul]) instance rename.is_ring_hom {α} [comm_ring α] [decidable_eq α] [decidable_eq β] [decidable_eq γ] (f : β → γ) : is_ring_hom (rename f : mv_polynomial β α → mv_polynomial γ α) := @is_ring_hom.of_semiring (mv_polynomial β α) (mv_polynomial γ α) _ _ (rename f) (rename.is_semiring_hom f) section equiv variables (α) [comm_ring α] variables [decidable_eq β] [decidable_eq γ] [decidable_eq δ] set_option class.instance_max_depth 40 def pempty_ring_equiv : mv_polynomial pempty α ≃r α := { to_fun := mv_polynomial.eval₂ id $ pempty.elim, inv_fun := C, left_inv := is_id _ (by apply_instance) (assume a, by rw [eval₂_C]; refl) (assume a, a.elim), right_inv := λ r, eval₂_C _ _ _, hom := eval₂.is_ring_hom _ _ } def punit_ring_equiv : mv_polynomial punit α ≃r polynomial α := { to_fun := eval₂ polynomial.C (λu:punit, polynomial.X), inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star), left_inv := begin refine is_id _ _ _ _, apply is_semiring_hom.comp (eval₂ polynomial.C (λu:punit, polynomial.X)) _; apply_instance, { assume a, rw [eval₂_C, polynomial.eval₂_C] }, { rintros ⟨⟩, rw [eval₂_X, polynomial.eval₂_X] } end, right_inv := assume p, polynomial.induction_on p (assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C]) (assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq]) (assume p n hp, by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C, eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]), hom := eval₂.is_ring_hom _ _ } def ring_equiv_of_equiv (e : β ≃ γ) : mv_polynomial β α ≃r mv_polynomial γ α := { to_fun := rename e, inv_fun := rename e.symm, left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p, right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p, hom := rename.is_ring_hom e } def ring_equiv_congr [comm_ring γ] (e : α ≃r γ) : mv_polynomial β α ≃r mv_polynomial β γ := { to_fun := map e.to_equiv, inv_fun := map e.symm.to_equiv, left_inv := assume p, have (e.symm.to_equiv ∘ e.to_equiv) = id, { ext a, exact e.to_equiv.symm_apply_apply a }, by simp only [map_map, this, map_id], right_inv := assume p, have (e.to_equiv ∘ e.symm.to_equiv) = id, { ext a, exact e.to_equiv.apply_symm_apply a }, by simp only [map_map, this, map_id], hom := map.is_ring_hom e.to_fun } section variables (β γ δ) instance ring_on_sum : ring (mv_polynomial (β ⊕ γ) α) := by apply_instance instance ring_on_iter : ring (mv_polynomial β (mv_polynomial γ α)) := by apply_instance def sum_to_iter : mv_polynomial (β ⊕ γ) α → mv_polynomial β (mv_polynomial γ α) := eval₂ (C ∘ C) (λbc, sum.rec_on bc X (C ∘ X)) instance is_semiring_hom_C_C : is_semiring_hom (C ∘ C : α → mv_polynomial β (mv_polynomial γ α)) := @is_semiring_hom.comp _ _ _ _ C mv_polynomial.is_semiring_hom _ _ C mv_polynomial.is_semiring_hom instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter α β γ) := eval₂.is_semiring_hom _ _ lemma sum_to_iter_C (a : α) : sum_to_iter α β γ (C a) = C (C a) := eval₂_C _ _ a lemma sum_to_iter_Xl (b : β) : sum_to_iter α β γ (X (sum.inl b)) = X b := eval₂_X _ _ (sum.inl b) lemma sum_to_iter_Xr (c : γ) : sum_to_iter α β γ (X (sum.inr c)) = C (X c) := eval₂_X _ _ (sum.inr c) def iter_to_sum : mv_polynomial β (mv_polynomial γ α) → mv_polynomial (β ⊕ γ) α := eval₂ (eval₂ C (X ∘ sum.inr)) (X ∘ sum.inl) section instance is_semiring_hom_iter_to_sum : is_semiring_hom (iter_to_sum α β γ) := eval₂.is_semiring_hom _ _ end lemma iter_to_sum_C_C (a : α) : iter_to_sum α β γ (C (C a)) = C a := eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _) lemma iter_to_sum_X (b : β) : iter_to_sum α β γ (X b) = X (sum.inl b) := eval₂_X _ _ _ lemma iter_to_sum_C_X (c : γ) : iter_to_sum α β γ (C (X c)) = X (sum.inr c) := eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _) def mv_polynomial_equiv_mv_polynomial [comm_ring δ] (f : mv_polynomial β α → mv_polynomial γ δ) (hf : is_semiring_hom f) (g : mv_polynomial γ δ → mv_polynomial β α) (hg : is_semiring_hom g) (hfgC : ∀a, f (g (C a)) = C a) (hfgX : ∀n, f (g (X n)) = X n) (hgfC : ∀a, g (f (C a)) = C a) (hgfX : ∀n, g (f (X n)) = X n) : mv_polynomial β α ≃r mv_polynomial γ δ := { to_fun := f, inv_fun := g, left_inv := is_id _ (is_semiring_hom.comp _ _) hgfC hgfX, right_inv := is_id _ (is_semiring_hom.comp _ _) hfgC hfgX, hom := is_ring_hom.of_semiring f } def sum_ring_equiv : mv_polynomial (β ⊕ γ) α ≃r mv_polynomial β (mv_polynomial γ α) := begin apply @mv_polynomial_equiv_mv_polynomial α (β ⊕ γ) _ _ _ _ _ _ _ _ (sum_to_iter α β γ) _ (iter_to_sum α β γ) _, { assume p, apply @hom_eq_hom _ _ _ _ _ _ _ _ _ _ _ _ _ p, apply_instance, { apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, apply @is_semiring_hom.comp _ _ _ _ _ _ _ _ _ _, apply_instance, { apply @mv_polynomial.is_semiring_hom }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ } }, { apply mv_polynomial.is_semiring_hom }, { assume a, rw [iter_to_sum_C_C α β γ, sum_to_iter_C α β γ] }, { assume c, rw [iter_to_sum_C_X α β γ, sum_to_iter_Xr α β γ] } }, { assume b, rw [iter_to_sum_X α β γ, sum_to_iter_Xl α β γ] }, { assume a, rw [sum_to_iter_C α β γ, iter_to_sum_C_C α β γ] }, { assume n, cases n with b c, { rw [sum_to_iter_Xl, iter_to_sum_X] }, { rw [sum_to_iter_Xr, iter_to_sum_C_X] } }, { apply mv_polynomial.is_semiring_hom_sum_to_iter α β γ }, { apply mv_polynomial.is_semiring_hom_iter_to_sum α β γ } end instance option_ring : ring (mv_polynomial (option β) α) := mv_polynomial.ring instance polynomial_ring : ring (polynomial (mv_polynomial β α)) := @comm_ring.to_ring _ polynomial.comm_ring instance polynomial_ring2 : ring (mv_polynomial β (polynomial α)) := by apply_instance def option_equiv_left : mv_polynomial (option β) α ≃r polynomial (mv_polynomial β α) := (ring_equiv_of_equiv α $ (equiv.option_equiv_sum_punit β).trans (equiv.sum_comm _ _)).trans $ (sum_ring_equiv α _ _).trans $ punit_ring_equiv _ def option_equiv_right : mv_polynomial (option β) α ≃r mv_polynomial β (polynomial α) := (ring_equiv_of_equiv α $ equiv.option_equiv_sum_punit.{0} β).trans $ (sum_ring_equiv α β unit).trans $ ring_equiv_congr (mv_polynomial unit α) (punit_ring_equiv α) end end equiv end mv_polynomial
ab702d49b6c9b7bda5c988e18bf6c2fae636adff
bb31430994044506fa42fd667e2d556327e18dfe
/src/number_theory/modular_forms/basic.lean
d2811244e051a534f03670dbbcbdb186bc5694cc
[ "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
11,578
lean
/- Copyright (c) 2022 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import geometry.manifold.mfderiv import analysis.complex.upper_half_plane.functions_bounded_at_infty import analysis.complex.upper_half_plane.topology import number_theory.modular_forms.slash_invariant_forms /-! # Modular forms This file defines modular forms and proves some basic properties about them. We begin by defining modular forms and cusp forms as extension of `slash_invariant_forms` then we define the space of modular forms, cusp forms and prove that the product of two modular forms is a modular form. -/ open complex upper_half_plane open_locale topological_space manifold upper_half_plane noncomputable theory instance upper_half_plane.charted_space : charted_space ℂ ℍ := upper_half_plane.open_embedding_coe.singleton_charted_space instance upper_half_plane.smooth_manifold_with_corners : smooth_manifold_with_corners 𝓘(ℂ) ℍ := upper_half_plane.open_embedding_coe.singleton_smooth_manifold_with_corners 𝓘(ℂ) local prefix `↑ₘ`:1024 := @coe _ (matrix (fin 2) (fin 2) _) _ local notation `GL(` n `, ` R `)`⁺ := matrix.GL_pos (fin n) R local notation `SL(` n `, ` R `)` := matrix.special_linear_group (fin n) R section modular_form open modular_form variables (F : Type*) (Γ : subgroup SL(2, ℤ)) (k : ℤ) local notation f `∣[`:73 k:0, A `]` :72 := slash_action.map ℂ k A f set_option old_structure_cmd true /--These are `slash_invariant_form`'s that are holomophic and bounded at infinity. -/ structure modular_form extends slash_invariant_form Γ k := (holo' : mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (to_fun : ℍ → ℂ)) (bdd_at_infty' : ∀ (A : SL(2, ℤ)), is_bounded_at_im_infty (to_fun ∣[k, A])) /-- The `slash_invariant_form` associated to a `modular_form`. -/ add_decl_doc modular_form.to_slash_invariant_form /--These are `slash_invariant_form`s that are holomophic and zero at infinity. -/ structure cusp_form extends slash_invariant_form Γ k := (holo' : mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (to_fun : ℍ → ℂ)) (zero_at_infty' : ∀ (A : SL(2, ℤ)), is_zero_at_im_infty (to_fun ∣[k, A])) /-- The `slash_invariant_form` associated to a `cusp_form`. -/ add_decl_doc cusp_form.to_slash_invariant_form /--`modular_form_class F Γ k` says that `F` is a type of bundled functions that extend `slash_invariant_forms_class` by requiring that the functions be holomorphic and bounded at infinity. -/ class modular_form_class extends slash_invariant_form_class F Γ k := (holo: ∀ f : F, mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (f : ℍ → ℂ)) (bdd_at_infty : ∀ (f : F) (A : SL(2, ℤ)), is_bounded_at_im_infty (f ∣[k, A])) /--`cusp_form_class F Γ k` says that `F` is a type of bundled functions that extend `slash_invariant_forms_class` by requiring that the functions be holomorphic and zero at infinity. -/ class cusp_form_class extends slash_invariant_form_class F Γ k := (holo: ∀ f : F, mdifferentiable 𝓘(ℂ) 𝓘(ℂ) (f : ℍ → ℂ)) (zero_at_infty : ∀ (f : F) (A : SL(2, ℤ)), is_zero_at_im_infty (f ∣[k, A])) @[priority 100] instance modular_form_class.modular_form : modular_form_class (modular_form Γ k) Γ k := { coe := modular_form.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', slash_action_eq := modular_form.slash_action_eq', holo:= modular_form.holo', bdd_at_infty := modular_form.bdd_at_infty' } @[priority 100] instance cusp_form_class.cusp_form : cusp_form_class (cusp_form Γ k) Γ k := { coe := cusp_form.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', slash_action_eq := cusp_form.slash_action_eq', holo:= cusp_form.holo', zero_at_infty := cusp_form.zero_at_infty' } variables {F Γ k} @[simp] lemma modular_form_to_fun_eq_coe {f : modular_form Γ k} : f.to_fun = (f : ℍ → ℂ) := rfl @[simp] lemma cusp_form_to_fun_eq_coe {f : cusp_form Γ k} : f.to_fun = (f : ℍ → ℂ) := rfl @[ext] theorem modular_form.ext {f g : modular_form Γ k} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h @[ext] theorem cusp_form.ext {f g : cusp_form Γ k} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h /-- Copy of a `modular_form` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def modular_form.copy (f : modular_form Γ k) (f' : ℍ → ℂ) (h : f' = ⇑f) : modular_form Γ k := { to_fun := f', slash_action_eq' := h.symm ▸ f.slash_action_eq', holo' := h.symm ▸ f.holo', bdd_at_infty' := λ A, h.symm ▸ f.bdd_at_infty' A } /-- Copy of a `cusp_form` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def cusp_form.copy (f : cusp_form Γ k) (f' : ℍ → ℂ) (h : f' = ⇑f) : cusp_form Γ k := { to_fun := f', slash_action_eq' := h.symm ▸ f.slash_action_eq', holo' := h.symm ▸ f.holo', zero_at_infty' := λ A, h.symm ▸ f.zero_at_infty' A } end modular_form namespace modular_form open slash_invariant_form variables {F : Type*} {Γ : subgroup SL(2, ℤ)} {k : ℤ} instance has_add : has_add (modular_form Γ k) := ⟨ λ f g, { holo' := f.holo'.add g.holo', bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).add (g.bdd_at_infty' A), .. (f : slash_invariant_form Γ k) + g }⟩ @[simp] lemma coe_add (f g : modular_form Γ k) : ⇑(f + g) = f + g := rfl @[simp] lemma add_apply (f g : modular_form Γ k) (z : ℍ) : (f + g) z = f z + g z := rfl instance has_zero : has_zero (modular_form Γ k) := ⟨ { holo' := (λ _, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ)), bdd_at_infty' := λ A, by simpa using zero_form_is_bounded_at_im_infty, .. (0 : slash_invariant_form Γ k) } ⟩ @[simp] lemma coe_zero : ⇑(0 : modular_form Γ k) = (0 : ℍ → ℂ) := rfl @[simp] lemma zero_apply (z : ℍ) : (0 : modular_form Γ k) z = 0 := rfl section variables {α : Type*} [has_smul α ℂ] [is_scalar_tower α ℂ ℂ] instance has_smul : has_smul α (modular_form Γ k) := ⟨ λ c f, { to_fun := c • f, holo' := by simpa using f.holo'.const_smul (c • (1 : ℂ)), bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).const_smul_left (c • (1 : ℂ)), .. c • (f : slash_invariant_form Γ k)}⟩ @[simp] lemma coe_smul (f : (modular_form Γ k)) (n : α) : ⇑(n • f) = n • f := rfl @[simp] lemma smul_apply (f : (modular_form Γ k)) (n : α) (z : ℍ) : (n • f) z = n • (f z) := rfl end instance has_neg : has_neg (modular_form Γ k) := ⟨λ f, { to_fun := -f, holo' := f.holo'.neg, bdd_at_infty':= λ A, by simpa using (f.bdd_at_infty' A).neg, .. -(f : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_neg (f : modular_form Γ k) : ⇑(-f) = -f := rfl @[simp] lemma neg_apply (f : modular_form Γ k) (z : ℍ) : (-f) z = - (f z) := rfl instance has_sub : has_sub (modular_form Γ k) := ⟨ λ f g, f + -g ⟩ @[simp] lemma coe_sub (f g : (modular_form Γ k)) : ⇑(f - g) = f - g := rfl @[simp] lemma sub_apply (f g : modular_form Γ k) (z : ℍ) : (f - g) z = f z - g z := rfl instance : add_comm_group (modular_form Γ k) := fun_like.coe_injective.add_comm_group _ rfl coe_add coe_neg coe_sub coe_smul coe_smul /--Additive coercion from `modular_form` to `ℍ → ℂ`. -/ @[simps] def coe_hom : (modular_form Γ k) →+ (ℍ → ℂ) := { to_fun := λ f, f, map_zero' := coe_zero, map_add' := λ _ _, rfl } instance : module ℂ (modular_form Γ k) := function.injective.module ℂ coe_hom fun_like.coe_injective (λ _ _, rfl) instance : inhabited (modular_form Γ k) := ⟨0⟩ /--The modular form of weight `k_1 + k_2` given by the product of two modular forms of weights `k_1` and `k_2`. -/ def mul {k_1 k_2 : ℤ} {Γ : subgroup SL(2, ℤ)} (f : (modular_form Γ k_1)) (g : (modular_form Γ k_2)) : (modular_form Γ (k_1 + k_2)) := { to_fun := f * g, slash_action_eq' := λ A, by simp_rw [mul_slash_subgroup, modular_form_class.slash_action_eq], holo' := f.holo'.mul g.holo', bdd_at_infty' := λ A, by simpa using (f.bdd_at_infty' A).mul (g.bdd_at_infty' A) } @[simp] lemma mul_coe {k_1 k_2 : ℤ} {Γ : subgroup SL(2, ℤ)} (f : (modular_form Γ k_1)) (g : (modular_form Γ k_2)) : ((f.mul g) : ℍ → ℂ) = f * g := rfl instance : has_one (modular_form Γ 0) := ⟨{ holo' := λ x, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ), bdd_at_infty' := λ A, by simpa using at_im_infty.const_bounded_at_filter (1:ℂ), .. (1 : slash_invariant_form Γ 0) }⟩ @[simp] lemma one_coe_eq_one : ((1 : modular_form Γ 0) : ℍ → ℂ) = 1 := rfl end modular_form namespace cusp_form open modular_form variables {F : Type*} {Γ : subgroup SL(2, ℤ)} {k : ℤ} instance has_add : has_add (cusp_form Γ k) := ⟨ λ f g, { to_fun := f + g, holo' := f.holo'.add g.holo', zero_at_infty' := λ A, by simpa using (f.zero_at_infty' A).add (g.zero_at_infty' A), .. (f : slash_invariant_form Γ k) + g }⟩ @[simp] lemma coe_add (f g : cusp_form Γ k) : ⇑(f + g) = f + g := rfl @[simp] lemma add_apply (f g : cusp_form Γ k) (z : ℍ) : (f + g) z = f z + g z := rfl instance has_zero : has_zero (cusp_form Γ k) := ⟨ { to_fun := 0, holo' := (λ _, mdifferentiable_at_const 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ)), zero_at_infty' := by simpa using filter.zero_zero_at_filter _, .. (0 : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_zero : ⇑(0 : cusp_form Γ k) = (0 : ℍ → ℂ) := rfl @[simp] lemma zero_apply (z : ℍ) : (0 : cusp_form Γ k) z = 0 := rfl section variables {α : Type*} [has_smul α ℂ] [is_scalar_tower α ℂ ℂ] instance has_smul : has_smul α (cusp_form Γ k) := ⟨ λ c f, { to_fun := c • f, holo' := by simpa using f.holo'.const_smul (c • (1 : ℂ)), zero_at_infty' := λ A, by simpa using (f.zero_at_infty' A).smul (c • (1 : ℂ)), .. c • (f : slash_invariant_form Γ k) }⟩ @[simp] lemma coe_smul (f : (cusp_form Γ k)) (n : α) : ⇑(n • f) = n • f := rfl @[simp] lemma smul_apply (f : (cusp_form Γ k)) (n : α) {z : ℍ} : (n • f) z = n • (f z) := rfl end instance has_neg : has_neg (cusp_form Γ k) := ⟨λ f, { to_fun := -f, holo' := f.holo'.neg, zero_at_infty':= λ A, by simpa using (f.zero_at_infty' A).neg, .. -(f : slash_invariant_form Γ k)} ⟩ @[simp] lemma coe_neg (f : cusp_form Γ k) : ⇑(-f) = -f := rfl @[simp] lemma neg_apply (f : cusp_form Γ k) (z : ℍ) : (-f) z = -(f z) := rfl instance has_sub : has_sub (cusp_form Γ k) := ⟨ λ f g, f + -g ⟩ @[simp] lemma coe_sub (f g : cusp_form Γ k) : ⇑(f - g) = f - g := rfl @[simp] lemma sub_apply (f g : cusp_form Γ k) (z : ℍ) : (f - g) z = f z - g z := rfl instance : add_comm_group (cusp_form Γ k) := fun_like.coe_injective.add_comm_group _ rfl coe_add coe_neg coe_sub coe_smul coe_smul /--Additive coercion from `cusp_form` to `ℍ → ℂ`. -/ @[simps] def coe_hom : (cusp_form Γ k) →+ (ℍ → ℂ) := { to_fun := λ f, f, map_zero' := cusp_form.coe_zero, map_add' := λ _ _, rfl } instance : module ℂ (cusp_form Γ k) := function.injective.module ℂ coe_hom fun_like.coe_injective (λ _ _, rfl) instance : inhabited (cusp_form Γ k) := ⟨0⟩ @[priority 99] instance [cusp_form_class F Γ k] : modular_form_class F Γ k := { coe := fun_like.coe, coe_injective' := fun_like.coe_injective', slash_action_eq := cusp_form_class.slash_action_eq, holo:= cusp_form_class.holo, bdd_at_infty := λ _ _, (cusp_form_class.zero_at_infty _ _).bounded_at_filter} end cusp_form
c2e971b0727ce6486a4e0826b8f6a97c75482830
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/propositions/eq.lean
46b8e9fbd4a9c599c6be4978993811af2d5115e1
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
1,948
lean
/- Eq -/ #check @eq -- binary relation : α → α → α #print eq /- inductive eq : Π {α : Sort u}, α → α → Prop constructors: eq.refl : ∀ {α : Sort u} (a : α), a = a -/ /- Properties -/ #print eq /- -- reflexive -- constructor -- symmetric <- reflexive -- transitive <- reflexive -- equivalence <- resymtr -/ /- AXIOMS -/ /- refl: Every thing equals itself -/ #check @eq.refl /- eq.refl: from any a, proof of a = a -/ -- the only eq constructor -- the one introduction rule -- the one elimination rule -- substitution of equals /- subst C : α → Prop / \ / \ C a >a=b> C b -/ #check @eq.rec -- What does this type say? /- eq.rec : Π {α : Sort u_2} {a : α} {C : α → Sort u_1}, C a → Π {ᾰ : α}, a = ᾰ → C ᾰ -/ /- For any type of object, α, for any a of this type, and for any property, C α, of objects of type α, If you know or assume that a has property C then for any other object, ᾰ, of type α if you know or assume that a and ᾰ are equal then you may conclude that ᾰ also has that proeprty, C. So, if you need to prove C b it suffices to show C a and a = b. Subst uses an equality a = b in order to rewrite a term C a to C b. -/ /- Prove that symmetry and transitivty are now theorems -/ #check @eq.symm example : ∀ {α : Type } {a b : α}, a = b ↔ b = a := begin assume α a b, split, assume h, rw h, assume h, rw h, end example : ∀ {α : Type } {a b : α}, a = b ↔ b = a := λ α a b, (iff.intro (λ aeqb, ( let s := eq.symm aeqb in eq.subst aeqb (eq.refl a) ) ) (_) -- exercise! ) #check @eq.trans -- exercise -- tactic script example : ∀ {α : Type } {a b c : α}, a = b → b = c → a = c := _ -- Lean term example : ∀ {α : Type } {a b c : α}, a = b → b = c → a = c := _
2b50d7312150b7c81cfbc983b6ef776e64fbaae8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/minimal.lean
b522b8e1675cb43b0fb391cbf2d44e8cf780b85a
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
5,089
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.antichain /-! # Minimal/maximal elements of a set This file defines minimal and maximal of a set with respect to an arbitrary relation. ## Main declarations * `maximals r s`: Maximal elements of `s` with respect to `r`. * `minimals r s`: Minimal elements of `s` with respect to `r`. ## TODO Do we need a `finset` version? -/ open function set variables {α : Type*} (r r₁ r₂ : α → α → Prop) (s t : set α) (a : α) /-- Turns a set into an antichain by keeping only the "maximal" elements. -/ def maximals : set α := {a ∈ s | ∀ ⦃b⦄, b ∈ s → r a b → a = b} /-- Turns a set into an antichain by keeping only the "maximal" elements. -/ def minimals : set α := {a ∈ s | ∀ ⦃b⦄, b ∈ s → r b a → a = b} lemma maximals_subset : maximals r s ⊆ s := sep_subset _ _ lemma minimals_subset : minimals r s ⊆ s := sep_subset _ _ @[simp] lemma maximals_empty : maximals r ∅ = ∅ := sep_empty _ @[simp] lemma minimals_empty : minimals r ∅ = ∅ := sep_empty _ @[simp] lemma maximals_singleton : maximals r {a} = {a} := (maximals_subset _ _).antisymm $ singleton_subset_iff.2 $ ⟨rfl, λ b hb _, hb.symm⟩ @[simp] lemma minimals_singleton : minimals r {a} = {a} := maximals_singleton _ _ lemma maximals_swap : maximals (swap r) s = minimals r s := rfl lemma minimals_swap : minimals (swap r) s = maximals r s := rfl lemma maximals_antichain : is_antichain r (maximals r s) := λ a ha b hb hab h, hab $ ha.2 hb.1 h lemma minimals_antichain : is_antichain r (minimals r s) := (maximals_antichain _ _).swap lemma maximals_eq_minimals [is_symm α r] : maximals r s = minimals r s := by { congr, ext a b, exact comm } variables {r r₁ r₂ s t a} lemma set.subsingleton.maximals_eq (h : s.subsingleton) : maximals r s = s := h.induction_on (minimals_empty _) (maximals_singleton _) lemma set.subsingleton.minimals_eq (h : s.subsingleton) : minimals r s = s := h.maximals_eq lemma maximals_mono (h : ∀ a b, r₁ a b → r₂ a b) : maximals r₂ s ⊆ maximals r₁ s := λ a ha, ⟨ha.1, λ b hb, ha.2 hb ∘ h _ _⟩ lemma minimals_mono (h : ∀ a b, r₁ a b → r₂ a b) : minimals r₂ s ⊆ minimals r₁ s := λ a ha, ⟨ha.1, λ b hb, ha.2 hb ∘ h _ _⟩ lemma maximals_union : maximals r (s ∪ t) ⊆ maximals r s ∪ maximals r t := begin intros a ha, obtain h | h := ha.1, { exact or.inl ⟨h, λ b hb, ha.2 $ or.inl hb⟩ }, { exact or.inr ⟨h, λ b hb, ha.2 $ or.inr hb⟩ } end lemma minimals_union : minimals r (s ∪ t) ⊆ minimals r s ∪ minimals r t := maximals_union lemma maximals_inter_subset : maximals r s ∩ t ⊆ maximals r (s ∩ t) := λ a ha, ⟨⟨ha.1.1, ha.2⟩, λ b hb, ha.1.2 hb.1⟩ lemma minimals_inter_subset : minimals r s ∩ t ⊆ minimals r (s ∩ t) := maximals_inter_subset lemma inter_maximals_subset : s ∩ maximals r t ⊆ maximals r (s ∩ t) := λ a ha, ⟨⟨ha.1, ha.2.1⟩, λ b hb, ha.2.2 hb.2⟩ lemma inter_minimals_subset : s ∩ minimals r t ⊆ minimals r (s ∩ t) := inter_maximals_subset lemma _root_.is_antichain.maximals_eq (h : is_antichain r s) : maximals r s = s := (maximals_subset _ _).antisymm $ λ a ha, ⟨ha, λ b, h.eq ha⟩ lemma _root_.is_antichain.minimals_eq (h : is_antichain r s) : minimals r s = s := (minimals_subset _ _).antisymm $ λ a ha, ⟨ha, λ b, h.eq' ha⟩ @[simp] lemma maximals_idem : maximals r (maximals r s) = maximals r s := (maximals_antichain _ _).maximals_eq @[simp] lemma minimals_idem : minimals r (minimals r s) = minimals r s := maximals_idem /-- If `maximals r s` is included in but *shadows* the antichain `t`, then it is actually equal to `t`. -/ lemma is_antichain.max_maximals (ht : is_antichain r t) (h : maximals r s ⊆ t) (hs : ∀ ⦃a⦄, a ∈ t → ∃ b ∈ maximals r s, r b a) : maximals r s = t := begin refine h.antisymm (λ a ha, _), obtain ⟨b, hb, hr⟩ := hs ha, rwa of_not_not (λ hab, ht (h hb) ha (ne.symm hab) hr), end /-- If `minimals r s` is included in but *shadows* the antichain `t`, then it is actually equal to `t`. -/ lemma is_antichain.max_minimals (ht : is_antichain r t) (h : minimals r s ⊆ t) (hs : ∀ ⦃a⦄, a ∈ t → ∃ b ∈ minimals r s, r a b) : minimals r s = t := begin refine h.antisymm (λ a ha, _), obtain ⟨b, hb, hr⟩ := hs ha, rwa of_not_not (λ hab, ht ha (h hb) hab hr), end variables [partial_order α] lemma is_least.mem_minimals (h : is_least s a) : a ∈ minimals (≤) s := ⟨h.1, λ b hb, (h.2 hb).antisymm⟩ lemma is_greatest.mem_maximals (h : is_greatest s a) : a ∈ maximals (≤) s := ⟨h.1, λ b hb, (h.2 hb).antisymm'⟩ lemma is_least.minimals_eq (h : is_least s a) : minimals (≤) s = {a} := eq_singleton_iff_unique_mem.2 ⟨h.mem_minimals, λ b hb, hb.2 h.1 $ h.2 hb.1⟩ lemma is_greatest.maximals_eq (h : is_greatest s a) : maximals (≤) s = {a} := eq_singleton_iff_unique_mem.2 ⟨h.mem_maximals, λ b hb, hb.2 h.1 $ h.2 hb.1⟩
f7ef95abf588bb6026477a6c3f446a897cf4eca1
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpPrio.lean
373e727a72280177f95715f7a7ee08ba5b51b17d
[ "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
181
lean
opaque n : Nat @[simp] axiom prio_1000 : n = 1000 @[simp 10] axiom prio_10 : n = 10 -- simp should prefer the prio_1000 lemma with the higher priority example : n = 1000 := by simp
f4cb2ac59c072144aa73b83d9999b9692d2dcf40
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/measure_theory/function/strongly_measurable.lean
fd32273af0730e0b422e17b23b8543ceadae7718
[ "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
19,005
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 measure_theory.function.simple_func_dense /-! # Strongly measurable and finitely strongly measurable functions A function `f` is said to be strongly measurable if `f` is the sequential limit of simple functions. It is said to be finitely strongly measurable with respect to a measure `μ` if the supports of those simple functions have finite measure. If the target space has a second countable topology, strongly measurable and measurable are equivalent. Functions in `Lp` for `0 < p < ∞` are finitely strongly measurable. If the measure is sigma-finite, strongly measurable and finitely strongly measurable are equivalent. The main property of finitely strongly measurable functions is `fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that the function is supported on `t` and `μ.restrict t` is sigma-finite. As a consequence, we can prove some results for those functions as if the measure was sigma-finite. ## Main definitions * `strongly_measurable f`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β`. * `fin_strongly_measurable f μ`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β` such that for all `n ∈ ℕ`, the measure of the support of `fs n` is finite. * `ae_fin_strongly_measurable f μ`: `f` is almost everywhere equal to a `fin_strongly_measurable` function. * `ae_fin_strongly_measurable.sigma_finite_set`: a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. ## Main statements * `ae_fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. * `mem_ℒp.ae_fin_strongly_measurable`: if `mem_ℒp f p μ` with `0 < p < ∞`, then `ae_fin_strongly_measurable f μ`. * `Lp.fin_strongly_measurable`: for `0 < p < ∞`, `Lp` functions are finitely strongly measurable. ## References * Hytönen, Tuomas, Jan Van Neerven, Mark Veraar, and Lutz Weis. Analysis in Banach spaces. Springer, 2016. -/ open measure_theory filter topological_space function open_locale ennreal topological_space measure_theory namespace measure_theory local infixr ` →ₛ `:25 := simple_func section definitions variables {α β : Type*} [topological_space β] /-- A function is `strongly_measurable` if it is the limit of simple functions. -/ def strongly_measurable [measurable_space α] (f : α → β) : Prop := ∃ fs : ℕ → α →ₛ β, ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) /-- A function is `fin_strongly_measurable` with respect to a measure if it is the limit of simple functions with support with finite measure. -/ def fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ fs : ℕ → α →ₛ β, (∀ n, μ (support (fs n)) < ∞) ∧ (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) /-- A function is `ae_fin_strongly_measurable` with respect to a measure if it is almost everywhere equal to the limit of a sequence of simple functions with support with finite measure. -/ def ae_fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, fin_strongly_measurable g μ ∧ f =ᵐ[μ] g end definitions /-! ## Strongly measurable functions -/ lemma subsingleton.strongly_measurable {α β} [measurable_space α] [topological_space β] [subsingleton β] (f : α → β) : strongly_measurable f := begin let f_sf : α →ₛ β := ⟨f, λ x, _, set.subsingleton.finite set.subsingleton_of_subsingleton⟩, { exact ⟨λ n, f_sf, λ x, tendsto_const_nhds⟩, }, { have h_univ : f ⁻¹' {x} = set.univ, by { ext1 y, simp, }, rw h_univ, exact measurable_set.univ, }, end namespace strongly_measurable variables {α β : Type*} {f g : α → β} /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))`. That property is given by `strongly_measurable.tendsto_approx`. -/ protected noncomputable def approx [measurable_space α] [topological_space β] (hf : strongly_measurable f) : ℕ → α →ₛ β := hf.some protected lemma tendsto_approx [measurable_space α] [topological_space β] (hf : strongly_measurable f) : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec lemma fin_strongly_measurable_of_set_sigma_finite [topological_space β] [has_zero β] {m : measurable_space α} {μ : measure α} (hf_meas : strongly_measurable f) {t : set α} (ht : measurable_set t) (hft_zero : ∀ x ∈ tᶜ, f x = 0) (htμ : sigma_finite (μ.restrict t)) : fin_strongly_measurable f μ := begin haveI : sigma_finite (μ.restrict t) := htμ, let S := spanning_sets (μ.restrict t), have hS_meas : ∀ n, measurable_set (S n), from measurable_spanning_sets (μ.restrict t), let f_approx := hf_meas.approx, let fs := λ n, simple_func.restrict (f_approx n) (S n ∩ t), have h_fs_t_compl : ∀ n, ∀ x ∉ t, fs n x = 0, { intros n x hxt, rw simple_func.restrict_apply _ ((hS_meas n).inter ht), refine set.indicator_of_not_mem _ _, simp [hxt], }, refine ⟨fs, _, λ x, _⟩, { simp_rw simple_func.support_eq, refine λ n, (measure_bUnion_finset_le _ _).trans_lt _, refine ennreal.sum_lt_top_iff.mpr (λ y hy, _), rw simple_func.restrict_preimage_singleton _ ((hS_meas n).inter ht), swap, { rw finset.mem_filter at hy, exact hy.2, }, refine (measure_mono (set.inter_subset_left _ _)).trans_lt _, have h_lt_top := measure_spanning_sets_lt_top (μ.restrict t) n, rwa measure.restrict_apply' ht at h_lt_top, }, { by_cases hxt : x ∈ t, swap, { rw [funext (λ n, h_fs_t_compl n x hxt), hft_zero x hxt], exact tendsto_const_nhds, }, have h : tendsto (λ n, (f_approx n) x) at_top (𝓝 (f x)), from hf_meas.tendsto_approx x, obtain ⟨n₁, hn₁⟩ : ∃ n, ∀ m, n ≤ m → fs m x = f_approx m x, { obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m ∩ t, { suffices : ∃ n, ∀ m, n ≤ m → x ∈ S m, { obtain ⟨n, hn⟩ := this, exact ⟨n, λ m hnm, set.mem_inter (hn m hnm) hxt⟩, }, suffices : ∃ n, x ∈ S n, { rcases this with ⟨n, hn⟩, exact ⟨n, λ m hnm, monotone_spanning_sets (μ.restrict t) hnm hn⟩, }, rw [← set.mem_Union, Union_spanning_sets (μ.restrict t)], trivial, }, refine ⟨n, λ m hnm, _⟩, simp_rw [fs, simple_func.restrict_apply _ ((hS_meas m).inter ht), set.indicator_of_mem (hn m hnm)], }, rw tendsto_at_top' at h ⊢, intros s hs, obtain ⟨n₂, hn₂⟩ := h s hs, refine ⟨max n₁ n₂, λ m hm, _⟩, rw hn₁ m ((le_max_left _ _).trans hm.le), exact hn₂ m ((le_max_right _ _).trans hm.le), }, end /-- If the measure is sigma-finite, all strongly measurable functions are `fin_strongly_measurable`. -/ protected lemma fin_strongly_measurable [topological_space β] [has_zero β] {m0 : measurable_space α} (hf : strongly_measurable f) (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ := hf.fin_strongly_measurable_of_set_sigma_finite measurable_set.univ (by simp) (by rwa measure.restrict_univ) /-- A strongly measurable function is measurable. -/ protected lemma measurable [measurable_space α] [metric_space β] [measurable_space β] [borel_space β] (hf : strongly_measurable f) : measurable f := measurable_of_tendsto_metric (λ n, (hf.approx n).measurable) (tendsto_pi.mpr hf.tendsto_approx) section arithmetic variables [measurable_space α] [topological_space β] protected lemma add [has_add β] [has_continuous_add β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f + g) := ⟨λ n, hf.approx n + hg.approx n, λ x, (hf.tendsto_approx x).add (hg.tendsto_approx x)⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : strongly_measurable f) : strongly_measurable (-f) := ⟨λ n, - hf.approx n, λ x, (hf.tendsto_approx x).neg⟩ protected lemma sub [has_sub β] [has_continuous_sub β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f - g) := ⟨λ n, hf.approx n - hg.approx n, λ x, (hf.tendsto_approx x).sub (hg.tendsto_approx x)⟩ end arithmetic end strongly_measurable section second_countable_strongly_measurable variables {α β : Type*} [measurable_space α] [measurable_space β] {f : α → β} /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.measurable.strongly_measurable [emetric_space β] [opens_measurable_space β] [second_countable_topology β] (hf : measurable f) : strongly_measurable f := begin rcases is_empty_or_nonempty β; resetI, { exact subsingleton.strongly_measurable f, }, { inhabit β, exact ⟨simple_func.approx_on f hf set.univ (default β) (set.mem_univ _), λ x, simple_func.tendsto_approx_on hf (set.mem_univ _) (by simp)⟩, }, end /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma strongly_measurable_iff_measurable [metric_space β] [borel_space β] [second_countable_topology β] : strongly_measurable f ↔ measurable f := ⟨λ h, h.measurable, λ h, measurable.strongly_measurable h⟩ end second_countable_strongly_measurable /-! ## Finitely strongly measurable functions -/ namespace fin_strongly_measurable variables {α β : Type*} [has_zero β] {m0 : measurable_space α} {μ : measure α} {f : α → β} lemma ae_fin_strongly_measurable [topological_space β] (hf : fin_strongly_measurable f μ) : ae_fin_strongly_measurable f μ := ⟨f, hf, ae_eq_refl f⟩ section sequence variables [topological_space β] (hf : fin_strongly_measurable f μ) /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))` and `∀ n, μ (support (hf.approx n)) < ∞`. These properties are given by `fin_strongly_measurable.tendsto_approx` and `fin_strongly_measurable.fin_support_approx`. -/ protected noncomputable def approx : ℕ → α →ₛ β := hf.some protected lemma fin_support_approx : ∀ n, μ (support (hf.approx n)) < ∞ := hf.some_spec.1 protected lemma tendsto_approx : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec.2 end sequence protected lemma strongly_measurable [topological_space β] (hf : fin_strongly_measurable f μ) : strongly_measurable f := ⟨hf.approx, hf.tendsto_approx⟩ lemma exists_set_sigma_finite [topological_space β] [t2_space β] (hf : fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨fs, hT_lt_top, h_approx⟩, let T := λ n, support (fs n), have hT_meas : ∀ n, measurable_set (T n), from λ n, simple_func.measurable_set_support (fs n), let t := ⋃ n, T n, refine ⟨t, measurable_set.Union hT_meas, _, _⟩, { have h_fs_zero : ∀ n, ∀ x ∈ tᶜ, fs n x = 0, { intros n x hxt, rw [set.mem_compl_iff, set.mem_Union, not_exists] at hxt, simpa using (hxt n), }, refine λ x hxt, tendsto_nhds_unique (h_approx x) _, rw funext (λ n, h_fs_zero n x hxt), exact tendsto_const_nhds, }, { refine measure.finite_spanning_sets_in.sigma_finite _ _, { exact set.range (λ n, tᶜ ∪ T n), }, { refine ⟨λ n, tᶜ ∪ T n, λ n, set.mem_range_self _, λ n, _, _⟩, { rw [measure.restrict_apply' (measurable_set.Union hT_meas), set.union_inter_distrib_right, set.compl_inter_self t, set.empty_union], exact (measure_mono (set.inter_subset_left _ _)).trans_lt (hT_lt_top n), }, rw ← set.union_Union tᶜ T, exact set.compl_union_self _, }, { intros s hs, rw set.mem_range at hs, cases hs with n hsn, rw ← hsn, exact (measurable_set.compl (measurable_set.Union hT_meas)).union (hT_meas n), }, }, end /-- A finitely strongly measurable function is measurable. -/ protected lemma measurable [metric_space β] [measurable_space β] [borel_space β] (hf : fin_strongly_measurable f μ) : measurable f := measurable_of_tendsto_metric (λ n, (hf.some n).measurable) (tendsto_pi.mpr hf.some_spec.2) protected lemma add {β} [topological_space β] [add_monoid β] [has_continuous_add β] {f g : α → β} (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f + g) μ := ⟨λ n, hf.approx n + hg.approx n, λ n, (measure_mono (function.support_add _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).add (hg.tendsto_approx x)⟩ protected lemma neg {β} [topological_space β] [add_group β] [topological_add_group β] {f : α → β} (hf : fin_strongly_measurable f μ) : fin_strongly_measurable (-f) μ := begin refine ⟨λ n, -hf.approx n, λ n, _, λ x, (hf.tendsto_approx x).neg⟩, suffices : μ (function.support (λ x, - (hf.approx n) x)) < ∞, by convert this, rw function.support_neg (hf.approx n), exact hf.fin_support_approx n, end protected lemma sub {β} [topological_space β] [add_group β] [has_continuous_sub β] {f g : α → β} (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f - g) μ := ⟨λ n, hf.approx n - hg.approx n, λ n, (measure_mono (function.support_sub _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).sub (hg.tendsto_approx x)⟩ end fin_strongly_measurable lemma fin_strongly_measurable_iff_strongly_measurable_and_exists_set_sigma_finite {α β} {f : α → β} [topological_space β] [t2_space β] [has_zero β] {m : measurable_space α} {μ : measure α} : fin_strongly_measurable f μ ↔ (strongly_measurable f ∧ (∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t))) := ⟨λ hf, ⟨hf.strongly_measurable, hf.exists_set_sigma_finite⟩, λ hf, hf.1.fin_strongly_measurable_of_set_sigma_finite hf.2.some_spec.1 hf.2.some_spec.2.1 hf.2.some_spec.2.2⟩ namespace ae_fin_strongly_measurable variables {α β : Type*} {m : measurable_space α} {μ : measure α} [topological_space β] {f g : α → β} protected lemma add [add_monoid β] [has_continuous_add β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f + g) μ := ⟨hf.some + hg.some, hf.some_spec.1.add hg.some_spec.1, hf.some_spec.2.add hg.some_spec.2⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : ae_fin_strongly_measurable f μ) : ae_fin_strongly_measurable (-f) μ := ⟨-hf.some, hf.some_spec.1.neg, hf.some_spec.2.neg⟩ protected lemma sub [add_group β] [has_continuous_sub β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f - g) μ := ⟨hf.some - hg.some, hf.some_spec.1.sub hg.some_spec.1, hf.some_spec.2.sub hg.some_spec.2⟩ variables [has_zero β] [t2_space β] lemma exists_set_sigma_finite (hf : ae_fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ f =ᵐ[μ.restrict tᶜ] 0 ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨g, hg, hfg⟩, obtain ⟨t, ht, hgt_zero, htμ⟩ := hg.exists_set_sigma_finite, refine ⟨t, ht, _, htμ⟩, refine eventually_eq.trans (ae_restrict_of_ae hfg) _, rw [eventually_eq, ae_restrict_iff' ht.compl], exact eventually_of_forall hgt_zero, end /-- A measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `sigma_finite (μ.restrict t)`. -/ def sigma_finite_set (hf : ae_fin_strongly_measurable f μ) : set α := hf.exists_set_sigma_finite.some protected lemma measurable_set (hf : ae_fin_strongly_measurable f μ) : measurable_set hf.sigma_finite_set := hf.exists_set_sigma_finite.some_spec.1 lemma ae_eq_zero_compl (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ.restrict hf.sigma_finite_setᶜ] 0 := hf.exists_set_sigma_finite.some_spec.2.1 instance sigma_finite_restrict (hf : ae_fin_strongly_measurable f μ) : sigma_finite (μ.restrict hf.sigma_finite_set) := hf.exists_set_sigma_finite.some_spec.2.2 end ae_fin_strongly_measurable variables {α G : Type*} {p : ℝ≥0∞} {m m0 : measurable_space α} {μ : measure α} [normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G] {f : α → G} /-- In a space with second countable topology and a sigma-finite measure, `fin_strongly_measurable` and `measurable` are equivalent. -/ lemma fin_strongly_measurable_iff_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ ↔ measurable f := ⟨λ h, h.measurable, λ h, (measurable.strongly_measurable h).fin_strongly_measurable μ⟩ /-- In a space with second countable topology and a sigma-finite measure, `ae_fin_strongly_measurable` and `ae_measurable` are equivalent. -/ lemma ae_fin_strongly_measurable_iff_ae_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : ae_fin_strongly_measurable f μ ↔ ae_measurable f μ := by simp_rw [ae_fin_strongly_measurable, ae_measurable, fin_strongly_measurable_iff_measurable] lemma mem_ℒp.fin_strongly_measurable_of_measurable (hf : mem_ℒp f p μ) (hf_meas : measurable f) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : fin_strongly_measurable f μ := begin let fs := simple_func.approx_on f hf_meas set.univ 0 (set.mem_univ _), refine ⟨fs, _, _⟩, { have h_fs_Lp : ∀ n, mem_ℒp (fs n) p μ, from simple_func.mem_ℒp_approx_on_univ hf_meas hf, exact λ n, (fs n).measure_support_lt_top_of_mem_ℒp (h_fs_Lp n) hp_ne_zero hp_ne_top, }, { exact λ x, simple_func.tendsto_approx_on hf_meas (set.mem_univ 0) (by simp), }, end lemma mem_ℒp.ae_fin_strongly_measurable (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : ae_fin_strongly_measurable f μ := ⟨hf.ae_measurable.mk f, ((mem_ℒp_congr_ae hf.ae_measurable.ae_eq_mk).mp hf).fin_strongly_measurable_of_measurable hf.ae_measurable.measurable_mk hp_ne_zero hp_ne_top, hf.ae_measurable.ae_eq_mk⟩ lemma integrable.ae_fin_strongly_measurable (hf : integrable f μ) : ae_fin_strongly_measurable f μ := (mem_ℒp_one_iff_integrable.mpr hf).ae_fin_strongly_measurable one_ne_zero ennreal.coe_ne_top lemma Lp.fin_strongly_measurable (f : Lp G p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : fin_strongly_measurable f μ := (Lp.mem_ℒp f).fin_strongly_measurable_of_measurable (Lp.measurable f) hp_ne_zero hp_ne_top end measure_theory
ca5cfe4b6d3a29f169262e1098d1bc1fb42c88ac
680b0d1592ce164979dab866b232f6fa743f2cc8
/tests/lean/626a.lean
475ea9fdfedd1305d1efcbe9d5818badc4ff54da
[ "Apache-2.0" ]
permissive
syohex/lean
657428ab520f8277fc18cf04bea2ad200dbae782
081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc
refs/heads/master
1,611,274,838,635
1,452,668,188,000
1,452,668,188,000
49,562,028
0
0
null
1,452,675,604,000
1,452,675,602,000
null
UTF-8
Lean
false
false
187
lean
namespace foo definition C¹ := nat definition foo (c : C¹) := nat.rec_on c _ _ end foo namespace boo notation `C¹` := nat definition foo (c : C¹) := nat.rec_on c _ _ end boo
27af89d2f5f6c9145251115cc26074bdd776ca5d
fd71e7836e9a5d14548f6d3291653cbb6c608dbe
/src/test/slim_check/arbitrary.lean
552f7a8013895952d93260b377fcb2f6cbcaa4a9
[]
no_license
cipher1024/slim_check
382c80a98aed105a97eeac84f590a06dca4d8c7d
5969b7f72e01fdd46f2502ed0cbf69c0699061d4
refs/heads/master
1,625,514,334,828
1,574,118,572,000
1,574,118,572,000
101,538,155
2
1
null
null
null
null
UTF-8
Lean
false
false
641
lean
import test.slim_check.gen universes u namespace slim_check variables (α : Type u) class arbitrary := (arby : gen α) export arbitrary (arby) open nat instance arbitrary_nat : arbitrary ℕ := ⟨ sized $ λ sz, fin.val <$> choose_any (fin $ succ sz) ⟩ instance arbitrary_int : arbitrary ℤ := ⟨ sized $ λ sz, (λ n : fin _, n.val - int.of_nat sz ) <$> choose_any (fin $ succ $ 2 * sz) ⟩ variables {α} variable [arbitrary α] instance arbitrary_list : arbitrary (list α) := ⟨ list_of (arby α) ⟩ instance arbitrary_prop : arbitrary Prop := { arby := do x ← choose_any bool, return $ ↑x } end slim_check
c6b71c4eb3f5b63e1db308fc9c06b8a5c47e4787
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/function/lp_order.lean
1c2719ccf8fe5be5d35a3510fbbbce1fc7ba5aca
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
3,465
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 measure_theory.function.lp_space import analysis.normed_space.lattice_ordered_group /-! # Order related properties of Lp spaces ### Results - `Lp E p μ` is an `ordered_add_comm_group` when `E` is a `normed_lattice_add_comm_group`. ### TODO - move definitions of `Lp.pos_part` and `Lp.neg_part` to this file, and define them as `has_pos_part.pos` and `has_pos_part.neg` given by the lattice structure. -/ open topological_space measure_theory lattice_ordered_comm_group open_locale ennreal variables {α E : Type*} {m : measurable_space α} {μ : measure α} {p : ℝ≥0∞} namespace measure_theory namespace Lp section order variables [normed_lattice_add_comm_group E] lemma coe_fn_le (f g : Lp E p μ) : f ≤ᵐ[μ] g ↔ f ≤ g := by rw [← subtype.coe_le_coe, ← ae_eq_fun.coe_fn_le, ← coe_fn_coe_base, ← coe_fn_coe_base] lemma coe_fn_nonneg (f : Lp E p μ) : 0 ≤ᵐ[μ] f ↔ 0 ≤ f := begin rw ← coe_fn_le, have h0 := Lp.coe_fn_zero E p μ, split; intro h; filter_upwards [h, h0] with _ _ h2, { rwa h2, }, { rwa ← h2, }, end instance : covariant_class (Lp E p μ) (Lp E p μ) (+) (≤) := begin refine ⟨λ f g₁ g₂ hg₁₂, _⟩, rw ← coe_fn_le at hg₁₂ ⊢, filter_upwards [coe_fn_add f g₁, coe_fn_add f g₂, hg₁₂] with _ h1 h2 h3, rw [h1, h2, pi.add_apply, pi.add_apply], exact add_le_add le_rfl h3, end instance : ordered_add_comm_group (Lp E p μ) := { add_le_add_left := λ f g, add_le_add_left, ..subtype.partial_order _, ..add_subgroup.to_add_comm_group _} lemma _root_.measure_theory.mem_ℒp.sup {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f ⊔ g) p μ := mem_ℒp.mono' (hf.norm.add hg.norm) (hf.1.sup hg.1) (filter.eventually_of_forall (λ x, norm_sup_le_add (f x) (g x))) lemma _root_.measure_theory.mem_ℒp.inf {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f ⊓ g) p μ := mem_ℒp.mono' (hf.norm.add hg.norm) (hf.1.inf hg.1) (filter.eventually_of_forall (λ x, norm_inf_le_add (f x) (g x))) lemma _root_.measure_theory.mem_ℒp.abs {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (|f|) p μ := hf.sup hf.neg instance : lattice (Lp E p μ) := subtype.lattice (λ f g hf hg, by { rw mem_Lp_iff_mem_ℒp at *, exact (mem_ℒp_congr_ae (ae_eq_fun.coe_fn_sup _ _)).mpr (hf.sup hg), }) (λ f g hf hg, by { rw mem_Lp_iff_mem_ℒp at *, exact (mem_ℒp_congr_ae (ae_eq_fun.coe_fn_inf _ _)).mpr (hf.inf hg),}) lemma coe_fn_sup (f g : Lp E p μ) : ⇑(f ⊔ g) =ᵐ[μ] ⇑f ⊔ ⇑g := ae_eq_fun.coe_fn_sup _ _ lemma coe_fn_inf (f g : Lp E p μ) : ⇑(f ⊓ g) =ᵐ[μ] ⇑f ⊓ ⇑g := ae_eq_fun.coe_fn_inf _ _ lemma coe_fn_abs (f : Lp E p μ) : ⇑|f| =ᵐ[μ] λ x, |f x| := ae_eq_fun.coe_fn_abs _ noncomputable instance [fact (1 ≤ p)] : normed_lattice_add_comm_group (Lp E p μ) := { add_le_add_left := λ f g, add_le_add_left, solid := λ f g hfg, begin rw ← coe_fn_le at hfg, simp_rw [Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top f) (Lp.snorm_ne_top g)], refine snorm_mono_ae _, filter_upwards [hfg, Lp.coe_fn_abs f, Lp.coe_fn_abs g] with x hx hxf hxg, rw [hxf, hxg] at hx, exact solid hx, end, ..Lp.lattice, ..Lp.normed_add_comm_group, } end order end Lp end measure_theory
5b64a187f0d314ecd3fc66480e9ffeaff96ff947
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/ResolveName.lean
44645f5a55f91e09389bcdb3ec1aa5f991aeaef0
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
14,416
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Data.OpenDecl import Lean.Hygiene import Lean.Modifiers import Lean.Exception namespace Lean /-! We use aliases to implement the `export <id> (<id>+)` command. An `export A (x)` in the namespace `B` produces an alias `B.x ~> A.x`. -/ abbrev AliasState := SMap Name (List Name) abbrev AliasEntry := Name × Name def addAliasEntry (s : AliasState) (e : AliasEntry) : AliasState := match s.find? e.1 with | none => s.insert e.1 [e.2] | some es => if es.elem e.2 then s else s.insert e.1 (e.2 :: es) builtin_initialize aliasExtension : SimplePersistentEnvExtension AliasEntry AliasState ← registerSimplePersistentEnvExtension { name := `aliasesExt, addEntryFn := addAliasEntry, addImportedFn := fun es => mkStateFromImportedEntries addAliasEntry {} es |>.switch } /-- Add alias `a` for `e` -/ @[export lean_add_alias] def addAlias (env : Environment) (a : Name) (e : Name) : Environment := aliasExtension.addEntry env (a, e) def getAliasState (env : Environment) : AliasState := aliasExtension.getState env /-- Retrieve aliases for `a`. If `skipProtected` is `true`, then the resulting list only includes declarations that are not marked as `proctected`. -/ def getAliases (env : Environment) (a : Name) (skipProtected : Bool) : List Name := match aliasExtension.getState env |>.find? a with | none => [] | some es => if skipProtected then es.filter (!isProtected env ·) else es -- slower, but only used in the pretty printer def getRevAliases (env : Environment) (e : Name) : List Name := (aliasExtension.getState env).fold (fun as a es => if List.contains es e then a :: as else as) [] /-! # Global name resolution -/ namespace ResolveName /-- Check whether `ns ++ id` is a valid namepace name and/or there are aliases names `ns ++ id`. -/ private def resolveQualifiedName (env : Environment) (ns : Name) (id : Name) : List Name := let resolvedId := ns ++ id -- We ignore protected aliases if `id` is atomic. let resolvedIds := getAliases env resolvedId (skipProtected := id.isAtomic) if env.contains resolvedId && (!id.isAtomic || !isProtected env resolvedId) then resolvedId :: resolvedIds else -- Check whether environment contains the private version. That is, `_private.<module_name>.ns.id`. let resolvedIdPrv := mkPrivateName env resolvedId if env.contains resolvedIdPrv then resolvedIdPrv :: resolvedIds else resolvedIds /-- Check surrounding namespaces -/ private def resolveUsingNamespace (env : Environment) (id : Name) : Name → List Name | ns@(.str p _) => match resolveQualifiedName env ns id with | [] => resolveUsingNamespace env id p | resolvedIds => resolvedIds | _ => [] /-- Check exact name -/ private def resolveExact (env : Environment) (id : Name) : Option Name := if id.isAtomic then none else let resolvedId := id.replacePrefix rootNamespace Name.anonymous if env.contains resolvedId then some resolvedId else -- We also allow `_root` when accessing private declarations. -- If we change our minds, we should just replace `resolvedId` with `id` let resolvedIdPrv := mkPrivateName env resolvedId if env.contains resolvedIdPrv then some resolvedIdPrv else none /-- Check `OpenDecl`s -/ private def resolveOpenDecls (env : Environment) (id : Name) : List OpenDecl → List Name → List Name | [], resolvedIds => resolvedIds | OpenDecl.simple ns exs :: openDecls, resolvedIds => if exs.elem id then resolveOpenDecls env id openDecls resolvedIds else let newResolvedIds := resolveQualifiedName env ns id resolveOpenDecls env id openDecls (newResolvedIds ++ resolvedIds) | OpenDecl.explicit openedId resolvedId :: openDecls, resolvedIds => let resolvedIds := if openedId == id then resolvedId :: resolvedIds else if openedId.isPrefixOf id then let candidate := id.replacePrefix openedId resolvedId if env.contains candidate then candidate :: resolvedIds else resolvedIds else resolvedIds resolveOpenDecls env id openDecls resolvedIds def resolveGlobalName (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : List (Name × List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id let rec loop (id : Name) (projs : List String) : List (Name × List String) := match id with | .str p s => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { extractionResult with name := id }.review match resolveUsingNamespace env id ns with | resolvedIds@(_ :: _) => resolvedIds.eraseDups.map fun id => (id, projs) | [] => match resolveExact env id with | some newId => [(newId, projs)] | none => let resolvedIds := if env.contains id then [id] else [] let idPrv := mkPrivateName env id let resolvedIds := if env.contains idPrv then [idPrv] ++ resolvedIds else resolvedIds let resolvedIds := resolveOpenDecls env id openDecls resolvedIds let resolvedIds := getAliases env id (skipProtected := id.isAtomic) ++ resolvedIds match resolvedIds with | _ :: _ => resolvedIds.eraseDups.map fun id => (id, projs) | [] => loop p (s::projs) | _ => [] loop extractionResult.name [] /-! # Namespace resolution -/ def resolveNamespaceUsingScope? (env : Environment) (n : Name) : Name → Option Name | .anonymous => if env.isNamespace n then some n else none | ns@(.str p _) => if env.isNamespace (ns ++ n) then some (ns ++ n) else resolveNamespaceUsingScope? env n p | _ => unreachable! def resolveNamespaceUsingOpenDecls (env : Environment) (n : Name) : List OpenDecl → List Name | [] => [] | OpenDecl.simple ns [] :: ds => if env.isNamespace (ns ++ n) then (ns ++ n) :: resolveNamespaceUsingOpenDecls env n ds else resolveNamespaceUsingOpenDecls env n ds | _ :: ds => resolveNamespaceUsingOpenDecls env n ds /-- Given a name `id` try to find namespaces it may refer to. The resolution procedure works as follows 1- If `id` is in the scope of `namespace` commands the namespace `s_1. ... . s_n`, then we include `s_1 . ... . s_i ++ n` in the result if it is the name of an existing namespace. We search "backwards", and include at most one of the in the list of resulting namespaces. 2- If `id` is the extact name of an existing namespace, then include `id` 3- Finally, for each command `open N`, include in the result `N ++ n` if it is the name of an existing namespace. We only consider simple `open` commands. -/ def resolveNamespace (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : List Name := match resolveNamespaceUsingScope? env id ns with | some ns => ns :: resolveNamespaceUsingOpenDecls env id openDecls | none => resolveNamespaceUsingOpenDecls env id openDecls end ResolveName class MonadResolveName (m : Type → Type) where getCurrNamespace : m Name getOpenDecls : m (List OpenDecl) export MonadResolveName (getCurrNamespace getOpenDecls) instance (m n) [MonadLift m n] [MonadResolveName m] : MonadResolveName n where getCurrNamespace := liftM (m:=m) getCurrNamespace getOpenDecls := liftM (m:=m) getOpenDecls /-- Given a name `n`, return a list of possible interpretations. Each interpretation is a pair `(declName, fieldList)`, where `declName` is the name of a declaration in the current environment, and `fieldList` are (potential) field names. The pair is needed because in Lean `.` may be part of a qualified name or a field (aka dot-notation). As an example, consider the following definitions ``` def Boo.x := 1 def Foo.x := 2 def Foo.x.y := 3 ``` After `open Foo`, we have - `resolveGlobalName x` => `[(Foo.x, [])]` - `resolveGlobalName x.y` => `[(Foo.x.y, [])]` - `resolveGlobalName x.z.w` => `[(Foo.x, [z, w])]` After `open Foo open Boo`, we have - `resolveGlobalName x` => `[(Foo.x, []), (Boo.x, [])]` - `resolveGlobalName x.y` => `[(Foo.x.y, [])]` - `resolveGlobalName x.z.w` => `[(Foo.x, [z, w]), (Boo.x, [z, w])]` -/ def resolveGlobalName [Monad m] [MonadResolveName m] [MonadEnv m] (id : Name) : m (List (Name × List String)) := do return ResolveName.resolveGlobalName (← getEnv) (← getCurrNamespace) (← getOpenDecls) id /-- Given a namespace name, return a list of possible interpretations. Names extracted from syntax should be passed to `resolveNamespace` instead. -/ def resolveNamespaceCore [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] (id : Name) (allowEmpty := false) : m (List Name) := do let nss := ResolveName.resolveNamespace (← getEnv) (← getCurrNamespace) (← getOpenDecls) id if !allowEmpty && nss.isEmpty then throwError s!"unknown namespace '{id}'" return nss /-- Given a namespace identifier, return a list of possible interpretations. -/ def resolveNamespace [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] : Ident → m (List Name) | stx@⟨Syntax.ident _ _ n pre⟩ => do let pre := pre.filterMap fun | .namespace ns => some ns | _ => none if pre.isEmpty then withRef stx <| resolveNamespaceCore n else return pre | stx => throwErrorAt stx s!"expected identifier" /-- Given a namespace identifier, return the unique interpretation or else fail. -/ def resolveUniqueNamespace [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] (id : Ident) : m Name := do match (← resolveNamespace id) with | [ns] => return ns | nss => throwError s!"ambiguous namespace '{id.getId}', possible interpretations: '{nss}'" /-- Given a name `n`, return a list of possible interpretations for global constants. Similar to `resolveGlobalName`, but discard any candidate whose `fieldList` is not empty. For identifiers taken from syntax, use `resolveGlobalConst` instead, which respects preresolved names. -/ def resolveGlobalConstCore [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] (n : Name) : m (List Name) := do let cs ← resolveGlobalName n let cs := cs.filter fun (_, fieldList) => fieldList.isEmpty if cs.isEmpty then throwUnknownConstant n return cs.map (·.1) /-- For identifiers taken from syntax, use `resolveGlobalConstNoOverload` instead, which respects preresolved names. -/ def resolveGlobalConstNoOverloadCore [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] (n : Name) : m Name := do let cs ← resolveGlobalConstCore n match cs with | [c] => pure c | _ => throwError s!"ambiguous identifier '{mkConst n}', possible interpretations: {cs.map mkConst}" /-- Interpret the syntax `n` as an identifier for a global constant, and return a list of resolved constant names that it could be refering to based on the currently open namespaces. This should be used instead of `resolveGlobalConstCore` for identifiers taken from syntax because `Syntax` objects may have names that have already been resolved. ## Example: ``` def Boo.x := 1 def Foo.x := 2 def Foo.x.y := 3 ``` After `open Foo`, we have - `resolveGlobalConst x` => `[Foo.x]` - `resolveGlobalConst x.y` => `[Foo.x.y]` - `resolveGlobalConst x.z.w` => error: unknown constant After `open Foo open Boo`, we have - `resolveGlobalConst x` => `[Foo.x, Boo.x]` - `resolveGlobalConst x.y` => `[Foo.x.y]` - `resolveGlobalConst x.z.w` => error: unknown constant -/ def resolveGlobalConst [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] : Syntax → m (List Name) | stx@(Syntax.ident _ _ n pre) => do let pre := pre.filterMap fun | .decl n [] => some n | _ => none if pre.isEmpty then withRef stx <| resolveGlobalConstCore n else return pre | stx => throwErrorAt stx s!"expected identifier" /-- Interpret the syntax `n` as an identifier for a global constant, and return a resolved constant name. If there are multiple possible interpretations it will throw. ## Example: ``` def Boo.x := 1 def Foo.x := 2 def Foo.x.y := 3 ``` After `open Foo`, we have - `resolveGlobalConstNoOverload x` => `Foo.x` - `resolveGlobalConstNoOverload x.y` => `Foo.x.y` - `resolveGlobalConstNoOverload x.z.w` => error: unknown constant After `open Foo open Boo`, we have - `resolveGlobalConstNoOverload x` => error: ambiguous identifier - `resolveGlobalConstNoOverload x.y` => `Foo.x.y` - `resolveGlobalConstNoOverload x.z.w` => error: unknown constant -/ def resolveGlobalConstNoOverload [Monad m] [MonadResolveName m] [MonadEnv m] [MonadError m] (id : Syntax) : m Name := do let cs ← resolveGlobalConst id match cs with | [c] => pure c | _ => throwErrorAt id s!"ambiguous identifier '{id}', possible interpretations: {cs.map mkConst}" def unresolveNameGlobal [Monad m] [MonadResolveName m] [MonadEnv m] (n₀ : Name) (fullNames := false) : m Name := do if n₀.hasMacroScopes then return n₀ if fullNames then match (← resolveGlobalName n₀) with | [(potentialMatch, _)] => if potentialMatch == n₀ then return n₀ else return rootNamespace ++ n₀ | _ => return n₀ -- if can't resolve, return the original let mut initialNames := (getRevAliases (← getEnv) n₀).toArray initialNames := initialNames.push (rootNamespace ++ n₀) for initialName in initialNames do match (← unresolveNameCore initialName) with | none => continue | some n => return n return n₀ -- if can't resolve, return the original where unresolveNameCore (n : Name) : m (Option Name) := do let mut revComponents := n.componentsRev let mut candidate := Name.anonymous for _ in [:revComponents.length] do match revComponents with | [] => return none | cmpt::rest => candidate := cmpt ++ candidate; revComponents := rest match (← resolveGlobalName candidate) with | [(potentialMatch, _)] => if potentialMatch == n₀ then return some candidate else continue | _ => continue return none end Lean
f3d7279da643f1eaec861c89b93ee0aa0a5624ac
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/ore_localization/basic.lean
7adf5f507509f2817d84f597342cf811457dc409
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
33,779
lean
/- Copyright (c) 2022 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Kevin Klinge -/ import algebra.group_with_zero.basic import group_theory.congruence import group_theory.monoid_localization import ring_theory.non_zero_divisors import ring_theory.ore_localization.ore_set import tactic.noncomm_ring /-! # Localization over right Ore sets. This file defines the localization of a monoid over a right Ore set and proves its universal mapping property. It then extends the definition and its properties first to semirings and then to rings. We show that in the case of a commutative monoid this definition coincides with the common monoid localization. Finally we show that in a ring without zero divisors, taking the Ore localization at `R - {0}` results in a division ring. ## Notations Introduces the notation `R[S⁻¹]` for the Ore localization of a monoid `R` at a right Ore subset `S`. Also defines a new heterogeneos division notation `r /ₒ s` for a numerator `r : R` and a denominator `s : S`. ## References * <https://ncatlab.org/nlab/show/Ore+localization> * [Zoran Škoda, *Noncommutative localization in noncommutative geometry*][skoda2006] ## Tags localization, Ore, non-commutative -/ universe u open ore_localization namespace ore_localization variables (R : Type*) [monoid R] (S : submonoid R) [ore_set S] /-- The setoid on `R × S` used for the Ore localization. -/ def ore_eqv : setoid (R × S) := { r := λ rs rs', ∃ (u : S) (v : R), rs'.1 * u = rs.1 * v ∧ (rs'.2 : R) * u = rs.2 * v, iseqv := begin refine ⟨_, _, _⟩, { rintro ⟨r,s⟩, use 1, use 1, simp [submonoid.one_mem] }, { rintros ⟨r, s⟩ ⟨r', s'⟩ ⟨u, v, hru, hsu⟩, rcases ore_condition (s : R) s' with ⟨r₂, s₂, h₁⟩, rcases ore_condition r₂ u with ⟨r₃, s₃, h₂⟩, have : (s : R) * ((v : R) * r₃) = (s : R) * (s₂ * s₃), { assoc_rw [h₁, h₂, hsu], symmetry, apply mul_assoc }, rcases ore_left_cancel (v * r₃) (s₂ * s₃) s this with ⟨w, hw⟩, use s₂ * s₃ * w, use u * r₃ * w, split; simp only [submonoid.coe_mul], { assoc_rw [hru, ←hw], simp [mul_assoc] }, { assoc_rw [hsu, ←hw], simp [mul_assoc] } }, { rintros ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ ⟨r₃, s₃⟩ ⟨u, v, hur₁, hs₁u⟩ ⟨u', v', hur₂, hs₂u⟩, rcases ore_condition v' u with ⟨r', s', h⟩, use u' * s', use v * r', split; simp only [submonoid.coe_mul], { assoc_rw [hur₂, h, hur₁, mul_assoc] }, { assoc_rw [hs₂u, h, hs₁u, mul_assoc] } } end } end ore_localization /-- The ore localization of a monoid and a submonoid fulfilling the ore condition. -/ def ore_localization (R : Type*) [monoid R] (S : submonoid R) [ore_set S] := quotient (ore_localization.ore_eqv R S) namespace ore_localization section monoid variables {R : Type*} [monoid R] {S : submonoid R} variables (R S) [ore_set S] notation R `[`:1075 S `⁻¹]`:1075 := ore_localization R S local attribute [instance] ore_eqv variables {R S} /-- The division in the ore localization `R[S⁻¹]`, as a fraction of an element of `R` and `S`. -/ def ore_div (r : R) (s : S) : R[S⁻¹] := quotient.mk (r, s) infixl ` /ₒ `:70 := ore_div @[elab_as_eliminator] protected lemma ind {β : R [S ⁻¹] → Prop} (c : ∀ (r : R) (s : S), β (r /ₒ s)) : ∀ q, β q := by { apply quotient.ind, rintro ⟨r, s⟩, exact c r s } lemma ore_div_eq_iff {r₁ r₂ : R} {s₁ s₂ : S} : r₁ /ₒ s₁ = r₂ /ₒ s₂ ↔ (∃ (u : S) (v : R), r₂ * u = r₁ * v ∧ (s₂ : R) * u = s₁ * v) := quotient.eq' /-- A fraction `r /ₒ s` is equal to its expansion by an arbitrary factor `t` if `s * t ∈ S`. -/ protected lemma expand (r : R) (s : S) (t : R) (hst : (s : R) * t ∈ S) : r /ₒ s = (r * t) /ₒ (⟨s * t, hst⟩) := by { apply quotient.sound, refine ⟨s, t * s, _, _⟩; dsimp; rw [mul_assoc]; refl } /-- A fraction is equal to its expansion by an factor from s. -/ protected lemma expand' (r : R) (s s' : S) : r /ₒ s = (r * s') /ₒ (s * s') := ore_localization.expand r s s' (by norm_cast; apply set_like.coe_mem) /-- Fractions which differ by a factor of the numerator can be proven equal if those factors expand to equal elements of `R`. -/ protected lemma eq_of_num_factor_eq {r r' r₁ r₂ : R} {s t : S} (h : r * t = r' * t) : (r₁ * r * r₂) /ₒ s = (r₁ * r' * r₂) /ₒ s := begin rcases ore_condition r₂ t with ⟨r₂',t', hr₂⟩, calc (r₁ * r * r₂) /ₒ s = (r₁ * r * r₂ * t') /ₒ (s * t') : ore_localization.expand _ _ t' _ ... = ((r₁ * r) * (r₂ * t')) /ₒ (s * t') : by simp [←mul_assoc] ... = ((r₁ * r) * (t * r₂')) /ₒ (s * t') : by rw hr₂ ... = (r₁ * (r * t) * r₂') /ₒ (s * t') : by simp [←mul_assoc] ... = (r₁ * (r' * t) * r₂') /ₒ (s * t') : by rw h ... = (r₁ * r' * (t * r₂')) /ₒ (s * t') : by simp [←mul_assoc] ... = (r₁ * r' * (r₂ * t')) /ₒ (s * t') : by rw hr₂ ... = (r₁ * r' * r₂ * t') /ₒ (s * t') : by simp [←mul_assoc] ... = (r₁ * r' * r₂) /ₒ s : by symmetry; apply ore_localization.expand end /-- A function or predicate over `R` and `S` can be lifted to `R[S⁻¹]` if it is invariant under expansion on the right. -/ def lift_expand {C : Sort*} (P : R → S → C) (hP : ∀ (r t : R) (s : S) (ht : ((s : R) * t) ∈ S), P r s = P (r * t) ⟨s * t, ht⟩) : R[S⁻¹] → C := quotient.lift (λ (p : R × S), P p.1 p.2) $ λ p q pq, begin cases p with r₁ s₁, cases q with r₂ s₂, rcases pq with ⟨u, v, hr₂, hs₂⟩, dsimp at *, have s₁vS : (s₁ : R) * v ∈ S, { rw [←hs₂, ←S.coe_mul], exact set_like.coe_mem (s₂ * u) }, replace hs₂ : s₂ * u = ⟨(s₁ : R) * v, s₁vS⟩, { ext, simp [hs₂] }, rw [hP r₁ v s₁ s₁vS, hP r₂ u s₂ (by { norm_cast, rw hs₂, assumption }), hr₂], simpa [← hs₂] end @[simp] lemma lift_expand_of {C : Sort*} {P : R → S → C} {hP : ∀ (r t : R) (s : S) (ht : ((s : R) * t) ∈ S), P r s = P (r * t) ⟨s * t, ht⟩} (r : R) (s : S) : lift_expand P hP (r /ₒ s) = P r s := rfl /-- A version of `lift_expand` used to simultaneously lift functions with two arguments in ``R[S⁻¹]`.-/ def lift₂_expand {C : Sort*} (P : R → S → R → S → C) (hP : ∀ (r₁ t₁ : R) (s₁ : S) (ht₁ : (s₁ : R) * t₁ ∈ S) (r₂ t₂ : R) (s₂ : S) (ht₂ : (s₂ : R) * t₂ ∈ S), P r₁ s₁ r₂ s₂ = P (r₁ * t₁) ⟨s₁ * t₁, ht₁⟩ (r₂ * t₂) ⟨s₂ * t₂, ht₂⟩) : R[S⁻¹] → R[S⁻¹] → C := lift_expand (λ r₁ s₁, lift_expand (P r₁ s₁) $ λ r₂ t₂ s₂ ht₂, by simp [hP r₁ 1 s₁ (by simp) r₂ t₂ s₂ ht₂]) $ λ r₁ t₁ s₁ ht₁, begin ext x, induction x using ore_localization.ind with r₂ s₂, rw [lift_expand_of, lift_expand_of, hP r₁ t₁ s₁ ht₁ r₂ 1 s₂ (by simp)], simp, end @[simp] lemma lift₂_expand_of {C : Sort*} {P : R → S → R → S → C} {hP : ∀ (r₁ t₁ : R) (s₁ : S) (ht₁ : (s₁ : R) * t₁ ∈ S) (r₂ t₂ : R) (s₂ : S) (ht₂ : (s₂ : R) * t₂ ∈ S), P r₁ s₁ r₂ s₂ = P (r₁ * t₁) ⟨s₁ * t₁, ht₁⟩ (r₂ * t₂) ⟨s₂ * t₂, ht₂⟩} (r₁ : R) (s₁ : S) (r₂ : R) (s₂ : S) : lift₂_expand P hP (r₁ /ₒ s₁) (r₂ /ₒ s₂) = P r₁ s₁ r₂ s₂ := rfl private def mul' (r₁ : R) (s₁ : S) (r₂ : R) (s₂ : S) : R[S⁻¹] := (r₁ * ore_num r₂ s₁) /ₒ (s₂ * ore_denom r₂ s₁) private lemma mul'_char (r₁ r₂ : R) (s₁ s₂ : S) (u : S) (v : R) (huv : r₂ * (u : R) = s₁ * v) : mul' r₁ s₁ r₂ s₂ = (r₁ * v) /ₒ (s₂ * u) := begin simp only [mul'], have h₀ := ore_eq r₂ s₁, set v₀ := ore_num r₂ s₁, set u₀ := ore_denom r₂ s₁, rcases ore_condition (u₀ : R) u with ⟨r₃, s₃, h₃⟩, have := calc (s₁ : R) * (v * r₃) = r₂ * u * r₃ : by assoc_rw ←huv; symmetry; apply mul_assoc ... = r₂ * u₀ * s₃ : by assoc_rw ←h₃; refl ... = s₁ * (v₀ * s₃) : by assoc_rw h₀; apply mul_assoc, rcases ore_left_cancel _ _ _ this with ⟨s₄, hs₄⟩, symmetry, rw ore_div_eq_iff, use s₃ * s₄, use r₃ * s₄, simp only [submonoid.coe_mul], split, { assoc_rw ←hs₄, simp only [mul_assoc] }, { assoc_rw h₃, simp only [mul_assoc] } end /-- The multiplication on the Ore localization of monoids. -/ protected def mul : R[S⁻¹] → R[S⁻¹] → R[S⁻¹] := lift₂_expand mul' $ λ r₂ p s₂ hp r₁ r s₁ hr, begin have h₁ := ore_eq r₁ s₂, set r₁' := ore_num r₁ s₂, set s₂' := ore_denom r₁ s₂, rcases ore_condition (↑s₂ * r₁') ⟨s₂ * p, hp⟩ with ⟨p', s_star, h₂⟩, dsimp at h₂, rcases ore_condition r (s₂' * s_star) with ⟨p_flat, s_flat, h₃⟩, simp only [S.coe_mul] at h₃, have : r₁ * r * s_flat = s₂ * p * (p' * p_flat), { rw [←mul_assoc, ←h₂, ←h₁, mul_assoc, h₃], simp only [mul_assoc] }, rw mul'_char (r₂ * p) (r₁ * r) ⟨↑s₂ * p, hp⟩ ⟨↑s₁ * r, hr⟩ _ _ this, clear this, have hsssp : ↑s₁ * ↑s₂' * ↑s_star * p_flat ∈ S, { rw [mul_assoc, mul_assoc, ←mul_assoc ↑s₂', ←h₃, ←mul_assoc], exact S.mul_mem hr (set_like.coe_mem s_flat) }, have : (⟨↑s₁ * r, hr⟩ : S) * s_flat = ⟨s₁ * s₂' * s_star * p_flat, hsssp⟩, { ext, simp only [set_like.coe_mk, submonoid.coe_mul], rw [mul_assoc, h₃, ←mul_assoc, ←mul_assoc] }, rw this, clear this, rcases ore_left_cancel (p * p') (r₁' * ↑s_star) s₂ (by simp [←mul_assoc, h₂]) with ⟨s₂'', h₂''⟩, rw [←mul_assoc, mul_assoc r₂, ore_localization.eq_of_num_factor_eq h₂''], norm_cast at ⊢ hsssp, rw [←ore_localization.expand _ _ _ hsssp, ←mul_assoc], apply ore_localization.expand end instance : has_mul R[S⁻¹] := ⟨ore_localization.mul⟩ lemma ore_div_mul_ore_div {r₁ r₂ : R} {s₁ s₂ : S} : (r₁ /ₒ s₁) * (r₂ /ₒ s₂) = (r₁ * ore_num r₂ s₁) /ₒ (s₂ * ore_denom r₂ s₁) := rfl /-- A characterization lemma for the multiplication on the Ore localization, allowing for a choice of Ore numerator and Ore denominator. -/ lemma ore_div_mul_char (r₁ r₂ : R) (s₁ s₂ : S) (r' : R) (s' : S) (huv : r₂ * (s' : R) = s₁ * r') : (r₁ /ₒ s₁) * (r₂ /ₒ s₂) = (r₁ * r') /ₒ (s₂ * s') := mul'_char r₁ r₂ s₁ s₂ s' r' huv /-- Another characterization lemma for the multiplication on the Ore localizaion delivering Ore witnesses and conditions bundled in a sigma type. -/ def ore_div_mul_char' (r₁ r₂ : R) (s₁ s₂ : S) : Σ' r' : R, Σ' s' : S, r₂ * (s' : R) = s₁ * r' ∧ (r₁ /ₒ s₁) * (r₂ /ₒ s₂) = (r₁ * r') /ₒ (s₂ * s') := ⟨ore_num r₂ s₁, ore_denom r₂ s₁, ore_eq r₂ s₁, ore_div_mul_ore_div⟩ instance : has_one R[S⁻¹] := ⟨1 /ₒ 1⟩ protected lemma one_def : (1 : R[S⁻¹]) = 1 /ₒ 1 := rfl instance : inhabited R[S⁻¹] := ⟨1⟩ @[simp] protected lemma div_eq_one' {r : R} (hr : r ∈ S) : r /ₒ ⟨r, hr⟩ = 1 := by { rw [ore_localization.one_def, ore_div_eq_iff], exact ⟨⟨r, hr⟩, 1, by simp, by simp⟩ } @[simp] protected lemma div_eq_one {s : S} : (s : R) /ₒ s = 1 := by { cases s; apply ore_localization.div_eq_one' } protected lemma one_mul (x : R[S⁻¹]) : 1 * x = x := begin induction x using ore_localization.ind with r s, simp [ore_localization.one_def, ore_div_mul_char (1 : R) r (1 : S) s r 1 (by simp)] end protected lemma mul_one (x : R[S⁻¹]) : x * 1 = x := begin induction x using ore_localization.ind with r s, simp [ore_localization.one_def, ore_div_mul_char r 1 s 1 1 s (by simp)] end protected lemma mul_assoc (x y z : R[S⁻¹]) : x * y * z = x * (y * z) := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_mul_char' r₁ r₂ s₁ s₂ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rcases ore_div_mul_char' r₂ r₃ s₂ s₃ with ⟨rb, sb, hb, hb'⟩, rw hb', clear hb', rcases ore_condition rb sa with ⟨rc, sc, hc⟩, rw ore_div_mul_char (r₁ * ra) r₃ (s₂ * sa) s₃ rc (sb * sc) (by { simp only [submonoid.coe_mul], assoc_rw [hb, hc] }), rw [mul_assoc, ←mul_assoc s₃], symmetry, apply ore_div_mul_char, assoc_rw [hc, ←ha], apply mul_assoc end instance : monoid R[S⁻¹] := { one_mul := ore_localization.one_mul, mul_one := ore_localization.mul_one, mul_assoc := ore_localization.mul_assoc, .. ore_localization.has_mul, .. ore_localization.has_one } protected lemma mul_inv (s s' : S) : ((s : R) /ₒ s') * (s' /ₒ s) = 1 := by simp [ore_div_mul_char (s :R) s' s' s 1 1 (by simp)] @[simp] protected lemma mul_one_div {r : R} {s t : S} : (r /ₒ s) * (1 /ₒ t) = r /ₒ (t * s) := by simp [ore_div_mul_char r 1 s t 1 s (by simp)] @[simp] protected lemma mul_cancel {r : R} {s t : S} : (r /ₒ s) * (s /ₒ t) = r /ₒ t := by simp [ore_div_mul_char r s s t 1 1 (by simp)] @[simp] protected lemma mul_cancel' {r₁ r₂ : R} {s t : S} : (r₁ /ₒ s) * ((s * r₂) /ₒ t) = (r₁ * r₂) /ₒ t := by simp [ore_div_mul_char r₁ (s * r₂) s t r₂ 1 (by simp)] @[simp] lemma div_one_mul {p r : R} {s : S} : (r /ₒ 1) * (p /ₒ s) = (r * p) /ₒ s := --TODO use coercion r ↦ r /ₒ 1 by simp [ore_div_mul_char r p 1 s p 1 (by simp)] /-- The fraction `s /ₒ 1` as a unit in `R[S⁻¹]`, where `s : S`. -/ def numerator_unit (s : S) : units R[S⁻¹] := { val := (s : R) /ₒ 1, inv := (1 : R) /ₒ s, val_inv := ore_localization.mul_inv s 1, inv_val := ore_localization.mul_inv 1 s } /-- The multiplicative homomorphism from `R` to `R[S⁻¹]`, mapping `r : R` to the fraction `r /ₒ 1`. -/ def numerator_hom : R →* R[S⁻¹] := { to_fun := λ r, r /ₒ 1, map_one' := rfl, map_mul' := λ r₁ r₂, div_one_mul.symm } lemma numerator_hom_apply {r : R} : numerator_hom r = r /ₒ (1 : S) := rfl lemma numerator_is_unit (s : S) : is_unit ((numerator_hom (s : R)) : R[S⁻¹]) := ⟨numerator_unit s, rfl⟩ section UMP variables {T : Type*} [monoid T] variables (f : R →* T) (fS : S →* units T) variables (hf : ∀ (s : S), f s = fS s) include f fS hf /-- The universal lift from a morphism `R →* T`, which maps elements of `S` to units of `T`, to a morphism `R[S⁻¹] →* T`. -/ def universal_mul_hom : R[S⁻¹] →* T := { to_fun := λ x, x.lift_expand (λ r s, (f r) * ((fS s)⁻¹ : units T)) $ λ r t s ht, begin have : ((fS ⟨s * t, ht⟩) : T) = fS s * f t, { simp only [←hf, set_like.coe_mk, monoid_hom.map_mul] }, conv_rhs { rw [monoid_hom.map_mul, ←mul_one (f r), ←units.coe_one, ←mul_left_inv (fS s)], rw [units.coe_mul, ←mul_assoc, mul_assoc _ ↑(fS s), ←this, mul_assoc] }, simp only [mul_one, units.mul_inv] end, map_one' := by rw [ore_localization.one_def, lift_expand_of]; simp, map_mul' := λ x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, rcases ore_div_mul_char' r₁ r₂ s₁ s₂ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rw [lift_expand_of, lift_expand_of, lift_expand_of], conv_rhs { congr, skip, congr, rw [←mul_one (f r₂), ←(fS sa).mul_inv, ←mul_assoc, ←hf, ←f.map_mul, ha, f.map_mul] }, rw [mul_assoc, mul_assoc, mul_assoc, ←mul_assoc _ (f s₁), hf s₁, (fS s₁).inv_mul, one_mul, f.map_mul, mul_assoc, fS.map_mul, ←units.coe_mul], refl end } lemma universal_mul_hom_apply {r : R} {s : S} : universal_mul_hom f fS hf (r /ₒ s) = (f r) * ((fS s)⁻¹ : units T) := rfl lemma universal_mul_hom_commutes {r : R} : universal_mul_hom f fS hf (numerator_hom r) = f r := by simp [numerator_hom_apply, universal_mul_hom_apply] /-- The universal morphism `universal_mul_hom` is unique. -/ lemma universal_mul_hom_unique (φ : R[S⁻¹] →* T) (huniv : ∀ (r : R), φ (numerator_hom r) = f r) : φ = universal_mul_hom f fS hf := begin ext, induction x using ore_localization.ind with r s, rw [universal_mul_hom_apply, ←huniv r, numerator_hom_apply, ←mul_one (φ (r /ₒ s)), ←units.coe_one, ←mul_right_inv (fS s), units.coe_mul, ←mul_assoc, ←hf, ←huniv, ←φ.map_mul, numerator_hom_apply, ore_localization.mul_cancel], end end UMP end monoid section comm_monoid variables {R : Type*} [comm_monoid R] {S : submonoid R} [ore_set S] lemma ore_div_mul_ore_div_comm {r₁ r₂ : R} {s₁ s₂ : S} : (r₁ /ₒ s₁) * (r₂ /ₒ s₂) = (r₁ * r₂) /ₒ (s₁ * s₂) := by rw [ore_div_mul_char r₁ r₂ s₁ s₂ r₂ s₁ (by simp [mul_comm]), mul_comm s₂] instance : comm_monoid R[S⁻¹] := { mul_comm := λ x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, rw [ore_div_mul_ore_div_comm, ore_div_mul_ore_div_comm, mul_comm r₁, mul_comm s₁], end, ..ore_localization.monoid } variables (R S) /-- The morphism `numerator_hom` is a monoid localization map in the case of commutative `R`. -/ protected def localization_map : S.localization_map R[S⁻¹] := { to_fun := numerator_hom, map_one' := rfl, map_mul' := λ r₁ r₂, by simp, map_units' := numerator_is_unit, surj' := λ z, begin induction z using ore_localization.ind with r s, use (r, s), dsimp, rw [numerator_hom_apply, numerator_hom_apply], simp end, eq_iff_exists' := λ r₁ r₂, begin dsimp, split, { intro h, rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff] at h, rcases h with ⟨u, v, h₁, h₂⟩, dsimp at h₂, rw [one_mul, one_mul] at h₂, subst h₂, use u, exact h₁.symm }, { rintro ⟨s, h⟩, rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff], use s, use s, simp [h, one_mul] } end } /-- If `R` is commutative, Ore localization and monoid localization are isomorphic. -/ protected noncomputable def equiv_monoid_localization : localization S ≃* R[S⁻¹] := localization.mul_equiv_of_quotient (ore_localization.localization_map R S) end comm_monoid section semiring variables {R : Type*} [semiring R] {S : submonoid R} [ore_set S] private def add'' (r₁ : R) (s₁ : S) (r₂ : R) (s₂ : S) : R[S⁻¹] := (r₁ * ore_denom (s₁ : R) s₂ + r₂ * ore_num s₁ s₂) /ₒ (s₁ * ore_denom s₁ s₂) private lemma add''_char (r₁ : R) (s₁ : S) (r₂ : R) (s₂ : S) (rb : R) (sb : S) (hb : (s₁ : R) * sb = (s₂ : R) * rb) : add'' r₁ s₁ r₂ s₂ = (r₁ * sb + r₂ * rb) /ₒ (s₁ * sb) := begin simp only [add''], have ha := ore_eq (s₁ : R) s₂, set! ra := ore_num (s₁ : R) s₂ with h, rw ←h at *, clear h, -- r tilde set! sa := ore_denom (s₁ : R) s₂ with h, rw ←h at *, clear h, -- s tilde rcases ore_condition (sa : R) sb with ⟨rc, sc, hc⟩, -- s*, r* have : (s₂ : R) * (rb * rc) = s₂ * (ra * sc), { rw [←mul_assoc, ←hb, mul_assoc, ←hc, ←mul_assoc, ←mul_assoc, ha] }, rcases ore_left_cancel _ _ s₂ this with ⟨sd, hd⟩, -- s# symmetry, rw ore_div_eq_iff, use sc * sd, use rc * sd, split; simp only [submonoid.coe_mul], { noncomm_ring, assoc_rw [hd, hc], noncomm_ring }, { assoc_rewrite [hc], noncomm_ring } end local attribute [instance] ore_localization.ore_eqv private def add' (r₂ : R) (s₂ : S) : R[S⁻¹] → R[S⁻¹] := --plus tilde quotient.lift (λ (r₁s₁ : R × S), add'' r₁s₁.1 r₁s₁.2 r₂ s₂) $ begin rintros ⟨r₁', s₁'⟩ ⟨r₁, s₁⟩ ⟨sb, rb, hb, hb'⟩, -- s*, r* rcases ore_condition (s₁' : R) s₂ with ⟨rc, sc, hc⟩, --s~~, r~~ rcases ore_condition rb sc with ⟨rd, sd, hd⟩, -- s#, r# dsimp at *, rw add''_char _ _ _ _ rc sc hc, have : ↑s₁ * ↑(sb * sd) = ↑s₂ * (rc * rd), { simp only [submonoid.coe_mul], assoc_rewrite [hb', hd, hc], noncomm_ring }, rw add''_char _ _ _ _ (rc * rd : R) (sb * sd : S) this, simp only [submonoid.coe_mul], assoc_rw [hb, hd], rw [←mul_assoc, ←add_mul, ore_div_eq_iff], use 1, use rd, split, { simp }, { simp only [mul_one, submonoid.coe_one, submonoid.coe_mul] at ⊢ this, assoc_rw [hc, this] }, end private lemma add'_comm (r₁ r₂ : R) (s₁ s₂ : S) : add' r₁ s₁ (r₂ /ₒ s₂) = add' r₂ s₂ (r₁ /ₒ s₁) := begin simp only [add', ore_div, add'', quotient.lift_mk, quotient.eq], have hb := ore_eq ↑s₂ s₁, set rb := ore_num ↑s₂ s₁ with h, -- r~~ rw ←h, clear h, set sb := ore_denom ↑s₂ s₁ with h, rw ←h, clear h, -- s~~ have ha := ore_eq ↑s₁ s₂, set ra := ore_num ↑s₁ s₂ with h, -- r~ rw ←h, clear h, set sa := ore_denom ↑s₁ s₂ with h, rw ←h, clear h, -- s~ rcases ore_condition ra sb with ⟨rc, sc, hc⟩, -- r#, s# have : (s₁ : R) * (rb * rc) = s₁ * (sa * sc), { rw [←mul_assoc, ←hb, mul_assoc, ←hc, ←mul_assoc, ←ha, mul_assoc] }, rcases ore_left_cancel _ _ s₁ this with ⟨sd, hd⟩, -- s+ use sc * sd, use rc * sd, dsimp, split, { rw [add_mul, add_mul, add_comm], assoc_rw [←hd, hc], noncomm_ring }, { rw [mul_assoc, ←mul_assoc ↑sa, ←hd, hb], noncomm_ring } end /-- The addition on the Ore localization. -/ private def add : R[S⁻¹] → R[S⁻¹] → R[S⁻¹] := λ x, quotient.lift (λ rs : R × S, add' rs.1 rs.2 x) begin rintros ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ hyz, induction x using ore_localization.ind with r₃ s₃, dsimp, rw [add'_comm, add'_comm r₂], simp [(/ₒ), quotient.sound hyz], end instance : has_add R[S⁻¹] := ⟨add⟩ lemma ore_div_add_ore_div {r r' : R} {s s' : S} : r /ₒ s + r' /ₒ s' = (r * ore_denom (s : R) s' + r' * ore_num s s') /ₒ (s * ore_denom s s') := rfl /-- A characterization of the addition on the Ore localizaion, allowing for arbitrary Ore numerator and Ore denominator. -/ lemma ore_div_add_char {r r' : R} (s s' : S) (rb : R) (sb : S) (h : (s : R) * sb = s' * rb) : r /ₒ s + r' /ₒ s' = (r * sb + r' * rb) /ₒ (s * sb) := add''_char r s r' s' rb sb h /-- Another characterization of the addition on the Ore localization, bundling up all witnesses and conditions into a sigma type. -/ def ore_div_add_char' (r r' : R) (s s' : S) : Σ' r'' : R, Σ' s'' : S, (s : R) * s'' = s' * r'' ∧ r /ₒ s + r' /ₒs' = (r * s'' + r' * r'') /ₒ (s * s'') := ⟨ore_num s s', ore_denom s s', ore_eq s s', ore_div_add_ore_div⟩ @[simp] lemma add_ore_div {r r' : R} {s : S} : (r /ₒ s) + (r' /ₒ s) = (r + r') /ₒ s := by simp [ore_div_add_char s s 1 1 (by simp)] protected lemma add_assoc (x y z : R[S⁻¹]) : (x + y) + z = x + (y + z) := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' r₁ r₂ s₁ s₂ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rcases ore_div_add_char' r₂ r₃ s₂ s₃ with ⟨rb, sb, hb, hb'⟩, rw hb', clear hb', rcases ore_div_add_char' (r₁ * sa + r₂ * ra) r₃ (s₁ * sa) s₃ with ⟨rc, sc, hc, q⟩, rw q, clear q, rcases ore_div_add_char' r₁ (r₂ * sb + r₃ * rb) s₁ (s₂ * sb) with ⟨rd, sd, hd, q⟩, rw q, clear q, noncomm_ring, rw add_comm (r₂ * _), repeat { rw ←add_ore_div }, congr' 1, { rcases ore_condition (sd : R) (sa * sc) with ⟨re, se, he⟩, { simp_rw ←submonoid.coe_mul at hb hc hd, assoc_rw [subtype.coe_eq_of_eq_mk hc], rw [←ore_localization.expand, subtype.coe_eq_of_eq_mk hd, ←mul_assoc, ←ore_localization.expand, subtype.coe_eq_of_eq_mk hb], apply ore_localization.expand } }, congr' 1, { rw [←ore_localization.expand', ←mul_assoc, ←mul_assoc, ←ore_localization.expand', ←ore_localization.expand'] }, { simp_rw [←submonoid.coe_mul] at ha hd, rw [subtype.coe_eq_of_eq_mk hd, ←mul_assoc, ←mul_assoc, ←mul_assoc, ←ore_localization.expand, ←ore_localization.expand', subtype.coe_eq_of_eq_mk ha, ←ore_localization.expand], apply ore_localization.expand' } end private def zero : R[S⁻¹] := 0 /ₒ 1 instance : has_zero R[S⁻¹] := ⟨zero⟩ protected lemma zero_def : (0 : R[S⁻¹]) = 0 /ₒ 1 := rfl @[simp] lemma zero_div_eq_zero (s : S) : 0 /ₒ s = 0 := by { rw [ore_localization.zero_def, ore_div_eq_iff], exact ⟨s, 1, by simp⟩ } protected lemma zero_add (x : R[S⁻¹]) : 0 + x = x := begin induction x using ore_localization.ind, rw [←zero_div_eq_zero, add_ore_div], simp end protected lemma add_comm (x y : R[S⁻¹]) : x + y = y + x := begin induction x using ore_localization.ind, induction y using ore_localization.ind, change add' _ _ (_ /ₒ _) = _, apply add'_comm end instance : add_comm_monoid R[S⁻¹] := { add_comm := ore_localization.add_comm, add_assoc := ore_localization.add_assoc, zero := zero, zero_add := ore_localization.zero_add, add_zero := λ x, by rw [ore_localization.add_comm, ore_localization.zero_add], .. ore_localization.has_add } protected lemma zero_mul (x : R[S⁻¹]) : 0 * x = 0 := begin induction x using ore_localization.ind with r s, rw [ore_localization.zero_def, ore_div_mul_char 0 r 1 s r 1 (by simp)], simp end protected lemma mul_zero (x : R[S⁻¹]) : x * 0 = 0 := begin induction x using ore_localization.ind with r s, rw [ore_localization.zero_def, ore_div_mul_char r 0 s 1 0 1 (by simp)], simp end protected lemma left_distrib (x y z : R[S⁻¹]) : x * (y + z) = x * y + x * z := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' r₂ r₃ s₂ s₃ with ⟨ra, sa, ha, q⟩, rw q, clear q, rw ore_localization.expand' r₂ s₂ sa, rcases ore_div_mul_char' r₁ (r₂ * sa) s₁ (s₂ * sa) with ⟨rb, sb, hb, q⟩, rw q, clear q, have hs₃rasb : ↑s₃ * (ra * sb) ∈ S, { rw [←mul_assoc, ←ha], norm_cast, apply set_like.coe_mem }, rw ore_localization.expand _ _ _ hs₃rasb, have ha' : (↑(s₂ * sa * sb)) = ↑s₃ * (ra * sb), { simp [ha, ←mul_assoc] }, rw ←subtype.coe_eq_of_eq_mk ha', rcases ore_div_mul_char' r₁ (r₃ * (ra * sb)) s₁ (s₂ * sa * sb) with ⟨rc, sc, hc, hc'⟩, rw hc', rw ore_div_add_char (s₂ * sa * sb) (s₂ * sa * sb * sc) 1 sc (by simp), rw ore_localization.expand' (r₂ * ↑sa + r₃ * ra) (s₂ * sa) (sb * sc), conv_lhs { congr, skip, congr, rw [add_mul, S.coe_mul, ←mul_assoc, hb, ←mul_assoc, mul_assoc r₃, hc, mul_assoc, ←mul_add] }, rw ore_localization.mul_cancel', simp only [mul_one, submonoid.coe_mul, mul_add, ←mul_assoc], end lemma right_distrib (x y z : R[S⁻¹]) : (x + y) * z = x * z + y * z := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' r₁ r₂ s₁ s₂ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', norm_cast at ha, rw ore_localization.expand' r₁ s₁ sa, rw ore_localization.expand r₂ s₂ ra (by rw ←ha; apply set_like.coe_mem), rw ←subtype.coe_eq_of_eq_mk ha, repeat { rw ore_div_mul_ore_div }, simp only [add_mul, add_ore_div] end instance : semiring R[S⁻¹] := { zero_mul := ore_localization.zero_mul, mul_zero := ore_localization.mul_zero, left_distrib := ore_localization.left_distrib, right_distrib := right_distrib, .. ore_localization.add_comm_monoid, .. ore_localization.monoid } section UMP variables {T : Type*} [semiring T] variables (f : R →+* T) (fS : S →* units T) variables (hf : ∀ (s : S), f s = fS s) include f fS hf /-- The universal lift from a ring homomorphism `f : R →+* T`, which maps elements in `S` to units of `T`, to a ring homomorphism `R[S⁻¹] →+* T`. This extends the construction on monoids. -/ def universal_hom : R[S⁻¹] →+* T := { map_zero' := begin rw [monoid_hom.to_fun_eq_coe, ore_localization.zero_def, universal_mul_hom_apply], simp end, map_add' := λ x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with r₂ s₂, rcases ore_div_add_char' r₁ r₂ s₁ s₂ with ⟨r₃, s₃, h₃, h₃'⟩, rw h₃', clear h₃', simp only [universal_mul_hom_apply, ring_hom.coe_monoid_hom, ring_hom.to_monoid_hom_eq_coe, monoid_hom.to_fun_eq_coe], simp only [mul_inv_rev, monoid_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, units.coe_mul], rw [add_mul, ←mul_assoc, mul_assoc (f r₁), hf, ←units.coe_mul], simp only [mul_one, mul_right_inv, units.coe_one], congr' 1, rw [mul_assoc], congr' 1, norm_cast at h₃, have h₃' := subtype.coe_eq_of_eq_mk h₃, rw [←units.coe_mul, ←mul_inv_rev, ←fS.map_mul, h₃'], have hs₂r₃ : ↑s₂ * r₃ ∈ S, { rw ←h₃, exact set_like.coe_mem (s₁ * s₃)}, apply (units.inv_mul_cancel_left (fS s₂) _).symm.trans, conv_lhs { congr, skip, rw [←units.mul_inv_cancel_left (fS ⟨s₂ * r₃, hs₂r₃⟩) (fS s₂), mul_assoc, mul_assoc], congr, skip, rw [←hf, ←mul_assoc (f s₂), ←f.map_mul], conv { congr, skip, congr, rw [←h₃] }, rw [hf, ←mul_assoc, ←h₃', units.inv_mul] }, rw [one_mul, ←h₃', units.mul_inv, mul_one], end, .. universal_mul_hom f.to_monoid_hom fS hf } lemma universal_hom_apply {r : R} {s : S} : universal_hom f fS hf (r /ₒ s) = (f r) * ((fS s)⁻¹ : units T) := rfl lemma universal_hom_commutes {r : R} : universal_hom f fS hf (numerator_hom r) = f r := by simp [numerator_hom_apply, universal_hom_apply] lemma universal_hom_unique (φ : R[S⁻¹] →+* T) (huniv : ∀ (r : R), φ (numerator_hom r) = f r) : φ = universal_hom f fS hf := ring_hom.coe_monoid_hom_injective $ universal_mul_hom_unique (ring_hom.to_monoid_hom f) fS hf ↑φ huniv end UMP end semiring section ring variables {R : Type*} [ring R] {S : submonoid R} [ore_set S] /-- Negation on the Ore localization is defined via negation on the numerator. -/ protected def neg : R[S⁻¹] → R[S⁻¹] := lift_expand (λ (r : R) (s : S), (- r) /ₒ s) $ λ r t s ht, by rw [neg_mul_eq_neg_mul, ←ore_localization.expand] instance : has_neg R[S⁻¹] := ⟨ore_localization.neg⟩ @[simp] protected lemma neg_def (r : R) (s : S) : - (r /ₒ s) = (- r) /ₒ s := rfl protected lemma add_left_neg (x : R[S⁻¹]) : (- x) + x = 0 := by induction x using ore_localization.ind with r s; simp instance : ring R[S⁻¹] := { add_left_neg := ore_localization.add_left_neg, .. ore_localization.semiring, .. ore_localization.has_neg } open_locale non_zero_divisors lemma numerator_hom_inj (hS : S ≤ R⁰) : function.injective (numerator_hom : R → R[S⁻¹]) := λ r₁ r₂ h, begin rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff] at h, rcases h with ⟨u, v, h₁, h₂⟩, simp only [S.coe_one, one_mul] at h₂, rwa [←h₂, mul_cancel_right_mem_non_zero_divisor (hS (set_like.coe_mem u)), eq_comm] at h₁, end lemma nontrivial_of_non_zero_divisors [nontrivial R] (hS : S ≤ R⁰) : nontrivial R[S⁻¹] := ⟨⟨0, 1, λ h, begin rw [ore_localization.one_def, ore_localization.zero_def] at h, apply non_zero_divisors.coe_ne_zero 1 (numerator_hom_inj hS h).symm end⟩⟩ end ring section division_ring open_locale non_zero_divisors open_locale classical variables {R : Type*} [ring R] [nontrivial R] [ore_set R⁰] instance : nontrivial R[R⁰⁻¹] := nontrivial_of_non_zero_divisors (refl R⁰) variables [no_zero_divisors R] noncomputable theory /-- The inversion of Ore fractions for a ring without zero divisors, satisying `0⁻¹ = 0` and `(r /ₒ r')⁻¹ = r' /ₒ r` for `r ≠ 0`. -/ protected def inv : R[R⁰⁻¹] → R[R⁰⁻¹] := lift_expand (λ r s, if hr: r = (0 : R) then (0 : R[R⁰⁻¹]) else (s /ₒ ⟨r, λ _, eq_zero_of_ne_zero_of_mul_right_eq_zero hr⟩)) begin intros r t s hst, by_cases hr : r = 0, { simp [hr] }, { by_cases ht : t = 0, { exfalso, apply non_zero_divisors.coe_ne_zero ⟨_, hst⟩, simp [ht, mul_zero] }, { simp only [hr, ht, set_like.coe_mk, dif_neg, not_false_iff, or_self, mul_eq_zero], apply ore_localization.expand } } end instance : has_inv R[R⁰⁻¹] := ⟨ore_localization.inv⟩ protected lemma inv_def {r : R} {s : R⁰} : (r /ₒ s)⁻¹ = if hr: r = (0 : R) then (0 : R[R⁰⁻¹]) else (s /ₒ ⟨r, λ _, eq_zero_of_ne_zero_of_mul_right_eq_zero hr⟩) := rfl protected lemma mul_inv_cancel (x : R[R⁰⁻¹]) (h : x ≠ 0) : x * x⁻¹ = 1 := begin induction x using ore_localization.ind with r s, rw [ore_localization.inv_def, ore_localization.one_def], by_cases hr : r = 0, { exfalso, apply h, simp [hr] }, { simp [hr], apply ore_localization.div_eq_one' } end protected lemma inv_zero : (0 : R[R⁰⁻¹])⁻¹ = 0 := by { rw [ore_localization.zero_def, ore_localization.inv_def], simp } instance : division_ring R[(R⁰)⁻¹] := { mul_inv_cancel := ore_localization.mul_inv_cancel, inv_zero := ore_localization.inv_zero, .. ore_localization.nontrivial, .. ore_localization.has_inv, .. ore_localization.ring } end division_ring end ore_localization
b1d998f94876ebd033d35949bb0bbb061eb31d88
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/397c.lean
3d2203a829d338eda3153ad19e03dcce0d7fb4e6
[ "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
237
lean
class ring (α) extends has_one α set_option trace.class_instances true -- this should only synthesize `has_one α` once, -- the second time it should already be cached example (α : Type) [ring α] (h : (1 : α) = 1) : true := trivial
277a9794d4550d8402e2ec8329913098bf8c60df
c777c32c8e484e195053731103c5e52af26a25d1
/src/geometry/euclidean/sphere/second_inter.lean
ec52336826a8c16a2cc09b5885ed784d7abf100d
[ "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
7,559
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import geometry.euclidean.sphere.basic /-! # Second intersection of a sphere and a line This file defines and proves basic results about the second intersection of a sphere with a line through a point on that sphere. ## Main definitions * `euclidean_geometry.sphere.second_inter` is the second intersection of a sphere with a line through a point on that sphere. -/ noncomputable theory open_locale real_inner_product_space namespace euclidean_geometry variables {V : Type*} {P : Type*} [normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] include V /-- The second intersection of a sphere with a line through a point on that sphere; that point if it is the only point of intersection of the line with the sphere. The intended use of this definition is when `p ∈ s`; the definition does not use `s.radius`, so in general it returns the second intersection with the sphere through `p` and with center `s.center`. -/ def sphere.second_inter (s : sphere P) (p : P) (v : V) : P := (-2 * ⟪v, p -ᵥ s.center⟫ / ⟪v, v⟫) • v +ᵥ p /-- The distance between `second_inter` and the center equals the distance between the original point and the center. -/ @[simp] lemma sphere.second_inter_dist (s : sphere P) (p : P) (v : V) : dist (s.second_inter p v) s.center = dist p s.center := begin rw sphere.second_inter, by_cases hv : v = 0, { simp [hv] }, rw dist_smul_vadd_eq_dist _ _ hv, exact or.inr rfl end /-- The point given by `second_inter` lies on the sphere. -/ @[simp] lemma sphere.second_inter_mem {s : sphere P} {p : P} (v : V) : s.second_inter p v ∈ s ↔ p ∈ s := by simp_rw [mem_sphere, sphere.second_inter_dist] variables (V) /-- If the vector is zero, `second_inter` gives the original point. -/ @[simp] lemma sphere.second_inter_zero (s : sphere P) (p : P) : s.second_inter p (0 : V) = p := by simp [sphere.second_inter] variables {V} /-- The point given by `second_inter` equals the original point if and only if the line is orthogonal to the radius vector. -/ lemma sphere.second_inter_eq_self_iff {s : sphere P} {p : P} {v : V} : s.second_inter p v = p ↔ ⟪v, p -ᵥ s.center⟫ = 0 := begin refine ⟨λ hp, _, λ hp, _⟩, { by_cases hv : v = 0, { simp [hv] }, rwa [sphere.second_inter, eq_comm, eq_vadd_iff_vsub_eq, vsub_self, eq_comm, smul_eq_zero, or_iff_left hv, div_eq_zero_iff, inner_self_eq_zero, or_iff_left hv, mul_eq_zero, or_iff_right (by norm_num : (-2 : ℝ) ≠ 0)] at hp }, { rw [sphere.second_inter, hp, mul_zero, zero_div, zero_smul, zero_vadd] } end /-- A point on a line through a point on a sphere equals that point or `second_inter`. -/ lemma sphere.eq_or_eq_second_inter_of_mem_mk'_span_singleton_iff_mem {s : sphere P} {p : P} (hp : p ∈ s) {v : V} {p' : P} (hp' : p' ∈ affine_subspace.mk' p (ℝ ∙ v)) : (p' = p ∨ p' = s.second_inter p v) ↔ p' ∈ s := begin refine ⟨λ h, _, λ h, _⟩, { rcases h with h | h, { rwa h }, { rwa [h, sphere.second_inter_mem] } }, { rw [affine_subspace.mem_mk'_iff_vsub_mem, submodule.mem_span_singleton] at hp', rcases hp' with ⟨r, hr⟩, rw [eq_comm, ←eq_vadd_iff_vsub_eq] at hr, subst hr, by_cases hv : v = 0, { simp [hv] }, rw sphere.second_inter, rw mem_sphere at h hp, rw [←hp, dist_smul_vadd_eq_dist _ _ hv] at h, rcases h with h | h; simp [h] } end /-- `second_inter` is unchanged by multiplying the vector by a nonzero real. -/ @[simp] lemma sphere.second_inter_smul (s : sphere P) (p : P) (v : V) {r : ℝ} (hr : r ≠ 0) : s.second_inter p (r • v) = s.second_inter p v := begin simp_rw [sphere.second_inter, real_inner_smul_left, inner_smul_right, smul_smul, div_mul_eq_div_div], rw [mul_comm, ←mul_div_assoc, ←mul_div_assoc, mul_div_cancel_left _ hr, mul_comm, mul_assoc, mul_div_cancel_left _ hr, mul_comm] end /-- `second_inter` is unchanged by negating the vector. -/ @[simp] lemma sphere.second_inter_neg (s : sphere P) (p : P) (v : V) : s.second_inter p (-v) = s.second_inter p v := by rw [←neg_one_smul ℝ v, s.second_inter_smul p v (by norm_num : (-1 : ℝ) ≠ 0)] /-- Applying `second_inter` twice returns the original point. -/ @[simp] lemma sphere.second_inter_second_inter (s : sphere P) (p : P) (v : V) : s.second_inter (s.second_inter p v) v = p := begin by_cases hv : v = 0, { simp [hv] }, have hv' : ⟪v, v⟫ ≠ 0 := inner_self_ne_zero.2 hv, simp only [sphere.second_inter, vadd_vsub_assoc, vadd_vadd, inner_add_right, inner_smul_right, div_mul_cancel _ hv'], rw [←@vsub_eq_zero_iff_eq V, vadd_vsub, ←add_smul, ←add_div], convert zero_smul ℝ _, convert zero_div _, ring end /-- If the vector passed to `second_inter` is given by a subtraction involving the point in `second_inter`, the result of `second_inter` may be expressed using `line_map`. -/ lemma sphere.second_inter_eq_line_map (s : sphere P) (p p' : P) : s.second_inter p (p' -ᵥ p) = affine_map.line_map p p' (-2 * ⟪p' -ᵥ p, p -ᵥ s.center⟫ / ⟪p' -ᵥ p, p' -ᵥ p⟫) := rfl /-- If the vector passed to `second_inter` is given by a subtraction involving the point in `second_inter`, the result lies in the span of the two points. -/ lemma sphere.second_inter_vsub_mem_affine_span (s : sphere P) (p₁ p₂ : P) : s.second_inter p₁ (p₂ -ᵥ p₁) ∈ line[ℝ, p₁, p₂] := smul_vsub_vadd_mem_affine_span_pair _ _ _ /-- If the vector passed to `second_inter` is given by a subtraction involving the point in `second_inter`, the three points are collinear. -/ lemma sphere.second_inter_collinear (s : sphere P) (p p' : P) : collinear ℝ ({p, p', s.second_inter p (p' -ᵥ p)} : set P) := begin rw [set.pair_comm, set.insert_comm], exact (collinear_insert_iff_of_mem_affine_span (s.second_inter_vsub_mem_affine_span _ _)).2 (collinear_pair ℝ _ _) end /-- If the vector passed to `second_inter` is given by a subtraction involving the point in `second_inter`, and the second point is not outside the sphere, the second point is weakly between the first point and the result of `second_inter`. -/ lemma sphere.wbtw_second_inter {s : sphere P} {p p' : P} (hp : p ∈ s) (hp' : dist p' s.center ≤ s.radius) : wbtw ℝ p p' (s.second_inter p (p' -ᵥ p)) := begin by_cases h : p' = p, { simp [h] }, refine wbtw_of_collinear_of_dist_center_le_radius (s.second_inter_collinear p p') hp hp' ((sphere.second_inter_mem _).2 hp) _, intro he, rw [eq_comm, sphere.second_inter_eq_self_iff, ←neg_neg (p' -ᵥ p), inner_neg_left, neg_vsub_eq_vsub_rev, neg_eq_zero, eq_comm] at he, exact ((inner_pos_or_eq_of_dist_le_radius hp hp').resolve_right (ne.symm h)).ne he end /-- If the vector passed to `second_inter` is given by a subtraction involving the point in `second_inter`, and the second point is inside the sphere, the second point is strictly between the first point and the result of `second_inter`. -/ lemma sphere.sbtw_second_inter {s : sphere P} {p p' : P} (hp : p ∈ s) (hp' : dist p' s.center < s.radius) : sbtw ℝ p p' (s.second_inter p (p' -ᵥ p)) := begin refine ⟨sphere.wbtw_second_inter hp hp'.le, _, _⟩, { rintro rfl, rw mem_sphere at hp, simpa [hp] using hp' }, { rintro h, rw [h, mem_sphere.1 ((sphere.second_inter_mem _).2 hp)] at hp', exact lt_irrefl _ hp' } end end euclidean_geometry
413a3e7fd8f05548a73722e433a66ccfa767d2f5
4da0c8e61fcd6ec3f3be47ee14a038850c03d0c3
/src/encodable.lean
84e386dd827fc2b385a4c7c6a7ef031bf72911be
[ "Apache-2.0" ]
permissive
bbentzen/mpl
fcbea60204bc8fd64667e0f76a5cebf4b67fb6ca
bb5066ec51fa11a4b66f440c4f6c9a3d8fb2e0de
refs/heads/master
1,625,175,849,308
1,624,207,634,000
1,624,207,634,000
142,774,375
9
0
null
null
null
null
UTF-8
Lean
false
false
3,121
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import data.equiv.encodable data.equiv.list data.fin data.finset data.fintype /- `Wfin α ar` is the type of finitely branching trees with labels from α, where a node labeled `a` has `ar a` children. -/ inductive Wfin {α : Type*} (ar : α → ℕ) | mk (a : α) (f : fin (ar a) → Wfin) : Wfin namespace Wfin variables {α : Type*} {ar : α → ℕ} def depth : Wfin ar → ℕ | ⟨a, f⟩ := finset.sup finset.univ (λ n, depth (f n)) + 1 def not_depth_le_zero (t : Wfin ar) : ¬ t.depth ≤ 0 := by { cases t, apply nat.not_succ_le_zero } lemma depth_lt_depth_mk (a : α) (f : fin (ar a) → Wfin ar) (i : fin (ar a)) : depth (f i) < depth ⟨a, f⟩ := nat.lt_succ_of_le (finset.le_sup (finset.mem_univ i)) end Wfin /- Show `Wfin` types are encodable. -/ namespace encodable @[reducible] private def Wfin' {α : Type*} (ar : α → ℕ) (n : ℕ) := { t : Wfin ar // t.depth ≤ n} variables {α : Type*} {ar : α → ℕ} private def encodable_zero : encodable (Wfin' ar 0) := let f : Wfin' ar 0 → empty := λ ⟨x, h⟩, absurd h (Wfin.not_depth_le_zero _), finv : empty → Wfin' ar 0 := by { intro x, cases x} in have ∀ x, finv (f x) = x, from λ ⟨x, h⟩, absurd h (Wfin.not_depth_le_zero _), encodable.of_left_inverse f finv this private def f (n : ℕ) : Wfin' ar (n + 1) → Σ a : α, fin (ar a) → Wfin' ar n | ⟨t, h⟩ := begin cases t with a f, have h₀ : ∀ i : fin (ar a), Wfin.depth (f i) ≤ n, from λ i, nat.le_of_lt_succ (lt_of_lt_of_le (Wfin.depth_lt_depth_mk a f i) h), exact ⟨a, λ i : fin (ar a), ⟨f i, h₀ i⟩⟩ end private def finv (n : ℕ) : (Σ a : α, fin (ar a) → Wfin' ar n) → Wfin' ar (n + 1) | ⟨a, f⟩ := let f' := λ i : fin (ar a), (f i).val in have Wfin.depth ⟨a, f'⟩ ≤ n + 1, from add_le_add_right (finset.sup_le (λ b h, (f b).2)) 1, ⟨⟨a, f'⟩, this⟩ variable [encodable α] private def encodable_succ (n : nat) (h : encodable (Wfin' ar n)) : encodable (Wfin' ar (n + 1)) := encodable.of_left_inverse (f n) (finv n) (by { intro t, cases t with t h, cases t with a fn, reflexivity }) instance : encodable (Wfin ar) := begin haveI h' : Π n, encodable (Wfin' ar n) := λ n, nat.rec_on n encodable_zero encodable_succ, let f : Wfin ar → Σ n, Wfin' ar n := λ t, ⟨t.depth, ⟨t, le_refl _⟩⟩, let finv : (Σ n, Wfin' ar n) → Wfin ar := λ p, p.2.1, have : ∀ t, finv (f t) = t, from λ t, rfl, exact encodable.of_left_inverse f finv this end end encodable /- Make it easier to construct funtions from a small `fin`. -/ namespace fin variable {α : Type*} def mk_fn0 : fin 0 → α | ⟨_, h⟩ := absurd h dec_trivial def mk_fn1 (t : α) : fin 1 → α | ⟨0, _⟩ := t | ⟨n+1, h⟩ := absurd h dec_trivial def mk_fn2 (s t : α) : fin 2 → α | ⟨0, _⟩ := s | ⟨1, _⟩ := t | ⟨n+2, h⟩ := absurd h dec_trivial attribute [simp] mk_fn0 mk_fn1 mk_fn2 end fin
f55299f279ae721315208fcf1aabf33169b91094
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/module/opposites.lean
b527c86de9ff60d77f6325c223d26db75def6589
[ "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
1,671
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.group_action.opposite import data.equiv.module /-! # Module operations on `Mᵐᵒᵖ` This file contains definitions that build on top of the group action definitions in `group_theory.group_action.opposite`. -/ namespace mul_opposite universes u v variables (R : Type u) {M : Type v} [semiring R] [add_comm_monoid M] [module R M] /-- `mul_opposite.distrib_mul_action` extends to a `module` -/ instance : module R (mul_opposite M) := { add_smul := λ r₁ r₂ x, unop_injective $ add_smul r₁ r₂ (unop x), zero_smul := λ x, unop_injective $ zero_smul _ (unop x), ..mul_opposite.distrib_mul_action M R } /-- The function `op` is a linear equivalence. -/ def op_linear_equiv : M ≃ₗ[R] Mᵐᵒᵖ := { map_smul' := mul_opposite.op_smul, .. op_add_equiv } @[simp] lemma coe_op_linear_equiv : (op_linear_equiv R : M → Mᵐᵒᵖ) = op := rfl @[simp] lemma coe_op_linear_equiv_symm : ((op_linear_equiv R).symm : Mᵐᵒᵖ → M) = unop := rfl @[simp] lemma coe_op_linear_equiv_to_linear_map : ((op_linear_equiv R).to_linear_map : M → Mᵐᵒᵖ) = op := rfl @[simp] lemma coe_op_linear_equiv_symm_to_linear_map : ((op_linear_equiv R).symm.to_linear_map : Mᵐᵒᵖ → M) = unop := rfl @[simp] lemma op_linear_equiv_to_add_equiv : (op_linear_equiv R : M ≃ₗ[R] Mᵐᵒᵖ).to_add_equiv = op_add_equiv := rfl @[simp] lemma op_linear_equiv_symm_to_add_equiv : (op_linear_equiv R : M ≃ₗ[R] Mᵐᵒᵖ).symm.to_add_equiv = op_add_equiv.symm := rfl end mul_opposite
a6c85bf8a85da798326e74f4a587eb359d16089f
8e31b9e0d8cec76b5aa1e60a240bbd557d01047c
/scratch/old_simplex.lean
7185212efbdac47c51e4e9d6bb880337473da8a0
[]
no_license
ChrisHughes24/LP
7bdd62cb648461c67246457f3ddcb9518226dd49
e3ed64c2d1f642696104584e74ae7226d8e916de
refs/heads/master
1,685,642,642,858
1,578,070,602,000
1,578,070,602,000
195,268,102
4
3
null
1,569,229,518,000
1,562,255,287,000
Lean
UTF-8
Lean
false
false
41,769
lean
import tableau order.lexicographic open matrix fintype finset function pequiv partition variables {m n : ℕ} local notation `rvec`:2000 n := matrix (fin 1) (fin n) ℚ local notation `cvec`:2000 m := matrix (fin m) (fin 1) ℚ local infix ` ⬝ `:70 := matrix.mul local postfix `ᵀ` : 1500 := transpose namespace tableau def pivot_linear_order (T : tableau m n) : decidable_linear_order (fin n) := decidable_linear_order.lift T.to_partition.colg (injective_colg _) (by apply_instance) def find_pivot_column (T : tableau m n) (obj : fin m) : option (fin n) := option.cases_on (fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted)) (((list.fin_range n).filter (λ c : fin n, 0 < T.to_matrix obj c)).argmin T.to_partition.colg) some def pivot_row_linear_order (T : tableau m n) (c : fin n) : decidable_linear_order (fin m) := decidable_linear_order.lift (show fin m → lex ℚ (fin (m + n)), from λ r', (abs (T.const r' 0 / T.to_matrix r' c), T.to_partition.rowg r')) (λ x y, by simp [T.to_partition.injective_rowg.eq_iff]) (by apply_instance) section local attribute [instance, priority 0] fin.has_le fin.decidable_linear_order lemma pivot_row_linear_order_le_def (T : tableau m n) (c : fin n) : @has_le.le (fin m) (by haveI := pivot_row_linear_order T c; apply_instance) = (λ i i', abs (T.const i 0 / T.to_matrix i c) < abs (T.const i' 0 / T.to_matrix i' c) ∨ (abs (T.const i 0 / T.to_matrix i c) = abs (T.const i' 0 / T.to_matrix i' c) ∧ T.to_partition.rowg i ≤ T.to_partition.rowg i')) := funext $ λ i, funext $ λ i', propext $ prod.lex_def _ _ end def find_pivot_row (T : tableau m n) (obj: fin m) (c : fin n) : option (fin m) := let l := (list.fin_range m).filter (λ r : fin m, obj ≠ r ∧ T.to_partition.rowg r ∈ T.restricted ∧ T.to_matrix obj c / T.to_matrix r c < 0) in @list.minimum _ (pivot_row_linear_order T c) l lemma find_pivot_column_spec {T : tableau m n} {obj : fin m} {c : fin n} : c ∈ find_pivot_column T obj → (T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted) ∨ (0 < T.to_matrix obj c ∧ T.to_partition.colg c ∈ T.restricted) := begin simp [find_pivot_column], cases h : fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted), { finish [h, fin.find_eq_some_iff, fin.find_eq_none_iff, lt_irrefl (0 : ℚ), list.argmin_eq_some_iff] }, { finish [fin.find_eq_some_iff] } end lemma nonpos_of_lt_find_pivot_column {T : tableau m n} {obj : fin m} {c j : fin n} (hc : c ∈ find_pivot_column T obj) (hcres : T.to_partition.colg c ∈ T.restricted) (hjc : T.to_partition.colg j < T.to_partition.colg c) : T.to_matrix obj j ≤ 0 := begin rw [find_pivot_column] at hc, cases h : fin.find (λ c, T.to_matrix obj c ≠ 0 ∧ colg (T.to_partition) c ∉ T.restricted), { rw h at hc, refine le_of_not_lt (λ hj0, _), exact not_le_of_gt hjc ((list.mem_argmin_iff.1 hc).2.1 j (list.mem_filter.2 (by simp [hj0]))) }, { rw h at hc, simp [*, fin.find_eq_some_iff] at * } end lemma find_pivot_column_eq_none {T : tableau m n} {obj : fin m} (hT : T.feasible) (h : find_pivot_column T obj = none) : T.is_optimal (T.of_col 0) (T.to_partition.rowg obj) := is_optimal_of_col_zero hT begin revert h, simp [find_pivot_column], cases h : fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted), { finish [fin.find_eq_none_iff, list.argmin_eq_some_iff, list.filter_eq_nil] }, { simp [h] } end lemma find_pivot_row_spec {T : tableau m n} {obj r : fin m} {c : fin n} : r ∈ find_pivot_row T obj c → obj ≠ r ∧ T.to_partition.rowg r ∈ T.restricted ∧ T.to_matrix obj c / T.to_matrix r c < 0 ∧ (∀ r' : fin m, obj ≠ r' → T.to_partition.rowg r' ∈ T.restricted → T.to_matrix obj c / T.to_matrix r' c < 0 → abs (T.const r 0 / T.to_matrix r c) ≤ abs (T.const r' 0 / T.to_matrix r' c)) := begin simp only [list.mem_filter, find_pivot_row, option.mem_def, with_bot.some_eq_coe, list.minimum_eq_coe_iff, list.mem_fin_range, true_and, and_imp], rw [pivot_row_linear_order_le_def], intros hor hres hr0 h, simp only [*, true_and, ne.def, not_false_iff], intros r' hor' hres' hr0', cases h r' hor' hres' hr0', { exact le_of_lt (by assumption) }, { exact le_of_eq (by tauto) } end lemma nonneg_of_lt_find_pivot_row {T : tableau m n} {obj : fin m} {r i : fin m} {c : fin n} (hc0 : 0 < T.to_matrix obj c) (hres : T.to_partition.rowg i ∈ T.restricted) (hc : c ∈ find_pivot_column T obj) (hr : r ∈ find_pivot_row T obj c) (hconst : T.const i 0 = 0) (hjc : T.to_partition.rowg i < T.to_partition.rowg r) : 0 ≤ T.to_matrix i c := if hobj : obj = i then le_of_lt $ hobj ▸ hc0 else le_of_not_gt $ λ hic, not_le_of_lt hjc begin have := ((@list.minimum_eq_coe_iff _ (id _) _ _).1 hr).2 i (list.mem_filter.2 ⟨list.mem_fin_range _, hobj, hres, div_neg_of_pos_of_neg hc0 hic⟩), rw [pivot_row_linear_order_le_def] at this, simp [hconst, not_lt_of_ge (abs_nonneg _), *] at * end lemma ne_zero_of_mem_find_pivot_row {T : tableau m n} {obj r : fin m} {c : fin n} (hr : r ∈ find_pivot_row T obj c) : T.to_matrix r c ≠ 0 := assume hrc, by simpa [lt_irrefl, hrc] using find_pivot_row_spec hr lemma ne_zero_of_mem_find_pivot_column {T : tableau m n} {obj : fin m} {c : fin n} (hc : c ∈ find_pivot_column T obj) : T.to_matrix obj c ≠ 0 := λ h, by simpa [h, lt_irrefl] using find_pivot_column_spec hc lemma find_pivot_row_eq_none_aux {T : tableau m n} {obj : fin m} {c : fin n} (hrow : find_pivot_row T obj c = none) (hs : c ∈ find_pivot_column T obj) : ∀ r, obj ≠ r → T.to_partition.rowg r ∈ T.restricted → 0 ≤ T.to_matrix obj c / T.to_matrix r c := by simpa [find_pivot_row, list.filter_eq_nil] using hrow lemma find_pivot_row_eq_none {T : tableau m n} {obj : fin m} {c : fin n} (hT : T.feasible) (hrow : find_pivot_row T obj c = none) (hs : c ∈ find_pivot_column T obj) : T.is_unbounded_above (T.to_partition.rowg obj) := have hrow : ∀ r, obj ≠ r → T.to_partition.rowg r ∈ T.restricted → 0 ≤ T.to_matrix obj c / T.to_matrix r c, from find_pivot_row_eq_none_aux hrow hs, have hc : (T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted) ∨ (0 < T.to_matrix obj c ∧ T.to_partition.colg c ∈ T.restricted), from find_pivot_column_spec hs, have hToc : T.to_matrix obj c ≠ 0, from λ h, by simpa [h, lt_irrefl] using hc, (lt_or_gt_of_ne hToc).elim (λ hToc : T.to_matrix obj c < 0, is_unbounded_above_rowg_of_nonpos hT c (hc.elim and.right (λ h, (not_lt_of_gt hToc h.1).elim)) (λ i hi, classical.by_cases (λ hoi : obj = i, le_of_lt (hoi ▸ hToc)) (λ hoi : obj ≠ i, inv_nonpos.1 $ nonpos_of_mul_nonneg_right (hrow _ hoi hi) hToc)) hToc) (λ hToc : 0 < T.to_matrix obj c, is_unbounded_above_rowg_of_nonneg hT c (λ i hi, classical.by_cases (λ hoi : obj = i, le_of_lt (hoi ▸ hToc)) (λ hoi : obj ≠ i, inv_nonneg.1 $ nonneg_of_mul_nonneg_left (hrow _ hoi hi) hToc)) hToc) def feasible_of_mem_pivot_row_and_column {T : tableau m n} {obj : fin m} (hT : T.feasible) {c} (hc : c ∈ find_pivot_column T obj) {r} (hr : r ∈ find_pivot_row T obj c) : feasible (T.pivot r c) := begin have := find_pivot_column_spec hc, have := find_pivot_row_spec hr, have := @feasible_simplex_pivot _ _ _ obj hT r c, tauto end section blands_rule local attribute [instance, priority 0] classical.dec variable (obj : fin m) lemma not_unique_row_and_unique_col {T T' : tableau m n} {r c c'} (hcobj0 : 0 < T.to_matrix obj c) (hc'obj0 : 0 < T'.to_matrix obj c') (hrc0 : T.to_matrix r c < 0) (hflat : T.flat = T'.flat) (hs : T.to_partition.rowg r = T'.to_partition.colg c') (hrobj : T.to_partition.rowg obj = T'.to_partition.rowg obj) (hfickle : ∀ i, T.to_partition.rowg i ≠ T'.to_partition.rowg i → T.const i 0 = 0) (hobj : T.const obj 0 = T'.const obj 0) (nonpos_of_colg_ne : ∀ j, T'.to_partition.colg j ≠ T.to_partition.colg j → j ≠ c' → T'.to_matrix obj j ≤ 0) (nonpos_of_colg_eq : ∀ j, j ≠ c' → T'.to_partition.colg j = T.to_partition.colg c → T'.to_matrix obj j ≤ 0) (unique_row : ∀ i ≠ r, T.const i 0 = 0 → T.to_partition.rowg i ≠ T'.to_partition.rowg i → 0 ≤ T.to_matrix i c) : false := let objr := T.to_partition.rowg obj in let x := λ y : ℚ, T.of_col (y • (single c 0).to_matrix) in have hxflatT' : ∀ {y}, x y ∈ flat T', from hflat ▸ λ _, of_col_mem_flat _ _, have hxrow : ∀ y i, x y (T.to_partition.rowg i) 0 = T.const i 0 + y * T.to_matrix i c, by simp [x, of_col_single_rowg], have hxcol : ∀ {y j}, j ≠ c → x y (T.to_partition.colg j) 0 = 0, from λ y j hjc, by simp [x, of_col_colg, pequiv.to_matrix, single_apply_of_ne hjc.symm], have hxcolc : ∀ {y}, x y (T.to_partition.colg c) 0 = y, by simp [x, of_col_colg, pequiv.to_matrix], let c_star : fin (m + n) → ℚ := λ v, option.cases_on (T'.to_partition.colp.symm v) 0 (T'.to_matrix obj) in have hxobj : ∀ y, x y objr 0 = T.const obj 0 + y * T.to_matrix obj c, from λ y, hxrow _ _, have hgetr : ∀ {y v}, c_star v * x y v 0 ≠ 0 → (T'.to_partition.colp.symm v).is_some, from λ y v, by cases h : T'.to_partition.colp.symm v; dsimp [c_star]; rw h; simp, have c_star_eq_get : ∀ {v} (hv : (T'.to_partition.colp.symm v).is_some), c_star v = T'.to_matrix obj (option.get hv), from λ v hv, by dsimp only [c_star]; conv_lhs{rw [← option.some_get hv]}; refl, have hsummmn : ∀ {y}, sum univ (λ j, T'.to_matrix obj j * x y (T'.to_partition.colg j) 0) = sum univ (λ v, c_star v * x y v 0), from λ y, sum_bij_ne_zero (λ j _ _, T'.to_partition.colg j) (λ _ _ _, mem_univ _) (λ _ _ _ _ _ _ h, T'.to_partition.injective_colg h) (λ v _ h0, ⟨option.get (hgetr h0), mem_univ _, by rw [← c_star_eq_get (hgetr h0)]; simpa using h0, by simp⟩) (λ _ _ h0, by dsimp [c_star]; rw [colp_colg]), have hgetc : ∀ {y v}, c_star v * x y v 0 ≠ 0 → v ≠ T.to_partition.colg c → (T.to_partition.rowp.symm v).is_some, from λ y v, (eq_rowg_or_colg T.to_partition v).elim (λ ⟨i, hi⟩, by rw [hi, rowp_rowg]; simp) (λ ⟨j, hj⟩ h0 hvc, by rw [hj, hxcol (mt (congr_arg T.to_partition.colg) (hvc ∘ hj.trans)), mul_zero] at h0; exact (h0 rfl).elim), have hsummmnn : ∀ {y}, (univ.erase (T.to_partition.colg c)).sum (λ v, c_star v * x y v 0) = univ.sum (λ i, c_star (T.to_partition.rowg i) * x y (T.to_partition.rowg i) 0), from λ y, eq.symm $ sum_bij_ne_zero (λ i _ _, T.to_partition.rowg i) (by simp) (λ _ _ _ _ _ _ h, T.to_partition.injective_rowg h) (λ v hvc h0, ⟨option.get (hgetc h0 (mem_erase.1 hvc).1), mem_univ _, by simpa using h0⟩) (by intros; refl), have hsumm : ∀ {y}, univ.sum (λ i, c_star (T.to_partition.rowg i) * x y (T.to_partition.rowg i) 0) = univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0) + y * univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), from λ y, by simp only [hxrow, mul_add, add_mul, sum_add_distrib, mul_assoc, mul_left_comm _ y, mul_sum.symm], have hxobj' : ∀ y, x y objr 0 = univ.sum (λ v, c_star v * x y v 0) + T'.const obj 0, from λ y, by dsimp [objr]; rw [hrobj, mem_flat_iff.1 hxflatT', hsummmn], have hy : ∀ {y}, y * T.to_matrix obj c = c_star (T.to_partition.colg c) * y + univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0) + y * univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), from λ y, by rw [← add_left_inj (T.const obj 0), ← hxobj, hxobj', ← insert_erase (mem_univ (T.to_partition.colg c)), sum_insert (not_mem_erase _ _), hsummmnn, hobj, hsumm, hxcolc]; simp, have hy' : ∀ (y), y * (T.to_matrix obj c - c_star (T.to_partition.colg c) - univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c)) = univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0), from λ y, by rw [mul_sub, mul_sub, hy]; simp [mul_comm, mul_assoc, mul_left_comm], have h0 : T.to_matrix obj c - c_star (T.to_partition.colg c) - univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c) = 0, by rw [← (domain.mul_left_inj (@one_ne_zero ℚ _)), hy', ← hy' 0, zero_mul, mul_zero], have hcolnec' : T'.to_partition.colp.symm (T.to_partition.colg c) ≠ some c', from λ h, by simpa [hs.symm] using congr_arg T'.to_partition.colg (option.eq_some_iff_get_eq.1 h).snd, have eq_of_roweqc' : ∀ {i}, T'.to_partition.colp.symm (T.to_partition.rowg i) = some c' → i = r, from λ i h, by simpa [hs.symm, T.to_partition.injective_rowg.eq_iff] using congr_arg T'.to_partition.colg (option.eq_some_iff_get_eq.1 h).snd, have sumpos : 0 < univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), by rw [← sub_eq_zero.1 h0]; exact add_pos_of_pos_of_nonneg hcobj0 (begin simp only [c_star, neg_nonneg], cases h : T'.to_partition.colp.symm (T.to_partition.colg c) with j, { refl }, { exact nonpos_of_colg_eq j (mt (congr_arg some) (h ▸ hcolnec')) (by rw [← (option.eq_some_iff_get_eq.1 h).snd]; simp) } end), have hexi : ∃ i, 0 < c_star (T.to_partition.rowg i) * T.to_matrix i c, from imp_of_not_imp_not _ _ (by simpa using @sum_nonpos _ _ (@univ (fin m) _) (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c) _ _) sumpos, let ⟨i, hi⟩ := hexi in have hi0 : T.const i 0 = 0, from hfickle i (λ h, by dsimp [c_star] at hi; rw [h, colp_rowg_eq_none] at hi; simpa [lt_irrefl] using hi), have hi_some : (T'.to_partition.colp.symm (T.to_partition.rowg i)).is_some, from option.ne_none_iff_is_some.1 (λ h, by dsimp only [c_star] at hi; rw h at hi; simpa [lt_irrefl] using hi), have hi' : 0 < T'.to_matrix obj (option.get hi_some) * T.to_matrix i c, by dsimp only [c_star] at hi; rwa [← option.some_get hi_some] at hi, have hir : i ≠ r, from λ hir, begin have : option.get hi_some = c', from T'.to_partition.injective_colg (by rw [colg_get_colp_symm, ← hs, hir]), rw [this, hir] at hi', exact not_lt_of_gt hi' (mul_neg_of_pos_of_neg hc'obj0 hrc0) end, have hnec' : option.get hi_some ≠ c', from λ eq_c', hir $ @eq_of_roweqc' i (eq_c' ▸ by simp), have hic0 : T.to_matrix i c < 0, from neg_of_mul_pos_right hi' (nonpos_of_colg_ne _ (by simp) hnec'), not_le_of_gt hic0 (unique_row _ hir hi0 (by rw [← colg_get_colp_symm _ _ hi_some]; exact colg_ne_rowg _ _ _)) inductive rel : tableau m n → tableau m n → Prop | pivot : ∀ {T}, feasible T → ∀ {r c}, c ∈ find_pivot_column T obj → r ∈ find_pivot_row T obj c → rel (T.pivot r c) T | trans_pivot : ∀ {T₁ T₂ r c}, rel T₁ T₂ → c ∈ find_pivot_column T₁ obj → r ∈ find_pivot_row T₁ obj c → rel (T₁.pivot r c) T₂ lemma feasible_of_rel_right {T T' : tableau m n} (h : rel obj T' T) : T.feasible := rel.rec_on h (by tauto) (by tauto) lemma feasible_of_rel_left {T T' : tableau m n} (h : rel obj T' T) : T'.feasible := rel.rec_on h (λ _ hT _ _ hc hr, feasible_of_mem_pivot_row_and_column hT hc hr) (λ _ _ _ _ _ hc hr hT, feasible_of_mem_pivot_row_and_column hT hc hr) /-- Slightly stronger recursor than the default recursor -/ @[elab_as_eliminator] lemma rel.rec_on' {obj : fin m} {C : tableau m n → tableau m n → Prop} {T T' : tableau m n} (hrel : rel obj T T') (hpivot : ∀ {T : tableau m n} {r : fin m} {c : fin n}, feasible T → c ∈ find_pivot_column T obj → r ∈ find_pivot_row T obj c → C (pivot T r c) T) (hpivot_trans : ∀ {T₁ T₂ : tableau m n} {r : fin m} {c : fin n}, rel obj (T₁.pivot r c) T₁ → rel obj T₁ T₂ → c ∈ find_pivot_column T₁ obj → r ∈ find_pivot_row T₁ obj c → C (T₁.pivot r c) T₁ → C T₁ T₂ → C (pivot T₁ r c) T₂) : C T T' := rel.rec_on hrel (λ T hT r c hc hr, hpivot hT hc hr) (λ T₁ T₂ r c hrelT₁₂ hc hr ih, hpivot_trans (rel.pivot (feasible_of_rel_left obj hrelT₁₂) hc hr) hrelT₁₂ hc hr (hpivot (feasible_of_rel_left obj hrelT₁₂) hc hr) ih) lemma rel.trans {obj : fin m} {T₁ T₂ T₃ : tableau m n} (h₁₂ : rel obj T₁ T₂) : rel obj T₂ T₃ → rel obj T₁ T₃ := rel.rec_on h₁₂ (λ T r c hT hc hr hrelT, rel.trans_pivot hrelT hc hr) (λ T₁ T₂ r c hrelT₁₂ hc hr ih hrelT₂₃, rel.trans_pivot (ih hrelT₂₃) hc hr) instance : is_trans (tableau m n) (rel obj) := ⟨@rel.trans _ _ obj⟩ lemma flat_eq_of_rel {T T' : tableau m n} (h : rel obj T' T) : flat T' = flat T := rel.rec_on' h (λ _ _ _ _ _ hr, flat_pivot (ne_zero_of_mem_find_pivot_row hr)) (λ _ _ _ _ _ _ _ _, eq.trans) lemma rowg_obj_eq_of_rel {T T' : tableau m n} (h : rel obj T T') : T.to_partition.rowg obj = T'.to_partition.rowg obj := rel.rec_on' h (λ T r c hfT hc hr, by simp [rowg_swap_of_ne _ (find_pivot_row_spec hr).1]) (λ _ _ _ _ _ _ _ _, eq.trans) lemma restricted_eq_of_rel {T T' : tableau m n} (h : rel obj T T') : T.restricted = T'.restricted := rel.rec_on' h (λ _ _ _ _ _ _, rfl) (λ _ _ _ _ _ _ _ _, eq.trans) lemma exists_mem_pivot_row_column_of_rel {T T' : tableau m n} (h : rel obj T' T) : ∃ r c, c ∈ find_pivot_column T obj ∧ r ∈ find_pivot_row T obj c := rel.rec_on' h (λ _ r c _ hc hr, ⟨r, c, hc, hr⟩) (λ _ _ _ _ _ _ _ _ _, id) lemma exists_mem_pivot_row_of_rel {T T' : tableau m n} (h : rel obj T' T) {c : fin n} (hc : c ∈ find_pivot_column T obj) : ∃ r, r ∈ find_pivot_row T obj c := let ⟨r, c', hc', hr⟩ := exists_mem_pivot_row_column_of_rel obj h in ⟨r, by simp * at *⟩ lemma colg_eq_or_exists_mem_pivot_column {T₁ T₂ : tableau m n} (h : rel obj T₂ T₁) {c : fin n} : T₁.to_partition.colg c = T₂.to_partition.colg c ∨ ∃ T₃, (T₃ = T₁ ∨ rel obj T₃ T₁) ∧ (rel obj T₂ T₃) ∧ T₃.to_partition.colg c = T₁.to_partition.colg c ∧ c ∈ find_pivot_column T₃ obj := rel.rec_on' h begin assume T r c' hT hc' hr, by_cases hcc : c = c', { subst hcc, exact or.inr ⟨T, or.inl rfl, rel.pivot hT hc' hr, rfl, hc'⟩ }, { simp [colg_swap_of_ne _ hcc] } end (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂, ih₁₂.elim (λ ih₁₂, ihp₁.elim (λ ihp₁, or.inl (ih₁₂.trans ihp₁)) (λ ⟨T₃, hT₃⟩, or.inr ⟨T₃, hT₃.1.elim (λ h, h.symm ▸ or.inr hrel₁₂) (λ h, or.inr $ h.trans hrel₁₂), hT₃.2.1, hT₃.2.2.1.trans ih₁₂.symm, hT₃.2.2.2⟩)) (λ ⟨T₃, hT₃⟩, or.inr ⟨T₃, hT₃.1, hrelp₁.trans hT₃.2.1, hT₃.2.2⟩)) lemma rowg_eq_or_exists_mem_pivot_row {T₁ T₂ : tableau m n} (h : rel obj T₂ T₁) (r : fin m) : T₁.to_partition.rowg r = T₂.to_partition.rowg r ∨ ∃ (T₃ : tableau m n) c, (T₃ = T₁ ∨ rel obj T₃ T₁) ∧ (rel obj T₂ T₃) ∧ T₃.to_partition.rowg r = T₁.to_partition.rowg r ∧ c ∈ find_pivot_column T₃ obj ∧ r ∈ find_pivot_row T₃ obj c := rel.rec_on' h begin assume T r' c hT hc hr', by_cases hrr : r = r', { subst hrr, exact or.inr ⟨T, c, or.inl rfl, rel.pivot hT hc hr', rfl, hc, hr'⟩ }, { simp [rowg_swap_of_ne _ hrr] } end (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂, ih₁₂.elim (λ ih₁₂, ihp₁.elim (λ ihp₁, or.inl $ ih₁₂.trans ihp₁) (λ ⟨T₃, c', hT₃⟩, or.inr ⟨T₃, c', hT₃.1.elim (λ h, h.symm ▸ or.inr hrel₁₂) (λ h, or.inr $ h.trans hrel₁₂), hT₃.2.1, ih₁₂.symm ▸ hT₃.2.2.1, hT₃.2.2.2⟩)) (λ ⟨T₃, c', hT₃⟩, or.inr ⟨T₃, c', hT₃.1, (rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr).trans hT₃.2.1, hT₃.2.2⟩)) lemma eq_or_rel_pivot_of_rel {T₁ T₂ : tableau m n} (h : rel obj T₁ T₂) : ∀ {r c} (hc : c ∈ find_pivot_column T₂ obj) (hr : r ∈ find_pivot_row T₂ obj c), T₁ = T₂.pivot r c ∨ rel obj T₁ (T₂.pivot r c) := rel.rec_on' h (λ T r c hT hc hr r' c' hc' hr', by simp * at *) (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂ r' c' hc' hr', (ih₁₂ hc' hr').elim (λ ih₁₂, or.inr $ ih₁₂ ▸ rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr) (λ ih₁₂, or.inr $ (rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr).trans ih₁₂)) lemma exists_mem_pivot_column_of_mem_pivot_row {T : tableau m n} (hrelTT : rel obj T T) {r c} (hc : c ∈ find_pivot_column T obj) (hr : r ∈ find_pivot_row T obj c) : ∃ (T' : tableau m n), c ∈ find_pivot_column T' obj ∧ T'.to_partition.colg c = T.to_partition.rowg r ∧ rel obj T' T ∧ rel obj T T' := have hrelTTp : rel obj T (T.pivot r c), from (eq_or_rel_pivot_of_rel _ hrelTT hc hr).elim (λ h, h ▸ hrelTT ) id, let ⟨T', hT'⟩ := (colg_eq_or_exists_mem_pivot_column obj hrelTTp).resolve_left (show (T.pivot r c).to_partition.colg c ≠ T.to_partition.colg c, by simp) in ⟨T', hT'.2.2.2, by simp [hT'.2.2.1], hT'.1.elim (λ h, h.symm ▸ rel.pivot (feasible_of_rel_left _ hrelTT) hc hr) (λ h, h.trans $ rel.pivot (feasible_of_rel_left _ hrelTT) hc hr), hT'.2.1⟩ lemma exists_mem_pivot_column_of_rowg_ne {T T' : tableau m n} (hrelTT' : rel obj T T') {r : fin m} (hrelT'T : rel obj T' T) (hrow : T.to_partition.rowg r ≠ T'.to_partition.rowg r) : ∃ (T₃ : tableau m n) c, c ∈ find_pivot_column T₃ obj ∧ T₃.to_partition.colg c = T.to_partition.rowg r ∧ rel obj T₃ T ∧ rel obj T T₃ := let ⟨T₃, c, hT₃, hrelT₃T, hrow₃, hc, hr⟩ := (rowg_eq_or_exists_mem_pivot_row obj hrelT'T _).resolve_left hrow in let ⟨T₄, hT₄⟩ := exists_mem_pivot_column_of_mem_pivot_row obj (show rel obj T₃ T₃, from hT₃.elim (λ h, h.symm ▸ hrelTT'.trans hrelT'T) (λ h, h.trans $ hrelTT'.trans hrelT₃T)) hc hr in ⟨T₄, c, hT₄.1, hT₄.2.1.trans hrow₃, hT₄.2.2.1.trans $ hT₃.elim (λ h, h.symm ▸ hrelTT'.trans hrelT'T) (λ h, h.trans $ hrelTT'.trans hrelT'T), hrelTT'.trans (hrelT₃T.trans hT₄.2.2.2)⟩ lemma const_obj_le_of_rel {T₁ T₂ : tableau m n} (h : rel obj T₁ T₂) : T₂.const obj 0 ≤ T₁.const obj 0 := rel.rec_on' h (λ T r c hT hc hr, have hr' : _ := find_pivot_row_spec hr, simplex_const_obj_le hT (by tauto) (by tauto)) (λ _ _ _ _ _ _ _ _ h₁ h₂, le_trans h₂ h₁) lemma const_obj_eq_of_rel_of_rel {T₁ T₂ : tableau m n} (h₁₂ : rel obj T₁ T₂) (h₂₁ : rel obj T₂ T₁) : T₁.const obj 0 = T₂.const obj 0 := le_antisymm (const_obj_le_of_rel _ h₂₁) (const_obj_le_of_rel _ h₁₂) lemma const_eq_const_of_const_obj_eq {T₁ T₂ : tableau m n} (h₁₂ : rel obj T₁ T₂) : ∀ (hobj : T₁.const obj 0 = T₂.const obj 0) (i : fin m), T₁.const i 0 = T₂.const i 0 := rel.rec_on' h₁₂ (λ T r c hfT hc hr hobj i, have hr0 : T.const r 0 = 0, from const_eq_zero_of_const_obj_eq hfT (ne_zero_of_mem_find_pivot_column hc) (ne_zero_of_mem_find_pivot_row hr) (find_pivot_row_spec hr).1 hobj, if hir : i = r then by simp [hir, hr0] else by simp [const_pivot_of_ne _ hir, hr0]) (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂ hobj i, have hobjp : (pivot T₁ r c).const obj 0 = T₁.const obj 0, from le_antisymm (hobj.symm ▸ const_obj_le_of_rel _ hrel₁₂) (const_obj_le_of_rel _ hrelp₁), by rw [ihp₁ hobjp, ih₁₂ (hobjp.symm.trans hobj)]) lemma const_eq_zero_of_rowg_ne_of_rel_self {T T' : tableau m n} (hrelTT' : rel obj T T') (hrelT'T : rel obj T' T) (i : fin m) (hrow : T.to_partition.rowg i ≠ T'.to_partition.rowg i) : T.const i 0 = 0 := let ⟨T₃, c, hT₃₁, hT'₃, hrow₃, hc, hi⟩ := (rowg_eq_or_exists_mem_pivot_row obj hrelT'T _).resolve_left hrow in have T₃.const i 0 = 0, from const_eq_zero_of_const_obj_eq (feasible_of_rel_right _ hT'₃) (ne_zero_of_mem_find_pivot_column hc) (ne_zero_of_mem_find_pivot_row hi) (find_pivot_row_spec hi).1 (const_obj_eq_of_rel_of_rel _ (rel.pivot (feasible_of_rel_right _ hT'₃) hc hi) ((eq_or_rel_pivot_of_rel _ hT'₃ hc hi).elim (λ h, h ▸ hT₃₁.elim (λ h, h.symm ▸ hrelTT') (λ h, h.trans hrelTT')) (λ hrelT'p, hT₃₁.elim (λ h, h.symm ▸ hrelTT'.trans (h ▸ hrelT'p)) (λ h, h.trans $ hrelTT'.trans hrelT'p)))), have hobj : T₃.const obj 0 = T.const obj 0, from hT₃₁.elim (λ h, h ▸ rfl) (λ h, const_obj_eq_of_rel_of_rel _ h (hrelTT'.trans hT'₃)), hT₃₁.elim (λ h, h ▸ this) (λ h, const_eq_const_of_const_obj_eq obj h hobj i ▸ this) lemma colg_mem_restricted_of_rel_self {T : tableau m n} (hrelTT : rel obj T T) {c} (hc : c ∈ find_pivot_column T obj) : T.to_partition.colg c ∈ T.restricted := let ⟨r, hr⟩ := exists_mem_pivot_row_of_rel obj hrelTT hc in let ⟨T', c', hT', hrelTT', hrowcol, _, hr'⟩ := (rowg_eq_or_exists_mem_pivot_row obj ((eq_or_rel_pivot_of_rel _ hrelTT hc hr).elim (λ h, show rel obj T (T.pivot r c), from h ▸ hrelTT) id) _).resolve_left (show (T.pivot r c).to_partition.rowg r ≠ T.to_partition.rowg r, by simp) in (restricted_eq_of_rel _ hrelTT').symm ▸ by convert (find_pivot_row_spec hr').2.1; simp [hrowcol] lemma eq_zero_of_not_mem_restricted_of_rel_self {T : tableau m n} (hrelTT : rel obj T T) {j} (hjres : T.to_partition.colg j ∉ T.restricted) : T.to_matrix obj j = 0 := let ⟨r, c, hc, hr⟩ := exists_mem_pivot_row_column_of_rel obj hrelTT in have hcres : T.to_partition.colg c ∈ T.restricted, from colg_mem_restricted_of_rel_self obj hrelTT hc, by_contradiction $ λ h0, begin simp [find_pivot_column] at hc, cases h : fin.find (λ c, T.to_matrix obj c ≠ 0 ∧ colg (T.to_partition) c ∉ T.restricted), { simp [*, fin.find_eq_none_iff] at * }, { rw h at hc, clear_aux_decl, have := (fin.find_eq_some_iff.1 h).1, simp * at * } end lemma rel.irrefl {obj : fin m} : ∀ (T : tableau m n), ¬ rel obj T T := λ T1 hrelT1, let ⟨rT1 , cT1, hrT1, hcT1⟩ := exists_mem_pivot_row_column_of_rel obj hrelT1 in let ⟨t, ht⟩ := finset.max_of_mem (show T1.to_partition.colg cT1 ∈ univ.filter (λ v, ∃ (T' : tableau m n) (c : fin n), rel obj T' T' ∧ c ∈ find_pivot_column T' obj ∧ T'.to_partition.colg c = v), by simp only [true_and, mem_filter, mem_univ, exists_and_distrib_left]; exact ⟨T1, hrelT1, cT1, hrT1, rfl⟩) in let ⟨_, T', c', hrelTT'', hcT', hct⟩ := finset.mem_filter.1 (finset.mem_of_max ht) in have htmax : ∀ (s : fin (m + n)) (T : tableau m n), rel obj T T → ∀ (j : fin n), find_pivot_column T obj = some j → T.to_partition.colg j = s → s ≤ t, by simpa using λ s (h : s ∈ _), finset.le_max_of_mem h ht, let ⟨r, hrT'⟩ := exists_mem_pivot_row_of_rel obj hrelTT'' hcT' in have hrelTT''p : rel obj T' (T'.pivot r c'), from (eq_or_rel_pivot_of_rel obj hrelTT'' hcT' hrT').elim (λ h, h ▸ hrelTT'') id, let ⟨T, c, hTT', hrelT'T, hT'Tr, hc, hr⟩ := (rowg_eq_or_exists_mem_pivot_row obj hrelTT''p r).resolve_left (by simp) in have hfT' : feasible T', from feasible_of_rel_left _ hrelTT'', have hfT : feasible T, from feasible_of_rel_right _ hrelT'T, have hrelT'pT' : rel obj (T'.pivot r c') T', from rel.pivot hfT' hcT' hrT', have hrelTT' : rel obj T T', from hTT'.elim (λ h, h.symm ▸ hrelT'pT') (λ h, h.trans hrelT'pT'), have hrelTT : rel obj T T, from hrelTT'.trans hrelT'T, have hc't : T.to_partition.colg c ≤ t, from htmax _ T hrelTT _ hc rfl, have hoT'T : T'.const obj 0 = T.const obj 0, from const_obj_eq_of_rel_of_rel _ hrelT'T hrelTT', have hfickle : ∀ i, T.to_partition.rowg i ≠ T'.to_partition.rowg i → T.const i 0 = 0, from const_eq_zero_of_rowg_ne_of_rel_self obj hrelTT' hrelT'T, have hobj : T.const obj 0 = T'.const obj 0, from const_obj_eq_of_rel_of_rel _ hrelTT' hrelT'T, have hflat : T.flat = T'.flat, from flat_eq_of_rel obj hrelTT', have hrobj : T.to_partition.rowg obj = T'.to_partition.rowg obj, from rowg_obj_eq_of_rel _ hrelTT', have hs : T.to_partition.rowg r = T'.to_partition.colg c', by simpa using hT'Tr, have hc'res : T'.to_partition.colg c' ∈ T'.restricted, from hs ▸ restricted_eq_of_rel _ hrelTT' ▸ (find_pivot_row_spec hr).2.1, have hc'obj0 : 0 < T'.to_matrix obj c', by simpa [hc'res] using find_pivot_column_spec hcT', have hcres : T.to_partition.colg c ∈ T.restricted, from colg_mem_restricted_of_rel_self obj hrelTT hc, have hcobj0 : 0 < to_matrix T obj c, by simpa [hcres] using find_pivot_column_spec hc, have hrc0 : T.to_matrix r c < 0, from inv_neg'.1 $ neg_of_mul_neg_left (find_pivot_row_spec hr).2.2.1 (le_of_lt hcobj0), have nonpos_of_colg_ne : ∀ j, T'.to_partition.colg j ≠ T.to_partition.colg j → j ≠ c' → T'.to_matrix obj j ≤ 0, from λ j hj hjc', let ⟨T₃, hT₃⟩ := (colg_eq_or_exists_mem_pivot_column obj hrelTT').resolve_left hj in nonpos_of_lt_find_pivot_column hcT' hc'res (lt_of_le_of_ne (hct.symm ▸ hT₃.2.2.1 ▸ htmax _ T₃ (hT₃.1.elim (λ h, h.symm ▸ hrelTT'') (λ h, h.trans (hrelT'T.trans hT₃.2.1))) _ hT₃.2.2.2 rfl) (by rwa [ne.def, T'.to_partition.injective_colg.eq_iff])), have nonpos_of_colg_eq : ∀ j, j ≠ c' → T'.to_partition.colg j = T.to_partition.colg c → T'.to_matrix obj j ≤ 0, from λ j hjc' hj, if hjc : j = c then by clear_aux_decl; subst j; exact nonpos_of_lt_find_pivot_column hcT' hc'res (lt_of_le_of_ne (hj.symm ▸ hct.symm ▸ htmax _ _ hrelTT _ hc rfl) (hs ▸ hj.symm ▸ colg_ne_rowg _ _ _)) else let ⟨T₃, hT₃⟩ := (colg_eq_or_exists_mem_pivot_column obj hrelTT').resolve_left (show T'.to_partition.colg j ≠ T.to_partition.colg j, by simpa [hj, T.to_partition.injective_colg.eq_iff, eq_comm] using hjc) in nonpos_of_lt_find_pivot_column hcT' hc'res (lt_of_le_of_ne (hct.symm ▸ hT₃.2.2.1 ▸ htmax _ T₃ (hT₃.1.elim (λ h, h.symm ▸ hrelTT'') (λ h, h.trans (hrelT'T.trans hT₃.2.1))) _ hT₃.2.2.2 rfl) (by rwa [ne.def, T'.to_partition.injective_colg.eq_iff])), have unique_row : ∀ i ≠ r, T.const i 0 = 0 → T.to_partition.rowg i ≠ T'.to_partition.rowg i → 0 ≤ T.to_matrix i c, from λ i hir hi0 hrow, let ⟨T₃, c₃, hc₃, hrow₃, hrelT₃T, hrelTT₃⟩ := exists_mem_pivot_column_of_rowg_ne _ hrelTT' hrelT'T hrow in have hrelT₃T₃ : rel obj T₃ T₃, from hrelT₃T.trans hrelTT₃, nonneg_of_lt_find_pivot_row (by exact hcobj0) (by rw [← hrow₃, ← restricted_eq_of_rel _ hrelT₃T]; exact colg_mem_restricted_of_rel_self _ hrelT₃T₃ hc₃) hc hr hi0 (lt_of_le_of_ne (by rw [hs, hct, ← hrow₃]; exact htmax _ _ hrelT₃T₃ _ hc₃ rfl) (by simpa [T.to_partition.injective_rowg.eq_iff])), not_unique_row_and_unique_col obj hcobj0 hc'obj0 hrc0 hflat hs hrobj hfickle hobj nonpos_of_colg_ne nonpos_of_colg_eq unique_row noncomputable instance fintype_rel (T : tableau m n) : fintype {T' | rel obj T' T} := fintype.of_injective (λ T', T'.val.to_partition) (λ T₁ T₂ h, subtype.eq $ tableau.ext (by rw [flat_eq_of_rel _ T₁.2, flat_eq_of_rel _ T₂.2]) h (by rw [restricted_eq_of_rel _ T₁.2, restricted_eq_of_rel _ T₂.2])) lemma rel_wf (m n : ℕ) (obj : fin m): well_founded (@rel m n obj) := subrelation.wf (show subrelation (@rel m n obj) (measure (λ T, fintype.card {T' | rel obj T' T})), from assume T₁ T₂ h, set.card_lt_card (set.ssubset_iff_subset_not_subset.2 ⟨λ T' hT', hT'.trans h, classical.not_forall.2 ⟨T₁, λ h', rel.irrefl _ (h' h)⟩⟩)) (measure_wf (λ T, fintype.card {T' | rel obj T' T})) end blands_rule @[derive _root_.decidable_eq] inductive termination : Type | while : termination | unbounded : termination | optimal : termination open termination instance : has_repr termination := ⟨λ t, termination.cases_on t "while" "unbounded" "optimal"⟩ instance : fintype termination := ⟨⟨quotient.mk [while, unbounded, optimal], dec_trivial⟩, λ x, by cases x; exact dec_trivial⟩ open termination /-- The simplex algorithm -/ def simplex (w : tableau m n → bool) (obj : fin m) : Π (T : tableau m n) (hT : feasible T), tableau m n × termination | T := λ hT, cond (w T) (match find_pivot_column T obj, @feasible_of_mem_pivot_row_and_column _ _ _ obj hT, @rel.pivot m n obj _ hT with | none, hc, hrel := (T, optimal) | some c, hc, hrel := match find_pivot_row T obj c, @hc _ rfl, (λ r, @hrel r c rfl) with | none, hr, hrel := (T, unbounded) | some r, hr, hrel := have wf : rel obj (pivot T r c) T, from hrel _ rfl, simplex (T.pivot r c) (hr rfl) end end) (T, while) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := tactic.assumption} lemma simplex_pivot {w : tableau m n → bool} {T : tableau m n} (hT : feasible T) (hw : w T = tt) {obj : fin m} {r : fin m} {c : fin n} (hc : c ∈ find_pivot_column T obj) (hr : r ∈ find_pivot_row T obj c) : (T.pivot r c).simplex w obj (feasible_of_mem_pivot_row_and_column hT hc hr) = T.simplex w obj hT := by conv_rhs { rw simplex }; simp [hw, show _ = _, from hr, show _ = _, from hc, simplex._match_1, simplex._match_2] lemma simplex_spec_aux (w : tableau m n → bool) (obj : fin m) : Π (T : tableau m n) (hT : feasible T), ((T.simplex w obj hT).2 = while ∧ w (T.simplex w obj hT).1 = ff) ∨ ((T.simplex w obj hT).2 = optimal ∧ w (T.simplex w obj hT).1 = tt ∧ find_pivot_column (T.simplex w obj hT).1 obj = none) ∨ ((T.simplex w obj hT).2 = unbounded ∧ w (T.simplex w obj hT).1 = tt ∧ ∃ c, c ∈ find_pivot_column (T.simplex w obj hT).1 obj ∧ find_pivot_row (T.simplex w obj hT).1 obj c = none) | T := λ hT, begin cases hw : w T, { rw simplex, simp [hw] }, { cases hc : find_pivot_column T obj with c, { rw simplex, simp [hc, hw, simplex._match_1] }, { cases hr : find_pivot_row T obj c with r, { rw simplex, simp [hr, hc, hw, simplex._match_1, simplex._match_2] }, { rw [← simplex_pivot hT hw hc hr], exact have wf : rel obj (T.pivot r c) T, from rel.pivot hT hc hr, simplex_spec_aux _ _ } } } end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := tactic.assumption} lemma simplex_while_eq_ff {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (hw : w T = ff) : T.simplex w obj hT = (T, while) := by rw [simplex, hw]; refl lemma simplex_find_pivot_column_eq_none {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} (hw : w T = tt) {obj : fin m} (hc : find_pivot_column T obj = none) : T.simplex w obj hT = (T, optimal) := by rw simplex; simp [hc, hw, simplex._match_1] lemma simplex_find_pivot_row_eq_none {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (hw : w T = tt) {c} (hc : c ∈ find_pivot_column T obj) (hr : find_pivot_row T obj c = none) : T.simplex w obj hT = (T, unbounded) := by rw simplex; simp [hw, show _ = _, from hc, hr, simplex._match_1, simplex._match_2] lemma simplex_induction (P : tableau m n → Prop) (w : tableau m n → bool) (obj : fin m): Π {T : tableau m n} (hT : feasible T) (h0 : P T) (hpivot : ∀ {T' r c}, w T' = tt → c ∈ find_pivot_column T' obj → r ∈ find_pivot_row T' obj c → feasible T' → P T' → P (T'.pivot r c)), P (T.simplex w obj hT).1 | T := λ hT h0 hpivot, begin cases hw : w T, { rwa [simplex_while_eq_ff hw] }, { cases hc : find_pivot_column T obj with c, { rwa [simplex_find_pivot_column_eq_none hw hc] }, { cases hr : find_pivot_row T obj c with r, { rwa simplex_find_pivot_row_eq_none hw hc hr }, { rw [← simplex_pivot _ hw hc hr], exact have wf : rel obj (pivot T r c) T, from rel.pivot hT hc hr, simplex_induction (feasible_of_mem_pivot_row_and_column hT hc hr) (hpivot hw hc hr hT h0) @hpivot } } } end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := `[tauto]} @[simp] lemma feasible_simplex {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : feasible (T.simplex w obj hT).1 := simplex_induction feasible _ _ hT hT (λ _ _ _ _ hc hr _ hT', feasible_of_mem_pivot_row_and_column hT' hc hr) @[simp] lemma simplex_simplex {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : (T.simplex w obj hT).1.simplex w obj feasible_simplex = T.simplex w obj hT := simplex_induction (λ T', ∀ (hT' : feasible T'), T'.simplex w obj hT' = T.simplex w obj hT) w _ _ (λ _, rfl) (λ T' r c hw hc hr hT' ih hpivot, by rw [simplex_pivot hT' hw hc hr, ih]) _ /-- `simplex` does not move the row variable it is trying to maximise. -/ @[simp] lemma rowg_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.to_partition.rowg obj = T.to_partition.rowg obj := simplex_induction (λ T', T'.to_partition.rowg obj = T.to_partition.rowg obj) _ _ _ rfl (λ T' r c hw hc hr, by simp [rowg_swap_of_ne _ (find_pivot_row_spec hr).1]) @[simp] lemma flat_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.flat = T.flat := simplex_induction (λ T', T'.flat = T.flat) w obj _ rfl (λ T' r c hw hc hr hT' ih, have T'.to_matrix r c ≠ 0, from λ h, by simpa [h, lt_irrefl] using find_pivot_row_spec hr, by rw [flat_pivot this, ih]) @[simp] lemma restricted_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.restricted = T.restricted := simplex_induction (λ T', T'.restricted = T.restricted) _ _ _ rfl (by simp { contextual := tt }) @[simp] lemma sol_set_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.sol_set = T.sol_set := by simp [sol_set] @[simp] lemma of_col_simplex_zero_mem_sol_set {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : (T.simplex w obj hT).1.of_col 0 ∈ sol_set T := by rw [← sol_set_simplex, of_col_zero_mem_sol_set_iff]; exact feasible_simplex @[simp] lemma of_col_simplex_rowg {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (x : cvec n) : (T.simplex w obj hT).1.of_col x (T.to_partition.rowg obj) = ((T.simplex w obj hT).1.to_matrix ⬝ x + (T.simplex w obj hT).1.const) obj := by rw [← of_col_rowg (T.simplex w obj hT).1 x obj, rowg_simplex] @[simp] lemma is_unbounded_above_simplex {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {v : fin (m + n)} : is_unbounded_above (T.simplex w obj hT).1 v ↔ is_unbounded_above T v := by simp [is_unbounded_above] @[simp] lemma is_optimal_simplex {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {x : cvec (m + n)} {v : fin (m + n)} : is_optimal (T.simplex w obj hT).1 x v ↔ is_optimal T x v := by simp [is_optimal] lemma termination_eq_while_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = while ↔ w (T.simplex w obj hT).1 = ff := by have := simplex_spec_aux w obj T hT; finish lemma termination_eq_optimal_iff_find_pivot_column_eq_none {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = optimal ↔ w (T.simplex w obj hT).1 = tt ∧ find_pivot_column (T.simplex w obj hT).1 obj = none := by have := simplex_spec_aux w obj T hT; finish lemma termination_eq_unbounded_iff_find_pivot_row_eq_none {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = unbounded ↔ w (T.simplex w obj hT).1 = tt ∧ ∃ c, c ∈ find_pivot_column (T.simplex w obj hT).1 obj ∧ find_pivot_row (T.simplex w obj hT).1 obj c = none := by have := simplex_spec_aux w obj T hT; finish lemma termination_eq_unbounded_iff_aux {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = unbounded → w (T.simplex w obj hT).1 = tt ∧ is_unbounded_above T (T.to_partition.rowg obj) := begin rw termination_eq_unbounded_iff_find_pivot_row_eq_none, rintros ⟨_, c, hc⟩, simpa * using find_pivot_row_eq_none feasible_simplex hc.2 hc.1 end lemma termination_eq_optimal_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = optimal ↔ w (T.simplex w obj hT).1 = tt ∧ is_optimal T ((T.simplex w obj hT).1.of_col 0) (T.to_partition.rowg obj) := begin rw [termination_eq_optimal_iff_find_pivot_column_eq_none], split, { rintros ⟨_, hc⟩, simpa * using find_pivot_column_eq_none feasible_simplex hc }, { cases ht : (T.simplex w obj hT).2, { simp [*, termination_eq_while_iff] at * }, { cases termination_eq_unbounded_iff_aux ht, simp [*, not_optimal_of_unbounded_above right] }, { simp [*, termination_eq_optimal_iff_find_pivot_column_eq_none] at * } } end lemma termination_eq_unbounded_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = unbounded ↔ w (T.simplex w obj hT).1 = tt ∧ is_unbounded_above T (T.to_partition.rowg obj) := ⟨termination_eq_unbounded_iff_aux, begin have := @not_optimal_of_unbounded_above m n (T.simplex w obj hT).1 (T.to_partition.rowg obj) ((T.simplex w obj hT).1.of_col 0), cases ht : (T.simplex w obj hT).2; simp [termination_eq_optimal_iff, termination_eq_while_iff, *] at * end⟩ end tableau
21b414add4c48a7df904e3d597bdcf8ca6b1cb1d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/int/gcd.lean
de63a730246ffa0910be1dbe3e42d88a596d4d79
[ "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
25,745
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 ℤ ## 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_non_zero_left {i : ℤ} (j : ℤ) (i_non_zero : i ≠ 0) : 0 < gcd i j := nat.gcd_pos_of_pos_left (nat_abs j) (nat_abs_pos_of_ne_zero i_non_zero) theorem gcd_pos_of_non_zero_right (i : ℤ) {j : ℤ} (j_non_zero : j ≠ 0) : 0 < gcd i j := nat.gcd_pos_of_pos_right (nat_abs i) (nat_abs_pos_of_ne_zero j_non_zero) 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_non_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
ff082a31385c9df37c6b616787e6366150c4c1db
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/group/defs.lean
a33049364eb5dc58f442a41f7295a178e3d5275a
[ "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
40,139
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro -/ import tactic.basic import logic.function.basic /-! # Typeclasses for (semi)groups and monoids > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define typeclasses for algebraic structures with one binary operation. The classes are named `(add_)?(comm_)?(semigroup|monoid|group)`, where `add_` means that the class uses additive notation and `comm_` means that the class assumes that the binary operation is commutative. The file does not contain any lemmas except for * axioms of typeclasses restated in the root namespace; * lemmas required for instances. For basic lemmas about these classes see `algebra.group.basic`. We also introduce notation classes `has_smul` and `has_vadd` for multiplicative and additive actions and register the following instances: - `has_pow M ℕ`, for monoids `M`, and `has_pow G ℤ` for groups `G`; - `has_smul ℕ M` for additive monoids `M`, and `has_smul ℤ G` for additive groups `G`. ## Notation - `+`, `-`, `*`, `/`, `^` : the usual arithmetic operations; the underlying functions are `has_add.add`, `has_neg.neg`/`has_sub.sub`, `has_mul.mul`, `has_div.div`, and `has_pow.pow`. - `a • b` is used as notation for `has_smul.smul a b`. - `a +ᵥ b` is used as notation for `has_vadd.vadd a b`. -/ open function /-- Type class for the `+ᵥ` notation. -/ class has_vadd (G : Type*) (P : Type*) := (vadd : G → P → P) /-- Type class for the `-ᵥ` notation. -/ class has_vsub (G : out_param Type*) (P : Type*) := (vsub : P → P → G) /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ @[ext, to_additive] class has_smul (M : Type*) (α : Type*) := (smul : M → α → α) infix ` +ᵥ `:65 := has_vadd.vadd infix ` -ᵥ `:65 := has_vsub.vsub infixr ` • `:73 := has_smul.smul attribute [to_additive_reorder 1] has_pow attribute [to_additive_reorder 1 4] has_pow.pow attribute [to_additive has_smul] has_pow attribute [to_additive has_smul.smul] has_pow.pow set_option old_structure_cmd true universe u variables {G : Type*} /- Additive "sister" structures. Example, add_semigroup mirrors semigroup. These structures exist just to help automation. In an alternative design, we could have the binary operation as an extra argument for semigroup, monoid, group, etc. However, the lemmas would be hard to index since they would not contain any constant. For example, mul_assoc would be lemma mul_assoc {α : Type u} {op : α → α → α} [semigroup α op] : ∀ a b c : α, op (op a b) c = op a (op b c) := semigroup.mul_assoc The simplifier cannot effectively use this lemma since the pattern for the left-hand-side would be ?op (?op ?a ?b) ?c Remark: we use a tactic for transporting theorems from the multiplicative fragment to the additive one. -/ section has_mul variables [has_mul G] /-- `left_mul g` denotes left multiplication by `g` -/ @[to_additive "`left_add g` denotes left addition by `g`"] def left_mul : G → G → G := λ g : G, λ x : G, g * x /-- `right_mul g` denotes right multiplication by `g` -/ @[to_additive "`right_add g` denotes right addition by `g`"] def right_mul : G → G → G := λ g : G, λ x : G, x * g /-- A mixin for left cancellative multiplication. -/ @[protect_proj] class is_left_cancel_mul (G : Type u) [has_mul G] : Prop := (mul_left_cancel : ∀ a b c : G, a * b = a * c → b = c) /-- A mixin for right cancellative multiplication. -/ @[protect_proj] class is_right_cancel_mul (G : Type u) [has_mul G] : Prop := (mul_right_cancel : ∀ a b c : G, a * b = c * b → a = c) /-- A mixin for cancellative multiplication. -/ @[protect_proj] class is_cancel_mul (G : Type u) [has_mul G] extends is_left_cancel_mul G, is_right_cancel_mul G : Prop /-- A mixin for left cancellative addition. -/ @[protect_proj] class is_left_cancel_add (G : Type u) [has_add G] : Prop := (add_left_cancel : ∀ a b c : G, a + b = a + c → b = c) attribute [to_additive] is_left_cancel_mul /-- A mixin for right cancellative addition. -/ @[protect_proj] class is_right_cancel_add (G : Type u) [has_add G] : Prop := (add_right_cancel : ∀ a b c : G, a + b = c + b → a = c) attribute [to_additive] is_right_cancel_mul /-- A mixin for cancellative addition. -/ class is_cancel_add (G : Type u) [has_add G] extends is_left_cancel_add G, is_right_cancel_add G : Prop attribute [to_additive] is_cancel_mul section is_left_cancel_mul variables [is_left_cancel_mul G] {a b c : G} @[to_additive] lemma mul_left_cancel : a * b = a * c → b = c := is_left_cancel_mul.mul_left_cancel a b c @[to_additive] lemma mul_left_cancel_iff : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[to_additive] theorem mul_right_injective (a : G) : function.injective ((*) a) := λ b c, mul_left_cancel @[simp, to_additive] theorem mul_right_inj (a : G) {b c : G} : a * b = a * c ↔ b = c := (mul_right_injective a).eq_iff @[to_additive] theorem mul_ne_mul_right (a : G) {b c : G} : a * b ≠ a * c ↔ b ≠ c := (mul_right_injective a).ne_iff end is_left_cancel_mul section is_right_cancel_mul variables [is_right_cancel_mul G] {a b c : G} @[to_additive] lemma mul_right_cancel : a * b = c * b → a = c := is_right_cancel_mul.mul_right_cancel a b c @[to_additive] lemma mul_right_cancel_iff : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ @[to_additive] theorem mul_left_injective (a : G) : function.injective (λ x, x * a) := λ b c, mul_right_cancel @[simp, to_additive] theorem mul_left_inj (a : G) {b c : G} : b * a = c * a ↔ b = c := (mul_left_injective a).eq_iff @[to_additive] theorem mul_ne_mul_left (a : G) {b c : G} : b * a ≠ c * a ↔ b ≠ c := (mul_left_injective a).ne_iff end is_right_cancel_mul end has_mul /-- A semigroup is a type with an associative `(*)`. -/ @[protect_proj, ancestor has_mul, ext] class semigroup (G : Type u) extends has_mul G := (mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)) /-- An additive semigroup is a type with an associative `(+)`. -/ @[protect_proj, ancestor has_add, ext] class add_semigroup (G : Type u) extends has_add G := (add_assoc : ∀ a b c : G, a + b + c = a + (b + c)) attribute [to_additive] semigroup section semigroup variables [semigroup G] @[no_rsimp, to_additive] lemma mul_assoc : ∀ a b c : G, a * b * c = a * (b * c) := semigroup.mul_assoc @[to_additive] instance semigroup.to_is_associative : is_associative G (*) := ⟨mul_assoc⟩ end semigroup /-- A commutative semigroup is a type with an associative commutative `(*)`. -/ @[protect_proj, ancestor semigroup, ext] class comm_semigroup (G : Type u) extends semigroup G := (mul_comm : ∀ a b : G, a * b = b * a) /-- A commutative additive semigroup is a type with an associative commutative `(+)`. -/ @[protect_proj, ancestor add_semigroup, ext] class add_comm_semigroup (G : Type u) extends add_semigroup G := (add_comm : ∀ a b : G, a + b = b + a) attribute [to_additive] comm_semigroup section comm_semigroup variables [comm_semigroup G] @[no_rsimp, to_additive] lemma mul_comm : ∀ a b : G, a * b = b * a := comm_semigroup.mul_comm @[to_additive] instance comm_semigroup.to_is_commutative : is_commutative G (*) := ⟨mul_comm⟩ /-- Any `comm_semigroup G` that satisfies `is_right_cancel_mul G` also satisfies `is_left_cancel_mul G`. -/ @[to_additive add_comm_semigroup.is_right_cancel_add.to_is_left_cancel_add "Any `add_comm_semigroup G` that satisfies `is_right_cancel_add G` also satisfies `is_right_cancel_add G`."] lemma comm_semigroup.is_right_cancel_mul.to_is_left_cancel_mul (G : Type u) [comm_semigroup G] [is_right_cancel_mul G] : is_left_cancel_mul G := ⟨λ a b c h, mul_right_cancel $ (mul_comm _ _).trans (h.trans $ mul_comm _ _)⟩ /-- Any `comm_semigroup G` that satisfies `is_left_cancel_mul G` also satisfies `is_right_cancel_mul G`. -/ @[to_additive add_comm_semigroup.is_left_cancel_add.to_is_right_cancel_add "Any `add_comm_semigroup G` that satisfies `is_left_cancel_add G` also satisfies `is_left_cancel_add G`."] lemma comm_semigroup.is_left_cancel_mul.to_is_right_cancel_mul (G : Type u) [comm_semigroup G] [is_left_cancel_mul G] : is_right_cancel_mul G := ⟨λ a b c h, mul_left_cancel $ (mul_comm _ _).trans (h.trans $ mul_comm _ _)⟩ /-- Any `comm_semigroup G` that satisfies `is_left_cancel_mul G` also satisfies `is_cancel_mul G`. -/ @[to_additive add_comm_semigroup.is_left_cancel_add.to_is_cancel_add "Any `add_comm_semigroup G` that satisfies `is_left_cancel_add G` also satisfies `is_cancel_add G`."] lemma comm_semigroup.is_left_cancel_mul.to_is_cancel_mul (G : Type u) [comm_semigroup G] [is_left_cancel_mul G] : is_cancel_mul G := { .. ‹is_left_cancel_mul G›, .. comm_semigroup.is_left_cancel_mul.to_is_right_cancel_mul G } /-- Any `comm_semigroup G` that satisfies `is_right_cancel_mul G` also satisfies `is_cancel_mul G`. -/ @[to_additive add_comm_semigroup.is_right_cancel_add.to_is_cancel_add "Any `add_comm_semigroup G` that satisfies `is_right_cancel_add G` also satisfies `is_cancel_add G`."] lemma comm_semigroup.is_right_cancel_mul.to_is_cancel_mul (G : Type u) [comm_semigroup G] [is_right_cancel_mul G] : is_cancel_mul G := { .. ‹is_right_cancel_mul G›, .. comm_semigroup.is_right_cancel_mul.to_is_left_cancel_mul G } end comm_semigroup /-- A `left_cancel_semigroup` is a semigroup such that `a * b = a * c` implies `b = c`. -/ @[protect_proj, ancestor semigroup, ext] class left_cancel_semigroup (G : Type u) extends semigroup G := (mul_left_cancel : ∀ a b c : G, a * b = a * c → b = c) /-- An `add_left_cancel_semigroup` is an additive semigroup such that `a + b = a + c` implies `b = c`. -/ @[protect_proj, ancestor add_semigroup, ext] class add_left_cancel_semigroup (G : Type u) extends add_semigroup G := (add_left_cancel : ∀ a b c : G, a + b = a + c → b = c) attribute [to_additive add_left_cancel_semigroup] left_cancel_semigroup /-- Any `left_cancel_semigroup` satisfies `is_left_cancel_mul`. -/ @[priority 100, to_additive "Any `add_left_cancel_semigroup` satisfies `is_left_cancel_add`."] instance left_cancel_semigroup.to_is_left_cancel_mul (G : Type u) [left_cancel_semigroup G] : is_left_cancel_mul G := { mul_left_cancel := left_cancel_semigroup.mul_left_cancel } /-- A `right_cancel_semigroup` is a semigroup such that `a * b = c * b` implies `a = c`. -/ @[protect_proj, ancestor semigroup, ext] class right_cancel_semigroup (G : Type u) extends semigroup G := (mul_right_cancel : ∀ a b c : G, a * b = c * b → a = c) /-- An `add_right_cancel_semigroup` is an additive semigroup such that `a + b = c + b` implies `a = c`. -/ @[protect_proj, ancestor add_semigroup, ext] class add_right_cancel_semigroup (G : Type u) extends add_semigroup G := (add_right_cancel : ∀ a b c : G, a + b = c + b → a = c) attribute [to_additive add_right_cancel_semigroup] right_cancel_semigroup /-- Any `right_cancel_semigroup` satisfies `is_right_cancel_mul`. -/ @[priority 100, to_additive "Any `add_right_cancel_semigroup` satisfies `is_right_cancel_add`."] instance right_cancel_semigroup.to_is_right_cancel_mul (G : Type u) [right_cancel_semigroup G] : is_right_cancel_mul G := { mul_right_cancel := right_cancel_semigroup.mul_right_cancel } /-- Typeclass for expressing that a type `M` with multiplication and a one satisfies `1 * a = a` and `a * 1 = a` for all `a : M`. -/ @[ancestor has_one has_mul] class mul_one_class (M : Type u) extends has_one M, has_mul M := (one_mul : ∀ (a : M), 1 * a = a) (mul_one : ∀ (a : M), a * 1 = a) /-- Typeclass for expressing that a type `M` with addition and a zero satisfies `0 + a = a` and `a + 0 = a` for all `a : M`. -/ @[ancestor has_zero has_add] class add_zero_class (M : Type u) extends has_zero M, has_add M := (zero_add : ∀ (a : M), 0 + a = a) (add_zero : ∀ (a : M), a + 0 = a) attribute [to_additive] mul_one_class @[ext, to_additive] lemma mul_one_class.ext {M : Type u} : ∀ ⦃m₁ m₂ : mul_one_class M⦄, m₁.mul = m₂.mul → m₁ = m₂ := begin rintros ⟨one₁, mul₁, one_mul₁, mul_one₁⟩ ⟨one₂, mul₂, one_mul₂, mul_one₂⟩ (rfl : mul₁ = mul₂), congr, exact (one_mul₂ one₁).symm.trans (mul_one₁ one₂), end section mul_one_class variables {M : Type u} [mul_one_class M] @[ematch, simp, to_additive] lemma one_mul : ∀ a : M, 1 * a = a := mul_one_class.one_mul @[ematch, simp, to_additive] lemma mul_one : ∀ a : M, a * 1 = a := mul_one_class.mul_one @[to_additive] instance mul_one_class.to_is_left_id : is_left_id M (*) 1 := ⟨ mul_one_class.one_mul ⟩ @[to_additive] instance mul_one_class.to_is_right_id : is_right_id M (*) 1 := ⟨ mul_one_class.mul_one ⟩ end mul_one_class section variables {M : Type u} -- use `x * npow_rec n x` and not `npow_rec n x * x` in the definition to make sure that -- definitional unfolding of `npow_rec` is blocked, to avoid deep recursion issues. /-- The fundamental power operation in a monoid. `npow_rec n a = a*a*...*a` n times. Use instead `a ^ n`, which has better definitional behavior. -/ def npow_rec [has_one M] [has_mul M] : ℕ → M → M | 0 a := 1 | (n+1) a := a * npow_rec n a /-- The fundamental scalar multiplication in an additive monoid. `nsmul_rec n a = a+a+...+a` n times. Use instead `n • a`, which has better definitional behavior. -/ def nsmul_rec [has_zero M] [has_add M] : ℕ → M → M | 0 a := 0 | (n+1) a := a + nsmul_rec n a attribute [to_additive] npow_rec end /-- 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 fewer 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" /-- `try_refl_tac` solves goals of the form `∀ a b, f a b = g a b`, if they hold by definition. -/ meta def try_refl_tac : tactic unit := `[intros; refl] /-! ### Design note on `add_monoid` and `monoid` An `add_monoid` has a natural `ℕ`-action, defined by `n • a = a + ... + a`, that we want to declare as an instance as it makes it possible to use the language of linear algebra. However, there are often other natural `ℕ`-actions. For instance, for any semiring `R`, the space of polynomials `R[X]` has a natural `R`-action defined by multiplication on the coefficients. This means that `ℕ[X]` would have two natural `ℕ`-actions, which are equal but not defeq. The same goes for linear maps, tensor products, and so on (and even for `ℕ` itself). To solve this issue, we embed an `ℕ`-action in the definition of an `add_monoid` (which is by default equal to the naive action `a + ... + a`, but can be adjusted when needed), and declare a `has_smul ℕ α` instance using this action. See Note [forgetful inheritance] for more explanations on this pattern. For example, when we define `R[X]`, then we declare the `ℕ`-action to be by multiplication on each coefficient (using the `ℕ`-action on `R` that comes from the fact that `R` is an `add_monoid`). In this way, the two natural `has_smul ℕ ℕ[X]` instances are defeq. The tactic `to_additive` transfers definitions and results from multiplicative monoids to additive monoids. To work, it has to map fields to fields. This means that we should also add corresponding fields to the multiplicative structure `monoid`, which could solve defeq problems for powers if needed. These problems do not come up in practice, so most of the time we will not need to adjust the `npow` field when defining multiplicative objects. A basic theory for the power function on monoids and the `ℕ`-action on additive monoids is built in the file `algebra.group_power.basic`. For now, we only register the most basic properties that we need right away. In the definition, we use `n.succ` instead of `n + 1` in the `nsmul_succ'` and `npow_succ'` fields to make sure that `to_additive` is not confused (otherwise, it would try to convert `1 : ℕ` to `0 : ℕ`). -/ /-- An `add_monoid` is an `add_semigroup` with an element `0` such that `0 + a = a + 0 = a`. -/ @[ancestor add_semigroup add_zero_class] class add_monoid (M : Type u) extends add_semigroup M, add_zero_class M := (nsmul : ℕ → M → M := nsmul_rec) (nsmul_zero' : ∀ x, nsmul 0 x = 0 . try_refl_tac) (nsmul_succ' : ∀ (n : ℕ) x, nsmul n.succ x = x + nsmul n x . try_refl_tac) /-- A `monoid` is a `semigroup` with an element `1` such that `1 * a = a * 1 = a`. -/ @[ancestor semigroup mul_one_class, to_additive] class monoid (M : Type u) extends semigroup M, mul_one_class M := (npow : ℕ → M → M := npow_rec) (npow_zero' : ∀ x, npow 0 x = 1 . try_refl_tac) (npow_succ' : ∀ (n : ℕ) x, npow n.succ x = x * npow n x . try_refl_tac) instance monoid.has_pow {M : Type*} [monoid M] : has_pow M ℕ := ⟨λ x n, monoid.npow n x⟩ instance add_monoid.has_smul_nat {M : Type*} [add_monoid M] : has_smul ℕ M := ⟨add_monoid.nsmul⟩ attribute [to_additive add_monoid.has_smul_nat] monoid.has_pow section variables {M : Type*} [monoid M] @[simp, to_additive nsmul_eq_smul] lemma npow_eq_pow (n : ℕ) (x : M) : monoid.npow n x = x^n := rfl -- the attributes are intentionally out of order. `zero_smul` proves `zero_nsmul`. @[to_additive zero_nsmul, simp] theorem pow_zero (a : M) : a^0 = 1 := monoid.npow_zero' _ @[to_additive succ_nsmul] theorem pow_succ (a : M) (n : ℕ) : a^(n+1) = a * a^n := monoid.npow_succ' n a end section monoid variables {M : Type u} [monoid M] @[to_additive] lemma left_inv_eq_right_inv {a b c : M} (hba : b * a = 1) (hac : a * c = 1) : b = c := by rw [←one_mul c, ←hba, mul_assoc, hac, mul_one b] end monoid /-- An additive commutative monoid is an additive monoid with commutative `(+)`. -/ @[protect_proj, ancestor add_monoid add_comm_semigroup] class add_comm_monoid (M : Type u) extends add_monoid M, add_comm_semigroup M /-- A commutative monoid is a monoid with commutative `(*)`. -/ @[protect_proj, ancestor monoid comm_semigroup, to_additive] class comm_monoid (M : Type u) extends monoid M, comm_semigroup M section left_cancel_monoid /-- An additive monoid in which addition is left-cancellative. Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero is useful to define the sum over the empty set, so `add_left_cancel_semigroup` is not enough. -/ @[protect_proj, ancestor add_left_cancel_semigroup add_monoid] class add_left_cancel_monoid (M : Type u) extends add_left_cancel_semigroup M, add_monoid M /-- A monoid in which multiplication is left-cancellative. -/ @[protect_proj, ancestor left_cancel_semigroup monoid, to_additive add_left_cancel_monoid] class left_cancel_monoid (M : Type u) extends left_cancel_semigroup M, monoid M end left_cancel_monoid section right_cancel_monoid /-- An additive monoid in which addition is right-cancellative. Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero is useful to define the sum over the empty set, so `add_right_cancel_semigroup` is not enough. -/ @[protect_proj, ancestor add_right_cancel_semigroup add_monoid] class add_right_cancel_monoid (M : Type u) extends add_right_cancel_semigroup M, add_monoid M /-- A monoid in which multiplication is right-cancellative. -/ @[protect_proj, ancestor right_cancel_semigroup monoid, to_additive add_right_cancel_monoid] class right_cancel_monoid (M : Type u) extends right_cancel_semigroup M, monoid M end right_cancel_monoid section cancel_monoid /-- An additive monoid in which addition is cancellative on both sides. Main examples are `ℕ` and groups. This is the right typeclass for many sum lemmas, as having a zero is useful to define the sum over the empty set, so `add_right_cancel_semigroup` is not enough. -/ @[protect_proj, ancestor add_left_cancel_monoid add_right_cancel_monoid] class add_cancel_monoid (M : Type u) extends add_left_cancel_monoid M, add_right_cancel_monoid M /-- A monoid in which multiplication is cancellative. -/ @[protect_proj, ancestor left_cancel_monoid right_cancel_monoid, to_additive add_cancel_monoid] class cancel_monoid (M : Type u) extends left_cancel_monoid M, right_cancel_monoid M /-- Commutative version of `add_cancel_monoid`. -/ @[protect_proj, ancestor add_left_cancel_monoid add_comm_monoid] class add_cancel_comm_monoid (M : Type u) extends add_left_cancel_monoid M, add_comm_monoid M /-- Commutative version of `cancel_monoid`. -/ @[protect_proj, ancestor left_cancel_monoid comm_monoid, to_additive add_cancel_comm_monoid] class cancel_comm_monoid (M : Type u) extends left_cancel_monoid M, comm_monoid M @[priority 100, to_additive] -- see Note [lower instance priority] instance cancel_comm_monoid.to_cancel_monoid (M : Type u) [cancel_comm_monoid M] : cancel_monoid M := { .. ‹cancel_comm_monoid M›, .. comm_semigroup.is_left_cancel_mul.to_is_right_cancel_mul M } /-- Any `cancel_monoid M` satisfies `is_cancel_mul M`. -/ @[priority 100, to_additive "Any `add_cancel_monoid M` satisfies `is_cancel_add M`."] instance cancel_monoid.to_is_cancel_mul (M : Type u) [cancel_monoid M] : is_cancel_mul M := { mul_left_cancel := cancel_monoid.mul_left_cancel, mul_right_cancel := cancel_monoid.mul_right_cancel } end cancel_monoid /-- The fundamental power operation in a group. `zpow_rec n a = a*a*...*a` n times, for integer `n`. Use instead `a ^ n`, which has better definitional behavior. -/ def zpow_rec {M : Type*} [has_one M] [has_mul M] [has_inv M] : ℤ → M → M | (int.of_nat n) a := npow_rec n a | -[1+ n] a := (npow_rec n.succ a) ⁻¹ /-- The fundamental scalar multiplication in an additive group. `zsmul_rec n a = a+a+...+a` n times, for integer `n`. Use instead `n • a`, which has better definitional behavior. -/ def zsmul_rec {M : Type*} [has_zero M] [has_add M] [has_neg M]: ℤ → M → M | (int.of_nat n) a := nsmul_rec n a | -[1+ n] a := - (nsmul_rec n.succ a) attribute [to_additive] zpow_rec section has_involutive_inv -- ensure that we don't go via these typeclasses to find `has_inv` on groups and groups with zero set_option extends_priority 50 /-- Auxiliary typeclass for types with an involutive `has_neg`. -/ @[ancestor has_neg] class has_involutive_neg (A : Type*) extends has_neg A := (neg_neg : ∀ x : A, - -x = x) /-- Auxiliary typeclass for types with an involutive `has_inv`. -/ @[ancestor has_inv, to_additive] class has_involutive_inv (G : Type*) extends has_inv G := (inv_inv : ∀ x : G, x⁻¹⁻¹ = x) variables [has_involutive_inv G] @[simp, to_additive] lemma inv_inv (a : G) : a⁻¹⁻¹ = a := has_involutive_inv.inv_inv _ end has_involutive_inv /-! ### Design note on `div_inv_monoid`/`sub_neg_monoid` and `division_monoid`/`subtraction_monoid` Those two pairs of made-up classes fulfill slightly different roles. `div_inv_monoid`/`sub_neg_monoid` provides the minimum amount of information to define the `ℤ` action (`zpow` or `zsmul`). Further, it provides a `div` field, matching the forgetful inheritance pattern. This is useful to shorten extension clauses of stronger structures (`group`, `group_with_zero`, `division_ring`, `field`) and for a few structures with a rather weak pseudo-inverse (`matrix`). `division_monoid`/`subtraction_monoid` is targeted at structures with stronger pseudo-inverses. It is an ad hoc collection of axioms that are mainly respected by three things: * Groups * Groups with zero * The pointwise monoids `set α`, `finset α`, `filter α` It acts as a middle ground for structures with an inversion operator that plays well with multiplication, except for the fact that it might not be a true inverse (`a / a ≠ 1` in general). The axioms are pretty arbitrary (many other combinations are equivalent to it), but they are independent: * Without `division_monoid.div_eq_mul_inv`, you can define `/` arbitrarily. * Without `division_monoid.inv_inv`, you can consider `with_top unit` with `a⁻¹ = ⊤` for all `a`. * Without `division_monoid.mul_inv_rev`, you can consider `with_top α` with `a⁻¹ = a` for all `a` where `α` non commutative. * Without `division_monoid.inv_eq_of_mul`, you can consider any `comm_monoid` with `a⁻¹ = a` for all `a`. As a consequence, a few natural structures do not fit in this framework. For example, `ennreal` respects everything except for the fact that `(0 * ∞)⁻¹ = 0⁻¹ = ∞` while `∞⁻¹ * 0⁻¹ = 0 * ∞ = 0`. -/ /-- A `div_inv_monoid` is a `monoid` with operations `/` and `⁻¹` satisfying `div_eq_mul_inv : ∀ a b, a / b = a * b⁻¹`. This deduplicates the name `div_eq_mul_inv`. The default for `div` is such that `a / b = a * b⁻¹` holds by definition. Adding `div` as a field rather than defining `a / b := a * b⁻¹` allows us to avoid certain classes of unification failures, for example: Let `foo X` be a type with a `∀ X, has_div (foo X)` instance but no `∀ X, has_inv (foo X)`, e.g. when `foo X` is a `euclidean_domain`. Suppose we also have an instance `∀ X [cromulent X], group_with_zero (foo X)`. Then the `(/)` coming from `group_with_zero_has_div` cannot be definitionally equal to the `(/)` coming from `foo.has_div`. In the same way, adding a `zpow` field makes it possible to avoid definitional failures in diamonds. See the definition of `monoid` and Note [forgetful inheritance] for more explanations on this. -/ @[protect_proj, ancestor monoid has_inv has_div] class div_inv_monoid (G : Type u) extends monoid G, has_inv G, has_div G := (div := λ a b, a * b⁻¹) (div_eq_mul_inv : ∀ a b : G, a / b = a * b⁻¹ . try_refl_tac) (zpow : ℤ → G → G := zpow_rec) (zpow_zero' : ∀ (a : G), zpow 0 a = 1 . try_refl_tac) (zpow_succ' : ∀ (n : ℕ) (a : G), zpow (int.of_nat n.succ) a = a * zpow (int.of_nat n) a . try_refl_tac) (zpow_neg' : ∀ (n : ℕ) (a : G), zpow (-[1+ n]) a = (zpow n.succ a)⁻¹ . try_refl_tac) /-- A `sub_neg_monoid` is an `add_monoid` with unary `-` and binary `-` operations satisfying `sub_eq_add_neg : ∀ a b, a - b = a + -b`. The default for `sub` is such that `a - b = a + -b` holds by definition. Adding `sub` as a field rather than defining `a - b := a + -b` allows us to avoid certain classes of unification failures, for example: Let `foo X` be a type with a `∀ X, has_sub (foo X)` instance but no `∀ X, has_neg (foo X)`. Suppose we also have an instance `∀ X [cromulent X], add_group (foo X)`. Then the `(-)` coming from `add_group.has_sub` cannot be definitionally equal to the `(-)` coming from `foo.has_sub`. In the same way, adding a `zsmul` field makes it possible to avoid definitional failures in diamonds. See the definition of `add_monoid` and Note [forgetful inheritance] for more explanations on this. -/ @[protect_proj, ancestor add_monoid has_neg has_sub] class sub_neg_monoid (G : Type u) extends add_monoid G, has_neg G, has_sub G := (sub := λ a b, a + -b) (sub_eq_add_neg : ∀ a b : G, a - b = a + -b . try_refl_tac) (zsmul : ℤ → G → G := zsmul_rec) (zsmul_zero' : ∀ (a : G), zsmul 0 a = 0 . try_refl_tac) (zsmul_succ' : ∀ (n : ℕ) (a : G), zsmul (int.of_nat n.succ) a = a + zsmul (int.of_nat n) a . try_refl_tac) (zsmul_neg' : ∀ (n : ℕ) (a : G), zsmul (-[1+ n]) a = - (zsmul n.succ a) . try_refl_tac) attribute [to_additive sub_neg_monoid] div_inv_monoid instance div_inv_monoid.has_pow {M} [div_inv_monoid M] : has_pow M ℤ := ⟨λ x n, div_inv_monoid.zpow n x⟩ instance sub_neg_monoid.has_smul_int {M} [sub_neg_monoid M] : has_smul ℤ M := ⟨sub_neg_monoid.zsmul⟩ attribute [to_additive sub_neg_monoid.has_smul_int] div_inv_monoid.has_pow section div_inv_monoid variables [div_inv_monoid G] {a b : G} @[simp, to_additive zsmul_eq_smul] lemma zpow_eq_pow (n : ℤ) (x : G) : div_inv_monoid.zpow n x = x^n := rfl @[simp, to_additive zero_zsmul] theorem zpow_zero (a : G) : a ^ (0:ℤ) = 1 := div_inv_monoid.zpow_zero' a @[simp, norm_cast, to_additive coe_nat_zsmul] theorem zpow_coe_nat (a : G) : ∀ n : ℕ, a ^ (n:ℤ) = a ^ n | 0 := (zpow_zero _).trans (pow_zero _).symm | (n + 1) := calc a ^ (↑(n + 1) : ℤ) = a * a ^ (n : ℤ) : div_inv_monoid.zpow_succ' _ _ ... = a * a ^ n : congr_arg ((*) a) (zpow_coe_nat n) ... = a ^ (n + 1) : (pow_succ _ _).symm @[to_additive of_nat_zsmul] theorem zpow_of_nat (a : G) (n : ℕ) : a ^ (int.of_nat n) = a ^ n := zpow_coe_nat a n @[simp, to_additive] theorem zpow_neg_succ_of_nat (a : G) (n : ℕ) : a ^ -[1+n] = (a ^ (n + 1))⁻¹ := by { rw ← zpow_coe_nat, exact div_inv_monoid.zpow_neg' n a } /-- Dividing by an element is the same as multiplying by its inverse. This is a duplicate of `div_inv_monoid.div_eq_mul_inv` ensuring that the types unfold better. -/ @[to_additive "Subtracting an element is the same as adding by its negative. This is a duplicate of `sub_neg_monoid.sub_eq_mul_neg` ensuring that the types unfold better."] lemma div_eq_mul_inv (a b : G) : a / b = a * b⁻¹ := div_inv_monoid.div_eq_mul_inv _ _ alias div_eq_mul_inv ← division_def end div_inv_monoid section inv_one_class set_option extends_priority 50 /-- Typeclass for expressing that `-0 = 0`. -/ class neg_zero_class (G : Type*) extends has_zero G, has_neg G := (neg_zero : -(0 : G) = 0) /-- A `sub_neg_monoid` where `-0 = 0`. -/ class sub_neg_zero_monoid (G : Type*) extends sub_neg_monoid G, neg_zero_class G /-- Typeclass for expressing that `1⁻¹ = 1`. -/ @[to_additive] class inv_one_class (G : Type*) extends has_one G, has_inv G := (inv_one : (1 : G)⁻¹ = 1) attribute [to_additive neg_zero_class.to_has_neg] inv_one_class.to_has_inv attribute [to_additive neg_zero_class.to_has_zero] inv_one_class.to_has_one /-- A `div_inv_monoid` where `1⁻¹ = 1`. -/ @[to_additive sub_neg_zero_monoid] class div_inv_one_monoid (G : Type*) extends div_inv_monoid G, inv_one_class G attribute [to_additive sub_neg_zero_monoid.to_sub_neg_monoid] div_inv_one_monoid.to_div_inv_monoid attribute [to_additive sub_neg_zero_monoid.to_neg_zero_class] div_inv_one_monoid.to_inv_one_class variables [inv_one_class G] @[simp, to_additive] lemma inv_one : (1 : G)⁻¹ = 1 := inv_one_class.inv_one end inv_one_class /-- A `subtraction_monoid` is a `sub_neg_monoid` with involutive negation and such that `-(a + b) = -b + -a` and `a + b = 0 → -a = b`. -/ @[protect_proj, ancestor sub_neg_monoid has_involutive_neg] class subtraction_monoid (G : Type u) extends sub_neg_monoid G, has_involutive_neg G := (neg_add_rev (a b : G) : -(a + b) = -b + -a) /- Despite the asymmetry of `neg_eq_of_add`, the symmetric version is true thanks to the involutivity of negation. -/ (neg_eq_of_add (a b : G) : a + b = 0 → -a = b) /-- A `division_monoid` is a `div_inv_monoid` with involutive inversion and such that `(a * b)⁻¹ = b⁻¹ * a⁻¹` and `a * b = 1 → a⁻¹ = b`. This is the immediate common ancestor of `group` and `group_with_zero`. -/ @[protect_proj, ancestor div_inv_monoid has_involutive_inv, to_additive] class division_monoid (G : Type u) extends div_inv_monoid G, has_involutive_inv G := (mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹) /- Despite the asymmetry of `inv_eq_of_mul`, the symmetric version is true thanks to the involutivity of inversion. -/ (inv_eq_of_mul (a b : G) : a * b = 1 → a⁻¹ = b) section division_monoid variables [division_monoid G] {a b : G} @[simp, to_additive neg_add_rev] lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := division_monoid.mul_inv_rev _ _ @[to_additive] lemma inv_eq_of_mul_eq_one_right : a * b = 1 → a⁻¹ = b := division_monoid.inv_eq_of_mul _ _ end division_monoid /-- Commutative `subtraction_monoid`. -/ @[protect_proj, ancestor subtraction_monoid add_comm_monoid] class subtraction_comm_monoid (G : Type u) extends subtraction_monoid G, add_comm_monoid G /-- Commutative `division_monoid`. This is the immediate common ancestor of `comm_group` and `comm_group_with_zero`. -/ @[protect_proj, ancestor division_monoid comm_monoid, to_additive subtraction_comm_monoid] class division_comm_monoid (G : Type u) extends division_monoid G, comm_monoid G /-- A `group` is a `monoid` with an operation `⁻¹` satisfying `a⁻¹ * a = 1`. There is also a division operation `/` such that `a / b = a * b⁻¹`, with a default so that `a / b = a * b⁻¹` holds by definition. -/ @[protect_proj, ancestor div_inv_monoid] class group (G : Type u) extends div_inv_monoid G := (mul_left_inv : ∀ a : G, a⁻¹ * a = 1) /-- An `add_group` is an `add_monoid` with a unary `-` satisfying `-a + a = 0`. There is also a binary operation `-` such that `a - b = a + -b`, with a default so that `a - b = a + -b` holds by definition. -/ @[protect_proj, ancestor sub_neg_monoid] class add_group (A : Type u) extends sub_neg_monoid A := (add_left_neg : ∀ a : A, -a + a = 0) attribute [to_additive] group /-- Abbreviation for `@div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _)`. Useful because it corresponds to the fact that `Grp` is a subcategory of `Mon`. Not an instance since it duplicates `@div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _)`. See note [reducible non-instances]. -/ @[reducible, to_additive "Abbreviation for `@sub_neg_monoid.to_add_monoid _ (@add_group.to_sub_neg_monoid _ _)`. Useful because it corresponds to the fact that `AddGroup` is a subcategory of `AddMon`. Not an instance since it duplicates `@sub_neg_monoid.to_add_monoid _ (@add_group.to_sub_neg_monoid _ _)`."] def group.to_monoid (G : Type u) [group G] : monoid G := @div_inv_monoid.to_monoid _ (@group.to_div_inv_monoid _ _) section group variables [group G] {a b c : G} @[simp, to_additive] lemma mul_left_inv : ∀ a : G, a⁻¹ * a = 1 := group.mul_left_inv @[to_additive] lemma inv_mul_self (a : G) : a⁻¹ * a = 1 := mul_left_inv a @[to_additive] private lemma inv_eq_of_mul (h : a * b = 1) : a⁻¹ = b := left_inv_eq_right_inv (inv_mul_self a) h @[simp, to_additive] lemma mul_right_inv (a : G) : a * a⁻¹ = 1 := by rw [←mul_left_inv a⁻¹, inv_eq_of_mul (mul_left_inv a)] @[to_additive] lemma mul_inv_self (a : G) : a * a⁻¹ = 1 := mul_right_inv a @[simp, to_additive] lemma inv_mul_cancel_left (a b : G) : a⁻¹ * (a * b) = b := by rw [←mul_assoc, mul_left_inv, one_mul] @[simp, to_additive] lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b := by rw [←mul_assoc, mul_right_inv, one_mul] @[simp, to_additive] lemma mul_inv_cancel_right (a b : G) : a * b * b⁻¹ = a := by rw [mul_assoc, mul_right_inv, mul_one] @[simp, to_additive] lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a := by rw [mul_assoc, mul_left_inv, mul_one] @[priority 100, to_additive add_group.to_subtraction_monoid] instance group.to_division_monoid : division_monoid G := { inv_inv := λ a, inv_eq_of_mul (mul_left_inv a), mul_inv_rev := λ a b, inv_eq_of_mul $ by rw [mul_assoc, mul_inv_cancel_left, mul_right_inv], inv_eq_of_mul := λ _ _, inv_eq_of_mul, ..‹group G› } @[priority 100, to_additive] -- see Note [lower instance priority] instance group.to_cancel_monoid : cancel_monoid G := { mul_right_cancel := λ a b c h, by rw [← mul_inv_cancel_right a b, h, mul_inv_cancel_right], mul_left_cancel := λ a b c h, by rw [← inv_mul_cancel_left a b, h, inv_mul_cancel_left], ..‹group G› } end group @[to_additive] lemma group.to_div_inv_monoid_injective {G : Type*} : function.injective (@group.to_div_inv_monoid G) := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, refl } /-- A commutative group is a group with commutative `(*)`. -/ @[protect_proj, ancestor group comm_monoid] class comm_group (G : Type u) extends group G, comm_monoid G /-- An additive commutative group is an additive group with commutative `(+)`. -/ @[protect_proj, ancestor add_group add_comm_monoid] class add_comm_group (G : Type u) extends add_group G, add_comm_monoid G attribute [to_additive] comm_group attribute [instance, priority 300] add_comm_group.to_add_comm_monoid @[to_additive] lemma comm_group.to_group_injective {G : Type u} : function.injective (@comm_group.to_group G) := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, refl } section comm_group variables [comm_group G] @[priority 100, to_additive] -- see Note [lower instance priority] instance comm_group.to_cancel_comm_monoid : cancel_comm_monoid G := { ..‹comm_group G›, ..group.to_cancel_monoid } @[priority 100, to_additive] -- see Note [lower instance priority] instance comm_group.to_division_comm_monoid : division_comm_monoid G := { ..‹comm_group G›, ..group.to_division_monoid } end comm_group
245f0a7a0cf71dc62108893d09e72318ae2f08c9
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/BuiltinNotation.lean
21e63e0aba911c31d00263ac361c4344114daa37
[ "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
16,370
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 Init.Data.ToString import Lean.Compiler.BorrowedAnnotation import Lean.Meta.KAbstract import Lean.Meta.Transform import Lean.Elab.App import Lean.Elab.SyntheticMVars namespace Lean.Elab.Term open Meta /-- The *anonymous constructor* `⟨e, ...⟩` is equivalent to `c e ...` if the expected type is an inductive type with a single constructor `c`. If more terms are given than `c` has parameters, the remaining arguments are turned into a new anonymous constructor application. For example, `⟨a, b, c⟩ : α × (β × γ)` is equivalent to `⟨a, ⟨b, c⟩⟩`. -/ @[builtinTermElab anonymousCtor] def elabAnonymousCtor : TermElab := fun stx expectedType? => match stx with | `(⟨$args,*⟩) => do tryPostponeIfNoneOrMVar expectedType? match expectedType? with | some expectedType => let expectedType ← whnf expectedType matchConstInduct expectedType.getAppFn (fun _ => throwError "invalid constructor ⟨...⟩, expected type must be an inductive type {indentExpr expectedType}") (fun ival us => do match ival.ctors with | [ctor] => let cinfo ← getConstInfoCtor ctor let numExplicitFields ← forallTelescopeReducing cinfo.type fun xs _ => do let mut n := 0 for i in [cinfo.numParams:xs.size] do if (← getFVarLocalDecl xs[i]).binderInfo.isExplicit then n := n + 1 return n let args := args.getElems if args.size < numExplicitFields then throwError "invalid constructor ⟨...⟩, insufficient number of arguments, constructs '{ctor}' has #{numExplicitFields} explicit fields, but only #{args.size} provided" let newStx ← if args.size == numExplicitFields then `($(mkCIdentFrom stx ctor) $(args)*) else if numExplicitFields == 0 then throwError "invalid constructor ⟨...⟩, insufficient number of arguments, constructs '{ctor}' does not have explicit fields, but #{args.size} provided" else let extra := args[numExplicitFields-1:args.size] let newLast ← `(⟨$[$extra],*⟩) let newArgs := args[0:numExplicitFields-1].toArray.push newLast `($(mkCIdentFrom stx ctor) $(newArgs)*) withMacroExpansion stx newStx $ elabTerm newStx expectedType? | _ => throwError "invalid constructor ⟨...⟩, expected type must be an inductive type with only one constructor {indentExpr expectedType}") | none => throwError "invalid constructor ⟨...⟩, expected type must be known" | _ => throwUnsupportedSyntax @[builtinTermElab borrowed] def elabBorrowed : TermElab := fun stx expectedType? => match stx with | `(@& $e) => return markBorrowed (← elabTerm e expectedType?) | _ => throwUnsupportedSyntax @[builtinMacro Lean.Parser.Term.show] def expandShow : Macro := fun stx => match stx with | `(show $type from $val) => let thisId := mkIdentFrom stx `this; `(let_fun $thisId : $type := $val; $thisId) | `(show $type by%$b $tac:tacticSeq) => `(show $type from by%$b $tac:tacticSeq) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.have] def expandHave : Macro := fun stx => let thisId := mkIdentFrom stx `this match stx with | `(have $x $bs* $[: $type]? := $val $[;]? $body) => `(let_fun $x $bs* $[: $type]? := $val; $body) | `(have $[: $type]? := $val $[;]? $body) => `(have $thisId:ident $[: $type]? := $val; $body) | `(have $x $bs* $[: $type]? $alts:matchAlts $[;]? $body) => `(let_fun $x $bs* $[: $type]? $alts:matchAlts; $body) | `(have $[: $type]? $alts:matchAlts $[;]? $body) => `(have $thisId:ident $[: $type]? $alts:matchAlts; $body) | `(have $pattern:term $[: $type]? := $val:term $[;]? $body) => `(let_fun $pattern:term $[: $type]? := $val:term ; $body) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.suffices] def expandSuffices : Macro | `(suffices $[$x :]? $type from $val $[;]? $body) => `(have $[$x]? : $type := $body; $val) | `(suffices $[$x :]? $type by%$b $tac:tacticSeq $[;]? $body) => `(have $[$x]? : $type := $body; by%$b $tac:tacticSeq) | _ => Macro.throwUnsupported open Lean.Parser in private def elabParserMacroAux (prec : Syntax) (e : Syntax) : TermElabM Syntax := do let (some declName) ← getDeclName? | throwError "invalid `leading_parser` macro, it must be used in definitions" match extractMacroScopes declName with | { name := Name.str _ s _, scopes := scps, .. } => let kind := quote declName let s := quote s -- if the parser decl is hidden by hygiene, it doesn't make sense to provide an antiquotation kind let antiquotKind ← if scps == [] then `(some $kind) else `(none) ``(withAntiquot (mkAntiquot $s $antiquotKind) (leadingNode $kind $prec $e)) | _ => throwError "invalid `leading_parser` macro, unexpected declaration name" @[builtinTermElab «leading_parser»] def elabLeadingParserMacro : TermElab := adaptExpander fun stx => match stx with | `(leading_parser $e) => elabParserMacroAux (quote Parser.maxPrec) e | `(leading_parser : $prec $e) => elabParserMacroAux prec e | _ => throwUnsupportedSyntax private def elabTParserMacroAux (prec lhsPrec : Syntax) (e : Syntax) : TermElabM Syntax := do let declName? ← getDeclName? match declName? with | some declName => let kind := quote declName; ``(Lean.Parser.trailingNode $kind $prec $lhsPrec $e) | none => throwError "invalid `trailing_parser` macro, it must be used in definitions" @[builtinTermElab «trailing_parser»] def elabTrailingParserMacro : TermElab := adaptExpander fun stx => match stx with | `(trailing_parser$[:$prec?]?$[:$lhsPrec?]? $e) => elabTParserMacroAux (prec?.getD <| quote Parser.maxPrec) (lhsPrec?.getD <| quote 0) e | _ => throwUnsupportedSyntax /-- `panic! msg` formally evaluates to `@Inhabited.default α` if the expected type `α` implements `Inhabited`. At runtime, `msg` and the file position are printed to stderr unless the C function `lean_set_panic_messages(false)` has been executed before. If the C function `lean_set_exit_on_panic(true)` has been executed before, the process is then aborted. -/ @[builtinTermElab panic] def elabPanic : TermElab := fun stx expectedType? => do let arg := stx[1] let pos ← getRefPosition let env ← getEnv let stxNew ← match (← getDeclName?) with | some declName => `(panicWithPosWithDecl $(quote (toString env.mainModule)) $(quote (toString declName)) $(quote pos.line) $(quote pos.column) $arg) | none => `(panicWithPos $(quote (toString env.mainModule)) $(quote pos.line) $(quote pos.column) $arg) withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? /-- A shorthand for `panic! "unreachable code has been reached"`. -/ @[builtinMacro Lean.Parser.Term.unreachable] def expandUnreachable : Macro := fun stx => `(panic! "unreachable code has been reached") /-- `assert! cond` panics if `cond` evaluates to `false`. -/ @[builtinMacro Lean.Parser.Term.assert] def expandAssert : Macro := fun stx => -- TODO: support for disabling runtime assertions let cond := stx[1] let body := stx[3] match cond.reprint with | some code => `(if $cond then $body else panic! ("assertion violation: " ++ $(quote code))) | none => `(if $cond then $body else panic! ("assertion violation")) /-- `dbg_trace e; body` evaluates to `body` and prints `e` (which can be an interpolated string literal) to stderr. It should only be used for debugging. -/ @[builtinMacro Lean.Parser.Term.dbgTrace] def expandDbgTrace : Macro := fun stx => let arg := stx[1] let body := stx[3] if arg.getKind == interpolatedStrKind then `(dbgTrace (s! $arg) fun _ => $body) else `(dbgTrace (toString $arg) fun _ => $body) /-- A temporary placeholder for a missing proof or value. -/ @[builtinTermElab «sorry»] def elabSorry : TermElab := fun stx expectedType? => do logWarning "declaration uses 'sorry'" let stxNew ← `(sorryAx _ false) withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? /-- Return syntax `Prod.mk elems[0] (Prod.mk elems[1] ... (Prod.mk elems[elems.size - 2] elems[elems.size - 1])))` -/ partial def mkPairs (elems : Array Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do if i > 0 then let i := i - 1 let elem := elems[i] let acc ← `(Prod.mk $elem $acc) loop i acc else pure acc loop (elems.size - 1) elems.back private partial def hasCDot : Syntax → Bool | Syntax.node _ k args => if k == ``Lean.Parser.Term.paren then false else if k == ``Lean.Parser.Term.cdot then true else args.any hasCDot | _ => false /-- Return `some` if succeeded expanding `·` notation occurring in the given syntax. Otherwise, return `none`. Examples: - `· + 1` => `fun _a_1 => _a_1 + 1` - `f · · b` => `fun _a_1 _a_2 => f _a_1 _a_2 b` -/ partial def expandCDot? (stx : Syntax) : MacroM (Option Syntax) := do if hasCDot stx then let (newStx, binders) ← (go stx).run #[]; `(fun $binders* => $newStx) else pure none where /-- Auxiliary function for expanding the `·` notation. The extra state `Array Syntax` contains the new binder names. If `stx` is a `·`, we create a fresh identifier, store in the extra state, and return it. Otherwise, we just return `stx`. -/ go : Syntax → StateT (Array Syntax) MacroM Syntax | stx@(Syntax.node i k args) => if k == ``Lean.Parser.Term.paren then pure stx else if k == ``Lean.Parser.Term.cdot then withFreshMacroScope do let id ← `(a) modify fun s => s.push id; pure id else do let args ← args.mapM go pure $ Syntax.node i k args | stx => pure stx /-- Helper method for elaborating terms such as `(.+.)` where a constant name is expected. This method is usually used to implement tactics that function names as arguments (e.g., `simp`). -/ def elabCDotFunctionAlias? (stx : Syntax) : TermElabM (Option Expr) := do let some stx ← liftMacroM <| expandCDotArg? stx | pure none let stx ← liftMacroM <| expandMacros stx match stx with | `(fun $binders* => $f:ident $args*) => if binders == args then try Term.resolveId? f catch _ => return none else return none | `(fun $binders* => binop% $f:ident $a $b) => if binders == #[a, b] then try Term.resolveId? f catch _ => return none else return none | _ => return none where expandCDotArg? (stx : Syntax) : MacroM (Option Syntax) := match stx with | `(($e)) => Term.expandCDot? e | _ => Term.expandCDot? stx /- Try to expand `·` notation. Recall that in Lean the `·` notation must be surrounded by parentheses. We may change this is the future, but right now, here are valid examples - `(· + 1)` - `(f ⟨·, 1⟩ ·)` - `(· + ·)` - `(f · a b)` -/ @[builtinMacro Lean.Parser.Term.paren] def expandParen : Macro | `(()) => `(Unit.unit) | `(($e : $type)) => do match (← expandCDot? e) with | some e => `(($e : $type)) | none => Macro.throwUnsupported | `(($e)) => return (← expandCDot? e).getD e | `(($e, $es,*)) => do let pairs ← mkPairs (#[e] ++ es) (← expandCDot? pairs).getD pairs | stx => if !stx[1][0].isMissing && stx[1][1].isMissing then -- parsed `(` and `term`, assume it's a basic parenthesis to get any elaboration output at all `(($(stx[1][0]))) else throw <| Macro.Exception.error stx "unexpected parentheses notation" @[builtinTermElab paren] def elabParen : TermElab := fun stx expectedType? => do match stx with | `(($e : $type)) => let type ← withSynthesize (mayPostpone := true) <| elabType type let e ← elabTerm e type ensureHasType type e | _ => throwUnsupportedSyntax /-- Return `true` if `lhs` is a free variable and `rhs` does not depend on it. -/ private def isSubstCandidate (lhs rhs : Expr) : MetaM Bool := if lhs.isFVar then return !(← dependsOn rhs lhs.fvarId!) else return false /-- Given an expression `e` that is the elaboration of `stx`, if `e` is a free variable, then return `k stx`. Otherwise, return `(fun x => k x) e` -/ private def withLocalIdentFor (stx : Syntax) (e : Expr) (k : Syntax → TermElabM Expr) : TermElabM Expr := do if e.isFVar then k stx else let id ← mkFreshUserName `h let aux ← withLocalDeclD id (← inferType e) fun x => do mkLambdaFVars #[x] (← k (mkIdentFrom stx id)) return mkApp aux e @[builtinTermElab subst] def elabSubst : TermElab := fun stx expectedType? => do let expectedType ← tryPostponeIfHasMVars expectedType? "invalid `▸` notation" match stx with | `($heqStx ▸ $hStx) => do let mut heq ← elabTerm heqStx none let heqType ← inferType heq let heqType ← instantiateMVars heqType match (← Meta.matchEq? heqType) with | none => throwError "invalid `▸` notation, argument{indentExpr heq}\nhas type{indentExpr heqType}\nequality expected" | some (α, lhs, rhs) => let mut lhs := lhs let mut rhs := rhs let mkMotive (typeWithLooseBVar : Expr) := do withLocalDeclD (← mkFreshUserName `x) α fun x => do mkLambdaFVars #[x] $ typeWithLooseBVar.instantiate1 x let mut expectedAbst ← kabstract expectedType rhs unless expectedAbst.hasLooseBVars do expectedAbst ← kabstract expectedType lhs unless expectedAbst.hasLooseBVars do throwError "invalid `▸` notation, expected type{indentExpr expectedType}\ndoes contain equation left-hand-side nor right-hand-side{indentExpr heqType}" heq ← mkEqSymm heq (lhs, rhs) := (rhs, lhs) let hExpectedType := expectedAbst.instantiate1 lhs let (h, badMotive?) ← withRef hStx do let h ← elabTerm hStx hExpectedType try return (← ensureHasType hExpectedType h, none) catch ex => -- if `rhs` occurs in `hType`, we try to apply `heq` to `h` too let hType ← inferType h let hTypeAbst ← kabstract hType rhs unless hTypeAbst.hasLooseBVars do throw ex let hTypeNew := hTypeAbst.instantiate1 lhs unless (← isDefEq hExpectedType hTypeNew) do throw ex let motive ← mkMotive hTypeAbst if !(← isTypeCorrect motive) then return (h, some motive) else return (← mkEqNDRec motive h (← mkEqSymm heq), none) let motive ← mkMotive expectedAbst if badMotive?.isSome || !(← isTypeCorrect motive) then -- Before failing try tos use `subst` if ← (isSubstCandidate lhs rhs <||> isSubstCandidate rhs lhs) then withLocalIdentFor heqStx heq fun heqStx => withLocalIdentFor hStx h fun hStx => do let stxNew ← `(by subst $heqStx; exact $hStx) withMacroExpansion stx stxNew (elabTerm stxNew expectedType) else throwError "invalid `▸` notation, failed to compute motive for the substitution" else mkEqNDRec motive h heq | _ => throwUnsupportedSyntax @[builtinTermElab stateRefT] def elabStateRefT : TermElab := fun stx _ => do let σ ← elabType stx[1] let mut mStx := stx[2] if mStx.getKind == ``Lean.Parser.Term.macroDollarArg then mStx := mStx[1] let m ← elabTerm mStx (← mkArrow (mkSort levelOne) (mkSort levelOne)) let ω ← mkFreshExprMVar (mkSort levelOne) let stWorld ← mkAppM ``STWorld #[ω, m] discard <| mkInstMVar stWorld mkAppM ``StateRefT' #[ω, σ, m] @[builtinTermElab noindex] def elabNoindex : TermElab := fun stx expectedType? => do let e ← elabTerm stx[1] expectedType? return DiscrTree.mkNoindexAnnotation e end Lean.Elab.Term
85c943112c9595862e25a8cdceae974da9e1fa00
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/algebra/order/absolute_value.lean
bf1c7ff477056e8b1fe57c1ddd2f0ca4a8b7593c
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
8,581
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Anne Baanen -/ import algebra.order.field /-! # Absolute values This file defines a bundled type of absolute values `absolute_value R S`. ## Main definitions * `absolute_value R S` is the type of absolute values on `R` mapping to `S`. * `absolute_value.abs` is the "standard" absolute value on `S`, mapping negative `x` to `-x`. * `absolute_value.to_monoid_with_zero_hom`: absolute values mapping to a linear ordered field preserve `0`, `*` and `1` * `is_absolute_value`: a type class stating that `f : β → α` satisfies the axioms of an abs val -/ /-- `absolute_value R S` is the type of absolute values on `R` mapping to `S`: the maps that preserve `*`, are nonnegative, positive definite and satisfy the triangle equality. -/ structure absolute_value (R S : Type*) [semiring R] [ordered_semiring S] extends R →ₙ* S := (nonneg' : ∀ x, 0 ≤ to_fun x) (eq_zero' : ∀ x, to_fun x = 0 ↔ x = 0) (add_le' : ∀ x y, to_fun (x + y) ≤ to_fun x + to_fun y) namespace absolute_value attribute [nolint doc_blame] absolute_value.to_mul_hom initialize_simps_projections absolute_value (to_mul_hom_to_fun → apply) section ordered_semiring variables {R S : Type*} [semiring R] [ordered_semiring S] (abv : absolute_value R S) instance : has_coe_to_fun (absolute_value R S) (λ f, R → S) := ⟨λ f, f.to_fun⟩ @[simp] lemma coe_to_mul_hom : ⇑abv.to_mul_hom = abv := rfl protected theorem nonneg (x : R) : 0 ≤ abv x := abv.nonneg' x @[simp] protected theorem eq_zero {x : R} : abv x = 0 ↔ x = 0 := abv.eq_zero' x protected theorem add_le (x y : R) : abv (x + y) ≤ abv x + abv y := abv.add_le' x y @[simp] protected theorem map_mul (x y : R) : abv (x * y) = abv x * abv y := abv.map_mul' x y protected theorem pos {x : R} (hx : x ≠ 0) : 0 < abv x := lt_of_le_of_ne (abv.nonneg x) (ne.symm $ mt abv.eq_zero.mp hx) @[simp] protected theorem pos_iff {x : R} : 0 < abv x ↔ x ≠ 0 := ⟨λ h₁, mt abv.eq_zero.mpr h₁.ne', abv.pos⟩ protected theorem ne_zero {x : R} (hx : x ≠ 0) : abv x ≠ 0 := (abv.pos hx).ne' @[simp] protected theorem map_zero : abv 0 = 0 := abv.eq_zero.2 rfl end ordered_semiring section ordered_ring variables {R S : Type*} [ring R] [ordered_ring S] (abv : absolute_value R S) protected lemma sub_le (a b c : R) : abv (a - c) ≤ abv (a - b) + abv (b - c) := by simpa [sub_eq_add_neg, add_assoc] using abv.add_le (a - b) (b - c) protected lemma le_sub (a b : R) : abv a - abv b ≤ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv.add_le (a - b) b @[simp] lemma map_sub_eq_zero_iff (a b : R) : abv (a - b) = 0 ↔ a = b := abv.eq_zero.trans sub_eq_zero end ordered_ring section linear_ordered_ring variables {R S : Type*} [semiring R] [linear_ordered_ring S] (abv : absolute_value R S) /-- `absolute_value.abs` is `abs` as a bundled `absolute_value`. -/ @[simps] protected def abs : absolute_value S S := { to_fun := abs, nonneg' := abs_nonneg, eq_zero' := λ _, abs_eq_zero, add_le' := abs_add, map_mul' := abs_mul } instance : inhabited (absolute_value S S) := ⟨absolute_value.abs⟩ variables [nontrivial R] @[simp] protected theorem map_one : abv 1 = 1 := (mul_right_inj' $ abv.ne_zero one_ne_zero).1 $ by rw [← abv.map_mul, mul_one, mul_one] /-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*`, `0` and `1`. -/ def to_monoid_with_zero_hom : R →*₀ S := { to_fun := abv, map_zero' := abv.map_zero, map_one' := abv.map_one, .. abv } @[simp] lemma coe_to_monoid_with_zero_hom : ⇑abv.to_monoid_with_zero_hom = abv := rfl /-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*` and `1`. -/ def to_monoid_hom : monoid_hom R S := { to_fun := abv, map_one' := abv.map_one, .. abv } @[simp] lemma coe_to_monoid_hom : ⇑abv.to_monoid_hom = abv := rfl @[simp] protected lemma map_pow (a : R) (n : ℕ) : abv (a ^ n) = abv a ^ n := abv.to_monoid_hom.map_pow a n end linear_ordered_ring section linear_ordered_comm_ring section ring variables {R S : Type*} [ring R] [linear_ordered_comm_ring S] (abv : absolute_value R S) @[simp] protected theorem map_neg (a : R) : abv (-a) = abv a := begin by_cases ha : a = 0, { simp [ha] }, refine (mul_self_eq_mul_self_iff.mp (by rw [← abv.map_mul, neg_mul_neg, abv.map_mul])).resolve_right _, exact ((neg_lt_zero.mpr (abv.pos ha)).trans (abv.pos (neg_ne_zero.mpr ha))).ne' end protected theorem map_sub (a b : R) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv.map_neg] lemma abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≤ abv (a - b) := abs_sub_le_iff.2 ⟨abv.le_sub _ _, by rw abv.map_sub; apply abv.le_sub⟩ end ring end linear_ordered_comm_ring end absolute_value section is_absolute_value /-- A function `f` is an absolute value if it is nonnegative, zero only at 0, additive, and multiplicative. See also the type `absolute_value` which represents a bundled version of absolute values. -/ class is_absolute_value {S} [ordered_semiring S] {R} [semiring R] (f : R → S) : Prop := (abv_nonneg [] : ∀ x, 0 ≤ f x) (abv_eq_zero [] : ∀ {x}, f x = 0 ↔ x = 0) (abv_add [] : ∀ x y, f (x + y) ≤ f x + f y) (abv_mul [] : ∀ x y, f (x * y) = f x * f y) namespace is_absolute_value section ordered_semiring variables {S : Type*} [ordered_semiring S] variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv] /-- A bundled absolute value is an absolute value. -/ instance absolute_value.is_absolute_value (abv : absolute_value R S) : is_absolute_value abv := { abv_nonneg := abv.nonneg, abv_eq_zero := λ _, abv.eq_zero, abv_add := abv.add_le, abv_mul := abv.map_mul } /-- Convert an unbundled `is_absolute_value` to a bundled `absolute_value`. -/ @[simps] def to_absolute_value : absolute_value R S := { to_fun := abv, add_le' := abv_add abv, eq_zero' := λ _, abv_eq_zero abv, nonneg' := abv_nonneg abv, map_mul' := abv_mul abv } theorem abv_zero : abv 0 = 0 := (abv_eq_zero abv).2 rfl theorem abv_pos {a : R} : 0 < abv a ↔ a ≠ 0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [abv_eq_zero abv, abv_nonneg abv] end ordered_semiring section linear_ordered_ring variables {S : Type*} [linear_ordered_ring S] variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv] instance abs_is_absolute_value {S} [linear_ordered_ring S] : is_absolute_value (abs : S → S) := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } end linear_ordered_ring section linear_ordered_comm_ring variables {S : Type*} [linear_ordered_comm_ring S] section semiring variables {R : Type*} [semiring R] (abv : R → S) [is_absolute_value abv] theorem abv_one [nontrivial R] : abv 1 = 1 := (mul_right_inj' $ mt (abv_eq_zero abv).1 one_ne_zero).1 $ by rw [← abv_mul abv, mul_one, mul_one] /-- `abv` as a `monoid_with_zero_hom`. -/ def abv_hom [nontrivial R] : R →*₀ S := ⟨abv, abv_zero abv, abv_one abv, abv_mul abv⟩ lemma abv_pow [nontrivial R] (abv : R → S) [is_absolute_value abv] (a : R) (n : ℕ) : abv (a ^ n) = abv a ^ n := (abv_hom abv).to_monoid_hom.map_pow a n end semiring end linear_ordered_comm_ring section linear_ordered_field variables {S : Type*} [linear_ordered_field S] section ring variables {R : Type*} [ring R] (abv : R → S) [is_absolute_value abv] theorem abv_neg (a : R) : abv (-a) = abv a := by rw [← mul_self_inj_of_nonneg (abv_nonneg abv _) (abv_nonneg abv _), ← abv_mul abv, ← abv_mul abv]; simp theorem abv_sub (a b : R) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv_neg abv] lemma abv_sub_le (a b c : R) : abv (a - c) ≤ abv (a - b) + abv (b - c) := by simpa [sub_eq_add_neg, add_assoc] using abv_add abv (a - b) (b - c) lemma sub_abv_le_abv_sub (a b : R) : abv a - abv b ≤ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv_add abv (a - b) b lemma abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≤ abv (a - b) := abs_sub_le_iff.2 ⟨sub_abv_le_abv_sub abv _ _, by rw abv_sub abv; apply sub_abv_le_abv_sub abv⟩ end ring section field variables {R : Type*} [division_ring R] (abv : R → S) [is_absolute_value abv] theorem abv_inv (a : R) : abv a⁻¹ = (abv a)⁻¹ := map_inv₀ (abv_hom abv) a theorem abv_div (a b : R) : abv (a / b) = abv a / abv b := map_div₀ (abv_hom abv) a b end field end linear_ordered_field end is_absolute_value end is_absolute_value
79808fb325d72e44571a261829a6786137b52558
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_1311.lean
b6317f80c1c3f0aff5bdd171dd68bd3e79fe650e
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
270
lean
import data.real.basic def fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def fn_has_ub (f : ℝ → ℝ) := ∃ a, fn_ub f a open_locale classical variable (f : ℝ → ℝ) -- BEGIN example (h : ¬ fn_has_ub f) : ∀ a, ∃ x, f x > a := sorry -- END
65894e20ff02603ec740651b8c999e720dd49d44
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/imo/imo1988_q6.lean
522c66e067cf7bf82078d9d43591cc6b3142863f
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
13,869
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.prime import data.rat.basic import order.well_founded import tactic.linarith /-! # IMO 1988 Q6 and constant descent Vieta jumping Question 6 of IMO1988 is somewhat (in)famous. Several expert problem solvers could not tackle the question within the given time limit. The problem lead to the introduction of a new proof technique, so called “Vieta jumping”. In this file we formalise constant descent Vieta jumping, and apply this to prove Q6 of IMO1988. To illustrate the technique, we also prove a similar result. -/ -- open_locale classical local attribute [instance] classical.prop_decidable local attribute [simp] sq /-- Constant descent Vieta jumping. This proof technique allows one to prove an arbitrary proposition `claim`, by running a descent argument on a hyperbola `H` in the first quadrant of the plane, under the following conditions: * `h₀` : There exists an integral point `(x,y)` on the hyperbola `H`. * `H_symm` : The hyperbola has a symmetry along the diagonal in the plane. * `H_zero` : If an integral point `(x,0)` lies on the hyperbola `H`, then `claim` is true. * `H_diag` : If an integral point `(x,x)` lies on the hyperbola `H`, then `claim` is true. * `H_desc` : If `(x,y)` is an integral point on the hyperbola `H`, with `x < y` then there exists a “smaller” point on `H`: a point `(x',y')` with `x' < y' ≤ x`. For reasons of usability, the hyperbola `H` is implemented as an arbitrary predicate. (In question 6 of IMO1988, where this proof technique was first developped, the predicate `claim` would be `∃ (d : ℕ), d ^ 2 = k` for some natural number `k`, and the predicate `H` would be `λ a b, a * a + b * b = (a * b + 1) * k`.) To ensure that the predicate `H` actually describes a hyperbola, the user must provide arguments `B` and `C` that are used as coefficients for a quadratic equation. Finally, `H_quad` is the proof obligation that the quadratic equation `(y:ℤ) * y - B x * y + C x = 0` describes the same hyperbola as the predicate `H`. For extra flexibility, one must provide a predicate `base` on the integral points in the plane. In the descent step `H_desc` this will give the user the additional assumption that the point `(x,y)` does not lie in this base locus. The user must provide a proof that the proposition `claim` is true if there exists an integral point `(x,y)` on the hyperbola `H` that lies in the base locus. If such a base locus is not necessary, once can simply let it be `λ x y, false`. -/ lemma constant_descent_vieta_jumping (x y : ℕ) {claim : Prop} {H : ℕ → ℕ → Prop} (h₀ : H x y) (B : ℕ → ℤ) (C : ℕ → ℤ) (base : ℕ → ℕ → Prop) (H_quad : ∀ {x y}, H x y ↔ (y:ℤ) * y - B x * y + C x = 0) (H_symm : ∀ {x y}, H x y ↔ H y x) (H_zero : ∀ {x}, H x 0 → claim) (H_diag : ∀ {x}, H x x → claim) (H_desc : ∀ {x y}, 0 < x → x < y → ¬base x y → H x y → ∀ y', y' * y' - B x * y' + C x = 0 → y' = B x - y → y' * y = C x → 0 ≤ y' ∧ y' ≤ x) (H_base : ∀ {x y}, H x y → base x y → claim) : claim := begin -- First of all, we may assume that x ≤ y. -- We justify this using H_symm. wlog hxy : x ≤ y, swap, { rw H_symm at h₀, solve_by_elim }, -- In fact, we can easily deal with the case x = y. by_cases x_eq_y : x = y, {subst x_eq_y, exact H_diag h₀}, -- Hence we may assume that x < y. replace hxy : x < y := lt_of_le_of_ne hxy x_eq_y, clear x_eq_y, -- Consider the upper branch of the hyperbola defined by H. let upper_branch : set (ℕ × ℕ) := {p | H p.1 p.2 ∧ p.1 < p.2}, -- Note that the point p = (x,y) lies on the upper branch. let p : ℕ × ℕ := ⟨x,y⟩, have hp : p ∈ upper_branch := ⟨h₀, hxy⟩, -- We also consider the exceptional set of solutions (a,b) that satisfy -- a = 0 or a = b or B a = b or B a = b + a or that lie in the base locus. let exceptional : set (ℕ × ℕ) := {p | H p.1 p.2 ∧ (base p.1 p.2 ∨ p.1 = 0 ∨ p.1 = p.2 ∨ B p.1 = p.2 ∨ B p.1 = p.2 + p.1) }, -- Let S be the projection of the upper branch on to the y-axis -- after removing the exceptional locus. let S : set ℕ := prod.snd '' (upper_branch \ exceptional), -- The strategy is to show that the exceptional locus in nonempty -- by running a descent argument that starts with the given point p = (x,y). -- Our assumptions ensure that we can then prove the claim. suffices exc : exceptional.nonempty, { -- Suppose that there exists an element in the exceptional locus. simp [exceptional, -add_comm, set.nonempty] at exc, -- Let (a,b) be such an element, and consider all the possible cases. rcases exc with ⟨a, b, hH, hb⟩, rcases hb with _|rfl|rfl|hB|hB, -- The first three cases are rather easy to solve. { solve_by_elim }, { rw H_symm at hH, solve_by_elim }, { solve_by_elim }, -- The final two cases are very similar. all_goals { -- Consider the quadratic equation that (a,b) satisfies. rw H_quad at hH, -- We find the other root of the equation, and Vieta's formulas. rcases Vieta_formula_quadratic hH with ⟨c, h_root, hV₁, hV₂⟩, -- By substitutions we find that b = 0 or b = a. simp [hB] at hV₁, subst hV₁, rw [← int.coe_nat_zero] at *, rw ← H_quad at h_root, -- And hence we are done by H_zero and H_diag. solve_by_elim } }, -- To finish the main proof, we need to show that the exceptional locus is nonempty. -- So we assume that the exceptional locus is empty, and work towards dering a contradiction. rw ← set.ne_empty_iff_nonempty, assume exceptional_empty, -- Observe that S is nonempty. have S_nonempty : S.nonempty, { -- It contains the image of p. use p.2, apply set.mem_image_of_mem, -- After all, we assumed that the exceptional locus is empty. rwa [exceptional_empty, set.diff_empty], }, -- We are now set for an infinite descent argument. -- Let m be the smallest element of the nonempty set S. let m : ℕ := well_founded.min nat.lt_wf S S_nonempty, have m_mem : m ∈ S := well_founded.min_mem nat.lt_wf S S_nonempty, have m_min : ∀ k ∈ S, ¬ k < m := λ k hk, well_founded.not_lt_min nat.lt_wf S S_nonempty hk, -- It suffices to show that there is point (a,b) with b ∈ S and b < m. suffices hp' : ∃ p' : ℕ × ℕ, p'.2 ∈ S ∧ p'.2 < m, { rcases hp' with ⟨p', p'_mem, p'_small⟩, solve_by_elim }, -- Let (m_x, m_y) be a point on the upper branch that projects to m ∈ S -- and that does not lie in the exceptional locus. rcases m_mem with ⟨⟨mx, my⟩, ⟨⟨hHm, mx_lt_my⟩, h_base⟩, m_eq⟩, -- This means that m_y = m, -- and the conditions H(m_x, m_y) and m_x < m_y are satisfied. simp [exceptional, hHm] at mx_lt_my h_base m_eq, push_neg at h_base, -- Finally, it also means that (m_x, m_y) does not lie in the base locus, -- that m_x ≠ 0, m_x ≠ m_y, B(m_x) ≠ m_y, and B(m_x) ≠ m_x + m_y. rcases h_base with ⟨h_base, hmx, hm_diag, hm_B₁, hm_B₂⟩, replace hmx : 0 < mx := pos_iff_ne_zero.mpr hmx, -- Consider the quadratic equation that (m_x, m_y) satisfies. have h_quad := hHm, rw H_quad at h_quad, -- We find the other root of the equation, and Vieta's formulas. rcases Vieta_formula_quadratic h_quad with ⟨c, h_root, hV₁, hV₂⟩, -- No we rewrite Vietas formulas a bit, and apply the descent step. replace hV₁ : c = B mx - my := eq_sub_of_add_eq' hV₁, rw mul_comm at hV₂, have Hc := H_desc hmx mx_lt_my h_base hHm c h_root hV₁ hV₂, -- This means that we may assume that c ≥ 0 and c ≤ m_x. cases Hc with c_nonneg c_lt, -- In other words, c is a natural number. lift c to ℕ using c_nonneg, -- Recall that we are trying find a point (a,b) such that b ∈ S and b < m. -- We claim that p' = (c, m_x) does the job. let p' : ℕ × ℕ := ⟨c, mx⟩, use p', -- The second condition is rather easy to check, so we do that first. split, swap, { rwa m_eq at mx_lt_my }, -- Now we need to show that p' projects onto S. In other words, that c ∈ S. -- We do that, by showing that it lies in the upper branch -- (which is sufficient, because we assumed that the exceptional locus is empty). apply set.mem_image_of_mem, rw [exceptional_empty, set.diff_empty], -- Now we are ready to prove that p' = (c, m_x) lies on the upper branch. -- We need to check two conditions: H(c, m_x) and c < m_x. split; dsimp only, { -- The first condition is not so hard. After all, c is the other root of the quadratic equation. rw [H_symm, H_quad], simpa using h_root, }, { -- For the second condition, we note that it suffices to check that c ≠ m_x. suffices hc : c ≠ mx, { refine lt_of_le_of_ne _ hc, exact_mod_cast c_lt, }, -- However, recall that B(m_x) ≠ m_x + m_y. -- If c = m_x, we can prove B(m_x) = m_x + m_y. contrapose! hm_B₂, subst c, simp [hV₁], } -- Hence p' = (c, m_x) lies on the upper branch, and we are done. end /--Question 6 of IMO1988. If a and b are two natural numbers such that a*b+1 divides a^2 + b^2, show that their quotient is a perfect square.-/ lemma imo1988_q6 {a b : ℕ} (h : (a*b+1) ∣ a^2 + b^2) : ∃ d, d^2 = (a^2 + b^2)/(a*b + 1) := begin rcases h with ⟨k, hk⟩, rw [hk, nat.mul_div_cancel_left _ (nat.succ_pos (a*b))], simp only [sq] at hk, apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x - k) (λ x y, false); clear hk a b, { -- We will now show that the fibers of the solution set are described by a quadratic equation. intros x y, dsimp only, rw [← int.coe_nat_inj', ← sub_eq_zero], apply eq_iff_eq_cancel_right.2, norm_cast, simp, ring, }, { -- Show that the solution set is symmetric in a and b. intros x y, simp [add_comm (x*x), mul_comm x], }, { -- Show that the claim is true if b = 0. suffices : ∀ a, a * a = k → ∃ d, d * d = k, by simpa, rintros x rfl, use x }, { -- Show that the claim is true if a = b. intros x hx, suffices : k ≤ 1, { rw [nat.le_add_one_iff, nat.le_zero_iff] at this, rcases this with rfl|rfl, { use 0, simp }, { use 1, simp } }, contrapose! hx with k_lt_one, apply ne_of_lt, calc x*x + x*x = x*x * 2 : by rw mul_two ... ≤ x*x * k : nat.mul_le_mul_left (x*x) k_lt_one ... < (x*x + 1) * k : by linarith }, { -- Show the descent step. intros x y hx x_lt_y hxky h z h_root hV₁ hV₀, split, { dsimp [-sub_eq_add_neg] at *, have hpos : z*z + x*x > 0, { apply add_pos_of_nonneg_of_pos, { apply mul_self_nonneg }, { apply mul_pos; exact_mod_cast hx }, }, have hzx : z*z + x*x = (z * x + 1) * k, { rw [← sub_eq_zero, ← h_root], ring, }, rw hzx at hpos, replace hpos : z * x + 1 > 0 := pos_of_mul_pos_right hpos (int.coe_zero_le k), replace hpos : z * x ≥ 0 := int.le_of_lt_add_one hpos, apply nonneg_of_mul_nonneg_right hpos (by exact_mod_cast hx), }, { contrapose! hV₀ with x_lt_z, apply ne_of_gt, calc z * y > x*x : by apply mul_lt_mul'; linarith ... ≥ x*x - k : sub_le_self _ (int.coe_zero_le k) }, }, { -- There is no base case in this application of Vieta jumping. simp }, end /- The following example illustrates the use of constant descent Vieta jumping in the presence of a non-trivial base case. -/ example {a b : ℕ} (h : a*b ∣ a^2 + b^2 + 1) : 3*a*b = a^2 + b^2 + 1 := begin rcases h with ⟨k, hk⟩, suffices : k = 3, { simp * at *, ring, }, simp only [sq] at hk, apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x + 1) (λ x y, x ≤ 1); clear hk a b, { -- We will now show that the fibers of the solution set are described by a quadratic equation. intros x y, dsimp only, rw [← int.coe_nat_inj', ← sub_eq_zero], apply eq_iff_eq_cancel_right.2, simp, ring, }, { -- Show that the solution set is symmetric in a and b. cc }, { -- Show that the claim is true if b = 0. simp }, { -- Show that the claim is true if a = b. intros x hx, have x_sq_dvd : x*x ∣ x*x*k := dvd_mul_right (x*x) k, rw ← hx at x_sq_dvd, obtain ⟨y, hy⟩ : x * x ∣ 1 := by simpa only [nat.dvd_add_self_left, add_assoc] using x_sq_dvd, obtain ⟨rfl,rfl⟩ : x = 1 ∧ y = 1 := by simpa [nat.mul_eq_one_iff] using hy.symm, simpa using hx.symm, }, { -- Show the descent step. intros x y x_lt_y hx h_base h z h_root hV₁ hV₀, split, { have zy_pos : z * y ≥ 0, { rw hV₀, exact_mod_cast (nat.zero_le _) }, apply nonneg_of_mul_nonneg_right zy_pos, linarith }, { contrapose! hV₀ with x_lt_z, apply ne_of_gt, push_neg at h_base, calc z * y > x * y : by apply mul_lt_mul_of_pos_right; linarith ... ≥ x * (x + 1) : by apply mul_le_mul; linarith ... > x * x + 1 : begin rw [mul_add, mul_one], apply add_lt_add_left, assumption_mod_cast end, } }, { -- Show the base case. intros x y h h_base, obtain rfl|rfl : x = 0 ∨ x = 1 := by rwa [nat.le_add_one_iff, nat.le_zero_iff] at h_base, { simpa using h, }, { simp only [mul_one, one_mul, add_comm, zero_add] at h, have y_dvd : y ∣ y * k := dvd_mul_right y k, rw [← h, ← add_assoc, nat.dvd_add_left (dvd_mul_left y y)] at y_dvd, obtain rfl|rfl := (nat.dvd_prime nat.prime_two).mp y_dvd; apply nat.eq_of_mul_eq_mul_left, exacts [zero_lt_one, h.symm, zero_lt_two, h.symm] } } end
dd9aa1e6b403b74891a9bcd4051fb96251c5a716
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/homotopy/cylinder.hlean
33df53e9a63407e02fe3f75af92fbb572393ce7c
[ "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
3,190
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Declaration of mapping cylinders -/ import hit.quotient open quotient eq sum equiv equiv.ops namespace cylinder section universe u parameters {A B : Type.{u}} (f : A → B) local abbreviation C := B + A inductive cylinder_rel : C → C → Type := | Rmk : Π(a : A), cylinder_rel (inl (f a)) (inr a) open cylinder_rel local abbreviation R := cylinder_rel definition cylinder := quotient cylinder_rel -- TODO: define this in root namespace definition base (b : B) : cylinder := class_of R (inl b) definition top (a : A) : cylinder := class_of R (inr a) definition seg (a : A) : base (f a) = top a := eq_of_rel cylinder_rel (Rmk f a) protected definition rec {P : cylinder → Type} (Pbase : Π(b : B), P (base b)) (Ptop : Π(a : A), P (top a)) (Pseg : Π(a : A), Pbase (f a) =[seg a] Ptop a) (x : cylinder) : P x := begin induction x, { cases a, apply Pbase, apply Ptop}, { cases H, apply Pseg} end protected definition rec_on [reducible] {P : cylinder → Type} (x : cylinder) (Pbase : Π(b : B), P (base b)) (Ptop : Π(a : A), P (top a)) (Pseg : Π(a : A), Pbase (f a) =[seg a] Ptop a) : P x := rec Pbase Ptop Pseg x theorem rec_seg {P : cylinder → Type} (Pbase : Π(b : B), P (base b)) (Ptop : Π(a : A), P (top a)) (Pseg : Π(a : A), Pbase (f a) =[seg a] Ptop a) (a : A) : apdo (rec Pbase Ptop Pseg) (seg a) = Pseg a := !rec_eq_of_rel protected definition elim {P : Type} (Pbase : B → P) (Ptop : A → P) (Pseg : Π(a : A), Pbase (f a) = Ptop a) (x : cylinder) : P := rec Pbase Ptop (λa, pathover_of_eq (Pseg a)) x protected definition elim_on [reducible] {P : Type} (x : cylinder) (Pbase : B → P) (Ptop : A → P) (Pseg : Π(a : A), Pbase (f a) = Ptop a) : P := elim Pbase Ptop Pseg x theorem elim_seg {P : Type} (Pbase : B → P) (Ptop : A → P) (Pseg : Π(a : A), Pbase (f a) = Ptop a) (a : A) : ap (elim Pbase Ptop Pseg) (seg a) = Pseg a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (seg a)), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_seg], end protected definition elim_type (Pbase : B → Type) (Ptop : A → Type) (Pseg : Π(a : A), Pbase (f a) ≃ Ptop a) (x : cylinder) : Type := elim Pbase Ptop (λa, ua (Pseg a)) x protected definition elim_type_on [reducible] (x : cylinder) (Pbase : B → Type) (Ptop : A → Type) (Pseg : Π(a : A), Pbase (f a) ≃ Ptop a) : Type := elim_type Pbase Ptop Pseg x theorem elim_type_seg (Pbase : B → Type) (Ptop : A → Type) (Pseg : Π(a : A), Pbase (f a) ≃ Ptop a) (a : A) : transport (elim_type Pbase Ptop Pseg) (seg a) = Pseg a := by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_seg];apply cast_ua_fn end end cylinder attribute cylinder.base cylinder.top [constructor] attribute cylinder.rec cylinder.elim [unfold 8] [recursor 8] attribute cylinder.elim_type [unfold 7] attribute cylinder.rec_on cylinder.elim_on [unfold 5] attribute cylinder.elim_type_on [unfold 4]
21fc1dbb76be847db37a2e2f6e73b29479e462b5
e8d53a7b78545d183a23dd7bd921bc7ff312989f
/cubical/square.hlean
f9c4ce5ba5a40b3bb68903199ab16cc22c9bdc61
[]
no_license
Sumit0730/Impredicative
857007626592440a27cf4440aa9a226d0ede7f3e
a75cb9989a684133d31d4889a746ee4fa7b66cea
refs/heads/master
1,631,994,804,745
1,531,980,761,000
1,531,980,761,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,819
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jakob von Raumer Squares in a type -/ import types.eq open eq equiv is_equiv sigma namespace eq variables {A B C : Type} {a a' a'' a₀₀ a₂₀ a₄₀ a₀₂ a₂₂ a₂₄ a₀₄ a₄₂ a₄₄ a₁ a₂ a₃ a₄ : A} /-a₀₀-/ {p₁₀ p₁₀' : a₀₀ = a₂₀} /-a₂₀-/ {p₃₀ : a₂₀ = a₄₀} /-a₄₀-/ {p₀₁ p₀₁' : a₀₀ = a₀₂} /-s₁₁-/ {p₂₁ p₂₁' : a₂₀ = a₂₂} /-s₃₁-/ {p₄₁ : a₄₀ = a₄₂} /-a₀₂-/ {p₁₂ p₁₂' : a₀₂ = a₂₂} /-a₂₂-/ {p₃₂ : a₂₂ = a₄₂} /-a₄₂-/ {p₀₃ : a₀₂ = a₀₄} /-s₁₃-/ {p₂₃ : a₂₂ = a₂₄} /-s₃₃-/ {p₄₃ : a₄₂ = a₄₄} /-a₀₄-/ {p₁₄ : a₀₄ = a₂₄} /-a₂₄-/ {p₃₄ : a₂₄ = a₄₄} /-a₄₄-/ {b : B} {c : C} inductive square {A : Type} {a₀₀ : A} : Π{a₂₀ a₀₂ a₂₂ : A}, a₀₀ = a₂₀ → a₀₂ = a₂₂ → a₀₀ = a₀₂ → a₂₀ = a₂₂ → Type := ids : square idp idp idp idp /- square top bottom left right -/ variables {s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁} {s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁} {s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃} {s₃₃ : square p₃₂ p₃₄ p₂₃ p₄₃} definition ids [reducible] [constructor] := @square.ids definition idsquare [reducible] [constructor] (a : A) := @square.ids A a definition hrefl [unfold 4] (p : a = a') : square idp idp p p := by induction p; exact ids definition vrefl [unfold 4] (p : a = a') : square p p idp idp := by induction p; exact ids definition hrfl [reducible] [unfold 4] {p : a = a'} : square idp idp p p := !hrefl definition vrfl [reducible] [unfold 4] {p : a = a'} : square p p idp idp := !vrefl definition hdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square idp idp p q := by induction r;apply hrefl definition vdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square p q idp idp := by induction r;apply vrefl definition hdeg_square_idp (p : a = a') : hdeg_square (refl p) = hrfl := by reflexivity definition vdeg_square_idp (p : a = a') : vdeg_square (refl p) = vrfl := by reflexivity definition hconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁) : square (p₁₀ ⬝ p₃₀) (p₁₂ ⬝ p₃₂) p₀₁ p₄₁ := by induction s₃₁; exact s₁₁ definition vconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃) : square p₁₀ p₁₄ (p₀₁ ⬝ p₀₃) (p₂₁ ⬝ p₂₃) := by induction s₁₃; exact s₁₁ definition dconcat [unfold 14] {p₀₀ : a₀₀ = a} {p₂₂ : a = a₂₂} (s₂₁ : square p₀₀ p₁₂ p₀₁ p₂₂) (s₁₂ : square p₁₀ p₂₂ p₀₀ p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction s₁₂; exact s₂₁ definition hinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₁₂⁻¹ p₂₁ p₀₁ := by induction s₁₁;exact ids definition vinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₂ p₁₀ p₀₁⁻¹ p₂₁⁻¹ := by induction s₁₁;exact ids definition eq_vconcat [unfold 11] {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p p₁₂ p₀₁ p₂₁ := by induction r; exact s₁₁ definition vconcat_eq [unfold 12] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : square p₁₀ p p₀₁ p₂₁ := by induction r; exact s₁₁ definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ p₁₂ p p₂₁ := by induction r; exact s₁₁ definition hconcat_eq [unfold 12] {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : square p₁₀ p₁₂ p₀₁ p := by induction r; exact s₁₁ infix ` ⬝h `:69 := hconcat --type using \tr infix ` ⬝v `:70 := vconcat --type using \tr infixl ` ⬝hp `:71 := hconcat_eq --type using \tr infixl ` ⬝vp `:73 := vconcat_eq --type using \tr infixr ` ⬝ph `:72 := eq_hconcat --type using \tr infixr ` ⬝pv `:74 := eq_vconcat --type using \tr postfix `⁻¹ʰ`:(max+1) := hinverse --type using \-1h postfix `⁻¹ᵛ`:(max+1) := vinverse --type using \-1v definition transpose [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₀₁ p₂₁ p₁₀ p₁₂ := by induction s₁₁;exact ids definition aps [unfold 12] (f : A → B) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (ap f p₁₀) (ap f p₁₂) (ap f p₀₁) (ap f p₂₁) := by induction s₁₁;exact ids /- canceling, whiskering and moving thinks along the sides of the square -/ definition whisker_tl (p : a = a₀₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁ := by induction s₁₁;induction p;constructor definition whisker_br (p : a₂₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p) := by induction p;exact s₁₁ definition whisker_rt (p : a = a₂₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁) := by induction s₁₁;induction p;constructor definition whisker_tr (p : a₂₀ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁) := by induction s₁₁;induction p;constructor definition whisker_bl (p : a = a₀₂) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁ := by induction s₁₁;induction p;constructor definition whisker_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁ := by induction s₁₁;induction p;constructor definition cancel_tl (p : a = a₀₀) (s₁₁ : square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite +idp_con at s₁₁; exact s₁₁ definition cancel_br (p : a₂₂ = a) (s₁₁ : square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p;exact s₁₁ definition cancel_rt (p : a = a₂₀) (s₁₁ : square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite idp_con at s₁₁; exact s₁₁ definition cancel_tr (p : a₂₀ = a) (s₁₁ : square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁ definition cancel_bl (p : a = a₀₂) (s₁₁ : square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite idp_con at s₁₁; exact s₁₁ definition cancel_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁ definition move_top_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁) : square (p⁻¹ ⬝ p₁₀) p₁₂ q p₂₁ := by apply cancel_tl p; rewrite con_inv_cancel_left; exact s definition move_top_of_left' {p : a = a₀₀} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p⁻¹ ⬝ q) p₂₁) : square (p ⬝ p₁₀) p₁₂ q p₂₁ := by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s definition move_left_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁) : square q p₁₂ (p⁻¹ ⬝ p₀₁) p₂₁ := by apply cancel_tl p; rewrite con_inv_cancel_left; exact s definition move_left_of_top' {p : a = a₀₀} {q : a = a₂₀} (s : square (p⁻¹ ⬝ q) p₁₂ p₀₁ p₂₁) : square q p₁₂ (p ⬝ p₀₁) p₂₁ := by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s definition move_bot_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q)) : square p₁₀ (p₁₂ ⬝ q⁻¹) p₀₁ p := by apply cancel_br q; rewrite inv_con_cancel_right; exact s definition move_bot_of_right' {p : a₂₀ = a} {q : a₂₂ = a} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q⁻¹)) : square p₁₀ (p₁₂ ⬝ q) p₀₁ p := by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s definition move_right_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁) : square p₁₀ p p₀₁ (p₂₁ ⬝ q⁻¹) := by apply cancel_br q; rewrite inv_con_cancel_right; exact s definition move_right_of_bot' {p : a₀₂ = a} {q : a₂₂ = a} (s : square p₁₀ (p ⬝ q⁻¹) p₀₁ p₂₁) : square p₁₀ p p₀₁ (p₂₁ ⬝ q) := by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s definition move_top_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q)) : square (p₁₀ ⬝ p) p₁₂ p₀₁ q := by apply cancel_rt p; rewrite con_inv_cancel_right; exact s definition move_right_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁) : square p p₁₂ p₀₁ (q ⬝ p₂₁) := by apply cancel_tr q; rewrite inv_con_cancel_left; exact s definition move_bot_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁) : square p₁₀ (q ⬝ p₁₂) p p₂₁ := by apply cancel_lb q; rewrite inv_con_cancel_left; exact s definition move_left_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁) : square p₁₀ q (p₀₁ ⬝ p) p₂₁ := by apply cancel_bl p; rewrite con_inv_cancel_right; exact s /- some higher ∞-groupoid operations -/ definition vconcat_vrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : s₁₁ ⬝v vrefl p₁₂ = s₁₁ := by induction s₁₁; reflexivity definition hconcat_hrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : s₁₁ ⬝h hrefl p₂₁ = s₁₁ := by induction s₁₁; reflexivity /- equivalences -/ definition eq_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂ := by induction s₁₁; apply idp definition square_of_eq (r : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₁₂; esimp at r; induction r; induction p₂₁; induction p₁₀; exact ids definition eq_top_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹ := by induction s₁₁; apply idp definition square_of_eq_top (r : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₂₁; induction p₁₂; esimp at r;induction r;induction p₁₀;exact ids definition eq_bot_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₂ = p₀₁⁻¹ ⬝ p₁₀ ⬝ p₂₁ := by induction s₁₁; apply idp definition square_of_eq_bot (r : p₀₁⁻¹ ⬝ p₁₀ ⬝ p₂₁ = p₁₂) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₂₁; induction p₁₀; esimp at r; induction r; induction p₀₁; exact ids definition square_equiv_eq [constructor] (t : a₀₀ = a₀₂) (b : a₂₀ = a₂₂) (l : a₀₀ = a₂₀) (r : a₀₂ = a₂₂) : square t b l r ≃ t ⬝ r = l ⬝ b := begin fapply equiv.MK, { exact eq_of_square}, { exact square_of_eq}, { intro s, induction b, esimp [concat] at s, induction s, induction r, induction t, apply idp}, { intro s, induction s, apply idp}, end definition hdeg_square_equiv' [constructor] (p q : a = a') : square idp idp p q ≃ p = q := by transitivity _;apply square_equiv_eq;transitivity _;apply eq_equiv_eq_symm; apply equiv_eq_closed_right;apply idp_con definition vdeg_square_equiv' [constructor] (p q : a = a') : square p q idp idp ≃ p = q := by transitivity _;apply square_equiv_eq;apply equiv_eq_closed_right; apply idp_con definition eq_of_hdeg_square [reducible] {p q : a = a'} (s : square idp idp p q) : p = q := to_fun !hdeg_square_equiv' s definition eq_of_vdeg_square [reducible] {p q : a = a'} (s : square p q idp idp) : p = q := to_fun !vdeg_square_equiv' s definition top_deg_square (l : a₁ = a₂) (b : a₂ = a₃) (r : a₄ = a₃) : square (l ⬝ b ⬝ r⁻¹) b l r := by induction r;induction b;induction l;constructor definition bot_deg_square (l : a₁ = a₂) (t : a₁ = a₃) (r : a₃ = a₄) : square t (l⁻¹ ⬝ t ⬝ r) l r := by induction r;induction t;induction l;constructor /- the following two equivalences have as underlying inverse function the functions hdeg_square and vdeg_square, respectively. See example below the definition -/ definition hdeg_square_equiv [constructor] (p q : a = a') : square idp idp p q ≃ p = q := begin fapply equiv_change_fun, { fapply equiv_change_inv, apply hdeg_square_equiv', exact hdeg_square, intro s, induction s, induction p, reflexivity}, { exact eq_of_hdeg_square}, { reflexivity} end definition vdeg_square_equiv [constructor] (p q : a = a') : square p q idp idp ≃ p = q := begin fapply equiv_change_fun, { fapply equiv_change_inv, apply vdeg_square_equiv',exact vdeg_square, intro s, induction s, induction p, reflexivity}, { exact eq_of_vdeg_square}, { reflexivity} end example (p q : a = a') : to_inv (hdeg_square_equiv p q) = hdeg_square := idp /- characterization of pathovers in a equality type. The type B of the equality is fixed here. A version where B may also varies over the path p is given in the file squareover -/ definition eq_pathover [unfold 7] {f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'} (s : square q r (ap f p) (ap g p)) : q =[p] r := begin induction p, apply pathover_idp_of_eq, exact eq_of_vdeg_square s end definition eq_pathover_constant_left {g : A → B} {p : a = a'} {b : B} {q : b = g a} {r : b = g a'} (s : square q r idp (ap g p)) : q =[p] r := eq_pathover (ap_constant p b ⬝ph s) definition eq_pathover_id_left {g : A → A} {p : a = a'} {q : a = g a} {r : a' = g a'} (s : square q r p (ap g p)) : q =[p] r := eq_pathover (ap_id p ⬝ph s) definition eq_pathover_constant_right {f : A → B} {p : a = a'} {b : B} {q : f a = b} {r : f a' = b} (s : square q r (ap f p) idp) : q =[p] r := eq_pathover (s ⬝hp (ap_constant p b)⁻¹) definition eq_pathover_id_right {f : A → A} {p : a = a'} {q : f a = a} {r : f a' = a'} (s : square q r (ap f p) p) : q =[p] r := eq_pathover (s ⬝hp (ap_id p)⁻¹) definition square_of_pathover [unfold 7] {f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'} (s : q =[p] r) : square q r (ap f p) (ap g p) := by induction p;apply vdeg_square;exact eq_of_pathover_idp s definition eq_pathover_constant_left_id_right {p : a = a'} {a₀ : A} {q : a₀ = a} {r : a₀ = a'} (s : square q r idp p) : q =[p] r := eq_pathover (ap_constant p a₀ ⬝ph s ⬝hp (ap_id p)⁻¹) definition eq_pathover_id_left_constant_right {p : a = a'} {a₀ : A} {q : a = a₀} {r : a' = a₀} (s : square q r p idp) : q =[p] r := eq_pathover (ap_id p ⬝ph s ⬝hp (ap_constant p a₀)⁻¹) definition loop_pathover {p : a = a'} {q : a = a} {r : a' = a'} (s : square q r p p) : q =[p] r := eq_pathover (ap_id p ⬝ph s ⬝hp (ap_id p)⁻¹) /- interaction of equivalences with operations on squares -/ definition eq_pathover_equiv_square [constructor] {f g : A → B} (p : a = a') (q : f a = g a) (r : f a' = g a') : q =[p] r ≃ square q r (ap f p) (ap g p) := equiv.MK square_of_pathover eq_pathover begin intro s, induction p, esimp [square_of_pathover,eq_pathover], exact ap vdeg_square (to_right_inv !pathover_idp (eq_of_vdeg_square s)) ⬝ to_left_inv !vdeg_square_equiv s end begin intro s, induction p, esimp [square_of_pathover,eq_pathover], exact ap pathover_idp_of_eq (to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s)) ⬝ to_left_inv !pathover_idp s end definition square_of_pathover_eq_concato {f g : A → B} {p : a = a'} {q q' : f a = g a} {r : f a' = g a'} (s' : q = q') (s : q' =[p] r) : square_of_pathover (s' ⬝po s) = s' ⬝pv square_of_pathover s := by induction s;induction s';reflexivity definition square_of_pathover_concato_eq {f g : A → B} {p : a = a'} {q : f a = g a} {r r' : f a' = g a'} (s' : r = r') (s : q =[p] r) : square_of_pathover (s ⬝op s') = square_of_pathover s ⬝vp s' := by induction s;induction s';reflexivity definition square_of_pathover_concato {f g : A → B} {p : a = a'} {p' : a' = a''} {q : f a = g a} {q' : f a' = g a'} {q'' : f a'' = g a''} (s : q =[p] q') (s' : q' =[p'] q'') : square_of_pathover (s ⬝o s') = ap_con f p p' ⬝ph (square_of_pathover s ⬝v square_of_pathover s') ⬝hp (ap_con g p p')⁻¹ := by induction s';induction s;esimp [ap_con,hconcat_eq];exact !vconcat_vrfl⁻¹ definition eq_of_square_hrfl [unfold 4] (p : a = a') : eq_of_square hrfl = idp_con p := by induction p;reflexivity definition eq_of_square_vrfl [unfold 4] (p : a = a') : eq_of_square vrfl = (idp_con p)⁻¹ := by induction p;reflexivity definition eq_of_square_hdeg_square {p q : a = a'} (r : p = q) : eq_of_square (hdeg_square r) = !idp_con ⬝ r⁻¹ := by induction r;induction p;reflexivity definition eq_of_square_vdeg_square {p q : a = a'} (r : p = q) : eq_of_square (vdeg_square r) = r ⬝ !idp_con⁻¹ := by induction r;induction p;reflexivity definition eq_of_square_eq_vconcat {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : eq_of_square (r ⬝pv s₁₁) = whisker_right p₂₁ r ⬝ eq_of_square s₁₁ := by induction s₁₁;cases r;reflexivity definition eq_of_square_eq_hconcat {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : eq_of_square (r ⬝ph s₁₁) = eq_of_square s₁₁ ⬝ (whisker_right p₁₂ r)⁻¹ := by induction r;reflexivity definition eq_of_square_vconcat_eq {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : eq_of_square (s₁₁ ⬝vp r) = eq_of_square s₁₁ ⬝ whisker_left p₀₁ r := by induction r;reflexivity definition eq_of_square_hconcat_eq {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : eq_of_square (s₁₁ ⬝hp r) = (whisker_left p₁₀ r)⁻¹ ⬝ eq_of_square s₁₁ := by induction s₁₁; induction r;reflexivity definition change_path_eq_pathover {A B : Type} {a a' : A} {f g : A → B} {p p' : a = a'} (r : p = p') {q : f a = g a} {q' : f a' = g a'} (s : square q q' (ap f p) (ap g p)) : change_path r (eq_pathover s) = eq_pathover ((ap02 f r)⁻¹ ⬝ph s ⬝hp (ap02 g r)) := by induction r; reflexivity definition eq_hconcat_hdeg_square {A : Type} {a a' : A} {p₁ p₂ p₃ : a = a'} (q₁ : p₁ = p₂) (q₂ : p₂ = p₃) : q₁ ⬝ph hdeg_square q₂ = hdeg_square (q₁ ⬝ q₂) := by induction q₁; induction q₂; reflexivity definition hdeg_square_hconcat_eq {A : Type} {a a' : A} {p₁ p₂ p₃ : a = a'} (q₁ : p₁ = p₂) (q₂ : p₂ = p₃) : hdeg_square q₁ ⬝hp q₂ = hdeg_square (q₁ ⬝ q₂) := by induction q₁; induction q₂; reflexivity definition eq_hconcat_eq_hdeg_square {A : Type} {a a' : A} {p₁ p₂ p₃ p₄ : a = a'} (q₁ : p₁ = p₂) (q₂ : p₂ = p₃) (q₃ : p₃ = p₄) : q₁ ⬝ph hdeg_square q₂ ⬝hp q₃ = hdeg_square (q₁ ⬝ q₂ ⬝ q₃) := by induction q₃; apply eq_hconcat_hdeg_square -- definition vconcat_eq [unfold 11] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : -- square p₁₀ p p₀₁ p₂₁ := -- by induction r; exact s₁₁ -- definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁) -- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ p₁₂ p p₂₁ := -- by induction r; exact s₁₁ -- definition hconcat_eq [unfold 11] {p : a₂₀ = a₂₂} -- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : square p₁₀ p₁₂ p₀₁ p := -- by induction r; exact s₁₁ -- the following definition is very slow, maybe it's interesting to see why? -- definition eq_pathover_equiv_square' {f g : A → B}(p : a = a') (q : f a = g a) (r : f a' = g a') -- : square q r (ap f p) (ap g p) ≃ q =[p] r := -- equiv.MK eq_pathover -- square_of_pathover -- (λs, begin -- induction p, rewrite [↑[square_of_pathover,eq_pathover], -- to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s), -- to_left_inv !pathover_idp s] -- end) -- (λs, begin -- induction p, rewrite [↑[square_of_pathover,eq_pathover],▸*, -- to_right_inv !(@pathover_idp A) (eq_of_vdeg_square s), -- to_left_inv !vdeg_square_equiv s] -- end) /- recursors for squares where some sides are reflexivity -/ definition rec_on_b [recursor] {a₀₀ : A} {P : Π{a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂}, square t idp l r → Type} {a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂} (s : square t idp l r) (H : P ids) : P s := have H2 : P (square_of_eq (eq_of_square s)), from eq.rec_on (eq_of_square s : t ⬝ r = l) (by induction r; induction t; exact H), left_inv (to_fun !square_equiv_eq) s ▸ H2 definition rec_on_r [recursor] {a₀₀ : A} {P : Π{a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂}, square t b l idp → Type} {a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂} (s : square t b l idp) (H : P ids) : P s := let p : l ⬝ b = t := (eq_of_square s)⁻¹ in have H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹), from @eq.rec_on _ _ (λx p, P (square_of_eq p⁻¹)) _ p (by induction b; induction l; exact H), left_inv (to_fun !square_equiv_eq) s ▸ !inv_inv ▸ H2 definition rec_on_l [recursor] {a₀₁ : A} {P : Π {a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂}, square t b idp r → Type} {a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂} (s : square t b idp r) (H : P ids) : P s := let p : t ⬝ r = b := eq_of_square s ⬝ !idp_con in have H2 : P (square_of_eq (p ⬝ !idp_con⁻¹)), from eq.rec_on p (by induction r; induction t; exact H), left_inv (to_fun !square_equiv_eq) s ▸ !con_inv_cancel_right ▸ H2 definition rec_on_t [recursor] {a₁₀ : A} {P : Π {a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂}, square idp b l r → Type} {a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂} (s : square idp b l r) (H : P ids) : P s := let p : l ⬝ b = r := (eq_of_square s)⁻¹ ⬝ !idp_con in have H2 : P (square_of_eq ((p ⬝ !idp_con⁻¹)⁻¹)), from eq.rec_on p (by induction b; induction l; exact H), have H3 : P (square_of_eq ((eq_of_square s)⁻¹⁻¹)), from eq.rec_on !con_inv_cancel_right H2, have H4 : P (square_of_eq (eq_of_square s)), from eq.rec_on !inv_inv H3, proof left_inv (to_fun !square_equiv_eq) s ▸ H4 qed definition rec_on_tb [recursor] {a : A} {P : Π{b : A} {l : a = b} {r : a = b}, square idp idp l r → Type} {b : A} {l : a = b} {r : a = b} (s : square idp idp l r) (H : P ids) : P s := have H2 : P (square_of_eq (eq_of_square s)), from eq.rec_on (eq_of_square s : idp ⬝ r = l) (by induction r; exact H), left_inv (to_fun !square_equiv_eq) s ▸ H2 definition rec_on_lr [recursor] {a : A} {P : Π{a' : A} {t : a = a'} {b : a = a'}, square t b idp idp → Type} {a' : A} {t : a = a'} {b : a = a'} (s : square t b idp idp) (H : P ids) : P s := let p : idp ⬝ b = t := (eq_of_square s)⁻¹ in have H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹), from @eq.rec_on _ _ (λx q, P (square_of_eq q⁻¹)) _ p (by induction b; exact H), to_left_inv (!square_equiv_eq) s ▸ !inv_inv ▸ H2 --we can also do the other recursors (tl, tr, bl, br, tbl, tbr, tlr, blr), but let's postpone this until they are needed definition whisker_square [unfold 14 15 16 17] (r₁₀ : p₁₀ = p₁₀') (r₁₂ : p₁₂ = p₁₂') (r₀₁ : p₀₁ = p₀₁') (r₂₁ : p₂₁ = p₂₁') (s : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀' p₁₂' p₀₁' p₂₁' := by induction r₁₀; induction r₁₂; induction r₀₁; induction r₂₁; exact s /- squares commute with some operations on 2-paths -/ definition square_inv2 {p₁ p₂ p₃ p₄ : a = a'} {t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄} (s : square t b l r) : square (inverse2 t) (inverse2 b) (inverse2 l) (inverse2 r) := by induction s;constructor definition square_con2 {p₁ p₂ p₃ p₄ : a₁ = a₂} {q₁ q₂ q₃ q₄ : a₂ = a₃} {t₁ : p₁ = p₂} {b₁ : p₃ = p₄} {l₁ : p₁ = p₃} {r₁ : p₂ = p₄} {t₂ : q₁ = q₂} {b₂ : q₃ = q₄} {l₂ : q₁ = q₃} {r₂ : q₂ = q₄} (s₁ : square t₁ b₁ l₁ r₁) (s₂ : square t₂ b₂ l₂ r₂) : square (t₁ ◾ t₂) (b₁ ◾ b₂) (l₁ ◾ l₂) (r₁ ◾ r₂) := by induction s₂;induction s₁;constructor open is_trunc definition is_set.elims [H : is_set A] : square p₁₀ p₁₂ p₀₁ p₂₁ := square_of_eq !is_set.elim definition is_trunc_square [instance] (n : trunc_index) [H : is_trunc n .+2 A] : is_trunc n (square p₁₀ p₁₂ p₀₁ p₂₁) := is_trunc_equiv_closed_rev n !square_equiv_eq -- definition square_of_con_inv_hsquare {p₁ p₂ p₃ p₄ : a₁ = a₂} -- {t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄} -- (s : square (con_inv_eq_idp t) (con_inv_eq_idp b) (l ◾ r⁻²) idp) -- : square t b l r := -- sorry --by induction s /- Square fillers -/ -- TODO replace by "more algebraic" fillers? variables (p₁₀ p₁₂ p₀₁ p₂₁) definition square_fill_t : Σ (p : a₀₀ = a₂₀), square p p₁₂ p₀₁ p₂₁ := by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩ definition square_fill_b : Σ (p : a₀₂ = a₂₂), square p₁₀ p p₀₁ p₂₁ := by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩ definition square_fill_l : Σ (p : a₀₀ = a₀₂), square p₁₀ p₁₂ p p₂₁ := by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩ definition square_fill_r : Σ (p : a₂₀ = a₂₂) , square p₁₀ p₁₂ p₀₁ p := by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩ /- Squares having an 'ap' term on one face -/ --TODO find better names definition square_Flr_ap_idp {c : B} {f : A → B} (p : Π a, f a = c) {a b : A} (q : a = b) : square (p a) (p b) (ap f q) idp := by induction q; apply vrfl definition square_Flr_idp_ap {c : B} {f : A → B} (p : Π a, c = f a) {a b : A} (q : a = b) : square (p a) (p b) idp (ap f q) := by induction q; apply vrfl definition square_ap_idp_Flr {b : B} {f : A → B} (p : Π a, f a = b) {a b : A} (q : a = b) : square (ap f q) idp (p a) (p b) := by induction q; apply hrfl /- Matching eq_hconcat with hconcat etc. -/ -- TODO maybe rename hconcat_eq and the like? variable (s₁₁) definition ph_eq_pv_h_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) : r ⬝ph s₁₁ = !idp_con⁻¹ ⬝pv ((hdeg_square r) ⬝h s₁₁) ⬝vp !idp_con := by cases r; cases s₁₁; esimp definition hdeg_h_eq_pv_ph_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) : hdeg_square r ⬝h s₁₁ = !idp_con ⬝pv (r ⬝ph s₁₁) ⬝vp !idp_con⁻¹ := by cases r; cases s₁₁; esimp definition hp_eq_h {p : a₂₀ = a₂₂} (r : p₂₁ = p) : s₁₁ ⬝hp r = s₁₁ ⬝h hdeg_square r := by cases r; cases s₁₁; esimp definition pv_eq_ph_vdeg_v_vh {p : a₀₀ = a₂₀} (r : p = p₁₀) : r ⬝pv s₁₁ = !idp_con⁻¹ ⬝ph ((vdeg_square r) ⬝v s₁₁) ⬝hp !idp_con := by cases r; cases s₁₁; esimp definition vdeg_v_eq_ph_pv_hp {p : a₀₀ = a₂₀} (r : p = p₁₀) : vdeg_square r ⬝v s₁₁ = !idp_con ⬝ph (r ⬝pv s₁₁) ⬝hp !idp_con⁻¹ := by cases r; cases s₁₁; esimp definition vp_eq_v {p : a₀₂ = a₂₂} (r : p₁₂ = p) : s₁₁ ⬝vp r = s₁₁ ⬝v vdeg_square r := by cases r; cases s₁₁; esimp definition natural_square [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') : square (p a) (p a') (ap f q) (ap g q) := square_of_pathover (apd p q) definition natural_square_tr [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') : square (ap f q) (ap g q) (p a) (p a') := transpose (natural_square p q) definition natural_square011 {A A' : Type} {B : A → Type} {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') {l r : Π⦃a⦄, B a → A'} (g : Π⦃a⦄ (b : B a), l b = r b) : square (apd011 l p q) (apd011 r p q) (g b) (g b') := begin induction q, exact hrfl end definition natural_square0111' {A A' : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} {q : b =[p] b'} {c : C b} {c' : C b'} (s : c =[apd011 C p q] c') {l r : Π⦃a⦄ {b : B a}, C b → A'} (g : Π⦃a⦄ {b : B a} (c : C b), l c = r c) : square (apd0111 l p q s) (apd0111 r p q s) (g c) (g c') := begin induction q, esimp at s, induction s using idp_rec_on, exact hrfl end -- this can be generalized a bit, by making the domain and codomain of k different, and also have -- a function at the RHS of s (similar to m) definition natural_square0111 {A A' : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} {q : b =[p] b'} {c : C b} {c' : C b'} (r : c =[apd011 C p q] c') {k : A → A} {l : Π⦃a⦄, B a → B (k a)} (m : Π⦃a⦄ {b : B a}, C b → C (l b)) {f : Π⦃a⦄ {b : B a}, C b → A'} (s : Π⦃a⦄ {b : B a} (c : C b), f (m c) = f c) : square (apd0111 (λa b c, f (m c)) p q r) (apd0111 f p q r) (s c) (s c') := begin induction q, esimp at r, induction r using idp_rec_on, exact hrfl end /- some higher coherence conditions -/ theorem whisker_bl_whisker_tl_eq (p : a = a') : whisker_bl p (whisker_tl p ids) = con.right_inv p ⬝ph vrfl := by induction p; reflexivity theorem ap_is_constant_natural_square {g : B → C} {f : A → B} (H : Πa, g (f a) = c) (p : a = a') : (ap_is_constant H p)⁻¹ ⬝ph natural_square H p ⬝hp ap_constant p c = whisker_bl (H a') (whisker_tl (H a) ids) := begin induction p, esimp, rewrite inv_inv, rewrite whisker_bl_whisker_tl_eq end definition inv_ph_eq_of_eq_ph {p : a₀₀ = a₀₂} {r : p₀₁ = p} {s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁} {s₁₁' : square p₁₀ p₁₂ p p₂₁} (t : s₁₁ = r ⬝ph s₁₁') : r⁻¹ ⬝ph s₁₁ = s₁₁' := by induction r; exact t -- the following is used for torus.elim_surf theorem whisker_square_aps_eq {f : A → B} {q₁₀ : f a₀₀ = f a₂₀} {q₀₁ : f a₀₀ = f a₀₂} {q₂₁ : f a₂₀ = f a₂₂} {q₁₂ : f a₀₂ = f a₂₂} {r₁₀ : ap f p₁₀ = q₁₀} {r₀₁ : ap f p₀₁ = q₀₁} {r₂₁ : ap f p₂₁ = q₂₁} {r₁₂ : ap f p₁₂ = q₁₂} {s₁₁ : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂} {t₁₁ : square q₁₀ q₁₂ q₀₁ q₂₁} (u : square (ap02 f s₁₁) (eq_of_square t₁₁) (ap_con f p₁₀ p₂₁ ⬝ (r₁₀ ◾ r₂₁)) (ap_con f p₀₁ p₁₂ ⬝ (r₀₁ ◾ r₁₂))) : whisker_square r₁₀ r₁₂ r₀₁ r₂₁ (aps f (square_of_eq s₁₁)) = t₁₁ := begin induction r₁₀, induction r₀₁, induction r₁₂, induction r₂₁, induction p₁₂, induction p₁₀, induction p₂₁, esimp at *, induction s₁₁, esimp at *, esimp [square_of_eq], apply eq_of_fn_eq_fn !square_equiv_eq, esimp, exact (eq_bot_of_square u)⁻¹ end definition natural_square_eq {A B : Type} {a a' : A} {f g : A → B} (p : f ~ g) (q : a = a') : natural_square p q = square_of_pathover (apd p q) := idp definition eq_of_square_hrfl_hconcat_eq {A : Type} {a a' : A} {p p' : a = a'} (q : p = p') : eq_of_square (hrfl ⬝hp q⁻¹) = !idp_con ⬝ q := by induction q; induction p; reflexivity definition aps_vrfl {A B : Type} {a a' : A} (f : A → B) (p : a = a') : aps f (vrefl p) = vrefl (ap f p) := by induction p; reflexivity definition aps_hrfl {A B : Type} {a a' : A} (f : A → B) (p : a = a') : aps f (hrefl p) = hrefl (ap f p) := by induction p; reflexivity -- should the following two equalities be cubes? definition natural_square_ap_fn {A B C : Type} {a a' : A} {g h : A → B} (f : B → C) (p : g ~ h) (q : a = a') : natural_square (λa, ap f (p a)) q = ap_compose f g q ⬝ph (aps f (natural_square p q) ⬝hp (ap_compose f h q)⁻¹) := begin induction q, exact !aps_vrfl⁻¹ end definition natural_square_compose {A B C : Type} {a a' : A} {g g' : B → C} (p : g ~ g') (f : A → B) (q : a = a') : natural_square (λa, p (f a)) q = ap_compose g f q ⬝ph (natural_square p (ap f q) ⬝hp (ap_compose g' f q)⁻¹) := by induction q; reflexivity definition natural_square_eq2 {A B : Type} {a a' : A} {f f' : A → B} (p : f ~ f') {q q' : a = a'} (r : q = q') : natural_square p q = ap02 f r ⬝ph (natural_square p q' ⬝hp (ap02 f' r)⁻¹) := by induction r; reflexivity definition natural_square_refl {A B : Type} {a a' : A} (f : A → B) (q : a = a') : natural_square (homotopy.refl f) q = hrfl := by induction q; reflexivity definition aps_eq_hconcat {p₀₁'} (f : A → B) (q : p₀₁' = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : aps f (q ⬝ph s₁₁) = ap02 f q ⬝ph aps f s₁₁ := by induction q; reflexivity definition aps_hconcat_eq {p₂₁'} (f : A → B) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁' = p₂₁) : aps f (s₁₁ ⬝hp r⁻¹) = aps f s₁₁ ⬝hp (ap02 f r)⁻¹ := by induction r; reflexivity definition aps_hconcat_eq' {p₂₁'} (f : A → B) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p₂₁') : aps f (s₁₁ ⬝hp r) = aps f s₁₁ ⬝hp ap02 f r := by induction r; reflexivity definition aps_square_of_eq (f : A → B) (s : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂) : aps f (square_of_eq s) = square_of_eq ((ap_con f p₁₀ p₂₁)⁻¹ ⬝ ap02 f s ⬝ ap_con f p₀₁ p₁₂) := by induction p₁₂; esimp at *; induction s; induction p₂₁; induction p₁₀; reflexivity definition aps_eq_hconcat_eq {p₀₁' p₂₁'} (f : A → B) (q : p₀₁' = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁' = p₂₁) : aps f (q ⬝ph s₁₁ ⬝hp r⁻¹) = ap02 f q ⬝ph aps f s₁₁ ⬝hp (ap02 f r)⁻¹ := by induction q; induction r; reflexivity end eq
aae8c442f6ec4068999ae49df3963a3a09194bad
7c2dd01406c42053207061adb11703dc7ce0b5e5
/src/exercises/02_iff_if_and.lean
9980421b2d3ba14fbc914712d4ccd89b7e663eab
[ "Apache-2.0" ]
permissive
leanprover-community/tutorials
50ec79564cbf2ad1afd1ac43d8ee3c592c2883a8
79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1
refs/heads/master
1,687,466,144,386
1,672,061,276,000
1,672,061,276,000
189,169,918
186
81
Apache-2.0
1,686,350,300,000
1,559,113,678,000
Lean
UTF-8
Lean
false
false
13,753
lean
import data.real.basic /- In the previous file, we saw how to rewrite using equalities. The analogue operation with mathematical statements is rewriting using equivalences. This is also done using the `rw` tactic. Lean uses ↔ to denote equivalence instead of ⇔. In the following exercises we will use the lemma: sub_nonneg {x y : ℝ} : 0 ≤ y - x ↔ x ≤ y The curly braces around x and y instead of parentheses mean Lean will always try to figure out what x and y are from context, unless we really insist on telling it (we'll see how to insist much later). Let's not worry about that for now. In order to announce an intermediate statement we use: have my_name : my statement, This triggers the apparition of a new goal: proving the statement. After this is done, the statement becomes available under the name `my_name`. We can focus on the current goal by typing tactics between curly braces. -/ example {a b c : ℝ} (hab : a ≤ b) : c + a ≤ c + b := begin rw ← sub_nonneg, have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key { ring, }, -- and prove it between curly braces rw key, -- we can now use the key statement rw sub_nonneg, exact hab, end /- Of course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below. Let's prove a variation (without invoking commutativity of addition since this would spoil our fun). -/ -- 0009 example {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c := begin sorry end /- Let's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`: add_le_add_right {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c This can be read as: "add_le_add_right is a function that will take as input real numbers a and b, an assumption `hab` claiming a ≤ b and a real number c, and will output a proof of a + c ≤ b + c". In addition, recall that curly braces around a b mean Lean will figure out those arguments unless we insist to help. This is because they can be deduced from the next argument `hab`. So it will be sufficient to feed `hab` and c to this function. -/ example {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b := begin calc b = 0 + b : by ring ... ≤ a + b : by exact add_le_add_right ha b, end /- In the second line of the above proof, we need to prove 0 + b ≤ a + b. The proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b. Actually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics to build such a proof term. But since the only tactic used in this block is `exact`, we can skip tactics entirely, and write: -/ example (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b := begin calc b = 0 + b : by ring ... ≤ a + b : add_le_add_right ha b, end /- Let's do a variant. -/ -- 0010 example (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b := begin sorry end /- The two preceding examples are in the core library : le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≤ b) : a ≤ a + b Again, there won't be any need to memorize those names, we will soon see how to get rid of such goals automatically. But we can already try to understand how their names are built: "le_add" describe the conclusion "less or equal than some addition" It comes first because we are focussed on proving stuff, and auto-completion works by looking at the beginning of words. "of" introduces assumptions. "nonneg" is Lean's abbreviation for non-negative. "left" or "right" disambiguates between the two variations. Let's use those lemmas by hand for now. Note that you can have several inequalities steps in a `calc` block, transitivity of inequalities will be used automatically to assemble the pieces. -/ -- 0011 example (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := begin sorry end /- And let's combine with our earlier lemmas. -/ -- 0012 example (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d := begin sorry end /- In the above examples, we prepared proofs of assumptions of our lemmas beforehand, so that we could feed them to the lemmas. This is called forward reasoning. The `calc` proofs also belong to this category. We can also announce the use of a lemma, and provide proofs after the fact, using the `apply` tactic. This is called backward reasoning because we get the conclusion first, and provide proofs later. Using `rw` on the goal (rather than on an assumption from the local context) is also backward reasoning. Let's do that using the lemma mul_nonneg {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : 0 ≤ x*y -/ example (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c := begin rw ← sub_nonneg, have key : b*c - a*c = (b - a)*c, { ring }, rw key, apply mul_nonneg, -- Here we don't provide proofs for the lemma's assumptions -- Now we need to provide the proofs. { rw sub_nonneg, exact hab }, { exact hc }, end /- Let's prove the same statement using only forward reasoning: announcing stuff, proving it by working with known facts, moving forward. -/ example (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c := begin have hab' : 0 ≤ b - a, { rw ← sub_nonneg at hab, exact hab, }, have h₁ : 0 ≤ (b - a)*c, { exact mul_nonneg hab' hc }, have h₂ : (b - a)*c = b*c - a*c, { ring, }, have h₃ : 0 ≤ b*c - a*c, { rw h₂ at h₁, exact h₁, }, rw sub_nonneg at h₃, exact h₃, end /- One reason why the backward reasoning proof is shorter is because Lean can infer of lot of things by comparing the goal and the lemma statement. Indeed in the `apply mul_nonneg` line, we didn't need to tell Lean that x = b - a and y = c in the lemma. It was infered by "unification" between the lemma statement and the goal. To be fair to the forward reasoning version, we should introduce a convenient variation on `rw`. The `rwa` tactic performs rewrite and then looks for an assumption matching the goal. We can use it to rewrite our latest proof as: -/ example (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c := begin have hab' : 0 ≤ b - a, { rwa ← sub_nonneg at hab, }, have h₁ : 0 ≤ (b - a)*c, { exact mul_nonneg hab' hc }, have h₂ : (b - a)*c = b*c - a*c, { ring, }, have h₃ : 0 ≤ b*c - a*c, { rwa h₂ at h₁, }, rwa sub_nonneg at h₃, end /- Let's now combine forward and backward reasoning, to get our most efficient proof of this statement. Note in particular how unification is used to know what to prove inside the parentheses in the `mul_nonneg` arguments. -/ example (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c := begin rw ← sub_nonneg, calc 0 ≤ (b - a)*c : mul_nonneg (by rwa sub_nonneg) hc ... = b*c - a*c : by ring, end /- Let's now practice all three styles using: mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b sub_nonpos {a b : α} : a - b ≤ 0 ↔ a ≤ b -/ /- First using mostly backward reasoning -/ -- 0013 example (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c := begin sorry end /- Using forward reasoning -/ -- 0014 example (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c := begin sorry end /-- Using a combination of both, with a `calc` block -/ -- 0015 example (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c := begin sorry end /- Let's now move to proving implications. Lean denotes implications using a simple arrow →, the same it uses for functions (say denoting the type of functions from ℕ to ℕ by ℕ → ℕ). This is because it sees a proof of P ⇒ Q as a function turning a proof of P into a proof Q. Many of the examples that we already met are implications under the hood. For instance we proved le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b But this can be rephrased as le_add_of_nonneg_left (a b : ℝ) : 0 ≤ a → b ≤ a + b In order to prove P → Q, we use the tactic `intros`, followed by an assumption name. This creates an assumption with that name asserting that P holds, and turns the goal into Q. Let's check we can go from our old version of `le_add_of_nonneg_left` to the new one. -/ example (a b : ℝ): 0 ≤ a → b ≤ a + b := begin intros ha, exact le_add_of_nonneg_left ha, end /- Actually Lean doesn't make any difference between those two versions. It is also happy with -/ example (a b : ℝ): 0 ≤ a → b ≤ a + b := le_add_of_nonneg_left /- No tactic state is shown in the above line because we don't even need to enter tactic mode using `begin` or `by`. Let's practise using `intros`. -/ -- 0016 example (a b : ℝ): 0 ≤ b → a ≤ a + b := begin sorry end /- What about lemmas having more than one assumption? For instance: add_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b A natural idea is to use the conjunction operator (logical AND), which Lean denotes by ∧. Assumptions built using this operator can be decomposed using the `cases` tactic, which is a very general assumption-decomposing tactic. -/ example {a b : ℝ} : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b := begin intros hyp, cases hyp with ha hb, exact add_nonneg ha hb, end /- Needing that intermediate line invoking `cases` shows this formulation is not what is used by Lean. It rather sees `add_nonneg` as two nested implications: if a is non-negative then if b is non-negative then a+b is non-negative. It reads funny, but it is much more convenient to use in practice. -/ example {a b : ℝ} : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) := add_nonneg /- The above pattern is so common that implications are defined as right-associative operators, hence parentheses are not needed above. Let's prove that the naive conjunction version implies the funny Lean version. For this we need to know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal. It can also be used to create two implication goals from an equivalence goal. -/ example {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) := begin intros ha, intros hb, apply H, split, exact ha, exact hb, end /- Let's practice `cases` and `split`. In the next exercise, P, Q and R denote unspecified mathematical statements. -/ -- 0017 example (P Q R : Prop) : P ∧ Q → Q ∧ P := begin sorry end /- Of course using `split` only to be able to use `exact` twice in a row feels silly. One can also use the anonymous constructor syntax: ⟨ ⟩ Beware those are not parentheses but angle brackets. This is a generic way of providing compound objects to Lean when Lean already has a very clear idea of what it is waiting for. So we could have replaced the last three lines by: exact ⟨hQ, hP⟩ We can also combine the `intros` steps. We can now compress our earlier proof to: -/ example {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) := begin intros ha hb, exact H ⟨ha, hb⟩, end /- The anonymous contructor trick actually also works in `intros` provided we use its recursive version `rintros`. So we can replace intro h, cases h with h₁ h₂ by rintros ⟨h₁, h₂⟩, Now redo the previous exercise using all those compressing techniques, in exactly two lines. -/ -- 0018 example (P Q R : Prop): P ∧ Q → Q ∧ P := begin sorry end /- We are ready to come back to the equivalence between the different formulations of lemmas having two assumptions. Remember the `split` tactic can be used to split an equivalence into two implications. -/ -- 0019 example (P Q R : Prop) : (P ∧ Q → R) ↔ (P → (Q → R)) := begin sorry end /- If you used more than five lines in the above exercise then try to compress things (without simply removing line ends). One last compression technique: given a proof h of a conjunction P ∧ Q, one can get a proof of P using h.left and a proof of Q using h.right, without using cases. One can also use the more generic (but less legible) names h.1 and h.2. Similarly, given a proof h of P ↔ Q, one can get a proof of P → Q using h.mp and a proof of Q → P using h.mpr (or the generic h.1 and h.2 that are even less legible in this case). Before the final exercise in this file, let's make sure we'll be able to leave without learning 10 lemma names. The `linarith` tactic will prove any equality or inequality or contradiction that follows by linear combinations of assumptions from the context (with constant coefficients). -/ example (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b := begin linarith, end /- Now let's enjoy this for a while. -/ -- 0020 example (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := begin sorry end /- And let's combine with our earlier lemmas. -/ -- 0021 example (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d := begin sorry end /- Final exercise In the last exercise of this file, we will use the divisibility relation on ℕ, denoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character), and the gcd function. The definitions are the usual ones, but our goal is to avoid using these definitions and only use the following three lemmas: dvd_refl (a : ℕ) : a ∣ a dvd_antisymm {a b : ℕ} : a ∣ b → b ∣ a → a = b := dvd_gcd_iff {a b c : ℕ} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b -/ -- All functions and lemmas below are about natural numbers. open nat -- 0022 example (a b : ℕ) : a ∣ b ↔ gcd a b = a := begin sorry end
3a392b2ff04e37cbf9070b870c65a6bee0ddde36
0845ae2ca02071debcfd4ac24be871236c01784f
/tests/compiler/uint_fold.lean
4c37e96c46bf5ad53dc81bd7281ff0335cddfe13
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
570
lean
@[noinline] def h (x : Nat) : UInt32 := UInt32.ofNat x def f (x y : UInt32) : UInt32 := let a1 : UInt32 := 128 * 100 - 100 in let v : Nat := 10 + x.toNat in let a2 : UInt32 := x + a1 in let a3 : UInt32 := 10 in y + a2 + h v + a3 partial def g : UInt32 → UInt32 → UInt32 | x y := if x = 0 then y else g (x-1) (y+2) def foo : UInt8 := let x : UInt8 := 100 in x + x + x def main : IO UInt32 := IO.println (toString (f 10 20)) *> IO.println (toString (f 0 0)) *> IO.println (toString (g 3 5)) *> IO.println (toString (g 0 6)) *> IO.println (toString foo) *> pure 0
340314872d6ce695f582d40c933ab33bfe33af07
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/perm/cycle/concrete.lean
8d4f2ac2b3d63c3677efe89f37953df600c6209e
[ "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
19,924
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.list.cycle import group_theory.perm.cycle.type import group_theory.perm.list /-! # Properties of cyclic permutations constructed from lists/cycles > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In the following, `{α : Type*} [fintype α] [decidable_eq α]`. ## Main definitions * `cycle.form_perm`: the cyclic permutation created by looping over a `cycle α` * `equiv.perm.to_list`: the list formed by iterating application of a permutation * `equiv.perm.to_cycle`: the cycle formed by iterating application of a permutation * `equiv.perm.iso_cycle`: the equivalence between cyclic permutations `f : perm α` and the terms of `cycle α` that correspond to them * `equiv.perm.iso_cycle'`: the same equivalence as `equiv.perm.iso_cycle` but with evaluation via choosing over fintypes * The notation `c[1, 2, 3]` to emulate notation of cyclic permutations `(1 2 3)` * A `has_repr` instance for any `perm α`, by representing the `finset` of `cycle α` that correspond to the cycle factors. ## Main results * `list.is_cycle_form_perm`: a nontrivial list without duplicates, when interpreted as a permutation, is cyclic * `equiv.perm.is_cycle.exists_unique_cycle`: there is only one nontrivial `cycle α` corresponding to each cyclic `f : perm α` ## Implementation details The forward direction of `equiv.perm.iso_cycle'` uses `fintype.choose` of the uniqueness result, relying on the `fintype` instance of a `cycle.nodup` subtype. It is unclear if this works faster than the `equiv.perm.to_cycle`, which relies on recursion over `finset.univ`. Running `#eval` on even a simple noncyclic permutation `c[(1 : fin 7), 2, 3] * c[0, 5]` to show it takes a long time. TODO: is this because computing the cycle factors is slow? -/ open equiv equiv.perm list variables {α : Type*} namespace list variables [decidable_eq α] {l l' : list α} lemma form_perm_disjoint_iff (hl : nodup l) (hl' : nodup l') (hn : 2 ≤ l.length) (hn' : 2 ≤ l'.length) : perm.disjoint (form_perm l) (form_perm l') ↔ l.disjoint l' := begin rw [disjoint_iff_eq_or_eq, list.disjoint], split, { rintro h x hx hx', specialize h x, rw [form_perm_apply_mem_eq_self_iff _ hl _ hx, form_perm_apply_mem_eq_self_iff _ hl' _ hx'] at h, rcases h with hl | hl'; linarith }, { intros h x, by_cases hx : x ∈ l, by_cases hx' : x ∈ l', { exact (h hx hx').elim }, all_goals { have := form_perm_eq_self_of_not_mem _ _ ‹_›, tauto } } end lemma is_cycle_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : is_cycle (form_perm l) := begin cases l with x l, { norm_num at hn }, induction l with y l IH generalizing x, { norm_num at hn }, { use x, split, { rwa form_perm_apply_mem_ne_self_iff _ hl _ (mem_cons_self _ _) }, { intros w hw, have : w ∈ (x :: y :: l) := mem_of_form_perm_ne_self _ _ hw, obtain ⟨k, hk, rfl⟩ := nth_le_of_mem this, use k, simp only [zpow_coe_nat, form_perm_pow_apply_head _ _ hl k, nat.mod_eq_of_lt hk] } } end lemma pairwise_same_cycle_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : pairwise (l.form_perm.same_cycle) l := pairwise.imp_mem.mpr (pairwise_of_forall (λ x y hx hy, (is_cycle_form_perm hl hn).same_cycle ((form_perm_apply_mem_ne_self_iff _ hl _ hx).mpr hn) ((form_perm_apply_mem_ne_self_iff _ hl _ hy).mpr hn))) lemma cycle_of_form_perm (hl : nodup l) (hn : 2 ≤ l.length) (x) : cycle_of l.attach.form_perm x = l.attach.form_perm := have hn : 2 ≤ l.attach.length := by rwa ← length_attach at hn, have hl : l.attach.nodup := by rwa ← nodup_attach at hl, (is_cycle_form_perm hl hn).cycle_of_eq ((form_perm_apply_mem_ne_self_iff _ hl _ (mem_attach _ _)).mpr hn) lemma cycle_type_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : cycle_type l.attach.form_perm = {l.length} := begin rw ←length_attach at hn, rw ←nodup_attach at hl, rw cycle_type_eq [l.attach.form_perm], { simp only [map, function.comp_app], rw [support_form_perm_of_nodup _ hl, card_to_finset, dedup_eq_self.mpr hl], { simp }, { intros x h, simpa [h, nat.succ_le_succ_iff] using hn } }, { simp }, { simpa using is_cycle_form_perm hl hn }, { simp } end lemma form_perm_apply_mem_eq_next (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x = next l x hx := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [next_nth_le _ hl, form_perm_apply_nth_le _ hl] end end list namespace cycle variables [decidable_eq α] (s s' : cycle α) /-- A cycle `s : cycle α` , given `nodup s` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. -/ def form_perm : Π (s : cycle α) (h : nodup s), equiv.perm α := λ s, quot.hrec_on s (λ l h, form_perm l) (λ l₁ l₂ (h : l₁ ~r l₂), begin ext, { exact h.nodup_iff }, { intros h₁ h₂ _, exact heq_of_eq (form_perm_eq_of_is_rotated h₁ h) } end) @[simp] lemma form_perm_coe (l : list α) (hl : l.nodup) : form_perm (l : cycle α) hl = l.form_perm := rfl lemma form_perm_subsingleton (s : cycle α) (h : subsingleton s) : form_perm s h.nodup = 1 := begin induction s using quot.induction_on, simp only [form_perm_coe, mk_eq_coe], simp only [length_subsingleton_iff, length_coe, mk_eq_coe] at h, cases s with hd tl, { simp }, { simp only [length_eq_zero, add_le_iff_nonpos_left, list.length, nonpos_iff_eq_zero] at h, simp [h] } end lemma is_cycle_form_perm (s : cycle α) (h : nodup s) (hn : nontrivial s) : is_cycle (form_perm s h) := begin induction s using quot.induction_on, exact list.is_cycle_form_perm h (length_nontrivial hn) end lemma support_form_perm [fintype α] (s : cycle α) (h : nodup s) (hn : nontrivial s) : support (form_perm s h) = s.to_finset := begin induction s using quot.induction_on, refine support_form_perm_of_nodup s h _, rintro _ rfl, simpa [nat.succ_le_succ_iff] using length_nontrivial hn end lemma form_perm_eq_self_of_not_mem (s : cycle α) (h : nodup s) (x : α) (hx : x ∉ s) : form_perm s h x = x := begin induction s using quot.induction_on, simpa using list.form_perm_eq_self_of_not_mem _ _ hx end lemma form_perm_apply_mem_eq_next (s : cycle α) (h : nodup s) (x : α) (hx : x ∈ s) : form_perm s h x = next s h x hx := begin induction s using quot.induction_on, simpa using list.form_perm_apply_mem_eq_next h _ _ end lemma form_perm_reverse (s : cycle α) (h : nodup s) : form_perm s.reverse (nodup_reverse_iff.mpr h) = (form_perm s h)⁻¹ := begin induction s using quot.induction_on, simpa using form_perm_reverse _ h end lemma form_perm_eq_form_perm_iff {α : Type*} [decidable_eq α] {s s' : cycle α} {hs : s.nodup} {hs' : s'.nodup} : s.form_perm hs = s'.form_perm hs' ↔ s = s' ∨ s.subsingleton ∧ s'.subsingleton := begin rw [cycle.length_subsingleton_iff, cycle.length_subsingleton_iff], revert s s', intros s s', apply quotient.induction_on₂' s s', intros l l', simpa using form_perm_eq_form_perm_iff end end cycle namespace equiv.perm section fintype variables [fintype α] [decidable_eq α] (p : equiv.perm α) (x : α) /-- `equiv.perm.to_list (f : perm α) (x : α)` generates the list `[x, f x, f (f x), ...]` until looping. That means when `f x = x`, `to_list f x = []`. -/ def to_list : list α := (list.range (cycle_of p x).support.card).map (λ k, (p ^ k) x) @[simp] lemma to_list_one : to_list (1 : perm α) x = [] := by simp [to_list, cycle_of_one] @[simp] lemma to_list_eq_nil_iff {p : perm α} {x} : to_list p x = [] ↔ x ∉ p.support := by simp [to_list] @[simp] lemma length_to_list : length (to_list p x) = (cycle_of p x).support.card := by simp [to_list] lemma to_list_ne_singleton (y : α) : to_list p x ≠ [y] := begin intro H, simpa [card_support_ne_one] using congr_arg length H end lemma two_le_length_to_list_iff_mem_support {p : perm α} {x : α} : 2 ≤ length (to_list p x) ↔ x ∈ p.support := by simp lemma length_to_list_pos_of_mem_support (h : x ∈ p.support) : 0 < length (to_list p x) := zero_lt_two.trans_le (two_le_length_to_list_iff_mem_support.mpr h) lemma nth_le_to_list (n : ℕ) (hn : n < length (to_list p x)) : nth_le (to_list p x) n hn = (p ^ n) x := by simp [to_list] lemma to_list_nth_le_zero (h : x ∈ p.support) : (to_list p x).nth_le 0 (length_to_list_pos_of_mem_support _ _ h) = x := by simp [to_list] variables {p} {x} lemma mem_to_list_iff {y : α} : y ∈ to_list p x ↔ same_cycle p x y ∧ x ∈ p.support := begin simp only [to_list, mem_range, mem_map], split, { rintro ⟨n, hx, rfl⟩, refine ⟨⟨n, rfl⟩, _⟩, contrapose! hx, rw ←support_cycle_of_eq_nil_iff at hx, simp [hx] }, { rintro ⟨h, hx⟩, simpa using h.exists_pow_eq_of_mem_support hx } end lemma nodup_to_list (p : perm α) (x : α) : nodup (to_list p x) := begin by_cases hx : p x = x, { rw [←not_mem_support, ←to_list_eq_nil_iff] at hx, simp [hx] }, have hc : is_cycle (cycle_of p x) := is_cycle_cycle_of p hx, rw nodup_iff_nth_le_inj, rintros n m hn hm, rw [length_to_list, ←hc.order_of] at hm hn, rw [←cycle_of_apply_self, ←ne.def, ←mem_support] at hx, rw [nth_le_to_list, nth_le_to_list, ←cycle_of_pow_apply_self p x n, ←cycle_of_pow_apply_self p x m], cases n; cases m, { simp }, { rw [←hc.support_pow_of_pos_of_lt_order_of m.zero_lt_succ hm, mem_support, cycle_of_pow_apply_self] at hx, simp [hx.symm] }, { rw [←hc.support_pow_of_pos_of_lt_order_of n.zero_lt_succ hn, mem_support, cycle_of_pow_apply_self] at hx, simp [hx] }, intro h, have hn' : ¬ order_of (p.cycle_of x) ∣ n.succ := nat.not_dvd_of_pos_of_lt n.zero_lt_succ hn, have hm' : ¬ order_of (p.cycle_of x) ∣ m.succ := nat.not_dvd_of_pos_of_lt m.zero_lt_succ hm, rw ←hc.support_pow_eq_iff at hn' hm', rw [←nat.mod_eq_of_lt hn, ←nat.mod_eq_of_lt hm, ←pow_inj_mod], refine support_congr _ _, { rw [hm', hn'], exact finset.subset.refl _ }, { rw hm', intros y hy, obtain ⟨k, rfl⟩ := hc.exists_pow_eq (mem_support.mp hx) (mem_support.mp hy), rw [←mul_apply, (commute.pow_pow_self _ _ _).eq, mul_apply, h, ←mul_apply, ←mul_apply, (commute.pow_pow_self _ _ _).eq] } end lemma next_to_list_eq_apply (p : perm α) (x y : α) (hy : y ∈ to_list p x) : next (to_list p x) y hy = p y := begin rw mem_to_list_iff at hy, obtain ⟨k, hk, hk'⟩ := hy.left.exists_pow_eq_of_mem_support hy.right, rw ←nth_le_to_list p x k (by simpa using hk) at hk', simp_rw ←hk', rw [next_nth_le _ (nodup_to_list _ _), nth_le_to_list, nth_le_to_list, ←mul_apply, ←pow_succ, length_to_list, pow_apply_eq_pow_mod_order_of_cycle_of_apply p (k + 1), is_cycle.order_of], exact is_cycle_cycle_of _ (mem_support.mp hy.right) end lemma to_list_pow_apply_eq_rotate (p : perm α) (x : α) (k : ℕ) : p.to_list ((p ^ k) x) = (p.to_list x).rotate k := begin apply ext_le, { simp only [length_to_list, cycle_of_self_apply_pow, length_rotate]}, { intros n hn hn', rw [nth_le_to_list, nth_le_rotate, nth_le_to_list, length_to_list, pow_mod_card_support_cycle_of_self_apply, pow_add, mul_apply] } end lemma same_cycle.to_list_is_rotated {f : perm α} {x y : α} (h : same_cycle f x y) : to_list f x ~r to_list f y := begin by_cases hx : x ∈ f.support, { obtain ⟨_ | k, hk, hy⟩ := h.exists_pow_eq_of_mem_support hx, { simp only [coe_one, id.def, pow_zero] at hy, simp [hy] }, use k.succ, rw [←to_list_pow_apply_eq_rotate, hy] }, { rw [to_list_eq_nil_iff.mpr hx, is_rotated_nil_iff', eq_comm, to_list_eq_nil_iff], rwa ←h.mem_support_iff } end lemma pow_apply_mem_to_list_iff_mem_support {n : ℕ} : (p ^ n) x ∈ p.to_list x ↔ x ∈ p.support := begin rw [mem_to_list_iff, and_iff_right_iff_imp], refine λ _, same_cycle.symm _, rw same_cycle_pow_left end lemma to_list_form_perm_nil (x : α) : to_list (form_perm ([] : list α)) x = [] := by simp lemma to_list_form_perm_singleton (x y : α) : to_list (form_perm [x]) y = [] := by simp lemma to_list_form_perm_nontrivial (l : list α) (hl : 2 ≤ l.length) (hn : nodup l) : to_list (form_perm l) (l.nth_le 0 (zero_lt_two.trans_le hl)) = l := begin have hc : l.form_perm.is_cycle := list.is_cycle_form_perm hn hl, have hs : l.form_perm.support = l.to_finset, { refine support_form_perm_of_nodup _ hn _, rintro _ rfl, simpa [nat.succ_le_succ_iff] using hl }, rw [to_list, hc.cycle_of_eq (mem_support.mp _), hs, card_to_finset, dedup_eq_self.mpr hn], { refine list.ext_le (by simp) (λ k hk hk', _), simp [form_perm_pow_apply_nth_le _ hn, nat.mod_eq_of_lt hk'] }, { simpa [hs] using nth_le_mem _ _ _ } end lemma to_list_form_perm_is_rotated_self (l : list α) (hl : 2 ≤ l.length) (hn : nodup l) (x : α) (hx : x ∈ l): to_list (form_perm l) x ~r l := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, have hr : l ~r l.rotate k := ⟨k, rfl⟩, rw form_perm_eq_of_is_rotated hn hr, rw ←nth_le_rotate' l k k, simp only [nat.mod_eq_of_lt hk, tsub_add_cancel_of_le hk.le, nat.mod_self], rw [to_list_form_perm_nontrivial], { simp }, { simpa using hl }, { simpa using hn } end lemma form_perm_to_list (f : perm α) (x : α) : form_perm (to_list f x) = f.cycle_of x := begin by_cases hx : f x = x, { rw [(cycle_of_eq_one_iff f).mpr hx, to_list_eq_nil_iff.mpr (not_mem_support.mpr hx), form_perm_nil] }, ext y, by_cases hy : same_cycle f x y, { obtain ⟨k, hk, rfl⟩ := hy.exists_pow_eq_of_mem_support (mem_support.mpr hx), rw [cycle_of_apply_apply_pow_self, list.form_perm_apply_mem_eq_next (nodup_to_list f x), next_to_list_eq_apply, pow_succ, mul_apply], rw mem_to_list_iff, exact ⟨⟨k, rfl⟩, mem_support.mpr hx⟩ }, { rw [cycle_of_apply_of_not_same_cycle hy, form_perm_apply_of_not_mem], simp [mem_to_list_iff, hy] } end /-- Given a cyclic `f : perm α`, generate the `cycle α` in the order of application of `f`. Implemented by finding an element `x : α` in the support of `f` in `finset.univ`, and iterating on using `equiv.perm.to_list f x`. -/ def to_cycle (f : perm α) (hf : is_cycle f) : cycle α := multiset.rec_on (finset.univ : finset α).val (quot.mk _ []) (λ x s l, if f x = x then l else to_list f x) (by { intros x y m s, refine heq_of_eq _, split_ifs with hx hy hy; try { refl }, { have hc : same_cycle f x y := is_cycle.same_cycle hf hx hy, exact quotient.sound' hc.to_list_is_rotated }}) lemma to_cycle_eq_to_list (f : perm α) (hf : is_cycle f) (x : α) (hx : f x ≠ x) : to_cycle f hf = to_list f x := begin have key : (finset.univ : finset α).val = x ::ₘ finset.univ.val.erase x, { simp }, rw [to_cycle, key], simp [hx] end lemma nodup_to_cycle (f : perm α) (hf : is_cycle f) : (to_cycle f hf).nodup := begin obtain ⟨x, hx, -⟩ := id hf, simpa [to_cycle_eq_to_list f hf x hx] using nodup_to_list _ _ end lemma nontrivial_to_cycle (f : perm α) (hf : is_cycle f) : (to_cycle f hf).nontrivial := begin obtain ⟨x, hx, -⟩ := id hf, simp [to_cycle_eq_to_list f hf x hx, hx, cycle.nontrivial_coe_nodup_iff (nodup_to_list _ _)] end /-- Any cyclic `f : perm α` is isomorphic to the nontrivial `cycle α` that corresponds to repeated application of `f`. The forward direction is implemented by `equiv.perm.to_cycle`. -/ def iso_cycle : {f : perm α // is_cycle f} ≃ {s : cycle α // s.nodup ∧ s.nontrivial} := { to_fun := λ f, ⟨to_cycle (f : perm α) f.prop, nodup_to_cycle f f.prop, nontrivial_to_cycle _ f.prop⟩, inv_fun := λ s, ⟨(s : cycle α).form_perm s.prop.left, (s : cycle α).is_cycle_form_perm _ s.prop.right⟩, left_inv := λ f, by { obtain ⟨x, hx, -⟩ := id f.prop, simpa [to_cycle_eq_to_list (f : perm α) f.prop x hx, form_perm_to_list, subtype.ext_iff] using f.prop.cycle_of_eq hx }, right_inv := λ s, by { rcases s with ⟨⟨s⟩, hn, ht⟩, obtain ⟨x, -, -, hx, -⟩ := id ht, have hl : 2 ≤ s.length := by simpa using cycle.length_nontrivial ht, simp only [cycle.mk_eq_coe, cycle.nodup_coe_iff, cycle.mem_coe_iff, subtype.coe_mk, cycle.form_perm_coe] at hn hx ⊢, rw to_cycle_eq_to_list _ _ x, { refine quotient.sound' _, exact to_list_form_perm_is_rotated_self _ hl hn _ hx }, { rw [←mem_support, support_form_perm_of_nodup _ hn], { simpa using hx }, { rintro _ rfl, simpa [nat.succ_le_succ_iff] using hl } } } } end fintype section finite variables [finite α] [decidable_eq α] lemma is_cycle.exists_unique_cycle {f : perm α} (hf : is_cycle f) : ∃! (s : cycle α), ∃ (h : s.nodup), s.form_perm h = f := begin casesI nonempty_fintype α, obtain ⟨x, hx, hy⟩ := id hf, refine ⟨f.to_list x, ⟨nodup_to_list f x, _⟩, _⟩, { simp [form_perm_to_list, hf.cycle_of_eq hx] }, { rintro ⟨l⟩ ⟨hn, rfl⟩, simp only [cycle.mk_eq_coe, cycle.coe_eq_coe, subtype.coe_mk, cycle.form_perm_coe], refine (to_list_form_perm_is_rotated_self _ _ hn _ _).symm, { contrapose! hx, suffices : form_perm l = 1, { simp [this] }, rw form_perm_eq_one_iff _ hn, exact nat.le_of_lt_succ hx }, { rw ←mem_to_finset, refine support_form_perm_le l _, simpa using hx } } end lemma is_cycle.exists_unique_cycle_subtype {f : perm α} (hf : is_cycle f) : ∃! (s : {s : cycle α // s.nodup}), (s : cycle α).form_perm s.prop = f := begin obtain ⟨s, ⟨hs, rfl⟩, hs'⟩ := hf.exists_unique_cycle, refine ⟨⟨s, hs⟩, rfl, _⟩, rintro ⟨t, ht⟩ ht', simpa using hs' _ ⟨ht, ht'⟩ end lemma is_cycle.exists_unique_cycle_nontrivial_subtype {f : perm α} (hf : is_cycle f) : ∃! (s : {s : cycle α // s.nodup ∧ s.nontrivial}), (s : cycle α).form_perm s.prop.left = f := begin obtain ⟨⟨s, hn⟩, hs, hs'⟩ := hf.exists_unique_cycle_subtype, refine ⟨⟨s, hn, _⟩, _, _⟩, { rw hn.nontrivial_iff, subst f, intro H, refine hf.ne_one _, simpa using cycle.form_perm_subsingleton _ H }, { simpa using hs }, { rintro ⟨t, ht, ht'⟩ ht'', simpa using hs' ⟨t, ht⟩ ht'' } end end finite variables [fintype α] [decidable_eq α] /-- Any cyclic `f : perm α` is isomorphic to the nontrivial `cycle α` that corresponds to repeated application of `f`. The forward direction is implemented by finding this `cycle α` using `fintype.choose`. -/ def iso_cycle' : {f : perm α // is_cycle f} ≃ {s : cycle α // s.nodup ∧ s.nontrivial} := { to_fun := λ f, fintype.choose _ f.prop.exists_unique_cycle_nontrivial_subtype, inv_fun := λ s, ⟨(s : cycle α).form_perm s.prop.left, (s : cycle α).is_cycle_form_perm _ s.prop.right⟩, left_inv := λ f, by simpa [subtype.ext_iff] using fintype.choose_spec _ f.prop.exists_unique_cycle_nontrivial_subtype, right_inv := λ ⟨s, hs, ht⟩, by { simp [subtype.coe_mk], convert fintype.choose_subtype_eq (λ (s' : cycle α), s'.nodup ∧ s'.nontrivial) _, ext ⟨s', hs', ht'⟩, simp [cycle.form_perm_eq_form_perm_iff, (iff_not_comm.mp hs.nontrivial_iff), (iff_not_comm.mp hs'.nontrivial_iff), ht] } } notation `c[` l:(foldr `, ` (h t, list.cons h t) list.nil `]`) := cycle.form_perm ↑l (cycle.nodup_coe_iff.mpr dec_trivial) meta instance repr_perm [has_repr α] : has_repr (perm α) := ⟨λ f, repr (multiset.pmap (λ (g : perm α) (hg : g.is_cycle), iso_cycle ⟨g, hg⟩) -- to_cycle is faster? (perm.cycle_factors_finset f).val (λ g hg, (mem_cycle_factors_finset_iff.mp (finset.mem_def.mpr hg)).left))⟩ end equiv.perm
3c34a9241f23cb681a284d92513b5b4beac38903
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/geometry/manifold/instances/real.lean
c66d1c5d4b1376119d48e9d5117fb3acbe57792e
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
13,470
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import geometry.manifold.algebra.smooth_functions import linear_algebra.finite_dimensional import analysis.normed_space.pi_Lp /-! # Constructing examples of manifolds over ℝ We introduce the necessary bits to be able to define manifolds modelled over `ℝ^n`, boundaryless or with boundary or with corners. As a concrete example, we construct explicitly the manifold with boundary structure on the real interval `[x, y]`. More specifically, we introduce * `model_with_corners ℝ (euclidean_space ℝ (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary * `model_with_corners ℝ (euclidean_space ℝ (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners ## Notations In the locale `manifold`, we introduce the notations * `𝓡 n` for the identity model with corners on `euclidean_space ℝ (fin n)` * `𝓡∂ n` for `model_with_corners ℝ (euclidean_space ℝ (fin n)) (euclidean_half_space n)`. For instance, if a manifold `M` is boundaryless, smooth and modelled on `euclidean_space ℝ (fin m)`, and `N` is smooth with boundary modelled on `euclidean_half_space n`, and `f : M → N` is a smooth map, then the derivative of `f` can be written simply as `mfderiv (𝓡 m) (𝓡∂ n) f` (as to why the model with corners can not be implicit, see the discussion in `smooth_manifold_with_corners.lean`). ## Implementation notes The manifold structure on the interval `[x, y] = Icc x y` requires the assumption `x < y` as a typeclass. We provide it as `[fact (x < y)]`. -/ noncomputable theory open set function open_locale manifold /-- The half-space in `ℝ^n`, used to model manifolds with boundary. We only define it when `1 ≤ n`, as the definition only makes sense in this case. -/ def euclidean_half_space (n : ℕ) [has_zero (fin n)] : Type := {x : euclidean_space ℝ (fin n) // 0 ≤ x 0} /-- The quadrant in `ℝ^n`, used to model manifolds with corners, made of all vectors with nonnegative coordinates. -/ def euclidean_quadrant (n : ℕ) : Type := {x : euclidean_space ℝ (fin n) // ∀i:fin n, 0 ≤ x i} section /- Register class instances for euclidean half-space and quadrant, that can not be noticed without the following reducibility attribute (which is only set in this section). -/ local attribute [reducible] euclidean_half_space euclidean_quadrant variable {n : ℕ} instance [has_zero (fin n)] : topological_space (euclidean_half_space n) := by apply_instance instance : topological_space (euclidean_quadrant n) := by apply_instance instance [has_zero (fin n)] : inhabited (euclidean_half_space n) := ⟨⟨0, le_refl _⟩⟩ instance : inhabited (euclidean_quadrant n) := ⟨⟨0, λ i, le_refl _⟩⟩ lemma range_half_space (n : ℕ) [has_zero (fin n)] : range (λx : euclidean_half_space n, x.val) = {y | 0 ≤ y 0} := by simp lemma range_quadrant (n : ℕ) : range (λx : euclidean_quadrant n, x.val) = {y | ∀i:fin n, 0 ≤ y i} := by simp end /-- Definition of the model with corners `(euclidean_space ℝ (fin n), euclidean_half_space n)`, used as a model for manifolds with boundary. In the locale `manifold`, use the shortcut `𝓡∂ n`. -/ def model_with_corners_euclidean_half_space (n : ℕ) [has_zero (fin n)] : model_with_corners ℝ (euclidean_space ℝ (fin n)) (euclidean_half_space n) := { to_fun := subtype.val, inv_fun := λx, ⟨update x 0 (max (x 0) 0), by simp [le_refl]⟩, source := univ, target := {x | 0 ≤ x 0}, map_source' := λx hx, x.property, map_target' := λx hx, mem_univ _, left_inv' := λ ⟨xval, xprop⟩ hx, begin rw [subtype.mk_eq_mk, update_eq_iff], exact ⟨max_eq_left xprop, λ i _, rfl⟩ end, right_inv' := λx hx, update_eq_iff.2 ⟨max_eq_left hx, λ i _, rfl⟩, source_eq := rfl, unique_diff' := by simpa only [singleton_pi] using unique_diff_on.pi (fin n) (λ _, ℝ) _ _ (λ i ∈ ({0} : set (fin n)), unique_diff_on_Ici 0), continuous_to_fun := continuous_subtype_val, continuous_inv_fun := continuous_subtype_mk _ $ continuous_id.update 0 $ (continuous_apply 0).max continuous_const } /-- Definition of the model with corners `(euclidean_space ℝ (fin n), euclidean_quadrant n)`, used as a model for manifolds with corners -/ def model_with_corners_euclidean_quadrant (n : ℕ) : model_with_corners ℝ (euclidean_space ℝ (fin n)) (euclidean_quadrant n) := { to_fun := subtype.val, inv_fun := λx, ⟨λi, max (x i) 0, λi, by simp only [le_refl, or_true, le_max_iff]⟩, source := univ, target := {x | ∀ i, 0 ≤ x i}, map_source' := λx hx, by simpa only [subtype.range_val] using x.property, map_target' := λx hx, mem_univ _, left_inv' := λ ⟨xval, xprop⟩ hx, by { ext i, simp only [subtype.coe_mk, xprop i, max_eq_left] }, right_inv' := λ x hx, by { ext1 i, simp only [hx i, max_eq_left] }, source_eq := rfl, unique_diff' := by simpa only [pi_univ_Ici] using unique_diff_on.univ_pi (fin n) (λ _, ℝ) _ (λ i, unique_diff_on_Ici 0), continuous_to_fun := continuous_subtype_val, continuous_inv_fun := continuous_subtype_mk _ $ continuous_pi $ λ i, (continuous_id.max continuous_const).comp (continuous_apply i) } localized "notation `𝓡 `n := model_with_corners_self ℝ (euclidean_space ℝ (fin n))" in manifold localized "notation `𝓡∂ `n := model_with_corners_euclidean_half_space n" in manifold /-- The left chart for the topological space `[x, y]`, defined on `[x,y)` and sending `x` to `0` in `euclidean_half_space 1`. -/ def Icc_left_chart (x y : ℝ) [fact (x < y)] : local_homeomorph (Icc x y) (euclidean_half_space 1) := { source := {z : Icc x y | z.val < y}, target := {z : euclidean_half_space 1 | z.val 0 < y - x}, to_fun := λ(z : Icc x y), ⟨λi, z.val - x, sub_nonneg.mpr z.property.1⟩, inv_fun := λz, ⟨min (z.val 0 + x) y, by simp [le_refl, z.prop, le_of_lt (fact.out (x < y))]⟩, map_source' := by simp only [imp_self, sub_lt_sub_iff_right, mem_set_of_eq, forall_true_iff], map_target' := by { simp only [min_lt_iff, mem_set_of_eq], assume z hz, left, dsimp [-subtype.val_eq_coe] at hz, linarith }, left_inv' := begin rintros ⟨z, hz⟩ h'z, simp only [mem_set_of_eq, mem_Icc] at hz h'z, simp only [hz, min_eq_left, sub_add_cancel] end, right_inv' := begin rintros ⟨z, hz⟩ h'z, rw subtype.mk_eq_mk, funext, dsimp at hz h'z, have A : x + z 0 ≤ y, by linarith, rw subsingleton.elim i 0, simp only [A, add_comm, add_sub_cancel', min_eq_left], end, open_source := begin have : is_open {z : ℝ | z < y} := is_open_Iio, exact this.preimage continuous_subtype_val end, open_target := begin have : is_open {z : ℝ | z < y - x} := is_open_Iio, have : is_open {z : euclidean_space ℝ (fin 1) | z 0 < y - x} := this.preimage (@continuous_apply (fin 1) (λ _, ℝ) _ 0), exact this.preimage continuous_subtype_val end, continuous_to_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have : continuous (λ (z : ℝ) (i : fin 1), z - x) := continuous.sub (continuous_pi $ λi, continuous_id) continuous_const, exact this.comp continuous_subtype_val, end, continuous_inv_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have A : continuous (λ z : ℝ, min (z + x) y) := (continuous_id.add continuous_const).min continuous_const, have B : continuous (λz : euclidean_space ℝ (fin 1), z 0) := continuous_apply 0, exact (A.comp B).comp continuous_subtype_val end } /-- The right chart for the topological space `[x, y]`, defined on `(x,y]` and sending `y` to `0` in `euclidean_half_space 1`. -/ def Icc_right_chart (x y : ℝ) [fact (x < y)] : local_homeomorph (Icc x y) (euclidean_half_space 1) := { source := {z : Icc x y | x < z.val}, target := {z : euclidean_half_space 1 | z.val 0 < y - x}, to_fun := λ(z : Icc x y), ⟨λi, y - z.val, sub_nonneg.mpr z.property.2⟩, inv_fun := λz, ⟨max (y - z.val 0) x, by simp [le_refl, z.prop, le_of_lt (fact.out (x < y)), sub_eq_add_neg]⟩, map_source' := by simp only [imp_self, mem_set_of_eq, sub_lt_sub_iff_left, forall_true_iff], map_target' := by { simp only [lt_max_iff, mem_set_of_eq], assume z hz, left, dsimp [-subtype.val_eq_coe] at hz, linarith }, left_inv' := begin rintros ⟨z, hz⟩ h'z, simp only [mem_set_of_eq, mem_Icc] at hz h'z, simp only [hz, sub_eq_add_neg, max_eq_left, add_add_neg_cancel'_right, neg_add_rev, neg_neg] end, right_inv' := begin rintros ⟨z, hz⟩ h'z, rw subtype.mk_eq_mk, funext, dsimp at hz h'z, have A : x ≤ y - z 0, by linarith, rw subsingleton.elim i 0, simp only [A, sub_sub_cancel, max_eq_left], end, open_source := begin have : is_open {z : ℝ | x < z} := is_open_Ioi, exact this.preimage continuous_subtype_val end, open_target := begin have : is_open {z : ℝ | z < y - x} := is_open_Iio, have : is_open {z : euclidean_space ℝ (fin 1) | z 0 < y - x} := this.preimage (@continuous_apply (fin 1) (λ _, ℝ) _ 0), exact this.preimage continuous_subtype_val end, continuous_to_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have : continuous (λ (z : ℝ) (i : fin 1), y - z) := continuous_const.sub (continuous_pi (λi, continuous_id)), exact this.comp continuous_subtype_val, end, continuous_inv_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have A : continuous (λ z : ℝ, max (y - z) x) := (continuous_const.sub continuous_id).max continuous_const, have B : continuous (λz : euclidean_space ℝ (fin 1), z 0) := continuous_apply 0, exact (A.comp B).comp continuous_subtype_val end } /-- Charted space structure on `[x, y]`, using only two charts taking values in `euclidean_half_space 1`. -/ instance Icc_manifold (x y : ℝ) [fact (x < y)] : charted_space (euclidean_half_space 1) (Icc x y) := { atlas := {Icc_left_chart x y, Icc_right_chart x y}, chart_at := λz, if z.val < y then Icc_left_chart x y else Icc_right_chart x y, mem_chart_source := λz, begin by_cases h' : z.val < y, { simp only [h', if_true], exact h' }, { simp only [h', if_false], apply lt_of_lt_of_le (fact.out (x < y)), simpa only [not_lt] using h'} end, chart_mem_atlas := λz, by { by_cases h' : z.val < y; simp [h'] } } /-- The manifold structure on `[x, y]` is smooth. -/ instance Icc_smooth_manifold (x y : ℝ) [fact (x < y)] : smooth_manifold_with_corners (𝓡∂ 1) (Icc x y) := begin have M : times_cont_diff_on ℝ ∞ (λz : euclidean_space ℝ (fin 1), - z + (λi, y - x)) univ, { rw times_cont_diff_on_univ, exact times_cont_diff_id.neg.add times_cont_diff_const }, apply smooth_manifold_with_corners_of_times_cont_diff_on, assume e e' he he', simp only [atlas, mem_singleton_iff, mem_insert_iff] at he he', /- We need to check that any composition of two charts gives a `C^∞` function. Each chart can be either the left chart or the right chart, leaving 4 possibilities that we handle successively. -/ rcases he with rfl | rfl; rcases he' with rfl | rfl, { -- `e = left chart`, `e' = left chart` exact (mem_groupoid_of_pregroupoid.mpr (symm_trans_mem_times_cont_diff_groupoid _ _ _)).1 }, { -- `e = left chart`, `e' = right chart` apply M.congr_mono _ (subset_univ _), rintro _ ⟨⟨hz₁, hz₂⟩, ⟨⟨z, hz₀⟩, rfl⟩⟩, simp only [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, update_same, max_eq_left, hz₀, lt_sub_iff_add_lt] with mfld_simps at hz₁ hz₂, rw [min_eq_left hz₁.le, lt_add_iff_pos_left] at hz₂, ext i, rw subsingleton.elim i 0, simp only [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, *, pi_Lp.add_apply, pi_Lp.neg_apply, max_eq_left, min_eq_left hz₁.le, update_same] with mfld_simps, abel }, { -- `e = right chart`, `e' = left chart` apply M.congr_mono _ (subset_univ _), rintro _ ⟨⟨hz₁, hz₂⟩, ⟨z, hz₀⟩, rfl⟩, simp only [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, max_lt_iff, update_same, max_eq_left hz₀] with mfld_simps at hz₁ hz₂, rw lt_sub at hz₁, ext i, rw subsingleton.elim i 0, simp only [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, pi_Lp.add_apply, pi_Lp.neg_apply, update_same, max_eq_left, hz₀, hz₁.le] with mfld_simps, abel }, { -- `e = right chart`, `e' = right chart` exact (mem_groupoid_of_pregroupoid.mpr (symm_trans_mem_times_cont_diff_groupoid _ _ _)).1 } end /-! Register the manifold structure on `Icc 0 1`, and also its zero and one. -/ section lemma fact_zero_lt_one : fact ((0 : ℝ) < 1) := ⟨zero_lt_one⟩ local attribute [instance] fact_zero_lt_one instance : charted_space (euclidean_half_space 1) (Icc (0 : ℝ) 1) := by apply_instance instance : smooth_manifold_with_corners (𝓡∂ 1) (Icc (0 : ℝ) 1) := by apply_instance end
2703a6b9ea3b199f0dd5251685d0d523ec42758e
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/algebra/norm_num.lean
795ab73a748d3d122a60aef3e35109b927726886
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
8,692
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis and Leonardo de Moura -/ prelude import init.algebra.field init.algebra.ordered_ring import init.data.nat.lemmas namespace norm_num universe u variable {α : Type u} def add1 [has_add α] [has_one α] (a : α) : α := a + 1 local attribute [reducible] bit0 bit1 add1 local attribute [simp] right_distrib left_distrib private meta def u : tactic unit := `[unfold bit0 bit1 add1] private meta def usimp : tactic unit := u >> `[simp] lemma mul_zero [mul_zero_class α] (a : α) : a * 0 = 0 := by simp lemma zero_mul [mul_zero_class α] (a : α) : 0 * a = 0 := by simp lemma mul_one [monoid α] (a : α) : a * 1 = a := by simp lemma mul_bit0 [distrib α] (a b : α) : a * (bit0 b) = bit0 (a * b) := by simp lemma mul_bit0_helper [distrib α] (a b t : α) (h : a * b = t) : a * (bit0 b) = bit0 t := begin rw [-h], simp end lemma mul_bit1 [semiring α] (a b : α) : a * (bit1 b) = bit0 (a * b) + a := by simp lemma mul_bit1_helper [semiring α] (a b s t : α) (hs : a * b = s) (ht : bit0 s + a = t) : a * (bit1 b) = t := by simp [hs, ht] lemma subst_into_prod [has_mul α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by simp [prl, prr, prt] lemma mk_cong (op : α → α) (a b : α) (h : a = b) : op a = op b := by simp [h] lemma neg_add_neg_eq_of_add_add_eq_zero [add_comm_group α] (a b c : α) (h : c + a + b = 0) : -a + -b = c := begin apply add_neg_eq_of_eq_add, apply neg_eq_of_add_eq_zero, simp at h, simp, assumption end lemma neg_add_neg_helper [add_comm_group α] (a b c : α) (h : a + b = c) : -a + -b = -c := begin apply @neg_inj α, simp [neg_add, neg_neg], assumption end lemma neg_add_pos_eq_of_eq_add [add_comm_group α] (a b c : α) (h : b = c + a) : -a + b = c := begin apply neg_add_eq_of_eq_add, simp at h, assumption end lemma neg_add_pos_helper1 [add_comm_group α] (a b c : α) (h : b + c = a) : -a + b = -c := begin apply neg_add_eq_of_eq_add, apply eq_add_neg_of_add_eq h end lemma neg_add_pos_helper2 [add_comm_group α] (a b c : α) (h : a + c = b) : -a + b = c := begin apply neg_add_eq_of_eq_add, rw h end lemma pos_add_neg_helper [add_comm_group α] (a b c : α) (h : b + a = c) : a + b = c := by rw [-h, add_comm a b] lemma subst_into_subtr [add_group α] (l r t : α) (h : l + -r = t) : l - r = t := by simp [h] lemma neg_neg_helper [add_group α] (a b : α) (h : a = -b) : -a = b := by simp [h] lemma neg_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * (-b) = c := by simp [h] lemma neg_mul_pos_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * b = -c := by simp [h] lemma pos_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : a * (-b) = -c := by simp [h] lemma div_add_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : n + b * d = val) (h2 : c * d = val) : n / d + b = c := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw [h2, -h, right_distrib, div_mul_cancel _ hd] end lemma add_div_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : d * b + n = val) (h2 : d * c = val) : b + n / d = c := begin apply eq_of_mul_eq_mul_of_nonzero_left hd, rw [h2, -h, left_distrib, mul_div_cancel' _ hd] end lemma div_mul_helper [field α] (n d c v : α) (hd : d ≠ 0) (h : (n * c) / d = v) : (n / d) * c = v := by rw [-h, field.div_mul_eq_mul_div_comm _ _ hd, mul_div_assoc] lemma mul_div_helper [field α] (a n d v : α) (hd : d ≠ 0) (h : (a * n) / d = v) : a * (n / d) = v := by rw [-h, mul_div_assoc] lemma nonzero_of_div_helper [field α] (a b : α) (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := begin intro hab, assert habb : (a / b) * b = 0, rw [hab, zero_mul], rw [div_mul_cancel _ hb] at habb, exact ha habb end lemma div_helper [field α] (n d v : α) (hd : d ≠ 0) (h : v * d = n) : n / d = v := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw (div_mul_cancel _ hd), exact eq.symm h end lemma div_eq_div_helper [field α] (a b c d v : α) (h1 : a * d = v) (h2 : c * b = v) (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d := begin apply eq_div_of_mul_eq, exact hd, rw div_mul_eq_mul_div, apply eq.symm, apply eq_div_of_mul_eq, exact hb, rw [h1, h2] end lemma subst_into_div [has_div α] (a₁ b₁ a₂ b₂ v : α) (h : a₁ / b₁ = v) (h1 : a₂ = a₁) (h2 : b₂ = b₁) : a₂ / b₂ = v := by rw [h1, h2, h] lemma add_comm_four [add_comm_semigroup α] (a b : α) : a + a + (b + b) = (a + b) + (a + b) := by simp lemma add_comm_middle [add_comm_semigroup α] (a b c : α) : a + b + c = a + c + b := by simp lemma bit0_add_bit0 [add_comm_semigroup α] (a b : α) : bit0 a + bit0 b = bit0 (a + b) := by usimp lemma bit0_add_bit0_helper [add_comm_semigroup α] (a b t : α) (h : a + b = t) : bit0 a + bit0 b = bit0 t := begin rw -h, usimp end lemma bit1_add_bit0 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit0 b = bit1 (a + b) := by usimp lemma bit1_add_bit0_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit1 a + bit0 b = bit1 t := begin rw -h, usimp end lemma bit0_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit0 a + bit1 b = bit1 (a + b) := by usimp lemma bit0_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit0 a + bit1 b = bit1 t := begin rw -h, usimp end lemma bit1_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit1 b = bit0 (add1 (a + b)) := by usimp lemma bit1_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t s : α) (h : (a + b) = t) (h2 : add1 t = s) : bit1 a + bit1 b = bit0 s := begin rw -h at h2, rw -h2, usimp end lemma bin_add_zero [add_monoid α] (a : α) : a + 0 = a := by simp lemma bin_zero_add [add_monoid α] (a : α) : 0 + a = a := by simp lemma one_add_bit0 [add_comm_semigroup α] [has_one α] (a : α) : 1 + bit0 a = bit1 a := begin unfold bit0 bit1, simp end lemma bit0_add_one [has_add α] [has_one α] (a : α) : bit0 a + 1 = bit1 a := rfl lemma bit1_add_one [has_add α] [has_one α] (a : α) : bit1 a + 1 = add1 (bit1 a) := rfl lemma bit1_add_one_helper [has_add α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : bit1 a + 1 = t := by rw -h lemma one_add_bit1 [add_comm_semigroup α] [has_one α] (a : α) : 1 + bit1 a = add1 (bit1 a) := begin unfold bit0 bit1 add1, simp end lemma one_add_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : 1 + bit1 a = t := begin rw -h, usimp end lemma add1_bit0 [has_add α] [has_one α] (a : α) : add1 (bit0 a) = bit1 a := rfl lemma add1_bit1 [add_comm_semigroup α] [has_one α] (a : α) : add1 (bit1 a) = bit0 (add1 a) := by usimp lemma add1_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 a = t) : add1 (bit1 a) = bit0 t := begin rw -h, usimp end lemma add1_one [has_add α] [has_one α] : add1 (1 : α) = bit0 1 := rfl lemma add1_zero [add_monoid α] [has_one α] : add1 (0 : α) = 1 := by usimp lemma one_add_one [has_add α] [has_one α] : (1 : α) + 1 = bit0 1 := rfl lemma subst_into_sum [has_add α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t := by rw [-prt, prr, prl] lemma neg_zero_helper [add_group α] (a : α) (h : a = 0) : - a = 0 := begin rw h, simp end lemma pos_bit0_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : bit0 a > 0 := begin u, apply add_pos h h end lemma nonneg_bit0_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit0 a ≥ 0 := begin u, apply add_nonneg h h end lemma pos_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a > 0 := begin u, apply add_pos_of_nonneg_of_pos, apply nonneg_bit0_helper _ h, apply zero_lt_one end lemma nonneg_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a ≥ 0 := begin apply le_of_lt, apply pos_bit1_helper _ h end lemma nonzero_of_pos_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : a ≠ 0 := ne_of_gt h lemma nonzero_of_neg_helper [linear_ordered_ring α] (a : α) (h : a ≠ 0) : -a ≠ 0 := begin intro ha, apply h, apply neg_inj, rwa neg_zero end lemma sub_nat_zero_helper {a b c d: ℕ} (hac : a = c) (hbd : b = d) (hcd : c < d) : a - b = 0 := begin simp_using_hs, apply nat.sub_eq_zero_of_le, apply le_of_lt, assumption end lemma sub_nat_pos_helper {a b c d e : ℕ} (hac : a = c) (hbd : b = d) (hced : e + d = c) : a - b = e := begin simp_using_hs, rw [-hced, nat.add_sub_cancel] end end norm_num
67a42b66492fcd62b3fb0804b4a6636c3e8bf286
9dc8cecdf3c4634764a18254e94d43da07142918
/src/probability/ident_distrib.lean
e397568c6c6f47941a20deb1fc06fc854e1e6e00
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
15,381
lean
/- Copyright (c) 2022 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 probability.variance import measure_theory.function.uniform_integrable /-! # Identically distributed random variables Two random variables defined on two (possibly different) probability spaces but taking value in the same space are *identically distributed* if their distributions (i.e., the image probability measures on the target space) coincide. We define this concept and establish its basic properties in this file. ## Main definitions and results * `ident_distrib f g μ ν` registers that the image of `μ` under `f` coincides with the image of `ν` under `g` (and that `f` and `g` are almost everywhere measurable, as otherwise the image measures don't make sense). The measures can be kept implicit as in `ident_distrib f g` if the spaces are registered as measure spaces. * `ident_distrib.comp`: being identically distributed is stable under composition with measurable maps. There are two main kind of lemmas, under the assumption that `f` and `g` are identically distributed: lemmas saying that two quantities computed for `f` and `g` are the same, and lemmas saying that if `f` has some property then `g` also has it. The first kind is registered as `ident_distrib.foo_eq`, the second one as `ident_distrib.foo_snd` (in the latter case, to deduce a property of `f` from one of `g`, use `h.symm.foo_snd` where `h : ident_distrib f g μ ν`). For instance: * `ident_distrib.measure_mem_eq`: if `f` and `g` are identically distributed, then the probabilities that they belong to a given measurable set are the same. * `ident_distrib.integral_eq`: if `f` and `g` are identically distributed, then their integrals are the same. * `ident_distrib.variance_eq`: if `f` and `g` are identically distributed, then their variances are the same. * `ident_distrib.ae_strongly_measurable_snd`: if `f` and `g` are identically distributed and `f` is almost everywhere strongly measurable, then so is `g`. * `ident_distrib.mem_ℒp_snd`: if `f` and `g` are identically distributed and `f` belongs to `ℒp`, then so does `g`. We also register several dot notation shortcuts for convenience. For instance, if `h : ident_distrib f g μ ν`, then `h.sq` states that `f^2` and `g^2` are identically distributed, and `h.norm` states that `∥f∥` and `∥g∥` are identically distributed, and so on. -/ open measure_theory filter finset noncomputable theory open_locale topological_space big_operators measure_theory ennreal nnreal variables {α β γ δ : Type*} [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] namespace probability_theory /-- Two functions defined on two (possibly different) measure spaces are identically distributed if their image measures coincide. This only makes sense when the functions are ae measurable (as otherwise the image measures are not defined), so we require this as well in the definition. -/ structure ident_distrib (f : α → γ) (g : β → γ) (μ : measure α . volume_tac) (ν : measure β . volume_tac) : Prop := (ae_measurable_fst : ae_measurable f μ) (ae_measurable_snd : ae_measurable g ν) (map_eq : measure.map f μ = measure.map g ν) namespace ident_distrib open topological_space variables {μ : measure α} {ν : measure β} {f : α → γ} {g : β → γ} protected lemma refl (hf : ae_measurable f μ) : ident_distrib f f μ μ := { ae_measurable_fst := hf, ae_measurable_snd := hf, map_eq := rfl } protected lemma symm (h : ident_distrib f g μ ν) : ident_distrib g f ν μ := { ae_measurable_fst := h.ae_measurable_snd, ae_measurable_snd := h.ae_measurable_fst, map_eq := h.map_eq.symm } protected lemma trans {ρ : measure δ} {h : δ → γ} (h₁ : ident_distrib f g μ ν) (h₂ : ident_distrib g h ν ρ) : ident_distrib f h μ ρ := { ae_measurable_fst := h₁.ae_measurable_fst, ae_measurable_snd := h₂.ae_measurable_snd, map_eq := h₁.map_eq.trans h₂.map_eq } protected lemma comp_of_ae_measurable {u : γ → δ} (h : ident_distrib f g μ ν) (hu : ae_measurable u (measure.map f μ)) : ident_distrib (u ∘ f) (u ∘ g) μ ν := { ae_measurable_fst := hu.comp_ae_measurable h.ae_measurable_fst, ae_measurable_snd := by { rw h.map_eq at hu, exact hu.comp_ae_measurable h.ae_measurable_snd }, map_eq := begin rw [← ae_measurable.map_map_of_ae_measurable hu h.ae_measurable_fst, ← ae_measurable.map_map_of_ae_measurable _ h.ae_measurable_snd, h.map_eq], rwa ← h.map_eq, end } protected lemma comp {u : γ → δ} (h : ident_distrib f g μ ν) (hu : measurable u) : ident_distrib (u ∘ f) (u ∘ g) μ ν := h.comp_of_ae_measurable hu.ae_measurable protected lemma of_ae_eq {g : α → γ} (hf : ae_measurable f μ) (heq : f =ᵐ[μ] g) : ident_distrib f g μ μ := { ae_measurable_fst := hf, ae_measurable_snd := hf.congr heq, map_eq := measure.map_congr heq } lemma measure_mem_eq (h : ident_distrib f g μ ν) {s : set γ} (hs : measurable_set s) : μ (f ⁻¹' s) = ν (g ⁻¹' s) := by rw [← measure.map_apply_of_ae_measurable h.ae_measurable_fst hs, ← measure.map_apply_of_ae_measurable h.ae_measurable_snd hs, h.map_eq] alias measure_mem_eq ← measure_preimage_eq lemma ae_snd (h : ident_distrib f g μ ν) {p : γ → Prop} (pmeas : measurable_set {x | p x}) (hp : ∀ᵐ x ∂μ, p (f x)) : ∀ᵐ x ∂ν, p (g x) := begin apply (ae_map_iff h.ae_measurable_snd pmeas).1, rw ← h.map_eq, exact (ae_map_iff h.ae_measurable_fst pmeas).2 hp, end lemma ae_mem_snd (h : ident_distrib f g μ ν) {t : set γ} (tmeas : measurable_set t) (ht : ∀ᵐ x ∂μ, f x ∈ t) : ∀ᵐ x ∂ν, g x ∈ t := h.ae_snd tmeas ht /-- In a second countable topology, the first function in an identically distributed pair is a.e. strongly measurable. So is the second function, but use `h.symm.ae_strongly_measurable_fst` as `h.ae_strongly_measurable_snd` has a different meaning.-/ lemma ae_strongly_measurable_fst [topological_space γ] [metrizable_space γ] [opens_measurable_space γ] [second_countable_topology γ] (h : ident_distrib f g μ ν) : ae_strongly_measurable f μ := h.ae_measurable_fst.ae_strongly_measurable /-- If `f` and `g` are identically distributed and `f` is a.e. strongly measurable, so is `g`. -/ lemma ae_strongly_measurable_snd [topological_space γ] [metrizable_space γ] [borel_space γ] (h : ident_distrib f g μ ν) (hf : ae_strongly_measurable f μ) : ae_strongly_measurable g ν := begin refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨h.ae_measurable_snd, _⟩, rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 hf).2 with ⟨t, t_sep, ht⟩, refine ⟨closure t, t_sep.closure, _⟩, apply h.ae_mem_snd is_closed_closure.measurable_set, filter_upwards [ht] with x hx using subset_closure hx, end lemma ae_strongly_measurable_iff [topological_space γ] [metrizable_space γ] [borel_space γ] (h : ident_distrib f g μ ν) : ae_strongly_measurable f μ ↔ ae_strongly_measurable g ν := ⟨λ hf, h.ae_strongly_measurable_snd hf, λ hg, h.symm.ae_strongly_measurable_snd hg⟩ lemma ess_sup_eq [conditionally_complete_linear_order γ] [topological_space γ] [opens_measurable_space γ] [order_closed_topology γ] (h : ident_distrib f g μ ν) : ess_sup f μ = ess_sup g ν := begin have I : ∀ a, μ {x : α | a < f x} = ν {x : β | a < g x} := λ a, h.measure_mem_eq measurable_set_Ioi, simp_rw [ess_sup_eq_Inf, I], end lemma lintegral_eq {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (h : ident_distrib f g μ ν) : ∫⁻ x, f x ∂μ = ∫⁻ x, g x ∂ν := begin change ∫⁻ x, id (f x) ∂μ = ∫⁻ x, id (g x) ∂ν, rw [← lintegral_map' ae_measurable_id h.ae_measurable_fst, ← lintegral_map' ae_measurable_id h.ae_measurable_snd, h.map_eq], end lemma integral_eq [normed_add_comm_group γ] [normed_space ℝ γ] [complete_space γ] [borel_space γ] (h : ident_distrib f g μ ν) : ∫ x, f x ∂μ = ∫ x, g x ∂ν := begin by_cases hf : ae_strongly_measurable f μ, { have A : ae_strongly_measurable id (measure.map f μ), { rw ae_strongly_measurable_iff_ae_measurable_separable, rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 hf).2 with ⟨t, t_sep, ht⟩, refine ⟨ae_measurable_id, ⟨closure t, t_sep.closure, _⟩⟩, rw ae_map_iff h.ae_measurable_fst, { filter_upwards [ht] with x hx using subset_closure hx }, { exact is_closed_closure.measurable_set } }, change ∫ x, id (f x) ∂μ = ∫ x, id (g x) ∂ν, rw [← integral_map h.ae_measurable_fst A], rw h.map_eq at A, rw [← integral_map h.ae_measurable_snd A, h.map_eq] }, { rw integral_non_ae_strongly_measurable hf, rw h.ae_strongly_measurable_iff at hf, rw integral_non_ae_strongly_measurable hf } end lemma snorm_eq [normed_add_comm_group γ] [opens_measurable_space γ] (h : ident_distrib f g μ ν) (p : ℝ≥0∞) : snorm f p μ = snorm g p ν := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp only [h_top, snorm, snorm_ess_sup, ennreal.top_ne_zero, eq_self_iff_true, if_true, if_false], apply ess_sup_eq, exact h.comp (measurable_coe_nnreal_ennreal.comp measurable_nnnorm) }, simp only [snorm_eq_snorm' h0 h_top, snorm', one_div], congr' 1, apply lintegral_eq, exact h.comp (measurable.pow_const (measurable_coe_nnreal_ennreal.comp measurable_nnnorm) p.to_real), end lemma mem_ℒp_snd [normed_add_comm_group γ] [borel_space γ] {p : ℝ≥0∞} (h : ident_distrib f g μ ν) (hf : mem_ℒp f p μ) : mem_ℒp g p ν := begin refine ⟨h.ae_strongly_measurable_snd hf.ae_strongly_measurable, _⟩, rw ← h.snorm_eq, exact hf.2 end lemma mem_ℒp_iff [normed_add_comm_group γ] [borel_space γ] {p : ℝ≥0∞} (h : ident_distrib f g μ ν) : mem_ℒp f p μ ↔ mem_ℒp g p ν := ⟨λ hf, h.mem_ℒp_snd hf, λ hg, h.symm.mem_ℒp_snd hg⟩ lemma integrable_snd [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) (hf : integrable f μ) : integrable g ν := begin rw ← mem_ℒp_one_iff_integrable at hf ⊢, exact h.mem_ℒp_snd hf end lemma integrable_iff [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) : integrable f μ ↔ integrable g ν := ⟨λ hf, h.integrable_snd hf, λ hg, h.symm.integrable_snd hg⟩ protected lemma norm [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) : ident_distrib (λ x, ∥f x∥) (λ x, ∥g x∥) μ ν := h.comp measurable_norm protected lemma nnnorm [normed_add_comm_group γ] [borel_space γ] (h : ident_distrib f g μ ν) : ident_distrib (λ x, ∥f x∥₊) (λ x, ∥g x∥₊) μ ν := h.comp measurable_nnnorm protected lemma pow [has_pow γ ℕ] [has_measurable_pow γ ℕ] (h : ident_distrib f g μ ν) {n : ℕ} : ident_distrib (λ x, (f x) ^ n) (λ x, (g x) ^ n) μ ν := h.comp (measurable_id.pow_const n) protected lemma sq [has_pow γ ℕ] [has_measurable_pow γ ℕ] (h : ident_distrib f g μ ν) : ident_distrib (λ x, (f x) ^ 2) (λ x, (g x) ^ 2) μ ν := h.comp (measurable_id.pow_const 2) protected lemma coe_nnreal_ennreal {f : α → ℝ≥0} {g : β → ℝ≥0} (h : ident_distrib f g μ ν) : ident_distrib (λ x, (f x : ℝ≥0∞)) (λ x, (g x : ℝ≥0∞)) μ ν := h.comp measurable_coe_nnreal_ennreal @[to_additive] lemma mul_const [has_mul γ] [has_measurable_mul γ] (h : ident_distrib f g μ ν) (c : γ) : ident_distrib (λ x, f x * c) (λ x, g x * c) μ ν := h.comp (measurable_mul_const c) @[to_additive] lemma const_mul [has_mul γ] [has_measurable_mul γ] (h : ident_distrib f g μ ν) (c : γ) : ident_distrib (λ x, c * f x) (λ x, c * g x) μ ν := h.comp (measurable_const_mul c) @[to_additive] lemma div_const [has_div γ] [has_measurable_div γ] (h : ident_distrib f g μ ν) (c : γ) : ident_distrib (λ x, f x / c) (λ x, g x / c) μ ν := h.comp (has_measurable_div.measurable_div_const c) @[to_additive] lemma const_div [has_div γ] [has_measurable_div γ] (h : ident_distrib f g μ ν) (c : γ) : ident_distrib (λ x, c / f x) (λ x, c / g x) μ ν := h.comp (has_measurable_div.measurable_const_div c) lemma variance_eq {f : α → ℝ} {g : β → ℝ} (h : ident_distrib f g μ ν) : variance f μ = variance g ν := begin convert (h.sub_const (∫ x, f x ∂μ)).sq.integral_eq, rw h.integral_eq, refl end end ident_distrib section uniform_integrable open topological_space variables {E : Type*} [measurable_space E] [normed_add_comm_group E] [borel_space E] [second_countable_topology E] {μ : measure α} [is_finite_measure μ] /-- This lemma is superceded by `mem_ℒp.uniform_integrable_of_ident_distrib` which only require `ae_strongly_measurable`. -/ lemma mem_ℒp.uniform_integrable_of_ident_distrib_aux {ι : Type*} {f : ι → α → E} {j : ι} {p : ℝ≥0∞} (hp : 1 ≤ p) (hp' : p ≠ ∞) (hℒp : mem_ℒp (f j) p μ) (hfmeas : ∀ i, strongly_measurable (f i)) (hf : ∀ i, ident_distrib (f i) (f j) μ μ) : uniform_integrable f p μ := begin refine uniform_integrable_of' hp hp' hfmeas (λ ε hε, _), by_cases hι : nonempty ι, swap, { exact ⟨0, λ i, false.elim (hι $ nonempty.intro i)⟩ }, obtain ⟨C, hC₁, hC₂⟩ := hℒp.snorm_indicator_norm_ge_pos_le μ (hfmeas _) hε, have hmeas : ∀ i, measurable_set {x | (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ∥f i x∥₊} := λ i, measurable_set_le measurable_const (hfmeas _).measurable.nnnorm, refine ⟨⟨C, hC₁.le⟩, λ i, le_trans (le_of_eq _) hC₂⟩, have : {x : α | (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ∥f i x∥₊}.indicator (f i) = (λ x : E, if (⟨C, hC₁.le⟩ : ℝ≥0) ≤ ∥x∥₊ then x else 0) ∘ (f i), { ext x, simp only [set.indicator, set.mem_set_of_eq] }, simp_rw [coe_nnnorm, this], rw [← snorm_map_measure _ (hf i).ae_measurable_fst, (hf i).map_eq, snorm_map_measure _ (hf j).ae_measurable_fst], { refl }, all_goals { exact ae_strongly_measurable_id.indicator (measurable_set_le measurable_const measurable_nnnorm) }, end /-- A sequence of identically distributed Lᵖ functions is p-uniformly integrable. -/ lemma mem_ℒp.uniform_integrable_of_ident_distrib {ι : Type*} {f : ι → α → E} {j : ι} {p : ℝ≥0∞} (hp : 1 ≤ p) (hp' : p ≠ ∞) (hℒp : mem_ℒp (f j) p μ) (hf : ∀ i, ident_distrib (f i) (f j) μ μ) : uniform_integrable f p μ := begin have hfmeas : ∀ i, ae_strongly_measurable (f i) μ := λ i, (hf i).ae_strongly_measurable_iff.2 hℒp.1, set g : ι → α → E := λ i, (hfmeas i).some, have hgmeas : ∀ i, strongly_measurable (g i) := λ i, (Exists.some_spec $ hfmeas i).1, have hgeq : ∀ i, g i =ᵐ[μ] f i := λ i, (Exists.some_spec $ hfmeas i).2.symm, have hgℒp : mem_ℒp (g j) p μ := hℒp.ae_eq (hgeq j).symm, exact uniform_integrable.ae_eq (mem_ℒp.uniform_integrable_of_ident_distrib_aux hp hp' hgℒp hgmeas $ λ i, (ident_distrib.of_ae_eq (hgmeas i).ae_measurable (hgeq i)).trans ((hf i).trans $ ident_distrib.of_ae_eq (hfmeas j).ae_measurable (hgeq j).symm)) hgeq, end end uniform_integrable end probability_theory
8fa360bb8299343665bf54d74f53998efdd44d62
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/structure_opt_param.lean
70c538d16b74dee54f9e2d143afe86782596217f
[ "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
201
lean
structure S (α : Type := int) := (a b : α) def x : S := {a := 10, b := 10} #check x.a def y : S nat := {a := 10, b := 10} #check y.a def z : S string := {a := "hello", b := "world"} #check z.a
95e980414b3b160fc51c50f26cb30ebb7899a01e
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/polynomial/vieta.lean
133254a9140286a392956804b3e1b4ae40c93b4a
[ "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
4,438
lean
/- Copyright (c) 2020 Hanting Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hanting Zhang -/ import ring_theory.polynomial.symmetric /-! # Vieta's Formula The main result is `vieta.prod_X_add_C_eq_sum_esymm`, which shows that the product of linear terms `λ + X i` is equal to a linear combination of the symmetric polynomials `esymm σ R j`. ## Implementation Notes: We first take the viewpoint where the "roots" `X i` are variables. This means we work over `polynomial (mv_polynomial σ R)`, which enables us to talk about linear combinations of `esymm σ R j`. We then derive Vieta's formula in `polynomial R` by giving a valuation from each `X i` to `r i`. -/ universes u open_locale big_operators open finset polynomial fintype namespace mv_polynomial variables {R : Type u} [comm_semiring R] variables (σ : Type u) [fintype σ] /-- A sum version of Vieta's formula. Viewing `X i` as variables, the product of linear terms `λ + X i` is equal to a linear combination of the symmetric polynomials `esymm σ R j`. -/ lemma prod_X_add_C_eq_sum_esymm : (∏ i : σ, (polynomial.C (X i) + polynomial.X) : polynomial (mv_polynomial σ R) )= ∑ j in range (card σ + 1), (polynomial.C (esymm σ R j) * polynomial.X ^ (card σ - j)) := begin classical, rw [prod_add, sum_powerset], refine sum_congr begin congr end (λ j hj, _), rw [esymm, polynomial.C.map_sum, sum_mul], refine sum_congr rfl (λ t ht, _), have h : (univ \ t).card = card σ - j := by { rw card_sdiff (mem_powerset_len.mp ht).1, congr, exact (mem_powerset_len.mp ht).2 }, rw [(polynomial.C : mv_polynomial σ R →+* polynomial _).map_prod, prod_const, ← h], congr, end /-- A fully expanded sum version of Vieta's formula, evaluated at the roots. The product of linear terms `X + r i` is equal to `∑ j in range (n + 1), e_j * X ^ (n - j)`, where `e_j` is the `j`th symmetric polynomial of the constant terms `r i`. -/ lemma prod_X_add_C_eval (r : σ → R) : ∏ i : σ, (polynomial.C (r i) + polynomial.X) = ∑ i in range (card σ + 1), (∑ t in powerset_len i (univ : finset σ), ∏ i in t, polynomial.C (r i)) * polynomial.X ^ (card σ - i) := begin classical, have h := @prod_X_add_C_eq_sum_esymm _ _ σ _, apply_fun (polynomial.map (eval r)) at h, rw [map_prod, map_sum] at h, convert h, simp only [eval_X, map_add, polynomial.map_C, polynomial.map_X, eq_self_iff_true], funext, simp only [function.funext_iff, esymm, polynomial.map_C, map_sum, polynomial.C.map_sum, polynomial.map_C, map_pow, polynomial.map_X, map_mul], congr, funext, simp only [eval_prod, eval_X, (polynomial.C : R →+* polynomial R).map_prod], end lemma esymm_to_sum (r : σ → R) (j : ℕ) : polynomial.C (eval r (esymm σ R j)) = ∑ t in powerset_len j (univ : finset σ), ∏ i in t, polynomial.C (r i) := by simp only [esymm, eval_sum, eval_prod, eval_X, polynomial.C.map_sum, (polynomial.C : R →+* polynomial _).map_prod] /-- Vieta's formula for the coefficients of the product of linear terms `X + r i`, The `k`th coefficient is `∑ t in powerset_len (card σ - k) (univ : finset σ), ∏ i in t, r i`, i.e. the symmetric polynomial `esymm σ R (card σ - k)` of the constant terms `r i`. -/ lemma prod_X_add_C_coeff (r : σ → R) (k : ℕ) (h : k ≤ card σ): polynomial.coeff (∏ i : σ, (polynomial.C (r i) + polynomial.X)) k = ∑ t in powerset_len (card σ - k) (univ : finset σ), ∏ i in t, r i := begin have hk : filter (λ (x : ℕ), k = card σ - x) (range (card σ + 1)) = {card σ - k} := begin refine finset.ext (λ a, ⟨λ ha, _, λ ha, _ ⟩), rw mem_singleton, have hσ := (tsub_eq_iff_eq_add_of_le (mem_range_succ_iff.mp (mem_filter.mp ha).1)).mp ((mem_filter.mp ha).2).symm, symmetry, rwa [(tsub_eq_iff_eq_add_of_le h), add_comm], rw mem_filter, have haσ : a ∈ range (card σ + 1) := by { rw mem_singleton.mp ha, exact mem_range_succ_iff.mpr (@tsub_le_self _ _ _ _ _ k) }, refine ⟨haσ, eq.symm _⟩, rw tsub_eq_iff_eq_add_of_le (mem_range_succ_iff.mp haσ), have hσ := (tsub_eq_iff_eq_add_of_le h).mp (mem_singleton.mp ha).symm, rwa add_comm, end, simp only [prod_X_add_C_eval, ← esymm_to_sum, finset_sum_coeff, coeff_C_mul_X, sum_ite, hk, sum_singleton, esymm, eval_sum, eval_prod, eval_X, add_zero, sum_const_zero], end end mv_polynomial
36054aa143128657fce5a6a6fb17c587ced281f0
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/matrix.lean
82b0140bd377f4cb68f6ad3c21ea17f0751bc144
[ "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
19,823
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Eric Wieser -/ import analysis.normed_space.basic import analysis.normed_space.pi_Lp import analysis.inner_product_space.pi_L2 /-! # Matrices as a normed space In this file we provide the following non-instances for norms on matrices: * The elementwise norm: * `matrix.semi_normed_group` * `matrix.normed_group` * `matrix.normed_space` * The Frobenius norm: * `matrix.frobenius_semi_normed_group` * `matrix.frobenius_normed_group` * `matrix.frobenius_normed_space` * `matrix.frobenius_normed_ring` * `matrix.frobenius_normed_algebra` * The $L^\infty$ operator norm: * `matrix.linfty_op_semi_normed_group` * `matrix.linfty_op_normed_group` * `matrix.linfty_op_normed_space` * `matrix.linfty_op_non_unital_semi_normed_ring` * `matrix.linfty_op_semi_normed_ring` * `matrix.linfty_op_non_unital_normed_ring` * `matrix.linfty_op_normed_ring` * `matrix.linfty_op_normed_algebra` These are not declared as instances because there are several natural choices for defining the norm of a matrix. -/ noncomputable theory open_locale big_operators nnreal matrix namespace matrix variables {R l m n α β : Type*} [fintype l] [fintype m] [fintype n] /-! ### The elementwise supremum norm -/ section linf_linf section semi_normed_group variables [semi_normed_group α] [semi_normed_group β] /-- Seminormed group instance (using sup norm of sup norm) for matrices over a seminormed group. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ protected def semi_normed_group : semi_normed_group (matrix m n α) := pi.semi_normed_group local attribute [instance] matrix.semi_normed_group lemma norm_le_iff {r : ℝ} (hr : 0 ≤ r) {A : matrix m n α} : ∥A∥ ≤ r ↔ ∀ i j, ∥A i j∥ ≤ r := by simp [pi_norm_le_iff hr] lemma nnnorm_le_iff {r : ℝ≥0} {A : matrix m n α} : ∥A∥₊ ≤ r ↔ ∀ i j, ∥A i j∥₊ ≤ r := by simp [pi_nnnorm_le_iff] lemma norm_lt_iff {r : ℝ} (hr : 0 < r) {A : matrix m n α} : ∥A∥ < r ↔ ∀ i j, ∥A i j∥ < r := by simp [pi_norm_lt_iff hr] lemma nnnorm_lt_iff {r : ℝ≥0} (hr : 0 < r) {A : matrix m n α} : ∥A∥₊ < r ↔ ∀ i j, ∥A i j∥₊ < r := by simp [pi_nnnorm_lt_iff hr] lemma norm_entry_le_entrywise_sup_norm (A : matrix m n α) {i : m} {j : n} : ∥A i j∥ ≤ ∥A∥ := (norm_le_pi_norm (A i) j).trans (norm_le_pi_norm A i) lemma nnnorm_entry_le_entrywise_sup_nnnorm (A : matrix m n α) {i : m} {j : n} : ∥A i j∥₊ ≤ ∥A∥₊ := (nnnorm_le_pi_nnnorm (A i) j).trans (nnnorm_le_pi_nnnorm A i) @[simp] lemma nnnorm_map_eq (A : matrix m n α) (f : α → β) (hf : ∀ a, ∥f a∥₊ = ∥a∥₊) : ∥A.map f∥₊ = ∥A∥₊ := by simp_rw [pi.nnnorm_def, matrix.map, hf] @[simp] lemma norm_map_eq (A : matrix m n α) (f : α → β) (hf : ∀ a, ∥f a∥ = ∥a∥) : ∥A.map f∥ = ∥A∥ := (congr_arg (coe : ℝ≥0 → ℝ) $ nnnorm_map_eq A f $ λ a, subtype.ext $ hf a : _) @[simp] lemma nnnorm_transpose (A : matrix m n α) : ∥Aᵀ∥₊ = ∥A∥₊ := by { simp_rw [pi.nnnorm_def], exact finset.sup_comm _ _ _ } @[simp] lemma norm_transpose (A : matrix m n α) : ∥Aᵀ∥ = ∥A∥ := congr_arg coe $ nnnorm_transpose A @[simp] lemma nnnorm_conj_transpose [star_add_monoid α] [normed_star_group α] (A : matrix m n α) : ∥Aᴴ∥₊ = ∥A∥₊ := (nnnorm_map_eq _ _ nnnorm_star).trans A.nnnorm_transpose @[simp] lemma norm_conj_transpose [star_add_monoid α] [normed_star_group α] (A : matrix m n α) : ∥Aᴴ∥ = ∥A∥ := congr_arg coe $ nnnorm_conj_transpose A instance [star_add_monoid α] [normed_star_group α] : normed_star_group (matrix m m α) := ⟨norm_conj_transpose⟩ @[simp] lemma nnnorm_col (v : m → α) : ∥col v∥₊ = ∥v∥₊ := by simp [pi.nnnorm_def] @[simp] lemma norm_col (v : m → α) : ∥col v∥ = ∥v∥ := congr_arg coe $ nnnorm_col v @[simp] lemma nnnorm_row (v : n → α) : ∥row v∥₊ = ∥v∥₊ := by simp [pi.nnnorm_def] @[simp] lemma norm_row (v : n → α) : ∥row v∥ = ∥v∥ := congr_arg coe $ nnnorm_row v @[simp] lemma nnnorm_diagonal [decidable_eq n] (v : n → α) : ∥diagonal v∥₊ = ∥v∥₊ := begin simp_rw pi.nnnorm_def, congr' 1 with i : 1, refine le_antisymm (finset.sup_le $ λ j hj, _) _, { obtain rfl | hij := eq_or_ne i j, { rw diagonal_apply_eq }, { rw [diagonal_apply_ne _ hij, nnnorm_zero], exact zero_le _ }, }, { refine eq.trans_le _ (finset.le_sup (finset.mem_univ i)), rw diagonal_apply_eq } end @[simp] lemma norm_diagonal [decidable_eq n] (v : n → α) : ∥diagonal v∥ = ∥v∥ := congr_arg coe $ nnnorm_diagonal v /-- Note this is safe as an instance as it carries no data. -/ instance [nonempty n] [decidable_eq n] [has_one α] [norm_one_class α] : norm_one_class (matrix n n α) := ⟨(norm_diagonal _).trans $ norm_one⟩ end semi_normed_group /-- Normed group instance (using sup norm of sup norm) for matrices over a normed group. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ protected def normed_group [normed_group α] : normed_group (matrix m n α) := pi.normed_group section normed_space local attribute [instance] matrix.semi_normed_group variables [normed_field R] [semi_normed_group α] [normed_space R α] /-- Normed space instance (using sup norm of sup norm) for matrices over a normed space. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ protected def normed_space : normed_space R (matrix m n α) := pi.normed_space end normed_space end linf_linf /-! ### The $L_\infty$ operator norm This section defines the matrix norm $\|A\|_\infty = \operatorname{sup}_i (\sum_j \|A_{ij}\|)$. Note that this is equivalent to the operator norm, considering $A$ as a linear map between two $L^\infty$ spaces. -/ section linfty_op /-- Seminormed group instance (using sup norm of L1 norm) for matrices over a seminormed group. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_semi_normed_group [semi_normed_group α] : semi_normed_group (matrix m n α) := (by apply_instance : semi_normed_group (m → pi_Lp 1 (λ j : n, α))) /-- Normed group instance (using sup norm of L1 norm) for matrices over a normed ring. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_normed_group [normed_group α] : normed_group (matrix m n α) := (by apply_instance : normed_group (m → pi_Lp 1 (λ j : n, α))) /-- Normed space instance (using sup norm of L1 norm) for matrices over a normed space. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_normed_space [normed_field R] [semi_normed_group α] [normed_space R α] : normed_space R (matrix m n α) := (by apply_instance : normed_space R (m → pi_Lp 1 (λ j : n, α))) section semi_normed_group variables [semi_normed_group α] lemma linfty_op_norm_def (A : matrix m n α) : ∥A∥ = ((finset.univ : finset m).sup (λ i : m, ∑ j : n, ∥A i j∥₊) : ℝ≥0) := by simp_rw [pi.norm_def, pi_Lp.nnnorm_eq, div_one, nnreal.rpow_one] lemma linfty_op_nnnorm_def (A : matrix m n α) : ∥A∥₊ = (finset.univ : finset m).sup (λ i : m, ∑ j : n, ∥A i j∥₊) := subtype.ext $ linfty_op_norm_def A @[simp] lemma linfty_op_nnnorm_col (v : m → α) : ∥col v∥₊ = ∥v∥₊ := begin rw [linfty_op_nnnorm_def, pi.nnnorm_def], simp, end @[simp] lemma linfty_op_norm_col (v : m → α) : ∥col v∥ = ∥v∥ := congr_arg coe $ linfty_op_nnnorm_col v @[simp] lemma linfty_op_nnnorm_row (v : n → α) : ∥row v∥₊ = ∑ i, ∥v i∥₊ := by simp [linfty_op_nnnorm_def] @[simp] lemma linfty_op_norm_row (v : n → α) : ∥row v∥ = ∑ i, ∥v i∥ := (congr_arg coe $ linfty_op_nnnorm_row v).trans $ by simp [nnreal.coe_sum] @[simp] lemma linfty_op_nnnorm_diagonal [decidable_eq m] (v : m → α) : ∥diagonal v∥₊ = ∥v∥₊ := begin rw [linfty_op_nnnorm_def, pi.nnnorm_def], congr' 1 with i : 1, refine (finset.sum_eq_single_of_mem _ (finset.mem_univ i) $ λ j hj hij, _).trans _, { rw [diagonal_apply_ne' _ hij, nnnorm_zero] }, { rw [diagonal_apply_eq] }, end @[simp] lemma linfty_op_norm_diagonal [decidable_eq m] (v : m → α) : ∥diagonal v∥ = ∥v∥ := congr_arg coe $ linfty_op_nnnorm_diagonal v end semi_normed_group section non_unital_semi_normed_ring variables [non_unital_semi_normed_ring α] lemma linfty_op_nnnorm_mul (A : matrix l m α) (B : matrix m n α) : ∥A ⬝ B∥₊ ≤ ∥A∥₊ * ∥B∥₊ := begin simp_rw [linfty_op_nnnorm_def, matrix.mul_apply], calc finset.univ.sup (λ i, ∑ k, ∥∑ j, A i j * B j k∥₊) ≤ finset.univ.sup (λ i, ∑ k j, ∥A i j∥₊ * ∥B j k∥₊) : finset.sup_mono_fun $ λ i hi, finset.sum_le_sum $ λ k hk, nnnorm_sum_le_of_le _ $ λ j hj, nnnorm_mul_le _ _ ... = finset.univ.sup (λ i, ∑ j, (∥A i j∥₊ * ∑ k, ∥B j k∥₊)) : by simp_rw [@finset.sum_comm _ m n, finset.mul_sum] ... ≤ finset.univ.sup (λ i, ∑ j, ∥A i j∥₊ * finset.univ.sup (λ i, ∑ j, ∥B i j∥₊)) : finset.sup_mono_fun $ λ i hi, finset.sum_le_sum $ λ j hj, mul_le_mul_of_nonneg_left (finset.le_sup hj) (zero_le _) ... ≤ finset.univ.sup (λ i, ∑ j, ∥A i j∥₊) * finset.univ.sup (λ i, ∑ j, ∥B i j∥₊) : by simp_rw [←finset.sum_mul, ←nnreal.finset_sup_mul], end lemma linfty_op_norm_mul (A : matrix l m α) (B : matrix m n α) : ∥A ⬝ B∥ ≤ ∥A∥ * ∥B∥ := linfty_op_nnnorm_mul _ _ lemma linfty_op_nnnorm_mul_vec (A : matrix l m α) (v : m → α) : ∥A.mul_vec v∥₊ ≤ ∥A∥₊ * ∥v∥₊ := begin rw [←linfty_op_nnnorm_col (A.mul_vec v), ←linfty_op_nnnorm_col v], exact linfty_op_nnnorm_mul A (col v), end lemma linfty_op_norm_mul_vec (A : matrix l m α) (v : m → α) : ∥matrix.mul_vec A v∥ ≤ ∥A∥ * ∥v∥ := linfty_op_nnnorm_mul_vec _ _ end non_unital_semi_normed_ring /-- Seminormed non-unital ring instance (using sup norm of L1 norm) for matrices over a semi normed non-unital ring. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_non_unital_semi_normed_ring [non_unital_semi_normed_ring α] : non_unital_semi_normed_ring (matrix n n α) := { norm_mul := linfty_op_norm_mul, .. matrix.linfty_op_semi_normed_group, .. matrix.non_unital_ring } /-- The `L₁-L∞` norm preserves one on non-empty matrices. Note this is safe as an instance, as it carries no data. -/ instance linfty_op_norm_one_class [semi_normed_ring α] [norm_one_class α] [decidable_eq n] [nonempty n] : norm_one_class (matrix n n α) := { norm_one := (linfty_op_norm_diagonal _).trans norm_one } /-- Seminormed ring instance (using sup norm of L1 norm) for matrices over a semi normed ring. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_semi_normed_ring [semi_normed_ring α] [decidable_eq n] : semi_normed_ring (matrix n n α) := { .. matrix.linfty_op_non_unital_semi_normed_ring, .. matrix.ring } /-- Normed non-unital ring instance (using sup norm of L1 norm) for matrices over a normed non-unital ring. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_non_unital_normed_ring [non_unital_normed_ring α] : non_unital_normed_ring (matrix n n α) := { ..matrix.linfty_op_non_unital_semi_normed_ring } /-- Normed ring instance (using sup norm of L1 norm) for matrices over a normed ring. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_normed_ring [normed_ring α] [decidable_eq n] : normed_ring (matrix n n α) := { ..matrix.linfty_op_semi_normed_ring } /-- Normed algebra instance (using sup norm of L1 norm) for matrices over a normed algebra. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] protected def linfty_op_normed_algebra [normed_field R] [semi_normed_ring α] [normed_algebra R α] [decidable_eq n] : normed_algebra R (matrix n n α) := { ..matrix.linfty_op_normed_space } end linfty_op /-! ### The Frobenius norm This is defined as $\|A\| = \sqrt{\sum_{i,j} \|A_{ij}\|^2}$. When the matrix is over the real or complex numbers, this norm is submultiplicative. -/ section frobenius open_locale matrix big_operators /-- Seminormed group instance (using frobenius norm) for matrices over a seminormed group. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] def frobenius_semi_normed_group [semi_normed_group α] : semi_normed_group (matrix m n α) := (by apply_instance : semi_normed_group (pi_Lp 2 (λ i : m, pi_Lp 2 (λ j : n, α)))) /-- Normed group instance (using frobenius norm) for matrices over a normed group. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] def frobenius_normed_group [normed_group α] : normed_group (matrix m n α) := (by apply_instance : normed_group (pi_Lp 2 (λ i : m, pi_Lp 2 (λ j : n, α)))) /-- Normed space instance (using frobenius norm) for matrices over a normed space. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] def frobenius_normed_space [normed_field R] [semi_normed_group α] [normed_space R α] : normed_space R (matrix m n α) := (by apply_instance : normed_space R (pi_Lp 2 (λ i : m, pi_Lp 2 (λ j : n, α)))) section semi_normed_group variables [semi_normed_group α] [semi_normed_group β] lemma frobenius_nnnorm_def (A : matrix m n α) : ∥A∥₊ = (∑ i j, ∥A i j∥₊ ^ (2 : ℝ)) ^ (1/2 : ℝ) := by simp_rw [pi_Lp.nnnorm_eq, ←nnreal.rpow_mul, div_mul_cancel (1 : ℝ) two_ne_zero, nnreal.rpow_one] lemma frobenius_norm_def (A : matrix m n α) : ∥A∥ = (∑ i j, ∥A i j∥ ^ (2 : ℝ)) ^ (1/2 : ℝ) := (congr_arg coe (frobenius_nnnorm_def A)).trans $ by simp [nnreal.coe_sum] @[simp] lemma frobenius_nnnorm_map_eq (A : matrix m n α) (f : α → β) (hf : ∀ a, ∥f a∥₊ = ∥a∥₊) : ∥A.map f∥₊ = ∥A∥₊ := by simp_rw [frobenius_nnnorm_def, matrix.map, hf] @[simp] lemma frobenius_norm_map_eq (A : matrix m n α) (f : α → β) (hf : ∀ a, ∥f a∥ = ∥a∥) : ∥A.map f∥ = ∥A∥ := (congr_arg (coe : ℝ≥0 → ℝ) $ frobenius_nnnorm_map_eq A f $ λ a, subtype.ext $ hf a : _) @[simp] lemma frobenius_nnnorm_transpose (A : matrix m n α) : ∥Aᵀ∥₊ = ∥A∥₊ := by { rw [frobenius_nnnorm_def, frobenius_nnnorm_def, finset.sum_comm], refl } @[simp] lemma frobenius_norm_transpose (A : matrix m n α) : ∥Aᵀ∥ = ∥A∥ := congr_arg coe $ frobenius_nnnorm_transpose A @[simp] lemma frobenius_nnnorm_conj_transpose [star_add_monoid α] [normed_star_group α] (A : matrix m n α) : ∥Aᴴ∥₊ = ∥A∥₊ := (frobenius_nnnorm_map_eq _ _ nnnorm_star).trans A.frobenius_nnnorm_transpose @[simp] lemma frobenius_norm_conj_transpose [star_add_monoid α] [normed_star_group α] (A : matrix m n α) : ∥Aᴴ∥ = ∥A∥ := congr_arg coe $ frobenius_nnnorm_conj_transpose A instance frobenius_normed_star_group [star_add_monoid α] [normed_star_group α] : normed_star_group (matrix m m α) := ⟨frobenius_norm_conj_transpose⟩ @[simp] lemma frobenius_norm_row (v : m → α) : ∥row v∥ = ∥(pi_Lp.equiv 2 _).symm v∥ := by { rw [frobenius_norm_def, fintype.sum_unique], refl } @[simp] lemma frobenius_nnnorm_row (v : m → α) : ∥row v∥₊ = ∥(pi_Lp.equiv 2 _).symm v∥₊ := subtype.ext $ frobenius_norm_row v @[simp] lemma frobenius_norm_col (v : n → α) : ∥col v∥ = ∥(pi_Lp.equiv 2 _).symm v∥ := by { simp_rw [frobenius_norm_def, fintype.sum_unique], refl } @[simp] lemma frobenius_nnnorm_col (v : n → α) : ∥col v∥₊ = ∥(pi_Lp.equiv 2 _).symm v∥₊ := subtype.ext $ frobenius_norm_col v @[simp] lemma frobenius_nnnorm_diagonal [decidable_eq n] (v : n → α) : ∥diagonal v∥₊ = ∥(pi_Lp.equiv 2 _).symm v∥₊ := begin simp_rw [frobenius_nnnorm_def, ←finset.sum_product', finset.univ_product_univ, pi_Lp.nnnorm_eq], let s := (finset.univ : finset n).map ⟨λ i : n, (i, i), λ i j h, congr_arg prod.fst h⟩, rw ←finset.sum_subset (finset.subset_univ s) (λ i hi his, _), { rw finset.sum_map, dsimp, simp_rw diagonal_apply_eq }, { suffices : i.1 ≠ i.2, { rw [diagonal_apply_ne _ this, nnnorm_zero, nnreal.zero_rpow two_ne_zero], }, intro h, exact finset.mem_map.not.mp his ⟨i.1, finset.mem_univ _, prod.ext rfl h⟩ } end @[simp] lemma frobenius_norm_diagonal [decidable_eq n] (v : n → α) : ∥diagonal v∥ = ∥(pi_Lp.equiv 2 _).symm v∥ := (congr_arg coe $ frobenius_nnnorm_diagonal v : _).trans rfl end semi_normed_group lemma frobenius_nnnorm_one [decidable_eq n] [semi_normed_group α] [has_one α] : ∥(1 : matrix n n α)∥₊ = nnreal.sqrt (fintype.card n) * ∥(1 : α)∥₊:= begin refine (frobenius_nnnorm_diagonal _).trans _, simp_rw [pi_Lp.nnnorm_equiv_symm_const, nnreal.sqrt_eq_rpow], end section is_R_or_C variables [is_R_or_C α] lemma frobenius_nnnorm_mul (A : matrix l m α) (B : matrix m n α) : ∥A ⬝ B∥₊ ≤ ∥A∥₊ * ∥B∥₊ := begin simp_rw [frobenius_nnnorm_def, matrix.mul_apply], rw [←nnreal.mul_rpow, @finset.sum_comm _ n m, finset.sum_mul_sum, finset.sum_product], refine nnreal.rpow_le_rpow _ one_half_pos.le, refine finset.sum_le_sum (λ i hi, finset.sum_le_sum $ λ j hj, _), rw [← nnreal.rpow_le_rpow_iff one_half_pos, ← nnreal.rpow_mul, mul_div_cancel' (1 : ℝ) two_ne_zero, nnreal.rpow_one, nnreal.mul_rpow], dsimp only, have := @nnnorm_inner_le_nnnorm α _ _ _ ((pi_Lp.equiv 2 (λ i, α)).symm (λ j, star (A i j))) ((pi_Lp.equiv 2 (λ i, α)).symm (λ k, B k j)), simpa only [pi_Lp.equiv_symm_apply, pi_Lp.inner_apply, is_R_or_C.inner_apply, star_ring_end_apply, pi.nnnorm_def, pi_Lp.nnnorm_eq, star_star, nnnorm_star] using this, end lemma frobenius_norm_mul (A : matrix l m α) (B : matrix m n α) : ∥A ⬝ B∥ ≤ ∥A∥ * ∥B∥ := frobenius_nnnorm_mul A B /-- Normed ring instance (using frobenius norm) for matrices over `ℝ` or `ℂ`. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] def frobenius_normed_ring [decidable_eq m] : normed_ring (matrix m m α) := { norm := has_norm.norm, norm_mul := frobenius_norm_mul, ..matrix.frobenius_semi_normed_group } /-- Normed algebra instance (using frobenius norm) for matrices over `ℝ` or `ℂ`. Not declared as an instance because there are several natural choices for defining the norm of a matrix. -/ local attribute [instance] def frobenius_normed_algebra [decidable_eq m] [normed_field R] [normed_algebra R α] : normed_algebra R (matrix m m α) := { ..matrix.frobenius_normed_space } end is_R_or_C end frobenius end matrix
f5b02255e81befb563fd34175cf49f8f398f788e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_simp1.lean
39fcb27eb84f3746d5750af3ab393f8332e8c712
[ "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
410
lean
example (a b c : Prop) : a ∧ b ∧ c ↔ c ∧ b ∧ a := by simp example (a b c : Prop) : a ∧ false ∧ c ↔ false := by simp example (a b c : Prop) : a ∨ false ∨ b ↔ b ∨ a := by simp example (a b c : Prop) : ¬ true ∨ false ∨ b ↔ b := by simp example (a b c : Prop) : if true then a else b ↔ if false then b else a := by simp example (a b : Prop) : a ∧ not a ↔ false := by simp
babe799aa1ebe83d908236b6544b5db4c422cb39
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/adic_completion.lean
3f73138ad4edd47a6a7ed16fbb06bfa3e3bec96b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,110
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.geom_sum import linear_algebra.smodeq import ring_theory.jacobson_ideal /-! # Completion of a module with respect to an ideal. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M` with respect to an ideal `I`: ## Main definitions - `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`. - `is_precomplete I M`: this says that every Cauchy sequence converges. - `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete. - `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`. - `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO) with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is precomplete. -/ open submodule variables {R : Type*} [comm_ring R] (I : ideal R) variables (M : Type*) [add_comm_group M] [module R M] variables {N : Type*} [add_comm_group N] [module R N] /-- A module `M` is Hausdorff with respect to an ideal `I` if `⋂ I^n M = 0`. -/ class is_Hausdorff : Prop := (haus' : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0) /-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/ class is_precomplete : Prop := (prec' : ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)]) /-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/ class is_adic_complete extends is_Hausdorff I M, is_precomplete I M : Prop variables {I M} theorem is_Hausdorff.haus (h : is_Hausdorff I M) : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := is_Hausdorff.haus' theorem is_Hausdorff_iff : is_Hausdorff I M ↔ ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := ⟨is_Hausdorff.haus, λ h, ⟨h⟩⟩ theorem is_precomplete.prec (h : is_precomplete I M) {f : ℕ → M} : (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := is_precomplete.prec' _ theorem is_precomplete_iff : is_precomplete I M ↔ ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := ⟨λ h, h.1, λ h, ⟨h⟩⟩ variables (I M) /-- The Hausdorffification of a module with respect to an ideal. -/ @[reducible] def Hausdorffification : Type* := M ⧸ (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) /-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff. In fact, this is only complete if the ideal is finitely generated. -/ def adic_completion : submodule R (Π n : ℕ, (M ⧸ (I ^ n • ⊤ : submodule R M))) := { carrier := { f | ∀ {m n} (h : m ≤ n), liftq _ (mkq _) (by { rw ker_mkq, exact smul_mono (ideal.pow_le_pow h) le_rfl }) (f n) = f m }, zero_mem' := λ m n hmn, by rw [pi.zero_apply, pi.zero_apply, linear_map.map_zero], add_mem' := λ f g hf hg m n hmn, by rw [pi.add_apply, pi.add_apply, linear_map.map_add, hf hmn, hg hmn], smul_mem' := λ c f hf m n hmn, by rw [pi.smul_apply, pi.smul_apply, linear_map.map_smul, hf hmn] } namespace is_Hausdorff instance bot : is_Hausdorff (⊥ : ideal R) M := ⟨λ x hx, by simpa only [pow_one ⊥, bot_smul, smodeq.bot] using hx 1⟩ variables {M} protected theorem subsingleton (h : is_Hausdorff (⊤ : ideal R) M) : subsingleton M := ⟨λ x y, eq_of_sub_eq_zero $ h.haus (x - y) $ λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩ variables (M) @[priority 100] instance of_subsingleton [subsingleton M] : is_Hausdorff I M := ⟨λ x _, subsingleton.elim _ _⟩ variables {I M} theorem infi_pow_smul (h : is_Hausdorff I M) : (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) = ⊥ := eq_bot_iff.2 $ λ x hx, (mem_bot _).2 $ h.haus x $ λ n, smodeq.zero.2 $ (mem_infi $ λ n : ℕ, I ^ n • ⊤).1 hx n end is_Hausdorff namespace Hausdorffification /-- The canonical linear map to the Hausdorffification. -/ def of : M →ₗ[R] Hausdorffification I M := mkq _ variables {I M} @[elab_as_eliminator] lemma induction_on {C : Hausdorffification I M → Prop} (x : Hausdorffification I M) (ih : ∀ x, C (of I M x)) : C x := quotient.induction_on' x ih variables (I M) instance : is_Hausdorff I (Hausdorffification I M) := ⟨λ x, quotient.induction_on' x $ λ x hx, (quotient.mk_eq_zero _).2 $ (mem_infi _).2 $ λ n, begin have := comap_map_mkq (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) (I ^ n • ⊤), simp only [sup_of_le_right (infi_le (λ n, (I ^ n • ⊤ : submodule R M)) n)] at this, rw [← this, map_smul'', mem_comap, submodule.map_top, range_mkq, ← smodeq.zero], exact hx n end⟩ variables {M} [h : is_Hausdorff I N] include h /-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a unique map from the Hausdorffification. -/ def lift (f : M →ₗ[R] N) : Hausdorffification I M →ₗ[R] N := liftq _ f $ map_le_iff_le_comap.1 $ h.infi_pow_smul ▸ le_infi (λ n, le_trans (map_mono $ infi_le _ n) $ by { rw map_smul'', exact smul_mono le_rfl le_top }) theorem lift_of (f : M →ₗ[R] N) (x : M) : lift I f (of I M x) = f x := rfl theorem lift_comp_of (f : M →ₗ[R] N) : (lift I f).comp (of I M) = f := linear_map.ext $ λ _, rfl /-- Uniqueness of lift. -/ theorem lift_eq (f : M →ₗ[R] N) (g : Hausdorffification I M →ₗ[R] N) (hg : g.comp (of I M) = f) : g = lift I f := linear_map.ext $ λ x, induction_on x $ λ x, by rw [lift_of, ← hg, linear_map.comp_apply] end Hausdorffification namespace is_precomplete instance bot : is_precomplete (⊥ : ideal R) M := begin refine ⟨λ f hf, ⟨f 1, λ n, _⟩⟩, cases n, { rw [pow_zero, ideal.one_eq_top, top_smul], exact smodeq.top }, specialize hf (nat.le_add_left 1 n), rw [pow_one, bot_smul, smodeq.bot] at hf, rw hf end instance top : is_precomplete (⊤ : ideal R) M := ⟨λ f hf, ⟨0, λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩⟩ @[priority 100] instance of_subsingleton [subsingleton M] : is_precomplete I M := ⟨λ f hf, ⟨0, λ n, by rw subsingleton.elim (f n) 0⟩⟩ end is_precomplete namespace adic_completion /-- The canonical linear map to the completion. -/ def of : M →ₗ[R] adic_completion I M := { to_fun := λ x, ⟨λ n, mkq _ x, λ m n hmn, rfl⟩, map_add' := λ x y, rfl, map_smul' := λ c x, rfl } @[simp] lemma of_apply (x : M) (n : ℕ) : (of I M x).1 n = mkq _ x := rfl /-- Linearly evaluating a sequence in the completion at a given input. -/ def eval (n : ℕ) : adic_completion I M →ₗ[R] (M ⧸ (I ^ n • ⊤ : submodule R M)) := { to_fun := λ f, f.1 n, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_eval (n : ℕ) : (eval I M n : adic_completion I M → (M ⧸ (I ^ n • ⊤ : submodule R M))) = λ f, f.1 n := rfl lemma eval_apply (n : ℕ) (f : adic_completion I M) : eval I M n f = f.1 n := rfl lemma eval_of (n : ℕ) (x : M) : eval I M n (of I M x) = mkq _ x := rfl @[simp] lemma eval_comp_of (n : ℕ) : (eval I M n).comp (of I M) = mkq _ := rfl @[simp] lemma range_eval (n : ℕ) : (eval I M n).range = ⊤ := linear_map.range_eq_top.2 $ λ x, quotient.induction_on' x $ λ x, ⟨of I M x, rfl⟩ variables {I M} @[ext] lemma ext {x y : adic_completion I M} (h : ∀ n, eval I M n x = eval I M n y) : x = y := subtype.eq $ funext h variables (I M) instance : is_Hausdorff I (adic_completion I M) := ⟨λ x hx, ext $ λ n, smul_induction_on (smodeq.zero.1 $ hx n) (λ r hr x _, ((eval I M n).map_smul r x).symm ▸ quotient.induction_on' (eval I M n x) (λ x, smodeq.zero.2 $ smul_mem_smul hr mem_top)) (λ _ _ ih1 ih2, by rw [linear_map.map_add, ih1, ih2, linear_map.map_zero, add_zero])⟩ end adic_completion namespace is_adic_complete instance bot : is_adic_complete (⊥ : ideal R) M := {} protected theorem subsingleton (h : is_adic_complete (⊤ : ideal R) M) : subsingleton M := h.1.subsingleton @[priority 100] instance of_subsingleton [subsingleton M] : is_adic_complete I M := {} open_locale big_operators open finset lemma le_jacobson_bot [is_adic_complete I R] : I ≤ (⊥ : ideal R).jacobson := begin intros x hx, rw [← ideal.neg_mem_iff, ideal.mem_jacobson_bot], intros y, rw add_comm, let f : ℕ → R := λ n, ∑ i in range n, (x * y) ^ i, have hf : ∀ m n, m ≤ n → f m ≡ f n [SMOD I ^ m • (⊤ : submodule R R)], { intros m n h, simp only [f, algebra.id.smul_eq_mul, ideal.mul_top, smodeq.sub_mem], rw [← add_tsub_cancel_of_le h, finset.sum_range_add, ← sub_sub, sub_self, zero_sub, neg_mem_iff], apply submodule.sum_mem, intros n hn, rw [mul_pow, pow_add, mul_assoc], exact ideal.mul_mem_right _ (I ^ m) (ideal.pow_mem_pow hx m) }, obtain ⟨L, hL⟩ := is_precomplete.prec to_is_precomplete hf, { rw is_unit_iff_exists_inv, use L, rw [← sub_eq_zero, neg_mul], apply is_Hausdorff.haus (to_is_Hausdorff : is_Hausdorff I R), intros n, specialize hL n, rw [smodeq.sub_mem, algebra.id.smul_eq_mul, ideal.mul_top] at ⊢ hL, rw sub_zero, suffices : (1 - x * y) * (f n) - 1 ∈ I ^ n, { convert (ideal.sub_mem _ this (ideal.mul_mem_left _ (1 + - (x * y)) hL)) using 1, ring }, cases n, { simp only [ideal.one_eq_top, pow_zero] }, { dsimp [f], rw [← neg_sub _ (1:R), neg_mul, mul_geom_sum, neg_sub, sub_sub, add_comm, ← sub_sub, sub_self, zero_sub, neg_mem_iff, mul_pow], exact ideal.mul_mem_right _ (I ^ _) (ideal.pow_mem_pow hx _), } }, end end is_adic_complete
c254f43874fafeca5f04ee84280cf8c97719b8ca
c777c32c8e484e195053731103c5e52af26a25d1
/src/probability/process/stopping.lean
29832258abf9948431fa357d73a37ac33a49545e
[ "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
50,600
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 probability.process.adapted /-! # Stopping times, stopped processes and stopped values Definition and properties of stopping times. ## Main definitions * `measure_theory.is_stopping_time`: a stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is `f i`-measurable * `measure_theory.is_stopping_time.measurable_space`: the σ-algebra associated with a stopping time ## Main results * `prog_measurable.stopped_process`: the stopped process of a progressively measurable process is progressively measurable. * `mem_ℒp_stopped_process`: if a process belongs to `ℒp` at every time in `ℕ`, then its stopped process belongs to `ℒp` as well. ## Tags stopping time, stochastic process -/ open filter order topological_space open_locale classical measure_theory nnreal ennreal topology big_operators namespace measure_theory variables {Ω β ι : Type*} {m : measurable_space Ω} /-! ### Stopping times -/ /-- A stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is measurable with respect to `f i`. Intuitively, the stopping time `τ` describes some stopping rule such that at time `i`, we may determine it with the information we have at time `i`. -/ def is_stopping_time [preorder ι] (f : filtration ι m) (τ : Ω → ι) := ∀ i : ι, measurable_set[f i] $ {ω | τ ω ≤ i} lemma is_stopping_time_const [preorder ι] (f : filtration ι m) (i : ι) : is_stopping_time f (λ ω, i) := λ j, by simp only [measurable_set.const] section measurable_set section preorder variables [preorder ι] {f : filtration ι m} {τ : Ω → ι} protected lemma is_stopping_time.measurable_set_le (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω ≤ i} := hτ i lemma is_stopping_time.measurable_set_lt_of_pred [pred_order ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin by_cases hi_min : is_min i, { suffices : {ω : Ω | τ ω < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), }, ext1 ω, simp only [set.mem_set_of_eq, set.mem_empty_iff_false, iff_false], rw is_min_iff_forall_not_lt at hi_min, exact hi_min (τ ω), }, have : {ω : Ω | τ ω < i} = τ ⁻¹' (set.Iio i) := rfl, rw [this, ←Iic_pred_of_not_is_min hi_min], exact f.mono (pred_le i) _ (hτ.measurable_set_le $ pred i), end end preorder section countable_stopping_time namespace is_stopping_time variables [partial_order ι] {τ : Ω → ι} {f : filtration ι m} protected lemma measurable_set_eq_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | τ ω = i} := begin have : {ω | τ ω = i} = {ω | τ ω ≤ i} \ (⋃ (j ∈ set.range τ) (hj : j < i), {ω | τ ω ≤ j}), { ext1 a, simp only [set.mem_set_of_eq, set.mem_range, set.Union_exists, set.Union_Union_eq', set.mem_diff, set.mem_Union, exists_prop, not_exists, not_and, not_le], split; intro h, { simp only [h, lt_iff_le_not_le, le_refl, and_imp, imp_self, implies_true_iff, and_self], }, { have h_lt_or_eq : τ a < i ∨ τ a = i := lt_or_eq_of_le h.1, rcases h_lt_or_eq with h_lt | rfl, { exfalso, exact h.2 a h_lt (le_refl (τ a)), }, { refl, }, }, }, rw this, refine (hτ.measurable_set_le i).diff _, refine measurable_set.bUnion h_countable (λ j hj, _), by_cases hji : j < i, { simp only [hji, set.Union_true], exact f.mono hji.le _ (hτ.measurable_set_le j), }, { simp only [hji, set.Union_false], exact @measurable_set.empty _ (f i), }, end protected lemma measurable_set_eq_of_countable [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω = i} := hτ.measurable_set_eq_of_countable_range (set.to_countable _) i protected lemma measurable_set_lt_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp [lt_iff_le_and_ne], }, rw this, exact (hτ.measurable_set_le i).diff (hτ.measurable_set_eq_of_countable_range h_countable i), end protected lemma measurable_set_lt_of_countable [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := hτ.measurable_set_lt_of_countable_range (set.to_countable _) i protected lemma measurable_set_ge_of_countable_range {ι} [linear_order ι] {τ : Ω → ι} {f : filtration ι m} (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_iff, not_lt], }, rw this, exact (hτ.measurable_set_lt_of_countable_range h_countable i).compl, end protected lemma measurable_set_ge_of_countable {ι} [linear_order ι] {τ : Ω → ι} {f : filtration ι m} [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := hτ.measurable_set_ge_of_countable_range (set.to_countable _) i end is_stopping_time end countable_stopping_time section linear_order variables [linear_order ι] {f : filtration ι m} {τ : Ω → ι} lemma is_stopping_time.measurable_set_gt (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i < τ ω} := begin have : {ω | i < τ ω} = {ω | τ ω ≤ i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_iff, not_le], }, rw this, exact (hτ.measurable_set_le i).compl, end section topological_space variables [topological_space ι] [order_topology ι] [first_countable_topology ι] /-- Auxiliary lemma for `is_stopping_time.measurable_set_lt`. -/ lemma is_stopping_time.measurable_set_lt_of_is_lub (hτ : is_stopping_time f τ) (i : ι) (h_lub : is_lub (set.Iio i) i) : measurable_set[f i] {ω | τ ω < i} := begin by_cases hi_min : is_min i, { suffices : {ω | τ ω < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), }, ext1 ω, simp only [set.mem_set_of_eq, set.mem_empty_iff_false, iff_false], exact is_min_iff_forall_not_lt.mp hi_min (τ ω), }, obtain ⟨seq, -, -, h_tendsto, h_bound⟩ : ∃ seq : ℕ → ι, monotone seq ∧ (∀ j, seq j ≤ i) ∧ tendsto seq at_top (𝓝 i) ∧ (∀ j, seq j < i), from h_lub.exists_seq_monotone_tendsto (not_is_min_iff.mp hi_min), have h_Ioi_eq_Union : set.Iio i = ⋃ j, {k | k ≤ seq j}, { ext1 k, simp only [set.mem_Iio, set.mem_Union, set.mem_set_of_eq], refine ⟨λ hk_lt_i, _, λ h_exists_k_le_seq, _⟩, { rw tendsto_at_top' at h_tendsto, have h_nhds : set.Ici k ∈ 𝓝 i, from mem_nhds_iff.mpr ⟨set.Ioi k, set.Ioi_subset_Ici le_rfl, is_open_Ioi, hk_lt_i⟩, obtain ⟨a, ha⟩ : ∃ (a : ℕ), ∀ (b : ℕ), b ≥ a → k ≤ seq b := h_tendsto (set.Ici k) h_nhds, exact ⟨a, ha a le_rfl⟩, }, { obtain ⟨j, hk_seq_j⟩ := h_exists_k_le_seq, exact hk_seq_j.trans_lt (h_bound j), }, }, have h_lt_eq_preimage : {ω | τ ω < i} = τ ⁻¹' (set.Iio i), { ext1 ω, simp only [set.mem_set_of_eq, set.mem_preimage, set.mem_Iio], }, rw [h_lt_eq_preimage, h_Ioi_eq_Union], simp only [set.preimage_Union, set.preimage_set_of_eq], exact measurable_set.Union (λ n, f.mono (h_bound n).le _ (hτ.measurable_set_le (seq n))), end lemma is_stopping_time.measurable_set_lt (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin obtain ⟨i', hi'_lub⟩ : ∃ i', is_lub (set.Iio i) i', from exists_lub_Iio i, cases lub_Iio_eq_self_or_Iio_eq_Iic i hi'_lub with hi'_eq_i h_Iio_eq_Iic, { rw ← hi'_eq_i at hi'_lub ⊢, exact hτ.measurable_set_lt_of_is_lub i' hi'_lub, }, { have h_lt_eq_preimage : {ω : Ω | τ ω < i} = τ ⁻¹' (set.Iio i) := rfl, rw [h_lt_eq_preimage, h_Iio_eq_Iic], exact f.mono (lub_Iio_le i hi'_lub) _ (hτ.measurable_set_le i'), }, end lemma is_stopping_time.measurable_set_ge (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_iff, not_lt], }, rw this, exact (hτ.measurable_set_lt i).compl, end lemma is_stopping_time.measurable_set_eq (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω = i} := begin have : {ω | τ ω = i} = {ω | τ ω ≤ i} ∩ {ω | τ ω ≥ i}, { ext1 ω, simp only [set.mem_set_of_eq, ge_iff_le, set.mem_inter_iff, le_antisymm_iff], }, rw this, exact (hτ.measurable_set_le i).inter (hτ.measurable_set_ge i), end lemma is_stopping_time.measurable_set_eq_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) : measurable_set[f j] {ω | τ ω = i} := f.mono hle _ $ hτ.measurable_set_eq i lemma is_stopping_time.measurable_set_lt_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) : measurable_set[f j] {ω | τ ω < i} := f.mono hle _ $ hτ.measurable_set_lt i end topological_space end linear_order section countable lemma is_stopping_time_of_measurable_set_eq [preorder ι] [countable ι] {f : filtration ι m} {τ : Ω → ι} (hτ : ∀ i, measurable_set[f i] {ω | τ ω = i}) : is_stopping_time f τ := begin intro i, rw show {ω | τ ω ≤ i} = ⋃ k ≤ i, {ω | τ ω = k}, by { ext, simp }, refine measurable_set.bUnion (set.to_countable _) (λ k hk, _), exact f.mono hk _ (hτ k), end end countable end measurable_set namespace is_stopping_time protected lemma max [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (λ ω, max (τ ω) (π ω)) := begin intro i, simp_rw [max_le_iff, set.set_of_and], exact (hτ i).inter (hπ i), end protected lemma max_const [linear_order ι] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) (i : ι) : is_stopping_time f (λ ω, max (τ ω) i) := hτ.max (is_stopping_time_const f i) protected lemma min [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (λ ω, min (τ ω) (π ω)) := begin intro i, simp_rw [min_le_iff, set.set_of_or], exact (hτ i).union (hπ i), end protected lemma min_const [linear_order ι] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) (i : ι) : is_stopping_time f (λ ω, min (τ ω) i) := hτ.min (is_stopping_time_const f i) lemma add_const [add_group ι] [preorder ι] [covariant_class ι ι (function.swap (+)) (≤)] [covariant_class ι ι (+) (≤)] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) {i : ι} (hi : 0 ≤ i) : is_stopping_time f (λ ω, τ ω + i) := begin intro j, simp_rw [← le_sub_iff_add_le], exact f.mono (sub_le_self j hi) _ (hτ (j - i)), end lemma add_const_nat {f : filtration ℕ m} {τ : Ω → ℕ} (hτ : is_stopping_time f τ) {i : ℕ} : is_stopping_time f (λ ω, τ ω + i) := begin refine is_stopping_time_of_measurable_set_eq (λ j, _), by_cases hij : i ≤ j, { simp_rw [eq_comm, ← nat.sub_eq_iff_eq_add hij, eq_comm], exact f.mono (j.sub_le i) _ (hτ.measurable_set_eq (j - i)) }, { rw not_le at hij, convert measurable_set.empty, ext ω, simp only [set.mem_empty_iff_false, iff_false], rintro (hx : τ ω + i = j), linarith }, end -- generalize to certain countable type? lemma add {f : filtration ℕ m} {τ π : Ω → ℕ} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (τ + π) := begin intro i, rw (_ : {ω | (τ + π) ω ≤ i} = ⋃ k ≤ i, {ω | π ω = k} ∩ {ω | τ ω + k ≤ i}), { exact measurable_set.Union (λ k, measurable_set.Union (λ hk, (hπ.measurable_set_eq_le hk).inter (hτ.add_const_nat i))) }, ext ω, simp only [pi.add_apply, set.mem_set_of_eq, set.mem_Union, set.mem_inter_iff, exists_prop], refine ⟨λ h, ⟨π ω, by linarith, rfl, h⟩, _⟩, rintro ⟨j, hj, rfl, h⟩, assumption end section preorder variables [preorder ι] {f : filtration ι m} {τ π : Ω → ι} /-- The associated σ-algebra with a stopping time. -/ protected def measurable_space (hτ : is_stopping_time f τ) : measurable_space Ω := { measurable_set' := λ s, ∀ i : ι, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}), measurable_set_empty := λ i, (set.empty_inter {ω | τ ω ≤ i}).symm ▸ @measurable_set.empty _ (f i), measurable_set_compl := λ s hs i, begin rw (_ : sᶜ ∩ {ω | τ ω ≤ i} = (sᶜ ∪ {ω | τ ω ≤ i}ᶜ) ∩ {ω | τ ω ≤ i}), { refine measurable_set.inter _ _, { rw ← set.compl_inter, exact (hs i).compl }, { exact hτ i} }, { rw set.union_inter_distrib_right, simp only [set.compl_inter_self, set.union_empty] } end, measurable_set_Union := λ s hs i, begin rw forall_swap at hs, rw set.Union_inter, exact measurable_set.Union (hs i), end } protected lemma measurable_set (hτ : is_stopping_time f τ) (s : set Ω) : measurable_set[hτ.measurable_space] s ↔ ∀ i : ι, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) := iff.rfl lemma measurable_space_mono (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (hle : τ ≤ π) : hτ.measurable_space ≤ hπ.measurable_space := begin intros s hs i, rw (_ : s ∩ {ω | π ω ≤ i} = s ∩ {ω | τ ω ≤ i} ∩ {ω | π ω ≤ i}), { exact (hs i).inter (hπ i) }, { ext, simp only [set.mem_inter_iff, iff_self_and, and.congr_left_iff, set.mem_set_of_eq], intros hle' _, exact le_trans (hle _) hle' }, end lemma measurable_space_le_of_countable [countable ι] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, rw (_ : s = ⋃ i, s ∩ {ω | τ ω ≤ i}), { exact measurable_set.Union (λ i, f.le i _ (hs i)) }, { ext ω, split; rw set.mem_Union, { exact λ hx, ⟨τ ω, hx, le_rfl⟩ }, { rintro ⟨_, hx, _⟩, exact hx } } end lemma measurable_space_le' [is_countably_generated (at_top : filter ι)] [(at_top : filter ι).ne_bot] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, obtain ⟨seq : ℕ → ι, h_seq_tendsto⟩ := at_top.exists_seq_tendsto, rw (_ : s = ⋃ n, s ∩ {ω | τ ω ≤ seq n}), { exact measurable_set.Union (λ i, f.le (seq i) _ (hs (seq i))), }, { ext ω, split; rw set.mem_Union, { intros hx, suffices : ∃ i, τ ω ≤ seq i, from ⟨this.some, hx, this.some_spec⟩, rw tendsto_at_top at h_seq_tendsto, exact (h_seq_tendsto (τ ω)).exists, }, { rintro ⟨_, hx, _⟩, exact hx }, }, all_goals { apply_instance, }, end lemma measurable_space_le {ι} [semilattice_sup ι] {f : filtration ι m} {τ : Ω → ι} [is_countably_generated (at_top : filter ι)] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin casesI is_empty_or_nonempty ι, { haveI : is_empty Ω := ⟨λ ω, is_empty.false (τ ω)⟩, intros s hsτ, suffices hs : s = ∅, by { rw hs, exact measurable_set.empty, }, haveI : unique (set Ω) := set.unique_empty, rw [unique.eq_default s, unique.eq_default ∅], }, exact measurable_space_le' hτ, end example {f : filtration ℕ m} {τ : Ω → ℕ} (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := hτ.measurable_space_le example {f : filtration ℝ m} {τ : Ω → ℝ} (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := hτ.measurable_space_le @[simp] lemma measurable_space_const (f : filtration ι m) (i : ι) : (is_stopping_time_const f i).measurable_space = f i := begin ext1 s, change measurable_set[(is_stopping_time_const f i).measurable_space] s ↔ measurable_set[f i] s, rw is_stopping_time.measurable_set, split; intro h, { specialize h i, simpa only [le_refl, set.set_of_true, set.inter_univ] using h, }, { intro j, by_cases hij : i ≤ j, { simp only [hij, set.set_of_true, set.inter_univ], exact f.mono hij _ h, }, { simp only [hij, set.set_of_false, set.inter_empty, measurable_set.empty], }, }, end lemma measurable_set_inter_eq_iff (hτ : is_stopping_time f τ) (s : set Ω) (i : ι) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω = i}) ↔ measurable_set[f i] (s ∩ {ω | τ ω = i}) := begin have : ∀ j, ({ω : Ω | τ ω = i} ∩ {ω : Ω | τ ω ≤ j}) = {ω : Ω | τ ω = i} ∩ {ω | i ≤ j}, { intro j, ext1 ω, simp only [set.mem_inter_iff, set.mem_set_of_eq, and.congr_right_iff], intro hxi, rw hxi, }, split; intro h, { specialize h i, simpa only [set.inter_assoc, this, le_refl, set.set_of_true, set.inter_univ] using h, }, { intro j, rw [set.inter_assoc, this], by_cases hij : i ≤ j, { simp only [hij, set.set_of_true, set.inter_univ], exact f.mono hij _ h, }, { simp [hij], }, }, end lemma measurable_space_le_of_le_const (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : hτ.measurable_space ≤ f i := (measurable_space_mono hτ _ hτ_le).trans (measurable_space_const _ _).le lemma measurable_space_le_of_le (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : hτ.measurable_space ≤ m := (hτ.measurable_space_le_of_le_const hτ_le).trans (f.le n) lemma le_measurable_space_of_const_le (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, i ≤ τ ω) : f i ≤ hτ.measurable_space := (measurable_space_const _ _).symm.le.trans (measurable_space_mono _ hτ hτ_le) end preorder instance sigma_finite_stopping_time {ι} [semilattice_sup ι] [order_bot ι] [(filter.at_top : filter ι).is_countably_generated] {μ : measure Ω} {f : filtration ι m} {τ : Ω → ι} [sigma_finite_filtration μ f] (hτ : is_stopping_time f τ) : sigma_finite (μ.trim hτ.measurable_space_le) := begin refine sigma_finite_trim_mono hτ.measurable_space_le _, { exact f ⊥, }, { exact hτ.le_measurable_space_of_const_le (λ _, bot_le), }, { apply_instance, }, end instance sigma_finite_stopping_time_of_le {ι} [semilattice_sup ι] [order_bot ι] {μ : measure Ω} {f : filtration ι m} {τ : Ω → ι} [sigma_finite_filtration μ f] (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : sigma_finite (μ.trim (hτ.measurable_space_le_of_le hτ_le)) := begin refine sigma_finite_trim_mono (hτ.measurable_space_le_of_le hτ_le) _, { exact f ⊥, }, { exact hτ.le_measurable_space_of_const_le (λ _, bot_le), }, { apply_instance, }, end section linear_order variables [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} protected lemma measurable_set_le' (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω ≤ i} := begin intro j, have : {ω : Ω | τ ω ≤ i} ∩ {ω : Ω | τ ω ≤ j} = {ω : Ω | τ ω ≤ min i j}, { ext1 ω, simp only [set.mem_inter_iff, set.mem_set_of_eq, le_min_iff], }, rw this, exact f.mono (min_le_right i j) _ (hτ _), end protected lemma measurable_set_gt' (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i < τ ω} := begin have : {ω : Ω | i < τ ω} = {ω : Ω | τ ω ≤ i}ᶜ, by { ext1 ω, simp, }, rw this, exact (hτ.measurable_set_le' i).compl, end protected lemma measurable_set_eq' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := begin rw [← set.univ_inter {ω | τ ω = i}, measurable_set_inter_eq_iff, set.univ_inter], exact hτ.measurable_set_eq i, end protected lemma measurable_set_ge' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω}, { ext1 ω, simp only [le_iff_lt_or_eq, set.mem_set_of_eq, set.mem_union], rw [@eq_comm _ i, or_comm], }, rw this, exact (hτ.measurable_set_eq' i).union (hτ.measurable_set_gt' i), end protected lemma measurable_set_lt' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp only [lt_iff_le_and_ne, set.mem_set_of_eq, set.mem_diff], }, rw this, exact (hτ.measurable_set_le' i).diff (hτ.measurable_set_eq' i), end section countable protected lemma measurable_set_eq_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := begin rw [← set.univ_inter {ω | τ ω = i}, measurable_set_inter_eq_iff, set.univ_inter], exact hτ.measurable_set_eq_of_countable_range h_countable i, end protected lemma measurable_set_eq_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := hτ.measurable_set_eq_of_countable_range' (set.to_countable _) i protected lemma measurable_set_ge_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω}, { ext1 ω, simp only [le_iff_lt_or_eq, set.mem_set_of_eq, set.mem_union], rw [@eq_comm _ i, or_comm], }, rw this, exact (hτ.measurable_set_eq_of_countable_range' h_countable i).union (hτ.measurable_set_gt' i), end protected lemma measurable_set_ge_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := hτ.measurable_set_ge_of_countable_range' (set.to_countable _) i protected lemma measurable_set_lt_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp only [lt_iff_le_and_ne, set.mem_set_of_eq, set.mem_diff], }, rw this, exact (hτ.measurable_set_le' i).diff (hτ.measurable_set_eq_of_countable_range' h_countable i), end protected lemma measurable_set_lt_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := hτ.measurable_set_lt_of_countable_range' (set.to_countable _) i protected lemma measurable_space_le_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, rw (_ : s = ⋃ (i ∈ set.range τ), s ∩ {ω | τ ω ≤ i}), { exact measurable_set.bUnion h_countable (λ i _, f.le i _ (hs i)), }, { ext ω, split; rw set.mem_Union, { exact λ hx, ⟨τ ω, by simpa using hx⟩,}, { rintro ⟨i, hx⟩, simp only [set.mem_range, set.Union_exists, set.mem_Union, set.mem_inter_iff, set.mem_set_of_eq, exists_prop, exists_and_distrib_right] at hx, exact hx.1.2, } } end end countable protected lemma measurable [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) : measurable[hτ.measurable_space] τ := @measurable_of_Iic ι Ω _ _ _ hτ.measurable_space _ _ _ _ (λ i, hτ.measurable_set_le' i) protected lemma measurable_of_le [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : measurable[f i] τ := hτ.measurable.mono (measurable_space_le_of_le_const _ hτ_le) le_rfl lemma measurable_space_min (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : (hτ.min hπ).measurable_space = hτ.measurable_space ⊓ hπ.measurable_space := begin refine le_antisymm _ _, { exact le_inf (measurable_space_mono _ hτ (λ _, min_le_left _ _)) (measurable_space_mono _ hπ (λ _, min_le_right _ _)), }, { intro s, change measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s → measurable_set[(hτ.min hπ).measurable_space] s, simp_rw is_stopping_time.measurable_set, have : ∀ i, {ω | min (τ ω) (π ω) ≤ i} = {ω | τ ω ≤ i} ∪ {ω | π ω ≤ i}, { intro i, ext1 ω, simp, }, simp_rw [this, set.inter_union_distrib_left], exact λ h i, (h.left i).union (h.right i), }, end lemma measurable_set_min_iff (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) : measurable_set[(hτ.min hπ).measurable_space] s ↔ measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s := by { rw measurable_space_min, refl, } lemma measurable_space_min_const (hτ : is_stopping_time f τ) {i : ι} : (hτ.min_const i).measurable_space = hτ.measurable_space ⊓ f i := by rw [hτ.measurable_space_min (is_stopping_time_const _ i), measurable_space_const] lemma measurable_set_min_const_iff (hτ : is_stopping_time f τ) (s : set Ω) {i : ι} : measurable_set[(hτ.min_const i).measurable_space] s ↔ measurable_set[hτ.measurable_space] s ∧ measurable_set[f i] s := by rw [measurable_space_min_const, measurable_space.measurable_set_inf] lemma measurable_set_inter_le [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) (hs : measurable_set[hτ.measurable_space] s) : measurable_set[(hτ.min hπ).measurable_space] (s ∩ {ω | τ ω ≤ π ω}) := begin simp_rw is_stopping_time.measurable_set at ⊢ hs, intro i, have : (s ∩ {ω | τ ω ≤ π ω} ∩ {ω | min (τ ω) (π ω) ≤ i}) = (s ∩ {ω | τ ω ≤ i}) ∩ {ω | min (τ ω) (π ω) ≤ i} ∩ {ω | min (τ ω) i ≤ min (min (τ ω) (π ω)) i}, { ext1 ω, simp only [min_le_iff, set.mem_inter_iff, set.mem_set_of_eq, le_min_iff, le_refl, true_and, and_true, true_or, or_true], by_cases hτi : τ ω ≤ i, { simp only [hτi, true_or, and_true, and.congr_right_iff], intro hx, split; intro h, { exact or.inl h, }, { cases h, { exact h, }, { exact hτi.trans h, }, }, }, simp only [hτi, false_or, and_false, false_and, iff_false, not_and, not_le, and_imp], refine λ hx hτ_le_π, lt_of_lt_of_le _ hτ_le_π, rw ← not_le, exact hτi, }, rw this, refine ((hs i).inter ((hτ.min hπ) i)).inter _, apply measurable_set_le, { exact (hτ.min_const i).measurable_of_le (λ _, min_le_right _ _), }, { exact ((hτ.min hπ).min_const i).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_inter_le_iff [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω ≤ π ω}) ↔ measurable_set[(hτ.min hπ).measurable_space] (s ∩ {ω | τ ω ≤ π ω}) := begin split; intro h, { have : s ∩ {ω | τ ω ≤ π ω} = s ∩ {ω | τ ω ≤ π ω} ∩ {ω | τ ω ≤ π ω}, by rw [set.inter_assoc, set.inter_self], rw this, exact measurable_set_inter_le _ _ _ h, }, { rw measurable_set_min_iff at h, exact h.1, }, end lemma measurable_set_inter_le_const_iff (hτ : is_stopping_time f τ) (s : set Ω) (i : ι) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω ≤ i}) ↔ measurable_set[(hτ.min_const i).measurable_space] (s ∩ {ω | τ ω ≤ i}) := begin rw [is_stopping_time.measurable_set_min_iff hτ (is_stopping_time_const _ i), is_stopping_time.measurable_space_const, is_stopping_time.measurable_set], refine ⟨λ h, ⟨h, _⟩, λ h j, h.1 j⟩, specialize h i, rwa [set.inter_assoc, set.inter_self] at h, end lemma measurable_set_le_stopping_time [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω ≤ π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω ≤ π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j ≤ min (π ω) j} ∩ {ω | τ ω ≤ j}, { ext1 ω, simp only [set.mem_inter_iff, set.mem_set_of_eq, min_le_iff, le_min_iff, le_refl, and_true, and.congr_left_iff], intro h, simp only [h, or_self, and_true], by_cases hj : j ≤ π ω, { simp only [hj, h.trans hj, or_self], }, { simp only [hj, or_false], }, }, rw this, refine measurable_set.inter _ (hτ.measurable_set_le j), apply measurable_set_le, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_stopping_time_le [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hπ.measurable_space] {ω | τ ω ≤ π ω} := begin suffices : measurable_set[(hτ.min hπ).measurable_space] {ω : Ω | τ ω ≤ π ω}, by { rw measurable_set_min_iff hτ hπ at this, exact this.2, }, rw [← set.univ_inter {ω : Ω | τ ω ≤ π ω}, ← hτ.measurable_set_inter_le_iff hπ, set.univ_inter], exact measurable_set_le_stopping_time hτ hπ, end lemma measurable_set_eq_stopping_time [add_group ι] [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [measurable_singleton_class ι] [second_countable_topology ι] [has_measurable_sub₂ ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω = π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω = π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j = min (π ω) j} ∩ {ω | τ ω ≤ j} ∩ {ω | π ω ≤ j}, { ext1 ω, simp only [set.mem_inter_iff, set.mem_set_of_eq], refine ⟨λ h, ⟨⟨_, h.2⟩, _⟩, λ h, ⟨_, h.1.2⟩⟩, { rw h.1, }, { rw ← h.1, exact h.2, }, { cases h with h' hσ_le, cases h' with h_eq hτ_le, rwa [min_eq_left hτ_le, min_eq_left hσ_le] at h_eq, }, }, rw this, refine measurable_set.inter (measurable_set.inter _ (hτ.measurable_set_le j)) (hπ.measurable_set_le j), apply measurable_set_eq_fun, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_eq_stopping_time_of_countable [countable ι] [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [measurable_singleton_class ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω = π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω = π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j = min (π ω) j} ∩ {ω | τ ω ≤ j} ∩ {ω | π ω ≤ j}, { ext1 ω, simp only [set.mem_inter_iff, set.mem_set_of_eq], refine ⟨λ h, ⟨⟨_, h.2⟩, _⟩, λ h, ⟨_, h.1.2⟩⟩, { rw h.1, }, { rw ← h.1, exact h.2, }, { cases h with h' hπ_le, cases h' with h_eq hτ_le, rwa [min_eq_left hτ_le, min_eq_left hπ_le] at h_eq, }, }, rw this, refine measurable_set.inter (measurable_set.inter _ (hτ.measurable_set_le j)) (hπ.measurable_set_le j), apply measurable_set_eq_fun_of_countable, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end end linear_order end is_stopping_time section linear_order /-! ## Stopped value and stopped process -/ /-- Given a map `u : ι → Ω → E`, its stopped value with respect to the stopping time `τ` is the map `x ↦ u (τ ω) ω`. -/ def stopped_value (u : ι → Ω → β) (τ : Ω → ι) : Ω → β := λ ω, u (τ ω) ω lemma stopped_value_const (u : ι → Ω → β) (i : ι) : stopped_value u (λ ω, i) = u i := rfl variable [linear_order ι] /-- Given a map `u : ι → Ω → E`, the stopped process with respect to `τ` is `u i ω` if `i ≤ τ ω`, and `u (τ ω) ω` otherwise. Intuitively, the stopped process stops evolving once the stopping time has occured. -/ def stopped_process (u : ι → Ω → β) (τ : Ω → ι) : ι → Ω → β := λ i ω, u (min i (τ ω)) ω lemma stopped_process_eq_stopped_value {u : ι → Ω → β} {τ : Ω → ι} : stopped_process u τ = λ i, stopped_value u (λ ω, min i (τ ω)) := rfl lemma stopped_value_stopped_process {u : ι → Ω → β} {τ σ : Ω → ι} : stopped_value (stopped_process u τ) σ = stopped_value u (λ ω, min (σ ω) (τ ω)) := rfl lemma stopped_process_eq_of_le {u : ι → Ω → β} {τ : Ω → ι} {i : ι} {ω : Ω} (h : i ≤ τ ω) : stopped_process u τ i ω = u i ω := by simp [stopped_process, min_eq_left h] lemma stopped_process_eq_of_ge {u : ι → Ω → β} {τ : Ω → ι} {i : ι} {ω : Ω} (h : τ ω ≤ i) : stopped_process u τ i ω = u (τ ω) ω := by simp [stopped_process, min_eq_right h] section prog_measurable variables [measurable_space ι] [topological_space ι] [order_topology ι] [second_countable_topology ι] [borel_space ι] [topological_space β] {u : ι → Ω → β} {τ : Ω → ι} {f : filtration ι m} lemma prog_measurable_min_stopping_time [metrizable_space ι] (hτ : is_stopping_time f τ) : prog_measurable f (λ i ω, min i (τ ω)) := begin intro i, let m_prod : measurable_space (set.Iic i × Ω) := measurable_space.prod _ (f i), let m_set : ∀ t : set (set.Iic i × Ω), measurable_space t := λ _, @subtype.measurable_space (set.Iic i × Ω) _ m_prod, let s := {p : set.Iic i × Ω | τ p.2 ≤ i}, have hs : measurable_set[m_prod] s, from @measurable_snd (set.Iic i) Ω _ (f i) _ (hτ i), have h_meas_fst : ∀ t : set (set.Iic i × Ω), measurable[m_set t] (λ x : t, ((x : set.Iic i × Ω).fst : ι)), from λ t, (@measurable_subtype_coe (set.Iic i × Ω) m_prod _).fst.subtype_coe, apply measurable.strongly_measurable, refine measurable_of_restrict_of_restrict_compl hs _ _, { refine @measurable.min _ _ _ _ _ (m_set s) _ _ _ _ _ (h_meas_fst s) _, refine @measurable_of_Iic ι s _ _ _ (m_set s) _ _ _ _ (λ j, _), have h_set_eq : (λ x : s, τ (x : set.Iic i × Ω).snd) ⁻¹' set.Iic j = (λ x : s, (x : set.Iic i × Ω).snd) ⁻¹' {ω | τ ω ≤ min i j}, { ext1 ω, simp only [set.mem_preimage, set.mem_Iic, iff_and_self, le_min_iff, set.mem_set_of_eq], exact λ _, ω.prop, }, rw h_set_eq, suffices h_meas : @measurable _ _ (m_set s) (f i) (λ x : s, (x : set.Iic i × Ω).snd), from h_meas (f.mono (min_le_left _ _) _ (hτ.measurable_set_le (min i j))), exact measurable_snd.comp (@measurable_subtype_coe _ m_prod _), }, { suffices h_min_eq_left : (λ x : sᶜ, min ↑((x : set.Iic i × Ω).fst) (τ (x : set.Iic i × Ω).snd)) = λ x : sᶜ, ↑((x : set.Iic i × Ω).fst), { rw [set.restrict, h_min_eq_left], exact h_meas_fst _, }, ext1 ω, rw min_eq_left, have hx_fst_le : ↑(ω : set.Iic i × Ω).fst ≤ i, from (ω : set.Iic i × Ω).fst.prop, refine hx_fst_le.trans (le_of_lt _), convert ω.prop, simp only [not_le, set.mem_compl_iff, set.mem_set_of_eq], }, end lemma prog_measurable.stopped_process [metrizable_space ι] (h : prog_measurable f u) (hτ : is_stopping_time f τ) : prog_measurable f (stopped_process u τ) := h.comp (prog_measurable_min_stopping_time hτ) (λ i x, min_le_left _ _) lemma prog_measurable.adapted_stopped_process [metrizable_space ι] (h : prog_measurable f u) (hτ : is_stopping_time f τ) : adapted f (stopped_process u τ) := (h.stopped_process hτ).adapted lemma prog_measurable.strongly_measurable_stopped_process [metrizable_space ι] (hu : prog_measurable f u) (hτ : is_stopping_time f τ) (i : ι) : strongly_measurable (stopped_process u τ i) := (hu.adapted_stopped_process hτ i).mono (f.le _) lemma strongly_measurable_stopped_value_of_le (h : prog_measurable f u) (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : strongly_measurable[f n] (stopped_value u τ) := begin have : stopped_value u τ = (λ (p : set.Iic n × Ω), u ↑(p.fst) p.snd) ∘ (λ ω, (⟨τ ω, hτ_le ω⟩, ω)), { ext1 ω, simp only [stopped_value, function.comp_app, subtype.coe_mk], }, rw this, refine strongly_measurable.comp_measurable (h n) _, exact (hτ.measurable_of_le hτ_le).subtype_mk.prod_mk measurable_id, end lemma measurable_stopped_value [metrizable_space β] [measurable_space β] [borel_space β] (hf_prog : prog_measurable f u) (hτ : is_stopping_time f τ) : measurable[hτ.measurable_space] (stopped_value u τ) := begin have h_str_meas : ∀ i, strongly_measurable[f i] (stopped_value u (λ ω, min (τ ω) i)), from λ i, strongly_measurable_stopped_value_of_le hf_prog (hτ.min_const i) (λ _, min_le_right _ _), intros t ht i, suffices : stopped_value u τ ⁻¹' t ∩ {ω : Ω | τ ω ≤ i} = stopped_value u (λ ω, min (τ ω) i) ⁻¹' t ∩ {ω : Ω | τ ω ≤ i}, by { rw this, exact ((h_str_meas i).measurable ht).inter (hτ.measurable_set_le i), }, ext1 ω, simp only [stopped_value, set.mem_inter_iff, set.mem_preimage, set.mem_set_of_eq, and.congr_left_iff], intro h, rw min_eq_left h, end end prog_measurable end linear_order section stopped_value_of_mem_finset variables {μ : measure Ω} {τ σ : Ω → ι} {E : Type*} {p : ℝ≥0∞} {u : ι → Ω → E} lemma stopped_value_eq_of_mem_finset [add_comm_monoid E] {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : stopped_value u τ = ∑ i in s, set.indicator {ω | τ ω = i} (u i) := begin ext y, rw [stopped_value, finset.sum_apply, finset.sum_indicator_eq_sum_filter], suffices : finset.filter (λ i, y ∈ {ω : Ω | τ ω = i}) s = ({τ y} : finset ι), by rw [this, finset.sum_singleton], ext1 ω, simp only [set.mem_set_of_eq, finset.mem_filter, finset.mem_singleton], split; intro h, { exact h.2.symm, }, { refine ⟨_, h.symm⟩, rw h, exact hbdd y, }, end lemma stopped_value_eq' [preorder ι] [locally_finite_order_bot ι] [add_comm_monoid E] {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : stopped_value u τ = ∑ i in finset.Iic N, set.indicator {ω | τ ω = i} (u i) := stopped_value_eq_of_mem_finset (λ ω, finset.mem_Iic.mpr (hbdd ω)) lemma stopped_process_eq_of_mem_finset [linear_order ι] [add_comm_monoid E] {s : finset ι} (n : ι) (hbdd : ∀ ω, τ ω < n → τ ω ∈ s) : stopped_process u τ n = set.indicator {a | n ≤ τ a} (u n) + ∑ i in s.filter (< n), set.indicator {ω | τ ω = i} (u i) := begin ext ω, rw [pi.add_apply, finset.sum_apply], cases le_or_lt n (τ ω), { rw [stopped_process_eq_of_le h, set.indicator_of_mem, finset.sum_eq_zero, add_zero], { intros m hm, refine set.indicator_of_not_mem _ _, rw [finset.mem_filter] at hm, exact (hm.2.trans_le h).ne', }, { exact h, } }, { rw [stopped_process_eq_of_ge (le_of_lt h), finset.sum_eq_single_of_mem (τ ω)], { rw [set.indicator_of_not_mem, zero_add, set.indicator_of_mem], { exact rfl }, -- refl does not work { exact not_le.2 h } }, { rw [finset.mem_filter], exact ⟨hbdd ω h, h⟩, }, { intros b hb hneq, rw set.indicator_of_not_mem, exact hneq.symm } }, end lemma stopped_process_eq'' [linear_order ι] [locally_finite_order_bot ι] [add_comm_monoid E] (n : ι) : stopped_process u τ n = set.indicator {a | n ≤ τ a} (u n) + ∑ i in finset.Iio n, set.indicator {ω | τ ω = i} (u i) := begin have h_mem : ∀ ω, τ ω < n → τ ω ∈ finset.Iio n := λ ω h, finset.mem_Iio.mpr h, rw stopped_process_eq_of_mem_finset n h_mem, swap, { apply_instance, }, congr' with i, simp only [finset.Iio_filter_lt, min_eq_right], end section stopped_value variables [partial_order ι] {ℱ : filtration ι m} [normed_add_comm_group E] lemma mem_ℒp_stopped_value_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : mem_ℒp (stopped_value u τ) p μ := begin rw stopped_value_eq_of_mem_finset hbdd, swap, apply_instance, refine mem_ℒp_finset_sum' _ (λ i hi, mem_ℒp.indicator _ (hu i)), refine ℱ.le i {a : Ω | τ a = i} (hτ.measurable_set_eq_of_countable_range _ i), refine ((finset.finite_to_set s).subset (λ ω hω, _)).countable, obtain ⟨y, rfl⟩ := hω, exact hbdd y, end lemma mem_ℒp_stopped_value [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : mem_ℒp (stopped_value u τ) p μ := mem_ℒp_stopped_value_of_mem_finset hτ hu (λ ω, finset.mem_Iic.mpr (hbdd ω)) lemma integrable_stopped_value_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : integrable (stopped_value u τ) μ := begin simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_value_of_mem_finset hτ hu hbdd, end variables (ι) lemma integrable_stopped_value [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : integrable (stopped_value u τ) μ := integrable_stopped_value_of_mem_finset hτ hu (λ ω, finset.mem_Iic.mpr (hbdd ω)) end stopped_value section stopped_process variables [linear_order ι] [topological_space ι] [order_topology ι] [first_countable_topology ι] {ℱ : filtration ι m} [normed_add_comm_group E] lemma mem_ℒp_stopped_process_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) (n : ι) {s : finset ι} (hbdd : ∀ ω, τ ω < n → τ ω ∈ s) : mem_ℒp (stopped_process u τ n) p μ := begin rw stopped_process_eq_of_mem_finset n hbdd, swap, { apply_instance, }, refine mem_ℒp.add _ _, { exact mem_ℒp.indicator (ℱ.le n {a : Ω | n ≤ τ a} (hτ.measurable_set_ge n)) (hu n) }, { suffices : mem_ℒp (λ ω, ∑ i in s.filter (< n), {a : Ω | τ a = i}.indicator (u i) ω) p μ, { convert this, ext1 ω, simp only [finset.sum_apply] }, refine mem_ℒp_finset_sum _ (λ i hi, mem_ℒp.indicator _ (hu i)), exact ℱ.le i {a : Ω | τ a = i} (hτ.measurable_set_eq i) }, end lemma mem_ℒp_stopped_process [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) (n : ι) : mem_ℒp (stopped_process u τ n) p μ := mem_ℒp_stopped_process_of_mem_finset hτ hu n (λ ω h, finset.mem_Iio.mpr h) lemma integrable_stopped_process_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) (n : ι) {s : finset ι} (hbdd : ∀ ω, τ ω < n → τ ω ∈ s) : integrable (stopped_process u τ n) μ := begin simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_process_of_mem_finset hτ hu n hbdd, end lemma integrable_stopped_process [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) (n : ι) : integrable (stopped_process u τ n) μ := integrable_stopped_process_of_mem_finset hτ hu n (λ ω h, finset.mem_Iio.mpr h) end stopped_process end stopped_value_of_mem_finset section adapted_stopped_process variables [topological_space β] [pseudo_metrizable_space β] [linear_order ι] [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] {f : filtration ι m} {u : ι → Ω → β} {τ : Ω → ι} /-- The stopped process of an adapted process with continuous paths is adapted. -/ lemma adapted.stopped_process [metrizable_space ι] (hu : adapted f u) (hu_cont : ∀ ω, continuous (λ i, u i ω)) (hτ : is_stopping_time f τ) : adapted f (stopped_process u τ) := ((hu.prog_measurable_of_continuous hu_cont).stopped_process hτ).adapted /-- If the indexing order has the discrete topology, then the stopped process of an adapted process is adapted. -/ lemma adapted.stopped_process_of_discrete [discrete_topology ι] (hu : adapted f u) (hτ : is_stopping_time f τ) : adapted f (stopped_process u τ) := (hu.prog_measurable_of_discrete.stopped_process hτ).adapted lemma adapted.strongly_measurable_stopped_process [metrizable_space ι] (hu : adapted f u) (hu_cont : ∀ ω, continuous (λ i, u i ω)) (hτ : is_stopping_time f τ) (n : ι) : strongly_measurable (stopped_process u τ n) := (hu.prog_measurable_of_continuous hu_cont).strongly_measurable_stopped_process hτ n lemma adapted.strongly_measurable_stopped_process_of_discrete [discrete_topology ι] (hu : adapted f u) (hτ : is_stopping_time f τ) (n : ι) : strongly_measurable (stopped_process u τ n) := hu.prog_measurable_of_discrete.strongly_measurable_stopped_process hτ n end adapted_stopped_process section nat /-! ### Filtrations indexed by `ℕ` -/ open filtration variables {f : filtration ℕ m} {u : ℕ → Ω → β} {τ π : Ω → ℕ} lemma stopped_value_sub_eq_sum [add_comm_group β] (hle : τ ≤ π) : stopped_value u π - stopped_value u τ = λ ω, (∑ i in finset.Ico (τ ω) (π ω), (u (i + 1) - u i)) ω := begin ext ω, rw [finset.sum_Ico_eq_sub _ (hle ω), finset.sum_range_sub, finset.sum_range_sub], simp [stopped_value], end lemma stopped_value_sub_eq_sum' [add_comm_group β] (hle : τ ≤ π) {N : ℕ} (hbdd : ∀ ω, π ω ≤ N) : stopped_value u π - stopped_value u τ = λ ω, (∑ i in finset.range (N + 1), set.indicator {ω | τ ω ≤ i ∧ i < π ω} (u (i + 1) - u i)) ω := begin rw stopped_value_sub_eq_sum hle, ext ω, simp only [finset.sum_apply, finset.sum_indicator_eq_sum_filter], refine finset.sum_congr _ (λ _ _, rfl), ext i, simp only [finset.mem_filter, set.mem_set_of_eq, finset.mem_range, finset.mem_Ico], exact ⟨λ h, ⟨lt_trans h.2 (nat.lt_succ_iff.2 $ hbdd _), h⟩, λ h, h.2⟩ end section add_comm_monoid variables [add_comm_monoid β] lemma stopped_value_eq {N : ℕ} (hbdd : ∀ ω, τ ω ≤ N) : stopped_value u τ = λ x, (∑ i in finset.range (N + 1), set.indicator {ω | τ ω = i} (u i)) x := stopped_value_eq_of_mem_finset (λ ω, finset.mem_range_succ_iff.mpr (hbdd ω)) lemma stopped_process_eq (n : ℕ) : stopped_process u τ n = set.indicator {a | n ≤ τ a} (u n) + ∑ i in finset.range n, set.indicator {ω | τ ω = i} (u i) := begin rw stopped_process_eq'' n, swap, { apply_instance, }, congr' with i, rw [finset.mem_Iio, finset.mem_range], end lemma stopped_process_eq' (n : ℕ) : stopped_process u τ n = set.indicator {a | n + 1 ≤ τ a} (u n) + ∑ i in finset.range (n + 1), set.indicator {a | τ a = i} (u i) := begin have : {a | n ≤ τ a}.indicator (u n) = {a | n + 1 ≤ τ a}.indicator (u n) + {a | τ a = n}.indicator (u n), { ext x, rw [add_comm, pi.add_apply, ← set.indicator_union_of_not_mem_inter], { simp_rw [@eq_comm _ _ n, @le_iff_eq_or_lt _ _ n, nat.succ_le_iff], refl }, { rintro ⟨h₁, h₂⟩, exact (nat.succ_le_iff.1 h₂).ne h₁.symm } }, rw [stopped_process_eq, this, finset.sum_range_succ_comm, ← add_assoc], end end add_comm_monoid end nat section piecewise_const variables [preorder ι] {𝒢 : filtration ι m} {τ η : Ω → ι} {i j : ι} {s : set Ω} [decidable_pred (∈ s)] /-- Given stopping times `τ` and `η` which are bounded below, `set.piecewise s τ η` is also a stopping time with respect to the same filtration. -/ lemma is_stopping_time.piecewise_of_le (hτ_st : is_stopping_time 𝒢 τ) (hη_st : is_stopping_time 𝒢 η) (hτ : ∀ ω, i ≤ τ ω) (hη : ∀ ω, i ≤ η ω) (hs : measurable_set[𝒢 i] s) : is_stopping_time 𝒢 (s.piecewise τ η) := begin intro n, have : {ω | s.piecewise τ η ω ≤ n} = (s ∩ {ω | τ ω ≤ n}) ∪ (sᶜ ∩ {ω | η ω ≤ n}), { ext1 ω, simp only [set.piecewise, set.mem_inter_iff, set.mem_set_of_eq, and.congr_right_iff], by_cases hx : ω ∈ s; simp [hx], }, rw this, by_cases hin : i ≤ n, { have hs_n : measurable_set[𝒢 n] s, from 𝒢.mono hin _ hs, exact (hs_n.inter (hτ_st n)).union (hs_n.compl.inter (hη_st n)), }, { have hτn : ∀ ω, ¬ τ ω ≤ n := λ ω hτn, hin ((hτ ω).trans hτn), have hηn : ∀ ω, ¬ η ω ≤ n := λ ω hηn, hin ((hη ω).trans hηn), simp [hτn, hηn], }, end lemma is_stopping_time_piecewise_const (hij : i ≤ j) (hs : measurable_set[𝒢 i] s) : is_stopping_time 𝒢 (s.piecewise (λ _, i) (λ _, j)) := (is_stopping_time_const 𝒢 i).piecewise_of_le (is_stopping_time_const 𝒢 j) (λ x, le_rfl) (λ _, hij) hs lemma stopped_value_piecewise_const {ι' : Type*} {i j : ι'} {f : ι' → Ω → ℝ} : stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.piecewise (f i) (f j) := by { ext ω, rw stopped_value, by_cases hx : ω ∈ s; simp [hx] } lemma stopped_value_piecewise_const' {ι' : Type*} {i j : ι'} {f : ι' → Ω → ℝ} : stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.indicator (f i) + sᶜ.indicator (f j) := by { ext ω, rw stopped_value, by_cases hx : ω ∈ s; simp [hx] } end piecewise_const end measure_theory
2ee97054b8c9ec75743a8b5e6261a9694e1c34cc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/workspaceSymbols.lean
f009f616cada41a8f5b04c246ac669e9c38bd496
[ "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
394
lean
import Lean open Lean Elab def orderFuzzyMatches (names : Array Name) (pattern : String) : Array Name := names.filterMap (fun n => (n, ·) <$> Lean.FuzzyMatching.fuzzyMatchScoreWithThreshold? pattern n.toString) |>.qsort (·.2 ≥ ·.2) |>.map (·.1) -- patterns matching at the end of a name should get a bonus #eval orderFuzzyMatches #[`Array.extract, `Lean.extractMainModule] "extract"
93dcc06fca7ec213fa63b293aa9afa11a84ffce6
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Lean/Meta/Tactic/Simp/Rewrite.lean
23a7d1d1e5bf08d794dc88a1176e71db223b510e
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
6,238
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.SynthInstance import Lean.Meta.Tactic.Simp.Types namespace Lean.Meta.Simp def synthesizeArgs (lemmaName : Name) (xs : Array Expr) (bis : Array BinderInfo) (discharge? : Expr → SimpM (Option Expr)) : SimpM Bool := do for x in xs, bi in bis do let type ← inferType x if bi.isInstImplicit then unless (← synthesizeInstance x type) do return false else if (← instantiateMVars x).isMVar then if (← isProp type) then match ← discharge? type with | some proof => unless (← isDefEq x proof) do trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign proof{indentExpr type}" return false | none => trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to discharge hypotheses{indentExpr type}" return false else if (← isClass? type).isSome then unless (← synthesizeInstance x type) do return false return true where synthesizeInstance (x type : Expr) : SimpM Bool := do match ← trySynthInstance type with | LOption.some val => if (← isDefEq x val) then return true else trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to assign instance{indentExpr type}" return false | _ => trace[Meta.Tactic.simp.discharge] "{lemmaName}, failed to synthesize instance{indentExpr type}" return false /- Remark: the parameter tag is used for creating trace messages. It is irrelevant otherwise. -/ def rewrite (e : Expr) (s : DiscrTree SimpLemma) (erased : Std.PHashSet Name) (discharge? : Expr → SimpM (Option Expr)) (tag : String) : SimpM Result := do let lemmas ← s.getMatch e if lemmas.isEmpty then trace[Debug.Meta.Tactic.simp] "no lemmas found for {tag}-rewriting {e}" return { expr := e } else let lemmas := lemmas.insertionSort fun e₁ e₂ => e₁.priority < e₂.priority for lemma in lemmas do unless inErasedSet lemma do if let some result ← tryLemma? lemma then return result return { expr := e } where inErasedSet (lemma : SimpLemma) : Bool := match lemma.name? with | none => false | some name => erased.contains name tryLemma? (lemma : SimpLemma) : SimpM (Option Result) := withNewMCtxDepth do let val ← lemma.getValue let type ← inferType val let (xs, bis, type) ← forallMetaTelescopeReducing type let type ← instantiateMVars type let lhs := type.appFn!.appArg! if (← isDefEq lhs e) then unless (← synthesizeArgs lemma.getName xs bis discharge?) do return none let proof ← instantiateMVars (mkAppN val xs) if ← hasAssignableMVar proof then trace[Meta.Tactic.simp.rewrite] "{lemma}, has unassigned metavariables after unification" return none let rhs ← instantiateMVars type.appArg! if e == rhs then return none if lemma.perm && !Expr.lt rhs e then trace[Meta.Tactic.simp.rewrite] "{lemma}, perm rejected {e} ==> {rhs}" return none trace[Meta.Tactic.simp.rewrite] "{lemma}, {e} ==> {rhs}" return some { expr := rhs, proof? := proof } else unless lhs.isMVar do -- We do not report unification failures when `lhs` is a metavariable -- Example: `x = ()` -- TODO: reconsider if we want lemmas such as `(x : Unit) → x = ()` trace[Meta.Tactic.simp.unify] "{lemma}, failed to unify {lhs} with {e}" return none def rewriteCtorEq? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do match e.eq? with | none => return none | some (_, lhs, rhs) => let lhs ← whnf lhs let rhs ← whnf rhs let env ← getEnv match lhs.constructorApp? env, rhs.constructorApp? env with | some (c₁, _), some (c₂, _) => if c₁.name != c₂.name then withLocalDeclD `h e fun h => return some { expr := mkConst ``False, proof? := (← mkEqFalse' (← mkLambdaFVars #[h] (← mkNoConfusion (mkConst ``False) h))) } else return none | _, _ => return none @[inline] def tryRewriteCtorEq (e : Expr) (x : SimpM Step) : SimpM Step := do match (← rewriteCtorEq? e) with | some r => return Step.done r | none => x def rewriteUsingDecide? (e : Expr) : MetaM (Option Result) := withReducibleAndInstances do if e.hasFVar || e.hasMVar || e.isConstOf ``True || e.isConstOf ``False then return none else try let d ← mkDecide e let r ← withDefault <| whnf d if r.isConstOf ``true then return some { expr := mkConst ``True, proof? := mkAppN (mkConst ``eqTrueOfDecide) #[e, d.appArg!, (← mkEqRefl (mkConst ``true))] } else if r.isConstOf ``false then let h ← mkEqRefl d return some { expr := mkConst ``False, proof? := mkAppN (mkConst ``eqFalseOfDecide) #[e, d.appArg!, (← mkEqRefl (mkConst ``false))] } else return none catch _ => return none @[inline] def tryRewriteUsingDecide (e : Expr) (x : SimpM Step) : SimpM Step := do if (← read).config.decide then match (← rewriteUsingDecide? e) with | some r => return Step.done r | none => x else x def rewritePre (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do let lemmas ← (← read).simpLemmas return Step.visit (← rewrite e lemmas.pre lemmas.erased discharge? (tag := "pre")) def rewritePost (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do let lemmas ← (← read).simpLemmas return Step.visit (← rewrite e lemmas.post lemmas.erased discharge? (tag := "post")) def preDefault (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := tryRewriteCtorEq e <| rewritePre e discharge? def postDefault (e : Expr) (discharge? : Expr → SimpM (Option Expr)) : SimpM Step := do -- TODO: try equation lemmas tryRewriteCtorEq e <| tryRewriteUsingDecide e <| rewritePost e discharge? end Lean.Meta.Simp
898e25e1eada4327f5ab975cbb7ddfcc8382e603
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/order/hom/esakia.lean
1ae1c28310069dda48f50b45602d5758f2811761
[ "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
11,598
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.hom.bounded import topology.order.hom.basic /-! # Esakia morphisms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines pseudo-epimorphisms and Esakia morphisms. We use the `fun_like` design, so each type of morphisms has a companion typeclass which is meant to be satisfied by itself and all stricter types. ## Types of morphisms * `pseudo_epimorphism`: Pseudo-epimorphisms. Maps `f` such that `f a ≤ b` implies the existence of `a'` such that `a ≤ a'` and `f a' = b`. * `esakia_hom`: Esakia morphisms. Continuous pseudo-epimorphisms. ## Typeclasses * `pseudo_epimorphism_class` * `esakia_hom_class` ## References * [Wikipedia, *Esakia space*](https://en.wikipedia.org/wiki/Esakia_space) -/ open function variables {F α β γ δ : Type*} /-- The type of pseudo-epimorphisms, aka p-morphisms, aka bounded maps, from `α` to `β`. -/ structure pseudo_epimorphism (α β : Type*) [preorder α] [preorder β] extends α →o β := (exists_map_eq_of_map_le' ⦃a : α⦄ ⦃b : β⦄ : to_fun a ≤ b → ∃ c, a ≤ c ∧ to_fun c = b) /-- The type of Esakia morphisms, aka continuous pseudo-epimorphisms, from `α` to `β`. -/ structure esakia_hom (α β : Type*) [topological_space α] [preorder α] [topological_space β] [preorder β] extends α →Co β := (exists_map_eq_of_map_le' ⦃a : α⦄ ⦃b : β⦄ : to_fun a ≤ b → ∃ c, a ≤ c ∧ to_fun c = b) section set_option old_structure_cmd true /-- `pseudo_epimorphism_class F α β` states that `F` is a type of `⊔`-preserving morphisms. You should extend this class when you extend `pseudo_epimorphism`. -/ class pseudo_epimorphism_class (F : Type*) (α β : out_param $ Type*) [preorder α] [preorder β] extends rel_hom_class F ((≤) : α → α → Prop) ((≤) : β → β → Prop) := (exists_map_eq_of_map_le (f : F) ⦃a : α⦄ ⦃b : β⦄ : f a ≤ b → ∃ c, a ≤ c ∧ f c = b) /-- `esakia_hom_class F α β` states that `F` is a type of lattice morphisms. You should extend this class when you extend `esakia_hom`. -/ class esakia_hom_class (F : Type*) (α β : out_param $ Type*) [topological_space α] [preorder α] [topological_space β] [preorder β] extends continuous_order_hom_class F α β := (exists_map_eq_of_map_le (f : F) ⦃a : α⦄ ⦃b : β⦄ : f a ≤ b → ∃ c, a ≤ c ∧ f c = b) end export pseudo_epimorphism_class (exists_map_eq_of_map_le) @[priority 100] -- See note [lower instance priority] instance pseudo_epimorphism_class.to_top_hom_class [partial_order α] [order_top α] [preorder β] [order_top β] [pseudo_epimorphism_class F α β] : top_hom_class F α β := { map_top := λ f, let ⟨b, h⟩ := exists_map_eq_of_map_le f (@le_top _ _ _ $ f ⊤) in by rw [←top_le_iff.1 h.1, h.2] .. ‹pseudo_epimorphism_class F α β› } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_pseudo_epimorphism_class [preorder α] [preorder β] [order_iso_class F α β] : pseudo_epimorphism_class F α β := { exists_map_eq_of_map_le := λ f a b h, ⟨equiv_like.inv f b, (le_map_inv_iff f).2 h, equiv_like.right_inv _ _⟩, .. order_iso_class.to_order_hom_class } @[priority 100] -- See note [lower instance priority] instance esakia_hom_class.to_pseudo_epimorphism_class [topological_space α] [preorder α] [topological_space β] [preorder β] [esakia_hom_class F α β] : pseudo_epimorphism_class F α β := { .. ‹esakia_hom_class F α β› } instance [preorder α] [preorder β] [pseudo_epimorphism_class F α β] : has_coe_t F (pseudo_epimorphism α β) := ⟨λ f, ⟨f, exists_map_eq_of_map_le f⟩⟩ instance [topological_space α] [preorder α] [topological_space β] [preorder β] [esakia_hom_class F α β] : has_coe_t F (esakia_hom α β) := ⟨λ f, ⟨f, exists_map_eq_of_map_le f⟩⟩ /-! ### Pseudo-epimorphisms -/ namespace pseudo_epimorphism variables [preorder α] [preorder β] [preorder γ] [preorder δ] instance : pseudo_epimorphism_class (pseudo_epimorphism α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_rel := λ f, f.monotone', exists_map_eq_of_map_le := pseudo_epimorphism.exists_map_eq_of_map_le' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (pseudo_epimorphism α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : pseudo_epimorphism α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : pseudo_epimorphism α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `pseudo_epimorphism` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : pseudo_epimorphism α β) (f' : α → β) (h : f' = f) : pseudo_epimorphism α β := ⟨f.to_order_hom.copy f' h, by simpa only [h.symm, to_fun_eq_coe] using f.exists_map_eq_of_map_le'⟩ @[simp] lemma coe_copy (f : pseudo_epimorphism α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : pseudo_epimorphism α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `pseudo_epimorphism`. -/ protected def id : pseudo_epimorphism α α := ⟨order_hom.id, λ a b h, ⟨b, h, rfl⟩⟩ instance : inhabited (pseudo_epimorphism α α) := ⟨pseudo_epimorphism.id α⟩ @[simp] lemma coe_id : ⇑(pseudo_epimorphism.id α) = id := rfl @[simp] lemma coe_id_order_hom : (pseudo_epimorphism.id α : α →o α) = order_hom.id := rfl variables {α} @[simp] lemma id_apply (a : α) : pseudo_epimorphism.id α a = a := rfl /-- Composition of `pseudo_epimorphism`s as a `pseudo_epimorphism`. -/ def comp (g : pseudo_epimorphism β γ) (f : pseudo_epimorphism α β) : pseudo_epimorphism α γ := ⟨g.to_order_hom.comp f.to_order_hom, λ a b h₀, begin obtain ⟨b, h₁, rfl⟩ := g.exists_map_eq_of_map_le' h₀, obtain ⟨b, h₂, rfl⟩ := f.exists_map_eq_of_map_le' h₁, exact ⟨b, h₂, rfl⟩, end⟩ @[simp] lemma coe_comp (g : pseudo_epimorphism β γ) (f : pseudo_epimorphism α β) : (g.comp f : α → γ) = g ∘ f := rfl @[simp] lemma coe_comp_order_hom (g : pseudo_epimorphism β γ) (f : pseudo_epimorphism α β) : (g.comp f : α →o γ) = (g : β →o γ).comp f := rfl @[simp] lemma comp_apply (g : pseudo_epimorphism β γ) (f : pseudo_epimorphism α β) (a : α) : (g.comp f) a = g (f a) := rfl @[simp] lemma comp_assoc (h : pseudo_epimorphism γ δ) (g : pseudo_epimorphism β γ) (f : pseudo_epimorphism α β) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma comp_id (f : pseudo_epimorphism α β) : f.comp (pseudo_epimorphism.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : pseudo_epimorphism α β) : (pseudo_epimorphism.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : pseudo_epimorphism β γ} {f : pseudo_epimorphism α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : pseudo_epimorphism β γ} {f₁ f₂ : pseudo_epimorphism α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end pseudo_epimorphism /-! ### Esakia morphisms -/ namespace esakia_hom variables [topological_space α] [preorder α] [topological_space β] [preorder β] [topological_space γ] [preorder γ] [topological_space δ] [preorder δ] /-- Reinterpret an `esakia_hom` as a `pseudo_epimorphism`. -/ def to_pseudo_epimorphism (f : esakia_hom α β) : pseudo_epimorphism α β := { ..f } instance : esakia_hom_class (esakia_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := f, obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := g, congr' }, map_rel := λ f, f.monotone', map_continuous := λ f, f.continuous_to_fun, exists_map_eq_of_map_le := λ f, f.exists_map_eq_of_map_le' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (esakia_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : esakia_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : esakia_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of an `esakia_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : esakia_hom α β) (f' : α → β) (h : f' = f) : esakia_hom α β := ⟨f.to_continuous_order_hom.copy f' h, by simpa only [h.symm, to_fun_eq_coe] using f.exists_map_eq_of_map_le'⟩ @[simp] lemma coe_copy (f : esakia_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : esakia_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as an `esakia_hom`. -/ protected def id : esakia_hom α α := ⟨continuous_order_hom.id α, λ a b h, ⟨b, h, rfl⟩⟩ instance : inhabited (esakia_hom α α) := ⟨esakia_hom.id α⟩ @[simp] lemma coe_id : ⇑(esakia_hom.id α) = id := rfl @[simp] lemma coe_id_continuous_order_hom : (esakia_hom.id α : α →Co α) = continuous_order_hom.id α := rfl @[simp] lemma coe_id_pseudo_epimorphism : (esakia_hom.id α : pseudo_epimorphism α α) = pseudo_epimorphism.id α := rfl variables {α} @[simp] lemma id_apply (a : α) : esakia_hom.id α a = a := rfl /-- Composition of `esakia_hom`s as an `esakia_hom`. -/ def comp (g : esakia_hom β γ) (f : esakia_hom α β) : esakia_hom α γ := ⟨g.to_continuous_order_hom.comp f.to_continuous_order_hom, λ a b h₀, begin obtain ⟨b, h₁, rfl⟩ := g.exists_map_eq_of_map_le' h₀, obtain ⟨b, h₂, rfl⟩ := f.exists_map_eq_of_map_le' h₁, exact ⟨b, h₂, rfl⟩, end⟩ @[simp] lemma coe_comp (g : esakia_hom β γ) (f : esakia_hom α β) : (g.comp f : α → γ) = g ∘ f := rfl @[simp] lemma comp_apply (g : esakia_hom β γ) (f : esakia_hom α β) (a : α) : (g.comp f) a = g (f a) := rfl @[simp] lemma coe_comp_continuous_order_hom (g : esakia_hom β γ) (f : esakia_hom α β) : (g.comp f : α →Co γ) = (g : β →Co γ).comp f := rfl @[simp] lemma coe_comp_pseudo_epimorphism (g : esakia_hom β γ) (f : esakia_hom α β) : (g.comp f : pseudo_epimorphism α γ) = (g : pseudo_epimorphism β γ).comp f := rfl @[simp] lemma comp_assoc (h : esakia_hom γ δ) (g : esakia_hom β γ) (f : esakia_hom α β) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma comp_id (f : esakia_hom α β) : f.comp (esakia_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : esakia_hom α β) : (esakia_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : esakia_hom β γ} {f : esakia_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : esakia_hom β γ} {f₁ f₂ : esakia_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end esakia_hom
cdecb3f067e2c2ea141c42ac62cc3f866fc791bb
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/ordering/basic.lean
9d944c0079cce9c1aed3b32788dd954a4d6e2938
[ "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
1,684
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.repr init.data.prod init.data.sum.basic universes u v inductive ordering | lt | eq | gt instance : has_repr ordering := ⟨(λ s, match s with | ordering.lt := "lt" | ordering.eq := "eq" | ordering.gt := "gt" end)⟩ namespace ordering def swap : ordering → ordering | lt := gt | eq := eq | gt := lt @[inline] def or_else : ordering → ordering → ordering | lt _ := lt | eq o := o | gt _ := gt theorem swap_swap : ∀ (o : ordering), o.swap.swap = o | lt := rfl | eq := rfl | gt := rfl end ordering def cmp_using {α : Type u} (lt : α → α → Prop) [decidable_rel lt] (a b : α) : ordering := if lt a b then ordering.lt else if lt b a then ordering.gt else ordering.eq def cmp {α : Type u} [has_lt α] [decidable_rel ((<) : α → α → Prop)] (a b : α) : ordering := cmp_using (<) a b instance : decidable_eq ordering := λ a b, match a with | ordering.lt := match b with | ordering.lt := is_true rfl | ordering.eq := is_false (λ h, ordering.no_confusion h) | ordering.gt := is_false (λ h, ordering.no_confusion h) end | ordering.eq := match b with | ordering.lt := is_false (λ h, ordering.no_confusion h) | ordering.eq := is_true rfl | ordering.gt := is_false (λ h, ordering.no_confusion h) end | ordering.gt := match b with | ordering.lt := is_false (λ h, ordering.no_confusion h) | ordering.eq := is_false (λ h, ordering.no_confusion h) | ordering.gt := is_true rfl end end
721e1795c1e3b88cd276a4cf778135d8c6178399
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/abelian/non_preadditive.lean
58ad340b354b26dbe77d69c72060535d14986446
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
33,207
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.kernels import category_theory.limits.shapes.pullbacks import category_theory.limits.shapes.regular_mono import category_theory.preadditive /-! # Every non_preadditive_abelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphis is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `σ : A ⨯ A ⟶ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ σ`. 4. Prove a small number of identities about this subtraction from the definition of `σ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. What's even more impressive is that all additive structures on a category are in some sense isomorphic, so for abelian categories with a natural preadditive structure, this construction manages to "almost" reconstruct this natural structure. However, we have not formalized this isomorphism. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ open category_theory open category_theory.limits namespace category_theory section universes v u variables (C : Type u) [category.{v} C] /-- We call a category `non_preadditive_abelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class non_preadditive_abelian := [has_zero_object : has_zero_object C] [has_zero_morphisms : has_zero_morphisms C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] [has_finite_products : has_finite_products C] [has_finite_coproducts : has_finite_coproducts C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) set_option default_priority 100 attribute [instance] non_preadditive_abelian.has_zero_object attribute [instance] non_preadditive_abelian.has_zero_morphisms attribute [instance] non_preadditive_abelian.has_kernels attribute [instance] non_preadditive_abelian.has_cokernels attribute [instance] non_preadditive_abelian.has_finite_products attribute [instance] non_preadditive_abelian.has_finite_coproducts end end category_theory open category_theory namespace category_theory.non_preadditive_abelian universes v u variables {C : Type u} [category.{v} C] section variables [non_preadditive_abelian C] section strong local attribute [instance] non_preadditive_abelian.normal_epi /-- In a `non_preadditive_abelian` category, every epimorphism is strong. -/ def strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In a `non_preadditive_abelian` category, a monomorphism which is also an epimorphism is an isomorphism. -/ def is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso /-- The pullback of two monomorphisms exists. -/ @[irreducible] def pullback_of_mono {X Y Z : C} (a : X ⟶ Z) (b : Y ⟶ Z) [mono a] [mono b] : has_limit (cospan a b) := let ⟨P, f, haf, i⟩ := non_preadditive_abelian.normal_mono a in let ⟨Q, g, hbg, i'⟩ := non_preadditive_abelian.normal_mono b in let ⟨a', ha'⟩ := kernel_fork.is_limit.lift' i (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ f = kernel.ι (prod.lift f g) ≫ prod.lift f g ≫ limits.prod.fst : by rw prod.lift_fst ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.fst : by rw kernel.condition_assoc ... = 0 : has_zero_morphisms.zero_comp _ _ in let ⟨b', hb'⟩ := kernel_fork.is_limit.lift' i' (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ g = kernel.ι (prod.lift f g) ≫ (prod.lift f g) ≫ limits.prod.snd : by rw prod.lift_snd ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.snd : by rw kernel.condition_assoc ... = 0 : has_zero_morphisms.zero_comp _ _ in { cone := pullback_cone.mk a' b' $ by { simp at ha' hb', rw [ha', hb'] }, is_limit := pullback_cone.is_limit.mk _ _ _ (λ s, kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) $ prod.hom_ext (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.fst = pullback_cone.snd s ≫ b ≫ f : by simp only [prod.lift_fst, category.assoc] ... = pullback_cone.fst s ≫ a ≫ f : by rw pullback_cone.condition_assoc ... = pullback_cone.fst s ≫ 0 : by rw haf ... = 0 ≫ limits.prod.fst : by rw [has_zero_morphisms.comp_zero, has_zero_morphisms.zero_comp]) (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.snd = pullback_cone.snd s ≫ b ≫ g : by simp only [prod.lift_snd, category.assoc] ... = pullback_cone.snd s ≫ 0 : by rw hbg ... = 0 ≫ limits.prod.snd : by rw [has_zero_morphisms.comp_zero, has_zero_morphisms.zero_comp])) (λ s, (cancel_mono a).1 $ by { rw kernel_fork.ι_of_ι at ha', simp [ha', pullback_cone.condition s] }) (λ s, (cancel_mono b).1 $ by { rw kernel_fork.ι_of_ι at hb', simp [hb'] }) (λ s m h₁ h₂, (cancel_mono (kernel.ι (prod.lift f g))).1 $ calc m ≫ kernel.ι (prod.lift f g) = m ≫ a' ≫ a : by { congr, exact ha'.symm } ... = pullback_cone.fst s ≫ a : by rw [←category.assoc, h₁] ... = pullback_cone.snd s ≫ b : pullback_cone.condition s ... = kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) _ ≫ kernel.ι (prod.lift f g) : by rw kernel.lift_ι) } /-- The pushout of two epimorphisms exists. -/ @[irreducible] def pushout_of_epi {X Y Z : C} (a : X ⟶ Y) (b : X ⟶ Z) [epi a] [epi b] : has_colimit (span a b) := let ⟨P, f, hfa, i⟩ := non_preadditive_abelian.normal_epi a in let ⟨Q, g, hgb, i'⟩ := non_preadditive_abelian.normal_epi b in let ⟨a', ha'⟩ := cokernel_cofork.is_colimit.desc' i (cokernel.π (coprod.desc f g)) $ calc f ≫ cokernel.π (coprod.desc f g) = coprod.inl ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inl_desc_assoc ... = coprod.inl ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in let ⟨b', hb'⟩ := cokernel_cofork.is_colimit.desc' i' (cokernel.π (coprod.desc f g)) $ calc g ≫ cokernel.π (coprod.desc f g) = coprod.inr ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inr_desc_assoc ... = coprod.inr ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in { cocone := pushout_cocone.mk a' b' $ by { simp only [cofork.π_of_π] at ha' hb', rw [ha', hb'] }, is_colimit := pushout_cocone.is_colimit.mk _ _ _ (λ s, cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) $ coprod.hom_ext (calc coprod.inl ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = f ≫ b ≫ pushout_cocone.inr s : by rw coprod.inl_desc_assoc ... = f ≫ a ≫ pushout_cocone.inl s : by rw pushout_cocone.condition ... = 0 ≫ pushout_cocone.inl s : by rw reassoc_of hfa ... = coprod.inl ≫ 0 : by rw [has_zero_morphisms.comp_zero, has_zero_morphisms.zero_comp]) (calc coprod.inr ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = g ≫ b ≫ pushout_cocone.inr s : by rw coprod.inr_desc_assoc ... = 0 ≫ pushout_cocone.inr s : by rw reassoc_of hgb ... = coprod.inr ≫ 0 : by rw [has_zero_morphisms.comp_zero, has_zero_morphisms.zero_comp])) (λ s, (cancel_epi a).1 $ by { rw cokernel_cofork.π_of_π at ha', simp [reassoc_of ha', pushout_cocone.condition s] }) (λ s, (cancel_epi b).1 $ by { rw cokernel_cofork.π_of_π at hb', simp [reassoc_of hb'] }) (λ s m h₁ h₂, (cancel_epi (cokernel.π (coprod.desc f g))).1 $ calc cokernel.π (coprod.desc f g) ≫ m = (a ≫ a') ≫ m : by { congr, exact ha'.symm } ... = a ≫ pushout_cocone.inl s : by rw [category.assoc, h₁] ... = b ≫ pushout_cocone.inr s : pushout_cocone.condition s ... = cokernel.π (coprod.desc f g) ≫ cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) _ : by rw cokernel.π_desc) } section local attribute [instance] pullback_of_mono /-- The pullback of `(𝟙 X, f)` and `(𝟙 X, g)` -/ private abbreviation P {X Y : C} (f g : X ⟶ Y) [mono (prod.lift (𝟙 X) f)] [mono (prod.lift (𝟙 X) g)] : C := pullback (prod.lift (𝟙 X) f) (prod.lift (𝟙 X) g) /-- The equalizer of `f` and `g` exists. -/ @[irreducible] def has_limit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_limit (parallel_pair f g) := have h1f : mono (prod.lift (𝟙 X) f), from mono_of_mono_fac $ prod.lift_fst (𝟙 X) f, have h1g : mono (prod.lift (𝟙 X) g), from mono_of_mono_fac $ prod.lift_fst (𝟙 X) g, have huv : (pullback.fst : P f g ⟶ X) = pullback.snd, from calc (pullback.fst : P f g ⟶ X) = pullback.fst ≫ 𝟙 _ : eq.symm $ category.comp_id _ ... = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.fst : by rw prod.lift_fst ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.fst : by rw pullback.condition_assoc ... = pullback.snd : by rw [prod.lift_fst, category.comp_id], have hvu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.snd ≫ g, from calc (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.snd : by rw prod.lift_snd ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.snd : by rw pullback.condition_assoc ... = pullback.snd ≫ g : by rw prod.lift_snd, have huu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ g, by rw [hvu, ←huv], { cone := fork.of_ι pullback.fst huu, is_limit := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s) (fork.ι s) $ prod.hom_ext (by simp only [prod.lift_fst, category.assoc]) (by simp only [fork.app_zero_right, fork.app_zero_left, prod.lift_snd, category.assoc])) (λ s, by simp only [fork.ι_of_ι, pullback.lift_fst]) (λ s m h, pullback.hom_ext (by simpa only [pullback.lift_fst] using h walking_parallel_pair.zero) (by simpa only [huv.symm, pullback.lift_fst] using h walking_parallel_pair.zero)) } end section local attribute [instance] pushout_of_epi /-- The pushout of `(𝟙 Y, f)` and `(𝟙 Y, g)`. -/ private abbreviation Q {X Y : C} (f g : X ⟶ Y) [epi (coprod.desc (𝟙 Y) f)] [epi (coprod.desc (𝟙 Y) g)] : C := pushout (coprod.desc (𝟙 Y) f) (coprod.desc (𝟙 Y) g) /-- The coequalizer of `f` and `g` exists. -/ @[irreducible] def has_colimit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_colimit (parallel_pair f g) := have h1f : epi (coprod.desc (𝟙 Y) f), from epi_of_epi_fac $ coprod.inl_desc _ _, have h1g : epi (coprod.desc (𝟙 Y) g), from epi_of_epi_fac $ coprod.inl_desc _ _, have huv : (pushout.inl : Y ⟶ Q f g) = pushout.inr, from calc (pushout.inl : Y ⟶ Q f g) = 𝟙 _ ≫ pushout.inl : eq.symm $ category.id_comp _ ... = (coprod.inl ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inl_desc ... = (coprod.inl ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = pushout.inr : by rw [coprod.inl_desc, category.id_comp], have hvu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inr, from calc f ≫ (pushout.inl : Y ⟶ Q f g) = (coprod.inr ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inr_desc ... = (coprod.inr ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = g ≫ pushout.inr : by rw coprod.inr_desc, have huu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inl, by rw [hvu, huv], { cocone := cofork.of_π pushout.inl huu, is_colimit := cofork.is_colimit.mk _ (λ s, pushout.desc (cofork.π s) (cofork.π s) $ coprod.hom_ext (by simp only [coprod.inl_desc_assoc]) (by simp only [cofork.right_app_one, coprod.inr_desc_assoc, cofork.left_app_one])) (λ s, by simp only [pushout.inl_desc, cofork.π_of_π]) (λ s m h, pushout.hom_ext (by simpa only [pushout.inl_desc] using h walking_parallel_pair.one) (by simpa only [huv.symm, pushout.inl_desc] using h walking_parallel_pair.one)) } end section local attribute [instance] has_limit_parallel_pair /-- A `non_preadditive_abelian` category has all equalizers. -/ @[priority 100] instance has_equalizers : has_equalizers C := has_equalizers_of_has_limit_parallel_pair _ end section local attribute [instance] has_colimit_parallel_pair /-- A `non_preadditive_abelian` category has all coequalizers. -/ @[priority 100] instance has_coequalizers : has_coequalizers C := has_coequalizers_of_has_colimit_parallel_pair _ end section /-- If a zero morphism is a kernel of `f`, then `f` is a monomorphism. -/ lemma mono_of_zero_kernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_limit (kernel_fork.of_ι (0 : Z ⟶ X) (show 0 ≫ f = 0, by simp))) : mono f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_epi (coequalizer.π u v), obtain ⟨m, hm⟩ := coequalizer.desc' f huv, have hwf : w ≫ f = 0, { rw [←hm, reassoc_of hw, has_zero_morphisms.zero_comp] }, obtain ⟨n, hn⟩ := kernel_fork.is_limit.lift' l _ hwf, rw [fork.ι_of_ι, has_zero_morphisms.comp_zero] at hn, haveI : is_iso (coequalizer.π u v) := by apply is_iso_colimit_cocone_parallel_pair_of_eq hn.symm hl, apply (cancel_mono (coequalizer.π u v)).1, exact coequalizer.condition _ _ end⟩ /-- If a zero morphism is a cokernel of `f`, then `f` is an epimorphism. -/ lemma epi_of_zero_cokernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ Z) (show f ≫ 0 = 0, by simp))) : epi f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_mono (equalizer.ι u v), obtain ⟨m, hm⟩ := equalizer.lift' f huv, have hwf : f ≫ w = 0, { rw [←hm, category.assoc, hw, has_zero_morphisms.comp_zero] }, obtain ⟨n, hn⟩ := cokernel_cofork.is_colimit.desc' l _ hwf, rw [cofork.π_of_π, has_zero_morphisms.zero_comp] at hn, haveI : is_iso (equalizer.ι u v) := by apply is_iso_limit_cone_parallel_pair_of_eq hn.symm hl, apply (cancel_epi (equalizer.ι u v)).1, exact equalizer.condition _ _ end⟩ local attribute [instance] has_zero_object.has_zero /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `0 : 0 ⟶ X` is a kernel of `f`. -/ def zero_kernel_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : is_limit (kernel_fork.of_ι (0 : 0 ⟶ X) (show 0 ≫ f = 0, by simp)) := fork.is_limit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (kernel_fork.condition s), has_zero_morphisms.zero_comp]) (λ s m h, by ext) /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `0 : Y ⟶ 0` is a cokernel of `f`. -/ def zero_cokernel_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ 0) (show f ≫ 0 = 0, by simp)) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (cokernel_cofork.condition s), has_zero_morphisms.comp_zero]) (λ s m h, by ext) /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `f` is a monomorphism. -/ lemma mono_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : mono f := mono_of_zero_kernel f 0 $ zero_kernel_of_cancel_zero f hf /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `g` is a monomorphism. -/ lemma epi_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : epi f := epi_of_zero_cokernel f 0 $ zero_cokernel_of_zero_cancel f hf end section factor variables {P Q : C} (f : P ⟶ Q) /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : non_preadditive_abelian.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ non_preadditive_abelian.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : non_preadditive_abelian.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (non_preadditive_abelian.factor_thru_image f) := let I := non_preadditive_abelian.image f, p := non_preadditive_abelian.factor_thru_image f, i := kernel.ι (cokernel.π f) in -- It will suffice to consider some g : I ⟶ R such that p ≫ g = 0 and show that g = 0. epi_of_zero_cancel _ $ λ R (g : I ⟶ R) (hpg : p ≫ g = 0), begin -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ι g ≫ i, haveI : mono u := mono_comp _ _, haveI hu := non_preadditive_abelian.normal_mono u, let h := hu.g, -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg, have fh : f ≫ h = 0, calc f ≫ h = (p ≫ i) ≫ h : (image.fac f).symm ▸ rfl ... = ((t ≫ kernel.ι g) ≫ i) ≫ h : ht ▸ rfl ... = t ≫ u ≫ h : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = t ≫ 0 : hu.w ▸ rfl ... = 0 : has_zero_morphisms.comp_zero _ _, -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh, have hih : i ≫ h = 0, calc i ≫ h = i ≫ cokernel.π f ≫ l : hl ▸ rfl ... = 0 ≫ l : by rw [←category.assoc, kernel.condition] ... = 0 : has_zero_morphisms.zero_comp _ _, -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := normal_mono.lift' u i hih, have hs' : (s ≫ kernel.ι g) ≫ i = 𝟙 I ≫ i, by rw [category.assoc, hs, category.id_comp], haveI : epi (kernel.ι g) := epi_of_epi_fac ((cancel_mono _).1 hs'), -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) end instance mono_factor_thru_image [mono f] : mono (non_preadditive_abelian.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (non_preadditive_abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ non_preadditive_abelian.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : non_preadditive_abelian.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ non_preadditive_abelian.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (non_preadditive_abelian.factor_thru_coimage f) := let I := non_preadditive_abelian.coimage f, i := non_preadditive_abelian.factor_thru_coimage f, p := cokernel.π (kernel.ι f) in mono_of_cancel_zero _ $ λ R (g : R ⟶ I) (hgi : g ≫ i = 0), begin -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.π g, haveI : epi u := epi_comp _ _, haveI hu := non_preadditive_abelian.normal_epi u, let h := hu.g, -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi, have hf : h ≫ f = 0, calc h ≫ f = h ≫ (p ≫ i) : (coimage.fac f).symm ▸ rfl ... = h ≫ (p ≫ (cokernel.π g ≫ t)) : ht ▸ rfl ... = h ≫ u ≫ t : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = 0 ≫ t : by rw [←category.assoc, hu.w] ... = 0 : has_zero_morphisms.zero_comp _ _, -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf, have hhp : h ≫ p = 0, calc h ≫ p = (l ≫ kernel.ι f) ≫ p : hl ▸ rfl ... = l ≫ 0 : by rw [category.assoc, cokernel.condition] ... = 0 : has_zero_morphisms.comp_zero _ _, -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := normal_epi.desc' u p hhp, have hs' : p ≫ cokernel.π g ≫ s = p ≫ 𝟙 I, by rw [←category.assoc, hs, category.comp_id], haveI : mono (cokernel.π g) := mono_of_mono_fac ((cancel_epi _).1 hs'), -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) end instance epi_factor_thru_coimage [epi f] : epi (non_preadditive_abelian.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (non_preadditive_abelian.factor_thru_coimage f) := is_iso_of_mono_of_epi _ end factor section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} /-- In a `non_preadditive_abelian` category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := is_cokernel.cokernel_iso _ _ (cokernel.of_iso_comp _ _ (limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit _) h) (cone_morphism.w (limits.is_limit.unique_up_to_iso (limit.is_limit _) h).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_coimage f) (coimage.fac f) /-- In a `non_preadditive_abelian` category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := is_kernel.iso_kernel _ _ (kernel.of_comp_iso _ _ (limits.is_colimit.cocone_point_unique_up_to_iso h (colimit.is_colimit _)) (cocone_morphism.w (limits.is_colimit.unique_up_to_iso h $ colimit.is_colimit _).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_image f) (image.fac f) end cokernel_of_kernel section /-- The diagonal morphism `(𝟙 A, 𝟙 A) : A → A ⨯ A`. -/ abbreviation Δ (A : C) : A ⟶ A ⨯ A := prod.lift (𝟙 A) (𝟙 A) /-- The composite `A ⟶ A ⨯ A ⟶ cokernel (Δ A)`, where the first map is `(𝟙 A, 0)` and the second map is the canonical projection into the cokernel. -/ abbreviation r (A : C) : A ⟶ cokernel (Δ A) := prod.lift (𝟙 A) 0 ≫ cokernel.π (Δ A) instance mono_Δ {A : C} : mono (Δ A) := mono_of_mono_fac $ prod.lift_fst _ _ instance mono_r {A : C} : mono (r A) := begin let hl : is_limit (kernel_fork.of_ι (Δ A) (cokernel.condition (Δ A))), { exact mono_is_kernel_of_cokernel _ (colimit.is_colimit _) }, apply mono_of_cancel_zero, intros Z x hx, have hxx : (x ≫ prod.lift (𝟙 A) (0 : A ⟶ A)) ≫ cokernel.π (Δ A) = 0, { rw [category.assoc, hx] }, obtain ⟨y, hy⟩ := kernel_fork.is_limit.lift' hl _ hxx, rw kernel_fork.ι_of_ι at hy, have hyy : y = 0, { erw [←category.comp_id y, ←limits.prod.lift_snd (𝟙 A) (𝟙 A), ←category.assoc, hy, category.assoc, prod.lift_snd, has_zero_morphisms.comp_zero] }, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, rw [←hy, hyy, has_zero_morphisms.zero_comp, has_zero_morphisms.zero_comp] end instance epi_r {A : C} : epi (r A) := begin have hlp : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ limits.prod.snd = 0 := prod.lift_snd _ _, let hp1 : is_limit (kernel_fork.of_ι (prod.lift (𝟙 A) (0 : A ⟶ A)) hlp), { refine fork.is_limit.mk _ (λ s, fork.ι s ≫ limits.prod.fst) _ _, { intro s, ext; simp, erw category.comp_id }, { intros s m h, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, convert h walking_parallel_pair.zero, ext; simp } }, let hp2 : is_colimit (cokernel_cofork.of_π (limits.prod.snd : A ⨯ A ⟶ A) hlp), { exact epi_is_cokernel_of_kernel _ hp1 }, apply epi_of_zero_cancel, intros Z z hz, have h : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ cokernel.π (Δ A) ≫ z = 0, { rw [←category.assoc, hz] }, obtain ⟨t, ht⟩ := cokernel_cofork.is_colimit.desc' hp2 _ h, rw cokernel_cofork.π_of_π at ht, have htt : t = 0, { rw [←category.id_comp t], change 𝟙 A ≫ t = 0, rw [←limits.prod.lift_snd (𝟙 A) (𝟙 A), category.assoc, ht, ←category.assoc, cokernel.condition, has_zero_morphisms.zero_comp] }, apply (cancel_epi (cokernel.π (Δ A))).1, rw [←ht, htt, has_zero_morphisms.comp_zero, has_zero_morphisms.comp_zero] end instance is_iso_r {A : C} : is_iso (r A) := is_iso_of_mono_of_epi _ /-- The composite `A ⨯ A ⟶ cokernel (Δ A) ⟶ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `σ` for "subtraction". -/ abbreviation σ {A : C} : A ⨯ A ⟶ A := cokernel.π (Δ A) ≫ is_iso.inv (r A) end @[simp, reassoc] lemma Δ_σ {X : C} : Δ X ≫ σ = 0 := by rw [cokernel.condition_assoc, has_zero_morphisms.zero_comp] @[simp, reassoc] lemma lift_σ {X : C} : prod.lift (𝟙 X) 0 ≫ σ = 𝟙 X := by rw [←category.assoc, is_iso.hom_inv_id] @[simp, reassoc] lemma Δ_map {X Y : C} (f : X ⟶ Y) : Δ X ≫ limits.prod.map f f = f ≫ Δ Y := by ext; simp; erw category.id_comp @[reassoc] lemma lift_map {X Y : C} (f : X ⟶ Y) : prod.lift (𝟙 X) 0 ≫ limits.prod.map f f = f ≫ prod.lift (𝟙 Y) 0 := by ext; simp; erw category.id_comp /-- σ is a cokernel of Δ X. -/ def is_colimit_σ {X : C} : is_colimit (cokernel_cofork.of_π σ Δ_σ) := cokernel.cokernel_iso _ σ (as_iso (r X)).symm (by simp) /-- This is the key identity satisfied by `σ`. -/ lemma σ_comp {X Y : C} (f : X ⟶ Y) : σ ≫ f = limits.prod.map f f ≫ σ := begin obtain ⟨g, hg⟩ := cokernel_cofork.is_colimit.desc' is_colimit_σ (limits.prod.map f f ≫ σ) (by simp), suffices hfg : f = g, { rw [←hg, cofork.π_of_π, hfg] }, calc f = f ≫ prod.lift (𝟙 Y) 0 ≫ σ : by rw [lift_σ, category.comp_id] ... = prod.lift (𝟙 X) 0 ≫ limits.prod.map f f ≫ σ : by rw lift_map_assoc ... = prod.lift (𝟙 X) 0 ≫ σ ≫ g : by rw [←hg, cokernel_cofork.π_of_π] ... = g : by rw [←category.assoc, lift_σ, category.id_comp] end section /- We write `f - g` for `prod.lift f g ≫ σ`. -/ /-- Subtraction of morphisms in a `non_preadditive_abelian` category. -/ def has_sub {X Y : C} : has_sub (X ⟶ Y) := ⟨λ f g, prod.lift f g ≫ σ⟩ local attribute [instance] has_sub /- We write `-f` for `0 - f`. -/ /-- Negation of morphisms in a `non_preadditive_abelian` category. -/ def has_neg {X Y : C} : has_neg (X ⟶ Y) := ⟨λ f, 0 - f⟩ local attribute [instance] has_neg /- We write `f + g` for `f - (-g)`. -/ /-- Addition of morphisms in a `non_preadditive_abelian` category. -/ def has_add {X Y : C} : has_add (X ⟶ Y) := ⟨λ f g, f - (-g)⟩ local attribute [instance] has_add lemma sub_def {X Y : C} (a b : X ⟶ Y) : a - b = prod.lift a b ≫ σ := rfl lemma add_def {X Y : C} (a b : X ⟶ Y) : a + b = a - (-b) := rfl lemma neg_def {X Y : C} (a : X ⟶ Y) : -a = 0 - a := rfl lemma sub_zero {X Y : C} (a : X ⟶ Y) : a - 0 = a := begin rw sub_def, conv_lhs { congr, congr, rw ←category.comp_id a, skip, rw (show 0 = a ≫ (0 : Y ⟶ Y), by simp)}, rw [prod.lift_comp_comp, category.assoc, lift_σ, category.comp_id] end lemma sub_self {X Y : C} (a : X ⟶ Y) : a - a = 0 := by rw [sub_def, ←category.comp_id a, prod.lift_comp_comp, category.assoc, Δ_σ, has_zero_morphisms.comp_zero] lemma lift_sub_lift {X Y : C} (a b c d : X ⟶ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := begin simp only [sub_def], ext, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] }, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] } end lemma sub_sub_sub {X Y : C} (a b c d : X ⟶ Y) : (a - c) - (b - d) = (a - b) - (c - d) := begin rw sub_def, erw ←lift_sub_lift, rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def, sub_def, sub_def] end lemma neg_sub {X Y : C} (a b : X ⟶ Y) : (-a) - b = (-b) - a := by conv_lhs { rw [neg_def, ←sub_zero b, sub_sub_sub, sub_zero, ←neg_def] } lemma neg_neg {X Y : C} (a : X ⟶ Y) : -(-a) = a := begin rw [neg_def, neg_def], conv_lhs { congr, rw ←sub_self a }, rw [sub_sub_sub, sub_zero, sub_self, sub_zero] end lemma add_comm {X Y : C} (a b : X ⟶ Y) : a + b = b + a := begin rw [add_def], conv_lhs { rw ←neg_neg a }, rw [neg_def, neg_def, neg_def, sub_sub_sub], conv_lhs {congr, skip, rw [←neg_def, neg_sub] }, rw [sub_sub_sub, add_def, ←neg_def, neg_neg b, neg_def] end lemma add_neg {X Y : C} (a b : X ⟶ Y) : a + (-b) = a - b := by rw [add_def, neg_neg] lemma add_neg_self {X Y : C} (a : X ⟶ Y) : a + (-a) = 0 := by rw [add_neg, sub_self] lemma neg_add_self {X Y : C} (a : X ⟶ Y) : (-a) + a = 0 := by rw [add_comm, add_neg_self] lemma neg_sub' {X Y : C} (a b : X ⟶ Y) : -(a - b) = (-a) + b := begin rw [neg_def, neg_def], conv_lhs { rw ←sub_self (0 : X ⟶ Y) }, rw [sub_sub_sub, add_def, neg_def] end lemma neg_add {X Y : C} (a b : X ⟶ Y) : -(a + b) = (-a) - b := by rw [add_def, neg_sub', add_neg] lemma sub_add {X Y : C} (a b c : X ⟶ Y) : (a - b) + c = a - (b - c) := by rw [add_def, neg_def, sub_sub_sub, sub_zero] lemma add_assoc {X Y : C} (a b c : X ⟶ Y) : (a + b) + c = a + (b + c) := begin conv_lhs { congr, rw add_def }, rw [sub_add, ←add_neg, neg_sub', neg_neg] end lemma add_zero {X Y : C} (a : X ⟶ Y) : a + 0 = a := by rw [add_def, neg_def, sub_self, sub_zero] lemma comp_sub {X Y Z : C} (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g - h) = f ≫ g - f ≫ h := by rw [sub_def, ←category.assoc, ←prod.lift_comp_comp, sub_def] lemma sub_comp {X Y Z : C} (f g : X ⟶ Y) (h : Y ⟶ Z) : (f - g) ≫ h = f ≫ h - g ≫ h := by rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def] lemma comp_add (X Y Z : C) (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by rw [add_def, comp_sub, neg_def, comp_sub, has_zero_morphisms.comp_zero, add_def, neg_def] lemma add_comp (X Y Z : C) (f g : X ⟶ Y) (h : Y ⟶ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by rw [add_def, sub_comp, neg_def, sub_comp, has_zero_morphisms.zero_comp, add_def, neg_def] /-- Every `non_preadditive_abelian` category is preadditive. -/ def preadditive : preadditive C := { hom_group := λ X Y, { add := (+), add_assoc := add_assoc, zero := 0, zero_add := neg_neg, add_zero := add_zero, neg := λ f, -f, add_left_neg := neg_add_self, add_comm := add_comm }, add_comp' := add_comp, comp_add' := comp_add } end end end category_theory.non_preadditive_abelian
0b71d4bc55bed79465cd4fdd9a75d3d8c06905af
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Init/System/FilePath.lean
b13c51359537209e13fd49d9c49d46b4e16498dd
[ "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
4,170
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.System.Platform import Init.Data.String.Basic import Init.Data.Repr import Init.Data.ToString.Basic namespace System open Platform structure FilePath where toString : String deriving Inhabited, DecidableEq, Hashable instance : Repr FilePath where reprPrec p := Repr.addAppParen ("FilePath.mk " ++ repr p.toString) instance : ToString FilePath where toString p := p.toString namespace FilePath /-- The character that separates directories. In the case where more than one character is possible, `pathSeparator` is the 'ideal' one. -/ def pathSeparator : Char := if isWindows then '\\' else '/' /-- The list of all possible separators. -/ def pathSeparators : List Char := if isWindows then ['\\', '/'] else ['/'] /-- File extension character -/ def extSeparator : Char := '.' def exeExtension : String := if isWindows then "exe" else "" /-- Case-insensitive file system -/ def isCaseInsensitive : Bool := isWindows || isOSX -- TODO: normalize `a/`, `a//b`, etc. def normalize (p : FilePath) (normalizeCase := isCaseInsensitive) : FilePath := if pathSeparators.length == 1 && !normalizeCase then p else ⟨p.toString.map fun c => if pathSeparators.contains c then pathSeparator else if normalizeCase then c.toLower else c⟩ -- the following functions follow the names and semantics from Rust's `std::path::Path` def isAbsolute (p : FilePath) : Bool := pathSeparators.contains p.toString.front || (isWindows && p.toString.length > 1 && p.toString.iter.next.curr == ':') def isRelative (p : FilePath) : Bool := !p.isAbsolute def join (p sub : FilePath) : FilePath := if sub.isAbsolute then sub else ⟨p.toString ++ pathSeparator.toString ++ sub.toString⟩ instance : Div FilePath where div := FilePath.join instance : HDiv FilePath String FilePath where hDiv p sub := FilePath.join p ⟨sub⟩ private def posOfLastSep (p : FilePath) : Option String.Pos := p.toString.revFind pathSeparators.contains def parent (p : FilePath) : Option FilePath := FilePath.mk <$> p.toString.extract {} <$> posOfLastSep p def fileName (p : FilePath) : Option String := let lastPart := match posOfLastSep p with | some sepPos => p.toString.extract (sepPos + '/') p.toString.endPos | none => p.toString if lastPart.isEmpty || lastPart == "." || lastPart == ".." then none else some lastPart /-- Extracts the stem (non-extension) part of `p.fileName`. -/ def fileStem (p : FilePath) : Option String := p.fileName.map fun fname => match fname.revPosOf '.' with | some ⟨0⟩ => fname | some pos => fname.extract 0 pos | none => fname def extension (p : FilePath) : Option String := p.fileName.bind fun fname => match fname.revPosOf '.' with | some 0 => none | some pos => fname.extract (pos + '.') fname.endPos | none => none def withFileName (p : FilePath) (fname : String) : FilePath := match p.parent with | none => ⟨fname⟩ | some p => p / fname def withExtension (p : FilePath) (ext : String) : FilePath := match p.fileStem with | none => p | some stem => p.withFileName (if ext.isEmpty then stem else stem ++ "." ++ ext) def components (p : FilePath) : List String := p.normalize (normalizeCase := false) |>.toString.splitOn pathSeparator.toString end FilePath def mkFilePath (parts : List String) : FilePath := ⟨String.intercalate FilePath.pathSeparator.toString parts⟩ instance : Coe String FilePath where coe := FilePath.mk abbrev SearchPath := List FilePath namespace SearchPath /-- The character that is used to separate the entries in the $PATH (or %PATH%) environment variable. -/ protected def separator : Char := if isWindows then ';' else ':' def parse (s : String) : SearchPath := s.split (fun c => SearchPath.separator == c) |>.map FilePath.mk def toString (path : SearchPath) : String := SearchPath.separator.toString.intercalate (path.map FilePath.toString) end SearchPath end System
ed76e49add923a857109732a2e96a90d724f6f3c
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/rw_set1.lean
d94265eab6c8dbc27f86a07e2d9d65e32aa4e380
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
229
lean
namespace foo attribute [simp] nat.add_assoc print nat.add_assoc end foo print nat.add_assoc namespace foo print nat.add_assoc attribute [simp] nat.add_comm open nat print "---------" print [simp] default end foo
ce8dfefd82bbad7dcc49517c70f0fd57e482cc40
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/ODE/gronwall.lean
803ade121774668b8ec4dbe2da2554995b89764c
[]
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
11,302
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.mean_value import Mathlib.analysis.special_functions.exp_log import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Grönwall's inequality The main technical result of this file is the Grönwall-like inequality `norm_le_gronwall_bound_of_norm_deriv_right_le`. It states that if `f : ℝ → E` satisfies `∥f a∥ ≤ δ` and `∀ x ∈ [a, b), ∥f' x∥ ≤ K * ∥f x∥ + ε`, then for all `x ∈ [a, b]` we have `∥f x∥ ≤ δ * exp (K * x) + (ε / K) * (exp (K * x) - 1)`. Then we use this inequality to prove some estimates on the possible rate of growth of the distance between two approximate or exact solutions of an ordinary differential equation. The proofs are based on [Hubbard and West, *Differential Equations: A Dynamical Systems Approach*, Sec. 4.5][HubbardWest-ode], where `norm_le_gronwall_bound_of_norm_deriv_right_le` is called “Fundamental Inequality”. ## TODO - Once we have FTC, prove an inequality for a function satisfying `∥f' x∥ ≤ K x * ∥f x∥ + ε`, or more generally `liminf_{y→x+0} (f y - f x)/(y - x) ≤ K x * f x + ε` with any sign of `K x` and `f x`. -/ /-! ### Technical lemmas about `gronwall_bound` -/ /-- Upper bound used in several Grönwall-like inequalities. -/ def gronwall_bound (δ : ℝ) (K : ℝ) (ε : ℝ) (x : ℝ) : ℝ := ite (K = 0) (δ + ε * x) (δ * real.exp (K * x) + ε / K * (real.exp (K * x) - 1)) theorem gronwall_bound_K0 (δ : ℝ) (ε : ℝ) : gronwall_bound δ 0 ε = fun (x : ℝ) => δ + ε * x := funext fun (x : ℝ) => if_pos rfl theorem gronwall_bound_of_K_ne_0 {δ : ℝ} {K : ℝ} {ε : ℝ} (hK : K ≠ 0) : gronwall_bound δ K ε = fun (x : ℝ) => δ * real.exp (K * x) + ε / K * (real.exp (K * x) - 1) := funext fun (x : ℝ) => if_neg hK theorem has_deriv_at_gronwall_bound (δ : ℝ) (K : ℝ) (ε : ℝ) (x : ℝ) : has_deriv_at (gronwall_bound δ K ε) (K * gronwall_bound δ K ε x + ε) x := sorry theorem has_deriv_at_gronwall_bound_shift (δ : ℝ) (K : ℝ) (ε : ℝ) (x : ℝ) (a : ℝ) : has_deriv_at (fun (y : ℝ) => gronwall_bound δ K ε (y - a)) (K * gronwall_bound δ K ε (x - a) + ε) x := sorry theorem gronwall_bound_x0 (δ : ℝ) (K : ℝ) (ε : ℝ) : gronwall_bound δ K ε 0 = δ := sorry theorem gronwall_bound_ε0 (δ : ℝ) (K : ℝ) (x : ℝ) : gronwall_bound δ K 0 x = δ * real.exp (K * x) := sorry theorem gronwall_bound_ε0_δ0 (K : ℝ) (x : ℝ) : gronwall_bound 0 K 0 x = 0 := sorry theorem gronwall_bound_continuous_ε (δ : ℝ) (K : ℝ) (x : ℝ) : continuous fun (ε : ℝ) => gronwall_bound δ K ε x := sorry /-! ### Inequality and corollaries -/ /-- A Grönwall-like inequality: if `f : ℝ → ℝ` is continuous on `[a, b]` and satisfies the inequalities `f a ≤ δ` and `∀ x ∈ [a, b), liminf_{z→x+0} (f z - f x)/(z - x) ≤ K * (f x) + ε`, then `f x` is bounded by `gronwall_bound δ K ε (x - a)` on `[a, b]`. See also `norm_le_gronwall_bound_of_norm_deriv_right_le` for a version bounding `∥f x∥`, `f : ℝ → E`. -/ theorem le_gronwall_bound_of_liminf_deriv_right_le {f : ℝ → ℝ} {f' : ℝ → ℝ} {δ : ℝ} {K : ℝ} {ε : ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → ∀ (r : ℝ), f' x < r → filter.frequently (fun (z : ℝ) => z - x⁻¹ * (f z - f x) < r) (nhds_within x (set.Ioi x))) (ha : f a ≤ δ) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f' x ≤ K * f x + ε) (x : ℝ) (H : x ∈ set.Icc a b) : f x ≤ gronwall_bound δ K ε (x - a) := sorry /-- A Grönwall-like inequality: if `f : ℝ → E` is continuous on `[a, b]`, has right derivative `f' x` at every point `x ∈ [a, b)`, and satisfies the inequalities `∥f a∥ ≤ δ`, `∀ x ∈ [a, b), ∥f' x∥ ≤ K * ∥f x∥ + ε`, then `∥f x∥` is bounded by `gronwall_bound δ K ε (x - a)` on `[a, b]`. -/ theorem norm_le_gronwall_bound_of_norm_deriv_right_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {f' : ℝ → E} {δ : ℝ} {K : ℝ} {ε : ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) (ha : norm (f a) ≤ δ) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f' x) ≤ K * norm (f x) + ε) (x : ℝ) (H : x ∈ set.Icc a b) : norm (f x) ≤ gronwall_bound δ K ε (x - a) := sorry /-- If `f` and `g` are two approximate solutions of the same ODE, then the distance between them can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some people call this Grönwall's inequality too. This version assumes all inequalities to be true in some time-dependent set `s t`, and assumes that the solutions never leave this set. -/ theorem dist_le_of_approx_trajectories_ODE_of_mem_set {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {s : ℝ → set E} {K : ℝ} (hv : ∀ (t : ℝ) (x y : E), x ∈ s t → y ∈ s t → dist (v t x) (v t y) ≤ K * dist x y) {f : ℝ → E} {g : ℝ → E} {f' : ℝ → E} {g' : ℝ → E} {a : ℝ} {b : ℝ} {εf : ℝ} {εg : ℝ} {δ : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (f' t) (set.Ici t) t) (f_bound : ∀ (t : ℝ), t ∈ set.Ico a b → dist (f' t) (v t (f t)) ≤ εf) (hfs : ∀ (t : ℝ), t ∈ set.Ico a b → f t ∈ s t) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (g' t) (set.Ici t) t) (g_bound : ∀ (t : ℝ), t ∈ set.Ico a b → dist (g' t) (v t (g t)) ≤ εg) (hgs : ∀ (t : ℝ), t ∈ set.Ico a b → g t ∈ s t) (ha : dist (f a) (g a) ≤ δ) (t : ℝ) (H : t ∈ set.Icc a b) : dist (f t) (g t) ≤ gronwall_bound δ K (εf + εg) (t - a) := sorry /-- If `f` and `g` are two approximate solutions of the same ODE, then the distance between them can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some people call this Grönwall's inequality too. This version assumes all inequalities to be true in the whole space. -/ theorem dist_le_of_approx_trajectories_ODE {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {K : nnreal} (hv : ∀ (t : ℝ), lipschitz_with K (v t)) {f : ℝ → E} {g : ℝ → E} {f' : ℝ → E} {g' : ℝ → E} {a : ℝ} {b : ℝ} {εf : ℝ} {εg : ℝ} {δ : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (f' t) (set.Ici t) t) (f_bound : ∀ (t : ℝ), t ∈ set.Ico a b → dist (f' t) (v t (f t)) ≤ εf) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (g' t) (set.Ici t) t) (g_bound : ∀ (t : ℝ), t ∈ set.Ico a b → dist (g' t) (v t (g t)) ≤ εg) (ha : dist (f a) (g a) ≤ δ) (t : ℝ) (H : t ∈ set.Icc a b) : dist (f t) (g t) ≤ gronwall_bound δ (↑K) (εf + εg) (t - a) := sorry /-- If `f` and `g` are two exact solutions of the same ODE, then the distance between them can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some people call this Grönwall's inequality too. This version assumes all inequalities to be true in some time-dependent set `s t`, and assumes that the solutions never leave this set. -/ theorem dist_le_of_trajectories_ODE_of_mem_set {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {s : ℝ → set E} {K : ℝ} (hv : ∀ (t : ℝ) (x y : E), x ∈ s t → y ∈ s t → dist (v t x) (v t y) ≤ K * dist x y) {f : ℝ → E} {g : ℝ → E} {a : ℝ} {b : ℝ} {δ : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (v t (f t)) (set.Ici t) t) (hfs : ∀ (t : ℝ), t ∈ set.Ico a b → f t ∈ s t) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (v t (g t)) (set.Ici t) t) (hgs : ∀ (t : ℝ), t ∈ set.Ico a b → g t ∈ s t) (ha : dist (f a) (g a) ≤ δ) (t : ℝ) (H : t ∈ set.Icc a b) : dist (f t) (g t) ≤ δ * real.exp (K * (t - a)) := sorry /-- If `f` and `g` are two exact solutions of the same ODE, then the distance between them can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some people call this Grönwall's inequality too. This version assumes all inequalities to be true in the whole space. -/ theorem dist_le_of_trajectories_ODE {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {K : nnreal} (hv : ∀ (t : ℝ), lipschitz_with K (v t)) {f : ℝ → E} {g : ℝ → E} {a : ℝ} {b : ℝ} {δ : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (v t (f t)) (set.Ici t) t) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (v t (g t)) (set.Ici t) t) (ha : dist (f a) (g a) ≤ δ) (t : ℝ) (H : t ∈ set.Icc a b) : dist (f t) (g t) ≤ δ * real.exp (↑K * (t - a)) := sorry /-- There exists only one solution of an ODE \(\dot x=v(t, x)\) in a set `s ⊆ ℝ × E` with a given initial value provided that RHS is Lipschitz continuous in `x` within `s`, and we consider only solutions included in `s`. -/ theorem ODE_solution_unique_of_mem_set {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {s : ℝ → set E} {K : ℝ} (hv : ∀ (t : ℝ) (x y : E), x ∈ s t → y ∈ s t → dist (v t x) (v t y) ≤ K * dist x y) {f : ℝ → E} {g : ℝ → E} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (v t (f t)) (set.Ici t) t) (hfs : ∀ (t : ℝ), t ∈ set.Ico a b → f t ∈ s t) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (v t (g t)) (set.Ici t) t) (hgs : ∀ (t : ℝ), t ∈ set.Ico a b → g t ∈ s t) (ha : f a = g a) (t : ℝ) (H : t ∈ set.Icc a b) : f t = g t := eq.mp (Eq._oldrec (Eq.refl (dist (f t) (g t) ≤ 0)) (propext dist_le_zero)) (eq.mp (Eq._oldrec (Eq.refl (dist (f t) (g t) ≤ 0 * real.exp (K * (t - a)))) (zero_mul (real.exp (K * (t - a))))) (dist_le_of_trajectories_ODE_of_mem_set hv hf hf' hfs hg hg' hgs (iff.mpr dist_le_zero ha) t ht)) /-- There exists only one solution of an ODE \(\dot x=v(t, x)\) with a given initial value provided that RHS is Lipschitz continuous in `x`. -/ theorem ODE_solution_unique {E : Type u_1} [normed_group E] [normed_space ℝ E] {v : ℝ → E → E} {K : nnreal} (hv : ∀ (t : ℝ), lipschitz_with K (v t)) {f : ℝ → E} {g : ℝ → E} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at f (v t (f t)) (set.Ici t) t) (hg : continuous_on g (set.Icc a b)) (hg' : ∀ (t : ℝ), t ∈ set.Ico a b → has_deriv_within_at g (v t (g t)) (set.Ici t) t) (ha : f a = g a) (t : ℝ) (H : t ∈ set.Icc a b) : f t = g t := sorry
bc01e4d1345353ebdd9f73360e49b27ee10b2dec
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/bicategory/End.lean
a5c38f929d6eb3f9810a0d442ee1c329a1b989b7
[ "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,251
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.bicategory.basic import category_theory.monoidal.category /-! # Endomorphisms of an object in a bicategory, as a monoidal category. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ namespace category_theory variables {C : Type*} [bicategory C] /-- The endomorphisms of an object in a bicategory can be considered as a monoidal category. -/ @[derive category] def End_monoidal (X : C) := X ⟶ X instance (X : C) : inhabited (End_monoidal X) := ⟨𝟙 X⟩ open_locale bicategory open monoidal_category open bicategory instance (X : C) : monoidal_category (End_monoidal X) := { tensor_obj := λ f g, f ≫ g, tensor_hom := λ f g h i η θ, (η ▷ h) ≫ (g ◁ θ), tensor_unit := 𝟙 _, associator := λ f g h, α_ f g h, left_unitor := λ f, λ_ f, right_unitor := λ f, ρ_ f, tensor_comp' := begin intros, rw [bicategory.whisker_left_comp, bicategory.comp_whisker_right, category.assoc, category.assoc, bicategory.whisker_exchange_assoc], end } end category_theory
5194981de61cdc54b201710a9f4262e314fd12bd
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world8/level2.lean
b8aa512b0539ac3b74bc6c6473022fe437a6093a
[ "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
1,447
lean
import mynat.definition -- hide import mynat.add -- hide import game.world8.level1 -- hide namespace mynat -- hide /- # Advanced Addition World ## Level 2: `succ_succ_inj`. -/ /- In the below theorem, we need to apply `succ_inj` twice. Once to prove $succ(succ(a))=succ(succ(b))\implies succ(a)=succ(b)$, and then again to prove $succ(a)=succ(b)\implies a=b$. However `succ(a)=succ(b)` is nowhere to be found, it's neither an assumption or a goal when we start this level. You can make it with `have` or you can use `apply`. -/ /- Theorem For all naturals $a$ and $b$, if we assume $succ(succ(a))=succ(succ(b))$, then we can deduce $a=b$. -/ theorem succ_succ_inj {a b : mynat} (h : succ(succ(a)) = succ(succ(b))) : a = b := begin [nat_num_game] have h2 : succ(a)=succ(b), exact succ_inj(h), exact succ_inj(h2), end /- ## Sample solutions to this level. Make sure you understand them all. And remember that `rw` should not be used with `succ_inj` -- `rw` works only with equalities or `↔` statements, not implications or functions. -/ example {a b : mynat} (h : succ(succ(a)) = succ(succ(b))) : a = b := begin apply succ_inj, apply succ_inj, exact h end example {a b : mynat} (h : succ(succ(a)) = succ(succ(b))) : a = b := begin apply succ_inj, exact succ_inj(h), end example {a b : mynat} (h : succ(succ(a)) = succ(succ(b))) : a = b := begin exact succ_inj(succ_inj(h)), end end mynat -- hide
023815807106553407bfbc8cdd98ab55f444608b
6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1
/ExamPractice/exam3TAStuff.lean
1270dc52f6892ccf55af11f435eb647328f8ccfa
[]
no_license
derekjohnsonva/CS2102
8ed45daa6658e6121bac0f6691eac6147d08246d
b3f507d4be824a2511838a1054d04fc9aef3304c
refs/heads/master
1,648,529,162,527
1,578,851,859,000
1,578,851,859,000
233,433,207
0
0
null
null
null
null
UTF-8
Lean
false
false
339
lean
example : ∀ P Q : Prop, (∀ p : P, Q) → (P → Q) := begin assume P Q, assume pimpq, exact pimpq, end def isEven : ℕ → Prop := λ (n : nat), ∃ k, 2*k = n example : ∀ n : nat, (isEven n) → (isEven (2*n)) := begin unfold isEven, assume n, assume premise, apply exists.intro n, refl, end
bad49f1189f99e1c82641896686f8ef6745ba8c6
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/analysis/special_functions/trigonometric.lean
65e45955b227f1180b8ac2644ca98e0efe85b4cc
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
123,679
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.exp_log import data.set.intervals.infinite import algebra.quadratic_discriminant import ring_theory.polynomial.chebyshev.defs /-! # Trigonometric functions ## Main definitions This file contains the following definitions: * π, arcsin, arccos, arctan * argument of a complex number * logarithm on complex numbers ## Main statements Many basic inequalities on trigonometric functions are established. The continuity and differentiability of the usual trigonometric functions are proved, and their derivatives are computed. * `polynomial.chebyshev₁_complex_cos`: the `n`-th Chebyshev polynomial evaluates on `complex.cos θ` to the value `n * complex.cos θ`. ## Tags log, sin, cos, tan, arcsin, arccos, arctan, angle, argument -/ noncomputable theory open_locale classical topological_space filter open set filter namespace complex /-- The complex sine function is everywhere differentiable, with the derivative `cos x`. -/ lemma has_deriv_at_sin (x : ℂ) : has_deriv_at sin (cos x) x := begin simp only [cos, div_eq_mul_inv], convert ((((has_deriv_at_id x).neg.mul_const I).cexp.sub ((has_deriv_at_id x).mul_const I).cexp).mul_const I).mul_const (2:ℂ)⁻¹, simp only [function.comp, id], rw [sub_mul, mul_assoc, mul_assoc, I_mul_I, neg_one_mul, neg_neg, mul_one, one_mul, mul_assoc, I_mul_I, mul_neg_one, sub_neg_eq_add, add_comm] end lemma times_cont_diff_sin {n} : times_cont_diff ℂ n sin := (((times_cont_diff_neg.mul times_cont_diff_const).cexp.sub (times_cont_diff_id.mul times_cont_diff_const).cexp).mul times_cont_diff_const).div_const lemma differentiable_sin : differentiable ℂ sin := λx, (has_deriv_at_sin x).differentiable_at lemma differentiable_at_sin {x : ℂ} : differentiable_at ℂ sin x := differentiable_sin x @[simp] lemma deriv_sin : deriv sin = cos := funext $ λ x, (has_deriv_at_sin x).deriv lemma continuous_sin : continuous sin := differentiable_sin.continuous lemma continuous_on_sin {s : set ℂ} : continuous_on sin s := continuous_sin.continuous_on lemma measurable_sin : measurable sin := continuous_sin.measurable /-- The complex cosine function is everywhere differentiable, with the derivative `-sin x`. -/ lemma has_deriv_at_cos (x : ℂ) : has_deriv_at cos (-sin x) x := begin simp only [sin, div_eq_mul_inv, neg_mul_eq_neg_mul], convert (((has_deriv_at_id x).mul_const I).cexp.add ((has_deriv_at_id x).neg.mul_const I).cexp).mul_const (2:ℂ)⁻¹, simp only [function.comp, id], ring end lemma times_cont_diff_cos {n} : times_cont_diff ℂ n cos := ((times_cont_diff_id.mul times_cont_diff_const).cexp.add (times_cont_diff_neg.mul times_cont_diff_const).cexp).div_const lemma differentiable_cos : differentiable ℂ cos := λx, (has_deriv_at_cos x).differentiable_at lemma differentiable_at_cos {x : ℂ} : differentiable_at ℂ cos x := differentiable_cos x lemma deriv_cos {x : ℂ} : deriv cos x = -sin x := (has_deriv_at_cos x).deriv @[simp] lemma deriv_cos' : deriv cos = (λ x, -sin x) := funext $ λ x, deriv_cos lemma continuous_cos : continuous cos := differentiable_cos.continuous lemma continuous_on_cos {s : set ℂ} : continuous_on cos s := continuous_cos.continuous_on lemma measurable_cos : measurable cos := continuous_cos.measurable /-- The complex hyperbolic sine function is everywhere differentiable, with the derivative `cosh x`. -/ lemma has_deriv_at_sinh (x : ℂ) : has_deriv_at sinh (cosh x) x := begin simp only [cosh, div_eq_mul_inv], convert ((has_deriv_at_exp x).sub (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹, rw [id, mul_neg_one, sub_eq_add_neg, neg_neg] end lemma times_cont_diff_sinh {n} : times_cont_diff ℂ n sinh := (times_cont_diff_exp.sub times_cont_diff_neg.cexp).div_const lemma differentiable_sinh : differentiable ℂ sinh := λx, (has_deriv_at_sinh x).differentiable_at lemma differentiable_at_sinh {x : ℂ} : differentiable_at ℂ sinh x := differentiable_sinh x @[simp] lemma deriv_sinh : deriv sinh = cosh := funext $ λ x, (has_deriv_at_sinh x).deriv lemma continuous_sinh : continuous sinh := differentiable_sinh.continuous lemma measurable_sinh : measurable sinh := continuous_sinh.measurable /-- The complex hyperbolic cosine function is everywhere differentiable, with the derivative `sinh x`. -/ lemma has_deriv_at_cosh (x : ℂ) : has_deriv_at cosh (sinh x) x := begin simp only [sinh, div_eq_mul_inv], convert ((has_deriv_at_exp x).add (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹, rw [id, mul_neg_one, sub_eq_add_neg] end lemma times_cont_diff_cosh {n} : times_cont_diff ℂ n cosh := (times_cont_diff_exp.add times_cont_diff_neg.cexp).div_const lemma differentiable_cosh : differentiable ℂ cosh := λx, (has_deriv_at_cosh x).differentiable_at lemma differentiable_at_cosh {x : ℂ} : differentiable_at ℂ cos x := differentiable_cos x @[simp] lemma deriv_cosh : deriv cosh = sinh := funext $ λ x, (has_deriv_at_cosh x).deriv lemma continuous_cosh : continuous cosh := differentiable_cosh.continuous lemma measurable_cosh : measurable cosh := continuous_cosh.measurable end complex section /-! ### Simp lemmas for derivatives of `λ x, complex.cos (f x)` etc., `f : ℂ → ℂ` -/ variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} /-! #### `complex.cos` -/ lemma measurable.ccos {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) : measurable (λ x, complex.cos (f x)) := complex.measurable_cos.comp hf lemma has_deriv_at.ccos (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') x := (complex.has_deriv_at_cos (f x)).comp x hf lemma has_deriv_within_at.ccos (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') s x := (complex.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf lemma deriv_within_ccos (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.cos (f x)) s x = - complex.sin (f x) * (deriv_within f s x) := hf.has_deriv_within_at.ccos.deriv_within hxs @[simp] lemma deriv_ccos (hc : differentiable_at ℂ f x) : deriv (λx, complex.cos (f x)) x = - complex.sin (f x) * (deriv f x) := hc.has_deriv_at.ccos.deriv /-! #### `complex.sin` -/ lemma measurable.csin {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) : measurable (λ x, complex.sin (f x)) := complex.measurable_sin.comp hf lemma has_deriv_at.csin (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') x := (complex.has_deriv_at_sin (f x)).comp x hf lemma has_deriv_within_at.csin (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') s x := (complex.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf lemma deriv_within_csin (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.sin (f x)) s x = complex.cos (f x) * (deriv_within f s x) := hf.has_deriv_within_at.csin.deriv_within hxs @[simp] lemma deriv_csin (hc : differentiable_at ℂ f x) : deriv (λx, complex.sin (f x)) x = complex.cos (f x) * (deriv f x) := hc.has_deriv_at.csin.deriv /-! #### `complex.cosh` -/ lemma measurable.ccosh {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) : measurable (λ x, complex.cosh (f x)) := complex.measurable_cosh.comp hf lemma has_deriv_at.ccosh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') x := (complex.has_deriv_at_cosh (f x)).comp x hf lemma has_deriv_within_at.ccosh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') s x := (complex.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf lemma deriv_within_ccosh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.cosh (f x)) s x = complex.sinh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.ccosh.deriv_within hxs @[simp] lemma deriv_ccosh (hc : differentiable_at ℂ f x) : deriv (λx, complex.cosh (f x)) x = complex.sinh (f x) * (deriv f x) := hc.has_deriv_at.ccosh.deriv /-! #### `complex.sinh` -/ lemma measurable.csinh {α : Type*} [measurable_space α] {f : α → ℂ} (hf : measurable f) : measurable (λ x, complex.sinh (f x)) := complex.measurable_sinh.comp hf lemma has_deriv_at.csinh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') x := (complex.has_deriv_at_sinh (f x)).comp x hf lemma has_deriv_within_at.csinh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') s x := (complex.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf lemma deriv_within_csinh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.sinh (f x)) s x = complex.cosh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.csinh.deriv_within hxs @[simp] lemma deriv_csinh (hc : differentiable_at ℂ f x) : deriv (λx, complex.sinh (f x)) x = complex.cosh (f x) * (deriv f x) := hc.has_deriv_at.csinh.deriv end section /-! ### Simp lemmas for derivatives of `λ x, complex.cos (f x)` etc., `f : E → ℂ` -/ variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E} {s : set E} /-! #### `complex.cos` -/ lemma has_fderiv_at.ccos (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.cos (f x)) (- complex.sin (f x) • f') x := (complex.has_deriv_at_cos (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.ccos (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) • f') s x := (complex.has_deriv_at_cos (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.ccos (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.cos (f x)) s x := hf.has_fderiv_within_at.ccos.differentiable_within_at @[simp] lemma differentiable_at.ccos (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.cos (f x)) x := hc.has_fderiv_at.ccos.differentiable_at lemma differentiable_on.ccos (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.cos (f x)) s := λx h, (hc x h).ccos @[simp] lemma differentiable.ccos (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.cos (f x)) := λx, (hc x).ccos lemma fderiv_within_ccos (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : fderiv_within ℂ (λx, complex.cos (f x)) s x = - complex.sin (f x) • (fderiv_within ℂ f s x) := hf.has_fderiv_within_at.ccos.fderiv_within hxs @[simp] lemma fderiv_ccos (hc : differentiable_at ℂ f x) : fderiv ℂ (λx, complex.cos (f x)) x = - complex.sin (f x) • (fderiv ℂ f x) := hc.has_fderiv_at.ccos.fderiv lemma times_cont_diff.ccos {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.cos (f x)) := complex.times_cont_diff_cos.comp h lemma times_cont_diff_at.ccos {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.cos (f x)) x := complex.times_cont_diff_cos.times_cont_diff_at.comp x hf lemma times_cont_diff_on.ccos {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.cos (f x)) s := complex.times_cont_diff_cos.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.ccos {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.cos (f x)) s x := complex.times_cont_diff_cos.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `complex.sin` -/ lemma has_fderiv_at.csin (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.sin (f x)) (complex.cos (f x) • f') x := (complex.has_deriv_at_sin (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.csin (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) • f') s x := (complex.has_deriv_at_sin (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.csin (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.sin (f x)) s x := hf.has_fderiv_within_at.csin.differentiable_within_at @[simp] lemma differentiable_at.csin (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.sin (f x)) x := hc.has_fderiv_at.csin.differentiable_at lemma differentiable_on.csin (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.sin (f x)) s := λx h, (hc x h).csin @[simp] lemma differentiable.csin (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.sin (f x)) := λx, (hc x).csin lemma fderiv_within_csin (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : fderiv_within ℂ (λx, complex.sin (f x)) s x = complex.cos (f x) • (fderiv_within ℂ f s x) := hf.has_fderiv_within_at.csin.fderiv_within hxs @[simp] lemma fderiv_csin (hc : differentiable_at ℂ f x) : fderiv ℂ (λx, complex.sin (f x)) x = complex.cos (f x) • (fderiv ℂ f x) := hc.has_fderiv_at.csin.fderiv lemma times_cont_diff.csin {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.sin (f x)) := complex.times_cont_diff_sin.comp h lemma times_cont_diff_at.csin {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.sin (f x)) x := complex.times_cont_diff_sin.times_cont_diff_at.comp x hf lemma times_cont_diff_on.csin {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.sin (f x)) s := complex.times_cont_diff_sin.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.csin {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.sin (f x)) s x := complex.times_cont_diff_sin.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `complex.cosh` -/ lemma has_fderiv_at.ccosh (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) • f') x := (complex.has_deriv_at_cosh (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.ccosh (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) • f') s x := (complex.has_deriv_at_cosh (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.ccosh (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.cosh (f x)) s x := hf.has_fderiv_within_at.ccosh.differentiable_within_at @[simp] lemma differentiable_at.ccosh (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.cosh (f x)) x := hc.has_fderiv_at.ccosh.differentiable_at lemma differentiable_on.ccosh (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.cosh (f x)) s := λx h, (hc x h).ccosh @[simp] lemma differentiable.ccosh (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.cosh (f x)) := λx, (hc x).ccosh lemma fderiv_within_ccosh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : fderiv_within ℂ (λx, complex.cosh (f x)) s x = complex.sinh (f x) • (fderiv_within ℂ f s x) := hf.has_fderiv_within_at.ccosh.fderiv_within hxs @[simp] lemma fderiv_ccosh (hc : differentiable_at ℂ f x) : fderiv ℂ (λx, complex.cosh (f x)) x = complex.sinh (f x) • (fderiv ℂ f x) := hc.has_fderiv_at.ccosh.fderiv lemma times_cont_diff.ccosh {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.cosh (f x)) := complex.times_cont_diff_cosh.comp h lemma times_cont_diff_at.ccosh {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.cosh (f x)) x := complex.times_cont_diff_cosh.times_cont_diff_at.comp x hf lemma times_cont_diff_on.ccosh {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.cosh (f x)) s := complex.times_cont_diff_cosh.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.ccosh {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.cosh (f x)) s x := complex.times_cont_diff_cosh.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `complex.sinh` -/ lemma has_fderiv_at.csinh (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) • f') x := (complex.has_deriv_at_sinh (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.csinh (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) • f') s x := (complex.has_deriv_at_sinh (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.csinh (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.sinh (f x)) s x := hf.has_fderiv_within_at.csinh.differentiable_within_at @[simp] lemma differentiable_at.csinh (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.sinh (f x)) x := hc.has_fderiv_at.csinh.differentiable_at lemma differentiable_on.csinh (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.sinh (f x)) s := λx h, (hc x h).csinh @[simp] lemma differentiable.csinh (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.sinh (f x)) := λx, (hc x).csinh lemma fderiv_within_csinh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : fderiv_within ℂ (λx, complex.sinh (f x)) s x = complex.cosh (f x) • (fderiv_within ℂ f s x) := hf.has_fderiv_within_at.csinh.fderiv_within hxs @[simp] lemma fderiv_csinh (hc : differentiable_at ℂ f x) : fderiv ℂ (λx, complex.sinh (f x)) x = complex.cosh (f x) • (fderiv ℂ f x) := hc.has_fderiv_at.csinh.fderiv lemma times_cont_diff.csinh {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.sinh (f x)) := complex.times_cont_diff_sinh.comp h lemma times_cont_diff_at.csinh {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.sinh (f x)) x := complex.times_cont_diff_sinh.times_cont_diff_at.comp x hf lemma times_cont_diff_on.csinh {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.sinh (f x)) s := complex.times_cont_diff_sinh.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.csinh {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.sinh (f x)) s x := complex.times_cont_diff_sinh.times_cont_diff_at.comp_times_cont_diff_within_at x hf end namespace real variables {x y z : ℝ} lemma has_deriv_at_sin (x : ℝ) : has_deriv_at sin (cos x) x := (complex.has_deriv_at_sin x).real_of_complex lemma times_cont_diff_sin {n} : times_cont_diff ℝ n sin := complex.times_cont_diff_sin.real_of_complex lemma differentiable_sin : differentiable ℝ sin := λx, (has_deriv_at_sin x).differentiable_at lemma differentiable_at_sin : differentiable_at ℝ sin x := differentiable_sin x @[simp] lemma deriv_sin : deriv sin = cos := funext $ λ x, (has_deriv_at_sin x).deriv lemma continuous_sin : continuous sin := differentiable_sin.continuous lemma measurable_sin : measurable sin := continuous_sin.measurable lemma has_deriv_at_cos (x : ℝ) : has_deriv_at cos (-sin x) x := (complex.has_deriv_at_cos x).real_of_complex lemma times_cont_diff_cos {n} : times_cont_diff ℝ n cos := complex.times_cont_diff_cos.real_of_complex lemma differentiable_cos : differentiable ℝ cos := λx, (has_deriv_at_cos x).differentiable_at lemma differentiable_at_cos : differentiable_at ℝ cos x := differentiable_cos x lemma deriv_cos : deriv cos x = - sin x := (has_deriv_at_cos x).deriv @[simp] lemma deriv_cos' : deriv cos = (λ x, - sin x) := funext $ λ _, deriv_cos lemma continuous_cos : continuous cos := differentiable_cos.continuous lemma continuous_on_cos {s} : continuous_on cos s := continuous_cos.continuous_on lemma measurable_cos : measurable cos := continuous_cos.measurable lemma has_deriv_at_sinh (x : ℝ) : has_deriv_at sinh (cosh x) x := (complex.has_deriv_at_sinh x).real_of_complex lemma times_cont_diff_sinh {n} : times_cont_diff ℝ n sinh := complex.times_cont_diff_sinh.real_of_complex lemma differentiable_sinh : differentiable ℝ sinh := λx, (has_deriv_at_sinh x).differentiable_at lemma differentiable_at_sinh : differentiable_at ℝ sinh x := differentiable_sinh x @[simp] lemma deriv_sinh : deriv sinh = cosh := funext $ λ x, (has_deriv_at_sinh x).deriv lemma continuous_sinh : continuous sinh := differentiable_sinh.continuous lemma measurable_sinh : measurable sinh := continuous_sinh.measurable lemma has_deriv_at_cosh (x : ℝ) : has_deriv_at cosh (sinh x) x := (complex.has_deriv_at_cosh x).real_of_complex lemma times_cont_diff_cosh {n} : times_cont_diff ℝ n cosh := complex.times_cont_diff_cosh.real_of_complex lemma differentiable_cosh : differentiable ℝ cosh := λx, (has_deriv_at_cosh x).differentiable_at lemma differentiable_at_cosh : differentiable_at ℝ cosh x := differentiable_cosh x @[simp] lemma deriv_cosh : deriv cosh = sinh := funext $ λ x, (has_deriv_at_cosh x).deriv lemma continuous_cosh : continuous cosh := differentiable_cosh.continuous lemma measurable_cosh : measurable cosh := continuous_cosh.measurable /-- `sinh` is strictly monotone. -/ lemma sinh_strict_mono : strict_mono sinh := strict_mono_of_deriv_pos differentiable_sinh (by { rw [real.deriv_sinh], exact cosh_pos }) end real section /-! ### Simp lemmas for derivatives of `λ x, real.cos (f x)` etc., `f : ℝ → ℝ` -/ variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} /-! #### `real.cos` -/ lemma measurable.cos {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) : measurable (λ x, real.cos (f x)) := real.measurable_cos.comp hf lemma has_deriv_at.cos (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.cos (f x)) (- real.sin (f x) * f') x := (real.has_deriv_at_cos (f x)).comp x hf lemma has_deriv_within_at.cos (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.cos (f x)) (- real.sin (f x) * f') s x := (real.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf lemma deriv_within_cos (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.cos (f x)) s x = - real.sin (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cos.deriv_within hxs @[simp] lemma deriv_cos (hc : differentiable_at ℝ f x) : deriv (λx, real.cos (f x)) x = - real.sin (f x) * (deriv f x) := hc.has_deriv_at.cos.deriv /-! #### `real.sin` -/ lemma measurable.sin {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) : measurable (λ x, real.sin (f x)) := real.measurable_sin.comp hf lemma has_deriv_at.sin (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.sin (f x)) (real.cos (f x) * f') x := (real.has_deriv_at_sin (f x)).comp x hf lemma has_deriv_within_at.sin (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.sin (f x)) (real.cos (f x) * f') s x := (real.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf lemma deriv_within_sin (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.sin (f x)) s x = real.cos (f x) * (deriv_within f s x) := hf.has_deriv_within_at.sin.deriv_within hxs @[simp] lemma deriv_sin (hc : differentiable_at ℝ f x) : deriv (λx, real.sin (f x)) x = real.cos (f x) * (deriv f x) := hc.has_deriv_at.sin.deriv /-! #### `real.cosh` -/ lemma measurable.cosh {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) : measurable (λ x, real.cosh (f x)) := real.measurable_cosh.comp hf lemma has_deriv_at.cosh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') x := (real.has_deriv_at_cosh (f x)).comp x hf lemma has_deriv_within_at.cosh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') s x := (real.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf lemma deriv_within_cosh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.cosh (f x)) s x = real.sinh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cosh.deriv_within hxs @[simp] lemma deriv_cosh (hc : differentiable_at ℝ f x) : deriv (λx, real.cosh (f x)) x = real.sinh (f x) * (deriv f x) := hc.has_deriv_at.cosh.deriv /-! #### `real.sinh` -/ lemma measurable.sinh {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) : measurable (λ x, real.sinh (f x)) := real.measurable_sinh.comp hf lemma has_deriv_at.sinh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') x := (real.has_deriv_at_sinh (f x)).comp x hf lemma has_deriv_within_at.sinh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') s x := (real.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf lemma deriv_within_sinh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.sinh (f x)) s x = real.cosh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.sinh.deriv_within hxs @[simp] lemma deriv_sinh (hc : differentiable_at ℝ f x) : deriv (λx, real.sinh (f x)) x = real.cosh (f x) * (deriv f x) := hc.has_deriv_at.sinh.deriv end section /-! ### Simp lemmas for derivatives of `λ x, real.cos (f x)` etc., `f : E → ℝ` -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E} {s : set E} /-! #### `real.cos` -/ lemma has_fderiv_at.cos (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.cos (f x)) (- real.sin (f x) • f') x := (real.has_deriv_at_cos (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.cos (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.cos (f x)) (- real.sin (f x) • f') s x := (real.has_deriv_at_cos (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.cos (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.cos (f x)) s x := hf.has_fderiv_within_at.cos.differentiable_within_at @[simp] lemma differentiable_at.cos (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.cos (f x)) x := hc.has_fderiv_at.cos.differentiable_at lemma differentiable_on.cos (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.cos (f x)) s := λx h, (hc x h).cos @[simp] lemma differentiable.cos (hc : differentiable ℝ f) : differentiable ℝ (λx, real.cos (f x)) := λx, (hc x).cos lemma fderiv_within_cos (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.cos (f x)) s x = - real.sin (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.cos.fderiv_within hxs @[simp] lemma fderiv_cos (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.cos (f x)) x = - real.sin (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.cos.fderiv lemma times_cont_diff.cos {n} (h : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.cos (f x)) := real.times_cont_diff_cos.comp h lemma times_cont_diff_at.cos {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.cos (f x)) x := real.times_cont_diff_cos.times_cont_diff_at.comp x hf lemma times_cont_diff_on.cos {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.cos (f x)) s := real.times_cont_diff_cos.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.cos {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.cos (f x)) s x := real.times_cont_diff_cos.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `real.sin` -/ lemma has_fderiv_at.sin (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.sin (f x)) (real.cos (f x) • f') x := (real.has_deriv_at_sin (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.sin (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.sin (f x)) (real.cos (f x) • f') s x := (real.has_deriv_at_sin (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.sin (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.sin (f x)) s x := hf.has_fderiv_within_at.sin.differentiable_within_at @[simp] lemma differentiable_at.sin (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.sin (f x)) x := hc.has_fderiv_at.sin.differentiable_at lemma differentiable_on.sin (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.sin (f x)) s := λx h, (hc x h).sin @[simp] lemma differentiable.sin (hc : differentiable ℝ f) : differentiable ℝ (λx, real.sin (f x)) := λx, (hc x).sin lemma fderiv_within_sin (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.sin (f x)) s x = real.cos (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.sin.fderiv_within hxs @[simp] lemma fderiv_sin (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.sin (f x)) x = real.cos (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.sin.fderiv lemma times_cont_diff.sin {n} (h : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.sin (f x)) := real.times_cont_diff_sin.comp h lemma times_cont_diff_at.sin {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.sin (f x)) x := real.times_cont_diff_sin.times_cont_diff_at.comp x hf lemma times_cont_diff_on.sin {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.sin (f x)) s := real.times_cont_diff_sin.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.sin {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.sin (f x)) s x := real.times_cont_diff_sin.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `real.cosh` -/ lemma has_fderiv_at.cosh (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.cosh (f x)) (real.sinh (f x) • f') x := (real.has_deriv_at_cosh (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.cosh (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) • f') s x := (real.has_deriv_at_cosh (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.cosh (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.cosh (f x)) s x := hf.has_fderiv_within_at.cosh.differentiable_within_at @[simp] lemma differentiable_at.cosh (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.cosh (f x)) x := hc.has_fderiv_at.cosh.differentiable_at lemma differentiable_on.cosh (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.cosh (f x)) s := λx h, (hc x h).cosh @[simp] lemma differentiable.cosh (hc : differentiable ℝ f) : differentiable ℝ (λx, real.cosh (f x)) := λx, (hc x).cosh lemma fderiv_within_cosh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.cosh (f x)) s x = real.sinh (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.cosh.fderiv_within hxs @[simp] lemma fderiv_cosh (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.cosh (f x)) x = real.sinh (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.cosh.fderiv lemma times_cont_diff.cosh {n} (h : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.cosh (f x)) := real.times_cont_diff_cosh.comp h lemma times_cont_diff_at.cosh {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.cosh (f x)) x := real.times_cont_diff_cosh.times_cont_diff_at.comp x hf lemma times_cont_diff_on.cosh {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.cosh (f x)) s := real.times_cont_diff_cosh.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.cosh {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.cosh (f x)) s x := real.times_cont_diff_cosh.times_cont_diff_at.comp_times_cont_diff_within_at x hf /-! #### `real.sinh` -/ lemma has_fderiv_at.sinh (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.sinh (f x)) (real.cosh (f x) • f') x := (real.has_deriv_at_sinh (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.sinh (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) • f') s x := (real.has_deriv_at_sinh (f x)).comp_has_fderiv_within_at x hf lemma differentiable_within_at.sinh (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.sinh (f x)) s x := hf.has_fderiv_within_at.sinh.differentiable_within_at @[simp] lemma differentiable_at.sinh (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.sinh (f x)) x := hc.has_fderiv_at.sinh.differentiable_at lemma differentiable_on.sinh (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.sinh (f x)) s := λx h, (hc x h).sinh @[simp] lemma differentiable.sinh (hc : differentiable ℝ f) : differentiable ℝ (λx, real.sinh (f x)) := λx, (hc x).sinh lemma fderiv_within_sinh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.sinh (f x)) s x = real.cosh (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.sinh.fderiv_within hxs @[simp] lemma fderiv_sinh (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.sinh (f x)) x = real.cosh (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.sinh.fderiv lemma times_cont_diff.sinh {n} (h : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.sinh (f x)) := real.times_cont_diff_sinh.comp h lemma times_cont_diff_at.sinh {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.sinh (f x)) x := real.times_cont_diff_sinh.times_cont_diff_at.comp x hf lemma times_cont_diff_on.sinh {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.sinh (f x)) s := real.times_cont_diff_sinh.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.sinh {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.sinh (f x)) s x := real.times_cont_diff_sinh.times_cont_diff_at.comp_times_cont_diff_within_at x hf end namespace real lemma exists_cos_eq_zero : 0 ∈ cos '' Icc (1:ℝ) 2 := intermediate_value_Icc' (by norm_num) continuous_on_cos ⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩ /-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from which one can derive all its properties. For explicit bounds on π, see `data.real.pi`. -/ noncomputable def pi : ℝ := 2 * classical.some exists_cos_eq_zero localized "notation `π` := real.pi" in real @[simp] lemma cos_pi_div_two : cos (π / 2) = 0 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).2 lemma one_le_pi_div_two : (1 : ℝ) ≤ π / 2 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).1.1 lemma pi_div_two_le_two : π / 2 ≤ 2 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).1.2 lemma two_le_pi : (2 : ℝ) ≤ π := (div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1 (by rw div_self (@two_ne_zero' ℝ _ _ _); exact one_le_pi_div_two) lemma pi_le_four : π ≤ 4 := (div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1 (calc π / 2 ≤ 2 : pi_div_two_le_two ... = 4 / 2 : by norm_num) lemma pi_pos : 0 < π := lt_of_lt_of_le (by norm_num) two_le_pi lemma pi_ne_zero : π ≠ 0 := ne_of_gt pi_pos lemma pi_div_two_pos : 0 < π / 2 := half_pos pi_pos lemma two_pi_pos : 0 < 2 * π := by linarith [pi_pos] @[simp] lemma sin_pi : sin π = 0 := by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), two_mul, add_div, sin_add, cos_pi_div_two]; simp @[simp] lemma cos_pi : cos π = -1 := by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), mul_div_assoc, cos_two_mul, cos_pi_div_two]; simp [bit0, pow_add] @[simp] lemma sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] lemma cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x := by simp [sin_add] lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x := by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi] lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x := by simp [cos_add, cos_two_pi, sin_two_pi] lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x := by simp [cos_add] lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x := by simp [sub_eq_add_neg, cos_add] lemma sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x := if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2 else have (2 : ℝ) + 2 = 4, from rfl, have π - x ≤ 2, from sub_le_iff_le_add.2 (le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)), sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this lemma sin_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo 0 π) : 0 < sin x := sin_pos_of_pos_of_lt_pi hx.1 hx.2 lemma sin_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc 0 π) : 0 ≤ sin x := begin rw ← closure_Ioo pi_pos at hx, exact closure_lt_subset_le continuous_const continuous_sin (closure_mono (λ y, sin_pos_of_mem_Ioo) hx) end lemma sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x := sin_nonneg_of_mem_Icc ⟨h0x, hxp⟩ lemma sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 := neg_pos.1 $ sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx) lemma sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 := neg_nonneg.1 $ sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx) @[simp] lemma sin_pi_div_two : sin (π / 2) = 1 := have sin (π / 2) = 1 ∨ sin (π / 2) = -1 := by simpa [pow_two, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2), this.resolve_right (λ h, (show ¬(0 : ℝ) < -1, by norm_num) $ h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos)) lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x := by simp [sin_add] lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x := by simp [cos_add] lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] lemma cos_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : 0 < cos x := sin_add_pi_div_two x ▸ sin_pos_of_mem_Ioo ⟨by linarith [hx.1], by linarith [hx.2]⟩ lemma cos_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : 0 ≤ cos x := sin_add_pi_div_two x ▸ sin_nonneg_of_mem_Icc ⟨by linarith [hx.1], by linarith [hx.2]⟩ lemma cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 := neg_pos.1 $ cos_pi_sub x ▸ cos_pos_of_mem_Ioo ⟨by linarith, by linarith⟩ lemma cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) : cos x ≤ 0 := neg_nonneg.1 $ cos_pi_sub x ▸ cos_nonneg_of_mem_Icc ⟨by linarith, by linarith⟩ lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := by induction n; simp [add_mul, sin_add, *] lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi] lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi] lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe, int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg, (neg_mul_eq_neg_mul _ _).symm, cos_neg] lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simp [cos_add, sin_add, cos_int_mul_two_pi] lemma sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) : sin x = 0 ↔ x = 0 := ⟨λ h, le_antisymm (le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $ calc 0 < sin x : sin_pos_of_pos_of_lt_pi h0 hx₂ ... = 0 : h)) (le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $ calc 0 = sin x : h.symm ... < 0 : sin_neg_of_neg_of_neg_pi_lt h0 hx₁)), λ h, by simp [h]⟩ lemma sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x := ⟨λ h, ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (sub_floor_div_mul_nonneg _ pi_pos)) (sub_nonpos.1 $ le_of_not_gt $ λ h₃, (sin_pos_of_pos_of_lt_pi h₃ (sub_floor_div_mul_lt _ pi_pos)).ne (by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩, λ ⟨n, hn⟩, hn ▸ sin_int_mul_pi _⟩ lemma sin_ne_zero_iff {x : ℝ} : sin x ≠ 0 ↔ ∀ n : ℤ, (n : ℝ) * π ≠ x := by rw [← not_exists, not_iff_not, sin_eq_zero_iff] lemma sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 := by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq x, pow_two, pow_two, ← sub_eq_iff_eq_add, sub_self]; exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩ lemma cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x := ⟨λ h, let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (or.inl h)) in ⟨n / 2, (int.mod_two_eq_zero_or_one n).elim (λ hn0, by rwa [← mul_assoc, ← @int.cast_two ℝ, ← int.cast_mul, int.div_mul_cancel ((int.dvd_iff_mod_eq_zero _ _).2 hn0)]) (λ hn1, by rw [← int.mod_add_div n 2, hn1, int.cast_add, int.cast_one, add_mul, one_mul, add_comm, mul_comm (2 : ℤ), int.cast_mul, mul_assoc, int.cast_two] at hn; rw [← hn, cos_int_mul_two_pi_add_pi] at h; exact absurd h (by norm_num))⟩, λ ⟨n, hn⟩, hn ▸ cos_int_mul_two_pi _⟩ lemma cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) : cos x = 1 ↔ x = 0 := ⟨λ h, begin rcases (cos_eq_one_iff _).1 h with ⟨n, rfl⟩, rw [mul_lt_iff_lt_one_left two_pi_pos] at hx₂, rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos] at hx₁, norm_cast at hx₁ hx₂, obtain rfl : n = 0, by omega, simp end, λ h, by simp [h]⟩ lemma cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π / 2) (hxy : x < y) : cos y < cos x := begin rw [← sub_lt_zero, cos_sub_cos], have : 0 < sin ((y + x) / 2), { refine sin_pos_of_pos_of_lt_pi _ _; linarith }, have : 0 < sin ((y - x) / 2), { refine sin_pos_of_pos_of_lt_pi _ _; linarith }, nlinarith, end lemma cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x < y) : cos y < cos x := match (le_total x (π / 2) : x ≤ π / 2 ∨ π / 2 ≤ x), le_total y (π / 2) with | or.inl hx, or.inl hy := cos_lt_cos_of_nonneg_of_le_pi_div_two hx₁ hy hxy | or.inl hx, or.inr hy := (lt_or_eq_of_le hx).elim (λ hx, calc cos y ≤ 0 : cos_nonpos_of_pi_div_two_le_of_le hy (by linarith [pi_pos]) ... < cos x : cos_pos_of_mem_Ioo ⟨by linarith, hx⟩) (λ hx, calc cos y < 0 : cos_neg_of_pi_div_two_lt_of_lt (by linarith) (by linarith [pi_pos]) ... = cos x : by rw [hx, cos_pi_div_two]) | or.inr hx, or.inl hy := by linarith | or.inr hx, or.inr hy := neg_lt_neg_iff.1 (by rw [← cos_pi_sub, ← cos_pi_sub]; apply cos_lt_cos_of_nonneg_of_le_pi_div_two; linarith) end lemma strict_mono_decr_on_cos : strict_mono_decr_on cos (Icc 0 π) := λ x hx y hy hxy, cos_lt_cos_of_nonneg_of_le_pi hx.1 hy.2 hxy lemma cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x ≤ y) : cos y ≤ cos x := (strict_mono_decr_on_cos.le_iff_le ⟨hx₁.trans hxy, hy₂⟩ ⟨hx₁, hxy.trans hy₂⟩).2 hxy lemma sin_lt_sin_of_lt_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y := by rw [← cos_sub_pi_div_two, ← cos_sub_pi_div_two, ← cos_neg (x - _), ← cos_neg (y - _)]; apply cos_lt_cos_of_nonneg_of_le_pi; linarith lemma strict_mono_incr_on_sin : strict_mono_incr_on sin (Icc (-(π / 2)) (π / 2)) := λ x hx y hy hxy, sin_lt_sin_of_lt_of_le_pi_div_two hx.1 hy.2 hxy lemma sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y := (strict_mono_incr_on_sin.le_iff_le ⟨hx₁, hxy.trans hy₂⟩ ⟨hx₁.trans hxy, hy₂⟩).2 hxy lemma inj_on_sin : inj_on sin (Icc (-(π / 2)) (π / 2)) := strict_mono_incr_on_sin.inj_on lemma inj_on_cos : inj_on cos (Icc 0 π) := strict_mono_decr_on_cos.inj_on lemma surj_on_sin : surj_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) := by simpa only [sin_neg, sin_pi_div_two] using intermediate_value_Icc (neg_le_self pi_div_two_pos.le) continuous_sin.continuous_on lemma surj_on_cos : surj_on cos (Icc 0 π) (Icc (-1) 1) := by simpa only [cos_zero, cos_pi] using intermediate_value_Icc' pi_pos.le continuous_cos.continuous_on lemma sin_mem_Icc (x : ℝ) : sin x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_sin x, sin_le_one x⟩ lemma cos_mem_Icc (x : ℝ) : cos x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_cos x, cos_le_one x⟩ lemma maps_to_sin (s : set ℝ) : maps_to sin s (Icc (-1 : ℝ) 1) := λ x _, sin_mem_Icc x lemma maps_to_cos (s : set ℝ) : maps_to cos s (Icc (-1 : ℝ) 1) := λ x _, cos_mem_Icc x lemma bij_on_sin : bij_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) := ⟨maps_to_sin _, inj_on_sin, surj_on_sin⟩ lemma bij_on_cos : bij_on cos (Icc 0 π) (Icc (-1) 1) := ⟨maps_to_cos _, inj_on_cos, surj_on_cos⟩ @[simp] lemma range_cos : range cos = (Icc (-1) 1 : set ℝ) := subset.antisymm (range_subset_iff.2 cos_mem_Icc) surj_on_cos.subset_range @[simp] lemma range_sin : range sin = (Icc (-1) 1 : set ℝ) := subset.antisymm (range_subset_iff.2 sin_mem_Icc) surj_on_sin.subset_range lemma range_cos_infinite : (range real.cos).infinite := by { rw real.range_cos, exact Icc.infinite (by norm_num) } lemma range_sin_infinite : (range real.sin).infinite := by { rw real.range_sin, exact Icc.infinite (by norm_num) } lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x := begin cases le_or_gt x 1 with h' h', { have hx : abs x = x := abs_of_nonneg (le_of_lt h), have : abs x ≤ 1, rwa [hx], have := sin_bound this, rw [abs_le] at this, have := this.2, rw [sub_le_iff_le_add', hx] at this, apply lt_of_le_of_lt this, rw [sub_add], apply lt_of_lt_of_le _ (le_of_eq (sub_zero x)), apply sub_lt_sub_left, rw [sub_pos, div_eq_mul_inv (x ^ 3)], apply mul_lt_mul', { rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)), rw mul_le_mul_right, exact h', apply pow_pos h }, norm_num, norm_num, apply pow_pos h }, exact lt_of_le_of_lt (sin_le_one x) h' end /- note 1: this inequality is not tight, the tighter inequality is sin x > x - x ^ 3 / 6. note 2: this is also true for x > 1, but it's nontrivial for x just above 1. -/ lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x := begin have hx : abs x = x := abs_of_nonneg (le_of_lt h), have : abs x ≤ 1, rwa [hx], have := sin_bound this, rw [abs_le] at this, have := this.1, rw [le_sub_iff_add_le, hx] at this, refine lt_of_lt_of_le _ this, rw [add_comm, sub_add, sub_neg_eq_add], apply sub_lt_sub_left, apply add_lt_of_lt_sub_left, rw (show x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 * 12⁻¹, by simp [div_eq_mul_inv, ← mul_sub]; norm_num), apply mul_lt_mul', { rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)), rw mul_le_mul_right, exact h', apply pow_pos h }, norm_num, norm_num, apply pow_pos h end section cos_div_pow_two variable (x : ℝ) /-- the series `sqrt_two_add_series x n` is `sqrt(2 + sqrt(2 + ... ))` with `n` square roots, starting with `x`. We define it here because `cos (pi / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2` -/ @[simp, pp_nodot] noncomputable def sqrt_two_add_series (x : ℝ) : ℕ → ℝ | 0 := x | (n+1) := sqrt (2 + sqrt_two_add_series n) lemma sqrt_two_add_series_zero : sqrt_two_add_series x 0 = x := by simp lemma sqrt_two_add_series_one : sqrt_two_add_series 0 1 = sqrt 2 := by simp lemma sqrt_two_add_series_two : sqrt_two_add_series 0 2 = sqrt (2 + sqrt 2) := by simp lemma sqrt_two_add_series_zero_nonneg : ∀(n : ℕ), 0 ≤ sqrt_two_add_series 0 n | 0 := le_refl 0 | (n+1) := sqrt_nonneg _ lemma sqrt_two_add_series_nonneg {x : ℝ} (h : 0 ≤ x) : ∀(n : ℕ), 0 ≤ sqrt_two_add_series x n | 0 := h | (n+1) := sqrt_nonneg _ lemma sqrt_two_add_series_lt_two : ∀(n : ℕ), sqrt_two_add_series 0 n < 2 | 0 := by norm_num | (n+1) := begin refine lt_of_lt_of_le _ (le_of_eq $ sqrt_sqr $ le_of_lt zero_lt_two), rw [sqrt_two_add_series, sqrt_lt, ← lt_sub_iff_add_lt'], { refine (sqrt_two_add_series_lt_two n).trans_le _, norm_num }, { exact add_nonneg zero_le_two (sqrt_two_add_series_zero_nonneg n) } end lemma sqrt_two_add_series_succ (x : ℝ) : ∀(n : ℕ), sqrt_two_add_series x (n+1) = sqrt_two_add_series (sqrt (2 + x)) n | 0 := rfl | (n+1) := by rw [sqrt_two_add_series, sqrt_two_add_series_succ, sqrt_two_add_series] lemma sqrt_two_add_series_monotone_left {x y : ℝ} (h : x ≤ y) : ∀(n : ℕ), sqrt_two_add_series x n ≤ sqrt_two_add_series y n | 0 := h | (n+1) := begin rw [sqrt_two_add_series, sqrt_two_add_series], exact sqrt_le_sqrt (add_le_add_left (sqrt_two_add_series_monotone_left _) _) end @[simp] lemma cos_pi_over_two_pow : ∀(n : ℕ), cos (pi / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2 | 0 := by simp | (n+1) := begin have : (2 : ℝ) ≠ 0 := two_ne_zero, symmetry, rw [div_eq_iff_mul_eq this], symmetry, rw [sqrt_two_add_series, sqrt_eq_iff_sqr_eq, mul_pow, cos_square, ←mul_div_assoc, nat.add_succ, pow_succ, mul_div_mul_left _ _ this, cos_pi_over_two_pow, add_mul], congr, { norm_num }, rw [mul_comm, pow_two, mul_assoc, ←mul_div_assoc, mul_div_cancel_left, ←mul_div_assoc, mul_div_cancel_left]; try { exact this }, apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg, norm_num, apply le_of_lt, apply cos_pos_of_mem_Ioo ⟨_, _⟩, { transitivity (0 : ℝ), rw neg_lt_zero, apply pi_div_two_pos, apply div_pos pi_pos, apply pow_pos, norm_num }, apply div_lt_div' (le_refl pi) _ pi_pos _, refine lt_of_le_of_lt (le_of_eq (pow_one _).symm) _, apply pow_lt_pow, norm_num, apply nat.succ_lt_succ, apply nat.succ_pos, all_goals {norm_num} end lemma sin_square_pi_over_two_pow (n : ℕ) : sin (pi / 2 ^ (n+1)) ^ 2 = 1 - (sqrt_two_add_series 0 n / 2) ^ 2 := by rw [sin_square, cos_pi_over_two_pow] lemma sin_square_pi_over_two_pow_succ (n : ℕ) : sin (pi / 2 ^ (n+2)) ^ 2 = 1 / 2 - sqrt_two_add_series 0 n / 4 := begin rw [sin_square_pi_over_two_pow, sqrt_two_add_series, div_pow, sqr_sqrt, add_div, ←sub_sub], congr, norm_num, norm_num, apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg, end @[simp] lemma sin_pi_over_two_pow_succ (n : ℕ) : sin (pi / 2 ^ (n+2)) = sqrt (2 - sqrt_two_add_series 0 n) / 2 := begin symmetry, rw [div_eq_iff_mul_eq], symmetry, rw [sqrt_eq_iff_sqr_eq, mul_pow, sin_square_pi_over_two_pow_succ, sub_mul], { congr, norm_num, rw [mul_comm], convert mul_div_cancel' _ _, norm_num, norm_num }, { rw [sub_nonneg], apply le_of_lt, apply sqrt_two_add_series_lt_two }, apply le_of_lt, apply mul_pos, apply sin_pos_of_pos_of_lt_pi, { apply div_pos pi_pos, apply pow_pos, norm_num }, refine lt_of_lt_of_le _ (le_of_eq (div_one _)), rw [div_lt_div_left], refine lt_of_le_of_lt (le_of_eq (pow_zero 2).symm) _, apply pow_lt_pow, norm_num, apply nat.succ_pos, apply pi_pos, apply pow_pos, all_goals {norm_num} end @[simp] lemma cos_pi_div_four : cos (pi / 4) = sqrt 2 / 2 := by { transitivity cos (pi / 2 ^ 2), congr, norm_num, simp } @[simp] lemma sin_pi_div_four : sin (pi / 4) = sqrt 2 / 2 := by { transitivity sin (pi / 2 ^ 2), congr, norm_num, simp } @[simp] lemma cos_pi_div_eight : cos (pi / 8) = sqrt (2 + sqrt 2) / 2 := by { transitivity cos (pi / 2 ^ 3), congr, norm_num, simp } @[simp] lemma sin_pi_div_eight : sin (pi / 8) = sqrt (2 - sqrt 2) / 2 := by { transitivity sin (pi / 2 ^ 3), congr, norm_num, simp } @[simp] lemma cos_pi_div_sixteen : cos (pi / 16) = sqrt (2 + sqrt (2 + sqrt 2)) / 2 := by { transitivity cos (pi / 2 ^ 4), congr, norm_num, simp } @[simp] lemma sin_pi_div_sixteen : sin (pi / 16) = sqrt (2 - sqrt (2 + sqrt 2)) / 2 := by { transitivity sin (pi / 2 ^ 4), congr, norm_num, simp } @[simp] lemma cos_pi_div_thirty_two : cos (pi / 32) = sqrt (2 + sqrt (2 + sqrt (2 + sqrt 2))) / 2 := by { transitivity cos (pi / 2 ^ 5), congr, norm_num, simp } @[simp] lemma sin_pi_div_thirty_two : sin (pi / 32) = sqrt (2 - sqrt (2 + sqrt (2 + sqrt 2))) / 2 := by { transitivity sin (pi / 2 ^ 5), congr, norm_num, simp } -- This section is also a convenient location for other explicit values of `sin` and `cos`. /-- The cosine of `π / 3` is `1 / 2`. -/ @[simp] lemma cos_pi_div_three : cos (π / 3) = 1 / 2 := begin have h₁ : (2 * cos (π / 3) - 1) ^ 2 * (2 * cos (π / 3) + 2) = 0, { have : cos (3 * (π / 3)) = cos π := by { congr' 1, ring }, linarith [cos_pi, cos_three_mul (π / 3)] }, cases mul_eq_zero.mp h₁ with h h, { linarith [pow_eq_zero h] }, { have : cos π < cos (π / 3), { refine cos_lt_cos_of_nonneg_of_le_pi _ rfl.ge _; linarith [pi_pos] }, linarith [cos_pi] } end /-- The square of the cosine of `π / 6` is `3 / 4` (this is sometimes more convenient than the result for cosine itself). -/ lemma square_cos_pi_div_six : cos (π / 6) ^ 2 = 3 / 4 := begin have h1 : cos (π / 6) ^ 2 = 1 / 2 + 1 / 2 / 2, { convert cos_square (π / 6), have h2 : 2 * (π / 6) = π / 3 := by cancel_denoms, rw [h2, cos_pi_div_three] }, rw ← sub_eq_zero at h1 ⊢, convert h1 using 1, ring end /-- The cosine of `π / 6` is `√3 / 2`. -/ @[simp] lemma cos_pi_div_six : cos (π / 6) = (sqrt 3) / 2 := begin suffices : sqrt 3 = cos (π / 6) * 2, { field_simp [(by norm_num : 0 ≠ 2)], exact this.symm }, rw sqrt_eq_iff_sqr_eq, { have h1 := (mul_right_inj' (by norm_num : (4:ℝ) ≠ 0)).mpr square_cos_pi_div_six, rw ← sub_eq_zero at h1 ⊢, convert h1 using 1, ring }, { norm_num }, { have : 0 < cos (π / 6) := by { apply cos_pos_of_mem_Ioo; split; linarith [pi_pos] }, linarith }, end /-- The sine of `π / 6` is `1 / 2`. -/ @[simp] lemma sin_pi_div_six : sin (π / 6) = 1 / 2 := begin rw [← cos_pi_div_two_sub, ← cos_pi_div_three], congr, ring end /-- The square of the sine of `π / 3` is `3 / 4` (this is sometimes more convenient than the result for cosine itself). -/ lemma square_sin_pi_div_three : sin (π / 3) ^ 2 = 3 / 4 := begin rw [← cos_pi_div_two_sub, ← square_cos_pi_div_six], congr, ring end /-- The sine of `π / 3` is `√3 / 2`. -/ @[simp] lemma sin_pi_div_three : sin (π / 3) = (sqrt 3) / 2 := begin rw [← cos_pi_div_two_sub, ← cos_pi_div_six], congr, ring end end cos_div_pow_two /-- The type of angles -/ def angle : Type := quotient_add_group.quotient (add_subgroup.gmultiples (2 * π)) namespace angle instance angle.add_comm_group : add_comm_group angle := quotient_add_group.add_comm_group _ instance : inhabited angle := ⟨0⟩ instance angle.has_coe : has_coe ℝ angle := ⟨quotient.mk'⟩ @[simp] lemma coe_zero : ↑(0 : ℝ) = (0 : angle) := rfl @[simp] lemma coe_add (x y : ℝ) : ↑(x + y : ℝ) = (↑x + ↑y : angle) := rfl @[simp] lemma coe_neg (x : ℝ) : ↑(-x : ℝ) = -(↑x : angle) := rfl @[simp] lemma coe_sub (x y : ℝ) : ↑(x - y : ℝ) = (↑x - ↑y : angle) := by rw [sub_eq_add_neg, sub_eq_add_neg, coe_add, coe_neg] @[simp, norm_cast] lemma coe_nat_mul_eq_nsmul (x : ℝ) (n : ℕ) : ↑((n : ℝ) * x) = n •ℕ (↑x : angle) := by simpa using add_monoid_hom.map_nsmul ⟨coe, coe_zero, coe_add⟩ _ _ @[simp, norm_cast] lemma coe_int_mul_eq_gsmul (x : ℝ) (n : ℤ) : ↑((n : ℝ) * x : ℝ) = n •ℤ (↑x : angle) := by simpa using add_monoid_hom.map_gsmul ⟨coe, coe_zero, coe_add⟩ _ _ @[simp] lemma coe_two_pi : ↑(2 * π : ℝ) = (0 : angle) := quotient.sound' ⟨-1, show (-1 : ℤ) •ℤ (2 * π) = _, by rw [neg_one_gsmul, add_zero]⟩ lemma angle_eq_iff_two_pi_dvd_sub {ψ θ : ℝ} : (θ : angle) = ψ ↔ ∃ k : ℤ, θ - ψ = 2 * π * k := by simp only [quotient_add_group.eq, add_subgroup.gmultiples_eq_closure, add_subgroup.mem_closure_singleton, gsmul_eq_mul', (sub_eq_neg_add _ _).symm, eq_comm] theorem cos_eq_iff_eq_or_eq_neg {θ ψ : ℝ} : cos θ = cos ψ ↔ (θ : angle) = ψ ∨ (θ : angle) = -ψ := begin split, { intro Hcos, rw [← sub_eq_zero, cos_sub_cos, mul_eq_zero, mul_eq_zero, neg_eq_zero, eq_false_intro two_ne_zero, false_or, sin_eq_zero_iff, sin_eq_zero_iff] at Hcos, rcases Hcos with ⟨n, hn⟩ | ⟨n, hn⟩, { right, rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), ← sub_eq_iff_eq_add] at hn, rw [← hn, coe_sub, eq_neg_iff_add_eq_zero, sub_add_cancel, mul_assoc, coe_int_mul_eq_gsmul, mul_comm, coe_two_pi, gsmul_zero] }, { left, rw [eq_div_iff_mul_eq (@two_ne_zero ℝ _ _), eq_sub_iff_add_eq] at hn, rw [← hn, coe_add, mul_assoc, coe_int_mul_eq_gsmul, mul_comm, coe_two_pi, gsmul_zero, zero_add] }, apply_instance, }, { rw [angle_eq_iff_two_pi_dvd_sub, ← coe_neg, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero_iff_eq, cos_sub_cos, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero], rw [← sub_eq_zero_iff_eq, cos_sub_cos, ← sub_neg_eq_add, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul] } end theorem sin_eq_iff_eq_or_add_eq_pi {θ ψ : ℝ} : sin θ = sin ψ ↔ (θ : angle) = ψ ∨ (θ : angle) + ψ = π := begin split, { intro Hsin, rw [← cos_pi_div_two_sub, ← cos_pi_div_two_sub] at Hsin, cases cos_eq_iff_eq_or_eq_neg.mp Hsin with h h, { left, rw [coe_sub, coe_sub] at h, exact sub_right_inj.1 h }, right, rw [coe_sub, coe_sub, eq_neg_iff_add_eq_zero, add_sub, sub_add_eq_add_sub, ← coe_add, add_halves, sub_sub, sub_eq_zero] at h, exact h.symm }, { rw [angle_eq_iff_two_pi_dvd_sub, ←eq_sub_iff_add_eq, ←coe_sub, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero_iff_eq, sin_sub_sin, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul], have H' : θ + ψ = (2 * k) * π + π := by rwa [←sub_add, sub_add_eq_add_sub, sub_eq_iff_eq_add, mul_assoc, mul_comm π _, ←mul_assoc] at H, rw [← sub_eq_zero_iff_eq, sin_sub_sin, H', add_div, mul_assoc 2 _ π, mul_div_cancel_left _ (@two_ne_zero ℝ _ _), cos_add_pi_div_two, sin_int_mul_pi, neg_zero, mul_zero] } end theorem cos_sin_inj {θ ψ : ℝ} (Hcos : cos θ = cos ψ) (Hsin : sin θ = sin ψ) : (θ : angle) = ψ := begin cases cos_eq_iff_eq_or_eq_neg.mp Hcos with hc hc, { exact hc }, cases sin_eq_iff_eq_or_add_eq_pi.mp Hsin with hs hs, { exact hs }, rw [eq_neg_iff_add_eq_zero, hs] at hc, cases quotient.exact' hc with n hn, change n •ℤ _ = _ at hn, rw [← neg_one_mul, add_zero, ← sub_eq_zero_iff_eq, gsmul_eq_mul, ← mul_assoc, ← sub_mul, mul_eq_zero, eq_false_intro (ne_of_gt pi_pos), or_false, sub_neg_eq_add, ← int.cast_zero, ← int.cast_one, ← int.cast_bit0, ← int.cast_mul, ← int.cast_add, int.cast_inj] at hn, have : (n * 2 + 1) % (2:ℤ) = 0 % (2:ℤ) := congr_arg (%(2:ℤ)) hn, rw [add_comm, int.add_mul_mod_self] at this, exact absurd this one_ne_zero end end angle /-- `real.sin` as an `order_iso` between `[-(π / 2), π / 2]` and `[-1, 1]`. -/ def sin_order_iso : Icc (-(π / 2)) (π / 2) ≃o Icc (-1:ℝ) 1 := (strict_mono_incr_on_sin.order_iso _ _).trans $ order_iso.set_congr _ _ bij_on_sin.image_eq @[simp] lemma coe_sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) : (sin_order_iso x : ℝ) = sin x := rfl lemma sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) : sin_order_iso x = ⟨sin x, sin_mem_Icc x⟩ := rfl /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`. It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arcsin : ℝ → ℝ := coe ∘ Icc_extend (neg_le_self zero_le_one) sin_order_iso.symm lemma arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := subtype.coe_prop _ @[simp] lemma range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) := by { rw [arcsin, range_comp coe], simp [Icc] } lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2 lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1 lemma arcsin_proj_Icc (x : ℝ) : arcsin (proj_Icc (-1) 1 (neg_le_self $ @zero_le_one ℝ _) x) = arcsin x := by rw [arcsin, function.comp_app, Icc_extend_coe, function.comp_app, Icc_extend] lemma sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x := by simpa [arcsin, Icc_extend_of_mem _ _ hx, -order_iso.apply_symm_apply] using subtype.ext_iff.1 (sin_order_iso.apply_symm_apply ⟨x, hx⟩) lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := sin_arcsin' ⟨hx₁, hx₂⟩ lemma arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x := inj_on_sin (arcsin_mem_Icc _) hx $ by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)] lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := arcsin_sin' ⟨hx₁, hx₂⟩ lemma strict_mono_incr_on_arcsin : strict_mono_incr_on arcsin (Icc (-1) 1) := (subtype.strict_mono_coe _).comp_strict_mono_incr_on $ sin_order_iso.symm.strict_mono.strict_mono_incr_on_Icc_extend _ lemma monotone_arcsin : monotone arcsin := (subtype.mono_coe _).comp $ sin_order_iso.symm.monotone.Icc_extend _ lemma inj_on_arcsin : inj_on arcsin (Icc (-1) 1) := strict_mono_incr_on_arcsin.inj_on lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arcsin x = arcsin y ↔ x = y := inj_on_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ lemma continuous_arcsin : continuous arcsin := continuous_subtype_coe.comp sin_order_iso.symm.continuous.Icc_extend lemma continuous_at_arcsin {x : ℝ} : continuous_at arcsin x := continuous_arcsin.continuous_at lemma arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin y = x := begin subst y, exact inj_on_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x)) end @[simp] lemma arcsin_zero : arcsin 0 = 0 := arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩ @[simp] lemma arcsin_one : arcsin 1 = π / 2 := arcsin_eq_of_sin_eq sin_pi_div_two $ right_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 := by rw [← arcsin_proj_Icc, proj_Icc_of_right_le _ hx, subtype.coe_mk, arcsin_one] lemma arcsin_neg_one : arcsin (-1) = -(π / 2) := arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) $ left_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) := by rw [← arcsin_proj_Icc, proj_Icc_of_le_left _ hx, subtype.coe_mk, arcsin_neg_one] @[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := begin cases le_total x (-1) with hx₁ hx₁, { rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] }, cases le_total 1 x with hx₂ hx₂, { rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] }, refine arcsin_eq_of_sin_eq _ _, { rw [sin_neg, sin_arcsin hx₁ hx₂] }, { exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ } end lemma arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rw [← arcsin_sin' hy, strict_mono_incr_on_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy] lemma arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := begin cases le_total x (-1) with hx₁ hx₁, { simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] }, cases lt_or_le 1 x with hx₂ hx₂, { simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] }, exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy) end lemma le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le hy hx).trans not_le lemma arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le' hy).trans not_le lemma lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin hy hx).trans not_le lemma lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin' hx).trans not_le lemma arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) : arcsin x = y ↔ x = sin y := by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy), le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)] @[simp] lemma arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x := (le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans $ by rw [sin_zero] @[simp] lemma arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 := neg_nonneg.symm.trans $ arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg @[simp] lemma arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 := by simp [le_antisymm_iff] @[simp] lemma zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 := eq_comm.trans arcsin_eq_zero_iff @[simp] lemma arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x := lt_iff_lt_of_le_iff_le arcsin_nonpos @[simp] lemma arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 := lt_iff_lt_of_le_iff_le arcsin_nonneg @[simp] lemma arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 := (arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 $ neg_lt_self pi_div_two_pos)).trans $ by rw sin_pi_div_two @[simp] lemma neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x := (lt_arcsin_iff_sin_lt' $ left_mem_Ico.2 $ neg_lt_self pi_div_two_pos).trans $ by rw [sin_neg, sin_pi_div_two] @[simp] lemma arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x := ⟨λ h, not_lt.1 $ λ h', (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩ @[simp] lemma pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x := eq_comm.trans arcsin_eq_pi_div_two @[simp] lemma pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x := (arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin @[simp] lemma arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 := ⟨λ h, not_lt.1 $ λ h', (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩ @[simp] lemma neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 := eq_comm.trans arcsin_eq_neg_pi_div_two @[simp] lemma arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 := (neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two lemma maps_to_sin_Ioo : maps_to sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) := λ x h, by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin, arcsin_sin h.1.le h.2.le] /-- `real.sin` as a `local_homeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/ @[simp] def sin_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := sin, inv_fun := arcsin, source := Ioo (-(π / 2)) (π / 2), target := Ioo (-1) 1, map_source' := maps_to_sin_Ioo, map_target' := λ y hy, ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩, left_inv' := λ x hx, arcsin_sin hx.1.le hx.2.le, right_inv' := λ y hy, sin_arcsin hy.1.le hy.2.le, open_source := is_open_Ioo, open_target := is_open_Ioo, continuous_to_fun := continuous_sin.continuous_on, continuous_inv_fun := continuous_arcsin.continuous_on } lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩ lemma cos_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arcsin x) = sqrt (1 - x ^ 2) := have sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x), begin rw [← eq_sub_iff_add_eq', ← sqrt_inj (pow_two_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), pow_two, sqrt_mul_self (cos_arcsin_nonneg _)] at this, rw [this, sin_arcsin hx₁ hx₂], end lemma deriv_arcsin_aux {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) : has_deriv_at arcsin (1 / sqrt (1 - x ^ 2)) x ∧ times_cont_diff_at ℝ ⊤ arcsin x := begin cases h₁.lt_or_lt with h₁ h₁, { have : 1 - x ^ 2 < 0, by nlinarith [h₁], rw [sqrt_eq_zero'.2 this.le, div_zero], have : arcsin =ᶠ[𝓝 x] λ _, -(π / 2) := (gt_mem_nhds h₁).mono (λ y hy, arcsin_of_le_neg_one hy.le), exact ⟨(has_deriv_at_const _ _).congr_of_eventually_eq this, times_cont_diff_at_const.congr_of_eventually_eq this⟩ }, cases h₂.lt_or_lt with h₂ h₂, { have : 0 < sqrt (1 - x ^ 2) := sqrt_pos.2 (by nlinarith [h₁, h₂]), simp only [← cos_arcsin h₁.le h₂.le, one_div] at this ⊢, exact ⟨sin_local_homeomorph.has_deriv_at_symm ⟨h₁, h₂⟩ this.ne' (has_deriv_at_sin _), sin_local_homeomorph.times_cont_diff_at_symm_deriv this.ne' ⟨h₁, h₂⟩ (has_deriv_at_sin _) times_cont_diff_sin.times_cont_diff_at⟩ }, { have : 1 - x ^ 2 < 0, by nlinarith [h₂], rw [sqrt_eq_zero'.2 this.le, div_zero], have : arcsin =ᶠ[𝓝 x] λ _, π / 2 := (lt_mem_nhds h₂).mono (λ y hy, arcsin_of_one_le hy.le), exact ⟨(has_deriv_at_const _ _).congr_of_eventually_eq this, times_cont_diff_at_const.congr_of_eventually_eq this⟩ } end lemma has_deriv_at_arcsin {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) : has_deriv_at arcsin (1 / sqrt (1 - x ^ 2)) x := (deriv_arcsin_aux h₁ h₂).1 lemma times_cont_diff_at_arcsin {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) {n : with_top ℕ} : times_cont_diff_at ℝ n arcsin x := (deriv_arcsin_aux h₁ h₂).2.of_le le_top lemma has_deriv_within_at_arcsin_Ici {x : ℝ} (h : x ≠ -1) : has_deriv_within_at arcsin (1 / sqrt (1 - x ^ 2)) (Ici x) x := begin rcases em (x = 1) with (rfl|h'), { convert (has_deriv_within_at_const _ _ (π / 2)).congr _ _; simp [arcsin_of_one_le] { contextual := tt } }, { exact (has_deriv_at_arcsin h h').has_deriv_within_at } end lemma has_deriv_within_at_arcsin_Iic {x : ℝ} (h : x ≠ 1) : has_deriv_within_at arcsin (1 / sqrt (1 - x ^ 2)) (Iic x) x := begin rcases em (x = -1) with (rfl|h'), { convert (has_deriv_within_at_const _ _ (-(π / 2))).congr _ _; simp [arcsin_of_le_neg_one] { contextual := tt } }, { exact (has_deriv_at_arcsin h' h).has_deriv_within_at } end lemma differentiable_within_at_arcsin_Ici {x : ℝ} : differentiable_within_at ℝ arcsin (Ici x) x ↔ x ≠ -1 := begin refine ⟨_, λ h, (has_deriv_within_at_arcsin_Ici h).differentiable_within_at⟩, rintro h rfl, have : sin ∘ arcsin =ᶠ[𝓝[Ici (-1:ℝ)] (-1)] id, { filter_upwards [Icc_mem_nhds_within_Ici ⟨le_rfl, neg_lt_self (@zero_lt_one ℝ _ _)⟩], exact λ x, sin_arcsin' }, have := h.has_deriv_within_at.sin.congr_of_eventually_eq this.symm (by simp), simpa using (unique_diff_on_Ici _ _ left_mem_Ici).eq_deriv _ this (has_deriv_within_at_id _ _) end lemma differentiable_within_at_arcsin_Iic {x : ℝ} : differentiable_within_at ℝ arcsin (Iic x) x ↔ x ≠ 1 := begin refine ⟨λ h, _, λ h, (has_deriv_within_at_arcsin_Iic h).differentiable_within_at⟩, rw [← neg_neg x, ← image_neg_Ici] at h, have := (h.comp (-x) differentiable_within_at_id.neg (maps_to_image _ _)).neg, simpa [(∘), differentiable_within_at_arcsin_Ici] using this end lemma differentiable_at_arcsin {x : ℝ} : differentiable_at ℝ arcsin x ↔ x ≠ -1 ∧ x ≠ 1 := ⟨λ h, ⟨differentiable_within_at_arcsin_Ici.1 h.differentiable_within_at, differentiable_within_at_arcsin_Iic.1 h.differentiable_within_at⟩, λ h, (has_deriv_at_arcsin h.1 h.2).differentiable_at⟩ @[simp] lemma deriv_arcsin : deriv arcsin = λ x, 1 / sqrt (1 - x ^ 2) := begin funext x, by_cases h : x ≠ -1 ∧ x ≠ 1, { exact (has_deriv_at_arcsin h.1 h.2).deriv }, { rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_arcsin.1 h)], simp only [not_and_distrib, ne.def, not_not] at h, rcases h with (rfl|rfl); simp } end lemma differentiable_on_arcsin : differentiable_on ℝ arcsin {-1, 1}ᶜ := λ x hx, (differentiable_at_arcsin.2 ⟨λ h, hx (or.inl h), λ h, hx (or.inr h)⟩).differentiable_within_at lemma times_cont_diff_on_arcsin {n : with_top ℕ} : times_cont_diff_on ℝ n arcsin {-1, 1}ᶜ := λ x hx, (times_cont_diff_at_arcsin (mt or.inl hx) (mt or.inr hx)).times_cont_diff_within_at lemma times_cont_diff_at_arcsin_iff {x : ℝ} {n : with_top ℕ} : times_cont_diff_at ℝ n arcsin x ↔ n = 0 ∨ (x ≠ -1 ∧ x ≠ 1) := ⟨λ h, or_iff_not_imp_left.2 $ λ hn, differentiable_at_arcsin.1 $ h.differentiable_at $ with_top.one_le_iff_pos.2 (pos_iff_ne_zero.2 hn), λ h, h.elim (λ hn, hn.symm ▸ (times_cont_diff_zero.2 continuous_arcsin).times_cont_diff_at) $ λ hx, times_cont_diff_at_arcsin hx.1 hx.2⟩ lemma measurable_arcsin : measurable arcsin := continuous_arcsin.measurable /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. If the argument is not between `-1` and `1` it defaults to `π / 2` -/ @[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [arccos] lemma arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma strict_mono_decr_on_arccos : strict_mono_decr_on arccos (Icc (-1) 1) := λ x hx y hy h, sub_lt_sub_left (strict_mono_incr_on_arcsin hx hy h) _ lemma arccos_inj_on : inj_on arccos (Icc (-1) 1) := strict_mono_decr_on_arccos.inj_on lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arccos x = arccos y ↔ x = y := arccos_inj_on.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] @[simp] lemma arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x := by simp [arccos, sub_eq_zero] @[simp] lemma arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 := by simp [arccos, sub_eq_iff_eq_add] @[simp] lemma arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 := by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin] lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add] lemma sin_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arccos x) = sqrt (1 - x ^ 2) := by rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin hx₁ hx₂] lemma continuous_arccos : continuous arccos := continuous_const.sub continuous_arcsin lemma has_deriv_at_arccos {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) : has_deriv_at arccos (-(1 / sqrt (1 - x ^ 2))) x := (has_deriv_at_arcsin h₁ h₂).const_sub (π / 2) lemma times_cont_diff_at_arccos {x : ℝ} (h₁ : x ≠ -1) (h₂ : x ≠ 1) {n : with_top ℕ} : times_cont_diff_at ℝ n arccos x := times_cont_diff_at_const.sub (times_cont_diff_at_arcsin h₁ h₂) lemma has_deriv_within_at_arccos_Ici {x : ℝ} (h : x ≠ -1) : has_deriv_within_at arccos (-(1 / sqrt (1 - x ^ 2))) (Ici x) x := (has_deriv_within_at_arcsin_Ici h).const_sub _ lemma has_deriv_within_at_arccos_Iic {x : ℝ} (h : x ≠ 1) : has_deriv_within_at arccos (-(1 / sqrt (1 - x ^ 2))) (Iic x) x := (has_deriv_within_at_arcsin_Iic h).const_sub _ lemma differentiable_within_at_arccos_Ici {x : ℝ} : differentiable_within_at ℝ arccos (Ici x) x ↔ x ≠ -1 := (differentiable_within_at_const_sub_iff _).trans differentiable_within_at_arcsin_Ici lemma differentiable_within_at_arccos_Iic {x : ℝ} : differentiable_within_at ℝ arccos (Iic x) x ↔ x ≠ 1 := (differentiable_within_at_const_sub_iff _).trans differentiable_within_at_arcsin_Iic lemma differentiable_at_arccos {x : ℝ} : differentiable_at ℝ arccos x ↔ x ≠ -1 ∧ x ≠ 1 := (differentiable_at_const_sub_iff _).trans differentiable_at_arcsin @[simp] lemma deriv_arccos : deriv arccos = λ x, -(1 / sqrt (1 - x ^ 2)) := funext $ λ x, (deriv_const_sub _).trans $ by simp only [deriv_arcsin] lemma differentiable_on_arccos : differentiable_on ℝ arccos {-1, 1}ᶜ := differentiable_on_arcsin.const_sub _ lemma times_cont_diff_on_arccos {n : with_top ℕ} : times_cont_diff_on ℝ n arccos {-1, 1}ᶜ := times_cont_diff_on_const.sub times_cont_diff_on_arcsin lemma times_cont_diff_at_arccos_iff {x : ℝ} {n : with_top ℕ} : times_cont_diff_at ℝ n arccos x ↔ n = 0 ∨ (x ≠ -1 ∧ x ≠ 1) := by refine iff.trans ⟨λ h, _, λ h, _⟩ times_cont_diff_at_arcsin_iff; simpa [arccos] using (@times_cont_diff_at_const _ _ _ _ _ _ _ _ _ _ (π / 2)).sub h lemma measurable_arccos : measurable arccos := continuous_arccos.measurable @[simp] lemma tan_pi_div_four : tan (π / 4) = 1 := begin rw [tan_eq_sin_div_cos, cos_pi_div_four, sin_pi_div_four], have h : (sqrt 2) / 2 > 0 := by cancel_denoms, exact div_self (ne_of_gt h), end @[simp] lemma tan_pi_div_two : tan (π / 2) = 0 := by simp [tan_eq_sin_div_cos] lemma tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x := by rw tan_eq_sin_div_cos; exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith)) (cos_pos_of_mem_Ioo ⟨by linarith, hxp⟩) lemma tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x := match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with | or.inl hx0, or.inl hxp := le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp) | or.inl hx0, or.inr hxp := by simp [hxp, tan_eq_sin_div_cos] | or.inr hx0, _ := by simp [hx0.symm] end lemma tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 := neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos])) lemma tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) : tan x ≤ 0 := neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith)) lemma tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := begin rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos], exact div_lt_div (sin_lt_sin_of_lt_of_le_pi_div_two (by linarith) (le_of_lt hy₂) hxy) (cos_le_cos_of_nonneg_of_le_pi hx₁ (by linarith) (le_of_lt hxy)) (sin_nonneg_of_nonneg_of_le_pi (by linarith) (by linarith)) (cos_pos_of_mem_Ioo ⟨by linarith, hy₂⟩) end lemma tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := match le_total x 0, le_total y 0 with | or.inl hx0, or.inl hy0 := neg_lt_neg_iff.1 $ by rw [← tan_neg, ← tan_neg]; exact tan_lt_tan_of_nonneg_of_lt_pi_div_two (neg_nonneg.2 hy0) (neg_lt.2 hx₁) (neg_lt_neg hxy) | or.inl hx0, or.inr hy0 := (lt_or_eq_of_le hy0).elim (λ hy0, calc tan x ≤ 0 : tan_nonpos_of_nonpos_of_neg_pi_div_two_le hx0 (le_of_lt hx₁) ... < tan y : tan_pos_of_pos_of_lt_pi_div_two hy0 hy₂) (λ hy0, by rw [← hy0, tan_zero]; exact tan_neg_of_neg_of_pi_div_two_lt (hy0.symm ▸ hxy) hx₁) | or.inr hx0, or.inl hy0 := by linarith | or.inr hx0, or.inr hy0 := tan_lt_tan_of_nonneg_of_lt_pi_div_two hx0 hy₂ hxy end lemma strict_mono_incr_on_tan : strict_mono_incr_on tan (Ioo (-(π / 2)) (π / 2)) := λ x hx y hy, tan_lt_tan_of_lt_of_lt_pi_div_two hx.1 hy.2 lemma inj_on_tan : inj_on tan (Ioo (-(π / 2)) (π / 2)) := strict_mono_incr_on_tan.inj_on lemma tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) (hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y := inj_on_tan ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ hxy end real namespace complex open_locale real /-- `arg` returns values in the range (-π, π], such that for `x ≠ 0`, `sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`, `arg 0` defaults to `0` -/ noncomputable def arg (x : ℂ) : ℝ := if 0 ≤ x.re then real.arcsin (x.im / x.abs) else if 0 ≤ x.im then real.arcsin ((-x).im / x.abs) + π else real.arcsin ((-x).im / x.abs) - π lemma arg_le_pi (x : ℂ) : arg x ≤ π := if hx₁ : 0 ≤ x.re then by rw [arg, if_pos hx₁]; exact le_trans (real.arcsin_le_pi_div_two _) (le_of_lt (half_lt_self real.pi_pos)) else if hx₂ : 0 ≤ x.im then by rw [arg, if_neg hx₁, if_pos hx₂, ← le_sub_iff_add_le, sub_self, real.arcsin_nonpos, neg_im, neg_div, neg_nonpos]; exact div_nonneg hx₂ (abs_nonneg _) else by rw [arg, if_neg hx₁, if_neg hx₂]; exact sub_le_iff_le_add.2 (le_trans (real.arcsin_le_pi_div_two _) (by linarith [real.pi_pos])) lemma neg_pi_lt_arg (x : ℂ) : -π < arg x := if hx₁ : 0 ≤ x.re then by rw [arg, if_pos hx₁]; exact lt_of_lt_of_le (neg_lt_neg (half_lt_self real.pi_pos)) (real.neg_pi_div_two_le_arcsin _) else have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁, if hx₂ : 0 ≤ x.im then by rw [arg, if_neg hx₁, if_pos hx₂, ← sub_lt_iff_lt_add]; exact (lt_of_lt_of_le (by linarith [real.pi_pos]) (real.neg_pi_div_two_le_arcsin _)) else by rw [arg, if_neg hx₁, if_neg hx₂, lt_sub_iff_add_lt, neg_add_self, real.arcsin_pos, neg_im]; exact div_pos (neg_pos.2 (lt_of_not_ge hx₂)) (abs_pos.2 hx) lemma arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : 0 ≤ x.im) : arg x = arg (-x) + π := have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos], by rw [arg, arg, if_neg (not_le.2 hxr), if_pos this, if_pos hxi, abs_neg] lemma arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : x.im < 0) : arg x = arg (-x) - π := have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos], by rw [arg, arg, if_neg (not_le.2 hxr), if_neg (not_le.2 hxi), if_pos this, abs_neg] @[simp] lemma arg_zero : arg 0 = 0 := by simp [arg, le_refl] @[simp] lemma arg_one : arg 1 = 0 := by simp [arg, zero_le_one] @[simp] lemma arg_neg_one : arg (-1) = π := by simp [arg, le_refl, not_le.2 (@zero_lt_one ℝ _ _)] @[simp] lemma arg_I : arg I = π / 2 := by simp [arg, le_refl] @[simp] lemma arg_neg_I : arg (-I) = -(π / 2) := by simp [arg, le_refl] lemma sin_arg (x : ℂ) : real.sin (arg x) = x.im / x.abs := by unfold arg; split_ifs; simp [sub_eq_add_neg, arg, real.sin_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1 (abs_le.1 (abs_im_div_abs_le_one x)).2, real.sin_add, neg_div, real.arcsin_neg, real.sin_neg] private lemma cos_arg_of_re_nonneg {x : ℂ} (hx : x ≠ 0) (hxr : 0 ≤ x.re) : real.cos (arg x) = x.re / x.abs := have 0 ≤ 1 - (x.im / abs x) ^ 2, from sub_nonneg.2 $ by rw [pow_two, ← _root_.abs_mul_self, _root_.abs_mul, ← pow_two]; exact pow_le_one _ (_root_.abs_nonneg _) (abs_im_div_abs_le_one _), by rw [eq_div_iff_mul_eq (mt abs_eq_zero.1 hx), ← real.mul_self_sqrt (abs_nonneg x), arg, if_pos hxr, real.cos_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1 (abs_le.1 (abs_im_div_abs_le_one x)).2, ← real.sqrt_mul (abs_nonneg _), ← real.sqrt_mul this, sub_mul, div_pow, ← pow_two, div_mul_cancel _ (pow_ne_zero 2 (mt abs_eq_zero.1 hx)), one_mul, pow_two, mul_self_abs, norm_sq_apply, pow_two, add_sub_cancel, real.sqrt_mul_self hxr] lemma cos_arg {x : ℂ} (hx : x ≠ 0) : real.cos (arg x) = x.re / x.abs := if hxr : 0 ≤ x.re then cos_arg_of_re_nonneg hx hxr else have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr, if hxi : 0 ≤ x.im then have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr, by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg (not_le.1 hxr) hxi, real.cos_add_pi, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this]; simp [neg_div] else by rw [arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg (not_le.1 hxr) (not_le.1 hxi)]; simp [sub_eq_add_neg, real.cos_add, neg_div, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this] lemma tan_arg {x : ℂ} : real.tan (arg x) = x.im / x.re := begin by_cases h : x = 0, { simp only [h, zero_div, complex.zero_im, complex.arg_zero, real.tan_zero, complex.zero_re] }, rw [real.tan_eq_sin_div_cos, sin_arg, cos_arg h, div_div_div_cancel_right _ (mt abs_eq_zero.1 h)] end lemma arg_cos_add_sin_mul_I {x : ℝ} (hx₁ : -π < x) (hx₂ : x ≤ π) : arg (cos x + sin x * I) = x := if hx₃ : -(π / 2) ≤ x ∧ x ≤ π / 2 then have hx₄ : 0 ≤ (cos x + sin x * I).re, by simp; exact real.cos_nonneg_of_mem_Icc hx₃, by rw [arg, if_pos hx₄]; simp [abs_cos_add_sin_mul_I, sin_of_real_re, real.arcsin_sin hx₃.1 hx₃.2] else if hx₄ : x < -(π / 2) then have hx₅ : ¬0 ≤ (cos x + sin x * I).re := suffices ¬ 0 ≤ real.cos x, by simpa, not_le.2 $ by rw ← real.cos_neg; apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith, have hx₆ : ¬0 ≤ (cos ↑x + sin ↑x * I).im := suffices real.sin x < 0, by simpa, by apply real.sin_neg_of_neg_of_neg_pi_lt; linarith, suffices -π + -real.arcsin (real.sin x) = x, by rw [arg, if_neg hx₅, if_neg hx₆]; simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re], by rw [← real.arcsin_neg, ← real.sin_add_pi, real.arcsin_sin]; try {simp [add_left_comm]}; linarith else have hx₅ : π / 2 < x, by cases not_and_distrib.1 hx₃; linarith, have hx₆ : ¬0 ≤ (cos x + sin x * I).re := suffices ¬0 ≤ real.cos x, by simpa, not_le.2 $ by apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith, have hx₇ : 0 ≤ (cos x + sin x * I).im := suffices 0 ≤ real.sin x, by simpa, by apply real.sin_nonneg_of_nonneg_of_le_pi; linarith, suffices π - real.arcsin (real.sin x) = x, by rw [arg, if_neg hx₆, if_pos hx₇]; simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re], by rw [← real.sin_pi_sub, real.arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma arg_eq_arg_iff {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) : arg x = arg y ↔ (abs y / abs x : ℂ) * x = y := have hax : abs x ≠ 0, from (mt abs_eq_zero.1 hx), have hay : abs y ≠ 0, from (mt abs_eq_zero.1 hy), ⟨λ h, begin have hcos := congr_arg real.cos h, rw [cos_arg hx, cos_arg hy, div_eq_div_iff hax hay] at hcos, have hsin := congr_arg real.sin h, rw [sin_arg, sin_arg, div_eq_div_iff hax hay] at hsin, apply complex.ext, { rw [mul_re, ← of_real_div, of_real_re, of_real_im, zero_mul, sub_zero, mul_comm, ← mul_div_assoc, hcos, mul_div_cancel _ hax] }, { rw [mul_im, ← of_real_div, of_real_re, of_real_im, zero_mul, add_zero, mul_comm, ← mul_div_assoc, hsin, mul_div_cancel _ hax] } end, λ h, have hre : abs (y / x) * x.re = y.re, by rw ← of_real_div at h; simpa [-of_real_div] using congr_arg re h, have hre' : abs (x / y) * y.re = x.re, by rw [← hre, abs_div, abs_div, ← mul_assoc, div_mul_div, mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul], have him : abs (y / x) * x.im = y.im, by rw ← of_real_div at h; simpa [-of_real_div] using congr_arg im h, have him' : abs (x / y) * y.im = x.im, by rw [← him, abs_div, abs_div, ← mul_assoc, div_mul_div, mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul], have hxya : x.im / abs x = y.im / abs y, by rw [← him, abs_div, mul_comm, ← mul_div_comm, mul_div_cancel_left _ hay], have hnxya : (-x).im / abs x = (-y).im / abs y, by rw [neg_im, neg_im, neg_div, neg_div, hxya], if hxr : 0 ≤ x.re then have hyr : 0 ≤ y.re, from hre ▸ mul_nonneg (abs_nonneg _) hxr, by simp [arg, *] at * else have hyr : ¬ 0 ≤ y.re, from λ hyr, hxr $ hre' ▸ mul_nonneg (abs_nonneg _) hyr, if hxi : 0 ≤ x.im then have hyi : 0 ≤ y.im, from him ▸ mul_nonneg (abs_nonneg _) hxi, by simp [arg, *] at * else have hyi : ¬ 0 ≤ y.im, from λ hyi, hxi $ him' ▸ mul_nonneg (abs_nonneg _) hyi, by simp [arg, *] at *⟩ lemma arg_real_mul (x : ℂ) {r : ℝ} (hr : 0 < r) : arg (r * x) = arg x := if hx : x = 0 then by simp [hx] else (arg_eq_arg_iff (mul_ne_zero (of_real_ne_zero.2 (ne_of_lt hr).symm) hx) hx).2 $ by rw [abs_mul, abs_of_nonneg (le_of_lt hr), ← mul_assoc, of_real_mul, mul_comm (r : ℂ), ← div_div_eq_div_mul, div_mul_cancel _ (of_real_ne_zero.2 (ne_of_lt hr).symm), div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), one_mul] lemma ext_abs_arg {x y : ℂ} (h₁ : x.abs = y.abs) (h₂ : x.arg = y.arg) : x = y := if hy : y = 0 then by simp * at * else have hx : x ≠ 0, from λ hx, by simp [*, eq_comm] at *, by rwa [arg_eq_arg_iff hx hy, h₁, div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hy)), one_mul] at h₂ lemma arg_of_real_of_nonneg {x : ℝ} (hx : 0 ≤ x) : arg x = 0 := by simp [arg, hx] lemma arg_of_real_of_neg {x : ℝ} (hx : x < 0) : arg x = π := by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg, ← of_real_neg, arg_of_real_of_nonneg]; simp [*, le_iff_eq_or_lt, lt_neg] /-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`. `log 0 = 0`-/ @[pp_nodot] noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log] lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log] lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x := by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx, ← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im] lemma range_exp : range exp = {x | x ≠ 0} := set.ext $ λ x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, λ hx, ⟨log x, exp_log hx⟩⟩ lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π) (hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy; exact complex.ext (real.exp_injective $ by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy) (by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _), arg_cos_add_sin_mul_I hx₁ hx₂, arg_cos_add_sin_mul_I hy₁ hy₂] using congr_arg arg hxy) lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x := exp_inj_of_neg_pi_lt_of_le_pi (by rw log_im; exact neg_pi_lt_arg _) (by rw log_im; exact arg_le_pi _) hx₁ hx₂ (by rw [exp_log (exp_ne_zero _)]) lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x := complex.ext (by rw [log_re, of_real_re, abs_of_nonneg hx]) (by rw [of_real_im, log_im, arg_of_real_of_nonneg hx]) lemma log_of_real_re (x : ℝ) : (log (x : ℂ)).re = real.log x := by simp [log_re] @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := by simp [log] lemma log_neg_one : log (-1) = π * I := by simp [log] lemma log_I : log I = π / 2 * I := by simp [log] lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log] lemma exists_pow_nat_eq (x : ℂ) {n : ℕ} (hn : 0 < n) : ∃ z, z ^ n = x := begin by_cases hx : x = 0, { use 0, simp only [hx, zero_pow_eq_zero, hn] }, { use exp (log x / n), rw [← exp_nat_mul, mul_div_cancel', exp_log hx], exact_mod_cast (pos_iff_ne_zero.mp hn) } end lemma exists_eq_mul_self (x : ℂ) : ∃ z, x = z * z := begin obtain ⟨z, rfl⟩ := exists_pow_nat_eq x zero_lt_two, exact ⟨z, pow_two z⟩ end lemma two_pi_I_ne_zero : (2 * π * I : ℂ) ≠ 0 := by norm_num [real.pi_ne_zero, I_ne_zero] lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) := have real.exp (x.re) * real.cos (x.im) = 1 → real.cos x.im ≠ -1, from λ h₁ h₂, begin rw [h₂, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁, have := real.exp_pos x.re, rw ← h₁ at this, exact absurd this (by norm_num) end, calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff] ... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 : begin rw exp_eq_exp_re_mul_sin_add_cos, simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re, real.exp_ne_zero], split; finish [real.sin_eq_zero_iff_cos_eq] end ... ↔ (∃ n : ℤ, ↑n * (2 * π) = x.im) ∧ (∃ n : ℤ, ↑n * π = x.im) ∧ x.re = 0 : by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff] ... ↔ ∃ n : ℤ, x = n * ((2 * π) * I) : ⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩, λ ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩, by simp [hn]⟩⟩ lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 := by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)] lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) := by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add'] @[simp] lemma cos_pi_div_two : cos (π / 2) = 0 := calc cos (π / 2) = real.cos (π / 2) : by rw [of_real_cos]; simp ... = 0 : by simp @[simp] lemma sin_pi_div_two : sin (π / 2) = 1 := calc sin (π / 2) = real.sin (π / 2) : by rw [of_real_sin]; simp ... = 1 : by simp @[simp] lemma sin_pi : sin π = 0 := by rw [← of_real_sin, real.sin_pi]; simp @[simp] lemma cos_pi : cos π = -1 := by rw [← of_real_cos, real.cos_pi]; simp @[simp] lemma sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] lemma cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] lemma sin_add_pi (x : ℂ) : sin (x + π) = -sin x := by simp [sin_add] lemma sin_add_two_pi (x : ℂ) : sin (x + 2 * π) = sin x := by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi] lemma cos_add_two_pi (x : ℂ) : cos (x + 2 * π) = cos x := by simp [cos_add, cos_two_pi, sin_two_pi] lemma sin_pi_sub (x : ℂ) : sin (π - x) = sin x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi (x : ℂ) : cos (x + π) = -cos x := by simp [cos_add] lemma cos_pi_sub (x : ℂ) : cos (π - x) = -cos x := by simp [sub_eq_add_neg, cos_add] lemma sin_add_pi_div_two (x : ℂ) : sin (x + π / 2) = cos x := by simp [sin_add] lemma sin_sub_pi_div_two (x : ℂ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] lemma sin_pi_div_two_sub (x : ℂ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi_div_two (x : ℂ) : cos (x + π / 2) = -sin x := by simp [cos_add] lemma cos_sub_pi_div_two (x : ℂ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] lemma cos_pi_div_two_sub (x : ℂ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := by induction n; simp [add_mul, sin_add, *] lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi] lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi] lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe, int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg, (neg_mul_eq_neg_mul _ _).symm, cos_neg] lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simp [cos_add, sin_add, cos_int_mul_two_pi] lemma exp_pi_mul_I : exp (π * I) = -1 := by rw exp_mul_I; simp theorem cos_eq_zero_iff {θ : ℂ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := begin have h : (exp (θ * I) + exp (-θ * I)) / 2 = 0 ↔ exp (2 * θ * I) = -1, { rw [@div_eq_iff _ _ (exp (θ * I) + exp (-θ * I)) 2 0 two_ne_zero', zero_mul, add_eq_zero_iff_eq_neg, neg_eq_neg_one_mul, ← div_eq_iff (exp_ne_zero _), ← exp_sub], field_simp only, congr' 3, ring }, rw [cos, h, ← exp_pi_mul_I, exp_eq_exp_iff_exists_int, mul_right_comm], refine exists_congr (λ x, _), refine (iff_of_eq $ congr_arg _ _).trans (mul_right_inj' $ mul_ne_zero two_ne_zero' I_ne_zero), ring, end theorem cos_ne_zero_iff {θ : ℂ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] theorem sin_eq_zero_iff {θ : ℂ} : sin θ = 0 ↔ ∃ k : ℤ, θ = k * π := begin rw [← complex.cos_sub_pi_div_two, cos_eq_zero_iff], split, { rintros ⟨k, hk⟩, use k + 1, field_simp [eq_add_of_sub_eq hk], ring }, { rintros ⟨k, rfl⟩, use k - 1, field_simp, ring } end theorem sin_ne_zero_iff {θ : ℂ} : sin θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π := by rw [← not_exists, not_iff_not, sin_eq_zero_iff] lemma sin_eq_zero_iff_cos_eq {z : ℂ} : sin z = 0 ↔ cos z = 1 ∨ cos z = -1 := by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq, pow_two, pow_two, ← sub_eq_iff_eq_add, sub_self]; exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩ lemma tan_eq_zero_iff {θ : ℂ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 := begin have h := (sin_two_mul θ).symm, rw mul_assoc at h, rw [tan, div_eq_zero_iff, ← mul_eq_zero, ← zero_mul ((1/2):ℂ), mul_one_div, cancel_factors.cancel_factors_eq_div h two_ne_zero', mul_comm], simpa only [zero_div, zero_mul, ne.def, not_false_iff] with field_simps using sin_eq_zero_iff, end lemma tan_ne_zero_iff {θ : ℂ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 := by rw [← not_exists, not_iff_not, tan_eq_zero_iff] lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 := tan_eq_zero_iff.mpr (by use n) lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 := by rw tan_eq_zero_iff; use (2*n); field_simp [mul_comm ((n:ℂ)*(π:ℂ)) 2, ← mul_assoc] lemma cos_eq_cos_iff {x y : ℂ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := calc cos x = cos y ↔ cos x - cos y = 0 : sub_eq_zero.symm ... ↔ -2 * sin((x + y)/2) * sin((x - y)/2) = 0 : by rw cos_sub_cos ... ↔ sin((x + y)/2) = 0 ∨ sin((x - y)/2) = 0 : by simp [(by norm_num : (2:ℂ) ≠ 0)] ... ↔ sin((x - y)/2) = 0 ∨ sin((x + y)/2) = 0 : or.comm ... ↔ (∃ k : ℤ, y = 2 * k * π + x) ∨ (∃ k :ℤ, y = 2 * k * π - x) : begin apply or_congr; field_simp [sin_eq_zero_iff, (by norm_num : -(2:ℂ) ≠ 0), eq_sub_iff_add_eq', sub_eq_iff_eq_add, mul_comm (2:ℂ), mul_right_comm _ (2:ℂ)], split; { rintros ⟨k, rfl⟩, use -k, simp, }, end ... ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x : exists_or_distrib.symm lemma sin_eq_sin_iff {x y : ℂ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := begin simp only [← complex.cos_sub_pi_div_two, cos_eq_cos_iff, sub_eq_iff_eq_add], refine exists_congr (λ k, or_congr _ _); refine eq.congr rfl _; field_simp; ring end lemma tan_add {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := begin rcases h with ⟨h1, h2⟩ | ⟨⟨k, rfl⟩, ⟨l, rfl⟩⟩, { rw [tan, sin_add, cos_add, ← div_div_div_cancel_right (sin x * cos y + cos x * sin y) (mul_ne_zero (cos_ne_zero_iff.mpr h1) (cos_ne_zero_iff.mpr h2)), add_div, sub_div], simp only [←div_mul_div, ←tan, mul_one, one_mul, div_self (cos_ne_zero_iff.mpr h1), div_self (cos_ne_zero_iff.mpr h2)] }, { obtain ⟨t, hx, hy, hxy⟩ := ⟨tan_int_mul_pi_div_two, t (2*k+1), t (2*l+1), t (2*k+1+(2*l+1))⟩, simp only [int.cast_add, int.cast_bit0, int.cast_mul, int.cast_one, hx, hy] at hx hy hxy, rw [hx, hy, add_zero, zero_div, mul_div_assoc, mul_div_assoc, ← add_mul (2*(k:ℂ)+1) (2*l+1) (π/2), ← mul_div_assoc, hxy] }, end lemma tan_add' {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := tan_add (or.inl h) lemma tan_two_mul {z : ℂ} : tan (2 * z) = 2 * tan z / (1 - tan z ^ 2) := begin by_cases h : ∀ k : ℤ, z ≠ (2 * k + 1) * π / 2, { rw [two_mul, two_mul, pow_two, tan_add (or.inl ⟨h, h⟩)] }, { rw not_forall_not at h, rw [two_mul, two_mul, pow_two, tan_add (or.inr ⟨h, h⟩)] }, end lemma tan_add_mul_I {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y * I = (2 * l + 1) * π / 2)) : tan (x + y*I) = (tan x + tanh y * I) / (1 - tan x * tanh y * I) := by rw [tan_add h, tan_mul_I, mul_assoc] lemma tan_eq {z : ℂ} (h : ((∀ k : ℤ, (z.re:ℂ) ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, (z.im:ℂ) * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, (z.re:ℂ) = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, (z.im:ℂ) * I = (2 * l + 1) * π / 2)) : tan z = (tan z.re + tanh z.im * I) / (1 - tan z.re * tanh z.im * I) := by convert tan_add_mul_I h; exact (re_add_im z).symm lemma has_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) : has_deriv_at tan (1 / (cos x)^2) x := begin convert has_deriv_at.div (has_deriv_at_sin x) (has_deriv_at_cos x) h, rw ← sin_sq_add_cos_sq x, ring, end lemma tendsto_abs_tan_of_cos_eq_zero {x : ℂ} (hx : cos x = 0) : tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top := begin simp only [tan_eq_sin_div_cos, ← norm_eq_abs, normed_field.norm_div], have A : sin x ≠ 0 := λ h, by simpa [*, pow_two] using sin_sq_add_cos_sq x, have B : tendsto cos (𝓝[{x}ᶜ] (x)) (𝓝[{0}ᶜ] 0), { refine tendsto_inf.2 ⟨tendsto.mono_left _ inf_le_left, tendsto_principal.2 _⟩, exacts [continuous_cos.tendsto' x 0 hx, hx ▸ (has_deriv_at_cos _).eventually_ne (neg_ne_zero.2 A)] }, exact tendsto.mul_at_top (norm_pos_iff.2 A) continuous_sin.continuous_within_at.norm (tendsto.inv_tendsto_zero $ tendsto_norm_nhds_within_zero.comp B), end lemma tendsto_abs_tan_at_top (k : ℤ) : tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top := tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩ @[simp] lemma continuous_at_tan {x : ℂ} : continuous_at tan x ↔ cos x ≠ 0 := begin refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩, exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _ (hc.norm.tendsto.mono_left inf_le_left) end @[simp] lemma differentiable_at_tan {x : ℂ} : differentiable_at ℂ tan x ↔ cos x ≠ 0:= ⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩ @[simp] lemma deriv_tan (x : ℂ) : deriv tan x = 1 / (cos x)^2 := if h : cos x = 0 then have ¬differentiable_at ℂ tan x := mt differentiable_at_tan.1 (not_not.2 h), by simp [deriv_zero_of_not_differentiable_at this, h, pow_two] else (has_deriv_at_tan h).deriv lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} := continuous_on_sin.div continuous_on_cos $ λ x, id lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) := continuous_on_iff_continuous_restrict.1 continuous_on_tan @[simp] lemma times_cont_diff_at_tan {x : ℂ} {n : with_top ℕ} : times_cont_diff_at ℂ n tan x ↔ cos x ≠ 0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, times_cont_diff_sin.times_cont_diff_at.div times_cont_diff_cos.times_cont_diff_at⟩ lemma cos_eq_iff_quadratic {z w : ℂ} : cos z = w ↔ (exp (z * I)) ^ 2 - 2 * w * exp (z * I) + 1 = 0 := begin rw ← sub_eq_zero, field_simp [cos, exp_neg, exp_ne_zero], refine eq.congr _ rfl, ring end lemma cos_surjective : function.surjective cos := begin intro x, obtain ⟨w, w₀, hw⟩ : ∃ w ≠ 0, 1 * w * w + (-2 * x) * w + 1 = 0, { rcases exists_quadratic_eq_zero one_ne_zero (exists_eq_mul_self _) with ⟨w, hw⟩, refine ⟨w, _, hw⟩, rintro rfl, simpa only [zero_add, one_ne_zero, mul_zero] using hw }, refine ⟨log w / I, cos_eq_iff_quadratic.2 _⟩, rw [div_mul_cancel _ I_ne_zero, exp_log w₀], convert hw, ring end @[simp] lemma range_cos : range cos = set.univ := cos_surjective.range_eq lemma sin_surjective : function.surjective sin := begin intro x, rcases cos_surjective x with ⟨z, rfl⟩, exact ⟨z + π / 2, sin_add_pi_div_two z⟩ end @[simp] lemma range_sin : range sin = set.univ := sin_surjective.range_eq end complex section chebyshev₁ open polynomial complex /-- the `n`-th Chebyshev polynomial evaluates on `cos θ` to the value `cos (n * θ)`. -/ lemma chebyshev₁_complex_cos (θ : ℂ) : ∀ n, (chebyshev₁ ℂ n).eval (cos θ) = cos (n * θ) | 0 := by simp only [chebyshev₁_zero, eval_one, nat.cast_zero, zero_mul, cos_zero] | 1 := by simp only [eval_X, one_mul, chebyshev₁_one, nat.cast_one] | (n + 2) := begin simp only [eval_X, eval_one, chebyshev₁_add_two, eval_sub, eval_bit0, nat.cast_succ, eval_mul], rw [chebyshev₁_complex_cos (n + 1), chebyshev₁_complex_cos n], have aux : sin θ * sin θ = 1 - cos θ * cos θ, { rw ← sin_sq_add_cos_sq θ, ring, }, simp only [nat.cast_add, nat.cast_one, add_mul, cos_add, one_mul, sin_add, mul_assoc, aux], ring, end /-- `cos (n * θ)` is equal to the `n`-th Chebyshev polynomial evaluated on `cos θ`. -/ lemma cos_nat_mul (n : ℕ) (θ : ℂ) : cos (n * θ) = (chebyshev₁ ℂ n).eval (cos θ) := (chebyshev₁_complex_cos θ n).symm end chebyshev₁ namespace real open_locale real lemma tan_add {x y : ℝ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_add, complex.of_real_div, complex.of_real_mul, complex.of_real_tan] using @complex.tan_add (x:ℂ) (y:ℂ) (by convert h; norm_cast) lemma tan_add' {x y : ℝ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := tan_add (or.inl h) lemma tan_two_mul {x:ℝ} : tan (2 * x) = 2 * tan x / (1 - tan x ^ 2) := by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_div, complex.of_real_pow, complex.of_real_mul, complex.of_real_tan, complex.of_real_bit0, complex.of_real_one] using complex.tan_two_mul theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := by exact_mod_cast @complex.cos_eq_zero_iff θ theorem cos_ne_zero_iff {θ : ℝ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] lemma tan_ne_zero_iff {θ : ℝ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 := by rw [← complex.of_real_ne_zero, complex.of_real_tan, complex.tan_ne_zero_iff]; norm_cast lemma tan_eq_zero_iff {θ : ℝ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 := by rw [← not_iff_not, not_exists, ← ne, tan_ne_zero_iff] lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 := tan_eq_zero_iff.mpr (by use n) lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 := by rw tan_eq_zero_iff; use (2*n); field_simp [mul_comm ((n:ℝ)*(π:ℝ)) 2, ← mul_assoc] lemma cos_eq_cos_iff {x y : ℝ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := by exact_mod_cast @complex.cos_eq_cos_iff x y lemma sin_eq_sin_iff {x y : ℝ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := by exact_mod_cast @complex.sin_eq_sin_iff x y lemma has_deriv_at_tan {x : ℝ} (h : cos x ≠ 0) : has_deriv_at tan (1 / (cos x)^2) x := by exact_mod_cast (complex.has_deriv_at_tan (by exact_mod_cast h)).real_of_complex lemma tendsto_abs_tan_of_cos_eq_zero {x : ℝ} (hx : cos x = 0) : tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top := begin have hx : complex.cos x = 0, by exact_mod_cast hx, simp only [← complex.abs_of_real, complex.of_real_tan], refine (complex.tendsto_abs_tan_of_cos_eq_zero hx).comp _, refine tendsto.inf complex.continuous_of_real.continuous_at _, exact tendsto_principal_principal.2 (λ y, mt complex.of_real_inj.1) end lemma tendsto_abs_tan_at_top (k : ℤ) : tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top := tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩ lemma continuous_at_tan {x : ℝ} : continuous_at tan x ↔ cos x ≠ 0 := begin refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩, exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _ (hc.norm.tendsto.mono_left inf_le_left) end lemma differentiable_at_tan {x : ℝ} : differentiable_at ℝ tan x ↔ cos x ≠ 0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩ @[simp] lemma deriv_tan (x : ℝ) : deriv tan x = 1 / (cos x)^2 := if h : cos x = 0 then have ¬differentiable_at ℝ tan x := mt differentiable_at_tan.1 (not_not.2 h), by simp [deriv_zero_of_not_differentiable_at this, h, pow_two] else (has_deriv_at_tan h).deriv @[simp] lemma times_cont_diff_at_tan {n x} : times_cont_diff_at ℝ n tan x ↔ cos x ≠ 0 := ⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (complex.times_cont_diff_at_tan.2 $ by exact_mod_cast h).real_of_complex⟩ lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} := λ x hx, (continuous_at_tan.2 hx).continuous_within_at lemma has_deriv_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) : has_deriv_at tan (1 / (cos x)^2) x := has_deriv_at_tan (cos_pos_of_mem_Ioo h).ne' lemma differentiable_at_tan_of_mem_Ioo {x : ℝ} (h : x ∈ Ioo (-(π/2):ℝ) (π/2)) : differentiable_at ℝ tan x := (has_deriv_at_tan_of_mem_Ioo h).differentiable_at lemma continuous_on_tan_Ioo : continuous_on tan (Ioo (-(π/2)) (π/2)) := λ x hx, (differentiable_at_tan_of_mem_Ioo hx).continuous_at.continuous_within_at lemma tendsto_sin_pi_div_two : tendsto sin (𝓝[Iio (π/2)] (π/2)) (𝓝 1) := by { convert continuous_sin.continuous_within_at, simp } lemma tendsto_cos_pi_div_two : tendsto cos (𝓝[Iio (π/2)] (π/2)) (𝓝[Ioi 0] 0) := begin apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within, { convert continuous_cos.continuous_within_at, simp }, { filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.mpr (norm_num.lt_neg_pos _ _ pi_div_two_pos pi_div_two_pos))] λ x hx, cos_pos_of_mem_Ioo hx }, end lemma tendsto_tan_pi_div_two : tendsto tan (𝓝[Iio (π/2)] (π/2)) at_top := begin convert (tendsto.inv_tendsto_zero tendsto_cos_pi_div_two).at_top_mul (by norm_num) tendsto_sin_pi_div_two, simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos] end lemma tendsto_sin_neg_pi_div_two : tendsto sin (𝓝[Ioi (-(π/2))] (-(π/2))) (𝓝 (-1)) := by { convert continuous_sin.continuous_within_at, simp } lemma tendsto_cos_neg_pi_div_two : tendsto cos (𝓝[Ioi (-(π/2))] (-(π/2))) (𝓝[Ioi 0] 0) := begin apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within, { convert continuous_cos.continuous_within_at, simp }, { filter_upwards [Ioo_mem_nhds_within_Ioi (left_mem_Ico.mpr (norm_num.lt_neg_pos _ _ pi_div_two_pos pi_div_two_pos))] λ x hx, cos_pos_of_mem_Ioo hx }, end lemma tendsto_tan_neg_pi_div_two : tendsto tan (𝓝[Ioi (-(π/2))] (-(π/2))) at_bot := begin convert (tendsto.inv_tendsto_zero tendsto_cos_neg_pi_div_two).at_top_mul_neg (by norm_num) tendsto_sin_neg_pi_div_two, simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos] end lemma surj_on_tan : surj_on tan (Ioo (-(π / 2)) (π / 2)) univ := have _ := neg_lt_self pi_div_two_pos, continuous_on_tan_Ioo.surj_on_of_tendsto (nonempty_Ioo.2 this) (by simp [tendsto_tan_neg_pi_div_two, this]) (by simp [tendsto_tan_pi_div_two, this]) lemma tan_surjective : function.surjective tan := λ x, surj_on_tan.subset_range trivial lemma image_tan_Ioo : tan '' (Ioo (-(π / 2)) (π / 2)) = univ := univ_subset_iff.1 surj_on_tan /-- `real.tan` as an `order_iso` between `(-(π / 2), π / 2)` and `ℝ`. -/ def tan_order_iso : Ioo (-(π / 2)) (π / 2) ≃o ℝ := (strict_mono_incr_on_tan.order_iso _ _).trans $ (order_iso.set_congr _ _ image_tan_Ioo).trans order_iso.set.univ /-- Inverse of the `tan` function, returns values in the range `-π / 2 < arctan x` and `arctan x < π / 2` -/ @[pp_nodot] noncomputable def arctan (x : ℝ) : ℝ := tan_order_iso.symm x @[simp] lemma tan_arctan (x : ℝ) : tan (arctan x) = x := tan_order_iso.apply_symm_apply x lemma arctan_mem_Ioo (x : ℝ) : arctan x ∈ Ioo (-(π / 2)) (π / 2) := subtype.coe_prop _ lemma arctan_tan {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : arctan (tan x) = x := subtype.ext_iff.1 $ tan_order_iso.symm_apply_apply ⟨x, hx₁, hx₂⟩ lemma cos_arctan_pos (x : ℝ) : 0 < cos (arctan x) := cos_pos_of_mem_Ioo $ arctan_mem_Ioo x lemma cos_sq_arctan (x : ℝ) : cos (arctan x) ^ 2 = 1 / (1 + x ^ 2) := by rw [one_div, ← inv_one_add_tan_sq (cos_arctan_pos x).ne', tan_arctan] lemma sin_arctan (x : ℝ) : sin (arctan x) = x / sqrt (1 + x ^ 2) := by rw [← tan_div_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan] lemma cos_arctan (x : ℝ) : cos (arctan x) = 1 / sqrt (1 + x ^ 2) := by rw [one_div, ← inv_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan] lemma arctan_lt_pi_div_two (x : ℝ) : arctan x < π / 2 := (arctan_mem_Ioo x).2 lemma neg_pi_div_two_lt_arctan (x : ℝ) : -(π / 2) < arctan x := (arctan_mem_Ioo x).1 lemma arctan_eq_arcsin (x : ℝ) : arctan x = arcsin (x / sqrt (1 + x ^ 2)) := eq.symm $ arcsin_eq_of_sin_eq (sin_arctan x) (mem_Icc_of_Ioo $ arctan_mem_Ioo x) @[simp] lemma arctan_zero : arctan 0 = 0 := by simp [arctan_eq_arcsin] lemma arctan_eq_of_tan_eq {x y : ℝ} (h : tan x = y) (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : arctan y = x := inj_on_tan (arctan_mem_Ioo _) hx (by rw [tan_arctan, h]) @[simp] lemma arctan_one : arctan 1 = π / 4 := arctan_eq_of_tan_eq tan_pi_div_four $ by split; linarith [pi_pos] @[simp] lemma arctan_neg (x : ℝ) : arctan (-x) = - arctan x := by simp [arctan_eq_arcsin, neg_div] lemma continuous_arctan : continuous arctan := continuous_subtype_coe.comp tan_order_iso.to_homeomorph.continuous_inv_fun lemma continuous_at_arctan {x : ℝ} : continuous_at arctan x := continuous_arctan.continuous_at /-- `real.tan` as a `local_homeomorph` between `(-(π / 2), π / 2)` and the whole line. -/ def tan_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := tan, inv_fun := arctan, source := Ioo (-(π / 2)) (π / 2), target := univ, map_source' := maps_to_univ _ _, map_target' := λ y hy, arctan_mem_Ioo y, left_inv' := λ x hx, arctan_tan hx.1 hx.2, right_inv' := λ y hy, tan_arctan y, open_source := is_open_Ioo, open_target := is_open_univ, continuous_to_fun := continuous_on_tan_Ioo, continuous_inv_fun := continuous_arctan.continuous_on } @[simp] lemma coe_tan_local_homeomorph : ⇑tan_local_homeomorph = tan := rfl @[simp] lemma coe_tan_local_homeomorph_symm : ⇑tan_local_homeomorph.symm = arctan := rfl lemma has_deriv_at_arctan (x : ℝ) : has_deriv_at arctan (1 / (1 + x^2)) x := have A : cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne', by simpa [cos_sq_arctan] using tan_local_homeomorph.has_deriv_at_symm trivial (by simpa) (has_deriv_at_tan A) lemma differentiable_at_arctan (x : ℝ) : differentiable_at ℝ arctan x := (has_deriv_at_arctan x).differentiable_at lemma differentiable_arctan : differentiable ℝ arctan := differentiable_at_arctan @[simp] lemma deriv_arctan : deriv arctan = (λ x, 1 / (1 + x^2)) := funext $ λ x, (has_deriv_at_arctan x).deriv lemma times_cont_diff_arctan {n : with_top ℕ} : times_cont_diff ℝ n arctan := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, have cos (arctan x) ≠ 0 := (cos_arctan_pos x).ne', tan_local_homeomorph.times_cont_diff_at_symm_deriv (by simpa) trivial (has_deriv_at_tan this) (times_cont_diff_at_tan.2 this) lemma measurable_arctan : measurable arctan := continuous_arctan.measurable end real section /-! ### Lemmas for derivatives of the composition of `real.arctan` with a differentiable function In this section we register lemmas for the derivatives of the composition of `real.arctan` with a differentiable function, for standalone use and use with `simp`. -/ open real lemma measurable.arctan {α : Type*} [measurable_space α] {f : α → ℝ} (hf : measurable f) : measurable (λ x, arctan (f x)) := measurable_arctan.comp hf section deriv variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} lemma has_deriv_at.arctan (hf : has_deriv_at f f' x) : has_deriv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') x := (real.has_deriv_at_arctan (f x)).comp x hf lemma has_deriv_within_at.arctan (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) * f') s x := (real.has_deriv_at_arctan (f x)).comp_has_deriv_within_at x hf lemma deriv_within_arctan (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) * (deriv_within f s x) := hf.has_deriv_within_at.arctan.deriv_within hxs @[simp] lemma deriv_arctan (hc : differentiable_at ℝ f x) : deriv (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) * (deriv f x) := hc.has_deriv_at.arctan.deriv end deriv section fderiv variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E} {s : set E} {n : with_top ℕ} lemma has_fderiv_at.arctan (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') x := (has_deriv_at_arctan (f x)).comp_has_fderiv_at x hf lemma has_fderiv_within_at.arctan (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, arctan (f x)) ((1 / (1 + (f x)^2)) • f') s x := (has_deriv_at_arctan (f x)).comp_has_fderiv_within_at x hf lemma fderiv_within_arctan (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λ x, arctan (f x)) s x = (1 / (1 + (f x)^2)) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.arctan.fderiv_within hxs @[simp] lemma fderiv_arctan (hc : differentiable_at ℝ f x) : fderiv ℝ (λ x, arctan (f x)) x = (1 / (1 + (f x)^2)) • (fderiv ℝ f x) := hc.has_fderiv_at.arctan.fderiv lemma differentiable_within_at.arctan (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.arctan (f x)) s x := hf.has_fderiv_within_at.arctan.differentiable_within_at @[simp] lemma differentiable_at.arctan (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λ x, arctan (f x)) x := hc.has_fderiv_at.arctan.differentiable_at lemma differentiable_on.arctan (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λ x, arctan (f x)) s := λ x h, (hc x h).arctan @[simp] lemma differentiable.arctan (hc : differentiable ℝ f) : differentiable ℝ (λ x, arctan (f x)) := λ x, (hc x).arctan lemma times_cont_diff_at.arctan (h : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, arctan (f x)) x := times_cont_diff_arctan.times_cont_diff_at.comp x h lemma times_cont_diff.arctan (h : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, arctan (f x)) := times_cont_diff_arctan.comp h lemma times_cont_diff_within_at.arctan (h : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, arctan (f x)) s x := times_cont_diff_arctan.comp_times_cont_diff_within_at h lemma times_cont_diff_on.arctan (h : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, arctan (f x)) s := times_cont_diff_arctan.comp_times_cont_diff_on h end fderiv end
a0ef3c118ab1db367719e086d158cf8c9f1cd911
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/sup_inf/supSumSets.lean
bdf62751dad6db50a23a30965398da359cd3bc0e
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,421
lean
import game.sup_inf.level04 namespace xena -- hide /- # Chapter 3 : Sup and Inf ## Level 5 A classical result: the supremum of an element-wise sum of sets. -/ -- see also ds_infSum.lean for only the better-organized version -- hide def mem_sum_sets (A : set ℝ) (B : set ℝ) := { x : ℝ | ∃ y ∈ A, ∃ z ∈ B, x = y + z} /- Lemma If $A$ and $B$ are sets of reals, then $$ \textrm{sup} (A + B) = \textrm{sup} (A) + \textrm{sup}(B)$$ -/ lemma sup_sum_sets (A : set ℝ) (B : set ℝ) (h1A : A.nonempty) (h1B : B.nonempty) (h2A : bdd_above A) (h2B : bdd_above B) (a : ℝ) (b : ℝ) : (is_lub A a) ∧ (is_lub B b) → is_lub (mem_sum_sets A B) (a + b) := begin intro h, cases h with hA hB, split, { -- prove that (a+b) is an upper bound intros x h0, cases h0 with y h1, cases h1 with yA h2, cases h2 with z h3, cases h3 with zB hx, have H12A := hA.left, have H12B := hB.left, have H13A := H12A y yA, have H13B := H12B z zB, linarith, }, -- now prove that (a+b) is the least upper bound intros S hS, --change ∀ xab ∈ (sum_of_sets A B), xab ≤ S at hS, have H1 : ∀ y ∈ A, ∀ z ∈ B, (y + z) ∈ (mem_sum_sets A B), intros y hy z hz, unfold mem_sum_sets, existsi y, existsi hy, existsi z, existsi hz, refl, have H2 : ∀ y ∈ A, ∀ z ∈ B, (y + z) ≤ S, intros y hy z hz, apply hS, exact H1 y hy z hz, have H3 : ∀ y ∈ A, ∀ z ∈ B, y ≤ S - z, intros y hy z hz, have H3a := H2 y hy z hz, exact le_sub_right_of_add_le H3a, have h21B := hB.right, have h22B := hB.left, --change ∀ z ∈ B, z ≤ b at h22B, have H4 : ∀ z ∈ B, (S - z) ∈ upper_bounds A, --! intros z hz y hy, exact H3 y hy z hz, have H5 : ∀ z ∈ B, a ≤ (S - z), intros z hz, have H13A := hA.right, change ∀ u ∈ upper_bounds A, a ≤ u at H13A, have H5a := H4 z hz, exact H13A (S-z) H5a, have H6 : ∀ z ∈ B, z ≤ S - a, intros z hz, have H6a := H5 z hz, exact le_sub.1 H6a, --have H7 : (S - a) ∈ upper_bounds B, exact H6, have H8 : b ≤ (S-a), have H13B := hB.right, change ∀ u ∈ upper_bounds B, b ≤ u at H13B, exact H13B (S-a) H6, -- I had H7 instead of H6 here exact add_le_of_le_sub_left H8, done end -- begin hide -- Kevin's term proof for second part -- NOTE: I altered this after is_lub changed. I don't *think* it's broken -- GT. lemma sup_sum_of_sets' (A : set ℝ) (B : set ℝ) (a : ℝ) (b : ℝ) (hA : is_lub A a) (hB : is_lub B b) : a + b ∈ lower_bounds (upper_bounds (mem_sum_sets A B)) := λ S hS, add_le_of_le_sub_left $ hB.2 (S - a) $ λ z hz, le_sub.1 $ hA.2 (S - z) $ λ y hy, le_sub_right_of_add_le $ hS ⟨y, hy, z, hz, rfl⟩ -- Patrick Massot's proof for second part lemma sup_sum_of_sets'' (A : set ℝ) (B : set ℝ) (a : ℝ) (b : ℝ) (hA : is_lub A a) (hB : is_lub B b) : a + b ∈ lower_bounds (upper_bounds (mem_sum_sets A B)) := begin intros S hS, have H1 : ∀ x ∈ A, S - x ∈ upper_bounds B, { intros x hx y hy, suffices : x + y ≤ S, by linarith, -- by rwa le_sub_iff_add_le', exact hS ⟨x, hx, y, hy, rfl⟩, }, have H2 : S - b ∈ upper_bounds A, { intros x hx, suffices : b ≤ S - x, by linarith, -- by rwa le_sub, exact hB.2 (S - x) (H1 x hx) }, linarith [hA.2 (S - b) H2], --exact le_sub_iff_add_le.mp (hA.2 H2), end --- end hide end xena -- hide
91e2cd2b192168da614ebaf1ef59927a1fa1331b
75bd9c50a345718d735a7533c007cf45f9da9a83
/src/measure_theory/simple_func_dense.lean
967c310e58e172b8bbcada16311f2076a13a012e
[ "Apache-2.0" ]
permissive
jtbarker/mathlib
a1a3b1ddc16179826260578410746756ef18032c
392d3e376b44265ef2dedbd92231d3177acc1fd0
refs/heads/master
1,671,246,411,096
1,600,801,712,000
1,600,801,712,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,911
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.l1_space /-! # Density of simple functions Show that each Borel measurable function can be approximated, both pointwise and in `L¹` norm, by a sequence of simple functions. -/ noncomputable theory open set filter topological_space open_locale classical topological_space universes u v variables {α : Type u} {β : Type v} {ι : Type*} namespace measure_theory open ennreal nat metric variables [measurable_space α] [normed_group β] [second_countable_topology β] [measurable_space β] [borel_space β] local infixr ` →ₛ `:25 := simple_func -- FIXME this monolithic proof used to cause a deterministic timeout with `-T50000` -- It no longer does, but it should still be broken up into a sequence of more manageable pieces. lemma simple_func_sequence_tendsto {f : α → β} (hf : measurable f) : ∃ (F : ℕ → (α →ₛ β)), ∀ x : α, tendsto (λ n, F n x) at_top (𝓝 (f x)) ∧ ∀ n, ∥F n x∥ ≤ ∥f x∥ + ∥f x∥ := -- enumerate a countable dense subset {e k} of β let ⟨D, ⟨D_countable, D_dense⟩⟩ := @separable_space.exists_countable_closure_eq_univ β _ _ in let e := enumerate_countable D_countable 0 in let E := range e in have E_dense : closure E = univ := dense_of_subset_dense (subset_range_enumerate D_countable 0) D_dense, let A' (N k : ℕ) : set α := f ⁻¹' (metric.ball (e k) (1 / (N+1 : ℝ)) \ metric.ball 0 (1 / (N+1 : ℝ))) in let A N := disjointed (A' N) in have is_measurable_A' : ∀ {N k}, is_measurable (A' N k) := λ N k, hf $ is_measurable_ball.inter is_measurable_ball.compl, have is_measurable_A : ∀ {N k}, is_measurable (A N k) := λ N, is_measurable.disjointed $ λ k, is_measurable_A', have A_subset_A' : ∀ {N k x}, x ∈ A N k → x ∈ A' N k := λ N k, inter_subset_left _ _, have dist_ek_fx' : ∀ {x N k}, x ∈ A' N k → (dist (e k) (f x) < 1 / (N+1 : ℝ)) := λ x N k, by { rw [dist_comm], simpa using (λ a b, a) }, have dist_ek_fx : ∀ {x N k}, x ∈ A N k → (dist (e k) (f x) < 1 / (N+1 : ℝ)) := λ x N k h, dist_ek_fx' (A_subset_A' h), have norm_fx' : ∀ {x N k}, x ∈ A' N k → (1 / (N+1 : ℝ)) ≤ ∥f x∥ := λ x N k, by simp [ball_0_eq], have norm_fx : ∀ {x N k}, x ∈ A N k → (1 / (N+1 : ℝ)) ≤ ∥f x∥ := λ x N k h, norm_fx' (A_subset_A' h), -- construct the desired sequence of simple functions let M N x := nat.find_greatest (λ M, x ∈ ⋃ k ≤ N, (A M k)) N in let k N x := nat.find_greatest (λ k, x ∈ A (M N x) k) N in let F N x := if x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k then e (k N x) else 0 in -- prove properties of the construction above have k_unique : ∀ {M k k' x}, x ∈ A M k ∧ x ∈ A M k' → k = k' := λ M k k' x h, begin by_contradiction k_ne_k', have NE : (A M k ∩ A M k').nonempty, from ⟨x, h⟩, have E : A M k ∩ A M k' = ∅ := disjoint_disjointed' k k' k_ne_k', exact NE.ne_empty E, end, have x_mem_Union_k : ∀ {N x}, (x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k) → x ∈ ⋃ k ≤ N, A (M N x) k := λ N x h, @nat.find_greatest_spec (λ M, x ∈ ⋃ k ≤ N, (A M k)) _ N ( let ⟨M, hM⟩ := mem_Union.1 (h) in let ⟨hM₁, hM₂⟩ := mem_Union.1 hM in ⟨M, ⟨hM₁, hM₂⟩⟩), have x_mem_A : ∀ {N x}, (x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k) → x ∈ A (M N x) (k N x) := λ N x h, @nat.find_greatest_spec (λ k, x ∈ A (M N x) k) _ N ( let ⟨k, hk⟩ := mem_Union.1 (x_mem_Union_k h) in let ⟨hk₁, hk₂⟩ := mem_Union.1 hk in ⟨k, ⟨hk₁, hk₂⟩⟩), have x_mem_A' : ∀ {N x}, (x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k) → x ∈ A' (M N x) (k N x) := λ N x h, mem_of_subset_of_mem (inter_subset_left _ _) (x_mem_A h), -- prove that for all N, (F N) has finite range have F_finite : ∀ {N}, finite (range (F N)) := begin assume N, apply finite_range_ite, { rw range_comp, exact finite_range_find_greatest.image _ }, { exact finite_range_const } end, -- prove that for all N, (F N) is a measurable function have F_measurable : ∀ {N}, measurable (F N) := begin assume N, refine measurable.piecewise _ _ measurable_const, show is_measurable (⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k), { simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, show measurable (λ x, e (k N x)), refine measurable_from_nat.comp (measurable_find_greatest _), assume k' k'_le_N, by_cases k'_eq_0 : k' = 0, -- if k' = 0 have : {x | k N x = 0} = (⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k)ᶜ ∪ (⋃ (m ≤ N), A m 0 \ ⋃ m' (hmm' : m < m') (hm'N : m' ≤ N) (k ≤ N), A m' k), { ext, split, { rw [mem_set_of_eq, mem_union_eq, or_iff_not_imp_left, mem_compl_eq, not_not_mem], assume k_eq_0 x_mem, simp only [not_exists, exists_prop, mem_Union, not_and, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_0, exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, { exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ } }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], by_cases x_mem : (x ∉ ⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k), { intro, apply find_greatest_eq_zero, assume k k_le_N hx, have : x ∈ ⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k, { simp only [mem_Union], use [M N x, nat.find_greatest_le, k, k_le_N, hx] }, contradiction }, { rw not_not_mem at x_mem, assume h, cases h, contradiction, simp only [not_exists, exists_prop, mem_Union, not_and, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, { have : x ∈ ⋃ k ≤ N, A m k, { exact mem_bUnion (nat.zero_le N) hx }, contradiction }, { exact ⟨m, m_le_N, mem_bUnion (nat.zero_le _) hx⟩ } }, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, rw ← k'_eq_0, exact k_unique ⟨x_mem_A x_mem, by { rw [k'_eq_0, M_eq_m], exact hx }⟩ } } }, -- end of `have` rw [k'_eq_0, this], apply is_measurable.union, { apply is_measurable.compl, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, { simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] }, -- if k' ≠ 0 have : {x | k N x = k'} = ⋃(m ≤ N), A m k' \ ⋃m' (hmm' : m < m') (hm'N : m' ≤ N) (k ≤ N), A m' k, { ext, split, { rw [mem_set_of_eq], assume k_eq_k', have x_mem : x ∈ ⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k, { have := find_greatest_of_ne_zero k_eq_k' k'_eq_0, simp only [mem_Union], use [M N x, nat.find_greatest_le, k', k'_le_N, this] }, simp only [not_exists, exists_prop, mem_Union, not_and, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_k', exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], assume h, have x_mem : x ∈ ⋃ (M : ℕ) (H : M ≤ N) (k : ℕ) (H : k ≤ N), A M k, { simp only [not_exists, exists_prop, mem_Union, not_and, mem_diff] at h, rcases h with ⟨m, hm, hx, _⟩, simp only [mem_Union], use [m, hm, k', k'_le_N, hx] }, simp only [not_exists, exists_prop, mem_Union, not_and, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, have : x ∈ ⋃ k ≤ N, A m k := mem_bUnion k'_le_N hx, contradiction, { simp only [mem_Union], use [m, m_le_N, k', k'_le_N, hx] }}, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, exact k_unique ⟨x_mem_A x_mem, by { rw M_eq_m, exact hx }⟩ } }, -- end of `have` rw this, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] end, -- start of proof ⟨λ N, ⟨F N, λ x, F_measurable (is_measurable_singleton _), F_finite⟩, -- The pointwise convergence part of the theorem λ x, ⟨metric.tendsto_at_top.2 $ λ ε hε, classical.by_cases --first case : f x = 0 ( assume fx_eq_0 : f x = 0, have x_not_mem_A' : ∀ {M k}, x ∉ A' M k := λ M k, begin simp only [mem_preimage, fx_eq_0, metric.mem_ball, one_div, norm_zero, not_and, not_lt, add_comm, not_le, dist_zero_right, mem_diff], assume h, rw add_comm, exact inv_pos_of_nat end, have x_not_mem_A : ∀ {M k}, x ∉ A M k := by { assume M k h, have := disjointed_subset h, exact absurd this x_not_mem_A' }, have F_eq_0 : ∀ {N}, F N x = 0 := λ N, by simp [F, if_neg, mem_Union, x_not_mem_A], -- end of `have` ⟨0, λ n hn, show dist (F n x) (f x) < ε, by {rw [fx_eq_0, F_eq_0, dist_self], exact hε}⟩ ) --second case : f x ≠ 0 ( assume fx_ne_0 : f x ≠ 0, let ⟨N₀, hN⟩ := exists_nat_one_div_lt (lt_min (norm_pos_iff.2 fx_ne_0) hε) in have norm_fx_gt : _ := (lt_min_iff.1 hN).1, have ε_gt : _ := (lt_min_iff.1 hN).2, have x_mem_Union_k_N₀ : x ∈ ⋃ k, A N₀ k := let ⟨k, hk⟩ := mem_closure_range_iff_nat.1 (by { rw E_dense, exact mem_univ (f x) }) N₀ in begin rw [Union_disjointed, mem_Union], use k, rw [mem_preimage], simp, rw [← one_div], exact ⟨hk, le_of_lt norm_fx_gt⟩ end, let ⟨k₀, x_mem_A⟩ := mem_Union.1 x_mem_Union_k_N₀ in let n := max N₀ k₀ in have x_mem_Union_Union : ∀ {N} (hN : n ≤ N), x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k := assume N hN, mem_Union.2 ⟨N₀, mem_Union.2 ⟨le_trans (le_max_left _ _) hN, mem_Union.2 ⟨k₀, mem_Union.2 ⟨le_trans (le_max_right _ _) hN, x_mem_A⟩⟩⟩⟩, have FN_eq : ∀ {N} (hN : n ≤ N), F N x = e (k N x) := assume N hN, if_pos $ x_mem_Union_Union hN, -- start of proof ⟨n, assume N hN, have N₀_le_N : N₀ ≤ N := le_trans (le_max_left _ _) hN, have k₀_le_N : k₀ ≤ N := le_trans (le_max_right _ _) hN, show dist (F N x) (f x) < ε, from calc dist (F N x) (f x) = dist (e (k N x)) (f x) : by rw FN_eq hN ... < 1 / ((M N x : ℝ) + 1) : begin have := x_mem_A' (x_mem_Union_Union hN), rw [mem_preimage, mem_diff, metric.mem_ball, dist_comm] at this, exact this.1 end ... ≤ 1 / ((N₀ : ℝ) + 1) : @one_div_le_one_div_of_le _ _ ((N₀ : ℝ) + 1) ((M N x : ℝ) + 1) (nat.cast_add_one_pos N₀) (add_le_add_right (nat.cast_le.2 (nat.le_find_greatest N₀_le_N (mem_bUnion k₀_le_N x_mem_A))) 1) ... < ε : ε_gt ⟩ ), -- second part of the theorem assume N, show ∥F N x∥ ≤ ∥f x∥ + ∥f x∥, from classical.by_cases ( assume h : x ∈ ⋃ M ≤ N, ⋃ k ≤ N, A M k, calc ∥F N x∥ = dist (F N x) 0 : by simp ... = dist (e (k N x)) 0 : begin simp only [F], rw if_pos h end ... ≤ dist (e (k N x)) (f x) + dist (f x) 0 : dist_triangle _ _ _ ... = dist (e (k N x)) (f x) + ∥f x∥ : by simp ... ≤ 1 / ((M N x : ℝ) + 1) + ∥f x∥ : le_of_lt $ add_lt_add_right (dist_ek_fx (x_mem_A h)) _ ... ≤ ∥f x∥ + ∥f x∥ : add_le_add_right (norm_fx (x_mem_A h) ) _) ( assume h : x ∉ ⋃ M ≤ N, ⋃ k ≤ N, A M k, have F_eq_0 : F N x = 0 := if_neg h, by { simp only [F_eq_0, norm_zero], exact add_nonneg (norm_nonneg _) (norm_nonneg _) } )⟩⟩ lemma simple_func_sequence_tendsto' {μ : measure α} {f : α → β} (hf : integrable f μ) : ∃ (F : ℕ → (α →ₛ β)), (∀n, integrable (F n) μ) ∧ tendsto (λ n, ∫⁻ x, nndist (F n x) (f x) ∂μ) at_top (𝓝 0) := let ⟨F, hF⟩ := simple_func_sequence_tendsto hf.measurable in let G : ℕ → α → ennreal := λn x, nndist (F n x) (f x) in let g : α → ennreal := λx, nnnorm (f x) + nnnorm (f x) + nnnorm (f x) in have hF_meas : ∀ n, measurable (G n) := λ n, measurable_coe.comp $ (F n).measurable.nndist hf.measurable, have hg_meas : measurable g := measurable_coe.comp $ (hf.measurable.nnnorm.add hf.measurable.nnnorm).add hf.measurable.nnnorm, have h_bound : ∀ n, G n ≤ᵐ[μ] g := λ n, ae_of_all _ $ λ x, coe_le_coe.2 $ calc nndist (F n x) (f x) ≤ nndist (F n x) 0 + nndist 0 (f x) : nndist_triangle _ _ _ ... = nnnorm (F n x) + nnnorm (f x) : by simp [nndist_eq_nnnorm] ... ≤ nnnorm (f x) + nnnorm (f x) + nnnorm (f x) : by { simp [nnreal.coe_le_coe.symm, (hF x).2, add_comm] }, have h_finite : ∫⁻ x, g x ∂μ < ⊤ := calc ∫⁻ x, nnnorm (f x) + nnnorm (f x) + nnnorm (f x) ∂μ = ∫⁻ x, nnnorm (f x) ∂μ + ∫⁻ x, nnnorm (f x) ∂μ + ∫⁻ x, nnnorm (f x) ∂μ : by { rw [lintegral_add, lintegral_nnnorm_add hf.measurable hf.measurable], exacts [hf.measurable.ennnorm.add hf.measurable.ennnorm, hf.measurable.ennnorm] } ... < ⊤ : by { simp only [and_self, add_lt_top], exact hf.has_finite_integral }, have h_lim : ∀ᵐ x ∂μ, tendsto (λ n, G n x) at_top (𝓝 0) := ae_of_all _ $ λ x, begin apply (@tendsto_coe ℕ at_top (λ n, nndist (F n x) (f x)) 0).2, apply (@nnreal.tendsto_coe ℕ at_top (λ n, nndist (F n x) (f x)) 0).1, apply tendsto_iff_dist_tendsto_zero.1 (hF x).1 end, begin use F, split, { assume n, use (F n).measurable, calc ∫⁻ a, nnnorm (F n a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (f a) ∂μ : lintegral_mono (by { assume a, simp only [coe_add.symm, coe_le_coe], exact (hF a).2 n }) ... = ∫⁻ a, nnnorm (f a) ∂μ + ∫⁻ a, nnnorm (f a) ∂μ : lintegral_nnnorm_add hf.measurable hf.measurable ... < ⊤ : by simp only [add_lt_top, and_self]; exact hf.has_finite_integral }, convert tendsto_lintegral_of_dominated_convergence g hF_meas h_bound h_finite h_lim, simp only [lintegral_zero] end end measure_theory
aeb087ce7da967e815029848b1e5034010a7d4ba
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/analysis/special_functions/pow.lean
db2fb2534a05d2624c7fb7a39a062e7f01af5248
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
49,412
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel -/ import analysis.special_functions.trigonometric import analysis.calculus.extend_deriv /-! # Power function on `ℂ`, `ℝ` and `ℝ⁺` We construct the power functions `x ^ y` where `x` and `y` are complex numbers, or `x` and `y` are real numbers, or `x` is a nonnegative real and `y` is real, and prove their basic properties. -/ noncomputable theory open_locale classical real topological_space nnreal namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp [cpow_def]; split_ifs; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp [cpow_def]; split_ifs; simp [exp_neg] lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ := by simpa using cpow_neg x 1 @[simp] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_add, hx, pow_add, cpow_nat_cast n] @[simp] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp; refl | -[1+ n] := by rw fpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℂ)) ^ n = x := have (log x * (↑n)⁻¹).im = (log x).im / n, by rw [div_eq_mul_inv, ← of_real_nat_cast, ← of_real_inv, mul_im, of_real_re, of_real_im]; simp, have h : -π < (log x * (↑n)⁻¹).im ∧ (log x * (↑n)⁻¹).im ≤ π, from (le_total (log x).im 0).elim (λ h, ⟨calc -π < (log x).im : by simp [log, neg_pi_lt_arg] ... ≤ ((log x).im * 1) / n : (le_div_iff (nat.cast_pos.2 hn : (0 : ℝ) < _)).mpr (mul_le_mul_of_nonpos_left (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h) ... = (log x * (↑n)⁻¹).im : by simp [this], this.symm ▸ le_trans (div_nonpos_of_nonpos_of_nonneg h n.cast_nonneg) (le_of_lt real.pi_pos)⟩) (λ h, ⟨this.symm ▸ lt_of_lt_of_le (neg_neg_of_pos real.pi_pos) (div_nonneg h n.cast_nonneg), calc (log x * (↑n)⁻¹).im = (1 * (log x).im) / n : by simp [this] ... ≤ (log x).im : (div_le_iff' (nat.cast_pos.2 hn : (0 : ℝ) < _)).mpr (mul_le_mul_of_nonneg_right (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h) ... ≤ _ : by simp [log, arg_le_pi]⟩), by rw [← cpow_nat_cast, ← cpow_mul _ h.1 h.2, inv_mul_cancel (show (n : ℂ) ≠ 0, from nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), cpow_one] end complex namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos lemma abs_rpow_le_abs_rpow (x y : ℝ) : abs (x ^ y) ≤ abs (x) ^ y := abs_le_of_le_of_neg_le begin cases lt_trichotomy 0 x, { rw abs_of_pos h }, cases h, { simp [h.symm] }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc exp (log x * y) * cos (y * π) ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (cos_le_one _) (le_of_lt $ exp_pos _) ... = _ : mul_one _ end begin cases lt_trichotomy 0 x, { rw abs_of_pos h, have : 0 < x^y := rpow_pos_of_pos h _, linarith }, cases h, { simp only [h.symm, abs_zero, rpow_def_of_nonneg], split_ifs, repeat {norm_num} }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc -(exp (log x * y) * cos (y * π)) = exp (log x * y) * (-cos (y * π)) : by ring ... ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (neg_le.2 $ neg_one_le_cos _) (le_of_lt $ exp_pos _) ... = exp (log x * y) : mul_one _ end end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx] @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := begin rw [real.rpow_def_of_nonneg (abs_nonneg _), complex.cpow_def], split_ifs; simp [*, abs_of_nonneg (le_of_lt (real.exp_pos _)), complex.log, complex.exp_add, add_mul, mul_right_comm _ I, exp_mul_I, abs_cos_add_sin_mul_I, (complex.of_real_mul _ _).symm, -complex.of_real_mul] at * end @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] end complex namespace real variables {x y z : ℝ} @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma rpow_add {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { simp only [← H, h, rpow_eq_zero_iff_of_nonneg, true_and, zero_rpow, eq_self_iff_true, ne.def, not_false_iff, zero_eq_mul], by_contradiction F, push_neg at F, apply h, simp [F] }, { exact rpow_add pos _ _ } end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] @[simp] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_pow _ _).symm, complex.cpow_nat_cast, complex.of_real_nat_cast, complex.of_real_re] @[simp] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_fpow _ _).symm, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by exact_mod_cast H, simp only [rpow_int_cast, fpow_one, fpow_neg], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, cases lt_or_eq_of_le h with h₂ h₂, exact h₂, exfalso, apply h_2, exact eq.symm h₂, have hy : 0 < y, cases lt_or_eq_of_le h₁ with h₂ h₂, exact h₂, exfalso, apply h_3, exact eq.symm h₂, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁}, { exact h}, { exact mul_nonneg h h₁}, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := begin by_cases hy0 : y = 0, { simp [*] }, by_cases hx0 : x = 0, { simp [*] }, simp only [real.rpow_def_of_nonneg hx, real.rpow_def_of_nonneg (inv_nonneg.2 hx), if_false, hx0, mt inv_eq_zero.1 hx0, log_inv, ← neg_mul_eq_neg_mul, exp_neg] end lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] section prove_rpow_is_continuous lemma continuous_rpow_aux1 : continuous (λp : {p:ℝ×ℝ // 0 < p.1}, p.val.1 ^ p.val.2) := suffices h : continuous (λ p : {p:ℝ×ℝ // 0 < p.1 }, exp (log p.val.1 * p.val.2)), by { convert h, ext p, rw rpow_def_of_pos p.2 }, continuous_exp.comp $ (show continuous ((λp:{p:ℝ//0 < p}, log (p.val)) ∘ (λp:{p:ℝ×ℝ//0<p.fst}, ⟨p.val.1, p.2⟩)), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id) lemma continuous_rpow_aux2 : continuous (λ p : {p:ℝ×ℝ // p.1 < 0}, p.val.1 ^ p.val.2) := suffices h : continuous (λp:{p:ℝ×ℝ // p.1 < 0}, exp (log (-p.val.1) * p.val.2) * cos (p.val.2 * π)), by { convert h, ext p, rw [rpow_def_of_neg p.2, log_neg_eq_log] }, (continuous_exp.comp $ (show continuous $ (λp:{p:ℝ//0<p}, log (p.val))∘(λp:{p:ℝ×ℝ//p.1<0}, ⟨-p.val.1, neg_pos_of_neg p.2⟩), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_neg.comp $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id)).mul (continuous_cos.comp $ (continuous_snd.comp $ continuous_subtype_val.comp continuous_id).mul continuous_const) lemma continuous_at_rpow_of_ne_zero (hx : x ≠ 0) (y : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := begin cases lt_trichotomy 0 x, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux1 _ h) (mem_nhds_sets (by { convert is_open_prod (is_open_lt' (0:ℝ)) is_open_univ, ext, finish }) h), cases h, { exact absurd h.symm hx }, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux2 _ h) (mem_nhds_sets (by { convert is_open_prod (is_open_gt' (0:ℝ)) is_open_univ, ext, finish }) h) end lemma continuous_rpow_aux3 : continuous (λ p : {p:ℝ×ℝ // 0 < p.2}, p.val.1 ^ p.val.2) := continuous_iff_continuous_at.2 $ λ ⟨(x₀, y₀), hy₀⟩, begin by_cases hx₀ : x₀ = 0, { simp only [continuous_at, hx₀, zero_rpow (ne_of_gt hy₀), metric.tendsto_nhds_nhds], assume ε ε0, rcases exists_pos_rat_lt (half_pos hy₀) with ⟨q, q_pos, q_lt⟩, let q := (q:ℝ), replace q_pos : 0 < q := rat.cast_pos.2 q_pos, let δ := min (min q (ε ^ (1 / q))) (1/2), have δ0 : 0 < δ := lt_min (lt_min q_pos (rpow_pos_of_pos ε0 _)) (by norm_num), have : δ ≤ q := le_trans (min_le_left _ _) (min_le_left _ _), have : δ ≤ ε ^ (1 / q) := le_trans (min_le_left _ _) (min_le_right _ _), have : δ < 1 := lt_of_le_of_lt (min_le_right _ _) (by norm_num), use δ, use δ0, rintros ⟨⟨x, y⟩, hy⟩, simp only [subtype.dist_eq, real.dist_eq, prod.dist_eq, sub_zero, subtype.coe_mk], assume h, rw max_lt_iff at h, cases h with xδ yy₀, have qy : q < y, calc q < y₀ / 2 : q_lt ... = y₀ - y₀ / 2 : (sub_half _).symm ... ≤ y₀ - δ : by linarith ... < y : sub_lt_of_abs_sub_lt_left yy₀, calc abs(x^y) ≤ abs(x)^y : abs_rpow_le_abs_rpow _ _ ... < δ ^ y : rpow_lt_rpow (abs_nonneg _) xδ hy ... < δ ^ q : by { refine rpow_lt_rpow_of_exponent_gt _ _ _, repeat {linarith} } ... ≤ (ε ^ (1 / q)) ^ q : by { refine rpow_le_rpow _ _ _, repeat {linarith} } ... = ε : by { rw [← rpow_mul, div_mul_cancel, rpow_one], exact ne_of_gt q_pos, linarith }}, { exact (continuous_within_at_iff_continuous_at_restrict (λp:ℝ×ℝ, p.1^p.2) _).1 (continuous_at_rpow_of_ne_zero hx₀ _).continuous_within_at } end lemma continuous_at_rpow_of_pos (hy : 0 < y) (x : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux3 _ hy) (mem_nhds_sets (by { convert is_open_prod is_open_univ (is_open_lt' (0:ℝ)), ext, finish }) hy) lemma continuous_at_rpow {x y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := by { cases h, exact continuous_at_rpow_of_ne_zero h _, exact continuous_at_rpow_of_pos h x } variables {α : Type*} [topological_space α] {f g : α → ℝ} /-- `real.rpow` is continuous at all points except for the lower half of the y-axis. In other words, the function `λp:ℝ×ℝ, p.1^p.2` is continuous at `(x, y)` if `x ≠ 0` or `y > 0`. Multiple forms of the claim is provided in the current section. -/ lemma continuous_rpow (h : ∀a, f a ≠ 0 ∨ 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_iff_continuous_at.2 $ λ a, begin show continuous_at ((λp:ℝ×ℝ, p.1^p.2) ∘ (λa, (f a, g a))) a, refine continuous_at.comp _ (continuous_iff_continuous_at.1 (hf.prod_mk hg) _), { replace h := h a, cases h, { exact continuous_at_rpow_of_ne_zero h _ }, { exact continuous_at_rpow_of_pos h _ }}, end lemma continuous_rpow_of_ne_zero (h : ∀a, f a ≠ 0) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inl $ h a) hf hg lemma continuous_rpow_of_pos (h : ∀a, 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inr $ h a) hf hg end prove_rpow_is_continuous section prove_rpow_is_differentiable lemma has_deriv_at_rpow_of_pos {x : ℝ} (h : 0 < x) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin have : has_deriv_at (λ x, exp (log x * p)) (p * x^(p-1)) x, { convert (has_deriv_at_exp _).comp x ((has_deriv_at_log (ne_of_gt h)).mul_const p) using 1, field_simp [rpow_def_of_pos h, mul_sub, exp_sub, exp_log h, ne_of_gt h], ring }, apply this.congr_of_eventually_eq, have : set.Ioi (0 : ℝ) ∈ 𝓝 x := mem_nhds_sets is_open_Ioi h, exact filter.eventually_of_mem this (λ y hy, rpow_def_of_pos hy _) end lemma has_deriv_at_rpow_of_neg {x : ℝ} (h : x < 0) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin have : has_deriv_at (λ x, exp (log x * p) * cos (p * π)) (p * x^(p-1)) x, { convert ((has_deriv_at_exp _).comp x ((has_deriv_at_log (ne_of_lt h)).mul_const p)).mul_const _ using 1, field_simp [rpow_def_of_neg h, mul_sub, exp_sub, sub_mul, cos_sub, exp_log_of_neg h, ne_of_lt h], ring }, apply this.congr_of_eventually_eq, have : set.Iio (0 : ℝ) ∈ 𝓝 x := mem_nhds_sets is_open_Iio h, exact filter.eventually_of_mem this (λ y hy, rpow_def_of_neg hy _) end lemma has_deriv_at_rpow {x : ℝ} (h : x ≠ 0) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin rcases lt_trichotomy x 0 with H|H|H, { exact has_deriv_at_rpow_of_neg H p }, { exact (h H).elim }, { exact has_deriv_at_rpow_of_pos H p }, end lemma has_deriv_at_rpow_zero_of_one_le {p : ℝ} (h : 1 ≤ p) : has_deriv_at (λ x, x^p) (p * (0 : ℝ)^(p-1)) 0 := begin apply has_deriv_at_of_has_deriv_at_of_ne (λ x hx, has_deriv_at_rpow hx p), { exact (continuous_rpow_of_pos (λ _, (lt_of_lt_of_le zero_lt_one h)) continuous_id continuous_const).continuous_at }, { rcases le_iff_eq_or_lt.1 h with rfl|h, { simp [continuous_const.continuous_at] }, { exact (continuous_const.mul (continuous_rpow_of_pos (λ _, sub_pos_of_lt h) continuous_id continuous_const)).continuous_at } } end lemma has_deriv_at_rpow_of_one_le (x : ℝ) {p : ℝ} (h : 1 ≤ p) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin by_cases hx : x = 0, { rw hx, exact has_deriv_at_rpow_zero_of_one_le h }, { exact has_deriv_at_rpow hx p } end end prove_rpow_is_differentiable section sqrt lemma sqrt_eq_rpow : sqrt = λx:ℝ, x ^ (1/(2:ℝ)) := begin funext, by_cases h : 0 ≤ x, { rw [← mul_self_inj_of_nonneg, mul_self_sqrt h, ← pow_two, ← rpow_nat_cast, ← rpow_mul h], norm_num, exact sqrt_nonneg _, exact rpow_nonneg_of_nonneg h _ }, { replace h : x < 0 := lt_of_not_ge h, have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos (le_of_lt h), rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma continuous_sqrt : continuous sqrt := by rw sqrt_eq_rpow; exact continuous_rpow_of_pos (λa, by norm_num) continuous_id continuous_const end sqrt end real section differentiability open real variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} (p : ℝ) /- Differentiability statements for the power of a function, when the function does not vanish and the exponent is arbitrary-/ lemma has_deriv_within_at.rpow (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) s x := begin convert (has_deriv_at_rpow hx p).comp_has_deriv_within_at x hf using 1, ring end lemma has_deriv_at.rpow (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) x := begin rw ← has_deriv_within_at_univ at *, exact hf.rpow p hx end lemma differentiable_within_at.rpow (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, (f x)^p) s x := (hf.has_deriv_within_at.rpow p hx).differentiable_within_at @[simp] lemma differentiable_at.rpow (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, (f x)^p) x := (hf.has_deriv_at.rpow p hx).differentiable_at lemma differentiable_on.rpow (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, (f x)^p) s := λx h, (hf x h).rpow p (hx x h) @[simp] lemma differentiable.rpow (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, (f x)^p) := λx, (hf x).rpow p (hx x) lemma deriv_within_rpow (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, (f x)^p) s x = (deriv_within f s x) * p * (f x)^(p-1) := (hf.has_deriv_within_at.rpow p hx).deriv_within hxs @[simp] lemma deriv_rpow (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, (f x)^p) x = (deriv f x) * p * (f x)^(p-1) := (hf.has_deriv_at.rpow p hx).deriv /- Differentiability statements for the power of a function, when the function may vanish but the exponent is at least one. -/ variable {p} lemma has_deriv_within_at.rpow_of_one_le (hf : has_deriv_within_at f f' s x) (hp : 1 ≤ p) : has_deriv_within_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) s x := begin convert (has_deriv_at_rpow_of_one_le (f x) hp).comp_has_deriv_within_at x hf using 1, ring end lemma has_deriv_at.rpow_of_one_le (hf : has_deriv_at f f' x) (hp : 1 ≤ p) : has_deriv_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) x := begin rw ← has_deriv_within_at_univ at *, exact hf.rpow_of_one_le hp end lemma differentiable_within_at.rpow_of_one_le (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) : differentiable_within_at ℝ (λx, (f x)^p) s x := (hf.has_deriv_within_at.rpow_of_one_le hp).differentiable_within_at @[simp] lemma differentiable_at.rpow_of_one_le (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : differentiable_at ℝ (λx, (f x)^p) x := (hf.has_deriv_at.rpow_of_one_le hp).differentiable_at lemma differentiable_on.rpow_of_one_le (hf : differentiable_on ℝ f s) (hp : 1 ≤ p) : differentiable_on ℝ (λx, (f x)^p) s := λx h, (hf x h).rpow_of_one_le hp @[simp] lemma differentiable.rpow_of_one_le (hf : differentiable ℝ f) (hp : 1 ≤ p) : differentiable ℝ (λx, (f x)^p) := λx, (hf x).rpow_of_one_le hp lemma deriv_within_rpow_of_one_le (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, (f x)^p) s x = (deriv_within f s x) * p * (f x)^(p-1) := (hf.has_deriv_within_at.rpow_of_one_le hp).deriv_within hxs @[simp] lemma deriv_rpow_of_one_le (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : deriv (λx, (f x)^p) x = (deriv f x) * p * (f x)^(p-1) := (hf.has_deriv_at.rpow_of_one_le hp).deriv /- Differentiability statements for the square root of a function, when the function does not vanish -/ lemma has_deriv_within_at.sqrt (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, sqrt (f y)) (f' / (2 * sqrt (f x))) s x := begin simp only [sqrt_eq_rpow], convert hf.rpow (1/2) hx, rcases lt_trichotomy (f x) 0 with H|H|H, { have A : (f x)^((1:ℝ)/2) = 0, { rw rpow_def_of_neg H, have : cos (1/2 * π) = 0, by { convert cos_pi_div_two using 2, ring }, rw [this], simp }, have B : f x ^ ((1:ℝ) / 2 - 1) = 0, { rw rpow_def_of_neg H, have : cos (π/2 - π) = 0, by simp [cos_sub], have : cos (((1:ℝ)/2 - 1) * π) = 0, by { convert this using 2, ring }, rw this, simp }, rw [A, B], simp }, { exact (hx H).elim }, { have A : 0 < (f x)^((1:ℝ)/2) := rpow_pos_of_pos H _, have B : (f x) ^ (-(1:ℝ)) = (f x)^(-((1:ℝ)/2)) * (f x)^(-((1:ℝ)/2)), { rw [← rpow_add H], congr, norm_num }, rw [sub_eq_add_neg, rpow_add H, B, rpow_neg (le_of_lt H)], field_simp [hx, ne_of_gt A], ring } end lemma has_deriv_at.sqrt (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, sqrt (f y)) (f' / (2 * sqrt(f x))) x := begin rw ← has_deriv_within_at_univ at *, exact hf.sqrt hx end lemma differentiable_within_at.sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, sqrt (f x)) s x := (hf.has_deriv_within_at.sqrt hx).differentiable_within_at @[simp] lemma differentiable_at.sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, sqrt (f x)) x := (hf.has_deriv_at.sqrt hx).differentiable_at lemma differentiable_on.sqrt (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, sqrt (f x)) s := λx h, (hf x h).sqrt (hx x h) @[simp] lemma differentiable.sqrt (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, sqrt (f x)) := λx, (hf x).sqrt (hx x) lemma deriv_within_sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, sqrt (f x)) s x = (deriv_within f s x) / (2 * sqrt (f x)) := (hf.has_deriv_within_at.sqrt hx).deriv_within hxs @[simp] lemma deriv_sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, sqrt (f x)) x = (deriv f x) / (2 * sqrt (f x)) := (hf.has_deriv_at.sqrt hx).deriv end differentiability namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (zero_lt_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (zero_lt_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx : 0 ≤ x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one hx hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) := begin have : (λp:ℝ≥0×ℝ, p.1^p.2) = nnreal.of_real ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, nnreal.coe_of_real _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine nnreal.continuous_of_real.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end end nnreal open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (tendsto.prod_mk_nhds hx hy) namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ennreal` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ennreal → ℝ → ennreal | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ennreal ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ennreal) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ennreal} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ennreal) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ennreal) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ennreal) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ennreal) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ennreal) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ennreal) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ennreal) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ennreal) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end @[simp] lemma rpow_one (x : ennreal) : x ^ (1 : ℝ) = x := by cases x; dsimp only [(^), rpow]; simp [zero_lt_one, not_lt_of_le zero_le_one] @[simp] lemma one_rpow (x : ℝ) : (1 : ennreal) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ennreal} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ennreal} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_add {x : ennreal} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ennreal) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_neg_one (x : ennreal) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ennreal) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ennreal) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ennreal) * y) ^ z = x^z * y^z := begin rcases lt_trichotomy z 0 with H|H|H, { by_cases hx : x = 0; by_cases hy : y = 0, { simp [hx, hy, zero_rpow_of_neg, H] }, { have : (y : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hy], simp [hx, hy, zero_rpow_of_neg, H, with_top.top_mul this] }, { have : (x : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hx], simp [hx, hy, zero_rpow_of_neg H, with_top.mul_top this] }, { rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx, coe_rpow_of_ne_zero hy], simp [hx, hy] } }, { simp [H] }, { by_cases hx : x = 0; by_cases hy : y = 0, { simp [hx, hy, zero_rpow_of_pos, H] }, { have : (y : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hy], simp [hx, hy, zero_rpow_of_pos H, with_top.top_mul this] }, { have : (x : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hx], simp [hx, hy, zero_rpow_of_pos H, with_top.mul_top this] }, { rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx, coe_rpow_of_ne_zero hy], simp [hx, hy] } }, end lemma mul_rpow_of_ne_top {x y : ennreal} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := begin lift x to ℝ≥0 using hx, lift y to ℝ≥0 using hy, exact coe_mul_rpow x y z end lemma mul_rpow_of_ne_zero {x y : ennreal} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := begin rcases lt_trichotomy z 0 with H|H|H, { cases x; cases y, { simp [hx, hy, top_rpow_of_neg, H] }, { have : y ≠ 0, by simpa using hy, simp [hx, hy, top_rpow_of_neg, H, rpow_eq_zero_iff, this] }, { have : x ≠ 0, by simpa using hx, simp [hx, hy, top_rpow_of_neg, H, rpow_eq_zero_iff, this] }, { have hx' : x ≠ 0, by simpa using hx, have hy' : y ≠ 0, by simpa using hy, simp only [some_eq_coe], rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx', coe_rpow_of_ne_zero hy'], simp [hx', hy'] } }, { simp [H] }, { cases x; cases y, { simp [hx, hy, top_rpow_of_pos, H] }, { have : y ≠ 0, by simpa using hy, simp [hx, hy, top_rpow_of_pos, H, rpow_eq_zero_iff, this] }, { have : x ≠ 0, by simpa using hx, simp [hx, hy, top_rpow_of_pos, H, rpow_eq_zero_iff, this] }, { have hx' : x ≠ 0, by simpa using hx, have hy' : y ≠ 0, by simpa using hy, simp only [some_eq_coe], rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx', coe_rpow_of_ne_zero hy'], simp [hx', hy'] } } end lemma mul_rpow_of_nonneg (x y : ennreal) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := begin rcases le_iff_eq_or_lt.1 hz with H|H, { simp [← H] }, by_cases h : x = 0 ∨ y = 0, { cases h; simp [h, zero_rpow_of_pos H] }, push_neg at h, exact mul_rpow_of_ne_zero h.1 h.2 z end lemma one_le_rpow {x : ennreal} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := begin cases x, { rcases le_iff_eq_or_lt.1 h₁ with H|H, { simp [← H, le_refl] }, { simp [top_rpow_of_pos H] } }, { simp only [one_le_coe_iff, some_eq_coe] at h, simp [coe_rpow_of_nonneg _ h₁, nnreal.one_le_rpow h h₁] } end lemma rpow_le_rpow {x y : ennreal} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases le_iff_eq_or_lt.1 h₂ with H|H, { simp [← H, le_refl] }, cases y, { simp [top_rpow_of_pos H] }, cases x, { exact (not_top_le_coe h₁).elim }, simp at h₁, simp [coe_rpow_of_nonneg _ h₂, nnreal.rpow_le_rpow h₁ h₂] end lemma rpow_lt_rpow {x y : ennreal} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := begin cases x, { exact (not_top_lt h₁).elim }, cases y, { simp [top_rpow_of_pos h₂, coe_rpow_of_nonneg _ (le_of_lt h₂)] }, simp at h₁, simp [coe_rpow_of_nonneg _ (le_of_lt h₂), nnreal.rpow_lt_rpow h₁ h₂] end lemma rpow_lt_rpow_of_exponent_lt {x : ennreal} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ennreal} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ennreal} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ennreal} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { simp at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_le_one {x : ennreal} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp at hx2, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx2 hz] end lemma one_lt_rpow {x : ennreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma rpow_lt_one {x : ennreal} {z : ℝ} (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := begin by_cases h : x = 0, { simp [h, zero_rpow_of_pos hz, ennreal.zero_lt_one] }, { lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp at h hx1, have : 0 ≤ x := le_of_lt (bot_lt_iff_ne_bot.mpr h), simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one this hx1 hz] } end lemma to_real_rpow (x : ennreal) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end end ennreal
f97315829e452af2fc4ce4e1e81c266fd3e9ab88
69bc7d0780be17e452d542a93f9599488f1c0c8e
/9-26-2019.lean
c11a3d6c492e1a70742bea7dc4dcb24504560180
[]
no_license
joek13/cs2102-notes
b7352285b1d1184fae25594f89f5926d74e6d7b4
25bb18788641b20af9cf3c429afe1da9b2f5eafb
refs/heads/master
1,673,461,162,867
1,575,561,090,000
1,575,561,090,000
207,573,549
0
0
null
null
null
null
UTF-8
Lean
false
false
1,661
lean
/- Notes 9/26/2019 First, let's recall some facts from the last lecture: - List are polymorphic (they abstract over generic types—they've been parameterized) - Lists have two constructors (nil, and cons h t) - cons h t takes a head element and a tail list; nesting cons expressions lets us make lists of any length [1,2,3,4,5] = (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 nil))))) Let's warm up that cache, says Kevin Sullivan. Recall an instance of "recursive thinking:" - we consider the inductive definition of some datatype when we're planning operations on that type -/ open list def countdown : nat → list nat | 0 := [0] | (nat.succ x) := (cons (nat.succ x) (countdown (x))) def countdown' : nat → list nat | 0 := [0] | n := (cons n (countdown (n-1))) -- countdown 10 = (cons 10 (countdown 9)) #eval countdown 5 #eval countdown' 6 def lt : nat → nat → bool | a b := a < b def fib : nat → nat | 0 := 0 | 1 := 1 | (nat.succ (nat.succ n)) := fib n + fib(n+1) def fib_invalid : nat → nat | 0 := 0 | 1 := 1 | n := fib(n-1) + fib (n-2) #eval fib_invalid 3 -- Higher-ordered function: a function that takes a function as an argument -- Read this definition super carefully! def kevinmap : (nat → nat) → list nat → list nat | f nil := nil | f (cons h t) := (cons (f h) (kevinmap f t)) #eval kevinmap (λ x, x*2) [1,2,3] -- (reduce + 0 [1,2,3]) - -- reduce applies the same function to each successive element def mreduce : (nat → nat → nat) → nat → list nat → nat | f ident nil := ident | f ident (cons h t) := (f h (mreduce f ident t)) #eval mreduce (λ a b, a+b) 0 [1,2,3] #eval mreduce (λ a b, a*b) 1 [1,2,3,4]
42dfe113661f9965eb41066906ccceea8f2cff8c
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/shapes/pullbacks.lean
a93b3dec670d2aedc6082b54dc492b10cfd2c196
[ "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
90,459
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel, Bhavik Mehta, Andrew Yang -/ import category_theory.limits.shapes.wide_pullbacks import category_theory.limits.shapes.binary_products /-! # Pullbacks We define a category `walking_cospan` (resp. `walking_span`), which is the index category for the given data for a pullback (resp. pushout) diagram. Convenience methods `cospan f g` and `span f g` construct functors from the walking (co)span, hitting the given morphisms. We define `pullback f g` and `pushout f g` as limits and colimits of such functors. ## References * [Stacks: Fibre products](https://stacks.math.columbia.edu/tag/001U) * [Stacks: Pushouts](https://stacks.math.columbia.edu/tag/0025) -/ noncomputable theory open category_theory namespace category_theory.limits universes w v₁ v₂ v u u₂ local attribute [tidy] tactic.case_bash /-- The type of objects for the diagram indexing a pullback, defined as a special case of `wide_pullback_shape`. -/ abbreviation walking_cospan : Type := wide_pullback_shape walking_pair /-- The left point of the walking cospan. -/ @[pattern] abbreviation walking_cospan.left : walking_cospan := some walking_pair.left /-- The right point of the walking cospan. -/ @[pattern] abbreviation walking_cospan.right : walking_cospan := some walking_pair.right /-- The central point of the walking cospan. -/ @[pattern] abbreviation walking_cospan.one : walking_cospan := none /-- The type of objects for the diagram indexing a pushout, defined as a special case of `wide_pushout_shape`. -/ abbreviation walking_span : Type := wide_pushout_shape walking_pair /-- The left point of the walking span. -/ @[pattern] abbreviation walking_span.left : walking_span := some walking_pair.left /-- The right point of the walking span. -/ @[pattern] abbreviation walking_span.right : walking_span := some walking_pair.right /-- The central point of the walking span. -/ @[pattern] abbreviation walking_span.zero : walking_span := none namespace walking_cospan /-- The type of arrows for the diagram indexing a pullback. -/ abbreviation hom : walking_cospan → walking_cospan → Type := wide_pullback_shape.hom /-- The left arrow of the walking cospan. -/ @[pattern] abbreviation hom.inl : left ⟶ one := wide_pullback_shape.hom.term _ /-- The right arrow of the walking cospan. -/ @[pattern] abbreviation hom.inr : right ⟶ one := wide_pullback_shape.hom.term _ /-- The identity arrows of the walking cospan. -/ @[pattern] abbreviation hom.id (X : walking_cospan) : X ⟶ X := wide_pullback_shape.hom.id X instance (X Y : walking_cospan) : subsingleton (X ⟶ Y) := by tidy end walking_cospan namespace walking_span /-- The type of arrows for the diagram indexing a pushout. -/ abbreviation hom : walking_span → walking_span → Type := wide_pushout_shape.hom /-- The left arrow of the walking span. -/ @[pattern] abbreviation hom.fst : zero ⟶ left := wide_pushout_shape.hom.init _ /-- The right arrow of the walking span. -/ @[pattern] abbreviation hom.snd : zero ⟶ right := wide_pushout_shape.hom.init _ /-- The identity arrows of the walking span. -/ @[pattern] abbreviation hom.id (X : walking_span) : X ⟶ X := wide_pushout_shape.hom.id X instance (X Y : walking_span) : subsingleton (X ⟶ Y) := by tidy end walking_span open walking_span.hom walking_cospan.hom wide_pullback_shape.hom wide_pushout_shape.hom variables {C : Type u} [category.{v} C] /-- To construct an isomorphism of cones over the walking cospan, it suffices to construct an isomorphism of the cone points and check it commutes with the legs to `left` and `right`. -/ def walking_cospan.ext {F : walking_cospan ⥤ C} {s t : cone F} (i : s.X ≅ t.X) (w₁ : s.π.app walking_cospan.left = i.hom ≫ t.π.app walking_cospan.left) (w₂ : s.π.app walking_cospan.right = i.hom ≫ t.π.app walking_cospan.right) : s ≅ t := begin apply cones.ext i, rintro (⟨⟩|⟨⟨⟩⟩), { have h₁ := s.π.naturality walking_cospan.hom.inl, dsimp at h₁, simp only [category.id_comp] at h₁, have h₂ := t.π.naturality walking_cospan.hom.inl, dsimp at h₂, simp only [category.id_comp] at h₂, simp_rw [h₂, ←category.assoc, ←w₁, ←h₁], }, { exact w₁, }, { exact w₂, }, end /-- To construct an isomorphism of cocones over the walking span, it suffices to construct an isomorphism of the cocone points and check it commutes with the legs from `left` and `right`. -/ def walking_span.ext {F : walking_span ⥤ C} {s t : cocone F} (i : s.X ≅ t.X) (w₁ : s.ι.app walking_cospan.left ≫ i.hom = t.ι.app walking_cospan.left) (w₂ : s.ι.app walking_cospan.right ≫ i.hom = t.ι.app walking_cospan.right) : s ≅ t := begin apply cocones.ext i, rintro (⟨⟩|⟨⟨⟩⟩), { have h₁ := s.ι.naturality walking_span.hom.fst, dsimp at h₁, simp only [category.comp_id] at h₁, have h₂ := t.ι.naturality walking_span.hom.fst, dsimp at h₂, simp only [category.comp_id] at h₂, simp_rw [←h₁, category.assoc, w₁, h₂], }, { exact w₁, }, { exact w₂, }, end /-- `cospan f g` is the functor from the walking cospan hitting `f` and `g`. -/ def cospan {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : walking_cospan ⥤ C := wide_pullback_shape.wide_cospan Z (λ j, walking_pair.cases_on j X Y) (λ j, walking_pair.cases_on j f g) /-- `span f g` is the functor from the walking span hitting `f` and `g`. -/ def span {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : walking_span ⥤ C := wide_pushout_shape.wide_span X (λ j, walking_pair.cases_on j Y Z) (λ j, walking_pair.cases_on j f g) @[simp] lemma cospan_left {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).obj walking_cospan.left = X := rfl @[simp] lemma span_left {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).obj walking_span.left = Y := rfl @[simp] lemma cospan_right {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).obj walking_cospan.right = Y := rfl @[simp] lemma span_right {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).obj walking_span.right = Z := rfl @[simp] lemma cospan_one {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).obj walking_cospan.one = Z := rfl @[simp] lemma span_zero {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).obj walking_span.zero = X := rfl @[simp] lemma cospan_map_inl {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).map walking_cospan.hom.inl = f := rfl @[simp] lemma span_map_fst {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).map walking_span.hom.fst = f := rfl @[simp] lemma cospan_map_inr {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).map walking_cospan.hom.inr = g := rfl @[simp] lemma span_map_snd {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).map walking_span.hom.snd = g := rfl lemma cospan_map_id {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) (w : walking_cospan) : (cospan f g).map (walking_cospan.hom.id w) = 𝟙 _ := rfl lemma span_map_id {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) (w : walking_span) : (span f g).map (walking_span.hom.id w) = 𝟙 _ := rfl /-- Every diagram indexing an pullback is naturally isomorphic (actually, equal) to a `cospan` -/ @[simps {rhs_md := semireducible}] def diagram_iso_cospan (F : walking_cospan ⥤ C) : F ≅ cospan (F.map inl) (F.map inr) := nat_iso.of_components (λ j, eq_to_iso (by tidy)) (by tidy) /-- Every diagram indexing a pushout is naturally isomorphic (actually, equal) to a `span` -/ @[simps {rhs_md := semireducible}] def diagram_iso_span (F : walking_span ⥤ C) : F ≅ span (F.map fst) (F.map snd) := nat_iso.of_components (λ j, eq_to_iso (by tidy)) (by tidy) variables {D : Type u₂} [category.{v₂} D] /-- A functor applied to a cospan is a cospan. -/ def cospan_comp_iso (F : C ⥤ D) {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : cospan f g ⋙ F ≅ cospan (F.map f) (F.map g) := nat_iso.of_components (by rintros (⟨⟩|⟨⟨⟩⟩); exact iso.refl _) (by rintros (⟨⟩|⟨⟨⟩⟩) (⟨⟩|⟨⟨⟩⟩) ⟨⟩; repeat { dsimp, simp, }) section variables (F : C ⥤ D) {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) @[simp] lemma cospan_comp_iso_app_left : (cospan_comp_iso F f g).app walking_cospan.left = iso.refl _ := rfl @[simp] lemma cospan_comp_iso_app_right : (cospan_comp_iso F f g).app walking_cospan.right = iso.refl _ := rfl @[simp] lemma cospan_comp_iso_app_one : (cospan_comp_iso F f g).app walking_cospan.one = iso.refl _ := rfl @[simp] lemma cospan_comp_iso_hom_app_left : (cospan_comp_iso F f g).hom.app walking_cospan.left = 𝟙 _ := rfl @[simp] lemma cospan_comp_iso_hom_app_right : (cospan_comp_iso F f g).hom.app walking_cospan.right = 𝟙 _ := rfl @[simp] lemma cospan_comp_iso_hom_app_one : (cospan_comp_iso F f g).hom.app walking_cospan.one = 𝟙 _ := rfl @[simp] lemma cospan_comp_iso_inv_app_left : (cospan_comp_iso F f g).inv.app walking_cospan.left = 𝟙 _ := rfl @[simp] lemma cospan_comp_iso_inv_app_right : (cospan_comp_iso F f g).inv.app walking_cospan.right = 𝟙 _ := rfl @[simp] lemma cospan_comp_iso_inv_app_one : (cospan_comp_iso F f g).inv.app walking_cospan.one = 𝟙 _ := rfl end /-- A functor applied to a span is a span. -/ def span_comp_iso (F : C ⥤ D) {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : span f g ⋙ F ≅ span (F.map f) (F.map g) := nat_iso.of_components (by rintros (⟨⟩|⟨⟨⟩⟩); exact iso.refl _) (by rintros (⟨⟩|⟨⟨⟩⟩) (⟨⟩|⟨⟨⟩⟩) ⟨⟩; repeat { dsimp, simp, }) section variables (F : C ⥤ D) {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) @[simp] lemma span_comp_iso_app_left : (span_comp_iso F f g).app walking_span.left = iso.refl _ := rfl @[simp] lemma span_comp_iso_app_right : (span_comp_iso F f g).app walking_span.right = iso.refl _ := rfl @[simp] lemma span_comp_iso_app_zero : (span_comp_iso F f g).app walking_span.zero = iso.refl _ := rfl @[simp] lemma span_comp_iso_hom_app_left : (span_comp_iso F f g).hom.app walking_span.left = 𝟙 _ := rfl @[simp] lemma span_comp_iso_hom_app_right : (span_comp_iso F f g).hom.app walking_span.right = 𝟙 _ := rfl @[simp] lemma span_comp_iso_hom_app_zero : (span_comp_iso F f g).hom.app walking_span.zero = 𝟙 _ := rfl @[simp] lemma span_comp_iso_inv_app_left : (span_comp_iso F f g).inv.app walking_span.left = 𝟙 _ := rfl @[simp] lemma span_comp_iso_inv_app_right : (span_comp_iso F f g).inv.app walking_span.right = 𝟙 _ := rfl @[simp] lemma span_comp_iso_inv_app_zero : (span_comp_iso F f g).inv.app walking_span.zero = 𝟙 _ := rfl end section variables {X Y Z X' Y' Z' : C} (iX : X ≅ X') (iY : Y ≅ Y') (iZ : Z ≅ Z') section variables {f : X ⟶ Z} {g : Y ⟶ Z} {f' : X' ⟶ Z'} {g' : Y' ⟶ Z'} /-- Construct an isomorphism of cospans from components. -/ def cospan_ext (wf : iX.hom ≫ f' = f ≫ iZ.hom) (wg : iY.hom ≫ g' = g ≫ iZ.hom) : cospan f g ≅ cospan f' g' := nat_iso.of_components (by { rintros (⟨⟩|⟨⟨⟩⟩), exacts [iZ, iX, iY], }) (by rintros (⟨⟩|⟨⟨⟩⟩) (⟨⟩|⟨⟨⟩⟩) ⟨⟩; repeat { dsimp, simp [wf, wg], }) variables (wf : iX.hom ≫ f' = f ≫ iZ.hom) (wg : iY.hom ≫ g' = g ≫ iZ.hom) @[simp] lemma cospan_ext_app_left : (cospan_ext iX iY iZ wf wg).app walking_cospan.left = iX := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_app_right : (cospan_ext iX iY iZ wf wg).app walking_cospan.right = iY := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_app_one : (cospan_ext iX iY iZ wf wg).app walking_cospan.one = iZ := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_hom_app_left : (cospan_ext iX iY iZ wf wg).hom.app walking_cospan.left = iX.hom := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_hom_app_right : (cospan_ext iX iY iZ wf wg).hom.app walking_cospan.right = iY.hom := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_hom_app_one : (cospan_ext iX iY iZ wf wg).hom.app walking_cospan.one = iZ.hom := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_inv_app_left : (cospan_ext iX iY iZ wf wg).inv.app walking_cospan.left = iX.inv := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_inv_app_right : (cospan_ext iX iY iZ wf wg).inv.app walking_cospan.right = iY.inv := by { dsimp [cospan_ext], simp, } @[simp] lemma cospan_ext_inv_app_one : (cospan_ext iX iY iZ wf wg).inv.app walking_cospan.one = iZ.inv := by { dsimp [cospan_ext], simp, } end section variables {f : X ⟶ Y} {g : X ⟶ Z} {f' : X' ⟶ Y'} {g' : X' ⟶ Z'} /-- Construct an isomorphism of spans from components. -/ def span_ext (wf : iX.hom ≫ f' = f ≫ iY.hom) (wg : iX.hom ≫ g' = g ≫ iZ.hom) : span f g ≅ span f' g' := nat_iso.of_components (by { rintros (⟨⟩|⟨⟨⟩⟩), exacts [iX, iY, iZ], }) (by rintros (⟨⟩|⟨⟨⟩⟩) (⟨⟩|⟨⟨⟩⟩) ⟨⟩; repeat { dsimp, simp [wf, wg], }) variables (wf : iX.hom ≫ f' = f ≫ iY.hom) (wg : iX.hom ≫ g' = g ≫ iZ.hom) @[simp] lemma span_ext_app_left : (span_ext iX iY iZ wf wg).app walking_span.left = iY := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_app_right : (span_ext iX iY iZ wf wg).app walking_span.right = iZ := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_app_one : (span_ext iX iY iZ wf wg).app walking_span.zero = iX := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_hom_app_left : (span_ext iX iY iZ wf wg).hom.app walking_span.left = iY.hom := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_hom_app_right : (span_ext iX iY iZ wf wg).hom.app walking_span.right = iZ.hom := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_hom_app_zero : (span_ext iX iY iZ wf wg).hom.app walking_span.zero = iX.hom := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_inv_app_left : (span_ext iX iY iZ wf wg).inv.app walking_span.left = iY.inv := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_inv_app_right : (span_ext iX iY iZ wf wg).inv.app walking_span.right = iZ.inv := by { dsimp [span_ext], simp, } @[simp] lemma span_ext_inv_app_zero : (span_ext iX iY iZ wf wg).inv.app walking_span.zero = iX.inv := by { dsimp [span_ext], simp, } end end variables {W X Y Z : C} /-- A pullback cone is just a cone on the cospan formed by two morphisms `f : X ⟶ Z` and `g : Y ⟶ Z`.-/ abbreviation pullback_cone (f : X ⟶ Z) (g : Y ⟶ Z) := cone (cospan f g) namespace pullback_cone variables {f : X ⟶ Z} {g : Y ⟶ Z} /-- The first projection of a pullback cone. -/ abbreviation fst (t : pullback_cone f g) : t.X ⟶ X := t.π.app walking_cospan.left /-- The second projection of a pullback cone. -/ abbreviation snd (t : pullback_cone f g) : t.X ⟶ Y := t.π.app walking_cospan.right @[simp] lemma condition_one (t : pullback_cone f g) : t.π.app walking_cospan.one = t.fst ≫ f := begin have w := t.π.naturality walking_cospan.hom.inl, dsimp at w, simpa using w, end /-- This is a slightly more convenient method to verify that a pullback cone is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def is_limit_aux (t : pullback_cone f g) (lift : Π (s : pullback_cone f g), s.X ⟶ t.X) (fac_left : ∀ (s : pullback_cone f g), lift s ≫ t.fst = s.fst) (fac_right : ∀ (s : pullback_cone f g), lift s ≫ t.snd = s.snd) (uniq : ∀ (s : pullback_cone f g) (m : s.X ⟶ t.X) (w : ∀ j : walking_cospan, m ≫ t.π.app j = s.π.app j), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, option.cases_on j (by { rw [← s.w inl, ← t.w inl, ←category.assoc], congr, exact fac_left s, } ) (λ j', walking_pair.cases_on j' (fac_left s) (fac_right s)), uniq' := uniq } /-- This is another convenient method to verify that a pullback cone is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def is_limit_aux' (t : pullback_cone f g) (create : Π (s : pullback_cone f g), {l // l ≫ t.fst = s.fst ∧ l ≫ t.snd = s.snd ∧ ∀ {m}, m ≫ t.fst = s.fst → m ≫ t.snd = s.snd → m = l}) : limits.is_limit t := pullback_cone.is_limit_aux t (λ s, (create s).1) (λ s, (create s).2.1) (λ s, (create s).2.2.1) (λ s m w, (create s).2.2.2 (w walking_cospan.left) (w walking_cospan.right)) /-- A pullback cone on `f` and `g` is determined by morphisms `fst : W ⟶ X` and `snd : W ⟶ Y` such that `fst ≫ f = snd ≫ g`. -/ @[simps] def mk {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : pullback_cone f g := { X := W, π := { app := λ j, option.cases_on j (fst ≫ f) (λ j', walking_pair.cases_on j' fst snd) } } @[simp] lemma mk_π_app_left {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : (mk fst snd eq).π.app walking_cospan.left = fst := rfl @[simp] lemma mk_π_app_right {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : (mk fst snd eq).π.app walking_cospan.right = snd := rfl @[simp] lemma mk_π_app_one {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : (mk fst snd eq).π.app walking_cospan.one = fst ≫ f := rfl @[simp] lemma mk_fst {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : (mk fst snd eq).fst = fst := rfl @[simp] lemma mk_snd {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : (mk fst snd eq).snd = snd := rfl @[reassoc] lemma condition (t : pullback_cone f g) : fst t ≫ f = snd t ≫ g := (t.w inl).trans (t.w inr).symm /-- To check whether a morphism is equalized by the maps of a pullback cone, it suffices to check it for `fst t` and `snd t` -/ lemma equalizer_ext (t : pullback_cone f g) {W : C} {k l : W ⟶ t.X} (h₀ : k ≫ fst t = l ≫ fst t) (h₁ : k ≫ snd t = l ≫ snd t) : ∀ (j : walking_cospan), k ≫ t.π.app j = l ≫ t.π.app j | (some walking_pair.left) := h₀ | (some walking_pair.right) := h₁ | none := by rw [← t.w inl, reassoc_of h₀] lemma is_limit.hom_ext {t : pullback_cone f g} (ht : is_limit t) {W : C} {k l : W ⟶ t.X} (h₀ : k ≫ fst t = l ≫ fst t) (h₁ : k ≫ snd t = l ≫ snd t) : k = l := ht.hom_ext $ equalizer_ext _ h₀ h₁ lemma mono_snd_of_is_pullback_of_mono {t : pullback_cone f g} (ht : is_limit t) [mono f] : mono t.snd := ⟨λ W h k i, is_limit.hom_ext ht (by simp [←cancel_mono f, t.condition, reassoc_of i]) i⟩ lemma mono_fst_of_is_pullback_of_mono {t : pullback_cone f g} (ht : is_limit t) [mono g] : mono t.fst := ⟨λ W h k i, is_limit.hom_ext ht i (by simp [←cancel_mono g, ←t.condition, reassoc_of i])⟩ /-- To construct an isomorphism of pullback cones, it suffices to construct an isomorphism of the cone points and check it commutes with `fst` and `snd`. -/ def ext {s t : pullback_cone f g} (i : s.X ≅ t.X) (w₁ : s.fst = i.hom ≫ t.fst) (w₂ : s.snd = i.hom ≫ t.snd) : s ≅ t := walking_cospan.ext i w₁ w₂ /-- If `t` is a limit pullback cone over `f` and `g` and `h : W ⟶ X` and `k : W ⟶ Y` are such that `h ≫ f = k ≫ g`, then we have `l : W ⟶ t.X` satisfying `l ≫ fst t = h` and `l ≫ snd t = k`. -/ def is_limit.lift' {t : pullback_cone f g} (ht : is_limit t) {W : C} (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : {l : W ⟶ t.X // l ≫ fst t = h ∧ l ≫ snd t = k} := ⟨ht.lift $ pullback_cone.mk _ _ w, ht.fac _ _, ht.fac _ _⟩ /-- This is a more convenient formulation to show that a `pullback_cone` constructed using `pullback_cone.mk` is a limit cone. -/ def is_limit.mk {W : C} {fst : W ⟶ X} {snd : W ⟶ Y} (eq : fst ≫ f = snd ≫ g) (lift : Π (s : pullback_cone f g), s.X ⟶ W) (fac_left : ∀ (s : pullback_cone f g), lift s ≫ fst = s.fst) (fac_right : ∀ (s : pullback_cone f g), lift s ≫ snd = s.snd) (uniq : ∀ (s : pullback_cone f g) (m : s.X ⟶ W) (w_fst : m ≫ fst = s.fst) (w_snd : m ≫ snd = s.snd), m = lift s) : is_limit (mk fst snd eq) := is_limit_aux _ lift fac_left fac_right (λ s m w, uniq s m (w walking_cospan.left) (w walking_cospan.right)) /-- The flip of a pullback square is a pullback square. -/ def flip_is_limit {W : C} {h : W ⟶ X} {k : W ⟶ Y} {comm : h ≫ f = k ≫ g} (t : is_limit (mk _ _ comm.symm)) : is_limit (mk _ _ comm) := is_limit_aux' _ $ λ s, begin refine ⟨(is_limit.lift' t _ _ s.condition.symm).1, (is_limit.lift' t _ _ _).2.2, (is_limit.lift' t _ _ _).2.1, λ m m₁ m₂, t.hom_ext _⟩, apply (mk k h _).equalizer_ext, { rwa (is_limit.lift' t _ _ _).2.1 }, { rwa (is_limit.lift' t _ _ _).2.2 }, end /-- The pullback cone `(𝟙 X, 𝟙 X)` for the pair `(f, f)` is a limit if `f` is a mono. The converse is shown in `mono_of_pullback_is_id`. -/ def is_limit_mk_id_id (f : X ⟶ Y) [mono f] : is_limit (mk (𝟙 X) (𝟙 X) rfl : pullback_cone f f) := is_limit.mk _ (λ s, s.fst) (λ s, category.comp_id _) (λ s, by rw [←cancel_mono f, category.comp_id, s.condition]) (λ s m m₁ m₂, by simpa using m₁) /-- `f` is a mono if the pullback cone `(𝟙 X, 𝟙 X)` is a limit for the pair `(f, f)`. The converse is given in `pullback_cone.is_id_of_mono`. -/ lemma mono_of_is_limit_mk_id_id (f : X ⟶ Y) (t : is_limit (mk (𝟙 X) (𝟙 X) rfl : pullback_cone f f)) : mono f := ⟨λ Z g h eq, by { rcases pullback_cone.is_limit.lift' t _ _ eq with ⟨_, rfl, rfl⟩, refl } ⟩ /-- Suppose `f` and `g` are two morphisms with a common codomain and `s` is a limit cone over the diagram formed by `f` and `g`. Suppose `f` and `g` both factor through a monomorphism `h` via `x` and `y`, respectively. Then `s` is also a limit cone over the diagram formed by `x` and `y`. -/ def is_limit_of_factors (f : X ⟶ Z) (g : Y ⟶ Z) (h : W ⟶ Z) [mono h] (x : X ⟶ W) (y : Y ⟶ W) (hxh : x ≫ h = f) (hyh : y ≫ h = g) (s : pullback_cone f g) (hs : is_limit s) : is_limit (pullback_cone.mk _ _ (show s.fst ≫ x = s.snd ≫ y, from (cancel_mono h).1 $ by simp only [category.assoc, hxh, hyh, s.condition])) := pullback_cone.is_limit_aux' _ $ λ t, ⟨hs.lift (pullback_cone.mk t.fst t.snd $ by rw [←hxh, ←hyh, reassoc_of t.condition]), ⟨hs.fac _ walking_cospan.left, hs.fac _ walking_cospan.right, λ r hr hr', begin apply pullback_cone.is_limit.hom_ext hs; simp only [pullback_cone.mk_fst, pullback_cone.mk_snd] at ⊢ hr hr'; simp only [hr, hr']; symmetry, exacts [hs.fac _ walking_cospan.left, hs.fac _ walking_cospan.right] end⟩⟩ /-- If `W` is the pullback of `f, g`, it is also the pullback of `f ≫ i, g ≫ i` for any mono `i`. -/ def is_limit_of_comp_mono (f : X ⟶ W) (g : Y ⟶ W) (i : W ⟶ Z) [mono i] (s : pullback_cone f g) (H : is_limit s) : is_limit (pullback_cone.mk _ _ (show s.fst ≫ f ≫ i = s.snd ≫ g ≫ i, by rw [← category.assoc, ← category.assoc, s.condition])) := begin apply pullback_cone.is_limit_aux', intro s, rcases pullback_cone.is_limit.lift' H s.fst s.snd ((cancel_mono i).mp (by simpa using s.condition)) with ⟨l, h₁, h₂⟩, refine ⟨l,h₁,h₂,_⟩, intros m hm₁ hm₂, exact (pullback_cone.is_limit.hom_ext H (hm₁.trans h₁.symm) (hm₂.trans h₂.symm) : _) end end pullback_cone /-- A pushout cocone is just a cocone on the span formed by two morphisms `f : X ⟶ Y` and `g : X ⟶ Z`.-/ abbreviation pushout_cocone (f : X ⟶ Y) (g : X ⟶ Z) := cocone (span f g) namespace pushout_cocone variables {f : X ⟶ Y} {g : X ⟶ Z} /-- The first inclusion of a pushout cocone. -/ abbreviation inl (t : pushout_cocone f g) : Y ⟶ t.X := t.ι.app walking_span.left /-- The second inclusion of a pushout cocone. -/ abbreviation inr (t : pushout_cocone f g) : Z ⟶ t.X := t.ι.app walking_span.right @[simp] lemma condition_zero (t : pushout_cocone f g) : t.ι.app walking_span.zero = f ≫ t.inl := begin have w := t.ι.naturality walking_span.hom.fst, dsimp at w, simpa using w.symm, end /-- This is a slightly more convenient method to verify that a pushout cocone is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def is_colimit_aux (t : pushout_cocone f g) (desc : Π (s : pushout_cocone f g), t.X ⟶ s.X) (fac_left : ∀ (s : pushout_cocone f g), t.inl ≫ desc s = s.inl) (fac_right : ∀ (s : pushout_cocone f g), t.inr ≫ desc s = s.inr) (uniq : ∀ (s : pushout_cocone f g) (m : t.X ⟶ s.X) (w : ∀ j : walking_span, t.ι.app j ≫ m = s.ι.app j), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, option.cases_on j (by { simp [← s.w fst, ← t.w fst, fac_left s] } ) (λ j', walking_pair.cases_on j' (fac_left s) (fac_right s)), uniq' := uniq } /-- This is another convenient method to verify that a pushout cocone is a colimit cocone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def is_colimit_aux' (t : pushout_cocone f g) (create : Π (s : pushout_cocone f g), {l // t.inl ≫ l = s.inl ∧ t.inr ≫ l = s.inr ∧ ∀ {m}, t.inl ≫ m = s.inl → t.inr ≫ m = s.inr → m = l}) : is_colimit t := is_colimit_aux t (λ s, (create s).1) (λ s, (create s).2.1) (λ s, (create s).2.2.1) (λ s m w, (create s).2.2.2 (w walking_cospan.left) (w walking_cospan.right)) /-- A pushout cocone on `f` and `g` is determined by morphisms `inl : Y ⟶ W` and `inr : Z ⟶ W` such that `f ≫ inl = g ↠ inr`. -/ @[simps] def mk {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : pushout_cocone f g := { X := W, ι := { app := λ j, option.cases_on j (f ≫ inl) (λ j', walking_pair.cases_on j' inl inr) } } @[simp] lemma mk_ι_app_left {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : (mk inl inr eq).ι.app walking_span.left = inl := rfl @[simp] lemma mk_ι_app_right {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : (mk inl inr eq).ι.app walking_span.right = inr := rfl @[simp] lemma mk_ι_app_zero {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : (mk inl inr eq).ι.app walking_span.zero = f ≫ inl := rfl @[simp] lemma mk_inl {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : (mk inl inr eq).inl = inl := rfl @[simp] lemma mk_inr {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : (mk inl inr eq).inr = inr := rfl @[reassoc] lemma condition (t : pushout_cocone f g) : f ≫ (inl t) = g ≫ (inr t) := (t.w fst).trans (t.w snd).symm /-- To check whether a morphism is coequalized by the maps of a pushout cocone, it suffices to check it for `inl t` and `inr t` -/ lemma coequalizer_ext (t : pushout_cocone f g) {W : C} {k l : t.X ⟶ W} (h₀ : inl t ≫ k = inl t ≫ l) (h₁ : inr t ≫ k = inr t ≫ l) : ∀ (j : walking_span), t.ι.app j ≫ k = t.ι.app j ≫ l | (some walking_pair.left) := h₀ | (some walking_pair.right) := h₁ | none := by rw [← t.w fst, category.assoc, category.assoc, h₀] lemma is_colimit.hom_ext {t : pushout_cocone f g} (ht : is_colimit t) {W : C} {k l : t.X ⟶ W} (h₀ : inl t ≫ k = inl t ≫ l) (h₁ : inr t ≫ k = inr t ≫ l) : k = l := ht.hom_ext $ coequalizer_ext _ h₀ h₁ /-- If `t` is a colimit pushout cocone over `f` and `g` and `h : Y ⟶ W` and `k : Z ⟶ W` are morphisms satisfying `f ≫ h = g ≫ k`, then we have a factorization `l : t.X ⟶ W` such that `inl t ≫ l = h` and `inr t ≫ l = k`. -/ def is_colimit.desc' {t : pushout_cocone f g} (ht : is_colimit t) {W : C} (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : {l : t.X ⟶ W // inl t ≫ l = h ∧ inr t ≫ l = k } := ⟨ht.desc $ pushout_cocone.mk _ _ w, ht.fac _ _, ht.fac _ _⟩ lemma epi_inr_of_is_pushout_of_epi {t : pushout_cocone f g} (ht : is_colimit t) [epi f] : epi t.inr := ⟨λ W h k i, is_colimit.hom_ext ht (by simp [←cancel_epi f, t.condition_assoc, i]) i⟩ lemma epi_inl_of_is_pushout_of_epi {t : pushout_cocone f g} (ht : is_colimit t) [epi g] : epi t.inl := ⟨λ W h k i, is_colimit.hom_ext ht i (by simp [←cancel_epi g, ←t.condition_assoc, i])⟩ /-- To construct an isomorphism of pushout cocones, it suffices to construct an isomorphism of the cocone points and check it commutes with `inl` and `inr`. -/ def ext {s t : pushout_cocone f g} (i : s.X ≅ t.X) (w₁ : s.inl ≫ i.hom = t.inl) (w₂ : s.inr ≫ i.hom = t.inr) : s ≅ t := walking_span.ext i w₁ w₂ /-- This is a more convenient formulation to show that a `pushout_cocone` constructed using `pushout_cocone.mk` is a colimit cocone. -/ def is_colimit.mk {W : C} {inl : Y ⟶ W} {inr : Z ⟶ W} (eq : f ≫ inl = g ≫ inr) (desc : Π (s : pushout_cocone f g), W ⟶ s.X) (fac_left : ∀ (s : pushout_cocone f g), inl ≫ desc s = s.inl) (fac_right : ∀ (s : pushout_cocone f g), inr ≫ desc s = s.inr) (uniq : ∀ (s : pushout_cocone f g) (m : W ⟶ s.X) (w_inl : inl ≫ m = s.inl) (w_inr : inr ≫ m = s.inr), m = desc s) : is_colimit (mk inl inr eq) := is_colimit_aux _ desc fac_left fac_right (λ s m w, uniq s m (w walking_cospan.left) (w walking_cospan.right)) /-- The flip of a pushout square is a pushout square. -/ def flip_is_colimit {W : C} {h : Y ⟶ W} {k : Z ⟶ W} {comm : f ≫ h = g ≫ k} (t : is_colimit (mk _ _ comm.symm)) : is_colimit (mk _ _ comm) := is_colimit_aux' _ $ λ s, begin refine ⟨(is_colimit.desc' t _ _ s.condition.symm).1, (is_colimit.desc' t _ _ _).2.2, (is_colimit.desc' t _ _ _).2.1, λ m m₁ m₂, t.hom_ext _⟩, apply (mk k h _).coequalizer_ext, { rwa (is_colimit.desc' t _ _ _).2.1 }, { rwa (is_colimit.desc' t _ _ _).2.2 }, end /-- The pushout cocone `(𝟙 X, 𝟙 X)` for the pair `(f, f)` is a colimit if `f` is an epi. The converse is shown in `epi_of_is_colimit_mk_id_id`. -/ def is_colimit_mk_id_id (f : X ⟶ Y) [epi f] : is_colimit (mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) := is_colimit.mk _ (λ s, s.inl) (λ s, category.id_comp _) (λ s, by rw [←cancel_epi f, category.id_comp, s.condition]) (λ s m m₁ m₂, by simpa using m₁) /-- `f` is an epi if the pushout cocone `(𝟙 X, 𝟙 X)` is a colimit for the pair `(f, f)`. The converse is given in `pushout_cocone.is_colimit_mk_id_id`. -/ lemma epi_of_is_colimit_mk_id_id (f : X ⟶ Y) (t : is_colimit (mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f := ⟨λ Z g h eq, by { rcases pushout_cocone.is_colimit.desc' t _ _ eq with ⟨_, rfl, rfl⟩, refl }⟩ /-- Suppose `f` and `g` are two morphisms with a common domain and `s` is a colimit cocone over the diagram formed by `f` and `g`. Suppose `f` and `g` both factor through an epimorphism `h` via `x` and `y`, respectively. Then `s` is also a colimit cocone over the diagram formed by `x` and `y`. -/ def is_colimit_of_factors (f : X ⟶ Y) (g : X ⟶ Z) (h : X ⟶ W) [epi h] (x : W ⟶ Y) (y : W ⟶ Z) (hhx : h ≫ x = f) (hhy : h ≫ y = g) (s : pushout_cocone f g) (hs : is_colimit s) : is_colimit (pushout_cocone.mk _ _ (show x ≫ s.inl = y ≫ s.inr, from (cancel_epi h).1 $ by rw [reassoc_of hhx, reassoc_of hhy, s.condition])) := pushout_cocone.is_colimit_aux' _ $ λ t, ⟨hs.desc (pushout_cocone.mk t.inl t.inr $ by rw [←hhx, ←hhy, category.assoc, category.assoc, t.condition]), ⟨hs.fac _ walking_span.left, hs.fac _ walking_span.right, λ r hr hr', begin apply pushout_cocone.is_colimit.hom_ext hs; simp only [pushout_cocone.mk_inl, pushout_cocone.mk_inr] at ⊢ hr hr'; simp only [hr, hr']; symmetry, exacts [hs.fac _ walking_span.left, hs.fac _ walking_span.right] end⟩⟩ /-- If `W` is the pushout of `f, g`, it is also the pushout of `h ≫ f, h ≫ g` for any epi `h`. -/ def is_colimit_of_epi_comp (f : X ⟶ Y) (g : X ⟶ Z) (h : W ⟶ X) [epi h] (s : pushout_cocone f g) (H : is_colimit s) : is_colimit (pushout_cocone.mk _ _ (show (h ≫ f) ≫ s.inl = (h ≫ g) ≫ s.inr, by rw [category.assoc, category.assoc, s.condition])) := begin apply pushout_cocone.is_colimit_aux', intro s, rcases pushout_cocone.is_colimit.desc' H s.inl s.inr ((cancel_epi h).mp (by simpa using s.condition)) with ⟨l, h₁, h₂⟩, refine ⟨l,h₁,h₂,_⟩, intros m hm₁ hm₂, exact (pushout_cocone.is_colimit.hom_ext H (hm₁.trans h₁.symm) (hm₂.trans h₂.symm) : _) end end pushout_cocone /-- This is a helper construction that can be useful when verifying that a category has all pullbacks. Given `F : walking_cospan ⥤ C`, which is really the same as `cospan (F.map inl) (F.map inr)`, and a pullback cone on `F.map inl` and `F.map inr`, we get a cone on `F`. If you're thinking about using this, have a look at `has_pullbacks_of_has_limit_cospan`, which you may find to be an easier way of achieving your goal. -/ @[simps] def cone.of_pullback_cone {F : walking_cospan ⥤ C} (t : pullback_cone (F.map inl) (F.map inr)) : cone F := { X := t.X, π := t.π ≫ (diagram_iso_cospan F).inv } /-- This is a helper construction that can be useful when verifying that a category has all pushout. Given `F : walking_span ⥤ C`, which is really the same as `span (F.map fst) (F.mal snd)`, and a pushout cocone on `F.map fst` and `F.map snd`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_pushouts_of_has_colimit_span`, which you may find to be an easiery way of achieving your goal. -/ @[simps] def cocone.of_pushout_cocone {F : walking_span ⥤ C} (t : pushout_cocone (F.map fst) (F.map snd)) : cocone F := { X := t.X, ι := (diagram_iso_span F).hom ≫ t.ι } /-- Given `F : walking_cospan ⥤ C`, which is really the same as `cospan (F.map inl) (F.map inr)`, and a cone on `F`, we get a pullback cone on `F.map inl` and `F.map inr`. -/ @[simps] def pullback_cone.of_cone {F : walking_cospan ⥤ C} (t : cone F) : pullback_cone (F.map inl) (F.map inr) := { X := t.X, π := t.π ≫ (diagram_iso_cospan F).hom } /-- A diagram `walking_cospan ⥤ C` is isomorphic to some `pullback_cone.mk` after composing with `diagram_iso_cospan`. -/ @[simps] def pullback_cone.iso_mk {F : walking_cospan ⥤ C} (t : cone F) : (cones.postcompose (diagram_iso_cospan.{v} _).hom).obj t ≅ pullback_cone.mk (t.π.app walking_cospan.left) (t.π.app walking_cospan.right) ((t.π.naturality inl).symm.trans (t.π.naturality inr : _)) := cones.ext (iso.refl _) $ by rintro (_|(_|_)); { dsimp, simp } /-- Given `F : walking_span ⥤ C`, which is really the same as `span (F.map fst) (F.map snd)`, and a cocone on `F`, we get a pushout cocone on `F.map fst` and `F.map snd`. -/ @[simps] def pushout_cocone.of_cocone {F : walking_span ⥤ C} (t : cocone F) : pushout_cocone (F.map fst) (F.map snd) := { X := t.X, ι := (diagram_iso_span F).inv ≫ t.ι } /-- A diagram `walking_span ⥤ C` is isomorphic to some `pushout_cocone.mk` after composing with `diagram_iso_span`. -/ @[simps] def pushout_cocone.iso_mk {F : walking_span ⥤ C} (t : cocone F) : (cocones.precompose (diagram_iso_span.{v} _).inv).obj t ≅ pushout_cocone.mk (t.ι.app walking_span.left) (t.ι.app walking_span.right) ((t.ι.naturality fst).trans (t.ι.naturality snd).symm) := cocones.ext (iso.refl _) $ by rintro (_|(_|_)); { dsimp, simp } /-- `has_pullback f g` represents a particular choice of limiting cone for the pair of morphisms `f : X ⟶ Z` and `g : Y ⟶ Z`. -/ abbreviation has_pullback {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) := has_limit (cospan f g) /-- `has_pushout f g` represents a particular choice of colimiting cocone for the pair of morphisms `f : X ⟶ Y` and `g : X ⟶ Z`. -/ abbreviation has_pushout {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) := has_colimit (span f g) /-- `pullback f g` computes the pullback of a pair of morphisms with the same target. -/ abbreviation pullback {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] := limit (cospan f g) /-- `pushout f g` computes the pushout of a pair of morphisms with the same source. -/ abbreviation pushout {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] := colimit (span f g) /-- The first projection of the pullback of `f` and `g`. -/ abbreviation pullback.fst {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : pullback f g ⟶ X := limit.π (cospan f g) walking_cospan.left /-- The second projection of the pullback of `f` and `g`. -/ abbreviation pullback.snd {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : pullback f g ⟶ Y := limit.π (cospan f g) walking_cospan.right /-- The first inclusion into the pushout of `f` and `g`. -/ abbreviation pushout.inl {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] : Y ⟶ pushout f g := colimit.ι (span f g) walking_span.left /-- The second inclusion into the pushout of `f` and `g`. -/ abbreviation pushout.inr {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] : Z ⟶ pushout f g := colimit.ι (span f g) walking_span.right /-- A pair of morphisms `h : W ⟶ X` and `k : W ⟶ Y` satisfying `h ≫ f = k ≫ g` induces a morphism `pullback.lift : W ⟶ pullback f g`. -/ abbreviation pullback.lift {W X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : W ⟶ pullback f g := limit.lift _ (pullback_cone.mk h k w) /-- A pair of morphisms `h : Y ⟶ W` and `k : Z ⟶ W` satisfying `f ≫ h = g ≫ k` induces a morphism `pushout.desc : pushout f g ⟶ W`. -/ abbreviation pushout.desc {W X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : pushout f g ⟶ W := colimit.desc _ (pushout_cocone.mk h k w) @[simp, reassoc] lemma pullback.lift_fst {W X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : pullback.lift h k w ≫ pullback.fst = h := limit.lift_π _ _ @[simp, reassoc] lemma pullback.lift_snd {W X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : pullback.lift h k w ≫ pullback.snd = k := limit.lift_π _ _ @[simp, reassoc] lemma pushout.inl_desc {W X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : pushout.inl ≫ pushout.desc h k w = h := colimit.ι_desc _ _ @[simp, reassoc] lemma pushout.inr_desc {W X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : pushout.inr ≫ pushout.desc h k w = k := colimit.ι_desc _ _ /-- A pair of morphisms `h : W ⟶ X` and `k : W ⟶ Y` satisfying `h ≫ f = k ≫ g` induces a morphism `l : W ⟶ pullback f g` such that `l ≫ pullback.fst = h` and `l ≫ pullback.snd = k`. -/ def pullback.lift' {W X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : {l : W ⟶ pullback f g // l ≫ pullback.fst = h ∧ l ≫ pullback.snd = k} := ⟨pullback.lift h k w, pullback.lift_fst _ _ _, pullback.lift_snd _ _ _⟩ /-- A pair of morphisms `h : Y ⟶ W` and `k : Z ⟶ W` satisfying `f ≫ h = g ≫ k` induces a morphism `l : pushout f g ⟶ W` such that `pushout.inl ≫ l = h` and `pushout.inr ≫ l = k`. -/ def pullback.desc' {W X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : {l : pushout f g ⟶ W // pushout.inl ≫ l = h ∧ pushout.inr ≫ l = k} := ⟨pushout.desc h k w, pushout.inl_desc _ _ _, pushout.inr_desc _ _ _⟩ @[reassoc] lemma pullback.condition {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : (pullback.fst : pullback f g ⟶ X) ≫ f = pullback.snd ≫ g := pullback_cone.condition _ @[reassoc] lemma pushout.condition {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] : f ≫ (pushout.inl : Y ⟶ pushout f g) = g ≫ pushout.inr := pushout_cocone.condition _ /-- Given such a diagram, then there is a natural morphism `W ×ₛ X ⟶ Y ×ₜ Z`. W ⟶ Y ↘ ↘ S ⟶ T ↗ ↗ X ⟶ Z -/ abbreviation pullback.map {W X Y Z S T : C} (f₁ : W ⟶ S) (f₂ : X ⟶ S) [has_pullback f₁ f₂] (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) [has_pullback g₁ g₂] (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) (eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) : pullback f₁ f₂ ⟶ pullback g₁ g₂ := pullback.lift (pullback.fst ≫ i₁) (pullback.snd ≫ i₂) (by simp [← eq₁, ← eq₂, pullback.condition_assoc]) /-- Given such a diagram, then there is a natural morphism `W ⨿ₛ X ⟶ Y ⨿ₜ Z`. W ⟶ Y ↗ ↗ S ⟶ T ↘ ↘ X ⟶ Z -/ abbreviation pushout.map {W X Y Z S T : C} (f₁ : S ⟶ W) (f₂ : S ⟶ X) [has_pushout f₁ f₂] (g₁ : T ⟶ Y) (g₂ : T ⟶ Z) [has_pushout g₁ g₂] (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) (eq₁ : f₁ ≫ i₁ = i₃ ≫ g₁) (eq₂ : f₂ ≫ i₂ = i₃ ≫ g₂) : pushout f₁ f₂ ⟶ pushout g₁ g₂ := pushout.desc (i₁ ≫ pushout.inl) (i₂ ≫ pushout.inr) (by { simp only [← category.assoc, eq₁, eq₂], simp [pushout.condition] }) /-- Two morphisms into a pullback are equal if their compositions with the pullback morphisms are equal -/ @[ext] lemma pullback.hom_ext {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] {W : C} {k l : W ⟶ pullback f g} (h₀ : k ≫ pullback.fst = l ≫ pullback.fst) (h₁ : k ≫ pullback.snd = l ≫ pullback.snd) : k = l := limit.hom_ext $ pullback_cone.equalizer_ext _ h₀ h₁ /-- The pullback cone built from the pullback projections is a pullback. -/ def pullback_is_pullback {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] : is_limit (pullback_cone.mk (pullback.fst : pullback f g ⟶ _) pullback.snd pullback.condition) := pullback_cone.is_limit.mk _ (λ s, pullback.lift s.fst s.snd s.condition) (by simp) (by simp) (by tidy) /-- The pullback of a monomorphism is a monomorphism -/ instance pullback.fst_of_mono {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] [mono g] : mono (pullback.fst : pullback f g ⟶ X) := pullback_cone.mono_fst_of_is_pullback_of_mono (limit.is_limit _) /-- The pullback of a monomorphism is a monomorphism -/ instance pullback.snd_of_mono {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] [mono f] : mono (pullback.snd : pullback f g ⟶ Y) := pullback_cone.mono_snd_of_is_pullback_of_mono (limit.is_limit _) /-- The map `X ×[Z] Y ⟶ X × Y` is mono. -/ instance mono_pullback_to_prod {C : Type*} [category C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_binary_product X Y] : mono (prod.lift pullback.fst pullback.snd : pullback f g ⟶ _) := ⟨λ W i₁ i₂ h, begin ext, { simpa using congr_arg (λ f, f ≫ prod.fst) h }, { simpa using congr_arg (λ f, f ≫ prod.snd) h } end⟩ /-- Two morphisms out of a pushout are equal if their compositions with the pushout morphisms are equal -/ @[ext] lemma pushout.hom_ext {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] {W : C} {k l : pushout f g ⟶ W} (h₀ : pushout.inl ≫ k = pushout.inl ≫ l) (h₁ : pushout.inr ≫ k = pushout.inr ≫ l) : k = l := colimit.hom_ext $ pushout_cocone.coequalizer_ext _ h₀ h₁ /-- The pushout cocone built from the pushout coprojections is a pushout. -/ def pushout_is_pushout {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] : is_colimit (pushout_cocone.mk (pushout.inl : _ ⟶ pushout f g) pushout.inr pushout.condition) := pushout_cocone.is_colimit.mk _ (λ s, pushout.desc s.inl s.inr s.condition) (by simp) (by simp) (by tidy) /-- The pushout of an epimorphism is an epimorphism -/ instance pushout.inl_of_epi {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] [epi g] : epi (pushout.inl : Y ⟶ pushout f g) := pushout_cocone.epi_inl_of_is_pushout_of_epi (colimit.is_colimit _) /-- The pushout of an epimorphism is an epimorphism -/ instance pushout.inr_of_epi {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] [epi f] : epi (pushout.inr : Z ⟶ pushout f g) := pushout_cocone.epi_inr_of_is_pushout_of_epi (colimit.is_colimit _) /-- The map ` X ⨿ Y ⟶ X ⨿[Z] Y` is epi. -/ instance epi_coprod_to_pushout {C : Type*} [category C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] [has_binary_coproduct Y Z] : epi (coprod.desc pushout.inl pushout.inr : _ ⟶ pushout f g) := ⟨λ W i₁ i₂ h, begin ext, { simpa using congr_arg (λ f, coprod.inl ≫ f) h }, { simpa using congr_arg (λ f, coprod.inr ≫ f) h } end⟩ instance pullback.map_is_iso {W X Y Z S T : C} (f₁ : W ⟶ S) (f₂ : X ⟶ S) [has_pullback f₁ f₂] (g₁ : Y ⟶ T) (g₂ : Z ⟶ T) [has_pullback g₁ g₂] (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) (eq₁ : f₁ ≫ i₃ = i₁ ≫ g₁) (eq₂ : f₂ ≫ i₃ = i₂ ≫ g₂) [is_iso i₁] [is_iso i₂] [is_iso i₃] : is_iso (pullback.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) := begin refine ⟨⟨pullback.map _ _ _ _ (inv i₁) (inv i₂) (inv i₃) _ _, _, _⟩⟩, { rw [is_iso.comp_inv_eq, category.assoc, eq₁, is_iso.inv_hom_id_assoc] }, { rw [is_iso.comp_inv_eq, category.assoc, eq₂, is_iso.inv_hom_id_assoc] }, tidy end /-- If `f₁ = f₂` and `g₁ = g₂`, we may construct a canonical isomorphism `pullback f₁ g₁ ≅ pullback f₂ g₂` -/ @[simps hom] def pullback.congr_hom {X Y Z : C} {f₁ f₂ : X ⟶ Z} {g₁ g₂ : Y ⟶ Z} (h₁ : f₁ = f₂) (h₂ : g₁ = g₂) [has_pullback f₁ g₁] [has_pullback f₂ g₂] : pullback f₁ g₁ ≅ pullback f₂ g₂ := as_iso $ pullback.map _ _ _ _ (𝟙 _) (𝟙 _) (𝟙 _) (by simp [h₁]) (by simp [h₂]) @[simp] lemma pullback.congr_hom_inv {X Y Z : C} {f₁ f₂ : X ⟶ Z} {g₁ g₂ : Y ⟶ Z} (h₁ : f₁ = f₂) (h₂ : g₁ = g₂) [has_pullback f₁ g₁] [has_pullback f₂ g₂] : (pullback.congr_hom h₁ h₂).inv = pullback.map _ _ _ _ (𝟙 _) (𝟙 _) (𝟙 _) (by simp [h₁]) (by simp [h₂]) := begin apply pullback.hom_ext, { erw pullback.lift_fst, rw iso.inv_comp_eq, erw pullback.lift_fst_assoc, rw [category.comp_id, category.comp_id] }, { erw pullback.lift_snd, rw iso.inv_comp_eq, erw pullback.lift_snd_assoc, rw [category.comp_id, category.comp_id] }, end instance pushout.map_is_iso {W X Y Z S T : C} (f₁ : S ⟶ W) (f₂ : S ⟶ X) [has_pushout f₁ f₂] (g₁ : T ⟶ Y) (g₂ : T ⟶ Z) [has_pushout g₁ g₂] (i₁ : W ⟶ Y) (i₂ : X ⟶ Z) (i₃ : S ⟶ T) (eq₁ : f₁ ≫ i₁ = i₃ ≫ g₁) (eq₂ : f₂ ≫ i₂ = i₃ ≫ g₂) [is_iso i₁] [is_iso i₂] [is_iso i₃] : is_iso (pushout.map f₁ f₂ g₁ g₂ i₁ i₂ i₃ eq₁ eq₂) := begin refine ⟨⟨pushout.map _ _ _ _ (inv i₁) (inv i₂) (inv i₃) _ _, _, _⟩⟩, { rw [is_iso.comp_inv_eq, category.assoc, eq₁, is_iso.inv_hom_id_assoc] }, { rw [is_iso.comp_inv_eq, category.assoc, eq₂, is_iso.inv_hom_id_assoc] }, tidy end /-- If `f₁ = f₂` and `g₁ = g₂`, we may construct a canonical isomorphism `pushout f₁ g₁ ≅ pullback f₂ g₂` -/ @[simps hom] def pushout.congr_hom {X Y Z : C} {f₁ f₂ : X ⟶ Y} {g₁ g₂ : X ⟶ Z} (h₁ : f₁ = f₂) (h₂ : g₁ = g₂) [has_pushout f₁ g₁] [has_pushout f₂ g₂] : pushout f₁ g₁ ≅ pushout f₂ g₂ := as_iso $ pushout.map _ _ _ _ (𝟙 _) (𝟙 _) (𝟙 _) (by simp [h₁]) (by simp [h₂]) @[simp] lemma pushout.congr_hom_inv {X Y Z : C} {f₁ f₂ : X ⟶ Y} {g₁ g₂ : X ⟶ Z} (h₁ : f₁ = f₂) (h₂ : g₁ = g₂) [has_pushout f₁ g₁] [has_pushout f₂ g₂] : (pushout.congr_hom h₁ h₂).inv = pushout.map _ _ _ _ (𝟙 _) (𝟙 _) (𝟙 _) (by simp [h₁]) (by simp [h₂]) := begin apply pushout.hom_ext, { erw pushout.inl_desc, rw [iso.comp_inv_eq, category.id_comp], erw pushout.inl_desc, rw category.id_comp }, { erw pushout.inr_desc, rw [iso.comp_inv_eq, category.id_comp], erw pushout.inr_desc, rw category.id_comp } end section variables (G : C ⥤ D) /-- The comparison morphism for the pullback of `f,g`. This is an isomorphism iff `G` preserves the pullback of `f,g`; see `category_theory/limits/preserves/shapes/pullbacks.lean` -/ def pullback_comparison (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (G.map f) (G.map g)] : G.obj (pullback f g) ⟶ pullback (G.map f) (G.map g) := pullback.lift (G.map pullback.fst) (G.map pullback.snd) (by simp only [←G.map_comp, pullback.condition]) @[simp, reassoc] lemma pullback_comparison_comp_fst (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (G.map f) (G.map g)] : pullback_comparison G f g ≫ pullback.fst = G.map pullback.fst := pullback.lift_fst _ _ _ @[simp, reassoc] lemma pullback_comparison_comp_snd (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (G.map f) (G.map g)] : pullback_comparison G f g ≫ pullback.snd = G.map pullback.snd := pullback.lift_snd _ _ _ @[simp, reassoc] lemma map_lift_pullback_comparison (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (G.map f) (G.map g)] {W : C} {h : W ⟶ X} {k : W ⟶ Y} (w : h ≫ f = k ≫ g) : G.map (pullback.lift _ _ w) ≫ pullback_comparison G f g = pullback.lift (G.map h) (G.map k) (by simp only [←G.map_comp, w]) := by { ext; simp [← G.map_comp] } /-- The comparison morphism for the pushout of `f,g`. This is an isomorphism iff `G` preserves the pushout of `f,g`; see `category_theory/limits/preserves/shapes/pullbacks.lean` -/ def pushout_comparison (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] [has_pushout (G.map f) (G.map g)] : pushout (G.map f) (G.map g) ⟶ G.obj (pushout f g) := pushout.desc (G.map pushout.inl) (G.map pushout.inr) (by simp only [←G.map_comp, pushout.condition]) @[simp, reassoc] lemma inl_comp_pushout_comparison (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] [has_pushout (G.map f) (G.map g)] : pushout.inl ≫ pushout_comparison G f g = G.map pushout.inl := pushout.inl_desc _ _ _ @[simp, reassoc] lemma inr_comp_pushout_comparison (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] [has_pushout (G.map f) (G.map g)] : pushout.inr ≫ pushout_comparison G f g = G.map pushout.inr := pushout.inr_desc _ _ _ @[simp, reassoc] lemma pushout_comparison_map_desc (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] [has_pushout (G.map f) (G.map g)] {W : C} {h : Y ⟶ W} {k : Z ⟶ W} (w : f ≫ h = g ≫ k) : pushout_comparison G f g ≫ G.map (pushout.desc _ _ w) = pushout.desc (G.map h) (G.map k) (by simp only [←G.map_comp, w]) := by { ext; simp [← G.map_comp] } end section pullback_symmetry open walking_cospan variables (f : X ⟶ Z) (g : Y ⟶ Z) /-- Making this a global instance would make the typeclass seach go in an infinite loop. -/ lemma has_pullback_symmetry [has_pullback f g] : has_pullback g f := ⟨⟨⟨pullback_cone.mk _ _ pullback.condition.symm, pullback_cone.flip_is_limit (pullback_is_pullback _ _)⟩⟩⟩ local attribute [instance] has_pullback_symmetry /-- The isomorphism `X ×[Z] Y ≅ Y ×[Z] X`. -/ def pullback_symmetry [has_pullback f g] : pullback f g ≅ pullback g f := is_limit.cone_point_unique_up_to_iso (pullback_cone.flip_is_limit (pullback_is_pullback f g) : is_limit (pullback_cone.mk _ _ pullback.condition.symm)) (limit.is_limit _) @[simp, reassoc] lemma pullback_symmetry_hom_comp_fst [has_pullback f g] : (pullback_symmetry f g).hom ≫ pullback.fst = pullback.snd := by simp [pullback_symmetry] @[simp, reassoc] lemma pullback_symmetry_hom_comp_snd [has_pullback f g] : (pullback_symmetry f g).hom ≫ pullback.snd = pullback.fst := by simp [pullback_symmetry] @[simp, reassoc] lemma pullback_symmetry_inv_comp_fst [has_pullback f g] : (pullback_symmetry f g).inv ≫ pullback.fst = pullback.snd := by simp [iso.inv_comp_eq] @[simp, reassoc] lemma pullback_symmetry_inv_comp_snd [has_pullback f g] : (pullback_symmetry f g).inv ≫ pullback.snd = pullback.fst := by simp [iso.inv_comp_eq] end pullback_symmetry section pushout_symmetry open walking_cospan variables (f : X ⟶ Y) (g : X ⟶ Z) /-- Making this a global instance would make the typeclass seach go in an infinite loop. -/ lemma has_pushout_symmetry [has_pushout f g] : has_pushout g f := ⟨⟨⟨pushout_cocone.mk _ _ pushout.condition.symm, pushout_cocone.flip_is_colimit (pushout_is_pushout _ _)⟩⟩⟩ local attribute [instance] has_pushout_symmetry /-- The isomorphism `Y ⨿[X] Z ≅ Z ⨿[X] Y`. -/ def pushout_symmetry [has_pushout f g] : pushout f g ≅ pushout g f := is_colimit.cocone_point_unique_up_to_iso (pushout_cocone.flip_is_colimit (pushout_is_pushout f g) : is_colimit (pushout_cocone.mk _ _ pushout.condition.symm)) (colimit.is_colimit _) @[simp, reassoc] lemma inl_comp_pushout_symmetry_hom [has_pushout f g] : pushout.inl ≫ (pushout_symmetry f g).hom = pushout.inr := (colimit.is_colimit (span f g)).comp_cocone_point_unique_up_to_iso_hom (pushout_cocone.flip_is_colimit (pushout_is_pushout g f)) _ @[simp, reassoc] lemma inr_comp_pushout_symmetry_hom [has_pushout f g] : pushout.inr ≫ (pushout_symmetry f g).hom = pushout.inl := (colimit.is_colimit (span f g)).comp_cocone_point_unique_up_to_iso_hom (pushout_cocone.flip_is_colimit (pushout_is_pushout g f)) _ @[simp, reassoc] lemma inl_comp_pushout_symmetry_inv [has_pushout f g] : pushout.inl ≫ (pushout_symmetry f g).inv = pushout.inr := by simp [iso.comp_inv_eq] @[simp, reassoc] lemma inr_comp_pushout_symmetry_inv [has_pushout f g] : pushout.inr ≫ (pushout_symmetry f g).inv = pushout.inl := by simp [iso.comp_inv_eq] end pushout_symmetry section pullback_left_iso open walking_cospan /-- The pullback of `f, g` is also the pullback of `f ≫ i, g ≫ i` for any mono `i`. -/ noncomputable def pullback_is_pullback_of_comp_mono (f : X ⟶ W) (g : Y ⟶ W) (i : W ⟶ Z) [mono i] [has_pullback f g] : is_limit (pullback_cone.mk pullback.fst pullback.snd _) := pullback_cone.is_limit_of_comp_mono f g i _ (limit.is_limit (cospan f g)) instance has_pullback_of_comp_mono (f : X ⟶ W) (g : Y ⟶ W) (i : W ⟶ Z) [mono i] [has_pullback f g] : has_pullback (f ≫ i) (g ≫ i) := ⟨⟨⟨_,pullback_is_pullback_of_comp_mono f g i⟩⟩⟩ variables (f : X ⟶ Z) (g : Y ⟶ Z) [is_iso f] /-- If `f : X ⟶ Z` is iso, then `X ×[Z] Y ≅ Y`. This is the explicit limit cone. -/ def pullback_cone_of_left_iso : pullback_cone f g := pullback_cone.mk (g ≫ inv f) (𝟙 _) $ by simp @[simp] lemma pullback_cone_of_left_iso_X : (pullback_cone_of_left_iso f g).X = Y := rfl @[simp] lemma pullback_cone_of_left_iso_fst : (pullback_cone_of_left_iso f g).fst = g ≫ inv f := rfl @[simp] lemma pullback_cone_of_left_iso_snd : (pullback_cone_of_left_iso f g).snd = 𝟙 _ := rfl @[simp] lemma pullback_cone_of_left_iso_π_app_none : (pullback_cone_of_left_iso f g).π.app none = g := by { delta pullback_cone_of_left_iso, simp } @[simp] lemma pullback_cone_of_left_iso_π_app_left : (pullback_cone_of_left_iso f g).π.app left = g ≫ inv f := rfl @[simp] lemma pullback_cone_of_left_iso_π_app_right : (pullback_cone_of_left_iso f g).π.app right = 𝟙 _ := rfl /-- Verify that the constructed limit cone is indeed a limit. -/ def pullback_cone_of_left_iso_is_limit : is_limit (pullback_cone_of_left_iso f g) := pullback_cone.is_limit_aux' _ (λ s, ⟨s.snd, by simp [← s.condition_assoc]⟩) lemma has_pullback_of_left_iso : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_left_iso_is_limit f g⟩⟩⟩ local attribute [instance] has_pullback_of_left_iso instance pullback_snd_iso_of_left_iso : is_iso (pullback.snd : pullback f g ⟶ _) := begin refine ⟨⟨pullback.lift (g ≫ inv f) (𝟙 _) (by simp), _, by simp⟩⟩, ext, { simp [← pullback.condition_assoc] }, { simp [pullback.condition_assoc] }, end variables (i : Z ⟶ W) [mono i] instance has_pullback_of_right_factors_mono (f : X ⟶ Z) : has_pullback i (f ≫ i) := by { conv { congr, rw ←category.id_comp i, }, apply_instance } instance pullback_snd_iso_of_right_factors_mono (f : X ⟶ Z) : is_iso (pullback.snd : pullback i (f ≫ i) ⟶ _) := begin convert (congr_arg is_iso (show _ ≫ pullback.snd = _, from limit.iso_limit_cone_hom_π ⟨_,pullback_is_pullback_of_comp_mono (𝟙 _) f i⟩ walking_cospan.right)).mp infer_instance; exact (category.id_comp _).symm end end pullback_left_iso section pullback_right_iso open walking_cospan variables (f : X ⟶ Z) (g : Y ⟶ Z) [is_iso g] /-- If `g : Y ⟶ Z` is iso, then `X ×[Z] Y ≅ X`. This is the explicit limit cone. -/ def pullback_cone_of_right_iso : pullback_cone f g := pullback_cone.mk (𝟙 _) (f ≫ inv g) $ by simp @[simp] lemma pullback_cone_of_right_iso_X : (pullback_cone_of_right_iso f g).X = X := rfl @[simp] lemma pullback_cone_of_right_iso_fst : (pullback_cone_of_right_iso f g).fst = 𝟙 _ := rfl @[simp] lemma pullback_cone_of_right_iso_snd : (pullback_cone_of_right_iso f g).snd = f ≫ inv g := rfl @[simp] lemma pullback_cone_of_right_iso_π_app_none : (pullback_cone_of_right_iso f g).π.app none = f := category.id_comp _ @[simp] lemma pullback_cone_of_right_iso_π_app_left : (pullback_cone_of_right_iso f g).π.app left = 𝟙 _ := rfl @[simp] lemma pullback_cone_of_right_iso_π_app_right : (pullback_cone_of_right_iso f g).π.app right = f ≫ inv g := rfl /-- Verify that the constructed limit cone is indeed a limit. -/ def pullback_cone_of_right_iso_is_limit : is_limit (pullback_cone_of_right_iso f g) := pullback_cone.is_limit_aux' _ (λ s, ⟨s.fst, by simp [s.condition_assoc]⟩) lemma has_pullback_of_right_iso : has_pullback f g := ⟨⟨⟨_, pullback_cone_of_right_iso_is_limit f g⟩⟩⟩ local attribute [instance] has_pullback_of_right_iso instance pullback_snd_iso_of_right_iso : is_iso (pullback.fst : pullback f g ⟶ _) := begin refine ⟨⟨pullback.lift (𝟙 _) (f ≫ inv g) (by simp), _, by simp⟩⟩, ext, { simp }, { simp [pullback.condition_assoc] }, end variables (i : Z ⟶ W) [mono i] instance has_pullback_of_left_factors_mono (f : X ⟶ Z) : has_pullback (f ≫ i) i := by { conv { congr, skip, rw ←category.id_comp i, }, apply_instance } instance pullback_snd_iso_of_left_factors_mono (f : X ⟶ Z) : is_iso (pullback.fst : pullback (f ≫ i) i ⟶ _) := begin convert (congr_arg is_iso (show _ ≫ pullback.fst = _, from limit.iso_limit_cone_hom_π ⟨_,pullback_is_pullback_of_comp_mono f (𝟙 _) i⟩ walking_cospan.left)).mp infer_instance; exact (category.id_comp _).symm end end pullback_right_iso section pushout_left_iso open walking_span /-- The pushout of `f, g` is also the pullback of `h ≫ f, h ≫ g` for any epi `h`. -/ noncomputable def pushout_is_pushout_of_epi_comp (f : X ⟶ Y) (g : X ⟶ Z) (h : W ⟶ X) [epi h] [has_pushout f g] : is_colimit (pushout_cocone.mk pushout.inl pushout.inr _) := pushout_cocone.is_colimit_of_epi_comp f g h _ (colimit.is_colimit (span f g)) instance has_pushout_of_epi_comp (f : X ⟶ Y) (g : X ⟶ Z) (h : W ⟶ X) [epi h] [has_pushout f g] : has_pushout (h ≫ f) (h ≫ g) := ⟨⟨⟨_,pushout_is_pushout_of_epi_comp f g h⟩⟩⟩ variables (f : X ⟶ Y) (g : X ⟶ Z) [is_iso f] /-- If `f : X ⟶ Y` is iso, then `Y ⨿[X] Z ≅ Z`. This is the explicit colimit cocone. -/ def pushout_cocone_of_left_iso : pushout_cocone f g := pushout_cocone.mk (inv f ≫ g) (𝟙 _) $ by simp @[simp] lemma pushout_cocone_of_left_iso_X : (pushout_cocone_of_left_iso f g).X = Z := rfl @[simp] lemma pushout_cocone_of_left_iso_inl : (pushout_cocone_of_left_iso f g).inl = inv f ≫ g := rfl @[simp] lemma pushout_cocone_of_left_iso_inr : (pushout_cocone_of_left_iso f g).inr = 𝟙 _ := rfl @[simp] lemma pushout_cocone_of_left_iso_ι_app_none : (pushout_cocone_of_left_iso f g).ι.app none = g := by { delta pushout_cocone_of_left_iso, simp } @[simp] lemma pushout_cocone_of_left_iso_ι_app_left : (pushout_cocone_of_left_iso f g).ι.app left = inv f ≫ g := rfl @[simp] lemma pushout_cocone_of_left_iso_ι_app_right : (pushout_cocone_of_left_iso f g).ι.app right = 𝟙 _ := rfl /-- Verify that the constructed cocone is indeed a colimit. -/ def pushout_cocone_of_left_iso_is_limit : is_colimit (pushout_cocone_of_left_iso f g) := pushout_cocone.is_colimit_aux' _ (λ s, ⟨s.inr, by simp [← s.condition]⟩) lemma has_pushout_of_left_iso : has_pushout f g := ⟨⟨⟨_, pushout_cocone_of_left_iso_is_limit f g⟩⟩⟩ local attribute [instance] has_pushout_of_left_iso instance pushout_inr_iso_of_left_iso : is_iso (pushout.inr : _ ⟶ pushout f g) := begin refine ⟨⟨pushout.desc (inv f ≫ g) (𝟙 _) (by simp), (by simp), _⟩⟩, ext, { simp [← pushout.condition] }, { simp [pushout.condition_assoc] }, end variables (h : W ⟶ X) [epi h] instance has_pushout_of_right_factors_epi (f : X ⟶ Y) : has_pushout h (h ≫ f) := by { conv { congr, rw ←category.comp_id h, }, apply_instance } instance pushout_inr_iso_of_right_factors_epi (f : X ⟶ Y) : is_iso (pushout.inr : _ ⟶ pushout h (h ≫ f)) := begin convert (congr_arg is_iso (show pushout.inr ≫ _ = _, from colimit.iso_colimit_cocone_ι_inv ⟨_, pushout_is_pushout_of_epi_comp (𝟙 _) f h⟩ walking_span.right)).mp infer_instance; exact (category.comp_id _).symm end end pushout_left_iso section pushout_right_iso open walking_span variables (f : X ⟶ Y) (g : X ⟶ Z) [is_iso g] /-- If `f : X ⟶ Z` is iso, then `Y ⨿[X] Z ≅ Y`. This is the explicit colimit cocone. -/ def pushout_cocone_of_right_iso : pushout_cocone f g := pushout_cocone.mk (𝟙 _) (inv g ≫ f) $ by simp @[simp] lemma pushout_cocone_of_right_iso_X : (pushout_cocone_of_right_iso f g).X = Y := rfl @[simp] lemma pushout_cocone_of_right_iso_inl : (pushout_cocone_of_right_iso f g).inl = 𝟙 _ := rfl @[simp] lemma pushout_cocone_of_right_iso_inr : (pushout_cocone_of_right_iso f g).inr = inv g ≫ f := rfl @[simp] lemma pushout_cocone_of_right_iso_ι_app_none : (pushout_cocone_of_right_iso f g).ι.app none = f := by { delta pushout_cocone_of_right_iso, simp } @[simp] lemma pushout_cocone_of_right_iso_ι_app_left : (pushout_cocone_of_right_iso f g).ι.app left = 𝟙 _ := rfl @[simp] lemma pushout_cocone_of_right_iso_ι_app_right : (pushout_cocone_of_right_iso f g).ι.app right = inv g ≫ f := rfl /-- Verify that the constructed cocone is indeed a colimit. -/ def pushout_cocone_of_right_iso_is_limit : is_colimit (pushout_cocone_of_right_iso f g) := pushout_cocone.is_colimit_aux' _ (λ s, ⟨s.inl, by simp [←s.condition]⟩) lemma has_pushout_of_right_iso : has_pushout f g := ⟨⟨⟨_, pushout_cocone_of_right_iso_is_limit f g⟩⟩⟩ local attribute [instance] has_pushout_of_right_iso instance pushout_inl_iso_of_right_iso : is_iso (pushout.inl : _ ⟶ pushout f g) := begin refine ⟨⟨pushout.desc (𝟙 _) (inv g ≫ f) (by simp), (by simp), _⟩⟩, ext, { simp [←pushout.condition] }, { simp [pushout.condition] }, end variables (h : W ⟶ X) [epi h] instance has_pushout_of_left_factors_epi (f : X ⟶ Y) : has_pushout (h ≫ f) h := by { conv { congr, skip, rw ←category.comp_id h, }, apply_instance } instance pushout_inl_iso_of_left_factors_epi (f : X ⟶ Y) : is_iso (pushout.inl : _ ⟶ pushout (h ≫ f) h) := begin convert (congr_arg is_iso (show pushout.inl ≫ _ = _, from colimit.iso_colimit_cocone_ι_inv ⟨_, pushout_is_pushout_of_epi_comp f (𝟙 _) h⟩ walking_span.left)).mp infer_instance; exact (category.comp_id _).symm end end pushout_right_iso section open walking_cospan variable (f : X ⟶ Y) instance has_kernel_pair_of_mono [mono f] : has_pullback f f := ⟨⟨⟨_, pullback_cone.is_limit_mk_id_id f⟩⟩⟩ lemma fst_eq_snd_of_mono_eq [mono f] : (pullback.fst : pullback f f ⟶ _) = pullback.snd := ((pullback_cone.is_limit_mk_id_id f).fac (get_limit_cone (cospan f f)).cone left).symm.trans ((pullback_cone.is_limit_mk_id_id f).fac (get_limit_cone (cospan f f)).cone right : _) @[simp] lemma pullback_symmetry_hom_of_mono_eq [mono f] : (pullback_symmetry f f).hom = 𝟙 _ := by ext; simp [fst_eq_snd_of_mono_eq] instance fst_iso_of_mono_eq [mono f] : is_iso (pullback.fst : pullback f f ⟶ _) := begin refine ⟨⟨pullback.lift (𝟙 _) (𝟙 _) (by simp), _, by simp⟩⟩, ext, { simp }, { simp [fst_eq_snd_of_mono_eq] } end instance snd_iso_of_mono_eq [mono f] : is_iso (pullback.snd : pullback f f ⟶ _) := by { rw ← fst_eq_snd_of_mono_eq, apply_instance } end section open walking_span variable (f : X ⟶ Y) instance has_cokernel_pair_of_epi [epi f] : has_pushout f f := ⟨⟨⟨_, pushout_cocone.is_colimit_mk_id_id f⟩⟩⟩ lemma inl_eq_inr_of_epi_eq [epi f] : (pushout.inl : _ ⟶ pushout f f) = pushout.inr := ((pushout_cocone.is_colimit_mk_id_id f).fac (get_colimit_cocone (span f f)).cocone left).symm.trans ((pushout_cocone.is_colimit_mk_id_id f).fac (get_colimit_cocone (span f f)).cocone right : _) @[simp] lemma pullback_symmetry_hom_of_epi_eq [epi f] : (pushout_symmetry f f).hom = 𝟙 _ := by ext; simp [inl_eq_inr_of_epi_eq] instance inl_iso_of_epi_eq [epi f] : is_iso (pushout.inl : _ ⟶ pushout f f) := begin refine ⟨⟨pushout.desc (𝟙 _) (𝟙 _) (by simp), by simp, _⟩⟩, ext, { simp }, { simp [inl_eq_inr_of_epi_eq] } end instance inr_iso_of_epi_eq [epi f] : is_iso (pushout.inr : _ ⟶ pushout f f) := by { rw ← inl_eq_inr_of_epi_eq, apply_instance } end section paste_lemma variables {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ X₂) (f₂ : X₂ ⟶ X₃) (g₁ : Y₁ ⟶ Y₂) (g₂ : Y₂ ⟶ Y₃) variables (i₁ : X₁ ⟶ Y₁) (i₂ : X₂ ⟶ Y₂) (i₃ : X₃ ⟶ Y₃) variables (h₁ : i₁ ≫ g₁ = f₁ ≫ i₂) (h₂ : i₂ ≫ g₂ = f₂ ≫ i₃) /-- Given X₁ - f₁ -> X₂ - f₂ -> X₃ | | | i₁ i₂ i₃ ∨ ∨ ∨ Y₁ - g₁ -> Y₂ - g₂ -> Y₃ Then the big square is a pullback if both the small squares are. -/ def big_square_is_pullback (H : is_limit (pullback_cone.mk _ _ h₂)) (H' : is_limit (pullback_cone.mk _ _ h₁)) : is_limit (pullback_cone.mk _ _ (show i₁ ≫ g₁ ≫ g₂ = (f₁ ≫ f₂) ≫ i₃, by rw [← category.assoc, h₁, category.assoc, h₂, category.assoc])) := begin fapply pullback_cone.is_limit_aux', intro s, have : (s.fst ≫ g₁) ≫ g₂ = s.snd ≫ i₃ := by rw [← s.condition, category.assoc], rcases pullback_cone.is_limit.lift' H (s.fst ≫ g₁) s.snd this with ⟨l₁, hl₁, hl₁'⟩, rcases pullback_cone.is_limit.lift' H' s.fst l₁ hl₁.symm with ⟨l₂, hl₂, hl₂'⟩, use l₂, use hl₂, use show l₂ ≫ f₁ ≫ f₂ = s.snd, by { rw [← hl₁', ← hl₂', category.assoc], refl }, intros m hm₁ hm₂, apply pullback_cone.is_limit.hom_ext H', { erw [hm₁, hl₂] }, { apply pullback_cone.is_limit.hom_ext H, { erw [category.assoc, ← h₁, ← category.assoc, hm₁, ← hl₂, category.assoc, category.assoc, h₁], refl }, { erw [category.assoc, hm₂, ← hl₁', ← hl₂'] } } end /-- Given X₁ - f₁ -> X₂ - f₂ -> X₃ | | | i₁ i₂ i₃ ∨ ∨ ∨ Y₁ - g₁ -> Y₂ - g₂ -> Y₃ Then the big square is a pushout if both the small squares are. -/ def big_square_is_pushout (H : is_colimit (pushout_cocone.mk _ _ h₂)) (H' : is_colimit (pushout_cocone.mk _ _ h₁)) : is_colimit (pushout_cocone.mk _ _ (show i₁ ≫ g₁ ≫ g₂ = (f₁ ≫ f₂) ≫ i₃, by rw [← category.assoc, h₁, category.assoc, h₂, category.assoc])) := begin fapply pushout_cocone.is_colimit_aux', intro s, have : i₁ ≫ s.inl = f₁ ≫ (f₂ ≫ s.inr) := by rw [s.condition, category.assoc], rcases pushout_cocone.is_colimit.desc' H' s.inl (f₂ ≫ s.inr) this with ⟨l₁, hl₁, hl₁'⟩, rcases pushout_cocone.is_colimit.desc' H l₁ s.inr hl₁' with ⟨l₂, hl₂, hl₂'⟩, use l₂, use show (g₁ ≫ g₂) ≫ l₂ = s.inl, by { rw [← hl₁, ← hl₂, category.assoc], refl }, use hl₂', intros m hm₁ hm₂, apply pushout_cocone.is_colimit.hom_ext H, { apply pushout_cocone.is_colimit.hom_ext H', { erw [← category.assoc, hm₁, hl₂, hl₁] }, { erw [← category.assoc, h₂, category.assoc, hm₂, ← hl₂', ← category.assoc, ← category.assoc, ← h₂], refl } }, { erw [hm₂, hl₂'] } end /-- Given X₁ - f₁ -> X₂ - f₂ -> X₃ | | | i₁ i₂ i₃ ∨ ∨ ∨ Y₁ - g₁ -> Y₂ - g₂ -> Y₃ Then the left square is a pullback if the right square and the big square are. -/ def left_square_is_pullback (H : is_limit (pullback_cone.mk _ _ h₂)) (H' : is_limit (pullback_cone.mk _ _ (show i₁ ≫ g₁ ≫ g₂ = (f₁ ≫ f₂) ≫ i₃, by rw [← category.assoc, h₁, category.assoc, h₂, category.assoc]))) : is_limit (pullback_cone.mk _ _ h₁) := begin fapply pullback_cone.is_limit_aux', intro s, have : s.fst ≫ g₁ ≫ g₂ = (s.snd ≫ f₂) ≫ i₃ := by { rw [← category.assoc, s.condition, category.assoc, category.assoc, h₂] }, rcases pullback_cone.is_limit.lift' H' s.fst (s.snd ≫ f₂) this with ⟨l₁, hl₁, hl₁'⟩, use l₁, use hl₁, split, { apply pullback_cone.is_limit.hom_ext H, { erw [category.assoc, ← h₁, ← category.assoc, hl₁, s.condition], refl }, { erw [category.assoc, hl₁'], refl } }, { intros m hm₁ hm₂, apply pullback_cone.is_limit.hom_ext H', { erw [hm₁, hl₁] }, { erw [hl₁', ← hm₂], exact (category.assoc _ _ _).symm } } end /-- Given X₁ - f₁ -> X₂ - f₂ -> X₃ | | | i₁ i₂ i₃ ∨ ∨ ∨ Y₁ - g₁ -> Y₂ - g₂ -> Y₃ Then the right square is a pushout if the left square and the big square are. -/ def right_square_is_pushout (H : is_colimit (pushout_cocone.mk _ _ h₁)) (H' : is_colimit (pushout_cocone.mk _ _ (show i₁ ≫ g₁ ≫ g₂ = (f₁ ≫ f₂) ≫ i₃, by rw [← category.assoc, h₁, category.assoc, h₂, category.assoc]))) : is_colimit (pushout_cocone.mk _ _ h₂) := begin fapply pushout_cocone.is_colimit_aux', intro s, have : i₁ ≫ g₁ ≫ s.inl = (f₁ ≫ f₂) ≫ s.inr := by { rw [category.assoc, ← s.condition, ← category.assoc, ← category.assoc, h₁] }, rcases pushout_cocone.is_colimit.desc' H' (g₁ ≫ s.inl) s.inr this with ⟨l₁, hl₁, hl₁'⟩, dsimp at *, use l₁, refine ⟨_,_,_⟩, { apply pushout_cocone.is_colimit.hom_ext H, { erw [← category.assoc, hl₁], refl }, { erw [← category.assoc, h₂, category.assoc, hl₁', s.condition] } }, { exact hl₁' }, { intros m hm₁ hm₂, apply pushout_cocone.is_colimit.hom_ext H', { erw [hl₁, category.assoc, hm₁] }, { erw [hm₂, hl₁'] } } end end paste_lemma section variables (f : X ⟶ Z) (g : Y ⟶ Z) (f' : W ⟶ X) variables [has_pullback f g] [has_pullback f' (pullback.fst : pullback f g ⟶ _)] variables [has_pullback (f' ≫ f) g] /-- The canonical isomorphism `W ×[X] (X ×[Z] Y) ≅ W ×[Z] Y` -/ noncomputable def pullback_right_pullback_fst_iso : pullback f' (pullback.fst : pullback f g ⟶ _) ≅ pullback (f' ≫ f) g := begin let := big_square_is_pullback (pullback.snd : pullback f' (pullback.fst : pullback f g ⟶ _) ⟶ _) pullback.snd f' f pullback.fst pullback.fst g pullback.condition pullback.condition (pullback_is_pullback _ _) (pullback_is_pullback _ _), exact (this.cone_point_unique_up_to_iso (pullback_is_pullback _ _) : _) end @[simp, reassoc] lemma pullback_right_pullback_fst_iso_hom_fst : (pullback_right_pullback_fst_iso f g f').hom ≫ pullback.fst = pullback.fst := is_limit.cone_point_unique_up_to_iso_hom_comp _ _ walking_cospan.left @[simp, reassoc] lemma pullback_right_pullback_fst_iso_hom_snd : (pullback_right_pullback_fst_iso f g f').hom ≫ pullback.snd = pullback.snd ≫ pullback.snd := is_limit.cone_point_unique_up_to_iso_hom_comp _ _ walking_cospan.right @[simp, reassoc] lemma pullback_right_pullback_fst_iso_inv_fst : (pullback_right_pullback_fst_iso f g f').inv ≫ pullback.fst = pullback.fst := is_limit.cone_point_unique_up_to_iso_inv_comp _ _ walking_cospan.left @[simp, reassoc] lemma pullback_right_pullback_fst_iso_inv_snd_snd : (pullback_right_pullback_fst_iso f g f').inv ≫ pullback.snd ≫ pullback.snd = pullback.snd := is_limit.cone_point_unique_up_to_iso_inv_comp _ _ walking_cospan.right @[simp, reassoc] lemma pullback_right_pullback_fst_iso_inv_snd_fst : (pullback_right_pullback_fst_iso f g f').inv ≫ pullback.snd ≫ pullback.fst = pullback.fst ≫ f' := begin rw ← pullback.condition, exact pullback_right_pullback_fst_iso_inv_fst_assoc _ _ _ _ end end section variables (f : X ⟶ Y) (g : X ⟶ Z) (g' : Z ⟶ W) variables [has_pushout f g] [has_pushout (pushout.inr : _ ⟶ pushout f g) g'] variables [has_pushout f (g ≫ g')] /-- The canonical isomorphism `(Y ⨿[X] Z) ⨿[Z] W ≅ Y ×[X] W` -/ noncomputable def pushout_left_pushout_inr_iso : pushout (pushout.inr : _ ⟶ pushout f g) g' ≅ pushout f (g ≫ g') := ((big_square_is_pushout g g' _ _ f _ _ pushout.condition pushout.condition (pushout_is_pushout _ _) (pushout_is_pushout _ _)) .cocone_point_unique_up_to_iso (pushout_is_pushout _ _) : _) @[simp, reassoc] lemma inl_pushout_left_pushout_inr_iso_inv : pushout.inl ≫ (pushout_left_pushout_inr_iso f g g').inv = pushout.inl ≫ pushout.inl := ((big_square_is_pushout g g' _ _ f _ _ pushout.condition pushout.condition (pushout_is_pushout _ _) (pushout_is_pushout _ _)) .comp_cocone_point_unique_up_to_iso_inv (pushout_is_pushout _ _) walking_span.left : _) @[simp, reassoc] lemma inr_pushout_left_pushout_inr_iso_hom : pushout.inr ≫ (pushout_left_pushout_inr_iso f g g').hom = pushout.inr := ((big_square_is_pushout g g' _ _ f _ _ pushout.condition pushout.condition (pushout_is_pushout _ _) (pushout_is_pushout _ _)) .comp_cocone_point_unique_up_to_iso_hom (pushout_is_pushout _ _) walking_span.right : _) @[simp, reassoc] lemma inr_pushout_left_pushout_inr_iso_inv : pushout.inr ≫ (pushout_left_pushout_inr_iso f g g').inv = pushout.inr := by rw [iso.comp_inv_eq, inr_pushout_left_pushout_inr_iso_hom] @[simp, reassoc] lemma inl_inl_pushout_left_pushout_inr_iso_hom : pushout.inl ≫ pushout.inl ≫ (pushout_left_pushout_inr_iso f g g').hom = pushout.inl := by rw [← category.assoc, ← iso.eq_comp_inv, inl_pushout_left_pushout_inr_iso_inv] @[simp, reassoc] lemma inr_inl_pushout_left_pushout_inr_iso_hom : pushout.inr ≫ pushout.inl ≫ (pushout_left_pushout_inr_iso f g g').hom = g' ≫ pushout.inr := by rw [← category.assoc, ← iso.eq_comp_inv, category.assoc, inr_pushout_left_pushout_inr_iso_inv, pushout.condition] end section pullback_assoc /- The objects and morphisms are as follows: Z₂ - g₄ -> X₃ | | g₃ f₄ ∨ ∨ Z₁ - g₂ -> X₂ - f₃ -> Y₂ | | g₁ f₂ ∨ ∨ X₁ - f₁ -> Y₁ where the two squares are pullbacks. We can then construct the pullback squares W - l₂ -> Z₂ - g₄ -> X₃ | | l₁ f₄ ∨ ∨ Z₁ - g₂ -> X₂ - f₃ -> Y₂ and W' - l₂' -> Z₂ | | l₁' g₃ ∨ ∨ Z₁ X₂ | | g₁ f₂ ∨ ∨ X₁ - f₁ -> Y₁ We will show that both `W` and `W'` are pullbacks over `g₁, g₂`, and thus we may construct a canonical isomorphism between them. -/ variables {X₁ X₂ X₃ Y₁ Y₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₁) (f₃ : X₂ ⟶ Y₂) variables (f₄ : X₃ ⟶ Y₂) [has_pullback f₁ f₂] [has_pullback f₃ f₄] include f₁ f₂ f₃ f₄ local notation `Z₁` := pullback f₁ f₂ local notation `Z₂` := pullback f₃ f₄ local notation `g₁` := (pullback.fst : Z₁ ⟶ X₁) local notation `g₂` := (pullback.snd : Z₁ ⟶ X₂) local notation `g₃` := (pullback.fst : Z₂ ⟶ X₂) local notation `g₄` := (pullback.snd : Z₂ ⟶ X₃) local notation `W` := pullback (g₂ ≫ f₃) f₄ local notation `W'` := pullback f₁ (g₃ ≫ f₂) local notation `l₁` := (pullback.fst : W ⟶ Z₁) local notation `l₂` := (pullback.lift (pullback.fst ≫ g₂) pullback.snd ((category.assoc _ _ _).trans pullback.condition) : W ⟶ Z₂) local notation `l₁'`:= (pullback.lift pullback.fst (pullback.snd ≫ g₃) (pullback.condition.trans (category.assoc _ _ _).symm) : W' ⟶ Z₁) local notation `l₂'`:= (pullback.snd : W' ⟶ Z₂) /-- `(X₁ ×[Y₁] X₂) ×[Y₂] X₃` is the pullback `(X₁ ×[Y₁] X₂) ×[X₂] (X₂ ×[Y₂] X₃)`. -/ def pullback_pullback_left_is_pullback [has_pullback (g₂ ≫ f₃) f₄] : is_limit (pullback_cone.mk l₁ l₂ (show l₁ ≫ g₂ = l₂ ≫ g₃, from (pullback.lift_fst _ _ _).symm)) := begin apply left_square_is_pullback, exact pullback_is_pullback f₃ f₄, convert pullback_is_pullback (g₂ ≫ f₃) f₄, rw pullback.lift_snd end /-- `(X₁ ×[Y₁] X₂) ×[Y₂] X₃` is the pullback `X₁ ×[Y₁] (X₂ ×[Y₂] X₃)`. -/ def pullback_assoc_is_pullback [has_pullback (g₂ ≫ f₃) f₄] : is_limit (pullback_cone.mk (l₁ ≫ g₁) l₂ (show (l₁ ≫ g₁) ≫ f₁ = l₂ ≫ (g₃ ≫ f₂), by rw [pullback.lift_fst_assoc, category.assoc, category.assoc, pullback.condition])) := begin apply pullback_cone.flip_is_limit, apply big_square_is_pullback, { apply pullback_cone.flip_is_limit, exact pullback_is_pullback f₁ f₂ }, { apply pullback_cone.flip_is_limit, apply pullback_pullback_left_is_pullback }, { exact pullback.lift_fst _ _ _ }, { exact pullback.condition.symm } end lemma has_pullback_assoc [has_pullback (g₂ ≫ f₃) f₄] : has_pullback f₁ (g₃ ≫ f₂) := ⟨⟨⟨_, pullback_assoc_is_pullback f₁ f₂ f₃ f₄⟩⟩⟩ /-- `X₁ ×[Y₁] (X₂ ×[Y₂] X₃)` is the pullback `(X₁ ×[Y₁] X₂) ×[X₂] (X₂ ×[Y₂] X₃)`. -/ def pullback_pullback_right_is_pullback [has_pullback f₁ (g₃ ≫ f₂)] : is_limit (pullback_cone.mk l₁' l₂' (show l₁' ≫ g₂ = l₂' ≫ g₃, from pullback.lift_snd _ _ _)) := begin apply pullback_cone.flip_is_limit, apply left_square_is_pullback, { apply pullback_cone.flip_is_limit, exact pullback_is_pullback f₁ f₂ }, { apply pullback_cone.flip_is_limit, convert pullback_is_pullback f₁ (g₃ ≫ f₂), rw pullback.lift_fst }, { exact pullback.condition.symm } end /-- `X₁ ×[Y₁] (X₂ ×[Y₂] X₃)` is the pullback `(X₁ ×[Y₁] X₂) ×[Y₂] X₃`. -/ def pullback_assoc_symm_is_pullback [has_pullback f₁ (g₃ ≫ f₂)] : is_limit (pullback_cone.mk l₁' (l₂' ≫ g₄) (show l₁' ≫ (g₂ ≫ f₃) = (l₂' ≫ g₄) ≫ f₄, by rw [pullback.lift_snd_assoc, category.assoc, category.assoc, pullback.condition])) := begin apply big_square_is_pullback, exact pullback_is_pullback f₃ f₄, apply pullback_pullback_right_is_pullback end lemma has_pullback_assoc_symm [has_pullback f₁ (g₃ ≫ f₂)] : has_pullback (g₂ ≫ f₃) f₄ := ⟨⟨⟨_, pullback_assoc_symm_is_pullback f₁ f₂ f₃ f₄⟩⟩⟩ variables [has_pullback (g₂ ≫ f₃) f₄] [has_pullback f₁ (g₃ ≫ f₂)] /-- The canonical isomorphism `(X₁ ×[Y₁] X₂) ×[Y₂] X₃ ≅ X₁ ×[Y₁] (X₂ ×[Y₂] X₃)`. -/ noncomputable def pullback_assoc : pullback (pullback.snd ≫ f₃ : pullback f₁ f₂ ⟶ _) f₄ ≅ pullback f₁ (pullback.fst ≫ f₂ : pullback f₃ f₄ ⟶ _) := (pullback_pullback_left_is_pullback f₁ f₂ f₃ f₄).cone_point_unique_up_to_iso (pullback_pullback_right_is_pullback f₁ f₂ f₃ f₄) @[simp, reassoc] lemma pullback_assoc_inv_fst_fst : (pullback_assoc f₁ f₂ f₃ f₄).inv ≫ pullback.fst ≫ pullback.fst = pullback.fst := begin transitivity l₁' ≫ pullback.fst, rw ← category.assoc, congr' 1, exact is_limit.cone_point_unique_up_to_iso_inv_comp _ _ walking_cospan.left, exact pullback.lift_fst _ _ _, end @[simp, reassoc] lemma pullback_assoc_hom_fst : (pullback_assoc f₁ f₂ f₃ f₄).hom ≫ pullback.fst = pullback.fst ≫ pullback.fst := by rw [← iso.eq_inv_comp, pullback_assoc_inv_fst_fst] @[simp, reassoc] lemma pullback_assoc_hom_snd_fst : (pullback_assoc f₁ f₂ f₃ f₄).hom ≫ pullback.snd ≫ pullback.fst = pullback.fst ≫ pullback.snd := begin transitivity l₂ ≫ pullback.fst, rw ← category.assoc, congr' 1, exact is_limit.cone_point_unique_up_to_iso_hom_comp _ _ walking_cospan.right, exact pullback.lift_fst _ _ _, end @[simp, reassoc] lemma pullback_assoc_hom_snd_snd : (pullback_assoc f₁ f₂ f₃ f₄).hom ≫ pullback.snd ≫ pullback.snd = pullback.snd := begin transitivity l₂ ≫ pullback.snd, rw ← category.assoc, congr' 1, exact is_limit.cone_point_unique_up_to_iso_hom_comp _ _ walking_cospan.right, exact pullback.lift_snd _ _ _, end @[simp, reassoc] lemma pullback_assoc_inv_fst_snd : (pullback_assoc f₁ f₂ f₃ f₄).inv ≫ pullback.fst ≫ pullback.snd = pullback.snd ≫ pullback.fst := by rw [iso.inv_comp_eq, pullback_assoc_hom_snd_fst] @[simp, reassoc] lemma pullback_assoc_inv_snd : (pullback_assoc f₁ f₂ f₃ f₄).inv ≫ pullback.snd = pullback.snd ≫ pullback.snd := by rw [iso.inv_comp_eq, pullback_assoc_hom_snd_snd] end pullback_assoc section pushout_assoc /- The objects and morphisms are as follows: Z₂ - g₄ -> X₃ | | g₃ f₄ ∨ ∨ Z₁ - g₂ -> X₂ - f₃ -> Y₂ | | g₁ f₂ ∨ ∨ X₁ - f₁ -> Y₁ where the two squares are pushouts. We can then construct the pushout squares Z₁ - g₂ -> X₂ - f₃ -> Y₂ | | g₁ l₂ ∨ ∨ X₁ - f₁ -> Y₁ - l₁ -> W and Z₂ - g₄ -> X₃ | | g₃ f₄ ∨ ∨ X₂ Y₂ | | f₂ l₂' ∨ ∨ Y₁ - l₁' -> W' We will show that both `W` and `W'` are pushouts over `f₂, f₃`, and thus we may construct a canonical isomorphism between them. -/ variables {X₁ X₂ X₃ Z₁ Z₂ : C} (g₁ : Z₁ ⟶ X₁) (g₂ : Z₁ ⟶ X₂) (g₃ : Z₂ ⟶ X₂) variables (g₄ : Z₂ ⟶ X₃) [has_pushout g₁ g₂] [has_pushout g₃ g₄] include g₁ g₂ g₃ g₄ local notation `Y₁` := pushout g₁ g₂ local notation `Y₂` := pushout g₃ g₄ local notation `f₁` := (pushout.inl : X₁ ⟶ Y₁) local notation `f₂` := (pushout.inr : X₂ ⟶ Y₁) local notation `f₃` := (pushout.inl : X₂ ⟶ Y₂) local notation `f₄` := (pushout.inr : X₃ ⟶ Y₂) local notation `W` := pushout g₁ (g₂ ≫ f₃) local notation `W'` := pushout (g₃ ≫ f₂) g₄ local notation `l₁` := (pushout.desc pushout.inl (f₃ ≫ pushout.inr) (pushout.condition.trans (category.assoc _ _ _)) : Y₁ ⟶ W) local notation `l₂` := (pushout.inr : Y₂ ⟶ W) local notation `l₁'`:= (pushout.inl : Y₁ ⟶ W') local notation `l₂'`:= (pushout.desc (f₂ ≫ pushout.inl) pushout.inr ((category.assoc _ _ _).symm.trans pushout.condition) : Y₂ ⟶ W') /-- `(X₁ ⨿[Z₁] X₂) ⨿[Z₂] X₃` is the pushout `(X₁ ⨿[Z₁] X₂) ×[X₂] (X₂ ⨿[Z₂] X₃)`. -/ def pushout_pushout_left_is_pushout [has_pushout (g₃ ≫ f₂) g₄] : is_colimit (pushout_cocone.mk l₁' l₂' (show f₂ ≫ l₁' = f₃ ≫ l₂', from (pushout.inl_desc _ _ _).symm)) := begin apply pushout_cocone.flip_is_colimit, apply right_square_is_pushout, { apply pushout_cocone.flip_is_colimit, exact pushout_is_pushout _ _ }, { apply pushout_cocone.flip_is_colimit, convert pushout_is_pushout (g₃ ≫ f₂) g₄, exact pushout.inr_desc _ _ _ }, { exact pushout.condition.symm } end /-- `(X₁ ⨿[Z₁] X₂) ⨿[Z₂] X₃` is the pushout `X₁ ⨿[Z₁] (X₂ ⨿[Z₂] X₃)`. -/ def pushout_assoc_is_pushout [has_pushout (g₃ ≫ f₂) g₄] : is_colimit (pushout_cocone.mk (f₁ ≫ l₁') l₂' (show g₁ ≫ (f₁ ≫ l₁') = (g₂ ≫ f₃) ≫ l₂', by rw [category.assoc, pushout.inl_desc, pushout.condition_assoc])) := begin apply big_square_is_pushout, { apply pushout_pushout_left_is_pushout }, { exact pushout_is_pushout _ _ } end lemma has_pushout_assoc [has_pushout (g₃ ≫ f₂) g₄] : has_pushout g₁ (g₂ ≫ f₃) := ⟨⟨⟨_, pushout_assoc_is_pushout g₁ g₂ g₃ g₄⟩⟩⟩ /-- `X₁ ⨿[Z₁] (X₂ ⨿[Z₂] X₃)` is the pushout `(X₁ ⨿[Z₁] X₂) ×[X₂] (X₂ ⨿[Z₂] X₃)`. -/ def pushout_pushout_right_is_pushout [has_pushout g₁ (g₂ ≫ f₃)] : is_colimit (pushout_cocone.mk l₁ l₂ (show f₂ ≫ l₁ = f₃ ≫ l₂, from pushout.inr_desc _ _ _)) := begin apply right_square_is_pushout, { exact pushout_is_pushout _ _ }, { convert pushout_is_pushout g₁ (g₂ ≫ f₃), rw pushout.inl_desc } end /-- `X₁ ⨿[Z₁] (X₂ ⨿[Z₂] X₃)` is the pushout `(X₁ ⨿[Z₁] X₂) ⨿[Z₂] X₃`. -/ def pushout_assoc_symm_is_pushout [has_pushout g₁ (g₂ ≫ f₃)] : is_colimit (pushout_cocone.mk l₁ (f₄ ≫ l₂) ((show (g₃ ≫ f₂) ≫ l₁ = g₄ ≫ (f₄ ≫ l₂), by rw [category.assoc, pushout.inr_desc, pushout.condition_assoc]))) := begin apply pushout_cocone.flip_is_colimit, apply big_square_is_pushout, { apply pushout_cocone.flip_is_colimit, apply pushout_pushout_right_is_pushout }, { apply pushout_cocone.flip_is_colimit, exact pushout_is_pushout _ _ }, { exact pushout.condition.symm }, { exact (pushout.inr_desc _ _ _).symm } end lemma has_pushout_assoc_symm [has_pushout g₁ (g₂ ≫ f₃)] : has_pushout (g₃ ≫ f₂) g₄ := ⟨⟨⟨_, pushout_assoc_symm_is_pushout g₁ g₂ g₃ g₄⟩⟩⟩ variables [has_pushout (g₃ ≫ f₂) g₄] [has_pushout g₁ (g₂ ≫ f₃)] /-- The canonical isomorphism `(X₁ ⨿[Z₁] X₂) ⨿[Z₂] X₃ ≅ X₁ ⨿[Z₁] (X₂ ⨿[Z₂] X₃)`. -/ noncomputable def pushout_assoc : pushout (g₃ ≫ pushout.inr : _ ⟶ pushout g₁ g₂) g₄ ≅ pushout g₁ (g₂ ≫ pushout.inl : _ ⟶ pushout g₃ g₄) := (pushout_pushout_left_is_pushout g₁ g₂ g₃ g₄).cocone_point_unique_up_to_iso (pushout_pushout_right_is_pushout g₁ g₂ g₃ g₄) @[simp, reassoc] lemma inl_inl_pushout_assoc_hom : pushout.inl ≫ pushout.inl ≫ (pushout_assoc g₁ g₂ g₃ g₄).hom = pushout.inl := begin transitivity f₁ ≫ l₁, { congr' 1, exact (pushout_pushout_left_is_pushout g₁ g₂ g₃ g₄) .comp_cocone_point_unique_up_to_iso_hom _ walking_cospan.left }, { exact pushout.inl_desc _ _ _ } end @[simp, reassoc] lemma inr_inl_pushout_assoc_hom : pushout.inr ≫ pushout.inl ≫ (pushout_assoc g₁ g₂ g₃ g₄).hom = pushout.inl ≫ pushout.inr := begin transitivity f₂ ≫ l₁, { congr' 1, exact (pushout_pushout_left_is_pushout g₁ g₂ g₃ g₄) .comp_cocone_point_unique_up_to_iso_hom _ walking_cospan.left }, { exact pushout.inr_desc _ _ _ } end @[simp, reassoc] lemma inr_inr_pushout_assoc_inv : pushout.inr ≫ pushout.inr ≫ (pushout_assoc g₁ g₂ g₃ g₄).inv = pushout.inr := begin transitivity f₄ ≫ l₂', { congr' 1, exact (pushout_pushout_left_is_pushout g₁ g₂ g₃ g₄).comp_cocone_point_unique_up_to_iso_inv (pushout_pushout_right_is_pushout g₁ g₂ g₃ g₄) walking_cospan.right }, { exact pushout.inr_desc _ _ _ } end @[simp, reassoc] lemma inl_pushout_assoc_inv : pushout.inl ≫ (pushout_assoc g₁ g₂ g₃ g₄).inv = pushout.inl ≫ pushout.inl := by rw [iso.comp_inv_eq, category.assoc, inl_inl_pushout_assoc_hom] @[simp, reassoc] lemma inl_inr_pushout_assoc_inv : pushout.inl ≫ pushout.inr ≫ (pushout_assoc g₁ g₂ g₃ g₄).inv = pushout.inr ≫ pushout.inl := by rw [← category.assoc, iso.comp_inv_eq, category.assoc, inr_inl_pushout_assoc_hom] @[simp, reassoc] lemma inr_pushout_assoc_hom : pushout.inr ≫ (pushout_assoc g₁ g₂ g₃ g₄).hom = pushout.inr ≫ pushout.inr := by rw [← iso.eq_comp_inv, category.assoc, inr_inr_pushout_assoc_inv] end pushout_assoc variables (C) /-- `has_pullbacks` represents a choice of pullback for every pair of morphisms See <https://stacks.math.columbia.edu/tag/001W> -/ abbreviation has_pullbacks := has_limits_of_shape walking_cospan C /-- `has_pushouts` represents a choice of pushout for every pair of morphisms -/ abbreviation has_pushouts := has_colimits_of_shape walking_span C /-- If `C` has all limits of diagrams `cospan f g`, then it has all pullbacks -/ lemma has_pullbacks_of_has_limit_cospan [Π {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z}, has_limit (cospan f g)] : has_pullbacks C := { has_limit := λ F, has_limit_of_iso (diagram_iso_cospan F).symm } /-- If `C` has all colimits of diagrams `span f g`, then it has all pushouts -/ lemma has_pushouts_of_has_colimit_span [Π {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z}, has_colimit (span f g)] : has_pushouts C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_span F) } /-- The duality equivalence `walking_spanᵒᵖ ≌ walking_cospan` -/ @[simps] def walking_span_op_equiv : walking_spanᵒᵖ ≌ walking_cospan := wide_pushout_shape_op_equiv _ /-- The duality equivalence `walking_cospanᵒᵖ ≌ walking_span` -/ @[simps] def walking_cospan_op_equiv : walking_cospanᵒᵖ ≌ walking_span := wide_pullback_shape_op_equiv _ /-- Having wide pullback at any universe level implies having binary pullbacks. -/ @[priority 100] -- see Note [lower instance priority] instance has_pullbacks_of_has_wide_pullbacks [has_wide_pullbacks.{w} C] : has_pullbacks C := begin haveI := has_wide_pullbacks_shrink.{0 w} C, apply_instance end end category_theory.limits
79f7120bdd516f94de6a23ca4ccd13842e63b21e
c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447
/src/categories/universal/initial.lean
447f0ca844b0ef8582b1d9f53299c3c0a81eb3b3
[ "Apache-2.0" ]
permissive
rwbarton/lean-category-theory-pr
77207b6674eeec1e258ec85dea58f3bff8d27065
591847d70c6a11c4d5561cd0eaf69b1fe85a70ab
refs/heads/master
1,584,595,111,303
1,528,029,041,000
1,528,029,041,000
135,919,126
0
0
null
1,528,041,805,000
1,528,041,805,000
null
UTF-8
Lean
false
false
3,489
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ..isomorphism import ..functor_categories import ..opposites open categories open categories.isomorphism namespace categories.initial universes u v structure InitialObject (C : Type u) [category.{u v} C] := (initial_object : C) (morphism_from_initial_object_to : ∀ Y : C, initial_object ⟶ Y) (uniqueness_of_morphisms_from_initial_object : ∀ Y : C, ∀ f g : initial_object ⟶ Y, f = g . obviously) attribute [applicable] InitialObject.morphism_from_initial_object_to make_lemma InitialObject.uniqueness_of_morphisms_from_initial_object attribute [applicable,ematch] InitialObject.uniqueness_of_morphisms_from_initial_object_lemma structure TerminalObject (C : Type u) [category.{u v} C] := (terminal_object : C) (morphism_to_terminal_object_from : ∀ Y : C, Y ⟶ terminal_object) (uniqueness_of_morphisms_to_terminal_object : ∀ Y : C, ∀ f g : Y ⟶ terminal_object, f = g . obviously) attribute [applicable] TerminalObject.morphism_to_terminal_object_from make_lemma TerminalObject.uniqueness_of_morphisms_to_terminal_object attribute [applicable,ematch] TerminalObject.uniqueness_of_morphisms_to_terminal_object_lemma section variables {C : Type u} [𝒞 : category.{u v} C] include 𝒞 instance InitialObject_coercion_to_object : has_coe (InitialObject C) C := { coe := InitialObject.initial_object } structure is_initial (X : C) := (morphism_from_initial_object_to : ∀ Y : C, X ⟶ Y) (uniqueness_of_morphisms_from_initial_object : ∀ Y : C, ∀ f g : X ⟶ Y, f = g) -- set_option pp.all true lemma InitialObjects_are_unique (X Y : InitialObject C) : X.initial_object ≅ Y.initial_object := begin -- `obviously'` says: fsplit, apply categories.initial.InitialObject.morphism_from_initial_object_to, apply categories.initial.InitialObject.morphism_from_initial_object_to, apply categories.initial.InitialObject.uniqueness_of_morphisms_from_initial_object_lemma, apply categories.initial.InitialObject.uniqueness_of_morphisms_from_initial_object_lemma end instance TerminalObject_coercion_to_object : has_coe (TerminalObject C) C := { coe := TerminalObject.terminal_object } structure is_terminal (X : C) := (morphism_to_terminal_object_from : ∀ Y : C, Y ⟶ X) (uniqueness_of_morphisms_to_terminal_object : ∀ Y : C, ∀ f g : Y ⟶ X, f = g) lemma TerminalObjects_are_unique (X Y : TerminalObject C) : X.terminal_object ≅ Y.terminal_object := begin -- `obviously'` says: fsplit, apply categories.initial.TerminalObject.morphism_to_terminal_object_from, apply categories.initial.TerminalObject.morphism_to_terminal_object_from, apply categories.initial.TerminalObject.uniqueness_of_morphisms_to_terminal_object_lemma, apply categories.initial.TerminalObject.uniqueness_of_morphisms_to_terminal_object_lemma end end class ZeroObject (C : Type u) [category.{u v} C] := (zero_object : C) (is_initial : is_initial.{u v} zero_object) (is_terminal : is_terminal.{u v} zero_object) definition ZeroObject.zero_morphism {C : Type u} [category.{u v} C] (Z : ZeroObject C) (X Y : C) : X ⟶ Y := (Z.is_terminal.morphism_to_terminal_object_from X) ≫ (Z.is_initial.morphism_from_initial_object_to Y) end categories.initial
34ae0b6b5f6b21dc6a065d96ebc2bcea02ccbb22
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/init/datatypes.lean
acd6e75264d813bdbca68f0c3e960eb5eefb6d2d
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
3,335
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Basic datatypes -/ prelude notation `Prop` := Type.{0} notation [parsing_only] `Type'` := Type.{_+1} notation [parsing_only] `Type₊` := Type.{_+1} notation `Type₁` := Type.{1} notation `Type₂` := Type.{2} notation `Type₃` := Type.{3} set_option structure.eta_thm true set_option structure.proj_mk_thm true inductive poly_unit.{l} : Type.{l} := star : poly_unit inductive unit : Type₁ := star : unit inductive true : Prop := intro : true inductive false : Prop inductive empty : Type₁ inductive eq {A : Type} (a : A) : A → Prop := refl : eq a a inductive heq {A : Type} (a : A) : Π {B : Type}, B → Prop := refl : heq a a inductive prod (A B : Type) := mk : A → B → prod A B definition prod.pr1 [reducible] [unfold 3] {A B : Type} (p : prod A B) : A := prod.rec (λ a b, a) p definition prod.pr2 [reducible] [unfold 3] {A B : Type} (p : prod A B) : B := prod.rec (λ a b, b) p inductive and (a b : Prop) : Prop := intro : a → b → and a b definition and.elim_left {a b : Prop} (H : and a b) : a := and.rec (λa b, a) H definition and.left := @and.elim_left definition and.elim_right {a b : Prop} (H : and a b) : b := and.rec (λa b, b) H definition and.right := @and.elim_right inductive sum (A B : Type) : Type := | inl {} : A → sum A B | inr {} : B → sum A B definition sum.intro_left [reducible] {A : Type} (B : Type) (a : A) : sum A B := sum.inl a definition sum.intro_right [reducible] (A : Type) {B : Type} (b : B) : sum A B := sum.inr b inductive or (a b : Prop) : Prop := | inl {} : a → or a b | inr {} : b → or a b definition or.intro_left {a : Prop} (b : Prop) (Ha : a) : or a b := or.inl Ha definition or.intro_right (a : Prop) {b : Prop} (Hb : b) : or a b := or.inr Hb structure sigma {A : Type} (B : A → Type) := mk :: (pr1 : A) (pr2 : B pr1) -- pos_num and num are two auxiliary datatypes used when parsing numerals such as 13, 0, 26. -- The parser will generate the terms (pos (bit1 (bit1 (bit0 one)))), zero, and (pos (bit0 (bit1 (bit1 one)))). -- This representation can be coerced in whatever we want (e.g., naturals, integers, reals, etc). inductive pos_num : Type := | one : pos_num | bit1 : pos_num → pos_num | bit0 : pos_num → pos_num namespace pos_num definition succ (a : pos_num) : pos_num := pos_num.rec_on a (bit0 one) (λn r, bit0 r) (λn r, bit1 n) end pos_num inductive num : Type := | zero : num | pos : pos_num → num namespace num open pos_num definition succ (a : num) : num := num.rec_on a (pos one) (λp, pos (succ p)) end num inductive bool : Type := | ff : bool | tt : bool inductive char : Type := mk : bool → bool → bool → bool → bool → bool → bool → bool → char inductive string : Type := | empty : string | str : char → string → string inductive option (A : Type) : Type := | none {} : option A | some : A → option A -- Remark: we manually generate the nat.rec_on, nat.induction_on, nat.cases_on and nat.no_confusion. -- We do that because we want 0 instead of nat.zero in these eliminators. set_option inductive.rec_on false set_option inductive.cases_on false inductive nat := | zero : nat | succ : nat → nat
a5cf66a3f3d5cf91270ec832f1eb8d690235e48d
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0309.lean
3fc7d67de9f2096fbb76f15ea1a8ab36263939f2
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
172
lean
variables p q : Prop example (h : p ∧ q) : q ∧ p ∧ q := ⟨h.right, ⟨h.left, h.right⟩⟩ example (h : p ∧ q) : q ∧ p ∧ q := ⟨h.right, h.left, h.right⟩
7ad6929bd5f16234d77c21ef04931f49329dd502
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/types/sigma.hlean
d26c73c120e9d922239cffe0dd62de2e40326e5a
[ "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
19,455
hlean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about sigma-types (dependent sums) -/ import types.prod open eq sigma sigma.ops equiv is_equiv function is_trunc sum unit namespace sigma variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} definition destruct := @sigma.cases_on /- Paths in a sigma-type -/ protected definition eta [unfold 3] : Π (u : Σa, B a), ⟨u.1 , u.2⟩ = u | eta ⟨u₁, u₂⟩ := idp definition eta2 : Π (u : Σa b, C a b), ⟨u.1, u.2.1, u.2.2⟩ = u | eta2 ⟨u₁, u₂, u₃⟩ := idp definition eta3 : Π (u : Σa b c, D a b c), ⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩ = u | eta3 ⟨u₁, u₂, u₃, u₄⟩ := idp definition dpair_eq_dpair [unfold 8] (p : a = a') (q : b =[p] b') : ⟨a, b⟩ = ⟨a', b'⟩ := apo011 sigma.mk p q definition sigma_eq [unfold 3 4] (p : u.1 = v.1) (q : u.2 =[p] v.2) : u = v := by induction u; induction v; exact (dpair_eq_dpair p q) definition eq_pr1 [unfold 5] (p : u = v) : u.1 = v.1 := ap pr1 p postfix `..1`:(max+1) := eq_pr1 definition eq_pr2 [unfold 5] (p : u = v) : u.2 =[p..1] v.2 := by induction p; exact idpo postfix `..2`:(max+1) := eq_pr2 definition dpair_sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : ⟨(sigma_eq p q)..1, (sigma_eq p q)..2⟩ = ⟨p, q⟩ := by induction u; induction v;esimp at *;induction q;esimp definition sigma_eq_pr1 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..1 = p := (dpair_sigma_eq p q)..1 definition sigma_eq_pr2 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..2 =[sigma_eq_pr1 p q] q := (dpair_sigma_eq p q)..2 definition sigma_eq_eta (p : u = v) : sigma_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity definition eq2_pr1 {p q : u = v} (r : p = q) : p..1 = q..1 := ap eq_pr1 r definition eq2_pr2 {p q : u = v} (r : p = q) : p..2 =[eq2_pr1 r] q..2 := !pathover_ap (apdo eq_pr2 r) definition tr_pr1_sigma_eq {B' : A → Type} (p : u.1 = v.1) (q : u.2 =[p] v.2) : transport (λx, B' x.1) (sigma_eq p q) = transport B' p := by induction u; induction v; esimp at *;induction q; reflexivity protected definition ap_pr1 (p : u = v) : ap (λx : sigma B, x.1) p = p..1 := idp /- the uncurried version of sigma_eq. We will prove that this is an equivalence -/ definition sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), u = v | sigma_eq_unc ⟨pq₁, pq₂⟩ := sigma_eq pq₁ pq₂ definition dpair_sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), ⟨(sigma_eq_unc pq)..1, (sigma_eq_unc pq)..2⟩ = pq | dpair_sigma_eq_unc ⟨pq₁, pq₂⟩ := dpair_sigma_eq pq₁ pq₂ definition sigma_eq_pr1_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..1 = pq.1 := (dpair_sigma_eq_unc pq)..1 definition sigma_eq_pr2_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..2 =[sigma_eq_pr1_unc pq] pq.2 := (dpair_sigma_eq_unc pq)..2 definition sigma_eq_eta_unc (p : u = v) : sigma_eq_unc ⟨p..1, p..2⟩ = p := sigma_eq_eta p definition tr_sigma_eq_pr1_unc {B' : A → Type} (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : transport (λx, B' x.1) (@sigma_eq_unc A B u v pq) = transport B' pq.1 := destruct pq tr_pr1_sigma_eq definition is_equiv_sigma_eq [instance] (u v : Σa, B a) : is_equiv (@sigma_eq_unc A B u v) := adjointify sigma_eq_unc (λp, ⟨p..1, p..2⟩) sigma_eq_eta_unc dpair_sigma_eq_unc definition sigma_eq_equiv (u v : Σa, B a) : (u = v) ≃ (Σ(p : u.1 = v.1), u.2 =[p] v.2) := (equiv.mk sigma_eq_unc _)⁻¹ᵉ definition dpair_eq_dpair_con (p1 : a = a' ) (q1 : b =[p1] b' ) (p2 : a' = a'') (q2 : b' =[p2] b'') : dpair_eq_dpair (p1 ⬝ p2) (q1 ⬝o q2) = dpair_eq_dpair p1 q1 ⬝ dpair_eq_dpair p2 q2 := by induction q1; induction q2; reflexivity definition sigma_eq_con (p1 : u.1 = v.1) (q1 : u.2 =[p1] v.2) (p2 : v.1 = w.1) (q2 : v.2 =[p2] w.2) : sigma_eq (p1 ⬝ p2) (q1 ⬝o q2) = sigma_eq p1 q1 ⬝ sigma_eq p2 q2 := by induction u; induction v; induction w; apply dpair_eq_dpair_con local attribute dpair_eq_dpair [reducible] definition dpair_eq_dpair_con_idp (p : a = a') (q : b =[p] b') : dpair_eq_dpair p q = dpair_eq_dpair p !pathover_tr ⬝ dpair_eq_dpair idp (pathover_idp_of_eq (tr_eq_of_pathover q)) := by induction q; reflexivity /- eq_pr1 commutes with the groupoid structure. -/ definition eq_pr1_idp (u : Σa, B a) : (refl u) ..1 = refl (u.1) := idp definition eq_pr1_con (p : u = v) (q : v = w) : (p ⬝ q) ..1 = (p..1) ⬝ (q..1) := !ap_con definition eq_pr1_inv (p : u = v) : p⁻¹ ..1 = (p..1)⁻¹ := !ap_inv /- Applying dpair to one argument is the same as dpair_eq_dpair with reflexivity in the first place. -/ definition ap_dpair (q : b₁ = b₂) : ap (sigma.mk a) q = dpair_eq_dpair idp (pathover_idp_of_eq q) := by induction q; reflexivity /- Dependent transport is the same as transport along a sigma_eq. -/ definition transportD_eq_transport (p : a = a') (c : C a b) : p ▸D c = transport (λu, C (u.1) (u.2)) (dpair_eq_dpair p !pathover_tr) c := by induction p; reflexivity definition sigma_eq_eq_sigma_eq {p1 q1 : a = a'} {p2 : b =[p1] b'} {q2 : b =[q1] b'} (r : p1 = q1) (s : p2 =[r] q2) : sigma_eq p1 p2 = sigma_eq q1 q2 := by induction s; reflexivity /- A path between paths in a total space is commonly shown component wise. -/ definition sigma_eq2 {p q : u = v} (r : p..1 = q..1) (s : p..2 =[r] q..2) : p = q := begin induction p, induction u with u1 u2, transitivity sigma_eq q..1 q..2, apply sigma_eq_eq_sigma_eq r s, apply sigma_eq_eta, end definition sigma_eq2_unc {p q : u = v} (rs : Σ(r : p..1 = q..1), p..2 =[r] q..2) : p = q := destruct rs sigma_eq2 definition ap_dpair_eq_dpair (f : Πa, B a → A') (p : a = a') (q : b =[p] b') : ap (sigma.rec f) (dpair_eq_dpair p q) = apo011 f p q := by induction q; reflexivity /- Transport -/ /- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD]. In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/ definition sigma_transport (p : a = a') (bc : Σ(b : B a), C a b) : p ▸ bc = ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; reflexivity /- The special case when the second variable doesn't depend on the first is simpler. -/ definition sigma_transport_nondep {B : Type} {C : A → B → Type} (p : a = a') (bc : Σ(b : B), C a b) : p ▸ bc = ⟨bc.1, p ▸ bc.2⟩ := by induction p; induction bc; reflexivity /- Or if the second variable contains a first component that doesn't depend on the first. -/ definition sigma_transport2_nondep {C : A → Type} {D : Π a:A, B a → C a → Type} (p : a = a') (bcd : Σ(b : B a) (c : C a), D a b c) : p ▸ bcd = ⟨p ▸ bcd.1, p ▸ bcd.2.1, p ▸D2 bcd.2.2⟩ := begin induction p, induction bcd with b cd, induction cd, reflexivity end /- Pathovers -/ definition etao (p : a = a') (bc : Σ(b : B a), C a b) : bc =[p] ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; apply idpo definition sigma_pathover (p : a = a') (u : Σ(b : B a), C a b) (v : Σ(b : B a'), C a' b) (r : u.1 =[p] v.1) (s : u.2 =[apo011 C p r] v.2) : u =[p] v := begin induction u, induction v, esimp at *, induction r, esimp [apo011] at s, induction s using idp_rec_on, apply idpo end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of sigma_pathover is an equivalence -/ /- Functorial action -/ variables (f : A → A') (g : Πa, B a → B' (f a)) definition sigma_functor [unfold 7] (u : Σa, B a) : Σa', B' a' := ⟨f u.1, g u.1 u.2⟩ definition total [reducible] [unfold 5] {B' : A → Type} (g : Πa, B a → B' a) (u : Σa, B a) : Σa', B' a' := sigma_functor id g u /- Equivalences -/ definition is_equiv_sigma_functor [constructor] [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : is_equiv (sigma_functor f g) := adjointify (sigma_functor f g) (sigma_functor f⁻¹ (λ(a' : A') (b' : B' a'), ((g (f⁻¹ a'))⁻¹ (transport B' (right_inv f a')⁻¹ b')))) abstract begin intro u', induction u' with a' b', apply sigma_eq (right_inv f a'), rewrite [▸*,right_inv (g (f⁻¹ a')),▸*], apply tr_pathover end end abstract begin intro u, induction u with a b, apply (sigma_eq (left_inv f a)), apply pathover_of_tr_eq, rewrite [▸*,adj f,-(fn_tr_eq_tr_fn (left_inv f a) (λ a, (g a)⁻¹)), ▸*,tr_compose B' f,tr_inv_tr,left_inv] end end definition sigma_equiv_sigma_of_is_equiv [constructor] [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : (Σa, B a) ≃ (Σa', B' a') := equiv.mk (sigma_functor f g) !is_equiv_sigma_functor definition sigma_equiv_sigma [constructor] (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (to_fun Hf a)) : (Σa, B a) ≃ (Σa', B' a') := sigma_equiv_sigma_of_is_equiv (to_fun Hf) (λ a, to_fun (Hg a)) definition sigma_equiv_sigma_right [constructor] {B' : A → Type} (Hg : Π a, B a ≃ B' a) : (Σa, B a) ≃ Σa, B' a := sigma_equiv_sigma equiv.refl Hg definition sigma_equiv_sigma_left [constructor] (Hf : A ≃ A') : (Σa, B a) ≃ (Σa', B (to_inv Hf a')) := sigma_equiv_sigma Hf (λ a, equiv_ap B !right_inv⁻¹) definition ap_sigma_functor_eq_dpair (p : a = a') (q : b =[p] b') : ap (sigma_functor f g) (sigma_eq p q) = sigma_eq (ap f p) (pathover.rec_on q idpo) := by induction q; reflexivity -- definition ap_sigma_functor_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) -- : ap (sigma_functor f g) (sigma_eq p q) = -- sigma_eq (ap f p) -- ((tr_compose B' f p (g u.1 u.2))⁻¹ ⬝ (fn_tr_eq_tr_fn p g u.2)⁻¹ ⬝ ap (g v.1) q) := -- by induction u; induction v; apply ap_sigma_functor_eq_dpair /- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/ definition is_equiv_pr1 [instance] [constructor] (B : A → Type) [H : Π a, is_contr (B a)] : is_equiv (@pr1 A B) := adjointify pr1 (λa, ⟨a, !center⟩) (λa, idp) (λu, sigma_eq idp (pathover_idp_of_eq !center_eq)) definition sigma_equiv_of_is_contr_right [constructor] [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A := equiv.mk pr1 _ /- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/ definition sigma_equiv_of_is_contr_left [constructor] (B : A → Type) [H : is_contr A] : (Σa, B a) ≃ B (center A) := equiv.MK (λu, (center_eq u.1)⁻¹ ▸ u.2) (λb, ⟨!center, b⟩) abstract (λb, ap (λx, x ▸ b) !prop_eq_of_is_contr) end abstract (λu, sigma_eq !center_eq !tr_pathover) end /- Associativity -/ --this proof is harder than in Coq because we don't have eta definitionally for sigma definition sigma_assoc_equiv [constructor] (C : (Σa, B a) → Type) : (Σa b, C ⟨a, b⟩) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩) (λuc, ⟨uc.1.1, uc.1.2, !sigma.eta⁻¹ ▸ uc.2⟩) abstract begin intro uc, induction uc with u c, induction u, reflexivity end end abstract begin intro av, induction av with a v, induction v, reflexivity end end) open prod prod.ops definition assoc_equiv_prod [constructor] (C : (A × A') → Type) : (Σa a', C (a,a')) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨(av.1, av.2.1), av.2.2⟩) (λuc, ⟨pr₁ (uc.1), pr₂ (uc.1), !prod.eta⁻¹ ▸ uc.2⟩) abstract proof (λuc, destruct uc (λu, prod.destruct u (λa b c, idp))) qed end abstract proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed end) /- Symmetry -/ definition comm_equiv_unc (C : A × A' → Type) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) := calc (Σa a', C (a, a')) ≃ Σu, C u : assoc_equiv_prod ... ≃ Σv, C (flip v) : sigma_equiv_sigma !prod_comm_equiv (λu, prod.destruct u (λa a', equiv.refl)) ... ≃ Σa' a, C (a, a') : assoc_equiv_prod definition sigma_comm_equiv [constructor] (C : A → A' → Type) : (Σa a', C a a') ≃ (Σa' a, C a a') := comm_equiv_unc (λu, C (prod.pr1 u) (prod.pr2 u)) definition equiv_prod [constructor] (A B : Type) : (Σ(a : A), B) ≃ A × B := equiv.mk _ (adjointify (λs, (s.1, s.2)) (λp, ⟨pr₁ p, pr₂ p⟩) proof (λp, prod.destruct p (λa b, idp)) qed proof (λs, destruct s (λa b, idp)) qed) definition comm_equiv_nondep (A B : Type) : (Σ(a : A), B) ≃ Σ(b : B), A := calc (Σ(a : A), B) ≃ A × B : equiv_prod ... ≃ B × A : prod_comm_equiv ... ≃ Σ(b : B), A : equiv_prod definition sigma_assoc_comm_equiv {A : Type} (B C : A → Type) : (Σ(v : Σa, B a), C v.1) ≃ (Σ(u : Σa, C a), B u.1) := calc (Σ(v : Σa, B a), C v.1) ≃ (Σa (b : B a), C a) : !sigma_assoc_equiv⁻¹ᵉ ... ≃ (Σa, B a × C a) : sigma_equiv_sigma_right (λa, !equiv_prod) ... ≃ (Σa, C a × B a) : sigma_equiv_sigma_right (λa, !prod_comm_equiv) ... ≃ (Σa (c : C a), B a) : sigma_equiv_sigma_right (λa, !equiv_prod) ... ≃ (Σ(u : Σa, C a), B u.1) : sigma_assoc_equiv /- Interaction with other type constructors -/ definition sigma_empty_left [constructor] (B : empty → Type) : (Σx, B x) ≃ empty := begin fapply equiv.MK, { intro v, induction v, contradiction}, { intro x, contradiction}, { intro x, contradiction}, { intro v, induction v, contradiction}, end definition sigma_empty_right [constructor] (A : Type) : (Σ(a : A), empty) ≃ empty := begin fapply equiv.MK, { intro v, induction v, contradiction}, { intro x, contradiction}, { intro x, contradiction}, { intro v, induction v, contradiction}, end definition sigma_unit_left [constructor] (B : unit → Type) : (Σx, B x) ≃ B star := !sigma_equiv_of_is_contr_left definition sigma_unit_right [constructor] (A : Type) : (Σ(a : A), unit) ≃ A := !sigma_equiv_of_is_contr_right definition sigma_sum_left [constructor] (B : A + A' → Type) : (Σp, B p) ≃ (Σa, B (inl a)) + (Σa, B (inr a)) := begin fapply equiv.MK, { intro v, induction v with p b, induction p: append (apply inl) (apply inr); constructor; assumption }, { intro p, induction p with v v: induction v; constructor; assumption}, { intro p, induction p with v v: induction v; reflexivity}, { intro v, induction v with p b, induction p: reflexivity}, end definition sigma_sum_right [constructor] (B C : A → Type) : (Σa, B a + C a) ≃ (Σa, B a) + (Σa, C a) := begin fapply equiv.MK, { intro v, induction v with a p, induction p: append (apply inl) (apply inr); constructor; assumption}, { intro p, induction p with v v: induction v; constructor; append (apply inl) (apply inr); assumption}, { intro p, induction p with v v: induction v; reflexivity}, { intro v, induction v with a p, induction p: reflexivity}, end /- ** Universal mapping properties -/ /- *** The positive universal property. -/ section definition is_equiv_sigma_rec [instance] (C : (Σa, B a) → Type) : is_equiv (sigma.rec : (Πa b, C ⟨a, b⟩) → Πab, C ab) := adjointify _ (λ g a b, g ⟨a, b⟩) (λ g, proof eq_of_homotopy (λu, destruct u (λa b, idp)) qed) (λ f, refl f) definition equiv_sigma_rec (C : (Σa, B a) → Type) : (Π(a : A) (b: B a), C ⟨a, b⟩) ≃ (Πxy, C xy) := equiv.mk sigma.rec _ /- *** The negative universal property. -/ protected definition coind_unc (fg : Σ(f : Πa, B a), Πa, C a (f a)) (a : A) : Σ(b : B a), C a b := ⟨fg.1 a, fg.2 a⟩ protected definition coind (f : Π a, B a) (g : Π a, C a (f a)) (a : A) : Σ(b : B a), C a b := sigma.coind_unc ⟨f, g⟩ a --is the instance below dangerous? --in Coq this can be done without function extensionality definition is_equiv_coind [instance] (C : Πa, B a → Type) : is_equiv (@sigma.coind_unc _ _ C) := adjointify _ (λ h, ⟨λa, (h a).1, λa, (h a).2⟩) (λ h, proof eq_of_homotopy (λu, !sigma.eta) qed) (λfg, destruct fg (λ(f : Π (a : A), B a) (g : Π (x : A), C x (f x)), proof idp qed)) definition sigma_pi_equiv_pi_sigma : (Σ(f : Πa, B a), Πa, C a (f a)) ≃ (Πa, Σb, C a b) := equiv.mk sigma.coind_unc _ end /- Subtypes (sigma types whose second components are props) -/ definition subtype [reducible] {A : Type} (P : A → Type) [H : Πa, is_prop (P a)] := Σ(a : A), P a notation [parsing_only] `{` binder `|` r:(scoped:1 P, subtype P) `}` := r /- To prove equality in a subtype, we only need equality of the first component. -/ definition subtype_eq [H : Πa, is_prop (B a)] {u v : {a | B a}} : u.1 = v.1 → u = v := sigma_eq_unc ∘ inv pr1 definition is_equiv_subtype_eq [H : Πa, is_prop (B a)] (u v : {a | B a}) : is_equiv (subtype_eq : u.1 = v.1 → u = v) := !is_equiv_compose local attribute is_equiv_subtype_eq [instance] definition equiv_subtype [H : Πa, is_prop (B a)] (u v : {a | B a}) : (u.1 = v.1) ≃ (u = v) := equiv.mk !subtype_eq _ definition subtype_eq_inv {A : Type} {B : A → Type} [H : Πa, is_prop (B a)] (u v : Σa, B a) : u = v → u.1 = v.1 := subtype_eq⁻¹ᶠ local attribute subtype_eq_inv [reducible] definition is_equiv_subtype_eq_inv {A : Type} {B : A → Type} [H : Πa, is_prop (B a)] (u v : Σa, B a) : is_equiv (subtype_eq_inv u v) := _ /- truncatedness -/ theorem is_trunc_sigma (B : A → Type) (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)] : is_trunc n (Σa, B a) := begin revert A B HA HB, induction n with n IH, { intro A B HA HB, fapply is_trunc_equiv_closed_rev, apply sigma_equiv_of_is_contr_left}, { intro A B HA HB, apply is_trunc_succ_intro, intro u v, apply is_trunc_equiv_closed_rev, apply sigma_eq_equiv, exact IH _ _ _ _} end theorem is_trunc_subtype (B : A → Prop) (n : trunc_index) [HA : is_trunc (n.+1) A] : is_trunc (n.+1) (Σa, B a) := @(is_trunc_sigma B (n.+1)) _ (λa, !is_trunc_succ_of_is_prop) end sigma attribute sigma.is_trunc_sigma [instance] [priority 1490] attribute sigma.is_trunc_subtype [instance] [priority 1200]
e1cf6a9eb7eea6d6c67f23fbaab94df341a35226
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Elab/PreDefinition/WF/Main.lean
e4ad73afe770186cc9f80b94335ef02530b1907d
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
5,553
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.WF.TerminationHint import Lean.Elab.PreDefinition.WF.PackDomain import Lean.Elab.PreDefinition.WF.PackMutual import Lean.Elab.PreDefinition.WF.Rel import Lean.Elab.PreDefinition.WF.Fix import Lean.Elab.PreDefinition.WF.Eqns import Lean.Elab.PreDefinition.WF.Ite namespace Lean.Elab open WF open Meta private def isOnlyOneUnaryDef (preDefs : Array PreDefinition) (fixedPrefixSize : Nat) : MetaM Bool := do if preDefs.size == 1 then lambdaTelescope preDefs[0]!.value fun xs _ => return xs.size == fixedPrefixSize + 1 else return false private partial def addNonRecPreDefs (preDefs : Array PreDefinition) (preDefNonRec : PreDefinition) (fixedPrefixSize : Nat) : TermElabM Unit := do if (← isOnlyOneUnaryDef preDefs fixedPrefixSize) then return () let us := preDefNonRec.levelParams.map mkLevelParam let all := preDefs.toList.map (·.declName) for fidx in [:preDefs.size] do let preDef := preDefs[fidx]! let value ← lambdaTelescope preDef.value fun xs _ => do let packedArgs : Array Expr := xs[fixedPrefixSize:] let mkProd (type : Expr) : MetaM Expr := do mkUnaryArg type packedArgs let rec mkSum (i : Nat) (type : Expr) : MetaM Expr := do if i == preDefs.size - 1 then mkProd type else (← whnfD type).withApp fun f args => do assert! args.size == 2 if i == fidx then return mkApp3 (mkConst ``PSum.inl f.constLevels!) args[0]! args[1]! (← mkProd args[0]!) else let r ← mkSum (i+1) args[1]! return mkApp3 (mkConst ``PSum.inr f.constLevels!) args[0]! args[1]! r let Expr.forallE _ domain _ _ := (← instantiateForall preDefNonRec.type xs[:fixedPrefixSize]) | unreachable! let arg ← mkSum 0 domain mkLambdaFVars xs (mkApp (mkAppN (mkConst preDefNonRec.declName us) xs[:fixedPrefixSize]) arg) trace[Elab.definition.wf] "{preDef.declName} := {value}" addNonRec { preDef with value } (applyAttrAfterCompilation := false) (all := all) partial def withCommonTelescope (preDefs : Array PreDefinition) (k : Array Expr → Array Expr → TermElabM α) : TermElabM α := go #[] (preDefs.map (·.value)) where go (fvars : Array Expr) (vals : Array Expr) : TermElabM α := do if !(vals.all fun val => val.isLambda) then k fvars vals else if !(← vals.allM fun val => return val.bindingName! == vals[0]!.bindingName! && val.binderInfo == vals[0]!.binderInfo && (← isDefEq val.bindingDomain! vals[0]!.bindingDomain!)) then k fvars vals else withLocalDecl vals[0]!.bindingName! vals[0]!.binderInfo vals[0]!.bindingDomain! fun x => go (fvars.push x) (vals.map fun val => val.bindingBody!.instantiate1 x) def getFixedPrefix (preDefs : Array PreDefinition) : TermElabM Nat := withCommonTelescope preDefs fun xs vals => do let resultRef ← IO.mkRef xs.size for val in vals do if (← resultRef.get) == 0 then return 0 forEachExpr' val fun e => do if preDefs.any fun preDef => e.isAppOf preDef.declName then let args := e.getAppArgs resultRef.modify (min args.size ·) for arg in args, x in xs do if !(← withoutProofIrrelevance <| withReducible <| isDefEq arg x) then -- We continue searching if e's arguments are not a prefix of `xs` return true return false else return true resultRef.get def wfRecursion (preDefs : Array PreDefinition) (wf? : Option TerminationWF) (decrTactic? : Option Syntax) : TermElabM Unit := do let (unaryPreDef, fixedPrefixSize) ← withoutModifyingEnv do for preDef in preDefs do addAsAxiom preDef let fixedPrefixSize ← getFixedPrefix preDefs trace[Elab.definition.wf] "fixed prefix: {fixedPrefixSize}" let preDefsDIte ← preDefs.mapM fun preDef => return { preDef with value := (← iteToDIte preDef.value) } let unaryPreDefs ← packDomain fixedPrefixSize preDefsDIte return (← packMutual fixedPrefixSize preDefs unaryPreDefs, fixedPrefixSize) let preDefNonRec ← forallBoundedTelescope unaryPreDef.type fixedPrefixSize fun prefixArgs type => do let type ← whnfForall type let packedArgType := type.bindingDomain! elabWFRel preDefs unaryPreDef.declName fixedPrefixSize packedArgType wf? fun wfRel => do trace[Elab.definition.wf] "wfRel: {wfRel}" let (value, envNew) ← withoutModifyingEnv' do addAsAxiom unaryPreDef let value ← mkFix unaryPreDef prefixArgs wfRel decrTactic? eraseRecAppSyntaxExpr value /- `mkFix` invokes `decreasing_tactic` which may add auxiliary theorems to the environment. -/ let value ← unfoldDeclsFrom envNew value return { unaryPreDef with value } trace[Elab.definition.wf] ">> {preDefNonRec.declName} :=\n{preDefNonRec.value}" let preDefs ← preDefs.mapM fun d => eraseRecAppSyntax d addNonRec preDefNonRec (applyAttrAfterCompilation := false) addNonRecPreDefs preDefs preDefNonRec fixedPrefixSize registerEqnsInfo preDefs preDefNonRec.declName fixedPrefixSize for preDef in preDefs do applyAttributesOf #[preDef] AttributeApplicationTime.afterCompilation addAndCompilePartialRec preDefs builtin_initialize registerTraceClass `Elab.definition.wf end Lean.Elab
6ea51be3e3bb1a362f410985c83fabb38600d24d
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/exercise_7_10_3.lean
c82501d5c022e8ebbf6a55e2588dbb4d09b33eeb
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
555
lean
inductive Expr : Type | Const : ℕ → Expr | Var : ℕ → Expr | Plus : Expr → Expr → Expr | Times : Expr → Expr → Expr open Expr def Env : Type := ℕ → ℕ def eval (env : Env) (e: Expr) : ℕ := Expr.rec_on e (λ n, n) (λ n, env n) (λ _ _ v1 v2, v1 + v2) (λ _ _ v1 v2, v1 * v2) def env : Env := λ n, n lemma L1 : eval env (Const 45) = 45 := rfl lemma L2 : eval env (Var 34) = 34 := rfl lemma L3 : eval env (Plus (Const 5) (Var 6)) = 11 := rfl lemma L4 : eval env (Times (Const 5) (Const 6)) = 30 := rfl
4d3418e88d729c2140f4433e791180219e544955
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex31.lean
37314ab5a6e3b980ab7dbb31e3b8ccc34b42391d
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
338
lean
variables p q r s : Prop theorem t2 : (q → r) → (p → q) → p → r := assume h1 : q → r, assume h2 : p → q, assume h3 : p, show r, from h1 (h2 h3) theorem t2' : (q → r) → (p → q) → p → r := λ (h1 : q → r) (h2 : p → q) (h3 : p), h1 (h2 h3) theorem t2'' (h1 : q → r) (h2 : p → q) (h3 : p) : r := h1 (h2 h3)
9bcadfc052e1e82b5b89f56e124a1b63a16b2705
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/elab_meta2.lean
a8484d3cdda67f7f453f679e2228968fb89aa8df
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
883
lean
print "parametric meta definition" meta definition f {A : Type} : nat → A → A → A | n a b := if n / 2 = 0 then a else f (n / 2) b a vm_eval if f 10 1 2 = 2 then "OK" else "FAILED" namespace foo print "parametric meta definition inside namespace" meta definition bla {A : Type} : nat → A → A → A | n a b := if n / 2 = 0 then a else bla (n / 2) b a vm_eval if foo.bla 10 1 2 = 2 then "OK" else "FAILED" end foo namespace foo section print "meta definition inside parametric scope" parameter {A : Type} meta definition bah : nat → A → A → A | n a b := if n / 2 = 0 then a else bah (n / 2) b a end vm_eval if foo.bah 10 1 2 = 2 then "OK" else "FAILED" end foo print "private meta definition" private meta definition hprv {A : Type} : nat → A → A → A | n a b := if n / 2 = 0 then a else hprv (n / 2) b a vm_eval if hprv 10 1 2 = 2 then "OK" else "FAILED"
d434cc6c9fb61b33fac7cbef5e894d87d6a7755c
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/determinant.lean
e55c28244a4b9a3221b4a544d431a94cad3fc6e7
[ "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
12,463
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.free_module_pid import linear_algebra.matrix.basis import linear_algebra.matrix.diagonal import linear_algebra.matrix.to_linear_equiv import linear_algebra.matrix.reindex import linear_algebra.multilinear import linear_algebra.dual import ring_theory.algebra_tower import ring_theory.matrix_algebra /-! # Determinant of families of vectors This file defines the determinant of an endomorphism, and of a family of vectors with respect to some basis. For the determinant of a matrix, see the file `linear_algebra.matrix.determinant`. ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite types used for indexing. * `basis.det`: the determinant of a family of vectors with respect to a basis, as a multilinear map * `linear_map.det`: the determinant of an endomorphism `f : End R M` as a multiplicative homomorphism (if `M` does not have a finite `R`-basis, the result is `1` instead) ## Tags basis, det, determinant -/ noncomputable theory open_locale big_operators open_locale matrix open linear_map open submodule universes u v w open linear_map matrix variables {R : Type*} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables {M' : Type*} [add_comm_group M'] [module R M'] variables {ι : Type*} [decidable_eq ι] [fintype ι] variables (e : basis ι R M) section conjugate variables {A : Type*} [integral_domain A] variables {m n : Type*} [fintype m] [fintype n] /-- If `R^m` and `R^n` are linearly equivalent, then `m` and `n` are also equivalent. -/ def equiv_of_pi_lequiv_pi {R : Type*} [integral_domain R] (e : (m → R) ≃ₗ[R] (n → R)) : m ≃ n := basis.index_equiv (basis.of_equiv_fun e.symm) (pi.basis_fun _ _) /-- If `M` and `M'` are each other's inverse matrices, they are square matrices up to equivalence of types. -/ def matrix.index_equiv_of_inv [decidable_eq m] [decidable_eq n] {M : matrix m n A} {M' : matrix n m A} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : m ≃ n := equiv_of_pi_lequiv_pi (matrix.to_lin'_of_inv hMM' hM'M) /-- If `M'` is a two-sided inverse for `M` (indexed differently), `det (M ⬝ N ⬝ M') = det N`. -/ lemma matrix.det_conj [decidable_eq m] [decidable_eq n] {M : matrix m n A} {M' : matrix n m A} {N : matrix n n A} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : det (M ⬝ N ⬝ M') = det N := begin -- Although `m` and `n` are different a priori, we will show they have the same cardinality. -- This turns the problem into one for square matrices (`matrix.det_units_conj`), which is easy. let e : m ≃ n := matrix.index_equiv_of_inv hMM' hM'M, let U : units (matrix n n A) := ⟨M.minor e.symm (equiv.refl _), M'.minor (equiv.refl _) e.symm, by rw [mul_eq_mul, ←minor_mul_equiv, hMM', minor_one_equiv], by rw [mul_eq_mul, ←minor_mul_equiv, hM'M, minor_one_equiv]⟩, rw [← matrix.det_units_conj U N, ← det_minor_equiv_self e.symm], simp only [minor_mul_equiv _ _ _ (equiv.refl n) _, equiv.coe_refl, minor_id_id, units.coe_mk, units.inv_mk] end end conjugate namespace linear_map /-! ### Determinant of a linear map -/ variables {A : Type*} [integral_domain A] [module A M] variables {κ : Type*} [fintype κ] /-- The determinant of `linear_map.to_matrix` does not depend on the choice of basis. -/ lemma det_to_matrix_eq_det_to_matrix [decidable_eq κ] (b : basis ι A M) (c : basis κ A M) (f : M →ₗ[A] M) : det (linear_map.to_matrix b b f) = det (linear_map.to_matrix c c f) := by rw [← linear_map_to_matrix_mul_basis_to_matrix c b c, ← basis_to_matrix_mul_linear_map_to_matrix b c b, matrix.det_conj]; rw [basis.to_matrix_mul_to_matrix, basis.to_matrix_self] /-- The determinant of an endomorphism given a basis. See `linear_map.det` for a version that populates the basis non-computably. Although the `trunc (basis ι A M)` parameter makes it slightly more convenient to switch bases, there is no good way to generalize over universe parameters, so we can't fully state in `det_aux`'s type that it does not depend on the choice of basis. Instead you can use the `det_aux_def'` lemma, or avoid mentioning a basis at all using `linear_map.det`. -/ def det_aux : trunc (basis ι A M) → (M →ₗ[A] M) →* A := trunc.lift (λ b : basis ι A M, (monoid_hom.of (matrix.det)).comp (to_matrix_alg_equiv b : (M →ₗ[A] M) →* matrix ι ι A)) (λ b c, monoid_hom.ext $ det_to_matrix_eq_det_to_matrix b c) /-- Unfold lemma for `det_aux`. See also `det_aux_def'` which allows you to vary the basis. -/ lemma det_aux_def (b : basis ι A M) (f : M →ₗ[A] M) : linear_map.det_aux (trunc.mk b) f = matrix.det (linear_map.to_matrix b b f) := rfl -- Discourage the elaborator from unfolding `det_aux` and producing a huge term. attribute [irreducible] linear_map.det_aux lemma det_aux_def' {ι' : Type*} [fintype ι'] [decidable_eq ι'] (tb : trunc $ basis ι A M) (b' : basis ι' A M) (f : M →ₗ[A] M) : linear_map.det_aux tb f = matrix.det (linear_map.to_matrix b' b' f) := by { apply trunc.induction_on tb, intro b, rw [det_aux_def, det_to_matrix_eq_det_to_matrix b b'] } @[simp] lemma det_aux_id (b : trunc $ basis ι A M) : linear_map.det_aux b (linear_map.id) = 1 := (linear_map.det_aux b).map_one @[simp] lemma det_aux_comp (b : trunc $ basis ι A M) (f g : M →ₗ[A] M) : linear_map.det_aux b (f.comp g) = linear_map.det_aux b f * linear_map.det_aux b g := (linear_map.det_aux b).map_mul f g section open_locale classical /-- The determinant of an endomorphism independent of basis. If there is no finite basis on `M`, the result is `1` instead. -/ protected def det : (M →ₗ[A] M) →* A := if H : ∃ (s : finset M), nonempty (basis s A M) then linear_map.det_aux (trunc.mk H.some_spec.some) else 1 lemma coe_det [decidable_eq M] : ⇑(linear_map.det : (M →ₗ[A] M) →* A) = if H : ∃ (s : finset M), nonempty (basis s A M) then linear_map.det_aux (trunc.mk H.some_spec.some) else 1 := by { ext, unfold linear_map.det, split_ifs, { congr }, -- use the correct `decidable_eq` instance refl } end -- Discourage the elaborator from unfolding `det` and producing a huge term. attribute [irreducible] linear_map.det -- Auxiliary lemma, the `simp` normal form goes in the other direction -- (using `linear_map.det_to_matrix`) lemma det_eq_det_to_matrix_of_finset [decidable_eq M] {s : finset M} (b : basis s A M) (f : M →ₗ[A] M) : f.det = matrix.det (linear_map.to_matrix b b f) := have ∃ (s : finset M), nonempty (basis s A M), from ⟨s, ⟨b⟩⟩, by rw [linear_map.coe_det, dif_pos, det_aux_def' _ b]; assumption @[simp] lemma det_to_matrix (b : basis ι A M) (f : M →ₗ[A] M) : matrix.det (to_matrix b b f) = f.det := by { haveI := classical.dec_eq M, rw [det_eq_det_to_matrix_of_finset b.reindex_finset_range, det_to_matrix_eq_det_to_matrix b] } /-- To show `P f.det` it suffices to consider `P (to_matrix _ _ f).det` and `P 1`. -/ @[elab_as_eliminator] lemma det_cases [decidable_eq M] {P : A → Prop} (f : M →ₗ[A] M) (hb : ∀ (s : finset M) (b : basis s A M), P (to_matrix b b f).det) (h1 : P 1) : P f.det := begin unfold linear_map.det, split_ifs with h, { convert hb _ h.some_spec.some, apply det_aux_def' }, { exact h1 } end @[simp] lemma det_comp (f g : M →ₗ[A] M) : (f.comp g).det = f.det * g.det := linear_map.det.map_mul f g @[simp] lemma det_id : (linear_map.id : M →ₗ[A] M).det = 1 := linear_map.det.map_one lemma det_zero {ι : Type*} [fintype ι] [nonempty ι] (b : basis ι A M) : linear_map.det (0 : M →ₗ[A] M) = 0 := by { haveI := classical.dec_eq ι, rw [← det_to_matrix b, linear_equiv.map_zero, det_zero], assumption } end linear_map -- Cannot be stated using `linear_map.det` because `f` is not an endomorphism. lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (v : basis ι R M) (v' : basis ι R M') : is_unit (linear_map.to_matrix v v' f).det := begin apply is_unit_det_of_left_inverse, simpa using (linear_map.to_matrix_comp v v' v f.symm f).symm end /-- Specialization of `linear_equiv.is_unit_det` -/ lemma linear_equiv.is_unit_det' {A : Type*} [integral_domain A] [module A M] (f : M ≃ₗ[A] M) : is_unit (linear_map.det (f : M →ₗ[A] M)) := by haveI := classical.dec_eq M; exact (f : M →ₗ[A] M).det_cases (λ s b, f.is_unit_det _ _) is_unit_one /-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/ @[simps] def linear_equiv.of_is_unit_det {f : M →ₗ[R] M'} {v : basis ι R M} {v' : basis ι R M'} (h : is_unit (linear_map.to_matrix v v' f).det) : M ≃ₗ[R] M' := { to_fun := f, map_add' := f.map_add, map_smul' := f.map_smul, inv_fun := to_lin v' v (to_matrix v v' f)⁻¹, left_inv := λ x, calc to_lin v' v (to_matrix v v' f)⁻¹ (f x) = to_lin v v ((to_matrix v v' f)⁻¹ ⬝ to_matrix v v' f) x : by { rw [to_lin_mul v v' v, to_lin_to_matrix, linear_map.comp_apply] } ... = x : by simp [h], right_inv := λ x, calc f (to_lin v' v (to_matrix v v' f)⁻¹ x) = to_lin v' v' (to_matrix v v' f ⬝ (to_matrix v v' f)⁻¹) x : by { rw [to_lin_mul v' v v', linear_map.comp_apply, to_lin_to_matrix v v'] } ... = x : by simp [h] } /-- The determinant of a family of vectors with respect to some basis, as an alternating multilinear map. -/ def basis.det : alternating_map R M R ι := { to_fun := λ v, det (e.to_matrix v), map_add' := begin intros v i x y, simp only [e.to_matrix_update, linear_equiv.map_add], apply det_update_column_add end, map_smul' := begin intros u i c x, simp only [e.to_matrix_update, algebra.id.smul_eq_mul, linear_equiv.map_smul], apply det_update_column_smul end, map_eq_zero_of_eq' := begin intros v i j h hij, rw [←function.update_eq_self i v, h, ←det_transpose, e.to_matrix_update, ←update_row_transpose, ←e.to_matrix_transpose_apply], apply det_zero_of_row_eq hij, rw [update_row_ne hij.symm, update_row_self], end } lemma basis.det_apply (v : ι → M) : e.det v = det (e.to_matrix v) := rfl lemma basis.det_self : e.det e = 1 := by simp [e.det_apply] lemma is_basis_iff_det {v : ι → M} : linear_independent R v ∧ span R (set.range v) = ⊤ ↔ is_unit (e.det v) := begin split, { rintro ⟨hli, hspan⟩, set v' := basis.mk hli hspan with v'_eq, rw e.det_apply, convert linear_equiv.is_unit_det (linear_equiv.refl _ _) v' e using 2, ext i j, simp }, { intro h, rw [basis.det_apply, basis.to_matrix_eq_to_matrix_constr] at h, set v' := basis.map e (linear_equiv.of_is_unit_det h) with v'_def, have : ⇑ v' = v, { ext i, rw [v'_def, basis.map_apply, linear_equiv.of_is_unit_det_apply, e.constr_basis] }, rw ← this, exact ⟨v'.linear_independent, v'.span_eq⟩ }, end lemma basis.is_unit_det (e' : basis ι R M) : is_unit (e.det e') := (is_basis_iff_det e).mp ⟨e'.linear_independent, e'.span_eq⟩ variables {A : Type*} [integral_domain A] [module A M] @[simp] lemma basis.det_comp (e : basis ι A M) (f : M →ₗ[A] M) (v : ι → M) : e.det (f ∘ v) = f.det * e.det v := by { rw [basis.det_apply, basis.det_apply, ← f.det_to_matrix e, ← matrix.det_mul, e.to_matrix_eq_to_matrix_constr (f ∘ v), e.to_matrix_eq_to_matrix_constr v, ← to_matrix_comp, e.constr_comp] } lemma basis.det_reindex {ι' : Type*} [fintype ι'] [decidable_eq ι'] (b : basis ι R M) (v : ι' → M) (e : ι ≃ ι') : (b.reindex e).det v = b.det (v ∘ e) := by rw [basis.det_apply, basis.to_matrix_reindex', det_reindex_alg_equiv, basis.det_apply] lemma basis.det_reindex_symm {ι' : Type*} [fintype ι'] [decidable_eq ι'] (b : basis ι R M) (v : ι → M) (e : ι' ≃ ι) : (b.reindex e.symm).det (v ∘ e) = b.det v := by rw [basis.det_reindex, function.comp.assoc, e.self_comp_symm, function.comp.right_id] @[simp] lemma basis.det_map (b : basis ι R M) (f : M ≃ₗ[R] M') (v : ι → M') : (b.map f).det v = b.det (f.symm ∘ v) := by { rw [basis.det_apply, basis.to_matrix_map, basis.det_apply] }
159ea94af633428575a11d439443b109b00529fa
9dc8cecdf3c4634764a18254e94d43da07142918
/src/field_theory/normal.lean
db49716d7fbc2044ead51a9ba378c390ca2ce174
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
20,908
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Thomas Browning, Patrick Lutz -/ import field_theory.adjoin import field_theory.tower import group_theory.solvable import ring_theory.power_basis /-! # Normal field extensions In this file we define normal field extensions and prove that for a finite extension, being normal is the same as being a splitting field (`normal.of_is_splitting_field` and `normal.exists_is_splitting_field`). ## Main Definitions - `normal F K` where `K` is a field extension of `F`. -/ noncomputable theory open_locale big_operators open_locale classical polynomial open polynomial is_scalar_tower variables (F K : Type*) [field F] [field K] [algebra F K] /-- Typeclass for normal field extension: `K` is a normal extension of `F` iff the minimal polynomial of every element `x` in `K` splits in `K`, i.e. every conjugate of `x` is in `K`. -/ class normal : Prop := (is_algebraic' : algebra.is_algebraic F K) (splits' (x : K) : splits (algebra_map F K) (minpoly F x)) variables {F K} theorem normal.is_algebraic (h : normal F K) (x : K) : is_algebraic F x := normal.is_algebraic' x theorem normal.is_integral (h : normal F K) (x : K) : is_integral F x := is_algebraic_iff_is_integral.mp (h.is_algebraic x) theorem normal.splits (h : normal F K) (x : K) : splits (algebra_map F K) (minpoly F x) := normal.splits' x theorem normal_iff : normal F K ↔ ∀ x : K, is_integral F x ∧ splits (algebra_map F K) (minpoly F x) := ⟨λ h x, ⟨h.is_integral x, h.splits x⟩, λ h, ⟨λ x, (h x).1.is_algebraic F, λ x, (h x).2⟩⟩ theorem normal.out : normal F K → ∀ x : K, is_integral F x ∧ splits (algebra_map F K) (minpoly F x) := normal_iff.1 variables (F K) instance normal_self : normal F F := ⟨λ x, is_integral_algebra_map.is_algebraic F, λ x, (minpoly.eq_X_sub_C' x).symm ▸ splits_X_sub_C _⟩ variables {K} variables (K) theorem normal.exists_is_splitting_field [h : normal F K] [finite_dimensional F K] : ∃ p : F[X], is_splitting_field F K p := begin let s := basis.of_vector_space F K, refine ⟨∏ x, minpoly F (s x), splits_prod _ $ λ x hx, h.splits (s x), subalgebra.to_submodule_injective _⟩, rw [algebra.top_to_submodule, eq_top_iff, ← s.span_eq, submodule.span_le, set.range_subset_iff], refine λ x, algebra.subset_adjoin (multiset.mem_to_finset.mpr $ (mem_roots $ mt (polynomial.map_eq_zero $ algebra_map F K).1 $ finset.prod_ne_zero_iff.2 $ λ x hx, _).2 _), { exact minpoly.ne_zero (h.is_integral (s x)) }, rw [is_root.def, eval_map, ← aeval_def, alg_hom.map_prod], exact finset.prod_eq_zero (finset.mem_univ _) (minpoly.aeval _ _) end section normal_tower variables (E : Type*) [field E] [algebra F E] [algebra K E] [is_scalar_tower F K E] lemma normal.tower_top_of_normal [h : normal F E] : normal K E := normal_iff.2 $ λ x, begin cases h.out x with hx hhx, rw algebra_map_eq F K E at hhx, exact ⟨is_integral_of_is_scalar_tower x hx, polynomial.splits_of_splits_of_dvd (algebra_map K E) (polynomial.map_ne_zero (minpoly.ne_zero hx)) ((polynomial.splits_map_iff (algebra_map F K) (algebra_map K E)).mpr hhx) (minpoly.dvd_map_of_is_scalar_tower F K x)⟩, end lemma alg_hom.normal_bijective [h : normal F E] (ϕ : E →ₐ[F] K) : function.bijective ϕ := ⟨ϕ.to_ring_hom.injective, λ x, by { letI : algebra E K := ϕ.to_ring_hom.to_algebra, obtain ⟨h1, h2⟩ := h.out (algebra_map K E x), cases minpoly.mem_range_of_degree_eq_one E x (h2.def.resolve_left (minpoly.ne_zero h1) (minpoly.irreducible (is_integral_of_is_scalar_tower x ((is_integral_algebra_map_iff (algebra_map K E).injective).mp h1))) (minpoly.dvd E x ((algebra_map K E).injective (by { rw [ring_hom.map_zero, aeval_map, ←is_scalar_tower.to_alg_hom_apply F K E, ←alg_hom.comp_apply, ←aeval_alg_hom], exact minpoly.aeval F (algebra_map K E x) })))) with y hy, exact ⟨y, hy⟩ }⟩ variables {F} {E} {E' : Type*} [field E'] [algebra F E'] lemma normal.of_alg_equiv [h : normal F E] (f : E ≃ₐ[F] E') : normal F E' := normal_iff.2 $ λ x, begin cases h.out (f.symm x) with hx hhx, have H := is_integral_alg_hom f.to_alg_hom hx, rw [alg_equiv.to_alg_hom_eq_coe, alg_equiv.coe_alg_hom, alg_equiv.apply_symm_apply] at H, use H, apply polynomial.splits_of_splits_of_dvd (algebra_map F E') (minpoly.ne_zero hx), { rw ← alg_hom.comp_algebra_map f.to_alg_hom, exact polynomial.splits_comp_of_splits (algebra_map F E) f.to_alg_hom.to_ring_hom hhx }, { apply minpoly.dvd _ _, rw ← add_equiv.map_eq_zero_iff f.symm.to_add_equiv, exact eq.trans (polynomial.aeval_alg_hom_apply f.symm.to_alg_hom x (minpoly F (f.symm x))).symm (minpoly.aeval _ _) }, end lemma alg_equiv.transfer_normal (f : E ≃ₐ[F] E') : normal F E ↔ normal F E' := ⟨λ h, by exactI normal.of_alg_equiv f, λ h, by exactI normal.of_alg_equiv f.symm⟩ lemma normal.of_is_splitting_field (p : F[X]) [hFEp : is_splitting_field F E p] : normal F E := begin by_cases hp : p = 0, { haveI : is_splitting_field F F p, { rw hp, exact ⟨splits_zero _, subsingleton.elim _ _⟩ }, exactI (alg_equiv.transfer_normal ((is_splitting_field.alg_equiv F p).trans (is_splitting_field.alg_equiv E p).symm)).mp (normal_self F) }, refine normal_iff.2 (λ x, _), haveI hFE : finite_dimensional F E := is_splitting_field.finite_dimensional E p, have Hx : is_integral F x := is_integral_of_noetherian (is_noetherian.iff_fg.2 hFE) x, refine ⟨Hx, or.inr _⟩, rintros q q_irred ⟨r, hr⟩, let D := adjoin_root q, haveI := fact.mk q_irred, let pbED := adjoin_root.power_basis q_irred.ne_zero, haveI : finite_dimensional E D := power_basis.finite_dimensional pbED, have finrankED : finite_dimensional.finrank E D = q.nat_degree := power_basis.finrank pbED, letI : algebra F D := ring_hom.to_algebra ((algebra_map E D).comp (algebra_map F E)), haveI : is_scalar_tower F E D := of_algebra_map_eq (λ _, rfl), haveI : finite_dimensional F D := finite_dimensional.trans F E D, suffices : nonempty (D →ₐ[F] E), { cases this with ϕ, rw [←with_bot.coe_one, degree_eq_iff_nat_degree_eq q_irred.ne_zero, ←finrankED], have nat_lemma : ∀ a b c : ℕ, a * b = c → c ≤ a → 0 < c → b = 1, { intros a b c h1 h2 h3, nlinarith }, exact nat_lemma _ _ _ (finite_dimensional.finrank_mul_finrank F E D) (linear_map.finrank_le_finrank_of_injective (show function.injective ϕ.to_linear_map, from ϕ.to_ring_hom.injective)) finite_dimensional.finrank_pos, }, let C := adjoin_root (minpoly F x), haveI Hx_irred := fact.mk (minpoly.irreducible Hx), letI : algebra C D := ring_hom.to_algebra (adjoin_root.lift (algebra_map F D) (adjoin_root.root q) (by rw [algebra_map_eq F E D, ←eval₂_map, hr, adjoin_root.algebra_map_eq, eval₂_mul, adjoin_root.eval₂_root, zero_mul])), letI : algebra C E := ring_hom.to_algebra (adjoin_root.lift (algebra_map F E) x (minpoly.aeval F x)), haveI : is_scalar_tower F C D := of_algebra_map_eq (λ x, (adjoin_root.lift_of _).symm), haveI : is_scalar_tower F C E := of_algebra_map_eq (λ x, (adjoin_root.lift_of _).symm), suffices : nonempty (D →ₐ[C] E), { exact nonempty.map (alg_hom.restrict_scalars F) this }, let S : set D := ((p.map (algebra_map F E)).roots.map (algebra_map E D)).to_finset, suffices : ⊤ ≤ intermediate_field.adjoin C S, { refine intermediate_field.alg_hom_mk_adjoin_splits' (top_le_iff.mp this) (λ y hy, _), rcases multiset.mem_map.mp (multiset.mem_to_finset.mp hy) with ⟨z, hz1, hz2⟩, have Hz : is_integral F z := is_integral_of_noetherian (is_noetherian.iff_fg.2 hFE) z, use (show is_integral C y, from is_integral_of_noetherian (is_noetherian.iff_fg.2 (finite_dimensional.right F C D)) y), apply splits_of_splits_of_dvd (algebra_map C E) (map_ne_zero (minpoly.ne_zero Hz)), { rw [splits_map_iff, ←algebra_map_eq F C E], exact splits_of_splits_of_dvd _ hp hFEp.splits (minpoly.dvd F z (eq.trans (eval₂_eq_eval_map _) ((mem_roots (map_ne_zero hp)).mp hz1))) }, { apply minpoly.dvd, rw [←hz2, aeval_def, eval₂_map, ←algebra_map_eq F C D, algebra_map_eq F E D, ←hom_eval₂, ←aeval_def, minpoly.aeval F z, ring_hom.map_zero] } }, rw [←intermediate_field.to_subalgebra_le_to_subalgebra, intermediate_field.top_to_subalgebra], apply ge_trans (intermediate_field.algebra_adjoin_le_adjoin C S), suffices : (algebra.adjoin C S).restrict_scalars F = (algebra.adjoin E {adjoin_root.root q}).restrict_scalars F, { rw [adjoin_root.adjoin_root_eq_top, subalgebra.restrict_scalars_top, ←@subalgebra.restrict_scalars_top F C] at this, exact top_le_iff.mpr (subalgebra.restrict_scalars_injective F this) }, dsimp only [S], rw [←finset.image_to_finset, finset.coe_image], apply eq.trans (algebra.adjoin_res_eq_adjoin_res F E C D hFEp.adjoin_roots adjoin_root.adjoin_root_eq_top), rw [set.image_singleton, ring_hom.algebra_map_to_algebra, adjoin_root.lift_root] end instance (p : F[X]) : normal F p.splitting_field := normal.of_is_splitting_field p end normal_tower namespace intermediate_field /-- A compositum of normal extensions is normal -/ instance normal_supr {ι : Type*} (t : ι → intermediate_field F K) [h : ∀ i, normal F (t i)] : normal F (⨆ i, t i : intermediate_field F K) := begin refine ⟨is_algebraic_supr (λ i, (h i).1), λ x, _⟩, obtain ⟨s, hx⟩ := exists_finset_of_mem_supr'' (λ i, (h i).1) x.2, let E : intermediate_field F K := ⨆ i ∈ s, adjoin F ((minpoly F (i.2 : _)).root_set K), have hF : normal F E, { apply normal.of_is_splitting_field (∏ i in s, minpoly F i.2), refine is_splitting_field_supr _ (λ i hi, adjoin_root_set_is_splitting_field _), { exact finset.prod_ne_zero_iff.mpr (λ i hi, minpoly.ne_zero ((h i.1).is_integral i.2)) }, { exact polynomial.splits_comp_of_splits _ (algebra_map (t i.1) K) ((h i.1).splits i.2) } }, have hE : E ≤ ⨆ i, t i, { refine supr_le (λ i, supr_le (λ hi, le_supr_of_le i.1 _)), rw [adjoin_le_iff, ←image_root_set ((h i.1).splits i.2) (t i.1).val], exact λ _ ⟨a, _, h⟩, h ▸ a.2 }, have := hF.splits ⟨x, hx⟩, rw [minpoly_eq, subtype.coe_mk, ←minpoly_eq] at this, exact polynomial.splits_comp_of_splits _ (inclusion hE).to_ring_hom this, end variables {F K} {L : Type*} [field L] [algebra F L] [algebra K L] [is_scalar_tower F K L] @[simp] lemma restrict_scalars_normal {E : intermediate_field K L} : normal F (E.restrict_scalars F) ↔ normal F E := iff.rfl end intermediate_field variables {F} {K} {K₁ K₂ K₃:Type*} [field K₁] [field K₂] [field K₃] [algebra F K₁] [algebra F K₂] [algebra F K₃] (ϕ : K₁ →ₐ[F] K₂) (χ : K₁ ≃ₐ[F] K₂) (ψ : K₂ →ₐ[F] K₃) (ω : K₂ ≃ₐ[F] K₃) section restrict variables (E : Type*) [field E] [algebra F E] [algebra E K₁] [algebra E K₂] [algebra E K₃] [is_scalar_tower F E K₁] [is_scalar_tower F E K₂] [is_scalar_tower F E K₃] /-- Restrict algebra homomorphism to image of normal subfield -/ def alg_hom.restrict_normal_aux [h : normal F E] : (to_alg_hom F E K₁).range →ₐ[F] (to_alg_hom F E K₂).range := { to_fun := λ x, ⟨ϕ x, by { suffices : (to_alg_hom F E K₁).range.map ϕ ≤ _, { exact this ⟨x, subtype.mem x, rfl⟩ }, rintros x ⟨y, ⟨z, hy⟩, hx⟩, rw [←hx, ←hy], apply minpoly.mem_range_of_degree_eq_one E, exact or.resolve_left (h.splits z).def (minpoly.ne_zero (h.is_integral z)) (minpoly.irreducible $ is_integral_of_is_scalar_tower _ $ is_integral_alg_hom ϕ $ is_integral_alg_hom _ $ h.is_integral z) (minpoly.dvd E _ $ by rw [aeval_map, aeval_alg_hom, aeval_alg_hom, alg_hom.comp_apply, alg_hom.comp_apply, minpoly.aeval, alg_hom.map_zero, alg_hom.map_zero]) }⟩, map_zero' := subtype.ext ϕ.map_zero, map_one' := subtype.ext ϕ.map_one, map_add' := λ x y, subtype.ext (ϕ.map_add x y), map_mul' := λ x y, subtype.ext (ϕ.map_mul x y), commutes' := λ x, subtype.ext (ϕ.commutes x) } /-- Restrict algebra homomorphism to normal subfield -/ def alg_hom.restrict_normal [normal F E] : E →ₐ[F] E := ((alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F E K₂)).symm.to_alg_hom.comp (ϕ.restrict_normal_aux E)).comp (alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F E K₁)).to_alg_hom /-- Restrict algebra homomorphism to normal subfield (`alg_equiv` version) -/ def alg_hom.restrict_normal' [normal F E] : E ≃ₐ[F] E := alg_equiv.of_bijective (alg_hom.restrict_normal ϕ E) (alg_hom.normal_bijective F E E _) @[simp] lemma alg_hom.restrict_normal_commutes [normal F E] (x : E) : algebra_map E K₂ (ϕ.restrict_normal E x) = ϕ (algebra_map E K₁ x) := subtype.ext_iff.mp (alg_equiv.apply_symm_apply (alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F E K₂)) (ϕ.restrict_normal_aux E ⟨is_scalar_tower.to_alg_hom F E K₁ x, x, rfl⟩)) lemma alg_hom.restrict_normal_comp [normal F E] : (ψ.restrict_normal E).comp (ϕ.restrict_normal E) = (ψ.comp ϕ).restrict_normal E := alg_hom.ext (λ _, (algebra_map E K₃).injective (by simp only [alg_hom.comp_apply, alg_hom.restrict_normal_commutes])) /-- Restrict algebra isomorphism to a normal subfield -/ def alg_equiv.restrict_normal [h : normal F E] : E ≃ₐ[F] E := alg_hom.restrict_normal' χ.to_alg_hom E @[simp] lemma alg_equiv.restrict_normal_commutes [normal F E] (x : E) : algebra_map E K₂ (χ.restrict_normal E x) = χ (algebra_map E K₁ x) := χ.to_alg_hom.restrict_normal_commutes E x lemma alg_equiv.restrict_normal_trans [normal F E] : (χ.trans ω).restrict_normal E = (χ.restrict_normal E).trans (ω.restrict_normal E) := alg_equiv.ext (λ _, (algebra_map E K₃).injective (by simp only [alg_equiv.trans_apply, alg_equiv.restrict_normal_commutes])) /-- Restriction to an normal subfield as a group homomorphism -/ def alg_equiv.restrict_normal_hom [normal F E] : (K₁ ≃ₐ[F] K₁) →* (E ≃ₐ[F] E) := monoid_hom.mk' (λ χ, χ.restrict_normal E) (λ ω χ, (χ.restrict_normal_trans ω E)) variables (F K₁ E) /-- If `K₁/E/F` is a tower of fields with `E/F` normal then `normal.alg_hom_equiv_aut` is an equivalence. -/ @[simps] def normal.alg_hom_equiv_aut [normal F E] : (E →ₐ[F] K₁) ≃ (E ≃ₐ[F] E) := { to_fun := λ σ, alg_hom.restrict_normal' σ E, inv_fun := λ σ, (is_scalar_tower.to_alg_hom F E K₁).comp σ.to_alg_hom, left_inv := λ σ, begin ext, simp[alg_hom.restrict_normal'], end, right_inv := λ σ, begin ext, simp only [alg_hom.restrict_normal', alg_equiv.to_alg_hom_eq_coe, alg_equiv.coe_of_bijective], apply no_zero_smul_divisors.algebra_map_injective E K₁, rw alg_hom.restrict_normal_commutes, simp, end } end restrict section lift variables {F} {K₁ K₂} (E : Type*) [field E] [algebra F E] [algebra K₁ E] [algebra K₂ E] [is_scalar_tower F K₁ E] [is_scalar_tower F K₂ E] /-- If `E/Kᵢ/F` are towers of fields with `E/F` normal then we can lift an algebra homomorphism `ϕ : K₁ →ₐ[F] K₂` to `ϕ.lift_normal E : E →ₐ[F] E`. -/ noncomputable def alg_hom.lift_normal [h : normal F E] : E →ₐ[F] E := @alg_hom.restrict_scalars F K₁ E E _ _ _ _ _ _ ((is_scalar_tower.to_alg_hom F K₂ E).comp ϕ).to_ring_hom.to_algebra _ _ _ _ $ nonempty.some $ @intermediate_field.alg_hom_mk_adjoin_splits' _ _ _ _ _ _ _ ((is_scalar_tower.to_alg_hom F K₂ E).comp ϕ).to_ring_hom.to_algebra _ (intermediate_field.adjoin_univ _ _) (λ x hx, ⟨is_integral_of_is_scalar_tower x (h.out x).1, splits_of_splits_of_dvd _ (map_ne_zero (minpoly.ne_zero (h.out x).1)) (by { rw [splits_map_iff, ←is_scalar_tower.algebra_map_eq], exact (h.out x).2 }) (minpoly.dvd_map_of_is_scalar_tower F K₁ x)⟩) @[simp] lemma alg_hom.lift_normal_commutes [normal F E] (x : K₁) : ϕ.lift_normal E (algebra_map K₁ E x) = algebra_map K₂ E (ϕ x) := by apply @alg_hom.commutes K₁ E E _ _ _ _ @[simp] lemma alg_hom.restrict_lift_normal (ϕ : K₁ →ₐ[F] K₁) [normal F K₁] [normal F E] : (ϕ.lift_normal E).restrict_normal K₁ = ϕ := alg_hom.ext (λ x, (algebra_map K₁ E).injective (eq.trans (alg_hom.restrict_normal_commutes _ K₁ x) (ϕ.lift_normal_commutes E x))) /-- If `E/Kᵢ/F` are towers of fields with `E/F` normal then we can lift an algebra isomorphism `ϕ : K₁ ≃ₐ[F] K₂` to `ϕ.lift_normal E : E ≃ₐ[F] E`. -/ noncomputable def alg_equiv.lift_normal [normal F E] : E ≃ₐ[F] E := alg_equiv.of_bijective (χ.to_alg_hom.lift_normal E) (alg_hom.normal_bijective F E E _) @[simp] lemma alg_equiv.lift_normal_commutes [normal F E] (x : K₁) : χ.lift_normal E (algebra_map K₁ E x) = algebra_map K₂ E (χ x) := χ.to_alg_hom.lift_normal_commutes E x @[simp] lemma alg_equiv.restrict_lift_normal (χ : K₁ ≃ₐ[F] K₁) [normal F K₁] [normal F E] : (χ.lift_normal E).restrict_normal K₁ = χ := alg_equiv.ext (λ x, (algebra_map K₁ E).injective (eq.trans (alg_equiv.restrict_normal_commutes _ K₁ x) (χ.lift_normal_commutes E x))) lemma alg_equiv.restrict_normal_hom_surjective [normal F K₁] [normal F E] : function.surjective (alg_equiv.restrict_normal_hom K₁ : (E ≃ₐ[F] E) → (K₁ ≃ₐ[F] K₁)) := λ χ, ⟨χ.lift_normal E, χ.restrict_lift_normal E⟩ variables (F) (K₁) (E) lemma is_solvable_of_is_scalar_tower [normal F K₁] [h1 : is_solvable (K₁ ≃ₐ[F] K₁)] [h2 : is_solvable (E ≃ₐ[K₁] E)] : is_solvable (E ≃ₐ[F] E) := begin let f : (E ≃ₐ[K₁] E) →* (E ≃ₐ[F] E) := { to_fun := λ ϕ, alg_equiv.of_alg_hom (ϕ.to_alg_hom.restrict_scalars F) (ϕ.symm.to_alg_hom.restrict_scalars F) (alg_hom.ext (λ x, ϕ.apply_symm_apply x)) (alg_hom.ext (λ x, ϕ.symm_apply_apply x)), map_one' := alg_equiv.ext (λ _, rfl), map_mul' := λ _ _, alg_equiv.ext (λ _, rfl) }, refine solvable_of_ker_le_range f (alg_equiv.restrict_normal_hom K₁) (λ ϕ hϕ, ⟨{commutes' := λ x, _, .. ϕ}, alg_equiv.ext (λ _, rfl)⟩), exact (eq.trans (ϕ.restrict_normal_commutes K₁ x).symm (congr_arg _ (alg_equiv.ext_iff.mp hϕ x))), end end lift section normal_closure open intermediate_field variables (F K) (L : Type*) [field L] [algebra F L] [algebra K L] [is_scalar_tower F K L] /-- The normal closure of `K` in `L`. -/ noncomputable! def normal_closure : intermediate_field K L := { algebra_map_mem' := λ r, le_supr (λ f : K →ₐ[F] L, f.field_range) (is_scalar_tower.to_alg_hom F K L) ⟨r, rfl⟩, .. (⨆ f : K →ₐ[F] L, f.field_range).to_subfield } namespace normal_closure lemma restrict_scalars_eq_supr_adjoin [h : normal F L] : (normal_closure F K L).restrict_scalars F = ⨆ x : K, adjoin F ((minpoly F x).root_set L) := begin refine le_antisymm (supr_le _) (supr_le (λ x, adjoin_le_iff.mpr (λ y hy, _))), { rintros f _ ⟨x, rfl⟩, refine le_supr (λ x, adjoin F ((minpoly F x).root_set L)) x (subset_adjoin F ((minpoly F x).root_set L) _), rw [polynomial.mem_root_set, alg_hom.to_ring_hom_eq_coe, alg_hom.coe_to_ring_hom, polynomial.aeval_alg_hom_apply, minpoly.aeval, map_zero], exact minpoly.ne_zero ((is_integral_algebra_map_iff (algebra_map K L).injective).mp (h.is_integral (algebra_map K L x))) }, { rw [polynomial.root_set, finset.mem_coe, multiset.mem_to_finset] at hy, let g := (alg_hom_adjoin_integral_equiv F ((is_integral_algebra_map_iff (algebra_map K L).injective).mp (h.is_integral (algebra_map K L x)))).symm ⟨y, hy⟩, refine le_supr (λ f : K →ₐ[F] L, f.field_range) ((g.lift_normal L).comp (is_scalar_tower.to_alg_hom F K L)) ⟨x, (g.lift_normal_commutes L (adjoin_simple.gen F x)).trans _⟩, rw [algebra.id.map_eq_id, ring_hom.id_apply], apply power_basis.lift_gen }, end instance normal [h : normal F L] : normal F (normal_closure F K L) := let ϕ := algebra_map K L in begin rw [←intermediate_field.restrict_scalars_normal, restrict_scalars_eq_supr_adjoin], apply intermediate_field.normal_supr F L _, intro x, apply normal.of_is_splitting_field (minpoly F x), exact adjoin_root_set_is_splitting_field ((minpoly.eq_of_algebra_map_eq ϕ.injective ((is_integral_algebra_map_iff ϕ.injective).mp (h.is_integral (ϕ x))) rfl).symm ▸ h.splits _), end instance is_finite_dimensional [finite_dimensional F K] : finite_dimensional F (normal_closure F K L) := begin haveI : ∀ f : K →ₐ[F] L, finite_dimensional F f.field_range := λ f, f.to_linear_map.finite_dimensional_range, apply intermediate_field.finite_dimensional_supr_of_finite, end end normal_closure end normal_closure
6f2aa7a7db3d2da99ab73c952c06bd8cebb86919
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/begin_end1.lean
dd634c58e9ad5f103b2a9291cfbf7c4b5d2fa8d6
[ "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
136
lean
open tactic example (p q : Prop) : p → q → q ∧ p := begin intros, constructor, trace_state, assumption, assumption end
e294ac8ee45a3f7d7a672e282b0e8f0b8ced5526
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/meta/coinductive_predicates.lean
0435717db727cc0b0365f772cdeaebcf935ec02f
[ "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
20,477
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 (CMU) -/ import tactic.core section universe u @[user_attribute] meta def monotonicity : user_attribute := { name := `monotonicity, descr := "Monotonicity rules for predicates" } lemma monotonicity.pi {α : Sort u} {p q : α → Prop} (h : ∀a, implies (p a) (q a)) : implies (Πa, p a) (Πa, q a) := assume h' a, h a (h' a) lemma monotonicity.imp {p p' q q' : Prop} (h₁ : implies p' q') (h₂ : implies q p) : implies (p → p') (q → q') := assume h, h₁ ∘ h ∘ h₂ @[monotonicity] lemma monotonicity.const (p : Prop) : implies p p := id @[monotonicity] lemma monotonicity.true (p : Prop) : implies p true := assume _, trivial @[monotonicity] lemma monotonicity.false (p : Prop) : implies false p := false.elim @[monotonicity] lemma monotonicity.exists {α : Sort u} {p q : α → Prop} (h : ∀a, implies (p a) (q a)) : implies (∃a, p a) (∃a, q a) := exists_imp_exists h @[monotonicity] lemma monotonicity.and {p p' q q' : Prop} (hp : implies p p') (hq : implies q q') : implies (p ∧ q) (p' ∧ q') := and.imp hp hq @[monotonicity] lemma monotonicity.or {p p' q q' : Prop} (hp : implies p p') (hq : implies q q') : implies (p ∨ q) (p' ∨ q') := or.imp hp hq @[monotonicity] lemma monotonicity.not {p q : Prop} (h : implies p q) : implies (¬ q) (¬ p) := mt h end namespace tactic open expr tactic /- TODO: use backchaining -/ private meta def mono_aux (ns : list name) (hs : list expr) : tactic unit := do intros, (do `(implies %%p %%q) ← target, (do is_def_eq p q, eapplyc `monotone.const) <|> (do (expr.pi pn pbi pd pb) ← whnf p, (expr.pi qn qbi qd qb) ← whnf q, sort u ← infer_type pd, (do is_def_eq pd qd, let p' := expr.lam pn pbi pd pb, let q' := expr.lam qn qbi qd qb, eapply ((const `monotonicity.pi [u] : expr) pd p' q'), skip) <|> (do guard $ u = level.zero ∧ is_arrow p ∧ is_arrow q, let p' := pb.lower_vars 0 1, let q' := qb.lower_vars 0 1, eapply ((const `monotonicity.imp []: expr) pd p' qd q'), skip))) <|> first (hs.map $ λh, apply_core h {md := transparency.none, new_goals := new_goals.non_dep_only} >> skip) <|> first (ns.map $ λn, do c ← mk_const n, apply_core c {md := transparency.none, new_goals := new_goals.non_dep_only}, skip), all_goals' mono_aux meta def mono (e : expr) (hs : list expr) : tactic unit := do t ← target, t' ← infer_type e, ns ← attribute.get_instances `monotonicity, ((), p) ← solve_aux `(implies %%t' %%t) (mono_aux ns hs), exact (p e) end tactic /- The coinductive predicate `pred`: coinductive {u} pred (A) : a → Prop | r : ∀A b, pred A p where `u` is a list of universe parameters `A` is a list of global parameters `pred` is a list predicates to be defined `a` are the indices for each `pred` `r` is a list of introduction rules for each `pred` `b` is a list of parameters for each rule in `r` and `pred` `p` is are the instances of `a` using `A` and `b` `pred` is compiled to the following defintions: inductive {u} pred.functional (A) ([pred'] : a → Prop) : a → Prop | r : ∀a [f], b[pred/pred'] → pred.functional a [f] p lemma {u} pred.functional.mono (A) ([pred₁] [pred₂] : a → Prop) [(h : ∀b, pred₁ b → pred₂ b)] : ∀p, pred.functional A pred₁ p → pred.functional A pred₂ p def {u} pred_i (A) (a) : Prop := ∃[pred'], (Λi, ∀a, pred_i a → pred_i.functional A [pred] a) ∧ pred'_i a lemma {u} pred_i.corec_functional (A) [Λi, C_i : a_i → Prop] [Λi, h : ∀a, C_i a → pred_i.functional A C_i a] : ∀a, C_i a → pred_i A a lemma {u} pred_i.destruct (A) (a) : pred A a → pred.functional A [pred A] a lemma {u} pred_i.construct (A) : ∀a, pred_i.functional A [pred A] a → pred_i A a lemma {u} pred_i.cases_on (A) (C : a → Prop) {a} (h : pred_i a) [Λi, ∀a, b → C p] → C a lemma {u} pred_i.corec_on (A) [(C : a → Prop)] (a) (h : C_i a) [Λi, h_i : ∀a, C_i a → [V j ∃b, a = p]] : pred_i A a lemma {u} pred.r (A) (b) : pred_i A p -/ namespace tactic open level expr tactic namespace add_coinductive_predicate /- private -/ meta structure coind_rule : Type := (orig_nm : name) (func_nm : name) (type : expr) (loc_type : expr) (args : list expr) (loc_args : list expr) (concl : expr) (insts : list expr) /- private -/ meta structure coind_pred : Type := (u_names : list name) (params : list expr) (pd_name : name) (type : expr) (intros : list coind_rule) (locals : list expr) (f₁ f₂ : expr) (u_f : level) namespace coind_pred meta def u_params (pd : coind_pred) : list level := pd.u_names.map param meta def f₁_l (pd : coind_pred) : expr := pd.f₁.app_of_list pd.locals meta def f₂_l (pd : coind_pred) : expr := pd.f₂.app_of_list pd.locals meta def pred (pd : coind_pred) : expr := const pd.pd_name pd.u_params meta def func (pd : coind_pred) : expr := const (pd.pd_name ++ "functional") pd.u_params meta def func_g (pd : coind_pred) : expr := pd.func.app_of_list $ pd.params meta def pred_g (pd : coind_pred) : expr := pd.pred.app_of_list $ pd.params meta def impl_locals (pd : coind_pred) : list expr := pd.locals.map to_implicit_binder meta def impl_params (pd : coind_pred) : list expr := pd.params.map to_implicit_binder meta def le (pd : coind_pred) (f₁ f₂ : expr) : expr := (imp (f₁.app_of_list pd.locals) (f₂.app_of_list pd.locals)).pis pd.impl_locals meta def corec_functional (pd : coind_pred) : expr := const (pd.pd_name ++ "corec_functional") pd.u_params meta def mono (pd : coind_pred) : expr := const (pd.func.const_name ++ "mono") pd.u_params meta def rec' (pd : coind_pred) : tactic expr := do let c := pd.func.const_name ++ "rec", env ← get_env, decl ← env.get c, let num := decl.univ_params.length, return (const c $ if num = pd.u_params.length then pd.u_params else level.zero :: pd.u_params) -- ^^ `rec`'s universes are not always `u_params`, e.g. eq, wf, false meta def construct (pd : coind_pred) : expr := const (pd.pd_name ++ "construct") pd.u_params meta def destruct (pd : coind_pred) : expr := const (pd.pd_name ++ "destruct") pd.u_params meta def add_theorem (pd : coind_pred) (n : name) (type : expr) (tac : tactic unit) : tactic expr := add_theorem_by n pd.u_names type tac end coind_pred end add_coinductive_predicate open add_coinductive_predicate /- compact_relation bs as_ps: Product a relation of the form: R := λ as, ∃ bs, Λ_i a_i = p_i[bs] This relation is user visible, so we compact it by removing each `b_j` where a `p_i = b_j`, and hence `a_i = b_j`. We need to take care when there are `p_i` and `p_j` with `p_i = p_j = b_k`. -/ private meta def compact_relation : list expr → list (expr × expr) → list expr × list (expr × expr) | [] ps := ([], ps) | (list.cons b bs) ps := match ps.span (λap:expr × expr, ¬ ap.2 =ₐ b) with | (_, []) := let (bs, ps) := compact_relation bs ps in (b::bs, ps) | (ps₁, list.cons (a, _) ps₂) := let i := a.instantiate_local b.local_uniq_name in compact_relation (bs.map i) ((ps₁ ++ ps₂).map (λ⟨a, p⟩, (a, i p))) end meta def add_coinductive_predicate (u_names : list name) (params : list expr) (preds : list $ expr × list expr) : command := do let params_names := params.map local_pp_name, let u_params := u_names.map param, pre_info ← preds.mmap (λ⟨c, is⟩, do (ls, t) ← open_pis c.local_type, (is_def_eq t `(Prop) <|> fail (format! "Type of {c.local_pp_name} is not Prop. Currently only " ++ "coinductive predicates are supported.")), let n := if preds.length = 1 then "" else "_" ++ c.local_pp_name.last_string, f₁ ← mk_local_def (mk_simple_name $ "C" ++ n) c.local_type, f₂ ← mk_local_def (mk_simple_name $ "C₂" ++ n) c.local_type, return (ls, (f₁, f₂))), let fs := pre_info.map prod.snd, let fs₁ := fs.map prod.fst, let fs₂ := fs.map prod.snd, pds ← (preds.zip pre_info).mmap (λ⟨⟨c, is⟩, ls, f₁, f₂⟩, do sort u_f ← infer_type f₁ >>= infer_type, let pred_g := λc:expr, (const c.local_uniq_name u_params : expr).app_of_list params, intros ← is.mmap (λi, do (args, t') ← open_pis i.local_type, (name.mk_string sub p) ← return i.local_uniq_name, let loc_args := args.map $ λe, (fs₁.zip preds).foldl (λ(e:expr) ⟨f, c, _⟩, e.replace_with (pred_g c) f) e, let t' := t'.replace_with (pred_g c) f₂, return { tactic.add_coinductive_predicate.coind_rule . orig_nm := i.local_uniq_name, func_nm := (p ++ "functional") ++ sub, type := i.local_type, loc_type := t'.pis loc_args, concl := t', loc_args := loc_args, args := args, insts := t'.get_app_args }), return { tactic.add_coinductive_predicate.coind_pred . pd_name := c.local_uniq_name, type := c.local_type, f₁ := f₁, f₂ := f₂, u_f := u_f, intros := intros, locals := ls, params := params, u_names := u_names }), /- Introduce all functionals -/ pds.mmap' (λpd:coind_pred, do let func_f₁ := pd.func_g.app_of_list $ fs₁, let func_f₂ := pd.func_g.app_of_list $ fs₂, /- Define functional for `pd` as inductive predicate -/ func_intros ← pd.intros.mmap (λr:coind_rule, do let t := instantiate_local pd.f₂.local_uniq_name (pd.func_g.app_of_list fs₁) r.loc_type, return (r.func_nm, r.orig_nm, t.pis $ params ++ fs₁)), add_inductive pd.func.const_name u_names (params.length + preds.length) (pd.type.pis $ params ++ fs₁) (func_intros.map $ λ⟨t, _, r⟩, (t, r)), /- Prove monotonicity rule -/ mono_params ← pds.mmap (λpd, do h ← mk_local_def `h $ pd.le pd.f₁ pd.f₂, return [pd.f₁, pd.f₂, h]), pd.add_theorem (pd.func.const_name ++ "mono") ((pd.le func_f₁ func_f₂).pis $ params ++ mono_params.join) (do ps ← intro_lst $ params.map expr.local_pp_name, fs ← pds.mmap (λpd, do [f₁, f₂, h] ← intro_lst [pd.f₁.local_pp_name, pd.f₂.local_pp_name, `h], -- the type of h' reduces to h let h' := local_const h.local_uniq_name h.local_pp_name h.local_binding_info $ (((const `implies [] : expr) (f₁.app_of_list pd.locals) (f₂.app_of_list pd.locals)).pis pd.locals).instantiate_locals $ (ps.zip params).map $ λ⟨lv, p⟩, (p.local_uniq_name, lv), return (f₂, h')), m ← pd.rec', eapply $ m.app_of_list ps, -- somehow `induction` / `cases` doesn't work? func_intros.mmap' (λ⟨n, pp_n, t⟩, solve1 $ do bs ← intros, ms ← apply_core ((const n u_params).app_of_list $ ps ++ fs.map prod.fst) {new_goals := new_goals.all}, params ← (ms.zip bs).enum.mfilter (λ⟨n, m, d⟩, bnot <$> is_assigned m.2), params.mmap' (λ⟨n, m, d⟩, mono d (fs.map prod.snd) <|> fail format! "failed to prove montonoicity of {n+1}. parameter of intro-rule {pp_n}")))), pds.mmap' (λpd, do let func_f := λpd:coind_pred, pd.func_g.app_of_list $ pds.map coind_pred.f₁, /- define final predicate -/ pred_body ← mk_exists_lst (pds.map coind_pred.f₁) $ mk_and_lst $ (pds.map $ λpd, pd.le pd.f₁ (func_f pd)) ++ [pd.f₁.app_of_list pd.locals], add_decl $ mk_definition pd.pd_name u_names (pd.type.pis $ params) $ pred_body.lambdas $ params ++ pd.locals, /- prove `corec_functional` rule -/ hs ← pds.mmap $ λpd:coind_pred, mk_local_def `hc $ pd.le pd.f₁ (func_f pd), pd.add_theorem (pd.pred.const_name ++ "corec_functional") ((pd.le pd.f₁ pd.pred_g).pis $ params ++ fs₁ ++ hs) (do intro_lst $ params.map local_pp_name, fs ← intro_lst $ fs₁.map local_pp_name, hs ← intro_lst $ hs.map local_pp_name, ls ← intro_lst $ pd.locals.map local_pp_name, h ← intro `h, whnf_target, fs.mmap' existsi, hs.mmap' (λf, econstructor >> exact f), exact h)), let func_f := λpd : coind_pred, pd.func_g.app_of_list $ pds.map coind_pred.pred_g, /- prove `destruct` rules -/ pds.enum.mmap' (λ⟨n, pd⟩, do let destruct := pd.le pd.pred_g (func_f pd), pd.add_theorem (pd.pred.const_name ++ "destruct") (destruct.pis params) (do ps ← intro_lst $ params.map local_pp_name, ls ← intro_lst $ pd.locals.map local_pp_name, h ← intro `h, (fs, h, _) ← elim_gen_prod pds.length h [] [], (hs, h, _) ← elim_gen_prod pds.length h [] [], eapply $ pd.mono.app_of_list ps, pds.mmap' (λpd:coind_pred, focus1 $ do eapply $ pd.corec_functional, focus $ hs.map exact), some h' ← return $ hs.nth n, eapply h', exact h)), /- prove `construct` rules -/ pds.mmap' (λpd, pd.add_theorem (pd.pred.const_name ++ "construct") ((pd.le (func_f pd) pd.pred_g).pis params) (do ps ← intro_lst $ params.map local_pp_name, let func_pred_g := λpd:coind_pred, pd.func.app_of_list $ ps ++ pds.map (λpd:coind_pred, pd.pred.app_of_list ps), eapply $ pd.corec_functional.app_of_list $ ps ++ pds.map func_pred_g, pds.mmap' (λpd:coind_pred, solve1 $ do eapply $ pd.mono.app_of_list ps, pds.mmap' (λpd, solve1 $ eapply (pd.destruct.app_of_list ps) >> skip)))), /- prove `cases_on` rules -/ pds.mmap' (λpd, do let C := pd.f₁.to_implicit_binder, h ← mk_local_def `h $ pd.pred_g.app_of_list pd.locals, rules ← pd.intros.mmap (λr:coind_rule, do mk_local_def (mk_simple_name r.orig_nm.last_string) $ (C.app_of_list r.insts).pis r.args), cases_on ← pd.add_theorem (pd.pred.const_name ++ "cases_on") ((C.app_of_list pd.locals).pis $ params ++ [C] ++ pd.impl_locals ++ [h] ++ rules) (do ps ← intro_lst $ params.map local_pp_name, C ← intro `C, ls ← intro_lst $ pd.locals.map local_pp_name, h ← intro `h, rules ← intro_lst $ rules.map local_pp_name, func_rec ← pd.rec', eapply $ func_rec.app_of_list $ ps ++ pds.map (λpd, pd.pred.app_of_list ps) ++ [C] ++ rules, eapply $ pd.destruct, exact h), set_basic_attribute `elab_as_eliminator cases_on.const_name), /- prove `corec_on` rules -/ pds.mmap' (λpd, do rules ← pds.mmap (λpd, do intros ← pd.intros.mmap (λr, do let (bs, eqs) := compact_relation r.loc_args $ pd.locals.zip r.insts, eqs ← eqs.mmap (λ⟨l, i⟩, do sort u ← infer_type l.local_type, return $ (const `eq [u] : expr) l.local_type i l), match bs, eqs with | [], [] := return ((0, 0), mk_true) | _, [] := prod.mk (bs.length, 0) <$> mk_exists_lst bs.init bs.ilast.local_type | _, _ := prod.mk (bs.length, eqs.length) <$> mk_exists_lst bs (mk_and_lst eqs) end), let shape := intros.map prod.fst, let intros := intros.map prod.snd, prod.mk shape <$> mk_local_def (mk_simple_name $ "h_" ++ pd.pd_name.last_string) (((pd.f₁.app_of_list pd.locals).imp (mk_or_lst intros)).pis pd.locals)), let shape := rules.map prod.fst, let rules := rules.map prod.snd, h ← mk_local_def `h $ pd.f₁.app_of_list pd.locals, pd.add_theorem (pd.pred.const_name ++ "corec_on") ((pd.pred_g.app_of_list $ pd.locals).pis $ params ++ fs₁ ++ pd.impl_locals ++ [h] ++ rules) (do ps ← intro_lst $ params.map local_pp_name, fs ← intro_lst $ fs₁.map local_pp_name, ls ← intro_lst $ pd.locals.map local_pp_name, h ← intro `h, rules ← intro_lst $ rules.map local_pp_name, eapply $ pd.corec_functional.app_of_list $ ps ++ fs, (pds.zip $ rules.zip shape).mmap (λ⟨pd, hr, s⟩, solve1 $ do ls ← intro_lst $ pd.locals.map local_pp_name, h' ← intro `h, h' ← note `h' none $ hr.app_of_list ls h', match s.length with | 0 := induction h' >> skip -- h' : false | (n+1) := do hs ← elim_gen_sum n h', (hs.zip $ pd.intros.zip s).mmap' (λ⟨h, r, n_bs, n_eqs⟩, solve1 $ do (as, h, _) ← elim_gen_prod (n_bs - (if n_eqs = 0 then 1 else 0)) h [] [], if n_eqs > 0 then do (eqs, eq', _) ← elim_gen_prod (n_eqs - 1) h [] [], (eqs ++ [eq']).mmap' subst else skip, eapply ((const r.func_nm u_params).app_of_list $ ps ++ fs), iterate assumption) end), exact h)), /- prove constructors -/ pds.mmap' (λpd, pd.intros.mmap' (λr, pd.add_theorem r.orig_nm (r.type.pis params) $ do ps ← intro_lst $ params.map local_pp_name, bs ← intros, eapply $ pd.construct, exact $ (const r.func_nm u_params).app_of_list $ ps ++ pds.map (λpd, pd.pred.app_of_list ps) ++ bs)), pds.mmap' (λpd:coind_pred, set_basic_attribute `irreducible pd.pd_name), try triv -- we setup a trivial goal for the tactic framework setup_tactic_parser @[user_command] meta def coinductive_predicate (meta_info : decl_meta_info) (_ : parse $ tk "coinductive") : lean.parser unit := do { decl ← inductive_decl.parse meta_info, add_coinductive_predicate decl.u_names decl.params $ decl.decls.map $ λ d, (d.sig, d.intros), decl.decls.mmap' $ λ d, do { get_env >>= λ env, set_env $ env.add_namespace d.name, meta_info.attrs.apply d.name, d.attrs.apply d.name, some doc_string ← pure meta_info.doc_string | skip, add_doc_string d.name doc_string } } /-- Prepares coinduction proofs. This tactic constructs the coinduction invariant from the quantifiers in the current goal. Current version: do not support mutual inductive rules -/ meta def coinduction (rule : expr) (ns : list name) : tactic unit := focus1 $ do ctxts' ← intros, ctxts ← ctxts'.mmap (λv, local_const v.local_uniq_name v.local_pp_name v.local_binding_info <$> infer_type v), mvars ← apply_core rule {approx := ff, new_goals := new_goals.all}, -- analyse relation g ← list.head <$> get_goals, (list.cons _ m_is) ← return $ mvars.drop_while (λv, v.2 ≠ g), tgt ← target, (is, ty) ← open_pis tgt, -- construct coinduction predicate (bs, eqs) ← compact_relation ctxts <$> ((is.zip m_is).mmap (λ⟨i, m⟩, prod.mk i <$> instantiate_mvars m.2)), solve1 (do eqs ← mk_and_lst <$> eqs.mmap (λ⟨i, m⟩, mk_app `eq [m, i] >>= instantiate_mvars) <|> do { x ← mk_psigma (eqs.map prod.fst), y ← mk_psigma (eqs.map prod.snd), t ← infer_type x, mk_mapp `eq [t,x,y] }, rel ← mk_exists_lst bs eqs, exact (rel.lambdas is)), -- prove predicate solve1 (do target >>= instantiate_mvars >>= change, -- TODO: bug in existsi & constructor when mvars in hyptohesis bs.mmap existsi, iterate' (econstructor >> skip)), -- clean up remaining coinduction steps all_goals' (do ctxts'.reverse.mmap clear, target >>= instantiate_mvars >>= change, -- TODO: bug in subst when mvars in hyptohesis is ← intro_lst $ is.map expr.local_pp_name, h ← intro1, (_, h, ns) ← elim_gen_prod (bs.length - (if eqs.length = 0 then 1 else 0)) h [] ns, (match eqs with | [] := clear h | (e::eqs) := do (hs, h, ns) ← elim_gen_prod eqs.length h [] ns, (h::(hs.reverse) : list _).mfoldl (λ (hs : list name) (h : expr), do [(_,hs',σ)] ← cases_core h hs, clear (h.instantiate_locals σ), pure $ hs.drop hs'.length) ns, skip end)) namespace interactive open interactive interactive.types expr lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many meta def coinduction (corec_name : parse ident) (ns : parse with_ident_list) (revert : parse $ (tk "generalizing" *> ident*)?) : tactic unit := do rule ← mk_const corec_name, locals ← mmap tactic.get_local $ revert.get_or_else [], revert_lst locals, tactic.coinduction rule ns, skip end interactive end tactic
eb886dae35fd70f4b00707db91dec5547ebb830a
359199d7253811b032ab92108191da7336eba86e
/src/mywork/9.15.2021_homework.lean
b2c53bae4b4e86c8fddf9cd405207748a33c6bd7
[]
no_license
arte-et-marte/my_cs2120f21
0bc6215cb5018a3b7c90d9d399a173233f587064
91609c3609ad81fda895bee8b97cc76813241e17
refs/heads/main
1,693,298,928,348
1,634,931,202,000
1,634,931,202,000
399,946,705
0
0
null
null
null
null
UTF-8
Lean
false
false
401
lean
/- 1) Prove that or is commutative. -/ theorem or_is_comm : ∀ (P Q : Prop) (q : Q), P ∨ Q → Q ∨ P := begin assume P Q q p_or_q, exact or.intro_left P q, end /- 2) Prove that or is associative. -/ theorem or_is_assoc : ∀ (P Q R : Prop) (p : P), (P ∨ Q) ∨ R → P ∨ (Q ∨ R) := begin assume P Q R p pq_r, exact or.intro_left (Q ∨ R) p, end
284544817777fb8029c8e57b1badff99b687ccc6
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/e17.lean
7835b23db4f2a45087d1d82ac06294783dae00ed
[ "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
452
lean
inductive nat : Type := zero : nat, succ : nat → nat inductive list (A : Type) : Type := nil {} : list A, cons : A → list A → list A inductive int : Type := of_nat : nat → int, neg : nat → int coercion int.of_nat constants n m : nat constants i j : int namespace list end list open list check cons i (cons i nil) check cons n (cons n nil) check cons i (cons n nil) check cons n (cons i nil) check cons n (cons i (cons m (cons j nil)))
530b12939d8e5cdaf618bf7f67a2d4a035281dd3
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Init/Control/StateCps.lean
6b00d4030ece6b74689cde461d93083e4df044e3
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,670
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Control.Lawful /- The State monad transformer using CPS style. -/ def StateCpsT (σ : Type u) (m : Type u → Type v) (α : Type u) := (δ : Type u) → σ → (α → σ → m δ) → m δ namespace StateCpsT @[inline] def runK {α σ : Type u} {m : Type u → Type v} (x : StateCpsT σ m α) (s : σ) (k : α → σ → m β) : m β := x _ s k @[inline] def run {α σ : Type u} {m : Type u → Type v} [Monad m] (x : StateCpsT σ m α) (s : σ) : m (α × σ) := runK x s (fun a s => pure (a, s)) @[inline] def run' {α σ : Type u} {m : Type u → Type v} [Monad m] (x : StateCpsT σ m α) (s : σ) : m α := runK x s (fun a s => pure a) instance : Monad (StateCpsT σ m) where map f x := fun δ s k => x δ s fun a s => k (f a) s pure a := fun δ s k => k a s bind x f := fun δ s k => x δ s fun a s => f a δ s k instance : LawfulMonad (StateCpsT σ m) := by refine! { .. } <;> intros <;> rfl instance : MonadStateOf σ (StateCpsT σ m) where get := fun δ s k => k s s set s := fun δ _ k => k ⟨⟩ s modifyGet f := fun _ s k => let (a, s) := f s; k a s @[inline] protected def lift [Monad m] (x : m α) : StateCpsT σ m α := fun _ s k => x >>= (k . s) instance [Monad m] : MonadLift m (StateCpsT σ m) where monadLift := StateCpsT.lift @[simp] theorem runK_pure {m : Type u → Type v} (a : α) (s : σ) (k : α → σ → m β) : (pure a : StateCpsT σ m α).runK s k = k a s := rfl @[simp] theorem runK_get {m : Type u → Type v} (s : σ) (k : σ → σ → m β) : (get : StateCpsT σ m σ).runK s k = k s s := rfl @[simp] theorem runK_set {m : Type u → Type v} (s s' : σ) (k : PUnit → σ → m β) : (set s' : StateCpsT σ m PUnit).runK s k = k ⟨⟩ s' := rfl @[simp] theorem runK_modify {m : Type u → Type v} (f : σ → σ) (s : σ) (k : PUnit → σ → m β) : (modify f : StateCpsT σ m PUnit).runK s k = k ⟨⟩ (f s) := rfl @[simp] theorem runK_lift {α σ : Type u} [Monad m] (x : m α) (s : σ) (k : α → σ → m β) : (StateCpsT.lift x : StateCpsT σ m α).runK s k = x >>= (k . s) := rfl @[simp] theorem runK_monadLift {σ : Type u} [Monad m] [MonadLiftT n m] (x : n α) (s : σ) (k : α → σ → m β) : (monadLift x : StateCpsT σ m α).runK s k = (monadLift x : m α) >>= (k . s) := rfl @[simp] theorem runK_bind_pure {α σ : Type u} [Monad m] (a : α) (f : α → StateCpsT σ m β) (s : σ) (k : β → σ → m γ) : (pure a >>= f).runK s k = (f a).runK s k := rfl @[simp] theorem runK_bind_lift {α σ : Type u} [Monad m] (x : m α) (f : α → StateCpsT σ m β) (s : σ) (k : β → σ → m γ) : (StateCpsT.lift x >>= f).runK s k = x >>= fun a => (f a).runK s k := rfl @[simp] theorem runK_bind_get {σ : Type u} [Monad m] (f : σ → StateCpsT σ m β) (s : σ) (k : β → σ → m γ) : (get >>= f).runK s k = (f s).runK s k := rfl @[simp] theorem runK_bind_set {σ : Type u} [Monad m] (f : PUnit → StateCpsT σ m β) (s s' : σ) (k : β → σ → m γ) : (set s' >>= f).runK s k = (f ⟨⟩).runK s' k := rfl @[simp] theorem runK_bind_modify {σ : Type u} [Monad m] (f : σ → σ) (g : PUnit → StateCpsT σ m β) (s : σ) (k : β → σ → m γ) : (modify f >>= g).runK s k = (g ⟨⟩).runK (f s) k := rfl @[simp] theorem run_eq [Monad m] (x : StateCpsT σ m α) (s : σ) : x.run s = x.runK s (fun a s => pure (a, s)) := rfl @[simp] theorem run'_eq [Monad m] (x : StateCpsT σ m α) (s : σ) : x.run' s = x.runK s (fun a s => pure a) := rfl end StateCpsT
998d7a2a2d8f1755a7a0f084f74447ab7970fe04
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/order/filter/bases.lean
c7cea22d374f5cbb4902f574335bdc43e298d54e
[ "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
38,689
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import order.filter.basic import data.set.countable import data.pprod /-! # Filter bases A filter basis `B : filter_basis α` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. Compared to filters, filter bases do not require that any set containing an element of `B` belongs to `B`. A filter basis `B` can be used to construct `B.filter : filter α` such that a set belongs to `B.filter` if and only if it contains an element of `B`. Given an indexing type `ι`, a predicate `p : ι → Prop`, and a map `s : ι → set α`, the proposition `h : filter.is_basis p s` makes sure the range of `s` bounded by `p` (ie. `s '' set_of p`) defines a filter basis `h.filter_basis`. If one already has a filter `l` on `α`, `filter.has_basis l p s` (where `p : ι → Prop` and `s : ι → set α` as above) means that a set belongs to `l` if and only if it contains some `s i` with `p i`. It implies `h : filter.is_basis p s`, and `l = h.filter_basis.filter`. The point of this definition is that checking statements involving elements of `l` often reduces to checking them on the basis elements. We define a function `has_basis.index (h : filter.has_basis l p s) (t) (ht : t ∈ l)` that returns some index `i` such that `p i` and `s i ⊆ t`. This function can be useful to avoid manual destruction of `h.mem_iff.mpr ht` using `cases` or `let`. This file also introduces more restricted classes of bases, involving monotonicity or countability. In particular, for `l : filter α`, `l.is_countably_generated` means there is a countable set of sets which generates `s`. This is reformulated in term of bases, and consequences are derived. ## Main statements * `has_basis.mem_iff`, `has_basis.mem_of_superset`, `has_basis.mem_of_mem` : restate `t ∈ f` in terms of a basis; * `basis_sets` : all sets of a filter form a basis; * `has_basis.inf`, `has_basis.inf_principal`, `has_basis.prod`, `has_basis.prod_self`, `has_basis.map`, `has_basis.comap` : combinators to construct filters of `l ⊓ l'`, `l ⊓ 𝓟 t`, `l ×ᶠ l'`, `l ×ᶠ l`, `l.map f`, `l.comap f` respectively; * `has_basis.le_iff`, `has_basis.ge_iff`, has_basis.le_basis_iff` : restate `l ≤ l'` in terms of bases. * `has_basis.tendsto_right_iff`, `has_basis.tendsto_left_iff`, `has_basis.tendsto_iff` : restate `tendsto f l l'` in terms of bases. * `is_countably_generated_iff_exists_antimono_basis` : proves a filter is countably generated if and only if it admis a basis parametrized by a decreasing sequence of sets indexed by `ℕ`. * `tendsto_iff_seq_tendsto ` : an abstract version of "sequentially continuous implies continuous". ## Implementation notes As with `Union`/`bUnion`/`sUnion`, there are three different approaches to filter bases: * `has_basis l s`, `s : set (set α)`; * `has_basis l s`, `s : ι → set α`; * `has_basis l p s`, `p : ι → Prop`, `s : ι → set α`. We use the latter one because, e.g., `𝓝 x` in an `emetric_space` or in a `metric_space` has a basis of this form. The other two can be emulated using `s = id` or `p = λ _, true`. With this approach sometimes one needs to `simp` the statement provided by the `has_basis` machinery, e.g., `simp only [exists_prop, true_and]` or `simp only [forall_const]` can help with the case `p = λ _, true`. -/ open set filter open_locale filter classical section sort variables {α β γ : Type*} {ι ι' : Sort*} /-- A filter basis `B` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure filter_basis (α : Type*) := (sets : set (set α)) (nonempty : sets.nonempty) (inter_sets {x y} : x ∈ sets → y ∈ sets → ∃ z ∈ sets, z ⊆ x ∩ y) instance filter_basis.nonempty_sets (B : filter_basis α) : nonempty B.sets := B.nonempty.to_subtype /-- If `B` is a filter basis on `α`, and `U` a subset of `α` then we can write `U ∈ B` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter_basis α) := ⟨λ U B, U ∈ B.sets⟩ -- For illustration purposes, the filter basis defining (at_top : filter ℕ) instance : inhabited (filter_basis ℕ) := ⟨{ sets := range Ici, nonempty := ⟨Ici 0, mem_range_self 0⟩, inter_sets := begin rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, refine ⟨Ici (max n m), mem_range_self _, _⟩, rintros p p_in, split ; rw mem_Ici at *, exact le_of_max_le_left p_in, exact le_of_max_le_right p_in, end }⟩ /-- `is_basis p s` means the image of `s` bounded by `p` is a filter basis. -/ protected structure filter.is_basis (p : ι → Prop) (s : ι → set α) : Prop := (nonempty : ∃ i, p i) (inter : ∀ {i j}, p i → p j → ∃ k, p k ∧ s k ⊆ s i ∩ s j) namespace filter namespace is_basis /-- Constructs a filter basis from an indexed family of sets satisfying `is_basis`. -/ protected def filter_basis {p : ι → Prop} {s : ι → set α} (h : is_basis p s) : filter_basis α := { sets := {t | ∃ i, p i ∧ s i = t}, nonempty := let ⟨i, hi⟩ := h.nonempty in ⟨s i, ⟨i, hi, rfl⟩⟩, inter_sets := by { rintros _ _ ⟨i, hi, rfl⟩ ⟨j, hj, rfl⟩, rcases h.inter hi hj with ⟨k, hk, hk'⟩, exact ⟨_, ⟨k, hk, rfl⟩, hk'⟩ } } variables {p : ι → Prop} {s : ι → set α} (h : is_basis p s) lemma mem_filter_basis_iff {U : set α} : U ∈ h.filter_basis ↔ ∃ i, p i ∧ s i = U := iff.rfl end is_basis end filter namespace filter_basis /-- The filter associated to a filter basis. -/ protected def filter (B : filter_basis α) : filter α := { sets := {s | ∃ t ∈ B, t ⊆ s}, univ_sets := let ⟨s, s_in⟩ := B.nonempty in ⟨s, s_in, s.subset_univ⟩, sets_of_superset := λ x y ⟨s, s_in, h⟩ hxy, ⟨s, s_in, set.subset.trans h hxy⟩, inter_sets := λ x y ⟨s, s_in, hs⟩ ⟨t, t_in, ht⟩, let ⟨u, u_in, u_sub⟩ := B.inter_sets s_in t_in in ⟨u, u_in, set.subset.trans u_sub $ set.inter_subset_inter hs ht⟩ } lemma mem_filter_iff (B : filter_basis α) {U : set α} : U ∈ B.filter ↔ ∃ s ∈ B, s ⊆ U := iff.rfl lemma mem_filter_of_mem (B : filter_basis α) {U : set α} : U ∈ B → U ∈ B.filter:= λ U_in, ⟨U, U_in, subset.refl _⟩ lemma eq_infi_principal (B : filter_basis α) : B.filter = ⨅ s : B.sets, 𝓟 s := begin have : directed (≥) (λ (s : B.sets), 𝓟 (s : set α)), { rintros ⟨U, U_in⟩ ⟨V, V_in⟩, rcases B.inter_sets U_in V_in with ⟨W, W_in, W_sub⟩, use [W, W_in], finish }, ext U, simp [mem_filter_iff, mem_infi_of_directed this] end protected lemma generate (B : filter_basis α) : generate B.sets = B.filter := begin apply le_antisymm, { intros U U_in, rcases B.mem_filter_iff.mp U_in with ⟨V, V_in, h⟩, exact generate_sets.superset (generate_sets.basic V_in) h }, { rw sets_iff_generate, apply mem_filter_of_mem } end end filter_basis namespace filter namespace is_basis variables {p : ι → Prop} {s : ι → set α} /-- Constructs a filter from an indexed family of sets satisfying `is_basis`. -/ protected def filter (h : is_basis p s) : filter α := h.filter_basis.filter protected lemma mem_filter_iff (h : is_basis p s) {U : set α} : U ∈ h.filter ↔ ∃ i, p i ∧ s i ⊆ U := begin erw [h.filter_basis.mem_filter_iff], simp only [mem_filter_basis_iff h, exists_prop], split, { rintros ⟨_, ⟨i, pi, rfl⟩, h⟩, tauto }, { tauto } end lemma filter_eq_generate (h : is_basis p s) : h.filter = generate {U | ∃ i, p i ∧ s i = U} := by erw h.filter_basis.generate ; refl end is_basis /-- We say that a filter `l` has a basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`. -/ protected structure has_basis (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop := (mem_iff' : ∀ (t : set α), t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t) section same_type variables {l l' : filter α} {p : ι → Prop} {s : ι → set α} {t : set α} {i : ι} {p' : ι' → Prop} {s' : ι' → set α} {i' : ι'} lemma has_basis_generate (s : set (set α)) : (generate s).has_basis (λ t, finite t ∧ t ⊆ s) (λ t, ⋂₀ t) := ⟨begin intro U, rw mem_generate_iff, apply exists_congr, tauto end⟩ /-- The smallest filter basis containing a given collection of sets. -/ def filter_basis.of_sets (s : set (set α)) : filter_basis α := { sets := sInter '' { t | finite t ∧ t ⊆ s}, nonempty := ⟨univ, ∅, ⟨⟨finite_empty, empty_subset s⟩, sInter_empty⟩⟩, inter_sets := begin rintros _ _ ⟨a, ⟨fina, suba⟩, rfl⟩ ⟨b, ⟨finb, subb⟩, rfl⟩, exact ⟨⋂₀ (a ∪ b), mem_image_of_mem _ ⟨fina.union finb, union_subset suba subb⟩, by rw sInter_union⟩, end } /-- Definition of `has_basis` unfolded with implicit set argument. -/ lemma has_basis.mem_iff (hl : l.has_basis p s) : t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := hl.mem_iff' t lemma has_basis.eq_of_same_basis (hl : l.has_basis p s) (hl' : l'.has_basis p s) : l = l' := begin ext t, rw [hl.mem_iff, hl'.mem_iff] end lemma has_basis_iff : l.has_basis p s ↔ ∀ t, t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := ⟨λ ⟨h⟩, h, λ h, ⟨h⟩⟩ lemma has_basis.ex_mem (h : l.has_basis p s) : ∃ i, p i := let ⟨i, pi, h⟩ := h.mem_iff.mp univ_mem in ⟨i, pi⟩ protected lemma has_basis.nonempty (h : l.has_basis p s) : nonempty ι := nonempty_of_exists h.ex_mem protected lemma is_basis.has_basis (h : is_basis p s) : has_basis h.filter p s := ⟨λ t, by simp only [h.mem_filter_iff, exists_prop]⟩ lemma has_basis.mem_of_superset (hl : l.has_basis p s) (hi : p i) (ht : s i ⊆ t) : t ∈ l := (hl.mem_iff).2 ⟨i, hi, ht⟩ lemma has_basis.mem_of_mem (hl : l.has_basis p s) (hi : p i) : s i ∈ l := hl.mem_of_superset hi $ subset.refl _ /-- Index of a basis set such that `s i ⊆ t` as an element of `subtype p`. -/ noncomputable def has_basis.index (h : l.has_basis p s) (t : set α) (ht : t ∈ l) : {i : ι // p i} := ⟨(h.mem_iff.1 ht).some, (h.mem_iff.1 ht).some_spec.fst⟩ lemma has_basis.property_index (h : l.has_basis p s) (ht : t ∈ l) : p (h.index t ht) := (h.index t ht).2 lemma has_basis.set_index_mem (h : l.has_basis p s) (ht : t ∈ l) : s (h.index t ht) ∈ l := h.mem_of_mem $ h.property_index _ lemma has_basis.set_index_subset (h : l.has_basis p s) (ht : t ∈ l) : s (h.index t ht) ⊆ t := (h.mem_iff.1 ht).some_spec.snd lemma has_basis.is_basis (h : l.has_basis p s) : is_basis p s := { nonempty := let ⟨i, hi, H⟩ := h.mem_iff.mp univ_mem in ⟨i, hi⟩, inter := λ i j hi hj, by simpa [h.mem_iff] using l.inter_sets (h.mem_of_mem hi) (h.mem_of_mem hj) } lemma has_basis.filter_eq (h : l.has_basis p s) : h.is_basis.filter = l := by { ext U, simp [h.mem_iff, is_basis.mem_filter_iff] } lemma has_basis.eq_generate (h : l.has_basis p s) : l = generate { U | ∃ i, p i ∧ s i = U } := by rw [← h.is_basis.filter_eq_generate, h.filter_eq] lemma generate_eq_generate_inter (s : set (set α)) : generate s = generate (sInter '' { t | finite t ∧ t ⊆ s}) := by erw [(filter_basis.of_sets s).generate, ← (has_basis_generate s).filter_eq] ; refl lemma of_sets_filter_eq_generate (s : set (set α)) : (filter_basis.of_sets s).filter = generate s := by rw [← (filter_basis.of_sets s).generate, generate_eq_generate_inter s] ; refl lemma has_basis.to_has_basis' (hl : l.has_basis p s) (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → s' i' ∈ l) : l.has_basis p' s' := begin refine ⟨λ t, ⟨λ ht, _, λ ⟨i', hi', ht⟩, mem_of_superset (h' i' hi') ht⟩⟩, rcases hl.mem_iff.1 ht with ⟨i, hi, ht⟩, rcases h i hi with ⟨i', hi', hs's⟩, exact ⟨i', hi', subset.trans hs's ht⟩ end lemma has_basis.to_has_basis (hl : l.has_basis p s) (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → ∃ i, p i ∧ s i ⊆ s' i') : l.has_basis p' s' := hl.to_has_basis' h $ λ i' hi', let ⟨i, hi, hss'⟩ := h' i' hi' in hl.mem_iff.2 ⟨i, hi, hss'⟩ lemma has_basis.to_subset (hl : l.has_basis p s) {t : ι → set α} (h : ∀ i, p i → t i ⊆ s i) (ht : ∀ i, p i → t i ∈ l) : l.has_basis p t := hl.to_has_basis' (λ i hi, ⟨i, hi, h i hi⟩) ht lemma has_basis.eventually_iff (hl : l.has_basis p s) {q : α → Prop} : (∀ᶠ x in l, q x) ↔ ∃ i, p i ∧ ∀ ⦃x⦄, x ∈ s i → q x := by simpa using hl.mem_iff lemma has_basis.frequently_iff (hl : l.has_basis p s) {q : α → Prop} : (∃ᶠ x in l, q x) ↔ ∀ i, p i → ∃ x ∈ s i, q x := by simp [filter.frequently, hl.eventually_iff] lemma has_basis.exists_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P t → P s) : (∃ s ∈ l, P s) ↔ ∃ (i) (hi : p i), P (s i) := ⟨λ ⟨s, hs, hP⟩, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in ⟨i, hi, mono his hP⟩, λ ⟨i, hi, hP⟩, ⟨s i, hl.mem_of_mem hi, hP⟩⟩ lemma has_basis.forall_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P s → P t) : (∀ s ∈ l, P s) ↔ ∀ i, p i → P (s i) := ⟨λ H i hi, H (s i) $ hl.mem_of_mem hi, λ H s hs, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in mono his (H i hi)⟩ lemma has_basis.ne_bot_iff (hl : l.has_basis p s) : ne_bot l ↔ (∀ {i}, p i → (s i).nonempty) := forall_mem_nonempty_iff_ne_bot.symm.trans $ hl.forall_iff $ λ _ _, nonempty.mono lemma has_basis.eq_bot_iff (hl : l.has_basis p s) : l = ⊥ ↔ ∃ i, p i ∧ s i = ∅ := not_iff_not.1 $ ne_bot_iff.symm.trans $ hl.ne_bot_iff.trans $ by simp only [not_exists, not_and, ← ne_empty_iff_nonempty] lemma basis_sets (l : filter α) : l.has_basis (λ s : set α, s ∈ l) id := ⟨λ t, exists_mem_subset_iff.symm⟩ lemma has_basis_self {l : filter α} {P : set α → Prop} : has_basis l (λ s, s ∈ l ∧ P s) id ↔ ∀ t ∈ l, ∃ r ∈ l, P r ∧ r ⊆ t := begin simp only [has_basis_iff, exists_prop, id, and_assoc], exact forall_congr (λ s, ⟨λ h, h.1, λ h, ⟨h, λ ⟨t, hl, hP, hts⟩, mem_of_superset hl hts⟩⟩) end /-- If `{s i | p i}` is a basis of a filter `l` and each `s i` includes `s j` such that `p j ∧ q j`, then `{s j | p j ∧ q j}` is a basis of `l`. -/ lemma has_basis.restrict (h : l.has_basis p s) {q : ι → Prop} (hq : ∀ i, p i → ∃ j, p j ∧ q j ∧ s j ⊆ s i) : l.has_basis (λ i, p i ∧ q i) s := begin refine ⟨λ t, ⟨λ ht, _, λ ⟨i, hpi, hti⟩, h.mem_iff.2 ⟨i, hpi.1, hti⟩⟩⟩, rcases h.mem_iff.1 ht with ⟨i, hpi, hti⟩, rcases hq i hpi with ⟨j, hpj, hqj, hji⟩, exact ⟨j, ⟨hpj, hqj⟩, subset.trans hji hti⟩ end /-- If `{s i | p i}` is a basis of a filter `l` and `V ∈ l`, then `{s i | p i ∧ s i ⊆ V}` is a basis of `l`. -/ lemma has_basis.restrict_subset (h : l.has_basis p s) {V : set α} (hV : V ∈ l) : l.has_basis (λ i, p i ∧ s i ⊆ V) s := h.restrict $ λ i hi, (h.mem_iff.1 (inter_mem hV (h.mem_of_mem hi))).imp $ λ j hj, ⟨hj.fst, subset_inter_iff.1 hj.snd⟩ lemma has_basis.has_basis_self_subset {p : set α → Prop} (h : l.has_basis (λ s, s ∈ l ∧ p s) id) {V : set α} (hV : V ∈ l) : l.has_basis (λ s, s ∈ l ∧ p s ∧ s ⊆ V) id := by simpa only [and_assoc] using h.restrict_subset hV theorem has_basis.ge_iff (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → s' i' ∈ l := ⟨λ h i' hi', h $ hl'.mem_of_mem hi', λ h s hs, let ⟨i', hi', hs⟩ := hl'.mem_iff.1 hs in mem_of_superset (h _ hi') hs⟩ theorem has_basis.le_iff (hl : l.has_basis p s) : l ≤ l' ↔ ∀ t ∈ l', ∃ i (hi : p i), s i ⊆ t := by simp only [le_def, hl.mem_iff] theorem has_basis.le_basis_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → ∃ i (hi : p i), s i ⊆ s' i' := by simp only [hl'.ge_iff, hl.mem_iff] lemma has_basis.ext (hl : l.has_basis p s) (hl' : l'.has_basis p' s') (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → ∃ i, p i ∧ s i ⊆ s' i') : l = l' := begin apply le_antisymm, { rw hl.le_basis_iff hl', simpa using h' }, { rw hl'.le_basis_iff hl, simpa using h }, end lemma has_basis.inf' (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : pprod ι ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := ⟨begin intro t, split, { simp only [mem_inf_iff, exists_prop, hl.mem_iff, hl'.mem_iff], rintros ⟨t, ⟨i, hi, ht⟩, t', ⟨i', hi', ht'⟩, rfl⟩, use [⟨i, i'⟩, ⟨hi, hi'⟩, inter_subset_inter ht ht'] }, { rintros ⟨⟨i, i'⟩, ⟨hi, hi'⟩, H⟩, exact mem_inf_of_inter (hl.mem_of_mem hi) (hl'.mem_of_mem hi') H } end⟩ lemma has_basis.inf {ι ι' : Type*} {p : ι → Prop} {s : ι → set α} {p' : ι' → Prop} {s' : ι' → set α} (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := (hl.inf' hl').to_has_basis (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) lemma has_basis_principal (t : set α) : (𝓟 t).has_basis (λ i : unit, true) (λ i, t) := ⟨λ U, by simp⟩ lemma has_basis.sup' (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊔ l').has_basis (λ i : pprod ι ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∪ s' i.2) := ⟨begin intros t, simp only [mem_sup, hl.mem_iff, hl'.mem_iff, pprod.exists, union_subset_iff, exists_prop, and_assoc, exists_and_distrib_left], simp only [← and_assoc, exists_and_distrib_right, and_comm] end⟩ lemma has_basis.sup {ι ι' : Type*} {p : ι → Prop} {s : ι → set α} {p' : ι' → Prop} {s' : ι' → set α} (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊔ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∪ s' i.2) := (hl.sup' hl').to_has_basis (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) lemma has_basis_supr {ι : Sort*} {ι' : ι → Type*} {l : ι → filter α} {p : Π i, ι' i → Prop} {s : Π i, ι' i → set α} (hl : ∀ i, (l i).has_basis (p i) (s i)) : (⨆ i, l i).has_basis (λ f : Π i, ι' i, ∀ i, p i (f i)) (λ f : Π i, ι' i, ⋃ i, s i (f i)) := has_basis_iff.mpr $ λ t, by simp only [has_basis_iff, (hl _).mem_iff, classical.skolem, forall_and_distrib, Union_subset_iff, mem_supr] lemma has_basis.inf_principal (hl : l.has_basis p s) (s' : set α) : (l ⊓ 𝓟 s').has_basis p (λ i, s i ∩ s') := ⟨λ t, by simp only [mem_inf_principal, hl.mem_iff, subset_def, mem_set_of_eq, mem_inter_iff, and_imp]⟩ lemma has_basis.inf_basis_ne_bot_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : ne_bot (l ⊓ l') ↔ ∀ ⦃i⦄ (hi : p i) ⦃i'⦄ (hi' : p' i'), (s i ∩ s' i').nonempty := (hl.inf' hl').ne_bot_iff.trans $ by simp [@forall_swap _ ι'] lemma has_basis.inf_ne_bot_iff (hl : l.has_basis p s) : ne_bot (l ⊓ l') ↔ ∀ ⦃i⦄ (hi : p i) ⦃s'⦄ (hs' : s' ∈ l'), (s i ∩ s').nonempty := hl.inf_basis_ne_bot_iff l'.basis_sets lemma has_basis.inf_principal_ne_bot_iff (hl : l.has_basis p s) {t : set α} : ne_bot (l ⊓ 𝓟 t) ↔ ∀ ⦃i⦄ (hi : p i), (s i ∩ t).nonempty := (hl.inf_principal t).ne_bot_iff lemma inf_ne_bot_iff : ne_bot (l ⊓ l') ↔ ∀ ⦃s : set α⦄ (hs : s ∈ l) ⦃s'⦄ (hs' : s' ∈ l'), (s ∩ s').nonempty := l.basis_sets.inf_ne_bot_iff lemma inf_principal_ne_bot_iff {s : set α} : ne_bot (l ⊓ 𝓟 s) ↔ ∀ U ∈ l, (U ∩ s).nonempty := l.basis_sets.inf_principal_ne_bot_iff lemma inf_eq_bot_iff {f g : filter α} : f ⊓ g = ⊥ ↔ ∃ (U ∈ f) (V ∈ g), U ∩ V = ∅ := not_iff_not.1 $ ne_bot_iff.symm.trans $ inf_ne_bot_iff.trans $ by simp [← ne_empty_iff_nonempty] protected lemma disjoint_iff {f g : filter α} : disjoint f g ↔ ∃ (U ∈ f) (V ∈ g), U ∩ V = ∅ := disjoint_iff.trans inf_eq_bot_iff lemma mem_iff_inf_principal_compl {f : filter α} {s : set α} : s ∈ f ↔ f ⊓ 𝓟 sᶜ = ⊥ := begin refine not_iff_not.1 ((inf_principal_ne_bot_iff.trans _).symm.trans ne_bot_iff), exact ⟨λ h hs, by simpa [empty_not_nonempty] using h s hs, λ hs t ht, inter_compl_nonempty_iff.2 $ λ hts, hs $ mem_of_superset ht hts⟩, end lemma not_mem_iff_inf_principal_compl {f : filter α} {s : set α} : s ∉ f ↔ ne_bot (f ⊓ 𝓟 sᶜ) := (not_congr mem_iff_inf_principal_compl).trans ne_bot_iff.symm lemma mem_iff_disjoint_principal_compl {f : filter α} {s : set α} : s ∈ f ↔ disjoint f (𝓟 sᶜ) := mem_iff_inf_principal_compl.trans disjoint_iff.symm lemma le_iff_forall_disjoint_principal_compl {f g : filter α} : f ≤ g ↔ ∀ V ∈ g, disjoint f (𝓟 Vᶜ) := forall_congr $ λ _, forall_congr $ λ _, mem_iff_disjoint_principal_compl lemma le_iff_forall_inf_principal_compl {f g : filter α} : f ≤ g ↔ ∀ V ∈ g, f ⊓ 𝓟 Vᶜ = ⊥ := forall_congr $ λ _, forall_congr $ λ _, mem_iff_inf_principal_compl lemma inf_ne_bot_iff_frequently_left {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x := by simpa only [inf_ne_bot_iff, frequently_iff, exists_prop, and_comm] lemma inf_ne_bot_iff_frequently_right {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x := by { rw inf_comm, exact inf_ne_bot_iff_frequently_left } lemma has_basis.eq_binfi (h : l.has_basis p s) : l = ⨅ i (_ : p i), 𝓟 (s i) := eq_binfi_of_mem_iff_exists_mem $ λ t, by simp only [h.mem_iff, mem_principal] lemma has_basis.eq_infi (h : l.has_basis (λ _, true) s) : l = ⨅ i, 𝓟 (s i) := by simpa only [infi_true] using h.eq_binfi lemma has_basis_infi_principal {s : ι → set α} (h : directed (≥) s) [nonempty ι] : (⨅ i, 𝓟 (s i)).has_basis (λ _, true) s := ⟨begin refine λ t, (mem_infi_of_directed (h.mono_comp _ _) t).trans $ by simp only [exists_prop, true_and, mem_principal], exact λ _ _, principal_mono.2 end⟩ /-- If `s : ι → set α` is an indexed family of sets, then finite intersections of `s i` form a basis of `⨅ i, 𝓟 (s i)`. -/ lemma has_basis_infi_principal_finite {ι : Type*} (s : ι → set α) : (⨅ i, 𝓟 (s i)).has_basis (λ t : set ι, finite t) (λ t, ⋂ i ∈ t, s i) := begin refine ⟨λ U, (mem_infi_finite _).trans _⟩, simp only [infi_principal_finset, mem_Union, mem_principal, exists_prop, exists_finite_iff_finset, finset.set_bInter_coe] end lemma has_basis_binfi_principal {s : β → set α} {S : set β} (h : directed_on (s ⁻¹'o (≥)) S) (ne : S.nonempty) : (⨅ i ∈ S, 𝓟 (s i)).has_basis (λ i, i ∈ S) s := ⟨begin refine λ t, (mem_binfi_of_directed _ ne).trans $ by simp only [mem_principal], rw [directed_on_iff_directed, ← directed_comp, (∘)] at h ⊢, apply h.mono_comp _ _, exact λ _ _, principal_mono.2 end⟩ lemma has_basis_binfi_principal' {ι : Type*} {p : ι → Prop} {s : ι → set α} (h : ∀ i, p i → ∀ j, p j → ∃ k (h : p k), s k ⊆ s i ∧ s k ⊆ s j) (ne : ∃ i, p i) : (⨅ i (h : p i), 𝓟 (s i)).has_basis p s := filter.has_basis_binfi_principal h ne lemma has_basis.map (f : α → β) (hl : l.has_basis p s) : (l.map f).has_basis p (λ i, f '' (s i)) := ⟨λ t, by simp only [mem_map, image_subset_iff, hl.mem_iff, preimage]⟩ lemma has_basis.comap (f : β → α) (hl : l.has_basis p s) : (l.comap f).has_basis p (λ i, f ⁻¹' (s i)) := ⟨begin intro t, simp only [mem_comap, exists_prop, hl.mem_iff], split, { rintros ⟨t', ⟨i, hi, ht'⟩, H⟩, exact ⟨i, hi, subset.trans (preimage_mono ht') H⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, ⟨i, hi, subset.refl _⟩, H⟩ } end⟩ lemma comap_has_basis (f : α → β) (l : filter β) : has_basis (comap f l) (λ s : set β, s ∈ l) (λ s, f ⁻¹' s) := ⟨λ t, mem_comap⟩ lemma has_basis.prod_self (hl : l.has_basis p s) : (l ×ᶠ l).has_basis p (λ i, (s i).prod (s i)) := ⟨begin intro t, apply mem_prod_iff.trans, split, { rintros ⟨t₁, ht₁, t₂, ht₂, H⟩, rcases hl.mem_iff.1 (inter_mem ht₁ ht₂) with ⟨i, hi, ht⟩, exact ⟨i, hi, λ p ⟨hp₁, hp₂⟩, H ⟨(ht hp₁).1, (ht hp₂).2⟩⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, hl.mem_of_mem hi, s i, hl.mem_of_mem hi, H⟩ } end⟩ lemma mem_prod_self_iff {s} : s ∈ l ×ᶠ l ↔ ∃ t ∈ l, set.prod t t ⊆ s := l.basis_sets.prod_self.mem_iff lemma has_basis.sInter_sets (h : has_basis l p s) : ⋂₀ l.sets = ⋂ i (hi : p i), s i := begin ext x, suffices : (∀ t ∈ l, x ∈ t) ↔ ∀ i, p i → x ∈ s i, by simpa only [mem_Inter, mem_set_of_eq, mem_sInter], simp_rw h.mem_iff, split, { intros h i hi, exact h (s i) ⟨i, hi, subset.refl _⟩ }, { rintros h _ ⟨i, hi, sub⟩, exact sub (h i hi) }, end variables {ι'' : Type*} [preorder ι''] (l) (p'' : ι'' → Prop) (s'' : ι'' → set α) /-- `is_antimono_basis p s` means the image of `s` bounded by `p` is a filter basis such that `s` is decreasing and `p` is increasing, ie `i ≤ j → p i → p j`. -/ structure is_antimono_basis extends is_basis p'' s'' : Prop := (decreasing : ∀ {i j}, p'' i → p'' j → i ≤ j → s'' j ⊆ s'' i) (mono : monotone p'') /-- We say that a filter `l` has a antimono basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`, and `s` is decreasing and `p` is increasing, ie `i ≤ j → p i → p j`. -/ structure has_antimono_basis (l : filter α) (p : ι'' → Prop) (s : ι'' → set α) extends has_basis l p s : Prop := (decreasing : ∀ {i j}, p i → p j → i ≤ j → s j ⊆ s i) (mono : monotone p) end same_type section two_types variables {la : filter α} {pa : ι → Prop} {sa : ι → set α} {lb : filter β} {pb : ι' → Prop} {sb : ι' → set β} {f : α → β} lemma has_basis.tendsto_left_iff (hla : la.has_basis pa sa) : tendsto f la lb ↔ ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := by { simp only [tendsto, (hla.map f).le_iff, image_subset_iff], refl } lemma has_basis.tendsto_right_iff (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := by simpa only [tendsto, hlb.ge_iff, mem_map, filter.eventually] lemma has_basis.tendsto_iff (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := by simp [hlb.tendsto_right_iff, hla.eventually_iff] lemma tendsto.basis_left (H : tendsto f la lb) (hla : la.has_basis pa sa) : ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := hla.tendsto_left_iff.1 H lemma tendsto.basis_right (H : tendsto f la lb) (hlb : lb.has_basis pb sb) : ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := hlb.tendsto_right_iff.1 H lemma tendsto.basis_both (H : tendsto f la lb) (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := (hla.tendsto_iff hlb).1 H lemma has_basis.prod'' (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la ×ᶠ lb).has_basis (λ i : pprod ι ι', pa i.1 ∧ pb i.2) (λ i, (sa i.1).prod (sb i.2)) := (hla.comap prod.fst).inf' (hlb.comap prod.snd) lemma has_basis.prod {ι ι' : Type*} {pa : ι → Prop} {sa : ι → set α} {pb : ι' → Prop} {sb : ι' → set β} (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la ×ᶠ lb).has_basis (λ i : ι × ι', pa i.1 ∧ pb i.2) (λ i, (sa i.1).prod (sb i.2)) := (hla.comap prod.fst).inf (hlb.comap prod.snd) lemma has_basis.prod' {la : filter α} {lb : filter β} {ι : Type*} {p : ι → Prop} {sa : ι → set α} {sb : ι → set β} (hla : la.has_basis p sa) (hlb : lb.has_basis p sb) (h_dir : ∀ {i j}, p i → p j → ∃ k, p k ∧ sa k ⊆ sa i ∧ sb k ⊆ sb j) : (la ×ᶠ lb).has_basis p (λ i, (sa i).prod (sb i)) := begin simp only [has_basis_iff, (hla.prod hlb).mem_iff], refine λ t, ⟨_, _⟩, { rintros ⟨⟨i, j⟩, ⟨hi, hj⟩, hsub : (sa i).prod (sb j) ⊆ t⟩, rcases h_dir hi hj with ⟨k, hk, ki, kj⟩, exact ⟨k, hk, (set.prod_mono ki kj).trans hsub⟩ }, { rintro ⟨i, hi, h⟩, exact ⟨⟨i, i⟩, ⟨hi, hi⟩, h⟩ }, end end two_types end filter end sort namespace filter variables {α β γ ι ι' : Type*} /-- `is_countably_generated f` means `f = generate s` for some countable `s`. -/ def is_countably_generated (f : filter α) : Prop := ∃ s : set (set α), countable s ∧ f = generate s /-- `is_countable_basis p s` means the image of `s` bounded by `p` is a countable filter basis. -/ structure is_countable_basis (p : ι → Prop) (s : ι → set α) extends is_basis p s : Prop := (countable : countable $ set_of p) /-- We say that a filter `l` has a countable basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`, and the set defined by `p` is countable. -/ structure has_countable_basis (l : filter α) (p : ι → Prop) (s : ι → set α) extends has_basis l p s : Prop := (countable : countable $ set_of p) /-- A countable filter basis `B` on a type `α` is a nonempty countable collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure countable_filter_basis (α : Type*) extends filter_basis α := (countable : countable sets) -- For illustration purposes, the countable filter basis defining (at_top : filter ℕ) instance nat.inhabited_countable_filter_basis : inhabited (countable_filter_basis ℕ) := ⟨{ countable := countable_range (λ n, Ici n), ..(default $ filter_basis ℕ),}⟩ lemma antimono_seq_of_seq (s : ℕ → set α) : ∃ t : ℕ → set α, (∀ i j, i ≤ j → t j ⊆ t i) ∧ (⨅ i, 𝓟 $ s i) = ⨅ i, 𝓟 (t i) := begin use λ n, ⋂ m ≤ n, s m, split, { exact λ i j hij, bInter_mono' (Iic_subset_Iic.2 hij) (λ n hn, subset.refl _) }, apply le_antisymm; rw le_infi_iff; intro i, { rw le_principal_iff, refine (bInter_mem (finite_le_nat _)).2 (λ j hji, _), rw ← le_principal_iff, apply infi_le_of_le j _, apply le_refl _ }, { apply infi_le_of_le i _, rw principal_mono, intro a, simp, intro h, apply h, refl }, end lemma countable_binfi_eq_infi_seq [complete_lattice α] {B : set ι} (Bcbl : countable B) (Bne : B.nonempty) (f : ι → α) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := begin rw countable_iff_exists_surjective_to_subtype Bne at Bcbl, rcases Bcbl with ⟨g, gsurj⟩, rw infi_subtype', use (λ n, g n), apply le_antisymm; rw le_infi_iff, { intro i, apply infi_le_of_le (g i) _, apply le_refl _ }, { intros a, rcases gsurj a with ⟨i, rfl⟩, apply infi_le } end lemma countable_binfi_eq_infi_seq' [complete_lattice α] {B : set ι} (Bcbl : countable B) (f : ι → α) {i₀ : ι} (h : f i₀ = ⊤) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := begin cases B.eq_empty_or_nonempty with hB Bnonempty, { rw [hB, infi_emptyset], use λ n, i₀, simp [h] }, { exact countable_binfi_eq_infi_seq Bcbl Bnonempty f } end lemma countable_binfi_principal_eq_seq_infi {B : set (set α)} (Bcbl : countable B) : ∃ (x : ℕ → set α), (⨅ t ∈ B, 𝓟 t) = ⨅ i, 𝓟 (x i) := countable_binfi_eq_infi_seq' Bcbl 𝓟 principal_univ namespace is_countably_generated /-- A set generating a countably generated filter. -/ def generating_set {f : filter α} (h : is_countably_generated f) := classical.some h lemma countable_generating_set {f : filter α} (h : is_countably_generated f) : countable h.generating_set := (classical.some_spec h).1 lemma eq_generate {f : filter α} (h : is_countably_generated f) : f = generate h.generating_set := (classical.some_spec h).2 /-- A countable filter basis for a countably generated filter. -/ def countable_filter_basis {l : filter α} (h : is_countably_generated l) : countable_filter_basis α := { countable := (countable_set_of_finite_subset h.countable_generating_set).image _, ..filter_basis.of_sets (h.generating_set) } lemma filter_basis_filter {l : filter α} (h : is_countably_generated l) : h.countable_filter_basis.to_filter_basis.filter = l := begin conv_rhs { rw h.eq_generate }, apply of_sets_filter_eq_generate, end lemma has_countable_basis {l : filter α} (h : is_countably_generated l) : l.has_countable_basis (λ t, finite t ∧ t ⊆ h.generating_set) (λ t, ⋂₀ t) := ⟨by convert has_basis_generate _ ; exact h.eq_generate, countable_set_of_finite_subset h.countable_generating_set⟩ lemma exists_countable_infi_principal {f : filter α} (h : f.is_countably_generated) : ∃ s : set (set α), countable s ∧ f = ⨅ t ∈ s, 𝓟 t := begin let B := h.countable_filter_basis, use [B.sets, B.countable], rw ← h.filter_basis_filter, rw B.to_filter_basis.eq_infi_principal, rw infi_subtype'' end lemma exists_seq {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 (x i) := begin rcases cblb.exists_countable_infi_principal with ⟨B, Bcbl, rfl⟩, exact countable_binfi_principal_eq_seq_infi Bcbl, end /-- If `f` is countably generated and `f.has_basis p s`, then `f` admits a decreasing basis enumerated by natural numbers such that all sets have the form `s i`. More precisely, there is a sequence `i n` such that `p (i n)` for all `n` and `s (i n)` is a decreasing sequence of sets which forms a basis of `f`-/ lemma exists_antimono_subbasis {f : filter α} (cblb : f.is_countably_generated) {p : ι → Prop} {s : ι → set α} (hs : f.has_basis p s) : ∃ x : ℕ → ι, (∀ i, p (x i)) ∧ f.has_antimono_basis (λ _, true) (λ i, s (x i)) := begin rcases cblb.exists_seq with ⟨x', hx'⟩, have : ∀ i, x' i ∈ f := λ i, hx'.symm ▸ (infi_le (λ i, 𝓟 (x' i)) i) (mem_principal_self _), let x : ℕ → {i : ι // p i} := λ n, nat.rec_on n (hs.index _ $ this 0) (λ n xn, (hs.index _ $ inter_mem (this $ n + 1) (hs.mem_of_mem xn.coe_prop))), have x_mono : ∀ n : ℕ, s (x n.succ) ⊆ s (x n) := λ n, subset.trans (hs.set_index_subset _) (inter_subset_right _ _), replace x_mono : ∀ ⦃i j⦄, i ≤ j → s (x j) ≤ s (x i), { refine @monotone_nat_of_le_succ (order_dual $ set α) _ _ _, exact x_mono }, have x_subset : ∀ i, s (x i) ⊆ x' i, { rintro (_|i), exacts [hs.set_index_subset _, subset.trans (hs.set_index_subset _) (inter_subset_left _ _)] }, refine ⟨λ i, x i, λ i, (x i).2, _⟩, have : (⨅ i, 𝓟 (s (x i))).has_antimono_basis (λ _, true) (λ i, s (x i)) := ⟨has_basis_infi_principal (directed_of_sup x_mono), λ i j _ _ hij, x_mono hij, monotone_const⟩, convert this, exact le_antisymm (le_infi $ λ i, le_principal_iff.2 $ by cases i; apply hs.set_index_mem) (hx'.symm ▸ le_infi (λ i, le_principal_iff.2 $ this.to_has_basis.mem_iff.2 ⟨i, trivial, x_subset i⟩)) end /-- A countably generated filter admits a basis formed by a monotonically decreasing sequence of sets. -/ lemma exists_antimono_basis {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, f.has_antimono_basis (λ _, true) x := let ⟨x, hxf, hx⟩ := cblb.exists_antimono_subbasis f.basis_sets in ⟨x, hx⟩ end is_countably_generated lemma has_countable_basis.is_countably_generated {f : filter α} {p : ι → Prop} {s : ι → set α} (h : f.has_countable_basis p s) : f.is_countably_generated := ⟨{t | ∃ i, p i ∧ s i = t}, h.countable.image s, h.to_has_basis.eq_generate⟩ lemma is_countably_generated_seq (x : ℕ → set α) : is_countably_generated (⨅ i, 𝓟 $ x i) := begin rcases antimono_seq_of_seq x with ⟨y, am, h⟩, rw h, use [range y, countable_range _], rw (has_basis_infi_principal _).eq_generate, { simp [range] }, { exact directed_of_sup am }, { use 0 }, end lemma is_countably_generated_of_seq {f : filter α} (h : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 $ x i) : f.is_countably_generated := let ⟨x, h⟩ := h in by rw h ; apply is_countably_generated_seq lemma is_countably_generated_binfi_principal {B : set $ set α} (h : countable B) : is_countably_generated (⨅ (s ∈ B), 𝓟 s) := is_countably_generated_of_seq (countable_binfi_principal_eq_seq_infi h) lemma is_countably_generated_iff_exists_antimono_basis {f : filter α} : is_countably_generated f ↔ ∃ x : ℕ → set α, f.has_antimono_basis (λ _, true) x := begin split, { exact λ h, h.exists_antimono_basis }, { rintros ⟨x, h⟩, rw h.to_has_basis.eq_infi, exact is_countably_generated_seq x }, end lemma is_countably_generated_principal (s : set α) : is_countably_generated (𝓟 s) := begin rw show 𝓟 s = ⨅ i : ℕ, 𝓟 s, by simp, apply is_countably_generated_seq end namespace is_countably_generated lemma inf {f g : filter α} (hf : is_countably_generated f) (hg : is_countably_generated g) : is_countably_generated (f ⊓ g) := begin rw is_countably_generated_iff_exists_antimono_basis at hf hg, rcases hf with ⟨s, hs⟩, rcases hg with ⟨t, ht⟩, exact has_countable_basis.is_countably_generated ⟨hs.to_has_basis.inf ht.to_has_basis, set.countable_encodable _⟩ end lemma inf_principal {f : filter α} (h : is_countably_generated f) (s : set α) : is_countably_generated (f ⊓ 𝓟 s) := h.inf (filter.is_countably_generated_principal s) lemma exists_antimono_seq' {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, (∀ i j, i ≤ j → x j ⊆ x i) ∧ ∀ {s}, (s ∈ f ↔ ∃ i, x i ⊆ s) := let ⟨x, hx⟩ := is_countably_generated_iff_exists_antimono_basis.mp cblb in ⟨x, λ i j, hx.decreasing trivial trivial, λ s, by simp [hx.to_has_basis.mem_iff]⟩ protected lemma comap {l : filter β} (h : l.is_countably_generated) (f : α → β) : (comap f l).is_countably_generated := let ⟨x, hx_mono⟩ := h.exists_antimono_basis in is_countably_generated_of_seq ⟨_, (hx_mono.to_has_basis.comap _).eq_infi⟩ end is_countably_generated end filter