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
1a95afa3aeba5cd53cb5d0ecb0950383969a530c
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/dense_embedding.lean
2757c5e9dcd461dc18fcc02a2ebbde3a78df9542
[ "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
13,600
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.separation import topology.bases /-! # Dense embeddings This file defines three properties of functions: * `dense_range f` means `f` has dense image; * `dense_inducing i` means `i` is also `inducing`; * `dense_embedding e` means `e` is also an `embedding`. The main theorem `continuous_extend` gives a criterion for a function `f : X → Z` to a regular (T₃) space Z to extend along a dense embedding `i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only has to be `dense_inducing` (not necessarily injective). -/ noncomputable theory open set filter open_locale classical topological_space filter variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α` is the one induced by `i` from the topology on `β`. -/ @[protect_proj] structure dense_inducing [topological_space α] [topological_space β] (i : α → β) extends inducing i : Prop := (dense : dense_range i) namespace dense_inducing variables [topological_space α] [topological_space β] variables {i : α → β} (di : dense_inducing i) lemma nhds_eq_comap (di : dense_inducing i) : ∀ a : α, 𝓝 a = comap i (𝓝 $ i a) := di.to_inducing.nhds_eq_comap protected lemma continuous (di : dense_inducing i) : continuous i := di.to_inducing.continuous lemma closure_range : closure (range i) = univ := di.dense.closure_range lemma preconnected_space [preconnected_space α] (di : dense_inducing i) : preconnected_space β := di.dense.preconnected_space di.continuous lemma closure_image_mem_nhds {s : set α} {a : α} (di : dense_inducing i) (hs : s ∈ 𝓝 a) : closure (i '' s) ∈ 𝓝 (i a) := begin rw [di.nhds_eq_comap a, ((nhds_basis_opens _).comap _).mem_iff] at hs, rcases hs with ⟨U, ⟨haU, hUo⟩, sub : i ⁻¹' U ⊆ s⟩, refine mem_of_superset (hUo.mem_nhds haU) _, calc U ⊆ closure (i '' (i ⁻¹' U)) : di.dense.subset_closure_image_preimage_of_is_open hUo ... ⊆ closure (i '' s) : closure_mono (image_subset i sub) end lemma dense_image (di : dense_inducing i) {s : set α} : dense (i '' s) ↔ dense s := begin refine ⟨λ H x, _, di.dense.dense_image di.continuous⟩, rw [di.to_inducing.closure_eq_preimage_closure_image, H.closure_eq, preimage_univ], trivial end /-- The product of two dense inducings is a dense inducing -/ protected lemma prod [topological_space γ] [topological_space δ] {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) : dense_inducing (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { induced := (de₁.to_inducing.prod_mk de₂.to_inducing).induced, dense := de₁.dense.prod_map de₂.dense } open topological_space /-- If the domain of a `dense_inducing` map is a separable space, then so is the codomain. -/ protected lemma separable_space [separable_space α] : separable_space β := di.dense.separable_space di.continuous variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β} /-- γ -f→ α g↓ ↓e δ -h→ β -/ lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (di : dense_inducing i) (H : tendsto h (𝓝 d) (𝓝 (i a))) (comm : h ∘ g = i ∘ f) : tendsto f (comap g (𝓝 d)) (𝓝 a) := begin have lim1 : map g (comap g (𝓝 d)) ≤ 𝓝 d := map_comap_le, replace lim1 : map h (map g (comap g (𝓝 d))) ≤ map h (𝓝 d) := map_mono lim1, rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1, have lim2 : comap i (map h (𝓝 d)) ≤ comap i (𝓝 (i a)) := comap_mono H, rw ← di.nhds_eq_comap at lim2, exact le_trans lim1 lim2, end protected lemma nhds_within_ne_bot (di : dense_inducing i) (b : β) : ne_bot (𝓝[range i] b) := di.dense.nhds_within_ne_bot b lemma comap_nhds_ne_bot (di : dense_inducing i) (b : β) : ne_bot (comap i (𝓝 b)) := comap_ne_bot $ λ s hs, let ⟨_, ⟨ha, a, rfl⟩⟩ := mem_closure_iff_nhds.1 (di.dense b) s hs in ⟨a, ha⟩ variables [topological_space γ] /-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends" to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a continuous extension, then `g` is the unique such extension. In general, `g` might not be continuous or even extend `f`. -/ def extend (di : dense_inducing i) (f : α → γ) (b : β) : γ := @@lim _ ⟨f (di.dense.some b)⟩ (comap i (𝓝 b)) f lemma extend_eq_of_tendsto [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : tendsto f (comap i (𝓝 b)) (𝓝 c)) : di.extend f b = c := by haveI := di.comap_nhds_ne_bot; exact hf.lim_eq lemma extend_eq_at [t2_space γ] {f : α → γ} {a : α} (hf : continuous_at f a) : di.extend f (i a) = f a := extend_eq_of_tendsto _ $ di.nhds_eq_comap a ▸ hf lemma extend_eq_at' [t2_space γ] {f : α → γ} {a : α} (c : γ) (hf : tendsto f (𝓝 a) (𝓝 c)) : di.extend f (i a) = f a := di.extend_eq_at (continuous_at_of_tendsto_nhds hf) lemma extend_eq [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) : di.extend f (i a) = f a := di.extend_eq_at hf.continuous_at /-- Variation of `extend_eq` where we ask that `f` has a limit along `comap i (𝓝 b)` for each `b : β`. This is a strictly stronger assumption than continuity of `f`, but in a lot of cases you'd have to prove it anyway to use `continuous_extend`, so this avoids doing the work twice. -/ lemma extend_eq' [t2_space γ] {f : α → γ} (di : dense_inducing i) (hf : ∀ b, ∃ c, tendsto f (comap i (𝓝 b)) (𝓝 c)) (a : α) : di.extend f (i a) = f a := begin rcases hf (i a) with ⟨b, hb⟩, refine di.extend_eq_at' b _, rwa ← di.to_inducing.nhds_eq_comap at hb, end lemma extend_unique_at [t2_space γ] {b : β} {f : α → γ} {g : β → γ} (di : dense_inducing i) (hf : ∀ᶠ x in comap i (𝓝 b), g (i x) = f x) (hg : continuous_at g b) : di.extend f b = g b := begin refine di.extend_eq_of_tendsto (λ s hs, mem_map.2 _), suffices : ∀ᶠ (x : α) in comap i (𝓝 b), g (i x) ∈ s, from hf.mp (this.mono $ λ x hgx hfx, hfx ▸ hgx), clear hf f, refine eventually_comap.2 ((hg.eventually hs).mono _), rintros _ hxs x rfl, exact hxs end lemma extend_unique [t2_space γ] {f : α → γ} {g : β → γ} (di : dense_inducing i) (hf : ∀ x, g (i x) = f x) (hg : continuous g) : di.extend f = g := funext $ λ b, extend_unique_at di (eventually_of_forall hf) hg.continuous_at lemma continuous_at_extend [regular_space γ] {b : β} {f : α → γ} (di : dense_inducing i) (hf : ∀ᶠ x in 𝓝 b, ∃c, tendsto f (comap i $ 𝓝 x) (𝓝 c)) : continuous_at (di.extend f) b := begin set φ := di.extend f, haveI := di.comap_nhds_ne_bot, suffices : ∀ V' ∈ 𝓝 (φ b), is_closed V' → φ ⁻¹' V' ∈ 𝓝 b, by simpa [continuous_at, (closed_nhds_basis _).tendsto_right_iff], intros V' V'_in V'_closed, set V₁ := {x | tendsto f (comap i $ 𝓝 x) (𝓝 $ φ x)}, have V₁_in : V₁ ∈ 𝓝 b, { filter_upwards [hf], rintros x ⟨c, hc⟩, dsimp [V₁, φ], rwa di.extend_eq_of_tendsto hc }, obtain ⟨V₂, V₂_in, V₂_op, hV₂⟩ : ∃ V₂ ∈ 𝓝 b, is_open V₂ ∧ ∀ x ∈ i ⁻¹' V₂, f x ∈ V', { simpa [and_assoc] using ((nhds_basis_opens' b).comap i).tendsto_left_iff.mp (mem_of_mem_nhds V₁_in : b ∈ V₁) V' V'_in }, suffices : ∀ x ∈ V₁ ∩ V₂, φ x ∈ V', { filter_upwards [inter_mem V₁_in V₂_in] using this, }, rintros x ⟨x_in₁, x_in₂⟩, have hV₂x : V₂ ∈ 𝓝 x := is_open.mem_nhds V₂_op x_in₂, apply V'_closed.mem_of_tendsto x_in₁, use V₂, tauto, end lemma continuous_extend [regular_space γ] {f : α → γ} (di : dense_inducing i) (hf : ∀b, ∃c, tendsto f (comap i (𝓝 b)) (𝓝 c)) : continuous (di.extend f) := continuous_iff_continuous_at.mpr $ assume b, di.continuous_at_extend $ univ_mem' hf lemma mk' (i : α → β) (c : continuous i) (dense : ∀x, x ∈ closure (range i)) (H : ∀ (a:α) s ∈ 𝓝 a, ∃t ∈ 𝓝 (i a), ∀ b, i b ∈ t → b ∈ s) : dense_inducing i := { induced := (induced_iff_nhds_eq i).2 $ λ a, le_antisymm (tendsto_iff_comap.1 $ c.tendsto _) (by simpa [filter.le_def] using H a), dense := dense } end dense_inducing /-- A dense embedding is an embedding with dense image. -/ structure dense_embedding [topological_space α] [topological_space β] (e : α → β) extends dense_inducing e : Prop := (inj : function.injective e) theorem dense_embedding.mk' [topological_space α] [topological_space β] (e : α → β) (c : continuous e) (dense : dense_range e) (inj : function.injective e) (H : ∀ (a:α) s ∈ 𝓝 a, ∃t ∈ 𝓝 (e a), ∀ b, e b ∈ t → b ∈ s) : dense_embedding e := { inj := inj, ..dense_inducing.mk' e c dense H} namespace dense_embedding open topological_space variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] variables {e : α → β} (de : dense_embedding e) lemma inj_iff {x y} : e x = e y ↔ x = y := de.inj.eq_iff lemma to_embedding : embedding e := { induced := de.induced, inj := de.inj } /-- If the domain of a `dense_embedding` is a separable space, then so is its codomain. -/ protected lemma separable_space [separable_space α] : separable_space β := de.to_dense_inducing.separable_space /-- The product of two dense embeddings is a dense embedding. -/ protected lemma prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩, ..dense_inducing.prod de₁.to_dense_inducing de₂.to_dense_inducing } /-- The dense embedding of a subtype inside its closure. -/ @[simps] def subtype_emb {α : Type*} (p : α → Prop) (e : α → β) (x : {x // p x}) : {x // x ∈ closure (e '' {x | p x})} := ⟨e x, subset_closure $ mem_image_of_mem e x.prop⟩ protected lemma subtype (p : α → Prop) : dense_embedding (subtype_emb p e) := { dense := dense_iff_closure_eq.2 $ begin ext ⟨x, hx⟩, rw image_eq_range at hx, simpa [closure_subtype, ← range_comp, (∘)], end, inj := (de.inj.comp subtype.coe_injective).cod_restrict _, induced := (induced_iff_nhds_eq _).2 (assume ⟨x, hx⟩, by simp [subtype_emb, nhds_subtype_eq_comap, de.to_inducing.nhds_eq_comap, comap_comap, (∘)]) } lemma dense_image {s : set α} : dense (e '' s) ↔ dense s := de.to_dense_inducing.dense_image end dense_embedding lemma dense.dense_embedding_coe [topological_space α] {s : set α} (hs : dense s) : dense_embedding (coe : s → α) := { dense := hs.dense_range_coe, .. embedding_subtype_coe } lemma is_closed_property [topological_space β] {e : α → β} {p : β → Prop} (he : dense_range e) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) : ∀b, p b := have univ ⊆ {b | p b}, from calc univ = closure (range e) : he.closure_range.symm ... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h ... = _ : hp.closure_eq, assume b, this trivial lemma is_closed_property2 [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) : ∀b₁ b₂, p b₁ b₂ := have ∀q:β×β, p q.1 q.2, from is_closed_property (he.prod_map he) hp $ λ _, h _ _, assume b₁ b₂, this ⟨b₁, b₂⟩ lemma is_closed_property3 [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) : ∀b₁ b₂ b₃, p b₁ b₂ b₃ := have ∀q:β×β×β, p q.1 q.2.1 q.2.2, from is_closed_property (he.prod_map $ he.prod_map he) hp $ λ _, h _ _ _, assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩ @[elab_as_eliminator] lemma dense_range.induction_on [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop} (b₀ : β) (hp : is_closed {b | p b}) (ih : ∀a:α, p $ e a) : p b₀ := is_closed_property he hp ih b₀ @[elab_as_eliminator] lemma dense_range.induction_on₂ [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) (b₁ b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h _ _ @[elab_as_eliminator] lemma dense_range.induction_on₃ [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) (b₁ b₂ b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h _ _ _ section variables [topological_space β] [topological_space γ] [t2_space γ] variables {f : α → β} /-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/ lemma dense_range.equalizer (hfd : dense_range f) {g h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) : g = h := funext $ λ y, hfd.induction_on y (is_closed_eq hg hh) $ congr_fun H end
cc37e748b6b8d579b1b97b1164f82e2183b720aa
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Init/Data/Format/Instances.lean
c7ece2fd10320cdc62282567154e743975d7e0d6
[ "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
1,167
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.Format.Basic import Init.Data.Array.Basic import Init.Data.ToString.Basic open Std instance (priority := low) [ToString α] : ToFormat α := ⟨Std.Format.text ∘ toString⟩ def List.format [ToFormat α] : List α → Format | [] => "[]" | xs => Format.sbracket <| Format.joinSep xs ("," ++ Format.line) instance [ToFormat α] : ToFormat (List α) where format := List.format instance [ToFormat α] : ToFormat (Array α) where format a := "#" ++ format a.toList def Option.format {α : Type u} [ToFormat α] : Option α → Format | none => "none" | some a => "some " ++ Std.format a instance {α : Type u} [ToFormat α] : ToFormat (Option α) := ⟨Option.format⟩ instance {α : Type u} {β : Type v} [ToFormat α] [ToFormat β] : ToFormat (Prod α β) where format := fun (a, b) => Format.paren <| format a ++ "," ++ Format.line ++ format b def String.toFormat (s : String) : Std.Format := Std.Format.joinSep (s.splitOn "\n") Std.Format.line
7d8db37d3a5383d6eb23ce47d9c125fe124ef20e
0dc59d2b959c9b11a672f655b104d7d7d3e37660
/Lean4_filters/Tactics/Basic.lean
b50f1e2c807f1e994d4a0b57da9bda31855e2833
[]
no_license
kbuzzard/lean4-filters
5aa17d95079ceb906622543209064151fa645e71
29f90055b7a2341c86d924954463c439bd128fb7
refs/heads/master
1,679,762,259,673
1,616,701,300,000
1,616,701,300,000
350,784,493
5
1
null
1,625,691,081,000
1,616,517,435,000
Lean
UTF-8
Lean
false
false
754
lean
/- ## `left` and `right` -/ syntax "left" : tactic macro_rules | `(tactic| left) => `(tactic| apply Or.inl) syntax "right" : tactic macro_rules | `(tactic| right) => `(tactic| apply Or.inr) /- ## `split` -/ syntax "split" : tactic macro_rules | `(tactic| split) => `(tactic| apply And.intro) syntax "split_iffs" : tactic macro_rules | `(tactic| split_iffs) => `(tactic| apply Iff.intro) /- ## `cases ... with` -/ -- thanks Yakov/Sebastian syntax "cases" term "with" ident withPosition(ident) : tactic macro_rules | `(tactic| cases $d:term with $p:ident $q:ident) => `(tactic| cases $d:term with | _ $p $q => ?_) /- ## `trivial` -/ syntax "trivial" : tactic macro_rules | `(tactic| trivial) => `(tactic| exact True.intro)
ed3138aede24a14ce570dcd460d766d41769b3d7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/convex/function.lean
4e71bbda04273ea81515fb9e9c41924e98e6302d
[ "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
41,675
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, François Dupuis -/ import analysis.convex.basic import tactic.field_simp import tactic.linarith import tactic.ring /-! # Convex and concave functions This file defines convex and concave functions in vector spaces and proves the finite Jensen inequality. The integral version can be found in `analysis.convex.integral`. A function `f : E → β` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s`, the segment joining `(x, f x)` to `(y, f y)` is above the graph of `f`. Equivalently, `convex_on 𝕜 f s` means that the epigraph `{p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set. ## Main declarations * `convex_on 𝕜 s f`: The function `f` is convex on `s` with scalars `𝕜`. * `concave_on 𝕜 s f`: The function `f` is concave on `s` with scalars `𝕜`. * `strict_convex_on 𝕜 s f`: The function `f` is strictly convex on `s` with scalars `𝕜`. * `strict_concave_on 𝕜 s f`: The function `f` is strictly concave on `s` with scalars `𝕜`. -/ open finset linear_map set open_locale big_operators classical convex pointwise variables {𝕜 E F β ι : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 β] (s : set E) (f : E → β) /-- Convexity of functions -/ def convex_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) /-- Strict convexity of functions -/ def strict_convex_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) < a • f x + b • f y /-- Strict concavity of functions -/ def strict_concave_on : Prop := convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y < f (a • x + b • y) variables {𝕜 s f} open order_dual (to_dual of_dual) lemma convex_on.dual (hf : convex_on 𝕜 s f) : concave_on 𝕜 s (to_dual ∘ f) := hf lemma concave_on.dual (hf : concave_on 𝕜 s f) : convex_on 𝕜 s (to_dual ∘ f) := hf lemma strict_convex_on.dual (hf : strict_convex_on 𝕜 s f) : strict_concave_on 𝕜 s (to_dual ∘ f) := hf lemma strict_concave_on.dual (hf : strict_concave_on 𝕜 s f) : strict_convex_on 𝕜 s (to_dual ∘ f) := hf lemma convex_on_id {s : set β} (hs : convex 𝕜 s) : convex_on 𝕜 s id := ⟨hs, by { intros, refl }⟩ lemma concave_on_id {s : set β} (hs : convex 𝕜 s) : concave_on 𝕜 s id := ⟨hs, by { intros, refl }⟩ lemma convex_on.subset {t : set E} (hf : convex_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : convex_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma concave_on.subset {t : set E} (hf : concave_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : concave_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma strict_convex_on.subset {t : set E} (hf : strict_convex_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : strict_convex_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ lemma strict_concave_on.subset {t : set E} (hf : strict_concave_on 𝕜 t f) (hst : s ⊆ t) (hs : convex 𝕜 s) : strict_concave_on 𝕜 s f := ⟨hs, λ x hx y hy, hf.2 (hst hx) (hst hy)⟩ end has_smul section distrib_mul_action variables [has_smul 𝕜 E] [distrib_mul_action 𝕜 β] {s : set E} {f g : E → β} lemma convex_on.add (hf : convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a • f x + b • f y) + (a • g x + b • g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a • (f x + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma concave_on.add (hf : concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : concave_on 𝕜 s (f + g) := hf.dual.add hg end distrib_mul_action section module variables [has_smul 𝕜 E] [module 𝕜 β] {s : set E} {f : E → β} lemma convex_on_const (c : β) (hs : convex 𝕜 s) : convex_on 𝕜 s (λ x:E, c) := ⟨hs, λ x y _ _ a b _ _ hab, (convex.combo_self hab c).ge⟩ lemma concave_on_const (c : β) (hs : convex 𝕜 s) : concave_on 𝕜 s (λ x:E, c) := @convex_on_const _ _ βᵒᵈ _ _ _ _ _ _ c hs lemma convex_on_of_convex_epigraph (h : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}) : convex_on 𝕜 s f := ⟨λ x hx y hy a b ha hb hab, (@h (x, f x) ⟨hx, le_rfl⟩ (y, f y) ⟨hy, le_rfl⟩ a b ha hb hab).1, λ x hx y hy a b ha hb hab, (@h (x, f x) ⟨hx, le_rfl⟩ (y, f y) ⟨hy, le_rfl⟩ a b ha hb hab).2⟩ lemma concave_on_of_convex_hypograph (h : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1}) : concave_on 𝕜 s f := @convex_on_of_convex_epigraph 𝕜 E βᵒᵈ _ _ _ _ _ _ _ h end module section ordered_smul variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.convex_le (hf : convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := λ x hx y hy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha hb hab, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx.1 hy.1 ha hb hab ... ≤ a • r + b • r : add_le_add (smul_le_smul_of_nonneg hx.2 ha) (smul_le_smul_of_nonneg hy.2 hb) ... = r : convex.combo_self hab r⟩ lemma concave_on.convex_ge (hf : concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := hf.dual.convex_le r lemma convex_on.convex_epigraph (hf : convex_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintro ⟨x, r⟩ ⟨hx, hr⟩ ⟨y, t⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • r + b • t : add_le_add (smul_le_smul_of_nonneg hr ha) (smul_le_smul_of_nonneg ht hb) end lemma concave_on.convex_hypograph (hf : concave_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1} := hf.dual.convex_epigraph lemma convex_on_iff_convex_epigraph : convex_on 𝕜 s f ↔ convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2} := ⟨convex_on.convex_epigraph, convex_on_of_convex_epigraph⟩ lemma concave_on_iff_convex_hypograph : concave_on 𝕜 s f ↔ convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on_iff_convex_epigraph 𝕜 E βᵒᵈ _ _ _ _ _ _ _ f end ordered_smul section module variables [module 𝕜 E] [has_smul 𝕜 β] {s : set E} {f : E → β} /-- Right translation preserves convexity. -/ lemma convex_on.translate_right (hf : convex_on 𝕜 s f) (c : E) : convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := ⟨hf.1.translate_preimage_right _, λ x hx y hy a b ha hb hab, calc f (c + (a • x + b • y)) = f (a • (c + x) + b • (c + y)) : by rw [smul_add, smul_add, add_add_add_comm, convex.combo_self hab] ... ≤ a • f (c + x) + b • f (c + y) : hf.2 hx hy ha hb hab⟩ /-- Right translation preserves concavity. -/ lemma concave_on.translate_right (hf : concave_on 𝕜 s f) (c : E) : concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := hf.dual.translate_right _ /-- Left translation preserves convexity. -/ lemma convex_on.translate_left (hf : convex_on 𝕜 s f) (c : E) : convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ /-- Left translation preserves concavity. -/ lemma concave_on.translate_left (hf : concave_on 𝕜 s f) (c : E) : concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := hf.dual.translate_left _ end module section module variables [module 𝕜 E] [module 𝕜 β] lemma convex_on_iff_forall_pos {s : set E} {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y := begin refine and_congr_right' ⟨λ h x hx y hy a b ha hb hab, h hx hy ha.le hb.le hab, λ h x hx y hy a b ha hb hab, _⟩, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, subst b, simp_rw [zero_smul, zero_add, one_smul] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, subst a, simp_rw [zero_smul, add_zero, one_smul] }, exact h hx hy ha' hb' hab, end lemma concave_on_iff_forall_pos {s : set E} {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) := @convex_on_iff_forall_pos 𝕜 E βᵒᵈ _ _ _ _ _ _ _ lemma convex_on_iff_pairwise_pos {s : set E} {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) := begin rw convex_on_iff_forall_pos, refine and_congr_right' ⟨λ h x hx y hy _ a b ha hb hab, h hx hy ha hb hab, λ h x hx y hy a b ha hb hab, _⟩, obtain rfl | hxy := eq_or_ne x y, { rw [convex.combo_self hab, convex.combo_self hab] }, exact h hx hy hxy ha hb hab, end lemma concave_on_iff_pairwise_pos {s : set E} {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) := @convex_on_iff_pairwise_pos 𝕜 E βᵒᵈ _ _ _ _ _ _ _ /-- A linear map is convex. -/ lemma linear_map.convex_on (f : E →ₗ[𝕜] β) {s : set E} (hs : convex 𝕜 s) : convex_on 𝕜 s f := ⟨hs, λ _ _ _ _ _ _ _ _ _, by rw [f.map_add, f.map_smul, f.map_smul]⟩ /-- A linear map is concave. -/ lemma linear_map.concave_on (f : E →ₗ[𝕜] β) {s : set E} (hs : convex 𝕜 s) : concave_on 𝕜 s f := ⟨hs, λ _ _ _ _ _ _ _ _ _, by rw [f.map_add, f.map_smul, f.map_smul]⟩ lemma strict_convex_on.convex_on {s : set E} {f : E → β} (hf : strict_convex_on 𝕜 s f) : convex_on 𝕜 s f := convex_on_iff_pairwise_pos.mpr ⟨hf.1, λ x hx y hy hxy a b ha hb hab, (hf.2 hx hy hxy ha hb hab).le⟩ lemma strict_concave_on.concave_on {s : set E} {f : E → β} (hf : strict_concave_on 𝕜 s f) : concave_on 𝕜 s f := hf.dual.convex_on section ordered_smul variables [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma strict_convex_on.convex_lt (hf : strict_convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x < r} := convex_iff_pairwise_pos.2 $ λ x hx y hy hxy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha.le hb.le hab, calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx.1 hy.1 hxy ha hb hab ... ≤ a • r + b • r : add_le_add (smul_lt_smul_of_pos hx.2 ha).le (smul_lt_smul_of_pos hy.2 hb).le ... = r : convex.combo_self hab r⟩ lemma strict_concave_on.convex_gt (hf : strict_concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r < f x} := hf.dual.convex_lt r end ordered_smul section linear_order variables [linear_order E] {s : set E} {f : E → β} /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is convex, it suffices to verify the inequality `f (a • x + b • y) ≤ a • f x + b • f y` only for `x < y` and positive `a`, `b`. The main use case is `E = 𝕜` however one can apply it, e.g., to `𝕜^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) : convex_on 𝕜 s f := begin refine convex_on_iff_pairwise_pos.2 ⟨hs, λ x hx y hy hxy a b ha hb hab, _⟩, wlog h : x ≤ y using [x y a b, y x b a], { exact le_total _ _ }, exact hf hx hy (h.lt_of_ne hxy) ha hb hab, end /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.concave_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on 𝕜 s f := @linear_order.convex_on_of_lt _ _ βᵒᵈ _ _ _ _ _ _ s f hs hf /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is strictly convex, it suffices to verify the inequality `f (a • x + b • y) < a • f x + b • f y` for `x < y` and positive `a`, `b`. The main use case is `E = 𝕜` however one can apply it, e.g., to `𝕜^n` with lexicographic order. -/ lemma linear_order.strict_convex_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) < a • f x + b • f y) : strict_convex_on 𝕜 s f := begin refine ⟨hs, λ x hx y hy hxy a b ha hb hab, _⟩, wlog h : x ≤ y using [x y a b, y x b a], { exact le_total _ _ }, exact hf hx hy (h.lt_of_ne hxy) ha hb hab, end /-- For a function on a convex set in a linearly ordered space (where the order and the algebraic structures aren't necessarily compatible), in order to prove that it is strictly concave it suffices to verify the inequality `a • f x + b • f y < f (a • x + b • y)` for `x < y` and positive `a`, `b`. The main use case is `E = 𝕜` however one can apply it, e.g., to `𝕜^n` with lexicographic order. -/ lemma linear_order.strict_concave_on_of_lt (hs : convex 𝕜 s) (hf : ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x < y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y < f (a • x + b • y)) : strict_concave_on 𝕜 s f := @linear_order.strict_convex_on_of_lt _ _ βᵒᵈ _ _ _ _ _ _ _ _ hs hf end linear_order end module section module variables [module 𝕜 E] [module 𝕜 F] [has_smul 𝕜 β] /-- If `g` is convex on `s`, so is `(f ∘ g)` on `f ⁻¹' s` for a linear `f`. -/ lemma convex_on.comp_linear_map {f : F → β} {s : set F} (hf : convex_on 𝕜 s f) (g : E →ₗ[𝕜] F) : convex_on 𝕜 (g ⁻¹' s) (f ∘ g) := ⟨hf.1.linear_preimage _, λ x hx y hy a b ha hb hab, calc f (g (a • x + b • y)) = f (a • (g x) + b • (g y)) : by rw [g.map_add, g.map_smul, g.map_smul] ... ≤ a • f (g x) + b • f (g y) : hf.2 hx hy ha hb hab⟩ /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma concave_on.comp_linear_map {f : F → β} {s : set F} (hf : concave_on 𝕜 s f) (g : E →ₗ[𝕜] F) : concave_on 𝕜 (g ⁻¹' s) (f ∘ g) := hf.dual.comp_linear_map g end module end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] section distrib_mul_action variables [has_smul 𝕜 E] [distrib_mul_action 𝕜 β] {s : set E} {f g : E → β} lemma strict_convex_on.add_convex_on (hf : strict_convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y hy hxy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) < (a • f x + b • f y) + (a • g x + b • g y) : add_lt_add_of_lt_of_le (hf.2 hx hy hxy ha hb hab) (hg.2 hx hy ha.le hb.le hab) ... = a • (f x + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma convex_on.add_strict_convex_on (hf : convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := (add_comm g f) ▸ hg.add_convex_on hf lemma strict_convex_on.add (hf : strict_convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f + g) := ⟨hf.1, λ x hx y hy hxy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) < (a • f x + b • f y) + (a • g x + b • g y) : add_lt_add (hf.2 hx hy hxy ha hb hab) (hg.2 hx hy hxy ha hb hab) ... = a • (f x + g x) + b • (f y + g y) : by rw [smul_add, smul_add, add_add_add_comm]⟩ lemma strict_concave_on.add_concave_on (hf : strict_concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add_convex_on hg.dual lemma concave_on.add_strict_concave_on (hf : concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add_strict_convex_on hg.dual lemma strict_concave_on.add (hf : strict_concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f + g) := hf.dual.add hg end distrib_mul_action section module variables [module 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.convex_lt (hf : convex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x < r} := convex_iff_forall_pos.2 $ λ x hx y hy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha.le hb.le hab, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx.1 hy.1 ha.le hb.le hab ... < a • r + b • r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx.2 ha) (smul_le_smul_of_nonneg hy.2.le hb.le) ... = r : convex.combo_self hab _⟩ lemma concave_on.convex_gt (hf : concave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r < f x} := hf.dual.convex_lt r lemma convex_on.open_segment_subset_strict_epigraph (hf : convex_on 𝕜 s f) (p q : E × β) (hp : p.1 ∈ s ∧ f p.1 < p.2) (hq : q.1 ∈ s ∧ f q.1 ≤ q.2) : open_segment 𝕜 p q ⊆ {p : E × β | p.1 ∈ s ∧ f p.1 < p.2} := begin rintro _ ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨hf.1 hp.1 hq.1 ha.le hb.le hab, _⟩, calc f (a • p.1 + b • q.1) ≤ a • f p.1 + b • f q.1 : hf.2 hp.1 hq.1 ha.le hb.le hab ... < a • p.2 + b • q.2 : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hp.2 ha) (smul_le_smul_of_nonneg hq.2 hb.le) end lemma concave_on.open_segment_subset_strict_hypograph (hf : concave_on 𝕜 s f) (p q : E × β) (hp : p.1 ∈ s ∧ p.2 < f p.1) (hq : q.1 ∈ s ∧ q.2 ≤ f q.1) : open_segment 𝕜 p q ⊆ {p : E × β | p.1 ∈ s ∧ p.2 < f p.1} := hf.dual.open_segment_subset_strict_epigraph p q hp hq lemma convex_on.convex_strict_epigraph (hf : convex_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ f p.1 < p.2} := convex_iff_open_segment_subset.mpr $ λ p hp q hq, hf.open_segment_subset_strict_epigraph p q hp ⟨hq.1, hq.2.le⟩ lemma concave_on.convex_strict_hypograph (hf : concave_on 𝕜 s f) : convex 𝕜 {p : E × β | p.1 ∈ s ∧ p.2 < f p.1} := hf.dual.convex_strict_epigraph end module end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} /-- The pointwise maximum of convex functions is convex. -/ lemma convex_on.sup (hf : convex_on 𝕜 s f) (hg : convex_on 𝕜 s g) : convex_on 𝕜 s (f ⊔ g) := begin refine ⟨hf.left, λ x hx y hy a b ha hb hab, sup_le _ _⟩, { calc f (a • x + b • y) ≤ a • f x + b • f y : hf.right hx hy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_left ha) (smul_le_smul_of_nonneg le_sup_left hb) }, { calc g (a • x + b • y) ≤ a • g x + b • g y : hg.right hx hy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_right ha) (smul_le_smul_of_nonneg le_sup_right hb) } end /-- The pointwise minimum of concave functions is concave. -/ lemma concave_on.inf (hf : concave_on 𝕜 s f) (hg : concave_on 𝕜 s g) : concave_on 𝕜 s (f ⊓ g) := hf.dual.sup hg /-- The pointwise maximum of strictly convex functions is strictly convex. -/ lemma strict_convex_on.sup (hf : strict_convex_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_convex_on 𝕜 s (f ⊔ g) := ⟨hf.left, λ x hx y hy hxy a b ha hb hab, max_lt (calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx hy hxy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_left ha.le) (smul_le_smul_of_nonneg le_sup_left hb.le)) (calc g (a • x + b • y) < a • g x + b • g y : hg.2 hx hy hxy ha hb hab ... ≤ a • (f x ⊔ g x) + b • (f y ⊔ g y) : add_le_add (smul_le_smul_of_nonneg le_sup_right ha.le) (smul_le_smul_of_nonneg le_sup_right hb.le))⟩ /-- The pointwise minimum of strictly concave functions is strictly concave. -/ lemma strict_concave_on.inf (hf : strict_concave_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_concave_on 𝕜 s (f ⊓ g) := hf.dual.sup hg /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment' (hf : convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha) (smul_le_smul_of_nonneg (le_max_right _ _) hb) ... = max (f x) (f y) : convex.combo_self hab _ /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.ge_on_segment' (hf : concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := hf.dual.le_on_segment' hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment (hf : convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x -[𝕜] y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.ge_on_segment (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x -[𝕜] y]) : min (f x) (f y) ≤ f z := hf.dual.le_on_segment hx hy hz /-- A strictly convex function on an open segment is strictly upper-bounded by the max of its endpoints. -/ lemma strict_convex_on.lt_on_open_segment' (hf : strict_convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) : f (a • x + b • y) < max (f x) (f y) := calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx hy hxy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha.le) (smul_le_smul_of_nonneg (le_max_right _ _) hb.le) ... = max (f x) (f y) : convex.combo_self hab _ /-- A strictly concave function on an open segment is strictly lower-bounded by the min of its endpoints. -/ lemma strict_concave_on.lt_on_open_segment' (hf : strict_concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) : min (f x) (f y) < f (a • x + b • y) := hf.dual.lt_on_open_segment' hx hy hxy ha hb hab /-- A strictly convex function on an open segment is strictly upper-bounded by the max of its endpoints. -/ lemma strict_convex_on.lt_on_open_segment (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) (hz : z ∈ open_segment 𝕜 x y) : f z < max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.lt_on_open_segment' hx hy hxy ha hb hab /-- A strictly concave function on an open segment is strictly lower-bounded by the min of its endpoints. -/ lemma strict_concave_on.lt_on_open_segment (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) (hz : z ∈ open_segment 𝕜 x y) : min (f x) (f y) < f z := hf.dual.lt_on_open_segment hx hy hxy hz end linear_ordered_add_comm_monoid section linear_ordered_cancel_add_comm_monoid variables [linear_ordered_cancel_add_comm_monoid β] section ordered_smul variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} lemma convex_on.le_left_of_right_le' (hf : convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 ≤ b) (hab : a + b = 1) (hfy : f y ≤ f (a • x + b • y)) : f (a • x + b • y) ≤ f x := le_of_not_lt $ λ h, lt_irrefl (f (a • x + b • y)) $ calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha.le hb hab ... < a • f (a • x + b • y) + b • f (a • x + b • y) : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos h ha) (smul_le_smul_of_nonneg hfy hb) ... = f (a • x + b • y) : convex.combo_self hab _ lemma concave_on.left_le_of_le_right' (hf : concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 ≤ b) (hab : a + b = 1) (hfy : f (a • x + b • y) ≤ f y) : f x ≤ f (a • x + b • y) := hf.dual.le_left_of_right_le' hx hy ha hb hab hfy lemma convex_on.le_right_of_left_le' (hf : convex_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 < b) (hab : a + b = 1) (hfx : f x ≤ f (a • x + b • y)) : f (a • x + b • y) ≤ f y := begin rw add_comm at ⊢ hab hfx, exact hf.le_left_of_right_le' hy hx hb ha hab hfx, end lemma concave_on.right_le_of_le_left' (hf : concave_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 < b) (hab : a + b = 1) (hfx : f (a • x + b • y) ≤ f x) : f y ≤ f (a • x + b • y) := hf.dual.le_right_of_left_le' hx hy ha hb hab hfx lemma convex_on.le_left_of_right_le (hf : convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f y ≤ f z) : f z ≤ f x := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.le_left_of_right_le' hx hy ha hb.le hab hyz, end lemma concave_on.left_le_of_le_right (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f z ≤ f y) : f x ≤ f z := hf.dual.le_left_of_right_le hx hy hz hyz lemma convex_on.le_right_of_left_le (hf : convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f x ≤ f z) : f z ≤ f y := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.le_right_of_left_le' hx hy ha.le hb hab hxz, end lemma concave_on.right_le_of_le_left (hf : concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f z ≤ f x) : f y ≤ f z := hf.dual.le_right_of_left_le hx hy hz hxz end ordered_smul section module variables [module 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f g : E → β} /- The following lemmas don't require `module 𝕜 E` if you add the hypothesis `x ≠ y`. At the time of the writing, we decided the resulting lemmas wouldn't be useful. Feel free to reintroduce them. -/ lemma strict_convex_on.lt_left_of_right_lt' (hf : strict_convex_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfy : f y < f (a • x + b • y)) : f (a • x + b • y) < f x := not_le.1 $ λ h, lt_irrefl (f (a • x + b • y)) $ calc f (a • x + b • y) < a • f x + b • f y : hf.2 hx hy begin rintro rfl, rw convex.combo_self hab at hfy, exact lt_irrefl _ hfy, end ha hb hab ... < a • f (a • x + b • y) + b • f (a • x + b • y) : add_lt_add_of_le_of_lt (smul_le_smul_of_nonneg h ha.le) (smul_lt_smul_of_pos hfy hb) ... = f (a • x + b • y) : convex.combo_self hab _ lemma strict_concave_on.left_lt_of_lt_right' (hf : strict_concave_on 𝕜 s f) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {a b : 𝕜} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfy : f (a • x + b • y) < f y) : f x < f (a • x + b • y) := hf.dual.lt_left_of_right_lt' hx hy ha hb hab hfy lemma strict_convex_on.lt_right_of_left_lt' (hf : strict_convex_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfx : f x < f (a • x + b • y)) : f (a • x + b • y) < f y := begin rw add_comm at ⊢ hab hfx, exact hf.lt_left_of_right_lt' hy hx hb ha hab hfx, end lemma strict_concave_on.lt_right_of_left_lt' (hf : strict_concave_on 𝕜 s f) {x y : E} {a b : 𝕜} (hx : x ∈ s) (hy : y ∈ s) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (hfx : f (a • x + b • y) < f x) : f y < f (a • x + b • y) := hf.dual.lt_right_of_left_lt' hx hy ha hb hab hfx lemma strict_convex_on.lt_left_of_right_lt (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f y < f z) : f z < f x := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.lt_left_of_right_lt' hx hy ha hb hab hyz, end lemma strict_concave_on.left_lt_of_lt_right (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hyz : f z < f y) : f x < f z := hf.dual.lt_left_of_right_lt hx hy hz hyz lemma strict_convex_on.lt_right_of_left_lt (hf : strict_convex_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f x < f z) : f z < f y := begin obtain ⟨a, b, ha, hb, hab, rfl⟩ := hz, exact hf.lt_right_of_left_lt' hx hy ha hb hab hxz, end lemma strict_concave_on.lt_right_of_left_lt (hf : strict_concave_on 𝕜 s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ open_segment 𝕜 x y) (hxz : f z < f x) : f y < f z := hf.dual.lt_right_of_left_lt hx hy hz hxz end module end linear_ordered_cancel_add_comm_monoid section ordered_add_comm_group variables [ordered_add_comm_group β] [has_smul 𝕜 E] [module 𝕜 β] {s : set E} {f g : E → β} /-- A function `-f` is convex iff `f` is concave. -/ @[simp] lemma neg_convex_on_iff : convex_on 𝕜 s (-f) ↔ concave_on 𝕜 s f := begin split, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy a b ha hb hab, _⟩, simp [neg_apply, neg_le, add_comm] at h, exact h hx hy ha hb hab }, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy a b ha hb hab, _⟩, rw ←neg_le_neg_iff, simp_rw [neg_add, pi.neg_apply, smul_neg, neg_neg], exact h hx hy ha hb hab } end /-- A function `-f` is concave iff `f` is convex. -/ @[simp] lemma neg_concave_on_iff : concave_on 𝕜 s (-f) ↔ convex_on 𝕜 s f:= by rw [← neg_convex_on_iff, neg_neg f] /-- A function `-f` is strictly convex iff `f` is strictly concave. -/ @[simp] lemma neg_strict_convex_on_iff : strict_convex_on 𝕜 s (-f) ↔ strict_concave_on 𝕜 s f := begin split, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy hxy a b ha hb hab, _⟩, simp [neg_apply, neg_lt, add_comm] at h, exact h hx hy hxy ha hb hab }, { rintro ⟨hconv, h⟩, refine ⟨hconv, λ x hx y hy hxy a b ha hb hab, _⟩, rw ←neg_lt_neg_iff, simp_rw [neg_add, pi.neg_apply, smul_neg, neg_neg], exact h hx hy hxy ha hb hab } end /-- A function `-f` is strictly concave iff `f` is strictly convex. -/ @[simp] lemma neg_strict_concave_on_iff : strict_concave_on 𝕜 s (-f) ↔ strict_convex_on 𝕜 s f := by rw [← neg_strict_convex_on_iff, neg_neg f] alias neg_convex_on_iff ↔ _ concave_on.neg alias neg_concave_on_iff ↔ _ convex_on.neg alias neg_strict_convex_on_iff ↔ _ strict_concave_on.neg alias neg_strict_concave_on_iff ↔ _ strict_convex_on.neg lemma convex_on.sub (hf : convex_on 𝕜 s f) (hg : concave_on 𝕜 s g) : convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma concave_on.sub (hf : concave_on 𝕜 s f) (hg : convex_on 𝕜 s g) : concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma strict_convex_on.sub (hf : strict_convex_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma strict_concave_on.sub (hf : strict_concave_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg lemma convex_on.sub_strict_concave_on (hf : convex_on 𝕜 s f) (hg : strict_concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_strict_convex_on hg.neg lemma concave_on.sub_strict_convex_on (hf : concave_on 𝕜 s f) (hg : strict_convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_strict_concave_on hg.neg lemma strict_convex_on.sub_concave_on (hf : strict_convex_on 𝕜 s f) (hg : concave_on 𝕜 s g) : strict_convex_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_convex_on hg.neg lemma strict_concave_on.sub_convex_on (hf : strict_concave_on 𝕜 s f) (hg : convex_on 𝕜 s g) : strict_concave_on 𝕜 s (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add_concave_on hg.neg end ordered_add_comm_group end add_comm_monoid section add_cancel_comm_monoid variables [add_cancel_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [has_smul 𝕜 β] {s : set E} {f : E → β} /-- Right translation preserves strict convexity. -/ lemma strict_convex_on.translate_right (hf : strict_convex_on 𝕜 s f) (c : E) : strict_convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := ⟨hf.1.translate_preimage_right _, λ x hx y hy hxy a b ha hb hab, calc f (c + (a • x + b • y)) = f (a • (c + x) + b • (c + y)) : by rw [smul_add, smul_add, add_add_add_comm, convex.combo_self hab] ... < a • f (c + x) + b • f (c + y) : hf.2 hx hy ((add_right_injective c).ne hxy) ha hb hab⟩ /-- Right translation preserves strict concavity. -/ lemma strict_concave_on.translate_right (hf : strict_concave_on 𝕜 s f) (c : E) : strict_concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, c + z)) := hf.dual.translate_right _ /-- Left translation preserves strict convexity. -/ lemma strict_convex_on.translate_left (hf : strict_convex_on 𝕜 s f) (c : E) : strict_convex_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ /-- Left translation preserves strict concavity. -/ lemma strict_concave_on.translate_left (hf : strict_concave_on 𝕜 s f) (c : E) : strict_concave_on 𝕜 ((λ z, c + z) ⁻¹' s) (f ∘ (λ z, z + c)) := by simpa only [add_comm] using hf.translate_right _ end add_cancel_comm_monoid end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] [add_comm_monoid E] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section module variables [has_smul 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.smul {c : 𝕜} (hc : 0 ≤ c) (hf : convex_on 𝕜 s f) : convex_on 𝕜 s (λ x, c • f x) := ⟨hf.1, λ x hx y hy a b ha hb hab, calc c • f (a • x + b • y) ≤ c • (a • f x + b • f y) : smul_le_smul_of_nonneg (hf.2 hx hy ha hb hab) hc ... = a • (c • f x) + b • (c • f y) : by rw [smul_add, smul_comm c, smul_comm c]; apply_instance⟩ lemma concave_on.smul {c : 𝕜} (hc : 0 ≤ c) (hf : concave_on 𝕜 s f) : concave_on 𝕜 s (λ x, c • f x) := hf.dual.smul hc end module end ordered_add_comm_monoid end ordered_comm_semiring section ordered_ring variables [linear_ordered_field 𝕜] [add_comm_group E] [add_comm_group F] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section module variables [module 𝕜 E] [module 𝕜 F] [has_smul 𝕜 β] /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ lemma convex_on.comp_affine_map {f : F → β} (g : E →ᵃ[𝕜] F) {s : set F} (hf : convex_on 𝕜 s f) : convex_on 𝕜 (g ⁻¹' s) (f ∘ g) := ⟨hf.1.affine_preimage _, λ x hx y hy a b ha hb hab, calc (f ∘ g) (a • x + b • y) = f (g (a • x + b • y)) : rfl ... = f (a • (g x) + b • (g y)) : by rw [convex.combo_affine_apply hab] ... ≤ a • f (g x) + b • f (g y) : hf.2 hx hy ha hb hab⟩ /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ lemma concave_on.comp_affine_map {f : F → β} (g : E →ᵃ[𝕜] F) {s : set F} (hf : concave_on 𝕜 s f) : concave_on 𝕜 (g ⁻¹' s) (f ∘ g) := hf.dual.comp_affine_map g end module end ordered_add_comm_monoid end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] [add_comm_monoid E] section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] section has_smul variables [has_smul 𝕜 E] [has_smul 𝕜 β] {s : set E} lemma convex_on_iff_div {f : E → β} : convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) • f x + (b/(a+b)) • f y := and_congr iff.rfl ⟨begin intros h x hx y hy a b ha hb hab, apply h hx hy (div_nonneg ha hab.le) (div_nonneg hb hab.le), rw [←add_div, div_self hab.ne'], end, begin intros h x hx y hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma concave_on_iff_div {f : E → β} : concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • f x + (b/(a+b)) • f y ≤ f ((a/(a+b)) • x + (b/(a+b)) • y) := @convex_on_iff_div _ _ βᵒᵈ _ _ _ _ _ _ _ lemma strict_convex_on_iff_div {f : E → β} : strict_convex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → f ((a/(a+b)) • x + (b/(a+b)) • y) < (a/(a+b)) • f x + (b/(a+b)) • f y := and_congr iff.rfl ⟨begin intros h x hx y hy hxy a b ha hb, have hab := add_pos ha hb, apply h hx hy hxy (div_pos ha hab) (div_pos hb hab), rw [←add_div, div_self hab.ne'], end, begin intros h x hx y hy hxy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy hxy ha hb, end⟩ lemma strict_concave_on_iff_div {f : E → β} : strict_concave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → (a/(a+b)) • f x + (b/(a+b)) • f y < f ((a/(a+b)) • x + (b/(a+b)) • y) := @strict_convex_on_iff_div _ _ βᵒᵈ _ _ _ _ _ _ _ end has_smul end ordered_add_comm_monoid end linear_ordered_field section variables [linear_ordered_field 𝕜] [linear_ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] {x y z : 𝕜} {s : set 𝕜} {f : 𝕜 → β} lemma convex_on.le_right_of_left_le'' (hf : convex_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y ≤ z) (h : f x ≤ f y) : f y ≤ f z := hyz.eq_or_lt.elim (λ hyz, (congr_arg f hyz).le) (λ hyz, hf.le_right_of_left_le hx hz (Ioo_subset_open_segment ⟨hxy, hyz⟩) h) lemma convex_on.le_left_of_right_le'' (hf : convex_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x ≤ y) (hyz : y < z) (h : f z ≤ f y) : f y ≤ f x := hxy.eq_or_lt.elim (λ hxy, (congr_arg f hxy).ge) (λ hxy, hf.le_left_of_right_le hx hz (Ioo_subset_open_segment ⟨hxy, hyz⟩) h) lemma concave_on.right_le_of_le_left'' (hf : concave_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y ≤ z) (h : f y ≤ f x) : f z ≤ f y := hf.dual.le_right_of_left_le'' hx hz hxy hyz h lemma concave_on.left_le_of_le_right'' (hf : concave_on 𝕜 s f) (hx : x ∈ s) (hz : z ∈ s) (hxy : x ≤ y) (hyz : y < z) (h : f y ≤ f z) : f x ≤ f y := hf.dual.le_left_of_right_le'' hx hz hxy hyz h end
50a8f371ada906177761d1bed728996646f338db
e151e9053bfd6d71740066474fc500a087837323
/src/hott/hit/trunc.lean
9c96f59750a6f006ac3ada159fc8bd2aecb9a4ea
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
7,664
lean
/- 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 n-truncation of types. Ported from Coq HoTT -/ /- The hit n-truncation is primitive, declared in init.hit. -/ import hott.types.sigma hott.types.pointed universes u v w hott_theory namespace hott open hott.is_trunc hott.eq hott.equiv hott.is_equiv function prod sum sigma namespace trunc @[hott, induction_using] protected def rec' {n : ℕ₋₂} {A : Type u} {P : trunc n A → Type v} (H₁ : Πaa, is_trunc n (P aa)) (H : Πa, P (tr a)) (aa : trunc n A) : P aa := trunc.rec H aa @[hott, induction, priority 1500] protected def elim {n : ℕ₋₂} {A : Type _} {P : Type _} [Pt : is_trunc n P] (H : A → P) : trunc n A → P := trunc.rec H @[hott, induction_using] protected def elim' {n : ℕ₋₂} {A : Type _} {P : Type _} (Pt : is_trunc n P) (H : A → P) : trunc n A → P := trunc.rec H @[hott, hsimp] def elim_tr {n : ℕ₋₂} {A : Type _} {P : Type _} [Pt : is_trunc n P] (H : A → P) (a : A) : trunc.elim H (@tr n A a) = H a := by refl @[hott] protected def elim_on {n : ℕ₋₂} {A : Type _} {P : Type _} (aa : trunc n A) [Pt : is_trunc n P] (H : A → P) : P := trunc.elim H aa variables {X : Type _} {Y : Type _} {Z : Type _} {P : X → Type _} {m : ℕ₋₂} (n : ℕ₋₂) (A : Type _) (B : Type _) @[hott] def untrunc_of_is_trunc [H : is_trunc m X] : trunc m X → X := trunc.rec id variables (A n) @[hott, instance] def is_equiv_tr [H : is_trunc n A] : is_equiv (@tr n A) := adjointify _ (untrunc_of_is_trunc) (λaa, trunc.rec_on aa (λa, idp)) (λa, idp) @[hott] def trunc_equiv [H : is_trunc n A] : trunc n A ≃ A := (equiv.mk tr (is_equiv_tr n A))⁻¹ᵉ @[hott] def is_trunc_of_is_equiv_tr [H : is_equiv (@tr n A)] : is_trunc n A := is_trunc_is_equiv_closed_rev n (@tr n A) (by infer) /- Functoriality -/ @[hott] def trunc_functor (f : X → Y) : trunc n X → trunc n Y := λxx, trunc.rec_on xx (λx, tr (f x)) @[hott, hsimp] def trunc_functor_tr (f : X → Y) (x : X) : trunc_functor n f (tr x) = tr (f x) := by refl @[hott] def trunc_functor_compose (g : Y → Z) (f : X → Y) : trunc_functor n (g ∘ f) ~ trunc_functor n g ∘ trunc_functor n f := λxx, trunc.rec_on xx (λx, idp) @[hott] def trunc_functor_id : trunc_functor n (@id A) ~ id := λxx, trunc.rec_on xx (λx, idp) @[hott] def trunc_functor_cast {X Y : Type _} (n : ℕ₋₂) (p : X = Y) : trunc_functor n (cast p) ~ cast (ap (trunc n) p) := begin intro x, hinduction x using trunc.rec with x, exact fn_tr_eq_tr_fn p (λy, tr) x ⬝ tr_eq_cast_ap _ _ end @[hott] def is_equiv_trunc_functor (f : X → Y) [H : is_equiv f] : is_equiv (trunc_functor n f) := adjointify _ (trunc_functor n f⁻¹ᶠ) (λyy, trunc.rec_on yy (λy, ap tr (right_inv _ _))) (λxx, trunc.rec_on xx (λx, ap tr (left_inv _ _))) @[hott] def trunc_homotopy {f g : X → Y} (p : f ~ g) : trunc_functor n f ~ trunc_functor n g := λxx, trunc.rec_on xx (λx, ap tr (p x)) section @[hott] def trunc_equiv_trunc (f : X ≃ Y) : trunc n X ≃ trunc n Y := equiv.mk _ (is_equiv_trunc_functor n f) end section @[hott] def trunc_prod_equiv : trunc n (X × Y) ≃ trunc n X × trunc n Y := begin fapply equiv.MK, { exact (λpp, trunc.rec_on pp (λp, (tr p.1, tr p.2))) }, { intro p, induction p with xx yy, hinduction xx with x, hinduction yy with y, exact tr (x,y) }, { intro p, induction p with xx yy, hinduction xx with x, hinduction yy with y, refl }, { intro pp, hinduction pp with p, induction p, refl } end end /- Propositional truncation -/ @[hott] def ttrunc (n : ℕ₋₂) (X : Type _) : n-Type := trunctype.mk (trunc n X) (by infer) @[hott, hsimp] def carrier_ttrunc (n : ℕ₋₂) (X : Type _) : @coe_sort _ (hott.has_coe_to_sort n) (ttrunc n X) = trunc n X := by refl @[hott, reducible] def merely (A : Type _) : Prop := ttrunc -1 A notation `∥`:max A `∥`:0 := merely A @[hott, reducible] def Exists (P : X → Type _) : Prop := ∥ sigma P ∥ @[hott, reducible] def or (A B : Type _) : Prop := ∥ A ⊎ B ∥ hott_theory_cmd "local notation `exists` binders `,` r:(scoped P, hott.trunc.Exists P) := r" hott_theory_cmd "local notation `∃` binders `,` r:(scoped P, hott.trunc.Exists P) := r" hott_theory_cmd "local notation [parsing_only] A ` \\/ ` B := hott.trunc.or A B" hott_theory_cmd "local notation A ∨ B := hott.trunc.or A B" @[hott, reducible] def merely.intro (a : A) : ∥ A ∥ := tr a @[hott, reducible] def exists.intro (x : X) (p : P x) : ∃x, P x := tr ⟨x, p⟩ @[hott, reducible] def or.intro_left (x : X) : X ∨ Y := tr (inl x) @[hott, reducible] def or.intro_right (y : Y) : X ∨ Y := tr (inr y) -- to do: give induction attribute @[hott] def merely.rec {A : Type u} {P : ∥A∥ → Type v} [Pt : Π (aa : ∥A∥), is_prop (P aa)] (H : Π(a : A), P (tr a)) (x : ∥A∥) : P x := begin hinduction x with a, exact H a end -- to do: give induction attribute @[hott] def exists.elim {A : Type _} {p : A → Type _} {B : Type _} [is_prop B] (H : Exists p) (H' : ∀ (a : A), p a → B) : B := begin hinduction H with x, induction x with a x, exact H' a x end -- to do: give induction attribute @[hott] def or.elim {A B C : Type _} [is_prop C] (f : A → C) (g : B → C) (H : A ∨ B) : C := begin hinduction H with x, induction x with a b, exact f a, exact g b end -- @[hott] def or.elim' {A B C : Type _} [is_prop A] [is_prop B] (f : A → C) (g : B → C) -- (fg : Πa b, f a = g b) (H : A ∨ B) : C := -- begin -- have : is_prop (Σ(x : C), (Π(a : A), x = f a) × (Π(b : B), x = g b)), -- { apply is_prop.mk, intros u v, induction u with c p, induction v with c' q, -- refine or.elim _ _ H, -- { intro a, sorry }, -- { intro b, sorry }}, -- have : Σ(x : C), (Π(a : A), x = f a) × (Π(b : B), x = g b), -- { refine @or.elim _ _ _ this _ _ H, -- { intro a, exact ⟨f a, λa', ap f (is_prop.elim a a'), fg a⟩ }, -- { intro b, exact ⟨g b, λa, (fg a b)⁻¹, λb', ap g (is_prop.elim b b')⟩ } }, -- exact this.1 -- end @[hott] def is_contr_of_merely_prop [H : is_prop A] (aa : merely A) : is_contr A := is_contr_of_inhabited_prop (trunc.rec_on aa id) @[hott] def trunc_sigma_equiv : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) := begin fapply equiv.MK; intro x, { hinduction x with p, exact tr ⟨p.1, tr p.2⟩ }, { hinduction x with p, induction p with a p, hinduction p with p, exact tr ⟨a, p⟩ }, { hinduction x with p, induction p with a p, hinduction p with p, refl }, { hinduction x with p, induction p with a p, refl } end @[hott] def trunc_sigma_equiv_of_is_trunc [H : is_trunc n X] : trunc n (Σ x, P x) ≃ Σ x, trunc n (P x) := calc trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) : trunc_sigma_equiv _ ... ≃ Σ x, trunc n (P x) : trunc_equiv _ _ /- the (non-dependent) universal property -/ @[hott] def trunc_arrow_equiv [H : is_trunc n B] : (trunc n A → B) ≃ (A → B) := begin fapply equiv.MK, { intros g a, exact g (tr a) }, { intros f x, hinduction x with a, exact f a }, { intro f, apply eq_of_homotopy, intro a, refl }, { intro g, apply eq_of_homotopy, intro x, hinduction x, refl }, end end trunc end hott
b2ff06e9483d7a9679d7848afb284a3b93520d5b
f313d4982feee650661f61ed73f0cb6635326350
/Mathlib/Data/Array/Basic.lean
33f701798bc842febc1ef766d3e379bf23855bc0
[ "Apache-2.0" ]
permissive
shingtaklam1324/mathlib4
38c6e172eec1385944db5a70a3b5545c924980ee
50610c343b7065e8eec056d641f859ceed608e69
refs/heads/master
1,683,032,333,313
1,621,942,699,000
1,621,942,699,000
371,130,608
0
0
Apache-2.0
1,622,053,166,000
1,622,053,166,000
null
UTF-8
Lean
false
false
1,047
lean
import Mathlib.Data.List.Basic @[simp] theorem List.toArrayAux_data : ∀ (l : List α) a, (l.toArrayAux a).data = a.data ++ l | [], r => (append_nil _).symm | a::as, r => (toArrayAux_data as (r.push a)).trans $ by simp [Array.push, append_assoc, List.concat_eq_append] @[simp] theorem List.toArray_data (l : List α) : l.toArray.data = l := toArrayAux_data _ _ namespace Array theorem ext' : {a b : Array α} → a.data = b.data → a = b | ⟨a⟩, ⟨_⟩, rfl => rfl @[simp] theorem data_toArray : (a : Array α) → a.data.toArray = a | ⟨l⟩ => ext' l.toArray_data theorem toArrayLit_eq (a : Array α) (n : Nat) (hsz : a.size = n) : a = toArrayLit a n hsz := by have _ from aux n rw [List.drop_eq_nil_of_le (Nat.le_of_eq hsz)] at this exact (data_toArray a).symm.trans $ congrArg List.toArray (this _).symm where aux : ∀ i hi, toListLitAux a n hsz i hi (a.data.drop i) = a.data | 0, _ => rfl | i+1, hi => by simp [toListLitAux] suffices _::_ = _ by rw [this]; apply aux apply List.get_cons_drop end Array
c9392f5834417f2b3ddd8c4b20aa9df2a797412f
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/topology/uniform_space/separation.lean
f8ee53653480b0787bb7ee654897d50949dc100d
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
13,800
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, Patrick Massot Hausdorff properties of uniform spaces. Separation quotient. -/ import topology.uniform_space.basic open filter topological_space set classical open_locale classical topological_space noncomputable theory set_option eqn_compiler.zeta true universes u v w variables {α : Type u} {β : Type v} {γ : Type w} variables [uniform_space α] [uniform_space β] [uniform_space γ] open_locale uniformity /- separated uniformity -/ /-- The separation relation is the intersection of all entourages. Two points which are related by the separation relation are "indistinguishable" according to the uniform structure. -/ protected def separation_rel (α : Type u) [u : uniform_space α] := ⋂₀ (𝓤 α).sets lemma separated_equiv : equivalence (λx y, (x, y) ∈ separation_rel α) := ⟨assume x, assume s, refl_mem_uniformity, assume x y, assume h (s : set (α×α)) hs, have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs, h _ this, assume x y z (hxy : (x, y) ∈ separation_rel α) (hyz : (y, z) ∈ separation_rel α) s (hs : s ∈ 𝓤 α), let ⟨t, ht, (h_ts : comp_rel t t ⊆ s)⟩ := comp_mem_uniformity_sets hs in h_ts $ show (x, z) ∈ comp_rel t t, from ⟨y, hxy t ht, hyz t ht⟩⟩ @[class] def separated (α : Type u) [uniform_space α] := separation_rel α = id_rel theorem separated_def {α : Type u} [uniform_space α] : separated α ↔ ∀ x y, (∀ r ∈ 𝓤 α, (x, y) ∈ r) → x = y := by simp [separated, id_rel_subset.2 separated_equiv.1, subset.antisymm_iff]; simp [subset_def, separation_rel] theorem separated_def' {α : Type u} [uniform_space α] : separated α ↔ ∀ x y, x ≠ y → ∃ r ∈ 𝓤 α, (x, y) ∉ r := separated_def.trans $ forall_congr $ λ x, forall_congr $ λ y, by rw ← not_imp_not; simp [classical.not_forall] @[priority 100] -- see Note [lower instance priority] instance separated_t2 [s : separated α] : t2_space α := ⟨assume x y, assume h : x ≠ y, let ⟨d, hd, (hxy : (x, y) ∉ d)⟩ := separated_def'.1 s x y h in let ⟨d', hd', (hd'd' : comp_rel d' d' ⊆ d)⟩ := comp_mem_uniformity_sets hd in have {y | (x, y) ∈ d'} ∈ 𝓝 x, from mem_nhds_left x hd', let ⟨u, hu₁, hu₂, hu₃⟩ := mem_nhds_sets_iff.mp this in have {x | (x, y) ∈ d'} ∈ 𝓝 y, from mem_nhds_right y hd', let ⟨v, hv₁, hv₂, hv₃⟩ := mem_nhds_sets_iff.mp this in have u ∩ v = ∅, from eq_empty_of_subset_empty $ assume z ⟨(h₁ : z ∈ u), (h₂ : z ∈ v)⟩, have (x, y) ∈ comp_rel d' d', from ⟨z, hu₁ h₁, hv₁ h₂⟩, hxy $ hd'd' this, ⟨u, v, hu₂, hv₂, hu₃, hv₃, this⟩⟩ @[priority 100] -- see Note [lower instance priority] instance separated_regular [separated α] : regular_space α := { regular := λs a hs ha, have -s ∈ 𝓝 a, from mem_nhds_sets hs ha, have {p : α × α | p.1 = a → p.2 ∈ -s} ∈ 𝓤 α, from mem_nhds_uniformity_iff.mp this, let ⟨d, hd, h⟩ := comp_mem_uniformity_sets this in let e := {y:α| (a, y) ∈ d} in have hae : a ∈ closure e, from subset_closure $ refl_mem_uniformity hd, have set.prod (closure e) (closure e) ⊆ comp_rel d (comp_rel (set.prod e e) d), begin rw [←closure_prod_eq, closure_eq_inter_uniformity], change (⨅d' ∈ 𝓤 α, _) ≤ comp_rel d (comp_rel _ d), exact (infi_le_of_le d $ infi_le_of_le hd $ le_refl _) end, have e_subset : closure e ⊆ -s, from assume a' ha', let ⟨x, (hx : (a, x) ∈ d), y, ⟨hx₁, hx₂⟩, (hy : (y, _) ∈ d)⟩ := @this ⟨a, a'⟩ ⟨hae, ha'⟩ in have (a, a') ∈ comp_rel d d, from ⟨y, hx₂, hy⟩, h this rfl, have closure e ∈ 𝓝 a, from (𝓝 a).sets_of_superset (mem_nhds_left a hd) subset_closure, have 𝓝 a ⊓ principal (-closure e) = ⊥, from (@inf_eq_bot_iff_le_compl _ _ _ (principal (- closure e)) (principal (closure e)) (by simp [principal_univ, union_comm]) (by simp)).mpr (by simp [this]), ⟨- closure e, is_closed_closure, assume x h₁ h₂, @e_subset x h₂ h₁, this⟩, ..separated_t2 } /- separation space -/ namespace uniform_space def separation_setoid (α : Type u) [uniform_space α] : setoid α := ⟨λx y, (x, y) ∈ separation_rel α, separated_equiv⟩ local attribute [instance] separation_setoid instance separation_setoid.uniform_space {α : Type u} [u : uniform_space α] : uniform_space (quotient (separation_setoid α)) := { to_topological_space := u.to_topological_space.coinduced (λx, ⟦x⟧), uniformity := map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) u.uniformity, refl := le_trans (by simp [quotient.exists_rep]) (filter.map_mono refl_le_uniformity), symm := tendsto_map' $ by simp [prod.swap, (∘)]; exact tendsto_map.comp tendsto_swap_uniformity, comp := calc (map (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) u.uniformity).lift' (λs, comp_rel s s) = u.uniformity.lift' ((λs, comp_rel s s) ∘ image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧))) : map_lift'_eq2 $ monotone_comp_rel monotone_id monotone_id ... ≤ u.uniformity.lift' (image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ∘ (λs:set (α×α), comp_rel s (comp_rel s s))) : lift'_mono' $ assume s hs ⟨a, b⟩ ⟨c, ⟨⟨a₁, a₂⟩, ha, a_eq⟩, ⟨⟨b₁, b₂⟩, hb, b_eq⟩⟩, begin simp at a_eq, simp at b_eq, have h : ⟦a₂⟧ = ⟦b₁⟧, { rw [a_eq.right, b_eq.left] }, have h : (a₂, b₁) ∈ separation_rel α := quotient.exact h, simp [function.comp, set.image, comp_rel, and.comm, and.left_comm, and.assoc], exact ⟨a₁, a_eq.left, b₂, b_eq.right, a₂, ha, b₁, h s hs, hb⟩ end ... = map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) (u.uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s))) : by rw [map_lift'_eq]; exact monotone_comp_rel monotone_id (monotone_comp_rel monotone_id monotone_id) ... ≤ map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) u.uniformity : map_mono comp_le_uniformity3, is_open_uniformity := assume s, have ∀a, ⟦a⟧ ∈ s → ({p:α×α | p.1 = a → ⟦p.2⟧ ∈ s} ∈ 𝓤 α ↔ {p:α×α | p.1 ≈ a → ⟦p.2⟧ ∈ s} ∈ 𝓤 α), from assume a ha, ⟨assume h, let ⟨t, ht, hts⟩ := comp_mem_uniformity_sets h in have hts : ∀{a₁ a₂}, (a, a₁) ∈ t → (a₁, a₂) ∈ t → ⟦a₂⟧ ∈ s, from assume a₁ a₂ ha₁ ha₂, @hts (a, a₂) ⟨a₁, ha₁, ha₂⟩ rfl, have ht' : ∀{a₁ a₂}, a₁ ≈ a₂ → (a₁, a₂) ∈ t, from assume a₁ a₂ h, sInter_subset_of_mem ht h, u.uniformity.sets_of_superset ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' $ setoid.symm h₂) h₁, assume h, u.uniformity.sets_of_superset h $ by simp {contextual := tt}⟩, begin simp [topological_space.coinduced, u.is_open_uniformity, uniformity, forall_quotient_iff], exact ⟨λh a ha, (this a ha).mp $ h a ha, λh a ha, (this a ha).mpr $ h a ha⟩ end } lemma uniformity_quotient : 𝓤 (quotient (separation_setoid α)) = (𝓤 α).map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) := rfl lemma uniform_continuous_quotient_mk : uniform_continuous (quotient.mk : α → quotient (separation_setoid α)) := le_refl _ lemma uniform_continuous_quotient {f : quotient (separation_setoid α) → β} (hf : uniform_continuous (λx, f ⟦x⟧)) : uniform_continuous f := hf lemma uniform_continuous_quotient_lift {f : α → β} {h : ∀a b, (a, b) ∈ separation_rel α → f a = f b} (hf : uniform_continuous f) : uniform_continuous (λa, quotient.lift f h a) := uniform_continuous_quotient hf lemma uniform_continuous_quotient_lift₂ {f : α → β → γ} {h : ∀a c b d, (a, b) ∈ separation_rel α → (c, d) ∈ separation_rel β → f a c = f b d} (hf : uniform_continuous (λp:α×β, f p.1 p.2)) : uniform_continuous (λp:_×_, quotient.lift₂ f h p.1 p.2) := begin rw [uniform_continuous, uniformity_prod_eq_prod, uniformity_quotient, uniformity_quotient, filter.prod_map_map_eq, filter.tendsto_map'_iff, filter.tendsto_map'_iff], rwa [uniform_continuous, uniformity_prod_eq_prod, filter.tendsto_map'_iff] at hf end lemma comap_quotient_le_uniformity : (𝓤 $ quotient $ separation_setoid α).comap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ≤ (𝓤 α) := assume t' ht', let ⟨t, ht, tt_t'⟩ := comp_mem_uniformity_sets ht' in let ⟨s, hs, ss_t⟩ := comp_mem_uniformity_sets ht in ⟨(λp:α×α, (⟦p.1⟧, ⟦p.2⟧)) '' s, (𝓤 α).sets_of_superset hs $ assume x hx, ⟨x, hx, rfl⟩, assume ⟨a₁, a₂⟩ ⟨⟨b₁, b₂⟩, hb, ab_eq⟩, have ⟦b₁⟧ = ⟦a₁⟧ ∧ ⟦b₂⟧ = ⟦a₂⟧, from prod.mk.inj ab_eq, have b₁ ≈ a₁ ∧ b₂ ≈ a₂, from and.imp quotient.exact quotient.exact this, have ab₁ : (a₁, b₁) ∈ t, from (setoid.symm this.left) t ht, have ba₂ : (b₂, a₂) ∈ s, from this.right s hs, tt_t' ⟨b₁, show ((a₁, a₂).1, b₁) ∈ t, from ab₁, ss_t ⟨b₂, show ((b₁, a₂).1, b₂) ∈ s, from hb, ba₂⟩⟩⟩ lemma comap_quotient_eq_uniformity : (𝓤 $ quotient $ separation_setoid α).comap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) = 𝓤 α := le_antisymm comap_quotient_le_uniformity le_comap_map instance separated_separation : separated (quotient (separation_setoid α)) := set.ext $ assume ⟨a, b⟩, quotient.induction_on₂ a b $ assume a b, ⟨assume h, have a ≈ b, from assume s hs, have s ∈ (𝓤 $ quotient $ separation_setoid α).comap (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)), from comap_quotient_le_uniformity hs, let ⟨t, ht, hts⟩ := this in hts begin dsimp [preimage], exact h t ht end, show ⟦a⟧ = ⟦b⟧, from quotient.sound this, assume heq : ⟦a⟧ = ⟦b⟧, assume h hs, heq ▸ refl_mem_uniformity hs⟩ lemma separated_of_uniform_continuous {f : α → β} {x y : α} (H : uniform_continuous f) (h : x ≈ y) : f x ≈ f y := assume _ h', h _ (H h') lemma eq_of_separated_of_uniform_continuous [separated β] {f : α → β} {x y : α} (H : uniform_continuous f) (h : x ≈ y) : f x = f y := separated_def.1 (by apply_instance) _ _ $ separated_of_uniform_continuous H h def separation_quotient (α : Type*) [uniform_space α] := quotient (separation_setoid α) namespace separation_quotient instance : uniform_space (separation_quotient α) := by dunfold separation_quotient ; apply_instance instance : separated (separation_quotient α) := by dunfold separation_quotient ; apply_instance instance [inhabited α] : inhabited (separation_quotient α) := by unfold separation_quotient; apply_instance def lift [separated β] (f : α → β) : (separation_quotient α → β) := if h : uniform_continuous f then quotient.lift f (λ x y, eq_of_separated_of_uniform_continuous h) else λ x, f (classical.inhabited_of_nonempty $ (nonempty_quotient_iff $ separation_setoid α).1 ⟨x⟩).default lemma lift_mk [separated β] {f : α → β} (h : uniform_continuous f) (a : α) : lift f ⟦a⟧ = f a := by rw [lift, dif_pos h]; refl lemma uniform_continuous_lift [separated β] (f : α → β) : uniform_continuous (lift f) := begin by_cases hf : uniform_continuous f, { rw [lift, dif_pos hf], exact uniform_continuous_quotient_lift hf }, { rw [lift, dif_neg hf], exact uniform_continuous_of_const (assume a b, rfl) } end def map (f : α → β) : separation_quotient α → separation_quotient β := lift (quotient.mk ∘ f) lemma map_mk {f : α → β} (h : uniform_continuous f) (a : α) : map f ⟦a⟧ = ⟦f a⟧ := by rw [map, lift_mk (uniform_continuous_quotient_mk.comp h)] lemma uniform_continuous_map (f : α → β) : uniform_continuous (map f) := uniform_continuous_lift (quotient.mk ∘ f) lemma map_unique {f : α → β} (hf : uniform_continuous f) {g : separation_quotient α → separation_quotient β} (comm : quotient.mk ∘ f = g ∘ quotient.mk) : map f = g := by ext ⟨a⟩; calc map f ⟦a⟧ = ⟦f a⟧ : map_mk hf a ... = g ⟦a⟧ : congr_fun comm a lemma map_id : map (@id α) = id := map_unique uniform_continuous_id rfl lemma map_comp {f : α → β} {g : β → γ} (hf : uniform_continuous f) (hg : uniform_continuous g) : map g ∘ map f = map (g ∘ f) := (map_unique (hg.comp hf) $ by simp only [(∘), map_mk, hf, hg]).symm end separation_quotient lemma separation_prod {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) ≈ (a₂, b₂) ↔ a₁ ≈ a₂ ∧ b₁ ≈ b₂ := begin split, { assume h, exact ⟨separated_of_uniform_continuous uniform_continuous_fst h, separated_of_uniform_continuous uniform_continuous_snd h⟩ }, { rintros ⟨eqv_α, eqv_β⟩ r r_in, rw uniformity_prod at r_in, rcases r_in with ⟨t_α, ⟨r_α, r_α_in, h_α⟩, t_β, ⟨r_β, r_β_in, h_β⟩, H⟩, let p_α := λ(p : (α × β) × (α × β)), (p.1.1, p.2.1), let p_β := λ(p : (α × β) × (α × β)), (p.1.2, p.2.2), have key_α : p_α ((a₁, b₁), (a₂, b₂)) ∈ r_α, { simp [p_α, eqv_α r_α r_α_in] }, have key_β : p_β ((a₁, b₁), (a₂, b₂)) ∈ r_β, { simp [p_β, eqv_β r_β r_β_in] }, exact H ⟨h_α key_α, h_β key_β⟩ }, end instance separated.prod [separated α] [separated β] : separated (α × β) := separated_def.2 $ assume x y H, prod.ext (eq_of_separated_of_uniform_continuous uniform_continuous_fst H) (eq_of_separated_of_uniform_continuous uniform_continuous_snd H) end uniform_space
758222702031bb4864b32a3839b20cba2dcc1073
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/affine_space/independent.lean
3128935830d75c92a6645832acddb8f62ba78717
[ "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
32,645
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import data.finset.sort import data.fin.vec_notation import linear_algebra.affine_space.combination import linear_algebra.affine_space.affine_equiv import linear_algebra.basis /-! # Affine independence This file defines affinely independent families of points. ## Main definitions * `affine_independent` defines affinely independent families of points as those where no nontrivial weighted subtraction is `0`. This is proved equivalent to two other formulations: linear independence of the results of subtracting a base point in the family from the other points in the family, or any equal affine combinations having the same weights. A bundled type `simplex` is provided for finite affinely independent families of points, with an abbreviation `triangle` for the case of three points. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable theory open_locale big_operators classical affine open function section affine_independent variables (k : Type*) {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V /-- An indexed family is said to be affinely independent if no nontrivial weighted subtractions (where the sum of weights is 0) are 0. -/ def affine_independent (p : ι → P) : Prop := ∀ (s : finset ι) (w : ι → k), ∑ i in s, w i = 0 → s.weighted_vsub p w = (0:V) → ∀ i ∈ s, w i = 0 /-- The definition of `affine_independent`. -/ lemma affine_independent_def (p : ι → P) : affine_independent k p ↔ ∀ (s : finset ι) (w : ι → k), ∑ i in s, w i = 0 → s.weighted_vsub p w = (0 : V) → ∀ i ∈ s, w i = 0 := iff.rfl /-- A family with at most one point is affinely independent. -/ lemma affine_independent_of_subsingleton [subsingleton ι] (p : ι → P) : affine_independent k p := λ s w h hs i hi, fintype.eq_of_subsingleton_of_sum_eq h i hi /-- A family indexed by a `fintype` is affinely independent if and only if no nontrivial weighted subtractions over `finset.univ` (where the sum of the weights is 0) are 0. -/ lemma affine_independent_iff_of_fintype [fintype ι] (p : ι → P) : affine_independent k p ↔ ∀ w : ι → k, ∑ i, w i = 0 → finset.univ.weighted_vsub p w = (0 : V) → ∀ i, w i = 0 := begin split, { exact λ h w hw hs i, h finset.univ w hw hs i (finset.mem_univ _) }, { intros h s w hw hs i hi, rw finset.weighted_vsub_indicator_subset _ _ (finset.subset_univ s) at hs, rw set.sum_indicator_subset _ (finset.subset_univ s) at hw, replace h := h ((↑s : set ι).indicator w) hw hs i, simpa [hi] using h } end /-- A family is affinely independent if and only if the differences from a base point in that family are linearly independent. -/ lemma affine_independent_iff_linear_independent_vsub (p : ι → P) (i1 : ι) : affine_independent k p ↔ linear_independent k (λ i : {x // x ≠ i1}, (p i -ᵥ p i1 : V)) := begin split, { intro h, rw linear_independent_iff', intros s g hg i hi, set f : ι → k := λ x, if hx : x = i1 then -∑ y in s, g y else g ⟨x, hx⟩ with hfdef, let s2 : finset ι := insert i1 (s.map (embedding.subtype _)), have hfg : ∀ x : {x // x ≠ i1}, g x = f x, { intro x, rw hfdef, dsimp only [], erw [dif_neg x.property, subtype.coe_eta] }, rw hfg, have hf : ∑ ι in s2, f ι = 0, { rw [finset.sum_insert (finset.not_mem_map_subtype_of_not_property s (not_not.2 rfl)), finset.sum_subtype_map_embedding (λ x hx, (hfg x).symm)], rw hfdef, dsimp only [], rw dif_pos rfl, exact neg_add_self _ }, have hs2 : s2.weighted_vsub p f = (0:V), { set f2 : ι → V := λ x, f x • (p x -ᵥ p i1) with hf2def, set g2 : {x // x ≠ i1} → V := λ x, g x • (p x -ᵥ p i1) with hg2def, have hf2g2 : ∀ x : {x // x ≠ i1}, f2 x = g2 x, { simp_rw [hf2def, hg2def, hfg], exact λ x, rfl }, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s2 f p hf (p i1), finset.weighted_vsub_of_point_insert, finset.weighted_vsub_of_point_apply, finset.sum_subtype_map_embedding (λ x hx, hf2g2 x)], exact hg }, exact h s2 f hf hs2 i (finset.mem_insert_of_mem (finset.mem_map.2 ⟨i, hi, rfl⟩)) }, { intro h, rw linear_independent_iff' at h, intros s w hw hs i hi, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p hw (p i1), ←s.weighted_vsub_of_point_erase w p i1, finset.weighted_vsub_of_point_apply] at hs, let f : ι → V := λ i, w i • (p i -ᵥ p i1), have hs2 : ∑ i in (s.erase i1).subtype (λ i, i ≠ i1), f i = 0, { rw [←hs], convert finset.sum_subtype_of_mem f (λ x, finset.ne_of_mem_erase) }, have h2 := h ((s.erase i1).subtype (λ i, i ≠ i1)) (λ x, w x) hs2, simp_rw [finset.mem_subtype] at h2, have h2b : ∀ i ∈ s, i ≠ i1 → w i = 0 := λ i his hi, h2 ⟨i, hi⟩ (finset.mem_erase_of_ne_of_mem hi his), exact finset.eq_zero_of_sum_eq_zero hw h2b i hi } end /-- A set is affinely independent if and only if the differences from a base point in that set are linearly independent. -/ lemma affine_independent_set_iff_linear_independent_vsub {s : set P} {p₁ : P} (hp₁ : p₁ ∈ s) : affine_independent k (λ p, p : s → P) ↔ linear_independent k (λ v, v : (λ p, (p -ᵥ p₁ : V)) '' (s \ {p₁}) → V) := begin rw affine_independent_iff_linear_independent_vsub k (λ p, p : s → P) ⟨p₁, hp₁⟩, split, { intro h, have hv : ∀ v : (λ p, (p -ᵥ p₁ : V)) '' (s \ {p₁}), (v : V) +ᵥ p₁ ∈ s \ {p₁} := λ v, (vsub_left_injective p₁).mem_set_image.1 ((vadd_vsub (v : V) p₁).symm ▸ v.property), let f : (λ p : P, (p -ᵥ p₁ : V)) '' (s \ {p₁}) → {x : s // x ≠ ⟨p₁, hp₁⟩} := λ x, ⟨⟨(x : V) +ᵥ p₁, set.mem_of_mem_diff (hv x)⟩, λ hx, set.not_mem_of_mem_diff (hv x) (subtype.ext_iff.1 hx)⟩, convert h.comp f (λ x1 x2 hx, (subtype.ext (vadd_right_cancel p₁ (subtype.ext_iff.1 (subtype.ext_iff.1 hx))))), ext v, exact (vadd_vsub (v : V) p₁).symm }, { intro h, let f : {x : s // x ≠ ⟨p₁, hp₁⟩} → (λ p : P, (p -ᵥ p₁ : V)) '' (s \ {p₁}) := λ x, ⟨((x : s) : P) -ᵥ p₁, ⟨x, ⟨⟨(x : s).property, λ hx, x.property (subtype.ext hx)⟩, rfl⟩⟩⟩, convert h.comp f (λ x1 x2 hx, subtype.ext (subtype.ext (vsub_left_cancel (subtype.ext_iff.1 hx)))) } end /-- A set of nonzero vectors is linearly independent if and only if, given a point `p₁`, the vectors added to `p₁` and `p₁` itself are affinely independent. -/ lemma linear_independent_set_iff_affine_independent_vadd_union_singleton {s : set V} (hs : ∀ v ∈ s, v ≠ (0 : V)) (p₁ : P) : linear_independent k (λ v, v : s → V) ↔ affine_independent k (λ p, p : {p₁} ∪ ((λ v, v +ᵥ p₁) '' s) → P) := begin rw affine_independent_set_iff_linear_independent_vsub k (set.mem_union_left _ (set.mem_singleton p₁)), have h : (λ p, (p -ᵥ p₁ : V)) '' (({p₁} ∪ (λ v, v +ᵥ p₁) '' s) \ {p₁}) = s, { simp_rw [set.union_diff_left, set.image_diff (vsub_left_injective p₁), set.image_image, set.image_singleton, vsub_self, vadd_vsub, set.image_id'], exact set.diff_singleton_eq_self (λ h, hs 0 h rfl) }, rw h end /-- A family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point have equal `set.indicator`. -/ lemma affine_independent_iff_indicator_eq_of_affine_combination_eq (p : ι → P) : affine_independent k p ↔ ∀ (s1 s2 : finset ι) (w1 w2 : ι → k), ∑ i in s1, w1 i = 1 → ∑ i in s2, w2 i = 1 → s1.affine_combination p w1 = s2.affine_combination p w2 → set.indicator ↑s1 w1 = set.indicator ↑s2 w2 := begin split, { intros ha s1 s2 w1 w2 hw1 hw2 heq, ext i, by_cases hi : i ∈ (s1 ∪ s2), { rw ←sub_eq_zero, rw set.sum_indicator_subset _ (finset.subset_union_left s1 s2) at hw1, rw set.sum_indicator_subset _ (finset.subset_union_right s1 s2) at hw2, have hws : ∑ i in s1 ∪ s2, (set.indicator ↑s1 w1 - set.indicator ↑s2 w2) i = 0, { simp [hw1, hw2] }, rw [finset.affine_combination_indicator_subset _ _ (finset.subset_union_left s1 s2), finset.affine_combination_indicator_subset _ _ (finset.subset_union_right s1 s2), ←@vsub_eq_zero_iff_eq V, finset.affine_combination_vsub] at heq, exact ha (s1 ∪ s2) (set.indicator ↑s1 w1 - set.indicator ↑s2 w2) hws heq i hi }, { rw [←finset.mem_coe, finset.coe_union] at hi, simp [mt (set.mem_union_left ↑s2) hi, mt (set.mem_union_right ↑s1) hi] } }, { intros ha s w hw hs i0 hi0, let w1 : ι → k := function.update (function.const ι 0) i0 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi0, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i0 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi0 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), let w2 := w + w1, have hw2 : ∑ i in s, w2 i = 1, { simp [w2, finset.sum_add_distrib, hw, hw1] }, have hw2s : s.affine_combination p w2 = p i0, { simp [w2, ←finset.weighted_vsub_vadd_affine_combination, hs, hw1s] }, replace ha := ha s s w2 w1 hw2 hw1 (hw1s.symm ▸ hw2s), have hws : w2 i0 - w1 i0 = 0, { rw ←finset.mem_coe at hi0, rw [←set.indicator_of_mem hi0 w2, ←set.indicator_of_mem hi0 w1, ha, sub_self] }, simpa [w2] using hws } end /-- A finite family is affinely independent if and only if any affine combinations (with sum of weights 1) that evaluate to the same point are equal. -/ lemma affine_independent_iff_eq_of_fintype_affine_combination_eq [fintype ι] (p : ι → P) : affine_independent k p ↔ ∀ (w1 w2 : ι → k), ∑ i, w1 i = 1 → ∑ i, w2 i = 1 → finset.univ.affine_combination p w1 = finset.univ.affine_combination p w2 → w1 = w2 := begin rw affine_independent_iff_indicator_eq_of_affine_combination_eq, split, { intros h w1 w2 hw1 hw2 hweq, simpa only [set.indicator_univ, finset.coe_univ] using h _ _ w1 w2 hw1 hw2 hweq, }, { intros h s1 s2 w1 w2 hw1 hw2 hweq, have hw1' : ∑ i, (s1 : set ι).indicator w1 i = 1, { rwa set.sum_indicator_subset _ (finset.subset_univ s1) at hw1, }, have hw2' : ∑ i, (s2 : set ι).indicator w2 i = 1, { rwa set.sum_indicator_subset _ (finset.subset_univ s2) at hw2, }, rw [finset.affine_combination_indicator_subset w1 p (finset.subset_univ s1), finset.affine_combination_indicator_subset w2 p (finset.subset_univ s2)] at hweq, exact h _ _ hw1' hw2' hweq, }, end variables {k} /-- If we single out one member of an affine-independent family of points and affinely transport all others along the line joining them to this member, the resulting new family of points is affine- independent. This is the affine version of `linear_independent.units_smul`. -/ lemma affine_independent.units_line_map {p : ι → P} (hp : affine_independent k p) (j : ι) (w : ι → units k) : affine_independent k (λ i, affine_map.line_map (p j) (p i) (w i : k)) := begin rw affine_independent_iff_linear_independent_vsub k _ j at hp ⊢, simp only [affine_map.line_map_vsub_left, affine_map.coe_const, affine_map.line_map_same], exact hp.units_smul (λ i, w i), end lemma affine_independent.indicator_eq_of_affine_combination_eq {p : ι → P} (ha : affine_independent k p) (s₁ s₂ : finset ι) (w₁ w₂ : ι → k) (hw₁ : ∑ i in s₁, w₁ i = 1) (hw₂ : ∑ i in s₂, w₂ i = 1) (h : s₁.affine_combination p w₁ = s₂.affine_combination p w₂) : set.indicator ↑s₁ w₁ = set.indicator ↑s₂ w₂ := (affine_independent_iff_indicator_eq_of_affine_combination_eq k p).1 ha s₁ s₂ w₁ w₂ hw₁ hw₂ h /-- An affinely independent family is injective, if the underlying ring is nontrivial. -/ protected lemma affine_independent.injective [nontrivial k] {p : ι → P} (ha : affine_independent k p) : function.injective p := begin intros i j hij, rw affine_independent_iff_linear_independent_vsub _ _ j at ha, by_contra hij', exact ha.ne_zero ⟨i, hij'⟩ (vsub_eq_zero_iff_eq.mpr hij) end /-- If a family is affinely independent, so is any subfamily given by composition of an embedding into index type with the original family. -/ lemma affine_independent.comp_embedding {ι2 : Type*} (f : ι2 ↪ ι) {p : ι → P} (ha : affine_independent k p) : affine_independent k (p ∘ f) := begin intros fs w hw hs i0 hi0, let fs' := fs.map f, let w' := λ i, if h : ∃ i2, f i2 = i then w h.some else 0, have hw' : ∀ i2 : ι2, w' (f i2) = w i2, { intro i2, have h : ∃ i : ι2, f i = f i2 := ⟨i2, rfl⟩, have hs : h.some = i2 := f.injective h.some_spec, simp_rw [w', dif_pos h, hs] }, have hw's : ∑ i in fs', w' i = 0, { rw [←hw, finset.sum_map], simp [hw'] }, have hs' : fs'.weighted_vsub p w' = (0:V), { rw [←hs, finset.weighted_vsub_map], congr' with i, simp [hw'] }, rw [←ha fs' w' hw's hs' (f i0) ((finset.mem_map' _).2 hi0), hw'] end /-- If a family is affinely independent, so is any subfamily indexed by a subtype of the index type. -/ protected lemma affine_independent.subtype {p : ι → P} (ha : affine_independent k p) (s : set ι) : affine_independent k (λ i : s, p i) := ha.comp_embedding (embedding.subtype _) /-- If an indexed family of points is affinely independent, so is the corresponding set of points. -/ protected lemma affine_independent.range {p : ι → P} (ha : affine_independent k p) : affine_independent k (λ x, x : set.range p → P) := begin let f : set.range p → ι := λ x, x.property.some, have hf : ∀ x, p (f x) = x := λ x, x.property.some_spec, let fe : set.range p ↪ ι := ⟨f, λ x₁ x₂ he, subtype.ext (hf x₁ ▸ hf x₂ ▸ he ▸ rfl)⟩, convert ha.comp_embedding fe, ext, simp [hf] end lemma affine_independent_equiv {ι' : Type*} (e : ι ≃ ι') {p : ι' → P} : affine_independent k (p ∘ e) ↔ affine_independent k p := begin refine ⟨_, affine_independent.comp_embedding e.to_embedding⟩, intros h, have : p = p ∘ e ∘ e.symm.to_embedding, { ext, simp, }, rw this, exact h.comp_embedding e.symm.to_embedding, end /-- If a set of points is affinely independent, so is any subset. -/ protected lemma affine_independent.mono {s t : set P} (ha : affine_independent k (λ x, x : t → P)) (hs : s ⊆ t) : affine_independent k (λ x, x : s → P) := ha.comp_embedding (s.embedding_of_subset t hs) /-- If the range of an injective indexed family of points is affinely independent, so is that family. -/ lemma affine_independent.of_set_of_injective {p : ι → P} (ha : affine_independent k (λ x, x : set.range p → P)) (hi : function.injective p) : affine_independent k p := ha.comp_embedding (⟨λ i, ⟨p i, set.mem_range_self _⟩, λ x y h, hi (subtype.mk_eq_mk.1 h)⟩ : ι ↪ set.range p) section composition variables {V₂ P₂ : Type*} [add_comm_group V₂] [module k V₂] [affine_space V₂ P₂] include V₂ /-- If the image of a family of points in affine space under an affine transformation is affine- independent, then the original family of points is also affine-independent. -/ lemma affine_independent.of_comp {p : ι → P} (f : P →ᵃ[k] P₂) (hai : affine_independent k (f ∘ p)) : affine_independent k p := begin cases is_empty_or_nonempty ι with h h, { haveI := h, apply affine_independent_of_subsingleton, }, obtain ⟨i⟩ := h, rw affine_independent_iff_linear_independent_vsub k p i, simp_rw [affine_independent_iff_linear_independent_vsub k (f ∘ p) i, function.comp_app, ← f.linear_map_vsub] at hai, exact linear_independent.of_comp f.linear hai, end /-- The image of a family of points in affine space, under an injective affine transformation, is affine-independent. -/ lemma affine_independent.map' {p : ι → P} (hai : affine_independent k p) (f : P →ᵃ[k] P₂) (hf : function.injective f) : affine_independent k (f ∘ p) := begin cases is_empty_or_nonempty ι with h h, { haveI := h, apply affine_independent_of_subsingleton, }, obtain ⟨i⟩ := h, rw affine_independent_iff_linear_independent_vsub k p i at hai, simp_rw [affine_independent_iff_linear_independent_vsub k (f ∘ p) i, function.comp_app, ← f.linear_map_vsub], have hf' : f.linear.ker = ⊥, { rwa [linear_map.ker_eq_bot, f.injective_iff_linear_injective], }, exact linear_independent.map' hai f.linear hf', end /-- Injective affine maps preserve affine independence. -/ lemma affine_map.affine_independent_iff {p : ι → P} (f : P →ᵃ[k] P₂) (hf : function.injective f) : affine_independent k (f ∘ p) ↔ affine_independent k p := ⟨affine_independent.of_comp f, λ hai, affine_independent.map' hai f hf⟩ /-- Affine equivalences preserve affine independence of families of points. -/ lemma affine_equiv.affine_independent_iff {p : ι → P} (e : P ≃ᵃ[k] P₂) : affine_independent k (e ∘ p) ↔ affine_independent k p := e.to_affine_map.affine_independent_iff e.to_equiv.injective /-- Affine equivalences preserve affine independence of subsets. -/ lemma affine_equiv.affine_independent_set_of_eq_iff {s : set P} (e : P ≃ᵃ[k] P₂) : affine_independent k (coe : (e '' s) → P₂) ↔ affine_independent k (coe : s → P) := begin have : e ∘ (coe : s → P) = (coe : e '' s → P₂) ∘ ((e : P ≃ P₂).image s) := rfl, rw [← e.affine_independent_iff, this, affine_independent_equiv], end end composition /-- If a family is affinely independent, and the spans of points indexed by two subsets of the index type have a point in common, those subsets of the index type have an element in common, if the underlying ring is nontrivial. -/ lemma affine_independent.exists_mem_inter_of_exists_mem_inter_affine_span [nontrivial k] {p : ι → P} (ha : affine_independent k p) {s1 s2 : set ι} {p0 : P} (hp0s1 : p0 ∈ affine_span k (p '' s1)) (hp0s2 : p0 ∈ affine_span k (p '' s2)): ∃ (i : ι), i ∈ s1 ∩ s2 := begin rw set.image_eq_range at hp0s1 hp0s2, rw [mem_affine_span_iff_eq_affine_combination, ←finset.eq_affine_combination_subset_iff_eq_affine_combination_subtype] at hp0s1 hp0s2, rcases hp0s1 with ⟨fs1, hfs1, w1, hw1, hp0s1⟩, rcases hp0s2 with ⟨fs2, hfs2, w2, hw2, hp0s2⟩, rw affine_independent_iff_indicator_eq_of_affine_combination_eq at ha, replace ha := ha fs1 fs2 w1 w2 hw1 hw2 (hp0s1 ▸ hp0s2), have hnz : ∑ i in fs1, w1 i ≠ 0 := hw1.symm ▸ one_ne_zero, rcases finset.exists_ne_zero_of_sum_ne_zero hnz with ⟨i, hifs1, hinz⟩, simp_rw [←set.indicator_of_mem (finset.mem_coe.2 hifs1) w1, ha] at hinz, use [i, hfs1 hifs1, hfs2 (set.mem_of_indicator_ne_zero hinz)] end /-- If a family is affinely independent, the spans of points indexed by disjoint subsets of the index type are disjoint, if the underlying ring is nontrivial. -/ lemma affine_independent.affine_span_disjoint_of_disjoint [nontrivial k] {p : ι → P} (ha : affine_independent k p) {s1 s2 : set ι} (hd : s1 ∩ s2 = ∅) : (affine_span k (p '' s1) : set P) ∩ affine_span k (p '' s2) = ∅ := begin by_contradiction hne, change (affine_span k (p '' s1) : set P) ∩ affine_span k (p '' s2) ≠ ∅ at hne, rw set.ne_empty_iff_nonempty at hne, rcases hne with ⟨p0, hp0s1, hp0s2⟩, cases ha.exists_mem_inter_of_exists_mem_inter_affine_span hp0s1 hp0s2 with i hi, exact set.not_mem_empty i (hd ▸ hi) end /-- If a family is affinely independent, a point in the family is in the span of some of the points given by a subset of the index type if and only if that point's index is in the subset, if the underlying ring is nontrivial. -/ @[simp] protected lemma affine_independent.mem_affine_span_iff [nontrivial k] {p : ι → P} (ha : affine_independent k p) (i : ι) (s : set ι) : p i ∈ affine_span k (p '' s) ↔ i ∈ s := begin split, { intro hs, have h := affine_independent.exists_mem_inter_of_exists_mem_inter_affine_span ha hs (mem_affine_span k (set.mem_image_of_mem _ (set.mem_singleton _))), rwa [←set.nonempty_def, set.inter_singleton_nonempty] at h }, { exact λ h, mem_affine_span k (set.mem_image_of_mem p h) } end /-- If a family is affinely independent, a point in the family is not in the affine span of the other points, if the underlying ring is nontrivial. -/ lemma affine_independent.not_mem_affine_span_diff [nontrivial k] {p : ι → P} (ha : affine_independent k p) (i : ι) (s : set ι) : p i ∉ affine_span k (p '' (s \ {i})) := by simp [ha] lemma exists_nontrivial_relation_sum_zero_of_not_affine_ind {t : finset V} (h : ¬ affine_independent k (coe : t → V)) : ∃ f : V → k, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin classical, rw affine_independent_iff_of_fintype at h, simp only [exists_prop, not_forall] at h, obtain ⟨w, hw, hwt, i, hi⟩ := h, simp only [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w (coe : t → V) hw 0, vsub_eq_sub, finset.weighted_vsub_of_point_apply, sub_zero] at hwt, let f : Π (x : V), x ∈ t → k := λ x hx, w ⟨x, hx⟩, refine ⟨λ x, if hx : x ∈ t then f x hx else (0 : k), _, _, by { use i, simp [hi, f], }⟩, suffices : ∑ (e : V) in t, dite (e ∈ t) (λ hx, (f e hx) • e) (λ hx, 0) = 0, { convert this, ext, by_cases hx : x ∈ t; simp [hx], }, all_goals { simp only [finset.sum_dite_of_true (λx h, h), subtype.val_eq_coe, finset.mk_coe, f, hwt, hw], }, end /-- Viewing a module as an affine space modelled on itself, we can characterise affine independence in terms of linear combinations. -/ lemma affine_independent_iff {ι} {p : ι → V} : affine_independent k p ↔ ∀ (s : finset ι) (w : ι → k), s.sum w = 0 → ∑ e in s, w e • p e = 0 → ∀ (e ∈ s), w e = 0 := forall₃_congr (λ s w hw, by simp [s.weighted_vsub_eq_linear_combination hw]) end affine_independent section division_ring variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V /-- An affinely independent set of points can be extended to such a set that spans the whole space. -/ lemma exists_subset_affine_independent_affine_span_eq_top {s : set P} (h : affine_independent k (λ p, p : s → P)) : ∃ t : set P, s ⊆ t ∧ affine_independent k (λ p, p : t → P) ∧ affine_span k t = ⊤ := begin rcases s.eq_empty_or_nonempty with rfl | ⟨p₁, hp₁⟩, { have p₁ : P := add_torsor.nonempty.some, let hsv := basis.of_vector_space k V, have hsvi := hsv.linear_independent, have hsvt := hsv.span_eq, rw basis.coe_of_vector_space at hsvi hsvt, have h0 : ∀ v : V, v ∈ (basis.of_vector_space_index _ _) → v ≠ 0, { intros v hv, simpa using hsv.ne_zero ⟨v, hv⟩ }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k h0 p₁ at hsvi, exact ⟨{p₁} ∪ (λ v, v +ᵥ p₁) '' _, set.empty_subset _, hsvi, affine_span_singleton_union_vadd_eq_top_of_span_eq_top p₁ hsvt⟩ }, { rw affine_independent_set_iff_linear_independent_vsub k hp₁ at h, let bsv := basis.extend h, have hsvi := bsv.linear_independent, have hsvt := bsv.span_eq, rw basis.coe_extend at hsvi hsvt, have hsv := h.subset_extend (set.subset_univ _), have h0 : ∀ v : V, v ∈ (h.extend _) → v ≠ 0, { intros v hv, simpa using bsv.ne_zero ⟨v, hv⟩ }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k h0 p₁ at hsvi, refine ⟨{p₁} ∪ (λ v, v +ᵥ p₁) '' h.extend (set.subset_univ _), _, _⟩, { refine set.subset.trans _ (set.union_subset_union_right _ (set.image_subset _ hsv)), simp [set.image_image] }, { use [hsvi, affine_span_singleton_union_vadd_eq_top_of_span_eq_top p₁ hsvt] } } end variables (k V) lemma exists_affine_independent (s : set P) : ∃ t ⊆ s, affine_span k t = affine_span k s ∧ affine_independent k (coe : t → P) := begin rcases s.eq_empty_or_nonempty with rfl | ⟨p, hp⟩, { exact ⟨∅, set.empty_subset ∅, rfl, affine_independent_of_subsingleton k _⟩, }, obtain ⟨b, hb₁, hb₂, hb₃⟩ := exists_linear_independent k ((equiv.vadd_const p).symm '' s), have hb₀ : ∀ (v : V), v ∈ b → v ≠ 0, { exact λ v hv, hb₃.ne_zero (⟨v, hv⟩ : b), }, rw linear_independent_set_iff_affine_independent_vadd_union_singleton k hb₀ p at hb₃, refine ⟨{p} ∪ (equiv.vadd_const p) '' b, _, _, hb₃⟩, { apply set.union_subset (set.singleton_subset_iff.mpr hp), rwa ← (equiv.vadd_const p).subset_image' b s, }, { rw [equiv.coe_vadd_const_symm, ← vector_span_eq_span_vsub_set_right k hp] at hb₂, apply affine_subspace.ext_of_direction_eq, { have : submodule.span k b = submodule.span k (insert 0 b), { simp, }, simp only [direction_affine_span, ← hb₂, equiv.coe_vadd_const, set.singleton_union, vector_span_eq_span_vsub_set_right k (set.mem_insert p _), this], congr, change (equiv.vadd_const p).symm '' insert p ((equiv.vadd_const p) '' b) = _, rw [set.image_insert_eq, ← set.image_comp], simp, }, { use p, simp only [equiv.coe_vadd_const, set.singleton_union, set.mem_inter_eq, coe_affine_span], exact ⟨mem_span_points k _ _ (set.mem_insert p _), mem_span_points k _ _ hp⟩, }, }, end variables (k) {V P} /-- Two different points are affinely independent. -/ lemma affine_independent_of_ne {p₁ p₂ : P} (h : p₁ ≠ p₂) : affine_independent k ![p₁, p₂] := begin rw affine_independent_iff_linear_independent_vsub k ![p₁, p₂] 0, let i₁ : {x // x ≠ (0 : fin 2)} := ⟨1, by norm_num⟩, have he' : ∀ i, i = i₁, { rintro ⟨i, hi⟩, ext, fin_cases i, { simpa using hi } }, haveI : unique {x // x ≠ (0 : fin 2)} := ⟨⟨i₁⟩, he'⟩, have hz : (![p₁, p₂] ↑default -ᵥ ![p₁, p₂] 0 : V) ≠ 0, { rw he' default, simpa using h.symm }, exact linear_independent_unique _ hz end end division_ring namespace affine variables (k : Type*) {V : Type*} (P : Type*) [ring k] [add_comm_group V] [module k V] variables [affine_space V P] include V /-- A `simplex k P n` is a collection of `n + 1` affinely independent points. -/ structure simplex (n : ℕ) := (points : fin (n + 1) → P) (independent : affine_independent k points) /-- A `triangle k P` is a collection of three affinely independent points. -/ abbreviation triangle := simplex k P 2 namespace simplex variables {P} /-- Construct a 0-simplex from a point. -/ def mk_of_point (p : P) : simplex k P 0 := ⟨λ _, p, affine_independent_of_subsingleton k _⟩ /-- The point in a simplex constructed with `mk_of_point`. -/ @[simp] lemma mk_of_point_points (p : P) (i : fin 1) : (mk_of_point k p).points i = p := rfl instance [inhabited P] : inhabited (simplex k P 0) := ⟨mk_of_point k default⟩ instance nonempty : nonempty (simplex k P 0) := ⟨mk_of_point k $ add_torsor.nonempty.some⟩ variables {k V} /-- Two simplices are equal if they have the same points. -/ @[ext] lemma ext {n : ℕ} {s1 s2 : simplex k P n} (h : ∀ i, s1.points i = s2.points i) : s1 = s2 := begin cases s1, cases s2, congr' with i, exact h i end /-- Two simplices are equal if and only if they have the same points. -/ lemma ext_iff {n : ℕ} (s1 s2 : simplex k P n): s1 = s2 ↔ ∀ i, s1.points i = s2.points i := ⟨λ h _, h ▸ rfl, ext⟩ /-- A face of a simplex is a simplex with the given subset of points. -/ def face {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : simplex k P m := ⟨s.points ∘ fs.order_emb_of_fin h, s.independent.comp_embedding (fs.order_emb_of_fin h).to_embedding⟩ /-- The points of a face of a simplex are given by `mono_of_fin`. -/ lemma face_points {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) (i : fin (m + 1)) : (s.face h).points i = s.points (fs.order_emb_of_fin h i) := rfl /-- The points of a face of a simplex are given by `mono_of_fin`. -/ lemma face_points' {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : (s.face h).points = s.points ∘ (fs.order_emb_of_fin h) := rfl /-- A single-point face equals the 0-simplex constructed with `mk_of_point`. -/ @[simp] lemma face_eq_mk_of_point {n : ℕ} (s : simplex k P n) (i : fin (n + 1)) : s.face (finset.card_singleton i) = mk_of_point k (s.points i) := by { ext, simp [face_points] } /-- The set of points of a face. -/ @[simp] lemma range_face_points {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : set.range (s.face h).points = s.points '' ↑fs := by rw [face_points', set.range_comp, finset.range_order_emb_of_fin] end simplex end affine namespace affine namespace simplex variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] [affine_space V P] include V /-- The centroid of a face of a simplex as the centroid of a subset of the points. -/ @[simp] lemma face_centroid_eq_centroid {n : ℕ} (s : simplex k P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : finset.univ.centroid k (s.face h).points = fs.centroid k s.points := begin convert (finset.univ.centroid_map k (fs.order_emb_of_fin h).to_embedding s.points).symm, rw [← finset.coe_inj, finset.coe_map, finset.coe_univ, set.image_univ], simp end /-- Over a characteristic-zero division ring, the centroids given by two subsets of the points of a simplex are equal if and only if those faces are given by the same subset of points. -/ @[simp] lemma centroid_eq_iff [char_zero k] {n : ℕ} (s : simplex k P n) {fs₁ fs₂ : finset (fin (n + 1))} {m₁ m₂ : ℕ} (h₁ : fs₁.card = m₁ + 1) (h₂ : fs₂.card = m₂ + 1) : fs₁.centroid k s.points = fs₂.centroid k s.points ↔ fs₁ = fs₂ := begin refine ⟨λ h, _, congr_arg _⟩, rw [finset.centroid_eq_affine_combination_fintype, finset.centroid_eq_affine_combination_fintype] at h, have ha := (affine_independent_iff_indicator_eq_of_affine_combination_eq k s.points).1 s.independent _ _ _ _ (fs₁.sum_centroid_weights_indicator_eq_one_of_card_eq_add_one k h₁) (fs₂.sum_centroid_weights_indicator_eq_one_of_card_eq_add_one k h₂) h, simp_rw [finset.coe_univ, set.indicator_univ, function.funext_iff, finset.centroid_weights_indicator_def, finset.centroid_weights, h₁, h₂] at ha, ext i, specialize ha i, have key : ∀ n : ℕ, (n : k) + 1 ≠ 0 := λ n h, by norm_cast at h, -- we should be able to golf this to `refine ⟨λ hi, decidable.by_contradiction (λ hni, _), ...⟩`, -- but for some unknown reason it doesn't work. split; intro hi; by_contra hni, { simpa [hni, hi, key] using ha }, { simpa [hni, hi, key] using ha.symm } end /-- Over a characteristic-zero division ring, the centroids of two faces of a simplex are equal if and only if those faces are given by the same subset of points. -/ lemma face_centroid_eq_iff [char_zero k] {n : ℕ} (s : simplex k P n) {fs₁ fs₂ : finset (fin (n + 1))} {m₁ m₂ : ℕ} (h₁ : fs₁.card = m₁ + 1) (h₂ : fs₂.card = m₂ + 1) : finset.univ.centroid k (s.face h₁).points = finset.univ.centroid k (s.face h₂).points ↔ fs₁ = fs₂ := begin rw [face_centroid_eq_centroid, face_centroid_eq_centroid], exact s.centroid_eq_iff h₁ h₂ end /-- Two simplices with the same points have the same centroid. -/ lemma centroid_eq_of_range_eq {n : ℕ} {s₁ s₂ : simplex k P n} (h : set.range s₁.points = set.range s₂.points) : finset.univ.centroid k s₁.points = finset.univ.centroid k s₂.points := begin rw [←set.image_univ, ←set.image_univ, ←finset.coe_univ] at h, exact finset.univ.centroid_eq_of_inj_on_of_image_eq k _ (λ _ _ _ _ he, affine_independent.injective s₁.independent he) (λ _ _ _ _ he, affine_independent.injective s₂.independent he) h end end simplex end affine
1d50c5c47cdf09068527b15674c7b961e99a373f
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/aops/1977-USAMO-Problem_3.lean
84605fd703c145da9e0d16a8014da968715e07ff
[]
no_license
ahayat16/lean_exos
d4f08c30adb601a06511a71b5ffb4d22d12ef77f
682f2552d5b04a8c8eb9e4ab15f875a91b03845c
refs/heads/main
1,693,101,073,585
1,636,479,336,000
1,636,479,336,000
415,000,441
0
0
null
null
null
null
UTF-8
Lean
false
false
546
lean
import data.real.basic import data.polynomial.basic import data.polynomial.ring_division import data.complex.basic theorem USAMO_Problem_3_1977 (a b : ℂ): (a ∈ polynomial.roots ((polynomial.monomial 4 (1:ℂ ) ) + (polynomial.monomial 3 (1:ℂ) ) - 1) ∧ b ∈ polynomial.roots ((polynomial.monomial 4 (1:ℂ) ) + (polynomial.monomial 3 (1:ℂ) ) - 1)) → (a*b) ∈ polynomial.roots (polynomial.monomial 6 (1:ℂ ) + polynomial.monomial 4 (1:ℂ ) + polynomial.monomial 3 (1:ℂ ) - polynomial.monomial 2 (1:ℂ ) -1) := sorry
efd99f44fa45bc8131d433ac69c2062ed8615a68
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/ring_theory/coprime/lemmas.lean
5d47a41c1fd3485c653dc5a2eb4a244a1ebf5dd6
[ "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
4,415
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Ken Lee, Chris Hughes -/ import algebra.big_operators.basic import data.fintype.basic import data.int.gcd import data.set.pairwise import ring_theory.coprime.basic /-! # Additional lemmas about elements of a ring satisfying `is_coprime` These lemmas are in a separate file to the definition of `is_coprime` as they require more imports. Notably, this includes lemmas about `finset.prod` as this requires importing big_operators, and lemmas about `has_pow` since these are easiest to prove via `finset.prod`. -/ universes u v variables {R : Type u} {I : Type v} [comm_semiring R] {x y z : R} {s : I → R} {t : finset I} open_locale big_operators classical theorem nat.is_coprime_iff_coprime {m n : ℕ} : is_coprime (m : ℤ) n ↔ nat.coprime m n := ⟨λ ⟨a, b, H⟩, nat.eq_one_of_dvd_one $ int.coe_nat_dvd.1 $ by { rw [int.coe_nat_one, ← H], exact dvd_add (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_left m n) _) (dvd_mul_of_dvd_right (int.coe_nat_dvd.2 $ nat.gcd_dvd_right m n) _) }, λ H, ⟨nat.gcd_a m n, nat.gcd_b m n, by rw [mul_comm _ (m : ℤ), mul_comm _ (n : ℤ), ← nat.gcd_eq_gcd_ab, show _ = _, from H, int.coe_nat_one]⟩⟩ theorem is_coprime.prod_left : (∀ i ∈ t, is_coprime (s i) x) → is_coprime (∏ i in t, s i) x := finset.induction_on t (λ _, is_coprime_one_left) $ λ b t hbt ih H, by { rw finset.prod_insert hbt, rw finset.forall_mem_insert at H, exact H.1.mul_left (ih H.2) } theorem is_coprime.prod_right : (∀ i ∈ t, is_coprime x (s i)) → is_coprime x (∏ i in t, s i) := by simpa only [is_coprime_comm] using is_coprime.prod_left theorem is_coprime.prod_left_iff : is_coprime (∏ i in t, s i) x ↔ ∀ i ∈ t, is_coprime (s i) x := finset.induction_on t (iff_of_true is_coprime_one_left $ λ _, false.elim) $ λ b t hbt ih, by rw [finset.prod_insert hbt, is_coprime.mul_left_iff, ih, finset.forall_mem_insert] theorem is_coprime.prod_right_iff : is_coprime x (∏ i in t, s i) ↔ ∀ i ∈ t, is_coprime x (s i) := by simpa only [is_coprime_comm] using is_coprime.prod_left_iff theorem is_coprime.of_prod_left (H1 : is_coprime (∏ i in t, s i) x) (i : I) (hit : i ∈ t) : is_coprime (s i) x := is_coprime.prod_left_iff.1 H1 i hit theorem is_coprime.of_prod_right (H1 : is_coprime x (∏ i in t, s i)) (i : I) (hit : i ∈ t) : is_coprime x (s i) := is_coprime.prod_right_iff.1 H1 i hit theorem finset.prod_dvd_of_coprime : ∀ (Hs : set.pairwise_on (↑t : set I) (is_coprime on s)) (Hs1 : ∀ i ∈ t, s i ∣ z), ∏ x in t, s x ∣ z := finset.induction_on t (λ _ _, one_dvd z) begin intros a r har ih Hs Hs1, rw finset.prod_insert har, have aux1 : a ∈ (↑(insert a r) : set I) := finset.mem_insert_self a r, refine (is_coprime.prod_right $ λ i hir, Hs a aux1 i _ (by { rintro rfl, exact har hir })).mul_dvd (Hs1 a aux1) (ih (Hs.mono _) $ λ i hi, Hs1 i (finset.mem_insert_of_mem hi)), { exact finset.mem_insert_of_mem hir }, { simp only [finset.coe_insert, set.subset_insert] } end theorem fintype.prod_dvd_of_coprime [fintype I] (Hs : pairwise (is_coprime on s)) (Hs1 : ∀ i, s i ∣ z) : ∏ x, s x ∣ z := finset.prod_dvd_of_coprime (Hs.pairwise_on _) (λ i _, Hs1 i) variables {m n : ℕ} theorem is_coprime.pow_left (H : is_coprime x y) : is_coprime (x ^ m) y := by { rw [← finset.card_range m, ← finset.prod_const], exact is_coprime.prod_left (λ _ _, H) } theorem is_coprime.pow_right (H : is_coprime x y) : is_coprime x (y ^ n) := by { rw [← finset.card_range n, ← finset.prod_const], exact is_coprime.prod_right (λ _ _, H) } theorem is_coprime.pow (H : is_coprime x y) : is_coprime (x ^ m) (y ^ n) := H.pow_left.pow_right theorem is_coprime.pow_left_iff (hm : 0 < m) : is_coprime (x ^ m) y ↔ is_coprime x y := begin refine ⟨λ h, _, is_coprime.pow_left⟩, rw [← finset.card_range m, ← finset.prod_const] at h, exact h.of_prod_left 0 (finset.mem_range.mpr hm), end theorem is_coprime.pow_right_iff (hm : 0 < m) : is_coprime x (y ^ m) ↔ is_coprime x y := is_coprime_comm.trans $ (is_coprime.pow_left_iff hm).trans $ is_coprime_comm theorem is_coprime.pow_iff (hm : 0 < m) (hn : 0 < n) : is_coprime (x ^ m) (y ^ n) ↔ is_coprime x y := (is_coprime.pow_left_iff hm).trans $ is_coprime.pow_right_iff hn
7df383c5f6c713c0426558003c26e470bc15136b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/list/intervals.lean
96d94416e0453f4dc69802b4d5ca35b952b5029a
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
6,958
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.list.range import data.list.bag_inter /-! # Intervals in ℕ This file defines intervals of naturals. `list.Ico m n` is the list of integers greater than `m` and strictly less than `n`. ## TODO - Define `Ioo` and `Icc`, state basic lemmas about them. - Also do the versions for integers? - One could generalise even further, defining 'locally finite partial orders', for which `set.Ico a b` is `[finite]`, and 'locally finite total orders', for which there is a list model. - Once the above is done, get rid of `data.int.range` (and maybe `list.range'`?). -/ open nat namespace list /-- `Ico n m` is the list of natural numbers `n ≤ x < m`. (Ico stands for "interval, closed-open".) See also `data/set/intervals.lean` for `set.Ico`, modelling intervals in general preorders, and `multiset.Ico` and `finset.Ico` for `n ≤ x < m` as a multiset or as a finset. -/ def Ico (n m : ℕ) : list ℕ := range' n (m - n) namespace Ico theorem zero_bot (n : ℕ) : Ico 0 n = range n := by rw [Ico, nat.sub_zero, range_eq_range'] @[simp] theorem length (n m : ℕ) : length (Ico n m) = m - n := by dsimp [Ico]; simp only [length_range'] theorem pairwise_lt (n m : ℕ) : pairwise (<) (Ico n m) := by dsimp [Ico]; simp only [pairwise_lt_range'] theorem nodup (n m : ℕ) : nodup (Ico n m) := by dsimp [Ico]; simp only [nodup_range'] @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := suffices n ≤ l ∧ l < n + (m - n) ↔ n ≤ l ∧ l < m, by simp [Ico, this], begin cases le_total n m with hnm hmn, { rw [nat.add_sub_of_le hnm] }, { rw [nat.sub_eq_zero_of_le hmn, add_zero], exact and_congr_right (assume hnl, iff.intro (assume hln, (not_le_of_gt hln hnl).elim) (assume hlm, lt_of_lt_of_le hlm hmn)) } end theorem eq_nil_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = [] := by simp [Ico, nat.sub_eq_zero_of_le h] theorem map_add (n m k : ℕ) : (Ico n m).map ((+) k) = Ico (n + k) (m + k) := by rw [Ico, Ico, map_add_range', nat.add_sub_add_right, add_comm n k] theorem map_sub (n m k : ℕ) (h₁ : k ≤ n) : (Ico n m).map (λ x, x - k) = Ico (n - k) (m - k) := begin by_cases h₂ : n < m, { rw [Ico, Ico], rw nat.sub_sub_sub_cancel_right h₁, rw [map_sub_range' _ _ _ h₁] }, { simp at h₂, rw [eq_nil_of_le h₂], rw [eq_nil_of_le (nat.sub_le_sub_right h₂ _)], refl } end @[simp] theorem self_empty {n : ℕ} : Ico n n = [] := eq_nil_of_le (le_refl n) @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = [] ↔ m ≤ n := iff.intro (assume h, nat.le_of_sub_eq_zero $ by rw [← length, h]; refl) eq_nil_of_le lemma append_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ++ Ico m l = Ico n l := begin dunfold Ico, convert range'_append _ _ _, { exact (nat.add_sub_of_le hnm).symm }, { rwa [← nat.add_sub_assoc hnm, nat.sub_add_cancel] } end @[simp] lemma inter_consecutive (n m l : ℕ) : Ico n m ∩ Ico m l = [] := begin apply eq_nil_iff_forall_not_mem.2, intro a, simp only [and_imp, not_and, not_lt, list.mem_inter, list.Ico.mem], intros h₁ h₂ h₃, exfalso, exact not_lt_of_ge h₃ h₂ end @[simp] lemma bag_inter_consecutive (n m l : ℕ) : list.bag_inter (Ico n m) (Ico m l) = [] := (bag_inter_nil_iff_inter_nil _ _).2 (inter_consecutive n m l) @[simp] theorem succ_singleton {n : ℕ} : Ico n (n+1) = [n] := by dsimp [Ico]; simp [nat.add_sub_cancel_left] theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = Ico n m ++ [m] := by rwa [← succ_singleton, append_consecutive]; exact nat.le_succ _ theorem eq_cons {n m : ℕ} (h : n < m) : Ico n m = n :: Ico (n + 1) m := by rw [← append_consecutive (nat.le_succ n) h, succ_singleton]; refl @[simp] theorem pred_singleton {m : ℕ} (h : 0 < m) : Ico (m - 1) m = [m - 1] := by dsimp [Ico]; rw nat.sub_sub_self h; simp theorem chain'_succ (n m : ℕ) : chain' (λa b, b = succ a) (Ico n m) := begin by_cases n < m, { rw [eq_cons h], exact chain_succ_range' _ _ }, { rw [eq_nil_of_le (le_of_not_gt h)], trivial } end @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := by simp; intros; refl lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := filter_eq_self.2 $ assume k hk, lt_of_lt_of_le (mem.1 hk).2 hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = [] := filter_eq_nil.2 $ assume k hk, not_lt_of_le $ le_trans hln $ (mem.1 hk).1 lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := begin cases le_total n l with hnl hln, { rw [← append_consecutive hnl hlm, filter_append, filter_lt_of_top_le (le_refl l), filter_lt_of_le_bot (le_refl l), append_nil] }, { rw [eq_nil_of_le hln, filter_lt_of_le_bot hln] } end @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := begin cases le_total m l with hml hlm, { rw [min_eq_left hml, filter_lt_of_top_le hml] }, { rw [min_eq_right hlm, filter_lt_of_ge hlm] } end lemma filter_le_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, l ≤ x) = Ico n m := filter_eq_self.2 $ assume k hk, le_trans hln (mem.1 hk).1 lemma filter_le_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, l ≤ x) = [] := filter_eq_nil.2 $ assume k hk, not_le_of_gt (lt_of_lt_of_le (mem.1 hk).2 hml) lemma filter_le_of_le {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, l ≤ x) = Ico l m := begin cases le_total l m with hlm hml, { rw [← append_consecutive hnl hlm, filter_append, filter_le_of_top_le (le_refl l), filter_le_of_le_bot (le_refl l), nil_append] }, { rw [eq_nil_of_le hml, filter_le_of_top_le hml] } end @[simp] lemma filter_le (n m l : ℕ) : (Ico n m).filter (λ x, l ≤ x) = Ico (_root_.max n l) m := begin cases le_total n l with hnl hln, { rw [max_eq_right hnl, filter_le_of_le hnl] }, { rw [max_eq_left hln, filter_le_of_le_bot hln] } end lemma filter_lt_of_succ_bot {n m : ℕ} (hnm : n < m) : (Ico n m).filter (λ x, x < n + 1) = [n] := begin have r : min m (n + 1) = n + 1 := (@inf_eq_right _ _ m (n + 1)).mpr hnm, simp [filter_lt n m (n + 1), r], end @[simp] lemma filter_le_of_bot {n m : ℕ} (hnm : n < m) : (Ico n m).filter (λ x, x ≤ n) = [n] := begin rw ←filter_lt_of_succ_bot hnm, exact filter_congr (λ _ _, lt_succ_iff.symm), end /-- For any natural numbers n, a, and b, one of the following holds: 1. n < a 2. n ≥ b 3. n ∈ Ico a b -/ lemma trichotomy (n a b : ℕ) : n < a ∨ b ≤ n ∨ n ∈ Ico a b := begin by_cases h₁ : n < a, { left, exact h₁ }, { right, by_cases h₂ : n ∈ Ico a b, { right, exact h₂ }, { left, simp only [Ico.mem, not_and, not_lt] at *, exact h₂ h₁ }} end end Ico end list
b4f963ddce77ed5f069e37afd869c91bd0d90a9d
7cef822f3b952965621309e88eadf618da0c8ae9
/src/order/galois_connection.lean
ecad1163b25cb4f01ad9519b5f8160721e7b4cf7
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
11,633
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Galois connections - order theoretic adjoints. -/ import order.complete_lattice order.bounds order.order_iso open function set lattice universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a a₁ a₂ : α} {b b₁ b₂ : β} /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are closely connected to adjoint functors in category theory. -/ def galois_connection [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀a b, l a ≤ b ↔ a ≤ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection [preorder α] [preorder β] (oi : @order_iso α β (≤) (≤)) : galois_connection oi oi.symm := λ b g, by rw [order_iso.ord' oi, order_iso.apply_symm_apply] namespace galois_connection section variables [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) lemma monotone_intro (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ a, l (u a) ≤ a) : galois_connection l u := assume a b, ⟨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)⟩ include gc lemma l_le {a : α} {b : β} : a ≤ u b → l a ≤ b := (gc _ _).mpr lemma le_u {a : α} {b : β} : l a ≤ b → a ≤ u b := (gc _ _).mp lemma le_u_l (a) : a ≤ u (l a) := gc.le_u $ le_refl _ lemma l_u_le (a) : l (u a) ≤ a := gc.l_le $ le_refl _ lemma monotone_u : monotone u := assume a b H, gc.le_u (le_trans (gc.l_u_le a) H) lemma monotone_l : monotone l := assume a b H, gc.l_le (le_trans H (gc.le_u_l b)) lemma upper_bounds_l_image_subset {s : set α} : upper_bounds (l '' s) ⊆ u ⁻¹' upper_bounds s := assume b hb c, assume : c ∈ s, gc.le_u (hb (mem_image_of_mem _ ‹c ∈ s›)) lemma lower_bounds_u_image_subset {s : set β} : lower_bounds (u '' s) ⊆ l ⁻¹' lower_bounds s := assume a ha c, assume : c ∈ s, gc.l_le (ha (mem_image_of_mem _ ‹c ∈ s›)) lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) := ⟨mem_upper_bounds_image gc.monotone_l $ and.elim_left ‹is_lub s a›, assume b hb, gc.l_le $ and.elim_right ‹is_lub s a› $ gc.upper_bounds_l_image_subset hb⟩ lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) := ⟨mem_lower_bounds_image gc.monotone_u $ and.elim_left ‹is_glb s b›, assume a ha, gc.le_u $ and.elim_right ‹is_glb s b› $ gc.lower_bounds_u_image_subset ha⟩ lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) := ⟨assume b, gc.l_le, assume b h, h $ gc.le_u_l _⟩ lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) := ⟨assume b, gc.le_u, assume b h, h $ gc.l_u_le _⟩ end section partial_order variables [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_l_u_eq_u : u ∘ l ∘ u = u := funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _)) lemma l_u_l_eq_l : l ∘ u ∘ l = l := funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _))) end partial_order section order_top variables [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_top : u ⊤ = ⊤ := eq_of_is_glb_of_is_glb (gc.is_glb_u_image is_glb_empty) $ by simp [is_glb_empty, image_empty] end order_top section order_bot variables [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_bot : l ⊥ = ⊥ := eq_of_is_lub_of_is_lub (gc.is_lub_l_image is_lub_empty) $ by simp [is_lub_empty, image_empty] end order_bot section semilattice_sup variables [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_sup : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ := have {l a₂, l a₁} = l '' {a₂, a₁}, by simp [image_insert_eq, image_singleton], eq.symm $ is_lub_iff_sup_eq.mp $ by rw [this]; exact gc.is_lub_l_image (is_lub_insert_sup is_lub_singleton) end semilattice_sup section semilattice_inf variables [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_inf : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ := have {u b₂, u b₁} = u '' {b₂, b₁}, by simp [image_insert_eq, image_singleton], eq.symm $ is_glb_iff_inf_eq.mp $ by rw [this]; exact gc.is_glb_u_image (is_glb_insert_inf is_glb_singleton) end semilattice_inf section complete_lattice variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_supr {f : ι → α} : l (supr f) = (⨆i, l (f i)) := eq.symm $ is_lub_iff_supr_eq.mp $ show is_lub (range (l ∘ f)) (l (supr f)), by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image is_lub_Sup lemma u_infi {f : ι → β} : u (infi f) = (⨅i, u (f i)) := eq.symm $ is_glb_iff_infi_eq.mp $ show is_glb (range (u ∘ f)) (u (infi f)), by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image is_glb_Inf end complete_lattice section complete_lattice variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_Sup {s : set α} : l (Sup s) = (⨆a∈s, l a) := by simp [Sup_eq_supr, gc.l_supr] lemma u_Inf {s : set β} : u (Inf s) = (⨅a∈s, u a) := by simp [Inf_eq_infi, gc.u_infi] end complete_lattice /- Constructing Galois connections -/ section constructions protected lemma id [pα : preorder α] : @galois_connection α α pα pα id id := assume a b, iff.intro (λx, x) (λx, x) protected lemma compose [preorder α] [preorder β] [preorder γ] (l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := by intros a b; rw [gc2, gc1] protected lemma dual [pα : preorder α] [pβ : preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : @galois_connection (order_dual β) (order_dual α) _ _ u l := assume a b, (gc _ _).symm protected lemma dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w} [∀i, preorder (α i)] [∀i, preorder (β i)] (l : Πi, α i → β i) (u : Πi, β i → α i) (gc : ∀i, galois_connection (l i) (u i)) : @galois_connection (Π i, α i) (Π i, β i) _ _ (λa i, l i (a i)) (λb i, u i (b i)) := assume a b, forall_congr $ assume i, gc i (a i) (b i) end constructions end galois_connection namespace nat lemma galois_connection_mul_div {k : ℕ} (h : k > 0) : galois_connection (λn, n * k) (λn, n / k) := assume x y, (le_div_iff_mul_le x y h).symm end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. -/ structure galois_insertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx:α, u (l x) ≤ x → β) (gc : galois_connection l u) (le_l_u : ∀x, x ≤ l (u x)) (choice_eq : ∀a h, choice a h = l a) /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ b, l (u b) = b) : galois_insertion l u := { choice := λ x _, l x, gc := galois_connection.monotone_intro hu hl hul (λ b, le_of_eq (hlu b)), le_l_u := λ b, le_of_eq $ (hlu b).symm, choice_eq := λ _ _, rfl } /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def order_iso.to_galois_insertion [preorder α] [preorder β] (oi : @order_iso α β (≤) (≤)) : @galois_insertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi b, gc := oi.to_galois_connection, le_l_u := λ g, le_of_eq (oi.right_inv g).symm, choice_eq := λ b h, rfl } /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {α β : Type*} [order_bot α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_bot β := { bot := l ⊥, bot_le := assume b, gc.l_le $ bot_le, .. ‹partial_order β› } namespace galois_insertion open lattice variables [partial_order β] {l : α → β} {u : β → α} lemma l_u_eq [preorder α] (gi : galois_insertion l u) (b : β) : l (u b) = b := le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _) /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β := { sup := λa b, l (u a ⊔ u b), le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left, le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right, sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β := { inf := λa b, gi.choice (u a ⊓ u b) $ (le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)), inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left, inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right, le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice [lattice α] (gi : galois_insertion l u) : lattice β := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the top along a Galois insertion -/ def lift_order_top [order_top α] (gi : galois_insertion l u) : order_top β := { top := gi.choice ⊤ $ le_top, le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top), .. ‹partial_order β› } /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β := { .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot } /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β := { Sup := λs, l (⨆ b∈s, u b), Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb, le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Inf := λs, gi.choice (⨅ b∈s, u b) $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _, Inf_le := by simp only [gi.choice_eq]; exact assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _, le_Inf := by simp only [gi.choice_eq]; exact assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b, show u a ≤ ⨅ (H : b ∈ s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb, .. gi.lift_bounded_lattice } end galois_insertion
161bac9f1f33c8060b3291dce0308b25d25d2397
037dba89703a79cd4a4aec5e959818147f97635d
/src/2021/logic/sheet5.lean
eab76d8b7de01445e5f6276f49b60672f99cbb3c
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
1,414
lean
/- Copyright (c) 2021 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard -/ import tactic -- imports all the Lean tactics /-! # Logic in Lean, example sheet 5 : "iff" (`↔`) We learn about how to manipulate `P ↔ Q` in Lean. ## Tactics You'll need to know about the tactics from the previous sheets, and also the following two new tactics: * `refl` * `rw` ### The `refl` tactic If your goal is `P ↔ P` then `refl,` will solve it. ### The `rw` tactic If `h : P ↔ Q` is a hypothesis, you can decompose it using `cases h with hPQ hQP,`. However, if you keep it around then you can do `rw h,` which changes all `P`s in the goal to `Q`s. Variant: `rw h at h2,` will change all `P`s to `Q`s in hypothesis `h2`. -/ variables (P Q R S : Prop) example : P ↔ P := begin sorry end example : (P ↔ Q) → (Q ↔ P) := begin sorry end example : (P ↔ Q) ↔ (Q ↔ P) := begin sorry end example : (P ↔ Q) → (Q ↔ R) → (P ↔ R) := begin sorry end example : P ∧ Q ↔ Q ∧ P := begin sorry end example : ((P ∧ Q) ∧ R) ↔ (P ∧ (Q ∧ R)) := begin sorry end example : P ↔ (P ∧ true) := begin sorry end example : false ↔ (P ∧ false) := begin sorry end example : (P ↔ Q) → (R ↔ S) → (P ∧ R ↔ Q ∧ S) := begin sorry end example : ¬ (P ↔ ¬ P) := begin sorry, end
e83a0027e638856184ba5acac16ac064ad3b45d5
aac33c518959cd0633fdc254edbbf27b2f581c31
/src/data/complex/basic.lean
f7858bea63e3a0711e4e57b236c1fabc7b296a9d
[ "Apache-2.0" ]
permissive
digama0/mathlib-ITP2019
992c4f9ac02260fca4a14860813c3ecbd5ca1ae6
5cbd0362e04e671ef5db1284870592af6950197c
refs/heads/master
1,588,517,123,478
1,554,081,078,000
1,554,081,078,000
178,686,466
2
1
null
null
null
null
UTF-8
Lean
false
false
18,616
lean
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Mario Carneiro The complex numbers, modelled as R^2 in the obvious way. -/ import data.real.basic tactic.ring algebra.field_power structure complex : Type := (re : ℝ) (im : ℝ) notation `ℂ` := complex namespace complex @[simp] theorem eta : ∀ z : ℂ, complex.mk z.re z.im = z | ⟨a, b⟩ := rfl theorem ext : ∀ {z w : ℂ}, z.re = w.re → z.im = w.im → z = w | ⟨zr, zi⟩ ⟨_, _⟩ rfl rfl := rfl theorem ext_iff {z w : ℂ} : z = w ↔ z.re = w.re ∧ z.im = w.im := ⟨λ H, by simp [H], and.rec ext⟩ def of_real (r : ℝ) : ℂ := ⟨r, 0⟩ instance : has_coe ℝ ℂ := ⟨of_real⟩ @[simp] lemma of_real_eq_coe (r : ℝ) : of_real r = r := rfl @[simp] lemma of_real_re (r : ℝ) : (r : ℂ).re = r := rfl @[simp] lemma of_real_im (r : ℝ) : (r : ℂ).im = 0 := rfl @[simp] theorem of_real_inj {z w : ℝ} : (z : ℂ) = w ↔ z = w := ⟨congr_arg re, congr_arg _⟩ instance : has_zero ℂ := ⟨(0 : ℝ)⟩ instance : inhabited ℂ := ⟨0⟩ @[simp] lemma zero_re : (0 : ℂ).re = 0 := rfl @[simp] lemma zero_im : (0 : ℂ).im = 0 := rfl @[simp] lemma of_real_zero : ((0 : ℝ) : ℂ) = 0 := rfl @[simp] theorem of_real_eq_zero {z : ℝ} : (z : ℂ) = 0 ↔ z = 0 := of_real_inj @[simp] theorem of_real_ne_zero {z : ℝ} : (z : ℂ) ≠ 0 ↔ z ≠ 0 := not_congr of_real_eq_zero instance : has_one ℂ := ⟨(1 : ℝ)⟩ @[simp] lemma one_re : (1 : ℂ).re = 1 := rfl @[simp] lemma one_im : (1 : ℂ).im = 0 := rfl @[simp] lemma of_real_one : ((1 : ℝ) : ℂ) = 1 := rfl def I : ℂ := ⟨0, 1⟩ @[simp] lemma I_re : I.re = 0 := rfl @[simp] lemma I_im : I.im = 1 := rfl instance : has_add ℂ := ⟨λ z w, ⟨z.re + w.re, z.im + w.im⟩⟩ @[simp] lemma add_re (z w : ℂ) : (z + w).re = z.re + w.re := rfl @[simp] lemma add_im (z w : ℂ) : (z + w).im = z.im + w.im := rfl @[simp] lemma of_real_add (r s : ℝ) : ((r + s : ℝ) : ℂ) = r + s := ext_iff.2 $ by simp @[simp] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : ℂ) = bit0 r := ext_iff.2 $ by simp [bit0] @[simp] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : ℂ) = bit1 r := ext_iff.2 $ by simp [bit1] instance : has_neg ℂ := ⟨λ z, ⟨-z.re, -z.im⟩⟩ @[simp] lemma neg_re (z : ℂ) : (-z).re = -z.re := rfl @[simp] lemma neg_im (z : ℂ) : (-z).im = -z.im := rfl @[simp] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : ℂ) = -r := ext_iff.2 $ by simp instance : has_mul ℂ := ⟨λ z w, ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩⟩ @[simp] lemma mul_re (z w : ℂ) : (z * w).re = z.re * w.re - z.im * w.im := rfl @[simp] lemma mul_im (z w : ℂ) : (z * w).im = z.re * w.im + z.im * w.re := rfl @[simp] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : ℂ) = r * s := ext_iff.2 $ by simp lemma smul_re (r : ℝ) (z : ℂ) : (↑r * z).re = r * z.re := by simp lemma smul_im (r : ℝ) (z : ℂ) : (↑r * z).im = r * z.im := by simp @[simp] lemma I_mul_I : I * I = -1 := ext_iff.2 $ by simp lemma I_ne_zero : (I : ℂ) ≠ 0 := mt (congr_arg im) zero_ne_one.symm lemma mk_eq_add_mul_I (a b : ℝ) : complex.mk a b = a + b * I := ext_iff.2 $ by simp @[simp] lemma re_add_im (z : ℂ) : (z.re : ℂ) + z.im * I = z := ext_iff.2 $ by simp def real_prod_equiv : ℂ ≃ (ℝ × ℝ) := { to_fun := λ z, ⟨z.re, z.im⟩, inv_fun := λ p, ⟨p.1, p.2⟩, left_inv := λ ⟨x, y⟩, rfl, right_inv := λ ⟨x, y⟩, rfl } @[simp] theorem real_prod_equiv_apply (z : ℂ) : real_prod_equiv z = (z.re, z.im) := rfl theorem real_prod_equiv_symm_re (x y : ℝ) : (real_prod_equiv.symm (x, y)).re = x := rfl theorem real_prod_equiv_symm_im (x y : ℝ) : (real_prod_equiv.symm (x, y)).im = y := rfl def conj (z : ℂ) : ℂ := ⟨z.re, -z.im⟩ @[simp] lemma conj_re (z : ℂ) : (conj z).re = z.re := rfl @[simp] lemma conj_im (z : ℂ) : (conj z).im = -z.im := rfl @[simp] lemma conj_of_real (r : ℝ) : conj r = r := ext_iff.2 $ by simp [conj] @[simp] lemma conj_zero : conj 0 = 0 := ext_iff.2 $ by simp [conj] @[simp] lemma conj_one : conj 1 = 1 := ext_iff.2 $ by simp @[simp] lemma conj_I : conj I = -I := ext_iff.2 $ by simp @[simp] lemma conj_neg_I : conj (-I) = I := ext_iff.2 $ by simp @[simp] lemma conj_add (z w : ℂ) : conj (z + w) = conj z + conj w := ext_iff.2 $ by simp @[simp] lemma conj_neg (z : ℂ) : conj (-z) = -conj z := rfl @[simp] lemma conj_mul (z w : ℂ) : conj (z * w) = conj z * conj w := ext_iff.2 $ by simp @[simp] lemma conj_conj (z : ℂ) : conj (conj z) = z := ext_iff.2 $ by simp lemma conj_bijective : function.bijective conj := ⟨function.injective_of_has_left_inverse ⟨conj, conj_conj⟩, function.surjective_of_has_right_inverse ⟨conj, conj_conj⟩⟩ lemma conj_inj {z w : ℂ} : conj z = conj w ↔ z = w := conj_bijective.1.eq_iff @[simp] lemma conj_eq_zero {z : ℂ} : conj z = 0 ↔ z = 0 := by simpa using @conj_inj z 0 @[simp] lemma eq_conj_iff_real (z : ℂ) : conj z = z ↔ ∃ r : ℝ, z = r := ⟨λ h, ⟨z.re, ext rfl $ eq_zero_of_neg_eq (congr_arg im h)⟩, λ ⟨h, e⟩, e.symm ▸ rfl⟩ def norm_sq (z : ℂ) : ℝ := z.re * z.re + z.im * z.im @[simp] lemma norm_sq_of_real (r : ℝ) : norm_sq r = r * r := by simp [norm_sq] @[simp] lemma norm_sq_zero : norm_sq 0 = 0 := by simp [norm_sq] @[simp] lemma norm_sq_one : norm_sq 1 = 1 := by simp [norm_sq] @[simp] lemma norm_sq_I : norm_sq I = 1 := by simp [norm_sq] lemma norm_sq_nonneg (z : ℂ) : 0 ≤ norm_sq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) @[simp] lemma norm_sq_eq_zero {z : ℂ} : norm_sq z = 0 ↔ z = 0 := ⟨λ h, ext (eq_zero_of_mul_self_add_mul_self_eq_zero h) (eq_zero_of_mul_self_add_mul_self_eq_zero $ (add_comm _ _).trans h), λ h, h.symm ▸ norm_sq_zero⟩ @[simp] lemma norm_sq_pos {z : ℂ} : 0 < norm_sq z ↔ z ≠ 0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [norm_sq_nonneg] @[simp] lemma norm_sq_neg (z : ℂ) : norm_sq (-z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_conj (z : ℂ) : norm_sq (conj z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_mul (z w : ℂ) : norm_sq (z * w) = norm_sq z * norm_sq w := by dsimp [norm_sq]; ring lemma norm_sq_add (z w : ℂ) : norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (z * conj w).re := by dsimp [norm_sq]; ring lemma re_sq_le_norm_sq (z : ℂ) : z.re * z.re ≤ norm_sq z := le_add_of_nonneg_right (mul_self_nonneg _) lemma im_sq_le_norm_sq (z : ℂ) : z.im * z.im ≤ norm_sq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : ℂ) : z * conj z = norm_sq z := ext_iff.2 $ by simp [norm_sq, mul_comm] theorem add_conj (z : ℂ) : z + conj z = (2 * z.re : ℝ) := ext_iff.2 $ by simp [two_mul] instance : comm_ring ℂ := by refine { zero := 0, add := (+), neg := has_neg.neg, one := 1, mul := (*), ..}; { intros, apply ext_iff.2; split; simp; ring } @[simp] lemma bit0_re (z : ℂ) : (bit0 z).re = bit0 z.re := rfl @[simp] lemma bit1_re (z : ℂ) : (bit1 z).re = bit1 z.re := rfl @[simp] lemma bit0_im (z : ℂ) : (bit0 z).im = bit0 z.im := eq.refl _ @[simp] lemma bit1_im (z : ℂ) : (bit1 z).im = bit0 z.im := add_zero _ @[simp] lemma sub_re (z w : ℂ) : (z - w).re = z.re - w.re := rfl @[simp] lemma sub_im (z w : ℂ) : (z - w).im = z.im - w.im := rfl @[simp] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : ℂ) = r - s := ext_iff.2 $ by simp @[simp] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : ℂ) = r ^ n := by induction n; simp [*, of_real_mul, pow_succ] theorem sub_conj (z : ℂ) : z - conj z = (2 * z.im : ℝ) * I := ext_iff.2 $ by simp [two_mul] lemma conj_pow (z : ℂ) (n : ℕ) : conj (z ^ n) = conj z ^ n := by induction n; simp [*, conj_mul, pow_succ] @[simp] lemma conj_two : conj (2 : ℂ) = 2 := by apply complex.ext; simp lemma norm_sq_sub (z w : ℂ) : norm_sq (z - w) = norm_sq z + norm_sq w - 2 * (z * conj w).re := by rw [sub_eq_add_neg, norm_sq_add]; simp [-mul_re] noncomputable instance : has_inv ℂ := ⟨λ z, conj z * ((norm_sq z)⁻¹:ℝ)⟩ theorem inv_def (z : ℂ) : z⁻¹ = conj z * ((norm_sq z)⁻¹:ℝ) := rfl @[simp] lemma inv_re (z : ℂ) : (z⁻¹).re = z.re / norm_sq z := by simp [inv_def, division_def] @[simp] lemma inv_im (z : ℂ) : (z⁻¹).im = -z.im / norm_sq z := by simp [inv_def, division_def] @[simp] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : ℂ) = r⁻¹ := ext_iff.2 $ begin simp, by_cases r = 0, {simp [h]}, rw [← div_div_eq_div_mul, div_self h, one_div_eq_inv] end protected lemma inv_zero : (0⁻¹ : ℂ) = 0 := by rw [← of_real_zero, ← of_real_inv, inv_zero] protected theorem mul_inv_cancel {z : ℂ} (h : z ≠ 0) : z * z⁻¹ = 1 := by rw [inv_def, ← mul_assoc, mul_conj, ← of_real_mul, mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one] noncomputable instance : discrete_field ℂ := { inv := has_inv.inv, zero_ne_one := mt (congr_arg re) zero_ne_one, mul_inv_cancel := @complex.mul_inv_cancel, inv_mul_cancel := λ z h, by rw [mul_comm, complex.mul_inv_cancel h], inv_zero := complex.inv_zero, has_decidable_eq := classical.dec_eq _, ..complex.comm_ring } instance re.is_add_group_hom : is_add_group_hom complex.re := by refine_struct {..}; simp instance im.is_add_group_hom : is_add_group_hom complex.im := by refine_struct {..}; simp instance : is_ring_hom conj := by refine_struct {..}; simp instance of_real.is_ring_hom : is_ring_hom (coe : ℝ → ℂ) := by refine_struct {..}; simp @[simp] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : ℂ) = r / s := is_field_hom.map_div coe @[simp] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n := is_field_hom.map_fpow of_real r n @[simp] theorem of_real_int_cast : ∀ n : ℤ, ((n : ℝ) : ℂ) = n := int.eq_cast (λ n, ((n : ℝ) : ℂ)) (by rw [int.cast_one, of_real_one]) (λ _ _, by rw [int.cast_add, of_real_add]) @[simp] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : ℂ) = n := by rw [← int.cast_coe_nat, of_real_int_cast]; refl @[simp] lemma conj_inv (z : ℂ) : conj z⁻¹ = (conj z)⁻¹ := if h : z = 0 then by simp [h] else (domain.mul_left_inj (mt conj_eq_zero.1 h)).1 $ by rw [← conj_mul]; simp [h, -conj_mul] @[simp] lemma conj_sub (z w : ℂ) : conj (z - w) = conj z - conj w := by simp @[simp] lemma conj_div (z w : ℂ) : conj (z / w) = conj z / conj w := by rw [division_def, conj_mul, conj_inv]; refl @[simp] lemma norm_sq_inv (z : ℂ) : norm_sq z⁻¹ = (norm_sq z)⁻¹ := if h : z = 0 then by simp [h] else (domain.mul_left_inj (mt norm_sq_eq_zero.1 h)).1 $ by rw [← norm_sq_mul]; simp [h, -norm_sq_mul] @[simp] lemma norm_sq_div (z w : ℂ) : norm_sq (z / w) = norm_sq z / norm_sq w := by rw [division_def, norm_sq_mul, norm_sq_inv]; refl instance char_zero_complex : char_zero ℂ := add_group.char_zero_of_inj_zero $ λ n h, by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h @[simp] theorem of_real_rat_cast : ∀ n : ℚ, ((n : ℝ) : ℂ) = n := by apply rat.eq_cast (λ n, ((n : ℝ) : ℂ)); simp theorem re_eq_add_conj (z : ℂ) : (z.re : ℂ) = (z + conj z) / 2 := by rw [add_conj]; simp; rw [mul_div_cancel_left (z.re:ℂ) two_ne_zero'] @[simp] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] @[simp] lemma nat_cast_im (n : ℕ) : (n : ℂ).im = 0 := by rw [← of_real_nat_cast, of_real_im] @[simp] lemma int_cast_re (n : ℤ) : (n : ℂ).re = n := by rw [← of_real_int_cast, of_real_re] @[simp] lemma int_cast_im (n : ℤ) : (n : ℂ).im = 0 := by rw [← of_real_int_cast, of_real_im] @[simp] lemma rat_cast_re (q : ℚ) : (q : ℂ).re = q := by rw [← of_real_rat_cast, of_real_re] @[simp] lemma rat_cast_im (q : ℚ) : (q : ℂ).im = 0 := by rw [← of_real_rat_cast, of_real_im] noncomputable def abs (z : ℂ) : ℝ := (norm_sq z).sqrt local notation `abs'` := _root_.abs @[simp] lemma abs_of_real (r : ℝ) : abs r = abs' r := by simp [abs, norm_sq_of_real, real.sqrt_mul_self_eq_abs] lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : abs r = r := (abs_of_real _).trans (abs_of_nonneg h) @[simp] lemma abs_of_nat (n : ℕ) : complex.abs n = n := calc complex.abs n = complex.abs (n:ℝ) : by rw [of_real_nat_cast] ... = _ : abs_of_nonneg (nat.cast_nonneg n) lemma mul_self_abs (z : ℂ) : abs z * abs z = norm_sq z := real.mul_self_sqrt (norm_sq_nonneg _) @[simp] lemma abs_zero : abs 0 = 0 := by simp [abs] @[simp] lemma abs_one : abs 1 = 1 := by simp [abs] @[simp] lemma abs_I : abs I = 1 := by simp [abs] @[simp] lemma abs_two : abs 2 = 2 := calc abs 2 = abs (2 : ℝ) : by rw [of_real_bit0, of_real_one] ... = (2 : ℝ) : abs_of_nonneg (by norm_num) lemma abs_nonneg (z : ℂ) : 0 ≤ abs z := real.sqrt_nonneg _ @[simp] lemma abs_eq_zero {z : ℂ} : abs z = 0 ↔ z = 0 := (real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero @[simp] lemma abs_conj (z : ℂ) : abs (conj z) = abs z := by simp [abs] @[simp] lemma abs_mul (z w : ℂ) : abs (z * w) = abs z * abs w := by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl lemma abs_re_le_abs (z : ℂ) : abs' z.re ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.re) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply re_sq_le_norm_sq lemma abs_im_le_abs (z : ℂ) : abs' z.im ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.im) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply im_sq_le_norm_sq lemma re_le_abs (z : ℂ) : z.re ≤ abs z := (abs_le.1 (abs_re_le_abs _)).2 lemma im_le_abs (z : ℂ) : z.im ≤ abs z := (abs_le.1 (abs_im_le_abs _)).2 lemma abs_add (z w : ℂ) : abs (z + w) ≤ abs z + abs w := (mul_self_le_mul_self_iff (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $ begin rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs, add_right_comm, norm_sq_add, add_le_add_iff_left, mul_assoc, mul_le_mul_left (@two_pos ℝ _)], simpa [-mul_re] using re_le_abs (z * conj w) end instance : is_absolute_value abs := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value @[simp] lemma abs_abs (z : ℂ) : abs' (abs z) = abs z := _root_.abs_of_nonneg (abs_nonneg _) @[simp] lemma abs_pos {z : ℂ} : 0 < abs z ↔ z ≠ 0 := abv_pos abs @[simp] lemma abs_neg : ∀ z, abs (-z) = abs z := abv_neg abs lemma abs_sub : ∀ z w, abs (z - w) = abs (w - z) := abv_sub abs lemma abs_sub_le : ∀ a b c, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs @[simp] theorem abs_inv : ∀ z, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs @[simp] theorem abs_div : ∀ z w, abs (z / w) = abs z / abs w := abv_div abs lemma abs_abs_sub_le_abs_sub : ∀ z w, abs' (abs z - abs w) ≤ abs (z - w) := abs_abv_sub_le_abv_sub abs lemma abs_le_abs_re_add_abs_im (z : ℂ) : abs z ≤ abs' z.re + abs' z.im := by simpa [re_add_im] using abs_add z.re (z.im * I) lemma abs_re_div_abs_le_one (z : ℂ) : abs' (z.re / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by rw [_root_.abs_div, abs_abs]; exact div_le_of_le_mul (abs_pos.2 hz) (by rw mul_one; exact abs_re_le_abs _) lemma abs_im_div_abs_le_one (z : ℂ) : abs' (z.im / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by rw [_root_.abs_div, abs_abs]; exact div_le_of_le_mul (abs_pos.2 hz) (by rw mul_one; exact abs_im_le_abs _) @[simp] lemma abs_cast_nat (n : ℕ) : abs (n : ℂ) = n := by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)] lemma norm_sq_eq_abs (x : ℂ) : norm_sq x = abs x ^ 2 := by rw [abs, pow_two, real.mul_self_sqrt (norm_sq_nonneg _)] theorem is_cau_seq_re (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).re) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij) theorem is_cau_seq_im (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).im) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij) noncomputable def cau_seq_re (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_re f⟩ noncomputable def cau_seq_im (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_im f⟩ lemma is_cau_seq_abs {f : ℕ → ℂ} (hf : is_cau_seq abs f) : is_cau_seq abs' (abs ∘ f) := λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩ noncomputable def lim_aux (f : cau_seq ℂ abs) : ℂ := ⟨cau_seq.lim (cau_seq_re f), cau_seq.lim (cau_seq_im f)⟩ theorem equiv_lim_aux (f : cau_seq ℂ abs) : f ≈ cau_seq.const abs (lim_aux f) := λ ε ε0, (exists_forall_ge_and (cau_seq.equiv_lim ⟨_, is_cau_seq_re f⟩ _ (half_pos ε0)) (cau_seq.equiv_lim ⟨_, is_cau_seq_im f⟩ _ (half_pos ε0))).imp $ λ i H j ij, begin cases H _ ij with H₁ H₂, apply lt_of_le_of_lt (abs_le_abs_re_add_abs_im _), dsimp [lim_aux] at *, have := add_lt_add H₁ H₂, rwa add_halves at this, end noncomputable instance : cau_seq.is_complete ℂ abs := ⟨λ f, ⟨lim_aux f, equiv_lim_aux f⟩⟩ open cau_seq lemma lim_eq_lim_im_add_lim_re (f : cau_seq ℂ abs) : lim f = ↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I := lim_eq_of_equiv_const $ calc f ≈ _ : equiv_lim_aux f ... = cau_seq.const abs (↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I) : cau_seq.ext (λ _, complex.ext (by simp [lim_aux, cau_seq_re]) (by simp [lim_aux, cau_seq_im])) lemma lim_re (f : cau_seq ℂ abs) : lim (cau_seq_re f) = (lim f).re := by rw [lim_eq_lim_im_add_lim_re]; simp lemma lim_im (f : cau_seq ℂ abs) : lim (cau_seq_im f) = (lim f).im := by rw [lim_eq_lim_im_add_lim_re]; simp lemma is_cau_seq_conj (f : cau_seq ℂ abs) : is_cau_seq abs (λ n, conj (f n)) := λ ε ε0, let ⟨i, hi⟩ := f.2 ε ε0 in ⟨i, λ j hj, by rw [← conj_sub, abs_conj]; exact hi j hj⟩ noncomputable def cau_seq_conj (f : cau_seq ℂ abs) : cau_seq ℂ abs := ⟨_, is_cau_seq_conj f⟩ lemma lim_conj (f : cau_seq ℂ abs) : lim (cau_seq_conj f) = conj (lim f) := complex.ext (by simp [cau_seq_conj, (lim_re _).symm, cau_seq_re]) (by simp [cau_seq_conj, (lim_im _).symm, cau_seq_im, (lim_neg _).symm]; refl) noncomputable def cau_seq_abs (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_abs f.2⟩ lemma lim_abs (f : cau_seq ℂ abs) : lim (cau_seq_abs f) = abs (lim f) := lim_eq_of_equiv_const (λ ε ε0, let ⟨i, hi⟩ := equiv_lim f ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩) end complex
f095ca9a6d89140bdcc4aa8029cb98df11fa2b71
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/opt1.lean
9d2a669aaaab5a41831986cb5251486424594cb0
[ "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
430
lean
#eval options.get_string options.mk `opt1 "<empty>" #eval options.get_string (options.set_string options.mk `opt1 "val1") `opt1 "<empty>" #eval if (options.mk = options.mk) then bool.tt else bool.ff #eval if (options.mk = (options.set_string options.mk `opt1 "val1")) then bool.tt else bool.ff #eval options.get_nat (options.set_nat options.mk `opt1 10) `opt1 0 #eval options.get_nat (options.set_nat options.mk `opt1 10) `opt2 0
63c589a8d55ec3e5e6fb6d630386958515d83aa2
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/tests/compiler/lazylist.lean
2a4bcfcb52c210083958a16bbb147cd8e87c99a1
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,907
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ universes u v w inductive LazyList (α : Type u) | nil : LazyList α | cons (hd : α) (tl : LazyList α) : LazyList α | delayed (t : Thunk $ LazyList α) : LazyList α @[extern c inline "#2"] def List.toLazy {α : Type u} : List α → LazyList α | [] => LazyList.nil | h::t => LazyList.cons h (toLazy t) namespace LazyList variables {α : Type u} {β : Type v} {δ : Type w} instance : Inhabited (LazyList α) := ⟨nil⟩ @[inline] def pure : α → LazyList α | a => cons a nil partial def isEmpty : LazyList α → Bool | nil => true | cons _ _ => false | delayed as => isEmpty as.get partial def toList : LazyList α → List α | nil => [] | cons a as => a :: toList as | delayed as => toList as.get partial def head [Inhabited α] : LazyList α → α | nil => arbitrary α | cons a as => a | delayed as => head as.get partial def tail : LazyList α → LazyList α | nil => nil | cons a as => as | delayed as => tail as.get partial def append : LazyList α → LazyList α → LazyList α | nil, bs => bs | cons a as, bs => delayed (cons a (append as bs)) | delayed as, bs => delayed (append as.get bs) instance : Append (LazyList α) := ⟨LazyList.append⟩ partial def interleave : LazyList α → LazyList α → LazyList α | nil, bs => bs | cons a as, bs => delayed (cons a (interleave bs as)) | delayed as, bs => delayed (interleave as.get bs) partial def map (f : α → β) : LazyList α → LazyList β | nil => nil | cons a as => delayed (cons (f a) (map f as)) | delayed as => delayed (map f as.get) partial def map₂ (f : α → β → δ) : LazyList α → LazyList β → LazyList δ | nil, _ => nil | _, nil => nil | cons a as, cons b bs => delayed (cons (f a b) (map₂ f as bs)) | delayed as, bs => delayed (map₂ f as.get bs) | as, delayed bs => delayed (map₂ f as bs.get) @[inline] def zip : LazyList α → LazyList β → LazyList (α × β) := map₂ Prod.mk partial def join : LazyList (LazyList α) → LazyList α | nil => nil | cons a as => delayed (append a (join as)) | delayed as => delayed (join as.get) @[inline] partial def bind (x : LazyList α) (f : α → LazyList β) : LazyList β := join (x.map f) instance isMonad : Monad LazyList := { pure := @LazyList.pure, bind := @LazyList.bind, map := @LazyList.map } instance : Alternative LazyList := { LazyList.isMonad with failure := nil, orElse := LazyList.append } partial def approx : Nat → LazyList α → List α | 0, as => [] | _, nil => [] | i+1, cons a as => a :: approx i as | i+1, delayed as => approx (i+1) as.get partial def iterate (f : α → α) : α → LazyList α | x => cons x (delayed (iterate f (f x))) partial def iterate₂ (f : α → α → α) : α → α → LazyList α | x, y => cons x (delayed (iterate₂ f y (f x y))) partial def filter (p : α → Bool) : LazyList α → LazyList α | nil => nil | cons a as => delayed (if p a then cons a (filter p as) else filter p as) | delayed as => delayed (filter p as.get) end LazyList def fib : LazyList Nat := LazyList.iterate₂ (· + ·) 0 1 def iota (i : Nat := 0) : LazyList Nat := LazyList.iterate Nat.succ i def tst : LazyList String := do let x ← [1, 2, 3].toLazy; let y ← [2, 3, 4].toLazy; guard (x + y > 5); pure s!"{x} + {y} = {x+y}" def main : IO Unit := do let n := 40; IO.println tst.isEmpty; IO.println tst.head; IO.println $ fib.interleave (iota.map (· + 100)) $.approx n; IO.println $ iota.map (· + 10) $.filter (· % 2 == 0) $.approx n
9d3980a466cd23f97a01bdb81c93e7d2900fcefd
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/wf.lean
f2fa9de7c2714b207e432c7df1d3e133385b6f32
[ "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
7,104
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat.basic init.data.prod universes u v /-- A value `x : α` is accessible from `r` when every value that's lesser under `r` is also accessible. Note that any value that's minimal under `r` is vacuously accessible. Equivalently, `acc r x` when there is no infinite chain of elements starting at `x` that are related under `r`. This is used to state the definition of well-foundedness (see `well_founded`). -/ inductive acc {α : Sort u} (r : α → α → Prop) : α → Prop | intro (x : α) (h : ∀ y, r y x → acc y) : acc x namespace acc variables {α : Sort u} {r : α → α → Prop} lemma inv {x y : α} (h₁ : acc r x) (h₂ : r y x) : acc r y := acc.rec_on h₁ (λ x₁ ac₁ ih h₂, ac₁ y h₂) h₂ end acc /-- A relation `r : α → α → Prop` is well-founded when `∀ x, (∀ y, r y x → P y → P x) → P x` for all predicates `P`. Equivalently, `acc r x` for all `x`. Once you know that a relation is well_founded, you can use it to define fixpoint functions on `α`.-/ structure well_founded {α : Sort u} (r : α → α → Prop) : Prop := intro :: (apply : ∀ a, acc r a) class has_well_founded (α : Sort u) : Type u := (r : α → α → Prop) (wf : well_founded r) namespace well_founded section parameters {α : Sort u} {r : α → α → Prop} local infix `≺`:50 := r parameter hwf : well_founded r def recursion {C : α → Sort v} (a : α) (h : Π x, (Π y, y ≺ x → C y) → C x) : C a := acc.rec_on (apply hwf a) (λ x₁ ac₁ ih, h x₁ ih) lemma induction {C : α → Prop} (a : α) (h : ∀ x, (∀ y, y ≺ x → C y) → C x) : C a := recursion a h variable {C : α → Sort v} variable F : Π x, (Π y, y ≺ x → C y) → C x def fix_F (x : α) (a : acc r x) : C x := acc.rec_on a (λ x₁ ac₁ ih, F x₁ ih) lemma fix_F_eq (x : α) (acx : acc r x) : fix_F F x acx = F x (λ (y : α) (p : y ≺ x), fix_F F y (acc.inv acx p)) := acc.drec (λ x r ih, rfl) acx end variables {α : Sort u} {C : α → Sort v} {r : α → α → Prop} /-- Well-founded fixpoint -/ def fix (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : C x := fix_F F x (apply hwf x) /-- Well-founded fixpoint satisfies fixpoint equation -/ lemma fix_eq (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : fix hwf F x = F x (λ y h, fix hwf F y) := fix_F_eq F x (apply hwf x) end well_founded open well_founded /-- Empty relation is well-founded -/ lemma empty_wf {α : Sort u} : well_founded (@empty_relation α) := well_founded.intro (λ (a : α), acc.intro a (λ (b : α) (lt : false), false.rec _ lt)) /-! Subrelation of a well-founded relation is well-founded -/ namespace subrelation section parameters {α : Sort u} {r Q : α → α → Prop} parameters (h₁ : subrelation Q r) parameters (h₂ : well_founded r) lemma accessible {a : α} (ac : acc r a) : acc Q a := acc.rec_on ac (λ x ax ih, acc.intro x (λ (y : α) (lt : Q y x), ih y (h₁ lt))) lemma wf : well_founded Q := ⟨λ a, accessible (apply h₂ a)⟩ end end subrelation -- The inverse image of a well-founded relation is well-founded namespace inv_image section parameters {α : Sort u} {β : Sort v} {r : β → β → Prop} parameters (f : α → β) parameters (h : well_founded r) private def acc_aux {b : β} (ac : acc r b) : ∀ (x : α), f x = b → acc (inv_image r f) x := acc.rec_on ac (λ x acx ih z e, acc.intro z (λ y lt, eq.rec_on e (λ acx ih, ih (f y) lt y rfl) acx ih)) lemma accessible {a : α} (ac : acc r (f a)) : acc (inv_image r f) a := acc_aux ac a rfl lemma wf : well_founded (inv_image r f) := ⟨λ a, accessible (apply h (f a))⟩ end end inv_image /-- less-than is well-founded -/ lemma nat.lt_wf : well_founded nat.lt := ⟨nat.rec (acc.intro 0 (λ n h, absurd h (nat.not_lt_zero n))) (λ n ih, acc.intro (nat.succ n) (λ m h, or.elim (nat.eq_or_lt_of_le (nat.le_of_succ_le_succ h)) (λ e, eq.substr e ih) (acc.inv ih)))⟩ def measure {α : Sort u} : (α → ℕ) → α → α → Prop := inv_image (<) lemma measure_wf {α : Sort u} (f : α → ℕ) : well_founded (measure f) := inv_image.wf f nat.lt_wf def sizeof_measure (α : Sort u) [has_sizeof α] : α → α → Prop := measure sizeof lemma sizeof_measure_wf (α : Sort u) [has_sizeof α] : well_founded (sizeof_measure α) := measure_wf sizeof instance has_well_founded_of_has_sizeof (α : Sort u) [has_sizeof α] : has_well_founded α := {r := sizeof_measure α, wf := sizeof_measure_wf α} namespace prod open well_founded section variables {α : Type u} {β : Type v} variable (ra : α → α → Prop) variable (rb : β → β → Prop) -- Lexicographical order based on ra and rb inductive lex : α × β → α × β → Prop | left {a₁} (b₁) {a₂} (b₂) (h : ra a₁ a₂) : lex (a₁, b₁) (a₂, b₂) | right (a) {b₁ b₂} (h : rb b₁ b₂) : lex (a, b₁) (a, b₂) -- relational product based on ra and rb inductive rprod : α × β → α × β → Prop | intro {a₁ b₁ a₂ b₂} (h₁ : ra a₁ a₂) (h₂ : rb b₁ b₂) : rprod (a₁, b₁) (a₂, b₂) end section parameters {α : Type u} {β : Type v} parameters {ra : α → α → Prop} {rb : β → β → Prop} local infix `≺`:50 := lex ra rb lemma lex_accessible {a} (aca : acc ra a) (acb : ∀ b, acc rb b): ∀ b, acc (lex ra rb) (a, b) := acc.rec_on aca (λ xa aca iha b, acc.rec_on (acb b) (λ xb acb ihb, acc.intro (xa, xb) (λ p lt, have aux : xa = xa → xb = xb → acc (lex ra rb) p, from @prod.lex.rec_on α β ra rb (λ p₁ p₂, fst p₂ = xa → snd p₂ = xb → acc (lex ra rb) p₁) p (xa, xb) lt (λ a₁ b₁ a₂ b₂ h (eq₂ : a₂ = xa) (eq₃ : b₂ = xb), iha a₁ (eq.rec_on eq₂ h) b₁) (λ a b₁ b₂ h (eq₂ : a = xa) (eq₃ : b₂ = xb), eq.rec_on eq₂.symm (ihb b₁ (eq.rec_on eq₃ h))), aux rfl rfl))) -- The lexicographical order of well founded relations is well-founded lemma lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := ⟨λ p, cases_on p (λ a b, lex_accessible (apply ha a) (well_founded.apply hb) b)⟩ -- relational product is a subrelation of the lex lemma rprod_sub_lex : ∀ a b, rprod ra rb a b → lex ra rb a b := λ a b h, prod.rprod.rec_on h (λ a₁ b₁ a₂ b₂ h₁ h₂, lex.left b₁ b₂ h₁) -- The relational product of well founded relations is well-founded lemma rprod_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (rprod ra rb) := subrelation.wf (rprod_sub_lex) (lex_wf ha hb) end instance has_well_founded {α : Type u} {β : Type v} [s₁ : has_well_founded α] [s₂ : has_well_founded β] : has_well_founded (α × β) := {r := lex s₁.r s₂.r, wf := lex_wf s₁.wf s₂.wf} end prod
ee17c6de19b8ce061f3d4055b3b2cd383df4b670
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/expr.lean
092fb8571bb5eea60d0fb5e524a956c50efde11d
[ "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
10,470
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.level structure pos := (line : nat) (column : nat) instance : decidable_eq pos | ⟨l₁, c₁⟩ ⟨l₂, c₂⟩ := if h₁ : l₁ = l₂ then if h₂ : c₁ = c₂ then is_true (eq.rec_on h₁ (eq.rec_on h₂ rfl)) else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₂ h₂)) else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₁ h₁)) inductive binder_info | default | implicit | strict_implicit | inst_implicit | other instance : has_to_string binder_info := ⟨λ bi, match bi with | binder_info.default := "default" | binder_info.implicit := "implicit" | binder_info.strict_implicit := "strict_implicit" | binder_info.inst_implicit := "inst_implicit" | binder_info.other := "other" end⟩ meta constant macro_def : Type /- Reflect a C++ expr object. The VM replaces it with the C++ implementation. -/ meta inductive expr | var : nat → expr | sort : level → expr | const : name → list level → expr | mvar : name → expr → expr | local_const : name → name → binder_info → expr → expr | app : expr → expr → expr | lam : name → binder_info → expr → expr → expr | pi : name → binder_info → expr → expr → expr | elet : name → expr → expr → expr → expr | macro : macro_def → ∀ n, (fin n → expr) → expr meta instance : inhabited expr := ⟨expr.sort level.zero⟩ meta constant expr.mk_macro (d : macro_def) : list expr → expr meta constant expr.macro_def_name (d : macro_def) : name meta def expr.mk_var (n : nat) : expr := expr.var n /- Choice macros are used to implement overloading. -/ meta constant expr.is_choice_macro : expr → bool -- Compares expressions, including binder names. meta constant expr.has_decidable_eq : decidable_eq expr attribute [instance] expr.has_decidable_eq -- Compares expressions while ignoring binder names. meta constant expr.alpha_eqv : expr → expr → bool notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt protected meta constant expr.to_string : expr → string meta instance : has_to_string expr := has_to_string.mk expr.to_string /- Coercion for letting users write (f a) instead of (expr.app f a) -/ meta instance : has_coe_to_fun expr := { F := λ e, expr → expr, coe := λ e, expr.app e } meta constant expr.hash : expr → nat -- Compares expressions, ignoring binder names, and sorting by hash. meta constant expr.lt : expr → expr → bool -- Compares expressions, ignoring binder names. meta constant expr.lex_lt : expr → expr → bool -- Compares expressions, ignoring binder names, and sorting by hash. meta def expr.cmp (a b : expr) : ordering := if expr.lt a b then ordering.lt else if a =ₐ b then ordering.eq else ordering.gt meta constant expr.fold {α : Type} : expr → α → (expr → nat → α → α) → α meta constant expr.replace : expr → (expr → nat → option expr) → expr meta constant expr.abstract_local : expr → name → expr meta constant expr.abstract_locals : expr → list name → expr meta def expr.abstract : expr → expr → expr | e (expr.local_const n m bi t) := e^.abstract_local n | e _ := e meta constant expr.instantiate_univ_params : expr → list (name × level) → expr meta constant expr.instantiate_var : expr → expr → expr meta constant expr.instantiate_vars : expr → list expr → expr meta constant expr.has_var : expr → bool meta constant expr.has_var_idx : expr → nat → bool meta constant expr.has_local : expr → bool meta constant expr.has_meta_var : expr → bool meta constant expr.lift_vars : expr → nat → nat → expr meta constant expr.lower_vars : expr → nat → nat → expr /- (copy_pos_info src tgt) copy position information from src to tgt. -/ meta constant expr.copy_pos_info : expr → expr → expr meta constant expr.is_internal_cnstr : expr → option unsigned meta constant expr.get_nat_value : expr → option nat meta constant expr.collect_univ_params : expr → list name /-- `occurs e t` returns `tt` iff `e` occurs in `t` -/ meta constant expr.occurs : expr → expr → bool namespace expr open decidable -- Compares expressions, ignoring binder names, and sorting by hash. meta instance : has_ordering expr := ⟨ expr.cmp ⟩ meta def mk_true : expr := const `true [] meta def mk_false : expr := const `false [] /-- Returns the sorry macro with the given type. -/ meta constant mk_sorry (type : expr) : expr /-- Checks whether e is sorry, and returns its type. -/ meta constant is_sorry (e : expr) : option expr meta def is_var : expr → bool | (var _) := tt | _ := ff meta def app_of_list : expr → list expr → expr | f [] := f | f (p::ps) := app_of_list (f p) ps meta def is_app : expr → bool | (app f a) := tt | e := ff meta def app_fn : expr → expr | (app f a) := f | a := a meta def app_arg : expr → expr | (app f a) := a | a := a meta def get_app_fn : expr → expr | (app f a) := get_app_fn f | a := a meta def get_app_num_args : expr → nat | (app f a) := get_app_num_args f + 1 | e := 0 meta def get_app_args_aux : list expr → expr → list expr | r (app f a) := get_app_args_aux (a::r) f | r e := r meta def get_app_args : expr → list expr := get_app_args_aux [] meta def mk_app : expr → list expr → expr | e [] := e | e (x::xs) := mk_app (e x) xs meta def ith_arg_aux : expr → nat → expr | (app f a) 0 := a | (app f a) (n+1) := ith_arg_aux f n | e _ := e meta def ith_arg (e : expr) (i : nat) : expr := ith_arg_aux e (get_app_num_args e - i - 1) meta def const_name : expr → name | (const n ls) := n | e := name.anonymous meta def is_constant : expr → bool | (const n ls) := tt | e := ff meta def is_local_constant : expr → bool | (local_const n m bi t) := tt | e := ff meta def local_uniq_name : expr → name | (local_const n m bi t) := n | e := name.anonymous meta def local_pp_name : expr → name | (local_const x n bi t) := n | e := name.anonymous meta def local_type : expr → expr | (local_const _ _ _ t) := t | e := e meta def is_constant_of : expr → name → bool | (const n₁ ls) n₂ := n₁ = n₂ | e n := ff meta def is_app_of (e : expr) (n : name) : bool := is_constant_of (get_app_fn e) n meta def is_napp_of (e : expr) (c : name) (n : nat) : bool := is_app_of e c ∧ get_app_num_args e = n meta def is_false : expr → bool | ``(false) := tt | _ := ff meta def is_not : expr → option expr | ``(not %%a) := some a | ``(%%a → false) := some a | e := none meta def is_eq : expr → option (expr × expr) | ``((%%a: %%α) = %%b) := some (a, b) | _ := none meta def is_ne : expr → option (expr × expr) | ``((%%a: %%α) ≠ %%b) := some (a, b) | _ := none meta def is_bin_arith_app (e : expr) (op : name) : option (expr × expr) := if is_napp_of e op 4 then some (app_arg (app_fn e), app_arg e) else none meta def is_lt (e : expr) : option (expr × expr) := is_bin_arith_app e `lt meta def is_gt (e : expr) : option (expr × expr) := is_bin_arith_app e `gt meta def is_le (e : expr) : option (expr × expr) := is_bin_arith_app e `le meta def is_ge (e : expr) : option (expr × expr) := is_bin_arith_app e `ge meta def is_heq : expr → option (expr × expr × expr × expr) | ``(@heq %%α %%a %%β %%b) := some (α, a, β, b) | _ := none meta def is_pi : expr → bool | (pi _ _ _ _) := tt | e := ff meta def is_arrow : expr → bool | (pi _ _ _ b) := bnot (has_var b) | e := ff meta def is_let : expr → bool | (elet _ _ _ _) := tt | e := ff meta def binding_name : expr → name | (pi n _ _ _) := n | (lam n _ _ _) := n | e := name.anonymous meta def binding_info : expr → binder_info | (pi _ bi _ _) := bi | (lam _ bi _ _) := bi | e := binder_info.default meta def binding_domain : expr → expr | (pi _ _ d _) := d | (lam _ _ d _) := d | e := e meta def binding_body : expr → expr | (pi _ _ _ b) := b | (lam _ _ _ b) := b | e := e meta def prop : expr := expr.sort level.zero meta def imp (a b : expr) : expr := pi `a binder_info.default a b meta def and_ (a b : expr) : expr := app (app (const ``and []) a) b meta def not_ (a : expr) : expr := app (const ``not []) a meta def false_ : expr := const ``false [] meta def lambdas : list expr → expr → expr | (local_const uniq pp info t :: es) f := lam pp info t (abstract_local (lambdas es f) uniq) | _ f := f meta def pis : list expr → expr → expr | (local_const uniq pp info t :: es) f := pi pp info t (abstract_local (pis es f) uniq) | _ f := f open format private meta def p := λ xs, paren (format.join (list.intersperse " " xs)) private meta def macro_args_to_list_aux (n : nat) (args : fin n → expr) : Π (i : nat), i ≤ n → list expr | 0 _ := [] | (i+1) h := args ⟨i, h⟩ :: macro_args_to_list_aux i (nat.le_trans (nat.le_succ _) h) meta def macro_args_to_list (n : nat) (args : fin n → expr) : list expr := macro_args_to_list_aux n args n (nat.le_refl _) meta def to_raw_fmt : expr → format | (var n) := p ["var", to_fmt n] | (sort l) := p ["sort", to_fmt l] | (const n ls) := p ["const", to_fmt n, to_fmt ls] | (mvar n t) := p ["mvar", to_fmt n, to_raw_fmt t] | (local_const n m bi t) := p ["local_const", to_fmt n, to_fmt m, to_raw_fmt t] | (app e f) := p ["app", to_raw_fmt e, to_raw_fmt f] | (lam n bi e t) := p ["lam", to_fmt n, to_string bi, to_raw_fmt e, to_raw_fmt t] | (pi n bi e t) := p ["pi", to_fmt n, to_string bi, to_raw_fmt e, to_raw_fmt t] | (elet n g e f) := p ["elet", to_fmt n, to_raw_fmt g, to_raw_fmt e, to_raw_fmt f] | (macro d n args) := sbracket (format.join (list.intersperse " " ("macro" :: to_fmt (macro_def_name d) :: list.map to_raw_fmt (macro_args_to_list n args)))) meta def mfold {α : Type} {m : Type → Type} [monad m] (e : expr) (a : α) (fn : expr → nat → α → m α) : m α := fold e (return a) (λ e n a, a >>= fn e n) end expr
5a27256fd771abd0e8381865fda7d2fb2574782d
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/analysis/hofer.lean
7f65002d996dc3d76ab3b4afdad643146ff37fba
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,575
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import analysis.specific_limits /-! # Hofer's lemma This is an elementary lemma about complete metric spaces. It is motivated by an application to the bubbling-off analysis for holomorphic curves in symplectic topology. We are *very* far away from having these applications, but the proof here is a nice example of a proof needing to construct a sequence by induction in the middle of the proof. ## References: * H. Hofer and C. Viterbo, *The Weinstein conjecture in the presence of holomorphic spheres* -/ open_locale classical topological_space big_operators open filter finset local notation `d` := dist lemma hofer {X: Type*} [metric_space X] [complete_space X] (x : X) (ε : ℝ) (ε_pos : 0 < ε) {ϕ : X → ℝ} (cont : continuous ϕ) (nonneg : ∀ y, 0 ≤ ϕ y) : ∃ (ε' > 0) (x' : X), ε' ≤ ε ∧ d x' x ≤ 2*ε ∧ ε * ϕ(x) ≤ ε' * ϕ x' ∧ ∀ y, d x' y ≤ ε' → ϕ y ≤ 2*ϕ x' := begin by_contradiction H, have reformulation : ∀ x' (k : ℕ), ε * ϕ x ≤ ε / 2 ^ k * ϕ x' ↔ 2^k * ϕ x ≤ ϕ x', { intros x' k, rw [div_mul_eq_mul_div, le_div_iff, mul_assoc, mul_le_mul_left ε_pos, mul_comm], exact pow_pos (by norm_num) k, }, -- Now let's specialize to `ε/2^k` replace H : ∀ k : ℕ, ∀ x', d x' x ≤ 2 * ε ∧ 2^k * ϕ x ≤ ϕ x' → ∃ y, d x' y ≤ ε/2^k ∧ 2 * ϕ x' < ϕ y, { intros k x', push_neg at H, simpa [reformulation] using H (ε/2^k) (by simp [ε_pos, zero_lt_two]) x' (by simp [ε_pos, zero_lt_two, one_le_two]) }, clear reformulation, haveI : nonempty X := ⟨x⟩, choose! F hF using H, -- Use the axiom of choice -- Now define u by induction starting at x, with u_{n+1} = F(n, u_n) let u : ℕ → X := λ n, nat.rec_on n x F, have hu0 : u 0 = x := rfl, -- The properties of F translate to properties of u have hu : ∀ n, d (u n) x ≤ 2 * ε ∧ 2^n * ϕ x ≤ ϕ (u n) → d (u n) (u $ n + 1) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u $ n + 1), { intro n, exact hF n (u n) }, clear hF, -- Key properties of u, to be proven by induction have key : ∀ n, d (u n) (u (n + 1)) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u (n + 1)), { intro n, induction n using nat.case_strong_induction_on with n IH, { specialize hu 0, simpa [hu0, mul_nonneg_iff, zero_le_one, ε_pos.le] using hu }, have A : d (u (n+1)) x ≤ 2 * ε, { rw [dist_comm], let r := range (n+1), -- range (n+1) = {0, ..., n} calc d (u 0) (u (n + 1)) ≤ ∑ i in r, d (u i) (u $ i+1) : dist_le_range_sum_dist u (n + 1) ... ≤ ∑ i in r, ε/2^i : sum_le_sum (λ i i_in, (IH i $ nat.lt_succ_iff.mp $ finset.mem_range.mp i_in).1) ... = ∑ i in r, (1/2)^i*ε : by { congr' with i, field_simp } ... = (∑ i in r, (1/2)^i)*ε : finset.sum_mul.symm ... ≤ 2*ε : mul_le_mul_of_nonneg_right (sum_geometric_two_le _) (le_of_lt ε_pos), }, have B : 2^(n+1) * ϕ x ≤ ϕ (u (n + 1)), { apply le_of_lt, exact geom_lt (by norm_num) (λ m hm, (IH _ hm).2), }, exact hu (n+1) ⟨A, B⟩, }, cases forall_and_distrib.mp key with key₁ key₂, clear hu key, -- Hence u is Cauchy have cauchy_u : cauchy_seq u, { apply cauchy_seq_of_le_geometric _ ε (by norm_num : 1/(2:ℝ) < 1), intro n, convert key₁ n, rw [one_div, inv_pow'] }, -- So u converges to some y obtain ⟨y, limy⟩ : ∃ y, tendsto u at_top (𝓝 y), from complete_space.complete cauchy_u, -- And ϕ ∘ u goes to +∞ have lim_top : tendsto (ϕ ∘ u) at_top at_top, { let v := λ n, (ϕ ∘ u) (n+1), suffices : tendsto v at_top at_top, by rwa tendsto_add_at_top_iff_nat at this, have hv₀ : 0 < v 0, { have : 0 ≤ ϕ (u 0) := nonneg x, calc 0 ≤ 2 * ϕ (u 0) : by linarith ... < ϕ (u (0 + 1)) : key₂ 0 }, apply tendsto_at_top_of_geom_lt hv₀ (by norm_num : (1 : ℝ) < 2), exact λ n, key₂ (n+1) }, -- But ϕ ∘ u also needs to go to ϕ(y) have lim : tendsto (ϕ ∘ u) at_top (𝓝 (ϕ y)), from tendsto.comp cont.continuous_at limy, -- So we have our contradiction! exact not_tendsto_at_top_of_tendsto_nhds lim lim_top, end
4422f9466f5d7111c0b0144576fc0d2d110e6b9a
f3849be5d845a1cb97680f0bbbe03b85518312f0
/tests/lean/run/my_tac_class.lean
5deecd3b798fb38244714ec1acf6c0ce152637a6
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,753
lean
meta def mytac := state_t nat tactic meta instance : monad mytac := state_t.monad _ _ meta instance : monad.has_monad_lift tactic mytac := monad.monad_transformer_lift (state_t nat) tactic meta instance (α : Type) : has_coe (tactic α) (mytac α) := ⟨monad.monad_lift⟩ namespace mytac meta def step {α : Type} (t : mytac α) : mytac unit := t >> return () meta def istep {α : Type} (line : nat) (col : nat) (t : mytac α) : mytac unit := λ v s, result.cases_on (@scope_trace _ line col (t v s)) (λ ⟨a, v⟩ new_s, result.success ((), v) new_s) (λ opt_msg_thunk e new_s, match opt_msg_thunk with | some msg_thunk := let msg := λ _ : unit, msg_thunk () ++ format.line ++ to_fmt "value: " ++ to_fmt v ++ format.line ++ to_fmt "state:" ++ format.line ++ new_s^.to_format in interaction_monad.result.exception (some msg) (some ⟨line, col⟩) new_s | none := interaction_monad.silent_fail new_s end) meta def execute (tac : mytac unit) : tactic unit := tac 0 >> return () meta def save_info (p : pos) : mytac unit := do v ← state_t.read, s ← tactic.read, tactic.save_info_thunk p (λ _, to_fmt "Custom state: " ++ to_fmt v ++ format.line ++ tactic_state.to_format s) namespace interactive meta def intros : mytac unit := tactic.intros >> return () meta def constructor : mytac unit := tactic.constructor meta def trace (s : string) : mytac unit := tactic.trace s meta def assumption : mytac unit := tactic.assumption meta def inc : mytac unit := do v ← state_t.read, state_t.write (v+1) end interactive end mytac example (p q : Prop) : p → q → p ∧ q := begin [mytac] intros, inc, trace "test", constructor, inc, assumption, assumption end
855fb5980c75ec24f4e8c456cf8abd0e4b95fe68
64874bd1010548c7f5a6e3e8902efa63baaff785
/hott/types/pi.hlean
67a8840daa681391fd8f53fd1a9830aeb0ef8958
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,665
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about pi-types (dependent function spaces) -/ import types.sigma open eq equiv is_equiv funext namespace pi universe variables l k variables {A A' : Type.{l}} {B : A → Type.{k}} {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''} {f g : Πa, B a} /- Paths -/ /- Paths [p : f ≈ g] in a function type [Πx:X, P x] are equivalent to functions taking values in path types, [H : Πx:X, f x ≈ g x], or concisely, [H : f ∼ g]. This equivalence, however, is just the combination of [apD10] and function extensionality [funext], and as such, [path_forall], et seq. are given in axioms.funext and path: -/ /- Now we show how these things compute. -/ definition apD10_path_pi [H : funext] (h : f ∼ g) : apD10 (path_pi h) ∼ h := apD10 (retr apD10 h) definition path_pi_eta [H : funext] (p : f = g) : path_pi (apD10 p) = p := sect apD10 p definition path_pi_idp [H : funext] : path_pi (λx : A, refl (f x)) = refl f := !path_pi_eta /- The identification of the path space of a dependent function space, up to equivalence, is of course just funext. -/ definition path_equiv_homotopy [H : funext] (f g : Πx, B x) : (f = g) ≃ (f ∼ g) := equiv.mk _ !funext.ap definition is_equiv_path_pi [instance] [H : funext] (f g : Πx, B x) : is_equiv (@path_pi _ _ _ f g) := inv_closed apD10 definition homotopy_equiv_path [H : funext] (f g : Πx, B x) : (f ∼ g) ≃ (f = g) := equiv.mk _ !is_equiv_path_pi /- Transport -/ protected definition transport (p : a = a') (f : Π(b : B a), C a b) : (transport (λa, Π(b : B a), C a b) p f) ∼ (λb, transport (C a') !transport_pV (transportD _ _ p _ (f (p⁻¹ ▹ b)))) := eq.rec_on p (λx, idp) /- A special case of [transport_pi] where the type [B] does not depend on [A], and so it is just a fixed type [B]. -/ definition transport_constant {C : A → A' → Type} (p : a = a') (f : Π(b : A'), C a b) : (eq.transport (λa, Π(b : A'), C a b) p f) ∼ (λb, eq.transport (λa, C a b) p (f b)) := eq.rec_on p (λx, idp) /- Maps on paths -/ /- The action of maps given by lambda. -/ definition ap_lambdaD [H : funext] {C : A' → Type} (p : a = a') (f : Πa b, C b) : ap (λa b, f a b) p = path_pi (λb, ap (λa, f a b) p) := begin apply (eq.rec_on p), apply inverse, apply path_pi_idp end /- Dependent paths -/ /- with more implicit arguments the conclusion of the following theorem is (Π(b : B a), transportD B C p b (f b) = g (eq.transport B p b)) ≃ (eq.transport (λa, Π(b : B a), C a b) p f = g) -/ definition dpath_pi [H : funext] (p : a = a') (f : Π(b : B a), C a b) (g : Π(b' : B a'), C a' b') : (Π(b : B a), p ▹D (f b) = g (p ▹ b)) ≃ (p ▹ f = g) := eq.rec_on p (λg, !homotopy_equiv_path) g section open sigma sigma.ops /- more implicit arguments: (Π(b : B a), eq.transport C (sigma.path p idp) (f b) = g (p ▹ b)) ≃ (Π(b : B a), transportD B (λ(a : A) (b : B a), C ⟨a, b⟩) p b (f b) = g (eq.transport B p b)) -/ definition dpath_pi_sigma {C : (Σa, B a) → Type} (p : a = a') (f : Π(b : B a), C ⟨a, b⟩) (g : Π(b' : B a'), C ⟨a', b'⟩) : (Π(b : B a), (sigma.path p idp) ▹ (f b) = g (p ▹ b)) ≃ (Π(b : B a), p ▹D (f b) = g (p ▹ b)) := eq.rec_on p (λg, !equiv.refl) g end /- truncation -/ open truncation definition trunc_pi [instance] [H : funext.{l k}] (B : A → Type.{k}) (n : trunc_index) [H : ∀a, is_trunc n (B a)] : is_trunc n (Πa, B a) := begin reverts (B, H), apply (trunc_index.rec_on n), intros (B, H), fapply is_contr.mk, intro a, apply center, apply H, --remove "apply H" when term synthesis works correctly intro f, apply path_pi, intro x, apply (contr (f x)), intros (n, IH, B, H), fapply is_trunc_succ, intros (f, g), fapply trunc_equiv', apply equiv.symm, apply path_equiv_homotopy, apply IH, intro a, show is_trunc n (f a = g a), from succ_is_trunc n (f a) (g a) end definition trunc_path_pi [instance] [H : funext.{l k}] (n : trunc_index) (f g : Πa, B a) [H : ∀a, is_trunc n (f a = g a)] : is_trunc n (f = g) := begin apply trunc_equiv', apply equiv.symm, apply path_equiv_homotopy, apply trunc_pi, exact H, end end pi
c34a3966f59d0c94bb5a17cb2aa214a7b99832ff
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/archive/100-theorems-list/9_area_of_a_circle.lean
bf1453fcab078fa99192201b142ec0e0b09af5d3
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,451
lean
/- Copyright (c) 2021 James Arthur, Benjamin Davidson, Andrew Souther. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: James Arthur, Benjamin Davidson, Andrew Souther -/ import measure_theory.integral.interval_integral import analysis.special_functions.sqrt /-! # Freek № 9: The Area of a Circle In this file we show that the area of a disc with nonnegative radius `r` is `π * r^2`. The main tools our proof uses are `volume_region_between_eq_integral`, which allows us to represent the area of the disc as an integral, and `interval_integral.integral_eq_sub_of_has_deriv_at'_of_le`, the second fundamental theorem of calculus. We begin by defining `disc` in `ℝ × ℝ`, then show that `disc` can be represented as the `region_between` two functions. Though not necessary for the main proof, we nonetheless choose to include a proof of the measurability of the disc in order to convince the reader that the set whose volume we will be calculating is indeed measurable and our result is therefore meaningful. In the main proof, `area_disc`, we use `volume_region_between_eq_integral` followed by `interval_integral.integral_of_le` to reduce our goal to a single `interval_integral`: `∫ (x : ℝ) in -r..r, 2 * sqrt (r ^ 2 - x ^ 2) = π * r ^ 2`. After disposing of the trivial case `r = 0`, we show that `λ x, 2 * sqrt (r ^ 2 - x ^ 2)` is equal to the derivative of `λ x, r ^ 2 * arcsin (x / r) + x * sqrt (r ^ 2 - x ^ 2)` everywhere on `Ioo (-r) r` and that those two functions are continuous, then apply the second fundamental theorem of calculus with those facts. Some simple algebra then completes the proof. Note that we choose to define `disc` as a set of points in `ℝ ⨯ ℝ`. This is admittedly not ideal; it would be more natural to define `disc` as a `metric.ball` in `euclidean_space ℝ (fin 2)` (as well as to provide a more general proof in higher dimensions). However, our proof indirectly relies on a number of theorems (particularly `measure_theory.measure.prod_apply`) which do not yet exist for Euclidean space, thus forcing us to use this less-preferable definition. As `measure_theory.pi` continues to develop, it should eventually become possible to redefine `disc` and extend our proof to the n-ball. -/ open set real measure_theory interval_integral open_locale real nnreal /-- A disc of radius `r` is defined as the collection of points `(p.1, p.2)` in `ℝ × ℝ` such that `p.1 ^ 2 + p.2 ^ 2 < r ^ 2`. Note that this definition is not equivalent to `metric.ball (0 : ℝ × ℝ) r`. This was done intentionally because `dist` in `ℝ × ℝ` is defined as the uniform norm, making the `metric.ball` in `ℝ × ℝ` a square, not a disc. See the module docstring for an explanation of why we don't define the disc in Euclidean space. -/ def disc (r : ℝ) := {p : ℝ × ℝ | p.1 ^ 2 + p.2 ^ 2 < r ^ 2} variable (r : ℝ≥0) /-- A disc of radius `r` can be represented as the region between the two curves `λ x, - sqrt (r ^ 2 - x ^ 2)` and `λ x, sqrt (r ^ 2 - x ^ 2)`. -/ lemma disc_eq_region_between : disc r = region_between (λ x, -sqrt (r^2 - x^2)) (λ x, sqrt (r^2 - x^2)) (Ioc (-r) r) := begin ext p, simp only [disc, region_between, mem_set_of_eq, mem_Ioo, mem_Ioc, pi.neg_apply], split; intro h, { cases abs_lt_of_sq_lt_sq' (lt_of_add_lt_of_nonneg_left h (sq_nonneg p.2)) r.2, rw [add_comm, ← lt_sub_iff_add_lt] at h, exact ⟨⟨left, right.le⟩, sq_lt.mp h⟩ }, { rw [add_comm, ← lt_sub_iff_add_lt], exact sq_lt.mpr h.2 }, end /-- The disc is a `measurable_set`. -/ theorem measurable_set_disc : measurable_set (disc r) := by apply measurable_set_lt; apply continuous.measurable; continuity /-- **Area of a Circle**: The area of a disc with radius `r` is `π * r ^ 2`. -/ theorem area_disc : volume (disc r) = nnreal.pi * r ^ 2 := begin let f := λ x, sqrt (r ^ 2 - x ^ 2), let F := λ x, (r:ℝ) ^ 2 * arcsin (r⁻¹ * x) + x * sqrt (r ^ 2 - x ^ 2), have hf : continuous f := by continuity, suffices : ∫ x in -r..r, 2 * f x = nnreal.pi * r ^ 2, { have h : integrable_on f (Ioc (-r) r) := (hf.integrable_on_compact is_compact_Icc).mono_set Ioc_subset_Icc_self, calc volume (disc r) = volume (region_between (λ x, -f x) f (Ioc (-r) r)) : by rw disc_eq_region_between ... = ennreal.of_real (∫ x in Ioc (-r:ℝ) r, (f - has_neg.neg ∘ f) x) : volume_region_between_eq_integral h.neg h measurable_set_Ioc (λ x hx, neg_le_self (sqrt_nonneg _)) ... = ennreal.of_real (∫ x in (-r:ℝ)..r, 2 * f x) : by simp [two_mul, integral_of_le] ... = nnreal.pi * r ^ 2 : by rw_mod_cast [this, ← ennreal.coe_nnreal_eq], }, obtain ⟨hle, (heq | hlt)⟩ := ⟨nnreal.coe_nonneg r, hle.eq_or_lt⟩, { simp [← heq] }, have hderiv : ∀ x ∈ Ioo (-r:ℝ) r, has_deriv_at F (2 * f x) x, { rintros x ⟨hx1, hx2⟩, convert ((has_deriv_at_const x ((r:ℝ)^2)).mul ((has_deriv_at_arcsin _ _).comp x ((has_deriv_at_const x (r:ℝ)⁻¹).mul (has_deriv_at_id' x)))).add ((has_deriv_at_id' x).mul (((has_deriv_at_id' x).pow.const_sub ((r:ℝ)^2)).sqrt _)), { have h : sqrt (1 - x ^ 2 / r ^ 2) * r = sqrt (r ^ 2 - x ^ 2), { rw [← sqrt_sq hle, ← sqrt_mul, sub_mul, sqrt_sq hle, div_mul_eq_mul_div_comm, div_self (pow_ne_zero 2 hlt.ne'), one_mul, mul_one], simpa [sqrt_sq hle, div_le_one (pow_pos hlt 2)] using sq_le_sq' hx1.le hx2.le }, field_simp, rw [h, mul_left_comm, ← sq, neg_mul_eq_mul_neg, mul_div_mul_left (-x^2) _ two_ne_zero, add_left_comm, div_add_div_same, tactic.ring.add_neg_eq_sub, div_sqrt, two_mul] }, { suffices : -(1:ℝ) < r⁻¹ * x, by exact this.ne', calc -(1:ℝ) = r⁻¹ * -r : by simp [hlt.ne'] ... < r⁻¹ * x : by nlinarith [inv_pos.mpr hlt] }, { suffices : (r:ℝ)⁻¹ * x < 1, by exact this.ne, calc (r:ℝ)⁻¹ * x < r⁻¹ * r : by nlinarith [inv_pos.mpr hlt] ... = 1 : inv_mul_cancel hlt.ne' }, { nlinarith } }, have hcont := (by continuity : continuous F).continuous_on, have hcont' := (continuous_const.mul hf).continuous_on, calc ∫ x in -r..r, 2 * f x = F r - F (-r) : integral_eq_sub_of_has_deriv_at'_of_le (neg_le_self r.2) hcont hderiv hcont' ... = nnreal.pi * r ^ 2 : by norm_num [F, inv_mul_cancel hlt.ne', ← mul_div_assoc, mul_comm π], end
ad0f381bbf01bd0cb68381471531ba00325b9adf
cb1829c15cd3d28210f93507f96dfb1f56ec0128
/theorem_proving/04-quantifiers_and_equality.lean
bc181bc66f30dcf720347a01510807ec35ff387b
[]
no_license
williamdemeo/LEAN_wjd
69f9f76e35092b89e4479a320be2fa3c18aed6fe
13826c75c06ef435166a26a72e76fe984c15bad7
refs/heads/master
1,609,516,630,137
1,518,123,893,000
1,518,123,893,000
97,740,278
2
0
null
null
null
null
UTF-8
Lean
false
false
27,895
lean
-- Chapter 4 Quantifiers and Equality #print "==================================" #print "Section 4.1 The Universal Quantifier" #print " " /- Notice that if α is any type, we can represent a unary predicate p on α as an object of type α → Prop. In that case, if x : α, then p x denotes the assertion that p holds of x. Similarly, an object r : α → α → Prop denotes a binary relation on α: if x y : α, then r x y denotes the assertion that x is related to y. The universal quantifier, ∀ x : α, p x is supposed to denote the assertion that "for every x : α, p x" holds. As with the propositional connectives, in systems of natural deduction, "forall" is governed by an introduction and elimination rule. Informally, the introduction rule states: Given a proof of p x, in a context where x : α is arbitrary, we obtain a proof ∀ x : α, p x. The elimination rule states: Given a proof ∀ x : α, p x and any term t : α, we obtain a proof of p t. The propositions-as-types interpretation now comes into play. Remember the introduction and elimination rules for Pi types: Introduction Rule for Pi types: Given a term t of type β x, in a context where x : α is arbitrary, we have (λ x : α, t) : Π x : α, β x. Elimination rule for Pi types: Given a term s : Π x : α, β x and any term x : α, we have s x : β x. If p x has type Prop, and if we replace Π x : α, β x with ∀ x : α, p x, then we can read these as the correct rules for building proofs involving the universal quantifier. The Calculus of Constructions therefore identifies Π and ∀ in this way. If p is any expression, ∀ x : α, p is simply alternative notation for Π x : α, p, with the idea that the former is more natural than the latter in cases where p is a proposition. Typically, the expression p will depend on x : α. Recall that, in the case of ordinary function spaces, we could interpret α → β as the special case of Π x : α, β in which β does not depend on x. Similarly, we can think of an implication p → q between propositions as the special case of ∀ x : p, q in which the expression q does not depend on x. -/ namespace Sec_4_1 -- Example: how the propositions-as-types correspondence is used in practice. namespace example1 variables (α : Type) (p q : α → Prop) example : (∀ x : α, p x ∧ q x) → ∀ y : α, p y := assume h : ∀ x : α, p x ∧ q x, assume y : α, show p y, from (h y).left -- alternative version of the last example example : (∀ x : α, p x ∧ q x) → ∀ y : α, p y := λ (h : ∀ x : α, p x ∧ q x) (y : α), and.left (h y) end example1 -- Example: expressing the fact that a relation r is transitive. namespace example2 variables (α : Type) (r : α → α → Prop) variable trans_r : ∀ x y z : α, r x y → r y z → r x z variables a b c : α variables (h₁ : r a b) ( h₂ : r b c) #check trans_r -- ∀ (x y z : α), r x y → r y z → r x z #check trans_r a b c -- r a b → r b c → r a c #check trans_r a b c h₁ -- r b c → r a c #check trans_r a b c h₁ h₂ -- r a c end example2 -- Example: impicit parameters namespace example3 /- It can be tedious to supply the arguments a b c, when they can be inferred from hab hbc. For that reason, it is common to make these arguments implicit: -/ universe u variables (α : Type) (r : α → α → Prop) variable trans_r : ∀ {x y z : α}, r x y → r y z → r x z variables a b c : α variables (h₁ : r a b) (h₂ : r b c) #check trans_r -- trans_r : r ?M_1 ?M_2 → r ?M_2 ?M_3 → r ?M_1 ?M_3 #check trans_r h₁ -- trans_r h₁ : r b ?M_1 → r a ?M_1 #check trans_r h₁ h₂ -- trans_r h₁ h₂ : r a c -- The advantage is that we can now write `trans_r h₁ h₂` as a proof of `r a c`. end example3 -- Example: elementary reasoning with an equivalence relation. namespace example4 variables (α : Type) (r : α → α → Prop) variable refl_r : ∀ {x : α}, r x x variable symm_r : ∀ {x y : α}, r x y → r y x variable trans_r : ∀ {x y z : α}, r x y → r y z → r x z example (a b c d : α) (h₁ : r a b) (h₂ : r c b) (h₃ : r c d) : r a d := have h₄ : r b c, from (symm_r h₂), have h₅ : r a c, from (trans_r h₁ h₄), show r a d, from (trans_r h₅ h₃) -- after working out a proof in simple steps, we can make it more concise: example (a b c d : α) (h₁ : r a b) (h₂ : r c b) (h₃ : r c d) : r a d := (trans_r (trans_r h₁ (symm_r h₂)) h₃) end example4 /- It is the typing rule for Pi types, and the universal quantifier in particular, that distinguishes Prop from other types. Suppose we have α : Sort i and β : Sort j, where the expression β may depend on a variable x : α. Then Π x : α, β is an element of Type (imax i j), where imax i j is the maximum of i and j if j is not 0, and 0 otherwise. The idea is as follows: If j is not 0, then Π x : α, β is an element of Sort (max i j). In other words, the type of dependent functions from α to β "lives" in the universe whose index is the maximum of i and j. Suppose, however, that β is of Sort 0, that is, an element of Prop. In that case, Π x : α, β is an element of Sort 0 as well, no matter which type universe α lives in. In other words, if β is a proposition depending on α, then ∀ x : α, β is again a proposition. This reflects the interpretation of Prop as the type of propositions rather than data, and it is what makes Prop *impredicative*. Impredicativity of Prop Type: If α is a type, we can form the type α → Prop of all predicates on α (aka the "power type" of α). The impredicativity of Prop means that we can form propositions (i.e., inhabitants of Prop) that quantify over α → Prop. In particular, we can define predicates on α by quantifying over all predicates on α; this is the type of circularity that was once considered problematic. -/ end Sec_4_1 #print "==================================" #print "Section 4.2 Equality" #print " " /- Chapter 7 explains how equality is defined from primitives of Lean's logical framework. Here we just see how to use equality. -/ -- Of course, equality is an equivalence relation: namespace Sec_4_2 namespace example1 #check eq.refl -- ∀ (a : ?M_1), a = a #check eq.symm -- ?M_2 = ?M_3 → ?M_3 = ?M_2 #check eq.trans -- ?M_2 = ?M_3 → ?M_3 = ?M_4 → ?M_2 = ?M_4 /- Output is easier to read if we have Lean instantiate implicit arguments at a particular universe u. -/ universe u #check @eq.refl.{u} -- ∀ {α : Sort u} (a : α), a = a #check @eq.symm.{u} -- ∀ {α : Sort u} {a b : α}, a = b → b = a #check @eq.trans.{u} -- ∀ {α : Sort u} {a b c : α}, a = b → b = c → a = c end example1 /- Reflexivity is more powerful than it looks. Recall that the Calculus of Constructions treats terms with a common reduct as equal. As a result, some nontrivial identities can be proved by reflexivity. -/ namespace example2 universe u variables (α β : Type u) example (f : α → β) (a : α) : (λ x : α, f x) a = f a := eq.refl _ example (a₁ : α) (a₂ : α) : (a₁, a₂).1 = a₁ := eq.refl _ example : 2 + 3 = 5 := eq.refl _ -- This feature is so important Lean defines a notation `rfl` for `eq.refl _`. example (f : α → β) (a : α) : (λ x : α, f x) a = f a := rfl end example2 /- Equality is more than just an equivalence relation. It is a congruence (the smallest congruence) in every congruence lattice; thus, it has the property that every assertion respects the equivalence. -/ /- For instance, given `h₁ : a = b` and `h₂ : p a`, we can construct a proof for `p b` using substitution: `eq.subst h₁ h₂`. -/ namespace example3 universe u example (α : Type u) (a₁ a₂ : α) (p : α → Prop) (h₁ : a₁ = a₂) (h₂ : p a₁) : p a₂ := eq.subst h₁ h₂ -- Lean has a shorthand for eq.subst as well; it's ▸, as shown here. example (α : Type u) (a₁ a₂ : α) (p : α → Prop) (h₁ : a₁ = a₂) (h₂ : p a₁) : p a₂ := h₁ ▸ h₂ end example3 /- `eq.subst` is used to define the following auxiliary rules, which carry out more explicit substitutions. They deal with applicative terms (of form s t). Specifically, we use `congr_arg` to replace the argument, `congr_fun` to replace the term that is being applied, and `congr` to replace both at once. -/ namespace example4 variable α : Type variables a₁ a₂ : α variables f g : α → ℕ variable h₁ : a₁ = a₂ variable h₂ : f = g example : f a₁ = f a₂ := congr_arg f h₁ example : f a₁ = g a₁ := congr_fun h₂ a₁ example : f a₁ = g a₂ := congr h₂ h₁ end example4 namespace example5 variables a b c d : ℤ example : a + 0 = a := add_zero a example : 0 + a = a := zero_add a example : a * 1 = a := mul_one a example : 1 * a = a := one_mul a example : -a + a = 0 := neg_add_self a example : a + -a = 0 := add_neg_self a example : a - a = 0 := sub_self a example : a + b = b + a := add_comm a b example : a + b + c = a + (b + c) := add_assoc a b c example : a * b = b * a := mul_comm a b example : a * b * c = a * (b * c) := mul_assoc a b c example : a * (b + c) = a * b + a * c := mul_add a b c example : a * (b + c) = a * b + a * c := left_distrib a b c example : (a + b) * c = a * c + b * c := add_mul a b c example : (a + b) * c = a * c + b * c := right_distrib a b c example : a * (b - c) = a * b - a * c := mul_sub a b c example : (a - b) * c = a * c - b * c := sub_mul a b c end example5 /- Identities likes these work in arbitrary instances of the relevant algebraic structures, using the type class mechanism described in Chapter 10. Chapter 6 provides shows how to find theorems like this in the library. -/ namespace example6 example (x y z : ℕ) : x * (y + z) = x * y + x * z := mul_add x y z example (x y z : ℕ) : x + y + z = x + (y + z) := add_assoc x y z example (x y : ℕ) : (x + y) * (x + y) = x * x + y * x + x * y + y * y := --sorry have h₁ : (x + y) * (x + y) = x * (x + y) + y * (x + y), from add_mul x y (x + y), have h₃ : x * (x + y) = x * x + x * y, from mul_add x x y, have h₄ : y * (x + y) = y * x + y * y, from mul_add y x y, calc (x + y) * (x + y) = x * (x + y) + y * (x + y) : by rw h₁ ... = (x*x + x*y) + (y*x + y*y) : by rw [h₃, h₄] ... = x*x + y*x + x*y + y*y : by simp end example6 /- The 2nd implicit parameter to ▸ provides the context in which the subst occurs. This parameter has type α → Prop, so inferring this predicate requires an instance of *higher-order unification*. The problem of determining whether a higher-order unifier exists is undecidable, and Lean can at best provide imperfect and approximate solutions. As a result, eq.subst doesn't always do what you want; this is discussed in greater detail in Section 6.10. -/ /- IMPORTANT (for project proposal) Because equational reasoning is so common and important, Lean provides a number of mechanisms to carry it out more effectively. The next section offers syntax that allow you to write calculational proofs in a more natural and perspicuous way. But, more importantly, equational reasoning is supported by a term rewriter, a simplifier, and other kinds of automation. The term rewriter and simplifier are described briefly in the next setion, and then in greater detail in the next chapter. -/ end Sec_4_2 #print "==================================" #print "Section 4.3 Calculational Proofs" #print " " /- A calculational proof (or "proof sequence") is a chain of intermediate results meant to be composed by basic principles such as the transitivity of equality. In Lean, such proofs start with the keyword `calc`, and have the following syntax: calc <expr>_0 'op_1' <expr>_1 ':' <proof>_1 '...' 'op_2' <expr>_2 ':' <proof>_2 ... '...' 'op_n' <expr>_n ':' <proof>_n where each `<proof>_i` is a proof for `<expr>_{i-1} op_i <expr>_i`. -/ namespace Sec_4_3 namespace example1 variables a b c d e : ℕ variables h₁ : a = b variables h₂ : b = c + 1 variables h₃ : c = d variables h₄ : e = 1 + d theorem T : a = e := calc a = b : h₁ ... = c + 1 : h₂ ... = d + 1 : congr_arg _ h₃ ... = 1 + d : add_comm d ( 1 : ℕ) ... = e : eq.symm h₄ end example1 /- The style is most effective when used in conjunction with `simp` and `rewrite` tactics, discussed in greater detail in Ch 5. -/ namespace example2 variables a b c d e : ℕ variables h₁ : a = b variables h₂ : b = c + 1 variables h₃ : c = d variables h₄ : e = 1 + d include h₁ h₂ h₃ h₄ theorem T₁ : a = e := calc a = b : h₁ ... = c + 1 : h₂ ... = d + 1 : by rw h₃ ... = 1 + d : by rw add_comm ... = e : by rw h₄ -- Rewrites can applied sequentially, so the above can be shortened as follows: theorem T₂ : a = e := calc a = d + 1 : by rw [h₁, h₂, h₃] ... = 1 + d : by rw add_comm ... = e : by rw h₄ -- ...or even this theorem T₃ : a = e := by rw [h₁, h₂, h₃, add_comm, h₄] end example2 end Sec_4_3 #print "==================================" #print "Section 4.4 The Existential Quantifier" #print " " /- Finally, consider the existential quantifier, which can be written as either `exists x : α, p x` or `∃ x : α, p x`. Both versions are actually abbreviations for the expression `Exists (λ x : α, p x)`, defined in Lean's library. -/ /- As usual, the library includes both an intro and elim rule for exists. INTRO: to prove `∃ x : α, p x`, it suffices to provide a term `t : α` and a proof of `p t`. -/ namespace Sec_4_4 namespace example1 open nat example : ∃ x : ℕ, x > 0 := have h : 1 > 0, from zero_lt_succ 0, exists.intro 1 h example (x : ℕ) (h : x > 0) : ∃ y : ℕ, y < x := exists.intro 0 h example (x y z : ℕ) (h₁ : x < y) (h₂ : y < z) : ∃ w, x < w ∧ w < z := exists.intro y (and.intro h₁ h₂) #check @exists.intro end example1 namespace example2 variable g : ℕ → ℕ → ℕ variable h₁ : g 0 0 = 0 theorem g₁ : ∃ x, g x x = x := ⟨0, h₁⟩ theorem g₂ : ∃ x, g x 0 = x := ⟨0, h₁⟩ theorem g₃ : ∃ x, g 0 0 = x := ⟨0, h₁⟩ theorem g₄ : ∃ x, g x x = 0 := ⟨0, h₁⟩ set_option pp.implicit true -- (to display implicit arguments) #check g₁ #check g₂ #check g₃ #check g₄ end example2 /- We can view `exists.intro` as an information-hiding operation, since it hides the witness to the body of the assertion. The existential elimination rule, `exists.elim`, performs the opposite operation. It allows us to prove a proposition `q` from `∃ x : α, p x`, by showing that `q` follows from `p w` for an arbitrary value `w`. Roughly speaking, since we know there is some `x` satisfying `p x`, we can give it a name, say, `w`. If `q` does not mention `w`, then showing that `q` follows from `p w` is tantamount to showing that `q` follows from the existence of any such `x`. -/ namespace example3 variables (α : Type) (p q : α → Prop) example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x := exists.elim h (assume w, assume hw : p w ∧ q w, show ∃ x, q x ∧ p x, from ⟨w, hw.right, hw.left⟩) /- IMPORTANT NOTE: The anonymous constructor notation ⟨w, hw.right, hw.left⟩ abbreviates a nested application. Equivalently, ⟨w, ⟨hw.right, hw.left⟩⟩. -/ end example3 /- It may be helpful to compare the exists-elimination rule to the or-elimination rule: the assertion ∃ x : α, p x can be thought of as a big disjunction of the propositions p a, as a ranges over all the elements of α. -/ /- An existential proposition is very similar to a sigma type (Sec 2.8). The difference is that, given `a : α` and `h : p a`, the term `exists.intro a h` has type `(∃ x : α, p x) : Prop` whereas the term `sigma.mk a h` has type `(Σ x : α, p x) : Type` The similarity between ∃ and Σ is another instance of Curry-Howard. -/ -- PATTERN MATCHING -- namespace example4 variables (α : Type) (p q : α → Prop) example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x := match h with ⟨w, hw⟩ := ⟨w, hw.right, hw.left⟩ end -- We can (and should, imho) annotate the types used in the match for greater clarity. example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x := match h with ⟨(w: α), (hw : p w ∧ q w)⟩ := ⟨w, hw.right, hw.left⟩ end -- We can even use match to decompose the conjunction at the same time. example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x := match h with ⟨(w: α), (hwp : p w), (hwq : q w)⟩ := ⟨w, hwq, hwp⟩ end -- Lean will even allow us to use an implicit match in the assume statement: example : (∃ x, p x ∧ q x) → ∃ x, q x ∧ p x := assume ⟨(w: α), (hwp : p w), (hwq : q w)⟩, ⟨w, hwq, hwp⟩ -- In Ch 8 we see that all these variations are instances of a more general pattern-matching construct. end example4 /- Next, we define `even a` as `∃ b, a = 2*b`, and then show the sum of two even numbers is even. -/ namespace example5 def is_even (a : ℕ) := ∃ b, a = 2 * b theorem even_plus_even_is_even {a b : ℕ} (h₁ : is_even a) (h₂ : is_even b) : is_even (a + b) := exists.elim h₁ (assume w₁, assume hw1 : a = 2 * w₁, exists.elim h₂ (assume w₂, assume hw2 : b = 2 * w₂, exists.intro (w₁ + w₂) (calc a + b = 2 * w₁ + 2 * w₂ : by rw [hw1, hw2] ... = 2 * (w₁ + w₂) : by rw mul_add))) -- we can rewrite the last proof more concisely... theorem even_plus_even {a b : ℕ} (h₁ : is_even a) (h₂ : is_even b) : is_even (a + b) := match h₁, h₂ with ⟨w₁, hw1⟩, ⟨w₂, hw2⟩ := ⟨w₁ + w₂, by rw [hw1, hw2, mul_add]⟩ end -- ...again, it's clearer to annotate the types. theorem even_plus_even' {a b : ℕ} (h₁ : is_even a) (h₂ : is_even b) : is_even (a + b) := match h₁, h₂ with ⟨(w₁ : ℕ), (hw1: a = 2 * w₁)⟩, ⟨(w₂ : ℕ), (hw2 : b = 2 * w₂)⟩ := ⟨w₁ + w₂, by rw [hw1, hw2, mul_add]⟩ end end example5 /- Just as the constructive "or" is stronger than the classical "or," so, too, is the constructive "exists" stronger than the classical "exists". For example, the following implication requires classical reasoning because, from a constructive standpoint, knowing that it is not the case that every x satisfies `¬ p x` is not the same as having a particular `x` that satisfies `p x`. -/ namespace example6 open classical variables (α : Type) (p : α → Prop) example (h : ¬ ∀ x, ¬ p x) : ∃ x, p x := by_contradiction (assume h₁ : ¬ ∃ x, p x, have h₂ : ∀ x, ¬ p x, from assume x, assume h₃ : p x, have h₄ : ∃ x, p x, from ⟨x, h₃⟩, show false, from h₁ h₄, show false, from h h₂) end example6 /- Here are some common identities involving the existential quantifier. (Prove as many as you can and determine which are nonconstructive, and hence require some form of classical reasoning.) -/ namespace constructive_examples variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop example : (∃ x : α, r) → r := assume ⟨(w : α), hr⟩, hr example : r → (∃ x : α, r) := exists.intro a example : (∃ x, p x ∧ r) ↔ (∃ x, p x) ∧ r := iff.intro -- ⇒ (assume ⟨w, (h₁ : p w ∧ r)⟩, ⟨⟨w, h₁.left⟩, h₁.right⟩) -- ⇐ (assume (h : (∃ x, p x) ∧ r), exists.elim h.left (assume w, assume hpw : p w, show (∃ x, p x ∧ r), from ⟨w, hpw, h.right⟩ ) ) example : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := iff.intro -- ⇒ (assume ⟨w, (h₁ : p w ∨ q w)⟩, or.elim h₁ (assume hpw : p w, or.inl ⟨w, hpw⟩) (assume hqw : q w, or.inr ⟨w, hqw⟩)) -- ⇐ (assume h : (∃ x, p x) ∨ (∃ x, q x), or.elim h (assume ⟨w, hpw⟩, ⟨w, or.inl hpw⟩) (assume ⟨w, hpq⟩, ⟨w, or.inr hpq⟩)) end constructive_examples namespace example1 /- (∀ x, p x) ↔ ¬ (∃ x, ¬ p x) NOTE: for this example, only one direction require classical axioms. We split the proof up in two in order to highlight this fact. -/ variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop -- Constructive direction: theorem constructive₁ : (∀ x, p x) → ¬ (∃ x, ¬ p x) := assume h : ∀ x, p x, assume h' : (∃ x, ¬ p x), -- then reach a contradiction, to conclude `¬ (∃ x, ¬ p x)` exists.elim h' (assume w, assume hw': ¬ p w, have hw : p w, from h w, show false, from hw' hw) -- Classical direction: open classical lemma dne : Π {α : Type} {x : α} {p : α → Prop}, (¬ ¬ (p x)) → (p x) := λ (α : Type) (x : α) (p : α → Prop) (h : ¬ ¬ p x), or.elim (em (p x)) (assume h₂ : p x, h₂) -- alternatively, (assume h₂ : ¬p, absurd h₂ h) (assume h₃ : ¬ p x, false.elim (h h₃)) #check dne theorem classical₁ : ¬ (∃ x, ¬ p x) → (∀ x, p x) := assume h₁ : ¬ (∃ x, ¬ p x), by_contradiction (assume hc : ¬ (∀ x, p x), -- (reach a contradiction to prove `∀ x, p x`) have hcc : ∀ x, ¬ ¬ p x, from assume w, assume h₂ : ¬ p w, -- (reach a contradiction to prove hcc) have h₃ : ∃ x, ¬ p x, from ⟨w, h₂⟩, show false, from h₁ h₃, -- (done proving hcc) have hz : ∀ x, p x, from assume z, show p z, from dne (hcc z), show false, from hc hz) #check classical₁ end example1 namespace example2 /- We are asked to prove the following equivalence: (∃ x, p x) ↔ ¬ (∀ x, ¬ p x) As above, we split it up to show one direction can be done constructively. -/ variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop theorem constructive₂ : (∃ x, p x) → ¬ (∀ x, ¬ p x) := assume h : ∃ x, p x, assume h' : ∀ x, ¬ p x, -- reach a contradiction (this is how we prove a negation) exists.elim h (assume w, assume hc : p w, have hnc : ¬ p w, from h' w, show false, from hnc hc) #check constructive₂ open classical theorem classical₂ : ¬ (∀ x, ¬ p x) → (∃ x, p x) := assume h : ¬ (∀ x, ¬ p x), by_contradiction ( assume h₁ : ¬ (∃ x, p x), -- (reach a contradiction to prove `∃ x, p x`) have hcc : ∀ x, ¬ p x, from assume w, assume h₂ : p w, -- (reach a contradiction to prove hcc) have h₃ : ∃ x, p x, from ⟨w, h₂⟩, show false, from h₁ h₃, -- (done proving hcc) show false, from h hcc) end example2 namespace example3 /- We are asked to prove the following equivalence: (¬ ∃ x, p x) ↔ (∀ x, ¬ p x) Again, we split it up to show one direction can be done without classical axioms. -/ variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop theorem constructive₃ : (∀ x, ¬ p x) → (¬ ∃ x, p x) := assume h : ∀ x, ¬ p x, assume hn : ∃ x, p x, exists.elim hn ( assume w, assume hw : p w, have hwc : ¬ p w, from h w, show false, from hwc hw) open classical theorem classical₃ : (¬ ∃ x, p x) → (∀ x, ¬ p x) := assume h : ¬ ∃ x, p x, by_contradiction ( assume hc : ¬ (∀ x, ¬ p x), have hcc : ∃ x, p x, from example2.classical₂ α p hc, show false, from h hcc) end example3 namespace example4 /- `(¬ ∀ x, p x) ↔ (∃ x, ¬ p x)` splits into constructive/classical directions -/ variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop theorem constructive₄ : (∃ x, ¬ p x) → (¬ ∀ x, p x) := assume h : ∃ x, ¬ p x, assume hn : ∀ x, p x, exists.elim h (assume w, assume hnw : ¬ p w, have hw : p w, from hn w, show false, from hnw hw) open classical theorem classical₄ : (¬ ∀ x, p x) → (∃ x, ¬ p x) := assume h : ¬ ∀ x, p x, by_contradiction ( assume hn : ¬ (∃ x, ¬ p x), have hc : ∀ x, p x, from example1.classical₁ α p hn, show false, from h hc) end example4 namespace example5 /- (∀ x, p x → r) ↔ (∃ x, p x) → r In this case, neither direction requires classical axioms. -/ variables (α :Type) (p q : α → Prop) variable a : α variable r : Prop theorem constructive₅ : (∀ x, (p x → r)) ↔ (∃ x, p x) → r := iff.intro (assume h₁ : ∀ x, (p x → r), assume h₂ : ∃ x, p x, exists.elim h₂ (assume w, assume hw : p w, show r, from h₁ w hw)) (assume h : ((∃ x, p x) → r), assume w, assume hw : p w, show r, from h ⟨w, hw⟩) end example5 namespace example6 /- (∃ x, p x → r) ↔ (∀ x, p x) → r := -/ theorem constructive₆ : (∃ x, p x → r) → (∀ x, p x) → r := (assume h : ∃ x, p x → r, assume h' : ∀ x, p x, exists.elim h ( assume w, assume hw : p w → r, show r, from hw (h' w) ) ) theorem classical₆ : (∀ x, p x) → r → (∃ x, p x → r) := sorry end example6 namespace example7 example : (∃ x, r → p x) ↔ (r → ∃ x, p x) := sorry end example7 end Sec_4_4 #print "==================================" #print "Section 4.5 More on the Proof Language" #print " " namespace Sec_4_5 end Sec_4_5 #print "==================================" #print "Section 4.6 Exercises" #print " " namespace Sec_4_6 end Sec_4_6
41547b6ebf412195be86a8e8617f3aa079d844f8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/group_action/group.lean
54b45136c44a9bc90f9c018f7dbcfeee1718bc9f
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
9,478
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.equiv.mul_add_aut import group_theory.group_action.units /-! # Group actions applied to various types of group This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action /-- `monoid.to_mul_action` is faithful on cancellative monoids. -/ @[to_additive /-" `add_monoid.to_add_action` is faithful on additive cancellative monoids. "-/] instance right_cancel_monoid.to_has_faithful_scalar [right_cancel_monoid α] : has_faithful_scalar α α := ⟨λ x y h, mul_right_cancel (h 1)⟩ section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive, simps] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm /-- `mul_action.to_perm` is injective on faithful actions. -/ @[to_additive] lemma mul_action.to_perm_injective [has_faithful_scalar α β] : function.injective (mul_action.to_perm : α → equiv.perm β) := (show function.injective (equiv.to_fun ∘ mul_action.to_perm), from smul_left_injective').of_comp variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } /-- The tautological action by `equiv.perm α` on `α`. This generalizes `function.End.apply_mul_action`.-/ instance equiv.perm.apply_mul_action (α : Type*) : mul_action (equiv.perm α) α := { smul := λ f a, f a, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma equiv.perm.smul_def {α : Type*} (f : equiv.perm α) (a : α) : f • a = f a := rfl /-- `equiv.perm.apply_mul_action` is faithful. -/ instance equiv.perm.apply_has_faithful_scalar (α : Type*) : has_faithful_scalar (equiv.perm α) α := ⟨λ x y, equiv.ext⟩ variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] lemma smul_zpow [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) (p : ℤ) : (c • x) ^ p = c ^ p • x ^ p := by { cases p; simp [smul_pow, smul_inv] } @[to_additive] protected lemma mul_action.bijective (g : α) : function.bijective (λ b : β, g • b) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : function.injective (λ b : β, g • b) := (mul_action.bijective g).injective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group /-- `monoid.to_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_scalar [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_scalar α α := ⟨λ x y h, mul_left_injective₀ one_ne_zero (h 1)⟩ section gwz variables [group_with_zero α] [mul_action α β] @[simp] lemma inv_smul_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] variables (β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def distrib_mul_action.to_add_equiv (x : α) : β ≃+ β := { .. distrib_mul_action.to_add_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def distrib_mul_action.to_add_aut : α →* add_aut β := { to_fun := distrib_mul_action.to_add_equiv β, map_one' := add_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, add_equiv.ext (mul_smul _ _) } variables {α β} theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := smul_eq_zero_iff_eq (units.mk0 a ha) theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := smul_ne_zero_iff_ne (units.mk0 a ha) end gwz end distrib_mul_action section mul_distrib_mul_action variables [group α] [monoid β] [mul_distrib_mul_action α β] variables (β) /-- Each element of the group defines a multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def mul_distrib_mul_action.to_mul_equiv (x : α) : β ≃* β := { .. mul_distrib_mul_action.to_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def mul_distrib_mul_action.to_mul_aut : α →* mul_aut β := { to_fun := mul_distrib_mul_action.to_mul_equiv β, map_one' := mul_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, mul_equiv.ext (mul_smul _ _) } variables {α β} end mul_distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[simps] def arrow_action {G A B : Type*} [group G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [one_inv, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- When `B` is a monoid, `arrow_action` is additionally a `mul_distrib_mul_action`. -/ def arrow_mul_distrib_mul_action {G A B : Type*} [group G] [mul_action G A] [monoid B] : mul_distrib_mul_action G (A → B) := { smul_one := λ g, rfl, smul_mul := λ g f₁ f₂, rfl } local attribute [instance] arrow_mul_distrib_mul_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [monoid H] : G →* mul_aut (A → H) := mul_distrib_mul_action.to_mul_aut _ _ end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ smul_eq_zero_iff_eq u end distrib_mul_action end is_unit @[simp] lemma is_unit_smul_iff [group α] [monoid β] [mul_action α β] [smul_comm_class α β β] [is_scalar_tower α β β] {g : α} {m : β} : is_unit (g • m) ↔ is_unit m := ⟨λ h, inv_smul_smul g m ▸ h.smul g⁻¹, is_unit.smul g⟩
165aad3a4fff0f0fe67e54be2dbbf22fb78baf9a
737dc4b96c97368cb66b925eeea3ab633ec3d702
/stage0/src/Lean/Elab/Tactic/Conv/Simp.lean
2c8267c5694c760d16633f5f2e80b474c1f69a2b
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
625
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.Tactic.Simp import Lean.Elab.Tactic.Conv.Basic namespace Lean.Elab.Tactic.Conv open Meta @[builtinTactic Lean.Parser.Tactic.Conv.simp] def evalSimp : Tactic := fun stx => withMainContext do let { ctx, .. } ← mkSimpContext stx (eraseLocal := false) let lhs ← getLhs let result ← simp lhs ctx if result.proof?.isNone then changeLhs result.expr else updateLhs result.expr (← result.getProof) end Lean.Elab.Tactic.Conv
f8ce3ade7cc9d2c0e4e0fd42d8a38cd6c58c6c55
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/dynamics/omega_limit.lean
47587d5ed13f07145f5f24fc0ff4ffb4e12b08fd
[ "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
16,715
lean
/- Copyright (c) 2020 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo -/ import dynamics.flow /-! # ω-limits For a function `ϕ : τ → α → β` where `β` is a topological space, we define the ω-limit under `ϕ` of a set `s` in `α` with respect to filter `f` on `τ`: an element `y : β` is in the ω-limit of `s` if the forward images of `s` intersect arbitrarily small neighbourhoods of `y` frequently "in the direction of `f`". In practice `ϕ` is often a continuous monoid-act, but the definition requires only that `ϕ` has a coercion to the appropriate function type. In the case where `τ` is `ℕ` or `ℝ` and `f` is `at_top`, we recover the usual definition of the ω-limit set as the set of all `y` such that there exist sequences `(tₙ)`, `(xₙ)` such that `ϕ tₙ xₙ ⟶ y` as `n ⟶ ∞`. ## Notations The `omega_limit` locale provides the localised notation `ω` for `omega_limit`, as well as `ω⁺` and `ω⁻` for `omega_limit at_top` and `omega_limit at_bot` respectively for when the acting monoid is endowed with an order. -/ open set function filter open_locale topological_space /-! ### Definition and notation -/ section omega_limit variables {τ : Type*} {α : Type*} {β : Type*} {ι : Type*} /-- The ω-limit of a set `s` under `ϕ` with respect to a filter `f` is ⋂ u ∈ f, cl (ϕ u s). -/ def omega_limit [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) : set β := ⋂ u ∈ f, closure (image2 ϕ u s) localized "notation `ω` := omega_limit" in omega_limit localized "notation `ω⁺` := omega_limit at_top" in omega_limit localized "notation `ω⁻` := omega_limit at_bot" in omega_limit variables [topological_space β] variables (f : filter τ) (ϕ : τ → α → β) (s s₁ s₂: set α) /-! ### Elementary properties -/ lemma omega_limit_def : ω f ϕ s = ⋂ u ∈ f, closure (image2 ϕ u s) := rfl lemma omega_limit_subset_of_tendsto {m : τ → τ} {f₁ f₂ : filter τ} (hf : tendsto m f₁ f₂) : ω f₁ (λ t x, ϕ (m t) x) s ⊆ ω f₂ ϕ s := begin apply Inter_subset_Inter2, intro u, use m ⁻¹' u, apply Inter_subset_Inter2, intro hu, use tendsto_def.mp hf _ hu, rw ←image2_image_left, exact closure_mono (image2_subset (image_preimage_subset _ _) subset.rfl), end lemma omega_limit_mono_left {f₁ f₂ : filter τ} (hf : f₁ ≤ f₂) : ω f₁ ϕ s ⊆ ω f₂ ϕ s := omega_limit_subset_of_tendsto ϕ s (tendsto_id' hf) lemma omega_limit_mono_right {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) : ω f ϕ s₁ ⊆ ω f ϕ s₂ := bInter_subset_bInter_right $ λ u hu, closure_mono (image2_subset subset.rfl hs) lemma is_closed_omega_limit : is_closed (ω f ϕ s) := is_closed_Inter $ λ u, is_closed_Inter $ λ hu, is_closed_closure lemma maps_to_omega_limit' {α' β' : Type*} [topological_space β'] {f : filter τ} {ϕ : τ → α → β} {ϕ' : τ → α' → β'} {ga : α → α'} {s' : set α'} (hs : maps_to ga s s') {gb : β → β'} (hg : ∀ᶠ t in f, eq_on (gb ∘ (ϕ t)) (ϕ' t ∘ ga) s) (hgc : continuous gb) : maps_to gb (ω f ϕ s) (ω f ϕ' s') := begin simp only [omega_limit_def, mem_Inter, maps_to], intros y hy u hu, refine map_mem_closure hgc (hy _ (inter_mem_sets hu hg)) (forall_image2_iff.2 $ λ t ht x hx, _), calc gb (ϕ t x) = ϕ' t (ga x) : ht.2 hx ... ∈ image2 ϕ' u s' : mem_image2_of_mem ht.1 (hs hx) end lemma maps_to_omega_limit {α' β' : Type*} [topological_space β'] {f : filter τ} {ϕ : τ → α → β} {ϕ' : τ → α' → β'} {ga : α → α'} {s' : set α'} (hs : maps_to ga s s') {gb : β → β'} (hg : ∀ t x, gb (ϕ t x) = ϕ' t (ga x)) (hgc : continuous gb) : maps_to gb (ω f ϕ s) (ω f ϕ' s') := maps_to_omega_limit' _ hs (eventually_of_forall $ λ t x hx, hg t x) hgc lemma omega_limit_image_eq {α' : Type*} (ϕ : τ → α' → β) (f : filter τ) (g : α → α') : ω f ϕ (g '' s) = ω f (λ t x, ϕ t (g x)) s := by simp only [omega_limit, image2_image_right] lemma omega_limit_preimage_subset {α' : Type*} (ϕ : τ → α' → β) (s : set α') (f : filter τ) (g : α → α') : ω f (λ t x, ϕ t (g x)) (g ⁻¹' s) ⊆ ω f ϕ s := maps_to_omega_limit _ (maps_to_preimage _ _) (λ t x, rfl) continuous_id /-! ### Equivalent definitions of the omega limit The next few lemmas are various versions of the property characterising ω-limits: -/ /-- An element `y` is in the ω-limit set of `s` w.r.t. `f` if the preimages of an arbitrary neighbourhood of `y` frequently (w.r.t. `f`) intersects of `s`. -/ lemma mem_omega_limit_iff_frequently (y : β) : y ∈ ω f ϕ s ↔ ∀ n ∈ 𝓝 y, ∃ᶠ t in f, (s ∩ ϕ t ⁻¹' n).nonempty := begin simp_rw [frequently_iff, omega_limit_def, mem_Inter, mem_closure_iff_nhds], split, { intros h _ hn _ hu, rcases h _ hu _ hn with ⟨_, _, _, _, ht, hx, hϕtx⟩, exact ⟨_, ht, _, hx, by rwa [mem_preimage, hϕtx]⟩, }, { intros h _ hu _ hn, rcases h _ hn hu with ⟨_, ht, _, hx, hϕtx⟩, exact ⟨_, hϕtx, _, _, ht, hx, rfl⟩ } end /-- An element `y` is in the ω-limit set of `s` w.r.t. `f` if the forward images of `s` frequently (w.r.t. `f`) intersect arbitrary neighbourhoods of `y`. -/ lemma mem_omega_limit_iff_frequently₂ (y : β) : y ∈ ω f ϕ s ↔ ∀ n ∈ 𝓝 y, ∃ᶠ t in f, (ϕ t '' s ∩ n).nonempty := by simp_rw [mem_omega_limit_iff_frequently, image_inter_nonempty_iff] /-- An element `y` is in the ω-limit of `x` w.r.t. `f` if the forward images of `x` frequently (w.r.t. `f`) falls within an arbitrary neighbourhood of `y`. -/ lemma mem_omega_limit_singleton_iff_map_cluster_point (x : α) (y : β) : y ∈ ω f ϕ {x} ↔ map_cluster_pt y f (λ t, ϕ t x) := by simp_rw [mem_omega_limit_iff_frequently, map_cluster_pt_iff, singleton_inter_nonempty, mem_preimage] /-! ### Set operations and omega limits -/ lemma omega_limit_inter : ω f ϕ (s₁ ∩ s₂) ⊆ ω f ϕ s₁ ∩ ω f ϕ s₂ := subset_inter (omega_limit_mono_right _ _ (inter_subset_left _ _)) (omega_limit_mono_right _ _(inter_subset_right _ _)) lemma omega_limit_Inter (p : ι → set α) : ω f ϕ (⋂ i, p i) ⊆ ⋂ i, ω f ϕ (p i) := subset_Inter $ λ i, omega_limit_mono_right _ _ (Inter_subset _ _) lemma omega_limit_union : ω f ϕ (s₁ ∪ s₂) = ω f ϕ s₁ ∪ ω f ϕ s₂ := begin ext y, split, { simp only [mem_union, mem_omega_limit_iff_frequently, union_inter_distrib_right, union_nonempty, frequently_or_distrib], contrapose!, simp only [not_frequently, not_nonempty_iff_eq_empty, ← subset_empty_iff], rintro ⟨⟨n₁, hn₁, h₁⟩, ⟨n₂, hn₂, h₂⟩⟩, refine ⟨n₁ ∩ n₂, inter_mem_sets hn₁ hn₂, h₁.mono $ λ t, _, h₂.mono $ λ t, _⟩, exacts [subset.trans $ inter_subset_inter_right _ $ preimage_mono $ inter_subset_left _ _, subset.trans $ inter_subset_inter_right _ $ preimage_mono $ inter_subset_right _ _] }, { rintros (hy|hy), exacts [omega_limit_mono_right _ _ (subset_union_left _ _) hy, omega_limit_mono_right _ _ (subset_union_right _ _) hy] }, end lemma omega_limit_Union (p : ι → set α) : (⋃ i, ω f ϕ (p i)) ⊆ ω f ϕ ⋃ i, p i := by { rw Union_subset_iff, exact λ i, omega_limit_mono_right _ _ (subset_Union _ _)} /-! Different expressions for omega limits, useful for rewrites. In particular, one may restrict the intersection to sets in `f` which are subsets of some set `v` also in `f`. -/ lemma omega_limit_eq_Inter : ω f ϕ s = ⋂ u : ↥f.sets, closure (image2 ϕ u s) := bInter_eq_Inter _ _ lemma omega_limit_eq_bInter_inter {v : set τ} (hv : v ∈ f) : ω f ϕ s = ⋂ u ∈ f, closure (image2 ϕ (u ∩ v) s) := subset.antisymm (Inter_subset_Inter2 (λ u, ⟨u ∩ v, Inter_subset_Inter2 (λ hu, ⟨inter_mem_sets hu hv, subset.rfl⟩)⟩)) (Inter_subset_Inter (λ u, Inter_subset_Inter (λ hu, closure_mono (image2_subset (inter_subset_left _ _) subset.rfl)))) lemma omega_limit_eq_Inter_inter {v : set τ} (hv : v ∈ f) : ω f ϕ s = ⋂ (u : ↥f.sets), closure (image2 ϕ (u ∩ v) s) := by { rw omega_limit_eq_bInter_inter _ _ _ hv, apply bInter_eq_Inter } lemma omega_limit_subset_closure_fw_image {u : set τ} (hu : u ∈ f) : ω f ϕ s ⊆ closure (image2 ϕ u s) := begin rw omega_limit_eq_Inter, intros _ hx, rw mem_Inter at hx, exact hx ⟨u, hu⟩, end /-! ### `ω-limits and compactness -/ /-- A set is eventually carried into any open neighbourhood of its ω-limit: if `c` is a compact set such that `closure {ϕ t x | t ∈ v, x ∈ s} ⊆ c` for some `v ∈ f` and `n` is an open neighbourhood of `ω f ϕ s`, then for some `u ∈ f` we have `closure {ϕ t x | t ∈ u, x ∈ s} ⊆ n`. -/ lemma eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' {c : set β} (hc₁ : is_compact c) (hc₂ : ∃ v ∈ f, closure (image2 ϕ v s) ⊆ c) {n : set β} (hn₁ : is_open n) (hn₂ : ω f ϕ s ⊆ n) : ∃ u ∈ f, closure (image2 ϕ u s) ⊆ n := begin rcases hc₂ with ⟨v, hv₁, hv₂⟩, let k := closure (image2 ϕ v s), have hk : is_compact (k \ n) := is_compact.diff (compact_of_is_closed_subset hc₁ is_closed_closure hv₂) hn₁, let j := λ u, (closure (image2 ϕ (u ∩ v) s))ᶜ, have hj₁ : ∀ u ∈ f, is_open (j u), from λ _ _, (is_open_compl_iff.mpr is_closed_closure), have hj₂ : k \ n ⊆ ⋃ u ∈ f, j u, begin have : (⋃ u ∈ f, j u) = ⋃ (u : ↥f.sets), j u, from bUnion_eq_Union _ _, rw [this, diff_subset_comm, diff_Union], rw omega_limit_eq_Inter_inter _ _ _ hv₁ at hn₂, simp_rw diff_compl, rw ←inter_Inter, exact subset.trans (inter_subset_right _ _) hn₂, end, rcases hk.elim_finite_subcover_image hj₁ hj₂ with ⟨g, hg₁ : ∀ u ∈ g, u ∈ f, hg₂, hg₃⟩, let w := (⋂ u ∈ g, u) ∩ v, have hw₂ : w ∈ f, by simpa *, have hw₃ : k \ n ⊆ (closure (image2 ϕ w s))ᶜ, from calc k \ n ⊆ ⋃ u ∈ g, j u : hg₃ ... ⊆ (closure (image2 ϕ w s))ᶜ : begin simp only [Union_subset_iff, compl_subset_compl], intros u hu, mono* using [w], exact Inter_subset_of_subset u (Inter_subset_of_subset hu subset.rfl), end, have hw₄ : kᶜ ⊆ (closure (image2 ϕ w s))ᶜ, begin rw compl_subset_compl, calc closure (image2 ϕ w s) ⊆ _ : closure_mono (image2_subset (inter_subset_right _ _) subset.rfl) end, have hnc : nᶜ ⊆ (k \ n) ∪ kᶜ, by rw [union_comm, ←inter_subset, diff_eq, inter_comm], have hw : closure (image2 ϕ w s) ⊆ n, from compl_subset_compl.mp (subset.trans hnc (union_subset hw₃ hw₄)), exact ⟨_, hw₂, hw⟩ end /-- A set is eventually carried into any open neighbourhood of its ω-limit: if `c` is a compact set such that `closure {ϕ t x | t ∈ v, x ∈ s} ⊆ c` for some `v ∈ f` and `n` is an open neighbourhood of `ω f ϕ s`, then for some `u ∈ f` we have `closure {ϕ t x | t ∈ u, x ∈ s} ⊆ n`. -/ lemma eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset [t2_space β] {c : set β} (hc₁ : is_compact c) (hc₂ : ∀ᶠ t in f, maps_to (ϕ t) s c) {n : set β} (hn₁ : is_open n) (hn₂ : ω f ϕ s ⊆ n) : ∃ u ∈ f, closure (image2 ϕ u s) ⊆ n := eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' f ϕ _ hc₁ ⟨_, hc₂, closure_minimal (image2_subset_iff.2 (λ t, id)) hc₁.is_closed⟩ hn₁ hn₂ lemma eventually_maps_to_of_is_compact_absorbing_of_is_open_of_omega_limit_subset [t2_space β] {c : set β} (hc₁ : is_compact c) (hc₂ : ∀ᶠ t in f, maps_to (ϕ t) s c) {n : set β} (hn₁ : is_open n) (hn₂ : ω f ϕ s ⊆ n) : ∀ᶠ t in f, maps_to (ϕ t) s n := begin rcases eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset f ϕ s hc₁ hc₂ hn₁ hn₂ with ⟨u, hu_mem, hu⟩, refine mem_sets_of_superset hu_mem (λ t ht x hx, _), exact hu (subset_closure $ mem_image2_of_mem ht hx) end lemma eventually_closure_subset_of_is_open_of_omega_limit_subset [compact_space β] {v : set β} (hv₁ : is_open v) (hv₂ : ω f ϕ s ⊆ v) : ∃ u ∈ f, closure (image2 ϕ u s) ⊆ v := eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' _ _ _ compact_univ ⟨univ, univ_mem_sets, subset_univ _⟩ hv₁ hv₂ lemma eventually_maps_to_of_is_open_of_omega_limit_subset [compact_space β] {v : set β} (hv₁ : is_open v) (hv₂ : ω f ϕ s ⊆ v) : ∀ᶠ t in f, maps_to (ϕ t) s v := begin rcases eventually_closure_subset_of_is_open_of_omega_limit_subset f ϕ s hv₁ hv₂ with ⟨u, hu_mem, hu⟩, refine mem_sets_of_superset hu_mem (λ t ht x hx, _), exact hu (subset_closure $ mem_image2_of_mem ht hx) end /-- The ω-limit of a nonempty set w.r.t. a nontrivial filter is nonempty. -/ lemma nonempty_omega_limit_of_is_compact_absorbing [ne_bot f] {c : set β} (hc₁ : is_compact c) (hc₂ : ∃ v ∈ f, closure (image2 ϕ v s) ⊆ c) (hs : s.nonempty) : (ω f ϕ s).nonempty := begin rcases hc₂ with ⟨v, hv₁, hv₂⟩, rw omega_limit_eq_Inter_inter _ _ _ hv₁, apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed, { rintro ⟨u₁, hu₁⟩ ⟨u₂, hu₂⟩, use ⟨u₁ ∩ u₂, inter_mem_sets hu₁ hu₂⟩, split, all_goals { exact closure_mono (image2_subset (inter_subset_inter_left _ (by simp)) subset.rfl) }}, { intro u, have hn : (image2 ϕ (u ∩ v) s).nonempty, from nonempty.image2 (nonempty_of_mem_sets (inter_mem_sets u.prop hv₁)) hs, exact hn.mono subset_closure }, { intro _, apply compact_of_is_closed_subset hc₁ is_closed_closure, calc _ ⊆ closure (image2 ϕ v s) : closure_mono (image2_subset (inter_subset_right _ _) subset.rfl) ... ⊆ c : hv₂ }, { exact λ _, is_closed_closure }, end lemma nonempty_omega_limit [compact_space β] [ne_bot f] (hs : s.nonempty) : (ω f ϕ s).nonempty := nonempty_omega_limit_of_is_compact_absorbing _ _ _ compact_univ ⟨univ, univ_mem_sets, subset_univ _⟩ hs end omega_limit /-! ### ω-limits of Flows by a Monoid -/ namespace flow variables {τ : Type*} [topological_space τ] [add_monoid τ] [has_continuous_add τ] {α : Type*} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) open_locale omega_limit lemma is_invariant_omega_limit (hf : ∀ t, tendsto ((+) t) f f) : is_invariant ϕ (ω f ϕ s) := λ t, maps_to.mono (subset.refl _) (omega_limit_subset_of_tendsto ϕ s (hf t)) $ maps_to_omega_limit _ (maps_to_id _) (λ t' x, (ϕ.map_add _ _ _).symm) (continuous_const.flow ϕ continuous_id) lemma omega_limit_image_subset (t : τ) (ht : tendsto (+ t) f f) : ω f ϕ (ϕ t '' s) ⊆ ω f ϕ s := begin simp only [omega_limit_image_eq, ← map_add], exact omega_limit_subset_of_tendsto ϕ s ht end end flow /-! ### ω-limits of Flows by a Group -/ namespace flow variables {τ : Type*} [topological_space τ] [add_comm_group τ] [topological_add_group τ] {α : Type*} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) open_locale omega_limit /-- the ω-limit of a forward image of `s` is the same as the ω-limit of `s`. -/ @[simp] lemma omega_limit_image_eq (hf : ∀ t, tendsto (+ t) f f) (t : τ) : ω f ϕ (ϕ t '' s) = ω f ϕ s := subset.antisymm (omega_limit_image_subset _ _ _ _ (hf t)) $ calc ω f ϕ s = ω f ϕ (ϕ (-t) '' (ϕ t '' s)) : by simp [image_image, ← map_add] ... ⊆ ω f ϕ (ϕ t '' s) : omega_limit_image_subset _ _ _ _ (hf _) lemma omega_limit_omega_limit (hf : ∀ t, tendsto ((+) t) f f) : ω f ϕ (ω f ϕ s) ⊆ ω f ϕ s := begin simp only [subset_def, mem_omega_limit_iff_frequently₂, frequently_iff], intros _ h, rintro n hn u hu, rcases mem_nhds_iff.mp hn with ⟨o, ho₁, ho₂, ho₃⟩, rcases h o (is_open.mem_nhds ho₂ ho₃) hu with ⟨t, ht₁, ht₂⟩, have l₁ : (ω f ϕ s ∩ o).nonempty, from ht₂.mono (inter_subset_inter_left _ ((is_invariant_iff_image _ _).mp (is_invariant_omega_limit _ _ _ hf) _)), have l₂ : ((closure (image2 ϕ u s)) ∩ o).nonempty := l₁.mono (λ b hb, ⟨omega_limit_subset_closure_fw_image _ _ _ hu hb.1, hb.2⟩), have l₃ : (o ∩ image2 ϕ u s).nonempty, begin rcases l₂ with ⟨b, hb₁, hb₂⟩, exact mem_closure_iff_nhds.mp hb₁ o (is_open.mem_nhds ho₂ hb₂) end, rcases l₃ with ⟨ϕra, ho, ⟨_, _, hr, ha, hϕra⟩⟩, exact ⟨_, hr, ϕra, ⟨_, ha, hϕra⟩, ho₁ ho⟩, end end flow
7bd83ca5ef64a102e33e99921f8b9ebf303c4ddf
2ee768fce4d0783536570385ee340d97e2411436
/src/existence.lean
2956319b0fc8c3ecef09b3ff48a0ae78319f0d48
[]
no_license
agjftucker/exists-unique
835cf0c0ec622d49dbceb84be37db62e6ab8a6d0
1fba6035a08347f424c765d8a1c56b1a15c8abbe
refs/heads/master
1,683,445,592,336
1,622,649,822,000
1,622,649,822,000
309,485,697
2
0
null
null
null
null
UTF-8
Lean
false
false
4,083
lean
import u_prop local prefix `𝒫`:100 := fun {α : Type} (s : finset α), {t // t ≤ s} variables {𝒩 : Type} [decidable_eq 𝒩] {T : with_top ℝ} variables (ℋ : well_behaved_soln 𝒩 T) (ℰ : equity_function 𝒩 T) structure survivors_fn (ψ : ∀ (A : finset 𝒩), Tt T → X 𝒩 → 𝒫 A) : Prop := (idempotent : ∀ A t y, y ∈ V ψ (ψ A t y) t) (maximal : ∀ A B t y, ↑(ψ A t y) < B → B ≤ A → y ∉ V ψ B t) (consistent : ∀ A t y, y ∈ V ψ A t ↔ ∀ i ∈ A, 0 < E_star ℰ (v ℋ ψ A) t y i) variables {ℋ} {ℰ} lemma φ_idempotent (A : finset 𝒩) : ∀ t y, y ∈ U ℋ ℰ (φ ℋ ℰ A t y) t := begin intros t y, apply mem_U_of_q, apply q_φ, end lemma φ_maximal (A B : finset 𝒩) (t : Tt T) (y : X 𝒩) : ↑(φ ℋ ℰ A t y) < B → B ≤ A → y ∉ U ℋ ℰ B t := begin intros hφ hB, rw mem_U_iff_q, intro hq, apply hφ.2, rw φ_eq, exact si.le_φ_of_q (r_iff' A) ⟨B, hB⟩ hq, end lemma φ_consistent (A : finset 𝒩) (t : Tt T) (y : X 𝒩) : y ∈ U ℋ ℰ A t ↔ r ℋ ℰ t y A A := begin split, { intros hU i hi, cases q_of_mem_U hU with _ B hlt hr hq, { exact false.elim hi, }, { apply lt_of_lt_of_le (hr i hi), apply ℰ.mono_wrt_debt_valuation, apply u_mono hlt.1, }, }, { intro hr, cases dec_em (y ∈ U ℋ ℰ A t) with h hn, { exact h, }, { rw mem_U_iff_q, refine q.succ ⟨(φ ℋ ℰ A t y).prop, hn⟩ _ q_φ, intros i hi, delta E_star, rw ←u_match, apply hr i hi, }, }, end lemma exists_soln : survivors_fn ℋ ℰ (φ ℋ ℰ) := ⟨φ_idempotent, φ_maximal, φ_consistent⟩ theorem φ_greatest (A : finset 𝒩) (t : Tt T) (y : X 𝒩) : ∀ B ≤ A, y ∈ U ℋ ℰ B t → B ≤ φ ℋ ℰ A t y := begin intros B hB hU, apply le_trans hU, apply φ_mono hB, end noncomputable instance (A : finset 𝒩) (t : Tt T) (y : X 𝒩) : order_top {B : 𝒫 A | y ∈ U ℋ ℰ B t} := { top := ⟨φ ℋ ℰ A t y, φ_idempotent A t y⟩, le_top := fun ⟨B, hB⟩, φ_greatest A t y B B.prop hB, .. subtype.partial_order _ } lemma mem_Vψ_iff_q {ψ : ∀ A, Tt T → X 𝒩 → 𝒫 A} {A : finset 𝒩} (ih : ∀ B < A, ψ B = φ ℋ ℰ B) {B : finset 𝒩} (hB : B < A) : ∀ t y, y ∈ V ψ B t ↔ q (r ℋ ℰ t y) B := begin intros t y, rw ← mem_U_iff_q, change B ≤ ψ B t y ↔ B ≤ φ ℋ ℰ B t y, rw ih B hB, end variables {ψ : ∀ (A : finset 𝒩), Tt T → X 𝒩 → 𝒫 A} (hψ : survivors_fn ℋ ℰ ψ) variables {A : finset 𝒩} (ih : ∀ B < A, ψ B = φ ℋ ℰ B) include hψ ih lemma U_subset_Vψ : U ℋ ℰ A ⊆ V ψ A := begin intros t y hA, rw mem_U_iff_q at hA, by_contra hnV, have ltA : ↑(ψ A t y) < A := ⟨(ψ A t y).prop, hnV⟩, have hq : q (r ℋ ℰ t y) (ψ A t y), { rw ← mem_Vψ_iff_q ih ltA, apply hψ.idempotent, }, obtain ⟨B, hB₁, hB₂, hB₃, hB₄⟩ := exists_ge_term_of_q hA (ψ A t y) ltA hq, cases lt_or_eq_of_le hB₁ with ltB eqB, { apply hψ.maximal A B t y ltB hB₂.1, rw mem_Vψ_iff_q ih hB₂, exact hB₃, }, { apply hnV, rw hψ.consistent, delta E_star, rw [v_match, v_eq_of_ψ_eq_on_ssubsets A ih _ ltA, eqB], exact hB₄, }, end lemma ψ_eq_φ_on_compl_Vψ {t : Tt T} : (V ψ A t)ᶜ.eq_on (ψ A t) (φ ℋ ℰ A t) := begin intros y hnV, have hUψ : y ∈ U ℋ ℰ (ψ A t y) t, { delta U V, rw ← ih _ ⟨(ψ A t y).prop, hnV⟩, apply hψ.idempotent, }, suffices : (⟨ψ A t y, hUψ⟩ : {B : 𝒫 A | y ∈ U ℋ ℰ B t}) = ⊤, { rwa subtype.ext_iff at this, }, apply eq_top_of_maximal, rintros ⟨B, hUB⟩ hψB, apply hψ.maximal A B t y hψB B.prop, refine U_subset_Vψ hψ _ t hUB, intros C hC, apply ih, exact lt_of_lt_of_le hC B.prop, end lemma vψ_eq_u_on_compl_Vψ {t : Tt T} : (V ψ A t)ᶜ.eq_on (v ℋ ψ A t) (u ℋ ℰ A t) := begin intros y hnV, rw [v_match, u_match, ← ψ_eq_φ_on_compl_Vψ hψ ih hnV], rw v_eq_of_ψ_eq_on_ssubsets _ ih _ ⟨(ψ A t y).prop, hnV⟩, refl, end
0b5b506989cde7242ef95dfb5e2e110db64fd129
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/algebra/category/Group/adjunctions.lean
f9ba10049cc4a327fa7cfe95efe05d20de43291c
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
1,818
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl -/ import algebra.category.Group import category_theory.adjunction.basic import group_theory.free_abelian_group /-! The free abelian group on a type is the left adjoint of the forgetful functor from abelian groups to types. -/ noncomputable theory universe u open category_theory namespace AddCommGroup open_locale classical /-- The free functor `Type u ⥤ AddCommGroup.{u}` sending a type `X` to the free abelian group with generators `x : X`. -/ def free : Type u ⥤ AddCommGroup.{u} := { obj := λ α, of (free_abelian_group α), map := λ X Y f, add_monoid_hom.of (λ x : free_abelian_group X, f <$> x), map_id' := λ X, add_monoid_hom.ext $ by simp, map_comp' := λ X Y Z f g, add_monoid_hom.ext $ by { intro x, simp [is_lawful_functor.comp_map], } } @[simp] lemma free_obj_coe {α : Type u} : (free.obj α : Type u) = (free_abelian_group α) := rfl @[simp] lemma free_map_coe {α β : Type u} {f : α → β} (x : free_abelian_group α) : (free.map f) x = f <$> x := rfl /-- The free-forgetful adjunction for abelian groups. -/ def adj : free ⊣ forget AddCommGroup.{u} := adjunction.mk_of_hom_equiv { hom_equiv := λ X G, free_abelian_group.hom_equiv X G, hom_equiv_naturality_left_symm' := by {intros, ext, dsimp at *, simp [free_abelian_group.lift_comp],} } /-- As an example, we now give a high-powered proof that the monomorphisms in `AddCommGroup` are just the injective functions. (This proof works in all universes.) -/ example {G H : AddCommGroup.{u}} (f : G ⟶ H) [mono f] : function.injective f := (mono_iff_injective f).1 (right_adjoint_preserves_mono adj (by apply_instance : mono f)) end AddCommGroup
34eacf442dbe357981fa7d0d1a7d09d2a4769ab7
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/data/nat/basic.lean
0693c8ab39b44783c60058cd6f8e212c3cb49970
[ "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
24,116
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.core universes u namespace Nat @[extern cpp "lean::nat_dec_eq"] def beq : Nat → Nat → Bool | zero zero := true | zero (succ m) := false | (succ n) zero := false | (succ n) (succ m) := beq n m theorem eqOfBeqEqTt : ∀ {n m : Nat}, beq n m = true → n = m | zero zero h := rfl | zero (succ m) h := Bool.noConfusion h | (succ n) zero h := Bool.noConfusion h | (succ n) (succ m) h := have beq n m = true from h; have n = m from eqOfBeqEqTt this; congrArg succ this theorem neOfBeqEqFf : ∀ {n m : Nat}, beq n m = false → n ≠ m | zero zero h₁ h₂ := Bool.noConfusion h₁ | zero (succ m) h₁ h₂ := Nat.noConfusion h₂ | (succ n) zero h₁ h₂ := Nat.noConfusion h₂ | (succ n) (succ m) h₁ h₂ := have beq n m = false from h₁; have n ≠ m from neOfBeqEqFf this; Nat.noConfusion h₂ (fun h₂ => absurd h₂ this) @[extern cpp "lean::nat_dec_eq"] protected def decEq (n m : @& Nat) : Decidable (n = m) := if h : beq n m = true then isTrue (eqOfBeqEqTt h) else isFalse (neOfBeqEqFf (eqFalseOfNeTrue h)) @[inline] instance : DecidableEq Nat := {decEq := Nat.decEq} @[extern cpp "lean::nat_dec_le"] def ble : Nat → Nat → Bool | zero zero := true | zero (succ m) := true | (succ n) zero := false | (succ n) (succ m) := ble n m protected def le (n m : Nat) : Prop := ble n m = true instance : HasLessEq Nat := ⟨Nat.le⟩ protected def lt (n m : Nat) : Prop := Nat.le (succ n) m instance : HasLess Nat := ⟨Nat.lt⟩ @[extern cpp inline "lean::nat_sub(#1, lean::box(1))"] def pred : Nat → Nat | 0 := 0 | (a+1) := a @[extern cpp "lean::nat_sub"] protected def sub : (@& Nat) → (@& Nat) → Nat | a 0 := a | a (b+1) := pred (sub a b) @[extern cpp "lean::nat_mul"] protected def mul : (@& Nat) → (@& Nat) → Nat | a 0 := 0 | a (b+1) := (mul a b) + a instance : HasSub Nat := ⟨Nat.sub⟩ instance : HasMul Nat := ⟨Nat.mul⟩ @[specialize] def foldAux {α : Type u} (f : Nat → α → α) (s : Nat) : Nat → α → α | 0 a := a | (succ n) a := foldAux n (f (s - (succ n)) a) @[inline] def fold {α : Type u} (f : Nat → α → α) (n : Nat) (a : α) : α := foldAux f n n a @[specialize] def anyAux (f : Nat → Bool) (s : Nat) : Nat → Bool | 0 := false | (succ n) := f (s - (succ n)) || anyAux n /- `any f n = true` iff there is `i in [0, n-1]` s.t. `f i = true` -/ @[inline] def any (f : Nat → Bool) (n : Nat) : Bool := anyAux f n n @[inline] def all (f : Nat → Bool) (n : Nat) : Bool := !any (fun i => !f i) n @[specialize] def repeatAux {α : Type u} (f : α → α) : Nat → α → α | 0 a := a | (succ n) a := repeatAux n (f a) @[inline] def repeat {α : Type u} (f : α → α) (n : Nat) (a : α) : α := repeatAux f n a protected def pow (m : Nat) : Nat → Nat | 0 := 1 | (succ n) := pow n * m instance : HasPow Nat Nat := ⟨Nat.pow⟩ /- Nat.add theorems -/ protected theorem zeroAdd : ∀ (n : Nat), 0 + n = n | 0 := rfl | (n+1) := congrArg succ (zeroAdd n) theorem succAdd : ∀ (n m : Nat), (succ n) + m = succ (n + m) | n 0 := rfl | n (m+1) := congrArg succ (succAdd n m) theorem addSucc (n m : Nat) : n + succ m = succ (n + m) := rfl protected theorem addZero (n : Nat) : n + 0 = n := rfl theorem addOne (n : Nat) : n + 1 = succ n := rfl theorem succEqAddOne (n : Nat) : succ n = n + 1 := rfl protected theorem addComm : ∀ (n m : Nat), n + m = m + n | n 0 := Eq.symm (Nat.zeroAdd n) | n (m+1) := suffices succ (n + m) = succ (m + n) from Eq.symm (succAdd m n) ▸ this; congrArg succ (addComm n m) protected theorem addAssoc : ∀ (n m k : Nat), (n + m) + k = n + (m + k) | n m 0 := rfl | n m (succ k) := congrArg succ (addAssoc n m k) protected theorem addLeftComm : ∀ (n m k : Nat), n + (m + k) = m + (n + k) := leftComm Nat.add Nat.addComm Nat.addAssoc protected theorem addRightComm : ∀ (n m k : Nat), (n + m) + k = (n + k) + m := rightComm Nat.add Nat.addComm Nat.addAssoc protected theorem addLeftCancel : ∀ {n m k : Nat}, n + m = n + k → m = k | 0 m k h := Nat.zeroAdd m ▸ Nat.zeroAdd k ▸ h | (succ n) m k h := have n+m = n+k from have succ (n + m) = succ (n + k) from succAdd n m ▸ succAdd n k ▸ h; Nat.noConfusion this id; addLeftCancel this protected theorem addRightCancel {n m k : Nat} (h : n + m = k + m) : n = k := have m + n = m + k from Nat.addComm n m ▸ Nat.addComm k m ▸ h; Nat.addLeftCancel this /- Nat.mul theorems -/ protected theorem mulZero (n : Nat) : n * 0 = 0 := rfl theorem mulSucc (n m : Nat) : n * succ m = n * m + n := rfl protected theorem zeroMul : ∀ (n : Nat), 0 * n = 0 | 0 := rfl | (succ n) := (mulSucc 0 n).symm ▸ (zeroMul n).symm ▸ rfl theorem succMul : ∀ (n m : Nat), (succ n) * m = (n * m) + m | n 0 := rfl | n (succ m) := have succ (n * m + m + n) = succ (n * m + n + m) from congrArg succ (Nat.addRightComm _ _ _); (mulSucc n m).symm ▸ (mulSucc (succ n) m).symm ▸ (succMul n m).symm ▸ this protected theorem mulComm : ∀ (n m : Nat), n * m = m * n | n 0 := (Nat.zeroMul n).symm ▸ (Nat.mulZero n).symm ▸ rfl | n (succ m) := (mulSucc n m).symm ▸ (succMul m n).symm ▸ (mulComm n m).symm ▸ rfl protected theorem mulOne : ∀ (n : Nat), n * 1 = n := Nat.zeroAdd protected theorem oneMul (n : Nat) : 1 * n = n := Nat.mulComm n 1 ▸ Nat.mulOne n protected theorem leftDistrib : ∀ (n m k : Nat), n * (m + k) = n * m + n * k | 0 m k := (Nat.zeroMul (m + k)).symm ▸ (Nat.zeroMul m).symm ▸ (Nat.zeroMul k).symm ▸ rfl | (succ n) m k := have h₁ : succ n * (m + k) = n * (m + k) + (m + k) from succMul _ _; have h₂ : n * (m + k) + (m + k) = (n * m + n * k) + (m + k) from leftDistrib n m k ▸ rfl; have h₃ : (n * m + n * k) + (m + k) = n * m + (n * k + (m + k)) from Nat.addAssoc _ _ _; have h₄ : n * m + (n * k + (m + k)) = n * m + (m + (n * k + k)) from congrArg (fun x => n*m + x) (Nat.addLeftComm _ _ _); have h₅ : n * m + (m + (n * k + k)) = (n * m + m) + (n * k + k) from (Nat.addAssoc _ _ _).symm; have h₆ : (n * m + m) + (n * k + k) = (n * m + m) + succ n * k from succMul n k ▸ rfl; have h₇ : (n * m + m) + succ n * k = succ n * m + succ n * k from succMul n m ▸ rfl; (((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆).trans h₇ protected theorem rightDistrib (n m k : Nat) : (n + m) * k = n * k + m * k := have h₁ : (n + m) * k = k * (n + m) from Nat.mulComm _ _; have h₂ : k * (n + m) = k * n + k * m from Nat.leftDistrib _ _ _; have h₃ : k * n + k * m = n * k + k * m from Nat.mulComm n k ▸ rfl; have h₄ : n * k + k * m = n * k + m * k from Nat.mulComm m k ▸ rfl; ((h₁.trans h₂).trans h₃).trans h₄ protected theorem mulAssoc : ∀ (n m k : Nat), (n * m) * k = n * (m * k) | n m 0 := rfl | n m (succ k) := have h₁ : n * m * succ k = n * m * (k + 1) from rfl; have h₂ : n * m * (k + 1) = (n * m * k) + n * m * 1 from Nat.leftDistrib _ _ _; have h₃ : (n * m * k) + n * m * 1 = (n * m * k) + n * m from (Nat.mulOne (n*m)).symm ▸ rfl; have h₄ : (n * m * k) + n * m = (n * (m * k)) + n * m from (mulAssoc n m k).symm ▸ rfl; have h₅ : (n * (m * k)) + n * m = n * (m * k + m) from (Nat.leftDistrib n (m*k) m).symm; have h₆ : n * (m * k + m) = n * (m * succ k) from Nat.mulSucc m k ▸ rfl; ((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆ /- Inequalities -/ protected def leRefl : ∀ (n : Nat), n ≤ n | zero := rfl | (succ n) := leRefl n theorem leSucc : ∀ (n : Nat), n ≤ succ n | zero := rfl | (succ n) := leSucc n theorem succLeSucc {n m : Nat} (h : n ≤ m) : succ n ≤ succ m := h theorem succLtSucc {n m : Nat} : n < m → succ n < succ m := succLeSucc theorem leStep : ∀ {n m : Nat}, n ≤ m → n ≤ succ m | zero zero h := rfl | zero (succ n) h := rfl | (succ n) zero h := Bool.noConfusion h | (succ n) (succ m) h := have n ≤ m from h; have n ≤ succ m from leStep this; succLeSucc this theorem zeroLe : ∀ (n : Nat), 0 ≤ n | zero := rfl | (succ n) := rfl theorem zeroLtSucc (n : Nat) : 0 < succ n := succLeSucc (zeroLe n) def succPos := zeroLtSucc theorem notSuccLeZero : ∀ (n : Nat), succ n ≤ 0 → False . theorem notLtZero (n : Nat) : ¬ n < 0 := notSuccLeZero n theorem predLePred : ∀ {n m : Nat}, n ≤ m → pred n ≤ pred m | zero zero h := rfl | zero (succ n) h := zeroLe n | (succ n) zero h := Bool.noConfusion h | (succ n) (succ m) h := h theorem leOfSuccLeSucc {n m : Nat} : succ n ≤ succ m → n ≤ m := predLePred @[extern cpp "lean::nat_dec_le"] instance decLe (n m : @& Nat) : Decidable (n ≤ m) := decEq (ble n m) true @[extern cpp "lean::nat_dec_lt"] instance decLt (n m : @& Nat) : Decidable (n < m) := Nat.decLe (succ n) m protected theorem eqOrLtOfLe : ∀ {n m: Nat}, n ≤ m → n = m ∨ n < m | zero zero h := Or.inl rfl | zero (succ n) h := Or.inr $ zeroLe n | (succ n) zero h := Bool.noConfusion h | (succ n) (succ m) h := have n ≤ m from h; have n = m ∨ n < m from eqOrLtOfLe this; Or.elim this (fun h => Or.inl $ congrArg succ h) (fun h => Or.inr $ succLtSucc h) theorem ltSuccOfLe {n m : Nat} : n ≤ m → n < succ m := succLeSucc protected theorem subZero (n : Nat) : n - 0 = n := rfl theorem succSubSuccEqSub (n m : Nat) : succ n - succ m = n - m := Nat.recOn m (show succ n - succ zero = n - zero from (Eq.refl (succ n - succ zero))) (fun m => congrArg pred) theorem notSuccLeSelf : ∀ (n : Nat), ¬succ n ≤ n := fun n => Nat.rec (notSuccLeZero 0) (fun a b c => b (leOfSuccLeSucc c)) n protected theorem ltIrrefl (n : Nat) : ¬n < n := notSuccLeSelf n protected theorem leTrans : ∀ {n m k : Nat}, n ≤ m → m ≤ k → n ≤ k | zero m k h₁ h₂ := zeroLe _ | (succ n) zero k h₁ h₂ := Bool.noConfusion h₁ | (succ n) (succ m) zero h₁ h₂ := Bool.noConfusion h₂ | (succ n) (succ m) (succ k) h₁ h₂ := have h₁' : n ≤ m from h₁; have h₂' : m ≤ k from h₂; have n ≤ k from leTrans h₁' h₂'; succLeSucc this theorem predLe : ∀ (n : Nat), pred n ≤ n | zero := rfl | (succ n) := leSucc _ theorem predLt : ∀ {n : Nat}, n ≠ 0 → pred n < n | zero h := absurd rfl h | (succ n) h := ltSuccOfLe (Nat.leRefl _) theorem subLe (n m : Nat) : n - m ≤ n := Nat.recOn m (Nat.leRefl (n - 0)) (fun m => Nat.leTrans (predLe (n - m))) theorem subLt : ∀ {n m : Nat}, 0 < n → 0 < m → n - m < n | 0 m h1 h2 := absurd h1 (Nat.ltIrrefl 0) | (n+1) 0 h1 h2 := absurd h2 (Nat.ltIrrefl 0) | (n+1) (m+1) h1 h2 := Eq.symm (succSubSuccEqSub n m) ▸ show n - m < succ n from ltSuccOfLe (subLe n m) protected theorem ltOfLtOfLe {n m k : Nat} : n < m → m ≤ k → n < k := Nat.leTrans protected theorem leOfEq {n m : Nat} (p : n = m) : n ≤ m := p ▸ Nat.leRefl n theorem leSuccOfLe {n m : Nat} (h : n ≤ m) : n ≤ succ m := Nat.leTrans h (leSucc m) theorem leOfSuccLe {n m : Nat} (h : succ n ≤ m) : n ≤ m := Nat.leTrans (leSucc n) h protected theorem leOfLt {n m : Nat} (h : n < m) : n ≤ m := leOfSuccLe h def lt.step {n m : Nat} : n < m → n < succ m := leStep theorem eqZeroOrPos : ∀ (n : Nat), n = 0 ∨ n > 0 | 0 := Or.inl rfl | (n+1) := Or.inr (succPos _) protected theorem ltTrans {n m k : Nat} (h₁ : n < m) : m < k → n < k := Nat.leTrans (leStep h₁) protected theorem ltOfLeOfLt {n m k : Nat} (h₁ : n ≤ m) : m < k → n < k := Nat.leTrans (succLeSucc h₁) def lt.base (n : Nat) : n < succ n := Nat.leRefl (succ n) theorem ltSuccSelf (n : Nat) : n < succ n := lt.base n protected theorem leAntisymm : ∀ {n m : Nat}, n ≤ m → m ≤ n → n = m | zero zero h₁ h₂ := rfl | (succ n) zero h₁ h₂ := Bool.noConfusion h₁ | zero (succ m) h₁ h₂ := Bool.noConfusion h₂ | (succ n) (succ m) h₁ h₂ := have h₁' : n ≤ m from h₁; have h₂' : m ≤ n from h₂; have n = m from leAntisymm h₁' h₂'; congrArg succ this protected theorem ltOrGe : ∀ (n m : Nat), n < m ∨ n ≥ m | n 0 := Or.inr (zeroLe n) | n (m+1) := match ltOrGe n m with | Or.inl h => Or.inl (leSuccOfLe h) | Or.inr h => match Nat.eqOrLtOfLe h with | Or.inl h1 => Or.inl (h1 ▸ ltSuccSelf m) | Or.inr h1 => Or.inr h1 protected theorem leTotal (m n : Nat) : m ≤ n ∨ n ≤ m := Or.elim (Nat.ltOrGe m n) (fun h => Or.inl (Nat.leOfLt h)) Or.inr protected theorem ltOfLeAndNe {m n : Nat} (h1 : m ≤ n) : m ≠ n → m < n := resolveRight (Or.swap (Nat.eqOrLtOfLe h1)) theorem eqZeroOfLeZero {n : Nat} (h : n ≤ 0) : n = 0 := Nat.leAntisymm h (zeroLe _) theorem ltOfSuccLt {n m : Nat} : succ n < m → n < m := leOfSuccLe theorem ltOfSuccLtSucc {n m : Nat} : succ n < succ m → n < m := leOfSuccLeSucc theorem ltOfSuccLe {n m : Nat} (h : succ n ≤ m) : n < m := h theorem succLeOfLt {n m : Nat} (h : n < m) : succ n ≤ m := h theorem ltOrEqOrLeSucc {m n : Nat} (h : m ≤ succ n) : m ≤ n ∨ m = succ n := Decidable.byCases (fun (h' : m = succ n) => Or.inr h') (fun (h' : m ≠ succ n) => have m < succ n from Nat.ltOfLeAndNe h h'; have succ m ≤ succ n from succLeOfLt this; Or.inl (leOfSuccLeSucc this)) theorem leAddRight : ∀ (n k : Nat), n ≤ n + k | n 0 := Nat.leRefl n | n (k+1) := leSuccOfLe (leAddRight n k) theorem leAddLeft (n m : Nat): n ≤ m + n := Nat.addComm n m ▸ leAddRight n m theorem le.dest : ∀ {n m : Nat}, n ≤ m → Exists (fun k => n + k = m) | zero zero h := ⟨0, rfl⟩ | zero (succ n) h := ⟨succ n, show 0 + succ n = succ n from (Nat.addComm 0 (succ n)).symm ▸ rfl⟩ | (succ n) zero h := Bool.noConfusion h | (succ n) (succ m) h := have n ≤ m from h; have Exists (fun k => n + k = m) from le.dest this; match this with | ⟨k, h⟩ => ⟨k, show succ n + k = succ m from ((succAdd n k).symm ▸ h ▸ rfl)⟩ theorem le.intro {n m k : Nat} (h : n + k = m) : n ≤ m := h ▸ leAddRight n k protected theorem notLeOfGt {n m : Nat} (h : n > m) : ¬ n ≤ m := fun h₁ => Or.elim (Nat.ltOrGe n m) (fun h₂ => absurd (Nat.ltTrans h h₂) (Nat.ltIrrefl _)) (fun h₂ => have Heq : n = m from Nat.leAntisymm h₁ h₂; absurd (@Eq.subst _ _ _ _ Heq h) (Nat.ltIrrefl m)) theorem gtOfNotLe {n m : Nat} (h : ¬ n ≤ m) : n > m := Or.elim (Nat.ltOrGe m n) (fun h₁ => h₁) (fun h₁ => absurd h₁ h) protected theorem ltOfLeOfNe {n m : Nat} (h₁ : n ≤ m) (h₂ : n ≠ m) : n < m := Or.elim (Nat.ltOrGe n m) (fun h₃ => h₃) (fun h₃ => absurd (Nat.leAntisymm h₁ h₃) h₂) protected theorem addLeAddLeft {n m : Nat} (h : n ≤ m) (k : Nat) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ => have h₁ : k + n + w = k + (n + w) from Nat.addAssoc _ _ _; have h₂ : k + (n + w) = k + m from congrArg _ hw; le.intro $ h₁.trans h₂ protected theorem addLeAddRight {n m : Nat} (h : n ≤ m) (k : Nat) : n + k ≤ m + k := have h₁ : n + k = k + n from Nat.addComm _ _; have h₂ : k + n ≤ k + m from Nat.addLeAddLeft h k; have h₃ : k + m = m + k from Nat.addComm _ _; transRelLeft (fun a b => a ≤ b) (transRelRight (fun a b => a ≤ b) h₁ h₂) h₃ protected theorem addLtAddLeft {n m : Nat} (h : n < m) (k : Nat) : k + n < k + m := ltOfSuccLe (addSucc k n ▸ Nat.addLeAddLeft (succLeOfLt h) k) protected theorem addLtAddRight {n m : Nat} (h : n < m) (k : Nat) : n + k < m + k := Nat.addComm k m ▸ Nat.addComm k n ▸ Nat.addLtAddLeft h k protected theorem zeroLtOne : 0 < (1:Nat) := zeroLtSucc 0 theorem leOfLtSucc {m n : Nat} : m < succ n → m ≤ n := leOfSuccLeSucc theorem addLeAdd {a b c d : Nat} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := Nat.leTrans (Nat.addLeAddRight h₁ c) (Nat.addLeAddLeft h₂ b) theorem addLtAdd {a b c d : Nat} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := Nat.ltTrans (Nat.addLtAddRight h₁ c) (Nat.addLtAddLeft h₂ b) /- Basic theorems for comparing numerals -/ theorem natZeroEqZero : Nat.zero = 0 := rfl protected theorem oneNeZero : 1 ≠ (0 : Nat) := fun h => Nat.noConfusion h protected theorem zeroNeOne : 0 ≠ (1 : Nat) := fun h => Nat.noConfusion h theorem succNeZero (n : Nat) : succ n ≠ 0 := fun h => Nat.noConfusion h protected theorem bit0SuccEq (n : Nat) : bit0 (succ n) = succ (succ (bit0 n)) := show succ (succ n + n) = succ (succ (n + n)) from congrArg succ (succAdd n n) protected theorem zeroLtBit0 : ∀ {n : Nat}, n ≠ 0 → 0 < bit0 n | 0 h := absurd rfl h | (succ n) h := have h₁ : 0 < succ (succ (bit0 n)) from zeroLtSucc _; have h₂ : succ (succ (bit0 n)) = bit0 (succ n) from (Nat.bit0SuccEq n).symm; transRelLeft (fun a b => a < b) h₁ h₂ protected theorem zeroLtBit1 (n : Nat) : 0 < bit1 n := zeroLtSucc _ protected theorem bit0NeZero : ∀ {n : Nat}, n ≠ 0 → bit0 n ≠ 0 | 0 h := absurd rfl h | (n+1) h := suffices (n+1) + (n+1) ≠ 0 from this; suffices succ ((n+1) + n) ≠ 0 from this; fun h => Nat.noConfusion h protected theorem bit1NeZero (n : Nat) : bit1 n ≠ 0 := show succ (n + n) ≠ 0 from fun h => Nat.noConfusion h protected theorem bit1EqSuccBit0 (n : Nat) : bit1 n = succ (bit0 n) := rfl protected theorem bit1SuccEq (n : Nat) : bit1 (succ n) = succ (succ (bit1 n)) := Eq.trans (Nat.bit1EqSuccBit0 (succ n)) (congrArg succ (Nat.bit0SuccEq n)) protected theorem bit1NeOne : ∀ {n : Nat}, n ≠ 0 → bit1 n ≠ 1 | 0 h h1 := absurd rfl h | (n+1) h h1 := Nat.noConfusion h1 (fun h2 => absurd h2 (succNeZero _)) protected theorem bit0NeOne : ∀ (n : Nat), bit0 n ≠ 1 | 0 h := absurd h (Ne.symm Nat.oneNeZero) | (n+1) h := have h1 : succ (succ (n + n)) = 1 from succAdd n n ▸ h; Nat.noConfusion h1 (fun h2 => absurd h2 (succNeZero (n + n))) protected theorem addSelfNeOne : ∀ (n : Nat), n + n ≠ 1 | 0 h := Nat.noConfusion h | (n+1) h := have h1 : succ (succ (n + n)) = 1 from succAdd n n ▸ h; Nat.noConfusion h1 (fun h2 => absurd h2 (Nat.succNeZero (n + n))) protected theorem bit1NeBit0 : ∀ (n m : Nat), bit1 n ≠ bit0 m | 0 m h := absurd h (Ne.symm (Nat.addSelfNeOne m)) | (n+1) 0 h := have h1 : succ (bit0 (succ n)) = 0 from h; absurd h1 (Nat.succNeZero _) | (n+1) (m+1) h := have h1 : succ (succ (bit1 n)) = succ (succ (bit0 m)) from Nat.bit0SuccEq m ▸ Nat.bit1SuccEq n ▸ h; have h2 : bit1 n = bit0 m from Nat.noConfusion h1 (fun h2' => Nat.noConfusion h2' (fun h2'' => h2'')); absurd h2 (bit1NeBit0 n m) protected theorem bit0NeBit1 : ∀ (n m : Nat), bit0 n ≠ bit1 m := fun n m => Ne.symm (Nat.bit1NeBit0 m n) protected theorem bit0Inj : ∀ {n m : Nat}, bit0 n = bit0 m → n = m | 0 0 h := rfl | 0 (m+1) h := absurd h.symm (succNeZero _) | (n+1) 0 h := absurd h (succNeZero _) | (n+1) (m+1) h := have (n+1) + n = (m+1) + m from Nat.noConfusion h id; have n + (n+1) = m + (m+1) from Nat.addComm (m+1) m ▸ Nat.addComm (n+1) n ▸ this; have succ (n + n) = succ (m + m) from this; have n + n = m + m from Nat.noConfusion this id; have n = m from bit0Inj this; congrArg (fun a => a + 1) this protected theorem bit1Inj : ∀ {n m : Nat}, bit1 n = bit1 m → n = m := fun n m h => have succ (bit0 n) = succ (bit0 m) from Nat.bit1EqSuccBit0 n ▸ Nat.bit1EqSuccBit0 m ▸ h; have bit0 n = bit0 m from Nat.noConfusion this id; Nat.bit0Inj this protected theorem bit0Ne {n m : Nat} : n ≠ m → bit0 n ≠ bit0 m := fun h₁ h₂ => absurd (Nat.bit0Inj h₂) h₁ protected theorem bit1Ne {n m : Nat} : n ≠ m → bit1 n ≠ bit1 m := fun h₁ h₂ => absurd (Nat.bit1Inj h₂) h₁ protected theorem zeroNeBit0 {n : Nat} : n ≠ 0 → 0 ≠ bit0 n := fun h => Ne.symm (Nat.bit0NeZero h) protected theorem zeroNeBit1 (n : Nat) : 0 ≠ bit1 n := Ne.symm (Nat.bit1NeZero n) protected theorem oneNeBit0 (n : Nat) : 1 ≠ bit0 n := Ne.symm (Nat.bit0NeOne n) protected theorem oneNeBit1 {n : Nat} : n ≠ 0 → 1 ≠ bit1 n := fun h => Ne.symm (Nat.bit1NeOne h) protected theorem oneLtBit1 : ∀ {n : Nat}, n ≠ 0 → 1 < bit1 n | 0 h := absurd rfl h | (succ n) h := suffices succ 0 < succ (succ (bit1 n)) from (Nat.bit1SuccEq n).symm ▸ this; succLtSucc (zeroLtSucc _) protected theorem oneLtBit0 : ∀ {n : Nat}, n ≠ 0 → 1 < bit0 n | 0 h := absurd rfl h | (succ n) h := suffices succ 0 < succ (succ (bit0 n)) from (Nat.bit0SuccEq n).symm ▸ this; succLtSucc (zeroLtSucc _) protected theorem bit0Lt {n m : Nat} (h : n < m) : bit0 n < bit0 m := Nat.addLtAdd h h protected theorem bit1Lt {n m : Nat} (h : n < m) : bit1 n < bit1 m := succLtSucc (Nat.addLtAdd h h) protected theorem bit0LtBit1 {n m : Nat} (h : n ≤ m) : bit0 n < bit1 m := ltSuccOfLe (Nat.addLeAdd h h) protected theorem bit1LtBit0 : ∀ {n m : Nat}, n < m → bit1 n < bit0 m | n 0 h := absurd h (notLtZero _) | n (succ m) h := have n ≤ m from leOfLtSucc h; have succ (n + n) ≤ succ (m + m) from succLeSucc (addLeAdd this this); have succ (n + n) ≤ succ m + m from (succAdd m m).symm ▸ this; show succ (n + n) < succ (succ m + m) from ltSuccOfLe this protected theorem oneLeBit1 (n : Nat) : 1 ≤ bit1 n := show 1 ≤ succ (bit0 n) from succLeSucc (zeroLe (bit0 n)) protected theorem oneLeBit0 : ∀ (n : Nat), n ≠ 0 → 1 ≤ bit0 n | 0 h := absurd rfl h | (n+1) h := suffices 1 ≤ succ (succ (bit0 n)) from Eq.symm (Nat.bit0SuccEq n) ▸ this; succLeSucc (zeroLe (succ (bit0 n))) /- mul + order -/ theorem mulLeMulLeft {n m : Nat} (k : Nat) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ => have k * n + k * l = k * m from Nat.leftDistrib k n l ▸ hl.symm ▸ rfl; le.intro this theorem mulLeMulRight {n m : Nat} (k : Nat) (h : n ≤ m) : n * k ≤ m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ mulLeMulLeft k h protected theorem mulLeMul {n₁ m₁ n₂ m₂ : Nat} (h₁ : n₁ ≤ n₂) (h₂ : m₁ ≤ m₂) : n₁ * m₁ ≤ n₂ * m₂ := Nat.leTrans (mulLeMulRight _ h₁) (mulLeMulLeft _ h₂) protected theorem mulLtMulOfPosLeft {n m k : Nat} (h : n < m) (hk : k > 0) : k * n < k * m := Nat.ltOfLtOfLe (Nat.addLtAddLeft hk _) (Nat.mulSucc k n ▸ Nat.mulLeMulLeft k (succLeOfLt h)) protected theorem mulLtMulOfPosRight {n m k : Nat} (h : n < m) (hk : k > 0) : n * k < m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ Nat.mulLtMulOfPosLeft h hk protected theorem mulPos {n m : Nat} (ha : n > 0) (hb : m > 0) : n * m > 0 := have h : 0 * m < n * m from Nat.mulLtMulOfPosRight ha hb; Nat.zeroMul m ▸ h /- power -/ theorem powSucc (n m : Nat) : n^(succ m) = n^m * n := rfl theorem powZero (n : Nat) : n^0 = 1 := rfl theorem powLePowOfLeLeft {n m : Nat} (h : n ≤ m) : ∀ (i : Nat), n^i ≤ m^i | 0 := Nat.leRefl _ | (succ i) := Nat.mulLeMul (powLePowOfLeLeft i) h theorem powLePowOfLeRight {n : Nat} (hx : n > 0) {i : Nat} : ∀ {j}, i ≤ j → n^i ≤ n^j | 0 h := have i = 0 from eqZeroOfLeZero h; this.symm ▸ Nat.leRefl _ | (succ j) h := Or.elim (ltOrEqOrLeSucc h) (fun h => show n^i ≤ n^j * n from suffices n^i * 1 ≤ n^j * n from Nat.mulOne (n^i) ▸ this; Nat.mulLeMul (powLePowOfLeRight h) hx) (fun h => h.symm ▸ Nat.leRefl _) theorem posPowOfPos {n : Nat} (m : Nat) (h : 0 < n) : 0 < n^m := powLePowOfLeRight h (Nat.zeroLe _) /- Max -/ protected def max (n m : Nat) : Nat := if n ≤ m then m else n end Nat namespace Prod @[inline] def foldI {α : Type u} (f : Nat → α → α) (i : Nat × Nat) (a : α) : α := Nat.foldAux f i.2 (i.2 - i.1) a @[inline] def anyI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux f i.2 (i.2 - i.1) @[inline] def allI (f : Nat → Bool) (i : Nat × Nat) : Bool := !Nat.anyAux (fun a => !f a) i.2 (i.2 - i.1) end Prod
f611f388e0771782a5ea4e78fb4859a434521f6c
4be1078262cf85c68a22c7edefe284bf4797327f
/mathlibtools/decls.lean
744e6cd317c58ede0fac1702d48caae8c93812af
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib-tools
7f2c67208c94e5c682f67635c73edde38b0e0a80
3460136245ba77db0e4765d1a0a32b34fce05f40
refs/heads/master
1,687,244,484,037
1,594,279,445,000
1,594,279,445,000
278,293,881
0
0
Apache-2.0
1,594,279,557,000
1,594,279,556,000
null
UTF-8
Lean
false
false
1,439
lean
import meta.expr system.io open tactic declaration environment io io.fs (put_str_ln close) -- The next instance is there to prevent PyYAML trying to be too smart meta def my_name_to_string : has_to_string name := ⟨λ n, "\"" ++ to_string n ++ "\""⟩ local attribute [instance] my_name_to_string meta def pos_line (p : option pos) : string := match p with | some x := to_string x.line | _ := "" end meta def file_name (p : option string) : string := match p with | some x := x | _ := "" end meta def print_item_crawl (env : environment) (h : handle) (decl : declaration) : io unit := let name := decl.to_name in do put_str_ln h ((to_string name) ++ ":"), put_str_ln h (" File: " ++ file_name (env.decl_olean name)), put_str_ln h (" Line: " ++ pos_line (env.decl_pos name)) /-- itersplit l n will cut a list l into 2^n pieces (not preserving order) -/ meta def itersplit {α} : list α → ℕ → list (list α) | l 0 := [l] | l 1 := let (l1, l2) := l.split in [l1, l2] | l (k+2) := let (l1, l2) := l.split in itersplit l1 (k+1) ++ itersplit l2 (k+1) meta def main : io unit := do curr_env ← run_tactic get_env, h ← mk_file_handle "decls.yaml" mode.write, let decls := curr_env.fold [] list.cons, let filtered_decls := decls.filter (λ x, not (to_name x).is_internal), let pieces := itersplit filtered_decls 3, pieces.mmap' (λ l, l.mmap' (print_item_crawl curr_env h)), close h
e17cc0470958540a74bc58ee49ed11df33a22df5
3bdd27ffdff3ffa22d4bb010eba695afcc96bc4a
/src/combinatorics/simplicial_complex/to_move/hahn_banach.lean
4a40f218b786fa6c3f2733e5633484dcb1c4e431
[]
no_license
mmasdeu/brouwerfixedpoint
684d712c982c6a8b258b4e2c6b2eab923f2f1289
548270f79ecf12d7e20a256806ccb9fcf57b87e2
refs/heads/main
1,690,539,793,996
1,631,801,831,000
1,631,801,831,000
368,139,809
4
3
null
1,624,453,250,000
1,621,246,034,000
Lean
UTF-8
Lean
false
false
1,596
lean
/- Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import algebra.module.linear_map import analysis.convex.topology import analysis.normed_space.operator_norm import linear_algebra.affine_space.finite_dimensional open_locale classical affine big_operators open set --TODO: Generalise to LCTVS variables {E : Type*} [normed_group E] [normed_space ℝ E] {x : E} {A B C : set E} {X : finset E} theorem geometric_hahn_banach_compact_closed {A B : set E} (hA₁ : convex A) (hA₂ : is_compact A) (hB₁ : convex B) (hB₂ : is_closed B) (disj : disjoint A B) : ∃ (f : E →L[ℝ] ℝ) (s t : ℝ), s < t ∧ (∀ b ∈ B, f b < s) ∧ (∀ a ∈ A, t < f a) := sorry theorem geometric_hahn_banach_closed_point {A : set E} {x : E} (hA₁ : convex A) (hA₂ : is_closed A) (disj : x ∉ A) : ∃ (f : E →L[ℝ] ℝ) (s : ℝ), (∀ a ∈ A, f a < s) ∧ s < f x := sorry theorem geometric_hahn_banach_open_point {A : set E} {x : E} (hA₁ : convex A) (hA₂ : is_open A) (disj : x ∉ A) : ∃ (f : E →L[ℝ] ℝ), (∀ a ∈ A, f a < f x) := sorry theorem geometric_hahn_banach_point_open {x : E} {B : set E} (hB₁ : convex B) (hB₂ : is_open B) (disj : x ∉ B) : ∃ (f : E →L[ℝ] ℝ), (∀ b ∈ B, f x < f b) := let ⟨f, hf⟩ := geometric_hahn_banach_open_point hB₁ hB₂ disj in ⟨-f, by simpa⟩ theorem geometric_hahn_banach_point_point {x y : E} (hxy : x ≠ y) : ∃ (f : E →L[ℝ] ℝ), f x < f y := sorry
75b3df1f0db0d6135adc0b7bce74adf901a7502b
c777c32c8e484e195053731103c5e52af26a25d1
/src/field_theory/finite/galois_field.lean
ac9250227e85cae57e988f55cf5fa4a1e5a75015
[ "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
8,622
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Alex J. Best, Johan Commelin, Eric Rodriguez, Ruben Van de Velde -/ import algebra.char_p.algebra import data.zmod.algebra import field_theory.finite.basic import field_theory.galois /-! # Galois fields If `p` is a prime number, and `n` a natural number, then `galois_field p n` is defined as the splitting field of `X^(p^n) - X` over `zmod p`. It is a finite field with `p ^ n` elements. ## Main definition * `galois_field p n` is a field with `p ^ n` elements ## Main Results - `galois_field.alg_equiv_galois_field`: Any finite field is isomorphic to some Galois field - `finite_field.alg_equiv_of_card_eq`: Uniqueness of finite fields : algebra isomorphism - `finite_field.ring_equiv_of_card_eq`: Uniqueness of finite fields : ring isomorphism -/ noncomputable theory open polynomial open_locale polynomial lemma galois_poly_separable {K : Type*} [field K] (p q : ℕ) [char_p K p] (h : p ∣ q) : separable (X ^ q - X : K[X]) := begin use [1, (X ^ q - X - 1)], rw [← char_p.cast_eq_zero_iff K[X] p] at h, rw [derivative_sub, derivative_X_pow, derivative_X, C_eq_nat_cast, h], ring, end /-- A finite field with `p ^ n` elements. Every field with the same cardinality is (non-canonically) isomorphic to this field. -/ @[derive field] def galois_field (p : ℕ) [fact p.prime] (n : ℕ) := splitting_field (X^(p^n) - X : (zmod p)[X]) instance : inhabited (@galois_field 2 (fact.mk nat.prime_two) 1) := ⟨37⟩ namespace galois_field variables (p : ℕ) [fact p.prime] (n : ℕ) instance : algebra (zmod p) (galois_field p n) := splitting_field.algebra _ instance : is_splitting_field (zmod p) (galois_field p n) (X^(p^n) - X) := polynomial.is_splitting_field.splitting_field _ instance : char_p (galois_field p n) p := (algebra.char_p_iff (zmod p) (galois_field p n) p).mp (by apply_instance) instance : fintype (galois_field p n) := by {dsimp only [galois_field], exact finite_dimensional.fintype_of_fintype (zmod p) (galois_field p n) } lemma finrank {n} (h : n ≠ 0) : finite_dimensional.finrank (zmod p) (galois_field p n) = n := begin set g_poly := (X^(p^n) - X : (zmod p)[X]), have hp : 1 < p := (fact.out (nat.prime p)).one_lt, have aux : g_poly ≠ 0 := finite_field.X_pow_card_pow_sub_X_ne_zero _ h hp, have key : fintype.card ((g_poly).root_set (galois_field p n)) = (g_poly).nat_degree := card_root_set_eq_nat_degree (galois_poly_separable p _ (dvd_pow (dvd_refl p) h)) (splitting_field.splits g_poly), have nat_degree_eq : (g_poly).nat_degree = p ^ n := finite_field.X_pow_card_pow_sub_X_nat_degree_eq _ h hp, rw nat_degree_eq at key, suffices : (g_poly).root_set (galois_field p n) = set.univ, { simp_rw [this, ←fintype.of_equiv_card (equiv.set.univ _)] at key, rw [@card_eq_pow_finrank (zmod p), zmod.card] at key, exact nat.pow_right_injective ((nat.prime.one_lt' p).out) key }, rw set.eq_univ_iff_forall, suffices : ∀ x (hx : x ∈ (⊤ : subalgebra (zmod p) (galois_field p n))), x ∈ (X ^ p ^ n - X : (zmod p)[X]).root_set (galois_field p n), { simpa, }, rw ← splitting_field.adjoin_root_set, simp_rw algebra.mem_adjoin_iff, intros x hx, -- We discharge the `p = 0` separately, to avoid typeclass issues on `zmod p`. unfreezingI { cases p, cases hp, }, apply subring.closure_induction hx; clear_dependent x; simp_rw mem_root_set_of_ne aux, { rintros x (⟨r, rfl⟩ | hx), { simp only [aeval_X_pow, aeval_X, alg_hom.map_sub], rw [← map_pow, zmod.pow_card_pow, sub_self], }, { dsimp only [galois_field] at hx, rwa mem_root_set_of_ne aux at hx, apply_instance }, }, { dsimp only [g_poly], rw [← coeff_zero_eq_aeval_zero'], simp only [coeff_X_pow, coeff_X_zero, sub_zero, _root_.map_eq_zero, ite_eq_right_iff, one_ne_zero, coeff_sub], intro hn, exact nat.not_lt_zero 1 (pow_eq_zero hn.symm ▸ hp), }, { simp, }, { simp only [aeval_X_pow, aeval_X, alg_hom.map_sub, add_pow_char_pow, sub_eq_zero], intros x y hx hy, rw [hx, hy], }, { intros x hx, simp only [sub_eq_zero, aeval_X_pow, aeval_X, alg_hom.map_sub, sub_neg_eq_add] at *, rw [neg_pow, hx, char_p.neg_one_pow_char_pow], simp, }, { simp only [aeval_X_pow, aeval_X, alg_hom.map_sub, mul_pow, sub_eq_zero], intros x y hx hy, rw [hx, hy], }, end lemma card (h : n ≠ 0) : fintype.card (galois_field p n) = p ^ n := begin let b := is_noetherian.finset_basis (zmod p) (galois_field p n), rw [module.card_fintype b, ← finite_dimensional.finrank_eq_card_basis b, zmod.card, finrank p h], end theorem splits_zmod_X_pow_sub_X : splits (ring_hom.id (zmod p)) (X ^ p - X) := begin have hp : 1 < p := (fact.out (nat.prime p)).one_lt, have h1 : roots (X ^ p - X : (zmod p)[X]) = finset.univ.val, { convert finite_field.roots_X_pow_card_sub_X _, exact (zmod.card p).symm }, have h2 := finite_field.X_pow_card_sub_X_nat_degree_eq (zmod p) hp, -- We discharge the `p = 0` separately, to avoid typeclass issues on `zmod p`. unfreezingI { cases p, cases hp, }, rw [splits_iff_card_roots, h1, ←finset.card_def, finset.card_univ, h2, zmod.card], end /-- A Galois field with exponent 1 is equivalent to `zmod` -/ def equiv_zmod_p : galois_field p 1 ≃ₐ[zmod p] (zmod p) := have h : (X ^ p ^ 1 : (zmod p)[X]) = X ^ (fintype.card (zmod p)), by rw [pow_one, zmod.card p], have inst : is_splitting_field (zmod p) (zmod p) (X ^ p ^ 1 - X), by { rw h, apply_instance }, by exactI (is_splitting_field.alg_equiv (zmod p) (X ^ (p ^ 1) - X : (zmod p)[X])).symm variables {K : Type*} [field K] [fintype K] [algebra (zmod p) K] theorem splits_X_pow_card_sub_X : splits (algebra_map (zmod p) K) (X ^ fintype.card K - X) := (finite_field.has_sub.sub.polynomial.is_splitting_field K (zmod p)).splits lemma is_splitting_field_of_card_eq (h : fintype.card K = p ^ n) : is_splitting_field (zmod p) K (X ^ (p ^ n) - X) := h ▸ finite_field.has_sub.sub.polynomial.is_splitting_field K (zmod p) @[priority 100] instance {K K' : Type*} [field K] [field K'] [finite K'] [algebra K K'] : is_galois K K' := begin casesI nonempty_fintype K', obtain ⟨p, hp⟩ := char_p.exists K, haveI : char_p K p := hp, haveI : char_p K' p := char_p_of_injective_algebra_map' K K' p, exact is_galois.of_separable_splitting_field (galois_poly_separable p (fintype.card K') (let ⟨n, hp, hn⟩ := finite_field.card K' p in hn.symm ▸ dvd_pow_self p n.ne_zero)), end /-- Any finite field is (possibly non canonically) isomorphic to some Galois field. -/ def alg_equiv_galois_field (h : fintype.card K = p ^ n) : K ≃ₐ[zmod p] galois_field p n := by haveI := is_splitting_field_of_card_eq _ _ h; exact is_splitting_field.alg_equiv _ _ end galois_field namespace finite_field variables {K : Type*} [field K] [fintype K] {K' : Type*} [field K'] [fintype K'] /-- Uniqueness of finite fields: Any two finite fields of the same cardinality are (possibly non canonically) isomorphic-/ def alg_equiv_of_card_eq (p : ℕ) [fact p.prime] [algebra (zmod p) K] [algebra (zmod p) K'] (hKK' : fintype.card K = fintype.card K') : K ≃ₐ[zmod p] K' := begin haveI : char_p K p, { rw ← algebra.char_p_iff (zmod p) K p, exact zmod.char_p p, }, haveI : char_p K' p, { rw ← algebra.char_p_iff (zmod p) K' p, exact zmod.char_p p, }, choose n a hK using finite_field.card K p, choose n' a' hK' using finite_field.card K' p, rw [hK,hK'] at hKK', have hGalK := galois_field.alg_equiv_galois_field p n hK, have hK'Gal := (galois_field.alg_equiv_galois_field p n' hK').symm, rw (nat.pow_right_injective (fact.out (nat.prime p)).one_lt hKK') at *, use alg_equiv.trans hGalK hK'Gal, end /-- Uniqueness of finite fields: Any two finite fields of the same cardinality are (possibly non canonically) isomorphic-/ def ring_equiv_of_card_eq (hKK' : fintype.card K = fintype.card K') : K ≃+* K' := begin choose p _char_p_K using char_p.exists K, choose p' _char_p'_K' using char_p.exists K', resetI, choose n hp hK using finite_field.card K p, choose n' hp' hK' using finite_field.card K' p', have hpp' : p = p', -- := eq_prime_of_eq_prime_pow { by_contra hne, have h2 := nat.coprime_pow_primes n n' hp hp' hne, rw [(eq.congr hK hK').mp hKK', nat.coprime_self, pow_eq_one_iff (pnat.ne_zero n')] at h2, exact nat.prime.ne_one hp' h2, all_goals {apply_instance}, }, rw ← hpp' at *, haveI := fact_iff.2 hp, exact alg_equiv_of_card_eq p hKK', end end finite_field
fded5da883027759bf7ea4720f0ae744bbfe9d21
0c1546a496eccfb56620165cad015f88d56190c5
/tests/lean/run/match_pattern1.lean
c15a050e42abc3c977255078668eef5ba3752082
[ "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
495
lean
open tactic expr set_option pp.all true example (a b c x y : nat) (H : nat.add (nat.add x y) y = 0) : true := by do a ← get_local `a, b ← get_local `b, c ← get_local `c, nat_add : expr ← mk_const `nat.add, p : pattern ← mk_pattern [] [a, b] (app_of_list nat_add [a, b]) [app_of_list nat_add [b, a], a, b], trace (pattern.output p), H ← get_local `H >>= infer_type, lhs_rhs ← match_eq H, out ← match_pattern p (prod.fst lhs_rhs), trace out, constructor
721cf795d157643b2d0709ae0ccce6cb7e4e40ec
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Lean/Structure.lean
c168ffb1447addbc0ccc9e01f7fb18a2d27c424f
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,674
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 Helper functions for retrieving structure information. -/ prelude import Init.Lean.Environment import Init.Lean.ProjFns namespace Lean /-- Return true iff `constName` is the a non-recursive inductive datatype that has only one constructor. -/ def isStructureLike (env : Environment) (constName : Name) : Bool := match env.find? constName with | some (ConstantInfo.inductInfo { isRec := false, ctors := [ctor], .. }) => true | _ => false /-- We mark subobject fields by prefixing them with "_" in the structure's intro rule. -/ def mkInternalSubobjectFieldName (fieldName : Name) : Name := fieldName.appendBefore "_" def isInternalSubobjectFieldName : Name → Bool | Name.str _ s _ => s.length > 0 && s.get 0 == '_' | _ => false def deinternalizeFieldName : Name → Name | n@(Name.str p s _) => if s.length > 0 && s.get 0 == '_' then mkNameStr p (s.drop 1) else n | n => n def getStructureCtor (env : Environment) (constName : Name) : ConstructorVal := match env.find? constName with | some (ConstantInfo.inductInfo { nparams := nparams, isRec := false, ctors := [ctorName], .. }) => match env.find? ctorName with | some (ConstantInfo.ctorInfo val) => val | _ => panic! "ill-formed environment" | _ => panic! "structure expected" private def getStructureFieldsAux (nparams : Nat) : Nat → Expr → Array Name → Array Name | i, Expr.forallE n d b _, fieldNames => if i < nparams then getStructureFieldsAux (i+1) b fieldNames else getStructureFieldsAux (i+1) b (fieldNames.push $ deinternalizeFieldName n) | _, _, fieldNames => fieldNames def getStructureFields (env : Environment) (structName : Name) : Array Name := let ctor := getStructureCtor env structName; getStructureFieldsAux ctor.nparams 0 ctor.type #[] private def isSubobjectFieldAux (nparams : Nat) (target : Name) : Nat → Expr → Option Name | i, Expr.forallE n d b _ => if i < nparams then isSubobjectFieldAux (i+1) b else if n == target then match d.getAppFn with | Expr.const parentStructName _ _ => some parentStructName | _ => panic! "ill-formed structure" else isSubobjectFieldAux (i+1) b | _, _ => none /-- If `fieldName` represents the relation to a parent structure `S`, return `S` -/ def isSubobjectField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := let ctor := getStructureCtor env structName; isSubobjectFieldAux ctor.nparams (mkInternalSubobjectFieldName fieldName) 0 ctor.type /-- Return immediate parent structures -/ def getParentStructures (env : Environment) (structName : Name) : Array Name := let fieldNames := getStructureFields env structName; fieldNames.foldl (fun (acc : Array Name) fieldName => match isSubobjectField? env structName fieldName with | some parentStructName => acc.push parentStructName | none => acc) #[] /-- `findField? env S fname`. If `fname` is defined in a parent `S'` of `S`, return `S'` -/ partial def findField? (env : Environment) : Name → Name → Option Name | structName, fieldName => if (getStructureFields env structName).contains fieldName then some structName else (getParentStructures env structName).find? $ fun parentStructName => findField? parentStructName fieldName private partial def getStructureFieldsFlattenedAux (env : Environment) : Name → Array Name → Array Name | structName, fullNames => (getStructureFields env structName).foldl (fun (fullNames : Array Name) (fieldName : Name) => let fullNames := fullNames.push fieldName; match isSubobjectField? env structName fieldName with | some parentStructName => getStructureFieldsFlattenedAux parentStructName fullNames | none => fullNames) fullNames def getStructureFieldsFlattened (env : Environment) (structName : Name) : Array Name := getStructureFieldsFlattenedAux env structName #[] private def hasProjFn (env : Environment) (structName : Name) (nparams : Nat) : Nat → Expr → Bool | i, Expr.forallE n d b _ => if i < nparams then hasProjFn (i+1) b else let fullFieldName := structName ++ deinternalizeFieldName n; env.isProjectionFn fullFieldName | _, _ => false /-- Return true if `constName` is the name of an inductive datatype created using the `structure` or `class` commands. We perform the check by testing whether auxiliary projection functions have been created. -/ def isStructure (env : Environment) (constName : Name) : Bool := if isStructureLike env constName then let ctor := getStructureCtor env constName; hasProjFn env constName ctor.nparams 0 ctor.type else false partial def getPathToBaseStructureAux (env : Environment) (baseStructName : Name) : Name → List Name → Option (List Name) | structName, path => if baseStructName == structName then some path.reverse else let fieldNames := getStructureFields env structName; fieldNames.find? $ fun fieldName => match isSubobjectField? env structName fieldName with | none => none | some parentStructName => getPathToBaseStructureAux parentStructName ((structName ++ fieldName) :: path) /-- If `baseStructName` is an ancestor structure for `structName`, then return a sequence of projection functions to go from `structName` to `baseStructName`. -/ def getPathToBaseStructure? (env : Environment) (baseStructName : Name) (structName : Name) : Option (List Name) := getPathToBaseStructureAux env baseStructName structName [] end Lean
8ba349c8cd15497f43d469f0235041fb1dfca42c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/euclidean_domain/instances.lean
994324f38030dfcd43aed11ad28a40ae54160855
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,105
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro -/ import algebra.euclidean_domain.defs import algebra.field.defs import algebra.group_with_zero.units.lemmas import data.nat.order.basic import data.int.order.basic /-! # Instances for Euclidean domains > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. * `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain. * `field.to_euclidean_domain`: shows that any field is a Euclidean domain. -/ instance int.euclidean_domain : euclidean_domain ℤ := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), quotient_zero := int.div_zero, remainder := (%), quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), int.coe_nat_abs], exact int.mod_lt _ b0 }, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by {rw [← mul_one a.nat_abs, int.nat_abs_mul], exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }, .. int.comm_ring, .. int.nontrivial } @[priority 100] -- see Note [lower instance priority] instance field.to_euclidean_domain {K : Type*} [field K] : euclidean_domain K := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), remainder := λ a b, a - a * b / b, quotient_zero := div_zero, quotient_mul_add_remainder_eq := λ a b, by { classical, by_cases b = 0; simp [h, mul_div_cancel'] }, r := λ a b, a = 0 ∧ b ≠ 0, r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩, acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb, remainder_lt := λ a b hnb, by simp [hnb], mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb, .. ‹field K› }
e71f0d1df1272914965a59ef7143aa7594636b69
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/category/Top/opens.lean
3ee313d55943690f7bed4de92727e1d88788a734
[ "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
10,695
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.category.preorder import category_theory.eq_to_hom import topology.category.Top.epi_mono import topology.sets.opens /-! # The category of open sets in a topological space. We define `to_Top : opens X ⥤ Top` and `map (f : X ⟶ Y) : opens Y ⥤ opens X`, given by taking preimages of open sets. Unfortunately `opens` isn't (usefully) a functor `Top ⥤ Cat`. (One can in fact define such a functor, but using it results in unresolvable `eq.rec` terms in goals.) Really it's a 2-functor from (spaces, continuous functions, equalities) to (categories, functors, natural isomorphisms). We don't attempt to set up the full theory here, but do provide the natural isomorphisms `map_id : map (𝟙 X) ≅ 𝟭 (opens X)` and `map_comp : map (f ≫ g) ≅ map g ⋙ map f`. Beyond that, there's a collection of simp lemmas for working with these constructions. -/ open category_theory open topological_space open opposite universe u namespace topological_space.opens variables {X Y Z : Top.{u}} /-! Since `opens X` has a partial order, it automatically receives a `category` instance. Unfortunately, because we do not allow morphisms in `Prop`, the morphisms `U ⟶ V` are not just proofs `U ≤ V`, but rather `ulift (plift (U ≤ V))`. -/ instance opens_hom_has_coe_to_fun {U V : opens X} : has_coe_to_fun (U ⟶ V) (λ f, U → V) := ⟨λ f x, ⟨x, f.le x.2⟩⟩ /-! We now construct as morphisms various inclusions of open sets. -/ -- This is tedious, but necessary because we decided not to allow Prop as morphisms in a category... /-- The inclusion `U ⊓ V ⟶ U` as a morphism in the category of open sets. -/ def inf_le_left (U V : opens X) : U ⊓ V ⟶ U := inf_le_left.hom /-- The inclusion `U ⊓ V ⟶ V` as a morphism in the category of open sets. -/ def inf_le_right (U V : opens X) : U ⊓ V ⟶ V := inf_le_right.hom /-- The inclusion `U i ⟶ supr U` as a morphism in the category of open sets. -/ def le_supr {ι : Type*} (U : ι → opens X) (i : ι) : U i ⟶ supr U := (le_supr U i).hom /-- The inclusion `⊥ ⟶ U` as a morphism in the category of open sets. -/ def bot_le (U : opens X) : ⊥ ⟶ U := bot_le.hom /-- The inclusion `U ⟶ ⊤` as a morphism in the category of open sets. -/ def le_top (U : opens X) : U ⟶ ⊤ := le_top.hom -- We do not mark this as a simp lemma because it breaks open `x`. -- Nevertheless, it is useful in `sheaf_of_functions`. lemma inf_le_left_apply (U V : opens X) (x) : (inf_le_left U V) x = ⟨x.1, (@_root_.inf_le_left _ _ U V : _ ≤ _) x.2⟩ := rfl @[simp] lemma inf_le_left_apply_mk (U V : opens X) (x) (m) : (inf_le_left U V) ⟨x, m⟩ = ⟨x, (@_root_.inf_le_left _ _ U V : _ ≤ _) m⟩ := rfl @[simp] lemma le_supr_apply_mk {ι : Type*} (U : ι → opens X) (i : ι) (x) (m) : (le_supr U i) ⟨x, m⟩ = ⟨x, (_root_.le_supr U i : _) m⟩ := rfl /-- The functor from open sets in `X` to `Top`, realising each open set as a topological space itself. -/ def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.le x.2⟩, (embedding.continuous_iff embedding_subtype_coe).2 continuous_induced_dom⟩ } @[simp] lemma to_Top_map (X : Top.{u}) {U V : opens X} {f : U ⟶ V} {x} {h} : ((to_Top X).map f) ⟨x, h⟩ = ⟨x, f.le h⟩ := rfl /-- The inclusion map from an open subset to the whole space, as a morphism in `Top`. -/ @[simps] def inclusion {X : Top.{u}} (U : opens X) : (to_Top X).obj U ⟶ X := { to_fun := _, continuous_to_fun := continuous_subtype_coe } lemma open_embedding {X : Top.{u}} (U : opens X) : open_embedding (inclusion U) := is_open.open_embedding_subtype_coe U.2 /-- The inclusion of the top open subset (i.e. the whole space) is an isomorphism. -/ def inclusion_top_iso (X : Top.{u}) : (to_Top X).obj ⊤ ≅ X := { hom := inclusion ⊤, inv := ⟨λ x, ⟨x, trivial⟩, continuous_def.2 $ λ U ⟨S, hS, hSU⟩, hSU ▸ hS⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f ⁻¹' U.val, U.property.preimage f.continuous ⟩, map := λ U V i, ⟨ ⟨ λ x h, i.le h ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟶ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨f ⁻¹' U, p.preimage f.continuous⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := let ⟨_,_⟩ := U in rfl @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := let ⟨_,_⟩ := U.unop in rfl @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp /-- The inclusion `U ⟶ (map f).obj ⊤` as a morphism in the category of open sets. -/ def le_map_top (f : X ⟶ Y) (U : opens X) : U ⟶ (map f).obj ⊤ := le_top U @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := rfl @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_map (f : X ⟶ Y) (g : Y ⟶ Z) {U V} (i : U ⟶ V) : (map (f ≫ g)).map i = (map f).map ((map g).map i) := rfl @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := rfl @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := rfl lemma map_supr (f : X ⟶ Y) {ι : Type*} (U : ι → opens Y) : (map f).obj (supr U) = supr ((map f).obj ∘ U) := begin apply subtype.eq, rw [supr_def, supr_def, map_obj], dsimp, rw set.preimage_Union, refl, end section variable (X) /-- The functor `opens X ⥤ opens X` given by taking preimages under the identity function is naturally isomorphic to the identity functor. -/ @[simps] def map_id : map (𝟙 X) ≅ 𝟭 (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } lemma map_id_eq : map (𝟙 X) = 𝟭 (opens X) := by { unfold map, congr, ext, refl, ext } end /-- The natural isomorphism between taking preimages under `f ≫ g`, and the composite of taking preimages under `g`, then preimages under `f`. -/ @[simps] def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } lemma map_comp_eq (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) = map g ⋙ map f := rfl /-- If two continuous maps `f g : X ⟶ Y` are equal, then the functors `opens Y ⥤ opens X` they induce are isomorphic. -/ -- We could make `f g` implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) lemma map_eq (f g : X ⟶ Y) (h : f = g) : map f = map g := by { unfold map, congr, ext, rw h, rw h, assumption' } @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl /-- A homeomorphism of spaces gives an equivalence of categories of open sets. -/ @[simps] def map_map_iso {X Y : Top.{u}} (H : X ≅ Y) : opens Y ≌ opens X := { functor := map H.hom, inverse := map H.inv, unit_iso := nat_iso.of_components (λ U, eq_to_iso (by simp [map, set.preimage_preimage])) (by { intros _ _ _, simp }), counit_iso := nat_iso.of_components (λ U, eq_to_iso (by simp [map, set.preimage_preimage])) (by { intros _ _ _, simp }) } end topological_space.opens /-- An open map `f : X ⟶ Y` induces a functor `opens X ⥤ opens Y`. -/ @[simps] def is_open_map.functor {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) : opens X ⥤ opens Y := { obj := λ U, ⟨f '' U, hf U U.2⟩, map := λ U V h, ⟨⟨set.image_subset _ h.down.down⟩⟩ } /-- An open map `f : X ⟶ Y` induces an adjunction between `opens X` and `opens Y`. -/ def is_open_map.adjunction {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) : adjunction hf.functor (topological_space.opens.map f) := adjunction.mk_of_unit_counit { unit := { app := λ U, hom_of_le $ λ x hxU, ⟨x, hxU, rfl⟩ }, counit := { app := λ V, hom_of_le $ λ y ⟨x, hfxV, hxy⟩, hxy ▸ hfxV } } instance is_open_map.functor_full_of_mono {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) [H : mono f] : full hf.functor := { preimage := λ U V i, hom_of_le (λ x hx, by { obtain ⟨y, hy, eq⟩ := i.le ⟨x, hx, rfl⟩, exact (Top.mono_iff_injective f).mp H eq ▸ hy }) } instance is_open_map.functor_faithful {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) : faithful hf.functor := {} namespace topological_space.opens open topological_space @[simp] lemma open_embedding_obj_top {X : Top} (U : opens X) : U.open_embedding.is_open_map.functor.obj ⊤ = U := by { ext1, exact set.image_univ.trans subtype.range_coe } @[simp] lemma inclusion_map_eq_top {X : Top} (U : opens X) : (opens.map U.inclusion).obj U = ⊤ := by { ext1, exact subtype.coe_preimage_self _ } @[simp] lemma adjunction_counit_app_self {X : Top} (U : opens X) : U.open_embedding.is_open_map.adjunction.counit.app U = eq_to_hom (by simp) := by ext lemma inclusion_top_functor (X : Top) : (@opens.open_embedding X ⊤).is_open_map.functor = map (inclusion_top_iso X).inv := begin apply functor.hext, intro, abstract obj_eq { ext, exact ⟨ λ ⟨⟨_,_⟩,h,rfl⟩, h, λ h, ⟨⟨x,trivial⟩,h,rfl⟩ ⟩ }, intros, apply subsingleton.helim, congr' 1, iterate 2 {apply inclusion_top_functor.obj_eq}, end lemma functor_obj_map_obj {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) (U : opens Y) : hf.functor.obj ((opens.map f).obj U) = hf.functor.obj ⊤ ⊓ U := begin ext, split, { rintros ⟨x, hx, rfl⟩, exact ⟨⟨x, trivial, rfl⟩, hx⟩ }, { rintros ⟨⟨x, -, rfl⟩, hx⟩, exact ⟨x, hx, rfl⟩ } end end topological_space.opens
3ad2ae470b6f0a0ab56c57687116ccbe00b1e597
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/vrel/type.lean
ca9ae1b523a88cb01346712d9620989e286999d0
[]
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
984
lean
/- This file contains the `vrel` type, a binary relation on finite sets of variable names. -/ import vname namespace acie ----------------------------------------------------------------- variables {V : Type} [decidable_eq V] -- Type of variable names variables {vs : Type → Type} [vset vs V] -- Type of variable name sets variables {X Y : vs V} -- Variable name sets -- `vrel` is the type of a binary relation on variable name sets. @[reducible] def vrel (X Y : vs V) : Type := ν∈ X → ν∈ Y → Prop -- Notation for `vrel`. infixl ` ×ν `:35 := vrel -- Notation for membership of an `vrel`. -- We use type ascription (R : vrel _ _) to improve elaboration/type inference. notation `⟪` x `, ` y `⟫` ` ∈ν ` R:50 := (R : vrel _ _) x y -- Lift a function on finite name sets to a `vrel` @[reducible] protected def vrel.lift (F : X →ν Y) : X ×ν Y := λ x y, (F x).1 = y.1 end /- namespace -/ acie -------------------------------------------------------
2ec07f9d87f91900b7fe906ce7521b12706c2b3c
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/algebra/order_functions.lean
89617cdeab12a9027330d2cd6edb9db21e737b87
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,325
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.ordered_group order.lattice open lattice universes u v variables {α : Type u} {β : Type v} section variables [decidable_linear_order α] [decidable_linear_order β] {f : α → β} {a b c d : α} -- translate from lattices to linear orders (sup → max, inf → min) lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_left_of_le : a ≤ b → a ≤ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≤ c → a ≤ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≤ c → min a b ≤ c := inf_le_left_of_le lemma min_le_right_of_le : b ≤ c → min a b ≤ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right instance max_idem : is_idempotent α max := by apply_instance instance min_idem : is_idempotent α min := by apply_instance lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := have a ≤ b → (a ≤ c ∨ b ≤ c ↔ a ≤ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≤ a → (a ≤ c ∨ b ≤ c ↔ b ≤ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp [*, min_eq_left, min_eq_right] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := have b ≤ c → (a ≤ b ∨ a ≤ c ↔ a ≤ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≤ b → (a ≤ b ∨ a ≤ c ↔ a ≤ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp [*, max_eq_left, max_eq_right] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(≥), le_max_iff, not_or_distrib] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(≥), min_le_iff, not_or_distrib] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(≥), max_le_iff, not_and_distrib] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(≥), le_min_iff, not_and_distrib] theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma max_distrib_of_monotone (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [max_eq_right, max_eq_left, h, hf h] lemma min_distrib_of_monotone (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [min_eq_right, min_eq_left, h, hf h] theorem min_choice (a b : α) : min a b = a ∨ min a b = b := by by_cases h : a ≤ b; simp [min, h] theorem max_choice (a b : α) : max a b = a ∨ max a b = b := by by_cases h : a ≤ b; simp [max, h] end section decidable_linear_ordered_comm_group variables [decidable_linear_ordered_comm_group α] {a b c : α} attribute [simp] abs_zero abs_neg def abs_add := @abs_add_le_abs_add_abs theorem abs_le : abs a ≤ b ↔ - b ≤ a ∧ a ≤ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_le_of_le_of_neg_le h₂ $ neg_le_of_neg_le h₁⟩ lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, h₂⟩, abs_lt_of_lt_of_neg_lt h₂ $ neg_lt_of_neg_lt h₁⟩ lemma abs_sub_le_iff : abs (a - b) ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] def sub_abs_le_abs_sub := @abs_sub_abs_le_abs_sub lemma abs_abs_sub_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, λ e, e.symm ▸ abs_zero⟩ lemma abs_pos_iff {a : α} : 0 < abs a ↔ a ≠ 0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : abs b ≤ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) end decidable_linear_ordered_comm_group section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring α] {a b c d : α} @[simp] lemma abs_one : abs (1 : α) = 1 := abs_of_pos zero_lt_one lemma monotone_mul_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma mul_max_of_nonneg (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) := max_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma mul_min_of_nonneg (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) := min_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma max_mul_mul_le_max_mul_max (b c : α) (ha : 0 ≤ a) (hd: 0 ≤ d) : max (a * b) (d * c) ≤ max a c * max d b := have ba : b * a ≤ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≤ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
53a8e36659acc992c7d14be228e067c3c7c4ddac
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter5/5.11.lean
287856ce1e28a66d82fc0c93e25750b843b54a48
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,018
lean
-- 5.1 Proof by Contradiction (and Classical Reasoning) continued -- Proof by contradiction is the same as the principle: ¬¬ A ↔ A. From right to -- left, this holds intuitionistically: open classical variables A B: Prop example: A -> ¬¬ A := assume ha: A, show ¬¬ A, from assume hNotA: ¬ A, show false, from hNotA ha -- ..., or more succictly, example: A -> ¬¬ A := assume hA: A, show ¬¬ A, from assume hNA: ¬ A, hNA hA -- (false, in the ND proof). -- -- The other direction we must prove with classical reasoning. -- example: ¬¬ A -> A := assume h1: ¬¬ A, show A, from by_contradiction( assume h2: ¬ A, -- Doing this because we're in contradiction "mode". show false, from h1 h2 ) -- Putting it together: example: A <-> ¬¬ A := iff.intro (assume hA: A, -- Intuitionistic, must do this version first. show ¬¬ A, from assume hNA: ¬ A, hNA hA) (assume h1: ¬¬ A, -- By contradiction. show A, from by_contradiction( assume h2: ¬ A, show false, from h1 h2 ))
03f4279768f78d90d270f1fc990fce67345d1486
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/notation4.lean
0f1dffeab4f67229b1090a5818a9b3d6f3175b67
[ "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
343
lean
import logic data.sigma open sigma inductive list (T : Type) : Type := nil {} : list T | cons : T → list T → list T open list notation h :: t := cons h t notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l check ∃ (A : Type₁) (x y : A), x = y check ∃ (x : num), x = 0 check Σ (x : num), x = 10 check Σ (A : Type₁), list A
f962b985d4de86564d6050e59fa1aff7ac9cde8d
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world07/level04.lean
383f2f5d95bc844fa8624acf973236f6692ead6f
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
169
lean
lemma iff_trans (P Q R : Prop) : (P ↔ Q) → (Q ↔ R) → (P ↔ R) := begin intro hpq, intro hqr, cases hpq with hpq hqp, cases hqr with hqr hrq, split, cc, cc, end
b76f8dbbb2032d00085e9d5b166c8e9520a5fcb1
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/targets/src/Foo/Bar.lean
d860a646d9407fb99dcc63848bb113d2d978f730
[ "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
24
lean
def Foo.bar := "foobar"
0253dd8fd08fbdde05cf51015afb30d5a345f93f
ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb
/ijcar16/examples/cc18.lean
997368739fac1c611527c95051dbb1863a81a93d
[ "MIT" ]
permissive
leanprover/leanprover.github.io
cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a
071a20d2e059a2c3733e004c681d3949cac3c07a
refs/heads/master
1,692,621,047,417
1,691,396,994,000
1,691,396,994,000
19,366,263
18
27
MIT
1,693,989,071,000
1,399,006,345,000
Lean
UTF-8
Lean
false
false
671
lean
/- Example/test file for the congruence closure procedure described in the paper: "Congruence Closure for Intensional Type Theory" Daniel Selsam and Leonardo de Moura The tactic `by blast` has been configured in this file to use just the congruence closure procedure using the command set_option blast.strategy "cc" -/ import data.unit open unit set_option blast.strategy "cc" example (a b : unit) : a = b := by blast example (a b : nat) (h₁ : a = 0) (h₂ : b = 0) : a = b → h₁ == h₂ := by blast constant inv' : ∀ (a : nat), a ≠ 0 → nat example (a b : nat) (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a = b → inv' a h₁ = inv' b h₂ := by blast
ae1313eeb5c39625ba3987381560a8d9569f2c11
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/finsupp_vector_space.lean
b65a171d4ed5e83fc71328b9f286b47b0a040fac
[ "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
7,545
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import linear_algebra.dimension import linear_algebra.finite_dimensional import linear_algebra.std_basis /-! # Linear structures on function with finite support `ι →₀ M` This file contains results on the `R`-module structure on functions of finite support from a type `ι` to an `R`-module `M`, in particular in the case that `R` is a field. Furthermore, it contains some facts about isomorphisms of vector spaces from equality of dimension as well as the cardinality of finite dimensional vector spaces. ## TODO Move the second half of this file to more appropriate other files. -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open set linear_map submodule open_locale cardinal universes u v w namespace finsupp section ring variables {R : Type*} {M : Type*} {ι : Type*} variables [ring R] [add_comm_group M] [module R M] lemma linear_independent_single {φ : ι → Type*} {f : Π ι, φ ι → M} (hf : ∀i, linear_independent R (f i)) : linear_independent R (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) := begin apply @linear_independent_Union_finite R _ _ _ _ ι φ (λ i x, single i (f i x)), { assume i, have h_disjoint : disjoint (span R (range (f i))) (ker (lsingle i)), { rw ker_lsingle, exact disjoint_bot_right }, apply (hf i).map h_disjoint }, { intros i t ht hit, refine (disjoint_lsingle_lsingle {i} t (disjoint_singleton_left.2 hit)).mono _ _, { rw span_le, simp only [supr_singleton], rw range_coe, apply range_comp_subset_range }, { refine supr₂_mono (λ i hi, _), rw [span_le, range_coe], apply range_comp_subset_range } } end end ring section semiring variables {R : Type*} {M : Type*} {ι : Type*} variables [semiring R] [add_comm_monoid M] [module R M] open linear_map submodule /-- The basis on `ι →₀ M` with basis vectors `λ ⟨i, x⟩, single i (b i x)`. -/ protected def basis {φ : ι → Type*} (b : ∀ i, basis (φ i) R M) : basis (Σ i, φ i) R (ι →₀ M) := basis.of_repr { to_fun := λ g, { to_fun := λ ix, (b ix.1).repr (g ix.1) ix.2, support := g.support.sigma (λ i, ((b i).repr (g i)).support), mem_support_to_fun := λ ix, by { simp only [finset.mem_sigma, mem_support_iff, and_iff_right_iff_imp, ne.def], intros b hg, simpa [hg] using b } }, inv_fun := λ g, { to_fun := λ i, (b i).repr.symm (g.comap_domain _ (set.inj_on_of_injective sigma_mk_injective _)), support := g.support.image sigma.fst, mem_support_to_fun := λ i, by { rw [ne.def, ← (b i).repr.injective.eq_iff, (b i).repr.apply_symm_apply, ext_iff], simp only [exists_prop, linear_equiv.map_zero, comap_domain_apply, zero_apply, exists_and_distrib_right, mem_support_iff, exists_eq_right, sigma.exists, finset.mem_image, not_forall] } }, left_inv := λ g, by { ext i, rw ← (b i).repr.injective.eq_iff, ext x, simp only [coe_mk, linear_equiv.apply_symm_apply, comap_domain_apply] }, right_inv := λ g, by { ext ⟨i, x⟩, simp only [coe_mk, linear_equiv.apply_symm_apply, comap_domain_apply] }, map_add' := λ g h, by { ext ⟨i, x⟩, simp only [coe_mk, add_apply, linear_equiv.map_add] }, map_smul' := λ c h, by { ext ⟨i, x⟩, simp only [coe_mk, smul_apply, linear_equiv.map_smul, ring_hom.id_apply] } } @[simp] lemma basis_repr {φ : ι → Type*} (b : ∀ i, basis (φ i) R M) (g : ι →₀ M) (ix) : (finsupp.basis b).repr g ix = (b ix.1).repr (g ix.1) ix.2 := rfl @[simp] lemma coe_basis {φ : ι → Type*} (b : ∀ i, basis (φ i) R M) : ⇑(finsupp.basis b) = λ (ix : Σ i, φ i), single ix.1 (b ix.1 ix.2) := funext $ λ ⟨i, x⟩, basis.apply_eq_iff.mpr $ begin ext ⟨j, y⟩, by_cases h : i = j, { cases h, simp only [basis_repr, single_eq_same, basis.repr_self, basis.finsupp.single_apply_left sigma_mk_injective] }, simp only [basis_repr, single_apply, h, false_and, if_false, linear_equiv.map_zero, zero_apply] end /-- The basis on `ι →₀ M` with basis vectors `λ i, single i 1`. -/ @[simps] protected def basis_single_one : basis ι R (ι →₀ R) := basis.of_repr (linear_equiv.refl _ _) @[simp] lemma coe_basis_single_one : (finsupp.basis_single_one : ι → (ι →₀ R)) = λ i, finsupp.single i 1 := funext $ λ i, basis.apply_eq_iff.mpr rfl end semiring section dim variables {K : Type u} {V : Type v} {ι : Type v} variables [field K] [add_comm_group V] [module K V] lemma dim_eq : module.rank K (ι →₀ V) = #ι * module.rank K V := begin let bs := basis.of_vector_space K V, rw [← bs.mk_eq_dim'', ← (finsupp.basis (λa:ι, bs)).mk_eq_dim'', cardinal.mk_sigma, cardinal.sum_const'] end end dim end finsupp section module variables {K : Type u} {V V₁ V₂ : Type v} {V' : Type w} variables [field K] variables [add_comm_group V] [module K V] variables [add_comm_group V₁] [module K V₁] variables [add_comm_group V₂] [module K V₂] variables [add_comm_group V'] [module K V'] open module lemma equiv_of_dim_eq_lift_dim (h : cardinal.lift.{w} (module.rank K V) = cardinal.lift.{v} (module.rank K V')) : nonempty (V ≃ₗ[K] V') := begin haveI := classical.dec_eq V, haveI := classical.dec_eq V', let m := basis.of_vector_space K V, let m' := basis.of_vector_space K V', rw [←cardinal.lift_inj.1 m.mk_eq_dim, ←cardinal.lift_inj.1 m'.mk_eq_dim] at h, rcases quotient.exact h with ⟨e⟩, let e := (equiv.ulift.symm.trans e).trans equiv.ulift, exact ⟨(m.repr ≪≫ₗ (finsupp.dom_lcongr e)) ≪≫ₗ m'.repr.symm⟩ end /-- Two `K`-vector spaces are equivalent if their dimension is the same. -/ def equiv_of_dim_eq_dim (h : module.rank K V₁ = module.rank K V₂) : V₁ ≃ₗ[K] V₂ := begin classical, exact classical.choice (equiv_of_dim_eq_lift_dim (cardinal.lift_inj.2 h)) end /-- An `n`-dimensional `K`-vector space is equivalent to `fin n → K`. -/ def fin_dim_vectorspace_equiv (n : ℕ) (hn : (module.rank K V) = n) : V ≃ₗ[K] (fin n → K) := begin have : cardinal.lift.{u} (n : cardinal.{v}) = cardinal.lift.{v} (n : cardinal.{u}), by simp, have hn := cardinal.lift_inj.{v u}.2 hn, rw this at hn, rw ←@dim_fin_fun K _ n at hn, exact classical.choice (equiv_of_dim_eq_lift_dim hn), end end module section module open module variables (K V : Type u) [field K] [add_comm_group V] [module K V] lemma cardinal_mk_eq_cardinal_mk_field_pow_dim [finite_dimensional K V] : #V = #K ^ module.rank K V := begin let s := basis.of_vector_space_index K V, let hs := basis.of_vector_space K V, calc #V = #(s →₀ K) : quotient.sound ⟨hs.repr.to_equiv⟩ ... = #(s → K) : quotient.sound ⟨finsupp.equiv_fun_on_fintype⟩ ... = _ : by rw [← cardinal.lift_inj.1 hs.mk_eq_dim, cardinal.power_def] end lemma cardinal_lt_aleph_0_of_finite_dimensional [_root_.finite K] [finite_dimensional K V] : #V < ℵ₀ := begin letI : is_noetherian K V := is_noetherian.iff_fg.2 infer_instance, rw cardinal_mk_eq_cardinal_mk_field_pow_dim K V, exact cardinal.power_lt_aleph_0 (cardinal.lt_aleph_0_of_finite K) (is_noetherian.dim_lt_aleph_0 K V), end end module
deb923f10727b6283a5a3e1b782e9832142ebdf4
52b9f0379b3b0200088f3b2ec594d4dd3d3e6128
/polynomial.lean
a23d77f8edf793f18adddc09751201ca32cbedeb
[]
no_license
minchaowu/mathematica_examples
f83fdf092a136f157dde8119b8a75c2cd4d91aae
fcc65b0b9fcb854f8671c0ebbca77bb3c1a9ecc1
refs/heads/master
1,610,729,580,720
1,498,331,049,000
1,498,331,049,000
99,731,631
0
0
null
1,502,222,706,000
1,502,222,706,000
null
UTF-8
Lean
false
false
3,207
lean
/- Copyright (c) 2017 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis -/ import init.meta.mathematica open expr tactic int meta def lam_bod : expr → tactic expr | (lam nm bi tp bd) := do head_beta $ app (lam nm bi tp bd) (local_const nm nm bi tp) | e := return e meta def lam_bod_rec : expr → tactic expr | (lam nm bi tp bd) := lam_bod (lam nm bi tp bd) >>= lam_bod_rec | e := return e meta def expr_of_list_expr : list expr → tactic expr | [] := to_expr ```([]) | (h :: t) := do t' ← expr_of_list_expr t, to_expr ```(%%h :: %%t') meta def dest_list_fst (e : expr) : tactic expr := do l ← match_app_of e `list.cons, match list.nth l 1 with | some k := return k | none := failed end meta def dest_list_snd (e : expr) : tactic (expr × expr) := do l ← match_app_of e `list.cons, match (list.nth l 1, list.nth l 2) with | (some k1, some l') := do k2 ← dest_list_fst l', return (k1, k2) | _ := failed end meta def count_poly_vars : expr → nat | (lam _ _ _ bd) := count_poly_vars bd + 1 | _ := 0 meta def get_poly_vars : expr → list expr | (lam nm bi tp bd) := local_const nm nm bi tp :: get_poly_vars bd | _ := [] meta def expr_list_of_list_expr : expr → tactic (list expr) | (app (app (app (const `list.cons _) _) h) t) := do t' ← expr_list_of_list_expr t, return $ h :: t' | (app (const `list.nil _) _) := return [] | _ := failed meta def fold_apps : expr → list expr → expr | e [] := e | e (h :: t) := fold_apps (app e h) t meta def multi_exact : list expr → tactic unit | [] := done | (t :: ts) := exact t >> multi_exact ts -- returns an expr k encoding a list ks and a list of proofs ps such that ps[i] proves l[i](ks) = 0 meta def solve_polys : list expr → tactic (expr × list expr) | [] := fail "solve_polys failed, no functions given" | (h :: t) := let vs' := get_poly_vars h in if bnot (list.all (h::t) (λ p, if count_poly_vars p = count_poly_vars h then tt else ff)) then fail "solve_polys failed, functions have different arities" else do l' ← monad.mapm lam_bod_rec (h::t), conj ← monad.foldl (λ e1 e2, to_expr ```(%%e1 ∧ (%%e2 = 0))) (const `true []) l', vs ← expr_of_list_expr vs', sol ← mathematica.run_command_on_2_using (λ s t, "Solve[ " ++ s ++ "// LeanForm // Activate, " ++ t ++" // LeanForm // Activate, Reals] // LUnrule") conj vs "~/lean/lean/extras/mathematica/poly.m", tp ← infer_type $ list.head vs', r ← to_expr ```((%%sol : list (list %%tp))), fstsol ← dest_list_fst r, intes ← expr_list_of_list_expr fstsol, apps ← monad.mapm head_beta $ list.map ((λ e, fold_apps e intes)) (h::t), zrprs ← monad.mapm (λ e, do e' ← norm_num e, return e'.2) apps, return (fstsol, zrprs) meta def strip_ex : expr → expr | (app (app (const `Exists _) _) (lam _ _ _ bod)) := strip_ex bod | a := a def e1 : ∃ x y : ℤ, x*x*x-y=0 ∧ y-8=0 := by do f ← to_expr ```(λ x y : ℤ, x*x*x-y), g ← to_expr ```(λ x y : ℤ, y-8), (_, prs) ← solve_polys [f, g], constructor, constructor, constructor, multi_exact prs
b183d5cf80020879fff9767b7d852c724e1b6040
274748215b6d042f0d9c9a505f9551fa8e0c5f38
/src/affine_algebraic_set/Zariski.lean
6d177985019eec34ed8f42d6365bc16183e27c4d
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/M4P33
878ecb515c77d20cc799ff1ebd78f1bf4fd65c12
1a179372db71ad6802d11eacbc1f02f327d55f8f
refs/heads/master
1,607,519,867,193
1,583,344,297,000
1,583,344,297,000
233,316,107
59
4
Apache-2.0
1,579,285,778,000
1,578,788,367,000
Lean
UTF-8
Lean
false
false
4,654
lean
import affine_algebraic_set.V import topology.opens import affine_algebraic_set.V_and_I import for_mathlib.calle_set namespace affine_algebraic_set variables {k : Type*} [integral_domain k] {σ : Type*} local notation `𝔸ⁿ` := σ → k open set open topological_space instance Zariski_topology : topological_space 𝔸ⁿ := { -- First we need to define what an open is, in lean we need to give a function -- from set (n → k) → Prop i.e. a function which takes a set in k^n and -- determines if this is open or not. is_open := λ U, ∃ (S : set (mv_polynomial σ k)), U = -𝕍 (S), -- Secondly we show that univ, the whole set, is open. is_open_univ := begin -- we know that the whole set will be the required set, so we "use univ" use (set.univ : set (mv_polynomial σ k)), -- Use fact that V(univ) = ∅ rw 𝕍_univ, -- Putting goal into canonical form, i.e. use the frontend notations such -- as - instead of compl -- this is important because rewrites wont recognize definitionally -- equivalent statements as the same show set.univ = - ∅, -- Now that we are using canonical form, rewrites will work again. -- So we finish up by using fact -- that -(∅) = univ rw compl_empty, end, -- Now we show that being open is preserved by intersections. is_open_inter := begin -- Let U, V be opens and let U_set be the fact that there is some S such -- that U = - 𝕍 (S). Similarly for V_set. intros U V U_set V_set, -- unpack U_set and V_set to access the underlying sets S and T cases U_set with S U_comp, cases V_set with T V_comp, -- Now we wish to show that S*T satisfies the goal use S*T, -- Use multiplicative property of 𝕍 rw [𝕍_mul], -- TODO: explain convert convert (compl_union _ _).symm, end, -- Finally we wish to show that opens is preserved by arbitary unions is_open_sUnion := begin -- Let opens be the set of opens that we wish to union over intros opens open_comp, -- Define H to be the set of sets of polynomials S s.t. - 𝕍 (S) is in opens. let H := {S : set (mv_polynomial σ k) | - 𝕍 (S) ∈ opens}, -- We now want to show that union over H satisfies the goal use ⋃₀ H, -- converting from sUnion to Union so that we can use the lemma 𝕍_union rw @sUnion_eq_Union (mv_polynomial σ k) H, rw 𝕍_Union, -- putting goal in canonical form show ⋃₀ opens = - (⋂ (i : H), 𝕍 (i.val)), -- Now that we are using canonical form, rewrites will work again. rw compl_Inter, rw sUnion_eq_Union, -- We prove the two sides are equal by showing the double inclusion apply eq_of_subset_of_subset, { apply Union_subset_Union2, intro U, cases (open_comp U U.2) with S eq, use S, change ↑U = compl (𝕍 S) at eq, show compl (𝕍 S) ∈ opens, rw ←eq, exact U.2, show U.val ⊆ compl (𝕍 (S)), rw subset.antisymm_iff at eq, cases eq, exact eq_left, }, apply Union_subset_Union2, intro S, use - 𝕍 S, exact S.2, show -𝕍 (S.1) ⊆ -𝕍 (S.1), refine set.subset.refl _, end } open_locale classical lemma is_closed_iff (C : set 𝔸ⁿ) : is_closed C ↔ ∃ (S : set (mv_polynomial σ k)), C = 𝕍 S := begin unfold is_closed, show (∃ (S : set (mv_polynomial σ k)), (compl C) = compl (𝕍 S)) ↔ _, rw exists_congr, intro S, split, intro h, ext x, apply not_iff_not.1, rw [←mem_compl_iff, ←mem_compl_iff], congr', apply_instance, apply_instance, intro h, rw h, end theorem zariski_wf {k : Type*} {n : Type*} [fintype n] [integral_domain k] [is_noetherian k k] : well_founded ((>) : (opens (n → k) → (opens (n → k)) → Prop)) := begin have subrel : ∀ (V U: opens (n → k)), U < V → 𝕀' (-↑U) < 𝕀' (-↑V), { intros U V lt, have exists_U_eq_𝕍_S := (is_closed_iff (-↑U)).1 (is_closed_compl_iff.2 U.2), cases exists_U_eq_𝕍_S with S U_eq_𝕍_S, have exists_V_eq_𝕍_T := (is_closed_iff (-↑V)).1 (is_closed_compl_iff.2 V.2), cases exists_V_eq_𝕍_T with T V_eq_𝕍_T, rw [U_eq_𝕍_S, V_eq_𝕍_T, submodule.lt_def], refine 𝕀_strantimono_on_𝕍 _, rw [←U_eq_𝕍_S, ←V_eq_𝕍_T], apply compl_lt_compl, rw [compl_compl, compl_compl], exact lt, }, apply subrelation.wf subrel _, refine @inv_image.wf _ _ (>) (λ U : opens (n → k), 𝕀' (-(↑U : set (n → k)))) _, apply is_noetherian_iff_well_founded.1, refine @is_noetherian_ring_mv_polynomial_of_fintype _ _ _ _ _inst_4, end end affine_algebraic_set
79083635be808da3db55a1707203e2dd4b74d7ca
bb31430994044506fa42fd667e2d556327e18dfe
/src/order/antichain.lean
ab3d0e5e6bbc0bf251be523ae7aec8af6f2b2f19
[ "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
12,459
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.set.pairwise /-! # Antichains > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines antichains. An antichain is a set where any two distinct elements are not related. If the relation is `(≤)`, this corresponds to incomparability and usual order antichains. If the relation is `G.adj` for `G : simple_graph α`, this corresponds to independent sets of `G`. ## Definitions * `is_antichain r s`: Any two elements of `s : set α` are unrelated by `r : α → α → Prop`. * `is_strong_antichain r s`: Any two elements of `s : set α` are not related by `r : α → α → Prop` to a common element. * `is_antichain.mk r s`: Turns `s` into an antichain by keeping only the "maximal" elements. -/ open function set section general variables {α β : Type*} {r r₁ r₂ : α → α → Prop} {r' : β → β → Prop} {s t : set α} {a : α} protected lemma symmetric.compl (h : symmetric r) : symmetric rᶜ := λ x y hr hr', hr $ h hr' /-- An antichain is a set such that no two distinct elements are related. -/ def is_antichain (r : α → α → Prop) (s : set α) : Prop := s.pairwise rᶜ namespace is_antichain protected lemma subset (hs : is_antichain r s) (h : t ⊆ s) : is_antichain r t := hs.mono h lemma mono (hs : is_antichain r₁ s) (h : r₂ ≤ r₁) : is_antichain r₂ s := hs.mono' $ compl_le_compl h lemma mono_on (hs : is_antichain r₁ s) (h : s.pairwise (λ ⦃a b⦄, r₂ a b → r₁ a b)) : is_antichain r₂ s := hs.imp_on $ h.imp $ λ a b h h₁ h₂, h₁ $ h h₂ protected lemma eq (hs : is_antichain r s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : r a b) : a = b := hs.eq ha hb $ not_not_intro h protected lemma eq' (hs : is_antichain r s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : r b a) : a = b := (hs.eq hb ha h).symm protected lemma is_antisymm (h : is_antichain r univ) : is_antisymm α r := ⟨λ a b ha _, h.eq trivial trivial ha⟩ protected lemma subsingleton [is_trichotomous α r] (h : is_antichain r s) : s.subsingleton := begin rintro a ha b hb, obtain hab | hab | hab := trichotomous_of r a b, { exact h.eq ha hb hab }, { exact hab }, { exact h.eq' ha hb hab } end protected lemma flip (hs : is_antichain r s) : is_antichain (flip r) s := λ a ha b hb h, hs hb ha h.symm lemma swap (hs : is_antichain r s) : is_antichain (swap r) s := hs.flip lemma image (hs : is_antichain r s) (f : α → β) (h : ∀ ⦃a b⦄, r' (f a) (f b) → r a b) : is_antichain r' (f '' s) := begin rintro _ ⟨b, hb, rfl⟩ _ ⟨c, hc, rfl⟩ hbc hr, exact hs hb hc (ne_of_apply_ne _ hbc) (h hr), end lemma preimage (hs : is_antichain r s) {f : β → α} (hf : injective f) (h : ∀ ⦃a b⦄, r' a b → r (f a) (f b)) : is_antichain r' (f ⁻¹' s) := λ b hb c hc hbc hr, hs hb hc (hf.ne hbc) $ h hr lemma _root_.is_antichain_insert : is_antichain r (insert a s) ↔ is_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b ∧ ¬ r b a := set.pairwise_insert protected lemma insert (hs : is_antichain r s) (hl : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r b a) (hr : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b) : is_antichain r (insert a s) := is_antichain_insert.2 ⟨hs, λ b hb hab, ⟨hr hb hab, hl hb hab⟩⟩ lemma _root_.is_antichain_insert_of_symmetric (hr : symmetric r) : is_antichain r (insert a s) ↔ is_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b := pairwise_insert_of_symmetric hr.compl lemma insert_of_symmetric (hs : is_antichain r s) (hr : symmetric r) (h : ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ r a b) : is_antichain r (insert a s) := (is_antichain_insert_of_symmetric hr).2 ⟨hs, h⟩ lemma image_rel_embedding (hs : is_antichain r s) (φ : r ↪r r') : is_antichain r' (φ '' s) := begin intros b hb b' hb' h₁ h₂, rw set.mem_image at hb hb', obtain ⟨⟨a,has,rfl⟩,⟨a',has',rfl⟩⟩ := ⟨hb,hb'⟩, exact hs has has' (λ haa', h₁ (haa'.subst (by refl))) (φ.map_rel_iff.mp h₂), end lemma preimage_rel_embedding {t : set β} (ht : is_antichain r' t) (φ : r ↪r r') : is_antichain r (φ ⁻¹' t) := λ a ha a' ha' hne hle, ht ha ha' (λ h, hne (φ.injective h)) (φ.map_rel_iff.mpr hle) lemma image_rel_iso (hs : is_antichain r s) (φ : r ≃r r') : is_antichain r' (φ '' s) := hs.image_rel_embedding φ lemma preimage_rel_iso {t : set β} (hs : is_antichain r' t) (φ : r ≃r r') : is_antichain r (φ ⁻¹' t) := hs.preimage_rel_embedding φ lemma image_rel_embedding_iff {φ : r ↪r r'} : is_antichain r' (φ '' s) ↔ is_antichain r s := ⟨λ h, (φ.injective.preimage_image s).subst (h.preimage_rel_embedding φ), λ h, h.image_rel_embedding φ⟩ lemma image_rel_iso_iff {φ : r ≃r r'} : is_antichain r' (φ '' s) ↔ is_antichain r s := @image_rel_embedding_iff _ _ _ _ _ (φ : r ↪r r') lemma image_embedding [has_le α] [has_le β] (hs : is_antichain (≤) s) (φ : α ↪o β) : is_antichain (≤) (φ '' s) := image_rel_embedding hs _ lemma preimage_embedding [has_le α] [has_le β] {t : set β} (ht : is_antichain (≤) t) (φ : α ↪o β) : is_antichain (≤) (φ ⁻¹' t) := preimage_rel_embedding ht _ lemma image_embedding_iff [has_le α] [has_le β] {φ : α ↪o β} : is_antichain (≤) (φ '' s) ↔ is_antichain (≤) s := image_rel_embedding_iff lemma image_iso [has_le α] [has_le β] (hs : is_antichain (≤) s) (φ : α ≃o β) : is_antichain (≤) (φ '' s) := image_rel_embedding hs _ lemma image_iso_iff [has_le α] [has_le β] {φ : α ≃o β} : is_antichain (≤) (φ '' s) ↔ is_antichain (≤) s := image_rel_embedding_iff lemma preimage_iso [has_le α] [has_le β] {t : set β} (ht : is_antichain (≤) t) (φ : α ≃o β) : is_antichain (≤) (φ ⁻¹' t) := preimage_rel_embedding ht _ lemma preimage_iso_iff [has_le α] [has_le β] {t : set β} {φ : α ≃o β} : is_antichain (≤) (φ ⁻¹' t) ↔ is_antichain (≤) t := ⟨λ h, (φ.image_preimage t).subst (h.image_iso φ), λ h, h.preimage_iso _⟩ lemma to_dual [has_le α] (hs : is_antichain (≤) s) : @is_antichain αᵒᵈ (≤) s := λ a ha b hb hab, hs hb ha hab.symm lemma to_dual_iff [has_le α] : is_antichain (≤) s ↔ @is_antichain αᵒᵈ (≤) s := ⟨to_dual, to_dual⟩ lemma image_compl [boolean_algebra α] (hs : is_antichain (≤) s) : is_antichain (≤) (compl '' s) := (hs.image_embedding (order_iso.compl α).to_order_embedding).flip lemma preimage_compl [boolean_algebra α] (hs : is_antichain (≤) s) : is_antichain (≤) (compl ⁻¹' s) := λ a ha a' ha' hne hle, hs ha' ha (λ h, hne (compl_inj_iff.mp h.symm)) (compl_le_compl hle) end is_antichain lemma is_antichain_singleton (a : α) (r : α → α → Prop) : is_antichain r {a} := pairwise_singleton _ _ lemma set.subsingleton.is_antichain (hs : s.subsingleton) (r : α → α → Prop) : is_antichain r s := hs.pairwise _ section preorder variables [preorder α] lemma is_antichain_and_least_iff : is_antichain (≤) s ∧ is_least s a ↔ s = {a} := ⟨λ h, eq_singleton_iff_unique_mem.2 ⟨h.2.1, λ b hb, h.1.eq' hb h.2.1 (h.2.2 hb)⟩, by { rintro rfl, exact ⟨is_antichain_singleton _ _, is_least_singleton⟩ }⟩ lemma is_antichain_and_greatest_iff : is_antichain (≤) s ∧ is_greatest s a ↔ s = {a} := ⟨λ h, eq_singleton_iff_unique_mem.2 ⟨h.2.1, λ b hb, h.1.eq hb h.2.1 (h.2.2 hb)⟩, by { rintro rfl, exact ⟨is_antichain_singleton _ _, is_greatest_singleton⟩ }⟩ lemma is_antichain.least_iff (hs : is_antichain (≤) s) : is_least s a ↔ s = {a} := (and_iff_right hs).symm.trans is_antichain_and_least_iff lemma is_antichain.greatest_iff (hs : is_antichain (≤) s) : is_greatest s a ↔ s = {a} := (and_iff_right hs).symm.trans is_antichain_and_greatest_iff lemma is_least.antichain_iff (hs : is_least s a) : is_antichain (≤) s ↔ s = {a} := (and_iff_left hs).symm.trans is_antichain_and_least_iff lemma is_greatest.antichain_iff (hs : is_greatest s a) : is_antichain (≤) s ↔ s = {a} := (and_iff_left hs).symm.trans is_antichain_and_greatest_iff lemma is_antichain.bot_mem_iff [order_bot α] (hs : is_antichain (≤) s) : ⊥ ∈ s ↔ s = {⊥} := is_least_bot_iff.symm.trans hs.least_iff lemma is_antichain.top_mem_iff [order_top α] (hs : is_antichain (≤) s) : ⊤ ∈ s ↔ s = {⊤} := is_greatest_top_iff.symm.trans hs.greatest_iff end preorder /-! ### Strong antichains -/ /-- A strong (upward) antichain is a set such that no two distinct elements are related to a common element. -/ def is_strong_antichain (r : α → α → Prop) (s : set α) : Prop := s.pairwise $ λ a b, ∀ c, ¬ r a c ∨ ¬ r b c namespace is_strong_antichain protected lemma subset (hs : is_strong_antichain r s) (h : t ⊆ s) : is_strong_antichain r t := hs.mono h lemma mono (hs : is_strong_antichain r₁ s) (h : r₂ ≤ r₁) : is_strong_antichain r₂ s := hs.mono' $ λ a b hab c, (hab c).imp (compl_le_compl h _ _) (compl_le_compl h _ _) lemma eq (hs : is_strong_antichain r s) {a b c : α} (ha : a ∈ s) (hb : b ∈ s) (hac : r a c) (hbc : r b c) : a = b := hs.eq ha hb $ λ h, false.elim $ (h c).elim (not_not_intro hac) (not_not_intro hbc) protected lemma is_antichain [is_refl α r] (h : is_strong_antichain r s) : is_antichain r s := h.imp $ λ a b hab, (hab b).resolve_right (not_not_intro $ refl _) protected lemma subsingleton [is_directed α r] (h : is_strong_antichain r s) : s.subsingleton := λ a ha b hb, let ⟨c, hac, hbc⟩ := directed_of r a b in h.eq ha hb hac hbc protected lemma flip [is_symm α r] (hs : is_strong_antichain r s) : is_strong_antichain (flip r) s := λ a ha b hb h c, (hs ha hb h c).imp (mt $ symm_of r) (mt $ symm_of r) lemma swap [is_symm α r] (hs : is_strong_antichain r s) : is_strong_antichain (swap r) s := hs.flip lemma image (hs : is_strong_antichain r s) {f : α → β} (hf : surjective f) (h : ∀ a b, r' (f a) (f b) → r a b) : is_strong_antichain r' (f '' s) := begin rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ hab c, obtain ⟨c, rfl⟩ := hf c, exact (hs ha hb (ne_of_apply_ne _ hab) _).imp (mt $ h _ _) (mt $ h _ _), end lemma preimage (hs : is_strong_antichain r s) {f : β → α} (hf : injective f) (h : ∀ a b, r' a b → r (f a) (f b)) : is_strong_antichain r' (f ⁻¹' s) := λ a ha b hb hab c, (hs ha hb (hf.ne hab) _).imp (mt $ h _ _) (mt $ h _ _) lemma _root_.is_strong_antichain_insert : is_strong_antichain r (insert a s) ↔ is_strong_antichain r s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ∀ c, ¬ r a c ∨ ¬ r b c := set.pairwise_insert_of_symmetric $ λ a b h c, (h c).symm protected lemma insert (hs : is_strong_antichain r s) (h : ∀ ⦃b⦄, b ∈ s → a ≠ b → ∀ c, ¬ r a c ∨ ¬ r b c) : is_strong_antichain r (insert a s) := is_strong_antichain_insert.2 ⟨hs, h⟩ end is_strong_antichain lemma set.subsingleton.is_strong_antichain (hs : s.subsingleton) (r : α → α → Prop) : is_strong_antichain r s := hs.pairwise _ end general /-! ### Weak antichains -/ section pi variables {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s t : set (Π i, α i)} {a b c : Π i, α i} local infix ` ≺ `:50 := strong_lt /-- A weak antichain in `Π i, α i` is a set such that no two distinct elements are strongly less than each other. -/ def is_weak_antichain (s : set (Π i, α i)) : Prop := is_antichain (≺) s namespace is_weak_antichain protected lemma subset (hs : is_weak_antichain s) : t ⊆ s → is_weak_antichain t := hs.subset protected lemma eq (hs : is_weak_antichain s) : a ∈ s → b ∈ s → a ≺ b → a = b := hs.eq protected lemma insert (hs : is_weak_antichain s) : (∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ b ≺ a) → (∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ a ≺ b) → is_weak_antichain (insert a s) := hs.insert end is_weak_antichain lemma is_weak_antichain_insert : is_weak_antichain (insert a s) ↔ is_weak_antichain s ∧ ∀ ⦃b⦄, b ∈ s → a ≠ b → ¬ a ≺ b ∧ ¬ b ≺ a := is_antichain_insert protected lemma is_antichain.is_weak_antichain (hs : is_antichain (≤) s) : is_weak_antichain s := hs.mono $ λ a b, le_of_strong_lt lemma set.subsingleton.is_weak_antichain (hs : s.subsingleton) : is_weak_antichain s := hs.is_antichain _ end pi
12c2112eeea4fb3a4abe63653559dea61883a657
0dc59d2b959c9b11a672f655b104d7d7d3e37660
/Lean4_filters/Set/CompleteLattice.lean
b01369956481823fc819cd4f644255b1287f7ad4
[]
no_license
kbuzzard/lean4-filters
5aa17d95079ceb906622543209064151fa645e71
29f90055b7a2341c86d924954463c439bd128fb7
refs/heads/master
1,679,762,259,673
1,616,701,300,000
1,616,701,300,000
350,784,493
5
1
null
1,625,691,081,000
1,616,517,435,000
Lean
UTF-8
Lean
false
false
1,078
lean
import Lean4_filters.Lattice.CompleteLattice variable {α : Type u} instance : CompleteLattice (Set α) := { top := Set.univ leTop := λ s x hx => True.intro bot := Set.empty botLe := λ s x hx => False.elim hx supr := λ C x => ∃ s, s ∈ C ∧ x ∈ s leSupr := λ C s hsC x hxs => ⟨s, hsC, hxs⟩ suprLe := λ C s h1 x ⟨t, htC, hxt⟩ => h1 t htC hxt infi := λ C x => ∀ s, s ∈ C → x ∈ s infiLe := λ X s hsX a h => h _ hsX leInfi := λ C s h a has t htC => h t htC has } example : CompleteLattice (Set α) := { top := Set.univ leTop := by intros s x hx; trivial; bot := Set.empty botLe := by intros s x hx; cases hx; supr := λ C x => ∃ s, s ∈ C ∧ x ∈ s leSupr := by intros C s hsC x hxs; exact ⟨s, hsC, hxs⟩; suprLe := by intros C s h1 x h; let ⟨t, htC, hxt⟩ := h; exact h1 t htC hxt; infi := λ C x => ∀ s, s ∈ C → x ∈ s infiLe := by intro X s hsX a h; exact h _ hsX; leInfi := by intro C s h a has t htC; exact h t htC has }
28643a1f27b6ab4cb6b699251db947ac1760ec15
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/structure_doc_string.lean
1a8914c2002dfe96463f49d5103ced2e24c6a62e
[ "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
58
lean
structure foo := (x : nat) /-- doc string -/ def x := 10
af1c0ef1ff6cccbb368274644136d3b6e03a1b53
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/inner_product_space/conformal_linear_map.lean
d02ee93c30e530b2ef8eb08a0b3a94848534464b
[ "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
1,757
lean
/- Copyright (c) 2021 Yourong Zang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yourong Zang -/ import analysis.normed_space.conformal_linear_map import analysis.inner_product_space.basic /-! # Conformal maps between inner product spaces In an inner product space, a map is conformal iff it preserves inner products up to a scalar factor. -/ variables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F] open linear_isometry continuous_linear_map open_locale real_inner_product_space /-- A map between two inner product spaces is a conformal map if and only if it preserves inner products up to a scalar factor, i.e., there exists a positive `c : ℝ` such that `⟪f u, f v⟫ = c * ⟪u, v⟫` for all `u`, `v`. -/ lemma is_conformal_map_iff (f : E →L[ℝ] F) : is_conformal_map f ↔ ∃ (c : ℝ), 0 < c ∧ ∀ (u v : E), ⟪f u, f v⟫ = c * ⟪u, v⟫ := begin split, { rintros ⟨c₁, hc₁, li, rfl⟩, refine ⟨c₁ * c₁, mul_self_pos.2 hc₁, λ u v, _⟩, simp only [real_inner_smul_left, real_inner_smul_right, mul_assoc, coe_smul', coe_to_continuous_linear_map, pi.smul_apply, inner_map_map] }, { rintros ⟨c₁, hc₁, huv⟩, obtain ⟨c, hc, rfl⟩ : ∃ c : ℝ, 0 < c ∧ c₁ = c * c, from ⟨real.sqrt c₁, real.sqrt_pos.2 hc₁, (real.mul_self_sqrt hc₁.le).symm⟩, refine ⟨c, hc.ne', (c⁻¹ • f : E →ₗ[ℝ] F).isometry_of_inner (λ u v, _), _⟩, { simp only [real_inner_smul_left, real_inner_smul_right, huv, mul_assoc, coe_smul, inv_mul_cancel_left₀ hc.ne', linear_map.smul_apply, continuous_linear_map.coe_coe] }, { ext1 x, exact (smul_inv_smul₀ hc.ne' (f x)).symm } } end
d3de17a753e30544596888f9ec2c07a21f689e79
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/list/nodup_equiv_fin.lean
e8c4a863d7d4f98efff59120a0b70307aaee879c
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,257
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import data.fin.basic import data.list.sort import data.list.duplicate /-! # Isomorphism between `fin (length l)` and `{x // x ∈ l}` Given a list `l, * if `l` has no duplicates, then `list.nodup.nth_le_equiv` is the bijection between `fin (length l)` and `{x // x ∈ l}` sending `⟨i, hi⟩` to `⟨nth_le l i hi, _⟩` with the inverse sending `⟨x, hx⟩` to `⟨index_of x l, _⟩`; * if `l` is sorted w.r.t. `(<)`, then `list.sorted.nth_le_iso` is the same bijection reinterpreted as an `order_iso`. -/ namespace list variable {α : Type*} namespace nodup variable [decidable_eq α] /-- If `l` has no duplicates, then `list.nth_le` defines a bijection between `fin (length l)` and the set of elements of `l`. -/ def nth_le_equiv (l : list α) (H : nodup l) : fin (length l) ≃ {x // x ∈ l} := { to_fun := λ i, ⟨nth_le l i i.2, nth_le_mem l i i.2⟩, inv_fun := λ x, ⟨index_of ↑x l, index_of_lt_length.2 x.2⟩, left_inv := λ i, by simp [H], right_inv := λ x, by simp } variables {l : list α} (H : nodup l) (x : {x // x ∈ l}) (i : fin (length l)) @[simp] lemma coe_nth_le_equiv_apply : (H.nth_le_equiv l i : α) = nth_le l i i.2 := rfl @[simp] lemma coe_nth_le_equiv_symm_apply : ((H.nth_le_equiv l).symm x : ℕ) = index_of ↑x l := rfl end nodup namespace sorted variables [preorder α] {l : list α} lemma nth_le_mono (h : l.sorted (≤)) : monotone (λ i : fin l.length, l.nth_le i i.2) := λ i j, h.rel_nth_le_of_le _ _ lemma nth_le_strict_mono (h : l.sorted (<)) : strict_mono (λ i : fin l.length, l.nth_le i i.2) := λ i j, h.rel_nth_le_of_lt _ _ variable [decidable_eq α] /-- If `l` is a list sorted w.r.t. `(<)`, then `list.nth_le` defines an order isomorphism between `fin (length l)` and the set of elements of `l`. -/ def nth_le_iso (l : list α) (H : sorted (<) l) : fin (length l) ≃o {x // x ∈ l} := { to_equiv := H.nodup.nth_le_equiv l, map_rel_iff' := λ i j, H.nth_le_strict_mono.le_iff_le } variables (H : sorted (<) l) {x : {x // x ∈ l}} {i : fin l.length} @[simp] lemma coe_nth_le_iso_apply : (H.nth_le_iso l i : α) = nth_le l i i.2 := rfl @[simp] lemma coe_nth_le_iso_symm_apply : ((H.nth_le_iso l).symm x : ℕ) = index_of ↑x l := rfl end sorted section sublist /-- If there is `f`, an order-preserving embedding of `ℕ` into `ℕ` such that any element of `l` found at index `ix` can be found at index `f ix` in `l'`, then `sublist l l'`. -/ lemma sublist_of_order_embedding_nth_eq {l l' : list α} (f : ℕ ↪o ℕ) (hf : ∀ (ix : ℕ), l.nth ix = l'.nth (f ix)) : l <+ l' := begin induction l with hd tl IH generalizing l' f, { simp }, have : some hd = _ := hf 0, rw [eq_comm, list.nth_eq_some] at this, obtain ⟨w, h⟩ := this, let f' : ℕ ↪o ℕ := order_embedding.of_map_le_iff (λ i, f (i + 1) - (f 0 + 1)) (λ a b, by simp [tsub_le_tsub_iff_right, nat.succ_le_iff, nat.lt_succ_iff]), have : ∀ ix, tl.nth ix = (l'.drop (f 0 + 1)).nth (f' ix), { intro ix, simp [list.nth_drop, add_tsub_cancel_of_le, nat.succ_le_iff, ←hf] }, rw [←list.take_append_drop (f 0 + 1) l', ←list.singleton_append], apply list.sublist.append _ (IH _ this), rw [list.singleton_sublist, ←h, l'.nth_le_take _ (nat.lt_succ_self _)], apply list.nth_le_mem end /-- A `l : list α` is `sublist l l'` for `l' : list α` iff there is `f`, an order-preserving embedding of `ℕ` into `ℕ` such that any element of `l` found at index `ix` can be found at index `f ix` in `l'`. -/ lemma sublist_iff_exists_order_embedding_nth_eq {l l' : list α} : l <+ l' ↔ ∃ (f : ℕ ↪o ℕ), ∀ (ix : ℕ), l.nth ix = l'.nth (f ix) := begin split, { intro H, induction H with xs ys y H IH xs ys x H IH, { simp }, { obtain ⟨f, hf⟩ := IH, refine ⟨f.trans (order_embedding.of_strict_mono (+ 1) (λ _, by simp)), _⟩, simpa using hf }, { obtain ⟨f, hf⟩ := IH, refine ⟨order_embedding.of_map_le_iff (λ (ix : ℕ), if ix = 0 then 0 else (f ix.pred).succ) _, _⟩, { rintro ⟨_|a⟩ ⟨_|b⟩; simp [nat.succ_le_succ_iff] }, { rintro ⟨_|i⟩, { simp }, { simpa using hf _ } } } }, { rintro ⟨f, hf⟩, exact sublist_of_order_embedding_nth_eq f hf } end /-- A `l : list α` is `sublist l l'` for `l' : list α` iff there is `f`, an order-preserving embedding of `fin l.length` into `fin l'.length` such that any element of `l` found at index `ix` can be found at index `f ix` in `l'`. -/ lemma sublist_iff_exists_fin_order_embedding_nth_le_eq {l l' : list α} : l <+ l' ↔ ∃ (f : fin l.length ↪o fin l'.length), ∀ (ix : fin l.length), l.nth_le ix ix.is_lt = l'.nth_le (f ix) (f ix).is_lt := begin rw sublist_iff_exists_order_embedding_nth_eq, split, { rintro ⟨f, hf⟩, have h : ∀ {i : ℕ} (h : i < l.length), f i < l'.length, { intros i hi, specialize hf i, rw [nth_le_nth hi, eq_comm, nth_eq_some] at hf, obtain ⟨h, -⟩ := hf, exact h }, refine ⟨order_embedding.of_map_le_iff (λ ix, ⟨f ix, h ix.is_lt⟩) _, _⟩, { simp }, { intro i, apply option.some_injective, simpa [←nth_le_nth] using hf _ } }, { rintro ⟨f, hf⟩, refine ⟨order_embedding.of_strict_mono (λ i, if hi : i < l.length then f ⟨i, hi⟩ else i + l'.length) _, _⟩, { intros i j h, dsimp only, split_ifs with hi hj hj hi, { simpa using h }, { rw add_comm, exact lt_add_of_lt_of_pos (fin.is_lt _) (i.zero_le.trans_lt h) }, { exact absurd (h.trans hj) hi }, { simpa using h } }, { intro i, simp only [order_embedding.coe_of_strict_mono], split_ifs with hi, { rw [nth_le_nth hi, nth_le_nth, ←hf], simp }, { rw [nth_len_le, nth_len_le], { simp }, { simpa using hi } } } } end /-- An element `x : α` of `l : list α` is a duplicate iff it can be found at two distinct indices `n m : ℕ` inside the list `l`. -/ lemma duplicate_iff_exists_distinct_nth_le {l : list α} {x : α} : l.duplicate x ↔ ∃ (n : ℕ) (hn : n < l.length) (m : ℕ) (hm : m < l.length) (h : n < m), x = l.nth_le n hn ∧ x = l.nth_le m hm := begin classical, rw [duplicate_iff_two_le_count, le_count_iff_repeat_sublist, sublist_iff_exists_fin_order_embedding_nth_le_eq], split, { rintro ⟨f, hf⟩, refine ⟨f ⟨0, by simp⟩, fin.is_lt _, f ⟨1, by simp⟩, fin.is_lt _, by simp, _, _⟩, { simpa using hf ⟨0, by simp⟩ }, { simpa using hf ⟨1, by simp⟩ } }, { rintro ⟨n, hn, m, hm, hnm, h, h'⟩, refine ⟨order_embedding.of_strict_mono (λ i, if (i : ℕ) = 0 then ⟨n, hn⟩ else ⟨m, hm⟩) _, _⟩, { rintros ⟨⟨_|i⟩, hi⟩ ⟨⟨_|j⟩, hj⟩, { simp }, { simp [hnm] }, { simp }, { simp only [nat.lt_succ_iff, nat.succ_le_succ_iff, repeat, length, nonpos_iff_eq_zero] at hi hj, simp [hi, hj] } }, { rintros ⟨⟨_|i⟩, hi⟩, { simpa using h }, { simpa using h' } } } end end sublist end list
da03b5840ec8f2e7294baa8f7349e8a9997ddccd
26ac254ecb57ffcb886ff709cf018390161a9225
/src/algebra/group_ring_action.lean
d721c07a81958039e284a8c6e488e7040ad4fe37
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
4,665
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Group action on rings. -/ import group_theory.group_action import data.equiv.ring import data.polynomial.eval universes u v variables (M G : Type u) [monoid M] [group G] variables (A R S F : Type v) [add_monoid A] [semiring R] [comm_semiring S] [field F] section prio set_option default_priority 100 -- see Note [default priority] /-- Typeclass for multiplicative actions by monoids on semirings. -/ class mul_semiring_action extends distrib_mul_action M R := (smul_one : ∀ (g : M), (g • 1 : R) = 1) (smul_mul : ∀ (g : M) (x y : R), g • (x * y) = (g • x) * (g • y)) end prio export mul_semiring_action (smul_one) variables {M R} lemma smul_mul' [mul_semiring_action M R] (g : M) (x y : R) : g • (x * y) = (g • x) * (g • y) := mul_semiring_action.smul_mul g x y variables (M R) /-- Each element of the monoid defines a additive monoid homomorphism. -/ def distrib_mul_action.to_add_monoid_hom [distrib_mul_action M A] (x : M) : A →+ A := { to_fun := (•) x, map_zero' := smul_zero x, map_add' := smul_add x } /-- Each element of the group defines an additive monoid isomorphism. -/ def distrib_mul_action.to_add_equiv [distrib_mul_action G A] (x : G) : A ≃+ A := { .. distrib_mul_action.to_add_monoid_hom G A x, .. mul_action.to_perm G A x } /-- The monoid of endomorphisms. -/ def monoid.End := M →* M instance monoid.End.monoid : monoid (monoid.End M) := { mul := monoid_hom.comp, one := monoid_hom.id M, mul_assoc := λ _ _ _, monoid_hom.comp_assoc _ _ _, mul_one := monoid_hom.comp_id, one_mul := monoid_hom.id_comp } instance monoid.End.inhabited : inhabited (monoid.End M) := ⟨1⟩ /-- The monoid of endomorphisms. -/ def add_monoid.End := A →+ A instance add_monoid.End.monoid : monoid (add_monoid.End A) := { mul := add_monoid_hom.comp, one := add_monoid_hom.id A, mul_assoc := λ _ _ _, add_monoid_hom.comp_assoc _ _ _, mul_one := add_monoid_hom.comp_id, one_mul := add_monoid_hom.id_comp } instance add_monoid.End.inhabited : inhabited (add_monoid.End A) := ⟨1⟩ /-- Each element of the group defines an additive monoid homomorphism. -/ def distrib_mul_action.hom_add_monoid_hom [distrib_mul_action M A] : M →* add_monoid.End A := { to_fun := distrib_mul_action.to_add_monoid_hom M A, map_one' := add_monoid_hom.ext $ λ x, one_smul M x, map_mul' := λ x y, add_monoid_hom.ext $ λ z, mul_smul x y z } /-- Each element of the monoid defines a semiring homomorphism. -/ def mul_semiring_action.to_semiring_hom [mul_semiring_action M R] (x : M) : R →+* R := { map_one' := smul_one x, map_mul' := smul_mul' x, .. distrib_mul_action.to_add_monoid_hom M R x } /-- Each element of the group defines a semiring isomorphism. -/ def mul_semiring_action.to_semiring_equiv [mul_semiring_action G R] (x : G) : R ≃+* R := { .. distrib_mul_action.to_add_equiv G R x, .. mul_semiring_action.to_semiring_hom G R x } section simp_lemmas variables {M G A R} attribute [simp] smul_one smul_mul' smul_zero smul_add @[simp] lemma smul_inv [mul_semiring_action M F] (x : M) (m : F) : x • m⁻¹ = (x • m)⁻¹ := (mul_semiring_action.to_semiring_hom M F x).map_inv _ @[simp] lemma smul_pow [mul_semiring_action M R] (x : M) (m : R) (n : ℕ) : x • m ^ n = (x • m) ^ n := nat.rec_on n (smul_one x) $ λ n ih, (smul_mul' x m (m ^ n)).trans $ congr_arg _ ih end simp_lemmas variables [mul_semiring_action M S] noncomputable instance : mul_semiring_action M (polynomial S) := { smul := λ m, polynomial.map $ mul_semiring_action.to_semiring_hom M S m, one_smul := λ p, by { ext n, erw polynomial.coeff_map, exact one_smul M (p.coeff n) }, mul_smul := λ m n p, by { ext i, iterate 3 { rw polynomial.coeff_map (mul_semiring_action.to_semiring_hom M S _) }, exact mul_smul m n (p.coeff i) }, smul_add := λ m p q, polynomial.map_add (mul_semiring_action.to_semiring_hom M S m), smul_zero := λ m, polynomial.map_zero (mul_semiring_action.to_semiring_hom M S m), smul_one := λ m, polynomial.map_one (mul_semiring_action.to_semiring_hom M S m), smul_mul := λ m p q, polynomial.map_mul (mul_semiring_action.to_semiring_hom M S m), } @[simp] lemma polynomial.coeff_smul' (m : M) (p : polynomial S) (n : ℕ) : (m • p).coeff n = m • p.coeff n := polynomial.coeff_map _ _ @[simp] lemma polynomial.smul_C (m : M) (r : S) : m • polynomial.C r = polynomial.C (m • r) := polynomial.map_C _ @[simp] lemma polynomial.smul_X (m : M) : (m • polynomial.X : polynomial S) = polynomial.X := polynomial.map_X _
6bfba26474698565c28a7f937211b929000ffe1b
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/topology/constructions.lean
c9b3a64fd56feaf9e3d5d9b6fc1b0af7babd36d4
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
33,861
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.maps /-! # Constructions of new topological spaces from old ones This file constructs products, sums, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, sum, disjoint union, subspace, quotient space -/ noncomputable theory open topological_space set filter open_locale classical topological_space filter universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} section constructions instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := induced coe t instance {r : α → α → Prop} [t : topological_space α] : topological_space (quot r) := coinduced (quot.mk r) t instance {s : setoid α} [t : topological_space α] : topological_space (quotient s) := coinduced quotient.mk t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := induced prod.fst t₁ ⊓ induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := coinduced sum.inl t₁ ⊔ coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨆a, coinduced (sigma.mk a) (t₂ a) instance Pi.topological_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨅a, induced (λf, f a) (t₂ a) instance ulift.topological_space [t : topological_space α] : topological_space (ulift.{v u} α) := t.induced ulift.down lemma quotient_dense_of_dense [setoid α] [topological_space α] {s : set α} (H : ∀ x, x ∈ closure s) : closure (quotient.mk '' s) = univ := eq_univ_of_forall $ λ x, begin rw mem_closure_iff, intros U U_op x_in_U, let V := quotient.mk ⁻¹' U, cases quotient.exists_rep x with y y_x, have y_in_V : y ∈ V, by simp only [mem_preimage, y_x, x_in_U], have V_op : is_open V := U_op, obtain ⟨w, w_in_V, w_in_range⟩ : (V ∩ s).nonempty := mem_closure_iff.1 (H y) V V_op y_in_V, exact ⟨_, w_in_V, mem_image_of_mem quotient.mk w_in_range⟩ end instance {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (subtype p) := ⟨bot_unique $ assume s hs, ⟨coe '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.coe_injective)⟩⟩ instance sum.discrete_topology [topological_space α] [topological_space β] [hα : discrete_topology α] [hβ : discrete_topology β] : discrete_topology (α ⊕ β) := ⟨by unfold sum.topological_space; simp [hα.eq_bot, hβ.eq_bot]⟩ instance sigma.discrete_topology {β : α → Type v} [Πa, topological_space (β a)] [h : Πa, discrete_topology (β a)] : discrete_topology (sigma β) := ⟨by { unfold sigma.topological_space, simp [λ a, (h a).eq_bot] }⟩ section topα variable [topological_space α] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ 𝓝 a ↔ ∃ u ∈ 𝓝 (a : α), coe ⁻¹' u ⊆ t := mem_nhds_induced coe a t theorem nhds_subtype (s : set α) (a : {x // x ∈ s}) : 𝓝 a = comap coe (𝓝 (a : α)) := nhds_induced coe a end topα end constructions section prod variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] @[continuity] lemma continuous_fst : continuous (@prod.fst α β) := continuous_inf_dom_left continuous_induced_dom lemma continuous_at_fst {p : α × β} : continuous_at prod.fst p := continuous_fst.continuous_at @[continuity] lemma continuous_snd : continuous (@prod.snd α β) := continuous_inf_dom_right continuous_induced_dom lemma continuous_at_snd {p : α × β} : continuous_at prod.snd p := continuous_snd.continuous_at @[continuity] lemma continuous.prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, (f x, g x)) := continuous_inf_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma continuous.prod_map {f : γ → α} {g : δ → β} (hf : continuous f) (hg : continuous g) : continuous (λ x : γ × δ, (f x.1, g x.2)) := (hf.comp continuous_fst).prod_mk (hg.comp continuous_snd) lemma filter.eventually.prod_inl_nhds {p : α → Prop} {a : α} (h : ∀ᶠ x in 𝓝 a, p x) (b : β) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).1 := continuous_at_fst h lemma filter.eventually.prod_inr_nhds {p : β → Prop} {b : β} (h : ∀ᶠ x in 𝓝 b, p x) (a : α) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).2 := continuous_at_snd h lemma filter.eventually.prod_mk_nhds {pa : α → Prop} {a} (ha : ∀ᶠ x in 𝓝 a, pa x) {pb : β → Prop} {b} (hb : ∀ᶠ y in 𝓝 b, pb y) : ∀ᶠ p in 𝓝 (a, b), pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl_nhds b).and (hb.prod_inr_nhds a) lemma continuous_swap : continuous (prod.swap : α × β → β × α) := continuous.prod_mk continuous_snd continuous_fst lemma is_open_prod {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open_inter (continuous_fst s hs) (continuous_snd t ht) lemma nhds_prod_eq {a : α} {b : β} : 𝓝 (a, b) = 𝓝 a ×ᶠ 𝓝 b := by rw [filter.prod, prod.topological_space, nhds_inf, nhds_induced, nhds_induced] instance [discrete_topology α] [discrete_topology β] : discrete_topology (α × β) := ⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_discrete α, nhds_discrete β, nhds_bot, filter.prod_pure_pure]⟩ lemma prod_mem_nhds_sets {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ 𝓝 a) (hb : t ∈ 𝓝 b) : set.prod s t ∈ 𝓝 (a, b) := by rw [nhds_prod_eq]; exact prod_mem_prod ha hb lemma nhds_swap (a : α) (b : β) : 𝓝 (a, b) = (𝓝 (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma filter.tendsto.prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : tendsto ma f (𝓝 a)) (hb : tendsto mb f (𝓝 b)) : tendsto (λc, (ma c, mb c)) f (𝓝 (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma filter.eventually.curry_nhds {p : α × β → Prop} {x : α} {y : β} (h : ∀ᶠ x in 𝓝 (x, y), p x) : ∀ᶠ x' in 𝓝 x, ∀ᶠ y' in 𝓝 y, p (x', y') := by { rw [nhds_prod_eq] at h, exact h.curry } lemma continuous_at.prod {f : α → β} {g : α → γ} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λx, (f x, g x)) x := hf.prod_mk_nhds hg lemma continuous_at.prod_map {f : α → γ} {g : β → δ} {p : α × β} (hf : continuous_at f p.fst) (hg : continuous_at g p.snd) : continuous_at (λ p : α × β, (f p.1, g p.2)) p := (hf.comp continuous_fst.continuous_at).prod (hg.comp continuous_snd.continuous_at) lemma continuous_at.prod_map' {f : α → γ} {g : β → δ} {x : α} {y : β} (hf : continuous_at f x) (hg : continuous_at g y) : continuous_at (λ p : α × β, (f p.1, g p.2)) (x, y) := have hf : continuous_at f (x, y).fst, from hf, have hg : continuous_at g (x, y).snd, from hg, hf.prod_map hg lemma prod_generate_from_generate_from_eq {α : Type*} {β : Type*} {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} := let G := generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} in le_antisymm (le_generate_from $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open_prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) (le_inf (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ : set.ext $ assume ⟨a, b⟩, by rw ← ht; simp [and.left_comm] {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this ▸ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this ▸ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) lemma prod_eq_generate_from : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (le_generate_from $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs ht) (le_inf (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨t, univ, by simpa [set.prod_eq] using ht⟩) (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨univ, t, by simpa [set.prod_eq] using ht⟩)) lemma is_open_prod_iff {s : set (α×β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := begin rw [is_open_iff_nhds], simp [nhds_prod_eq, mem_prod_iff], simp [mem_nhds_sets_iff], exact forall_congr (assume a, ball_congr $ assume b h, ⟨assume ⟨u', ⟨u, us, uo, au⟩, v', ⟨v, vs, vo, bv⟩, h⟩, ⟨u, uo, v, vo, au, bv, subset.trans (set.prod_mono us vs) h⟩, assume ⟨u, uo, v, vo, au, bv, h⟩, ⟨u, ⟨u, subset.refl u, uo, au⟩, v, ⟨v, subset.refl v, vo, bv⟩, h⟩⟩) end /-- Given an open neighborhood `s` of `(x, x)`, then `(x, x)` has a square open neighborhood that is a subset of `s`. -/ lemma exists_nhds_square {s : set (α × α)} (hs : is_open s) {x : α} (hx : (x, x) ∈ s) : ∃U, is_open U ∧ x ∈ U ∧ set.prod U U ⊆ s := begin rcases is_open_prod_iff.mp hs x x hx with ⟨u, v, hu, hv, h1x, h2x, h2s⟩, refine ⟨u ∩ v, is_open_inter hu hv, ⟨h1x, h2x⟩, subset.trans _ h2s⟩, simp only [prod_subset_prod_iff, inter_subset_left, true_or, inter_subset_right, and_self], end /-- The first projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_fst : is_open_map (@prod.fst α β) := begin assume s hs, rw is_open_iff_forall_mem_open, assume x xs, rw mem_image_eq at xs, rcases xs with ⟨⟨y₁, y₂⟩, ys, yx⟩, rcases is_open_prod_iff.1 hs _ _ ys with ⟨o₁, o₂, o₁_open, o₂_open, yo₁, yo₂, ho⟩, simp at yx, rw yx at yo₁, refine ⟨o₁, _, o₁_open, yo₁⟩, assume z zs, rw mem_image_eq, exact ⟨(z, y₂), ho (by simp [zs, yo₂]), rfl⟩ end /-- The second projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_snd : is_open_map (@prod.snd α β) := begin /- This lemma could be proved by composing the fact that the first projection is open, and exchanging coordinates is a homeomorphism, hence open. As the `prod_comm` homeomorphism is defined later, we rather go for the direct proof, copy-pasting the proof for the first projection. -/ assume s hs, rw is_open_iff_forall_mem_open, assume x xs, rw mem_image_eq at xs, rcases xs with ⟨⟨y₁, y₂⟩, ys, yx⟩, rcases is_open_prod_iff.1 hs _ _ ys with ⟨o₁, o₂, o₁_open, o₂_open, yo₁, yo₂, ho⟩, simp at yx, rw yx at yo₂, refine ⟨o₂, _, o₂_open, yo₂⟩, assume z zs, rw mem_image_eq, exact ⟨(y₁, z), ho (by simp [zs, yo₁]), rfl⟩ end /-- A product set is open in a product space if and only if each factor is open, or one of them is empty -/ lemma is_open_prod_iff' {s : set α} {t : set β} : is_open (set.prod s t) ↔ (is_open s ∧ is_open t) ∨ (s = ∅) ∨ (t = ∅) := begin cases (set.prod s t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, from prod_nonempty_iff.1 h, split, { assume H : is_open (set.prod s t), refine or.inl ⟨_, _⟩, show is_open s, { rw ← fst_image_prod s st.2, exact is_open_map_fst _ H }, show is_open t, { rw ← snd_image_prod st.1 t, exact is_open_map_snd _ H } }, { assume H, simp [st.1.ne_empty, st.2.ne_empty] at H, exact is_open_prod H.1 H.2 } } end lemma closure_prod_eq {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have (𝓝 a ×ᶠ 𝓝 b) ⊓ 𝓟 (set.prod s t) = (𝓝 a ⊓ 𝓟 s) ×ᶠ (𝓝 b ⊓ 𝓟 t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_cluster_pts, cluster_pt, nhds_prod_eq, this]; exact prod_ne_bot lemma mem_closure2 {s : set α} {t : set β} {u : set γ} {f : α → β → γ} {a : α} {b : β} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (hu : ∀a b, a ∈ s → b ∈ t → f a b ∈ u) : f a b ∈ closure u := have (a, b) ∈ closure (set.prod s t), by rw [closure_prod_eq]; from ⟨ha, hb⟩, show (λp:α×β, f p.1 p.2) (a, b) ∈ closure u, from mem_closure hf this $ assume ⟨a, b⟩ ⟨ha, hb⟩, hu a b ha hb lemma is_closed_prod {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := closure_eq_iff_is_closed.mp $ by simp only [h₁.closure_eq, h₂.closure_eq, closure_prod_eq] lemma inducing.prod_mk {f : α → β} {g : γ → δ} (hf : inducing f) (hg : inducing g) : inducing (λx:α×γ, (f x.1, g x.2)) := ⟨by rw [prod.topological_space, prod.topological_space, hf.induced, hg.induced, induced_compose, induced_compose, induced_inf, induced_compose, induced_compose]⟩ lemma embedding.prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := { inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.inj h₁, hg.inj h₂⟩, ..hf.to_inducing.prod_mk hg.to_inducing } protected lemma is_open_map.prod {f : α → β} {g : γ → δ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (λ p : α × γ, (f p.1, g p.2)) := begin rw [is_open_map_iff_nhds_le], rintros ⟨a, b⟩, rw [nhds_prod_eq, nhds_prod_eq, ← filter.prod_map_map_eq], exact filter.prod_mono (is_open_map_iff_nhds_le.1 hf a) (is_open_map_iff_nhds_le.1 hg b) end protected lemma open_embedding.prod {f : α → β} {g : γ → δ} (hf : open_embedding f) (hg : open_embedding g) : open_embedding (λx:α×γ, (f x.1, g x.2)) := open_embedding_of_embedding_open (hf.1.prod_mk hg.1) (hf.is_open_map.prod hg.is_open_map) lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λx, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id end prod section sum open sum variables [topological_space α] [topological_space β] [topological_space γ] @[continuity] lemma continuous_inl : continuous (@inl α β) := continuous_sup_rng_left continuous_coinduced_rng @[continuity] lemma continuous_inr : continuous (@inr α β) := continuous_sup_rng_right continuous_coinduced_rng @[continuity] lemma continuous_sum_rec {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := continuous_sup_dom hf hg lemma is_open_sum_iff {s : set (α ⊕ β)} : is_open s ↔ is_open (inl ⁻¹' s) ∧ is_open (inr ⁻¹' s) := iff.rfl lemma is_open_map_sum {f : α ⊕ β → γ} (h₁ : is_open_map (λ a, f (inl a))) (h₂ : is_open_map (λ b, f (inr b))) : is_open_map f := begin intros u hu, rw is_open_sum_iff at hu, cases hu with hu₁ hu₂, have : u = inl '' (inl ⁻¹' u) ∪ inr '' (inr ⁻¹' u), { ext (_|_); simp }, rw [this, set.image_union, set.image_image, set.image_image], exact is_open_union (h₁ _ hu₁) (h₂ _ hu₂) end lemma embedding_inl : embedding (@inl α β) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_left }, { intros u hu, existsi (inl '' u), change (is_open (inl ⁻¹' (@inl α β '' u)) ∧ is_open (inr ⁻¹' (@inl α β '' u))) ∧ inl ⁻¹' (inl '' u) = u, have : inl ⁻¹' (@inl α β '' u) = u := preimage_image_eq u (λ _ _, inl.inj_iff.mp), rw this, have : inr ⁻¹' (@inl α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume a ⟨b, _, h⟩, inl_ne_inr h), rw this, exact ⟨⟨hu, is_open_empty⟩, rfl⟩ } end, inj := λ _ _, inl.inj_iff.mp } lemma embedding_inr : embedding (@inr α β) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_right }, { intros u hu, existsi (inr '' u), change (is_open (inl ⁻¹' (@inr α β '' u)) ∧ is_open (inr ⁻¹' (@inr α β '' u))) ∧ inr ⁻¹' (inr '' u) = u, have : inl ⁻¹' (@inr α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume b ⟨a, _, h⟩, inr_ne_inl h), rw this, have : inr ⁻¹' (@inr α β '' u) = u := preimage_image_eq u (λ _ _, inr.inj_iff.mp), rw this, exact ⟨⟨is_open_empty, hu⟩, rfl⟩ } end, inj := λ _ _, inr.inj_iff.mp } lemma open_embedding_inl : open_embedding (inl : α → α ⊕ β) := { open_range := begin rw is_open_sum_iff, convert and.intro is_open_univ is_open_empty; { ext, simp } end, .. embedding_inl } lemma open_embedding_inr : open_embedding (inr : β → α ⊕ β) := { open_range := begin rw is_open_sum_iff, convert and.intro is_open_empty is_open_univ; { ext, simp } end, .. embedding_inr } end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma embedding_subtype_coe : embedding (coe : subtype p → α) := ⟨⟨rfl⟩, subtype.coe_injective⟩ @[continuity] lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_coe : continuous (coe : subtype p → α) := continuous_subtype_val lemma is_open.open_embedding_subtype_coe {s : set α} (hs : is_open s) : open_embedding (coe : s → α) := { induced := rfl, inj := subtype.coe_injective, open_range := (subtype.range_coe : range coe = s).symm ▸ hs } lemma is_open.is_open_map_subtype_coe {s : set α} (hs : is_open s) : is_open_map (coe : s → α) := hs.open_embedding_subtype_coe.is_open_map lemma is_open_map.restrict {f : α → β} (hf : is_open_map f) {s : set α} (hs : is_open s) : is_open_map (s.restrict f) := hf.comp hs.is_open_map_subtype_coe lemma is_closed.closed_embedding_subtype_coe {s : set α} (hs : is_closed s) : closed_embedding (coe : {x // x ∈ s} → α) := { induced := rfl, inj := subtype.coe_injective, closed_range := (subtype.range_coe : range coe = s).symm ▸ hs } @[continuity] lemma continuous_subtype_mk {f : β → α} (hp : ∀x, p (f x)) (h : continuous f) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma continuous_inclusion {s t : set α} (h : s ⊆ t) : continuous (inclusion h) := continuous_subtype_mk _ continuous_subtype_coe lemma continuous_at_subtype_coe {p : α → Prop} {a : subtype p} : continuous_at (coe : subtype p → α) a := continuous_iff_continuous_at.mp continuous_subtype_coe _ lemma map_nhds_subtype_coe_eq {a : α} (ha : p a) (h : {a | p a} ∈ 𝓝 a) : map (coe : subtype p → α) (𝓝 ⟨a, ha⟩) = 𝓝 a := map_nhds_induced_eq $ by simpa only [subtype.coe_mk, subtype.range_coe] using h lemma nhds_subtype_eq_comap {a : α} {h : p a} : 𝓝 (⟨a, h⟩ : subtype p) = comap coe (𝓝 a) := nhds_induced _ _ lemma tendsto_subtype_rng {β : Type*} {p : α → Prop} {b : filter β} {f : β → subtype p} : ∀{a:subtype p}, tendsto f b (𝓝 a) ↔ tendsto (λx, (f x : α)) b (𝓝 (a : α)) | ⟨a, ha⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff, subtype.coe_mk] lemma continuous_subtype_nhds_cover {ι : Sort*} {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ 𝓝 x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x)) : continuous f := continuous_iff_continuous_at.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ 𝓝 x)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in calc map f (𝓝 x) = map f (map coe (𝓝 x')) : congr_arg (map f) (map_nhds_subtype_coe_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x) (𝓝 x') : rfl ... ≤ 𝓝 (f x) : continuous_iff_continuous_at.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ι : Sort*} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed ((coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), from assume i, embedding_is_closed embedding_subtype_coe (by simp [subtype.range_coe]; exact h_is_closed i) (continuous_iff_is_closed.mp (f_cont i) _ hs), have is_closed (⋃i, (coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), from is_closed_Union_of_locally_finite (locally_finite_subset h_lf $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have f ⁻¹' s = (⋃i, (coe : {x | c i x} → α) '' (f ∘ coe ⁻¹' s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : ι), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simpa [and.comm, @and.left_comm (c _ _), ← exists_and_distrib_right], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ (x : α) ∈ closure ((coe : _ → α) '' s) := closure_induced $ assume x y, subtype.eq end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map_quot_mk : quotient_map (@quot.mk α r) := ⟨quot.exists_rep, rfl⟩ @[continuity] lemma continuous_quot_mk : continuous (@quot.mk α r) := continuous_coinduced_rng @[continuity] lemma continuous_quot_lift {f : α → β} (hr : ∀ a b, r a b → f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r → β) := continuous_coinduced_dom h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk α s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk α s) := continuous_coinduced_rng lemma continuous_quotient_lift {f : α → β} (hs : ∀ a b, a ≈ b → f a = f b) (h : continuous f) : continuous (quotient.lift f hs : quotient s → β) := continuous_coinduced_dom h end quotient section pi variables {ι : Type*} {π : ι → Type*} @[continuity] lemma continuous_pi [topological_space α] [∀i, topological_space (π i)] {f : α → Πi:ι, π i} (h : ∀i, continuous (λa, f a i)) : continuous f := continuous_infi_rng $ assume i, continuous_induced_rng $ h i @[continuity] lemma continuous_apply [∀i, topological_space (π i)] (i : ι) : continuous (λp:Πi, π i, p i) := continuous_infi_dom continuous_induced_dom /-- Embedding a factor into a product space (by fixing arbitrarily all the other coordinates) is continuous. -/ @[continuity] lemma continuous_update [decidable_eq ι] [∀i, topological_space (π i)] {i : ι} {f : Πi:ι, π i} : continuous (λ x : π i, function.update f i x) := begin refine continuous_pi (λj, _), by_cases h : j = i, { rw h, simpa using continuous_id }, { simpa [h] using continuous_const } end lemma nhds_pi [t : ∀i, topological_space (π i)] {a : Πi, π i} : 𝓝 a = (⨅i, comap (λx, x i) (𝓝 (a i))) := calc 𝓝 a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, π i, x i) (t i)) a) : nhds_infi ... = (⨅i, comap (λx, x i) (𝓝 (a i))) : by simp [nhds_induced] lemma is_open_set_pi [∀a, topological_space (π a)] {i : set ι} {s : Πa, set (π a)} (hi : finite i) (hs : ∀a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, continuous_apply a _ $ hs a ha) lemma pi_eq_generate_from [∀a, topological_space (π a)] : Pi.topological_space = generate_from {g | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (le_generate_from $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi) (le_infi $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨function.update (λa, univ) a t, {a}, by simpa using ht, by ext f; simp [s_eq.symm, pi]⟩) lemma pi_generate_from_eq {g : Πa, set (set (π a))} : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩, { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (λf:Πa, π a, f a)).is_open (t a), refine le_generate_from _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨function.update (λa, univ) a s, {a}, by simp [hs]⟩ } end lemma pi_generate_from_eq_fintype {g : Πa, set (set (π a))} [fintype ι] (hg : ∀a, ⋃₀ g a = univ) : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} := let G := {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} in begin rw [pi_generate_from_eq], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩, { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, choose c hc using show ∀a, ∃s, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, refine ⟨pi univ (λa, if a ∈ i then t a else (c : Πa, set (π a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, pi] at * }, { have : f ∈ pi {a | a ∉ i} c, { simp [*, pi] at * }, simpa [pi_if, hf] } } end end pi section sigma variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)] @[continuity] lemma continuous_sigma_mk {i : ι} : continuous (@sigma.mk ι σ i) := continuous_supr_rng continuous_coinduced_rng lemma is_open_sigma_iff {s : set (sigma σ)} : is_open s ↔ ∀ i, is_open (sigma.mk i ⁻¹' s) := by simp only [is_open_supr_iff, is_open_coinduced] lemma is_closed_sigma_iff {s : set (sigma σ)} : is_closed s ↔ ∀ i, is_closed (sigma.mk i ⁻¹' s) := is_open_sigma_iff lemma is_open_map_sigma_mk {i : ι} : is_open_map (@sigma.mk ι σ i) := begin intros s hs, rw is_open_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ sigma_mk_injective }, { convert is_open_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_open_range_sigma_mk {i : ι} : is_open (set.range (@sigma.mk ι σ i)) := by { rw ←set.image_univ, exact is_open_map_sigma_mk _ is_open_univ } lemma is_closed_map_sigma_mk {i : ι} : is_closed_map (@sigma.mk ι σ i) := begin intros s hs, rw is_closed_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ sigma_mk_injective }, { convert is_closed_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_closed_sigma_mk {i : ι} : is_closed (set.range (@sigma.mk ι σ i)) := by { rw ←set.image_univ, exact is_closed_map_sigma_mk _ is_closed_univ } lemma open_embedding_sigma_mk {i : ι} : open_embedding (@sigma.mk ι σ i) := open_embedding_of_continuous_injective_open continuous_sigma_mk sigma_mk_injective is_open_map_sigma_mk lemma closed_embedding_sigma_mk {i : ι} : closed_embedding (@sigma.mk ι σ i) := closed_embedding_of_continuous_injective_closed continuous_sigma_mk sigma_mk_injective is_closed_map_sigma_mk lemma embedding_sigma_mk {i : ι} : embedding (@sigma.mk ι σ i) := closed_embedding_sigma_mk.1 /-- A map out of a sum type is continuous if its restriction to each summand is. -/ @[continuity] lemma continuous_sigma [topological_space β] {f : sigma σ → β} (h : ∀ i, continuous (λ a, f ⟨i, a⟩)) : continuous f := continuous_supr_dom (λ i, continuous_coinduced_dom (h i)) @[continuity] lemma continuous_sigma_map {κ : Type*} {τ : κ → Type*} [Π k, topological_space (τ k)] {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (hf : ∀ i, continuous (f₂ i)) : continuous (sigma.map f₁ f₂) := continuous_sigma $ λ i, show continuous (λ a, sigma.mk (f₁ i) (f₂ i a)), from continuous_sigma_mk.comp (hf i) lemma is_open_map_sigma [topological_space β] {f : sigma σ → β} (h : ∀ i, is_open_map (λ a, f ⟨i, a⟩)) : is_open_map f := begin intros s hs, rw is_open_sigma_iff at hs, have : s = ⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s), { rw Union_image_preimage_sigma_mk_eq_self }, rw this, rw [image_Union], apply is_open_Union, intro i, rw [image_image], exact h i _ (hs i) end /-- The sum of embeddings is an embedding. -/ lemma embedding_sigma_map {τ : ι → Type*} [Π i, topological_space (τ i)] {f : Π i, σ i → τ i} (hf : ∀ i, embedding (f i)) : embedding (sigma.map id f) := begin refine ⟨⟨_⟩, function.injective_id.sigma_map (λ i, (hf i).inj)⟩, refine le_antisymm (continuous_iff_le_induced.mp (continuous_sigma_map (λ i, (hf i).continuous))) _, intros s hs, replace hs := is_open_sigma_iff.mp hs, have : ∀ i, ∃ t, is_open t ∧ f i ⁻¹' t = sigma.mk i ⁻¹' s, { intro i, apply is_open_induced_iff.mp, convert hs i, exact (hf i).induced.symm }, choose t ht using this, apply is_open_induced_iff.mpr, refine ⟨⋃ i, sigma.mk i '' t i, is_open_Union (λ i, is_open_map_sigma_mk _ (ht i).1), _⟩, ext ⟨i, x⟩, change (sigma.mk i (f i x) ∈ ⋃ (i : ι), sigma.mk i '' t i) ↔ x ∈ sigma.mk i ⁻¹' s, rw [←(ht i).2, mem_Union], split, { rintro ⟨j, hj⟩, rw mem_image at hj, rcases hj with ⟨y, hy₁, hy₂⟩, rcases sigma.mk.inj_iff.mp hy₂ with ⟨rfl, hy⟩, replace hy := eq_of_heq hy, subst y, exact hy₁ }, { intro hx, use i, rw mem_image, exact ⟨f i x, hx, rfl⟩ } end end sigma section ulift @[continuity] lemma continuous_ulift_down [topological_space α] : continuous (ulift.down : ulift.{v u} α → α) := continuous_induced_dom @[continuity] lemma continuous_ulift_up [topological_space α] : continuous (ulift.up : α → ulift.{v u} α) := continuous_induced_rng continuous_id end ulift lemma mem_closure_of_continuous [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : maps_to f s (closure t)) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... ⊆ closure (f '' s) : image_closure_subset_closure_image hf ... ⊆ closure t : closure_minimal h.image_subset is_closed_closure lemma mem_closure_of_continuous2 [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀a∈s, ∀b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (α×β) _ _ _ (λp:α×β, f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, p₂⟩ ⟨h₁, h₂⟩, h p₁ h₁ p₂ h₂
b440845e010774136adc6818564e4b6defc2f8ee
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/monoidal_categories/monoidal_structure_from_products.1.lean
b4d51686d6214206b8b780583bf1aebdb2ec4d7a
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
1,069
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import .monoidal_structure_from_products open tqft.categories open tqft.categories.functor open tqft.categories.products open tqft.categories.natural_transformation open tqft.categories.monoidal_category open tqft.categories.universal namespace tqft.categories.monoidal_category -- definition MonoidalStructure_from_Products { C : Category } [ has_FiniteProducts C ] : MonoidalStructure C := -- { -- tensor := TensorProduct_from_Products, -- tensor_unit := terminal_object, -- associator_transformation := Associator_for_Products, -- left_unitor := LeftUnitor_for_Products, -- right_unitor := RightUnitor_for_Products, -- pentagon := sorry, -- triangle := begin -- dunfold_everything' -- end -- } -- PROJECT show that this monoidal structure is uniquely braided -- PROJECT and that braiding is symmetric end tqft.categories.monoidal_category
56447c7d32bf2e236f66719588efcb5f35f538ff
1446f520c1db37e157b631385707cc28a17a595e
/src/Init/Lean/Expr.lean
fe3309c6aa349bd1d371dd6c007f8ce863aea6c4
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,342
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.HashMap import Init.Data.HashSet import Init.Data.PersistentHashMap import Init.Data.PersistentHashSet import Init.Lean.Data.KVMap import Init.Lean.Level namespace Lean inductive Literal | natVal (val : Nat) | strVal (val : String) instance Literal.inhabited : Inhabited Literal := ⟨Literal.natVal 0⟩ def Literal.hash : Literal → USize | Literal.natVal v => hash v | Literal.strVal v => hash v instance Literal.hashable : Hashable Literal := ⟨Literal.hash⟩ def Literal.beq : Literal → Literal → Bool | Literal.natVal v₁, Literal.natVal v₂ => v₁ == v₂ | Literal.strVal v₁, Literal.strVal v₂ => v₁ == v₂ | _, _ => false instance Literal.hasBeq : HasBeq Literal := ⟨Literal.beq⟩ def Literal.lt : Literal → Literal → Bool | Literal.natVal _, Literal.strVal _ => true | Literal.natVal v₁, Literal.natVal v₂ => v₁ < v₂ | Literal.strVal v₁, Literal.strVal v₂ => v₁ < v₂ | _, _ => false instance Literal.hasLess : HasLess Literal := ⟨fun a b => a.lt b⟩ inductive BinderInfo | default | implicit | strictImplicit | instImplicit | auxDecl def BinderInfo.hash : BinderInfo → USize | BinderInfo.default => 947 | BinderInfo.implicit => 1019 | BinderInfo.strictImplicit => 1087 | BinderInfo.instImplicit => 1153 | BinderInfo.auxDecl => 1229 def BinderInfo.isExplicit : BinderInfo → Bool | BinderInfo.implicit => false | BinderInfo.strictImplicit => false | BinderInfo.instImplicit => false | _ => true instance BinderInfo.hashable : Hashable BinderInfo := ⟨BinderInfo.hash⟩ instance BinderInfo.inhabited : Inhabited BinderInfo := ⟨BinderInfo.default⟩ def BinderInfo.isInstImplicit : BinderInfo → Bool | BinderInfo.instImplicit => true | _ => false def BinderInfo.isAuxDecl : BinderInfo → Bool | BinderInfo.auxDecl => true | _ => false protected def BinderInfo.beq : BinderInfo → BinderInfo → Bool | BinderInfo.default, BinderInfo.default => true | BinderInfo.implicit, BinderInfo.implicit => true | BinderInfo.strictImplicit, BinderInfo.strictImplicit => true | BinderInfo.instImplicit, BinderInfo.instImplicit => true | BinderInfo.auxDecl, BinderInfo.auxDecl => true | _, _ => false instance BinderInfo.hasBeq : HasBeq BinderInfo := ⟨BinderInfo.beq⟩ abbrev MData := KVMap abbrev MData.empty : MData := {KVMap .} instance MVData.hasEmptc : HasEmptyc MData := ⟨MData.empty⟩ /-- Cached hash code, cached results, and other data for `Expr`. hash : 32-bits hasFVar : 1-bit hasExprMVar : 1-bit hasLevelMVar : 1-bit hasLevelParam : 1-bit nonDepLet : 1-bit binderInfo : 3-bits looseBVarRange : 24-bits -/ def Expr.Data := UInt64 instance Expr.Data.inhabited : Inhabited Expr.Data := inferInstanceAs (Inhabited UInt64) def Expr.Data.hash (c : Expr.Data) : USize := c.toUInt32.toUSize instance Expr.Data.hasBeq : HasBeq Expr.Data := ⟨fun (a b : UInt64) => a == b⟩ def Expr.Data.looseBVarRange (c : Expr.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Expr.Data.hasFVar (c : Expr.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Expr.Data.hasExprMVar (c : Expr.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Expr.Data.hasLevelMVar (c : Expr.Data) : Bool := ((c.shiftRight 34).land 1) == 1 def Expr.Data.hasLevelParam (c : Expr.Data) : Bool := ((c.shiftRight 35).land 1) == 1 def Expr.Data.nonDepLet (c : Expr.Data) : Bool := ((c.shiftRight 36).land 1) == 1 @[extern c inline "(uint8_t)((#1 << 24) >> 61)"] def Expr.Data.binderInfo (c : Expr.Data) : BinderInfo := let bi := (c.shiftLeft 24).shiftRight 61; if bi == 0 then BinderInfo.default else if bi == 1 then BinderInfo.implicit else if bi == 2 then BinderInfo.strictImplicit else if bi == 3 then BinderInfo.instImplicit else BinderInfo.auxDecl @[extern c inline "(uint64_t)#1"] def BinderInfo.toUInt64 : BinderInfo → UInt64 | BinderInfo.default => 0 | BinderInfo.implicit => 1 | BinderInfo.strictImplicit => 2 | BinderInfo.instImplicit => 3 | BinderInfo.auxDecl => 4 @[inline] private def Expr.mkDataCore (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet : Bool) (bi : BinderInfo) : Expr.Data := if looseBVarRange > Nat.pow 2 24 - 1 then panic! "bound variable index is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasFVar.toUInt64.shiftLeft 32 + hasExprMVar.toUInt64.shiftLeft 33 + hasLevelMVar.toUInt64.shiftLeft 34 + hasLevelParam.toUInt64.shiftLeft 35 + nonDepLet.toUInt64.shiftLeft 36 + bi.toUInt64.shiftLeft 37 + looseBVarRange.toUInt64.shiftLeft 40; r def Expr.mkData (h : USize) (looseBVarRange : Nat := 0) (hasFVar hasExprMVar hasLevelMVar hasLevelParam : Bool := false) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam false BinderInfo.default def Expr.mkDataForBinder (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam : Bool) (bi : BinderInfo) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam false bi def Expr.mkDataForLet (h : USize) (looseBVarRange : Nat) (hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet : Bool) : Expr.Data := Expr.mkDataCore h looseBVarRange hasFVar hasExprMVar hasLevelMVar hasLevelParam nonDepLet BinderInfo.default open Expr abbrev MVarId := Name abbrev FVarId := Name /- We use the `E` suffix (short for `Expr`) to avoid collision with keywords. We considered using «...», but it is too inconvenient to use. -/ inductive Expr | bvar : Nat → Data → Expr -- bound variables | fvar : FVarId → Data → Expr -- free variables | mvar : MVarId → Data → Expr -- meta variables | sort : Level → Data → Expr -- Sort | const : Name → List Level → Data → Expr -- constants | app : Expr → Expr → Data → Expr -- application | lam : Name → Expr → Expr → Data → Expr -- lambda abstraction | forallE : Name → Expr → Expr → Data → Expr -- (dependent) arrow | letE : Name → Expr → Expr → Expr → Data → Expr -- let expressions | lit : Literal → Data → Expr -- literals | mdata : MData → Expr → Data → Expr -- metadata | proj : Name → Nat → Expr → Data → Expr -- projection -- IMPORTANT: the following constructor will be deleted | localE : Name → Name → Expr → Data → Expr -- Lean2 legacy. TODO: delete namespace Expr instance : Inhabited Expr := ⟨sort (arbitrary _) (arbitrary _)⟩ @[inline] def data : Expr → Data | bvar _ d => d | fvar _ d => d | mvar _ d => d | sort _ d => d | const _ _ d => d | app _ _ d => d | lam _ _ _ d => d | forallE _ _ _ d => d | letE _ _ _ _ d => d | lit _ d => d | mdata _ _ d => d | proj _ _ _ d => d | localE _ _ _ d => d def hash (e : Expr) : USize := e.data.hash instance : Hashable Expr := ⟨Expr.hash⟩ def hasFVar (e : Expr) : Bool := e.data.hasFVar def hasExprMVar (e : Expr) : Bool := e.data.hasExprMVar def hasLevelMVar (e : Expr) : Bool := e.data.hasLevelMVar def hasMVar (e : Expr) : Bool := let d := e.data; d.hasExprMVar || d.hasLevelMVar def hasLevelParam (e : Expr) : Bool := e.data.hasLevelParam def looseBVarRange (e : Expr) : Nat := e.data.looseBVarRange.toNat def binderInfo (e : Expr) : BinderInfo := e.data.binderInfo @[export lean_expr_hash] def hashEx : Expr → USize := hash @[export lean_expr_has_fvar] def hasFVarEx : Expr → Bool := hasFVar @[export lean_expr_has_expr_mvar] def hasExprMVarEx : Expr → Bool := hasExprMVar @[export lean_expr_has_level_mvar] def hasLevelMVarEx : Expr → Bool := hasLevelMVar @[export lean_expr_has_mvar] def hasMVarEx : Expr → Bool := hasMVar @[export lean_expr_has_level_param] def hasLevelParamEx : Expr → Bool := hasLevelParam @[export lean_expr_loose_bvar_range] def looseBVarRangeEx (e : Expr) : UInt32 := e.data.looseBVarRange @[export lean_expr_binder_info] def binderInfoEx : Expr → BinderInfo := binderInfo end Expr def mkLit (l : Literal) : Expr := Expr.lit l $ mkData (mixHash 3 (hash l)) def mkNatLit (n : Nat) : Expr := mkLit (Literal.natVal n) def mkStrLit (s : String) : Expr := mkLit (Literal.strVal s) def mkConst (n : Name) (lvls : List Level := []) : Expr := Expr.const n lvls $ mkData (mixHash 5 $ mixHash (hash n) (hash lvls)) 0 false false (lvls.any Level.hasMVar) (lvls.any Level.hasParam) def Literal.type : Literal → Expr | Literal.natVal _ => mkConst `Nat | Literal.strVal _ => mkConst `String @[export lean_lit_type] def Literal.typeEx : Literal → Expr := Literal.type def mkBVar (idx : Nat) : Expr := Expr.bvar idx $ mkData (mixHash 7 $ hash idx) (idx+1) def mkSort (lvl : Level) : Expr := Expr.sort lvl $ mkData (mixHash 11 $ hash lvl) 0 false false lvl.hasMVar lvl.hasParam def mkFVar (fvarId : FVarId) : Expr := Expr.fvar fvarId $ mkData (mixHash 13 $ hash fvarId) 0 true def mkMVar (fvarId : MVarId) : Expr := Expr.mvar fvarId $ mkData (mixHash 17 $ hash fvarId) 0 false true def mkMData (d : MData) (e : Expr) : Expr := Expr.mdata d e $ mkData (mixHash 19 $ hash e) e.looseBVarRange e.hasFVar e.hasExprMVar e.hasLevelMVar e.hasLevelParam def mkProj (s : Name) (i : Nat) (e : Expr) : Expr := Expr.proj s i e $ mkData (mixHash 23 $ mixHash (hash s) $ mixHash (hash i) (hash e)) e.looseBVarRange e.hasFVar e.hasExprMVar e.hasLevelMVar e.hasLevelParam def mkApp (f a : Expr) : Expr := Expr.app f a $ mkData (mixHash 29 $ mixHash (hash f) (hash a)) (Nat.max f.looseBVarRange a.looseBVarRange) (f.hasFVar || a.hasFVar) (f.hasExprMVar || a.hasExprMVar) (f.hasLevelMVar || a.hasLevelMVar) (f.hasLevelParam || a.hasLevelParam) def mkLambda (x : Name) (bi : BinderInfo) (t : Expr) (b : Expr) : Expr := let x := x.eraseMacroScopes; Expr.lam x t b $ mkDataForBinder (mixHash 31 $ mixHash (hash t) (hash b)) (Nat.max t.looseBVarRange (b.looseBVarRange - 1)) (t.hasFVar || b.hasFVar) (t.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || b.hasLevelParam) bi def mkForall (x : Name) (bi : BinderInfo) (t : Expr) (b : Expr) : Expr := let x := x.eraseMacroScopes; Expr.forallE x t b $ mkDataForBinder (mixHash 37 $ mixHash (hash t) (hash b)) (Nat.max t.looseBVarRange (b.looseBVarRange - 1)) (t.hasFVar || b.hasFVar) (t.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || b.hasLevelParam) bi def mkLet (x : Name) (t : Expr) (v : Expr) (b : Expr) (nonDep : Bool := false) : Expr := let x := x.eraseMacroScopes; Expr.letE x t v b $ mkDataForLet (mixHash 41 $ mixHash (hash t) $ mixHash (hash v) (hash b)) (Nat.max (Nat.max t.looseBVarRange v.looseBVarRange) (b.looseBVarRange - 1)) (t.hasFVar || v.hasFVar || b.hasFVar) (t.hasExprMVar || v.hasExprMVar || b.hasExprMVar) (t.hasLevelMVar || v.hasLevelMVar || b.hasLevelMVar) (t.hasLevelParam || v.hasLevelParam || b.hasLevelParam) nonDep -- TODO: delete def mkLocal (x u : Name) (t : Expr) (bi : BinderInfo) : Expr := Expr.localE x u t $ mkDataForBinder (mixHash 43 $ hash t) t.looseBVarRange true t.hasExprMVar t.hasLevelMVar t.hasLevelParam bi @[export lean_expr_mk_bvar] def mkBVarEx : Nat → Expr := mkBVar @[export lean_expr_mk_fvar] def mkFVarEx : FVarId → Expr := mkFVar @[export lean_expr_mk_mvar] def mkMVarEx : MVarId → Expr := mkMVar @[export lean_expr_mk_sort] def mkSortEx : Level → Expr := mkSort @[export lean_expr_mk_const] def mkConstEx (c : Name) (lvls : List Level) : Expr := mkConst c lvls @[export lean_expr_mk_app] def mkAppEx : Expr → Expr → Expr := mkApp @[export lean_expr_mk_lambda] def mkLambdaEx (n : Name) (d b : Expr) (bi : BinderInfo) : Expr := mkLambda n bi d b @[export lean_expr_mk_forall] def mkForallEx (n : Name) (d b : Expr) (bi : BinderInfo) : Expr := mkForall n bi d b @[export lean_expr_mk_let] def mkLetEx (n : Name) (t v b : Expr) : Expr := mkLet n t v b @[export lean_expr_mk_lit] def mkLitEx : Literal → Expr := mkLit @[export lean_expr_mk_mdata] def mkMDataEx : MData → Expr → Expr := mkMData @[export lean_expr_mk_proj] def mkProjEx : Name → Nat → Expr → Expr := mkProj @[export lean_expr_mk_local] def mkLocalEx : Name → Name → Expr → BinderInfo → Expr := mkLocal def mkAppN (f : Expr) (args : Array Expr) : Expr := args.foldl mkApp f private partial def mkAppRangeAux (n : Nat) (args : Array Expr) : Nat → Expr → Expr | i, e => if i < n then mkAppRangeAux (i+1) (mkApp e (args.get! i)) else e /-- `mkAppRange f i j #[a_1, ..., a_i, ..., a_j, ... ]` ==> the expression `f a_i ... a_{j-1}` -/ def mkAppRange (f : Expr) (i j : Nat) (args : Array Expr) : Expr := mkAppRangeAux j args i f def mkAppRev (fn : Expr) (revArgs : Array Expr) : Expr := revArgs.foldr (fun a r => mkApp r a) fn namespace Expr -- TODO: implement it in Lean @[extern "lean_expr_dbg_to_string"] constant dbgToString (e : @& Expr) : String := arbitrary String @[extern "lean_expr_quick_lt"] constant quickLt (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ @[extern "lean_expr_lt"] constant lt (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ /- Return true iff `a` and `b` are alpha equivalent. Binder annotations are ignored. -/ @[extern "lean_expr_eqv"] constant eqv (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ instance : HasBeq Expr := ⟨Expr.eqv⟩ /- Return true iff `a` and `b` are equal. Binder names and annotations are taking into account. -/ @[extern "lean_expr_equal"] constant equal (a : @& Expr) (b : @& Expr) : Bool := arbitrary _ def isSort : Expr → Bool | sort _ _ => true | _ => false def isBVar : Expr → Bool | bvar _ _ => true | _ => false def isMVar : Expr → Bool | mvar _ _ => true | _ => false def isFVar : Expr → Bool | fvar _ _ => true | _ => false def isApp : Expr → Bool | app _ _ _ => true | _ => false def isProj : Expr → Bool | proj _ _ _ _ => true | _ => false def isConst : Expr → Bool | const _ _ _ => true | _ => false def isConstOf : Expr → Name → Bool | const n _ _, m => n == m | _, _ => false def isForall : Expr → Bool | forallE _ _ _ _ => true | _ => false def isLambda : Expr → Bool | lam _ _ _ _ => true | _ => false def isBinding : Expr → Bool | lam _ _ _ _ => true | forallE _ _ _ _ => true | _ => false def isLet : Expr → Bool | letE _ _ _ _ _ => true | _ => false def isMData : Expr → Bool | mdata _ _ _ => true | _ => false def getAppFn : Expr → Expr | app f a _ => getAppFn f | e => e def getAppNumArgsAux : Expr → Nat → Nat | app f a _, n => getAppNumArgsAux f (n+1) | e, n => n def getAppNumArgs (e : Expr) : Nat := getAppNumArgsAux e 0 private def getAppArgsAux : Expr → Array Expr → Nat → Array Expr | app f a _, as, i => getAppArgsAux f (as.set! i a) (i-1) | _, as, _ => as @[inline] def getAppArgs (e : Expr) : Array Expr := let dummy := mkSort levelZero; let nargs := e.getAppNumArgs; getAppArgsAux e (mkArray nargs dummy) (nargs-1) private def getAppRevArgsAux : Expr → Array Expr → Array Expr | app f a _, as => getAppRevArgsAux f (as.push a) | _, as => as @[inline] def getAppRevArgs (e : Expr) : Array Expr := getAppRevArgsAux e (Array.mkEmpty e.getAppNumArgs) @[specialize] def withAppAux {α} (k : Expr → Array Expr → α) : Expr → Array Expr → Nat → α | app f a _, as, i => withAppAux f (as.set! i a) (i-1) | f, as, i => k f as @[inline] def withApp {α} (e : Expr) (k : Expr → Array Expr → α) : α := let dummy := mkSort levelZero; let nargs := e.getAppNumArgs; withAppAux k e (mkArray nargs dummy) (nargs-1) @[specialize] private def withAppRevAux {α} (k : Expr → Array Expr → α) : Expr → Array Expr → α | app f a _, as => withAppRevAux f (as.push a) | f, as => k f as @[inline] def withAppRev {α} (e : Expr) (k : Expr → Array Expr → α) : α := withAppRevAux k e (Array.mkEmpty e.getAppNumArgs) def getRevArgD : Expr → Nat → Expr → Expr | app f a _, 0, _ => a | app f _ _, i+1, v => getRevArgD f i v | _, _, v => v def getRevArg! : Expr → Nat → Expr | app f a _, 0 => a | app f _ _, i+1 => getRevArg! f i | _, _ => panic! "invalid index" @[inline] def getArg! (e : Expr) (i : Nat) (n := e.getAppNumArgs) : Expr := getRevArg! e (n - i - 1) @[inline] def getArgD (e : Expr) (i : Nat) (v₀ : Expr) (n := e.getAppNumArgs) : Expr := getRevArgD e (n - i - 1) v₀ def isAppOf (e : Expr) (n : Name) : Bool := match e.getAppFn with | const c _ _ => c == n | _ => false def isAppOfArity : Expr → Name → Nat → Bool | const c _ _, n, 0 => c == n | app f _ _, n, a+1 => isAppOfArity f n a | _, _, _ => false def appFn! : Expr → Expr | app f _ _ => f | _ => panic! "application expected" def appArg! : Expr → Expr | app _ a _ => a | _ => panic! "application expected" def constName! : Expr → Name | const n _ _ => n | _ => panic! "constant expected" def constLevels! : Expr → List Level | const _ ls _ => ls | _ => panic! "constant expected" def bvarIdx! : Expr → Nat | bvar idx _ => idx | _ => panic! "bvar expected" def fvarId! : Expr → FVarId | fvar n _ => n | _ => panic! "fvar expected" def mvarId! : Expr → MVarId | mvar n _ => n | _ => panic! "mvar expected" def bindingName! : Expr → Name | forallE n _ _ _ => n | lam n _ _ _ => n | _ => panic! "binding expected" def bindingDomain! : Expr → Expr | forallE _ _ d _ => d | lam _ _ d _ => d | _ => panic! "binding expected" def bindingBody! : Expr → Expr | forallE _ _ b _ => b | lam _ _ b _ => b | _ => panic! "binding expected" def letName! : Expr → Name | letE n _ _ _ _ => n | _ => panic! "let expression expected" def consumeMData : Expr → Expr | mdata _ e _ => consumeMData e | e => e def hasLooseBVars (e : Expr) : Bool := e.looseBVarRange > 0 @[extern "lean_expr_has_loose_bvar"] constant hasLooseBVar (e : @& Expr) (bvarIdx : @& Nat) : Bool := arbitrary _ /-- Lower the loose bound variables `>= s` in `e` by `d`. That is, a loose bound variable `bvar i`. `i >= s` is mapped into `bvar (i-d)`. Remark: if `s < d`, then result is `e` -/ @[extern "lean_expr_lower_loose_bvars"] constant lowerLooseBVars (e : @& Expr) (s d : @& Nat) : Expr := arbitrary _ /-- Lift loose bound variables `>= s` in `e` by `d`. -/ @[extern "lean_expr_lift_loose_bvars"] constant liftLooseBVars (e : @& Expr) (s d : @& Nat) : Expr := arbitrary _ /-- Instantiate the loose bound variables in `e` using `subst`. That is, a loose `Expr.bvar i` is replaced with `subst[i]`. -/ @[extern "lean_expr_instantiate"] constant instantiate (e : @& Expr) (subst : @& Array Expr) : Expr := arbitrary _ @[extern "lean_expr_instantiate1"] constant instantiate1 (e : @& Expr) (subst : @& Expr) : Expr := arbitrary _ /-- Similar to instantiate, but `Expr.bvar i` is replaced with `subst[subst.size - i - 1]` -/ @[extern "lean_expr_instantiate_rev"] constant instantiateRev (e : @& Expr) (subst : @& Array Expr) : Expr := arbitrary _ /-- Similar to `instantiate`, but consider only the variables `xs` in the range `[beginIdx, endIdx)`. Function panics if `beginIdx <= endIdx <= xs.size` does not hold. -/ @[extern "lean_expr_instantiate_range"] constant instantiateRange (e : @& Expr) (beginIdx endIdx : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ /-- Similar to `instantiateRev`, but consider only the variables `xs` in the range `[beginIdx, endIdx)`. Function panics if `beginIdx <= endIdx <= xs.size` does not hold. -/ @[extern "lean_expr_instantiate_rev_range"] constant instantiateRevRange (e : @& Expr) (beginIdx endIdx : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ /-- Replace free variables `xs` with loose bound variables. -/ @[extern "lean_expr_abstract"] constant abstract (e : @& Expr) (xs : @& Array Expr) : Expr := arbitrary _ /-- Similar to `abstract`, but consider only the first `min n xs.size` entries in `xs`. -/ @[extern "lean_expr_abstract_range"] constant abstractRange (e : @& Expr) (n : @& Nat) (xs : @& Array Expr) : Expr := arbitrary _ instance : HasToString Expr := ⟨Expr.dbgToString⟩ -- TODO: should not use dbgToString, but constructors. instance : HasRepr Expr := ⟨Expr.dbgToString⟩ end Expr def mkAppB (f a b : Expr) := mkApp (mkApp f a) b def mkApp2 (f a b : Expr) := mkAppB f a b def mkApp3 (f a b c : Expr) := mkApp (mkAppB f a b) c def mkApp4 (f a b c d : Expr) := mkAppB (mkAppB f a b) c d def mkApp5 (f a b c d e : Expr) := mkApp (mkApp4 f a b c d) e def mkApp6 (f a b c d e₁ e₂ : Expr) := mkAppB (mkApp4 f a b c d) e₁ e₂ def mkApp7 (f a b c d e₁ e₂ e₃ : Expr) := mkApp3 (mkApp4 f a b c d) e₁ e₂ e₃ def mkApp8 (f a b c d e₁ e₂ e₃ e₄ : Expr) := mkApp4 (mkApp4 f a b c d) e₁ e₂ e₃ e₄ def mkApp9 (f a b c d e₁ e₂ e₃ e₄ e₅ : Expr) := mkApp5 (mkApp4 f a b c d) e₁ e₂ e₃ e₄ e₅ def mkApp10 (f a b c d e₁ e₂ e₃ e₄ e₅ e₆ : Expr) := mkApp6 (mkApp4 f a b c d) e₁ e₂ e₃ e₄ e₅ e₆ def mkDecIsTrue (pred proof : Expr) := mkAppB (mkConst `Decidable.isTrue) pred proof def mkDecIsFalse (pred proof : Expr) := mkAppB (mkConst `Decidable.isFalse) pred proof abbrev ExprMap (α : Type) := HashMap Expr α abbrev PersistentExprMap (α : Type) := PHashMap Expr α abbrev ExprSet := HashSet Expr abbrev PersistentExprSet := PHashSet Expr abbrev PExprSet := PersistentExprSet /- Auxiliary type for forcing `==` to be structural equality for `Expr` -/ structure ExprStructEq := (val : Expr) instance exprToExprStructEq : HasCoe Expr ExprStructEq := ⟨ExprStructEq.mk⟩ namespace ExprStructEq protected def beq : ExprStructEq → ExprStructEq → Bool | ⟨e₁⟩, ⟨e₂⟩ => Expr.equal e₁ e₂ protected def hash : ExprStructEq → USize | ⟨e⟩ => e.hash instance : Inhabited ExprStructEq := ⟨{ val := arbitrary _ }⟩ instance : HasBeq ExprStructEq := ⟨ExprStructEq.beq⟩ instance : Hashable ExprStructEq := ⟨ExprStructEq.hash⟩ instance : HasToString ExprStructEq := ⟨fun e => toString e.val⟩ instance : HasRepr ExprStructEq := ⟨fun e => repr e.val⟩ end ExprStructEq abbrev ExprStructMap (α : Type) := HashMap ExprStructEq α abbrev PersistentExprStructMap (α : Type) := PHashMap ExprStructEq α namespace Expr private partial def mkAppRevRangeAux (revArgs : Array Expr) (start : Nat) : Expr → Nat → Expr | b, i => if i == start then b else let i := i - 1; mkAppRevRangeAux (mkApp b (revArgs.get! i)) i /-- `mkAppRevRange f b e args == mkAppRev f (revArgs.extract b e)` -/ def mkAppRevRange (f : Expr) (beginIdx endIdx : Nat) (revArgs : Array Expr) : Expr := mkAppRevRangeAux revArgs beginIdx f endIdx private def betaRevAux (revArgs : Array Expr) (sz : Nat) : Expr → Nat → Expr | Expr.lam _ _ b _, i => if i + 1 < sz then betaRevAux b (i+1) else let n := sz - (i + 1); mkAppRevRange (b.instantiateRange n sz revArgs) 0 n revArgs | Expr.mdata _ b _, i => betaRevAux b i | b, i => let n := sz - i; mkAppRevRange (b.instantiateRange n sz revArgs) 0 n revArgs /-- If `f` is a lambda expression, than "beta-reduce" it using `revArgs`. This function is often used with `getAppRev` or `withAppRev`. Examples: - `betaRev (fun x y => t x y) #[]` ==> `fun x y => t x y` - `betaRev (fun x y => t x y) #[a]` ==> `fun y => t a y` - `betaRev (fun x y => t x y) #[a, b]` ==> t b a` - `betaRev (fun x y => t x y) #[a, b, c, d]` ==> t d c b a` Suppose `t` is `(fun x y => t x y) a b c d`, then `args := t.getAppRev` is `#[d, c, b, a]`, and `betaRev (fun x y => t x y) #[d, c, b, a]` is `t a b c d`. -/ def betaRev (f : Expr) (revArgs : Array Expr) : Expr := if revArgs.size == 0 then f else betaRevAux revArgs revArgs.size f 0 def isHeadBetaTargetFn : Expr → Bool | Expr.lam _ _ _ _ => true | Expr.mdata _ b _ => isHeadBetaTargetFn b | _ => false def headBeta (e : Expr) : Expr := let f := e.getAppFn; if f.isHeadBetaTargetFn then betaRev f e.getAppRevArgs else e def isHeadBetaTarget (e : Expr) : Bool := e.getAppFn.isHeadBetaTargetFn private def etaExpandedBody : Expr → Nat → Nat → Option Expr | app f (bvar j _) _, n+1, i => if j == i then etaExpandedBody f n (i+1) else none | _, n+1, _ => none | f, 0, _ => if f.hasLooseBVars then none else some f private def etaExpandedAux : Expr → Nat → Option Expr | lam _ _ b _, n => etaExpandedAux b (n+1) | e, n => etaExpandedBody e n 0 /-- If `e` is of the form `(fun x₁ ... xₙ => f x₁ ... xₙ)` and `f` does not contain `x₁`, ..., `xₙ`, then return `some f`. Otherwise, return `none`. It assumes `e` does not have loose bound variables. Remark: `ₙ` may be 0 -/ def etaExpanded? (e : Expr) : Option Expr := etaExpandedAux e 0 /-- Similar to `etaExpanded?`, but only succeeds if `ₙ ≥ 1`. -/ def etaExpandedStrict? : Expr → Option Expr | lam _ _ b _ => etaExpandedAux b 1 | _ => none def getOptParamDefault? (e : Expr) : Option Expr := if e.isAppOfArity `optParam 2 then some e.appArg! else none def getAutoParamTactic? (e : Expr) : Option Expr := if e.isAppOfArity `autoParam 2 then some e.appArg! else none def isOptParam (e : Expr) : Bool := e.isAppOfArity `optParam 2 def isAutoParam (e : Expr) : Bool := e.isAppOfArity `autoParam 2 @[specialize] private partial def hasAnyFVarAux (p : FVarId → Bool) : Expr → Bool | e => if !e.hasFVar then false else match e with | Expr.forallE _ d b _ => hasAnyFVarAux d || hasAnyFVarAux b | Expr.lam _ d b _ => hasAnyFVarAux d || hasAnyFVarAux b | Expr.mdata _ e _ => hasAnyFVarAux e | Expr.letE _ t v b _ => hasAnyFVarAux t || hasAnyFVarAux v || hasAnyFVarAux b | Expr.app f a _ => hasAnyFVarAux f || hasAnyFVarAux a | Expr.proj _ _ e _ => hasAnyFVarAux e | Expr.localE _ _ _ _ => unreachable! | e@(Expr.fvar fvarId _) => p fvarId | e => false /-- Return true iff `e` contains a free variable which statisfies `p`. -/ @[inline] def hasAnyFVar (e : Expr) (p : FVarId → Bool) : Bool := hasAnyFVarAux p e /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_expr_update_app"] def updateApp (e : Expr) (newFn : Expr) (newArg : Expr) (h : e.isApp = true) : Expr := mkApp newFn newArg @[inline] def updateApp! (e : Expr) (newFn : Expr) (newArg : Expr) : Expr := match e with | app fn arg c => updateApp (app fn arg c) newFn newArg rfl | _ => panic! "application expected" @[extern "lean_expr_update_const"] def updateConst (e : Expr) (newLevels : List Level) (h : e.isConst = true) : Expr := mkConst e.constName! newLevels @[inline] def updateConst! (e : Expr) (newLevels : List Level) : Expr := match e with | const n ls c => updateConst (const n ls c) newLevels rfl | _ => panic! "constant expected" @[extern "lean_expr_update_sort"] def updateSort (e : Expr) (newLevel : Level) (h : e.isSort = true) : Expr := mkSort newLevel @[inline] def updateSort! (e : Expr) (newLevel : Level) : Expr := match e with | sort l c => updateSort (sort l c) newLevel rfl | _ => panic! "level expected" @[extern "lean_expr_update_proj"] def updateProj (e : Expr) (newExpr : Expr) (h : e.isProj = true) : Expr := match e with | proj s i _ _ => mkProj s i newExpr | _ => e -- unreachable because of `h` @[extern "lean_expr_update_mdata"] def updateMData (e : Expr) (newExpr : Expr) (h : e.isMData = true) : Expr := match e with | mdata d _ _ => mkMData d newExpr | _ => e -- unreachable because of `h` @[inline] def updateMData! (e : Expr) (newExpr : Expr) : Expr := match e with | mdata d e c => updateMData (mdata d e c) newExpr rfl | _ => panic! "mdata expected" @[inline] def updateProj! (e : Expr) (newExpr : Expr) : Expr := match e with | proj s i e c => updateProj (proj s i e c) newExpr rfl | _ => panic! "proj expected" @[extern "lean_expr_update_forall"] def updateForall (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) (h : e.isForall = true) : Expr := mkForall e.bindingName! newBinfo newDomain newBody @[inline] def updateForall! (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) : Expr := match e with | forallE n d b c => updateForall (forallE n d b c) newBinfo newDomain newBody rfl | _ => panic! "forall expected" @[inline] def updateForallE! (e : Expr) (newDomain : Expr) (newBody : Expr) : Expr := match e with | forallE n d b c => updateForall (forallE n d b c) c.binderInfo newDomain newBody rfl | _ => panic! "forall expected" @[extern "lean_expr_update_lambda"] def updateLambda (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) (h : e.isLambda = true) : Expr := mkLambda e.bindingName! newBinfo newDomain newBody @[inline] def updateLambda! (e : Expr) (newBinfo : BinderInfo) (newDomain : Expr) (newBody : Expr) : Expr := match e with | lam n d b c => updateLambda (lam n d b c) newBinfo newDomain newBody rfl | _ => panic! "lambda expected" @[inline] def updateLambdaE! (e : Expr) (newDomain : Expr) (newBody : Expr) : Expr := match e with | lam n d b c => updateLambda (lam n d b c) c.binderInfo newDomain newBody rfl | _ => panic! "lambda expected" @[extern "lean_expr_update_let"] def updateLet (e : Expr) (newType : Expr) (newVal : Expr) (newBody : Expr) (h : e.isLet = true) : Expr := mkLet e.letName! newType newVal newBody @[inline] def updateLet! (e : Expr) (newType : Expr) (newVal : Expr) (newBody : Expr) : Expr := match e with | letE n t v b c => updateLet (letE n t v b c) newType newVal newBody rfl | _ => panic! "let expression expected" def updateFn : Expr → Expr → Expr | e@(app f a _), g => e.updateApp (updateFn f g) a rfl | _, g => g /- Instantiate level parameters -/ namespace InstantiateLevelParams @[inline] def visit (f : Expr → Expr) (e : Expr) : Expr := if e.hasLevelParam then f e else e @[specialize] partial def instantiate (s : Name → Option Level) : Expr → Expr | e@(lam n d b _) => e.updateLambdaE! (visit instantiate d) (visit instantiate b) | e@(forallE n d b _) => e.updateForallE! (visit instantiate d) (visit instantiate b) | e@(letE n t v b _) => e.updateLet! (visit instantiate t) (visit instantiate v) (visit instantiate b) | e@(app f a _) => e.updateApp (visit instantiate f) (visit instantiate a) rfl | e@(proj _ _ s _) => e.updateProj (visit instantiate s) rfl | e@(mdata _ b _) => e.updateMData (visit instantiate b) rfl | e@(const _ us _) => e.updateConst (us.map (fun u => u.instantiateParams s)) rfl | e@(sort u _) => e.updateSort (u.instantiateParams s) rfl | localE _ _ _ _ => unreachable! | e => e end InstantiateLevelParams @[inline] def instantiateLevelParamsCore (s : Name → Option Level) (e : Expr) : Expr := if e.hasLevelParam then InstantiateLevelParams.instantiate s e else e private def getParamSubst : List Name → List Level → Name → Option Level | p::ps, u::us, p' => if p == p' then some u else getParamSubst ps us p' | _, _, _ => none def instantiateLevelParams (e : Expr) (paramNames : List Name) (lvls : List Level) : Expr := instantiateLevelParamsCore (getParamSubst paramNames lvls) e private partial def getParamSubstArray (ps : Array Name) (us : Array Level) (p' : Name) : Nat → Option Level | i => if h : i < ps.size then let p := ps.get ⟨i, h⟩; if h : i < us.size then let u := us.get ⟨i, h⟩; if p == p' then some u else getParamSubstArray (i+1) else none else none def instantiateLevelParamsArray (e : Expr) (paramNames : Array Name) (lvls : Array Level) : Expr := instantiateLevelParamsCore (fun p => getParamSubstArray paramNames lvls p 0) e end Expr def mkAnnotation (kind : Name) (e : Expr) : Expr := mkMData (KVMap.empty.insert kind (DataValue.ofBool true)) e def isAnnotation? (kind : Name) (e : Expr) : Option Expr := match e with | Expr.mdata d e _ => if d.size == 1 && d.getBool kind false then some e else none | _ => none end Lean
e07ebfc7308d8a8b093571a9157130feaf5e5291
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/equiv/basic.lean
9a8d779056b610faf4d66c5ff0ba277438d044bc
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
105,283
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import data.sigma.basic /-! # Equivalence between types In this file we define two types: * `equiv α β` a.k.a. `α ≃ β`: a bijective map `α → β` bundled with its inverse map; we use this (and not equality!) to express that various `Type`s or `Sort`s are equivalent. * `equiv.perm α`: the group of permutations `α ≃ α`. More lemmas about `equiv.perm` can be found in `group_theory/perm`. Then we define * canonical isomorphisms between various types: e.g., - `equiv.refl α` is the identity map interpreted as `α ≃ α`; - `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `α ⊕ β` and the sigma-type `Σ b : bool, cond b α β`; - `equiv.prod_sum_distrib : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ)` shows that type product and type sum satisfy the distributive law up to a canonical equivalence; * operations on equivalences: e.g., - `equiv.symm e : β ≃ α` is the inverse of `e : α ≃ β`; - `equiv.trans e₁ e₂ : α ≃ γ` is the composition of `e₁ : α ≃ β` and `e₂ : β ≃ γ` (note the order of the arguments!); - `equiv.prod_congr ea eb : α₁ × β₁ ≃ α₂ × β₂`: combine two equivalences `ea : α₁ ≃ α₂` and `eb : β₁ ≃ β₂` using `prod.map`. * definitions that transfer some instances along an equivalence. By convention, we transfer instances from right to left. - `equiv.inhabited` takes `e : α ≃ β` and `[inhabited β]` and returns `inhabited α`; - `equiv.unique` takes `e : α ≃ β` and `[unique β]` and returns `unique α`; - `equiv.decidable_eq` takes `e : α ≃ β` and `[decidable_eq β]` and returns `decidable_eq α`. More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance` does it for many algebraic type classes like `group`, `module`, etc. ## Tags equivalence, congruence, bijective map -/ open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ @[nolint has_inhabited_instance] structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : α → α) (h : involutive f) : α ≃ α := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl /-- The map `coe_fn : (r ≃ s) → (r → s)` is injective. -/ theorem coe_fn_injective : @function.injective (α ≃ β) (α → β) coe_fn | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from l₁.eq_right_inverse (this.symm ▸ r₂), by simp * @[simp, norm_cast] protected lemma coe_inj {e₁ e₂ : α ≃ β} : ⇑e₁ = e₂ ↔ e₁ = e₂ := coe_fn_injective.eq_iff @[ext] lemma ext {f g : equiv α β} (H : ∀ x, f x = g x) : f = g := coe_fn_injective (funext H) protected lemma congr_arg {f : equiv α β} : Π {x x' : α}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : equiv α β} (h : f = g) (x : α) : f x = g x := h ▸ rfl lemma ext_iff {f g : equiv α β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ @[ext] lemma perm.ext {σ τ : equiv.perm α} (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext H protected lemma perm.congr_arg {f : equiv.perm α} {x x' : α} : x = x' → f x = f x' := equiv.congr_arg protected lemma perm.congr_fun {f g : equiv.perm α} (h : f = g) (x : α) : f x = g x := equiv.congr_fun h x lemma perm.ext_iff {σ τ : equiv.perm α} : σ = τ ↔ ∀ x, σ x = τ x := ext_iff /-- Any type is equivalent to itself. -/ @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ instance inhabited' : inhabited (α ≃ α) := ⟨equiv.refl α⟩ /-- Inverse of an equivalence `e : α ≃ β`. -/ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- See Note [custom simps projection] -/ def simps.symm_apply (e : α ≃ β) : β → α := e.symm initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply) -- Generate the `simps` projections for previously defined equivs. attribute [simps] function.involutive.to_equiv /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm, e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : α ≃ β) : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe (e : α ≃ β) : e.inv_fun = e.symm := rfl protected theorem injective (e : α ≃ β) : injective e := e.left_inv.injective protected theorem surjective (e : α ≃ β) : surjective e := e.right_inv.surjective protected theorem bijective (f : α ≃ β) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : α ≃ β) [subsingleton β] : subsingleton α := e.injective.subsingleton protected theorem subsingleton.symm (e : α ≃ β) [subsingleton α] : subsingleton β := e.symm.injective.subsingleton lemma subsingleton_congr (e : α ≃ β) : subsingleton α ↔ subsingleton β := ⟨λ h, by exactI e.symm.subsingleton, λ h, by exactI e.subsingleton⟩ instance equiv_subsingleton_cod [subsingleton β] : subsingleton (α ≃ β) := ⟨λ f g, equiv.ext $ λ x, subsingleton.elim _ _⟩ instance equiv_subsingleton_dom [subsingleton α] : subsingleton (α ≃ β) := ⟨λ f g, equiv.ext $ λ x, @subsingleton.elim _ (equiv.subsingleton.symm f) _ _⟩ instance perm_unique [subsingleton α] : unique (perm α) := unique_of_subsingleton (equiv.refl α) lemma perm.subsingleton_eq_refl [subsingleton α] (e : perm α) : e = equiv.refl α := subsingleton.elim _ _ /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : α ≃ β) [decidable_eq β] : decidable_eq α := e.injective.decidable_eq lemma nonempty_congr (e : α ≃ β) : nonempty α ↔ nonempty β := nonempty.congr e e.symm protected lemma nonempty (e : α ≃ β) [nonempty β] : nonempty α := e.nonempty_congr.mpr ‹_› /-- If `α ≃ β` and `β` is inhabited, then so is `α`. -/ protected def inhabited [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ /-- If `α ≃ β` and `β` is a singleton type, then so is `α`. -/ protected def unique [unique β] (e : α ≃ β) : unique α := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl α) = id := rfl @[simp] theorem perm.coe_subsingleton {α : Type*} [subsingleton α] (e : perm α) : ⇑(e) = id := by rw [perm.subsingleton_eq_refl e, coe_refl] theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem coe_trans (f : α ≃ β) (g : β ≃ γ) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : α ≃ β) (x : β) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : α ≃ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : α ≃ β) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : α ≃ β) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl -- The `simp` attribute is needed to make this a `dsimp` lemma. -- `simp` will always rewrite with `equiv.symm_symm` before this has a chance to fire. @[simp, nolint simp_nf] theorem symm_symm_apply (f : α ≃ β) (b : α) : f.symm.symm b = f b := rfl @[simp] theorem apply_eq_iff_eq (f : α ≃ β) {x y : α} : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {α β : Sort*} (f : α ≃ β) {x : α} {y : β} : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl @[simp] theorem cast_symm {α β} (h : α = β) : (equiv.cast h).symm = equiv.cast h.symm := rfl @[simp] theorem cast_refl {α} (h : α = α := rfl) : equiv.cast h = equiv.refl α := rfl @[simp] theorem cast_trans {α β γ} (h : α = β) (h2 : β = γ) : (equiv.cast h).trans (equiv.cast h2) = equiv.cast (h.trans h2) := ext $ λ x, by { substs h h2, refl } lemma cast_eq_iff_heq {α β} (h : α = β) {a : α} {b : β} : equiv.cast h a = b ↔ a == b := by { subst h, simp } lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv @[simp] lemma injective_comp (e : α ≃ β) (f : β → γ) : injective (f ∘ e) ↔ injective f := injective.of_comp_iff' f e.bijective @[simp] lemma comp_injective (f : α → β) (e : β ≃ γ) : injective (e ∘ f) ↔ injective f := e.injective.of_comp_iff f @[simp] lemma surjective_comp (e : α ≃ β) (f : β → γ) : surjective (f ∘ e) ↔ surjective f := e.surjective.of_comp_iff f @[simp] lemma comp_surjective (f : α → β) (e : β ≃ γ) : surjective (e ∘ f) ↔ surjective f := surjective.of_comp_iff' e.bijective f @[simp] lemma bijective_comp (e : α ≃ β) (f : β → γ) : bijective (f ∘ e) ↔ bijective f := e.bijective.of_comp_iff f @[simp] lemma comp_bijective (f : α → β) (e : β ≃ γ) : bijective (e ∘ f) ↔ bijective f := bijective.of_comp_iff' e.bijective f /-- If `α` is equivalent to `β` and `γ` is equivalent to `δ`, then the type of equivalences `α ≃ γ` is equivalent to the type of equivalences `β ≃ δ`. -/ def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ @[simp] lemma equiv_congr_refl {α β} : (equiv.refl α).equiv_congr (equiv.refl β) = equiv.refl (α ≃ β) := by { ext, refl } @[simp] lemma equiv_congr_symm {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (ab.equiv_congr cd).symm = ab.symm.equiv_congr cd.symm := by { ext, refl } @[simp] lemma equiv_congr_trans {δ ε ζ} (ab : α ≃ β) (de : δ ≃ ε) (bc : β ≃ γ) (ef : ε ≃ ζ) : (ab.equiv_congr de).trans (bc.equiv_congr ef) = (ab.trans bc).equiv_congr (de.trans ef) := by { ext, refl } @[simp] lemma equiv_congr_refl_left {α β γ} (bg : β ≃ γ) (e : α ≃ β) : (equiv.refl α).equiv_congr bg e = e.trans bg := rfl @[simp] lemma equiv_congr_refl_right {α β} (ab e : α ≃ β) : ab.equiv_congr (equiv.refl β) e = ab.symm.trans e := rfl @[simp] lemma equiv_congr_apply_apply {δ} (ab : α ≃ β) (cd : γ ≃ δ) (e : α ≃ γ) (x) : ab.equiv_congr cd e x = cd (e (ab.symm x)) := rfl section perm_congr variables {α' β' : Type*} (e : α' ≃ β') /-- If `α` is equivalent to `β`, then `perm α` is equivalent to `perm β`. -/ def perm_congr : perm α' ≃ perm β' := equiv_congr e e lemma perm_congr_def (p : equiv.perm α') : e.perm_congr p = (e.symm.trans p).trans e := rfl @[simp] lemma perm_congr_refl : e.perm_congr (equiv.refl _) = equiv.refl _ := by simp [perm_congr_def] @[simp] lemma perm_congr_symm : e.perm_congr.symm = e.symm.perm_congr := rfl @[simp] lemma perm_congr_apply (p : equiv.perm α') (x) : e.perm_congr p x = e (p (e.symm x)) := rfl lemma perm_congr_symm_apply (p : equiv.perm β') (x) : e.perm_congr.symm p x = e.symm (p (e x)) := rfl lemma perm_congr_trans (p p' : equiv.perm α') : (e.perm_congr p).trans (e.perm_congr p') = e.perm_congr (p.trans p') := by { ext, simp } end perm_congr protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} : x ∈ f '' S ↔ f.symm x ∈ S := set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] @[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s := e.left_inverse_symm.image_image s lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective @[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.right_inverse_symm.preimage_preimage s @[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.left_inverse_symm.preimage_preimage s @[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t := e.surjective.preimage_subset_preimage_iff @[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t := set.image_subset_image_iff e.injective @[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t := set.image_eq_image e.injective lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t := set.preimage_eq_iff_eq_image e.bijective lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t := set.eq_preimage_iff_image_eq e.bijective /-- If `α` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (α : Sort u) [is_empty α] : α ≃ empty := ⟨is_empty_elim, λ e, e.rec _, is_empty_elim, λ e, e.rec _⟩ /-- `α` is equivalent to an empty type iff `α` is empty. -/ def equiv_empty_equiv (α : Sort u) : (α ≃ empty) ≃ is_empty α := ⟨λ e, function.is_empty e, @equiv_empty α, λ e, ext $ λ x, (e x).elim, λ p, rfl⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _ /-- If `α` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty (α : Sort v') [is_empty α] : α ≃ pempty.{u'} := ⟨is_empty_elim, λ e, e.rec _, is_empty_elim, λ e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _ /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty _ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def ulift {α : Type v} : ulift.{u} α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ /-- `plift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := λ x, rfl, right_inv := λ y, rfl } /-- If `α₁` is equivalent to `α₂` and `β₁` is equivalent to `β₂`, then the type of maps `α₁ → β₁` is equivalent to the type of maps `α₂ → β₂`. -/ @[congr, simps apply] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := { to_fun := λ f, e₂ ∘ f ∘ e₁.symm, inv_fun := λ f, e₂.symm ∘ f ∘ e₁, left_inv := λ f, funext $ λ x, by simp, right_inv := λ f, funext $ λ x, by simp } lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {α β : Sort*} : arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr, simps apply] def arrow_congr' {α₁ β₁ α₂ β₂ : Type*} (hα : α₁ ≃ α₂) (hβ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := equiv.arrow_congr hα hβ @[simp] lemma arrow_congr'_refl {α β : Type*} : arrow_congr' (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Type*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr' (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr' e₁ e₁').trans (arrow_congr' e₂ e₂') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr' e₁ e₂).symm = arrow_congr' e₁.symm e₂.symm := rfl /-- Conjugate a map `f : α → α` by an equivalence `α ≃ β`. -/ @[simps apply] def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e @[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl @[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : (e₁.trans e₂).conj = e₁.conj.trans e₂.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ f₂` with any `g` using -- `f₁ := g` and `f₂ := λ x, x`. This causes nontermination. lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) : e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) := by apply arrow_congr_comp section binary_op variables {α₁ β₁ : Type*} (e : α₁ ≃ β₁) (f : α₁ → α₁ → α₁) lemma semiconj_conj (f : α₁ → α₁) : semiconj e f (e.conj f) := λ x, by simp lemma semiconj₂_conj : semiconj₂ e f (e.arrow_congr e.conj f) := λ x y, by simp instance [is_associative α₁ f] : is_associative β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_associative_right e.surjective instance [is_idempotent α₁ f] : is_idempotent β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_idempotent_right e.surjective instance [is_left_cancel α₁ f] : is_left_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_left_cancel.left_cancel _ f _ x y z⟩ instance [is_right_cancel α₁ f] : is_right_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_right_cancel.right_cancel _ f _ x y z⟩ end binary_op /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩ /-- If `α` has a unique term, then the type of function `α → β` is equivalent to `β`. -/ @[simps] def fun_unique (α β) [unique α] : (α → β) ≃ β := { to_fun := λ f, f (default α), inv_fun := λ b a, b, left_inv := λ f, funext $ λ a, congr_arg f $ subsingleton.elim _ _, right_inv := λ b, rfl } /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := fun_unique _ _ /-- The sort of maps from `true` is equivalent to the codomain. -/ def true_arrow_equiv (α : Sort*) : (true → α) ≃ α := fun_unique _ _ /-- The sort of maps from a type that `is_empty` is equivalent to `punit`. -/ def arrow_punit_of_is_empty (α β : Sort*) [is_empty α] : (α → β) ≃ punit.{u} := ⟨λ f, punit.star, λ u, is_empty_elim, λ f, funext is_empty_elim, λ u, by { cases u, refl }⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := arrow_punit_of_is_empty _ _ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := arrow_punit_of_is_empty _ _ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := arrow_punit_of_is_empty _ _ end /-- Product of two equivalences. If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then `α₁ × β₁ ≃ α₂ × β₂`. -/ @[congr, simps apply] def prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := ⟨prod.map e₁ e₂, prod.map e₁.symm e₂.symm, λ ⟨a, b⟩, by simp, λ ⟨a, b⟩, by simp⟩ @[simp] theorem prod_congr_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (prod_congr e₁ e₂).symm = prod_congr e₁.symm e₂.symm := rfl /-- Type product is commutative up to an equivalence: `α × β ≃ β × α`. -/ @[simps apply] def prod_comm (α β : Type*) : α × β ≃ β × α := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma prod_comm_symm (α β) : (prod_comm α β).symm = prod_comm β α := rfl /-- Type product is associative up to an equivalence. -/ @[simps] def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) := ⟨λ p, (p.1.1, p.1.2, p.2), λp, ((p.1, p.2.1), p.2.2), λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u := by { ext, simp [and_assoc] } /-- Functions on `α × β` are equivalent to functions `α → β → γ`. -/ @[simps {fully_applied := ff}] def curry (α β γ : Type*) : (α × β → γ) ≃ (α → β → γ) := { to_fun := curry, inv_fun := uncurry, left_inv := uncurry_curry, right_inv := curry_uncurry } section /-- `punit` is a right identity for type product up to an equivalence. -/ @[simps] def prod_punit (α : Type*) : α × punit.{u+1} ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ /-- `punit` is a left identity for type product up to an equivalence. -/ @[simps] def punit_prod (α : Type*) : punit.{u+1} × α ≃ α := calc punit × α ≃ α × punit : prod_comm _ _ ... ≃ α : prod_punit _ /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (α : Type*) : α × empty ≃ empty := equiv_empty _ /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (α : Type*) : empty × α ≃ empty := equiv_empty _ /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (α : Type*) : α × pempty ≃ pempty := equiv_pempty _ /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (α : Type*) : pempty × α ≃ pempty := equiv_pempty _ end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (α β : Type*) : psum α β ≃ α ⊕ β := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ /-- If `α ≃ α'` and `β ≃ β'`, then `α ⊕ β ≃ α' ⊕ β'`. -/ @[simps apply] def sum_congr {α₁ β₁ α₂ β₂ : Type*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) : α₁ ⊕ β₁ ≃ α₂ ⊕ β₂ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, λ x, by simp, λ x, by simp⟩ @[simp] lemma sum_congr_trans {α₁ α₂ β₁ β₂ γ₁ γ₂ : Sort*} (e : α₁ ≃ β₁) (f : α₂ ≃ β₂) (g : β₁ ≃ γ₁) (h : β₂ ≃ γ₂) : (equiv.sum_congr e f).trans (equiv.sum_congr g h) = (equiv.sum_congr (e.trans g) (f.trans h)) := by { ext i, cases i; refl } @[simp] lemma sum_congr_symm {α β γ δ : Sort*} (e : α ≃ β) (f : γ ≃ δ) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl @[simp] lemma sum_congr_refl {α β : Sort*} : equiv.sum_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ⊕ β) := by { ext i, cases i; refl } namespace perm /-- Combine a permutation of `α` and of `β` into a permutation of `α ⊕ β`. -/ @[reducible] def sum_congr {α β : Type*} (ea : equiv.perm α) (eb : equiv.perm β) : equiv.perm (α ⊕ β) := equiv.sum_congr ea eb @[simp] lemma sum_congr_apply {α β : Type*} (ea : equiv.perm α) (eb : equiv.perm β) (x : α ⊕ β) : sum_congr ea eb x = sum.map ⇑ea ⇑eb x := equiv.sum_congr_apply ea eb x @[simp] lemma sum_congr_trans {α β : Sort*} (e : equiv.perm α) (f : equiv.perm β) (g : equiv.perm α) (h : equiv.perm β) : (sum_congr e f).trans (sum_congr g h) = sum_congr (e.trans g) (f.trans h) := equiv.sum_congr_trans e f g h @[simp] lemma sum_congr_symm {α β : Sort*} (e : equiv.perm α) (f : equiv.perm β) : (sum_congr e f).symm = sum_congr (e.symm) (f.symm) := equiv.sum_congr_symm e f @[simp] lemma sum_congr_refl {α β : Sort*} : sum_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ⊕ β) := equiv.sum_congr_refl end perm /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ @[simps apply] def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_symm (α β) : (sum_comm α β).symm = sum_comm β α := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ @[simps symm_apply] def sum_empty (α β : Type*) [is_empty β] : α ⊕ β ≃ α := ⟨sum.elim id is_empty_elim, inl, λ s, by { rcases s with _ | x, refl, exact is_empty_elim x }, λ a, rfl⟩ @[simp] lemma sum_empty_apply_inl {α β : Type*} [is_empty β] (a : α) : sum_empty α β (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ @[simps symm_apply] def empty_sum (α β : Type*) [is_empty α] : α ⊕ β ≃ β := (sum_comm _ _).trans $ sum_empty _ _ @[simp] lemma empty_sum_apply_inr {α β : Type*} [is_empty α] (b : β) : empty_sum α β (sum.inr b) = b := rfl /-- `option α` is equivalent to `α ⊕ punit` -/ def option_equiv_sum_punit (α : Type*) : option α ≃ α ⊕ punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {α} : option_equiv_sum_punit α none = sum.inr punit.star := rfl @[simp] lemma option_equiv_sum_punit_some {α} (a) : option_equiv_sum_punit α (some a) = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_coe {α} (a : α) : option_equiv_sum_punit α a = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_symm_inl {α} (a) : (option_equiv_sum_punit α).symm (sum.inl a) = a := rfl @[simp] lemma option_equiv_sum_punit_symm_inr {α} (a) : (option_equiv_sum_punit α).symm (sum.inr a) = none := rfl /-- The set of `x : option α` such that `is_some x` is equivalent to `α`. -/ def option_is_some_equiv (α : Type*) : {x : option α // x.is_some} ≃ α := { to_fun := λ o, option.get o.2, inv_fun := λ x, ⟨some x, dec_trivial⟩, left_inv := λ o, subtype.eq $ option.some_get _, right_inv := λ x, option.get_some _ _ } /-- The product over `option α` of `β a` is the binary product of the product over `α` of `β (some α)` and `β none` -/ @[simps] def pi_option_equiv_prod {α : Type*} {β : option α → Type*} : (Π a : option α, β a) ≃ (β none × Π a : α, β (some a)) := { to_fun := λ f, (f none, λ a, f (some a)), inv_fun := λ x a, option.cases_on a x.fst x.snd, left_inv := λ f, funext $ λ a, by cases a; refl, right_inv := λ x, by simp } /-- `α ⊕ β` is equivalent to a `sigma`-type over `bool`. Note that this definition assumes `α` and `β` to be types from the same universe, so it cannot by used directly to transfer theorems about sigma types to theorems about sum types. In many cases one can use `ulift` to work around this difficulty. -/ def sum_equiv_sigma_bool (α β : Type u) : α ⊕ β ≃ (Σ b: bool, cond b α β) := ⟨λ s, s.elim (λ x, ⟨tt, x⟩) (λ x, ⟨ff, x⟩), λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : α → β` is the natural equivalence between the type of all fibres of `f` and the total space `α`. -/ @[simps] def sigma_preimage_equiv {α β : Type*} (f : α → β) : (Σ y : β, {x // f x = y}) ≃ α := ⟨λ x, ↑x.2, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩ /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } end section sum_compl /-- For any predicate `p` on `α`, the sum of the two subtypes `{a // p a}` and its complement `{a // ¬ p a}` is naturally equivalent to `α`. See `subtype_or_equiv` for sum types over subtypes `{x // p x}` and `{x // q x}` that are not necessarily `is_compl p q`. -/ def sum_compl {α : Type*} (p : α → Prop) [decidable_pred p] : {a // p a} ⊕ {a // ¬ p a} ≃ α := { to_fun := sum.elim coe coe, inv_fun := λ a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := λ a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // ¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : ¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h /-- Combines an `equiv` between two subtypes with an `equiv` between their complements to form a permutation. -/ def subtype_congr {α : Type*} {p q : α → Prop} [decidable_pred p] [decidable_pred q] (e : {x // p x} ≃ {x // q x}) (f : {x // ¬p x} ≃ {x // ¬q x}) : perm α := (sum_compl p).symm.trans ((sum_congr e f).trans (sum_compl q)) open equiv variables {ε : Type*} {p : ε → Prop} [decidable_pred p] variables (ep ep' : perm {a // p a}) (en en' : perm {a // ¬ p a}) /-- Combining permutations on `ε` that permute only inside or outside the subtype split induced by `p : ε → Prop` constructs a permutation on `ε`. -/ def perm.subtype_congr : equiv.perm ε := perm_congr (sum_compl p) (sum_congr ep en) lemma perm.subtype_congr.apply (a : ε) : ep.subtype_congr en a = if h : p a then ep ⟨a, h⟩ else en ⟨a, h⟩ := by { by_cases h : p a; simp [perm.subtype_congr, h] } @[simp] lemma perm.subtype_congr.left_apply {a : ε} (h : p a) : ep.subtype_congr en a = ep ⟨a, h⟩ := by simp [perm.subtype_congr.apply, h] @[simp] lemma perm.subtype_congr.left_apply_subtype (a : {a // p a}) : ep.subtype_congr en a = ep a := by { convert perm.subtype_congr.left_apply _ _ a.property, simp } @[simp] lemma perm.subtype_congr.right_apply {a : ε} (h : ¬ p a) : ep.subtype_congr en a = en ⟨a, h⟩ := by simp [perm.subtype_congr.apply, h] @[simp] lemma perm.subtype_congr.right_apply_subtype (a : {a // ¬ p a}) : ep.subtype_congr en a = en a := by { convert perm.subtype_congr.right_apply _ _ a.property, simp } @[simp] lemma perm.subtype_congr.refl : perm.subtype_congr (equiv.refl {a // p a}) (equiv.refl {a // ¬ p a}) = equiv.refl ε := by { ext x, by_cases h : p x; simp [h] } @[simp] lemma perm.subtype_congr.symm : (ep.subtype_congr en).symm = perm.subtype_congr ep.symm en.symm := begin ext x, by_cases h : p x, { have : p (ep.symm ⟨x, h⟩) := subtype.property _, simp [perm.subtype_congr.apply, h, symm_apply_eq, this] }, { have : ¬ p (en.symm ⟨x, h⟩) := subtype.property (en.symm _), simp [perm.subtype_congr.apply, h, symm_apply_eq, this] } end @[simp] lemma perm.subtype_congr.trans : (ep.subtype_congr en).trans (ep'.subtype_congr en') = perm.subtype_congr (ep.trans ep') (en.trans en') := begin ext x, by_cases h : p x, { have : p (ep ⟨x, h⟩) := subtype.property _, simp [perm.subtype_congr.apply, h, this] }, { have : ¬ p (en ⟨x, h⟩) := subtype.property (en _), simp [perm.subtype_congr.apply, h, symm_apply_eq, this] } end end sum_compl section subtype_preimage variables (p : α → Prop) [decidable_pred p] (x₀ : {a // p a} → β) /-- For a fixed function `x₀ : {a // p a} → β` defined on a subtype of `α`, the subtype of functions `x : α → β` that agree with `x₀` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // ¬ p a} → β`. -/ @[simps] def subtype_preimage : {x : α → β // x ∘ coe = x₀} ≃ ({a // ¬ p a} → β) := { to_fun := λ (x : {x : α → β // x ∘ coe = x₀}) a, (x : α → β) a, inv_fun := λ x, ⟨λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩, funext $ λ ⟨a, h⟩, dif_pos h⟩, left_inv := λ ⟨x, hx⟩, subtype.val_injective $ funext $ λ a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := λ x, funext $ λ ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } lemma subtype_preimage_symm_apply_coe_pos (x : {a // ¬ p a} → β) (a : α) (h : p a) : ((subtype_preimage p x₀).symm x : α → β) a = x₀ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // ¬ p a} → β) (a : α) (h : ¬ p a) : ((subtype_preimage p x₀).symm x : α → β) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Σ i, β i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). This is `sigma.curry` and `sigma.uncurry` together as an equiv. -/ def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : Σ i, β i, γ x.1 x.2) ≃ (Π a b, γ a b) := { to_fun := sigma.curry, inv_fun := sigma.uncurry, left_inv := sigma.uncurry_curry, right_inv := sigma.curry_uncurry } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ @[simps apply symm_apply] def psigma_equiv_sigma {α} (β : α → Sort*) : (Σ' i, β i) ≃ Σ i, β i := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Σ a, β₁ a` and `Σ a, β₂ a`. -/ @[simps apply] def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Σ a, β₁ a) ≃ Σ a, β₂ a := ⟨λ a, ⟨a.1, F a.1 a.2⟩, λ a, ⟨a.1, (F a.1).symm a.2⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ @[simp] lemma sigma_congr_right_trans {α} {β₁ β₂ β₃ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) (G : Π a, β₂ a ≃ β₃ a) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (λ a, (F a).trans (G a)) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_symm {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (sigma_congr_right F).symm = sigma_congr_right (λ a, (F a).symm) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_refl {α} {β : α → Sort*} : (sigma_congr_right (λ a, equiv.refl (β a))) = equiv.refl (Σ a, β a) := by { ext1 x, cases x, refl } namespace perm /-- A family of permutations `Π a, perm (β a)` generates a permuation `perm (Σ a, β₁ a)`. -/ @[reducible] def sigma_congr_right {α} {β : α → Sort*} (F : Π a, perm (β a)) : perm (Σ a, β a) := equiv.sigma_congr_right F @[simp] lemma sigma_congr_right_trans {α} {β : α → Sort*} (F : Π a, perm (β a)) (G : Π a, perm (β a)) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (λ a, (F a).trans (G a)) := equiv.sigma_congr_right_trans F G @[simp] lemma sigma_congr_right_symm {α} {β : α → Sort*} (F : Π a, perm (β a)) : (sigma_congr_right F).symm = sigma_congr_right (λ a, (F a).symm) := equiv.sigma_congr_right_symm F @[simp] lemma sigma_congr_right_refl {α} {β : α → Sort*} : (sigma_congr_right (λ a, equiv.refl (β a))) = equiv.refl (Σ a, β a) := equiv.sigma_congr_right_refl end perm /-- An equivalence `f : α₁ ≃ α₂` generates an equivalence between `Σ a, β (f a)` and `Σ a, β a`. -/ @[simps apply] def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} (e : α₁ ≃ α₂) : (Σ a:α₁, β (e a)) ≃ (Σ a:α₂, β a) := ⟨λ a, ⟨e a.1, a.2⟩, λ a, ⟨e.symm a.1, @@eq.rec β a.2 (e.right_inv a.1).symm⟩, λ ⟨a, b⟩, match e.symm (e a), e.left_inv a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ e) _ (@@eq.rec β b (congr_arg e h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match e (e.symm a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ α₂} {β : α₁ → Sort*} (f : α₁ ≃ α₂) : (Σ a:α₁, β a) ≃ (Σ a:α₂, β (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ α₂} {β₁ : α₁ → Sort*} {β₂ : α₂ → Sort*} (f : α₁ ≃ α₂) (F : ∀ a, β₁ a ≃ β₂ (f a)) : sigma β₁ ≃ sigma β₂ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ @[simps apply symm_apply] def sigma_equiv_prod (α β : Type*) : (Σ_:α, β) ≃ α × β := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : Π a, β₁ a ≃ β) : sigma β₁ ≃ α × β := (sigma_congr_right F).trans (sigma_equiv_prod α β) /-- Dependent product of types is associative up to an equivalence. -/ def sigma_assoc {α : Type*} {β : α → Type*} (γ : Π (a : α), β a → Type*) : (Σ (ab : Σ (a : α), β a), γ ab.1 ab.2) ≃ Σ (a : α), (Σ (b : β a), γ a b) := { to_fun := λ x, ⟨x.1.1, ⟨x.1.2, x.2⟩⟩, inv_fun := λ x, ⟨⟨x.1, x.2.1⟩, x.2.2⟩, left_inv := λ ⟨⟨a, b⟩, c⟩, rfl, right_inv := λ ⟨a, ⟨b, c⟩⟩, rfl } end section prod_congr variables {α₁ β₁ β₂ : Type*} (e : α₁ → β₁ ≃ β₂) /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `β₁ × α₁` and `β₂ × α₁`. -/ def prod_congr_left : β₁ × α₁ ≃ β₂ × α₁ := { to_fun := λ ab, ⟨e ab.2 ab.1, ab.2⟩, inv_fun := λ ab, ⟨(e ab.2).symm ab.1, ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_left_apply (b : β₁) (a : α₁) : prod_congr_left e (b, a) = (e a b, a) := rfl lemma prod_congr_refl_right (e : β₁ ≃ β₂) : prod_congr e (equiv.refl α₁) = prod_congr_left (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `α₁ × β₁` and `α₁ × β₂`. -/ def prod_congr_right : α₁ × β₁ ≃ α₁ × β₂ := { to_fun := λ ab, ⟨ab.1, e ab.1 ab.2⟩, inv_fun := λ ab, ⟨ab.1, (e ab.1).symm ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_right_apply (a : α₁) (b : β₁) : prod_congr_right e (a, b) = (a, e a b) := rfl lemma prod_congr_refl_left (e : β₁ ≃ β₂) : prod_congr (equiv.refl α₁) e = prod_congr_right (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_left_trans_prod_comm : (prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_right_trans_prod_comm : (prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_congr_right_sigma_equiv_prod : (sigma_congr_right e).trans (sigma_equiv_prod α₁ β₂) = (sigma_equiv_prod α₁ β₁).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_equiv_prod_sigma_congr_right : (sigma_equiv_prod α₁ β₁).symm.trans (sigma_congr_right e) = (prod_congr_right e).trans (sigma_equiv_prod α₁ β₂).symm := by { ext ⟨a, b⟩ : 1, simp } /-- A variation on `equiv.prod_congr` where the equivalence in the second component can depend on the first component. A typical example is a shear mapping, explaining the name of this declaration. -/ @[simps {fully_applied := ff}] def prod_shear {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : α₁ → β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := { to_fun := λ x : α₁ × β₁, (e₁ x.1, e₂ x.1 x.2), inv_fun := λ y : α₂ × β₂, (e₁.symm y.1, (e₂ $ e₁.symm y.1).symm y.2), left_inv := by { rintro ⟨x₁, y₁⟩, simp only [symm_apply_apply] }, right_inv := by { rintro ⟨x₁, y₁⟩, simp only [apply_symm_apply] } } end prod_congr namespace perm variables {α₁ β₁ β₂ : Type*} [decidable_eq α₁] (a : α₁) (e : perm β₁) /-- `prod_extend_right a e` extends `e : perm β` to `perm (α × β)` by sending `(a, b)` to `(a, e b)` and keeping the other `(a', b)` fixed. -/ def prod_extend_right : perm (α₁ × β₁) := { to_fun := λ ab, if ab.fst = a then (a, e ab.snd) else ab, inv_fun := λ ab, if ab.fst = a then (a, e.symm ab.snd) else ab, left_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] }, right_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] } } @[simp] lemma prod_extend_right_apply_eq (b : β₁) : prod_extend_right a e (a, b) = (a, e b) := if_pos rfl lemma prod_extend_right_apply_ne {a a' : α₁} (h : a' ≠ a) (b : β₁) : prod_extend_right a e (a', b) = (a', b) := if_neg h lemma eq_of_prod_extend_right_ne {e : perm β₁} {a a' : α₁} {b : β₁} (h : prod_extend_right a e (a', b) ≠ (a', b)) : a' = a := by { contrapose! h, exact prod_extend_right_apply_ne _ h _ } @[simp] lemma fst_prod_extend_right (ab : α₁ × β₁) : (prod_extend_right a e ab).fst = ab.fst := begin rw [prod_extend_right, coe_fn_mk], split_ifs with h, { rw h }, { refl } end end perm section /-- The type of functions to a product `α × β` is equivalent to the type of pairs of functions `γ → α` and `γ → β`. -/ def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by { cases p, refl }⟩ open sum /-- The type of functions on a sum type `α ⊕ β` is equivalent to the type of pairs of functions on `α` and on `β`. -/ def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p, sum.elim p.1 p.2, λ f, by { ext ⟨⟩; refl }, λ p, by { cases p, refl }⟩ @[simp] lemma sum_arrow_equiv_prod_arrow_apply_fst {α β γ} (f : (α ⊕ β) → γ) (a : α) : (sum_arrow_equiv_prod_arrow α β γ f).1 a = f (inl a) := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_apply_snd {α β γ} (f : (α ⊕ β) → γ) (b : β) : (sum_arrow_equiv_prod_arrow α β γ f).2 b = f (inr b) := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inl {α β γ} (f : α → γ) (g : β → γ) (a : α) : ((sum_arrow_equiv_prod_arrow α β γ).symm (f, g)) (inl a) = f a := rfl @[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inr {α β γ} (f : α → γ) (g : β → γ) (b : β) : ((sum_arrow_equiv_prod_arrow α β γ).symm (f, g)) (inr b) = g b := rfl /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) := calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _ ... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _ ... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Σ i, α i`) by a type `β` is equivalent to the sum of products `Σ i, (α i × β)`. -/ def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) : ((Σ i, α i) × β) ≃ (Σ i, (α i × β)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, λ p, (⟨p.1, p.2.1⟩, p.2.2), λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool × α` is equivalent to `α ⊕ α`. -/ def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α := calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit × α) ⊕ (unit × α) : sum_prod_distrib _ _ _ ... ≃ α ⊕ α : sum_congr (punit_prod _) (punit_prod _) /-- The function type `bool → α` is equivalent to `α × α`. -/ @[simps] def bool_arrow_equiv_prod (α : Type u) : (bool → α) ≃ α × α := { to_fun := λ f, (f tt, f ff), inv_fun := λ p b, cond b p.1 p.2, left_inv := λ f, funext $ bool.forall_bool.2 ⟨rfl, rfl⟩, right_inv := λ ⟨x, y⟩, rfl } end section open sum nat /-- The set of natural numbers is equivalent to `ℕ ⊕ punit`. -/ def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `ℕ ⊕ punit` is equivalent to `ℕ`. -/ def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `ℕ ⊕ ℕ`. -/ def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `α` and `β` generates an equivalence between `list α` and `list β`. -/ def list_equiv_of_equiv {α β : Type*} (e : α ≃ β) : list α ≃ list β := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := λ l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := λ l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ /-- If `α` is equivalent to `β`, then `unique α` is equivalent to `unique β`. -/ def unique_congr (e : α ≃ β) : unique α ≃ unique β := { to_fun := λ h, @equiv.unique _ _ h e.symm, inv_fun := λ h, @equiv.unique _ _ h e, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is equivalent to `β`, then `is_empty α` is equivalent to `is_empty β`. -/ lemma is_empty_congr (e : α ≃ β) : is_empty α ↔ is_empty β := ⟨λ h, @function.is_empty _ _ h e.symm, λ h, @function.is_empty _ _ h e⟩ protected lemma is_empty (e : α ≃ β) [is_empty β] : is_empty α := e.is_empty_congr.mpr ‹_› section open subtype /-- If `α` is equivalent to `β` and the predicates `p : α → Prop` and `q : β → Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. For the statement where `α = β`, that is, `e : perm α`, see `perm.subtype_perm`. -/ def subtype_equiv {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} := ⟨λ x, ⟨e x, (h _).1 x.2⟩, λ y, ⟨e.symm y, (h _).2 (by { simp, exact y.2 })⟩, λ ⟨x, h⟩, subtype.ext_val $ by simp, λ ⟨y, h⟩, subtype.ext_val $ by simp⟩ @[simp] lemma subtype_equiv_refl {p : α → Prop} (h : ∀ a, p a ↔ p (equiv.refl _ a) := λ a, iff.rfl) : (equiv.refl α).subtype_equiv h = equiv.refl {a : α // p a} := by { ext, refl } @[simp] lemma subtype_equiv_symm {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) : (e.subtype_equiv h).symm = e.symm.subtype_equiv (λ a, by { convert (h $ e.symm a).symm, exact (e.apply_symm_apply a).symm }) := rfl @[simp] lemma subtype_equiv_trans {p : α → Prop} {q : β → Prop} {r : γ → Prop} (e : α ≃ β) (f : β ≃ γ) (h : ∀ (a : α), p a ↔ q (e a)) (h' : ∀ (b : β), q b ↔ r (f b)): (e.subtype_equiv h).trans (f.subtype_equiv h') = (e.trans f).subtype_equiv (λ a, (h a).trans (h' $ e a)) := rfl @[simp] lemma subtype_equiv_apply {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) (x : {x // p x}) : e.subtype_equiv h x = ⟨e x, (h _).1 x.2⟩ := rfl /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ @[simps] def subtype_equiv_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_equiv (equiv.refl _) e /-- If `α ≃ β`, then for any predicate `p : β → Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : β → Prop} (e : α ≃ β) : {a : α // p (e a)} ≃ {b : β // p b} := subtype_equiv e $ by simp /-- If `α ≃ β`, then for any predicate `p : α → Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) : {a : α // p a} ≃ {b : β // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_equiv_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q := subtype_equiv (equiv.refl α) (assume a, h ▸ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_equiv_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the “inner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ @[simp] lemma subtype_subtype_equiv_subtype_exists_apply {α : Type u} (p : α → Prop) (q : subtype p → Prop) (a) : (subtype_subtype_equiv_subtype_exists p q a : α) = a := by { cases a, cases a_val, refl } /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) : {x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_equiv_right $ λ x, exists_prop @[simp] lemma subtype_subtype_equiv_subtype_inter_apply {α : Type u} (p q : α → Prop) (a) : (subtype_subtype_equiv_subtype_inter p q a : α) = a := by { cases a, cases a_val, refl } /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_equiv_right $ assume x, ⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩ @[simp] lemma subtype_subtype_equiv_subtype_apply {α : Type u} {p q : α → Prop} (h : ∀ x, q x → p x) (a : {x : subtype p // q x.1}) : (subtype_subtype_equiv_subtype h a : α) = a := by { cases a, cases a_val, refl } /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ @[simps apply symm_apply] def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) : subtype p ≃ α := ⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) : { y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ ⟨⟨x, h⟩, y⟩, rfl, λ ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop) (h : ∀ x, p x → q x) : (Σ x : subtype q, p x) ≃ Σ x : α, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2 /-- If a predicate `p : β → Prop` is true on the range of a map `f : α → β`, then `Σ y : {y // p y}, {x // f x = y}` is equivalent to `α`. -/ def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop) (h : ∀ x, p (f x)) : (Σ y : subtype p, {x : α // f x = y}) ≃ α := calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x) ... ≃ α : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Σ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β) {p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) : (Σ y : subtype q, {x : α // f x = y}) ≃ subtype p := calc (Σ y : subtype q, {x : α // f x = y}) ≃ Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_equiv_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- A sigma type over an `option` is equivalent to the sigma set over the original type, if the fiber is empty at none. -/ def sigma_option_equiv_of_some {α : Type u} (p : option α → Type v) (h : p none → false) : (Σ x : option α, p x) ≃ (Σ x : α, p (some x)) := begin have h' : ∀ x, p x → x.is_some, { intro x, cases x, { intro n, exfalso, exact h n }, { intro s, exact rfl } }, exact (sigma_subtype_equiv_of_subset _ _ h').symm.trans (sigma_congr_left' (option_is_some_equiv α)), end /-- The `pi`-type `Π i, π i` is equivalent to the type of sections `f : ι → Σ i, π i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Π a, β a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Π a, {b : β a // p a b}`. -/ def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} : {f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {α : Type u} {β : Type v} {p : α → Prop} {q : β → Prop} : {c : α × β // p c.1 ∧ q c.2} ≃ ({a // p a} × {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ /-- A subtype of a `prod` is equivalent to a sigma type whose fibers are subtypes. -/ def subtype_prod_equiv_sigma_subtype {α β : Type*} (p : α → β → Prop) : {x : α × β // p x.1 x.2} ≃ Σ a, {b : β // p a b} := { to_fun := λ x, ⟨x.1.1, x.1.2, x.prop⟩, inv_fun := λ x, ⟨⟨x.1, x.2⟩, x.2.prop⟩, left_inv := λ x, by ext; refl, right_inv := λ ⟨a, b, pab⟩, rfl } /-- The type `Π (i : α), β i` can be split as a product by separating the indices in `α` depending on whether they satisfy a predicate `p` or not. -/ @[simps] def pi_equiv_pi_subtype_prod {α : Type*} (p : α → Prop) (β : α → Type*) [decidable_pred p] : (Π (i : α), β i) ≃ (Π (i : {x // p x}), β i) × (Π (i : {x // ¬ p x}), β i) := { to_fun := λ f, (λ x, f x, λ x, f x), inv_fun := λ f x, if h : p x then f.1 ⟨x, h⟩ else f.2 ⟨x, h⟩, right_inv := begin rintros ⟨f, g⟩, ext1; { ext y, rcases y, simp only [y_property, dif_pos, dif_neg, not_false_iff, subtype.coe_mk], refl }, end, left_inv := λ f, begin ext x, by_cases h : p x; { simp only [h, dif_neg, dif_pos, not_false_iff], refl }, end } end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X → Y` with prescribed values for all `x' ≠ x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : {g : X → Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // ¬ x' ≠ x} _ $ show unique {x' // ¬ x' ≠ x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := λ ⟨x', h⟩, subtype.val_injective h }) (subtype_equiv_right $ λ a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : (subtype_equiv_codomain f : {g : X → Y // g ∘ coe = f} → Y) = λ g, (g : X → Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' ≠ x} → Y) (g : {g : X → Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X → Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' ≠ x} → Y) : ((subtype_equiv_codomain f).symm : Y → {g : X → Y // g ∘ coe = f}) = λ y, ⟨λ x', if h : x' ≠ x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X → Y) x' = if h : x' ≠ x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' ≠ x} → Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X → Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) (h : x' ≠ x) : ((subtype_equiv_codomain f).symm y : X → Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain /-- A set is equivalent to its image under an equivalence. -/ -- We could construct this using `equiv.set.image e s e.injective`, -- but this definition provides an explicit inverse. @[simps] def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s := { to_fun := λ x, ⟨e x.1, by simp⟩, inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩, left_inv := λ x, by simp, right_inv := λ y, by simp, }. namespace set open set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty _ /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)] [decidable_pred (∈ t)] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_equiv e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) @[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f) (x : α) (h : x ∈ s) : (set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ := begin apply (set.image f s H).injective, simp [(set.image f s H).apply_symm_apply], end lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this], end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ @[simps] protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } /-- If `s` is a set in `range f`, then its image under `range_splitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) : range_splitting f '' s ≃ s := { to_fun := λ x, ⟨⟨f x, by simp⟩, (by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩, inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩, left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] }, right_inv := λ x, by simp [apply_range_splitting f], } end set /-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the range of `f`. While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like `linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def of_left_inverse {α β : Sort*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : α ≃ set.range f := { to_fun := λ a, ⟨f a, a, rfl⟩, inv_fun := λ b, f_inv (nonempty_of_exists b.2) b, left_inv := λ a, hf ⟨a⟩ a, right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b, from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha } /-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`. Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `of_left_inverse`. -/ abbreviation of_left_inverse' {α β : Sort*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : α ≃ set.range f := of_left_inverse f (λ _, f_inv) (λ _, hf) /-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/ @[simps apply] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ set.range f := equiv.of_left_inverse f (λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf) theorem apply_of_injective_symm {α β} (f : α → β) (hf : injective f) (b : set.range f) : f ((of_injective f hf).symm b) = b := subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b @[simp] theorem of_injective_symm_apply {α β} (f : α → β) (hf : injective f) (a : α) : (of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := begin apply (of_injective f hf).injective, simp [apply_of_injective_symm f hf], end @[simp] lemma self_comp_of_injective_symm {α β} (f : α → β) (hf : injective f) : f ∘ ((of_injective f hf).symm) = coe := funext (λ x, apply_of_injective_symm f hf x) lemma of_left_inverse_eq_of_injective {α β : Type*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : of_left_inverse f f_inv hf = of_injective f ((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by { haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) := by { ext, simp } lemma of_left_inverse'_eq_of_injective {α β : Type*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : of_left_inverse' f f_inv hf = of_injective f hf.injective := by { ext, simp } /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ @[simps apply] noncomputable def of_bijective {α β} (f : α → β) (hf : bijective f) : α ≃ β := (of_injective f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ β lemma of_bijective_apply_symm_apply {α β} (f : α → β) (hf : bijective f) (x : β) : f ((of_bijective f hf).symm x) = x := (of_bijective f hf).apply_symm_apply x @[simp] lemma of_bijective_symm_apply_apply {α β} (f : α → β) (hf : bijective f) (x : α) : (of_bijective f hf).symm (f x) = x := (of_bijective f hf).symm_apply_apply x section variables {α' β' : Type*} (e : perm α') {p : β' → Prop} [decidable_pred p] (f : α' ≃ subtype p) /-- Extend the domain of `e : equiv.perm α` to one that is over `β` via `f : α → subtype p`, where `p : β → Prop`, permuting only the `b : β` that satisfy `p b`. This can be used to extend the domain across a function `f : α → β`, keeping everything outside of `set.range f` fixed. For this use-case `equiv` given by `f` can be constructed by `equiv.of_left_inverse'` or `equiv.of_left_inverse` when there is a known inverse, or `equiv.of_injective` in the general case.`. -/ def perm.extend_domain : perm β' := (perm_congr f e).subtype_congr (equiv.refl _) @[simp] lemma perm.extend_domain_apply_image (a : α') : e.extend_domain f (f a) = f (e a) := by simp [perm.extend_domain] lemma perm.extend_domain_apply_subtype {b : β'} (h : p b) : e.extend_domain f b = f (e (f.symm ⟨b, h⟩)) := by simp [perm.extend_domain, h] lemma perm.extend_domain_apply_not_subtype {b : β'} (h : ¬ p b) : e.extend_domain f b = b := by simp [perm.extend_domain, h] @[simp] lemma perm.extend_domain_refl : perm.extend_domain (equiv.refl _) f = equiv.refl _ := by simp [perm.extend_domain] @[simp] lemma perm.extend_domain_symm : (e.extend_domain f).symm = perm.extend_domain e.symm f := rfl lemma perm.extend_domain_trans (e e' : perm α') : (e.extend_domain f).trans (e'.extend_domain f) = perm.extend_domain (e.trans e') f := by simp [perm.extend_domain, perm_congr_trans] end /-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `α` be a setoid with equivalence relation `~`. Let `p₂` be a predicate on the quotient type `α/~`, and `p₁` be the lift of this predicate to `α`: `p₁ a ↔ p₂ ⟦a⟧`. Let `~₂` be the restriction of `~` to `{x // p₁ x}`. Then `{x // p₂ x}` is equivalent to the quotient of `{x // p₁ x}` by `~₂`. -/ def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α] [s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧) (h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) : {x // p₂ x} ≃ quotient s₂ := { to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧) (λ a b hab, hfunext (by rw quotient.sound hab) (λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x})) (λ a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha, right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) } section swap variable [decidable_eq α] /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ @[simp] theorem swap_self (a : α) : swap a a = equiv.refl _ := ext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := ext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ λ x, swap_core_swap_core _ _ _ @[simp] lemma symm_swap (a b : α) : (swap a b).symm = swap a b := rfl @[simp] lemma swap_eq_refl_iff {x y : α} : swap x y = equiv.refl _ ↔ x = y := begin refine ⟨λ h, (equiv.refl _).injective _, λ h, h ▸ (swap_self _)⟩, rw [←h, swap_apply_left, h, refl_apply] end theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by { cases π, refl } lemma swap_eq_update (i j : α) : ⇑(equiv.swap i j) = update (update id j i) i j := funext $ λ x, by rw [update_apply _ i j, update_apply _ j i, equiv.swap_apply_def, id.def] lemma comp_swap_eq_update (i j : α) (f : α → β) : f ∘ equiv.swap i j = update (update f j (f i)) i (f j) := by rw [swap_eq_update, comp_update, comp_update, comp.right_id] @[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma trans_swap_trans_symm [decidable_eq β] (a b : β) (e : α ≃ β) : (e.trans (swap a b)).trans e.symm = swap (e.symm a) (e.symm b) := symm_trans_swap_trans a b e.symm @[simp] lemma swap_apply_self (i j a : α) : swap i j (swap i j a) = a := by rw [← equiv.trans_apply, equiv.swap_swap, equiv.refl_apply] /-- A function is invariant to a swap if it is equal at both elements -/ lemma apply_swap_eq_self {v : α → β} {i j : α} (hv : v i = v j) (k : α) : v (swap i j k) = v k := begin by_cases hi : k = i, { rw [hi, swap_apply_left, hv] }, by_cases hj : k = j, { rw [hj, swap_apply_right, hv] }, rw swap_apply_of_ne_of_ne hi hj, end lemma swap_apply_eq_iff {x y z w : α} : swap x y z = w ↔ z = swap x y w := by rw [apply_eq_iff_eq_symm_apply, symm_swap] lemma swap_apply_ne_self_iff {a b x : α} : swap a b x ≠ x ↔ a ≠ b ∧ (x = a ∨ x = b) := begin by_cases hab : a = b, { simp [hab] }, by_cases hax : x = a, { simp [hax, eq_comm] }, by_cases hbx : x = b, { simp [hbx] }, simp [hab, hax, hbx, swap_apply_of_ne_of_ne] end namespace perm @[simp] lemma sum_congr_swap_refl {α β : Sort*} [decidable_eq α] [decidable_eq β] (i j : α) : equiv.perm.sum_congr (equiv.swap i j) (equiv.refl β) = equiv.swap (sum.inl i) (sum.inl j) := begin ext x, cases x, { simp [sum.map, swap_apply_def], split_ifs; refl}, { simp [sum.map, swap_apply_of_ne_of_ne] }, end @[simp] lemma sum_congr_refl_swap {α β : Sort*} [decidable_eq α] [decidable_eq β] (i j : β) : equiv.perm.sum_congr (equiv.refl α) (equiv.swap i j) = equiv.swap (sum.inr i) (sum.inr j) := begin ext x, cases x, { simp [sum.map, swap_apply_of_ne_of_ne] }, { simp [sum.map, swap_apply_def], split_ifs; refl}, end end perm /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap end equiv lemma plift.eq_up_iff_down_eq {x : plift α} {y : α} : x = plift.up y ↔ x.down = y := equiv.plift.eq_symm_apply lemma function.injective.map_swap {α β : Type*} [decidable_eq α] [decidable_eq β] {f : α → β} (hf : function.injective f) (x y z : α) : f (equiv.swap x y z) = equiv.swap (f x) (f y) (f z) := begin conv_rhs { rw equiv.swap_apply_def }, split_ifs with h₁ h₂, { rw [hf h₁, equiv.swap_apply_left] }, { rw [hf h₂, equiv.swap_apply_right] }, { rw [equiv.swap_apply_of_ne_of_ne (mt (congr_arg f) h₁) (mt (congr_arg f) h₂)] } end namespace equiv protected lemma exists_unique_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∃! x, p x) ↔ ∃! y, q y := begin split, { rintro ⟨a, ha₁, ha₂⟩, exact ⟨f a, h.1 ha₁, λ b hb, f.symm_apply_eq.1 (ha₂ (f.symm b) (h.2 (by simpa using hb)))⟩ }, { rintro ⟨b, hb₁, hb₂⟩, exact ⟨f.symm b, h.2 (by simpa using hb₁), λ y hy, (eq_symm_apply f).2 (hb₂ _ (h.1 hy))⟩ } end protected lemma exists_unique_congr_left' {p : α → Prop} (f : α ≃ β) : (∃! x, p x) ↔ (∃! y, p (f.symm y)) := equiv.exists_unique_congr f (λx, by simp) protected lemma exists_unique_congr_left {p : β → Prop} (f : α ≃ β) : (∃! x, p (f x)) ↔ (∃! y, p y) := (equiv.exists_unique_congr_left' f.symm).symm protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) := begin split; intros h₂ x, { rw [←f.right_inv x], apply h.mp, apply h₂ }, apply h.mpr, apply h₂ end protected lemma forall_congr' {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p (f.symm x) ↔ q x) : (∀x, p x) ↔ (∀y, q y) := (equiv.forall_congr f.symm (λ x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {α₂ : Sort ua2} {β₁ : Sort ub1} {β₂ : Sort ub2} {γ₁ : Sort ug1} {γ₂ : Sort ug2} protected lemma forall₂_congr {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p x y ↔ q (eα x) (eβ y)) : (∀x y, p x y) ↔ (∀x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₂_congr' {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p (eα.symm x) (eβ.symm y) ↔ q x y) : (∀x y, p x y) ↔ (∀x y, q x y) := (equiv.forall₂_congr eα.symm eβ.symm (λ x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p x y z ↔ q (eα x) (eβ y) (eγ z)) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := begin apply equiv.forall₂_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p (eα.symm x) (eβ.symm y) (eγ.symm z) ↔ q x y z) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := (equiv.forall₃_congr eα.symm eβ.symm eγ.symm (λ x y z, h.symm)).symm protected lemma forall_congr_left' {p : α → Prop} (f : α ≃ β) : (∀x, p x) ↔ (∀y, p (f.symm y)) := equiv.forall_congr f (λx, by simp) protected lemma forall_congr_left {p : β → Prop} (f : α ≃ β) : (∀x, p (f x)) ↔ (∀y, p y) := (equiv.forall_congr_left' f.symm).symm protected lemma exists_congr_left {α β} (f : α ≃ β) {p : α → Prop} : (∃ a, p a) ↔ (∃ b, p (f.symm b)) := ⟨λ ⟨a, h⟩, ⟨f a, by simpa using h⟩, λ ⟨b, h⟩, ⟨_, h⟩⟩ protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} : (∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) := by simpa [equiv.image_eq_preimage] using (equiv.set.congr e).forall_congr_left' protected lemma preimage_sUnion {α β} (f : α ≃ β) {s : set (set β)} : f ⁻¹' (⋃₀ s) = ⋃₀ (_root_.set.image f ⁻¹' s) := by { ext x, simp [(equiv.set.congr f).symm.exists_congr_left] } section variables (P : α → Sort w) (e : α ≃ β) /-- Transport dependent functions through an equivalence of the base space. -/ @[simps] def Pi_congr_left' : (Π a, P a) ≃ (Π b, P (e.symm b)) := { to_fun := λ f x, f (e.symm x), inv_fun := λ f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } end section variables (P : β → Sort w) (e : α ≃ β) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Π a, P (e a)) ≃ (Π b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π a : α, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Π a, W a) ≃ (Π b, Z b) := (equiv.Pi_congr_right h₂).trans (equiv.Pi_congr_left _ h₁) end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π b : β, (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Π a, W a) ≃ (Π b, Z b) := (Pi_congr h₁.symm (λ b, (h₂ b).symm)).symm end end equiv lemma function.injective.swap_apply [decidable_eq α] [decidable_eq β] {f : α → β} (hf : function.injective f) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := begin by_cases hx : z = x, by simp [hx], by_cases hy : z = y, by simp [hy], rw [equiv.swap_apply_of_ne_of_ne hx hy, equiv.swap_apply_of_ne_of_ne (hf.ne hx) (hf.ne hy)] end lemma function.injective.swap_comp [decidable_eq α] [decidable_eq β] {f : α → β} (hf : function.injective f) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := funext $ λ z, hf.swap_apply _ _ _ /-- If both `α` and `β` are singletons, then `α ≃ β`. -/ def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β := { to_fun := λ _, default β, inv_fun := λ _, default α, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique α] : α ≃ punit.{v} := equiv_of_unique_of_unique /-- If `α` is a subsingleton, then it is equivalent to `α × α`. -/ def subsingleton_prod_self_equiv {α : Type*} [subsingleton α] : α × α ≃ α := { to_fun := λ p, p.1, inv_fun := λ a, (a, a), left_inv := λ p, subsingleton.elim _ _, right_inv := λ p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton α] [subsingleton β] (f : α → β) (g : β → α) : α ≃ β := { to_fun := f, inv_fun := g, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- A nonempty subsingleton type is (noncomputably) equivalent to `punit`. -/ noncomputable def equiv.punit_of_nonempty_of_subsingleton {α : Sort*} [h : nonempty α] [subsingleton α] : α ≃ punit.{v} := equiv_of_subsingleton_of_subsingleton (λ _, punit.star) (λ _, h.some) /-- `unique (unique α)` is equivalent to `unique α`. -/ def unique_unique_equiv : unique (unique α) ≃ unique α := equiv_of_subsingleton_of_subsingleton (λ h, h.default) (λ h, { default := h, uniq := λ _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1), inv_fun := quot.map e.symm (assume b₁ b₂ h, (eq (e.symm b₁) (e.symm b₂)).2 ((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } @[simp] lemma congr_mk {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀ (a₁ a₂ : α), ra a₁ a₂ ↔ rb (e a₁) (e a₂)) (a : α) : quot.congr e eq (quot.mk ra a) = quot.mk rb (e a) := rfl /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) : quot r ≃ quot r' := quot.congr (equiv.refl α) eq /-- An equivalence `e : α ≃ β` generates an equivalence between the quotient space of `α` by a relation `ra` and the quotient space of `β` by the image of this relation under `e`. -/ protected def congr_left {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := @quot.congr α β r (λ b b', r (e.symm b) (e.symm b')) e (λ a₁ a₂, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) : quotient ra ≃ quotient rb := quot.congr e eq @[simp] lemma congr_mk {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀ (a₁ a₂ : α), setoid.r a₁ a₂ ↔ setoid.r (e a₁) (e a₂)) (a : α): quotient.congr e eq (quotient.mk a) = quotient.mk (e a) := rfl /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid α} (eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the the types `↥s` and ``↥t`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β) (h : set.bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective namespace function lemma update_comp_equiv {α β α' : Sort*} [decidable_eq α'] [decidable_eq α] (f : α → β) (g : α' ≃ α) (a : α) (v : β) : update f a v ∘ g = update (f ∘ g) (g.symm a) v := by rw [← update_comp_eq_of_injective _ g.injective, g.apply_symm_apply] lemma update_apply_equiv_apply {α β α' : Sort*} [decidable_eq α'] [decidable_eq α] (f : α → β) (g : α' ≃ α) (a : α) (v : β) (a' : α') : update f a v (g a') = update (f ∘ g) (g.symm a) v a' := congr_fun (update_comp_equiv f g a v) a' end function /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe, congr, }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
9742a7830362b6a538deb1815da1e52080668d42
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/decomposition/signed_hahn.lean
43422efb0b07fe4769c10c5b88a4b14d49ea1950
[ "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
21,761
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.measure.vector_measure import order.symm_diff /-! # Hahn decomposition This file prove the Hahn decomposition theorem (signed version). The Hahn decomposition theorem states that, given a signed measure `s`, there exist complement, measurable sets `i` and `j`, such that `i` is positive and `j` is negative with repsect to `s`; that is, `s` restricted on `i` is non-negative and `s` restricted on `j` is non-positive. The Hahn decomposition theorem leads to many other results in measure theory, most notably, the Jordan decomposition theorem, the Lebesgue decomposition theorem and the Radon-Nikodym theorem. ## Main results * `measure_theory.signed_measure.exists_is_compl_positive_negative` : the Hahn decomposition theorem. * `measure_theory.signed_measure.exists_subset_restrict_nonpos` : A measurable set of negative measure contains a negative subset. ## Notation We use the notations `0 ≤[i] s` and `s ≤[i] 0` to denote the usual definitions of a set `i` being positive/negative with respect to the signed measure `s`. ## Tags Hahn decomposition theorem -/ noncomputable theory open_locale classical big_operators nnreal ennreal measure_theory variables {α β : Type*} [measurable_space α] variables {M : Type*} [add_comm_monoid M] [topological_space M] [ordered_add_comm_monoid M] namespace measure_theory namespace signed_measure open filter vector_measure variables {s : signed_measure α} {i j : set α} section exists_subset_restrict_nonpos /-! ### exists_subset_restrict_nonpos In this section we will prove that a set `i` whose measure is negative contains a negative subset `j` with respect to the signed measure `s` (i.e. `s ≤[j] 0`), whose measure is negative. This lemma is used to prove the Hahn decomposition theorem. To prove this lemma, we will construct a sequence of measurable sets $(A_n)_{n \in \mathbb{N}}$, such that, for all $n$, $s(A_{n + 1})$ is close to maximal among subsets of $i \setminus \bigcup_{k \le n} A_k$. This sequence of sets does not necessarily exist. However, if this sequence terminates; that is, there does not exists any sets satisfying the property, the last $A_n$ will be a negative subset of negative measure, hence proving our claim. In the case that the sequence does not terminate, it is easy to see that $i \setminus \bigcup_{k = 0}^\infty A_k$ is the required negative set. To implement this in Lean, we define several auxilary definitions. - given the sets `i` and the natural number `n`, `exists_one_div_lt s i n` is the property that there exists a measurable set `k ⊆ i` such that `1 / (n + 1) < s k`. - given the sets `i` and that `i` is not negative, `find_exists_one_div_lt s i` is the least natural number `n` such that `exists_one_div_lt s i n`. - given the sets `i` and that `i` is not negative, `some_exists_one_div_lt` chooses the set `k` from `exists_one_div_lt s i (find_exists_one_div_lt s i)`. - lastly, given the set `i`, `restrict_nonpos_seq s i` is the sequence of sets defined inductively where `restrict_nonpos_seq s i 0 = some_exists_one_div_lt s (i \ ∅)` and `restrict_nonpos_seq s i (n + 1) = some_exists_one_div_lt s (i \ ⋃ k ≤ n, restrict_nonpos_seq k)`. This definition represents the sequence $(A_n)$ in the proof as described above. With these definitions, we are able consider the case where the sequence terminates separately, allowing us to prove `exists_subset_restrict_nonpos`. -/ /-- Given the set `i` and the natural number `n`, `exists_one_div_lt s i j` is the property that there exists a measurable set `k ⊆ i` such that `1 / (n + 1) < s k`. -/ private def exists_one_div_lt (s : signed_measure α) (i : set α) (n : ℕ) : Prop := ∃ k : set α, k ⊆ i ∧ measurable_set k ∧ (1 / (n + 1) : ℝ) < s k private lemma exists_nat_one_div_lt_measure_of_not_negative (hi : ¬ s ≤[i] 0) : ∃ (n : ℕ), exists_one_div_lt s i n := let ⟨k, hj₁, hj₂, hj⟩ := exists_pos_measure_of_not_restrict_le_zero s hi in let ⟨n, hn⟩ := exists_nat_one_div_lt hj in ⟨n, k, hj₂, hj₁, hn⟩ /-- Given the set `i`, if `i` is not negative, `find_exists_one_div_lt s i` is the least natural number `n` such that `exists_one_div_lt s i n`, otherwise, it returns 0. -/ private def find_exists_one_div_lt (s : signed_measure α) (i : set α) : ℕ := if hi : ¬ s ≤[i] 0 then nat.find (exists_nat_one_div_lt_measure_of_not_negative hi) else 0 private lemma find_exists_one_div_lt_spec (hi : ¬ s ≤[i] 0) : exists_one_div_lt s i (find_exists_one_div_lt s i) := begin rw [find_exists_one_div_lt, dif_pos hi], convert nat.find_spec _, end private lemma find_exists_one_div_lt_min (hi : ¬ s ≤[i] 0) {m : ℕ} (hm : m < find_exists_one_div_lt s i) : ¬ exists_one_div_lt s i m := begin rw [find_exists_one_div_lt, dif_pos hi] at hm, exact nat.find_min _ hm end /-- Given the set `i`, if `i` is not negative, `some_exists_one_div_lt` chooses the set `k` from `exists_one_div_lt s i (find_exists_one_div_lt s i)`, otherwise, it returns the empty set. -/ private def some_exists_one_div_lt (s : signed_measure α) (i : set α) : set α := if hi : ¬ s ≤[i] 0 then classical.some (find_exists_one_div_lt_spec hi) else ∅ private lemma some_exists_one_div_lt_spec (hi : ¬ s ≤[i] 0) : (some_exists_one_div_lt s i) ⊆ i ∧ measurable_set (some_exists_one_div_lt s i) ∧ (1 / (find_exists_one_div_lt s i + 1) : ℝ) < s (some_exists_one_div_lt s i) := begin rw [some_exists_one_div_lt, dif_pos hi], exact classical.some_spec (find_exists_one_div_lt_spec hi), end private lemma some_exists_one_div_lt_subset : some_exists_one_div_lt s i ⊆ i := begin by_cases hi : ¬ s ≤[i] 0, { exact let ⟨h, _⟩ := some_exists_one_div_lt_spec hi in h }, { rw [some_exists_one_div_lt, dif_neg hi], exact set.empty_subset _ }, end private lemma some_exists_one_div_lt_subset' : some_exists_one_div_lt s (i \ j) ⊆ i := set.subset.trans some_exists_one_div_lt_subset (set.diff_subset _ _) private lemma some_exists_one_div_lt_measurable_set : measurable_set (some_exists_one_div_lt s i) := begin by_cases hi : ¬ s ≤[i] 0, { exact let ⟨_, h, _⟩ := some_exists_one_div_lt_spec hi in h }, { rw [some_exists_one_div_lt, dif_neg hi], exact measurable_set.empty } end private lemma some_exists_one_div_lt_lt (hi : ¬ s ≤[i] 0) : (1 / (find_exists_one_div_lt s i + 1) : ℝ) < s (some_exists_one_div_lt s i) := let ⟨_, _, h⟩ := some_exists_one_div_lt_spec hi in h /-- Given the set `i`, `restrict_nonpos_seq s i` is the sequence of sets defined inductively where `restrict_nonpos_seq s i 0 = some_exists_one_div_lt s (i \ ∅)` and `restrict_nonpos_seq s i (n + 1) = some_exists_one_div_lt s (i \ ⋃ k ≤ n, restrict_nonpos_seq k)`. For each `n : ℕ`,`s (restrict_nonpos_seq s i n)` is close to maximal among all subsets of `i \ ⋃ k ≤ n, restrict_nonpos_seq s i k`. -/ private def restrict_nonpos_seq (s : signed_measure α) (i : set α) : ℕ → set α | 0 := some_exists_one_div_lt s (i \ ∅) -- I used `i \ ∅` instead of `i` to simplify some proofs | (n + 1) := some_exists_one_div_lt s (i \ ⋃ k ≤ n, have k < n + 1 := nat.lt_succ_iff.mpr H, restrict_nonpos_seq k) private lemma restrict_nonpos_seq_succ (n : ℕ) : restrict_nonpos_seq s i n.succ = some_exists_one_div_lt s (i \ ⋃ k ≤ n, restrict_nonpos_seq s i k) := by rw restrict_nonpos_seq private lemma restrict_nonpos_seq_subset (n : ℕ) : restrict_nonpos_seq s i n ⊆ i := begin cases n; { rw restrict_nonpos_seq, exact some_exists_one_div_lt_subset' } end private lemma restrict_nonpos_seq_lt (n : ℕ) (hn : ¬ s ≤[i \ ⋃ k ≤ n, restrict_nonpos_seq s i k] 0) : (1 / (find_exists_one_div_lt s (i \ ⋃ k ≤ n, restrict_nonpos_seq s i k) + 1) : ℝ) < s (restrict_nonpos_seq s i n.succ) := begin rw restrict_nonpos_seq_succ, apply some_exists_one_div_lt_lt hn, end private lemma measure_of_restrict_nonpos_seq (hi₂ : ¬ s ≤[i] 0) (n : ℕ) (hn : ¬ s ≤[i \ ⋃ k < n, restrict_nonpos_seq s i k] 0) : 0 < s (restrict_nonpos_seq s i n) := begin cases n, { rw restrict_nonpos_seq, rw ← @set.diff_empty _ i at hi₂, rcases some_exists_one_div_lt_spec hi₂ with ⟨_, _, h⟩, exact (lt_trans nat.one_div_pos_of_nat h) }, { rw restrict_nonpos_seq_succ, have h₁ : ¬ s ≤[i \ ⋃ (k : ℕ) (H : k ≤ n), restrict_nonpos_seq s i k] 0, { refine mt (restrict_le_zero_subset _ _ (by simp [nat.lt_succ_iff])) hn, convert measurable_of_not_restrict_le_zero _ hn, exact funext (λ x, by rw nat.lt_succ_iff) }, rcases some_exists_one_div_lt_spec h₁ with ⟨_, _, h⟩, exact (lt_trans nat.one_div_pos_of_nat h) } end private lemma restrict_nonpos_seq_measurable_set (n : ℕ) : measurable_set (restrict_nonpos_seq s i n) := begin cases n; { rw restrict_nonpos_seq, exact some_exists_one_div_lt_measurable_set }, end private lemma restrict_nonpos_seq_disjoint' {n m : ℕ} (h : n < m) : restrict_nonpos_seq s i n ∩ restrict_nonpos_seq s i m = ∅ := begin rw set.eq_empty_iff_forall_not_mem, rintro x ⟨hx₁, hx₂⟩, cases m, { linarith }, { rw restrict_nonpos_seq at hx₂, exact (some_exists_one_div_lt_subset hx₂).2 (set.mem_Union.2 ⟨n, set.mem_Union.2 ⟨nat.lt_succ_iff.mp h, hx₁⟩⟩) } end private lemma restrict_nonpos_seq_disjoint : pairwise (disjoint on (restrict_nonpos_seq s i)) := begin intros n m h, rcases lt_or_gt_of_ne h with (h | h), { intro x, rw [set.inf_eq_inter, restrict_nonpos_seq_disjoint' h], exact id }, { intro x, rw [set.inf_eq_inter, set.inter_comm, restrict_nonpos_seq_disjoint' h], exact id } end private lemma exists_subset_restrict_nonpos' (hi₁ : measurable_set i) (hi₂ : s i < 0) (hn : ¬ ∀ n : ℕ, ¬ s ≤[i \ ⋃ l < n, restrict_nonpos_seq s i l] 0) : ∃ j : set α, measurable_set j ∧ j ⊆ i ∧ s ≤[j] 0 ∧ s j < 0 := begin by_cases s ≤[i] 0, { exact ⟨i, hi₁, set.subset.refl _, h, hi₂⟩ }, push_neg at hn, set k := nat.find hn with hk₁, have hk₂ : s ≤[i \ ⋃ l < k, restrict_nonpos_seq s i l] 0 := nat.find_spec hn, have hmeas : measurable_set (⋃ (l : ℕ) (H : l < k), restrict_nonpos_seq s i l) := (measurable_set.Union $ λ _, measurable_set.Union_Prop (λ _, restrict_nonpos_seq_measurable_set _)), refine ⟨i \ ⋃ l < k, restrict_nonpos_seq s i l, hi₁.diff hmeas, set.diff_subset _ _, hk₂, _⟩, rw [of_diff hmeas hi₁, s.of_disjoint_Union_nat], { have h₁ : ∀ l < k, 0 ≤ s (restrict_nonpos_seq s i l), { intros l hl, refine le_of_lt (measure_of_restrict_nonpos_seq h _ _), refine mt (restrict_le_zero_subset _ (hi₁.diff _) (set.subset.refl _)) (nat.find_min hn hl), exact (measurable_set.Union $ λ _, measurable_set.Union_Prop (λ _, restrict_nonpos_seq_measurable_set _)) }, suffices : 0 ≤ ∑' (l : ℕ), s (⋃ (H : l < k), restrict_nonpos_seq s i l), { rw sub_neg, exact lt_of_lt_of_le hi₂ this }, refine tsum_nonneg _, intro l, by_cases l < k, { convert h₁ _ h, ext x, rw [set.mem_Union, exists_prop, and_iff_right_iff_imp], exact λ _, h }, { convert le_of_eq s.empty.symm, ext, simp only [exists_prop, set.mem_empty_eq, set.mem_Union, not_and, iff_false], exact λ h', false.elim (h h') } }, { intro, exact measurable_set.Union_Prop (λ _, restrict_nonpos_seq_measurable_set _) }, { intros a b hab x hx, simp only [exists_prop, set.mem_Union, set.mem_inter_eq, set.inf_eq_inter] at hx, exact let ⟨⟨_, hx₁⟩, _, hx₂⟩ := hx in restrict_nonpos_seq_disjoint a b hab ⟨hx₁, hx₂⟩ }, { apply set.Union_subset, intros a x, simp only [and_imp, exists_prop, set.mem_Union], intros _ hx, exact restrict_nonpos_seq_subset _ hx }, { apply_instance } end /-- A measurable set of negative measure has a negative subset of negative measure. -/ theorem exists_subset_restrict_nonpos (hi : s i < 0) : ∃ j : set α, measurable_set j ∧ j ⊆ i ∧ s ≤[j] 0 ∧ s j < 0 := begin have hi₁ : measurable_set i := classical.by_contradiction (λ h, ne_of_lt hi $ s.not_measurable h), by_cases s ≤[i] 0, { exact ⟨i, hi₁, set.subset.refl _, h, hi⟩ }, by_cases hn : ∀ n : ℕ, ¬ s ≤[i \ ⋃ l < n, restrict_nonpos_seq s i l] 0, swap, { exact exists_subset_restrict_nonpos' hi₁ hi hn }, set A := i \ ⋃ l, restrict_nonpos_seq s i l with hA, set bdd : ℕ → ℕ := λ n, find_exists_one_div_lt s (i \ ⋃ k ≤ n, restrict_nonpos_seq s i k) with hbdd, have hn' : ∀ n : ℕ, ¬ s ≤[i \ ⋃ l ≤ n, restrict_nonpos_seq s i l] 0, { intro n, convert hn (n + 1); { ext l, simp only [exists_prop, set.mem_Union, and.congr_left_iff], exact λ _, nat.lt_succ_iff.symm } }, have h₁ : s i = s A + ∑' l, s (restrict_nonpos_seq s i l), { rw [hA, ← s.of_disjoint_Union_nat, add_comm, of_add_of_diff], exact measurable_set.Union (λ _, restrict_nonpos_seq_measurable_set _), exacts [hi₁, set.Union_subset (λ _, restrict_nonpos_seq_subset _), λ _, restrict_nonpos_seq_measurable_set _, restrict_nonpos_seq_disjoint] }, have h₂ : s A ≤ s i, { rw h₁, apply le_add_of_nonneg_right, exact tsum_nonneg (λ n, le_of_lt (measure_of_restrict_nonpos_seq h _ (hn n))) }, have h₃' : summable (λ n, (1 / (bdd n + 1) : ℝ)), { have : summable (λ l, s (restrict_nonpos_seq s i l)) := has_sum.summable (s.m_Union (λ _, restrict_nonpos_seq_measurable_set _) restrict_nonpos_seq_disjoint), refine summable_of_nonneg_of_le (λ n, _) (λ n, _) (summable.comp_injective this nat.succ_injective), { exact le_of_lt nat.one_div_pos_of_nat }, { exact le_of_lt (restrict_nonpos_seq_lt n (hn' n)) } }, have h₃ : tendsto (λ n, (bdd n : ℝ) + 1) at_top at_top, { simp only [one_div] at h₃', exact summable.tendsto_top_of_pos h₃' (λ n, nat.cast_add_one_pos (bdd n)) }, have h₄ : tendsto (λ n, (bdd n : ℝ)) at_top at_top, { convert at_top.tendsto_at_top_add_const_right (-1) h₃, simp }, have A_meas : measurable_set A := hi₁.diff (measurable_set.Union (λ _, restrict_nonpos_seq_measurable_set _)), refine ⟨A, A_meas, set.diff_subset _ _, _, h₂.trans_lt hi⟩, by_contra hnn, rw restrict_le_restrict_iff _ _ A_meas at hnn, push_neg at hnn, obtain ⟨E, hE₁, hE₂, hE₃⟩ := hnn, have : ∃ k, 1 ≤ bdd k ∧ 1 / (bdd k : ℝ) < s E, { rw tendsto_at_top_at_top at h₄, obtain ⟨k, hk⟩ := h₄ (max (1 / s E + 1) 1), refine ⟨k, _, _⟩, { have hle := le_of_max_le_right (hk k le_rfl), norm_cast at hle, exact hle }, { have : 1 / s E < bdd k, { linarith [le_of_max_le_left (hk k le_rfl)] {restrict_type := ℝ} }, rw one_div at this ⊢, rwa inv_lt (lt_trans (inv_pos.2 hE₃) this) hE₃ } }, obtain ⟨k, hk₁, hk₂⟩ := this, have hA' : A ⊆ i \ ⋃ l ≤ k, restrict_nonpos_seq s i l, { apply set.diff_subset_diff_right, intro x, simp only [set.mem_Union], rintro ⟨n, _, hn₂⟩, exact ⟨n, hn₂⟩ }, refine find_exists_one_div_lt_min (hn' k) (buffer.lt_aux_2 hk₁) ⟨E, set.subset.trans hE₂ hA', hE₁, _⟩, convert hk₂, norm_cast, exact tsub_add_cancel_of_le hk₁ end end exists_subset_restrict_nonpos /-- The set of measures of the set of measurable negative sets. -/ def measure_of_negatives (s : signed_measure α) : set ℝ := s '' { B | measurable_set B ∧ s ≤[B] 0 } lemma zero_mem_measure_of_negatives : (0 : ℝ) ∈ s.measure_of_negatives := ⟨∅, ⟨measurable_set.empty, le_restrict_empty _ _⟩, s.empty⟩ lemma bdd_below_measure_of_negatives : bdd_below s.measure_of_negatives := begin simp_rw [bdd_below, set.nonempty, mem_lower_bounds], by_contra, push_neg at h, have h' : ∀ n : ℕ, ∃ y : ℝ, y ∈ s.measure_of_negatives ∧ y < -n := λ n, h (-n), choose f hf using h', have hf' : ∀ n : ℕ, ∃ B, measurable_set B ∧ s ≤[B] 0 ∧ s B < -n, { intro n, rcases hf n with ⟨⟨B, ⟨hB₁, hBr⟩, hB₂⟩, hlt⟩, exact ⟨B, hB₁, hBr, hB₂.symm ▸ hlt⟩ }, choose B hmeas hr h_lt using hf', set A := ⋃ n, B n with hA, have hfalse : ∀ n : ℕ, s A ≤ -n, { intro n, refine le_trans _ (le_of_lt (h_lt _)), rw [hA, ← set.diff_union_of_subset (set.subset_Union _ n), of_union (disjoint.comm.1 set.disjoint_diff) _ (hmeas n)], { refine add_le_of_nonpos_left _, have : s ≤[A] 0 := restrict_le_restrict_Union _ _ hmeas hr, refine nonpos_of_restrict_le_zero _ (restrict_le_zero_subset _ _ (set.diff_subset _ _) this), exact measurable_set.Union hmeas }, { apply_instance }, { exact (measurable_set.Union hmeas).diff (hmeas n) } }, rcases exists_nat_gt (-(s A)) with ⟨n, hn⟩, exact lt_irrefl _ ((neg_lt.1 hn).trans_le (hfalse n)), end /-- Alternative formulation of `measure_theory.signed_measure.exists_is_compl_positive_negative` (the Hahn decomposition theorem) using set complements. -/ lemma exists_compl_positive_negative (s : signed_measure α) : ∃ i : set α, measurable_set i ∧ 0 ≤[i] s ∧ s ≤[iᶜ] 0 := begin obtain ⟨f, _, hf₂, hf₁⟩ := exists_seq_tendsto_Inf ⟨0, @zero_mem_measure_of_negatives _ _ s⟩ bdd_below_measure_of_negatives, choose B hB using hf₁, have hB₁ : ∀ n, measurable_set (B n) := λ n, (hB n).1.1, have hB₂ : ∀ n, s ≤[B n] 0 := λ n, (hB n).1.2, set A := ⋃ n, B n with hA, have hA₁ : measurable_set A := measurable_set.Union hB₁, have hA₂ : s ≤[A] 0 := restrict_le_restrict_Union _ _ hB₁ hB₂, have hA₃ : s A = Inf s.measure_of_negatives, { apply le_antisymm, { refine le_of_tendsto_of_tendsto tendsto_const_nhds hf₂ (eventually_of_forall (λ n, _)), rw [← (hB n).2, hA, ← set.diff_union_of_subset (set.subset_Union _ n), of_union (disjoint.comm.1 set.disjoint_diff) _ (hB₁ n)], { refine add_le_of_nonpos_left _, have : s ≤[A] 0 := restrict_le_restrict_Union _ _ hB₁ (λ m, let ⟨_, h⟩ := (hB m).1 in h), refine nonpos_of_restrict_le_zero _ (restrict_le_zero_subset _ _ (set.diff_subset _ _) this), exact measurable_set.Union hB₁ }, { apply_instance }, { exact (measurable_set.Union hB₁).diff (hB₁ n) } }, { exact cInf_le bdd_below_measure_of_negatives ⟨A, ⟨hA₁, hA₂⟩, rfl⟩ } }, refine ⟨Aᶜ, hA₁.compl, _, (compl_compl A).symm ▸ hA₂⟩, rw restrict_le_restrict_iff _ _ hA₁.compl, intros C hC hC₁, by_contra hC₂, push_neg at hC₂, rcases exists_subset_restrict_nonpos hC₂ with ⟨D, hD₁, hD, hD₂, hD₃⟩, have : s (A ∪ D) < Inf s.measure_of_negatives, { rw [← hA₃, of_union (set.disjoint_of_subset_right (set.subset.trans hD hC₁) disjoint_compl_right) hA₁ hD₁], linarith, apply_instance }, refine not_le.2 this _, refine cInf_le bdd_below_measure_of_negatives ⟨A ∪ D, ⟨_, _⟩, rfl⟩, { exact hA₁.union hD₁ }, { exact restrict_le_restrict_union _ _ hA₁ hA₂ hD₁ hD₂ }, end /-- **The Hahn decomposition thoerem**: Given a signed measure `s`, there exist complement measurable sets `i` and `j` such that `i` is positive, `j` is negative. -/ theorem exists_is_compl_positive_negative (s : signed_measure α) : ∃ i j : set α, measurable_set i ∧ 0 ≤[i] s ∧ measurable_set j ∧ s ≤[j] 0 ∧ is_compl i j := let ⟨i, hi₁, hi₂, hi₃⟩ := exists_compl_positive_negative s in ⟨i, iᶜ, hi₁, hi₂, hi₁.compl, hi₃, is_compl_compl⟩ /-- The symmetric difference of two Hahn decompositions have measure zero. -/ lemma of_symm_diff_compl_positive_negative {s : signed_measure α} {i j : set α} (hi : measurable_set i) (hj : measurable_set j) (hi' : 0 ≤[i] s ∧ s ≤[iᶜ] 0) (hj' : 0 ≤[j] s ∧ s ≤[jᶜ] 0) : s (i Δ j) = 0 ∧ s (iᶜ Δ jᶜ) = 0 := begin rw [restrict_le_restrict_iff s 0, restrict_le_restrict_iff 0 s] at hi' hj', split, { rw [symm_diff_def, set.diff_eq_compl_inter, set.diff_eq_compl_inter, set.sup_eq_union, of_union, le_antisymm (hi'.2 (hi.compl.inter hj) (set.inter_subset_left _ _)) (hj'.1 (hi.compl.inter hj) (set.inter_subset_right _ _)), le_antisymm (hj'.2 (hj.compl.inter hi) (set.inter_subset_left _ _)) (hi'.1 (hj.compl.inter hi) (set.inter_subset_right _ _)), zero_apply, zero_apply, zero_add], { exact set.disjoint_of_subset_left (set.inter_subset_left _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) (disjoint.comm.1 (is_compl.disjoint is_compl_compl))) }, { exact hj.compl.inter hi }, { exact hi.compl.inter hj } }, { rw [symm_diff_def, set.diff_eq_compl_inter, set.diff_eq_compl_inter, compl_compl, compl_compl, set.sup_eq_union, of_union, le_antisymm (hi'.2 (hj.inter hi.compl) (set.inter_subset_right _ _)) (hj'.1 (hj.inter hi.compl) (set.inter_subset_left _ _)), le_antisymm (hj'.2 (hi.inter hj.compl) (set.inter_subset_right _ _)) (hi'.1 (hi.inter hj.compl) (set.inter_subset_left _ _)), zero_apply, zero_apply, zero_add], { exact set.disjoint_of_subset_left (set.inter_subset_left _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) (is_compl.disjoint is_compl_compl)) }, { exact hj.inter hi.compl }, { exact hi.inter hj.compl } }, all_goals { measurability }, end end signed_measure end measure_theory
5a192d145905a51a858e1425e5c19738e1f518b8
c777c32c8e484e195053731103c5e52af26a25d1
/src/probability/independence/zero_one.lean
a806d92324928231ba5ce6cc19992dc5a885abed
[ "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,713
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 probability.independence.basic /-! # Kolmogorov's 0-1 law Let `s : ι → measurable_space Ω` be an independent sequence of sub-σ-algebras. Then any set which is measurable with respect to the tail σ-algebra `limsup s at_top` has probability 0 or 1. ## Main statements * `measure_zero_or_one_of_measurable_set_limsup_at_top`: Kolmogorov's 0-1 law. Any set which is measurable with respect to the tail σ-algebra `limsup s at_top` of an independent sequence of σ-algebras `s` has probability 0 or 1. -/ open measure_theory measurable_space open_locale measure_theory ennreal namespace probability_theory variables {Ω ι : Type*} {m m0 : measurable_space Ω} {μ : measure Ω} lemma measure_eq_zero_or_one_or_top_of_indep_set_self {t : set Ω} (h_indep : indep_set t t μ) : μ t = 0 ∨ μ t = 1 ∨ μ t = ∞ := begin specialize h_indep t t (measurable_set_generate_from (set.mem_singleton t)) (measurable_set_generate_from (set.mem_singleton t)), by_cases h0 : μ t = 0, { exact or.inl h0, }, by_cases h_top : μ t = ∞, { exact or.inr (or.inr h_top), }, rw [← one_mul (μ (t ∩ t)), set.inter_self, ennreal.mul_eq_mul_right h0 h_top] at h_indep, exact or.inr (or.inl h_indep.symm), end lemma measure_eq_zero_or_one_of_indep_set_self [is_finite_measure μ] {t : set Ω} (h_indep : indep_set t t μ) : μ t = 0 ∨ μ t = 1 := begin have h_0_1_top := measure_eq_zero_or_one_or_top_of_indep_set_self h_indep, simpa [measure_ne_top μ] using h_0_1_top, end variables [is_probability_measure μ] {s : ι → measurable_space Ω} open filter lemma indep_bsupr_compl (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (t : set ι) : indep (⨆ n ∈ t, s n) (⨆ n ∈ tᶜ, s n) μ := indep_supr_of_disjoint h_le h_indep disjoint_compl_right section abstract variables {α : Type*} {p : set ι → Prop} {f : filter ι} {ns : α → set ι} /-! We prove a version of Kolmogorov's 0-1 law for the σ-algebra `limsup s f` where `f` is a filter for which we can define the following two functions: * `p : set ι → Prop` such that for a set `t`, `p t → tᶜ ∈ f`, * `ns : α → set ι` a directed sequence of sets which all verify `p` and such that `⋃ a, ns a = set.univ`. For the example of `f = at_top`, we can take `p = bdd_above` and `ns : ι → set ι := λ i, set.Iic i`. -/ lemma indep_bsupr_limsup (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (hf : ∀ t, p t → tᶜ ∈ f) {t : set ι} (ht : p t) : indep (⨆ n ∈ t, s n) (limsup s f) μ := begin refine indep_of_indep_of_le_right (indep_bsupr_compl h_le h_indep t) _, refine Limsup_le_of_le (by is_bounded_default) _, simp only [set.mem_compl_iff, eventually_map], exact eventually_of_mem (hf t ht) le_supr₂, end lemma indep_supr_directed_limsup (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (hf : ∀ t, p t → tᶜ ∈ f) (hns : directed (≤) ns) (hnsp : ∀ a, p (ns a)) : indep (⨆ a, ⨆ n ∈ (ns a), s n) (limsup s f) μ := begin refine indep_supr_of_directed_le _ _ _ _, { exact λ a, indep_bsupr_limsup h_le h_indep hf (hnsp a), }, { exact λ a, supr₂_le (λ n hn, h_le n), }, { exact limsup_le_supr.trans (supr_le h_le), }, { intros a b, obtain ⟨c, hc⟩ := hns a b, refine ⟨c, _, _⟩; refine supr_mono (λ n, supr_mono' (λ hn, ⟨_, le_rfl⟩)), { exact hc.1 hn, }, { exact hc.2 hn, }, }, end lemma indep_supr_limsup (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (hf : ∀ t, p t → tᶜ ∈ f) (hns : directed (≤) ns) (hnsp : ∀ a, p (ns a)) (hns_univ : ∀ n, ∃ a, n ∈ ns a) : indep (⨆ n, s n) (limsup s f) μ := begin suffices : (⨆ a, ⨆ n ∈ (ns a), s n) = ⨆ n, s n, { rw ← this, exact indep_supr_directed_limsup h_le h_indep hf hns hnsp, }, rw supr_comm, refine supr_congr (λ n, _), have : (⨆ (i : α) (H : n ∈ ns i), s n) = (⨆ (h : ∃ i, n ∈ ns i), s n), by rw supr_exists, haveI : nonempty (∃ (i : α), n ∈ ns i) := ⟨hns_univ n⟩, rw [this, supr_const], end lemma indep_limsup_self (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (hf : ∀ t, p t → tᶜ ∈ f) (hns : directed (≤) ns) (hnsp : ∀ a, p (ns a)) (hns_univ : ∀ n, ∃ a, n ∈ ns a) : indep (limsup s f) (limsup s f) μ := indep_of_indep_of_le_left (indep_supr_limsup h_le h_indep hf hns hnsp hns_univ) limsup_le_supr theorem measure_zero_or_one_of_measurable_set_limsup (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) (hf : ∀ t, p t → tᶜ ∈ f) (hns : directed (≤) ns) (hnsp : ∀ a, p (ns a)) (hns_univ : ∀ n, ∃ a, n ∈ ns a) {t : set Ω} (ht_tail : measurable_set[limsup s f] t) : μ t = 0 ∨ μ t = 1 := measure_eq_zero_or_one_of_indep_set_self ((indep_limsup_self h_le h_indep hf hns hnsp hns_univ).indep_set_of_measurable_set ht_tail ht_tail) end abstract section at_top variables [semilattice_sup ι] [no_max_order ι] [nonempty ι] lemma indep_limsup_at_top_self (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) : indep (limsup s at_top) (limsup s at_top) μ := begin let ns : ι → set ι := set.Iic, have hnsp : ∀ i, bdd_above (ns i) := λ i, bdd_above_Iic, refine indep_limsup_self h_le h_indep _ _ hnsp _, { simp only [mem_at_top_sets, ge_iff_le, set.mem_compl_iff, bdd_above, upper_bounds, set.nonempty], rintros t ⟨a, ha⟩, obtain ⟨b, hb⟩ : ∃ b, a < b := exists_gt a, refine ⟨b, λ c hc hct, _⟩, suffices : ∀ i ∈ t, i < c, from lt_irrefl c (this c hct), exact λ i hi, (ha hi).trans_lt (hb.trans_le hc), }, { exact monotone.directed_le (λ i j hij k hki, le_trans hki hij), }, { exact λ n, ⟨n, le_rfl⟩, }, end /-- **Kolmogorov's 0-1 law** : any event in the tail σ-algebra of an independent sequence of sub-σ-algebras has probability 0 or 1. The tail σ-algebra `limsup s at_top` is the same as `⋂ n, ⋃ i ≥ n, s i`. -/ theorem measure_zero_or_one_of_measurable_set_limsup_at_top (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) {t : set Ω} (ht_tail : measurable_set[limsup s at_top] t) : μ t = 0 ∨ μ t = 1 := measure_eq_zero_or_one_of_indep_set_self ((indep_limsup_at_top_self h_le h_indep).indep_set_of_measurable_set ht_tail ht_tail) end at_top section at_bot variables [semilattice_inf ι] [no_min_order ι] [nonempty ι] lemma indep_limsup_at_bot_self (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) : indep (limsup s at_bot) (limsup s at_bot) μ := begin let ns : ι → set ι := set.Ici, have hnsp : ∀ i, bdd_below (ns i) := λ i, bdd_below_Ici, refine indep_limsup_self h_le h_indep _ _ hnsp _, { simp only [mem_at_bot_sets, ge_iff_le, set.mem_compl_iff, bdd_below, lower_bounds, set.nonempty], rintros t ⟨a, ha⟩, obtain ⟨b, hb⟩ : ∃ b, b < a := exists_lt a, refine ⟨b, λ c hc hct, _⟩, suffices : ∀ i ∈ t, c < i, from lt_irrefl c (this c hct), exact λ i hi, hc.trans_lt (hb.trans_le (ha hi)), }, { exact directed_of_inf (λ i j hij k hki, hij.trans hki), }, { exact λ n, ⟨n, le_rfl⟩, }, end /-- **Kolmogorov's 0-1 law** : any event in the tail σ-algebra of an independent sequence of sub-σ-algebras has probability 0 or 1. -/ theorem measure_zero_or_one_of_measurable_set_limsup_at_bot (h_le : ∀ n, s n ≤ m0) (h_indep : Indep s μ) {t : set Ω} (ht_tail : measurable_set[limsup s at_bot] t) : μ t = 0 ∨ μ t = 1 := measure_eq_zero_or_one_of_indep_set_self ((indep_limsup_at_bot_self h_le h_indep).indep_set_of_measurable_set ht_tail ht_tail) end at_bot end probability_theory
87a15896c3b40ef9b185a70b4d6829723fe2a397
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/shapes/equalizers.lean
b2d4fc9da76f24049777dea27cae10d2a4d0fe11
[ "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
45,420
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 -/ import category_theory.epi_mono import category_theory.limits.has_limits /-! # Equalizers and coequalizers This file defines (co)equalizers as special cases of (co)limits. An equalizer is the categorical generalization of the subobject {a ∈ A | f(a) = g(a)} known from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`. A coequalizer is the dual concept. ## Main definitions * `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams * `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`. * a `fork` is a cone over a parallel pair. * there is really only one interesting morphism in a fork: the arrow from the vertex of the fork to the domain of f and g. It is called `fork.ι`. * an `equalizer` is now just a `limit (parallel_pair f g)` Each of these has a dual. ## Main statements * `equalizer.ι_mono` states that every equalizer map is a monomorphism * `is_iso_limit_cone_parallel_pair_of_self` states that the identity on the domain of `f` is an equalizer of `f` and `f`. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1] -/ noncomputable theory open category_theory opposite namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v v₂ u u₂ /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq, derive inhabited] inductive walking_parallel_pair : Type | zero | one open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Π X : walking_parallel_pair, walking_parallel_pair_hom X X /-- Satisfying the inhabited linter -/ instance : inhabited (walking_parallel_pair_hom zero one) := { default := walking_parallel_pair_hom.left } open walking_parallel_pair_hom /-- Composition of morphisms in the indexing diagram for (co)equalizers. -/ def walking_parallel_pair_hom.comp : Π (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } @[simp] lemma walking_parallel_pair_hom_id (X : walking_parallel_pair) : walking_parallel_pair_hom.id X = 𝟙 X := rfl /-- The functor `walking_parallel_pair ⥤ walking_parallel_pairᵒᵖ` sending left to left and right to right. -/ def walking_parallel_pair_op : walking_parallel_pair ⥤ walking_parallel_pairᵒᵖ := { obj := (λ x, op $ by { cases x, exacts [one, zero] }), map := λ i j f, by { cases f; apply quiver.hom.op, exacts [left, right, walking_parallel_pair_hom.id _] }, map_comp' := by { rintros (_|_) (_|_) (_|_) (_|_|_) (_|_|_); refl } } @[simp] lemma walking_parallel_pair_op_zero : walking_parallel_pair_op.obj zero = op one := rfl @[simp] lemma walking_parallel_pair_op_one : walking_parallel_pair_op.obj one = op zero := rfl @[simp] lemma walking_parallel_pair_op_left : walking_parallel_pair_op.map left = @quiver.hom.op _ _ zero one left := rfl @[simp] lemma walking_parallel_pair_op_right : walking_parallel_pair_op.map right = @quiver.hom.op _ _ zero one right := rfl /-- The equivalence `walking_parallel_pair ⥤ walking_parallel_pairᵒᵖ` sending left to left and right to right. -/ @[simps functor inverse] def walking_parallel_pair_op_equiv : walking_parallel_pair ≌ walking_parallel_pairᵒᵖ := { functor := walking_parallel_pair_op, inverse := walking_parallel_pair_op.left_op, unit_iso := nat_iso.of_components (λ j, eq_to_iso (by { cases j; refl })) (by { rintros (_|_) (_|_) (_|_|_); refl }), counit_iso := nat_iso.of_components (λ j, eq_to_iso (by { induction j using opposite.rec, cases j; refl })) (λ i j f, by { induction i using opposite.rec, induction j using opposite.rec, let g := f.unop, have : f = g.op := rfl, clear_value g, subst this, rcases i with (_|_); rcases j with (_|_); rcases g with (_|_|_); refl }) } @[simp] lemma walking_parallel_pair_op_equiv_unit_iso_zero : walking_parallel_pair_op_equiv.unit_iso.app zero = iso.refl zero := rfl @[simp] lemma walking_parallel_pair_op_equiv_unit_iso_one : walking_parallel_pair_op_equiv.unit_iso.app one = iso.refl one := rfl @[simp] lemma walking_parallel_pair_op_equiv_counit_iso_zero : walking_parallel_pair_op_equiv.counit_iso.app (op zero) = iso.refl (op zero) := rfl @[simp] lemma walking_parallel_pair_op_equiv_counit_iso_one : walking_parallel_pair_op_equiv.counit_iso.app (op one) = iso.refl (op one) := rfl variables {C : Type u} [category.{v} C] variables {X Y : C} /-- `parallel_pair f g` is the diagram in `C` consisting of the two morphisms `f` and `g` with common domain and codomain. -/ def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair ⥤ C := { obj := λ x, match x with | zero := X | one := Y end, map := λ x y h, match x, y, h with | _, _, (id _) := 𝟙 _ | _, _, left := f | _, _, right := g end, -- `tidy` can cope with this, but it's too slow: map_comp' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩⟨⟩; { unfold_aux, simp; refl }, end, }. @[simp] lemma parallel_pair_obj_zero (f g : X ⟶ Y) : (parallel_pair f g).obj zero = X := rfl @[simp] lemma parallel_pair_obj_one (f g : X ⟶ Y) : (parallel_pair f g).obj one = Y := rfl @[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair ⥤ C} (j : walking_parallel_pair) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end /-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a `parallel_pair` -/ @[simps] def diagram_iso_parallel_pair (F : walking_parallel_pair ⥤ C) : F ≅ parallel_pair (F.map left) (F.map right) := nat_iso.of_components (λ j, eq_to_iso $ by cases j; tidy) $ by tidy /-- Construct a morphism between parallel pairs. -/ def parallel_pair_hom {X' Y' : C} (f g : X ⟶ Y) (f' g' : X' ⟶ Y') (p : X ⟶ X') (q : Y ⟶ Y') (wf : f ≫ q = p ≫ f') (wg : g ≫ q = p ≫ g') : parallel_pair f g ⟶ parallel_pair f' g' := { app := λ j, match j with | zero := p | one := q end, naturality' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩; { unfold_aux, simp [wf, wg], }, end } @[simp] lemma parallel_pair_hom_app_zero {X' Y' : C} (f g : X ⟶ Y) (f' g' : X' ⟶ Y') (p : X ⟶ X') (q : Y ⟶ Y') (wf : f ≫ q = p ≫ f') (wg : g ≫ q = p ≫ g') : (parallel_pair_hom f g f' g' p q wf wg).app zero = p := rfl @[simp] lemma parallel_pair_hom_app_one {X' Y' : C} (f g : X ⟶ Y) (f' g' : X' ⟶ Y') (p : X ⟶ X') (q : Y ⟶ Y') (wf : f ≫ q = p ≫ f') (wg : g ≫ q = p ≫ g') : (parallel_pair_hom f g f' g' p q wf wg).app one = q := rfl /-- Construct a natural isomorphism between functors out of the walking parallel pair from its components. -/ @[simps] def parallel_pair.ext {F G : walking_parallel_pair ⥤ C} (zero : F.obj zero ≅ G.obj zero) (one : F.obj one ≅ G.obj one) (left : F.map left ≫ one.hom = zero.hom ≫ G.map left) (right : F.map right ≫ one.hom = zero.hom ≫ G.map right) : F ≅ G := nat_iso.of_components (by { rintro ⟨j⟩, exacts [zero, one] }) (by { rintro ⟨j₁⟩ ⟨j₂⟩ ⟨f⟩; simp [left, right], }) /-- Construct a natural isomorphism between `parallel_pair f g` and `parallel_pair f' g'` given equalities `f = f'` and `g = g'`. -/ @[simps] def parallel_pair.eq_of_hom_eq {f g f' g' : X ⟶ Y} (hf : f = f') (hg : g = g') : parallel_pair f g ≅ parallel_pair f' g' := parallel_pair.ext (iso.refl _) (iso.refl _) (by simp [hf]) (by simp [hg]) /-- A fork on `f` and `g` is just a `cone (parallel_pair f g)`. -/ abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g) /-- A cofork on `f` and `g` is just a `cocone (parallel_pair f g)`. -/ abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g) variables {f g : X ⟶ Y} /-- A fork `t` on the parallel pair `f g : X ⟶ Y` consists of two morphisms `t.π.app zero : t.X ⟶ X` and `t.π.app one : t.X ⟶ Y`. Of these, only the first one is interesting, and we give it the shorter name `fork.ι t`. -/ def fork.ι (t : fork f g) := t.π.app zero @[simp] lemma fork.app_zero_eq_ι (t : fork f g) : t.π.app zero = t.ι := rfl /-- A cofork `t` on the parallel_pair `f g : X ⟶ Y` consists of two morphisms `t.ι.app zero : X ⟶ t.X` and `t.ι.app one : Y ⟶ t.X`. Of these, only the second one is interesting, and we give it the shorter name `cofork.π t`. -/ def cofork.π (t : cofork f g) := t.ι.app one @[simp] lemma cofork.app_one_eq_π (t : cofork f g) : t.ι.app one = t.π := rfl @[simp] lemma fork.app_one_eq_ι_comp_left (s : fork f g) : s.π.app one = s.ι ≫ f := by rw [←s.app_zero_eq_ι, ←s.w left, parallel_pair_map_left] @[reassoc] lemma fork.app_one_eq_ι_comp_right (s : fork f g) : s.π.app one = s.ι ≫ g := by rw [←s.app_zero_eq_ι, ←s.w right, parallel_pair_map_right] @[simp] lemma cofork.app_zero_eq_comp_π_left (s : cofork f g) : s.ι.app zero = f ≫ s.π := by rw [←s.app_one_eq_π, ←s.w left, parallel_pair_map_left] @[reassoc] lemma cofork.app_zero_eq_comp_π_right (s : cofork f g) : s.ι.app zero = g ≫ s.π := by rw [←s.app_one_eq_π, ←s.w right, parallel_pair_map_right] /-- A fork on `f g : X ⟶ Y` is determined by the morphism `ι : P ⟶ X` satisfying `ι ≫ f = ι ≫ g`. -/ @[simps] def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g := { X := P, π := { app := λ X, begin cases X, exact ι, exact ι ≫ f, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- See note [dsimp, simp]. { exact w }, { dsimp, simp, }, end } } /-- A cofork on `f g : X ⟶ Y` is determined by the morphism `π : Y ⟶ P` satisfying `f ≫ π = g ≫ π`. -/ @[simps] def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g := { X := P, ι := { app := λ X, walking_parallel_pair.cases_on X (f ≫ π) π, naturality' := λ i j f, by { cases f; dsimp; simp [w] } } } -- See note [dsimp, simp] @[simp] lemma fork.ι_of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).ι = ι := rfl @[simp] lemma cofork.π_of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : (cofork.of_π π w).π = π := rfl @[simp, reassoc] lemma fork.condition (t : fork f g) : t.ι ≫ f = t.ι ≫ g := by rw [←t.app_one_eq_ι_comp_left, ←t.app_one_eq_ι_comp_right] @[simp, reassoc] lemma cofork.condition (t : cofork f g) : f ≫ t.π = g ≫ t.π := by rw [←t.app_zero_eq_comp_π_left, ←t.app_zero_eq_comp_π_right] /-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the first map -/ lemma fork.equalizer_ext (s : fork f g) {W : C} {k l : W ⟶ s.X} (h : k ≫ s.ι = l ≫ s.ι) : ∀ (j : walking_parallel_pair), k ≫ s.π.app j = l ≫ s.π.app j | zero := h | one := by rw [s.app_one_eq_ι_comp_left, reassoc_of h] /-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for the second map -/ lemma cofork.coequalizer_ext (s : cofork f g) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : ∀ (j : walking_parallel_pair), s.ι.app j ≫ k = s.ι.app j ≫ l | zero := by simp only [s.app_zero_eq_comp_π_left, category.assoc, h] | one := h lemma fork.is_limit.hom_ext {s : fork f g} (hs : is_limit s) {W : C} {k l : W ⟶ s.X} (h : k ≫ fork.ι s = l ≫ fork.ι s) : k = l := hs.hom_ext $ fork.equalizer_ext _ h lemma cofork.is_colimit.hom_ext {s : cofork f g} (hs : is_colimit s) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : k = l := hs.hom_ext $ cofork.coequalizer_ext _ h @[simp, reassoc] lemma fork.is_limit.lift_ι {s t : fork f g} (hs : is_limit s) : hs.lift t ≫ s.ι = t.ι := hs.fac _ _ @[simp, reassoc] lemma cofork.is_colimit.π_desc {s t : cofork f g} (hs : is_colimit s) : s.π ≫ hs.desc t = t.π := hs.fac _ _ /-- If `s` is a limit fork over `f` and `g`, then a morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ s.X` such that `l ≫ fork.ι s = k`. -/ def fork.is_limit.lift' {s : fork f g} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ s.X // l ≫ fork.ι s = k} := ⟨hs.lift $ fork.of_ι _ h, hs.fac _ _⟩ /-- If `s` is a colimit cofork over `f` and `g`, then a morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/ def cofork.is_colimit.desc' {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : s.X ⟶ W // cofork.π s ≫ l = k} := ⟨hs.desc $ cofork.of_π _ h, hs.fac _ _⟩ lemma fork.is_limit.exists_unique {s : fork f g} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : ∃! (l : W ⟶ s.X), l ≫ fork.ι s = k := ⟨hs.lift $ fork.of_ι _ h, hs.fac _ _, λ m hm, fork.is_limit.hom_ext hs $ hm.symm ▸ (hs.fac (fork.of_ι _ h) walking_parallel_pair.zero).symm⟩ lemma cofork.is_colimit.exists_unique {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : ∃! (d : s.X ⟶ W), cofork.π s ≫ d = k := ⟨hs.desc $ cofork.of_π _ h, hs.fac _ _, λ m hm, cofork.is_colimit.hom_ext hs $ hm.symm ▸ (hs.fac (cofork.of_π _ h) walking_parallel_pair.one).symm⟩ /-- This is a slightly more convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ @[simps lift] def fork.is_limit.mk (t : fork f g) (lift : Π (s : fork f g), s.X ⟶ t.X) (fac : ∀ (s : fork f g), lift s ≫ fork.ι t = fork.ι s) (uniq : ∀ (s : fork f g) (m : s.X ⟶ t.X) (w : m ≫ t.ι = s.ι), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, walking_parallel_pair.cases_on j (fac s) $ by erw [←s.w left, ←t.w left, ←category.assoc, fac]; refl, uniq' := λ s m j, by tidy } /-- This is another convenient method to verify that a fork 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 fork.is_limit.mk' {X Y : C} {f g : X ⟶ Y} (t : fork f g) (create : Π (s : fork f g), {l // l ≫ t.ι = s.ι ∧ ∀ {m}, m ≫ t.ι = s.ι → m = l}) : is_limit t := fork.is_limit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 w) /-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def cofork.is_colimit.mk (t : cofork f g) (desc : Π (s : cofork f g), t.X ⟶ s.X) (fac : ∀ (s : cofork f g), cofork.π t ≫ desc s = cofork.π s) (uniq : ∀ (s : cofork f g) (m : t.X ⟶ s.X) (w : t.π ≫ m = s.π), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, walking_parallel_pair.cases_on j (by erw [←s.w left, ←t.w left, category.assoc, fac]; refl) (fac s), uniq' := by tidy } /-- This is another convenient method to verify that a fork 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 cofork.is_colimit.mk' {X Y : C} {f g : X ⟶ Y} (t : cofork f g) (create : Π (s : cofork f g), {l : t.X ⟶ s.X // t.π ≫ l = s.π ∧ ∀ {m}, t.π ≫ m = s.π → m = l}) : is_colimit t := cofork.is_colimit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 w) /-- Noncomputably make a limit cone from the existence of unique factorizations. -/ def fork.is_limit.of_exists_unique {t : fork f g} (hs : ∀ (s : fork f g), ∃! l : s.X ⟶ t.X, l ≫ fork.ι t = fork.ι s) : is_limit t := by { choose d hd hd' using hs, exact fork.is_limit.mk _ d hd (λ s m hm, hd' _ _ hm) } /-- Noncomputably make a colimit cocone from the existence of unique factorizations. -/ def cofork.is_colimit.of_exists_unique {t : cofork f g} (hs : ∀ (s : cofork f g), ∃! d : t.X ⟶ s.X, cofork.π t ≫ d = cofork.π s) : is_colimit t := by { choose d hd hd' using hs, exact cofork.is_colimit.mk _ d hd (λ s m hm, hd' _ _ hm) } /-- Given a limit cone for the pair `f g : X ⟶ Y`, for any `Z`, morphisms from `Z` to its point are in bijection with morphisms `h : Z ⟶ X` such that `h ≫ f = h ≫ g`. Further, this bijection is natural in `Z`: see `fork.is_limit.hom_iso_natural`. This is a special case of `is_limit.hom_iso'`, often useful to construct adjunctions. -/ @[simps] def fork.is_limit.hom_iso {X Y : C} {f g : X ⟶ Y} {t : fork f g} (ht : is_limit t) (Z : C) : (Z ⟶ t.X) ≃ {h : Z ⟶ X // h ≫ f = h ≫ g} := { to_fun := λ k, ⟨k ≫ t.ι, by simp only [category.assoc, t.condition]⟩, inv_fun := λ h, (fork.is_limit.lift' ht _ h.prop).1, left_inv := λ k, fork.is_limit.hom_ext ht (fork.is_limit.lift' _ _ _).prop, right_inv := λ h, subtype.ext (fork.is_limit.lift' ht _ _).prop } /-- The bijection of `fork.is_limit.hom_iso` is natural in `Z`. -/ lemma fork.is_limit.hom_iso_natural {X Y : C} {f g : X ⟶ Y} {t : fork f g} (ht : is_limit t) {Z Z' : C} (q : Z' ⟶ Z) (k : Z ⟶ t.X) : (fork.is_limit.hom_iso ht _ (q ≫ k) : Z' ⟶ X) = q ≫ (fork.is_limit.hom_iso ht _ k : Z ⟶ X) := category.assoc _ _ _ /-- Given a colimit cocone for the pair `f g : X ⟶ Y`, for any `Z`, morphisms from the cocone point to `Z` are in bijection with morphisms `h : Y ⟶ Z` such that `f ≫ h = g ≫ h`. Further, this bijection is natural in `Z`: see `cofork.is_colimit.hom_iso_natural`. This is a special case of `is_colimit.hom_iso'`, often useful to construct adjunctions. -/ @[simps] def cofork.is_colimit.hom_iso {X Y : C} {f g : X ⟶ Y} {t : cofork f g} (ht : is_colimit t) (Z : C) : (t.X ⟶ Z) ≃ {h : Y ⟶ Z // f ≫ h = g ≫ h} := { to_fun := λ k, ⟨t.π ≫ k, by simp only [←category.assoc, t.condition]⟩, inv_fun := λ h, (cofork.is_colimit.desc' ht _ h.prop).1, left_inv := λ k, cofork.is_colimit.hom_ext ht (cofork.is_colimit.desc' _ _ _).prop, right_inv := λ h, subtype.ext (cofork.is_colimit.desc' ht _ _).prop } /-- The bijection of `cofork.is_colimit.hom_iso` is natural in `Z`. -/ lemma cofork.is_colimit.hom_iso_natural {X Y : C} {f g : X ⟶ Y} {t : cofork f g} {Z Z' : C} (q : Z ⟶ Z') (ht : is_colimit t) (k : t.X ⟶ Z) : (cofork.is_colimit.hom_iso ht _ (k ≫ q) : Y ⟶ Z') = (cofork.is_colimit.hom_iso ht _ k : Y ⟶ Z) ≫ q := (category.assoc _ _ _).symm /-- This is a helper construction that can be useful when verifying that a category has all equalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a fork on `F.map left` and `F.map right`, we get a cone on `F`. If you're thinking about using this, have a look at `has_equalizers_of_has_limit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cone.of_fork {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy), naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } /-- This is a helper construction that can be useful when verifying that a category has all coequalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a cofork on `F.map left` and `F.map right`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_coequalizers_of_has_colimit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cocone.of_cofork {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X, naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } @[simp] lemma cone.of_fork_π {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ι {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cone on `F`, we get a fork on `F.map left` and `F.map right`. -/ def fork.of_cone {F : walking_parallel_pair ⥤ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } } /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cocone on `F`, we get a cofork on `F.map left` and `F.map right`. -/ def cofork.of_cocone {F : walking_parallel_pair ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } } @[simp] lemma fork.of_cone_π {F : walking_parallel_pair ⥤ C} (t : cone F) (j) : (fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair ⥤ C} (t : cocone F) (j) : (cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl @[simp] lemma fork.ι_postcompose {f' g' : X ⟶ Y} {α : parallel_pair f g ⟶ parallel_pair f' g'} {c : fork f g} : fork.ι ((cones.postcompose α).obj c) = c.ι ≫ α.app _ := rfl @[simp] lemma cofork.π_precompose {f' g' : X ⟶ Y} {α : parallel_pair f g ⟶ parallel_pair f' g'} {c : cofork f' g'} : cofork.π ((cocones.precompose α).obj c) = α.app _ ≫ c.π := rfl /-- Helper function for constructing morphisms between equalizer forks. -/ @[simps] def fork.mk_hom {s t : fork f g} (k : s.X ⟶ t.X) (w : k ≫ t.ι = s.ι) : s ⟶ t := { hom := k, w' := begin rintro ⟨_|_⟩, { exact w }, { simp only [fork.app_one_eq_ι_comp_left, reassoc_of w] }, end } /-- To construct an isomorphism between forks, it suffices to give an isomorphism between the cone points and check that it commutes with the `ι` morphisms. -/ @[simps] def fork.ext {s t : fork f g} (i : s.X ≅ t.X) (w : i.hom ≫ t.ι = s.ι) : s ≅ t := { hom := fork.mk_hom i.hom w, inv := fork.mk_hom i.inv (by rw [← w, iso.inv_hom_id_assoc]) } /-- Every fork is isomorphic to one of the form `fork.of_ι _ _`. -/ def fork.iso_fork_of_ι (c : fork f g) : c ≅ fork.of_ι c.ι c.condition := fork.ext (by simp only [fork.of_ι_X, functor.const.obj_obj]) (by simp) /-- Helper function for constructing morphisms between coequalizer coforks. -/ @[simps] def cofork.mk_hom {s t : cofork f g} (k : s.X ⟶ t.X) (w : s.π ≫ k = t.π) : s ⟶ t := { hom := k, w' := begin rintro ⟨_|_⟩, { simp [cofork.app_zero_eq_comp_π_left, w] }, { exact w } end } @[simp, reassoc] lemma fork.hom_comp_ι {s t : fork f g} (f : s ⟶ t) : f.hom ≫ t.ι = s.ι := by tidy @[simp, reassoc] lemma fork.π_comp_hom {s t : cofork f g} (f : s ⟶ t) : s.π ≫ f.hom = t.π := by tidy /-- To construct an isomorphism between coforks, it suffices to give an isomorphism between the cocone points and check that it commutes with the `π` morphisms. -/ @[simps] def cofork.ext {s t : cofork f g} (i : s.X ≅ t.X) (w : s.π ≫ i.hom = t.π) : s ≅ t := { hom := cofork.mk_hom i.hom w, inv := cofork.mk_hom i.inv (by rw [iso.comp_inv_eq, w]) } /-- Every cofork is isomorphic to one of the form `cofork.of_π _ _`. -/ def cofork.iso_cofork_of_π (c : cofork f g) : c ≅ cofork.of_π c.π c.condition := cofork.ext (by simp only [cofork.of_π_X, functor.const.obj_obj]) (by dsimp; simp) variables (f g) section /-- `has_equalizer f g` represents a particular choice of limiting cone for the parallel pair of morphisms `f` and `g`. -/ abbreviation has_equalizer := has_limit (parallel_pair f g) variables [has_equalizer f g] /-- If an equalizer of `f` and `g` exists, we can access an arbitrary choice of such by saying `equalizer f g`. -/ abbreviation equalizer : C := limit (parallel_pair f g) /-- If an equalizer of `f` and `g` exists, we can access the inclusion `equalizer f g ⟶ X` by saying `equalizer.ι f g`. -/ abbreviation equalizer.ι : equalizer f g ⟶ X := limit.π (parallel_pair f g) zero /-- An equalizer cone for a parallel pair `f` and `g`. -/ abbreviation equalizer.fork : fork f g := limit.cone (parallel_pair f g) @[simp] lemma equalizer.fork_ι : (equalizer.fork f g).ι = equalizer.ι f g := rfl @[simp] lemma equalizer.fork_π_app_zero : (equalizer.fork f g).π.app zero = equalizer.ι f g := rfl @[reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g := fork.condition $ limit.cone $ parallel_pair f g /-- The equalizer built from `equalizer.ι f g` is limiting. -/ def equalizer_is_equalizer : is_limit (fork.of_ι (equalizer.ι f g) (equalizer.condition f g)) := is_limit.of_iso_limit (limit.is_limit _) (fork.ext (iso.refl _) (by tidy)) variables {f g} /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` factors through the equalizer of `f` and `g` via `equalizer.lift : W ⟶ equalizer f g`. -/ abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ι k h) @[simp, reassoc] lemma equalizer.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : equalizer.lift k h ≫ equalizer.ι f g = k := limit.lift_π _ _ /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ equalizer f g` satisfying `l ≫ equalizer.ι f g = k`. -/ def equalizer.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ equalizer f g // l ≫ equalizer.ι f g = k} := ⟨equalizer.lift k h, equalizer.lift_ι _ _⟩ /-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer map. -/ @[ext] lemma equalizer.hom_ext {W : C} {k l : W ⟶ equalizer f g} (h : k ≫ equalizer.ι f g = l ≫ equalizer.ι f g) : k = l := fork.is_limit.hom_ext (limit.is_limit _) h lemma equalizer.exists_unique {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : ∃! (l : W ⟶ equalizer f g), l ≫ equalizer.ι f g = k := fork.is_limit.exists_unique (limit.is_limit _) _ h /-- An equalizer morphism is a monomorphism -/ instance equalizer.ι_mono : mono (equalizer.ι f g) := { right_cancellation := λ Z h k w, equalizer.hom_ext w } end section variables {f g} /-- The equalizer morphism in any limit cone is a monomorphism. -/ lemma mono_of_is_limit_fork {c : fork f g} (i : is_limit c) : mono (fork.ι c) := { right_cancellation := λ Z h k w, fork.is_limit.hom_ext i w } end section variables {f g} /-- The identity determines a cone on the equalizer diagram of `f` and `g` if `f = g`. -/ def id_fork (h : f = g) : fork f g := fork.of_ι (𝟙 X) $ h ▸ rfl /-- The identity on `X` is an equalizer of `(f, g)`, if `f = g`. -/ def is_limit_id_fork (h : f = g) : is_limit (id_fork h) := fork.is_limit.mk _ (λ s, fork.ι s) (λ s, category.comp_id _) (λ s m h, by { convert h, exact (category.comp_id _).symm }) /-- Every equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ lemma is_iso_limit_cone_parallel_pair_of_eq (h₀ : f = g) {c : fork f g} (h : is_limit c) : is_iso c.ι := is_iso.of_iso $ is_limit.cone_point_unique_up_to_iso h $ is_limit_id_fork h₀ /-- The equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ lemma equalizer.ι_of_eq [has_equalizer f g] (h : f = g) : is_iso (equalizer.ι f g) := is_iso_limit_cone_parallel_pair_of_eq h $ limit.is_limit _ /-- Every equalizer of `(f, f)` is an isomorphism. -/ lemma is_iso_limit_cone_parallel_pair_of_self {c : fork f f} (h : is_limit c) : is_iso c.ι := is_iso_limit_cone_parallel_pair_of_eq rfl h /-- An equalizer that is an epimorphism is an isomorphism. -/ lemma is_iso_limit_cone_parallel_pair_of_epi {c : fork f g} (h : is_limit c) [epi (c.ι)] : is_iso c.ι := is_iso_limit_cone_parallel_pair_of_eq ((cancel_epi _).1 (fork.condition c)) h /-- Two morphisms are equal if there is a fork whose inclusion is epi. -/ lemma eq_of_epi_fork_ι (t : fork f g) [epi (fork.ι t)] : f = g := (cancel_epi (fork.ι t)).1 $ fork.condition t /-- If the equalizer of two morphisms is an epimorphism, then the two morphisms are equal. -/ lemma eq_of_epi_equalizer [has_equalizer f g] [epi (equalizer.ι f g)] : f = g := (cancel_epi (equalizer.ι f g)).1 $ equalizer.condition _ _ end instance has_equalizer_of_self : has_equalizer f f := has_limit.mk { cone := id_fork rfl, is_limit := is_limit_id_fork rfl } /-- The equalizer inclusion for `(f, f)` is an isomorphism. -/ instance equalizer.ι_of_self : is_iso (equalizer.ι f f) := equalizer.ι_of_eq rfl /-- The equalizer of a morphism with itself is isomorphic to the source. -/ def equalizer.iso_source_of_self : equalizer f f ≅ X := as_iso (equalizer.ι f f) @[simp] lemma equalizer.iso_source_of_self_hom : (equalizer.iso_source_of_self f).hom = equalizer.ι f f := rfl @[simp] lemma equalizer.iso_source_of_self_inv : (equalizer.iso_source_of_self f).inv = equalizer.lift (𝟙 X) (by simp) := by { ext, simp [equalizer.iso_source_of_self], } section /-- `has_coequalizer f g` represents a particular choice of colimiting cocone for the parallel pair of morphisms `f` and `g`. -/ abbreviation has_coequalizer := has_colimit (parallel_pair f g) variables [has_coequalizer f g] /-- If a coequalizer of `f` and `g` exists, we can access an arbitrary choice of such by saying `coequalizer f g`. -/ abbreviation coequalizer : C := colimit (parallel_pair f g) /-- If a coequalizer of `f` and `g` exists, we can access the corresponding projection by saying `coequalizer.π f g`. -/ abbreviation coequalizer.π : Y ⟶ coequalizer f g := colimit.ι (parallel_pair f g) one /-- An arbitrary choice of coequalizer cocone for a parallel pair `f` and `g`. -/ abbreviation coequalizer.cofork : cofork f g := colimit.cocone (parallel_pair f g) @[simp] lemma coequalizer.cofork_π : (coequalizer.cofork f g).π = coequalizer.π f g := rfl @[simp] lemma coequalizer.cofork_ι_app_one : (coequalizer.cofork f g).ι.app one = coequalizer.π f g := rfl @[reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g := cofork.condition $ colimit.cocone $ parallel_pair f g /-- The cofork built from `coequalizer.π f g` is colimiting. -/ def coequalizer_is_coequalizer : is_colimit (cofork.of_π (coequalizer.π f g) (coequalizer.condition f g)) := is_colimit.of_iso_colimit (colimit.is_colimit _) (cofork.ext (iso.refl _) (by tidy)) variables {f g} /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` factors through the coequalizer of `f` and `g` via `coequalizer.desc : coequalizer f g ⟶ W`. -/ abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W := colimit.desc (parallel_pair f g) (cofork.of_π k h) @[simp, reassoc] lemma coequalizer.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer.π f g ≫ coequalizer.desc k h = k := colimit.ι_desc _ _ /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : coequalizer f g ⟶ W` satisfying `coequalizer.π ≫ g = l`. -/ def coequalizer.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : coequalizer f g ⟶ W // coequalizer.π f g ≫ l = k} := ⟨coequalizer.desc k h, coequalizer.π_desc _ _⟩ /-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer map -/ @[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g ⟶ W} (h : coequalizer.π f g ≫ k = coequalizer.π f g ≫ l) : k = l := cofork.is_colimit.hom_ext (colimit.is_colimit _) h lemma coequalizer.exists_unique {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : ∃! (d : coequalizer f g ⟶ W), coequalizer.π f g ≫ d = k := cofork.is_colimit.exists_unique (colimit.is_colimit _) _ h /-- A coequalizer morphism is an epimorphism -/ instance coequalizer.π_epi : epi (coequalizer.π f g) := { left_cancellation := λ Z h k w, coequalizer.hom_ext w } end section variables {f g} /-- The coequalizer morphism in any colimit cocone is an epimorphism. -/ lemma epi_of_is_colimit_cofork {c : cofork f g} (i : is_colimit c) : epi c.π := { left_cancellation := λ Z h k w, cofork.is_colimit.hom_ext i w } end section variables {f g} /-- The identity determines a cocone on the coequalizer diagram of `f` and `g`, if `f = g`. -/ def id_cofork (h : f = g) : cofork f g := cofork.of_π (𝟙 Y) $ h ▸ rfl /-- The identity on `Y` is a coequalizer of `(f, g)`, where `f = g`. -/ def is_colimit_id_cofork (h : f = g) : is_colimit (id_cofork h) := cofork.is_colimit.mk _ (λ s, cofork.π s) (λ s, category.id_comp _) (λ s m h, by { convert h, exact (category.id_comp _).symm }) /-- Every coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ lemma is_iso_colimit_cocone_parallel_pair_of_eq (h₀ : f = g) {c : cofork f g} (h : is_colimit c) : is_iso c.π := is_iso.of_iso $ is_colimit.cocone_point_unique_up_to_iso (is_colimit_id_cofork h₀) h /-- The coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ lemma coequalizer.π_of_eq [has_coequalizer f g] (h : f = g) : is_iso (coequalizer.π f g) := is_iso_colimit_cocone_parallel_pair_of_eq h $ colimit.is_colimit _ /-- Every coequalizer of `(f, f)` is an isomorphism. -/ lemma is_iso_colimit_cocone_parallel_pair_of_self {c : cofork f f} (h : is_colimit c) : is_iso c.π := is_iso_colimit_cocone_parallel_pair_of_eq rfl h /-- A coequalizer that is a monomorphism is an isomorphism. -/ lemma is_iso_limit_cocone_parallel_pair_of_epi {c : cofork f g} (h : is_colimit c) [mono c.π] : is_iso c.π := is_iso_colimit_cocone_parallel_pair_of_eq ((cancel_mono _).1 (cofork.condition c)) h /-- Two morphisms are equal if there is a cofork whose projection is mono. -/ lemma eq_of_mono_cofork_π (t : cofork f g) [mono (cofork.π t)] : f = g := (cancel_mono (cofork.π t)).1 $ cofork.condition t /-- If the coequalizer of two morphisms is a monomorphism, then the two morphisms are equal. -/ lemma eq_of_mono_coequalizer [has_coequalizer f g] [mono (coequalizer.π f g)] : f = g := (cancel_mono (coequalizer.π f g)).1 $ coequalizer.condition _ _ end instance has_coequalizer_of_self : has_coequalizer f f := has_colimit.mk { cocone := id_cofork rfl, is_colimit := is_colimit_id_cofork rfl } /-- The coequalizer projection for `(f, f)` is an isomorphism. -/ instance coequalizer.π_of_self : is_iso (coequalizer.π f f) := coequalizer.π_of_eq rfl /-- The coequalizer of a morphism with itself is isomorphic to the target. -/ def coequalizer.iso_target_of_self : coequalizer f f ≅ Y := (as_iso (coequalizer.π f f)).symm @[simp] lemma coequalizer.iso_target_of_self_hom : (coequalizer.iso_target_of_self f).hom = coequalizer.desc (𝟙 Y) (by simp) := by { ext, simp [coequalizer.iso_target_of_self], } @[simp] lemma coequalizer.iso_target_of_self_inv : (coequalizer.iso_target_of_self f).inv = coequalizer.π f f := rfl section comparison variables {D : Type u₂} [category.{v₂} D] (G : C ⥤ D) /-- The comparison morphism for the equalizer of `f,g`. This is an isomorphism iff `G` preserves the equalizer of `f,g`; see `category_theory/limits/preserves/shapes/equalizers.lean` -/ def equalizer_comparison [has_equalizer f g] [has_equalizer (G.map f) (G.map g)] : G.obj (equalizer f g) ⟶ equalizer (G.map f) (G.map g) := equalizer.lift (G.map (equalizer.ι _ _)) (by simp only [←G.map_comp, equalizer.condition]) @[simp, reassoc] lemma equalizer_comparison_comp_π [has_equalizer f g] [has_equalizer (G.map f) (G.map g)] : equalizer_comparison f g G ≫ equalizer.ι (G.map f) (G.map g) = G.map (equalizer.ι f g) := equalizer.lift_ι _ _ @[simp, reassoc] lemma map_lift_equalizer_comparison [has_equalizer f g] [has_equalizer (G.map f) (G.map g)] {Z : C} {h : Z ⟶ X} (w : h ≫ f = h ≫ g) : G.map (equalizer.lift h w) ≫ equalizer_comparison f g G = equalizer.lift (G.map h) (by simp only [←G.map_comp, w]) := by { ext, simp [← G.map_comp] } /-- The comparison morphism for the coequalizer of `f,g`. -/ def coequalizer_comparison [has_coequalizer f g] [has_coequalizer (G.map f) (G.map g)] : coequalizer (G.map f) (G.map g) ⟶ G.obj (coequalizer f g) := coequalizer.desc (G.map (coequalizer.π _ _)) (by simp only [←G.map_comp, coequalizer.condition]) @[simp, reassoc] lemma ι_comp_coequalizer_comparison [has_coequalizer f g] [has_coequalizer (G.map f) (G.map g)] : coequalizer.π _ _ ≫ coequalizer_comparison f g G = G.map (coequalizer.π _ _) := coequalizer.π_desc _ _ @[simp, reassoc] lemma coequalizer_comparison_map_desc [has_coequalizer f g] [has_coequalizer (G.map f) (G.map g)] {Z : C} {h : Y ⟶ Z} (w : f ≫ h = g ≫ h) : coequalizer_comparison f g G ≫ G.map (coequalizer.desc h w) = coequalizer.desc (G.map h) (by simp only [←G.map_comp, w]) := by { ext, simp [← G.map_comp] } end comparison variables (C) /-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/ abbreviation has_equalizers := has_limits_of_shape walking_parallel_pair C /-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/ abbreviation has_coequalizers := has_colimits_of_shape walking_parallel_pair C /-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/ lemma has_equalizers_of_has_limit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_limit (parallel_pair f g)] : has_equalizers C := { has_limit := λ F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } /-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/ lemma has_coequalizers_of_has_colimit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_colimit (parallel_pair f g)] : has_coequalizers C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_parallel_pair F) } section -- In this section we show that a split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. variables {C} [split_mono f] /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone. -/ @[simps {rhs_md := semireducible}] def cone_of_split_mono : fork (𝟙 Y) (retraction f ≫ f) := fork.of_ι f (by simp) @[simp] lemma cone_of_split_mono_ι : (cone_of_split_mono f).ι = f := rfl /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. -/ def split_mono_equalizes {X Y : C} (f : X ⟶ Y) [split_mono f] : is_limit (cone_of_split_mono f) := fork.is_limit.mk' _ $ λ s, ⟨s.ι ≫ retraction f, by { dsimp, rw [category.assoc, ←s.condition], apply category.comp_id }, λ m hm, by simp [←hm]⟩ end /-- We show that the converse to `split_mono_equalizes` is true: Whenever `f` equalizes `(r ≫ f)` and `(𝟙 Y)`, then `r` is a retraction of `f`. -/ def split_mono_of_equalizer {X Y : C} {f : X ⟶ Y} {r : Y ⟶ X} (hr : f ≫ r ≫ f = f) (h : is_limit (fork.of_ι f (hr.trans (category.comp_id _).symm : f ≫ r ≫ f = f ≫ 𝟙 Y))) : split_mono f := { retraction := r, id' := fork.is_limit.hom_ext h ((category.assoc _ _ _).trans $ hr.trans (category.id_comp _).symm) } variables {C f g} /-- The fork obtained by postcomposing an equalizer fork with a monomorphism is an equalizer. -/ def is_equalizer_comp_mono {c : fork f g} (i : is_limit c) {Z : C} (h : Y ⟶ Z) [hm : mono h] : is_limit (fork.of_ι c.ι (by simp [reassoc_of c.condition]) : fork (f ≫ h) (g ≫ h)) := fork.is_limit.mk' _ $ λ s, let s' : fork f g := fork.of_ι s.ι (by apply hm.right_cancellation; simp [s.condition]) in let l := fork.is_limit.lift' i s'.ι s'.condition in ⟨l.1, l.2, λ m hm, by apply fork.is_limit.hom_ext i; rw fork.ι_of_ι at hm; rw hm; exact l.2.symm⟩ variables (C f g) @[instance] lemma has_equalizer_comp_mono [has_equalizer f g] {Z : C} (h : Y ⟶ Z) [mono h] : has_equalizer (f ≫ h) (g ≫ h) := ⟨⟨{ cone := _, is_limit := is_equalizer_comp_mono (limit.is_limit _) h }⟩⟩ /-- An equalizer of an idempotent morphism and the identity is split mono. -/ @[simps] def split_mono_of_idempotent_of_is_limit_fork {X : C} {f : X ⟶ X} (hf : f ≫ f = f) {c : fork (𝟙 X) f} (i : is_limit c) : split_mono c.ι := { retraction := i.lift (fork.of_ι f (by simp [hf])), id' := begin letI := mono_of_is_limit_fork i, rw [←cancel_mono_id c.ι, category.assoc, fork.is_limit.lift_ι, fork.ι_of_ι, ←c.condition], exact category.comp_id c.ι end } /-- The equalizer of an idempotent morphism and the identity is split mono. -/ def split_mono_of_idempotent_equalizer {X : C} {f : X ⟶ X} (hf : f ≫ f = f) [has_equalizer (𝟙 X) f] : split_mono (equalizer.ι (𝟙 X) f) := split_mono_of_idempotent_of_is_limit_fork _ hf (limit.is_limit _) section -- In this section we show that a split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. variables {C} [split_epi f] /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone. -/ @[simps {rhs_md := semireducible}] def cocone_of_split_epi : cofork (𝟙 X) (f ≫ section_ f) := cofork.of_π f (by simp) @[simp] lemma cocone_of_split_epi_π : (cocone_of_split_epi f).π = f := rfl /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. -/ def split_epi_coequalizes {X Y : C} (f : X ⟶ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) := cofork.is_colimit.mk' _ $ λ s, ⟨section_ f ≫ s.π, by { dsimp, rw [← category.assoc, ← s.condition, category.id_comp] }, λ m hm, by simp [← hm]⟩ end /-- We show that the converse to `split_epi_equalizes` is true: Whenever `f` coequalizes `(f ≫ s)` and `(𝟙 X)`, then `s` is a section of `f`. -/ def split_epi_of_coequalizer {X Y : C} {f : X ⟶ Y} {s : Y ⟶ X} (hs : f ≫ s ≫ f = f) (h : is_colimit (cofork.of_π f ((category.assoc _ _ _).trans $ hs.trans (category.id_comp f).symm : (f ≫ s) ≫ f = 𝟙 X ≫ f))) : split_epi f := { section_ := s, id' := cofork.is_colimit.hom_ext h (hs.trans (category.comp_id _).symm) } variables {C f g} /-- The cofork obtained by precomposing a coequalizer cofork with an epimorphism is a coequalizer. -/ def is_coequalizer_epi_comp {c : cofork f g} (i : is_colimit c) {W : C} (h : W ⟶ X) [hm : epi h] : is_colimit (cofork.of_π c.π (by simp) : cofork (h ≫ f) (h ≫ g)) := cofork.is_colimit.mk' _ $ λ s, let s' : cofork f g := cofork.of_π s.π (by apply hm.left_cancellation; simp_rw [←category.assoc, s.condition]) in let l := cofork.is_colimit.desc' i s'.π s'.condition in ⟨l.1, l.2, λ m hm,by apply cofork.is_colimit.hom_ext i; rw cofork.π_of_π at hm; rw hm; exact l.2.symm⟩ lemma has_coequalizer_epi_comp [has_coequalizer f g] {W : C} (h : W ⟶ X) [hm : epi h] : has_coequalizer (h ≫ f) (h ≫ g) := ⟨⟨{ cocone := _, is_colimit := is_coequalizer_epi_comp (colimit.is_colimit _) h }⟩⟩ variables (C f g) /-- A coequalizer of an idempotent morphism and the identity is split epi. -/ @[simps] def split_epi_of_idempotent_of_is_colimit_cofork {X : C} {f : X ⟶ X} (hf : f ≫ f = f) {c : cofork (𝟙 X) f} (i : is_colimit c) : split_epi c.π := { section_ := i.desc (cofork.of_π f (by simp [hf])), id' := begin letI := epi_of_is_colimit_cofork i, rw [← cancel_epi_id c.π, ← category.assoc, cofork.is_colimit.π_desc, cofork.π_of_π, ← c.condition], exact category.id_comp _, end } /-- The coequalizer of an idempotent morphism and the identity is split epi. -/ def split_epi_of_idempotent_coequalizer {X : C} {f : X ⟶ X} (hf : f ≫ f = f) [has_coequalizer (𝟙 X) f] : split_epi (coequalizer.π (𝟙 X) f) := split_epi_of_idempotent_of_is_colimit_cofork _ hf (colimit.is_colimit _) end category_theory.limits
3ad0dea87933d11f2ab1027866a31f179e13c765
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/data/equiv/algebra.lean
9dc042d5d781acdd8c76a74ff81a7a5b62ddeb76
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
7,822
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.equiv.basic algebra.field universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} namespace equiv section group variables [group α] protected def mul_left (a : α) : α ≃ α := { to_fun := λx, a * x, inv_fun := λx, a⁻¹ * x, left_inv := assume x, show a⁻¹ * (a * x) = x, from inv_mul_cancel_left a x, right_inv := assume x, show a * (a⁻¹ * x) = x, from mul_inv_cancel_left a x } attribute [to_additive equiv.add_left._proof_1] equiv.mul_left._proof_1 attribute [to_additive equiv.add_left._proof_2] equiv.mul_left._proof_2 attribute [to_additive equiv.add_left] equiv.mul_left protected def mul_right (a : α) : α ≃ α := { to_fun := λx, x * a, inv_fun := λx, x * a⁻¹, left_inv := assume x, show (x * a) * a⁻¹ = x, from mul_inv_cancel_right x a, right_inv := assume x, show (x * a⁻¹) * a = x, from inv_mul_cancel_right x a } attribute [to_additive equiv.add_right._proof_1] equiv.mul_right._proof_1 attribute [to_additive equiv.add_right._proof_2] equiv.mul_right._proof_2 attribute [to_additive equiv.add_right] equiv.mul_right protected def inv (α) [group α] : α ≃ α := { to_fun := λa, a⁻¹, inv_fun := λa, a⁻¹, left_inv := assume a, inv_inv a, right_inv := assume a, inv_inv a } attribute [to_additive equiv.neg._proof_1] equiv.inv._proof_1 attribute [to_additive equiv.neg._proof_2] equiv.inv._proof_2 attribute [to_additive equiv.neg] equiv.inv def units_equiv_ne_zero (α : Type*) [field α] : units α ≃ {a : α | a ≠ 0} := ⟨λ a, ⟨a.1, units.ne_zero _⟩, λ a, units.mk0 _ a.2, λ ⟨_, _, _, _⟩, units.ext rfl, λ ⟨_, _⟩, rfl⟩ @[simp] lemma coe_units_equiv_ne_zero [field α] (a : units α) : ((units_equiv_ne_zero α a) : α) = a := rfl end group section instances variables (e : α ≃ β) protected def has_zero [has_zero β] : has_zero α := ⟨e.symm 0⟩ lemma zero_def [has_zero β] : @has_zero.zero _ (equiv.has_zero e) = e.symm 0 := rfl protected def has_one [has_one β] : has_one α := ⟨e.symm 1⟩ lemma one_def [has_one β] : @has_one.one _ (equiv.has_one e) = e.symm 1 := rfl protected def has_mul [has_mul β] : has_mul α := ⟨λ x y, e.symm (e x * e y)⟩ lemma mul_def [has_mul β] (x y : α) : @has_mul.mul _ (equiv.has_mul e) x y = e.symm (e x * e y) := rfl protected def has_add [has_add β] : has_add α := ⟨λ x y, e.symm (e x + e y)⟩ lemma add_def [has_add β] (x y : α) : @has_add.add _ (equiv.has_add e) x y = e.symm (e x + e y) := rfl protected def has_inv [has_inv β] : has_inv α := ⟨λ x, e.symm (e x)⁻¹⟩ lemma inv_def [has_inv β] (x : α) : @has_inv.inv _ (equiv.has_inv e) x = e.symm (e x)⁻¹ := rfl protected def has_neg [has_neg β] : has_neg α := ⟨λ x, e.symm (-e x)⟩ lemma neg_def [has_neg β] (x : α) : @has_neg.neg _ (equiv.has_neg e) x = e.symm (-e x) := rfl protected def semigroup [semigroup β] : semigroup α := { mul_assoc := by simp [mul_def, mul_assoc], ..equiv.has_mul e } protected def comm_semigroup [comm_semigroup β] : comm_semigroup α := { mul_comm := by simp [mul_def, mul_comm], ..equiv.semigroup e } protected def monoid [monoid β] : monoid α := { one_mul := by simp [mul_def, one_def], mul_one := by simp [mul_def, one_def], ..equiv.semigroup e, ..equiv.has_one e } protected def comm_monoid [comm_monoid β] : comm_monoid α := { ..equiv.comm_semigroup e, ..equiv.monoid e } protected def group [group β] : group α := { mul_left_inv := by simp [mul_def, inv_def, one_def], ..equiv.monoid e, ..equiv.has_inv e } protected def comm_group [comm_group β] : comm_group α := { ..equiv.group e, ..equiv.comm_semigroup e } protected def add_semigroup [add_semigroup β] : add_semigroup α := @additive.add_semigroup _ (@equiv.semigroup _ _ e multiplicative.semigroup) protected def add_comm_semigroup [add_comm_semigroup β] : add_comm_semigroup α := @additive.add_comm_semigroup _ (@equiv.comm_semigroup _ _ e multiplicative.comm_semigroup) protected def add_monoid [add_monoid β] : add_monoid α := @additive.add_monoid _ (@equiv.monoid _ _ e multiplicative.monoid) protected def add_comm_monoid [add_comm_monoid β] : add_comm_monoid α := @additive.add_comm_monoid _ (@equiv.comm_monoid _ _ e multiplicative.comm_monoid) protected def add_group [add_group β] : add_group α := @additive.add_group _ (@equiv.group _ _ e multiplicative.group) protected def add_comm_group [add_comm_group β] : add_comm_group α := @additive.add_comm_group _ (@equiv.comm_group _ _ e multiplicative.comm_group) protected def semiring [semiring β] : semiring α := { right_distrib := by simp [mul_def, add_def, add_mul], left_distrib := by simp [mul_def, add_def, mul_add], zero_mul := by simp [mul_def, zero_def], mul_zero := by simp [mul_def, zero_def], ..equiv.has_zero e, ..equiv.has_mul e, ..equiv.has_add e, ..equiv.monoid e, ..equiv.add_comm_monoid e } protected def comm_semiring [comm_semiring β] : comm_semiring α := { ..equiv.semiring e, ..equiv.comm_monoid e } protected def ring [ring β] : ring α := { ..equiv.semiring e, ..equiv.add_comm_group e } protected def comm_ring [comm_ring β] : comm_ring α := { ..equiv.comm_monoid e, ..equiv.ring e } protected def zero_ne_one_class [zero_ne_one_class β] : zero_ne_one_class α := { zero_ne_one := by simp [zero_def, one_def], ..equiv.has_zero e, ..equiv.has_one e } protected def nonzero_comm_ring [nonzero_comm_ring β] : nonzero_comm_ring α := { ..equiv.zero_ne_one_class e, ..equiv.comm_ring e } protected def domain [domain β] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := by simp [mul_def, zero_def, equiv.eq_symm_apply], ..equiv.has_zero e, ..equiv.zero_ne_one_class e, ..equiv.has_mul e, ..equiv.ring e } protected def integral_domain [integral_domain β] : integral_domain α := { ..equiv.domain e, ..equiv.nonzero_comm_ring e } protected def division_ring [division_ring β] : division_ring α := { inv_mul_cancel := λ _, by simp [mul_def, inv_def, zero_def, one_def, (equiv.symm_apply_eq _).symm]; exact inv_mul_cancel, mul_inv_cancel := λ _, by simp [mul_def, inv_def, zero_def, one_def, (equiv.symm_apply_eq _).symm]; exact mul_inv_cancel, ..equiv.has_zero e, ..equiv.has_one e, ..equiv.domain e, ..equiv.has_inv e } protected def field [field β] : field α := { ..equiv.integral_domain e, ..equiv.division_ring e } protected def discrete_field [discrete_field β] : discrete_field α := { has_decidable_eq := equiv.decidable_eq e, inv_zero := by simp [mul_def, inv_def, zero_def], ..equiv.has_mul e, ..equiv.has_inv e, ..equiv.has_zero e, ..equiv.field e } end instances end equiv structure ring_equiv (α β : Type*) [ring α] [ring β] extends α ≃ β := (hom : is_ring_hom to_fun) infix ` ≃r `:50 := ring_equiv namespace ring_equiv variables [ring α] [ring β] [ring γ] instance {e : α ≃r β} : is_ring_hom e.to_equiv := hom _ protected def refl (α : Type*) [ring α] : α ≃r α := { hom := is_ring_hom.id, .. equiv.refl α } protected def symm {α β : Type*} [ring α] [ring β] (e : α ≃r β) : β ≃r α := { hom := ⟨(equiv.symm_apply_eq _).2 e.hom.1.symm, λ x y, (equiv.symm_apply_eq _).2 $ show _ = e.to_equiv.to_fun _, by rw [e.2.2, e.1.4, e.1.4], λ x y, (equiv.symm_apply_eq _).2 $ show _ = e.to_equiv.to_fun _, by rw [e.2.3, e.1.4, e.1.4]⟩, .. e.to_equiv.symm } protected def trans {α β γ : Type*} [ring α] [ring β] [ring γ] (e₁ : α ≃r β) (e₂ : β ≃r γ) : α ≃r γ := { hom := is_ring_hom.comp _ _, .. e₁.1.trans e₂.1 } end ring_equiv
301d70aebe07f93a2bb188f7ac1d15f042dc7946
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Level.lean
40bfcadef55f1ac28bee47d82d903ba6444df0eb
[ "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
18,582
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Std.Data.HashMap import Std.Data.HashSet import Std.Data.PersistentHashMap import Std.Data.PersistentHashSet import Lean.Hygiene import Lean.Data.Name import Lean.Data.Format def Nat.imax (n m : Nat) : Nat := if m = 0 then 0 else Nat.max n m namespace Lean /-- Cached hash code, cached results, and other data for `Level`. hash : 32-bits hasMVar : 1-bit hasParam : 1-bit depth : 24-bits -/ def Level.Data := UInt64 instance : Inhabited Level.Data := inferInstanceAs (Inhabited UInt64) def Level.Data.hash (c : Level.Data) : USize := c.toUInt32.toUSize instance : BEq Level.Data := ⟨fun (a b : UInt64) => a == b⟩ def Level.Data.depth (c : Level.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Level.Data.hasMVar (c : Level.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Level.Data.hasParam (c : Level.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Level.mkData (h : USize) (depth : Nat) (hasMVar hasParam : Bool) : Level.Data := if depth > Nat.pow 2 24 - 1 then panic! "universe level depth is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasMVar.toUInt64.shiftLeft 32 + hasParam.toUInt64.shiftLeft 33 + depth.toUInt64.shiftLeft 40 r open Level inductive Level where | zero : Data → Level | succ : Level → Data → Level | max : Level → Level → Data → Level | imax : Level → Level → Data → Level | param : Name → Data → Level | mvar : Name → Data → Level deriving Inhabited namespace Level @[inline] def data : Level → Data | zero d => d | mvar _ d => d | param _ d => d | succ _ d => d | max _ _ d => d | imax _ _ d => d protected def hash (u : Level) : USize := u.data.hash instance : Hashable Level := ⟨Level.hash⟩ def depth (u : Level) : Nat := u.data.depth.toNat def hasMVar (u : Level) : Bool := u.data.hasMVar def hasParam (u : Level) : Bool := u.data.hasParam @[export lean_level_hash] def hashEx : Level → USize := Level.hash @[export lean_level_has_mvar] def hasMVarEx : Level → Bool := hasMVar @[export lean_level_has_param] def hasParamEx : Level → Bool := hasParam @[export lean_level_depth] def depthEx (u : Level) : UInt32 := u.data.depth end Level def levelZero := Level.zero $ mkData 2221 0 false false def mkLevelMVar (mvarId : Name) := Level.mvar mvarId $ mkData (mixHash 2237 $ hash mvarId) 0 true false def mkLevelParam (name : Name) := Level.param name $ mkData (mixHash 2239 $ hash name) 0 false true def mkLevelSucc (u : Level) := Level.succ u $ mkData (mixHash 2243 $ hash u) (u.depth + 1) u.hasMVar u.hasParam def mkLevelMax (u v : Level) := Level.max u v $ mkData (mixHash 2251 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def mkLevelIMax (u v : Level) := Level.imax u v $ mkData (mixHash 2267 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def levelOne := mkLevelSucc levelZero @[export lean_level_mk_zero] def mkLevelZeroEx : Unit → Level := fun _ => levelZero @[export lean_level_mk_succ] def mkLevelSuccEx : Level → Level := mkLevelSucc @[export lean_level_mk_mvar] def mkLevelMVarEx : Name → Level := mkLevelMVar @[export lean_level_mk_param] def mkLevelParamEx : Name → Level := mkLevelParam @[export lean_level_mk_max] def mkLevelMaxEx : Level → Level → Level := mkLevelMax @[export lean_level_mk_imax] def mkLevelIMaxEx : Level → Level → Level := mkLevelIMax namespace Level def isZero : Level → Bool | zero _ => true | _ => false def isSucc : Level → Bool | succ .. => true | _ => false def isMax : Level → Bool | max .. => true | _ => false def isIMax : Level → Bool | imax .. => true | _ => false def isMaxIMax : Level → Bool | max .. => true | imax .. => true | _ => false def isParam : Level → Bool | param .. => true | _ => false def isMVar : Level → Bool | mvar .. => true | _ => false def mvarId! : Level → Name | mvar mvarId _ => mvarId | _ => panic! "metavariable expected" /-- If result is true, then forall assignments `A` which assigns all parameters and metavariables occuring in `l`, `l[A] != zero` -/ def isNeverZero : Level → Bool | zero _ => false | param .. => false | mvar .. => false | succ .. => true | max l₁ l₂ _ => isNeverZero l₁ || isNeverZero l₂ | imax l₁ l₂ _ => isNeverZero l₂ def ofNat : Nat → Level | 0 => levelZero | n+1 => mkLevelSucc (ofNat n) def addOffsetAux : Nat → Level → Level | 0, u => u | (n+1), u => addOffsetAux n (mkLevelSucc u) def addOffset (u : Level) (n : Nat) : Level := u.addOffsetAux n def isExplicit : Level → Bool | zero _ => true | succ u _ => !u.hasMVar && !u.hasParam && isExplicit u | _ => false def getOffsetAux : Level → Nat → Nat | succ u _, r => getOffsetAux u (r+1) | _, r => r def getOffset (lvl : Level) : Nat := getOffsetAux lvl 0 def getLevelOffset : Level → Level | succ u _ => getLevelOffset u | u => u def toNat (lvl : Level) : Option Nat := match lvl.getLevelOffset with | zero _ => lvl.getOffset | _ => none @[extern "lean_level_eq"] protected constant beq (a : @& Level) (b : @& Level) : Bool instance : BEq Level := ⟨Level.beq⟩ /-- `occurs u l` return `true` iff `u` occurs in `l`. -/ def occurs : Level → Level → Bool | u, v@(succ v₁ _) => u == v || occurs u v₁ | u, v@(max v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v@(imax v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v => u == v def ctorToNat : Level → Nat | zero .. => 0 | param .. => 1 | mvar .. => 2 | succ .. => 3 | max .. => 4 | imax .. => 5 /- TODO: use well founded recursion. -/ partial def normLtAux : Level → Nat → Level → Nat → Bool | succ l₁ _, k₁, l₂, k₂ => normLtAux l₁ (k₁+1) l₂ k₂ | l₁, k₁, succ l₂ _, k₂ => normLtAux l₁ k₁ l₂ (k₂+1) | l₁@(max l₁₁ l₁₂ _), k₁, l₂@(max l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | l₁@(imax l₁₁ l₁₂ _), k₁, l₂@(imax l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | param n₁ _, k₁, param n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.lt n₁ n₂ -- use Name.lt because it is lexicographical | mvar n₁ _, k₁, mvar n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.quickLt n₁ n₂ -- metavariables are temporary, the actual order doesn't matter | l₁, k₁, l₂, k₂ => if l₁ == l₂ then k₁ < k₂ else ctorToNat l₁ < ctorToNat l₂ /-- A total order on level expressions that has the following properties - `succ l` is an immediate successor of `l`. - `zero` is the minimal element. This total order is used in the normalization procedure. -/ def normLt (l₁ l₂ : Level) : Bool := normLtAux l₁ 0 l₂ 0 private def isAlreadyNormalizedCheap : Level → Bool | zero _ => true | param _ _ => true | mvar _ _ => true | succ u _ => isAlreadyNormalizedCheap u | _ => false /- Auxiliary function used at `normalize` -/ private def mkIMaxAux : Level → Level → Level | _, u@(zero _) => u | zero _, u => u | u₁, u₂ => if u₁ == u₂ then u₁ else mkLevelIMax u₁ u₂ /- Auxiliary function used at `normalize` -/ @[specialize] private partial def getMaxArgsAux (normalize : Level → Level) : Level → Bool → Array Level → Array Level | max l₁ l₂ _, alreadyNormalized, lvls => getMaxArgsAux normalize l₂ alreadyNormalized (getMaxArgsAux normalize l₁ alreadyNormalized lvls) | l, false, lvls => getMaxArgsAux normalize (normalize l) true lvls | l, true, lvls => lvls.push l private def accMax (result : Level) (prev : Level) (offset : Nat) : Level := if result.isZero then prev.addOffset offset else mkLevelMax result (prev.addOffset offset) /- Auxiliary function used at `normalize`. Remarks: - `lvls` are sorted using `normLt` - `extraK` is the outter offset of the `max` term. We will push it inside. - `i` is the current array index - `prev + prevK` is the "previous" level that has not been added to `result` yet. - `result` is the accumulator -/ private partial def mkMaxAux (lvls : Array Level) (extraK : Nat) (i : Nat) (prev : Level) (prevK : Nat) (result : Level) : Level := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ let curr := lvl.getLevelOffset let currK := lvl.getOffset if curr == prev then mkMaxAux lvls extraK (i+1) curr currK result else mkMaxAux lvls extraK (i+1) curr currK (accMax result prev (extraK + prevK)) else accMax result prev (extraK + prevK) /- Auxiliary function for `normalize`. It assumes `lvls` has been sorted using `normLt`. It finds the first position that is not an explicit universe. -/ private partial def skipExplicit (lvls : Array Level) (i : Nat) : Nat := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getLevelOffset.isZero then skipExplicit lvls (i+1) else i else i /- Auxiliary function for `normalize`. `maxExplicit` is the maximum explicit universe level at `lvls`. Return true if it finds a level with offset ≥ maxExplicit. `i` starts at the first non explict level. It assumes `lvls` has been sorted using `normLt`. -/ private partial def isExplicitSubsumedAux (lvls : Array Level) (maxExplicit : Nat) (i : Nat) : Bool := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getOffset ≥ maxExplicit then true else isExplicitSubsumedAux lvls maxExplicit (i+1) else false /- Auxiliary function for `normalize`. See `isExplicitSubsumedAux` -/ private def isExplicitSubsumed (lvls : Array Level) (firstNonExplicit : Nat) : Bool := if firstNonExplicit == 0 then false else let max := (lvls.get! (firstNonExplicit - 1)).getOffset; isExplicitSubsumedAux lvls max firstNonExplicit partial def normalize (l : Level) : Level := if isAlreadyNormalizedCheap l then l else let k := l.getOffset let u := l.getLevelOffset match u with | max l₁ l₂ _ => let lvls := getMaxArgsAux normalize l₁ false #[] let lvls := getMaxArgsAux normalize l₂ false lvls let lvls := lvls.qsort normLt let firstNonExplicit := skipExplicit lvls 0 let i := if isExplicitSubsumed lvls firstNonExplicit then firstNonExplicit else firstNonExplicit - 1 let lvl₁ := lvls[i] let prev := lvl₁.getLevelOffset let prevK := lvl₁.getOffset mkMaxAux lvls k (i+1) prev prevK levelZero | imax l₁ l₂ _ => if l₂.isNeverZero then addOffset (normalize (mkLevelMax l₁ l₂)) k else let l₁ := normalize l₁ let l₂ := normalize l₂ addOffset (mkIMaxAux l₁ l₂) k | _ => unreachable! /- Return true if `u` and `v` denote the same level. Check is currently incomplete. -/ def isEquiv (u v : Level) : Bool := u == v || u.normalize == v.normalize /-- Reduce (if possible) universe level by 1 -/ def dec : Level → Option Level | zero _ => none | param _ _ => none | mvar _ _ => none | succ l _ => l | max l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Remark: `mkLevelMax` in the following line is not a typo. If `dec l₂` succeeds, then `imax l₁ l₂` is equivalent to `max l₁ l₂`. -/ | imax l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Level to Format/Syntax -/ namespace PP inductive Result where | leaf : Name → Result | num : Nat → Result | offset : Result → Nat → Result | maxNode : List Result → Result | imaxNode : List Result → Result def Result.succ : Result → Result | Result.offset f k => Result.offset f (k+1) | Result.num k => Result.num (k+1) | f => Result.offset f 1 def Result.max : Result → Result → Result | f, Result.maxNode Fs => Result.maxNode (f::Fs) | f₁, f₂ => Result.maxNode [f₁, f₂] def Result.imax : Result → Result → Result | f, Result.imaxNode Fs => Result.imaxNode (f::Fs) | f₁, f₂ => Result.imaxNode [f₁, f₂] def toResult : Level → Result | zero _ => Result.num 0 | succ l _ => Result.succ (toResult l) | max l₁ l₂ _ => Result.max (toResult l₁) (toResult l₂) | imax l₁ l₂ _ => Result.imax (toResult l₁) (toResult l₂) | param n _ => Result.leaf n | mvar n _ => let n := n.replacePrefix `_uniq (Name.mkSimple "?u"); Result.leaf n private def parenIfFalse : Format → Bool → Format | f, true => f | f, false => f.paren mutual private partial def Result.formatLst : List Result → Format | [] => Format.nil | r::rs => Format.line ++ format r false ++ formatLst rs partial def Result.format : Result → Bool → Format | Result.leaf n, _ => fmt n | Result.num k, _ => toString k | Result.offset f 0, r => format f r | Result.offset f (k+1), r => let f' := format f false; parenIfFalse (f' ++ "+" ++ fmt (k+1)) r | Result.maxNode fs, r => parenIfFalse (Format.group $ "max" ++ formatLst fs) r | Result.imaxNode fs, r => parenIfFalse (Format.group $ "imax" ++ formatLst fs) r end protected partial def Result.quote (r : Result) (prec : Nat) : Syntax := let addParen (s : Syntax) := if prec > 0 then Unhygienic.run `(level| ( $s )) else s match r with | Result.leaf n => Unhygienic.run `(level| $(mkIdent n):ident) | Result.num k => Unhygienic.run `(level| $(quote k):numLit) | Result.offset r 0 => Result.quote r prec | Result.offset r (k+1) => addParen <| Unhygienic.run `(level| $(Result.quote r 65) + $(quote (k+1)):numLit) | Result.maxNode rs => addParen <| Unhygienic.run `(level| max $(rs.toArray.map (Result.quote · maxPrec!))*) | Result.imaxNode rs => addParen <| Unhygienic.run `(level| imax $(rs.toArray.map (Result.quote · maxPrec!))*) end PP protected def format (u : Level) : Format := (PP.toResult u).format true instance : ToFormat Level where format u := Level.format u instance : ToString Level where toString u := Format.pretty (Level.format u) protected def quote (u : Level) (prec : Nat := 0) : Syntax := (PP.toResult u).quote prec instance : Quote Level where quote u := Level.quote u end Level /- Similar to `mkLevelMax`, but applies cheap simplifications -/ @[export lean_level_mk_max_simp] def mkLevelMax' (u v : Level) : Level := let subsumes (u v : Level) : Bool := if v.isExplicit && u.getOffset ≥ v.getOffset then true else match u with | Level.max u₁ u₂ _ => v == u₁ || v == u₂ | _ => false if u == v then u else if u.isZero then v else if v.isZero then u else if subsumes u v then u else if subsumes v u then v else if u.getLevelOffset == v.getLevelOffset then if u.getOffset ≥ v.getOffset then u else v else mkLevelMax u v /- Similar to `mkLevelIMax`, but applies cheap simplifications -/ @[export lean_level_mk_imax_simp] def mkLevelIMax' (u v : Level) : Level := if v.isNeverZero then mkLevelMax' u v else if v.isZero then v else if u.isZero then v else if u == v then u else mkLevelIMax u v namespace Level /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_level_update_succ"] def updateSucc (lvl : Level) (newLvl : Level) (h : lvl.isSucc = true) : Level := mkLevelSucc newLvl @[inline] def updateSucc! (lvl : Level) (newLvl : Level) : Level := match lvl with | succ lvl d => updateSucc (succ lvl d) newLvl rfl | _ => panic! "succ level expected" @[extern "lean_level_update_max"] def updateMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isMax = true) : Level := mkLevelMax' newLhs newRhs @[inline] def updateMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | max lhs rhs d => updateMax (max lhs rhs d) newLhs newRhs rfl | _ => panic! "max level expected" @[extern "lean_level_update_imax"] def updateIMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isIMax = true) : Level := mkLevelIMax' newLhs newRhs @[inline] def updateIMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | imax lhs rhs d => updateIMax (imax lhs rhs d) newLhs newRhs rfl | _ => panic! "imax level expected" def mkNaryMax : List Level → Level | [] => levelZero | [u] => u | u::us => mkLevelMax' u (mkNaryMax us) /- Level to Format -/ @[specialize] def instantiateParams (s : Name → Option Level) : Level → Level | u@(zero _) => u | u@(succ v _) => if u.hasParam then u.updateSucc! (instantiateParams s v) else u | u@(max v₁ v₂ _) => if u.hasParam then u.updateMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(imax v₁ v₂ _) => if u.hasParam then u.updateIMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(param n _) => match s n with | some u' => u' | none => u | u => u end Level open Std (HashMap HashSet PHashMap PHashSet) abbrev LevelMap (α : Type) := HashMap Level α abbrev PersistentLevelMap (α : Type) := PHashMap Level α abbrev LevelSet := HashSet Level abbrev PersistentLevelSet := PHashSet Level abbrev PLevelSet := PersistentLevelSet end Lean abbrev Nat.toLevel (n : Nat) : Lean.Level := Lean.Level.ofNat n
c8a681e33c22918606bdf3257e1e0dbc12251005
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/isDefEqConstApproxIssue.lean
4371191c05d448138d209b3f1dea133b3540d442
[ "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
184
lean
def allPairsAux (xs: List α) (ys: List β) (accum: List (α × β)) := match xs, ys with | _, [] => accum | [], _ => accum | x::xs, y::ys => allPairsAux xs ys ((x, y)::accum)
adfa788d6686ff9bd335c8ee67f6854c88af5e4f
fe84e287c662151bb313504482b218a503b972f3
/src/combinatorics/falling.lean
fde8cd7eb9a94064e610085af170de5c8e282b0a
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
2,014
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This is about the function falling n k = n (n - 1) ... (n - k + 1) Note that the above formula involves subtraction but we give a different recursive definition that only involves addition and so works more smoothly as a map ℕ → ℕ → ℕ. -/ import data.nat.choose open nat def falling : ℕ → ℕ → ℕ | 0 0 := 1 | 0 (k + 1) := 0 | (n + 1) 0 := 1 | (n + 1) (k + 1) := (n + 1) * falling n k @[simp] lemma falling_zero (n : ℕ) : falling n 0 = 1 := begin cases n; refl, end @[simp] lemma falling_succ (n k : ℕ) : falling (n + 1) (k + 1) = (n + 1) * falling n k := rfl @[simp] lemma falling_zero_succ (k : ℕ) : falling 0 (k + 1) = 0 := rfl /- falling n n = n! -/ lemma falling_factorial : ∀ n : ℕ, falling n n = factorial n | 0 := rfl | (n + 1) := by {rw[falling,factorial,falling_factorial n]} /- falling p n = p ! / (p - n)! for p ≥ n. We formalise this with p = n + m rather than with an inequality. -/ lemma falling_factorial_quot : ∀ n m : ℕ, (falling (n + m) n) * factorial m = factorial (n + m) | n 0 := by {rw[add_zero,factorial,mul_one,falling_factorial n]} | 0 (m + 1) := by {rw[zero_add,falling,one_mul]} | (n + 1) (m + 1) := calc falling ((n + 1) + (m + 1)) (n + 1) * factorial (m + 1) = ((n + 1) + (m + 1)) * (falling ((n + 1) + m) n) * factorial (m + 1) : rfl ... = ((n + 1) + (m + 1)) * ((falling (n + (m + 1)) n) * factorial (m + 1)) : by rw[mul_assoc,add_assoc n 1 m,add_comm 1 m] ... = ((n + 1) + (m + 1)) * factorial (n + (m + 1)) : by {rw[falling_factorial_quot n (m + 1)]} ... = (n + (m + 1) + 1) * factorial (n + (m + 1)) : by {rw[add_assoc n 1 (m + 1),add_comm 1 (m + 1),← add_assoc n (m + 1) 1]} ... = factorial (n + (m + 1) + 1) : rfl ... = factorial ((n + 1) + (m + 1)) : by {rw[add_assoc n (m + 1) 1,add_assoc n 1 (m + 1),add_comm (m + 1) 1],}
056bda2079f3f885b1ba5156904777a1930ef7ea
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/data/polynomial.lean
e872d962ef7fa684e3b5e438e66b3ead1d23a9f7
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
105,145
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker Theory of univariate polynomials, represented as `add_monoid_algebra α ℕ`, where α is a commutative semiring. -/ import data.monoid_algebra ring_theory.algebra import algebra.gcd_domain ring_theory.euclidean_domain ring_theory.multiplicity import tactic.ring_exp noncomputable theory local attribute [instance, priority 100] classical.prop_decidable local attribute [instance, priority 10] is_semiring_hom.comp is_ring_hom.comp /-- `polynomial α` is the type of univariate polynomials over `α`. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from α is called `C`. -/ def polynomial (α : Type*) [comm_semiring α] := add_monoid_algebra α ℕ open finsupp finset add_monoid_algebra namespace polynomial universes u v variables {α : Type u} {β : Type v} {a b : α} {m n : ℕ} section comm_semiring variables [comm_semiring α] {p q r : polynomial α} instance : inhabited (polynomial α) := finsupp.inhabited instance : comm_semiring (polynomial α) := add_monoid_algebra.comm_semiring instance : has_scalar α (polynomial α) := add_monoid_algebra.has_scalar instance : semimodule α (polynomial α) := add_monoid_algebra.semimodule /-- the coercion turning a `polynomial` into the function which reports the coefficient of a given monomial `X^n` -/ def coeff_coe_to_fun : has_coe_to_fun (polynomial α) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun @[simp] lemma support_zero : (0 : polynomial α).support = ∅ := rfl /-- `monomial s a` is the monomial `a * X^s` -/ @[reducible] def monomial (n : ℕ) (a : α) : polynomial α := finsupp.single n a /-- `C a` is the constant polynomial `a`. -/ def C (a : α) : polynomial α := monomial 0 a /-- `X` is the polynomial variable (aka indeterminant). -/ def X : polynomial α := monomial 1 1 /-- coeff p n is the coefficient of X^n in p -/ def coeff (p : polynomial α) := p.to_fun @[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial α) = f := rfl instance [has_repr α] : has_repr (polynomial α) := ⟨λ p, if p = 0 then "0" else (p.support.sort (≤)).foldr (λ n a, a ++ (if a = "" then "" else " + ") ++ if n = 0 then "C (" ++ repr (coeff p n) ++ ")" else if n = 1 then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X" else if (coeff p n) = 1 then "X ^ " ++ repr n else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩ theorem ext_iff {p q : polynomial α} : p = q ↔ ∀ n, coeff p n = coeff q n := ⟨λ h n, h ▸ rfl, finsupp.ext⟩ @[ext] lemma ext {p q : polynomial α} : (∀ n, coeff p n = coeff q n) → p = q := (@ext_iff _ _ p q).2 /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : polynomial α) : with_bot ℕ := p.support.sup some lemma degree_lt_wf : well_founded (λp q : polynomial α, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial α) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to ℕ, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial α) : ℕ := (degree p).get_or_else 0 lemma single_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n | 0 := (mul_one _).symm | (n+1) := calc monomial (n + 1) a = monomial n a * X : by rw [X, single_mul_single, mul_one] ... = (C a * X^n) * X : by rw [single_eq_C_mul_X] ... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one] lemma sum_C_mul_X_eq (p : polynomial α) : p.sum (λn a, C a * X^n) = p := eq.trans (sum_congr rfl $ assume n hn, single_eq_C_mul_X.symm) (finsupp.sum_single _) @[elab_as_eliminator] protected lemma induction_on {M : polynomial α → Prop} (p : polynomial α) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_monomial : ∀(n : ℕ) (a : α), M (C a * X^n) → M (C a * X^(n+1))) : M p := have ∀{n:ℕ} {a}, M (C a * X^n), begin assume n a, induction n with n ih, { simp only [pow_zero, mul_one, h_C] }, { exact h_monomial _ _ ih } end, finsupp.induction p (suffices M (C 0), by { convert this, exact single_zero.symm, }, h_C 0) (assume n a p _ _ hp, suffices M (C a * X^n + p), by { convert this, exact single_eq_C_mul_X }, h_add _ _ this hp) @[simp] lemma C_0 : C (0 : α) = 0 := single_zero @[simp] lemma C_1 : C (1 : α) = 1 := rfl @[simp] lemma C_mul : C (a * b) = C a * C b := (@single_mul_single _ _ _ _ 0 0 a b).symm @[simp] lemma C_add : C (a + b) = C a + C b := finsupp.single_add instance C.is_semiring_hom : is_semiring_hom (C : α → polynomial α) := ⟨C_0, C_1, λ _ _, C_add, λ _ _, C_mul⟩ @[simp] lemma C_pow : C (a ^ n) = C a ^ n := is_semiring_hom.map_pow _ _ _ lemma nat_cast_eq_C (n : ℕ) : (n : polynomial α) = C n := ((ring_hom.of C).map_nat_cast n).symm section coeff lemma apply_eq_coeff : p n = coeff p n := rfl @[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial α) n = 0 := rfl lemma coeff_single : coeff (single n a) m = if n = m then a else 0 := by { dsimp [single, finsupp.single], congr } @[simp] lemma coeff_one_zero : coeff (1 : polynomial α) 0 = 1 := coeff_single @[simp] lemma coeff_add (p q : polynomial α) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl instance coeff.is_add_monoid_hom {n : ℕ} : is_add_monoid_hom (λ p : polynomial α, p.coeff n) := { map_add := λ p q, coeff_add p q n, map_zero := coeff_zero _ } lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 := by simp [coeff, eq_comm, C, monomial, single]; congr @[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_single @[simp] lemma coeff_X_one : coeff (X : polynomial α) 1 = 1 := coeff_single @[simp] lemma coeff_X_zero : coeff (X : polynomial α) 0 = 0 := coeff_single lemma coeff_X : coeff (X : polynomial α) n = if 1 = n then 1 else 0 := coeff_single lemma coeff_C_mul_X (x : α) (k n : ℕ) : coeff (C x * X^k : polynomial α) n = if n = k then x else 0 := by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr lemma coeff_sum [comm_semiring β] (n : ℕ) (f : ℕ → α → polynomial β) : coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) := finsupp.sum_apply @[simp] lemma coeff_C_mul (p : polynomial α) : coeff (C a * p) n = a * coeff p n := begin conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] }, rw [mul_def, C, sum_single_index], { simp [coeff_single, finsupp.mul_sum, coeff_sum], apply sum_congr rfl, assume i hi, by_cases i = n; simp [h] }, { simp [finsupp.sum] } end @[simp] lemma coeff_smul (p : polynomial α) (r : α) (n : ℕ) : coeff (r • p) n = r * coeff p n := finsupp.smul_apply lemma C_mul' (a : α) (f : polynomial α) : C a * f = a • f := ext $ λ n, coeff_C_mul f @[simp, priority 990] lemma coeff_one (n : ℕ) : coeff (1 : polynomial α) n = if 0 = n then 1 else 0 := coeff_single @[simp] lemma coeff_X_pow (k n : ℕ) : coeff (X^k : polynomial α) n = if n = k then 1 else 0 := by simpa only [C_1, one_mul] using coeff_C_mul_X (1:α) k n lemma coeff_mul (p q : polynomial α) (n : ℕ) : coeff (p * q) n = (nat.antidiagonal n).sum (λ x, coeff p x.1 * coeff q x.2) := have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 ≠ 0 → a.1 + a.2 = n, from λ a ha, by_contradiction (λ h, absurd (eq.refl (0 : α)) (by rwa if_neg h at ha)), calc coeff (p * q) n = p.support.sum (λ a, q.support.sum (λ b, ite (a + b = n) (coeff p a * coeff q b) 0)) : by simp only [mul_def, coeff_sum, coeff_single]; refl ... = (p.support.product q.support).sum (λ v : ℕ × ℕ, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0) : by rw sum_product ... = (nat.antidiagonal n).sum (λ x, coeff p x.1 * coeff q x.2) : begin refine sum_bij_ne_zero (λ x _ _, x) (λ x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (λ _ _ _ _ _ _ h, h) (λ x h₁ h₂, ⟨x, _, _, rfl⟩) _, { rw [mem_product, mem_support_iff, mem_support_iff], exact ⟨ne_zero_of_mul_ne_zero_right h₂, ne_zero_of_mul_ne_zero_left h₂⟩ }, { rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] }, { intros x h hx, rw [if_pos (hite x hx)] } end theorem coeff_mul_X_pow (p : polynomial α) (n d : ℕ) : coeff (p * polynomial.X ^ n) (d + n) = coeff p d := begin rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim } end theorem coeff_mul_X (p : polynomial α) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n theorem mul_X_pow_eq_zero {p : polynomial α} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n) end coeff lemma C_inj : C a = C b ↔ a = b := ⟨λ h, coeff_C_zero.symm.trans (h.symm ▸ coeff_C_zero), congr_arg C⟩ section eval₂ variables [semiring β] variables (f : α → β) (x : β) open is_semiring_hom /-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring to the target and a value `x` for the variable in the target -/ def eval₂ (p : polynomial α) : β := p.sum (λ e a, f a * x ^ e) variables [is_semiring_hom f] @[simp] lemma eval₂_C : (C a).eval₂ f x = f a := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [pow_zero, mul_one] @[simp] lemma eval₂_X : X.eval₂ f x = x := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [map_one f, one_mul, pow_one] @[simp] lemma eval₂_zero : (0 : polynomial α).eval₂ f x = 0 := finsupp.sum_zero_index @[simp] lemma eval₂_add : (p + q).eval₂ f x = p.eval₂ f x + q.eval₂ f x := finsupp.sum_add_index (λ _, by rw [map_zero f, zero_mul]) (λ _ _ _, by rw [map_add f, add_mul]) @[simp] lemma eval₂_one : (1 : polynomial α).eval₂ f x = 1 := by rw [← C_1, eval₂_C, map_one f] instance eval₂.is_add_monoid_hom : is_add_monoid_hom (eval₂ f x) := { map_zero := eval₂_zero _ _, map_add := λ _ _, eval₂_add _ _ } end eval₂ section eval₂ variables [comm_semiring β] variables (f : α → β) [is_semiring_hom f] (x : β) open is_semiring_hom @[simp] lemma eval₂_mul : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := begin dunfold eval₂, rw [mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index], { apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index], { apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, map_mul f, pow_add], { simp only [mul_assoc, mul_left_comm] }, { rw [map_zero f, zero_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } end instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f x) := ⟨eval₂_zero _ _, eval₂_one _ _, λ _ _, eval₂_add _ _, λ _ _, eval₂_mul _ _⟩ /-- `eval₂` as a `ring_hom` -/ def eval₂_ring_hom (f : α →+* β) (x) : polynomial α →+* β := ring_hom.of (eval₂ f x) @[simp] lemma coe_eval₂_ring_hom (f : α →+* β) (x) : ⇑(eval₂_ring_hom f x) = eval₂ f x := rfl lemma eval₂_pow (n : ℕ) : (p ^ n).eval₂ f x = p.eval₂ f x ^ n := map_pow _ _ _ lemma eval₂_sum (p : polynomial α) (g : ℕ → α → polynomial α) (x : β) : (p.sum g).eval₂ f x = p.sum (λ n a, (g n a).eval₂ f x) := finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f]) (by intros; simp [right_distrib, is_add_monoid_hom.map_add f]) end eval₂ section eval variable {x : α} /-- `eval x p` is the evaluation of the polynomial `p` at `x` -/ def eval : α → polynomial α → α := eval₂ id @[simp] lemma eval_C : (C a).eval x = a := eval₂_C _ _ @[simp] lemma eval_X : X.eval x = x := eval₂_X _ _ @[simp] lemma eval_zero : (0 : polynomial α).eval x = 0 := eval₂_zero _ _ @[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := eval₂_add _ _ @[simp] lemma eval_one : (1 : polynomial α).eval x = 1 := eval₂_one _ _ @[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := eval₂_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval x) := eval₂.is_semiring_hom _ _ @[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval x = p.eval x ^ n := eval₂_pow _ _ _ lemma eval_sum (p : polynomial α) (f : ℕ → α → polynomial α) (x : α) : (p.sum f).eval x = p.sum (λ n a, (f n a).eval x) := eval₂_sum _ _ _ _ lemma eval₂_hom [comm_semiring β] (f : α → β) [is_semiring_hom f] (x : α) : p.eval₂ f (f x) = f (p.eval x) := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, eval_pow, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) /-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/ def is_root (p : polynomial α) (a : α) : Prop := p.eval a = 0 instance [decidable_eq α] : decidable (is_root p a) := by unfold is_root; apply_instance @[simp] lemma is_root.def : is_root p a ↔ p.eval a = 0 := iff.rfl lemma root_mul_left_of_is_root (p : polynomial α) {q : polynomial α} : is_root q a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero] lemma root_mul_right_of_is_root {p : polynomial α} (q : polynomial α) : is_root p a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul] lemma coeff_zero_eq_eval_zero (p : polynomial α) : coeff p 0 = p.eval 0 := calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp ... = p.eval 0 : eq.symm $ finset.sum_eq_single _ (λ b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp) lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial α} (hp : p.coeff 0 = 0) : is_root p 0 := by rwa coeff_zero_eq_eval_zero at hp end eval section comp def comp (p q : polynomial α) : polynomial α := p.eval₂ C q lemma eval₂_comp [comm_semiring β] (f : α → β) [is_semiring_hom f] {x : β} : (p.comp q).eval₂ f x = p.eval₂ f (q.eval₂ f x) := show (p.sum (λ e a, C a * q ^ e)).eval₂ f x = p.eval₂ f (eval₂ f x q), by simp only [eval₂_mul, eval₂_C, eval₂_pow, eval₂_sum]; refl lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := eval₂_comp _ @[simp] lemma comp_X : p.comp X = p := begin refine ext (λ n, _), rw [comp, eval₂], conv in (C _ * _) { rw ← single_eq_C_mul_X }, rw finsupp.sum_single end @[simp] lemma X_comp : X.comp p = p := eval₂_X _ _ @[simp] lemma comp_C : p.comp (C a) = C (p.eval a) := begin dsimp [comp, eval₂, eval, finsupp.sum], rw [← p.support.sum_hom (@C α _)], apply finset.sum_congr rfl; simp end @[simp] lemma C_comp : (C a).comp p = C a := eval₂_C _ _ @[simp] lemma comp_zero : p.comp (0 : polynomial α) = C (p.eval 0) := by rw [← C_0, comp_C] @[simp] lemma zero_comp : comp (0 : polynomial α) p = 0 := by rw [← C_0, C_comp] @[simp] lemma comp_one : p.comp 1 = C (p.eval 1) := by rw [← C_1, comp_C] @[simp] lemma one_comp : comp (1 : polynomial α) p = 1 := by rw [← C_1, C_comp] instance : is_semiring_hom (λ q : polynomial α, q.comp p) := by unfold comp; apply_instance @[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := eval₂_add _ _ @[simp] lemma mul_comp : (p * q).comp r = p.comp r * q.comp r := eval₂_mul _ _ end comp /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial α) : α := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial α) := leading_coeff p = (1 : α) lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq α] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial α} (hp : p.monic) : leading_coeff p = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial α) = ⊥ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial α) = 0 := rfl @[simp] lemma degree_C (ha : a ≠ 0) : degree (C a) = (0 : with_bot ℕ) := show sup (ite (a = 0) ∅ {0}) some = 0, by rw if_neg ha; refl lemma degree_C_le : degree (C a) ≤ (0 : with_bot ℕ) := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial α) ≤ (0 : with_bot ℕ) := by rw [← C_1]; exact degree_C_le lemma degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), λ h, h.symm ▸ rfl⟩ lemma degree_eq_nat_degree (hp : p ≠ 0) : degree p = (nat_degree p : with_bot ℕ) := let ⟨n, hn⟩ := classical.not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial α} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial α} {n : ℕ} (hn : n > 0) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial α} {n : ℕ} (h : degree p = n) : nat_degree p = n := have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot ℕ) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≤ nat_degree p := begin by_cases hp : p = 0, { rw hp, exact bot_le }, rw [degree_eq_nat_degree hp], exact le_refl _ end lemma nat_degree_eq_of_degree_eq [comm_semiring β] {q : polynomial β} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : with_bot ℕ) ≤ degree p := show @has_le.le (with_bot ℕ) _ (some n : with_bot ℕ) (p.support.sup some : with_bot ℕ), from finset.le_sup (finsupp.mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma degree_le_degree (h : coeff q (nat_degree p) ≠ 0) : degree p ≤ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end @[simp] lemma nat_degree_C (a : α) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial α) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : ℕ) : nat_degree (n : polynomial α) = 0 := by simp [nat_cast_eq_C] @[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [← single_eq_C_mul_X, degree, support_single_ne_zero ha]; refl lemma degree_monomial_le (n : ℕ) (a : α) : degree (C a * X ^ n) ≤ n := if h : a = 0 then by rw [h, C_0, zero_mul]; exact bot_le else le_of_eq (degree_monomial n h) lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial α} {n : ℕ} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end -- TODO find a home (this file) @[simp] lemma finset_sum_coeff (s : finset β) (f : β → polynomial α) (n : ℕ) : coeff (s.sum f) n = s.sum (λ b, coeff (f b) n) := (s.sum_hom (λ q : polynomial α, q.coeff n)).symm -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial α) (n : ℕ) (I : decidable (n < 1 + nat_degree p)) : @ite (n < 1 + nat_degree p) I _ (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (λ w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum (p : polynomial α) : p = (range (p.nat_degree + 1)).sum (λ i, C (p.coeff i) * X^i) := begin ext n, simp only [add_comm, coeff_X_pow, coeff_C_mul, finset.mem_range, finset.sum_mul_boole, finset_sum_coeff, ite_le_nat_degree_coeff], end lemma monic.as_sum {p : polynomial α} (hp : p.monic) : p = X^(p.nat_degree) + ((finset.range p.nat_degree).sum $ λ i, C (p.coeff i) * X^i) := begin conv_lhs { rw [p.as_sum, finset.sum_range_succ] }, suffices : C (p.coeff p.nat_degree) = 1, { rw [this, one_mul] }, exact congr_arg C hp end section map variables [comm_semiring β] variables (f : α →+* β) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : polynomial α → polynomial β := eval₂ (C ∘ f) X instance is_semiring_hom_C_f : is_semiring_hom (C ∘ f) := is_semiring_hom.comp _ _ @[simp] lemma map_C : (C a).map f = C (f a) := eval₂_C _ _ @[simp] lemma map_X : X.map f = X := eval₂_X _ _ @[simp] lemma map_zero : (0 : polynomial α).map f = 0 := eval₂_zero _ _ @[simp] lemma map_add : (p + q).map f = p.map f + q.map f := eval₂_add _ _ @[simp] lemma map_one : (1 : polynomial α).map f = 1 := eval₂_one _ _ @[simp] lemma map_mul : (p * q).map f = p.map f * q.map f := eval₂_mul _ _ instance map.is_semiring_hom : is_semiring_hom (map f) := eval₂.is_semiring_hom _ _ @[simp] lemma map_pow (n : ℕ) : (p ^ n).map f = p.map f ^ n := eval₂_pow _ _ _ lemma coeff_map (n : ℕ) : coeff (p.map f) n = f (coeff p n) := begin rw [map, eval₂, coeff_sum], conv_rhs { rw [← sum_C_mul_X_eq p, coeff_sum, finsupp.sum, ← p.support.sum_hom f], }, refine finset.sum_congr rfl (λ x hx, _), simp [function.comp, coeff_C_mul_X, is_semiring_hom.map_mul f], split_ifs; simp [is_semiring_hom.map_zero f], end lemma map_map {γ : Type*} [comm_semiring γ] (g : β →+* γ) (p : polynomial α) : (p.map f).map g = p.map (g.comp f) := ext (by simp [coeff_map]) lemma eval₂_map {γ : Type*} [comm_semiring γ] (g : β → γ) [is_semiring_hom g] (x : γ) : (p.map f).eval₂ g x = p.eval₂ (λ y, g (f y)) x := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) lemma eval_map (x : β) : (p.map f).eval x = p.eval₂ f x := eval₂_map _ _ _ @[simp] lemma map_id : p.map (ring_hom.id _) = p := by simp [polynomial.ext_iff, coeff_map] lemma mem_map_range {p : polynomial β} : p ∈ set.range (map f) ↔ ∀ n, p.coeff n ∈ (set.range f) := begin split, { rintro ⟨p, rfl⟩ n, rw coeff_map, exact set.mem_range_self _ }, { intro h, rw p.as_sum, apply is_add_submonoid.finset_sum_mem, intros i hi, rcases h i with ⟨c, hc⟩, use [C c * X^i], rw [map_mul, map_C, hc, map_pow, map_X] } end end map section variables {γ : Type*} [comm_semiring β] [comm_semiring γ] variables (f : α →+* β) (g : β →+* γ) (p) lemma hom_eval₂ (x : β) : g (p.eval₂ f x) = p.eval₂ (g ∘ f) (g x) := begin apply polynomial.induction_on p; clear p, { intros a, rw [eval₂_C, eval₂_C] }, { intros p q hp hq, simp only [hp, hq, eval₂_add, is_semiring_hom.map_add g] }, { intros n a ih, replace ih := congr_arg (λ y, y * g x) ih, simpa [pow_succ', is_semiring_hom.map_mul g, (mul_assoc _ _ _).symm, eval₂_C, eval₂_mul, eval₂_X] using ih } end end lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := begin refine ext (λ n, _), cases n, { simp }, { have : degree p < ↑(nat.succ n) := lt_of_le_of_lt h (with_bot.some_lt_some.2 (nat.succ_pos _)), rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt this] } end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h ▸ le_refl _) lemma degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, λ h, h.symm ▸ degree_C_le⟩ lemma degree_add_le (p q : polynomial α) : degree (p + q) ≤ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≤ (p.support ∪ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some ⊔ q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial α) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), λ h, h.symm ▸ leading_coeff_zero⟩ lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = ⊥ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma degree_add_eq_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := le_antisymm (max_eq_right_of_lt h ▸ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, zero_add], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [add_comm, degree_add_eq_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial α) (n : ℕ) : degree (p.erase n) ≤ degree p := by convert sup_mono (erase_subset _ _) lemma degree_erase_lt (hp : p ≠ 0) : degree (p.erase (nat_degree p)) < degree p := lt_of_le_of_ne (degree_erase_le _ _) $ (degree_eq_nat_degree hp).symm ▸ (by convert λ h, not_mem_erase _ _ (mem_of_max h)) lemma degree_sum_le (s : finset β) (f : β → polynomial α) : degree (s.sum f) ≤ s.sup (λ b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree ((insert a s).sum f) ≤ max (degree (f a)) (degree (s.sum f)) : by rw sum_insert has; exact degree_add_le _ _ ... ≤ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial α) : degree (p * q) ≤ degree p + degree q := calc degree (p * q) ≤ (p.support).sup (λi, degree (sum q (λj a, C (coeff p i * a) * X ^ (i + j)))) : by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _ ... ≤ p.support.sup (λi, q.support.sup (λj, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≤ degree p + degree q : begin refine finset.sup_le (λ a ha, finset.sup_le (λ b hb, le_trans (degree_monomial_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add' (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial α) : ∀ n, degree (p ^ n) ≤ add_monoid.smul n (degree p) | 0 := by rw [pow_zero, add_monoid.zero_smul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≤ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≤ _ : by rw succ_smul; exact add_le_add' (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : α) (n : ℕ) : leading_coeff (C a * X ^ n) = a := begin by_cases ha : a = 0, { simp only [ha, C_0, zero_mul, leading_coeff_zero] }, { rw [leading_coeff, nat_degree, degree_monomial _ ha, ← single_eq_C_mul_X], exact @finsupp.single_eq_same _ _ _ n a } end @[simp] lemma leading_coeff_C (a : α) : leading_coeff (C a) = a := suffices leading_coeff (C a * X^0) = a, by rwa [pow_zero, mul_one] at this, leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial α) = 1 := suffices leading_coeff (C (1:α) * X^1) = 1, by rwa [C_1, pow_one, one_mul] at this, leading_coeff_monomial 1 1 @[simp] lemma monic_X : monic (X : polynomial α) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial α) = 1 := suffices leading_coeff (C (1:α) * X^0) = 1, by rwa [C_1, pow_zero, mul_one] at this, leading_coeff_monomial 1 0 @[simp] lemma monic_one : monic (1 : polynomial α) := leading_coeff_C _ lemma monic.ne_zero_of_zero_ne_one (h : (0:α) ≠ 1) {p : polynomial α} (hp : p.monic) : p ≠ 0 := by { contrapose! h, rwa [h] at hp } lemma monic.ne_zero {α : Type*} [nonzero_comm_ring α] {p : polynomial α} (hp : p.monic) : p ≠ 0 := hp.ne_zero_of_zero_ne_one $ zero_ne_one lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q ≠ 0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial α) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = (nat.antidiagonal (nat_degree p + nat_degree q)).sum (λ x, coeff p x.1 * coeff q x.2) : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ h₂, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact h₂ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul_eq' (h : leading_coeff p * leading_coeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt _ h; exact λ hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q ≠ 0 := by refine mt _ h; exact λ hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma nat_degree_mul_eq' (h : leading_coeff p * leading_coeff q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, zero_mul]), have hq : q ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, mul_zero]), have hpq : p * q ≠ 0 := λ hpq, by rw [← coeff_mul_degree_add_degree, hpq, coeff_zero] at h; exact h rfl, option.some_inj.1 (show (nat_degree (p * q) : with_bot ℕ) = nat_degree p + nat_degree q, by rw [← degree_eq_nat_degree hpq, degree_mul_eq' h, degree_eq_nat_degree hp, degree_eq_nat_degree hq]) lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul_eq' h, coeff_mul_degree_add_degree], refl end lemma leading_coeff_pow' : leading_coeff p ^ n ≠ 0 → leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ λ n ih h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' h₂, ih h₁] lemma degree_pow_eq' : ∀ {n}, leading_coeff p ^ n ≠ 0 → degree (p ^ n) = add_monoid.smul n (degree p) | 0 := λ h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, add_monoid.zero_smul] | (n+1) := λ h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul_eq' h₂, succ_smul, degree_pow_eq' h₁] lemma nat_degree_pow_eq' {n : ℕ} (h : leading_coeff p ^ n ≠ 0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n ≠ 0, from λ hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot ℕ) = (n * nat_degree p : ℕ), by rw [← degree_eq_nat_degree hpn, degree_pow_eq' h, degree_eq_nat_degree hp0, ← with_bot.coe_smul]; simp @[simp] lemma leading_coeff_X_pow : ∀ n : ℕ, leading_coeff ((X : polynomial α) ^ n) = 1 | 0 := by simp | (n+1) := if h10 : (1 : α) = 0 then by rw [pow_succ, ← one_mul X, ← C_1, h10]; simp else have h : leading_coeff (X : polynomial α) * leading_coeff (X ^ n) ≠ 0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact h10, by rw [pow_succ, leading_coeff_mul' h, leading_coeff_X, leading_coeff_X_pow, one_mul] lemma nat_degree_comp_le : nat_degree (p.comp q) ≤ nat_degree p * nat_degree q := if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _ else with_bot.coe_le_coe.1 $ calc ↑(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm ... ≤ _ : degree_sum_le _ _ ... ≤ _ : sup_le (λ n hn, calc degree (C (coeff p n) * q ^ n) ≤ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _ ... ≤ nat_degree (C (coeff p n)) + add_monoid.smul n (degree q) : add_le_add' degree_le_nat_degree (degree_pow_le _ _) ... ≤ nat_degree (C (coeff p n)) + add_monoid.smul n (nat_degree q) : add_le_add_left' (add_monoid.smul_le_smul_of_le_right (@degree_le_nat_degree _ _ q) n) ... = (n * nat_degree q : ℕ) : by rw [nat_degree_C, with_bot.coe_zero, zero_add, ← with_bot.coe_smul, add_monoid.smul_eq_mul]; simp ... ≤ (nat_degree p * nat_degree q : ℕ) : with_bot.coe_le_coe.2 $ mul_le_mul_of_nonneg_right (le_nat_degree_of_ne_zero (finsupp.mem_support_iff.1 hn)) (nat.zero_le _)) lemma degree_map_le [comm_semiring β] (f : α →+* β) : degree (p.map f) ≤ degree p := if h : p.map f = 0 then by simp [h] else begin rw [degree_eq_nat_degree h], refine le_degree_of_ne_zero (mt (congr_arg f) _), rw [← coeff_map f, is_semiring_hom.map_zero f], exact mt leading_coeff_eq_zero.1 h end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial α)) : (∀ p q : polynomial α, p = q) ∧ (∀ a b : α, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], λ a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma degree_map_eq_of_leading_coeff_ne_zero [comm_semiring β] (f : α →+* β) (hf : f (leading_coeff p) ≠ 0) : degree (p.map f) = degree p := le_antisymm (degree_map_le f) $ have hp0 : p ≠ 0, from λ hp0, by simpa [hp0, is_semiring_hom.map_zero f] using hf, begin rw [degree_eq_nat_degree hp0], refine le_degree_of_ne_zero _, rw [coeff_map], exact hf end lemma monic_map [comm_semiring β] (f : α →+* β) (hp : monic p) : monic (p.map f) := if h : (0 : β) = 1 then by haveI := subsingleton_of_zero_eq_one β h; exact subsingleton.elim _ _ else have f (leading_coeff p) ≠ 0, by rwa [show _ = _, from hp, is_semiring_hom.map_one f, ne.def, eq_comm], by erw [monic, leading_coeff, nat_degree_eq_of_degree_eq (degree_map_eq_of_leading_coeff_ne_zero f this), coeff_map, ← leading_coeff, show _ = _, from hp, is_semiring_hom.map_one f] lemma zero_le_degree_iff {p : polynomial α} : 0 ≤ degree p ↔ p ≠ 0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial @[simp] lemma coeff_mul_X_zero (p : polynomial α) : coeff (p * X) 0 = 0 := by rw [coeff_mul, nat.antidiagonal_zero]; simp only [polynomial.coeff_X_zero, finset.insert_empty_eq_singleton, finset.sum_singleton, mul_zero] end comm_semiring instance subsingleton [subsingleton α] [comm_semiring α] : subsingleton (polynomial α) := ⟨λ _ _, ext (λ _, subsingleton.elim _ _)⟩ section comm_semiring variables [comm_semiring α] {p q r : polynomial α} lemma ne_zero_of_monic_of_zero_ne_one (hp : monic p) (h : (0 : α) ≠ 1) : p ≠ 0 := mt (congr_arg leading_coeff) $ by rw [monic.def.1 hp, leading_coeff_zero]; cc lemma eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (λ n, nat.cases_on n (by simp) (λ n, nat.cases_on n (by simp [coeff_C]) (λ m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm ℕ 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≤ 1, from h ▸ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) theorem degree_C_mul_X_pow_le (r : α) (n : ℕ) : degree (C r * X^n) ≤ n := begin rw [← single_eq_C_mul_X], refine finset.sup_le (λ b hb, _), rw list.eq_of_mem_singleton (finsupp.support_single_subset hb), exact le_refl _ end theorem degree_X_pow_le (n : ℕ) : degree (X^n : polynomial α) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le (1:α) n theorem degree_X_le : degree (X : polynomial α) ≤ 1 := by simpa only [C_1, one_mul, pow_one] using degree_C_mul_X_pow_le (1:α) 1 section injective open function variables [comm_semiring β] {f : α →+* β} (hf : function.injective f) include hf lemma degree_map_eq_of_injective (p : polynomial α) : degree (p.map f) = degree p := if h : p = 0 then by simp [h] else degree_map_eq_of_leading_coeff_ne_zero _ (by rw [← is_semiring_hom.map_zero f]; exact mt hf.eq_iff.1 (mt leading_coeff_eq_zero.1 h)) lemma degree_map' (p : polynomial α) : degree (p.map f) = degree p := p.degree_map_eq_of_injective hf lemma nat_degree_map' (p : polynomial α) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map' hf p) lemma map_injective (p : polynomial α) : injective (map f) := λ p q h, ext $ λ m, hf $ begin rw ext_iff at h, specialize h m, rw [coeff_map f, coeff_map f] at h, exact h end lemma leading_coeff_of_injective (p : polynomial α) : leading_coeff (p.map f) = f (leading_coeff p) := begin delta leading_coeff, rw [coeff_map f, nat_degree_map' hf p] end lemma monic_of_injective {p : polynomial α} (hp : (p.map f).monic) : p.monic := begin apply hf, rw [← leading_coeff_of_injective hf, hp.leading_coeff, is_semiring_hom.map_one f] end end injective theorem monic_of_degree_le (n : ℕ) (H1 : degree p ≤ n) (H2 : coeff p n = 1) : monic p := decidable.by_cases (assume H : degree p < n, @subsingleton.elim _ (subsingleton_of_zero_eq_one α $ H2 ▸ (coeff_eq_zero_of_degree_lt H).symm) _ _) (assume H : ¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H]) theorem monic_X_pow_add {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) + p) := have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)), monic_of_degree_le (n+1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) theorem monic_X_add_C (x : α) : monic (X + C x) := pow_one (X : polynomial α) ▸ monic_X_pow_add degree_C_le theorem degree_le_iff_coeff_zero (f : polynomial α) (n : with_bot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≤ n) m (Hm : n < (m : with_bot ℕ)), decidable.of_not_not $ λ H4, have H1 : m ∉ f.support, from λ H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot ℕ) ≤ n)) Hm, H1 $ (finsupp.mem_support_to_fun f m).2 H4, λ H, finset.sup_le $ λ b Hb, decidable.of_not_not $ λ Hn, (finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem nat_degree_le_of_degree_le {p : polynomial α} {n : ℕ} (H : degree p ≤ n) : nat_degree p ≤ n := show option.get_or_else (degree p) 0 ≤ n, from match degree p, H with | none, H := zero_le _ | (some d), H := with_bot.coe_le_coe.1 H end theorem leading_coeff_mul_X_pow {p : polynomial α} {n : ℕ} : leading_coeff (p * X ^ n) = leading_coeff p := decidable.by_cases (assume H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (assume H : leading_coeff p ≠ 0, by rw [leading_coeff_mul', leading_coeff_X_pow, mul_one]; rwa [leading_coeff_X_pow, mul_one]) lemma monic_mul (hp : monic p) (hq : monic q) : monic (p * q) := if h0 : (0 : α) = 1 then by haveI := subsingleton_of_zero_eq_one _ h0; exact subsingleton.elim _ _ else have leading_coeff p * leading_coeff q ≠ 0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0], by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul] lemma monic_pow (hp : monic p) : ∀ (n : ℕ), monic (p ^ n) | 0 := monic_one | (n+1) := monic_mul hp (monic_pow n) lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot ℕ) < degree p) (hmp : monic p) (hq : q ≠ 0) : multiplicity.finite p q := have zn0 : (0 : α) ≠ 1, from λ h, by haveI := subsingleton_of_zero_eq_one _ h; exact hq (subsingleton.elim _ _), ⟨nat_degree q, λ ⟨r, hr⟩, have hp0 : p ≠ 0, from λ hp0, by simp [hp0] at hp; contradiction, have hr0 : r ≠ 0, from λ hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) ≠ 0, from hpn1.symm ▸ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r ≠ 0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hpn0 : p ^ (nat_degree q + 1) ≠ 0, from mt leading_coeff_eq_zero.2 $ by rw [leading_coeff_pow' hpn0', show _ = _, from hmp, one_pow]; exact zn0.symm, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul_eq' hpnr0, nat_degree_pow_eq' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right' (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section nonzero_comm_semiring variables [nonzero_comm_semiring α] {p q : polynomial α} instance : nonzero_comm_semiring (polynomial α) := { zero_ne_one := λ (h : (0 : polynomial α) = 1), @zero_ne_one α _ $ calc (0 : α) = eval 0 0 : eval_zero.symm ... = eval 0 1 : congr_arg _ h ... = 1 : eval_C, ..polynomial.comm_semiring } @[simp] lemma degree_one : degree (1 : polynomial α) = (0 : with_bot ℕ) := degree_C (show (1 : α) ≠ 0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial α) = 1 := begin unfold X degree monomial single finsupp.support, rw if_neg (zero_ne_one).symm, refl end lemma X_ne_zero : (X : polynomial α) ≠ 0 := mt (congr_arg (λ p, coeff p 1)) (by simp) @[simp] lemma degree_X_pow : ∀ (n : ℕ), degree ((X : polynomial α) ^ n) = n | 0 := by simp only [pow_zero, degree_one]; refl | (n+1) := have h : leading_coeff (X : polynomial α) * leading_coeff (X ^ n) ≠ 0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact zero_ne_one.symm, by rw [pow_succ, degree_mul_eq' h, degree_X, degree_X_pow, add_comm]; refl @[simp] lemma not_monic_zero : ¬monic (0 : polynomial α) := by simpa only [monic, leading_coeff_zero] using zero_ne_one lemma ne_zero_of_monic (h : monic p) : p ≠ 0 := λ h₁, @not_monic_zero α _ (h₁ ▸ h) end nonzero_comm_semiring section comm_semiring variables [comm_semiring α] {p q : polynomial α} /-- `dix_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`. It can be used in a semiring where the usual division algorithm is not possible -/ def div_X (p : polynomial α) : polynomial α := { to_fun := λ n, p.coeff (n + 1), support := ⟨(p.support.filter (> 0)).1.map (λ n, n - 1), multiset.nodup_map_on begin simp only [finset.mem_def.symm, finset.mem_erase, finset.mem_filter], assume x hx y hy hxy, rwa [← @add_right_cancel_iff _ _ 1, nat.sub_add_cancel hx.2, nat.sub_add_cancel hy.2] at hxy end (p.support.filter (> 0)).2⟩, mem_support_to_fun := λ n, suffices (∃ (a : ℕ), (¬coeff p a = 0 ∧ a > 0) ∧ a - 1 = n) ↔ ¬coeff p (n + 1) = 0, by simpa [finset.mem_def.symm, apply_eq_coeff], ⟨λ ⟨a, ha⟩, by rw [← ha.2, nat.sub_add_cancel ha.1.2]; exact ha.1.1, λ h, ⟨n + 1, ⟨h, nat.succ_pos _⟩, nat.succ_sub_one _⟩⟩ } lemma div_X_mul_X_add (p : polynomial α) : div_X p * X + C (p.coeff 0) = p := ext $ λ n, nat.cases_on n (by simp) (by simp [coeff_C, nat.succ_ne_zero, coeff_mul_X, div_X]) @[simp] lemma div_X_C (a : α) : div_X (C a) = 0 := ext $ λ n, by cases n; simp [div_X, coeff_C]; simp [coeff] lemma div_X_eq_zero_iff : div_X p = 0 ↔ p = C (p.coeff 0) := ⟨λ h, by simpa [eq_comm, h] using div_X_mul_X_add p, λ h, by rw [h, div_X_C]⟩ lemma div_X_add : div_X (p + q) = div_X p + div_X q := ext $ by simp [div_X] def nonzero_comm_semiring.of_polynomial_ne (h : p ≠ q) : nonzero_comm_semiring α := { zero_ne_one := λ h01 : 0 = 1, h $ by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero], ..show comm_semiring α, by apply_instance } lemma degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X ≠ 0, by simpa, by erw [degree_mul_eq' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma degree_div_X_lt (hp0 : p ≠ 0) : (div_X p).degree < p.degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp0; exact calc (div_X p).degree < (div_X p * X + C (p.coeff 0)).degree : if h : degree p ≤ 0 then begin have h' : C (p.coeff 0) ≠ 0, by rwa [← eq_C_of_degree_le_zero h], rw [eq_C_of_degree_le_zero h, div_X_C, degree_zero, zero_mul, zero_add], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 $ by simp [h'])), end else have hXp0 : div_X p ≠ 0, by simpa [div_X_eq_zero_iff, -not_le, degree_le_zero_iff] using h, have leading_coeff (div_X p) * leading_coeff X ≠ 0, by simpa, have degree (C (p.coeff 0)) < degree (div_X p * X), from calc degree (C (p.coeff 0)) ≤ 0 : degree_C_le ... < 1 : dec_trivial ... = degree (X : polynomial α) : degree_X.symm ... ≤ degree (div_X p * X) : by rw [← zero_add (degree X), degree_mul_eq' this]; exact add_le_add' (by rw [zero_le_degree_iff, ne.def, div_X_eq_zero_iff]; exact λ h0, h (h0.symm ▸ degree_C_le)) (le_refl _), by rw [add_comm, degree_add_eq_of_degree_lt this]; exact degree_lt_degree_mul_X hXp0 ... = p.degree : by rw div_X_mul_X_add @[elab_as_eliminator] noncomputable def rec_on_horner {M : polynomial α → Sort*} : Π (p : polynomial α), M 0 → (Π p a, coeff p 0 = 0 → a ≠ 0 → M p → M (p + C a)) → (Π p, p ≠ 0 → M p → M (p * X)) → M p | p := λ M0 MC MX, if hp : p = 0 then eq.rec_on hp.symm M0 else have wf : degree (div_X p) < degree p, from degree_div_X_lt hp, by rw [← div_X_mul_X_add p] at *; exact if hcp0 : coeff p 0 = 0 then by rw [hcp0, C_0, add_zero]; exact MX _ (λ h : div_X p = 0, by simpa [h, hcp0] using hp) (rec_on_horner _ M0 MC MX) else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : div_X p = 0 then show M (div_X p * X), by rw [hpX0, zero_mul]; exact M0 else MX (div_X p) hpX0 (rec_on_horner _ M0 MC MX)) using_well_founded {dec_tac := tactic.assumption} @[elab_as_eliminator] lemma degree_pos_induction_on {P : polynomial α → Prop} (p : polynomial α) (h0 : 0 < degree p) (hC : ∀ {a}, a ≠ 0 → P (C a * X)) (hX : ∀ {p}, 0 < degree p → P p → P (p * X)) (hadd : ∀ {p} {a}, 0 < degree p → P p → P (p + C a)) : P p := rec_on_horner p (λ h, by rw degree_zero at h; exact absurd h dec_trivial) (λ p a _ _ ih h0, have 0 < degree p, from lt_of_not_ge (λ h, (not_lt_of_ge degree_C_le) $ by rwa [eq_C_of_degree_le_zero h, ← C_add] at h0), hadd this (ih this)) (λ p _ ih h0', if h0 : 0 < degree p then hX h0 (ih h0) else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at *; exact hC (λ h : coeff p 0 = 0, by simpa [h, nat.not_lt_zero] using h0')) h0 end comm_semiring section comm_ring variables [comm_ring α] {p q : polynomial α} instance : comm_ring (polynomial α) := add_monoid_algebra.comm_ring instance : module α (polynomial α) := add_monoid_algebra.module variable (α) def lcoeff (n : ℕ) : polynomial α →ₗ α := { to_fun := λ f, coeff f n, add := λ f g, coeff_add f g n, smul := λ r p, coeff_smul p r n } variable {α} @[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial α) : lcoeff α n f = coeff f n := rfl instance C.is_ring_hom : is_ring_hom (@C α _) := by apply is_ring_hom.of_semiring lemma int_cast_eq_C (n : ℤ) : (n : polynomial α) = C n := ((ring_hom.of C).map_int_cast n).symm @[simp] lemma C_neg : C (-a) = -C a := is_ring_hom.map_neg C @[simp] lemma C_sub : C (a - b) = C a - C b := is_ring_hom.map_sub C instance eval₂.is_ring_hom {β} [comm_ring β] (f : α → β) [is_ring_hom f] {x : β} : is_ring_hom (eval₂ f x) := by apply is_ring_hom.of_semiring instance eval.is_ring_hom {x : α} : is_ring_hom (eval x) := eval₂.is_ring_hom _ instance map.is_ring_hom {β} [comm_ring β] (f : α →+* β) : is_ring_hom (map f) := eval₂.is_ring_hom (C ∘ f) @[simp] lemma map_sub {β} [comm_ring β] (f : α →+* β) : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg {β} [comm_ring β] (f : α →+* β) : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ @[simp] lemma degree_neg (p : polynomial α) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial α) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : ℤ) : nat_degree (n : polynomial α) = 0 := by simp [int_cast_eq_C] @[simp] lemma coeff_neg (p : polynomial α) (n : ℕ) : coeff (-p) n = -coeff p n := rfl @[simp] lemma coeff_sub (p q : polynomial α) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n := rfl @[simp] lemma eval₂_neg {β} [comm_ring β] (f : α → β) [is_ring_hom f] {x : β} : (-p).eval₂ f x = -p.eval₂ f x := is_ring_hom.map_neg _ @[simp] lemma eval₂_sub {β} [comm_ring β] (f : α → β) [is_ring_hom f] {x : β} : (p - q).eval₂ f x = p.eval₂ f x - q.eval₂ f x := is_ring_hom.map_sub _ @[simp] lemma eval_neg (p : polynomial α) (x : α) : (-p).eval x = -p.eval x := is_ring_hom.map_neg _ @[simp] lemma eval_sub (p q : polynomial α) (x : α) : (p - q).eval x = p.eval x - q.eval x := is_ring_hom.map_sub _ section aeval /-- `R[X]` is the generator of the category `R-Alg`. -/ instance polynomial (R : Type u) [comm_semiring R] : algebra R (polynomial R) := { commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (polynomial.C_mul' c p).symm, .. polynomial.semimodule, .. ring_hom.of polynomial.C, } variables (R : Type u) (A : Type v) variables [comm_ring R] [comm_ring A] [algebra R A] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. -/ def aeval : polynomial R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _, ..eval₂_ring_hom (algebra_map R A) x } theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map R A) x p := rfl @[simp] lemma aeval_X : aeval R A x X = x := eval₂_X _ x @[simp] lemma aeval_C (r : R) : aeval R A x (C r) = algebra_map R A r := eval₂_C _ x theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map R A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, φ.map_mul, eval₂_mul (algebra_map R A), eval₂_X, ih] } end end aeval lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := finsupp.single_add_erase, have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := finsupp.single_add_erase, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv {to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]} ... ≤ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) ▸ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ lemma ne_zero_of_ne_zero_of_monic (hp : p ≠ 0) (hq : monic q) : q ≠ 0 | h := begin rw [h, monic.def, leading_coeff_zero] at hq, rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp, exact hp rfl end lemma div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p ≠ 0 := mt leading_coeff_eq_zero.1 h.2, have hpq : leading_coeff (C (leading_coeff p) * X ^ (nat_degree p - nat_degree q)) * leading_coeff q ≠ 0, by rwa [leading_coeff_monomial, monic.def.1 hq, mul_one], if h0 : p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q = 0 then h0.symm ▸ (lt_of_not_ge $ mt le_bot_iff.1 (mt degree_eq_bot.1 h.2)) else have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic h.2 hq, have hlt : nat_degree q ≤ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [degree_mul_eq' hpq, degree_monomial _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, nat.sub_add_cancel hlt]) h.2 (by rw [leading_coeff_mul' hpq, leading_coeff_monomial, monic.def.1 hq, mul_one]) noncomputable def div_mod_by_monic_aux : Π (p : polynomial α) {q : polynomial α}, monic q → polynomial α × polynomial α | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : polynomial α) : polynomial α := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : polynomial α) : polynomial α := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /ₘ ` : 70 := div_by_monic infixl ` %ₘ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt : ∀ (p : polynomial α) {q : polynomial α} (hq : monic q) (hq0 : q ≠ 0), degree (p %ₘ q) < degree q | p := λ q hq hq0, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %ₘ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq hq0, begin unfold mod_by_monic at this ⊢, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊢, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq0)), end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_eq_sub_mul_div : ∀ (p : polynomial α) {q : polynomial α} (hq : monic q), p %ₘ q = p - q * (p /ₘ q) | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : polynomial α) {q : polynomial α} (hq : monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) @[simp] lemma zero_mod_by_monic (p : polynomial α) : 0 %ₘ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : polynomial α) : 0 /ₘ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : polynomial α) : p %ₘ 0 = p := if h : monic (0 : polynomial α) then (subsingleton_of_monic_zero h).1 _ _ else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : polynomial α) : p /ₘ 0 = 0 := if h : monic (0 : polynomial α) then (subsingleton_of_monic_zero h).1 _ _ else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : polynomial α) (hq : ¬monic q) : p /ₘ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : polynomial α) (hq : ¬monic q) : p %ₘ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff (hq : monic q) (hq0 : q ≠ 0) : p %ₘ q = p ↔ degree p < degree q := ⟨λ h, h ▸ degree_mod_by_monic_lt _ hq hq0, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma div_by_monic_eq_zero_iff (hq : monic q) (hq0 : q ≠ 0) : p /ₘ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq hq0] at this, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := if hq0 : q = 0 then have ∀ (p : polynomial α), p = 0, from λ p, (@subsingleton_of_monic_zero α _ (hq0 ▸ hq)).1 _ _, by rw [this (p /ₘ q), this p, this q]; refl else have hdiv0 : p /ₘ q ≠ 0 := by rwa [(≠), div_by_monic_eq_zero_iff hq hq0, not_lt], have hlc : leading_coeff q * leading_coeff (p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul, (≠), leading_coeff_eq_zero], have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q : degree_mod_by_monic_lt _ hq hq0 ... ≤ _ : by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) : eq.symm (degree_mul_eq' hlc) ... = degree (p %ₘ q + q * (p /ₘ q)) : (degree_add_eq_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) lemma degree_div_by_monic_le (p q : polynomial α) : degree (p /ₘ q) ≤ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if h : degree q ≤ degree p then by rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm ▸ bot_le lemma degree_div_by_monic_lt (p : polynomial α) {q : polynomial α} (hq : monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if hpq : degree p < degree q then begin rw [(div_by_monic_eq_zero_iff hq hq0).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_some _ end else begin rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq0) ▸ h0q)) end lemma div_mod_by_monic_unique {f g} (q r : polynomial α) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := if hg0 : g = 0 then by split; exact (subsingleton_of_monic_zero (hg0 ▸ hg : monic (0 : polynomial α))).1 _ _ else have h₁ : r - f %ₘ g = -g * (q - f /ₘ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm, sub_eq_add_neg, add_comm, add_left_comm]), have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)), by simp [h₁], have h₄ : degree (r - f %ₘ g) < degree g, from calc degree (r - f %ₘ g) ≤ max (degree r) (degree (-(f %ₘ g))) : degree_add_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, by rw degree_neg; exact degree_mod_by_monic_lt _ hg hg0⟩, have h₅ : q - (f /ₘ g) = 0, from by_contradiction (λ hqf, not_le_of_gt h₄ $ calc degree g ≤ degree g + degree (q - f /ₘ g) : by erw [degree_eq_nat_degree hg0, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %ₘ g) : by rw [h₂, degree_mul_eq']; simpa [monic.def.1 hg]), ⟨eq.symm $ eq_of_sub_eq_zero h₅, eq.symm $ eq_of_sub_eq_zero $ by simpa [h₅] using h₁⟩ lemma map_mod_div_by_monic [comm_ring β] (f : α →+* β) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := if h01 : (0 : β) = 1 then by haveI := subsingleton_of_zero_eq_one β h01; exact ⟨subsingleton.elim _ _, subsingleton.elim _ _⟩ else have h01α : (0 : α) ≠ 1, from mt (congr_arg f) (by rwa [is_semiring_hom.map_one f, is_semiring_hom.map_zero f]), have map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q), from (div_mod_by_monic_unique ((p /ₘ q).map f) _ (monic_map f hq) ⟨eq.symm $ by rw [← map_mul, ← map_add, mod_by_monic_add_div _ hq], calc _ ≤ degree (p %ₘ q) : degree_map_le _ ... < degree q : degree_mod_by_monic_lt _ hq $ (ne_zero_of_monic_of_zero_ne_one hq h01α) ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, is_semiring_hom.map_one f]; exact ne.symm h01)⟩), ⟨this.1.symm, this.2.symm⟩ lemma map_div_by_monic [comm_ring β] (f : α →+* β) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring β] (f : α →+* β) (hq : monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, λ h, if hq0 : q = 0 then by rw hq0 at hq; exact (subsingleton_of_monic_zero hq).1 _ _ else let ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h in by_contradiction (λ hpq0, have hmod : p %ₘ q = q * (r - p /ₘ q) := by rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr], have degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_mod_by_monic_lt _ hq hq0, have hrpq0 : leading_coeff (r - p /ₘ q) ≠ 0 := λ h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul], by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this; exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this))⟩ @[simp] lemma mod_by_monic_one (p : polynomial α) : p %ₘ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero monic_one).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : polynomial α) : p /ₘ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp lemma degree_pos_of_root (hp : p ≠ 0) (h : is_root p a) : 0 < degree p := lt_of_not_ge $ λ hlt, begin have := eq_C_of_degree_le_zero hlt, rw [is_root, this, eval_C] at h, exact hp (finsupp.ext (λ n, show coeff p n = 0, from nat.cases_on n h (λ _, coeff_eq_zero_of_degree_lt (lt_of_le_of_lt hlt (with_bot.coe_lt_coe.2 (nat.succ_pos _)))))), end theorem monic_X_sub_C (x : α) : monic (X - C x) := by simpa only [C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) - p) := monic_X_pow_add ((degree_neg p).symm ▸ H) theorem degree_mod_by_monic_le (p : polynomial α) {q : polynomial α} (hq : monic q) : degree (p %ₘ q) ≤ degree q := decidable.by_cases (assume H : q = 0, by rw [monic, H, leading_coeff_zero] at hq; have : (0:polynomial α) = 1 := (by rw [← C_0, ← C_1, hq]); rw [eq_zero_of_zero_eq_one _ this (p %ₘ q), eq_zero_of_zero_eq_one _ this q]; exact le_refl _) (assume H : q ≠ 0, le_of_lt $ degree_mod_by_monic_lt _ hq H) lemma root_X_sub_C : is_root (X - C a) b ↔ a = b := by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm] def nonzero_comm_ring.of_polynomial_ne (h : p ≠ q) : nonzero_comm_ring α := { zero := 0, one := 1, zero_ne_one := λ h01, h $ by rw [← one_mul p, ← one_mul q, ← C_1, ← h01, C_0, zero_mul, zero_mul], ..show comm_ring α, by apply_instance } end comm_ring section nonzero_comm_ring variables [nonzero_comm_ring α] {p q : polynomial α} instance : nonzero_comm_ring (polynomial α) := { ..polynomial.nonzero_comm_semiring, ..polynomial.comm_ring } @[simp] lemma degree_X_sub_C (a : α) : degree (X - C a) = 1 := begin rw [sub_eq_add_neg, add_comm, ← @degree_X α], by_cases ha : a = 0, { simp only [ha, C_0, neg_zero, zero_add] }, exact degree_add_eq_of_degree_lt (by rw [degree_X, degree_neg, degree_C ha]; exact dec_trivial) end lemma degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : α) : degree ((X : polynomial α) ^ n - C a) = n := have degree (-C a) < degree ((X : polynomial α) ^ n), from calc degree (-C a) ≤ 0 : by rw degree_neg; exact degree_C_le ... < degree ((X : polynomial α) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [sub_eq_add_neg, add_comm, degree_add_eq_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : α) : (X : polynomial α) ^ n - C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : polynomial α) ^ n - C a) ≠ ⊥, by rw degree_X_pow_sub_C hn; exact dec_trivial) end nonzero_comm_ring section comm_ring variables [comm_ring α] {p q : polynomial α} @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : polynomial α) (a : α) : p %ₘ (X - C a) = C (p.eval a) := if h0 : (0 : α) = 1 then by letI := subsingleton_of_zero_eq_one α h0; exact subsingleton.elim _ _ else by letI : nonzero_comm_ring α := nonzero_comm_ring.of_ne h0; exact have h : (p %ₘ (X - C a)).eval a = p.eval a := by rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero], have degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_mod_by_monic_lt p (monic_X_sub_C a) ((degree_X_sub_C a).symm ▸ ne_zero_of_monic (monic_X_sub_C _)), have degree (p %ₘ (X - C a)) ≤ 0 := begin cases (degree (p %ₘ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } end, begin rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /ₘ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], λ h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, λ h, ⟨(p /ₘ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : polynomial α) : p %ₘ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] section multiplicity def decidable_dvd_monic (p : polynomial α) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : α) (h0 : p ≠ 0) : multiplicity.finite (X - C a) p := multiplicity_finite_of_degree_pos_of_monic (have (0 : α) ≠ 1, from (λ h, by haveI := subsingleton_of_zero_eq_one _ h; exact h0 (subsingleton.elim _ _)), by letI : nonzero_comm_ring α := { zero_ne_one := this, ..show comm_ring α, by apply_instance }; rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) h0 def root_multiplicity (a : α) (p : polynomial α) : ℕ := if h0 : p = 0 then 0 else let I : decidable_pred (λ n : ℕ, ¬(X - C a) ^ (n + 1) ∣ p) := λ n, @not.decidable _ (decidable_dvd_monic p (monic_pow (monic_X_sub_C a) (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : polynomial α) (a : α) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, roption.dom]; congr; funext; congr lemma pow_root_multiplicity_dvd (p : polynomial α) (a : α) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : polynomial α) (a : α) : p /ₘ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from monic_pow (monic_X_sub_C _) _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : polynomial α} (a : α) (hp : p ≠ 0) : (p /ₘ ((X - C a) ^ root_multiplicity a p)).eval a ≠ 0 := begin letI : nonzero_comm_ring α := nonzero_comm_ring.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot ℕ) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) _ hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring section integral_domain variables [integral_domain α] {p q : polynomial α} @[simp] lemma degree_mul_eq : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul_eq' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow_eq (p : polynomial α) (n : ℕ) : degree (p ^ n) = add_monoid.smul n (degree p) := by induction n; [simp only [pow_zero, degree_one, add_monoid.zero_smul], simp only [*, pow_succ, succ_smul, degree_mul_eq]] @[simp] lemma leading_coeff_mul (p q : polynomial α) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_pow (p : polynomial α) (n : ℕ) : leading_coeff (p ^ n) = leading_coeff p ^ n := by induction n; [simp only [pow_zero, leading_coeff_one], simp only [*, pow_succ, leading_coeff_mul]] instance : integral_domain (polynomial α) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin have : leading_coeff 0 = leading_coeff a * leading_coeff b := h ▸ leading_coeff_mul a b, rw [leading_coeff_zero, eq_comm] at this, erw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], exact eq_zero_or_eq_zero_of_mul_eq_zero this end, ..polynomial.nonzero_comm_ring } lemma nat_degree_mul_eq (hp : p ≠ 0) (hq : q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul_eq] @[simp] lemma nat_degree_pow_eq (p : polynomial α) (n : ℕ) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow_eq' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := by rw [is_root, eval_mul] at h; exact eq_zero_or_eq_zero_of_mul_eq_zero h lemma degree_le_mul_left (p : polynomial α) (hq : q ≠ 0) : degree p ≤ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul_eq, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) lemma exists_finset_roots : ∀ {p : polynomial α} (hp : p ≠ 0), ∃ s : finset α, (s.card : with_bot ℕ) ≤ degree p ∧ ∀ x, x ∈ s ↔ is_root p x | p := λ hp, by haveI := classical.prop_decidable (∃ x, is_root p x); exact if h : ∃ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /ₘ (X - C x) ≠ 0 := λ h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /ₘ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm ▸ dec_trivial), let ⟨t, htd, htr⟩ := @exists_finset_roots (p /ₘ (X - C x)) hd0 in have hdeg : degree (X - C x) ≤ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /ₘ (X - C x) ≠ 0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨insert x t, calc (card (insert x t) : with_bot ℕ) ≤ card t + 1 : with_bot.coe_le_coe.2 $ finset.card_insert_le _ _ ... ≤ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add' (le_refl (1 : with_bot ℕ)) htd, begin assume y, rw [mem_insert, htr, eq_comm, ← root_X_sub_C], conv {to_rhs, rw ← mul_div_by_monic_eq_iff_is_root.2 hx}, exact ⟨λ h, or.cases_on h (root_mul_right_of_is_root _) (root_mul_left_of_is_root _), root_or_root_of_root_mul⟩ end⟩ else ⟨∅, (degree_eq_nat_degree hp).symm ▸ with_bot.coe_le_coe.2 (nat.zero_le _), by simpa only [not_mem_empty, false_iff, not_exists] using h⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a finset containing all the roots of `p` -/ noncomputable def roots (p : polynomial α) : finset α := if h : p = 0 then ∅ else classical.some (exists_finset_roots h) lemma card_roots (hp0 : p ≠ 0) : ((roots p).card : with_bot ℕ) ≤ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_finset_roots hp0)).1 end lemma card_roots' {p : polynomial α} (hp0 : p ≠ 0) : p.roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial α} {a : α} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot ℕ) ≤ degree p := calc ((p - C a).roots.card : with_bot ℕ) ≤ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial α} {a : α} (hp0 : 0 < degree p) : (p - C a).roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (λ h, by simp [*, lt_irrefl] at *))) @[simp] lemma mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ is_root p a := by unfold roots; rw dif_neg hp; exact (classical.some_spec (exists_finset_roots hp)).2 _ @[simp] lemma mem_roots_sub_C {p : polynomial α} {a x : α} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a ≠ 0, from mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) lemma card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : α) : (roots ((X : polynomial α) ^ n - C a)).card ≤ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial α) ^ n - C a)).card : with_bot ℕ) ≤ degree ((X : polynomial α) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots {α : Type*} [integral_domain α] (n : ℕ) (a : α) : finset α := roots ((X : polynomial α) ^ n - C a) @[simp] lemma mem_nth_roots {α : Type*} [integral_domain α] {n : ℕ} (hn : 0 < n) {a x : α} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq] lemma card_nth_roots {α : Type*} [integral_domain α] (n : ℕ) (a : α) : (nth_roots n a).card ≤ n := if hn : n = 0 then if h : (X : polynomial α) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, card_empty] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C α _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q ≠ 0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (λ n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, eval₂, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q ≠ 0, from λ hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b ≠ 0, rwa [← apply_eq_coeff, ← finsupp.mem_support_iff], dsimp [apply_eq_coeff], refine coeff_eq_zero_of_degree_lt _, rw [degree_mul_eq, degree_C this, degree_pow_eq, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_smul, add_monoid.smul_eq_mul, with_bot.coe_lt_coe, nat.cast_id], exact (mul_lt_mul_right (nat.pos_of_ne_zero hqd0)).2 (lt_of_le_of_ne (with_bot.coe_le_coe.1 (by rw ← degree_eq_nat_degree hp0; exact le_sup hbs)) hbp) end (by rw [finsupp.mem_support_iff, apply_eq_coeff, ← leading_coeff, ne.def, leading_coeff_eq_zero, classical.not_not]; simp {contextual := tt}) ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow_eq], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≤ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q ≠ 0, from λ hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... ≠ 0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q ≠ 0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p ≠ 0, from λ hp0, by simpa [hp0] using hq, have hq0 : q ≠ 0, from λ hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial α) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul_eq hp0 hq0, eq_comm, _root_.add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial α)) : degree (u : polynomial α) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ @[simp] lemma nat_degree_coe_units (u : units (polynomial α)) : nat_degree (u : polynomial α) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) lemma coeff_coe_units_zero_ne_zero (u : units (polynomial α)) : coeff (u : polynomial α) 0 ≠ 0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.coe_ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : α} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.2 _ _ hg, this.elim (λ h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have h₂ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at h₂; exact absurd h₂ dec_trivial) (λ hgu, by rw [hg, degree_mul_eq, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) end integral_domain section field variables [field α] {p q : polynomial α} instance : vector_space α (polynomial α) := finsupp.vector_space _ _ lemma is_unit_iff_degree_eq_zero : is_unit p ↔ degree p = 0 := ⟨degree_eq_zero_of_is_unit, λ h, have degree p ≤ 0, by simp [*, le_refl], have hc : coeff p 0 ≠ 0, from λ hc, by rw [eq_C_of_degree_le_zero this, hc] at h; simpa using h, is_unit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, begin conv in p { rw eq_C_of_degree_le_zero this }, rw [← C_mul, _root_.mul_inv_cancel hc, C_1] end⟩⟩ lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p ≠ 0) (hp : ¬is_unit p) : 0 < degree p := lt_of_not_ge (λ h, by rw [eq_C_of_degree_le_zero h] at hp0 hp; exact (hp $ is_unit.map' C $ is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))) lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p := ⟨mt is_unit_iff_dvd_one.1 (λ ⟨q, hq⟩, absurd (congr_arg degree hq) (λ h, have degree q = 0, by rw [degree_one, degree_mul_eq, hp1, eq_comm, nat.with_bot.add_eq_zero_iff] at h; exact h.2, by simp [degree_mul_eq, this, degree_one, hp1] at h; exact absurd h dec_trivial)), λ q r hpqr, begin have := congr_arg degree hpqr, rw [hp1, degree_mul_eq, eq_comm, nat.with_bot.add_eq_one_iff] at this, rw [is_unit_iff_degree_eq_zero, is_unit_iff_degree_eq_zero]; tautology end⟩ lemma monic_mul_leading_coeff_inv (h : p ≠ 0) : monic (p * C (leading_coeff p)⁻¹) := by rw [monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel (show leading_coeff p ≠ 0, from mt leading_coeff_eq_zero.1 h)] lemma degree_mul_leading_coeff_inv (p : polynomial α) (h : q ≠ 0) : degree (p * C (leading_coeff q)⁻¹) = degree p := have h₁ : (leading_coeff q)⁻¹ ≠ 0 := inv_ne_zero (mt leading_coeff_eq_zero.1 h), by rw [degree_mul_eq, degree_C h₁, add_zero] def div (p q : polynomial α) := C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) def mod (p q : polynomial α) := p %ₘ (q * C (leading_coeff q)⁻¹) private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial α) : q * div p q + mod p q = p := if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add] else begin conv {to_rhs, rw ← mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)}, rw [div, mod, add_comm, mul_assoc] end private lemma remainder_lt_aux (p : polynomial α) (hq : q ≠ 0) : degree (mod p q) < degree q := by rw ← degree_mul_leading_coeff_inv q hq; exact degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq) (mul_ne_zero hq (mt leading_coeff_eq_zero.2 (by rw leading_coeff_C; exact inv_ne_zero (mt leading_coeff_eq_zero.1 hq)))) instance : has_div (polynomial α) := ⟨div⟩ instance : has_mod (polynomial α) := ⟨mod⟩ lemma div_def : p / q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) := rfl lemma mod_def : p % q = p %ₘ (q * C (leading_coeff q)⁻¹) := rfl lemma mod_by_monic_eq_mod (p : polynomial α) (hq : monic q) : p %ₘ q = p % q := show p %ₘ q = p %ₘ (q * C (leading_coeff q)⁻¹), by simp only [monic.def.1 hq, inv_one, mul_one, C_1] lemma div_by_monic_eq_div (p : polynomial α) (hq : monic q) : p /ₘ q = p / q := show p /ₘ q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)), by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one] lemma mod_X_sub_C_eq_C_eval (p : polynomial α) (a : α) : p % (X - C a) = C (p.eval a) := mod_by_monic_eq_mod p (monic_X_sub_C a) ▸ mod_by_monic_X_sub_C_eq_C_eval _ _ lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p ↔ is_root p a := div_by_monic_eq_div p (monic_X_sub_C a) ▸ mul_div_by_monic_eq_iff_is_root instance : euclidean_domain (polynomial α) := { quotient := (/), quotient_zero := by simp [div_def], remainder := (%), r := _, r_well_founded := degree_lt_wf, quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux, remainder_lt := λ p q hq, remainder_lt_aux _ hq, mul_left_not_lt := λ p q hq, not_lt_of_ge (degree_le_mul_left _ hq) } lemma mod_eq_self_iff (hq0 : q ≠ 0) : p % q = p ↔ degree p < degree q := ⟨λ h, h ▸ euclidean_domain.mod_lt _ hq0, λ h, have ¬degree (q * C (leading_coeff q)⁻¹) ≤ degree p := not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0, begin rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)], unfold div_mod_by_monic_aux, simp only [this, false_and, if_false] end⟩ lemma div_eq_zero_iff (hq0 : q ≠ 0) : p / q = 0 ↔ degree p < degree q := ⟨λ h, by have := euclidean_domain.div_add_mod p q; rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this, λ h, have hlt : degree p < degree (q * C (leading_coeff q)⁻¹), by rwa degree_mul_leading_coeff_inv q hq0, have hm : monic (q * C (leading_coeff q)⁻¹) := monic_mul_leading_coeff_inv hq0, by rw [div_def, (div_by_monic_eq_zero_iff hm (ne_zero_of_monic hm)).2 hlt, mul_zero]⟩ lemma degree_add_div (hq0 : q ≠ 0) (hpq : degree q ≤ degree p) : degree q + degree (p / q) = degree p := have degree (p % q) < degree (q * (p / q)) := calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0 ... ≤ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)), by conv {to_rhs, rw [← euclidean_domain.div_add_mod p q, add_comm, degree_add_eq_of_degree_lt this, degree_mul_eq]} lemma degree_div_le (p q : polynomial α) : degree (p / q) ≤ degree p := if hq : q = 0 then by simp [hq] else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq]; exact degree_div_by_monic_le _ _ lemma degree_div_lt (hp : p ≠ 0) (hq : 0 < degree q) : degree (p / q) < degree p := have hq0 : q ≠ 0, from λ hq0, by simpa [hq0] using hq, by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0]; exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp (by rw degree_mul_leading_coeff_inv _ hq0; exact hq) @[simp] lemma degree_map [field β] (p : polynomial α) (f : α →+* β) : degree (p.map f) = degree p := p.degree_map_eq_of_injective (is_ring_hom.injective f) @[simp] lemma nat_degree_map [field β] (f : α →+* β) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map _ f) @[simp] lemma leading_coeff_map [field β] (f : α →+* β) : leading_coeff (p.map f) = f (leading_coeff p) := by simp [leading_coeff, coeff_map f] lemma map_div [field β] (f : α →+* β) : (p / q).map f = p.map f / q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)]; simp [is_ring_hom.map_inv f, leading_coeff, coeff_map f] lemma map_mod [field β] (f : α →+* β) : (p % q).map f = p.map f % q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [mod_def, mod_def, leading_coeff_map f, ← is_ring_hom.map_inv f, ← map_C f, ← map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)] @[simp] lemma map_eq_zero [field β] (f : α →+* β) : p.map f = 0 ↔ p = 0 := by simp [polynomial.ext_iff, is_ring_hom.map_eq_zero f, coeff_map] lemma exists_root_of_degree_eq_one (h : degree p = 1) : ∃ x, is_root p x := ⟨-(p.coeff 0 / p.coeff 1), have p.coeff 1 ≠ 0, by rw ← nat_degree_eq_of_degree_eq_some h; exact mt leading_coeff_eq_zero.1 (λ h0, by simpa [h0] using h), by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p ≤ 1, by rw h; exact le_refl _)] }; simp [is_root, mul_div_cancel' _ this]⟩ lemma coeff_inv_units (u : units (polynomial α)) (n : ℕ) : ((↑u : polynomial α).coeff n)⁻¹ = ((↑u⁻¹ : polynomial α).coeff n) := begin rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹), coeff_C, coeff_C, inv_eq_one_div], split_ifs, { rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero, coeff_zero_eq_eval_zero, ← eval_mul, ← units.coe_mul, inv_mul_self]; simp }, { simp } end instance : normalization_domain (polynomial α) := { norm_unit := λ p, if hp0 : p = 0 then 1 else ⟨C p.leading_coeff⁻¹, C p.leading_coeff, by rw [← C_mul, inv_mul_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0, by rw [← C_mul, mul_inv_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0,⟩, norm_unit_zero := dif_pos rfl, norm_unit_mul := λ p q hp0 hq0, begin rw [dif_neg hp0, dif_neg hq0, dif_neg (mul_ne_zero hp0 hq0)], apply units.ext, show C (leading_coeff (p * q))⁻¹ = C (leading_coeff p)⁻¹ * C (leading_coeff q)⁻¹, rw [leading_coeff_mul, mul_inv', C_mul, mul_comm] end, norm_unit_coe_units := λ u, have hu : degree ↑u⁻¹ = 0, from degree_eq_zero_of_is_unit ⟨u⁻¹, rfl⟩, begin apply units.ext, rw [dif_neg (units.coe_ne_zero u)], conv_rhs {rw eq_C_of_degree_eq_zero hu}, refine C_inj.2 _, rw [← nat_degree_eq_of_degree_eq_some hu, leading_coeff, coeff_inv_units], simp end, ..polynomial.integral_domain } lemma monic_normalize (hp0 : p ≠ 0) : monic (normalize p) := show leading_coeff (p * ↑(dite _ _ _)) = 1, by rw dif_neg hp0; exact monic_mul_leading_coeff_inv hp0 lemma coe_norm_unit (hp : p ≠ 0) : (norm_unit p : polynomial α) = C p.leading_coeff⁻¹ := show ↑(dite _ _ _) = C p.leading_coeff⁻¹, by rw dif_neg hp; refl end field section derivative variables [comm_semiring α] /-- `derivative p` formal derivative of the polynomial `p` -/ def derivative (p : polynomial α) : polynomial α := p.sum (λn a, C (a * n) * X^(n - 1)) lemma coeff_derivative (p : polynomial α) (n : ℕ) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := begin rw [derivative], simp only [coeff_X_pow, coeff_sum, coeff_C_mul], rw [finsupp.sum, finset.sum_eq_single (n + 1), apply_eq_coeff], { rw [if_pos (nat.add_sub_cancel _ _).symm, mul_one, nat.cast_add, nat.cast_one] }, { assume b, cases b, { intros, rw [nat.cast_zero, mul_zero, zero_mul] }, { intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } }, { intro H, rw [not_mem_support_iff.1 H, zero_mul, zero_mul] } end @[simp] lemma derivative_zero : derivative (0 : polynomial α) = 0 := finsupp.sum_zero_index lemma derivative_monomial (a : α) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) := by rw [← single_eq_C_mul_X, ← single_eq_C_mul_X, derivative, sum_single_index, single_eq_C_mul_X]; simp only [zero_mul, C_0]; refl @[simp] lemma derivative_C {a : α} : derivative (C a) = 0 := suffices derivative (C a * X^0) = C (a * 0:α) * X ^ 0, by simpa only [mul_one, zero_mul, C_0, mul_zero, pow_zero], derivative_monomial a 0 @[simp] lemma derivative_X : derivative (X : polynomial α) = 1 := suffices derivative (C (1:α) * X^1) = C (1 * (1:ℕ)) * X ^ 0, by simpa only [mul_one, one_mul, C_1, pow_one, nat.cast_one, pow_zero], derivative_monomial 1 1 @[simp] lemma derivative_one : derivative (1 : polynomial α) = 0 := derivative_C @[simp] lemma derivative_add {f g : polynomial α} : derivative (f + g) = derivative f + derivative g := by refine finsupp.sum_add_index _ _; intros; simp only [add_mul, zero_mul, C_0, C_add, C_mul] instance : is_add_monoid_hom (derivative : polynomial α → polynomial α) := { map_add := λ _ _, derivative_add, map_zero := derivative_zero } @[simp] lemma derivative_sum {s : finset β} {f : β → polynomial α} : derivative (s.sum f) = s.sum (λb, derivative (f b)) := (s.sum_hom derivative).symm @[simp] lemma derivative_mul {f g : polynomial α} : derivative (f * g) = derivative f * g + f * derivative g := calc derivative (f * g) = f.sum (λn a, g.sum (λm b, C ((a * b) * (n + m : ℕ)) * X^((n + m) - 1))) : begin transitivity, exact derivative_sum, transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum }, apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm, transitivity, { apply congr_arg, exact single_eq_C_mul_X }, exact derivative_monomial _ _ end ... = f.sum (λn a, g.sum (λm b, (C (a * n) * X^(n - 1)) * (C b * X^m) + (C a * X^n) * (C (b * m) * X^(m - 1)))) : sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm, by simp only [nat.cast_add, mul_add, add_mul, C_add, C_mul]; cases n; simp only [nat.succ_sub_succ, pow_zero]; cases m; simp only [nat.cast_zero, C_0, nat.succ_sub_succ, zero_mul, mul_zero, nat.sub_zero, pow_zero, pow_add, one_mul, pow_succ, mul_comm, mul_left_comm] ... = derivative f * g + f * derivative g : begin conv { to_rhs, congr, { rw [← sum_C_mul_X_eq g] }, { rw [← sum_C_mul_X_eq f] } }, unfold derivative finsupp.sum, simp only [sum_add_distrib, finset.mul_sum, finset.sum_mul] end lemma derivative_eval (p : polynomial α) (x : α) : p.derivative.eval x = p.sum (λ n a, (a * n)*x^(n-1)) := by simp [derivative, eval_sum, eval_pow] end derivative section domain variables [integral_domain α] lemma mem_support_derivative [char_zero α] (p : polynomial α) (n : ℕ) : n ∈ (derivative p).support ↔ n + 1 ∈ p.support := suffices (¬(coeff p (n + 1) = 0 ∨ ((n + 1:ℕ) : α) = 0)) ↔ coeff p (n + 1) ≠ 0, by simpa only [coeff_derivative, apply_eq_coeff, mem_support_iff, ne.def, mul_eq_zero], by rw [nat.cast_eq_zero]; simp only [nat.succ_ne_zero, or_false] @[simp] lemma degree_derivative_eq [char_zero α] (p : polynomial α) (hp : 0 < nat_degree p) : degree (derivative p) = (nat_degree p - 1 : ℕ) := le_antisymm (le_trans (degree_sum_le _ _) $ sup_le $ assume n hn, have n ≤ nat_degree p, begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], { refine le_degree_of_ne_zero _, simpa only [mem_support_iff] using hn }, { assume h, simpa only [h, support_zero] using hn } end, le_trans (degree_monomial_le _ _) $ with_bot.coe_le_coe.2 $ nat.sub_le_sub_right this _) begin refine le_sup _, rw [mem_support_derivative, nat.sub_add_cancel, mem_support_iff], { show ¬ leading_coeff p = 0, rw [leading_coeff_eq_zero], assume h, rw [h, nat_degree_zero] at hp, exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), }, exact hp end end domain section identities /- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {α : Type*} [comm_semiring α] (x y : α) : ∀ (n : ℕ), {k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2} | 0 := ⟨0, by simp⟩ | 1 := ⟨0, by simp⟩ | (n+2) := begin cases pow_add_expansion (n+1) with z hz, existsi x*z + (n+1)*x^n+z*y, calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp ... = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz ... = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 : by { push_cast, ring_exp! } end variables [comm_ring α] private def poly_binom_aux1 (x y : α) (e : ℕ) (a : α) : {k : α // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} := begin existsi (pow_add_expansion x y e).val, congr, apply (pow_add_expansion _ _ _).property end private lemma poly_binom_aux2 (f : polynomial α) (x y : α) : f.eval (x + y) = f.sum (λ e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) := begin unfold eval eval₂, congr, ext, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial α) (x y : α) : f.eval (x + y) = f.sum (λ e a, a * x^e) + f.sum (λ e a, (a * e * x^(e-1)) * y) + f.sum (λ e a, (a *(poly_binom_aux1 x y e a).val)*y^2) := by rw poly_binom_aux2; simp [left_distrib, finsupp.sum_add, mul_assoc] def binom_expansion (f : polynomial α) (x y : α) : {k : α // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} := begin existsi f.sum (λ e a, a *((poly_binom_aux1 x y e a).val)), rw poly_binom_aux3, congr, { rw derivative_eval, symmetry, apply finsupp.sum_mul }, { symmetry, apply finsupp.sum_mul } end def pow_sub_pow_factor (x y : α) : Π {i : ℕ},{z : α // x^i - y^i = z*(x - y)} | 0 := ⟨0, by simp⟩ | 1 := ⟨1, by simp⟩ | (k+2) := begin cases @pow_sub_pow_factor (k+1) with z hz, existsi z*x + y^(k+1), calc x ^ (k + 2) - y ^ (k + 2) = x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp ... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz ... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp end def eval_sub_factor (f : polynomial α) (x y : α) : {z : α // f.eval x - f.eval y = z*(x - y)} := begin existsi f.sum (λ a b, b * (pow_sub_pow_factor x y).val), unfold eval eval₂, rw [←finsupp.sum_sub], have : finsupp.sum f (λ (a : ℕ) (b : α), b * (pow_sub_pow_factor x y).val) * (x - y) = finsupp.sum f (λ (a : ℕ) (b : α), b * (pow_sub_pow_factor x y).val * (x - y)), { apply finsupp.sum_mul }, rw this, congr, ext e a, rw [mul_assoc, ←(pow_sub_pow_factor x y).property], simp [mul_sub] end end identities end polynomial namespace is_integral_domain variables {α : Type*} [comm_ring α] /-- Lift evidence that `is_integral_domain α` to `is_integral_domain (polynomial α)`. -/ lemma polynomial (h : is_integral_domain α) : is_integral_domain (polynomial α) := @integral_domain.to_is_integral_domain _ (@polynomial.integral_domain _ (h.to_integral_domain _)) end is_integral_domain
eba534ca6a4c08028790807c58c7fe45359342d0
82e44445c70db0f03e30d7be725775f122d72f3e
/src/measure_theory/interval_integral.lean
cf6812d40af255479a1530400e269a98a8d8a8f5
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
105,696
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Patrick Massot -/ import measure_theory.set_integral import measure_theory.lebesgue_measure import analysis.calculus.fderiv_measurable import analysis.calculus.extend_deriv /-! # Integral over an interval In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and `-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and many versions of the first part of the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus). Recall that it states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`. ## Main statements ### FTC-1 for Lebesgue measure We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`. They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`. Let us explain the meaning of each part of the name: * `_strict` means that the theorem is about strict differentiability; * `f` means that the theorem is about differentiability in both endpoints; incompatible with `_right|_left`; * `_within` means that the theorem is about one-sided derivatives, see below for details; * `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit almost surely as `x` tends to `a` and/or `b`; * `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left) endpoint. We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named `(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the name. ### One-sided derivatives Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and corresponding filters `la`, `lb` are given in the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and `integral_has_fderiv_at_of_tendsto_ae`). Similarly, `integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the same typeclass to find an appropriate filter. ### FTC for a locally finite measure Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for any measure. The most general of them, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')` be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. ### FTC-2 and corollaries We use FTC-1 to prove several versions of FTC-2 for the Lebesgue measure, using a similar naming scheme as for the versions of FTC-1. They include: * `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` - most general version, for functions with a right derivative * `interval_integral.integral_eq_sub_of_has_deriv_at'` - version for functions with a derivative on an open set * `interval_integral.integral_deriv_eq_sub'` - version that is easiest to use when computing the integral of a specific function We then derive additional integration techniques from FTC-2: * `interval_integral.integral_mul_deriv_eq_deriv_mul` - integration by parts * `interval_integral.integral_comp_mul_deriv'` - integration by substitution Many applications of these theorems can be found in the file `analysis.special_functions.integrals`. ## Implementation notes ### Avoiding `if`, `min`, and `max` In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as `integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these intervals is empty and the other coincides with `Ioc (min a b) (max a b)`. Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result. This way some properties can be translated from integrals over sets without dealing with the cases `a ≤ b` and `b ≤ a` separately. ### Choice of the interval We use integral over `Ioc (min a b) (max a b)` instead of one of the other three possible intervals with the same endpoints for two reasons: * this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever `f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom at `b`; this rules out `Ioo` and `Icc` intervals; * with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) of `μ`. ### `FTC_filter` class As explained above, many theorems in this file rely on the typeclass `FTC_filter (a : α) (l l' : filter α)` to avoid code duplication. This typeclass combines four assumptions: - `pure a ≤ l`; - `l' ≤ 𝓝 a`; - `l'` has a basis of measurable sets; - if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included in `s`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure, it becomes important in the versions of FTC about any locally finite measure if this measure has an atom at one of the endpoints. ## Tags integral, fundamental theorem of calculus -/ noncomputable theory open topological_space (second_countable_topology) open measure_theory set classical filter open_locale classical topological_space filter ennreal big_operators variables {α β 𝕜 E F : Type*} [linear_order α] [measurable_space α] [measurable_space E] [normed_group E] open_locale interval /-! ### Almost everywhere on an interval -/ section variables {μ : measure α} {a b : α} {P : α → Prop} lemma ae_interval_oc_iff : (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂μ, x ∈ Ioc a b → P x) ∧ (∀ᵐ x ∂μ, x ∈ Ioc b a → P x) := by { dsimp [interval_oc], cases le_total a b with hab hab ; simp [hab] } lemma ae_measurable_interval_oc_iff {μ : measure α} {β : Type*} [measurable_space β] {f : α → β} : (ae_measurable f $ μ.restrict $ Ι a b) ↔ (ae_measurable f $ μ.restrict $ Ioc a b) ∧ (ae_measurable f $ μ.restrict $ Ioc b a) := by { dsimp [interval_oc], cases le_total a b with hab hab ; simp [hab] } variables [topological_space α] [opens_measurable_space α] [order_closed_topology α] lemma ae_interval_oc_iff' : (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂ (μ.restrict $ Ioc a b), P x) ∧ (∀ᵐ x ∂ (μ.restrict $ Ioc b a), P x) := begin simp_rw ae_interval_oc_iff, rw [ae_restrict_eq, eventually_inf_principal, ae_restrict_eq, eventually_inf_principal] ; exact measurable_set_Ioc end end /-! ### Integrability at an interval -/ /-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these intervals is always empty, so this property is equivalent to `f` being integrable on `(min a b, max a b]`. -/ def interval_integrable (f : α → E) (μ : measure α) (a b : α) := integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ /-- A function is interval integrable with respect to a given measure `μ` on `interval a b` if and only if it is integrable on `Ioc (min a b) (max a b)` with respect to `μ`. This is an equivalent defintion of `interval_integrable`. -/ lemma interval_integrable_iff {f : α → E} {a b : α} {μ : measure α} : interval_integrable f μ a b ↔ integrable_on f (Ioc (min a b) (max a b)) μ := by cases le_total a b; simp [h, interval_integrable] /-- If a function is interval integrable with respect to a given measure `μ` on `interval a b` then it is integrable on `Ioc (min a b) (max a b)` with respect to `μ`. -/ lemma interval_integrable.def {f : α → E} {a b : α} {μ : measure α} (h : interval_integrable f μ a b) : integrable_on f (Ioc (min a b) (max a b)) μ := interval_integrable_iff.mp h /-- If a function is integrable with respect to a given measure `μ` then it is interval integrable with respect to `μ` on `interval a b`. -/ lemma measure_theory.integrable.interval_integrable {f : α → E} {a b : α} {μ : measure α} (hf : integrable f μ) : interval_integrable f μ a b := ⟨hf.integrable_on, hf.integrable_on⟩ namespace interval_integrable section variables {f : α → E} {a b c d : α} {μ ν : measure α} @[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a := h.symm @[refl] lemma refl : interval_integrable f μ a a := by split; simp @[trans] lemma trans (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : interval_integrable f μ a c := ⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc, (hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩ lemma trans_iterate {a : ℕ → α} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : interval_integrable f μ (a 0) (a n) := begin induction n with n hn, { simp }, { exact (hn (λ k hk, hint k (hk.trans n.lt_succ_self))).trans (hint n n.lt_succ_self) } end lemma neg [borel_space E] (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b := ⟨h.1.neg, h.2.neg⟩ lemma norm [opens_measurable_space E] (h : interval_integrable f μ a b) : interval_integrable (λ x, ∥f x∥) μ a b := ⟨h.1.norm, h.2.norm⟩ lemma abs {f : α → ℝ} (h : interval_integrable f μ a b) : interval_integrable (λ x, abs (f x)) μ a b := h.norm lemma mono (hf : interval_integrable f ν a b) (h1 : interval c d ⊆ interval a b) (h2 : μ ≤ ν) : interval_integrable f μ c d := let ⟨h1₁, h1₂⟩ := interval_subset_interval_iff_le.mp h1 in interval_integrable_iff.mpr $ hf.def.mono (Ioc_subset_Ioc h1₁ h1₂) h2 lemma mono_set (hf : interval_integrable f μ a b) (h : interval c d ⊆ interval a b) : interval_integrable f μ c d := hf.mono h rfl.le lemma mono_measure (hf : interval_integrable f ν a b) (h : μ ≤ ν) : interval_integrable f μ a b := hf.mono rfl.subset h lemma mono_set_ae (hf : interval_integrable f μ a b) (h : Ioc (min c d) (max c d) ≤ᵐ[μ] Ioc (min a b) (max a b)) : interval_integrable f μ c d := interval_integrable_iff.mpr $ hf.def.mono_set_ae h protected lemma ae_measurable (h : interval_integrable f μ a b) : ae_measurable f (μ.restrict (Ioc a b)):= h.1.ae_measurable protected lemma ae_measurable' (h : interval_integrable f μ a b) : ae_measurable f (μ.restrict (Ioc b a)):= h.2.ae_measurable end variables [borel_space E] {f g : α → E} {a b : α} {μ : measure α} lemma smul [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] {f : α → E} {a b : α} {μ : measure α} (h : interval_integrable f μ a b) (r : 𝕜) : interval_integrable (r • f) μ a b := ⟨h.1.smul r, h.2.smul r⟩ @[simp] lemma add [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x + g x) μ a b := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ @[simp] lemma sub [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x - g x) μ a b := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ end interval_integrable section variables {μ : measure ℝ} [locally_finite_measure μ] lemma continuous_on.interval_integrable [borel_space E] {u : ℝ → E} {a b : ℝ} (hu : continuous_on u (interval a b)) : interval_integrable u μ a b := begin split, all_goals { refine measure_theory.integrable_on.mono_set _ Ioc_subset_Icc_self, refine continuous_on.integrable_on_compact is_compact_Icc (hu.mono _) }, exacts [Icc_subset_interval, Icc_subset_interval'] end lemma continuous_on.interval_integrable_of_Icc [borel_space E] {u : ℝ → E} {a b : ℝ} (h : a ≤ b) (hu : continuous_on u (Icc a b)) : interval_integrable u μ a b := continuous_on.interval_integrable ((interval_of_le h).symm ▸ hu) /-- A continuous function on `ℝ` is `interval_integrable` with respect to any locally finite measure `ν` on ℝ. -/ lemma continuous.interval_integrable [borel_space E] {u : ℝ → E} (hu : continuous u) (a b : ℝ) : interval_integrable u μ a b := hu.continuous_on.interval_integrable end section variables {ι : Type*} [topological_space ι] [conditionally_complete_linear_order ι] [order_topology ι] [measurable_space ι] [borel_space ι] {μ : measure ι} [locally_finite_measure μ] [conditionally_complete_linear_order E] [order_topology E] [second_countable_topology E] [borel_space E] lemma interval_integrable_of_monotone_on {u : ι → E} {a b : ι} (hu : ∀ ⦃x y⦄, x ∈ interval a b → y ∈ interval a b → x ≤ y → u x ≤ u y) : interval_integrable u μ a b := begin rw interval_integrable_iff, exact (integrable_on_compact_of_monotone_on is_compact_interval hu).mono_set Ioc_subset_Icc_self, end lemma interval_integrable_of_antimono_on {u : ι → E} {a b : ι} (hu : ∀ ⦃x y⦄, x ∈ interval a b → y ∈ interval a b → x ≤ y → u y ≤ u x) : interval_integrable u μ a b := @interval_integrable_of_monotone_on (order_dual E) _ ‹_› ι _ _ _ _ _ _ _ _ _ ‹_› ‹_› u a b hu lemma interval_integrable_of_monotone {u : ι → E} {a b : ι} (hu : monotone u) : interval_integrable u μ a b := interval_integrable_of_monotone_on (λ x y _ _ hxy, hu hxy) alias interval_integrable_of_monotone ← monotone.interval_integrable lemma interval_integrable_of_antimono {u : ι → E} {a b : ι} (hu : ∀ ⦃x y⦄, x ≤ y → u y ≤ u x) : interval_integrable u μ a b := @interval_integrable_of_monotone (order_dual E) _ ‹_› ι _ _ _ _ _ _ _ _ _ ‹_› ‹_› u a b hu end /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable_ae {f : α → E} {μ : measure α} {l l' : filter α} (hfm : measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := have _ := (hf.integrable_at_filter_ae hfm hμ).eventually, ((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable {f : α → E} {μ : measure α} {l l' : filter α} (hfm : measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := (hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv /-! ### Interval integral: definition and basic properties In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ` and prove some basic properties. -/ variables [second_countable_topology E] [complete_space E] [normed_space ℝ E] [borel_space E] /-- The interval integral `∫ x in a..b, f x ∂μ` is defined as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals `∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/ def interval_integral (f : α → E) (a b : α) (μ : measure α) := ∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r namespace interval_integral section basic variables {a b : α} {f g : α → E} {μ : measure α} @[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 := by simp [interval_integral] lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ := by simp [interval_integral, h] @[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 := sub_self _ lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ := by simp only [interval_integral, neg_sub] lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ := by simp only [integral_symm b, integral_of_le h] lemma integral_cases (f : α → E) (a b) : ∫ x in a..b, f x ∂μ ∈ ({∫ x in Ioc (min a b) (max a b), f x ∂μ, -∫ x in Ioc (min a b) (max a b), f x ∂μ} : set E) := (le_total a b).imp (λ h, by simp [h, integral_of_le]) (λ h, by simp [h, integral_of_ge]) lemma integral_undef (h : ¬ interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 := by cases le_total a b with hab hab; simp only [integral_of_le, integral_of_ge, hab, neg_eq_zero]; refine integral_undef (not_imp_not.mpr integrable.integrable_on' _); simpa [hab] using not_and_distrib.mp h lemma integral_non_ae_measurable (hf : ¬ ae_measurable f (μ.restrict (Ioc (min a b) (max a b)))) : ∫ x in a..b, f x ∂μ = 0 := by cases le_total a b; simpa [integral_of_le, integral_of_ge, h] using integral_non_ae_measurable hf lemma integral_non_ae_measurable_of_le (h : a ≤ b) (hf : ¬ ae_measurable f (μ.restrict (Ioc a b))) : ∫ x in a..b, f x ∂μ = 0 := integral_non_ae_measurable $ by simpa [h] using hf lemma norm_integral_eq_norm_integral_Ioc : ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ := (integral_cases f a b).elim (congr_arg _) (λ h, (congr_arg _ h).trans (norm_neg _)) lemma norm_integral_le_integral_norm_Ioc : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ := calc ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ : norm_integral_eq_norm_integral_Ioc ... ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ : norm_integral_le_integral_norm f lemma norm_integral_le_abs_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ abs (∫ x in a..b, ∥f x∥ ∂μ) := begin simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc], exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _) end lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E} (h : ∀ᵐ x, x ∈ Ioc (min a b) (max a b) → ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := begin rw [norm_integral_eq_norm_integral_Ioc], convert norm_set_integral_le_of_norm_le_const_ae'' _ measurable_set_Ioc h, { rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] }, { simp only [real.volume_Ioc, ennreal.of_real_lt_top] }, end lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E} (h : ∀ x ∈ Ioc (min a b) (max a b), ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h @[simp] lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ := by { simp only [interval_integral, integral_add hf.1 hg.1, integral_add hf.2 hg.2], abel } @[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ := by { simp only [interval_integral, integral_neg], abel } @[simp] lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ := by simpa only [sub_eq_add_neg] using (integral_add hf hg.neg).trans (congr_arg _ integral_neg) @[simp] lemma integral_smul (r : ℝ) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, integral_smul, smul_sub] lemma integral_const' (c : E) : ∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c := by simp only [interval_integral, set_integral_const, sub_smul] @[simp] lemma integral_const {a b : ℝ} (c : E) : ∫ x in a..b, c = (b - a) • c := by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b, max_zero_sub_eq_self] lemma integral_smul_measure (c : ℝ≥0∞) : ∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub] end basic section comp variables {a b c d : ℝ} (f : ℝ → E) @[simp] lemma integral_comp_mul_right (hc : c ≠ 0) : ∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x := begin have A : closed_embedding (λ x, x * c) := (homeomorph.mul_right' c hc).closed_embedding, conv_rhs { rw [← real.smul_map_volume_mul_right hc] }, simp_rw [integral_smul_measure, interval_integral, set_integral_map_of_closed_embedding measurable_set_Ioc A, ennreal.to_real_of_real (abs_nonneg c)], cases lt_or_gt_of_ne hc, { simp [h, mul_div_cancel, hc, abs_of_neg, restrict_congr_set Ico_ae_eq_Ioc] }, { simp [(show 0 < c, from h), mul_div_cancel, hc, abs_of_pos] } end @[simp] lemma smul_integral_comp_mul_right (c) : c • ∫ x in a..b, f (x * c) = ∫ x in a*c..b*c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_left (hc : c ≠ 0) : ∫ x in a..b, f (c * x) = c⁻¹ • ∫ x in c*a..c*b, f x := by simpa only [mul_comm c] using integral_comp_mul_right f hc @[simp] lemma smul_integral_comp_mul_left (c) : c • ∫ x in a..b, f (c * x) = ∫ x in c*a..c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div (hc : c ≠ 0) : ∫ x in a..b, f (x / c) = c • ∫ x in a/c..b/c, f x := by simpa only [inv_inv'] using integral_comp_mul_right f (inv_ne_zero hc) @[simp] lemma inv_smul_integral_comp_div (c) : c⁻¹ • ∫ x in a..b, f (x / c) = ∫ x in a/c..b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_right (d) : ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x := have A : closed_embedding (λ x, x + d) := (homeomorph.add_right d).closed_embedding, calc ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x ∂(measure.map (λ x, x + d) volume) : by simp [interval_integral, set_integral_map_of_closed_embedding _ A] ... = ∫ x in a+d..b+d, f x : by rw [real.map_volume_add_right] @[simp] lemma integral_comp_add_left (d) : ∫ x in a..b, f (d + x) = ∫ x in d+a..d+b, f x := by simpa only [add_comm] using integral_comp_add_right f d @[simp] lemma integral_comp_mul_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x + d) = c⁻¹ • ∫ x in c*a+d..c*b+d, f x := by rw [← integral_comp_add_right, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_mul_add (c d) : c • ∫ x in a..b, f (c * x + d) = ∫ x in c*a+d..c*b+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + c * x) = c⁻¹ • ∫ x in d+c*a..d+c*b, f x := by rw [← integral_comp_add_left, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_add_mul (c d) : c • ∫ x in a..b, f (d + c * x) = ∫ x in d+c*a..d+c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c + d) = c • ∫ x in a/c+d..b/c+d, f x := by simpa only [div_eq_inv_mul, inv_inv'] using integral_comp_mul_add f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_add (c d) : c⁻¹ • ∫ x in a..b, f (x / c + d) = ∫ x in a/c+d..b/c+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + x / c) = c • ∫ x in d+a/c..d+b/c, f x := by simpa only [div_eq_inv_mul, inv_inv'] using integral_comp_add_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_add_div (c d) : c⁻¹ • ∫ x in a..b, f (d + x / c) = ∫ x in d+a/c..d+b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x - d) = c⁻¹ • ∫ x in c*a-d..c*b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_mul_add f hc (-d) @[simp] lemma smul_integral_comp_mul_sub (c d) : c • ∫ x in a..b, f (c * x - d) = ∫ x in c*a-d..c*b-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - c * x) = c⁻¹ • ∫ x in d-c*b..d-c*a, f x := begin simp only [sub_eq_add_neg, neg_mul_eq_neg_mul], rw [integral_comp_add_mul f (neg_ne_zero.mpr hc) d, integral_symm], simp only [inv_neg, smul_neg, neg_neg, neg_smul], end @[simp] lemma smul_integral_comp_sub_mul (c d) : c • ∫ x in a..b, f (d - c * x) = ∫ x in d-c*b..d-c*a, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c - d) = c • ∫ x in a/c-d..b/c-d, f x := by simpa only [div_eq_inv_mul, inv_inv'] using integral_comp_mul_sub f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_sub (c d) : c⁻¹ • ∫ x in a..b, f (x / c - d) = ∫ x in a/c-d..b/c-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - x / c) = c • ∫ x in d-b/c..d-a/c, f x := by simpa only [div_eq_inv_mul, inv_inv'] using integral_comp_sub_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_sub_div (c d) : c⁻¹ • ∫ x in a..b, f (d - x / c) = ∫ x in d-b/c..d-a/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_right (d) : ∫ x in a..b, f (x - d) = ∫ x in a-d..b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_add_right f (-d) @[simp] lemma integral_comp_sub_left (d) : ∫ x in a..b, f (d - x) = ∫ x in d-b..d-a, f x := by simpa only [one_mul, one_smul, inv_one] using integral_comp_sub_mul f one_ne_zero d @[simp] lemma integral_comp_neg : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x := by simpa only [zero_sub] using integral_comp_sub_left f 0 end comp /-! ### Integral is an additive function of the interval In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` as well as a few other identities trivially equivalent to this one. We also prove that `∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`. -/ section order_closed_topology variables [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b c d : α} {f g : α → E} {μ : measure α} lemma integral_Icc_eq_integral_Ioc {f : α → E} {a b : α} (ha : μ {a} = 0) : ∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ := begin cases le_or_lt a b with hab hab, { have : μ.restrict (Icc a b) = μ.restrict (Ioc a b), { rw [← Ioc_union_left hab, measure_theory.measure.restrict_union _ measurable_set_Ioc (measurable_set_singleton a)], { simp [measure_theory.measure.restrict_zero_set ha] }, { simp } }, rw this }, { simp [hab, hab.le] } end /-- If two functions are equal in the relevant interval, their interval integrals are also equal. -/ lemma integral_congr {a b : α} (h : eq_on f g (interval a b)) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := by cases le_total a b with hab hab; simpa [hab, integral_of_le, integral_of_ge] using set_integral_congr measurable_set_Ioc (h.mono Ioc_subset_Icc_self) lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 := begin have hac := hab.trans hbc, simp only [interval_integral, ← add_sub_comm, sub_eq_zero], iterate 4 { rw ← integral_union }, { suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this, rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle, min_left_comm, max_left_comm] }, all_goals { simp [*, measurable_set.union, measurable_set_Ioc, Ioc_disjoint_Ioc_same, Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] } end lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ := by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc] lemma sum_integral_adjacent_intervals {a : ℕ → α} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : ∑ (k : ℕ) in finset.range n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a 0)..(a n), f x ∂μ := begin induction n with n hn, { simp }, { rw [finset.sum_range_succ, hn (λ k hk, hint k (hk.trans n.lt_succ_self))], exact integral_add_adjacent_intervals (interval_integrable.trans_iterate $ λ k hk, hint k (hk.trans n.lt_succ_self)) (hint n n.lt_succ_self) } end lemma integral_interval_sub_left (hab : interval_integrable f μ a b) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ := sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab) lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ := by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm, integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm] lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ := by simp only [sub_eq_add_neg, ← integral_symm, integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)] lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ := by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c, sub_neg_eq_add, sub_eq_neg_add], } lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) : ∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ := begin wlog hab : a ≤ b using [a b] tactic.skip, { rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc (le_refl _)), Iic_union_Ioc_eq_Iic hab], exacts [measurable_set_Iic, measurable_set_Ioc, ha, hb.mono_set (λ _, and.right)] }, { intros ha hb, rw [integral_symm, ← this hb ha, neg_sub] } end /-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/ lemma integral_const_of_cdf [finite_measure μ] (c : E) : ∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c := begin simp only [sub_smul, ← set_integral_const], refine (integral_Iic_sub_Iic _ _).symm; simp only [integrable_on_const, measure_lt_top, or_true] end lemma integral_eq_integral_of_support_subset {f : α → E} {a b} (h : function.support f ⊆ Ioc a b) : ∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ := begin cases le_total a b with hab hab, { rw [integral_of_le hab, ← integral_indicator measurable_set_Ioc, indicator_eq_self.2 h]; apply_instance }, { rw [Ioc_eq_empty hab.not_lt, subset_empty_iff, function.support_eq_empty_iff] at h, simp [h] } end lemma integral_congr_ae' {f g : α → E} (h : ∀ᵐ x ∂μ, x ∈ Ioc a b → f x = g x) (h' : ∀ᵐ x ∂μ, x ∈ Ioc b a → f x = g x) : ∫ (x : α) in a..b, f x ∂μ = ∫ (x : α) in a..b, g x ∂μ := by simp only [interval_integral, set_integral_congr_ae (measurable_set_Ioc) h, set_integral_congr_ae (measurable_set_Ioc) h'] lemma integral_congr_ae {f g : α → E} (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = g x) : ∫ (x : α) in a..b, f x ∂μ = ∫ (x : α) in a..b, g x ∂μ := integral_congr_ae' (ae_interval_oc_iff.mp h).1 (ae_interval_oc_iff.mp h).2 lemma integral_zero_ae {f : α → E} (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = 0) : ∫ (x : α) in a..b, f x ∂μ = 0 := calc ∫ x in a..b, f x ∂μ = ∫ x in a..b, 0 ∂μ : integral_congr_ae h ... = 0 : integral_zero lemma integral_indicator {a₁ a₂ a₃ : α} (h : a₂ ∈ Icc a₁ a₃) {f : α → E} : ∫ x in a₁..a₃, indicator {x | x ≤ a₂} f x ∂ μ = ∫ x in a₁..a₂, f x ∂ μ := begin have : {x | x ≤ a₂} ∩ Ioc a₁ a₃ = Ioc a₁ a₂, from Iic_inter_Ioc_of_le h.2, rw [integral_of_le h.1, integral_of_le (h.1.trans h.2), integral_indicator, measure.restrict_restrict, this], exact measurable_set_Iic, all_goals { apply measurable_set_Iic }, end end order_closed_topology section continuity_wrt_parameter open topological_space variables {X : Type*} [topological_space X] [first_countable_topology X] variables {μ : measure α} /-- Continuity of interval integral with respect to a parameter, at a point within a set. Given `F : X → α → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀` within `s` and at `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀` within `s`. If `(λ x, F x t)` is continuous at `x₀` within `s` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_within_at_of_dominated_interval {F : X → α → E} {x₀ : X} {bound : α → ℝ} {a b : α} {s : set X} (hF_meas : ∀ᶠ x in 𝓝[s] x₀, ae_measurable (F x) (μ.restrict $ Ι a b)) (hF_meas₀ : ae_measurable (F x₀) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ t ∂(μ.restrict $ Ι a b), ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂(μ.restrict $ Ι a b), continuous_within_at (λ x, F x t) s x₀) : continuous_within_at (λ x, ∫ t in a..b, F x t ∂μ) s x₀ := begin have gcs := is_countably_generated_nhds_within x₀ s, cases bound_integrable, cases le_or_lt a b with hab hab; [{ rw interval_oc_of_le hab at *, simp_rw interval_integral.integral_of_le hab }, { rw interval_oc_of_lt hab at *, simp_rw interval_integral.integral_of_ge hab.le, refine tendsto.neg _ }]; apply tendsto_integral_filter_of_dominated_convergence bound gcs hF_meas hF_meas₀ h_bound, exacts [bound_integrable_left, h_cont, bound_integrable_right, h_cont] end /-- Continuity of interval integral with respect to a parameter at a point. Given `F : X → α → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀`. If `(λ x, F x t)` is continuous at `x₀` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_at_of_dominated_interval {F : X → α → E} {x₀ : X} {bound : α → ℝ} {a b : α} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (F x) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ t ∂(μ.restrict $ Ι a b), ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂(μ.restrict $ Ι a b), continuous_at (λ x, F x t) x₀) : continuous_at (λ x, ∫ t in a..b, F x t ∂μ) x₀ := begin rw ← continuous_within_at_univ, apply continuous_within_at_of_dominated_interval ; try { rw nhds_within_univ}, exacts [hF_meas, (mem_of_mem_nhds hF_meas : _), h_bound, bound_integrable, h_cont.mono (λ a, (continuous_within_at_univ (λ x, F x a) x₀).mpr)] end /-- Continuity of interval integral with respect to a parameter. Given `F : X → α → E`, assume each `F x` is ae-measurable on `[a, b]`, and assume it is bounded by a function integrable on `[a, b]` independent of `x`. If `(λ x, F x t)` is continuous for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_of_dominated_interval {F : X → α → E} {bound : α → ℝ} {a b : α} (hF_meas : ∀ x, ae_measurable (F x) $ μ.restrict $ Ι a b) (h_bound : ∀ x, ∀ᵐ t ∂(μ.restrict $ Ι a b), ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂(μ.restrict $ Ι a b), continuous (λ x, F x t)) : continuous (λ x, ∫ t in a..b, F x t ∂μ) := continuous_iff_continuous_at.mpr (λ x₀, continuous_at_of_dominated_interval (eventually_of_forall hF_meas) (eventually_of_forall h_bound) bound_integrable $ h_cont.mono $ λ _, continuous.continuous_at) end continuity_wrt_parameter section continuous_primitive open topological_space variables [topological_space α] [order_topology α] [opens_measurable_space α] [first_countable_topology α] {a b : α} {μ : measure α} lemma continuous_within_at_primitive {f : α → E} {a b₀ b₁ b₂ : α} (hb₀ : μ {b₀} = 0) (h_int : interval_integrable f μ (min a b₁) (max a b₂)) : continuous_within_at (λ b, ∫ x in a .. b, f x ∂ μ) (Icc b₁ b₂) b₀ := begin by_cases h₀ : b₀ ∈ Icc b₁ b₂, { have h₁₂ : b₁ ≤ b₂ := h₀.1.trans h₀.2, have min₁₂ : min b₁ b₂ = b₁ := min_eq_left h₁₂, have h_int' : ∀ {x}, x ∈ Icc b₁ b₂ → interval_integrable f μ b₁ x, { rintros x ⟨h₁, h₂⟩, apply h_int.mono_set, apply interval_subset_interval, { exact ⟨min_le_of_left_le (min_le_right a b₁), h₁.trans (h₂.trans $ le_max_of_le_right $ le_max_right _ _)⟩ }, { exact ⟨min_le_of_left_le $ (min_le_right _ _).trans h₁, le_max_of_le_right $ h₂.trans $ le_max_right _ _⟩ } }, have : ∀ b ∈ Icc b₁ b₂, ∫ x in a..b, f x ∂μ = ∫ x in a..b₁, f x ∂μ + ∫ x in b₁..b, f x ∂μ, { rintros b ⟨h₁, h₂⟩, rw ← integral_add_adjacent_intervals _ (h_int' ⟨h₁, h₂⟩), apply h_int.mono_set, apply interval_subset_interval, { exact ⟨min_le_of_left_le (min_le_left a b₁), le_max_of_le_right (le_max_left _ _)⟩ }, { exact ⟨min_le_of_left_le (min_le_right _ _), le_max_of_le_right (h₁.trans $ h₂.trans (le_max_right a b₂))⟩ } }, apply continuous_within_at.congr _ this (this _ h₀), clear this, refine continuous_within_at_const.add _, have : (λ b, ∫ x in b₁..b, f x ∂μ) =ᶠ[𝓝[Icc b₁ b₂] b₀] λ b, ∫ x in b₁..b₂, indicator {x | x ≤ b} f x ∂ μ, { apply eventually_eq_of_mem self_mem_nhds_within, exact λ b b_in, (integral_indicator b_in).symm }, apply continuous_within_at.congr_of_eventually_eq _ this (integral_indicator h₀).symm, have : interval_integrable (λ x, ∥f x∥) μ b₁ b₂, from interval_integrable.norm (h_int' $ right_mem_Icc.mpr h₁₂), refine continuous_within_at_of_dominated_interval _ _ _ this _ ; clear this, { apply eventually.mono (self_mem_nhds_within), intros x hx, erw [ae_measurable_indicator_iff, measure.restrict_restrict, Iic_inter_Ioc_of_le], { rw min₁₂, exact (h_int' hx).1.ae_measurable }, { exact le_max_of_le_right hx.2 }, exacts [measurable_set_Iic, measurable_set_Iic] }, { erw [ae_measurable_indicator_iff, measure.restrict_restrict, Iic_inter_Ioc_of_le], { rw min₁₂, exact (h_int' h₀).1.ae_measurable }, { exact le_max_of_le_right h₀.2 }, exact measurable_set_Iic, exact measurable_set_Iic }, { refine eventually_of_forall (λ (x : α), eventually_of_forall (λ (t : α), _)), dsimp [indicator], split_ifs ; simp }, { have : ∀ᵐ t ∂μ.restrict (Ι b₁ b₂), t < b₀ ∨ b₀ < t, { apply ae_restrict_of_ae, apply eventually.mono (compl_mem_ae_iff.mpr hb₀), intros x hx, exact ne.lt_or_lt hx }, apply this.mono, rintros x₀ (hx₀ | hx₀), { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : α | t ≤ x}.indicator f x₀ = f x₀, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Ioi_mem_nhds hx₀), intros x hx, simp [hx.le] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀.le] }, { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : α | t ≤ x}.indicator f x₀ = 0, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Iio_mem_nhds hx₀), intros x hx, simp [hx] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀] } } }, { apply continuous_within_at_of_not_mem_closure, rwa [closure_Icc] } end lemma continuous_on_primitive {f : α → E} {a b : α} [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Ioc a x, f t ∂ μ) (Icc a b) := begin by_cases h : a ≤ b, { have : ∀ x ∈ Icc a b, ∫ (t : α) in Ioc a x, f t ∂μ = ∫ (t : α) in a..x, f t ∂μ, { intros x x_in, simp_rw [← interval_oc_of_le h, integral_of_le x_in.1] }, rw continuous_on_congr this, intros x₀ hx₀, refine continuous_within_at_primitive (measure_singleton x₀) _, rw interval_integrable_iff, simp only [h, max_eq_right, min_eq_left], exact h_int.mono Ioc_subset_Icc_self le_rfl }, { rw Icc_eq_empty h, exact continuous_on_empty _ }, end lemma continuous_on_primitive' {f : α → E} {a b : α} [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Icc a x, f t ∂ μ) (Icc a b) := begin rw show (λ x, ∫ t in Icc a x, f t ∂μ) = λ x, ∫ t in Ioc a x, f t ∂μ, by { ext x, exact integral_Icc_eq_integral_Ioc (measure_singleton a) }, exact continuous_on_primitive h_int end lemma continuous_on_primitive'' {f : α → E} {a b : α} [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in a..x, f t ∂ μ) (Icc a b) := (continuous_on_primitive h_int).congr (λ x₀ hx₀, integral_of_le hx₀.1) variables [no_bot_order α] [no_top_order α] [has_no_atoms μ] lemma continuous_primitive {f : α → E} (h_int : ∀ a b : α, interval_integrable f μ a b) (a : α) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := begin rw continuous_iff_continuous_at, intro b₀, cases no_bot b₀ with b₁ hb₁, cases no_top b₀ with b₂ hb₂, apply continuous_within_at.continuous_at _ (Icc_mem_nhds hb₁ hb₂), exact continuous_within_at_primitive (measure_singleton b₀) (h_int _ _) end lemma _root_.measure_theory.integrable.continuous_primitive {f : α → E} (h_int : integrable f μ) (a : α) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := continuous_primitive (λ _ _, h_int.interval_integrable) a end continuous_primitive section variables {f g : α → ℝ} {a b : α} {μ : measure α} lemma integral_eq_zero_iff_of_le_of_nonneg_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Ioc a b)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b)] 0 := by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1] lemma integral_eq_zero_iff_of_nonneg_ae (hf : 0 ≤ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] 0 := begin cases le_total a b with hab hab; simp only [Ioc_eq_empty hab.not_lt, empty_union, union_empty] at hf ⊢, { exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi }, { rw [integral_symm, neg_eq_zero, integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm] } end lemma integral_pos_iff_support_of_nonneg_ae' (hf : 0 ≤ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (function.support f ∩ Ioc a b) := begin obtain hab | hab := le_total b a; simp only [Ioc_eq_empty hab.not_lt, empty_union, union_empty] at hf ⊢, { rw [←not_iff_not, not_and_distrib, not_lt, not_lt, integral_of_ge hab, neg_nonpos], exact iff_of_true (integral_nonneg_of_ae hf) (or.intro_left _ hab) }, rw [integral_of_le hab, set_integral_pos_iff_support_of_nonneg_ae hf hfi.1, iff.comm, and_iff_right_iff_imp], contrapose!, intro h, rw [Ioc_eq_empty h.not_lt, inter_empty, measure_empty], exact le_refl 0, end lemma integral_pos_iff_support_of_nonneg_ae (hf : 0 ≤ᵐ[μ] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (function.support f ∩ Ioc a b) := integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi variable (hab : a ≤ b) include hab lemma integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict (Icc a b)] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := let H := ae_restrict_of_ae_restrict_of_subset Ioc_subset_Icc_self hf in by simpa only [integral_of_le hab] using set_integral_nonneg_of_ae_restrict H lemma integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ ae_restrict_of_ae hf lemma integral_nonneg_of_forall (hf : ∀ u, 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae hab $ eventually_of_forall hf lemma integral_nonneg [topological_space α] [opens_measurable_space α] [order_closed_topology α] (hf : ∀ u, u ∈ Icc a b → 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ (ae_restrict_iff' measurable_set_Icc).mpr $ ae_of_all μ hf lemma norm_integral_le_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in a..b, ∥f x∥ ∂μ := norm_integral_le_abs_integral_norm.trans_eq $ abs_of_nonneg $ integral_nonneg_of_forall hab $ λ x, norm_nonneg _ lemma abs_integral_le_integral_abs : abs (∫ x in a..b, f x ∂μ) ≤ ∫ x in a..b, abs (f x) ∂μ := norm_integral_le_integral_norm hab section mono variables (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) include hf hg lemma integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict (Icc a b)] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := h.filter_mono $ ae_mono $ measure.restrict_mono Ioc_subset_Icc_self $ le_refl μ in by simpa only [integral_of_le hab] using set_integral_mono_ae_restrict hf.1 hg.1 H lemma integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := by simpa only [integral_of_le hab] using set_integral_mono_ae hf.1 hg.1 h lemma integral_mono_on [topological_space α] [opens_measurable_space α] [order_closed_topology α] (h : ∀ x ∈ Icc a b, f x ≤ g x) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := λ x hx, h x $ Ioc_subset_Icc_self hx in by simpa only [integral_of_le hab] using set_integral_mono_on hf.1 hg.1 measurable_set_Ioc H lemma integral_mono (h : f ≤ g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := integral_mono_ae hab hf hg $ ae_of_all _ h end mono end /-! ### Fundamental theorem of calculus, part 1, for any measure In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integrals w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by `FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar cases. Lean can automatically find both `a` and `l'` based on `l`. The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes only that `f` has finite limits almost surely at `a` and `b`. Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This theorem is formulated with integral of constants instead of measures in the right hand sides for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is possible to write better `simp` lemmas for these integrals, see `integral_const` and `integral_const_of_cdf`. In the next subsection we apply this theorem to prove various theorems about differentiability of the integral w.r.t. Lebesgue measure. -/ /-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. There are four instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a], 𝓝[Ioi a])`, `(a, 𝓝[Iic a], 𝓝[Iic a])`, and `(a, 𝓝 a, 𝓝 a)`. -/ class FTC_filter {β : Type*} [linear_order β] [measurable_space β] [topological_space β] (a : out_param β) (outer : filter β) (inner : out_param $ filter β) extends tendsto_Ixx_class Ioc outer inner : Prop := (pure_le : pure a ≤ outer) (le_nhds : inner ≤ 𝓝 a) [meas_gen : is_measurably_generated inner] /- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that `FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`. -/ attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class namespace FTC_filter variables [linear_order β] [measurable_space β] [topological_space β] instance pure (a : β) : FTC_filter a (pure a) ⊥ := { pure_le := le_refl _, le_nhds := bot_le } instance nhds_within_singleton (a : β) : FTC_filter a (𝓝[{a}] a) ⊥ := by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance } lemma finite_at_inner {a : β} (l : filter β) {l'} [h : FTC_filter a l l'] {μ : measure β} [locally_finite_measure μ] : μ.finite_at_filter l' := (μ.finite_at_nhds a).filter_mono h.le_nhds variables [opens_measurable_space β] [order_topology β] instance nhds (a : β) : FTC_filter a (𝓝 a) (𝓝 a) := { pure_le := pure_le_nhds a, le_nhds := le_refl _ } instance nhds_univ (a : β) : FTC_filter a (𝓝[univ] a) (𝓝 a) := by { rw nhds_within_univ, apply_instance } instance nhds_left (a : β) : FTC_filter a (𝓝[Iic a] a) (𝓝[Iic a] a) := { pure_le := pure_le_nhds_within right_mem_Iic, le_nhds := inf_le_left } instance nhds_right (a : β) : FTC_filter a (𝓝[Ici a] a) (𝓝[Ioi a] a) := { pure_le := pure_le_nhds_within left_mem_Ici, le_nhds := inf_le_left } end FTC_filter open asymptotics section variables {f : α → E} {a b : α} {c ca cb : E} {l l' la la' lb lb' : filter α} {lt : filter β} {μ : measure α} {u v ua va ub vb : β → α} /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := begin have A := hf.integral_sub_linear_is_o_ae hfm hl (hu.Ioc hv), have B := hf.integral_sub_linear_is_o_ae hfm hl (hv.Ioc hu), simp only [integral_const'], convert (A.trans_le _).sub (B.trans_le _), { ext t, simp_rw [interval_integral, sub_smul], abel }, all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] } end /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l` so that `u ≤ v`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr' (huv.mono $ λ x hx, by simp [integral_const', hx]) (huv.mono $ λ x hx, by simp [integral_const', hx]) /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l` so that `v ≤ u`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $ λ t, by simp [integral_symm (u t), add_comm] variables [topological_space α] section variables [locally_finite_measure μ] [FTC_filter a l l'] include a local attribute [instance] FTC_filter.meas_gen /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l) hu hv huv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge (hfm : measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l) hu hv huv end variables [order_topology α] [borel_space α] local attribute [instance] FTC_filter.meas_gen variables [FTC_filter a la la'] [FTC_filter b lb lb'] [locally_finite_measure μ] /-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f μ a b) (hmeas_a : measurable_at_filter f la' μ) (hmeas_b : measurable_at_filter f lb' μ) (ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) - (∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) (λ t, ∥∫ x in ua t..va t, (1:ℝ) ∂μ∥ + ∥∫ x in ub t..vb t, (1:ℝ) ∂μ∥) lt := begin refine ((measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_a ha_lim hua hva).neg_left.add_add (measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_b hb_lim hub hvb)).congr' _ eventually_eq.rfl, have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) hua hva, have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) (tendsto_const_pure.mono_right FTC_filter.pure_le) hua, have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) hub hvb, have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) (tendsto_const_pure.mono_right FTC_filter.pure_le) hub, filter_upwards [A, A', B, B'], intros t ua_va a_ua ub_vb b_ub, rw [← integral_interval_sub_interval_comm'], { dsimp only [], abel }, exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua] end /-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`. Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f μ a b) (hmeas : measurable_at_filter f lb' μ) (hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab measurable_at_bot hmeas ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv /-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`. Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `la`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f μ a b) (hmeas : measurable_at_filter f la' μ) (hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas measurable_at_bot hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure end /-! ### Fundamental theorem of calculus-1 for Lebesgue measure In this section we restate theorems from the previous section for Lebesgue measure. In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)` at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`. -/ variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter β} {a b z : ℝ} {u v ua ub va vb : β → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb'] /-! #### Auxiliary `is_o` statements In this section we prove several lemmas that can be interpreted as strict differentiability of `(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because we have no definition of `has_strict_(f)deriv_at_filter` in the library. -/ /-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at `l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then `∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/ lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l'] (hfm : measurable_at_filter f l') (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c)) {u v : β → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) (v - u) lt := by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca + o(∥va - ua∥ + ∥vb - ub∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f la') (hmeas_b : measurable_at_filter f lb') (ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) - ((vb t - ub t) • cb - (va t - ua t) • ca)) (λ t, ∥va t - ua t∥ + ∥vb t - ub t∥) lt := by simpa [integral_const] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas_a hmeas_b ha_lim hb_lim hua hva hub hvb /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then `(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `lb`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f volume a b) (hmeas : measurable_at_filter f lb') (hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hmeas hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `f` has a finite limit `c` almost surely at `la'`, then `(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `la`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f volume a b) (hmeas : measurable_at_filter f la') (hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hmeas hf hu hv open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub) /-! #### Strict differentiability In this section we prove that for a measurable function `f` integrable on `a..b`, * `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`, respectively; * `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability provided that `f` is continuous at `a` and `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `b`; * `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability provided that `f` is continuous at `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `a`; * `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability provided that `f` is continuous at `a`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := begin have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb ((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))), refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- **First Fundamental Theorem of Calculus**: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb continuous_at_snd continuous_at_fst /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm hmeas ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm hmeas ha).neg /-! #### Fréchet differentiability In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`, `has_deriv_at`, `fderiv`, and `deriv`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := (integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := (integral_has_strict_fderiv_at hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca := (integral_has_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f (𝓝 a)) (hmeas_b : measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) := (integral_has_fderiv_at hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/ lemma integral_has_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b := (integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/ lemma integral_has_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := (integral_has_strict_deriv_at_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c := (integral_has_deriv_at_of_tendsto_ae_right hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_integral_right (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : deriv (λ u, ∫ x in a..u, f x) b = f b := (integral_has_deriv_at_right hf hmeas hb).deriv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/ lemma integral_has_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := (integral_has_strict_deriv_at_of_tendsto_ae_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/ lemma integral_has_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := (integral_has_strict_deriv_at_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c := (integral_has_deriv_at_of_tendsto_ae_left hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_integral_left (hf : interval_integrable f volume a b) (hmeas : measurable_at_filter f (𝓝 a)) (hb : continuous_at f a) : deriv (λ u, ∫ x in u..b, f x) a = -f a := (integral_has_deriv_at_left hf hmeas hb).deriv /-! #### One-sided derivatives -/ /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at_of_tendsto_ae (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (hmeas_a : measurable_at_filter f la) (hmeas_b : measurable_at_filter f lb) (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s.prod t) (a, b) := begin rw [has_fderiv_within_at, nhds_within_prod_eq], have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd, refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f la) (hmeas_b : measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s.prod t) (a, b) := integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where `s ∈ {Iic a, Ici a, univ}`. -/ meta def unique_diff_within_at_Ici_Iic_univ : tactic unit := `[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic, left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]] /-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}` and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s.prod t)` is equal to `(u, v) ↦ u • cb - v • ca`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma fderiv_within_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : measurable_at_filter f la) (hmeas_b : measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) (ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) : fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s.prod t) (a, b) = ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) := (integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv_within $ hs.prod ht /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb (tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right) derivative `f b` at `b`. -/ lemma integral_has_deriv_within_at_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) : has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b := integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_within_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas: measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = c := (integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `b`, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_within_integral_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = f b := (integral_has_deriv_within_at_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a := by { simp only [integral_symm b], exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm hmeas ha).neg } /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right) derivative `-f a` at `a`. -/ lemma integral_has_deriv_within_at_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a := integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas (ha.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_within_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -c := (integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas ha).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `a`, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_within_integral_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -f a := (integral_has_deriv_within_at_left hf hmeas ha).deriv_within hs /-! ### Fundamental theorem of calculus, part 2 This section contains theorems pertaining to FTC-2 for interval integrals. -/ variable {f' : ℝ → E} /-- The integral of a continuous function is differentiable on a real set `s`. -/ theorem differentiable_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : differentiable_on ℝ (λ u, ∫ x in a..u, f x) s := λ y hy, (integral_has_deriv_at_right (hintg y hy) hcont.measurable.ae_measurable.measurable_at_filter hcont.continuous_at) .differentiable_at.differentiable_within_at /-- The integral of a continuous function is continuous on a real set `s`. This is true even without the assumption of continuity, but a proof of that fact does not yet exist in mathlib. -/ theorem continuous_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : continuous_on (λ u, ∫ x in a..u, f x) s := (differentiable_on_integral_of_continuous hintg hcont).continuous_on /-- **Second Fundamental Theorem of Calculus**: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is continuous on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (hcont' : continuous_on f' (Icc a b)) : ∫ y in a..b, f' y = f b - f a := begin have hmeas' : ae_measurable f' (volume.restrict (Icc a b)), from hcont'.ae_measurable measurable_set_Icc, refine eq_sub_of_add_eq (eq_of_has_deriv_right_eq (λ y hy, _) hderiv (λ y hy, _) hcont (by simp) _ (right_mem_Icc.2 hab)), { refine (integral_has_deriv_within_at_right _ _ _).add_const _, { refine (hcont'.mono _).interval_integrable, simp only [hy.left, Icc_subset_Icc_right hy.right.le, interval_of_le] }, { exact ⟨_, Icc_mem_nhds_within_Ioi hy, hmeas'⟩, }, { exact (hcont' _ (mem_Icc_of_Ico hy)).mono_of_mem (Icc_mem_nhds_within_Ioi hy) } }, { -- TODO: prove that the integral of any integrable function is continuous and use here letI : tendsto_Ixx_class Ioc (𝓟 (Icc a b)) (𝓟 (Ioc a b)) := tendsto_Ixx_class_principal.2 (λ x hx y hy, Ioc_subset_Ioc hx.1 hy.2), haveI : is_measurably_generated (𝓝[Ioc a b] y) := measurable_set_Ioc.nhds_within_is_measurably_generated y, letI : FTC_filter y (𝓝[Icc a b] y) (𝓝[Ioc a b] y) := ⟨pure_le_nhds_within hy, inf_le_left⟩, refine (integral_has_deriv_within_at_right _ _ _).continuous_within_at.add continuous_within_at_const, { exact (hcont'.mono $ Icc_subset_Icc_right hy.2).interval_integrable_of_Icc hy.1 }, { exact ⟨_, mem_sets_of_superset self_mem_nhds_within Ioc_subset_Icc_self, hmeas'⟩ }, { exact (hcont' y hy).mono Ioc_subset_Icc_self } } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is continuous on `[a, b]` then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ico (min a b) (max a b), has_deriv_within_at f (f' x) (Ici x) x) (hcont' : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := begin cases le_total a b with hab hab, { simp only [interval_of_le, min_eq_left, max_eq_right, hab] at hcont hcont' hderiv, exact integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hcont' }, { simp only [interval_of_ge, min_eq_right, max_eq_left, hab] at hcont hcont' hderiv, rw [integral_symm, integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hcont', neg_sub] } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a derivative at `f' x` for all `x` in `(a, b)`, and `f'` is continuous on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at' (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_at f (f' x) x) (hcont' : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := begin refine integral_eq_sub_of_has_deriv_right hcont _ hcont', intros y hy', obtain (hy | hy) : y ∈ Ioo (min a b) (max a b) ∨ min a b = y ∧ y < max a b, { simpa only [le_iff_lt_or_eq, or_and_distrib_right, mem_Ioo, mem_Ico] using hy' }, { exact (hderiv y hy).has_deriv_within_at }, { refine has_deriv_at_interval_left_endpoint_of_tendsto_deriv (λ x hx, (hderiv x hx).has_deriv_within_at.differentiable_within_at) _ _ _, { exact (hcont y (Ico_subset_Icc_self hy')).mono Ioo_subset_Icc_self }, { exact Ioo_mem_nhds_within_Ioi hy' }, { have : tendsto f' (𝓝[Ioi y] y) (𝓝 (f' y)), { refine tendsto.mono_left _ (nhds_within_mono y Ioi_subset_Ici_self), have h := hcont'.continuous_within_at (left_mem_Icc.mpr min_le_max), simpa only [← nhds_within_Icc_eq_nhds_within_Ici hy.2, interval, hy.1] using h }, have h := eventually_of_mem (Ioo_mem_nhds_within_Ioi hy') (λ x hx, (hderiv x hx).deriv), rwa tendsto_congr' h } }, end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a derivative at `f' x` for all `x` in `(a, b)`, and `f'` is continuous on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at'_of_le (hab : a ≤ b) (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hcont' : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_at' hcont (by rwa [min_eq_left hab, max_eq_right hab]) hcont' /-- Fundamental theorem of calculus-2: If `f : ℝ → E` has a derivative at `f' x` for all `x` in `[a, b]` and `f'` is continuous on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at (hderiv : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hcont' : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_at' (has_deriv_at.continuous_on hderiv) (λ x hx, hderiv _ (mem_Icc_of_Ioo hx)) hcont' /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is differentiable at every `x` in `[a, b]` and its derivative is continuous on `[a, b]`, then `∫ y in a..b, deriv f y` equals `f b - f a`. -/ theorem integral_deriv_eq_sub (hderiv : ∀ x ∈ interval a b, differentiable_at ℝ f x) (hcont' : continuous_on (deriv f) (interval a b)) : ∫ y in a..b, deriv f y = f b - f a := integral_eq_sub_of_has_deriv_at (λ x hx, (hderiv x hx).has_deriv_at) hcont' theorem integral_deriv_eq_sub' (f) (hderiv : deriv f = f') (hdiff : ∀ x ∈ interval a b, differentiable_at ℝ f x) (hcont' : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := by rw [← hderiv, integral_deriv_eq_sub hdiff]; cc /-! ### Integration by parts -/ theorem integral_deriv_mul_eq_sub {u v u' v' : ℝ → ℝ} (hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x) (hcu' : continuous_on u' (interval a b)) (hcv' : continuous_on v' (interval a b)) : ∫ x in a..b, u' x * v x + u x * v' x = u b * v b - u a * v a := integral_eq_sub_of_has_deriv_at (λ x hx, (hu x hx).mul (hv x hx)) $ (hcu'.mul (has_deriv_at.continuous_on hv)).add ((has_deriv_at.continuous_on hu).mul hcv') theorem integral_mul_deriv_eq_deriv_mul {u v u' v' : ℝ → ℝ} (hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x) (hcu' : continuous_on u' (interval a b)) (hcv' : continuous_on v' (interval a b)) : ∫ x in a..b, u x * v' x = u b * v b - u a * v a - ∫ x in a..b, v x * u' x := begin have hcv := has_deriv_at.continuous_on hv, rw [← integral_deriv_mul_eq_sub hu hv hcu' hcv', ← integral_sub], { exact integral_congr (λ x hx, by simp only [mul_comm, add_sub_cancel']) }, { exact ((hcu'.mul hcv).add ((has_deriv_at.continuous_on hu).mul hcv')).interval_integrable }, { exact (hcv.mul hcu').interval_integrable }, end /-! ### Integration by substitution / Change of variables -/ theorem integral_comp_mul_deriv' {f f' g : ℝ → ℝ} (hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hf' : continuous_on f' (interval a b)) (hg : ∀ x ∈ f '' (interval a b), continuous_at g x) (hgm : ∀ x ∈ f '' (interval a b), measurable_at_filter g (𝓝 x)) : -- TODO: prove that the integral of any integrable function is continuous and use here to remove -- assumption `hgm` ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := let hg' := continuous_at.continuous_on hg in have h : ∀ x ∈ interval a b, has_deriv_at (λ u, ∫ t in f a..f u, g t) ((g ∘ f) x * f' x) x, { intros x hx, have hs := interval_subset_interval_left hx, exact (integral_has_deriv_at_right (hg'.mono $ trans (intermediate_value_interval $ has_deriv_at.continuous_on $ λ y hy, hf y $ hs hy) $ image_subset f hs).interval_integrable (hgm (f x) ⟨x, hx, rfl⟩) $ hg (f x) ⟨x, hx, rfl⟩).comp _ (hf x hx) }, by simp_rw [integral_eq_sub_of_has_deriv_at h $ (hg'.comp (has_deriv_at.continuous_on hf) $ subset_preimage_image f _).mul hf', integral_same, sub_zero] theorem integral_comp_mul_deriv {f f' g : ℝ → ℝ} (h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (interval a b)) (hg : continuous g) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := integral_comp_mul_deriv' h h' (λ x h, hg.continuous_at) (λ x h, hg.measurable.measurable_at_filter) theorem integral_deriv_comp_mul_deriv' {f f' g g' : ℝ → ℝ} (hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ interval (f a) (f b), has_deriv_at g (g' x) x) (hf' : continuous_on f' (interval a b)) (hg1 : continuous_on g' (interval (f a) (f b))) (hg2 : ∀ x ∈ f '' (interval a b), continuous_at g' x) (hgm : ∀ x ∈ f '' (interval a b), measurable_at_filter g' (𝓝 x)) : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := by rw [integral_comp_mul_deriv' hf hf' hg2 hgm, integral_eq_sub_of_has_deriv_at hg hg1] theorem integral_deriv_comp_mul_deriv {f f' g g' : ℝ → ℝ} (hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ interval a b, has_deriv_at g (g' (f x)) (f x)) (hf' : continuous_on f' (interval a b)) (hg' : continuous g') : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := integral_eq_sub_of_has_deriv_at (λ x hx, (hg x hx).comp x $ hf x hx) $ (hg'.comp_continuous_on $ has_deriv_at.continuous_on hf).mul hf' end interval_integral
bfdc8f5fe06ea34fa88ec595f4cb0d4aacd1fe0b
8e31b9e0d8cec76b5aa1e60a240bbd557d01047c
/scratch/simplex1.lean
7c35905d4880348731fe94fc4949514ffbc006cc
[]
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
3,751
lean
import data.matrix data.rat.basic linear_algebra.basis data.fintype tactic.omega import algebra.associated import linear_algebra.determinant open matrix fintype finset variables {m n : ℕ} variables (A : matrix (fin m) (fin n) ℚ) local notation `rvec` n := matrix (fin 1) (fin n) ℚ local notation `cvec` m := matrix (fin m) (fin 1) ℚ -- def column (j : fin n) : cvec m := λ i _, A i j -- def row (i : fin m) : rvec n := λ _ j, A i j variables (b : cvec m) (c : cvec n) --instance : partial_order (matrix (fin m) (fin n) ℚ) := pi.partial_order constant matrix.has_inv : has_inv (matrix (fin n) (fin n) ℚ) local notation M `⬝` N := M.mul N local postfix `ᵀ` : 1500 := transpose instance : partial_order (matrix (fin m ) (fin n) ℚ) := pi.partial_order def is_feasible (x : cvec n) : Prop := 0 ≤ x ∧ A ⬝ x = b instance decidable_le : decidable_rel ((≤) : matrix (fin m) (fin n) ℚ → matrix (fin m) (fin n) ℚ → Prop) := λ M N, show decidable (∀ i j, M i j ≤ N i j), by apply_instance instance (x : cvec n) : decidable (is_feasible A b x) := by dunfold is_feasible; apply_instance def is_optimal (x : cvec n) : Prop := is_feasible A b x ∧ ∀ y, is_feasible A b y → cᵀ ⬝ y ≤ cᵀ ⬝ x def is_invertible (M : matrix (fin m) (fin n) ℚ) : Prop := ∃ h : m = n, by rw h at M; exact det M ≠ 0 instance is_invertible.decidable : decidable_pred (@is_invertible m n) := λ _, by unfold is_invertible; apply_instance def comatrix (M : matrix (fin n) (fin n) ℚ) : matrix (fin n) (fin n) ℚ := begin cases n, { exact fin.elim0 }, { exact λ i j, det (minor M (λ i' : fin n, if i'.1 < i.1 then i'.cast_le sorry else i'.succ) (λ j' : fin n, if j'.1 < j.1 then j'.cast_le sorry else j'.succ)) } end def inverse (M : matrix (fin m) (fin n) ℚ) : matrix (fin n) (fin m) ℚ := if h : m = n then by subst h; exact (det M)⁻¹ • (comatrix M)ᵀ else 0 def check_reduced_cost (x : cvec n) (B : finset (fin n)) : Prop := let permB : matrix (fin n) (fin B.card) ℚ := minor 1 id (λ i, (finset.sort (≤) B).nth_le i.1 (by simpa using i.2)) in let N : finset (fin n) := univ.filter (∉ B) in let permN : matrix (fin n) (fin N.card) ℚ := minor 1 id (λ i, (finset.sort (≤) N).nth_le i.1 (by simpa using i.2)) in let basis := A ⬝ permB in let non_basis := A ⬝ permN in let ctN := cᵀ ⬝ permN in let ctB := cᵀ ⬝ permB in xᵀ ⬝ permN = 0 ∧ is_invertible basis ∧ ctN - ctB ⬝ inverse basis ⬝ non_basis ≤ 0 instance (x : cvec n) (B : finset (fin n)) : decidable (check_reduced_cost A c x B) := by dunfold check_reduced_cost; apply_instance lemma is_optimal_of_check_reduced_cost (x : cvec n) (B : finset (fin n)) : check_reduced_cost A c x B ∧ is_feasible A b x → is_optimal A b c x := sorry set_option profiler true def ex.A : matrix (fin 2) (fin 4) ℚ := λ i j, (list.nth_le [[1,0,1,0], [1,2,0,1]] i sorry).nth_le j sorry def ex.B : finset (fin 4) := {2,3} def ex.c : cvec 4 := λ i _, (list.nth_le [1,1,0,0] i sorry) def ex.b : cvec 2 := λ i _, (list.nth_le [2,4] i sorry) def ex.x : cvec 4 := λ i _, (list.nth_le [0,0,2,4] i sorry) --#reduce (is_feasible ex.A ex.b ex.x : bool) --set_option trace.class_instances true --#reduce (check_reduced_cost ex.A ex.c ex.x ex.B : bool) def ex2.A : matrix (fin 2) (fin 4) ℚ := λ i j, (list.nth_le [[1,0,1,0], [1,2,0,1]] i sorry).nth_le j sorry def ex2.B : finset (fin 4) := {0,1} def ex2.c : cvec 4 := λ i _, (list.nth_le [1,1,0,0] i sorry) def ex2.b : cvec 2 := λ i _, (list.nth_le [2,4] i sorry) def ex2.x : cvec 4 := λ i _, (list.nth_le [2,1,0,0] i sorry) #eval (is_feasible ex2.A ex2.b ex2.x : bool) #eval (check_reduced_cost ex2.A ex2.c ex2.x ex2.B : bool)
a4a7c5b3cd2e71bc32ad1e97bfda5d5de8b4a580
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/compiler/closure_bug4.lean
e6580f157f46b08de86b2d6a53e732e884a017dc
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
545
lean
def f (x : Nat) : Nat × (Nat → String) := let x1 := x + 1; let x2 := x + 2; let x3 := x + 3; let x4 := x + 4; let x5 := x + 5; let x6 := x + 6; let x7 := x + 7; let x8 := x + 8; let x9 := x + 9; let x10 := x + 10; let x11 := x + 11; let x12 := x + 12; let x13 := x + 13; let x14 := x + 14; let x15 := x + 15; let x16 := x + 16; let x17 := x + 17; (x, fun y => toString [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15]) def main (xs : List String) : IO Unit := IO.println ((f (xs.headD "0").toNat!).2 (xs.headD "0").toNat!)
5ac2f2a6cf3d7bdf5fb5e0d5363471cad3a81ef0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/compact_open.lean
134079539294a2f8a996ece3e147f96acf311f78
[ "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
15,994
lean
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton -/ import tactic.tidy import topology.continuous_function.basic import topology.homeomorph import topology.subset_properties import topology.maps /-! # The compact-open topology In this file, we define the compact-open topology on the set of continuous maps between two topological spaces. ## Main definitions * `compact_open` is the compact-open topology on `C(α, β)`. It is declared as an instance. * `ev` is the evaluation map `C(α, β) × α → β`. It is continuous as long as `α` is locally compact. * `coev` is the coevaluation map `β → C(α, β × α)`. It is always continuous. * `continuous_map.curry` is the currying map `C(α × β, γ) → C(α, C(β, γ))`. This map always exists and it is continuous as long as `α × β` is locally compact. * `continuous_map.uncurry` is the uncurrying map `C(α, C(β, γ)) → C(α × β, γ)`. For this map to exist, we need `β` to be locally compact. If `α` is also locally compact, then this map is continuous. * `homeomorph.curry` combines the currying and uncurrying operations into a homeomorphism `C(α × β, γ) ≃ₜ C(α, C(β, γ))`. This homeomorphism exists if `α` and `β` are locally compact. ## Tags compact-open, curry, function space -/ open set open_locale topological_space namespace continuous_map section compact_open variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] /-- A generating set for the compact-open topology (when `s` is compact and `u` is open). -/ def compact_open.gen (s : set α) (u : set β) : set C(α,β) := {f | f '' s ⊆ u} -- The compact-open topology on the space of continuous maps α → β. instance compact_open : topological_space C(α, β) := topological_space.generate_from {m | ∃ (s : set α) (hs : is_compact s) (u : set β) (hu : is_open u), m = compact_open.gen s u} protected lemma is_open_gen {s : set α} (hs : is_compact s) {u : set β} (hu : is_open u) : is_open (compact_open.gen s u) := topological_space.generate_open.basic _ (by dsimp [mem_set_of_eq]; tauto) section functorial variables (g : C(β, γ)) private lemma preimage_gen {s : set α} (hs : is_compact s) {u : set γ} (hu : is_open u) : continuous_map.comp g ⁻¹' (compact_open.gen s u) = compact_open.gen s (g ⁻¹' u) := begin ext ⟨f, _⟩, change g ∘ f '' s ⊆ u ↔ f '' s ⊆ g ⁻¹' u, rw [image_comp, image_subset_iff] end /-- C(α, -) is a functor. -/ lemma continuous_comp : continuous (continuous_map.comp g : C(α, β) → C(α, γ)) := continuous_generated_from $ assume m ⟨s, hs, u, hu, hm⟩, by rw [hm, preimage_gen g hs hu]; exact continuous_map.is_open_gen hs (hu.preimage g.2) end functorial section ev variables (α β) /-- The evaluation map `map C(α, β) × α → β` -/ def ev (p : C(α, β) × α) : β := p.1 p.2 variables {α β} /-- The evaluation map `C(α, β) × α → β` is continuous if `α` is locally compact. -/ lemma continuous_ev [locally_compact_space α] : continuous (ev α β) := continuous_iff_continuous_at.mpr $ assume ⟨f, x⟩ n hn, let ⟨v, vn, vo, fxv⟩ := mem_nhds_iff.mp hn in have v ∈ 𝓝 (f x), from is_open.mem_nhds vo fxv, let ⟨s, hs, sv, sc⟩ := locally_compact_space.local_compact_nhds x (f ⁻¹' v) (f.continuous.tendsto x this) in let ⟨u, us, uo, xu⟩ := mem_nhds_iff.mp hs in show (ev α β) ⁻¹' n ∈ 𝓝 (f, x), from let w := compact_open.gen s v ×ˢ u in have w ⊆ ev α β ⁻¹' n, from assume ⟨f', x'⟩ ⟨hf', hx'⟩, calc f' x' ∈ f' '' s : mem_image_of_mem f' (us hx') ... ⊆ v : hf' ... ⊆ n : vn, have is_open w, from (continuous_map.is_open_gen sc vo).prod uo, have (f, x) ∈ w, from ⟨image_subset_iff.mpr sv, xu⟩, mem_nhds_iff.mpr ⟨w, by assumption, by assumption, by assumption⟩ lemma continuous_ev₁ [locally_compact_space α] (a : α) : continuous (λ f : C(α, β), f a) := continuous_ev.comp (continuous_id.prod_mk continuous_const) instance [t2_space β] [locally_compact_space α] : t2_space C(α, β) := ⟨ begin intros f₁ f₂ h, obtain ⟨p, hp⟩ := not_forall.mp (mt continuous_map.ext h), exact separated_by_continuous (continuous_ev₁ p) hp, end ⟩ end ev section Inf_induced lemma compact_open_le_induced (s : set α) : (continuous_map.compact_open : topological_space C(α, β)) ≤ topological_space.induced (continuous_map.restrict s) continuous_map.compact_open := begin simp only [induced_generate_from_eq, continuous_map.compact_open], apply generate_from_mono, rintros b ⟨a, ⟨c, hc, u, hu, rfl⟩, rfl⟩, refine ⟨coe '' c, hc.image continuous_subtype_coe, u, hu, _⟩, ext f, simp only [compact_open.gen, mem_set_of_eq, mem_preimage, continuous_map.coe_restrict], rw image_comp f (coe : s → α), end /-- The compact-open topology on `C(α, β)` is equal to the infimum of the compact-open topologies on `C(s, β)` for `s` a compact subset of `α`. The key point of the proof is that the union of the compact subsets of `α` is equal to the union of compact subsets of the compact subsets of `α`. -/ lemma compact_open_eq_Inf_induced : (continuous_map.compact_open : topological_space C(α, β)) = ⨅ (s : set α) (hs : is_compact s), topological_space.induced (continuous_map.restrict s) continuous_map.compact_open := begin refine le_antisymm _ _, { refine le_binfi _, exact λ s hs, compact_open_le_induced s }, simp only [← generate_from_Union, induced_generate_from_eq, continuous_map.compact_open], apply generate_from_mono, rintros _ ⟨s, hs, u, hu, rfl⟩, rw mem_Union₂, refine ⟨s, hs, _, ⟨univ, is_compact_iff_is_compact_univ.mp hs, u, hu, rfl⟩, _⟩, ext f, simp only [compact_open.gen, mem_set_of_eq, mem_preimage, continuous_map.coe_restrict], rw image_comp f (coe : s → α), simp end /-- For any subset `s` of `α`, the restriction of continuous functions to `s` is continuous as a function from `C(α, β)` to `C(s, β)` with their respective compact-open topologies. -/ lemma continuous_restrict (s : set α) : continuous (λ F : C(α, β), F.restrict s) := by { rw continuous_iff_le_induced, exact compact_open_le_induced s } lemma nhds_compact_open_eq_Inf_nhds_induced (f : C(α, β)) : 𝓝 f = ⨅ s (hs : is_compact s), (𝓝 (f.restrict s)).comap (continuous_map.restrict s) := by { rw [compact_open_eq_Inf_induced], simp [nhds_infi, nhds_induced] } lemma tendsto_compact_open_restrict {ι : Type*} {l : filter ι} {F : ι → C(α, β)} {f : C(α, β)} (hFf : filter.tendsto F l (𝓝 f)) (s : set α) : filter.tendsto (λ i, (F i).restrict s) l (𝓝 (f.restrict s)) := (continuous_restrict s).continuous_at.tendsto.comp hFf lemma tendsto_compact_open_iff_forall {ι : Type*} {l : filter ι} (F : ι → C(α, β)) (f : C(α, β)) : filter.tendsto F l (𝓝 f) ↔ ∀ s (hs : is_compact s), filter.tendsto (λ i, (F i).restrict s) l (𝓝 (f.restrict s)) := by { rw [compact_open_eq_Inf_induced], simp [nhds_infi, nhds_induced, filter.tendsto_comap_iff] } /-- A family `F` of functions in `C(α, β)` converges in the compact-open topology, if and only if it converges in the compact-open topology on each compact subset of `α`. -/ lemma exists_tendsto_compact_open_iff_forall [locally_compact_space α] [t2_space α] [t2_space β] {ι : Type*} {l : filter ι} [filter.ne_bot l] (F : ι → C(α, β)) : (∃ f, filter.tendsto F l (𝓝 f)) ↔ ∀ (s : set α) (hs : is_compact s), ∃ f, filter.tendsto (λ i, (F i).restrict s) l (𝓝 f) := begin split, { rintros ⟨f, hf⟩ s hs, exact ⟨f.restrict s, tendsto_compact_open_restrict hf s⟩ }, { intros h, choose f hf using h, -- By uniqueness of limits in a `t2_space`, since `λ i, F i x` tends to both `f s₁ hs₁ x` and -- `f s₂ hs₂ x`, we have `f s₁ hs₁ x = f s₂ hs₂ x` have h : ∀ s₁ (hs₁ : is_compact s₁) s₂ (hs₂ : is_compact s₂) (x : α) (hxs₁ : x ∈ s₁) (hxs₂ : x ∈ s₂), f s₁ hs₁ ⟨x, hxs₁⟩ = f s₂ hs₂ ⟨x, hxs₂⟩, { rintros s₁ hs₁ s₂ hs₂ x hxs₁ hxs₂, haveI := is_compact_iff_compact_space.mp hs₁, haveI := is_compact_iff_compact_space.mp hs₂, have h₁ := (continuous_ev₁ (⟨x, hxs₁⟩ : s₁)).continuous_at.tendsto.comp (hf s₁ hs₁), have h₂ := (continuous_ev₁ (⟨x, hxs₂⟩ : s₂)).continuous_at.tendsto.comp (hf s₂ hs₂), exact tendsto_nhds_unique h₁ h₂ }, -- So glue the `f s hs` together and prove that this glued function `f₀` is a limit on each -- compact set `s` have hs : ∀ x : α, ∃ s (hs : is_compact s), s ∈ 𝓝 x, { intros x, obtain ⟨s, hs, hs'⟩ := exists_compact_mem_nhds x, exact ⟨s, hs, hs'⟩ }, refine ⟨lift_cover' _ _ h hs, _⟩, rw tendsto_compact_open_iff_forall, intros s hs, rw lift_cover_restrict', exact hf s hs } end end Inf_induced section coev variables (α β) /-- The coevaluation map `β → C(α, β × α)` sending a point `x : β` to the continuous function on `α` sending `y` to `(x, y)`. -/ def coev (b : β) : C(α, β × α) := ⟨λ a, (b, a), continuous.prod_mk continuous_const continuous_id⟩ variables {α β} lemma image_coev {y : β} (s : set α) : (coev α β y) '' s = ({y} : set β) ×ˢ s := by tidy -- The coevaluation map β → C(α, β × α) is continuous (always). lemma continuous_coev : continuous (coev α β) := continuous_generated_from $ begin rintros _ ⟨s, sc, u, uo, rfl⟩, rw is_open_iff_forall_mem_open, intros y hy, change (coev α β y) '' s ⊆ u at hy, rw image_coev s at hy, rcases generalized_tube_lemma is_compact_singleton sc uo hy with ⟨v, w, vo, wo, yv, sw, vwu⟩, refine ⟨v, _, vo, singleton_subset_iff.mp yv⟩, intros y' hy', change (coev α β y') '' s ⊆ u, rw image_coev s, exact subset.trans (prod_mono (singleton_subset_iff.mpr hy') sw) vwu end end coev section curry /-- Auxiliary definition, see `continuous_map.curry` and `homeomorph.curry`. -/ def curry' (f : C(α × β, γ)) (a : α) : C(β, γ) := ⟨function.curry f a⟩ /-- If a map `α × β → γ` is continuous, then its curried form `α → C(β, γ)` is continuous. -/ lemma continuous_curry' (f : C(α × β, γ)) : continuous (curry' f) := have hf : curry' f = continuous_map.comp f ∘ coev _ _, by { ext, refl }, hf ▸ continuous.comp (continuous_comp f) continuous_coev /-- To show continuity of a map `α → C(β, γ)`, it suffices to show that its uncurried form `α × β → γ` is continuous. -/ lemma continuous_of_continuous_uncurry (f : α → C(β, γ)) (h : continuous (function.uncurry (λ x y, f x y))) : continuous f := by { convert continuous_curry' ⟨_, h⟩, ext, refl } /-- The curried form of a continuous map `α × β → γ` as a continuous map `α → C(β, γ)`. If `a × β` is locally compact, this is continuous. If `α` and `β` are both locally compact, then this is a homeomorphism, see `homeomorph.curry`. -/ def curry (f : C(α × β, γ)) : C(α, C(β, γ)) := ⟨_, continuous_curry' f⟩ /-- The currying process is a continuous map between function spaces. -/ lemma continuous_curry [locally_compact_space (α × β)] : continuous (curry : C(α × β, γ) → C(α, C(β, γ))) := begin apply continuous_of_continuous_uncurry, apply continuous_of_continuous_uncurry, rw ←homeomorph.comp_continuous_iff' (homeomorph.prod_assoc _ _ _).symm, convert continuous_ev; tidy end @[simp] lemma curry_apply (f : C(α × β, γ)) (a : α) (b : β) : f.curry a b = f (a, b) := rfl /-- The uncurried form of a continuous map `α → C(β, γ)` is a continuous map `α × β → γ`. -/ lemma continuous_uncurry_of_continuous [locally_compact_space β] (f : C(α, C(β, γ))) : continuous (function.uncurry (λ x y, f x y)) := have hf : function.uncurry (λ x y, f x y) = ev β γ ∘ prod.map f id, by { ext, refl }, hf ▸ continuous.comp continuous_ev $ continuous.prod_map f.2 id.2 /-- The uncurried form of a continuous map `α → C(β, γ)` as a continuous map `α × β → γ` (if `β` is locally compact). If `α` is also locally compact, then this is a homeomorphism between the two function spaces, see `homeomorph.curry`. -/ def uncurry [locally_compact_space β] (f : C(α, C(β, γ))) : C(α × β, γ) := ⟨_, continuous_uncurry_of_continuous f⟩ /-- The uncurrying process is a continuous map between function spaces. -/ lemma continuous_uncurry [locally_compact_space α] [locally_compact_space β] : continuous (uncurry : C(α, C(β, γ)) → C(α × β, γ)) := begin apply continuous_of_continuous_uncurry, rw ←homeomorph.comp_continuous_iff' (homeomorph.prod_assoc _ _ _), apply continuous.comp continuous_ev (continuous.prod_map continuous_ev id.2); apply_instance end /-- The family of constant maps: `β → C(α, β)` as a continuous map. -/ def const' : C(β, C(α, β)) := curry ⟨prod.fst, continuous_fst⟩ @[simp] lemma coe_const' : (const' : β → C(α, β)) = const := rfl lemma continuous_const' : continuous (const : β → C(α, β)) := const'.continuous end curry end compact_open end continuous_map open continuous_map namespace homeomorph variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] /-- Currying as a homeomorphism between the function spaces `C(α × β, γ)` and `C(α, C(β, γ))`. -/ def curry [locally_compact_space α] [locally_compact_space β] : C(α × β, γ) ≃ₜ C(α, C(β, γ)) := ⟨⟨curry, uncurry, by tidy, by tidy⟩, continuous_curry, continuous_uncurry⟩ /-- If `α` has a single element, then `β` is homeomorphic to `C(α, β)`. -/ def continuous_map_of_unique [unique α] : β ≃ₜ C(α, β) := { to_fun := continuous_map.comp ⟨_, continuous_fst⟩ ∘ coev α β, inv_fun := ev α β ∘ (λ f, (f, default)), left_inv := λ a, rfl, right_inv := λ f, by { ext, rw unique.eq_default x, refl }, continuous_to_fun := continuous.comp (continuous_comp _) continuous_coev, continuous_inv_fun := continuous.comp continuous_ev (continuous.prod_mk continuous_id continuous_const) } @[simp] lemma continuous_map_of_unique_apply [unique α] (b : β) (a : α) : continuous_map_of_unique b a = b := rfl @[simp] lemma continuous_map_of_unique_symm_apply [unique α] (f : C(α, β)) : continuous_map_of_unique.symm f = f default := rfl end homeomorph section quotient_map variables {X₀ X Y Z : Type*} [topological_space X₀] [topological_space X] [topological_space Y] [topological_space Z] [locally_compact_space Y] {f : X₀ → X} lemma quotient_map.continuous_lift_prod_left (hf : quotient_map f) {g : X × Y → Z} (hg : continuous (λ p : X₀ × Y, g (f p.1, p.2))) : continuous g := begin let Gf : C(X₀, C(Y, Z)) := continuous_map.curry ⟨_, hg⟩, have h : ∀ x : X, continuous (λ y, g (x, y)), { intros x, obtain ⟨x₀, rfl⟩ := hf.surjective x, exact (Gf x₀).continuous }, let G : X → C(Y, Z) := λ x, ⟨_, h x⟩, have : continuous G, { rw hf.continuous_iff, exact Gf.continuous }, convert continuous_map.continuous_uncurry_of_continuous ⟨G, this⟩, ext x, cases x, refl, end lemma quotient_map.continuous_lift_prod_right (hf : quotient_map f) {g : Y × X → Z} (hg : continuous (λ p : Y × X₀, g (p.1, f p.2))) : continuous g := begin have : continuous (λ p : X₀ × Y, g ((prod.swap p).1, f (prod.swap p).2)), { exact hg.comp continuous_swap }, have : continuous (λ p : X₀ × Y, (g ∘ prod.swap) (f p.1, p.2)) := this, convert (hf.continuous_lift_prod_left this).comp continuous_swap, ext x, simp, end end quotient_map
0f3b78b59a023acf267beff69b5f6870fff77a09
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Data/PersistentHashMap.lean
ff75ec38e46775b1d94403ded8132dfd3bf6410d
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
213
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.PersistentHashMap.Basic
4769a70c049eea2ec9e6d2231930ed6f475f96f4
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/ematch2.lean
3a2390ca662dbbb798a836a98c9808ea6135f0b0
[ "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,321
lean
class add_comm_monoid (α : Type*) extends has_zero α, has_add α. section variables {α : Type*} [add_comm_monoid α] (a b c : α) lemma add_assoc : a + b + c = a + (b + c) := sorry lemma add_comm : a + b = b + a := sorry instance aa : is_associative α (+) := ⟨sorry⟩ instance ac : is_commutative α (+) := ⟨sorry⟩ end namespace foo universe variables u variables {α : Type u} open tactic meta def add_insts : list (expr × expr) → tactic unit | [] := skip | ((inst, pr)::r) := do assertv `_einst inst pr, add_insts r meta def internalize_hs : list expr → ematch_state → tactic ematch_state | [] s := return s | (h::hs) s := do t ← infer_type h, s ← s^.internalize t, internalize_hs hs s meta def ematch_test (h : name) : tactic unit := do cc ← cc_state.mk_using_hs, ctx ← local_context, ems ← internalize_hs ctx (ematch_state.mk {}), tgt ← target, ems ← ems^.internalize tgt, hlemma ← hinst_lemma.mk_from_decl h, (r, cc, ems) ← ematch_all cc ems hlemma tt, add_insts r section variables [add_comm_monoid α] theorem add_comm_three (a b c : α) : a + b + c = c + b + a := begin ematch_test `add_comm, ematch_test `add_assoc, cc end theorem add.comm4 : ∀ (n m k l : α), n + m + (k + l) = n + k + (m + l) := by cc end end foo
fcdcac64c2048e550bf6b80d4dc65636ffcf02fa
b147e1312077cdcfea8e6756207b3fa538982e12
/data/set/finite.lean
f1e3dc5470a1d22bfc45641a38830ffa9a946dd5
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,417
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Finite sets. -/ import data.set.lattice data.nat.basic logic.function data.fintype open set lattice function universes u v w variables {α : Type u} {β : Type v} {ι : Sort w} namespace set /-- A set is finite if the subtype is a fintype, i.e. there is a list that enumerates its members. -/ def finite (s : set α) : Prop := nonempty (fintype s) /-- A set is infinite if it is not finite. -/ def infinite (s : set α) : Prop := ¬ finite s /-- Construct a fintype from a finset with the same elements. -/ def fintype_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_fintype_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (fintype_of_finset s H) = s.card := fintype.subtype_card s H theorem card_fintype_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_fintype_of_finset s H; congr /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset noncomputable instance finite.fintype {s : set α} (h : finite s) : fintype s := classical.choice h /-- Get a finset from a finite set -/ noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α := @set.to_finset _ _ (finite.fintype h) @[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s := @mem_to_finset _ _ (finite.fintype h) _ theorem finite_mem_finset (s : finset α) : finite {a | a ∈ s} := ⟨fintype_of_finset s (λ _, iff.rfl)⟩ instance decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) := decidable_of_iff _ mem_to_finset instance fintype_empty : fintype (∅ : set α) := fintype_of_finset ∅ $ by simp theorem empty_card : fintype.card (∅ : set α) = 0 := rfl @[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} : @fintype.card (∅ : set α) h = 0 := eq.trans (by congr) empty_card @[simp] theorem finite_empty : @finite α ∅ := ⟨set.fintype_empty⟩ def fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : fintype (insert a s : set α) := fintype_of_finset ⟨a :: s.to_finset.1, multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp theorem card_fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : @fintype.card _ (fintype_insert' s h) = fintype.card s + 1 := by rw [fintype_insert', card_fintype_of_finset]; simp [finset.card, to_finset]; refl @[simp] theorem card_insert {a : α} (s : set α) [fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} : @fintype.card _ d = fintype.card s + 1 := by rw ← card_fintype_insert' s h; congr instance fintype_insert [decidable_eq α] (a : α) (s : set α) [fintype s] : fintype (insert a s : set α) := if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)] else fintype_insert' _ h @[simp] theorem finite_insert (a : α) {s : set α} : finite s → finite (insert a s) | ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq α) _ _ h⟩ lemma to_finset_insert [decidable_eq α] {a : α} {s : set α} (hs : finite s) : (finite_insert a hs).to_finset = insert a hs.to_finset := finset.ext.mpr $ by simp @[elab_as_eliminator] theorem finite.induction_on {C : set α → Prop} {s : set α} (h : finite s) (H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → finite s → C s → C (insert a s)) : C s := let ⟨t⟩ := h in by exactI match s.to_finset, @mem_to_finset _ s _ with | ⟨l, nd⟩, al := begin change ∀ a, a ∈ l ↔ a ∈ s at al, clear _let_match _match t h, revert s nd al, refine multiset.induction_on l _ (λ a l IH, _); intros s nd al, { rw show s = ∅, from eq_empty_iff_forall_not_mem.2 (by simpa using al), exact H0 }, { rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al), cases multiset.nodup_cons.1 nd with m nd', refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (λ _, iff.rfl)), exact m } end end @[elab_as_eliminator] theorem finite.dinduction_on {C : ∀s:set α, finite s → Prop} {s : set α} (h : finite s) (H0 : C ∅ finite_empty) (H1 : ∀ {a s}, a ∉ s → ∀h:finite s, C s h → C (insert a s) (finite_insert a h)) : C s h := have ∀h:finite s, C s h, from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)), this h instance fintype_singleton (a : α) : fintype ({a} : set α) := fintype_insert' _ (not_mem_empty _) @[simp] theorem card_singleton (a : α) : fintype.card ({a} : set α) = 1 := by rw [show fintype.card ({a} : set α) = _, from card_fintype_insert' ∅ (not_mem_empty a)]; refl @[simp] theorem finite_singleton (a : α) : finite ({a} : set α) := ⟨set.fintype_singleton _⟩ instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] : fintype (s ∪ t : set α) := fintype_of_finset (s.to_finset ∪ t.to_finset) $ by simp theorem finite_union {s t : set α} : finite s → finite t → finite (s ∪ t) | ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq α) _ _ hs ht⟩ instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] : fintype ({a ∈ s | p a} : set α) := fintype_of_finset (s.to_finset.filter p) $ by simp instance fintype_inter (s t : set α) [fintype s] [decidable_pred t] : fintype (s ∩ t : set α) := set.fintype_sep s t def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred t] (h : t ⊆ s) : fintype t := by rw ← inter_eq_self_of_subset_right h; apply_instance theorem finite_subset {s : set α} : finite s → ∀ {t : set α}, t ⊆ s → finite t | ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩ instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) := fintype_of_finset (s.to_finset.image f) $ by simp instance fintype_range [decidable_eq β] (f : α → β) [fintype α] : fintype (range f) := fintype_of_finset (finset.univ.image f) $ by simp [range] theorem finite_image {s : set α} (f : α → β) : finite s → finite (f '' s) | ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq β) _ _ h⟩ def fintype_of_fintype_image [decidable_eq β] (s : set α) {f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s := fintype_of_finset ⟨_, @multiset.nodup_filter_map β α g _ (@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ λ a, begin suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s, by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc], rw exists_swap, suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]}, simp [I _, (injective_of_partial_inv I).eq_iff] end theorem finite_of_finite_image {s : set α} {f : α → β} (I : injective f) : finite (f '' s) → finite s | ⟨hs⟩ := by haveI := classical.dec_eq β; exact ⟨fintype_of_fintype_image _ (partial_inv_of_injective I)⟩ theorem finite_preimage {s : set β} {f : α → β} (I : injective f) (h : finite s) : finite (f ⁻¹' s) := finite_of_finite_image I (finite_subset h (image_preimage_subset f s)) instance fintype_Union [decidable_eq α] {ι : Type*} [fintype ι] (f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) := fintype_of_finset (finset.univ.bind (λ i, (f i).to_finset)) $ by simp theorem finite_Union {ι : Type*} [fintype ι] {f : ι → set α} (H : ∀i, finite (f i)) : finite (⋃ i, f i) := ⟨@set.fintype_Union _ (classical.dec_eq α) _ _ _ (λ i, finite.fintype (H i))⟩ theorem finite_sUnion {s : set (set α)} (h : finite s) (H : ∀t∈s, finite t) : finite (⋃₀ s) := by rw sUnion_eq_Union; haveI := finite.fintype h; apply finite_Union; simpa using H instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} := fintype_of_finset (finset.range n) $ by simp instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} := by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1) lemma finite_le_nat (n : ℕ) : finite {i | i ≤ n} := ⟨set.fintype_le_nat _⟩ instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] : fintype (set.prod s t) := fintype_of_finset (s.to_finset.product t.to_finset) $ by simp lemma finite_prod {s : set α} {t : set β} : finite s → finite t → finite (set.prod s t) | ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩ end set namespace finset variables [decidable_eq β] variables {s t u : finset α} {f : α → β} {a : α} lemma finite_to_set (s : finset α) : set.finite (↑s : set α) := set.finite_mem_finset s @[simp] lemma coe_bind {f : α → finset β} : ↑(s.bind f) = (⋃x ∈ (↑s : set α), ↑(f x) : set β) := by simp [set.ext_iff] @[simp] lemma coe_to_finset {s : set α} {hs : set.finite s} : ↑(hs.to_finset) = s := by simp [set.ext_iff] end finset namespace set lemma infinite_univ_nat : infinite (univ : set ℕ) := assume (h : finite (univ : set ℕ)), let ⟨n, hn⟩ := finset.exists_nat_subset_range h.to_finset in have n ∈ finset.range n, from finset.subset_iff.mpr hn $ by simp, by simp * at * lemma not_injective_nat_fintype [fintype α] [decidable_eq α] {f : ℕ → α} : ¬ injective f := assume (h : injective f), have finite (f '' univ), from finite_subset (finset.finite_to_set $ fintype.elems α) (assume a h, fintype.complete a), have finite (univ : set ℕ), from finite_of_finite_image h this, infinite_univ_nat this lemma not_injective_int_fintype [fintype α] [decidable_eq α] {f : ℤ → α} : ¬ injective f := assume hf, have injective (f ∘ (coe : ℕ → ℤ)), from injective_comp hf $ assume i j, int.of_nat_inj, not_injective_nat_fintype this end set
5631e433121cbf6c5437bbe6380cbf924f9bd1e3
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/over_subst.lean
d821251aa3c4a6a22e11deed5a476fa3a196ff3b
[ "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
867
lean
import logic namespace experiment namespace nat constant nat : Type.{1} constant add : nat → nat → nat constant le : nat → nat → Prop constant one : nat infixl `+` := add infix `≤` := le axiom add_assoc (a b c : nat) : (a + b) + c = a + (b + c) axiom add_le_left {a b : nat} (H : a ≤ b) (c : nat) : c + a ≤ c + b end nat namespace int constant int : Type.{1} constant add : int → int → int constant le : int → int → Prop constant one1 : int infixl `+` := add infix `≤` := le axiom add_assoc (a b c : int) : (a + b) + c = a + (b + c) axiom add_le_left {a b : int} (H : a ≤ b) (c : int) : c + a ≤ c + b definition lt (a b : int) := a + one1 ≤ b infix `<` := lt end int open int open nat open eq theorem add_lt_left {a b : int} (H : a < b) (c : int) : c + a < c + b := subst (symm (add_assoc c a one1)) (add_le_left H c) end experiment
23e0687d225fa032ddea4050b0da4297060fae6e
36938939954e91f23dec66a02728db08a7acfcf9
/old-lean4/hex.lean
ff61eb2dfdf0f28f2d9d5f9a0824b704cb5ce124
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
432
lean
namespace Char def isHexDigit (c:Char) : Bool := 'a' ≤ c ∧ c ≤ 'f' ∨ 'A' ≤ c ∧ c ≤ 'F' ∨ '0' ≤ c ∧ c ≤ '9' def hexToNat (c:Char) : Nat := if 'a' ≤ c ∧ c ≤ 'f' then c.toNat - 'a'.toNat else if 'A' ≤ c ∧ c ≤ 'F' then c.toNat - 'A'.toNat else c.toNat - '0'.toNat end Char namespace String def hexToNat (s : String) : Nat := s.foldl (λ n c, 16*n + c.hexToNat) 0 end String
f56e4d6d4407bd5b891a077d7bfc843531458b52
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/src/Lean/Elab/Frontend.lean
d648d89f0e71cb831b5ef54648b6d655445eb96f
[ "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
5,145
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.Elab.Import import Lean.Elab.Command import Lean.Util.Profile namespace Lean.Elab.Frontend structure State where commandState : Command.State parserState : Parser.ModuleParserState cmdPos : String.Pos commands : Array Syntax := #[] structure Context where inputCtx : Parser.InputContext abbrev FrontendM := ReaderT Context $ StateRefT State IO def setCommandState (commandState : Command.State) : FrontendM Unit := modify fun s => { s with commandState := commandState } @[inline] def runCommandElabM (x : Command.CommandElabM Unit) : FrontendM Unit := do let ctx ← read let s ← get let cmdCtx : Command.Context := { cmdPos := s.cmdPos, fileName := ctx.inputCtx.fileName, fileMap := ctx.inputCtx.fileMap } match ← liftM $ EIO.toIO' (do let (_, s) ← (x cmdCtx).run s.commandState; pure $ some s) with | Except.error e => throw <| IO.Error.userError s!"unexpected internal error: {← e.toMessageData.toString}" | Except.ok (some sNew) => setCommandState sNew | Except.ok none => pure () def elabCommandAtFrontend (stx : Syntax) : FrontendM Unit := do runCommandElabM do let infoTreeEnabled := (← getInfoState).enabled if checkTraceOption (← getOptions) `Elab.info then enableInfoTree Command.elabCommand stx enableInfoTree infoTreeEnabled def updateCmdPos : FrontendM Unit := do modify fun s => { s with cmdPos := s.parserState.pos } def getParserState : FrontendM Parser.ModuleParserState := do pure (← get).parserState def getCommandState : FrontendM Command.State := do pure (← get).commandState def setParserState (ps : Parser.ModuleParserState) : FrontendM Unit := modify fun s => { s with parserState := ps } def setMessages (msgs : MessageLog) : FrontendM Unit := modify fun s => { s with commandState := { s.commandState with messages := msgs } } def getInputContext : FrontendM Parser.InputContext := do pure (← read).inputCtx register_builtin_option showPartialSyntaxErrors : Bool := { defValue := false descr := "show elaboration errors from partial syntax tree (i.e. after parser recovery)" } def processCommand : FrontendM Bool := do updateCmdPos let cmdState ← getCommandState let ictx ← getInputContext let pstate ← getParserState let scope := cmdState.scopes.head! let pmctx := { env := cmdState.env, options := scope.opts, currNamespace := scope.currNamespace, openDecls := scope.openDecls } let pos := ictx.fileMap.toPosition pstate.pos match profileit "parsing" scope.opts fun _ => Parser.parseCommand ictx pmctx pstate {} with | (cmd, ps, parseMessages) => modify fun s => { s with commands := s.commands.push cmd } setParserState ps setMessages (cmdState.messages ++ parseMessages) if Parser.isEOI cmd || Parser.isExitCommand cmd then pure true -- Done else profileitM IO.Error "elaboration" scope.opts <| elabCommandAtFrontend cmd if parseMessages.hasErrors && !showPartialSyntaxErrors.get scope.opts then -- discard elaboration errors setMessages (cmdState.messages ++ parseMessages) pure false partial def processCommands : FrontendM Unit := do let done ← processCommand unless done do processCommands end Frontend open Frontend def IO.processCommands (inputCtx : Parser.InputContext) (parserState : Parser.ModuleParserState) (commandState : Command.State) : IO State := do let (_, s) ← (Frontend.processCommands.run { inputCtx := inputCtx }).run { commandState := commandState, parserState := parserState, cmdPos := parserState.pos } pure s def process (input : String) (env : Environment) (opts : Options) (fileName : Option String := none) : IO (Environment × MessageLog) := do let fileName := fileName.getD "<input>" let inputCtx := Parser.mkInputContext input fileName let s ← IO.processCommands inputCtx { : Parser.ModuleParserState } (Command.mkState env {} opts) pure (s.commandState.env, s.commandState.messages) builtin_initialize registerOption `printMessageEndPos { defValue := false, descr := "print end position of each message in addition to start position" } registerTraceClass `Elab.info def getPrintMessageEndPos (opts : Options) : Bool := opts.getBool `printMessageEndPos false @[export lean_run_frontend] def runFrontend (input : String) (opts : Options) (fileName : String) (mainModuleName : Name) : IO (Environment × Bool) := do let inputCtx := Parser.mkInputContext input fileName let (header, parserState, messages) ← Parser.parseHeader inputCtx let (env, messages) ← processHeader header opts messages inputCtx let env := env.setMainModule mainModuleName let s ← IO.processCommands inputCtx parserState (Command.mkState env messages opts) for msg in s.commandState.messages.toList do IO.print (← msg.toString (includeEndPos := getPrintMessageEndPos opts)) pure (s.commandState.env, !s.commandState.messages.hasErrors) end Lean.Elab
8e894796c314d59f92da6bb5fc1ab3968d648591
82e44445c70db0f03e30d7be725775f122d72f3e
/src/ring_theory/witt_vector/witt_polynomial.lean
4c099ea7efcee5c978020b87e202aa06adc5dbaf
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
10,851
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import algebra.char_p.invertible import data.fintype.card import data.mv_polynomial.variables import data.mv_polynomial.comm_ring import data.mv_polynomial.expand import data.zmod.basic /-! # Witt polynomials To endow `witt_vector p R` with a ring structure, we need to study the so-called Witt polynomials. Fix a base value `p : ℕ`. The `p`-adic Witt polynomials are an infinite family of polynomials indexed by a natural number `n`, taking values in an arbitrary ring `R`. The variables of these polynomials are represented by natural numbers. The variable set of the `n`th Witt polynomial contains at most `n+1` elements `{0, ..., n}`, with exactly these variables when `R` has characteristic `0`. These polynomials are used to define the addition and multiplication operators on the type of Witt vectors. (While this type itself is not complicated, the ring operations are what make it interesting.) When the base `p` is invertible in `R`, the `p`-adic Witt polynomials form a basis for `mv_polynomial ℕ R`, equivalent to the standard basis. ## Main declarations * `witt_polynomial p R n`: the `n`-th Witt polynomial, viewed as polynomial over the ring `R` * `X_in_terms_of_W p R n`: if `p` is invertible, the polynomial `X n` is contained in the subalgebra generated by the Witt polynomials. `X_in_terms_of_W p R n` is the explicit polynomial, which upon being bound to the Witt polynomials yields `X n`. * `bind₁_witt_polynomial_X_in_terms_of_W`: the proof of the claim that `bind₁ (X_in_terms_of_W p R) (W_ R n) = X n` * `bind₁_X_in_terms_of_W_witt_polynomial`: the converse of the above statement ## Notation In this file we use the following notation * `p` is a natural number, typically assumed to be prime. * `R` and `S` are commutative rings * `W n` (and `W_ R n` when the ring needs to be explicit) denotes the `n`th Witt polynomial ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ open mv_polynomial open finset (hiding map) open finsupp (single) open_locale big_operators local attribute [-simp] coe_eval₂_hom variables (p : ℕ) variables (R : Type*) [comm_ring R] /-- `witt_polynomial p R n` is the `n`-th Witt polynomial with respect to a prime `p` with coefficients in a commutative ring `R`. It is defined as: `∑_{i ≤ n} p^i X_i^{p^{n-i}} ∈ R[X_0, X_1, X_2, …]`. -/ noncomputable def witt_polynomial (n : ℕ) : mv_polynomial ℕ R := ∑ i in range (n+1), monomial (single i (p ^ (n - i))) (p ^ i) lemma witt_polynomial_eq_sum_C_mul_X_pow (n : ℕ) : witt_polynomial p R n = ∑ i in range (n+1), C (p ^ i : R) * X i ^ (p ^ (n - i)) := begin apply sum_congr rfl, rintro i -, rw [monomial_eq, finsupp.prod_single_index], rw pow_zero, end /-! We set up notation locally to this file, to keep statements short and comprehensible. This allows us to simply write `W n` or `W_ ℤ n`. -/ -- Notation with ring of coefficients explicit localized "notation `W_` := witt_polynomial p" in witt -- Notation with ring of coefficients implicit localized "notation `W` := witt_polynomial p _" in witt open_locale witt open mv_polynomial /- The first observation is that the Witt polynomial doesn't really depend on the coefficient ring. If we map the coefficients through a ring homomorphism, we obtain the corresponding Witt polynomial over the target ring. -/ section variables {R} {S : Type*} [comm_ring S] @[simp] lemma map_witt_polynomial (f : R →+* S) (n : ℕ) : map f (W n) = W n := begin rw [witt_polynomial, ring_hom.map_sum, witt_polynomial, sum_congr rfl], intros i hi, rw [map_monomial, ring_hom.map_pow, ring_hom.map_nat_cast], end variables (R) @[simp] lemma constant_coeff_witt_polynomial [hp : fact p.prime] (n : ℕ) : constant_coeff (witt_polynomial p R n) = 0 := begin simp only [witt_polynomial, ring_hom.map_sum, constant_coeff_monomial], rw [sum_eq_zero], rintro i hi, rw [if_neg], rw [finsupp.single_eq_zero], exact ne_of_gt (pow_pos hp.1.pos _) end @[simp] lemma witt_polynomial_zero : witt_polynomial p R 0 = X 0 := by simp only [witt_polynomial, X, sum_singleton, range_one, pow_zero] @[simp] lemma witt_polynomial_one : witt_polynomial p R 1 = C ↑p * X 1 + (X 0) ^ p := by simp only [witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ_comm, range_one, sum_singleton, one_mul, pow_one, C_1, pow_zero] lemma aeval_witt_polynomial {A : Type*} [comm_ring A] [algebra R A] (f : ℕ → A) (n : ℕ) : aeval f (W_ R n) = ∑ i in range (n+1), p^i * (f i) ^ (p ^ (n-i)) := by simp [witt_polynomial, alg_hom.map_sum, aeval_monomial, finsupp.prod_single_index] /-- Over the ring `zmod (p^(n+1))`, we produce the `n+1`st Witt polynomial by expanding the `n`th witt polynomial by `p`. -/ @[simp] lemma witt_polynomial_zmod_self (n : ℕ) : W_ (zmod (p ^ (n + 1))) (n + 1) = expand p (W_ (zmod (p^(n + 1))) n) := begin simp only [witt_polynomial_eq_sum_C_mul_X_pow], rw [sum_range_succ, ← nat.cast_pow, char_p.cast_eq_zero (zmod (p^(n+1))) (p^(n+1)), C_0, zero_mul, add_zero, alg_hom.map_sum, sum_congr rfl], intros k hk, rw [alg_hom.map_mul, alg_hom.map_pow, expand_X, alg_hom_C, ← pow_mul, ← pow_succ], congr, rw mem_range at hk, rw [add_comm, nat.add_sub_assoc (nat.lt_succ_iff.mp hk), ← add_comm], end section p_prime -- in fact, `0 < p` would be sufficient variables [hp : fact p.prime] include hp lemma witt_polynomial_vars [char_zero R] (n : ℕ) : (witt_polynomial p R n).vars = range (n + 1) := begin have : ∀ i, (monomial (finsupp.single i (p ^ (n - i))) (p ^ i : R)).vars = {i}, { intro i, rw vars_monomial_single, { rw ← pos_iff_ne_zero, apply pow_pos hp.1.pos }, { rw [← nat.cast_pow, nat.cast_ne_zero], apply ne_of_gt, apply pow_pos hp.1.pos i } }, rw [witt_polynomial, vars_sum_of_disjoint], { simp only [this, int.nat_cast_eq_coe_nat, bUnion_singleton_eq_self], }, { simp only [this, int.nat_cast_eq_coe_nat], intros a b h, apply singleton_disjoint.mpr, rwa mem_singleton, }, end lemma witt_polynomial_vars_subset (n : ℕ) : (witt_polynomial p R n).vars ⊆ range (n + 1) := begin rw [← map_witt_polynomial p (int.cast_ring_hom R), ← witt_polynomial_vars p ℤ], apply vars_map, end end p_prime end /-! ## Witt polynomials as a basis of the polynomial algebra If `p` is invertible in `R`, then the Witt polynomials form a basis of the polynomial algebra `mv_polynomial ℕ R`. The polynomials `X_in_terms_of_W` give the coordinate transformation in the backwards direction. -/ /-- The `X_in_terms_of_W p R n` is the polynomial on the basis of Witt polynomials that corresponds to the ordinary `X n`. -/ noncomputable def X_in_terms_of_W [invertible (p : R)] : ℕ → mv_polynomial ℕ R | n := (X n - (∑ i : fin n, have _ := i.2, (C (p^(i : ℕ) : R) * (X_in_terms_of_W i)^(p^(n-i))))) * C (⅟p ^ n : R) lemma X_in_terms_of_W_eq [invertible (p : R)] {n : ℕ} : X_in_terms_of_W p R n = (X n - (∑ i in range n, C (p^i : R) * X_in_terms_of_W p R i ^ p ^ (n - i))) * C (⅟p ^ n : R) := by { rw [X_in_terms_of_W, ← fin.sum_univ_eq_sum_range] } @[simp] lemma constant_coeff_X_in_terms_of_W [hp : fact p.prime] [invertible (p : R)] (n : ℕ) : constant_coeff (X_in_terms_of_W p R n) = 0 := begin apply nat.strong_induction_on n; clear n, intros n IH, rw [X_in_terms_of_W_eq, mul_comm, ring_hom.map_mul, ring_hom.map_sub, ring_hom.map_sum, constant_coeff_C, sum_eq_zero], { simp only [constant_coeff_X, sub_zero, mul_zero] }, { intros m H, rw mem_range at H, simp only [ring_hom.map_mul, ring_hom.map_pow, constant_coeff_C, IH m H], rw [zero_pow, mul_zero], apply pow_pos hp.1.pos, } end @[simp] lemma X_in_terms_of_W_zero [invertible (p : R)] : X_in_terms_of_W p R 0 = X 0 := by rw [X_in_terms_of_W_eq, range_zero, sum_empty, pow_zero, C_1, mul_one, sub_zero] section p_prime variables [hp : fact p.prime] include hp lemma X_in_terms_of_W_vars_aux (n : ℕ) : n ∈ (X_in_terms_of_W p ℚ n).vars ∧ (X_in_terms_of_W p ℚ n).vars ⊆ range (n + 1) := begin apply nat.strong_induction_on n, clear n, intros n ih, rw [X_in_terms_of_W_eq, mul_comm, vars_C_mul, vars_sub_of_disjoint, vars_X, range_succ, insert_eq], swap 3, { apply nonzero_of_invertible }, work_on_goal 0 { simp only [true_and, true_or, eq_self_iff_true, mem_union, mem_singleton], intro i, rw [mem_union, mem_union], apply or.imp id }, work_on_goal 1 { rw [vars_X, singleton_disjoint] }, all_goals { intro H, replace H := vars_sum_subset _ _ H, rw mem_bUnion at H, rcases H with ⟨j, hj, H⟩, rw vars_C_mul at H, swap, { apply pow_ne_zero, exact_mod_cast hp.1.ne_zero }, rw mem_range at hj, replace H := (ih j hj).2 (vars_pow _ _ H), rw mem_range at H }, { rw mem_range, exact lt_of_lt_of_le H hj }, { exact lt_irrefl n (lt_of_lt_of_le H hj) }, end lemma X_in_terms_of_W_vars_subset (n : ℕ) : (X_in_terms_of_W p ℚ n).vars ⊆ range (n + 1) := (X_in_terms_of_W_vars_aux p n).2 end p_prime lemma X_in_terms_of_W_aux [invertible (p : R)] (n : ℕ) : X_in_terms_of_W p R n * C (p^n : R) = X n - ∑ i in range n, C (p^i : R) * (X_in_terms_of_W p R i)^p^(n-i) := by rw [X_in_terms_of_W_eq, mul_assoc, ← C_mul, ← mul_pow, inv_of_mul_self, one_pow, C_1, mul_one] @[simp] lemma bind₁_X_in_terms_of_W_witt_polynomial [invertible (p : R)] (k : ℕ) : bind₁ (X_in_terms_of_W p R) (W_ R k) = X k := begin rw [witt_polynomial_eq_sum_C_mul_X_pow, alg_hom.map_sum], simp only [alg_hom.map_pow, C_pow, alg_hom.map_mul, alg_hom_C], rw [sum_range_succ_comm, nat.sub_self, pow_zero, pow_one, bind₁_X_right, mul_comm, ← C_pow, X_in_terms_of_W_aux], simp only [C_pow, bind₁_X_right, sub_add_cancel], end @[simp] lemma bind₁_witt_polynomial_X_in_terms_of_W [invertible (p : R)] (n : ℕ) : bind₁ (W_ R) (X_in_terms_of_W p R n) = X n := begin apply nat.strong_induction_on n, clear n, intros n H, rw [X_in_terms_of_W_eq, alg_hom.map_mul, alg_hom.map_sub, bind₁_X_right, alg_hom_C, alg_hom.map_sum], have : W_ R n - ∑ i in range n, C (p ^ i : R) * (X i) ^ p ^ (n - i) = C (p ^ n : R) * X n, by simp only [witt_polynomial_eq_sum_C_mul_X_pow, nat.sub_self, sum_range_succ_comm, pow_one, add_sub_cancel, pow_zero], rw [sum_congr rfl, this], { -- this is really slow for some reason rw [mul_right_comm, ← C_mul, ← mul_pow, mul_inv_of_self, one_pow, C_1, one_mul] }, { intros i h, rw mem_range at h, simp only [alg_hom.map_mul, alg_hom.map_pow, alg_hom_C, H i h] }, end
7ee23f50b5b84b34952f70ae027adb37568f6c4f
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/module/equiv.lean
70322a8407a86ee8f332c1665b0e8e5912a4d56e
[ "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
22,539
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro, Anne Baanen, Frédéric Dupuis, Heather Macbeth -/ import algebra.module.linear_map /-! # (Semi)linear equivalences In this file we define * `linear_equiv σ M M₂`, `M ≃ₛₗ[σ] M₂`: an invertible semilinear map. Here, `σ` is a `ring_hom` from `R` to `R₂` and an `e : M ≃ₛₗ[σ] M₂` satisfies `e (c • x) = (σ c) • (e x)`. The plain linear version, with `σ` being `ring_hom.id R`, is denoted by `M ≃ₗ[R] M₂`, and the star-linear version (with `σ` being `star_ring_end`) is denoted by `M ≃ₗ⋆[R] M₂`. ## Implementation notes To ensure that composition works smoothly for semilinear equivalences, we use the typeclasses `ring_hom_comp_triple`, `ring_hom_inv_pair` and `ring_hom_surjective` from `algebra/ring/comp_typeclasses`. The group structure on automorphisms, `linear_equiv.automorphism_group`, is provided elsewhere. ## TODO * Parts of this file have not yet been generalized to semilinear maps ## Tags linear equiv, linear equivalences, linear isomorphism, linear isomorphic -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} variables {k : Type*} {S : Type*} {M : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables {N₁ : Type*} {N₂ : Type*} {N₃ : Type*} {N₄ : Type*} {ι : Type*} section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ @[nolint has_inhabited_instance] structure linear_equiv {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) (M₂ : Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends linear_map σ M M₂, M ≃+ M₂ end attribute [nolint doc_blame] linear_equiv.to_linear_map attribute [nolint doc_blame] linear_equiv.to_add_equiv notation M ` ≃ₛₗ[`:50 σ `] ` M₂ := linear_equiv σ M M₂ notation M ` ≃ₗ[`:50 R `] ` M₂ := linear_equiv (ring_hom.id R) M M₂ notation M ` ≃ₗ⋆[`:50 R `] ` M₂ := linear_equiv (star_ring_end R) M M₂ namespace linear_equiv section add_comm_monoid variables {M₄ : Type*} variables [semiring R] [semiring S] section variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M] [module S M₂] {σ : R →+* S} {σ' : S →+* R} variables [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] include R include σ' instance : has_coe (M ≃ₛₗ[σ] M₂) (M →ₛₗ[σ] M₂) := ⟨to_linear_map⟩ -- see Note [function coercion] instance : has_coe_to_fun (M ≃ₛₗ[σ] M₂) (λ _, M → M₂) := ⟨to_fun⟩ @[simp] lemma coe_mk {to_fun inv_fun map_add map_smul left_inv right_inv } : ⇑(⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₛₗ[σ] M₂) = to_fun := rfl -- This exists for compatibility, previously `≃ₗ[R]` extended `≃` instead of `≃+`. @[nolint doc_blame] def to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂ := λ f, f.to_add_equiv.to_equiv lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[simp] lemma to_equiv_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : e₁.to_equiv = e₂.to_equiv ↔ e₁ = e₂ := to_equiv_injective.eq_iff lemma to_linear_map_injective : injective (coe : (M ≃ₛₗ[σ] M₂) → (M →ₛₗ[σ] M₂)) := λ e₁ e₂ H, to_equiv_injective $ equiv.ext $ linear_map.congr_fun H @[simp, norm_cast] lemma to_linear_map_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : (e₁ : M →ₛₗ[σ] M₂) = e₂ ↔ e₁ = e₂ := to_linear_map_injective.eq_iff instance : semilinear_map_class (M ≃ₛₗ[σ] M₂) σ M M₂ := { coe := linear_equiv.to_fun, coe_injective' := λ f g h, to_linear_map_injective (fun_like.coe_injective h), map_add := map_add', map_smulₛₗ := map_smul' } lemma coe_injective : @injective (M ≃ₛₗ[σ] M₂) (M → M₂) coe_fn := fun_like.coe_injective end section variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [add_comm_monoid M₃] [add_comm_monoid M₄] variables [add_comm_monoid N₁] [add_comm_monoid N₂] variables {module_M : module R M} {module_S_M₂ : module S M₂} {σ : R →+* S} {σ' : S →+* R} variables {re₁ : ring_hom_inv_pair σ σ'} {re₂ : ring_hom_inv_pair σ' σ} variables (e e' : M ≃ₛₗ[σ] M₂) lemma to_linear_map_eq_coe : e.to_linear_map = (e : M →ₛₗ[σ] M₂) := rfl @[simp, norm_cast] theorem coe_coe : ⇑(e : M →ₛₗ[σ] M₂) = e := rfl @[simp] lemma coe_to_equiv : ⇑e.to_equiv = e := rfl @[simp] lemma coe_to_linear_map : ⇑e.to_linear_map = e := rfl @[simp] lemma to_fun_eq_coe : e.to_fun = e := rfl section variables {e e'} @[ext] lemma ext (h : ∀ x, e x = e' x) : e = e' := fun_like.ext _ _ h lemma ext_iff : e = e' ↔ ∀ x, e x = e' x := fun_like.ext_iff protected lemma congr_arg {x x'} : x = x' → e x = e x' := fun_like.congr_arg e protected lemma congr_fun (h : e = e') (x : M) : e x = e' x := fun_like.congr_fun h x end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [module R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [module R M] (x : M) : refl R M x = x := rfl include module_M module_S_M₂ re₁ re₂ /-- Linear equivalences are symmetric. -/ @[symm] def symm (e : M ≃ₛₗ[σ] M₂) : M₂ ≃ₛₗ[σ'] M := { to_fun := e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, inv_fun := e.to_equiv.symm.inv_fun, map_smul' := λ r x, by rw map_smulₛₗ, .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } omit module_M module_S_M₂ re₁ re₂ /-- See Note [custom simps projection] -/ def simps.symm_apply {R : Type*} {S : Type*} [semiring R] [semiring S] {σ : R →+* S} {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {M : Type*} {M₂ : Type*} [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] (e : M ≃ₛₗ[σ] M₂) : M₂ → M := e.symm initialize_simps_projections linear_equiv (to_fun → apply, inv_fun → symm_apply) include σ' @[simp] lemma inv_fun_eq_symm : e.inv_fun = e.symm := rfl omit σ' @[simp] lemma coe_to_equiv_symm : ⇑e.to_equiv.symm = e.symm := rfl variables {module_M₁ : module R₁ M₁} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {module_N₁ : module R₁ N₁} {module_N₂ : module R₁ N₂} variables {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} variables {σ₂₁ : R₂ →+* R₁} {σ₃₂ : R₃ →+* R₂} {σ₃₁ : R₃ →+* R₁} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₃ : ring_hom_inv_pair σ₂₃ σ₃₂} variables [ring_hom_inv_pair σ₁₃ σ₃₁] {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₃₂ : ring_hom_inv_pair σ₃₂ σ₂₃} [ring_hom_inv_pair σ₃₁ σ₁₃] variables (e₁₂ : M₁ ≃ₛₗ[σ₁₂] M₂) (e₂₃ : M₂ ≃ₛₗ[σ₂₃] M₃) include σ₃₁ /-- Linear equivalences are transitive. -/ -- Note: The linter thinks the `ring_hom_comp_triple` argument is doubled -- it is not. @[trans, nolint unused_arguments] def trans : M₁ ≃ₛₗ[σ₁₃] M₃ := { .. e₂₃.to_linear_map.comp e₁₂.to_linear_map, .. e₁₂.to_equiv.trans e₂₃.to_equiv } omit σ₃₁ infixl ` ≪≫ₗ `:80 := @linear_equiv.trans _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) ring_hom_comp_triple.ids ring_hom_comp_triple.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids variables {e₁₂} {e₂₃} @[simp] lemma coe_to_add_equiv : ⇑(e.to_add_equiv) = e := rfl /-- The two paths coercion can take to an `add_monoid_hom` are equivalent -/ lemma to_add_monoid_hom_commutes : e.to_linear_map.to_add_monoid_hom = e.to_add_equiv.to_add_monoid_hom := rfl include σ₃₁ @[simp] theorem trans_apply (c : M₁) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃) c = e₂₃ (e₁₂ c) := rfl omit σ₃₁ include σ' @[simp] theorem apply_symm_apply (c : M₂) : e (e.symm c) = c := e.right_inv c @[simp] theorem symm_apply_apply (b : M) : e.symm (e b) = b := e.left_inv b omit σ' include σ₃₁ σ₂₁ σ₃₂ @[simp] lemma trans_symm : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃).symm = e₂₃.symm.trans e₁₂.symm := rfl lemma symm_trans_apply (c : M₃) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃).symm c = e₁₂.symm (e₂₃.symm c) := rfl omit σ₃₁ σ₂₁ σ₃₂ @[simp] lemma trans_refl : e.trans (refl S M₂) = e := to_equiv_injective e.to_equiv.trans_refl @[simp] lemma refl_trans : (refl R M).trans e = e := to_equiv_injective e.to_equiv.refl_trans include σ' lemma symm_apply_eq {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq lemma eq_symm_apply {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply omit σ' lemma eq_comp_symm {α : Type*} (f : M₂ → α) (g : M₁ → α) : f = g ∘ e₁₂.symm ↔ f ∘ e₁₂ = g := e₁₂.to_equiv.eq_comp_symm f g lemma comp_symm_eq {α : Type*} (f : M₂ → α) (g : M₁ → α) : g ∘ e₁₂.symm = f ↔ g = f ∘ e₁₂ := e₁₂.to_equiv.comp_symm_eq f g lemma eq_symm_comp {α : Type*} (f : α → M₁) (g : α → M₂) : f = e₁₂.symm ∘ g ↔ e₁₂ ∘ f = g := e₁₂.to_equiv.eq_symm_comp f g lemma symm_comp_eq {α : Type*} (f : α → M₁) (g : α → M₂) : e₁₂.symm ∘ g = f ↔ g = e₁₂ ∘ f := e₁₂.to_equiv.symm_comp_eq f g variables [ring_hom_comp_triple σ₂₁ σ₁₃ σ₂₃] [ring_hom_comp_triple σ₃₁ σ₁₂ σ₃₂] include module_M₃ lemma eq_comp_to_linear_map_symm (f : M₂ →ₛₗ[σ₂₃] M₃) (g : M₁ →ₛₗ[σ₁₃] M₃) : f = g.comp e₁₂.symm.to_linear_map ↔ f.comp e₁₂.to_linear_map = g := begin split; intro H; ext, { simp [H, e₁₂.to_equiv.eq_comp_symm f g] }, { simp [←H, ←e₁₂.to_equiv.eq_comp_symm f g] } end lemma comp_to_linear_map_symm_eq (f : M₂ →ₛₗ[σ₂₃] M₃) (g : M₁ →ₛₗ[σ₁₃] M₃) : g.comp e₁₂.symm.to_linear_map = f ↔ g = f.comp e₁₂.to_linear_map := begin split; intro H; ext, { simp [←H, ←e₁₂.to_equiv.comp_symm_eq f g] }, { simp [H, e₁₂.to_equiv.comp_symm_eq f g] } end lemma eq_to_linear_map_symm_comp (f : M₃ →ₛₗ[σ₃₁] M₁) (g : M₃ →ₛₗ[σ₃₂] M₂) : f = e₁₂.symm.to_linear_map.comp g ↔ e₁₂.to_linear_map.comp f = g := begin split; intro H; ext, { simp [H, e₁₂.to_equiv.eq_symm_comp f g] }, { simp [←H, ←e₁₂.to_equiv.eq_symm_comp f g] } end lemma to_linear_map_symm_comp_eq (f : M₃ →ₛₗ[σ₃₁] M₁) (g : M₃ →ₛₗ[σ₃₂] M₂) : e₁₂.symm.to_linear_map.comp g = f ↔ g = e₁₂.to_linear_map.comp f := begin split; intro H; ext, { simp [←H, ←e₁₂.to_equiv.symm_comp_eq f g] }, { simp [H, e₁₂.to_equiv.symm_comp_eq f g] } end omit module_M₃ @[simp] lemma refl_symm [module R M] : (refl R M).symm = linear_equiv.refl R M := rfl @[simp] lemma self_trans_symm [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) : f.trans f.symm = linear_equiv.refl R M := by { ext x, simp } @[simp] lemma symm_trans_self [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) : f.symm.trans f = linear_equiv.refl R M₂ := by { ext x, simp } @[simp, norm_cast] lemma refl_to_linear_map [module R M] : (linear_equiv.refl R M : M →ₗ[R] M) = linear_map.id := rfl @[simp, norm_cast] lemma comp_coe [module R M] [module R M₂] [module R M₃] (f : M ≃ₗ[R] M₂) (f' : M₂ ≃ₗ[R] M₃) : (f' : M₂ →ₗ[R] M₃).comp (f : M →ₗ[R] M₂) = (f.trans f' : M ≃ₗ[R] M₃) := rfl @[simp] lemma mk_coe (h₁ h₂ f h₃ h₄) : (linear_equiv.mk e h₁ h₂ f h₃ h₄ : M ≃ₛₗ[σ] M₂) = e := ext $ λ _, rfl protected theorem map_add (a b : M) : e (a + b) = e a + e b := map_add e a b protected theorem map_zero : e 0 = 0 := map_zero e -- TODO: `simp` isn't picking up `map_smulₛₗ` for `linear_equiv`s without specifying `map_smulₛₗ f` @[simp] protected theorem map_smulₛₗ (c : R) (x : M) : e (c • x) = (σ c) • e x := e.map_smul' c x include module_N₁ module_N₂ theorem map_smul (e : N₁ ≃ₗ[R₁] N₂) (c : R₁) (x : N₁) : e (c • x) = c • e x := map_smulₛₗ e c x omit module_N₁ module_N₂ @[simp] lemma map_sum {s : finset ι} (u : ι → M) : e (∑ i in s, u i) = ∑ i in s, e (u i) := e.to_linear_map.map_sum @[simp] theorem map_eq_zero_iff {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff theorem map_ne_zero_iff {x : M} : e x ≠ 0 ↔ x ≠ 0 := e.to_add_equiv.map_ne_zero_iff include module_M module_S_M₂ re₁ re₂ @[simp] theorem symm_symm (e : M ≃ₛₗ[σ] M₂): e.symm.symm = e := by { cases e, refl } omit module_M module_S_M₂ re₁ re₂ lemma symm_bijective [module R M] [module S M₂] [ring_hom_inv_pair σ' σ] [ring_hom_inv_pair σ σ'] : function.bijective (symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)) := equiv.bijective ⟨(symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)), (symm : (M₂ ≃ₛₗ[σ'] M) → (M ≃ₛₗ[σ] M₂)), symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (f h₁ h₂ h₃ h₄) : (linear_equiv.mk f h₁ h₂ ⇑e h₃ h₄ : M₂ ≃ₛₗ[σ'] M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[simp] theorem symm_mk (f h₁ h₂ h₃ h₄) : (⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm = { to_fun := f, inv_fun := e, ..(⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm } := rfl @[simp] lemma coe_symm_mk [module R M] [module R M₂] {to_fun inv_fun map_add map_smul left_inv right_inv} : ⇑((⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₗ[R] M₂).symm) = inv_fun := rfl protected lemma bijective : function.bijective e := e.to_equiv.bijective protected lemma injective : function.injective e := e.to_equiv.injective protected lemma surjective : function.surjective e := e.to_equiv.surjective protected lemma image_eq_preimage (s : set M) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s protected lemma image_symm_eq_preimage (s : set M₂) : e.symm '' s = e ⁻¹' s := e.to_equiv.symm.image_eq_preimage s end /-- Interpret a `ring_equiv` `f` as an `f`-semilinear equiv. -/ @[simps] def _root_.ring_equiv.to_semilinear_equiv (f : R ≃+* S) : by haveI := ring_hom_inv_pair.of_ring_equiv f; haveI := ring_hom_inv_pair.symm (↑f : R →+* S) (f.symm : S →+* R); exact (R ≃ₛₗ[(↑f : R →+* S)] S) := by exact { to_fun := f, map_smul' := f.map_mul, .. f} variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] /-- An involutive linear map is a linear equivalence. -/ def of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : M ≃ₛₗ[σ] M := { .. f, .. hf.to_perm f } @[simp] lemma coe_of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : ⇑(of_involutive f hf) = f := rfl section restrict_scalars variables (R) [module R M] [module R M₂] [module S M] [module S M₂] [linear_map.compatible_smul M M₂ R S] /-- If `M` and `M₂` are both `R`-semimodules and `S`-semimodules and `R`-semimodule structures are defined by an action of `R` on `S` (formally, we have two scalar towers), then any `S`-linear equivalence from `M` to `M₂` is also an `R`-linear equivalence. See also `linear_map.restrict_scalars`. -/ @[simps] def restrict_scalars (f : M ≃ₗ[S] M₂) : M ≃ₗ[R] M₂ := { to_fun := f, inv_fun := f.symm, left_inv := f.left_inv, right_inv := f.right_inv, .. f.to_linear_map.restrict_scalars R } lemma restrict_scalars_injective : function.injective (restrict_scalars R : (M ≃ₗ[S] M₂) → (M ≃ₗ[R] M₂)) := λ f g h, ext (linear_equiv.congr_fun h : _) @[simp] lemma restrict_scalars_inj (f g : M ≃ₗ[S] M₂) : f.restrict_scalars R = g.restrict_scalars R ↔ f = g := (restrict_scalars_injective R).eq_iff end restrict_scalars section automorphisms variables [module R M] instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, inv := λ f, f.symm, mul_assoc := λ f g h, rfl, mul_one := λ f, ext $ λ x, rfl, one_mul := λ f, ext $ λ x, rfl, mul_left_inv := λ f, ext $ f.left_inv } /-- Restriction from `R`-linear automorphisms of `M` to `R`-linear endomorphisms of `M`, promoted to a monoid hom. -/ @[simps] def automorphism_group.to_linear_map_monoid_hom : (M ≃ₗ[R] M) →* (M →ₗ[R] M) := { to_fun := coe, map_one' := rfl, map_mul' := λ _ _, rfl } /-- The tautological action by `M ≃ₗ[R] M` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_distrib_mul_action : distrib_mul_action (M ≃ₗ[R] M) M := { smul := ($), smul_zero := linear_equiv.map_zero, smul_add := linear_equiv.map_add, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : M ≃ₗ[R] M) (a : M) : f • a = f a := rfl /-- `linear_equiv.apply_distrib_mul_action` is faithful. -/ instance apply_has_faithful_smul : has_faithful_smul (M ≃ₗ[R] M) M := ⟨λ _ _, linear_equiv.ext⟩ instance apply_smul_comm_class : smul_comm_class R (M ≃ₗ[R] M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M ≃ₗ[R] M) R M := { smul_comm := linear_equiv.map_smul } end automorphisms end add_comm_monoid end linear_equiv namespace module /-- `g : R ≃+* S` is `R`-linear when the module structure on `S` is `module.comp_hom S g` . -/ @[simps] def comp_hom.to_linear_equiv {R S : Type*} [semiring R] [semiring S] (g : R ≃+* S) : (by haveI := comp_hom S (↑g : R →+* S); exact (R ≃ₗ[R] S)) := by exact { to_fun := (g : R → S), inv_fun := (g.symm : S → R), map_smul' := g.map_mul, ..g } end module namespace distrib_mul_action variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables [group S] [distrib_mul_action S M] [smul_comm_class S R M] /-- Each element of the group defines a linear equivalence. This is a stronger version of `distrib_mul_action.to_add_equiv`. -/ @[simps] def to_linear_equiv (s : S) : M ≃ₗ[R] M := { ..to_add_equiv M s, ..to_linear_map R M s } /-- Each element of the group defines a module automorphism. This is a stronger version of `distrib_mul_action.to_add_aut`. -/ @[simps] def to_module_aut : S →* M ≃ₗ[R] M := { to_fun := to_linear_equiv R M, map_one' := linear_equiv.ext $ one_smul _, map_mul' := λ a b, linear_equiv.ext $ mul_smul _ _ } end distrib_mul_action namespace add_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] variable (e : M ≃+ M₂) /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl /-- An additive equivalence between commutative additive monoids is a linear equivalence between ℕ-modules -/ def to_nat_linear_equiv : M ≃ₗ[ℕ] M₂ := e.to_linear_equiv $ λ c a, by { erw e.to_add_monoid_hom.map_nsmul, refl } @[simp] lemma coe_to_nat_linear_equiv : ⇑(e.to_nat_linear_equiv) = e := rfl @[simp] lemma to_nat_linear_equiv_to_add_equiv : e.to_nat_linear_equiv.to_add_equiv = e := by { ext, refl } @[simp] lemma _root_.linear_equiv.to_add_equiv_to_nat_linear_equiv (e : M ≃ₗ[ℕ] M₂) : e.to_add_equiv.to_nat_linear_equiv = e := fun_like.coe_injective rfl @[simp] lemma to_nat_linear_equiv_symm : (e.to_nat_linear_equiv).symm = e.symm.to_nat_linear_equiv := rfl @[simp] lemma to_nat_linear_equiv_refl : ((add_equiv.refl M).to_nat_linear_equiv) = linear_equiv.refl ℕ M := rfl @[simp] lemma to_nat_linear_equiv_trans (e₂ : M₂ ≃+ M₃) : (e.to_nat_linear_equiv).trans (e₂.to_nat_linear_equiv) = (e.trans e₂).to_nat_linear_equiv := rfl end add_comm_monoid section add_comm_group variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variable (e : M ≃+ M₂) /-- An additive equivalence between commutative additive groups is a linear equivalence between ℤ-modules -/ def to_int_linear_equiv : M ≃ₗ[ℤ] M₂ := e.to_linear_equiv $ λ c a, e.to_add_monoid_hom.map_zsmul a c @[simp] lemma coe_to_int_linear_equiv : ⇑(e.to_int_linear_equiv) = e := rfl @[simp] lemma to_int_linear_equiv_to_add_equiv : e.to_int_linear_equiv.to_add_equiv = e := by { ext, refl } @[simp] lemma _root_.linear_equiv.to_add_equiv_to_int_linear_equiv (e : M ≃ₗ[ℤ] M₂) : e.to_add_equiv.to_int_linear_equiv = e := fun_like.coe_injective rfl @[simp] lemma to_int_linear_equiv_symm : (e.to_int_linear_equiv).symm = e.symm.to_int_linear_equiv := rfl @[simp] lemma to_int_linear_equiv_refl : ((add_equiv.refl M).to_int_linear_equiv) = linear_equiv.refl ℤ M := rfl @[simp] lemma to_int_linear_equiv_trans (e₂ : M₂ ≃+ M₃) : (e.to_int_linear_equiv).trans (e₂.to_int_linear_equiv) = (e.trans e₂).to_int_linear_equiv := rfl end add_comm_group end add_equiv
8c51321539a8d23350c74a19733c91c3ca697991
2a2864136cf8f2871e86f5fd14e624e3daa8fd24
/Monads/Monad.lean
74831dd1c70389b8d4af60f06c9c80ca774806b5
[ "MIT" ]
permissive
hargoniX/lean-monads
d054ac71a351b7c86f318a477977cc166117b8ec
2e87ca7ddf394641ea1b16bcbd8c384026d68e2f
refs/heads/main
1,693,530,528,286
1,633,100,386,000
1,633,100,386,000
412,509,769
1
0
null
null
null
null
UTF-8
Lean
false
false
3,347
lean
import Monads.Applicative namespace Monads /- The monad is a close relativ to the applicative, however unlike an applicative functor it is in fact able to change the structure of the context the value is wrapped in since it allows users to pass a function that produces a new structure: (α → m β) -/ class Monad (m : Type u → Type v) extends Applicative m where -- return is a keyword so don't use it /- Basically equivalent to pure from applicative. -/ ret : α → m α bind : m α → (α → m β) → m β /- Drop the action on the left and only return the right one. If left contains e.g. a sideffect it will still be executed. -/ and_then {α β : Type u} : m α → m β → m β := λ a1 a2 => bind a1 (Function.const α a2) export Monad (ret) infixl:65 " >>= " => Monads.Monad.bind infixl:60 " >> " => Monads.Monad.and_then /- Establishes some well behavedeness for ret and bind as well as a relation to apply and pure from applicative. -/ class LawfulMonad (m : Type u → Type v) [mon: Monad m] extends LawfulApplicative m : Prop where /- Binding a pure value to a function should be the same as applying the function to the value directly -/ ret_left_id : ∀ (a : α) (h : α → m β), ret a >>= h = h a /- Binding a monadic value to ret should be a no op -/ ret_right_id : ∀ (a : m α), a >>= ret = a /- A modified associativity law for bind -/ bind_assoc : ∀ (a : m α) (g : α → m β) (h : β → m γ), (a >>= g) >>= h = a >>= (λ x => g x >>= h) /- Pure and return should behave equivalently -/ pure_behaved : ∀ (a : α), pure a = mon.ret a /- Apply and bind are related in this way as shown below -/ apply_behaved : ∀ {α β : Type u} (mf : m (α → β)) (ma : m α), mf <*> ma = mf >>= (λ f => ma >>= (λ a => ret $ f a)) /- and_then and seq_right are the same.-/ and_then_behaved: ∀ (a1 : m α) (a2 : m β), a1 *> a2 = a1 >> a2 namespace LawfulMonad variable {m : Type u → Type v} [monad : Monad m] [LawfulMonad m] -- Constructing a lawful applicative from a lawful monad -- pure is just return so don't define it explicitly def bind_apply {α β : Type u} : m (α → β) → m α → m β | mf, ma => mf >>= (λ f => ma >>= (λ a => ret $ f a)) -- law 1 example : ∀ x : m α, bind_apply (ret id) x = x := by intro x simp only [bind_apply] rw [ret_left_id] simp only [id] rw [ret_right_id] -- law 2 example : ∀ (x : α) (g : α → β), bind_apply (ret g) (ret x) = monad.ret (g x) := by intro x g simp only [bind_apply] rw [ret_left_id] rw [ret_left_id] -- law 3 example : ∀ {α β : Type u} (x : α) (g : m (α → β)), bind_apply g (ret x) = bind_apply (ret (· $ x)) g := by intro α β x g simp only [bind_apply] simp only [ret_left_id] -- law 4 (a very automated proof since its just a definition massacre) example : ∀ {α β γ: Type u} (x : m (β → γ)) (y : m (α → β)) (z : m α), bind_apply (bind_apply (bind_apply (ret (@Function.comp α β γ)) x) y) z = bind_apply x (bind_apply y z) := by intro α β γ x y z simp only [bind_apply] simp only [ret_left_id, bind_assoc] end LawfulMonad end Monads
760ca859cb49ecdc106796173e0c95ee6a6ad843
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/nat/pairing.lean
070f78b56d355ffa19871c3ebafa3b5b68f3a0db
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,021
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.nat.sqrt import data.set.lattice /-! # Naturals pairing function This file defines a pairing function for the naturals as follows: ```text 0 1 4 9 16 2 3 5 10 17 6 7 8 11 18 12 13 14 15 19 20 21 22 23 24 ``` It has the advantage of being monotone in both directions and sending `⟦0, n^2 - 1⟧` to `⟦0, n - 1⟧²`. -/ open prod decidable function namespace nat /-- Pairing function for the natural numbers. -/ @[pp_nodot] def mkpair (a b : ℕ) : ℕ := if a < b then b*b + a else a*a + a + b /-- Unpairing function for the natural numbers. -/ @[pp_nodot] def unpair (n : ℕ) : ℕ × ℕ := let s := sqrt n in if n - s*s < s then (n - s*s, s) else (s, n - s*s - s) @[simp] theorem mkpair_unpair (n : ℕ) : mkpair (unpair n).1 (unpair n).2 = n := begin dsimp only [unpair], set s := sqrt n, have sm : s * s + (n - s * s) = n := nat.add_sub_cancel' (sqrt_le _), split_ifs, { simp [mkpair, h, sm] }, { have hl : n - s*s - s ≤ s := nat.sub_le_left_of_le_add (nat.sub_le_left_of_le_add $ by rw ← add_assoc; apply sqrt_le_add), simp [mkpair, hl.not_lt, add_assoc, nat.add_sub_cancel' (le_of_not_gt h), sm] } end theorem mkpair_unpair' {n a b} (H : unpair n = (a, b)) : mkpair a b = n := by simpa [H] using mkpair_unpair n @[simp] theorem unpair_mkpair (a b : ℕ) : unpair (mkpair a b) = (a, b) := begin dunfold mkpair, split_ifs, { show unpair (b * b + a) = (a, b), have be : sqrt (b * b + a) = b, from sqrt_add_eq _ (le_trans (le_of_lt h) (nat.le_add_left _ _)), simp [unpair, be, nat.add_sub_cancel, h] }, { show unpair (a * a + a + b) = (a, b), have ae : sqrt (a * a + (a + b)) = a, { rw sqrt_add_eq, exact add_le_add_left (le_of_not_gt h) _ }, simp [unpair, ae, nat.not_lt_zero, add_assoc] } end lemma surjective_unpair : surjective unpair := λ ⟨m, n⟩, ⟨mkpair m n, unpair_mkpair m n⟩ theorem unpair_lt {n : ℕ} (n1 : 1 ≤ n) : (unpair n).1 < n := let s := sqrt n in begin simp [unpair], change sqrt n with s, by_cases h : n - s * s < s; simp [h], { exact lt_of_lt_of_le h (sqrt_le_self _) }, { simp at h, have s0 : 0 < s := sqrt_pos.2 n1, exact lt_of_le_of_lt h (nat.sub_lt_self n1 (mul_pos s0 s0)) } end @[simp] lemma unpair_zero : unpair 0 = 0 := by { rw unpair, simp } theorem unpair_left_le : ∀ (n : ℕ), (unpair n).1 ≤ n | 0 := by simp | (n+1) := le_of_lt (unpair_lt (nat.succ_pos _)) theorem left_le_mkpair (a b : ℕ) : a ≤ mkpair a b := by simpa using unpair_left_le (mkpair a b) theorem right_le_mkpair (a b : ℕ) : b ≤ mkpair a b := begin by_cases h : a < b; simp [mkpair, h], exact le_trans (le_mul_self _) (nat.le_add_right _ _) end theorem unpair_right_le (n : ℕ) : (unpair n).2 ≤ n := by simpa using right_le_mkpair n.unpair.1 n.unpair.2 theorem mkpair_lt_mkpair_left {a₁ a₂} (b) (h : a₁ < a₂) : mkpair a₁ b < mkpair a₂ b := begin by_cases h₁ : a₁ < b; simp [mkpair, h₁, add_assoc], { by_cases h₂ : a₂ < b; simp [mkpair, h₂, h], simp at h₂, apply add_lt_add_of_le_of_lt, exact mul_self_le_mul_self h₂, exact lt_add_right _ _ _ h }, { simp at h₁, simp [not_lt_of_gt (lt_of_le_of_lt h₁ h)], apply add_lt_add, exact mul_self_lt_mul_self h, apply add_lt_add_right; assumption } end theorem mkpair_lt_mkpair_right (a) {b₁ b₂} (h : b₁ < b₂) : mkpair a b₁ < mkpair a b₂ := begin by_cases h₁ : a < b₁; simp [mkpair, h₁, add_assoc], { simp [mkpair, lt_trans h₁ h, h], exact mul_self_lt_mul_self h }, { by_cases h₂ : a < b₂; simp [mkpair, h₂, h], simp at h₁, rw [add_comm, add_comm _ a, add_assoc, add_lt_add_iff_left], rwa [add_comm, ← sqrt_lt, sqrt_add_eq], exact le_trans h₁ (nat.le_add_left _ _) } end end nat open nat section complete_lattice lemma supr_unpair {α} [complete_lattice α] (f : ℕ → ℕ → α) : (⨆ n : ℕ, f n.unpair.1 n.unpair.2) = ⨆ i j : ℕ, f i j := by rw [← (supr_prod : (⨆ i : ℕ × ℕ, f i.1 i.2) = _), ← nat.surjective_unpair.supr_comp] lemma infi_unpair {α} [complete_lattice α] (f : ℕ → ℕ → α) : (⨅ n : ℕ, f n.unpair.1 n.unpair.2) = ⨅ i j : ℕ, f i j := supr_unpair (show ℕ → ℕ → order_dual α, from f) end complete_lattice namespace set lemma Union_unpair_prod {α β} {s : ℕ → set α} {t : ℕ → set β} : (⋃ n : ℕ, (s n.unpair.fst).prod (t n.unpair.snd)) = (⋃ n, s n).prod (⋃ n, t n) := by { rw [← Union_prod], convert surjective_unpair.Union_comp _, refl } lemma Union_unpair {α} (f : ℕ → ℕ → set α) : (⋃ n : ℕ, f n.unpair.1 n.unpair.2) = ⋃ i j : ℕ, f i j := supr_unpair f lemma Inter_unpair {α} (f : ℕ → ℕ → set α) : (⋂ n : ℕ, f n.unpair.1 n.unpair.2) = ⋂ i j : ℕ, f i j := infi_unpair f end set
e2d216c0f5156fc06ace600014c0a3f16364d6f7
6c00cfda5c9ad50f4a53971f9a9b03647f5ae586
/src/sig.lean
83989d7b723752f1bbe1f5f9ddecb56974817537
[]
no_license
jonsterling/lean-syntax
d505bc5eb88a4a2f9018417ba574b65c266da742
a8e3483cc88df3e5ed22d59a1d52f14df8a49885
refs/heads/master
1,585,287,567,824
1,535,317,257,000
1,535,317,257,000
146,037,577
2
0
null
null
null
null
UTF-8
Lean
false
false
2,575
lean
import category_theory.category inductive bwd (α : Type) : Type | emp {} : bwd | snoc : bwd → α → bwd notation `ε` := @bwd.emp _ infixl `≪`:5 := @bwd.snoc _ notation `⟪` l:(foldl `, ` (h t, (t ≪ h)) ε `⟫`) := l @[simp] def append {α} : bwd α → list α → bwd α | xs [] := xs | xs (y :: ys) := append (xs ≪ y) ys infixl `⋉`:3 := append def fam (α : Type) := α → Type /-- Thinnings -/ inductive thn {α} : bwd α → bwd α → Type | emp {} : thn ⟪⟫ ⟪⟫ | cong {Γ Δ τ} : thn Γ Δ → thn (Γ ≪ τ) (Δ ≪ τ) | drop {Γ Δ τ} : thn Γ Δ → thn (Γ ≪ τ) Δ infixr `⇾`:30 := @thn _ infixl `≤` := @thn _ def idn {α} : Π (Γ : bwd α), Γ ⇾ Γ | bwd.emp := thn.emp | (bwd.snoc Γ _) := thn.cong (idn Γ) def seq {α} : Π {Γ Δ Ξ : bwd α}, Γ ⇾ Δ → Δ ⇾ Ξ → Γ ⇾ Ξ | _ _ _ thn.emp thn.emp := thn.emp | _ _ _ (thn.cong δ) (thn.cong ξ) := thn.cong (seq δ ξ) | _ _ _ (thn.cong δ) (thn.drop ξ) := thn.drop (seq δ ξ) | _ _ _ (thn.drop δ) ξ := thn.drop (seq δ ξ) theorem seq_left_idn {α} : Π {Γ Δ : bwd α} (γ : Δ ⇾ Γ), seq (idn _) γ = γ | _ _ thn.emp := by refl | _ _ (thn.cong ξ) := begin unfold idn seq, rewrite (seq_left_idn ξ) end | _ _ (thn.drop ξ) := begin unfold idn seq, rewrite (seq_left_idn ξ) end inductive arity (α : Type) : Type | mk : list arity → α → arity def sig (α : Type) := fam (arity α) infixl `▶`:3 := arity.mk /-- the clone (type of terms) and type of substitutions -/ mutual inductive cn, sb {α} (𝔖 : sig α) with cn : bwd (arity α) → α → Type | opr {Γ Δ τ} : 𝔖 (Δ ▶ τ) → sb Γ Δ → cn Γ τ | var {Γ Δ τ} : Γ ⇾ ⟪ Δ ▶ τ ⟫ → sb Γ Δ → cn Γ τ with sb : bwd (arity α) → list (arity α) → Type | nil {Γ} : sb Γ [] | cons {Γ Ξ Δ τ} : cn (Γ ⋉ Δ) τ → sb Γ Ξ → sb Γ ((Δ ▶ τ) :: Ξ) namespace lambda_calculus inductive sort : Type | chk | syn open sort inductive LAM : arity sort → Type | lam : LAM ([[[] ▶ syn] ▶ chk] ▶ chk) | app : LAM ([[] ▶ syn, [] ▶ chk] ▶ syn) | up : LAM ([[] ▶ syn] ▶ chk) infix `∙`:5 := cn.opr notation `⦃` l:(foldr `, ` (h t, (sb.cons h t)) (sb.nil _) `⦄`) := l notation `ƛ` t := LAM.lam ∙ ⦃ t ⦄ notation ξ `#` γ := cn.var ξ γ notation `⇑` t := LAM.up ∙ ⦃ t ⦄ notation `x₀` := thn.cong thn.emp def tm (Γ : bwd (arity sort)) := cn LAM Γ chk -- identity function def foo : _ := ƛ ⇑ (x₀ # ⦃⦄) end lambda_calculus
4d54e0e1433e5ef8196d7aaaa2bbb8fb0ac31ba0
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightDistributiveMagma.lean
0b5697b788f68fc8f53fcea86b24c21806bc9df4
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,598
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightDistributiveMagma structure RightDistributiveMagma (A : Type) : Type := (op : (A → (A → A))) (rightDistributive : (∀ {x y z : A} , (op (op y z) x) = (op (op y x) (op z x)))) open RightDistributiveMagma structure Sig (AS : Type) : Type := (opS : (AS → (AS → AS))) structure Product (A : Type) : Type := (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightDistributiveP : (∀ {xP yP zP : (Prod A A)} , (opP (opP yP zP) xP) = (opP (opP yP xP) (opP zP xP)))) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightDistributiveMagma A1)) (Ri2 : (RightDistributiveMagma A2)) : Type := (hom : (A1 → A2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Ri1) x1 x2)) = ((op Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightDistributiveMagma A1)) (Ri2 : (RightDistributiveMagma A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Ri1) x1 x2) ((op Ri2) y1 y2)))))) inductive RightDistributiveMagmaTerm : Type | opL : (RightDistributiveMagmaTerm → (RightDistributiveMagmaTerm → RightDistributiveMagmaTerm)) open RightDistributiveMagmaTerm inductive ClRightDistributiveMagmaTerm (A : Type) : Type | sing : (A → ClRightDistributiveMagmaTerm) | opCl : (ClRightDistributiveMagmaTerm → (ClRightDistributiveMagmaTerm → ClRightDistributiveMagmaTerm)) open ClRightDistributiveMagmaTerm inductive OpRightDistributiveMagmaTerm (n : ℕ) : Type | v : ((fin n) → OpRightDistributiveMagmaTerm) | opOL : (OpRightDistributiveMagmaTerm → (OpRightDistributiveMagmaTerm → OpRightDistributiveMagmaTerm)) open OpRightDistributiveMagmaTerm inductive OpRightDistributiveMagmaTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightDistributiveMagmaTerm2) | sing2 : (A → OpRightDistributiveMagmaTerm2) | opOL2 : (OpRightDistributiveMagmaTerm2 → (OpRightDistributiveMagmaTerm2 → OpRightDistributiveMagmaTerm2)) open OpRightDistributiveMagmaTerm2 def simplifyCl {A : Type} : ((ClRightDistributiveMagmaTerm A) → (ClRightDistributiveMagmaTerm A)) | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightDistributiveMagmaTerm n) → (OpRightDistributiveMagmaTerm n)) | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightDistributiveMagmaTerm2 n A) → (OpRightDistributiveMagmaTerm2 n A)) | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightDistributiveMagma A) → (RightDistributiveMagmaTerm → A)) | Ri (opL x1 x2) := ((op Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightDistributiveMagma A) → ((ClRightDistributiveMagmaTerm A) → A)) | Ri (sing x1) := x1 | Ri (opCl x1 x2) := ((op Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightDistributiveMagma A) → ((vector A n) → ((OpRightDistributiveMagmaTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (opOL x1 x2) := ((op Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightDistributiveMagma A) → ((vector A n) → ((OpRightDistributiveMagmaTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (opOL2 x1 x2) := ((op Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightDistributiveMagmaTerm → Type)} : ((∀ (x1 x2 : RightDistributiveMagmaTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : RightDistributiveMagmaTerm) , (P x))) | popl (opL x1 x2) := (popl _ _ (inductionB popl x1) (inductionB popl x2)) def inductionCl {A : Type} {P : ((ClRightDistributiveMagmaTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClRightDistributiveMagmaTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClRightDistributiveMagmaTerm A)) , (P x)))) | psing popcl (sing x1) := (psing x1) | psing popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl x1) (inductionCl psing popcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightDistributiveMagmaTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpRightDistributiveMagmaTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpRightDistributiveMagmaTerm n)) , (P x)))) | pv popol (v x1) := (pv x1) | pv popol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol x1) (inductionOpB pv popol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightDistributiveMagmaTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpRightDistributiveMagmaTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpRightDistributiveMagmaTerm2 n A)) , (P x))))) | pv2 psing2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 x1) (inductionOp pv2 psing2 popol2 x2)) def stageB : (RightDistributiveMagmaTerm → (Staged RightDistributiveMagmaTerm)) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightDistributiveMagmaTerm A) → (Staged (ClRightDistributiveMagmaTerm A))) | (sing x1) := (Now (sing x1)) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightDistributiveMagmaTerm n) → (Staged (OpRightDistributiveMagmaTerm n))) | (v x1) := (const (code (v x1))) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightDistributiveMagmaTerm2 n A) → (Staged (OpRightDistributiveMagmaTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (opT : ((Repr A) → ((Repr A) → (Repr A)))) end RightDistributiveMagma
12ca6d8278ad365b56ebb5f5920dfee3c47b0ecb
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/fin.lean
845a14eb7903e452f171c6048625527b46325ef8
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
7,702
lean
import util.logic import util.data.nat open nat universe variables u v variables {α : Type u} {β : Type v} lemma fin.veq_def {n} (x y : fin n) : x = y ↔ x.val = y.val := begin split, apply fin.veq_of_eq, apply fin.eq_of_veq, end instance {n : ℕ} : inhabited (fin (succ n)) := inhabited.mk ⟨n,lt_succ_self _⟩ lemma fin.zero_def (n : ℕ) : (0 : fin (succ n)).val = 0 := by refl lemma fin.zero_def' (n : ℕ) : (0 : fin (succ n)) = ⟨0, zero_lt_succ _ ⟩ := by { apply fin.eq_of_veq, rw fin.zero_def } lemma fin.all_eq_zero (x : fin 1) : x = 0 := by { cases x, cases x_val, refl, cases x_is_lt with i h, cases h, } def fin.next {n : ℕ} : fin n → fin (succ n) | ⟨i,P⟩ := ⟨succ i, succ_lt_succ P⟩ def fin.pred' {n} : fin n → fin n | ⟨i,P⟩ := ⟨pred i,lt_of_le_of_lt (pred_le _) P⟩ def fin.succ' {n} : fin n → fin n | ⟨i,P⟩ := if P' : succ i < n then ⟨succ i,P'⟩ else ⟨i,P⟩ lemma fin.val_succ_le_nat_succ {n : ℕ} (x : fin n) : x.succ'.val ≤ succ (x.val) := begin cases n with n ; cases x with x Pn ; unfold fin.succ' fin.val, { cases not_lt_zero _ Pn }, cases decidable.em (succ x < succ n) with h h, { rw dif_pos h }, { rw dif_neg h, apply le_succ }, end lemma fin.pred_def {n : ℕ} (x : fin n) : x.pred'.val = pred x.val := begin cases x with x Px, refl end lemma fin.succ_def {n : ℕ} (x : fin n) (h : succ x.val < n) : (x.succ').val = succ x.val := begin cases x with x Px, unfold fin.succ' fin.val, unfold fin.val at h, rw dif_pos h, end def fin.max {n} : fin (succ n) := ⟨n,lt_succ_self _⟩ lemma fin.max_def {n} : (@fin.max n).val = n := rfl lemma fin.pred_succ' {n} (x : fin n) (h : succ x.val < n) : x.succ'.pred' = x := begin apply fin.eq_of_veq, rw [fin.pred_def,fin.succ_def,pred_succ], apply h, end lemma fin.succ_pred' {n} (x : fin (succ n)) (h : 0 < x) : x.pred'.succ' = x := begin apply fin.eq_of_veq, have h' : 0 < x.val, { rw [fin.lt_def,fin.zero_def] at h, apply h }, rw [fin.succ_def,fin.pred_def,succ_pred_eq_of_pos h'], rw [fin.pred_def,succ_pred_eq_of_pos h'], apply x.is_lt, end lemma fin.succ_le_self {n} (x : fin (succ n)) (h : x < fin.max) : ¬ x.succ' ≤ x := begin rw [fin.le_def,fin.succ_def], apply not_succ_le_self, apply succ_lt_succ, rw [fin.lt_def,fin.max_def] at h, apply h end lemma fin.le_succ_self {n} (x : fin (succ n)) : x ≤ x.succ' := begin cases x with x Px, unfold fin.succ', cases decidable.em (succ x < succ n) with h h, { rw [dif_pos h,fin.le_def], apply le_succ }, { rw [dif_neg h,fin.le_def], }, end lemma fin.eq_zero_of_val_le_zero {n} (x : fin (succ n)) (h : x.val ≤ 0) : x = 0 := begin apply fin.eq_of_veq, rw fin.zero_def, apply le_antisymm h, apply nat.zero_le end lemma fin.val_injective {n} : function.injective (@fin.val n) | ⟨x,_⟩ ⟨.(x),_⟩ rfl := rfl def widen {n} : fin n → fin (nat.succ n) | ⟨i,P⟩ := ⟨i,nat.le_succ_of_le P⟩ def restr {n α} (f : fin (nat.succ n) → α) (x : fin n) : α := f (widen x) lemma widen_val {n : ℕ} (x : fin n) : (widen x).val = x.val := by { cases x, refl } lemma forall_fin_zero_iff_true (p : fin 0 → Prop) : (∀ i, p i) ↔ true := begin split, { intros, trivial }, { intros h' i, apply fin.elim0 i } end lemma forall_split_one {n : ℕ} (p : fin (nat.succ n) → Prop) : (∀ i, p i) ↔ p fin.max ∧ (∀ i, restr p i) := begin split ; intro h, split, { apply h }, { intro, apply h }, { intro i, cases i with i hi, cases (lt_or_eq_of_le $ le_of_lt_succ hi) with h₀ h₁, { apply h.right ⟨i,h₀⟩ }, { subst i, apply h.left } }, end lemma exists_split_one {n : ℕ} (p : fin (nat.succ n) → Prop) : (∃ i, p i) ↔ p fin.max ∨ (∃ i, restr p i) := begin rw [← not_iff_not_iff], simp [not_or_iff_not_and_not,not_exists_iff_forall_not,forall_split_one], refl end def fin.nest' {n m : ℕ} (H : m ≤ n) : fin m → fin n | ⟨i,P⟩ := ⟨i,lt_of_lt_of_le P H⟩ lemma fin.nest'_injective {n m : ℕ} (H : m ≤ n) : function.injective (fin.nest' H) := begin intros i j, cases i with i Hi, cases j with j Hj, unfold fin.nest', intros H, apply fin.eq_of_veq, unfold fin.val, apply fin.veq_of_eq H, end def fin.nest {n m : ℕ} : fin m → fin (m + n) | ⟨i,P⟩ := ⟨i,lt_of_lt_of_le P (le_add_right m n)⟩ def fin.shift {n m : ℕ} : fin n → fin (m + n) | ⟨i,P⟩ := ⟨m+i,add_lt_add_left P _⟩ def fin.split {n m : ℕ} : fin (m + n) → fin m ⊕ fin n | ⟨k,P⟩ := if h : k < m then sum.inl ⟨k,h⟩ else have h₀ : n = (m + n) - m, by rw nat.add_sub_cancel_left, have h₁ : k - m < n, begin rw h₀, apply nat.sub_lt_sub_right _ P, apply le_of_not_gt h end, sum.inr ⟨k-m,h₁⟩ def fin.unsplit {n m : ℕ} : fin m ⊕ fin n → fin (m + n) | (sum.inl n) := fin.nest n | (sum.inr n) := fin.shift n lemma fin.split_nest {n m : ℕ} (k : fin m) : fin.split (@fin.nest n m k) = sum.inl k := begin cases k with k Hk, unfold fin.shift fin.nest fin.split, rw dif_pos Hk end lemma fin.split_shift {n m : ℕ} (k : fin n) : fin.split (@fin.shift n m k) = sum.inr k := begin cases k with k Hk, unfold fin.shift fin.nest fin.split, { have H : ¬ m + k < m, { apply not_lt_of_ge, apply nat.le_add_right }, rw [dif_neg H], apply congr_arg, apply fin.eq_of_veq, unfold fin.val, -- simp, below, invokes two similar lemmas for the -- stability of the proof simp [nat.add_sub_cancel_left,nat.add_sub_cancel] }, end lemma fin.split_unsplit {n m : ℕ} (k : fin m ⊕ fin n) : fin.split (fin.unsplit k) = k := begin cases k with k k ; unfold fin.unsplit, { rw fin.split_nest }, { rw fin.split_shift }, end lemma fin.unsplit_split {n m : ℕ} (k : fin (m + n)) : fin.unsplit (fin.split k) = k := begin cases k with k Hk, cases classical.em (k < m) with h h ; unfold fin.split, { rw dif_pos h, unfold fin.unsplit fin.nest, }, { rw dif_neg h, apply fin.eq_of_veq, unfold fin.unsplit fin.shift fin.val, rw [← nat.add_sub_assoc,nat.add_sub_cancel_left], apply le_of_not_gt h }, end lemma fin.unsplit_eq_iff_eq_split {n m : ℕ} (p : fin m ⊕ fin n) (q : fin (m + n)) : fin.unsplit p = q ↔ p = fin.split q := begin split ; intro h, { rw [← h,fin.split_unsplit], }, { rw [h,fin.unsplit_split], }, end lemma fin.nest_eq_iff_eq_split {n m : ℕ} (p : fin m) (q : fin (m + n)) : fin.nest p = q ↔ sum.inl p = fin.split q := by { rw ← fin.unsplit_eq_iff_eq_split, refl } lemma fin.shift_eq_iff_eq_split {n m : ℕ} (p : fin n) (q : fin (m + n)) : fin.shift p = q ↔ sum.inr p = fin.split q := by { rw ← fin.unsplit_eq_iff_eq_split, refl } lemma fin.split_injective {n m : ℕ} (p q : fin (m + n)) (h : fin.split p = fin.split q) : p = q := by { rw [← fin.unsplit_eq_iff_eq_split,fin.unsplit_split] at h, assumption } lemma fin.val_shift_zero (m n : ℕ) : (@fin.shift _ m (0 : fin (succ n))).val = m := rfl lemma fin.shift_zero (m : ℕ) : fin.max = (@fin.shift _ m (0 : fin 1)) := begin apply fin.eq_of_veq, unfold fin.shift fin.max, simp [fin.val_shift_zero], end lemma fin.val_of_nat_eq_mod (m n : ℕ) : (@fin.of_nat m n).val = n % succ m := by refl lemma fin.val_of_nat {m n : ℕ} (h : n < nat.succ m) : (@fin.of_nat m n).val = n := by rw [fin.val_of_nat_eq_mod,mod_eq_of_lt h] def fin.foldl {n : ℕ} (f : α → β → β) (x : β) (a : fin n → α) : β := array.foldl (d_array.mk a) x f def fin.sum (n : ℕ) [has_add α] [has_zero α] (a : fin n → α) : α := fin.foldl has_add.add 0 a
aafc2403f8aef8daa862480fdb1ed5a9c8dd5c3d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/witt_vector/init_tail_auto.lean
f17679861956aa59cfab63993be6bf508fc1389f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,922
lean
/- Copyright (c) 2020 Johan Commelin and Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.witt_vector.basic import Mathlib.ring_theory.witt_vector.is_poly import Mathlib.PostPort universes u_1 namespace Mathlib /-! # `init` and `tail` Given a Witt vector `x`, we are sometimes interested in its components before and after an index `n`. This file defines those operations, proves that `init` is polynomial, and shows how that polynomial interacts with `mv_polynomial.bind₁`. ## Main declarations * `witt_vector.init n x`: the first `n` coefficients of `x`, as a Witt vector. All coefficients at indices ≥ `n` are 0. * `witt_vector.tail n x`: the complementary part to `init`. All coefficients at indices < `n` are 0, otherwise they are the same as in `x`. * `witt_vector.coeff_add_of_disjoint`: if `x` and `y` are Witt vectors such that for every `n` the `n`-th coefficient of `x` or of `y` is `0`, then the coefficients of `x + y` are just `x.coeff n + y.coeff n`. -/ namespace tactic namespace interactive /-- `init_ring` is an auxiliary tactic that discharges goals factoring `init` over ring operations. -/ end interactive end tactic namespace witt_vector /-- `witt_vector.select P x`, for a predicate `P : ℕ → Prop` is the Witt vector whose `n`-th coefficient is `x.coeff n` if `P n` is true, and `0` otherwise. -/ def select {p : ℕ} {R : Type u_1} [comm_ring R] (P : ℕ → Prop) (x : witt_vector p R) : witt_vector p R := mk p fun (n : ℕ) => ite (P n) (coeff x n) 0 /-- The polynomial that witnesses that `witt_vector.select` is a polynomial function. `select_poly n` is `X n` if `P n` holds, and `0` otherwise. -/ def select_poly (P : ℕ → Prop) (n : ℕ) : mv_polynomial ℕ ℤ := ite (P n) (mv_polynomial.X n) 0 theorem coeff_select {p : ℕ} {R : Type u_1} [comm_ring R] (P : ℕ → Prop) (x : witt_vector p R) (n : ℕ) : coeff (select P x) n = coe_fn (mv_polynomial.aeval (coeff x)) (select_poly P n) := sorry theorem select_is_poly {p : ℕ} (P : ℕ → Prop) : is_poly p fun (R : Type u_1) (_Rcr : comm_ring R) (x : witt_vector p R) => select P x := Exists.intro (select_poly P) (id fun (R : Type u_1) (_Rcr : comm_ring R) (x : witt_vector p R) => funext fun (i : ℕ) => coeff_select P x i) theorem select_add_select_not {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (P : ℕ → Prop) (x : witt_vector p R) : select P x + select (fun (i : ℕ) => ¬P i) x = x := sorry theorem coeff_add_of_disjoint {p : ℕ} [hp : fact (nat.prime p)] (n : ℕ) {R : Type u_1} [comm_ring R] (x : witt_vector p R) (y : witt_vector p R) (h : ∀ (n : ℕ), coeff x n = 0 ∨ coeff y n = 0) : coeff (x + y) n = coeff x n + coeff y n := sorry /-- `witt_vector.init n x` is the Witt vector of which the first `n` coefficients are those from `x` and all other coefficients are `0`. See `witt_vector.tail` for the complementary part. -/ def init {p : ℕ} {R : Type u_1} [comm_ring R] (n : ℕ) : witt_vector p R → witt_vector p R := select fun (i : ℕ) => i < n /-- `witt_vector.tail n x` is the Witt vector of which the first `n` coefficients are `0` and all other coefficients are those from `x`. See `witt_vector.init` for the complementary part. -/ def tail {p : ℕ} {R : Type u_1} [comm_ring R] (n : ℕ) : witt_vector p R → witt_vector p R := select fun (i : ℕ) => n ≤ i @[simp] theorem init_add_tail {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (x : witt_vector p R) (n : ℕ) : init n x + tail n x = x := sorry @[simp] theorem init_init {p : ℕ} {R : Type u_1} [comm_ring R] (x : witt_vector p R) (n : ℕ) : init n (init n x) = init n x := sorry theorem init_add {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (x : witt_vector p R) (y : witt_vector p R) (n : ℕ) : init n (x + y) = init n (init n x + init n y) := sorry theorem init_mul {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (x : witt_vector p R) (y : witt_vector p R) (n : ℕ) : init n (x * y) = init n (init n x * init n y) := sorry theorem init_neg {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (x : witt_vector p R) (n : ℕ) : init n (-x) = init n (-init n x) := sorry theorem init_sub {p : ℕ} [hp : fact (nat.prime p)] {R : Type u_1} [comm_ring R] (x : witt_vector p R) (y : witt_vector p R) (n : ℕ) : init n (x - y) = init n (init n x - init n y) := sorry /-- `witt_vector.init n x` is polynomial in the coefficients of `x`. -/ theorem init_is_poly (p : ℕ) (n : ℕ) : is_poly p fun (R : Type u_1) (_Rcr : comm_ring R) => init n := select_is_poly fun (i : ℕ) => i < n end Mathlib
bd30f7560202e1d7fe223d9d60e8b95d4c45d85e
a1179fa077c09acc49e4fbc8d67084ba89ac4f4c
/tutorials/src/my_exercises/00_first_proofs.lean
ef1704419ac9af8c464a1412404c7ff2b089f9e6
[]
no_license
Seeram/Lean-proof-assistant
11ca0ca0e0446bacdd1773c4c481a3653b2f1074
e672d46e0e5f39d8de2933ad4f4cac095ca6094f
refs/heads/master
1,682,754,224,366
1,620,959,431,000
1,620,959,431,000
299,000,950
0
1
null
1,620,680,462,000
1,601,200,258,000
Lean
UTF-8
Lean
false
false
45
lean
import data.real.basic import tactic.suggest
938590b23f4053c253c928a5fdc30f35eef1ab31
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/convex/topology.lean
347d9193a782acd61870db616fcded392aced704
[ "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
11,202
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import analysis.convex.jensen import analysis.normed_space.finite_dimension import topology.path_connected import topology.algebra.affine /-! # Topological and metric properties of convex sets We prove the following facts: * `convex.interior` : interior of a convex set is convex; * `convex.closure` : closure of a convex set is convex; * `set.finite.compact_convex_hull` : convex hull of a finite set is compact; * `set.finite.is_closed_convex_hull` : convex hull of a finite set is closed; * `convex_on_dist` : distance to a fixed point is convex on any convex set; * `convex_hull_ediam`, `convex_hull_diam` : convex hull of a set has the same (e)metric diameter as the original set; * `bounded_convex_hull` : convex hull of a set is bounded if and only if the original set is bounded. * `bounded_std_simplex`, `is_closed_std_simplex`, `compact_std_simplex`: topological properties of the standard simplex; -/ variables {ι : Type*} {E : Type*} open set open_locale pointwise lemma real.convex_iff_is_preconnected {s : set ℝ} : convex ℝ s ↔ is_preconnected s := convex_iff_ord_connected.trans is_preconnected_iff_ord_connected.symm alias real.convex_iff_is_preconnected ↔ convex.is_preconnected is_preconnected.convex /-! ### Standard simplex -/ section std_simplex variables [fintype ι] /-- Every vector in `std_simplex 𝕜 ι` has `max`-norm at most `1`. -/ lemma std_simplex_subset_closed_ball : std_simplex ℝ ι ⊆ metric.closed_ball 0 1 := begin assume f hf, rw [metric.mem_closed_ball, dist_zero_right], refine (nnreal.coe_one ▸ nnreal.coe_le_coe.2 $ finset.sup_le $ λ x hx, _), change |f x| ≤ 1, rw [abs_of_nonneg $ hf.1 x], exact (mem_Icc_of_mem_std_simplex hf x).2 end variable (ι) /-- `std_simplex ℝ ι` is bounded. -/ lemma bounded_std_simplex : metric.bounded (std_simplex ℝ ι) := (metric.bounded_iff_subset_ball 0).2 ⟨1, std_simplex_subset_closed_ball⟩ /-- `std_simplex ℝ ι` is closed. -/ lemma is_closed_std_simplex : is_closed (std_simplex ℝ ι) := (std_simplex_eq_inter ℝ ι).symm ▸ is_closed.inter (is_closed_Inter $ λ i, is_closed_le continuous_const (continuous_apply i)) (is_closed_eq (continuous_finset_sum _ $ λ x _, continuous_apply x) continuous_const) /-- `std_simplex ℝ ι` is compact. -/ lemma compact_std_simplex : is_compact (std_simplex ℝ ι) := metric.compact_iff_closed_bounded.2 ⟨is_closed_std_simplex ι, bounded_std_simplex ι⟩ end std_simplex /-! ### Topological vector space -/ section has_continuous_smul variables [add_comm_group E] [module ℝ E] [topological_space E] [topological_add_group E] [has_continuous_smul ℝ E] /-- In a topological vector space, the interior of a convex set is convex. -/ lemma convex.interior {s : set E} (hs : convex ℝ s) : convex ℝ (interior s) := convex_iff_pointwise_add_subset.mpr $ λ a b ha hb hab, have h : is_open (a • interior s + b • interior s), from or.elim (classical.em (a = 0)) (λ heq, have hne : b ≠ 0, by { rw [heq, zero_add] at hab, rw hab, exact one_ne_zero }, by { rw ← image_smul, exact (is_open_map_smul₀ hne _ is_open_interior).add_left } ) (λ hne, by { rw ← image_smul, exact (is_open_map_smul₀ hne _ is_open_interior).add_right }), (subset_interior_iff_subset_of_open h).mpr $ subset.trans (by { simp only [← image_smul], apply add_subset_add; exact image_subset _ interior_subset }) (convex_iff_pointwise_add_subset.mp hs ha hb hab) /-- In a topological vector space, the closure of a convex set is convex. -/ lemma convex.closure {s : set E} (hs : convex ℝ s) : convex ℝ (closure s) := λ x y hx hy a b ha hb hab, let f : E → E → E := λ x' y', a • x' + b • y' in have hf : continuous (λ p : E × E, f p.1 p.2), from (continuous_const.smul continuous_fst).add (continuous_const.smul continuous_snd), show f x y ∈ closure s, from mem_closure_of_continuous2 hf hx hy (λ x' hx' y' hy', subset_closure (hs hx' hy' ha hb hab)) /-- Convex hull of a finite set is compact. -/ lemma set.finite.compact_convex_hull {s : set E} (hs : finite s) : is_compact (convex_hull ℝ s) := begin rw [hs.convex_hull_eq_image], apply (compact_std_simplex _).image, haveI := hs.fintype, apply linear_map.continuous_on_pi end /-- Convex hull of a finite set is closed. -/ lemma set.finite.is_closed_convex_hull [t2_space E] {s : set E} (hs : finite s) : is_closed (convex_hull ℝ s) := hs.compact_convex_hull.is_closed /-- If `x ∈ s` and `y ∈ interior s`, then the segment `(x, y]` is included in `interior s`. -/ lemma convex.add_smul_sub_mem_interior {s : set E} (hs : convex ℝ s) {x y : E} (hx : x ∈ s) (hy : y ∈ interior s) {t : ℝ} (ht : t ∈ Ioc (0 : ℝ) 1) : x + t • (y - x) ∈ interior s := begin let f := λ z, x + t • (z - x), have : is_open_map f := (is_open_map_add_left _).comp ((is_open_map_smul (units.mk0 _ ht.1.ne')).comp (is_open_map_sub_right _)), apply mem_interior.2 ⟨f '' (interior s), _, this _ is_open_interior, mem_image_of_mem _ hy⟩, refine image_subset_iff.2 (λ z hz, _), exact hs.add_smul_sub_mem hx (interior_subset hz) ⟨ht.1.le, ht.2⟩, end /-- If `x ∈ s` and `x + y ∈ interior s`, then `x + t y ∈ interior s` for `t ∈ (0, 1]`. -/ lemma convex.add_smul_mem_interior {s : set E} (hs : convex ℝ s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ interior s) {t : ℝ} (ht : t ∈ Ioc (0 : ℝ) 1) : x + t • y ∈ interior s := by { convert hs.add_smul_sub_mem_interior hx hy ht, abel } open affine_map /-- If we dilate a convex set about a point in its interior by a scale `t > 1`, the interior of the result contains the original set. TODO Generalise this from convex sets to sets that are balanced / star-shaped about `x`. -/ lemma convex.subset_interior_image_homothety_of_one_lt {s : set E} (hs : convex ℝ s) {x : E} (hx : x ∈ interior s) (t : ℝ) (ht : 1 < t) : s ⊆ interior (image (homothety x t) s) := begin intros y hy, let I := { z | ∃ (u : ℝ), u ∈ Ioc (0 : ℝ) 1 ∧ z = y + u • (x - y) }, have hI : I ⊆ interior s, { rintros z ⟨u, hu, rfl⟩, exact hs.add_smul_sub_mem_interior hy hx hu, }, let z := homothety x t⁻¹ y, have hz₁ : z ∈ interior s, { suffices : z ∈ I, { exact hI this, }, use 1 - t⁻¹, split, { simp only [mem_Ioc, sub_le_self_iff, inv_nonneg, sub_pos, inv_lt_one ht, true_and], linarith, }, { simp only [z, homothety_apply, sub_smul, smul_sub, vsub_eq_sub, vadd_eq_add, one_smul], abel, }, }, have ht' : t ≠ 0, { linarith, }, have hz₂ : y = homothety x t z, { simp [z, ht', homothety_apply, smul_smul], }, rw hz₂, rw mem_interior at hz₁ ⊢, obtain ⟨U, hU₁, hU₂, hU₃⟩ := hz₁, exact ⟨image (homothety x t) U, image_subset ⇑(homothety x t) hU₁, homothety_is_open_map x t ht' U hU₂, mem_image_of_mem ⇑(homothety x t) hU₃⟩, end end has_continuous_smul /-! ### Normed vector space -/ section normed_space variables [normed_group E] [normed_space ℝ E] lemma convex_on_dist (z : E) (s : set E) (hs : convex ℝ s) : convex_on ℝ s (λz', dist z' z) := and.intro hs $ assume x y hx hy a b ha hb hab, calc dist (a • x + b • y) z = ∥ (a • x + b • y) - (a + b) • z ∥ : by rw [hab, one_smul, normed_group.dist_eq] ... = ∥a • (x - z) + b • (y - z)∥ : by rw [add_smul, smul_sub, smul_sub, sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg, neg_add, ←add_assoc, add_assoc (a • x), add_comm (b • y)]; simp only [add_assoc] ... ≤ ∥a • (x - z)∥ + ∥b • (y - z)∥ : norm_add_le (a • (x - z)) (b • (y - z)) ... = a * dist x z + b * dist y z : by simp [norm_smul, normed_group.dist_eq, real.norm_eq_abs, abs_of_nonneg ha, abs_of_nonneg hb] lemma convex_ball (a : E) (r : ℝ) : convex ℝ (metric.ball a r) := by simpa only [metric.ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_lt r lemma convex_closed_ball (a : E) (r : ℝ) : convex ℝ (metric.closed_ball a r) := by simpa only [metric.closed_ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_le r /-- Given a point `x` in the convex hull of `s` and a point `y`, there exists a point of `s` at distance at least `dist x y` from `y`. -/ lemma convex_hull_exists_dist_ge {s : set E} {x : E} (hx : x ∈ convex_hull ℝ s) (y : E) : ∃ x' ∈ s, dist x y ≤ dist x' y := (convex_on_dist y _ (convex_convex_hull ℝ _)).exists_ge_of_mem_convex_hull hx /-- Given a point `x` in the convex hull of `s` and a point `y` in the convex hull of `t`, there exist points `x' ∈ s` and `y' ∈ t` at distance at least `dist x y`. -/ lemma convex_hull_exists_dist_ge2 {s t : set E} {x y : E} (hx : x ∈ convex_hull ℝ s) (hy : y ∈ convex_hull ℝ t) : ∃ (x' ∈ s) (y' ∈ t), dist x y ≤ dist x' y' := begin rcases convex_hull_exists_dist_ge hx y with ⟨x', hx', Hx'⟩, rcases convex_hull_exists_dist_ge hy x' with ⟨y', hy', Hy'⟩, use [x', hx', y', hy'], exact le_trans Hx' (dist_comm y x' ▸ dist_comm y' x' ▸ Hy') end /-- Emetric diameter of the convex hull of a set `s` equals the emetric diameter of `s. -/ @[simp] lemma convex_hull_ediam (s : set E) : emetric.diam (convex_hull ℝ s) = emetric.diam s := begin refine (emetric.diam_le $ λ x hx y hy, _).antisymm (emetric.diam_mono $ subset_convex_hull ℝ s), rcases convex_hull_exists_dist_ge2 hx hy with ⟨x', hx', y', hy', H⟩, rw edist_dist, apply le_trans (ennreal.of_real_le_of_real H), rw ← edist_dist, exact emetric.edist_le_diam_of_mem hx' hy' end /-- Diameter of the convex hull of a set `s` equals the emetric diameter of `s. -/ @[simp] lemma convex_hull_diam (s : set E) : metric.diam (convex_hull ℝ s) = metric.diam s := by simp only [metric.diam, convex_hull_ediam] /-- Convex hull of `s` is bounded if and only if `s` is bounded. -/ @[simp] lemma bounded_convex_hull {s : set E} : metric.bounded (convex_hull ℝ s) ↔ metric.bounded s := by simp only [metric.bounded_iff_ediam_ne_top, convex_hull_ediam] lemma convex.is_path_connected {s : set E} (hconv : convex ℝ s) (hne : s.nonempty) : is_path_connected s := begin refine is_path_connected_iff.mpr ⟨hne, _⟩, intros x y x_in y_in, let f := λ θ : ℝ, x + θ • (y - x), have hf : continuous f, by continuity, have h₀ : f 0 = x, by simp [f], have h₁ : f 1 = y, by { dsimp [f], rw one_smul, abel }, have H := hconv.segment_subset x_in y_in, rw segment_eq_image' at H, exact joined_in.of_line hf.continuous_on h₀ h₁ H end @[priority 100] instance normed_space.path_connected : path_connected_space E := path_connected_space_iff_univ.mpr $ convex_univ.is_path_connected ⟨(0 : E), trivial⟩ @[priority 100] instance normed_space.loc_path_connected : loc_path_connected_space E := loc_path_connected_of_bases (λ x, metric.nhds_basis_ball) (λ x r r_pos, (convex_ball x r).is_path_connected $ by simp [r_pos]) end normed_space
d87ae124e825a5ada27af0a2cc42f16fda03c3c3
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/partial_explicit.lean
cf5b3d2d3389237ba8bd691ada733bcf7e1daa57
[ "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
494
lean
section variables a b c : nat variable H1 : a = b variable H2 : a + b + a + b = 0 example : b + b + a + b = 0 := @@eq.rec_on (λ x, x + b + a + b = 0) H1 H2 end section variables (f : Π {T : Type₁} {a : T} {P : T → Prop}, P a → Π {b : T} {Q : T → Prop}, Q b → Prop) variables (T : Type₁) (a : T) (P : T → Prop) (pa : P a) variables (b : T) (Q : T → Prop) (qb : Q b) check @f T a P pa b Q qb -- Prop check @@f P pa Q qb -- Prop check f pa qb -- Prop end
2c175e7f44299f17f71381ff404d92cf27e26b87
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/lazy_list2.lean
07f5943ead7f3397638d3be269ec80d5a354f49b
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
1,657
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Traversable instance for lazy_lists. -/ import category.traversable.equiv category.traversable.instances data.lazy_list namespace lazy_list open function def list_equiv_lazy_list (α : Type*) : list α ≃ lazy_list α := { to_fun := lazy_list.of_list, inv_fun := lazy_list.to_list, right_inv := by { intro, induction x, refl, simp! [*], ext, cases x, refl }, left_inv := by { intro, induction x, refl, simp! [*] } } universes u def thunk.mk {α} (x : α) : thunk α := λ _, x protected def traverse {m : Type u → Type u} [applicative m] {α β : Type u} (f : α → m β) : lazy_list α → m (lazy_list β) | lazy_list.nil := pure lazy_list.nil | (lazy_list.cons x xs):= lazy_list.cons <$> f x <*> (thunk.mk <$> traverse (xs ())) instance : traversable lazy_list := { map := @lazy_list.traverse id _, traverse := @lazy_list.traverse } instance : is_lawful_traversable lazy_list := begin apply equiv.is_lawful_traversable' list_equiv_lazy_list; intros ; resetI; ext, { induction x, refl, simp! [equiv.map,functor.map] at *, simp [*], refl, }, { induction x, refl, simp! [equiv.map,functor.map_const] at *, simp [*], refl, }, { induction x, { simp! [traversable.traverse,equiv.traverse] with functor_norm, refl }, simp! [equiv.map,functor.map_const,traversable.traverse] at *, rw x_ih, dsimp [list_equiv_lazy_list,equiv.traverse,to_list,traversable.traverse,list.traverse], simp! with functor_norm, refl }, end end lazy_list
c0fb9388b7dd2e0d73ea65bd101f5435c97623db
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/fixed_points.lean
9b84ca9632005d0b7487fbc176f011daeafa7946
[ "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
9,508
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, Kenny Lau, Yury Kudryashov -/ import order.preorder_hom import dynamics.fixed_points.basic /-! # Fixed point construction on complete lattices This file sets up the basic theory of fixed points of a monotone function in a complete lattice. ## Main definitions * `preorder_hom.lfp`: The least fixed point of a bundled monotone function. * `preorder_hom.gfp`: The greatest fixed point of a bundled monotone function. * `preorder_hom.prev_fixed`: The greatest fixed point of a bundled monotone function smaller than or equal to a given element. * `preorder_hom.next_fixed`: The least fixed point of a bundled monotone function greater than or equal to a given element. * `fixed_points.complete_lattice`: The Knaster-Tarski theorem: fixed points of a monotone self-map of a complete lattice form themselves a complete lattice. ## Tags fixed point, complete lattice, monotone function -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function (fixed_points is_fixed_pt) namespace preorder_hom section basic variables [complete_lattice α] (f : α →ₘ α) /-- Least fixed point of a monotone function -/ def lfp : (α →ₘ α) →ₘ α := { to_fun := λ f, Inf {a | f a ≤ a}, monotone' := λ f g hle, Inf_le_Inf $ λ a ha, (hle a).trans ha } /-- Greatest fixed point of a monotone function -/ def gfp : (α →ₘ α) →ₘ α := { to_fun := λ f, Sup {a | a ≤ f a}, monotone' := λ f g hle, Sup_le_Sup $ λ a ha, le_trans ha (hle a) } lemma lfp_le {a : α} (h : f a ≤ a) : lfp f ≤ a := Inf_le h lemma lfp_le_fixed {a : α} (h : f a = a) : lfp f ≤ a := f.lfp_le h.le lemma le_lfp {a : α} (h : ∀ b, f b ≤ b → a ≤ b) : a ≤ lfp f := le_Inf h lemma map_le_lfp {a : α} (ha : a ≤ f.lfp) : f a ≤ f.lfp := f.le_lfp $ λ b hb, (f.mono $ le_Inf_iff.1 ha _ hb).trans hb @[simp] lemma map_lfp : f (lfp f) = lfp f := have h : f (lfp f) ≤ lfp f, from f.map_le_lfp le_rfl, h.antisymm $ f.lfp_le $ f.mono h lemma is_fixed_pt_lfp : is_fixed_pt f f.lfp := f.map_lfp lemma lfp_le_map {a : α} (ha : lfp f ≤ a) : lfp f ≤ f a := calc lfp f = f (lfp f) : f.map_lfp.symm ... ≤ f a : f.mono ha lemma is_least_lfp_le : is_least {a | f a ≤ a} (lfp f) := ⟨f.map_lfp.le, λ a, f.lfp_le⟩ lemma is_least_lfp : is_least (fixed_points f) (lfp f) := ⟨f.is_fixed_pt_lfp, λ a, f.lfp_le_fixed⟩ lemma lfp_induction {p : α → Prop} (step : ∀ a, p a → a ≤ lfp f → p (f a)) (hSup : ∀ s, (∀ a ∈ s, p a) → p (Sup s)) : p (lfp f) := begin set s := {a | a ≤ lfp f ∧ p a}, specialize hSup s (λ a, and.right), suffices : Sup s = lfp f, from this ▸ hSup, have h : Sup s ≤ lfp f := Sup_le (λ b, and.left), have hmem : f (Sup s) ∈ s, from ⟨f.map_le_lfp h, step _ hSup h⟩, exact h.antisymm (f.lfp_le $ le_Sup hmem) end lemma le_gfp {a : α} (h : a ≤ f a) : a ≤ gfp f := le_Sup h lemma gfp_le {a : α} (h : ∀ b, b ≤ f b → b ≤ a) : gfp f ≤ a := Sup_le h lemma is_fixed_pt_gfp : is_fixed_pt f (gfp f) := f.dual.is_fixed_pt_lfp @[simp] lemma map_gfp : f (gfp f) = gfp f := f.dual.map_lfp lemma map_le_gfp {a : α} (ha : a ≤ gfp f) : f a ≤ gfp f := f.dual.lfp_le_map ha lemma gfp_le_map {a : α} (ha : gfp f ≤ a) : gfp f ≤ f a := f.dual.map_le_lfp ha lemma is_greatest_gfp_le : is_greatest {a | a ≤ f a} (gfp f) := f.dual.is_least_lfp_le lemma is_greatest_gfp : is_greatest (fixed_points f) (gfp f) := f.dual.is_least_lfp lemma gfp_induction {p : α → Prop} (step : ∀ a, p a → gfp f ≤ a → p (f a)) (hInf : ∀ s, (∀ a ∈ s, p a) → p (Inf s)) : p (gfp f) := f.dual.lfp_induction step hInf end basic section eqn variables [complete_lattice α] [complete_lattice β] (f : β →ₘ α) (g : α →ₘ β) -- Rolling rule lemma map_lfp_comp : f (lfp (g.comp f)) = lfp (f.comp g) := le_antisymm ((f.comp g).map_lfp ▸ f.mono (lfp_le_fixed _ $ congr_arg g (f.comp g).map_lfp)) $ lfp_le _ (congr_arg f (g.comp f).map_lfp).le lemma map_gfp_comp : f ((g.comp f).gfp) = (f.comp g).gfp := f.dual.map_lfp_comp g.dual -- Diagonal rule lemma lfp_lfp (h : α →ₘ α →ₘ α) : lfp (lfp.comp h) = lfp h.on_diag := begin let a := lfp (lfp.comp h), refine (lfp_le _ _).antisymm (lfp_le _ (eq.le _)), { exact lfp_le _ h.on_diag.map_lfp.le }, have ha : (lfp ∘ h) a = a := (lfp.comp h).map_lfp, calc h a a = h a (lfp (h a)) : congr_arg (h a) ha.symm ... = lfp (h a) : (h a).map_lfp ... = a : ha end lemma gfp_gfp (h : α →ₘ α →ₘ α) : gfp (gfp.comp h) = gfp h.on_diag := @lfp_lfp (order_dual α) _ $ (preorder_hom.dual_iso (order_dual α) (order_dual α)).symm.to_order_embedding.to_preorder_hom.comp h.dual end eqn section prev_next variables [complete_lattice α] (f : α →ₘ α) lemma gfp_const_inf_le (x : α) : gfp (const α x ⊓ f) ≤ x := gfp_le _ $ λ b hb, hb.trans inf_le_left /-- Previous fixed point of a monotone map. If `f` is a monotone self-map of a complete lattice and `x` is a point such that `f x ≤ x`, then `f.prev_fixed x hx` is the greatest fixed point of `f` that is less than or equal to `x`. -/ def prev_fixed (x : α) (hx : f x ≤ x) : fixed_points f := ⟨gfp (const α x ⊓ f), calc f (gfp (const α x ⊓ f)) = x ⊓ f (gfp (const α x ⊓ f)) : eq.symm $ inf_of_le_right $ (f.mono $ f.gfp_const_inf_le x).trans hx ... = gfp (const α x ⊓ f) : (const α x ⊓ f).map_gfp ⟩ /-- Next fixed point of a monotone map. If `f` is a monotone self-map of a complete lattice and `x` is a point such that `x ≤ f x`, then `f.next_fixed x hx` is the least fixed point of `f` that is greater than or equal to `x`. -/ def next_fixed (x : α) (hx : x ≤ f x) : fixed_points f := { val := (const α x ⊔ f).lfp, .. f.dual.prev_fixed x hx } lemma prev_fixed_le {x : α} (hx : f x ≤ x) : ↑(f.prev_fixed x hx) ≤ x := f.gfp_const_inf_le x lemma le_next_fixed {x : α} (hx : x ≤ f x) : x ≤ f.next_fixed x hx := f.dual.prev_fixed_le hx lemma next_fixed_le {x : α} (hx : x ≤ f x) {y : fixed_points f} (h : x ≤ y) : f.next_fixed x hx ≤ y := subtype.coe_le_coe.1 $ lfp_le _ $ sup_le h y.2.le @[simp] lemma next_fixed_le_iff {x : α} (hx : x ≤ f x) {y : fixed_points f} : f.next_fixed x hx ≤ y ↔ x ≤ y := ⟨λ h, (f.le_next_fixed hx).trans h, f.next_fixed_le hx⟩ @[simp] lemma le_prev_fixed_iff {x : α} (hx : f x ≤ x) {y : fixed_points f} : y ≤ f.prev_fixed x hx ↔ ↑y ≤ x := f.dual.next_fixed_le_iff hx lemma le_prev_fixed {x : α} (hx : f x ≤ x) {y : fixed_points f} (h : ↑y ≤ x) : y ≤ f.prev_fixed x hx := (f.le_prev_fixed_iff hx).2 h lemma le_map_sup_fixed_points (x y : fixed_points f) : (x ⊔ y : α) ≤ f (x ⊔ y) := calc (x ⊔ y : α) = f x ⊔ f y : congr_arg2 (⊔) x.2.symm y.2.symm ... ≤ f (x ⊔ y) : f.mono.le_map_sup x y lemma map_inf_fixed_points_le (x y : fixed_points f) : f (x ⊓ y) ≤ x ⊓ y := f.dual.le_map_sup_fixed_points x y lemma le_map_Sup_subset_fixed_points (A : set α) (hA : A ⊆ fixed_points f) : Sup A ≤ f (Sup A) := Sup_le $ λ x hx, hA hx ▸ (f.mono $ le_Sup hx) lemma map_Inf_subset_fixed_points_le (A : set α) (hA : A ⊆ fixed_points f) : f (Inf A) ≤ Inf A := le_Inf $ λ x hx, (hA hx) ▸ (f.mono $ Inf_le hx) end prev_next end preorder_hom namespace fixed_points open preorder_hom variables [complete_lattice α] (f : α →ₘ α) instance : semilattice_sup (fixed_points f) := { sup := λ x y, f.next_fixed (x ⊔ y) (f.le_map_sup_fixed_points x y), le_sup_left := λ x y, subtype.coe_le_coe.1 $ le_sup_left.trans (f.le_next_fixed _), le_sup_right := λ x y, subtype.coe_le_coe.1 $ le_sup_right.trans (f.le_next_fixed _), sup_le := λ x y z hxz hyz, f.next_fixed_le _ $ sup_le hxz hyz, .. subtype.partial_order _ } instance : semilattice_inf (fixed_points f) := { inf := λ x y, f.prev_fixed (x ⊓ y) (f.map_inf_fixed_points_le x y), .. subtype.partial_order _, .. (order_dual.semilattice_inf (fixed_points f.dual)) } instance : complete_semilattice_Sup (fixed_points f) := { Sup := λ s, f.next_fixed (Sup (coe '' s)) (f.le_map_Sup_subset_fixed_points (coe '' s) (λ z ⟨x, hx⟩, hx.2 ▸ x.2)), le_Sup := λ s x hx, subtype.coe_le_coe.1 $ le_trans (le_Sup $ set.mem_image_of_mem _ hx) (f.le_next_fixed _), Sup_le := λ s x hx, f.next_fixed_le _ $ Sup_le $ set.ball_image_iff.2 hx, .. subtype.partial_order _ } instance : complete_semilattice_Inf (fixed_points f) := { Inf := λ s, f.prev_fixed (Inf (coe '' s)) (f.map_Inf_subset_fixed_points_le (coe '' s) (λ z ⟨x, hx⟩, hx.2 ▸ x.2)), le_Inf := λ s x hx, f.le_prev_fixed _ $ le_Inf $ set.ball_image_iff.2 hx, Inf_le := λ s x hx, subtype.coe_le_coe.1 $ le_trans (f.prev_fixed_le _) (Inf_le $ set.mem_image_of_mem _ hx), .. subtype.partial_order _ } /-- **Knaster-Tarski Theorem**: The fixed points of `f` form a complete lattice. -/ instance : complete_lattice (fixed_points f) := { top := ⟨f.gfp, f.is_fixed_pt_gfp⟩, bot := ⟨f.lfp, f.is_fixed_pt_lfp⟩, le_top := λ x, f.le_gfp x.2.ge, bot_le := λ x, f.lfp_le x.2.le, .. subtype.partial_order _, .. fixed_points.semilattice_sup f, .. fixed_points.semilattice_inf f, .. fixed_points.complete_semilattice_Sup f, .. fixed_points.complete_semilattice_Inf f } end fixed_points
6a836362ecf61c08c14c2a1c1c975a54aa1bd9be
297c4ceafbbaed2a59b6215504d09e6bf201a2ee
/kruskal_final/lemmas.lean
943ad1eadb6320acc746a55a2746e9e5271b7a5d
[]
no_license
minchaowu/Kruskal.lean3
559c91b82033ce44ea61593adcec9cfff725c88d
a14516f47b21e636e9df914fc6ebe64cbe5cd38d
refs/heads/master
1,611,010,001,429
1,497,935,421,000
1,497,935,421,000
82,000,982
1
0
null
null
null
null
UTF-8
Lean
false
false
6,863
lean
import tools.super --.card open classical nat function set tactic namespace classical universe variable u variables {A : Type u} {p : A → Prop} local attribute [instance] prop_decidable theorem decidable.exists_not_of_not_forall [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] (h : ¬ ∀ x, p x) : ∃ x, ¬ p x := decidable.by_contradiction (assume h₁, h (take x, decidable.by_contradiction (assume hnpx, h₁ ⟨x, hnpx⟩))) -- theorem not_forall_of_exists_not (h : ∃ x, ¬ p x) : ¬ ∀ x, p x := -- assume h₁, match h with ⟨x, hnpx⟩ := hnpx (h₁ x) end theorem exists_not_of_not_forall (h : ¬ ∀ x, p x) : ∃ x, ¬ p x := decidable.exists_not_of_not_forall h end classical theorem ne_nil_desturct {A : Type} {l : list A} : l ≠ [] → ∃ p : A × list A, l = p.1::p.2 := list.rec_on l (λ H, absurd rfl H) (λ a l' IH H, ⟨(a,l'),rfl⟩) --(λ a l' IH H, ⟨a, ⟨l',rfl⟩⟩) theorem or_resolve_right {a b : Prop} (h₁ : a ∨ b) (h₂ : ¬a) : b := by super theorem or_resolve_left {a b : Prop} (h₁ : a ∨ b) (h₂ : ¬b) : a := by super theorem lt_of_le_pred' {n m : ℕ} : n ≤ pred m → n < m ∨ n = 0 := nat.rec_on m (λ h, or.elim (lt_or_eq_of_le h) (λ l, by super) (λ r, or.inr r)) (λ a ih h, or.inl (lt_succ_of_le h)) theorem lt_of_le_pred {n m : ℕ} : n ≤ pred m → n < m ∨ m = 0 := nat.rec_on m (λ h, or.inr rfl) (λ a ih h, or.inl (lt_succ_of_le h)) theorem lt_succ_iff_le (m n : nat) : m < succ n ↔ m ≤ n := iff.intro le_of_lt_succ lt_succ_of_le theorem eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := begin induction n, repeat { super } end theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k := by super with eq_zero_or_eq_succ_pred theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n := eq.symm (or_resolve_right (eq_zero_or_eq_succ_pred n) (ne.symm (ne_of_lt H))) section -- some facts about sets in the old library parameter {X : Type} theorem ext {a b : set X} (H : ∀x, x ∈ a ↔ x ∈ b) : a = b := funext (λ x, propext (H x)) theorem not_mem_empty (x : X) : ¬ (x ∈ (∅ : set X)) := λ H, H lemma ne_empty_of_mem {s : set X} {x : X} (h : x ∈ s) : s ≠ ∅ := begin intro hs, rewrite hs at h, apply not_mem_empty _ h end theorem eq_empty_of_forall_not_mem {s : set X} (H : ∀ x, x ∉ s) : s = ∅ := ext (λ x, iff.intro (λ xs, absurd xs (H x)) (λ xe, absurd xe (not_mem_empty _))) theorem exists_mem_of_ne_empty {s : set X} (H : s ≠ ∅) : ∃ x, x ∈ s := by_contradiction (λ H', H (eq_empty_of_forall_not_mem (forall_not_of_not_exists H'))) end theorem image_nonempty {A B : Type} {f : A → B} {S : set A} (H : S ≠ ∅) : image f S ≠ ∅ := have ∃ x, x ∈ S, from exists_mem_of_ne_empty H, let ⟨s,h⟩ := this in have f s ∈ image f S, from ⟨s, ⟨h,rfl⟩⟩, ne_empty_of_mem this section set variable {α : Type} theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := assume H : ∅ = univ, absurd (mem_univ (inhabited.default α)) (eq.rec_on H (not_mem_empty _)) theorem ne_empty_of_image_on_univ {A B : Type} (f : A → B) [inhabited A] : image f univ ≠ ∅ := have (∅ : set A) ≠ univ, from empty_ne_univ, have ∃ a, a ∈ (univ : set A), from exists_mem_of_ne_empty (ne.symm this), let ⟨a,h⟩ := this in have f a ∈ image f univ, from exists.intro a (and.intro h rfl), ne_empty_of_mem this end set section -- the least number principle. def complete_induction_on (n : ℕ) {p : ℕ → Prop} (h : ∀ n, (∀ m < n, p m) → p n) : p n := suffices ∀ n, ∀ m < n, p m, from this (succ n) _ (lt_succ_self n), take n, nat.rec_on n (take m, suppose m < 0, absurd this (not_lt_zero m)) (take n, assume ih : ∀ m < n, p m, take m, suppose m < succ n, or.elim (lt_or_eq_of_le (le_of_lt_succ this)) (ih m) (suppose m = n, begin rw this, apply h, exact ih end)) lemma wf_aux {A : set ℕ} (n : ℕ) : n ∈ A → ∃ a, a ∈ A ∧ ∀ b, b ∈ A → a ≤ b := @complete_induction_on n (λ x, x ∈ A → ∃ a, a ∈ A ∧ ∀ b, b ∈ A → a ≤ b) (λ k ih h, by_cases (suppose ∃ m, m ∈ A ∧ m <k, let ⟨m, Hmem, Hlt⟩ := this in ih m Hlt Hmem) (λ Hn, have ∀ m, m ∈ A → ¬ m < k, by super, ⟨k,h,(λ m h, le_of_not_gt $ this m h)⟩)) theorem wf_of_le (S : set ℕ) (H : S ≠ ∅) : ∃ a, a ∈ S ∧ ∀ b, b ∈ S → a ≤ b := let ⟨n,Hn⟩ := exists_mem_of_ne_empty H in wf_aux n Hn noncomputable definition least (S : set ℕ) (H : S ≠ ∅) : ℕ := some (wf_of_le S H) theorem least_is_mem (S : set ℕ) (H : S ≠ ∅) : least S H ∈ S := let ⟨bound, Ha⟩ := some_spec (wf_of_le S H) in bound theorem minimality {S : set ℕ} (neq : S ≠ ∅): ∀ x, x ∈ S → least S neq ≤ x := λ x Hx, let ⟨bound, Ha⟩ := some_spec (wf_of_le S neq) in Ha x Hx end -- lemma nonzero_card_of_finite {A : Type} {S : set A} (H : card S ≠ 0) : finite S := -- by_contradiction -- (suppose ¬ finite S, -- have card S = 0, from card_of_not_finite this, -- H this) -- lemma mem_not_in_diff {A : Type} {S : set A} {a : A} : a ∉ S \ insert a (∅ : set A) := -- assume h, -- have a ∉ insert a (∅ : set A), from not_mem_of_mem_diff h, -- this (mem_singleton a) -- lemma insert_of_diff_singleton {A : Type} {S : set A} {a : A} (H : a ∈ S) : insert a (S \ insert a (∅ : set A)) = S := -- begin -- apply eq_of_subset_of_subset, -- intros x h, apply or.elim h, intro h1, simp [H, h1], -- simp, -- intro hr, apply and.left hr, -- intros x h', cases (decidable.em (x ∈ insert a (∅ : set A))), -- apply or.inl, apply eq_of_mem_singleton, assumption, -- apply or.inr, apply and.intro, repeat {assumption} -- end -- lemma union_of_diff_singleton {A : Type} {S : set A} {a : A} (H : a ∈ S) -- : S \ (insert a (∅ : set A)) ∪ (insert a (∅ : set A)) = S := -- begin -- apply eq_of_subset_of_subset, -- intros x h, apply or.elim h, intro hl, apply and.left hl, -- intro hr, have h : x = a, from (mem_singleton_iff x a)^.mp hr, -- rewrite h, simp, -- intros x h', cases (decidable.em (x ∈ insert a (∅ : set A))), -- apply or.inr, simp, apply or.inl, apply and.intro, repeat {simp} -- end -- lemma finite_singleton {A : Type} {a : A} : finite '{a} := -- have carda : card '{a} = 1, from card_singleton a, -- have (1:ℕ) ≠ 0, from dec_trivial, -- have card '{a} ≠ 0, by+ rewrite -carda at this;exact this, -- nonzero_card_of_finite this -- lemma sub_of_eq {A : Type} {S T: set A} (H : S = T) : S ⊆ T := -- have T ⊆ T, from subset.refl T, -- by+ rewrite -H at this{1};exact this -- theorem ne_empty_of_mem' {X : Type} {s : set X} {x : X} (H : x ∈ s) : s ≠ ∅ := -- begin intro Hs, rewrite Hs at H, apply not_mem_empty _ H end --this is on github
37a5702bc7f3cae5a91efc866e6ae090ee803a73
5c47d5f6f51310e594c57486e051666894b008a1
/CategoryTheory/Category.lean
fe1ad23f4203e9c195d2265d7bee4cc63f4a712c
[ "Apache-2.0" ]
permissive
adrianmartir/category-theory-lean4
100ed364c07441b30d1483949152c39060563ccf
fa786c12114b7fad3f6c9e831d8ef2120a46bd05
refs/heads/main
1,687,620,544,571
1,626,958,787,000
1,626,958,787,000
372,252,151
1
0
null
null
null
null
UTF-8
Lean
false
false
8,944
lean
-- Some basic category-theoretic constructions namespace CategoryTheory structure HomStruct where obj : Type u hom : obj → obj → Type v class Category (C : HomStruct) where id' : (c : C.obj) → C.hom c c comp : {c d e : C.obj} → C.hom c d → C.hom d e → C.hom c e id_comp : {c d : C.obj} → (f : C.hom c d) → comp (id' c) f = f comp_id : {c d : C.obj} → (f : C.hom c d) → comp f (id' d) = f assoc : {a b c d : C.obj} → (f : C.hom a b) → (g : C.hom b c) → (h : C.hom c d) → comp (comp f g) h = comp f (comp g h) open HomStruct open Category notation:80 lhs " ∘ " rhs:81 => comp rhs lhs attribute [simp] id_comp comp_id assoc section Set universe u abbrev SmallCategory (C : HomStruct) := Category.{u,u} C abbrev LargeCategory (C : HomStruct) := Category.{u+1,u} C def Set : HomStruct := { obj := Type u, hom := fun a b => a → b } instance Set.Category : LargeCategory Set := { id' := fun a x => x comp := fun f g x => g (f x) id_comp := by intros simp comp_id := by intros simp assoc := by intros simp } end Set def HomStruct.opposite (C: HomStruct) : HomStruct := { obj := C.obj hom := fun f g => C.hom g f } notation:1030 arg "ᵒᵖ" => HomStruct.opposite arg instance Category.opposite (C: HomStruct) [Category C]: Category Cᵒᵖ := { id' := fun c => id' (C := C) c comp := fun f g => comp (C := C) g f id_comp := by intros simp comp_id := by intros simp assoc := by intros simp } -- Note that this doesn't allow for the usual trick where we -- replace `C` by `Cᵒᵖ` in a lemma and then reduce `Cᵒᵖᵒᵖ` to `C` -- since this is *an equality of `HomStruct`s* and that means that -- we can't construct a motive where the `HomStruct` `C` has the -- `[Category C]` instance. -- We would need bundled structures to actually use this trick. theorem opop (C: HomStruct) : Cᵒᵖᵒᵖ = C := by revert C intro { obj := obj, hom := hom} simp [HomStruct.opposite] attribute [simp] opop def inverses (C: HomStruct) [Category C] {c d: C.obj} (f: C.hom c d) (g: C.hom d c) := g ∘ f = id' c ∧ f ∘ g = id' d theorem inverse_unique (C: HomStruct) [Category C] {c d: C.obj} (f: C.hom c d) (g h: C.hom d c) : inverses C f g ∧ inverses C f h → g = h := by intro ⟨⟨_,p⟩,⟨q,_⟩⟩ have r : (h ∘ f) ∘ g = h := by rw [<- assoc, p, id_comp] rw [<- r, q, comp_id] def isomorphism (C: HomStruct) [Category C] {c d: C.obj} (f: C.hom c d) := exists (g: C.hom d c), inverses C f g namespace Function variable {A: Type _} {B: Type _} def inverses (f: A → B) (g: B → A) := (Function.comp g f = id) ∧ (Function.comp f g = id) theorem inverses_sym (f: A → B) (g: B → A) : inverses f g <-> inverses g f := by simp [inverses] exact ⟨ by intro ⟨p,q⟩ exact ⟨q,p⟩, by intro ⟨p,q⟩ exact ⟨q,p⟩ ⟩ def isomorphism (f: A → B) := exists (g: B → A), inverses f g end Function structure Functor (C D: HomStruct) [Category C] [Category D] where obj : C.obj → D.obj map : {c d : C.obj} → C.hom c d → D.hom (obj c) (obj d) map_id : {c : C.obj} → map (id' c) = id' (obj c) map_comp : {c d e : C.obj} → (f : C.hom c d) → (g : C.hom d e) → map (g ∘ f) = map g ∘ map f attribute [simp] Functor.map_id Functor.map_comp theorem Functor.ext {C D : HomStruct} [Category C] [Category D]: (F G : Functor C D) → (p: F.obj = G.obj) → (q: (fun c d (f: C.hom c d) => F.map f) ≅ (fun c d (f: C.hom c d) => G.map f)) → F = G := by intro { obj := F, map := FMap, ..} intro { obj := G, map := GMap, ..} intro p q simp at p q simp -- applies propext exact ⟨p,q⟩ def fId (C: HomStruct) [Category C] : Functor C C := { obj := id map := id map_id := by intros rfl map_comp := by intros rfl } def fComp {C D E: HomStruct} [Category C] [Category D] [Category E] (F: Functor C D) (G: Functor D E) : Functor C E := { obj := Function.comp G.obj F.obj map := fun f => G.map (F.map f) map_id := by intros simp map_comp := by intros simp } structure BundledCategory where base : HomStruct inst : Category base instance (C: BundledCategory) : Category C.base := C.inst def Cat : HomStruct.{max (u + 1) (v + 1), max u v} := { obj := BundledCategory.{u,v}, hom := fun C D => Functor C.base D.base } instance : Category Cat where id' := fun {C} => fId C.base comp := fComp id_comp := by intros simp [fComp, fId] apply Functor.ext simp [Function.comp] simp exact HEq.rfl comp_id := by intros simp [fComp, fId] apply Functor.ext simp [Function.comp] simp exact HEq.rfl assoc := by intros simp [fComp] apply Functor.ext simp [Function.comp] simp exact HEq.rfl def fully_faithful {C: HomStruct} [Category C] {D: HomStruct} [Category D] (F: Functor C D) := forall c d : C.obj, Function.isomorphism (F.map (c := c) (d := d)) section NatTrans variable {C : HomStruct} [Category C] variable {D : HomStruct} [Category D] structure NatTrans (F G : Functor C D) := app : (c : C.obj) → D.hom (F.obj c) (G.obj c) naturality : {c d : C.obj} → (f : C.hom c d) → app d ∘ (F.map f) = (G.map f) ∘ app c open NatTrans theorem NatTrans.ext {F G : Functor C D} : (η μ : NatTrans F G) → (p: η.app = μ.app) → (η = μ) := by intro { app := η, .. } intro { app := μ, .. } intro p simp at p simp -- applies propext exact p def FunctorCat (C: HomStruct) [Category C] (D: HomStruct) [Category D]: HomStruct := { obj := Functor C D hom := NatTrans } def idTrans (F : Functor C D) : NatTrans F F := { app := fun c => id' (Functor.obj F c), naturality := by intro c d f simp } def vComp {F G H : Functor C D} (η : NatTrans F G) (μ : NatTrans G H) : NatTrans F H := { app := fun c => (app μ c) ∘ (app η c), naturality := by intro c d f simp rw [<- naturality μ f] rw [<- Category.assoc] rw [naturality η f] rw [Category.assoc] } instance : Category (FunctorCat C D) := { id' := idTrans, comp := vComp, id_comp := by intros apply NatTrans.ext simp [id', idTrans, comp, vComp] comp_id := by intros apply NatTrans.ext simp [id', idTrans, comp, vComp] assoc := by intros apply NatTrans.ext simp [comp, vComp] } end NatTrans section Yoneda universes u v variable {C : HomStruct.{u,v}} [Category C] def yObj (c: C.obj) : (Functor Cᵒᵖ Set.{v}) := { obj := fun d => C.hom d c -- f gets sent to precomposition with f map := fun f g => g ∘ f map_id := by intros simp [id'] map_comp := by intros simp [comp] } def yMap {c d: C.obj} (f: C.hom c d) : NatTrans (yObj c) (yObj d) := { app := fun c g => f ∘ g naturality := by intros simp [yObj, comp] } def y : Functor C (FunctorCat Cᵒᵖ Set.{v}) := { obj := yObj map := yMap map_id := by intros apply NatTrans.ext simp [yMap, id', idTrans] map_comp := by intros apply NatTrans.ext simp [yMap, comp, vComp] } def yonedaMap (c : C.obj) (F: Functor Cᵒᵖ Set.{v}) (η: NatTrans (y.obj c) F) : F.obj c := η.app c (id' c) def yonedaMapInv (c : C.obj) (F: Functor Cᵒᵖ Set.{v}) (x: F.obj c) : NatTrans (y.obj c) F := { app := fun d f => F.map f x naturality := by intros d e f simp [y, yObj, comp] funext g -- It honestly is a bit confusing not knowing in which category -- the composition takes place have p: comp (C := C) f g = comp (C := Cᵒᵖ) g f := by rfl rw [p, Functor.map_comp] simp [comp] } theorem yoneda (c : C.obj) (F: Functor Cᵒᵖ Set.{v}) : Function.inverses (yonedaMap c F) (yonedaMapInv c F) := ⟨ by simp [yonedaMap, yonedaMapInv] apply funext intro { app := η, naturality := nat } simp [comp,id',y,yObj] funext d f -- Rewrite the application in the goal as a composition in order -- to apply naturality have p: F.map f (η c (id' c)) = (comp (η c) (F.map f)) (id' c) := by rfl rw [p, <- nat f] simp [y, yObj, comp], by simp [yonedaMap, yonedaMapInv, Function.comp] funext x have p: id' (C := C) c = id' (C := Cᵒᵖ) c := by rfl rw [p, Functor.map_id] simp [id'] ⟩ theorem y_fully_faithful: fully_faithful (y (C := C)) := by simp [fully_faithful, Function.isomorphism] intros c d -- The crucial insight is that `y.map` is in fact equal to `yonedaMapInverse` and thus by the yoneda lemma has an inverse have yoneda := yoneda c (y.obj d) refine ⟨ yonedaMap c (y.obj d), ?_ ⟩ have p: yonedaMapInv c (Functor.obj y d) = y.map := by funext f apply NatTrans.ext simp [yonedaMapInv, y, yMap, yObj] rw [<- p, Function.inverses_sym] exact yoneda end Yoneda end CategoryTheory
cf92b7a6d1b40be31f694a3674609f3bdeb206e6
a6b711a4e8db20755026231f7ed529a9014b2b6d
/ZZ_IGNORE/CUTS/set-stuff.cuts.lean
200d4c88a796f62a27fa199937b21ff7a4bb6bc5
[]
no_license
chaseboettner/cs-dm-1
b67d4a7e86f56bce59d2af115503769749d423b2
80b35f2957ffaa45b8b7a4479a3570a2d6eb4db0
refs/heads/master
1,585,367,603,488
1,536,235,675,000
1,536,235,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,444
lean
/- TUPLE TYPES -/ /- A tuple is an ordered list of values of specified types. Mathematicians generally write tuples as comma separated, unnamed "field" values enclosed in parenthesis. Given that tt and ff are values of type boolean, we can thus define values that are 2-tuples, or pairs, of boolean values. These pair values include (tt, tt) and (tt, ff). -/ /- EXERCISE: How many such pairs are there? What are the missing pairs? -/ /- Here's a piece of Lean code that defines TT as a variable that has as its value the tuple (tt, tt). -/ def TT := (tt, ff) /- The type of a tuple is said to be the "product type" of the given field types. -/ /- The type of the tuple if the "product" of the types of its field values. For example the type of TT = (tt, tt), is the "product type" of boolean and boolean, which we write as boolean × boolean. Check it! -/ #check TT /- Note: You get the × symbol in Lean VS Code by typing \times. Try it! -/ /- SETS -/ /- A set is understood intuitively as simply a collection of values. A value is either in or not in a given set. A value can be in many different sets. For example, the natural number, 2, is in the set of even natural numbers and it is also in the set of prime natural numbers. (By contrast, in the theory of types, a value has exactly one type.) Here are two sets, to which we give names, illustrating the possibility for a given value to be in multiple sets. small_primes := { 2, 3, 5, 7, 11 } small_evens := { 2, 4, 6, 8, 10 } -/ /- FUNCTIONS -/ /- A function is properly understood mathematically as a set of tuples, and in particular as a set of tuples that satisfies a major constraint: there is at most one tuple in the set with any given value in the first, or argument, position. Here's an example of a set of tuples for one particular function: { (T, T), (F, F) }. This is what mathematicians and computer scientists call the "identity function" on booleans. It associates each (argument) value, of type boolean, with itself. Here is another example, usually called the "negation" function on booleans: { (T, F), (F, T) }. It associates each value of type boolean with the other value of type boolean: i.e., it associates true with false and false with true. EXERCISE: We have claimed that this set defines a function. That means that it better be single-valued. Study the set and convince yourself, and ideally someone else, that the set of tuples is indeed single valued. -/ /- The types of values appearing in the tuples of a function tells us the type of the function in question. In the preceeding examples both the argument and result types are boolean. More generally, tf the type of argument values is some type, let's just call it S, and the type of the result values is some type, let's call it T, then we say that the type of the function is S → T, which we can pronounce as "S to T." EXERCISE: What is the type of the identify function on booleans? EXERCISE: What is the type of the negation function on booleans? EXERCISE: How many such functions are there from boolean to boolean? -/ /- When there is at most one tuple with any given value of the argument type, then there is at most one result value corresponding to any given argument value. We say that functions are "single-valued". That is, if you are given an argument value, there is at most one corresponding result value. If *for every* value of the argument type there is a tuple defining a corresponding result value, then we say the function is "total". If there is not a tuple for each value of the argument type, we say that the function is partial. EXERCISE: Think of a function from real numbers to real numbers that is partial and explain why it is partial. -/ /- If a set of tuples of a given type has multiple tuples with the same argument value but different result values, then set constitutes what we call a "relation." Not being single-valued, however, it does not consitute a function. EXERCISE: Which of the following relations are and are not functions? 1. { (T, T) } 2. { (T, F), (F, T), (F, F) } 3. { (T, F), (F, T) } -/ /- "FUNCTIONS" --- PROGRAMS THAT COMPUTE FUNCTIONS -/ /- In practical mathematics and in computer science, we often want to "compute functions". For the time being, we will restrict ourselves to discussing total functions. What this means is that, when given the first element of a tuple in the set of tuples for a given function, we want to have a mechanical (even mechanized!) way to determine the corresponding second element. For example, we might want a machine that, when given a natural number, n, computes and returns its square, n^2. Given an argument value, such as 3, for example, such a machine would determine that the result is 9, thereby identifying the tuple, (3, 9), as the one and only tuple in the set of tuples with argument value, 3. We can view a machine that can compute a function in this sense as being a kind of "implicit" representation of the function. It does not explicitly enumerate a set of tuples, but given any value of the argument type, it will produce the corresponding result value, thus producing the corresponding tuple in the set of types constituting the function in question. The machines we are talking about are of course just "programs." Computer scientists routinely "abuse terminology" by referring to such programs as "functions." -/
f8ff45b64cc4e03936c315a02bd9b98829b98f0d
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Compiler/ConstFolding.lean
8fd31aab8f9ee1784f7df8eab502432bf58f3ef7
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,087
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Expr import Lean.Compiler.Util /- Constant folding for primitives that have special runtime support. -/ namespace Lean.Compiler def BinFoldFn := Bool → Expr → Expr → Option Expr def UnFoldFn := Bool → Expr → Option Expr def mkUIntTypeName (nbytes : Nat) : Name := Name.mkSimple ("UInt" ++ toString nbytes) structure NumScalarTypeInfo := (nbits : Nat) (id : Name := mkUIntTypeName nbits) (ofNatFn : Name := Name.mkStr id "ofNat") (toNatFn : Name := Name.mkStr id "toNat") (size : Nat := 2^nbits) def numScalarTypes : List NumScalarTypeInfo := [{nbits := 8}, {nbits := 16}, {nbits := 32}, {nbits := 64}, {id := `USize, nbits := System.Platform.numBits}] def isOfNat (fn : Name) : Bool := numScalarTypes.any (fun info => info.ofNatFn == fn) def isToNat (fn : Name) : Bool := numScalarTypes.any (fun info => info.toNatFn == fn) def getInfoFromFn (fn : Name) : List NumScalarTypeInfo → Option NumScalarTypeInfo | [] => none | info::infos => if info.ofNatFn == fn then some info else getInfoFromFn fn infos def getInfoFromVal : Expr → Option NumScalarTypeInfo | Expr.app (Expr.const fn _ _) _ _ => getInfoFromFn fn numScalarTypes | _ => none @[export lean_get_num_lit] def getNumLit : Expr → Option Nat | Expr.lit (Literal.natVal n) _ => some n | Expr.app (Expr.const fn _ _) a _ => if isOfNat fn then getNumLit a else none | _ => none def mkUIntLit (info : NumScalarTypeInfo) (n : Nat) : Expr := mkApp (mkConst info.ofNatFn) (mkNatLit (n%info.size)) def mkUInt32Lit (n : Nat) : Expr := mkUIntLit {nbits := 32} n def foldBinUInt (fn : NumScalarTypeInfo → Bool → Nat → Nat → Nat) (beforeErasure : Bool) (a₁ a₂ : Expr) : Option Expr := do let n₁ ← getNumLit a₁ let n₂ ← getNumLit a₂ let info ← getInfoFromVal a₁ pure $ mkUIntLit info (fn info beforeErasure n₁ n₂) def foldUIntAdd := foldBinUInt $ fun _ _ => Add.add def foldUIntMul := foldBinUInt $ fun _ _ => Mul.mul def foldUIntDiv := foldBinUInt $ fun _ _ => Div.div def foldUIntMod := foldBinUInt $ fun _ _ => Mod.mod def foldUIntSub := foldBinUInt $ fun info _ a b => (a + (info.size - b)) def preUIntBinFoldFns : List (Name × BinFoldFn) := [(`add, foldUIntAdd), (`mul, foldUIntMul), (`div, foldUIntDiv), (`mod, foldUIntMod), (`sub, foldUIntSub)] def uintBinFoldFns : List (Name × BinFoldFn) := numScalarTypes.foldl (fun r info => r ++ (preUIntBinFoldFns.map (fun ⟨suffix, fn⟩ => (info.id ++ suffix, fn)))) [] def foldNatBinOp (fn : Nat → Nat → Nat) (a₁ a₂ : Expr) : Option Expr := do let n₁ ← getNumLit a₁ let n₂ ← getNumLit a₂ pure $ mkNatLit (fn n₁ n₂) def foldNatAdd (_ : Bool) := foldNatBinOp Add.add def foldNatMul (_ : Bool) := foldNatBinOp Mul.mul def foldNatDiv (_ : Bool) := foldNatBinOp Div.div def foldNatMod (_ : Bool) := foldNatBinOp Mod.mod -- TODO: add option for controlling the limit def natPowThreshold := 256 def foldNatPow (_ : Bool) (a₁ a₂ : Expr) : Option Expr := do let n₁ ← getNumLit a₁ let n₂ ← getNumLit a₂ if n₂ < natPowThreshold then pure $ mkNatLit (n₁ ^ n₂) else none def mkNatEq (a b : Expr) : Expr := mkAppN (mkConst `Eq [levelOne]) #[(mkConst `Nat), a, b] def mkNatLt (a b : Expr) : Expr := mkAppN (mkConst `HasLess.Less [levelZero]) #[mkConst `Nat, mkConst `Nat.less, a, b] def mkNatLe (a b : Expr) : Expr := mkAppN (mkConst `HasLessEq.LessEq [levelZero]) #[mkConst `Nat, mkConst `Nat.lessEq, a, b] def toDecidableExpr (beforeErasure : Bool) (pred : Expr) (r : Bool) : Expr := match beforeErasure, r with | false, true => mkDecIsTrue neutralExpr neutralExpr | false, false => mkDecIsFalse neutralExpr neutralExpr | true, true => mkDecIsTrue pred (mkLcProof pred) | true, false => mkDecIsFalse pred (mkLcProof pred) def foldNatBinPred (mkPred : Expr → Expr → Expr) (fn : Nat → Nat → Bool) (beforeErasure : Bool) (a₁ a₂ : Expr) : Option Expr := do let n₁ ← getNumLit a₁ let n₂ ← getNumLit a₂ pure $ toDecidableExpr beforeErasure (mkPred a₁ a₂) (fn n₁ n₂) def foldNatDecEq := foldNatBinPred mkNatEq (fun a b => a = b) def foldNatDecLt := foldNatBinPred mkNatLt (fun a b => a < b) def foldNatDecLe := foldNatBinPred mkNatLe (fun a b => a ≤ b) def natFoldFns : List (Name × BinFoldFn) := [(`Nat.add, foldNatAdd), (`Nat.mul, foldNatMul), (`Nat.div, foldNatDiv), (`Nat.mod, foldNatMod), (`Nat.pow, foldNatPow), (`Nat.pow._main, foldNatPow), (`Nat.decEq, foldNatDecEq), (`Nat.decLt, foldNatDecLt), (`Nat.decLe, foldNatDecLe)] def getBoolLit : Expr → Option Bool | Expr.const `Bool.true _ _ => some true | Expr.const `Bool.false _ _ => some false | _ => none def foldStrictAnd (_ : Bool) (a₁ a₂ : Expr) : Option Expr := let v₁ := getBoolLit a₁ let v₂ := getBoolLit a₂ match v₁, v₂ with | some true, _ => a₂ | some false, _ => a₁ | _, some true => a₁ | _, some false => a₂ | _, _ => none def foldStrictOr (_ : Bool) (a₁ a₂ : Expr) : Option Expr := let v₁ := getBoolLit a₁ let v₂ := getBoolLit a₂ match v₁, v₂ with | some true, _ => a₁ | some false, _ => a₂ | _, some true => a₂ | _, some false => a₁ | _, _ => none def boolFoldFns : List (Name × BinFoldFn) := [(`strictOr, foldStrictOr), (`strictAnd, foldStrictAnd)] def binFoldFns : List (Name × BinFoldFn) := boolFoldFns ++ uintBinFoldFns ++ natFoldFns def foldNatSucc (_ : Bool) (a : Expr) : Option Expr := do let n ← getNumLit a pure $ mkNatLit (n+1) def foldCharOfNat (beforeErasure : Bool) (a : Expr) : Option Expr := do guard (!beforeErasure) let n ← getNumLit a pure $ if isValidChar n.toUInt32 then mkUInt32Lit n else mkUInt32Lit 0 def foldToNat (_ : Bool) (a : Expr) : Option Expr := do let n ← getNumLit a pure $ mkNatLit n def uintFoldToNatFns : List (Name × UnFoldFn) := numScalarTypes.foldl (fun r info => (info.toNatFn, foldToNat) :: r) [] def unFoldFns : List (Name × UnFoldFn) := [(`Nat.succ, foldNatSucc), (`Char.ofNat, foldCharOfNat)] ++ uintFoldToNatFns def findBinFoldFn (fn : Name) : Option BinFoldFn := binFoldFns.lookup fn def findUnFoldFn (fn : Name) : Option UnFoldFn := unFoldFns.lookup fn @[export lean_fold_bin_op] def foldBinOp (beforeErasure : Bool) (f : Expr) (a : Expr) (b : Expr) : Option Expr := do match f with | Expr.const fn _ _ => let foldFn ← findBinFoldFn fn foldFn beforeErasure a b | _ => none @[export lean_fold_un_op] def foldUnOp (beforeErasure : Bool) (f : Expr) (a : Expr) : Option Expr := do match f with | Expr.const fn _ _ => let foldFn ← findUnFoldFn fn foldFn beforeErasure a | _ => none end Lean.Compiler
31e8bfcf9e111a2bd633f6c1792965105544257e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1335.lean
37f5bb581bdfe3797eb34c2700fb953552a59a55
[ "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
611
lean
namespace int private lemma sub_nat_nat_elim (m n : ℕ) (P : ℕ → ℕ → ℤ → Prop) (hp : ∀i n, P (n + i) n (of_nat i)) (hn : ∀i m, P m (m + i + 1) (-[1+ i])) : P m n (sub_nat_nat m n) := sorry inductive rel_int_nat_nat__ : ℤ → ℕ × ℕ → Prop | pos : ∀m p, rel_int_nat_nat__ (of_nat p) (m + p, m) | neg : ∀m n, rel_int_nat_nat__ (neg_succ_of_nat n) (m, m + n) lemma rel_sub_nat_nat__ {a b : ℕ} : rel_int_nat_nat__ (sub_nat_nat a b) (a, b) := /- The next statement kills lean -/ sub_nat_nat_elim a b (λ(a b : ℕ) (i : ℤ), rel_int_nat_nat__ i (a, b)) sorry sorry end int
e61f5b9d6b2adea1830501a11799423fb5c8df7f
1dd482be3f611941db7801003235dc84147ec60a
/src/analysis/specific_limits.lean
95bab1f672fdd00fdac148c4999a58aa3bf22f90
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
11,998
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 A collection of specific limit computations. -/ import algebra.big_operators algebra.group_power tactic.norm_num topology.instances.ennreal topology.algebra.infinite_sum noncomputable theory open classical finset function filter metric local attribute [instance] prop_decidable section real lemma has_sum_of_absolute_convergence {f : ℕ → ℝ} (hf : ∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) : has_sum f := let f' := λs:finset ℕ, s.sum (λi, abs (f i)) in suffices cauchy (map (λs:finset ℕ, s.sum f) at_top), from complete_space.complete this, cauchy_iff.mpr $ and.intro (map_ne_bot at_top_ne_bot) $ assume s hs, let ⟨ε, hε, hsε⟩ := mem_uniformity_dist.mp hs, ⟨r, hr⟩ := hf in have hε' : {p : ℝ × ℝ | dist p.1 p.2 < ε / 2} ∈ (@uniformity ℝ _).sets, from mem_uniformity_dist.mpr ⟨ε / 2, div_pos_of_pos_of_pos hε two_pos, assume a b h, h⟩, have cauchy (at_top.map $ λn, f' (range n)), from cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hr, have ∃n, ∀{n'}, n ≤ n' → dist (f' (range n)) (f' (range n')) < ε / 2, by simp [cauchy_iff, mem_at_top_sets] at this; from let ⟨t, ⟨u, hu⟩, ht⟩ := this _ hε' in ⟨u, assume n' hn, ht $ set.prod_mk_mem_set_prod_eq.mpr ⟨hu _ (le_refl _), hu _ hn⟩⟩, let ⟨n, hn⟩ := this in have ∀{s}, range n ⊆ s → abs ((s \ range n).sum f) < ε / 2, from assume s hs, let ⟨n', hn'⟩ := @exists_nat_subset_range s in have range n ⊆ range n', from finset.subset.trans hs hn', have f'_nn : 0 ≤ f' (range n' \ range n), from zero_le_sum $ assume _ _, abs_nonneg _, calc abs ((s \ range n).sum f) ≤ f' (s \ range n) : abs_sum_le_sum_abs ... ≤ f' (range n' \ range n) : sum_le_sum_of_subset_of_nonneg (finset.sdiff_subset_sdiff hn' (finset.subset.refl _)) (assume _ _ _, abs_nonneg _) ... = abs (f' (range n' \ range n)) : (abs_of_nonneg f'_nn).symm ... = abs (f' (range n') - f' (range n)) : by simp [f', (sum_sdiff ‹range n ⊆ range n'›).symm] ... = abs (f' (range n) - f' (range n')) : abs_sub _ _ ... < ε / 2 : hn $ range_subset.mp this, have ∀{s t}, range n ⊆ s → range n ⊆ t → dist (s.sum f) (t.sum f) < ε, from assume s t hs ht, calc abs (s.sum f - t.sum f) = abs ((s \ range n).sum f + - (t \ range n).sum f) : by rw [←sum_sdiff hs, ←sum_sdiff ht]; simp ... ≤ abs ((s \ range n).sum f) + abs ((t \ range n).sum f) : le_trans (abs_add_le_abs_add_abs _ _) $ by rw [abs_neg]; exact le_refl _ ... < ε / 2 + ε / 2 : add_lt_add (this hs) (this ht) ... = ε : by rw [←add_div, add_self_div_two], ⟨(λs:finset ℕ, s.sum f) '' {s | range n ⊆ s}, image_mem_map $ mem_at_top (range n), assume ⟨a, b⟩ ⟨⟨t, ht, ha⟩, ⟨s, hs, hb⟩⟩, by simp at ha hb; exact ha ▸ hb ▸ hsε (this ht hs)⟩ lemma is_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} {r : ℝ} (hf : ∀n, 0 ≤ f n) : is_sum f r ↔ tendsto (λn, (range n).sum f) at_top (nhds r) := ⟨tendsto_sum_nat_of_is_sum, assume hr, have tendsto (λn, (range n).sum (λn, abs (f n))) at_top (nhds r), by simp [(λi, abs_of_nonneg (hf i)), hr], let ⟨p, h⟩ := has_sum_of_absolute_convergence ⟨r, this⟩ in have hp : tendsto (λn, (range n).sum f) at_top (nhds p), from tendsto_sum_nat_of_is_sum h, have p = r, from tendsto_nhds_unique at_top_ne_bot hp hr, this ▸ h⟩ end real lemma mul_add_one_le_pow {r : ℝ} (hr : 0 ≤ r) : ∀{n:ℕ}, (n:ℝ) * r + 1 ≤ (r + 1) ^ n | 0 := by simp; exact le_refl 1 | (n + 1) := let h : (n:ℝ) ≥ 0 := nat.cast_nonneg n in calc ↑(n + 1) * r + 1 ≤ ((n + 1) * r + 1) + r * r * n : le_add_of_le_of_nonneg (le_refl _) (mul_nonneg (mul_nonneg hr hr) h) ... = (r + 1) * (n * r + 1) : by simp [mul_add, add_mul, mul_comm, mul_assoc] ... ≤ (r + 1) * (r + 1) ^ n : mul_le_mul (le_refl _) mul_add_one_le_pow (add_nonneg (mul_nonneg h hr) zero_le_one) (add_nonneg hr zero_le_one) lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ n * (r - 1) + 1 : le_add_of_le_of_nonneg (le_refl _) zero_le_one ... ≤ ((r - 1) + 1) ^ n : mul_add_one_le_pow $ le_of_lt this ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma map_succ_at_top_eq : map nat.succ at_top = at_top := le_antisymm (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b, assume c hc, hb (c + 1) $ le_trans hc $ nat.le_succ _⟩) (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b + 1, assume c, match c with | 0 := assume h, have 0 > 0, from lt_of_lt_of_le (lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one) h, (lt_irrefl 0 this).elim | (c+1) := assume h, hb _ (nat.le_of_succ_le_succ h) end⟩) lemma tendsto_comp_succ_at_top_iff {α : Type*} {f : ℕ → α} {x : filter α} : tendsto (λn, f (nat.succ n)) at_top x ↔ tendsto f at_top x := calc tendsto (f ∘ nat.succ) at_top x ↔ tendsto f (map nat.succ at_top) x : by simp [tendsto, filter.map_map] ... ↔ _ : by rw [map_succ_at_top_eq] lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, tendsto_comp_succ_at_top_iff.mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ by simp *) lemma tendsto_coe_iff {f : ℕ → ℕ} : tendsto (λ n, (f n : ℝ)) at_top at_top ↔ tendsto f at_top at_top := ⟨ λ h, tendsto_infi.2 $ λ i, tendsto_principal.2 (have _, from tendsto_infi.1 h i, by simpa using tendsto_principal.1 this), λ h, tendsto.comp h tendsto_of_nat_at_top_at_top ⟩ lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) := tendsto.comp (tendsto_coe_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0 lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) := by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (nhds 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (nhds 0), by simpa, tendsto_comp_succ_at_top_iff.2 tendsto_one_div_at_top_nhds_0_nat lemma sum_geometric' {r : ℝ} (h : r ≠ 0) : ∀{n}, (finset.range n).sum (λi, (r + 1) ^ i) = ((r + 1) ^ n - 1) / r | 0 := by simp [zero_div] | (n+1) := by simp [@sum_geometric' n, h, pow_succ, range_succ, add_div_eq_mul_add_div, add_mul, mul_comm, mul_assoc] lemma sum_geometric {r : ℝ} {n : ℕ} (h : r ≠ 1) : (range n).sum (λi, r ^ i) = (r ^ n - 1) / (r - 1) := calc (range n).sum (λi, r ^ i) = (range n).sum (λi, ((r - 1) + 1) ^ i) : by simp ... = (((r - 1) + 1) ^ n - 1) / (r - 1) : sum_geometric' $ by simp [sub_eq_iff_eq_add, -sub_eq_add_neg, h] ... = (r ^ n - 1) / (r - 1) : by simp lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg $ pow_nonneg h₁).mpr $ by simp [neg_inv, sum_geometric, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_sum_geometric_two _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, let g : ℕ → ℝ := λ n, option.cases_on (encodable.decode2 ι n) 0 (f ∘ encodable.encode), have : ∀ n, g n = 0 ∨ g n = f n, { intro n, dsimp [g], cases e : encodable.decode2 ι n with a, { exact or.inl rfl }, { simp [encodable.mem_decode2.1 e] } }, cases has_sum_of_has_sum_of_sub ⟨_, hf⟩ this with c hg, have cε : c ≤ ε, { refine is_sum_le (λ n, _) hg hf, cases this n; rw h, exact le_of_lt (f0 _) }, have hs : ∀ n, g n ≠ 0 → (encodable.decode2 ι n).is_some, { intros n h, dsimp [g] at h, cases encodable.decode2 ι n, exact (h rfl).elim, exact rfl }, refine ⟨c, _, cε⟩, refine is_sum_of_is_sum_ne_zero (λ n h, option.get (hs n h)) (λ n _, ne_of_gt (f0 _)) (λ i _, encodable.encode i) (λ n h, ne_of_gt _) (λ n h, _) (λ i _, _) (λ i _, _) hg, { dsimp [g], rw encodable.encodek2, exact f0 _ }, { exact encodable.mem_decode2.1 (option.get_mem _) }, { exact option.get_of_mem _ (encodable.encodek2 _) }, { dsimp [g], rw encodable.encodek2 } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, is_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := dense hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt.2 $ hε' i, ⟨c, is_sum_le (assume i, le_of_lt $ hε' i) is_sum_zero hc ⟩, nnreal.is_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε := begin rcases dense hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end end ennreal
76fa8b16217c065e91aa73fd94437bcda64c067b
cf39355caa609c0f33405126beee2739aa3cb77e
/library/data/buffer/parser.lean
ba0a409bf19ce3a3874aaa37b4270aa7b17a2b65
[ "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
8,356
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import data.buffer data.dlist inductive parse_result (α : Type) | done (pos : ℕ) (result : α) : parse_result | fail (pos : ℕ) (expected : dlist string) : parse_result /-- The parser monad. If you are familiar with the Parsec library in Haskell, you will understand this. -/ def parser (α : Type) := ∀ (input : char_buffer) (start : ℕ), parse_result α namespace parser variables {α β γ : Type} protected def bind (p : parser α) (f : α → parser β) : parser β := λ input pos, match p input pos with | parse_result.done pos a := f a input pos | parse_result.fail pos expected := parse_result.fail pos expected end protected def pure (a : α) : parser α := λ input pos, parse_result.done pos a private lemma parser.id_map (p : parser α) : parser.bind p parser.pure = p := begin apply funext, intro input, apply funext, intro pos, dunfold parser.bind, cases (p input pos); exact rfl end private lemma parser.bind_assoc (p : parser α) (q : α → parser β) (r : β → parser γ) : parser.bind (parser.bind p q) r = parser.bind p (λ a, parser.bind (q a) r) := begin apply funext, intro input, apply funext, intro pos, dunfold parser.bind, cases (p input pos); try {dunfold bind}, cases (q result input pos_1); try {dunfold bind}, all_goals {refl} end protected def fail (msg : string) : parser α := λ _ pos, parse_result.fail pos (dlist.singleton msg) instance : monad parser := { pure := @parser.pure, bind := @parser.bind } instance : is_lawful_monad parser := { id_map := @parser.id_map, pure_bind := λ _ _ _ _, rfl, bind_assoc := @parser.bind_assoc } instance : monad_fail parser := { fail := @parser.fail, ..parser.monad } protected def failure : parser α := λ _ pos, parse_result.fail pos dlist.empty protected def orelse (p q : parser α) : parser α := λ input pos, match p input pos with | parse_result.fail pos₁ expected₁ := if pos₁ ≠ pos then parse_result.fail pos₁ expected₁ else match q input pos with | parse_result.fail pos₂ expected₂ := if pos₁ < pos₂ then parse_result.fail pos₁ expected₁ else if pos₂ < pos₁ then parse_result.fail pos₂ expected₂ else -- pos₁ = pos₂ parse_result.fail pos₁ (expected₁ ++ expected₂) | ok := ok end | ok := ok end instance : alternative parser := { failure := @parser.failure, orelse := @parser.orelse } instance : inhabited (parser α) := ⟨parser.failure⟩ /-- Overrides the expected token name, and does not consume input on failure. -/ def decorate_errors (msgs : thunk (list string)) (p : parser α) : parser α := λ input pos, match p input pos with | parse_result.fail _ expected := parse_result.fail pos (dlist.lazy_of_list (msgs ())) | ok := ok end /-- Overrides the expected token name, and does not consume input on failure. -/ def decorate_error (msg : thunk string) (p : parser α) : parser α := decorate_errors [msg ()] p /-- Matches a single character. Fails only if there is no more input. -/ def any_char : parser char := λ input pos, if h : pos < input.size then let c := input.read ⟨pos, h⟩ in parse_result.done (pos+1) c else parse_result.fail pos dlist.empty /-- Matches a single character satisfying the given predicate. -/ def sat (p : char → Prop) [decidable_pred p] : parser char := λ input pos, if h : pos < input.size then let c := input.read ⟨pos, h⟩ in if p c then parse_result.done (pos+1) c else parse_result.fail pos dlist.empty else parse_result.fail pos dlist.empty /-- Matches the empty word. -/ def eps : parser unit := return () /-- Matches the given character. -/ def ch (c : char) : parser unit := decorate_error c.to_string $ sat (= c) >> eps /-- Matches a whole char_buffer. Does not consume input in case of failure. -/ def char_buf (s : char_buffer) : parser unit := decorate_error s.to_string $ s.to_list.mmap' ch /-- Matches one out of a list of characters. -/ def one_of (cs : list char) : parser char := decorate_errors (do c ← cs, return c.to_string) $ sat (∈ cs) def one_of' (cs : list char) : parser unit := one_of cs >> eps /-- Matches a string. Does not consume input in case of failure. -/ def str (s : string) : parser unit := decorate_error s $ s.to_list.mmap' ch /-- Number of remaining input characters. -/ def remaining : parser ℕ := λ input pos, parse_result.done pos (input.size - pos) /-- Matches the end of the input. -/ def eof : parser unit := decorate_error "<end-of-file>" $ do rem ← remaining, guard $ rem = 0 def foldr_core (f : α → β → β) (p : parser α) (b : β) : ∀ (reps : ℕ), parser β | 0 := failure | (reps+1) := (do x ← p, xs ← foldr_core reps, return (f x xs)) <|> return b /-- Matches zero or more occurrences of `p`, and folds the result. -/ def foldr (f : α → β → β) (p : parser α) (b : β) : parser β := λ input pos, foldr_core f p b (input.size - pos + 1) input pos def foldl_core (f : α → β → α) : ∀ (a : α) (p : parser β) (reps : ℕ), parser α | a p 0 := failure | a p (reps+1) := (do x ← p, foldl_core (f a x) p reps) <|> return a /-- Matches zero or more occurrences of `p`, and folds the result. -/ def foldl (f : α → β → α) (a : α) (p : parser β) : parser α := λ input pos, foldl_core f a p (input.size - pos + 1) input pos /-- Matches zero or more occurrences of `p`. -/ def many (p : parser α) : parser (list α) := foldr list.cons p [] def many_char (p : parser char) : parser string := list.as_string <$> many p /-- Matches zero or more occurrences of `p`. -/ def many' (p : parser α) : parser unit := many p >> eps /-- Matches one or more occurrences of `p`. -/ def many1 (p : parser α) : parser (list α) := list.cons <$> p <*> many p /-- Matches one or more occurences of the char parser `p` and implodes them into a string. -/ def many_char1 (p : parser char) : parser string := list.as_string <$> many1 p /-- Matches one or more occurrences of `p`, separated by `sep`. -/ def sep_by1 (sep : parser unit) (p : parser α) : parser (list α) := list.cons <$> p <*> many (sep >> p) /-- Matches zero or more occurrences of `p`, separated by `sep`. -/ def sep_by (sep : parser unit) (p : parser α) : parser (list α) := sep_by1 sep p <|> return [] def fix_core (F : parser α → parser α) : ∀ (max_depth : ℕ), parser α | 0 := failure | (max_depth+1) := F (fix_core max_depth) /-- Matches a digit (0-9). -/ def digit : parser nat := decorate_error "<digit>" $ do c ← sat (λ c, '0' ≤ c ∧ c ≤ '9'), pure $ c.to_nat - '0'.to_nat /-- Matches a natural number. Large numbers may cause performance issues, so don't run this parser on untrusted input. -/ def nat : parser nat := decorate_error "<natural>" $ do digits ← many1 digit, pure $ prod.fst $ digits.foldr (λ digit ⟨sum, magnitude⟩, ⟨sum + digit * magnitude, magnitude * 10⟩) ⟨0, 1⟩ /-- Fixpoint combinator satisfying `fix F = F (fix F)`. -/ def fix (F : parser α → parser α) : parser α := λ input pos, fix_core F (input.size - pos + 1) input pos private def make_monospaced : char → char | '\n' := ' ' | '\t' := ' ' | '\x0d' := ' ' | c := c def mk_error_msg (input : char_buffer) (pos : ℕ) (expected : dlist string) : char_buffer := let left_ctx := (input.take pos).take_right 10, right_ctx := (input.drop pos).take 10 in left_ctx.map make_monospaced ++ right_ctx.map make_monospaced ++ "\n".to_char_buffer ++ left_ctx.map (λ _, ' ') ++ "^\n".to_char_buffer ++ "\n".to_char_buffer ++ "expected: ".to_char_buffer ++ string.to_char_buffer (" | ".intercalate expected.to_list) ++ "\n".to_char_buffer /-- Runs a parser on the given input. The parser needs to match the complete input. -/ def run (p : parser α) (input : char_buffer) : sum string α := match (p <* eof) input 0 with | parse_result.done pos res := sum.inr res | parse_result.fail pos expected := sum.inl $ buffer.to_string $ mk_error_msg input pos expected end /-- Runs a parser on the given input. The parser needs to match the complete input. -/ def run_string (p : parser α) (input : string) : sum string α := run p input.to_char_buffer end parser
b042c256f937baa7fa9c571aeb85394e98395b38
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/algebra/module/basic.lean
527bff98a22517df742b9a4526d139836c3944b0
[ "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
92,721
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import topology.algebra.ring import topology.algebra.mul_action import topology.algebra.uniform_group import topology.uniform_space.uniform_embedding import algebra.algebra.basic import linear_algebra.projection import linear_algebra.pi import linear_algebra.determinant /-! # Theory of topological modules and continuous linear maps. We use the class `has_continuous_smul` for topological (semi) modules and topological vector spaces. In this file we define continuous (semi-)linear maps, as semilinear maps between topological modules which are continuous. The set of continuous semilinear maps between the topological `R₁`-module `M` and `R₂`-module `M₂` with respect to the `ring_hom` `σ` is denoted by `M →SL[σ] M₂`. Plain linear maps are denoted by `M →L[R] M₂` and star-linear maps by `M →L⋆[R] M₂`. The corresponding notation for equivalences is `M ≃SL[σ] M₂`, `M ≃L[R] M₂` and `M ≃L⋆[R] M₂`. -/ open filter open_locale topological_space big_operators filter universes u v w u' section variables {R : Type*} {M : Type*} [ring R] [topological_space R] [topological_space M] [add_comm_group M] [module R M] lemma has_continuous_smul.of_nhds_zero [topological_ring R] [topological_add_group M] (hmul : tendsto (λ p : R × M, p.1 • p.2) (𝓝 0 ×ᶠ (𝓝 0)) (𝓝 0)) (hmulleft : ∀ m : M, tendsto (λ a : R, a • m) (𝓝 0) (𝓝 0)) (hmulright : ∀ a : R, tendsto (λ m : M, a • m) (𝓝 0) (𝓝 0)) : has_continuous_smul R M := ⟨begin rw continuous_iff_continuous_at, rintros ⟨a₀, m₀⟩, have key : ∀ p : R × M, p.1 • p.2 = a₀ • m₀ + ((p.1 - a₀) • m₀ + a₀ • (p.2 - m₀) + (p.1 - a₀) • (p.2 - m₀)), { rintro ⟨a, m⟩, simp [sub_smul, smul_sub], abel }, rw funext key, clear key, refine tendsto_const_nhds.add (tendsto.add (tendsto.add _ _) _), { rw [sub_self, zero_smul], apply (hmulleft m₀).comp, rw [show (λ p : R × M, p.1 - a₀) = (λ a, a - a₀) ∘ prod.fst, by {ext, refl }, nhds_prod_eq], have : tendsto (λ a, a - a₀) (𝓝 a₀) (𝓝 0), { rw ← sub_self a₀, exact tendsto_id.sub tendsto_const_nhds }, exact this.comp tendsto_fst }, { rw [sub_self, smul_zero], apply (hmulright a₀).comp, rw [show (λ p : R × M, p.2 - m₀) = (λ m, m - m₀) ∘ prod.snd, by {ext, refl }, nhds_prod_eq], have : tendsto (λ m, m - m₀) (𝓝 m₀) (𝓝 0), { rw ← sub_self m₀, exact tendsto_id.sub tendsto_const_nhds }, exact this.comp tendsto_snd }, { rw [sub_self, zero_smul, nhds_prod_eq, show (λ p : R × M, (p.fst - a₀) • (p.snd - m₀)) = (λ p : R × M, p.1 • p.2) ∘ (prod.map (λ a, a - a₀) (λ m, m - m₀)), by { ext, refl }], apply hmul.comp (tendsto.prod_map _ _); { rw ← sub_self , exact tendsto_id.sub tendsto_const_nhds } }, end⟩ end section variables {R : Type*} {M : Type*} [ring R] [topological_space R] [topological_space M] [add_comm_group M] [has_continuous_add M] [module R M] [has_continuous_smul R M] /-- If `M` is a topological module over `R` and `0` is a limit of invertible elements of `R`, then `⊤` is the only submodule of `M` with a nonempty interior. This is the case, e.g., if `R` is a nontrivially normed field. -/ lemma submodule.eq_top_of_nonempty_interior' [ne_bot (𝓝[{x : R | is_unit x}] 0)] (s : submodule R M) (hs : (interior (s:set M)).nonempty) : s = ⊤ := begin rcases hs with ⟨y, hy⟩, refine (submodule.eq_top_iff'.2 $ λ x, _), rw [mem_interior_iff_mem_nhds] at hy, have : tendsto (λ c:R, y + c • x) (𝓝[{x : R | is_unit x}] 0) (𝓝 (y + (0:R) • x)), from tendsto_const_nhds.add ((tendsto_nhds_within_of_tendsto_nhds tendsto_id).smul tendsto_const_nhds), rw [zero_smul, add_zero] at this, obtain ⟨_, hu : y + _ • _ ∈ s, u, rfl⟩ := nonempty_of_mem (inter_mem (mem_map.1 (this hy)) self_mem_nhds_within), have hy' : y ∈ ↑s := mem_of_mem_nhds hy, rwa [s.add_mem_iff_right hy', ←units.smul_def, s.smul_mem_iff' u] at hu, end variables (R M) /-- Let `R` be a topological ring such that zero is not an isolated point (e.g., a nontrivially normed field, see `normed_field.punctured_nhds_ne_bot`). Let `M` be a nontrivial module over `R` such that `c • x = 0` implies `c = 0 ∨ x = 0`. Then `M` has no isolated points. We formulate this using `ne_bot (𝓝[≠] x)`. This lemma is not an instance because Lean would need to find `[has_continuous_smul ?m_1 M]` with unknown `?m_1`. We register this as an instance for `R = ℝ` in `real.punctured_nhds_module_ne_bot`. One can also use `haveI := module.punctured_nhds_ne_bot R M` in a proof. -/ lemma module.punctured_nhds_ne_bot [nontrivial M] [ne_bot (𝓝[≠] (0 : R))] [no_zero_smul_divisors R M] (x : M) : ne_bot (𝓝[≠] x) := begin rcases exists_ne (0 : M) with ⟨y, hy⟩, suffices : tendsto (λ c : R, x + c • y) (𝓝[≠] 0) (𝓝[≠] x), from this.ne_bot, refine tendsto.inf _ (tendsto_principal_principal.2 $ _), { convert tendsto_const_nhds.add ((@tendsto_id R _).smul_const y), rw [zero_smul, add_zero] }, { intros c hc, simpa [hy] using hc } end end section lattice_ops variables {ι R M₁ M₂ : Type*} [semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [module R M₁] [module R M₂] [u : topological_space R] {t : topological_space M₂} [has_continuous_smul R M₂] (f : M₁ →ₗ[R] M₂) lemma has_continuous_smul_induced : @has_continuous_smul R M₁ _ u (t.induced f) := { continuous_smul := begin letI : topological_space M₁ := t.induced f, refine continuous_induced_rng.2 _, simp_rw [function.comp, f.map_smul], refine continuous_fst.smul (continuous_induced_dom.comp continuous_snd) end } end lattice_ops namespace submodule variables {α β : Type*} [topological_space β] instance [topological_space α] [semiring α] [add_comm_monoid β] [module α β] [has_continuous_smul α β] (S : submodule α β) : has_continuous_smul α S := { continuous_smul := begin rw embedding_subtype_coe.to_inducing.continuous_iff, exact continuous_fst.smul (continuous_subtype_coe.comp continuous_snd) end } instance [ring α] [add_comm_group β] [module α β] [topological_add_group β] (S : submodule α β) : topological_add_group S := S.to_add_subgroup.topological_add_group end submodule section closure variables {R : Type u} {M : Type v} [semiring R] [topological_space R] [topological_space M] [add_comm_monoid M] [module R M] [has_continuous_smul R M] lemma submodule.closure_smul_self_subset (s : submodule R M) : (λ p : R × M, p.1 • p.2) '' (set.univ ×ˢ closure s) ⊆ closure s := calc (λ p : R × M, p.1 • p.2) '' (set.univ ×ˢ closure s) = (λ p : R × M, p.1 • p.2) '' closure (set.univ ×ˢ s) : by simp [closure_prod_eq] ... ⊆ closure ((λ p : R × M, p.1 • p.2) '' (set.univ ×ˢ s)) : image_closure_subset_closure_image continuous_smul ... = closure s : begin congr, ext x, refine ⟨_, λ hx, ⟨⟨1, x⟩, ⟨set.mem_univ _, hx⟩, one_smul R _⟩⟩, rintros ⟨⟨c, y⟩, ⟨hc, hy⟩, rfl⟩, simp [s.smul_mem c hy] end lemma submodule.closure_smul_self_eq (s : submodule R M) : (λ p : R × M, p.1 • p.2) '' (set.univ ×ˢ closure s) = closure s := s.closure_smul_self_subset.antisymm $ λ x hx, ⟨⟨1, x⟩, ⟨set.mem_univ _, hx⟩, one_smul R _⟩ variables [has_continuous_add M] /-- The (topological-space) closure of a submodule of a topological `R`-module `M` is itself a submodule. -/ def submodule.topological_closure (s : submodule R M) : submodule R M := { carrier := closure (s : set M), smul_mem' := λ c x hx, s.closure_smul_self_subset ⟨⟨c, x⟩, ⟨set.mem_univ _, hx⟩, rfl⟩, ..s.to_add_submonoid.topological_closure } @[simp] lemma submodule.topological_closure_coe (s : submodule R M) : (s.topological_closure : set M) = closure (s : set M) := rfl lemma submodule.submodule_topological_closure (s : submodule R M) : s ≤ s.topological_closure := subset_closure lemma submodule.is_closed_topological_closure (s : submodule R M) : is_closed (s.topological_closure : set M) := by convert is_closed_closure lemma submodule.topological_closure_minimal (s : submodule R M) {t : submodule R M} (h : s ≤ t) (ht : is_closed (t : set M)) : s.topological_closure ≤ t := closure_minimal h ht lemma submodule.topological_closure_mono {s : submodule R M} {t : submodule R M} (h : s ≤ t) : s.topological_closure ≤ t.topological_closure := s.topological_closure_minimal (h.trans t.submodule_topological_closure) t.is_closed_topological_closure /-- The topological closure of a closed submodule `s` is equal to `s`. -/ lemma is_closed.submodule_topological_closure_eq {s : submodule R M} (hs : is_closed (s : set M)) : s.topological_closure = s := le_antisymm (s.topological_closure_minimal rfl.le hs) s.submodule_topological_closure /-- A subspace is dense iff its topological closure is the entire space. -/ lemma submodule.dense_iff_topological_closure_eq_top {s : submodule R M} : dense (s : set M) ↔ s.topological_closure = ⊤ := by { rw [←set_like.coe_set_eq, dense_iff_closure_eq], simp } instance {M' : Type*} [add_comm_monoid M'] [module R M'] [uniform_space M'] [has_continuous_add M'] [has_continuous_smul R M'] [complete_space M'] (U : submodule R M') : complete_space U.topological_closure := is_closed_closure.complete_space_coe end closure /-- Continuous linear maps between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological ring `R`. -/ structure continuous_linear_map {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) (M : Type*) [topological_space M] [add_comm_monoid M] (M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends M →ₛₗ[σ] M₂ := (cont : continuous to_fun . tactic.interactive.continuity') notation M ` →SL[`:25 σ `] ` M₂ := continuous_linear_map σ M M₂ notation M ` →L[`:25 R `] ` M₂ := continuous_linear_map (ring_hom.id R) M M₂ notation M ` →L⋆[`:25 R `] ` M₂ := continuous_linear_map (star_ring_end R) M M₂ set_option old_structure_cmd true /-- `continuous_semilinear_map_class F σ M M₂` asserts `F` is a type of bundled continuous `σ`-semilinear maps `M → M₂`. See also `continuous_linear_map_class F R M M₂` for the case where `σ` is the identity map on `R`. A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S` is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = (σ c) • f x`. -/ class continuous_semilinear_map_class (F : Type*) {R S : out_param Type*} [semiring R] [semiring S] (σ : out_param $ R →+* S) (M : out_param Type*) [topological_space M] [add_comm_monoid M] (M₂ : out_param Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends semilinear_map_class F σ M M₂, continuous_map_class F M M₂ -- `σ`, `R` and `S` become metavariables, but they are all outparams so it's OK attribute [nolint dangerous_instance] continuous_semilinear_map_class.to_continuous_map_class /-- `continuous_linear_map_class F R M M₂` asserts `F` is a type of bundled continuous `R`-linear maps `M → M₂`. This is an abbreviation for `continuous_semilinear_map_class F (ring_hom.id R) M M₂`. -/ abbreviation continuous_linear_map_class (F : Type*) (R : out_param Type*) [semiring R] (M : out_param Type*) [topological_space M] [add_comm_monoid M] (M₂ : out_param Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module R M₂] := continuous_semilinear_map_class F (ring_hom.id R) M M₂ set_option old_structure_cmd false /-- Continuous linear equivalences between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological semiring `R`. -/ @[nolint has_nonempty_instance] structure continuous_linear_equiv {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) [topological_space M] [add_comm_monoid M] (M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends M ≃ₛₗ[σ] M₂ := (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') notation M ` ≃SL[`:50 σ `] ` M₂ := continuous_linear_equiv σ M M₂ notation M ` ≃L[`:50 R `] ` M₂ := continuous_linear_equiv (ring_hom.id R) M M₂ notation M ` ≃L⋆[`:50 R `] ` M₂ := continuous_linear_equiv (star_ring_end R) M M₂ set_option old_structure_cmd true /-- `continuous_semilinear_equiv_class F σ M M₂` asserts `F` is a type of bundled continuous `σ`-semilinear equivs `M → M₂`. See also `continuous_linear_equiv_class F R M M₂` for the case where `σ` is the identity map on `R`. A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S` is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = (σ c) • f x`. -/ class continuous_semilinear_equiv_class (F : Type*) {R : out_param Type*} {S : out_param Type*} [semiring R] [semiring S] (σ : out_param $ R →+* S) {σ' : out_param $ S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : out_param Type*) [topological_space M] [add_comm_monoid M] (M₂ : out_param Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] extends semilinear_equiv_class F σ M M₂ := (map_continuous : ∀ (f : F), continuous f . tactic.interactive.continuity') (inv_continuous : ∀ (f : F), continuous (inv f) . tactic.interactive.continuity') /-- `continuous_linear_equiv_class F σ M M₂` asserts `F` is a type of bundled continuous `R`-linear equivs `M → M₂`. This is an abbreviation for `continuous_semilinear_equiv_class F (ring_hom.id) M M₂`. -/ abbreviation continuous_linear_equiv_class (F : Type*) (R : out_param Type*) [semiring R] (M : out_param Type*) [topological_space M] [add_comm_monoid M] (M₂ : out_param Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module R M₂] := continuous_semilinear_equiv_class F (ring_hom.id R) M M₂ set_option old_structure_cmd false namespace continuous_semilinear_equiv_class variables (F : Type*) {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) [topological_space M] [add_comm_monoid M] (M₂ : Type*) [topological_space M₂] [add_comm_monoid M₂] [module R M] [module S M₂] include σ' -- `σ'` becomes a metavariable, but it's OK since it's an outparam @[priority 100, nolint dangerous_instance] instance [s: continuous_semilinear_equiv_class F σ M M₂] : continuous_semilinear_map_class F σ M M₂ := { coe := (coe : F → M → M₂), coe_injective' := @fun_like.coe_injective F _ _ _, ..s } omit σ' end continuous_semilinear_equiv_class section pointwise_limits variables {M₁ M₂ α R S : Type*} [topological_space M₂] [t2_space M₂] [semiring R] [semiring S] [add_comm_monoid M₁] [add_comm_monoid M₂] [module R M₁] [module S M₂] [has_continuous_const_smul S M₂] section variables (M₁ M₂) (σ : R →+* S) lemma is_closed_set_of_map_smul : is_closed {f : M₁ → M₂ | ∀ c x, f (c • x) = σ c • f x} := begin simp only [set.set_of_forall], exact is_closed_Inter (λ c, is_closed_Inter (λ x, is_closed_eq (continuous_apply _) ((continuous_apply _).const_smul _))) end end variables [has_continuous_add M₂] {σ : R →+* S} {l : filter α} /-- Constructs a bundled linear map from a function and a proof that this function belongs to the closure of the set of linear maps. -/ @[simps { fully_applied := ff }] def linear_map_of_mem_closure_range_coe (f : M₁ → M₂) (hf : f ∈ closure (set.range (coe_fn : (M₁ →ₛₗ[σ] M₂) → (M₁ → M₂)))) : M₁ →ₛₗ[σ] M₂ := { to_fun := f, map_smul' := (is_closed_set_of_map_smul M₁ M₂ σ).closure_subset_iff.2 (set.range_subset_iff.2 linear_map.map_smulₛₗ) hf, .. add_monoid_hom_of_mem_closure_range_coe f hf } /-- Construct a bundled linear map from a pointwise limit of linear maps -/ @[simps { fully_applied := ff }] def linear_map_of_tendsto (f : M₁ → M₂) (g : α → M₁ →ₛₗ[σ] M₂) [l.ne_bot] (h : tendsto (λ a x, g a x) l (𝓝 f)) : M₁ →ₛₗ[σ] M₂ := linear_map_of_mem_closure_range_coe f $ mem_closure_of_tendsto h $ eventually_of_forall $ λ a, set.mem_range_self _ variables (M₁ M₂ σ) lemma linear_map.is_closed_range_coe : is_closed (set.range (coe_fn : (M₁ →ₛₗ[σ] M₂) → (M₁ → M₂))) := is_closed_of_closure_subset $ λ f hf, ⟨linear_map_of_mem_closure_range_coe f hf, rfl⟩ end pointwise_limits namespace continuous_linear_map section semiring /-! ### Properties that hold for non-necessarily commutative semirings. -/ variables {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} [semiring R₁] [semiring R₂] [semiring R₃] {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} {M₁ : Type*} [topological_space M₁] [add_comm_monoid M₁] {M'₁ : Type*} [topological_space M'₁] [add_comm_monoid M'₁] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] {M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄] [module R₁ M₁] [module R₁ M'₁] [module R₂ M₂] [module R₃ M₃] /-- Coerce continuous linear maps to linear maps. -/ instance : has_coe (M₁ →SL[σ₁₂] M₂) (M₁ →ₛₗ[σ₁₂] M₂) := ⟨to_linear_map⟩ -- make the coercion the preferred form @[simp] lemma to_linear_map_eq_coe (f : M₁ →SL[σ₁₂] M₂) : f.to_linear_map = f := rfl theorem coe_injective : function.injective (coe : (M₁ →SL[σ₁₂] M₂) → (M₁ →ₛₗ[σ₁₂] M₂)) := by { intros f g H, cases f, cases g, congr' } instance : continuous_semilinear_map_class (M₁ →SL[σ₁₂] M₂) σ₁₂ M₁ M₂ := { coe := λ f, f.to_fun, coe_injective' := λ f g h, coe_injective (fun_like.coe_injective h), map_add := λ f, map_add f.to_linear_map, map_continuous := λ f, f.2, map_smulₛₗ := λ f, f.to_linear_map.map_smul' } /-- Coerce continuous linear maps to functions. -/ -- see Note [function coercion] instance to_fun : has_coe_to_fun (M₁ →SL[σ₁₂] M₂) (λ _, M₁ → M₂) := ⟨λ f, f.to_fun⟩ @[simp] lemma coe_mk (f : M₁ →ₛₗ[σ₁₂] M₂) (h) : (mk f h : M₁ →ₛₗ[σ₁₂] M₂) = f := rfl @[simp] lemma coe_mk' (f : M₁ →ₛₗ[σ₁₂] M₂) (h) : (mk f h : M₁ → M₂) = f := rfl @[continuity] protected lemma continuous (f : M₁ →SL[σ₁₂] M₂) : continuous f := f.2 protected lemma uniform_continuous {E₁ E₂ : Type*} [uniform_space E₁] [uniform_space E₂] [add_comm_group E₁] [add_comm_group E₂] [module R₁ E₁] [module R₂ E₂] [uniform_add_group E₁] [uniform_add_group E₂] (f : E₁ →SL[σ₁₂] E₂) : uniform_continuous f := uniform_continuous_add_monoid_hom_of_continuous f.continuous @[simp, norm_cast] lemma coe_inj {f g : M₁ →SL[σ₁₂] M₂} : (f : M₁ →ₛₗ[σ₁₂] M₂) = g ↔ f = g := coe_injective.eq_iff theorem coe_fn_injective : @function.injective (M₁ →SL[σ₁₂] M₂) (M₁ → M₂) coe_fn := fun_like.coe_injective /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : M₁ →SL[σ₁₂] M₂) : M₁ → M₂ := h /-- See Note [custom simps projection]. -/ def simps.coe (h : M₁ →SL[σ₁₂] M₂) : M₁ →ₛₗ[σ₁₂] M₂ := h initialize_simps_projections continuous_linear_map (to_linear_map_to_fun → apply, to_linear_map → coe) @[ext] theorem ext {f g : M₁ →SL[σ₁₂] M₂} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h theorem ext_iff {f g : M₁ →SL[σ₁₂] M₂} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Copy of a `continuous_linear_map` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : M₁ →SL[σ₁₂] M₂) (f' : M₁ → M₂) (h : f' = ⇑f) : M₁ →SL[σ₁₂] M₂ := { to_linear_map := f.to_linear_map.copy f' h, cont := show continuous f', from h.symm ▸ f.continuous } -- make some straightforward lemmas available to `simp`. protected lemma map_zero (f : M₁ →SL[σ₁₂] M₂) : f (0 : M₁) = 0 := map_zero f protected lemma map_add (f : M₁ →SL[σ₁₂] M₂) (x y : M₁) : f (x + y) = f x + f y := map_add f x y @[simp] protected lemma map_smulₛₗ (f : M₁ →SL[σ₁₂] M₂) (c : R₁) (x : M₁) : f (c • x) = (σ₁₂ c) • f x := (to_linear_map _).map_smulₛₗ _ _ @[simp] protected lemma map_smul [module R₁ M₂] (f : M₁ →L[R₁] M₂)(c : R₁) (x : M₁) : f (c • x) = c • f x := by simp only [ring_hom.id_apply, continuous_linear_map.map_smulₛₗ] @[simp, priority 900] lemma map_smul_of_tower {R S : Type*} [semiring S] [has_smul R M₁] [module S M₁] [has_smul R M₂] [module S M₂] [linear_map.compatible_smul M₁ M₂ R S] (f : M₁ →L[S] M₂) (c : R) (x : M₁) : f (c • x) = c • f x := linear_map.compatible_smul.map_smul f c x protected lemma map_sum {ι : Type*} (f : M₁ →SL[σ₁₂] M₂) (s : finset ι) (g : ι → M₁) : f (∑ i in s, g i) = ∑ i in s, f (g i) := f.to_linear_map.map_sum @[simp, norm_cast] lemma coe_coe (f : M₁ →SL[σ₁₂] M₂) : ⇑(f : M₁ →ₛₗ[σ₁₂] M₂) = f := rfl @[ext] theorem ext_ring [topological_space R₁] {f g : R₁ →L[R₁] M₁} (h : f 1 = g 1) : f = g := coe_inj.1 $ linear_map.ext_ring h theorem ext_ring_iff [topological_space R₁] {f g : R₁ →L[R₁] M₁} : f = g ↔ f 1 = g 1 := ⟨λ h, h ▸ rfl, ext_ring⟩ /-- If two continuous linear maps are equal on a set `s`, then they are equal on the closure of the `submodule.span` of this set. -/ lemma eq_on_closure_span [t2_space M₂] {s : set M₁} {f g : M₁ →SL[σ₁₂] M₂} (h : set.eq_on f g s) : set.eq_on f g (closure (submodule.span R₁ s : set M₁)) := (linear_map.eq_on_span' h).closure f.continuous g.continuous /-- If the submodule generated by a set `s` is dense in the ambient module, then two continuous linear maps equal on `s` are equal. -/ lemma ext_on [t2_space M₂] {s : set M₁} (hs : dense (submodule.span R₁ s : set M₁)) {f g : M₁ →SL[σ₁₂] M₂} (h : set.eq_on f g s) : f = g := ext $ λ x, eq_on_closure_span h (hs x) /-- Under a continuous linear map, the image of the `topological_closure` of a submodule is contained in the `topological_closure` of its image. -/ lemma _root_.submodule.topological_closure_map [ring_hom_surjective σ₁₂] [topological_space R₁] [topological_space R₂] [has_continuous_smul R₁ M₁] [has_continuous_add M₁] [has_continuous_smul R₂ M₂] [has_continuous_add M₂] (f : M₁ →SL[σ₁₂] M₂) (s : submodule R₁ M₁) : (s.topological_closure.map (f : M₁ →ₛₗ[σ₁₂] M₂)) ≤ (s.map (f : M₁ →ₛₗ[σ₁₂] M₂)).topological_closure := image_closure_subset_closure_image f.continuous /-- Under a dense continuous linear map, a submodule whose `topological_closure` is `⊤` is sent to another such submodule. That is, the image of a dense set under a map with dense range is dense. -/ lemma _root_.dense_range.topological_closure_map_submodule [ring_hom_surjective σ₁₂] [topological_space R₁] [topological_space R₂] [has_continuous_smul R₁ M₁] [has_continuous_add M₁] [has_continuous_smul R₂ M₂] [has_continuous_add M₂] {f : M₁ →SL[σ₁₂] M₂} (hf' : dense_range f) {s : submodule R₁ M₁} (hs : s.topological_closure = ⊤) : (s.map (f : M₁ →ₛₗ[σ₁₂] M₂)).topological_closure = ⊤ := begin rw set_like.ext'_iff at hs ⊢, simp only [submodule.topological_closure_coe, submodule.top_coe, ← dense_iff_closure_eq] at hs ⊢, exact hf'.dense_image f.continuous hs end section smul_monoid variables {S₂ T₂ : Type*} [monoid S₂] [monoid T₂] variables [distrib_mul_action S₂ M₂] [smul_comm_class R₂ S₂ M₂] [has_continuous_const_smul S₂ M₂] variables [distrib_mul_action T₂ M₂] [smul_comm_class R₂ T₂ M₂] [has_continuous_const_smul T₂ M₂] instance : mul_action S₂ (M₁ →SL[σ₁₂] M₂) := { smul := λ c f, ⟨c • f, (f.2.const_smul _ : continuous (λ x, c • f x))⟩, one_smul := λ f, ext $ λ x, one_smul _ _, mul_smul := λ a b f, ext $ λ x, mul_smul _ _ _ } lemma smul_apply (c : S₂) (f : M₁ →SL[σ₁₂] M₂) (x : M₁) : (c • f) x = c • (f x) := rfl @[simp, norm_cast] lemma coe_smul (c : S₂) (f : M₁ →SL[σ₁₂] M₂) : (↑(c • f) : M₁ →ₛₗ[σ₁₂] M₂) = c • f := rfl @[simp, norm_cast] lemma coe_smul' (c : S₂) (f : M₁ →SL[σ₁₂] M₂) : ⇑(c • f) = c • f := rfl instance [has_smul S₂ T₂] [is_scalar_tower S₂ T₂ M₂] : is_scalar_tower S₂ T₂ (M₁ →SL[σ₁₂] M₂) := ⟨λ a b f, ext $ λ x, smul_assoc a b (f x)⟩ instance [smul_comm_class S₂ T₂ M₂] : smul_comm_class S₂ T₂ (M₁ →SL[σ₁₂] M₂) := ⟨λ a b f, ext $ λ x, smul_comm a b (f x)⟩ end smul_monoid /-- The continuous map that is constantly zero. -/ instance: has_zero (M₁ →SL[σ₁₂] M₂) := ⟨⟨0, continuous_zero⟩⟩ instance : inhabited (M₁ →SL[σ₁₂] M₂) := ⟨0⟩ @[simp] lemma default_def : (default : M₁ →SL[σ₁₂] M₂) = 0 := rfl @[simp] lemma zero_apply (x : M₁) : (0 : M₁ →SL[σ₁₂] M₂) x = 0 := rfl @[simp, norm_cast] lemma coe_zero : ((0 : M₁ →SL[σ₁₂] M₂) : M₁ →ₛₗ[σ₁₂] M₂) = 0 := rfl /- no simp attribute on the next line as simp does not always simplify `0 x` to `0` when `0` is the zero function, while it does for the zero continuous linear map, and this is the most important property we care about. -/ @[norm_cast] lemma coe_zero' : ⇑(0 : M₁ →SL[σ₁₂] M₂) = 0 := rfl instance unique_of_left [subsingleton M₁] : unique (M₁ →SL[σ₁₂] M₂) := coe_injective.unique instance unique_of_right [subsingleton M₂] : unique (M₁ →SL[σ₁₂] M₂) := coe_injective.unique lemma exists_ne_zero {f : M₁ →SL[σ₁₂] M₂} (hf : f ≠ 0) : ∃ x, f x ≠ 0 := by { by_contra' h, exact hf (continuous_linear_map.ext h) } section variables (R₁ M₁) /-- the identity map as a continuous linear map. -/ def id : M₁ →L[R₁] M₁ := ⟨linear_map.id, continuous_id⟩ end instance : has_one (M₁ →L[R₁] M₁) := ⟨id R₁ M₁⟩ lemma one_def : (1 : M₁ →L[R₁] M₁) = id R₁ M₁ := rfl lemma id_apply (x : M₁) : id R₁ M₁ x = x := rfl @[simp, norm_cast] lemma coe_id : (id R₁ M₁ : M₁ →ₗ[R₁] M₁) = linear_map.id := rfl @[simp, norm_cast] lemma coe_id' : ⇑(id R₁ M₁) = _root_.id := rfl @[simp, norm_cast] lemma coe_eq_id {f : M₁ →L[R₁] M₁} : (f : M₁ →ₗ[R₁] M₁) = linear_map.id ↔ f = id _ _ := by rw [← coe_id, coe_inj] @[simp] lemma one_apply (x : M₁) : (1 : M₁ →L[R₁] M₁) x = x := rfl section add variables [has_continuous_add M₂] instance : has_add (M₁ →SL[σ₁₂] M₂) := ⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩ @[simp] lemma add_apply (f g : M₁ →SL[σ₁₂] M₂) (x : M₁) : (f + g) x = f x + g x := rfl @[simp, norm_cast] lemma coe_add (f g : M₁ →SL[σ₁₂] M₂) : (↑(f + g) : M₁ →ₛₗ[σ₁₂] M₂) = f + g := rfl @[norm_cast] lemma coe_add' (f g : M₁ →SL[σ₁₂] M₂) : ⇑(f + g) = f + g := rfl instance : add_comm_monoid (M₁ →SL[σ₁₂] M₂) := { zero := (0 : M₁ →SL[σ₁₂] M₂), add := (+), zero_add := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_zero := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_comm := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], add_assoc := by intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm], nsmul := (•), nsmul_zero' := λ f, by { ext, simp }, nsmul_succ' := λ n f, by { ext, simp [nat.succ_eq_one_add, add_smul] } } @[simp, norm_cast] lemma coe_sum {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) : ↑(∑ d in t, f d) = (∑ d in t, f d : M₁ →ₛₗ[σ₁₂] M₂) := (add_monoid_hom.mk (coe : (M₁ →SL[σ₁₂] M₂) → (M₁ →ₛₗ[σ₁₂] M₂)) rfl (λ _ _, rfl)).map_sum _ _ @[simp, norm_cast] lemma coe_sum' {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) : ⇑(∑ d in t, f d) = ∑ d in t, f d := by simp only [← coe_coe, coe_sum, linear_map.coe_fn_sum] lemma sum_apply {ι : Type*} (t : finset ι) (f : ι → M₁ →SL[σ₁₂] M₂) (b : M₁) : (∑ d in t, f d) b = ∑ d in t, f d b := by simp only [coe_sum', finset.sum_apply] end add variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] /-- Composition of bounded linear maps. -/ def comp (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : M₁ →SL[σ₁₃] M₃ := ⟨(g : M₂ →ₛₗ[σ₂₃] M₃).comp ↑f, g.2.comp f.2⟩ infixr ` ∘L `:80 := @continuous_linear_map.comp _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) _ _ _ _ _ _ _ _ _ _ _ _ ring_hom_comp_triple.ids @[simp, norm_cast] lemma coe_comp (h : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : (h.comp f : M₁ →ₛₗ[σ₁₃] M₃) = (h : M₂ →ₛₗ[σ₂₃] M₃).comp (f : M₁ →ₛₗ[σ₁₂] M₂) := rfl include σ₁₃ @[simp, norm_cast] lemma coe_comp' (h : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : ⇑(h.comp f) = h ∘ f := rfl lemma comp_apply (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) (x : M₁) : (g.comp f) x = g (f x) := rfl omit σ₁₃ @[simp] theorem comp_id (f : M₁ →SL[σ₁₂] M₂) : f.comp (id R₁ M₁) = f := ext $ λ x, rfl @[simp] theorem id_comp (f : M₁ →SL[σ₁₂] M₂) : (id R₂ M₂).comp f = f := ext $ λ x, rfl include σ₁₃ @[simp] theorem comp_zero (g : M₂ →SL[σ₂₃] M₃) : g.comp (0 : M₁ →SL[σ₁₂] M₂) = 0 := by { ext, simp } @[simp] theorem zero_comp (f : M₁ →SL[σ₁₂] M₂) : (0 : M₂ →SL[σ₂₃] M₃).comp f = 0 := by { ext, simp } @[simp] lemma comp_add [has_continuous_add M₂] [has_continuous_add M₃] (g : M₂ →SL[σ₂₃] M₃) (f₁ f₂ : M₁ →SL[σ₁₂] M₂) : g.comp (f₁ + f₂) = g.comp f₁ + g.comp f₂ := by { ext, simp } @[simp] lemma add_comp [has_continuous_add M₃] (g₁ g₂ : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : (g₁ + g₂).comp f = g₁.comp f + g₂.comp f := by { ext, simp } omit σ₁₃ theorem comp_assoc {R₄ : Type*} [semiring R₄] [module R₄ M₄] {σ₁₄ : R₁ →+* R₄} {σ₂₄ : R₂ →+* R₄} {σ₃₄ : R₃ →+* R₄} [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] [ring_hom_comp_triple σ₂₃ σ₃₄ σ₂₄] [ring_hom_comp_triple σ₁₂ σ₂₄ σ₁₄] (h : M₃ →SL[σ₃₄] M₄) (g : M₂ →SL[σ₂₃] M₃) (f : M₁ →SL[σ₁₂] M₂) : (h.comp g).comp f = h.comp (g.comp f) := rfl instance : has_mul (M₁ →L[R₁] M₁) := ⟨comp⟩ lemma mul_def (f g : M₁ →L[R₁] M₁) : f * g = f.comp g := rfl @[simp] lemma coe_mul (f g : M₁ →L[R₁] M₁) : ⇑(f * g) = f ∘ g := rfl lemma mul_apply (f g : M₁ →L[R₁] M₁) (x : M₁) : (f * g) x = f (g x) := rfl instance : monoid_with_zero (M₁ →L[R₁] M₁) := { mul := (*), one := 1, zero := 0, mul_zero := λ f, ext $ λ _, map_zero f, zero_mul := λ _, ext $ λ _, rfl, mul_one := λ _, ext $ λ _, rfl, one_mul := λ _, ext $ λ _, rfl, mul_assoc := λ _ _ _, ext $ λ _, rfl, } instance [has_continuous_add M₁] : semiring (M₁ →L[R₁] M₁) := { mul := (*), one := 1, left_distrib := λ f g h, ext $ λ x, map_add f (g x) (h x), right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _, ..continuous_linear_map.monoid_with_zero, ..continuous_linear_map.add_comm_monoid } /-- `continuous_linear_map.to_linear_map` as a `ring_hom`.-/ @[simps] def to_linear_map_ring_hom [has_continuous_add M₁] : (M₁ →L[R₁] M₁) →+* (M₁ →ₗ[R₁] M₁) := { to_fun := to_linear_map, map_zero' := rfl, map_one' := rfl, map_add' := λ _ _, rfl, map_mul' := λ _ _, rfl } section apply_action variables [has_continuous_add M₁] /-- The tautological action by `M₁ →L[R₁] M₁` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_module : module (M₁ →L[R₁] M₁) M₁ := module.comp_hom _ to_linear_map_ring_hom @[simp] protected lemma smul_def (f : M₁ →L[R₁] M₁) (a : M₁) : f • a = f a := rfl /-- `continuous_linear_map.apply_module` is faithful. -/ instance apply_has_faithful_smul : has_faithful_smul (M₁ →L[R₁] M₁) M₁ := ⟨λ _ _, continuous_linear_map.ext⟩ instance apply_smul_comm_class : smul_comm_class R₁ (M₁ →L[R₁] M₁) M₁ := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M₁ →L[R₁] M₁) R₁ M₁ := { smul_comm := continuous_linear_map.map_smul } instance : has_continuous_const_smul (M₁ →L[R₁] M₁) M₁ := ⟨continuous_linear_map.continuous⟩ end apply_action /-- The cartesian product of two bounded linear maps, as a bounded linear map. -/ protected def prod [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) : M₁ →L[R₁] (M₂ × M₃) := ⟨(f₁ : M₁ →ₗ[R₁] M₂).prod f₂, f₁.2.prod_mk f₂.2⟩ @[simp, norm_cast] lemma coe_prod [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) : (f₁.prod f₂ : M₁ →ₗ[R₁] M₂ × M₃) = linear_map.prod f₁ f₂ := rfl @[simp, norm_cast] lemma prod_apply [module R₁ M₂] [module R₁ M₃] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₁ →L[R₁] M₃) (x : M₁) : f₁.prod f₂ x = (f₁ x, f₂ x) := rfl section variables (R₁ M₁ M₂) /-- The left injection into a product is a continuous linear map. -/ def inl [module R₁ M₂] : M₁ →L[R₁] M₁ × M₂ := (id R₁ M₁).prod 0 /-- The right injection into a product is a continuous linear map. -/ def inr [module R₁ M₂] : M₂ →L[R₁] M₁ × M₂ := (0 : M₂ →L[R₁] M₁).prod (id R₁ M₂) end @[simp] lemma inl_apply [module R₁ M₂] (x : M₁) : inl R₁ M₁ M₂ x = (x, 0) := rfl @[simp] lemma inr_apply [module R₁ M₂] (x : M₂) : inr R₁ M₁ M₂ x = (0, x) := rfl @[simp, norm_cast] lemma coe_inl [module R₁ M₂] : (inl R₁ M₁ M₂ : M₁ →ₗ[R₁] M₁ × M₂) = linear_map.inl R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_inr [module R₁ M₂] : (inr R₁ M₁ M₂ : M₂ →ₗ[R₁] M₁ × M₂) = linear_map.inr R₁ M₁ M₂ := rfl /-- Kernel of a continuous linear map. -/ def ker (f : M₁ →SL[σ₁₂] M₂) : submodule R₁ M₁ := (f : M₁ →ₛₗ[σ₁₂] M₂).ker @[norm_cast] lemma ker_coe (f : M₁ →SL[σ₁₂] M₂) : (f : M₁ →ₛₗ[σ₁₂] M₂).ker = f.ker := rfl @[simp] lemma mem_ker {f : M₁ →SL[σ₁₂] M₂} {x} : x ∈ f.ker ↔ f x = 0 := linear_map.mem_ker lemma is_closed_ker [t1_space M₂] (f : M₁ →SL[σ₁₂] M₂) : is_closed (f.ker : set M₁) := continuous_iff_is_closed.1 f.cont _ is_closed_singleton @[simp] lemma apply_ker (f : M₁ →SL[σ₁₂] M₂) (x : f.ker) : f x = 0 := mem_ker.1 x.2 lemma is_complete_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M'] [module R₁ M'] [t1_space M₂] (f : M' →SL[σ₁₂] M₂) : is_complete (f.ker : set M') := f.is_closed_ker.is_complete instance complete_space_ker {M' : Type*} [uniform_space M'] [complete_space M'] [add_comm_monoid M'] [module R₁ M'] [t1_space M₂] (f : M' →SL[σ₁₂] M₂) : complete_space f.ker := f.is_closed_ker.complete_space_coe @[simp] lemma ker_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : ker (f.prod g) = ker f ⊓ ker g := linear_map.ker_prod f g /-- Range of a continuous linear map. -/ def range [ring_hom_surjective σ₁₂] (f : M₁ →SL[σ₁₂] M₂) : submodule R₂ M₂ := (f : M₁ →ₛₗ[σ₁₂] M₂).range lemma range_coe [ring_hom_surjective σ₁₂] (f : M₁ →SL[σ₁₂] M₂) : (f.range : set M₂) = set.range f := linear_map.range_coe _ lemma mem_range [ring_hom_surjective σ₁₂] {f : M₁ →SL[σ₁₂] M₂} {y} : y ∈ f.range ↔ ∃ x, f x = y := linear_map.mem_range lemma mem_range_self [ring_hom_surjective σ₁₂] (f : M₁ →SL[σ₁₂] M₂) (x : M₁) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ lemma range_prod_le [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : range (f.prod g) ≤ (range f).prod (range g) := (f : M₁ →ₗ[R₁] M₂).range_prod_le g /-- Restrict codomain of a continuous linear map. -/ def cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : M₁ →SL[σ₁₂] p := { cont := f.continuous.subtype_mk _, to_linear_map := (f : M₁ →ₛₗ[σ₁₂] M₂).cod_restrict p h} @[norm_cast] lemma coe_cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : (f.cod_restrict p h : M₁ →ₛₗ[σ₁₂] p) = (f : M₁ →ₛₗ[σ₁₂] M₂).cod_restrict p h := rfl @[simp] lemma coe_cod_restrict_apply (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) (x) : (f.cod_restrict p h x : M₂) = f x := rfl @[simp] lemma ker_cod_restrict (f : M₁ →SL[σ₁₂] M₂) (p : submodule R₂ M₂) (h : ∀ x, f x ∈ p) : ker (f.cod_restrict p h) = ker f := (f : M₁ →ₛₗ[σ₁₂] M₂).ker_cod_restrict p h /-- `submodule.subtype` as a `continuous_linear_map`. -/ def _root_.submodule.subtypeL (p : submodule R₁ M₁) : p →L[R₁] M₁ := { cont := continuous_subtype_val, to_linear_map := p.subtype } @[simp, norm_cast] lemma _root_.submodule.coe_subtypeL (p : submodule R₁ M₁) : (p.subtypeL : p →ₗ[R₁] M₁) = p.subtype := rfl @[simp] lemma _root_.submodule.coe_subtypeL' (p : submodule R₁ M₁) : ⇑p.subtypeL = p.subtype := rfl @[simp, norm_cast] lemma _root_.submodule.subtypeL_apply (p : submodule R₁ M₁) (x : p) : p.subtypeL x = x := rfl @[simp] lemma _root_.submodule.range_subtypeL (p : submodule R₁ M₁) : p.subtypeL.range = p := submodule.range_subtype _ @[simp] lemma _root_.submodule.ker_subtypeL (p : submodule R₁ M₁) : p.subtypeL.ker = ⊥ := submodule.ker_subtype _ variables (R₁ M₁ M₂) /-- `prod.fst` as a `continuous_linear_map`. -/ def fst [module R₁ M₂] : M₁ × M₂ →L[R₁] M₁ := { cont := continuous_fst, to_linear_map := linear_map.fst R₁ M₁ M₂ } /-- `prod.snd` as a `continuous_linear_map`. -/ def snd [module R₁ M₂] : M₁ × M₂ →L[R₁] M₂ := { cont := continuous_snd, to_linear_map := linear_map.snd R₁ M₁ M₂ } variables {R₁ M₁ M₂} @[simp, norm_cast] lemma coe_fst [module R₁ M₂] : ↑(fst R₁ M₁ M₂) = linear_map.fst R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_fst' [module R₁ M₂] : ⇑(fst R₁ M₁ M₂) = prod.fst := rfl @[simp, norm_cast] lemma coe_snd [module R₁ M₂] : ↑(snd R₁ M₁ M₂) = linear_map.snd R₁ M₁ M₂ := rfl @[simp, norm_cast] lemma coe_snd' [module R₁ M₂] : ⇑(snd R₁ M₁ M₂) = prod.snd := rfl @[simp] lemma fst_prod_snd [module R₁ M₂] : (fst R₁ M₁ M₂).prod (snd R₁ M₁ M₂) = id R₁ (M₁ × M₂) := ext $ λ ⟨x, y⟩, rfl @[simp] lemma fst_comp_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : (fst R₁ M₂ M₃).comp (f.prod g) = f := ext $ λ x, rfl @[simp] lemma snd_comp_prod [module R₁ M₂] [module R₁ M₃] (f : M₁ →L[R₁] M₂) (g : M₁ →L[R₁] M₃) : (snd R₁ M₂ M₃).comp (f.prod g) = g := ext $ λ x, rfl /-- `prod.map` of two continuous linear maps. -/ def prod_map [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : (M₁ × M₃) →L[R₁] (M₂ × M₄) := (f₁.comp (fst R₁ M₁ M₃)).prod (f₂.comp (snd R₁ M₁ M₃)) @[simp, norm_cast] lemma coe_prod_map [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : ↑(f₁.prod_map f₂) = ((f₁ : M₁ →ₗ[R₁] M₂).prod_map (f₂ : M₃ →ₗ[R₁] M₄)) := rfl @[simp, norm_cast] lemma coe_prod_map' [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (f₁ : M₁ →L[R₁] M₂) (f₂ : M₃ →L[R₁] M₄) : ⇑(f₁.prod_map f₂) = prod.map f₁ f₂ := rfl /-- The continuous linear map given by `(x, y) ↦ f₁ x + f₂ y`. -/ def coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (M₁ × M₂) →L[R₁] M₃ := ⟨linear_map.coprod f₁ f₂, (f₁.cont.comp continuous_fst).add (f₂.cont.comp continuous_snd)⟩ @[norm_cast, simp] lemma coe_coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (f₁.coprod f₂ : (M₁ × M₂) →ₗ[R₁] M₃) = linear_map.coprod f₁ f₂ := rfl @[simp] lemma coprod_apply [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) (x) : f₁.coprod f₂ x = f₁ x.1 + f₂ x.2 := rfl lemma range_coprod [module R₁ M₂] [module R₁ M₃] [has_continuous_add M₃] (f₁ : M₁ →L[R₁] M₃) (f₂ : M₂ →L[R₁] M₃) : (f₁.coprod f₂).range = f₁.range ⊔ f₂.range := linear_map.range_coprod _ _ section variables {R S : Type*} [semiring R] [semiring S] [module R M₁] [module R M₂] [module R S] [module S M₂] [is_scalar_tower R S M₂] [topological_space S] [has_continuous_smul S M₂] /-- The linear map `λ x, c x • f`. Associates to a scalar-valued linear map and an element of `M₂` the `M₂`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `M₂`). See also `continuous_linear_map.smul_rightₗ` and `continuous_linear_map.smul_rightL`. -/ def smul_right (c : M₁ →L[R] S) (f : M₂) : M₁ →L[R] M₂ := { cont := c.2.smul continuous_const, ..c.to_linear_map.smul_right f } @[simp] lemma smul_right_apply {c : M₁ →L[R] S} {f : M₂} {x : M₁} : (smul_right c f : M₁ → M₂) x = c x • f := rfl end variables [module R₁ M₂] [topological_space R₁] [has_continuous_smul R₁ M₂] @[simp] lemma smul_right_one_one (c : R₁ →L[R₁] M₂) : smul_right (1 : R₁ →L[R₁] R₁) (c 1) = c := by ext; simp [← continuous_linear_map.map_smul_of_tower] @[simp] lemma smul_right_one_eq_iff {f f' : M₂} : smul_right (1 : R₁ →L[R₁] R₁) f = smul_right (1 : R₁ →L[R₁] R₁) f' ↔ f = f' := by simp only [ext_ring_iff, smul_right_apply, one_apply, one_smul] lemma smul_right_comp [has_continuous_mul R₁] {x : M₂} {c : R₁} : (smul_right (1 : R₁ →L[R₁] R₁) x).comp (smul_right (1 : R₁ →L[R₁] R₁) c) = smul_right (1 : R₁ →L[R₁] R₁) (c • x) := by { ext, simp [mul_smul] } end semiring section pi variables {R : Type*} [semiring R] {M : Type*} [topological_space M] [add_comm_monoid M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [module R M₂] {ι : Type*} {φ : ι → Type*} [∀i, topological_space (φ i)] [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)] /-- `pi` construction for continuous linear functions. From a family of continuous linear functions it produces a continuous linear function into a family of topological modules. -/ def pi (f : Πi, M →L[R] φ i) : M →L[R] (Πi, φ i) := ⟨linear_map.pi (λ i, f i), continuous_pi (λ i, (f i).continuous)⟩ @[simp] lemma coe_pi' (f : Π i, M →L[R] φ i) : ⇑(pi f) = λ c i, f i c := rfl @[simp] lemma coe_pi (f : Π i, M →L[R] φ i) : (pi f : M →ₗ[R] Π i, φ i) = linear_map.pi (λ i, f i) := rfl lemma pi_apply (f : Πi, M →L[R] φ i) (c : M) (i : ι) : pi f c i = f i c := rfl lemma pi_eq_zero (f : Πi, M →L[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by { simp only [ext_iff, pi_apply, function.funext_iff], exact forall_swap } lemma pi_zero : pi (λi, 0 : Πi, M →L[R] φ i) = 0 := ext $ λ _, rfl lemma pi_comp (f : Πi, M →L[R] φ i) (g : M₂ →L[R] M) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of topological modules are continuous linear maps. -/ def proj (i : ι) : (Πi, φ i) →L[R] φ i := ⟨linear_map.proj i, continuous_apply _⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →L[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →L[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := linear_map.infi_ker_proj variables (R φ) /-- If `I` and `J` are complementary index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃L[R] (Πi:I, φ i) := { to_linear_equiv := linear_map.infi_ker_proj_equiv R φ hd hu, continuous_to_fun := continuous_pi (λ i, begin have := @continuous_subtype_coe _ _ (λ x, x ∈ (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i))), have := continuous.comp (by exact continuous_apply i) this, exact this end), continuous_inv_fun := continuous.subtype_mk (continuous_pi (λ i, begin dsimp, split_ifs; [apply continuous_apply, exact continuous_zero] end)) _ } end pi section ring variables {R : Type*} [ring R] {R₂ : Type*} [ring R₂] {R₃ : Type*} [ring R₃] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R₂ M₂] [module R₃ M₃] {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} section protected lemma map_neg (f : M →SL[σ₁₂] M₂) (x : M) : f (-x) = - (f x) := map_neg _ _ protected lemma map_sub (f : M →SL[σ₁₂] M₂) (x y : M) : f (x - y) = f x - f y := map_sub _ _ _ @[simp] lemma sub_apply' (f g : M →SL[σ₁₂] M₂) (x : M) : ((f : M →ₛₗ[σ₁₂] M₂) - g) x = f x - g x := rfl end section variables [module R M₂] [module R M₃] [module R M₄] lemma range_prod_eq {f : M →L[R] M₂} {g : M →L[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (f.prod g) = (range f).prod (range g) := linear_map.range_prod_eq h lemma ker_prod_ker_le_ker_coprod [has_continuous_add M₃] (f : M →L[R] M₃) (g : M₂ →L[R] M₃) : (ker f).prod (ker g) ≤ ker (f.coprod g) := linear_map.ker_prod_ker_le_ker_coprod f.to_linear_map g.to_linear_map lemma ker_coprod_of_disjoint_range [has_continuous_add M₃] (f : M →L[R] M₃) (g : M₂ →L[R] M₃) (hd : disjoint f.range g.range) : ker (f.coprod g) = (ker f).prod (ker g) := linear_map.ker_coprod_of_disjoint_range f.to_linear_map g.to_linear_map hd end section variables [topological_add_group M₂] instance : has_neg (M →SL[σ₁₂] M₂) := ⟨λ f, ⟨-f, f.2.neg⟩⟩ @[simp] lemma neg_apply (f : M →SL[σ₁₂] M₂) (x : M) : (-f) x = - (f x) := rfl @[simp, norm_cast] lemma coe_neg (f : M →SL[σ₁₂] M₂) : (↑(-f) : M →ₛₗ[σ₁₂] M₂) = -f := rfl @[norm_cast] lemma coe_neg' (f : M →SL[σ₁₂] M₂) : ⇑(-f) = -f := rfl instance : has_sub (M →SL[σ₁₂] M₂) := ⟨λ f g, ⟨f - g, f.2.sub g.2⟩⟩ instance : add_comm_group (M →SL[σ₁₂] M₂) := by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, nsmul := (•), zsmul := (•), zsmul_zero' := λ f, by { ext, simp }, zsmul_succ' := λ n f, by { ext, simp [add_smul, add_comm] }, zsmul_neg' := λ n f, by { ext, simp [nat.succ_eq_add_one, add_smul] }, .. continuous_linear_map.add_comm_monoid, .. }; intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm, sub_eq_add_neg] lemma sub_apply (f g : M →SL[σ₁₂] M₂) (x : M) : (f - g) x = f x - g x := rfl @[simp, norm_cast] lemma coe_sub (f g : M →SL[σ₁₂] M₂) : (↑(f - g) : M →ₛₗ[σ₁₂] M₂) = f - g := rfl @[simp, norm_cast] lemma coe_sub' (f g : M →SL[σ₁₂] M₂) : ⇑(f - g) = f - g := rfl end @[simp] lemma comp_neg [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [topological_add_group M₂] [topological_add_group M₃] (g : M₂ →SL[σ₂₃] M₃) (f : M →SL[σ₁₂] M₂) : g.comp (-f) = -g.comp f := by { ext, simp } @[simp] lemma neg_comp [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [topological_add_group M₃] (g : M₂ →SL[σ₂₃] M₃) (f : M →SL[σ₁₂] M₂) : (-g).comp f = -g.comp f := by { ext, simp } @[simp] lemma comp_sub [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [topological_add_group M₂] [topological_add_group M₃] (g : M₂ →SL[σ₂₃] M₃) (f₁ f₂ : M →SL[σ₁₂] M₂) : g.comp (f₁ - f₂) = g.comp f₁ - g.comp f₂ := by { ext, simp } @[simp] lemma sub_comp [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [topological_add_group M₃] (g₁ g₂ : M₂ →SL[σ₂₃] M₃) (f : M →SL[σ₁₂] M₂) : (g₁ - g₂).comp f = g₁.comp f - g₂.comp f := by { ext, simp } instance [topological_add_group M] : ring (M →L[R] M) := { mul := (*), one := 1, ..continuous_linear_map.semiring, ..continuous_linear_map.add_comm_group } lemma smul_right_one_pow [topological_space R] [topological_ring R] (c : R) (n : ℕ) : (smul_right (1 : R →L[R] R) c)^n = smul_right (1 : R →L[R] R) (c^n) := begin induction n with n ihn, { ext, simp }, { rw [pow_succ, ihn, mul_def, smul_right_comp, smul_eq_mul, pow_succ'] } end section variables {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁] /-- Given a right inverse `f₂ : M₂ →L[R] M` to `f₁ : M →L[R] M₂`, `proj_ker_of_right_inverse f₁ f₂ h` is the projection `M →L[R] f₁.ker` along `f₂.range`. -/ def proj_ker_of_right_inverse [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) : M →L[R] f₁.ker := (id R M - f₂.comp f₁).cod_restrict f₁.ker $ λ x, by simp [h (f₁ x)] @[simp] lemma coe_proj_ker_of_right_inverse_apply [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (x : M) : (f₁.proj_ker_of_right_inverse f₂ h x : M) = x - f₂ (f₁ x) := rfl @[simp] lemma proj_ker_of_right_inverse_apply_idem [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (x : f₁.ker) : f₁.proj_ker_of_right_inverse f₂ h x = x := subtype.ext_iff_val.2 $ by simp @[simp] lemma proj_ker_of_right_inverse_comp_inv [topological_add_group M] (f₁ : M →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M) (h : function.right_inverse f₂ f₁) (y : M₂) : f₁.proj_ker_of_right_inverse f₂ h (f₂ y) = 0 := subtype.ext_iff_val.2 $ by simp [h y] end end ring section division_monoid variables {R M : Type*} /-- A nonzero continuous linear functional is open. -/ protected lemma is_open_map_of_ne_zero [topological_space R] [division_ring R] [has_continuous_sub R] [add_comm_group M] [topological_space M] [has_continuous_add M] [module R M] [has_continuous_smul R M] (f : M →L[R] R) (hf : f ≠ 0) : is_open_map f := let ⟨x, hx⟩ := exists_ne_zero hf in is_open_map.of_sections $ λ y, ⟨λ a, y + (a - f y) • (f x)⁻¹ • x, continuous.continuous_at $ by continuity, by simp, λ a, by simp [hx]⟩ end division_monoid section smul_monoid -- The M's are used for semilinear maps, and the N's for plain linear maps variables {R R₂ R₃ S S₃ : Type*} [semiring R] [semiring R₂] [semiring R₃] [monoid S] [monoid S₃] {M : Type*} [topological_space M] [add_comm_monoid M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [module R₂ M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] [module R₃ M₃] {N₂ : Type*} [topological_space N₂] [add_comm_monoid N₂] [module R N₂] {N₃ : Type*} [topological_space N₃] [add_comm_monoid N₃] [module R N₃] [distrib_mul_action S₃ M₃] [smul_comm_class R₃ S₃ M₃] [has_continuous_const_smul S₃ M₃] [distrib_mul_action S N₃] [smul_comm_class R S N₃] [has_continuous_const_smul S N₃] {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] include σ₁₃ @[simp] lemma smul_comp (c : S₃) (h : M₂ →SL[σ₂₃] M₃) (f : M →SL[σ₁₂] M₂) : (c • h).comp f = c • (h.comp f) := rfl omit σ₁₃ variables [distrib_mul_action S₃ M₂] [has_continuous_const_smul S₃ M₂] [smul_comm_class R₂ S₃ M₂] variables [distrib_mul_action S N₂] [has_continuous_const_smul S N₂] [smul_comm_class R S N₂] @[simp] lemma comp_smul [linear_map.compatible_smul N₂ N₃ S R] (hₗ : N₂ →L[R] N₃) (c : S) (fₗ : M →L[R] N₂) : hₗ.comp (c • fₗ) = c • (hₗ.comp fₗ) := by { ext x, exact hₗ.map_smul_of_tower c (fₗ x) } include σ₁₃ @[simp] lemma comp_smulₛₗ [smul_comm_class R₂ R₂ M₂] [smul_comm_class R₃ R₃ M₃] [has_continuous_const_smul R₂ M₂] [has_continuous_const_smul R₃ M₃] (h : M₂ →SL[σ₂₃] M₃) (c : R₂) (f : M →SL[σ₁₂] M₂) : h.comp (c • f) = (σ₂₃ c) • (h.comp f) := by { ext x, simp only [coe_smul', coe_comp', function.comp_app, pi.smul_apply, continuous_linear_map.map_smulₛₗ] } omit σ₁₃ instance [has_continuous_add M₂] : distrib_mul_action S₃ (M →SL[σ₁₂] M₂) := { smul_add := λ a f g, ext $ λ x, smul_add a (f x) (g x), smul_zero := λ a, ext $ λ x, smul_zero _ } end smul_monoid section smul -- The M's are used for semilinear maps, and the N's for plain linear maps variables {R R₂ R₃ S S₃ : Type*} [semiring R] [semiring R₂] [semiring R₃] [semiring S] [semiring S₃] {M : Type*} [topological_space M] [add_comm_monoid M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] [module R₂ M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] [module R₃ M₃] {N₂ : Type*} [topological_space N₂] [add_comm_monoid N₂] [module R N₂] {N₃ : Type*} [topological_space N₃] [add_comm_monoid N₃] [module R N₃] [module S₃ M₃] [smul_comm_class R₃ S₃ M₃] [has_continuous_const_smul S₃ M₃] [module S N₂] [has_continuous_const_smul S N₂] [smul_comm_class R S N₂] [module S N₃] [smul_comm_class R S N₃] [has_continuous_const_smul S N₃] {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] (c : S) (h : M₂ →SL[σ₂₃] M₃) (f g : M →SL[σ₁₂] M₂) (x y z : M) /-- `continuous_linear_map.prod` as an `equiv`. -/ @[simps apply] def prod_equiv : ((M →L[R] N₂) × (M →L[R] N₃)) ≃ (M →L[R] N₂ × N₃) := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ⟨(fst _ _ _).comp f, (snd _ _ _).comp f⟩, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl } lemma prod_ext_iff {f g : M × N₂ →L[R] N₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := by { simp only [← coe_inj, linear_map.prod_ext_iff], refl } @[ext] lemma prod_ext {f g : M × N₂ →L[R] N₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ variables [has_continuous_add M₂] [has_continuous_add M₃] [has_continuous_add N₂] instance : module S₃ (M →SL[σ₁₃] M₃) := { zero_smul := λ _, ext $ λ _, zero_smul _ _, add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _ } instance [module S₃ᵐᵒᵖ M₃] [is_central_scalar S₃ M₃] : is_central_scalar S₃ (M →SL[σ₁₃] M₃) := { op_smul_eq_smul := λ _ _, ext $ λ _, op_smul_eq_smul _ _ } variables (S) [has_continuous_add N₃] /-- `continuous_linear_map.prod` as a `linear_equiv`. -/ @[simps apply] def prodₗ : ((M →L[R] N₂) × (M →L[R] N₃)) ≃ₗ[S] (M →L[R] N₂ × N₃) := { map_add' := λ f g, rfl, map_smul' := λ c f, rfl, .. prod_equiv } /-- The coercion from `M →L[R] M₂` to `M →ₗ[R] M₂`, as a linear map. -/ @[simps] def coe_lm : (M →L[R] N₃) →ₗ[S] (M →ₗ[R] N₃) := { to_fun := coe, map_add' := λ f g, coe_add f g, map_smul' := λ c f, coe_smul c f } variables {S} (σ₁₃) /-- The coercion from `M →SL[σ] M₂` to `M →ₛₗ[σ] M₂`, as a linear map. -/ @[simps] def coe_lmₛₗ : (M →SL[σ₁₃] M₃) →ₗ[S₃] (M →ₛₗ[σ₁₃] M₃) := { to_fun := coe, map_add' := λ f g, coe_add f g, map_smul' := λ c f, coe_smul c f } variables {σ₁₃} end smul section smul_rightₗ variables {R S T M M₂ : Type*} [semiring R] [semiring S] [semiring T] [module R S] [add_comm_monoid M₂] [module R M₂] [module S M₂] [is_scalar_tower R S M₂] [topological_space S] [topological_space M₂] [has_continuous_smul S M₂] [topological_space M] [add_comm_monoid M] [module R M] [has_continuous_add M₂] [module T M₂] [has_continuous_const_smul T M₂] [smul_comm_class R T M₂] [smul_comm_class S T M₂] /-- Given `c : E →L[𝕜] 𝕜`, `c.smul_rightₗ` is the linear map from `F` to `E →L[𝕜] F` sending `f` to `λ e, c e • f`. See also `continuous_linear_map.smul_rightL`. -/ def smul_rightₗ (c : M →L[R] S) : M₂ →ₗ[T] (M →L[R] M₂) := { to_fun := c.smul_right, map_add' := λ x y, by { ext e, apply smul_add }, map_smul' := λ a x, by { ext e, dsimp, apply smul_comm } } @[simp] lemma coe_smul_rightₗ (c : M →L[R] S) : ⇑(smul_rightₗ c : M₂ →ₗ[T] (M →L[R] M₂)) = c.smul_right := rfl end smul_rightₗ section comm_ring /-- The determinant of a continuous linear map, mainly as a convenience device to be able to write `A.det` instead of `(A : M →ₗ[R] M).det`. -/ @[reducible] noncomputable def det {R : Type*} [comm_ring R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] (A : M →L[R] M) : R := linear_map.det (A : M →ₗ[R] M) variables {R : Type*} [comm_ring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] [has_continuous_const_smul R M₃] variables [topological_add_group M₂] [has_continuous_const_smul R M₂] instance : algebra R (M₂ →L[R] M₂) := algebra.of_module smul_comp (λ _ _ _, comp_smul _ _ _) end comm_ring section restrict_scalars variables {A M M₂ : Type*} [ring A] [add_comm_group M] [add_comm_group M₂] [module A M] [module A M₂] [topological_space M] [topological_space M₂] (R : Type*) [ring R] [module R M] [module R M₂] [linear_map.compatible_smul M M₂ R A] /-- If `A` is an `R`-algebra, then a continuous `A`-linear map can be interpreted as a continuous `R`-linear map. We assume `linear_map.compatible_smul M M₂ R A` to match assumptions of `linear_map.map_smul_of_tower`. -/ def restrict_scalars (f : M →L[A] M₂) : M →L[R] M₂ := ⟨(f : M →ₗ[A] M₂).restrict_scalars R, f.continuous⟩ variable {R} @[simp, norm_cast] lemma coe_restrict_scalars (f : M →L[A] M₂) : (f.restrict_scalars R : M →ₗ[R] M₂) = (f : M →ₗ[A] M₂).restrict_scalars R := rfl @[simp] lemma coe_restrict_scalars' (f : M →L[A] M₂) : ⇑(f.restrict_scalars R) = f := rfl @[simp] lemma restrict_scalars_zero : (0 : M →L[A] M₂).restrict_scalars R = 0 := rfl section variable [topological_add_group M₂] @[simp] lemma restrict_scalars_add (f g : M →L[A] M₂) : (f + g).restrict_scalars R = f.restrict_scalars R + g.restrict_scalars R := rfl @[simp] lemma restrict_scalars_neg (f : M →L[A] M₂) : (-f).restrict_scalars R = -f.restrict_scalars R := rfl end variables {S : Type*} [ring S] [module S M₂] [has_continuous_const_smul S M₂] [smul_comm_class A S M₂] [smul_comm_class R S M₂] @[simp] lemma restrict_scalars_smul (c : S) (f : M →L[A] M₂) : (c • f).restrict_scalars R = c • f.restrict_scalars R := rfl variables (A M M₂ R S) [topological_add_group M₂] /-- `continuous_linear_map.restrict_scalars` as a `linear_map`. See also `continuous_linear_map.restrict_scalarsL`. -/ def restrict_scalarsₗ : (M →L[A] M₂) →ₗ[S] (M →L[R] M₂) := { to_fun := restrict_scalars R, map_add' := restrict_scalars_add, map_smul' := restrict_scalars_smul } variables {A M M₂ R S} @[simp] lemma coe_restrict_scalarsₗ : ⇑(restrict_scalarsₗ A M M₂ R S) = restrict_scalars R := rfl end restrict_scalars end continuous_linear_map namespace continuous_linear_equiv section add_comm_monoid variables {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} [semiring R₁] [semiring R₂] [semiring R₃] {σ₁₂ : R₁ →+* R₂} {σ₂₁ : R₂ →+* R₁} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {σ₂₃ : R₂ →+* R₃} {σ₃₂ : R₃ →+* R₂} [ring_hom_inv_pair σ₂₃ σ₃₂] [ring_hom_inv_pair σ₃₂ σ₂₃] {σ₁₃ : R₁ →+* R₃} {σ₃₁ : R₃ →+* R₁} [ring_hom_inv_pair σ₁₃ σ₃₁] [ring_hom_inv_pair σ₃₁ σ₁₃] [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] {M₁ : Type*} [topological_space M₁] [add_comm_monoid M₁] {M'₁ : Type*} [topological_space M'₁] [add_comm_monoid M'₁] {M₂ : Type*} [topological_space M₂] [add_comm_monoid M₂] {M₃ : Type*} [topological_space M₃] [add_comm_monoid M₃] {M₄ : Type*} [topological_space M₄] [add_comm_monoid M₄] [module R₁ M₁] [module R₁ M'₁] [module R₂ M₂] [module R₃ M₃] include σ₂₁ /-- A continuous linear equivalence induces a continuous linear map. -/ def to_continuous_linear_map (e : M₁ ≃SL[σ₁₂] M₂) : M₁ →SL[σ₁₂] M₂ := { cont := e.continuous_to_fun, ..e.to_linear_equiv.to_linear_map } /-- Coerce continuous linear equivs to continuous linear maps. -/ instance : has_coe (M₁ ≃SL[σ₁₂] M₂) (M₁ →SL[σ₁₂] M₂) := ⟨to_continuous_linear_map⟩ instance : continuous_semilinear_equiv_class (M₁ ≃SL[σ₁₂] M₂) σ₁₂ M₁ M₂ := { coe := λ f, f, inv := λ f, f.inv_fun, coe_injective' := λ f g h₁ h₂, by { cases f with f' _, cases g with g' _, cases f', cases g', congr' }, left_inv := λ f, f.left_inv, right_inv := λ f, f.right_inv, map_add := λ f, f.map_add', map_smulₛₗ := λ f, f.map_smul', map_continuous := continuous_to_fun, inv_continuous := continuous_inv_fun } /-- Coerce continuous linear equivs to maps. -/ -- see Note [function coercion] instance : has_coe_to_fun (M₁ ≃SL[σ₁₂] M₂) (λ _, M₁ → M₂) := ⟨λ f, f⟩ @[simp] theorem coe_def_rev (e : M₁ ≃SL[σ₁₂] M₂) : e.to_continuous_linear_map = e := rfl theorem coe_apply (e : M₁ ≃SL[σ₁₂] M₂) (b : M₁) : (e : M₁ →SL[σ₁₂] M₂) b = e b := rfl @[simp] lemma coe_to_linear_equiv (f : M₁ ≃SL[σ₁₂] M₂) : ⇑f.to_linear_equiv = f := rfl @[simp, norm_cast] lemma coe_coe (e : M₁ ≃SL[σ₁₂] M₂) : ⇑(e : M₁ →SL[σ₁₂] M₂) = e := rfl lemma to_linear_equiv_injective : function.injective (to_linear_equiv : (M₁ ≃SL[σ₁₂] M₂) → (M₁ ≃ₛₗ[σ₁₂] M₂)) | ⟨e, _, _⟩ ⟨e', _, _⟩ rfl := rfl @[ext] lemma ext {f g : M₁ ≃SL[σ₁₂] M₂} (h : (f : M₁ → M₂) = g) : f = g := to_linear_equiv_injective $ linear_equiv.ext $ congr_fun h lemma coe_injective : function.injective (coe : (M₁ ≃SL[σ₁₂] M₂) → (M₁ →SL[σ₁₂] M₂)) := λ e e' h, ext $ funext $ continuous_linear_map.ext_iff.1 h @[simp, norm_cast] lemma coe_inj {e e' : M₁ ≃SL[σ₁₂] M₂} : (e : M₁ →SL[σ₁₂] M₂) = e' ↔ e = e' := coe_injective.eq_iff /-- A continuous linear equivalence induces a homeomorphism. -/ def to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : M₁ ≃ₜ M₂ := { to_equiv := e.to_linear_equiv.to_equiv, ..e } @[simp] lemma coe_to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : ⇑e.to_homeomorph = e := rfl lemma image_closure (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e '' closure s = closure (e '' s) := e.to_homeomorph.image_closure s lemma preimage_closure (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e ⁻¹' closure s = closure (e ⁻¹' s) := e.to_homeomorph.preimage_closure s @[simp] lemma is_closed_image (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} : is_closed (e '' s) ↔ is_closed s := e.to_homeomorph.is_closed_image lemma map_nhds_eq (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : map e (𝓝 x) = 𝓝 (e x) := e.to_homeomorph.map_nhds_eq x -- Make some straightforward lemmas available to `simp`. @[simp] lemma map_zero (e : M₁ ≃SL[σ₁₂] M₂) : e (0 : M₁) = 0 := (e : M₁ →SL[σ₁₂] M₂).map_zero @[simp] lemma map_add (e : M₁ ≃SL[σ₁₂] M₂) (x y : M₁) : e (x + y) = e x + e y := (e : M₁ →SL[σ₁₂] M₂).map_add x y @[simp] lemma map_smulₛₗ (e : M₁ ≃SL[σ₁₂] M₂) (c : R₁) (x : M₁) : e (c • x) = σ₁₂ c • (e x) := (e : M₁ →SL[σ₁₂] M₂).map_smulₛₗ c x omit σ₂₁ @[simp] lemma map_smul [module R₁ M₂] (e : M₁ ≃L[R₁] M₂) (c : R₁) (x : M₁) : e (c • x) = c • (e x) := (e : M₁ →L[R₁] M₂).map_smul c x include σ₂₁ @[simp] lemma map_eq_zero_iff (e : M₁ ≃SL[σ₁₂] M₂) {x : M₁} : e x = 0 ↔ x = 0 := e.to_linear_equiv.map_eq_zero_iff attribute [continuity] continuous_linear_equiv.continuous_to_fun continuous_linear_equiv.continuous_inv_fun @[continuity] protected lemma continuous (e : M₁ ≃SL[σ₁₂] M₂) : continuous (e : M₁ → M₂) := e.continuous_to_fun protected lemma continuous_on (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} : continuous_on (e : M₁ → M₂) s := e.continuous.continuous_on protected lemma continuous_at (e : M₁ ≃SL[σ₁₂] M₂) {x : M₁} : continuous_at (e : M₁ → M₂) x := e.continuous.continuous_at protected lemma continuous_within_at (e : M₁ ≃SL[σ₁₂] M₂) {s : set M₁} {x : M₁} : continuous_within_at (e : M₁ → M₂) s x := e.continuous.continuous_within_at lemma comp_continuous_on_iff {α : Type*} [topological_space α] (e : M₁ ≃SL[σ₁₂] M₂) {f : α → M₁} {s : set α} : continuous_on (e ∘ f) s ↔ continuous_on f s := e.to_homeomorph.comp_continuous_on_iff _ _ lemma comp_continuous_iff {α : Type*} [topological_space α] (e : M₁ ≃SL[σ₁₂] M₂) {f : α → M₁} : continuous (e ∘ f) ↔ continuous f := e.to_homeomorph.comp_continuous_iff omit σ₂₁ /-- An extensionality lemma for `R ≃L[R] M`. -/ lemma ext₁ [topological_space R₁] {f g : R₁ ≃L[R₁] M₁} (h : f 1 = g 1) : f = g := ext $ funext $ λ x, mul_one x ▸ by rw [← smul_eq_mul, map_smul, h, map_smul] section variables (R₁ M₁) /-- The identity map as a continuous linear equivalence. -/ @[refl] protected def refl : M₁ ≃L[R₁] M₁ := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. linear_equiv.refl R₁ M₁ } end @[simp, norm_cast] lemma coe_refl : ↑(continuous_linear_equiv.refl R₁ M₁) = continuous_linear_map.id R₁ M₁ := rfl @[simp, norm_cast] lemma coe_refl' : ⇑(continuous_linear_equiv.refl R₁ M₁) = id := rfl /-- The inverse of a continuous linear equivalence as a continuous linear equivalence-/ @[symm] protected def symm (e : M₁ ≃SL[σ₁₂] M₂) : M₂ ≃SL[σ₂₁] M₁ := { continuous_to_fun := e.continuous_inv_fun, continuous_inv_fun := e.continuous_to_fun, .. e.to_linear_equiv.symm } include σ₂₁ @[simp] lemma symm_to_linear_equiv (e : M₁ ≃SL[σ₁₂] M₂) : e.symm.to_linear_equiv = e.to_linear_equiv.symm := by { ext, refl } @[simp] lemma symm_to_homeomorph (e : M₁ ≃SL[σ₁₂] M₂) : e.to_homeomorph.symm = e.symm.to_homeomorph := rfl /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : M₁ ≃SL[σ₁₂] M₂) : M₁ → M₂ := h /-- See Note [custom simps projection] -/ def simps.symm_apply (h : M₁ ≃SL[σ₁₂] M₂) : M₂ → M₁ := h.symm initialize_simps_projections continuous_linear_equiv (to_linear_equiv_to_fun → apply, to_linear_equiv_inv_fun → symm_apply) lemma symm_map_nhds_eq (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : map e.symm (𝓝 (e x)) = 𝓝 x := e.to_homeomorph.symm_map_nhds_eq x omit σ₂₁ include σ₂₁ σ₃₂ σ₃₁ /-- The composition of two continuous linear equivalences as a continuous linear equivalence. -/ @[trans] protected def trans (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) : M₁ ≃SL[σ₁₃] M₃ := { continuous_to_fun := e₂.continuous_to_fun.comp e₁.continuous_to_fun, continuous_inv_fun := e₁.continuous_inv_fun.comp e₂.continuous_inv_fun, .. e₁.to_linear_equiv.trans e₂.to_linear_equiv } include σ₁₃ @[simp] lemma trans_to_linear_equiv (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) : (e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := by { ext, refl } omit σ₁₃ σ₂₁ σ₃₂ σ₃₁ /-- Product of two continuous linear equivalences. The map comes from `equiv.prod_congr`. -/ def prod [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) : (M₁ × M₃) ≃L[R₁] (M₂ × M₄) := { continuous_to_fun := e.continuous_to_fun.prod_map e'.continuous_to_fun, continuous_inv_fun := e.continuous_inv_fun.prod_map e'.continuous_inv_fun, .. e.to_linear_equiv.prod e'.to_linear_equiv } @[simp, norm_cast] lemma prod_apply [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) (x) : e.prod e' x = (e x.1, e' x.2) := rfl @[simp, norm_cast] lemma coe_prod [module R₁ M₂] [module R₁ M₃] [module R₁ M₄] (e : M₁ ≃L[R₁] M₂) (e' : M₃ ≃L[R₁] M₄) : (e.prod e' : (M₁ × M₃) →L[R₁] (M₂ × M₄)) = (e : M₁ →L[R₁] M₂).prod_map (e' : M₃ →L[R₁] M₄) := rfl include σ₂₁ theorem bijective (e : M₁ ≃SL[σ₁₂] M₂) : function.bijective e := e.to_linear_equiv.to_equiv.bijective theorem injective (e : M₁ ≃SL[σ₁₂] M₂) : function.injective e := e.to_linear_equiv.to_equiv.injective theorem surjective (e : M₁ ≃SL[σ₁₂] M₂) : function.surjective e := e.to_linear_equiv.to_equiv.surjective include σ₃₂ σ₃₁ σ₁₃ @[simp] theorem trans_apply (e₁ : M₁ ≃SL[σ₁₂] M₂) (e₂ : M₂ ≃SL[σ₂₃] M₃) (c : M₁) : (e₁.trans e₂) c = e₂ (e₁ c) := rfl omit σ₃₂ σ₃₁ σ₁₃ @[simp] theorem apply_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) (c : M₂) : e (e.symm c) = c := e.1.right_inv c @[simp] theorem symm_apply_apply (e : M₁ ≃SL[σ₁₂] M₂) (b : M₁) : e.symm (e b) = b := e.1.left_inv b include σ₁₂ σ₂₃ σ₁₃ σ₃₁ @[simp] theorem symm_trans_apply (e₁ : M₂ ≃SL[σ₂₁] M₁) (e₂ : M₃ ≃SL[σ₃₂] M₂) (c : M₁) : (e₂.trans e₁).symm c = e₂.symm (e₁.symm c) := rfl omit σ₁₂ σ₂₃ σ₁₃ σ₃₁ @[simp] theorem symm_image_image (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e.symm '' (e '' s) = s := e.to_linear_equiv.to_equiv.symm_image_image s @[simp] theorem image_symm_image (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e '' (e.symm '' s) = s := e.symm.symm_image_image s include σ₃₂ σ₃₁ @[simp, norm_cast] lemma comp_coe (f : M₁ ≃SL[σ₁₂] M₂) (f' : M₂ ≃SL[σ₂₃] M₃) : (f' : M₂ →SL[σ₂₃] M₃).comp (f : M₁ →SL[σ₁₂] M₂) = (f.trans f' : M₁ →SL[σ₁₃] M₃) := rfl omit σ₃₂ σ₃₁ σ₂₁ @[simp] theorem coe_comp_coe_symm (e : M₁ ≃SL[σ₁₂] M₂) : (e : M₁ →SL[σ₁₂] M₂).comp (e.symm : M₂ →SL[σ₂₁] M₁) = continuous_linear_map.id R₂ M₂ := continuous_linear_map.ext e.apply_symm_apply @[simp] theorem coe_symm_comp_coe (e : M₁ ≃SL[σ₁₂] M₂) : (e.symm : M₂ →SL[σ₂₁] M₁).comp (e : M₁ →SL[σ₁₂] M₂) = continuous_linear_map.id R₁ M₁ := continuous_linear_map.ext e.symm_apply_apply include σ₂₁ @[simp] lemma symm_comp_self (e : M₁ ≃SL[σ₁₂] M₂) : (e.symm : M₂ → M₁) ∘ (e : M₁ → M₂) = id := by{ ext x, exact symm_apply_apply e x } @[simp] lemma self_comp_symm (e : M₁ ≃SL[σ₁₂] M₂) : (e : M₁ → M₂) ∘ (e.symm : M₂ → M₁) = id := by{ ext x, exact apply_symm_apply e x } @[simp] theorem symm_symm (e : M₁ ≃SL[σ₁₂] M₂) : e.symm.symm = e := by { ext x, refl } omit σ₂₁ @[simp] lemma refl_symm : (continuous_linear_equiv.refl R₁ M₁).symm = continuous_linear_equiv.refl R₁ M₁ := rfl include σ₂₁ theorem symm_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) (x : M₁) : e.symm.symm x = e x := rfl lemma symm_apply_eq (e : M₁ ≃SL[σ₁₂] M₂) {x y} : e.symm x = y ↔ x = e y := e.to_linear_equiv.symm_apply_eq lemma eq_symm_apply (e : M₁ ≃SL[σ₁₂] M₂) {x y} : y = e.symm x ↔ e y = x := e.to_linear_equiv.eq_symm_apply protected lemma image_eq_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e '' s = e.symm ⁻¹' s := e.to_linear_equiv.to_equiv.image_eq_preimage s protected lemma image_symm_eq_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e.symm '' s = e ⁻¹' s := by rw [e.symm.image_eq_preimage, e.symm_symm] @[simp] protected lemma symm_preimage_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₂) : e.symm ⁻¹' (e ⁻¹' s) = s := e.to_linear_equiv.to_equiv.symm_preimage_preimage s @[simp] protected lemma preimage_symm_preimage (e : M₁ ≃SL[σ₁₂] M₂) (s : set M₁) : e ⁻¹' (e.symm ⁻¹' s) = s := e.symm.symm_preimage_preimage s protected lemma uniform_embedding {E₁ E₂ : Type*} [uniform_space E₁] [uniform_space E₂] [add_comm_group E₁] [add_comm_group E₂] [module R₁ E₁] [module R₂ E₂] [uniform_add_group E₁] [uniform_add_group E₂] (e : E₁ ≃SL[σ₁₂] E₂) : uniform_embedding e := e.to_linear_equiv.to_equiv.uniform_embedding e.to_continuous_linear_map.uniform_continuous e.symm.to_continuous_linear_map.uniform_continuous protected lemma _root_.linear_equiv.uniform_embedding {E₁ E₂ : Type*} [uniform_space E₁] [uniform_space E₂] [add_comm_group E₁] [add_comm_group E₂] [module R₁ E₁] [module R₂ E₂] [uniform_add_group E₁] [uniform_add_group E₂] (e : E₁ ≃ₛₗ[σ₁₂] E₂) (h₁ : continuous e) (h₂ : continuous e.symm) : uniform_embedding e := continuous_linear_equiv.uniform_embedding ({ continuous_to_fun := h₁, continuous_inv_fun := h₂, .. e } : E₁ ≃SL[σ₁₂] E₂) omit σ₂₁ /-- Create a `continuous_linear_equiv` from two `continuous_linear_map`s that are inverse of each other. -/ def equiv_of_inverse (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ : M₂ →SL[σ₂₁] M₁) (h₁ : function.left_inverse f₂ f₁) (h₂ : function.right_inverse f₂ f₁) : M₁ ≃SL[σ₁₂] M₂ := { to_fun := f₁, continuous_to_fun := f₁.continuous, inv_fun := f₂, continuous_inv_fun := f₂.continuous, left_inv := h₁, right_inv := h₂, .. f₁ } include σ₂₁ @[simp] lemma equiv_of_inverse_apply (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ h₁ h₂ x) : equiv_of_inverse f₁ f₂ h₁ h₂ x = f₁ x := rfl @[simp] lemma symm_equiv_of_inverse (f₁ : M₁ →SL[σ₁₂] M₂) (f₂ h₁ h₂) : (equiv_of_inverse f₁ f₂ h₁ h₂).symm = equiv_of_inverse f₂ f₁ h₂ h₁ := rfl omit σ₂₁ variable (M₁) /-- The continuous linear equivalences from `M` to itself form a group under composition. -/ instance automorphism_group : group (M₁ ≃L[R₁] M₁) := { mul := λ f g, g.trans f, one := continuous_linear_equiv.refl R₁ M₁, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } variables {M₁} {R₄ : Type*} [semiring R₄] [module R₄ M₄] {σ₃₄ : R₃ →+* R₄} {σ₄₃ : R₄ →+* R₃} [ring_hom_inv_pair σ₃₄ σ₄₃] [ring_hom_inv_pair σ₄₃ σ₃₄] {σ₂₄ : R₂ →+* R₄} {σ₁₄ : R₁ →+* R₄} [ring_hom_comp_triple σ₂₁ σ₁₄ σ₂₄] [ring_hom_comp_triple σ₂₄ σ₄₃ σ₂₃] [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] include σ₂₁ σ₃₄ σ₂₃ σ₂₄ σ₁₃ /-- A pair of continuous (semi)linear equivalences generates an equivalence between the spaces of continuous linear maps. See also `continuous_linear_equiv.arrow_congr`. -/ @[simps] def arrow_congr_equiv (e₁₂ : M₁ ≃SL[σ₁₂] M₂) (e₄₃ : M₄ ≃SL[σ₄₃] M₃) : (M₁ →SL[σ₁₄] M₄) ≃ (M₂ →SL[σ₂₃] M₃) := { to_fun := λ f, (e₄₃ : M₄ →SL[σ₄₃] M₃).comp (f.comp (e₁₂.symm : M₂ →SL[σ₂₁] M₁)), inv_fun := λ f, (e₄₃.symm : M₃ →SL[σ₃₄] M₄).comp (f.comp (e₁₂ : M₁ →SL[σ₁₂] M₂)), left_inv := λ f, continuous_linear_map.ext $ λ x, by simp only [continuous_linear_map.comp_apply, symm_apply_apply, coe_coe], right_inv := λ f, continuous_linear_map.ext $ λ x, by simp only [continuous_linear_map.comp_apply, apply_symm_apply, coe_coe] } end add_comm_monoid section add_comm_group variables {R : Type*} [semiring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R M₂] [module R M₃] [module R M₄] variables [topological_add_group M₄] /-- Equivalence given by a block lower diagonal matrix. `e` and `e'` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ def skew_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) : (M × M₃) ≃L[R] M₂ × M₄ := { continuous_to_fun := (e.continuous_to_fun.comp continuous_fst).prod_mk ((e'.continuous_to_fun.comp continuous_snd).add $ f.continuous.comp continuous_fst), continuous_inv_fun := (e.continuous_inv_fun.comp continuous_fst).prod_mk (e'.continuous_inv_fun.comp $ continuous_snd.sub $ f.continuous.comp $ e.continuous_inv_fun.comp continuous_fst), .. e.to_linear_equiv.skew_prod e'.to_linear_equiv ↑f } @[simp] lemma skew_prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) : e.skew_prod e' f x = (e x.1, e' x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) : (e.skew_prod e' f).symm x = (e.symm x.1, e'.symm (x.2 - f (e.symm x.1))) := rfl end add_comm_group section ring variables {R : Type*} [ring R] {R₂ : Type*} [ring R₂] {M : Type*} [topological_space M] [add_comm_group M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R₂ M₂] variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ @[simp] lemma map_sub (e : M ≃SL[σ₁₂] M₂) (x y : M) : e (x - y) = e x - e y := (e : M →SL[σ₁₂] M₂).map_sub x y @[simp] lemma map_neg (e : M ≃SL[σ₁₂] M₂) (x : M) : e (-x) = -e x := (e : M →SL[σ₁₂] M₂).map_neg x omit σ₂₁ section /-! The next theorems cover the identification between `M ≃L[𝕜] M`and the group of units of the ring `M →L[R] M`. -/ variables [topological_add_group M] /-- An invertible continuous linear map `f` determines a continuous equivalence from `M` to itself. -/ def of_unit (f : (M →L[R] M)ˣ) : (M ≃L[R] M) := { to_linear_equiv := { to_fun := f.val, map_add' := by simp, map_smul' := by simp, inv_fun := f.inv, left_inv := λ x, show (f.inv * f.val) x = x, by {rw f.inv_val, simp}, right_inv := λ x, show (f.val * f.inv) x = x, by {rw f.val_inv, simp}, }, continuous_to_fun := f.val.continuous, continuous_inv_fun := f.inv.continuous } /-- A continuous equivalence from `M` to itself determines an invertible continuous linear map. -/ def to_unit (f : (M ≃L[R] M)) : (M →L[R] M)ˣ := { val := f, inv := f.symm, val_inv := by {ext, simp}, inv_val := by {ext, simp} } variables (R M) /-- The units of the algebra of continuous `R`-linear endomorphisms of `M` is multiplicatively equivalent to the type of continuous linear equivalences between `M` and itself. -/ def units_equiv : (M →L[R] M)ˣ ≃* (M ≃L[R] M) := { to_fun := of_unit, inv_fun := to_unit, left_inv := λ f, by {ext, refl}, right_inv := λ f, by {ext, refl}, map_mul' := λ x y, by {ext, refl} } @[simp] lemma units_equiv_apply (f : (M →L[R] M)ˣ) (x : M) : units_equiv R M f x = f x := rfl end section variables (R) [topological_space R] [has_continuous_mul R] /-- Continuous linear equivalences `R ≃L[R] R` are enumerated by `Rˣ`. -/ def units_equiv_aut : Rˣ ≃ (R ≃L[R] R) := { to_fun := λ u, equiv_of_inverse (continuous_linear_map.smul_right (1 : R →L[R] R) ↑u) (continuous_linear_map.smul_right (1 : R →L[R] R) ↑u⁻¹) (λ x, by simp) (λ x, by simp), inv_fun := λ e, ⟨e 1, e.symm 1, by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, symm_apply_apply], by rw [← smul_eq_mul, ← map_smul, smul_eq_mul, mul_one, apply_symm_apply]⟩, left_inv := λ u, units.ext $ by simp, right_inv := λ e, ext₁ $ by simp } variable {R} @[simp] lemma units_equiv_aut_apply (u : Rˣ) (x : R) : units_equiv_aut R u x = x * u := rfl @[simp] lemma units_equiv_aut_apply_symm (u : Rˣ) (x : R) : (units_equiv_aut R u).symm x = x * ↑u⁻¹ := rfl @[simp] lemma units_equiv_aut_symm_apply (e : R ≃L[R] R) : ↑((units_equiv_aut R).symm e) = e 1 := rfl end variables [module R M₂] [topological_add_group M] open _root_.continuous_linear_map (id fst snd mem_ker) /-- A pair of continuous linear maps such that `f₁ ∘ f₂ = id` generates a continuous linear equivalence `e` between `M` and `M₂ × f₁.ker` such that `(e x).2 = x` for `x ∈ f₁.ker`, `(e x).1 = f₁ x`, and `(e (f₂ y)).2 = 0`. The map is given by `e x = (f₁ x, x - f₂ (f₁ x))`. -/ def equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) : M ≃L[R] M₂ × f₁.ker := equiv_of_inverse (f₁.prod (f₁.proj_ker_of_right_inverse f₂ h)) (f₂.coprod f₁.ker.subtypeL) (λ x, by simp) (λ ⟨x, y⟩, by simp [h x]) @[simp] lemma fst_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) : (equiv_of_right_inverse f₁ f₂ h x).1 = f₁ x := rfl @[simp] lemma snd_equiv_of_right_inverse (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (x : M) : ((equiv_of_right_inverse f₁ f₂ h x).2 : M) = x - f₂ (f₁ x) := rfl @[simp] lemma equiv_of_right_inverse_symm_apply (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) (y : M₂ × f₁.ker) : (equiv_of_right_inverse f₁ f₂ h).symm y = f₂ y.1 + y.2 := rfl end ring section variables (ι R M : Type*) [unique ι] [semiring R] [add_comm_monoid M] [module R M] [topological_space M] /-- If `ι` has a unique element, then `ι → M` is continuously linear equivalent to `M`. -/ def fun_unique : (ι → M) ≃L[R] M := { to_linear_equiv := linear_equiv.fun_unique ι R M, .. homeomorph.fun_unique ι M } variables {ι R M} @[simp] lemma coe_fun_unique : ⇑(fun_unique ι R M) = function.eval default := rfl @[simp] lemma coe_fun_unique_symm : ⇑(fun_unique ι R M).symm = function.const ι := rfl variables (R M) /-- Continuous linear equivalence between dependent functions `Π i : fin 2, M i` and `M 0 × M 1`. -/ @[simps { fully_applied := ff }] def pi_fin_two (M : fin 2 → Type*) [Π i, add_comm_monoid (M i)] [Π i, module R (M i)] [Π i, topological_space (M i)] : (Π i, M i) ≃L[R] M 0 × M 1 := { to_linear_equiv := linear_equiv.pi_fin_two R M, .. homeomorph.pi_fin_two M } /-- Continuous linear equivalence between vectors in `M² = fin 2 → M` and `M × M`. -/ @[simps { fully_applied := ff }] def fin_two_arrow : (fin 2 → M) ≃L[R] M × M := { to_linear_equiv := linear_equiv.fin_two_arrow R M, .. pi_fin_two R (λ _, M) } end @[simp] lemma det_coe_symm {R : Type*} [field R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] (A : M ≃L[R] M) : (A.symm : M →L[R] M).det = (A : M →L[R] M).det ⁻¹ := linear_equiv.det_coe_symm A.to_linear_equiv end continuous_linear_equiv namespace continuous_linear_map open_locale classical variables {R : Type*} {M : Type*} {M₂ : Type*} [topological_space M] [topological_space M₂] section variables [semiring R] variables [add_comm_monoid M₂] [module R M₂] variables [add_comm_monoid M] [module R M] /-- Introduce a function `inverse` from `M →L[R] M₂` to `M₂ →L[R] M`, which sends `f` to `f.symm` if `f` is a continuous linear equivalence and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather than partially) defined inverse function for some purposes, including for calculus. -/ noncomputable def inverse : (M →L[R] M₂) → (M₂ →L[R] M) := λ f, if h : ∃ (e : M ≃L[R] M₂), (e : M →L[R] M₂) = f then ((classical.some h).symm : M₂ →L[R] M) else 0 /-- By definition, if `f` is invertible then `inverse f = f.symm`. -/ @[simp] lemma inverse_equiv (e : M ≃L[R] M₂) : inverse (e : M →L[R] M₂) = e.symm := begin have h : ∃ (e' : M ≃L[R] M₂), (e' : M →L[R] M₂) = ↑e := ⟨e, rfl⟩, simp only [inverse, dif_pos h], congr, exact_mod_cast (classical.some_spec h) end /-- By definition, if `f` is not invertible then `inverse f = 0`. -/ @[simp] lemma inverse_non_equiv (f : M →L[R] M₂) (h : ¬∃ (e' : M ≃L[R] M₂), ↑e' = f) : inverse f = 0 := dif_neg h end section variables [ring R] variables [add_comm_group M] [topological_add_group M] [module R M] variables [add_comm_group M₂] [module R M₂] @[simp] lemma ring_inverse_equiv (e : M ≃L[R] M) : ring.inverse ↑e = inverse (e : M →L[R] M) := begin suffices : ring.inverse ((((continuous_linear_equiv.units_equiv _ _).symm e) : M →L[R] M)) = inverse ↑e, { convert this }, simp, refl, end /-- The function `continuous_linear_equiv.inverse` can be written in terms of `ring.inverse` for the ring of self-maps of the domain. -/ lemma to_ring_inverse (e : M ≃L[R] M₂) (f : M →L[R] M₂) : inverse f = (ring.inverse ((e.symm : (M₂ →L[R] M)).comp f)) ∘L ↑e.symm := begin by_cases h₁ : ∃ (e' : M ≃L[R] M₂), ↑e' = f, { obtain ⟨e', he'⟩ := h₁, rw ← he', change _ = (ring.inverse ↑(e'.trans e.symm)) ∘L ↑e.symm, ext, simp }, { suffices : ¬is_unit ((e.symm : M₂ →L[R] M).comp f), { simp [this, h₁] }, contrapose! h₁, rcases h₁ with ⟨F, hF⟩, use (continuous_linear_equiv.units_equiv _ _ F).trans e, ext, dsimp, rw [coe_fn_coe_base' F, hF], simp } end lemma ring_inverse_eq_map_inverse : ring.inverse = @inverse R M M _ _ _ _ _ _ _ := begin ext, simp [to_ring_inverse (continuous_linear_equiv.refl R M)], end end end continuous_linear_map namespace submodule variables {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] [module R M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M₂] open continuous_linear_map /-- A submodule `p` is called *complemented* if there exists a continuous projection `M →ₗ[R] p`. -/ def closed_complemented (p : submodule R M) : Prop := ∃ f : M →L[R] p, ∀ x : p, f x = x lemma closed_complemented.has_closed_complement {p : submodule R M} [t1_space p] (h : closed_complemented p) : ∃ (q : submodule R M) (hq : is_closed (q : set M)), is_compl p q := exists.elim h $ λ f hf, ⟨f.ker, f.is_closed_ker, linear_map.is_compl_of_proj hf⟩ protected lemma closed_complemented.is_closed [topological_add_group M] [t1_space M] {p : submodule R M} (h : closed_complemented p) : is_closed (p : set M) := begin rcases h with ⟨f, hf⟩, have : ker (id R M - p.subtypeL.comp f) = p := linear_map.ker_id_sub_eq_of_proj hf, exact this ▸ (is_closed_ker _) end @[simp] lemma closed_complemented_bot : closed_complemented (⊥ : submodule R M) := ⟨0, λ x, by simp only [zero_apply, eq_zero_of_bot_submodule x]⟩ @[simp] lemma closed_complemented_top : closed_complemented (⊤ : submodule R M) := ⟨(id R M).cod_restrict ⊤ (λ x, trivial), λ x, subtype.ext_iff_val.2 $ by simp⟩ end submodule lemma continuous_linear_map.closed_complemented_ker_of_right_inverse {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] [module R M] [module R M₂] [topological_add_group M] (f₁ : M →L[R] M₂) (f₂ : M₂ →L[R] M) (h : function.right_inverse f₂ f₁) : f₁.ker.closed_complemented := ⟨f₁.proj_ker_of_right_inverse f₂ h, f₁.proj_ker_of_right_inverse_apply_idem f₂ h⟩ section quotient namespace submodule variables {R M : Type*} [ring R] [add_comm_group M] [module R M] [topological_space M] (S : submodule R M) lemma is_open_map_mkq [topological_add_group M] : is_open_map S.mkq := quotient_add_group.is_open_map_coe S.to_add_subgroup instance topological_add_group_quotient [topological_add_group M] : topological_add_group (M ⧸ S) := topological_add_group_quotient S.to_add_subgroup instance has_continuous_smul_quotient [topological_space R] [topological_add_group M] [has_continuous_smul R M] : has_continuous_smul R (M ⧸ S) := begin split, have quot : quotient_map (λ au : R × M, (au.1, S.mkq au.2)), from is_open_map.to_quotient_map (is_open_map.id.prod S.is_open_map_mkq) (continuous_id.prod_map continuous_quot_mk) (function.surjective_id.prod_map $ surjective_quot_mk _), rw quot.continuous_iff, exact continuous_quot_mk.comp continuous_smul end instance t3_quotient_of_is_closed [topological_add_group M] [is_closed (S : set M)] : t3_space (M ⧸ S) := begin letI : is_closed (S.to_add_subgroup : set M) := ‹_›, exact S.to_add_subgroup.t3_quotient_of_is_closed end end submodule end quotient
ef55cbb4d73ec48b17abdb0874bd0566c6d02568
4727251e0cd73359b15b664c3170e5d754078599
/src/model_theory/basic.lean
47caf0a815deea02d34402b7bd75d6b7a1014dbe
[ "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
29,860
lean
/- Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn -/ import category_theory.concrete_category.bundled import data.fin.tuple.basic import data.fin.vec_notation import logic.encodable.basic import logic.small import set_theory.cardinal.basic /-! # Basics on First-Order Structures This file defines first-order languages and structures in the style of the [Flypitch project](https://flypitch.github.io/), as well as several important maps between structures. ## Main Definitions * A `first_order.language` defines a language as a pair of functions from the natural numbers to `Type l`. One sends `n` to the type of `n`-ary functions, and the other sends `n` to the type of `n`-ary relations. * A `first_order.language.Structure` interprets the symbols of a given `first_order.language` in the context of a given type. * A `first_order.language.hom`, denoted `M →[L] N`, is a map from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations (although only in the forward direction). * A `first_order.language.embedding`, denoted `M ↪[L] N`, is an embedding from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. * A `first_order.language.elementary_embedding`, denoted `M ↪ₑ[L] N`, is an embedding from the `L`-structure `M` to the `L`-structure `N` that commutes with the realizations of all formulas. * A `first_order.language.equiv`, denoted `M ≃[L] N`, is an equivalence from the `L`-structure `M` to the `L`-structure `N` that commutes with the interpretations of functions, and which preserves the interpretations of relations in both directions. ## References For the Flypitch project: - [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*] [flypitch_cpp] - [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of the continuum hypothesis*][flypitch_itp] -/ universes u v u' v' w w' open_locale cardinal open cardinal namespace first_order /-! ### Languages and Structures -/ /-- A first-order language consists of a type of functions of every natural-number arity and a type of relations of every natural-number arity. -/ @[nolint check_univs] -- intended to be used with explicit universe parameters structure language := (functions : ℕ → Type u) (relations : ℕ → Type v) /-- Used to define `first_order.language₂`. -/ @[simp] def sequence₂ (a₀ a₁ a₂ : Type u) : ℕ → Type u | 0 := a₀ | 1 := a₁ | 2 := a₂ | _ := pempty namespace sequence₂ variables (a₀ a₁ a₂ : Type u) instance inhabited₀ [h : inhabited a₀] : inhabited (sequence₂ a₀ a₁ a₂ 0) := h instance inhabited₁ [h : inhabited a₁] : inhabited (sequence₂ a₀ a₁ a₂ 1) := h instance inhabited₂ [h : inhabited a₂] : inhabited (sequence₂ a₀ a₁ a₂ 2) := h instance {n : ℕ} : is_empty (sequence₂ a₀ a₁ a₂ (n + 3)) := pempty.is_empty @[simp] lemma lift_mk {i : ℕ} : cardinal.lift (# (sequence₂ a₀ a₁ a₂ i)) = # (sequence₂ (ulift a₀) (ulift a₁) (ulift a₂) i) := begin rcases i with (_ | _ | _ | i); simp only [sequence₂, mk_ulift, mk_fintype, fintype.card_of_is_empty, nat.cast_zero, lift_zero], end @[simp] lemma sum_card : cardinal.sum (λ i, # (sequence₂ a₀ a₁ a₂ i)) = # a₀ + # a₁ + # a₂ := begin rw [sum_nat_eq_add_sum_succ, sum_nat_eq_add_sum_succ, sum_nat_eq_add_sum_succ], simp [add_assoc], end end sequence₂ namespace language /-- A constructor for languages with only constants, unary and binary functions, and unary and binary relations. -/ @[simps] protected def mk₂ (c f₁ f₂ : Type u) (r₁ r₂ : Type v) : language := ⟨sequence₂ c f₁ f₂, sequence₂ pempty r₁ r₂⟩ /-- The empty language has no symbols. -/ protected def empty : language := ⟨λ _, empty, λ _, empty⟩ instance : inhabited language := ⟨language.empty⟩ /-- The sum of two languages consists of the disjoint union of their symbols. -/ protected def sum (L : language.{u v}) (L' : language.{u' v'}) : language := ⟨λn, L.functions n ⊕ L'.functions n, λ n, L.relations n ⊕ L'.relations n⟩ variable (L : language.{u v}) /-- The type of constants in a given language. -/ @[nolint has_inhabited_instance] protected def «constants» := L.functions 0 @[simp] lemma constants_mk₂ (c f₁ f₂ : Type u) (r₁ r₂ : Type v) : (language.mk₂ c f₁ f₂ r₁ r₂).constants = c := rfl /-- The type of symbols in a given language. -/ @[nolint has_inhabited_instance] def symbols := (Σl, L.functions l) ⊕ (Σl, L.relations l) /-- The cardinality of a language is the cardinality of its type of symbols. -/ def card : cardinal := # L.symbols /-- A language is countable when it has countably many symbols. -/ class countable : Prop := (card_le_omega' : L.card ≤ ω) lemma card_le_omega [L.countable] : L.card ≤ ω := countable.card_le_omega' /-- A language is relational when it has no function symbols. -/ class is_relational : Prop := (empty_functions : ∀ n, is_empty (L.functions n)) /-- A language is algebraic when it has no relation symbols. -/ class is_algebraic : Prop := (empty_relations : ∀ n, is_empty (L.relations n)) /-- A language is countable when it has countably many symbols. -/ class countable_functions : Prop := (card_functions_le_omega' : # (Σl, L.functions l) ≤ ω) lemma card_functions_le_omega [L.countable_functions] : # (Σl, L.functions l) ≤ ω := countable_functions.card_functions_le_omega' variables {L} {L' : language.{u' v'}} lemma card_eq_card_functions_add_card_relations : L.card = cardinal.sum (λ l, (cardinal.lift.{v} (#(L.functions l)))) + cardinal.sum (λ l, cardinal.lift.{u} (#(L.relations l))) := by simp [card, symbols] instance [L.is_relational] {n : ℕ} : is_empty (L.functions n) := is_relational.empty_functions n instance [L.is_algebraic] {n : ℕ} : is_empty (L.relations n) := is_algebraic.empty_relations n instance is_relational_of_empty_functions {symb : ℕ → Type*} : is_relational ⟨λ _, empty, symb⟩ := ⟨λ _, empty.is_empty⟩ instance is_algebraic_of_empty_relations {symb : ℕ → Type*} : is_algebraic ⟨symb, λ _, empty⟩ := ⟨λ _, empty.is_empty⟩ instance is_relational_empty : is_relational language.empty := language.is_relational_of_empty_functions instance is_algebraic_empty : is_algebraic language.empty := language.is_algebraic_of_empty_relations instance is_relational_sum [L.is_relational] [L'.is_relational] : is_relational (L.sum L') := ⟨λ n, sum.is_empty⟩ instance is_algebraic_sum [L.is_algebraic] [L'.is_algebraic] : is_algebraic (L.sum L') := ⟨λ n, sum.is_empty⟩ instance is_relational_mk₂ {c f₁ f₂ : Type u} {r₁ r₂ : Type v} [h0 : is_empty c] [h1 : is_empty f₁] [h2 : is_empty f₂] : is_relational (language.mk₂ c f₁ f₂ r₁ r₂) := ⟨λ n, nat.cases_on n h0 (λ n, nat.cases_on n h1 (λ n, nat.cases_on n h2 (λ _, pempty.is_empty)))⟩ instance is_algebraic_mk₂ {c f₁ f₂ : Type u} {r₁ r₂ : Type v} [h1 : is_empty r₁] [h2 : is_empty r₂] : is_algebraic (language.mk₂ c f₁ f₂ r₁ r₂) := ⟨λ n, nat.cases_on n pempty.is_empty (λ n, nat.cases_on n h1 (λ n, nat.cases_on n h2 (λ _, pempty.is_empty)))⟩ instance subsingleton_mk₂_functions {c f₁ f₂ : Type u} {r₁ r₂ : Type v} [h0 : subsingleton c] [h1 : subsingleton f₁] [h2 : subsingleton f₂] {n : ℕ} : subsingleton ((language.mk₂ c f₁ f₂ r₁ r₂).functions n) := nat.cases_on n h0 (λ n, nat.cases_on n h1 (λ n, nat.cases_on n h2 (λ n, ⟨λ x, pempty.elim x⟩))) instance subsingleton_mk₂_relations {c f₁ f₂ : Type u} {r₁ r₂ : Type v} [h1 : subsingleton r₁] [h2 : subsingleton r₂] {n : ℕ} : subsingleton ((language.mk₂ c f₁ f₂ r₁ r₂).relations n) := nat.cases_on n ⟨λ x, pempty.elim x⟩ (λ n, nat.cases_on n h1 (λ n, nat.cases_on n h2 (λ n, ⟨λ x, pempty.elim x⟩))) lemma encodable.countable [h : encodable L.symbols] : L.countable := ⟨cardinal.encodable_iff.1 ⟨h⟩⟩ @[simp] lemma empty_card : language.empty.card = 0 := by simp [card_eq_card_functions_add_card_relations] instance countable_empty : language.empty.countable := ⟨by simp⟩ @[priority 100] instance countable.countable_functions [L.countable] : L.countable_functions := ⟨begin refine lift_le_omega.1 (trans _ L.card_le_omega), rw [card, symbols, mk_sum], exact le_self_add, end⟩ lemma encodable.countable_functions [h : encodable (Σl, L.functions l)] : L.countable_functions := ⟨cardinal.encodable_iff.1 ⟨h⟩⟩ @[priority 100] instance is_relational.countable_functions [L.is_relational] : L.countable_functions := encodable.countable_functions @[simp] lemma card_functions_sum (i : ℕ) : #((L.sum L').functions i) = (#(L.functions i)).lift + cardinal.lift.{u} (#(L'.functions i)) := by simp [language.sum] @[simp] lemma card_relations_sum (i : ℕ) : #((L.sum L').relations i) = (#(L.relations i)).lift + cardinal.lift.{v} (#(L'.relations i)) := by simp [language.sum] @[simp] lemma card_sum : (L.sum L').card = cardinal.lift.{max u' v'} L.card + cardinal.lift.{max u v} L'.card := begin simp only [card_eq_card_functions_add_card_relations, card_functions_sum, card_relations_sum, sum_add_distrib', lift_add, lift_sum, lift_lift], rw [add_assoc, ←add_assoc (cardinal.sum (λ i, (# (L'.functions i)).lift)), add_comm (cardinal.sum (λ i, (# (L'.functions i)).lift)), add_assoc, add_assoc] end @[simp] lemma card_mk₂ (c f₁ f₂ : Type u) (r₁ r₂ : Type v) : (language.mk₂ c f₁ f₂ r₁ r₂).card = cardinal.lift.{v} (# c) + cardinal.lift.{v} (# f₁) + cardinal.lift.{v} (# f₂) + cardinal.lift.{u} (# r₁) + cardinal.lift.{u} (# r₂) := by simp [card_eq_card_functions_add_card_relations, add_assoc] variables (L) (M : Type w) /-- A first-order structure on a type `M` consists of interpretations of all the symbols in a given language. Each function of arity `n` is interpreted as a function sending tuples of length `n` (modeled as `(fin n → M)`) to `M`, and a relation of arity `n` is a function from tuples of length `n` to `Prop`. -/ @[ext] class Structure := (fun_map : ∀{n}, L.functions n → (fin n → M) → M) (rel_map : ∀{n}, L.relations n → (fin n → M) → Prop) variables (N : Type w') [L.Structure M] [L.Structure N] open Structure /-- Used for defining `first_order.language.Theory.Model.inhabited`. -/ def trivial_unit_structure : L.Structure unit := ⟨default, default⟩ /-! ### Maps -/ /-- A homomorphism between first-order structures is a function that commutes with the interpretations of functions and maps tuples in one structure where a given relation is true to tuples in the second structure where that relation is still true. -/ structure hom := (to_fun : M → N) (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r x → rel_map r (to_fun ∘ x) . obviously) localized "notation A ` →[`:25 L `] ` B := first_order.language.hom L A B" in first_order /-- An embedding of first-order structures is an embedding that commutes with the interpretations of functions and relations. -/ @[ancestor function.embedding] structure embedding extends M ↪ N := (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r (to_fun ∘ x) ↔ rel_map r x . obviously) localized "notation A ` ↪[`:25 L `] ` B := first_order.language.embedding L A B" in first_order /-- An equivalence of first-order structures is an equivalence that commutes with the interpretations of functions and relations. -/ structure equiv extends M ≃ N := (map_fun' : ∀{n} (f : L.functions n) x, to_fun (fun_map f x) = fun_map f (to_fun ∘ x) . obviously) (map_rel' : ∀{n} (r : L.relations n) x, rel_map r (to_fun ∘ x) ↔ rel_map r x . obviously) localized "notation A ` ≃[`:25 L `] ` B := first_order.language.equiv L A B" in first_order variables {L M N} {P : Type*} [L.Structure P] {Q : Type*} [L.Structure Q] instance : has_coe_t L.constants M := ⟨λ c, fun_map c default⟩ lemma fun_map_eq_coe_constants {c : L.constants} {x : fin 0 → M} : fun_map c x = c := congr rfl (funext fin.elim0) /-- Given a language with a nonempty type of constants, any structure will be nonempty. This cannot be a global instance, because `L` becomes a metavariable. -/ lemma nonempty_of_nonempty_constants [h : nonempty L.constants] : nonempty M := h.map coe /-- The function map for `first_order.language.Structure₂`. -/ def fun_map₂ {c f₁ f₂ : Type u} {r₁ r₂ : Type v} (c' : c → M) (f₁' : f₁ → M → M) (f₂' : f₂ → M → M → M) : ∀{n}, (language.mk₂ c f₁ f₂ r₁ r₂).functions n → (fin n → M) → M | 0 f _ := c' f | 1 f x := f₁' f (x 0) | 2 f x := f₂' f (x 0) (x 1) | (n + 3) f _ := pempty.elim f /-- The relation map for `first_order.language.Structure₂`. -/ def rel_map₂ {c f₁ f₂ : Type u} {r₁ r₂ : Type v} (r₁' : r₁ → set M) (r₂' : r₂ → M → M → Prop) : ∀{n}, (language.mk₂ c f₁ f₂ r₁ r₂).relations n → (fin n → M) → Prop | 0 r _ := pempty.elim r | 1 r x := (x 0) ∈ r₁' r | 2 r x := r₂' r (x 0) (x 1) | (n + 3) r _ := pempty.elim r /-- A structure constructor to match `first_order.language₂`. -/ protected def Structure.mk₂ {c f₁ f₂ : Type u} {r₁ r₂ : Type v} (c' : c → M) (f₁' : f₁ → M → M) (f₂' : f₂ → M → M → M) (r₁' : r₁ → set M) (r₂' : r₂ → M → M → Prop) : (language.mk₂ c f₁ f₂ r₁ r₂).Structure M := ⟨λ _, fun_map₂ c' f₁' f₂', λ _, rel_map₂ r₁' r₂'⟩ namespace Structure variables {c f₁ f₂ : Type u} {r₁ r₂ : Type v} variables {c' : c → M} {f₁' : f₁ → M → M} {f₂' : f₂ → M → M → M} variables {r₁' : r₁ → set M} {r₂' : r₂ → M → M → Prop} @[simp] lemma fun_map_apply₀ (c₀ : c) {x : fin 0 → M} : @Structure.fun_map _ M (Structure.mk₂ c' f₁' f₂' r₁' r₂') 0 c₀ x = c' c₀ := rfl @[simp] lemma fun_map_apply₁ (f : f₁) (x : M) : @Structure.fun_map _ M (Structure.mk₂ c' f₁' f₂' r₁' r₂') 1 f (![x]) = f₁' f x := rfl @[simp] lemma fun_map_apply₂ (f : f₂) (x y : M) : @Structure.fun_map _ M (Structure.mk₂ c' f₁' f₂' r₁' r₂') 2 f (![x,y]) = f₂' f x y := rfl @[simp] lemma rel_map_apply₁ (r : r₁) (x : M) : @Structure.rel_map _ M (Structure.mk₂ c' f₁' f₂' r₁' r₂') 1 r (![x]) = (x ∈ r₁' r) := rfl @[simp] lemma rel_map_apply₂ (r : r₂) (x y : M) : @Structure.rel_map _ M (Structure.mk₂ c' f₁' f₂' r₁' r₂') 2 r (![x,y]) = r₂' r x y := rfl end Structure /-- `hom_class L F M N` states that `F` is a type of `L`-homomorphisms. You should extend this typeclass when you extend `first_order.language.hom`. -/ class hom_class (L : out_param language) (F : Type*) (M N : out_param $ Type*) [fun_like F M (λ _, N)] [L.Structure M] [L.Structure N] := (map_fun : ∀ (φ : F) {n} (f : L.functions n) x, φ (fun_map f x) = fun_map f (φ ∘ x)) (map_rel : ∀ (φ : F) {n} (r : L.relations n) x, rel_map r x → rel_map r (φ ∘ x)) /-- `strong_hom_class L F M N` states that `F` is a type of `L`-homomorphisms which preserve relations in both directions. -/ class strong_hom_class (L : out_param language) (F : Type*) (M N : out_param $ Type*) [fun_like F M (λ _, N)] [L.Structure M] [L.Structure N] := (map_fun : ∀ (φ : F) {n} (f : L.functions n) x, φ (fun_map f x) = fun_map f (φ ∘ x)) (map_rel : ∀ (φ : F) {n} (r : L.relations n) x, rel_map r (φ ∘ x) ↔ rel_map r x) @[priority 100] instance strong_hom_class.hom_class {F M N} [L.Structure M] [L.Structure N] [fun_like F M (λ _, N)] [strong_hom_class L F M N] : hom_class L F M N := { map_fun := strong_hom_class.map_fun, map_rel := λ φ n R x, (strong_hom_class.map_rel φ R x).2 } /-- Not an instance to avoid a loop. -/ def hom_class.strong_hom_class_of_is_algebraic [L.is_algebraic] {F M N} [L.Structure M] [L.Structure N] [fun_like F M (λ _, N)] [hom_class L F M N] : strong_hom_class L F M N := { map_fun := hom_class.map_fun, map_rel := λ φ n R x, (is_algebraic.empty_relations n).elim R } lemma hom_class.map_constants {F M N} [L.Structure M] [L.Structure N] [fun_like F M (λ _, N)] [hom_class L F M N] (φ : F) (c : L.constants) : φ (c) = c := (hom_class.map_fun φ c default).trans (congr rfl (funext default)) namespace hom instance fun_like : fun_like (M →[L] N) M (λ _, N) := { coe := hom.to_fun, coe_injective' := λ f g h, by {cases f, cases g, cases h, refl} } instance hom_class : hom_class L (M →[L] N) M N := { map_fun := map_fun', map_rel := map_rel' } instance [L.is_algebraic] : strong_hom_class L (M →[L] N) M N := hom_class.strong_hom_class_of_is_algebraic instance has_coe_to_fun : has_coe_to_fun (M →[L] N) (λ _, M → N) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : M →[L] N} : f.to_fun = (f : M → N) := rfl @[ext] lemma ext ⦃f g : M →[L] N⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h lemma ext_iff {f g : M →[L] N} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff @[simp] lemma map_fun (φ : M →[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := hom_class.map_fun φ f x @[simp] lemma map_constants (φ : M →[L] N) (c : L.constants) : φ c = c := hom_class.map_constants φ c @[simp] lemma map_rel (φ : M →[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r x → rel_map r (φ ∘ x) := hom_class.map_rel φ r x variables (L) (M) /-- The identity map from a structure to itself -/ @[refl] def id : M →[L] M := { to_fun := id } variables {L} {M} instance : inhabited (M →[L] M) := ⟨id L M⟩ @[simp] lemma id_apply (x : M) : id L M x = x := rfl /-- Composition of first-order homomorphisms -/ @[trans] def comp (hnp : N →[L] P) (hmn : M →[L] N) : M →[L] P := { to_fun := hnp ∘ hmn, map_rel' := λ _ _ _ h, by simp [h] } @[simp] lemma comp_apply (g : N →[L] P) (f : M →[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order homomorphisms is associative. -/ lemma comp_assoc (f : M →[L] N) (g : N →[L] P) (h : P →[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl end hom /-- Any element of a `hom_class` can be realized as a first_order homomorphism. -/ def hom_class.to_hom {F M N} [L.Structure M] [L.Structure N] [fun_like F M (λ _, N)] [hom_class L F M N] : F → (M →[L] N) := λ φ, ⟨φ, λ _, hom_class.map_fun φ, λ _, hom_class.map_rel φ⟩ namespace embedding instance embedding_like : embedding_like (M ↪[L] N) M N := { coe := λ f, f.to_fun, injective' := λ f, f.to_embedding.injective, coe_injective' := λ f g h, begin cases f, cases g, simp only, ext x, exact function.funext_iff.1 h x end } instance strong_hom_class : strong_hom_class L (M ↪[L] N) M N := { map_fun := map_fun', map_rel := map_rel' } instance has_coe_to_fun : has_coe_to_fun (M ↪[L] N) (λ _, M → N) := fun_like.has_coe_to_fun @[simp] lemma map_fun (φ : M ↪[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := hom_class.map_fun φ f x @[simp] lemma map_constants (φ : M ↪[L] N) (c : L.constants) : φ c = c := hom_class.map_constants φ c @[simp] lemma map_rel (φ : M ↪[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := strong_hom_class.map_rel φ r x /-- A first-order embedding is also a first-order homomorphism. -/ def to_hom : (M ↪[L] N) → M →[L] N := hom_class.to_hom @[simp] lemma coe_to_hom {f : M ↪[L] N} : (f.to_hom : M → N) = f := rfl lemma coe_injective : @function.injective (M ↪[L] N) (M → N) coe_fn | f g h := begin cases f, cases g, simp only, ext x, exact function.funext_iff.1 h x, end @[ext] lemma ext ⦃f g : M ↪[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {f g : M ↪[L] N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ lemma injective (f : M ↪[L] N) : function.injective f := f.to_embedding.injective /-- In an algebraic language, any injective homomorphism is an embedding. -/ @[simps] def of_injective [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : M ↪[L] N := { inj' := hf, map_rel' := λ n r x, strong_hom_class.map_rel f r x, .. f } @[simp] lemma coe_fn_of_injective [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : (of_injective hf : M → N) = f := rfl @[simp] lemma of_injective_to_hom [L.is_algebraic] {f : M →[L] N} (hf : function.injective f) : (of_injective hf).to_hom = f := by { ext, simp } variables (L) (M) /-- The identity embedding from a structure to itself -/ @[refl] def refl : M ↪[L] M := { to_embedding := function.embedding.refl M } variables {L} {M} instance : inhabited (M ↪[L] M) := ⟨refl L M⟩ @[simp] lemma refl_apply (x : M) : refl L M x = x := rfl /-- Composition of first-order embeddings -/ @[trans] def comp (hnp : N ↪[L] P) (hmn : M ↪[L] N) : M ↪[L] P := { to_fun := hnp ∘ hmn, inj' := hnp.injective.comp hmn.injective } @[simp] lemma comp_apply (g : N ↪[L] P) (f : M ↪[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order embeddings is associative. -/ lemma comp_assoc (f : M ↪[L] N) (g : N ↪[L] P) (h : P ↪[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma comp_to_hom (hnp : N ↪[L] P) (hmn : M ↪[L] N) : (hnp.comp hmn).to_hom = hnp.to_hom.comp hmn.to_hom := by { ext, simp only [coe_to_hom, comp_apply, hom.comp_apply] } end embedding /-- Any element of an injective `strong_hom_class` can be realized as a first_order embedding. -/ def strong_hom_class.to_embedding {F M N} [L.Structure M] [L.Structure N] [embedding_like F M N] [strong_hom_class L F M N] : F → (M ↪[L] N) := λ φ, ⟨⟨φ, embedding_like.injective φ⟩, λ _, strong_hom_class.map_fun φ, λ _, strong_hom_class.map_rel φ⟩ namespace equiv instance : equiv_like (M ≃[L] N) M N := { coe := λ f, f.to_fun, inv := λ f, f.inv_fun, left_inv := λ f, f.left_inv, right_inv := λ f, f.right_inv, coe_injective' := λ f g h₁ h₂, begin cases f, cases g, simp only, ext x, exact function.funext_iff.1 h₁ x, end, } instance : strong_hom_class L (M ≃[L] N) M N := { map_fun := map_fun', map_rel := map_rel', } /-- The inverse of a first-order equivalence is a first-order equivalence. -/ @[symm] def symm (f : M ≃[L] N) : N ≃[L] M := { map_fun' := λ n f' x, begin simp only [equiv.to_fun_as_coe], rw [equiv.symm_apply_eq], refine eq.trans _ (f.map_fun' f' (f.to_equiv.symm ∘ x)).symm, rw [← function.comp.assoc, equiv.to_fun_as_coe, equiv.self_comp_symm, function.comp.left_id] end, map_rel' := λ n r x, begin simp only [equiv.to_fun_as_coe], refine (f.map_rel' r (f.to_equiv.symm ∘ x)).symm.trans _, rw [← function.comp.assoc, equiv.to_fun_as_coe, equiv.self_comp_symm, function.comp.left_id] end, .. f.to_equiv.symm } instance has_coe_to_fun : has_coe_to_fun (M ≃[L] N) (λ _, M → N) := fun_like.has_coe_to_fun @[simp] lemma apply_symm_apply (f : M ≃[L] N) (a : N) : f (f.symm a) = a := f.to_equiv.apply_symm_apply a @[simp] lemma symm_apply_apply (f : M ≃[L] N) (a : M) : f.symm (f a) = a := f.to_equiv.symm_apply_apply a @[simp] lemma map_fun (φ : M ≃[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := hom_class.map_fun φ f x @[simp] lemma map_constants (φ : M ≃[L] N) (c : L.constants) : φ c = c := hom_class.map_constants φ c @[simp] lemma map_rel (φ : M ≃[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := strong_hom_class.map_rel φ r x /-- A first-order equivalence is also a first-order embedding. -/ def to_embedding : (M ≃[L] N) → M ↪[L] N := strong_hom_class.to_embedding /-- A first-order equivalence is also a first-order homomorphism. -/ def to_hom : (M ≃[L] N) → M →[L] N := hom_class.to_hom @[simp] lemma to_embedding_to_hom (f : M ≃[L] N) : f.to_embedding.to_hom = f.to_hom := rfl @[simp] lemma coe_to_hom {f : M ≃[L] N} : (f.to_hom : M → N) = (f : M → N) := rfl @[simp] lemma coe_to_embedding (f : M ≃[L] N) : (f.to_embedding : M → N) = (f : M → N) := rfl lemma coe_injective : @function.injective (M ≃[L] N) (M → N) coe_fn := fun_like.coe_injective @[ext] lemma ext ⦃f g : M ≃[L] N⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {f g : M ≃[L] N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ lemma bijective (f : M ≃[L] N) : function.bijective f := equiv_like.bijective f lemma injective (f : M ≃[L] N) : function.injective f := equiv_like.injective f lemma surjective (f : M ≃[L] N) : function.surjective f := equiv_like.surjective f variables (L) (M) /-- The identity equivalence from a structure to itself -/ @[refl] def refl : M ≃[L] M := { to_equiv := equiv.refl M } variables {L} {M} instance : inhabited (M ≃[L] M) := ⟨refl L M⟩ @[simp] lemma refl_apply (x : M) : refl L M x = x := rfl /-- Composition of first-order equivalences -/ @[trans] def comp (hnp : N ≃[L] P) (hmn : M ≃[L] N) : M ≃[L] P := { to_fun := hnp ∘ hmn, .. (hmn.to_equiv.trans hnp.to_equiv) } @[simp] lemma comp_apply (g : N ≃[L] P) (f : M ≃[L] N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of first-order homomorphisms is associative. -/ lemma comp_assoc (f : M ≃[L] N) (g : N ≃[L] P) (h : P ≃[L] Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl end equiv /-- Any element of a bijective `strong_hom_class` can be realized as a first_order isomorphism. -/ def strong_hom_class.to_equiv {F M N} [L.Structure M] [L.Structure N] [equiv_like F M N] [strong_hom_class L F M N] : F → (M ≃[L] N) := λ φ, ⟨⟨φ, equiv_like.inv φ, equiv_like.left_inv φ, equiv_like.right_inv φ⟩, λ _, hom_class.map_fun φ, λ _, strong_hom_class.map_rel φ⟩ section sum_Structure variables (L₁ L₂ : language) (S : Type*) [L₁.Structure S] [L₂.Structure S] instance sum_Structure : (L₁.sum L₂).Structure S := { fun_map := λ n, sum.elim fun_map fun_map, rel_map := λ n, sum.elim rel_map rel_map, } variables {L₁ L₂ S} @[simp] lemma fun_map_sum_inl {n : ℕ} (f : L₁.functions n) : @fun_map (L₁.sum L₂) S _ n (sum.inl f) = fun_map f := rfl @[simp] lemma fun_map_sum_inr {n : ℕ} (f : L₂.functions n) : @fun_map (L₁.sum L₂) S _ n (sum.inr f) = fun_map f := rfl @[simp] lemma rel_map_sum_inl {n : ℕ} (R : L₁.relations n) : @rel_map (L₁.sum L₂) S _ n (sum.inl R) = rel_map R := rfl @[simp] lemma rel_map_sum_inr {n : ℕ} (R : L₂.relations n) : @rel_map (L₁.sum L₂) S _ n (sum.inr R) = rel_map R := rfl end sum_Structure section empty section variables [language.empty.Structure M] [language.empty.Structure N] @[simp] lemma empty.nonempty_embedding_iff : nonempty (M ↪[language.empty] N) ↔ cardinal.lift.{w'} (# M) ≤ cardinal.lift.{w} (# N) := trans ⟨nonempty.map (λ f, f.to_embedding), nonempty.map (λ f, {to_embedding := f})⟩ cardinal.lift_mk_le'.symm @[simp] lemma empty.nonempty_equiv_iff : nonempty (M ≃[language.empty] N) ↔ cardinal.lift.{w'} (# M) = cardinal.lift.{w} (# N) := trans ⟨nonempty.map (λ f, f.to_equiv), nonempty.map (λ f, {to_equiv := f})⟩ cardinal.lift_mk_eq'.symm end instance empty_Structure : language.empty.Structure M := ⟨λ _, empty.elim, λ _, empty.elim⟩ instance : unique (language.empty.Structure M) := ⟨⟨language.empty_Structure⟩, λ a, begin ext n f, { exact empty.elim f }, { exact subsingleton.elim _ _ }, end⟩ @[priority 100] instance strong_hom_class_empty {F M N} [fun_like F M (λ _, N)] : strong_hom_class language.empty F M N := ⟨λ _ _ f, empty.elim f, λ _ _ r, empty.elim r⟩ /-- Makes a `language.empty.hom` out of any function. -/ @[simps] def _root_.function.empty_hom (f : M → N) : (M →[language.empty] N) := { to_fun := f } /-- Makes a `language.empty.embedding` out of any function. -/ @[simps] def _root_.embedding.empty (f : M ↪ N) : (M ↪[language.empty] N) := { to_embedding := f } /-- Makes a `language.empty.equiv` out of any function. -/ @[simps] def _root_.equiv.empty (f : M ≃ N) : (M ≃[language.empty] N) := { to_equiv := f } end empty end language end first_order namespace equiv open first_order first_order.language first_order.language.Structure open_locale first_order variables {L : language} {M : Type*} {N : Type*} [L.Structure M] /-- A structure induced by a bijection. -/ @[simps] def induced_Structure (e : M ≃ N) : L.Structure N := ⟨λ n f x, e (fun_map f (e.symm ∘ x)), λ n r x, rel_map r (e.symm ∘ x)⟩ /-- A bijection as a first-order isomorphism with the induced structure on the codomain. -/ @[simps] def induced_Structure_equiv (e : M ≃ N) : @language.equiv L M N _ (induced_Structure e) := { map_fun' := λ n f x, by simp [← function.comp.assoc e.symm e x], map_rel' := λ n r x, by simp [← function.comp.assoc e.symm e x], .. e } end equiv
b5773c3b9e7796768d165409e9785ba02e3cd916
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/smt_ematch1.lean
2c66070e4c53dddb00716a8c7fb2da2d9d32bda8
[ "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
1,657
lean
constant f : nat → nat axiom fax : ∀ x, f x = x attribute [ematch] fax lemma ex1 (a b c : nat) : f a = b → b = f c → a = c := begin [smt] intros, trace_state, ematch end constant g : nat → nat → nat axiom gax : ∀ x, g x x = x lemma ex2 (a b c d e : nat) : d = a → c = e → g a d = b → b = g e c → f a = c := begin [smt] intros, add_lemma gax, ematch end lemma ex3 (a b c d e : nat) : d = a → c = e → g a d = b → b = g e c → f a = c := begin [smt] intros, ematch_using [fax, gax] end local attribute [-ematch] fax lemma ex4 (a b c d e : nat) : d = a → c = e → g a d = b → b = g e c → f a = c := begin [smt] intros, add_lemma [fax, gax], ematch end lemma ex5 (a b c d e : nat) : d = a → c = e → g a d = b → b = g e c → f a = c := begin [smt] intros, ematch_using [fax, gax] end lemma ex6 (a b c d e : nat) : (∀ x, g x (f x) = 0) → a = f b → g b a + 0 = f 0 := begin [smt] intros, assert h : ∀ x, g x (f x) = 0, add_lemma [h, fax, add_zero], ematch end lemma ex7 (a b c d e : nat) : (∀ x, g x (f x) = 0) → a = f b → g b a + 0 = f 0 := begin [smt] intros, assert h : ∀ x, g x (f x) = 0, ematch_using [h, fax, add_zero] end local attribute [ematch] fax add_zero open smt_tactic lemma ex8 (a b c d e : nat) : (∀ x, g x (f x) = 0) → a = f b → g b a + 0 = f 0 := begin [smt] intros, add_lemmas_from_facts, ematch end lemma ex9 (a b c d e : nat) : d ≠ e → (∀ x, g x (f x) = 0) → a = f b → g b a + 0 = f 0 := begin [smt] intros, get_facts >>= trace, get_refuted_facts >>= trace, add_lemmas_from_facts, ematch end
1042742db402cea2e7cb0c72f0d1fd19be25592f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/geometry/euclidean/angle/oriented/affine.lean
4539469cf2dad951069debaf4013f003decf43d0
[ "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
44,543
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 analysis.convex.side import geometry.euclidean.angle.oriented.rotation import geometry.euclidean.angle.unoriented.affine /-! # Oriented angles. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines oriented angles in Euclidean affine spaces. ## Main definitions * `euclidean_geometry.oangle`, with notation `∡`, is the oriented angle determined by three points. -/ noncomputable theory open finite_dimensional complex open_locale affine euclidean_geometry real real_inner_product_space complex_conjugate 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] [hd2 : fact (finrank ℝ V = 2)] [module.oriented ℝ V (fin 2)] include hd2 local notation `o` := module.oriented.positive_orientation /-- The oriented angle at `p₂` between the line segments to `p₁` and `p₃`, modulo `2 * π`. If either of those points equals `p₂`, this is 0. See `euclidean_geometry.angle` for the corresponding unoriented angle definition. -/ def oangle (p₁ p₂ p₃ : P) : real.angle := (o).oangle (p₁ -ᵥ p₂) (p₃ -ᵥ p₂) localized "notation (name := oangle) `∡` := euclidean_geometry.oangle" in euclidean_geometry /-- Oriented angles are continuous when neither end point equals the middle point. -/ lemma continuous_at_oangle {x : P × P × P} (hx12 : x.1 ≠ x.2.1) (hx32 : x.2.2 ≠ x.2.1) : continuous_at (λ y : P × P × P, ∡ y.1 y.2.1 y.2.2) x := begin let f : P × P × P → V × V := λ y, (y.1 -ᵥ y.2.1, y.2.2 -ᵥ y.2.1), have hf1 : (f x).1 ≠ 0, by simp [hx12], have hf2 : (f x).2 ≠ 0, by simp [hx32], exact ((o).continuous_at_oangle hf1 hf2).comp ((continuous_fst.vsub continuous_snd.fst).prod_mk (continuous_snd.snd.vsub continuous_snd.fst)).continuous_at end /-- The angle ∡AAB at a point. -/ @[simp] lemma oangle_self_left (p₁ p₂ : P) : ∡ p₁ p₁ p₂ = 0 := by simp [oangle] /-- The angle ∡ABB at a point. -/ @[simp] lemma oangle_self_right (p₁ p₂ : P) : ∡ p₁ p₂ p₂ = 0 := by simp [oangle] /-- The angle ∡ABA at a point. -/ @[simp] lemma oangle_self_left_right (p₁ p₂ : P) : ∡ p₁ p₂ p₁ = 0 := (o).oangle_self _ /-- If the angle between three points is nonzero, the first two points are not equal. -/ lemma left_ne_of_oangle_ne_zero {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ ≠ 0) : p₁ ≠ p₂ := by { rw ←@vsub_ne_zero V, exact (o).left_ne_zero_of_oangle_ne_zero h } /-- If the angle between three points is nonzero, the last two points are not equal. -/ lemma right_ne_of_oangle_ne_zero {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ ≠ 0) : p₃ ≠ p₂ := by { rw ←@vsub_ne_zero V, exact (o).right_ne_zero_of_oangle_ne_zero h } /-- If the angle between three points is nonzero, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_ne_zero {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ ≠ 0) : p₁ ≠ p₃ := by { rw ←(vsub_left_injective p₂).ne_iff, exact (o).ne_of_oangle_ne_zero h } /-- If the angle between three points is `π`, the first two points are not equal. -/ lemma left_ne_of_oangle_eq_pi {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = π) : p₁ ≠ p₂ := left_ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `π`, the last two points are not equal. -/ lemma right_ne_of_oangle_eq_pi {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = π) : p₃ ≠ p₂ := right_ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `π`, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_eq_pi {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = π) : p₁ ≠ p₃ := left_ne_right_of_oangle_ne_zero (h.symm ▸ real.angle.pi_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `π / 2`, the first two points are not equal. -/ lemma left_ne_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (π / 2 : ℝ)) : p₁ ≠ p₂ := left_ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `π / 2`, the last two points are not equal. -/ lemma right_ne_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (π / 2 : ℝ)) : p₃ ≠ p₂ := right_ne_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `π / 2`, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (π / 2 : ℝ)) : p₁ ≠ p₃ := left_ne_right_of_oangle_ne_zero (h.symm ▸ real.angle.pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `-π / 2`, the first two points are not equal. -/ lemma left_ne_of_oangle_eq_neg_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (-π / 2 : ℝ)) : p₁ ≠ p₂ := left_ne_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `-π / 2`, the last two points are not equal. -/ lemma right_ne_of_oangle_eq_neg_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (-π / 2 : ℝ)) : p₃ ≠ p₂ := right_ne_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the angle between three points is `-π / 2`, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_eq_neg_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = (-π / 2 : ℝ)) : p₁ ≠ p₃ := left_ne_right_of_oangle_ne_zero (h.symm ▸ real.angle.neg_pi_div_two_ne_zero : ∡ p₁ p₂ p₃ ≠ 0) /-- If the sign of the angle between three points is nonzero, the first two points are not equal. -/ lemma left_ne_of_oangle_sign_ne_zero {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign ≠ 0) : p₁ ≠ p₂ := left_ne_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between three points is nonzero, the last two points are not equal. -/ lemma right_ne_of_oangle_sign_ne_zero {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign ≠ 0) : p₃ ≠ p₂ := right_ne_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between three points is nonzero, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_sign_ne_zero {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign ≠ 0) : p₁ ≠ p₃ := left_ne_right_of_oangle_ne_zero (real.angle.sign_ne_zero_iff.1 h).1 /-- If the sign of the angle between three points is positive, the first two points are not equal. -/ lemma left_ne_of_oangle_sign_eq_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = 1) : p₁ ≠ p₂ := left_ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- If the sign of the angle between three points is positive, the last two points are not equal. -/ lemma right_ne_of_oangle_sign_eq_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = 1) : p₃ ≠ p₂ := right_ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- If the sign of the angle between three points is positive, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_sign_eq_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = 1) : p₁ ≠ p₃ := left_ne_right_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- If the sign of the angle between three points is negative, the first two points are not equal. -/ lemma left_ne_of_oangle_sign_eq_neg_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = -1) : p₁ ≠ p₂ := left_ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- If the sign of the angle between three points is negative, the last two points are not equal. -/ lemma right_ne_of_oangle_sign_eq_neg_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = -1) : p₃ ≠ p₂ := right_ne_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- If the sign of the angle between three points is negative, the first and third points are not equal. -/ lemma left_ne_right_of_oangle_sign_eq_neg_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = -1) : p₁ ≠ p₃ := left_ne_right_of_oangle_sign_ne_zero (h.symm ▸ dec_trivial : (∡ p₁ p₂ p₃).sign ≠ 0) /-- Reversing the order of the points passed to `oangle` negates the angle. -/ lemma oangle_rev (p₁ p₂ p₃ : P) : ∡ p₃ p₂ p₁ = -∡ p₁ p₂ p₃ := (o).oangle_rev _ _ /-- Adding an angle to that with the order of the points reversed results in 0. -/ @[simp] lemma oangle_add_oangle_rev (p₁ p₂ p₃ : P) : ∡ p₁ p₂ p₃ + ∡ p₃ p₂ p₁ = 0 := (o).oangle_add_oangle_rev _ _ /-- An oriented angle is zero if and only if the angle with the order of the points reversed is zero. -/ lemma oangle_eq_zero_iff_oangle_rev_eq_zero {p₁ p₂ p₃ : P} : ∡ p₁ p₂ p₃ = 0 ↔ ∡ p₃ p₂ p₁ = 0 := (o).oangle_eq_zero_iff_oangle_rev_eq_zero /-- An oriented angle is `π` if and only if the angle with the order of the points reversed is `π`. -/ lemma oangle_eq_pi_iff_oangle_rev_eq_pi {p₁ p₂ p₃ : P} : ∡ p₁ p₂ p₃ = π ↔ ∡ p₃ p₂ p₁ = π := (o).oangle_eq_pi_iff_oangle_rev_eq_pi /-- An oriented angle is not zero or `π` if and only if the three points are affinely independent. -/ lemma oangle_ne_zero_and_ne_pi_iff_affine_independent {p₁ p₂ p₃ : P} : (∡ p₁ p₂ p₃ ≠ 0 ∧ ∡ p₁ p₂ p₃ ≠ π) ↔ affine_independent ℝ ![p₁, p₂, p₃] := begin rw [oangle, (o).oangle_ne_zero_and_ne_pi_iff_linear_independent, affine_independent_iff_linear_independent_vsub ℝ _ (1 : fin 3), ←linear_independent_equiv (fin_succ_above_equiv (1 : fin 3)).to_equiv], convert iff.rfl, ext i, fin_cases i; refl end /-- An oriented angle is zero or `π` if and only if the three points are collinear. -/ lemma oangle_eq_zero_or_eq_pi_iff_collinear {p₁ p₂ p₃ : P} : (∡ p₁ p₂ p₃ = 0 ∨ ∡ p₁ p₂ p₃ = π) ↔ collinear ℝ ({p₁, p₂, p₃} : set P) := by rw [←not_iff_not, not_or_distrib, oangle_ne_zero_and_ne_pi_iff_affine_independent, affine_independent_iff_not_collinear_set] /-- If twice the oriented angles between two triples of points are equal, one triple is affinely independent if and only if the other is. -/ lemma affine_independent_iff_of_two_zsmul_oangle_eq {p₁ p₂ p₃ p₄ p₅ p₆ : P} (h : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₄ p₅ p₆) : affine_independent ℝ ![p₁, p₂, p₃] ↔ affine_independent ℝ ![p₄, p₅, p₆] := by simp_rw [←oangle_ne_zero_and_ne_pi_iff_affine_independent, ←real.angle.two_zsmul_ne_zero_iff, h] /-- If twice the oriented angles between two triples of points are equal, one triple is collinear if and only if the other is. -/ lemma collinear_iff_of_two_zsmul_oangle_eq {p₁ p₂ p₃ p₄ p₅ p₆ : P} (h : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₄ p₅ p₆) : collinear ℝ ({p₁, p₂, p₃} : set P) ↔ collinear ℝ ({p₄, p₅, p₆} : set P) := by simp_rw [←oangle_eq_zero_or_eq_pi_iff_collinear, ←real.angle.two_zsmul_eq_zero_iff, h] /-- If corresponding pairs of points in two angles have the same vector span, twice those angles are equal. -/ lemma two_zsmul_oangle_of_vector_span_eq {p₁ p₂ p₃ p₄ p₅ p₆ : P} (h₁₂₄₅ : vector_span ℝ ({p₁, p₂} : set P) = vector_span ℝ ({p₄, p₅} : set P)) (h₃₂₆₅ : vector_span ℝ ({p₃, p₂} : set P) = vector_span ℝ ({p₆, p₅} : set P)) : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₄ p₅ p₆ := begin simp_rw vector_span_pair at h₁₂₄₅ h₃₂₆₅, exact (o).two_zsmul_oangle_of_span_eq_of_span_eq h₁₂₄₅ h₃₂₆₅ end /-- If the lines determined by corresponding pairs of points in two angles are parallel, twice those angles are equal. -/ lemma two_zsmul_oangle_of_parallel {p₁ p₂ p₃ p₄ p₅ p₆ : P} (h₁₂₄₅ : line[ℝ, p₁, p₂] ∥ line[ℝ, p₄, p₅]) (h₃₂₆₅ : line[ℝ, p₃, p₂] ∥ line[ℝ, p₆, p₅]) : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₄ p₅ p₆ := begin rw affine_subspace.affine_span_pair_parallel_iff_vector_span_eq at h₁₂₄₅ h₃₂₆₅, exact two_zsmul_oangle_of_vector_span_eq h₁₂₄₅ h₃₂₆₅ end /-- Given three points not equal to `p`, the angle between the first and the second at `p` plus the angle between the second and the third equals the angle between the first and the third. -/ @[simp] lemma oangle_add {p p₁ p₂ p₃ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) (hp₃ : p₃ ≠ p) : ∡ p₁ p p₂ + ∡ p₂ p p₃ = ∡ p₁ p p₃ := (o).oangle_add (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) (vsub_ne_zero.2 hp₃) /-- Given three points not equal to `p`, the angle between the second and the third at `p` plus the angle between the first and the second equals the angle between the first and the third. -/ @[simp] lemma oangle_add_swap {p p₁ p₂ p₃ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) (hp₃ : p₃ ≠ p) : ∡ p₂ p p₃ + ∡ p₁ p p₂ = ∡ p₁ p p₃ := (o).oangle_add_swap (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) (vsub_ne_zero.2 hp₃) /-- Given three points not equal to `p`, the angle between the first and the third at `p` minus the angle between the first and the second equals the angle between the second and the third. -/ @[simp] lemma oangle_sub_left {p p₁ p₂ p₃ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) (hp₃ : p₃ ≠ p) : ∡ p₁ p p₃ - ∡ p₁ p p₂ = ∡ p₂ p p₃ := (o).oangle_sub_left (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) (vsub_ne_zero.2 hp₃) /-- Given three points not equal to `p`, the angle between the first and the third at `p` minus the angle between the second and the third equals the angle between the first and the second. -/ @[simp] lemma oangle_sub_right {p p₁ p₂ p₃ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) (hp₃ : p₃ ≠ p) : ∡ p₁ p p₃ - ∡ p₂ p p₃ = ∡ p₁ p p₂ := (o).oangle_sub_right (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) (vsub_ne_zero.2 hp₃) /-- Given three points not equal to `p`, adding the angles between them at `p` in cyclic order results in 0. -/ @[simp] lemma oangle_add_cyc3 {p p₁ p₂ p₃ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) (hp₃ : p₃ ≠ p) : ∡ p₁ p p₂ + ∡ p₂ p p₃ + ∡ p₃ p p₁ = 0 := (o).oangle_add_cyc3 (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) (vsub_ne_zero.2 hp₃) /-- Pons asinorum, oriented angle-at-point form. -/ lemma oangle_eq_oangle_of_dist_eq {p₁ p₂ p₃ : P} (h : dist p₁ p₂ = dist p₁ p₃) : ∡ p₁ p₂ p₃ = ∡ p₂ p₃ p₁ := begin simp_rw dist_eq_norm_vsub at h, rw [oangle, oangle, ←vsub_sub_vsub_cancel_left p₃ p₂ p₁, ←vsub_sub_vsub_cancel_left p₂ p₃ p₁, (o).oangle_sub_eq_oangle_sub_rev_of_norm_eq h] end /-- The angle at the apex of an isosceles triangle is `π` minus twice a base angle, oriented angle-at-point form. -/ lemma oangle_eq_pi_sub_two_zsmul_oangle_of_dist_eq {p₁ p₂ p₃ : P} (hn : p₂ ≠ p₃) (h : dist p₁ p₂ = dist p₁ p₃) : ∡ p₃ p₁ p₂ = π - (2 : ℤ) • ∡ p₁ p₂ p₃ := begin simp_rw dist_eq_norm_vsub at h, rw [oangle, oangle], convert (o).oangle_eq_pi_sub_two_zsmul_oangle_sub_of_norm_eq _ h using 1, { rw [←neg_vsub_eq_vsub_rev p₁ p₃, ←neg_vsub_eq_vsub_rev p₁ p₂, (o).oangle_neg_neg] }, { rw [←(o).oangle_sub_eq_oangle_sub_rev_of_norm_eq h], simp }, { simpa using hn } end /-- A base angle of an isosceles triangle is acute, oriented angle-at-point form. -/ lemma abs_oangle_right_to_real_lt_pi_div_two_of_dist_eq {p₁ p₂ p₃ : P} (h : dist p₁ p₂ = dist p₁ p₃) : |(∡ p₁ p₂ p₃).to_real| < π / 2 := begin simp_rw dist_eq_norm_vsub at h, rw [oangle, ←vsub_sub_vsub_cancel_left p₃ p₂ p₁], exact (o).abs_oangle_sub_right_to_real_lt_pi_div_two h end /-- A base angle of an isosceles triangle is acute, oriented angle-at-point form. -/ lemma abs_oangle_left_to_real_lt_pi_div_two_of_dist_eq {p₁ p₂ p₃ : P} (h : dist p₁ p₂ = dist p₁ p₃) : |(∡ p₂ p₃ p₁).to_real| < π / 2 := (oangle_eq_oangle_of_dist_eq h) ▸ abs_oangle_right_to_real_lt_pi_div_two_of_dist_eq h /-- The cosine of the oriented angle at `p` between two points not equal to `p` equals that of the unoriented angle. -/ lemma cos_oangle_eq_cos_angle {p p₁ p₂ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) : real.angle.cos (∡ p₁ p p₂) = real.cos (∠ p₁ p p₂) := (o).cos_oangle_eq_cos_angle (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) /-- The oriented angle at `p` between two points not equal to `p` is plus or minus the unoriented angle. -/ lemma oangle_eq_angle_or_eq_neg_angle {p p₁ p₂ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) : ∡ p₁ p p₂ = ∠ p₁ p p₂ ∨ ∡ p₁ p p₂ = -∠ p₁ p p₂ := (o).oangle_eq_angle_or_eq_neg_angle (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) /-- The unoriented angle at `p` between two points not equal to `p` is the absolute value of the oriented angle. -/ lemma angle_eq_abs_oangle_to_real {p p₁ p₂ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) : ∠ p₁ p p₂ = |(∡ p₁ p p₂).to_real| := (o).angle_eq_abs_oangle_to_real (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) /-- If the sign of the oriented angle at `p` between two points is zero, either one of the points equals `p` or the unoriented angle is 0 or π. -/ lemma eq_zero_or_angle_eq_zero_or_pi_of_sign_oangle_eq_zero {p p₁ p₂ : P} (h : (∡ p₁ p p₂).sign = 0) : p₁ = p ∨ p₂ = p ∨ ∠ p₁ p p₂ = 0 ∨ ∠ p₁ p p₂ = π := begin convert (o).eq_zero_or_angle_eq_zero_or_pi_of_sign_oangle_eq_zero h; simp end /-- If two unoriented angles are equal, and the signs of the corresponding oriented angles are equal, then the oriented angles are equal (even in degenerate cases). -/ lemma oangle_eq_of_angle_eq_of_sign_eq {p₁ p₂ p₃ p₄ p₅ p₆ : P} (h : ∠ p₁ p₂ p₃ = ∠ p₄ p₅ p₆) (hs : (∡ p₁ p₂ p₃).sign = (∡ p₄ p₅ p₆).sign) : ∡ p₁ p₂ p₃ = ∡ p₄ p₅ p₆ := (o).oangle_eq_of_angle_eq_of_sign_eq h hs /-- If the signs of two nondegenerate oriented angles between points are equal, the oriented angles are equal if and only if the unoriented angles are equal. -/ lemma angle_eq_iff_oangle_eq_of_sign_eq {p₁ p₂ p₃ p₄ p₅ p₆ : P} (hp₁ : p₁ ≠ p₂) (hp₃ : p₃ ≠ p₂) (hp₄ : p₄ ≠ p₅) (hp₆ : p₆ ≠ p₅) (hs : (∡ p₁ p₂ p₃).sign = (∡ p₄ p₅ p₆).sign) : ∠ p₁ p₂ p₃ = ∠ p₄ p₅ p₆ ↔ ∡ p₁ p₂ p₃ = ∡ p₄ p₅ p₆ := (o).angle_eq_iff_oangle_eq_of_sign_eq (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₃) (vsub_ne_zero.2 hp₄) (vsub_ne_zero.2 hp₆) hs /-- The oriented angle between three points equals the unoriented angle if the sign is positive. -/ lemma oangle_eq_angle_of_sign_eq_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = 1) : ∡ p₁ p₂ p₃ = ∠ p₁ p₂ p₃ := (o).oangle_eq_angle_of_sign_eq_one h /-- The oriented angle between three points equals minus the unoriented angle if the sign is negative. -/ lemma oangle_eq_neg_angle_of_sign_eq_neg_one {p₁ p₂ p₃ : P} (h : (∡ p₁ p₂ p₃).sign = -1) : ∡ p₁ p₂ p₃ = -∠ p₁ p₂ p₃ := (o).oangle_eq_neg_angle_of_sign_eq_neg_one h /-- The unoriented angle at `p` between two points not equal to `p` is zero if and only if the unoriented angle is zero. -/ lemma oangle_eq_zero_iff_angle_eq_zero {p p₁ p₂ : P} (hp₁ : p₁ ≠ p) (hp₂ : p₂ ≠ p) : ∡ p₁ p p₂ = 0 ↔ ∠ p₁ p p₂ = 0 := (o).oangle_eq_zero_iff_angle_eq_zero (vsub_ne_zero.2 hp₁) (vsub_ne_zero.2 hp₂) /-- The oriented angle between three points is `π` if and only if the unoriented angle is `π`. -/ lemma oangle_eq_pi_iff_angle_eq_pi {p₁ p₂ p₃ : P} : ∡ p₁ p₂ p₃ = π ↔ ∠ p₁ p₂ p₃ = π := (o).oangle_eq_pi_iff_angle_eq_pi /-- If the oriented angle between three points is `π / 2`, so is the unoriented angle. -/ lemma angle_eq_pi_div_two_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∠ p₁ p₂ p₃ = π / 2 := begin rw [angle, ←inner_product_geometry.inner_eq_zero_iff_angle_eq_pi_div_two], exact (o).inner_eq_zero_of_oangle_eq_pi_div_two h end /-- If the oriented angle between three points is `π / 2`, so is the unoriented angle (reversed). -/ lemma angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∠ p₃ p₂ p₁ = π / 2 := begin rw angle_comm, exact angle_eq_pi_div_two_of_oangle_eq_pi_div_two h, end /-- If the oriented angle between three points is `-π / 2`, the unoriented angle is `π / 2`. -/ lemma angle_eq_pi_div_two_of_oangle_eq_neg_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(-π / 2)) : ∠ p₁ p₂ p₃ = π / 2 := begin rw [angle, ←inner_product_geometry.inner_eq_zero_iff_angle_eq_pi_div_two], exact (o).inner_eq_zero_of_oangle_eq_neg_pi_div_two h end /-- If the oriented angle between three points is `-π / 2`, the unoriented angle (reversed) is `π / 2`. -/ lemma angle_rev_eq_pi_div_two_of_oangle_eq_neg_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(-π / 2)) : ∠ p₃ p₂ p₁ = π / 2 := begin rw angle_comm, exact angle_eq_pi_div_two_of_oangle_eq_neg_pi_div_two h end /-- Swapping the first and second points in an oriented angle negates the sign of that angle. -/ lemma oangle_swap₁₂_sign (p₁ p₂ p₃ : P) : -(∡ p₁ p₂ p₃).sign = (∡ p₂ p₁ p₃).sign := begin rw [eq_comm, oangle, oangle, ←(o).oangle_neg_neg, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev, ←vsub_sub_vsub_cancel_left p₁ p₃ p₂, ←neg_vsub_eq_vsub_rev p₃ p₂, sub_eq_add_neg, neg_vsub_eq_vsub_rev p₂ p₁, add_comm, ←@neg_one_smul ℝ], nth_rewrite 1 [←one_smul ℝ (p₁ -ᵥ p₂)], rw (o).oangle_sign_smul_add_smul_right, simp end /-- Swapping the first and third points in an oriented angle negates the sign of that angle. -/ lemma oangle_swap₁₃_sign (p₁ p₂ p₃ : P) : -(∡ p₁ p₂ p₃).sign = (∡ p₃ p₂ p₁).sign := by rw [oangle_rev, real.angle.sign_neg, neg_neg] /-- Swapping the second and third points in an oriented angle negates the sign of that angle. -/ lemma oangle_swap₂₃_sign (p₁ p₂ p₃ : P) : -(∡ p₁ p₂ p₃).sign = (∡ p₁ p₃ p₂).sign := by rw [oangle_swap₁₃_sign, ←oangle_swap₁₂_sign, oangle_swap₁₃_sign] /-- Rotating the points in an oriented angle does not change the sign of that angle. -/ lemma oangle_rotate_sign (p₁ p₂ p₃ : P) : (∡ p₂ p₃ p₁).sign = (∡ p₁ p₂ p₃).sign := by rw [←oangle_swap₁₂_sign, oangle_swap₁₃_sign] /-- The oriented angle between three points is π if and only if the second point is strictly between the other two. -/ lemma oangle_eq_pi_iff_sbtw {p₁ p₂ p₃ : P} : ∡ p₁ p₂ p₃ = π ↔ sbtw ℝ p₁ p₂ p₃ := by rw [oangle_eq_pi_iff_angle_eq_pi, angle_eq_pi_iff_sbtw] /-- If the second of three points is strictly between the other two, the oriented angle at that point is π. -/ lemma _root_.sbtw.oangle₁₂₃_eq_pi {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₁ p₂ p₃ = π := oangle_eq_pi_iff_sbtw.2 h /-- If the second of three points is strictly between the other two, the oriented angle at that point (reversed) is π. -/ lemma _root_.sbtw.oangle₃₂₁_eq_pi {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₃ p₂ p₁ = π := by rw [oangle_eq_pi_iff_oangle_rev_eq_pi, ←h.oangle₁₂₃_eq_pi] /-- If the second of three points is weakly between the other two, the oriented angle at the first point is zero. -/ lemma _root_.wbtw.oangle₂₁₃_eq_zero {p₁ p₂ p₃ : P} (h : wbtw ℝ p₁ p₂ p₃) : ∡ p₂ p₁ p₃ = 0 := begin by_cases hp₂p₁ : p₂ = p₁, { simp [hp₂p₁] }, by_cases hp₃p₁ : p₃ = p₁, { simp [hp₃p₁] }, rw oangle_eq_zero_iff_angle_eq_zero hp₂p₁ hp₃p₁, exact h.angle₂₁₃_eq_zero_of_ne hp₂p₁ end /-- If the second of three points is strictly between the other two, the oriented angle at the first point is zero. -/ lemma _root_.sbtw.oangle₂₁₃_eq_zero {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₂ p₁ p₃ = 0 := h.wbtw.oangle₂₁₃_eq_zero /-- If the second of three points is weakly between the other two, the oriented angle at the first point (reversed) is zero. -/ lemma _root_.wbtw.oangle₃₁₂_eq_zero {p₁ p₂ p₃ : P} (h : wbtw ℝ p₁ p₂ p₃) : ∡ p₃ p₁ p₂ = 0 := by rw [oangle_eq_zero_iff_oangle_rev_eq_zero, h.oangle₂₁₃_eq_zero] /-- If the second of three points is strictly between the other two, the oriented angle at the first point (reversed) is zero. -/ lemma _root_.sbtw.oangle₃₁₂_eq_zero {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₃ p₁ p₂ = 0 := h.wbtw.oangle₃₁₂_eq_zero /-- If the second of three points is weakly between the other two, the oriented angle at the third point is zero. -/ lemma _root_.wbtw.oangle₂₃₁_eq_zero {p₁ p₂ p₃ : P} (h : wbtw ℝ p₁ p₂ p₃) : ∡ p₂ p₃ p₁ = 0 := h.symm.oangle₂₁₃_eq_zero /-- If the second of three points is strictly between the other two, the oriented angle at the third point is zero. -/ lemma _root_.sbtw.oangle₂₃₁_eq_zero {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₂ p₃ p₁ = 0 := h.wbtw.oangle₂₃₁_eq_zero /-- If the second of three points is weakly between the other two, the oriented angle at the third point (reversed) is zero. -/ lemma _root_.wbtw.oangle₁₃₂_eq_zero {p₁ p₂ p₃ : P} (h : wbtw ℝ p₁ p₂ p₃) : ∡ p₁ p₃ p₂ = 0 := h.symm.oangle₃₁₂_eq_zero /-- If the second of three points is strictly between the other two, the oriented angle at the third point (reversed) is zero. -/ lemma _root_.sbtw.oangle₁₃₂_eq_zero {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : ∡ p₁ p₃ p₂ = 0 := h.wbtw.oangle₁₃₂_eq_zero /-- The oriented angle between three points is zero if and only if one of the first and third points is weakly between the other two. -/ lemma oangle_eq_zero_iff_wbtw {p₁ p₂ p₃ : P} : ∡ p₁ p₂ p₃ = 0 ↔ wbtw ℝ p₂ p₁ p₃ ∨ wbtw ℝ p₂ p₃ p₁ := begin by_cases hp₁p₂ : p₁ = p₂, { simp [hp₁p₂] }, by_cases hp₃p₂ : p₃ = p₂, { simp [hp₃p₂] }, rw [oangle_eq_zero_iff_angle_eq_zero hp₁p₂ hp₃p₂, angle_eq_zero_iff_ne_and_wbtw], simp [hp₁p₂, hp₃p₂] end /-- An oriented angle is unchanged by replacing the first point by one weakly further away on the same ray. -/ lemma _root_.wbtw.oangle_eq_left {p₁ p₁' p₂ p₃ : P} (h : wbtw ℝ p₂ p₁ p₁') (hp₁p₂ : p₁ ≠ p₂) : ∡ p₁ p₂ p₃ = ∡ p₁' p₂ p₃ := begin by_cases hp₃p₂ : p₃ = p₂, { simp [hp₃p₂] }, by_cases hp₁'p₂ : p₁' = p₂, { rw [hp₁'p₂, wbtw_self_iff] at h, exact false.elim (hp₁p₂ h) }, rw [←oangle_add hp₁'p₂ hp₁p₂ hp₃p₂, h.oangle₃₁₂_eq_zero, zero_add] end /-- An oriented angle is unchanged by replacing the first point by one strictly further away on the same ray. -/ lemma _root_.sbtw.oangle_eq_left {p₁ p₁' p₂ p₃ : P} (h : sbtw ℝ p₂ p₁ p₁') : ∡ p₁ p₂ p₃ = ∡ p₁' p₂ p₃ := h.wbtw.oangle_eq_left h.ne_left /-- An oriented angle is unchanged by replacing the third point by one weakly further away on the same ray. -/ lemma _root_.wbtw.oangle_eq_right {p₁ p₂ p₃ p₃' : P} (h : wbtw ℝ p₂ p₃ p₃') (hp₃p₂ : p₃ ≠ p₂) : ∡ p₁ p₂ p₃ = ∡ p₁ p₂ p₃' := by rw [oangle_rev, h.oangle_eq_left hp₃p₂, ←oangle_rev] /-- An oriented angle is unchanged by replacing the third point by one strictly further away on the same ray. -/ lemma _root_.sbtw.oangle_eq_right {p₁ p₂ p₃ p₃' : P} (h : sbtw ℝ p₂ p₃ p₃') : ∡ p₁ p₂ p₃ = ∡ p₁ p₂ p₃' := h.wbtw.oangle_eq_right h.ne_left /-- An oriented angle is unchanged by replacing the first point with the midpoint of the segment between it and the second point. -/ @[simp] lemma oangle_midpoint_left (p₁ p₂ p₃ : P) : ∡ (midpoint ℝ p₁ p₂) p₂ p₃ = ∡ p₁ p₂ p₃ := begin by_cases h : p₁ = p₂, { simp [h] }, exact (sbtw_midpoint_of_ne ℝ h).symm.oangle_eq_left end /-- An oriented angle is unchanged by replacing the first point with the midpoint of the segment between the second point and that point. -/ @[simp] lemma oangle_midpoint_rev_left (p₁ p₂ p₃ : P) : ∡ (midpoint ℝ p₂ p₁) p₂ p₃ = ∡ p₁ p₂ p₃ := by rw [midpoint_comm, oangle_midpoint_left] /-- An oriented angle is unchanged by replacing the third point with the midpoint of the segment between it and the second point. -/ @[simp] lemma oangle_midpoint_right (p₁ p₂ p₃ : P) : ∡ p₁ p₂ (midpoint ℝ p₃ p₂) = ∡ p₁ p₂ p₃ := begin by_cases h : p₃ = p₂, { simp [h] }, exact (sbtw_midpoint_of_ne ℝ h).symm.oangle_eq_right end /-- An oriented angle is unchanged by replacing the third point with the midpoint of the segment between the second point and that point. -/ @[simp] lemma oangle_midpoint_rev_right (p₁ p₂ p₃ : P) : ∡ p₁ p₂ (midpoint ℝ p₂ p₃) = ∡ p₁ p₂ p₃ := by rw [midpoint_comm, oangle_midpoint_right] /-- Replacing the first point by one on the same line but the opposite ray adds π to the oriented angle. -/ lemma _root_.sbtw.oangle_eq_add_pi_left {p₁ p₁' p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₁') (hp₃p₂ : p₃ ≠ p₂) : ∡ p₁ p₂ p₃ = ∡ p₁' p₂ p₃ + π := by rw [←h.oangle₁₂₃_eq_pi, oangle_add_swap h.left_ne h.right_ne hp₃p₂] /-- Replacing the third point by one on the same line but the opposite ray adds π to the oriented angle. -/ lemma _root_.sbtw.oangle_eq_add_pi_right {p₁ p₂ p₃ p₃' : P} (h : sbtw ℝ p₃ p₂ p₃') (hp₁p₂ : p₁ ≠ p₂) : ∡ p₁ p₂ p₃ = ∡ p₁ p₂ p₃' + π := by rw [←h.oangle₃₂₁_eq_pi, oangle_add hp₁p₂ h.right_ne h.left_ne] /-- Replacing both the first and third points by ones on the same lines but the opposite rays does not change the oriented angle (vertically opposite angles). -/ lemma _root_.sbtw.oangle_eq_left_right {p₁ p₁' p₂ p₃ p₃' : P} (h₁ : sbtw ℝ p₁ p₂ p₁') (h₃ : sbtw ℝ p₃ p₂ p₃') : ∡ p₁ p₂ p₃ = ∡ p₁' p₂ p₃' := by rw [h₁.oangle_eq_add_pi_left h₃.left_ne, h₃.oangle_eq_add_pi_right h₁.right_ne, add_assoc, real.angle.coe_pi_add_coe_pi, add_zero] /-- Replacing the first point by one on the same line does not change twice the oriented angle. -/ lemma _root_.collinear.two_zsmul_oangle_eq_left {p₁ p₁' p₂ p₃ : P} (h : collinear ℝ ({p₁, p₂, p₁'} : set P)) (hp₁p₂ : p₁ ≠ p₂) (hp₁'p₂ : p₁' ≠ p₂) : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₁' p₂ p₃ := begin by_cases hp₃p₂ : p₃ = p₂, { simp [hp₃p₂] }, rcases h.wbtw_or_wbtw_or_wbtw with hw | hw | hw, { have hw' : sbtw ℝ p₁ p₂ p₁' := ⟨hw, hp₁p₂.symm, hp₁'p₂.symm⟩, rw [hw'.oangle_eq_add_pi_left hp₃p₂, smul_add, real.angle.two_zsmul_coe_pi, add_zero] }, { rw hw.oangle_eq_left hp₁'p₂ }, { rw hw.symm.oangle_eq_left hp₁p₂ } end /-- Replacing the third point by one on the same line does not change twice the oriented angle. -/ lemma _root_.collinear.two_zsmul_oangle_eq_right {p₁ p₂ p₃ p₃' : P} (h : collinear ℝ ({p₃, p₂, p₃'} : set P)) (hp₃p₂ : p₃ ≠ p₂) (hp₃'p₂ : p₃' ≠ p₂) : (2 : ℤ) • ∡ p₁ p₂ p₃ = (2 : ℤ) • ∡ p₁ p₂ p₃' := by rw [oangle_rev, smul_neg, h.two_zsmul_oangle_eq_left hp₃p₂ hp₃'p₂, ←smul_neg, ←oangle_rev] /-- Two different points are equidistant from a third point if and only if that third point equals some multiple of a `π / 2` rotation of the vector between those points, plus the midpoint of those points. -/ lemma dist_eq_iff_eq_smul_rotation_pi_div_two_vadd_midpoint {p₁ p₂ p : P} (h : p₁ ≠ p₂) : dist p₁ p = dist p₂ p ↔ ∃ r : ℝ, r • ((o).rotation (π / 2 : ℝ) (p₂ -ᵥ p₁)) +ᵥ midpoint ℝ p₁ p₂ = p := begin refine ⟨λ hd, _, λ hr, _⟩, { have hi : ⟪p₂ -ᵥ p₁, p -ᵥ midpoint ℝ p₁ p₂⟫ = 0, { rw [@dist_eq_norm_vsub' V, @dist_eq_norm_vsub' V, ←mul_self_inj (norm_nonneg _) (norm_nonneg _), ←real_inner_self_eq_norm_mul_norm, ←real_inner_self_eq_norm_mul_norm] at hd, simp_rw [vsub_midpoint, ←vsub_sub_vsub_cancel_left p₂ p₁ p, inner_sub_left, inner_add_right, inner_smul_right, hd, real_inner_comm (p -ᵥ p₁)], abel }, rw [@orientation.inner_eq_zero_iff_eq_zero_or_eq_smul_rotation_pi_div_two V _ _ _ o, or_iff_right (vsub_ne_zero.2 h.symm)] at hi, rcases hi with ⟨r, hr⟩, rw [eq_comm, ←eq_vadd_iff_vsub_eq] at hr, exact ⟨r, hr.symm⟩ }, { rcases hr with ⟨r, rfl⟩, simp_rw [@dist_eq_norm_vsub V, vsub_vadd_eq_vsub_sub, left_vsub_midpoint, right_vsub_midpoint, inv_of_eq_inv, ←neg_vsub_eq_vsub_rev p₂ p₁, ←mul_self_inj (norm_nonneg _) (norm_nonneg _), ←real_inner_self_eq_norm_mul_norm, inner_sub_sub_self], simp [-neg_vsub_eq_vsub_rev] } end open affine_subspace /-- Given two pairs of distinct points on the same line, such that the vectors between those pairs of points are on the same ray (oriented in the same direction on that line), and a fifth point, the angles at the fifth point between each of those two pairs of points have the same sign. -/ lemma _root_.collinear.oangle_sign_of_same_ray_vsub {p₁ p₂ p₃ p₄ : P} (p₅ : P) (hp₁p₂ : p₁ ≠ p₂) (hp₃p₄ : p₃ ≠ p₄) (hc : collinear ℝ ({p₁, p₂, p₃, p₄} : set P)) (hr : same_ray ℝ (p₂ -ᵥ p₁) (p₄ -ᵥ p₃)) : (∡ p₁ p₅ p₂).sign = (∡ p₃ p₅ p₄).sign := begin by_cases hc₅₁₂ : collinear ℝ ({p₅, p₁, p₂} : set P), { have hc₅₁₂₃₄ : collinear ℝ ({p₅, p₁, p₂, p₃, p₄} : set P) := (hc.collinear_insert_iff_of_ne (set.mem_insert _ _) (set.mem_insert_of_mem _ (set.mem_insert _ _)) hp₁p₂).2 hc₅₁₂, have hc₅₃₄ : collinear ℝ ({p₅, p₃, p₄} : set P) := (hc.collinear_insert_iff_of_ne (set.mem_insert_of_mem _ (set.mem_insert_of_mem _ (set.mem_insert _ _))) (set.mem_insert_of_mem _ (set.mem_insert_of_mem _ (set.mem_insert_of_mem _ (set.mem_singleton _)))) hp₃p₄).1 hc₅₁₂₃₄, rw set.insert_comm at hc₅₁₂ hc₅₃₄, have hs₁₅₂ := oangle_eq_zero_or_eq_pi_iff_collinear.2 hc₅₁₂, have hs₃₅₄ := oangle_eq_zero_or_eq_pi_iff_collinear.2 hc₅₃₄, rw ←real.angle.sign_eq_zero_iff at hs₁₅₂ hs₃₅₄, rw [hs₁₅₂, hs₃₅₄] }, { let s : set (P × P × P) := (λ x : line[ℝ, p₁, p₂] × V, (x.1, p₅, x.2 +ᵥ x.1)) '' set.univ ×ˢ {v | same_ray ℝ (p₂ -ᵥ p₁) v ∧ v ≠ 0}, have hco : is_connected s, { haveI : connected_space line[ℝ, p₁, p₂] := add_torsor.connected_space _ _, exact (is_connected_univ.prod (is_connected_set_of_same_ray_and_ne_zero (vsub_ne_zero.2 hp₁p₂.symm))).image _ ((continuous_fst.subtype_coe.prod_mk (continuous_const.prod_mk (continuous_snd.vadd continuous_fst.subtype_coe))).continuous_on) }, have hf : continuous_on (λ p : P × P × P, ∡ p.1 p.2.1 p.2.2) s, { refine continuous_at.continuous_on (λ p hp, continuous_at_oangle _ _), all_goals { simp_rw [s, set.mem_image, set.mem_prod, set.mem_univ, true_and, prod.ext_iff] at hp, obtain ⟨q₁, q₅, q₂⟩ := p, dsimp only at ⊢ hp, obtain ⟨⟨⟨q, hq⟩, v⟩, hv, rfl, rfl, rfl⟩ := hp, dsimp only [subtype.coe_mk, set.mem_set_of] at ⊢ hv, obtain ⟨hvr, -⟩ := hv, rintro rfl, refine hc₅₁₂ ((collinear_insert_iff_of_mem_affine_span _).2 (collinear_pair _ _ _)) }, { exact hq }, { refine vadd_mem_of_mem_direction _ hq, rw ←exists_nonneg_left_iff_same_ray (vsub_ne_zero.2 hp₁p₂.symm) at hvr, obtain ⟨r, -, rfl⟩ := hvr, rw direction_affine_span, exact smul_vsub_rev_mem_vector_span_pair _ _ _ } }, have hsp : ∀ p : P × P × P, p ∈ s → ∡ p.1 p.2.1 p.2.2 ≠ 0 ∧ ∡ p.1 p.2.1 p.2.2 ≠ π, { intros p hp, simp_rw [s, set.mem_image, set.mem_prod, set.mem_set_of, set.mem_univ, true_and, prod.ext_iff] at hp, obtain ⟨q₁, q₅, q₂⟩ := p, dsimp only at ⊢ hp, obtain ⟨⟨⟨q, hq⟩, v⟩, hv, rfl, rfl, rfl⟩ := hp, dsimp only [subtype.coe_mk, set.mem_set_of] at ⊢ hv, obtain ⟨hvr, hv0⟩ := hv, rw ←exists_nonneg_left_iff_same_ray (vsub_ne_zero.2 hp₁p₂.symm) at hvr, obtain ⟨r, -, rfl⟩ := hvr, change q ∈ line[ℝ, p₁, p₂] at hq, rw [oangle_ne_zero_and_ne_pi_iff_affine_independent], refine affine_independent_of_ne_of_mem_of_not_mem_of_mem _ hq (λ h, hc₅₁₂ ((collinear_insert_iff_of_mem_affine_span h).2 (collinear_pair _ _ _))) _, { rwa [←@vsub_ne_zero V, vsub_vadd_eq_vsub_sub, vsub_self, zero_sub, neg_ne_zero] }, { refine vadd_mem_of_mem_direction _ hq, rw direction_affine_span, exact smul_vsub_rev_mem_vector_span_pair _ _ _ } }, have hp₁p₂s : (p₁, p₅, p₂) ∈ s, { simp_rw [s, set.mem_image, set.mem_prod, set.mem_set_of, set.mem_univ, true_and, prod.ext_iff], refine ⟨⟨⟨p₁, left_mem_affine_span_pair _ _ _⟩, p₂ -ᵥ p₁⟩, ⟨same_ray.rfl, vsub_ne_zero.2 hp₁p₂.symm⟩, _⟩, simp }, have hp₃p₄s : (p₃, p₅, p₄) ∈ s, { simp_rw [s, set.mem_image, set.mem_prod, set.mem_set_of, set.mem_univ, true_and, prod.ext_iff], refine ⟨⟨⟨p₃, hc.mem_affine_span_of_mem_of_ne (set.mem_insert _ _) (set.mem_insert_of_mem _ (set.mem_insert _ _)) (set.mem_insert_of_mem _ (set.mem_insert_of_mem _ (set.mem_insert _ _))) hp₁p₂⟩, p₄ -ᵥ p₃⟩, ⟨hr, vsub_ne_zero.2 hp₃p₄.symm⟩, _⟩, simp }, convert real.angle.sign_eq_of_continuous_on hco hf hsp hp₃p₄s hp₁p₂s } end /-- Given three points in strict order on the same line, and a fourth point, the angles at the fourth point between the first and second or second and third points have the same sign. -/ lemma _root_.sbtw.oangle_sign_eq {p₁ p₂ p₃ : P} (p₄ : P) (h : sbtw ℝ p₁ p₂ p₃) : (∡ p₁ p₄ p₂).sign = (∡ p₂ p₄ p₃).sign := begin have hc : collinear ℝ ({p₁, p₂, p₂, p₃} : set P), { simpa using h.wbtw.collinear }, exact hc.oangle_sign_of_same_ray_vsub _ h.left_ne h.ne_right h.wbtw.same_ray_vsub end /-- Given three points in weak order on the same line, with the first not equal to the second, and a fourth point, the angles at the fourth point between the first and second or first and third points have the same sign. -/ lemma _root_.wbtw.oangle_sign_eq_of_ne_left {p₁ p₂ p₃ : P} (p₄ : P) (h : wbtw ℝ p₁ p₂ p₃) (hne : p₁ ≠ p₂) : (∡ p₁ p₄ p₂).sign = (∡ p₁ p₄ p₃).sign := begin have hc : collinear ℝ ({p₁, p₂, p₁, p₃} : set P), { simpa [set.insert_comm p₂] using h.collinear }, exact hc.oangle_sign_of_same_ray_vsub _ hne (h.left_ne_right_of_ne_left hne.symm) h.same_ray_vsub_left end /-- Given three points in strict order on the same line, and a fourth point, the angles at the fourth point between the first and second or first and third points have the same sign. -/ lemma _root_.sbtw.oangle_sign_eq_left {p₁ p₂ p₃ : P} (p₄ : P) (h : sbtw ℝ p₁ p₂ p₃) : (∡ p₁ p₄ p₂).sign = (∡ p₁ p₄ p₃).sign := h.wbtw.oangle_sign_eq_of_ne_left _ h.left_ne /-- Given three points in weak order on the same line, with the second not equal to the third, and a fourth point, the angles at the fourth point between the second and third or first and third points have the same sign. -/ lemma _root_.wbtw.oangle_sign_eq_of_ne_right {p₁ p₂ p₃ : P} (p₄ : P) (h : wbtw ℝ p₁ p₂ p₃) (hne : p₂ ≠ p₃) : (∡ p₂ p₄ p₃).sign = (∡ p₁ p₄ p₃).sign := by simp_rw [oangle_rev p₃, real.angle.sign_neg, h.symm.oangle_sign_eq_of_ne_left _ hne.symm] /-- Given three points in strict order on the same line, and a fourth point, the angles at the fourth point between the second and third or first and third points have the same sign. -/ lemma _root_.sbtw.oangle_sign_eq_right {p₁ p₂ p₃ : P} (p₄ : P) (h : sbtw ℝ p₁ p₂ p₃) : (∡ p₂ p₄ p₃).sign = (∡ p₁ p₄ p₃).sign := h.wbtw.oangle_sign_eq_of_ne_right _ h.ne_right /-- Given two points in an affine subspace, the angles between those two points at two other points on the same side of that subspace have the same sign. -/ lemma _root_.affine_subspace.s_same_side.oangle_sign_eq {s : affine_subspace ℝ P} {p₁ p₂ p₃ p₄ : P} (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) (hp₃p₄ : s.s_same_side p₃ p₄) : (∡ p₁ p₄ p₂).sign = (∡ p₁ p₃ p₂).sign := begin by_cases h : p₁ = p₂, { simp [h] }, let sp : set (P × P × P) := (λ p : P, (p₁, p, p₂)) '' {p | s.s_same_side p₃ p}, have hc : is_connected sp := (is_connected_set_of_s_same_side hp₃p₄.2.1 hp₃p₄.nonempty).image _ (continuous_const.prod_mk (continuous.prod.mk_left _)).continuous_on, have hf : continuous_on (λ p : P × P × P, ∡ p.1 p.2.1 p.2.2) sp, { refine continuous_at.continuous_on (λ p hp, continuous_at_oangle _ _), all_goals { simp_rw [sp, set.mem_image, set.mem_set_of] at hp, obtain ⟨p', hp', rfl⟩ := hp, dsimp only, rintro rfl }, { exact hp'.2.2 hp₁ }, { exact hp'.2.2 hp₂ } }, have hsp : ∀ p : P × P × P, p ∈ sp → ∡ p.1 p.2.1 p.2.2 ≠ 0 ∧ ∡ p.1 p.2.1 p.2.2 ≠ π, { intros p hp, simp_rw [sp, set.mem_image, set.mem_set_of] at hp, obtain ⟨p', hp', rfl⟩ := hp, dsimp only, rw [oangle_ne_zero_and_ne_pi_iff_affine_independent], exact affine_independent_of_ne_of_mem_of_not_mem_of_mem h hp₁ hp'.2.2 hp₂ }, have hp₃ : (p₁, p₃, p₂) ∈ sp := set.mem_image_of_mem _ (s_same_side_self_iff.2 ⟨hp₃p₄.nonempty, hp₃p₄.2.1⟩), have hp₄ : (p₁, p₄, p₂) ∈ sp := set.mem_image_of_mem _ hp₃p₄, convert real.angle.sign_eq_of_continuous_on hc hf hsp hp₃ hp₄ end /-- Given two points in an affine subspace, the angles between those two points at two other points on opposite sides of that subspace have opposite signs. -/ lemma _root_.affine_subspace.s_opp_side.oangle_sign_eq_neg {s : affine_subspace ℝ P} {p₁ p₂ p₃ p₄ : P} (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) (hp₃p₄ : s.s_opp_side p₃ p₄) : (∡ p₁ p₄ p₂).sign = -(∡ p₁ p₃ p₂).sign := begin have hp₁p₃ : p₁ ≠ p₃, { rintro rfl, exact hp₃p₄.left_not_mem hp₁ }, rw [←(hp₃p₄.symm.trans (s_opp_side_point_reflection hp₁ hp₃p₄.left_not_mem)).oangle_sign_eq hp₁ hp₂, ←oangle_rotate_sign p₁, ←oangle_rotate_sign p₁, oangle_swap₁₃_sign, (sbtw_point_reflection_of_ne ℝ hp₁p₃).symm.oangle_sign_eq _], end end euclidean_geometry
b4553835384ae23d64cad969053ef70760fedee7
46125763b4dbf50619e8846a1371029346f4c3db
/src/measure_theory/borel_space.lean
8dc1f342b52ee8ec009491e6c7f28c1d9702c8e0
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
26,510
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 Borel (measurable) space -- the smallest σ-algebra generated by open sets It would be nice to encode this in the topological space type class, i.e. each topological space carries a measurable space, the Borel space. This would be similar how each uniform space carries a topological space. The idea is to allow definitional equality for product instances. We would like to have definitional equality for borel t₁ × borel t₂ = borel (t₁ × t₂) Unfortunately, this only holds if t₁ and t₂ are second-countable topologies. -/ import measure_theory.measurable_space topology.instances.ennreal analysis.normed_space.basic noncomputable theory open classical set lattice real open_locale classical universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} {s t u : set α} open measurable_space topological_space @[instance, priority 900] def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @is_measurable.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @is_measurable.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @is_measurable.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma borel_eq_generate_Iio (α) [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (order_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range Iio)) (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = -Iio a'), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, -Iio x.1.1, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi (α) [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] : borel α = generate_from (range (λ a, {x | a < x})) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (order_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range (λ a, {x | a < x}))) {x | a < x} := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩, {apply H}, by_cases h : ∃ a', ∀ b, b < a ↔ b ≤ a', { rcases h with ⟨a', ha'⟩, rw (_ : Iio a = -Ioi a'), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a' < a}, {b | b < a'.1}) (λ a', is_open_gt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Iio a = ⋃ x : v, -Ioi x.1.1, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_le_of_lt ax h⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), λ h, lt_of_le_of_lt h ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ (is_open_lt' _) } end lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := calc @borel α (t.induced f) = measurable_space.generate_from (preimage f '' {s | is_open s }) : congr_arg measurable_space.generate_from $ set.ext $ assume s : set α, show (t.induced f).is_open s ↔ s ∈ preimage f '' {s | is_open s}, by simp [topological_space.induced, set.image, eq_comm]; refl ... = (@borel β t).comap f : comap_generate_from.symm section variables [topological_space α] lemma is_measurable_of_is_open : is_open s → is_measurable s := generate_measurable.basic s lemma is_measurable_interior : is_measurable (interior s) := is_measurable_of_is_open is_open_interior lemma is_measurable_ball [metric_space β] {x : β} {ε : ℝ} : is_measurable (metric.ball x ε) := is_measurable_of_is_open metric.is_open_ball lemma is_measurable_of_is_closed (h : is_closed s) : is_measurable s := is_measurable.compl_iff.1 $ is_measurable_of_is_open h lemma is_measurable_singleton [t1_space α] {x : α} : is_measurable ({x} : set α) := is_measurable_of_is_closed is_closed_singleton lemma is_measurable_closure : is_measurable (closure s) := is_measurable_of_is_closed is_closed_closure lemma measurable_of_continuous [topological_space β] {f : α → β} (h : continuous f) : measurable f := measurable_generate_from $ assume t ht, is_measurable_of_is_open $ h t ht lemma borel_prod_le [topological_space β] : prod.measurable_space ≤ borel (α × β) := sup_le (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_fst) (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_snd) lemma borel_induced {α β} [t : topological_space β] (f : α → β) : @borel α (t.induced f) = (borel β).comap f := comap_generate_from.symm lemma borel_eq_subtype (s : set α) : borel s = subtype.measurable_space := borel_induced coe lemma borel_prod [second_countable_topology α] [topological_space β] [second_countable_topology β] : prod.measurable_space = borel (α × β) := let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := @is_open_generated_countable_inter α _ _ in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := @is_open_generated_countable_inter β _ _ in le_antisymm borel_prod_le begin have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v}, { rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ }, rw [borel_eq_generate_from_of_subbasis this], exact generate_from_le (assume p ⟨u, hu, v, hv, eq⟩, have hu : is_open u, by rw [ha₅]; exact generate_open.basic _ hu, have hv : is_open v, by rw [hb₅]; exact generate_open.basic _ hv, eq.symm ▸ is_measurable_set_prod (is_measurable_of_is_open hu) (is_measurable_of_is_open hv)) end lemma measurable_of_continuous2 {α β γ} [topological_space α] [second_countable_topology α] [topological_space β] [second_countable_topology β] [topological_space γ] [measurable_space δ] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λp:α×β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λa, c (f a) (g a)) := show measurable ((λp:α×β, c p.1 p.2) ∘ (λa, (f a, g a))), begin apply measurable.comp, { rw borel_prod, exact measurable_of_continuous h }, { exact measurable.prod_mk hf hg } end lemma measurable.add [add_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a + g a) := measurable_of_continuous2 continuous_add lemma measurable_finset_sum {ι : Type*} [add_comm_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : ι → β → α} (s : finset ι) (hf : ∀i, measurable (f i)) : measurable (λa, s.sum (λi, f i a)) := finset.induction_on s (by simpa using measurable_const) (assume i s his ih, by simpa [his] using measurable.add (hf i) ih) lemma measurable.neg [add_group α] [topological_add_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, - f a) := (measurable_of_continuous continuous_neg).comp hf lemma measurable_neg_iff [add_group α] [topological_add_group α] [measurable_space β] (f : β → α) : measurable (λa, -f a) ↔ measurable f := iff.intro begin assume h, have := measurable.neg h, convert this, funext, simp only [pi.neg_apply, _root_.neg_neg] end $ measurable.neg lemma measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a - g a) := measurable_of_continuous2 continuous_sub lemma measurable.mul [monoid α] [topological_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a * g a) := measurable_of_continuous2 continuous_mul lemma is_measurable_le {α β} [topological_space α] [partial_order α] [order_closed_topology α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} (hf : measurable f) (hg : measurable g) : is_measurable {a | f a ≤ g a} := have is_measurable {p : α × α | p.1 ≤ p.2}, by rw borel_prod; exact is_measurable_of_is_closed (order_closed_topology.is_closed_le' _), show is_measurable {a | (f a, g a).1 ≤ (f a, g a).2}, begin refine measurable.preimage _ this, exact measurable.prod_mk hf hg end lemma measurable.max {α β} [topological_space α] [decidable_linear_order α] [order_closed_topology α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λa, max (f a) (g a)) := measurable.if (is_measurable_le hf hg) hg hf lemma measurable.min {α β} [topological_space α] [decidable_linear_order α] [order_closed_topology α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λa, min (f a) (g a)) := measurable.if (is_measurable_le hf hg) hf hg -- generalize lemma measurable_coe_int_real : measurable (λa, a : ℤ → ℝ) := assume s (hs : is_measurable s), by trivial section order_closed_topology variables [linear_order α] [order_closed_topology α] {a b c : α} lemma is_measurable_Iio : is_measurable (Iio a) := is_measurable_of_is_open is_open_Iio lemma is_measurable_Ioi : is_measurable (Ioi a) := is_measurable_of_is_open is_open_Ioi lemma is_measurable_Ici : is_measurable (Ici a) := is_measurable_of_is_closed is_closed_Ici lemma is_measurable_Iic : is_measurable (Iic a) := is_measurable_of_is_closed is_closed_Iic lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_measurable_of_is_open is_open_Ioo lemma is_measurable_Ioc : is_measurable (Ioc a b) := is_measurable_Ioi.inter is_measurable_Iic lemma is_measurable_Ico : is_measurable (Ico a b) := is_measurable_Ici.inter is_measurable_Iio lemma is_measurable_Icc : is_measurable (Icc a b) := is_measurable_of_is_closed is_closed_Icc open_locale interval lemma is_measurable_interval {α} [decidable_linear_order α] [topological_space α] [order_closed_topology α] {a b : α} : is_measurable [a, b] := is_measurable_Icc end order_closed_topology lemma measurable.is_lub {α} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Ioi α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | a < g b} = ⋃ i, {b | a < f i b}, { simp [set.ext_iff], intro b, rw [lt_is_lub_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | a < g b}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_lt' _))) end lemma measurable.is_glb {α} [topological_space α] [linear_order α] [order_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Iio α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | g b < a} = ⋃ i, {b | f i b < a}, { simp [set.ext_iff], intro b, rw [is_glb_lt_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | g b < a}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_gt' _))) end lemma measurable.supr {α} [topological_space α] [complete_linear_order α] [order_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr lemma measurable.infi {α} [topological_space α] [complete_linear_order α] [order_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi lemma measurable.supr_Prop {α} [topological_space α] [complete_linear_order α] {β} [measurable_space β] {p : Prop} {f : β → α} (hf : measurable f) : measurable (λ b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end) lemma measurable.infi_Prop {α} [topological_space α] [complete_linear_order α] {β} [measurable_space β] {p : Prop} {f : β → α} (hf : measurable f) : measurable (λ b, ⨅ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end) end def homemorph.to_measurable_equiv [topological_space α] [topological_space β] (h : α ≃ₜ β) : measurable_equiv α β := { to_equiv := h.to_equiv, measurable_to_fun := measurable_of_continuous h.continuous_to_fun, measurable_inv_fun := measurable_of_continuous h.continuous_inv_fun } namespace real open measurable_space lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃a:ℚ, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union], rintro ⟨a, b, h, rfl|⟨⟨⟩⟩⟩, rw (set.ext (λ x, _) : Ioo (a:ℝ) b = (⋃c>a, - Iio c) ∩ Iio b), { have hg : ∀q:ℚ, g.is_measurable (Iio q) := λ q, generate_measurable.basic _ (by simp; exact ⟨_, rfl⟩), refine @is_measurable.inter _ g _ _ _ (hg _), refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @is_measurable.compl _ _ g (hg _) }, { simp [Ioo, Iio], refine and_congr _ iff.rfl, exact ⟨λ h, let ⟨c, ac, cx⟩ := exists_rat_btwn h in ⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩, λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } }, { simp, rintro r rfl, exact is_measurable_of_is_open (is_open_gt' _) } end end real namespace nnreal open filter lemma measurable.add [measurable_space α] {f : α → nnreal} {g : α → nnreal} : measurable f → measurable g → measurable (λa, f a + g a) := measurable_of_continuous2 continuous_add lemma measurable.sub [measurable_space α] {f g: α → nnreal} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a - g a) := measurable_of_continuous2 continuous_sub hf hg lemma measurable.mul [measurable_space α] {f : α → nnreal} {g : α → nnreal} : measurable f → measurable g → measurable (λa, f a * g a) := measurable_of_continuous2 continuous_mul lemma measurable_of_real : measurable nnreal.of_real := measurable_of_continuous nnreal.continuous_of_real end nnreal namespace ennreal open filter lemma measurable_coe : measurable (coe : nnreal → ennreal) := measurable_of_continuous (continuous_coe.2 continuous_id) def ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r < ⊤} nnreal := { to_fun := λr, ennreal.to_nnreal r.1, inv_fun := λr, ⟨r, coe_lt_top⟩, left_inv := assume ⟨r, hr⟩, by simp [coe_to_nnreal (ne_of_lt hr)], right_inv := assume r, to_nnreal_coe, measurable_to_fun := begin rw [← borel_eq_subtype], refine measurable_of_continuous (continuous_iff_continuous_at.2 _), rintros ⟨r, hr⟩, simp [continuous_at, nhds_subtype_eq_comap], refine tendsto.comp (tendsto_to_nnreal (ne_of_lt hr)) tendsto_comap end, measurable_inv_fun := measurable.subtype_mk measurable_coe } lemma measurable_of_measurable_nnreal [measurable_space α] {f : ennreal → α} (h : measurable (λp:nnreal, f p)) : measurable f := begin refine measurable_of_measurable_union_cover {⊤} {r : ennreal | r < ⊤} (is_measurable_of_is_closed $ is_closed_singleton) (is_measurable_of_is_open $ is_open_gt' _) (assume r _, by cases r; simp [ennreal.none_eq_top, ennreal.some_eq_coe]) _ _, exact (measurable_equiv.set.singleton ⊤).symm.measurable_coe_iff.1 (measurable_unit _), exact (ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) end def ennreal_equiv_sum : @measurable_equiv ennreal (nnreal ⊕ unit) _ sum.measurable_space := { to_fun := @option.rec nnreal (λ_, nnreal ⊕ unit) (sum.inr ()) (sum.inl : nnreal → nnreal ⊕ unit), inv_fun := @sum.rec nnreal unit (λ_, ennreal) (coe : nnreal → ennreal) (λ_, ⊤), left_inv := assume s, by cases s; refl, right_inv := assume s, by rcases s with r | ⟨⟨⟩⟩; refl, measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤) } lemma measurable_of_measurable_nnreal_nnreal [measurable_space α] [measurable_space β] (f : ennreal → ennreal → β) {g : α → ennreal} {h : α → ennreal} (h₁ : measurable (λp:nnreal × nnreal, f p.1 p.2)) (h₂ : measurable (λr:nnreal, f ⊤ r)) (h₃ : measurable (λr:nnreal, f r ⊤)) (hg : measurable g) (hh : measurable h) : measurable (λa, f (g a) (h a)) := let e : measurable_equiv (ennreal × ennreal) (((nnreal × nnreal) ⊕ (nnreal × unit)) ⊕ ((unit × nnreal) ⊕ (unit × unit))) := (measurable_equiv.prod_congr ennreal_equiv_sum ennreal_equiv_sum).trans (measurable_equiv.sum_prod_sum _ _ _ _) in have measurable (λp:ennreal×ennreal, f p.1 p.2), begin refine e.symm.measurable_coe_iff.1 (measurable_sum (measurable_sum _ _) (measurable_sum _ _)), { show measurable (λp:nnreal × nnreal, f p.1 p.2), exact h₁ }, { show measurable (λp:nnreal × unit, f p.1 ⊤), exact h₃.comp (measurable.fst measurable_id) }, { show measurable ((λp:nnreal, f ⊤ p) ∘ (λp:unit × nnreal, p.2)), exact h₂.comp (measurable.snd measurable_id) }, { show measurable (λp:unit × unit, f ⊤ ⊤), exact measurable_const } end, this.comp (measurable.prod_mk hg hh) lemma measurable.mul {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a * g a) := begin refine measurable_of_measurable_nnreal_nnreal (*) _ _ _, { simp only [ennreal.coe_mul.symm], exact measurable_coe.comp (measurable.mul (measurable.fst measurable_id) (measurable.snd measurable_id)) }, { simp [top_mul], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const }, { simp [mul_top], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const } end lemma measurable.add {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a + g a) := begin refine measurable_of_measurable_nnreal_nnreal (+) _ _ _, { simp only [ennreal.coe_add.symm], exact measurable_coe.comp (measurable.add (measurable.fst measurable_id) (measurable.snd measurable_id)) }, { simp [measurable_const] }, { simp [measurable_const] } end lemma measurable.sub {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a - g a) := begin refine measurable_of_measurable_nnreal_nnreal (has_sub.sub) _ _ _, { simp only [ennreal.coe_sub.symm], exact measurable_coe.comp (nnreal.measurable.sub (measurable.fst measurable_id) (measurable.snd measurable_id)) }, { simp [measurable_const] }, { simp [measurable_const] } end lemma measurable_of_real : measurable ennreal.of_real := measurable_of_continuous ennreal.continuous_of_real end ennreal open topological_space lemma measurable.smul' {α : Type*} {β : Type*} {γ : Type*} [semiring α] [topological_space α] [second_countable_topology α] [topological_space β] [add_comm_monoid β] [second_countable_topology β] [semimodule α β] [topological_semimodule α β] [measurable_space γ] {f : γ → α} {g : γ → β} (hf : measurable f) (hg : measurable g) : measurable (λ c, f c • g c) := measurable_of_continuous2 (continuous_fst.smul continuous_snd) hf hg lemma measurable.smul {α : Type*} {β : Type*} {γ : Type*} [semiring α] [topological_space α] [topological_space β] [add_comm_monoid β] [semimodule α β] [topological_semimodule α β] [measurable_space γ] (c : α) {f : γ → β} (hf : measurable f) : measurable (λ x, c • f x) := measurable.comp (measurable_of_continuous (continuous_const.smul continuous_id)) hf lemma measurable_smul_iff {α : Type*} {β : Type*} {γ : Type*} [division_ring α] [topological_space α] [topological_space β] [add_comm_monoid β] [semimodule α β] [topological_semimodule α β] [measurable_space γ] {c : α} (hc : c ≠ 0) (f : γ → β) : measurable (λ x, c • f x) ↔ measurable f := iff.intro begin assume h, have eq : (λ (x : γ), c⁻¹ • (λ (x : γ), c • f x) x) = f, { funext, rw [smul_smul, inv_mul_cancel hc, one_smul] }, have := h.smul c⁻¹, rwa eq at this end $ measurable.smul c lemma measurable_dist {α : Type*} [metric_space α] [second_countable_topology α] : measurable (λp:α×α, dist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_dist continuous_fst continuous_snd end lemma measurable.dist {α : Type*} [metric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) := measurable_dist.comp (measurable.prod_mk hf hg) lemma measurable_nndist {α : Type*} [metric_space α] [second_countable_topology α] : measurable (λp:α×α, nndist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_nndist continuous_fst continuous_snd end lemma measurable.nndist {α : Type*} [metric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, nndist (f b) (g b)) := measurable_nndist.comp (measurable.prod_mk hf hg) lemma measurable_edist {α : Type*} [emetric_space α] [second_countable_topology α] : measurable (λp:α×α, edist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_edist continuous_fst continuous_snd end lemma measurable.edist {α : Type*} [emetric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, edist (f b) (g b)) := measurable_edist.comp (measurable.prod_mk hf hg) lemma measurable_norm {α : Type*} [normed_group α] : measurable (norm : α → ℝ) := measurable_of_continuous continuous_norm lemma measurable.norm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, norm (f a)) := measurable_norm.comp hf lemma measurable_nnnorm {α : Type*} [normed_group α] : measurable (nnnorm : α → nnreal) := measurable_of_continuous continuous_nnnorm lemma measurable.nnnorm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, nnnorm (f a)) := measurable_nnnorm.comp hf lemma measurable.coe_nnnorm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, (nnnorm (f a) : ennreal)) := ennreal.measurable_coe.comp $ hf.nnnorm
ae119664592c681fbe2943cc533f21c45f16a244
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finsupp/indicator.lean
a6449f604f820a72ac02757b9a4f868693c5fd0b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,322
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 data.finsupp.defs /-! # Building finitely supported functions off finsets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines `finsupp.indicator` to help create finsupps from finsets. ## Main declarations * `finsupp.indicator`: Turns a map from a `finset` into a `finsupp` from the entire type. -/ noncomputable theory open finset function variables {ι α : Type*} namespace finsupp variables [has_zero α] {s : finset ι} (f : Π i ∈ s, α) {i : ι} /-- Create an element of `ι →₀ α` from a finset `s` and a function `f` defined on this finset. -/ def indicator (s : finset ι) (f : Π i ∈ s, α) : ι →₀ α := { to_fun := λ i, by haveI := classical.dec_eq ι; exact if H : i ∈ s then f i H else 0, support := by haveI := classical.dec_eq α; exact (s.attach.filter $ λ i : s, f i.1 i.2 ≠ 0).map (embedding.subtype _), mem_support_to_fun := λ i, begin letI := classical.dec_eq α, rw [mem_map, dite_ne_right_iff], exact ⟨λ ⟨⟨j, hj⟩, hf, rfl⟩, ⟨hj, (mem_filter.1 hf).2⟩, λ ⟨hi, hf⟩, ⟨⟨i, hi⟩, mem_filter.2 $ ⟨mem_attach _ _, hf⟩, rfl⟩⟩, end } lemma indicator_of_mem (hi : i ∈ s) (f : Π i ∈ s, α) : indicator s f i = f i hi := @dif_pos _ (id _) hi _ _ _ lemma indicator_of_not_mem (hi : i ∉ s) (f : Π i ∈ s, α) : indicator s f i = 0 := @dif_neg _ (id _) hi _ _ _ variables (s i) @[simp] lemma indicator_apply [decidable_eq ι] : indicator s f i = if hi : i ∈ s then f i hi else 0 := by convert rfl lemma indicator_injective : injective (λ f : Π i ∈ s, α, indicator s f) := begin intros a b h, ext i hi, rw [←indicator_of_mem hi a, ←indicator_of_mem hi b], exact congr_fun h i, end lemma support_indicator_subset : ((indicator s f).support : set ι) ⊆ s := begin intros i hi, rw [mem_coe, mem_support_iff] at hi, by_contra, exact hi (indicator_of_not_mem h _), end lemma single_eq_indicator (i : ι) (b : α) : single i b = indicator {i} (λ _ _, b) := by { classical, ext, simp [single_apply, indicator_apply, @eq_comm _ a] } end finsupp