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
ecade774522df64c210e803a16264ec01f2d9767
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/category_theory/linear/default.lean
35cada9a4352f599ffe595d658c46a7c86343270
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,902
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.preadditive import algebra.module.linear_map import algebra.invertible import linear_algebra.basic import algebra.algebra.basic /-! # Linear categories An `R`-linear category is a category in which `X ⟶ Y` is an `R`-module in such a way that composition of morphisms is `R`-linear in both variables. ## Implementation Corresponding to the fact that we need to have an `add_comm_group X` structure in place to talk about a `module R X` structure, we need `preadditive C` as a prerequisite typeclass for `linear R C`. This makes for longer signatures than would be ideal. ## Future work It would be nice to have a usable framework of enriched categories in which this just became a category enriched in `Module R`. -/ universes w v u open category_theory.limits open linear_map namespace category_theory /-- A category is called `R`-linear if `P ⟶ Q` is an `R`-module such that composition is `R`-linear in both variables. -/ class linear (R : Type w) [ring R] (C : Type u) [category.{v} C] [preadditive C] := (hom_module : Π X Y : C, module R (X ⟶ Y) . tactic.apply_instance) (smul_comp' : ∀ (X Y Z : C) (r : R) (f : X ⟶ Y) (g : Y ⟶ Z), (r • f) ≫ g = r • (f ≫ g) . obviously) (comp_smul' : ∀ (X Y Z : C) (f : X ⟶ Y) (r : R) (g : Y ⟶ Z), f ≫ (r • g) = r • (f ≫ g) . obviously) attribute [instance] linear.hom_module restate_axiom linear.smul_comp' restate_axiom linear.comp_smul' attribute [simp,reassoc] linear.smul_comp attribute [reassoc, simp] linear.comp_smul -- (the linter doesn't like `simp` on the `_assoc` lemma) end category_theory open category_theory namespace category_theory.linear variables {C : Type u} [category.{v} C] [preadditive C] section End variables {R : Type w} [comm_ring R] [linear R C] instance (X : C) : module R (End X) := by { dsimp [End], apply_instance, } instance (X : C) : algebra R (End X) := algebra.of_module (λ r f g, comp_smul _ _ _ _ _ _) (λ r f g, smul_comp _ _ _ _ _ _) end End section variables {R : Type w} [ring R] [linear R C] section induced_category universes u' variables {C} {D : Type u'} (F : D → C) instance induced_category.category : linear.{w v} R (induced_category C F) := { hom_module := λ X Y, @linear.hom_module R _ C _ _ _ (F X) (F Y), smul_comp' := λ P Q R f f' g, smul_comp' _ _ _ _ _ _, comp_smul' := λ P Q R f g g', comp_smul' _ _ _ _ _ _, } end induced_category variables (R) /-- Composition by a fixed left argument as an `R`-linear map. -/ @[simps] def left_comp {X Y : C} (Z : C) (f : X ⟶ Y) : (Y ⟶ Z) →ₗ[R] (X ⟶ Z) := { to_fun := λ g, f ≫ g, map_add' := by simp, map_smul' := by simp, } /-- Composition by a fixed right argument as an `R`-linear map. -/ @[simps] def right_comp (X : C) {Y Z : C} (g : Y ⟶ Z) : (X ⟶ Y) →ₗ[R] (X ⟶ Z) := { to_fun := λ f, f ≫ g, map_add' := by simp, map_smul' := by simp, } instance {X Y : C} (f : X ⟶ Y) [epi f] (r : R) [invertible r] : epi (r • f) := ⟨λ R g g' H, begin rw [smul_comp, smul_comp, ←comp_smul, ←comp_smul, cancel_epi] at H, simpa [smul_smul] using congr_arg (λ f, ⅟r • f) H, end⟩ instance {X Y : C} (f : X ⟶ Y) [mono f] (r : R) [invertible r] : mono (r • f) := ⟨λ R g g' H, begin rw [comp_smul, comp_smul, ←smul_comp, ←smul_comp, cancel_mono] at H, simpa [smul_smul] using congr_arg (λ f, ⅟r • f) H, end⟩ end section variables {S : Type w} [comm_ring S] [linear S C] /-- Composition as a bilinear map. -/ @[simps] def comp (X Y Z : C) : (X ⟶ Y) →ₗ[S] ((Y ⟶ Z) →ₗ[S] (X ⟶ Z)) := { to_fun := λ f, left_comp S Z f, map_add' := by { intros, ext, simp, }, map_smul' := by { intros, ext, simp, }, } end end category_theory.linear
24d1be71f3e58f7c1da966fb011438a25a86031c
e07b1aca72e83a272dd59d24c6e0fa246034d774
/src/tutorials/00_first_proofs.lean
7163c062a3c7188c4e37b810816f7c543dce57d7
[ "Apache-2.0", "LicenseRef-scancode-unknown-license-reference" ]
permissive
pedrominicz/learn
637a343bd4f8669d76819ac660a2d2d3e0958710
b79b802a9846c86c21d4b6f3e17af36e7382f0ef
refs/heads/master
1,671,746,990,402
1,670,778,113,000
1,670,778,113,000
265,735,177
1
0
null
null
null
null
UTF-8
Lean
false
false
21,054
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix `is_a_max_of`:55 := is_max /- Let's prove something now! A set of real numbers has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin cases hx with x_in x_up, cases hy with y_in y_up, specialize x_up y y_in, specialize y_up x x_in, linarith end lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- the assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning; we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement `a ≤ y`. More precisely, this assumption is the abbreviation of `∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≤ y`. One could argue a three-line-long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what `linarith` did for us. It invoked the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function sending a proof `q'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix `is_an_inf_of`:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ example {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin intro y, contrapose, push_neg, intro h, exact hx.2 y h, end lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common that it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, intro h, use (y-x)/2, split; linarith, end lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `ε` such that `ε` is positive and `x + ε < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, from λ h, ⟨(y-x)/2, by linarith, by linarith⟩, end example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `h` of `P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocations, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, by_contradiction H, push_neg at H, have := calc y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, linarith, end example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's set up notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ example {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin apply le_of_le_add_eps, intros ε ε_pos, cases hu ε ε_pos with N HN, calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith ... ≤ x + |u N - x| : add_le_add (by refl) (le_abs_self _) ... ≤ x + ε : add_le_add (by refl) (HN N (by unfold ge)) end -- If y ≤ u n for all n and u n goes to x then y ≤ x lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≤ x + ε for all positive ε. -- Let ε be any positive real intros ε ε_pos, -- we now specialize our limit assumption to this `ε`, and immediately -- fix a `N` as promised by the definition. cases hu ε ε_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d` -- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_left` which -- says `a ≤ max a b`. Actually there is a more specific lemma `le_abs_self` ... ≤ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ example (n : ℕ) : 1/(n+1 : ℝ) > 0 := begin suffices : (n + 1 : ℝ) > 0, { exact one_div_pos_of_pos this }, norm_cast, exact nat.succ_pos n end lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos_of_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `λ`. We get down to: -/ example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := λ n, one_div_pos_of_pos (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ example : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { cases this with N HN, use N, intros n Hn, rw [div_le_iff', ←div_le_iff ε_pos], replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, exact_mod_cast nat.succ_pos n }, exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε) end lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)` -- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff ε_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε), end /- We can now put all pieces together, with almost no new things to explain. -/ example (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, have : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, apply inf_lt h, have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, choose u hu using this, use u, split, { intros ε ε_pos, cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n+1) : (hu n).2 ... ≤ x + ε : add_le_add (by refl) (H n hn), rw abs_of_nonneg; linarith }, { intro n, exact (hu n).1 } }, { intro h, rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros ε ε_pos, -- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N` cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≤ x + ε : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
97006f9c62991c4e2774d5f4e608a085dbc882b8
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/nat/gcd.lean
630226e3f23085d420e4cf0d7beeb84a0ff86c57
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
16,959
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import data.nat.basic /-! # Definitions and properties of `gcd`, `lcm`, and `coprime` -/ namespace nat /-! ### `gcd` -/ theorem gcd_dvd (m n : ℕ) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (λn, by rw gcd_zero_left; exact ⟨dvd_zero n, dvd_refl n⟩) (λm n npos, by rw ←gcd_rec; exact λ ⟨IH₁, IH₂⟩, ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (m n : ℕ) : gcd m n ∣ m := (gcd_dvd m n).left theorem gcd_dvd_right (m n : ℕ) : gcd m n ∣ n := (gcd_dvd m n).right theorem gcd_le_left {m} (n) (h : 0 < m) : gcd m n ≤ m := le_of_dvd h $ gcd_dvd_left m n theorem gcd_le_right (m) {n} (h : 0 < n) : gcd m n ≤ n := le_of_dvd h $ gcd_dvd_right m n theorem dvd_gcd {m n k : ℕ} : k ∣ m → k ∣ n → k ∣ gcd m n := gcd.induction m n (λn _ kn, by rw gcd_zero_left; exact kn) (λn m mpos IH H1 H2, by rw gcd_rec; exact IH ((dvd_mod_iff H1).2 H2) H1) theorem dvd_gcd_iff {m n k : ℕ} : k ∣ gcd m n ↔ k ∣ m ∧ k ∣ n := iff.intro (λ h, ⟨dvd_trans h (gcd_dvd m n).left, dvd_trans h (gcd_dvd m n).right⟩) (λ h, dvd_gcd h.left h.right) theorem gcd_comm (m n : ℕ) : gcd m n = gcd n m := dvd_antisymm (dvd_gcd (gcd_dvd_right m n) (gcd_dvd_left m n)) (dvd_gcd (gcd_dvd_right n m) (gcd_dvd_left n m)) theorem gcd_eq_left_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd m n = m := ⟨λ h, by rw [gcd_rec, mod_eq_zero_of_dvd h, gcd_zero_left], λ h, h ▸ gcd_dvd_right m n⟩ theorem gcd_eq_right_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd n m = m := by rw gcd_comm; apply gcd_eq_left_iff_dvd theorem gcd_assoc (m n k : ℕ) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_left m n)) (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_left n k))) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_right n k))) @[simp] theorem gcd_one_right (n : ℕ) : gcd n 1 = 1 := eq.trans (gcd_comm n 1) $ gcd_one_left n theorem gcd_mul_left (m n k : ℕ) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (λk, by repeat {rw mul_zero <|> rw gcd_zero_left}) (λk n H IH, by rwa [←mul_mod_mul_left, ←gcd_rec, ←gcd_rec] at IH) theorem gcd_mul_right (m n k : ℕ) : gcd (m * n) (k * n) = gcd m k * n := by rw [mul_comm m n, mul_comm k n, mul_comm (gcd m k) n, gcd_mul_left] theorem gcd_pos_of_pos_left {m : ℕ} (n : ℕ) (mpos : 0 < m) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_left m n) mpos theorem gcd_pos_of_pos_right (m : ℕ) {n : ℕ} (npos : 0 < n) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_right m n) npos theorem eq_zero_of_gcd_eq_zero_left {m n : ℕ} (H : gcd m n = 0) : m = 0 := or.elim (eq_zero_or_pos m) id (assume H1 : 0 < m, absurd (eq.symm H) (ne_of_lt (gcd_pos_of_pos_left _ H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : ℕ} (H : gcd m n = 0) : n = 0 := by rw gcd_comm at H; exact eq_zero_of_gcd_eq_zero_left H theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m / k) (n / k) = gcd m n / k := or.elim (eq_zero_or_pos k) (λk0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right]) (λH3, nat.eq_of_mul_eq_mul_right H3 $ by rw [ nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right, nat.div_mul_cancel H1, nat.div_mul_cancel H2]) theorem gcd_dvd_gcd_of_dvd_left {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd m n ∣ gcd k n := dvd_gcd (dvd.trans (gcd_dvd_left m n) H) (gcd_dvd_right m n) theorem gcd_dvd_gcd_of_dvd_right {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd n m ∣ gcd n k := dvd_gcd (gcd_dvd_left n m) (dvd.trans (gcd_dvd_right n m) H) theorem gcd_dvd_gcd_mul_left (m n k : ℕ) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (m n k : ℕ) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (m n k : ℕ) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : ℕ) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {m n : ℕ} (H : m ∣ n) : gcd m n = m := dvd_antisymm (gcd_dvd_left _ _) (dvd_gcd (dvd_refl _) H) theorem gcd_eq_right {m n : ℕ} (H : n ∣ m) : gcd m n = n := by rw [gcd_comm, gcd_eq_left H] @[simp] lemma gcd_mul_left_left (m n : ℕ) : gcd (m * n) n = n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (dvd_mul_left _ _) (dvd_refl _)) @[simp] lemma gcd_mul_left_right (m n : ℕ) : gcd n (m * n) = n := by rw [gcd_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_left (m n : ℕ) : gcd (n * m) n = n := by rw [mul_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_right (m n : ℕ) : gcd n (n * m) = n := by rw [gcd_comm, gcd_mul_right_left] @[simp] lemma gcd_gcd_self_right_left (m n : ℕ) : gcd m (gcd m n) = gcd m n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (gcd_dvd_left _ _) (dvd_refl _)) @[simp] lemma gcd_gcd_self_right_right (m n : ℕ) : gcd m (gcd n m) = gcd n m := by rw [gcd_comm n m, gcd_gcd_self_right_left] @[simp] lemma gcd_gcd_self_left_right (m n : ℕ) : gcd (gcd n m) m = gcd n m := by rw [gcd_comm, gcd_gcd_self_right_right] @[simp] lemma gcd_gcd_self_left_left (m n : ℕ) : gcd (gcd m n) m = gcd m n := by rw [gcd_comm m n, gcd_gcd_self_left_right] lemma gcd_add_mul_self (m n k : ℕ) : gcd m (n + k * m) = gcd m n := by simp [gcd_rec m (n + k * m), gcd_rec m n] theorem gcd_eq_zero_iff {i j : ℕ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin split, { intro h, exact ⟨eq_zero_of_gcd_eq_zero_left h, eq_zero_of_gcd_eq_zero_right h⟩, }, { intro h, rw [h.1, h.2], exact nat.gcd_zero_right _ } end /-! ### `lcm` -/ theorem lcm_comm (m n : ℕ) : lcm m n = lcm n m := by delta lcm; rw [mul_comm, gcd_comm] @[simp] theorem lcm_zero_left (m : ℕ) : lcm 0 m = 0 := by delta lcm; rw [zero_mul, nat.zero_div] @[simp] theorem lcm_zero_right (m : ℕ) : lcm m 0 = 0 := lcm_comm 0 m ▸ lcm_zero_left m @[simp] theorem lcm_one_left (m : ℕ) : lcm 1 m = m := by delta lcm; rw [one_mul, gcd_one_left, nat.div_one] @[simp] theorem lcm_one_right (m : ℕ) : lcm m 1 = m := lcm_comm 1 m ▸ lcm_one_left m @[simp] theorem lcm_self (m : ℕ) : lcm m m = m := or.elim (eq_zero_or_pos m) (λh, by rw [h, lcm_zero_left]) (λh, by delta lcm; rw [gcd_self, nat.mul_div_cancel _ h]) theorem dvd_lcm_left (m n : ℕ) : m ∣ lcm m n := dvd.intro (n / gcd m n) (nat.mul_div_assoc _ $ gcd_dvd_right m n).symm theorem dvd_lcm_right (m n : ℕ) : n ∣ lcm m n := lcm_comm n m ▸ dvd_lcm_left n m theorem gcd_mul_lcm (m n : ℕ) : gcd m n * lcm m n = m * n := by delta lcm; rw [nat.mul_div_cancel' (dvd.trans (gcd_dvd_left m n) (dvd_mul_right m n))] theorem lcm_dvd {m n k : ℕ} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (eq_zero_or_pos k) (λh, by rw h; exact dvd_zero _) (λkpos, dvd_of_mul_dvd_mul_left (gcd_pos_of_pos_left n (pos_of_dvd_of_pos H1 kpos)) $ by rw [gcd_mul_lcm, ←gcd_mul_right, mul_comm n k]; exact dvd_gcd (mul_dvd_mul_left _ H2) (mul_dvd_mul_right H1 _)) theorem lcm_assoc (m n k : ℕ) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm (lcm_dvd (lcm_dvd (dvd_lcm_left m (lcm n k)) (dvd.trans (dvd_lcm_left n k) (dvd_lcm_right m (lcm n k)))) (dvd.trans (dvd_lcm_right n k) (dvd_lcm_right m (lcm n k)))) (lcm_dvd (dvd.trans (dvd_lcm_left m n) (dvd_lcm_left (lcm m n) k)) (lcm_dvd (dvd.trans (dvd_lcm_right m n) (dvd_lcm_left (lcm m n) k)) (dvd_lcm_right (lcm m n) k))) theorem lcm_ne_zero {m n : ℕ} (hm : m ≠ 0) (hn : n ≠ 0) : lcm m n ≠ 0 := by { intro h, simpa [h, hm, hn] using gcd_mul_lcm m n, } /-! ### `coprime` See also `nat.coprime_of_dvd` and `nat.coprime_of_dvd'` to prove `nat.coprime m n`. -/ instance (m n : ℕ) : decidable (coprime m n) := by unfold coprime; apply_instance theorem coprime_iff_gcd_eq_one {m n : ℕ} : coprime m n ↔ gcd m n = 1 := iff.rfl theorem coprime.gcd_eq_one {m n : ℕ} : coprime m n → gcd m n = 1 := id theorem coprime.symm {m n : ℕ} : coprime n m → coprime m n := (gcd_comm m n).trans theorem coprime_comm {m n : ℕ} : coprime n m ↔ coprime m n := ⟨coprime.symm, coprime.symm⟩ theorem coprime.dvd_of_dvd_mul_right {m n k : ℕ} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := let t := dvd_gcd (dvd_mul_left k m) H2 in by rwa [gcd_mul_left, H1.gcd_eq_one, mul_one] at t theorem coprime.dvd_of_dvd_mul_left {m n k : ℕ} (H1 : coprime k m) (H2 : k ∣ m * n) : k ∣ n := by rw mul_comm at H2; exact H1.dvd_of_dvd_mul_right H2 theorem coprime.gcd_mul_left_cancel {k : ℕ} (m : ℕ) {n : ℕ} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, by rw [coprime, gcd_assoc, H.symm.gcd_eq_one, gcd_one_right], dvd_antisymm (dvd_gcd (H1.dvd_of_dvd_mul_left (gcd_dvd_left _ _)) (gcd_dvd_right _ _)) (gcd_dvd_gcd_mul_left _ _ _) theorem coprime.gcd_mul_right_cancel (m : ℕ) {k n : ℕ} (H : coprime k n) : gcd (m * k) n = gcd m n := by rw [mul_comm m k, H.gcd_mul_left_cancel m] theorem coprime.gcd_mul_left_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (k * n) = gcd m n := by rw [gcd_comm m n, gcd_comm m (k * n), H.gcd_mul_left_cancel n] theorem coprime.gcd_mul_right_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (n * k) = gcd m n := by rw [mul_comm n k, H.gcd_mul_left_cancel_right n] theorem coprime_div_gcd_div_gcd {m n : ℕ} (H : 0 < gcd m n) : coprime (m / gcd m n) (n / gcd m n) := by rw [coprime_iff_gcd_eq_one, gcd_div (gcd_dvd_left m n) (gcd_dvd_right m n), nat.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : ℕ} (dgt1 : 1 < d) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := λ co, not_lt_of_ge (le_of_dvd zero_lt_one $ by rw [←co.gcd_eq_one]; exact dvd_gcd Hm Hn) dgt1 theorem exists_coprime {m n : ℕ} (H : 0 < gcd m n) : ∃ m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, coprime_div_gcd_div_gcd H, (nat.div_mul_cancel (gcd_dvd_left m n)).symm, (nat.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_coprime' {m n : ℕ} (H : 0 < gcd m n) : ∃ g m' n', 0 < g ∧ coprime m' n' ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_coprime H in ⟨_, m', n', H, h⟩ theorem coprime.mul {m n k : ℕ} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := (H1.gcd_mul_left_cancel n).trans H2 theorem coprime.mul_right {k m n : ℕ} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := (H1.symm.mul H2.symm).symm theorem coprime.coprime_dvd_left {m k n : ℕ} (H1 : m ∣ k) (H2 : coprime k n) : coprime m n := eq_one_of_dvd_one (by delta coprime at H2; rw ← H2; exact gcd_dvd_gcd_of_dvd_left _ H1) theorem coprime.coprime_dvd_right {m k n : ℕ} (H1 : n ∣ m) (H2 : coprime k m) : coprime k n := (H2.symm.coprime_dvd_left H1).symm theorem coprime.coprime_mul_left {k m n : ℕ} (H : coprime (k * m) n) : coprime m n := H.coprime_dvd_left (dvd_mul_left _ _) theorem coprime.coprime_mul_right {k m n : ℕ} (H : coprime (m * k) n) : coprime m n := H.coprime_dvd_left (dvd_mul_right _ _) theorem coprime.coprime_mul_left_right {k m n : ℕ} (H : coprime m (k * n)) : coprime m n := H.coprime_dvd_right (dvd_mul_left _ _) theorem coprime.coprime_mul_right_right {k m n : ℕ} (H : coprime m (n * k)) : coprime m n := H.coprime_dvd_right (dvd_mul_right _ _) theorem coprime.coprime_div_left {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ m) : coprime (m / a) n := begin by_cases a_split : (a = 0), { subst a_split, rw zero_dvd_iff at dvd, simpa [dvd] using cmn, }, { rcases dvd with ⟨k, rfl⟩, rw nat.mul_div_cancel_left _ (nat.pos_of_ne_zero a_split), exact coprime.coprime_mul_left cmn, }, end theorem coprime.coprime_div_right {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ n) : coprime m (n / a) := (coprime.coprime_div_left cmn.symm dvd).symm lemma coprime_mul_iff_left {k m n : ℕ} : coprime (m * n) k ↔ coprime m k ∧ coprime n k := ⟨λ h, ⟨coprime.coprime_mul_right h, coprime.coprime_mul_left h⟩, λ ⟨h, _⟩, by rwa [coprime_iff_gcd_eq_one, coprime.gcd_mul_left_cancel n h]⟩ lemma coprime_mul_iff_right {k m n : ℕ} : coprime k (m * n) ↔ coprime k m ∧ coprime k n := by simpa only [coprime_comm] using coprime_mul_iff_left lemma coprime.gcd_left (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) n := hmn.coprime_dvd_left $ gcd_dvd_right k m lemma coprime.gcd_right (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime m (gcd k n) := hmn.coprime_dvd_right $ gcd_dvd_right k n lemma coprime.gcd_both (k l : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) (gcd l n) := (hmn.gcd_left k).gcd_right l lemma coprime.mul_dvd_of_dvd_of_dvd {a n m : ℕ} (hmn : coprime m n) (hm : m ∣ a) (hn : n ∣ a) : m * n ∣ a := let ⟨k, hk⟩ := hm in hk.symm ▸ mul_dvd_mul_left _ (hmn.symm.dvd_of_dvd_mul_left (hk ▸ hn)) theorem coprime_one_left : ∀ n, coprime 1 n := gcd_one_left theorem coprime_one_right : ∀ n, coprime n 1 := gcd_one_right theorem coprime.pow_left {m k : ℕ} (n : ℕ) (H1 : coprime m k) : coprime (m ^ n) k := nat.rec_on n (coprime_one_left _) (λn IH, H1.mul IH) theorem coprime.pow_right {m k : ℕ} (n : ℕ) (H1 : coprime k m) : coprime k (m ^ n) := (H1.symm.pow_left n).symm theorem coprime.pow {k l : ℕ} (m n : ℕ) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) := (H1.pow_left _).pow_right _ theorem coprime.eq_one_of_dvd {k m : ℕ} (H : coprime k m) (d : k ∣ m) : k = 1 := by rw [← H.gcd_eq_one, gcd_eq_left d] @[simp] theorem coprime_zero_left (n : ℕ) : coprime 0 n ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_zero_right (n : ℕ) : coprime n 0 ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_one_left_iff (n : ℕ) : coprime 1 n ↔ true := by simp [coprime] @[simp] theorem coprime_one_right_iff (n : ℕ) : coprime n 1 ↔ true := by simp [coprime] @[simp] theorem coprime_self (n : ℕ) : coprime n n ↔ n = 1 := by simp [coprime] /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. -/ def prod_dvd_and_dvd_of_dvd_prod {m n k : ℕ} (H : k ∣ m * n) : { d : {m' // m' ∣ m} × {n' // n' ∣ n} // k = d.1 * d.2 } := begin cases h0 : (gcd k m), case nat.zero { have : k = 0 := eq_zero_of_gcd_eq_zero_left h0, subst this, have : m = 0 := eq_zero_of_gcd_eq_zero_right h0, subst this, exact ⟨⟨⟨0, dvd_refl 0⟩, ⟨n, dvd_refl n⟩⟩, (zero_mul n).symm⟩ }, case nat.succ : tmp { have hpos : 0 < gcd k m := h0.symm ▸ nat.zero_lt_succ _; clear h0 tmp, have hd : gcd k m * (k / gcd k m) = k := (nat.mul_div_cancel' (gcd_dvd_left k m)), refine ⟨⟨⟨gcd k m, gcd_dvd_right k m⟩, ⟨k / gcd k m, _⟩⟩, hd.symm⟩, apply dvd_of_mul_dvd_mul_left hpos, rw [hd, ← gcd_mul_right], exact dvd_gcd (dvd_mul_right _ _) H } end theorem gcd_mul_dvd_mul_gcd (k m n : ℕ) : gcd k (m * n) ∣ gcd k m * gcd k n := begin rcases (prod_dvd_and_dvd_of_dvd_prod $ gcd_dvd_right k (m * n)) with ⟨⟨⟨m', hm'⟩, ⟨n', hn'⟩⟩, h⟩, replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := dvd_trans (dvd_mul_right m' n') hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := dvd_trans (dvd_mul_left n' m') hm'n', exact dvd_gcd hn'k hn' } end theorem coprime.gcd_mul (k : ℕ) {m n : ℕ} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := dvd_antisymm (gcd_mul_dvd_mul_gcd k m n) ((h.gcd_both k k).mul_dvd_of_dvd_of_dvd (gcd_dvd_gcd_mul_right_right _ _ _) (gcd_dvd_gcd_mul_left_right _ _ _)) theorem pow_dvd_pow_iff {a b n : ℕ} (n0 : 0 < n) : a ^ n ∣ b ^ n ↔ a ∣ b := begin refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩, cases eq_zero_or_pos (gcd a b) with g0 g0, { simp [eq_zero_of_gcd_eq_zero_right g0] }, rcases exists_coprime' g0 with ⟨g, a', b', g0', co, rfl, rfl⟩, rw [mul_pow, mul_pow] at h, replace h := dvd_of_mul_dvd_mul_right (pow_pos g0' _) h, have := pow_dvd_pow a' n0, rw [pow_one, (co.pow n n).eq_one_of_dvd h] at this, simp [eq_one_of_dvd_one this] end lemma gcd_mul_gcd_of_coprime_of_mul_eq_mul {a b c d : ℕ} (cop : c.coprime d) (h : a * b = c * d) : a.gcd c * b.gcd c = c := begin apply dvd_antisymm, { apply nat.coprime.dvd_of_dvd_mul_right (nat.coprime.mul (cop.gcd_left _) (cop.gcd_left _)), rw ← h, apply mul_dvd_mul (gcd_dvd _ _).1 (gcd_dvd _ _).1 }, { rw [gcd_comm a _, gcd_comm b _], transitivity c.gcd (a * b), rw [h, gcd_mul_right_right d c], apply gcd_mul_dvd_mul_gcd } end end nat
fe3a1f4faa36c19fa20af488ad4a58ef259c4f1e
54deab7025df5d2df4573383df7e1e5497b7a2c2
/data/set/lattice.lean
deb01bb08ec8132f4a40103f9a727e1e69acc7c4
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,675
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors Jeremy Avigad, Leonardo de Moura, Johannes Hölzl -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -/ import logic.basic data.set.basic import order.complete_boolean_algebra category.basic import tactic.finish open function tactic set lattice auto universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} namespace set instance lattice_set : complete_lattice (set α) := { lattice.complete_lattice . le := (⊆), le_refl := subset.refl, le_trans := assume a b c, subset.trans, le_antisymm := assume a b, subset.antisymm, lt := λ x y, x ⊆ y ∧ ¬ y ⊆ x, lt_iff_le_not_le := λ x y, iff.refl _, sup := (∪), le_sup_left := subset_union_left, le_sup_right := subset_union_right, sup_le := assume a b c, union_subset, inf := (∩), inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, le_inf := assume a b c, subset_inter, top := {a | true }, le_top := assume s a h, trivial, bot := ∅, bot_le := assume s a, false.elim, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in } instance : distrib_lattice (set α) := { set.lattice_set with le_sup_inf := assume s t u x ⟨h₁, h₂⟩, match h₁ with | or.inl h₁ := or.inl h₁ | or.inr h₁ := match h₂ with | or.inl h₂ := or.inl h₂ | or.inr h₂ := or.inr ⟨h₁, h₂⟩ end end } lemma monotone_image {f : α → β} : monotone (image f) := assume s t, assume h : s ⊆ t, image_subset _ h /- union and intersection over a family of sets indexed by a type -/ @[reducible] def Union (s : ι → set β) : set β := supr s @[reducible] def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union_eq (x : β) (s : ι → set β) : (x ∈ ⋃ i, s i) = (∃ i, x ∈ s i) := propext ⟨assume ⟨t, ⟨⟨a, (t_eq : t = s a)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq ▸ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter_eq (x : β) (s : ι → set β) : (x ∈ ⋂ i, s i) = (∀ i, x ∈ s i) := propext ⟨assume (h : ∀a ∈ {a : set β | ∃i, a = s i}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : t = s a)⟩, eq.symm ▸ h a⟩ theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem Union_subset_iff {α : Sort u} {s : α → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t):= ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter {α : Sort u} {x : β} {s : α → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := assume h t ⟨a, (eq : t = s a)⟩, eq.symm ▸ h a theorem subset_Inter {t : set β} {s : α → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h @[simp] -- complete_boolean_algebra theorem compl_Union (s : α → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (λ x, begin simp, apply not_exists_iff end) -- classical -- complete_boolean_algebra theorem compl_Inter (s : α → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, begin simp, apply classical.not_forall_iff end) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : α → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : α → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_distrib_Union_left (s : set β) (t : α → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := set.ext (by simp) -- classical theorem union_distrib_Inter_left (s : set β) (t : α → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := set.ext $ assume x, by simp [classical.forall_or_distrib_left] /- bounded unions and intersections -/ theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := show t ≤ (⨅ x ∈ s, u x), -- TODO: should not be necessary when sets' order is based on lattices from le_infi $ assume x, le_infi (h x) theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs @[simp] theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset @[simp] theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by rw insert_of_has_insert; simp @[simp] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset @[simp] theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by rw insert_of_has_insert; simp @[reducible] def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter theorem mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ @[simp] theorem mem_sUnion_eq {x : α} {S : set (set α)} : x ∈ ⋃₀ S = (∃t ∈ S, x ∈ t) := rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := assume : x ∈ t, have x ∈ ⋃₀ S, from mem_sUnion this ht, show false, from hx this theorem mem_sInter {x : α} {t : set α} {S : set (set α)} (h : ∀ t ∈ S, x ∈ t) : x ∈ ⋂₀ S := h @[simp] theorem mem_sInter_eq {x : α} {S : set (set α)} : x ∈ ⋂₀ S = (∀ t ∈ S, x ∈ t) := rfl theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : (⋂₀ S) ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ (⋃₀ S) := le_Sup tS theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : (⋃₀ s) ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl '' S) := set.ext $ assume x, ⟨assume : ¬ (∃s∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : ∀s, s ∈ compl '' S → x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty begin rw ←h, apply inter_subset_inter_left, apply subset_sUnion_of_mem hs end theorem Union_eq_sUnion_image (s : α → set β) : (⋃ i, s i) = ⋃₀ (s '' univ) := by simp theorem Inter_eq_sInter_image {α I : Type} (s : I → set α) : (⋂ i, s i) = ⋂₀ (s '' univ) := by simp lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀i, s i ⊆ t i) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {ι₂ : Sort x} {s : ι → set α} {t : ι₂ → set α} (h : ∀i, ∃j, s i ⊆ t j) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {ι₂ : Sort x} {s : set α} (h : ι → ι₂) : (⋃ i:ι, s) ⊆ (⋃ j:ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := set.ext $ by simp instance : complete_boolean_algebra (set α) := { set.lattice_set with neg := compl, sub := (\), inf_neg_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_neg_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, le_sup_inf := distrib_lattice.le_sup_inf, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (⋂ b ∈ t, s ∪ b) → x ∈ s ∪ (⋂₀ t), by simp; exact assume h, or.imp_right (assume hn : x ∉ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) → x ∈ (⋃ b ∈ t, s ∩ b), by simp; exact id } theorem union_sdiff_same {a b : set α} : a ∪ (b \ a) = a ∪ b := lattice.sup_sub_same @[simp] theorem union_same_compl {a : set α} : a ∪ (-a) = univ := sup_neg_eq_top @[simp] theorem sdiff_singleton_eq_same {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := sub_eq_left $ eq_empty_of_forall_not_mem $ assume x ⟨ht, ha⟩, begin simp at ha, simp [ha] at ht, exact h ht end @[simp] theorem insert_sdiff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_sdiff_same] lemma compl_subset_compl_iff_subset {α : Type u} {x y : set α} : - y ⊆ - x ↔ x ⊆ y := @neg_le_neg_iff_le (set α) _ _ _ section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image], exact ⟨assume ⟨a, h, i, hi⟩, ⟨i, a, h, hi⟩, assume ⟨i, a, h, hi⟩, ⟨a, h, i, hi⟩⟩ end lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, begin simp, exact ⟨x, h, rfl⟩ end end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort w} {f : α → β} {s : ι → set β} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] end preimage instance : monad set := { monad . pure := λ(α : Type u) a, {a}, bind := λ(α β : Type u) s f, ⋃i∈s, f i, map := λ(α β : Type u), set.image, pure_bind := assume α β x f, by simp, bind_assoc := assume α β γ s f g, set.ext $ assume a, by simp; exact ⟨assume ⟨b, ag, a, as, bf⟩, ⟨a, as, b, bf, ag⟩, assume ⟨a, as, b, bf, ag⟩, ⟨b, ag, a, as, bf⟩⟩, id_map := assume α, functor.id_map, bind_pure_comp_eq_map := assume α β f s, set.ext $ by simp [set.image, eq_comm] } section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl lemma fmap_eq_image : f <$> s = f '' s := rfl lemma mem_seq_iff {b : β'} : b ∈ (g <*> s) ↔ (∃(f' : α' → β'), ∃a∈s, f' ∈ g ∧ b = f' a) := begin simp [seq_eq_bind_map], apply exists_congr, intro f', exact ⟨assume ⟨hf', a, ha, h_eq⟩, ⟨a, h_eq.symm, ha, hf'⟩, assume ⟨a, h_eq, ha, hf'⟩, ⟨hf', a, ha, h_eq.symm⟩⟩ end end monad /- disjoint sets -/ section disjoint variable [semilattice_inf_bot α] def disjoint (a b : α) : Prop := a ⊓ b = ⊥ theorem disjoint_symm {a b : α} : disjoint a b → disjoint b a := assume : a ⊓ b = ⊥, show b ⊓ a = ⊥, from this ▸ inf_comm theorem disjoint_bot_left {a : α} : disjoint ⊥ a := bot_inf_eq theorem disjoint_bot_right {a : α} : disjoint a ⊥ := inf_bot_eq end disjoint end set
99101a816056b82de482885509af4c24667cb4f5
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/limits/shapes/binary_products.lean
31ceb17e08c6790d4d929ba47d93ea7385fa2945
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
24,299
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.shapes.terminal /-! # Binary (co)products We define a category `walking_pair`, which is the index category for a binary (co)product diagram. A convenience method `pair X Y` constructs the functor from the walking pair, hitting the given objects. We define `prod X Y` and `coprod X Y` as limits and colimits of such functors. Typeclasses `has_binary_products` and `has_binary_coproducts` assert the existence of (co)limits shaped as walking pairs. We include lemmas for simplifying equations involving projections and coprojections, and define braiding and associating isomorphisms, and the product comparison morphism. -/ universes v u u₂ open category_theory namespace category_theory.limits /-- The type of objects for the diagram indexing a binary (co)product. -/ @[derive decidable_eq, derive inhabited] inductive walking_pair : Type v | left | right open walking_pair instance fintype_walking_pair : fintype walking_pair := { elems := {left, right}, complete := λ x, by { cases x; simp } } variables {C : Type u} [category.{v} C] /-- The diagram on the walking pair, sending the two points to `X` and `Y`. -/ def pair (X Y : C) : discrete walking_pair ⥤ C := discrete.functor (λ j, walking_pair.cases_on j X Y) @[simp] lemma pair_obj_left (X Y : C) : (pair X Y).obj left = X := rfl @[simp] lemma pair_obj_right (X Y : C) : (pair X Y).obj right = Y := rfl section variables {F G : discrete walking_pair.{v} ⥤ C} (f : F.obj left ⟶ G.obj left) (g : F.obj right ⟶ G.obj right) /-- The natural transformation between two functors out of the walking pair, specified by its components. -/ def map_pair : F ⟶ G := { app := λ j, match j with | left := f | right := g end } @[simp] lemma map_pair_left : (map_pair f g).app left = f := rfl @[simp] lemma map_pair_right : (map_pair f g).app right = g := rfl /-- The natural isomorphism between two functors out of the walking pair, specified by its components. -/ @[simps] def map_pair_iso (f : F.obj left ≅ G.obj left) (g : F.obj right ≅ G.obj right) : F ≅ G := { hom := map_pair f.hom g.hom, inv := map_pair f.inv g.inv, hom_inv_id' := begin ext ⟨⟩; { dsimp, simp, } end, inv_hom_id' := begin ext ⟨⟩; { dsimp, simp, } end } end section variables {D : Type u} [category.{v} D] /-- The natural isomorphism between `pair X Y ⋙ F` and `pair (F.obj X) (F.obj Y)`. -/ def pair_comp (X Y : C) (F : C ⥤ D) : pair X Y ⋙ F ≅ pair (F.obj X) (F.obj Y) := map_pair_iso (eq_to_iso rfl) (eq_to_iso rfl) end /-- Every functor out of the walking pair is naturally isomorphic (actually, equal) to a `pair` -/ def diagram_iso_pair (F : discrete walking_pair ⥤ C) : F ≅ pair (F.obj walking_pair.left) (F.obj walking_pair.right) := map_pair_iso (eq_to_iso rfl) (eq_to_iso rfl) /-- A binary fan is just a cone on a diagram indexing a product. -/ abbreviation binary_fan (X Y : C) := cone (pair X Y) /-- The first projection of a binary fan. -/ abbreviation binary_fan.fst {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.left /-- The second projection of a binary fan. -/ abbreviation binary_fan.snd {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.right lemma binary_fan.is_limit.hom_ext {W X Y : C} {s : binary_fan X Y} (h : is_limit s) {f g : W ⟶ s.X} (h₁ : f ≫ s.fst = g ≫ s.fst) (h₂ : f ≫ s.snd = g ≫ s.snd) : f = g := h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂ /-- A binary cofan is just a cocone on a diagram indexing a coproduct. -/ abbreviation binary_cofan (X Y : C) := cocone (pair X Y) /-- The first inclusion of a binary cofan. -/ abbreviation binary_cofan.inl {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.left /-- The second inclusion of a binary cofan. -/ abbreviation binary_cofan.inr {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.right lemma binary_cofan.is_colimit.hom_ext {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s) {f g : s.X ⟶ W} (h₁ : s.inl ≫ f = s.inl ≫ g) (h₂ : s.inr ≫ f = s.inr ≫ g) : f = g := h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂ variables {X Y : C} /-- A binary fan with vertex `P` consists of the two projections `π₁ : P ⟶ X` and `π₂ : P ⟶ Y`. -/ def binary_fan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : binary_fan X Y := { X := P, π := { app := λ j, walking_pair.cases_on j π₁ π₂ }} /-- A binary cofan with vertex `P` consists of the two inclusions `ι₁ : X ⟶ P` and `ι₂ : Y ⟶ P`. -/ def binary_cofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : binary_cofan X Y := { X := P, ι := { app := λ j, walking_pair.cases_on j ι₁ ι₂ }} @[simp] lemma binary_fan.mk_π_app_left {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.left = π₁ := rfl @[simp] lemma binary_fan.mk_π_app_right {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.right = π₂ := rfl @[simp] lemma binary_cofan.mk_ι_app_left {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.left = ι₁ := rfl @[simp] lemma binary_cofan.mk_ι_app_right {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.right = ι₂ := rfl /-- If `s` is a limit binary fan over `X` and `Y`, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ s.X` satisfying `l ≫ s.fst = f` and `l ≫ s.snd = g`. -/ def binary_fan.is_limit.lift' {W X Y : C} {s : binary_fan X Y} (h : is_limit s) (f : W ⟶ X) (g : W ⟶ Y) : {l : W ⟶ s.X // l ≫ s.fst = f ∧ l ≫ s.snd = g} := ⟨h.lift $ binary_fan.mk f g, h.fac _ _, h.fac _ _⟩ /-- If `s` is a colimit binary cofan over `X` and `Y`,, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : s.X ⟶ W` satisfying `s.inl ≫ l = f` and `s.inr ≫ l = g`. -/ def binary_cofan.is_colimit.desc' {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s) (f : X ⟶ W) (g : Y ⟶ W) : {l : s.X ⟶ W // s.inl ≫ l = f ∧ s.inr ≫ l = g} := ⟨h.desc $ binary_cofan.mk f g, h.fac _ _, h.fac _ _⟩ /-- If we have chosen a product of `X` and `Y`, we can access it using `prod X Y` or `X ⨯ Y`. -/ abbreviation prod (X Y : C) [has_limit (pair X Y)] := limit (pair X Y) /-- If we have chosen a coproduct of `X` and `Y`, we can access it using `coprod X Y ` or `X ⨿ Y`. -/ abbreviation coprod (X Y : C) [has_colimit (pair X Y)] := colimit (pair X Y) notation X ` ⨯ `:20 Y:20 := prod X Y notation X ` ⨿ `:20 Y:20 := coprod X Y /-- The projection map to the first component of the product. -/ abbreviation prod.fst {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ X := limit.π (pair X Y) walking_pair.left /-- The projecton map to the second component of the product. -/ abbreviation prod.snd {X Y : C} [has_limit (pair X Y)] : X ⨯ Y ⟶ Y := limit.π (pair X Y) walking_pair.right /-- The inclusion map from the first component of the coproduct. -/ abbreviation coprod.inl {X Y : C} [has_colimit (pair X Y)] : X ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.left /-- The inclusion map from the second component of the coproduct. -/ abbreviation coprod.inr {X Y : C} [has_colimit (pair X Y)] : Y ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.right @[ext] lemma prod.hom_ext {W X Y : C} [has_limit (pair X Y)] {f g : W ⟶ X ⨯ Y} (h₁ : f ≫ prod.fst = g ≫ prod.fst) (h₂ : f ≫ prod.snd = g ≫ prod.snd) : f = g := binary_fan.is_limit.hom_ext (limit.is_limit _) h₁ h₂ @[ext] lemma coprod.hom_ext {W X Y : C} [has_colimit (pair X Y)] {f g : X ⨿ Y ⟶ W} (h₁ : coprod.inl ≫ f = coprod.inl ≫ g) (h₂ : coprod.inr ≫ f = coprod.inr ≫ g) : f = g := binary_cofan.is_colimit.hom_ext (colimit.is_colimit _) h₁ h₂ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `prod.lift f g : W ⟶ X ⨯ Y`. -/ abbreviation prod.lift {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y := limit.lift _ (binary_fan.mk f g) /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `coprod.desc f g : X ⨿ Y ⟶ W`. -/ abbreviation coprod.desc {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W := colimit.desc _ (binary_cofan.mk f g) @[simp, reassoc] lemma prod.lift_fst {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.fst = f := limit.lift_π _ _ @[simp, reassoc] lemma prod.lift_snd {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.snd = g := limit.lift_π _ _ /- The redundant simp lemma linter says that simp can prove the reassoc version of this lemma. -/ @[reassoc, simp] lemma prod.lift_comp_comp {V W X Y : C} [has_limit (pair X Y)] (f : V ⟶ W) (g : W ⟶ X) (h : W ⟶ Y) : prod.lift (f ≫ g) (f ≫ h) = f ≫ prod.lift g h := by tidy @[simp, reassoc] lemma coprod.inl_desc {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inl ≫ coprod.desc f g = f := colimit.ι_desc _ _ @[simp, reassoc] lemma coprod.inr_desc {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inr ≫ coprod.desc f g = g := colimit.ι_desc _ _ /- The redundant simp lemma linter says that simp can prove the reassoc version of this lemma. -/ @[reassoc, simp] lemma coprod.desc_comp_comp {V W X Y : C} [has_colimit (pair X Y)] (f : V ⟶ W) (g : X ⟶ V) (h : Y ⟶ V) : coprod.desc (g ≫ f) (h ≫ f) = coprod.desc g h ≫ f := by tidy instance prod.mono_lift_of_mono_left {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) [mono f] : mono (prod.lift f g) := mono_of_mono_fac $ prod.lift_fst _ _ instance prod.mono_lift_of_mono_right {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) [mono g] : mono (prod.lift f g) := mono_of_mono_fac $ prod.lift_snd _ _ instance coprod.epi_desc_of_epi_left {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) [epi f] : epi (coprod.desc f g) := epi_of_epi_fac $ coprod.inl_desc _ _ instance coprod.epi_desc_of_epi_right {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) [epi g] : epi (coprod.desc f g) := epi_of_epi_fac $ coprod.inr_desc _ _ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ X ⨯ Y` satisfying `l ≫ prod.fst = f` and `l ≫ prod.snd = g`. -/ def prod.lift' {W X Y : C} [has_limit (pair X Y)] (f : W ⟶ X) (g : W ⟶ Y) : {l : W ⟶ X ⨯ Y // l ≫ prod.fst = f ∧ l ≫ prod.snd = g} := ⟨prod.lift f g, prod.lift_fst _ _, prod.lift_snd _ _⟩ /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : X ⨿ Y ⟶ W` satisfying `coprod.inl ≫ l = f` and `coprod.inr ≫ l = g`. -/ def coprod.desc' {W X Y : C} [has_colimit (pair X Y)] (f : X ⟶ W) (g : Y ⟶ W) : {l : X ⨿ Y ⟶ W // coprod.inl ≫ l = f ∧ coprod.inr ≫ l = g} := ⟨coprod.desc f g, coprod.inl_desc _ _, coprod.inr_desc _ _⟩ /-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : X ⟶ Z` induces a morphism `prod.map f g : W ⨯ X ⟶ Y ⨯ Z`. -/ abbreviation prod.map {W X Y Z : C} [has_limits_of_shape.{v} (discrete walking_pair) C] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z := lim.map (map_pair f g) /-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : W ⟶ Z` induces a morphism `coprod.map f g : W ⨿ X ⟶ Y ⨿ Z`. -/ abbreviation coprod.map {W X Y Z : C} [has_colimits_of_shape.{v} (discrete walking_pair) C] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z := colim.map (map_pair f g) section prod_lemmas variable [has_limits_of_shape.{v} (discrete walking_pair) C] @[reassoc] lemma prod.map_fst {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.fst = prod.fst ≫ f := by simp @[reassoc] lemma prod.map_snd {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.snd = prod.snd ≫ g := by simp @[simp] lemma prod_map_id_id {X Y : C} : prod.map (𝟙 X) (𝟙 Y) = 𝟙 _ := by tidy @[simp] lemma prod_lift_fst_snd {X Y : C} : prod.lift prod.fst prod.snd = 𝟙 (X ⨯ Y) := by tidy -- I don't think it's a good idea to make any of the following simp lemmas. @[reassoc] lemma prod_map_map {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) : prod.map (𝟙 X) f ≫ prod.map g (𝟙 B) = prod.map g (𝟙 A) ≫ prod.map (𝟙 Y) f := by tidy @[reassoc] lemma prod_map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) : prod.map (f ≫ g) (𝟙 W) = prod.map f (𝟙 W) ≫ prod.map g (𝟙 W) := by tidy @[reassoc] lemma prod_map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) : prod.map (𝟙 W) (f ≫ g) = prod.map (𝟙 W) f ≫ prod.map (𝟙 W) g := by tidy @[reassoc] lemma prod.lift_map (V W X Y Z : C) (f : V ⟶ W) (g : V ⟶ X) (h : W ⟶ Y) (k : X ⟶ Z) : prod.lift f g ≫ prod.map h k = prod.lift (f ≫ h) (g ≫ k) := by tidy end prod_lemmas section coprod_lemmas variable [has_colimits_of_shape.{v} (discrete walking_pair) C] @[reassoc] lemma coprod.inl_map {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : coprod.inl ≫ coprod.map f g = f ≫ coprod.inl := by simp @[reassoc] lemma coprod.inr_map {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : coprod.inr ≫ coprod.map f g = g ≫ coprod.inr := by simp @[simp] lemma coprod_map_id_id {X Y : C} : coprod.map (𝟙 X) (𝟙 Y) = 𝟙 _ := by tidy @[simp] lemma coprod_desc_inl_inr {X Y : C} : coprod.desc coprod.inl coprod.inr = 𝟙 (X ⨿ Y) := by tidy -- I don't think it's a good idea to make any of the following simp lemmas. @[reassoc] lemma coprod_map_map {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) : coprod.map (𝟙 X) f ≫ coprod.map g (𝟙 B) = coprod.map g (𝟙 A) ≫ coprod.map (𝟙 Y) f := by tidy @[reassoc] lemma coprod_map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) : coprod.map (f ≫ g) (𝟙 W) = coprod.map f (𝟙 W) ≫ coprod.map g (𝟙 W) := by tidy @[reassoc] lemma coprod_map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) : coprod.map (𝟙 W) (f ≫ g) = coprod.map (𝟙 W) f ≫ coprod.map (𝟙 W) g := by tidy @[reassoc] lemma coprod.map_desc {S T U V W : C} (f : U ⟶ S) (g : W ⟶ S) (h : T ⟶ U) (k : V ⟶ W) : coprod.map h k ≫ coprod.desc f g = coprod.desc (h ≫ f) (k ≫ g) := by tidy end coprod_lemmas variables (C) /-- `has_binary_products` represents a choice of product for every pair of objects. -/ class has_binary_products := (has_limits_of_shape : has_limits_of_shape.{v} (discrete walking_pair) C) /-- `has_binary_coproducts` represents a choice of coproduct for every pair of objects. -/ class has_binary_coproducts := (has_colimits_of_shape : has_colimits_of_shape.{v} (discrete walking_pair) C) attribute [instance] has_binary_products.has_limits_of_shape has_binary_coproducts.has_colimits_of_shape @[priority 200] -- see Note [lower instance priority] instance [has_finite_products.{v} C] : has_binary_products.{v} C := { has_limits_of_shape := by apply_instance } @[priority 200] -- see Note [lower instance priority] instance [has_finite_coproducts.{v} C] : has_binary_coproducts.{v} C := { has_colimits_of_shape := by apply_instance } /-- If `C` has all limits of diagrams `pair X Y`, then it has all binary products -/ def has_binary_products_of_has_limit_pair [Π {X Y : C}, has_limit (pair X Y)] : has_binary_products.{v} C := { has_limits_of_shape := { has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm } } /-- If `C` has all colimits of diagrams `pair X Y`, then it has all binary coproducts -/ def has_binary_coproducts_of_has_colimit_pair [Π {X Y : C}, has_colimit (pair X Y)] : has_binary_coproducts.{v} C := { has_colimits_of_shape := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) } } section variables {C} [has_binary_products.{v} C] variables {D : Type u₂} [category.{v} D] [has_binary_products.{v} D] -- FIXME deterministic timeout with `-T50000` /-- The binary product functor. -/ @[simps] def prod_functor : C ⥤ C ⥤ C := { obj := λ X, { obj := λ Y, X ⨯ Y, map := λ Y Z, prod.map (𝟙 X) }, map := λ Y Z f, { app := λ T, prod.map f (𝟙 T) }} /-- The braiding isomorphism which swaps a binary product. -/ @[simps] def prod.braiding (P Q : C) : P ⨯ Q ≅ Q ⨯ P := { hom := prod.lift prod.snd prod.fst, inv := prod.lift prod.snd prod.fst } /-- The braiding isomorphism can be passed through a map by swapping the order. -/ @[reassoc] lemma braid_natural {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) : prod.map f g ≫ (prod.braiding _ _).hom = (prod.braiding _ _).hom ≫ prod.map g f := by tidy @[simp, reassoc] lemma prod.symmetry' (P Q : C) : prod.lift prod.snd prod.fst ≫ prod.lift prod.snd prod.fst = 𝟙 (P ⨯ Q) := by tidy /-- The braiding isomorphism is symmetric. -/ @[reassoc] lemma prod.symmetry (P Q : C) : (prod.braiding P Q).hom ≫ (prod.braiding Q P).hom = 𝟙 _ := by simp /-- The associator isomorphism for binary products. -/ @[simps] def prod.associator (P Q R : C) : (P ⨯ Q) ⨯ R ≅ P ⨯ (Q ⨯ R) := { hom := prod.lift (prod.fst ≫ prod.fst) (prod.lift (prod.fst ≫ prod.snd) prod.snd), inv := prod.lift (prod.lift prod.fst (prod.snd ≫ prod.fst)) (prod.snd ≫ prod.snd) } /-- The product functor can be decomposed. -/ def prod_functor_left_comp (X Y : C) : prod_functor.obj (X ⨯ Y) ≅ prod_functor.obj Y ⋙ prod_functor.obj X := nat_iso.of_components (prod.associator _ _) (by tidy) @[reassoc] lemma prod.pentagon (W X Y Z : C) : prod.map ((prod.associator W X Y).hom) (𝟙 Z) ≫ (prod.associator W (X ⨯ Y) Z).hom ≫ prod.map (𝟙 W) ((prod.associator X Y Z).hom) = (prod.associator (W ⨯ X) Y Z).hom ≫ (prod.associator W X (Y ⨯ Z)).hom := by tidy @[reassoc] lemma prod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : prod.map (prod.map f₁ f₂) f₃ ≫ (prod.associator Y₁ Y₂ Y₃).hom = (prod.associator X₁ X₂ X₃).hom ≫ prod.map f₁ (prod.map f₂ f₃) := by tidy /-- The product comparison morphism. In `category_theory/limits/preserves` we show this is always an iso iff F preserves binary products. -/ def prod_comparison (F : C ⥤ D) (A B : C) : F.obj (A ⨯ B) ⟶ F.obj A ⨯ F.obj B := prod.lift (F.map prod.fst) (F.map prod.snd) /-- Naturality of the prod_comparison morphism in both arguments. -/ @[reassoc] lemma prod_comparison_natural (F : C ⥤ D) {A A' B B' : C} (f : A ⟶ A') (g : B ⟶ B') : F.map (prod.map f g) ≫ prod_comparison F A' B' = prod_comparison F A B ≫ prod.map (F.map f) (F.map g) := begin rw [prod_comparison, prod_comparison, prod.lift_map], apply prod.hom_ext, { simp only [← F.map_comp, category.assoc, prod.lift_fst, prod.map_fst, category.comp_id] }, { simp only [← F.map_comp, category.assoc, prod.lift_snd, prod.map_snd, prod.lift_snd_assoc] }, end @[reassoc] lemma inv_prod_comparison_map_fst (F : C ⥤ D) (A B : C) [is_iso (prod_comparison F A B)] : inv (prod_comparison F A B) ≫ F.map prod.fst = prod.fst := begin erw (as_iso (prod_comparison F A B)).inv_comp_eq, dsimp [as_iso_hom, prod_comparison], rw prod.lift_fst, end @[reassoc] lemma inv_prod_comparison_map_snd (F : C ⥤ D) (A B : C) [is_iso (prod_comparison F A B)] : inv (prod_comparison F A B) ≫ F.map prod.snd = prod.snd := begin erw (as_iso (prod_comparison F A B)).inv_comp_eq, dsimp [as_iso_hom, prod_comparison], rw prod.lift_snd, end /-- If the product comparison morphism is an iso, its inverse is natural. -/ @[reassoc] lemma prod_comparison_inv_natural (F : C ⥤ D) {A A' B B' : C} (f : A ⟶ A') (g : B ⟶ B') [is_iso (prod_comparison F A B)] [is_iso (prod_comparison F A' B')] : inv (prod_comparison F A B) ≫ F.map (prod.map f g) = prod.map (F.map f) (F.map g) ≫ inv (prod_comparison F A' B') := by { erw [(as_iso (prod_comparison F A' B')).eq_comp_inv, category.assoc, (as_iso (prod_comparison F A B)).inv_comp_eq, prod_comparison_natural], refl } variables [has_terminal.{v} C] /-- The left unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.left_unitor (P : C) : ⊤_ C ⨯ P ≅ P := { hom := prod.snd, inv := prod.lift (terminal.from P) (𝟙 _) } /-- The right unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.right_unitor (P : C) : P ⨯ ⊤_ C ≅ P := { hom := prod.fst, inv := prod.lift (𝟙 _) (terminal.from P) } @[reassoc] lemma prod_left_unitor_hom_naturality (f : X ⟶ Y): prod.map (𝟙 _) f ≫ (prod.left_unitor Y).hom = (prod.left_unitor X).hom ≫ f := prod.map_snd _ _ @[reassoc] lemma prod_left_unitor_inv_naturality (f : X ⟶ Y): (prod.left_unitor X).inv ≫ prod.map (𝟙 _) f = f ≫ (prod.left_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod_left_unitor_hom_naturality] @[reassoc] lemma prod_right_unitor_hom_naturality (f : X ⟶ Y): prod.map f (𝟙 _) ≫ (prod.right_unitor Y).hom = (prod.right_unitor X).hom ≫ f := prod.map_fst _ _ @[reassoc] lemma prod_right_unitor_inv_naturality (f : X ⟶ Y): (prod.right_unitor X).inv ≫ prod.map f (𝟙 _) = f ≫ (prod.right_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod_right_unitor_hom_naturality] lemma prod.triangle (X Y : C) : (prod.associator X (⊤_ C) Y).hom ≫ prod.map (𝟙 X) ((prod.left_unitor Y).hom) = prod.map ((prod.right_unitor X).hom) (𝟙 Y) := by tidy end section variables {C} [has_binary_coproducts.{v} C] /-- The braiding isomorphism which swaps a binary coproduct. -/ @[simps] def coprod.braiding (P Q : C) : P ⨿ Q ≅ Q ⨿ P := { hom := coprod.desc coprod.inr coprod.inl, inv := coprod.desc coprod.inr coprod.inl } @[simp] lemma coprod.symmetry' (P Q : C) : coprod.desc coprod.inr coprod.inl ≫ coprod.desc coprod.inr coprod.inl = 𝟙 (P ⨿ Q) := by tidy /-- The braiding isomorphism is symmetric. -/ lemma coprod.symmetry (P Q : C) : (coprod.braiding P Q).hom ≫ (coprod.braiding Q P).hom = 𝟙 _ := by simp /-- The associator isomorphism for binary coproducts. -/ @[simps] def coprod.associator (P Q R : C) : (P ⨿ Q) ⨿ R ≅ P ⨿ (Q ⨿ R) := { hom := coprod.desc (coprod.desc coprod.inl (coprod.inl ≫ coprod.inr)) (coprod.inr ≫ coprod.inr), inv := coprod.desc (coprod.inl ≫ coprod.inl) (coprod.desc (coprod.inr ≫ coprod.inl) coprod.inr) } lemma coprod.pentagon (W X Y Z : C) : coprod.map ((coprod.associator W X Y).hom) (𝟙 Z) ≫ (coprod.associator W (X ⨿ Y) Z).hom ≫ coprod.map (𝟙 W) ((coprod.associator X Y Z).hom) = (coprod.associator (W ⨿ X) Y Z).hom ≫ (coprod.associator W X (Y ⨿ Z)).hom := by tidy lemma coprod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : coprod.map (coprod.map f₁ f₂) f₃ ≫ (coprod.associator Y₁ Y₂ Y₃).hom = (coprod.associator X₁ X₂ X₃).hom ≫ coprod.map f₁ (coprod.map f₂ f₃) := by tidy variables [has_initial.{v} C] /-- The left unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.left_unitor (P : C) : ⊥_ C ⨿ P ≅ P := { hom := coprod.desc (initial.to P) (𝟙 _), inv := coprod.inr } /-- The right unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.right_unitor (P : C) : P ⨿ ⊥_ C ≅ P := { hom := coprod.desc (𝟙 _) (initial.to P), inv := coprod.inl } lemma coprod.triangle (X Y : C) : (coprod.associator X (⊥_ C) Y).hom ≫ coprod.map (𝟙 X) ((coprod.left_unitor Y).hom) = coprod.map ((coprod.right_unitor X).hom) (𝟙 Y) := by tidy end end category_theory.limits
d8286256b9603cd916b6559830c3420af295b3de
3bdd27ffdff3ffa22d4bb010eba695afcc96bc4a
/src/combinatorics/simplicial_complex/to_move/list.lean
b1715dbc826cdd70aaf9506365589ac2bc5d1126
[]
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,218
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 data.list.basic lemma list.exists_min {α : Type*} [semilattice_inf α] : ∀ {s : list α} (hs₁ : s ≠ []), ∃ z, ∀ y ∈ s, z ≤ y | [] h := (h rfl).elim | [x] h := ⟨x, by simp⟩ | (x :: y :: ys) _ := begin rcases list.exists_min (list.cons_ne_nil y ys) with ⟨t, ht⟩, refine ⟨t ⊓ x, _⟩, rintro z (rfl | rfl | hz), { apply inf_le_right }, { apply le_trans inf_le_left (ht z (list.mem_cons_self z ys)) }, { apply le_trans inf_le_left (ht z (list.mem_cons_of_mem _ hz)) } end lemma list.exists_min_of_inf_closed {α : Type*} [semilattice_inf α] {s : list α} (hs₁ : s ≠ []) (hs₂ : ∀ x y ∈ s, x ⊓ y ∈ s) : ∃ z ∈ s, ∀ y ∈ s, z ≤ y := begin have hs'₁ : s.attach ≠ [] := by simpa using hs₁, letI : semilattice_inf {y // y ∈ s} := subtype.semilattice_inf (λ x y hx hy, hs₂ x y hx hy), rcases list.exists_min hs'₁ with ⟨⟨x, hx₁⟩, hx₂⟩, refine ⟨x, hx₁, λ y hy, hx₂ ⟨y, hy⟩ _⟩, simp only [list.mem_attach], end
bb6d9d743c7c7b7c880dc973c8811c3eb03b317e
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/undergraduate/MAS114/Semester 1/Q02.lean
d7c25a797468f813fc6a5cbf3b97efd58c64788b
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,615
lean
import data.real.basic tactic.norm_num namespace MAS114 namespace exercises_1 namespace Q02 /- part (i) We are asked about the following definition: f : ℕ → ℝ is given by f n = sqrt n. There are two wrinkles when formalising this in Lean. Firstly, like most things with reals, we need to mark the definition as noncomputable. In Lean, "computation" means "exact computation", and we do not have algorithms for exact computation with real numbers. Secondly, the Lean library defines a function real.sqrt : ℝ → ℝ, which is the ordinary square root for nonnegative arguments, but is zero for negative arguments, so it does not satisfy (real.sqrt x) ^ 2 = x in general. Because of this, we feel obliged to provide a proof that our function f has the expected property (f n) ^ 2 = n. Note here that the right hand side of this equation involves an implicit cast from ℕ to ℝ. We will have to do quite a lot of messing around with casts like this. -/ noncomputable def f : ℕ → ℝ := λ n, real.sqrt n lemma f_spec : ∀ n : ℕ, (f n) ^ 2 = n := begin /- Let `n` be an arbitrary natural number -/ intro n, /- The theorem `nat.zero_le n` says that `n ≤ 0` in `ℕ`. To convert this to an inequality in `ℝ`, we use the theorem `nat.cast_le`. This gives a bidirectional implication, and we use the suffix `.mpr` to refer to the right-to-left implication. -/ have h0 : (0 : ℝ) ≤ (n : ℝ) := nat.cast_le.mpr (nat.zero_le n), /- From the inequality `h0` we can deduce that `max 0 n = n` using the theorem `max_eq_right`. This is valid in any linearly ordered set. The standard library defines a default linear order on `ℝ` by the typeclass instance mechanism, to be discussed elsewhere. Note that the left hand side of our equation is in `ℝ` whereas the right hand side is apparently in `ℕ`. Lean digests the left hand side first, realises that the right hand side must be interpreted as a real number to ensure that everything is consistent, and so silently applies the standard map `ℕ → ℝ` (which may be called "cast" or "coercion"). -/ have h1 : max (0 : ℝ) (n : ℝ) = n := max_eq_right h0, /- Here `pow_two (f n)` is the equation `(f n) ^ 2 = (f n) * (f n)`. On the other hand, `real.sqrt_prop n` says that `f n ≥ 0 ∧ (f n) * (f n) = max 0 n`. We use the suffix `.right` to extract the equation `(f n) * (f n) = max 0 n`. Next, given equations `u : a = b` and `v : b = c`, Lean's notation for the resulting equality `a = c` is `u.trans v`. Using this syntax, we combine the three equations that we have proved to get `(f n) ^ 2 = n`. (For longer or more complicated chains of equations, it would be better to use the `calc` construct, which will be discussed elsewhere.) -/ exact (pow_two (f n)).trans ((real.sqrt_prop n).right.trans h1), end /- f is injective -/ lemma f_inj : function.injective f := begin /- Let `n₀` and `n₁` be arbitrary natural numbers, and let `e` be a proof that `f n₀ = f n₁` in `ℝ`. Our goal is now to prove that `n₀ = n₁`. -/ intros n₀ n₁ e, /- For convenience, we introduce notation for the real numbers corresponding to `n₀` and `n₁`. -/ let n_R₀ := (n₀ : ℝ), let n_R₁ := (n₁ : ℝ), have e₁ : n_R₀ = n_R₁ := calc n_R₀ = (f n₀) ^ 2 : (f_spec n₀).symm ... = (f n₁) ^ 2 : by rw[e] ... = n_R₁ : (f_spec n₁), exact nat.cast_inj.mp e₁, end /- f is not surjective -/ lemma f_not_surj : ¬ (function.surjective f) := begin intro f_surj, rcases (f_surj (1/2)) with ⟨n,e0⟩, let n_R := (n : ℝ), have e1 : 4 * n_R = (((4 * n) : ℕ) : ℝ) := by { dsimp[n_R],rw[nat.cast_mul,nat.cast_bit0,nat.cast_bit0,nat.cast_one] }, let e2 := calc 4 * n_R = 4 * ((f n) ^ 2) : by rw[f_spec n] ... = 4 * ((1 / 2) ^ 2) : by rw[e0] ... = 1 : by ring ... = ((1 : ℕ) : ℝ) : by rw[nat.cast_one], let e3 : 4 * n = 1 := nat.cast_inj.mp (e1.symm.trans e2), let e4 := calc 0 = (4 * n) % 4 : (nat.mul_mod_right 4 n).symm ... = 1 % 4 : by rw[e3] ... = 1 : rfl, injection e4, end /- part (ii) -/ /- We are asked to pass judgement on the following "definition" : g : ℤ → ℝ is given by g n = sqrt n. We do this by proving that there is no map g : ℤ → ℝ such that (g n) ^ 2 = n for all n. -/ lemma square_nonneg {α : Type*} [linear_ordered_ring α] (x : α) : 0 ≤ x * x := begin rcases (le_or_gt 0 x) with x_nonneg | x_neg, {exact mul_nonneg x_nonneg x_nonneg,}, {have x_nonpos : x ≤ 0 := le_of_lt x_neg, exact mul_nonneg_of_nonpos_of_nonpos x_nonpos x_nonpos } end /- I am surprised that this does not seem to be in the library. Perhaps I did not look in the right way. -/ lemma neg_one_not_square {α : Type*} [linear_ordered_ring α] (x : α) : x * x ≠ -1 := begin intro e0, have e1 : (0 : α) ≤ -1 := e0.subst (square_nonneg x), have e2 : (-1 : α) < 0 := neg_neg_of_pos zero_lt_one, exact not_le_of_gt e2 e1, end lemma g_does_not_exist : ¬ ∃ (g : ℤ → ℝ), (∀ n : ℤ, (g n) ^ 2 = n) := begin rintro ⟨g,g_spec⟩, let x := g ( -1 ), have e0 : -1 = (( -1 : ℤ ) : ℝ) := by simp, have e1 : x * x = -1 := by { rw[← pow_two], dsimp[x], rw[e0], exact g_spec ( -1 )}, exact neg_one_not_square x e1, end /- part (iii) -/ /- We are asked about the following definition: h : ℤ → ℕ is given by h(n) = |n|. There is no problem with formalising the definition. Note, however, that the Lean library defines both int.abs : ℤ → ℤ and int.nat_abs : ℤ → ℕ; we need the latter here. -/ def h : ℤ → ℕ := int.nat_abs /- h is not injective -/ lemma h_not_inj : ¬ (function.injective h) := begin intro h_inj, let e : (1 : ℤ) = (-1 : ℤ) := @h_inj (1 : ℤ) (-1 : ℤ) rfl, injection e, end /- h is surjective -/ lemma h_surj : function.surjective h := begin intro n,use n,refl, end /- part (iv) -/ /- We are asked to pass judgement on the following "definition" : i : ℕ → ℕ is given by i n = 100 - n. We do this by proving that there is no map i : ℕ → ℕ such that (i n) + n = 100 for all n. Note, however, that Lean would happily accept the definition def i (n : ℕ) : ℕ := 100 - n, but it would interpret the minus sign as truncated subtraction, so that i n = 0 for n ≥ 100. -/ lemma i_does_not_exist : ¬ ∃ (i : ℕ → ℕ), (∀ n : ℕ, (i n) + n = 100) := begin rintro ⟨i,i_spec⟩, have e0 : 101 ≤ 100 := (i_spec 101).subst (nat.le_add_left 101 (i 101)), let e1 : 100 < 101 := nat.lt_succ_self 100, exact not_le_of_gt e1 e0, end /- part (v) -/ /- We are asked about the following definition: j : ℤ → ℤ is given by j(n) = - n. There is no problem with formalising the definition. We then prove that j j = 1, so j is self-inverse. We use theorems from the library to deduce from this that j is injective, surjective and bijective, rather than working directly from the definitions. -/ def j : ℤ → ℤ := λ n, - n lemma jj (n : ℤ) : j (j n) = n := by simp[j] lemma j_inj : function.injective j := function.injective_of_left_inverse jj lemma j_surj : function.surjective j := function.surjective_of_has_right_inverse ⟨j,jj⟩ lemma j_bij : function.bijective j := ⟨j_inj,j_surj⟩ /- part (vi) -/ /- We are asked to pass judgement on the following "definition" : k : ℝ → ℤ sends x ∈ ℝ to the closest integer. We define precisely what it means for n to be the closest integer to x : we should have | x - m | > | x - n | for any integer m ≠ n. We then show that if m ∈ ℤ, there is no closest integer to m + 1/2. From this we deduce that there is no function k with the expected properties. This is much harder work than you might think. A lot of the problem is caused by the cast maps ℤ → ℚ → ℝ -/ def is_closest_integer (n : ℤ) (x : ℝ) := ∀ m : ℤ, m ≠ n → abs ( x - m ) > abs ( x - n ) /- This version suggested by Kevin Buzzard -/ lemma no_closest_integer (n m : ℤ) : ¬ (is_closest_integer n ((m : ℝ) + 1/2)) := begin intro h0, let x_R : ℝ := (m : ℝ) + 1/2, let k := 2 * m + 1 - n, by_cases e0 : k = n, {-- In this block we consider the possibility that k = n, and -- show that it is impossible. exfalso, change 2 * m + 1 - n = n at e0, -- I prefer this to dsimp -- every operation I make here is "natural" hence will be in mathlib somewhere -- and I find it using ctrl-space and name guesswork rw [sub_eq_iff_eq_add, ←two_mul, eq_comm, ←sub_eq_iff_eq_add', ←mul_sub] at e0, -- e0 : 2 * (n - m) = 1 have e2 : (2 * (n - m)) % 2 = 0 := int.mul_mod_right _ _, rw [e0, @int.mod_eq_of_lt (1 : ℤ) (2 : ℤ) (dec_trivial) (dec_trivial)] at e2, -- e2 : 1 = 0 cases e2, -- just showing off here },{ let h1 := ne_of_gt (h0 k e0), apply h1, convert abs_neg _, -- get rid of k show (m : ℝ) + 1/2 - ((2 * m + 1 - n) : ℤ) = -(m + 1/2 - n), -- simp does the casts, although it's a sin to apply it when not closing a goal -- instead of simp one should say: -- suffices : -(1 : ℝ) + (2⁻¹ + (↑n + (↑m + -(2 * ↑m)))) = -2⁻¹ + (↑n + -↑m), -- simpa, simp, -- now ring finishes the job ring, } end lemma k_does_not_exist : ¬ ∃ (k : ℝ → ℤ), (∀ x : ℝ, is_closest_integer (k x) x) := begin rintro ⟨k,k_spec⟩, let x : ℝ := ((0 : ℤ) : ℝ) + 1/2, exact no_closest_integer (k x) 0 (k_spec x) end end Q02 end exercises_1 end MAS114
ad5f47e64a873fc804a3864d39a06d091a6a3a2e
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/category/CompHaus.lean
d9484104bddcd695939e52f99e3cb69c43804e3c
[ "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
7,161
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Bhavik Mehta -/ import category_theory.adjunction.reflective import topology.category.Top import topology.stone_cech import category_theory.monad.limits /-! # The category of Compact Hausdorff Spaces We construct the category of compact Hausdorff spaces. The type of compact Hausdorff spaces is denoted `CompHaus`, and it is endowed with a category instance making it a full subcategory of `Top`. The fully faithful functor `CompHaus ⥤ Top` is denoted `CompHaus_to_Top`. **Note:** The file `topology/category/Compactum.lean` provides the equivalence between `Compactum`, which is defined as the category of algebras for the ultrafilter monad, and `CompHaus`. `Compactum_to_CompHaus` is the functor from `Compactum` to `CompHaus` which is proven to be an equivalence of categories in `Compactum_to_CompHaus.is_equivalence`. See `topology/category/Compactum.lean` for a more detailed discussion where these definitions are introduced. -/ universe u open category_theory /-- The type of Compact Hausdorff topological spaces. -/ structure CompHaus := (to_Top : Top) [is_compact : compact_space to_Top] [is_hausdorff : t2_space to_Top] namespace CompHaus instance : inhabited CompHaus := ⟨{to_Top := { α := pempty }}⟩ instance : has_coe_to_sort CompHaus := ⟨Type*, λ X, X.to_Top⟩ instance {X : CompHaus} : compact_space X := X.is_compact instance {X : CompHaus} : t2_space X := X.is_hausdorff instance category : category CompHaus := induced_category.category to_Top instance concrete_category : concrete_category CompHaus := induced_category.concrete_category _ @[simp] lemma coe_to_Top {X : CompHaus} : (X.to_Top : Type*) = X := rfl variables (X : Type*) [topological_space X] [compact_space X] [t2_space X] /-- A constructor for objects of the category `CompHaus`, taking a type, and bundling the compact Hausdorff topology found by typeclass inference. -/ def of : CompHaus := { to_Top := Top.of X, is_compact := ‹_›, is_hausdorff := ‹_› } @[simp] lemma coe_of : (CompHaus.of X : Type _) = X := rfl /-- Any continuous function on compact Hausdorff spaces is a closed map. -/ lemma is_closed_map {X Y : CompHaus} (f : X ⟶ Y) : is_closed_map f := λ C hC, (hC.is_compact.image f.continuous).is_closed /-- Any continuous bijection of compact Hausdorff spaces is an isomorphism. -/ lemma is_iso_of_bijective {X Y : CompHaus} (f : X ⟶ Y) (bij : function.bijective f) : is_iso f := begin let E := equiv.of_bijective _ bij, have hE : continuous E.symm, { rw continuous_iff_is_closed, intros S hS, rw ← E.image_eq_preimage, exact is_closed_map f S hS }, refine ⟨⟨⟨E.symm, hE⟩, _, _⟩⟩, { ext x, apply E.symm_apply_apply }, { ext x, apply E.apply_symm_apply } end /-- Any continuous bijection of compact Hausdorff spaces induces an isomorphism. -/ noncomputable def iso_of_bijective {X Y : CompHaus} (f : X ⟶ Y) (bij : function.bijective f) : X ≅ Y := by letI := is_iso_of_bijective _ bij; exact as_iso f end CompHaus /-- The fully faithful embedding of `CompHaus` in `Top`. -/ @[simps {rhs_md := semireducible}, derive [full, faithful]] def CompHaus_to_Top : CompHaus.{u} ⥤ Top.{u} := induced_functor _ instance CompHaus.forget_reflects_isomorphisms : reflects_isomorphisms (forget CompHaus) := ⟨by introsI A B f hf; exact CompHaus.is_iso_of_bijective _ ((is_iso_iff_bijective ⇑f).mp hf)⟩ /-- (Implementation) The object part of the compactification functor from topological spaces to compact Hausdorff spaces. -/ @[simps] def StoneCech_obj (X : Top) : CompHaus := CompHaus.of (stone_cech X) /-- (Implementation) The bijection of homsets to establish the reflective adjunction of compact Hausdorff spaces in topological spaces. -/ noncomputable def stone_cech_equivalence (X : Top) (Y : CompHaus) : (StoneCech_obj X ⟶ Y) ≃ (X ⟶ CompHaus_to_Top.obj Y) := { to_fun := λ f, { to_fun := f ∘ stone_cech_unit, continuous_to_fun := f.2.comp (@continuous_stone_cech_unit X _) }, inv_fun := λ f, { to_fun := stone_cech_extend f.2, continuous_to_fun := continuous_stone_cech_extend f.2 }, left_inv := begin rintro ⟨f : stone_cech X ⟶ Y, hf : continuous f⟩, ext (x : stone_cech X), refine congr_fun _ x, apply continuous.ext_on dense_range_stone_cech_unit (continuous_stone_cech_extend _) hf, rintro _ ⟨y, rfl⟩, apply congr_fun (stone_cech_extend_extends (hf.comp _)) y, end, right_inv := begin rintro ⟨f : ↥X ⟶ Y, hf : continuous f⟩, ext, exact congr_fun (stone_cech_extend_extends hf) x, end } /-- The Stone-Cech compactification functor from topological spaces to compact Hausdorff spaces, left adjoint to the inclusion functor. -/ noncomputable def Top_to_CompHaus : Top.{u} ⥤ CompHaus.{u} := adjunction.left_adjoint_of_equiv stone_cech_equivalence.{u u} (λ _ _ _ _ _, rfl) lemma Top_to_CompHaus_obj (X : Top) : ↥(Top_to_CompHaus.obj X) = stone_cech X := rfl /-- The category of compact Hausdorff spaces is reflective in the category of topological spaces. -/ noncomputable instance CompHaus_to_Top.reflective : reflective CompHaus_to_Top := { to_is_right_adjoint := ⟨Top_to_CompHaus, adjunction.adjunction_of_equiv_left _ _⟩ } noncomputable instance CompHaus_to_Top.creates_limits : creates_limits CompHaus_to_Top := monadic_creates_limits _ instance CompHaus.has_limits : limits.has_limits CompHaus := has_limits_of_has_limits_creates_limits CompHaus_to_Top instance CompHaus.has_colimits : limits.has_colimits CompHaus := has_colimits_of_reflective CompHaus_to_Top namespace CompHaus /-- An explicit limit cone for a functor `F : J ⥤ CompHaus`, defined in terms of `Top.limit_cone`. -/ def limit_cone {J : Type u} [small_category J] (F : J ⥤ CompHaus.{u}) : limits.cone F := { X := { to_Top := (Top.limit_cone (F ⋙ CompHaus_to_Top)).X, is_compact := begin dsimp [Top.limit_cone], rw ← is_compact_iff_compact_space, apply is_closed.is_compact, have : {u : Π j, F.obj j | ∀ {i j : J} (f : i ⟶ j), F.map f (u i) = u j} = ⋂ (i j : J) (f : i ⟶ j), {u | F.map f (u i) = u j}, by tidy, rw this, apply is_closed_Inter, intros i, apply is_closed_Inter, intros j, apply is_closed_Inter, intros f, apply is_closed_eq; continuity, end, is_hausdorff := by { dsimp [Top.limit_cone], apply_instance } }, π := { app := λ j, (Top.limit_cone (F ⋙ CompHaus_to_Top)).π.app j, -- tidy needs a little help in the `naturality'` field to avoid deterministic timeouts. naturality' := by { intros _ _ _, ext, tidy } } } /-- The limit cone `CompHaus.limit_cone F` is indeed a limit cone. -/ def limit_cone_is_limit {J : Type u} [small_category J] (F : J ⥤ CompHaus.{u}) : limits.is_limit (limit_cone F) := { lift := λ S, (Top.limit_cone_is_limit (F ⋙ CompHaus_to_Top)).lift (CompHaus_to_Top.map_cone S), uniq' := λ S m h, (Top.limit_cone_is_limit _).uniq (CompHaus_to_Top.map_cone S) _ h } end CompHaus
2dbdcb40ad877eecab245e5c87edf7e16bc0d354
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Elab/Deriving/Basic.lean
7119a641e0aa36eb8d559f9dc2821d125ae3464b
[ "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
3,456
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Wojciech Nawrocki -/ import Lean.Elab.Command import Lean.Elab.MutualDef namespace Lean.Elab open Command def DerivingHandler := (typeNames : Array Name) → (args? : Option Syntax) → CommandElabM Bool def DerivingHandlerNoArgs := (typeNames : Array Name) → CommandElabM Bool builtin_initialize derivingHandlersRef : IO.Ref (NameMap DerivingHandler) ← IO.mkRef {} def registerBuiltinDerivingHandlerWithArgs (className : Name) (handler : DerivingHandler) : IO Unit := do let initializing ← IO.initializing unless initializing do throw (IO.userError "failed to register deriving handler, it can only be registered during initialization") if (← derivingHandlersRef.get).contains className then throw (IO.userError s!"failed to register deriving handler, a handler has already been registered for '{className}'") derivingHandlersRef.modify fun m => m.insert className handler def registerBuiltinDerivingHandler (className : Name) (handler : DerivingHandlerNoArgs) : IO Unit := do registerBuiltinDerivingHandlerWithArgs className fun typeNames _ => handler typeNames def defaultHandler (className : Name) (typeNames : Array Name) : CommandElabM Unit := do throwError "default handlers have not been implemented yet, class: '{className}' types: {typeNames}" def applyDerivingHandlers (className : Name) (typeNames : Array Name) (args? : Option Syntax) : CommandElabM Unit := do match (← derivingHandlersRef.get).find? className with | some handler => unless (← handler typeNames args?) do defaultHandler className typeNames | none => defaultHandler className typeNames private def tryApplyDefHandler (className : Name) (declName : Name) : CommandElabM Bool := liftTermElabM none do Term.processDefDeriving className declName @[builtinCommandElab «deriving»] def elabDeriving : CommandElab | `(deriving instance $[$classes $[with $argss?]?],* for $[$declNames],*) => do let declNames ← declNames.mapM resolveGlobalConstNoOverloadWithInfo for cls in classes, args? in argss? do try let className ← resolveGlobalConstNoOverloadWithInfo cls withRef cls do if declNames.size == 1 && args?.isNone then if (← tryApplyDefHandler className declNames[0]) then return () applyDerivingHandlers className declNames args? catch ex => logException ex | _ => throwUnsupportedSyntax structure DerivingClassView where ref : Syntax className : Name args? : Option Syntax def getOptDerivingClasses [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadInfoTree m] (optDeriving : Syntax) : m (Array DerivingClassView) := do match optDeriving with | `(Parser.Command.optDeriving| deriving $[$classes $[with $argss?]?],*) => let mut ret := #[] for cls in classes, args? in argss? do let className ← resolveGlobalConstNoOverloadWithInfo cls ret := ret.push { ref := cls, className := className, args? } return ret | _ => return #[] def DerivingClassView.applyHandlers (view : DerivingClassView) (declNames : Array Name) : CommandElabM Unit := withRef view.ref do applyDerivingHandlers view.className declNames view.args? builtin_initialize registerTraceClass `Elab.Deriving end Lean.Elab
853a522e73eb3964edb06adc8ea8349de18b9bf7
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/data/matrix/pequiv.lean
5c731a29d6ecdd198e24a31cedd41e1039779277
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
4,905
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.matrix.basic data.pequiv /- # partial equivalences for matrices Using partial equivalences to represent matrices. This file introduces the function `pequiv.to_matrix`, which returns a matrix containing ones and zeros. For any partial equivalence `f`, `f.to_matrix i j = 1 ↔ f i = some j`. The following important properties of this function are proved `to_matrix_trans : (f.trans g).to_matrix = f.to_matrix ⬝ g.to_matrix` `to_matrix_symm : f.symm.to_matrix = f.to_matrixᵀ` `to_matrix_refl : (pequiv.refl n).to_matrix = 1` `to_matrix_bot : ⊥.to_matrix = 0` This theory gives the matrix representation of projection linear maps, and their right inverses. For example, the matrix `(single (0 : fin 1) (i : fin n)).to_matrix` corresponds to the the ith projection map from R^n to R. Any injective function `fin m → fin n` gives rise to a `pequiv`, whose matrix is the projection map from R^m → R^n represented by the same function. The transpose of this matrix is the right inverse of this map, sending anything not in the image to zero. ## notations This file uses the notation ` ⬝ ` for `matrix.mul` and `ᵀ` for `matrix.transpose`. -/ namespace pequiv open matrix universes u v variables {k l m n : Type u} variables [fintype k] [fintype l] [fintype m] [fintype n] variables [decidable_eq k] [decidable_eq l] [decidable_eq m] [decidable_eq n] variables {α : Type v} local infix ` ⬝ `:70 := matrix.mul local postfix `ᵀ` : 1500 := transpose /-- `to_matrix` returns a matrix containing ones and zeros. `f.to_matrix i j` is `1` if `f i = some j` and `0` otherwise -/ def to_matrix [has_one α] [has_zero α] (f : m ≃. n) : matrix m n α | i j := if j ∈ f i then 1 else 0 lemma to_matrix_symm [has_one α] [has_zero α] (f : m ≃. n) : (f.symm.to_matrix : matrix n m α) = f.to_matrixᵀ := by ext; simp only [transpose, mem_iff_mem f, to_matrix]; congr @[simp] lemma to_matrix_refl [has_one α] [has_zero α] : ((pequiv.refl n).to_matrix : matrix n n α) = 1 := by ext; simp [to_matrix, one_val]; congr lemma mul_matrix_apply [semiring α] (f : l ≃. m) (M : matrix m n α) (i j) : (f.to_matrix ⬝ M) i j = option.cases_on (f i) 0 (λ fi, M fi j) := begin dsimp [to_matrix, matrix.mul], cases h : f i with fi, { simp [h] }, { rw finset.sum_eq_single fi; simp [h, eq_comm] {contextual := tt} } end lemma matrix_mul_apply [semiring α] (M : matrix l m α) (f : m ≃. n) (i j) : (M ⬝ f.to_matrix) i j = option.cases_on (f.symm j) 0 (λ fj, M i fj) := begin dsimp [to_matrix, matrix.mul], cases h : f.symm j with fj, { simp [h, f.eq_some_iff.symm] }, { conv in (_ ∈ _) { rw ← f.mem_iff_mem }, rw finset.sum_eq_single fj; simp [h, eq_comm] {contextual := tt} } end lemma to_matrix_trans [semiring α] (f : l ≃. m) (g : m ≃. n) : ((f.trans g).to_matrix : matrix l n α) = f.to_matrix ⬝ g.to_matrix := begin ext i j, rw [mul_matrix_apply], dsimp [to_matrix, pequiv.trans], cases f i; simp end @[simp] lemma to_matrix_bot [has_one α] [has_zero α] : ((⊥ : pequiv m n).to_matrix : matrix m n α) = 0 := rfl lemma to_matrix_injective [zero_ne_one_class α] : function.injective (@to_matrix m n _ _ _ _ α _ _) := λ f g, not_imp_not.1 begin simp only [matrix.ext_iff.symm, to_matrix, pequiv.ext_iff, classical.not_forall, exists_imp_distrib], assume i hi, use i, cases hf : f i with fi, { cases hg : g i with gi, { cc }, { use gi, simp } }, { use fi, simp [hf.symm, ne.symm hi] } end lemma to_matrix_swap [ring α] (i j : n) : (equiv.swap i j).to_pequiv.to_matrix = (1 : matrix n n α) - (single i i).to_matrix - (single j j).to_matrix + (single i j).to_matrix + (single j i).to_matrix := begin ext, dsimp [to_matrix, single, equiv.swap_apply_def, equiv.to_pequiv, one_val], split_ifs; simp * at * end @[simp] lemma single_mul_single [semiring α] (a : m) (b : n) (c : k) : ((single a b).to_matrix : matrix _ _ α) ⬝ (single b c).to_matrix = (single a c).to_matrix := by rw [← to_matrix_trans, single_trans_single] lemma single_mul_single_of_ne [semiring α] {b₁ b₂ : n} (hb : b₁ ≠ b₂) (a : m) (c : k) : ((single a b₁).to_matrix : matrix _ _ α) ⬝ (single b₂ c).to_matrix = 0 := by rw [← to_matrix_trans, single_trans_single_of_ne hb, to_matrix_bot] /-- Restatement of `single_mul_single`, which will simplify expressions in `simp` normal form, when associativity may otherwise need to be carefully applied. -/ @[simp] lemma single_mul_single_right [semiring α] (a : m) (b : n) (c : k) (M : matrix k l α) : (single a b).to_matrix ⬝ ((single b c).to_matrix ⬝ M) = (single a c).to_matrix ⬝ M := by rw [← matrix.mul_assoc, single_mul_single] end pequiv
9de0b588e26c7f0f93fec2332b9fc5ffb7bba5db
4919181312c130f03eed71db8c80aa1cbd140256
/src/zfc.lean
c35db6ef44fc4f94f8a3b73a69a4aa41a0ccedfb
[]
no_license
williamdemeo/flypitch
62143800b790f0a0ffcd338c82b6bf3cd401552b
f73127c3ad36e6c2f074a26518dc333f07c1ab85
refs/heads/master
1,587,042,220,139
1,547,238,998,000
1,547,238,998,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,079
lean
import .fol set_theory.zfc tactic.tidy open fol namespace zfc inductive ZFC_rel : ℕ → Type 1 | ϵ : ZFC_rel 2 def L_ZFC : Language.{1} := ⟨λ_, ulift empty, ZFC_rel⟩ /- Note from Mario: should try using L_ZFC' -/ def ZFC_el : L_ZFC.relations 2 := ZFC_rel.ϵ local infix ` ∈' `:100 := bounded_formula_of_relation ZFC_el ---ugly but working (str_formula says it's not well-founded recursion, but it evaluates anyways) --def str_preterm : ∀ n m : ℕ, ℕ → bounded_preterm L_ZFC n m → string -- | n m z &k := "x" ++ to_string(z - k) -- | _ _ _ _ := "h" --def str_term: ∀ n : ℕ, ℕ → bounded_term L_ZFC n → string -- | n m &k := "x" ++ to_string(m - k.val) -- | _ _ _ := "n" --def str_preformula : ∀ n m : ℕ, ℕ → bounded_preformula L_ZFC n m → string -- | _ _ _ (bd_falsum ) := "⊥" -- | n m z (bd_equal a b) := str_preterm n m z a ++ " = " ++ str_preterm n m z b -- | n m z (a ⟹ b) := str_preformula n m z a ++ " ⟹ " ++ str_preformula n m z b -- | n m z (bd_rel _) := "∈" -- | n m z (bd_apprel a b) := str_preformula n (m+1) z a ++ "(" ++ str_term n z b ++ ")" -- | n m z (∀' t) := "(∀x" ++ to_string(z+1) ++ "," ++ str_preformula (n+1) m (z+1) t ++ ")" --def str_formula : ∀ {n : ℕ}, bounded_formula L_ZFC n → ℕ → string -- | n ((f₁ ⟹ (f₂ ⟹ bd_falsum)) ⟹ bd_falsum) m:= "(" ++ str_formula f₁ m ++ "∧" ++ str_formula f₂ m ++ ")" -- | n ((f₁ ⟹ bd_falsum) ⟹ f₂) m := "(" ++ str_formula f₁ m ++ " ∨ " ++ str_formula f₂ m ++ ")" -- | n (bd_equal s1 s2) m := "(" ++ str_term n m s1 ++ " = " ++ str_term n m s2 ++ ")" -- | n (∀' f) m := "(∀x"++ to_string(m + 1) ++ "," ++ (str_formula f (m+1) ) ++ ")" -- | _ bd_falsum _ := "⊥" -- | n (f ⟹ bd_falsum) m := "~" ++ str_formula f m -- | n (bd_apprel (f₁) f₂) m := str_preformula n 1 m f₁ ++ "(" ++ str_term n m f₂ ++ ")" -- | n (bd_imp a b) m := "(" ++ str_formula a m ++ " ⟹ " ++ str_formula b m ++ ")" -- --def print_formula : ∀ {n : ℕ}, bounded_formula L_ZFC n → string := λ n f, str_formula f n notation `lift_cast` := by {repeat{apply nat.succ_le_succ}, apply nat.zero_le} -- section test -- /- ∀ x, ∀ y, x = y → ∀ z, z = x → z = y -/ -- def testsentence : sentence L_ZFC := ∀' ∀' (&1 ≃ &0 ⟹ ∀' (&0 ≃ &2 ⟹ &0 ≃ &1)) -- end test ---------------------------------------------------------------------------- def Class : Type 1 := bounded_formula L_ZFC 1 def small {n} (c : bounded_formula L_ZFC (n+1)) : bounded_formula L_ZFC n := ∃' ∀' (&0 ∈' &1 ⇔ (c ↑' 1 # 1)) def subclass (c₁ c₂ : Class) : sentence L_ZFC := ∀' (c₁ ⟹ c₂) def functional {n} (c : bounded_formula L_ZFC (n+2)) : bounded_formula L_ZFC n := -- ∀x ∃y ∀z, c z x ↔ z = y ∀' ∃' ∀' (c ↑' 1 # 1 ⇔ &0 ≃ &1) def subset : bounded_formula L_ZFC 2 := ∀' (&0 ∈' &1 ⟹ &0 ∈' &2) def is_emptyset : bounded_formula L_ZFC 1 := ∼ ∃' (&0 ∈' &1) def pair : bounded_formula L_ZFC 3 := (&0 ≃ &1 : bounded_formula L_ZFC 3) ⊔ (&0 ≃ &2 : bounded_formula L_ZFC 3) def singl : bounded_formula L_ZFC 2 := &0 ≃ &1 def binary_union : bounded_formula L_ZFC 3 := &0 ∈' &1 ⊔ &0 ∈' &2 def succ : bounded_formula L_ZFC 2 := (&0 ≃ &1 : bounded_formula L_ZFC 2) ⊔ &0 ∈' &1 def ordered_pair : bounded_formula L_ZFC 3 := ∀'(&0 ∈' &1 ⇔ (&0 ≃ &3 : bounded_formula L_ZFC 4) ⊔ ∀'(&0 ∈' &1 ⇔ pair ↑' 1 # 1 ↑' 1 # 1)) -- &0 is an ordered pair of &2 and &1 (z = ⟨x, y⟩) def ordered_pair' : bounded_formula L_ZFC 3 := ∀'(&0 ∈' &3 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 4) ⊔ ∀'(&0 ∈' &1 ⇔ (pair ↑' 1 # 1).cast(lift_cast))) -- &2 is an ordered pair of &1 and &0 (x = ⟨y,z⟩) -- TODO: Make defns like this unnecessary (current effort is subst_var_* in fol.lean) def is_ordered_pair : bounded_formula L_ZFC 1 := ∃' ∃' ordered_pair --&0 is an ordered pair of some two elements-- local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l def relation : bounded_formula L_ZFC 1 := ∀' ((&0 ∈' &1) ⟹ is_ordered_pair ↑' 1 # 1) --&0 is a relation (is a set of ordered pairs) def relation' : bounded_formula L_ZFC 1 := ∀' (((&0 ∈' &1) : bounded_formula L_ZFC 2) ⟹ (is_ordered_pair ⊚ [0])) lemma chk: relation = relation' := sorry --ideally this should be by refl. Comment out the set_option above the defn of subst_var_bounded_formula to see the snag def function : bounded_formula L_ZFC 1 := relation ⊓ ∀'∀'∀'∀'∀'(&1 ∈' &5 ⊓ ordered_pair ↑' 1 # 3 ↑' 1 # 1 ↑' 1 # 0 ⊓ ((&0 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ↑' 1 # 2 ↑' 1 # 1).cast(lift_cast)) ⟹ (&3 ≃ &2 : bounded_formula L_ZFC 6)) -- X is a function iff X is a relation and the following holds: -- ∀x ∀y ∀z ∀w ∀t, ((w ∈ X) ∧ (w = ⟨x, y⟩) ∧ (z ∈ X) ∧ (z = ⟨x, z⟩ ))) → y = z def function' : bounded_formula L_ZFC 1 := relation ⊓ ∀'∀'∀'∀'∀'(((&1 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ⊚ [1,3,4]) ⊓ ((&0 ∈' &5 : bounded_formula L_ZFC 6) ⊓ (ordered_pair ⊚ [0,2,4]))) ⟹ (&3 ≃ &2 : bounded_formula L_ZFC 6)) def fn_app : bounded_formula L_ZFC 3 := ∃'(&0 ∈' &3 ⊓ ∀'(&0 ∈' &1 ⇔ ((&0 ≃ &3): bounded_formula L_ZFC 5) ⊔ (pair ↑' 1 # 1).cast(lift_cast))) -- ⟨&1, &0⟩ ∈ &2 -- &0 = &2(&1) def fn_domain : bounded_formula L_ZFC 2 := ∀'(&0 ∈' &2 ⇔ ∃'∃'(ordered_pair ↑' 1 # 1 ↑' 1 # 1 ⊓ &0 ∈' &3)) -- &1 is the domain of &0 def fn_range : bounded_formula L_ZFC 2 := ∀'(&0 ∈' &2 ⇔ ∃'∃'(∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 6) ⊔ (pair ↑' 1 # 1).cast(lift_cast)) ⊓ &0 ∈' &3)) --&1 is the range of &0 def inverse_relation : bounded_formula L_ZFC 2 := ∀'(&0 ∈' &1 ⇔ ∃'∃'(∃'(∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 7) ⊔ (pair ↑' 1 # 1).cast(lift_cast)) ⊓ &0 ∈' &5 : bounded_formula L_ZFC 6)) ⊓ (ordered_pair'.cast(lift_cast))) -- &0 is the inverse relation of &1 def function_one_one : bounded_formula L_ZFC 1 := function ⊓ ∀' (inverse_relation ⟹ function.cast(lift_cast)) def irreflexive_relation : bounded_formula L_ZFC 2 := (∀'(&0 ∈' &2 ⟹ (∀'(∀'(&0 ∈' &1) ⇔ ((&0 ≃ &2 : bounded_formula L_ZFC 4) ⊔ ∀'(&0 ∈' &1 ⇔ (&0 ≃ &3 : bounded_formula L_ZFC 5))) ⟹ ∼(&0 ∈' &3))))) ⊓ relation.cast(lift_cast) -- &0 is an irreflexive relation on &1 def transitive_relation : bounded_formula L_ZFC 2 := ((∀'∀'∀'((((&2 ∈' &4 : bounded_formula L_ZFC 5) ⊓ (&1 ∈' &4)) ⊓ (&0 ∈' &4)) ⊓ ∃'((ordered_pair ↑' 1 # 1).cast(lift_cast) ⊓ (&0 ∈' &4 : bounded_formula L_ZFC 6)) ⊓ ∃'(ordered_pair.cast(lift_cast) ⊓ &0 ∈' &4 : bounded_formula L_ZFC 6) ⟹ ∃'((ordered_pair ↑' 1 # 2).cast(lift_cast) ⊓ &0 ∈' &4 : bounded_formula L_ZFC 6)))) ⊓ relation.cast(lift_cast) --&0 is a transitive relation on &1 -- X Tr Y iff X is a relation and the following holds: -- ∀u ∀v ∀w, (u ∈ Y ∧ v ∈ Y ∧ w ∈ Y ∧ ⟨u, v⟩ ∈ X ∧ ⟨v,w⟩ ∈ X) → ⟨u,w⟩ ∈ X def partial_order_zfc : bounded_formula L_ZFC 2 := irreflexive_relation ⊓ transitive_relation def connected_relation : bounded_formula L_ZFC 2 := relation ↑' 1 # 1 ⊓ ∀'∀'((bd_and (bd_and (&0 ∈' &3) (&1 ∈' &3)) ∼(bd_equal &0 &1)) ⟹ ∃'((&0 ∈' &3 : bounded_formula L_ZFC 5) ⊓ ((ordered_pair ↑' 1 # 3 ↑' 1 # 3) ⊔ ∀'(&0 ∈' &1 ⇔ (&0 ≃ &2 : bounded_formula L_ZFC 6) ⊔ (pair ↑' 1 # 1).cast(lift_cast))))) --&0 is a connected relation on &1 -- X Con Y iff Rel(X) and ∀u ∀v (u ∈ Y ∧ v ∈ Y ∧ u ≠ v) → ⟨u,v⟩ ∈ X ∨ ⟨v, u⟩ ∈ X def total_order : bounded_formula L_ZFC 2 := irreflexive_relation ⊓ transitive_relation ⊓ connected_relation def well_order : bounded_formula L_ZFC 2 := irreflexive_relation ⊓ ∀'(subset ↑' 1 # 1 ⊓ ∃'(&0 ∈' &1) ⟹ ∃'(&0 ∈' &1 ⊓ ∀'(((&0 ∈' &2) ⊓ ∼(&0 ≃ &1 : bounded_formula L_ZFC 5)) ⟹ ∃'(ordered_pair.cast(lift_cast) ⊓ (&0 ∈' &4 : bounded_formula L_ZFC 6)) ⊓ ∼∃'(∀'(&0 ∈' &1 ⇔ (( &0 ≃ &2 : bounded_formula L_ZFC 7) ⊔ (pair ↑' 1 # 1).cast(lift_cast) )) ⊓ &0 ∈' &4)))) -- &0 well-orders &1 def membership_relation : bounded_formula L_ZFC 1 := relation ⊓ ∀'(&0 ∈' &1 ⇔ ∃'∃'∀'(&0 ∈' &3 ⇔ ((bd_equal &0 &2) ⊔ pair ↑' 1 # 3 ↑' 1 # 3) ⊓ &2 ∈' &1)) -- &0 is E, the membership relation {⟨x,y⟩ | x ∈ y} def transitive_zfc : bounded_formula L_ZFC 1 := ∀' ((&0 ∈' &1) ⟹ subset) --&0 is transitive def fn_zfc_equiv : bounded_formula L_ZFC 3 := ((function_one_one.cast(lift_cast)) ⊓ (fn_domain ↑' 1 # 1)) ⊓ (fn_range ↑' 1 # 2) def zfc_equiv : bounded_formula L_ZFC 2 := ∃' fn_zfc_equiv --&0 ≃ &1, i.e. they are equinumerous def is_powerset : bounded_formula L_ZFC 2 := ∀' ((&0 ∈' &2) ⇔ subset ↑' 1 # 2) --&1 is P(&0) def is_suc_of : bounded_formula L_ZFC 2 := ∀' ((&0 ∈' &2) ⇔ ((&0 ∈' &1) ⊔ ( &0 ≃ &1 : bounded_formula L_ZFC 3))) -- &1 = succ(&0) def is_ordinal : bounded_formula L_ZFC 1 := (∀' ((membership_relation.cast(lift_cast)) ⟹ well_order)) ⊓ transitive_zfc def is_suc_ordinal : bounded_formula L_ZFC 1 := is_ordinal ⊓ ∃' is_suc_of --&0 is a successor ordinal def ordinal_lt : bounded_formula L_ZFC 2 := (is_ordinal.cast(lift_cast)) ⊓ (is_ordinal ↑' 1 # 0) ⊓ (&0 ∈' &1) -- &0 < &1 def ordinal_le : bounded_formula L_ZFC 2 := ordinal_lt ⊔ (bd_equal &0 &1) -- &0 ≤ &1 def is_first_infinite_ordinal : bounded_formula L_ZFC 1 := ∀' ((&0 ∈' &1) ⇔ (((is_emptyset ⊔ is_suc_ordinal)↑' 1 # 1) ⊓ ∀'((&0 ∈' &1) ⟹ ((is_emptyset ⊔ is_suc_ordinal).cast(lift_cast))))) --&0 = ω def is_uncountable_ordinal : bounded_formula L_ZFC 1 := ∀' ((is_first_infinite_ordinal.cast(lift_cast)) ⟹ ∀' (subset.cast(lift_cast) ⟹(∼(zfc_equiv ↑' 1 # 1)))) --&0 ≥ ω₁ def is_first_uncountable_ordinal : bounded_formula L_ZFC 1 := is_uncountable_ordinal ⊓ (∀' ((is_uncountable_ordinal ↑' 1 # 1) ⟹ ordinal_le)) --&0 = ω₁ /- Statement of CH -/ def continuum_hypothesis : sentence L_ZFC := ∀' ∀' ((∃'((is_first_infinite_ordinal ↑' 1 # 1 ↑' 1 # 1) ⊓ (is_powerset ↑' 1 # 2)) ⊓ (is_first_uncountable_ordinal ↑' 1 #0)) ⟹ zfc_equiv) def axiom_of_extensionality : sentence L_ZFC := ∀' ∀' (∀' (&0 ∈' &1 ⇔ &0 ∈' &2) ⟹ &0 ≃ &1) def axiom_of_union : sentence L_ZFC := ∀' (small ∃' (&1 ∈' &0 ⊓ &0 ∈' &2)) -- todo: c can have free variables. Note that c y x is interpreted as y is the image of x def axiom_of_replacement (c : bounded_formula L_ZFC 2) : sentence L_ZFC := -- ∀α small (λy, ∃x, x ∈ α ∧ c y x) functional c ⟹ ∀' (small ∃' (&0 ∈' &2 ⊓ c.cast1)) def axiom_of_powerset : sentence L_ZFC := -- the class of all subsets of x is small ∀' small subset def axiom_of_infinity : sentence L_ZFC := --∀x∃y(x ∈ y ∧ ∀z(z ∈ y → ∃w(z ∈ w ∧ w ∈ y))) ∀' ∃' (&1 ∈' &0 ⊓ ∀'(&0 ∈' &1 ⟹ ∃' (bd_and (&1 ∈' &0) (&0 ∈' &2)))) def axiom_of_choice : sentence L_ZFC := ∀'∀'(fn_domain ⟹ ∃'∀'(&0 ∈' &2 ⟹∀'(fn_app ↑' 1 # 2 ↑' 1 # 2 ⟹ (∀'(fn_app ↑' 1 # 1 ↑' 1 # 4 ↑' 1 # 4 ⊓ ∃'(&0 ∈' &2)) ⟹ &0 ∈' &1)))) def axiom_of_emptyset : sentence L_ZFC := small ⊥ -- todo: c can have free variables def axiom_of_separation (c : Class) : sentence L_ZFC := ∀' (small $ &0 ∈' &1 ⊓ c.cast1) -- the class consisting of the unordered pair {x, y} def axiom_of_pairing : sentence L_ZFC := ∀' ∀' small pair --the class consisting of the ordered pair ⟨x, y⟩ def axiom_of_ordered_pairing : sentence L_ZFC := ∀' ∀' small ordered_pair def ZF : Theory L_ZFC := {axiom_of_extensionality, axiom_of_union, axiom_of_powerset, axiom_of_infinity} ∪ (λ(c : bounded_formula L_ZFC 2), axiom_of_replacement c) '' set.univ def ZFC : Theory L_ZFC := ZF ∪ {axiom_of_choice} /- ZFC formulae in terms of Mario's Set type. Many of these are a lot shorter than the formulae above, largely because things like {x} and {{x},{x,y}} don't have to be existentially instantiated. It might be advantageous to refactor these to match the FOL formulae more precisely. (e.g. ({x} ∈ y) vs (∃ w, w = {x} ∧ w ∈ y) -/ def Set_subset : Set → Set → Prop := Set.subset def Set_is_powerset : Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ w ⊆ y def Set_is_emptyset: Set → Prop := λ x, x = ∅ def Set_pair : Set → Set → Set → Prop := λ x y z, x = {y,z} def Set_ordered_pair : Set → Set → Set → Prop := λ x y z, x = {{y},{y,z}} --TODO : angle bracket notation ⟪x,y⟫ = {{x},{x,y}} def Set_is_ordered_pair: Set → Prop := λ x, ∃ y z, Set_ordered_pair x y z def Set_relation : Set → Prop := λ x, ∀w, w ∈ x ↔ Set_is_ordered_pair w def Set_function : Set → Prop := λ x, Set_relation x ∧ ∀ a b c, ((({{a},{a,b}} ∈ x) ∧ ({{a},{a,c}} ∈ x)) → (b = c)) def Set_fn_app : Set → Set → Set → Prop := λ x y z, {{x},{x,y}} ∈ z def Set_fn_domain : Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ ∃ a b, w = {{a},{a,b}} ∧ a ∈ y def Set_fn_range: Set → Set → Prop := λ x y, ∀ w, w ∈ x ↔ ∃ a b, w = {{a},{a,b}} ∧ b ∈ y def Set_inverse_relation : Set → Set → Prop := λ x y, ∀ a b, {{a},{a,b}} ∈ x ↔ {{b},{b,a}} ∈ y def Set_function_one_one : Set → Prop := λ x, Set_function x ∧ ∀ y, ((Set_inverse_relation x y) → Set_function y) def Set_irreflexive_relation : Set → Set → Prop := λ x y, Set_relation x ∧ ∀ z, z ∈ y → {{z},{z}} ∉ x def Set_transitive_relation : Set → Set → Prop := λ x y, ∀ u v w, ((u ∈ y ∧ v ∈ y ∧ w ∈ w ∧ {{u},{u,v}} ∈ x ∧ {{v},{v,w}} ∈ x) → {{u},{u,w}} ∈ x) def Set_partial_order : Set → Set → Prop := λ x y, Set_irreflexive_relation x y ⊓ Set_transitive_relation x y def Set_connected_relation: Set → Set → Prop := λ x y, Set_relation x ∧ ∀ u v, (u ∈ y ∧ v ∈ y ∧ u ≠ v) → ({{u},{u,v}} ∈ x ∨ {{v},{v,u}} ∈ x) def Set_total_order : Set → Set → Prop := λ X Y, (Set_irreflexive_relation X Y) ∧ (Set_transitive_relation X Y) ∧ (Set_connected_relation X Y) def Set_well_order : Set → Set → Prop := λ x y, Set_irreflexive_relation x y ∧ ∀ z, (z ⊆ y ∧ (z ≠ ∅) → ∃w, (w ∈ z ∧ ∀ v, ((v ∈ z ∧ v ≠ w) → {{w},{w,v}} ∈ x ∧ {{v},{v,w}} ∉ x))) def Set_membership_relation : Set → Prop := λ x, Set_relation x ∧ ∀ w ∈ x, ∃ u v, {{u},{u,v}} = w ∧ u ∈ v def Set_transitive : Set → Prop := λ x, ∀ w, w ∈ x → w ⊆ x def Set_fn_equiv : Set → Set → Set → Prop := λ x y z, Set_function_one_one x ∧ Set_fn_domain x y ∧ Set_fn_range x z def Set_zfc_equiv : Set → Set → Prop := λ x y, ∃ f, Set_fn_equiv f x y def Set_is_suc_of : Set → Set → Prop := λ x y, ∀ w, (w ∈ x ↔ (w ∈ y ∨ w = y)) def Set_is_ordinal : Set → Prop := λ x, (∀ w, (Set_membership_relation w → Set_well_order w x)) ∧ Set_transitive x def Set_is_suc_ordinal : Set → Prop := λ x, Set_is_ordinal x ∧ ∃ w, Set_is_suc_of x w def Set_ordinal_lt : Set → Set → Prop := λ x y, Set_is_ordinal x ∧ Set_is_ordinal y ∧ x ∈ y def Set_ordinal_le : Set → Set → Prop := λ x y, Set_ordinal_lt x y ∨ x = y def Set_is_first_infinite_ordinal : Set → Prop := λ x, ∀ w, (w ∈ x ↔ ((w = ∅ ∨ Set_is_suc_ordinal w) ∧ ∀ z, (z ∈ w → ((z = ∅ ∨ Set_is_suc_ordinal z))))) def Set_is_uncountable_ordinal : Set → Prop := λ x, ∀ w, (Set_is_first_infinite_ordinal w → ∀ z, (Set.subset z w → ¬ Set_zfc_equiv z x)) def Set_is_first_uncountable_ordinal : Set → Prop := λ x, Set_is_uncountable_ordinal x ∧ ∀ w, (Set_is_uncountable_ordinal w → Set_ordinal_le x w) def Set_continuum_hypothesis : Prop := ∀ x y, ((∃ w, (Set_is_first_infinite_ordinal w ∧ Set_is_powerset x w)) ∧ Set_is_first_uncountable_ordinal y) → Set_zfc_equiv x y end zfc
fb0ab8e7f66374315db6172ffe9e17acb0e675c7
a6f55abce20abcd06e718cb3e5fba7bf8a230fa1
/topic/housing.lean
06a35bce003f771cf153e7bd7e0a6d8d33cedf74
[]
no_license
sonna0909/abc
b8a53e906d4d000d1f2347173a1cd4221757fabf
ff7b4c621cdf6d53937f2d1b6def28de2085a2aa
refs/heads/master
1,599,114,664,248
1,573,634,309,000
1,573,634,309,000
219,406,484
0
0
null
null
null
null
UTF-8
Lean
false
false
7,718
lean
[ { "key": "living_room", "title": "Living room", "spelling": "/'liviɳ rum/", "subs": [ { "key": "living_room1", "title": "This is living room", "spelling": "" } ] }, { "key": "bed_room", "title": "Bed room", "spelling": "/'bed rum/", "subs": [ { "key": "2_bed_rooms", "title": "My house is 2 bed rooms", "spelling": "" } ] }, { "key": "bath_room", "title": "Bath room", "spelling": "/'bɑ:θ rum/", "subs": [ { "key": "bath_room1", "title": "This is bath room", "spelling": "" } ] }, { "key": "dinning_room", "title": "Dinning room", "spelling": "/daining rum/", "subs": [ { "key": "dinning_room1", "title": "This is dinning room", "spelling": "" } ] }, { "key": "kitchen", "title": "Kitchen", "spelling": "/ˈkɪ.tʃən/", "subs": [ { "key": "kitchen1", "title": "This is kitchen", "spelling": "" } ] }, { "key": "yard", "title": "Yard", "spelling": "/jɑ:d/", "subs": [ { "key": "yard1", "title": "That is yard", "spelling": "" } ] }, { "key": "garden", "title": "Garden", "spelling": "/'gɑ:dn/", "subs": [ { "key": "garden1", "title": "This is garden", "spelling": "" } ] }, { "key": "garage", "title": "Garage", "spelling": "/'gærɑ:ʤ/", "subs": [ { "key": "garage1", "title": "This is garage", "spelling": "" } ] }, { "key": "roof", "title": "Roof", "spelling": "/ruːf/", "subs": [ { "key": "roof1", "title": "That is roof", "spelling": "" } ] }, { "key": "porch", "title": "Porch", "spelling": "/pɔːtʃ/", "subs": [ { "key": "porch1", "title": "That is porch", "spelling": "" } ] }, { "key": "window", "title": "Window", "spelling": "/ˈwɪn.dəʊ/", "subs": [ { "key": "window1", "title": "That is window", "spelling": "" } ] }, { "key": "door", "title": "Door", "spelling": "/dɔ:/", "subs": [ { "key": "door1", "title": "That is door", "spelling": "" } ] }, { "key": "shutter", "title": "shutter", "spelling": "/ˈʃʌt.əʳ/", "subs": [ { "key": "shutter1", "title": "This is shutter", "spelling": "" } ] }, { "key": "armchair", "title": "Armchair", "spelling": "/'ɑ:m'tʃeə/", "subs": [ { "key": "armchairs", "title": "These are armchairs", "spelling": "" } ] }, { "key": "bed", "title": "Bed", "spelling": "/bed/", "subs": [ { "key": "beds", "title": "These are beds", "spelling": "" } ] }, { "key": "bookshelf", "title": "Bookshelf", "spelling": "/ˈbʊkʃelf/", "subs": [ { "key": "bookshelf1", "title": "This is the bookshelf", "spelling": "" } ] }, { "key": "chair", "title": "Chair", "spelling": "/tʃeə/", "subs": [ { "key": "chairs", "title": "These are chairs", "spelling": "" } ] }, { "key": "clock", "title": "Clock", "spelling": "/klɔk/ (o'clock) /ə'klɔk/", "subs": [ { "key": "clock", "title": "This is a clock", "spelling": "" } ] }, { "key": "desk", "title": "Desk", "spelling": "/desk/", "subs": [ { "key": "3_desks", "title": "There ar 3 desks", "spelling": "" } ] }, { "key": "mirror", "title": "Mirror", "spelling": "/'mirə/", "subs": [ { "key": "mirror1", "title": " This is mirror", "spelling": "" } ] }, { "key": "sideboard", "title": "Sideboard", "spelling": "/'saidbɔ:d/", "subs": [ { "key": "sideboard1", "title": " This is the sideboard", "spelling": "" } ] }, { "key": "sofa", "title": "Sofa", "spelling": "/'soufə/", "subs": [ { "key": "sofa1", "title": " This is the sofa", "spelling": "" } ] }, { "key": "wardrobe", "title": "Wardrobe", "spelling": "/'wɔ:droub/", "subs": [ { "key": "wardrobe1", "title": " That is the wardrobe", "spelling": "" } ] }, { "key": "lamp", "title": "Lamp", "spelling": "/læmp/", "subs": [ { "key": "lamp1", "title": " That is the lamp", "spelling": "" } ] }, { "key": "radiator", "title": "Radiator", "spelling": "/'reidieitə/", "subs": [ { "key": "radiator1", "title": " That is a radiator", "spelling": "" } ] }, { "key": "telephone", "title": "Telephone", "spelling": "/'telifoun/", "subs": [ { "key": "telephone1", "title": " That is a telephone", "spelling": "" } ] }, { "key": "television", "title": "Television", "spelling": "/'teli,viʤn/", "subs": [ { "key": "television1", "title": " This is a television", "spelling": "" } ] }, { "key": "washing_machine", "title": "Washing machine", "spelling": "/'teli,viʤn/", "subs": [ { "key": "washing_machine1", "title": " This is a washing machine", "spelling": "" } ] }, { "key": "curtain", "title": "Curtain", "spelling": "/'kə:tn/", "subs": [ { "key": "curtain1", "title": " This is the curtain", "spelling": "" } ] }, { "key": "rug", "title": "Rug", "spelling": "/rug/", "subs": [ { "key": "rug1", "title": " This is the rug", "spelling": "" } ] }, { "key": "duvet", "title": "Duvet", "spelling": "/'dju:vei/", "subs": [ { "key": "duvet1", "title": " This is the duvet", "spelling": "" } ] }, { "key": "mattress", "title": "Mattress", "spelling": "/'mætris/", "subs": [ { "key": "mattress1", "title": " That is the mattress", "spelling": "" } ] }, { "key": "pillow", "title": "Pillow", "spelling": "/'pilou/", "subs": [ { "key": "pillow1", "title": " That is the pillow", "spelling": "" } ] }, { "key": "bath", "title": "Bath", "spelling": "/bɑ:θ/", "subs": [ { "key": "bath1", "title": " That is the bath", "spelling": "" } ] }, { "key": "bin", "title": "Bin", "spelling": "/bin/", "subs": [ { "key": "bin1", "title": " That is the bin", "spelling": "" } ] }, { "key": "broom", "title": "Broom", "spelling": "/bru:m/", "subs": [ { "key": "broom1", "title": " This is the broom", "spelling": "" } ] }, { "key": "bucket", "title": "Bucket", "spelling": "/'bʌkit/", "subs": [ { "key": "bucket1", "title": " This is a bucket", "spelling": "" } ] } ]
e14da7bc79b9870d0142023329ed50c8d2598bb7
7c92a46ce39266c13607ecdef7f228688f237182
/src/for_mathlib/uniform_space/uniform_field.lean
5f1c935288a8886397c37b2ccda5c6e32e9c6527
[ "Apache-2.0" ]
permissive
asym57/lean-perfectoid-spaces
3217d01f6ddc0d13e9fb68651749469750420767
359187b429f254a946218af4411d45f08705c83e
refs/heads/master
1,609,457,937,251
1,577,542,616,000
1,577,542,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,011
lean
/- The goal of this file is to prove the main part of Proposition 7 of Bourbaki GT III 6.8 : The completion hat K of a Hausdorff topological field is a field if the image under the mapping x ↦ x⁻¹ of every Cauchy filter (with respect to the additive uniform structure) which does not have a cluster point at 0 is a Cauchy filter (with respect to the additive uniform structure). Bourbaki does *not* prove this proposition, he refers to the general discussion of extending function defined on a dense subset with values in a complete Hausdorff space. In particular the subtlety about clustering at zero is totally left to readers. All this is very general. The application we have in mind is extension of valuations. In this application K will be equipped with a valuation and the topology on K will be the nonarchimedean topology coming from v. Note that the separated completion of a non-separated topological field is the zero ring, hence the separation assumption is needed. Indeed the kernel of the completion map is the closure of zero which is an ideal. Hence it's either zero (and the field is separated) or the full field, which implies one is sent to zero and the completion ring is trivial. -/ import topology.algebra.uniform_ring import for_mathlib.topological_field import for_mathlib.data.set.basic import for_mathlib.filter noncomputable theory local attribute [instance, priority 0] classical.prop_decidable open filter set_option class.instance_max_depth 100 open set uniform_space uniform_space.completion filter local attribute [instance] topological_add_group.to_uniform_space topological_add_group_is_uniform local notation `𝓝` x:70 := nhds x local notation `𝓤` := uniformity variables {K : Type*} [discrete_field K] [topological_space K] [topological_ring K] variables (K) local notation `hat` := completion def help_tc_search : uniform_space (hat K) := completion.uniform_space K local attribute [instance] help_tc_search def help_tc_search' : separated (hat K) := completion.separated K local attribute [instance] help_tc_search' def help_tc_search'' : complete_space (hat K) := completion.complete_space K local attribute [instance] help_tc_search'' def help_tc_search''' : t1_space (hat K) := t2_space.t1_space local attribute [instance] help_tc_search''' instance [separated K] : nonzero_comm_ring (hat K) := { zero_ne_one := assume h, zero_ne_one $ (uniform_embedding_coe K).inj h, ..completion.comm_ring K } class completable_top_field : Prop := (separated : separated K) (nice : ∀ F : filter K, cauchy F → 𝓝 0 ⊓ F = ⊥ → cauchy (map (λ x, x⁻¹) F)) local attribute [instance] completable_top_field.separated variables [completable_top_field K] [topological_division_ring K] {K} /-- extension of inversion to `hat K` -/ def hat_inv : hat K → hat K := dense_inducing_coe.extend (λ x : K, (coe x⁻¹ : hat K)) lemma continuous_hat_inv {x : hat K} (h : x ≠ 0) : continuous_at hat_inv x := begin haveI : regular_space (hat K) := completion.regular_space K, refine dense_inducing_coe.tendsto_extend _, apply mem_sets_of_superset (compl_singleton_mem_nhds h), intros y y_ne, rw mem_compl_singleton_iff at y_ne, dsimp, apply complete_space.complete, change cauchy (map (coe ∘ (λ (x : K), x⁻¹)) (comap coe (𝓝 y))), rw ← filter.map_map, apply cauchy_map (completion.uniform_continuous_coe K), apply completable_top_field.nice, { apply cauchy_comap, { rw completion.comap_coe_eq_uniformity, exact le_refl _ }, { exact cauchy_nhds }, { exact dense_inducing_coe.comap_nhds_neq_bot } }, { have eq_bot : 𝓝 ↑(0 : K) ⊓ 𝓝 y = ⊥, { by_contradiction h, exact y_ne (eq_of_nhds_neq_bot h).symm }, rw [dense_inducing_coe.nhds_eq_comap (0 : K), ← comap_inf, eq_bot], exact comap_bot }, end lemma hat_inv_extends {x : K} (h : x ≠ 0) : hat_inv (x : hat K) = coe (x⁻¹ : K) := dense_inducing_coe.extend_e_eq _ ((continuous_coe K).continuous_at.comp (topological_division_ring.continuous_inv x h)) lemma hat_inv_is_inv {x : hat K} (x_ne : x ≠ 0) : x*hat_inv x = 1 := begin haveI : t1_space (hat K) := t2_space.t1_space, let f := λ x : hat K, x*hat_inv x, let c := (coe : K → hat K), change f x = 1, have cont : continuous_at f x, { letI : topological_space (hat K × hat K) := prod.topological_space, have : continuous_at (λ y : hat K, ((y, hat_inv y) : hat K × hat K)) x, from continuous_at.prod_mk continuous_id.continuous_at (continuous_hat_inv x_ne), exact (_root_.continuous_mul.continuous_at.comp this : _) }, have clo : x ∈ closure (c '' -{0}), { have := dense_inducing_coe.dense x, rw [← image_univ, show (univ : set K) = {0} ∪ -{0}, from (union_compl_self _).symm, image_union] at this, apply mem_closure_union this, rw image_singleton, exact compl_singleton_mem_nhds x_ne }, have fxclo : f x ∈ closure (f '' (c '' -{0})) := mem_closure_image cont clo, have : f '' (c '' -{0}) ⊆ {1}, { rw image_image, rintros _ ⟨z, z_ne, rfl⟩, rw mem_singleton_iff, rw mem_compl_singleton_iff at z_ne, dsimp [c, f], rw hat_inv_extends z_ne, norm_cast, rw mul_inv_cancel z_ne, norm_cast }, replace fxclo := closure_mono this fxclo, rwa [closure_singleton, mem_singleton_iff] at fxclo end /- The value of `hat_inv` at zero is not really specified, although it's probably zero. Here we explicitly enforce the `inv_zero` axiom. -/ instance completion.has_inv : has_inv (hat K) := ⟨λ x, if x = 0 then 0 else hat_inv x⟩ @[move_cast] lemma coe_inv (x : K) : ((x⁻¹ : K) : hat K)= (x : hat K)⁻¹ := begin by_cases h : x = 0, { rw [h, inv_zero], dsimp [has_inv.inv], norm_cast, simp [if_pos] }, { conv_rhs { dsimp [has_inv.inv] }, norm_cast, rw if_neg, { exact (hat_inv_extends h).symm }, { exact λ H, h (completion.dense_embedding_coe.inj H) } } end instance : discrete_field (hat K) := { zero_ne_one := assume h, discrete_field.zero_ne_one K ((uniform_embedding_coe K).inj h), mul_inv_cancel := λ x x_ne, by { dsimp [has_inv.inv], simp [if_neg x_ne, hat_inv_is_inv x_ne], }, inv_mul_cancel := λ x x_ne, by { dsimp [has_inv.inv], rw [mul_comm, if_neg x_ne, hat_inv_is_inv x_ne] }, inv_zero := show (0 : hat K)⁻¹ = (0 : hat K), by rw_mod_cast inv_zero, has_decidable_eq := by apply_instance, ..completion.has_inv, ..(by apply_instance : comm_ring (hat K)) } instance : topological_division_ring (hat K) := { continuous_inv := begin intros x x_ne, have : {y | y⁻¹ = hat_inv y } ∈ 𝓝 x, { have : -{(0 : hat K)} ⊆ {y : hat K | y⁻¹ = hat_inv y }, { intros y y_ne, rw mem_compl_singleton_iff at y_ne, dsimp [has_inv.inv], rw if_neg y_ne }, exact mem_sets_of_superset (compl_singleton_mem_nhds x_ne) this }, rw continuous_at.congr this, exact continuous_hat_inv x_ne end, ..completion.top_ring_compl }
2895e6289f63f9ce4c9ebf18402322eefc4327ce
4d3f29a7b2eff44af8fd0d3176232e039acb9ee3
/User/hello_world.lean
8462440a2febea2841779cf5887bcbd55f4a07c9
[]
no_license
marijnheule/lamr
5fc5d69d326ff92e321242cfd7f72e78d7f99d7e
28cc4114c7361059bb54f407fa312bf38b48728b
refs/heads/main
1,689,338,013,620
1,630,359,632,000
1,630,359,632,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
44
lean
import Init #eval "hello world" #eval 2 + 2
96950b3347ae79e31ca2de2c2a676a915d548a36
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/finset/fold_auto.lean
c5bc3ec243cc62a63ddf01d62aea38a6c6fe7db1
[]
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
7,115
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.finset.basic import Mathlib.data.multiset.fold import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # The fold operation for a commutative associative operation over a finset. -/ namespace finset /-! ### fold -/ /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold {α : Type u_1} {β : Type u_2} (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] (b : β) (f : α → β) (s : finset α) : β := multiset.fold op b (multiset.map f (val s)) @[simp] theorem fold_empty {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} : fold op b f ∅ = b := rfl @[simp] theorem fold_insert {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {a : α} [DecidableEq α] (h : ¬a ∈ s) : fold op b f (insert a s) = op (f a) (fold op b f s) := sorry @[simp] theorem fold_singleton {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {a : α} : fold op b f (singleton a) = op (f a) b := rfl @[simp] theorem fold_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {g : γ ↪ α} {s : finset γ} : fold op b f (map g s) = fold op b (f ∘ ⇑g) s := sorry @[simp] theorem fold_image {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} [DecidableEq α] {g : γ → α} {s : finset γ} (H : ∀ (x : γ), x ∈ s → ∀ (y : γ), y ∈ s → g x = g y → x = y) : fold op b f (image g s) = fold op b (f ∘ g) s := sorry theorem fold_congr {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {g : α → β} (H : ∀ (x : α), x ∈ s → f x = g x) : fold op b f s = fold op b g s := sorry theorem fold_op_distrib {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {s : finset α} {f : α → β} {g : α → β} {b₁ : β} {b₂ : β} : fold op (op b₁ b₂) (fun (x : α) => op (f x) (g x)) s = op (fold op b₁ f s) (fold op b₂ g s) := sorry theorem fold_hom {α : Type u_1} {β : Type u_2} {γ : Type u_3} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀ (x y : β), m (op x y) = op' (m x) (m y)) : fold op' (m b) (fun (x : α) => m (f x)) s = m (fold op b f s) := sorry theorem fold_union_inter {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} [DecidableEq α] {s₁ : finset α} {s₂ : finset α} {b₁ : β} {b₂ : β} : op (fold op b₁ f (s₁ ∪ s₂)) (fold op b₂ f (s₁ ∩ s₂)) = op (fold op b₂ f s₁) (fold op b₁ f s₂) := sorry @[simp] theorem fold_insert_idem {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {a : α} [DecidableEq α] [hi : is_idempotent β op] : fold op b f (insert a s) = op (f a) (fold op b f s) := sorry theorem fold_op_rel_iff_and {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {r : β → β → Prop} (hr : ∀ {x y z : β}, r x (op y z) ↔ r x y ∧ r x z) {c : β} : r c (fold op b f s) ↔ r c b ∧ ∀ (x : α), x ∈ s → r c (f x) := sorry theorem fold_op_rel_iff_or {α : Type u_1} {β : Type u_2} {op : β → β → β} [hc : is_commutative β op] [ha : is_associative β op] {f : α → β} {b : β} {s : finset α} {r : β → β → Prop} (hr : ∀ {x y z : β}, r x (op y z) ↔ r x y ∨ r x z) {c : β} : r c (fold op b f s) ↔ r c b ∨ ∃ (x : α), ∃ (H : x ∈ s), r c (f x) := sorry @[simp] theorem fold_union_empty_singleton {α : Type u_1} [DecidableEq α] (s : finset α) : fold has_union.union ∅ singleton s = s := sorry @[simp] theorem fold_sup_bot_singleton {α : Type u_1} [DecidableEq α] (s : finset α) : fold has_sup.sup ⊥ singleton s = s := fold_union_empty_singleton s theorem le_fold_min {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : c ≤ fold min b f s ↔ c ≤ b ∧ ∀ (x : α), x ∈ s → c ≤ f x := fold_op_rel_iff_and fun (x y z : β) => le_min_iff theorem fold_min_le {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : fold min b f s ≤ c ↔ b ≤ c ∨ ∃ (x : α), ∃ (H : x ∈ s), f x ≤ c := id (fold_op_rel_iff_or fun (x y z : β) => id min_le_iff) theorem lt_fold_min {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : c < fold min b f s ↔ c < b ∧ ∀ (x : α), x ∈ s → c < f x := fold_op_rel_iff_and fun (x y z : β) => lt_min_iff theorem fold_min_lt {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : fold min b f s < c ↔ b < c ∨ ∃ (x : α), ∃ (H : x ∈ s), f x < c := id (fold_op_rel_iff_or fun (x y z : β) => id min_lt_iff) theorem fold_max_le {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : fold max b f s ≤ c ↔ b ≤ c ∧ ∀ (x : α), x ∈ s → f x ≤ c := id (fold_op_rel_iff_and fun (x y z : β) => id max_le_iff) theorem le_fold_max {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : c ≤ fold max b f s ↔ c ≤ b ∨ ∃ (x : α), ∃ (H : x ∈ s), c ≤ f x := fold_op_rel_iff_or fun (x y z : β) => le_max_iff theorem fold_max_lt {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : fold max b f s < c ↔ b < c ∧ ∀ (x : α), x ∈ s → f x < c := id (fold_op_rel_iff_and fun (x y z : β) => id max_lt_iff) theorem lt_fold_max {α : Type u_1} {β : Type u_2} {f : α → β} {b : β} {s : finset α} [linear_order β] (c : β) : c < fold max b f s ↔ c < b ∨ ∃ (x : α), ∃ (H : x ∈ s), c < f x := fold_op_rel_iff_or fun (x y z : β) => lt_max_iff end Mathlib
04cbbb3b22af605506e7702d656d537960de628e
4ec0e92c725fad3fd2871a0ab050a7da1c719444
/src/mywork/hw4.lean
3ad2a38f1dbb4b497bfcb9f9c8c3b8e5e55ceb37
[]
no_license
mitchelltaylor06/cs2120f21
cc2c2b61a7e45c07faa849fcb8a66eb948753a25
efb71a748d7c76e24834d03b8f01c3ae084c1756
refs/heads/main
1,693,841,444,092
1,633,713,850,000
1,633,713,850,000
399,946,415
0
0
null
null
null
null
UTF-8
Lean
false
false
6,461
lean
-- Group of Denny Garcia lfg3z@virginia.edu -- Mitchell Taylor mbt3vgd@virginia.edu -- Decker Bristow jdb3qn@virginia.edu -- Aleksander Schultz aps4yj@virginia.edu -- 1 example : 0 ≠ 1 := begin -- ¬ (0 = 1) -- (0 = 1) → false assume h, cases h, end -- 2 example : 0 ≠ 0 → 2 = 3 := begin assume h, have f : false := h (eq.refl 0), exact false.elim (f), end -- 3 example : ∀ (P : Prop), P → ¬¬P := begin assume P, assume (p : P), -- ¬¬P -- ¬P → false -- (P → false) → false assume h, have f := h p, exact f, end -- We might need classical (vs constructive) reasoning #check classical.em open classical #check em /- axiom em : ∀ (p : Prop), p ∨ ¬p This is the famous and historically controversial "law" (now axiom) of the excluded middle. It's is a key to proving many intuitive theorems in logic and mathematics. But it also leads to giving up on having evidence *why* something is either true or not true, in that you no longer need a proof of either P or of ¬P to have a proof of P ∨ ¬P. -/ -- 4 theorem neg_elim : ∀ (P : Prop), ¬¬P → P := begin assume P, assume h, have pornp := classical.em P, cases pornp with p pn, assumption, contradiction, end -- 5 theorem demorgan_1 : ∀ (P Q : Prop), ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q := begin assume P Q, apply iff.intro, assume h, have pornp := classical.em P, cases pornp with p pn, apply or.intro_right, have qornq := classical.em Q, cases qornq, assume q, apply not.intro h, apply and.intro, exact p, exact q, exact qornq, apply or.inl, exact pn, assume h, have pornp := classical.em P, cases pornp, have qornq := classical.em Q, cases qornq, apply not.intro, assume p, cases h, contradiction, contradiction, apply not.intro, assume p, cases h, contradiction, apply not.intro qornq, have q : Q := and.elim_right p, exact q, apply not.intro, assume h, have p : P := and.elim_left h, contradiction, end -- 6 theorem demorgan_2 : ∀ (P Q : Prop), ¬ (P ∨ Q) → ¬P ∧ ¬Q := begin assume P, assume PQ, assume PPQ, apply and.intro, apply not.intro, assume p, apply not.intro PPQ, apply or.intro_left, exact p, assume PQ, apply not.intro PPQ, apply or.intro_right, exact PQ, end -- 7 theorem disappearing_opposite : ∀ (P Q : Prop), P ∨ ¬P ∧ Q ↔ P ∨ Q := begin assume P Q, apply iff.intro, assume p, apply or.elim p, assume x, apply or.intro_left, exact x, assume p, have q : Q := and.elim_right p, apply or.intro_right, exact q, assume y, apply or.elim y, assume h, apply or.intro_left, exact h, assume x, have pornp := classical.em P, cases pornp, apply or.inl, exact pornp, apply or.inr, apply and.intro, exact pornp, exact x, end -- 8 theorem distrib_and_or : ∀ (P Q R: Prop), (P ∨ Q) ∧ (P ∨ R) ↔ P ∨ (Q ∧ R) := begin assume P Q R, apply iff.intro, assume p, have porq : P ∨ Q := and.elim_left p, have porr : P ∨ R := and.elim_right p, apply or.elim porq, assume x, apply or.intro_left, exact x, assume y, apply or.elim porr, assume z, apply or.elim porr, assume x, apply or.intro_left, exact z, assume h, apply or.intro_left, exact z, assume g, apply or.intro_right, apply and.intro, exact y, exact g, assume p, apply or.elim p, assume p, apply and.intro, apply or.intro_left, exact p, apply or.intro_left, exact p, assume p, have q : Q := and.elim_left p, have r : R := and.elim_right p, apply and.intro, apply or.intro_right, exact q, apply or.intro_right, exact r, end -- remember or is right associative -- you need this to know what the lefts and rights are -- 9 theorem distrib_and_or_foil : ∀ (P Q R S : Prop), (P ∨ Q) ∧ (R ∨ S) ↔ (P ∧ R) ∨ (P ∧ S) ∨ (Q ∧ R) ∨ (Q ∧ S) := begin assume P Q R S, apply iff.intro, assume p, have porq : P ∨ Q := and.elim_left p, have rors : R ∨ S := and.elim_right p, apply or.elim porq, assume x, apply or.elim rors, assume y, apply or.intro_left, apply and.intro, exact x, exact y, assume z, apply or.intro_right, apply or.intro_left, apply and.intro, exact x, exact z, assume a, apply or.elim rors, assume b, apply or.intro_right, apply or.intro_right, apply or.intro_left, apply and.intro, exact a, exact b, assume c, apply or.intro_right, apply or.intro_right, apply or.intro_right, apply and.intro, exact a, exact c, assume d, apply or.elim d, assume e, have p : P := and.elim_left e, have r : R := and.elim_right e, apply and.intro, apply or.intro_left, exact p, apply or.intro_left, exact r, assume f, apply or.elim f, assume g, have p : P := and.elim_left g, have s : S := and.elim_right g, apply and.intro, apply or.intro_left, exact p, apply or.intro_right, exact s, assume i, apply or.elim i, assume j, have q : Q := and.elim_left j, have r : R := and.elim_right j, apply and.intro, apply or.intro_right, exact q, apply or.intro_left, exact r, assume k, have q : Q := and.elim_left k, have s : S := and.elim_right k, apply and.intro, apply or.intro_right, exact q, apply or.intro_right, exact s, end /- 10 Formally state and prove the proposition that not every natural number is equal to zero. -/ lemma not_all_nats_are_zero : exists (n : ℕ), n ≠ 0 := begin apply exists.intro _ _, exact 6, contradiction, end -- 11. equivalence of P→Q and (¬P∨Q) example : ∀ (P Q : Prop), (P → Q) ↔ (¬P ∨ Q) := begin assume P Q, apply iff.intro, assume p, have pnp := classical.em Q, have qnq := classical.em P, cases pnp, apply or.intro_right, assumption, cases qnq, apply or.inr, have q : Q := p qnq, exact q, apply or.inl, exact qnq, assume x, assume p, cases x, contradiction, exact x, end -- 12 example : ∀ (P Q : Prop), (P → Q) → (¬ Q → ¬ P) := begin assume P Q, assume p, assume x, apply not.intro, assume h, apply not.intro x, have q : Q := p h, exact q, end -- 13 example : ∀ (P Q : Prop), ( ¬P → ¬Q) → (Q → P) := begin assume P Q, assume p, assume h, have pnp := classical.em P, have qnq := classical.em Q, cases pnp, exact pnp, have notq : ¬Q := p pnp, contradiction, end
36f111f50dc049f2cc07b713cb7de5671ee72564
63abd62053d479eae5abf4951554e1064a4c45b4
/src/topology/category/UniformSpace.lean
d9be3cd3ea59a5bff1f21480b671eb26dce22343
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
6,960
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Patrick Massot, Scott Morrison -/ import category_theory.monad.limits import topology.uniform_space.completion import topology.category.Top.basic /-! # The category of uniform spaces We construct the category of uniform spaces, show that the complete separated uniform spaces form a reflective subcategory, and hence possess all limits that uniform spaces do. TODO: show that uniform spaces actually have all limits! -/ universes u open category_theory /-- A (bundled) uniform space. -/ def UniformSpace : Type (u+1) := bundled uniform_space namespace UniformSpace /-- The information required to build morphisms for `UniformSpace`. -/ instance : unbundled_hom @uniform_continuous := ⟨@uniform_continuous_id, @uniform_continuous.comp⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] UniformSpace instance (x : UniformSpace) : uniform_space x := x.str /-- Construct a bundled `UniformSpace` from the underlying type and the typeclass. -/ def of (α : Type u) [uniform_space α] : UniformSpace := ⟨α⟩ instance : inhabited UniformSpace := ⟨UniformSpace.of empty⟩ @[simp] lemma coe_of (X : Type u) [uniform_space X] : (of X : Type u) = X := rfl instance (X Y : UniformSpace) : has_coe_to_fun (X ⟶ Y) := { F := λ _, X → Y, coe := category_theory.functor.map (forget UniformSpace) } @[simp] lemma coe_comp {X Y Z : UniformSpace} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g : X → Z) = g ∘ f := rfl @[simp] lemma coe_id (X : UniformSpace) : (𝟙 X : X → X) = id := rfl @[simp] lemma coe_mk {X Y : UniformSpace} (f : X → Y) (hf : uniform_continuous f) : ((⟨f, hf⟩ : X ⟶ Y) : X → Y) = f := rfl lemma hom_ext {X Y : UniformSpace} {f g : X ⟶ Y} : (f : X → Y) = g → f = g := subtype.eq /-- The forgetful functor from uniform spaces to topological spaces. -/ instance has_forget_to_Top : has_forget₂ UniformSpace.{u} Top.{u} := { forget₂ := { obj := λ X, Top.of X, map := λ X Y f, { to_fun := f, continuous_to_fun := uniform_continuous.continuous f.property }, }, } end UniformSpace /-- A (bundled) complete separated uniform space. -/ structure CpltSepUniformSpace := (α : Type u) [is_uniform_space : uniform_space α] [is_complete_space : complete_space α] [is_separated : separated_space α] namespace CpltSepUniformSpace instance : has_coe_to_sort CpltSepUniformSpace := { S := Type u, coe := CpltSepUniformSpace.α } attribute [instance] is_uniform_space is_complete_space is_separated def to_UniformSpace (X : CpltSepUniformSpace) : UniformSpace := UniformSpace.of X instance complete_space (X : CpltSepUniformSpace) : complete_space ((to_UniformSpace X).α) := CpltSepUniformSpace.is_complete_space X instance separated_space (X : CpltSepUniformSpace) : separated_space ((to_UniformSpace X).α) := CpltSepUniformSpace.is_separated X /-- Construct a bundled `UniformSpace` from the underlying type and the appropriate typeclasses. -/ def of (X : Type u) [uniform_space X] [complete_space X] [separated_space X] : CpltSepUniformSpace := ⟨X⟩ @[simp] lemma coe_of (X : Type u) [uniform_space X] [complete_space X] [separated_space X] : (of X : Type u) = X := rfl instance : inhabited CpltSepUniformSpace := begin haveI : separated_space empty := separated_iff_t2.mpr (by apply_instance), exact ⟨CpltSepUniformSpace.of empty⟩ end /-- The category instance on `CpltSepUniformSpace`. -/ instance category : large_category CpltSepUniformSpace := induced_category.category to_UniformSpace /-- The concrete category instance on `CpltSepUniformSpace`. -/ instance concrete_category : concrete_category CpltSepUniformSpace := induced_category.concrete_category to_UniformSpace instance has_forget_to_UniformSpace : has_forget₂ CpltSepUniformSpace UniformSpace := induced_category.has_forget₂ to_UniformSpace end CpltSepUniformSpace namespace UniformSpace open uniform_space open CpltSepUniformSpace /-- The functor turning uniform spaces into complete separated uniform spaces. -/ noncomputable def completion_functor : UniformSpace ⥤ CpltSepUniformSpace := { obj := λ X, CpltSepUniformSpace.of (completion X), map := λ X Y f, ⟨completion.map f.1, completion.uniform_continuous_map⟩, map_id' := λ X, subtype.eq completion.map_id, map_comp' := λ X Y Z f g, subtype.eq (completion.map_comp g.property f.property).symm, }. /-- The inclusion of a uniform space into its completion. -/ def completion_hom (X : UniformSpace) : X ⟶ (forget₂ CpltSepUniformSpace UniformSpace).obj (completion_functor.obj X) := { val := (coe : X → completion X), property := completion.uniform_continuous_coe X } @[simp] lemma completion_hom_val (X : UniformSpace) (x) : (completion_hom X) x = (x : completion X) := rfl /-- The mate of a morphism from a `UniformSpace` to a `CpltSepUniformSpace`. -/ noncomputable def extension_hom {X : UniformSpace} {Y : CpltSepUniformSpace} (f : X ⟶ (forget₂ CpltSepUniformSpace UniformSpace).obj Y) : completion_functor.obj X ⟶ Y := { val := completion.extension f, property := completion.uniform_continuous_extension } @[simp] lemma extension_hom_val {X : UniformSpace} {Y : CpltSepUniformSpace} (f : X ⟶ (forget₂ _ _).obj Y) (x) : (extension_hom f) x = completion.extension f x := rfl. @[simp] lemma extension_comp_coe {X : UniformSpace} {Y : CpltSepUniformSpace} (f : to_UniformSpace (CpltSepUniformSpace.of (completion X)) ⟶ to_UniformSpace Y) : extension_hom (completion_hom X ≫ f) = f := by { apply subtype.eq, funext x, exact congr_fun (completion.extension_comp_coe f.property) x } /-- The completion functor is left adjoint to the forgetful functor. -/ noncomputable def adj : completion_functor ⊣ forget₂ CpltSepUniformSpace UniformSpace := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, completion_hom X ≫ f, inv_fun := λ f, extension_hom f, left_inv := λ f, by { dsimp, erw extension_comp_coe }, right_inv := λ f, begin apply subtype.eq, funext x, cases f, exact @completion.extension_coe _ _ _ _ _ (CpltSepUniformSpace.separated_space _) f_property _ end }, hom_equiv_naturality_left_symm' := λ X X' Y f g, begin apply hom_ext, funext x, dsimp, erw [coe_comp, ←completion.extension_map], refl, exact g.property, exact f.property, end } noncomputable instance : is_right_adjoint (forget₂ CpltSepUniformSpace UniformSpace) := ⟨completion_functor, adj⟩ noncomputable instance : reflective (forget₂ CpltSepUniformSpace UniformSpace) := {} open category_theory.limits -- TODO Once someone defines `has_limits UniformSpace`, turn this into an instance. example [has_limits.{u} UniformSpace.{u}] : has_limits.{u} CpltSepUniformSpace.{u} := has_limits_of_reflective $ forget₂ CpltSepUniformSpace UniformSpace end UniformSpace
33fa84a224a6f6e98470988d5318f075a41f9357
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter4/4.12.lean
e1ed18b2c0e47bae6bff013b7710b2bd34fc1153
[]
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
330
lean
-- Simple proof of A /\ A, with a variable and hypothesis that A holds. variables A B C: Prop variable h1: B variable h2: B -> C -- At this point, we're "applying" h2 to h1. Note that lean is -- left-associative. Also, to me it is more intuitive to "apply" h1 to h2 here, -- however lean does not seem to like that. #check h2 h1
cf6eff7fd29a951070941bee284fd396142bceb1
7cef822f3b952965621309e88eadf618da0c8ae9
/src/ring_theory/algebra_operations.lean
c3198a233b7e72c4391683b777c24e324cd163e8
[ "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
7,871
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Multiplication of submodules of an algebra. -/ import ring_theory.algebra algebra.pointwise import tactic.chain import tactic.monotonicity.basic universes u v open lattice algebra local attribute [instance] set.pointwise_mul_semiring namespace submodule variables {R : Type u} [comm_ring R] section ring variables {A : Type v} [ring A] [algebra R A] variables (S T : set A) {M N P Q : submodule R A} {m n : A} instance : has_one (submodule R A) := ⟨submodule.map (of_id R A).to_linear_map (⊤ : ideal R)⟩ theorem one_eq_map_top : (1 : submodule R A) = submodule.map (of_id R A).to_linear_map (⊤ : ideal R) := rfl theorem one_eq_span : (1 : submodule R A) = span R {1} := begin apply submodule.ext, intro a, erw [mem_map, mem_span_singleton], apply exists_congr, intro r, simpa [smul_def], end theorem one_le : (1 : submodule R A) ≤ P ↔ (1 : A) ∈ P := by simpa only [one_eq_span, span_le, set.singleton_subset_iff] set_option class.instance_max_depth 50 instance : has_mul (submodule R A) := ⟨λ M N, ⨆ s : M, N.map $ algebra.lmul R A s.1⟩ set_option class.instance_max_depth 32 theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := (le_supr _ ⟨m, hm⟩ : _ ≤ M * N) ⟨n, hn, rfl⟩ theorem mul_le : M * N ≤ P ↔ ∀ (m ∈ M) (n ∈ N), m * n ∈ P := ⟨λ H m hm n hn, H $ mul_mem_mul hm hn, λ H, supr_le $ λ ⟨m, hm⟩, map_le_iff_le_comap.2 $ λ n hn, H m hm n hn⟩ @[elab_as_eliminator] protected theorem mul_induction_on {C : A → Prop} {r : A} (hr : r ∈ M * N) (hm : ∀ (m ∈ M) (n ∈ N), C (m * n)) (h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y)) (hs : ∀ (r : R) x, C x → C (r • x)) : C r := (@mul_le _ _ _ _ _ _ _ ⟨C, h0, ha, hs⟩).2 hm hr variables R theorem span_mul_span : span R S * span R T = span R (S * T) := begin apply le_antisymm, { rw mul_le, intros a ha b hb, apply span_induction ha, work_on_goal 0 { intros, apply span_induction hb, work_on_goal 0 { intros, exact subset_span ⟨_, ‹_›, _, ‹_›, rfl⟩ } }, all_goals { intros, simp only [mul_zero, zero_mul, zero_mem, left_distrib, right_distrib, mul_smul_comm, smul_mul_assoc], try {apply add_mem _ _ _}, try {apply smul_mem _ _ _} }, assumption' }, { rw span_le, rintros _ ⟨a, ha, b, hb, rfl⟩, exact mul_mem_mul (subset_span ha) (subset_span hb) } end variables {R} variables (M N P Q) set_option class.instance_max_depth 50 protected theorem mul_assoc : (M * N) * P = M * (N * P) := le_antisymm (mul_le.2 $ λ mn hmn p hp, suffices M * N ≤ (M * (N * P)).comap ((algebra.lmul R A).flip p), from this hmn, mul_le.2 $ λ m hm n hn, show m * n * p ∈ M * (N * P), from (mul_assoc m n p).symm ▸ mul_mem_mul hm (mul_mem_mul hn hp)) (mul_le.2 $ λ m hm np hnp, suffices N * P ≤ (M * N * P).comap (algebra.lmul R A m), from this hnp, mul_le.2 $ λ n hn p hp, show m * (n * p) ∈ M * N * P, from mul_assoc m n p ▸ mul_mem_mul (mul_mem_mul hm hn) hp) set_option class.instance_max_depth 32 @[simp] theorem mul_bot : M * ⊥ = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hn ⊢; rw [hn, mul_zero] @[simp] theorem bot_mul : ⊥ * M = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hm ⊢; rw [hm, zero_mul] @[simp] protected theorem one_mul : (1 : submodule R A) * M = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, one_mul, span_eq] } @[simp] protected theorem mul_one : M * 1 = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, mul_one, span_eq] } variables {M N P Q} @[mono] theorem mul_le_mul (hmp : M ≤ P) (hnq : N ≤ Q) : M * N ≤ P * Q := mul_le.2 $ λ m hm n hn, mul_mem_mul (hmp hm) (hnq hn) theorem mul_le_mul_left (h : M ≤ N) : M * P ≤ N * P := mul_le_mul h (le_refl P) theorem mul_le_mul_right (h : N ≤ P) : M * N ≤ M * P := mul_le_mul (le_refl M) h variables (M N P) theorem mul_sup : M * (N ⊔ P) = M * N ⊔ M * P := le_antisymm (mul_le.2 $ λ m hm np hnp, let ⟨n, hn, p, hp, hnp⟩ := mem_sup.1 hnp in mem_sup.2 ⟨_, mul_mem_mul hm hn, _, mul_mem_mul hm hp, hnp ▸ (mul_add m n p).symm⟩) (sup_le (mul_le_mul_right le_sup_left) (mul_le_mul_right le_sup_right)) theorem sup_mul : (M ⊔ N) * P = M * P ⊔ N * P := le_antisymm (mul_le.2 $ λ mn hmn p hp, let ⟨m, hm, n, hn, hmn⟩ := mem_sup.1 hmn in mem_sup.2 ⟨_, mul_mem_mul hm hp, _, mul_mem_mul hn hp, hmn ▸ (add_mul m n p).symm⟩) (sup_le (mul_le_mul_left le_sup_left) (mul_le_mul_left le_sup_right)) lemma mul_subset_mul : (↑M : set A) * (↑N : set A) ⊆ (↑(M * N) : set A) := begin rintros _ ⟨i, hi, j, hj, rfl⟩, exact mul_mem_mul hi hj end variables {M N P} instance : semiring (submodule R A) := { one_mul := submodule.one_mul, mul_one := submodule.mul_one, mul_assoc := submodule.mul_assoc, zero_mul := bot_mul, mul_zero := mul_bot, left_distrib := mul_sup, right_distrib := sup_mul, ..submodule.add_comm_monoid, ..submodule.has_one, ..submodule.has_mul } variables (M) lemma pow_subset_pow {n : ℕ} : (↑M : set A)^n ⊆ ↑(M^n : submodule R A) := begin induction n with n ih, { erw [pow_zero, pow_zero, set.singleton_subset_iff], rw [mem_coe, ← one_le], exact le_refl _ }, { rw [pow_succ, pow_succ], refine set.subset.trans (set.pointwise_mul_subset_mul (set.subset.refl _) ih) _, apply mul_subset_mul } end instance span.is_semiring_hom : is_semiring_hom (submodule.span R : set A → submodule R A) := { map_zero := span_empty, map_one := show _ = map _ ⊤, by erw [← ideal.span_singleton_one, ← span_image, set.image_singleton, alg_hom.map_one]; refl, map_add := span_union, map_mul := λ s t, by erw [span_mul_span, set.pointwise_mul_eq_image] } end ring section comm_ring variables {A : Type v} [comm_ring A] [algebra R A] variables {M N : submodule R A} {m n : A} theorem mul_mem_mul_rev (hm : m ∈ M) (hn : n ∈ N) : n * m ∈ M * N := mul_comm m n ▸ mul_mem_mul hm hn variables (M N) protected theorem mul_comm : M * N = N * M := le_antisymm (mul_le.2 $ λ r hrm s hsn, mul_mem_mul_rev hsn hrm) (mul_le.2 $ λ r hrn s hsm, mul_mem_mul_rev hsm hrn) instance : comm_semiring (submodule R A) := { mul_comm := submodule.mul_comm, .. submodule.semiring } variables (R A) instance semimodule_set : semimodule (set A) (submodule R A) := { smul := λ s P, span R s * P, smul_add := λ _ _ _, mul_add _ _ _, add_smul := λ s t P, show span R (s ⊔ t) * P = _, by { erw [span_union, right_distrib] }, mul_smul := λ s t P, show _ = _ * (_ * _), by { rw [← mul_assoc, span_mul_span, set.pointwise_mul_eq_image] }, one_smul := λ P, show span R {(1 : A)} * P = _, by { conv_lhs {erw ← span_eq P}, erw [span_mul_span, one_mul, span_eq] }, zero_smul := λ P, show span R ∅ * P = ⊥, by erw [span_empty, bot_mul], smul_zero := λ _, mul_bot _ } set_option class.instance_max_depth 45 variables {R A} lemma smul_def {s : set A} {P : submodule R A} : s • P = span R s * P := rfl lemma smul_le_smul {s t : set A} {M N : submodule R A} (h₁ : s ≤ t) (h₂ : M ≤ N) : s • M ≤ t • N := mul_le_mul (span_mono h₁) h₂ lemma smul_singleton (a : A) (M : submodule R A) : ({a} : set A) • M = M.map (lmul_left _ _ a) := begin conv_lhs {rw ← span_eq M}, change span _ _ * span _ _ = _, rw [span_mul_span], apply le_antisymm, { rw span_le, rintros _ ⟨b, hb, m, hm, rfl⟩, erw [mem_map, set.mem_singleton_iff.mp hb], exact ⟨m, hm, rfl⟩ }, { rintros _ ⟨m, hm, rfl⟩, exact subset_span ⟨a, set.mem_singleton a, m, hm, rfl⟩ } end end comm_ring end submodule
723adb227b33a457c0175d629b987cade8ca8634
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/finset/locally_finite.lean
2434c17409cd4191665a9e1839d9b6fe9e2aea9c
[ "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
23,841
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Yaël Dillies -/ import order.locally_finite /-! # Intervals as finsets This file provides basic results about all the `finset.Ixx`, which are defined in `order.locally_finite`. ## TODO This file was originally only about `finset.Ico a b` where `a b : ℕ`. No care has yet been taken to generalize these lemmas properly and many lemmas about `Icc`, `Ioc`, `Ioo` are missing. In general, what's to do is taking the lemmas in `data.x.intervals` and abstract away the concrete structure. Complete the API. See https://github.com/leanprover-community/mathlib/pull/14448#discussion_r906109235 for some ideas. -/ open_locale big_operators variables {ι α : Type*} namespace finset section preorder variables [preorder α] section locally_finite_order variables [locally_finite_order α] {a a₁ a₂ b b₁ b₂ c x : α} @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := by rw [←coe_nonempty, coe_Icc, set.nonempty_Icc] @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ico, set.nonempty_Ico] @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ioc, set.nonempty_Ioc] @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ioo, set.nonempty_Ioo] @[simp] lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ ¬a ≤ b := by rw [←coe_eq_empty, coe_Icc, set.Icc_eq_empty_iff] @[simp] lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ico, set.Ico_eq_empty_iff] @[simp] lemma Ioc_eq_empty_iff : Ioc a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ioc, set.Ioc_eq_empty_iff] @[simp] lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ioo, set.Ioo_eq_empty_iff] alias Icc_eq_empty_iff ↔ _ Icc_eq_empty alias Ico_eq_empty_iff ↔ _ Ico_eq_empty alias Ioc_eq_empty_iff ↔ _ Ioc_eq_empty @[simp] lemma Ioo_eq_empty (h : ¬a < b) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x hx, h ((mem_Ioo.1 hx).1.trans (mem_Ioo.1 hx).2) @[simp] lemma Icc_eq_empty_of_lt (h : b < a) : Icc a b = ∅ := Icc_eq_empty h.not_le @[simp] lemma Ico_eq_empty_of_le (h : b ≤ a) : Ico a b = ∅ := Ico_eq_empty h.not_lt @[simp] lemma Ioc_eq_empty_of_le (h : b ≤ a) : Ioc a b = ∅ := Ioc_eq_empty h.not_lt @[simp] lemma Ioo_eq_empty_of_le (h : b ≤ a) : Ioo a b = ∅ := Ioo_eq_empty h.not_lt @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp only [mem_Icc, true_and, le_rfl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp only [mem_Ico, true_and, le_refl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp only [mem_Icc, and_true, le_rfl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp only [mem_Ioc, and_true, le_rfl] @[simp] lemma left_not_mem_Ioc : a ∉ Ioc a b := λ h, lt_irrefl _ (mem_Ioc.1 h).1 @[simp] lemma left_not_mem_Ioo : a ∉ Ioo a b := λ h, lt_irrefl _ (mem_Ioo.1 h).1 @[simp] lemma right_not_mem_Ico : b ∉ Ico a b := λ h, lt_irrefl _ (mem_Ico.1 h).2 @[simp] lemma right_not_mem_Ioo : b ∉ Ioo a b := λ h, lt_irrefl _ (mem_Ioo.1 h).2 lemma Icc_subset_Icc (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := by simpa [←coe_subset] using set.Icc_subset_Icc ha hb lemma Ico_subset_Ico (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := by simpa [←coe_subset] using set.Ico_subset_Ico ha hb lemma Ioc_subset_Ioc (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := by simpa [←coe_subset] using set.Ioc_subset_Ioc ha hb lemma Ioo_subset_Ioo (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := by simpa [←coe_subset] using set.Ioo_subset_Ioo ha hb lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h le_rfl lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h le_rfl lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h le_rfl lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h le_rfl lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc le_rfl h lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico le_rfl h lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc le_rfl h lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo le_rfl h lemma Ico_subset_Ioo_left (h : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := by { rw [←coe_subset, coe_Ico, coe_Ioo], exact set.Ico_subset_Ioo_left h } lemma Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ := by { rw [←coe_subset, coe_Ioc, coe_Ioo], exact set.Ioc_subset_Ioo_right h } lemma Icc_subset_Ico_right (h : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := by { rw [←coe_subset, coe_Icc, coe_Ico], exact set.Icc_subset_Ico_right h } lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := by { rw [←coe_subset, coe_Ioo, coe_Ico], exact set.Ioo_subset_Ico_self } lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := by { rw [←coe_subset, coe_Ioo, coe_Ioc], exact set.Ioo_subset_Ioc_self } lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := by { rw [←coe_subset, coe_Ico, coe_Icc], exact set.Ico_subset_Icc_self } lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := by { rw [←coe_subset, coe_Ioc, coe_Icc], exact set.Ioc_subset_Icc_self } lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := Ioo_subset_Ico_self.trans Ico_subset_Icc_self lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := by rw [←coe_subset, coe_Icc, coe_Icc, set.Icc_subset_Icc_iff h₁] lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := by rw [←coe_subset, coe_Icc, coe_Ioo, set.Icc_subset_Ioo_iff h₁] lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := by rw [←coe_subset, coe_Icc, coe_Ico, set.Icc_subset_Ico_iff h₁] lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := (Icc_subset_Ico_iff h₁.dual).trans and.comm --TODO: `Ico_subset_Ioo_iff`, `Ioc_subset_Ioo_iff` lemma Icc_ssubset_Icc_left (hI : a₂ ≤ b₂) (ha : a₂ < a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := by { rw [←coe_ssubset, coe_Icc, coe_Icc], exact set.Icc_ssubset_Icc_left hI ha hb } lemma Icc_ssubset_Icc_right (hI : a₂ ≤ b₂) (ha : a₂ ≤ a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := by { rw [←coe_ssubset, coe_Icc, coe_Icc], exact set.Icc_ssubset_Icc_right hI ha hb } variables (a) @[simp] lemma Ico_self : Ico a a = ∅ := Ico_eq_empty $ lt_irrefl _ @[simp] lemma Ioc_self : Ioc a a = ∅ := Ioc_eq_empty $ lt_irrefl _ @[simp] lemma Ioo_self : Ioo a a = ∅ := Ioo_eq_empty $ lt_irrefl _ variables {a} /-- A set with upper and lower bounds in a locally finite order is a fintype -/ def _root_.set.fintype_of_mem_bounds {s : set α} [decidable_pred (∈ s)] (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : fintype s := set.fintype_subset (set.Icc a b) $ λ x hx, ⟨ha hx, hb hx⟩ lemma _root_.bdd_below.finite_of_bdd_above {s : set α} (h₀ : bdd_below s) (h₁ : bdd_above s) : s.finite := let ⟨a, ha⟩ := h₀, ⟨b, hb⟩ := h₁ in by { classical, exact ⟨set.fintype_of_mem_bounds ha hb⟩ } section filter lemma Ico_filter_lt_of_le_left [decidable_pred (< c)] (hca : c ≤ a) : (Ico a b).filter (< c) = ∅ := filter_false_of_mem (λ x hx, (hca.trans (mem_Ico.1 hx).1).not_lt) lemma Ico_filter_lt_of_right_le [decidable_pred (< c)] (hbc : b ≤ c) : (Ico a b).filter (< c) = Ico a b := filter_true_of_mem (λ x hx, (mem_Ico.1 hx).2.trans_le hbc) lemma Ico_filter_lt_of_le_right [decidable_pred (< c)] (hcb : c ≤ b) : (Ico a b).filter (< c) = Ico a c := begin ext x, rw [mem_filter, mem_Ico, mem_Ico, and.right_comm], exact and_iff_left_of_imp (λ h, h.2.trans_le hcb), end lemma Ico_filter_le_of_le_left {a b c : α} [decidable_pred ((≤) c)] (hca : c ≤ a) : (Ico a b).filter ((≤) c) = Ico a b := filter_true_of_mem (λ x hx, hca.trans (mem_Ico.1 hx).1) lemma Ico_filter_le_of_right_le {a b : α} [decidable_pred ((≤) b)] : (Ico a b).filter ((≤) b) = ∅ := filter_false_of_mem (λ x hx, (mem_Ico.1 hx).2.not_le) lemma Ico_filter_le_of_left_le {a b c : α} [decidable_pred ((≤) c)] (hac : a ≤ c) : (Ico a b).filter ((≤) c) = Ico c b := begin ext x, rw [mem_filter, mem_Ico, mem_Ico, and_comm, and.left_comm], exact and_iff_right_of_imp (λ h, hac.trans h.1), end variables (a b) [fintype α] lemma filter_lt_lt_eq_Ioo [decidable_pred (λ j, a < j ∧ j < b)] : univ.filter (λ j, a < j ∧ j < b) = Ioo a b := by { ext, simp } lemma filter_lt_le_eq_Ioc [decidable_pred (λ j, a < j ∧ j ≤ b)] : univ.filter (λ j, a < j ∧ j ≤ b) = Ioc a b := by { ext, simp } lemma filter_le_lt_eq_Ico [decidable_pred (λ j, a ≤ j ∧ j < b)] : univ.filter (λ j, a ≤ j ∧ j < b) = Ico a b := by { ext, simp } lemma filter_le_le_eq_Icc [decidable_pred (λ j, a ≤ j ∧ j ≤ b)] : univ.filter (λ j, a ≤ j ∧ j ≤ b) = Icc a b := by { ext, simp } end filter section locally_finite_order_top variables [locally_finite_order_top α] lemma Icc_subset_Ici_self : Icc a b ⊆ Ici a := by simpa [←coe_subset] using set.Icc_subset_Ici_self lemma Ico_subset_Ici_self : Ico a b ⊆ Ici a := by simpa [←coe_subset] using set.Ico_subset_Ici_self lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := by simpa [←coe_subset] using set.Ioc_subset_Ioi_self lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := by simpa [←coe_subset] using set.Ioo_subset_Ioi_self lemma Ioc_subset_Ici_self : Ioc a b ⊆ Ici a := Ioc_subset_Icc_self.trans Icc_subset_Ici_self lemma Ioo_subset_Ici_self : Ioo a b ⊆ Ici a := Ioo_subset_Ico_self.trans Ico_subset_Ici_self end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] lemma Icc_subset_Iic_self : Icc a b ⊆ Iic b := by simpa [←coe_subset] using set.Icc_subset_Iic_self lemma Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := by simpa [←coe_subset] using set.Ioc_subset_Iic_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := by simpa [←coe_subset] using set.Ico_subset_Iio_self lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := by simpa [←coe_subset] using set.Ioo_subset_Iio_self lemma Ico_subset_Iic_self : Ico a b ⊆ Iic b := Ico_subset_Icc_self.trans Icc_subset_Iic_self lemma Ioo_subset_Iic_self : Ioo a b ⊆ Iic b := Ioo_subset_Ioc_self.trans Ioc_subset_Iic_self end locally_finite_order_bot end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] {a : α} lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := by simpa [←coe_subset] using set.Ioi_subset_Ici_self lemma _root_.bdd_below.finite {s : set α} (hs : bdd_below s) : s.finite := let ⟨a, ha⟩ := hs in (Ici a).finite_to_set.subset $ λ x hx, mem_Ici.2 $ ha hx variables [fintype α] lemma filter_lt_eq_Ioi [decidable_pred ((<) a)] : univ.filter ((<) a) = Ioi a := by { ext, simp } lemma filter_le_eq_Ici [decidable_pred ((≤) a)] : univ.filter ((≤) a) = Ici a := by { ext, simp } end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] {a : α} lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := by simpa [←coe_subset] using set.Iio_subset_Iic_self lemma _root_.bdd_above.finite {s : set α} (hs : bdd_above s) : s.finite := hs.dual.finite variables [fintype α] lemma filter_gt_eq_Iio [decidable_pred (< a)] : univ.filter (< a) = Iio a := by { ext, simp } lemma filter_ge_eq_Iic [decidable_pred (≤ a)] : univ.filter (≤ a) = Iic a := by { ext, simp } end locally_finite_order_bot variables [decidable_eq α] [locally_finite_order_top α] [locally_finite_order_bot α] lemma disjoint_Ioi_Iio (a : α) : disjoint (Ioi a) (Iio a) := disjoint_left.2 $ λ b hab hba, (mem_Ioi.1 hab).not_lt $ mem_Iio.1 hba end preorder section partial_order variables [partial_order α] [locally_finite_order α] {a b c : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := by rw [←coe_eq_singleton, coe_Icc, set.Icc_self] @[simp] lemma Icc_eq_singleton_iff : Icc a b = {c} ↔ a = c ∧ b = c := by rw [←coe_eq_singleton, coe_Icc, set.Icc_eq_singleton_iff] section decidable_eq variables [decidable_eq α] @[simp] lemma Icc_erase_left (a b : α) : (Icc a b).erase a = Ioc a b := by simp [←coe_inj] @[simp] lemma Icc_erase_right (a b : α) : (Icc a b).erase b = Ico a b := by simp [←coe_inj] @[simp] lemma Ico_erase_left (a b : α) : (Ico a b).erase a = Ioo a b := by simp [←coe_inj] @[simp] lemma Ioc_erase_right (a b : α) : (Ioc a b).erase b = Ioo a b := by simp [←coe_inj] @[simp] lemma Icc_diff_both (a b : α) : Icc a b \ {a, b} = Ioo a b := by simp [←coe_inj] @[simp] lemma Ico_insert_right (h : a ≤ b) : insert b (Ico a b) = Icc a b := by rw [←coe_inj, coe_insert, coe_Icc, coe_Ico, set.insert_eq, set.union_comm, set.Ico_union_right h] @[simp] lemma Ioc_insert_left (h : a ≤ b) : insert a (Ioc a b) = Icc a b := by rw [←coe_inj, coe_insert, coe_Ioc, coe_Icc, set.insert_eq, set.union_comm, set.Ioc_union_left h] @[simp] lemma Ioo_insert_left (h : a < b) : insert a (Ioo a b) = Ico a b := by rw [←coe_inj, coe_insert, coe_Ioo, coe_Ico, set.insert_eq, set.union_comm, set.Ioo_union_left h] @[simp] lemma Ioo_insert_right (h : a < b) : insert b (Ioo a b) = Ioc a b := by rw [←coe_inj, coe_insert, coe_Ioo, coe_Ioc, set.insert_eq, set.union_comm, set.Ioo_union_right h] @[simp] lemma Icc_diff_Ico_self (h : a ≤ b) : Icc a b \ Ico a b = {b} := by simp [←coe_inj, h] @[simp] lemma Icc_diff_Ioc_self (h : a ≤ b) : Icc a b \ Ioc a b = {a} := by simp [←coe_inj, h] @[simp] lemma Icc_diff_Ioo_self (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} := by simp [←coe_inj, h] @[simp] lemma Ico_diff_Ioo_self (h : a < b) : Ico a b \ Ioo a b = {a} := by simp [←coe_inj, h] @[simp] lemma Ioc_diff_Ioo_self (h : a < b) : Ioc a b \ Ioo a b = {b} := by simp [←coe_inj, h] @[simp] lemma Ico_inter_Ico_consecutive (a b c : α) : Ico a b ∩ Ico b c = ∅ := begin refine eq_empty_of_forall_not_mem (λ x hx, _), rw [mem_inter, mem_Ico, mem_Ico] at hx, exact hx.1.2.not_le hx.2.1, end lemma Ico_disjoint_Ico_consecutive (a b c : α) : disjoint (Ico a b) (Ico b c) := le_of_eq $ Ico_inter_Ico_consecutive a b c end decidable_eq -- Those lemmas are purposefully the other way around lemma Icc_eq_cons_Ico (h : a ≤ b) : Icc a b = (Ico a b).cons b right_not_mem_Ico := by { classical, rw [cons_eq_insert, Ico_insert_right h] } lemma Icc_eq_cons_Ioc (h : a ≤ b) : Icc a b = (Ioc a b).cons a left_not_mem_Ioc := by { classical, rw [cons_eq_insert, Ioc_insert_left h] } lemma Ico_filter_le_left {a b : α} [decidable_pred (≤ a)] (hab : a < b) : (Ico a b).filter (λ x, x ≤ a) = {a} := begin ext x, rw [mem_filter, mem_Ico, mem_singleton, and.right_comm, ←le_antisymm_iff, eq_comm], exact and_iff_left_of_imp (λ h, h.le.trans_lt hab), end lemma card_Ico_eq_card_Icc_sub_one (a b : α) : (Ico a b).card = (Icc a b).card - 1 := begin classical, by_cases h : a ≤ b, { rw [←Ico_insert_right h, card_insert_of_not_mem right_not_mem_Ico], exact (nat.add_sub_cancel _ _).symm }, { rw [Ico_eq_empty (λ h', h h'.le), Icc_eq_empty h, card_empty, zero_tsub] } end lemma card_Ioc_eq_card_Icc_sub_one (a b : α) : (Ioc a b).card = (Icc a b).card - 1 := @card_Ico_eq_card_Icc_sub_one αᵒᵈ _ _ _ _ lemma card_Ioo_eq_card_Ico_sub_one (a b : α) : (Ioo a b).card = (Ico a b).card - 1 := begin classical, by_cases h : a ≤ b, { obtain rfl | h' := h.eq_or_lt, { rw [Ioo_self, Ico_self, card_empty] }, rw [←Ioo_insert_left h', card_insert_of_not_mem left_not_mem_Ioo], exact (nat.add_sub_cancel _ _).symm }, { rw [Ioo_eq_empty (λ h', h h'.le), Ico_eq_empty (λ h', h h'.le), card_empty, zero_tsub] } end lemma card_Ioo_eq_card_Ioc_sub_one (a b : α) : (Ioo a b).card = (Ioc a b).card - 1 := @card_Ioo_eq_card_Ico_sub_one αᵒᵈ _ _ _ _ lemma card_Ioo_eq_card_Icc_sub_two (a b : α) : (Ioo a b).card = (Icc a b).card - 2 := by { rw [card_Ioo_eq_card_Ico_sub_one, card_Ico_eq_card_Icc_sub_one], refl } section order_top variables [order_top α] @[simp] lemma Ici_erase [decidable_eq α] (a : α) : (Ici a).erase a = Ioi a := Icc_erase_left _ _ @[simp] lemma Ioi_insert [decidable_eq α] (a : α) : insert a (Ioi a) = Ici a := Ioc_insert_left le_top -- Purposefully written the other way around lemma Ici_eq_cons_Ioi (a : α) : Ici a = (Ioi a).cons a left_not_mem_Ioc := by { classical, rw [cons_eq_insert, Ioi_insert] } end order_top section order_bot variables [order_bot α] @[simp] lemma Iic_erase [decidable_eq α] (b : α) : (Iic b).erase b = Iio b := Icc_erase_right _ _ @[simp] lemma Iio_insert [decidable_eq α] (b : α) : insert b (Iio b) = Iic b := Ico_insert_right bot_le -- Purposefully written the other way around lemma Iic_eq_cons_Iio (b : α) : Iic b = (Iio b).cons b right_not_mem_Ico := by { classical, rw [cons_eq_insert, Iio_insert] } end order_bot end partial_order section linear_order variables [linear_order α] section locally_finite_order variables [locally_finite_order α] {a b : α} lemma Ico_subset_Ico_iff {a₁ b₁ a₂ b₂ : α} (h : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := by rw [←coe_subset, coe_Ico, coe_Ico, set.Ico_subset_Ico_iff h] lemma Ico_union_Ico_eq_Ico {a b c : α} (hab : a ≤ b) (hbc : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico_eq_Ico hab hbc] @[simp] lemma Ioc_union_Ioc_eq_Ioc {a b c : α} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := by rw [←coe_inj, coe_union, coe_Ioc, coe_Ioc, coe_Ioc, set.Ioc_union_Ioc_eq_Ioc h₁ h₂] lemma Ico_subset_Ico_union_Ico {a b c : α} : Ico a c ⊆ Ico a b ∪ Ico b c := by { rw [←coe_subset, coe_union, coe_Ico, coe_Ico, coe_Ico], exact set.Ico_subset_Ico_union_Ico } lemma Ico_union_Ico' {a b c d : α} (hcb : c ≤ b) (had : a ≤ d) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico' hcb had] lemma Ico_union_Ico {a b c d : α} (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico h₁ h₂] lemma Ico_inter_Ico {a b c d : α} : Ico a b ∩ Ico c d = Ico (max a c) (min b d) := by rw [←coe_inj, coe_inter, coe_Ico, coe_Ico, coe_Ico, ←inf_eq_min, ←sup_eq_max, set.Ico_inter_Ico] @[simp] lemma Ico_filter_lt (a b c : α) : (Ico a b).filter (λ x, x < c) = Ico a (min b c) := begin cases le_total b c, { rw [Ico_filter_lt_of_right_le h, min_eq_left h] }, { rw [Ico_filter_lt_of_le_right h, min_eq_right h] } end @[simp] lemma Ico_filter_le (a b c : α) : (Ico a b).filter (λ x, c ≤ x) = Ico (max a c) b := begin cases le_total a c, { rw [Ico_filter_le_of_left_le h, max_eq_right h] }, { rw [Ico_filter_le_of_le_left h, max_eq_left h] } end @[simp] lemma Ico_diff_Ico_left (a b c : α) : (Ico a b) \ (Ico a c) = Ico (max a c) b := begin cases le_total a c, { ext x, rw [mem_sdiff, mem_Ico, mem_Ico, mem_Ico, max_eq_right h, and.right_comm, not_and, not_lt], exact and_congr_left' ⟨λ hx, hx.2 hx.1, λ hx, ⟨h.trans hx, λ _, hx⟩⟩ }, { rw [Ico_eq_empty_of_le h, sdiff_empty, max_eq_left h] } end @[simp] lemma Ico_diff_Ico_right (a b c : α) : (Ico a b) \ (Ico c b) = Ico a (min b c) := begin cases le_total b c, { rw [Ico_eq_empty_of_le h, sdiff_empty, min_eq_left h] }, { ext x, rw [mem_sdiff, mem_Ico, mem_Ico, mem_Ico, min_eq_right h, and_assoc, not_and', not_le], exact and_congr_right' ⟨λ hx, hx.2 hx.1, λ hx, ⟨hx.trans_le h, λ _, hx⟩⟩ } end end locally_finite_order variables [fintype α] [locally_finite_order_top α] [locally_finite_order_bot α] lemma Ioi_disj_union_Iio (a : α) : (Ioi a).disj_union (Iio a) (disjoint_left.1 $ disjoint_Ioi_Iio a) = ({a} : finset α)ᶜ := by { ext, simp [eq_comm] } end linear_order section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [decidable_eq α] [locally_finite_order α] lemma image_add_left_Icc (a b c : α) : (Icc a b).image ((+) c) = Icc (c + a) (c + b) := begin ext x, rw [mem_image, mem_Icc], split, { rintro ⟨y, hy, rfl⟩, rw mem_Icc at hy, exact ⟨add_le_add_left hy.1 c, add_le_add_left hy.2 c⟩ }, { intro hx, obtain ⟨y, hy⟩ := exists_add_of_le hx.1, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Icc.2 ⟨le_of_add_le_add_left hx.1, le_of_add_le_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ico (a b c : α) : (Ico a b).image ((+) c) = Ico (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ico], split, { rintro ⟨y, hy, rfl⟩, rw mem_Ico at hy, exact ⟨add_le_add_left hy.1 c, add_lt_add_left hy.2 c⟩ }, { intro hx, obtain ⟨y, hy⟩ := exists_add_of_le hx.1, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ico.2 ⟨le_of_add_le_add_left hx.1, lt_of_add_lt_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ioc (a b c : α) : (Ioc a b).image ((+) c) = Ioc (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ioc], refine ⟨_, λ hx, _⟩, { rintro ⟨y, hy, rfl⟩, rw mem_Ioc at hy, exact ⟨add_lt_add_left hy.1 c, add_le_add_left hy.2 c⟩ }, { obtain ⟨y, hy⟩ := exists_add_of_le hx.1.le, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ioc.2 ⟨lt_of_add_lt_add_left hx.1, le_of_add_le_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ioo (a b c : α) : (Ioo a b).image ((+) c) = Ioo (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ioo], refine ⟨_, λ hx, _⟩, { rintro ⟨y, hy, rfl⟩, rw mem_Ioo at hy, exact ⟨add_lt_add_left hy.1 c, add_lt_add_left hy.2 c⟩ }, { obtain ⟨y, hy⟩ := exists_add_of_le hx.1.le, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ioo.2 ⟨lt_of_add_lt_add_left hx.1, lt_of_add_lt_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_right_Icc (a b c : α) : (Icc a b).image (+ c) = Icc (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Icc a b c } lemma image_add_right_Ico (a b c : α) : (Ico a b).image (+ c) = Ico (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ico a b c } lemma image_add_right_Ioc (a b c : α) : (Ioc a b).image (+ c) = Ioc (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ioc a b c } lemma image_add_right_Ioo (a b c : α) : (Ioo a b).image (+ c) = Ioo (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ioo a b c } end ordered_cancel_add_comm_monoid @[to_additive] lemma prod_prod_Ioi_mul_eq_prod_prod_off_diag [fintype ι] [linear_order ι] [locally_finite_order_top ι] [locally_finite_order_bot ι] [comm_monoid α] (f : ι → ι → α) : ∏ i, ∏ j in Ioi i, f j i * f i j = ∏ i, ∏ j in {i}ᶜ, f j i := begin simp_rw [←Ioi_disj_union_Iio, prod_disj_union, prod_mul_distrib], congr' 1, rw [prod_sigma', prod_sigma'], refine prod_bij' (λ i hi, ⟨i.2, i.1⟩) _ _ (λ i hi, ⟨i.2, i.1⟩) _ _ _; simp, end end finset
3057a3ded73c9b726164c927edd29a4a298dc85f
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/maps.lean
45c4b0b3318efbf5a19405a808fa2a2ee4716198
[ "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
25,059
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.order import topology.nhds_set /-! # Specific classes of maps between topological spaces This file introduces the following properties of a map `f : X → Y` between topological spaces: * `is_open_map f` means the image of an open set under `f` is open. * `is_closed_map f` means the image of a closed set under `f` is closed. (Open and closed maps need not be continuous.) * `inducing f` means the topology on `X` is the one induced via `f` from the topology on `Y`. These behave like embeddings except they need not be injective. Instead, points of `X` which are identified by `f` are also inseparable in the topology on `X`. * `embedding f` means `f` is inducing and also injective. Equivalently, `f` identifies `X` with a subspace of `Y`. * `open_embedding f` means `f` is an embedding with open image, so it identifies `X` with an open subspace of `Y`. Equivalently, `f` is an embedding and an open map. * `closed_embedding f` similarly means `f` is an embedding with closed image, so it identifies `X` with a closed subspace of `Y`. Equivalently, `f` is an embedding and a closed map. * `quotient_map f` is the dual condition to `embedding f`: `f` is surjective and the topology on `Y` is the one coinduced via `f` from the topology on `X`. Equivalently, `f` identifies `Y` with a quotient of `X`. Quotient maps are also sometimes known as identification maps. ## References * <https://en.wikipedia.org/wiki/Open_and_closed_maps> * <https://en.wikipedia.org/wiki/Embedding#General_topology> * <https://en.wikipedia.org/wiki/Quotient_space_(topology)#Quotient_map> ## Tags open map, closed map, embedding, quotient map, identification map -/ open set filter function open_locale topological_space filter variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section inducing /-- A function `f : α → β` between topological spaces is inducing if the topology on `α` is induced by the topology on `β` through `f`, meaning that a set `s : set α` is open iff it is the preimage under `f` of some open set `t : set β`. -/ @[mk_iff] structure inducing [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := (induced : tα = tβ.induced f) variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma inducing_id : inducing (@id α) := ⟨induced_id.symm⟩ protected lemma inducing.comp {g : β → γ} {f : α → β} (hg : inducing g) (hf : inducing f) : inducing (g ∘ f) := ⟨by rw [hf.induced, hg.induced, induced_compose]⟩ lemma inducing_of_inducing_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : inducing (g ∘ f)) : inducing f := ⟨le_antisymm (by rwa ← continuous_iff_le_induced) (by { rw [hgf.induced, ← continuous_iff_le_induced], apply hg.comp continuous_induced_dom })⟩ lemma inducing_iff_nhds {f : α → β} : inducing f ↔ ∀ a, 𝓝 a = comap f (𝓝 (f a)) := (inducing_iff _).trans (induced_iff_nhds_eq f) lemma inducing.nhds_eq_comap {f : α → β} (hf : inducing f) : ∀ (a : α), 𝓝 a = comap f (𝓝 $ f a) := inducing_iff_nhds.1 hf lemma inducing.nhds_set_eq_comap {f : α → β} (hf : inducing f) (s : set α) : 𝓝ˢ s = comap f (𝓝ˢ (f '' s)) := by simp only [nhds_set, Sup_image, comap_supr, hf.nhds_eq_comap, supr_image] lemma inducing.map_nhds_eq {f : α → β} (hf : inducing f) (a : α) : (𝓝 a).map f = 𝓝[range f] (f a) := hf.induced.symm ▸ map_nhds_induced_eq a lemma inducing.map_nhds_of_mem {f : α → β} (hf : inducing f) (a : α) (h : range f ∈ 𝓝 (f a)) : (𝓝 a).map f = 𝓝 (f a) := hf.induced.symm ▸ map_nhds_induced_of_mem h lemma inducing.image_mem_nhds_within {f : α → β} (hf : inducing f) {a : α} {s : set α} (hs : s ∈ 𝓝 a) : f '' s ∈ 𝓝[range f] (f a) := hf.map_nhds_eq a ▸ image_mem_map hs lemma inducing.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : inducing g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := by rw [hg.nhds_eq_comap, tendsto_comap_iff] lemma inducing.continuous_at_iff {f : α → β} {g : β → γ} (hg : inducing g) {x : α} : continuous_at f x ↔ continuous_at (g ∘ f) x := by simp_rw [continuous_at, inducing.tendsto_nhds_iff hg] lemma inducing.continuous_iff {f : α → β} {g : β → γ} (hg : inducing g) : continuous f ↔ continuous (g ∘ f) := by simp_rw [continuous_iff_continuous_at, hg.continuous_at_iff] lemma inducing.continuous_at_iff' {f : α → β} {g : β → γ} (hf : inducing f) {x : α} (h : range f ∈ 𝓝 (f x)) : continuous_at (g ∘ f) x ↔ continuous_at g (f x) := by { simp_rw [continuous_at, filter.tendsto, ← hf.map_nhds_of_mem _ h, filter.map_map] } protected lemma inducing.continuous {f : α → β} (hf : inducing f) : continuous f := hf.continuous_iff.mp continuous_id protected lemma inducing.inducing_iff {f : α → β} {g : β → γ} (hg : inducing g) : inducing f ↔ inducing (g ∘ f) := begin refine ⟨λ h, hg.comp h, λ hgf, inducing_of_inducing_compose _ hg.continuous hgf⟩, rw hg.continuous_iff, exact hgf.continuous end lemma inducing.closure_eq_preimage_closure_image {f : α → β} (hf : inducing f) (s : set α) : closure s = f ⁻¹' closure (f '' s) := by { ext x, rw [set.mem_preimage, ← closure_induced, hf.induced] } lemma inducing.is_closed_iff {f : α → β} (hf : inducing f) {s : set α} : is_closed s ↔ ∃ t, is_closed t ∧ f ⁻¹' t = s := by rw [hf.induced, is_closed_induced_iff] lemma inducing.is_closed_iff' {f : α → β} (hf : inducing f) {s : set α} : is_closed s ↔ ∀ x, f x ∈ closure (f '' s) → x ∈ s := by rw [hf.induced, is_closed_induced_iff'] lemma inducing.is_closed_preimage {f : α → β} (h : inducing f) (s : set β) (hs : is_closed s) : is_closed (f ⁻¹' s) := (inducing.is_closed_iff h).mpr ⟨s, hs, rfl⟩ lemma inducing.is_open_iff {f : α → β} (hf : inducing f) {s : set α} : is_open s ↔ ∃ t, is_open t ∧ f ⁻¹' t = s := by rw [hf.induced, is_open_induced_iff] lemma inducing.dense_iff {f : α → β} (hf : inducing f) {s : set α} : dense s ↔ ∀ x, f x ∈ closure (f '' s) := by simp only [dense, hf.closure_eq_preimage_closure_image, mem_preimage] end inducing section embedding /-- A function between topological spaces is an embedding if it is injective, and for all `s : set α`, `s` is open iff it is the preimage of an open set. -/ @[mk_iff] structure embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β) extends inducing f : Prop := (inj : injective f) lemma function.injective.embedding_induced [t : topological_space β] {f : α → β} (hf : injective f) : @_root_.embedding α β (t.induced f) t f := { induced := rfl, inj := hf } variables [topological_space α] [topological_space β] [topological_space γ] lemma embedding.mk' (f : α → β) (inj : injective f) (induced : ∀ a, comap f (𝓝 (f a)) = 𝓝 a) : embedding f := ⟨inducing_iff_nhds.2 (λ a, (induced a).symm), inj⟩ lemma embedding_id : embedding (@id α) := ⟨inducing_id, assume a₁ a₂ h, h⟩ lemma embedding.comp {g : β → γ} {f : α → β} (hg : embedding g) (hf : embedding f) : embedding (g ∘ f) := { inj:= assume a₁ a₂ h, hf.inj $ hg.inj h, ..hg.to_inducing.comp hf.to_inducing } lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : embedding (g ∘ f)) : embedding f := { induced := (inducing_of_inducing_compose hf hg hgf.to_inducing).induced, inj := assume a₁ a₂ h, hgf.inj $ by simp [h, (∘)] } protected lemma function.left_inverse.embedding {f : α → β} {g : β → α} (h : left_inverse f g) (hf : continuous f) (hg : continuous g) : embedding g := embedding_of_embedding_compose hg hf $ h.comp_eq_id.symm ▸ embedding_id lemma embedding.map_nhds_eq {f : α → β} (hf : embedding f) (a : α) : (𝓝 a).map f = 𝓝[range f] (f a) := hf.1.map_nhds_eq a lemma embedding.map_nhds_of_mem {f : α → β} (hf : embedding f) (a : α) (h : range f ∈ 𝓝 (f a)) : (𝓝 a).map f = 𝓝 (f a) := hf.1.map_nhds_of_mem a h lemma embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : embedding g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := hg.to_inducing.tendsto_nhds_iff lemma embedding.continuous_iff {f : α → β} {g : β → γ} (hg : embedding g) : continuous f ↔ continuous (g ∘ f) := inducing.continuous_iff hg.1 lemma embedding.continuous {f : α → β} (hf : embedding f) : continuous f := inducing.continuous hf.1 lemma embedding.closure_eq_preimage_closure_image {e : α → β} (he : embedding e) (s : set α) : closure s = e ⁻¹' closure (e '' s) := he.1.closure_eq_preimage_closure_image s end embedding /-- A function between topological spaces is a quotient map if it is surjective, and for all `s : set β`, `s` is open iff its preimage is an open set. -/ def quotient_map {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := surjective f ∧ tβ = tα.coinduced f lemma quotient_map_iff {α β : Type*} [topological_space α] [topological_space β] {f : α → β} : quotient_map f ↔ surjective f ∧ ∀ s : set β, is_open s ↔ is_open (f ⁻¹' s) := and_congr iff.rfl topological_space_eq_iff namespace quotient_map variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {g : β → γ} {f : α → β} protected lemma id : quotient_map (@id α) := ⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩ protected lemma comp (hg : quotient_map g) (hf : quotient_map f) : quotient_map (g ∘ f) := ⟨hg.left.comp hf.left, by rw [hg.right, hf.right, coinduced_compose]⟩ protected lemma of_quotient_map_compose (hf : continuous f) (hg : continuous g) (hgf : quotient_map (g ∘ f)) : quotient_map g := ⟨hgf.1.of_comp, le_antisymm (by { rw [hgf.right, ← continuous_iff_coinduced_le], apply continuous_coinduced_rng.comp hf }) (by rwa ← continuous_iff_coinduced_le)⟩ lemma of_inverse {g : β → α} (hf : continuous f) (hg : continuous g) (h : left_inverse g f) : quotient_map g := quotient_map.of_quotient_map_compose hf hg $ h.comp_eq_id.symm ▸ quotient_map.id protected lemma continuous_iff (hf : quotient_map f) : continuous g ↔ continuous (g ∘ f) := by rw [continuous_iff_coinduced_le, continuous_iff_coinduced_le, hf.right, coinduced_compose] protected lemma continuous (hf : quotient_map f) : continuous f := hf.continuous_iff.mp continuous_id protected lemma surjective (hf : quotient_map f) : surjective f := hf.1 protected lemma is_open_preimage (hf : quotient_map f) {s : set β} : is_open (f ⁻¹' s) ↔ is_open s := ((quotient_map_iff.1 hf).2 s).symm protected lemma is_closed_preimage (hf : quotient_map f) {s : set β} : is_closed (f ⁻¹' s) ↔ is_closed s := by simp only [← is_open_compl_iff, ← preimage_compl, hf.is_open_preimage] end quotient_map /-- A map `f : α → β` is said to be an *open map*, if the image of any open `U : set α` is open in `β`. -/ def is_open_map [topological_space α] [topological_space β] (f : α → β) := ∀ U : set α, is_open U → is_open (f '' U) namespace is_open_map variables [topological_space α] [topological_space β] [topological_space γ] {f : α → β} protected lemma id : is_open_map (@id α) := assume s hs, by rwa [image_id] protected lemma comp {g : β → γ} {f : α → β} (hg : is_open_map g) (hf : is_open_map f) : is_open_map (g ∘ f) := by intros s hs; rw [image_comp]; exact hg _ (hf _ hs) lemma is_open_range (hf : is_open_map f) : is_open (range f) := by { rw ← image_univ, exact hf _ is_open_univ } lemma image_mem_nhds (hf : is_open_map f) {x : α} {s : set α} (hx : s ∈ 𝓝 x) : f '' s ∈ 𝓝 (f x) := let ⟨t, hts, ht, hxt⟩ := mem_nhds_iff.1 hx in mem_of_superset (is_open.mem_nhds (hf t ht) (mem_image_of_mem _ hxt)) (image_subset _ hts) lemma range_mem_nhds (hf : is_open_map f) (x : α) : range f ∈ 𝓝 (f x) := hf.is_open_range.mem_nhds $ mem_range_self _ lemma maps_to_interior (hf : is_open_map f) {s : set α} {t : set β} (h : maps_to f s t) : maps_to f (interior s) (interior t) := maps_to'.2 $ interior_maximal (h.mono interior_subset subset.rfl).image_subset (hf _ is_open_interior) lemma image_interior_subset (hf : is_open_map f) (s : set α) : f '' interior s ⊆ interior (f '' s) := (hf.maps_to_interior (maps_to_image f s)).image_subset lemma nhds_le (hf : is_open_map f) (a : α) : 𝓝 (f a) ≤ (𝓝 a).map f := le_map $ λ s, hf.image_mem_nhds lemma of_nhds_le (hf : ∀ a, 𝓝 (f a) ≤ map f (𝓝 a)) : is_open_map f := λ s hs, is_open_iff_mem_nhds.2 $ λ b ⟨a, has, hab⟩, hab ▸ hf _ (image_mem_map $ is_open.mem_nhds hs has) lemma of_sections {f : α → β} (h : ∀ x, ∃ g : β → α, continuous_at g (f x) ∧ g (f x) = x ∧ right_inverse g f) : is_open_map f := of_nhds_le $ λ x, let ⟨g, hgc, hgx, hgf⟩ := h x in calc 𝓝 (f x) = map f (map g (𝓝 (f x))) : by rw [map_map, hgf.comp_eq_id, map_id] ... ≤ map f (𝓝 (g (f x))) : map_mono hgc ... = map f (𝓝 x) : by rw hgx lemma of_inverse {f : α → β} {f' : β → α} (h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') : is_open_map f := of_sections $ λ x, ⟨f', h.continuous_at, r_inv _, l_inv⟩ /-- A continuous surjective open map is a quotient map. -/ lemma to_quotient_map {f : α → β} (open_map : is_open_map f) (cont : continuous f) (surj : surjective f) : quotient_map f := quotient_map_iff.2 ⟨surj, λ s, ⟨λ h, h.preimage cont, λ h, surj.image_preimage s ▸ open_map _ h⟩⟩ lemma interior_preimage_subset_preimage_interior (hf : is_open_map f) {s : set β} : interior (f⁻¹' s) ⊆ f⁻¹' (interior s) := hf.maps_to_interior (maps_to_preimage _ _) lemma preimage_interior_eq_interior_preimage (hf₁ : is_open_map f) (hf₂ : continuous f) (s : set β) : f⁻¹' (interior s) = interior (f⁻¹' s) := subset.antisymm (preimage_interior_subset_interior_preimage hf₂) (interior_preimage_subset_preimage_interior hf₁) lemma preimage_closure_subset_closure_preimage (hf : is_open_map f) {s : set β} : f ⁻¹' (closure s) ⊆ closure (f ⁻¹' s) := begin rw ← compl_subset_compl, simp only [← interior_compl, ← preimage_compl, hf.interior_preimage_subset_preimage_interior] end lemma preimage_closure_eq_closure_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) : f ⁻¹' (closure s) = closure (f ⁻¹' s) := hf.preimage_closure_subset_closure_preimage.antisymm (hfc.closure_preimage_subset s) lemma preimage_frontier_subset_frontier_preimage (hf : is_open_map f) {s : set β} : f ⁻¹' (frontier s) ⊆ frontier (f ⁻¹' s) := by simpa only [frontier_eq_closure_inter_closure, preimage_inter] using inter_subset_inter hf.preimage_closure_subset_closure_preimage hf.preimage_closure_subset_closure_preimage lemma preimage_frontier_eq_frontier_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) : f ⁻¹' (frontier s) = frontier (f ⁻¹' s) := by simp only [frontier_eq_closure_inter_closure, preimage_inter, preimage_compl, hf.preimage_closure_eq_closure_preimage hfc] end is_open_map lemma is_open_map_iff_nhds_le [topological_space α] [topological_space β] {f : α → β} : is_open_map f ↔ ∀(a:α), 𝓝 (f a) ≤ (𝓝 a).map f := ⟨λ hf, hf.nhds_le, is_open_map.of_nhds_le⟩ lemma is_open_map_iff_interior [topological_space α] [topological_space β] {f : α → β} : is_open_map f ↔ ∀ s, f '' (interior s) ⊆ interior (f '' s) := ⟨is_open_map.image_interior_subset, λ hs u hu, subset_interior_iff_open.mp $ calc f '' u = f '' (interior u) : by rw hu.interior_eq ... ⊆ interior (f '' u) : hs u⟩ /-- An inducing map with an open range is an open map. -/ protected lemma inducing.is_open_map [topological_space α] [topological_space β] {f : α → β} (hi : inducing f) (ho : is_open (range f)) : is_open_map f := is_open_map.of_nhds_le $ λ x, (hi.map_nhds_of_mem _ $ is_open.mem_nhds ho $ mem_range_self _).ge section is_closed_map variables [topological_space α] [topological_space β] /-- A map `f : α → β` is said to be a *closed map*, if the image of any closed `U : set α` is closed in `β`. -/ def is_closed_map (f : α → β) := ∀ U : set α, is_closed U → is_closed (f '' U) end is_closed_map namespace is_closed_map variables [topological_space α] [topological_space β] [topological_space γ] open function protected lemma id : is_closed_map (@id α) := assume s hs, by rwa image_id protected lemma comp {g : β → γ} {f : α → β} (hg : is_closed_map g) (hf : is_closed_map f) : is_closed_map (g ∘ f) := by { intros s hs, rw image_comp, exact hg _ (hf _ hs) } lemma closure_image_subset {f : α → β} (hf : is_closed_map f) (s : set α) : closure (f '' s) ⊆ f '' closure s := closure_minimal (image_subset _ subset_closure) (hf _ is_closed_closure) lemma of_inverse {f : α → β} {f' : β → α} (h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') : is_closed_map f := assume s hs, have f' ⁻¹' s = f '' s, by ext x; simp [mem_image_iff_of_inverse r_inv l_inv], this ▸ hs.preimage h lemma of_nonempty {f : α → β} (h : ∀ s, is_closed s → s.nonempty → is_closed (f '' s)) : is_closed_map f := begin intros s hs, cases eq_empty_or_nonempty s with h2s h2s, { simp_rw [h2s, image_empty, is_closed_empty] }, { exact h s hs h2s } end lemma closed_range {f : α → β} (hf : is_closed_map f) : is_closed (range f) := @image_univ _ _ f ▸ hf _ is_closed_univ end is_closed_map lemma inducing.is_closed_map [topological_space α] [topological_space β] {f : α → β} (hf : inducing f) (h : is_closed (range f)) : is_closed_map f := begin intros s hs, rcases hf.is_closed_iff.1 hs with ⟨t, ht, rfl⟩, rw image_preimage_eq_inter_range, exact ht.inter h end lemma is_closed_map_iff_closure_image [topological_space α] [topological_space β] {f : α → β} : is_closed_map f ↔ ∀ s, closure (f '' s) ⊆ f '' closure s := ⟨is_closed_map.closure_image_subset, λ hs c hc, is_closed_of_closure_subset $ calc closure (f '' c) ⊆ f '' (closure c) : hs c ... = f '' c : by rw hc.closure_eq⟩ section open_embedding variables [topological_space α] [topological_space β] [topological_space γ] /-- An open embedding is an embedding with open image. -/ @[mk_iff] structure open_embedding (f : α → β) extends _root_.embedding f : Prop := (open_range : is_open $ range f) lemma open_embedding.is_open_map {f : α → β} (hf : open_embedding f) : is_open_map f := hf.to_embedding.to_inducing.is_open_map hf.open_range lemma open_embedding.map_nhds_eq {f : α → β} (hf : open_embedding f) (a : α) : map f (𝓝 a) = 𝓝 (f a) := hf.to_embedding.map_nhds_of_mem _ $ hf.open_range.mem_nhds $ mem_range_self _ lemma open_embedding.open_iff_image_open {f : α → β} (hf : open_embedding f) {s : set α} : is_open s ↔ is_open (f '' s) := ⟨hf.is_open_map s, λ h, begin convert ← h.preimage hf.to_embedding.continuous, apply preimage_image_eq _ hf.inj end⟩ lemma open_embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : open_embedding g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := hg.to_embedding.tendsto_nhds_iff lemma open_embedding.continuous {f : α → β} (hf : open_embedding f) : continuous f := hf.to_embedding.continuous lemma open_embedding.open_iff_preimage_open {f : α → β} (hf : open_embedding f) {s : set β} (hs : s ⊆ range f) : is_open s ↔ is_open (f ⁻¹' s) := begin convert ←hf.open_iff_image_open.symm, rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left] end lemma open_embedding_of_embedding_open {f : α → β} (h₁ : embedding f) (h₂ : is_open_map f) : open_embedding f := ⟨h₁, h₂.is_open_range⟩ lemma open_embedding_iff_embedding_open {f : α → β} : open_embedding f ↔ embedding f ∧ is_open_map f := ⟨λ h, ⟨h.1, h.is_open_map⟩, λ h, open_embedding_of_embedding_open h.1 h.2⟩ lemma open_embedding_of_continuous_injective_open {f : α → β} (h₁ : continuous f) (h₂ : injective f) (h₃ : is_open_map f) : open_embedding f := begin simp only [open_embedding_iff_embedding_open, embedding_iff, inducing_iff_nhds, *, and_true], exact λ a, le_antisymm (h₁.tendsto _).le_comap (@comap_map _ _ (𝓝 a) _ h₂ ▸ comap_mono (h₃.nhds_le _)) end lemma open_embedding_iff_continuous_injective_open {f : α → β} : open_embedding f ↔ continuous f ∧ injective f ∧ is_open_map f := ⟨λ h, ⟨h.continuous, h.inj, h.is_open_map⟩, λ h, open_embedding_of_continuous_injective_open h.1 h.2.1 h.2.2⟩ lemma open_embedding_id : open_embedding (@id α) := ⟨embedding_id, is_open_map.id.is_open_range⟩ lemma open_embedding.comp {g : β → γ} {f : α → β} (hg : open_embedding g) (hf : open_embedding f) : open_embedding (g ∘ f) := ⟨hg.1.comp hf.1, (hg.is_open_map.comp hf.is_open_map).is_open_range⟩ lemma open_embedding.is_open_map_iff {g : β → γ} {f : α → β} (hg : open_embedding g) : is_open_map f ↔ is_open_map (g ∘ f) := by simp only [is_open_map_iff_nhds_le, ← @map_map _ _ _ _ f g, ← hg.map_nhds_eq, map_le_map_iff hg.inj] lemma open_embedding.of_comp_iff (f : α → β) {g : β → γ} (hg : open_embedding g) : open_embedding (g ∘ f) ↔ open_embedding f := by simp only [open_embedding_iff_continuous_injective_open, ← hg.is_open_map_iff, ← hg.1.continuous_iff, hg.inj.of_comp_iff] lemma open_embedding.of_comp (f : α → β) {g : β → γ} (hg : open_embedding g) (h : open_embedding (g ∘ f)) : open_embedding f := (open_embedding.of_comp_iff f hg).1 h end open_embedding section closed_embedding variables [topological_space α] [topological_space β] [topological_space γ] /-- A closed embedding is an embedding with closed image. -/ @[mk_iff] structure closed_embedding (f : α → β) extends _root_.embedding f : Prop := (closed_range : is_closed $ range f) variables {f : α → β} lemma closed_embedding.tendsto_nhds_iff {ι : Type*} {g : ι → α} {a : filter ι} {b : α} (hf : closed_embedding f) : tendsto g a (𝓝 b) ↔ tendsto (f ∘ g) a (𝓝 (f b)) := hf.to_embedding.tendsto_nhds_iff lemma closed_embedding.continuous (hf : closed_embedding f) : continuous f := hf.to_embedding.continuous lemma closed_embedding.is_closed_map (hf : closed_embedding f) : is_closed_map f := hf.to_embedding.to_inducing.is_closed_map hf.closed_range lemma closed_embedding.closed_iff_image_closed (hf : closed_embedding f) {s : set α} : is_closed s ↔ is_closed (f '' s) := ⟨hf.is_closed_map s, λ h, begin convert ←continuous_iff_is_closed.mp hf.continuous _ h, apply preimage_image_eq _ hf.inj end⟩ lemma closed_embedding.closed_iff_preimage_closed (hf : closed_embedding f) {s : set β} (hs : s ⊆ range f) : is_closed s ↔ is_closed (f ⁻¹' s) := begin convert ←hf.closed_iff_image_closed.symm, rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left] end lemma closed_embedding_of_embedding_closed (h₁ : embedding f) (h₂ : is_closed_map f) : closed_embedding f := ⟨h₁, by convert h₂ univ is_closed_univ; simp⟩ lemma closed_embedding_of_continuous_injective_closed (h₁ : continuous f) (h₂ : injective f) (h₃ : is_closed_map f) : closed_embedding f := begin refine closed_embedding_of_embedding_closed ⟨⟨_⟩, h₂⟩ h₃, apply le_antisymm (continuous_iff_le_induced.mp h₁) _, intro s', change is_open _ ≤ is_open _, rw [←is_closed_compl_iff, ←is_closed_compl_iff], generalize : s'ᶜ = s, rw is_closed_induced_iff, refine λ hs, ⟨f '' s, h₃ s hs, _⟩, rw preimage_image_eq _ h₂ end lemma closed_embedding_id : closed_embedding (@id α) := ⟨embedding_id, by convert is_closed_univ; apply range_id⟩ lemma closed_embedding.comp {g : β → γ} {f : α → β} (hg : closed_embedding g) (hf : closed_embedding f) : closed_embedding (g ∘ f) := ⟨hg.to_embedding.comp hf.to_embedding, show is_closed (range (g ∘ f)), by rw [range_comp, ←hg.closed_iff_image_closed]; exact hf.closed_range⟩ lemma closed_embedding.closure_image_eq {f : α → β} (hf : closed_embedding f) (s : set α) : closure (f '' s) = f '' closure s := le_antisymm (is_closed_map_iff_closure_image.mp hf.is_closed_map _) (image_closure_subset_closure_image hf.continuous) end closed_embedding
5fb10c4211340a19b31f7bb1647e339d3477a5bd
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/meta/attribute.lean
55f1ce69683f36f9d6fa4eaccc2e2992e046afa6
[ "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
842
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ prelude import init.meta.tactic meta constant attribute.get_instances : name → tactic (list name) meta constant attribute.fingerprint : name → tactic nat structure user_attribute := (name : name) (descr : string) /- Registers a new user-defined attribute. The argument must be the name of a definition of type `user_attribute` or a sub-structure. -/ meta constant attribute.register : name → command meta structure caching_user_attribute (α : Type) extends user_attribute := (mk_cache : list _root_.name → tactic α) (dependencies : list _root_.name) meta constant caching_user_attribute.get_cache : Π {α : Type}, caching_user_attribute α → tactic α
6ec17d6fe8fee2985244cdc3ec3016fa6b2d5135
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/monoidal/Mon_.lean
7edbf17bd7ce7a29a0985b8806e68605d79019a5
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,413
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.discrete import category_theory.monoidal.unitors import category_theory.limits.shapes.terminal import algebra.punit_instances /-! # The category of monoids in a monoidal category. -/ universes v₁ v₂ u₁ u₂ open category_theory open category_theory.monoidal_category variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] /-- A monoid object internal to a monoidal category. When the monoidal category is preadditive, this is also sometimes called an "algebra object". -/ structure Mon_ := (X : C) (one : 𝟙_ C ⟶ X) (mul : X ⊗ X ⟶ X) (one_mul' : (one ⊗ 𝟙 X) ≫ mul = (λ_ X).hom . obviously) (mul_one' : (𝟙 X ⊗ one) ≫ mul = (ρ_ X).hom . obviously) -- Obviously there is some flexibility stating this axiom. -- This one has left- and right-hand sides matching the statement of `monoid.mul_assoc`, -- and chooses to place the associator on the right-hand side. -- The heuristic is that unitors and associators "don't have much weight". (mul_assoc' : (mul ⊗ 𝟙 X) ≫ mul = (α_ X X X).hom ≫ (𝟙 X ⊗ mul) ≫ mul . obviously) restate_axiom Mon_.one_mul' restate_axiom Mon_.mul_one' restate_axiom Mon_.mul_assoc' attribute [reassoc] Mon_.one_mul Mon_.mul_one -- We prove a more general `@[simp]` lemma below. attribute [simp, reassoc] Mon_.mul_assoc namespace Mon_ /-- The trivial monoid object. We later show this is initial in `Mon_ C`. -/ @[simps] def trivial : Mon_ C := { X := 𝟙_ C, one := 𝟙 _, mul := (λ_ _).hom, mul_assoc' := by simp_rw [triangle_assoc, iso.cancel_iso_hom_right, tensor_right_iff, unitors_equal], mul_one' := by simp [unitors_equal] } instance : inhabited (Mon_ C) := ⟨trivial C⟩ variables {C} {M : Mon_ C} @[simp] lemma one_mul_hom {Z : C} (f : Z ⟶ M.X) : (M.one ⊗ f) ≫ M.mul = (λ_ Z).hom ≫ f := by rw [←id_tensor_comp_tensor_id, category.assoc, M.one_mul, left_unitor_naturality] @[simp] lemma mul_one_hom {Z : C} (f : Z ⟶ M.X) : (f ⊗ M.one) ≫ M.mul = (ρ_ Z).hom ≫ f := by rw [←tensor_id_comp_id_tensor, category.assoc, M.mul_one, right_unitor_naturality] lemma assoc_flip : (𝟙 M.X ⊗ M.mul) ≫ M.mul = (α_ M.X M.X M.X).inv ≫ (M.mul ⊗ 𝟙 M.X) ≫ M.mul := by simp /-- A morphism of monoid objects. -/ @[ext] structure hom (M N : Mon_ C) := (hom : M.X ⟶ N.X) (one_hom' : M.one ≫ hom = N.one . obviously) (mul_hom' : M.mul ≫ hom = (hom ⊗ hom) ≫ N.mul . obviously) restate_axiom hom.one_hom' restate_axiom hom.mul_hom' attribute [simp, reassoc] hom.one_hom hom.mul_hom /-- The identity morphism on a monoid object. -/ @[simps] def id (M : Mon_ C) : hom M M := { hom := 𝟙 M.X, } instance hom_inhabited (M : Mon_ C) : inhabited (hom M M) := ⟨id M⟩ /-- Composition of morphisms of monoid objects. -/ @[simps] def comp {M N O : Mon_ C} (f : hom M N) (g : hom N O) : hom M O := { hom := f.hom ≫ g.hom, } instance : category (Mon_ C) := { hom := λ M N, hom M N, id := id, comp := λ M N O f g, comp f g, } @[simp] lemma id_hom' (M : Mon_ C) : (𝟙 M : hom M M).hom = 𝟙 M.X := rfl @[simp] lemma comp_hom' {M N K : Mon_ C} (f : M ⟶ N) (g : N ⟶ K) : (f ≫ g : hom M K).hom = f.hom ≫ g.hom := rfl section variables (C) /-- The forgetful functor from monoid objects to the ambient category. -/ @[simps] def forget : Mon_ C ⥤ C := { obj := λ A, A.X, map := λ A B f, f.hom, } end instance forget_faithful : faithful (@forget C _ _) := { } instance {A B : Mon_ C} (f : A ⟶ B) [e : is_iso ((forget C).map f)] : is_iso f.hom := e /-- The forgetful functor from monoid objects to the ambient category reflects isomorphisms. -/ instance : reflects_isomorphisms (forget C) := { reflects := λ X Y f e, by exactI { inv := { hom := inv f.hom, mul_hom' := begin simp only [is_iso.comp_inv_eq, hom.mul_hom, category.assoc, ←tensor_comp_assoc, is_iso.inv_hom_id, tensor_id, category.id_comp], end } } } instance unique_hom_from_trivial (A : Mon_ C) : unique (trivial C ⟶ A) := { default := { hom := A.one, one_hom' := by { dsimp, simp, }, mul_hom' := by { dsimp, simp [A.one_mul, unitors_equal], } }, uniq := λ f, begin ext, simp, rw [←category.id_comp f.hom], erw f.one_hom, end } open category_theory.limits instance : has_initial (Mon_ C) := has_initial_of_unique (trivial C) end Mon_ namespace category_theory.lax_monoidal_functor variables {C} {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] /-- A lax monoidal functor takes monoid objects to monoid objects. That is, a lax monoidal functor `F : C ⥤ D` induces a functor `Mon_ C ⥤ Mon_ D`. -/ -- TODO: map_Mod F A : Mod A ⥤ Mod (F.map_Mon A) @[simps] def map_Mon (F : lax_monoidal_functor C D) : Mon_ C ⥤ Mon_ D := { obj := λ A, { X := F.obj A.X, one := F.ε ≫ F.map A.one, mul := F.μ _ _ ≫ F.map A.mul, one_mul' := begin conv_lhs { rw [comp_tensor_id, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.one_mul], }, rw [F.to_functor.map_id], rw [F.left_unitality], end, mul_one' := begin conv_lhs { rw [id_tensor_comp, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.mul_one], }, rw [F.to_functor.map_id], rw [F.right_unitality], end, mul_assoc' := begin conv_lhs { rw [comp_tensor_id, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.mul_assoc], }, conv_lhs { rw [F.to_functor.map_id] }, conv_lhs { rw [F.to_functor.map_comp, F.to_functor.map_comp] }, conv_rhs { rw [id_tensor_comp, ←F.to_functor.map_id], }, slice_rhs 3 4 { rw [F.μ_natural], }, conv_rhs { rw [F.to_functor.map_id] }, slice_rhs 1 3 { rw [←F.associativity], }, simp only [category.assoc], end, }, map := λ A B f, { hom := F.map f.hom, one_hom' := by { dsimp, rw [category.assoc, ←F.to_functor.map_comp, f.one_hom], }, mul_hom' := begin dsimp, rw [category.assoc, F.μ_natural_assoc, ←F.to_functor.map_comp, ←F.to_functor.map_comp, f.mul_hom], end }, map_id' := λ A, by { ext, simp, }, map_comp' := λ A B C f g, by { ext, simp, }, } variables (C D) /-- `map_Mon` is functorial in the lax monoidal functor. -/ def map_Mon_functor : (lax_monoidal_functor C D) ⥤ (Mon_ C ⥤ Mon_ D) := { obj := map_Mon, map := λ F G α, { app := λ A, { hom := α.app A.X, } } } end category_theory.lax_monoidal_functor namespace Mon_ open category_theory.lax_monoidal_functor namespace equiv_lax_monoidal_functor_punit /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def lax_monoidal_to_Mon : lax_monoidal_functor (discrete punit) C ⥤ Mon_ C := { obj := λ F, (F.map_Mon : Mon_ _ ⥤ Mon_ C).obj (trivial (discrete punit)), map := λ F G α, ((map_Mon_functor (discrete punit) C).map α).app _ } /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def Mon_to_lax_monoidal : Mon_ C ⥤ lax_monoidal_functor (discrete punit) C := { obj := λ A, { obj := λ _, A.X, map := λ _ _ _, 𝟙 _, ε := A.one, μ := λ _ _, A.mul, map_id' := λ _, rfl, map_comp' := λ _ _ _ _ _, (category.id_comp (𝟙 A.X)).symm, }, map := λ A B f, { app := λ _, f.hom, naturality' := λ _ _ _, by { dsimp, rw [category.id_comp, category.comp_id], }, unit' := f.one_hom, tensor' := λ _ _, f.mul_hom, }, } /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def unit_iso : 𝟭 (lax_monoidal_functor (discrete punit) C) ≅ lax_monoidal_to_Mon C ⋙ Mon_to_lax_monoidal C := nat_iso.of_components (λ F, monoidal_nat_iso.of_components (λ _, F.to_functor.map_iso (eq_to_iso (by ext))) (by tidy) (by tidy) (by tidy)) (by tidy) /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def counit_iso : Mon_to_lax_monoidal C ⋙ lax_monoidal_to_Mon C ≅ 𝟭 (Mon_ C) := nat_iso.of_components (λ F, { hom := { hom := 𝟙 _, }, inv := { hom := 𝟙 _, } }) (by tidy) end equiv_lax_monoidal_functor_punit open equiv_lax_monoidal_functor_punit /-- Monoid objects in `C` are "just" lax monoidal functors from the trivial monoidal category to `C`. -/ @[simps] def equiv_lax_monoidal_functor_punit : lax_monoidal_functor (discrete punit) C ≌ Mon_ C := { functor := lax_monoidal_to_Mon C, inverse := Mon_to_lax_monoidal C, unit_iso := unit_iso C, counit_iso := counit_iso C, } end Mon_ /-! Projects: * Check that `Mon_ Mon ≌ CommMon`, via the Eckmann-Hilton argument. (You'll have to hook up the cartesian monoidal structure on `Mon` first, available in #3463) * Check that `Mon_ Top ≌ [bundled topological monoids]`. * Check that `Mon_ AddCommGroup ≌ Ring`. (We've already got `Mon_ (Module R) ≌ Algebra R`, in `category_theory.monoidal.internal.Module`.) * Can you transport this monoidal structure to `Ring` or `Algebra R`? How does it compare to the "native" one? * Show that if `C` is braided then `Mon_ C` is naturally monoidal. -/
4216729c0af8d482d4535a13890bafb4269e97eb
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/ProjFns.lean
818f71cee9f32ab3665f72980ee0f66ed301529d
[ "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,108
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment namespace Lean /- Given a structure `S`, Lean automatically creates an auxiliary definition (projection function) for each field. This structure caches information about these auxiliary definitions. -/ structure ProjectionFunctionInfo := (ctorName : Name) -- Constructor associated with the auxiliary projection function. (nparams : Nat) -- Number of parameters in the structure (i : Nat) -- The field index associated with the auxiliary projection function. (fromClass : Bool) -- `true` if the structure is a class @[export lean_mk_projection_info] def mkProjectionInfoEx (ctorName : Name) (nparams : Nat) (i : Nat) (fromClass : Bool) : ProjectionFunctionInfo := {ctorName := ctorName, nparams := nparams, i := i, fromClass := fromClass } @[export lean_projection_info_from_class] def ProjectionFunctionInfo.fromClassEx (info : ProjectionFunctionInfo) : Bool := info.fromClass instance : Inhabited ProjectionFunctionInfo := ⟨{ ctorName := arbitrary _, nparams := arbitrary _, i := 0, fromClass := false }⟩ builtin_initialize projectionFnInfoExt : SimplePersistentEnvExtension (Name × ProjectionFunctionInfo) (NameMap ProjectionFunctionInfo) ← registerSimplePersistentEnvExtension { name := `projinfo, addImportedFn := fun as => {}, addEntryFn := fun s p => s.insert p.1 p.2, toArrayFn := fun es => es.toArray.qsort (fun a b => Name.quickLt a.1 b.1) } @[export lean_add_projection_info] def addProjectionFnInfo (env : Environment) (projName : Name) (ctorName : Name) (nparams : Nat) (i : Nat) (fromClass : Bool) : Environment := projectionFnInfoExt.addEntry env (projName, { ctorName := ctorName, nparams := nparams, i := i, fromClass := fromClass }) namespace Environment @[export lean_get_projection_info] def getProjectionFnInfo? (env : Environment) (projName : Name) : Option ProjectionFunctionInfo := match env.getModuleIdxFor? projName with | some modIdx => match (projectionFnInfoExt.getModuleEntries env modIdx).binSearch (projName, arbitrary _) (fun a b => Name.quickLt a.1 b.1) with | some e => some e.2 | none => none | none => (projectionFnInfoExt.getState env).find? projName def isProjectionFn (env : Environment) (n : Name) : Bool := match env.getModuleIdxFor? n with | some modIdx => (projectionFnInfoExt.getModuleEntries env modIdx).binSearchContains (n, arbitrary _) (fun a b => Name.quickLt a.1 b.1) | none => (projectionFnInfoExt.getState env).contains n /-- If `projName` is the name of a projection function, return the associated structure name -/ def getProjectionStructureName? (env : Environment) (projName : Name) : Option Name := match env.getProjectionFnInfo? projName with | none => none | some projInfo => match env.find? projInfo.ctorName with | some (ConstantInfo.ctorInfo val) => some val.induct | _ => none end Environment end Lean
29775c8864a5bb3b7b72911a982007ded3a15afd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/times_cont_diff.lean
e7d003d868b1dc01a6a7416321f5adfbcaec469f
[]
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
139,095
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.mean_value import Mathlib.analysis.calculus.formal_multilinear_series import Mathlib.PostPort universes u_1 u_2 u_3 l u_4 u u_5 u_6 u_7 namespace Mathlib /-! # Higher differentiability A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. Finally, it is `C^∞` if it is `C^n` for all n. We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain, as well as predicates `times_cont_diff_within_at`, `times_cont_diff_at`, `times_cont_diff_on` and `times_cont_diff` saying that the function is `C^n` within a set at a point, at a point, on a set and on the whole space respectively. To avoid the issue of choice when choosing a derivative in sets where the derivative is not necessarily unique, `times_cont_diff_on` is not defined directly in terms of the regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the existence of a nice sequence of derivatives, expressed with a predicate `has_ftaylor_series_up_to_on`. We prove basic properties of these notions. ## Main definitions and results Let `f : E → F` be a map between normed vector spaces over a nondiscrete normed field `𝕜`. * `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`). * `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative is now taken inside `s`. In particular, derivatives don't have to be unique. * `times_cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to rank `n`. * `times_cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`. * `times_cont_diff_at 𝕜 n f x`: expresses that `f` is `C^n` around `x`. * `times_cont_diff_within_at 𝕜 n f s x`: expresses that `f` is `C^n` around `x` within the set `s`. * `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise. * `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of `iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise. In sets of unique differentiability, `times_cont_diff_on 𝕜 n f s` can be expressed in terms of the properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space, `times_cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f` for `m ≤ n`. We also prove that the usual operations (addition, multiplication, difference, composition, and so on) preserve `C^n` functions. ## Implementation notes The definitions in this file are designed to work on any field `𝕜`. They are sometimes slightly more complicated than the naive definitions one would guess from the intuition over the real or complex numbers, but they are designed to circumvent the lack of gluing properties and partitions of unity in general. In the usual situations, they coincide with the usual definitions. ### Definition of `C^n` functions in domains One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n` functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`. This definition still has the problem that a function which is locally `C^n` would not need to be `C^n`, as different choices of sequences of derivatives around different points might possibly not be glued together to give a globally defined sequence of derivatives. (Note that this issue can not happen over reals, thanks to partition of unity, but the behavior over a general field is not so clear, and we want a definition for general fields). Also, there are locality problems for the order parameter: one could image a function which, for each `n`, has a nice sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore not be glued to give rise to an infinite sequence of derivatives. This would give a function which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions in space and order in our definition of `times_cont_diff_within_at` and `times_cont_diff_on`. The resulting definition is slightly more complicated to work with (in fact not so much), but it gives rise to completely satisfactory theorems. For instance, with this definition, a real function which is `C^m` (but not better) on `(-1/m, 1/m)` for each natural `m` is by definition `C^∞` at `0`. There is another issue with the definition of `times_cont_diff_within_at 𝕜 n f s x`. We can require the existence and good behavior of derivatives up to order `n` on a neighborhood of `x` within `s`. However, this does not imply continuity or differentiability within `s` of the function at `x` when `x` does not belong to `s`. Therefore, we require such existence and good behavior on a neighborhood of `x` within `s ∪ {x}` (which appears as `insert x s` in this file). ### Side of the composition, and universe issues With a naïve direct definition, the `n`-th derivative of a function belongs to the space `E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space may also be seen as the space of continuous multilinear functions on `n` copies of `E` with values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks, and that we also use. This means that the definition and the first proofs are slightly involved, as one has to keep track of the uncurrying operation. The uncurrying can be done from the left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of the `n`-th derivative, or as the `n`-th derivative of the derivative. For proofs, it would be more convenient to use the latter approach (from the right), as it means to prove things at the `n+1`-th step we only need to understand well enough the derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know enough on the `n`-th derivative to deduce things on the `n+1`-th derivative). However, the definition from the right leads to a universe polymorphism problem: if we define `iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is only possible to generalize over all spaces in some fixed universe in an inductive definition. For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only work if `F` and `E →L[𝕜] F` are in the same universe. This issue does not appear with the definition from the left, where one does not need to generalize over all spaces. Therefore, we use the definition from the left. This means some proofs later on become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the inductive approach where one would prove smoothness statements without giving a formula for the derivative). In the end, this approach is still satisfactory as it is good to have formulas for the iterated derivatives in various constructions. One point where we depart from this explicit approach is in the proof of smoothness of a composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula), but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we give the inductive proof. As explained above, it works by generalizing over the target space, hence it only works well if all spaces belong to the same universe. To get the general version, we lift things to a common universe using a trick. ### Variables management The textbook definitions and proofs use various identifications and abuse of notations, for instance when saying that the natural space in which the derivative lives, i.e., `E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things formally, we need to provide explicit maps for these identifications, and chase some diagrams to see everything is compatible with the identifications. In particular, one needs to check that taking the derivative and then doing the identification, or first doing the identification and then taking the derivative, gives the same result. The key point for this is that taking the derivative commutes with continuous linear equivalences. Therefore, we need to implement all our identifications with continuous linear equivs. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. In this file, we denote `⊤ : with_top ℕ` with `∞`. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ /-! ### Functions with a Taylor series on a domain -/ /-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_within_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E) where zero_eq : ∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x fderiv_within : ∀ (m : ℕ), ↑m < n → ∀ (x : E), x ∈ s → has_fderiv_within_at (fun (y : E) => p y m) (continuous_multilinear_map.curry_left (p x (Nat.succ m))) s x cont : ∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => p x m) s theorem has_ftaylor_series_up_to_on.zero_eq' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) : p x 0 = coe_fn (continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) (f x) := sorry /-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a Taylor series for the second one. -/ theorem has_ftaylor_series_up_to_on.congr {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) : has_ftaylor_series_up_to_on n f₁ p s := sorry theorem has_ftaylor_series_up_to_on.mono {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) : has_ftaylor_series_up_to_on n f p t := sorry theorem has_ftaylor_series_up_to_on.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {m : with_top ℕ} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) : has_ftaylor_series_up_to_on m f p s := sorry theorem has_ftaylor_series_up_to_on.continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s := sorry theorem has_ftaylor_series_up_to_on_zero_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} : has_ftaylor_series_up_to_on 0 f p s ↔ continuous_on f s ∧ ∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x := sorry theorem has_ftaylor_series_up_to_on_top_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} : has_ftaylor_series_up_to_on ⊤ f p s ↔ ∀ (n : ℕ), has_ftaylor_series_up_to_on (↑n) f p s := sorry /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_on.has_fderiv_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) : has_fderiv_within_at f (coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) s x := sorry theorem has_ftaylor_series_up_to_on.differentiable_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := fun (x : E) (hx : x ∈ s) => has_fderiv_within_at.differentiable_within_at (has_ftaylor_series_up_to_on.has_fderiv_within_at h hn hx) /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and `p (n + 1)` is a derivative of `p n`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_left {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} : has_ftaylor_series_up_to_on (↑n + 1) f p s ↔ has_ftaylor_series_up_to_on (↑n) f p s ∧ (∀ (x : E), x ∈ s → has_fderiv_within_at (fun (y : E) => p y n) (continuous_multilinear_map.curry_left (p x (Nat.succ n))) s x) ∧ continuous_on (fun (x : E) => p x (n + 1)) s := sorry /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} : has_ftaylor_series_up_to_on (↑(n + 1)) f p s ↔ (∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x) ∧ (∀ (x : E), x ∈ s → has_fderiv_within_at (fun (y : E) => p y 0) (continuous_multilinear_map.curry_left (p x 1)) s x) ∧ has_ftaylor_series_up_to_on (↑n) (fun (x : E) => coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) (fun (x : E) => formal_multilinear_series.shift (p x)) s := sorry /-! ### Smooth functions within a set around a point -/ /-- A function is continuously differentiable up to order `n` within a set `s` at a point `x` if it admits continuous derivatives up to order `n` in a neighborhood of `x` in `s ∪ {x}`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). For instance, a real function which is `C^m` on `(-1/m, 1/m)` for each natural `m`, but not better, is `C^∞` at `0` within `univ`. -/ def times_cont_diff_within_at (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) (s : set E) (x : E) := ∀ (m : ℕ), ↑m ≤ n → ∃ (u : set E), ∃ (H : u ∈ nhds_within x (insert x s)), ∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to_on (↑m) f p u theorem times_cont_diff_within_at_nat {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : ℕ} : times_cont_diff_within_at 𝕜 (↑n) f s x ↔ ∃ (u : set E), ∃ (H : u ∈ nhds_within x (insert x s)), ∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to_on (↑n) f p u := sorry theorem times_cont_diff_within_at.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hmn : m ≤ n) : times_cont_diff_within_at 𝕜 m f s x := fun (k : ℕ) (hk : ↑k ≤ m) => h k (le_trans hk hmn) theorem times_cont_diff_within_at_iff_forall_nat_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f s x ↔ ∀ (m : ℕ), ↑m ≤ n → times_cont_diff_within_at 𝕜 (↑m) f s x := { mp := fun (H : times_cont_diff_within_at 𝕜 n f s x) (m : ℕ) (hm : ↑m ≤ n) => times_cont_diff_within_at.of_le H hm, mpr := fun (H : ∀ (m : ℕ), ↑m ≤ n → times_cont_diff_within_at 𝕜 (↑m) f s x) (m : ℕ) (hm : ↑m ≤ n) => H m hm m le_rfl } theorem times_cont_diff_within_at_top {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} : times_cont_diff_within_at 𝕜 ⊤ f s x ↔ ∀ (n : ℕ), times_cont_diff_within_at 𝕜 (↑n) f s x := sorry theorem times_cont_diff_within_at.continuous_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) : continuous_within_at f s x := sorry theorem times_cont_diff_within_at.congr_of_eventually_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : filter.eventually_eq (nhds_within x s) f₁ f) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x := sorry theorem times_cont_diff_within_at.congr_of_eventually_eq' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : filter.eventually_eq (nhds_within x s) f₁ f) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f₁ s x := times_cont_diff_within_at.congr_of_eventually_eq h h₁ (filter.eventually.self_of_nhds_within h₁ hx) theorem filter.eventually_eq.times_cont_diff_within_at_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ} (h₁ : filter.eventually_eq (nhds_within x s) f₁ f) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x ↔ times_cont_diff_within_at 𝕜 n f s x := sorry theorem times_cont_diff_within_at.congr {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ (y : E), y ∈ s → f₁ y = f y) (hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x := times_cont_diff_within_at.congr_of_eventually_eq h (filter.eventually_eq_of_mem self_mem_nhds_within h₁) hx theorem times_cont_diff_within_at.mono_of_mem {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : s ∈ nhds_within x t) : times_cont_diff_within_at 𝕜 n f t x := sorry theorem times_cont_diff_within_at.mono {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : t ⊆ s) : times_cont_diff_within_at 𝕜 n f t x := times_cont_diff_within_at.mono_of_mem h (filter.mem_sets_of_superset self_mem_nhds_within hst) theorem times_cont_diff_within_at.congr_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : nhds_within x s = nhds_within x t) : times_cont_diff_within_at 𝕜 n f t x := times_cont_diff_within_at.mono_of_mem h (hst ▸ self_mem_nhds_within) theorem times_cont_diff_within_at_congr_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} {t : set E} (hst : nhds_within x s = nhds_within x t) : times_cont_diff_within_at 𝕜 n f s x ↔ times_cont_diff_within_at 𝕜 n f t x := { mp := fun (h : times_cont_diff_within_at 𝕜 n f s x) => times_cont_diff_within_at.congr_nhds h hst, mpr := fun (h : times_cont_diff_within_at 𝕜 n f t x) => times_cont_diff_within_at.congr_nhds h (Eq.symm hst) } theorem times_cont_diff_within_at_inter' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {t : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : t ∈ nhds_within x s) : times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at_congr_nhds (Eq.symm (nhds_within_restrict'' s h)) theorem times_cont_diff_within_at_inter {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {t : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : t ∈ nhds x) : times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds h) /-- If a function is `C^n` within a set at a point, with `n ≥ 1`, then it is differentiable within this set at this point. -/ theorem times_cont_diff_within_at.differentiable_within_at' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) : differentiable_within_at 𝕜 f (insert x s) x := sorry theorem times_cont_diff_within_at.differentiable_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) : differentiable_within_at 𝕜 f s x := differentiable_within_at.mono (times_cont_diff_within_at.differentiable_within_at' h hn) (set.subset_insert x s) /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_within_at_succ_iff_has_fderiv_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : ℕ} : times_cont_diff_within_at 𝕜 (↑(n + 1)) f s x ↔ ∃ (u : set E), ∃ (H : u ∈ nhds_within x (insert x s)), ∃ (f' : E → continuous_linear_map 𝕜 E F), (∀ (x : E), x ∈ u → has_fderiv_within_at f (f' x) u x) ∧ times_cont_diff_within_at 𝕜 (↑n) f' u x := sorry /-! ### Smooth functions within a set -/ /-- A function is continuously differentiable up to `n` on `s` if, for any point `x` in `s`, it admits continuous derivatives up to order `n` on a neighborhood of `x` in `s`. For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may depend on the finite order we consider). -/ def times_cont_diff_on (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) (s : set E) := ∀ (x : E), x ∈ s → times_cont_diff_within_at 𝕜 n f s x theorem times_cont_diff_on.times_cont_diff_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f s x := h x hx theorem times_cont_diff_within_at.times_cont_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} {m : ℕ} (hm : ↑m ≤ n) (h : times_cont_diff_within_at 𝕜 n f s x) : ∃ (u : set E), ∃ (H : u ∈ nhds_within x (insert x s)), u ⊆ insert x s ∧ times_cont_diff_on 𝕜 (↑m) f u := sorry theorem times_cont_diff_on.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : m ≤ n) : times_cont_diff_on 𝕜 m f s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.of_le (h x hx) hmn theorem times_cont_diff_on_iff_forall_nat_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} : times_cont_diff_on 𝕜 n f s ↔ ∀ (m : ℕ), ↑m ≤ n → times_cont_diff_on 𝕜 (↑m) f s := sorry theorem times_cont_diff_on_top {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} : times_cont_diff_on 𝕜 ⊤ f s ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 (↑n) f s := sorry theorem times_cont_diff_on_all_iff_nat {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} : (∀ (n : with_top ℕ), times_cont_diff_on 𝕜 n f s) ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 (↑n) f s := sorry theorem times_cont_diff_on.continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) : continuous_on f s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.continuous_within_at (h x hx) theorem times_cont_diff_on.congr {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.congr (h x hx) h₁ (h₁ x hx) theorem times_cont_diff_on_congr {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ} (h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s ↔ times_cont_diff_on 𝕜 n f s := { mp := fun (H : times_cont_diff_on 𝕜 n f₁ s) => times_cont_diff_on.congr H fun (x : E) (hx : x ∈ s) => Eq.symm (h₁ x hx), mpr := fun (H : times_cont_diff_on 𝕜 n f s) => times_cont_diff_on.congr H h₁ } theorem times_cont_diff_on.mono {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) {t : set E} (hst : t ⊆ s) : times_cont_diff_on 𝕜 n f t := fun (x : E) (hx : x ∈ t) => times_cont_diff_within_at.mono (h x (hst hx)) hst theorem times_cont_diff_on.congr_mono {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {s₁ : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ (x : E), x ∈ s₁ → f₁ x = f x) (hs : s₁ ⊆ s) : times_cont_diff_on 𝕜 n f₁ s₁ := times_cont_diff_on.congr (times_cont_diff_on.mono hf hs) h₁ /-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/ theorem times_cont_diff_on.differentiable_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.differentiable_within_at (h x hx) hn /-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/ theorem times_cont_diff_on_of_locally_times_cont_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : ∀ (x : E), x ∈ s → ∃ (u : set E), is_open u ∧ x ∈ u ∧ times_cont_diff_on 𝕜 n f (s ∩ u)) : times_cont_diff_on 𝕜 n f s := sorry /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_on_succ_iff_has_fderiv_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : ℕ} : times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔ ∀ (x : E) (H : x ∈ s), ∃ (u : set E), ∃ (H : u ∈ nhds_within x (insert x s)), ∃ (f' : E → continuous_linear_map 𝕜 E F), (∀ (x : E), x ∈ u → has_fderiv_within_at f (f' x) u x) ∧ times_cont_diff_on 𝕜 (↑n) f' u := sorry /-! ### Iterated derivative within a set -/ /-- The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with an uncurrying step to see it as a multilinear map in `n+1` variables.. -/ def iterated_fderiv_within (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : ℕ) (f : E → F) (s : set E) : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F := nat.rec_on n (fun (x : E) => continuous_multilinear_map.curry0 𝕜 E (f x)) fun (n : ℕ) (rec : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F) (x : E) => continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series_within (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F := fun (n : ℕ) => iterated_fderiv_within 𝕜 n f s x @[simp] theorem iterated_fderiv_within_zero_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} (m : fin 0 → E) : coe_fn (iterated_fderiv_within 𝕜 0 f s x) m = f x := rfl theorem iterated_fderiv_within_zero_eq_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} : iterated_fderiv_within 𝕜 0 f s = ⇑(continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) ∘ f := rfl theorem iterated_fderiv_within_succ_apply_left {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) : coe_fn (iterated_fderiv_within 𝕜 (n + 1) f s x) m = coe_fn (coe_fn (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x) (m 0)) (fin.tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ theorem iterated_fderiv_within_succ_eq_comp_left {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : ℕ} : iterated_fderiv_within 𝕜 (n + 1) f s = ⇑(continuous_multilinear_curry_left_equiv 𝕜 (fun (i : fin (n + 1)) => E) F) ∘ fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s := rfl theorem iterated_fderiv_within_succ_apply_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin (n + 1) → E) : coe_fn (iterated_fderiv_within 𝕜 (n + 1) f s x) m = coe_fn (coe_fn (iterated_fderiv_within 𝕜 n (fun (y : E) => fderiv_within 𝕜 f s y) s x) (fin.init m)) (m (fin.last n)) := sorry /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ theorem iterated_fderiv_within_succ_eq_comp_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 (n + 1) f s x = function.comp (⇑(continuous_multilinear_curry_right_equiv' 𝕜 n E F)) (iterated_fderiv_within 𝕜 n (fun (y : E) => fderiv_within 𝕜 f s y) s) x := sorry @[simp] theorem iterated_fderiv_within_one_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin 1 → E) : coe_fn (iterated_fderiv_within 𝕜 1 f s x) m = coe_fn (fderiv_within 𝕜 f s x) (m 0) := sorry /-- If two functions coincide on a set `s` of unique differentiability, then their iterated differentials within this set coincide. -/ theorem iterated_fderiv_within_congr {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s) (hL : ∀ (y : E), y ∈ s → f₁ y = f y) (hx : x ∈ s) : iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x := sorry /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with an open set containing `x`. -/ theorem iterated_fderiv_within_inter_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : is_open u) (hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := sorry /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x` within `s`. -/ theorem iterated_fderiv_within_inter' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : u ∈ nhds_within x s) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := sorry /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x`. -/ theorem iterated_fderiv_within_inter {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : u ∈ nhds x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs @[simp] theorem times_cont_diff_on_zero {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} : times_cont_diff_on 𝕜 0 f s ↔ continuous_on f s := sorry theorem times_cont_diff_within_at_zero {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} (hx : x ∈ s) : times_cont_diff_within_at 𝕜 0 f s x ↔ ∃ (u : set E), ∃ (H : u ∈ nhds_within x s), continuous_on f (s ∩ u) := sorry /-- On a set with unique differentiability, any choice of iterated differential has to coincide with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/ theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {m : ℕ} (hmn : ↑m ≤ n) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : p x m = iterated_fderiv_within 𝕜 m f s x := sorry /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on.ftaylor_series_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) : has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s := sorry theorem times_cont_diff_on_of_continuous_on_differentiable_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (Hcont : ∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s) (Hdiff : ∀ (m : ℕ), ↑m < n → differentiable_on 𝕜 (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s) : times_cont_diff_on 𝕜 n f s := sorry theorem times_cont_diff_on_of_differentiable_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : ∀ (m : ℕ), ↑m ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) : times_cont_diff_on 𝕜 n f s := times_cont_diff_on_of_continuous_on_differentiable_on (fun (m : ℕ) (hm : ↑m ≤ n) => differentiable_on.continuous_on (h m hm)) fun (m : ℕ) (hm : ↑m < n) => h m (le_of_lt hm) theorem times_cont_diff_on.continuous_on_iterated_fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : ↑m ≤ n) (hs : unique_diff_on 𝕜 s) : continuous_on (iterated_fderiv_within 𝕜 m f s) s := has_ftaylor_series_up_to_on.cont (times_cont_diff_on.ftaylor_series_within h hs) m hmn theorem times_cont_diff_on.differentiable_on_iterated_fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : ↑m < n) (hs : unique_diff_on 𝕜 s) : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s := fun (x : E) (hx : x ∈ s) => has_fderiv_within_at.differentiable_within_at (has_ftaylor_series_up_to_on.fderiv_within (times_cont_diff_on.ftaylor_series_within h hs) m hmn x hx) theorem times_cont_diff_on_iff_continuous_on_differentiable_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 n f s ↔ (∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s) ∧ ∀ (m : ℕ), ↑m < n → differentiable_on 𝕜 (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s := sorry /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (expressed with `fderiv_within`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 (↑n) (fun (y : E) => fderiv_within 𝕜 f s y) s := sorry /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (expressed with `fderiv`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : ℕ} (hs : is_open s) : times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 (↑n) (fun (y : E) => fderiv 𝕜 f y) s := sorry /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (expressed with `fderiv_within`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ⊤ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (fun (y : E) => fderiv_within 𝕜 f s y) s := sorry /-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its derivative (expressed with `fderiv`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} (hs : is_open s) : times_cont_diff_on 𝕜 ⊤ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (fun (y : E) => fderiv 𝕜 f y) s := sorry theorem times_cont_diff_on.fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (fun (y : E) => fderiv_within 𝕜 f s y) s := sorry theorem times_cont_diff_on.fderiv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : is_open s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (fun (y : E) => fderiv 𝕜 f y) s := times_cont_diff_on.congr (times_cont_diff_on.fderiv_within hf (is_open.unique_diff_on hs) hmn) fun (x : E) (hx : x ∈ s) => Eq.symm (fderiv_within_of_open hs hx) theorem times_cont_diff_on.continuous_on_fderiv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (fun (x : E) => fderiv_within 𝕜 f s x) s := times_cont_diff_on.continuous_on (and.right (iff.mp (times_cont_diff_on_succ_iff_fderiv_within hs) (times_cont_diff_on.of_le h hn))) theorem times_cont_diff_on.continuous_on_fderiv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : is_open s) (hn : 1 ≤ n) : continuous_on (fun (x : E) => fderiv 𝕜 f x) s := times_cont_diff_on.continuous_on (and.right (iff.mp (times_cont_diff_on_succ_iff_fderiv_of_open hs) (times_cont_diff_on.of_le h hn))) /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ theorem times_cont_diff_on.continuous_on_fderiv_within_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (fun (p : E × E) => coe_fn (fderiv_within 𝕜 f s (prod.fst p)) (prod.snd p)) (set.prod s set.univ) := sorry /-! ### Functions with a Taylor series on the whole space -/ /-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) where zero_eq : ∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x fderiv : ∀ (m : ℕ), ↑m < n → ∀ (x : E), has_fderiv_at (fun (y : E) => p y m) (continuous_multilinear_map.curry_left (p x (Nat.succ m))) x cont : ∀ (m : ℕ), ↑m ≤ n → continuous fun (x : E) => p x m theorem has_ftaylor_series_up_to.zero_eq' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (x : E) : p x 0 = coe_fn (continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) (f x) := sorry theorem has_ftaylor_series_up_to_on_univ_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} : has_ftaylor_series_up_to_on n f p set.univ ↔ has_ftaylor_series_up_to n f p := sorry theorem has_ftaylor_series_up_to.has_ftaylor_series_up_to_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (s : set E) : has_ftaylor_series_up_to_on n f p s := has_ftaylor_series_up_to_on.mono (iff.mpr has_ftaylor_series_up_to_on_univ_iff h) (set.subset_univ s) theorem has_ftaylor_series_up_to.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {m : with_top ℕ} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) : has_ftaylor_series_up_to m f p := sorry theorem has_ftaylor_series_up_to.continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) : continuous f := sorry theorem has_ftaylor_series_up_to_zero_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} : has_ftaylor_series_up_to 0 f p ↔ continuous f ∧ ∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x := sorry /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ theorem has_ftaylor_series_up_to.has_fderiv_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) : has_fderiv_at f (coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) x := sorry theorem has_ftaylor_series_up_to.differentiable {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f := fun (x : E) => has_fderiv_at.differentiable_at (has_ftaylor_series_up_to.has_fderiv_at h hn x) /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_succ_iff_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} : has_ftaylor_series_up_to (↑(n + 1)) f p ↔ (∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x) ∧ (∀ (x : E), has_fderiv_at (fun (y : E) => p y 0) (continuous_multilinear_map.curry_left (p x 1)) x) ∧ has_ftaylor_series_up_to (↑n) (fun (x : E) => coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) fun (x : E) => formal_multilinear_series.shift (p x) := sorry /-! ### Smooth functions at a point -/ /-- A function is continuously differentiable up to `n` at a point `x` if, for any integer `k ≤ n`, there is a neighborhood of `x` where `f` admits derivatives up to order `n`, which are continuous. -/ def times_cont_diff_at (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) (x : E) := times_cont_diff_within_at 𝕜 n f set.univ x theorem times_cont_diff_within_at_univ {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f set.univ x ↔ times_cont_diff_at 𝕜 n f x := iff.rfl theorem times_cont_diff_at_top {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} : times_cont_diff_at 𝕜 ⊤ f x ↔ ∀ (n : ℕ), times_cont_diff_at 𝕜 (↑n) f x := sorry theorem times_cont_diff_at.times_cont_diff_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) : times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_within_at.mono h (set.subset_univ s) theorem times_cont_diff_within_at.times_cont_diff_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x) (hx : s ∈ nhds x) : times_cont_diff_at 𝕜 n f x := sorry theorem times_cont_diff_at.congr_of_eventually_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hg : filter.eventually_eq (nhds x) f₁ f) : times_cont_diff_at 𝕜 n f₁ x := times_cont_diff_within_at.congr_of_eventually_eq' h (eq.mpr (id (Eq._oldrec (Eq.refl (filter.eventually_eq (nhds_within x set.univ) f₁ f)) (nhds_within_univ x))) hg) (set.mem_univ x) theorem times_cont_diff_at.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hmn : m ≤ n) : times_cont_diff_at 𝕜 m f x := times_cont_diff_within_at.of_le h hmn theorem times_cont_diff_at.continuous_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) : continuous_at f x := eq.mpr (id (Eq.refl (continuous_at f x))) (eq.mp (propext (continuous_within_at_univ f x)) (times_cont_diff_within_at.continuous_within_at h)) /-- If a function is `C^n` with `n ≥ 1` at a point, then it is differentiable there. -/ theorem times_cont_diff_at.differentiable_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) (hn : 1 ≤ n) : differentiable_at 𝕜 f x := sorry /-- A function is `C^(n + 1)` at a point iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_at_succ_iff_has_fderiv_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : ℕ} : times_cont_diff_at 𝕜 (↑(n + 1)) f x ↔ ∃ (f' : E → continuous_linear_map 𝕜 E F), (∃ (u : set E), ∃ (H : u ∈ nhds x), ∀ (x : E), x ∈ u → has_fderiv_at f (f' x) x) ∧ times_cont_diff_at 𝕜 (↑n) f' x := sorry /-! ### Smooth functions -/ /-- A function is continuously differentiable up to `n` if it admits derivatives up to order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives might not be unique) we do not need to localize the definition in space or time. -/ def times_cont_diff (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : with_top ℕ) (f : E → F) := ∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to n f p theorem times_cont_diff_on_univ {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} : times_cont_diff_on 𝕜 n f set.univ ↔ times_cont_diff 𝕜 n f := sorry theorem times_cont_diff_iff_times_cont_diff_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ ∀ (x : E), times_cont_diff_at 𝕜 n f x := sorry theorem times_cont_diff.times_cont_diff_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_at 𝕜 n f x := iff.mp times_cont_diff_iff_times_cont_diff_at h x theorem times_cont_diff.times_cont_diff_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_within_at 𝕜 n f s x := times_cont_diff_at.times_cont_diff_within_at (times_cont_diff.times_cont_diff_at h) theorem times_cont_diff_top {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : times_cont_diff 𝕜 ⊤ f ↔ ∀ (n : ℕ), times_cont_diff 𝕜 (↑n) f := sorry theorem times_cont_diff_all_iff_nat {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : (∀ (n : with_top ℕ), times_cont_diff 𝕜 n f) ↔ ∀ (n : ℕ), times_cont_diff 𝕜 (↑n) f := sorry theorem times_cont_diff.times_cont_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_on 𝕜 n f s := times_cont_diff_on.mono (iff.mpr times_cont_diff_on_univ h) (set.subset_univ s) @[simp] theorem times_cont_diff_zero {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : times_cont_diff 𝕜 0 f ↔ continuous f := sorry theorem times_cont_diff_at_zero {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} : times_cont_diff_at 𝕜 0 f x ↔ ∃ (u : set E), ∃ (H : u ∈ nhds x), continuous_on f u := sorry theorem times_cont_diff.of_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hmn : m ≤ n) : times_cont_diff 𝕜 m f := iff.mp times_cont_diff_on_univ (times_cont_diff_on.of_le (iff.mpr times_cont_diff_on_univ h) hmn) theorem times_cont_diff.continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : continuous f := iff.mp times_cont_diff_zero (times_cont_diff.of_le h bot_le) /-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/ theorem times_cont_diff.differentiable {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f := iff.mp differentiable_on_univ (times_cont_diff_on.differentiable_on (iff.mpr times_cont_diff_on_univ h) hn) /-! ### Iterated derivative -/ /-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/ def iterated_fderiv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (n : ℕ) (f : E → F) : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F := nat.rec_on n (fun (x : E) => continuous_multilinear_map.curry0 𝕜 E (f x)) fun (n : ℕ) (rec : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F) (x : E) => continuous_linear_map.uncurry_left (fderiv 𝕜 rec x) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (x : E) : formal_multilinear_series 𝕜 E F := fun (n : ℕ) => iterated_fderiv 𝕜 n f x @[simp] theorem iterated_fderiv_zero_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} (m : fin 0 → E) : coe_fn (iterated_fderiv 𝕜 0 f x) m = f x := rfl theorem iterated_fderiv_zero_eq_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : iterated_fderiv 𝕜 0 f = ⇑(continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) ∘ f := rfl theorem iterated_fderiv_succ_apply_left {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) : coe_fn (iterated_fderiv 𝕜 (n + 1) f x) m = coe_fn (coe_fn (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x) (m 0)) (fin.tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ theorem iterated_fderiv_succ_eq_comp_left {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f = ⇑(continuous_multilinear_curry_left_equiv 𝕜 (fun (i : fin (n + 1)) => E) F) ∘ fderiv 𝕜 (iterated_fderiv 𝕜 n f) := rfl theorem iterated_fderiv_within_univ {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : ℕ} : iterated_fderiv_within 𝕜 n f set.univ = iterated_fderiv 𝕜 n f := sorry theorem ftaylor_series_within_univ {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : ftaylor_series_within 𝕜 f set.univ = ftaylor_series 𝕜 f := sorry theorem iterated_fderiv_succ_apply_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) : coe_fn (iterated_fderiv 𝕜 (n + 1) f x) m = coe_fn (coe_fn (iterated_fderiv 𝕜 n (fun (y : E) => fderiv 𝕜 f y) x) (fin.init m)) (m (fin.last n)) := sorry /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ theorem iterated_fderiv_succ_eq_comp_right {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f x = function.comp (⇑(continuous_multilinear_curry_right_equiv' 𝕜 n E F)) (iterated_fderiv 𝕜 n fun (y : E) => fderiv 𝕜 f y) x := sorry @[simp] theorem iterated_fderiv_one_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} (m : fin 1 → E) : coe_fn (iterated_fderiv 𝕜 1 f x) m = coe_fn (fderiv 𝕜 f x) (m 0) := sorry /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on_iff_ftaylor_series {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) := sorry theorem times_cont_diff_iff_continuous_differentiable {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ (∀ (m : ℕ), ↑m ≤ n → continuous fun (x : E) => iterated_fderiv 𝕜 m f x) ∧ ∀ (m : ℕ), ↑m < n → differentiable 𝕜 fun (x : E) => iterated_fderiv 𝕜 m f x := sorry theorem times_cont_diff_of_differentiable_iterated_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (h : ∀ (m : ℕ), ↑m ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) : times_cont_diff 𝕜 n f := iff.mpr times_cont_diff_iff_continuous_differentiable { left := fun (m : ℕ) (hm : ↑m ≤ n) => differentiable.continuous (h m hm), right := fun (m : ℕ) (hm : ↑m < n) => h m (le_of_lt hm) } /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : ℕ} : times_cont_diff 𝕜 (↑(n + 1)) f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ↑n fun (y : E) => fderiv 𝕜 f y := sorry /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_top_iff_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} : times_cont_diff 𝕜 ⊤ f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ⊤ fun (y : E) => fderiv 𝕜 f y := sorry theorem times_cont_diff.continuous_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous fun (x : E) => fderiv 𝕜 f x := times_cont_diff.continuous (and.right (iff.mp times_cont_diff_succ_iff_fderiv (times_cont_diff.of_le h hn))) /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ theorem times_cont_diff.continuous_fderiv_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous fun (p : E × E) => coe_fn (fderiv 𝕜 f (prod.fst p)) (prod.snd p) := continuous.comp (is_bounded_bilinear_map.continuous is_bounded_bilinear_map_apply) (continuous.prod_mk (continuous.comp (times_cont_diff.continuous_fderiv h hn) continuous_fst) continuous_snd) /-! ### Constants -/ theorem iterated_fderiv_within_zero_fun {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : ℕ} : (iterated_fderiv 𝕜 n fun (x : E) => 0) = 0 := sorry theorem times_cont_diff_zero_fun {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (x : E) => 0 := sorry /-- Constants are `C^∞`. -/ theorem times_cont_diff_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {c : F} : times_cont_diff 𝕜 n fun (x : E) => c := sorry theorem times_cont_diff_on_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {c : F} {s : set E} : times_cont_diff_on 𝕜 n (fun (x : E) => c) s := times_cont_diff.times_cont_diff_on times_cont_diff_const theorem times_cont_diff_at_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {c : F} : times_cont_diff_at 𝕜 n (fun (x : E) => c) x := times_cont_diff.times_cont_diff_at times_cont_diff_const theorem times_cont_diff_within_at_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {x : E} {n : with_top ℕ} {c : F} : times_cont_diff_within_at 𝕜 n (fun (x : E) => c) s x := times_cont_diff_at.times_cont_diff_within_at times_cont_diff_at_const theorem times_cont_diff_of_subsingleton {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} [subsingleton F] {n : with_top ℕ} : times_cont_diff 𝕜 n f := eq.mpr (id (Eq._oldrec (Eq.refl (times_cont_diff 𝕜 n f)) (subsingleton.elim f fun (_x : E) => 0))) times_cont_diff_const theorem times_cont_diff_at_of_subsingleton {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} [subsingleton F] {n : with_top ℕ} : times_cont_diff_at 𝕜 n f x := eq.mpr (id (Eq._oldrec (Eq.refl (times_cont_diff_at 𝕜 n f x)) (subsingleton.elim f fun (_x : E) => 0))) times_cont_diff_at_const theorem times_cont_diff_within_at_of_subsingleton {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} [subsingleton F] {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n f s x := eq.mpr (id (Eq._oldrec (Eq.refl (times_cont_diff_within_at 𝕜 n f s x)) (subsingleton.elim f fun (_x : E) => 0))) times_cont_diff_within_at_const theorem times_cont_diff_on_of_subsingleton {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} [subsingleton F] {n : with_top ℕ} : times_cont_diff_on 𝕜 n f s := eq.mpr (id (Eq._oldrec (Eq.refl (times_cont_diff_on 𝕜 n f s)) (subsingleton.elim f fun (_x : E) => 0))) times_cont_diff_on_const /-! ### Linear functions -/ /-- Unbundled bounded linear functions are `C^∞`. -/ theorem is_bounded_linear_map.times_cont_diff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {n : with_top ℕ} (hf : is_bounded_linear_map 𝕜 f) : times_cont_diff 𝕜 n f := sorry theorem continuous_linear_map.times_cont_diff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} (f : continuous_linear_map 𝕜 E F) : times_cont_diff 𝕜 n ⇑f := is_bounded_linear_map.times_cont_diff (continuous_linear_map.is_bounded_linear_map f) /-- The first projection in a product is `C^∞`. -/ theorem times_cont_diff_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n prod.fst := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst /-- The first projection on a domain in a product is `C^∞`. -/ theorem times_cont_diff_on_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set (E × F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n prod.fst s := times_cont_diff.times_cont_diff_on times_cont_diff_fst /-- The first projection at a point in a product is `C^∞`. -/ theorem times_cont_diff_at_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n prod.fst p := times_cont_diff.times_cont_diff_at times_cont_diff_fst /-- The first projection within a domain at a point in a product is `C^∞`. -/ theorem times_cont_diff_within_at_fst {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n prod.fst s p := times_cont_diff.times_cont_diff_within_at times_cont_diff_fst /-- The second projection in a product is `C^∞`. -/ theorem times_cont_diff_snd {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n prod.snd := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd /-- The second projection on a domain in a product is `C^∞`. -/ theorem times_cont_diff_on_snd {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set (E × F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n prod.snd s := times_cont_diff.times_cont_diff_on times_cont_diff_snd /-- The second projection at a point in a product is `C^∞`. -/ theorem times_cont_diff_at_snd {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n prod.snd p := times_cont_diff.times_cont_diff_at times_cont_diff_snd /-- The second projection within a domain at a point in a product is `C^∞`. -/ theorem times_cont_diff_within_at_snd {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n prod.snd s p := times_cont_diff.times_cont_diff_within_at times_cont_diff_snd /-- The identity is `C^∞`. -/ theorem times_cont_diff_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} : times_cont_diff 𝕜 n id := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.id theorem times_cont_diff_within_at_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} {x : E} : times_cont_diff_within_at 𝕜 n id s x := times_cont_diff.times_cont_diff_within_at times_cont_diff_id theorem times_cont_diff_at_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {x : E} : times_cont_diff_at 𝕜 n id x := times_cont_diff.times_cont_diff_at times_cont_diff_id theorem times_cont_diff_on_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} : times_cont_diff_on 𝕜 n id s := times_cont_diff.times_cont_diff_on times_cont_diff_id /-- Bilinear functions are `C^∞`. -/ theorem is_bounded_bilinear_map.times_cont_diff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {b : E × F → G} {n : with_top ℕ} (hb : is_bounded_bilinear_map 𝕜 b) : times_cont_diff 𝕜 n b := sorry /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ theorem has_ftaylor_series_up_to_on.continuous_linear_map_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : has_ftaylor_series_up_to_on n f p s) : has_ftaylor_series_up_to_on n (⇑g ∘ f) (fun (x : E) (k : ℕ) => continuous_linear_map.comp_continuous_multilinear_map g (p x k)) s := sorry /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ theorem times_cont_diff_within_at.continuous_linear_map_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (⇑g ∘ f) s x := sorry /-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain at a point. -/ theorem times_cont_diff_at.continuous_linear_map_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {f : E → F} {x : E} {n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (⇑g ∘ f) x := times_cont_diff_within_at.continuous_linear_map_comp g hf /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ theorem times_cont_diff_on.continuous_linear_map_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (⇑g ∘ f) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.continuous_linear_map_comp g (hf x hx) /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ theorem times_cont_diff.continuous_linear_map_comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {f : E → F} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n fun (x : E) => coe_fn g (f x) := iff.mp times_cont_diff_on_univ (times_cont_diff_on.continuous_linear_map_comp g (iff.mpr times_cont_diff_on_univ hf)) /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ theorem continuous_linear_equiv.comp_times_cont_diff_within_at_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 F G) : times_cont_diff_within_at 𝕜 n (⇑e ∘ f) s x ↔ times_cont_diff_within_at 𝕜 n f s x := sorry /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ theorem continuous_linear_equiv.comp_times_cont_diff_on_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 F G) : times_cont_diff_on 𝕜 n (⇑e ∘ f) s ↔ times_cont_diff_on 𝕜 n f s := sorry /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ theorem has_ftaylor_series_up_to_on.comp_continuous_linear_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (g : continuous_linear_map 𝕜 G E) : has_ftaylor_series_up_to_on n (f ∘ ⇑g) (fun (x : G) (k : ℕ) => continuous_multilinear_map.comp_continuous_linear_map (p (coe_fn g x) k) fun (_x : fin k) => g) (⇑g ⁻¹' s) := sorry /-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on a domain. -/ theorem times_cont_diff_within_at.comp_continuous_linear_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {n : with_top ℕ} {x : G} (g : continuous_linear_map 𝕜 G E) (hf : times_cont_diff_within_at 𝕜 n f s (coe_fn g x)) : times_cont_diff_within_at 𝕜 n (f ∘ ⇑g) (⇑g ⁻¹' s) x := sorry /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ theorem times_cont_diff_on.comp_continuous_linear_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (g : continuous_linear_map 𝕜 G E) : times_cont_diff_on 𝕜 n (f ∘ ⇑g) (⇑g ⁻¹' s) := fun (x : G) (hx : x ∈ ⇑g ⁻¹' s) => times_cont_diff_within_at.comp_continuous_linear_map g (hf (coe_fn g x) hx) /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ theorem times_cont_diff.comp_continuous_linear_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {f : E → F} {g : continuous_linear_map 𝕜 G E} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (f ∘ ⇑g) := iff.mp times_cont_diff_on_univ (times_cont_diff_on.comp_continuous_linear_map (iff.mpr times_cont_diff_on_univ hf) g) /-- Composition by continuous linear equivs on the right respects higher differentiability at a point in a domain. -/ theorem continuous_linear_equiv.times_cont_diff_within_at_comp_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 G E) : times_cont_diff_within_at 𝕜 n (f ∘ ⇑e) (⇑e ⁻¹' s) (coe_fn (continuous_linear_equiv.symm e) x) ↔ times_cont_diff_within_at 𝕜 n f s x := sorry /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ theorem continuous_linear_equiv.times_cont_diff_on_comp_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 G E) : times_cont_diff_on 𝕜 n (f ∘ ⇑e) (⇑e ⁻¹' s) ↔ times_cont_diff_on 𝕜 n f s := sorry /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ theorem has_ftaylor_series_up_to_on.prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) : has_ftaylor_series_up_to_on n (fun (y : E) => (f y, g y)) (fun (y : E) (k : ℕ) => continuous_multilinear_map.prod (p y k) (q y k)) s := sorry /-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/ theorem times_cont_diff_within_at.prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {x : E} {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => (f x, g x)) s x := sorry /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ theorem times_cont_diff_on.prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => (f x, g x)) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.prod (hf x hx) (hg x hx) /-- The cartesian product of `C^n` functions at a point is `C^n`. -/ theorem times_cont_diff_at.prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {x : E} {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => (f x, g x)) x := iff.mp times_cont_diff_within_at_univ (times_cont_diff_within_at.prod (iff.mpr times_cont_diff_within_at_univ hf) (iff.mpr times_cont_diff_within_at_univ hg)) /-- The cartesian product of `C^n` functions is `C^n`. -/ theorem times_cont_diff.prod {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => (f x, g x) := iff.mp times_cont_diff_on_univ (times_cont_diff_on.prod (iff.mpr times_cont_diff_on_univ hf) (iff.mpr times_cont_diff_on_univ hg)) /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity, but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u` and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f` belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above proof would work fine, but this is not the case. One could still write the proof considering spaces in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same universe (where everything is fine), and then we deduce the general result by lifting all our spaces to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level, and then argue that composing with such a linear equiv does not change the fact of being `C^n`, which we have already proved previously. -/ /-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all spaces live in the same universe. Use instead `times_cont_diff_on.comp` which removes the universe assumption (but is deduced from this one). -/ /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem times_cont_diff_on.comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := sorry /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem times_cont_diff_on.comp' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) := times_cont_diff_on.comp hg (times_cont_diff_on.mono hf (set.inter_subset_left s (f ⁻¹' t))) (set.inter_subset_right s (f ⁻¹' t)) /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ theorem times_cont_diff.comp_times_cont_diff_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := times_cont_diff_on.comp (iff.mpr times_cont_diff_on_univ hg) hf set.subset_preimage_univ /-- The composition of `C^n` functions is `C^n`. -/ theorem times_cont_diff.comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (g ∘ f) := iff.mp times_cont_diff_on_univ (times_cont_diff_on.comp (iff.mpr times_cont_diff_on_univ hg) (iff.mpr times_cont_diff_on_univ hf) (set.subset_univ set.univ)) /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem times_cont_diff_within_at.comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) (st : s ⊆ f ⁻¹' t) : times_cont_diff_within_at 𝕜 n (g ∘ f) s x := sorry /-- The composition of `C^n` functions at points in domains is `C^n`. -/ theorem times_cont_diff_within_at.comp' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) x := times_cont_diff_within_at.comp x hg (times_cont_diff_within_at.mono hf (set.inter_subset_left s (f ⁻¹' t))) (set.inter_subset_right s (f ⁻¹' t)) theorem times_cont_diff_at.comp_times_cont_diff_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F} {g : F → G} {n : with_top ℕ} (x : E) (hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (g ∘ f) s x := times_cont_diff_within_at.comp x hg hf (set.maps_to_univ (fun (a : E) => a) s) /-- The composition of `C^n` functions at points is `C^n`. -/ theorem times_cont_diff_at.comp {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {f : E → F} {g : F → G} {n : with_top ℕ} (x : E) (hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (g ∘ f) x := times_cont_diff_within_at.comp x hg hf set.subset_preimage_univ theorem times_cont_diff.comp_times_cont_diff_within_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {t : set E} {x : E} {n : with_top ℕ} {g : F → G} {f : E → F} (h : times_cont_diff 𝕜 n g) (hf : times_cont_diff_within_at 𝕜 n f t x) : times_cont_diff_within_at 𝕜 n (g ∘ f) t x := times_cont_diff_within_at.comp x (times_cont_diff_at.times_cont_diff_within_at (times_cont_diff.times_cont_diff_at h)) hf (set.subset_univ t) theorem times_cont_diff.comp_times_cont_diff_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {g : F → G} {f : E → F} (x : E) (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (g ∘ f) x := times_cont_diff.comp_times_cont_diff_within_at hg hf /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ theorem times_cont_diff_on_fderiv_within_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {m : with_top ℕ} {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (fun (p : E × E) => coe_fn (fderiv_within 𝕜 f s (prod.fst p)) (prod.snd p)) (set.prod s set.univ) := sorry /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ theorem times_cont_diff.times_cont_diff_fderiv_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {m : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) : times_cont_diff 𝕜 m fun (p : E × E) => coe_fn (fderiv 𝕜 f (prod.fst p)) (prod.snd p) := sorry /-! ### Sum of two functions -/ /- The sum is smooth. -/ theorem times_cont_diff_add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (p : F × F) => prod.fst p + prod.snd p := is_bounded_linear_map.times_cont_diff (is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd) /-- The sum of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ theorem times_cont_diff_within_at.add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x + g x) s x := times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_add) (times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ /-- The sum of two `C^n` functions at a point is `C^n` at this point. -/ theorem times_cont_diff_at.add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x + g x) x := sorry /-- The sum of two `C^n`functions is `C^n`. -/ theorem times_cont_diff.add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x + g x := times_cont_diff.comp times_cont_diff_add (times_cont_diff.prod hf hg) /-- The sum of two `C^n` functions on a domain is `C^n`. -/ theorem times_cont_diff_on.add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x + g x) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.add (hf x hx) (hg x hx) /-! ### Negative -/ /- The negative is smooth. -/ theorem times_cont_diff_neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (p : F) => -p := is_bounded_linear_map.times_cont_diff (is_bounded_linear_map.neg is_bounded_linear_map.id) /-- The negative of a `C^n` function within a domain at a point is `C^n` within this domain at this point. -/ theorem times_cont_diff_within_at.neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => -f x) s x := times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_neg) hf set.subset_preimage_univ /-- The negative of a `C^n` function at a point is `C^n` at this point. -/ theorem times_cont_diff_at.neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {f : E → F} (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (fun (x : E) => -f x) x := sorry /-- The negative of a `C^n`function is `C^n`. -/ theorem times_cont_diff.neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n fun (x : E) => -f x := times_cont_diff.comp times_cont_diff_neg hf /-- The negative of a `C^n` function on a domain is `C^n`. -/ theorem times_cont_diff_on.neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (fun (x : E) => -f x) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.neg (hf x hx) /-! ### Subtraction -/ /-- The difference of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ theorem times_cont_diff_within_at.sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x - g x) s x := sorry /-- The difference of two `C^n` functions at a point is `C^n` at this point. -/ theorem times_cont_diff_at.sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x - g x) x := sorry /-- The difference of two `C^n` functions on a domain is `C^n`. -/ theorem times_cont_diff_on.sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x - g x) s := sorry /-- The difference of two `C^n` functions is `C^n`. -/ theorem times_cont_diff.sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x - g x := sorry /-! ### Sum of finitely many functions -/ theorem times_cont_diff_within_at.sum {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E} {x : E} (h : ∀ (i : ι), i ∈ s → times_cont_diff_within_at 𝕜 n (fun (x : E) => f i x) t x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) t x := sorry theorem times_cont_diff_at.sum {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {x : E} (h : ∀ (i : ι), i ∈ s → times_cont_diff_at 𝕜 n (fun (x : E) => f i x) x) : times_cont_diff_at 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) x := sorry theorem times_cont_diff_on.sum {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E} (h : ∀ (i : ι), i ∈ s → times_cont_diff_on 𝕜 n (fun (x : E) => f i x) t) : times_cont_diff_on 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) t := fun (x : E) (hx : x ∈ t) => times_cont_diff_within_at.sum fun (i : ι) (hi : i ∈ s) => h i hi x hx theorem times_cont_diff.sum {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} (h : ∀ (i : ι), i ∈ s → times_cont_diff 𝕜 n fun (x : E) => f i x) : times_cont_diff 𝕜 n fun (x : E) => finset.sum s fun (i : ι) => f i x := sorry /-! ### Product of two functions -/ /- The product is smooth. -/ theorem times_cont_diff_mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (p : 𝕜 × 𝕜) => prod.fst p * prod.snd p := is_bounded_bilinear_map.times_cont_diff is_bounded_bilinear_map_mul /-- The product of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ theorem times_cont_diff_within_at.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {x : E} {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x * g x) s x := times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_mul) (times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ /-- The product of two `C^n` functions at a point is `C^n` at this point. -/ theorem times_cont_diff_at.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {x : E} {n : with_top ℕ} {f : E → 𝕜} {g : E → 𝕜} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x * g x) x := sorry /-- The product of two `C^n` functions on a domain is `C^n`. -/ theorem times_cont_diff_on.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → 𝕜} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x * g x) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.mul (hf x hx) (hg x hx) /-- The product of two `C^n`functions is `C^n`. -/ theorem times_cont_diff.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {f : E → 𝕜} {g : E → 𝕜} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x * g x := times_cont_diff.comp times_cont_diff_mul (times_cont_diff.prod hf hg) theorem times_cont_diff_within_at.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x / c) s x := times_cont_diff_within_at.mul hf times_cont_diff_within_at_const theorem times_cont_diff_at.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {x : E} {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜} (hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x / c) x := times_cont_diff_at.mul hf times_cont_diff_at_const theorem times_cont_diff_on.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {s : set E} {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x / c) s := times_cont_diff_on.mul hf times_cont_diff_on_const theorem times_cont_diff.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n fun (x : E) => f x / c := times_cont_diff.mul hf times_cont_diff_const theorem times_cont_diff.pow {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff 𝕜 n f) (m : ℕ) : times_cont_diff 𝕜 n fun (x : E) => f x ^ m := sorry /-! ### Scalar multiplication -/ /- The scalar multiplication is smooth. -/ theorem times_cont_diff_smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (p : 𝕜 × F) => prod.fst p • prod.snd p := is_bounded_bilinear_map.times_cont_diff is_bounded_bilinear_map_smul /-- The scalar multiplication of two `C^n` functions within a set at a point is `C^n` within this set at this point. -/ theorem times_cont_diff_within_at.smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x • g x) s x := times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_smul) (times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ /-- The scalar multiplication of two `C^n` functions at a point is `C^n` at this point. -/ theorem times_cont_diff_at.smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {x : E} {n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x • g x) x := sorry /-- The scalar multiplication of two `C^n` functions is `C^n`. -/ theorem times_cont_diff.smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x • g x := times_cont_diff.comp times_cont_diff_smul (times_cont_diff.prod hf hg) /-- The scalar multiplication of two `C^n` functions on a domain is `C^n`. -/ theorem times_cont_diff_on.smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x • g x) s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.smul (hf x hx) (hg x hx) /-! ### Cartesian product of two functions-/ /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ theorem times_cont_diff_within_at.prod_map' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {p : E × E'} (hf : times_cont_diff_within_at 𝕜 n f s (prod.fst p)) (hg : times_cont_diff_within_at 𝕜 n g t (prod.snd p)) : times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) p := times_cont_diff_within_at.prod (times_cont_diff_within_at.comp p hf times_cont_diff_within_at_fst (set.prod_subset_preimage_fst s t)) (times_cont_diff_within_at.comp p hg times_cont_diff_within_at_snd (set.prod_subset_preimage_snd s t)) theorem times_cont_diff_within_at.prod_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {s : set E} {t : set E'} {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g t y) : times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) (x, y) := times_cont_diff_within_at.prod_map' hf hg /-- The product map of two `C^n` functions on a set is `C^n` on the product set. -/ theorem times_cont_diff_on.prod_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_4} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_5} [normed_group F'] [normed_space 𝕜 F'] {s : set E} {t : set E'} {n : with_top ℕ} {f : E → F} {g : E' → F'} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g t) : times_cont_diff_on 𝕜 n (prod.map f g) (set.prod s t) := times_cont_diff_on.prod (times_cont_diff_on.comp hf times_cont_diff_on_fst (set.prod_subset_preimage_fst s t)) (times_cont_diff_on.comp hg times_cont_diff_on_snd (set.prod_subset_preimage_snd s t)) /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ theorem times_cont_diff_at.prod_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} {x : E} {y : E'} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g y) : times_cont_diff_at 𝕜 n (prod.map f g) (x, y) := sorry /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ theorem times_cont_diff_at.prod_map' {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} {p : E × E'} (hf : times_cont_diff_at 𝕜 n f (prod.fst p)) (hg : times_cont_diff_at 𝕜 n g (prod.snd p)) : times_cont_diff_at 𝕜 n (prod.map f g) p := sorry /-- The product map of two `C^n` functions is `C^n`. -/ theorem times_cont_diff.prod_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (prod.map f g) := sorry /-! ### Inversion in a complete normed algebra -/ /-- In a complete normed algebra, the operation of inversion is `C^n`, for all `n`, at each invertible element. The proof is by induction, bootstrapping using an identity expressing the derivative of inversion as a bilinear map of inversion itself. -/ theorem times_cont_diff_at_ring_inverse (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {R : Type u_5} [normed_ring R] [normed_algebra 𝕜 R] [complete_space R] {n : with_top ℕ} (x : units R) : times_cont_diff_at 𝕜 n ring.inverse ↑x := sorry theorem times_cont_diff_at_inv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {𝕜' : Type u_6} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {x : 𝕜'} (hx : x ≠ 0) {n : with_top ℕ} : times_cont_diff_at 𝕜 n has_inv.inv x := sorry theorem times_cont_diff_on_inv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {𝕜' : Type u_6} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {n : with_top ℕ} : times_cont_diff_on 𝕜 n has_inv.inv (singleton 0ᶜ) := fun (x : 𝕜') (hx : x ∈ (singleton 0ᶜ)) => times_cont_diff_at.times_cont_diff_within_at (times_cont_diff_at_inv 𝕜 hx) -- TODO: the next few lemmas don't need `𝕜` or `𝕜'` to be complete -- A good way to show this is to generalize `times_cont_diff_at_ring_inverse` to the setting -- of a function `f` such that `∀ᶠ x in 𝓝 a, x * f x = 1`. theorem times_cont_diff_within_at.inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} {𝕜' : Type u_6} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {f : E → 𝕜'} {n : with_top ℕ} (hf : times_cont_diff_within_at 𝕜 n f s x) (hx : f x ≠ 0) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x⁻¹) s x := times_cont_diff_at.comp_times_cont_diff_within_at x (times_cont_diff_at_inv 𝕜 hx) hf theorem times_cont_diff_at.inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {x : E} {𝕜' : Type u_6} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {f : E → 𝕜'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕜 n f x) (hx : f x ≠ 0) : times_cont_diff_at 𝕜 n (fun (x : E) => f x⁻¹) x := times_cont_diff_within_at.inv hf hx -- TODO: generalize to `f g : E → 𝕜'` theorem times_cont_diff_within_at.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} [complete_space 𝕜] {f : E → 𝕜} {g : E → 𝕜} {n : with_top ℕ} (hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) (hx : g x ≠ 0) : times_cont_diff_within_at 𝕜 n (fun (x : E) => f x / g x) s x := times_cont_diff_within_at.mul hf (times_cont_diff_within_at.inv hg hx) theorem times_cont_diff_at.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {x : E} [complete_space 𝕜] {f : E → 𝕜} {g : E → 𝕜} {n : with_top ℕ} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) (hx : g x ≠ 0) : times_cont_diff_at 𝕜 n (fun (x : E) => f x / g x) x := times_cont_diff_within_at.div hf hg hx theorem times_cont_diff.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] [complete_space 𝕜] {f : E → 𝕜} {g : E → 𝕜} {n : with_top ℕ} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) (h0 : ∀ (x : E), g x ≠ 0) : times_cont_diff 𝕜 n fun (x : E) => f x / g x := sorry /-! ### Inversion of continuous linear maps between Banach spaces -/ /-- At a continuous linear equivalence `e : E ≃L[𝕜] F` between Banach spaces, the operation of inversion is `C^n`, for all `n`. -/ theorem times_cont_diff_at_map_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space E] {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 E F) : times_cont_diff_at 𝕜 n continuous_linear_map.inverse ↑e := sorry /-- If `f` is a local homeomorphism and the point `a` is in its target, and if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is a continuous linear equivalence, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.times_cont_diff_at_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space E] {n : with_top ℕ} (f : local_homeomorph E F) {f₀' : continuous_linear_equiv 𝕜 E F} {a : F} (ha : a ∈ local_equiv.target (local_homeomorph.to_local_equiv f)) (hf₀' : has_fderiv_at (⇑f) (↑f₀') (coe_fn (local_homeomorph.symm f) a)) (hf : times_cont_diff_at 𝕜 n (⇑f) (coe_fn (local_homeomorph.symm f) a)) : times_cont_diff_at 𝕜 n (⇑(local_homeomorph.symm f)) a := sorry /-- Let `f` be a local homeomorphism of a nondiscrete normed field, let `a` be a point in its target. if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is nonzero, then `f.symm` is `n` times continuously differentiable at the point `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem local_homeomorph.times_cont_diff_at_symm_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {n : with_top ℕ} (f : local_homeomorph 𝕜 𝕜) {f₀' : 𝕜} {a : 𝕜} (h₀ : f₀' ≠ 0) (ha : a ∈ local_equiv.target (local_homeomorph.to_local_equiv f)) (hf₀' : has_deriv_at (⇑f) f₀' (coe_fn (local_homeomorph.symm f) a)) (hf : times_cont_diff_at 𝕜 n (⇑f) (coe_fn (local_homeomorph.symm f) a)) : times_cont_diff_at 𝕜 n (⇑(local_homeomorph.symm f)) a := local_homeomorph.times_cont_diff_at_symm f ha (has_deriv_at.has_fderiv_at_equiv hf₀' h₀) hf /-! ### Results over `ℝ` or `ℂ` The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and its extension fields such as `ℂ`). -/ /-- If a function has a Taylor series at order at least 1, then at points in the interior of the domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/ theorem has_ftaylor_series_up_to_on.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F'] {s : set E'} {f : E' → F'} {x : E'} {p : E' → formal_multilinear_series 𝕂 E' F'} {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ nhds x) : has_strict_fderiv_at f (coe_fn (continuous_multilinear_curry_fin1 𝕂 E' F') (p x 1)) x := sorry /-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point is also a strict derivative. -/ theorem times_cont_diff_at.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F'] {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := sorry /-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to us as `f'`, then `f'` is also a strict derivative. -/ theorem times_cont_diff_at.has_strict_fderiv_at' {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F'] {f : E' → F'} {f' : continuous_linear_map 𝕂 E' F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hf' : has_fderiv_at f f' x) (hn : 1 ≤ n) : has_strict_fderiv_at f f' x := sorry /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ theorem times_cont_diff.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F'] {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff 𝕂 n f) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv 𝕂 f x) x := times_cont_diff_at.has_strict_fderiv_at (times_cont_diff.times_cont_diff_at hf) hn /-! ### One dimension All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this paragraph, we reformulate some higher smoothness results in terms of `deriv`. -/ /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : ℕ} (hs : unique_diff_on 𝕜 s₂) : times_cont_diff_on 𝕜 (↑(n + 1)) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 (↑n) (deriv_within f₂ s₂) s₂ := sorry /-- A function is `C^(n + 1)` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/ theorem times_cont_diff_on_succ_iff_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : ℕ} (hs : is_open s₂) : times_cont_diff_on 𝕜 (↑(n + 1)) f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 (↑n) (deriv f₂) s₂ := sorry /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative (formulated with `deriv_within`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} (hs : unique_diff_on 𝕜 s₂) : times_cont_diff_on 𝕜 ⊤ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ⊤ (deriv_within f₂ s₂) s₂ := sorry /-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its derivative (formulated with `deriv`) is `C^∞`. -/ theorem times_cont_diff_on_top_iff_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} (hs : is_open s₂) : times_cont_diff_on 𝕜 ⊤ f₂ s₂ ↔ differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ⊤ (deriv f₂) s₂ := sorry theorem times_cont_diff_on.deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (deriv_within f₂ s₂) s₂ := sorry theorem times_cont_diff_on.deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (deriv f₂) s₂ := times_cont_diff_on.congr (times_cont_diff_on.deriv_within hf (is_open.unique_diff_on hs) hmn) fun (x : 𝕜) (hx : x ∈ s₂) => Eq.symm (deriv_within_of_open hs hx) theorem times_cont_diff_on.continuous_on_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hn : 1 ≤ n) : continuous_on (deriv_within f₂ s₂) s₂ := times_cont_diff_on.continuous_on (and.right (iff.mp (times_cont_diff_on_succ_iff_deriv_within hs) (times_cont_diff_on.of_le h hn))) theorem times_cont_diff_on.continuous_on_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hn : 1 ≤ n) : continuous_on (deriv f₂) s₂ := times_cont_diff_on.continuous_on (and.right (iff.mp (times_cont_diff_on_succ_iff_deriv_of_open hs) (times_cont_diff_on.of_le h hn))) /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {n : ℕ} : times_cont_diff 𝕜 (↑(n + 1)) f₂ ↔ differentiable 𝕜 f₂ ∧ times_cont_diff 𝕜 (↑n) (deriv f₂) := sorry /-! ### Restricting from `ℂ` to `ℝ`, or generally from `𝕜'` to `𝕜` If a function is `n` times continuously differentiable over `ℂ`, then it is `n` times continuously differentiable over `ℝ`. In this paragraph, we give variants of this statement, in the general situation where `ℂ` and `ℝ` are replaced respectively by `𝕜'` and `𝕜` where `𝕜'` is a normed algebra over `𝕜`. -/ theorem has_ftaylor_series_up_to_on.restrict_scalars (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] {p' : E → formal_multilinear_series 𝕜' E F} {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p' s) : has_ftaylor_series_up_to_on n f (fun (x : E) => formal_multilinear_series.restrict_scalars 𝕜 (p' x)) s := sorry theorem times_cont_diff_within_at.restrict_scalars (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {x : E} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜' n f s x) : times_cont_diff_within_at 𝕜 n f s x := sorry theorem times_cont_diff_on.restrict_scalars (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {s : set E} {f : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_on 𝕜' n f s) : times_cont_diff_on 𝕜 n f s := fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.restrict_scalars 𝕜 (h x hx) theorem times_cont_diff_at.restrict_scalars (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {x : E} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_at 𝕜' n f x) : times_cont_diff_at 𝕜 n f x := iff.mp times_cont_diff_within_at_univ (times_cont_diff_within_at.restrict_scalars 𝕜 (times_cont_diff_at.times_cont_diff_within_at h)) theorem times_cont_diff.restrict_scalars (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff 𝕜' n f) : times_cont_diff 𝕜 n f := iff.mpr times_cont_diff_iff_times_cont_diff_at fun (x : E) => times_cont_diff_at.restrict_scalars 𝕜 (times_cont_diff.times_cont_diff_at h)
bb8234aa2ed3ec39afdcbcdb3ad35d4071d65687
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/analytic/inverse.lean
ca347a5d6dcd64bb5ce7f6a1ce9b874829ef9cc3
[ "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
27,120
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.analytic.composition /-! # Inverse of analytic functions We construct the left and right inverse of a formal multilinear series with invertible linear term, we prove that they coincide and study their properties (notably convergence). ## Main statements * `p.left_inv i`: the formal left inverse of the formal multilinear series `p`, for `i : E ≃L[𝕜] F` which coincides with `p₁`. * `p.right_inv i`: the formal right inverse of the formal multilinear series `p`, for `i : E ≃L[𝕜] F` which coincides with `p₁`. * `p.left_inv_comp` says that `p.left_inv i` is indeed a left inverse to `p` when `p₁ = i`. * `p.right_inv_comp` says that `p.right_inv i` is indeed a right inverse to `p` when `p₁ = i`. * `p.left_inv_eq_right_inv`: the two inverses coincide. * `p.radius_right_inv_pos_of_radius_pos`: if a power series has a positive radius of convergence, then so does its inverse. -/ open_locale big_operators classical topological_space open finset filter namespace formal_multilinear_series variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] /-! ### The left inverse of a formal multilinear series -/ /-- The left inverse of a formal multilinear series, where the `n`-th term is defined inductively in terms of the previous ones to make sure that `(left_inv p i) ∘ p = id`. For this, the linear term `p₁` in `p` should be invertible. In the definition, `i` is a linear isomorphism that should coincide with `p₁`, so that one can use its inverse in the construction. The definition does not use that `i = p₁`, but proofs that the definition is well-behaved do. The `n`-th term in `q ∘ p` is `∑ qₖ (p_{j₁}, ..., p_{jₖ})` over `j₁ + ... + jₖ = n`. In this expression, `qₙ` appears only once, in `qₙ (p₁, ..., p₁)`. We adjust the definition so that this term compensates the rest of the sum, using `i⁻¹` as an inverse to `p₁`. These formulas only make sense when the constant term `p₀` vanishes. The definition we give is general, but it ignores the value of `p₀`. -/ noncomputable def left_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : formal_multilinear_series 𝕜 F E | 0 := 0 | 1 := (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm | (n+2) := - ∑ c : {c : composition (n+2) // c.length < n + 2}, have (c : composition (n+2)).length < n+2 := c.2, (left_inv (c : composition (n+2)).length).comp_along_composition (p.comp_continuous_linear_map i.symm) c @[simp] lemma left_inv_coeff_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.left_inv i 0 = 0 := by rw left_inv @[simp] lemma left_inv_coeff_one (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.left_inv i 1 = (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm := by rw left_inv /-- The left inverse does not depend on the zeroth coefficient of a formal multilinear series. -/ lemma left_inv_remove_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.remove_zero.left_inv i = p.left_inv i := begin ext1 n, induction n using nat.strong_rec' with n IH, cases n, { simp }, -- if one replaces `simp` with `refl`, the proof times out in the kernel. cases n, { simp }, -- TODO: why? simp only [left_inv, neg_inj], refine finset.sum_congr rfl (λ c cuniv, _), rcases c with ⟨c, hc⟩, ext v, dsimp, simp [IH _ hc], end /-- The left inverse to a formal multilinear series is indeed a left inverse, provided its linear term is invertible. -/ lemma left_inv_comp (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) : (left_inv p i).comp p = id 𝕜 E := begin ext n v, cases n, { simp only [left_inv, continuous_multilinear_map.zero_apply, id_apply_ne_one, ne.def, not_false_iff, zero_ne_one, comp_coeff_zero']}, cases n, { simp only [left_inv, comp_coeff_one, h, id_apply_one, continuous_linear_equiv.coe_apply, continuous_linear_equiv.symm_apply_apply, continuous_multilinear_curry_fin1_symm_apply] }, have A : (finset.univ : finset (composition (n+2))) = {c | composition.length c < n + 2}.to_finset ∪ {composition.ones (n+2)}, { refine subset.antisymm (λ c hc, _) (subset_univ _), by_cases h : c.length < n + 2, { simp [h] }, { simp [composition.eq_ones_iff_le_length.2 (not_lt.1 h)] } }, have B : disjoint ({c | composition.length c < n + 2} : set (composition (n + 2))).to_finset {composition.ones (n+2)}, by simp, have C : (p.left_inv i (composition.ones (n + 2)).length) (λ (j : fin (composition.ones n.succ.succ).length), p 1 (λ k, v ((fin.cast_le (composition.length_le _)) j))) = p.left_inv i (n+2) (λ (j : fin (n+2)), p 1 (λ k, v j)), { apply formal_multilinear_series.congr _ (composition.ones_length _) (λ j hj1 hj2, _), exact formal_multilinear_series.congr _ rfl (λ k hk1 hk2, by congr) }, have D : p.left_inv i (n+2) (λ (j : fin (n+2)), p 1 (λ k, v j)) = - ∑ (c : composition (n + 2)) in {c : composition (n + 2) | c.length < n + 2}.to_finset, (p.left_inv i c.length) (p.apply_composition c v), { simp only [left_inv, continuous_multilinear_map.neg_apply, neg_inj, continuous_multilinear_map.sum_apply], convert (sum_to_finset_eq_subtype (λ (c : composition (n+2)), c.length < n+2) (λ (c : composition (n+2)), (continuous_multilinear_map.comp_along_composition (p.comp_continuous_linear_map ↑(i.symm)) c (p.left_inv i c.length)) (λ (j : fin (n + 2)), p 1 (λ (k : fin 1), v j)))).symm.trans _, simp only [comp_continuous_linear_map_apply_composition, continuous_multilinear_map.comp_along_composition_apply], congr, ext c, congr, ext k, simp [h] }, simp [formal_multilinear_series.comp, show n + 2 ≠ 1, by dec_trivial, A, finset.sum_union B, apply_composition_ones, C, D], end /-! ### The right inverse of a formal multilinear series -/ /-- The right inverse of a formal multilinear series, where the `n`-th term is defined inductively in terms of the previous ones to make sure that `p ∘ (right_inv p i) = id`. For this, the linear term `p₁` in `p` should be invertible. In the definition, `i` is a linear isomorphism that should coincide with `p₁`, so that one can use its inverse in the construction. The definition does not use that `i = p₁`, but proofs that the definition is well-behaved do. The `n`-th term in `p ∘ q` is `∑ pₖ (q_{j₁}, ..., q_{jₖ})` over `j₁ + ... + jₖ = n`. In this expression, `qₙ` appears only once, in `p₁ (qₙ)`. We adjust the definition of `qₙ` so that this term compensates the rest of the sum, using `i⁻¹` as an inverse to `p₁`. These formulas only make sense when the constant term `p₀` vanishes. The definition we give is general, but it ignores the value of `p₀`. -/ noncomputable def right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : formal_multilinear_series 𝕜 F E | 0 := 0 | 1 := (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm | (n+2) := let q : formal_multilinear_series 𝕜 F E := λ k, if h : k < n + 2 then right_inv k else 0 in - (i.symm : F →L[𝕜] E).comp_continuous_multilinear_map ((p.comp q) (n+2)) @[simp] lemma right_inv_coeff_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.right_inv i 0 = 0 := by rw right_inv @[simp] lemma right_inv_coeff_one (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.right_inv i 1 = (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm := by rw right_inv /-- The right inverse does not depend on the zeroth coefficient of a formal multilinear series. -/ lemma right_inv_remove_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.remove_zero.right_inv i = p.right_inv i := begin ext1 n, induction n using nat.strong_rec' with n IH, cases n, { simp }, cases n, { simp }, simp only [right_inv, neg_inj], unfold_coes, congr' 1, rw remove_zero_comp_of_pos _ _ (show 0 < n+2, by dec_trivial), congr' 1, ext k, by_cases hk : k < n+2; simp [hk, IH] end lemma comp_right_inv_aux1 {n : ℕ} (hn : 0 < n) (p : formal_multilinear_series 𝕜 E F) (q : formal_multilinear_series 𝕜 F E) (v : fin n → F) : p.comp q n v = (∑ (c : composition n) in {c : composition n | 1 < c.length}.to_finset, p c.length (q.apply_composition c v)) + p 1 (λ i, q n v) := begin have A : (finset.univ : finset (composition n)) = {c | 1 < composition.length c}.to_finset ∪ {composition.single n hn}, { refine subset.antisymm (λ c hc, _) (subset_univ _), by_cases h : 1 < c.length, { simp [h] }, { have : c.length = 1, by { refine (eq_iff_le_not_lt.2 ⟨ _, h⟩).symm, exact c.length_pos_of_pos hn }, rw ← composition.eq_single_iff_length hn at this, simp [this] } }, have B : disjoint ({c | 1 < composition.length c} : set (composition n)).to_finset {composition.single n hn}, by simp, have C : p (composition.single n hn).length (q.apply_composition (composition.single n hn) v) = p 1 (λ (i : fin 1), q n v), { apply p.congr (composition.single_length hn) (λ j hj1 hj2, _), simp [apply_composition_single] }, simp [formal_multilinear_series.comp, A, finset.sum_union B, C], end lemma comp_right_inv_aux2 (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (n : ℕ) (v : fin (n + 2) → F) : ∑ (c : composition (n + 2)) in {c : composition (n + 2) | 1 < c.length}.to_finset, p c.length (apply_composition (λ (k : ℕ), ite (k < n + 2) (p.right_inv i k) 0) c v) = ∑ (c : composition (n + 2)) in {c : composition (n + 2) | 1 < c.length}.to_finset, p c.length ((p.right_inv i).apply_composition c v) := begin have N : 0 < n + 2, by dec_trivial, refine sum_congr rfl (λ c hc, p.congr rfl (λ j hj1 hj2, _)), have : ∀ k, c.blocks_fun k < n + 2, { simp only [set.mem_to_finset, set.mem_set_of_eq] at hc, simp [← composition.ne_single_iff N, composition.eq_single_iff_length, ne_of_gt hc] }, simp [apply_composition, this], end /-- The right inverse to a formal multilinear series is indeed a right inverse, provided its linear term is invertible and its constant term vanishes. -/ lemma comp_right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) (h0 : p 0 = 0) : p.comp (right_inv p i) = id 𝕜 F := begin ext n v, cases n, { simp only [h0, continuous_multilinear_map.zero_apply, id_apply_ne_one, ne.def, not_false_iff, zero_ne_one, comp_coeff_zero']}, cases n, { simp only [comp_coeff_one, h, right_inv, continuous_linear_equiv.apply_symm_apply, id_apply_one, continuous_linear_equiv.coe_apply, continuous_multilinear_curry_fin1_symm_apply] }, have N : 0 < n+2, by dec_trivial, simp [comp_right_inv_aux1 N, h, right_inv, lt_irrefl n, show n + 2 ≠ 1, by dec_trivial, ← sub_eq_add_neg, sub_eq_zero, comp_right_inv_aux2], end lemma right_inv_coeff (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (n : ℕ) (hn : 2 ≤ n) : p.right_inv i n = - (i.symm : F →L[𝕜] E).comp_continuous_multilinear_map (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition n)), p.comp_along_composition (p.right_inv i) c) := begin cases n, { exact false.elim (zero_lt_two.not_le hn) }, cases n, { exact false.elim (one_lt_two.not_le hn) }, simp only [right_inv, neg_inj], congr' 1, ext v, have N : 0 < n + 2, by dec_trivial, have : (p 1) (λ (i : fin 1), 0) = 0 := continuous_multilinear_map.map_zero _, simp [comp_right_inv_aux1 N, lt_irrefl n, this, comp_right_inv_aux2] end /-! ### Coincidence of the left and the right inverse -/ private lemma left_inv_eq_right_inv_aux (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) (h0 : p 0 = 0) : left_inv p i = right_inv p i := calc left_inv p i = (left_inv p i).comp (id 𝕜 F) : by simp ... = (left_inv p i).comp (p.comp (right_inv p i)) : by rw comp_right_inv p i h h0 ... = ((left_inv p i).comp p).comp (right_inv p i) : by rw comp_assoc ... = (id 𝕜 E).comp (right_inv p i) : by rw left_inv_comp p i h ... = right_inv p i : by simp /-- The left inverse and the right inverse of a formal multilinear series coincide. This is not at all obvious from their definition, but it follows from uniqueness of inverses (which comes from the fact that composition is associative on formal multilinear series). -/ theorem left_inv_eq_right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) : left_inv p i = right_inv p i := calc left_inv p i = left_inv p.remove_zero i : by rw left_inv_remove_zero ... = right_inv p.remove_zero i : by { apply left_inv_eq_right_inv_aux; simp [h] } ... = right_inv p i : by rw right_inv_remove_zero /-! ### Convergence of the inverse of a power series Assume that `p` is a convergent multilinear series, and let `q` be its (left or right) inverse. Using the left-inverse formula gives $$ q_n = - (p_1)^{-n} \sum_{k=0}^{n-1} \sum_{i_1 + \dotsc + i_k = n} q_k (p_{i_1}, \dotsc, p_{i_k}). $$ Assume for simplicity that we are in dimension `1` and `p₁ = 1`. In the formula for `qₙ`, the term `q_{n-1}` appears with a multiplicity of `n-1` (choosing the index `i_j` for which `i_j = 2` while all the other indices are equal to `1`), which indicates that `qₙ` might grow like `n!`. This is bad for summability properties. It turns out that the right-inverse formula is better behaved, and should instead be used for this kind of estimate. It reads $$ q_n = - (p_1)^{-1} \sum_{k=2}^n \sum_{i_1 + \dotsc + i_k = n} p_k (q_{i_1}, \dotsc, q_{i_k}). $$ Here, `q_{n-1}` can only appear in the term with `k = 2`, and it only appears twice, so there is hope this formula can lead to an at most geometric behavior. Let `Qₙ = ∥qₙ∥`. Bounding `∥pₖ∥` with `C r^k` gives an inequality $$ Q_n ≤ C' \sum_{k=2}^n r^k \sum_{i_1 + \dotsc + i_k = n} Q_{i_1} \dotsm Q_{i_k}. $$ This formula is not enough to prove by naive induction on `n` a bound of the form `Qₙ ≤ D R^n`. However, assuming that the inequality above were an equality, one could get a formula for the generating series of the `Qₙ`: $$ \begin{align} Q(z) & := \sum Q_n z^n = Q_1 z + C' \sum_{2 \leq k \leq n} \sum_{i_1 + \dotsc + i_k = n} (r z^{i_1} Q_{i_1}) \dotsm (r z^{i_k} Q_{i_k}) \\ & = Q_1 z + C' \sum_{k = 2}^\infty (\sum_{i_1 \geq 1} r z^{i_1} Q_{i_1}) \dotsm (\sum_{i_k \geq 1} r z^{i_k} Q_{i_k}) \\ & = Q_1 z + C' \sum_{k = 2}^\infty (r Q(z))^k = Q_1 z + C' (r Q(z))^2 / (1 - r Q(z)). \end{align} $$ One can solve this formula explicitly. The solution is analytic in a neighborhood of `0` in `ℂ`, hence its coefficients grow at most geometrically (by a contour integral argument), and therefore the original `Qₙ`, which are bounded by these ones, are also at most geometric. This classical argument is not really satisfactory, as it requires an a priori bound on a complex analytic function. Another option would be to compute explicitly its terms (with binomial coefficients) to obtain an explicit geometric bound, but this would be very painful. Instead, we will use the above intuition, but in a slightly different form, with finite sums and an induction. I learnt this trick in [pöschel2017siegelsternberg]. Let $S_n = \sum_{k=1}^n Q_k a^k$ (where `a` is a positive real parameter to be chosen suitably small). The above computation but with finite sums shows that $$ S_n \leq Q_1 a + C' \sum_{k=2}^n (r S_{n-1})^k. $$ In particular, $S_n \leq Q_1 a + C' (r S_{n-1})^2 / (1- r S_{n-1})$. Assume that $S_{n-1} \leq K a$, where `K > Q₁` is fixed and `a` is small enough so that `r K a ≤ 1/2` (to control the denominator). Then this equation gives a bound $S_n \leq Q_1 a + 2 C' r^2 K^2 a^2$. If `a` is small enough, this is bounded by `K a` as the second term is quadratic in `a`, and therefore negligible. By induction, we deduce `Sₙ ≤ K a` for all `n`, which gives in particular the fact that `aⁿ Qₙ` remains bounded. -/ /-- First technical lemma to control the growth of coefficients of the inverse. Bound the explicit expression for `∑_{k<n+1} aᵏ Qₖ` in terms of a sum of powers of the same sum one step before, in a general abstract setup. -/ lemma radius_right_inv_pos_of_radius_pos_aux1 (n : ℕ) (p : ℕ → ℝ) (hp : ∀ k, 0 ≤ p k) {r a : ℝ} (hr : 0 ≤ r) (ha : 0 ≤ a) : ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, p (c.blocks_fun j)) ≤ ∑ j in Ico 2 (n + 1), r ^ j * (∑ k in Ico 1 n, a ^ k * p k) ^ j := calc ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, p (c.blocks_fun j)) = ∑ k in Ico 2 (n + 1), (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), ∏ j, r * (a ^ (c.blocks_fun j) * p (c.blocks_fun j))) : begin simp_rw [mul_sum], apply sum_congr rfl (λ k hk, _), apply sum_congr rfl (λ c hc, _), rw [prod_mul_distrib, prod_mul_distrib, prod_pow_eq_pow_sum, composition.sum_blocks_fun, prod_const, card_fin], ring, end ... ≤ ∑ d in comp_partial_sum_target 2 (n + 1) n, ∏ (j : fin d.2.length), r * (a ^ d.2.blocks_fun j * p (d.2.blocks_fun j)) : begin rw sum_sigma', refine sum_le_sum_of_subset_of_nonneg _ (λ x hx1 hx2, prod_nonneg (λ j hj, mul_nonneg hr (mul_nonneg (pow_nonneg ha _) (hp _)))), rintros ⟨k, c⟩ hd, simp only [set.mem_to_finset, mem_Ico, mem_sigma, set.mem_set_of_eq] at hd, simp only [mem_comp_partial_sum_target_iff], refine ⟨hd.2, c.length_le.trans_lt hd.1.2, λ j, _⟩, have : c ≠ composition.single k (zero_lt_two.trans_le hd.1.1), by simp [composition.eq_single_iff_length, ne_of_gt hd.2], rw composition.ne_single_iff at this, exact (this j).trans_le (nat.lt_succ_iff.mp hd.1.2) end ... = ∑ e in comp_partial_sum_source 2 (n+1) n, ∏ (j : fin e.1), r * (a ^ e.2 j * p (e.2 j)) : begin symmetry, apply comp_change_of_variables_sum, rintros ⟨k, blocks_fun⟩ H, have K : (comp_change_of_variables 2 (n + 1) n ⟨k, blocks_fun⟩ H).snd.length = k, by simp, congr' 2; try { rw K }, rw fin.heq_fun_iff K.symm, assume j, rw comp_change_of_variables_blocks_fun, end ... = ∑ j in Ico 2 (n+1), r ^ j * (∑ k in Ico 1 n, a ^ k * p k) ^ j : begin rw [comp_partial_sum_source, ← sum_sigma' (Ico 2 (n + 1)) (λ (k : ℕ), (fintype.pi_finset (λ (i : fin k), Ico 1 n) : finset (fin k → ℕ))) (λ n e, ∏ (j : fin n), r * (a ^ e j * p (e j)))], apply sum_congr rfl (λ j hj, _), simp only [← @multilinear_map.mk_pi_algebra_apply ℝ (fin j) _ _ ℝ], simp only [← multilinear_map.map_sum_finset (multilinear_map.mk_pi_algebra ℝ (fin j) ℝ) (λ k (m : ℕ), r * (a ^ m * p m))], simp only [multilinear_map.mk_pi_algebra_apply], dsimp, simp [prod_const, ← mul_sum, mul_pow], end /-- Second technical lemma to control the growth of coefficients of the inverse. Bound the explicit expression for `∑_{k<n+1} aᵏ Qₖ` in terms of a sum of powers of the same sum one step before, in the specific setup we are interesting in, by reducing to the general bound in `radius_right_inv_pos_of_radius_pos_aux1`. -/ lemma radius_right_inv_pos_of_radius_pos_aux2 {n : ℕ} (hn : 2 ≤ n + 1) (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) {r a C : ℝ} (hr : 0 ≤ r) (ha : 0 ≤ a) (hC : 0 ≤ C) (hp : ∀ n, ∥p n∥ ≤ C * r ^ n) : (∑ k in Ico 1 (n + 1), a ^ k * ∥p.right_inv i k∥) ≤ ∥(i.symm : F →L[𝕜] E)∥ * a + ∥(i.symm : F →L[𝕜] E)∥ * C * ∑ k in Ico 2 (n + 1), (r * ((∑ j in Ico 1 n, a ^ j * ∥p.right_inv i j∥))) ^ k := let I := ∥(i.symm : F →L[𝕜] E)∥ in calc ∑ k in Ico 1 (n + 1), a ^ k * ∥p.right_inv i k∥ = a * I + ∑ k in Ico 2 (n + 1), a ^ k * ∥p.right_inv i k∥ : by simp only [linear_isometry_equiv.norm_map, pow_one, right_inv_coeff_one, nat.Ico_succ_singleton, sum_singleton, ← sum_Ico_consecutive _ one_le_two hn] ... = a * I + ∑ k in Ico 2 (n + 1), a ^ k * ∥(i.symm : F →L[𝕜] E).comp_continuous_multilinear_map (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), p.comp_along_composition (p.right_inv i) c)∥ : begin congr' 1, apply sum_congr rfl (λ j hj, _), rw [right_inv_coeff _ _ _ (mem_Ico.1 hj).1, norm_neg], end ... ≤ a * ∥(i.symm : F →L[𝕜] E)∥ + ∑ k in Ico 2 (n + 1), a ^ k * (I * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), C * r ^ c.length * ∏ j, ∥p.right_inv i (c.blocks_fun j)∥)) : begin apply_rules [add_le_add, le_refl, sum_le_sum (λ j hj, _), mul_le_mul_of_nonneg_left, pow_nonneg, ha], apply (continuous_linear_map.norm_comp_continuous_multilinear_map_le _ _).trans, apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), apply (norm_sum_le _ _).trans, apply sum_le_sum (λ c hc, _), apply (comp_along_composition_norm _ _ _).trans, apply mul_le_mul_of_nonneg_right (hp _), exact prod_nonneg (λ j hj, norm_nonneg _), end ... = I * a + I * C * ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, ∥p.right_inv i (c.blocks_fun j)∥) : begin simp_rw [mul_assoc C, ← mul_sum, ← mul_assoc, mul_comm _ (∥↑i.symm∥), mul_assoc, ← mul_sum, ← mul_assoc, mul_comm _ C, mul_assoc, ← mul_sum], ring, end ... ≤ I * a + I * C * ∑ k in Ico 2 (n+1), (r * ((∑ j in Ico 1 n, a ^ j * ∥p.right_inv i j∥))) ^ k : begin apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, norm_nonneg, hC, mul_nonneg], simp_rw [mul_pow], apply radius_right_inv_pos_of_radius_pos_aux1 n (λ k, ∥p.right_inv i k∥) (λ k, norm_nonneg _) hr ha, end /-- If a a formal multilinear series has a positive radius of convergence, then its right inverse also has a positive radius of convergence. -/ theorem radius_right_inv_pos_of_radius_pos (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (hp : 0 < p.radius) : 0 < (p.right_inv i).radius := begin obtain ⟨C, r, Cpos, rpos, ple⟩ : ∃ C r (hC : 0 < C) (hr : 0 < r), ∀ (n : ℕ), ∥p n∥ ≤ C * r ^ n := le_mul_pow_of_radius_pos p hp, let I := ∥(i.symm : F →L[𝕜] E)∥, -- choose `a` small enough to make sure that `∑_{k ≤ n} aᵏ Qₖ` will be controllable by -- induction obtain ⟨a, apos, ha1, ha2⟩ : ∃ a (apos : 0 < a), (2 * I * C * r^2 * (I + 1) ^ 2 * a ≤ 1) ∧ (r * (I + 1) * a ≤ 1/2), { have : tendsto (λ a, 2 * I * C * r^2 * (I + 1) ^ 2 * a) (𝓝 0) (𝓝 (2 * I * C * r^2 * (I + 1) ^ 2 * 0)) := tendsto_const_nhds.mul tendsto_id, have A : ∀ᶠ a in 𝓝 0, 2 * I * C * r^2 * (I + 1) ^ 2 * a < 1, by { apply (tendsto_order.1 this).2, simp [zero_lt_one] }, have : tendsto (λ a, r * (I + 1) * a) (𝓝 0) (𝓝 (r * (I + 1) * 0)) := tendsto_const_nhds.mul tendsto_id, have B : ∀ᶠ a in 𝓝 0, r * (I + 1) * a < 1/2, by { apply (tendsto_order.1 this).2, simp [zero_lt_one] }, have C : ∀ᶠ a in 𝓝[set.Ioi (0 : ℝ)] (0 : ℝ), (0 : ℝ) < a, by { filter_upwards [self_mem_nhds_within], exact λ a ha, ha }, rcases (C.and ((A.and B).filter_mono inf_le_left)).exists with ⟨a, ha⟩, exact ⟨a, ha.1, ha.2.1.le, ha.2.2.le⟩ }, -- check by induction that the partial sums are suitably bounded, using the choice of `a` and the -- inductive control from Lemma `radius_right_inv_pos_of_radius_pos_aux2`. let S := λ n, ∑ k in Ico 1 n, a ^ k * ∥p.right_inv i k∥, have IRec : ∀ n, 1 ≤ n → S n ≤ (I + 1) * a, { apply nat.le_induction, { simp only [S], rw [Ico_eq_empty_of_le (le_refl 1), sum_empty], exact mul_nonneg (add_nonneg (norm_nonneg _) zero_le_one) apos.le }, { assume n one_le_n hn, have In : 2 ≤ n + 1, by linarith, have Snonneg : 0 ≤ S n := sum_nonneg (λ x hx, mul_nonneg (pow_nonneg apos.le _) (norm_nonneg _)), have rSn : r * S n ≤ 1/2 := calc r * S n ≤ r * ((I+1) * a) : mul_le_mul_of_nonneg_left hn rpos.le ... ≤ 1/2 : by rwa [← mul_assoc], calc S (n + 1) ≤ I * a + I * C * ∑ k in Ico 2 (n + 1), (r * S n)^k : radius_right_inv_pos_of_radius_pos_aux2 In p i rpos.le apos.le Cpos.le ple ... = I * a + I * C * (((r * S n) ^ 2 - (r * S n) ^ (n + 1)) / (1 - r * S n)) : by { rw geom_sum_Ico' _ In, exact ne_of_lt (rSn.trans_lt (by norm_num)) } ... ≤ I * a + I * C * ((r * S n) ^ 2 / (1/2)) : begin apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, Cpos.le], refine div_le_div (sq_nonneg _) _ (by norm_num) (by linarith), simp only [sub_le_self_iff], apply pow_nonneg (mul_nonneg rpos.le Snonneg), end ... = I * a + 2 * I * C * (r * S n) ^ 2 : by ring ... ≤ I * a + 2 * I * C * (r * ((I + 1) * a)) ^ 2 : by apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, Cpos.le, zero_le_two, pow_le_pow_of_le_left, rpos.le] ... = (I + 2 * I * C * r^2 * (I + 1) ^ 2 * a) * a : by ring ... ≤ (I + 1) * a : by apply_rules [mul_le_mul_of_nonneg_right, apos.le, add_le_add, le_refl] } }, -- conclude that all coefficients satisfy `aⁿ Qₙ ≤ (I + 1) a`. let a' : nnreal := ⟨a, apos.le⟩, suffices H : (a' : ennreal) ≤ (p.right_inv i).radius, by { apply lt_of_lt_of_le _ H, exact_mod_cast apos }, apply le_radius_of_bound _ ((I + 1) * a) (λ n, _), by_cases hn : n = 0, { have : ∥p.right_inv i n∥ = ∥p.right_inv i 0∥, by congr; try { rw hn }, simp only [this, norm_zero, zero_mul, right_inv_coeff_zero], apply_rules [mul_nonneg, add_nonneg, norm_nonneg, zero_le_one, apos.le] }, { have one_le_n : 1 ≤ n := bot_lt_iff_ne_bot.2 hn, calc ∥p.right_inv i n∥ * ↑a' ^ n = a ^ n * ∥p.right_inv i n∥ : mul_comm _ _ ... ≤ ∑ k in Ico 1 (n + 1), a ^ k * ∥p.right_inv i k∥ : begin have : ∀ k ∈ Ico 1 (n + 1), 0 ≤ a ^ k * ∥p.right_inv i k∥ := λ k hk, mul_nonneg (pow_nonneg apos.le _) (norm_nonneg _), exact single_le_sum this (by simp [one_le_n]), end ... ≤ (I + 1) * a : IRec (n + 1) (by dec_trivial) } end end formal_multilinear_series
f3251d2adafa66325e6e18b761b2b790a7d956fd
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/Pointed.lean
e0a7a264893e8a749805bbad9bc7b46e0f6e0fce
[]
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
3,869
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 Pointed structure Pointed (A : Type) : Type := (e : A) open Pointed structure Sig (AS : Type) : Type := (eS : AS) structure Product (A : Type) : Type := (eP : (Prod A A)) structure Hom {A1 : Type} {A2 : Type} (Po1 : (Pointed A1)) (Po2 : (Pointed A2)) : Type := (hom : (A1 → A2)) (pres_e : (hom (e Po1)) = (e Po2)) structure RelInterp {A1 : Type} {A2 : Type} (Po1 : (Pointed A1)) (Po2 : (Pointed A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_e : (interp (e Po1) (e Po2))) inductive PointedTerm : Type | eL : PointedTerm open PointedTerm inductive ClPointedTerm (A : Type) : Type | sing : (A → ClPointedTerm) | eCl : ClPointedTerm open ClPointedTerm inductive OpPointedTerm (n : ℕ) : Type | v : ((fin n) → OpPointedTerm) | eOL : OpPointedTerm open OpPointedTerm inductive OpPointedTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpPointedTerm2) | sing2 : (A → OpPointedTerm2) | eOL2 : OpPointedTerm2 open OpPointedTerm2 def simplifyCl {A : Type} : ((ClPointedTerm A) → (ClPointedTerm A)) | eCl := eCl | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpPointedTerm n) → (OpPointedTerm n)) | eOL := eOL | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpPointedTerm2 n A) → (OpPointedTerm2 n A)) | eOL2 := eOL2 | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((Pointed A) → (PointedTerm → A)) | Po eL := (e Po) def evalCl {A : Type} : ((Pointed A) → ((ClPointedTerm A) → A)) | Po (sing x1) := x1 | Po eCl := (e Po) def evalOpB {A : Type} {n : ℕ} : ((Pointed A) → ((vector A n) → ((OpPointedTerm n) → A))) | Po vars (v x1) := (nth vars x1) | Po vars eOL := (e Po) def evalOp {A : Type} {n : ℕ} : ((Pointed A) → ((vector A n) → ((OpPointedTerm2 n A) → A))) | Po vars (v2 x1) := (nth vars x1) | Po vars (sing2 x1) := x1 | Po vars eOL2 := (e Po) def inductionB {P : (PointedTerm → Type)} : ((P eL) → (∀ (x : PointedTerm) , (P x))) | pel eL := pel def inductionCl {A : Type} {P : ((ClPointedTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P eCl) → (∀ (x : (ClPointedTerm A)) , (P x)))) | psing pecl (sing x1) := (psing x1) | psing pecl eCl := pecl def inductionOpB {n : ℕ} {P : ((OpPointedTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P eOL) → (∀ (x : (OpPointedTerm n)) , (P x)))) | pv peol (v x1) := (pv x1) | pv peol eOL := peol def inductionOp {n : ℕ} {A : Type} {P : ((OpPointedTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P eOL2) → (∀ (x : (OpPointedTerm2 n A)) , (P x))))) | pv2 psing2 peol2 (v2 x1) := (pv2 x1) | pv2 psing2 peol2 (sing2 x1) := (psing2 x1) | pv2 psing2 peol2 eOL2 := peol2 def stageB : (PointedTerm → (Staged PointedTerm)) | eL := (Now eL) def stageCl {A : Type} : ((ClPointedTerm A) → (Staged (ClPointedTerm A))) | (sing x1) := (Now (sing x1)) | eCl := (Now eCl) def stageOpB {n : ℕ} : ((OpPointedTerm n) → (Staged (OpPointedTerm n))) | (v x1) := (const (code (v x1))) | eOL := (Now eOL) def stageOp {n : ℕ} {A : Type} : ((OpPointedTerm2 n A) → (Staged (OpPointedTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | eOL2 := (Now eOL2) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (eT : (Repr A)) end Pointed
8fedb9a426d60de395c39dceff4e059a98e29ce1
ea11767c9c6a467c4b7710ec6f371c95cfc023fd
/src/monoidal_categories/coherence_thm.lean
4b3b2b0687e788714b7b66159e095693228bb0aa
[]
no_license
RitaAhmadi/lean-monoidal-categories
68a23f513e902038e44681336b87f659bbc281e0
81f43e1e0d623a96695aa8938951d7422d6d7ba6
refs/heads/master
1,651,458,686,519
1,529,824,613,000
1,529,824,613,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,428
lean
-- This was written by Tim (who?) in 2017, but the codebase has moved on and I haven't -- been able to maintain it. -- In any case, there was never a clear main statement, and I think I'd prefer to do this -- much more explicitly by giving an equivalence to a strictly monoidal category. -- import .monoidal_category -- import .util.data.nonempty_list -- import .util.data.bin_tree -- import .util.data.bin_tree.cong_clos -- import tidy.congr_struct -- open categories -- open categories.monoidal_category -- open util.data.nonempty_list -- open util.data.bin_tree' -- open util.data.bin_tree'.bin_tree' -- namespace categories.monoidal_category.coherence_thm -- universes u v -- variable {α : Type u} -- inductive reassoc_dir_single_step : bin_tree' α → bin_tree' α → Type u -- | rotate_right : Π r s t, reassoc_dir_single_step (branch (branch r s) t) (branch r (branch s t)) -- namespace reassoc_dir_single_step -- lemma respects_to_list : Π (s t : bin_tree' α), reassoc_dir_single_step s t → s.to_list = t.to_list -- | ._ ._ (rotate_right _ _ _) := -- begin -- unfold bin_tree'.to_list, -- rewrite nonempty_list.append_assoc -- end -- end reassoc_dir_single_step -- @[reducible] def reassoc_dir_step : bin_tree' α → bin_tree' α → Type u := -- cong_clos_step reassoc_dir_single_step -- namespace reassoc_dir_step -- lemma respects_lopsided {s t : bin_tree' α} (p : reassoc_dir_step s t) : s.lopsided = t.lopsided := -- cong_clos_step.respects_lopsided reassoc_dir_single_step.respects_to_list p -- end reassoc_dir_step -- @[reducible] def reassoc_dir : bin_tree' α → bin_tree' α → Type u := -- cong_clos reassoc_dir_single_step -- @[reducible] def reassoc_dir' : bin_tree' α → bin_tree' α → Type u := -- cong_clos' reassoc_dir_single_step -- namespace reassoc_dir -- @[reducible] def refl (t : bin_tree' α) : reassoc_dir t t := cong_clos.refl _ t -- def rotate_right (r s t : bin_tree' α) : reassoc_dir (branch (branch r s) t) (branch r (branch s t)) := -- cong_clos.lift (reassoc_dir_single_step.rotate_right _ _ _) -- def lopsided_combine : Π (xs ys : nonempty_list α), -- reassoc_dir (branch (from_list_lopsided xs) (from_list_lopsided ys)) (from_list_lopsided (xs ++ ys)) -- | (nonempty_list.singleton x) ys := refl _ -- | (nonempty_list.cons x xs) ys := -- begin -- simp, unfold from_list_lopsided, -- apply cong_clos.trans, -- apply rotate_right, -- apply cong_clos.cong, -- apply reassoc_dir.refl, -- apply lopsided_combine -- end -- def reassoc_lopsided : Π (t : bin_tree' α), reassoc_dir t t.lopsided -- | (leaf x) := refl _ -- | (branch l r) := -- begin -- apply cong_clos.trans, -- apply cong_clos.cong, -- apply reassoc_lopsided, -- apply reassoc_lopsided, -- apply lopsided_combine -- end -- lemma reassoc_already_lopsided : -- Π (l : nonempty_list α), -- reassoc_lopsided (bin_tree'.from_list_lopsided l) == refl (bin_tree'.from_list_lopsided l) -- | (nonempty_list.singleton x) := by reflexivity -- | (nonempty_list.cons x xs) := -- calc -- cong_clos.trans -- (cong_clos.inject_right _ (reassoc_lopsided (from_list_lopsided xs))) -- (refl (branch (leaf x) (from_list_lopsided (to_list (from_list_lopsided xs))))) -- = cong_clos.inject_right (leaf x) (reassoc_lopsided (from_list_lopsided xs)) -- : by apply cong_clos.trans_refl_right -- ... == cong_clos.inject_right (leaf x) (refl (from_list_lopsided xs)) -- : begin -- congr_args, -- unfold lopsided, rewrite from_list_lopsided_to_list, -- apply reassoc_already_lopsided -- end -- ... == refl (branch (leaf x) (from_list_lopsided xs)) -- : by reflexivity -- lemma respects_to_list {s t : bin_tree' α} (p : reassoc_dir s t) : s.to_list = t.to_list := -- cong_clos.respects_to_list reassoc_dir_single_step.respects_to_list p -- lemma respects_lopsided {s t : bin_tree' α} (p : reassoc_dir s t) : s.lopsided = t.lopsided := -- cong_clos.respects_lopsided reassoc_dir_single_step.respects_to_list p -- end reassoc_dir -- inductive reassoc_single_step : bin_tree' α → bin_tree' α → Type u -- | rotate_left : Π r s t, reassoc_single_step (branch r (branch s t)) (branch (branch r s) t) -- | rotate_right : Π r s t, reassoc_single_step (branch (branch r s) t) (branch r (branch s t)) -- @[reducible] def reassoc_step : bin_tree' α → bin_tree' α → Type u := -- cong_clos_step reassoc_single_step -- @[reducible] def reassoc : bin_tree' α → bin_tree' α → Type u := -- cong_clos reassoc_single_step -- namespace reassoc_single_step -- def sym : Π (x y : bin_tree' α), reassoc_single_step x y → reassoc_single_step y x -- | ._ ._ (rotate_left _ _ _) := rotate_right _ _ _ -- | ._ ._ (rotate_right _ _ _) := rotate_left _ _ _ -- lemma respects_to_list : Π (s t : bin_tree' α), reassoc_single_step s t → s.to_list = t.to_list -- | ._ ._ (rotate_left _ _ _) := -- begin -- unfold bin_tree'.to_list, -- rewrite nonempty_list.append_assoc -- end -- | ._ ._ (rotate_right _ _ _) := -- begin -- unfold bin_tree'.to_list, -- rewrite nonempty_list.append_assoc -- end -- end reassoc_single_step -- def reassoc_dir_to_reassoc_single_step : Π (s t : bin_tree' α), reassoc_dir_single_step s t → reassoc_single_step s t -- | ._ ._ (reassoc_dir_single_step.rotate_right s t u) := reassoc_single_step.rotate_right s t u -- def reassoc_dir_to_reassoc : Π {s t : bin_tree' α}, reassoc_dir s t → reassoc s t := -- λ s t p, cong_clos.transport reassoc_dir_to_reassoc_single_step p -- namespace reassoc -- @[reducible] def refl (t : bin_tree' α) : reassoc_dir t t := cong_clos.refl _ t -- def sym : Π {x y : bin_tree' α}, reassoc x y → reassoc y x := -- λ x y, cong_clos.sym reassoc_single_step.sym -- lemma respects_to_list : Π {s t : bin_tree' α}, reassoc s t → s.to_list = t.to_list := -- λ x y, cong_clos.respects_to_list reassoc_single_step.respects_to_list -- def reassoc_lopsided : Π (t : bin_tree' α), reassoc t t.lopsided := -- λ t, reassoc_dir_to_reassoc (reassoc_dir.reassoc_lopsided t) -- -- TODO: more economical construction (with fewer rotations) -- def reassoc_tree : Π (r t : bin_tree' α) (h : r.to_list = t.to_list), reassoc r t := -- begin -- intros, -- apply cong_clos.trans, -- apply reassoc_lopsided, -- -- TODO: doing `rewrite h` here doesn't work ~> report bug -- apply sym, -- unfold lopsided, rewrite h, -- apply reassoc_lopsided -- end -- end reassoc -- section interpretation -- parameter (C : Category.{u v}) -- parameter (M : MonoidalStructure C) -- @[reducible] def tensor : C.Obj → C.Obj → C.Obj := λ X Y, M.tensor (X, Y) -- local infixl `⟩C⟩`:60 := C.compose -- local infix `⊗`:60 := tensor -- -- this is better behaved wrt type inference than `M.tensor.onMorphisms` because no tuple has to be inferred -- @[reducible] def tensor_homs : Π {W X Y Z : C.Obj}, C.Hom W X → C.Hom Y Z → C.Hom (W ⊗ Y) (X ⊗ Z) := -- λ W X Y Z f g, M.tensor.onMorphisms (f, g) -- local infix `⟨⊗⟩`:60 := tensor_homs -- open cong_clos -- @[reducible] def tensor_tree : bin_tree' C.Obj → C.Obj -- | (bin_tree'.leaf A) := A -- | (bin_tree'.branch l r) := tensor_tree l ⊗ tensor_tree r -- def interpret_cong_clos' -- {R : bin_tree' C.Obj → bin_tree' C.Obj → Type u} -- (I : Π (Xs Ys : bin_tree' C.Obj), R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys)) -- : Π {Xs Ys : bin_tree' C.Obj}, cong_clos' R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) -- | ._ ._ (cong_clos'.lift _ _ p) := I _ _ p -- | ._ ._ (cong_clos'.refl ._ t) := C.identity _ -- | ._ ._ (cong_clos'.trans _ _ _ p q) := C.compose (interpret_cong_clos' p) (interpret_cong_clos' q) -- | ._ ._ (cong_clos'.cong _ _ _ _ l r) := M.tensor.onMorphisms (interpret_cong_clos' l, interpret_cong_clos' r) -- -- the equation compiler somehow can't handle the next definitions ~> turn it off -- -- TODO(tim): report bug -- set_option eqn_compiler.lemmas false -- -- TODO(tim): factor out I as a variable -- def interpret_cong_clos_step -- {R : bin_tree' C.Obj → bin_tree' C.Obj → Type u} -- (I : Π (Xs Ys : bin_tree' C.Obj), R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys)) -- : Π {Xs Ys : bin_tree' C.Obj}, cong_clos_step R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) -- | ._ ._ (cong_clos_step.lift x y p) := I x y p -- | ._ ._ (cong_clos_step.left l₁ l₂ r l) := interpret_cong_clos_step l ⟨⊗⟩ C.identity (tensor_tree r) -- | ._ ._ (cong_clos_step.right l r₁ r₂ r) := C.identity (tensor_tree l) ⟨⊗⟩ interpret_cong_clos_step r -- set_option eqn_compiler.lemmas true -- def interpret_cong_clos -- {R : bin_tree' C.Obj → bin_tree' C.Obj → Type u} -- (I : Π (Xs Ys : bin_tree' C.Obj), R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys)) -- : Π {Xs Ys : bin_tree' C.Obj}, cong_clos R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) -- | ._ ._ (cong_clos.refl ._ t) := C.identity _ -- | ._ ._ (cong_clos.step _ _ _ p q) := C.compose (interpret_cong_clos_step I p) (interpret_cong_clos q) -- lemma interpret_cong_clos_functoriality -- {R : bin_tree' C.Obj → bin_tree' C.Obj → Type u} -- (I : Π (Xs Ys : bin_tree' C.Obj), R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys)) -- : Π (Xs Ys Zs : bin_tree' C.Obj) (ps : cong_clos R Xs Ys) (qs : cong_clos R Ys Zs), -- interpret_cong_clos I ps ⟩C⟩ interpret_cong_clos I qs = interpret_cong_clos I (cong_clos.trans ps qs) -- | ._ ._ _ (cong_clos.refl ._ t) qs := C.left_identity _ -- | ._ ._ _ (cong_clos.step _ _ _ p ps) qs := -- begin -- unfold cong_clos.trans, -- unfold interpret_cong_clos, -- rewrite C.associativity, -- rewrite interpret_cong_clos_functoriality -- end -- -- TODO(tim): move this somewhere else? -- lemma functoriality_left -- (X Y Z A : C.Obj) -- (f : C.Hom X Y) (g : C.Hom Y Z) -- : (f ⟩C⟩ g) ⟨⊗⟩ C.identity A = (f ⟨⊗⟩ C.identity A) ⟩C⟩ (g ⟨⊗⟩ C.identity A) := ♯ -- lemma interpret_cong_clos_inject_left -- {R : bin_tree' C.Obj → bin_tree' C.Obj → Type u} -- (I : Π (Xs Ys : bin_tree' C.Obj), R Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys)) -- : Π (Xs Ys Zs : bin_tree' C.Obj) (ps : cong_clos R Xs Ys), -- interpret_cong_clos I (inject_left Zs ps) = interpret_cong_clos I ps ⟨⊗⟩ C.identity (tensor_tree Zs) -- | ._ ._ _ (cong_clos.refl ._ _) := by {symmetry, apply M.tensor.identities} -- | ._ ._ _ (cong_clos.step _ _ _ p ps) := -- begin -- unfold inject_left, -- unfold interpret_cong_clos, -- rewrite functoriality_left, -- rewrite (interpret_cong_clos_inject_left _ _ _ ps), -- reflexivity -- end -- -- TODO(tim): it would be cool if interpretation were a real functor from a suitable formal category -- def interpret_reassoc_dir_single_step : Π (Xs Ys : bin_tree' C.Obj), reassoc_dir_single_step Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) -- | ._ ._ (reassoc_dir_single_step.rotate_right s t u) := M.associator _ _ _ -- def interpret_reassoc_dir {Xs Ys : bin_tree' C.Obj} : reassoc_dir Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) := -- interpret_cong_clos interpret_reassoc_dir_single_step -- lemma interpret_reassoc_dir_functoriality -- {Xs Ys Zs : bin_tree' C.Obj} (ps : reassoc_dir Xs Ys) (qs : reassoc_dir Ys Zs) -- : interpret_reassoc_dir ps ⟩C⟩ interpret_reassoc_dir qs = interpret_reassoc_dir (cong_clos.trans ps qs) -- := by apply interpret_cong_clos_functoriality -- def interpret_reassoc_dir' {Xs Ys : bin_tree' C.Obj} : reassoc_dir' Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) := -- interpret_cong_clos' interpret_reassoc_dir_single_step -- def interpret_reassoc_dir_step {Xs Ys : bin_tree' C.Obj} : reassoc_dir_step Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) := -- interpret_cong_clos_step interpret_reassoc_dir_single_step -- def interpret_reassoc_single_step : Π (Xs Ys : bin_tree' C.Obj), reassoc_single_step Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) -- | ._ ._ (reassoc_single_step.rotate_right s t u) := M.associator _ _ _ -- | ._ ._ (reassoc_single_step.rotate_left s t u) := M.inverse_associator _ _ _ -- def interpret_reassoc {Xs Ys : bin_tree' C.Obj} : reassoc Xs Ys → C.Hom (tensor_tree Xs) (tensor_tree Ys) := -- interpret_cong_clos interpret_reassoc_single_step -- @[reducible] def to_lopsided (t : bin_tree' C.Obj) : C.Hom (tensor_tree t) (tensor_tree t.lopsided) := -- interpret_reassoc_dir (reassoc_dir.reassoc_lopsided t) -- -- TODO: generalize to cong_clos -- def rewrite_source : Π {s₁ s₂ t : bin_tree' α} (eq : s₁ = s₂), reassoc_dir s₁ t → reassoc_dir s₂ t -- | _ ._ _ (eq.refl ._) p := p -- -- TODO: generalize to cong_clos -- def rewrite_target : Π {s t₁ t₂ : bin_tree' α} (eq : t₁ = t₂), reassoc_dir s t₁ → reassoc_dir s t₂ -- | _ ._ _ (eq.refl ._) p := p -- -- TODO: generalize to cong_clos -- def rewrite_target_cong : Π {s t₁ t₂ : bin_tree' α} (eq : t₁ = t₂) (p : reassoc_dir s t₁), rewrite_target eq p == p -- | _ _ ._ (eq.refl ._) p := heq.refl _ -- @[reducible] def trans_lopsided' {s t : bin_tree' α} (p : reassoc_dir s t) : reassoc_dir s t.lopsided := -- cong_clos.trans p (reassoc_dir.reassoc_lopsided t) -- -- all roads lead to rome -- def trans_lopsided {s t : bin_tree' α} (p : reassoc_dir s t) : reassoc_dir s s.lopsided := -- rewrite_target (eq.symm (reassoc_dir.respects_lopsided p)) (trans_lopsided' p) -- lemma trans_lopsided_heq {s t : bin_tree' α} (p : reassoc_dir s t) : trans_lopsided p == trans_lopsided' p := -- by apply rewrite_target_cong -- lemma trans_lopsided_already_lopsided {s : bin_tree' α} (p : reassoc_dir s s.lopsided) : trans_lopsided p == p := -- calc -- rewrite_target (eq.symm (reassoc_dir.respects_lopsided p)) (cong_clos.trans p (reassoc_dir.reassoc_lopsided s.lopsided)) -- == cong_clos.trans p (reassoc_dir.reassoc_lopsided s.lopsided) -- : by apply rewrite_target_cong -- ... == cong_clos.trans p (reassoc_dir.refl s.lopsided) -- : begin -- congr_args, rewrite lopsided_idempotent, apply reassoc_dir.reassoc_already_lopsided -- end -- ... = p -- : by apply trans_refl_right -- @[reducible] def step_lopsided' {s t : bin_tree' α} (p : reassoc_dir_step s t) : reassoc_dir s t.lopsided := -- cong_clos.step _ _ _ p (reassoc_dir.reassoc_lopsided t) -- def step_lopsided {s t : bin_tree' α} (p : reassoc_dir_step s t) : reassoc_dir s s.lopsided := -- rewrite_target (eq.symm (reassoc_dir_step.respects_lopsided p)) (step_lopsided' p) -- lemma step_lopsided_heq {s t : bin_tree' α} (p : reassoc_dir_step s t) : step_lopsided p == step_lopsided' p := -- by apply rewrite_target_cong -- -- -- TODO(tim): use well-founded induction once lean supports it -- -- -- TODO(tim): why does lean complain that there are missing cases??? -- -- meta lemma directed_associator_coherence_thm : -- -- Π (Xs Ys : bin_tree' C.Obj) (e : Ys = Xs.lopsided) -- -- (p q : reassoc_dir Xs Ys), -- -- interpret_reassoc_dir p = interpret_reassoc_dir q -- -- | (branch l r) ._ (eq.refl ._) (cong_clos.step ._ ._ ._ (cong_clos_step.left ._ lp ._ p) ps) (cong_clos.step ._ ._ ._ (cong_clos_step.left ._ lq ._ q) qs) := -- -- have H : Π (l' : bin_tree' C.Obj) (f : reassoc_dir_step l l') (fs : reassoc_dir (branch l' r) (branch l r).lopsided), -- -- interpret_reassoc_dir (step (branch l r) _ _ (cong_clos_step.left _ _ r f) fs) == -- -- (to_lopsided l ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ to_lopsided (branch l.lopsided r), from -- -- λ l' f fs, -- -- have e₀ : lopsided l = lopsided l', from -- -- by {apply reassoc_dir_step.respects_lopsided, assumption}, -- -- have e₁ : lopsided (branch l r) = lopsided (branch l' r), from -- -- by {apply reassoc_dir_step.respects_lopsided, apply cong_clos_step.left, assumption}, -- -- have e₂ : lopsided (branch l' r) = lopsided (branch (lopsided l') r), from -- -- by {unfold lopsided, congr_args, unfold to_list, rewrite from_list_lopsided_to_list}, -- -- have e₃ : interpret_reassoc_dir fs == (interpret_reassoc_dir (reassoc_dir.reassoc_lopsided l') ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)), from -- -- calc -- -- interpret_reassoc_dir fs -- -- == interpret_reassoc_dir (rewrite_target e₁ fs) -- -- : by {congr_args, assumption, symmetry, apply rewrite_target_cong} -- -- ... = interpret_reassoc_dir (trans_lopsided (cong_clos.inject_left r (reassoc_dir.reassoc_lopsided l'))) -- -- : by apply directed_associator_coherence_thm (branch l' r) (branch l' r).lopsided (eq.refl _) -- -- ... == interpret_reassoc_dir (trans_lopsided' (cong_clos.inject_left r (reassoc_dir.reassoc_lopsided l'))) -- -- : by {congr_args, assumption, apply trans_lopsided_heq} -- -- ... = interpret_reassoc_dir (cong_clos.inject_left r (reassoc_dir.reassoc_lopsided l')) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)) -- -- : by {symmetry, apply interpret_reassoc_dir_functoriality} -- -- ... = (interpret_reassoc_dir (reassoc_dir.reassoc_lopsided l') ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)) -- -- : by {congr_args, apply interpret_cong_clos_inject_left}, -- -- calc -- -- (interpret_reassoc_dir_step f ⟨⊗⟩ C.identity _) ⟩C⟩ interpret_reassoc_dir fs -- -- == (interpret_reassoc_dir_step f ⟨⊗⟩ C.identity _) ⟩C⟩ ((interpret_reassoc_dir (reassoc_dir.reassoc_lopsided l') ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r))) -- -- : by {congr_args, cc, assumption} -- -- ... == ((interpret_reassoc_dir_step f ⟨⊗⟩ C.identity _) ⟩C⟩ (interpret_reassoc_dir (reassoc_dir.reassoc_lopsided l') ⟨⊗⟩ C.identity (tensor_tree r))) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)) -- -- : ♮ -- -- ... == ((interpret_reassoc_dir_step f ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided l')) ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)) -- -- : by rewrite functoriality_left -- -- ... == (interpret_reassoc_dir (cong_clos.step _ _ _ f (reassoc_dir.reassoc_lopsided l')) ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l'.lopsided r)) -- -- : by reflexivity -- -- ... == (interpret_reassoc_dir (step_lopsided f) ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ interpret_reassoc_dir (reassoc_dir.reassoc_lopsided (branch l.lopsided r)) -- -- : begin -- -- congr_args, -- -- {cc}, -- -- {cc}, -- -- {congr_args, cc, congr_args, cc, symmetry, apply step_lopsided_heq}, -- -- {cc} -- -- end -- -- ... = (to_lopsided l ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ to_lopsided (branch l.lopsided r) -- -- : by {congr_args, congr_args, apply directed_associator_coherence_thm l l.lopsided (eq.refl _)}, -- -- eq_of_heq $ -- -- calc -- -- interpret_reassoc_dir (step (branch l r) _ _ (cong_clos_step.left _ _ r p) ps) -- -- == (to_lopsided l ⟨⊗⟩ C.identity (tensor_tree r)) ⟩C⟩ to_lopsided (branch l.lopsided r) -- -- : by apply H -- -- ... == interpret_reassoc_dir (step (branch l r) _ _ (cong_clos_step.left _ _ r q) qs) -- -- : by {symmetry, apply H} -- -- | (branch l r) Ys e (cong_clos.step ._ ._ ._ (cong_clos_step.right ._ ._ rp p) ps) (cong_clos.step ._ ._ ._ (cong_clos_step.right ._ ._ rq q) qs) := sorry -- -- | (branch l r) Ys e (cong_clos.step ._ ._ ._ (cong_clos_step.left ._ lp ._ p) ps) (cong_clos.step ._ ._ ._ (cong_clos_step.right ._ ._ rq q) qs) := sorry -- -- | (branch l r) Ys e (cong_clos.step ._ ._ ._ (cong_clos_step.right ._ ._ rp p) ps) (cong_clos.step ._ ._ ._ (cong_clos_step.left ._ lq ._ q) qs) := sorry -- -- | (branch (branch x y) z) Ys e (cong_clos.step ._ ._ ._ (cong_clos_step.lift ._ ._ (reassoc_dir_single_step.rotate_right ._ ._ ._)) _) (cong_clos.step ._ _ ._ _ _) := sorry -- -- | (branch (branch x y) z) Ys e (cong_clos.step ._ _ ._ _ _) (cong_clos.step ._ ._ ._ (cong_clos_step.lift ._ ._ (reassoc_dir_single_step.rotate_right ._ ._ ._)) _) := sorry -- -- | Xs ._ e (cong_clos.refl ._ ._) (cong_clos.step ._ t' ._ q _) := sorry -- -- | Xs ._ e (cong_clos.step ._ t' ._ p _) (cong_clos.refl ._ ._) := sorry -- -- | Xs ._ e (cong_clos.refl ._ ._) (cong_clos.refl ._ ._) := sorry -- end interpretation -- end categories.monoidal_category.coherence_thm
4cc6f4afa4c238ddc81bfcb0a61c8c0f2e815580
3f48345ac9bbaa421714efc9872a0409379bb4ae
/src/coalgebra/Coalgebra.lean
37d31960807b3d8d4a51505e9467b98a1a1abed1
[]
no_license
QaisHamarneh/Coalgebra-in-Lean
b4318ee6d83780e5c734eb78fed98b1fe8016f7e
bd0452df98bc64b608e5dfd7babc42c301bb6a46
refs/heads/master
1,663,371,200,241
1,661,004,695,000
1,661,004,695,000
209,798,828
0
0
null
null
null
null
UTF-8
Lean
false
false
16,782
lean
import set_category.diagram_lemmas import set_category.category_set import set_category.em_square import help_functions namespace coalgebra open category_theory set category_set diagram_lemmas help_functions classical em_square universes v u local notation f ` ⊚ `:80 g:80 := category_struct.comp g f /-- The stucture of coalgebra of signature F -/ structure Coalgebra (F : Type u ⥤ Type u) : Type (u+1) := (carrier : Type u) (α : carrier ⟶ F.obj carrier) variables {F : Type u ⥤ Type u} /-- Allows us to use the coalgebra as a Type referring to its carrier-/ instance Coalgebra_to_sort : has_coe_to_sort (Coalgebra F) := ⟨Type u, λ 𝔸, 𝔸.carrier⟩ variables {𝔸 Β ℂ: Coalgebra F} /-- A Set-Category morphism(φ : A -> B) is coalgebra morphism iff α_B ∘ φ = F φ ∘ α_A -/ @[simp , tidy] def is_coalgebra_homomorphism (ϕ : 𝔸 → Β) : Prop := Β.α ∘ ϕ = (F.map ϕ) ∘ 𝔸.α /-- The subtype of coalgebra-homomorphism -/ def homomorphism (𝔸 Β : Coalgebra F) : set (𝔸 → Β):= λ ϕ , is_coalgebra_homomorphism ϕ /-- Allows us to use a homomorphism as a map-/ instance homomorphism_to_map (𝔸 Β : Coalgebra F) : has_coe_to_fun (homomorphism 𝔸 Β) := { F := λ _, 𝔸 → Β, coe := λ m, m.val} /-- A proof that id is a coalgebra-homomorphism -/ lemma id_is_hom (𝔸 : Coalgebra F): is_coalgebra_homomorphism (@id 𝔸) := show 𝔸.α ∘ id = (F.map id) ∘ 𝔸.α, from calc 𝔸.α ∘ id = (𝟙 (F.obj 𝔸)) ∘ 𝔸.α : rfl ... = (F.map (𝟙 𝔸)) ∘ 𝔸.α : by rw ← functor.map_id' F 𝔸 /-- A proof that composition of two homomorphism is a coalgebra-homomorphism -/ lemma comp_is_hom (φ : homomorphism 𝔸 Β) (ψ : homomorphism Β ℂ) : is_coalgebra_homomorphism (ψ ∘ φ) := have ab : Β.α ∘ φ = F.map φ ∘ 𝔸.α := φ.property, have bc : ℂ.α ∘ ψ = F.map ψ ∘ Β.α := ψ.property, calc (ℂ.α ∘ ψ) ∘ φ = (F.map ψ) ∘ Β.α ∘ φ : by rw bc ... = (F.map ψ) ∘ (F.map φ) ∘ 𝔸.α : by rw ab ... = ((F.map ψ) ⊚ (F.map φ)) ∘ 𝔸.α : rfl ... = (F.map (ψ ⊚ φ)) ∘ 𝔸.α : by rw ← functor.map_comp /-- The category of Coalgebras of the signature F (Set-F) -/ instance coalgebra_category : category (Coalgebra F) := { hom := λ 𝔸 Β, homomorphism 𝔸 Β, id := λ 𝔸 , ⟨@id 𝔸, id_is_hom 𝔸⟩ , comp := λ 𝔸 Β ℂ φ ψ, ⟨ψ ∘ φ, comp_is_hom φ ψ⟩ } instance set_F_to_set : has_coe (𝔸 ⟶ Β) (𝔸.carrier ⟶ Β.carrier) := ⟨λ ϕ :𝔸 ⟶ Β, ϕ⟩ open function lemma empty_hom_dom (ϕ : 𝔸.carrier ⟶ Β.carrier) (em_𝔸 : ¬ nonempty 𝔸): is_coalgebra_homomorphism ϕ := begin dsimp at *, ext1, have ex : ∃ a : 𝔸 , true := exists.intro x trivial, exact absurd (nonempty_of_exists ex) em_𝔸 end lemma empty_hom_codom (ϕ : 𝔸.carrier ⟶ Β.carrier) (em_Β : ¬ nonempty Β): is_coalgebra_homomorphism ϕ := begin dsimp at *, ext1, have ex : ∃ b : Β , true := exists.intro (ϕ x) trivial, exact absurd (nonempty_of_exists ex) em_Β end noncomputable def empty_map (S : Type u) (emp : ¬ nonempty S) (X : Type u): S → X := graph_to_map (λ s fs, false) -- ∀ a : A , ∃! b : B, G a b (λ s, absurd (⟨s⟩ : nonempty S) emp) lemma not_nonempty_empty : ¬ nonempty (∅: set 𝔸) := assume ⟨s⟩, by tidy noncomputable def empty_coalgebra : Coalgebra F := { carrier := (∅ : set 𝔸), α := empty_map (∅ : set 𝔸) not_nonempty_empty (F.obj (∅ : set 𝔸)) } /-- The inverse of homomorphism is homomorphism if the the homomorphism is bijective. -/ theorem bij_inverse_of_hom_is_hom (φ : homomorphism 𝔸 Β) (bij : bijective φ) : let inv : Β → 𝔸 := some (bijective_iff_has_inverse.1 bij) in is_coalgebra_homomorphism inv := begin intro inv, let hom :Β.α ∘ φ = F.map φ ∘ 𝔸.α := φ.property, have has_lr_inv :left_inverse inv φ ∧ right_inverse inv φ := some_spec (bijective_iff_has_inverse.1 bij), calc 𝔸.α ∘ inv = id ∘ 𝔸.α ∘ inv : rfl ... = (𝟙 (F.obj 𝔸)) ∘ 𝔸.α ∘ inv : rfl ... = (F.map (𝟙 𝔸)) ∘ 𝔸.α ∘ inv : by rw ← functor.map_id' ... = (F.map id) ∘ 𝔸.α ∘ inv : rfl ... = (F.map (inv ∘ φ)) ∘ 𝔸.α ∘ inv : by rw id_of_left_inverse has_lr_inv.1 ... = (F.map (inv ⊚ φ)) ∘ 𝔸.α ∘ inv : rfl ... = ((F.map inv) ⊚ (F.map φ)) ∘ 𝔸.α ∘ inv : by rw ← functor.map_comp ... = (F.map inv) ∘ ((F.map φ) ∘ 𝔸.α) ∘ inv : rfl ... = (F.map inv) ∘ (Β.α ∘ φ) ∘ inv : by rw hom ... = (F.map inv) ∘ Β.α ∘ (φ ∘ inv) : rfl ... = ((F.map inv) ∘ Β.α ∘ id) : by rw id_of_right_inverse has_lr_inv.2 ... = (F.map inv) ∘ Β.α : rfl end /-- Let 𝔸, Β, and ℂ be coalgebras and f : A → B, g : B → C set maps, so that ϕ := g ∘ f : A → C is a homomorphism. If f is a surjective homomorphism, then g is a homomorphism. -/ lemma surj_to_hom (f : 𝔸.carrier ⟶ Β.carrier) (g : Β.carrier ⟶ ℂ.carrier) (hom_gf : is_coalgebra_homomorphism (g ∘ f)) (hom_f : is_coalgebra_homomorphism f) (ep : epi f) : is_coalgebra_homomorphism g := have h1 : (ℂ.α ∘ g) ⊚ f = (F.map g ∘ Β.α) ⊚ f := calc (ℂ.α ∘ g) ⊚ f = F.map (g ⊚ f) ∘ 𝔸.α : hom_gf ... = (F.map g ⊚ F.map f) ∘ 𝔸.α : by rw functor.map_comp ... = F.map g ∘ F.map f ∘ 𝔸.α : by tidy ... = F.map g ∘ Β.α ∘ f : by rw [eq.symm hom_f], right_cancel f h1 /-- If g is an injective homomorphismus, then f is a homomorphism. -/ lemma inj_to_hom (f : 𝔸.carrier → Β.carrier) (g : Β.carrier → ℂ.carrier) (hom_gf : is_coalgebra_homomorphism (g ∘ f)) (hom_g : is_coalgebra_homomorphism g) (inj : injective g) : is_coalgebra_homomorphism f := begin cases classical.em (nonempty Β) with n_em_Β emp_Β, have h1 : (F.map g) ⊚ (F.map f) ⊚ 𝔸.α = F.map g ⊚ (Β.α ⊚ f) := calc ((F.map g) ⊚ (F.map f)) ∘ 𝔸.α = (F.map (g ⊚ f)) ∘ 𝔸.α : by rw functor.map_comp ... = F.map (g ∘ f) ∘ 𝔸.α : rfl ... = ℂ.α ∘ g ∘ f : by rw [eq.symm hom_gf] ... = (F.map g ∘ Β.α) ∘ f : by rw [eq.symm hom_g], haveI inh_Β : inhabited Β := ⟨choice n_em_Β⟩, haveI fg_mono : mono (F.map g) := mono_preserving_functor g inj, exact left_cancel (F.map g) (eq.symm h1), exact empty_hom_codom f emp_Β end /-- Let ϕ : 𝔸 → Β, ψ : 𝔸 → ℂ be homomorphisms, and ϕ is surjective. Then there exists a unique homomorphism χ : Β ⟶ ℂ, such that χ ∘ ϕ = ψ iff kern ϕ ⊆ kern ψ -/ lemma coalgebra_diagram (ϕ : homomorphism 𝔸 Β) (ψ : homomorphism 𝔸 ℂ) (sur : surjective ϕ) : (∃! χ : homomorphism Β ℂ , χ ∘ ϕ = ψ) ↔ (sub_kern ϕ ψ) := iff.intro -- The "exists such χ" → "kern ϕ ⊆ kern ψ" direction: begin intro ex, cases ex with χ h1, exact h1.left ▸ (kern_comp ϕ χ) end -- The "kern ϕ ⊆ kern ψ" → "exists a unique χ" direction: begin intro k, -- using the diagram lemma of Set-Category to -- prove the existance and uniqueness of such morphism have ex_uni : ∃ χ : Β → ℂ, (χ ∘ ϕ = ψ ∧ ∀ χ₁, χ₁ ∘ ϕ = ψ → χ₁ = χ) := (diagram_surjective ϕ ψ sur).2 k, cases ex_uni with χ spec, -- χ : Β → ℂ -- spec : χ ∘ ϕ = ψ ∧ ∀ χ₁, χ₁ ∘ ϕ = ψ → χ₁ = χ have hom_χ_ϕ : is_coalgebra_homomorphism (χ ∘ ϕ) := (eq.symm spec.left) ▸ ψ.property, have hom_χ : is_coalgebra_homomorphism χ := surj_to_hom ϕ χ hom_χ_ϕ ϕ.property ((epi_iff_surjective ϕ).2 sur), have unique : ∀ (χ₁ : homomorphism Β ℂ), χ₁ ∘ ϕ = ψ → χ₁ = ⟨χ , hom_χ⟩ := by tidy, exact exists_unique.intro ⟨χ , hom_χ⟩ spec.left unique end /-- Let Q be a nonempty set, ϕ : 𝔸 ⟶ Β be a homomorphisms, f : A → Q and g : Q → B be maps with ϕ = g ∘ f and f surjective and g injective Then there exists a unique α_Q : Q → F(Q) coalgebra structure such that both f and g are homomorphisms. -/ theorem Factorization {Q : Type u} (ϕ : homomorphism 𝔸 Β) (f : 𝔸.carrier ⟶ Q) (g : Q ⟶ Β.carrier) (h : ϕ.val = g ∘ f) (ep : epi f) (inj : injective g): ∃! α_Q : Q ⟶ F.obj Q , @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f ∧ @is_coalgebra_homomorphism F ⟨Q , α_Q⟩ Β g := begin cases classical.em (nonempty 𝔸) with n_em_𝔸 emp_𝔸, haveI inh_𝔸 : inhabited 𝔸 := ⟨choice n_em_𝔸⟩ , haveI inh_ℚ : inhabited Q := ⟨f (default 𝔸)⟩, let hom_ϕ := ϕ.property, /- Using the E-M-Square A ⟶(f)⟶ Q ⟶(Β.α ∘ g)⟶ F(B), A ⟶(F f ∘ α_𝔸)⟶ F(Q) ⟶(F g)⟶ F(B) -/ /- showing that the triangles commute -/ have commute : (Β.α ∘ g) ∘ f = (F.map g) ∘ ((F.map f) ∘ 𝔸.α) := calc Β.α ∘ g ∘ f = Β.α ∘ ϕ.val : by simp [h] ... = (F.map ϕ.val) ∘ 𝔸.α : hom_ϕ ... = (F.map (g ∘ f)) ∘ 𝔸.α : by rw [h] ... = (F.map (g ⊚ f)) ∘ 𝔸.α : rfl ... = ((F.map g) ⊚ (F.map f)) ∘ 𝔸.α : by rw functor.map_comp ... = (F.map g) ∘ (F.map f) ∘ 𝔸.α : by simp, /- we get the existance and the uniqueness of d the diagonal of the square and the coalgebra structure -/ have em_square : _ := E_M_square f ep (Β.α ∘ g) ((F.map f) ∘ 𝔸.α) (F.map g) (mono_preserving_functor g inj) commute, cases em_square with d spec, have uni_d : _ := spec.2, let ℚ : Coalgebra F := ⟨Q , d⟩, have homomorphism_f : @is_coalgebra_homomorphism F 𝔸 ℚ f := eq.symm spec.left.left, have homomorphism_g : @is_coalgebra_homomorphism F ℚ Β g := spec.left.right, have unique : ∀ (α_Q : Q ⟶ F.obj Q), @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f ∧ @is_coalgebra_homomorphism F ⟨Q , α_Q⟩ Β g → α_Q = d := assume α_Q ⟨hom_f , hom_g⟩, uni_d α_Q ⟨eq.symm hom_f , hom_g⟩, exact exists_unique.intro d ⟨homomorphism_f , homomorphism_g⟩ unique, have A_Q : nonempty Q → nonempty 𝔸 := λ n_Q, ⟨some ((epi_iff_surjective f).1 ep (choice n_Q))⟩, have em_Q : nonempty Q → false := λ n_Q, emp_𝔸 (A_Q n_Q), have n_Q : ¬ (nonempty Q) := em_Q, let α_Q : Q → F.obj Q := empty_map Q n_Q (F.obj Q), have hom_f : @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f := empty_hom_dom f emp_𝔸, exact exists_unique.intro α_Q ⟨hom_f , by tidy⟩ (by tidy) end theorem factorization {Q : Type u} (ϕ : homomorphism 𝔸 Β) (f : 𝔸.carrier ⟶ Q) (g : Q ⟶ Β.carrier) (h : ϕ.val = g ∘ f) (sur : surjective f) (inj : injective g) : (∃! α_Q : Q ⟶ F.obj Q , @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f) := begin cases classical.em (nonempty 𝔸) with n_em_𝔸 emp_𝔸, haveI inh : inhabited 𝔸 := ⟨choice n_em_𝔸⟩ , let hom_ϕ := ϕ.property, /- Using the E-M-Square A ⟶(f)⟶ Q ⟶(Β.α ∘ g)⟶ F(B), A ⟶(F f ∘ α_𝔸)⟶ F(Q) ⟶(F g)⟶ F(B) -/ /- showing that the triangles commute -/ have commute : (Β.α ∘ g) ∘ f = (F.map g) ∘ ((F.map f) ∘ 𝔸.α) := calc Β.α ∘ g ∘ f = Β.α ∘ ϕ.val : by simp [h] ... = (F.map ϕ.val) ∘ 𝔸.α : hom_ϕ ... = (F.map (g ∘ f)) ∘ 𝔸.α : by rw [h] ... = (F.map (g ⊚ f)) ∘ 𝔸.α : rfl ... = ((F.map g) ⊚ (F.map f)) ∘ 𝔸.α : by rw functor.map_comp ... = (F.map g) ∘ (F.map f) ∘ 𝔸.α : by simp, haveI inh_Q : inhabited Q := ⟨f (default 𝔸)⟩ , haveI epi_f : epi f := (epi_iff_surjective f).2 sur, haveI mono_Fg : mono (F.map g) := mono_preserving_functor g inj, /- we get the existance and the uniqueness of d the diagonal of the square and the coalgebra structure -/ have em_square : _ := E_M_square f epi_f (Β.α ∘ g) ((F.map f) ∘ 𝔸.α) (F.map g) mono_Fg commute, cases em_square with d spec, have homomorphism_f : d ⊚ f = (F.map f) ⊚ 𝔸.α := eq.symm spec.left.left, have com_tri_epi := commutative_triangles_epi f (Β.α ∘ g) ((F.map f) ∘ 𝔸.α) (F.map g) commute d (eq.symm homomorphism_f), have uni_f : ∀ (α_Q : Q ⟶ F.obj Q), @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f → α_Q = d := assume α_Q hom_f, have com : α_Q ⊚ f = F.map f ∘ 𝔸.α := hom_f, com_tri_epi.1 α_Q (eq.symm com), exact exists_unique.intro d homomorphism_f uni_f, have A_Q : nonempty Q → nonempty 𝔸 := λ n_Q, ⟨some (sur (choice n_Q))⟩, have em_Q : nonempty Q → false := λ n_Q, emp_𝔸 (A_Q n_Q), have n_Q : ¬ (nonempty Q) := em_Q, let α_Q : Q → F.obj Q := empty_map Q n_Q (F.obj Q), have hom_f : @is_coalgebra_homomorphism F 𝔸 ⟨Q , α_Q⟩ f := empty_hom_dom f emp_𝔸, exact exists_unique.intro α_Q hom_f (by tidy) end theorem factorization_hom {Q : Type u} (ϕ : homomorphism 𝔸 Β) (f : 𝔸.carrier ⟶ Q) (g : Q ⟶ Β.carrier) (h : ϕ.val = g ∘ f) (sur : surjective f) (inj : injective g) [inhabited 𝔸] : let α_Q := some (factorization ϕ f g h sur inj) in @is_coalgebra_homomorphism F ⟨Q , α_Q⟩ Β g := begin intros α_Q, have hom_ϕ := ϕ.property, have commute : (Β.α ∘ g) ∘ f = (F.map g) ∘ ((F.map f) ∘ 𝔸.α) := calc Β.α ∘ g ∘ f = Β.α ∘ ϕ.val : by simp [h] ... = (F.map ϕ.val) ∘ 𝔸.α : hom_ϕ ... = (F.map (g ∘ f)) ∘ 𝔸.α : by rw [h] ... = (F.map (g ⊚ f)) ∘ 𝔸.α : rfl ... = ((F.map g) ⊚ (F.map f)) ∘ 𝔸.α : by rw functor.map_comp ... = (F.map g) ∘ (F.map f) ∘ 𝔸.α : by simp, haveI inh_Q : inhabited Q := ⟨f (default 𝔸)⟩ , haveI epi_f : epi f := (epi_iff_surjective f).2 sur, haveI mono_Fg : mono (F.map g) := mono_preserving_functor g inj, have spec := some_spec (factorization ϕ f g h sur inj), have com_tri_epi : _ := commutative_triangles_epi f (Β.α ∘ g) ((F.map f) ∘ 𝔸.α) (F.map g) commute α_Q (eq.symm spec.1), exact com_tri_epi.2 end end coalgebra
ab4182e3b615051070dd08a47ac2fd9e02574e0e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/group_action/group.lean
be6086db460c178fb24d563ffb6b8592afb9d71e
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
12,199
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.hom.aut import group_theory.group_action.units /-! # Group actions applied to various types of group > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ open function universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action /-- `monoid.to_mul_action` is faithful on cancellative monoids. -/ @[to_additive /-" `add_monoid.to_add_action` is faithful on additive cancellative monoids. "-/] instance right_cancel_monoid.to_has_faithful_smul [right_cancel_monoid α] : has_faithful_smul α α := ⟨λ x y h, mul_right_cancel (h 1)⟩ section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive, simps] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm /-- `mul_action.to_perm` is injective on faithful actions. -/ @[to_additive "`add_action.to_perm` is injective on faithful actions."] lemma mul_action.to_perm_injective [has_faithful_smul α β] : function.injective (mul_action.to_perm : α → equiv.perm β) := (show function.injective (equiv.to_fun ∘ mul_action.to_perm), from smul_left_injective').of_comp variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } /-- The tautological action by `equiv.perm α` on `α`. This generalizes `function.End.apply_mul_action`.-/ instance equiv.perm.apply_mul_action (α : Type*) : mul_action (equiv.perm α) α := { smul := λ f a, f a, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma equiv.perm.smul_def {α : Type*} (f : equiv.perm α) (a : α) : f • a = f a := rfl /-- `equiv.perm.apply_mul_action` is faithful. -/ instance equiv.perm.apply_has_faithful_smul (α : Type*) : has_faithful_smul (equiv.perm α) α := ⟨λ x y, equiv.ext⟩ variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] lemma smul_zpow [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) (p : ℤ) : (c • x) ^ p = c ^ p • x ^ p := by { cases p; simp [smul_pow, smul_inv] } @[simp] lemma commute.smul_right_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute a (r • b) ↔ commute a b := ⟨λ h, inv_smul_smul r b ▸ h.smul_right r⁻¹, λ h, h.smul_right r⟩ @[simp] lemma commute.smul_left_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute (r • a) b ↔ commute a b := by rw [commute.symm_iff, commute.smul_right_iff, commute.symm_iff] @[to_additive] protected lemma mul_action.bijective (g : α) : bijective ((•) g : β → β) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : injective ((•) g : β → β) := (mul_action.bijective g).injective @[to_additive] protected lemma mul_action.surjective (g : α) : surjective ((•) g : β → β) := (mul_action.bijective g).surjective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group /-- `monoid.to_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_smul [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_smul α α := ⟨λ x y h, mul_left_injective₀ one_ne_zero (h 1)⟩ section gwz variables [group_with_zero α] [mul_action α β] {a : α} @[simp] lemma inv_smul_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply @[simp] lemma commute.smul_right_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute a (c • b) ↔ commute a b := commute.smul_right_iff (units.mk0 c hc) @[simp] lemma commute.smul_left_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute (c • a) b ↔ commute a b := commute.smul_left_iff (units.mk0 c hc) protected lemma mul_action.bijective₀ (ha : a ≠ 0) : bijective ((•) a : β → β) := mul_action.bijective $ units.mk0 a ha protected lemma mul_action.injective₀ (ha : a ≠ 0) : injective ((•) a : β → β) := (mul_action.bijective₀ ha).injective protected lemma mul_action.surjective₀ (ha : a ≠ 0) : surjective ((•) a : β → β) := (mul_action.bijective₀ ha).surjective end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] variables (β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def distrib_mul_action.to_add_equiv (x : α) : β ≃+ β := { .. distrib_mul_action.to_add_monoid_hom β x, .. mul_action.to_perm_hom α β x } /-- Each non-zero element of a `group_with_zero` defines an additive monoid isomorphism of an `add_monoid` on which it acts distributively. This is a stronger version of `distrib_mul_action.to_add_monoid_hom`. -/ def distrib_mul_action.to_add_equiv₀ {α : Type*} (β : Type*) [group_with_zero α] [add_monoid β] [distrib_mul_action α β] (x : α) (hx : x ≠ 0) : β ≃+ β := { inv_fun := λ b, x⁻¹ • b, left_inv := inv_smul_smul₀ hx, right_inv := smul_inv_smul₀ hx, .. distrib_mul_action.to_add_monoid_hom β x, } variables (α β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def distrib_mul_action.to_add_aut : α →* add_aut β := { to_fun := distrib_mul_action.to_add_equiv β, map_one' := add_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, add_equiv.ext (mul_smul _ _) } variables {α β} theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := show units.mk0 a ha • x = 0 ↔ x = 0, from smul_eq_zero_iff_eq _ theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := show units.mk0 a ha • x ≠ 0 ↔ x ≠ 0, from smul_ne_zero_iff_ne _ end gwz end distrib_mul_action section mul_distrib_mul_action variables [group α] [monoid β] [mul_distrib_mul_action α β] variables (β) /-- Each element of the group defines a multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def mul_distrib_mul_action.to_mul_equiv (x : α) : β ≃* β := { .. mul_distrib_mul_action.to_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def mul_distrib_mul_action.to_mul_aut : α →* mul_aut β := { to_fun := mul_distrib_mul_action.to_mul_equiv β, map_one' := mul_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, mul_equiv.ext (mul_smul _ _) } variables {α β} end mul_distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[to_additive arrow_add_action "If `G` acts on `A`, then it acts also on `A → B`, by `(g +ᵥ F) a = F (g⁻¹ +ᵥ a)`", simps] def arrow_action {G A B : Type*} [division_monoid G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [inv_one, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- When `B` is a monoid, `arrow_action` is additionally a `mul_distrib_mul_action`. -/ def arrow_mul_distrib_mul_action {G A B : Type*} [group G] [mul_action G A] [monoid B] : mul_distrib_mul_action G (A → B) := { smul_one := λ g, rfl, smul_mul := λ g f₁ f₂, rfl } local attribute [instance] arrow_mul_distrib_mul_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [monoid H] : G →* mul_aut (A → H) := mul_distrib_mul_action.to_mul_aut _ _ end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ show u • x = 0 ↔ x = 0, from smul_eq_zero_iff_eq u end distrib_mul_action end is_unit section smul variables [group α] [monoid β] @[simp] lemma is_unit_smul_iff [mul_action α β] [smul_comm_class α β β] [is_scalar_tower α β β] (g : α) (m : β) : is_unit (g • m) ↔ is_unit m := ⟨λ h, inv_smul_smul g m ▸ h.smul g⁻¹, is_unit.smul g⟩ lemma is_unit.smul_sub_iff_sub_inv_smul [add_group β] [distrib_mul_action α β] [is_scalar_tower α β β] [smul_comm_class α β β] (r : α) (a : β) : is_unit (r • 1 - a) ↔ is_unit (1 - r⁻¹ • a) := by rw [←is_unit_smul_iff r (1 - r⁻¹ • a), smul_sub, smul_inv_smul] end smul
9292c604af0426d10090da72102ed983fe67f1dd
4727251e0cd73359b15b664c3170e5d754078599
/src/data/nat/sqrt_norm_num.lean
8f63384879b0208b33d37b17692e713a18282812
[ "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,358
lean
/- Copyright (c) 2022 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.norm_num import data.nat.sqrt /-! ### `norm_num` plugin for `sqrt` The `norm_num` plugin evaluates `sqrt` by bounding it between consecutive integers. -/ namespace norm_num open tactic nat lemma is_sqrt {n a a2 b : ℕ} (ha2 : a * a = a2) (hb : a2 + b = n) (hle : b ≤ bit0 a) : sqrt n = a := by { rw [← hb, ← ha2, ← pow_two], exact sqrt_add_eq' _ hle } /-- Given `n` provides `(a, ⊢ nat.sqrt n = a)`. -/ meta def prove_sqrt (ic : instance_cache) (n : expr) : tactic (instance_cache × expr × expr) := do nn ← n.to_nat, let na := nn.sqrt, (ic, a) ← ic.of_nat na, (ic, a2, ha2) ← prove_mul_nat ic a a, (ic, b) ← ic.of_nat (nn - na*na), (ic, hb) ← prove_add_nat ic a2 b n, (ic, hle) ← prove_le_nat ic b (`(bit0:ℕ→ℕ).mk_app [a]), pure (ic, a, `(@is_sqrt).mk_app [n, a, a2, b, ha2, hb, hle]) /-- A `norm_num` plugin for `sqrt n` when `n` is a numeral. -/ @[norm_num] meta def eval_sqrt : expr → tactic (expr × expr) | `(sqrt %%en) := do n ← en.to_nat, match n with | 0 := pure (`(0:ℕ), `(sqrt_zero)) | _ := do c ← mk_instance_cache `(ℕ), prod.snd <$> prove_sqrt c en end | _ := failed end norm_num
5fc3e98341b42a1e74e2fe4f5ded8cd6e2ea31b2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/closed/monoidal_auto.lean
04114aae46aacc32c5cce9236fcd97baf3707487
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,656
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.monoidal.category import Mathlib.category_theory.adjunction.basic import Mathlib.PostPort universes v u l namespace Mathlib /-! # Closed monoidal categories Define (right) closed objects and (right) closed monoidal categories. ## TODO Some of the theorems proved about cartesian closed categories should be generalised and moved to this file. -/ namespace category_theory /-- An object `X` is (right) closed if `(X ⊗ -)` is a left adjoint. -/ class closed {C : Type u} [category C] [monoidal_category C] (X : C) where is_adj : is_left_adjoint (monoidal_category.tensor_left X) /-- A monoidal category `C` is (right) monoidal closed if every object is (right) closed. -/ class monoidal_closed (C : Type u) [category C] [monoidal_category C] where closed : (X : C) → closed X /-- The unit object is always closed. This isn't an instance because most of the time we'll prove closedness for all objects at once, rather than just for this one. -/ def unit_closed {C : Type u} [category C] [monoidal_category C] : closed 𝟙_ := closed.mk (is_left_adjoint.mk 𝟭 (adjunction.mk_of_hom_equiv (adjunction.core_hom_equiv.mk fun (X _x : C) => equiv.mk (fun (a : functor.obj (monoidal_category.tensor_left 𝟙_) X ⟶ _x) => iso.inv λ_ ≫ a) (fun (a : X ⟶ functor.obj 𝟭 _x) => iso.hom λ_ ≫ a) sorry sorry))) end Mathlib
ad04a27d6eb7cd4a44f25607f4c4810befbbd732
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/list/qsort_auto.lean
9b728401b11cbd27ddf8b89f7b1c80773f80c58d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,291
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.lemmas import Mathlib.Lean3Lib.init.wf universes u_1 namespace Mathlib namespace list -- Note: we can't use the equation compiler here because -- init.meta.well_founded_tactics uses this file def qsort.F {α : Type u_1} (lt : α → α → Bool) (x : List α) : ((y : List α) → length y < length x → List α) → List α := sorry /- This is based on the minimalist Haskell "quicksort". Remark: this is *not* really quicksort since it doesn't partition the elements in-place -/ def qsort {α : Type u_1} (lt : α → α → Bool) : List α → List α := well_founded.fix sorry sorry @[simp] theorem qsort_nil {α : Type u_1} (lt : α → α → Bool) : qsort lt [] = [] := sorry @[simp] theorem qsort_cons {α : Type u_1} (lt : α → α → Bool) (h : α) (t : List α) : qsort lt (h :: t) = (fun (_a : List α × List α) => prod.cases_on _a fun (fst snd : List α) => idRhs (List α) (qsort lt snd ++ h :: qsort lt fst)) (partition (fun (x : α) => lt h x = tt) t) := sorry end Mathlib
665762a682339bec42f8f70fa887bc3d8e7dee59
42610cc2e5db9c90269470365e6056df0122eaa0
/hott/homotopy/join.hlean
f10888354774aa9637f99940c2793ccdf102e10b
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
22,664
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz Declaration of a join as a special case of a pushout -/ import hit.pushout .sphere cubical.cube open eq function prod equiv is_trunc bool sigma.ops definition join (A B : Type) : Type := @pushout.pushout (A × B) A B pr1 pr2 namespace join section variables {A B : Type} definition inl (a : A) : join A B := @pushout.inl (A × B) A B pr1 pr2 a definition inr (b : B) : join A B := @pushout.inr (A × B) A B pr1 pr2 b definition glue (a : A) (b : B) : inl a = inr b := @pushout.glue (A × B) A B pr1 pr2 (a, b) protected definition rec {P : join A B → Type} (Pinl : Π(x : A), P (inl x)) (Pinr : Π(y : B), P (inr y)) (Pglue : Π(x : A)(y : B), Pinl x =[glue x y] Pinr y) (z : join A B) : P z := pushout.rec Pinl Pinr (prod.rec Pglue) z protected definition rec_glue {P : join A B → Type} (Pinl : Π(x : A), P (inl x)) (Pinr : Π(y : B), P (inr y)) (Pglue : Π(x : A)(y : B), Pinl x =[glue x y] Pinr y) (x : A) (y : B) : apd (join.rec Pinl Pinr Pglue) (glue x y) = Pglue x y := !quotient.rec_eq_of_rel protected definition elim {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Π(x : A)(y : B), Pinl x = Pinr y) (z : join A B) : P := join.rec Pinl Pinr (λx y, pathover_of_eq (Pglue x y)) z protected definition elim_glue {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Π(x : A)(y : B), Pinl x = Pinr y) (x : A) (y : B) : ap (join.elim Pinl Pinr Pglue) (glue x y) = Pglue x y := begin apply equiv.eq_of_fn_eq_fn_inv !(pathover_constant (glue x y)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑join.elim], apply join.rec_glue end protected definition elim_ap_inl {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Π(x : A)(y : B), Pinl x = Pinr y) {a a' : A} (p : a = a') : ap (join.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p := by cases p; reflexivity protected definition hsquare {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (ap inl p) (ap inr q) (glue a b) (glue a' b') := eq.rec_on p (eq.rec_on q hrfl) protected definition vsquare {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (glue a b) (glue a' b') (ap inl p) (ap inr q) := eq.rec_on p (eq.rec_on q vrfl) end end join attribute join.inl join.inr [constructor] attribute join.rec [recursor] attribute join.elim [recursor 7] attribute join.rec join.elim [unfold 7] /- Diamonds in joins -/ namespace join variables {A B : Type} protected definition diamond (a a' : A) (b b' : B) := square (glue a b) (glue a' b')⁻¹ (glue a b') (glue a' b)⁻¹ protected definition hdiamond {a a' : A} (b b' : B) (p : a = a') : join.diamond a a' b b' := begin cases p, unfold join.diamond, assert H : (glue a b' ⬝ (glue a b')⁻¹ ⬝ (glue a b)⁻¹⁻¹) = glue a b, { rewrite [con.right_inv,inv_inv,idp_con] }, exact H ▸ top_deg_square (glue a b') (glue a b')⁻¹ (glue a b)⁻¹, end protected definition vdiamond (a a' : A) {b b' : B} (q : b = b') : join.diamond a a' b b' := begin cases q, unfold join.diamond, assert H : (glue a b ⬝ (glue a' b)⁻¹ ⬝ (glue a' b)⁻¹⁻¹) = glue a b, { rewrite [con.assoc,con.right_inv] }, exact H ▸ top_deg_square (glue a b) (glue a' b)⁻¹ (glue a' b)⁻¹ end protected definition symm_diamond (a : A) (b : B) : join.vdiamond a a idp = join.hdiamond b b idp := begin unfold join.hdiamond, unfold join.vdiamond, assert H : Π{X : Type} ⦃x y : X⦄ (p : x = y), eq.rec (eq.rec (refl p) (symm (con.right_inv p⁻¹))) (symm (con.assoc p p⁻¹ p⁻¹⁻¹)) ▸ top_deg_square p p⁻¹ p⁻¹ = eq.rec (eq.rec (eq.rec (refl p) (symm (idp_con p))) (symm (inv_inv p))) (symm (con.right_inv p)) ▸ top_deg_square p p⁻¹ p⁻¹ :> square p p⁻¹ p p⁻¹, { intros X x y p, cases p, reflexivity }, apply H (glue a b) end end join namespace join variables {A₁ A₂ B₁ B₂ : Type} protected definition functor [reducible] (f : A₁ → A₂) (g : B₁ → B₂) : join A₁ B₁ → join A₂ B₂ := begin intro x, induction x with a b a b, { exact inl (f a) }, { exact inr (g b) }, { apply glue } end protected definition ap_diamond (f : A₁ → A₂) (g : B₁ → B₂) {a a' : A₁} {b b' : B₁} : join.diamond a a' b b' → join.diamond (f a) (f a') (g b) (g b') := begin unfold join.diamond, intro s, note s' := aps (join.functor f g) s, do 2 rewrite eq.ap_inv at s', do 4 rewrite join.elim_glue at s', exact s' end protected definition equiv_closed : A₁ ≃ A₂ → B₁ ≃ B₂ → join A₁ B₁ ≃ join A₂ B₂ := begin intros H K, fapply equiv.MK, { intro x, induction x with a b a b, { exact inl (to_fun H a) }, { exact inr (to_fun K b) }, { apply glue } }, { intro y, induction y with a b a b, { exact inl (to_inv H a) }, { exact inr (to_inv K b) }, { apply glue } }, { intro y, induction y with a b a b, { apply ap inl, apply to_right_inv }, { apply ap inr, apply to_right_inv }, { apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), do 2 krewrite join.elim_glue, apply join.hsquare } }, { intro x, induction x with a b a b, { apply ap inl, apply to_left_inv }, { apply ap inr, apply to_left_inv }, { apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), do 2 krewrite join.elim_glue, apply join.hsquare } } end protected definition twist_diamond {A : Type} {a a' : A} (p : a = a') : pathover (λx, join.diamond a' x a x) (join.vdiamond a' a idp) p (join.hdiamond a a' idp) := begin cases p, apply pathover_idp_of_eq, apply join.symm_diamond end protected definition empty (A : Type) : join empty A ≃ A := begin fapply equiv.MK, { intro x, induction x with z a z a, { induction z }, { exact a }, { induction z } }, { intro a, exact inr a }, { intro a, reflexivity }, { intro x, induction x with z a z a, { induction z }, { reflexivity }, { induction z } } end protected definition bool (A : Type) : join bool A ≃ susp A := begin fapply equiv.MK, { intro ba, induction ba with [b, a, b, a], { induction b, exact susp.south, exact susp.north }, { exact susp.north }, { induction b, esimp, { apply inverse, apply susp.merid, exact a }, { reflexivity } } }, { intro s, induction s with a, { exact inl tt }, { exact inl ff }, { exact (glue tt a) ⬝ (glue ff a)⁻¹ } }, { intro s, induction s with a, { reflexivity }, { reflexivity }, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (join.elim _ _ _)), rewrite [susp.elim_merid,ap_con,ap_inv], krewrite [join.elim_glue,join.elim_glue], esimp, rewrite [inv_inv,idp_con], apply hdeg_square, reflexivity } }, { intro ba, induction ba with [b, a, b, a], esimp, { induction b, do 2 reflexivity }, { apply glue }, { induction b, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (susp.elim _ _ _)), krewrite join.elim_glue, rewrite ap_inv, krewrite susp.elim_merid, apply square_of_eq_top, apply inverse, rewrite con.assoc, apply con.left_inv }, { esimp, apply eq_pathover, rewrite ap_id, rewrite (ap_compose' (susp.elim _ _ _)), krewrite join.elim_glue, esimp, apply square_of_eq_top, rewrite [idp_con,con.right_inv] } } } end end join namespace join variables (A B C : Type) protected definition is_contr [HA : is_contr A] : is_contr (join A B) := begin fapply is_contr.mk, exact inl (center A), intro x, induction x with a b a b, apply ap inl, apply center_eq, apply glue, apply pathover_of_tr_eq, apply concat, apply transport_eq_Fr, esimp, rewrite ap_id, generalize center_eq a, intro p, cases p, apply idp_con, end protected definition swap : join A B → join B A := begin intro x, induction x with a b a b, exact inr a, exact inl b, apply !glue⁻¹ end protected definition swap_involutive (x : join A B) : join.swap B A (join.swap A B x) = x := begin induction x with a b a b, do 2 reflexivity, apply eq_pathover, rewrite ap_id, apply hdeg_square, esimp[join.swap], apply concat, apply ap_compose' (join.elim _ _ _), krewrite [join.elim_glue, ap_inv, join.elim_glue], apply inv_inv, end protected definition symm : join A B ≃ join B A := by fapply equiv.MK; do 2 apply join.swap; do 2 apply join.swap_involutive end join /- This proves that the join operator is associative. The proof is more or less ported from Evan Cavallo's agda version: https://github.com/HoTT/HoTT-Agda/blob/master/homotopy/JoinAssocCubical.agda -/ namespace join section join_switch private definition massage_sq' {A : Type} {a₀₀ a₂₀ a₀₂ a₂₂ : A} {p₁₀ : a₀₀ = a₂₀} {p₁₂ : a₀₂ = a₂₂} {p₀₁ : a₀₀ = a₀₂} {p₂₁ : a₂₀ = a₂₂} (sq : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₀₁⁻¹ (p₂₁ ⬝ p₁₂⁻¹) idp := by induction sq; exact ids private definition massage_sq {A : Type} {a₀₀ a₂₀ a₀₂ : A} {p₁₀ : a₀₀ = a₂₀} {p₁₂ : a₀₂ = a₂₀} {p₀₁ : a₀₀ = a₀₂} (sq : square p₁₀ p₁₂ p₀₁ idp) : square p₁₀⁻¹ p₀₁⁻¹ p₁₂⁻¹ idp := !idp_con⁻¹ ⬝ph (massage_sq' sq) private definition ap_square_massage {A B : Type} (f : A → B) {a₀₀ a₀₂ a₂₀ : A} {p₀₁ : a₀₀ = a₀₂} {p₁₀ : a₀₀ = a₂₀} {p₁₁ : a₂₀ = a₀₂} (sq : square p₀₁ p₁₁ p₁₀ idp) : cube (hdeg_square (ap_inv f p₁₁)) ids (aps f (massage_sq sq)) (massage_sq (aps f sq)) (hdeg_square !ap_inv) (hdeg_square !ap_inv) := by apply rec_on_r sq; apply idc private definition massage_cube' {A : Type} {a₀₀₀ a₂₀₀ a₀₂₀ a₂₂₀ a₀₀₂ a₂₀₂ a₀₂₂ a₂₂₂ : A} {p₁₀₀ : a₀₀₀ = a₂₀₀} {p₀₁₀ : a₀₀₀ = a₀₂₀} {p₀₀₁ : a₀₀₀ = a₀₀₂} {p₁₂₀ : a₀₂₀ = a₂₂₀} {p₂₁₀ : a₂₀₀ = a₂₂₀} {p₂₀₁ : a₂₀₀ = a₂₀₂} {p₁₀₂ : a₀₀₂ = a₂₀₂} {p₀₁₂ : a₀₀₂ = a₀₂₂} {p₀₂₁ : a₀₂₀ = a₀₂₂} {p₁₂₂ : a₀₂₂ = a₂₂₂} {p₂₁₂ : a₂₀₂ = a₂₂₂} {p₂₂₁ : a₂₂₀ = a₂₂₂} {s₁₁₀ : square p₀₁₀ p₂₁₀ p₁₀₀ p₁₂₀} {s₁₁₂ : square p₀₁₂ p₂₁₂ p₁₀₂ p₁₂₂} {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} {s₂₁₁ : square p₂₁₀ p₂₁₂ p₂₀₁ p₂₂₁} {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ p₂₀₁} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ p₂₂₁} (c : cube s₀₁₁ s₂₁₁ s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) : cube (s₂₁₁ ⬝v s₁₁₂⁻¹ᵛ) vrfl (massage_sq' s₁₀₁) (massage_sq' s₁₂₁) s₁₁₀⁻¹ᵛ s₀₁₁⁻¹ᵛ := by cases c; apply idc private definition massage_cube {A : Type} {a₀₀₀ a₂₀₀ a₀₂₀ a₂₂₀ a₀₀₂ a₀₂₂ : A} {p₁₀₀ : a₀₀₀ = a₂₀₀} {p₀₁₀ : a₀₀₀ = a₀₂₀} {p₀₀₁ : a₀₀₀ = a₀₀₂} {p₁₂₀ : a₀₂₀ = a₂₂₀} {p₂₁₀ : a₂₀₀ = a₂₂₀} {p₁₀₂ : a₀₀₂ = a₂₀₀} {p₀₁₂ : a₀₀₂ = a₀₂₂} {p₀₂₁ : a₀₂₀ = a₀₂₂} {p₁₂₂ : a₀₂₂ = a₂₂₀} {s₁₁₀ : square p₀₁₀ _ _ _} {s₁₁₂ : square p₀₁₂ p₂₁₀ p₁₀₂ p₁₂₂} {s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} --{s₂₁₁ : square p₂₁₀ p₂₁₀ idp idp} {s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ idp} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ idp} (c : cube s₀₁₁ vrfl s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) : cube s₁₁₂⁻¹ᵛ vrfl (massage_sq s₁₀₁) (massage_sq s₁₂₁) s₁₁₀⁻¹ᵛ s₀₁₁⁻¹ᵛ := begin cases p₁₀₀, cases p₁₀₂, cases p₁₂₂, note c' := massage_cube' c, esimp[massage_sq], krewrite vdeg_v_eq_ph_pv_hp at c', exact c', end private definition massage_massage {A : Type} {a₀₀ a₀₂ a₂₀ : A} {p₀₁ : a₀₀ = a₀₂} {p₁₀ : a₀₀ = a₂₀} {p₁₁ : a₂₀ = a₀₂} (sq : square p₀₁ p₁₁ p₁₀ idp) : cube (hdeg_square !inv_inv) ids (massage_sq (massage_sq sq)) sq (hdeg_square !inv_inv) (hdeg_square !inv_inv) := by apply rec_on_r sq; apply idc private definition square_Flr_ap_idp_cube {A B : Type} {b : B} {f : A → B} {p₁ p₂ : Π a, f a = b} (α : Π a, p₁ a = p₂ a) {a₁ a₂ : A} (q : a₁ = a₂) : cube hrfl hrfl (square_Flr_ap_idp p₁ q) (square_Flr_ap_idp p₂ q) (hdeg_square (α _)) (hdeg_square (α _)) := by cases q; esimp[square_Flr_ap_idp]; apply deg3_cube; esimp variables {A B C : Type} definition switch_left [reducible] : join A B → join (join C B) A := begin intro x, induction x with a b a b, exact inr a, exact inl (inr b), apply !glue⁻¹, end private definition switch_coh_fill_square (a : A) (b : B) (c : C) := square (glue (inl c) a)⁻¹ (ap inl (glue c b))⁻¹ (ap switch_left (glue a b)) idp private definition switch_coh_fill_cube (a : A) (b : B) (c : C) (sq : switch_coh_fill_square a b c) := cube (hdeg_square !join.elim_glue) ids sq (massage_sq !square_Flr_ap_idp) hrfl hrfl private definition switch_coh_fill_type (a : A) (b : B) (c : C) := Σ sq : switch_coh_fill_square a b c, switch_coh_fill_cube a b c sq private definition switch_coh_fill (a : A) (b : B) (c : C) : switch_coh_fill_type a b c := by esimp; apply cube_fill101 private definition switch_coh (ab : join A B) (c : C) : switch_left ab = inl (inl c) := begin induction ab with a b a b, apply !glue⁻¹, apply (ap inl !glue)⁻¹, apply eq_pathover, refine _ ⬝hp !ap_constant⁻¹, apply !switch_coh_fill.1, end protected definition switch [reducible] : join (join A B) C → join (join C B) A := begin intro x, induction x with ab c ab c, exact switch_left ab, exact inl (inl c), exact switch_coh ab c, end private definition switch_inv_left_square (a : A) (b : B) : square idp idp (ap (!(@join.switch C) ∘ switch_left) (glue a b)) (ap inl (glue a b)) := begin refine hdeg_square !ap_compose ⬝h _, refine aps join.switch (hdeg_square !join.elim_glue) ⬝h _, esimp, refine hdeg_square !(ap_inv join.switch) ⬝h _, refine hrfl⁻¹ʰ⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left,switch_coh], refine (hdeg_square !join.elim_glue)⁻¹ᵛ ⬝h _, esimp, refine hrfl⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_coh_left (c : C) (a : A) : square idp idp (ap !(@join.switch C B) (switch_coh (inl a) c)) (glue (inl a) c) := begin refine hrfl ⬝h _, refine aps join.switch hrfl ⬝h _, esimp[switch_coh], refine hdeg_square !ap_inv ⬝h _, refine hrfl⁻¹ʰ⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left], refine (hdeg_square !join.elim_glue)⁻¹ᵛ ⬝h _, refine hrfl⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_coh_right (c : C) (b : B) : square idp idp (ap !(@join.switch _ _ A) (switch_coh (inr b) c)) (glue (inr b) c) := begin refine hrfl ⬝h _, refine aps join.switch hrfl ⬝h _, esimp[switch_coh], refine hdeg_square !ap_inv ⬝h _, refine (hdeg_square !ap_compose)⁻¹ʰ⁻¹ᵛ ⬝h _, refine hrfl⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left], refine (hdeg_square !join.elim_glue)⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv, end private definition switch_inv_left (ab : join A B) : !(@join.switch C) (join.switch (inl ab)) = inl ab := begin induction ab with a b a b, do 2 reflexivity, apply eq_pathover, exact !switch_inv_left_square, end section variables (a : A) (b : B) (c : C) private definition switch_inv_cube_aux1 {A B C : Type} {b : B} {f : A → B} (h : B → C) (g : Π a, f a = b) {x y : A} (p : x = y) : cube (hdeg_square (ap_compose h f p)) ids (square_Flr_ap_idp (λ a, ap h (g a)) p) (aps h (square_Flr_ap_idp _ _)) hrfl hrfl := by cases p; esimp[square_Flr_ap_idp]; apply deg2_cube; cases (g x); esimp private definition switch_inv_cube_aux2 {A B : Type} {b : B} {f : A → B} (g : Π a, f a = b) {x y : A} (p : x = y) {sq : square (g x) (g y) (ap f p) idp} (q : apd g p = eq_pathover (sq ⬝hp !ap_constant⁻¹)) : square_Flr_ap_idp _ _ = sq := begin cases p, esimp at *, apply concat, apply inverse, apply vdeg_square_idp, apply concat, apply ap vdeg_square, exact ap eq_of_pathover_idp q, krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)), exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq, end private definition switch_inv_cube (a : A) (b : B) (c : C) : cube (switch_inv_left_square a b) ids (square_Flr_ap_idp _ _) (square_Flr_ap_idp _ _) (switch_inv_coh_left c a) (switch_inv_coh_right c b) := begin esimp [switch_inv_coh_left, switch_inv_coh_right, switch_inv_left_square], apply cube_concat2, apply switch_inv_cube_aux1, apply cube_concat2, apply cube_transport101, apply inverse, apply ap (λ x, aps join.switch x), apply switch_inv_cube_aux2, apply join.rec_glue, apply apc, apply (switch_coh_fill a b c).2, apply cube_concat2, esimp, apply ap_square_massage, apply cube_concat2, apply massage_cube, apply cube_inverse2, apply switch_inv_cube_aux1, apply cube_concat2, apply massage_cube, apply square_Flr_ap_idp_cube, apply cube_concat2, apply massage_cube, apply cube_transport101, apply inverse, apply switch_inv_cube_aux2, esimp[switch_coh], apply join.rec_glue, apply (switch_coh_fill c b a).2, apply massage_massage, end end private definition pathover_of_triangle_cube {A B : Type} {b₀ b₁ : A → B} {b : B} {p₀₁ : Π a, b₀ a = b₁ a} {p₀ : Π a, b₀ a = b} {p₁ : Π a, b₁ a = b} {x y : A} {q : x = y} {sqx : square (p₀₁ x) idp (p₀ x) (p₁ x)} {sqy : square (p₀₁ y) idp (p₀ y) (p₁ y)} (c : cube (natural_square_tr _ _) ids (square_Flr_ap_idp p₀ q) (square_Flr_ap_idp p₁ q) sqx sqy) : sqx =[q] sqy := by cases q; apply pathover_of_eq_tr; apply eq_of_deg12_cube; exact c private definition pathover_of_ap_ap_square {A : Type} {x y : A} {p : x = y} (g : B → A) (f : A → B) {u : g (f x) = x} {v : g (f y) = y} (sq : square (ap g (ap f p)) p u v) : u =[p] v := by cases p; apply eq_pathover; apply transpose; exact sq private definition natural_square_tr_beta {A B : Type} {f₁ f₂ : A → B} (p : Π a, f₁ a = f₂ a) {x y : A} (q : x = y) {sq : square (p x) (p y) (ap f₁ q) (ap f₂ q)} (e : apd p q = eq_pathover sq) : natural_square_tr p q = sq := begin cases q, esimp at *, apply concat, apply inverse, apply vdeg_square_idp, apply concat, apply ap vdeg_square, apply ap eq_of_pathover_idp e, krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)), exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq, end private definition switch_inv_coh (c : C) (k : join A B) : square (switch_inv_left k) idp (ap join.switch (switch_coh k c)) (glue k c) := begin induction k with a b a b, apply switch_inv_coh_left, apply switch_inv_coh_right, refine pathover_of_triangle_cube _, esimp, apply cube_transport011, apply inverse, rotate 1, apply switch_inv_cube, apply natural_square_tr_beta, apply join.rec_glue, end protected definition switch_involutive (x : join (join A B) C) : join.switch (join.switch x) = x := begin induction x with ab c ab c, apply switch_inv_left, reflexivity, apply pathover_of_ap_ap_square join.switch join.switch, krewrite join.elim_glue, esimp, apply transpose, exact !switch_inv_coh, end end join_switch protected definition switch_equiv (A B C : Type) : join (join A B) C ≃ join (join C B) A := by apply equiv.MK; do 2 apply join.switch_involutive protected definition assoc (A B C : Type) : join (join A B) C ≃ join A (join B C) := calc join (join A B) C ≃ join (join C B) A : join.switch_equiv ... ≃ join A (join C B) : join.symm ... ≃ join A (join B C) : join.equiv_closed erfl (join.symm C B) protected definition ap_assoc_inv_glue_inl {A B : Type} (C : Type) (a : A) (b : B) : ap (to_inv (join.assoc A B C)) (glue a (inl b)) = ap inl (glue a b) := begin unfold join.assoc, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_inv, krewrite join.elim_glue, unfold switch_coh, unfold join.symm, unfold join.swap, esimp, rewrite eq.inv_inv end protected definition ap_assoc_inv_glue_inr {A C : Type} (B : Type) (a : A) (c : C) : ap (to_inv (join.assoc A B C)) (glue a (inr c)) = glue (inl a) c := begin unfold join.assoc, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_compose, krewrite join.elim_glue, rewrite ap_inv, krewrite join.elim_glue, unfold switch_coh, unfold join.symm, unfold join.swap, esimp, rewrite eq.inv_inv end end join namespace join open sphere sphere_index sphere.ops protected definition spheres (n m : ℕ₋₁) : join (S n) (S m) ≃ S (n+1+m) := begin apply equiv.trans (join.symm (S n) (S m)), induction m with m IH, { exact join.empty (S n) }, { calc join (S m.+1) (S n) ≃ join (join bool (S m)) (S n) : join.equiv_closed (equiv.symm (join.bool (S m))) erfl ... ≃ join bool (join (S m) (S n)) : join.assoc ... ≃ join bool (S (n+1+m)) : join.equiv_closed erfl IH ... ≃ sphere (n+1+m.+1) : join.bool (S (n+1+m)) } end end join
23d9c175d7c2c4d045a5164d2205b89e99d4d2e7
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/tactic/rcases.lean
92da5dad5c16526970f6d5ecb6e3e2d038873705
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
14,539
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.dlist tactic.cache open lean lean.parser namespace tactic /- These synonyms for `list` are used to clarify the meanings of the many usages of lists in this module. - `listΣ` is used where a list represents a disjunction, such as the list of possible constructors of an inductive type. - `listΠ` is used where a list represents a conjunction, such as the list of arguments of an individual constructor. These are merely type synonyms, and so are not checked for consistency by the compiler. The `def`/`local notation` combination makes Lean retain these annotations in reported types. -/ @[reducible] def list_Sigma := list @[reducible] def list_Pi := list local notation `listΣ` := list_Sigma local notation `listΠ` := list_Pi @[reducible] meta def goals := list expr meta inductive rcases_patt : Type | one : name → rcases_patt | many : listΣ (listΠ rcases_patt) → rcases_patt meta instance rcases_patt.inhabited : inhabited rcases_patt := ⟨rcases_patt.one `_⟩ meta def rcases_patt.name : rcases_patt → name | (rcases_patt.one n) := n | _ := `_ meta instance rcases_patt.has_reflect : has_reflect rcases_patt | (rcases_patt.one n) := `(_) | (rcases_patt.many l) := `(λ l, rcases_patt.many l).subst $ by haveI := rcases_patt.has_reflect; exact list.reflect l /-- The parser/printer uses an "inverted" meaning for the `many` constructor: rather than representing a sum of products, here it represents a product of sums. We fix this by applying `invert`, defined below, to the result. -/ meta inductive rcases_patt_inverted : Type | one : name → rcases_patt_inverted | many : listΠ (listΣ rcases_patt_inverted) → rcases_patt_inverted meta instance rcases_patt_inverted.inhabited : inhabited rcases_patt_inverted := ⟨rcases_patt_inverted.one `_⟩ meta instance rcases_patt_inverted.has_reflect : has_reflect rcases_patt_inverted | (rcases_patt_inverted.one n) := `(_) | (rcases_patt_inverted.many l) := `(λ l, rcases_patt_inverted.many l).subst $ by haveI := rcases_patt_inverted.has_reflect; exact list.reflect l meta mutual def rcases_patt_inverted.invert, rcases_patt_inverted.invert_list with rcases_patt_inverted.invert : listΣ rcases_patt_inverted → rcases_patt | [rcases_patt_inverted.one n] := rcases_patt.one n | l := rcases_patt.many (rcases_patt_inverted.invert_list l) with rcases_patt_inverted.invert_list : listΣ rcases_patt_inverted → listΣ (listΠ rcases_patt) | l := l.map $ λ p, match p with | rcases_patt_inverted.one n := [rcases_patt.one n] | rcases_patt_inverted.many l := rcases_patt_inverted.invert <$> l end meta mutual def rcases_patt.invert, rcases_patt.invert_many, rcases_patt.invert_list, rcases_patt.invert' with rcases_patt.invert : rcases_patt → listΣ rcases_patt_inverted | (rcases_patt.one n) := [rcases_patt_inverted.one n] | (rcases_patt.many ls) := rcases_patt.invert_many ls with rcases_patt.invert_many : listΣ (listΠ rcases_patt) → listΣ rcases_patt_inverted | [] := [] | [[rcases_patt.many ls@(_::_::_)]] := rcases_patt.invert_many ls | (l::ls) := rcases_patt.invert' l :: rcases_patt.invert_many ls with rcases_patt.invert_list : listΠ rcases_patt → listΠ (listΣ rcases_patt_inverted) | [] := [] | [rcases_patt.many [l@(_::_::_)]] := rcases_patt.invert_list l | (p::l) := rcases_patt.invert p :: rcases_patt.invert_list l with rcases_patt.invert' : listΠ rcases_patt → rcases_patt_inverted | [rcases_patt.one n] := rcases_patt_inverted.one n | [] := rcases_patt_inverted.one `_ | ls := rcases_patt_inverted.many (rcases_patt.invert_list ls) meta mutual def rcases_patt_inverted.format, rcases_patt_inverted.format_list with rcases_patt_inverted.format : rcases_patt_inverted → format | (rcases_patt_inverted.one n) := to_fmt n | (rcases_patt_inverted.many []) := "⟨⟩" | (rcases_patt_inverted.many ls) := "⟨" ++ format.group (format.nest 1 $ format.join $ list.intersperse ("," ++ format.line) $ ls.map (format.group ∘ rcases_patt_inverted.format_list)) ++ "⟩" with rcases_patt_inverted.format_list : listΣ rcases_patt_inverted → opt_param bool ff → format | [] br := "⟨⟩" | [p] br := rcases_patt_inverted.format p | (p::l) br := let fmt := rcases_patt_inverted.format p ++ " |" ++ format.space ++ rcases_patt_inverted.format_list l in if br then format.bracket "(" ")" fmt else fmt meta instance rcases_patt_inverted.has_to_format : has_to_format rcases_patt_inverted := ⟨rcases_patt_inverted.format⟩ meta def rcases_patt.format (p : rcases_patt) (br := ff) : format := rcases_patt_inverted.format_list p.invert br meta instance rcases_patt.has_to_format : has_to_format rcases_patt := ⟨rcases_patt.format⟩ /-- Takes the number of fields of a single constructor and patterns to match its fields against (not necessarily the same number). The returned lists each contain one element per field of the constructor. The `name` is the name which will be used in the top-level `cases` tactic, and the `rcases_patt` is the pattern which the field will be matched against by subsequent `cases` tactics. -/ meta def rcases.process_constructor : nat → listΠ rcases_patt → listΠ name × listΠ rcases_patt | 0 ids := ([], []) | 1 [] := ([`_], [default _]) | 1 [id] := ([id.name], [id]) -- The interesting case: we matched the last field against multiple -- patterns, so split off the remaining patterns into a subsequent -- match. This handles matching `α × β × γ` against `⟨a, b, c⟩`. | 1 ids := ([`_], [rcases_patt.many [ids]]) | (n+1) ids := let (ns, ps) := rcases.process_constructor n ids.tail, p := ids.head in (p.name :: ns, p :: ps) meta def rcases.process_constructors (params : nat) : listΣ name → listΣ (listΠ rcases_patt) → tactic (dlist name × listΣ (name × listΠ rcases_patt)) | [] ids := pure (dlist.empty, []) | (c::cs) ids := do n ← mk_const c >>= get_arity, let (h, t) := (match cs, ids.tail with -- We matched the last constructor against multiple patterns, -- so split off the remaining constructors. This handles matching -- `α ⊕ β ⊕ γ` against `a|b|c`. | [], _::_ := ([rcases_patt.many ids], []) | _, _ := (ids.head, ids.tail) end : _), let (ns, ps) := rcases.process_constructor (n - params) h, (l, r) ← rcases.process_constructors cs t, pure (dlist.of_list ns ++ l, (c, ps) :: r) private def align {α β} (p : α → β → Prop) [∀ a b, decidable (p a b)] : list α → list β → list (α × β) | (a::as) (b::bs) := if p a b then (a, b) :: align as bs else align as (b::bs) | _ _ := [] private meta def get_local_and_type (e : expr) : tactic (expr × expr) := (do t ← infer_type e, pure (t, e)) <|> (do e ← get_local e.local_pp_name, t ← infer_type e, pure (t, e)) meta mutual def rcases_core, rcases.continue with rcases_core : listΣ (listΠ rcases_patt) → expr → tactic goals | ids e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, (ids, r, l) ← (if I ≠ `quot then do when (¬env.is_inductive I) $ fail format!"rcases tactic failed: {e} : {I} is not an inductive datatype", let params := env.inductive_num_params I, let c := env.constructors_of I, (ids, r) ← rcases.process_constructors params c ids, l ← cases_core e ids.to_list, return (ids, r, l) else do (ids, r) ← rcases.process_constructors 2 [`quot.mk] ids, [(_, d)] ← induction e ids.to_list `quot.induction_on | fail format!"quotient induction on {e} failed. Maybe goal is not in Prop?", -- the result from `induction` is missing the information that the original constructor was -- `quot.mk` so we fix this up: return (ids, r, [(`quot.mk, d)])), gs ← get_goals, -- `cases_core` may not generate a new goal for every constructor, -- as some constructors may be impossible for type reasons. (See its -- documentation.) Match up the new goals with our remaining work -- by constructor name. list.join <$> (align (λ (a : name × _) (b : _ × name × _), a.1 = b.2.1) r (gs.zip l)).mmap (λ⟨⟨_, ps⟩, g, _, hs, _⟩, set_goals [g] >> rcases.continue (ps.zip hs)) with rcases.continue : listΠ (rcases_patt × expr) → tactic goals | [] := get_goals | ((rcases_patt.many ids, e) :: l) := do gs ← rcases_core ids e, list.join <$> gs.mmap (λ g, set_goals [g] >> rcases.continue l) | ((rcases_patt.one `rfl, e) :: l) := do (t, e) ← get_local_and_type e, subst e, rcases.continue l -- If the pattern is any other name, we already bound the name in the -- top-level `cases` tactic, so there is no more work to do for it. | (_ :: l) := rcases.continue l meta def rcases (p : pexpr) (ids : listΣ (listΠ rcases_patt)) : tactic unit := do e ← i_to_expr p, if e.is_local_constant then focus1 (rcases_core ids e >>= set_goals) else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, (tactic.generalize e x) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, return ()), h ← tactic.intro1, focus1 (rcases_core ids h >>= set_goals) meta def rintro (ids : listΠ rcases_patt) : tactic unit := do l ← ids.mmap (λ id, do e ← intro id.name, return (id, e)), focus1 (rcases.continue l >>= set_goals) def merge_list {α} (m : α → α → α) : list α → list α → list α | [] l₂ := l₂ | l₁ [] := l₁ | (a :: l₁) (b :: l₂) := m a b :: merge_list l₁ l₂ meta def rcases_patt.merge : rcases_patt → rcases_patt → rcases_patt | (rcases_patt.many ids₁) (rcases_patt.many ids₂) := rcases_patt.many (merge_list (merge_list rcases_patt.merge) ids₁ ids₂) | (rcases_patt.one `rfl) (rcases_patt.many ids₂) := rcases_patt.many (merge_list (merge_list rcases_patt.merge) [[]] ids₂) | (rcases_patt.many ids₁) (rcases_patt.one `rfl) := rcases_patt.many (merge_list (merge_list rcases_patt.merge) ids₁ [[]]) | (rcases_patt.one `rfl) (rcases_patt.one `rfl) := rcases_patt.one `rfl | (rcases_patt.one `_) p := p | p (rcases_patt.one `_) := p | (rcases_patt.one n) _ := rcases_patt.one n | _ (rcases_patt.one n) := rcases_patt.one n meta mutual def rcases_hint_core, rcases_hint.process_constructors, rcases_hint.continue with rcases_hint_core : ℕ → expr → tactic (rcases_patt × goals) | depth e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, some l ← try_core (guard (depth ≠ 0) >> cases_core e) | prod.mk (rcases_patt.one e.local_pp_name) <$> get_goals, let I := t.get_app_fn.const_name, if I = ``eq then prod.mk (rcases_patt.one `rfl) <$> get_goals else do let c := env.constructors_of I, gs ← get_goals, (ps, gs') ← rcases_hint.process_constructors (depth - 1) c (gs.zip l), pure (rcases_patt.many ps, gs') with rcases_hint.process_constructors : ℕ → listΣ name → list (expr × name × listΠ expr × list (name × expr)) → tactic (listΣ (listΠ rcases_patt) × goals) | depth [] _ := pure ([], []) | depth cs [] := pure (cs.map (λ _, []), []) | depth (c::cs) ((g, c', hs, _) :: l) := if c ≠ c' then do (ps, gs) ← rcases_hint.process_constructors depth cs l, pure ([] :: ps, gs) else do (p, gs) ← set_goals [g] >> rcases_hint.continue depth hs, (ps, gs') ← rcases_hint.process_constructors depth cs l, pure (p :: ps, gs ++ gs') with rcases_hint.continue : ℕ → listΠ expr → tactic (listΠ rcases_patt × goals) | depth [] := prod.mk [] <$> get_goals | depth (e :: l) := do (p, gs) ← rcases_hint_core depth e, (ps, gs') ← gs.mfoldl (λ (r : listΠ rcases_patt × goals) g, do (ps, gs') ← set_goals [g] >> rcases_hint.continue depth l, pure (merge_list rcases_patt.merge r.1 ps, r.2 ++ gs')) ([], []), pure (p :: ps, gs') meta def rcases_hint (p : pexpr) (depth : nat) : tactic rcases_patt := do e ← i_to_expr p, if e.is_local_constant then focus1 $ do (p, gs) ← rcases_hint_core depth e, set_goals gs, pure p else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, (tactic.generalize e x) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), h ← tactic.intro1, focus1 $ do (p, gs) ← rcases_hint_core depth h, set_goals gs, pure p meta def rintro_hint (depth : nat) : tactic (listΠ rcases_patt) := do l ← intros, focus1 $ do (p, gs) ← rcases_hint.continue depth l, set_goals gs, pure p section interactive open interactive interactive.types expr local notation `listΣ` := list_Sigma local notation `listΠ` := list_Pi local postfix `?`:9001 := optional local postfix *:9001 := many meta def rcases_patt_parse_core (rcases_patt_parse_list : parser (listΣ rcases_patt_inverted)) : parser rcases_patt_inverted | x := ((rcases_patt_inverted.one <$> ident_) <|> (rcases_patt_inverted.many <$> brackets "⟨" "⟩" (sep_by (tk ",") rcases_patt_parse_list))) x meta def rcases_patt_parse_list : parser (listΣ rcases_patt_inverted) := with_desc "patt" $ list.cons <$> rcases_patt_parse_core rcases_patt_parse_list <*> (tk "|" *> rcases_patt_parse_core rcases_patt_parse_list)* meta def rcases_patt_parse : parser rcases_patt_inverted := with_desc "patt_list" $ rcases_patt_parse_core rcases_patt_parse_list meta def rcases_parse_depth : parser nat := do o ← (tk ":" *> small_nat)?, pure $ o.get_or_else 5 meta def rcases_parse : parser (pexpr × (listΣ (listΠ rcases_patt) ⊕ nat)) := do hint ← (tk "?")?, p ← texpr, match hint with | none := do ids ← (tk "with" *> rcases_patt_parse_list)?, pure (p, sum.inl $ rcases_patt_inverted.invert_list (ids.get_or_else [default _])) | some _ := do depth ← rcases_parse_depth, pure (p, sum.inr depth) end meta def rintro_parse : parser (listΠ rcases_patt ⊕ nat) := (tk "?" >> sum.inr <$> rcases_parse_depth) <|> sum.inl <$> (rcases_patt_inverted.invert <$> (brackets "(" ")" rcases_patt_parse_list <|> (λ x, [x]) <$> rcases_patt_parse))* meta def ext_patt := listΠ rcases_patt meta def ext_parse : parser ext_patt := (rcases_patt_inverted.invert <$> (brackets "(" ")" rcases_patt_parse_list <|> (λ x, [x]) <$> rcases_patt_parse))* end interactive end tactic
fefe7d26f4ceb5bd525e29a53de7c6888869135f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/limits/full_subcategory.lean
7691455df253ab6e8c06cb5715b8e327a60a0a57
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,644
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.creates /-! # Limits in full subcategories We introduce the notion of a property closed under taking limits and show that if `P` is closed under taking limits, then limits in `full_subcategory P` can be constructed from limits in `C`. More precisely, the inclusion creates such limits. -/ noncomputable theory universes w' w v u open category_theory namespace category_theory.limits /-- We say that a property is closed under limits of shape `J` if whenever all objects in a `J`-shaped diagram have the property, any limit of this diagram also has the property. -/ def closed_under_limits_of_shape {C : Type u} [category.{v} C] (J : Type w) [category.{w'} J] (P : C → Prop) : Prop := ∀ ⦃F : J ⥤ C⦄ ⦃c : cone F⦄ (hc : is_limit c), (∀ j, P (F.obj j)) → P c.X /-- We say that a property is closed under colimits of shape `J` if whenever all objects in a `J`-shaped diagram have the property, any colimit of this diagram also has the property. -/ def closed_under_colimits_of_shape {C : Type u} [category.{v} C] (J : Type w) [category.{w'} J] (P : C → Prop) : Prop := ∀ ⦃F : J ⥤ C⦄ ⦃c : cocone F⦄ (hc : is_colimit c), (∀ j, P (F.obj j)) → P c.X section variables {C : Type u} [category.{v} C] {J : Type w} [category.{w'} J] {P : C → Prop} lemma closed_under_limits_of_shape.limit (h : closed_under_limits_of_shape J P) {F : J ⥤ C} [has_limit F] : (∀ j, P (F.obj j)) → P (limit F) := h (limit.is_limit _) lemma closed_under_colimits_of_shape.colimit (h : closed_under_colimits_of_shape J P) {F : J ⥤ C} [has_colimit F] : (∀ j, P (F.obj j)) → P (colimit F) := h (colimit.is_colimit _) end section variables {J : Type w} [category.{w'} J] {C : Type u} [category.{v} C] {P : C → Prop} /-- If a `J`-shaped diagram in `full_subcategory P` has a limit cone in `C` whose cone point lives in the full subcategory, then this defines a limit in the full subcategory. -/ def creates_limit_full_subcategory_inclusion' (F : J ⥤ full_subcategory P) {c : cone (F ⋙ full_subcategory_inclusion P)} (hc : is_limit c) (h : P c.X) : creates_limit F (full_subcategory_inclusion P) := creates_limit_of_fully_faithful_of_iso' hc ⟨_, h⟩ (iso.refl _) /-- If a `J`-shaped diagram in `full_subcategory P` has a limit in `C` whose cone point lives in the full subcategory, then this defines a limit in the full subcategory. -/ def creates_limit_full_subcategory_inclusion (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] (h : P (limit (F ⋙ full_subcategory_inclusion P))) : creates_limit F (full_subcategory_inclusion P) := creates_limit_full_subcategory_inclusion' F (limit.is_limit _) h /-- If a `J`-shaped diagram in `full_subcategory P` has a colimit cocone in `C` whose cocone point lives in the full subcategory, then this defines a colimit in the full subcategory. -/ def creates_colimit_full_subcategory_inclusion' (F : J ⥤ full_subcategory P) {c : cocone (F ⋙ full_subcategory_inclusion P)} (hc : is_colimit c) (h : P c.X) : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_of_fully_faithful_of_iso' hc ⟨_, h⟩ (iso.refl _) /-- If a `J`-shaped diagram in `full_subcategory P` has a colimit in `C` whose cocone point lives in the full subcategory, then this defines a colimit in the full subcategory. -/ def creates_colimit_full_subcategory_inclusion (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] (h : P (colimit (F ⋙ full_subcategory_inclusion P))) : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_full_subcategory_inclusion' F (colimit.is_colimit _) h /-- If `P` is closed under limits of shape `J`, then the inclusion creates such limits. -/ def creates_limit_full_subcategory_inclusion_of_closed (h : closed_under_limits_of_shape J P) (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] : creates_limit F (full_subcategory_inclusion P) := creates_limit_full_subcategory_inclusion F (h.limit (λ j, (F.obj j).property)) /-- If `P` is closed under limits of shape `J`, then the inclusion creates such limits. -/ def creates_limits_of_shape_full_subcategory_inclusion (h : closed_under_limits_of_shape J P) [has_limits_of_shape J C] : creates_limits_of_shape J (full_subcategory_inclusion P) := { creates_limit := λ F, creates_limit_full_subcategory_inclusion_of_closed h F } lemma has_limit_of_closed_under_limits (h : closed_under_limits_of_shape J P) (F : J ⥤ full_subcategory P) [has_limit (F ⋙ full_subcategory_inclusion P)] : has_limit F := have creates_limit F (full_subcategory_inclusion P), from creates_limit_full_subcategory_inclusion_of_closed h F, by exactI has_limit_of_created F (full_subcategory_inclusion P) lemma has_limits_of_shape_of_closed_under_limits (h : closed_under_limits_of_shape J P) [has_limits_of_shape J C] : has_limits_of_shape J (full_subcategory P) := { has_limit := λ F, has_limit_of_closed_under_limits h F } /-- If `P` is closed under colimits of shape `J`, then the inclusion creates such colimits. -/ def creates_colimit_full_subcategory_inclusion_of_closed (h : closed_under_colimits_of_shape J P) (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] : creates_colimit F (full_subcategory_inclusion P) := creates_colimit_full_subcategory_inclusion F (h.colimit (λ j, (F.obj j).property)) /-- If `P` is closed under colimits of shape `J`, then the inclusion creates such colimits. -/ def creates_colimits_of_shape_full_subcategory_inclusion (h : closed_under_colimits_of_shape J P) [has_colimits_of_shape J C] : creates_colimits_of_shape J (full_subcategory_inclusion P) := { creates_colimit := λ F, creates_colimit_full_subcategory_inclusion_of_closed h F } lemma has_colimit_of_closed_under_colimits (h : closed_under_colimits_of_shape J P) (F : J ⥤ full_subcategory P) [has_colimit (F ⋙ full_subcategory_inclusion P)] : has_colimit F := have creates_colimit F (full_subcategory_inclusion P), from creates_colimit_full_subcategory_inclusion_of_closed h F, by exactI has_colimit_of_created F (full_subcategory_inclusion P) lemma has_colimits_of_shape_of_closed_under_colimits (h : closed_under_colimits_of_shape J P) [has_colimits_of_shape J C] : has_colimits_of_shape J (full_subcategory P) := { has_colimit := λ F, has_colimit_of_closed_under_colimits h F } end end category_theory.limits
d67b0f00e490959e7fd5d1408de3a75da5a33dff
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/algebra/algebra/subalgebra.lean
f24c133d250cf689de81b6cee2886c4e4a05a51f
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,260
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.operations /-! # Subalgebras over Commutative Semiring In this file we define `subalgebra`s and the usual operations on them (`map`, `comap`). More lemmas about `adjoin` can be found in `ring_theory.adjoin`. -/ universes u v w open_locale tensor_product big_operators set_option old_structure_cmd true /-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/ structure subalgebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v := (algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier) (zero_mem' := (algebra_map R A).map_zero ▸ algebra_map_mem' 0) (one_mem' := (algebra_map R A).map_one ▸ algebra_map_mem' 1) /-- Reinterpret a `subalgebra` as a `subsemiring`. -/ add_decl_doc subalgebra.to_subsemiring namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] include R instance : has_coe (subalgebra R A) (subsemiring A) := ⟨λ S, { ..S }⟩ instance : has_mem A (subalgebra R A) := ⟨λ x S, x ∈ (S : set A)⟩ variables {A} theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := by cases S; cases T; congr; ext x; exact h x theorem ext_iff {S T : subalgebra R A} : S = T ↔ ∀ x : A, x ∈ S ↔ x ∈ T := ⟨λ h x, by rw h, ext⟩ variables (S : subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≤ S := λ x ⟨r, _, hr⟩, hr ▸ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) ⊆ S := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≤ S := S.range_subset theorem one_mem : (1 : A) ∈ S := subsemiring.one_mem S theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := subsemiring.mul_mem S hx hy theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := (algebra.smul_def r x).symm ▸ S.mul_mem (S.algebra_map_mem r) hx theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := subsemiring.pow_mem S hx n theorem zero_mem : (0 : A) ∈ S := subsemiring.zero_mem S theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := subsemiring.add_mem S hx hy theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_one_smul R x ▸ S.smul_mem hx _ theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := by simpa only [sub_eq_add_neg] using S.add_mem hx (S.neg_mem hy) theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n •ℕ x ∈ S := subsemiring.nsmul_mem S hx n theorem gsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n •ℤ x ∈ S := int.cases_on n (λ i, S.nsmul_mem hx i) (λ i, S.neg_mem $ S.nsmul_mem hx _) theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S := subsemiring.coe_nat_mem S n theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S := int.cases_on n (λ i, S.coe_nat_mem i) (λ i, S.neg_mem $ S.coe_nat_mem $ i + 1) theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := subsemiring.list_prod_mem S h theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := subsemiring.list_sum_mem S h theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S := subsemiring.multiset_prod_mem S m h theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S := subsemiring.multiset_sum_mem S m h theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S := subsemiring.prod_mem S h theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∑ x in t, f x ∈ S := subsemiring.sum_mem S h instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_add_submonoid (S : set A) := { zero_mem := S.zero_mem, add_mem := λ _ _, S.add_mem } instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_submonoid (S : set A) := { one_mem := S.one_mem, mul_mem := λ _ _, S.mul_mem } /-- A subalgebra over a ring is also a `subring`. -/ def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : subring A := { neg_mem' := λ _, S.neg_mem, .. S.to_subsemiring } instance {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S : set A) := { neg_mem := λ _, S.neg_mem } instance : inhabited S := ⟨0⟩ instance (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : semiring S := subsemiring.to_semiring S instance (R : Type u) (A : Type v) [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : comm_semiring S := subsemiring.to_comm_semiring S instance (R : Type u) (A : Type v) [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : ring S := @@subtype.ring _ S.is_subring instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring instance algebra : algebra R S := { smul := λ (c:R) x, ⟨c • x.1, S.smul_mem x.2 c⟩, commutes' := λ c x, subtype.eq $ algebra.commutes _ _, smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _, .. (algebra_map R A).cod_srestrict S $ λ x, S.range_le ⟨x, rfl⟩ } instance to_algebra {R A B : Type*} [comm_semiring R] [comm_semiring A] [semiring B] [algebra R A] [algebra A B] (A₀ : subalgebra R A) : algebra A₀ B := algebra.of_subsemiring A₀ instance nontrivial [nontrivial A] : nontrivial S := subsemiring.nontrivial S instance no_zero_smul_divisors_bot [no_zero_smul_divisors R A] : no_zero_smul_divisors R S := ⟨λ c x h, have c = 0 ∨ (x : A) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := by refine_struct { to_fun := (coe : S → A) }; intros; refl @[simp] lemma coe_val : (S.val : S → A) = coe := rfl lemma val_apply (x : S) : S.val x = (x : A) := rfl /-- Convert a `subalgebra` to `submodule` -/ def to_submodule : submodule R A := { carrier := S, zero_mem' := (0:S).2, add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) := ⟨to_submodule⟩ instance to_submodule.is_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring ((S : submodule R A) : set A) := S.is_subring @[simp] lemma mem_to_submodule {x} : x ∈ (S : submodule R A) ↔ x ∈ S := iff.rfl theorem to_submodule_injective {S U : subalgebra R A} (h : (S : submodule R A) = U) : S = U := ext $ λ x, by rw [← mem_to_submodule, ← mem_to_submodule, h] theorem to_submodule_inj {S U : subalgebra R A} : (S : submodule R A) = U ↔ S = U := ⟨to_submodule_injective, congr_arg _⟩ /-- As submodules, subalgebras are idempotent. -/ @[simp] theorem mul_self : (S : submodule R A) * (S : submodule R A) = (S : submodule R A) := begin apply le_antisymm, { rw submodule.mul_le, intros y hy z hz, exact mul_mem S hy hz }, { intros x hx1, rw ← mul_one x, exact submodule.mul_mem_mul hx1 (one_mem S) } end /-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal, we define it as a `linear_equiv` to avoid type equalities. -/ def to_submodule_equiv (S : subalgebra R A) : (S : submodule R A) ≃ₗ[R] S := linear_equiv.of_eq _ _ rfl instance : partial_order (subalgebra R A) := { le := λ S T, (S : set A) ⊆ (T : set A), le_refl := λ S, set.subset.refl S, le_trans := λ _ _ _, set.subset.trans, le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ } /-- Reinterpret an `S`-subalgebra as an `R`-subalgebra in `comap R S A`. -/ def comap {R : Type u} {S : Type v} {A : Type w} [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] (iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) := { algebra_map_mem' := λ r, iSB.algebra_map_mem (algebra_map R S r), .. iSB } /-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`, then `T` is an `R`-subalgebra of `A`. -/ def under {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { algebra_map_mem' := λ r, T.algebra_map_mem ⟨algebra_map R A r, S.algebra_map_mem r⟩, .. T } /-- Transport a subalgebra via an algebra homomorphism. -/ def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r), .. subsemiring.map (f : A →+* B) S,} /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap' (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A := { algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S, from (f.commutes r).symm ▸ S.algebra_map_mem r, .. subsemiring.comap (f : A →+* B) S,} lemma map_mono {S₁ S₂ : subalgebra R A} {f : A →ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := set.image_subset f theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} : map S f ≤ U ↔ S ≤ comap' U f := set.image_subset_iff lemma map_injective {S₁ S₂ : subalgebra R A} (f : A →ₐ[R] B) (hf : function.injective f) (ih : S₁.map f = S₂.map f) : S₁ = S₂ := ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ ext_iff.1 ih lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map S f ↔ ∃ x ∈ S, f x = y := subsemiring.mem_map instance no_zero_divisors {R A : Type*} [comm_ring R] [semiring A] [no_zero_divisors A] [algebra R A] (S : subalgebra R A) : no_zero_divisors S := S.to_subsemiring.no_zero_divisors instance no_zero_smul_divisors_top {R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] [no_zero_divisors A] (S : subalgebra R A) : no_zero_smul_divisors S A := ⟨λ c x h, have (c : A) = 0 ∨ x = 0, from eq_zero_or_eq_zero_of_mul_eq_zero h, this.imp_left (@subtype.ext_iff _ _ c 0).mpr⟩ instance integral_domain {R A : Type*} [comm_ring R] [integral_domain A] [algebra R A] (S : subalgebra R A) : integral_domain S := @subring.domain A _ S _ end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] variables (φ : A →ₐ[R] B) /-- Range of an `alg_hom` as a subalgebra. -/ protected def range (φ : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, ⟨algebra_map R A r, set.mem_univ _, φ.commutes r⟩, .. φ.to_ring_hom.srange } @[simp] lemma mem_range (φ : A →ₐ[R] B) {y : B} : y ∈ φ.range ↔ ∃ x, φ x = y := ring_hom.mem_srange theorem mem_range_self (φ : A →ₐ[R] B) (x : A) : φ x ∈ φ.range := φ.mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (φ : A →ₐ[R] B) : (φ.range : set B) = set.range φ := by { ext, rw [subalgebra.mem_coe, mem_range], refl } /-- Restrict the codomain of an algebra homomorphism. -/ def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S := { commutes' := λ r, subtype.eq $ f.commutes r, .. ring_hom.cod_srestrict (f : A →+* B) S hf } @[simp] lemma val_comp_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : S.val.comp (f.cod_restrict S hf) = f := alg_hom.ext $ λ _, rfl @[simp] lemma coe_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) : ↑(f.cod_restrict S hf x) = f x := rfl theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : function.injective (f.cod_restrict S hf) ↔ function.injective f := ⟨λ H x y hxy, H $ subtype.eq hxy, λ H x y hxy, H (congr_arg subtype.val hxy : _)⟩ /-- Restrict the codomain of a alg_hom `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : A →ₐ[R] B) : A →ₐ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (ϕ ψ : A →ₐ[R] B) : subalgebra R A := { carrier := {a | ϕ a = ψ a}, add_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x + y) = ψ (x + y), rw [alg_hom.map_add, alg_hom.map_add, hx, hy] }, mul_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x * y) = ψ (x * y), rw [alg_hom.map_mul, alg_hom.map_mul, hx, hy] }, algebra_map_mem' := λ x, by { change ϕ (algebra_map R A x) = ψ (algebra_map R A x), rw [alg_hom.commutes, alg_hom.commutes] } } @[simp] lemma mem_equalizer (ϕ ψ : A →ₐ[R] B) (x : A) : x ∈ ϕ.equalizer ψ ↔ ϕ x = ψ x := iff.rfl end alg_hom namespace alg_equiv variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] /-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range. This is a computable alternative to `alg_equiv.of_injective`. -/ def of_left_inverse {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) : A ≃ₐ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.val, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := f.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : A) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Restrict an injective algebra homomorphism to an algebra isomorphism -/ noncomputable def of_injective (f : A →ₐ[R] B) (hf : function.injective f) : A ≃ₐ[R] f.range := of_left_inverse (classical.some_spec hf.has_left_inverse) @[simp] lemma of_injective_apply (f : A →ₐ[R] B) (hf : function.injective f) (x : A) : ↑(of_injective f hf x) = f x := rfl /-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/ noncomputable def of_injective_field {E F : Type*} [division_ring E] [semiring F] [nontrivial F] [algebra R E] [algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range := of_injective f f.to_ring_hom.injective end alg_equiv namespace algebra variables (R : Type u) {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : set A) : subalgebra R A := { algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩, .. subsemiring.closure (set.range (algebra_map R A) ∪ s) } variables {R} protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H, λ H, subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, adjoin R s, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := λ _ _, rfl } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi instance : inhabited (subalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := suffices (of_id R A).range = (⊥ : subalgebra R A), by { rw [← this, ← subalgebra.mem_coe, alg_hom.coe_range], refl }, le_bot_iff.mp (λ x hx, subalgebra.range_le _ ((of_id R A).coe_range ▸ hx)) theorem to_submodule_bot : ((⊥ : subalgebra R A) : submodule R A) = R ∙ 1 := by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] } @[simp] theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := subsemiring.subset_closure $ or.inr trivial @[simp] theorem coe_top : ((⊤ : subalgebra R A) : submodule R A) = ⊤ := submodule.ext $ λ x, iff_of_true mem_top trivial @[simp] theorem coe_bot : ((⊥ : subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, algebra.mem_bot] theorem eq_top_iff {S : subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩ @[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊤ : subalgebra R A) f = f.range := subalgebra.ext $ λ x, ⟨λ ⟨y, _, hy⟩, ⟨y, set.mem_univ _, hy⟩, λ ⟨y, mem, hy⟩, ⟨y, algebra.mem_top, hy⟩⟩ @[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (⊥ : subalgebra R A) f = ⊥ := eq_bot_iff.2 $ λ x ⟨y, hy, hfy⟩, let ⟨r, hr⟩ := mem_bot.1 hy in subalgebra.range_le _ ⟨r, by rwa [← f.commutes, hr]⟩ @[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap' (⊤ : subalgebra R B) f = ⊤ := eq_top_iff.2 $ λ x, mem_top /-- `alg_hom` to `⊤ : subalgebra R A`. -/ def to_top : A →ₐ[R] (⊤ : subalgebra R A) := by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl theorem surjective_algebra_map_iff : function.surjective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, eq_bot_iff.2 $ λ y _, let ⟨x, hx⟩ := h y in hx ▸ subalgebra.algebra_map_mem _ _, λ h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩ theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A] [algebra R A] : function.bijective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, surjective_algebra_map_iff.1 h.2, λ h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) : (⊥ : subalgebra R A) ≃ₐ[R] R := alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _) ⟨λ x y hxy, h (congr_arg subtype.val hxy : _), λ ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] : (⊥ : subalgebra F R) ≃ₐ[F] F := bot_equiv_of_injective (ring_hom.injective _) /-- The top subalgebra is isomorphic to the field. -/ noncomputable def top_equiv : (⊤ : subalgebra R A) ≃ₐ[R] A := (alg_equiv.of_bijective to_top ⟨λ _ _, subtype.mk.inj, λ x, ⟨x.val, by { ext, refl }⟩⟩ : A ≃ₐ[R] (⊤ : subalgebra R A)).symm end algebra namespace subalgebra open algebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] variables (S : subalgebra R A) -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_subsingleton [subsingleton A] : subsingleton (subalgebra R A) := ⟨λ B C, ext (λ x, by { simp only [subsingleton.elim x 0, zero_mem] })⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_hom.subsingleton [subsingleton (subalgebra R A)] : subsingleton (A →ₐ[R] B) := ⟨λ f g, alg_hom.ext $ λ a, have a ∈ (⊥ : subalgebra R A) := subsingleton.elim (⊤ : subalgebra R A) ⊥ ▸ mem_top, let ⟨x, hx⟩ := set.mem_range.mp (mem_bot.mp this) in hx ▸ (f.commutes _).trans (g.commutes _).symm⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_equiv.subsingleton_left [subsingleton (subalgebra R A)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (A →ₐ[R] B) := alg_hom.subsingleton, exact ⟨λ f g, alg_equiv.ext (λ x, alg_hom.ext_iff.mp (subsingleton.elim f.to_alg_hom g.to_alg_hom) x)⟩, end -- TODO[gh-6025]: make this an instance once safe to do so lemma alg_equiv.subsingleton_right [subsingleton (subalgebra R B)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (B ≃ₐ[R] A) := alg_equiv.subsingleton_left, exact ⟨λ f g, eq.trans (alg_equiv.symm_symm.symm) (by rw [subsingleton.elim f.symm g.symm, alg_equiv.symm_symm])⟩ end lemma range_val : S.val.range = S := ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val instance : unique (subalgebra R R) := { uniq := begin intro S, refine le_antisymm (λ r hr, _) bot_le, simp only [set.mem_range, coe_bot, id.map_eq_self, exists_apply_eq_apply, default], end .. algebra.subalgebra.inhabited } end subalgebra section nat variables {R : Type*} [semiring R] /-- A subsemiring is a `ℕ`-subalgebra. -/ def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R := { algebra_map_mem' := λ i, S.coe_nat_mem i, .. S } @[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} : x ∈ subalgebra_of_subsemiring S ↔ x ∈ S := iff.rfl end nat section int variables {R : Type*} [ring R] /-- A subring is a `ℤ`-subalgebra. -/ def subalgebra_of_subring (S : subring R) : subalgebra ℤ R := { algebra_map_mem' := λ i, int.induction_on i S.zero_mem (λ i ih, S.add_mem ih S.one_mem) (λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one], exact S.sub_mem ih S.one_mem }), .. S } /-- A subset closed under the ring operations is a `ℤ`-subalgebra. -/ def subalgebra_of_is_subring (S : set R) [is_subring S] : subalgebra ℤ R := subalgebra_of_subring S.to_subring variables {S : Type*} [semiring S] @[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl @[simp] lemma mem_subalgebra_of_is_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_is_subring S ↔ x ∈ S := iff.rfl end int
1efdbae3ec1e25a6131458d8a04195ada8cf822d
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/algebra/field.lean
8950f3202da28480eab9df01bf497c6e25c411a7
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
7,860
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import algebra.ring logic.basic open set universe u variables {α : Type u} instance division_ring.to_domain [s : division_ring α] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, classical.by_contradiction $ λ hn, division_ring.mul_ne_zero (mt or.inl hn) (mt or.inr hn) h ..s } namespace units variables [division_ring α] {a b : α} /-- Embed an element of a division ring into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : α) (ha : a ≠ 0) : units α := ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩ @[simp] theorem inv_eq_inv (u : units α) : (↑u⁻¹ : α) = u⁻¹ := (mul_left_inj u).1 $ by rw [units.mul_inv, mul_inv_cancel]; apply units.ne_zero @[simp] theorem mk0_val (ha : a ≠ 0) : (mk0 a ha : α) = a := rfl @[simp] theorem mk0_inv (ha : a ≠ 0) : ((mk0 a ha)⁻¹ : α) = a⁻¹ := rfl @[simp] lemma units.mk0_inj [field α] {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ end units section division_ring variables [s : division_ring α] {a b c : α} include s lemma div_eq_mul_inv : a / b = a * b⁻¹ := rfl attribute [simp] div_one zero_div div_self theorem divp_eq_div (a : α) (u : units α) : a /ₚ u = a / u := congr_arg _ $ units.inv_eq_inv _ @[simp] theorem divp_mk0 (a : α) {b : α} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma inv_div (ha : a ≠ 0) (hb : b ≠ 0) : (a / b)⁻¹ = b / a := (mul_inv_eq (inv_ne_zero hb) ha).trans $ by rw division_ring.inv_inv hb; refl lemma inv_div_left (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ / b = (b * a)⁻¹ := (mul_inv_eq ha hb).symm lemma neg_inv (h : a ≠ 0) : - a⁻¹ = (- a)⁻¹ := by rw [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div _ h] lemma division_ring.inv_comm_of_comm (h : a ≠ 0) (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [mul_assoc, mul_assoc, mul_inv_cancel, mul_one, ← mul_assoc, inv_mul_cancel, one_mul] at this; exact h end lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := division_ring.mul_ne_zero ha (inv_ne_zero hb) lemma div_ne_zero_iff (hb : b ≠ 0) : a / b ≠ 0 ↔ a ≠ 0 := ⟨mt (λ h, by rw [h, zero_div]), λ ha, div_ne_zero ha hb⟩ lemma div_eq_zero_iff (hb : b ≠ 0) : a / b = 0 ↔ a = 0 := by haveI := classical.prop_decidable; exact not_iff_not.1 (div_ne_zero_iff hb) lemma add_div (a b c : α) : (a + b) / c = a / c + b / c := (div_add_div_same _ _ _).symm lemma div_right_inj (hc : c ≠ 0) : a / c = b / c ↔ a = b := by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_right_inj] lemma sub_div (a b c : α) : (a - b) / c = a / c - b / c := (div_sub_div_same _ _ _).symm lemma division_ring.inv_inj (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← division_ring.inv_inv ha, ← division_ring.inv_inv hb, h], congr_arg (λx,x⁻¹)⟩ lemma division_ring.inv_eq_iff (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ = b ↔ b⁻¹ = a := by rw [← division_ring.inv_inj (inv_ne_zero ha) hb, eq_comm, division_ring.inv_inv ha] lemma div_neg (a : α) (hb : b ≠ 0) : a / -b = -(a / b) := by rw [← division_ring.neg_div_neg_eq _ (neg_ne_zero.2 hb), neg_neg, neg_div] lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := ⟨λ h, by rw [← h, div_mul_cancel _ hb], λ h, by rw [← h, mul_div_cancel _ hb]⟩ end division_ring instance field.to_integral_domain [F : field α] : integral_domain α := { ..F, ..division_ring.to_domain } section variables [field α] {a b c d : α} lemma div_eq_inv_mul : a / b = b⁻¹ * a := mul_comm _ _ lemma inv_add_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, one_div_add_one_div ha hb] lemma inv_sub_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, div_sub_div _ _ ha hb, one_mul, mul_one] lemma mul_div_right_comm (a b c : α) : (a * b) / c = (a / c) * b := (div_mul_eq_mul_div _ _ _).symm lemma mul_comm_div (a b c : α) : (a / b) * c = a * (c / b) := by rw [← mul_div_assoc, mul_div_right_comm] lemma div_mul_comm (a b c : α) : (a / b) * c = (c / b) * a := by rw [div_mul_eq_mul_div, mul_comm, mul_div_right_comm] lemma mul_div_comm (a b c : α) : a * (b / c) = b * (a / c) := by rw [← mul_div_assoc, mul_comm, mul_div_assoc] lemma field.div_right_comm (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / b) / c = (a / c) / b := by rw [field.div_div_eq_div_mul _ hb hc, field.div_div_eq_div_mul _ hc hb, mul_comm] lemma field.div_div_div_cancel_right (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [field.div_div_eq_mul_div _ hb hc, div_mul_cancel _ hc] lemma field.div_mul_div_cancel (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := (domain.mul_right_inj (mul_ne_zero' hb hd)).symm.trans $ by rw [← mul_assoc, div_mul_cancel _ hb, ← mul_assoc, mul_right_comm, div_mul_cancel _ hd] lemma field.div_div_cancel (ha : a ≠ 0) (hb : b ≠ 0) : a / (a / b) = b := by rw [div_eq_mul_inv, inv_div ha hb, mul_div_cancel' _ ha] end section variables [discrete_field α] {a b c : α} attribute [simp] inv_zero div_zero lemma div_right_comm (a b c : α) : (a / b) / c = (a / c) / b := if b0 : b = 0 then by simp only [b0, div_zero, zero_div] else if c0 : c = 0 then by simp only [c0, div_zero, zero_div] else field.div_right_comm _ b0 c0 lemma div_div_div_cancel_right (a b : α) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := if b0 : b = 0 then by simp only [b0, div_zero, zero_div] else field.div_div_div_cancel_right _ b0 hc lemma div_mul_div_cancel (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := if b0 : b = 0 then by simp only [b0, div_zero, mul_zero] else field.div_mul_div_cancel _ b0 hc lemma div_div_cancel (ha : a ≠ 0) : a / (a / b) = b := if b0 : b = 0 then by simp only [b0, div_zero] else field.div_div_cancel ha b0 end @[reducible] def is_field_hom {α β} [division_ring α] [division_ring β] (f : α → β) := is_ring_hom f namespace is_field_hom open is_ring_hom section variables {β : Type*} [division_ring α] [division_ring β] variables (f : α → β) [is_field_hom f] {x y : α} lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 := ⟨mt $ λ h, h.symm ▸ map_zero f, λ x0 h, one_ne_zero $ calc 1 = f (x * x⁻¹) : by rw [mul_inv_cancel x0, map_one f] ... = 0 : by rw [map_mul f, h, zero_mul]⟩ lemma map_eq_zero : f x = 0 ↔ x = 0 := by haveI := classical.dec; exact not_iff_not.1 (map_ne_zero f) lemma map_inv' (h : x ≠ 0) : f x⁻¹ = (f x)⁻¹ := (domain.mul_left_inj ((map_ne_zero f).2 h)).1 $ by rw [mul_inv_cancel ((map_ne_zero f).2 h), ← map_mul f, mul_inv_cancel h, map_one f] lemma map_div' (h : y ≠ 0) : f (x / y) = f x / f y := (map_mul f).trans $ congr_arg _ $ map_inv' f h end section variables {β : Type*} [discrete_field α] [discrete_field β] variables (f : α → β) [is_field_hom f] {x y : α} lemma map_inv : f x⁻¹ = (f x)⁻¹ := classical.by_cases (by rintro rfl; simp only [map_zero f, inv_zero]) (map_inv' f) lemma map_div : f (x / y) = f x / f y := (map_mul f).trans $ congr_arg _ $ map_inv f end end is_field_hom
ecb5d0c6c76d9774cb3c7abab67ceb09a91c6fa9
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/monad/types.lean
8b8b939783d10f124f1043428ea063184f078fd9
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
954
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 category_theory.monad.basic import category_theory.types /-! # Convert from `monad` (i.e. Lean's `Type`-based monads) to `category_theory.monad` This allows us to use these monads in category theory. -/ namespace category_theory section universes u variables (m : Type u → Type u) [_root_.monad m] [is_lawful_monad m] instance : monad (of_type_functor m) := { η := ⟨@pure m _, assume α β f, (is_lawful_applicative.map_comp_pure f).symm ⟩, μ := ⟨@mjoin m _, assume α β (f : α → β), funext $ assume a, mjoin_map_map f a ⟩, assoc' := assume α, funext $ assume a, mjoin_map_mjoin a, left_unit' := assume α, funext $ assume a, mjoin_pure a, right_unit' := assume α, funext $ assume a, mjoin_map_pure a } end end category_theory
7fd41bf9d4aa5e64b0cd088f0ae8b9b2a8db5861
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/isomorphism.lean
4d73c78010cd5ab17dd84af4ed2c13da91ddaad1
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
13,487
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.functor /-! # Isomorphisms This file defines isomorphisms between objects of a category. ## Main definitions - `structure iso` : a bundled isomorphism between two objects of a category; - `class is_iso` : an unbundled version of `iso`; note that `is_iso f` is usually *not* a `Prop`, because it holds the inverse morphism; - `as_iso` : convert from `is_iso` to `iso`; - `of_iso` : convert from `iso` to `is_iso`; - standard operations on isomorphisms (composition, inverse etc) ## Notations - `X ≅ Y` : same as `iso X Y`; - `α ≪≫ β` : composition of two isomorphisms; it is called `iso.trans` ## Tags category, category theory, isomorphism -/ universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open category /-- An isomorphism (a.k.a. an invertible morphism) between two objects of a category. The inverse morphism is bundled. See also `category_theory.core` for the category with the same objects and isomorphisms playing the role of morphisms. See https://stacks.math.columbia.edu/tag/0017. -/ structure iso {C : Type u} [category.{v} C] (X Y : C) := (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id' : hom ≫ inv = 𝟙 X . obviously) (inv_hom_id' : inv ≫ hom = 𝟙 Y . obviously) restate_axiom iso.hom_inv_id' restate_axiom iso.inv_hom_id' attribute [simp, reassoc] iso.hom_inv_id iso.inv_hom_id infixr ` ≅ `:10 := iso -- type as \cong or \iso variables {C : Type u} [category.{v} C] variables {X Y Z : C} namespace iso @[ext] lemma ext ⦃α β : X ≅ Y⦄ (w : α.hom = β.hom) : α = β := suffices α.inv = β.inv, by cases α; cases β; cc, calc α.inv = α.inv ≫ (β.hom ≫ β.inv) : by rw [iso.hom_inv_id, category.comp_id] ... = (α.inv ≫ α.hom) ≫ β.inv : by rw [category.assoc, ←w] ... = β.inv : by rw [iso.inv_hom_id, category.id_comp] /-- Inverse isomorphism. -/ @[symm] def symm (I : X ≅ Y) : Y ≅ X := { hom := I.inv, inv := I.hom, hom_inv_id' := I.inv_hom_id', inv_hom_id' := I.hom_inv_id' } @[simp] lemma symm_hom (α : X ≅ Y) : α.symm.hom = α.inv := rfl @[simp] lemma symm_inv (α : X ≅ Y) : α.symm.inv = α.hom := rfl @[simp] lemma symm_mk {X Y : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) : iso.symm {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} = {hom := inv, inv := hom, hom_inv_id' := inv_hom_id, inv_hom_id' := hom_inv_id} := rfl @[simp] lemma symm_symm_eq {X Y : C} (α : X ≅ Y) : α.symm.symm = α := by cases α; refl @[simp] lemma symm_eq_iff {X Y : C} {α β : X ≅ Y} : α.symm = β.symm ↔ α = β := ⟨λ h, symm_symm_eq α ▸ symm_symm_eq β ▸ congr_arg symm h, congr_arg symm⟩ /-- Identity isomorphism. -/ @[refl, simps] def refl (X : C) : X ≅ X := { hom := 𝟙 X, inv := 𝟙 X } instance : inhabited (X ≅ X) := ⟨iso.refl X⟩ @[simp] lemma refl_symm (X : C) : (iso.refl X).symm = iso.refl X := rfl /-- Composition of two isomorphisms -/ @[trans, simps] def trans (α : X ≅ Y) (β : Y ≅ Z) : X ≅ Z := { hom := α.hom ≫ β.hom, inv := β.inv ≫ α.inv } infixr ` ≪≫ `:80 := iso.trans -- type as `\ll \gg`. @[simp] lemma trans_mk {X Y Z : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) (hom' : Y ⟶ Z) (inv' : Z ⟶ Y) (hom_inv_id') (inv_hom_id') (hom_inv_id'') (inv_hom_id'') : iso.trans {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} {hom := hom', inv := inv', hom_inv_id' := hom_inv_id', inv_hom_id' := inv_hom_id'} = { hom := hom ≫ hom', inv := inv' ≫ inv, hom_inv_id' := hom_inv_id'', inv_hom_id' := inv_hom_id''} := rfl @[simp] lemma trans_symm (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).symm = β.symm ≪≫ α.symm := rfl @[simp] lemma trans_assoc {Z' : C} (α : X ≅ Y) (β : Y ≅ Z) (γ : Z ≅ Z') : (α ≪≫ β) ≪≫ γ = α ≪≫ β ≪≫ γ := by ext; simp only [trans_hom, category.assoc] @[simp] lemma refl_trans (α : X ≅ Y) : (iso.refl X) ≪≫ α = α := by ext; apply category.id_comp @[simp] lemma trans_refl (α : X ≅ Y) : α ≪≫ (iso.refl Y) = α := by ext; apply category.comp_id @[simp] lemma symm_self_id (α : X ≅ Y) : α.symm ≪≫ α = iso.refl Y := ext α.inv_hom_id @[simp] lemma self_symm_id (α : X ≅ Y) : α ≪≫ α.symm = iso.refl X := ext α.hom_inv_id @[simp] lemma symm_self_id_assoc (α : X ≅ Y) (β : Y ≅ Z) : α.symm ≪≫ α ≪≫ β = β := by rw [← trans_assoc, symm_self_id, refl_trans] @[simp] lemma self_symm_id_assoc (α : X ≅ Y) (β : X ≅ Z) : α ≪≫ α.symm ≪≫ β = β := by rw [← trans_assoc, self_symm_id, refl_trans] lemma inv_comp_eq (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : α.inv ≫ f = g ↔ f = α.hom ≫ g := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_inv_comp (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : g = α.inv ≫ f ↔ α.hom ≫ g = f := (inv_comp_eq α.symm).symm lemma comp_inv_eq (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ α.inv = g ↔ f = g ≫ α.hom := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_comp_inv (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ α.inv ↔ g ≫ α.hom = f := (comp_inv_eq α.symm).symm lemma inv_eq_inv (f g : X ≅ Y) : f.inv = g.inv ↔ f.hom = g.hom := have ∀{X Y : C} (f g : X ≅ Y), f.hom = g.hom → f.inv = g.inv, from λ X Y f g h, by rw [ext h], ⟨this f.symm g.symm, this f g⟩ lemma hom_comp_eq_id (α : X ≅ Y) {f : Y ⟶ X} : α.hom ≫ f = 𝟙 X ↔ f = α.inv := by rw [←eq_inv_comp, comp_id] lemma comp_hom_eq_id (α : X ≅ Y) {f : Y ⟶ X} : f ≫ α.hom = 𝟙 Y ↔ f = α.inv := by rw [←eq_comp_inv, id_comp] lemma hom_eq_inv (α : X ≅ Y) (β : Y ≅ X) : α.hom = β.inv ↔ β.hom = α.inv := by { erw [inv_eq_inv α.symm β, eq_comm], refl } end iso /-- `is_iso` typeclass expressing that a morphism is invertible. This contains the data of the inverse, but is a subsingleton type. -/ class is_iso (f : X ⟶ Y) := (inv : Y ⟶ X) (hom_inv_id' : f ≫ inv = 𝟙 X . obviously) (inv_hom_id' : inv ≫ f = 𝟙 Y . obviously) export is_iso (inv) /-- Reinterpret a morphism `f` with an `is_iso f` instance as an `iso`. -/ def as_iso (f : X ⟶ Y) [h : is_iso f] : X ≅ Y := { hom := f, ..h } @[simp] lemma as_iso_hom (f : X ⟶ Y) [is_iso f] : (as_iso f).hom = f := rfl @[simp] lemma as_iso_inv (f : X ⟶ Y) [is_iso f] : (as_iso f).inv = inv f := rfl namespace is_iso @[simp] lemma hom_inv_id (f : X ⟶ Y) [is_iso f] : f ≫ inv f = 𝟙 X := is_iso.hom_inv_id' @[simp] lemma inv_hom_id (f : X ⟶ Y) [is_iso f] : inv f ≫ f = 𝟙 Y := is_iso.inv_hom_id' @[simp] lemma hom_inv_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : X ⟶ Z) : f ≫ inv f ≫ g = g := (as_iso f).hom_inv_id_assoc g @[simp] lemma inv_hom_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : Y ⟶ Z) : inv f ≫ f ≫ g = g := (as_iso f).inv_hom_id_assoc g instance id (X : C) : is_iso (𝟙 X) := { inv := 𝟙 X } instance of_iso (f : X ≅ Y) : is_iso f.hom := { .. f } instance of_iso_inv (f : X ≅ Y) : is_iso f.inv := is_iso.of_iso f.symm variables {f g : X ⟶ Y} {h : Y ⟶ Z} instance inv_is_iso [is_iso f] : is_iso (inv f) := is_iso.of_iso_inv (as_iso f) instance comp_is_iso [is_iso f] [is_iso h] : is_iso (f ≫ h) := is_iso.of_iso $ (as_iso f) ≪≫ (as_iso h) @[simp] lemma inv_id : inv (𝟙 X) = 𝟙 X := rfl @[simp] lemma inv_comp [is_iso f] [is_iso h] : inv (f ≫ h) = inv h ≫ inv f := rfl @[simp] lemma inv_inv [is_iso f] : inv (inv f) = f := rfl @[simp] lemma iso.inv_inv (f : X ≅ Y) : inv (f.inv) = f.hom := rfl @[simp] lemma iso.inv_hom (f : X ≅ Y) : inv (f.hom) = f.inv := rfl @[simp] lemma inv_comp_eq (α : X ⟶ Y) [is_iso α] {f : X ⟶ Z} {g : Y ⟶ Z} : inv α ≫ f = g ↔ f = α ≫ g := (as_iso α).inv_comp_eq @[simp] lemma eq_inv_comp (α : X ⟶ Y) [is_iso α] {f : X ⟶ Z} {g : Y ⟶ Z} : g = inv α ≫ f ↔ α ≫ g = f := (as_iso α).eq_inv_comp @[simp] lemma comp_inv_eq (α : X ⟶ Y) [is_iso α] {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ inv α = g ↔ f = g ≫ α := (as_iso α).comp_inv_eq @[simp] lemma eq_comp_inv (α : X ⟶ Y) [is_iso α] {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ inv α ↔ g ≫ α = f := (as_iso α).eq_comp_inv @[priority 100] -- see Note [lower instance priority] instance epi_of_iso (f : X ⟶ Y) [is_iso f] : epi f := { left_cancellation := λ Z g h w, -- This is an interesting test case for better rewrite automation. by rw [← is_iso.inv_hom_id_assoc f g, w, is_iso.inv_hom_id_assoc f h] } @[priority 100] -- see Note [lower instance priority] instance mono_of_iso (f : X ⟶ Y) [is_iso f] : mono f := { right_cancellation := λ Z g h w, by rw [← category.comp_id g, ← category.comp_id h, ← is_iso.hom_inv_id f, ← category.assoc, w, ← category.assoc] } end is_iso open is_iso lemma eq_of_inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] (p : inv f = inv g) : f = g := begin apply (cancel_epi (inv f)).1, erw [inv_hom_id, p, inv_hom_id], end instance (f : X ⟶ Y) : subsingleton (is_iso f) := ⟨λ a b, suffices a.inv = b.inv, by cases a; cases b; congr; exact this, show (@as_iso C _ _ _ f a).inv = (@as_iso C _ _ _ f b).inv, by congr' 1; ext; refl⟩ lemma is_iso.inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] : inv f = inv g ↔ f = g := iso.inv_eq_inv (as_iso f) (as_iso g) lemma hom_comp_eq_id (g : X ⟶ Y) [is_iso g] {f : Y ⟶ X} : g ≫ f = 𝟙 X ↔ f = inv g := (as_iso g).hom_comp_eq_id lemma comp_hom_eq_id (g : X ⟶ Y) [is_iso g] {f : Y ⟶ X} : f ≫ g = 𝟙 Y ↔ f = inv g := (as_iso g).comp_hom_eq_id namespace iso /-! All these cancellation lemmas can be solved by `simp [cancel_mono]` (or `simp [cancel_epi]`), but with the current design `cancel_mono` is not a good `simp` lemma, because it generates a typeclass search. When we can see syntactically that a morphism is a `mono` or an `epi` because it came from an isomorphism, it's fine to do the cancellation via `simp`. In the longer term, it might be worth exploring making `mono` and `epi` structures, rather than typeclasses, with coercions back to `X ⟶ Y`. Presumably we could write `X ↪ Y` and `X ↠ Y`. -/ @[simp] lemma cancel_iso_hom_left {X Y Z : C} (f : X ≅ Y) (g g' : Y ⟶ Z) : f.hom ≫ g = f.hom ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_iso_inv_left {X Y Z : C} (f : Y ≅ X) (g g' : Y ⟶ Z) : f.inv ≫ g = f.inv ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_iso_hom_right {X Y Z : C} (f f' : X ⟶ Y) (g : Y ≅ Z) : f ≫ g.hom = f' ≫ g.hom ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_iso_inv_right {X Y Z : C} (f f' : X ⟶ Y) (g : Z ≅ Y) : f ≫ g.inv = f' ≫ g.inv ↔ f = f' := by simp only [cancel_mono] /- Unfortunately cancelling an isomorphism from the right of a chain of compositions is awkward. We would need separate lemmas for each chain length (worse: for each pair of chain lengths). We provide two more lemmas, for case of three morphisms, because this actually comes up in practice, but then stop. -/ @[simp] lemma cancel_iso_hom_right_assoc {W X X' Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) (h : Y ≅ Z) : f ≫ g ≫ h.hom = f' ≫ g' ≫ h.hom ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_iso_inv_right_assoc {W X X' Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) (h : Z ≅ Y) : f ≫ g ≫ h.inv = f' ≫ g' ≫ h.inv ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] end iso namespace functor universes u₁ v₁ u₂ v₂ variables {D : Type u₂} variables [category.{v₂} D] /-- A functor `F : C ⥤ D` sends isomorphisms `i : X ≅ Y` to isomorphisms `F.obj X ≅ F.obj Y` -/ def map_iso (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.obj X ≅ F.obj Y := { hom := F.map i.hom, inv := F.map i.inv, hom_inv_id' := by rw [←map_comp, iso.hom_inv_id, ←map_id], inv_hom_id' := by rw [←map_comp, iso.inv_hom_id, ←map_id] } @[simp] lemma map_iso_hom (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).hom = F.map i.hom := rfl @[simp] lemma map_iso_inv (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).inv = F.map i.inv := rfl @[simp] lemma map_iso_symm (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.map_iso i.symm = (F.map_iso i).symm := rfl @[simp] lemma map_iso_trans (F : C ⥤ D) {X Y Z : C} (i : X ≅ Y) (j : Y ≅ Z) : F.map_iso (i ≪≫ j) = (F.map_iso i) ≪≫ (F.map_iso j) := by ext; apply functor.map_comp @[simp] lemma map_iso_refl (F : C ⥤ D) (X : C) : F.map_iso (iso.refl X) = iso.refl (F.obj X) := iso.ext $ F.map_id X instance map_is_iso (F : C ⥤ D) (f : X ⟶ Y) [is_iso f] : is_iso (F.map f) := is_iso.of_iso $ F.map_iso (as_iso f) @[simp] lemma map_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) = inv (F.map f) := rfl lemma map_hom_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map f ≫ F.map (inv f) = 𝟙 (F.obj X) := by simp lemma map_inv_hom (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) ≫ F.map f = 𝟙 (F.obj Y) := by simp end functor end category_theory
617de0a3b0121aa0a946d90be149524288fb3000
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/tactic/ext.lean
08105b6c2496a059e6b98888098f2e99eeb37118
[ "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
20,489
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Jesse Michael Han -/ import tactic.rcases import data.sum import logic.function.basic universes u₁ u₂ open interactive interactive.types section ext open lean.parser nat tactic declare_trace ext /-- `derive_struct_ext_lemma n` generates two extensionality lemmas based on the equality of all non-propositional projections. On the following: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` `derive_struct_lemma` generates: ```lean lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ meta def derive_struct_ext_lemma (n : name) : tactic name := do e ← get_env, fs ← e.structure_fields n, d ← get_decl n, n ← resolve_constant n, let r := @expr.const tt n $ d.univ_params.map level.param, (args,_) ← infer_type r >>= open_pis, let args := args.map expr.to_implicit_local_const, let t := r.mk_app args, x ← mk_local_def `x t, y ← mk_local_def `y t, let args_x := args ++ [x], let args_y := args ++ [y], bs ← fs.mmap $ λ f, do { d ← get_decl (n ++ f), let a := @expr.const tt (n ++ f) $ d.univ_params.map level.param, t ← infer_type a, s ← infer_type t, if s ≠ `(Prop) then do let x := a.mk_app args_x, let y := a.mk_app args_y, t ← infer_type x, t' ← infer_type y, some <$> if t = t' then mk_app `eq [x,y] >>= mk_local_def `h else mk_mapp `heq [none,x,none,y] >>= mk_local_def `h else pure none }, let bs := bs.filter_map id, eq_t ← mk_app `eq [x,y], t ← pis (args ++ [x,y] ++ bs) eq_t, pr ← run_async $ do { (_,pr) ← solve_aux t (do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, bs.mmap' (λ _, do e ← intro1, cases e), reflexivity }), instantiate_mvars pr }, let decl_n := n <.> "ext", add_decl (declaration.thm decl_n d.univ_params t pr), bs ← bs.mmap infer_type, let rhs := expr.mk_and_lst bs, iff_t ← mk_app `iff [eq_t,rhs], t ← pis (args ++ [x,y]) iff_t, pr ← run_async $ do { (_,pr) ← solve_aux t $ do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, split, solve1 $ do { h ← intro1, hs ← injection h, subst_vars, repeat (refine ``( and.intro _ _ ) >> reflexivity ), done <|> reflexivity }, solve1 $ do { repeat (do refine ``(and_imp.mpr _), h ← intro1, cases h, skip ), h ← intro1, cases h, reflexivity } }, instantiate_mvars pr }, add_decl (declaration.thm (n <.> "ext_iff") d.univ_params t pr), pure decl_n meta def get_ext_subject : expr → tactic name | (expr.pi n bi d b) := do v ← mk_local' n bi d, b' ← whnf $ b.instantiate_var v, get_ext_subject b' | (expr.app _ e) := do t ← infer_type e >>= instantiate_mvars >>= head_beta, if t.get_app_fn.is_constant then pure $ t.get_app_fn.const_name else if t.is_pi then pure $ name.mk_numeral 0 name.anonymous else if t.is_sort then pure $ name.mk_numeral 1 name.anonymous else do t ← pp t, fail format!"only constants and Pi types are supported: {t}" | e := fail format!"Only expressions of the form `_ → _ → ... → R ... e are supported: {e}" open native @[reducible] def ext_param_type := option name ⊕ option name meta def opt_minus : lean.parser (option name → ext_param_type) := sum.inl <$ tk "-" <|> pure sum.inr meta def ext_param := opt_minus <*> ( name.mk_numeral 0 name.anonymous <$ brackets "(" ")" (tk "→" <|> tk "->") <|> none <$ tk "*" <|> some <$> ident ) meta def saturate_fun : name → tactic expr | (name.mk_numeral 0 name.anonymous) := do v₀ ← mk_mvar, v₁ ← mk_mvar, return $ v₀.imp v₁ | (name.mk_numeral 1 name.anonymous) := do u ← mk_meta_univ, pure $ expr.sort u | n := do e ← resolve_constant n >>= mk_const, a ← get_arity e, e.mk_app <$> (list.iota a).mmap (λ _, mk_mvar) meta def equiv_type_constr (n n' : name) : tactic unit := do e ← saturate_fun n, e' ← saturate_fun n', unify e e' <|> fail format!"{n} and {n'} are not definitionally equal types" section performance_hack /-- For performance reasons, it is inadvisable to use `user_attribute.get_param`. The parameter is stored as a reflected expression. When calling `get_param`, the stored parameter is evaluated using `eval_expr`, which first compiles the expression into VM bytecode. The unevaluated expression is available using `user_attribute.get_param_untyped`. In particular, `user_attribute.get_param` MUST NEVER BE USED in the implementation of an attribute cache. This is because calling `eval_expr` disables the attribute cache. There are several possible workarounds: 1. Set a different attribute depending on the parameter. 2. Use your own evaluation function instead of `eval_expr`, such as e.g. `expr.to_nat`. 3. Write your own `has_reflect Param` instance (using a more efficient serialization format). The `user_attribute` code unfortunately checks whether the expression has the correct type, but you can use `` `(id %%e : Param) `` to pretend that your expression `e` has type `Param`. -/ library_note "user attribute parameters" /-! For performance reasons, the parameters of the `@[ext]` attribute are stored in two auxiliary attributes: ```lean attribute [ext [thunk]] funext -- is turned into attribute [_ext_core (@id name @funext)] thunk attribute [_ext_lemma_core] funext ``` see Note [user attribute parameters] -/ local attribute [semireducible] reflected local attribute [instance, priority 9000] private meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) @[user_attribute] private meta def ext_attr_core : user_attribute (name_map name) name := { name := `_ext_core, descr := "(internal attribute used by ext)", cache_cfg := { dependencies := [], mk_cache := λ ns, do attrs ← ns.mmap (λ n, do ext_l ← ext_attr_core.get_param_untyped n, pure (n, ext_l.app_arg.const_name)), pure $ rb_map.of_list attrs }, parser := failure } end performance_hack /-- Private attribute used to tag extensionality lemmas. -/ @[user_attribute] private meta def ext_lemma_attr_core : user_attribute := { name := `_ext_lemma_core, descr := "(internal attribute used by ext)", parser := failure } /-- Returns the extensionality lemmas in the environment, as a map from structure name to lemma name. -/ meta def get_ext_lemmas : tactic (name_map name) := ext_attr_core.get_cache /-- Returns the extensionality lemmas in the environment, as a list of lemma names. -/ meta def get_ext_lemma_names : tactic (list name) := attribute.get_instances ext_lemma_attr_core.name /-- Tag lemmas of the form: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The attribute indexes extensionality lemma using the type of the objects (i.e. `my_collection`) which it gets from the statement of the lemma. In some cases, the same lemma can be used to state the extensionality of multiple types that are definitionally equivalent. ```lean attribute [ext [(→),thunk,stream]] funext ``` Those parameters are cumulative. The following are equivalent: ```lean attribute [ext [(→),thunk]] funext attribute [ext [stream]] funext ``` and ```lean attribute [ext [(→),thunk,stream]] funext ``` One removes type names from the list for one lemma with: ```lean attribute [ext [-stream,-thunk]] funext ``` Also, the following: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` is equivalent to ```lean @[ext *] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` This allows us specify type synonyms along with the type that is referred to in the lemma statement. ```lean @[ext [*,my_type_synonym]] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The `ext` attribute can be applied to a structure to generate its extensionality lemmas: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` will generate: ```lean @[ext] lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ @[user_attribute] meta def extensional_attribute : user_attribute unit (list ext_param_type) := { name := `ext, descr := "lemmas usable by `ext` tactic", parser := pure <$> ext_param <|> list_of ext_param <|> pure [], after_set := some $ λ n prio b, do ls ← extensional_attribute.get_param n, e ← get_env, n ← if (e.structure_fields n).is_some then derive_struct_ext_lemma n else pure n, s ← mk_const n >>= infer_type >>= get_ext_subject, let (rs,ls'') := if ls.empty then ([],[s]) else ls.partition_map (sum.map (flip option.get_or_else s) (flip option.get_or_else s)), ls''.mmap' (equiv_type_constr s), ls' ← get_ext_lemmas, let l := ls'' ∪ (ls'.to_list.filter $ λ l, prod.snd l = n).map prod.fst \ rs, l.mmap' $ λ l, do ext_attr_core.set l n b prio, ext_lemma_attr_core.set n () b prio } add_tactic_doc { name := "ext", category := doc_category.attr, decl_names := [`extensional_attribute], tags := ["rewrite", "logic"] } /-- When possible, `ext` lemmas are stated without a full set of arguments. As an example, for bundled homs `f`, `g`, and `of`, `f.comp of = g.comp of → f = g` is a better `ext` lemma than `(∀ x, f (of x) = g (of x)) → f = g`, as the former allows a second type-specific extensionality lemmas to be applied to `f.comp of = g.comp of`. If the domain of `of` is `ℕ` or `ℤ` and `of` is a `ring_hom`, such a lemma could then make the goal `f (of 1) = g (of 1)`. For bundled morphisms, there is a `ext` lemma that always applies of the form `(∀ x, ⇑f x = ⇑g x) → f = g`. When adding type-specific `ext` lemmas like the one above, we want these to be tried first. This happens automatically since the type-specific lemmas are inevitably defined later. -/ library_note "partially-applied ext lemmas" -- We mark some existing extensionality lemmas. attribute [ext] array.ext propext function.hfunext attribute [ext [(→),thunk]] _root_.funext -- We create some extensionality lemmas for existing structures. attribute [ext] ulift namespace plift -- This is stronger than the one generated automatically. @[ext] lemma ext {P : Prop} (a b : plift P) : a = b := begin cases a, cases b, refl end end plift -- Conservatively, we'll only add extensionality lemmas for `has_*` structures -- as they become useful. attribute [ext] has_zero @[ext] lemma unit.ext {x y : unit} : x = y := by { cases x, cases y, refl, } @[ext] lemma punit.ext {x y : punit} : x = y := by { cases x, cases y, refl, } namespace tactic /-- Helper structure for `ext` and `ext1`. `lemmas` keeps track of extensionality lemmas applied so far. -/ meta structure ext_state : Type := (patts : list rcases_patt := []) (trace_msg : list string := []) (fuel : option ℕ := none) /-- Helper function for `try_intros`. Additionally populates the `trace_msg` field of `ext_state`. -/ private meta def try_intros_core : state_t ext_state tactic unit := do ⟨patts, trace_msg, fuel⟩ ← get, match patts with | [] := do { es ← state_t.lift intros, when (es.length > 0) $ do let msg := "intros " ++ (" ".intercalate (es.map (λ e, e.local_pp_name.to_string))), modify (λ ⟨patts, trace_msg, fuel⟩, ⟨patts, trace_msg ++ [msg], fuel⟩) } <|> pure () | (x::xs) := do tgt ← state_t.lift (target >>= whnf), when tgt.is_pi $ do state_t.lift (rintro [x]), msg ← state_t.lift (((++) "rintro ") <$> format.to_string <$> x.format ff), modify (λ ⟨_, trace_msg, fuel⟩, ⟨xs, trace_msg ++ [msg], fuel⟩), try_intros_core end /-- Try to introduce as many arguments as possible, using the given patterns to destruct the introduced variables. Returns the unused patterns. -/ meta def try_intros (patts : list rcases_patt) : tactic (list rcases_patt) := let σ := ext_state.mk patts [] none in (ext_state.patts ∘ prod.snd) <$> state_t.run try_intros_core σ /-- Apply one extensionality lemma, and destruct the arguments using the patterns in the ext_state. -/ meta def ext1_core (cfg : apply_cfg := {}) : state_t ext_state tactic unit := do ⟨patts, trace_msg, _⟩ ← get, (new_msgs) ← state_t.lift $ focus1 $ do { m ← get_ext_lemmas, tgt ← target, when_tracing `ext $ trace!"[ext] goal: {tgt}", subject ← get_ext_subject tgt, new_trace_msg ← do { rule ← (m.find subject), if is_trace_enabled_for `ext then trace!"[ext] matched goal to rule: {rule}" >> timetac "[ext] application attempt time" (applyc rule cfg) else applyc rule cfg, pure (["apply " ++ rule.to_string]) } <|> do { ls ← get_ext_lemma_names, let nms := ls.map name.to_string, rule ← (ls.any_of (λ n, (if is_trace_enabled_for `ext then trace!"[ext] trying to apply ext lemma: {n}" >> timetac "[ext] application attempt time" (applyc n cfg) else applyc n cfg) *> pure n)), pure (["apply " ++ rule.to_string]) } <|> (fail format!"no applicable extensionality rule found for {subject}"), pure new_trace_msg }, modify (λ ⟨patts, trace_msg, fuel⟩, ⟨patts, trace_msg ++ new_msgs, fuel⟩), try_intros_core /-- Apply multiple extensionality lemmas, destructing the arguments using the given patterns. -/ meta def ext_core (cfg : apply_cfg := {}) : state_t ext_state tactic unit := do acc@⟨_, _, fuel⟩ ← get, match fuel with | (some 0) := pure () | n := do { ext1_core cfg, modify (λ ⟨patts, lemmas, _⟩, ⟨patts, lemmas, nat.pred <$> n⟩), ext_core <|> pure () } end /-- Apply one extensionality lemma, and destruct the arguments using the given patterns. Returns the unused patterns. -/ meta def ext1 (xs : list rcases_patt) (cfg : apply_cfg := {}) (trace : bool := ff) : tactic (list rcases_patt) := do ⟨_, σ⟩ ← state_t.run (ext1_core cfg) {patts := xs}, when trace $ tactic.trace $ "Try this: " ++ ", ".intercalate σ.trace_msg, pure σ.patts /-- Apply multiple extensionality lemmas, destructing the arguments using the given patterns. `ext ps (some n)` applies at most `n` extensionality lemmas. Returns the unused patterns. -/ meta def ext (xs : list rcases_patt) (fuel : option ℕ) (cfg : apply_cfg := {}) (trace : bool := ff): tactic (list rcases_patt) := do ⟨_, σ⟩ ← state_t.run (ext_core cfg) {patts := xs, fuel := fuel}, when trace $ tactic.trace $ "Try this: " ++ ", ".intercalate σ.trace_msg, pure σ.patts local postfix `?`:9001 := optional local postfix *:9001 := many /-- `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. Placing a `?` after `ext1` (e.g. `ext1? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext1`. -/ meta def interactive.ext1 (trace : parse (tk "?")?) (xs : parse (rcases_patt_parse tt)*) : tactic unit := ext1 xs {} trace.is_some $> () /-- - `ext` applies as many extensionality lemmas as possible; - `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them until it runs out of identifiers in `ids` to name the local constants. - `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. - Placing a `?` after `ext` (e.g. `ext? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext`. - `set_option trace.ext true` will trace every attempted lemma application, along with the time it takes for the application to succeed or fail. This is useful for debugging slow `ext` calls. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ f x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. In the previous example, applying `ext? ⟨a,b⟩` will produce the trace message: ```lean Try this: apply funext, rintro ⟨a, b⟩ ``` A maximum depth can be provided with `ext x y z : 3`. -/ meta def interactive.ext : (parse $ (tk "?")?) → parse (rcases_patt_parse tt)* → parse (tk ":" *> small_nat)? → tactic unit | trace [] (some n) := iterate_range 1 n (ext1 [] {} trace.is_some $> ()) | trace [] none := repeat1 (ext1 [] {} trace.is_some $> ()) | trace xs n := ext xs n {} trace.is_some $> () /-- * `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. * `ext` applies as many extensionality lemmas as possible; * `ext ids`, with `ids` a list of identifiers, finds extensionality lemmas and applies them until it runs out of identifiers in `ids` to name the local constants. * `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. - Placing a `?` after `ext`/`ext1` (e.g. `ext? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext`/`ext1`. - `set_option trace.ext true` will trace every attempted lemma application, along with the time it takes for the application to succeed or fail. This is useful for debugging slow `ext` calls. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ g x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. In the previous example, applying `ext? ⟨a,b⟩` will produce the trace message: ```lean Try this: apply funext, rintro ⟨a, b⟩ ``` A maximum depth can be provided with `ext x y z : 3`. -/ add_tactic_doc { name := "ext1 / ext", category := doc_category.tactic, decl_names := [`tactic.interactive.ext1, `tactic.interactive.ext], tags := ["rewriting", "logic"] } end tactic end ext
61fa4561008f1effce1ef8f784f11d046597c6bb
95ac00a060729805e0320c0f4084d6b35729f8dd
/src/linear_algebra/basic.lean
3cfd2c93535e835d4694bc5048eea227fdaf5712
[ "Apache-2.0" ]
permissive
holtzermann17/mathlib
e71f8319634d8a1171ff0bf104e64ff838e8b641
d8f6dc46f2efb7278c6e5d68442c6e969388e93a
refs/heads/master
1,584,975,663,098
1,547,733,323,000
1,549,020,382,000
141,714,796
0
0
null
1,532,092,944,000
1,532,092,944,000
null
UTF-8
Lean
false
false
44,375
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard Basics of linear algebra. This sets up the "categorical/lattice structure" of modules, submodules, and linear maps. -/ import algebra.pi_instances data.finsupp order.order_iso open function lattice reserve infix `≃ₗ` : 50 universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ι : Type x} namespace finset lemma smul_sum [ring γ] [add_comm_group β] [module γ β] {s : finset α} {a : γ} {f : α → β} : a • (s.sum f) = s.sum (λc, a • f c) := (finset.sum_hom ((•) a)).symm end finset namespace finsupp lemma smul_sum [has_zero β] [ring γ] [add_comm_group δ] [module γ δ] {v : α →₀ β} {c : γ} {h : α → β → δ} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp namespace linear_map section variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α theorem comp_id (f : β →ₗ[α] γ) : f.comp id = f := linear_map.ext $ λ x, rfl theorem id_comp (f : β →ₗ[α] γ) : id.comp f = f := linear_map.ext $ λ x, rfl def cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h : ∀c, f c ∈ p) : γ →ₗ[α] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule α β) (f : γ →ₗ[α] β) {h} (x : γ) : (cod_restrict p f h x : β) = f x := rfl def inverse (g : γ → β) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : γ →ₗ[α] β := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ instance : has_zero (β →ₗ[α] γ) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ @[simp] lemma zero_apply (x : β) : (0 : β →ₗ[α] γ) x = 0 := rfl instance : has_neg (β →ₗ[α] γ) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩ @[simp] lemma neg_apply (x : β) : (- f) x = - f x := rfl instance : has_add (β →ₗ[α] γ) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : β) : (f + g) x = f x + g x := rfl instance : add_comm_group (β →ₗ[α] γ) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp instance linear_map.is_add_group_hom : is_add_group_hom f := by refine_struct {..}; simp instance linear_map_apply_is_add_group_hom (a : β) : is_add_group_hom (λ f : β →ₗ[α] γ, f a) := by refine_struct {..}; simp lemma sum_apply [decidable_eq δ] (t : finset δ) (f : δ → β →ₗ[α] γ) (b : β) : t.sum f b = t.sum (λd, f d b) := (@finset.sum_hom _ _ _ t f _ _ (λ g : β →ₗ[α] γ, g b) _).symm @[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl def smul_right (f : γ →ₗ[α] α) (x : β) : γ →ₗ[α] β := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : γ →ₗ[α] α) (x : β) (c : γ) : (smul_right f x : γ → β) c = f c • x := rfl instance : has_one (β →ₗ[α] β) := ⟨linear_map.id⟩ instance : has_mul (β →ₗ[α] β) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : β) : (1 : β →ₗ[α] β) x = x := rfl @[simp] lemma mul_app (A B : β →ₗ[α] β) (x : β) : (A * B) x = A (B x) := rfl section variables (α β) include β -- declaring this an instance breaks `real.lean` with reaching max. instance resolution depth def endomorphism_ring : ring (β →ₗ[α] β) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp } /-- The group of invertible linear maps from `β` to itself -/ def general_linear_group := by haveI := endomorphism_ring α β; exact units (β →ₗ[α] β) end section variables (α β γ) def fst : β × γ →ₗ[α] β := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ def snd : β × γ →ₗ[α] γ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : β × γ) : fst α β γ x = x.1 := rfl @[simp] theorem snd_apply (x : β × γ) : snd α β γ x = x.2 := rfl def pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : β →ₗ[α] γ × δ := ⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩ @[simp] theorem pair_apply (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (x : β) : pair f g x = (f x, g x) := rfl @[simp] theorem fst_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (fst α γ δ).comp (pair f g) = f := by ext; refl @[simp] theorem snd_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (snd α γ δ).comp (pair f g) = g := by ext; refl @[simp] theorem pair_fst_snd : pair (fst α β γ) (snd α β γ) = linear_map.id := by ext; refl section variables (α β γ) def inl : β →ₗ[α] β × γ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] def inr : γ →ₗ[α] β × γ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : β) : inl α β γ x = (x, 0) := rfl @[simp] theorem inr_apply (x : γ) : inr α β γ x = (0, x) := rfl def copair (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : β × γ →ₗ[α] δ := ⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩ @[simp] theorem copair_apply (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (x : β) (y : γ) : copair f g (x, y) = f x + g y := rfl @[simp] theorem copair_inl (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inl α β γ) = f := by ext; simp @[simp] theorem copair_inr (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inr α β γ) = g := by ext; simp @[simp] theorem copair_inl_inr : copair (inl α β γ) (inr α β γ) = linear_map.id := by ext ⟨x, y⟩; simp theorem fst_eq_copair : fst α β γ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_copair : snd α β γ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_pair : inl α β γ = pair linear_map.id 0 := rfl theorem inr_eq_pair : inr α β γ = pair 0 linear_map.id := rfl end section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α instance : has_scalar α (β →ₗ[α] γ) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : α) (x : β) : (a • f) x = a • f x := rfl instance : module α (β →ₗ[α] γ) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] def congr_right (f : γ →ₗ[α] δ) : (β →ₗ[α] γ) →ₗ[α] (β →ₗ[α] δ) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ end comm_ring end linear_map namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (p p' : submodule α β) (q q' : submodule α γ) variables {r : α} {x y : β} open set lattice instance : partial_order (submodule α β) := partial_order.lift (coe : submodule α β → set β) $ λ a b, ext' lemma le_def {p p' : submodule α β} : p ≤ p' ↔ (p : set β) ⊆ p' := iff.rfl lemma le_def' {p p' : submodule α β} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl def of_le {p p' : submodule α β} (h : p ≤ p') : p →ₗ[α] p' := linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx @[simp] theorem of_le_apply {p p' : submodule α β} (h : p ≤ p') (x : p) : (of_le h x : β) = x := rfl instance : has_bot (submodule α β) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ @[simp] lemma bot_coe : ((⊥ : submodule α β) : set β) = {0} := rfl section variables (α) @[simp] lemma mem_bot : x ∈ (⊥ : submodule α β) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule α β) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } instance : has_top (submodule α β) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule α β) : set β) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule α β) := trivial instance : order_top (submodule α β) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule α β) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule α β)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule α β)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule α β) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule α β) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.lattice.order_top, ..submodule.lattice.order_bot } instance : add_comm_monoid (submodule α β) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (M N : submodule α β) : M + N = M ⊔ N := rfl @[simp] lemma zero_eq_bot : (0 : submodule α β) = ⊥ := rfl lemma eq_top_iff' {p : submodule α β} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set β) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule α β} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule α β)) : (↑(Inf P) : set β) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule α β) : (↑⨅ i, p i : set β) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule α β) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule α β} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:β) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set β)) ↔ _, by simp /-- The pushforward -/ def map (f : β →ₗ[α] γ) (p : submodule α β) : submodule α γ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } lemma map_coe (f : β →ₗ[α] γ) (p : submodule α β) : (map f p : set γ) = f '' p := rfl @[simp] lemma mem_map {f : β →ₗ[α] γ} {p : submodule α β} {x : γ} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : β →ₗ[α] γ} {p : submodule α β} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α β) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : β →ₗ[α] γ} {p p' : submodule α β} : p ≤ p' → map f p ≤ map f p' := image_subset _ /-- The pullback -/ def comap (f : β →ₗ[α] γ) (p : submodule α γ) : submodule α β := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : β →ₗ[α] γ) (p : submodule α γ) : (comap f p : set β) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : β →ₗ[α] γ} {p : submodule α γ} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α δ) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : β →ₗ[α] γ} {q q' : submodule α γ} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono @[simp] lemma comap_top (f : β →ₗ[α] γ) : comap f ⊤ = ⊤ := rfl lemma map_le_iff_le_comap {f : β →ₗ[α] γ} {p : submodule α β} {q : submodule α γ} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma map_comap_le (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) ≤ q := map_le_iff_le_comap.2 $ le_refl _ lemma le_comap_map (f : β →ₗ[α] γ) (p : submodule α β) : p ≤ comap f (map f p) := map_le_iff_le_comap.1 $ le_refl _ @[simp] lemma map_bot (f : β →ₗ[α] γ) : map f ⊥ = ⊥ := eq_bot_iff.2 $ map_le_iff_le_comap.2 bot_le --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : β →ₗ[α] γ} {p : submodule α β} {p' : submodule α γ} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ section variables (α) def span (s : set β) : submodule α β := Inf {p | s ⊆ p} end variables {s t : set β} lemma mem_span : x ∈ span α s ↔ ∀ p : submodule α β, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span α s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span α s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span α s ≤ span α t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span α s) : span α s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span α (p : set β) = p := span_eq_of_le _ (subset.refl _) subset_span @[elab_as_eliminator] lemma span_induction {p : β → Prop} (h : x ∈ span α s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:α) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h variables (α β) protected def gi : galois_insertion (@span α β _ _ _) coe := { choice := λ s _, span α s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } variables {α β} @[simp] lemma span_empty : span α (∅ : set β) = ⊥ := (submodule.gi α β).gc.l_bot lemma span_union (s t : set β) : span α (s ∪ t) = span α s ⊔ span α t := (submodule.gi α β).gc.l_sup lemma span_Union {ι} (s : ι → set β) : span α (⋃ i, s i) = ⨆ i, span α (s i) := (submodule.gi α β).gc.l_supr @[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) : ((supr S : submodule α β) : set β) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), rw [show supr S = ⨆ i, span α (S i), by simp, ← span_Union], unfreezeI, refine λ x hx, span_induction hx (λ _, id) _ _ _, { cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ }, { simp, intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end @[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl theorem mem_Sup_of_directed {s : set (submodule α β)} {z} (hzs : z ∈ Sup s) (x ∈ s) (hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) : ∃ y ∈ s, z ∈ y := begin haveI := classical.dec, rw Sup_eq_supr at hzs, have, { refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs, by_cases his : i ∈ s; by_cases hjs : j ∈ s, { rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩, exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik), le_supr_of_le hks (supr_le $ λ _, hjk)⟩ }, { exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ }, { exact ⟨j, supr_le $ his.elim, le_refl _⟩ }, { exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } }, cases this with N hzn, by_cases hns : N ∈ s, { have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _), exact ⟨N, hns, this hzn⟩ }, { have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim, cases (mem_bot α).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ } end variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ variables (p p') lemma mem_span_singleton {y : β} : x ∈ span α ({y} : set β) ↔ ∃ a:α, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : β) : (span α ({y} : set β) : set β) = range ((• y) : α → β) := set.ext $ λ x, mem_span_singleton lemma mem_span_insert {y} : x ∈ span α (insert y s) ↔ ∃ (a:α) (z ∈ span α s), x = a • y + z := begin rw [← union_singleton, span_union, mem_sup], simp [mem_span_singleton], split, { rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ }, { rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ } end lemma mem_span_insert' {y} : x ∈ span α (insert y s) ↔ ∃(a:α), x + a • y ∈ span α s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ } end lemma span_insert_eq_span (h : x ∈ span α s) : span α (insert x s) = span α s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span α (span α s : set β) = span α s := span_eq _ lemma span_eq_bot : span α (s : set β) = ⊥ ↔ ∀ x ∈ s, (x:β) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot α).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot α).2 $ H x h)⟩ lemma span_singleton_eq_bot : span α ({x} : set β) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : β →ₗ[α] γ) : span α (f '' s) = map f (span α s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span def prod : submodule α (β × γ) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (β × γ)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule α β} {q : submodule α γ} {x : β × γ} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set β) (t : set γ) : span α (set.prod s t) ≤ prod (span α s) (span α t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule α (β × γ)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule α (β × γ)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule α β} {q q' : submodule α γ} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup def quotient_rel : setoid β := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩ def quotient : Type* := quotient (quotient_rel p) namespace quotient def mk {p : submodule α β} : β → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule α β} (x : β) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule α β} (x : β) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule α β} (x : β) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : β} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm] instance : has_scalar α (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module α (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] instance {α β} {R:discrete_field α} [add_comm_group β] [vector_space α β] (p : submodule α β) : vector_space α (quotient p) := {} end quotient end submodule namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open submodule @[simp] lemma finsupp_sum {α β γ δ} [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] [has_zero δ] (f : β →ₗ[α] γ) {t : ι →₀ δ} {g : ι → δ → β} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ def range (f : β →ₗ[α] γ) : submodule α γ := map f ⊤ theorem range_coe (f : β →ₗ[α] γ) : (range f : set γ) = set.range f := set.image_univ @[simp] theorem mem_range {f : β →ₗ[α] γ} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := (set.ext_iff _ _).1 (range_coe f). @[simp] theorem range_id : range (linear_map.id : β →ₗ[α] β) = ⊤ := map_id _ theorem range_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : β →ₗ[α] γ} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : β →ₗ[α] γ} {p : submodule α γ} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] def ker (f : β →ₗ[α] γ) : submodule α β := comap f ⊥ @[simp] theorem mem_ker {f : β →ₗ[α] γ} {y} : y ∈ ker f ↔ f y = 0 := mem_bot α @[simp] theorem ker_id : ker (linear_map.id : β →ₗ[α] β) = ⊥ := rfl theorem ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : β →ₗ[α] γ} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} {s : set β} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot {f : β →ₗ[α] γ} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : β →ₗ[α] γ} {p : submodule α β} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : β →ₗ[α] γ} {q : submodule α γ} (h : q ≤ range f) : map f (comap f q) = q := by rw [map_comap_eq, inf_of_le_right h] lemma comap_map_eq (f : β →ₗ[α] γ) (p : submodule α β) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : β →ₗ[α] γ} {p : submodule α β} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] @[simp] theorem ker_zero : ker (0 : β →ₗ[α] γ) = ⊤ := eq_top_iff'.2 $ λ x, by simp theorem ker_eq_top {f : β →ₗ[α] γ} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ theorem map_le_map_iff {f : β →ₗ[α] γ} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : β →ₗ[α] γ} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : β →ₗ[α] γ} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : β →ₗ[α] γ} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_copair_prod (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (p : submodule α β) (q : submodule α γ) : map (copair f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_pair_prod (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (p : submodule α γ) (q : submodule α δ) : comap (pair f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule α β) (q : submodule α γ) : p.prod q = p.comap (linear_map.fst α β γ) ⊓ q.comap (linear_map.snd α β γ) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule α β) (q : submodule α γ) : p.prod q = p.map (linear_map.inl α β γ) ⊔ q.map (linear_map.inr α β γ) := by rw [← map_copair_prod, copair_inl_inr, map_id] lemma span_inl_union_inr {s : set β} {t : set γ} : span α (prod.inl '' s ∪ prod.inr '' t) = (span α s).prod (span α t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : ker (pair f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_pair_prod]; refl end linear_map namespace submodule variables {R:ring α} [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] variables (p p' : submodule α β) (q : submodule α γ) include R open linear_map @[simp] theorem map_top (f : β →ₗ[α] γ) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : β →ₗ[α] γ) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule α p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) @[simp] theorem ker_of_le (p p' : submodule α β) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule α p → submodule α p → Prop) ≃o ((≤) : {p' : submodule α β // p' ≤ p} → {p' : submodule α β // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } def map_subtype.le_order_embedding : ((≤) : submodule α p → submodule α p → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule α p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl def map_subtype.lt_order_embedding : ((<) : submodule α p → submodule α p → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl α β γ) = prod p ⊥ := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem map_inr : q.map (inr α β γ) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst α β γ) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd α β γ) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl α β γ) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr α β γ) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst α β γ) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd α β γ) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] def mkq : β →ₗ[α] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : β) : p.mkq x = quotient.mk x := rfl def liftq (f : β →ₗ[α] γ) (h : p ≤ f.ker) : p.quotient →ₗ[α] γ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact map_add f x y, by rintro a ⟨x⟩; exact map_smul f a x⟩ @[simp] theorem liftq_apply (f : β →ₗ[α] γ) {h} (x : β) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : β →ₗ[α] γ) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule α p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] def mapq (f : β →ₗ[α] γ) (h : p ≤ comap f q) : p.quotient →ₗ[α] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : β →ₗ[α] γ) {h} (x : β) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : β →ₗ[α] γ) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : β →ₗ[α] γ) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : β →ₗ[α] γ) (h) (q : submodule α (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : β →ₗ[α] γ) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : β →ₗ[α] γ) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : β →ₗ[α] γ) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- Correspondence Theorem -/ def comap_mkq.order_iso : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≃o ((≤) : {p' : submodule α β // p ≤ p'} → {p' : submodule α β // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq], ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } def comap_mkq.le_order_embedding : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule α p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl def comap_mkq.lt_order_embedding : ((<) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true structure linear_equiv (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] extends β →ₗ[α] γ, β ≃ γ end infix ` ≃ₗ ` := linear_equiv _ notation β ` ≃ₗ[`:50 α `] ` γ := linear_equiv α β γ namespace linear_equiv section ring variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α section variable (β) def refl : β ≃ₗ[α] β := { .. linear_map.id, .. equiv.refl β } end def symm (e : β ≃ₗ[α] γ) : γ ≃ₗ[α] β := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } def trans (e₁ : β ≃ₗ[α] γ) (e₂ : γ ≃ₗ[α] δ) : β ≃ₗ[α] δ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } instance : has_coe (β ≃ₗ[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩ @[simp] theorem apply_symm_apply (e : β ≃ₗ[α] γ) (c : γ) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (e : β ≃ₗ[α] γ) (b : β) : e.symm (e b) = b := e.5 b @[simp] theorem coe_apply (e : β ≃ₗ[α] γ) (b : β) : (e : β →ₗ[α] γ) b = e b := rfl noncomputable def of_bijective (f : β →ₗ[α] γ) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : β ≃ₗ[α] γ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply (f : β →ₗ[α] γ) {hf₁ hf₂} (x : β) : of_bijective f hf₁ hf₂ x = f x := rfl def of_linear (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : β ≃ₗ[α] γ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : β) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : γ) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem ker (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).ker = ⊥ := linear_map.ker_eq_bot.2 f.to_equiv.bijective.1 @[simp] protected theorem range (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).range = ⊤ := linear_map.range_eq_top.2 f.to_equiv.bijective.2 def of_top (p : submodule α β) (h : p = ⊤) : p ≃ₗ[α] β := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply (p : submodule α β) {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply (p : submodule α β) {h} (x : β) : ↑((of_top p h).symm x) = x := rfl end ring section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α def congr_right (f : γ ≃ₗ[α] δ) : (β →ₗ[α] γ) ≃ₗ (β →ₗ δ) := of_linear f.to_linear_map.congr_right f.symm.to_linear_map.congr_right (linear_map.ext $ λ _, linear_map.ext $ λ _, f.6 _) (linear_map.ext $ λ _, linear_map.ext $ λ _, f.5 _) end comm_ring end linear_equiv namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f : β →ₗ[α] γ) /-- First Isomorphism Law -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[α] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[α] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule def sup_quotient_to_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient →ₗ[α] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end set_option class.instance_max_depth 41 /-- Second Isomorphism Law -/ noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[α] (comap (p ⊔ p').subtype p').quotient := { .. sup_quotient_to_quotient_inf p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin constructor, { rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } end linear_map
50a1a00c36522a2cb2510e1aeeae6aa6293f5366
9028d228ac200bbefe3a711342514dd4e4458bff
/src/category_theory/adjunction/opposites.lean
9b528e7773260b149cb792fb85cdaefa083f23f3
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,019
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Thomas Read -/ import category_theory.adjunction.basic import category_theory.yoneda import category_theory.opposites /-! # Opposite adjunctions This file contains constructions to relate adjunctions of functors to adjunctions of their opposites. These constructions are used to show uniqueness of adjoints (up to natural isomorphism). ## Tags adjunction, opposite, uniqueness -/ open category_theory universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace adjunction /-- If `G.op` is adjoint to `F.op` then `F` is adjoint to `G`. -/ def adjoint_of_op_adjoint_op (F : C ⥤ D) (G : D ⥤ C) (h : G.op ⊣ F.op) : F ⊣ G := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, ((h.hom_equiv (opposite.op Y) (opposite.op X)).trans (op_equiv _ _)).symm.trans (op_equiv _ _) } /-- If `G` is adjoint to `F.op` then `F` is adjoint to `G.unop`. -/ def adjoint_unop_of_adjoint_op (F : C ⥤ D) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G ⊣ F.op) : F ⊣ G.unop := adjoint_of_op_adjoint_op F G.unop (h.of_nat_iso_left G.op_unop_iso.symm) /-- If `G.op` is adjoint to `F` then `F.unop` is adjoint to `G`. -/ def unop_adjoint_of_op_adjoint (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : D ⥤ C) (h : G.op ⊣ F) : F.unop ⊣ G := adjoint_of_op_adjoint_op _ _ (h.of_nat_iso_right F.op_unop_iso.symm) /-- If `G` is adjoint to `F` then `F.unop` is adjoint to `G.unop`. -/ def unop_adjoint_unop_of_adjoint (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G ⊣ F) : F.unop ⊣ G.unop := adjoint_unop_of_adjoint_op F.unop G (h.of_nat_iso_right F.op_unop_iso.symm) /-- If `G` is adjoint to `F` then `F.op` is adjoint to `G.op`. -/ def op_adjoint_op_of_adjoint (F : C ⥤ D) (G : D ⥤ C) (h : G ⊣ F) : F.op ⊣ G.op := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (op_equiv _ Y).trans ((h.hom_equiv _ _).symm.trans (op_equiv X (opposite.op _)).symm) } /-- If `G` is adjoint to `F.unop` then `F` is adjoint to `G.op`. -/ def adjoint_op_of_adjoint_unop (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : D ⥤ C) (h : G ⊣ F.unop) : F ⊣ G.op := (op_adjoint_op_of_adjoint F.unop _ h).of_nat_iso_left F.op_unop_iso /-- If `G.unop` is adjoint to `F` then `F.op` is adjoint to `G`. -/ def op_adjoint_of_unop_adjoint (F : C ⥤ D) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G.unop ⊣ F) : F.op ⊣ G := (op_adjoint_op_of_adjoint _ G.unop h).of_nat_iso_right G.op_unop_iso /-- If `G.unop` is adjoint to `F.unop` then `F` is adjoint to `G`. -/ def adjoint_of_unop_adjoint_unop (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G.unop ⊣ F.unop) : F ⊣ G := (adjoint_op_of_adjoint_unop _ _ h).of_nat_iso_right G.op_unop_iso /-- If `F` and `F'` are both adjoint to `G`, there is a natural isomorphism `F.op ⋙ coyoneda ≅ F'.op ⋙ coyoneda`. We use this in combination with `fully_faithful_cancel_right` to show left adjoints are unique. -/ def left_adjoints_coyoneda_equiv {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G): F.op ⋙ coyoneda ≅ F'.op ⋙ coyoneda := nat_iso.of_components (λ X, nat_iso.of_components (λ Y, ((adj1.hom_equiv X.unop Y).trans (adj2.hom_equiv X.unop Y).symm).to_iso) (by tidy)) (by tidy) /-- If `F` and `F'` are both left adjoint to `G`, then they are naturally isomorphic. -/ def left_adjoint_uniq {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) : F ≅ F' := nat_iso.remove_op (fully_faithful_cancel_right _ (left_adjoints_coyoneda_equiv adj2 adj1)) /-- If `G` and `G'` are both right adjoint to `F`, then they are naturally isomorphic. -/ def right_adjoint_uniq {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') : G ≅ G' := nat_iso.remove_op (left_adjoint_uniq (op_adjoint_op_of_adjoint _ F adj2) (op_adjoint_op_of_adjoint _ _ adj1)) end adjunction
6b209e7bb628f9a650aecad91b801cc0184d75bb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_topology/dold_kan/homotopies.lean
13533ae3f6936062fe4d41b3797d2bb426d36ccc
[ "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
8,149
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebra.homology.homotopy import algebraic_topology.dold_kan.notations /-! # Construction of homotopies for the Dold-Kan correspondence > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. TODO (@joelriou) continue adding the various files referenced below (The general strategy of proof of the Dold-Kan correspondence is explained in `equivalence.lean`.) The purpose of the files `homotopies.lean`, `faces.lean`, `projections.lean` and `p_infty.lean` is to construct an idempotent endomorphism `P_infty : K[X] ⟶ K[X]` of the alternating face map complex for each `X : simplicial_object C` when `C` is a preadditive category. In the case `C` is abelian, this `P_infty` shall be the projection on the normalized Moore subcomplex of `K[X]` associated to the decomposition of the complex `K[X]` as a direct sum of this normalized subcomplex and of the degenerate subcomplex. In `p_infty.lean`, this endomorphism `P_infty` shall be obtained by passing to the limit idempotent endomorphisms `P q` for all `(q : ℕ)`. These endomorphisms `P q` are defined by induction. The idea is to start from the identity endomorphism `P 0` of `K[X]` and to ensure by induction that the `q` higher face maps (except $d_0$) vanish on the image of `P q`. Then, in a certain degree `n`, the image of `P q` for a big enough `q` will be contained in the normalized subcomplex. This construction is done in `projections.lean`. It would be easy to define the `P q` degreewise (similarly as it is done in *Simplicial Homotopy Theory* by Goerrs-Jardine p. 149), but then we would have to prove that they are compatible with the differential (i.e. they are chain complex maps), and also that they are homotopic to the identity. These two verifications are quite technical. In order to reduce the number of such technical lemmas, the strategy that is followed here is to define a series of null homotopic maps `Hσ q` (attached to families of maps `hσ`) and use these in order to construct `P q` : the endomorphisms `P q` shall basically be obtained by altering the identity endomorphism by adding null homotopic maps, so that we get for free that they are morphisms of chain complexes and that they are homotopic to the identity. The most technical verifications that are needed about the null homotopic maps `Hσ` are obtained in `faces.lean`. In this file `homotopies.lean`, we define the null homotopic maps `Hσ q : K[X] ⟶ K[X]`, show that they are natural (see `nat_trans_Hσ`) and compatible the application of additive functors (see `map_Hσ`). ## References * [Albrecht Dold, *Homology of Symmetric Products and Other Functors of Complexes*][dold1958] * [Paul G. Goerss, John F. Jardine, *Simplical Homotopy Theory*][goerss-jardine-2009] -/ open category_theory open category_theory.category open category_theory.limits open category_theory.preadditive open category_theory.simplicial_object open homotopy open opposite open_locale simplicial dold_kan noncomputable theory namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] variables {X : simplicial_object C} /-- As we are using chain complexes indexed by `ℕ`, we shall need the relation `c` such `c m n` if and only if `n+1=m`. -/ abbreviation c := complex_shape.down ℕ /-- Helper when we need some `c.rel i j` (i.e. `complex_shape.down ℕ`), e.g. `c_mk n (n+1) rfl` -/ lemma c_mk (i j : ℕ) (h : j+1 = i) : c.rel i j := complex_shape.down_mk i j h /-- This lemma is meant to be used with `null_homotopic_map'_f_of_not_rel_left` -/ lemma cs_down_0_not_rel_left (j : ℕ) : ¬c.rel 0 j := begin intro hj, dsimp at hj, apply nat.not_succ_le_zero j, rw [nat.succ_eq_add_one, hj], end /-- The sequence of maps which gives the null homotopic maps `Hσ` that shall be in the inductive construction of the projections `P q : K[X] ⟶ K[X]` -/ def hσ (q : ℕ) (n : ℕ) : X _[n] ⟶ X _[n+1] := if n<q then 0 else (-1 : ℤ)^(n-q) • X.σ ⟨n-q, nat.sub_lt_succ n q⟩ /-- We can turn `hσ` into a datum that can be passed to `null_homotopic_map'`. -/ def hσ' (q : ℕ) : Π n m, c.rel m n → (K[X].X n ⟶ K[X].X m) := λ n m hnm, (hσ q n) ≫ eq_to_hom (by congr') lemma hσ'_eq_zero {q n m : ℕ} (hnq : n<q) (hnm : c.rel m n) : (hσ' q n m hnm : X _[n] ⟶ X _[m])= 0 := by { simp only [hσ', hσ], split_ifs, exact zero_comp, } lemma hσ'_eq {q n a m : ℕ} (ha : n=a+q) (hnm : c.rel m n) : (hσ' q n m hnm : X _[n] ⟶ X _[m]) = ((-1 : ℤ)^a • X.σ ⟨a, nat.lt_succ_iff.mpr (nat.le.intro (eq.symm ha))⟩) ≫ eq_to_hom (by congr') := begin simp only [hσ', hσ], split_ifs, { exfalso, linarith, }, { have h' := tsub_eq_of_eq_add ha, congr', } end lemma hσ'_eq' {q n a : ℕ} (ha : n=a+q) : (hσ' q n (n+1) rfl : X _[n] ⟶ X _[n+1]) = (-1 : ℤ)^a • X.σ ⟨a, nat.lt_succ_iff.mpr (nat.le.intro (eq.symm ha))⟩ := by rw [hσ'_eq ha rfl, eq_to_hom_refl, comp_id] /-- The null homotopic map $(hσ q) ∘ d + d ∘ (hσ q)$ -/ def Hσ (q : ℕ) : K[X] ⟶ K[X] := null_homotopic_map' (hσ' q) /-- `Hσ` is null homotopic -/ def homotopy_Hσ_to_zero (q : ℕ) : homotopy (Hσ q : K[X] ⟶ K[X]) 0 := null_homotopy' (hσ' q) /-- In degree `0`, the null homotopic map `Hσ` is zero. -/ lemma Hσ_eq_zero (q : ℕ) : (Hσ q : K[X] ⟶ K[X]).f 0 = 0 := begin unfold Hσ, rw null_homotopic_map'_f_of_not_rel_left (c_mk 1 0 rfl) cs_down_0_not_rel_left, cases q, { rw hσ'_eq (show 0=0+0, by refl) (c_mk 1 0 rfl), simp only [pow_zero, fin.mk_zero, one_zsmul, eq_to_hom_refl, category.comp_id], erw chain_complex.of_d, simp only [alternating_face_map_complex.obj_d, fin.sum_univ_two, fin.coe_zero, pow_zero, one_zsmul, fin.coe_one, pow_one, comp_add, neg_smul, one_zsmul, comp_neg, add_neg_eq_zero], erw [δ_comp_σ_self, δ_comp_σ_succ], }, { rw [hσ'_eq_zero (nat.succ_pos q) (c_mk 1 0 rfl), zero_comp], }, end /-- The maps `hσ' q n m hnm` are natural on the simplicial object -/ lemma hσ'_naturality (q : ℕ) (n m : ℕ) (hnm : c.rel m n) {X Y : simplicial_object C} (f : X ⟶ Y) : f.app (op [n]) ≫ hσ' q n m hnm = hσ' q n m hnm ≫ f.app (op [m]) := begin have h : n+1 = m := hnm, subst h, simp only [hσ', eq_to_hom_refl, comp_id], unfold hσ, split_ifs, { rw [zero_comp, comp_zero], }, { simp only [zsmul_comp, comp_zsmul], erw f.naturality, refl, }, end /-- For each q, `Hσ q` is a natural transformation. -/ def nat_trans_Hσ (q : ℕ) : alternating_face_map_complex C ⟶ alternating_face_map_complex C := { app := λ X, Hσ q, naturality' := λ X Y f, begin unfold Hσ, rw [null_homotopic_map'_comp, comp_null_homotopic_map'], congr, ext n m hnm, simp only [alternating_face_map_complex_map_f, hσ'_naturality], end, } /-- The maps `hσ' q n m hnm` are compatible with the application of additive functors. -/ lemma map_hσ' {D : Type*} [category D] [preadditive D] (G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n m : ℕ) (hnm : c.rel m n) : (hσ' q n m hnm : K[((whiskering _ _).obj G).obj X].X n ⟶ _) = G.map (hσ' q n m hnm : K[X].X n ⟶ _) := begin unfold hσ' hσ, split_ifs, { simp only [functor.map_zero, zero_comp], }, { simpa only [eq_to_hom_map, functor.map_comp, functor.map_zsmul], }, end /-- The null homotopic maps `Hσ` are compatible with the application of additive functors. -/ lemma map_Hσ {D : Type*} [category D] [preadditive D] (G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n : ℕ) : (Hσ q : K[((whiskering C D).obj G).obj X] ⟶ _).f n = G.map ((Hσ q : K[X] ⟶ _).f n) := begin unfold Hσ, have eq := homological_complex.congr_hom (map_null_homotopic_map' G (hσ' q)) n, simp only [functor.map_homological_complex_map_f, ← map_hσ'] at eq, rw eq, let h := (functor.congr_obj (map_alternating_face_map_complex G) X).symm, congr', end end dold_kan end algebraic_topology
c5e444d95678bddd83cb80befa038dc64dc1ded2
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/10_Structures_and_Records.org.11.lean
57d275611269d39af092e86198c81dc0e52be667
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
269
lean
import standard open nat structure big := (field1 : nat) (field2 : nat) (field3 : nat) (field4 : nat) (field5 : nat) (field6 : nat) definition b : big := big.mk 1 2 3 4 5 6 definition v3 : nat := match b with {| big, field3 := v |} := v end example : v3 = 3 := rfl
5609e4c47ddb308911f3372df7c6e0be09c8e46a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/plugin/SnakeLinter.lean
93a0a77d6962e04139a26fbeb6f04fba9fd21225
[ "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
551
lean
import Lean open Lean def oh_no : Nat := 0 def snakeLinter : Linter where run stx := do if stx.getKind == `Lean.Parser.Command.declaration then let decl := stx[1] if decl.getKind == `Lean.Parser.Command.def then let declId := decl[1] withRef declId do let declName := declId[0].getId if declName.eraseMacroScopes.toString.contains '_' then -- TODO(Sebastian): return actual message with position from syntax tree throwError "SNAKES!!" initialize addLinter snakeLinter
3d832250ea73621aa035e19bdb07ad7bac848043
626e312b5c1cb2d88fca108f5933076012633192
/src/topology/path_connected.lean
6bf4f22b3f8ee13ea63e8a96d5bb28eae3712794
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,311
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 topology.unit_interval import topology.algebra.ordered.proj_Icc /-! # Path connectedness ## Main definitions In the file the unit interval `[0, 1]` in `ℝ` is denoted by `I`, and `X` is a topological space. * `path (x y : X)` is the type of paths from `x` to `y`, i.e., continuous maps from `I` to `X` mapping `0` to `x` and `1` to `y`. * `path.map` is the image of a path under a continuous map. * `joined (x y : X)` means there is a path between `x` and `y`. * `joined.some_path (h : joined x y)` selects some path between two points `x` and `y`. * `path_component (x : X)` is the set of points joined to `x`. * `path_connected_space X` is a predicate class asserting that `X` is non-empty and every two points of `X` are joined. Then there are corresponding relative notions for `F : set X`. * `joined_in F (x y : X)` means there is a path `γ` joining `x` to `y` with values in `F`. * `joined_in.some_path (h : joined_in F x y)` selects a path from `x` to `y` inside `F`. * `path_component_in F (x : X)` is the set of points joined to `x` in `F`. * `is_path_connected F` asserts that `F` is non-empty and every two points of `F` are joined in `F`. * `loc_path_connected_space X` is a predicate class asserting that `X` is locally path-connected: each point has a basis of path-connected neighborhoods (we do *not* ask these to be open). ## Main theorems * `joined` and `joined_in F` are transitive relations. One can link the absolute and relative version in two directions, using `(univ : set X)` or the subtype `↥F`. * `path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X)` * `is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space ↥F` For locally path connected spaces, we have * `path_connected_space_iff_connected_space : path_connected_space X ↔ connected_space X` * `is_connected_iff_is_path_connected (U_op : is_open U) : is_path_connected U ↔ is_connected U` ## Implementation notes By default, all paths have `I` as their source and `X` as their target, but there is an operation `set.Icc_extend` that will extend any continuous map `γ : I → X` into a continuous map `Icc_extend zero_le_one γ : ℝ → X` that is constant before `0` and after `1`. This is used to define `path.extend` that turns `γ : path x y` into a continuous map `γ.extend : ℝ → X` whose restriction to `I` is the original `γ`, and is equal to `x` on `(-∞, 0]` and to `y` on `[1, +∞)`. -/ noncomputable theory open_locale classical topological_space filter unit_interval open filter set function unit_interval variables {X : Type*} [topological_space X] {x y z : X} {ι : Type*} /-! ### Paths -/ /-- Continuous path connecting two points `x` and `y` in a topological space -/ @[nolint has_inhabited_instance] structure path (x y : X) := (to_fun : I → X) (continuous' : continuous to_fun) (source' : to_fun 0 = x) (target' : to_fun 1 = y) instance : has_coe_to_fun (path x y) := ⟨_, path.to_fun⟩ @[ext] protected lemma path.ext {X : Type*} [topological_space X] {x y : X} : ∀ {γ₁ γ₂ : path x y}, (γ₁ : I → X) = γ₂ → γ₁ = γ₂ | ⟨x, h11, h12, h13⟩ ⟨.(x), h21, h22, h23⟩ rfl := rfl namespace path @[simp] lemma coe_mk (f : I → X) (h₁ h₂ h₃) : ⇑(mk f h₁ h₂ h₃ : path x y) = f := rfl variable (γ : path x y) @[continuity] protected lemma continuous : continuous γ := γ.continuous' @[simp] protected lemma source : γ 0 = x := γ.source' @[simp] protected lemma target : γ 1 = y := γ.target' /-- Any function `φ : Π (a : α), path (x a) (y a)` can be seen as a function `α × I → X`. -/ instance has_uncurry_path {X α : Type*} [topological_space X] {x y : α → X} : has_uncurry (Π (a : α), path (x a) (y a)) (α × I) X := ⟨λ φ p, φ p.1 p.2⟩ /-- The constant path from a point to itself -/ @[refl] def refl (x : X) : path x x := { to_fun := λ t, x, continuous' := continuous_const, source' := rfl, target' := rfl } @[simp] lemma refl_range {X : Type*} [topological_space X] {a : X} : range (path.refl a) = {a} := by simp [path.refl, has_coe_to_fun.coe, coe_fn] /-- The reverse of a path from `x` to `y`, as a path from `y` to `x` -/ @[symm] def symm (γ : path x y) : path y x := { to_fun := γ ∘ σ, continuous' := by continuity, source' := by simpa [-path.target] using γ.target, target' := by simpa [-path.source] using γ.source } @[simp] lemma refl_symm {X : Type*} [topological_space X] {a : X} : (path.refl a).symm = path.refl a := by { ext, refl } @[simp] lemma symm_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) : range γ.symm = range γ := begin ext x, simp only [mem_range, path.symm, has_coe_to_fun.coe, coe_fn, unit_interval.symm, set_coe.exists, comp_app, subtype.coe_mk, subtype.val_eq_coe], split; rintros ⟨y, hy, hxy⟩; refine ⟨1-y, mem_iff_one_sub_mem.mp hy, _⟩; convert hxy, simp end /-- A continuous map extending a path to `ℝ`, constant before `0` and after `1`. -/ def extend : ℝ → X := Icc_extend zero_le_one γ lemma continuous_extend : continuous γ.extend := γ.continuous.Icc_extend @[simp] lemma extend_extends {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ∈ (Icc 0 1 : set ℝ)) : γ.extend t = γ ⟨t, ht⟩ := Icc_extend_of_mem _ γ ht lemma extend_zero : γ.extend 0 = x := by simp lemma extend_one : γ.extend 1 = y := by simp @[simp] lemma extend_extends' {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : (Icc 0 1 : set ℝ)) : γ.extend t = γ t := Icc_extend_coe _ γ t @[simp] lemma extend_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) : range γ.extend = range γ := Icc_extend_range _ γ lemma extend_of_le_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ≤ 0) : γ.extend t = a := (Icc_extend_of_le_left _ _ ht).trans γ.source lemma extend_of_one_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : 1 ≤ t) : γ.extend t = b := (Icc_extend_of_right_le _ _ ht).trans γ.target @[simp] lemma refl_extend {X : Type*} [topological_space X] {a : X} : (path.refl a).extend = λ _, a := rfl /-- The path obtained from a map defined on `ℝ` by restriction to the unit interval. -/ def of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : path x y := { to_fun := f ∘ coe, continuous' := hf.comp_continuous continuous_subtype_coe subtype.prop, source' := h₀, target' := h₁ } lemma of_line_mem {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : ∀ t, of_line hf h₀ h₁ t ∈ f '' I := λ ⟨t, t_in⟩, ⟨t, t_in, rfl⟩ local attribute [simp] Iic_def /-- Concatenation of two paths from `x` to `y` and from `y` to `z`, putting the first path on `[0, 1/2]` and the second one on `[1/2, 1]`. -/ @[trans] def trans (γ : path x y) (γ' : path y z) : path x z := { to_fun := (λ t : ℝ, if t ≤ 1/2 then γ.extend (2*t) else γ'.extend (2*t-1)) ∘ coe, continuous' := begin refine (continuous.if_le _ _ continuous_id continuous_const (by norm_num)).comp continuous_subtype_coe, -- TODO: the following are provable by `continuity` but it is too slow exacts [γ.continuous_extend.comp (continuous_const.mul continuous_id), γ'.continuous_extend.comp ((continuous_const.mul continuous_id).sub continuous_const)] end, source' := by norm_num, target' := by norm_num } @[simp] lemma refl_trans_refl {X : Type*} [topological_space X] {a : X} : (path.refl a).trans (path.refl a) = path.refl a := begin ext, simp only [path.trans, if_t_t, one_div, path.refl_extend], refl end lemma trans_range {X : Type*} [topological_space X] {a b c : X} (γ₁ : path a b) (γ₂ : path b c) : range (γ₁.trans γ₂) = range γ₁ ∪ range γ₂ := begin rw path.trans, apply eq_of_subset_of_subset, { rintros x ⟨⟨t, ht0, ht1⟩, hxt⟩, by_cases h : t ≤ 1/2, { left, use [2*t, ⟨by linarith, by linarith⟩], rw ← γ₁.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_true] at hxt, exact hxt }, { right, use [2*t-1, ⟨by linarith, by linarith⟩], rw ← γ₂.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_false] at hxt, exact hxt } }, { rintros x (⟨⟨t, ht0, ht1⟩, hxt⟩ | ⟨⟨t, ht0, ht1⟩, hxt⟩), { use ⟨t/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, have : t/2 ≤ 1/2 := by linarith, simp only [this, comp_app, if_true], ring_nf, rwa γ₁.extend_extends }, { by_cases h : t = 0, { use ⟨1/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, simp only [h, comp_app, if_true, le_refl, mul_one_div_cancel (@two_ne_zero ℝ _ _)], rw γ₁.extend_one, rwa [← γ₂.extend_extends, h, γ₂.extend_zero] at hxt }, { use ⟨(t+1)/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, change t ≠ 0 at h, have ht0 := lt_of_le_of_ne ht0 h.symm, have : ¬ (t+1)/2 ≤ 1/2 := by {rw not_le, linarith}, simp only [comp_app, if_false, this], ring_nf, rwa γ₂.extend_extends } } } end /-- Image of a path from `x` to `y` by a continuous map -/ def map (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : path (f x) (f y) := { to_fun := f ∘ γ, continuous' := by continuity, source' := by simp, target' := by simp } @[simp] lemma map_coe (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : (γ.map h : I → Y) = f ∘ γ := by { ext t, refl } /-- Casting a path from `x` to `y` to a path from `x'` to `y'` when `x' = x` and `y' = y` -/ def cast (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : path x' y' := { to_fun := γ, continuous' := γ.continuous, source' := by simp [hx], target' := by simp [hy] } @[simp] lemma symm_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ : X} (γ : path a₂ b₂) (ha : a₁ = a₂) (hb : b₁ = b₂) : (γ.cast ha hb).symm = (γ.symm).cast hb ha := rfl @[simp] lemma trans_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ c₁ c₂ : X} (γ : path a₂ b₂) (γ' : path b₂ c₂) (ha : a₁ = a₂) (hb : b₁ = b₂) (hc : c₁ = c₂) : (γ.cast ha hb).trans (γ'.cast hb hc) = (γ.trans γ').cast ha hc := rfl @[simp] lemma cast_coe (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : (γ.cast hx hy : I → X) = γ := rfl lemma symm_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).symm) := h.comp (continuous_id.prod_map continuous_symm) lemma continuous_uncurry_extend_of_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).extend) := h.comp (continuous_id.prod_map continuous_proj_Icc) lemma trans_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b c : ι → X} (γ₁ : Π (t : ι), path (a t) (b t)) (h₁ : continuous ↿γ₁) (γ₂ : Π (t : ι), path (b t) (c t)) (h₂ : continuous ↿γ₂) : continuous ↿(λ t, (γ₁ t).trans (γ₂ t)) := begin have h₁' := path.continuous_uncurry_extend_of_continuous_family γ₁ h₁, have h₂' := path.continuous_uncurry_extend_of_continuous_family γ₂ h₂, simp only [has_uncurry.uncurry, has_coe_to_fun.coe, coe_fn, path.trans, (∘)], refine continuous.if_le _ _ (continuous_subtype_coe.comp continuous_snd) continuous_const _, { change continuous ((λ p : ι × ℝ, (γ₁ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x : I → ℝ))), exact h₁'.comp (continuous_id.prod_map $ continuous_const.mul continuous_subtype_coe) }, { change continuous ((λ p : ι × ℝ, (γ₂ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x - 1 : I → ℝ))), exact h₂'.comp (continuous_id.prod_map $ (continuous_const.mul continuous_subtype_coe).sub continuous_const) }, { rintros st hst, simp [hst, mul_inv_cancel (@two_ne_zero ℝ _ _)] } end /-! #### Truncating a path -/ /-- `γ.truncate t₀ t₁` is the path which follows the path `γ` on the time interval `[t₀, t₁]` and stays still otherwise. -/ def truncate {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t₀ t₁ : ℝ) : path (γ.extend $ min t₀ t₁) (γ.extend t₁) := { to_fun := λ s, γ.extend (min (max s t₀) t₁), continuous' := γ.continuous_extend.comp ((continuous_subtype_coe.max continuous_const).min continuous_const), source' := begin unfold min max, norm_cast, split_ifs with h₁ h₂ h₃ h₄, { simp [γ.extend_of_le_zero h₁] }, { congr, linarith }, { have h₄ : t₁ ≤ 0 := le_of_lt (by simpa using h₂), simp [γ.extend_of_le_zero h₄, γ.extend_of_le_zero h₁] }, all_goals { refl } end, target' := begin unfold min max, norm_cast, split_ifs with h₁ h₂ h₃, { simp [γ.extend_of_one_le h₂] }, { refl }, { have h₄ : 1 ≤ t₀ := le_of_lt (by simpa using h₁), simp [γ.extend_of_one_le h₄, γ.extend_of_one_le (h₄.trans h₃)] }, { refl } end } /-- `γ.truncate_of_le t₀ t₁ h`, where `h : t₀ ≤ t₁` is `γ.truncate t₀ t₁` casted as a path from `γ.extend t₀` to `γ.extend t₁`. -/ def truncate_of_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} (h : t₀ ≤ t₁) : path (γ.extend t₀) (γ.extend t₁) := (γ.truncate t₀ t₁).cast (by rw min_eq_left h) rfl lemma truncate_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} : range (γ.truncate t₀ t₁) ⊆ range γ := begin rw ← γ.extend_range, simp only [range_subset_iff, set_coe.exists, set_coe.forall], intros x hx, simp only [has_coe_to_fun.coe, coe_fn, path.truncate, mem_range_self] end /-- For a path `γ`, `γ.truncate` gives a "continuous family of paths", by which we mean the uncurried function which maps `(t₀, t₁, s)` to `γ.truncate t₀ t₁ s` is continuous. -/ lemma truncate_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) : continuous (λ x, γ.truncate x.1 x.2.1 x.2.2 : ℝ × ℝ × I → X) := γ.continuous_extend.comp (((continuous_subtype_coe.comp (continuous_snd.comp continuous_snd)).max continuous_fst).min (continuous_fst.comp continuous_snd)) /- TODO : When `continuity` gets quicker, change the proof back to : `begin` `simp only [has_coe_to_fun.coe, coe_fn, path.truncate],` `continuity,` `exact continuous_subtype_coe` `end` -/ lemma truncate_const_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : continuous ↿(γ.truncate t) := have key : continuous (λ x, (t, x) : ℝ × I → ℝ × ℝ × I) := continuous_const.prod_mk continuous_id, by convert γ.truncate_continuous_family.comp key @[simp] lemma truncate_self {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : γ.truncate t t = (path.refl $ γ.extend t).cast (by rw min_self) rfl := begin ext x, rw cast_coe, simp only [truncate, has_coe_to_fun.coe, coe_fn, refl, min, max], split_ifs with h₁ h₂; congr, linarith end @[simp] lemma truncate_zero_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 0 = (path.refl a).cast (by rw [min_self, γ.extend_zero]) γ.extend_zero := by convert γ.truncate_self 0; exact γ.extend_zero.symm @[simp] lemma truncate_one_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 1 1 = (path.refl b).cast (by rw [min_self, γ.extend_one]) γ.extend_one := by convert γ.truncate_self 1; exact γ.extend_one.symm @[simp] lemma truncate_zero_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 1 = γ.cast (by simp [zero_le_one, extend_zero]) (by simp) := begin ext x, rw cast_coe, have : ↑x ∈ (Icc 0 1 : set ℝ) := x.2, rw [truncate, coe_mk, max_eq_left this.1, min_eq_left this.2, extend_extends'] end end path /-! ### Being joined by a path -/ /-- The relation "being joined by a path". This is an equivalence relation. -/ def joined (x y : X) : Prop := nonempty (path x y) @[refl] lemma joined.refl (x : X) : joined x x := ⟨path.refl x⟩ /-- When two points are joined, choose some path from `x` to `y`. -/ def joined.some_path (h : joined x y) : path x y := nonempty.some h @[symm] lemma joined.symm {x y : X} (h : joined x y) : joined y x := ⟨h.some_path.symm⟩ @[trans] lemma joined.trans {x y z : X} (hxy : joined x y) (hyz : joined y z) : joined x z := ⟨hxy.some_path.trans hyz.some_path⟩ variables (X) /-- The setoid corresponding the equivalence relation of being joined by a continuous path. -/ def path_setoid : setoid X := { r := joined, iseqv := mk_equivalence _ joined.refl (λ x y, joined.symm) (λ x y z, joined.trans) } /-- The quotient type of points of a topological space modulo being joined by a continuous path. -/ def zeroth_homotopy := quotient (path_setoid X) instance : inhabited (zeroth_homotopy ℝ) := ⟨@quotient.mk ℝ (path_setoid ℝ) 0⟩ variables {X} /-! ### Being joined by a path inside a set -/ /-- The relation "being joined by a path in `F`". Not quite an equivalence relation since it's not reflexive for points that do not belong to `F`. -/ def joined_in (F : set X) (x y : X) : Prop := ∃ γ : path x y, ∀ t, γ t ∈ F variables {F : set X} lemma joined_in.mem (h : joined_in F x y) : x ∈ F ∧ y ∈ F := begin rcases h with ⟨γ, γ_in⟩, have : γ 0 ∈ F ∧ γ 1 ∈ F, by { split; apply γ_in }, simpa using this end lemma joined_in.source_mem (h : joined_in F x y) : x ∈ F := h.mem.1 lemma joined_in.target_mem (h : joined_in F x y) : y ∈ F := h.mem.2 /-- When `x` and `y` are joined in `F`, choose a path from `x` to `y` inside `F` -/ def joined_in.some_path (h : joined_in F x y) : path x y := classical.some h lemma joined_in.some_path_mem (h : joined_in F x y) (t : I) : h.some_path t ∈ F := classical.some_spec h t /-- If `x` and `y` are joined in the set `F`, then they are joined in the subtype `F`. -/ lemma joined_in.joined_subtype (h : joined_in F x y) : joined (⟨x, h.source_mem⟩ : F) (⟨y, h.target_mem⟩ : F) := ⟨{ to_fun := λ t, ⟨h.some_path t, h.some_path_mem t⟩, continuous' := by continuity, source' := by simp, target' := by simp }⟩ lemma joined_in.of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) (hF : f '' I ⊆ F) : joined_in F x y := ⟨path.of_line hf h₀ h₁, λ t, hF $ path.of_line_mem hf h₀ h₁ t⟩ lemma joined_in.joined (h : joined_in F x y) : joined x y := ⟨h.some_path⟩ lemma joined_in_iff_joined (x_in : x ∈ F) (y_in : y ∈ F) : joined_in F x y ↔ joined (⟨x, x_in⟩ : F) (⟨y, y_in⟩ : F) := ⟨λ h, h.joined_subtype, λ h, ⟨h.some_path.map continuous_subtype_coe, by simp⟩⟩ @[simp] lemma joined_in_univ : joined_in univ x y ↔ joined x y := by simp [joined_in, joined, exists_true_iff_nonempty] lemma joined_in.mono {U V : set X} (h : joined_in U x y) (hUV : U ⊆ V) : joined_in V x y := ⟨h.some_path, λ t, hUV (h.some_path_mem t)⟩ lemma joined_in.refl (h : x ∈ F) : joined_in F x x := ⟨path.refl x, λ t, h⟩ @[symm] lemma joined_in.symm (h : joined_in F x y) : joined_in F y x := begin cases h.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact h.symm end lemma joined_in.trans (hxy : joined_in F x y) (hyz : joined_in F y z) : joined_in F x z := begin cases hxy.mem with hx hy, cases hyz.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact hxy.trans hyz end /-! ### Path component -/ /-- The path component of `x` is the set of points that can be joined to `x`. -/ def path_component (x : X) := {y | joined x y} @[simp] lemma mem_path_component_self (x : X) : x ∈ path_component x := joined.refl x @[simp] lemma path_component.nonempty (x : X) : (path_component x).nonempty := ⟨x, mem_path_component_self x⟩ lemma mem_path_component_of_mem (h : x ∈ path_component y) : y ∈ path_component x := joined.symm h lemma path_component_symm : x ∈ path_component y ↔ y ∈ path_component x := ⟨λ h, mem_path_component_of_mem h, λ h, mem_path_component_of_mem h⟩ lemma path_component_congr (h : x ∈ path_component y) : path_component x = path_component y := begin ext z, split, { intro h', rw path_component_symm, exact (h.trans h').symm }, { intro h', rw path_component_symm at h' ⊢, exact h'.trans h }, end lemma path_component_subset_component (x : X) : path_component x ⊆ connected_component x := λ y h, (is_connected_range h.some_path.continuous).subset_connected_component ⟨0, by simp⟩ ⟨1, by simp⟩ /-- The path component of `x` in `F` is the set of points that can be joined to `x` in `F`. -/ def path_component_in (x : X) (F : set X) := {y | joined_in F x y} @[simp] lemma path_component_in_univ (x : X) : path_component_in x univ = path_component x := by simp [path_component_in, path_component, joined_in, joined, exists_true_iff_nonempty] lemma joined.mem_path_component (hyz : joined y z) (hxy : y ∈ path_component x) : z ∈ path_component x := hxy.trans hyz /-! ### Path connected sets -/ /-- A set `F` is path connected if it contains a point that can be joined to all other in `F`. -/ def is_path_connected (F : set X) : Prop := ∃ x ∈ F, ∀ {y}, y ∈ F → joined_in F x y lemma is_path_connected_iff_eq : is_path_connected F ↔ ∃ x ∈ F, path_component_in x F = F := begin split ; rintros ⟨x, x_in, h⟩ ; use [x, x_in], { ext y, exact ⟨λ hy, hy.mem.2, h⟩ }, { intros y y_in, rwa ← h at y_in }, end lemma is_path_connected.joined_in (h : is_path_connected F) : ∀ x y ∈ F, joined_in F x y := λ x y x_in y_in, let ⟨b, b_in, hb⟩ := h in (hb x_in).symm.trans (hb y_in) lemma is_path_connected_iff : is_path_connected F ↔ F.nonempty ∧ ∀ x y ∈ F, joined_in F x y := ⟨λ h, ⟨let ⟨b, b_in, hb⟩ := h in ⟨b, b_in⟩, h.joined_in⟩, λ ⟨⟨b, b_in⟩, h⟩, ⟨b, b_in, λ x x_in, h b x b_in x_in⟩⟩ lemma is_path_connected.image {Y : Type*} [topological_space Y] (hF : is_path_connected F) {f : X → Y} (hf : continuous f) : is_path_connected (f '' F) := begin rcases hF with ⟨x, x_in, hx⟩, use [f x, mem_image_of_mem f x_in], rintros _ ⟨y, y_in, rfl⟩, exact ⟨(hx y_in).some_path.map hf, λ t, ⟨_, (hx y_in).some_path_mem t, rfl⟩⟩, end lemma is_path_connected.mem_path_component (h : is_path_connected F) (x_in : x ∈ F) (y_in : y ∈ F) : y ∈ path_component x := (h.joined_in x y x_in y_in).joined lemma is_path_connected.subset_path_component (h : is_path_connected F) (x_in : x ∈ F) : F ⊆ path_component x := λ y y_in, h.mem_path_component x_in y_in lemma is_path_connected.union {U V : set X} (hU : is_path_connected U) (hV : is_path_connected V) (hUV : (U ∩ V).nonempty) : is_path_connected (U ∪ V) := begin rcases hUV with ⟨x, xU, xV⟩, use [x, or.inl xU], rintros y (yU | yV), { exact (hU.joined_in x y xU yU).mono (subset_union_left U V) }, { exact (hV.joined_in x y xV yV).mono (subset_union_right U V) }, end /-- If a set `W` is path-connected, then it is also path-connected when seen as a set in a smaller ambient type `U` (when `U` contains `W`). -/ lemma is_path_connected.preimage_coe {U W : set X} (hW : is_path_connected W) (hWU : W ⊆ U) : is_path_connected ((coe : U → X) ⁻¹' W) := begin rcases hW with ⟨x, x_in, hx⟩, use [⟨x, hWU x_in⟩, by simp [x_in]], rintros ⟨y, hyU⟩ hyW, exact ⟨(hx hyW).joined_subtype.some_path.map (continuous_inclusion hWU), by simp⟩ end lemma is_path_connected.exists_path_through_family {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ γ : path (p 0) (p n), (range γ ⊆ s) ∧ (∀ i, p i ∈ range γ) := begin let p' : ℕ → X := λ k, if h : k < n+1 then p ⟨k, h⟩ else p ⟨0, n.zero_lt_succ⟩, obtain ⟨γ, hγ⟩ : ∃ (γ : path (p' 0) (p' n)), (∀ i ≤ n, p' i ∈ range γ) ∧ range γ ⊆ s, { have hp' : ∀ i ≤ n, p' i ∈ s, { intros i hi, simp [p', nat.lt_succ_of_le hi, hp] }, clear_value p', clear hp p, induction n with n hn, { use (λ _, p' 0), { continuity }, { split, { rintros i hi, rw nat.le_zero_iff.mp hi, exact ⟨0, rfl⟩ }, { rw range_subset_iff, rintros x, exact hp' 0 (le_refl _) } } }, { rcases hn (λ i hi, hp' i $ nat.le_succ_of_le hi) with ⟨γ₀, hγ₀⟩, rcases h.joined_in (p' n) (p' $ n+1) (hp' n n.le_succ) (hp' (n+1) $ le_refl _) with ⟨γ₁, hγ₁⟩, let γ : path (p' 0) (p' $ n+1) := γ₀.trans γ₁, use γ, have range_eq : range γ = range γ₀ ∪ range γ₁ := γ₀.trans_range γ₁, split, { rintros i hi, by_cases hi' : i ≤ n, { rw range_eq, left, exact hγ₀.1 i hi' }, { rw [not_le, ← nat.succ_le_iff] at hi', have : i = n.succ := by linarith, rw this, use 1, exact γ.target } }, { rw range_eq, apply union_subset hγ₀.2, rw range_subset_iff, exact hγ₁ } } }, have hpp' : ∀ k < n+1, p k = p' k, { intros k hk, simp only [p', hk, dif_pos], congr, ext, rw fin.coe_coe_of_lt hk, norm_cast }, use γ.cast (hpp' 0 n.zero_lt_succ) (hpp' n n.lt_succ_self), simp only [γ.cast_coe], refine and.intro hγ.2 _, rintros ⟨i, hi⟩, convert hγ.1 i (nat.le_of_lt_succ hi), rw ← hpp' i hi, congr, ext, rw fin.coe_coe_of_lt hi, norm_cast end lemma is_path_connected.exists_path_through_family' {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), (∀ t, γ t ∈ s) ∧ ∀ i, γ (t i) = p i := begin rcases h.exists_path_through_family p hp with ⟨γ, hγ⟩, rcases hγ with ⟨h₁, h₂⟩, simp only [range, mem_set_of_eq] at h₂, rw range_subset_iff at h₁, choose! t ht using h₂, exact ⟨γ, t, h₁, ht⟩ end /-! ### Path connected spaces -/ /-- A topological space is path-connected if it is non-empty and every two points can be joined by a continuous path. -/ class path_connected_space (X : Type*) [topological_space X] : Prop := (nonempty : nonempty X) (joined : ∀ x y : X, joined x y) attribute [instance, priority 50] path_connected_space.nonempty lemma path_connected_space_iff_zeroth_homotopy : path_connected_space X ↔ nonempty (zeroth_homotopy X) ∧ subsingleton (zeroth_homotopy X) := begin letI := path_setoid X, split, { introI h, refine ⟨(nonempty_quotient_iff _).mpr h.1, ⟨_⟩⟩, rintros ⟨x⟩ ⟨y⟩, exact quotient.sound (path_connected_space.joined x y) }, { unfold zeroth_homotopy, rintros ⟨h, h'⟩, resetI, exact ⟨(nonempty_quotient_iff _).mp h, λ x y, quotient.exact $ subsingleton.elim ⟦x⟧ ⟦y⟧⟩ }, end namespace path_connected_space variables [path_connected_space X] /-- Use path-connectedness to build a path between two points. -/ def some_path (x y : X) : path x y := nonempty.some (joined x y) end path_connected_space lemma is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space F := begin rw is_path_connected_iff, split, { rintro ⟨⟨x, x_in⟩, h⟩, refine ⟨⟨⟨x, x_in⟩⟩, _⟩, rintros ⟨y, y_in⟩ ⟨z, z_in⟩, have H := h y z y_in z_in, rwa joined_in_iff_joined y_in z_in at H }, { rintros ⟨⟨x, x_in⟩, H⟩, refine ⟨⟨x, x_in⟩, λ y z y_in z_in, _⟩, rw joined_in_iff_joined y_in z_in, apply H } end lemma path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X) := begin split, { introI h, inhabit X, refine ⟨default X, mem_univ _, _⟩, simpa using path_connected_space.joined (default X) }, { intro h, have h' := h.joined_in, cases h with x h, exact ⟨⟨x⟩, by simpa using h'⟩ }, end lemma path_connected_space_iff_eq : path_connected_space X ↔ ∃ x : X, path_component x = univ := by simp [path_connected_space_iff_univ, is_path_connected_iff_eq] @[priority 100] -- see Note [lower instance priority] instance path_connected_space.connected_space [path_connected_space X] : connected_space X := begin rw connected_space_iff_connected_component, rcases is_path_connected_iff_eq.mp (path_connected_space_iff_univ.mp ‹_›) with ⟨x, x_in, hx⟩, use x, rw ← univ_subset_iff, exact (by simpa using hx : path_component x = univ) ▸ path_component_subset_component x end namespace path_connected_space variables [path_connected_space X] lemma exists_path_through_family {n : ℕ} (p : fin (n+1) → X) : ∃ γ : path (p 0) (p n), (∀ i, p i ∈ range γ) := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family p (λ i, true.intro) with ⟨γ, -, h⟩, exact ⟨γ, h⟩ end lemma exists_path_through_family' {n : ℕ} (p : fin (n+1) → X) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), ∀ i, γ (t i) = p i := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family' p (λ i, true.intro) with ⟨γ, t, -, h⟩, exact ⟨γ, t, h⟩ end end path_connected_space /-! ### Locally path connected spaces -/ /-- A topological space is locally path connected, at every point, path connected neighborhoods form a neighborhood basis. -/ class loc_path_connected_space (X : Type*) [topological_space X] : Prop := (path_connected_basis : ∀ x : X, (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s) id) export loc_path_connected_space (path_connected_basis) lemma loc_path_connected_of_bases {p : ι → Prop} {s : X → ι → set X} (h : ∀ x, (𝓝 x).has_basis p (s x)) (h' : ∀ x i, p i → is_path_connected (s x i)) : loc_path_connected_space X := begin constructor, intro x, apply (h x).to_has_basis, { intros i pi, exact ⟨s x i, ⟨(h x).mem_of_mem pi, h' x i pi⟩, by refl⟩ }, { rintros U ⟨U_in, hU⟩, rcases (h x).mem_iff.mp U_in with ⟨i, pi, hi⟩, tauto } end lemma path_connected_space_iff_connected_space [loc_path_connected_space X] : path_connected_space X ↔ connected_space X := begin split, { introI h, apply_instance }, { introI hX, inhabit X, let x₀ := default X, rw path_connected_space_iff_eq, use x₀, refine eq_univ_of_nonempty_clopen (by simp) ⟨_, _⟩, { rw is_open_iff_mem_nhds, intros y y_in, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, apply mem_of_superset U_in, rw ← path_component_congr y_in, exact hU.subset_path_component (mem_of_mem_nhds U_in) }, { rw is_closed_iff_nhds, intros y H, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, rcases H U U_in with ⟨z, hz, hz'⟩, exact ((hU.joined_in z y hz $ mem_of_mem_nhds U_in).joined.mem_path_component hz') } }, end lemma path_connected_subset_basis [loc_path_connected_space X] {U : set X} (h : is_open U) (hx : x ∈ U) : (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s ∧ s ⊆ U) id := (path_connected_basis x).has_basis_self_subset (is_open.mem_nhds h hx) lemma loc_path_connected_of_is_open [loc_path_connected_space X] {U : set X} (h : is_open U) : loc_path_connected_space U := ⟨begin rintros ⟨x, x_in⟩, rw nhds_subtype_eq_comap, constructor, intros V, rw (has_basis.comap (coe : U → X) (path_connected_subset_basis h x_in)).mem_iff, split, { rintros ⟨W, ⟨W_in, hW, hWU⟩, hWV⟩, exact ⟨coe ⁻¹' W, ⟨⟨preimage_mem_comap W_in, hW.preimage_coe hWU⟩, hWV⟩⟩ }, { rintros ⟨W, ⟨W_in, hW⟩, hWV⟩, refine ⟨coe '' W, ⟨filter.image_coe_mem_of_mem_comap (is_open.mem_nhds h x_in) W_in, hW.image continuous_subtype_coe, subtype.coe_image_subset U W⟩, _⟩, rintros x ⟨y, ⟨y_in, hy⟩⟩, rw ← subtype.coe_injective hy, tauto }, end⟩ lemma is_open.is_connected_iff_is_path_connected [loc_path_connected_space X] {U : set X} (U_op : is_open U) : is_path_connected U ↔ is_connected U := begin rw [is_connected_iff_connected_space, is_path_connected_iff_path_connected_space], haveI := loc_path_connected_of_is_open U_op, exact path_connected_space_iff_connected_space end
67f8326e6fcefdd2a67bb93b9c908bb908283136
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/bilinear_form.lean
29f68aba76bd8fd19cfed4589ef4f1a754a85b60
[ "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
23,826
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Kexing Ying -/ import linear_algebra.matrix.basis import linear_algebra.matrix.nondegenerate import linear_algebra.matrix.nonsingular_inverse import linear_algebra.matrix.to_linear_equiv import linear_algebra.bilinear_form /-! # Bilinear form This file defines the conversion between bilinear forms and matrices. ## Main definitions * `matrix.to_bilin` given a basis define a bilinear form * `matrix.to_bilin'` define the bilinear form on `n → R` * `bilin_form.to_matrix`: calculate the matrix coefficients of a bilinear form * `bilin_form.to_matrix'`: calculate the matrix coefficients of a bilinear form on `n → R` ## Notations In this file we use the following type variables: - `M`, `M'`, ... are modules over the semiring `R`, - `M₁`, `M₁'`, ... are modules over the ring `R₁`, - `M₂`, `M₂'`, ... are modules over the commutative semiring `R₂`, - `M₃`, `M₃'`, ... are modules over the commutative ring `R₃`, - `V`, ... is a vector space over the field `K`. ## Tags bilinear_form, matrix, basis -/ variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M] variables {R₁ : Type*} {M₁ : Type*} [ring R₁] [add_comm_group M₁] [module R₁ M₁] variables {R₂ : Type*} {M₂ : Type*} [comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂] variables {R₃ : Type*} {M₃ : Type*} [comm_ring R₃] [add_comm_group M₃] [module R₃ M₃] variables {V : Type*} {K : Type*} [field K] [add_comm_group V] [module K V] variables {B : bilin_form R M} {B₁ : bilin_form R₁ M₁} {B₂ : bilin_form R₂ M₂} section matrix variables {n o : Type*} open_locale big_operators open bilin_form finset linear_map matrix open_locale matrix /-- The map from `matrix n n R` to bilinear forms on `n → R`. This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/ def matrix.to_bilin'_aux [fintype n] (M : matrix n n R₂) : bilin_form R₂ (n → R₂) := { bilin := λ v w, ∑ i j, v i * M i j * w j, bilin_add_left := λ x y z, by simp only [pi.add_apply, add_mul, sum_add_distrib], bilin_smul_left := λ a x y, by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_sum], bilin_add_right := λ x y z, by simp only [pi.add_apply, mul_add, sum_add_distrib], bilin_smul_right := λ a x y, by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_left_comm, mul_sum] } lemma matrix.to_bilin'_aux_std_basis [fintype n] [decidable_eq n] (M : matrix n n R₂) (i j : n) : M.to_bilin'_aux (std_basis R₂ (λ _, R₂) i 1) (std_basis R₂ (λ _, R₂) j 1) = M i j := begin rw [matrix.to_bilin'_aux, coe_fn_mk, sum_eq_single i, sum_eq_single j], { simp only [std_basis_same, std_basis_same, one_mul, mul_one] }, { rintros j' - hj', apply mul_eq_zero_of_right, exact std_basis_ne R₂ (λ _, R₂) _ _ hj' 1 }, { intros, have := finset.mem_univ j, contradiction }, { rintros i' - hi', refine finset.sum_eq_zero (λ j _, _), apply mul_eq_zero_of_left, apply mul_eq_zero_of_left, exact std_basis_ne R₂ (λ _, R₂) _ _ hi' 1 }, { intros, have := finset.mem_univ i, contradiction } end /-- The linear map from bilinear forms to `matrix n n R` given an `n`-indexed basis. This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/ def bilin_form.to_matrix_aux (b : n → M₂) : bilin_form R₂ M₂ →ₗ[R₂] matrix n n R₂ := { to_fun := λ B i j, B (b i) (b j), map_add' := λ f g, rfl, map_smul' := λ f g, rfl } variables [fintype n] [fintype o] lemma to_bilin'_aux_to_matrix_aux [decidable_eq n] (B₃ : bilin_form R₃ (n → R₃)) : matrix.to_bilin'_aux (bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) B₃) = B₃ := begin refine ext_basis (pi.basis_fun R₃ n) (λ i j, _), rw [bilin_form.to_matrix_aux, linear_map.coe_mk, pi.basis_fun_apply, pi.basis_fun_apply, matrix.to_bilin'_aux_std_basis] end section to_matrix' /-! ### `to_matrix'` section This section deals with the conversion between matrices and bilinear forms on `n → R₃`. -/ variables [decidable_eq n] [decidable_eq o] /-- The linear equivalence between bilinear forms on `n → R` and `n × n` matrices -/ def bilin_form.to_matrix' : bilin_form R₃ (n → R₃) ≃ₗ[R₃] matrix n n R₃ := { inv_fun := matrix.to_bilin'_aux, left_inv := by convert to_bilin'_aux_to_matrix_aux, right_inv := λ M, by { ext i j, simp only [bilin_form.to_matrix_aux, matrix.to_bilin'_aux_std_basis] }, ..bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) } @[simp] lemma bilin_form.to_matrix_aux_std_basis (B : bilin_form R₃ (n → R₃)) : bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) B = bilin_form.to_matrix' B := rfl /-- The linear equivalence between `n × n` matrices and bilinear forms on `n → R` -/ def matrix.to_bilin' : matrix n n R₃ ≃ₗ[R₃] bilin_form R₃ (n → R₃) := bilin_form.to_matrix'.symm @[simp] lemma matrix.to_bilin'_aux_eq (M : matrix n n R₃) : matrix.to_bilin'_aux M = matrix.to_bilin' M := rfl lemma matrix.to_bilin'_apply (M : matrix n n R₃) (x y : n → R₃) : matrix.to_bilin' M x y = ∑ i j, x i * M i j * y j := rfl lemma matrix.to_bilin'_apply' (M : matrix n n R₃) (v w : n → R₃) : matrix.to_bilin' M v w = matrix.dot_product v (M.mul_vec w) := begin simp_rw [matrix.to_bilin'_apply, matrix.dot_product, matrix.mul_vec, matrix.dot_product], refine finset.sum_congr rfl (λ _ _, _), rw finset.mul_sum, refine finset.sum_congr rfl (λ _ _, _), rw ← mul_assoc, end @[simp] lemma matrix.to_bilin'_std_basis (M : matrix n n R₃) (i j : n) : matrix.to_bilin' M (std_basis R₃ (λ _, R₃) i 1) (std_basis R₃ (λ _, R₃) j 1) = M i j := matrix.to_bilin'_aux_std_basis M i j @[simp] lemma bilin_form.to_matrix'_symm : (bilin_form.to_matrix'.symm : matrix n n R₃ ≃ₗ[R₃] _) = matrix.to_bilin' := rfl @[simp] lemma matrix.to_bilin'_symm : (matrix.to_bilin'.symm : _ ≃ₗ[R₃] matrix n n R₃) = bilin_form.to_matrix' := bilin_form.to_matrix'.symm_symm @[simp] lemma matrix.to_bilin'_to_matrix' (B : bilin_form R₃ (n → R₃)) : matrix.to_bilin' (bilin_form.to_matrix' B) = B := matrix.to_bilin'.apply_symm_apply B @[simp] lemma bilin_form.to_matrix'_to_bilin' (M : matrix n n R₃) : bilin_form.to_matrix' (matrix.to_bilin' M) = M := bilin_form.to_matrix'.apply_symm_apply M @[simp] lemma bilin_form.to_matrix'_apply (B : bilin_form R₃ (n → R₃)) (i j : n) : bilin_form.to_matrix' B i j = B (std_basis R₃ (λ _, R₃) i 1) (std_basis R₃ (λ _, R₃) j 1) := rfl @[simp] lemma bilin_form.to_matrix'_comp (B : bilin_form R₃ (n → R₃)) (l r : (o → R₃) →ₗ[R₃] (n → R₃)) : (B.comp l r).to_matrix' = l.to_matrix'ᵀ ⬝ B.to_matrix' ⬝ r.to_matrix' := begin ext i j, simp only [bilin_form.to_matrix'_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply, linear_map.to_matrix', linear_equiv.coe_mk, sum_mul], rw sum_comm, conv_lhs { rw ← bilin_form.sum_repr_mul_repr_mul (pi.basis_fun R₃ n) (l _) (r _) }, rw finsupp.sum_fintype, { apply sum_congr rfl, rintros i' -, rw finsupp.sum_fintype, { apply sum_congr rfl, rintros j' -, simp only [smul_eq_mul, pi.basis_fun_repr, mul_assoc, mul_comm, mul_left_comm, pi.basis_fun_apply] }, { intros, simp only [zero_smul, smul_zero] } }, { intros, simp only [zero_smul, finsupp.sum_zero] } end lemma bilin_form.to_matrix'_comp_left (B : bilin_form R₃ (n → R₃)) (f : (n → R₃) →ₗ[R₃] (n → R₃)) : (B.comp_left f).to_matrix' = f.to_matrix'ᵀ ⬝ B.to_matrix' := by simp only [bilin_form.comp_left, bilin_form.to_matrix'_comp, to_matrix'_id, matrix.mul_one] lemma bilin_form.to_matrix'_comp_right (B : bilin_form R₃ (n → R₃)) (f : (n → R₃) →ₗ[R₃] (n → R₃)) : (B.comp_right f).to_matrix' = B.to_matrix' ⬝ f.to_matrix' := by simp only [bilin_form.comp_right, bilin_form.to_matrix'_comp, to_matrix'_id, transpose_one, matrix.one_mul] lemma bilin_form.mul_to_matrix'_mul (B : bilin_form R₃ (n → R₃)) (M : matrix o n R₃) (N : matrix n o R₃) : M ⬝ B.to_matrix' ⬝ N = (B.comp Mᵀ.to_lin' N.to_lin').to_matrix' := by simp only [B.to_matrix'_comp, transpose_transpose, to_matrix'_to_lin'] lemma bilin_form.mul_to_matrix' (B : bilin_form R₃ (n → R₃)) (M : matrix n n R₃) : M ⬝ B.to_matrix' = (B.comp_left Mᵀ.to_lin').to_matrix' := by simp only [B.to_matrix'_comp_left, transpose_transpose, to_matrix'_to_lin'] lemma bilin_form.to_matrix'_mul (B : bilin_form R₃ (n → R₃)) (M : matrix n n R₃) : B.to_matrix' ⬝ M = (B.comp_right M.to_lin').to_matrix' := by simp only [B.to_matrix'_comp_right, to_matrix'_to_lin'] lemma matrix.to_bilin'_comp (M : matrix n n R₃) (P Q : matrix n o R₃) : M.to_bilin'.comp P.to_lin' Q.to_lin' = (Pᵀ ⬝ M ⬝ Q).to_bilin' := bilin_form.to_matrix'.injective (by simp only [bilin_form.to_matrix'_comp, bilin_form.to_matrix'_to_bilin', to_matrix'_to_lin']) end to_matrix' section to_matrix /-! ### `to_matrix` section This section deals with the conversion between matrices and bilinear forms on a module with a fixed basis. -/ variables [decidable_eq n] (b : basis n R₃ M₃) /-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and `n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/ noncomputable def bilin_form.to_matrix : bilin_form R₃ M₃ ≃ₗ[R₃] matrix n n R₃ := (bilin_form.congr b.equiv_fun).trans bilin_form.to_matrix' /-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and `n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/ noncomputable def matrix.to_bilin : matrix n n R₃ ≃ₗ[R₃] bilin_form R₃ M₃ := (bilin_form.to_matrix b).symm @[simp] lemma basis.equiv_fun_symm_std_basis (i : n) : b.equiv_fun.symm (std_basis R₃ (λ _, R₃) i 1) = b i := begin rw [b.equiv_fun_symm_apply, finset.sum_eq_single i], { rw [std_basis_same, one_smul] }, { rintros j - hj, rw [std_basis_ne _ _ _ _ hj, zero_smul] }, { intro, have := mem_univ i, contradiction } end @[simp] lemma bilin_form.to_matrix_apply (B : bilin_form R₃ M₃) (i j : n) : bilin_form.to_matrix b B i j = B (b i) (b j) := by rw [bilin_form.to_matrix, linear_equiv.trans_apply, bilin_form.to_matrix'_apply, congr_apply, b.equiv_fun_symm_std_basis, b.equiv_fun_symm_std_basis] @[simp] lemma matrix.to_bilin_apply (M : matrix n n R₃) (x y : M₃) : matrix.to_bilin b M x y = ∑ i j, b.repr x i * M i j * b.repr y j := begin rw [matrix.to_bilin, bilin_form.to_matrix, linear_equiv.symm_trans_apply, ← matrix.to_bilin'], simp only [congr_symm, congr_apply, linear_equiv.symm_symm, matrix.to_bilin'_apply, basis.equiv_fun_apply] end -- Not a `simp` lemma since `bilin_form.to_matrix` needs an extra argument lemma bilinear_form.to_matrix_aux_eq (B : bilin_form R₃ M₃) : bilin_form.to_matrix_aux b B = bilin_form.to_matrix b B := ext (λ i j, by rw [bilin_form.to_matrix_apply, bilin_form.to_matrix_aux, linear_map.coe_mk]) @[simp] lemma bilin_form.to_matrix_symm : (bilin_form.to_matrix b).symm = matrix.to_bilin b := rfl @[simp] lemma matrix.to_bilin_symm : (matrix.to_bilin b).symm = bilin_form.to_matrix b := (bilin_form.to_matrix b).symm_symm lemma matrix.to_bilin_basis_fun : matrix.to_bilin (pi.basis_fun R₃ n) = matrix.to_bilin' := by { ext M, simp only [matrix.to_bilin_apply, matrix.to_bilin'_apply, pi.basis_fun_repr] } lemma bilin_form.to_matrix_basis_fun : bilin_form.to_matrix (pi.basis_fun R₃ n) = bilin_form.to_matrix' := by { ext B, rw [bilin_form.to_matrix_apply, bilin_form.to_matrix'_apply, pi.basis_fun_apply, pi.basis_fun_apply] } @[simp] lemma matrix.to_bilin_to_matrix (B : bilin_form R₃ M₃) : matrix.to_bilin b (bilin_form.to_matrix b B) = B := (matrix.to_bilin b).apply_symm_apply B @[simp] lemma bilin_form.to_matrix_to_bilin (M : matrix n n R₃) : bilin_form.to_matrix b (matrix.to_bilin b M) = M := (bilin_form.to_matrix b).apply_symm_apply M variables {M₃' : Type*} [add_comm_group M₃'] [module R₃ M₃'] variables (c : basis o R₃ M₃') variables [decidable_eq o] -- Cannot be a `simp` lemma because `b` must be inferred. lemma bilin_form.to_matrix_comp (B : bilin_form R₃ M₃) (l r : M₃' →ₗ[R₃] M₃) : bilin_form.to_matrix c (B.comp l r) = (to_matrix c b l)ᵀ ⬝ bilin_form.to_matrix b B ⬝ to_matrix c b r := begin ext i j, simp only [bilin_form.to_matrix_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply, linear_map.to_matrix', linear_equiv.coe_mk, sum_mul], rw sum_comm, conv_lhs { rw ← bilin_form.sum_repr_mul_repr_mul b }, rw finsupp.sum_fintype, { apply sum_congr rfl, rintros i' -, rw finsupp.sum_fintype, { apply sum_congr rfl, rintros j' -, simp only [smul_eq_mul, linear_map.to_matrix_apply, basis.equiv_fun_apply, mul_assoc, mul_comm, mul_left_comm] }, { intros, simp only [zero_smul, smul_zero] } }, { intros, simp only [zero_smul, finsupp.sum_zero] } end lemma bilin_form.to_matrix_comp_left (B : bilin_form R₃ M₃) (f : M₃ →ₗ[R₃] M₃) : bilin_form.to_matrix b (B.comp_left f) = (to_matrix b b f)ᵀ ⬝ bilin_form.to_matrix b B := by simp only [comp_left, bilin_form.to_matrix_comp b b, to_matrix_id, matrix.mul_one] lemma bilin_form.to_matrix_comp_right (B : bilin_form R₃ M₃) (f : M₃ →ₗ[R₃] M₃) : bilin_form.to_matrix b (B.comp_right f) = bilin_form.to_matrix b B ⬝ (to_matrix b b f) := by simp only [bilin_form.comp_right, bilin_form.to_matrix_comp b b, to_matrix_id, transpose_one, matrix.one_mul] @[simp] lemma bilin_form.to_matrix_mul_basis_to_matrix (c : basis o R₃ M₃) (B : bilin_form R₃ M₃) : (b.to_matrix c)ᵀ ⬝ bilin_form.to_matrix b B ⬝ b.to_matrix c = bilin_form.to_matrix c B := by rw [← linear_map.to_matrix_id_eq_basis_to_matrix, ← bilin_form.to_matrix_comp, bilin_form.comp_id_id] lemma bilin_form.mul_to_matrix_mul (B : bilin_form R₃ M₃) (M : matrix o n R₃) (N : matrix n o R₃) : M ⬝ bilin_form.to_matrix b B ⬝ N = bilin_form.to_matrix c (B.comp (to_lin c b Mᵀ) (to_lin c b N)) := by simp only [B.to_matrix_comp b c, to_matrix_to_lin, transpose_transpose] lemma bilin_form.mul_to_matrix (B : bilin_form R₃ M₃) (M : matrix n n R₃) : M ⬝ bilin_form.to_matrix b B = bilin_form.to_matrix b (B.comp_left (to_lin b b Mᵀ)) := by rw [B.to_matrix_comp_left b, to_matrix_to_lin, transpose_transpose] lemma bilin_form.to_matrix_mul (B : bilin_form R₃ M₃) (M : matrix n n R₃) : bilin_form.to_matrix b B ⬝ M = bilin_form.to_matrix b (B.comp_right (to_lin b b M)) := by rw [B.to_matrix_comp_right b, to_matrix_to_lin] lemma matrix.to_bilin_comp (M : matrix n n R₃) (P Q : matrix n o R₃) : (matrix.to_bilin b M).comp (to_lin c b P) (to_lin c b Q) = matrix.to_bilin c (Pᵀ ⬝ M ⬝ Q) := (bilin_form.to_matrix c).injective (by simp only [bilin_form.to_matrix_comp b c, bilin_form.to_matrix_to_bilin, to_matrix_to_lin]) end to_matrix end matrix section matrix_adjoints open_locale matrix variables {n : Type*} [fintype n] variables (b : basis n R₃ M₃) variables (J J₃ A A' : matrix n n R₃) /-- The condition for the square matrices `A`, `A'` to be an adjoint pair with respect to the square matrices `J`, `J₃`. -/ def matrix.is_adjoint_pair := Aᵀ ⬝ J₃ = J ⬝ A' /-- The condition for a square matrix `A` to be self-adjoint with respect to the square matrix `J`. -/ def matrix.is_self_adjoint := matrix.is_adjoint_pair J J A A /-- The condition for a square matrix `A` to be skew-adjoint with respect to the square matrix `J`. -/ def matrix.is_skew_adjoint := matrix.is_adjoint_pair J J A (-A) @[simp] lemma is_adjoint_pair_to_bilin' [decidable_eq n] : bilin_form.is_adjoint_pair (matrix.to_bilin' J) (matrix.to_bilin' J₃) (matrix.to_lin' A) (matrix.to_lin' A') ↔ matrix.is_adjoint_pair J J₃ A A' := begin rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right, have h : ∀ (B B' : bilin_form R₃ (n → R₃)), B = B' ↔ (bilin_form.to_matrix' B) = (bilin_form.to_matrix' B'), { intros B B', split; intros h, { rw h }, { exact bilin_form.to_matrix'.injective h } }, rw [h, bilin_form.to_matrix'_comp_left, bilin_form.to_matrix'_comp_right, linear_map.to_matrix'_to_lin', linear_map.to_matrix'_to_lin', bilin_form.to_matrix'_to_bilin', bilin_form.to_matrix'_to_bilin'], refl, end @[simp] lemma is_adjoint_pair_to_bilin [decidable_eq n] : bilin_form.is_adjoint_pair (matrix.to_bilin b J) (matrix.to_bilin b J₃) (matrix.to_lin b b A) (matrix.to_lin b b A') ↔ matrix.is_adjoint_pair J J₃ A A' := begin rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right, have h : ∀ (B B' : bilin_form R₃ M₃), B = B' ↔ (bilin_form.to_matrix b B) = (bilin_form.to_matrix b B'), { intros B B', split; intros h, { rw h }, { exact (bilin_form.to_matrix b).injective h } }, rw [h, bilin_form.to_matrix_comp_left, bilin_form.to_matrix_comp_right, linear_map.to_matrix_to_lin, linear_map.to_matrix_to_lin, bilin_form.to_matrix_to_bilin, bilin_form.to_matrix_to_bilin], refl, end lemma matrix.is_adjoint_pair_equiv [decidable_eq n] (P : matrix n n R₃) (h : is_unit P) : (Pᵀ ⬝ J ⬝ P).is_adjoint_pair (Pᵀ ⬝ J ⬝ P) A A' ↔ J.is_adjoint_pair J (P ⬝ A ⬝ P⁻¹) (P ⬝ A' ⬝ P⁻¹) := have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h, begin let u := P.nonsing_inv_unit h', let v := Pᵀ.nonsing_inv_unit (P.is_unit_det_transpose h'), let x := Aᵀ * Pᵀ * J, let y := J * P * A', suffices : x * ↑u = ↑v * y ↔ ↑v⁻¹ * x = y * ↑u⁻¹, { dunfold matrix.is_adjoint_pair, repeat { rw matrix.transpose_mul, }, simp only [←matrix.mul_eq_mul, ←mul_assoc, P.transpose_nonsing_inv], conv_lhs { to_rhs, rw [mul_assoc, mul_assoc], congr, skip, rw ←mul_assoc, }, conv_rhs { rw [mul_assoc, mul_assoc], conv { to_lhs, congr, skip, rw ←mul_assoc }, }, exact this, }, rw units.eq_mul_inv_iff_mul_eq, conv_rhs { rw mul_assoc, }, rw v.inv_mul_eq_iff_eq_mul, end variables [decidable_eq n] /-- The submodule of pair-self-adjoint matrices with respect to bilinear forms corresponding to given matrices `J`, `J₂`. -/ def pair_self_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) := (bilin_form.is_pair_self_adjoint_submodule (matrix.to_bilin' J) (matrix.to_bilin' J₃)).map ((linear_map.to_matrix' : ((n → R₃) →ₗ[R₃] (n → R₃)) ≃ₗ[R₃] matrix n n R₃) : ((n → R₃) →ₗ[R₃] (n → R₃)) →ₗ[R₃] matrix n n R₃) @[simp] lemma mem_pair_self_adjoint_matrices_submodule : A ∈ (pair_self_adjoint_matrices_submodule J J₃) ↔ matrix.is_adjoint_pair J J₃ A A := begin simp only [pair_self_adjoint_matrices_submodule, linear_equiv.coe_coe, linear_map.to_matrix'_apply, submodule.mem_map, bilin_form.mem_is_pair_self_adjoint_submodule], split, { rintros ⟨f, hf, hA⟩, have hf' : f = A.to_lin' := by rw [←hA, matrix.to_lin'_to_matrix'], rw hf' at hf, rw ← is_adjoint_pair_to_bilin', exact hf, }, { intros h, refine ⟨A.to_lin', _, linear_map.to_matrix'_to_lin' _⟩, exact (is_adjoint_pair_to_bilin' _ _ _ _).mpr h, }, end /-- The submodule of self-adjoint matrices with respect to the bilinear form corresponding to the matrix `J`. -/ def self_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) := pair_self_adjoint_matrices_submodule J J @[simp] lemma mem_self_adjoint_matrices_submodule : A ∈ self_adjoint_matrices_submodule J ↔ J.is_self_adjoint A := by { erw mem_pair_self_adjoint_matrices_submodule, refl, } /-- The submodule of skew-adjoint matrices with respect to the bilinear form corresponding to the matrix `J`. -/ def skew_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) := pair_self_adjoint_matrices_submodule (-J) J @[simp] lemma mem_skew_adjoint_matrices_submodule : A ∈ skew_adjoint_matrices_submodule J ↔ J.is_skew_adjoint A := begin erw mem_pair_self_adjoint_matrices_submodule, simp [matrix.is_skew_adjoint, matrix.is_adjoint_pair], end end matrix_adjoints namespace bilin_form section det open matrix variables {A : Type*} [comm_ring A] [is_domain A] [module A M₃] (B₃ : bilin_form A M₃) variables {ι : Type*} [decidable_eq ι] [fintype ι] lemma _root_.matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin {M : matrix ι ι R₃} (b : basis ι R₃ M₃) : M.to_bilin'.nondegenerate ↔ (matrix.to_bilin b M).nondegenerate := (nondegenerate_congr_iff b.equiv_fun.symm).symm -- Lemmas transferring nondegeneracy between a matrix and its associated bilinear form theorem _root_.matrix.nondegenerate.to_bilin' {M : matrix ι ι R₃} (h : M.nondegenerate) : M.to_bilin'.nondegenerate := λ x hx, h.eq_zero_of_ortho $ λ y, by simpa only [to_bilin'_apply'] using hx y @[simp] lemma _root_.matrix.nondegenerate_to_bilin'_iff {M : matrix ι ι R₃} : M.to_bilin'.nondegenerate ↔ M.nondegenerate := ⟨λ h v hv, h v $ λ w, (M.to_bilin'_apply' _ _).trans $ hv w, matrix.nondegenerate.to_bilin'⟩ theorem _root_.matrix.nondegenerate.to_bilin {M : matrix ι ι R₃} (h : M.nondegenerate) (b : basis ι R₃ M₃) : (to_bilin b M).nondegenerate := (matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin b).mp h.to_bilin' @[simp] lemma _root_.matrix.nondegenerate_to_bilin_iff {M : matrix ι ι R₃} (b : basis ι R₃ M₃) : (to_bilin b M).nondegenerate ↔ M.nondegenerate := by rw [←matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin, matrix.nondegenerate_to_bilin'_iff] -- Lemmas transferring nondegeneracy between a bilinear form and its associated matrix @[simp] theorem nondegenerate_to_matrix'_iff {B : bilin_form R₃ (ι → R₃)} : B.to_matrix'.nondegenerate ↔ B.nondegenerate := matrix.nondegenerate_to_bilin'_iff.symm.trans $ (matrix.to_bilin'_to_matrix' B).symm ▸ iff.rfl theorem nondegenerate.to_matrix' {B : bilin_form R₃ (ι → R₃)} (h : B.nondegenerate) : B.to_matrix'.nondegenerate := nondegenerate_to_matrix'_iff.mpr h @[simp] theorem nondegenerate_to_matrix_iff {B : bilin_form R₃ M₃} (b : basis ι R₃ M₃) : (to_matrix b B).nondegenerate ↔ B.nondegenerate := (matrix.nondegenerate_to_bilin_iff b).symm.trans $ (matrix.to_bilin_to_matrix b B).symm ▸ iff.rfl theorem nondegenerate.to_matrix {B : bilin_form R₃ M₃} (h : B.nondegenerate) (b : basis ι R₃ M₃) : (to_matrix b B).nondegenerate := (nondegenerate_to_matrix_iff b).mpr h -- Some shorthands for combining the above with `matrix.nondegenerate_of_det_ne_zero` lemma nondegenerate_to_bilin'_iff_det_ne_zero {M : matrix ι ι A} : M.to_bilin'.nondegenerate ↔ M.det ≠ 0 := by rw [matrix.nondegenerate_to_bilin'_iff, matrix.nondegenerate_iff_det_ne_zero] theorem nondegenerate_to_bilin'_of_det_ne_zero' (M : matrix ι ι A) (h : M.det ≠ 0) : M.to_bilin'.nondegenerate := nondegenerate_to_bilin'_iff_det_ne_zero.mpr h lemma nondegenerate_iff_det_ne_zero {B : bilin_form A M₃} (b : basis ι A M₃) : B.nondegenerate ↔ (to_matrix b B).det ≠ 0 := by rw [←matrix.nondegenerate_iff_det_ne_zero, nondegenerate_to_matrix_iff] theorem nondegenerate_of_det_ne_zero (b : basis ι A M₃) (h : (to_matrix b B₃).det ≠ 0) : B₃.nondegenerate := (nondegenerate_iff_det_ne_zero b).mpr h end det end bilin_form
0d662fa308a8120e0f744e757b71de0dbe2b1d85
b9def12ac9858ba514e44c0758ebb4e9b73ae5ed
/src/monoidal_categories_reboot/monoidal_functor.lean
b105a3ad5f7f3b05b38eb9d01eb913a1ace07789
[ "Apache-2.0" ]
permissive
cipher1024/monoidal-categories-reboot
5f826017db2f71920336331739a0f84be2f97bf7
998f2a0553c22369d922195dc20a20fa7dccc6e5
refs/heads/master
1,586,710,273,395
1,543,592,573,000
1,543,592,573,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,961
lean
-- Copyright (c) 2018 Michael Jendrusch. All rights reserved. import category_theory.category import category_theory.functor import category_theory.products import category_theory.natural_isomorphism import .tensor_product import .monoidal_category import tidy.rewrite_search import tactic.interactive import .slice_tactic open category_theory open category_theory.slice open tactic universe u universes u₁ u₂ u₃ v₁ v₂ v₃ open category_theory.category open category_theory.functor open category_theory.prod open category_theory.functor.category.nat_trans open category_theory.nat_iso namespace category_theory.monoidal section open monoidal_category structure monoidal_functor (C : Type u₁) [𝒞 : monoidal_category.{u₁ v₁} C] (D : Type u₂) [𝒟 : monoidal_category.{u₂ v₂} D] extends category_theory.functor C D := -- unit morphism (ε : tensor_unit D ≅ obj (tensor_unit C)) -- natural transformation (μ : Π X Y : C, (obj X) ⊗ (obj Y) ≅ obj (X ⊗ Y)) (μ_natural' : ∀ (X Y X' Y' : C) (f : X ⟶ Y) (g : X' ⟶ Y'), (μ X X').hom ≫ map (f ⊗ g) = ((map f) ⊗ (map g)) ≫ (μ Y Y').hom . obviously) -- associativity (associativity' : ∀ (X Y Z : C), ((μ X Y).hom ⊗ 𝟙 (obj Z)) ≫ (μ (X ⊗ Y) Z).hom ≫ map (associator X Y Z).hom = (associator (obj X) (obj Y) (obj Z)).hom ≫ (𝟙 (obj X) ⊗ (μ Y Z).hom) ≫ (μ X (Y ⊗ Z)).hom . obviously) -- unitality (left_unitality' : ∀ X : C, (left_unitor (obj X)).hom = (ε.hom ⊗ 𝟙 (obj X)) ≫ (μ (tensor_unit C) X).hom ≫ map (left_unitor X).hom . obviously) (right_unitality' : ∀ X : C, (right_unitor (obj X)).hom = (𝟙 (obj X) ⊗ ε.hom) ≫ (μ X (tensor_unit C)).hom ≫ map (right_unitor X).hom . obviously) restate_axiom monoidal_functor.μ_natural' attribute [simp,search] monoidal_functor.μ_natural restate_axiom monoidal_functor.left_unitality' attribute [simp,search] monoidal_functor.left_unitality restate_axiom monoidal_functor.right_unitality' attribute [simp,search] monoidal_functor.right_unitality restate_axiom monoidal_functor.associativity' attribute [simp,search] monoidal_functor.associativity end namespace monoidal_functor variables {C : Type u₁} [𝒞 : monoidal_category.{u₁ v₁} C] variables {D : Type u₂} [𝒟 : monoidal_category.{u₂ v₂} D] include 𝒞 𝒟 -- This is unfortunate; we need all sorts of struts to give -- monoidal functors the features of functors... @[reducible] def on_iso (F : monoidal_functor C D) {X Y : C} (f : X ≅ Y) : F.obj X ≅ F.obj Y := F.to_functor.on_iso f @[search] lemma map_id (F : monoidal_functor C D) (X : C) : F.map (𝟙 X) = 𝟙 (F.obj X) := F.map_id' X @[search] lemma map_comp (F : monoidal_functor C D) {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : F.map (f ≫ g) = F.map f ≫ F.map g := F.map_comp' f g end monoidal_functor section variables (C : Type u₁) [𝒞 : monoidal_category.{u₁ v₁} C] variables (D : Type u₂) [𝒟 : monoidal_category.{u₂ v₂} D] variables (E : Type u₃) [ℰ : monoidal_category.{u₃ v₃} E] include 𝒞 𝒟 ℰ open tidy.rewrite_search.tracer -- set_option profiler true def monoidal_functor.comp (F : monoidal_functor C D) (G : monoidal_functor D E) : monoidal_functor C E := { ε := G.ε ≪≫ (G.on_iso F.ε), μ := λ X Y, G.μ (F.obj X) (F.obj Y) ≪≫ G.on_iso (F.μ X Y), μ_natural' := begin tidy, /- `rewrite_search` says -/ -- FIXME actually, its output is broken conv_lhs { congr, skip, erw [←map_comp] }, conv_lhs { erw [monoidal_functor.μ_natural] }, conv_lhs { congr, skip, erw [map_comp] }, conv_lhs { erw [←category.assoc] }, conv_lhs { congr, erw [monoidal_functor.μ_natural] }, conv_rhs { erw [←category.assoc] }, end, associativity' := λ X Y Z, begin -- obviously fails here, but it seems like it should be doable! dsimp, conv { to_rhs, rw ←interchange_right_identity, slice 3 4, rw ← G.map_id, rw ← G.μ_natural, }, -- rewrite_search { view := visualiser, trace_summary := tt, explain := tt, max_iterations := 50 }, -- fails conv { to_rhs, slice 1 3, rw ←G.associativity, }, -- rewrite_search (saw/visited/used) 137/23/16 expressions during proof of category_theory.monoidal.monoidal_functor.comp conv { to_lhs, rw ←interchange_left_identity, slice 2 3, rw ← G.map_id, rw ← G.μ_natural, }, repeat { rw category.assoc }, repeat { rw ←G.map_comp }, rw F.associativity, end, left_unitality' := λ X, begin -- Don't attempt to read this; it is a Frankenstein effort of Scott + rewrite_search dsimp, rw G.left_unitality, rw ←interchange_left_identity, repeat {rw category.assoc}, apply congr_arg, /- `rewrite_search` says -/ -- FIXME actually, its output is broken rw F.left_unitality, conv_lhs { congr, skip, erw [map_comp] }, conv_lhs { erw [←category.id_app] }, conv_lhs { erw [←category.assoc] }, conv_lhs { congr, erw [monoidal_functor.μ_natural] }, conv_lhs { congr, congr, congr, skip, erw [map_id] }, conv_rhs { erw [←category.assoc] }, erw map_comp, end, right_unitality' := λ X, begin dsimp, rw G.right_unitality, rw ←interchange_right_identity, repeat {rw category.assoc}, apply congr_arg, /- `rewrite_search` says -/ -- FIXME actually, its output is broken rw F.right_unitality, conv_lhs { congr, skip, erw [map_comp] }, conv_lhs { erw [←category.id_app] }, conv_lhs { erw [←category.assoc] }, conv_lhs { congr, erw [monoidal_functor.μ_natural] }, conv_lhs { congr, congr, congr, erw [map_id] }, conv_rhs { erw [←category.assoc] }, erw map_comp, end, .. (F.to_functor) ⋙ (G.to_functor) } end end category_theory.monoidal
b340dc5ddd4ac6d9926cc11dcae637359caaf788
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/complete_rec_var.lean
9235f83a40b3580da07483cfdb6eb85a2639a810
[ "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
246
lean
def f : nat → nat → nat | (x+1) (y+1) := f (x+10) y | _ _ := 1 vm_eval f 1 1000 example (x y) : f (x+1) (y+1) = f (x+10) y := rfl example (y) : f 0 (y+1) = 1 := rfl example (x) : f (x+1) 0 = 1 := rfl example : f 0 0 = 1 := rfl
313fb931e87a768f12a949b6906237e98e78cdd3
d1bbf1801b3dcb214451d48214589f511061da63
/src/field_theory/minpoly.lean
15613a95aee3976e256110580c2085a9801830ae
[ "Apache-2.0" ]
permissive
cheraghchi/mathlib
5c366f8c4f8e66973b60c37881889da8390cab86
f29d1c3038422168fbbdb2526abf7c0ff13e86db
refs/heads/master
1,676,577,831,283
1,610,894,638,000
1,610,894,638,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,297
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johan Commelin -/ import ring_theory.integral_closure import data.polynomial.field_division import ring_theory.polynomial.gauss_lemma /-! # Minimal polynomials This file defines the minimal polynomial of an element x of an A-algebra B, under the assumption that x is integral over A. After stating the defining property we specialize to the setting of field extensions and derive some well-known properties, amongst which the fact that minimal polynomials are irreducible, and uniquely determined by their defining property. -/ universes u v w open_locale classical open polynomial set function variables {α : Type u} {β : Type v} section min_poly_def variables [comm_ring α] [ring β] [algebra α β] /-- Let `B` be an `A`-algebra, and `x` an element of `B` that is integral over `A` so we have some term `hx : is_integral A x`. The minimal polynomial `minpoly hx` of `x` is a monic polynomial of smallest degree that has `x` as its root. For instance, if `V` is a `K`-vector space for some field `K`, and `f : V →ₗ[K] V` then the minimal polynomial of `f` is `minpoly f.is_integral`. -/ noncomputable def minpoly {x : β} (hx : is_integral α x) : polynomial α := well_founded.min degree_lt_wf _ hx end min_poly_def namespace minpoly section ring variables [comm_ring α] [ring β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is monic.-/ lemma monic : monic (minpoly hx) := (well_founded.min_mem degree_lt_wf _ hx).1 /--An element is a root of its minimal polynomial.-/ @[simp] lemma aeval : aeval x (minpoly hx) = 0 := (well_founded.min_mem degree_lt_wf _ hx).2 /--The defining property of the minimal polynomial of an element x: it is the monic polynomial with smallest degree that has x as its root.-/ lemma min {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval x p = 0) : degree (minpoly hx) ≤ degree p := le_of_not_lt $ well_founded.not_lt_min degree_lt_wf _ hx ⟨pmonic, hp⟩ /-- A minimal polynomial is nonzero. -/ lemma ne_zero [nontrivial α] : (minpoly hx) ≠ 0 := ne_zero_of_monic (monic hx) /-- If an element `x` is a root of a nonzero monic polynomial `p`, then the degree of `p` is at least the degree of the minimal polynomial of `x`. -/ lemma degree_le_of_monic {p : polynomial α} (hmonic : p.monic) (hp : polynomial.aeval x p = 0) : degree (minpoly hx) ≤ degree p := min _ hmonic (by simp [hp]) end ring section integral_domain variables [integral_domain α] section ring variables [ring β] [algebra α β] [nontrivial β] variables {x : β} (hx : is_integral α x) /--The degree of a minimal polynomial is positive. -/ lemma degree_pos [nontrivial α] : 0 < degree (minpoly hx) := begin apply lt_of_le_of_ne, { simpa only [zero_le_degree_iff] using ne_zero hx }, assume deg_eq_zero, rw eq_comm at deg_eq_zero, have ndeg_eq_zero : nat_degree (minpoly hx) = 0, { simpa using congr_arg nat_degree (eq_C_of_degree_eq_zero deg_eq_zero) }, have eq_one : minpoly hx = 1, { rw eq_C_of_degree_eq_zero deg_eq_zero, convert C_1, simpa only [ndeg_eq_zero.symm] using (monic hx).leading_coeff }, simpa only [eq_one, alg_hom.map_one, one_ne_zero] using aeval hx end /-- If `L/K` is a ring extension, and `x` is an element of `L` in the image of `K`, then the minimal polynomial of `x` is `X - C x`. -/ lemma eq_X_sub_C_of_algebra_map_inj [nontrivial α] (a : α) (hf : function.injective (algebra_map α β)) : minpoly (@is_integral_algebra_map α β _ _ _ a) = X - C a := begin have hdegle : (minpoly (@is_integral_algebra_map α β _ _ _ a)).nat_degree ≤ 1, { apply with_bot.coe_le_coe.1, rw [←degree_eq_nat_degree (ne_zero (@is_integral_algebra_map α β _ _ _ a)), with_top.coe_one, ←degree_X_sub_C a], refine degree_le_of_monic (@is_integral_algebra_map α β _ _ _ a) (monic_X_sub_C a) _, simp only [aeval_C, aeval_X, alg_hom.map_sub, sub_self] }, have hdeg : (minpoly (@is_integral_algebra_map α β _ _ _ a)).degree = 1, { apply (degree_eq_iff_nat_degree_eq (ne_zero (@is_integral_algebra_map α β _ _ _ a))).2, exact (has_le.le.antisymm hdegle (nat.succ_le_of_lt (with_bot.coe_lt_coe.1 (lt_of_lt_of_le (degree_pos (@is_integral_algebra_map α β _ _ _ a)) degree_le_nat_degree)))) }, have hrw := eq_X_add_C_of_degree_eq_one hdeg, simp only [monic (@is_integral_algebra_map α β _ _ _ a), one_mul, monic.leading_coeff, ring_hom.map_one] at hrw, have h0 : (minpoly (@is_integral_algebra_map α β _ _ _ a)).coeff 0 = -a, { have hroot := aeval (@is_integral_algebra_map α β _ _ _ a), rw [hrw, add_comm] at hroot, simp only [aeval_C, aeval_X, aeval_add] at hroot, replace hroot := eq_neg_of_add_eq_zero hroot, rw [←ring_hom.map_neg _ a] at hroot, exact (hf hroot) }, rw hrw, simp only [h0, ring_hom.map_neg, sub_eq_add_neg], end /-- A minimal polynomial is not a unit. -/ lemma not_is_unit : ¬ is_unit (minpoly hx) := assume H, (ne_of_lt (degree_pos hx)).symm $ degree_eq_zero_of_is_unit H end ring section domain variables [domain β] [algebra α β] variables {x : β} (hx : is_integral α x) /-- If `a` strictly divides the minimal polynomial of `x`, then `x` cannot be a root for `a`. -/ lemma aeval_ne_zero_of_dvd_not_unit_minpoly {a : polynomial α} (hamonic : a.monic) (hdvd : dvd_not_unit a (minpoly hx)) : polynomial.aeval x a ≠ 0 := begin intro ha, refine not_lt_of_ge (minpoly.min hx hamonic ha) _, obtain ⟨hzeroa, b, hb_nunit, prod⟩ := hdvd, have hbmonic : b.monic, { rw monic.def, have := monic hx, rwa [monic.def, prod, leading_coeff_mul, monic.def.mp hamonic, one_mul] at this }, have hzerob : b ≠ 0 := hbmonic.ne_zero, have degbzero : 0 < b.nat_degree, { apply nat.pos_of_ne_zero, intro h, have h₁ := eq_C_of_nat_degree_eq_zero h, rw [←h, ←leading_coeff, monic.def.1 hbmonic, C_1] at h₁, rw h₁ at hb_nunit, have := is_unit_one, contradiction }, rw [prod, degree_mul, degree_eq_nat_degree hzeroa, degree_eq_nat_degree hzerob], exact_mod_cast lt_add_of_pos_right _ degbzero, end /--A minimal polynomial is irreducible.-/ lemma irreducible : irreducible (minpoly hx) := begin cases irreducible_or_factor (minpoly hx) (not_is_unit hx) with hirr hred, { exact hirr }, exfalso, obtain ⟨a, b, ha_nunit, hb_nunit, hab_eq⟩ := hred, have coeff_prod : a.leading_coeff * b.leading_coeff = 1, { rw [←monic.def.1 (monic hx), ←hab_eq], simp only [leading_coeff_mul] }, have hamonic : (a * C b.leading_coeff).monic, { rw monic.def, simp only [coeff_prod, leading_coeff_mul, leading_coeff_C] }, have hbmonic : (b * C a.leading_coeff).monic, { rw [monic.def, mul_comm], simp only [coeff_prod, leading_coeff_mul, leading_coeff_C] }, have prod : minpoly hx = (a * C b.leading_coeff) * (b * C a.leading_coeff), { symmetry, calc a * C b.leading_coeff * (b * C a.leading_coeff) = a * b * (C a.leading_coeff * C b.leading_coeff) : by ring ... = a * b * (C (a.leading_coeff * b.leading_coeff)) : by simp only [ring_hom.map_mul] ... = a * b : by rw [coeff_prod, C_1, mul_one] ... = minpoly hx : hab_eq }, have hzero := aeval hx, rw [prod, aeval_mul, mul_eq_zero] at hzero, cases hzero, { refine aeval_ne_zero_of_dvd_not_unit_minpoly hx hamonic _ hzero, exact ⟨hamonic.ne_zero, _, mt is_unit_of_mul_is_unit_left hb_nunit, prod⟩ }, { refine aeval_ne_zero_of_dvd_not_unit_minpoly hx hbmonic _ hzero, rw mul_comm at prod, exact ⟨hbmonic.ne_zero, _, mt is_unit_of_mul_is_unit_left ha_nunit, prod⟩ }, end end domain end integral_domain section field variables [field α] section ring variables [ring β] [algebra α β] variables {x : β} (hx : is_integral α x) /--If an element x is a root of a nonzero polynomial p, then the degree of p is at least the degree of the minimal polynomial of x.-/ lemma degree_le_of_ne_zero {p : polynomial α} (pnz : p ≠ 0) (hp : polynomial.aeval x p = 0) : degree (minpoly hx) ≤ degree p := calc degree (minpoly hx) ≤ degree (p * C (leading_coeff p)⁻¹) : min _ (monic_mul_leading_coeff_inv pnz) (by simp [hp]) ... = degree p : degree_mul_leading_coeff_inv p pnz /--The minimal polynomial of an element x is uniquely characterized by its defining property: if there is another monic polynomial of minimal degree that has x as a root, then this polynomial is equal to the minimal polynomial of x.-/ lemma unique {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval x p = 0) (pmin : ∀ q : polynomial α, q.monic → polynomial.aeval x q = 0 → degree p ≤ degree q) : p = minpoly hx := begin symmetry, apply eq_of_sub_eq_zero, by_contra hnz, have := degree_le_of_ne_zero hx hnz (by simp [hp]), contrapose! this, apply degree_sub_lt _ (ne_zero hx), { rw [(monic hx).leading_coeff, pmonic.leading_coeff] }, { exact le_antisymm (min hx pmonic hp) (pmin (minpoly hx) (monic hx) (aeval hx)) }, end /--If an element x is a root of a polynomial p, then the minimal polynomial of x divides p.-/ lemma dvd {p : polynomial α} (hp : polynomial.aeval x p = 0) : minpoly hx ∣ p := begin rw ← dvd_iff_mod_by_monic_eq_zero (monic hx), by_contra hnz, have := degree_le_of_ne_zero hx hnz _, { contrapose! this, exact degree_mod_by_monic_lt _ (monic hx) (ne_zero hx) }, { rw ← mod_by_monic_add_div p (monic hx) at hp, simpa using hp } end lemma dvd_map_of_is_scalar_tower {α γ : Type*} (β : Type*) [comm_ring α] [field β] [comm_ring γ] [algebra α β] [algebra α γ] [algebra β γ] [is_scalar_tower α β γ] {x : γ} (hx : is_integral α x) : minpoly (is_integral_of_is_scalar_tower x hx) ∣ (minpoly hx).map (algebra_map α β) := by { apply minpoly.dvd, rw [← is_scalar_tower.aeval_apply, minpoly.aeval] } variables [nontrivial β] theorem unique' {p : polynomial α} (hp1 : _root_.irreducible p) (hp2 : polynomial.aeval x p = 0) (hp3 : p.monic) : p = minpoly hx := let ⟨q, hq⟩ := dvd hx hp2 in eq_of_monic_of_associated hp3 (monic hx) $ mul_one (minpoly hx) ▸ hq.symm ▸ associated_mul_mul (associated.refl _) $ associated_one_iff_is_unit.2 $ (hp1.is_unit_or_is_unit hq).resolve_left $ not_is_unit hx section gcd_domain /-- For GCD domains, the minimal polynomial over the ring is the same as the minimal polynomial over the fraction field. -/ lemma gcd_domain_eq_field_fractions {α : Type u} {β : Type v} {γ : Type w} [integral_domain α] [gcd_monoid α] [field β] [integral_domain γ] (f : fraction_map α β) [algebra f.codomain γ] [algebra α γ] [is_scalar_tower α f.codomain γ] {x : γ} (hx : is_integral α x) : minpoly (@is_integral_of_is_scalar_tower α f.codomain γ _ _ _ _ _ _ _ x hx) = ((minpoly hx).map (localization_map.to_ring_hom f)) := begin refine (unique' (@is_integral_of_is_scalar_tower α f.codomain γ _ _ _ _ _ _ _ x hx) _ _ _).symm, { exact (polynomial.is_primitive.irreducible_iff_irreducible_map_fraction_map f (polynomial.monic.is_primitive (monic hx))).1 (irreducible hx) }, { have htower := is_scalar_tower.aeval_apply α f.codomain γ x (minpoly hx), simp only [localization_map.algebra_map_eq, aeval] at htower, exact htower.symm }, { exact monic_map _ (monic hx) } end /-- The minimal polynomial over `ℤ` is the same as the minimal polynomial over `ℚ`. -/ --TODO use `gcd_domain_eq_field_fractions` directly when localizations are defined -- in terms of algebras instead of `ring_hom`s lemma over_int_eq_over_rat {α : Type u} [integral_domain α] {x : α} [algebra ℚ α] (hx : is_integral ℤ x) : minpoly (@is_integral_of_is_scalar_tower ℤ ℚ α _ _ _ _ _ _ _ x hx) = map (int.cast_ring_hom ℚ) (minpoly hx) := begin refine (unique' (@is_integral_of_is_scalar_tower ℤ ℚ α _ _ _ _ _ _ _ x hx) _ _ _).symm, { exact (is_primitive.int.irreducible_iff_irreducible_map_cast (polynomial.monic.is_primitive (monic hx))).1 (irreducible hx) }, { have htower := is_scalar_tower.aeval_apply ℤ ℚ α x (minpoly hx), simp only [localization_map.algebra_map_eq, aeval] at htower, exact htower.symm }, { exact monic_map _ (monic hx) } end /-- For GCD domains, the minimal polynomial divides any primitive polynomial that has the integral element as root. -/ lemma gcd_domain_dvd {α : Type u} {β : Type v} {γ : Type w} [integral_domain α] [gcd_monoid α] [field β] [integral_domain γ] (f : fraction_map α β) [algebra f.codomain γ] [algebra α γ] [is_scalar_tower α f.codomain γ] {x : γ} (hx : is_integral α x) {P : polynomial α} (hprim : is_primitive P) (hroot : polynomial.aeval x P = 0) : minpoly hx ∣ P := begin apply (is_primitive.dvd_iff_fraction_map_dvd_fraction_map f (monic.is_primitive (monic hx)) hprim ).2, rw [← gcd_domain_eq_field_fractions f hx], refine dvd (is_integral_of_is_scalar_tower x hx) _, rwa [← localization_map.algebra_map_eq, ← is_scalar_tower.aeval_apply] end /-- The minimal polynomial over `ℤ` divides any primitive polynomial that has the integral element as root. -/ -- TODO use `gcd_domain_dvd` directly when localizations are defined in terms of algebras -- instead of `ring_hom`s lemma integer_dvd {α : Type u} [integral_domain α] [algebra ℚ α] {x : α} (hx : is_integral ℤ x) {P : polynomial ℤ} (hprim : is_primitive P) (hroot : polynomial.aeval x P = 0) : minpoly hx ∣ P := begin apply (is_primitive.int.dvd_iff_map_cast_dvd_map_cast _ _ (monic.is_primitive (monic hx)) hprim ).2, rw [← over_int_eq_over_rat hx], refine dvd (is_integral_of_is_scalar_tower x hx) _, rwa [(int.cast_ring_hom ℚ).ext_int (algebra_map ℤ ℚ), ← is_scalar_tower.aeval_apply] end end gcd_domain variable (β) /--If L/K is a field extension, and x is an element of L in the image of K, then the minimal polynomial of x is X - C x.-/ lemma eq_X_sub_C (a : α) : minpoly (@is_integral_algebra_map α β _ _ _ a) = X - C a := eq.symm $ unique' (@is_integral_algebra_map α β _ _ _ a) (irreducible_X_sub_C a) (by rw [alg_hom.map_sub, aeval_X, aeval_C, sub_self]) (monic_X_sub_C a) variable {β} /--The minimal polynomial of 0 is X.-/ @[simp] lemma zero {h₀ : is_integral α (0:β)} : minpoly h₀ = X := by simpa only [add_zero, C_0, sub_eq_add_neg, neg_zero, ring_hom.map_zero] using eq_X_sub_C β (0:α) /--The minimal polynomial of 1 is X - 1.-/ @[simp] lemma one {h₁ : is_integral α (1:β)} : minpoly h₁ = X - 1 := by simpa only [ring_hom.map_one, C_1, sub_eq_add_neg] using eq_X_sub_C β (1:α) end ring section domain variables [domain β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is prime.-/ lemma prime : prime (minpoly hx) := begin refine ⟨ne_zero hx, not_is_unit hx, _⟩, rintros p q ⟨d, h⟩, have : polynomial.aeval x (p*q) = 0 := by simp [h, aeval hx], replace : polynomial.aeval x p = 0 ∨ polynomial.aeval x q = 0 := by simpa, exact or.imp (dvd hx) (dvd hx) this end /--If L/K is a field extension and an element y of K is a root of the minimal polynomial of an element x ∈ L, then y maps to x under the field embedding.-/ lemma root {x : β} (hx : is_integral α x) {y : α} (h : is_root (minpoly hx) y) : algebra_map α β y = x := have key : minpoly hx = X - C y := eq_of_monic_of_associated (monic hx) (monic_X_sub_C y) (associated_of_dvd_dvd (dvd_symm_of_irreducible (irreducible_X_sub_C y) (irreducible hx) (dvd_iff_is_root.2 h)) (dvd_iff_is_root.2 h)), by { have := aeval hx, rwa [key, alg_hom.map_sub, aeval_X, aeval_C, sub_eq_zero, eq_comm] at this } /--The constant coefficient of the minimal polynomial of x is 0 if and only if x = 0.-/ @[simp] lemma coeff_zero_eq_zero : coeff (minpoly hx) 0 = 0 ↔ x = 0 := begin split, { intro h, have zero_root := zero_is_root_of_coeff_zero_eq_zero h, rw ← root hx zero_root, exact ring_hom.map_zero _ }, { rintro rfl, simp } end /--The minimal polynomial of a nonzero element has nonzero constant coefficient.-/ lemma coeff_zero_ne_zero (h : x ≠ 0) : coeff (minpoly hx) 0 ≠ 0 := by { contrapose! h, simpa using h } end domain end field end minpoly
fa34e57868755104b2c83a02a1f66a8897900233
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/perm/cycle/concrete.lean
d52b8721d9ebcffd7926b2c7b6a9f7dd0df7f8f1
[ "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
19,813
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.list.cycle import group_theory.perm.cycle.type import group_theory.perm.list /-! # Properties of cyclic permutations constructed from lists/cycles In the following, `{α : Type*} [fintype α] [decidable_eq α]`. ## Main definitions * `cycle.form_perm`: the cyclic permutation created by looping over a `cycle α` * `equiv.perm.to_list`: the list formed by iterating application of a permutation * `equiv.perm.to_cycle`: the cycle formed by iterating application of a permutation * `equiv.perm.iso_cycle`: the equivalence between cyclic permutations `f : perm α` and the terms of `cycle α` that correspond to them * `equiv.perm.iso_cycle'`: the same equivalence as `equiv.perm.iso_cycle` but with evaluation via choosing over fintypes * The notation `c[1, 2, 3]` to emulate notation of cyclic permutations `(1 2 3)` * A `has_repr` instance for any `perm α`, by representing the `finset` of `cycle α` that correspond to the cycle factors. ## Main results * `list.is_cycle_form_perm`: a nontrivial list without duplicates, when interpreted as a permutation, is cyclic * `equiv.perm.is_cycle.exists_unique_cycle`: there is only one nontrivial `cycle α` corresponding to each cyclic `f : perm α` ## Implementation details The forward direction of `equiv.perm.iso_cycle'` uses `fintype.choose` of the uniqueness result, relying on the `fintype` instance of a `cycle.nodup` subtype. It is unclear if this works faster than the `equiv.perm.to_cycle`, which relies on recursion over `finset.univ`. Running `#eval` on even a simple noncyclic permutation `c[(1 : fin 7), 2, 3] * c[0, 5]` to show it takes a long time. TODO: is this because computing the cycle factors is slow? -/ open equiv equiv.perm list variables {α : Type*} namespace list variables [decidable_eq α] {l l' : list α} lemma form_perm_disjoint_iff (hl : nodup l) (hl' : nodup l') (hn : 2 ≤ l.length) (hn' : 2 ≤ l'.length) : perm.disjoint (form_perm l) (form_perm l') ↔ l.disjoint l' := begin rw [disjoint_iff_eq_or_eq, list.disjoint], split, { rintro h x hx hx', specialize h x, rw [form_perm_apply_mem_eq_self_iff _ hl _ hx, form_perm_apply_mem_eq_self_iff _ hl' _ hx'] at h, rcases h with hl | hl'; linarith }, { intros h x, by_cases hx : x ∈ l, by_cases hx' : x ∈ l', { exact (h hx hx').elim }, all_goals { have := form_perm_eq_self_of_not_mem _ _ ‹_›, tauto } } end lemma is_cycle_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : is_cycle (form_perm l) := begin cases l with x l, { norm_num at hn }, induction l with y l IH generalizing x, { norm_num at hn }, { use x, split, { rwa form_perm_apply_mem_ne_self_iff _ hl _ (mem_cons_self _ _) }, { intros w hw, have : w ∈ (x :: y :: l) := mem_of_form_perm_ne_self _ _ hw, obtain ⟨k, hk, rfl⟩ := nth_le_of_mem this, use k, simp only [zpow_coe_nat, form_perm_pow_apply_head _ _ hl k, nat.mod_eq_of_lt hk] } } end lemma pairwise_same_cycle_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : pairwise (l.form_perm.same_cycle) l := pairwise.imp_mem.mpr (pairwise_of_forall (λ x y hx hy, (is_cycle_form_perm hl hn).same_cycle ((form_perm_apply_mem_ne_self_iff _ hl _ hx).mpr hn) ((form_perm_apply_mem_ne_self_iff _ hl _ hy).mpr hn))) lemma cycle_of_form_perm (hl : nodup l) (hn : 2 ≤ l.length) (x) : cycle_of l.attach.form_perm x = l.attach.form_perm := have hn : 2 ≤ l.attach.length := by rwa ← length_attach at hn, have hl : l.attach.nodup := by rwa ← nodup_attach at hl, (is_cycle_form_perm hl hn).cycle_of_eq ((form_perm_apply_mem_ne_self_iff _ hl _ (mem_attach _ _)).mpr hn) lemma cycle_type_form_perm (hl : nodup l) (hn : 2 ≤ l.length) : cycle_type l.attach.form_perm = {l.length} := begin rw ←length_attach at hn, rw ←nodup_attach at hl, rw cycle_type_eq [l.attach.form_perm], { simp only [map, function.comp_app], rw [support_form_perm_of_nodup _ hl, card_to_finset, dedup_eq_self.mpr hl], { simp }, { intros x h, simpa [h, nat.succ_le_succ_iff] using hn } }, { simp }, { simpa using is_cycle_form_perm hl hn }, { simp } end lemma form_perm_apply_mem_eq_next (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x = next l x hx := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [next_nth_le _ hl, form_perm_apply_nth_le _ hl] end end list namespace cycle variables [decidable_eq α] (s s' : cycle α) /-- A cycle `s : cycle α` , given `nodup s` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. -/ def form_perm : Π (s : cycle α) (h : nodup s), equiv.perm α := λ s, quot.hrec_on s (λ l h, form_perm l) (λ l₁ l₂ (h : l₁ ~r l₂), begin ext, { exact h.nodup_iff }, { intros h₁ h₂ _, exact heq_of_eq (form_perm_eq_of_is_rotated h₁ h) } end) @[simp] lemma form_perm_coe (l : list α) (hl : l.nodup) : form_perm (l : cycle α) hl = l.form_perm := rfl lemma form_perm_subsingleton (s : cycle α) (h : subsingleton s) : form_perm s h.nodup = 1 := begin induction s using quot.induction_on, simp only [form_perm_coe, mk_eq_coe], simp only [length_subsingleton_iff, length_coe, mk_eq_coe] at h, cases s with hd tl, { simp }, { simp only [length_eq_zero, add_le_iff_nonpos_left, list.length, nonpos_iff_eq_zero] at h, simp [h] } end lemma is_cycle_form_perm (s : cycle α) (h : nodup s) (hn : nontrivial s) : is_cycle (form_perm s h) := begin induction s using quot.induction_on, exact list.is_cycle_form_perm h (length_nontrivial hn) end lemma support_form_perm [fintype α] (s : cycle α) (h : nodup s) (hn : nontrivial s) : support (form_perm s h) = s.to_finset := begin induction s using quot.induction_on, refine support_form_perm_of_nodup s h _, rintro _ rfl, simpa [nat.succ_le_succ_iff] using length_nontrivial hn end lemma form_perm_eq_self_of_not_mem (s : cycle α) (h : nodup s) (x : α) (hx : x ∉ s) : form_perm s h x = x := begin induction s using quot.induction_on, simpa using list.form_perm_eq_self_of_not_mem _ _ hx end lemma form_perm_apply_mem_eq_next (s : cycle α) (h : nodup s) (x : α) (hx : x ∈ s) : form_perm s h x = next s h x hx := begin induction s using quot.induction_on, simpa using list.form_perm_apply_mem_eq_next h _ _ end lemma form_perm_reverse (s : cycle α) (h : nodup s) : form_perm s.reverse (nodup_reverse_iff.mpr h) = (form_perm s h)⁻¹ := begin induction s using quot.induction_on, simpa using form_perm_reverse _ h end lemma form_perm_eq_form_perm_iff {α : Type*} [decidable_eq α] {s s' : cycle α} {hs : s.nodup} {hs' : s'.nodup} : s.form_perm hs = s'.form_perm hs' ↔ s = s' ∨ s.subsingleton ∧ s'.subsingleton := begin rw [cycle.length_subsingleton_iff, cycle.length_subsingleton_iff], revert s s', intros s s', apply quotient.induction_on₂' s s', intros l l', simpa using form_perm_eq_form_perm_iff end end cycle namespace equiv.perm section fintype variables [fintype α] [decidable_eq α] (p : equiv.perm α) (x : α) /-- `equiv.perm.to_list (f : perm α) (x : α)` generates the list `[x, f x, f (f x), ...]` until looping. That means when `f x = x`, `to_list f x = []`. -/ def to_list : list α := (list.range (cycle_of p x).support.card).map (λ k, (p ^ k) x) @[simp] lemma to_list_one : to_list (1 : perm α) x = [] := by simp [to_list, cycle_of_one] @[simp] lemma to_list_eq_nil_iff {p : perm α} {x} : to_list p x = [] ↔ x ∉ p.support := by simp [to_list] @[simp] lemma length_to_list : length (to_list p x) = (cycle_of p x).support.card := by simp [to_list] lemma to_list_ne_singleton (y : α) : to_list p x ≠ [y] := begin intro H, simpa [card_support_ne_one] using congr_arg length H end lemma two_le_length_to_list_iff_mem_support {p : perm α} {x : α} : 2 ≤ length (to_list p x) ↔ x ∈ p.support := by simp lemma length_to_list_pos_of_mem_support (h : x ∈ p.support) : 0 < length (to_list p x) := zero_lt_two.trans_le (two_le_length_to_list_iff_mem_support.mpr h) lemma nth_le_to_list (n : ℕ) (hn : n < length (to_list p x)) : nth_le (to_list p x) n hn = (p ^ n) x := by simp [to_list] lemma to_list_nth_le_zero (h : x ∈ p.support) : (to_list p x).nth_le 0 (length_to_list_pos_of_mem_support _ _ h) = x := by simp [to_list] variables {p} {x} lemma mem_to_list_iff {y : α} : y ∈ to_list p x ↔ same_cycle p x y ∧ x ∈ p.support := begin simp only [to_list, mem_range, mem_map], split, { rintro ⟨n, hx, rfl⟩, refine ⟨⟨n, rfl⟩, _⟩, contrapose! hx, rw ←support_cycle_of_eq_nil_iff at hx, simp [hx] }, { rintro ⟨h, hx⟩, simpa using h.exists_pow_eq_of_mem_support hx } end lemma nodup_to_list (p : perm α) (x : α) : nodup (to_list p x) := begin by_cases hx : p x = x, { rw [←not_mem_support, ←to_list_eq_nil_iff] at hx, simp [hx] }, have hc : is_cycle (cycle_of p x) := is_cycle_cycle_of p hx, rw nodup_iff_nth_le_inj, rintros n m hn hm, rw [length_to_list, ←hc.order_of] at hm hn, rw [←cycle_of_apply_self, ←ne.def, ←mem_support] at hx, rw [nth_le_to_list, nth_le_to_list, ←cycle_of_pow_apply_self p x n, ←cycle_of_pow_apply_self p x m], cases n; cases m, { simp }, { rw [←hc.support_pow_of_pos_of_lt_order_of m.zero_lt_succ hm, mem_support, cycle_of_pow_apply_self] at hx, simp [hx.symm] }, { rw [←hc.support_pow_of_pos_of_lt_order_of n.zero_lt_succ hn, mem_support, cycle_of_pow_apply_self] at hx, simp [hx] }, intro h, have hn' : ¬ order_of (p.cycle_of x) ∣ n.succ := nat.not_dvd_of_pos_of_lt n.zero_lt_succ hn, have hm' : ¬ order_of (p.cycle_of x) ∣ m.succ := nat.not_dvd_of_pos_of_lt m.zero_lt_succ hm, rw ←hc.support_pow_eq_iff at hn' hm', rw [←nat.mod_eq_of_lt hn, ←nat.mod_eq_of_lt hm, ←pow_inj_mod], refine support_congr _ _, { rw [hm', hn'], exact finset.subset.refl _ }, { rw hm', intros y hy, obtain ⟨k, rfl⟩ := hc.exists_pow_eq (mem_support.mp hx) (mem_support.mp hy), rw [←mul_apply, (commute.pow_pow_self _ _ _).eq, mul_apply, h, ←mul_apply, ←mul_apply, (commute.pow_pow_self _ _ _).eq] } end lemma next_to_list_eq_apply (p : perm α) (x y : α) (hy : y ∈ to_list p x) : next (to_list p x) y hy = p y := begin rw mem_to_list_iff at hy, obtain ⟨k, hk, hk'⟩ := hy.left.exists_pow_eq_of_mem_support hy.right, rw ←nth_le_to_list p x k (by simpa using hk) at hk', simp_rw ←hk', rw [next_nth_le _ (nodup_to_list _ _), nth_le_to_list, nth_le_to_list, ←mul_apply, ←pow_succ, length_to_list, pow_apply_eq_pow_mod_order_of_cycle_of_apply p (k + 1), is_cycle.order_of], exact is_cycle_cycle_of _ (mem_support.mp hy.right) end lemma to_list_pow_apply_eq_rotate (p : perm α) (x : α) (k : ℕ) : p.to_list ((p ^ k) x) = (p.to_list x).rotate k := begin apply ext_le, { simp only [length_to_list, cycle_of_self_apply_pow, length_rotate]}, { intros n hn hn', rw [nth_le_to_list, nth_le_rotate, nth_le_to_list, length_to_list, pow_mod_card_support_cycle_of_self_apply, pow_add, mul_apply] } end lemma same_cycle.to_list_is_rotated {f : perm α} {x y : α} (h : same_cycle f x y) : to_list f x ~r to_list f y := begin by_cases hx : x ∈ f.support, { obtain ⟨_ | k, hk, hy⟩ := h.exists_pow_eq_of_mem_support hx, { simp only [coe_one, id.def, pow_zero] at hy, simp [hy] }, use k.succ, rw [←to_list_pow_apply_eq_rotate, hy] }, { rw [to_list_eq_nil_iff.mpr hx, is_rotated_nil_iff', eq_comm, to_list_eq_nil_iff], rwa ←h.mem_support_iff } end lemma pow_apply_mem_to_list_iff_mem_support {n : ℕ} : (p ^ n) x ∈ p.to_list x ↔ x ∈ p.support := begin rw [mem_to_list_iff, and_iff_right_iff_imp], refine λ _, same_cycle.symm _, rw same_cycle_pow_left end lemma to_list_form_perm_nil (x : α) : to_list (form_perm ([] : list α)) x = [] := by simp lemma to_list_form_perm_singleton (x y : α) : to_list (form_perm [x]) y = [] := by simp lemma to_list_form_perm_nontrivial (l : list α) (hl : 2 ≤ l.length) (hn : nodup l) : to_list (form_perm l) (l.nth_le 0 (zero_lt_two.trans_le hl)) = l := begin have hc : l.form_perm.is_cycle := list.is_cycle_form_perm hn hl, have hs : l.form_perm.support = l.to_finset, { refine support_form_perm_of_nodup _ hn _, rintro _ rfl, simpa [nat.succ_le_succ_iff] using hl }, rw [to_list, hc.cycle_of_eq (mem_support.mp _), hs, card_to_finset, dedup_eq_self.mpr hn], { refine list.ext_le (by simp) (λ k hk hk', _), simp [form_perm_pow_apply_nth_le _ hn, nat.mod_eq_of_lt hk'] }, { simpa [hs] using nth_le_mem _ _ _ } end lemma to_list_form_perm_is_rotated_self (l : list α) (hl : 2 ≤ l.length) (hn : nodup l) (x : α) (hx : x ∈ l): to_list (form_perm l) x ~r l := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, have hr : l ~r l.rotate k := ⟨k, rfl⟩, rw form_perm_eq_of_is_rotated hn hr, rw ←nth_le_rotate' l k k, simp only [nat.mod_eq_of_lt hk, tsub_add_cancel_of_le hk.le, nat.mod_self], rw [to_list_form_perm_nontrivial], { simp }, { simpa using hl }, { simpa using hn } end lemma form_perm_to_list (f : perm α) (x : α) : form_perm (to_list f x) = f.cycle_of x := begin by_cases hx : f x = x, { rw [(cycle_of_eq_one_iff f).mpr hx, to_list_eq_nil_iff.mpr (not_mem_support.mpr hx), form_perm_nil] }, ext y, by_cases hy : same_cycle f x y, { obtain ⟨k, hk, rfl⟩ := hy.exists_pow_eq_of_mem_support (mem_support.mpr hx), rw [cycle_of_apply_apply_pow_self, list.form_perm_apply_mem_eq_next (nodup_to_list f x), next_to_list_eq_apply, pow_succ, mul_apply], rw mem_to_list_iff, exact ⟨⟨k, rfl⟩, mem_support.mpr hx⟩ }, { rw [cycle_of_apply_of_not_same_cycle hy, form_perm_apply_of_not_mem], simp [mem_to_list_iff, hy] } end /-- Given a cyclic `f : perm α`, generate the `cycle α` in the order of application of `f`. Implemented by finding an element `x : α` in the support of `f` in `finset.univ`, and iterating on using `equiv.perm.to_list f x`. -/ def to_cycle (f : perm α) (hf : is_cycle f) : cycle α := multiset.rec_on (finset.univ : finset α).val (quot.mk _ []) (λ x s l, if f x = x then l else to_list f x) (by { intros x y m s, refine heq_of_eq _, split_ifs with hx hy hy; try { refl }, { have hc : same_cycle f x y := is_cycle.same_cycle hf hx hy, exact quotient.sound' hc.to_list_is_rotated }}) lemma to_cycle_eq_to_list (f : perm α) (hf : is_cycle f) (x : α) (hx : f x ≠ x) : to_cycle f hf = to_list f x := begin have key : (finset.univ : finset α).val = x ::ₘ finset.univ.val.erase x, { simp }, rw [to_cycle, key], simp [hx] end lemma nodup_to_cycle (f : perm α) (hf : is_cycle f) : (to_cycle f hf).nodup := begin obtain ⟨x, hx, -⟩ := id hf, simpa [to_cycle_eq_to_list f hf x hx] using nodup_to_list _ _ end lemma nontrivial_to_cycle (f : perm α) (hf : is_cycle f) : (to_cycle f hf).nontrivial := begin obtain ⟨x, hx, -⟩ := id hf, simp [to_cycle_eq_to_list f hf x hx, hx, cycle.nontrivial_coe_nodup_iff (nodup_to_list _ _)] end /-- Any cyclic `f : perm α` is isomorphic to the nontrivial `cycle α` that corresponds to repeated application of `f`. The forward direction is implemented by `equiv.perm.to_cycle`. -/ def iso_cycle : {f : perm α // is_cycle f} ≃ {s : cycle α // s.nodup ∧ s.nontrivial} := { to_fun := λ f, ⟨to_cycle (f : perm α) f.prop, nodup_to_cycle f f.prop, nontrivial_to_cycle _ f.prop⟩, inv_fun := λ s, ⟨(s : cycle α).form_perm s.prop.left, (s : cycle α).is_cycle_form_perm _ s.prop.right⟩, left_inv := λ f, by { obtain ⟨x, hx, -⟩ := id f.prop, simpa [to_cycle_eq_to_list (f : perm α) f.prop x hx, form_perm_to_list, subtype.ext_iff] using f.prop.cycle_of_eq hx }, right_inv := λ s, by { rcases s with ⟨⟨s⟩, hn, ht⟩, obtain ⟨x, -, -, hx, -⟩ := id ht, have hl : 2 ≤ s.length := by simpa using cycle.length_nontrivial ht, simp only [cycle.mk_eq_coe, cycle.nodup_coe_iff, cycle.mem_coe_iff, subtype.coe_mk, cycle.form_perm_coe] at hn hx ⊢, rw to_cycle_eq_to_list _ _ x, { refine quotient.sound' _, exact to_list_form_perm_is_rotated_self _ hl hn _ hx }, { rw [←mem_support, support_form_perm_of_nodup _ hn], { simpa using hx }, { rintro _ rfl, simpa [nat.succ_le_succ_iff] using hl } } } } end fintype section finite variables [finite α] [decidable_eq α] lemma is_cycle.exists_unique_cycle {f : perm α} (hf : is_cycle f) : ∃! (s : cycle α), ∃ (h : s.nodup), s.form_perm h = f := begin casesI nonempty_fintype α, obtain ⟨x, hx, hy⟩ := id hf, refine ⟨f.to_list x, ⟨nodup_to_list f x, _⟩, _⟩, { simp [form_perm_to_list, hf.cycle_of_eq hx] }, { rintro ⟨l⟩ ⟨hn, rfl⟩, simp only [cycle.mk_eq_coe, cycle.coe_eq_coe, subtype.coe_mk, cycle.form_perm_coe], refine (to_list_form_perm_is_rotated_self _ _ hn _ _).symm, { contrapose! hx, suffices : form_perm l = 1, { simp [this] }, rw form_perm_eq_one_iff _ hn, exact nat.le_of_lt_succ hx }, { rw ←mem_to_finset, refine support_form_perm_le l _, simpa using hx } } end lemma is_cycle.exists_unique_cycle_subtype {f : perm α} (hf : is_cycle f) : ∃! (s : {s : cycle α // s.nodup}), (s : cycle α).form_perm s.prop = f := begin obtain ⟨s, ⟨hs, rfl⟩, hs'⟩ := hf.exists_unique_cycle, refine ⟨⟨s, hs⟩, rfl, _⟩, rintro ⟨t, ht⟩ ht', simpa using hs' _ ⟨ht, ht'⟩ end lemma is_cycle.exists_unique_cycle_nontrivial_subtype {f : perm α} (hf : is_cycle f) : ∃! (s : {s : cycle α // s.nodup ∧ s.nontrivial}), (s : cycle α).form_perm s.prop.left = f := begin obtain ⟨⟨s, hn⟩, hs, hs'⟩ := hf.exists_unique_cycle_subtype, refine ⟨⟨s, hn, _⟩, _, _⟩, { rw hn.nontrivial_iff, subst f, intro H, refine hf.ne_one _, simpa using cycle.form_perm_subsingleton _ H }, { simpa using hs }, { rintro ⟨t, ht, ht'⟩ ht'', simpa using hs' ⟨t, ht⟩ ht'' } end end finite variables [fintype α] [decidable_eq α] /-- Any cyclic `f : perm α` is isomorphic to the nontrivial `cycle α` that corresponds to repeated application of `f`. The forward direction is implemented by finding this `cycle α` using `fintype.choose`. -/ def iso_cycle' : {f : perm α // is_cycle f} ≃ {s : cycle α // s.nodup ∧ s.nontrivial} := { to_fun := λ f, fintype.choose _ f.prop.exists_unique_cycle_nontrivial_subtype, inv_fun := λ s, ⟨(s : cycle α).form_perm s.prop.left, (s : cycle α).is_cycle_form_perm _ s.prop.right⟩, left_inv := λ f, by simpa [subtype.ext_iff] using fintype.choose_spec _ f.prop.exists_unique_cycle_nontrivial_subtype, right_inv := λ ⟨s, hs, ht⟩, by { simp [subtype.coe_mk], convert fintype.choose_subtype_eq (λ (s' : cycle α), s'.nodup ∧ s'.nontrivial) _, ext ⟨s', hs', ht'⟩, simp [cycle.form_perm_eq_form_perm_iff, (iff_not_comm.mp hs.nontrivial_iff), (iff_not_comm.mp hs'.nontrivial_iff), ht] } } notation `c[` l:(foldr `, ` (h t, list.cons h t) list.nil `]`) := cycle.form_perm ↑l (cycle.nodup_coe_iff.mpr dec_trivial) meta instance repr_perm [has_repr α] : has_repr (perm α) := ⟨λ f, repr (multiset.pmap (λ (g : perm α) (hg : g.is_cycle), iso_cycle ⟨g, hg⟩) -- to_cycle is faster? (perm.cycle_factors_finset f).val (λ g hg, (mem_cycle_factors_finset_iff.mp (finset.mem_def.mpr hg)).left))⟩ end equiv.perm
04b708b1fb4b34392b46382130d9068b62fedf8e
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/meta2.lean
a26336f19272f2883c829b2479ff426e1474c2fa
[ "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
18,269
lean
import Lean.Meta open Lean open Lean.Meta -- set_option trace.Meta true --set_option trace.Meta.isDefEq.step false -- set_option trace.Meta.isDefEq.delta false set_option trace.Meta.debug true def print (msg : MessageData) : MetaM Unit := trace[Meta.debug] msg def checkM (x : MetaM Bool) : MetaM Unit := unless (← x) do throwError "check failed" def getAssignment (m : Expr) : MetaM Expr := do let v? ← getExprMVarAssignment? m.mvarId!; (match v? with | some v => pure v | none => throwError "metavariable is not assigned") def nat := mkConst `Nat def boolE := mkConst `Bool def succ := mkConst `Nat.succ def zero := mkConst `Nat.zero def add := mkConst `Nat.add def io := mkConst `IO def type := mkSort levelOne def boolFalse := mkConst `Bool.false def boolTrue := mkConst `Bool.true def tst1 : MetaM Unit := do print "----- tst1 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq mvar (mkNatLit 10); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst1 def tst2 : MetaM Unit := do print "----- tst2 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq (mkApp succ mvar) (mkApp succ (mkNatLit 10)); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst2 def tst3 : MetaM Unit := do print "----- tst3 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); lambdaTelescope t fun xs _ => do let x := xs[0]; checkM $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); pure (); let v ← getAssignment mvar; print v; pure () #eval tst3 def tst4 : MetaM Unit := do print "----- tst4 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; lambdaTelescope t fun xs _ => do let x := xs[0]; let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); -- the following `isExprDefEq` fails because `x` is also in the context of `mvar` checkM $ not <$> isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); checkM $ approxDefEq $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); let v ← getAssignment mvar; print v; pure (); pure () #eval tst4 def mkAppC (c : Name) (xs : Array Expr) : MetaM Expr := do let r ← mkAppM c xs; check r; pure r def mkProd (a b : Expr) : MetaM Expr := mkAppC `Prod #[a, b] def mkPair (a b : Expr) : MetaM Expr := mkAppC `Prod.mk #[a, b] def mkFst (s : Expr) : MetaM Expr := mkAppC `Prod.fst #[s] def mkSnd (s : Expr) : MetaM Expr := mkAppC `Prod.snd #[s] def tst5 : MetaM Unit := do print "----- tst5 -----"; let p₁ ← mkPair (mkNatLit 1) (mkNatLit 2); let x ← mkFst p₁; print x; let v ← whnf x; print v; let v ← withTransparency TransparencyMode.reducible $ whnf x; print v; let x ← mkId x; print x; let prod ← mkProd nat nat; let m ← mkFreshExprMVar prod; let y ← mkFst m; checkM $ isExprDefEq y x; print y; let x ← mkProjection p₁ `fst; print x; let y ← mkProjection p₁ `snd; print y #eval tst5 def tst6 : MetaM Unit := do print "----- tst6 -----"; withLocalDeclD `x nat $ fun x => do let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkAppN add #[x, mkNatLit 3]; checkM $ not <$> isExprDefEq t s; let s := mkAppN add #[x, mkNatLit 6]; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == mkAppN add #[x, mkNatLit 2]; print v; let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkNatLit 3; checkM $ not <$> isExprDefEq t s; let s := mkNatLit 10; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == mkNatLit 6; print v; pure () #eval tst6 def tst7 : MetaM Unit := do print "----- tst7 -----"; withLocalDeclD `x type $ fun x => do let m1 ← mkFreshExprMVar (← mkArrow type type); let m2 ← mkFreshExprMVar type; let t := mkApp io x; -- we need to use foApprox to solve `?m1 ?m2 =?= IO x` checkM $ not <$> isDefEq (mkApp m1 m2) t; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) t; let v ← getAssignment m1; checkM $ pure $ v == io; let v ← getAssignment m2; checkM $ pure $ v == x; pure () #eval tst7 def tst9 : MetaM Unit := do print "----- tst9 -----"; let env ← getEnv; print (toString (← isReducible `Prod.fst)) print (toString (← isReducible `Add.add)) pure () #eval tst9 def tst10 : MetaM Unit := do print "----- tst10 -----"; let t ← withLocalDeclD `x nat $ fun x => do { let b := mkAppN add #[x, mkAppN add #[mkNatLit 2, mkNatLit 3]]; mkLambdaFVars #[x] b }; print t; let t ← reduce t; print t; pure () #eval tst10 def tst11 : MetaM Unit := do print "----- tst11 -----"; checkM $ isType nat; checkM $ isType (← mkArrow nat nat); checkM $ not <$> isType add; checkM $ not <$> isType (mkNatLit 1); withLocalDeclD `x nat fun x => do checkM $ not <$> isType x; checkM $ not <$> (mkLambdaFVars #[x] x >>= isType); checkM $ not <$> (mkLambdaFVars #[x] nat >>= isType); let t ← mkEq x (mkNatLit 0); let t ← mkForallFVars #[x] t (usedOnly := true); print t; checkM $ isType t; pure (); pure () #eval tst11 def tst12 : MetaM Unit := do print "----- tst12 -----"; withLocalDeclD `x nat $ fun x => do let t ← mkEqRefl x >>= mkLambdaFVars #[x]; print t; let type ← inferType t; print type; isProofQuick t >>= fun b => print (toString b); isProofQuick nat >>= fun b => print (toString b); isProofQuick type >>= fun b => print (toString b); pure (); pure () #eval tst12 def tst13 : MetaM Unit := do print "----- tst13 -----"; let m₁ ← mkFreshExprMVar (← mkArrow type type); let m₂ ← mkFreshExprMVar (mkApp m₁ nat); let t ← mkId m₂; print t; let r ← abstractMVars t; print r.expr; let (_, _, e) ← openAbstractMVarsResult r; print e; pure () def mkDecEq (type : Expr) : MetaM Expr := mkAppC `DecidableEq #[type] def mkStateM (σ : Expr) : MetaM Expr := mkAppC `StateM #[σ] def mkMonad (m : Expr) : MetaM Expr := mkAppC `Monad #[m] def mkMonadState (σ m : Expr) : MetaM Expr := mkAppC `MonadState #[σ, m] def mkAdd (a : Expr) : MetaM Expr := mkAppC `Add #[a] def mkToString (a : Expr) : MetaM Expr := mkAppC `ToString #[a] def tst14 : MetaM Unit := do print "----- tst14 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; let globalInsts ← getGlobalInstancesIndex; let insts ← globalInsts.getUnify monad; print (insts.map (·.val)); pure () #eval tst14 def tst15 : MetaM Unit := do print "----- tst15 -----"; let inst ← _root_.mkAdd nat; let r ← synthInstance inst; print r; pure () #eval tst15 def tst16 : MetaM Unit := do print "----- tst16 -----"; let prod ← mkProd nat nat; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst16 def tst17 : MetaM Unit := do print "----- tst17 -----"; let prod ← mkProd nat nat; let prod ← mkProd boolE prod; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst17 def tst18 : MetaM Unit := do print "----- tst18 -----"; let decEqNat ← mkDecEq nat; let r ← synthInstance decEqNat; print r; pure () #eval tst18 def tst19 : MetaM Unit := do print "----- tst19 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; print monad; let r ← synthInstance monad; print r; pure () #eval tst19 def tst20 : MetaM Unit := do print "----- tst20 -----"; let stateM ← mkStateM nat; print stateM; let monadState ← mkMonadState nat stateM; print monadState; let r ← synthInstance monadState; print r; pure () #eval tst20 def tst21 : MetaM Unit := do print "----- tst21 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do withLocalDeclD `z nat $ fun z => do let eq₁ ← mkEq x y; let eq₂ ← mkEq y z; withLocalDeclD `h₁ eq₁ $ fun h₁ => do withLocalDeclD `h₂ eq₂ $ fun h₂ => do let h ← mkEqTrans h₁ h₂; let h ← mkEqSymm h; let h ← mkCongrArg succ h; let h₂ ← mkEqRefl succ; let h ← mkCongr h₂ h; let t ← inferType h; check h; print h; print t; let h ← mkCongrFun h₂ x; let t ← inferType h; check h; print t; pure () #eval tst21 def tst22 : MetaM Unit := do print "----- tst22 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do let t ← mkAppC `Add.add #[x, y]; print t; let t ← mkAppC `Add.add #[y, x]; print t; let t ← mkAppC `ToString.toString #[x]; print t; pure () #eval tst22 def test1 : Nat := (fun x y => x + y) 0 1 def tst23 : MetaM Unit := do print "----- tst23 -----"; let cinfo ← getConstInfo `test1; let v := cinfo.value?.get!; print v; print v.headBeta #eval tst23 def tst26 : MetaM Unit := do print "----- tst26 -----"; let m1 ← mkFreshExprMVar (← mkArrow nat nat); let m2 ← mkFreshExprMVar nat; let m3 ← mkFreshExprMVar nat; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) m3; checkM $ do { let b ← isExprMVarAssigned $ m1.mvarId!; pure (!b) }; checkM $ isExprMVarAssigned $ m3.mvarId!; pure () #eval tst26 section set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true def tst27 : MetaM Unit := do print "----- tst27 -----"; let m ← mkFreshExprMVar nat; checkM $ isDefEq (mkNatLit 1) (mkApp (mkConst `Nat.succ) m); pure () #eval tst27 end def tst28 : MetaM Unit := do print "----- tst28 -----"; withLocalDeclD `x nat $ fun x => withLocalDeclD `y nat $ fun y => withLocalDeclD `z nat $ fun z => do let t1 ← mkAppM `Add.add #[x, y]; let t1 ← mkAppM `Add.add #[x, t1]; let t1 ← mkAppM `Add.add #[t1, t1]; let t2 ← mkAppM `Add.add #[z, y]; let t3 ← mkAppM `Eq #[t2, t1]; let t3 ← mkForallFVars #[z] t3; let m ← mkFreshExprMVar nat; let p ← mkAppM `Add.add #[x, m]; print t3; let r ← kabstract t3 p; print r; let p ← mkAppM `Add.add #[x, y]; let r ← kabstract t3 p; print r; pure () #eval tst28 def norm : Level → Level := @Lean.Level.normalize def tst29 : MetaM Unit := do print "----- tst29 -----"; let u := mkLevelParam `u; let v := mkLevelParam `v; let u1 := mkLevelSucc u; let m := mkLevelMax levelOne u1; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax u1 levelOne; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax (mkLevelMax levelOne (mkLevelSucc u1)) (mkLevelSucc levelOne); checkM $ pure $ norm m == mkLevelSucc u1; print m; print (norm m); let m := mkLevelMax (mkLevelMax (mkLevelSucc (mkLevelSucc u1)) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); checkM $ pure $ norm m == mkLevelSucc (mkLevelSucc u1); let m := mkLevelMax (mkLevelMax (mkLevelSucc v) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); pure () #eval tst29 def tst30 : MetaM Unit := do print "----- tst30 -----"; let m1 ← mkFreshExprMVar nat; let m2 ← mkFreshExprMVar (← mkArrow nat nat); withLocalDeclD `x nat $ fun x => do let t := mkApp succ $ mkApp m2 x; print t; checkM $ approxDefEq $ isDefEq m1 t; let r ← instantiateMVars m1; print r; let r ← instantiateMVars m2; print r; pure () #eval tst30 def tst31 : MetaM Unit := do print "----- tst31 -----"; let m ← mkFreshExprMVar nat; let t := mkLet `x nat zero m; print t; checkM $ isDefEq t m; pure () def tst32 : MetaM Unit := do print "----- tst32 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; print t; let motive := mkLambda `x BinderInfo.default nat (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 0)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqNDRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst32 def tst33 : MetaM Unit := do print "----- tst33 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; let motive := mkLambda `x BinderInfo.default nat $ mkLambda `h BinderInfo.default (mkApp3 (mkConst `Eq [levelOne]) nat a (mkBVar 0)) $ (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 1)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst33 def tst34 : MetaM Unit := do print "----- tst34 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m ← mkFreshExprMVar type; let t ← mkLambdaFVars #[α] (← mkArrow m m); print t; pure () #eval tst34 def tst35 : MetaM Unit := do print "----- tst35 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m1 ← mkFreshExprMVar type; let m2 ← mkFreshExprMVar (← mkArrow nat type); let v := mkLambda `x BinderInfo.default nat m1; assignExprMVar m2.mvarId! v; let w := mkApp m2 zero; let t1 ← mkLambdaFVars #[α] (← mkArrow w w); print t1; let m3 ← mkFreshExprMVar type; let t2 ← mkLambdaFVars #[α] (← mkArrow (mkBVar 0) (mkBVar 1)); print t2; checkM $ isDefEq t1 t2; pure () #eval tst35 #check @Id def tst36 : MetaM Unit := do print "----- tst36 -----"; let type := mkSort levelOne; let m1 ← mkFreshExprMVar (← mkArrow type type); withLocalDeclD `α type $ fun α => do let m2 ← mkFreshExprMVar type; let t ← mkAppM `Id #[m2]; checkM $ approxDefEq $ isDefEq (mkApp m1 α) t; checkM $ approxDefEq $ isDefEq m1 (mkConst `Id [levelZero]); pure () #eval tst36 def tst37 : MetaM Unit := do print "----- tst37 -----"; let m1 ← mkFreshExprMVar (←mkArrow nat (←mkArrow type type)); let m2 ← mkFreshExprMVar (←mkArrow nat type); withLocalDeclD `v nat $ fun v => do let lhs := mkApp2 m1 v (mkApp m2 v); let rhs ← mkAppM `StateM #[nat, nat]; print lhs; print rhs; checkM $ approxDefEq $ isDefEq lhs rhs; pure () #eval tst37 def tst38 : MetaM Unit := do print "----- tst38 -----"; let m1 ← mkFreshExprMVar nat; withLocalDeclD `x nat $ fun x => do let m2 ← mkFreshExprMVar type; withLocalDeclD `y m2 $ fun y => do let m3 ← mkFreshExprMVar (←mkArrow m2 nat); let rhs := mkApp m3 y; checkM $ approxDefEq $ isDefEq m2 nat; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; checkM $ approxDefEq $ isDefEq m1 rhs; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; pure () set_option pp.all true set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true #eval tst38 def tst39 : MetaM Unit := do print "----- tst39 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => do let p ← mkProd α β; let t ← mkForallFVars #[α, β] p; print t; let e ← instantiateForall t #[nat, boolE]; print e; pure () #eval tst39 def tst40 : MetaM Unit := do print "----- tst40 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => withLocalDeclD `a α $ fun a => withLocalDeclD `b β $ fun b => do let p ← mkProd α β; let t1 ← mkForallFVars #[α, β] p; let t2 ← mkForallFVars #[α, β, a, b] p; print t1; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 true; print $ toString $ t2.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ t1.inferImplicit 2 false; checkM $ pure $ ((t1.inferImplicit 2 false).bindingInfo! == BinderInfo.default); checkM $ pure $ ((t1.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.default); print $ t1.inferImplicit 2 true; checkM $ pure $ ((t1.inferImplicit 2 true).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t1.inferImplicit 2 true).bindingBody!.bindingInfo! == BinderInfo.implicit); print t2; print $ t2.inferImplicit 2 false; checkM $ pure $ ((t2.inferImplicit 2 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.implicit); print $ t2.inferImplicit 1 false; checkM $ pure $ ((t2.inferImplicit 1 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 1 false).bindingBody!.bindingInfo! == BinderInfo.default); pure () #eval tst40 universes u structure A (α : Type u) := (x y : α) structure B (α : Type u) := (z : α) structure C (α : Type u) extends A α, B α := (w : Bool) def mkA (x y : Expr) : MetaM Expr := mkAppC `A.mk #[x, y] def mkB (z : Expr) : MetaM Expr := mkAppC `B.mk #[z] def mkC (x y z w : Expr) : MetaM Expr := do let a ← mkA x y; let b ← mkB z; mkAppC `C.mk #[a, b, w] def tst41 : MetaM Unit := do print "----- tst41 -----"; let c ← mkC (mkNatLit 1) (mkNatLit 2) (mkNatLit 3) boolTrue; print c; let x ← mkProjection c `x; check x; print x; let y ← mkProjection c `y; check y; print y; let z ← mkProjection c `z; check z; print z; let w ← mkProjection c `w; check w; print w; pure () set_option trace.Meta.isDefEq.step false set_option trace.Meta.isDefEq.delta false set_option trace.Meta.isDefEq.assign false #eval tst41 set_option pp.all false def tst42 : MetaM Unit := do print "----- tst42 -----"; let t ← mkListLit nat [mkNatLit 1, mkNatLit 2]; print t; check t; let t ← mkArrayLit nat [mkNatLit 1, mkNatLit 2]; print t; check t; (match t.arrayLit? with | some (_, xs) => do checkM $ pure $ xs.length == 2; (match (xs.get! 0).natLit?, (xs.get! 1).natLit? with | some 1, some 2 => pure () | _, _ => throwError "nat lits expected") | none => throwError "array lit expected") #eval tst42
d1334091994fc03f9ac5d075d32ab75e2fff0e8d
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/276.lean
18cc0cdc799ac9a99062fc1bca5cb58ca9b1186a
[ "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
460
lean
universes u v -- `Type u` version can be defined without this option, but I get the same error set_option bootstrap.inductiveCheckResultingUniverse false in inductive PEmpty : Sort u -- `#check` works set_option pp.all true in #check fun {α : Sort v} => PEmpty.rec (fun _ => α) -- but `def` doesn't work -- error: (kernel) compiler failed to infer low level type, unknown declaration 'PEmpty.rec' def PEmpty.elim {α : Sort v} := PEmpty.rec (fun _ => α)
e0a41253b407d7effec586db3a74ef5bb4ee5439
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Meta/Tactic/Constructor.lean
373038c167a2e49b1b2f5db374adefc4d3cafb27
[ "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,709
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Check import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Apply namespace Lean.Meta def constructor (mvarId : MVarId) : MetaM (List MVarId) := do withMVarContext mvarId do checkNotAssigned mvarId `constructor let target ← getMVarType' mvarId matchConstInduct target.getAppFn (fun _ => throwTacticEx `constructor mvarId "target is not an inductive datatype") fun ival us => do for ctor in ival.ctors do try return ← apply mvarId (Lean.mkConst ctor us) catch _ => pure () throwTacticEx `constructor mvarId "no applicable constructor found" def existsIntro (mvarId : MVarId) (w : Expr) : MetaM MVarId := do withMVarContext mvarId do checkNotAssigned mvarId `exists let target ← getMVarType' mvarId matchConstStruct target.getAppFn (fun _ => throwTacticEx `exists mvarId "target is not an inductive datatype with one constructor") fun ival us cval => do if cval.numFields < 2 then throwTacticEx `exists mvarId "constructor must have at least two fields" let ctor := mkAppN (Lean.mkConst cval.name us) target.getAppArgs[:cval.numParams] let ctorType ← inferType ctor let (mvars, _, _) ← forallMetaTelescopeReducing ctorType (some (cval.numFields-2)) let f := mkAppN ctor mvars checkApp f w let [mvarId] ← apply mvarId <| mkApp f w | throwTacticEx `exists mvarId "unexpected number of subgoals" pure mvarId end Lean.Meta
867341eb96a6a94472883b18262d7468b7225884
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/data/list/default.lean
888692691ecdae83fef0b4f6ef4df1dc1861a2ed
[ "Apache-2.0" ]
permissive
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
659
lean
import .nth_le import .with_mem namespace list /-- Take conjunction of all propositions in list. -/ protected def forall_prop : list Prop → Prop | [] := true | (h::r) := h ∧ forall_prop r section is_empty /-- Return true if list is empty -/ def is_empty {α: Type _} : list α → Prop | [] := true | (_::_) := false /-- Decide whether list is empty -/ instance is_empty.decidable (α: Type _) : decidable_pred (@is_empty α) | [] := decidable.is_true trivial | (_::_) := decidable.is_false id end is_empty theorem map_eq_nil {α} {β} (f : α → β) (l:list α) : (list.map f l = nil) ↔ (l = nil) := begin cases l; simp [map], end end list
1f7fda976b30764bcda66ff338c7fa69f2731745
bdb33f8b7ea65f7705fc342a178508e2722eb851
/data/list/basic.lean
7b56d414d41d0291a66f0e927e00f3d72befe9db
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
130,156
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro Basic properties of lists. -/ import tactic.interactive algebra.group logic.basic logic.function data.nat.basic data.option data.bool data.prod data.sigma open function nat namespace list universes u v w variables {α : Type u} {β : Type v} {γ : Type w} @[simp] theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ []. theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) theorem cons_inj {a : α} : injective (cons a) := assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe @[simp] theorem cons_inj' (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' := ⟨λ e, cons_inj e, congr_arg _⟩ /- mem -/ theorem eq_nil_of_forall_not_mem : ∀ {l : list α}, (∀ a, a ∉ l) → l = nil | [] := assume h, rfl | (b :: l') := assume h, absurd (mem_cons_self b l') (h b) theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _ theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b := assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, this) (assume : a ∈ [], absurd this (not_mem_nil a)) @[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b := ⟨eq_of_mem_singleton, by intro h; simp [h]⟩ theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (assume : a = b, begin subst a, exact binl end) (assume : a ∈ l, this) theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t := mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩ theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] := ⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩ theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l | (b::l) _ := zero_lt_succ _ theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l | (b::l) _ := ⟨b, mem_cons_self _ _⟩ theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l := ⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩ theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t := begin induction l with b l ih; simp at h; cases h with h h, { subst h, exact ⟨[], l, rfl⟩ }, { cases ih h with s e, cases e with t e, subst l, exact ⟨b::s, t, rfl⟩ } end theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r) theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l := assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2)) theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l := assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p) theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l := begin induction l with b l' ih, {simp at h, contradiction }, {simp, simp at h, cases h with h h, {simp *}, {exact or.inr (ih h)}} end theorem exists_of_mem_map {f : α → β} {b : β} {l : list α} (h : b ∈ map f l) : ∃ a, a ∈ l ∧ f a = b := begin induction l with c l' ih, {simp at h, contradiction}, {cases (eq_or_mem_of_mem_cons h) with h h, {existsi c, simp [h]}, {cases ih h with a ha, cases ha with ha₁ ha₂, existsi a, simp * }} end @[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b := ⟨exists_of_mem_map, λ ⟨a, la, h⟩, by rw [← h]; exact mem_map_of_mem f la⟩ @[simp] theorem mem_map_of_inj {f : α → β} (H : injective f) {a : α} {l : list α} : f a ∈ map f l ↔ a ∈ l := ⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩ @[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l | [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩ | (c :: L) := by simp [join, @mem_join L, or_and_distrib_right, exists_or_distrib] theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l := mem_join.1 theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := mem_join.2 ⟨l, lL, al⟩ @[simp] theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a := iff.trans mem_join ⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩, λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩ theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a := mem_bind.1 theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := mem_bind.2 ⟨a, al, h⟩ /- list subset -/ theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl theorem subset_app_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_left _ _ theorem subset_app_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_right _ _ @[simp] theorem cons_subset {a : α} {l m : list α} : a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem cons_subset_of_subset_of_mem {a : α} {l m : list α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem app_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = [] | [] s := rfl | (a::l) s := false.elim $ s $ mem_cons_self a l theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l := show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩ /- append -/ theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction theorem append_foldl (f : α → β → α) (a : α) (s t : list β) : foldl f a (s ++ t) = foldl f (foldl f a s) t := by {induction s with b s H generalizing a, refl, simp [foldl], rw H _} theorem append_foldr (f : α → β → β) (a : β) (s t : list α) : foldr f a (s ++ t) = foldr f (foldr f a t) s := by {induction s with b s H generalizing a, refl, simp [foldr], rw H _} @[simp] lemma append_eq_nil (p q : list α) : (p ++ q) = [] ↔ p = [] ∧ q = [] := by cases p; simp /- join -/ attribute [simp] join @[simp] theorem join_append (L₁ L₂ : list (list α)) : join (L₁ ++ L₂) = join L₁ ++ join L₂ := by induction L₁; simp * /- repeat take drop -/ /-- Split a list at an index. `split 2 [a, b, c] = ([a, b], [c])` -/ def split_at : ℕ → list α → list α × list α | 0 a := ([], a) | (succ n) [] := ([], []) | (succ n) (x :: xs) := let (l, r) := split_at n xs in (x :: l, r) @[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l) | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp [split_at, split_at_eq_take_drop n xs] @[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp [take_append_drop n xs] -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj : ∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂ | [] [] t₁ t₂ h hl := ⟨rfl, h⟩ | (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl | [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm | (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap, let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩ theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : t₁ = t₂ := (append_inj h hl).right theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : s₁ = s₂ := (append_inj h hl).left theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ ∧ t₁ = t₂ := append_inj h $ @nat.add_right_cancel _ (length t₁) _ $ let hap := congr_arg length h in by simp at hap; rwa [← hl] at hap theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : t₁ = t₂ := (append_inj' h hl).right theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ := (append_inj' h hl).left theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ := append_inj_left h rfl theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ := append_inj_right' h rfl theorem append_left_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ := ⟨append_left_cancel, congr_arg _⟩ theorem append_right_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ := ⟨append_right_cancel, congr_arg _⟩ theorem eq_of_mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n → b = a | (n+1) h := or.elim h id $ @eq_of_mem_repeat _ theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length | [] H := rfl | (b::l) H := have b = a ∧ ∀ (x : α), x ∈ l → x = a, by simpa [or_imp_distrib, forall_and_distrib] using H, by dsimp; congr; [exact this.1, exact eq_repeat_of_mem this.2] theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩ theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩, λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩ theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] := λ b h, mem_singleton.2 (eq_of_mem_repeat h) @[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length := by induction l; simp [-add_comm, *] theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := by rw map_const at h; exact eq_of_mem_repeat h /- bind -/ @[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) : l >>= f = l.bind f := rfl /- concat -/ /-- Concatenate an element at the end of a list. `concat [a, b] c = [a, b, c]` -/ @[simp] def concat : list α → α → list α | [] a := [a] | (b::l) a := b :: concat l a @[simp] theorem concat_nil (a : α) : concat [] a = [a] := rfl @[simp] theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] := by induction l; intro h; contradiction @[simp] theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ := by induction l₁ with b l₁ ih; [simp, simp [ih]] @[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] := by induction l; simp [*, concat] @[simp] theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) := by simp [succ_eq_add_one] theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a := by induction l₂ with b l₂ ih; simp /- reverse -/ @[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl local attribute [simp] reverse_core @[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a := have aux : ∀ l₁ l₂, reverse_core l₁ (concat l₂ a) = concat (reverse_core l₁ l₂) a, by intros l₁; induction l₁; intros; rsimp, aux l nil theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] := by simp @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) := by induction s; simp * @[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l := by induction l; simp * theorem reverse_injective : injective (@reverse α) := injective_of_left_inverse reverse_reverse @[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ := reverse_injective.eq_iff @[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] := @reverse_inj _ l [] theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) := by simp @[simp] theorem length_reverse (l : list α) : length (reverse l) = length l := by induction l; simp * @[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) := by induction l; simp * @[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l := by induction l; simp [*, or_comm] @[elab_as_eliminator] theorem reverse_rec_on {C : list α → Sort*} (l : list α) (H0 : C []) (H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l := begin rw ← reverse_reverse l, induction reverse l, { exact H0 }, { simp, exact H1 _ _ ih } end /- last -/ @[simp] theorem last_cons {a : α} {l : list α} : ∀ (h₁ : a :: l ≠ nil) (h₂ : l ≠ nil), last (a :: l) h₁ = last l h₂ := by {induction l; intros, contradiction, simp *, reflexivity} @[simp] theorem last_append {a : α} (l : list α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a := begin induction l with hd tl ih; rsimp, have haux : tl ++ [a] ≠ [], {apply append_ne_nil_of_ne_nil_right, contradiction}, simp * end theorem last_concat {a : α} (l : list α) (h : concat l a ≠ []) : last (concat l a) h = a := by simp * @[simp] theorem last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl @[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ /- head and tail -/ @[simp] theorem head_cons [h : inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil α) = [] := rfl @[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl @[simp] theorem head_append [h : inhabited α] (t : list α) {s : list α} (h : s ≠ []) : head (s ++ t) = head s := by {induction s, contradiction, simp} theorem cons_head_tail [h : inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l := by {induction l, contradiction, simp} /- map -/ lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l | [] _ := rfl | (a::l) h := have f a = g a, from h _ (mem_cons_self _ _), have map f l = map g l, from map_congr $ assume a', h _ ∘ mem_cons_of_mem _, show f a :: map f l = g a :: map g l, by simp [*] theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) := by induction l; simp * theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l := by induction l; simp * @[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) : foldl f a (map g l) = foldl (λx y, f x (g y)) a l := by revert a; induction l; intros; simp * @[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) : foldr f a (map g l) = foldr (f ∘ g) a l := by revert a; induction l; intros; simp * theorem foldl_hom (f : α → β) (g : α → γ → α) (g' : β → γ → β) (a : α) (h : ∀a x, f (g a x) = g' (f a) x) (l : list γ) : f (foldl g a l) = foldl g' (f a) l := by revert a; induction l; intros; simp * theorem foldr_hom (f : α → β) (g : γ → α → α) (g' : γ → β → β) (a : α) (h : ∀x a, f (g x a) = g' x (f a)) (l : list γ) : f (foldr g a l) = foldr g' (f a) l := by revert a; induction l; intros; simp * theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil := eq_nil_of_length_eq_zero (begin rw [← length_map f l], simp [h] end) @[simp] theorem map_join (f : α → β) (L : list (list α)) : map f (join L) = join (map (map f) L) := by induction L; simp * theorem bind_ret_eq_map {α β} (f : α → β) (l : list α) : l.bind (list.ret ∘ f) = map f l := by simp [list.bind]; induction l; simp [list.ret, join, *] @[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl /- map₂ -/ theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] := by cases l; refl theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] := by cases l; refl /- sublists -/ @[simp] theorem nil_sublist : Π (l : list α), [] <+ l | [] := sublist.slnil | (a :: l) := sublist.cons _ _ a (nil_sublist l) @[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l | [] := sublist.slnil | (a :: l) := sublist.cons2 _ _ a (sublist.refl l) @[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ := sublist.rec_on h₂ (λ_ s, s) (λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁)) (λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁ (λ_, nil_sublist _) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons _ _ _ (IH _ h₁) end) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons2 _ _ _ (IH _ h₁) end) rfl) l₁ h₁ @[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l := sublist.cons _ _ _ (sublist.refl l) theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ := sublist.trans (sublist_cons a l₁) theorem cons_sublist_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ := sublist.cons2 _ _ _ s @[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂ | [] l₂ := nil_sublist _ | (a::l₁) l₂ := cons_sublist_cons _ (sublist_append_left l₁ l₂) @[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂ | [] l₂ := sublist.refl _ | (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂) theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ := sublist.cons _ _ _ theorem sublist_app_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ := s.trans $ sublist_append_left _ _ theorem sublist_app_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ := s.trans $ sublist_append_right _ _ theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂ | ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s | ._ (sublist.cons2 ._ ._ a s) := s theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ := ⟨sublist_of_cons_sublist_cons, cons_sublist_cons _⟩ @[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂ | [] := iff.rfl | (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l) theorem append_sublist_append_of_sublist_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l := begin induction h with _ _ a _ ih _ _ a _ ih, { refl }, { apply sublist_cons_of_sublist a ih }, { apply cons_sublist_cons a ih } end theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) : l <+ l₁ ++ l₂ ∨ a ∈ l := begin induction l₁ with b l₁ IH generalizing l, { cases h; simp * }, { cases h with _ _ _ h _ _ _ h, { exact or.imp_left (sublist_cons_of_sublist _) (IH h) }, { exact (IH h).imp (cons_sublist_cons _) (mem_cons_of_mem _) } } end theorem reverse_sublist {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse := begin induction h with _ _ _ _ ih _ _ a _ ih; simp, { exact sublist_app_of_sublist_left ih }, { exact append_sublist_append_of_sublist_right ih [a] } end @[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ := ⟨λ h, by have := reverse_sublist h; simp at this; assumption, reverse_sublist⟩ @[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ := ⟨λ h, by have := reverse_sublist h; simp at this; assumption, λ h, append_sublist_append_of_sublist_right h l⟩ theorem subset_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂ | ._ ._ sublist.slnil b h := h | ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (subset_of_sublist s h) | ._ ._ (sublist.cons2 l₁ l₂ a s) b h := match eq_or_mem_of_mem_cons h with | or.inl h := h ▸ mem_cons_self _ _ | or.inr h := mem_cons_of_mem _ (subset_of_sublist s h) end theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l := ⟨λ h, subset_of_sublist h (mem_singleton_self _), λ h, let ⟨s, t, e⟩ := mem_split h in e.symm ▸ (cons_sublist_cons _ (nil_sublist _)).trans (sublist_append_right _ _)⟩ theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] := eq_nil_of_subset_nil $ subset_of_sublist s theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n := ⟨λ h, by simpa using length_le_of_sublist h, λ h, by induction h; [apply sublist.refl, simp [*, sublist.cons]] ⟩ theorem eq_of_sublist_of_length_eq : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂ | ._ ._ sublist.slnil h := rfl | ._ ._ (sublist.cons l₁ l₂ a s) h := absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self | ._ ._ (sublist.cons2 l₁ l₂ a s) h := by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h theorem eq_of_sublist_of_length_le {l₁ l₂ : list α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) : l₁ = l₂ := eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h) theorem sublist_antisymm {l₁ l₂ : list α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂) instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂) | [] l₂ := is_true $ nil_sublist _ | (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h | (a::l₁) (b::l₂) := if h : a = b then decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $ by rw [← h]; exact ⟨cons_sublist_cons _, sublist_of_cons_sublist_cons⟩ else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂) ⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with | a, l₁, sublist.cons ._ ._ ._ s', h := s' | ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h end⟩ /- index_of -/ section index_of variable [decidable_eq α] @[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl theorem index_of_cons (a b : α) (l : list α) : index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 := assume e, if_pos e @[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 := index_of_cons_eq _ rfl @[simp] theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) := assume n, if_neg n theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l := begin induction l with b l ih; simp [-add_comm], by_cases h : a = b; simp [h, -add_comm], { intro, contradiction }, { rw ← ih, exact ⟨succ_inj, congr_arg _⟩ } end @[simp] theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l := index_of_eq_length.2 theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l := begin induction l with b l ih; simp [-add_comm, index_of_cons], by_cases h : a = b; simp [h, -add_comm, zero_le], exact succ_le_succ ih end theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l := ⟨λh, by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al, λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩ end index_of /- nth element -/ theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a | a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩ | a (b :: l) (or.inr m) := let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩ theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h) | (a :: l) 0 h := rfl | (a :: l) (n+1) h := @nth_le_nth l n _ theorem nth_ge_len : ∀ {l : list α} {n}, n ≥ length l → nth l n = none | [] n h := rfl | (a :: l) (n+1) h := nth_ge_len (le_of_succ_le_succ h) theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a := ⟨λ e, have h : n < length l, from lt_of_not_ge $ λ hn, by rw nth_ge_len hn at e; contradiction, ⟨h, by rw nth_le_nth h at e; injection e with e; apply nth_le_mem⟩, λ ⟨h, e⟩, e ▸ nth_le_nth _⟩ theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a := let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩ theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l | (a :: l) 0 h := mem_cons_self _ _ | (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _) theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l := let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _ theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a := ⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩ theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a := mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂ | [] [] h := rfl | (a::l₁) [] h := by have h0 := h 0; contradiction | [] (a'::l₂) h := by have h0 := h 0; contradiction | (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa; simp [*, ext (λn, h (n+1))] theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂) (h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ := ext $ λn, if h₁ : n < length l₁ then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])] else let h₁ := le_of_not_gt h₁ in by rw [nth_ge_len h₁, nth_ge_len (by rwa [← hl])] @[simp] theorem index_of_nth_le [decidable_eq α] {a : α} : ∀ {l : list α} h, nth_le l (index_of a l) h = a | (b::l) h := by by_cases h' : a = b; simp * @[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) : nth l (index_of a l) = some a := by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)] theorem nth_le_reverse_aux1 : ∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 | [] r i := λh1 h2, rfl | (a :: l) r i := by rw (show i + length (a :: l) = i + 1 + length l, by simp); exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2) theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2), nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 | [] r i h1 h2 := absurd h2 (not_lt_zero _) | (a :: l) r 0 h1 h2 := begin have aux := nth_le_reverse_aux1 l (a :: r) 0, rw zero_add at aux, exact aux _ (zero_lt_succ _) end | (a :: l) r (i+1) h1 h2 := begin have aux := nth_le_reverse_aux2 l (a :: r) i, have heq := calc length (a :: l) - 1 - (i + 1) = length l - (1 + i) : by rw add_comm; refl ... = length l - 1 - i : by rw nat.sub_sub, rw [← heq] at aux, apply aux end @[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 _ _ _ _ _ /-- Convert a list into an array (whose length is the length of `l`) -/ def to_array (l : list α) : array l.length α := {data := λ v, l.nth_le v.1 v.2} /-- "inhabited" `nth` function: returns `default` instead of `none` in the case that the index is out of bounds. -/ @[simp] def inth [h : inhabited α] (l : list α) (n : nat) : α := (nth l n).iget /- nth tail operation -/ /-- Apply a function to the nth tail of `l`. `modify_nth_tail f 2 [a, b, c] = [a, b] ++ f [c]`. Returns the input without using `f` if the index is larger than the length of the list. -/ @[simp] def modify_nth_tail (f : list α → list α) : ℕ → list α → list α | 0 l := f l | (n+1) [] := [] | (n+1) (a::l) := a :: modify_nth_tail n l /-- Apply `f` to the head of the list, if it exists. -/ @[simp] def modify_head (f : α → α) : list α → list α | [] := [] | (a::l) := f a :: l /-- Apply `f` to the nth element of the list, if it exists. -/ def modify_nth (f : α → α) : ℕ → list α → list α := modify_nth_tail (modify_head f) theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _) theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α), update_nth l n a = modify_nth (λ _, a) n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _) theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α), modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := (congr_arg (cons b) (modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m, nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m | n l 0 := by cases l; cases n; refl | n [] (m+1) := by cases n; refl | 0 (a::l) (m+1) := by cases nth l m; refl | (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $ by cases nth l m with b; by_cases n = m; simp [h, mt succ_inj] theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) : ∀ n l, length (modify_nth_tail f n l) = length l | 0 l := H _ | (n+1) [] := rfl | (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _) @[simp] theorem modify_nth_length (f : α → α) : ∀ n l, length (modify_nth f n l) = length l := modify_nth_tail_length _ (λ l, by cases l; refl) @[simp] theorem update_nth_length (l : list α) (n) (a : α) : length (update_nth l n a) = length l := by simp [update_nth_eq_modify_nth] @[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) : nth (modify_nth f n l) n = f <$> nth l n := by simp [nth_modify_nth] @[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) : nth (modify_nth f m l) n = nth l n := by simp [nth_modify_nth, h]; cases nth l n; refl theorem nth_update_nth_eq (a : α) (n) (l : list α) : nth (update_nth l n a) n = (λ _, a) <$> nth l n := by simp [update_nth_eq_modify_nth] theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) : nth (update_nth l n a) n = some a := by rw [nth_update_nth_eq, nth_le_nth h]; refl theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) : nth (update_nth l m a) n = nth l n := by simp [update_nth_eq_modify_nth, h] /- take, drop -/ @[simp] theorem take_zero : ∀ (l : list α), take 0 l = [] := begin intros, reflexivity end @[simp] theorem take_nil : ∀ n, take n [] = ([] : list α) | 0 := rfl | (n+1) := rfl theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl theorem take_all : ∀ (l : list α), take (length l) l = l | [] := rfl | (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_all end theorem take_all_of_ge : ∀ {n} {l : list α}, n ≥ length l → take n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: take n l = a :: l, rw [take_all_of_ge (le_of_succ_le_succ h)] end theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l | n 0 l := by rw [min_zero, take_zero, take_nil] | 0 m l := by simp | (succ n) (succ m) nil := by simp | (succ n) (succ m) (a::l) := by simp [min_succ_succ, take_take] theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h, drop n l = nth_le l n h :: drop (n+1) l | 0 (a::l) h := rfl | (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _ theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) : ∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l) | 0 l := rfl | (n+1) [] := H.symm | (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l) theorem modify_nth_eq_take_drop (f : α → α) : ∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop _ rfl theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l := by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n+1) l := by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h] @[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] := by cases l; cases n; simp [update_nth] /- take_while -/ /-- Get the longest initial segment of the list whose members all satisfy `p`. `take_while (λ x, x < 3) [0, 2, 5, 1] = [0, 2]` -/ def take_while (p : α → Prop) [decidable_pred p] : list α → list α | [] := [] | (a::l) := if p a then a :: take_while l else [] /- foldl, foldr, scanl, scanr -/ @[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : α → β → α) : ∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂ | a [] l₂ := rfl | a (b::l₁) l₂ := by simp [foldl_append] @[simp] theorem foldr_append (f : α → β → β) : ∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by simp [foldr_append] @[simp] theorem foldl_join (f : α → β → α) : ∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L | a [] := rfl | a (l::L) := by simp [foldl_join] @[simp] theorem foldr_join (f : α → β → β) : ∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L | a [] := rfl | a (l::L) := by simp [foldr_join] theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) : foldl f a (reverse l) = foldr (λx y, f y x) a l := by induction l; simp [*, foldr] theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) : foldr f a (reverse l) = foldl (λx y, f y x) a l := let t := foldl_reverse (λx y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t @[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l | [] := rfl | (x::l) := by simp [foldr_eta l] /-- Fold a function `f` over the list from the left, returning the list of partial results. `scanl (+) 0 [1, 2, 3] = [0, 1, 3, 6]` -/ def scanl (f : α → β → α) : α → list β → list α | a [] := [a] | a (b::l) := a :: scanl (f a b) l def scanr_aux (f : α → β → β) (b : β) : list α → β × list β | [] := (b, []) | (a::l) := let (b', l') := scanr_aux l in (f a b', b' :: l') /-- Fold a function `f` over the list from the right, returning the list of partial results. `scanr (+) 0 [1, 2, 3] = [6, 5, 3, 0]` -/ def scanr (f : α → β → β) (b : β) (l : list α) : list β := let (b', l') := scanr_aux f b l in b' :: l' @[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α), scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l) | a [] := rfl | a (x::l) := let t := scanr_aux_cons x l in by simp [scanr, scanr_aux] at t; simp [scanr, scanr_aux, t] @[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : scanr f b (a::l) = foldr f b (a::l) :: scanr f b l := by simp [scanr] section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f) include hassoc theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l) | a b nil := rfl | a b (c :: l) := by simp [foldl1_eq_foldr1 _ _ l]; rw hassoc include hcomm theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := by simp; rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; simp theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := by simp [foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l) end foldl_eq_foldr section variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op] local notation a * b := op a b local notation l <*> a := foldl op a l include ha lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂) | [] a₁ a₂ := by simp | (a :: l) a₁ a₂ := calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp [ha.assoc] ... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc]; simp lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂ | [] a₁ a₂ := by simp | (a :: l) a₁ a₂ := by simp [foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] include hc lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) := by rw [foldl_cons, hc.comm, foldl_assoc] end /- sum -/ /-- Product of a list. `prod [a, b, c] = ((1 * a) * b) * c` -/ @[to_additive list.sum] def prod [has_mul α] [has_one α] : list α → α := foldl (*) 1 attribute [to_additive list.sum.equations._eqn_1] list.prod.equations._eqn_1 section monoid variables [monoid α] {l l₁ l₂ : list α} {a : α} @[simp, to_additive list.sum_nil] theorem prod_nil : ([] : list α).prod = 1 := rfl @[simp, to_additive list.sum_cons] theorem prod_cons : (a::l).prod = a * l.prod := calc (a::l).prod = foldl (*) (a * 1) l : by simp [list.prod] ... = _ : foldl_assoc @[simp, to_additive list.sum_append] theorem prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod := calc (l₁ ++ l₂).prod = foldl (*) (foldl (*) 1 l₁ * 1) l₂ : by simp [list.prod] ... = l₁.prod * l₂.prod : foldl_assoc @[simp, to_additive list.sum_join] theorem prod_join {l : list (list α)} : l.join.prod = (l.map list.prod).prod := by induction l; simp [list.join, *] at * end monoid @[simp] theorem sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n := by induction n; simp [*, nat.mul_succ] @[simp] theorem length_join (L : list (list α)) : length (join L) = sum (map length L) := by induction L; simp * @[simp] theorem length_bind (l : list α) (f : α → list β) : length (list.bind l f) = sum (map (length ∘ f) l) := by rw [list.bind, length_join, map_map] /- all & any, bounded quantifiers over lists -/ theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x := by simp @[simp] theorem forall_mem_cons' {p : α → Prop} {a : α} {l : list α} : (∀ (x : α), x = a ∨ x ∈ l → p x) ↔ p a ∧ ∀ x ∈ l, p x := by simp [or_imp_distrib, forall_and_distrib] theorem forall_mem_cons {p : α → Prop} {a : α} {l : list α} : (∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x := by simp theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x := by simp theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) : ∃ x ∈ a :: l, p x := bex.intro a (by simp) h theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) : ∃ x ∈ a :: l, p x := bex.elim h (λ x xl px, bex.intro x (by simp [xl]) px) theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) : p a ∨ ∃ x ∈ l, p x := bex.elim h (λ x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, simp [px] end) (assume : x ∈ l, or.inr (bex.intro x this px))) @[simp] theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) @[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl @[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) : all (a::l) p = (p a && all l p) := rfl theorem all_iff_forall {p : α → bool} {l : list α} : all l p ↔ ∀ a ∈ l, p a := by induction l with a l; simp [forall_and_distrib, *] theorem all_iff_forall_prop {p : α → Prop} [decidable_pred p] {l : list α} : all l (λ a, p a) ↔ ∀ a ∈ l, p a := by simp [all_iff_forall] @[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl @[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) : any (a::l) p = (p a || any l p) := rfl theorem any_iff_exists {p : α → bool} {l : list α} : any l p ↔ ∃ a ∈ l, p a := by induction l with a l; simp [or_and_distrib_right, exists_or_distrib, *] theorem any_iff_exists_prop {p : α → Prop} [decidable_pred p] {l : list α} : any l (λ a, p a) ↔ ∃ a ∈ l, p a := by simp [any_iff_exists] theorem any_of_mem {p : α → bool} {a : α} {l : list α} (h₁ : a ∈ l) (h₂ : p a) : any l p := any_iff_exists.2 ⟨_, h₁, h₂⟩ instance decidable_forall_mem {p : α → Prop} [decidable_pred p] (l : list α) : decidable (∀ x ∈ l, p x) := decidable_of_iff _ all_iff_forall_prop instance decidable_exists_mem {p : α → Prop} [decidable_pred p] (l : list α) : decidable (∃ x ∈ l, p x) := decidable_of_iff _ any_iff_exists_prop /- map for partial functions -/ /-- Partial map. If `f : Π a, p a → β` is a partial function defined on `a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β | [] H := [] | (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2 /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id) theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) : @pmap _ _ p (λ a _, f a) l H = map f l := by induction l; simp * theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β} (l : list α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) : pmap f l H₁ = pmap g l H₂ := by induction l with _ _ ih; simp *; apply ih theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β) (l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H := by induction l; simp * theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β) (l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) := by rw [attach, map_pmap]; exact pmap_congr l (λ a h₁ h₂, rfl) theorem attach_map_val (l : list α) : l.attach.map subtype.val = l := by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l) @[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ := by have := mem_map.1 (by rw [attach_map_val]; exact h); { rcases this with ⟨a, m, rfl⟩, cases a, exact m } @[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β} {l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b := by simp [pmap_eq_map_attach] @[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β} {l H} : length (pmap f l H) = length l := by induction l; simp * /- find -/ section find variables (p : α → Prop) [decidable_pred p] /-- `find p l` is the first element of `l` satisfying `p`, or `none` if no such element exists. -/ def find : list α → option α | [] := none | (a::l) := if p a then some a else find l def find_indexes_aux (p : α → Prop) [decidable_pred p] : list α → nat → list nat | [] n := [] | (a::l) n := let t := find_indexes_aux l (succ n) in if p a then n :: t else t /-- `find_indexes p l` is the list of indexes of elements of `l` that satisfy `p`. -/ def find_indexes (p : α → Prop) [decidable_pred p] (l : list α) : list nat := find_indexes_aux p l 0 @[simp] theorem find_nil : find p [] = none := rfl @[simp] theorem find_cons_of_pos {p : α → Prop} [h : decidable_pred p] {a : α} (l) (h : p a) : find p (a::l) = some a := if_pos h @[simp] theorem find_cons_of_neg {p : α → Prop} [h : decidable_pred p] {a : α} (l) (h : ¬ p a) : find p (a::l) = find p l := if_neg h @[simp] theorem find_eq_none {p : α → Prop} [h : decidable_pred p] {l : list α} : find p l = none ↔ ∀ x ∈ l, ¬ p x := begin induction l with a l IH, {simp}, by_cases p a; simp [h, IH] end @[simp] theorem find_some {p : α → Prop} [h : decidable_pred p] {l : list α} {a : α} (H : find p l = some a) : p a := begin induction l with b l IH, {contradiction}, by_cases p b; simp [h] at H, { subst b, assumption }, { exact IH H } end @[simp] theorem find_mem {p : α → Prop} [h : decidable_pred p] {l : list α} {a : α} (H : find p l = some a) : a ∈ l := begin induction l with b l IH, {contradiction}, by_cases p b; simp [h] at H, { subst b, apply mem_cons_self }, { exact mem_cons_of_mem _ (IH H) } end end find /-- `indexes_of a l` is the list of all indexes of `a` in `l`. `indexes_of a [a, b, a, a] = [0, 2, 3]` -/ def indexes_of [decidable_eq α] (a : α) : list α → list nat := find_indexes (eq a) /- filter_map -/ @[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) : filter_map f (a :: l) = filter_map f l := by simp [filter_map, h] @[simp] theorem filter_map_cons_some (f : α → option β) (a : α) (l : list α) {b : β} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := by simp [filter_map, h] theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f := begin funext l, induction l with a l IH, {simp}, simp [filter_map_cons_some (some ∘ f) _ _ rfl, IH] end theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := begin funext l, induction l with a l IH, {simp}, by_cases pa : p a; simp [filter_map, option.guard, pa, IH] end theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) : filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l := begin induction l with a l IH, {refl}, cases h : f a with b, { rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH], simp [h, option.bind] }, rw filter_map_cons_some _ _ _ h, cases h' : g b with c; [ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH], rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ]; simp [h, h', option.bind] end theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) : map g (filter_map f l) = filter_map (λ x, (f x).map g) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) : filter_map g (map f l) = filter_map (g ∘ f) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) : filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l := by rw [← filter_map_eq_filter, filter_map_filter_map]; refl theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) : filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l := begin rw [← filter_map_eq_filter, filter_map_filter_map], congr, funext x, show (option.guard p x).bind f = ite (p x) (f x) none, by_cases p x; simp [h, option.guard, option.bind] end @[simp] theorem filter_map_some (l : list α) : filter_map some l = l := by rw filter_map_eq_map; apply map_id @[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} : b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b := begin induction l with a l IH, {simp}, cases h : f a with b', { have : f a ≠ some b, {rw h, intro, contradiction}, simp [filter_map_cons_none _ _ h, IH, or_and_distrib_right, exists_or_distrib, this] }, { have : f a = some b ↔ b = b', { split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} }, simp [filter_map_cons_some _ _ _ h, IH, or_and_distrib_right, exists_or_distrib, this] } end theorem map_filter_map_of_inv (f : α → option β) (g : β → α) (H : ∀ x : α, (f x).map g = some x) (l : list α) : map g (filter_map f l) = l := by simp [map_filter_map, H] theorem filter_map_sublist_filter_map (f : α → option β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ := by induction s with l₁ l₂ a s IH l₁ l₂ a s IH; simp [filter_map]; cases f a with b; simp [filter_map, IH, sublist.cons, sublist.cons2] theorem map_sublist_map (f : α → β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : map f l₁ <+ map f l₂ := by rw ← filter_map_eq_map; exact filter_map_sublist_filter_map _ s /- filter -/ section filter variables {p : α → Prop} [decidable_pred p] @[simp] theorem filter_subset (l : list α) : filter p l ⊆ l := subset_of_sublist $ filter_sublist l theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a | [] ain := absurd ain (not_mem_nil a) | (b::l) ain := if pb : p b then have a ∈ b :: filter p l, begin simp [pb] at ain, assumption end, or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, begin rw [← this] at pb, exact pb end) (assume : a ∈ filter p l, of_mem_filter this) else begin simp [pb] at ain, exact (of_mem_filter ain) end theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l | [] ain pa := absurd ain (not_mem_nil a) | (b::l) ain pa := if pb : p b then or.elim (eq_or_mem_of_mem_cons ain) (assume : a = b, by simp [pb, this]) (assume : a ∈ l, begin simp [pb], exact (mem_cons_of_mem _ (mem_filter_of_mem this pa)) end) else or.elim (eq_or_mem_of_mem_cons ain) (assume : a = b, begin simp [this] at pa, contradiction end) --absurd (this ▸ pa) pb) (assume : a ∈ l, by simp [pa, pb, mem_filter_of_mem this]) @[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a := ⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩ theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a := begin induction l with a l, {simp}, by_cases p a; simp [filter, *], show filter p l ≠ a :: l, intro e, have := filter_sublist l, rw e at this, exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) end theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a := by simp [-and.comm, eq_nil_iff_forall_not_mem, mem_filter] theorem filter_sublist_filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ := by rw ← filter_map_eq_filter; exact filter_map_sublist_filter_map _ s @[simp] theorem span_eq_take_drop (p : α → Prop) [decidable_pred p] : ∀ (l : list α), span p l = (take_while p l, drop_while p l) | [] := rfl | (a::l) := by by_cases pa : p a; simp [span, take_while, drop_while, pa, span_eq_take_drop l] @[simp] theorem take_while_append_drop (p : α → Prop) [decidable_pred p] : ∀ (l : list α), take_while p l ++ drop_while p l = l | [] := rfl | (a::l) := by by_cases pa : p a; simp [take_while, drop_while, pa, take_while_append_drop l] /-- `countp p l` is the number of elements of `l` that satisfy `p`. -/ def countp (p : α → Prop) [decidable_pred p] : list α → nat | [] := 0 | (x::xs) := if p x then succ (countp xs) else countp xs @[simp] theorem countp_nil (p : α → Prop) [decidable_pred p] : countp p [] = 0 := rfl @[simp] theorem countp_cons_of_pos {a : α} (l) (pa : p a) : countp p (a::l) = countp p l + 1 := if_pos pa @[simp] theorem countp_cons_of_neg {a : α} (l) (pa : ¬ p a) : countp p (a::l) = countp p l := if_neg pa theorem countp_eq_length_filter (l) : countp p l = length (filter p l) := by induction l with x l; [refl, by_cases (p x)]; simp [*, -add_comm] local attribute [simp] countp_eq_length_filter @[simp] theorem countp_append (l₁ l₂) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ := by simp theorem countp_pos {l} : 0 < countp p l ↔ ∃ a ∈ l, p a := by simp [countp_eq_length_filter, length_pos_iff_exists_mem] theorem countp_le_of_sublist {l₁ l₂} (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ := by simpa using length_le_of_sublist (filter_sublist_filter s) end filter /- count -/ section count variable [decidable_eq α] /-- `count a l` is the number of occurrences of `a` in `l`. -/ def count (a : α) : list α → nat := countp (eq a) @[simp] theorem count_nil (a : α) : count a [] = 0 := rfl theorem count_cons (a b : α) (l : list α) : count a (b :: l) = if a = b then succ (count a l) else count a l := rfl theorem count_cons' (a b : α) (l : list α) : count a (b :: l) = count a l + (if a = b then 1 else 0) := decidable.by_cases (assume : a = b, begin rw [count_cons, if_pos this, if_pos this] end) (assume : a ≠ b, begin rw [count_cons, if_neg this, if_neg this], reflexivity end) @[simp] theorem count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) := if_pos rfl @[simp] theorem count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l := if_neg h theorem count_le_of_sublist (a : α) {l₁ l₂} : l₁ <+ l₂ → count a l₁ ≤ count a l₂ := countp_le_of_sublist theorem count_le_count_cons (a b : α) (l : list α) : count a l ≤ count a (b :: l) := count_le_of_sublist _ (sublist_cons _ _) theorem count_singleton (a : α) : count a [a] = 1 := by simp @[simp] theorem count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ := countp_append @[simp] theorem count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) := by rw [concat_eq_append, count_append, count_singleton] theorem count_pos {a : α} {l : list α} : 0 < count a l ↔ a ∈ l := by simp [count, countp_pos] @[simp] theorem count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 := by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h') theorem not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l := λ h', ne_of_gt (count_pos.2 h') h @[simp] theorem count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n := by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat]; exact λ b m, (eq_of_mem_repeat m).symm theorem le_count_iff_repeat_sublist {a : α} {l : list α} {n : ℕ} : n ≤ count a l ↔ repeat a n <+ l := ⟨λ h, ((repeat_sublist_repeat a).2 h).trans $ have filter (eq a) l = repeat a (count a l), from eq_repeat.2 ⟨by simp [count, countp_eq_length_filter], λ b m, (of_mem_filter m).symm⟩, by rw ← this; apply filter_sublist, λ h, by simpa using count_le_of_sublist a h⟩ end count /- prefix, suffix, infix -/ /-- `is_prefix l₁ l₂`, or `l₁ <+: l₂`, means that `l₁` is a prefix of `l₂`, that is, `l₂` has the form `l₁ ++ t` for some `t`. -/ def is_prefix (l₁ : list α) (l₂ : list α) : Prop := ∃ t, l₁ ++ t = l₂ /-- `is_suffix l₁ l₂`, or `l₁ <:+ l₂`, means that `l₁` is a suffix of `l₂`, that is, `l₂` has the form `t ++ l₁` for some `t`. -/ def is_suffix (l₁ : list α) (l₂ : list α) : Prop := ∃ t, t ++ l₁ = l₂ /-- `is_infix l₁ l₂`, or `l₁ <:+: l₂`, means that `l₁` is a contiguous substring of `l₂`, that is, `l₂` has the form `s ++ l₁ ++ t` for some `s, t`. -/ def is_infix (l₁ : list α) (l₂ : list α) : Prop := ∃ s t, s ++ l₁ ++ t = l₂ infix ` <+: `:50 := is_prefix infix ` <:+ `:50 := is_suffix infix ` <:+: `:50 := is_infix @[simp] theorem prefix_append (l₁ l₂ : list α) : l₁ <+: l₁ ++ l₂ := ⟨l₂, rfl⟩ @[simp] theorem suffix_append (l₁ l₂ : list α) : l₂ <:+ l₁ ++ l₂ := ⟨l₁, rfl⟩ @[simp] theorem infix_append (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := ⟨l₁, l₃, rfl⟩ theorem nil_prefix (l : list α) : [] <+: l := ⟨l, rfl⟩ theorem nil_suffix (l : list α) : [] <:+ l := ⟨l, append_nil _⟩ @[refl] theorem prefix_refl (l : list α) : l <+: l := ⟨[], append_nil _⟩ @[refl] theorem suffix_refl (l : list α) : l <:+ l := ⟨[], rfl⟩ @[simp] theorem suffix_cons (a : α) : ∀ l, l <:+ a :: l := suffix_append [a] @[simp] theorem prefix_concat (a : α) (l) : l <+: concat l a := by simp theorem infix_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <:+: l₂ := λ⟨t, h⟩, ⟨[], t, h⟩ theorem infix_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <:+: l₂ := λ⟨t, h⟩, ⟨t, [], by simp [h]⟩ @[refl] theorem infix_refl (l : list α) : l <:+: l := infix_of_prefix $ prefix_refl l theorem nil_infix (l : list α) : [] <:+: l := infix_of_prefix $ nil_prefix l @[trans] theorem is_prefix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃ | l ._ ._ ⟨r₁, rfl⟩ ⟨r₂, rfl⟩ := ⟨r₁ ++ r₂, by simp⟩ @[trans] theorem is_suffix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃ | l ._ ._ ⟨l₁, rfl⟩ ⟨l₂, rfl⟩ := ⟨l₂ ++ l₁, by simp⟩ @[trans] theorem is_infix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃ | l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨l₂, r₂, rfl⟩ := ⟨l₂ ++ l₁, r₁ ++ r₂, by simp⟩ theorem sublist_of_infix {l₁ l₂ : list α} : l₁ <:+: l₂ → l₁ <+ l₂ := λ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _) theorem sublist_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_prefix theorem sublist_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_suffix theorem reverse_suffix {l₁ l₂ : list α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ := ⟨λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩, λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩ theorem reverse_prefix {l₁ l₂ : list α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ := by rw ← reverse_suffix; simp theorem length_le_of_infix {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ := length_le_of_sublist $ sublist_of_infix s theorem eq_nil_of_infix_nil {l : list α} (s : l <:+: []) : l = [] := eq_nil_of_sublist_nil $ sublist_of_infix s theorem eq_nil_of_prefix_nil {l : list α} (s : l <+: []) : l = [] := eq_nil_of_infix_nil $ infix_of_prefix s theorem eq_nil_of_suffix_nil {l : list α} (s : l <:+ []) : l = [] := eq_nil_of_infix_nil $ infix_of_suffix s theorem infix_iff_prefix_suffix (l₁ l₂ : list α) : l₁ <:+: l₂ ↔ ∃ t, l₁ <+: t ∧ t <:+ l₂ := ⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩, λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩ theorem eq_of_infix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_infix s theorem eq_of_prefix_of_length_eq {l₁ l₂ : list α} (s : l₁ <+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_prefix s theorem eq_of_suffix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+ l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_suffix s theorem prefix_of_prefix_length_le : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂ | [] l₂ l₃ h₁ h₂ _ := nil_prefix _ | (a::l₁) (b::l₂) _ ⟨r₁, rfl⟩ ⟨r₂, e⟩ ll := begin injection e with _ e', subst b, rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩ (le_of_succ_le_succ ll) with ⟨r₃, rfl⟩, exact ⟨r₃, rfl⟩ end theorem prefix_or_prefix_of_prefix {l₁ l₂ l₃ : list α} (h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ := (le_total (length l₁) (length l₂)).imp (prefix_of_prefix_length_le h₁ h₂) (prefix_of_prefix_length_le h₂ h₁) theorem suffix_of_suffix_length_le {l₁ l₂ l₃ : list α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ := reverse_prefix.1 $ prefix_of_prefix_length_le (reverse_prefix.2 h₁) (reverse_prefix.2 h₂) (by simp [ll]) theorem suffix_or_suffix_of_suffix {l₁ l₂ l₃ : list α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ := (prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 h₂)).imp reverse_prefix.1 reverse_prefix.1 theorem infix_of_mem_join : ∀ {L : list (list α)} {l}, l ∈ L → l <:+: join L | (_ :: L) l (or.inl rfl) := infix_append [] _ _ | (l' :: L) l (or.inr h) := is_infix.trans (infix_of_mem_join h) $ infix_of_suffix $ suffix_append _ _ theorem prefix_append_left_inj {l₁ l₂ : list α} (l) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ := exists_congr $ λ r, by rw [append_assoc, append_left_inj] theorem prefix_cons_inj {l₁ l₂ : list α} (a) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ := prefix_append_left_inj [a] theorem take_prefix (n) (l : list α) : take n l <+: l := ⟨_, take_append_drop _ _⟩ theorem drop_suffix (n) (l : list α) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩ theorem prefix_iff_eq_append {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ := ⟨λ h, let ⟨r, e⟩ := h in begin rwa append_inj_left ((take_append_drop (length l₁) l₂).trans e.symm) _, simp [min_eq_left, length_le_of_sublist (sublist_of_prefix h)], end, λ e, ⟨_, e⟩⟩ theorem suffix_iff_eq_append {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ := ⟨λ ⟨r, e⟩, begin rwa append_inj_right ((take_append_drop (length l₂ - length l₁) l₂).trans e.symm) _, simp [min_eq_left, nat.sub_le, e.symm], apply nat.add_sub_cancel_left end, λ e, ⟨_, e⟩⟩ theorem prefix_iff_eq_take {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ := ⟨λ h, append_right_cancel $ (prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, λ e, e.symm ▸ take_prefix _ _⟩ theorem suffix_iff_eq_drop {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ := ⟨λ h, append_left_cancel $ (suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, λ e, e.symm ▸ drop_suffix _ _⟩ instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂) | [] l₂ := is_true ⟨l₂, rfl⟩ | (a::l₁) [] := is_false $ λ ⟨t, te⟩, list.no_confusion te | (a::l₁) (b::l₂) := if h : a = b then @decidable_of_iff _ _ (by rw [← h, prefix_cons_inj]) (decidable_prefix l₁ l₂) else is_false $ λ ⟨t, te⟩, h $ by injection te -- Alternatively, use mem_tails instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂) | [] l₂ := is_true ⟨l₂, append_nil _⟩ | (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ sublist_of_suffix) dec_trivial | l₁ l₂ := let len1 := length l₁, len2 := length l₂ in if hl : len1 ≤ len2 then decidable_of_iff' (l₁ = drop (len2-len1) l₂) suffix_iff_eq_drop else is_false $ λ h, hl $ length_le_of_sublist $ sublist_of_suffix h /-- `inits l` is the list of initial segments of `l`. `inits [1, 2, 3] = [[], [1], [1, 2], [1, 2, 3]]` -/ @[simp] def inits : list α → list (list α) | [] := [[]] | (a::l) := [] :: map (λt, a::t) (inits l) @[simp] theorem mem_inits : ∀ (s t : list α), s ∈ inits t ↔ s <+: t | s [] := suffices s = nil ↔ s <+: nil, by simpa, ⟨λh, h.symm ▸ prefix_refl [], eq_nil_of_prefix_nil⟩ | s (a::t) := suffices (s = nil ∨ ∃ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa, ⟨λo, match s, o with | ._, or.inl rfl := ⟨_, rfl⟩ | s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in by rw [← hs, ← ht]; exact ⟨s, rfl⟩ end, λmi, match s, mi with | [], ⟨._, rfl⟩ := or.inl rfl | (b::s), ⟨r, hr⟩ := list.no_confusion hr $ λba (st : s++r = t), or.inr $ by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩ end⟩ /-- `tails l` is the list of terminal segments of `l`. `tails [1, 2, 3] = [[1, 2, 3], [2, 3], [3], []]` -/ @[simp] def tails : list α → list (list α) | [] := [[]] | (a::l) := (a::l) :: tails l @[simp] theorem mem_tails : ∀ (s t : list α), s ∈ tails t ↔ s <:+ t | s [] := by simp; exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩ | s (a::t) := by simp [mem_tails s t]; exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from ⟨λo, match s, t, o with | ._, t, or.inl rfl := suffix_refl _ | s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩ end, λe, match s, t, e with | ._, t, ⟨[], rfl⟩ := or.inl rfl | s, t, ⟨b::l, he⟩ := list.no_confusion he (λab lt, or.inr ⟨l, lt⟩) end⟩ instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂) | [] l₂ := is_true ⟨[], l₂, rfl⟩ | (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $ append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h | l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $ by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm; exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩ /- sublists -/ def sublists'_aux : list α → (list α → list β) → list (list β) → list (list β) | [] f r := f [] :: r | (a::l) f r := sublists'_aux l f (sublists'_aux l (f ∘ cons a) r) /-- `sublists' l` is the list of all (non-contiguous) sublists of `l`. It differs from `sublists` only in the order of appearance of the sublists; `sublists'` uses the first element of the list as the MSB, `sublists` uses the first element of the list as the LSB. `sublists' [1, 2, 3] = [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]` -/ def sublists' (l : list α) : list (list α) := sublists'_aux l id [] @[simp] theorem sublists'_nil : sublists' (@nil α) = [[]] := rfl @[simp] theorem sublists'_singleton (a : α) : sublists' [a] = [[], [a]] := rfl theorem map_sublists'_aux (g : list β → list γ) (l : list α) (f r) : map g (sublists'_aux l f r) = sublists'_aux l (g ∘ f) (map g r) := by induction l generalizing f r; simp! * theorem sublists'_aux_append (r' : list (list β)) (l : list α) (f r) : sublists'_aux l f (r ++ r') = sublists'_aux l f r ++ r' := by induction l generalizing f r; simp! * theorem sublists'_aux_eq_sublists' (l f r) : @sublists'_aux α β l f r = map f (sublists' l) ++ r := by rw [sublists', map_sublists'_aux, ← sublists'_aux_append]; refl @[simp] theorem sublists'_cons (a : α) (l : list α) : sublists' (a :: l) = sublists' l ++ map (cons a) (sublists' l) := by rw [sublists', sublists'_aux]; simp [sublists'_aux_eq_sublists'] @[simp] theorem mem_sublists' {s t : list α} : s ∈ sublists' t ↔ s <+ t := begin induction t with a t IH generalizing s; simp, { exact ⟨λ h, by rw h, eq_nil_of_sublist_nil⟩ }, split; intro h, rcases h with h | ⟨s, h, rfl⟩, { exact sublist_cons_of_sublist _ (IH.1 h) }, { exact cons_sublist_cons _ (IH.1 h) }, { cases h with _ _ _ h s _ _ h, { exact or.inl (IH.2 h) }, { exact or.inr ⟨s, IH.2 h, rfl⟩ } } end @[simp] theorem length_sublists' : ∀ l : list α, length (sublists' l) = 2 ^ length l | [] := rfl | (a::l) := by simp [-add_comm, *]; rw [← two_mul, mul_comm]; refl def sublists_aux : list α → (list α → list β → list β) → list β | [] f := [] | (a::l) f := f [a] (sublists_aux l (λys r, f ys (f (a :: ys) r))) /-- `sublists l` is the list of all (non-contiguous) sublists of `l`. `sublists [1, 2, 3] = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]` -/ def sublists (l : list α) : list (list α) := [] :: sublists_aux l cons @[simp] theorem sublists_nil : sublists (@nil α) = [[]] := rfl @[simp] theorem sublists_singleton (a : α) : sublists [a] = [[], [a]] := rfl def sublists_aux₁ : list α → (list α → list β) → list β | [] f := [] | (a::l) f := f [a] ++ sublists_aux₁ l (λys, f ys ++ f (a :: ys)) theorem sublists_aux₁_eq_sublists_aux : ∀ l (f : list α → list β), sublists_aux₁ l f = sublists_aux l (λ ys r, f ys ++ r) | [] f := rfl | (a::l) f := by rw [sublists_aux₁, sublists_aux]; simp * theorem sublists_aux_cons_eq_sublists_aux₁ (l : list α) : sublists_aux l cons = sublists_aux₁ l (λ x, [x]) := by rw [sublists_aux₁_eq_sublists_aux]; refl theorem sublists_aux_eq_foldr.aux {a : α} {l : list α} (IH₁ : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons)) (IH₂ : ∀ (f : list α → list (list α) → list (list α)), sublists_aux l f = foldr f [] (sublists_aux l cons)) (f : list α → list β → list β) : sublists_aux (a::l) f = foldr f [] (sublists_aux (a::l) cons) := begin simp [sublists_aux], rw [IH₂, IH₁], congr_n 1, induction sublists_aux l cons with _ _ ih; simp * end theorem sublists_aux_eq_foldr (l : list α) : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons) := suffices _ ∧ ∀ f : list α → list (list α) → list (list α), sublists_aux l f = foldr f [] (sublists_aux l cons), from this.1, begin induction l with a l IH, {split; intro; refl}, exact ⟨sublists_aux_eq_foldr.aux IH.1 IH.2, sublists_aux_eq_foldr.aux IH.2 IH.2⟩ end theorem sublists_aux_cons_cons (l : list α) (a : α) : sublists_aux (a::l) cons = [a] :: foldr (λys r, ys :: (a :: ys) :: r) [] (sublists_aux l cons) := by rw [← sublists_aux_eq_foldr]; refl theorem sublists_aux₁_append : ∀ (l₁ l₂ : list α) (f : list α → list β), sublists_aux₁ (l₁ ++ l₂) f = sublists_aux₁ l₁ f ++ sublists_aux₁ l₂ (λ x, f x ++ sublists_aux₁ l₁ (f ∘ (++ x))) | [] l₂ f := by simp [sublists_aux₁] | (a::l₁) l₂ f := by simp [sublists_aux₁]; rw [sublists_aux₁_append]; simp theorem sublists_aux₁_concat (l : list α) (a : α) (f : list α → list β) : sublists_aux₁ (l ++ [a]) f = sublists_aux₁ l f ++ f [a] ++ sublists_aux₁ l (λ x, f (x ++ [a])) := by simp [sublists_aux₁_append, sublists_aux₁] theorem sublists_aux₁_bind : ∀ (l : list α) (f : list α → list β) (g : β → list γ), (sublists_aux₁ l f).bind g = sublists_aux₁ l (λ x, (f x).bind g) | [] f g := by simp [sublists_aux₁] | (a::l) f g := by simp [sublists_aux₁]; rw [sublists_aux₁_bind]; simp theorem sublists_aux_cons_append (l₁ l₂ : list α) : sublists_aux (l₁ ++ l₂) cons = sublists_aux l₁ cons ++ (do x ← sublists_aux l₂ cons, (++ x) <$> sublists l₁) := begin simp [sublists, sublists_aux_cons_eq_sublists_aux₁], rw [sublists_aux₁_append, sublists_aux₁_bind], congr, funext x, simp, rw [← bind_ret_eq_map, sublists_aux₁_bind], simp [list.ret] end theorem sublists_append (l₁ l₂ : list α) : sublists (l₁ ++ l₂) = (do x ← sublists l₂, (++ x) <$> sublists l₁) := by simp [sublists_aux_cons_append, sublists, map_id'] @[simp] theorem sublists_concat (l : list α) (a : α) : sublists (l ++ [a]) = sublists l ++ map (λ x, x ++ [a]) (sublists l) := by simp [sublists_append]; rw [sublists, sublists_aux_cons_eq_sublists_aux₁]; simp [map_id', sublists_aux₁] theorem sublists_reverse (l : list α) : sublists (reverse l) = map reverse (sublists' l) := by induction l; simp [(∘), *] theorem sublists_eq_sublists' (l : list α) : sublists l = map reverse (sublists' (reverse l)) := by rw [← sublists_reverse, reverse_reverse] theorem sublists'_reverse (l : list α) : sublists' (reverse l) = map reverse (sublists l) := by simp [sublists_eq_sublists', map_id'] theorem sublists'_eq_sublists (l : list α) : sublists' l = map reverse (sublists (reverse l)) := by rw [← sublists'_reverse, reverse_reverse] theorem sublists_aux_ne_nil : ∀ (l : list α), [] ∉ sublists_aux l cons | [] := id | (a::l) := begin rw [sublists_aux_cons_cons], refine not_mem_cons_of_ne_of_not_mem (cons_ne_nil _ _).symm _, have := sublists_aux_ne_nil l, revert this, induction sublists_aux l cons; intro; simp [not_or_distrib], exact ⟨ne_of_not_mem_cons this, ih (not_mem_of_not_mem_cons this)⟩ end @[simp] theorem mem_sublists {s t : list α} : s ∈ sublists t ↔ s <+ t := by rw [← reverse_sublist_iff, ← mem_sublists', sublists'_reverse, mem_map_of_inj reverse_injective] @[simp] theorem length_sublists (l : list α) : length (sublists l) = 2 ^ length l := by simp [sublists_eq_sublists', length_sublists'] theorem map_ret_sublist_sublists (l : list α) : map list.ret l <+ sublists l := reverse_rec_on l (nil_sublist _) $ λ l a IH, by simp; exact ((append_sublist_append_left _).2 (singleton_sublist.2 $ mem_map.2 ⟨[], by simp [list.ret]⟩)).trans ((append_sublist_append_right _).2 IH) /- transpose -/ def transpose_aux : list α → list (list α) → list (list α) | [] ls := ls | (a::i) [] := [a] :: transpose_aux i [] | (a::i) (l::ls) := (a::l) :: transpose_aux i ls /-- transpose of a list of lists, treated as a matrix. `transpose [[1, 2], [3, 4], [5, 6]] = [[1, 3, 5], [2, 4, 6]]` -/ def transpose : list (list α) → list (list α) | [] := [] | (l::ls) := transpose_aux l (transpose ls) /- permutations -/ section permutations def permutations_aux2 (t : α) (ts : list α) (r : list β) : list α → (list α → β) → list α × list β | [] f := (ts, r) | (y::ys) f := let (us, zs) := permutations_aux2 ys (λx : list α, f (y::x)) in (y :: us, f (t :: y :: us) :: zs) private def meas : (Σ'_:list α, list α) → ℕ × ℕ | ⟨l, i⟩ := (length l + length i, length l) local infix ` ≺ `:50 := inv_image (prod.lex (<) (<)) meas @[elab_as_eliminator] def permutations_aux.rec {C : list α → list α → Sort v} (H0 : ∀ is, C [] is) (H1 : ∀ t ts is, C ts (t::is) → C is [] → C (t::ts) is) : ∀ l₁ l₂, C l₁ l₂ | [] is := H0 is | (t::ts) is := have h1 : ⟨ts, t :: is⟩ ≺ ⟨t :: ts, is⟩, from show prod.lex _ _ (succ (length ts + length is), length ts) (succ (length ts) + length is, length (t :: ts)), by rw nat.succ_add; exact prod.lex.right _ _ (lt_succ_self _), have h2 : ⟨is, []⟩ ≺ ⟨t :: ts, is⟩, from prod.lex.left _ _ _ (lt_add_of_pos_left _ (succ_pos _)), H1 t ts is (permutations_aux.rec ts (t::is)) (permutations_aux.rec is []) using_well_founded { dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨(≺), @inv_image.wf _ _ _ meas (prod.lex_wf lt_wf lt_wf)⟩] } def permutations_aux : list α → list α → list (list α) := @@permutations_aux.rec (λ _ _, list (list α)) (λ is, []) (λ t ts is IH1 IH2, foldr (λy r, (permutations_aux2 t ts r y id).2) IH1 (is :: IH2)) /-- List of all permutations of `l`. permutations [1, 2, 3] = [[1, 2, 3], [2, 1, 3], [3, 2, 1], [2, 3, 1], [3, 1, 2], [1, 3, 2]] -/ def permutations (l : list α) : list (list α) := l :: permutations_aux l [] @[simp] theorem permutations_aux_nil (is : list α) : permutations_aux [] is = [] := by simp [permutations_aux, permutations_aux.rec] @[simp] theorem permutations_aux_cons (t : α) (ts is : list α) : permutations_aux (t :: ts) is = foldr (λy r, (permutations_aux2 t ts r y id).2) (permutations_aux ts (t::is)) (permutations is) := by simp [permutations_aux, permutations_aux.rec, permutations] end permutations /- insert -/ section insert variable [decidable_eq α] @[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl @[simp] theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l := by simp [insert.def, h] @[simp] theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l := by simp [insert.def, h] @[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l := begin by_cases h' : b ∈ l; simp [h'], apply (or_iff_right_of_imp _).symm, exact λ e, e.symm ▸ h' end @[simp] theorem suffix_insert (a : α) (l : list α) : l <:+ insert a l := by by_cases a ∈ l; simp * @[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l := mem_insert_iff.2 (or.inl rfl) @[simp] theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l := mem_insert_iff.2 (or.inr h) theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l := mem_insert_iff.1 h @[simp] theorem length_insert_of_mem {a : α} [decidable_eq α] {l : list α} (h : a ∈ l) : length (insert a l) = length l := by simp [h] @[simp] theorem length_insert_of_not_mem {a : α} [decidable_eq α] {l : list α} (h : a ∉ l) : length (insert a l) = length l + 1 := by simp [h] end insert /- erase -/ section erase variable [decidable_eq α] @[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl theorem erase_cons (a b : α) (l : list α) : (b :: l).erase a = if b = a then l else b :: l.erase a := rfl @[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l := by simp [erase_cons] @[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) : (b::l).erase a = b :: l.erase a := by simp [erase_cons, h] @[simp] theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l := by induction l with _ _ ih; [refl, simp [(ne_of_not_mem_cons h).symm, ih (not_mem_of_not_mem_cons h)]] theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) : ∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ := by induction l with b l ih; [cases h, { simp at h, by_cases e : b = a, { subst b, exact ⟨[], l, not_mem_nil _, rfl, by simp⟩ }, { exact let ⟨l₁, l₂, h₁, h₂, h₃⟩ := ih (h.resolve_left (ne.symm e)) in ⟨b::l₁, l₂, not_mem_cons_of_ne_of_not_mem (ne.symm e) h₁, by rw h₂; refl, by simp [e, h₃]⟩ } }] @[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) : length (l.erase a) = pred (length l) := match l, l.erase a, exists_erase_eq h with | ._, ._, ⟨l₁, l₂, _, rfl, rfl⟩ := by simp [-add_comm]; refl end theorem erase_append_left {a : α} : ∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erase a = l₁.erase a ++ l₂ | (x::xs) l₂ h := begin by_cases h' : x = a; simp [h'], rw erase_append_left l₂ (mem_of_ne_of_mem (ne.symm h') h) end theorem erase_append_right {a : α} : ∀ {l₁ : list α} (l₂), a ∉ l₁ → (l₁++l₂).erase a = l₁ ++ l₂.erase a | [] l₂ h := rfl | (x::xs) l₂ h := by simp [*, (ne_of_not_mem_cons h).symm, (not_mem_of_not_mem_cons h)] theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l := if h : a ∈ l then match l, l.erase a, exists_erase_eq h with | ._, ._, ⟨l₁, l₂, _, rfl, rfl⟩ := by simp end else by simp [h] theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l := subset_of_sublist (erase_sublist a l) theorem erase_sublist_erase (a : α) : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → l₁.erase a <+ l₂.erase a | ._ ._ sublist.slnil := sublist.slnil | ._ ._ (sublist.cons l₁ l₂ b s) := if h : b = a then by rw [h, erase_cons_head]; exact (erase_sublist _ _).trans s else by rw erase_cons_tail _ h; exact (erase_sublist_erase s).cons _ _ _ | ._ ._ (sublist.cons2 l₁ l₂ b s) := if h : b = a then by rw [h, erase_cons_head, erase_cons_head]; exact s else by rw [erase_cons_tail _ h, erase_cons_tail _ h]; exact (erase_sublist_erase s).cons2 _ _ _ theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l := @erase_subset _ _ _ _ _ @[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l := ⟨mem_of_mem_erase, λ al, if h : b ∈ l then match l, l.erase b, exists_erase_eq h, al with | ._, ._, ⟨l₁, l₂, _, rfl, rfl⟩, al := by simpa [ab] using al end else by simp [h, al]⟩ theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a := if ab : a = b then by simp [ab] else if ha : a ∈ l then if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with | ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb := if h₁ : b ∈ l₁ then by rw [erase_append_left _ h₁, erase_append_left _ h₁, erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head] else by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha', erase_cons_tail _ ab, erase_cons_head] end else by simp [hb, mt mem_of_mem_erase hb] else by simp [ha, mt mem_of_mem_erase ha] end erase /- diff -/ section diff variable [decidable_eq α] @[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl @[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ := by by_cases a ∈ l₁; simp [list.diff, h] theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂ | l₁ [] := rfl | l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _) @[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ := by simp [diff_eq_foldl] end diff /- zip & unzip -/ @[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl @[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl @[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] := by cases l; refl @[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl @[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = (a :: (unzip l).1, b :: (unzip l).2) := by rw unzip; cases unzip l; refl theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := by simp [zip_unzip l] /- enum -/ theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l | n [] := rfl | n (a::l) := congr_arg nat.succ (length_enum_from _ _) theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _ @[simp] theorem enum_from_nth : ∀ n (l : list α) m, nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m | n [] m := rfl | n (a :: l) 0 := rfl | n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $ by rw [add_right_comm]; refl @[simp] theorem enum_nth : ∀ (l : list α) n, nth (enum l) n = (λ a, (n, a)) <$> nth l n := by simp [enum] @[simp] theorem enum_from_map_snd : ∀ n (l : list α), map prod.snd (enum_from n l) = l | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _) @[simp] theorem enum_map_snd : ∀ (l : list α), map prod.snd (enum l) = l := enum_from_map_snd _ /- product -/ /-- `product l₁ l₂` is the list of pairs `(a, b)` where `a ∈ l₁` and `b ∈ l₂`. product [1, 2] [5, 6] = [(1, 5), (1, 6), (2, 5), (2, 6)] -/ def product (l₁ : list α) (l₂ : list β) : list (α × β) := l₁.bind $ λ a, l₂.map $ prod.mk a @[simp] theorem nil_product (l : list β) : product (@nil α) l = [] := rfl @[simp] theorem product_cons (a : α) (l₁ : list α) (l₂ : list β) : product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl @[simp] theorem product_nil : ∀ (l : list α), product l (@nil β) = [] | [] := rfl | (a::l) := by rw [product_cons, product_nil]; refl @[simp] theorem mem_product {l₁ : list α} {l₂ : list β} {a : α} {b : β} : (a, b) ∈ product l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ := by simp [product, and.left_comm] theorem length_product (l₁ : list α) (l₂ : list β) : length (product l₁ l₂) = length l₁ * length l₂ := by induction l₁ with x l₁ IH; simp [*, right_distrib] /- sigma -/ section variable {σ : α → Type*} /-- `sigma l₁ l₂` is the list of dependent pairs `(a, b)` where `a ∈ l₁` and `b ∈ l₂ a`. sigma [1, 2] (λ_, [5, 6]) = [(1, 5), (1, 6), (2, 5), (2, 6)] -/ def sigma (l₁ : list α) (l₂ : Π a, list (σ a)) : list (Σ a, σ a) := l₁.bind $ λ a, (l₂ a).map $ sigma.mk a @[simp] theorem nil_sigma (l : Π a, list (σ a)) : (@nil α).sigma l = [] := rfl @[simp] theorem sigma_cons (a : α) (l₁ : list α) (l₂ : Π a, list (σ a)) : (a::l₁).sigma l₂ = map (sigma.mk a) (l₂ a) ++ l₁.sigma l₂ := rfl @[simp] theorem sigma_nil : ∀ (l : list α), l.sigma (λ a, @nil (σ a)) = [] | [] := rfl | (a::l) := by rw [sigma_cons, sigma_nil]; refl @[simp] theorem mem_sigma {l₁ : list α} {l₂ : Π a, list (σ a)} {a : α} {b : σ a} : sigma.mk a b ∈ l₁.sigma l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ a := by simp [sigma, and.left_comm] theorem length_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) : length (sigma l₁ l₂) = (l₁.map (λ a, length (l₂ a))).sum := by induction l₁ with x l₁ IH; simp * end /- disjoint -/ section disjoint /-- `disjoint l₁ l₂` means that `l₁` and `l₂` have no elements in common. -/ def disjoint (l₁ l₂ : list α) : Prop := ∀ ⦃a⦄, a ∈ l₁ → a ∈ l₂ → false theorem disjoint.symm {l₁ l₂ : list α} (d : disjoint l₁ l₂) : disjoint l₂ l₁ | a i₂ i₁ := d i₁ i₂ @[simp] theorem disjoint_comm {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ disjoint l₂ l₁ := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₁ → a ∉ l₂ := iff.rfl theorem disjoint_right {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₂ → a ∉ l₁ := disjoint_comm theorem disjoint_iff_ne {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ a ∈ l₁, ∀ b ∈ l₂, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {l₁ l₂ l : list α} (ss : l₁ ⊆ l) (d : disjoint l l₂) : disjoint l₁ l₂ | x m₁ := d (ss m₁) theorem disjoint_of_subset_right {l₁ l₂ l : list α} (ss : l₂ ⊆ l) (d : disjoint l₁ l) : disjoint l₁ l₂ | x m m₁ := d m (ss m₁) theorem disjoint_of_disjoint_cons_left {a : α} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ := disjoint_of_subset_left (list.subset_cons _ _) theorem disjoint_of_disjoint_cons_right {a : α} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ := disjoint_of_subset_right (list.subset_cons _ _) @[simp] theorem disjoint_nil_left (l : list α) : disjoint [] l | a := (not_mem_nil a).elim @[simp] theorem singleton_disjoint {l : list α} {a : α} : disjoint [a] l ↔ a ∉ l := by simp [disjoint]; refl @[simp] theorem disjoint_singleton {l : list α} {a : α} : disjoint l [a] ↔ a ∉ l := by rw disjoint_comm; simp @[simp] theorem disjoint_append_left {l₁ l₂ l : list α} : disjoint (l₁++l₂) l ↔ disjoint l₁ l ∧ disjoint l₂ l := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_append_right {l₁ l₂ l : list α} : disjoint l (l₁++l₂) ↔ disjoint l l₁ ∧ disjoint l l₂ := disjoint_comm.trans $ by simp [disjoint_append_left] @[simp] theorem disjoint_cons_left {a : α} {l₁ l₂ : list α} : disjoint (a::l₁) l₂ ↔ a ∉ l₂ ∧ disjoint l₁ l₂ := (@disjoint_append_left _ [a] l₁ l₂).trans $ by simp @[simp] theorem disjoint_cons_right {a : α} {l₁ l₂ : list α} : disjoint l₁ (a::l₂) ↔ a ∉ l₁ ∧ disjoint l₁ l₂ := disjoint_comm.trans $ by simp [disjoint_cons_left] theorem disjoint_of_disjoint_append_left_left {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) : disjoint l₁ l := (disjoint_append_left.1 d).1 theorem disjoint_of_disjoint_append_left_right {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) : disjoint l₂ l := (disjoint_append_left.1 d).2 theorem disjoint_of_disjoint_append_right_left {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) : disjoint l l₁ := (disjoint_append_right.1 d).1 theorem disjoint_of_disjoint_append_right_right {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) : disjoint l l₂ := (disjoint_append_right.1 d).2 end disjoint /- union -/ section union variable [decidable_eq α] @[simp] theorem nil_union (l : list α) : [] ∪ l = l := rfl @[simp] theorem cons_union (l₁ l₂ : list α) (a : α) : a :: l₁ ∪ l₂ = insert a (l₁ ∪ l₂) := rfl @[simp] theorem mem_union {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∪ l₂ ↔ a ∈ l₁ ∨ a ∈ l₂ := by induction l₁; simp [*, or_assoc] theorem mem_union_left {a : α} {l₁ : list α} (h : a ∈ l₁) (l₂ : list α) : a ∈ l₁ ∪ l₂ := mem_union.2 (or.inl h) theorem mem_union_right {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : a ∈ l₁ ∪ l₂ := mem_union.2 (or.inr h) theorem sublist_suffix_of_union : ∀ l₁ l₂ : list α, ∃ t, t <+ l₁ ∧ t ++ l₂ = l₁ ∪ l₂ | [] l₂ := ⟨[], by refl, rfl⟩ | (a::l₁) l₂ := let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in by simp [e.symm]; by_cases h : a ∈ t ++ l₂; [existsi t, existsi a::t]; simp [h]; [apply sublist_cons_of_sublist _ s, apply cons_sublist_cons _ s] theorem suffix_union_right (l₁ l₂ : list α) : l₂ <:+ l₁ ∪ l₂ := (sublist_suffix_of_union l₁ l₂).imp (λ a, and.right) theorem union_sublist_append (l₁ l₂ : list α) : l₁ ∪ l₂ <+ l₁ ++ l₂ := let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in e ▸ (append_sublist_append_right _).2 s theorem forall_mem_union {p : α → Prop} {l₁ l₂ : list α} : (∀ x ∈ l₁ ∪ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) := by simp [or_imp_distrib, forall_and_distrib] theorem forall_mem_of_forall_mem_union_left {p : α → Prop} {l₁ l₂ : list α} (h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₁, p x := (forall_mem_union.1 h).1 theorem forall_mem_of_forall_mem_union_right {p : α → Prop} {l₁ l₂ : list α} (h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₂, p x := (forall_mem_union.1 h).2 end union /- inter -/ section inter variable [decidable_eq α] @[simp] theorem inter_nil (l : list α) : [] ∩ l = [] := rfl @[simp] theorem inter_cons_of_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : (a::l₁) ∩ l₂ = a :: (l₁ ∩ l₂) := if_pos h @[simp] theorem inter_cons_of_not_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∉ l₂) : (a::l₁) ∩ l₂ = l₁ ∩ l₂ := if_neg h theorem mem_of_mem_inter_left {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₁ := mem_of_mem_filter theorem mem_of_mem_inter_right {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₂ := of_mem_filter theorem mem_inter_of_mem_of_mem {l₁ l₂ : list α} {a : α} : a ∈ l₁ → a ∈ l₂ → a ∈ l₁ ∩ l₂ := mem_filter_of_mem @[simp] theorem mem_inter {a : α} {l₁ l₂ : list α} : a ∈ l₁ ∩ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ := mem_filter theorem inter_subset_left (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₁ := filter_subset _ theorem inter_subset_right (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₂ := λ a, mem_of_mem_inter_right theorem subset_inter {l l₁ l₂ : list α} (h₁ : l ⊆ l₁) (h₂ : l ⊆ l₂) : l ⊆ l₁ ∩ l₂ := λ a h, mem_inter.2 ⟨h₁ h, h₂ h⟩ theorem inter_eq_nil_iff_disjoint {l₁ l₂ : list α} : l₁ ∩ l₂ = [] ↔ disjoint l₁ l₂ := by simp [eq_nil_iff_forall_not_mem]; refl theorem forall_mem_inter_of_forall_left {p : α → Prop} {l₁ : list α} (h : ∀ x ∈ l₁, p x) (l₂ : list α) : ∀ x, x ∈ l₁ ∩ l₂ → p x := ball.imp_left (λ x, mem_of_mem_inter_left) h theorem forall_mem_inter_of_forall_right {p : α → Prop} (l₁ : list α) {l₂ : list α} (h : ∀ x ∈ l₂, p x) : ∀ x, x ∈ l₁ ∩ l₂ → p x := ball.imp_left (λ x, mem_of_mem_inter_right) h end inter /- bag_inter -/ section bag_inter variable [decidable_eq α] @[simp] theorem nil_bag_inter (l : list α) : [].bag_inter l = [] := by cases l; refl @[simp] theorem bag_inter_nil (l : list α) : l.bag_inter [] = [] := by cases l; refl @[simp] theorem cons_bag_inter_of_pos {a} (l₁ : list α) {l₂} (h : a ∈ l₂) : (a :: l₁).bag_inter l₂ = a :: l₁.bag_inter (l₂.erase a) := by cases l₂; exact if_pos h @[simp] theorem cons_bag_inter_of_neg {a} (l₁ : list α) {l₂} (h : a ∉ l₂) : (a :: l₁).bag_inter l₂ = l₁.bag_inter l₂ := by cases l₂; simp [h, list.bag_inter] theorem mem_bag_inter {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁.bag_inter l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ | [] l₂ := by simp | (b::l₁) l₂ := by by_cases b ∈ l₂; simp [*, and_or_distrib_left]; by_cases ba : a = b; simp * theorem bag_inter_sublist_left : ∀ l₁ l₂ : list α, l₁.bag_inter l₂ <+ l₁ | [] l₂ := by simp [nil_sublist] | (b::l₁) l₂ := begin by_cases b ∈ l₂; simp [h], { apply cons_sublist_cons, apply bag_inter_sublist_left }, { apply sublist_cons_of_sublist, apply bag_inter_sublist_left } end end bag_inter /- pairwise relation (generalized no duplicate) -/ section pairwise variable (R : α → α → Prop) /-- `pairwise R l` means that all the elements with earlier indexes are `R`-related to all the elements with later indexes. pairwise R [1, 2, 3] ↔ R 1 2 ∧ R 1 3 ∧ R 2 3 For example if `R = (≠)` then it asserts `l` has no duplicates, and if `R = (<)` then it asserts that `l` is (strictly) sorted. -/ inductive pairwise : list α → Prop | nil : pairwise [] | cons : ∀ {a : α} {l : list α}, (∀ a' ∈ l, R a a') → pairwise l → pairwise (a::l) attribute [simp] pairwise.nil variable {R} @[simp] theorem pairwise_cons {a : α} {l : list α} : pairwise R (a::l) ↔ (∀ a' ∈ l, R a a') ∧ pairwise R l := ⟨λ p, by cases p with a l n p; exact ⟨n, p⟩, λ ⟨n, p⟩, p.cons n⟩ theorem rel_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : ∀ {a'}, a' ∈ l → R a a' := (pairwise_cons.1 p).1 theorem pairwise_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : pairwise R l := (pairwise_cons.1 p).2 theorem pairwise.imp_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l := begin induction p with a l r p IH generalizing H; constructor, { exact ball.imp_right (λ x h, H (mem_cons_self _ _) (mem_cons_of_mem _ h)) r }, { exact IH (λ a b m m', H (mem_cons_of_mem _ m) (mem_cons_of_mem _ m')) } end theorem pairwise.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {l : list α} : pairwise R l → pairwise S l := pairwise.imp_of_mem (λ a b _ _, H a b) theorem pairwise.iff_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l := ⟨pairwise.imp_of_mem (λ a b m m', (H m m').1), pairwise.imp_of_mem (λ a b m m', (H m m').2)⟩ theorem pairwise.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : list α} : pairwise R l ↔ pairwise S l := pairwise.iff_of_mem (λ a b _ _, H a b) theorem pairwise_of_forall {l : list α} (H : ∀ x y, R x y) : pairwise R l := by induction l; simp * theorem pairwise.and_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l := pairwise.iff_of_mem (by simp {contextual := tt}) theorem pairwise.imp_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l → y ∈ l → R x y) l := pairwise.iff_of_mem (by simp {contextual := tt}) theorem pairwise_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁ | ._ ._ sublist.slnil h := h | ._ ._ (sublist.cons l₁ l₂ a s) (pairwise.cons i n) := pairwise_of_sublist s n | ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i n) := (pairwise_of_sublist s n).cons (ball.imp_left (subset_of_sublist s) i) theorem pairwise_singleton (R) (a : α) : pairwise R [a] := by simp theorem pairwise_pair {a b : α} : pairwise R [a, b] ↔ R a b := by simp theorem pairwise_append {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ x ∈ l₁, ∀ y ∈ l₂, R x y := by induction l₁ with x l₁ IH; simp [*, or_imp_distrib, forall_and_distrib, and_assoc, and.left_comm] theorem pairwise_app_comm (s : symmetric R) {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R (l₂++l₁) := have ∀ l₁ l₂ : list α, (∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) → (∀ (x : α), x ∈ l₂ → ∀ (y : α), y ∈ l₁ → R x y), from λ l₁ l₂ a x xm y ym, s (a y ym x xm), by simp [pairwise_append, and.left_comm]; rw iff.intro (this l₁ l₂) (this l₂ l₁) theorem pairwise_middle (s : symmetric R) {a : α} {l₁ l₂ : list α} : pairwise R (l₁ ++ a::l₂) ↔ pairwise R (a::(l₁++l₂)) := show pairwise R (l₁ ++ ([a] ++ l₂)) ↔ pairwise R ([a] ++ l₁ ++ l₂), by rw [← append_assoc, pairwise_append, @pairwise_append _ _ ([a] ++ l₁), pairwise_app_comm s]; simp only [mem_append, or_comm] theorem pairwise_map (f : β → α) : ∀ {l : list β}, pairwise R (map f l) ↔ pairwise (λ a b : β, R (f a) (f b)) l | [] := by simp | (b::l) := have (∀ a b', b' ∈ l → f b' = a → R (f b) a) ↔ ∀ (b' : β), b' ∈ l → R (f b) (f b'), from forall_swap.trans $ forall_congr $ λ a, forall_swap.trans $ by simp, by simp *; rw this theorem pairwise_of_pairwise_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α} (p : pairwise S (map f l)) : pairwise R l := ((pairwise_map f).1 p).imp H theorem pairwise_map_of_pairwise {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α} (p : pairwise R l) : pairwise S (map f l) := (pairwise_map f).2 $ p.imp H theorem pairwise_filter_map (f : β → option α) {l : list β} : pairwise R (filter_map f l) ↔ pairwise (λ a a' : β, ∀ (b ∈ f a) (b' ∈ f a'), R b b') l := let S (a a' : β) := ∀ (b ∈ f a) (b' ∈ f a'), R b b' in begin simp, induction l with a l IH; simp, cases e : f a with b; simp [e, IH], rw [filter_map_cons_some _ _ _ e], simp [IH], show (∀ (a' : α) (x : β), x ∈ l → f x = some a' → R b a') ∧ pairwise S l ↔ (∀ (a' : β), a' ∈ l → ∀ (b' : α), f a' = some b' → R b b') ∧ pairwise S l, from and_congr ⟨λ h b mb a ma, h a b mb ma, λ h a b mb ma, h b mb a ma⟩ iff.rfl end theorem pairwise_filter_map_of_pairwise {S : β → β → Prop} (f : α → option β) (H : ∀ (a a' : α), R a a' → ∀ (b ∈ f a) (b' ∈ f a'), S b b') {l : list α} (p : pairwise R l) : pairwise S (filter_map f l) := (pairwise_filter_map _).2 $ p.imp H theorem pairwise_filter (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R (filter p l) ↔ pairwise (λ x y, p x → p y → R x y) l := begin rw [← filter_map_eq_filter, pairwise_filter_map], apply pairwise.iff, simp end theorem pairwise_filter_of_pairwise (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R l → pairwise R (filter p l) := pairwise_of_sublist (filter_sublist _) theorem pairwise_join {L : list (list α)} : pairwise R (join L) ↔ (∀ l ∈ L, pairwise R l) ∧ pairwise (λ l₁ l₂, ∀ (x ∈ l₁) (y ∈ l₂), R x y) L := begin induction L with l L IH, {simp}, have : (∀ (x : α), x ∈ l → ∀ (y : α) (x_1 : list α), x_1 ∈ L → y ∈ x_1 → R x y) ↔ ∀ (a' : list α), a' ∈ L → ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ a' → R x y := ⟨λ h a b c d e, h c d e a b, λ h c d e a b, h a b c d e⟩, simp [pairwise_append, IH, this], simp [and_assoc, and_comm, and.left_comm], end @[simp] theorem pairwise_reverse : ∀ {R} {l : list α}, pairwise R (reverse l) ↔ pairwise (λ x y, R y x) l := suffices ∀ {R l}, @pairwise α R l → pairwise (λ x y, R y x) (reverse l), from λ R l, ⟨λ p, reverse_reverse l ▸ this p, this⟩, λ R l p, by induction p with a l h p IH; [simp, simpa [pairwise_append, IH] using h] theorem pairwise_iff_nth_le {R} : ∀ {l : list α}, pairwise R l ↔ ∀ i j (h₁ : j < length l) (h₂ : i < j), R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁) | [] := by simp; exact λ i j h, (not_lt_zero j).elim h | (a::l) := begin rw [pairwise_cons, pairwise_iff_nth_le], refine ⟨λ H i j h₁ h₂, _, λ H, ⟨λ a' m, _, λ i j h₁ h₂, H _ _ (succ_lt_succ h₁) (succ_lt_succ h₂)⟩⟩, { cases j with j, {exact (not_lt_zero _).elim h₂}, cases i with i, { apply H.1, simp [nth_le_mem] }, { exact H.2 _ _ (lt_of_succ_lt_succ h₁) (lt_of_succ_lt_succ h₂) } }, { rcases nth_le_of_mem m with ⟨n, h, rfl⟩, exact H _ _ (succ_lt_succ h) (succ_pos _) } end inductive lex (R : α → α → Prop) : list α → list α → Prop | nil {} (a l) : lex [] (a::l) | cons {} (a) {l l'} : lex l l' → lex (a::l) (a::l') | rel {a a'} (l l') : R a a' → lex (a::l) (a'::l') theorem lex_append_right (R : α → α → Prop) : ∀ {s₁ s₂} t, lex R s₁ s₂ → lex R s₁ (s₂ ++ t) | _ _ t (lex.nil a l) := lex.nil _ _ | _ _ t (lex.cons a h) := lex.cons _ (lex_append_right _ h) | _ _ t (lex.rel _ _ r) := lex.rel _ _ r theorem lex_append_left (R : α → α → Prop) {t₁ t₂} (h : lex R t₁ t₂) : ∀ s, lex R (s ++ t₁) (s ++ t₂) | [] := h | (a::l) := lex.cons _ (lex_append_left l) theorem lex.imp {R S : α → α → Prop} (H : ∀ a b, R a b → S a b) : ∀ l₁ l₂, lex R l₁ l₂ → lex S l₁ l₂ | _ _ (lex.nil a l) := lex.nil _ _ | _ _ (lex.cons a h) := lex.cons _ (lex.imp _ _ h) | _ _ (lex.rel _ _ r) := lex.rel _ _ (H _ _ r) theorem ne_of_lex_ne : ∀ {l₁ l₂ : list α}, lex (≠) l₁ l₂ → l₁ ≠ l₂ | _ _ (lex.cons a h) e := ne_of_lex_ne h (list.cons.inj e).2 | _ _ (lex.rel _ _ r) e := r (list.cons.inj e).1 theorem lex_ne_iff {l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ := ⟨ne_of_lex_ne, λ h, begin induction l₁ with a l₁ IH generalizing l₂; cases l₂ with b l₂, { contradiction }, { apply lex.nil }, { exact (not_lt_of_ge H).elim (succ_pos _) }, { cases classical.em (a = b) with ab ab, { subst b, apply lex.cons, exact IH (le_of_succ_le_succ H) (mt (congr_arg _) h) }, { exact lex.rel _ _ ab } } end⟩ theorem pairwise_sublists' {R} : ∀ {l : list α}, pairwise R l → pairwise (lex (swap R)) (sublists' l) | _ (pairwise.nil _) := pairwise_singleton _ _ | _ (@pairwise.cons _ _ a l H₁ H₂) := begin simp [pairwise_append, pairwise_map], have IH := pairwise_sublists' H₂, refine ⟨IH, IH.imp (λ l₁ l₂, lex.cons _), _⟩, intros l₁ sl₁ x l₂ sl₂ e, subst e, cases l₁ with b l₁, {constructor}, exact lex.rel _ _ (H₁ _ $ subset_of_sublist sl₁ $ mem_cons_self _ _) end theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) : pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) := by have := pairwise_sublists' (pairwise_reverse.2 H); rwa [sublists'_reverse, pairwise_map] at this variable [decidable_rel R] instance decidable_pairwise (l : list α) : decidable (pairwise R l) := by induction l; simp; resetI; apply_instance /- pairwise reduct -/ /-- `pw_filter R l` is a maximal sublist of `l` which is `pairwise R`. `pw_filter (≠)` is the erase duplicates function, and `pw_filter (<)` finds a maximal increasing subsequence in `l`. For example, pw_filter (<) [0, 1, 5, 2, 6, 3, 4] = [0, 1, 5, 6] -/ def pw_filter (R : α → α → Prop) [decidable_rel R] : list α → list α | [] := [] | (x :: xs) := let IH := pw_filter xs in if ∀ y ∈ IH, R x y then x :: IH else IH @[simp] theorem pw_filter_nil : pw_filter R [] = [] := rfl @[simp] theorem pw_filter_cons_of_pos {a : α} {l : list α} (h : ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = a :: pw_filter R l := if_pos h @[simp] theorem pw_filter_cons_of_neg {a : α} {l : list α} (h : ¬ ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = pw_filter R l := if_neg h theorem pw_filter_sublist : ∀ (l : list α), pw_filter R l <+ l | [] := nil_sublist _ | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h, { rw [pw_filter_cons_of_pos h], exact cons_sublist_cons _ (pw_filter_sublist l) }, { rw [pw_filter_cons_of_neg h], exact sublist_cons_of_sublist _ (pw_filter_sublist l) }, end theorem pw_filter_subset (l : list α) : pw_filter R l ⊆ l := subset_of_sublist (pw_filter_sublist _) theorem pairwise_pw_filter : ∀ (l : list α), pairwise R (pw_filter R l) | [] := pairwise.nil _ | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h, { rw [pw_filter_cons_of_pos h], exact pairwise_cons.2 ⟨h, pairwise_pw_filter l⟩ }, { rw [pw_filter_cons_of_neg h], exact pairwise_pw_filter l }, end theorem pw_filter_eq_self {l : list α} : pw_filter R l = l ↔ pairwise R l := ⟨λ e, e ▸ pairwise_pw_filter l, λ p, begin induction l with x l IH, {simp}, cases pairwise_cons.1 p with al p, rw [pw_filter_cons_of_pos (ball.imp_left (pw_filter_subset l) al), IH p], end⟩ theorem forall_mem_pw_filter (neg_trans : ∀ {x y z}, R x z → R x y ∨ R y z) (a : α) (l : list α) : (∀ b ∈ pw_filter R l, R a b) ↔ (∀ b ∈ l, R a b) := ⟨begin induction l with x l IH; simp *, by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h, { simp [pw_filter_cons_of_pos h], exact λ r H, ⟨r, IH H⟩ }, { rw [pw_filter_cons_of_neg h], refine λ H, ⟨_, IH H⟩, cases e : find (λ y, ¬ R x y) (pw_filter R l) with k, { refine h.elim (ball.imp_right _ (find_eq_none.1 e)), exact λ y _, not_not.1 }, { have := find_some e, exact (neg_trans (H k (find_mem e))).resolve_right this } } end, ball.imp_left (pw_filter_subset l)⟩ end pairwise /- chain relation (conjunction of R a b ∧ R b c ∧ R c d ...) -/ section chain variable (R : α → α → Prop) /-- `chain R a l` means that `R` holds between adjacent elements of `a::l`. `chain R a [b, c, d] ↔ R a b ∧ R b c ∧ R c d` -/ inductive chain : α → list α → Prop | nil (a : α) : chain a [] | cons : ∀ {a b : α} {l : list α}, R a b → chain b l → chain a (b::l) attribute [simp] chain.nil variable {R} @[simp] theorem chain_cons {a b : α} {l : list α} : chain R a (b::l) ↔ R a b ∧ chain R b l := ⟨λ p, by cases p with _ a b l n p; exact ⟨n, p⟩, λ ⟨n, p⟩, p.cons n⟩ theorem rel_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : R a b := (chain_cons.1 p).1 theorem chain_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : chain R b l := (chain_cons.1 p).2 theorem chain.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {a : α} {l : list α} (p : chain R a l) : chain S a l := by induction p with _ a b l r p IH; constructor; [exact H _ _ r, exact IH] theorem chain.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {a : α} {l : list α} : chain R a l ↔ chain S a l := ⟨chain.imp (λ a b, (H a b).1), chain.imp (λ a b, (H a b).2)⟩ theorem chain.iff_mem {S : α → α → Prop} {a : α} {l : list α} : chain R a l ↔ chain (λ x y, x ∈ a :: l ∧ y ∈ l ∧ R x y) a l := ⟨λ p, by induction p with _ a b l r p IH; constructor; [exact ⟨mem_cons_self _ _, mem_cons_self _ _, r⟩, exact IH.imp (λ a b ⟨am, bm, h⟩, ⟨mem_cons_of_mem _ am, mem_cons_of_mem _ bm, h⟩)], chain.imp (λ a b h, h.2.2)⟩ theorem chain_singleton {a b : α} : chain R a [b] ↔ R a b := by simp theorem chain_split {a b : α} {l₁ l₂ : list α} : chain R a (l₁++b::l₂) ↔ chain R a (l₁++[b]) ∧ chain R b l₂ := by induction l₁ with x l₁ IH generalizing a; simp [*, and_assoc] theorem chain_map (f : β → α) {b : β} {l : list β} : chain R (f b) (map f l) ↔ chain (λ a b : β, R (f a) (f b)) b l := by induction l generalizing b; simp * theorem chain_of_chain_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {a : α} {l : list α} (p : chain S (f a) (map f l)) : chain R a l := ((chain_map f).1 p).imp H theorem chain_map_of_chain {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {a : α} {l : list α} (p : chain R a l) : chain S (f a) (map f l) := (chain_map f).2 $ p.imp H theorem chain_of_pairwise {a : α} {l : list α} (p : pairwise R (a::l)) : chain R a l := begin cases pairwise_cons.1 p with r p', clear p, induction p' with b l r' p IH generalizing a; simp, simp at r, simp [r], show chain R b l, from IH r' end theorem chain_iff_pairwise (tr : transitive R) {a : α} {l : list α} : chain R a l ↔ pairwise R (a::l) := ⟨λ c, begin induction c with b b c l r p IH, {simp}, apply IH.cons _, simp [r], show ∀ x ∈ l, R b x, from λ x m, (tr r (rel_of_pairwise_cons IH m)), end, chain_of_pairwise⟩ instance decidable_chain [decidable_rel R] (a : α) (l : list α) : decidable (chain R a l) := by induction l generalizing a; simp; resetI; apply_instance end chain /- no duplicates predicate -/ /-- `nodup l` means that `l` has no duplicates, that is, any element appears at most once in the list. It is defined as `pairwise (≠)`. -/ def nodup : list α → Prop := pairwise (≠) section nodup @[simp] theorem forall_mem_ne {a : α} {l : list α} : (∀ (a' : α), a' ∈ l → ¬a = a') ↔ a ∉ l := ⟨λ h m, h _ m rfl, λ h a' m e, h (e.symm ▸ m)⟩ @[simp] theorem nodup_nil : @nodup α [] := pairwise.nil _ @[simp] theorem nodup_cons {a : α} {l : list α} : nodup (a::l) ↔ a ∉ l ∧ nodup l := by simp [nodup] theorem nodup_cons_of_nodup {a : α} {l : list α} (m : a ∉ l) (n : nodup l) : nodup (a::l) := nodup_cons.2 ⟨m, n⟩ theorem nodup_singleton (a : α) : nodup [a] := nodup_cons_of_nodup (not_mem_nil a) nodup_nil theorem nodup_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : nodup l := (nodup_cons.1 h).2 theorem not_mem_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : a ∉ l := (nodup_cons.1 h).1 theorem not_nodup_cons_of_mem {a : α} {l : list α} : a ∈ l → ¬ nodup (a :: l) := imp_not_comm.1 not_mem_of_nodup_cons theorem nodup_of_sublist {l₁ l₂ : list α} : l₁ <+ l₂ → nodup l₂ → nodup l₁ := pairwise_of_sublist theorem not_nodup_pair (a : α) : ¬ nodup [a, a] := not_nodup_cons_of_mem $ mem_singleton_self _ theorem nodup_iff_sublist {l : list α} : nodup l ↔ ∀ a, ¬ [a, a] <+ l := ⟨λ d a h, not_nodup_pair a (nodup_of_sublist h d), begin induction l with a l IH; intro h, {simp}, exact nodup_cons_of_nodup (λ al, h a $ cons_sublist_cons _ $ singleton_sublist.2 al) (IH $ λ a s, h a $ sublist_cons_of_sublist _ s) end⟩ theorem nodup_iff_nth_le_inj {l : list α} : nodup l ↔ ∀ i j h₁ h₂, nth_le l i h₁ = nth_le l j h₂ → i = j := pairwise_iff_nth_le.trans ⟨λ H i j h₁ h₂ h, ((lt_trichotomy _ _) .resolve_left (λ h', H _ _ h₂ h' h)) .resolve_right (λ h', H _ _ h₁ h' h.symm), λ H i j h₁ h₂ h, ne_of_lt h₂ (H _ _ _ _ h)⟩ @[simp] theorem nth_le_index_of [decidable_eq α] {l : list α} (H : nodup l) (n h) : index_of (nth_le l n h) l = n := nodup_iff_nth_le_inj.1 H _ _ _ h $ index_of_nth_le $ index_of_lt_length.2 $ nth_le_mem _ _ _ theorem nodup_iff_count_le_one [decidable_eq α] {l : list α} : nodup l ↔ ∀ a, count a l ≤ 1 := nodup_iff_sublist.trans $ forall_congr $ λ a, have [a, a] <+ l ↔ 1 < count a l, from (@le_count_iff_repeat_sublist _ _ a l 2).symm, (not_congr this).trans not_lt @[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {l : list α} (d : nodup l) (h : a ∈ l) : count a l = 1 := le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h) theorem nodup_of_nodup_append_left {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₁ := nodup_of_sublist (sublist_append_left l₁ l₂) theorem nodup_of_nodup_append_right {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₂ := nodup_of_sublist (sublist_append_right l₁ l₂) theorem nodup_append {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup l₁ ∧ nodup l₂ ∧ disjoint l₁ l₂ := by simp [nodup, pairwise_append, disjoint_iff_ne] theorem disjoint_of_nodup_append {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : disjoint l₁ l₂ := (nodup_append.1 d).2.2 theorem nodup_append_of_nodup {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂) (dj : disjoint l₁ l₂) : nodup (l₁++l₂) := nodup_append.2 ⟨d₁, d₂, dj⟩ theorem nodup_app_comm {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup (l₂++l₁) := by simp [nodup_append, and.left_comm] theorem nodup_middle {a : α} {l₁ l₂ : list α} : nodup (l₁ ++ a::l₂) ↔ nodup (a::(l₁++l₂)) := by simp [nodup_append, not_or_distrib, and.left_comm, and_assoc] theorem nodup_of_nodup_map (f : α → β) {l : list α} : nodup (map f l) → nodup l := pairwise_of_pairwise_map f $ λ a b, mt $ congr_arg f theorem nodup_map_on {f : α → β} {l : list α} (H : ∀x∈l, ∀y∈l, f x = f y → x = y) (d : nodup l) : nodup (map f l) := pairwise_map_of_pairwise _ (by exact λ a b ⟨ma, mb, n⟩ e, n (H a ma b mb e)) (pairwise.and_mem.1 d) theorem nodup_map {f : α → β} {l : list α} (hf : injective f) : nodup l → nodup (map f l) := nodup_map_on (assume x _ y _ h, hf h) theorem nodup_map_iff {f : α → β} {l : list α} (hf : injective f) : nodup (map f l) ↔ nodup l := ⟨nodup_of_nodup_map _, nodup_map hf⟩ @[simp] theorem nodup_attach {l : list α} : nodup (attach l) ↔ nodup l := ⟨λ h, attach_map_val l ▸ nodup_map (λ a b, subtype.eq) h, λ h, nodup_of_nodup_map subtype.val ((attach_map_val l).symm ▸ h)⟩ theorem nodup_pmap {p : α → Prop} {f : Π a, p a → β} {l : list α} {H} (hf : ∀ a ha b hb, f a ha = f b hb → a = b) (h : nodup l) : nodup (pmap f l H) := by rw [pmap_eq_map_attach]; exact nodup_map (λ ⟨a, ha⟩ ⟨b, hb⟩ h, by congr; exact hf a (H _ ha) b (H _ hb) h) (nodup_attach.2 h) theorem nodup_filter (p : α → Prop) [decidable_pred p] {l} : nodup l → nodup (filter p l) := pairwise_filter_of_pairwise p @[simp] theorem nodup_reverse {l : list α} : nodup (reverse l) ↔ nodup l := pairwise_reverse.trans $ by simp [nodup, eq_comm] instance nodup_decidable [decidable_eq α] : ∀ l : list α, decidable (nodup l) := list.decidable_pairwise theorem nodup_erase_eq_filter [decidable_eq α] (a : α) {l} (d : nodup l) : l.erase a = filter (≠ a) l := begin induction d with b l m d IH; simp [list.erase, list.filter], by_cases b = a; simp *, subst b, show l = filter (λ a', ¬ a' = a) l, rw filter_eq_self.2, simpa only [eq_comm] using m end theorem nodup_erase_of_nodup [decidable_eq α] (a : α) {l} : nodup l → nodup (l.erase a) := nodup_of_sublist (erase_sublist _ _) theorem mem_erase_iff_of_nodup [decidable_eq α] {a b : α} {l} (d : nodup l) : a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l := by rw nodup_erase_eq_filter b d; simp [and_comm] theorem mem_erase_of_nodup [decidable_eq α] {a : α} {l} (h : nodup l) : a ∉ l.erase a := by rw mem_erase_iff_of_nodup h; simp theorem nodup_join {L : list (list α)} : nodup (join L) ↔ (∀ l ∈ L, nodup l) ∧ pairwise disjoint L := by simp [nodup, pairwise_join, disjoint_left.symm] theorem nodup_bind {l₁ : list α} {f : α → list β} : nodup (l₁.bind f) ↔ (∀ x ∈ l₁, nodup (f x)) ∧ pairwise (λ (a b : α), disjoint (f a) (f b)) l₁ := by simp [list.bind, nodup_join, pairwise_map, and_comm, and.left_comm]; rw [show (∀ (l : list β) (x : α), f x = l → x ∈ l₁ → nodup l) ↔ (∀ (x : α), x ∈ l₁ → nodup (f x)), from forall_swap.trans $ forall_congr $ λ_, by simp] theorem nodup_product {l₁ : list α} {l₂ : list β} (d₁ : nodup l₁) (d₂ : nodup l₂) : nodup (product l₁ l₂) := nodup_bind.2 ⟨λ a ma, nodup_map (injective_of_left_inverse (λ b, (rfl : (a,b).2 = b))) d₂, d₁.imp (λ a₁ a₂ n x, suffices ∀ (b₁ : β), b₁ ∈ l₂ → (a₁, b₁) = x → ∀ (b₂ : β), b₂ ∈ l₂ → (a₂, b₂) ≠ x, by simpa, λ b₁ mb₁ e b₂ mb₂ e', by subst e'; injection e; contradiction)⟩ theorem nodup_sigma {σ : α → Type*} {l₁ : list α} {l₂ : Π a, list (σ a)} (d₁ : nodup l₁) (d₂ : ∀ a, nodup (l₂ a)) : nodup (sigma l₁ l₂) := nodup_bind.2 ⟨λ a ma, nodup_map (λ b b' h, by injection h with _ h; exact eq_of_heq h) (d₂ a), d₁.imp (λ a₁ a₂ n x, suffices ∀ (b₁ : σ a₁), sigma.mk a₁ b₁ = x → b₁ ∈ l₂ a₁ → ∀ (b₂ : σ a₂), sigma.mk a₂ b₂ = x → b₂ ∉ l₂ a₂, by simpa [and_comm], λ b₁ e mb₁ b₂ e' mb₂, by subst e'; injection e; contradiction)⟩ theorem nodup_filter_map {f : α → option β} {l : list α} (H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') : nodup l → nodup (filter_map f l) := pairwise_filter_map_of_pairwise f $ λ a a' n b bm b' bm' e, n $ H a a' b' (e ▸ bm) bm' theorem nodup_concat {a : α} {l : list α} (h : a ∉ l) (h' : nodup l) : nodup (concat l a) := by simp; exact nodup_append_of_nodup h' (nodup_singleton _) (disjoint_singleton.2 h) theorem nodup_insert [decidable_eq α] {a : α} {l : list α} (h : nodup l) : nodup (insert a l) := by by_cases h' : a ∈ l; simp [h', h]; apply nodup_cons h' h theorem nodup_union [decidable_eq α] (l₁ : list α) {l₂ : list α} (h : nodup l₂) : nodup (l₁ ∪ l₂) := begin induction l₁ with a l₁ ih generalizing l₂, { exact h }, simp, apply nodup_insert, exact ih h end theorem nodup_inter_of_nodup [decidable_eq α] {l₁ : list α} (l₂) : nodup l₁ → nodup (l₁ ∩ l₂) := nodup_filter _ @[simp] theorem nodup_sublists {l : list α} : nodup (sublists l) ↔ nodup l := ⟨λ h, nodup_of_nodup_map _ (nodup_of_sublist (map_ret_sublist_sublists _) h), λ h, (pairwise_sublists h).imp (λ _ _ h, mt reverse_inj.2 (ne_of_lex_ne h))⟩ @[simp] theorem nodup_sublists' {l : list α} : nodup (sublists' l) ↔ nodup l := by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective, nodup_sublists, nodup_reverse] end nodup /- erase duplicates function -/ section erase_dup variable [decidable_eq α] /-- `erase_dup l` removes duplicates from `l` (taking only the first occurrence). erase_dup [1, 2, 2, 0, 1] = [1, 2, 0] -/ def erase_dup : list α → list α := pw_filter (≠) @[simp] theorem erase_dup_nil : erase_dup [] = ([] : list α) := rfl theorem erase_dup_cons_of_mem' {a : α} {l : list α} (h : a ∈ erase_dup l) : erase_dup (a::l) = erase_dup l := pw_filter_cons_of_neg $ by simpa using h theorem erase_dup_cons_of_not_mem' {a : α} {l : list α} (h : a ∉ erase_dup l) : erase_dup (a::l) = a :: erase_dup l := pw_filter_cons_of_pos $ by simpa using h @[simp] theorem mem_erase_dup {a : α} {l : list α} : a ∈ erase_dup l ↔ a ∈ l := by simpa using not_congr (@forall_mem_pw_filter α (≠) _ (λ x y z xz, not_and_distrib.1 $ mt (and.rec eq.trans) xz) a l) @[simp] theorem erase_dup_cons_of_mem {a : α} {l : list α} (h : a ∈ l) : erase_dup (a::l) = erase_dup l := erase_dup_cons_of_mem' $ mem_erase_dup.2 h @[simp] theorem erase_dup_cons_of_not_mem {a : α} {l : list α} (h : a ∉ l) : erase_dup (a::l) = a :: erase_dup l := erase_dup_cons_of_not_mem' $ mt mem_erase_dup.1 h theorem erase_dup_sublist : ∀ (l : list α), erase_dup l <+ l := pw_filter_sublist theorem erase_dup_subset : ∀ (l : list α), erase_dup l ⊆ l := pw_filter_subset theorem subset_erase_dup (l : list α) : l ⊆ erase_dup l := λ a, mem_erase_dup.2 theorem nodup_erase_dup : ∀ l : list α, nodup (erase_dup l) := pairwise_pw_filter theorem erase_dup_eq_self {l : list α} : erase_dup l = l ↔ nodup l := pw_filter_eq_self theorem erase_dup_append (l₁ l₂ : list α) : erase_dup (l₁ ++ l₂) = l₁ ∪ erase_dup l₂ := begin induction l₁ with a l₁ IH; simp, rw ← IH, show erase_dup (a :: (l₁ ++ l₂)) = insert a (erase_dup (l₁ ++ l₂)), by_cases a ∈ erase_dup (l₁ ++ l₂); [ rw [erase_dup_cons_of_mem' h, insert_of_mem h], rw [erase_dup_cons_of_not_mem' h, insert_of_not_mem h]] end end erase_dup /- iota and range -/ /-- `range' s n` is the list of numbers `[s, s+1, ..., s+n-1]`. It is intended mainly for proving properties of `range` and `iota`. -/ @[simp] def range' : ℕ → ℕ → list ℕ | s 0 := [] | s (n+1) := s :: range' (s+1) n @[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n | s 0 := rfl | s (n+1) := congr_arg succ (length_range' _ _) @[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n | s 0 := by simp | s (n+1) := have m = s → m < s + (n + 1), from λ e, e ▸ lt_succ_of_le (le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa [eq_comm] using (@le_iff_eq_or_lt _ _ s m).symm, by simp [@mem_range' (s+1) n, or_and_distrib_left, or_iff_right_of_imp this, l] theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n) | s 0 := chain.nil _ _ | s (n+1) := (chain_succ_range' (s+1) n).cons rfl theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) := (chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _) theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n) | s 0 := pairwise.nil _ | s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m) | s 0 n := rfl | s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m), by rw [add_right_comm, range'_append] theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := ⟨λ h, by simpa using length_le_of_sublist h, λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, subset_of_sublist (range'_sublist_right.2 h)⟩ theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m) | s 0 (n+1) _ := by simp | s (m+1) (n+1) h := by simp [nth_range' (s+1) (lt_of_add_lt_add_right h)] theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] := by rw add_comm n 1; exact (range'_append s n 1).symm theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s) | 0 n := rfl | (s+1) n := by rw [show n+(s+1) = n+1+s, by simp]; exact range_core_range' s (n+1) theorem range_eq_range' (n : ℕ) : range n = range' 0 n := (range_core_range' n 0).trans $ by rw zero_add @[simp] theorem length_range (n : ℕ) : length (range n) = n := by simp [range_eq_range'] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp [range_eq_range', pairwise_lt_range'] theorem nodup_range (n : ℕ) : nodup (range n) := by simp [range_eq_range', nodup_range'] theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n := by simp [range_eq_range', range'_sublist_right] theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := by simp [range_eq_range', range'_subset_right] @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := by simp [range_eq_range', zero_le] @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := mt mem_range.1 $ lt_irrefl _ theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m := by simp [range_eq_range', nth_range' _ h] theorem range_concat (n : ℕ) : range (n + 1) = range n ++ [n] := by simp [range_eq_range', range'_concat] theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n) | 0 := rfl | (n+1) := by simp [iota, range'_concat, iota_eq_reverse_range' n] @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := by simp [iota_eq_reverse_range'] theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) := by simp [iota_eq_reverse_range', pairwise_lt_range'] theorem nodup_iota (n : ℕ) : nodup (iota n) := by simp [iota_eq_reverse_range', nodup_range'] theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := by simp [iota_eq_reverse_range', lt_succ_iff] @[simp] theorem enum_from_map_fst : ∀ n (l : list α), map prod.fst (enum_from n l) = range' n l.length | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _) @[simp] theorem enum_map_fst (l : list α) : map prod.fst (enum l) = range l.length := by simp [enum, range_eq_range'] end list theorem option.to_list_nodup {α} (o : option α) : o.to_list.nodup := by cases o; simp [option.to_list]
50b46244d63fb746c7231d4f6737518a95802125
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/data/list/comb.lean
6f78acdf4d24b9ce08cbfb06476e1cd8728f5044
[ "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
3,183
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Haitao Zhang, Floris van Doorn List combinators. -/ import init.data.list.basic universes u v w namespace list open nat variables {α : Type u} {β : Type v} {φ : Type w} section map_accumr variable {σ : Type} -- This runs a function over a list returning the intermediate results and a -- a final result. definition map_accumr (f : α → σ → σ × β) : list α → σ → (σ × list β) | [] c := (c, []) | (y::yr) c := let r := map_accumr yr c in let z := f y (prod.fst r) in (prod.fst z, prod.snd z :: prod.snd r) @[simp] theorem length_map_accumr : ∀ (f : α → σ → σ × β) (x : list α) (s : σ), length (prod.snd (map_accumr f x s)) = length x | f (a::x) s := congr_arg succ (length_map_accumr f x s) | f [] s := rfl end map_accumr section map_accumr₂ -- This runs a function over two lists returning the intermediate results and a -- a final result. definition map_accumr₂ {α β σ φ : Type} (f : α → β → σ → σ × φ) : list α → list β → σ → σ × list φ | [] _ c := (c,[]) | _ [] c := (c,[]) | (x::xr) (y::yr) c := let r := map_accumr₂ xr yr c in let q := f x y (prod.fst r) in (prod.fst q, prod.snd q :: (prod.snd r)) @[simp] theorem length_map_accumr₂ {α β σ φ : Type} : ∀ (f : α → β → σ → σ × φ) x y c, length (prod.snd (map_accumr₂ f x y c)) = min (length x) (length y) | f (a::x) (b::y) c := calc succ (length (prod.snd (map_accumr₂ f x y c))) = succ (min (length x) (length y)) : congr_arg succ (length_map_accumr₂ f x y c) ... = min (succ (length x)) (succ (length y)) : eq.symm (min_succ_succ (length x) (length y)) | f (a::x) [] c := rfl | f [] (b::y) c := rfl | f [] [] c := rfl end map_accumr₂ @[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : β → α → β) : ∀ (b : β) (l₁ l₂ : list α), foldl f b (l₁++l₂) = foldl f (foldl f b l₁) l₂ | b [] l₂ := rfl | b (a::l₁) l₂ := by simp [foldl_append] @[simp] theorem foldr_append (f : α → β → β) : ∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by simp [foldr_append] theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) : foldl f a (reverse l) = foldr (λx y, f y x) a l := by induction l; simph [foldl, foldr] theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) : foldr f a (reverse l) = foldl (λx y, f y x) a l := let t := foldl_reverse (λx y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t end list
c5218f5743bbef962cf8b8bb9589dcc6f3c6104f
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/category/Group/colimits.lean
f389a689ba4c029a4f50bc9a214e045087d1899b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,213
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 algebra.category.Group.preadditive import group_theory.quotient_group import category_theory.limits.limits import category_theory.limits.concrete_category import category_theory.limits.shapes.kernels import category_theory.limits.shapes.concrete_category /-! # The category of additive commutative groups has all colimits. This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`. It is a very uniform approach, that conceivably could be synthesised directly by a tactic that analyses the shape of `add_comm_group` and `monoid_hom`. TODO: In fact, in `AddCommGroup` there is a much nicer model of colimits as quotients of finitely supported functions, and we really should implement this as well (or instead). -/ universes u v open category_theory open category_theory.limits -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. namespace AddCommGroup.colimits /-! We build the colimit of a diagram in `AddCommGroup` by constructing the free group on the disjoint union of all the abelian groups in the diagram, then taking the quotient by the abelian group laws within each abelian group, and the identifications given by the morphisms in the diagram. -/ variables {J : Type v} [small_category J] (F : J ⥤ AddCommGroup.{v}) /-- An inductive type representing all group expressions (without relations) on a collection of types indexed by the objects of `J`. -/ inductive prequotient -- There's always `of` | of : Π (j : J) (x : F.obj j), prequotient -- Then one generator for each operation | zero : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient instance : inhabited (prequotient F) := ⟨prequotient.zero⟩ open prequotient /-- The relation on `prequotient` saying when two expressions are equal because of the abelian group laws, or because one element is mapped to another by a morphism in the diagram. -/ inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : F.obj j), relation (of j' (F.map f x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | neg : Π (j) (x : F.obj j), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : F.obj j), relation (of j (x + y)) (add (of j x) (of j y)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) /-- The setoid corresponding to group expressions modulo abelian group relations and identifications. -/ def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid /-- The underlying type of the colimit of a diagram in `AddCommGroup`. -/ @[derive inhabited] def colimit_type : Type v := quotient (colimit_setoid F) instance : add_comm_group (colimit_type F) := { zero := begin exact quot.mk _ zero end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl /-- The bundled abelian group giving the colimit of a diagram. -/ def colimit : AddCommGroup := AddCommGroup.of (colimit_type F) /-- The function from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_fun (j : J) (x : F.obj j) : colimit_type F := quot.mk _ (of j x) /-- The group homomorphism from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, map_zero' := by apply quot.sound; apply relation.zero, map_add' := by intros; apply quot.sound; apply relation.add } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } /-- The cocone over the proposed colimit abelian group. -/ def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. /-- The function from the free abelian group on the diagram to the cone point of any other cocone. -/ @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y /-- The function from the colimit abelian group to the cone point of any other cocone. -/ def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { simp, }, -- zero { simp, }, -- neg { simp, }, -- add { simp, }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- add_assoc { rw add_assoc, }, } end /-- The group homomorphism from the colimit abelian group to the cone point of any other cocone. -/ def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_zero' := rfl, map_add' := λ x y, by { induction x; induction y; refl }, } /-- Evidence that the proposed colimit is the colimit. -/ def colimit_cocone_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp *, }, { simp *, }, { simp *, }, refl end }. instance has_colimits_AddCommGroup : has_colimits AddCommGroup := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } end AddCommGroup.colimits namespace AddCommGroup open quotient_add_group /-- The categorical cokernel of a morphism in `AddCommGroup` agrees with the usual group-theoretical quotient. -/ noncomputable def cokernel_iso_quotient {G H : AddCommGroup} (f : G ⟶ H) : cokernel f ≅ AddCommGroup.of (quotient (add_monoid_hom.range f)) := { hom := cokernel.desc f (mk' _) (by { ext, apply quotient.sound, fsplit, exact -x, simp, }), inv := add_monoid_hom.of (quotient_add_group.lift _ (cokernel.π f) (by tidy)), } end AddCommGroup
faadbf1487abf5788d569a04eaccafabf67a100d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/model_theory/basic.lean
11406be9294202379dd77477e0088bddbb98a2eb
[ "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
45,960
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 data.nat.basic import data.set_like.basic import data.set.lattice import data.fin.tuple.basic import data.fintype.basic import order.closure /-! # Basics on First-Order Structures This file defines first-order languages and structures in the style of the [Flypitch project](https://flypitch.github.io/). ## 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. * A `first_order.language.term` is defined so that `L.term α` is the type of `L`-terms with free variables indexed by `α`. * A `first_order.language.formula` is defined so that `L.formula α` is the type of `L`-formulas with free variables indexed by `α`. * A `first_order.language.sentence` is a formula with no free variables. * A `first_order.language.theory` is a set of sentences. * A `first_order.language.definable_set` is defined so that `L.definable_set M α` is the boolean algebra of subsets of `α → M` defined by formulas. ## 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 namespace first_order /-- 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) namespace language /-- The empty language has no symbols. -/ def empty : language := ⟨λ _, pempty, λ _, pempty⟩ instance : inhabited language := ⟨empty⟩ /-- The type of constants in a given language. -/ @[nolint has_inhabited_instance] def const (L : language) := L.functions 0 variable (L : language.{u v}) /-- A language is relational when it has no function symbols. -/ class is_relational : Prop := (empty_functions : ∀ n, L.functions n → false) /-- A language is algebraic when it has no relation symbols. -/ class is_algebraic : Prop := (empty_relations : ∀ n, L.relations n → false) variable {L} instance is_relational_of_empty_functions {symb : ℕ → Type*} : is_relational ⟨λ _, pempty, symb⟩ := ⟨by { intro n, apply pempty.elim }⟩ instance is_algebraic_of_empty_relations {symb : ℕ → Type*} : is_algebraic ⟨symb, λ _, pempty⟩ := ⟨by { intro n, apply pempty.elim }⟩ instance is_relational_empty : is_relational (empty) := language.is_relational_of_empty_functions instance is_algebraic_empty : is_algebraic (empty) := language.is_algebraic_of_empty_relations variables (L) (M : Type*) /-- 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`. -/ 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*) [L.Structure M] [L.Structure N] open first_order.language.Structure /-- 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 := L.hom A B" in first_order /-- An embedding of first-order structures is an embedding that commutes with the interpretations of functions and relations. -/ 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 := L.embedding 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 := L.equiv A B" in first_order variables {L M N} {P : Type*} [L.Structure P] {Q : Type*} [L.Structure Q] instance : has_coe_t L.const M := ⟨λ c, fun_map c fin.elim0⟩ lemma fun_map_eq_coe_const {c : L.const} {x : fin 0 → M} : fun_map c x = c := congr rfl (funext fin.elim0) namespace hom instance has_coe_to_fun : has_coe_to_fun (M →[L] N) (λ _, M → N) := ⟨to_fun⟩ @[simp] lemma to_fun_eq_coe {f : M →[L] N} : f.to_fun = (f : M → N) := rfl lemma coe_injective : @function.injective (M →[L] N) (M → N) coe_fn | f g h := by {cases f, cases g, cases h, refl} @[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⟩ @[simp] lemma map_fun (φ : M →[L] N) {n : ℕ} (f : L.functions n) (x : fin n → M) : φ (fun_map f x) = fun_map f (φ ∘ x) := φ.map_fun' f x @[simp] lemma map_const (φ : M →[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M →[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r x → rel_map r (φ ∘ x) := φ.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 namespace embedding instance has_coe_to_fun : has_coe_to_fun (M ↪[L] N) (λ _, M → N) := ⟨λ f, f.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) := φ.map_fun' f x @[simp] lemma map_const (φ : M ↪[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M ↪[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := φ.map_rel' r x /-- A first-order embedding is also a first-order homomorphism. -/ def to_hom (f : M ↪[L] N) : M →[L] N := { to_fun := f } @[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, (is_algebraic.empty_relations n r).elim, .. 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 end embedding namespace equiv /-- 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) := ⟨λ f, f.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) := φ.map_fun' f x @[simp] lemma map_const (φ : M ≃[L] N) (c : L.const) : φ c = c := (φ.map_fun c fin.elim0).trans (congr rfl (funext fin.elim0)) @[simp] lemma map_rel (φ : M ≃[L] N) {n : ℕ} (r : L.relations n) (x : fin n → M) : rel_map r (φ ∘ x) ↔ rel_map r x := φ.map_rel' r x /-- A first-order equivalence is also a first-order embedding. -/ def to_embedding (f : M ≃[L] N) : M ↪[L] N := { to_fun := f, inj' := f.to_equiv.injective } /-- A first-order equivalence is also a first-order homomorphism. -/ def to_hom (f : M ≃[L] N) : M →[L] N := { to_fun := f } @[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 | 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 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 section closed_under open set variables {n : ℕ} (f : L.functions n) (s : set M) /-- Indicates that a set in a given structure is a closed under a function symbol. -/ def closed_under : Prop := ∀ (x : fin n → M), (∀ i : fin n, x i ∈ s) → fun_map f x ∈ s variable (L) @[simp] lemma closed_under_univ : closed_under f (univ : set M) := λ _ _, mem_univ _ variables {L f s} {t : set M} namespace closed_under lemma inter (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ∩ t) := λ x h, mem_inter (hs x (λ i, mem_of_mem_inter_left (h i))) (ht x (λ i, mem_of_mem_inter_right (h i))) lemma inf (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ⊓ t) := hs.inter ht variables {S : set (set M)} lemma Inf (hS : ∀ s, s ∈ S → closed_under f s) : closed_under f (Inf S) := λ x h s hs, hS s hs x (λ i, h i s hs) end closed_under end closed_under variables (L) (M) /-- A substructure of a structure `M` is a set closed under application of function symbols. -/ structure substructure := (carrier : set M) (fun_mem : ∀{n}, ∀ (f : L.functions n), closed_under f carrier) variables {L} {M} namespace substructure instance : set_like (L.substructure M) M := ⟨substructure.carrier, λ p q h, by cases p; cases q; congr'⟩ /-- See Note [custom simps projection] -/ def simps.coe (S : L.substructure M) : set M := S initialize_simps_projections substructure (carrier → coe) @[simp] lemma mem_carrier {s : L.substructure M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two substructures are equal if they have the same elements. -/ @[ext] theorem ext {S T : L.substructure M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy a substructure replacing `carrier` with a set that is equal to it. -/ protected def copy (S : L.substructure M) (s : set M) (hs : s = S) : L.substructure M := { carrier := s, fun_mem := λ n f, hs.symm ▸ (S.fun_mem f) } variable {S : L.substructure M} @[simp] lemma coe_copy {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl lemma copy_eq {s : set M} (hs : s = S) : S.copy s hs = S := set_like.coe_injective hs lemma const_mem {c : L.const} : ↑c ∈ S := mem_carrier.2 (S.fun_mem c _ fin.elim0) /-- The substructure `M` of the structure `M`. -/ instance : has_top (L.substructure M) := ⟨{ carrier := set.univ, fun_mem := λ n f x h, set.mem_univ _ }⟩ instance : inhabited (L.substructure M) := ⟨⊤⟩ @[simp] lemma mem_top (x : M) : x ∈ (⊤ : L.substructure M) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : L.substructure M) : set M) = set.univ := rfl /-- The inf of two substructures is their intersection. -/ instance : has_inf (L.substructure M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, fun_mem := λ n f, (S₁.fun_mem f).inf (S₂.fun_mem f) }⟩ @[simp] lemma coe_inf (p p' : L.substructure M) : ((p ⊓ p' : L.substructure M) : set M) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : L.substructure M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (L.substructure M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, fun_mem := λ n f, closed_under.Inf begin rintro _ ⟨t, rfl⟩, by_cases h : t ∈ s, { simpa [h] using t.fun_mem f }, { simp [h] } end }⟩ @[simp, norm_cast] lemma coe_Inf (S : set (L.substructure M)) : ((Inf S : L.substructure M) : set M) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (L.substructure M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ lemma mem_infi {ι : Sort*} {S : ι → L.substructure M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → L.substructure M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Substructures of a structure form a complete lattice. -/ instance : complete_lattice (L.substructure M) := { le := (≤), lt := (<), top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (L.substructure M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from set_like.coe_subset_coe) is_glb_binfi } variable (L) /-- The `L.substructure` generated by a set. -/ def closure : lower_adjoint (coe : L.substructure M → set M) := ⟨λ s, Inf {S | s ⊆ S}, λ s S, ⟨set.subset.trans (λ x hx, mem_Inf.2 $ λ S hS, hS hx), λ h, Inf_le h⟩⟩ variables {L} {s : set M} lemma mem_closure {x : M} : x ∈ closure L s ↔ ∀ S : L.substructure M, s ⊆ S → x ∈ S := mem_Inf /-- The substructure generated by a set includes the set. -/ @[simp] lemma subset_closure : s ⊆ closure L s := (closure L).le_closure s lemma not_mem_of_not_mem_closure {P : M} (hP : P ∉ closure L s) : P ∉ s := λ h, hP (subset_closure h) @[simp] lemma closed (S : L.substructure M) : (closure L).closed (S : set M) := congr rfl ((closure L).eq_of_le set.subset.rfl (λ x xS, mem_closure.2 (λ T hT, hT xS))) open set /-- A substructure `S` includes `closure L s` if and only if it includes `s`. -/ @[simp] lemma closure_le : closure L s ≤ S ↔ s ⊆ S := (closure L).closure_le_closed_iff_le s S.closed /-- Substructure closure of a set is monotone in its argument: if `s ⊆ t`, then `closure L s ≤ closure L t`. -/ lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure L s ≤ closure L t := (closure L).monotone h lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure L s) : closure L s = S := (closure L).eq_of_le h₁ h₂ variable (S) /-- An induction principle for closure membership. If `p` holds for all elements of `s`, and is preserved under function symbols, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure L s) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := (@closure_le L M _ ⟨set_of p, λ n, Hfun⟩ _).2 Hs h /-- If `s` is a dense set in a structure `M`, `substructure.closure L s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, and verify that `p` is preserved under function symbols. -/ @[elab_as_eliminator] lemma dense_induction {p : M → Prop} (x : M) {s : set M} (hs : closure L s = ⊤) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := have ∀ x ∈ closure L s, p x, from λ x hx, closure_induction hx Hs (λ n, Hfun), by simpa [hs] using this x variables (L) (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure L M _) coe := { choice := λ s _, closure L s, gc := (closure L).gc, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variables {L} {M} /-- Closure of a substructure `S` equals `S`. -/ @[simp] lemma closure_eq : closure L (S : set M) = S := (substructure.gi L M).l_u_eq S @[simp] lemma closure_empty : closure L (∅ : set M) = ⊥ := (substructure.gi L M).gc.l_bot @[simp] lemma closure_univ : closure L (univ : set M) = ⊤ := @coe_top L M _ ▸ closure_eq ⊤ lemma closure_union (s t : set M) : closure L (s ∪ t) = closure L s ⊔ closure L t := (substructure.gi L M).gc.l_sup lemma closure_Union {ι} (s : ι → set M) : closure L (⋃ i, s i) = ⨆ i, closure L (s i) := (substructure.gi L M).gc.l_supr /-! ### `comap` and `map` -/ /-- The preimage of a substructure along a homomorphism is a substructure. -/ @[simps] def comap (φ : M →[L] N) (S : L.substructure N) : L.substructure M := { carrier := (φ ⁻¹' S), fun_mem := λ n f x hx, begin rw [mem_preimage, φ.map_fun], exact S.fun_mem f (φ ∘ x) hx, end } @[simp] lemma mem_comap {S : L.substructure N} {f : M →[L] N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl lemma comap_comap (S : L.substructure P) (g : N →[L] P) (f : M →[L] N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp] lemma comap_id (S : L.substructure P) : S.comap (hom.id _ _) = S := ext (by simp) /-- The image of a substructure along a homomorphism is a substructure. -/ @[simps] def map (φ : M →[L] N) (S : L.substructure M) : L.substructure N := { carrier := (φ '' S), fun_mem := λ n f x hx, (mem_image _ _ _).1 ⟨fun_map f (λ i, classical.some (hx i)), S.fun_mem f _ (λ i, (classical.some_spec (hx i)).1), begin simp only [hom.map_fun, set_like.mem_coe], exact congr rfl (funext (λ i, (classical.some_spec (hx i)).2)), end⟩ } @[simp] lemma mem_map {f : M →[L] N} {S : L.substructure M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex lemma mem_map_of_mem (f : M →[L] N) {S : L.substructure M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx lemma apply_coe_mem_map (f : M →[L] N) (S : L.substructure M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop lemma map_map (g : N →[L] P) (f : M →[L] N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ lemma map_le_iff_le_comap {f : M →[L] N} {S : L.substructure M} {T : L.substructure N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff lemma gc_map_comap (f : M →[L] N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap lemma map_le_of_le_comap {T : L.substructure N} {f : M →[L] N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le lemma le_comap_of_map_le {T : L.substructure N} {f : M →[L] N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u lemma le_comap_map {f : M →[L] N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ lemma map_comap_le {S : L.substructure N} {f : M →[L] N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ lemma monotone_map {f : M →[L] N} : monotone (map f) := (gc_map_comap f).monotone_l lemma monotone_comap {f : M →[L] N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp] lemma map_comap_map {f : M →[L] N} : ((S.map f).comap f).map f = S.map f := (gc_map_comap f).l_u_l_eq_l _ @[simp] lemma comap_map_comap {S : L.substructure N} {f : M →[L] N} : ((S.comap f).map f).comap f = S.comap f := (gc_map_comap f).u_l_u_eq_u _ lemma map_sup (S T : L.substructure M) (f : M →[L] N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (S T : L.substructure N) (f : M →[L] N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : M →[L] N) : (⊥ : L.substructure M).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : M →[L] N) : (⊤ : L.substructure N).comap f = ⊤ := (gc_map_comap f).u_top @[simp] lemma map_id (S : L.substructure M) : S.map (hom.id L M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →[L] N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : L.substructure M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : L.substructure M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → L.substructure M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : L.substructure M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → L.substructure M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : L.substructure M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →[L] N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : L.substructure N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → L.substructure N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → L.substructure N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : L.substructure N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion instance induced_Structure {S : L.substructure M} : L.Structure S := { fun_map := λ n f x, ⟨fun_map f (λ i, x i), S.fun_mem f (λ i, x i) (λ i, (x i).2)⟩, rel_map := λ n r x, rel_map r (λ i, (x i : M)) } /-- The natural embedding of an `L.substructure` of `M` into `M`. -/ def subtype (S : L.substructure M) : S ↪[L] M := { to_fun := coe, inj' := subtype.coe_injective } @[simp] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- The equivalence between the maximal substructure of a structure and the structure itself. -/ def top_equiv : (⊤ : L.substructure M) ≃[L] M := { to_fun := subtype ⊤, inv_fun := λ m, ⟨m, mem_top m⟩, left_inv := λ m, by simp, right_inv := λ m, rfl } @[simp] lemma coe_top_equiv : ⇑(top_equiv : (⊤ : L.substructure M) ≃[L] M) = coe := rfl /-- A dependent version of `substructure.closure_induction`. -/ @[elab_as_eliminator] lemma closure_induction' (s : set M) {p : Π x, x ∈ closure L s → Prop} (Hs : ∀ x (h : x ∈ s), p x (subset_closure h)) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f {x | ∃ hx, p x hx}) {x} (hx : x ∈ closure L s) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ closure L s) (hc : p x hx), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hs x hx⟩) @Hfun end end substructure namespace hom open substructure /-- The substructure of elements `x : M` such that `f x = g x` -/ def eq_locus (f g : M →[L] N) : substructure L M := { carrier := {x : M | f x = g x}, fun_mem := λ n fn x hx, by { have h : f ∘ x = g ∘ x := by { ext, repeat {rw function.comp_apply}, apply hx, }, simp [h], } } /-- If two `L.hom`s are equal on a set, then they are equal on its substructure closure. -/ lemma eq_on_closure {f g : M →[L] N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure L s) := show closure L s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_top {f g : M →[L] N} (h : set.eq_on f g (⊤ : substructure L M)) : f = g := ext $ λ x, h trivial variable {s : set M} lemma eq_of_eq_on_dense (hs : closure L s = ⊤) {f g : M →[L] N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h end hom variable (L) /-- A term on `α` is either a variable indexed by an element of `α` or a function symbol applied to simpler terms. -/ inductive term (α : Type) : Type u | var {} : ∀ (a : α), term | func {} : ∀ {l : ℕ} (f : L.functions l) (ts : fin l → term), term export term variable {L} /-- Relabels a term's variables along a particular function. -/ @[simp] def term.relabel {α β : Type} (g : α → β) : L.term α → L.term β | (var i) := var (g i) | (func f ts) := func f (λ i, (ts i).relabel) instance {α : Type} [inhabited α] : inhabited (L.term α) := ⟨var default⟩ instance {α} : has_coe L.const (L.term α) := ⟨λ c, func c fin_zero_elim⟩ /-- A term `t` with variables indexed by `α` can be evaluated by giving a value to each variable. -/ @[simp] def realize_term {α : Type} (v : α → M) : ∀ (t : L.term α), M | (var k) := v k | (func f ts) := fun_map f (λ i, realize_term (ts i)) @[simp] lemma realize_term_relabel {α β : Type} (g : α → β) (v : β → M) (t : L.term α) : realize_term v (t.relabel g) = realize_term (v ∘ g) t := begin induction t with _ n f ts ih, { refl, }, { simp [ih] } end @[simp] lemma hom.realize_term {α : Type} (v : α → M) (t : L.term α) (g : M →[L] N) : realize_term (g ∘ v) t = g (realize_term v t) := begin induction t, { refl }, { rw [realize_term, realize_term, g.map_fun], refine congr rfl _, ext x, simp [t_ih x], }, end @[simp] lemma embedding.realize_term {α : Type} (v : α → M) (t : L.term α) (g : M ↪[L] N) : realize_term (g ∘ v) t = g (realize_term v t) := g.to_hom.realize_term v t @[simp] lemma equiv.realize_term {α : Type} (v : α → M) (t : L.term α) (g : M ≃[L] N) : realize_term (g ∘ v) t = g (realize_term v t) := g.to_hom.realize_term v t @[simp] lemma realize_term_substructure {α : Type} {S : L.substructure M} (v : α → S) (t : L.term α) : realize_term (coe ∘ v) t = (↑(realize_term v t) : M) := S.subtype.realize_term v t variable (L) /-- `bounded_formula α n` is the type of formulas with free variables indexed by `α` and up to `n` additional free variables. -/ inductive bounded_formula (α : Type) : ℕ → Type (max u v) | bd_falsum {} {n} : bounded_formula n | bd_equal {n} (t₁ t₂ : L.term (α ⊕ fin n)) : bounded_formula n | bd_rel {n l : ℕ} (R : L.relations l) (ts : fin l → L.term (α ⊕ fin n)) : bounded_formula n | bd_imp {n} (f₁ f₂ : bounded_formula n) : bounded_formula n | bd_all {n} (f : bounded_formula (n+1)) : bounded_formula n export bounded_formula instance {α : Type} {n : ℕ} : inhabited (L.bounded_formula α n) := ⟨bd_falsum⟩ /-- `formula α` is the type of formulas with all free variables indexed by `α`. -/ @[reducible] def formula (α : Type) := L.bounded_formula α 0 /-- A sentence is a formula with no free variables. -/ @[reducible] def sentence := L.formula pempty /-- A theory is a set of sentences. -/ @[reducible] def theory := set L.sentence variables {L} {α : Type} section formula variable {n : ℕ} @[simps] instance : has_bot (L.bounded_formula α n) := ⟨bd_falsum⟩ /-- The negation of a bounded formula is also a bounded formula. -/ @[reducible] def bd_not (φ : L.bounded_formula α n) : L.bounded_formula α n := bd_imp φ ⊥ @[simps] instance : has_top (L.bounded_formula α n) := ⟨bd_not bd_falsum⟩ @[simps] instance : has_inf (L.bounded_formula α n) := ⟨λ f g, bd_not (bd_imp f (bd_not g))⟩ @[simps] instance : has_sup (L.bounded_formula α n) := ⟨λ f g, bd_imp (bd_not f) g⟩ /-- Relabels a bounded formula's variables along a particular function. -/ @[simp] def bounded_formula.relabel {α β : Type} (g : α → β) : ∀ {n : ℕ}, L.bounded_formula α n → L.bounded_formula β n | n bd_falsum := bd_falsum | n (bd_equal t₁ t₂) := bd_equal (t₁.relabel (sum.elim (sum.inl ∘ g) sum.inr)) (t₂.relabel (sum.elim (sum.inl ∘ g) sum.inr)) | n (bd_rel R ts) := bd_rel R ((term.relabel (sum.elim (sum.inl ∘ g) sum.inr)) ∘ ts) | n (bd_imp f₁ f₂) := bd_imp f₁.relabel f₂.relabel | n (bd_all f) := bd_all f.relabel namespace formula /-- The equality of two terms as a first-order formula. -/ def equal (t₁ t₂ : L.term α) : (L.formula α) := bd_equal (t₁.relabel sum.inl) (t₂.relabel sum.inl) /-- The graph of a function as a first-order formula. -/ def graph (f : L.functions n) : L.formula (fin (n + 1)) := equal (func f (λ i, var i)) (var n) end formula end formula variable {L} instance nonempty_bounded_formula {α : Type} (n : ℕ) : nonempty $ L.bounded_formula α n := nonempty.intro (by constructor) variables (M) /-- A bounded formula can be evaluated as true or false by giving values to each free variable. -/ @[simp] def realize_bounded_formula : ∀ {l} (f : L.bounded_formula α l) (v : α → M) (xs : fin l → M), Prop | _ bd_falsum v xs := false | _ (bd_equal t₁ t₂) v xs := realize_term (sum.elim v xs) t₁ = realize_term (sum.elim v xs) t₂ | _ (bd_rel R ts) v xs := rel_map R (λ i, realize_term (sum.elim v xs) (ts i)) | _ (bd_imp f₁ f₂) v xs := realize_bounded_formula f₁ v xs → realize_bounded_formula f₂ v xs | _ (bd_all f) v xs := ∀(x : M), realize_bounded_formula f v (fin.cons x xs) @[simp] lemma realize_not {l} (f : L.bounded_formula α l) (v : α → M) (xs : fin l → M) : realize_bounded_formula M (bd_not f) v xs = ¬ realize_bounded_formula M f v xs := rfl /-- A bounded formula can be evaluated as true or false by giving values to each free variable. -/ @[reducible] def realize_formula (f : L.formula α) (v : α → M) : Prop := realize_bounded_formula M f v fin_zero_elim /-- A sentence can be evaluated as true or false in a structure. -/ @[reducible] def realize_sentence (φ : L.sentence) : Prop := realize_formula M φ pempty.elim variable {M} @[simp] lemma realize_bounded_formula_relabel {α β : Type} {n : ℕ} (g : α → β) (v : β → M) (xs : fin n → M) (φ : L.bounded_formula α n) : realize_bounded_formula M (φ.relabel g) v xs ↔ realize_bounded_formula M φ (v ∘ g) xs := begin have h : ∀ (m : ℕ) (xs' : fin m → M), sum.elim v xs' ∘ sum.elim (sum.inl ∘ g) sum.inr = sum.elim (v ∘ g) xs', { intros m xs', ext x, cases x; simp, }, induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp [h _ xs] }, { simp [h _ xs] }, { simp [ih1, ih2] }, { simp [ih3] } end @[simp] lemma equiv.realize_bounded_formula {α : Type} {n : ℕ} (v : α → M) (xs : fin n → M) (φ : L.bounded_formula α n) (g : M ≃[L] N) : realize_bounded_formula N φ (g ∘ v) (g ∘ xs) ↔ realize_bounded_formula M φ v xs := begin induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp only [realize_bounded_formula, ← sum.comp_elim, equiv.realize_term, g.injective.eq_iff] }, { simp only [realize_bounded_formula, ← sum.comp_elim, equiv.realize_term, g.map_rel], }, { rw [realize_bounded_formula, ih1, ih2, realize_bounded_formula] }, { rw [realize_bounded_formula, realize_bounded_formula], split, { intros h a, have h' := h (g a), rw [← fin.comp_cons, ih3] at h', exact h' }, { intros h a, have h' := h (g.symm a), rw [← ih3, fin.comp_cons, g.apply_symm_apply] at h', exact h' }} end @[simp] lemma realize_bounded_formula_top {α : Type} {n : ℕ} (v : α → (⊤ : L.substructure M)) (xs : fin n → (⊤ : L.substructure M)) (φ : L.bounded_formula α n) : realize_bounded_formula (⊤ : L.substructure M) φ v xs ↔ realize_bounded_formula M φ (coe ∘ v) (coe ∘ xs) := begin rw ← substructure.top_equiv.realize_bounded_formula v xs φ, simp, end @[simp] lemma realize_formula_relabel {α β : Type} (g : α → β) (v : β → M) (φ : L.formula α) : realize_formula M (φ.relabel g) v ↔ realize_formula M φ (v ∘ g) := by rw [realize_formula, realize_formula, realize_bounded_formula_relabel] @[simp] lemma realize_formula_equiv {α : Type} (v : α → M) (φ : L.formula α) (g : M ≃[L] N) : realize_formula N φ (g ∘ v) ↔ realize_formula M φ v := begin rw [realize_formula, realize_formula, ← equiv.realize_bounded_formula v fin_zero_elim φ g, iff_eq_eq], exact congr rfl (funext fin_zero_elim), end @[simp] lemma realize_equal {α : Type*} (t₁ t₂ : L.term α) (x : α → M) : realize_formula M (formula.equal t₁ t₂) x ↔ realize_term x t₁ = realize_term x t₂ := by simp [formula.equal, realize_formula] @[simp] lemma realize_graph {l : ℕ} (f : L.functions l) (x : fin l → M) (y : M) : realize_formula M (formula.graph f) (fin.snoc x y) ↔ fun_map f x = y := begin simp only [formula.graph, realize_term, fin.coe_eq_cast_succ, realize_equal, fin.snoc_cast_succ], rw [fin.coe_nat_eq_last, fin.snoc_last], end section definability variables (L) [fintype α] /-- A subset of a finite Cartesian product of a structure is definable when membership in the set is given by a first-order formula. -/ structure is_definable (s : set (α → M)) : Prop := (exists_formula : ∃ (φ : L.formula α), s = set_of (realize_formula M φ)) variables {L} @[simp] lemma is_definable_empty : L.is_definable (∅ : set (α → M)) := ⟨⟨⊥, by {ext, simp} ⟩⟩ @[simp] lemma is_definable_univ : L.is_definable (set.univ : set (α → M)) := ⟨⟨⊤, by {ext, simp} ⟩⟩ @[simp] lemma is_definable.inter {f g : set (α → M)} (hf : L.is_definable f) (hg : L.is_definable g) : L.is_definable (f ∩ g) := ⟨begin rcases hf.exists_formula with ⟨φ, hφ⟩, rcases hg.exists_formula with ⟨θ, hθ⟩, refine ⟨φ ⊓ θ, _⟩, ext, simp [hφ, hθ], end⟩ @[simp] lemma is_definable.union {f g : set (α → M)} (hf : L.is_definable f) (hg : L.is_definable g) : L.is_definable (f ∪ g) := ⟨begin rcases hf.exists_formula with ⟨φ, hφ⟩, rcases hg.exists_formula with ⟨θ, hθ⟩, refine ⟨φ ⊔ θ, _⟩, ext, simp only [hφ, hθ, set.sup_eq_union, realize_not, realize_bounded_formula, bounded_formula.has_sup_sup, set.mem_union_eq, set.mem_set_of_eq], tauto, end⟩ @[simp] lemma is_definable.compl {s : set (α → M)} (hf : L.is_definable s) : L.is_definable sᶜ := ⟨begin rcases hf.exists_formula with ⟨φ, hφ⟩, refine ⟨bd_not φ, _⟩, rw hφ, refl, end⟩ @[simp] lemma is_definable.sdiff {s t : set (α → M)} (hs : L.is_definable s) (ht : L.is_definable t) : L.is_definable (s \ t) := hs.inter ht.compl variables (L) (M) (α) /-- Definable sets are subsets of finite Cartesian products of a structure such that membership is given by a first-order formula. -/ def definable_set := subtype (λ s : set (α → M), is_definable L s) namespace definable_set variables {M} {α} instance : has_top (L.definable_set M α) := ⟨⟨⊤, is_definable_univ⟩⟩ instance : has_bot (L.definable_set M α) := ⟨⟨⊥, is_definable_empty⟩⟩ instance : inhabited (L.definable_set M α) := ⟨⊥⟩ instance : set_like (L.definable_set M α) (α → M) := { coe := subtype.val, coe_injective' := subtype.val_injective } @[simp] lemma mem_top {x : α → M} : x ∈ (⊤ : L.definable_set M α) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : L.definable_set M α) : set (α → M)) = ⊤ := rfl @[simp] lemma not_mem_bot {x : α → M} : ¬ x ∈ (⊥ : L.definable_set M α) := set.not_mem_empty x @[simp] lemma coe_bot : ((⊥ : L.definable_set M α) : set (α → M)) = ⊥ := rfl instance : lattice (L.definable_set M α) := subtype.lattice (λ _ _, is_definable.union) (λ _ _, is_definable.inter) lemma le_iff {s t : L.definable_set M α} : s ≤ t ↔ (s : set (α → M)) ≤ (t : set (α → M)) := iff.rfl @[simp] lemma coe_sup {s t : L.definable_set M α} : ((s ⊔ t : L.definable_set M α) : set (α → M)) = s ∪ t := rfl @[simp] lemma mem_sup {s t : L.definable_set M α} {x : α → M} : x ∈ s ⊔ t ↔ x ∈ s ∨ x ∈ t := iff.rfl @[simp] lemma coe_inf {s t : L.definable_set M α} : ((s ⊓ t : L.definable_set M α) : set (α → M)) = s ∩ t := rfl @[simp] lemma mem_inf {s t : L.definable_set M α} {x : α → M} : x ∈ s ⊓ t ↔ x ∈ s ∧ x ∈ t := iff.rfl instance : bounded_order (L.definable_set M α) := { bot_le := λ s x hx, false.elim hx, le_top := λ s x hx, set.mem_univ x, .. definable_set.has_top L, .. definable_set.has_bot L } instance : distrib_lattice (L.definable_set M α) := { le_sup_inf := begin intros s t u x, simp only [and_imp, set.mem_inter_eq, set_like.mem_coe, coe_sup, coe_inf, set.mem_union_eq, subtype.val_eq_coe], tauto, end, .. definable_set.lattice L } /-- The complement of a definable set is also definable. -/ @[reducible] instance : has_compl (L.definable_set M α) := ⟨λ ⟨s, hs⟩, ⟨sᶜ, hs.compl⟩⟩ @[simp] lemma mem_compl {s : L.definable_set M α} {x : α → M} : x ∈ sᶜ ↔ ¬ x ∈ s := begin cases s with s hs, refl, end @[simp] lemma coe_compl {s : L.definable_set M α} : ((sᶜ : L.definable_set M α) : set (α → M)) = sᶜ := begin ext, simp, end instance : boolean_algebra (L.definable_set M α) := { sdiff := λ s t, s ⊓ tᶜ, sdiff_eq := λ s t, rfl, sup_inf_sdiff := λ ⟨s, hs⟩ ⟨t, ht⟩, begin apply le_antisymm; simp [le_iff], end, inf_inf_sdiff := λ ⟨s, hs⟩ ⟨t, ht⟩, begin rw eq_bot_iff, simp only [coe_compl, le_iff, coe_bot, coe_inf, subtype.coe_mk, set.le_eq_subset], intros x hx, simp only [set.mem_inter_eq, set.mem_compl_eq] at hx, tauto, end, inf_compl_le_bot := λ ⟨s, hs⟩, by simp [le_iff], top_le_sup_compl := λ ⟨s, hs⟩, by simp [le_iff], .. definable_set.has_compl L, .. definable_set.bounded_order L, .. definable_set.distrib_lattice L } end definable_set end definability section quotients variables (L) {M' : Type*} /-- A prestructure is a first-order structure with a `setoid` equivalence relation on it, such that quotienting by that equivalence relation is still a structure. -/ class prestructure (s : setoid M') := (to_structure : L.Structure M') (fun_equiv : ∀{n} {f : L.functions n} (x y : fin n → M'), x ≈ y → fun_map f x ≈ fun_map f y) (rel_equiv : ∀{n} {r : L.relations n} (x y : fin n → M') (h : x ≈ y), (rel_map r x = rel_map r y)) variables {L} {M'} {s : setoid M'} [ps : L.prestructure s] instance quotient_structure : L.Structure (quotient s) := { fun_map := λ n f x, quotient.map (@fun_map L M' ps.to_structure n f) prestructure.fun_equiv (quotient.fin_choice x), rel_map := λ n r x, quotient.lift (@rel_map L M' ps.to_structure n r) prestructure.rel_equiv (quotient.fin_choice x) } variables [s] include s lemma fun_map_quotient_mk {n : ℕ} (f : L.functions n) (x : fin n → M') : fun_map f (λ i, ⟦x i⟧) = ⟦@fun_map _ _ ps.to_structure _ f x⟧ := begin change quotient.map (@fun_map L M' ps.to_structure n f) prestructure.fun_equiv (quotient.fin_choice _) = _, rw [quotient.fin_choice_eq, quotient.map_mk], end lemma realize_term_quotient_mk {β : Type*} (x : β → M') (t : L.term β) : realize_term (λ i, ⟦x i⟧) t = ⟦@realize_term _ _ ps.to_structure _ x t⟧ := begin induction t with a1 a2 a3 a4 ih a6 a7 a8 a9 a0, { refl }, simp only [ih, fun_map_quotient_mk, realize_term], end end quotients end language end first_order
38889d4d83ffffd095cd83c78667eab2ace6ad5f
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/extmacro.lean
f8d9c5c21d85ca10be5c363e8b8d76eb6de02713
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
470
lean
new_frontend macro ext_tactic t:tactic "=>" newT:tactic : command => `(macro_rules | `($t) => `($newT)) syntax "trivial" : tactic ext_tactic trivial => apply Eq.refl theorem tst1 (x : Nat) : x = x := by trivial -- theorem tst2 (x y : Nat) (h : x = y) : x = y := -- by trivial -- fail as expected ext_tactic trivial => assumption theorem tst1b (x : Nat) : x = x := by trivial -- still works theorem tst2 (x y : Nat) (h : x = y) : x = y := by trivial -- works too
03f7d76951cf1aed763d2e4678ef91dd4865eba7
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/src/o_minimal/sheaf/constants.lean
62742c192ebec88bc198516159164b5494783416
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
5,628
lean
import o_minimal.sheaf.tactic namespace o_minimal variables {R : Type*} {S : struc R} variables {X Y Z : Type*} [definable_sheaf S X] [definable_sheaf S Y] [definable_sheaf S Z] -- TODO: consistent naming lemma definable.const : definable S (@function.const X Y) := begin [defin] intro x, intro y, var end lemma definable_comp : definable S (@function.comp X Y Z) := begin [defin] intro f, intro g, intro x, app, var, app, var, var end lemma definable.app {f : X → Y} (hf : definable S f) {x : X} (hx : definable S x) : definable S (f x) := begin [defin] app, exact hf.definable _, exact hx.definable _ end lemma definable.comp {g : Y → Z} (hg : definable S g) {f : X → Y} (hf : definable S f) : definable S (g ∘ f) := (definable_comp.app hg).app hf lemma definable.prod_mk : definable S (@prod.mk X Y) := begin [defin] intro x, intro y, exact ⟨x.definable, y.definable⟩ end lemma definable.fst : definable S (prod.fst : X × Y → X) := begin [defin] intro p, exact p.definable.1 end lemma definable.snd : definable S (prod.snd : X × Y → Y) := begin [defin] intro p, exact p.definable.2 end lemma definable.curry : definable S (@function.curry X Y Z) := begin [defin] intro f, intro x, intro y, app, var, app, app, exact definable.prod_mk.definable _, var, var end lemma definable.uncurry : definable S (@function.uncurry X Y Z) := begin [defin] intro f, intro p, app, app, var, app, exact definable.fst.definable _, var, app, exact definable.snd.definable _, var end instance punit.definable_sheaf : definable_sheaf S punit := { definable := λ K f, true, definable_precomp := λ L K φ f hf, trivial, definable_cover := λ K f 𝓛 h, trivial } lemma definable.star : definable S punit.star := begin [defin] exact trivial end instance Prop.definable_sheaf : definable_sheaf S Prop := { definable := λ K f, def_set S f, definable_precomp := λ L K φ f hf, φ.is_definable.preimage hf, definable_cover := λ K f 𝓛 h, Def.set_subcanonical 𝓛 f h } lemma definable.and : definable S and := begin [defin] intro p, intro q, exact def_set.inter p.definable q.definable end lemma definable.or : definable S or := begin [defin] intro p, intro q, exact def_set.union p.definable q.definable end lemma definable.imp : definable S ((→) : Prop → Prop → Prop) := begin [defin] intro p, intro q, exact def_set.imp p.definable q.definable end lemma definable.not : definable S not := begin [defin] intro p, exact def_set.compl p.definable end lemma definable.iff : definable S iff := begin [defin] intro p, intro q, exact def_set.iff p.definable q.definable end instance set.definable_sheaf : definable_sheaf S (set X) := show definable_sheaf S (X → Prop), by apply_instance lemma definable.mem : definable S ((∈) : X → set X → Prop) := begin [defin] intro x, intro s, app, var, var end lemma definable.inter : definable S ((∩) : set X → set X → set X) := begin [defin] intro s, intro t, intro x, app, app, exact definable.and.definable _, app, app, exact definable.mem.definable _, var, var, app, app, exact definable.mem.definable _, var, var end lemma definable.union : definable S ((∪) : set X → set X → set X) := begin [defin] intro s, intro t, intro x, app, app, exact definable.or.definable _, app, app, exact definable.mem.definable _, var, var, app, app, exact definable.mem.definable _, var, var end lemma definable.compl : definable S (set.compl : set X → set X) := begin [defin] intro s, intro x, app, exact definable.not.definable _, app, app, exact definable.mem.definable _, var, var end lemma definable.diff : definable S ((\) : set X → set X → set X) := begin [defin] intro s, intro t, intro x, app, app, exact definable.and.definable _, app, app, exact definable.mem.definable _, var, var, -- unnecessarily complicated, but hey why not -- TODO: `change` app, intro a, app, exact definable.not.definable _, app, app, exact definable.mem.definable _, var, var, var end -- Quantification over X is not definable in general. -- Needs a special property of X: "quasicompactness"? -- See `quantifiers` (for now, just over representables) instance set.coe.definable_sheaf {s : set X} : definable_sheaf S s := { definable := λ K f, definable_sheaf.definable (subtype.val ∘ f), definable_precomp := λ L K φ f hf, definable_sheaf.definable_precomp φ (subtype.val ∘ f) hf, definable_cover := λ K f 𝓛 hf, definable_sheaf.definable_cover (subtype.val ∘ f) 𝓛 hf } instance subtype.definable_sheaf {p : X → Prop} : definable_sheaf S {x // p x} := show definable_sheaf S (set_of p), by apply_instance -- instance prop.definable_sheaf {p : Prop} : definable_sheaf S p := sorry -- TODO: With an instance for Pi types, can we state the definable dependence on `s`? lemma definable.subtype.val {s : set X} : definable S (subtype.val : s → X) := begin [defin] intro v, exact v.definable end lemma definable_of_subtype_val {s : set Y} {f : X → s} (hf : definable S (subtype.val ∘ f)) : definable S f := begin cases hf with hf, exact ⟨λ K, hf K⟩ end -- How to state definable subtype.mk? /- lemma definable.subtype.mk {s : set X} : definable S (subtype.mk : Π (x : X), x ∈ s → s) := begin end -/ lemma definable_subtype.map {s : set X} {t : set Y} {f : X → Y} (hf : definable S f) (h : ∀ x ∈ s, f x ∈ t) : definable S (subtype.map f h : s → t) := definable_of_subtype_val $ hf.comp definable.subtype.val end o_minimal
cfbc0d02eaa503958a2ea25ec4e9bce6a1829b68
82e44445c70db0f03e30d7be725775f122d72f3e
/src/order/galois_connection.lean
2d6ec021bf3a01704b812718c6d0099063f3d89a
[ "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
25,869
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import order.complete_lattice import order.rel_iso import order.order_dual /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `∀ a b, l a ≤ b ↔ a ≤ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `α` is a complete lattice, and `l : α → β`, and `u : β → α` form a Galois insertion, then `β` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of a Galois insertion is in group theory. If `G` is a group, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The lower adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, and the upper adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of a subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Π S : set G, ↑(closure S) ≤ S → subgroup G`. When `↑(closure S) ≤ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ open function set 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 special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ 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 : α ≃o β) : galois_connection oi oi.symm := λ b g, oi.rel_symm_apply.symm 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 := λ a b, ⟨λ h, (hul _).trans (hu h), λ h, (hl h).trans (hlu _)⟩ include gc protected lemma dual {l : α → β} {u : β → α} (gc : galois_connection l u) : galois_connection (order_dual.to_dual ∘ u ∘ order_dual.of_dual) (order_dual.to_dual ∘ l ∘ order_dual.of_dual) := λ a b, (gc b a).symm 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 := λ a b H, gc.le_u ((gc.l_u_le a).trans H) lemma monotone_l : monotone l := gc.dual.monotone_u.order_dual lemma upper_bounds_l_image (s : set α) : upper_bounds (l '' s) = u ⁻¹' upper_bounds s := set.ext $ λ b, by simp [upper_bounds, gc _ _] lemma lower_bounds_u_image (s : set β) : lower_bounds (u '' s) = l ⁻¹' lower_bounds s := gc.dual.upper_bounds_l_image s lemma bdd_above_l_image {s : set α} : bdd_above (l '' s) ↔ bdd_above s := ⟨λ ⟨x, hx⟩, ⟨u x, by rwa [gc.upper_bounds_l_image] at hx⟩, gc.monotone_l.map_bdd_above⟩ lemma bdd_below_u_image {s : set β} : bdd_below (u '' s) ↔ bdd_below s := gc.dual.bdd_above_l_image lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) := ⟨gc.monotone_l.mem_upper_bounds_image h.left, λ b hb, gc.l_le $ h.right $ by rwa [gc.upper_bounds_l_image] at hb⟩ lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) := gc.dual.is_lub_l_image h lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) := ⟨λ b, gc.l_le, λ b h, h $ gc.le_u_l _⟩ lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) := ⟨λ b, gc.le_u, λ b h, h $ gc.l_u_le _⟩ end section partial_order variables [partial_order α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_l_u_eq_u : u ∘ l ∘ u = u := funext (λ x, (gc.monotone_u (gc.l_u_le _)).antisymm (gc.le_u_l _)) lemma u_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hl : ∀ a, l a = l' a) {b : β} : u b = u' b := le_antisymm (gc'.le_u $ hl (u b) ▸ gc.l_u_le _) (gc.le_u $ (hl (u' b)).symm ▸ gc'.l_u_le _) end partial_order section partial_order variables [preorder α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_u_l_eq_l : l ∘ u ∘ l = l := funext (λ x, (gc.l_u_le _).antisymm (gc.monotone_l (gc.le_u_l _))) lemma l_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hu : ∀ b, u b = u' b) {a : α} : l a = l' a := le_antisymm (gc.l_le $ (hu (l' a)).symm ▸ gc'.le_u_l _) (gc'.l_le $ hu (l a) ▸ 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 ⊤ = ⊤ := (gc.is_glb_u_image is_glb_empty).unique $ by simp only [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 ⊥ = ⊥ := (gc.is_lub_l_image is_lub_empty).unique $ by simp only [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₂ := (gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair] 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₂ := (gc.is_glb_u_image is_glb_pair).unique $ by simp only [image_pair, is_glb_pair] 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.supr_eq $ 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.infi_eq $ show is_glb (range (u ∘ f)) (u (infi f)), by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image (is_glb_Inf _) lemma l_Sup {s : set α} : l (Sup s) = (⨆a ∈ s, l a) := by simp only [Sup_eq_supr, gc.l_supr] lemma u_Inf {s : set β} : u (Inf s) = (⨅a ∈ s, u a) := by simp only [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 := λ 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 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)) := λ a b, forall_congr $ λ i, gc i (a i) (b i) end constructions end galois_connection namespace order_iso variables [preorder α] [preorder β] @[simp] lemma upper_bounds_image (e : α ≃o β) (s : set α) : upper_bounds (e '' s) = e.symm ⁻¹' upper_bounds s := e.to_galois_connection.upper_bounds_l_image s @[simp] lemma lower_bounds_image (e : α ≃o β) (s : set α) : lower_bounds (e '' s) = e.symm ⁻¹' lower_bounds s := e.dual.upper_bounds_image s @[simp] lemma bdd_above_image (e : α ≃o β) {s : set α} : bdd_above (e '' s) ↔ bdd_above s := e.to_galois_connection.bdd_above_l_image @[simp] lemma bdd_below_image (e : α ≃o β) {s : set α} : bdd_below (e '' s) ↔ bdd_below s := e.dual.bdd_above_image end order_iso namespace nat lemma galois_connection_mul_div {k : ℕ} (h : 0 < k) : galois_connection (λ n, n * k) (λ n, n / k) := λ 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. Dual to `galois_coinsertion` -/ @[nolint has_inhabited_instance] 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 : α ≃o β) : @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 } /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_insertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ b, b ≤ l (u b)) : galois_insertion l u := { choice := λ x _, l x, gc := gc, le_l_u := h, choice_eq := λ _ _, 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 := λ b, gc.l_le $ bot_le, .. ‹partial_order β› } namespace galois_insertion variables {l : α → β} {u : β → α} lemma l_u_eq [preorder α] [partial_order β] (gi : galois_insertion l u) (b : β) : l (u b) = b := (gi.gc.l_u_le _).antisymm (gi.le_l_u _) lemma l_surjective [preorder α] [partial_order β] (gi : galois_insertion l u) : surjective l := λ b, ⟨u b, gi.l_u_eq b⟩ lemma u_injective [preorder α] [partial_order β] (gi : galois_insertion l u) : injective u := λ a b h, calc a = l (u a) : (gi.l_u_eq a).symm ... = l (u b) : congr_arg l h ... = b : gi.l_u_eq b lemma l_sup_u [semilattice_sup α] [semilattice_sup β] (gi : galois_insertion l u) (a b : β) : l (u a ⊔ u b) = a ⊔ b := calc l (u a ⊔ u b) = l (u a) ⊔ l (u b) : gi.gc.l_sup ... = a ⊔ b : by simp only [gi.l_u_eq] lemma l_supr_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨆ i, u (f i)) = ⨆ i, (f i) := calc l (⨆ (i : ι), u (f i)) = ⨆ (i : ι), l (u (f i)) : gi.gc.l_supr ... = ⨆ (i : ι), f i : congr_arg _ $ funext $ λ i, gi.l_u_eq (f i) lemma l_supr_of_ul_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) : l (⨆ i, (f i)) = ⨆ i, l (f i) := calc l (⨆ (i : ι), (f i)) = l ⨆ (i : ι), (u (l (f i))) : by simp [hf] ... = ⨆ (i : ι), l (f i) : gi.l_supr_u _ lemma l_inf_u [semilattice_inf α] [semilattice_inf β] (gi : galois_insertion l u) (a b : β) : l (u a ⊓ u b) = a ⊓ b := calc l (u a ⊓ u b) = l (u (a ⊓ b)) : congr_arg l gi.gc.u_inf.symm ... = a ⊓ b : by simp only [gi.l_u_eq] lemma l_infi_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨅ i, u (f i)) = ⨅ i, (f i) := calc l (⨅ (i : ι), u (f i)) = l (u (⨅ (i : ι), (f i))) : congr_arg l gi.gc.u_infi.symm ... = ⨅ (i : ι), f i : gi.l_u_eq _ lemma l_infi_of_ul_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) : l (⨅ i, (f i)) = ⨅ i, l (f i) := calc l (⨅ i, (f i)) = l ⨅ (i : ι), (u (l (f i))) : by simp [hf] ... = ⨅ i, l (f i) : gi.l_infi_u _ lemma u_le_u_iff [preorder α] [preorder β] (gi : galois_insertion l u) {a b} : u a ≤ u b ↔ a ≤ b := ⟨λ h, (gi.le_l_u _).trans (gi.gc.l_le h), λ h, gi.gc.monotone_u h⟩ lemma strict_mono_u [preorder α] [preorder β] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le $ λ _ _, gi.u_le_u_iff.symm section lift variables [partial_order β] /-- 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 := λ a b, (gi.le_l_u a).trans $ gi.gc.monotone_l $ le_sup_left, le_sup_right := λ a b, (gi.le_l_u b).trans $ gi.gc.monotone_l $ le_sup_right, sup_le := λ 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 λ a b, gi.gc.l_le inf_le_left, inf_le_right := by simp only [gi.choice_eq]; exact λ a b, gi.gc.l_le inf_le_right, le_inf := by simp only [gi.choice_eq]; exact λ a b c hac hbc, (gi.le_l_u a).trans $ 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 λ b, (gi.le_l_u b).trans (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 := λ s a hs, gi.gc.l_le $ supr_le $ λ b, supr_le $ λ hb, gi.gc.monotone_u $ hs _ hb, le_Sup := λ s a ha, (gi.le_l_u a).trans $ 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 $ λ b, le_infi $ λ 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 λ 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 λ s a hs, (gi.le_l_u a).trans $ gi.gc.monotone_l $ le_infi $ λ b, show u a ≤ ⨅ (H : b ∈ s), u b, from le_infi $ λ hb, gi.gc.monotone_u $ hs _ hb, .. gi.lift_bounded_lattice } end lift end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ @[nolint has_inhabited_instance] structure galois_coinsertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx : β, x ≤ l (u x) → α) (gc : galois_connection l u) (u_l_le : ∀ x, u (l x) ≤ x) (choice_eq : ∀ a h, choice a h = u a) /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_coinsertion l u → @galois_insertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_insertion l u → @galois_coinsertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_insertion (order_dual β) (order_dual α) _ _ u l → galois_coinsertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_coinsertion (order_dual β) (order_dual α) _ _ u l → galois_insertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion [preorder α] [preorder β] (oi : α ≃o β) : @galois_coinsertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi.symm b, gc := oi.to_galois_connection, u_l_le := λ g, le_of_eq (oi.left_inv g), choice_eq := λ b h, rfl } /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hlu : ∀ b, l (u b) ≤ b) (hul : ∀ a, u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro hl.order_dual hu.order_dual hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_coinsertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ a, u (l a) ≤ a) : galois_coinsertion l u := { choice := λ x _, u x, gc := gc, u_l_le := h, choice_eq := λ _ _, rfl } /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {α β : Type*} [partial_order α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_top α := { top := u ⊤, le_top := λ b, gc.le_u $ le_top, .. ‹partial_order α› } namespace galois_coinsertion variables {l : α → β} {u : β → α} lemma u_l_eq [partial_order α] [preorder β] (gi : galois_coinsertion l u) (a : α) : u (l a) = a := gi.dual.l_u_eq a lemma u_surjective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : surjective u := gi.dual.l_surjective lemma l_injective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : injective l := gi.dual.u_injective lemma u_inf_l [semilattice_inf α] [semilattice_inf β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊓ l b) = a ⊓ b := gi.dual.l_sup_u a b lemma u_infi_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨅ i, l (f i)) = ⨅ i, (f i) := gi.dual.l_supr_u _ lemma u_infi_of_lu_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ i, l (u (f i)) = f i) : u (⨅ i, (f i)) = ⨅ i, u (f i) := gi.dual.l_supr_of_ul_eq_self _ hf lemma u_sup_l [semilattice_sup α] [semilattice_sup β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊔ l b) = a ⊔ b := gi.dual.l_inf_u _ _ lemma u_supr_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨆ i, l (f i)) = ⨆ i, (f i) := gi.dual.l_infi_u _ lemma u_supr_of_lu_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ i, l (u (f i)) = f i) : u (⨆ i, (f i)) = ⨆ i, u (f i) := gi.dual.l_infi_of_ul_eq_self _ hf lemma l_le_l_iff [preorder α] [preorder β] (gi : galois_coinsertion l u) {a b} : l a ≤ l b ↔ a ≤ b := gi.dual.u_le_u_iff lemma strict_mono_l [partial_order α] [preorder β] (gi : galois_coinsertion l u) : strict_mono l := λ a b h, gi.dual.strict_mono_u h section lift variables [partial_order α] /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf [semilattice_inf β] (gi : galois_coinsertion l u) : semilattice_inf α := { inf := λ a b, u (l a ⊓ l b), inf_le_left := λ a b, (gi.gc.monotone_u $ inf_le_left).trans (gi.u_l_le a), inf_le_right := λ a b, (gi.gc.monotone_u $ inf_le_right).trans (gi.u_l_le b), le_inf := λ a b c hac hbc, gi.gc.le_u $ le_inf (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc), .. ‹partial_order α› } /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup [semilattice_sup β] (gi : galois_coinsertion l u) : semilattice_sup α := { sup := λ a b, gi.choice (l a ⊔ l b) $ (sup_le (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_left) (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_right)), le_sup_left := by simp only [gi.choice_eq]; exact λ a b, gi.gc.le_u le_sup_left, le_sup_right := by simp only [gi.choice_eq]; exact λ a b, gi.gc.le_u le_sup_right, sup_le := by simp only [gi.choice_eq]; exact λ a b c hac hbc, (gi.gc.monotone_u $ sup_le (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc)).trans (gi.u_l_le c), .. ‹partial_order α› } /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice [lattice β] (gi : galois_coinsertion l u) : lattice α := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot [order_bot β] (gi : galois_coinsertion l u) : order_bot α := { bot := gi.choice ⊥ $ bot_le, bot_le := by simp only [gi.choice_eq]; exact λ b, (gi.gc.monotone_u bot_le).trans (gi.u_l_le b), .. ‹partial_order α› } /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice [bounded_lattice β] (gi : galois_coinsertion l u) : bounded_lattice α := { .. gi.lift_lattice, .. gi.lift_order_bot, .. gi.gc.lift_order_top } /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice [complete_lattice β] (gi : galois_coinsertion l u) : complete_lattice α := { Inf := λ s, u (⨅ a ∈ s, l a), le_Inf := λ s a hs, gi.gc.le_u $ le_infi $ λ b, le_infi $ λ hb, gi.gc.monotone_l $ hs _ hb, Inf_le := λ s a ha, (gi.gc.monotone_u $ infi_le_of_le a $ infi_le_of_le ha $ le_refl (l a)).trans (gi.u_l_le a), Sup := λ s, gi.choice (⨆ a ∈ s, l a) $ supr_le $ λ b, supr_le $ λ hb, gi.gc.monotone_l $ gi.gc.le_u $ le_supr_of_le b $ le_supr_of_le hb $ le_refl _, le_Sup := by simp only [gi.choice_eq]; exact λ s a ha, gi.gc.le_u $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Sup_le := by simp only [gi.choice_eq]; exact λ s a hs, (gi.gc.monotone_u $ supr_le $ λ b, show (⨆ (hb : b ∈ s), l b) ≤ l a, from supr_le $ λ hb, gi.gc.monotone_l $ hs b hb).trans (gi.u_l_le a), .. gi.lift_bounded_lattice } end lift end galois_coinsertion /-- If `α` is a partial order with bottom element (e.g., `ℕ`, `ℝ≥0`), then `λ o : with_bot α, o.get_or_else ⊥` and coercion form a Galois insertion. -/ def with_bot.gi_get_or_else_bot [order_bot α] : galois_insertion (λ o : with_bot α, o.get_or_else ⊥) coe := { gc := λ a b, with_bot.get_or_else_bot_le_iff, le_l_u := λ a, le_rfl, choice := λ o ho, _, choice_eq := λ _ _, rfl }
c397924955f2346d16f82a76d241616c9f446acb
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/library/src_real_field.lean
a6f23330dc2d70912398b742197d79db3c1fbe6e
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
539
lean
import .src_ordered_field namespace mth1001 namespace myreal class myreal_field (R : Type) extends myordered_field R := (nan : R) (completeness : ∀ {S : set R}, S ≠ ∅ → (∃ u : R, upper_bound u S) → ∃ v : R, is_sup v S) variables {R : Type} [myreal_field R] open classical myreal_field local attribute [instance] prop_decidable noncomputable def sup (S : set R) := if h : ∃ x, is_sup x S then some h else nan noncomputable def inf (S : set R) := if h : ∃ x, is_inf x S then some h else nan end myreal end mth1001
4614341731ec23ef278c295727a18bcc4f00d5f6
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/measure_theory/measurable_space.lean
a719dc69c27a61e92436111e5c89ace3ab0d7109
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
40,501
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 Measurable spaces -- σ-algberas -/ import data.set.disjointed order.galois_connection data.set.countable open set lattice encodable local attribute [instance] classical.prop_decidable universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort x} {s t u : set α} structure measurable_space (α : Type u) := (is_measurable : set α → Prop) (is_measurable_empty : is_measurable ∅) (is_measurable_compl : ∀s, is_measurable s → is_measurable (- s)) (is_measurable_Union : ∀f:ℕ → set α, (∀i, is_measurable (f i)) → is_measurable (⋃i, f i)) attribute [class] measurable_space section variable [measurable_space α] /-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `α`) -/ def is_measurable : set α → Prop := ‹measurable_space α›.is_measurable lemma is_measurable.empty : is_measurable (∅ : set α) := ‹measurable_space α›.is_measurable_empty lemma is_measurable.compl : is_measurable s → is_measurable (-s) := ‹measurable_space α›.is_measurable_compl s lemma is_measurable.compl_iff : is_measurable (-s) ↔ is_measurable s := ⟨λ h, by simpa using h.compl, is_measurable.compl⟩ lemma is_measurable.univ : is_measurable (univ : set α) := by simpa using (@is_measurable.empty α _).compl lemma encodable.Union_decode2 {α} [encodable β] (f : β → set α) : (⋃ b, f b) = ⋃ (i : ℕ) (b ∈ decode2 β i), f b := ext $ by simp [mem_decode2, exists_swap] @[elab_as_eliminator] lemma encodable.Union_decode2_cases {α} [encodable β] {f : β → set α} {C : set α → Prop} (H0 : C ∅) (H1 : ∀ b, C (f b)) {n} : C (⋃ b ∈ decode2 β n, f b) := match decode2 β n with | none := by simp; apply H0 | (some b) := by convert H1 b; simp [ext_iff] end lemma is_measurable.Union [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by rw encodable.Union_decode2; exact ‹measurable_space α›.is_measurable_Union (λ n, ⋃ b ∈ decode2 β n, f b) (λ n, encodable.Union_decode2_cases is_measurable.empty h) lemma is_measurable.bUnion {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋃b∈s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact is_measurable.Union (by simpa using h) end lemma is_measurable.sUnion {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋃₀ s) := by rw sUnion_eq_bUnion; exact is_measurable.bUnion hs h lemma is_measurable.Union_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by by_cases p; simp [h, hf, is_measurable.empty] lemma is_measurable.Inter [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := is_measurable.compl_iff.1 $ by rw compl_Inter; exact is_measurable.Union (λ b, (h b).compl) lemma is_measurable.bInter {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋂b∈s, f b) := is_measurable.compl_iff.1 $ by rw compl_bInter; exact is_measurable.bUnion hs (λ b hb, (h b hb).compl) lemma is_measurable.sInter {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋂₀ s) := by rw sInter_eq_bInter; exact is_measurable.bInter hs h lemma is_measurable.Inter_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := by by_cases p; simp [h, hf, is_measurable.univ] lemma is_measurable.union {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∪ s₂) := by rw union_eq_Union; exact is_measurable.Union (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma is_measurable.inter {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∩ s₂) := by rw inter_eq_compl_compl_union_compl; exact (h₁.compl.union h₂.compl).compl lemma is_measurable.diff {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ \ s₂) := h₁.inter h₂.compl lemma is_measurable.sub {s₁ s₂ : set α} : is_measurable s₁ → is_measurable s₂ → is_measurable (s₁ - s₂) := is_measurable.diff lemma is_measurable.disjointed {f : ℕ → set α} (h : ∀i, is_measurable (f i)) (n) : is_measurable (disjointed f n) := disjointed_induct (h n) (assume t i ht, is_measurable.diff ht $ h _) lemma is_measurable.const (p : Prop) : is_measurable {a : α | p} := by by_cases p; simp [h, is_measurable.empty]; apply is_measurable.univ end @[extensionality] lemma measurable_space.ext : ∀{m₁ m₂ : measurable_space α}, (∀s:set α, m₁.is_measurable s ↔ m₂.is_measurable s) → m₁ = m₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this namespace measurable_space section complete_lattice instance : partial_order (measurable_space α) := { le := λm₁ m₂, m₁.is_measurable ≤ m₂.is_measurable, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, measurable_space.ext $ assume s, ⟨h₁ s, h₂ s⟩ } /-- The smallest σ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set α)) : set α → Prop | basic : ∀u∈s, generate_measurable u | empty : generate_measurable ∅ | compl : ∀s, generate_measurable s → generate_measurable (-s) | union : ∀f:ℕ → set α, (∀n, generate_measurable (f n)) → generate_measurable (⋃i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set α)) : measurable_space α := { is_measurable := generate_measurable s, is_measurable_empty := generate_measurable.empty s, is_measurable_compl := generate_measurable.compl, is_measurable_Union := generate_measurable.union } lemma is_measurable_generate_from {s : set (set α)} {t : set α} (ht : t ∈ s) : (generate_from s).is_measurable t := generate_measurable.basic t ht lemma generate_from_le {s : set (set α)} {m : measurable_space α} (h : ∀t∈s, m.is_measurable t) : generate_from s ≤ m := assume t (ht : generate_measurable s t), ht.rec_on h (is_measurable_empty m) (assume s _ hs, is_measurable_compl m s hs) (assume f _ hf, is_measurable_Union m f hf) lemma generate_from_le_iff {s : set (set α)} {m : measurable_space α} : generate_from s ≤ m ↔ s ⊆ {t | m.is_measurable t} := iff.intro (assume h u hu, h _ $ is_measurable_generate_from hu) (assume h, generate_from_le h) protected def mk_of_closure (g : set (set α)) (hg : {t | (generate_from g).is_measurable t} = g) : measurable_space α := { is_measurable := λs, s ∈ g, is_measurable_empty := hg ▸ is_measurable_empty _, is_measurable_compl := hg ▸ is_measurable_compl _, is_measurable_Union := hg ▸ is_measurable_Union _ } lemma mk_of_closure_sets {s : set (set α)} {hs : {t | (generate_from s).is_measurable t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by rw [← hs] {occs := occurrences.pos [1] }; refl def gi_generate_from : galois_insertion (@generate_from α) (λm, {t | @is_measurable α m t}) := { gc := assume s m, generate_from_le_iff, le_l_u := assume m s, is_measurable_generate_from, choice := λg hg, measurable_space.mk_of_closure g $ le_antisymm hg $ generate_from_le_iff.1 $ le_refl _, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space α) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space α) := ⟨⊤⟩ lemma is_measurable_bot_iff {s : set α} : @is_measurable α ⊥ s ↔ (s = ∅ ∨ s = univ) := let b : measurable_space α := { is_measurable := λs, s = ∅ ∨ s = univ, is_measurable_empty := or.inl rfl, is_measurable_compl := by simp [or_imp_distrib] {contextual := tt}, is_measurable_Union := assume f hf, classical.by_cases (assume h : ∃i, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi ▸ le_supr f i) (assume h : ¬ ∃i, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = ⊥, from bot_unique $ assume s hs, hs.elim (assume s, s.symm ▸ @is_measurable_empty _ ⊥) (assume s, s.symm ▸ @is_measurable.univ _ ⊥), this ▸ iff.refl _ @[simp] theorem is_measurable_top {s : set α} : @is_measurable _ ⊤ s := trivial @[simp] theorem is_measurable_inf {m₁ m₂ : measurable_space α} {s : set α} : @is_measurable _ (m₁ ⊓ m₂) s ↔ @is_measurable _ m₁ s ∧ @is_measurable _ m₂ s := iff.rfl @[simp] theorem is_measurable_Inf {ms : set (measurable_space α)} {s : set α} : @is_measurable _ (Inf ms) s ↔ ∀ m ∈ ms, @is_measurable _ m s := show s ∈ (⋂m∈ms, {t | @is_measurable _ m t }) ↔ _, by simp @[simp] theorem is_measurable_infi {ι} {m : ι → measurable_space α} {s : set α} : @is_measurable _ (infi m) s ↔ ∀ i, @is_measurable _ (m i) s := show s ∈ (λm, {s | @is_measurable _ m s }) (infi m) ↔ _, by rw (@gi_generate_from α).gc.u_infi; simp; refl end complete_lattice section functors variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set β` whose preimage under `f` is measurable. -/ protected def map (f : α → β) (m : measurable_space α) : measurable_space β := { is_measurable := λs, m.is_measurable $ f ⁻¹' s, is_measurable_empty := m.is_measurable_empty, is_measurable_compl := assume s hs, m.is_measurable_compl _ hs, is_measurable_Union := assume f hf, by rw [preimage_Union]; exact m.is_measurable_Union _ hf } @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/ protected def comap (f : α → β) (m : measurable_space β) : measurable_space α := { is_measurable := λs, ∃s', m.is_measurable s' ∧ f ⁻¹' s' = s, is_measurable_empty := ⟨∅, m.is_measurable_empty, rfl⟩, is_measurable_compl := assume s ⟨s', h₁, h₂⟩, ⟨-s', m.is_measurable_compl _ h₁, h₂ ▸ rfl⟩, is_measurable_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in ⟨⋃i, s' i, m.is_measurable_Union _ (λi, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩ lemma gc_comap_map (f : α → β) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (⊥:measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ι → measurable_space α} :(⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊤:measurable_space α).map f = ⊤ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm lemma comap_generate_from {f : α → β} {s : set (set β)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [m₁ : measurable_space α] [m₂ : measurable_space β] (f : α → β) : Prop := m₂ ≤ m₁.map f lemma measurable_id [measurable_space α] : measurable (@id α) := le_refl _ lemma measurable.preimage [measurable_space α] [measurable_space β] {f : α → β} (hf : measurable f) {s : set β} : is_measurable s → is_measurable (f ⁻¹' s) := hf _ lemma measurable.comp [measurable_space α] [measurable_space β] [measurable_space γ] {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := le_trans hg $ map_mono hf lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β} (h : ∀t∈s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := generate_from_le h lemma measurable.if [measurable_space α] [measurable_space β] {p : α → Prop} {h : decidable_pred p} {f g : α → β} (hp : is_measurable {a | p a}) (hf : measurable f) (hg : measurable g) : measurable (λa, if p a then f a else g a) := λ s hs, show is_measurable {a | (if p a then f a else g a) ∈ s}, begin convert (hp.inter $ hf s hs).union (hp.compl.inter $ hg s hs), exact ext (λ a, by by_cases p a ; { rw mem_def, simp [h] }) end lemma measurable_const {α β} [measurable_space α] [measurable_space β] {a : α} : measurable (λb:β, a) := assume s hs, show is_measurable {b : β | a ∈ s}, from classical.by_cases (assume h : a ∈ s, by simp [h]; from is_measurable.univ) (assume h : a ∉ s, by simp [h]; from is_measurable.empty) end measurable_functions section constructions instance : measurable_space empty := ⊤ instance : measurable_space unit := ⊤ instance : measurable_space bool := ⊤ instance : measurable_space ℕ := ⊤ instance : measurable_space ℤ := ⊤ lemma measurable_unit [measurable_space α] (f : unit → α) : measurable f := have f = (λu, f ()) := funext $ assume ⟨⟩, rfl, by rw this; exact measurable_const section nat lemma measurable_from_nat [measurable_space α] {f : ℕ → α} : measurable f := assume s hs, show is_measurable {n : ℕ | f n ∈ s}, from trivial lemma measurable_to_nat [measurable_space α] {f : α → ℕ} : (∀ k, is_measurable {x | f x = k}) → measurable f := begin assume h s hs, show is_measurable {x | f x ∈ s}, have : {x | f x ∈ s} = ⋃ (n ∈ s), {x | f x = n}, { ext, simp }, rw this, simp [is_measurable.Union, is_measurable.Union_Prop, h] end lemma measurable_find_greatest [measurable_space α] {p : ℕ → α → Prop} : ∀ {N}, (∀ k ≤ N, is_measurable {x | nat.find_greatest (λ n, p n x) N = k}) → measurable (λ x, nat.find_greatest (λ n, p n x) N) | 0 := assume h s hs, show is_measurable {x : α | (nat.find_greatest (λ n, p n x) 0) ∈ s}, begin by_cases h : 0 ∈ s, { convert is_measurable.univ, simp only [nat.find_greatest_zero, h] }, { convert is_measurable.empty, simp only [nat.find_greatest_zero, h], refl } end | (n + 1) := assume h, begin apply measurable_to_nat, assume k, by_cases hk : k ≤ n + 1, { exact h k hk }, { have := is_measurable.empty, rw ← set_of_false at this, convert this, funext, rw eq_false, assume h, rw ← h at hk, have := nat.find_greatest_le, contradiction } end end nat section subtype instance {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) := m.comap subtype.val lemma measurable_subtype_val [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → subtype p} (hf : measurable f) : measurable (λa:α, (f a).val) := measurable.comp (measurable_space.comap_le_iff_le_map.mp (le_refl _)) hf lemma measurable_subtype_mk [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → subtype p} (hf : measurable (λa, (f a).val)) : measurable f := measurable_space.comap_le_iff_le_map.mpr $ by rw [measurable_space.map_comp]; exact hf lemma is_measurable_subtype_image [measurable_space α] {s : set α} {t : set s} (hs : is_measurable s) : is_measurable t → is_measurable ((coe : s → α) '' t) | ⟨u, (hu : is_measurable u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, image_preimage_eq_inter_range, range_coe_subtype], exact is_measurable.inter hu hs end lemma measurable_of_measurable_union_cover [measurable_space α] [measurable_space β] {f : α → β} (s t : set α) (hs : is_measurable s) (ht : is_measurable t) (h : univ ⊆ s ∪ t) (hc : measurable (λa:s, f a)) (hd : measurable (λa:t, f a)) : measurable f := assume u (hu : is_measurable u), show is_measurable (f ⁻¹' u), from begin rw show f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), by rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, range_coe_subtype, range_coe_subtype, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], exact is_measurable.union (is_measurable_subtype_image hs (hc _ hu)) (is_measurable_subtype_image ht (hd _ hu)) end end subtype section prod instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) := m₁.comap prod.fst ⊔ m₂.comap prod.snd lemma measurable_fst [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).1) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_left) hf lemma measurable_snd [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).2) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_right) hf lemma measurable.prod [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf₁ : measurable (λa, (f a).1)) (hf₂ : measurable (λa, (f a).2)) : measurable f := sup_le (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₁) (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₂) lemma measurable_prod_mk [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λa:α, (f a, g a)) := measurable.prod hf hg lemma is_measurable_set_prod [measurable_space α] [measurable_space β] {s : set α} {t : set β} (hs : is_measurable s) (ht : is_measurable t) : is_measurable (set.prod s t) := is_measurable.inter (measurable_fst measurable_id _ hs) (measurable_snd measurable_id _ ht) end prod instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) := m₁.map sum.inl ⊓ m₂.map sum.inr section sum variables [measurable_space α] [measurable_space β] [measurable_space γ] lemma measurable_inl : measurable (@sum.inl α β) := inf_le_left lemma measurable_inr : measurable (@sum.inr α β) := inf_le_right lemma measurable_sum {f : α ⊕ β → γ} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable_space.comap_le_iff_le_map.1 $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable_sum_rec {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) : @measurable (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := measurable_sum hf hg lemma is_measurable_inl_image [measurable_space α] [measurable_space β] {s : set α} (hs : is_measurable s) : is_measurable (sum.inl '' s : set (α ⊕ β)) := ⟨show is_measurable (sum.inl ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inl.inj), have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inr ⁻¹' _), by rw [this]; exact is_measurable.empty⟩ lemma is_measurable_range_inl [measurable_space α] [measurable_space β] : is_measurable (range sum.inl : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inl_image is_measurable.univ lemma is_measurable_inr_image [measurable_space α] [measurable_space β] {s : set β} (hs : is_measurable s) : is_measurable (sum.inr '' s : set (α ⊕ β)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inl ⁻¹' _), by rw [this]; exact is_measurable.empty, show is_measurable (sum.inr ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inr.inj)⟩ lemma is_measurable_range_inr [measurable_space α] [measurable_space β] : is_measurable (range sum.inr : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inr_image is_measurable.univ end sum instance {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) := ⨅a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) namespace measurable_equiv instance (α β) [measurable_space α] [measurable_space β] : has_coe_to_fun (measurable_equiv α β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : (e : α → β) = e.to_equiv := rfl def refl (α : Type*) [measurable_space α] : measurable_equiv α α := { to_equiv := equiv.refl α, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } def trans [measurable_space α] [measurable_space β] [measurable_space γ] (ab : measurable_equiv α β) (bc : measurable_equiv β γ) : measurable_equiv α γ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } lemma trans_to_equiv {α β} [measurable_space α] [measurable_space β] [measurable_space γ] (e : measurable_equiv α β) (f : measurable_equiv β γ) : (e.trans f).to_equiv = e.to_equiv.trans f.to_equiv := rfl def symm [measurable_space α] [measurable_space β] (ab : measurable_equiv α β) : measurable_equiv β α := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } lemma symm_to_equiv {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : e.symm.to_equiv = e.to_equiv.symm := rfl protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β] (h : α = β) (hi : i₁ == i₂) : measurable_equiv α β := { to_equiv := equiv.cast h, measurable_to_fun := by unfreezeI; subst h; subst hi; exact measurable_id, measurable_inv_fun := by unfreezeI; subst h; subst hi; exact measurable_id } protected lemma measurable {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : measurable (e : α → β) := e.measurable_to_fun protected lemma measurable_coe_iff {α β γ} [measurable_space α] [measurable_space β] [measurable_space γ] {f : β → γ} (e : measurable_equiv α β) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (λh, h.comp e.measurable) def prod_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α × γ) (β × δ) := { to_equiv := equiv.prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := measurable_prod_mk (ab.measurable_to_fun.comp (measurable_fst measurable_id)) (cd.measurable_to_fun.comp (measurable_snd measurable_id)), measurable_inv_fun := measurable_prod_mk (ab.measurable_inv_fun.comp (measurable_fst measurable_id)) (cd.measurable_inv_fun.comp (measurable_snd measurable_id)) } def prod_comm [measurable_space α] [measurable_space β] : measurable_equiv (α × β) (β × α) := { to_equiv := equiv.prod_comm α β, measurable_to_fun := measurable_prod_mk (measurable_snd measurable_id) (measurable_fst measurable_id), measurable_inv_fun := measurable_prod_mk (measurable_snd measurable_id) (measurable_fst measurable_id) } def sum_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α ⊕ γ) (β ⊕ δ) := { to_equiv := equiv.sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } def set.prod [measurable_space α] [measurable_space β] (s : set α) (t : set β) : measurable_equiv (set.prod s t) (s × t) := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_prod_mk (measurable_subtype_mk $ measurable_fst $ measurable_subtype_val $ measurable_id) (measurable_subtype_mk $ measurable_snd $ measurable_subtype_val $ measurable_id), measurable_inv_fun := measurable_subtype_mk $ measurable_prod_mk (measurable_subtype_val $ measurable_fst $ measurable_id) (measurable_subtype_val $ measurable_snd $ measurable_id) } def set.univ (α : Type*) [measurable_space α] : measurable_equiv (univ : set α) α := { to_equiv := equiv.set.univ α, measurable_to_fun := measurable_subtype_val measurable_id, measurable_inv_fun := measurable_subtype_mk measurable_id } def set.singleton [measurable_space α] (a:α) : measurable_equiv ({a} : set α) unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_subtype_mk $ show measurable (λu:unit, a), from measurable_const } noncomputable def set.image [measurable_space α] [measurable_space β] (f : α → β) (s : set α) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv s (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := begin have : measurable (λa:s, f a) := hfm.comp (measurable_subtype_val measurable_id), refine measurable_subtype_mk _, convert this, ext ⟨a, h⟩, refl end, measurable_inv_fun := assume t ⟨u, (hu : is_measurable u), eq⟩, begin clear_, subst eq, show is_measurable {x : f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u}, have : ∀(a ∈ s) (h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λa ha h, (classical.some_spec h).2, rw show {x:f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u} = subtype.val ⁻¹' (f '' u), by ext ⟨b, a, hbs, rfl⟩; simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this _ hbs], exact (measurable_subtype_val measurable_id) (f '' u) (hfi u hu) end } noncomputable def set.range [measurable_space α] [measurable_space β] (f : α → β) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv α (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) def set.range_inl [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inl : set (α ⊕ β)) α := { to_fun := λab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λa, ⟨sum.inl a, a, rfl⟩, left_inv := assume ⟨ab, a, eq⟩, by subst eq; refl, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inl_image hs, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable_subtype_mk measurable_inl } def set.range_inr [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inr : set (α ⊕ β)) β := { to_fun := λab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λb, ⟨sum.inr b, b, rfl⟩, left_inv := assume ⟨ab, b, eq⟩, by subst eq; refl, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable_subtype_mk measurable_inr } def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv ((α ⊕ β) × γ) ((α × γ) ⊕ (β × γ)) := { to_equiv := equiv.sum_prod_distrib α β γ, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (is_measurable_set_prod is_measurable_range_inl is_measurable.univ) (is_measurable_set_prod is_measurable_range_inr is_measurable.univ) (assume ⟨ab, c⟩ s, by cases ab; simp [set.prod_eq]) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := begin refine measurable_sum _ _, { convert measurable_prod_mk (measurable_inl.comp (measurable_fst measurable_id)) (measurable_snd measurable_id), ext ⟨a, c⟩; refl }, { convert measurable_prod_mk (measurable_inr.comp (measurable_fst measurable_id)) (measurable_snd measurable_id), ext ⟨b, c⟩; refl } end } def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv (α × (β ⊕ γ)) ((α × β) ⊕ (α × γ)) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm def sum_prod_sum (α β γ δ) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] : measurable_equiv ((α ⊕ β) × (γ ⊕ δ)) (((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ))) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) end measurable_equiv namespace measurable_equiv end measurable_equiv namespace measurable_space /-- Dynkin systems The main purpose of Dynkin systems is to provide a powerful induction rule for σ-algebras generated by intersection stable set systems. -/ structure dynkin_system (α : Type*) := (has : set α → Prop) (has_empty : has ∅) (has_compl : ∀{a}, has a → has (-a)) (has_Union_nat : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, has (f i)) → has (⋃i, f i)) theorem Union_decode2_disjoint_on {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) : pairwise (disjoint on λ i, ⋃ b ∈ decode2 β i, f b) := begin rintro i j ij x ⟨h₁, h₂⟩, revert h₁ h₂, simp, intros b₁ e₁ h₁ b₂ e₂ h₂, refine hd _ _ _ ⟨h₁, h₂⟩, cases encodable.mem_decode2.1 e₁, cases encodable.mem_decode2.1 e₂, exact mt (congr_arg _) ij end namespace dynkin_system @[extensionality] lemma ext : ∀{d₁ d₂ : dynkin_system α}, (∀s:set α, d₁.has s ↔ d₂.has s) → d₁ = d₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system α) lemma has_compl_iff {a} : d.has (-a) ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, λ h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (h : ∀i, d.has (f i)) : d.has (⋃i, f i) := by rw encodable.Union_decode2; exact d.has_Union_nat (Union_decode2_disjoint_on hd) (λ n, encodable.Union_decode2_cases d.has_empty h) theorem has_union {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₁ ∩ s₂ ⊆ ∅) : d.has (s₁ ∪ s₂) := by rw union_eq_Union; exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma has_diff {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₂ ⊆ s₁) : d.has (s₁ \ s₂) := d.has_compl_iff.1 begin simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) h₂ (λ x ⟨h₁, h₂⟩, h₁ (h h₂)), end instance : partial_order (dynkin_system α) := { le := λm₁ m₂, m₁.has ≤ m₂.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, ext $ assume s, ⟨h₁ s, h₂ s⟩ } def of_measurable_space (m : measurable_space α) : dynkin_system α := { has := m.is_measurable, has_empty := m.is_measurable_empty, has_compl := m.is_measurable_compl, has_Union_nat := assume f _ hf, m.is_measurable_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ m₂ : measurable_space α} : of_measurable_space m₁ ≤ of_measurable_space m₂ ↔ m₁ ≤ m₂ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. -/ inductive generate_has (s : set (set α)) : set α → Prop | basic : ∀t∈s, generate_has t | empty : generate_has ∅ | compl : ∀{a}, generate_has a → generate_has (-a) | Union : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, generate_has (f i)) → generate_has (⋃i, f i) def generate (s : set (set α)) : dynkin_system α := { has := generate_has s, has_empty := generate_has.empty s, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } def to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) := { measurable_space . is_measurable := d.has, is_measurable_empty := d.has_empty, is_measurable_compl := assume s h, d.has_compl h, is_measurable_Union := assume f hf, have ∀n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl def restrict_on {s : set α} (h : d.has s) : dynkin_system α := { has := λt, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have -t ∩ s = (- (t ∩ s)) \ -s, from set.ext $ assume x, by by_cases x ∈ s; simp [h], by rw [this]; from d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _), has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact λ i j h x ⟨⟨_, h₁⟩, _, h₂⟩, hd i j h ⟨h₁, h₂⟩ }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set α)} (h : ∀t∈s, d.has t) : generate s ≤ d := λ t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_inter {s : set (set α)} (hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) {t₁ t₂ : set α} (ht₁ : (generate s).has t₁) (ht₂ : (generate s).has t₂) : (generate s).has (t₁ ∩ t₂) := have generate s ≤ (generate s).restrict_on ht₂, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≤ (generate s).restrict_on this, from generate_le _ $ assume s₂ hs₂, show (generate s).has (s₂ ∩ s₁), from if h : s₂ ∩ s₁ = ∅ then by rw [h]; exact generate_has.empty _ else generate_has.basic _ (hs _ _ hs₂ hs₁ h), have (generate s).has (t₂ ∩ s₁), from this _ ht₂, show (generate s).has (s₁ ∩ t₂), by rwa [inter_comm], this _ ht₁ lemma generate_from_eq {s : set (set α)} (hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) : generate_from s = (generate s).to_measurable_space (assume t₁ t₂, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by rw [of_measurable_space_to_measurable_space]; from (generate_le _ $ assume t ht, is_measurable_generate_from ht)) end dynkin_system lemma induction_on_inter {C : set α → Prop} {s : set (set α)} {m : measurable_space α} (h_eq : m = generate_from s) (h_inter : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) (h_empty : C ∅) (h_basic : ∀t∈s, C t) (h_compl : ∀t, m.is_measurable t → C t → C (- t)) (h_union : ∀f:ℕ → set α, (∀i j, i ≠ j → f i ∩ f j ⊆ ∅) → (∀i, m.is_measurable (f i)) → (∀i, C (f i)) → C (⋃i, f i)) : ∀{t}, m.is_measurable t → C t := have eq : m.is_measurable = dynkin_system.generate_has s, by rw [h_eq, dynkin_system.generate_from_eq h_inter]; refl, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by rw [eq]; exact ht) (assume f hf ht, h_union f hf $ assume i, by rw [eq]; exact ht _) end measurable_space
b516d55a405cf17c905e7bb64185ad46ffd3d903
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world06/level06.lean
4c04767aadee83ed68aada1ea8420c843c9d82e4
[]
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
131
lean
example (P Q R : Prop) : (P → (Q → R)) → ((P → Q) → (P → R)) := begin intros f h p, apply (f p), apply h, exact p, end
39d303a75083a2fdd9e7c13cf8705e5fbc00aa41
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/data/real/nnreal.lean
17a8deea2d6d3c703e4d8c4cf3e0c4954586f8b8
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,789
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.real.basic import data.indicator_function /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `archimedean ℝ≥0`. * `nnreal.of_real x` is defined as `⟨max x 0, _⟩`, i.e. `↑(nnreal.of_real x) = x` when `0 ≤ x` and `↑(nnreal.of_real x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. -/ noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance : can_lift ℝ ℝ≥0 := { coe := coe, cond := λ r, 0 ≤ r, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ protected lemma injective_coe : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.injective_coe.eq_iff @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { exists_pair_ne := ⟨0, 1, assume h, zero_ne_one $ nnreal.eq_iff.2 h⟩, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma of_real_sum_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : nnreal.of_real (∑ a in s, f a) = ∑ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, nnreal.coe_of_real _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (λ x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma of_real_prod_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : nnreal.of_real (∏ a in s, f a) = ∏ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, nnreal.coe_of_real _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (λ x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n •ℕ r) = n •ℕ (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : linear_order ℝ≥0 := linear_order.lift (coe : ℝ≥0 → ℝ) nnreal.injective_coe @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : ℕ) : @eq ℝ≥0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝ≥0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma of_real_coe_nat (n : ℕ) : nnreal.of_real n = n := nnreal.eq $ by simp [coe_of_real] /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.linear_order } instance : canonically_linear_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_le_one := @zero_le_one ℝ _, exists_pair_ne := ⟨0, 1, ne_of_lt (@zero_lt_one ℝ _ _)⟩, .. nnreal.canonically_linear_ordered_add_monoid, .. nnreal.comm_semiring, } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.canonically_linear_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := exists_between h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := rfl lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : ℝ≥0 → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : ℝ≥0 → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_linear_order, .. nnreal.order_bot } instance : archimedean ℝ≥0 := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : ℝ≥0), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_pos_le_add {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end -- TODO: generalize to some ordered add_monoids, based on #6145 lemma le_of_add_le_left {a b c : ℝ≥0} (h : a + b ≤ c) : a ≤ c := by { refine le_trans _ h, simp } lemma le_of_add_le_right {a b c : ℝ≥0} (h : a + b ≤ c) : b ≤ c := by { refine le_trans _ h, simp } lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma le_of_real_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_of_real_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, of_real_eq_zero.2 hp.le, hr.not_le] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [nnreal.of_real, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : ℝ≥0} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : ℝ≥0} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : ℝ≥0} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : ℝ≥0} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] lemma sub_add_eq_max {r p : ℝ≥0} : (r - p) + p = max r p := nnreal.eq $ by rw [sub_def, nnreal.coe_add, coe_max, nnreal.of_real, coe_mk, ← max_add_add_right, zero_add, sub_add_cancel] lemma add_sub_eq_max {r p : ℝ≥0} : p + (r - p) = max p r := by rw [add_comm, sub_add_eq_max, max_comm] @[simp] lemma sub_add_cancel_of_le {a b : ℝ≥0} (h : b ≤ a) : (a - b) + b = a := by rw [sub_add_eq_max, max_eq_left h] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : ℝ≥0} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : ℝ≥0) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end lemma sub_lt_iff_lt_add {a b c : ℝ≥0} (h : b ≤ a) : a - b < c ↔ a < b + c := by simp only [←nnreal.coe_lt_coe, nnreal.coe_sub h, nnreal.coe_add, sub_lt_iff_lt_add'] lemma sub_eq_iff_eq_add {a b c : ℝ≥0} (h : b ≤ a) : a - b = c ↔ a = c + b := by rw [←nnreal.eq_iff, nnreal.coe_sub h, ←nnreal.eq_iff, nnreal.coe_add, sub_eq_iff_eq_add] end sub section inv lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [div_eq_mul_inv, finset.sum_mul] @[simp] lemma inv_pos {r : ℝ≥0} : 0 < r⁻¹ ↔ 0 < r := by simp [pos_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := by simpa only [div_eq_mul_inv] using mul_pos hr (inv_pos.2 hp) protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_eq_inv_mul, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma lt_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma mul_lt_of_lt_div {a b r : ℝ≥0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ λ hr, false.elim _).1 h, subst r, simpa using h end lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv'], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := lt_iff_lt_of_le_iff_le $ nnreal.le_div_iff_mul_le hc lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma of_real_inv {x : ℝ} : nnreal.of_real x⁻¹ = (nnreal.of_real x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← coe_of_real x hx, rw [←nnreal.coe_inv, of_real_coe], }, { have hx' := le_of_not_ge hx, rw [of_real_eq_zero.mpr hx', inv_zero, of_real_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma of_real_div {x y : ℝ} (hx : 0 ≤ x) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, ←of_real_inv, ←of_real_mul hx] lemma of_real_div' {x y : ℝ} (hy : 0 ≤ y) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_inv_mul, div_eq_inv_mul, of_real_mul (inv_nonneg.2 hy), of_real_inv] end inv @[simp] lemma abs_eq (x : ℝ≥0) : abs (x : ℝ) = x := abs_of_nonneg x.property end nnreal /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] def real.nnabs (x : ℝ) : ℝ≥0 := ⟨abs x, abs_nonneg x⟩ @[norm_cast, simp] lemma nnreal.coe_nnabs (x : ℝ) : (real.nnabs x : ℝ) = abs x := by simp [real.nnabs]
5ba3c370138d9bd57db6074ebeb243ee9fb701c4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/quadratic_discriminant.lean
85e7ced68ae97ae723e862bed1c05482a1269f6f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,343
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.invertible import Mathlib.tactic.linarith.default import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Quadratic discriminants and roots of a quadratic This file defines the discriminant of a quadratic and gives the solution to a quadratic equation. ## Main definition - `discrim a b c`: the discriminant of a quadratic `a * x * x + b * x + c` is `b * b - 4 * a * c`. ## Main statements - `quadratic_eq_zero_iff`: roots of a quadratic can be written as `(-b + s) / (2 * a)` or `(-b - s) / (2 * a)`, where `s` is a square root of the discriminant. - `quadratic_ne_zero_of_discrim_ne_square`: if the discriminant has no square root, then the corresponding quadratic has no root. - `discrim_le_zero`: if a quadratic is always non-negative, then its discriminant is non-positive. ## Tags polynomial, quadratic, discriminant, root -/ /-- Discriminant of a quadratic -/ def discrim {R : Type u_1} [ring R] (a : R) (b : R) (c : R) : R := b ^ bit0 1 - bit0 (bit0 1) * a * c /-- A quadratic has roots if and only if its discriminant equals some square. -/ theorem quadratic_eq_zero_iff_discrim_eq_square {R : Type u_1} [integral_domain R] {a : R} {b : R} {c : R} (h2 : bit0 1 ≠ 0) (ha : a ≠ 0) (x : R) : a * x * x + b * x + c = 0 ↔ discrim a b c = (bit0 1 * a * x + b) ^ bit0 1 := sorry /-- A quadratic has no root if its discriminant has no square root. -/ theorem quadratic_ne_zero_of_discrim_ne_square {R : Type u_1} [integral_domain R] {a : R} {b : R} {c : R} (h2 : bit0 1 ≠ 0) (ha : a ≠ 0) (h : ∀ (s : R), discrim a b c ≠ s * s) (x : R) : a * x * x + b * x + c ≠ 0 := sorry /-- Roots of a quadratic -/ theorem quadratic_eq_zero_iff {K : Type u_1} [field K] [invertible (bit0 1)] {a : K} {b : K} {c : K} (ha : a ≠ 0) {s : K} (h : discrim a b c = s * s) (x : K) : a * x * x + b * x + c = 0 ↔ x = (-b + s) / (bit0 1 * a) ∨ x = (-b - s) / (bit0 1 * a) := sorry /-- A quadratic has roots if its discriminant has square roots -/ theorem exists_quadratic_eq_zero {K : Type u_1} [field K] [invertible (bit0 1)] {a : K} {b : K} {c : K} (ha : a ≠ 0) (h : ∃ (s : K), discrim a b c = s * s) : ∃ (x : K), a * x * x + b * x + c = 0 := sorry /-- Root of a quadratic when its discriminant equals zero -/ theorem quadratic_eq_zero_iff_of_discrim_eq_zero {K : Type u_1} [field K] [invertible (bit0 1)] {a : K} {b : K} {c : K} (ha : a ≠ 0) (h : discrim a b c = 0) (x : K) : a * x * x + b * x + c = 0 ↔ x = -b / (bit0 1 * a) := sorry /-- If a polynomial of degree 2 is always nonnegative, then its discriminant is nonpositive -/ theorem discrim_le_zero {K : Type u_1} [linear_ordered_field K] {a : K} {b : K} {c : K} (h : ∀ (x : K), 0 ≤ a * x * x + b * x + c) : discrim a b c ≤ 0 := sorry /-- If a polynomial of degree 2 is always positive, then its discriminant is negative, at least when the coefficient of the quadratic term is nonzero. -/ theorem discrim_lt_zero {K : Type u_1} [linear_ordered_field K] {a : K} {b : K} {c : K} (ha : a ≠ 0) (h : ∀ (x : K), 0 < a * x * x + b * x + c) : discrim a b c < 0 := sorry
738ad5b099f14d8efe59e16fa5505524b2256f82
4f065978c49388d188224610d9984673079f7d91
/free_group.lean
91ee380d5ec36848f75d91775617abf7b651063f
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
28,325
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.group algebra.group_power import data.equiv data.fintype data.list.basic data.quot import group_theory.subgroup universes u v w variables {α : Type u} lemma list.append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl lemma list.infix_cons {L₁ L₂ : list α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ := λ ⟨LP, LS, H⟩, ⟨x :: LP, LS, eq.rec_on H rfl⟩ local attribute [simp] list.append_eq_has_append namespace is_group_hom variables {β : Type v} [group α] [group β] variables (f : α → β) [is_group_hom f] instance range_subgroup : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ end is_group_hom namespace free_group /-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words `w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/ inductive red.step : list (α × bool) → list (α × bool) → Prop | bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂) /-- Reflexive-transitive closure of red.step -/ inductive red : list (α × bool) → list (α × bool) → Prop | refl {L} : red L L | step_trans {L₁ L₂ L₃} (H : free_group.red.step L₁ L₂) : red L₂ L₃ → red L₁ L₃ attribute [simp] red.step.bnot attribute [refl] red.refl variables {L L₁ L₂ L₃ L₄ : list (α × bool)} theorem red.trans.aux (H12 : red L₁ L₂) : ∀ {L₃}, red L₂ L₃ → red L₁ L₃ := red.rec_on H12 (λ _ _, id) $ λ _ _ _ H1 H2 ih L₃ H23, red.step_trans H1 $ ih H23 @[trans] theorem red.trans (H12 : red L₁ L₂) (H23 : red L₂ L₃) : red L₁ L₃ := red.trans.aux H12 H23 @[simp] lemma red.step.bnot_rev {x b} : red.step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) := by cases b; from step.bnot theorem red.step.length : ∀ {L₁ L₂ : list (α × bool)}, red.step L₁ L₂ → L₂.length + 2 = L₁.length | _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl theorem red.sizeof : ∀ {L₁ L₂ : list (α × bool)}, red.step L₁ L₂ → L₂.sizeof < L₁.sizeof | _ _ (@red.step.bnot _ L1 L2 x b) := begin induction L1 with hd tl ih, case list.nil { dsimp [list.sizeof], have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2) = (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1), { ac_refl }, rw H, exact nat.le_add_right _ _ }, case list.cons { dsimp [list.sizeof], exact nat.add_lt_add_left ih _ } end theorem red.of_step (H : red.step L₁ L₂) : red L₁ L₂ := red.step_trans H red.refl @[simp] lemma red.bnot {x b} : red (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂) := red.step_trans red.step.bnot red.refl @[simp] lemma red.step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L := @red.step.bnot _ [] _ _ _ @[simp] lemma red.cons_bnot {x b} : red ((x, b) :: (x, bnot b) :: L) L := @red.bnot _ [] _ _ _ @[simp] lemma red.cons_bnot_rev {x b} : red ((x, bnot b) :: (x, b) :: L) L := red.of_step $ @red.step.bnot_rev _ [] _ _ _ theorem red.step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, red.step L₂ L₃ → red.step (L₁ ++ L₂) (L₁ ++ L₃) | _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor theorem red.step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, red.step L₁ L₂ → red.step (L₁ ++ L₃) (L₂ ++ L₃) | _ _ _ red.step.bnot := by simp theorem red.step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) := @red.step.append_left _ [x] _ _ H theorem red.append : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)}, red L₁ L₃ → red L₂ L₄ → red (L₁ ++ L₂) (L₃ ++ L₄) | _ _ _ _ red.refl red.refl := red.refl | _ _ _ _ red.refl (red.step_trans H3 H4) := have _ := red.sizeof H3, red.step_trans (red.step.append_left H3) (red.append red.refl H4) | _ _ _ _ (red.step_trans H1 H2) H3 := have _ := red.sizeof H1, red.step_trans (red.step.append_right H1) (red.append H2 H3) theorem red.cons {x} (H : red L₁ L₂) : red (x :: L₁) (x :: L₂) := @red.append _ [x] _ _ _ red.refl H theorem red.of_cons {x} : red (x :: L₁) (x :: L₂) → red L₁ L₂ := begin generalize H1 : (x :: L₁ : list _) = L1, generalize H2 : (x :: L₂ : list _) = L2, intro H, induction H with L3 L3 L4 L5 H3 H4 ih generalizing x L₁ L₂, case red.refl { subst H1; injections; subst_vars }, case red.step_trans { cases H3 with L6 L7 x1 b1, subst_vars, cases L6 with hd tl, case list.nil { injection H1 with H5 H6, substs H5 H6, clear H1 H3, transitivity, { exact red.cons H4 }, { simp } }, case list.cons { injection H1 with H5 H6, substs H5 H6, exact red.trans red.bnot (ih rfl rfl) } } end @[simp] lemma red.cons_iff {x} : red (x :: L₁) (x :: L₂) ↔ red L₁ L₂ := ⟨red.of_cons, red.cons⟩ theorem red.length (H : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n := red.rec_on H (λ _, ⟨0, rfl⟩) $ λ _ _ _ H1 H2 ⟨n, ih⟩, ⟨nat.succ n, red.step.length H1 ▸ ih.symm ▸ rfl⟩ theorem red.antisymm (H : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ := red.rec_on H (λ _ _, rfl) $ λ L1 L2 L3 H1 H2 ih H21, let ⟨n, hn⟩ := red.length (red.trans H2 H21) in have H3 : list.length L2 = list.length L2 + 2 + 2 * n, from (red.step.length H1).symm ▸ hn, have H4 : list.length L2 + 0 = list.length L2 + (2 * n + 2), by simpa using H3, nat.no_confusion $ nat.add_left_cancel H4 theorem red.step.church_rosser.aux2 : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2}, L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ → L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅ | [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp | [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩ | ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩ | ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H := let ⟨H1, H2⟩ := list.cons.inj H in match red.step.church_rosser.aux2 H2 with | or.inl H3 := or.inl $ by simp [H1, H3] | or.inr ⟨L₅, H3, H4⟩ := or.inr ⟨_, red.step.cons H3, by simpa [H1] using red.step.cons H4⟩ end theorem red.step.church_rosser.aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)}, red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ → L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅ | _ _ _ _ red.step.bnot red.step.bnot H := red.step.church_rosser.aux2 H theorem red.step.church_rosser (H12 : red.step L₁ L₂) (H13 : red.step L₁ L₃) : L₂ = L₃ ∨ ∃ L₄, red.step L₂ L₄ ∧ red.step L₃ L₄ := red.step.church_rosser.aux H12 H13 rfl theorem church_rosser_1 : ∀ {L₁ L₂ L₃ : list (α × bool)}, red.step L₁ L₂ → red L₁ L₃ → red L₂ L₃ ∨ ∃ L₄, red L₂ L₄ ∧ step L₃ L₄ | _ _ _ H12 red.refl := or.inr ⟨_, red.refl, H12⟩ | _ _ _ H12 (red.step_trans H1 H2) := have _ := red.sizeof H1, match red.step.church_rosser H12 H1 with | or.inl H3 := or.inl $ H3.symm ▸ H2 | or.inr ⟨L1, H3, H4⟩ := match church_rosser_1 H4 H2 with | or.inl H5 := or.inl $ red.step_trans H3 H5 | or.inr ⟨L2, H5, H6⟩ := or.inr $ ⟨L2, red.step_trans H3 H5, H6⟩ end end /-- Church-Rosser theorem for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2` and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` respectively. -/ theorem church_rosser : ∀ {L₁ L₂ L₃ : list (α × bool)}, red L₁ L₂ → red L₁ L₃ → ∃ L₄, red L₂ L₄ ∧ red L₃ L₄ | _ _ _ red.refl H23 := ⟨_, H23, red.refl⟩ | _ _ _ (red.step_trans H1 H2) H23 := have _ := red.sizeof H1, match church_rosser_1 H1 H23 with | or.inl H3 := church_rosser H2 H3 | or.inr ⟨L7, H3, H4⟩ := let ⟨L8, H5, H6⟩ := church_rosser H2 H3 in ⟨L8, H5, red.step_trans H4 H6⟩ end end free_group variable α /-- The free group over a type, i.e. the words formed by the elements of the type and their formal inverses, quotient by one step reduction. -/ def free_group : Type u := quot $ @free_group.red.step α namespace free_group variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)} def mk (L) : free_group α := quot.mk red.step L @[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl @[simp] lemma quot_lift_mk (β : Type*) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift f H (mk L) = f L := rfl @[simp] lemma quot_lift_on_mk (β : Type*) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift_on (mk L) f H = f L := rfl def inv : list (α × bool) → list (α × bool) := λ L, (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse theorem red.step.inv (H : red.step L₁ L₂) : red.step (inv L₁) (inv L₂) := by cases H; simp [inv] instance : group (free_group α) := { mul := λ x y, quot.lift_on x (λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H)) (λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H), mul_assoc := λ x y z, quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, quot.induction_on z $ λ L₃, by simp, one := mk [], one_mul := λ x, quot.induction_on x $ λ L, rfl, mul_one := λ x, quot.induction_on x $ λ L, by simp [(*)], inv := λ x, quot.lift_on x (λ L, mk $ inv L) (λ L₁ L₂ H, quot.sound $ red.step.inv H), mul_left_inv := λ x, quot.induction_on x $ λ L, list.rec_on L rfl $ λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [inv]) ih } @[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl /-- The canonical injection from the type to the free group over that type by sending each element to the equivalence class of the letter that is the element. -/ def of (x : α) : free_group α := mk [(x, tt)] theorem red.step.eqv_gen_of_red (H : red L₁ L₂) : eqv_gen red.step L₁ L₂ := red.rec_on H (λ _, eqv_gen.refl _) $ λ _ _ _ H1 _ ih, eqv_gen.trans _ _ _ (eqv_gen.rel _ _ H1) ih theorem red.step.exact (H : mk L₁ = mk L₂) : ∃ L₃, red L₁ L₃ ∧ red L₂ L₃ := eqv_gen.rec_on (quot.exact red.step H) (λ L1 L2 H12, ⟨L2, red.of_step H12, red.refl⟩) (λ L, ⟨L, red.refl, red.refl⟩) (λ L1 L2 H12 ⟨L3, H13, H23⟩, ⟨L3, H23, H13⟩) (λ L1 L2 L3 H12 H13 ⟨L4, H14, H24⟩ ⟨L5, H25, H35⟩, let ⟨L6, H46, H56⟩ := church_rosser H24 H25 in ⟨L6, red.trans H14 H46, red.trans H35 H56⟩) theorem red.step.sound (L₃) (H13 : red L₁ L₃) (H23 : red L₂ L₃) : mk L₁ = mk L₂ := quot.eqv_gen_sound $ eqv_gen.trans _ _ _ (red.step.eqv_gen_of_red H13) (eqv_gen.symm _ _ $ red.step.eqv_gen_of_red H23) theorem red.nil.aux : ∀ {L₁ L₂ : list (α × bool)}, red L₁ L₂ → L₁ = [] → [] = L₂ | _ _ red.refl H := H.symm | _ _ (red.step_trans (@red.step.bnot _ [] _ _ _) H2) H := list.no_confusion H /-- The empty word `[]` only reduces to itself. -/ theorem red.nil (H : red [] L) : [] = L := red.nil.aux H rfl theorem red.singleton.aux : ∀ {L₁ L₂ : list (α × bool)} {x}, red L₁ L₂ → L₁ = [x] → [x] = L₂ | _ _ _ red.refl H := H.symm | _ _ _ (red.step_trans (@red.step.bnot _ [] _ _ _) H2) H := list.no_confusion (list.cons.inj H).2 | _ _ _ (red.step_trans (@red.step.bnot _ [x] _ _ _) H2) H := list.no_confusion (list.cons.inj H).2 /-- A letter only reduces to itself. -/ theorem red.singleton {x} (H : red [x] L₁) : [x] = L₁ := red.singleton.aux H rfl /-- The canonical injection from the type to the free group is an injection. -/ theorem of.inj {x y : α} (H : of x = of y) : x = y := let ⟨L₁, hx, hy⟩ := red.step.exact H in have H1 : _ := (red.singleton hx).trans (red.singleton hy).symm, by injections theorem red.step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ := by cases H; simp; constructor; constructor; refl /-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/ theorem red.sublist (H : red L₁ L₂) : L₂ <+ L₁ := red.rec_on H list.sublist.refl $ λ _ _ _ H1 H2 ih, list.sublist.trans ih $ red.step.sublist H1 theorem red.inv_of_red_nil.aux {x b} (H : red L₁ L₂) : ∀ {L}, L₁ = ((x, b) :: L) → L₂ = [] → red L [(x, bnot b)] := red.rec_on H (by cc) $ λ L3 L4 L5 H34, red.step.cases_on H34 $ λ L6 L7 x1 b1, list.cases_on L6 (λ H45 ih L H7 H5, by injections; subst_vars; simpa) (λ ⟨x2, b2⟩ tl H45 ih L H7 H5, by injections; subst_vars; from red.step_trans red.step.bnot (ih rfl H5)) /-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduecs to `x⁻¹` -/ theorem red.inv_of_red_nil {x b} (H : red ((x, b) :: L) []) : red L [(x, bnot b)] := red.inv_of_red_nil.aux H rfl rfl theorem red.inv_of_red_of_ne.aux {x1 b1 x2 b2} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red L₁ L₂) : ∀ {L₃}, L₁ = (x1, b1) :: L₃ → L₂ = (x2, b2) :: L₄ → red L₃ ((x1, bnot b1) :: L₂) := red.rec_on H2 (by cc) $ λ L5 L6 L7 H56, red.step.cases_on H56 $ λ L8 L9 x b, list.cases_on L8 (λ H67 ih L₃ H6 H7, by injections; subst_vars; simpa) (λ ⟨x, b⟩ tl H67 ih L₃ H6 H7, by injections; subst_vars; from red.step_trans red.step.bnot (ih rfl H7)) /-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then `w₁` reduces to `x⁻¹yw₂`. -/ theorem red.inv_of_red_of_ne {x1 b1 x2 b2} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) : red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) := red.inv_of_red_of_ne.aux H1 H2 rfl rfl section to_group variables {β : Type v} [group β] (f : α → β) {x y : free_group α} def to_group.aux : list (α × bool) → β := λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹ theorem red.step.to_group {f : α → β} (H : red.step L₁ L₂) : to_group.aux f L₁ = to_group.aux f L₂ := by cases H with _ _ _ b; cases b; simp [to_group.aux] theorem red.to_group {f : α → β} (H : red L₁ L₂) : to_group.aux f L₁ = to_group.aux f L₂ := red.rec_on H (λ _, rfl) $ λ _ _ _ H1 H2 ih, eq.trans (by cases H1 with _ _ _ b; cases b; simp [to_group.aux]) ih /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β` -/ def to_group : free_group α → β := quot.lift (to_group.aux f) $ λ L₁ L₂ H, red.step.to_group H variable {f} @[simp] lemma to_group.mk : to_group f (mk L) = list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) := rfl @[simp] lemma to_group.of {x} : to_group f (of x) = f x := one_mul _ instance to_group.is_group_hom : is_group_hom (to_group f) := ⟨λ x y, quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, by simp⟩ @[simp] lemma to_group.mul : to_group f (x * y) = to_group f x * to_group f y := is_group_hom.mul _ _ _ @[simp] lemma to_group.one : to_group f 1 = 1 := is_group_hom.one _ @[simp] lemma to_group.inv : to_group f x⁻¹ = (to_group f x)⁻¹ := is_group_hom.inv _ _ theorem to_group.unique (g : free_group α → β) [is_group_hom g] (hg : ∀ x, g (of x) = f x) {x} : g x = to_group f x := quot.induction_on x $ λ L, list.rec_on L (is_group_hom.one g) $ λ ⟨x, b⟩ t (ih : g (mk t) = _), bool.rec_on b (show g ((of x)⁻¹ * mk t) = to_group f (mk ((x, ff) :: t)), by simp [is_group_hom.mul g, is_group_hom.inv g, hg, ih, to_group, to_group.aux]) (show g (of x * mk t) = to_group f (mk ((x, tt) :: t)), by simp [is_group_hom.mul g, is_group_hom.inv g, hg, ih, to_group, to_group.aux]) theorem to_group.of_eq (x : free_group α) : to_group of x = x := eq.symm $ to_group.unique id (λ x, rfl) theorem to_group.range_subset {s : set β} [is_subgroup s] (H : set.range f ⊆ s) : set.range (to_group f) ⊆ s := λ y ⟨x, H1⟩, H1 ▸ (quot.induction_on x $ λ L, list.rec_on L (is_submonoid.one_mem s) $ λ ⟨x, b⟩ tl ih, bool.rec_on b (by simp at ih ⊢; from is_submonoid.mul_mem (is_subgroup.inv_mem $ H ⟨x, rfl⟩) ih) (by simp at ih ⊢; from is_submonoid.mul_mem (H ⟨x, rfl⟩) ih)) theorem to_group.range_eq_closure : set.range (to_group f) = group.closure (set.range f) := set.subset.antisymm (to_group.range_subset group.subset_closure) (group.closure_subset $ λ y ⟨x, hx⟩, ⟨of x, by simpa⟩) end to_group section map variables {β : Type v} (f : α → β) {x y : free_group α} def map.aux (L : list (α × bool)) : list (β × bool) := L.map $ λ x, (f x.1, x.2) /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group ver `α` to the free group over `β`. -/ def map (x : free_group α) : free_group β := x.lift_on (λ L, mk $ map.aux f L) $ λ L₁ L₂ H, quot.sound $ by cases H; simp [map.aux] instance map.is_group_hom : is_group_hom (map f) := ⟨λ x y, quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, by simp [map, map.aux]⟩ variable {f} @[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) := rfl @[simp] lemma map.id : map id x = x := have H1 : (λ (x : α × bool), x) = id := rfl, quot.induction_on x $ λ L, by simp [H1] @[simp] lemma map.id' : map (λ z, z) x = x := map.id theorem map.comp {γ : Type w} {f : α → β} {g : β → γ} {x} : map g (map f x) = map (g ∘ f) x := quot.induction_on x $ λ L, by simp @[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl @[simp] lemma map.mul : map f (x * y) = map f x * map f y := is_group_hom.mul _ x y @[simp] lemma map.one : map f 1 = 1 := is_group_hom.one _ @[simp] lemma map.inv : map f x⁻¹ = (map f x)⁻¹ := is_group_hom.inv _ x theorem map.unique (g : free_group α → free_group β) [is_group_hom g] (hg : ∀ x, g (of x) = of (f x)) {x} : g x = map f x := quot.induction_on x $ λ L, list.rec_on L (is_group_hom.one g) $ λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b (show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t), by simp [is_group_hom.mul g, is_group_hom.inv g, hg, ih]) (show g (of x * mk t) = map f (of x * mk t), by simp [is_group_hom.mul g, hg, ih]) /-- Equivalent types give rise to equivalent free groups. -/ def free_group_congr {α β} (e : α ≃ β) : free_group α ≃ free_group β := ⟨map e, map e.symm, λ x, by simp [function.comp, map.comp], λ x, by simp [function.comp, map.comp]⟩ theorem map_eq_to_group : map f x = to_group (of ∘ f) x := eq.symm $ map.unique _ $ λ x, by simp end map section prod variables [group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the multiplicative version of `sum`. -/ def prod : α := to_group id x variables {x y} @[simp] lemma prod_mk : prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) := rfl @[simp] lemma prod.of {x : α} : prod (of x) = x := to_group.of instance prod.is_group_hom : is_group_hom (@prod α _) := to_group.is_group_hom @[simp] lemma prod.mul : prod (x * y) = prod x * prod y := to_group.mul @[simp] lemma prod.one : prod (1:free_group α) = 1 := to_group.one @[simp] lemma prod.inv : prod x⁻¹ = (prod x)⁻¹ := to_group.inv lemma prod.unique (g : free_group α → α) [is_group_hom g] (hg : ∀ x, g (of x) = x) {x} : g x = prod x := to_group.unique g hg end prod theorem to_group_eq_prod_map {β : Type v} [group β] {f : α → β} {x} : to_group f x = prod (map f x) := eq.symm $ to_group.unique (prod ∘ map f) $ λ _, by simp section sum variables [add_group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the additive version of `prod`. -/ def sum : α := @prod (multiplicative _) _ x variables {x y} @[simp] lemma sum_mk : sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) := rfl @[simp] lemma sum.of {x : α} : sum (of x) = x := prod.of instance sum.is_group_hom : is_group_hom (@sum α _) := prod.is_group_hom @[simp] lemma sum.sum : sum (x * y) = sum x + sum y := prod.mul @[simp] lemma sum.one : sum (1:free_group α) = 0 := prod.one @[simp] lemma sum.inv : sum x⁻¹ = -sum x := prod.inv end sum def free_group_empty_equiv_unit : free_group empty ≃ unit := { to_fun := λ _, (), inv_fun := λ _, 1, left_inv := λ x, quot.induction_on x $ λ L, match L with [] := rfl end, right_inv := λ ⟨⟩, rfl } def free_group_unit_equiv_int : free_group unit ≃ int := { to_fun := λ x, sum $ map (λ _, 1) x, inv_fun := λ x, of () ^ x, left_inv := λ x, quot.induction_on x $ λ L, list.rec_on L rfl $ λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [gpow_add] at ih ⊢; rw ih; refl, right_inv := λ x, int.induction_on x (by simp) (λ i ih, by simp at ih; simp [gpow_add, ih]) (λ i ih, by simp at ih; simp [gpow_add, ih]) } section reduce variable [decidable_eq α] /-- The maximal reduction of a word. It is computable iff `α` has decidable equality. -/ def reduce (L : list (α × bool)) : list (α × bool) := list.rec_on L [] $ λ hd1 tl1 ih, list.cases_on ih [hd1] $ λ hd2 tl2, if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2 else hd1 :: hd2 :: tl2 @[simp] lemma reduce.cons (x) : reduce (x :: L) = list.cases_on (reduce L) [x] (λ hd tl, if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl else x :: hd :: tl) := rfl /-- The first theorem that characterises the function `reduce`: a word reduces to its maximal reduction. -/ theorem reduce.red : red L (reduce L) := begin induction L with hd1 tl1 ih, case list.nil { refl }, case list.cons { dsimp, revert ih, generalize htl : reduce tl1 = TL, intro ih, cases TL with hd2 tl2, case list.nil { exact red.cons ih }, case list.cons { dsimp, by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd), { rw [if_pos h], transitivity, { exact red.cons ih }, { cases hd1, cases hd2, cases h, dsimp at *, subst_vars, simp } }, { rw [if_neg h], exact red.cons ih } } } end theorem reduce.not {p : Prop} : ∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ =L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p | [] L2 L3 _ _ := λ h, by cases L2; injections | ((x,b)::L1) L2 L3 x' b' := begin dsimp, cases r : reduce L1, { dsimp, intro h, have := congr_arg list.length h, simp [-add_comm] at this, exact absurd this dec_trivial }, cases hd with y c, by_cases x = y ∧ b = bnot c; simp [h]; intro H, { rw H at r, exact @reduce.not L1 ((y,c)::L2) L3 x' b' r }, rcases L2 with _|⟨a, L2⟩, { injections, subst_vars, simp at h, cc }, { refine @reduce.not L1 L2 L3 x' b' _, injection H with _ H, rw [r, H], refl } end /-- The second theorem that characterises the function `reduce`: the maximal reduction of a word only reduces to itself. -/ theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ := begin revert H, generalize h1 : reduce L₁ = L, intro H, induction H with L1 L1 L2 L3 H1 H2 ih, case red.refl { refl }, case red.step_trans { cases H1 with L4 L5 x b, exact reduce.not h1 } end /-- `reduce` is idempotent, i.e. the maximal reduction of the maximal reduction of a word is the maximal reduction of the word. -/ theorem reduce.idem : reduce (reduce L) = reduce L := eq.symm $ reduce.min reduce.red theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := church_rosser reduce.red (red.step_trans H reduce.red) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If a word reduces to another word, then they have a common maximal reduction. -/ theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := church_rosser reduce.red (red.trans H reduce.red) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If two words correspond to the same element in the free group, then they have a common maximal reduction. This is the proof that the function that sends an element of the free group to its maximal reduction is well-defined. -/ theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, H13, H23⟩ := red.step.exact H in (reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm /-- If two words have a common maximal reduction, then they correspond to the same element in the free group. -/ theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ := red.step.sound (reduce L₂) (H ▸ reduce.red) (reduce.red) /-- A word and its maximal reduction correspond to the same element of the free group. -/ theorem reduce.self : mk (reduce L) = mk L := reduce.exact reduce.idem /-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`, then `w₂` reduces to the maximal reduction of `w₁`. -/ theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) := (reduce.eq_of_red H).symm ▸ reduce.red /-- The function that sends an element of the free group to its maximal reduction. -/ def to_word : free_group α → list (α × bool) := quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H def to_word.mk {x : free_group α} : mk (to_word x) = x := quot.induction_on x $ λ L₁, reduce.self def to_word.inj (x y : free_group α) : to_word x = to_word y → x = y := quot.induction_on x $ λ L₁, quot.induction_on y $ λ L₂, reduce.exact /-- Constructive Church-Rosser theorem (compare `church_rosser`). -/ def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) : { L₄ // red L₂ L₄ ∧ red L₃ L₄ } := ⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩ instance : decidable_eq (free_group α) := function.injective.decidable_eq to_word.inj instance red.decidable_rel : decidable_rel (@red α) | [] [] := is_true red.refl | [] (hd2::tl2) := is_false $ λ H, list.no_confusion $ red.nil H | ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with | is_true H := is_true $ red.trans (red.cons H) $ red.of_step $ @red.step.bnot _ [] [] _ _ | is_false H := is_false $ λ H2, H $ red.inv_of_red_nil H2 end | ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2) then match red.decidable_rel tl1 tl2 with | is_true H := is_true $ h ▸ red.cons H | is_false H := is_false $ λ H2, H $ h ▸ red.of_cons $ H2 end else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with | is_true H := is_true $ red.trans (red.cons H) $ red.cons_bnot | is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2 end /-- A list containing every word that `w₁` reduces to. -/ def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) := list.filter (λ L₂, red L₁ L₂) (list.sublists L₁) theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ := list.of_mem_filter H theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ := list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H instance : fintype { L₂ // red L₁ L₂ } := fintype.subtype (list.to_finset $ red.enum L₁) $ λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H, λ H, list.mem_to_finset.2 $ red.enum.complete H⟩ end reduce end free_group
fda71f3c7bfb5ee74725df0061991848d9aba159
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/myset/equinumerous.lean
e0db5b77e1f76b26c69cb70b46286a1929ada088
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
4,051
lean
-- vim: ts=2 sw=0 sts=-1 et ai tw=70 import ..logic import .functions namespace hidden namespace myset universes u v w open myset variables {α : Type u} {β : Type v} {γ : Type w} section definitions variables (s : myset α) (t : myset β) def card_le : Prop := ∃ f : α → β, ∃ h: well_defined s t f, (injective h) def card_ge : Prop := ∃ f : α → β, ∃ h: well_defined s t f, (surjective h) -- of same cardinality def equinumerous : Prop := ∃ f : α → β, ∃ h: well_defined s t f, (bijective h) end definitions -- The trivial function from a set to itself is well defined theorem trivial_well_defined (s : myset α): well_defined s s (λ a, a) := begin intro a, assume h, assumption, end theorem trivial_injective (s : myset α): injective (trivial_well_defined s) := begin intros a₁ a₂, assume h₁ h₂ hneq, assumption, end theorem trivial_surjective (s : myset α): surjective (trivial_well_defined s) := begin intro a, assume h, existsi a, split, from h, refl, end @[refl] theorem card_le_refl (s : myset α) : card_le s s := begin let f := (λ a : α, a), let h := trivial_well_defined s, existsi f, existsi h, from trivial_injective s, end @[refl] theorem card_ge_refl (s : myset α) : card_ge s s := begin let f := (λ a : α, a), let h := trivial_well_defined s, existsi f, existsi h, from trivial_surjective s, end @[refl] theorem equinumerous_refl (s : myset α) : equinumerous s s := begin let f := (λ a : α, a), have h := trivial_well_defined s, existsi f, existsi h, split, from trivial_injective s, from trivial_surjective s, end @[symm] theorem equinumerous_symm (s : myset α) (t : myset β) : equinumerous s t → equinumerous t s := sorry @[trans] theorem equinumerous_trans (r : myset α) (s : myset β) (t : myset γ) : equinumerous r s → equinumerous s t → equinumerous r t := begin assume hrs hst, cases hrs with f hf, cases hf with hwf hf, cases hst with g hg, cases hg with hwg hg, existsi g ∘ f, existsi composition_well_defined hwf hwg, from bij_bij hwf hwg hf hg, end section -- for classical logic open classical local attribute [instance] prop_decidable theorem card_ne_power_set (s : myset α) : ¬equinumerous s (power_set s) := begin assume heq, cases heq with f hf, cases hf with hwell hbi, cases hbi with hinj hsurj, suffices hcontra : ∃ t : myset α, t ∈ (power_set s) ∧ ∀ a : α, a ∈ s → f a ≠ t, { cases hcontra with t ht, cases hsurj t ht.left with preim hpreim, from ht.right preim hpreim.left hpreim.right, }, let t := λ a : α, a ∈ s ∧ ¬(a ∈ f a), existsi t, split, { intro a, assume hta, from hta.left, }, { intro a, assume h heq, have hnta : ¬(t a), { assume hta, have haninfa := hta.right, rw heq at haninfa, contradiction, }, have hta : t a, { rw not_and at hnta, have hnin := hnta h, rw [not_not, heq] at hnin, from hnin, }, contradiction, }, end end -- for classical logic theorem schroeder_bernstein_theorem {s : myset α} {t : myset β}: card_le s t → card_ge s t → equinumerous s t := sorry variables {r : myset α} {s : myset β} {t : myset γ} variables {f : α → β} (hwf : well_defined r s f) include hwf def restriction (r': myset α): r' ⊆ r → (α → β) := (λ _ a, f a) omit hwf theorem restriction_well_defined (r': myset α) (hrss: r' ⊆ r): well_defined r' s (restriction hwf r' hrss) := begin intro a, assume har', apply hwf, apply hrss, from har', end -- this can probably be shorter but I keep getting confused by all the -- definitions theorem restriction_injective (r': myset α) (hrss: r' ⊆ r) (hif: injective hwf): injective (restriction_well_defined hwf r' hrss) := begin intros a b, assume har' hbr' hrarb, apply hif _ _ _ _ _, { from hrss _ har', }, { from hrss _ hbr', }, { from hrarb, }, end end myset end hidden
19c6576aafe400753f70f26c9cb468aa8b62fb98
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/category_theory/comma.lean
6fa21251fce3cd6f88c7697312dc3e93df353dee
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
11,561
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison, Johan Commelin import category_theory.types import category_theory.isomorphism import category_theory.whiskering import category_theory.opposites import category_theory.punit import category_theory.equivalence namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Sort u₁} [𝒜 : category.{v₁} A] variables {B : Sort u₂} [ℬ : category.{v₂} B] variables {T : Sort u₃} [𝒯 : category.{v₃} T] include 𝒜 ℬ 𝒯 structure comma (L : A ⥤ T) (R : B ⥤ T) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟶ R.obj right) variables {L : A ⥤ T} {R : B ⥤ T} structure comma_morphism (X Y : comma L R) := (left : X.left ⟶ Y.left . obviously) (right : X.right ⟶ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w namespace comma_morphism @[extensionality] lemma ext {X Y : comma L R} {f g : comma_morphism X Y} (l : f.left = g.left) (r : f.right = g.right) : f = g := begin cases f, cases g, congr; assumption end end comma_morphism instance comma_category : category (comma L R) := { hom := comma_morphism, id := λ X, { left := 𝟙 X.left, right := 𝟙 X.right }, comp := λ X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R ⥤ A := { obj := λ X, X.left, map := λ _ _ f, f.left } def snd : comma L R ⥤ B := { obj := λ X, X.right, map := λ _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R := { app := λ X, X.hom } section variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T} def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R := { obj := λ X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (𝟙 L) ≅ functor.id _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) : (map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl end def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ := { obj := λ X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (𝟙 R) ≅ functor.id _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl end end end comma omit 𝒜 ℬ def over (X : T) := comma.{v₃ 1 v₃} (functor.id T) (functor.of.obj X) namespace over variables {X : T} instance category : category (over X) := by delta over; apply_instance @[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Sort u₃} [𝒟 : category.{v₃} D] include 𝒟 def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over def under (X : T) := comma.{1 v₃ v₃} (functor.of.obj X) (functor.id T) namespace under variables {X : T} instance : category (under X) := by delta under; apply_instance @[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Sort u₃} [𝒟 : category.{v₃} D] include 𝒟 def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
8b638046eb14041acc1d8bc9cae078ae708fba5d
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/pnat/xgcd.lean
e42da9b542a54ee786b0247842fea7ef42c4db4a
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
13,437
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Neil Strickland This file sets up a version of the Euclidean algorithm that works only with natural numbers. Given a, b > 0, it computes the unique system (w, x, y, z, d) such that the following identities hold: w * z = x * y + 1 a = (w + x) d b = (y + z) d These equations force w, z, d > 0. They also imply that the integers a' = w + x = a / d and b' = y + z = b / d are coprime, and that d is the gcd of a and b. This story is closely related to the structure of SL₂(ℕ) (as a free monoid on two generators) and the theory of continued fractions. -/ import tactic.basic import data.pnat.basic import tactic.ring tactic.abel namespace pnat open nat pnat /-- A term of xgcd_type is a system of six naturals. They should be thought of as representing the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] together with the vector [a, b] = [ap + 1, bp + 1]. -/ @[derive inhabited] structure xgcd_type := (wp x y zp ap bp : ℕ) namespace xgcd_type variable (u : xgcd_type) instance : has_sizeof xgcd_type := ⟨λ u, u.bp⟩ /-- The has_repr instance converts terms to strings in a way that reflects the matrix/vector interpretation as above. -/ instance : has_repr xgcd_type := ⟨λ u, "[[[" ++ (repr (u.wp + 1)) ++ ", " ++ (repr u.x) ++ "], [" ++ (repr u.y) ++ ", " ++ (repr (u.zp + 1)) ++ "]], [" ++ (repr (u.ap + 1)) ++ ", " ++ (repr (u.bp + 1)) ++ "]]"⟩ def mk' (w : ℕ+) (x : ℕ) (y : ℕ) (z : ℕ+) (a : ℕ+) (b : ℕ+) : xgcd_type := mk w.val.pred x y z.val.pred a.val.pred b.val.pred def w : ℕ+ := succ_pnat u.wp def z : ℕ+ := succ_pnat u.zp def a : ℕ+ := succ_pnat u.ap def b : ℕ+ := succ_pnat u.bp def r : ℕ := (u.ap + 1) % (u.bp + 1) def q : ℕ := (u.ap + 1) / (u.bp + 1) def qp : ℕ := u.q - 1 /-- The map v gives the product of the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] and the vector [a, b] = [ap + 1, bp + 1]. The map vp gives [sp, tp] such that v = [sp + 1, tp + 1]. -/ def vp : ℕ × ℕ := ⟨ u.wp + u.x + u.ap + u.wp * u.ap + u.x * u.bp, u.y + u.zp + u.bp + u.y * u.ap + u.zp * u.bp ⟩ def v : ℕ × ℕ := ⟨u.w * u.a + u.x * u.b, u.y * u.a + u.z * u.b⟩ def succ₂ (t : ℕ × ℕ) : ℕ × ℕ := ⟨t.1.succ, t.2.succ⟩ theorem v_eq_succ_vp : u.v = succ₂ u.vp := by { ext; dsimp [v, vp, w, z, a, b, succ₂]; repeat { rw [nat.succ_eq_add_one] }; ring } /-- is_special holds if the matrix has determinant one. -/ def is_special : Prop := u.wp + u.zp + u.wp * u.zp = u.x * u.y def is_special' : Prop := u.w * u.z = succ_pnat (u.x * u.y) theorem is_special_iff : u.is_special ↔ u.is_special' := begin dsimp [is_special, is_special'], split; intro h, { apply eq, dsimp [w, z, succ_pnat], rw [← h], repeat { rw [nat.succ_eq_add_one] }, ring }, { apply nat.succ_inj, replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [mul_coe, w, z] at h, repeat { rw [succ_pnat_coe, nat.succ_eq_add_one] at h }, repeat { rw [nat.succ_eq_add_one] }, rw [← h], ring } end /-- is_reduced holds if the two entries in the vector are the same. The reduction algorithm will produce a system with this property, whose product vector is the same as for the original system. -/ def is_reduced : Prop := u.ap = u.bp def is_reduced' : Prop := u.a = u.b theorem is_reduced_iff : u.is_reduced ↔ u.is_reduced' := ⟨ congr_arg succ_pnat, succ_pnat_inj ⟩ def flip : xgcd_type := { wp := u.zp, x := u.y, y := u.x, zp := u.wp, ap := u.bp, bp := u.ap } @[simp] theorem flip_w : (flip u).w = u.z := rfl @[simp] theorem flip_x : (flip u).x = u.y := rfl @[simp] theorem flip_y : (flip u).y = u.x := rfl @[simp] theorem flip_z : (flip u).z = u.w := rfl @[simp] theorem flip_a : (flip u).a = u.b := rfl @[simp] theorem flip_b : (flip u).b = u.a := rfl theorem flip_is_reduced : (flip u).is_reduced ↔ u.is_reduced := by { dsimp [is_reduced, flip], split; intro h; exact h.symm } theorem flip_is_special : (flip u).is_special ↔ u.is_special := by { dsimp [is_special, flip], rw[mul_comm u.x, mul_comm u.zp, add_comm u.zp] } theorem flip_v : (flip u).v = (u.v).swap := by { dsimp [v], ext, { simp only [], ring }, { simp only [], ring } } /-- Properties of division with remainder for a / b. -/ theorem rq_eq : u.r + (u.bp + 1) * u.q = u.ap + 1 := nat.mod_add_div (u.ap + 1) (u.bp + 1) theorem qp_eq (hr : u.r = 0) : u.q = u.qp + 1 := begin by_cases hq : u.q = 0, { let h := u.rq_eq, rw [hr, hq, mul_zero, add_zero] at h, cases h }, { exact (nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hq)).symm } end /-- The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying is_reduced. The gcd can be read off from this final system. -/ def start (a b : ℕ+) : xgcd_type := ⟨0, 0, 0, 0, a - 1, b - 1⟩ theorem start_is_special (a b : ℕ+) : (start a b).is_special := by { dsimp [start, is_special], refl } theorem start_v (a b : ℕ+) : (start a b).v = ⟨a, b⟩ := begin dsimp [start, v, xgcd_type.a, xgcd_type.b, w, z], rw [one_mul, one_mul, zero_mul, zero_mul, zero_add, add_zero], rw [← nat.pred_eq_sub_one, ← nat.pred_eq_sub_one], rw [nat.succ_pred_eq_of_pos a.pos, nat.succ_pred_eq_of_pos b.pos] end def finish : xgcd_type := xgcd_type.mk u.wp ((u.wp + 1) * u.qp + u.x) u.y (u.y * u.qp + u.zp) u.bp u.bp theorem finish_is_reduced : u.finish.is_reduced := by { dsimp [is_reduced], refl } theorem finish_is_special (hs : u.is_special) : u.finish.is_special := begin dsimp [is_special, finish] at hs ⊢, rw [add_mul _ _ u.y, add_comm _ (u.x * u.y), ← hs], ring end theorem finish_v (hr : u.r = 0) : u.finish.v = u.v := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, rw [hr, zero_add] at ha, ext, { change (u.wp + 1) * u.b + ((u.wp + 1) * u.qp + u.x) * u.b = u.w * u.a + u.x * u.b, have : u.wp + 1 = u.w := rfl, rw [this, ← ha, u.qp_eq hr], ring }, { change u.y * u.b + (u.y * u.qp + u.z) * u.b = u.y * u.a + u.z * u.b, rw [← ha, u.qp_eq hr], ring } end /-- This is the main reduction step, which is used when u.r ≠ 0, or equivalently b does not divide a. -/ def step : xgcd_type := xgcd_type.mk (u.y * u.q + u.zp) u.y ((u.wp + 1) * u.q + u.x) u.wp u.bp (u.r - 1) /-- We will apply the above step recursively. The following result is used to ensure that the process terminates. -/ theorem step_wf (hr : u.r ≠ 0) : sizeof u.step < sizeof u := begin change u.r - 1 < u.bp, have h₀ : (u.r - 1) + 1 = u.r := nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hr), have h₁ : u.r < u.bp + 1 := nat.mod_lt (u.ap + 1) u.bp.succ_pos, rw[← h₀] at h₁, exact lt_of_succ_lt_succ h₁, end theorem step_is_special (hs : u.is_special) : u.step.is_special := begin dsimp [is_special, step] at hs ⊢, rw [mul_add, mul_comm u.y u.x, ← hs], ring end /-- The reduction step does not change the product vector. -/ theorem step_v (hr : u.r ≠ 0) : u.step.v = (u.v).swap := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, let hr : (u.r - 1) + 1 = u.r := (add_comm _ 1).trans (nat.add_sub_of_le (nat.pos_of_ne_zero hr)), ext, { change ((u.y * u.q + u.z) * u.b + u.y * (u.r - 1 + 1) : ℕ) = u.y * u.a + u.z * u.b, rw [← ha, hr], ring }, { change ((u.w * u.q + u.x) * u.b + u.w * (u.r - 1 + 1) : ℕ) = u.w * u.a + u.x * u.b, rw [← ha, hr], ring } end /-- We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below. -/ def reduce : xgcd_type → xgcd_type | u := dite (u.r = 0) (λ h, u.finish) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, flip (reduce u.step)) theorem reduce_a {u : xgcd_type} (h : u.r = 0) : u.reduce = u.finish := by { rw [reduce], simp only [], rw [if_pos h] } theorem reduce_b {u : xgcd_type} (h : u.r ≠ 0) : u.reduce = u.step.reduce.flip := by { rw [reduce], simp only [], rw [if_neg h, step] } theorem reduce_reduced : ∀ (u : xgcd_type), u.reduce.is_reduced | u := dite (u.r = 0) (λ h, by { rw [reduce_a h], exact u.finish_is_reduced }) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h, flip_is_reduced], apply reduce_reduced }) theorem reduce_reduced' (u : xgcd_type) : u.reduce.is_reduced' := (is_reduced_iff _).mp u.reduce_reduced theorem reduce_special : ∀ (u : xgcd_type), u.is_special → u.reduce.is_special | u := dite (u.r = 0) (λ h hs, by { rw [reduce_a h], exact u.finish_is_special hs }) (λ h hs, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h], let u' := u.step.reduce, have : u'.is_special := reduce_special u.step (u.step_is_special hs), exact (flip_is_special _).mpr (reduce_special _ (u.step_is_special hs)) }) theorem reduce_special' (u : xgcd_type) (hs : u.is_special) : u.reduce.is_special' := (is_special_iff _).mp (u.reduce_special hs) theorem reduce_v : ∀ (u : xgcd_type), u.reduce.v = u.v | u := dite (u.r = 0) (λ h, by {rw[reduce_a h, finish_v u h]}) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw[reduce_b h, flip_v, reduce_v (step u), step_v u h, prod.swap_swap] }) end xgcd_type section gcd variables (a b : ℕ+) def xgcd: xgcd_type := (xgcd_type.start a b).reduce def gcd_d : ℕ+ := (xgcd a b).a def gcd_w : ℕ+ := (xgcd a b).w def gcd_x : ℕ := (xgcd a b).x def gcd_y : ℕ := (xgcd a b).y def gcd_z : ℕ+ := (xgcd a b).z def gcd_a' : ℕ+ := succ_pnat ((xgcd a b).wp + (xgcd a b).x) def gcd_b' : ℕ+ := succ_pnat ((xgcd a b).y + (xgcd a b).zp) theorem gcd_a'_coe : ((gcd_a' a b) : ℕ) = (gcd_w a b) + (gcd_x a b) := by { dsimp [gcd_a', gcd_w, xgcd_type.w], rw [nat.succ_eq_add_one, nat.succ_eq_add_one], ring } theorem gcd_b'_coe : ((gcd_b' a b) : ℕ) = (gcd_y a b) + (gcd_z a b) := by { dsimp [gcd_b', gcd_z, xgcd_type.z], rw [nat.succ_eq_add_one, nat.succ_eq_add_one], ring } theorem gcd_props : let d := gcd_d a b, w := gcd_w a b, x := gcd_x a b, y := gcd_y a b, z := gcd_z a b, a' := gcd_a' a b, b' := gcd_b' a b in (w * z = succ_pnat (x * y) ∧ (a = a' * d) ∧ (b = b' * d) ∧ z * a' = succ_pnat (x * b') ∧ w * b' = succ_pnat (y * a') ∧ (z * a : ℕ) = x * b + d ∧ (w * b : ℕ) = y * a + d ) := begin intros, let u := (xgcd_type.start a b), let ur := u.reduce, have ha : d = ur.a := rfl, have hb : d = ur.b := u.reduce_reduced', have ha' : (a' : ℕ) = w + x := gcd_a'_coe a b, have hb' : (b' : ℕ) = y + z := gcd_b'_coe a b, have hdet : w * z = succ_pnat (x * y) := u.reduce_special' rfl, split, exact hdet, have hdet' : ((w * z) : ℕ) = x * y + 1 := by { rw [← mul_coe, hdet, succ_pnat_coe] }, have huv : u.v = ⟨a, b⟩ := (xgcd_type.start_v a b), let hv : prod.mk (w * d + x * ur.b : ℕ) (y * d + z * ur.b : ℕ) = ⟨a, b⟩ := u.reduce_v.trans (xgcd_type.start_v a b), rw [← hb, ← add_mul, ← add_mul, ← ha', ← hb'] at hv, have ha'' : (a : ℕ) = a' * d := (congr_arg prod.fst hv).symm, have hb'' : (b : ℕ) = b' * d := (congr_arg prod.snd hv).symm, split, exact eq ha'', split, exact eq hb'', have hza' : (z * a' : ℕ) = x * b' + 1, by { rw [ha', hb', mul_add, mul_add, mul_comm (z : ℕ), hdet'], ring }, have hwb' : (w * b' : ℕ) = y * a' + 1, by { rw [ha', hb', mul_add, mul_add, hdet'], ring }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hza'] }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hwb'] }, rw [ha'', hb''], repeat { rw [← mul_assoc] }, rw [hza', hwb'], split; ring, end theorem gcd_eq : gcd_d a b = gcd a b := begin rcases gcd_props a b with ⟨h₀, h₁, h₂, h₃, h₄, h₅, h₆⟩, apply dvd_antisymm, { apply dvd_gcd, exact dvd_intro (gcd_a' a b) (h₁.trans (mul_comm _ _)).symm, exact dvd_intro (gcd_b' a b) (h₂.trans (mul_comm _ _)).symm}, { have h₇ := calc ((gcd a b) : ℕ) ∣ a : nat.gcd_dvd_left a b ... ∣ (gcd_z a b) * a : dvd_mul_left _ _, have h₈ := calc ((gcd a b) : ℕ) ∣ b : nat.gcd_dvd_right a b ... ∣ (gcd_x a b) * b : dvd_mul_left _ _, rw[h₅] at h₇, exact (nat.dvd_add_iff_right h₈).mpr h₇} end theorem gcd_det_eq : (gcd_w a b) * (gcd_z a b) = succ_pnat ((gcd_x a b) * (gcd_y a b)) := (gcd_props a b).1 theorem gcd_a_eq : a = (gcd_a' a b) * (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.1 theorem gcd_b_eq : b = (gcd_b' a b) * (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.1 theorem gcd_rel_left' : (gcd_z a b) * (gcd_a' a b) = succ_pnat ((gcd_x a b) * (gcd_b' a b)) := (gcd_props a b).2.2.2.1 theorem gcd_rel_right' : (gcd_w a b) * (gcd_b' a b) = succ_pnat ((gcd_y a b) * (gcd_a' a b)) := (gcd_props a b).2.2.2.2.1 theorem gcd_rel_left : ((gcd_z a b) * a : ℕ) = (gcd_x a b) * b + (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.2.2.2.1 theorem gcd_rel_right : ((gcd_w a b) * b : ℕ) = (gcd_y a b) * a + (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.2.2.2.2 end gcd end pnat
29a663e28ea22110ba8e5b843b58392dce78d823
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/dual.lean
51f96ecd316d7821db87a828f5a8ff37c7ead3dc
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
14,901
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Fabian Glöckle -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.linear_algebra.finite_dimensional import Mathlib.tactic.apply_fun import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u v w l namespace Mathlib /-! # Dual vector spaces The dual space of an R-module M is the R-module of linear maps `M → R`. ## Main definitions * `dual R M` defines the dual space of M over R. * Given a basis for a K-vector space `V`, `is_basis.to_dual` produces a map from `V` to `dual K V`. * Given families of vectors `e` and `ε`, `dual_pair e ε` states that these families have the characteristic properties of a basis and a dual. ## Main results * `to_dual_equiv` : the dual space is linearly equivalent to the primal space. * `dual_pair.is_basis` and `dual_pair.eq_dual`: if `e` and `ε` form a dual pair, `e` is a basis and `ε` is its dual basis. ## Notation We sometimes use `V'` as local notation for `dual K V`. -/ namespace module /-- The dual space of an R-module M is the R-module of linear maps `M → R`. -/ def dual (R : Type u_1) (M : Type u_2) [comm_ring R] [add_comm_group M] [module R M] := linear_map R M R namespace dual protected instance inhabited (R : Type u_1) (M : Type u_2) [comm_ring R] [add_comm_group M] [module R M] : Inhabited (dual R M) := id linear_map.inhabited protected instance has_coe_to_fun (R : Type u_1) (M : Type u_2) [comm_ring R] [add_comm_group M] [module R M] : has_coe_to_fun (dual R M) := has_coe_to_fun.mk (fun (x : dual R M) => M → R) linear_map.to_fun /-- Maps a module M to the dual of the dual of M. See `vector_space.erange_coe` and `vector_space.eval_equiv`. -/ def eval (R : Type u_1) (M : Type u_2) [comm_ring R] [add_comm_group M] [module R M] : linear_map R M (dual R (dual R M)) := linear_map.flip linear_map.id @[simp] theorem eval_apply (R : Type u_1) (M : Type u_2) [comm_ring R] [add_comm_group M] [module R M] (v : M) (a : dual R M) : coe_fn (coe_fn (eval R M) v) a = coe_fn a v := sorry /-- The transposition of linear maps, as a linear map from `M →ₗ[R] M'` to `dual R M' →ₗ[R] dual R M`. -/ def transpose {R : Type u_1} {M : Type u_2} [comm_ring R] [add_comm_group M] [module R M] {M' : Type u_3} [add_comm_group M'] [module R M'] : linear_map R (linear_map R M M') (linear_map R (dual R M') (dual R M)) := linear_map.flip (linear_map.llcomp R M M' R) theorem transpose_apply {R : Type u_1} {M : Type u_2} [comm_ring R] [add_comm_group M] [module R M] {M' : Type u_3} [add_comm_group M'] [module R M'] (u : linear_map R M M') (l : dual R M') : coe_fn (coe_fn transpose u) l = linear_map.comp l u := rfl theorem transpose_comp {R : Type u_1} {M : Type u_2} [comm_ring R] [add_comm_group M] [module R M] {M' : Type u_3} [add_comm_group M'] [module R M'] {M'' : Type u_4} [add_comm_group M''] [module R M''] (u : linear_map R M' M'') (v : linear_map R M M') : coe_fn transpose (linear_map.comp u v) = linear_map.comp (coe_fn transpose v) (coe_fn transpose u) := rfl end dual end module namespace is_basis /-- The linear map from a vector space equipped with basis to its dual vector space, taking basis elements to corresponding dual basis elements. -/ def to_dual {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] (B : ι → V) (h : is_basis K B) : linear_map K V (module.dual K V) := constr h fun (v : ι) => constr h fun (w : ι) => ite (w = v) 1 0 theorem to_dual_apply {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (i : ι) (j : ι) : coe_fn (coe_fn (to_dual B h) (B i)) (B j) = ite (i = j) 1 0 := sorry @[simp] theorem to_dual_total_left {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (f : ι →₀ K) (i : ι) : coe_fn (coe_fn (to_dual B h) (coe_fn (finsupp.total ι V K B) f)) (B i) = coe_fn f i := sorry @[simp] theorem to_dual_total_right {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (f : ι →₀ K) (i : ι) : coe_fn (coe_fn (to_dual B h) (B i)) (coe_fn (finsupp.total ι V K B) f) = coe_fn f i := sorry theorem to_dual_apply_left {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (v : V) (i : ι) : coe_fn (coe_fn (to_dual B h) v) (B i) = coe_fn (coe_fn (repr h) v) i := sorry theorem to_dual_apply_right {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (i : ι) (v : V) : coe_fn (coe_fn (to_dual B h) (B i)) v = coe_fn (coe_fn (repr h) v) i := sorry /-- `h.to_dual_flip v` is the linear map sending `w` to `h.to_dual w v`. -/ def to_dual_flip {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] (B : ι → V) (h : is_basis K B) (v : V) : linear_map K V K := coe_fn (linear_map.flip (to_dual B h)) v -- TODO: unify this with `finsupp.lapply`. /-- Evaluation of finitely supported functions at a fixed point `i`, as a `K`-linear map. -/ def eval_finsupp_at {K : Type u} {ι : Type w} [field K] (i : ι) : linear_map K (ι →₀ K) K := linear_map.mk (fun (f : ι →₀ K) => coe_fn f i) sorry sorry /-- `h.coord_fun i` sends vectors to their `i`'th coordinate with respect to the basis `h`. -/ def coord_fun {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] {B : ι → V} (h : is_basis K B) (i : ι) : linear_map K V K := linear_map.comp (eval_finsupp_at i) (repr h) theorem coord_fun_eq_repr {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] {B : ι → V} (h : is_basis K B) (v : V) (i : ι) : coe_fn (coord_fun h i) v = coe_fn (coe_fn (repr h) v) i := rfl -- TODO: this lemma should be called something like `to_dual_flip_apply` theorem to_dual_swap_eq_to_dual {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (v : V) (w : V) : coe_fn (to_dual_flip B h v) w = coe_fn (coe_fn (to_dual B h) w) v := rfl theorem to_dual_eq_repr {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (v : V) (i : ι) : coe_fn (coe_fn (to_dual B h) v) (B i) = coe_fn (coe_fn (repr h) v) i := to_dual_apply_left h v i theorem to_dual_eq_equiv_fun {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] (v : V) (i : ι) : coe_fn (coe_fn (to_dual B h) v) (B i) = coe_fn (equiv_fun h) v i := sorry theorem to_dual_inj {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (v : V) (a : coe_fn (to_dual B h) v = 0) : v = 0 := sorry theorem to_dual_ker {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) : linear_map.ker (to_dual B h) = ⊥ := iff.mpr linear_map.ker_eq_bot' (to_dual_inj h) theorem to_dual_range {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fin : fintype ι] : linear_map.range (to_dual B h) = ⊤ := sorry /-- Maps a basis for `V` to a basis for the dual space. -/ def dual_basis {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) : ι → module.dual K V := fun (i : ι) => coe_fn (to_dual B h) (B i) theorem dual_lin_independent {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) : linear_independent K (dual_basis h) := linear_independent.map' (and.left h) (to_dual B h) (to_dual_ker h) @[simp] theorem dual_basis_apply_self {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) (i : ι) (j : ι) : coe_fn (dual_basis h i) (B j) = ite (i = j) 1 0 := to_dual_apply h i j /-- A vector space is linearly equivalent to its dual space. -/ def to_dual_equiv {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] (B : ι → V) (h : is_basis K B) [fintype ι] : linear_equiv K V (module.dual K V) := linear_equiv.of_bijective (to_dual B h) sorry sorry theorem dual_basis_is_basis {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] : is_basis K (dual_basis h) := linear_equiv.is_basis h (to_dual_equiv B h) @[simp] theorem total_dual_basis {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] (f : ι →₀ K) (i : ι) : coe_fn (coe_fn (finsupp.total ι (module.dual K V) K (dual_basis h)) f) (B i) = coe_fn f i := sorry theorem dual_basis_repr {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] (l : module.dual K V) (i : ι) : coe_fn (coe_fn (repr (dual_basis_is_basis h)) l) i = coe_fn l (B i) := sorry theorem dual_basis_equiv_fun {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] (l : module.dual K V) (i : ι) : coe_fn (equiv_fun (dual_basis_is_basis h)) l i = coe_fn l (B i) := sorry theorem dual_basis_apply {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] (i : ι) (v : V) : coe_fn (dual_basis h i) v = coe_fn (equiv_fun h) v i := to_dual_apply_right h i v @[simp] theorem to_dual_to_dual {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] [de : DecidableEq ι] {B : ι → V} (h : is_basis K B) [fintype ι] : linear_map.comp (to_dual (dual_basis h) (dual_basis_is_basis h)) (to_dual B h) = module.dual.eval K V := sorry theorem dual_dim_eq {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] {B : ι → V} (h : is_basis K B) [fintype ι] : cardinal.lift (vector_space.dim K V) = vector_space.dim K (module.dual K V) := sorry end is_basis namespace vector_space theorem eval_ker {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] : linear_map.ker (module.dual.eval K V) = ⊥ := sorry theorem dual_dim_eq {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] : cardinal.lift (dim K V) = dim K (module.dual K V) := sorry theorem erange_coe {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] : linear_map.range (module.dual.eval K V) = ⊤ := sorry /-- A vector space is linearly equivalent to the dual of its dual space. -/ def eval_equiv {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] : linear_equiv K V (module.dual K (module.dual K V)) := linear_equiv.of_bijective (module.dual.eval K V) eval_ker erange_coe end vector_space /-- `e` and `ε` have characteristic properties of a basis and its dual -/ structure dual_pair {K : Type u} {V : Type v} {ι : Type w} [DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] (e : ι → V) (ε : ι → module.dual K V) where eval : ∀ (i j : ι), coe_fn (ε i) (e j) = ite (i = j) 1 0 total : ∀ {v : V}, (∀ (i : ι), coe_fn (ε i) v = 0) → v = 0 finite : (v : V) → fintype ↥(set_of fun (i : ι) => coe_fn (ε i) v ≠ 0) namespace dual_pair /-- The coefficients of `v` on the basis `e` -/ def coeffs {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) (v : V) : ι →₀ K := finsupp.mk (set.to_finset (set_of fun (i : ι) => coe_fn (ε i) v ≠ 0)) (fun (i : ι) => coe_fn (ε i) v) sorry @[simp] theorem coeffs_apply {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) (v : V) (i : ι) : coe_fn (coeffs h v) i = coe_fn (ε i) v := rfl /-- linear combinations of elements of `e`. This is a convenient abbreviation for `finsupp.total _ V K e l` -/ def lc {K : Type u} {V : Type v} {ι : Type w} [field K] [add_comm_group V] [vector_space K V] (e : ι → V) (l : ι →₀ K) : V := finsupp.sum l fun (i : ι) (a : K) => a • e i theorem dual_lc {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) (l : ι →₀ K) (i : ι) : coe_fn (ε i) (lc e l) = coe_fn l i := sorry @[simp] theorem coeffs_lc {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) (l : ι →₀ K) : coeffs h (lc e l) = l := sorry /-- For any v : V n, \sum_{p ∈ Q n} (ε p v) • e p = v -/ theorem decomposition {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) (v : V) : lc e (coeffs h v) = v := sorry theorem mem_of_mem_span {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) {H : set ι} {x : V} (hmem : x ∈ submodule.span K (e '' H)) (i : ι) : coe_fn (ε i) x ≠ 0 → i ∈ H := sorry theorem is_basis {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) : is_basis K e := sorry theorem eq_dual {K : Type u} {V : Type v} {ι : Type w} [dι : DecidableEq ι] [field K] [add_comm_group V] [vector_space K V] {e : ι → V} {ε : ι → module.dual K V} (h : dual_pair e ε) : ε = is_basis.dual_basis (is_basis h) := sorry
bfb427705005e2deb2bf69056fd333a0b975a235
80162757f50b09d3cad5564907e4c9b00742e045
/rosser.lean
270e1f98a00eeaef9fffb4717b9d9773b45e8b1e
[]
no_license
EdAyers/edlib
cc30d0a54fed347a85b6df6045f68e6b48bc71a3
78b8c5d91f023f939c102837d748868e2f3ed27d
refs/heads/master
1,586,459,758,216
1,571,322,179,000
1,571,322,179,000
160,538,917
2
0
null
null
null
null
UTF-8
Lean
false
false
526
lean
universe u inductive pred (α : Type u) : Type u |and : pred → pred → pred |not : pred → pred |atom : α → pred prefix `~` := pred.not infixl `&`:50 := pred.and def pred.or {α : Type u} : pred α → pred α → pred α := λ P Q, ~((~P)&(~Q)) def pred.imp {α : Type u}: pred α → pred α→ pred α := λ P Q, ~(P&(~Q)) infixl `|`:40 := pred.or local infixl `⊃`:50 := pred.imp instance atom_coe {α} : has_coe (α) (pred α) := ⟨pred.atom⟩ section parameter {α : Type u} abbreviation p := pred α end
064f72468424e185cb9cedb4de8a913dbc099c79
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/cc_constructors.lean
38b6e837011fc807ce37a015ef17c5acb6015475
[ "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,826
lean
open tactic example (a b : nat) (s t : list nat) : a::s = b::t → a ≠ b → false := by cc example (a b : nat) (s t : list nat) : a::s = b::t → t ≠ s → false := by cc example (a c b : nat) (s t : list nat) : a::s = b::t → a ≠ c → c = b → false := by cc example (a c b : nat) (s t : list nat) : a::a::s = a::b::t → a ≠ c → c = b → false := by cc example (a b : nat) (s t r : list nat) : a::s = r → r = b::t → a ≠ b → false := by cc example (a b : nat) (s t r : list nat) : a::s = r → r = b::t → a = b := by cc example (a b : nat) (s t r : list nat) : list.cons a = list.cons b → a = b := begin intro h1, /- In the current implementation, cc does not "complete" partially applied constructor applications. So, the following one should fail. -/ try {cc}, /- Complete it manually. TODO(Leo): we can automate it for inhabited types. -/ note h := congr_fun h1 [], cc end inductive foo | mk1 : nat → nat → foo | mk2 : nat → nat → foo example (a b : nat) : foo.mk1 a = foo.mk2 b → false := begin intro h1, /- In the current implementation, cc does not "complete" partially applied constructor applications. So, the following one should fail. -/ try {cc}, note h := congr_fun h1 0, cc end universe variables u inductive Vec (α : Type u) : nat → Type (max 1 u) | nil : Vec 0 | cons : ∀ {n}, α → Vec n → Vec (nat.succ n) example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → a ≠ b → false := by cc example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → t ≠ s → false := by cc example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a (Vec.cons a s) = Vec.cons a (Vec.cons b t) → b ≠ c → c = a → false := by cc
8049ed6277499461cc5faf25b71880ee413780ce
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/data/prod.lean
079dac3db780836f0a659c18310d47e2a05ea3a5
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
1,083
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad -/ prelude import init.logic universes u v instance {α : Type u} {β : Type v} [inhabited α] [inhabited β] : inhabited (prod α β) := ⟨(default α, default β)⟩ instance {α : Type u} {β : Type v} [h₁ : decidable_eq α] [h₂ : decidable_eq β] : decidable_eq (α × β) | (a, b) (a', b') := match (h₁ a a') with | (is_true e₁) := match (h₂ b b') with | (is_true e₂) := is_true (eq.rec_on e₁ (eq.rec_on e₂ rfl)) | (is_false n₂) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₂' n₂)) end | (is_false n₁) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₁' n₁)) end namespace prod def {u₁ u₂ v₁ v₂} map {α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂} (f : α₁ → α₂) (g : β₁ → β₂) : α₁ × β₁ → α₂ × β₂ | (a, b) := (f a, g b) end prod
104cc607ec12a05c5cac9135de7943f2ffbaaa43
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/computability/partrec.lean
b76bd937bfaef41ab5fdb65c5c72eb8afb93f3c5
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
28,198
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro The partial recursive functions are defined similarly to the primitive recursive functions, but now all functions are partial, implemented using the `roption` monad, and there is an additional operation, called μ-recursion, which performs unbounded minimization. -/ import computability.primrec data.pfun open encodable denumerable roption namespace nat section rfind parameter (p : ℕ →. bool) private def lbp (m n : ℕ) : Prop := m = n + 1 ∧ ∀ k ≤ n, ff ∈ p k parameter (H : ∃ n, tt ∈ p n ∧ ∀ k < n, (p k).dom) private def wf_lbp : well_founded lbp := ⟨let ⟨n, pn⟩ := H in begin suffices : ∀m k, n ≤ k + m → acc (lbp p) k, { from λa, this _ _ (nat.le_add_left _ _) }, intros m k kn, induction m with m IH generalizing k; refine ⟨_, λ y r, _⟩; rcases r with ⟨rfl, a⟩, { injection mem_unique pn.1 (a _ kn) }, { exact IH _ (by rw nat.add_right_comm; exact kn) } end⟩ def rfind_x : {n // tt ∈ p n ∧ ∀m < n, ff ∈ p m} := suffices ∀ k, (∀n < k, ff ∈ p n) → {n // tt ∈ p n ∧ ∀m < n, ff ∈ p m}, from this 0 (λ n, (nat.not_lt_zero _).elim), @well_founded.fix _ _ lbp wf_lbp begin intros m IH al, have pm : (p m).dom, { rcases H with ⟨n, h₁, h₂⟩, rcases decidable.lt_trichotomy m n with h₃|h₃|h₃, { exact h₂ _ h₃ }, { rw h₃, exact h₁.fst }, { injection mem_unique h₁ (al _ h₃) } }, cases e : (p m).get pm, { suffices, exact IH _ ⟨rfl, this⟩ (λ n h, this _ (le_of_lt_succ h)), intros n h, cases decidable.lt_or_eq_of_le h with h h, { exact al _ h }, { rw h, exact ⟨_, e⟩ } }, { exact ⟨m, ⟨_, e⟩, al⟩ } end end rfind def rfind (p : ℕ →. bool) : roption ℕ := ⟨_, λ h, (rfind_x p h).1⟩ theorem rfind_spec {p : ℕ →. bool} {n : ℕ} (h : n ∈ rfind p) : tt ∈ p n := h.snd ▸ (rfind_x p h.fst).2.1 theorem rfind_min {p : ℕ →. bool} {n : ℕ} (h : n ∈ rfind p) : ∀ {m : ℕ}, m < n → ff ∈ p m := h.snd ▸ (rfind_x p h.fst).2.2 @[simp] theorem rfind_dom {p : ℕ →. bool} : (rfind p).dom ↔ ∃ n, tt ∈ p n ∧ ∀ {m : ℕ}, m < n → (p m).dom := iff.rfl theorem rfind_dom' {p : ℕ →. bool} : (rfind p).dom ↔ ∃ n, tt ∈ p n ∧ ∀ {m : ℕ}, m ≤ n → (p m).dom := exists_congr $ λ n, and_congr_right $ λ pn, ⟨λ H m h, (eq_or_lt_of_le h).elim (λ e, e.symm ▸ pn.fst) (H _), λ H m h, H (le_of_lt h)⟩ @[simp] theorem mem_rfind {p : ℕ →. bool} {n : ℕ} : n ∈ rfind p ↔ tt ∈ p n ∧ ∀ {m : ℕ}, m < n → ff ∈ p m := ⟨λ h, ⟨rfind_spec h, @rfind_min _ _ h⟩, λ ⟨h₁, h₂⟩, let ⟨m, hm⟩ := dom_iff_mem.1 $ (@rfind_dom p).2 ⟨_, h₁, λ m mn, (h₂ mn).fst⟩ in begin rcases lt_trichotomy m n with h|h|h, { injection mem_unique (h₂ h) (rfind_spec hm) }, { rwa ← h }, { injection mem_unique h₁ (rfind_min hm h) }, end⟩ theorem rfind_min' {p : ℕ → bool} {m : ℕ} (pm : p m) : ∃ n ∈ rfind p, n ≤ m := have tt ∈ (p : ℕ →. bool) m, from ⟨trivial, pm⟩, let ⟨n, hn⟩ := dom_iff_mem.1 $ (@rfind_dom p).2 ⟨m, this, λ k h, ⟨⟩⟩ in ⟨n, hn, not_lt.1 $ λ h, by injection mem_unique this (rfind_min hn h)⟩ theorem rfind_zero_none (p : ℕ →. bool) (p0 : p 0 = none) : rfind p = none := eq_none_iff.2 $ λ a h, let ⟨n, h₁, h₂⟩ := rfind_dom'.1 h.fst in (p0 ▸ h₂ (zero_le _) : (@roption.none bool).dom) def rfind_opt {α} (f : ℕ → option α) : roption α := (rfind (λ n, (f n).is_some)).bind (λ n, f n) theorem rfind_opt_spec {α} {f : ℕ → option α} {a} (h : a ∈ rfind_opt f) : ∃ n, a ∈ f n := let ⟨n, h₁, h₂⟩ := mem_bind_iff.1 h in ⟨n, mem_coe.1 h₂⟩ theorem rfind_opt_dom {α} {f : ℕ → option α} : (rfind_opt f).dom ↔ ∃ n a, a ∈ f n := ⟨λ h, (rfind_opt_spec ⟨h, rfl⟩).imp (λ n h, ⟨_, h⟩), λ h, begin have h' : ∃ n, (f n).is_some := h.imp (λ n, option.is_some_iff_exists.2), have s := nat.find_spec h', have fd : (rfind (λ n, (f n).is_some)).dom := ⟨nat.find h', by simpa using s.symm, λ _ _, trivial⟩, refine ⟨fd, _⟩, have := rfind_spec (get_mem fd), simp at this ⊢, cases option.is_some_iff_exists.1 this.symm with a e, rw e, trivial end⟩ theorem rfind_opt_mono {α} {f : ℕ → option α} (H : ∀ {a m n}, m ≤ n → a ∈ f m → a ∈ f n) {a} : a ∈ rfind_opt f ↔ ∃ n, a ∈ f n := ⟨rfind_opt_spec, λ ⟨n, h⟩, begin have h' := rfind_opt_dom.2 ⟨_, _, h⟩, cases rfind_opt_spec ⟨h', rfl⟩ with k hk, have := (H (le_max_left _ _) h).symm.trans (H (le_max_right _ _) hk), simp at this, simp [this, get_mem] end⟩ inductive partrec : (ℕ →. ℕ) → Prop | zero : partrec (pure 0) | succ : partrec succ | left : partrec (λ n, n.unpair.1) | right : partrec (λ n, n.unpair.2) | pair {f g} : partrec f → partrec g → partrec (λ n, mkpair <$> f n <*> g n) | comp {f g} : partrec f → partrec g → partrec (λ n, g n >>= f) | prec {f g} : partrec f → partrec g → partrec (unpaired (λ a n, n.elim (f a) (λ y IH, do i ← IH, g (mkpair a (mkpair y i))))) | rfind {f} : partrec f → partrec (λ a, rfind (λ n, (λ m, m = 0) <$> f (mkpair a n))) namespace partrec theorem of_eq {f g : ℕ →. ℕ} (hf : partrec f) (H : ∀ n, f n = g n) : partrec g := (funext H : f = g) ▸ hf theorem of_eq_tot {f : ℕ →. ℕ} {g : ℕ → ℕ} (hf : partrec f) (H : ∀ n, g n ∈ f n) : partrec g := hf.of_eq (λ n, eq_some_iff.2 (H n)) theorem of_primrec {f : ℕ → ℕ} (hf : primrec f) : partrec f := begin induction hf, case nat.primrec.zero { exact zero }, case nat.primrec.succ { exact succ }, case nat.primrec.left { exact left }, case nat.primrec.right { exact right }, case nat.primrec.pair : f g hf hg pf pg { refine (pf.pair pg).of_eq_tot (λ n, _), simp [has_seq.seq] }, case nat.primrec.comp : f g hf hg pf pg { refine (pf.comp pg).of_eq_tot (λ n, _), simp }, case nat.primrec.prec : f g hf hg pf pg { refine (pf.prec pg).of_eq_tot (λ n, _), simp, induction n.unpair.2 with m IH, {simp}, simp, exact ⟨_, IH, rfl⟩ }, end protected theorem some : partrec some := of_primrec primrec.id theorem none : partrec (λ n, none) := (of_primrec (nat.primrec.const 1)).rfind.of_eq $ λ n, eq_none_iff.2 $ λ a ⟨h, e⟩, by simpa using h theorem prec' {f g h} (hf : partrec f) (hg : partrec g) (hh : partrec h) : partrec (λ a, (f a).bind (λ n, n.elim (g a) (λ y IH, do i ← IH, h (mkpair a (mkpair y i))))) := ((prec hg hh).comp (pair partrec.some hf)).of_eq $ λ a, ext $ λ s, by simp [(<*>)]; exact ⟨λ ⟨n, h₁, h₂⟩, ⟨_, ⟨_, h₁, rfl⟩, by simpa using h₂⟩, λ ⟨_, ⟨n, h₁, rfl⟩, h₂⟩, ⟨_, h₁, by simpa using h₂⟩⟩ theorem ppred : partrec (λ n, ppred n) := have primrec₂ (λ n m, if n = nat.succ m then 0 else 1), from (primrec.ite (@@primrec_rel.comp _ _ _ _ _ _ _ primrec.eq primrec.fst (_root_.primrec.succ.comp primrec.snd)) (_root_.primrec.const 0) (_root_.primrec.const 1)).to₂, (of_primrec (primrec₂.unpaired'.2 this)).rfind.of_eq $ λ n, begin cases n; simp, { exact eq_none_iff.2 (λ a ⟨⟨m, h, _⟩, _⟩, by simpa [show 0 ≠ m.succ, by intro h; injection h] using h) }, { refine eq_some_iff.2 _, simp, intros m h, simp [ne_of_gt h] } end end partrec end nat def partrec {α σ} [primcodable α] [primcodable σ] (f : α →. σ) := nat.partrec (λ n, roption.bind (decode α n) (λ a, (f a).map encode)) def partrec₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β →. σ) := partrec (λ p : α × β, f p.1 p.2) def computable {α σ} [primcodable α] [primcodable σ] (f : α → σ) := partrec (f : α →. σ) def computable₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β → σ) := computable (λ p : α × β, f p.1 p.2) theorem primrec.to_comp {α σ} [primcodable α] [primcodable σ] {f : α → σ} (hf : primrec f) : computable f := (nat.partrec.ppred.comp (nat.partrec.of_primrec hf)).of_eq $ λ n, by simp; cases decode α n; simp theorem primrec₂.to_comp {α β σ} [primcodable α] [primcodable β] [primcodable σ] {f : α → β → σ} (hf : primrec₂ f) : computable₂ f := hf.to_comp theorem computable.part {α σ} [primcodable α] [primcodable σ] {f : α → σ} (hf : computable f) : partrec (f : α →. σ) := hf theorem computable₂.part {α β σ} [primcodable α] [primcodable β] [primcodable σ] {f : α → β → σ} (hf : computable₂ f) : partrec₂ (λ a, (f a : β →. σ)) := hf namespace computable variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem of_eq {f g : α → σ} (hf : computable f) (H : ∀ n, f n = g n) : computable g := (funext H : f = g) ▸ hf theorem const (s : σ) : computable (λ a : α, s) := (primrec.const _).to_comp theorem of_option {f : α → option β} (hf : computable f) : partrec (λ a, (f a : roption β)) := (nat.partrec.ppred.comp hf).of_eq $ λ n, begin cases decode α n with a; simp, cases f a with b; simp end theorem to₂ {f : α × β → σ} (hf : computable f) : computable₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl protected theorem id : computable (@id α) := primrec.id.to_comp theorem fst : computable (@prod.fst α β) := primrec.fst.to_comp theorem snd : computable (@prod.snd α β) := primrec.snd.to_comp theorem pair {f : α → β} {g : α → γ} (hf : computable f) (hg : computable g) : computable (λ a, (f a, g a)) := (hf.pair hg).of_eq $ λ n, by cases decode α n; simp [(<*>)] theorem unpair : computable nat.unpair := primrec.unpair.to_comp theorem succ : computable nat.succ := primrec.succ.to_comp theorem pred : computable nat.pred := primrec.pred.to_comp theorem nat_bodd : computable nat.bodd := primrec.nat_bodd.to_comp theorem nat_div2 : computable nat.div2 := primrec.nat_div2.to_comp theorem sum_inl : computable (@sum.inl α β) := primrec.sum_inl.to_comp theorem sum_inr : computable (@sum.inr α β) := primrec.sum_inr.to_comp theorem list_cons : computable₂ (@list.cons α) := primrec.list_cons.to_comp theorem list_reverse : computable (@list.reverse α) := primrec.list_reverse.to_comp theorem list_nth : computable₂ (@list.nth α) := primrec.list_nth.to_comp theorem list_append : computable₂ ((++) : list α → list α → list α) := primrec.list_append.to_comp theorem list_concat : computable₂ (λ l (a:α), l ++ [a]) := primrec.list_concat.to_comp theorem list_length : computable (@list.length α) := primrec.list_length.to_comp theorem vector_cons {n} : computable₂ (@vector.cons α n) := primrec.vector_cons.to_comp theorem vector_to_list {n} : computable (@vector.to_list α n) := primrec.vector_to_list.to_comp theorem vector_length {n} : computable (@vector.length α n) := primrec.vector_length.to_comp theorem vector_head {n} : computable (@vector.head α n) := primrec.vector_head.to_comp theorem vector_tail {n} : computable (@vector.tail α n) := primrec.vector_tail.to_comp theorem vector_nth {n} : computable₂ (@vector.nth α n) := primrec.vector_nth.to_comp theorem vector_nth' {n} : computable (@vector.nth α n) := primrec.vector_nth'.to_comp theorem vector_of_fn' {n} : computable (@vector.of_fn α n) := primrec.vector_of_fn'.to_comp theorem fin_app {n} : computable₂ (@id (fin n → σ)) := primrec.fin_app.to_comp protected theorem encode : computable (@encode α _) := primrec.encode.to_comp protected theorem decode : computable (decode α) := primrec.decode.to_comp protected theorem of_nat (α) [denumerable α] : computable (of_nat α) := (primrec.of_nat _).to_comp theorem encode_iff {f : α → σ} : computable (λ a, encode (f a)) ↔ computable f := iff.rfl theorem option_some : computable (@option.some α) := primrec.option_some.to_comp end computable namespace partrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] open computable theorem of_eq {f g : α →. σ} (hf : partrec f) (H : ∀ n, f n = g n) : partrec g := (funext H : f = g) ▸ hf theorem of_eq_tot {f : α →. σ} {g : α → σ} (hf : partrec f) (H : ∀ n, g n ∈ f n) : computable g := hf.of_eq (λ a, eq_some_iff.2 (H a)) theorem none : partrec (λ a : α, @roption.none σ) := nat.partrec.none.of_eq $ λ n, by cases decode α n; simp protected theorem some : partrec (@roption.some α) := computable.id theorem const' (s : roption σ) : partrec (λ a : α, s) := by haveI := classical.dec s.dom; exact (of_option (const (to_option s))).of_eq (λ a, of_to_option s) protected theorem bind {f : α →. β} {g : α → β →. σ} (hf : partrec f) (hg : partrec₂ g) : partrec (λ a, (f a).bind (g a)) := (hg.comp (nat.partrec.some.pair hf)).of_eq $ λ n, by simp [(<*>)]; cases e : decode α n with a; simp [e, encodek] theorem map {f : α →. β} {g : α → β → σ} (hf : partrec f) (hg : computable₂ g) : partrec (λ a, (f a).map (g a)) := by simpa [bind_some_eq_map] using @@partrec.bind _ _ _ (λ a b, roption.some (g a b)) hf hg theorem to₂ {f : α × β →. σ} (hf : partrec f) : partrec₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl theorem nat_elim {f : α → ℕ} {g : α →. σ} {h : α → ℕ × σ →. σ} (hf : computable f) (hg : partrec g) (hh : partrec₂ h) : partrec (λ a, (f a).elim (g a) (λ y IH, IH.bind (λ i, h a (y, i)))) := (nat.partrec.prec' hf hg hh).of_eq $ λ n, begin cases e : decode α n with a; simp [e], induction f a with m IH; simp, rw [IH, bind_map], congr, funext s, simp [encodek] end theorem comp {f : β →. σ} {g : α → β} (hf : partrec f) (hg : computable g) : partrec (λ a, f (g a)) := (hf.comp hg).of_eq $ λ n, by simp; cases e : decode α n with a; simp [e, encodek] theorem nat_iff {f : ℕ →. ℕ} : partrec f ↔ nat.partrec f := by simp [partrec, map_id'] theorem map_encode_iff {f : α →. σ} : partrec (λ a, (f a).map encode) ↔ partrec f := iff.rfl end partrec namespace partrec₂ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem unpaired {f : ℕ → ℕ →. α} : partrec (nat.unpaired f) ↔ partrec₂ f := ⟨λ h, by simpa using h.comp primrec₂.mkpair.to_comp, λ h, h.comp primrec.unpair.to_comp⟩ theorem unpaired' {f : ℕ → ℕ →. ℕ} : nat.partrec (nat.unpaired f) ↔ partrec₂ f := partrec.nat_iff.symm.trans unpaired theorem comp {f : β → γ →. σ} {g : α → β} {h : α → γ} (hf : partrec₂ f) (hg : computable g) (hh : computable h) : partrec (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem comp₂ {f : γ → δ →. σ} {g : α → β → γ} {h : α → β → δ} (hf : partrec₂ f) (hg : computable₂ g) (hh : computable₂ h) : partrec₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh end partrec₂ namespace computable variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem comp {f : β → σ} {g : α → β} (hf : computable f) (hg : computable g) : computable (λ a, f (g a)) := hf.comp hg theorem comp₂ {f : γ → σ} {g : α → β → γ} (hf : computable f) (hg : computable₂ g) : computable₂ (λ a b, f (g a b)) := hf.comp hg end computable namespace computable₂ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem comp {f : β → γ → σ} {g : α → β} {h : α → γ} (hf : computable₂ f) (hg : computable g) (hh : computable h) : computable (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem comp₂ {f : γ → δ → σ} {g : α → β → γ} {h : α → β → δ} (hf : computable₂ f) (hg : computable₂ g) (hh : computable₂ h) : computable₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh end computable₂ namespace partrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] open computable theorem rfind {p : α → ℕ →. bool} (hp : partrec₂ p) : partrec (λ a, nat.rfind (p a)) := (nat.partrec.rfind $ hp.map ((primrec.dom_bool (λ b, cond b 0 1)) .comp primrec.snd).to₂.to_comp).of_eq $ λ n, begin cases e : decode α n with a; simp [e, nat.rfind_zero_none, map_id'], congr, funext n, simp [roption.map_map, (∘)], apply map_id' (λ b, _), cases b; refl end theorem rfind_opt {f : α → ℕ → option σ} (hf : computable₂ f) : partrec (λ a, nat.rfind_opt (f a)) := (rfind (primrec.option_is_some.to_comp.comp hf).part.to₂).bind (of_option hf) theorem nat_cases_right {f : α → ℕ} {g : α → σ} {h : α → ℕ →. σ} (hf : computable f) (hg : computable g) (hh : partrec₂ h) : partrec (λ a, (f a).cases (some (g a)) (h a)) := (nat_elim hf hg (hh.comp fst (pred.comp $ hf.comp fst)).to₂).of_eq $ λ a, begin simp, cases f a; simp, refine ext (λ b, ⟨λ H, _, λ H, _⟩), { rcases mem_bind_iff.1 H with ⟨c, h₁, h₂⟩, exact h₂ }, { have : ∀ m, (nat.elim (roption.some (g a)) (λ y IH, IH.bind (λ _, h a n)) m).dom, { intro, induction m; simp [*, H.fst] }, exact ⟨⟨this n, H.fst⟩, H.snd⟩ } end theorem bind_decode2_iff {f : α →. σ} : partrec f ↔ nat.partrec (λ n, roption.bind (decode2 α n) (λ a, (f a).map encode)) := ⟨λ hf, nat_iff.1 $ (of_option primrec.decode2.to_comp).bind $ (map hf (computable.encode.comp snd).to₂).comp snd, λ h, map_encode_iff.1 $ by simpa [encodek2] using (nat_iff.2 h).comp (@computable.encode α _)⟩ theorem vector_m_of_fn : ∀ {n} {f : fin n → α →. σ}, (∀ i, partrec (f i)) → partrec (λ (a : α), vector.m_of_fn (λ i, f i a)) | 0 f hf := const _ | (n+1) f hf := by simp [vector.m_of_fn]; exact (hf 0).bind (partrec.bind ((vector_m_of_fn (λ i, hf i.succ)).comp fst) (primrec.vector_cons.to_comp.comp (snd.comp fst) snd)) end partrec @[simp] theorem vector.m_of_fn_roption_some {α n} : ∀ (f : fin n → α), vector.m_of_fn (λ i, roption.some (f i)) = roption.some (vector.of_fn f) := vector.m_of_fn_pure namespace computable variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem option_some_iff {f : α → σ} : computable (λ a, some (f a)) ↔ computable f := ⟨λ h, encode_iff.1 $ primrec.pred.to_comp.comp $ encode_iff.2 h, option_some.comp⟩ theorem bind_decode_iff {f : α → β → option σ} : computable₂ (λ a n, (decode β n).bind (f a)) ↔ computable₂ f := ⟨λ hf, nat.partrec.of_eq (((partrec.nat_iff.2 (nat.partrec.ppred.comp $ nat.partrec.of_primrec $ primcodable.prim β)).comp snd).bind (computable.comp hf fst).to₂.part) $ λ n, by simp; cases decode α n.unpair.1; simp; cases decode β n.unpair.2; simp, λ hf, begin have : partrec (λ a : α × ℕ, (encode (decode β a.2)).cases (some option.none) (λ n, roption.map (f a.1) (decode β n))) := partrec.nat_cases_right (primrec.encdec.to_comp.comp snd) (const none) ((of_option (computable.decode.comp snd)).map (hf.comp (fst.comp $ fst.comp fst) snd).to₂), refine this.of_eq (λ a, _), simp, cases decode β a.2; simp [encodek] end⟩ theorem map_decode_iff {f : α → β → σ} : computable₂ (λ a n, (decode β n).map (f a)) ↔ computable₂ f := bind_decode_iff.trans option_some_iff theorem nat_elim {f : α → ℕ} {g : α → σ} {h : α → ℕ × σ → σ} (hf : computable f) (hg : computable g) (hh : computable₂ h) : computable (λ a, (f a).elim (g a) (λ y IH, h a (y, IH))) := (partrec.nat_elim hf hg hh.part).of_eq $ λ a, by simp; induction f a; simp * theorem nat_cases {f : α → ℕ} {g : α → σ} {h : α → ℕ → σ} (hf : computable f) (hg : computable g) (hh : computable₂ h) : computable (λ a, (f a).cases (g a) (h a)) := nat_elim hf hg (hh.comp fst $ fst.comp snd).to₂ theorem cond {c : α → bool} {f : α → σ} {g : α → σ} (hc : computable c) (hf : computable f) (hg : computable g) : computable (λ a, cond (c a) (f a) (g a)) := (nat_cases (encode_iff.2 hc) hg (hf.comp fst).to₂).of_eq $ λ a, by cases c a; refl theorem option_cases {o : α → option β} {f : α → σ} {g : α → β → σ} (ho : computable o) (hf : computable f) (hg : computable₂ g) : @computable _ σ _ _ (λ a, option.cases_on (o a) (f a) (g a)) := option_some_iff.1 $ (nat_cases (encode_iff.2 ho) (option_some_iff.2 hf) (map_decode_iff.2 hg)).of_eq $ λ a, by cases o a; simp [encodek]; refl theorem option_bind {f : α → option β} {g : α → β → option σ} (hf : computable f) (hg : computable₂ g) : computable (λ a, (f a).bind (g a)) := (option_cases hf (const option.none) hg).of_eq $ λ a, by cases f a; refl theorem option_map {f : α → option β} {g : α → β → σ} (hf : computable f) (hg : computable₂ g) : computable (λ a, (f a).map (g a)) := option_bind hf (option_some.comp₂ hg) theorem sum_cases {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ → σ} (hf : computable f) (hg : computable₂ g) (hh : computable₂ h) : @computable _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (nat_bodd.comp $ encode_iff.2 hf) (option_map (computable.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hh) (option_map (computable.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hg)).of_eq $ λ a, by cases f a with b c; simp [nat.div2_bit, nat.bodd_bit, encodek]; refl theorem nat_strong_rec (f : α → ℕ → σ) {g : α → list σ → option σ} (hg : computable₂ g) (H : ∀ a n, g a ((list.range n).map (f a)) = some (f a n)) : computable₂ f := suffices computable₂ (λ a n, (list.range n).map (f a)), from option_some_iff.1 $ (list_nth.comp (this.comp fst (succ.comp snd)) snd).to₂.of_eq $ λ a, by simp [list.nth_range (nat.lt_succ_self a.2)]; refl, option_some_iff.1 $ (nat_elim snd (const (option.some [])) (to₂ $ option_bind (snd.comp snd) $ to₂ $ option_map (hg.comp (fst.comp $ fst.comp fst) snd) (to₂ $ list_concat.comp (snd.comp fst) snd))).of_eq $ λ a, begin simp, induction a.2 with n IH, {refl}, simp [IH, H, list.range_concat] end theorem list_of_fn : ∀ {n} {f : fin n → α → σ}, (∀ i, computable (f i)) → computable (λ a, list.of_fn (λ i, f i a)) | 0 f hf := const [] | (n+1) f hf := by simp [list.of_fn_succ]; exact list_cons.comp (hf 0) (list_of_fn (λ i, hf i.succ)) theorem vector_of_fn {n} {f : fin n → α → σ} (hf : ∀ i, computable (f i)) : computable (λ a, vector.of_fn (λ i, f i a)) := (partrec.vector_m_of_fn hf).of_eq $ λ a, by simp end computable namespace partrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] open computable theorem option_some_iff {f : α →. σ} : partrec (λ a, (f a).map option.some) ↔ partrec f := ⟨λ h, (nat.partrec.ppred.comp h).of_eq $ λ n, by simp [roption.bind_assoc, bind_some_eq_map], λ hf, hf.map (option_some.comp snd).to₂⟩ theorem option_cases_right {o : α → option β} {f : α → σ} {g : α → β →. σ} (ho : computable o) (hf : computable f) (hg : partrec₂ g) : @partrec _ σ _ _ (λ a, option.cases_on (o a) (some (f a)) (g a)) := have partrec (λ (a : α), nat.cases (roption.some (f a)) (λ n, roption.bind (decode β n) (g a)) (encode (o a))) := nat_cases_right (encode_iff.2 ho) hf.part $ ((@computable.decode β _).comp snd).of_option.bind (hg.comp (fst.comp fst) snd).to₂, this.of_eq $ λ a, by cases o a with b; simp [encodek] theorem sum_cases_right {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ →. σ} (hf : computable f) (hg : computable₂ g) (hh : partrec₂ h) : @partrec _ σ _ _ (λ a, sum.cases_on (f a) (λ b, some (g a b)) (h a)) := have partrec (λ a, (option.cases_on (sum.cases_on (f a) (λ b, option.none) option.some : option γ) (some (sum.cases_on (f a) (λ b, some (g a b)) (λ c, option.none))) (λ c, (h a c).map option.some) : roption (option σ))) := option_cases_right (sum_cases hf (const option.none).to₂ (option_some.comp snd).to₂) (sum_cases hf (option_some.comp hg) (const option.none).to₂) (option_some_iff.2 hh), option_some_iff.1 $ this.of_eq $ λ a, by cases f a; simp theorem sum_cases_left {f : α → β ⊕ γ} {g : α → β →. σ} {h : α → γ → σ} (hf : computable f) (hg : partrec₂ g) (hh : computable₂ h) : @partrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (λ c, some (h a c))) := (sum_cases_right (sum_cases hf (sum_inr.comp snd).to₂ (sum_inl.comp snd).to₂) hh hg).of_eq $ λ a, by cases f a; simp private lemma fix_aux {f : α →. σ ⊕ α} (hf : partrec f) (a : α) (b : σ) : let F : α → ℕ →. σ ⊕ α := λ a n, n.elim (some (sum.inr a)) $ λ y IH, IH.bind $ λ s, sum.cases_on s (λ _, roption.some s) f in (∃ (n : ℕ), ((∃ (b' : σ), sum.inl b' ∈ F a n) ∧ ∀ {m : ℕ}, m < n → (∃ (b : α), sum.inr b ∈ F a m)) ∧ sum.inl b ∈ F a n) ↔ b ∈ pfun.fix f a := begin intro, refine ⟨λ h, _, λ h, _⟩, { rcases h with ⟨n, ⟨_x, h₁⟩, h₂⟩, have : ∀ m a' (_: sum.inr a' ∈ F a m) (_: b ∈ pfun.fix f a'), b ∈ pfun.fix f a, { intros m a' am ba, induction m with m IH generalizing a'; simp [F] at am, { rwa ← am }, rcases am with ⟨a₂, am₂, fa₂⟩, exact IH _ am₂ (pfun.mem_fix_iff.2 (or.inr ⟨_, fa₂, ba⟩)) }, cases n; simp [F] at h₂, {cases h₂}, rcases h₂ with h₂ | ⟨a', am', fa'⟩, { cases h₁ (nat.lt_succ_self _) with a' h, injection mem_unique h h₂ }, { exact this _ _ am' (pfun.mem_fix_iff.2 (or.inl fa')) } }, { suffices : ∀ a' (_: b ∈ pfun.fix f a') k (_: sum.inr a' ∈ F a k), ∃ n, sum.inl b ∈ F a n ∧ ∀ (m < n) (_ : k ≤ m), ∃ a₂, sum.inr a₂ ∈ F a m, { rcases this _ h 0 (by simp [F]) with ⟨n, hn₁, hn₂⟩, exact ⟨_, ⟨⟨_, hn₁⟩, λ m mn, hn₂ m mn (nat.zero_le _)⟩, hn₁⟩ }, intros a₁ h₁, apply pfun.fix_induction h₁, intros a₂ h₂ IH k hk, rcases pfun.mem_fix_iff.1 h₂ with h₂ | ⟨a₃, am₃, fa₃⟩, { refine ⟨k.succ, _, λ m mk km, ⟨a₂, _⟩⟩, { simp [F], exact or.inr ⟨_, hk, h₂⟩ }, { rwa le_antisymm (nat.le_of_lt_succ mk) km } }, { rcases IH _ fa₃ am₃ k.succ _ with ⟨n, hn₁, hn₂⟩, { refine ⟨n, hn₁, λ m mn km, _⟩, cases lt_or_eq_of_le km with km km, { exact hn₂ _ mn km }, { exact km ▸ ⟨_, hk⟩ } }, { simp [F], exact ⟨_, hk, am₃⟩ } } } end theorem fix {f : α →. σ ⊕ α} (hf : partrec f) : partrec (pfun.fix f) := let F : α → ℕ →. σ ⊕ α := λ a n, n.elim (some (sum.inr a)) $ λ y IH, IH.bind $ λ s, sum.cases_on s (λ _, roption.some s) f in have hF : partrec₂ F := partrec.nat_elim snd (sum_inr.comp fst).part (sum_cases_right (snd.comp snd) (snd.comp $ snd.comp fst).to₂ (hf.comp snd).to₂).to₂, let p := λ a n, @roption.map _ bool (λ s, sum.cases_on s (λ_, tt) (λ _, ff)) (F a n) in have hp : partrec₂ p := hF.map ((sum_cases computable.id (const tt).to₂ (const ff).to₂).comp snd).to₂, (hp.rfind.bind (hF.bind (sum_cases_right snd snd.to₂ none.to₂).to₂).to₂).of_eq $ λ a, ext $ λ b, by simp; apply fix_aux hf end partrec
b699928d512f7c5970f65ec41549ab9cf171f7c9
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/transitiveInferenceJoin3way.lean
2e6b120abe1059bb0bec26d1d3e4e1e407e68fed
[ "BSD-2-Clause" ]
permissive
Shamrock-Frost/Cosette
b477c442c07e45082348a145f19ebb35a7f29392
24cbc4adebf627f13f5eac878f04ffa20d1209af
refs/heads/master
1,619,721,304,969
1,526,082,841,000
1,526,082,841,000
121,695,605
1
0
null
1,518,737,210,000
1,518,737,210,000
null
UTF-8
Lean
false
false
2,650
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..ucongr import ..TDP open Expr open Proj open Pred open SQL open tree set_option profiler true notation `int` := datatypes.int variable integer_1: const datatypes.int variable integer_7: const datatypes.int theorem rule: forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt))) (product (table rel_emp) (table rel_emp))) WHERE (and (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅left⋅emp_deptno))) (equal (uvariable (right⋅right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅right⋅emp_deptno))))) :SQL Γ _) = denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt))) (product ((SELECT * FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt))) ((SELECT * FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt))))) WHERE (and (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅left⋅emp_deptno))) (equal (uvariable (right⋅right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅right⋅emp_deptno))))) :SQL Γ _) := begin intros, unfold_all_denotations, funext, try {simp}, TDP, end
443bf0a2c645c5e421a6e1f7e8e79dc8cd180497
827a8a5c2041b1d7f55e128581f583dfbd65ecf6
/Jakob_truncation.hlean
203d9ee7e41120fea7f7c9ac42b46c6575f8e31e
[ "Apache-2.0" ]
permissive
fpvandoorn/leansnippets
6af0499f6f3fd2c07e4b580734d77b67574e7c27
601bafbe07e9534af76f60994d6bdf741996ef93
refs/heads/master
1,590,063,910,882
1,545,093,878,000
1,545,093,878,000
36,044,957
2
2
null
1,442,619,708,000
1,432,256,875,000
Lean
UTF-8
Lean
false
false
1,765
hlean
import types.pi open eq is_trunc pi equiv sigma.ops sigma definition prop_truncate (A : Type) : Type := Π (P : Type), (is_prop P) → (A → P) → P namespace prop_truncate notation `∥` A `∥` := prop_truncate A definition is_prop_of_prop_truncate [instance] (A : Type) : is_prop ∥ A ∥ := begin apply is_trunc_pi, end definition prop_abs {A : Type} (a : A) : ∥ A ∥ := (λ P H f, f a) protected definition rec [reducible] {A B : Type} [HB : is_prop B] (f : A → B) (aa : ∥ A ∥) : B := aa B HB f protected definition rec_on [reducible] {A : Type} (aa : ∥ A ∥) {B : Type} [HB : is_prop B] (f : A → B) : B := aa B HB f definition equiv_lift (A : Type) : A ≃ lift A := begin fapply equiv.mk, exact (@lift.up A), fapply is_equiv.adjointify, exact (@lift.down A), intro a, apply (lift.rec_on a), intro down, apply idp, intro a, apply idp, end universe variable l definition equiv_prop_truncate (A : Type.{l}) [HA : is_prop A] : A ≃ prop_truncate.{l l+1} A := begin have HlA : is_prop (lift A), begin apply is_trunc_equiv_closed, apply equiv_lift end, apply equiv.trans, apply equiv_lift.{l l+1}, fapply equiv.mk, intro la, eapply (lift.rec_on la), exact (@prop_abs.{l l+1} A), fapply is_equiv.adjointify, intro aa, apply (aa (lift A) HlA (λ x, lift.up x)), intros, apply (@is_prop.elim (prop_truncate.{l l+1} A) (is_prop_of_prop_truncate.{l l+1} A)), intros, apply is_prop.elim, end definition unique_choice {A : Type} (P : A → Type) [HP : Π x, is_prop (P x)] (choice : Π x, ∥ P x ∥) : Π x, P x := (λ x, (is_equiv.inv (equiv.to_fun (equiv_prop_truncate (P x)))) (choice x)) end prop_truncate
69731f0a603ca3aadaf683c477b82ce3a5ec4dea
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/algebra/group/basic.lean
1c52823fd483374b4deca8d5a38740dad11b7a40
[ "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
8,697
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon, Mario Carneiro -/ import algebra.group.to_additive logic.function attribute [simp] sub_neg_eq_add /-! # Extra identities for semigroups, monoids, and groups -/ universes u v w variables {M : Type u} {A : Type v} {G : Type w} @[to_additive add_monoid_to_is_left_id] instance monoid_to_is_left_id [monoid M] : is_left_id M (*) 1 := ⟨ monoid.one_mul ⟩ @[to_additive add_monoid_to_is_right_id] instance monoid_to_is_right_id [monoid M] : is_right_id M (*) 1 := ⟨ monoid.mul_one ⟩ @[to_additive] theorem mul_left_injective [left_cancel_semigroup M] (a : M) : function.injective ((*) a) := λ b c, mul_left_cancel @[to_additive] theorem mul_right_injective [right_cancel_semigroup M] (a : M) : function.injective (λ x, x * a) := λ b c, mul_right_cancel @[simp, to_additive] theorem mul_left_inj [left_cancel_semigroup M] (a : M) {b c : M} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[simp, to_additive] theorem mul_right_inj [right_cancel_semigroup M] (a : M) {b c : M} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ @[to_additive] theorem mul_mul_mul_comm [comm_semigroup M] {a b c d : M} : (a * b) * (c * d) = (a * c) * (b * d) := by simp only [mul_left_comm, mul_assoc] section group variables [group G] {a b c : G} @[simp, to_additive] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← inv_inv a, h, inv_inv], congr_arg _⟩ @[to_additive] theorem mul_left_surjective (a : G) : function.surjective ((*) a) := λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩ @[to_additive] theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) := λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩ @[to_additive] theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b := inv_inj'.1 @[to_additive] theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 := by have := @mul_left_inj _ _ a a 1; rwa mul_one at this @[simp, to_additive] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj' _ _ a 1, one_inv] @[to_additive] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive] theorem left_inverse_inv (G) [group G] : function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) := inv_inv attribute [simp] mul_inv_cancel_left add_neg_cancel_left mul_inv_cancel_right add_neg_cancel_right @[to_additive] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm @[to_additive] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := by simpa [mul_left_inv, -mul_right_inj] using @mul_right_inj _ _ b a (b⁻¹) @[to_additive] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive] theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive] theorem inv_comm_of_comm (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:G, a⁻¹ * x * a⁻¹) H.symm, rwa [inv_mul_cancel_left, mul_assoc, mul_inv_cancel_right] at this end @[simp, to_additive] lemma mul_left_eq_self : a * b = b ↔ a = 1 := ⟨λ h, @mul_right_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩ @[simp, to_additive] lemma mul_right_eq_self : a * b = a ↔ b = 1 := ⟨λ h, @mul_left_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩ end group section add_group variables [add_group A] {a b c d : A} local attribute [simp] sub_eq_add_neg @[simp] lemma sub_left_inj : a - b = a - c ↔ b = c := (add_left_inj _).trans neg_inj' @[simp] lemma sub_right_inj : b - a = c - a ↔ b = c := add_right_inj _ lemma sub_add_sub_cancel (a b c : A) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] lemma sub_sub_sub_cancel_right (a b c : A) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_sub_assoc_swap : a - (b - c) = a + c - b := by simp theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := eq_add_neg_iff_add_eq theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := add_neg_eq_iff_eq_add theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : A) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : A) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : A) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : A) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section add_comm_group variables [add_comm_group A] {a b c : A} lemma sub_sub_cancel (a b : A) : a - (a - b) = b := sub_sub_self a b lemma sub_eq_neg_add (a b : A) : a - b = -b + a := add_comm _ _ theorem neg_add' (a b : A) : -(a + b) = -a - b := neg_add a b attribute [simp] zero_sub sub_zero sub_self neg_sub @[simp] lemma neg_sub_neg (a b : A) : -a - -b = b - a := by simp [sub_eq_neg_add, add_comm] lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] attribute [simp] add_sub_cancel sub_add_cancel attribute [simp] add_sub_add_left_eq_sub add_sub_add_right_eq_sub @[simp] lemma add_sub_cancel' (a b : A) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] @[simp] lemma add_sub_cancel'_right (a b : A) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] @[simp] lemma add_add_neg_cancel'_right (a b : A) : a + (b + -a) = b := add_sub_cancel'_right a b lemma sub_right_comm (a b c : A) : a - b - c = a - c - b := add_right_comm _ _ _ lemma add_add_sub_cancel (a b c : A) : (a + c) + (b - c) = a + b := by rw [add_assoc, add_sub_cancel'_right] lemma sub_add_add_cancel (a b c : A) : (a - c) + (b + c) = a + b := by rw [add_left_comm, sub_add_cancel, add_comm] lemma sub_add_sub_cancel' (a b c : A) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel lemma add_sub_sub_cancel (a b c : A) : (a + b) - (a - c) = b + c := by rw [← sub_add, add_sub_cancel'] lemma sub_sub_sub_cancel_left (a b c : A) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] lemma sub_eq_sub_iff_add_eq_add {d : A} : a - b = c - d ↔ a + d = c + b := begin rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'], simp only [add_comm, eq_comm] end lemma sub_eq_sub_iff_sub_eq_sub {d : A} : a - b = c - d ↔ a - c = b - d := by simp [sub_eq_sub_iff_add_eq_add, add_comm] end add_comm_group section add_monoid variables [add_monoid A] {a b c : A} @[simp] lemma bit0_zero : bit0 (0 : A) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one A] : bit1 (0 : A) = 1 := by rw [bit1, bit0_zero, zero_add] end add_monoid section comm_monoid variables [comm_monoid M] @[to_additive] lemma inv_unique {x y z : M} (hy : x * y = 1) (hz : x * z = 1) : y = z := by rw [←one_mul y, ←hz, mul_comm x, mul_assoc, hy, mul_one] end comm_monoid @[to_additive] lemma inv_involutive {α} [group α] : function.involutive (has_inv.inv : α → α) := inv_inv
5b270b8368723361b3c5d4e5470f16e9fdc799a5
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/elim2.lean
01b82c087430284d369996f785d379ca843b056d
[ "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
256
lean
import logic open tactic constant p : num → num → num → Prop axiom H1 : ∃ x y z, p x y z axiom H2 : ∀ {x y z : num}, p x y z → p x x x theorem tst : ∃ x, p x x x := obtain a b c H, from H1, by (apply exists.intro; apply H2; eassumption)
f4fdcd4d258fed839255342592979cf537171baf
dac4e6671410f506c880986cbb2212dd7f5b3dfd
/hanoi_project/Homework_Hoan.lean
6a3dfd653e5954052b2d9a06a0d0e0a7f604aae7
[ "CC-BY-4.0" ]
permissive
thalesant/formalabstracts-2018
e6ddfe8b3ce5c6f055ddcccf345bf55c41f850c1
d206dfa32a6b4a84aacc3a5500a144757e6d3454
refs/heads/master
1,642,678,879,231
1,561,648,713,000
1,561,648,713,000
97,608,420
1
0
null
1,564,063,995,000
1,500,388,250,000
Lean
UTF-8
Lean
false
false
939
lean
-- Nguyen Duc Hoan -- --------------------- --Twin Prime Conjecture def infinite (s : set ℕ) : Prop := ∀ n : ℕ, ∃ m ∈ s, n < m def isPrime (n : ℕ) : Prop := (n ≥ 2) ∧ (∀ m : ℕ, m ≥ 1 ∧ m ∣ n → (m = 1 ∨ m =n)) def is_pair_Primes (p:nat) :Prop := isPrime p ∧ isPrime (p+2) #print is_pair_Primes def the_set_of_pair_primes := { p:nat | is_pair_Primes p} theorem Thm_Twin_Prime_Conj : infinite the_set_of_pair_primes := begin admit, end ------------------------------------------------ --Theorem n^2+1 Conjecture theorem The_n2_plus_1_prime_Thm : infinite { p:ℕ | isPrime p ∧ ( ∃ n: nat, p = n*n + 1)} := begin admit, end ------------------------------------------------- --Goldbach’s Conjecture-- def isEven (n:nat) : Prop := n> 1 ∧ (2 ∣ n) theorem Goldbach_Conjecture (n:nat) : isEven n → ∃ (p1: ℕ) (p2 : ℕ), isPrime p1 ∧ isPrime p2 ∧ n = p1 + p2 := begin admit, end
5e7111cd0b908137052f0bf25baf8fbc34c1dd0b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Init/Data/Float.lean
f35ba7458609c73c33332600b44cc4c076a44af7
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
4,558
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Core import Init.Data.Int.Basic import Init.Data.ToString.Basic structure FloatSpec where float : Type val : float lt : float → float → Prop le : float → float → Prop decLt : DecidableRel lt decLe : DecidableRel le -- Just show FloatSpec is inhabited. constant floatSpec : FloatSpec := { float := Unit, val := (), lt := fun _ _ => True, le := fun _ _ => True, decLt := fun _ _ => inferInstanceAs (Decidable True), decLe := fun _ _ => inferInstanceAs (Decidable True) } structure Float where val : floatSpec.float instance : Inhabited Float := ⟨{ val := floatSpec.val }⟩ @[extern "lean_float_of_nat"] constant Float.ofNat : (@& Nat) → Float @[extern c inline "#1 + #2"] constant Float.add : Float → Float → Float @[extern c inline "#1 - #2"] constant Float.sub : Float → Float → Float @[extern c inline "#1 * #2"] constant Float.mul : Float → Float → Float @[extern c inline "#1 / #2"] constant Float.div : Float → Float → Float @[extern c inline "(- #1)"] constant Float.neg : Float → Float def Float.ofInt : Int → Float | Int.ofNat n => Float.ofNat n | Int.negSucc n => Float.neg (Float.ofNat (Nat.succ n)) set_option bootstrap.genMatcherCode false def Float.lt : Float → Float → Prop := fun a b => match a, b with | ⟨a⟩, ⟨b⟩ => floatSpec.lt a b def Float.le : Float → Float → Prop := fun a b => floatSpec.le a.val b.val instance : OfNat Float n := ⟨Float.ofNat n⟩ instance : Add Float := ⟨Float.add⟩ instance : Sub Float := ⟨Float.sub⟩ instance : Mul Float := ⟨Float.mul⟩ instance : Div Float := ⟨Float.div⟩ instance : Neg Float := ⟨Float.neg⟩ instance : LT Float := ⟨Float.lt⟩ instance : LE Float := ⟨Float.le⟩ @[extern c inline "#1 == #2"] constant Float.beq (a b : Float) : Bool instance : BEq Float := ⟨Float.beq⟩ @[extern c inline "#1 < #2"] constant Float.decLt (a b : Float) : Decidable (a < b) := match a, b with | ⟨a⟩, ⟨b⟩ => floatSpec.decLt a b @[extern c inline "#1 <= #2"] constant Float.decLe (a b : Float) : Decidable (a ≤ b) := match a, b with | ⟨a⟩, ⟨b⟩ => floatSpec.decLe a b instance floatDecLt (a b : Float) : Decidable (a < b) := Float.decLt a b instance floatDecLe (a b : Float) : Decidable (a ≤ b) := Float.decLe a b @[extern "lean_float_to_string"] constant Float.toString : Float → String @[extern c inline "(uint8_t)#1"] constant Float.toUInt8 : Float → UInt8 @[extern c inline "(uint16_t)#1"] constant Float.toUInt16 : Float → UInt16 @[extern c inline "(uint32_t)#1"] constant Float.toUInt32 : Float → UInt32 @[extern c inline "(uint64_t)#1"] constant Float.toUInt64 : Float → UInt64 @[extern c inline "(size_t)#1"] constant Float.toUSize : Float → USize instance : ToString Float where toString := Float.toString instance : Repr Float where reprPrec n _ := Float.toString n instance : ReprAtom Float := ⟨⟩ abbrev Nat.toFloat (n : Nat) : Float := Float.ofNat n @[extern "sin"] constant Float.sin : Float → Float @[extern "cos"] constant Float.cos : Float → Float @[extern "tan"] constant Float.tan : Float → Float @[extern "asin"] constant Float.asin : Float → Float @[extern "acos"] constant Float.acos : Float → Float @[extern "atan"] constant Float.atan : Float → Float @[extern "atan2"] constant Float.atan2 : Float → Float → Float @[extern "sinh"] constant Float.sinh : Float → Float @[extern "cosh"] constant Float.cosh : Float → Float @[extern "tanh"] constant Float.tanh : Float → Float @[extern "asinh"] constant Float.asinh : Float → Float @[extern "acosh"] constant Float.acosh : Float → Float @[extern "atanh"] constant Float.atanh : Float → Float @[extern "exp"] constant Float.exp : Float → Float @[extern "exp2"] constant Float.exp2 : Float → Float @[extern "log"] constant Float.log : Float → Float @[extern "log2"] constant Float.log2 : Float → Float @[extern "log10"] constant Float.log10 : Float → Float @[extern "pow"] constant Float.pow : Float → Float → Float @[extern "sqrt"] constant Float.sqrt : Float → Float @[extern "cbrt"] constant Float.cbrt : Float → Float instance : Pow Float := ⟨Float.pow⟩ @[extern "lean_float_of_scientific"] constant Float.ofScientific (m : Nat) (s : Bool) (e : Nat) : Float
4cb7e7939ab21d813f695fbc8a0c6928b1b3f493
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/470_lean3.lean
fe42fd0bd41ffbb7b40b062e0c318616b31a1e06
[ "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
57
lean
section foo axiom foo : Type opaque bla : Nat end foo
4c04bc795810f197706d20af5f250c2d1bf3da1c
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/topology/metric_space/baire.lean
cb09d93a415f4ebe6867e778f37701145b214ca0
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
15,206
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel Baire theorem: in a complete metric space, a countable intersection of dense open subsets is dense. The good concept underlying the theorem is that of a Gδ set, i.e., a countable intersection of open sets. Then Baire theorem can also be formulated as the fact that a countable intersection of dense Gδ sets is a dense Gδ set. We prove Baire theorem, giving several different formulations that can be handy. We also prove the important consequence that, if the space is covered by a countable union of closed sets, then the union of their interiors is dense. The names of the theorems do not contain the string "Baire", but are instead built from the form of the statement. "Baire" is however in the docstring of all the theorems, to facilitate grep searches. -/ import topology.metric_space.basic analysis.specific_limits noncomputable theory open_locale classical open filter lattice encodable set variables {α : Type*} {β : Type*} {γ : Type*} section is_Gδ variable [topological_space α] /-- A Gδ set is a countable intersection of open sets. -/ def is_Gδ (s : set α) : Prop := ∃T : set (set α), (∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T) /-- An open set is a Gδ set. -/ lemma is_open.is_Gδ {s : set α} (h : is_open s) : is_Gδ s := ⟨{s}, by simp [h], countable_singleton _, (set.sInter_singleton _).symm⟩ lemma is_Gδ_bInter_of_open {ι : Type*} {I : set ι} (hI : countable I) {f : ι → set α} (hf : ∀i ∈ I, is_open (f i)) : is_Gδ (⋂i∈I, f i) := ⟨f '' I, by rwa ball_image_iff, countable_image _ hI, by rw sInter_image⟩ lemma is_Gδ_Inter_of_open {ι : Type*} [encodable ι] {f : ι → set α} (hf : ∀i, is_open (f i)) : is_Gδ (⋂i, f i) := ⟨range f, by rwa forall_range_iff, countable_range _, by rw sInter_range⟩ /-- A countable intersection of Gδ sets is a Gδ set. -/ lemma is_Gδ_sInter {S : set (set α)} (h : ∀s∈S, is_Gδ s) (hS : countable S) : is_Gδ (⋂₀ S) := begin have : ∀s : set α, ∃T : set (set α), s ∈ S → ((∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T)), { assume s, by_cases hs : s ∈ S, { simp [hs], exact h s hs }, { simp [hs] }}, choose T hT using this, refine ⟨⋃s∈S, T s, λt ht, _, _, _⟩, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, exact (hT s hs).1 t tTs }, { exact countable_bUnion hS (λs hs, (hT s hs).2.1) }, { exact (sInter_bUnion (λs hs, (hT s hs).2.2)).symm } end /-- The union of two Gδ sets is a Gδ set. -/ lemma is_Gδ.union {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) : is_Gδ (s ∪ t) := begin rcases hs with ⟨S, Sopen, Scount, sS⟩, rcases ht with ⟨T, Topen, Tcount, tT⟩, rw [sS, tT, sInter_union_sInter], apply is_Gδ_bInter_of_open (countable_prod Scount Tcount), rintros ⟨a, b⟩ hab, simp only [set.prod_mk_mem_set_prod_eq] at hab, have aopen : is_open a := Sopen a hab.1, have bopen : is_open b := Topen b hab.2, simp [aopen, bopen, is_open_union] end end is_Gδ section Baire_theorem open metric variables [metric_space α] [complete_space α] /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here when the source space is ℕ (and subsumed below by `dense_Inter_of_open` working with any encodable source space). -/ theorem dense_Inter_of_open_nat {f : ℕ → set α} (ho : ∀n, is_open (f n)) (hd : ∀n, closure (f n) = univ) : closure (⋂n, f n) = univ := begin let B : ℕ → ℝ := λn, ((1/2)^n : ℝ), have Bpos : ∀n, 0 < B n := λn, begin apply pow_pos, by norm_num end, /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closed_ball center radius` is included both in `f n` and in `closed_ball x δ`. We can also require `radius ≤ (1/2)^(n+1), to ensure we get a Cauchy sequence later. -/ have : ∀n x δ, ∃y r, δ > 0 → (r > 0 ∧ r ≤ B (n+1) ∧ closed_ball y r ⊆ (closed_ball x δ) ∩ f n), { assume n x δ, by_cases δpos : δ > 0, { have : x ∈ closure (f n) := by simpa only [(hd n).symm] using mem_univ x, rcases mem_closure_iff'.1 this (δ/2) (half_pos δpos) with ⟨y, ys, xy⟩, rw dist_comm at xy, rcases is_open_iff.1 (ho n) y ys with ⟨r, rpos, hr⟩, refine ⟨y, min (min (δ/2) (r/2)) (B (n+1)), λ_, ⟨_, _, λz hz, ⟨_, _⟩⟩⟩, show 0 < min (min (δ / 2) (r/2)) (B (n+1)), from lt_min (lt_min (half_pos δpos) (half_pos rpos)) (Bpos (n+1)), show min (min (δ / 2) (r/2)) (B (n+1)) ≤ B (n+1), from min_le_right _ _, show z ∈ closed_ball x δ, from calc dist z x ≤ dist z y + dist y x : dist_triangle _ _ _ ... ≤ (min (min (δ / 2) (r/2)) (B (n+1))) + (δ/2) : add_le_add hz (le_of_lt xy) ... ≤ δ/2 + δ/2 : add_le_add (le_trans (min_le_left _ _) (min_le_left _ _)) (le_refl _) ... = δ : add_halves _, show z ∈ f n, from hr (calc dist z y ≤ min (min (δ / 2) (r/2)) (B (n+1)) : hz ... ≤ r/2 : le_trans (min_le_left _ _) (min_le_right _ _) ... < r : half_lt_self rpos) }, { use [x, 0] }}, choose center radius H using this, refine subset.antisymm (subset_univ _) (λx hx, _), refine metric.mem_closure_iff'.2 (λε εpos, _), /- ε is positive. We have to find a point in the ball of radius ε around x belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closed_ball (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → (α × ℝ) := λn, nat.rec_on n (prod.mk x (min (ε/2) 1)) (λn p, prod.mk (center n p.1 p.2) (radius n p.1 p.2)), let c : ℕ → α := λn, (F n).1, let r : ℕ → ℝ := λn, (F n).2, have rpos : ∀n, r n > 0, { assume n, induction n with n hn, exact lt_min (half_pos εpos) (zero_lt_one), exact (H n (c n) (r n) hn).1 }, have rB : ∀n, r n ≤ B n, { assume n, induction n with n hn, exact min_le_right _ _, exact (H n (c n) (r n) (rpos n)).2.1 }, have incl : ∀n, closed_ball (c (n+1)) (r (n+1)) ⊆ (closed_ball (c n) (r n)) ∩ (f n) := λn, (H n (c n) (r n) (rpos n)).2.2, have cdist : ∀n, dist (c n) (c (n+1)) ≤ B n, { assume n, rw dist_comm, have A : c (n+1) ∈ closed_ball (c (n+1)) (r (n+1)) := mem_closed_ball_self (le_of_lt (rpos (n+1))), have I := calc closed_ball (c (n+1)) (r (n+1)) ⊆ closed_ball (c n) (r n) : subset.trans (incl n) (inter_subset_left _ _) ... ⊆ closed_ball (c n) (B n) : closed_ball_subset_closed_ball (rB n), exact I A }, have : cauchy_seq c, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact cdist n }, -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchy_seq_tendsto_of_complete this with ⟨y, ylim⟩, -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y, simp only [exists_prop, set.mem_Inter], have I : ∀n, ∀m ≥ n, closed_ball (c m) (r m) ⊆ closed_ball (c n) (r n), { assume n, refine nat.le_induction _ (λm hnm h, _), { exact subset.refl _ }, { exact subset.trans (incl m) (subset.trans (inter_subset_left _ _) h) }}, have yball : ∀n, y ∈ closed_ball (c n) (r n), { assume n, refine mem_of_closed_of_tendsto (by simp) ylim is_closed_ball _, simp only [filter.mem_at_top_sets, nonempty_of_inhabited, set.mem_preimage], exact ⟨n, λm hm, I n m hm (mem_closed_ball_self (le_of_lt (rpos m)))⟩ }, split, show ∀n, y ∈ f n, { assume n, have : closed_ball (c (n+1)) (r (n+1)) ⊆ f n := subset.trans (incl n) (inter_subset_right _ _), exact this (yball (n+1)) }, show dist x y < ε, from calc dist x y = dist y x : dist_comm _ _ ... ≤ r 0 : yball 0 ... < ε : lt_of_le_of_lt (min_le_left _ _) (half_lt_self εpos) end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_open {S : set (set α)} (ho : ∀s∈S, is_open s) (hS : countable S) (hd : ∀s∈S, closure s = univ) : closure (⋂₀S) = univ := begin by_cases h : S = ∅, { simp [h] }, { rcases exists_surjective_of_countable h hS with ⟨f, hf⟩, have F : ∀n, f n ∈ S := λn, by rw hf; exact mem_range_self _, rw [hf, sInter_range], exact dense_Inter_of_open_nat (λn, ho _ (F n)) (λn, hd _ (F n)) } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_open {S : set β} {f : β → set α} (ho : ∀s∈S, is_open (f s)) (hS : countable S) (hd : ∀s∈S, closure (f s) = univ) : closure (⋂s∈S, f s) = univ := begin rw ← sInter_image, apply dense_sInter_of_open, { rwa ball_image_iff }, { exact countable_image _ hS }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_open [encodable β] {f : β → set α} (ho : ∀s, is_open (f s)) (hd : ∀s, closure (f s) = univ) : closure (⋂s, f s) = univ := begin rw ← sInter_range, apply dense_sInter_of_open, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_Gδ {S : set (set α)} (ho : ∀s∈S, is_Gδ s) (hS : countable S) (hd : ∀s∈S, closure s = univ) : closure (⋂₀S) = univ := begin -- the result follows from the result for a countable intersection of dense open sets, -- by rewriting each set as a countable intersection of open sets, which are of course dense. have : ∀s : set α, ∃T : set (set α), s ∈ S → ((∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T)), { assume s, by_cases hs : s ∈ S, { simp [hs], exact ho s hs }, { simp [hs] }}, choose T hT using this, have : ⋂₀ S = ⋂₀ (⋃s∈S, T s) := (sInter_bUnion (λs hs, (hT s hs).2.2)).symm, rw this, refine dense_sInter_of_open (λt ht, _) (countable_bUnion hS (λs hs, (hT s hs).2.1)) (λt ht, _), show is_open t, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, exact (hT s hs).1 t tTs }, show closure t = univ, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, apply subset.antisymm (subset_univ _), rw ← (hd s hs), apply closure_mono, have := sInter_subset_of_mem tTs, rwa ← (hT s hs).2.2 at this } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_Gδ {S : set β} {f : β → set α} (ho : ∀s∈S, is_Gδ (f s)) (hS : countable S) (hd : ∀s∈S, closure (f s) = univ) : closure (⋂s∈S, f s) = univ := begin rw ← sInter_image, apply dense_sInter_of_Gδ, { rwa ball_image_iff }, { exact countable_image _ hS }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_Gδ [encodable β] {f : β → set α} (ho : ∀s, is_Gδ (f s)) (hd : ∀s, closure (f s) = univ) : closure (⋂s, f s) = univ := begin rw ← sInter_range, apply dense_sInter_of_Gδ, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bUnion_interior_of_closed {S : set β} {f : β → set α} (hc : ∀s∈S, is_closed (f s)) (hS : countable S) (hU : (⋃s∈S, f s) = univ) : closure (⋃s∈S, interior (f s)) = univ := begin let g := λs, - (frontier (f s)), have clos_g : closure (⋂s∈S, g s) = univ, { refine dense_bInter_of_open (λs hs, _) hS (λs hs, _), show is_open (g s), from is_open_compl_iff.2 is_closed_frontier, show closure (g s) = univ, { apply subset.antisymm (subset_univ _), simp [g, interior_frontier (hc s hs)] }}, have : (⋂s∈S, g s) ⊆ (⋃s∈S, interior (f s)), { assume x hx, have : x ∈ ⋃s∈S, f s, { have := mem_univ x, rwa ← hU at this }, rcases mem_bUnion_iff.1 this with ⟨s, hs, xs⟩, have : x ∈ g s := mem_bInter_iff.1 hx s hs, have : x ∈ interior (f s), { have : x ∈ f s \ (frontier (f s)) := mem_inter xs this, simpa [frontier, xs, closure_eq_of_is_closed (hc s hs)] using this }, exact mem_bUnion_iff.2 ⟨s, ⟨hs, this⟩⟩ }, have := closure_mono this, rw clos_g at this, exact subset.antisymm (subset_univ _) this end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with ⋃₀. -/ theorem dense_sUnion_interior_of_closed {S : set (set α)} (hc : ∀s∈S, is_closed s) (hS : countable S) (hU : (⋃₀ S) = univ) : closure (⋃s∈S, interior s) = univ := by rw sUnion_eq_bUnion at hU; exact dense_bUnion_interior_of_closed hc hS hU /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Union_interior_of_closed [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : closure (⋃s, interior (f s)) = univ := begin rw ← bUnion_univ, apply dense_bUnion_interior_of_closed, { simp [hc] }, { apply countable_encodable }, { rwa ← bUnion_univ at hU } end /-- One of the most useful consequences of Baire theorem: if a countable union of closed sets covers the space, then one of the sets has nonempty interior. -/ theorem nonempty_interior_of_Union_of_closed [n : nonempty α] [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : ∃s x ε, ε > 0 ∧ ball x ε ⊆ f s := begin have : ∃s, interior (f s) ≠ ∅, { by_contradiction h, simp only [not_exists_not, ne.def] at h, have := calc ∅ = closure (⋃s, interior (f s)) : by simp [h] ... = univ : dense_Union_interior_of_closed hc hU, exact nonempty_iff_univ_ne_empty.1 n this.symm }, rcases this with ⟨s, hs⟩, rcases ne_empty_iff_exists_mem.1 hs with ⟨x, hx⟩, rcases mem_nhds_iff.1 (mem_interior_iff_mem_nhds.1 hx) with ⟨ε, εpos, hε⟩, exact ⟨s, x, ε, εpos, hε⟩, end end Baire_theorem
0a61760528782cf8e2ca29cb0a2216e15a668c99
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/interactive.lean
093b189b928e4ae5c839e30d285e69bf1c7c3c14
[ "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
23,613
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.tactic init.meta.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.category.combinators import init.meta.lean.parser init.meta.quote open lean open lean.parser local postfix ?:9001 := optional local postfix *:9001 := many namespace interactive /-- (parse p) as the parameter type of an interactive tactic will instruct the Lean parser to run `p` when parsing the parameter and to pass the parsed value as an argument to the tactic. -/ @[reducible] meta def parse {α : Type} [has_quote α] (p : parser α) : Type := α namespace types variables {α β : Type} meta def list_of (p : parser α) := tk "[" *> sep_by "," p <* tk "]" /-- A 'tactic expression', which uses right-binding power 2 so that it is terminated by '<|>' (rbp 2), ';' (rbp 1), and ',' (rbp 0). It should be used for any (potentially) trailing expression parameters. -/ meta def texpr := qexpr 2 meta def using_ident := (tk "using" *> ident)? meta def with_ident_list := (tk "with" *> ident*) <|> return [] meta def without_ident_list := (tk "without" *> ident*) <|> return [] meta def location := (tk "at" *> ident*) <|> return [] meta def qexpr_list := list_of (qexpr 0) meta def opt_qexpr_list := qexpr_list <|> return [] meta def qexpr_list_or_texpr := qexpr_list <|> return <$> texpr end types end interactive namespace tactic meta def report_resolve_name_failure {α : Type} (e : expr) (n : name) : tactic α := if e^.is_choice_macro then fail ("failed to resolve name '" ++ to_string n ++ "', it is overloaded") else fail ("failed to resolve name '" ++ to_string n ++ "', unexpected result") /- allows metavars and report errors -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt tt /- doesn't allows metavars and report errors -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff tt namespace interactive open interactive interactive.types expr /- itactic: parse a nested "interactive" tactic. That is, parse `(` tactic `)` -/ meta def itactic : Type := tactic unit /- irtactic: parse a nested "interactive" tactic. That is, parse `(` tactic `)` It is similar to itactic, but the interactive mode will report errors when any of the nested tactics fail. This is good for tactics such as async and abstract. -/ meta def irtactic : Type := tactic unit /-- This tactic applies to a goal that is either a Pi/forall or starts with a let binder. If the current goal is a Pi/forall `∀ x:T, U` (resp `let x:=t in U`) then intro puts `x:T` (resp `x:=t`) in the local context. The new subgoal target is `U`. If the goal is an arrow `T → U`, then it puts in the local context either `h:T`, and the new goal target is `U`. If the goal is neither a Pi/forall nor starting with a let definition, the tactic `intro` applies the tactic `whnf` until the tactic `intro` can be applied or the goal is not `head-reducible`. -/ meta def intro : parse ident? → tactic unit | none := intro1 >> skip | (some h) := tactic.intro h >> skip /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h_1 ... h_n` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident* → tactic unit | [] := tactic.intros >> skip | hs := intro_lst hs >> skip /-- The tactic `rename h₁ h₂` renames hypothesis `h₁` into `h₂` in the current local context. -/ meta def rename : parse ident → parse ident → tactic unit := tactic.rename /-- This tactic applies to any goal. The argument term is a term well-formed in the local context of the main goal. The tactic apply tries to match the current goal against the conclusion of the type of term. If it succeeds, then the tactic returns as many subgoals as the number of non-dependent premises that have not been fixed by type inference or type class resolution. The tactic `apply` uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.apply /-- Similar to the `apply` tactic, but it also creates subgoals for dependent premises that have not been fixed by type inference or type class resolution. -/ meta def fapply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.fapply /-- This tactic tries to close the main goal `... |- U` using type class resolution. It succeeds if it generates a term of type `U` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic applies to any goal. It behaves like `exact` with a big difference: the user can leave some holes `_` in the term. `refine` will generate as many subgoals as there are holes in the term. Note that some holes may be implicit. The type of holes must be either synthesized by the system or declared by an explicit type ascription like (e.g., `(_ : nat → Prop)`). -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q tt /-- This tactic looks in the local context for an hypothesis which type is equal to the goal target. If it is the case, the subgoal is proved. Otherwise, it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- This tactic applies to any goal. `change U` replaces the main goal target `T` with `U` providing that `U` is well-formed with respect to the main goal local context, and `T` and `U` are definitionally equal. -/ meta def change (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.change /-- This tactic applies to any goal. It gives directly the exact proof term of the goal. Let `T` be our goal, let `p` be a term of type `U` then `exact p` succeeds iff `T` and `U` are definitionally equal. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact private meta def get_locals : list name → tactic (list expr) | [] := return [] | (n::ns) := do h ← get_local n, hs ← get_locals ns, return (h::hs) /-- `revert h₁ ... hₙ` applies to any goal with hypotheses `h₁` ... `hₙ`. It moves the hypotheses and its dependencies to the goal target. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := do hs ← get_locals ids, revert_lst hs, skip /- Return (some a) iff p is of the form (- a) -/ private meta def is_neg (p : pexpr) : option pexpr := /- Remark: we use the low-level to_raw_expr and of_raw_expr to be able to pattern match pre-terms. This is a low-level trick (aka hack). -/ match pexpr.to_raw_expr p with | (app (const c []) arg) := if c = `neg then some (pexpr.of_raw_expr arg) else none | _ := none end private meta def resolve_name' (n : name) : tactic expr := do { e ← resolve_name n, match e with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := return e end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ private meta def to_expr' (p : pexpr) : tactic expr := let e := pexpr.to_raw_expr p in match e with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | _ := i_to_expr p end private meta def maybe_save_info : option pos → tactic unit | (some p) := save_info p | none := skip private meta def symm_expr := bool × expr × option pos private meta def to_symm_expr_list : list pexpr → tactic (list symm_expr) | [] := return [] | (p::ps) := match is_neg p with | some a := do r ← to_expr' a, rs ← to_symm_expr_list ps, return ((tt, r, pexpr.pos p) :: rs) | none := do r ← to_expr' p, rs ← to_symm_expr_list ps, return ((ff, r, pexpr.pos p) :: rs) end private meta def rw_goal : transparency → list symm_expr → tactic unit | m [] := return () | m ((symm, e, pos)::es) := maybe_save_info pos >> rewrite_core m tt tt occurrences.all symm e >> rw_goal m es private meta def rw_hyp : transparency → list symm_expr → name → tactic unit | m [] hname := return () | m ((symm, e, pos)::es) hname := do h ← get_local hname, maybe_save_info pos, rewrite_at_core m tt tt occurrences.all symm e h, rw_hyp m es hname private meta def rw_hyps : transparency → list symm_expr → list name → tactic unit | m es [] := return () | m es (h::hs) := rw_hyp m es h >> rw_hyps m es hs private meta def rw_core (m : transparency) (hs : parse qexpr_list_or_texpr) (loc : parse location) : tactic unit := do hlist ← to_symm_expr_list hs, match loc with | [] := rw_goal m hlist >> try (reflexivity reducible) | hs := rw_hyps m hlist hs >> try (reflexivity reducible) end meta def rewrite : parse qexpr_list_or_texpr → parse location → tactic unit := rw_core reducible meta def rw : parse qexpr_list_or_texpr → parse location → tactic unit := rewrite /- rewrite followed by assumption -/ meta def rwa (q : parse qexpr_list_or_texpr) (l : parse location) : tactic unit := rewrite q l >> try assumption meta def erewrite : parse qexpr_list_or_texpr → parse location → tactic unit := rw_core semireducible meta def erw : parse qexpr_list_or_texpr → parse location → tactic unit := erewrite private meta def get_type_name (e : expr) : tactic name := do e_type ← infer_type e >>= whnf, (const I ls) ← return $ get_app_fn e_type, return I meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.induction e ids rec_name, return () meta def cases (p : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.cases e ids meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct meta def generalize (p : parse qexpr) (x : parse ident) : tactic unit := do e ← i_to_expr p, tactic.generalize e x meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := tactic.admit /-- This tactic applies to any goal. The contradiction tactic attempts to find in the current local context an hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction meta def repeat : itactic → tactic unit := tactic.repeat meta def try : itactic → tactic unit := tactic.try meta def solve1 : itactic → tactic unit := tactic.solve1 meta def abstract (id : parse ident? ) (tac : irtactic) : tactic unit := tactic.abstract tac id meta def all_goals : itactic → tactic unit := tactic.all_goals meta def any_goals : itactic → tactic unit := tactic.any_goals meta def focus (tac : irtactic) : tactic unit := tactic.focus [tac] /-- This tactic applies to any goal. `assert h : T` adds a new hypothesis of name `h` and type `T` to the current goal and opens a new subgoal with target `T`. The new subgoal becomes the main goal. -/ meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.assert h e meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.define h e /-- This tactic applies to any goal. `assertv h : T := p` adds a new hypothesis of name `h` and type `T` to the current goal if `p` a term of type `T`. -/ meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%q₂ : %%t), tactic.assertv h t v meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%q₂ : %%t), tactic.definev h t v meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.note h p meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.pose h p /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {α : Type} [has_to_tactic_format α] (a : α) : tactic unit := tactic.trace a meta def existsi (e : parse texpr) : tactic unit := i_to_expr e >>= tactic.existsi /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := tactic.constructor meta def left : tactic unit := tactic.left meta def right : tactic unit := tactic.right meta def split : tactic unit := tactic.split meta def exfalso : tactic unit := tactic.exfalso /-- The injection tactic is based on the fact that constructors of inductive datatypes are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c t₂)` are two terms that are equal then `t₁` and `t₂` are equal too. If `q` is a proof of a statement of conclusion `t₁ = t₂`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `t₂` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `t₂` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds to new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ h₂` uses the names `h₁` an `h₂` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs private meta def add_simps : simp_lemmas → list name → tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s^.add_simp n, add_simps s' ns private meta def report_invalid_simp_lemma {α : Type} (n : name): tactic α := fail ("invalid simplification lemma '" ++ to_string n ++ "' (use command 'set_option trace.simp_lemmas true' for more details)") private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (n : name) (ref : expr) : tactic simp_lemmas := do e ← resolve_name n, match e with | expr.const n _ := (do b ← is_valid_simp_lemma_cnst reducible n, guard b, save_const_type_info n ref, s^.add_simp n) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns^.length > 0), save_const_type_info n ref, add_simps s eqns) <|> report_invalid_simp_lemma n | _ := (do b ← is_valid_simp_lemma reducible e, guard b, try (save_type_info e ref), s^.add e) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (p : pexpr) : tactic simp_lemmas := let e := pexpr.to_raw_expr p in match e with | (const c []) := simp_lemmas.resolve_and_add s c e | (local_const c _ _ _) := simp_lemmas.resolve_and_add s c e | _ := do new_e ← i_to_expr p, s^.add new_e end private meta def simp_lemmas.append_pexprs : simp_lemmas → list pexpr → tactic simp_lemmas | s [] := return s | s (l::ls) := do new_s ← simp_lemmas.add_pexpr s l, simp_lemmas.append_pexprs new_s ls private meta def mk_simp_set (attr_names : list name) (hs : list pexpr) (ex : list name) : tactic simp_lemmas := do s₀ ← join_user_simp_lemmas attr_names, s₁ ← simp_lemmas.append_pexprs s₀ hs, return $ simp_lemmas.erase s₁ ex private meta def simp_goal (cfg : simp_config) : simp_lemmas → tactic unit | s := do (new_target, Heq) ← target >>= simplify_core cfg s `eq, tactic.assert `Htarget new_target, swap, Ht ← get_local `Htarget, mk_eq_mpr Heq Ht >>= tactic.exact private meta def simp_hyp (cfg : simp_config) (s : simp_lemmas) (h_name : name) : tactic unit := do h ← get_local h_name, htype ← infer_type h, (new_htype, eqpr) ← simplify_core cfg s `eq htype, tactic.assert (expr.local_pp_name h) new_htype, mk_eq_mp eqpr h >>= tactic.exact, try $ tactic.clear h private meta def simp_hyps (cfg : simp_config) : simp_lemmas → list name → tactic unit | s [] := skip | s (h::hs) := simp_hyp cfg s h >> simp_hyps s hs private meta def simp_core (cfg : simp_config) (ctx : list expr) (hs : list pexpr) (attr_names : list name) (ids : list name) (loc : list name) : tactic unit := do s ← mk_simp_set attr_names hs ids, s ← s^.append ctx, match loc : _ → tactic unit with | [] := simp_goal cfg s | _ := simp_hyps cfg s loc end, try tactic.triv, try (tactic.reflexivity reducible) /-- This tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. - `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. - `simp [h_1, ..., h_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `h_i`s. The `h_i`'s are terms. If a `h_i` is a definition `f`, then the equational lemmas associated with `f` are used. This is a convenient way to "unfold" `f`. - `simp without id_1 ... id_n` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `id_i`s. - `simp at h` simplifies the non dependent hypothesis `h : T`. The tactic fails if the target or another hypothesis depends on `h`. - `simp with attr` simplifies the main goal target using the lemmas tagged with the attribute `[attr]`. -/ meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (loc : parse location) (cfg : simp_config := {}) : tactic unit := simp_core cfg [] hs attr_names ids loc /-- Similar to the `simp` tactic, but adds all applicable hypotheses as simplification rules. -/ meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do ctx ← collect_ctx_simps, simp_core cfg ctx hs attr_names ids [] meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := simp_using_hs hs attr_names ids cfg meta def simp_intros (ids : parse ident*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simp_intros_using s cfg | ns := simp_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) meta def simph_intros (ids : parse ident*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simph_intros_using s cfg | ns := simph_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) private meta def dsimp_hyps (s : simp_lemmas) : list name → tactic unit | [] := skip | (h::hs) := get_local h >>= dsimp_at_core s meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : parse location → tactic unit | [] := do s ← mk_simp_set attr_names es ids, tactic.dsimp_core s | hs := do s ← mk_simp_set attr_names es ids, dsimp_hyps s hs /-- This tactic applies to a goal that has the form `t ~ u` where `~` is a reflexive relation. That is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity meta def symmetry : tactic unit := tactic.symmetry meta def transitivity : tactic unit := tactic.transitivity meta def ac_reflexivity : tactic unit := tactic.ac_refl meta def ac_refl : tactic unit := tactic.ac_refl meta def cc : tactic unit := tactic.cc meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) meta def clear : parse ident* → tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name → list name → tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env^.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env^.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name → tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) private meta def dunfold_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= dunfold_at cs >> dunfold_hyps cs hs meta def dunfold : parse ident* → parse location → tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.dunfold new_cs | cs hs := do new_cs ← to_qualified_names cs, dunfold_hyps new_cs hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold : parse ident* → parse location → tactic unit := dunfold private meta def dunfold_hyps_occs : name → occurrences → list name → tactic unit | c occs [] := skip | c occs (h::hs) := get_local h >>= dunfold_core_at occs [c] >> dunfold_hyps_occs c occs hs meta def dunfold_occs : parse ident → parse location → list nat → tactic unit | c [] ps := do new_c ← to_qualified_name c, tactic.dunfold_occs_of ps new_c | c hs ps := do new_c ← to_qualified_name c, dunfold_hyps_occs new_c (occurrences.pos ps) hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold_occs : parse ident → parse location → list nat → tactic unit := dunfold_occs private meta def delta_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_at cs >> dunfold_hyps cs hs meta def delta : parse ident* → parse location → tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.delta new_cs | cs hs := do new_cs ← to_qualified_names cs, delta_hyps new_cs hs end interactive end tactic
250dee62caadb65b7743d8abba2a370ffbfafc7c
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/function/simple_func_dense_lp.lean
afefae1a8803e5ec2dbcb012b1e57353eeb35a5e
[ "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
41,430
lean
/- Copyright (c) 2022 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth -/ import measure_theory.function.l1_space import measure_theory.function.simple_func_dense /-! # Density of simple functions Show that each `Lᵖ` Borel measurable function can be approximated in `Lᵖ` norm by a sequence of simple functions. ## Main definitions * `measure_theory.Lp.simple_func`, the type of `Lp` simple functions * `coe_to_Lp`, the embedding of `Lp.simple_func E p μ` into `Lp E p μ` ## Main results * `tendsto_approx_on_univ_Lp` (Lᵖ convergence): If `E` is a `normed_group` and `f` is measurable and `mem_ℒp` (for `p < ∞`), then the simple functions `simple_func.approx_on f hf s 0 h₀ n` may be considered as elements of `Lp E p μ`, and they tend in Lᵖ to `f`. * `Lp.simple_func.dense_embedding`: the embedding `coe_to_Lp` of the `Lp` simple functions into `Lp` is dense. * `Lp.simple_func.induction`, `Lp.induction`, `mem_ℒp.induction`, `integrable.induction`: to prove a predicate for all elements of one of these classes of functions, it suffices to check that it behaves correctly on simple functions. ## TODO For `E` finite-dimensional, simple functions `α →ₛ E` are dense in L^∞ -- prove this. ## Notations * `α →ₛ β` (local notation): the type of simple functions `α → β`. * `α →₁ₛ[μ] E`: the type of `L1` simple functions `α → β`. -/ noncomputable theory open set function filter topological_space ennreal emetric finset open_locale classical topological_space ennreal measure_theory big_operators variables {α β ι E F 𝕜 : Type*} namespace measure_theory local infixr ` →ₛ `:25 := simple_func namespace simple_func /-! ### Lp approximation by simple functions -/ section Lp variables [measurable_space β] variables [measurable_space E] [normed_group E] [normed_group F] {q : ℝ} {p : ℝ≥0∞} lemma nnnorm_approx_on_le [opens_measurable_space E] {f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) : ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ≤ ∥f x - y₀∥₊ := begin have := edist_approx_on_le hf h₀ x n, rw edist_comm y₀ at this, simp only [edist_nndist, nndist_eq_nnnorm] at this, exact_mod_cast this end lemma norm_approx_on_y₀_le [opens_measurable_space E] {f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) : ∥approx_on f hf s y₀ h₀ n x - y₀∥ ≤ ∥f x - y₀∥ + ∥f x - y₀∥ := begin have := edist_approx_on_y0_le hf h₀ x n, repeat { rw [edist_comm y₀, edist_eq_coe_nnnorm_sub] at this }, exact_mod_cast this, end lemma norm_approx_on_zero_le [opens_measurable_space E] {f : β → E} (hf : measurable f) {s : set E} (h₀ : (0 : E) ∈ s) [separable_space s] (x : β) (n : ℕ) : ∥approx_on f hf s 0 h₀ n x∥ ≤ ∥f x∥ + ∥f x∥ := begin have := edist_approx_on_y0_le hf h₀ x n, simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this, exact_mod_cast this, end lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E] {f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (hp_ne_top : p ≠ ∞) {μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : snorm (λ x, f x - y₀) p μ < ∞) : tendsto (λ n, snorm (approx_on f hf s y₀ h₀ n - f) p μ) at_top (𝓝 0) := begin by_cases hp_zero : p = 0, { simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds }, have hp : 0 < p.to_real := to_real_pos hp_zero hp_ne_top, suffices : tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ^ p.to_real ∂μ) at_top (𝓝 0), { simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top], convert continuous_rpow_const.continuous_at.tendsto.comp this; simp [_root_.inv_pos.mpr hp] }, -- We simply check the conditions of the Dominated Convergence Theorem: -- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable have hF_meas : ∀ n, measurable (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real), { simpa only [← edist_eq_coe_nnnorm_sub] using λ n, (approx_on f hf s y₀ h₀ n).measurable_bind (λ y x, (edist y (f x)) ^ p.to_real) (λ y, (measurable_edist_right.comp hf).pow_const p.to_real) }, -- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly -- bounded, at any given point, by `λ x, ∥f x - y₀∥ ^ p.to_real` have h_bound : ∀ n, (λ x, (∥approx_on f hf s y₀ h₀ n x - f x∥₊ : ℝ≥0∞) ^ p.to_real) ≤ᵐ[μ] (λ x, ∥f x - y₀∥₊ ^ p.to_real), { exact λ n, eventually_of_forall (λ x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf h₀ x n)) to_real_nonneg) }, -- (3) The bounding function `λ x, ∥f x - y₀∥ ^ p.to_real` has finite integral have h_fin : ∫⁻ (a : β), ∥f a - y₀∥₊ ^ p.to_real ∂μ ≠ ⊤, from (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi).ne, -- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise -- to zero have h_lim : ∀ᵐ (a : β) ∂μ, tendsto (λ n, (∥approx_on f hf s y₀ h₀ n a - f a∥₊ : ℝ≥0∞) ^ p.to_real) at_top (𝓝 0), { filter_upwards [hμ] with a ha, have : tendsto (λ n, (approx_on f hf s y₀ h₀ n) a - f a) at_top (𝓝 (f a - f a)), { exact (tendsto_approx_on hf h₀ ha).sub tendsto_const_nhds }, convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm), simp [zero_rpow_of_pos hp] }, -- Then we apply the Dominated Convergence Theorem simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim, end lemma mem_ℒp_approx_on [borel_space E] {f : β → E} {μ : measure β} (fmeas : measurable f) (hf : mem_ℒp f p μ) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (hi₀ : mem_ℒp (λ x, y₀) p μ) (n : ℕ) : mem_ℒp (approx_on f fmeas s y₀ h₀ n) p μ := begin refine ⟨(approx_on f fmeas s y₀ h₀ n).ae_strongly_measurable, _⟩, suffices : snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ < ⊤, { have : mem_ℒp (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ := ⟨(approx_on f fmeas s y₀ h₀ n - const β y₀).ae_strongly_measurable, this⟩, convert snorm_add_lt_top this hi₀, ext x, simp }, have hf' : mem_ℒp (λ x, ∥f x - y₀∥) p μ, { have h_meas : measurable (λ x, ∥f x - y₀∥), { simp only [← dist_eq_norm], exact (continuous_id.dist continuous_const).measurable.comp fmeas }, refine ⟨h_meas.ae_measurable.ae_strongly_measurable, _⟩, rw snorm_norm, convert snorm_add_lt_top hf hi₀.neg, ext x, simp [sub_eq_add_neg] }, have : ∀ᵐ x ∂μ, ∥approx_on f fmeas s y₀ h₀ n x - y₀∥ ≤ ∥(∥f x - y₀∥ + ∥f x - y₀∥)∥, { refine eventually_of_forall _, intros x, convert norm_approx_on_y₀_le fmeas h₀ x n, rw [real.norm_eq_abs, abs_of_nonneg], exact add_nonneg (norm_nonneg _) (norm_nonneg _) }, calc snorm (λ x, approx_on f fmeas s y₀ h₀ n x - y₀) p μ ≤ snorm (λ x, ∥f x - y₀∥ + ∥f x - y₀∥) p μ : snorm_mono_ae this ... < ⊤ : snorm_add_lt_top hf' hf', end lemma tendsto_approx_on_range_Lp_snorm [borel_space E] {f : β → E} (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f) [separable_space (range f ∪ {0} : set E)] (hf : snorm f p μ < ∞) : tendsto (λ n, snorm (approx_on f fmeas (range f ∪ {0}) 0 (by simp) n - f) p μ) at_top (𝓝 0) := begin refine tendsto_approx_on_Lp_snorm fmeas _ hp_ne_top _ _, { apply eventually_of_forall, assume x, apply subset_closure, simp }, { simpa using hf } end lemma mem_ℒp_approx_on_range [borel_space E] {f : β → E} {μ : measure β} (fmeas : measurable f) [separable_space (range f ∪ {0} : set E)] (hf : mem_ℒp f p μ) (n : ℕ) : mem_ℒp (approx_on f fmeas (range f ∪ {0}) 0 (by simp) n) p μ := mem_ℒp_approx_on fmeas hf (by simp) zero_mem_ℒp n lemma tendsto_approx_on_range_Lp [borel_space E] {f : β → E} [hp : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) {μ : measure β} (fmeas : measurable f) [separable_space (range f ∪ {0} : set E)] (hf : mem_ℒp f p μ) : tendsto (λ n, (mem_ℒp_approx_on_range fmeas hf n).to_Lp (approx_on f fmeas (range f ∪ {0}) 0 (by simp) n)) at_top (𝓝 (hf.to_Lp f)) := by simpa only [Lp.tendsto_Lp_iff_tendsto_ℒp''] using tendsto_approx_on_range_Lp_snorm hp_ne_top fmeas hf.2 end Lp /-! ### L1 approximation by simple functions -/ section integrable variables [measurable_space β] variables [measurable_space E] [normed_group E] lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E] {f : β → E} (hf : measurable f) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] {μ : measure β} (hμ : ∀ᵐ x ∂μ, f x ∈ closure s) (hi : has_finite_integral (λ x, f x - y₀) μ) : tendsto (λ n, ∫⁻ x, ∥approx_on f hf s y₀ h₀ n x - f x∥₊ ∂μ) at_top (𝓝 0) := by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf h₀ one_ne_top hμ (by simpa [snorm_one_eq_lintegral_nnnorm] using hi) lemma integrable_approx_on [borel_space E] {f : β → E} {μ : measure β} (fmeas : measurable f) (hf : integrable f μ) {s : set E} {y₀ : E} (h₀ : y₀ ∈ s) [separable_space s] (hi₀ : integrable (λ x, y₀) μ) (n : ℕ) : integrable (approx_on f fmeas s y₀ h₀ n) μ := begin rw ← mem_ℒp_one_iff_integrable at hf hi₀ ⊢, exact mem_ℒp_approx_on fmeas hf h₀ hi₀ n, end lemma tendsto_approx_on_range_L1_nnnorm [opens_measurable_space E] {f : β → E} {μ : measure β} [separable_space (range f ∪ {0} : set E)] (fmeas : measurable f) (hf : integrable f μ) : tendsto (λ n, ∫⁻ x, ∥approx_on f fmeas (range f ∪ {0}) 0 (by simp) n x - f x∥₊ ∂μ) at_top (𝓝 0) := begin apply tendsto_approx_on_L1_nnnorm fmeas, { apply eventually_of_forall, assume x, apply subset_closure, simp }, { simpa using hf.2 } end lemma integrable_approx_on_range [borel_space E] {f : β → E} {μ : measure β} (fmeas : measurable f) [separable_space (range f ∪ {0} : set E)] (hf : integrable f μ) (n : ℕ) : integrable (approx_on f fmeas (range f ∪ {0}) 0 (by simp) n) μ := integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n end integrable section simple_func_properties variables [measurable_space α] variables [normed_group E] [normed_group F] variables {μ : measure α} {p : ℝ≥0∞} /-! ### Properties of simple functions in `Lp` spaces A simple function `f : α →ₛ E` into a normed group `E` verifies, for a measure `μ`: - `mem_ℒp f 0 μ` and `mem_ℒp f ∞ μ`, since `f` is a.e.-measurable and bounded, - for `0 < p < ∞`, `mem_ℒp f p μ ↔ integrable f μ ↔ f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞`. -/ lemma exists_forall_norm_le (f : α →ₛ F) : ∃ C, ∀ x, ∥f x∥ ≤ C := exists_forall_le (f.map (λ x, ∥x∥)) lemma mem_ℒp_zero (f : α →ₛ E) (μ : measure α) : mem_ℒp f 0 μ := mem_ℒp_zero_iff_ae_strongly_measurable.mpr f.ae_strongly_measurable lemma mem_ℒp_top (f : α →ₛ E) (μ : measure α) : mem_ℒp f ∞ μ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_ℒp_top_of_bound f.ae_strongly_measurable C $ eventually_of_forall hfC protected lemma snorm'_eq {p : ℝ} (f : α →ₛ F) (μ : measure α) : snorm' f p μ = (∑ y in f.range, (∥y∥₊ : ℝ≥0∞) ^ p * μ (f ⁻¹' {y})) ^ (1/p) := have h_map : (λ a, (∥f a∥₊ : ℝ≥0∞) ^ p) = f.map (λ a : F, (∥a∥₊ : ℝ≥0∞) ^ p), by simp, by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral] lemma measure_preimage_lt_top_of_mem_ℒp (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) (f : α →ₛ E) (hf : mem_ℒp f p μ) (y : E) (hy_ne : y ≠ 0) : μ (f ⁻¹' {y}) < ∞ := begin have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos hp_pos hp_ne_top, have hf_snorm := mem_ℒp.snorm_lt_top hf, rw [snorm_eq_snorm' hp_pos hp_ne_top, f.snorm'_eq, ← @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]), @ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]), ennreal.sum_lt_top_iff] at hf_snorm, by_cases hyf : y ∈ f.range, swap, { suffices h_empty : f ⁻¹' {y} = ∅, by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, }, ext1 x, rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false], refine λ hxy, hyf _, rw [mem_range, set.mem_range], exact ⟨x, hxy⟩, }, specialize hf_snorm y hyf, rw ennreal.mul_lt_top_iff at hf_snorm, cases hf_snorm, { exact hf_snorm.2, }, cases hf_snorm, { refine absurd _ hy_ne, simpa [hp_pos_real] using hf_snorm, }, { simp [hf_snorm], }, end lemma mem_ℒp_of_finite_measure_preimage (p : ℝ≥0∞) {f : α →ₛ E} (hf : ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞) : mem_ℒp f p μ := begin by_cases hp0 : p = 0, { rw [hp0, mem_ℒp_zero_iff_ae_strongly_measurable], exact f.ae_strongly_measurable, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_ℒp_top f μ, }, refine ⟨f.ae_strongly_measurable, _⟩, rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq], refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (λ y hy, _)).ne, by_cases hy0 : y = 0, { simp [hy0, ennreal.to_real_pos hp0 hp_top], }, { refine ennreal.mul_lt_top _ (hf y hy0).ne, exact (ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top).ne }, end lemma mem_ℒp_iff {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp f p μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ := ⟨λ h, measure_preimage_lt_top_of_mem_ℒp hp_pos hp_ne_top f h, λ h, mem_ℒp_of_finite_measure_preimage p h⟩ lemma integrable_iff {f : α →ₛ E} : integrable f μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ := mem_ℒp_one_iff_integrable.symm.trans $ mem_ℒp_iff ennreal.zero_lt_one.ne' ennreal.coe_ne_top lemma mem_ℒp_iff_integrable {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp f p μ ↔ integrable f μ := (mem_ℒp_iff hp_pos hp_ne_top).trans integrable_iff.symm lemma mem_ℒp_iff_fin_meas_supp {f : α →ₛ E} (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp f p μ ↔ f.fin_meas_supp μ := (mem_ℒp_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm lemma integrable_iff_fin_meas_supp {f : α →ₛ E} : integrable f μ ↔ f.fin_meas_supp μ := integrable_iff.trans fin_meas_supp_iff.symm lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair {f : α →ₛ E} {g : α →ₛ F} : integrable f μ → integrable g μ → integrable (pair f g) μ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair lemma mem_ℒp_of_is_finite_measure (f : α →ₛ E) (p : ℝ≥0∞) (μ : measure α) [is_finite_measure μ] : mem_ℒp f p μ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_ℒp.of_bound f.ae_strongly_measurable C $ eventually_of_forall hfC lemma integrable_of_is_finite_measure [is_finite_measure μ] (f : α →ₛ E) : integrable f μ := mem_ℒp_one_iff_integrable.mp (f.mem_ℒp_of_is_finite_measure 1 μ) lemma measure_preimage_lt_top_of_integrable (f : α →ₛ E) (hf : integrable f μ) {x : E} (hx : x ≠ 0) : μ (f ⁻¹' {x}) < ∞ := integrable_iff.mp hf x hx lemma measure_support_lt_top [has_zero β] (f : α →ₛ β) (hf : ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞) : μ (support f) < ∞ := begin rw support_eq, refine (measure_bUnion_finset_le _ _).trans_lt (ennreal.sum_lt_top_iff.mpr (λ y hy, _)), rw finset.mem_filter at hy, exact hf y hy.2, end lemma measure_support_lt_top_of_mem_ℒp (f : α →ₛ E) (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : μ (support f) < ∞ := f.measure_support_lt_top ((mem_ℒp_iff hp_ne_zero hp_ne_top).mp hf) lemma measure_support_lt_top_of_integrable (f : α →ₛ E) (hf : integrable f μ) : μ (support f) < ∞ := f.measure_support_lt_top (integrable_iff.mp hf) lemma measure_lt_top_of_mem_ℒp_indicator (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) {c : E} (hc : c ≠ 0) {s : set α} (hs : measurable_set s) (hcs : mem_ℒp ((const α c).piecewise s hs (const α 0)) p μ) : μ s < ⊤ := begin have : function.support (const α c) = set.univ := function.support_const hc, simpa only [mem_ℒp_iff_fin_meas_supp hp_pos hp_ne_top, fin_meas_supp_iff_support, support_indicator, set.inter_univ, this] using hcs end end simple_func_properties end simple_func /-! Construction of the space of `Lp` simple functions, and its dense embedding into `Lp`. -/ namespace Lp open ae_eq_fun variables [measurable_space α] [normed_group E] [normed_group F] (p : ℝ≥0∞) (μ : measure α) variables (E) /-- `Lp.simple_func` is a subspace of Lp consisting of equivalence classes of an integrable simple function. -/ def simple_func : add_subgroup (Lp E p μ) := { carrier := {f : Lp E p μ | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_strongly_measurable : α →ₘ[μ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [←hs, ←ht, ae_eq_fun.mk_add_mk, add_subgroup.coe_add, ae_eq_fun.mk_eq_mk, simple_func.coe_add]⟩, neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [←hs, ae_eq_fun.neg_mk, simple_func.coe_neg, ae_eq_fun.mk_eq_mk, add_subgroup.coe_neg]⟩ } variables {E p μ} namespace simple_func section instances /-! Simple functions in Lp space form a `normed_space`. -/ @[norm_cast] lemma coe_coe (f : Lp.simple_func E p μ) : ⇑(f : Lp E p μ) = f := rfl protected lemma eq' {f g : Lp.simple_func E p μ} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g := subtype.eq ∘ subtype.eq /-! Implementation note: If `Lp.simple_func E p μ` were defined as a `𝕜`-submodule of `Lp E p μ`, then the next few lemmas, putting a normed `𝕜`-group structure on `Lp.simple_func E p μ`, would be unnecessary. But instead, `Lp.simple_func E p μ` is defined as an `add_subgroup` of `Lp E p μ`, which does not permit this (but has the advantage of working when `E` itself is a normed group, i.e. has no scalar action). -/ variables [normed_field 𝕜] [normed_space 𝕜 E] /-- If `E` is a normed space, `Lp.simple_func E p μ` is a `has_smul`. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def has_smul : has_smul 𝕜 (Lp.simple_func E p μ) := ⟨λ k f, ⟨k • f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k • s, apply eq.trans (ae_eq_fun.smul_mk k s s.ae_strongly_measurable).symm _, rw hs, refl, end ⟩⟩ local attribute [instance] simple_func.has_smul @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : Lp.simple_func E p μ) : ((c • f : Lp.simple_func E p μ) : Lp E p μ) = c • (f : Lp E p μ) := rfl /-- If `E` is a normed space, `Lp.simple_func E p μ` is a module. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def module : module 𝕜 (Lp.simple_func E p μ) := { one_smul := λf, by { ext1, exact one_smul _ _ }, mul_smul := λx y f, by { ext1, exact mul_smul _ _ _ }, smul_add := λx f g, by { ext1, exact smul_add _ _ _ }, smul_zero := λx, by { ext1, exact smul_zero _ }, add_smul := λx y f, by { ext1, exact add_smul _ _ _ }, zero_smul := λf, by { ext1, exact zero_smul _ _ } } local attribute [instance] simple_func.module /-- If `E` is a normed space, `Lp.simple_func E p μ` is a normed space. Not declared as an instance as it is (as of writing) used only in the construction of the Bochner integral. -/ protected def normed_space [fact (1 ≤ p)] : normed_space 𝕜 (Lp.simple_func E p μ) := ⟨ λc f, by { rw [add_subgroup.coe_norm, add_subgroup.coe_norm, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.module simple_func.normed_space section to_Lp /-- Construct the equivalence class `[f]` of a simple function `f` satisfying `mem_ℒp`. -/ @[reducible] def to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) : (Lp.simple_func E p μ) := ⟨hf.to_Lp f, ⟨f, rfl⟩⟩ lemma to_Lp_eq_to_Lp (f : α →ₛ E) (hf : mem_ℒp f p μ) : (to_Lp f hf : Lp E p μ) = hf.to_Lp f := rfl lemma to_Lp_eq_mk (f : α →ₛ E) (hf : mem_ℒp f p μ) : (to_Lp f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_strongly_measurable := rfl lemma to_Lp_zero : to_Lp (0 : α →ₛ E) zero_mem_ℒp = (0 : Lp.simple_func E p μ) := rfl lemma to_Lp_add (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : to_Lp (f + g) (hf.add hg) = to_Lp f hf + to_Lp g hg := rfl lemma to_Lp_neg (f : α →ₛ E) (hf : mem_ℒp f p μ) : to_Lp (-f) hf.neg = -to_Lp f hf := rfl lemma to_Lp_sub (f g : α →ₛ E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : to_Lp (f - g) (hf.sub hg) = to_Lp f hf - to_Lp g hg := by { simp only [sub_eq_add_neg, ← to_Lp_neg, ← to_Lp_add], refl } variables [normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_smul (f : α →ₛ E) (hf : mem_ℒp f p μ) (c : 𝕜) : to_Lp (c • f) (hf.const_smul c) = c • to_Lp f hf := rfl lemma norm_to_Lp [fact (1 ≤ p)] (f : α →ₛ E) (hf : mem_ℒp f p μ) : ∥to_Lp f hf∥ = ennreal.to_real (snorm f p μ) := norm_to_Lp f hf end to_Lp section to_simple_func /-- Find a representative of a `Lp.simple_func`. -/ def to_simple_func (f : Lp.simple_func E p μ) : α →ₛ E := classical.some f.2 /-- `(to_simple_func f)` is measurable. -/ @[measurability] protected lemma measurable [measurable_space E] (f : Lp.simple_func E p μ) : measurable (to_simple_func f) := (to_simple_func f).measurable protected lemma strongly_measurable (f : Lp.simple_func E p μ) : strongly_measurable (to_simple_func f) := (to_simple_func f).strongly_measurable @[measurability] protected lemma ae_measurable [measurable_space E] (f : Lp.simple_func E p μ) : ae_measurable (to_simple_func f) μ := (simple_func.measurable f).ae_measurable protected lemma ae_strongly_measurable (f : Lp.simple_func E p μ) : ae_strongly_measurable (to_simple_func f) μ := (simple_func.strongly_measurable f).ae_strongly_measurable lemma to_simple_func_eq_to_fun (f : Lp.simple_func E p μ) : to_simple_func f =ᵐ[μ] f := show ⇑(to_simple_func f) =ᵐ[μ] ⇑(f : α →ₘ[μ] E), begin convert (ae_eq_fun.coe_fn_mk (to_simple_func f) (to_simple_func f).ae_strongly_measurable).symm using 2, exact (classical.some_spec f.2).symm, end /-- `to_simple_func f` satisfies the predicate `mem_ℒp`. -/ protected lemma mem_ℒp (f : Lp.simple_func E p μ) : mem_ℒp (to_simple_func f) p μ := mem_ℒp.ae_eq (to_simple_func_eq_to_fun f).symm $ mem_Lp_iff_mem_ℒp.mp (f : Lp E p μ).2 lemma to_Lp_to_simple_func (f : Lp.simple_func E p μ) : to_Lp (to_simple_func f) (simple_func.mem_ℒp f) = f := simple_func.eq' (classical.some_spec f.2) lemma to_simple_func_to_Lp (f : α →ₛ E) (hfi : mem_ℒp f p μ) : to_simple_func (to_Lp f hfi) =ᵐ[μ] f := by { rw ← ae_eq_fun.mk_eq_mk, exact classical.some_spec (to_Lp f hfi).2 } variables (E μ) lemma zero_to_simple_func : to_simple_func (0 : Lp.simple_func E p μ) =ᵐ[μ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : Lp.simple_func E p μ), Lp.coe_fn_zero E 1 μ] with _ h₁ _, rwa h₁, end variables {E μ} lemma add_to_simple_func (f g : Lp.simple_func E p μ) : to_simple_func (f + g) =ᵐ[μ] to_simple_func f + to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Lp E p μ) g] with _, simp only [← coe_coe, add_subgroup.coe_add, pi.add_apply], iterate 4 { assume h, rw h, }, end lemma neg_to_simple_func (f : Lp.simple_func E p μ) : to_simple_func (-f) =ᵐ[μ] - to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, Lp.coe_fn_neg (f : Lp E p μ)] with _, simp only [pi.neg_apply, add_subgroup.coe_neg, ← coe_coe], repeat { assume h, rw h, }, end lemma sub_to_simple_func (f g : Lp.simple_func E p μ) : to_simple_func (f - g) =ᵐ[μ] to_simple_func f - to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Lp E p μ) g] with _, simp only [add_subgroup.coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h, }, end variables [normed_field 𝕜] [normed_space 𝕜 E] lemma smul_to_simple_func (k : 𝕜) (f : Lp.simple_func E p μ) : to_simple_func (k • f) =ᵐ[μ] k • to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, Lp.coe_fn_smul k (f : Lp E p μ)] with _, simp only [pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h, }, end lemma norm_to_simple_func [fact (1 ≤ p)] (f : Lp.simple_func E p μ) : ∥f∥ = ennreal.to_real (snorm (to_simple_func f) p μ) := by simpa [to_Lp_to_simple_func] using norm_to_Lp (to_simple_func f) (simple_func.mem_ℒp f) end to_simple_func section induction variables (p) /-- The characteristic function of a finite-measure measurable set `s`, as an `Lp` simple function. -/ def indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : Lp.simple_func E p μ := to_Lp ((simple_func.const _ c).piecewise s hs (simple_func.const _ 0)) (mem_ℒp_indicator_const p hs c (or.inr hμs)) variables {p} @[simp] lemma coe_indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : (↑(indicator_const p hs hμs c) : Lp E p μ) = indicator_const_Lp p hs hμs c := rfl lemma to_simple_func_indicator_const {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : to_simple_func (indicator_const p hs hμs c) =ᵐ[μ] (simple_func.const _ c).piecewise s hs (simple_func.const _ 0) := Lp.simple_func.to_simple_func_to_Lp _ _ /-- To prove something for an arbitrary `Lp` simple function, with `0 < p < ∞`, it suffices to show that the property holds for (multiples of) characteristic functions of finite-measure measurable sets and is closed under addition (of functions with disjoint support). -/ @[elab_as_eliminator] protected lemma induction (hp_pos : p ≠ 0) (hp_ne_top : p ≠ ∞) {P : Lp.simple_func E p μ → Prop} (h_ind : ∀ (c : E) {s : set α} (hs : measurable_set s) (hμs : μ s < ∞), P (Lp.simple_func.indicator_const p hs hμs.ne c)) (h_add : ∀ ⦃f g : α →ₛ E⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, disjoint (support f) (support g) → P (Lp.simple_func.to_Lp f hf) → P (Lp.simple_func.to_Lp g hg) → P (Lp.simple_func.to_Lp f hf + Lp.simple_func.to_Lp g hg)) (f : Lp.simple_func E p μ) : P f := begin suffices : ∀ f : α →ₛ E, ∀ hf : mem_ℒp f p μ, P (to_Lp f hf), { rw ← to_Lp_to_simple_func f, apply this }, clear f, refine simple_func.induction _ _, { intros c s hs hf, by_cases hc : c = 0, { convert h_ind 0 measurable_set.empty (by simp) using 1, ext1, simp [hc] }, exact h_ind c hs (simple_func.measure_lt_top_of_mem_ℒp_indicator hp_pos hp_ne_top hc hs hf) }, { intros f g hfg hf hg hfg', obtain ⟨hf', hg'⟩ : mem_ℒp f p μ ∧ mem_ℒp g p μ, { exact (mem_ℒp_add_of_disjoint hfg f.strongly_measurable g.strongly_measurable).mp hfg' }, exact h_add hf' hg' hfg (hf hf') (hg hg') }, end end induction section coe_to_Lp variables [fact (1 ≤ p)] protected lemma uniform_continuous : uniform_continuous (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding (hp_ne_top : p ≠ ∞) : dense_embedding (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := begin borelize E, apply simple_func.uniform_embedding.dense_embedding, assume f, rw mem_closure_iff_seq_limit, have hfi' : mem_ℒp f p μ := Lp.mem_ℒp f, haveI : separable_space (range f ∪ {0} : set E) := (Lp.strongly_measurable f).separable_space_range_union_singleton, refine ⟨λ n, ↑(to_Lp (simple_func.approx_on f (Lp.strongly_measurable f).measurable (range f ∪ {0}) 0 (by simp) n) (simple_func.mem_ℒp_approx_on_range (Lp.strongly_measurable f).measurable hfi' n)), λ n, mem_range_self _, _⟩, convert simple_func.tendsto_approx_on_range_Lp hp_ne_top (Lp.strongly_measurable f).measurable hfi', rw to_Lp_coe_fn f (Lp.mem_ℒp f) end protected lemma dense_inducing (hp_ne_top : p ≠ ∞) : dense_inducing (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := (simple_func.dense_embedding hp_ne_top).to_dense_inducing protected lemma dense_range (hp_ne_top : p ≠ ∞) : dense_range (coe : (Lp.simple_func E p μ) → (Lp E p μ)) := (simple_func.dense_inducing hp_ne_top).dense variables [normed_field 𝕜] [normed_space 𝕜 E] variables (α E 𝕜) /-- The embedding of Lp simple functions into Lp functions, as a continuous linear map. -/ def coe_to_Lp : (Lp.simple_func E p μ) →L[𝕜] (Lp E p μ) := { map_smul' := λk f, rfl, cont := Lp.simple_func.uniform_continuous.continuous, .. add_subgroup.subtype (Lp.simple_func E p μ) } variables {α E 𝕜} end coe_to_Lp section order variables {G : Type*} [normed_lattice_add_comm_group G] lemma coe_fn_le (f g : Lp.simple_func G p μ) : f ≤ᵐ[μ] g ↔ f ≤ g := by rw [← subtype.coe_le_coe, ← Lp.coe_fn_le, coe_fn_coe_base', coe_fn_coe_base' g] instance : covariant_class (Lp.simple_func G p μ) (Lp.simple_func G p μ) (+) (≤) := begin refine ⟨λ f g₁ g₂ hg₁₂, _⟩, rw ← Lp.simple_func.coe_fn_le at hg₁₂ ⊢, have h_add_1 : ⇑(f + g₁) =ᵐ[μ] f + g₁, from Lp.coe_fn_add _ _, have h_add_2 : ⇑(f + g₂) =ᵐ[μ] f + g₂, from Lp.coe_fn_add _ _, filter_upwards [h_add_1, h_add_2, hg₁₂] with _ h1 h2 h3, rw [h1, h2, pi.add_apply, pi.add_apply], exact add_le_add le_rfl h3, end variables (p μ G) lemma coe_fn_zero : (0 : Lp.simple_func G p μ) =ᵐ[μ] (0 : α → G) := Lp.coe_fn_zero _ _ _ variables{p μ G} lemma coe_fn_nonneg (f : Lp.simple_func G p μ) : 0 ≤ᵐ[μ] f ↔ 0 ≤ f := begin rw ← Lp.simple_func.coe_fn_le, have h0 : (0 : Lp.simple_func G p μ) =ᵐ[μ] (0 : α → G), from Lp.simple_func.coe_fn_zero p μ G, split; intro h; filter_upwards [h, h0] with _ _ h2, { rwa h2, }, { rwa ← h2, }, end lemma exists_simple_func_nonneg_ae_eq {f : Lp.simple_func G p μ} (hf : 0 ≤ f) : ∃ f' : α →ₛ G, 0 ≤ f' ∧ f =ᵐ[μ] f' := begin rw ← Lp.simple_func.coe_fn_nonneg at hf, have hf_ae : 0 ≤ᵐ[μ] (simple_func.to_simple_func f), by { filter_upwards [to_simple_func_eq_to_fun f, hf] with _ h1 _, rwa h1 }, let s := (to_measurable μ {x | ¬ 0 ≤ simple_func.to_simple_func f x})ᶜ, have hs_zero : μ sᶜ = 0, by { rw [compl_compl, measure_to_measurable], rwa [eventually_le, ae_iff] at hf_ae, }, have hfs_nonneg : ∀ x ∈ s, 0 ≤ simple_func.to_simple_func f x, { intros x hxs, rw mem_compl_iff at hxs, have hx' : x ∉ {a : α | ¬0 ≤ simple_func.to_simple_func f a}, from λ h, hxs (subset_to_measurable μ _ h), rwa [set.nmem_set_of_eq, not_not] at hx', }, let f' := simple_func.piecewise s (measurable_set_to_measurable μ _).compl (simple_func.to_simple_func f) (simple_func.const α (0 : G)), refine ⟨f', λ x, _, _⟩, { rw simple_func.piecewise_apply, by_cases hxs : x ∈ s, { simp only [hxs, hfs_nonneg x hxs, if_true, pi.zero_apply, simple_func.coe_zero], }, { simp only [hxs, simple_func.const_zero, if_false], }, }, { rw simple_func.coe_piecewise, have : s =ᵐ[μ] univ, { rw ae_eq_set, simp only [true_and, measure_empty, eq_self_iff_true, diff_univ, ← compl_eq_univ_diff], exact hs_zero, }, refine eventually_eq.trans (to_simple_func_eq_to_fun f).symm _, refine eventually_eq.trans _ (piecewise_ae_eq_of_ae_eq_set this.symm), simp only [simple_func.const_zero, indicator_univ, piecewise_eq_indicator, simple_func.coe_zero], }, end variables (p μ G) /-- Coercion from nonnegative simple functions of Lp to nonnegative functions of Lp. -/ def coe_simple_func_nonneg_to_Lp_nonneg : {g : Lp.simple_func G p μ // 0 ≤ g} → {g : Lp G p μ // 0 ≤ g} := λ g, ⟨g, g.2⟩ lemma dense_range_coe_simple_func_nonneg_to_Lp_nonneg [hp : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) : dense_range (coe_simple_func_nonneg_to_Lp_nonneg p μ G) := begin borelize G, assume g, rw mem_closure_iff_seq_limit, have hg_mem_ℒp : mem_ℒp g p μ := Lp.mem_ℒp g, have zero_mem : (0 : G) ∈ (range g ∪ {0} : set G) ∩ {y | 0 ≤ y}, by simp only [union_singleton, mem_inter_eq, mem_insert_iff, eq_self_iff_true, true_or, mem_set_of_eq, le_refl, and_self], haveI : separable_space (((range g ∪ {0}) ∩ {y | 0 ≤ y}) : set G), { apply is_separable.separable_space, apply is_separable.mono _ (set.inter_subset_left _ _), exact (Lp.strongly_measurable (g : Lp G p μ)).is_separable_range.union (finite_singleton _).is_separable }, have g_meas : measurable g := (Lp.strongly_measurable (g : Lp G p μ)).measurable, let x := λ n, simple_func.approx_on g g_meas ((range g ∪ {0}) ∩ {y | 0 ≤ y}) 0 zero_mem n, have hx_nonneg : ∀ n, 0 ≤ x n, { assume n a, change x n a ∈ {y : G | 0 ≤ y}, have A : (range g ∪ {0} : set G) ∩ {y | 0 ≤ y} ⊆ {y | 0 ≤ y} := inter_subset_right _ _, apply A, exact simple_func.approx_on_mem g_meas _ n a }, have hx_mem_ℒp : ∀ n, mem_ℒp (x n) p μ, from simple_func.mem_ℒp_approx_on _ hg_mem_ℒp _ ⟨ae_strongly_measurable_const, by simp⟩, have h_to_Lp := λ n, mem_ℒp.coe_fn_to_Lp (hx_mem_ℒp n), have hx_nonneg_Lp : ∀ n, 0 ≤ to_Lp (x n) (hx_mem_ℒp n), { intro n, rw [← Lp.simple_func.coe_fn_le, coe_fn_coe_base' (simple_func.to_Lp (x n) _), Lp.simple_func.to_Lp_eq_to_Lp], have h0 := Lp.simple_func.coe_fn_zero p μ G, filter_upwards [Lp.simple_func.coe_fn_zero p μ G, h_to_Lp n] with a ha0 ha_to_Lp, rw [ha0, ha_to_Lp], exact hx_nonneg n a, }, have hx_tendsto : tendsto (λ (n : ℕ), snorm (x n - g) p μ) at_top (𝓝 0), { apply simple_func.tendsto_approx_on_Lp_snorm g_meas zero_mem hp_ne_top, { have hg_nonneg : 0 ≤ᵐ[μ] g, from (Lp.coe_fn_nonneg _).mpr g.2, refine hg_nonneg.mono (λ a ha, subset_closure _), simpa using ha, }, { simp_rw sub_zero, exact hg_mem_ℒp.snorm_lt_top, }, }, refine ⟨λ n, (coe_simple_func_nonneg_to_Lp_nonneg p μ G) ⟨to_Lp (x n) (hx_mem_ℒp n), hx_nonneg_Lp n⟩, λ n, mem_range_self _, _⟩, suffices : tendsto (λ (n : ℕ), ↑(to_Lp (x n) (hx_mem_ℒp n))) at_top (𝓝 (g : Lp G p μ)), { rw tendsto_iff_dist_tendsto_zero at this ⊢, simp_rw subtype.dist_eq, convert this, }, rw Lp.tendsto_Lp_iff_tendsto_ℒp', convert hx_tendsto, refine funext (λ n, snorm_congr_ae (eventually_eq.sub _ _)), { rw Lp.simple_func.to_Lp_eq_to_Lp, exact h_to_Lp n, }, { rw ← coe_fn_coe_base, }, end variables {p μ G} end order end simple_func end Lp variables [measurable_space α] [normed_group E] {f : α → E} {p : ℝ≥0∞} {μ : measure α} /-- To prove something for an arbitrary `Lp` function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in `Lp` for which the property holds is closed. -/ @[elab_as_eliminator] lemma Lp.induction [_i : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) (P : Lp E p μ → Prop) (h_ind : ∀ (c : E) {s : set α} (hs : measurable_set s) (hμs : μ s < ∞), P (Lp.simple_func.indicator_const p hs hμs.ne c)) (h_add : ∀ ⦃f g⦄, ∀ hf : mem_ℒp f p μ, ∀ hg : mem_ℒp g p μ, disjoint (support f) (support g) → P (hf.to_Lp f) → P (hg.to_Lp g) → P ((hf.to_Lp f) + (hg.to_Lp g))) (h_closed : is_closed {f : Lp E p μ | P f}) : ∀ f : Lp E p μ, P f := begin refine λ f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed _, refine Lp.simple_func.induction (lt_of_lt_of_le ennreal.zero_lt_one _i.elim).ne' hp_ne_top _ _, { exact λ c s, h_ind c }, { exact λ f g hf hg, h_add hf hg }, end /-- To prove something for an arbitrary `mem_ℒp` function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `Lᵖ` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma mem_ℒp.induction [_i : fact (1 ≤ p)] (hp_ne_top : p ≠ ∞) (P : (α → E) → Prop) (h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c))) (h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → mem_ℒp f p μ → mem_ℒp g p μ → P f → P g → P (f + g)) (h_closed : is_closed {f : Lp E p μ | P f} ) (h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → mem_ℒp f p μ → P f → P g) : ∀ ⦃f : α → E⦄ (hf : mem_ℒp f p μ), P f := begin have : ∀ (f : simple_func α E), mem_ℒp f p μ → P f, { refine simple_func.induction _ _, { intros c s hs h, by_cases hc : c = 0, { subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, ext, simp [const] }, have hp_pos : p ≠ 0 := (lt_of_lt_of_le ennreal.zero_lt_one _i.elim).ne', exact h_ind c hs (simple_func.measure_lt_top_of_mem_ℒp_indicator hp_pos hp_ne_top hc hs h) }, { intros f g hfg hf hg int_fg, rw [simple_func.coe_add, mem_ℒp_add_of_disjoint hfg f.strongly_measurable g.strongly_measurable] at int_fg, refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } }, have : ∀ (f : Lp.simple_func E p μ), P f, { intro f, exact h_ae (Lp.simple_func.to_simple_func_eq_to_fun f) (Lp.simple_func.mem_ℒp f) (this (Lp.simple_func.to_simple_func f) (Lp.simple_func.mem_ℒp f)) }, have : ∀ (f : Lp E p μ), P f := λ f, (Lp.simple_func.dense_range hp_ne_top).induction_on f h_closed this, exact λ f hf, h_ae hf.coe_fn_to_Lp (Lp.mem_ℒp _) (this (hf.to_Lp f)), end section integrable notation α ` →₁ₛ[`:25 μ `] ` E := @measure_theory.Lp.simple_func α E _ _ 1 μ lemma L1.simple_func.to_Lp_one_eq_to_L1 (f : α →ₛ E) (hf : integrable f μ) : (Lp.simple_func.to_Lp f (mem_ℒp_one_iff_integrable.2 hf) : α →₁[μ] E) = hf.to_L1 f := rfl protected lemma L1.simple_func.integrable (f : α →₁ₛ[μ] E) : integrable (Lp.simple_func.to_simple_func f) μ := by { rw ← mem_ℒp_one_iff_integrable, exact (Lp.simple_func.mem_ℒp f) } /-- To prove something for an arbitrary integrable function in a normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `L¹` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma integrable.induction (P : (α → E) → Prop) (h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c))) (h_add : ∀ ⦃f g : α → E⦄, disjoint (support f) (support g) → integrable f μ → integrable g μ → P f → P g → P (f + g)) (h_closed : is_closed {f : α →₁[μ] E | P f} ) (h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → integrable f μ → P f → P g) : ∀ ⦃f : α → E⦄ (hf : integrable f μ), P f := begin simp only [← mem_ℒp_one_iff_integrable] at *, exact mem_ℒp.induction one_ne_top P h_ind h_add h_closed h_ae end end integrable end measure_theory
49b7dcf4392b6a4947fc1b0e6a1979b9f5622d85
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/notation3.lean
bece0aae64ae2bd2d65d9a7f92609a711db41451
[ "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
322
lean
import data.prod data.num 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 open prod num constants a b : num check [a, b, b] check (a, true, a = b, b) check (a, b) check [(1:num), 2+2, 3]
633b736ecd33b0583e33c87115af9f9b63b1555c
d7189ea2ef694124821b033e533f18905b5e87ef
/galois/list/member.lean
72be6d5d30c9757b6b272ebd7bf8d9b602159989
[ "Apache-2.0" ]
permissive
digama0/lean-protocol-support
eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59
cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda
refs/heads/master
1,625,421,450,627
1,506,035,462,000
1,506,035,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,700
lean
import .preds universes u namespace list inductive member {A : Type u} : list A → Type u | here : ∀ {x : A} {xs : list A}, member (x :: xs) | there : ∀ {x : A} {xs : list A}, member xs → member (x :: xs) namespace member section parameter {α : Type u} def value : ∀ {xs : list α}, member xs -> α | (x :: xs) member.here := x | (x :: xs) (member.there m) := value m -- Given a member of the append of two lists, this returns a member -- of one or the other depending on which list is referenced. def case_append : Π {l1 l2 : list α}, member (l1 ++ l2) → member l1 ⊕ member l2 | (h::r) _ member.here := sum.inl member.here | (h::r) l2 (member.there m) := match @case_append r l2 m with | sum.inr z := sum.inr z | sum.inl z := sum.inl (member.there z) end | [] l2 m := let q := congr_arg member (list.nil_append l2) in sum.inr (cast q m) end end member definition remove_member {A : Type u} : ∀ (xs : list A), member xs → list A | (x :: xs) member.here := xs | (x :: xs) (member.there m) := x :: remove_member xs m definition update_member {A : Type u} (y : A) : ∀ (xs : list A), member xs → list A | (x :: xs) member.here := y :: xs | (x :: xs) (member.there m) := x :: update_member xs m inductive member_st {A : Type u} (P : A → Prop) : list A → Type u | here {} : ∀ {x : A} {xs : list A}, P x → member_st (x :: xs) | there {} : ∀ {x : A} {xs : list A}, member_st xs → member_st (x :: xs) inductive el_member {A : Type u} : A → list A → Type u | here : ∀ {x : A} {xs : list A}, el_member x (x :: xs) | there : ∀ {y x : A} {xs : list A}, el_member y xs → el_member y (x :: xs) namespace member def to_el_member {A : Type u} : ∀ {xs : list A} (m : member xs), el_member m.value xs | (x :: xs) here := el_member.here | (x :: xs) (there m) := el_member.there (to_el_member m) def to_member_st {A : Type u} : ∀ {xs : list A} (m : member xs) (P : A → Prop) (Pm : P m.value), member_st P xs | (x :: xs) here _ Px := member_st.here Px | (x :: xs) (there m) _ Px := member_st.there (to_member_st m _ Px) end member namespace member_st def to_member {A : Type u} {P : A → Prop} : ∀ {xs : list A}, member_st P xs → member xs | ._ (@here ._ ._ x xs H) := member.here | ._ (@there ._ ._ _ _ m) := member.there (to_member m) end member_st namespace el_member def to_member {A : Type u} : ∀ {x : A} {xs : list A}, el_member x xs → member xs | ._ .(x :: xs) (@here ._ x xs) := member.here | ._ ._ (@there ._ _ _ _ m) := member.there (to_member m) end el_member inductive void : Type def member_st_decide' {A : Type u} (P : A → Prop) [decidable_pred P] (xs : list A) : member_st P xs ⊕ (member_st P xs → void) := begin induction xs, { right, intros H, cases H }, { induction ih_1, { left, apply member_st.there, assumption }, { apply (@decidable.by_cases (P a)); intros, { left, apply member_st.here, assumption }, { right, intros contra, cases contra, contradiction, apply a_2, assumption } } } end def member_st_decide {A : Type u} (P : A → Prop) [decidable_pred P] : ∀ (xs : list A) , member_st P xs ⊕ (member_st P xs → void) | [] := begin right, intros x, cases x end | (x :: xs) := if H : P x then sum.inl (@member_st.here _ _ x xs H) else match member_st_decide xs with | (sum.inr f) := sum.inr begin intros contra, cases contra, contradiction, apply f, assumption end | (sum.inl m) := sum.inl (member_st.there m) end def check_member_st {A : Type u} (P : A → Prop) [decidable_pred P] (xs : list A) : option (member_st P xs) := match member_st_decide P xs with | sum.inl r := some r | sum.inr contra := none end lemma member_st_decide_present {A : Type u} {P : A → Prop} [decP : decidable_pred P] {xs : list A} (m : member_st P xs) : psigma (λ m' : member_st P xs, member_st_decide P xs = sum.inl m') := begin induction m; clear xs; rename xs_1 xs, { constructor, unfold member_st_decide, rw (dif_pos a), }, { have H := decP x, induction H, { induction ih_1 with mxs Pmxs, constructor, unfold member_st_decide, rw (dif_neg a_1), rw Pmxs, dsimp [member_st_decide], reflexivity, }, { constructor, unfold member_st_decide, rw (dif_pos a_1), } } end def el_member_decide {A : Type u} [decidable_eq A] (z : A) : ∀ (xs : list A) , el_member z xs ⊕ (el_member z xs → void) | [] := begin right, intros x, cases x end | (x :: xs) := if H : z = x then begin induction H, exact sum.inl el_member.here end else match el_member_decide xs with | (sum.inr f) := sum.inr begin intros contra, clear _match, cases contra, contradiction, apply f, assumption end | (sum.inl m) := sum.inl (el_member.there m) end lemma el_member_decide_present {A : Type u} [deceqA : decidable_eq A] {z : A} {xs : list A} (m : el_member z xs) : psigma (λ m', el_member_decide z xs = sum.inl m') := begin induction m; clear z xs; rename xs_1 xs, { constructor, unfold el_member_decide, rw (dif_pos (eq.refl x)), }, { have H := deceqA y x, induction H, { induction ih_1 with mxs Pmxs, constructor, unfold el_member_decide, rw (dif_neg a_1), rw Pmxs, dsimp [el_member_decide], reflexivity, }, { induction a_1, constructor, unfold el_member_decide, rw (dif_pos (eq.refl _)), } } end def check_member {A : Type u} [decidable_eq A] (x : A) (xs : list A) : option (member xs) := match el_member_decide x xs with | (sum.inl m) := some m.to_member | (sum.inr _) := none end lemma el_member_value {A : Type u} (x : A) (xs : list A) (m : el_member x xs) : m.to_member.value = x := begin induction m; simp [member.value, el_member.to_member], assumption end lemma member_st_P_value {A : Type u} {P : A → Prop} {xs : list A} (m : member_st P xs) : P m.to_member.value := begin induction m; simp [member_st.to_member, member.value]; assumption, end lemma check_member_ok {A : Type u} [decidable_eq A] (x : A) (xs : list A) (m : xs.member) (H : check_member x xs = some m) : m.value = x := begin unfold check_member at H, cases (el_member_decide x xs); dsimp [check_member] at H; try {contradiction}, injection H with H', clear H,subst m, apply el_member_value, end lemma check_member_present {A : Type u} [decidable_eq A] (xs : list A) (m : xs.member) : psigma (λ m', check_member m.value xs = some m') := begin have m' := m.to_el_member, have H := el_member_decide_present m', unfold check_member, induction H with m'' Hm'', rw Hm'', dsimp [check_member], constructor, reflexivity end end list
c1ff4db9a1bcc6003462e098040327ed61159fa1
32da3d0f92cab08875472ef6cacc1931c2b3eafa
/src/analysis/convex/integral.lean
f5db808c75258e15e2e19df1d959ab8866bb21ad
[ "Apache-2.0" ]
permissive
karthiknadig/mathlib
b6073c3748860bfc9a3e55da86afcddba62dc913
33a86cfff12d7f200d0010cd03b95e9b69a6c1a5
refs/heads/master
1,676,389,371,851
1,610,061,127,000
1,610,061,127,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,689
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import analysis.convex.basic import measure_theory.set_integral /-! # Jensen's inequality for integrals In this file we prove four theorems: * `convex.smul_integral_mem`: if `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version of this lemma. * `convex.integral_mem`: if `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. * `convex_on.map_smul_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma. * `convex_on.map_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a finite sum version of this lemma. ## Tags convex, integral, center mass, Jensen's inequality -/ open measure_theory set filter open_locale topological_space big_operators variables {α E : Type*} [measurable_space α] {μ : measure α} [normed_group E] [normed_space ℝ E] [complete_space E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] private lemma convex.smul_integral_mem_of_measurable [finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) (hfm : measurable f) : (μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s := begin rcases eq_empty_or_nonempty s with rfl|⟨y₀, h₀⟩, { refine (hμ _).elim, simpa using hfs }, rw ← hsc.closure_eq at hfs, have hc : integrable (λ _, y₀) μ := integrable_const _, set F : ℕ → simple_func α E := simple_func.approx_on f hfm s y₀ h₀, have : tendsto (λ n, (F n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ), { simp only [simple_func.integral_eq_integral _ (simple_func.integrable_approx_on hfm hfi h₀ hc _)], exact tendsto_integral_of_l1 _ hfi (eventually_of_forall $ simple_func.integrable_approx_on hfm hfi h₀ hc) (simple_func.tendsto_approx_on_l1_edist hfm h₀ hfs (hfi.sub hc).2) }, refine hsc.mem_of_tendsto (tendsto_const_nhds.smul this) (eventually_of_forall $ λ n, _), have : ∑ y in (F n).range, (μ ((F n) ⁻¹' {y})).to_real = (μ univ).to_real, by rw [← (F n).sum_range_measure_preimage_singleton, @ennreal.to_real_sum _ _ (λ y, μ ((F n) ⁻¹' {y})) (λ _ _, (measure_lt_top _ _))], rw [← this, simple_func.integral], refine hs.center_mass_mem (λ _ _, ennreal.to_real_nonneg) _ _, { rw [this, ennreal.to_real_pos_iff, zero_lt_iff_ne_zero, ne.def, measure.measure_univ_eq_zero], exact ⟨hμ, measure_ne_top _ _⟩ }, { simp only [simple_func.mem_range], rintros _ ⟨x, rfl⟩, exact simple_func.approx_on_mem hfm h₀ n x } end /-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version of this lemma. -/ lemma convex.smul_integral_mem [finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) : (μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s := begin have : ∀ᵐ (x : α) ∂μ, hfi.ae_measurable.mk f x ∈ s, { filter_upwards [hfs, hfi.ae_measurable.ae_eq_mk], assume a ha h, rwa ← h }, convert convex.smul_integral_mem_of_measurable hs hsc hμ this (hfi.congr hfi.ae_measurable.ae_eq_mk) (hfi.ae_measurable.measurable_mk) using 2, apply integral_congr_ae, exact hfi.ae_measurable.ae_eq_mk end /-- If `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. -/ lemma convex.integral_mem [probability_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s) {f : α → E} (hf : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) : ∫ x, f x ∂μ ∈ s := by simpa [measure_univ] using hs.smul_integral_mem hsc (probability_measure.ne_zero μ) hf hfi /-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma. -/ lemma convex_on.map_smul_integral_le [finite_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g) (hgc : continuous_on g s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) : g ((μ univ).to_real⁻¹ • ∫ x, f x ∂μ) ≤ (μ univ).to_real⁻¹ • ∫ x, g (f x) ∂μ := begin set t := {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2}, have ht_conv : convex t := hg.convex_epigraph, have ht_closed : is_closed t := (hsc.preimage continuous_fst).is_closed_le (hgc.comp continuous_on_fst (subset.refl _)) continuous_on_snd, have ht_mem : ∀ᵐ x ∂μ, (f x, g (f x)) ∈ t := hfs.mono (λ x hx, ⟨hx, le_rfl⟩), simpa [integral_pair hfi hgi] using (ht_conv.smul_integral_mem ht_closed hμ ht_mem (hfi.prod_mk hgi)).2 end /-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a finite sum version of this lemma. -/ lemma convex_on.map_integral_le [probability_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g) (hgc : continuous_on g s) (hsc : is_closed s) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) : g (∫ x, f x ∂μ) ≤ ∫ x, g (f x) ∂μ := by simpa [measure_univ] using hg.map_smul_integral_le hgc hsc (probability_measure.ne_zero μ) hfs hfi hgi
b188b9a4f02f70ab336be2742e75c4822c8e7931
a8c03ed21a1bd6fc45901943b79dd6574ea3f0c2
/equality.lean
2e49db9d9dad928d0817555c9aca0934dc9197e5
[]
no_license
gebner/resolution.lean
716c355fbb5204e5c4d0c5a7f3f3cc825892a2bf
c6fafe06fba1cfad73db68f2aa474b29fe892a2b
refs/heads/master
1,601,111,444,528
1,475,256,701,000
1,475,256,701,000
67,711,151
0
0
null
null
null
null
UTF-8
Lean
false
false
1,079
lean
import clause prover_state utils open tactic monad expr list meta def try_unify_eq_l (c : clause) (i : nat) : tactic clause := do guard $ clause.literal.is_neg (clause.get_lit c i), qf ← clause.open_metan c c↣num_quants, match is_eq (qf↣1↣get_lit i)↣formula with | none := failed | some (lhs, rhs) := do unify lhs rhs, ty ← infer_type lhs, univ ← infer_univ ty, refl ← return $ app_of_list (const ``eq.refl [univ]) [ty, lhs], opened ← clause.open_constn qf↣1 i, clause.meta_closure qf↣2 $ clause.close_constn (opened↣1↣inst refl) opened↣2 end meta def unify_eq_inf : inference := take given, sequence' $ do i ← given↣selected, [(do u ← ↑(try_unify_eq_l given↣c i), add_inferred u [given]) <|> return ()] meta def has_refl_r (c : clause) : bool := list.bor $ do literal ← c↣get_lits, guard literal↣is_pos, match is_eq literal↣formula with | some (lhs, rhs) := [decidable.to_bool (lhs = rhs)] | none := [] end meta def refl_r_pre : resolution_prover unit := preprocessing_rule $ take new, return (filter (λc, ¬has_refl_r c) new)
b1b9f05f2b57d05e5bf5462bee33b19bd2297698
618003631150032a5676f229d13a079ac875ff77
/src/tactic/local_cache.lean
c12836c979a52f8e6860e246400c7969c63dfd6f
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
7,334
lean
/- Copyright (c) 2019 Keeley Hoek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Keeley Hoek -/ import tactic.norm_num namespace tactic namespace local_cache namespace internal variables {α : Type} [reflected α] [has_reflect α] meta def mk_full_namespace (ns : name) : name := `local_cache ++ ns meta def save_data (dn : name) (a : α) [reflected a] : tactic unit := tactic.add_decl $ mk_definition dn [] (reflect α) (reflect a) meta def load_data (dn : name) : tactic α := do e ← tactic.get_env, d ← e.get dn, tactic.eval_expr α d.value meta def poke_data (dn : name) : tactic bool := do e ← tactic.get_env, return (e.get dn).to_bool meta def run_once_under_name {α : Type} [reflected α] [has_reflect α] (t : tactic α) (cache_name : name) : tactic α := do load_data cache_name <|> do { a ← t, save_data cache_name a, return a } -- We maintain two separate caches with different scopes: -- one local to `begin ... end` or `by` blocks, and another -- for entire `def`/`lemma`s. meta structure cache_scope := -- Returns the name of the def used to store the contents of is cache, -- making a new one and recording this in private state if neccesary. (get_name : name → tactic name) -- Same as above but fails instead of making a new name, and never -- mutates state. (try_get_name : name → tactic name) -- Asks whether the namespace `ns` currently has a value-in-cache (present : name → tactic bool) -- Clear cache associated to namespace `ns` (clear : name → tactic unit) namespace block_local -- `mk_new` gives a way to generate a new name if no current one -- exists. private meta def get_name_aux (ns : name) (mk_new : options → name → tactic name) : tactic name := do o ← tactic.get_options, let opt := mk_full_namespace ns, match o.get_string opt "" with | "" := mk_new o opt | s := return $ name.from_components $ s.split (= '.') end meta def get_name (ns : name) : tactic name := get_name_aux ns $ λ o opt, do n ← mk_user_fresh_name, tactic.set_options $ o.set_string opt n.to_string, return n -- Like `get_name`, but fail if `ns` does not have a cached -- decl name (we create a new one above). meta def try_get_name (ns : name) : tactic name := get_name_aux ns $ λ o opt, fail format!"no cache for \"{ns}\"" meta def present (ns : name) : tactic bool := do o ← tactic.get_options, match o.get_string (mk_full_namespace ns) "" with | "" := return ff | s := return tt end meta def clear (ns : name) : tactic unit := do o ← tactic.get_options, set_options $ o.set_string (mk_full_namespace ns) "" end block_local namespace def_local -- Fowler-Noll-Vo hash function (FNV-1a) section fnv_a1 def FNV_OFFSET_BASIS := 0xcbf29ce484222325 def FNV_PRIME := 0x100000001b3 def RADIX := by apply_normed 2^64 def hash_byte (seed : ℕ) (c : char) : ℕ := let n : ℕ := c.to_nat in ((seed.lxor n) * FNV_PRIME) % RADIX def hash_string (s : string) : ℕ := s.to_list.foldl hash_byte FNV_OFFSET_BASIS end fnv_a1 meta def hash_context : tactic string := do ns ← open_namespaces, dn ← decl_name, let flat := ((list.cons dn ns).map to_string).foldl string.append "", return $ (to_string dn) ++ (to_string (hash_string flat)) meta def get_root_name (ns : name) : tactic name := do hc ← hash_context, return $ mk_full_namespace $ hc ++ ns meta def apply_tag (n : name) (tag : ℕ) : name := n ++ to_string format!"t{tag}" meta def mk_dead_name (n : name) : name := n ++ `dead meta def kill_name (n : name) : tactic unit := save_data (mk_dead_name n) () meta def is_name_dead (n : name) : tactic bool := do { witness : unit ← load_data $ mk_dead_name n, return true } <|> return false -- `get_with_status_tag_aux rn n` fails exactly when `rn ++ to_string n` does -- not exist. private meta def get_with_status_tag_aux (rn : name) : ℕ → tactic (ℕ × bool) | tag := do let n := apply_tag rn tag, present ← poke_data n, if ¬present then fail format!"{rn} never seen in cache!" else do is_dead ← is_name_dead n, if is_dead then get_with_status_tag_aux (tag + 1) <|> return (tag, false) else return (tag, true) -- Find the latest tag for the name `rn` and report whether it is alive. meta def get_tag_with_status (rn : name) : tactic (ℕ × bool) := get_with_status_tag_aux rn 0 meta def get_name (ns : name) : tactic name := do rn ← get_root_name ns, (tag, alive) ← get_tag_with_status rn <|> return (0, true), return $ apply_tag rn $ if alive then tag else tag + 1 meta def try_get_name (ns : name) : tactic name := do rn ← get_root_name ns, (tag, alive) ← get_tag_with_status rn, if alive then return $ apply_tag rn tag else fail format!"no cache for \"{ns}\"" meta def present (ns : name) : tactic bool := do rn ← get_root_name ns, (prod.snd <$> get_tag_with_status rn) <|> return false meta def clear (ns : name) : tactic unit := do { n ← try_get_name ns, kill_name n } <|> skip end def_local end internal open internal /-- This scope propogates the cache within a `begin ... end` or `by` block and its decendants. -/ meta def cache_scope.block_local : cache_scope := ⟨ block_local.get_name, block_local.try_get_name, block_local.present, block_local.clear ⟩ /-- This scope propogates the cache within an entire `def`/`lemma`. -/ meta def cache_scope.def_local : cache_scope := ⟨ def_local.get_name, def_local.try_get_name, def_local.present, def_local.clear ⟩ open cache_scope /-- Asks whether the namespace `ns` currently has a value-in-cache. -/ meta def present (ns : name) (s : cache_scope := block_local) : tactic bool := s.present ns /-- Clear cache associated to namespace `ns`. -/ meta def clear (ns : name) (s : cache_scope := block_local) : tactic unit := s.clear ns /-- Gets the (optionally present) value-in-cache for `ns`. -/ meta def get (ns : name) (α : Type) [reflected α] [has_reflect α] (s : cache_scope := block_local) : tactic (option α) := do dn ← some <$> s.try_get_name ns <|> return none, match dn with | none := return none | some dn := some <$> load_data dn end -- Note: we can't just use `<|>` on `load_data` since it will fail -- when a cached value is not present *as well as* when the type of -- `α` is just wrong. end local_cache open local_cache local_cache.internal /-- Using the namespace `ns` as its key, when called for the first time `run_once ns t` runs `t`, then saves and returns the result. Upon subsequent invocations in the same tactic block, with the scope of the caching being inherited by child tactic blocks) we return the cached result directly. You can configure the cached scope to be entire `def`/`lemma`s changing the optional cache_scope argument to `cache_scope.def_local`. Note: the caches backing each scope are different. If `α` is just `unit`, this means we just run `t` once each tactic block. -/ meta def run_once {α : Type} [reflected α] [has_reflect α] (ns : name) (t : tactic α) (s : cache_scope := cache_scope.block_local) : tactic α := s.get_name ns >>= run_once_under_name t end tactic
d1198d9a3ad175948104fc507644668e88a3e5eb
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/src/uexp/rules/subquery.lean
f48d497a169fe3d0da139955408bc2b664ed5348
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
1,201
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..meta.ucongr import ..meta.TDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int -- TODO: this should be easily solvable lemma inlineCorrelatedSubquery : forall (Γ s: Schema) (a : relation s) ty (c : Column ty s), denoteSQL ((SELECT * FROM1 (table a) WHERE (EXISTS (SELECT * (FROM1 (table a) WHERE (equal (uvariable (left⋅right⋅c)) (uvariable (right⋅c))))))) : SQL Γ s) = denoteSQL ((SELECT * FROM1 (table a)) : SQL Γ s) := begin intros, unfold_all_denotations, funext, simp, sorry end lemma pullUpSubqueryInFrom : forall Γ (s1 s2: Schema) (a : SQL Γ s1) (b: SQL Γ s2) (slct1: Pred (Γ ++ (s1 ++ s2))) (slct0: Pred (Γ ++ s2)), denoteSQL ((SELECT * FROM1 ((product a (SELECT * FROM1 b WHERE slct0)) WHERE (slct1))): SQL Γ _ ) = denoteSQL ((SELECT * FROM1 (product a b) WHERE (and slct1 (castPred (combine left (right⋅right)) slct0))) : SQL Γ _) := begin intros, unfold_all_denotations, funext, simp, ucongr, end
b05ed48784119b63bc92ef139aa7e21bba3834ed
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/category.lean
fa8f1d25fd71581ed682cc7a8f41358b55f5c73c
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
5,569
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton Defines a category, as a typeclass parametrised by the type of objects. Introduces notations `X ⟶ Y` for the morphism spaces, `f ≫ g` for composition in the 'arrows' convention. Users may like to add `f ⊚ g` for composition in the standard convention, using ``` local notation f ` ⊚ `:80 g:80 := category.comp g f -- type as \oo ``` -/ import tactic.basic import tactic.tidy universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory /- The propositional fields of `category` are annotated with the auto_param `obviously`, which is defined here as a [`replacer` tactic](https://github.com/leanprover/mathlib/blob/master/docs/tactics.md#def_replacer). We then immediately set up `obviously` to call `tidy`. Later, this can be replaced with more powerful tactics. -/ def_replacer obviously @[obviously] meta def obviously' := tactic.tidy class has_hom (obj : Type u) : Type (max u v) := (hom : obj → obj → Sort v) infixr ` ⟶ `:10 := has_hom.hom -- type as \h class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u v) := (id : Π X : obj, hom X X) (comp : Π {X Y Z : obj}, (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z)) notation `𝟙` := category_struct.id -- type as \b1 infixr ` ≫ `:80 := category_struct.comp -- type as \gg /-- The typeclass `category C` describes morphisms associated to objects of type `C`. The universe levels of the objects and morphisms are unconstrained, and will often need to be specified explicitly, as `category.{v} C`. (See also `large_category` and `small_category`.) -/ class category (obj : Type u) extends category_struct.{v} obj : Type (max u v) := (id_comp' : ∀ {X Y : obj} (f : hom X Y), 𝟙 X ≫ f = f . obviously) (comp_id' : ∀ {X Y : obj} (f : hom X Y), f ≫ 𝟙 Y = f . obviously) (assoc' : ∀ {W X Y Z : obj} (f : hom W X) (g : hom X Y) (h : hom Y Z), (f ≫ g) ≫ h = f ≫ (g ≫ h) . obviously) -- `restate_axiom` is a command that creates a lemma from a structure field, -- discarding any auto_param wrappers from the type. -- (It removes a backtick from the name, if it finds one, and otherwise adds "_lemma".) restate_axiom category.id_comp' restate_axiom category.comp_id' restate_axiom category.assoc' attribute [simp] category.id_comp category.comp_id category.assoc attribute [trans] category_struct.comp lemma category.assoc_symm {C : Type u} [category.{v} C] {W X Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) : f ≫ (g ≫ h) = (f ≫ g) ≫ h := by rw ←category.assoc /-- A `large_category` has objects in one universe level higher than the universe level of the morphisms. It is useful for examples such as the category of types, or the category of groups, etc. -/ abbreviation large_category (C : Type u) : Type u := category.{u} C /-- A `small_category` has objects and morphisms in the same universe level. -/ abbreviation small_category (C : Type u) : Type (u+1) := category.{u+1} C section variables {C : Type u} [𝒞 : category.{v} C] {X Y Z : C} include 𝒞 lemma eq_of_comp_left_eq {f g : X ⟶ Y} (w : ∀ {Z : C} (h : Y ⟶ Z), f ≫ h = g ≫ h) : f = g := by { convert w (𝟙 Y), tidy } lemma eq_of_comp_right_eq {f g : Y ⟶ Z} (w : ∀ {X : C} (h : X ⟶ Y), h ≫ f = h ≫ g) : f = g := by { convert w (𝟙 Y), tidy } lemma eq_of_comp_left_eq' (f g : X ⟶ Y) (w : (λ {Z : C} (h : Y ⟶ Z), f ≫ h) = (λ {Z : C} (h : Y ⟶ Z), g ≫ h)) : f = g := eq_of_comp_left_eq (λ Z h, by convert congr_fun (congr_fun w Z) h) lemma eq_of_comp_right_eq' (f g : Y ⟶ Z) (w : (λ {X : C} (h : X ⟶ Y), h ≫ f) = (λ {X : C} (h : X ⟶ Y), h ≫ g)) : f = g := eq_of_comp_right_eq (λ X h, by convert congr_fun (congr_fun w X) h) lemma id_of_comp_left_id (f : X ⟶ X) (w : ∀ {Y : C} (g : X ⟶ Y), f ≫ g = g) : f = 𝟙 X := by { convert w (𝟙 X), tidy } lemma id_of_comp_right_id (f : X ⟶ X) (w : ∀ {Y : C} (g : Y ⟶ X), g ≫ f = g) : f = 𝟙 X := by { convert w (𝟙 X), tidy } class epi (f : X ⟶ Y) : Prop := (left_cancellation : Π {Z : C} (g h : Y ⟶ Z) (w : f ≫ g = f ≫ h), g = h) class mono (f : X ⟶ Y) : Prop := (right_cancellation : Π {Z : C} (g h : Z ⟶ X) (w : g ≫ f = h ≫ f), g = h) @[simp] lemma cancel_epi (f : X ⟶ Y) [epi f] {g h : Y ⟶ Z} : (f ≫ g = f ≫ h) ↔ g = h := ⟨ λ p, epi.left_cancellation g h p, begin intro a, subst a end ⟩ @[simp] lemma cancel_mono (f : X ⟶ Y) [mono f] {g h : Z ⟶ X} : (g ≫ f = h ≫ f) ↔ g = h := ⟨ λ p, mono.right_cancellation g h p, begin intro a, subst a end ⟩ end section variable (C : Type u) variable [category.{v} C] universe u' instance ulift_category : category.{v} (ulift.{u'} C) := { hom := λ X Y, (X.down ⟶ Y.down), id := λ X, 𝟙 X.down, comp := λ _ _ _ f g, f ≫ g } -- We verify that this previous instance can lift small categories to large categories. example (D : Type u) [small_category D] : large_category (ulift.{u+1} D) := by apply_instance end end category_theory open category_theory namespace preorder variables (α : Type u) instance small_category [preorder α] : small_category α := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans _ _ _ f.down.down g.down.down ⟩ ⟩ } end preorder
f416bd155e76db3412f4c31fe14f1b12f2f83667
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/395b.lean
e9dd4ee3f05ad606af791c2d1a5710651fcf109c
[ "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
307
lean
def foo (n : ℕ) : Prop := true lemma bar (n : ℕ) : foo n := trivial lemma baz (h : true) : ∀ {n : ℕ} (h' : foo n), true := λ a b, trivial meta def mk_true : tactic unit := do e ← tactic.to_expr ``(baz trivial), tactic.to_expr ``(%%e (bar 2)) >>= tactic.exact example : true := by mk_true
cb26036407a77b7fcf9739b070d6659c0c9d367d
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/algebra/category/CommRing/colimits.lean
704e4dfa0bfe07d946c2762f701085c1b738ac2a
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
11,932
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 algebra.category.CommRing.basic import category_theory.limits.limits /-! # The category of commutative rings has all colimits. This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`. It is a very uniform approach, that conceivably could be synthesised directly by a tactic that analyses the shape of `comm_ring` and `ring_hom`. -/ universes u v open category_theory open category_theory.limits -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. -- Note that this means this file does not meet documentation standards. /- `#print comm_ring` says: structure comm_ring : Type u → Type u fields: comm_ring.zero : Π (α : Type u) [c : comm_ring α], α comm_ring.one : Π (α : Type u) [c : comm_ring α], α comm_ring.neg : Π {α : Type u} [c : comm_ring α], α → α comm_ring.add : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.mul : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.zero_add : ∀ {α : Type u} [c : comm_ring α] (a : α), 0 + a = a comm_ring.add_zero : ∀ {α : Type u} [c : comm_ring α] (a : α), a + 0 = a comm_ring.one_mul : ∀ {α : Type u} [c : comm_ring α] (a : α), 1 * a = a comm_ring.mul_one : ∀ {α : Type u} [c : comm_ring α] (a : α), a * 1 = a comm_ring.add_left_neg : ∀ {α : Type u} [c : comm_ring α] (a : α), -a + a = 0 comm_ring.add_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a + b = b + a comm_ring.mul_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a * b = b * a comm_ring.add_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a + b + c_1 = a + (b + c_1) comm_ring.mul_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) comm_ring.left_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * (b + c_1) = a * b + a * c_1 comm_ring.right_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), (a + b) * c_1 = a * c_1 + b * c_1 -/ namespace CommRing.colimits variables {J : Type v} [small_category J] (F : J ⥤ CommRing.{v}) inductive prequotient -- There's always `of` | of : Π (j : J) (x : (F.obj j).α), prequotient -- Then one generator for each operation | zero {} : prequotient | one {} : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient | mul : prequotient → prequotient → prequotient open prequotient inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : (F.obj j).α), relation (of j' (F.map f x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | one : Π (j), relation (of j 1) one | neg : Π (j) (x : (F.obj j).α), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : (F.obj j).α), relation (of j (x + y)) (add (of j x) (of j y)) | mul : Π (j) (x y : (F.obj j).α), relation (of j (x * y)) (mul (of j x) (of j y)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') | mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y) | mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | one_mul : Π (x), relation (mul one x) x | mul_one : Π (x), relation (mul x one) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | mul_comm : Π (x y), relation (mul x y) (mul y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) | mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z)) | left_distrib : Π (x y z), relation (mul x (add y z)) (add (mul x y) (mul x z)) | right_distrib : Π (x y z), relation (mul (add x y) z) (add (mul x z) (mul y z)) def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid def colimit_type : Type v := quotient (colimit_setoid F) instance : comm_ring (colimit_type F) := { zero := begin exact quot.mk _ zero end, one := begin exact quot.mk _ one end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, mul := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (mul x y) }, { intros y y' r, apply quot.sound, exact relation.mul_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.mul_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, one_mul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_mul, refl, end, mul_one := λ x, begin induction x, dsimp, apply quot.sound, apply relation.mul_one, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, mul_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.mul_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, mul_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.mul_assoc, refl, refl, refl, end, left_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.left_distrib, refl, refl, refl, end, right_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.right_distrib, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl @[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl def colimit : CommRing := CommRing.of (colimit_type F) def cocone_fun (j : J) (x : (F.obj j).α) : colimit_type F := quot.mk _ (of j x) def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, map_one' := by apply quot.sound; apply relation.one, map_mul' := by intros; apply quot.sound; apply relation.mul, map_zero' := by apply quot.sound; apply relation.zero, map_add' := by intros; apply quot.sound; apply relation.add } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | one := 1 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y | (mul x y) := desc_fun_lift x * desc_fun_lift y def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { rw cocone.naturality_bundled, }, -- zero { erw is_ring_hom.map_zero ⇑((s.ι).app r), refl }, -- one { erw is_ring_hom.map_one ⇑((s.ι).app r), refl }, -- neg { rw is_ring_hom.map_neg ⇑((s.ι).app r_j) }, -- add { rw is_ring_hom.map_add ⇑((s.ι).app r_j) }, -- mul { rw is_ring_hom.map_mul ⇑((s.ι).app r_j) }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- mul_1 { rw r_ih, }, -- mul_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- one_mul { rw one_mul, }, -- mul_one { rw mul_one, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- mul_comm { rw mul_comm, }, -- add_assoc { rw add_assoc, }, -- mul_assoc { rw mul_assoc, }, -- left_distrib { rw left_distrib, }, -- right_distrib { rw right_distrib, }, } end @[simp] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_one' := rfl, map_zero' := rfl, map_add' := λ x y, by { induction x; induction y; refl }, map_mul' := λ x y, by { induction x; induction y; refl }, } def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp only [desc_morphism, quot_zero], erw is_ring_hom.map_zero ⇑m, refl, }, { simp only [desc_morphism, quot_one], erw is_ring_hom.map_one ⇑m, refl, }, { simp only [desc_morphism, quot_neg], erw is_ring_hom.map_neg ⇑m, rw [x_ih], refl, }, { simp only [desc_morphism, quot_add], erw is_ring_hom.map_add ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, { simp only [desc_morphism, quot_mul], erw is_ring_hom.map_mul ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, refl end }. instance has_colimits_CommRing : has_colimits.{v} CommRing.{v} := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end CommRing.colimits
431357533930cd7338700ba9280da63b76e897be
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/control/functor.lean
e831fe1851b557ffee7c76158fc9b0c66ce72d21
[ "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
8,771
lean
/- Copyright (c) 2017 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.ext import tactic.lint /-! # Functors This module provides additional lemmas, definitions, and instances for `functor`s. ## Main definitions * `const α` is the functor that sends all types to `α`. * `add_const α` is `const α` but for when `α` has an additive structure. * `comp F G` for functors `F` and `G` is the functor composition of `F` and `G`. * `liftp` and `liftr` respectively lift predicates and relations on a type `α` to `F α`. Terms of `F α` are considered to, in some sense, contain values of type `α`. ## Tags functor, applicative -/ attribute [functor_norm] seq_assoc pure_seq_eq_map map_pure seq_map_assoc map_seq universe variables u v w section functor variables {F : Type u → Type v} variables {α β γ : Type u} variables [functor F] [is_lawful_functor F] lemma functor.map_id : (<$>) id = (id : F α → F α) := by apply funext; apply id_map lemma functor.map_comp_map (f : α → β) (g : β → γ) : ((<$>) g ∘ (<$>) f : F α → F γ) = (<$>) (g ∘ f) := by apply funext; intro; rw comp_map theorem functor.ext {F} : ∀ {F1 : functor F} {F2 : functor F} [@is_lawful_functor F F1] [@is_lawful_functor F F2] (H : ∀ α β (f : α → β) (x : F α), @functor.map _ F1 _ _ f x = @functor.map _ F2 _ _ f x), F1 = F2 | ⟨m, mc⟩ ⟨m', mc'⟩ H1 H2 H := begin cases show @m = @m', by funext α β f x; apply H, congr, funext α β, have E1 := @map_const_eq _ ⟨@m, @mc⟩ H1, have E2 := @map_const_eq _ ⟨@m, @mc'⟩ H2, exact E1.trans E2.symm end end functor /-- Introduce the `id` functor. Incidentally, this is `pure` for `id` as a `monad` and as an `applicative` functor. -/ def id.mk {α : Sort u} : α → id α := id namespace functor /-- `const α` is the constant functor, mapping every type to `α`. When `α` has a monoid structure, `const α` has an `applicative` instance. (If `α` has an additive monoid structure, see `functor.add_const`.) -/ @[nolint unused_arguments] def const (α : Type*) (β : Type*) := α /-- `const.mk` is the canonical map `α → const α β` (the identity), and it can be used as a pattern to extract this value. -/ @[pattern] def const.mk {α β} (x : α) : const α β := x /-- `const.mk'` is `const.mk` but specialized to map `α` to `const α punit`, where `punit` is the terminal object in `Type*`. -/ def const.mk' {α} (x : α) : const α punit := x /-- Extract the element of `α` from the `const` functor. -/ def const.run {α β} (x : const α β) : α := x namespace const protected lemma ext {α β} {x y : const α β} (h : x.run = y.run) : x = y := h /-- The map operation of the `const γ` functor. -/ @[nolint unused_arguments] protected def map {γ α β} (f : α → β) (x : const γ β) : const γ α := x instance {γ} : functor (const γ) := { map := @const.map γ } instance {γ} : is_lawful_functor (const γ) := by constructor; intros; refl instance {α β} [inhabited α] : inhabited (const α β) := ⟨(default _ : α)⟩ end const /-- `add_const α` is a synonym for constant functor `const α`, mapping every type to `α`. When `α` has a additive monoid structure, `add_const α` has an `applicative` instance. (If `α` has a multiplicative monoid structure, see `functor.const`.) -/ def add_const (α : Type*) := const α /-- `add_const.mk` is the canonical map `α → add_const α β`, which is the identity, where `add_const α β = const α β`. It can be used as a pattern to extract this value. -/ @[pattern] def add_const.mk {α β} (x : α) : add_const α β := x /-- Extract the element of `α` from the constant functor. -/ def add_const.run {α β} : add_const α β → α := id instance add_const.functor {γ} : functor (add_const γ) := @const.functor γ instance add_const.is_lawful_functor {γ} : is_lawful_functor (add_const γ) := @const.is_lawful_functor γ instance {α β} [inhabited α] : inhabited (add_const α β) := ⟨(default _ : α)⟩ /-- `functor.comp` is a wrapper around `function.comp` for types. It prevents Lean's type class resolution mechanism from trying a `functor (comp F id)` when `functor F` would do. -/ def comp (F : Type u → Type w) (G : Type v → Type u) (α : Type v) : Type w := F $ G α /-- Construct a term of `comp F G α` from a term of `F (G α)`, which is the same type. Can be used as a pattern to extract a term of `F (G α)`. -/ @[pattern] def comp.mk {F : Type u → Type w} {G : Type v → Type u} {α : Type v} (x : F (G α)) : comp F G α := x /-- Extract a term of `F (G α)` from a term of `comp F G α`, which is the same type. -/ def comp.run {F : Type u → Type w} {G : Type v → Type u} {α : Type v} (x : comp F G α) : F (G α) := x namespace comp variables {F : Type u → Type w} {G : Type v → Type u} protected lemma ext {α} {x y : comp F G α} : x.run = y.run → x = y := id instance {α} [inhabited (F (G α))] : inhabited (comp F G α) := ⟨(default _ : F (G α))⟩ variables [functor F] [functor G] /-- The map operation for the composition `comp F G` of functors `F` and `G`. -/ protected def map {α β : Type v} (h : α → β) : comp F G α → comp F G β | (comp.mk x) := comp.mk ((<$>) h <$> x) instance : functor (comp F G) := { map := @comp.map F G _ _ } @[functor_norm] lemma map_mk {α β} (h : α → β) (x : F (G α)) : h <$> comp.mk x = comp.mk ((<$>) h <$> x) := rfl @[simp] protected lemma run_map {α β} (h : α → β) (x : comp F G α) : (h <$> x).run = (<$>) h <$> x.run := rfl variables [is_lawful_functor F] [is_lawful_functor G] variables {α β γ : Type v} protected lemma id_map : ∀ (x : comp F G α), comp.map id x = x | (comp.mk x) := by simp [comp.map, functor.map_id] protected lemma comp_map (g' : α → β) (h : β → γ) : ∀ (x : comp F G α), comp.map (h ∘ g') x = comp.map h (comp.map g' x) | (comp.mk x) := by simp [comp.map, functor.map_comp_map g' h] with functor_norm instance : is_lawful_functor (comp F G) := { id_map := @comp.id_map F G _ _ _ _, comp_map := @comp.comp_map F G _ _ _ _ } theorem functor_comp_id {F} [AF : functor F] [is_lawful_functor F] : @comp.functor F id _ _ = AF := @functor.ext F _ AF (@comp.is_lawful_functor F id _ _ _ _) _ (λ α β f x, rfl) theorem functor_id_comp {F} [AF : functor F] [is_lawful_functor F] : @comp.functor id F _ _ = AF := @functor.ext F _ AF (@comp.is_lawful_functor id F _ _ _ _) _ (λ α β f x, rfl) end comp namespace comp open function (hiding comp) open functor variables {F : Type u → Type w} {G : Type v → Type u} variables [applicative F] [applicative G] /-- The `<*>` operation for the composition of applicative functors. -/ protected def seq {α β : Type v} : comp F G (α → β) → comp F G α → comp F G β | (comp.mk f) (comp.mk x) := comp.mk $ (<*>) <$> f <*> x instance : has_pure (comp F G) := ⟨λ _ x, comp.mk $ pure $ pure x⟩ instance : has_seq (comp F G) := ⟨λ _ _ f x, comp.seq f x⟩ @[simp] protected lemma run_pure {α : Type v} : ∀ x : α, (pure x : comp F G α).run = pure (pure x) | _ := rfl @[simp] protected lemma run_seq {α β : Type v} (f : comp F G (α → β)) (x : comp F G α) : (f <*> x).run = (<*>) <$> f.run <*> x.run := rfl instance : applicative (comp F G) := { map := @comp.map F G _ _, seq := @comp.seq F G _ _, ..comp.has_pure } end comp variables {F : Type u → Type u} [functor F] /-- If we consider `x : F α` to, in some sense, contain values of type `α`, predicate `liftp p x` holds iff every value contained by `x` satisfies `p`. -/ def liftp {α : Type u} (p : α → Prop) (x : F α) : Prop := ∃ u : F (subtype p), subtype.val <$> u = x /-- If we consider `x : F α` to, in some sense, contain values of type `α`, then `liftr r x y` relates `x` and `y` iff (1) `x` and `y` have the same shape and (2) we can pair values `a` from `x` and `b` from `y` so that `r a b` holds. -/ def liftr {α : Type u} (r : α → α → Prop) (x y : F α) : Prop := ∃ u : F {p : α × α // r p.fst p.snd}, (λ t : {p : α × α // r p.fst p.snd}, t.val.fst) <$> u = x ∧ (λ t : {p : α × α // r p.fst p.snd}, t.val.snd) <$> u = y /-- If we consider `x : F α` to, in some sense, contain values of type `α`, then `supp x` is the set of values of type `α` that `x` contains. -/ def supp {α : Type u} (x : F α) : set α := { y : α | ∀ ⦃p⦄, liftp p x → p y } theorem of_mem_supp {α : Type u} {x : F α} {p : α → Prop} (h : liftp p x) : ∀ y ∈ supp x, p y := λ y hy, hy h end functor namespace ulift instance : functor ulift := { map := λ α β f, up ∘ f ∘ down } end ulift
871615b0a0aa7b40172e412d627bf4a751744305
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/t7.lean
3704d002bccd661d3a6a822a62e5361ec891d8cd
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
629
lean
prelude definition Prop : Type.{1} := Type.{0} constant and : Prop → Prop → Prop section parameter {A : Type} -- Mark A as implicit parameter parameter R : A → A → Prop parameter B : Type definition id (a : A) : A := a private definition refl : Prop := ∀ (a : A), R a a definition symm : Prop := ∀ (a b : A), R a b → R b a definition trans : Prop := ∀ (a b c : A), R a b → R b c → R a c definition equivalence : Prop := and (and refl symm) trans end check id.{2} check trans.{1} check symm.{1} check equivalence.{1} (* local env = get_env() print(env:find("equivalence"):value()) *)
fc5d051b861fa3368c912f40b94d7eedf5b04f2c
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/check.lean
2d5cf6de830a23bf34d848354828816f47c2f96d
[ "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
60
lean
-- #check And.intro #check Or.elim #check Eq #check Eq.rec
e36e27b610ebd4f326461e714f1e9b15ad9135a6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/equiv/mul_add.lean
770c9216b909bf123658d63f51aa47bb28b3b392
[ "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
24,707
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, Callum Sutton, Yury Kudryashov -/ import algebra.group.type_tags import algebra.group_with_zero import data.equiv.set /-! # Multiplicative and additive equivs In this file we define two extensions of `equiv` called `add_equiv` and `mul_equiv`, which are datatypes representing isomorphisms of `add_monoid`s/`add_group`s and `monoid`s/`group`s. ## Notations * ``infix ` ≃* `:25 := mul_equiv`` * ``infix ` ≃+ `:25 := add_equiv`` The extended equivs all have coercions to functions, and the coercions are the canonical notation when treating the isomorphisms as maps. ## Implementation notes The fields for `mul_equiv`, `add_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. ## Tags equiv, mul_equiv, add_equiv -/ variables {A : Type*} {B : Type*} {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {G : Type*} {H : Type*} /-- Makes a multiplicative inverse from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive inverse from a bijection which preserves addition."] def mul_hom.inverse [has_mul M] [has_mul N] (f : mul_hom M N) (g : N → M) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : mul_hom N M := { to_fun := g, map_mul' := λ x y, calc g (x * y) = g (f (g x) * f (g y)) : by rw [h₂ x, h₂ y] ... = g (f (g x * g y)) : by rw f.map_mul ... = g x * g y : h₁ _, } /-- The inverse of a bijective `monoid_hom` is a `monoid_hom`. -/ @[to_additive "The inverse of a bijective `add_monoid_hom` is an `add_monoid_hom`.", simps] def monoid_hom.inverse {A B : Type*} [monoid A] [monoid B] (f : A →* B) (g : B → A) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : B →* A := { to_fun := g, map_one' := by rw [← f.map_one, h₁], .. (f : mul_hom A B).inverse g h₁ h₂, } set_option old_structure_cmd true /-- add_equiv α β is the type of an equiv α ≃ β which preserves addition. -/ @[ancestor equiv add_hom] structure add_equiv (A B : Type*) [has_add A] [has_add B] extends A ≃ B, add_hom A B /-- The `equiv` underlying an `add_equiv`. -/ add_decl_doc add_equiv.to_equiv /-- The `add_hom` underlying a `add_equiv`. -/ add_decl_doc add_equiv.to_add_hom /-- `mul_equiv α β` is the type of an equiv `α ≃ β` which preserves multiplication. -/ @[ancestor equiv mul_hom, to_additive] structure mul_equiv (M N : Type*) [has_mul M] [has_mul N] extends M ≃ N, mul_hom M N /-- The `equiv` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_equiv /-- The `mul_hom` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_mul_hom infix ` ≃* `:25 := mul_equiv infix ` ≃+ `:25 := add_equiv namespace mul_equiv @[to_additive] instance [has_mul M] [has_mul N] : has_coe_to_fun (M ≃* N) (λ _, M → N) := ⟨mul_equiv.to_fun⟩ variables [has_mul M] [has_mul N] [has_mul P] [has_mul Q] @[simp, to_additive] lemma to_fun_eq_coe {f : M ≃* N} : f.to_fun = f := rfl @[simp, to_additive] lemma coe_to_equiv {f : M ≃* N} : ⇑f.to_equiv = f := rfl @[simp, to_additive] lemma coe_to_mul_hom {f : M ≃* N} : ⇑f.to_mul_hom = f := rfl /-- A multiplicative isomorphism preserves multiplication (canonical form). -/ @[simp, to_additive] lemma map_mul (f : M ≃* N) : ∀ x y, f (x * y) = f x * f y := f.map_mul' /-- Makes a multiplicative isomorphism from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive isomorphism from a bijection which preserves addition."] def mk' (f : M ≃ N) (h : ∀ x y, f (x * y) = f x * f y) : M ≃* N := ⟨f.1, f.2, f.3, f.4, h⟩ @[to_additive] protected lemma bijective (e : M ≃* N) : function.bijective e := e.to_equiv.bijective @[to_additive] protected lemma injective (e : M ≃* N) : function.injective e := e.to_equiv.injective @[to_additive] protected lemma surjective (e : M ≃* N) : function.surjective e := e.to_equiv.surjective /-- The identity map is a multiplicative isomorphism. -/ @[refl, to_additive "The identity map is an additive isomorphism."] def refl (M : Type*) [has_mul M] : M ≃* M := { map_mul' := λ _ _, rfl, ..equiv.refl _} @[to_additive] instance : inhabited (M ≃* M) := ⟨refl M⟩ /-- The inverse of an isomorphism is an isomorphism. -/ @[symm, to_additive "The inverse of an isomorphism is an isomorphism."] def symm (h : M ≃* N) : N ≃* M := { map_mul' := (h.to_mul_hom.inverse h.to_equiv.symm h.left_inv h.right_inv).map_mul, .. h.to_equiv.symm} @[simp, to_additive] lemma inv_fun_eq_symm {f : M ≃* N} : f.inv_fun = f.symm := rfl /-- See Note [custom simps projection] -/ -- we don't hyperlink the note in the additive version, since that breaks syntax highlighting -- in the whole file. @[to_additive "See Note custom simps projection"] def simps.symm_apply (e : M ≃* N) : N → M := e.symm initialize_simps_projections add_equiv (to_fun → apply, inv_fun → symm_apply) initialize_simps_projections mul_equiv (to_fun → apply, inv_fun → symm_apply) @[simp, to_additive] theorem to_equiv_symm (f : M ≃* N) : f.symm.to_equiv = f.to_equiv.symm := rfl @[simp, to_additive] theorem coe_mk (f : M → N) (g h₁ h₂ h₃) : ⇑(mul_equiv.mk f g h₁ h₂ h₃) = f := rfl @[simp, to_additive] lemma to_equiv_mk (f : M → N) (g : N → M) (h₁ h₂ h₃) : (mk f g h₁ h₂ h₃).to_equiv = ⟨f, g, h₁, h₂⟩ := rfl @[simp, to_additive] lemma symm_symm : ∀ (f : M ≃* N), f.symm.symm = f | ⟨f, g, h₁, h₂, h₃⟩ := rfl @[to_additive] lemma symm_bijective : function.bijective (symm : (M ≃* N) → (N ≃* M)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp, to_additive] theorem symm_mk (f : M → N) (g h₁ h₂ h₃) : (mul_equiv.mk f g h₁ h₂ h₃).symm = { to_fun := g, inv_fun := f, ..(mul_equiv.mk f g h₁ h₂ h₃).symm} := rfl /-- Transitivity of multiplication-preserving isomorphisms -/ @[trans, to_additive "Transitivity of addition-preserving isomorphisms"] def trans (h1 : M ≃* N) (h2 : N ≃* P) : (M ≃* P) := { map_mul' := λ x y, show h2 (h1 (x * y)) = h2 (h1 x) * h2 (h1 y), by rw [h1.map_mul, h2.map_mul], ..h1.to_equiv.trans h2.to_equiv } /-- e.right_inv in canonical form -/ @[simp, to_additive] lemma apply_symm_apply (e : M ≃* N) : ∀ y, e (e.symm y) = y := e.to_equiv.apply_symm_apply /-- e.left_inv in canonical form -/ @[simp, to_additive] lemma symm_apply_apply (e : M ≃* N) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp, to_additive] theorem symm_comp_self (e : M ≃* N) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp, to_additive] theorem self_comp_symm (e : M ≃* N) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp, to_additive] theorem coe_refl : ⇑(refl M) = id := rfl @[to_additive] theorem refl_apply (m : M) : refl M m = m := rfl @[simp, to_additive] theorem coe_trans (e₁ : M ≃* N) (e₂ : N ≃* P) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[to_additive] theorem trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (m : M) : e₁.trans e₂ m = e₂ (e₁ m) := rfl @[simp, to_additive] theorem symm_trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (p : P) : (e₁.trans e₂).symm p = e₁.symm (e₂.symm p) := rfl @[simp, to_additive] theorem apply_eq_iff_eq (e : M ≃* N) {x y : M} : e x = e y ↔ x = y := e.injective.eq_iff @[to_additive] lemma apply_eq_iff_symm_apply (e : M ≃* N) {x : M} {y : N} : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply @[to_additive] lemma symm_apply_eq (e : M ≃* N) {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq @[to_additive] lemma eq_symm_apply (e : M ≃* N) {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply /-- Two multiplicative isomorphisms agree if they are defined by the same underlying function. -/ @[ext, to_additive "Two additive isomorphisms agree if they are defined by the same underlying function."] lemma ext {f g : mul_equiv M N} (h : ∀ x, f x = g x) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end @[to_additive] lemma ext_iff {f g : mul_equiv M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ @[simp, to_additive] lemma mk_coe (e : M ≃* N) (e' h₁ h₂ h₃) : (⟨e, e', h₁, h₂, h₃⟩ : M ≃* N) = e := ext $ λ _, rfl @[simp, to_additive] lemma mk_coe' (e : M ≃* N) (f h₁ h₂ h₃) : (mul_equiv.mk f ⇑e h₁ h₂ h₃ : N ≃* M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[to_additive] protected lemma congr_arg {f : mul_equiv M N} : Π {x x' : M}, x = x' → f x = f x' | _ _ rfl := rfl @[to_additive] protected lemma congr_fun {f g : mul_equiv M N} (h : f = g) (x : M) : f x = g x := h ▸ rfl /-- The `mul_equiv` between two monoids with a unique element. -/ @[to_additive "The `add_equiv` between two add_monoids with a unique element."] def mul_equiv_of_unique_of_unique {M N} [unique M] [unique N] [has_mul M] [has_mul N] : M ≃* N := { map_mul' := λ _ _, subsingleton.elim _ _, ..equiv_of_unique_of_unique } /-- There is a unique monoid homomorphism between two monoids with a unique element. -/ @[to_additive] instance {M N} [unique M] [unique N] [has_mul M] [has_mul N] : unique (M ≃* N) := { default := mul_equiv_of_unique_of_unique , uniq := λ _, ext $ λ x, subsingleton.elim _ _} /-! ## Monoids -/ /-- A multiplicative equiv of monoids sends 1 to 1 (and is hence a monoid isomorphism). -/ @[simp, to_additive] lemma map_one {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : h 1 = 1 := by rw [←mul_one (h 1), ←h.apply_symm_apply 1, ←h.map_mul, one_mul] @[simp, to_additive] lemma map_eq_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x = 1 ↔ x = 1 := h.map_one ▸ h.to_equiv.apply_eq_iff_eq @[to_additive] lemma map_ne_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x ≠ 1 ↔ x ≠ 1 := ⟨mt h.map_eq_one_iff.2, mt h.map_eq_one_iff.1⟩ /-- A bijective `monoid` homomorphism is an isomorphism -/ @[to_additive "A bijective `add_monoid` homomorphism is an isomorphism"] noncomputable def of_bijective {M N} [mul_one_class M] [mul_one_class N] (f : M →* N) (hf : function.bijective f) : M ≃* N := { map_mul' := f.map_mul', ..equiv.of_bijective f hf } /-- Extract the forward direction of a multiplicative equivalence as a multiplication-preserving function. -/ @[to_additive "Extract the forward direction of an additive equivalence as an addition-preserving function."] def to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : (M →* N) := { map_one' := h.map_one, .. h } @[simp, to_additive] lemma coe_to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (e : M ≃* N) : ⇑e.to_monoid_hom = e := rfl @[to_additive] lemma to_monoid_hom_injective {M N} [mul_one_class M] [mul_one_class N] : function.injective (to_monoid_hom : (M ≃* N) → M →* N) := λ f g h, mul_equiv.ext (monoid_hom.ext_iff.1 h) /-- A multiplicative analogue of `equiv.arrow_congr`, where the equivalence between the targets is multiplicative. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, where the equivalence between the targets is additive.", simps apply] def arrow_congr {M N P Q : Type*} [mul_one_class P] [mul_one_class Q] (f : M ≃ N) (g : P ≃* Q) : (M → P) ≃* (N → Q) := { to_fun := λ h n, g (h (f.symm n)), inv_fun := λ k m, g.symm (k (f m)), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A multiplicative analogue of `equiv.arrow_congr`, for multiplicative maps from a monoid to a commutative monoid. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, for additive maps from an additive monoid to a commutative additive monoid.", simps apply] def monoid_hom_congr {M N P Q} [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M ≃* N) (g : P ≃* Q) : (M →* P) ≃* (N →* Q) := { to_fun := λ h, g.to_monoid_hom.comp (h.comp f.symm.to_monoid_hom), inv_fun := λ k, g.symm.to_monoid_hom.comp (k.comp f.to_monoid_hom), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A family of multiplicative equivalences `Π j, (Ms j ≃* Ns j)` generates a multiplicative equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `mul_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `mul_equiv.arrow_congr`. -/ @[to_additive add_equiv.Pi_congr_right "A family of additive equivalences `Π j, (Ms j ≃+ Ns j)` generates an additive equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `add_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `add_equiv.arrow_congr`.", simps apply] def Pi_congr_right {η : Type*} {Ms Ns : η → Type*} [Π j, mul_one_class (Ms j)] [Π j, mul_one_class (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Π j, Ms j) ≃* (Π j, Ns j) := { to_fun := λ x j, es j (x j), inv_fun := λ x j, (es j).symm (x j), map_mul' := λ x y, funext $ λ j, (es j).map_mul (x j) (y j), .. equiv.Pi_congr_right (λ j, (es j).to_equiv) } @[simp] lemma Pi_congr_right_refl {η : Type*} {Ms : η → Type*} [Π j, mul_one_class (Ms j)] : Pi_congr_right (λ j, mul_equiv.refl (Ms j)) = mul_equiv.refl _ := rfl @[simp] lemma Pi_congr_right_symm {η : Type*} {Ms Ns : η → Type*} [Π j, mul_one_class (Ms j)] [Π j, mul_one_class (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Pi_congr_right es).symm = (Pi_congr_right $ λ i, (es i).symm) := rfl @[simp] lemma Pi_congr_right_trans {η : Type*} {Ms Ns Ps : η → Type*} [Π j, mul_one_class (Ms j)] [Π j, mul_one_class (Ns j)] [Π j, mul_one_class (Ps j)] (es : ∀ j, Ms j ≃* Ns j) (fs : ∀ j, Ns j ≃* Ps j) : (Pi_congr_right es).trans (Pi_congr_right fs) = (Pi_congr_right $ λ i, (es i).trans (fs i)) := rfl /-! # Groups -/ /-- A multiplicative equivalence of groups preserves inversion. -/ @[simp, to_additive] lemma map_inv [group G] [group H] (h : G ≃* H) (x : G) : h x⁻¹ = (h x)⁻¹ := h.to_monoid_hom.map_inv x end mul_equiv /-- Given a pair of monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an multiplicative equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for monoid homomorphisms. -/ @[to_additive /-"Given a pair of additive monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an additive equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for additive monoid homomorphisms."-/, simps {fully_applied := ff}] def monoid_hom.to_mul_equiv [mul_one_class M] [mul_one_class N] (f : M →* N) (g : N →* M) (h₁ : g.comp f = monoid_hom.id _) (h₂ : f.comp g = monoid_hom.id _) : M ≃* N := { to_fun := f, inv_fun := g, left_inv := monoid_hom.congr_fun h₁, right_inv := monoid_hom.congr_fun h₂, map_mul' := f.map_mul } /-- An additive equivalence of additive groups preserves subtraction. -/ lemma add_equiv.map_sub [add_group A] [add_group B] (h : A ≃+ B) (x y : A) : h (x - y) = h x - h y := h.to_add_monoid_hom.map_sub x y /-- A group is isomorphic to its group of units. -/ @[to_additive to_add_units "An additive group is isomorphic to its group of additive units"] def to_units [group G] : G ≃* units G := { to_fun := λ x, ⟨x, x⁻¹, mul_inv_self _, inv_mul_self _⟩, inv_fun := coe, left_inv := λ x, rfl, right_inv := λ u, units.ext rfl, map_mul' := λ x y, units.ext rfl } @[simp, to_additive coe_to_add_units] lemma coe_to_units [group G] (g : G) : (to_units g : G) = g := rfl protected lemma group.is_unit {G} [group G] (x : G) : is_unit x := (to_units x).is_unit namespace units @[simp, to_additive] lemma coe_inv [group G] (u : units G) : ↑u⁻¹ = (u⁻¹ : G) := to_units.symm.map_inv u variables [monoid M] [monoid N] [monoid P] /-- A multiplicative equivalence of monoids defines a multiplicative equivalence of their groups of units. -/ def map_equiv (h : M ≃* N) : units M ≃* units N := { inv_fun := map h.symm.to_monoid_hom, left_inv := λ u, ext $ h.left_inv u, right_inv := λ u, ext $ h.right_inv u, .. map h.to_monoid_hom } /-- Left multiplication by a unit of a monoid is a permutation of the underlying type. -/ @[to_additive "Left addition of an additive unit is a permutation of the underlying type.", simps apply {fully_applied := ff}] def mul_left (u : units M) : equiv.perm M := { to_fun := λx, u * x, inv_fun := λx, ↑u⁻¹ * x, left_inv := u.inv_mul_cancel_left, right_inv := u.mul_inv_cancel_left } @[simp, to_additive] lemma mul_left_symm (u : units M) : u.mul_left.symm = u⁻¹.mul_left := equiv.ext $ λ x, rfl @[to_additive] lemma mul_left_bijective (a : units M) : function.bijective ((*) a : M → M) := (mul_left a).bijective /-- Right multiplication by a unit of a monoid is a permutation of the underlying type. -/ @[to_additive "Right addition of an additive unit is a permutation of the underlying type.", simps apply {fully_applied := ff}] def mul_right (u : units M) : equiv.perm M := { to_fun := λx, x * u, inv_fun := λx, x * ↑u⁻¹, left_inv := λ x, mul_inv_cancel_right x u, right_inv := λ x, inv_mul_cancel_right x u } @[simp, to_additive] lemma mul_right_symm (u : units M) : u.mul_right.symm = u⁻¹.mul_right := equiv.ext $ λ x, rfl @[to_additive] lemma mul_right_bijective (a : units M) : function.bijective ((* a) : M → M) := (mul_right a).bijective end units namespace equiv section group variables [group G] /-- Left multiplication in a `group` is a permutation of the underlying type. -/ @[to_additive "Left addition in an `add_group` is a permutation of the underlying type."] protected def mul_left (a : G) : perm G := (to_units a).mul_left @[simp, to_additive] lemma coe_mul_left (a : G) : ⇑(equiv.mul_left a) = (*) a := rfl /-- extra simp lemma that `dsimp` can use. `simp` will never use this. -/ @[simp, nolint simp_nf, to_additive] lemma mul_left_symm_apply (a : G) : ((equiv.mul_left a).symm : G → G) = (*) a⁻¹ := rfl @[simp, to_additive] lemma mul_left_symm (a : G) : (equiv.mul_left a).symm = equiv.mul_left a⁻¹ := ext $ λ x, rfl @[to_additive] lemma _root_.group.mul_left_bijective (a : G) : function.bijective ((*) a) := (equiv.mul_left a).bijective /-- Right multiplication in a `group` is a permutation of the underlying type. -/ @[to_additive "Right addition in an `add_group` is a permutation of the underlying type."] protected def mul_right (a : G) : perm G := (to_units a).mul_right @[simp, to_additive] lemma coe_mul_right (a : G) : ⇑(equiv.mul_right a) = λ x, x * a := rfl @[simp, to_additive] lemma mul_right_symm (a : G) : (equiv.mul_right a).symm = equiv.mul_right a⁻¹ := ext $ λ x, rfl /-- extra simp lemma that `dsimp` can use. `simp` will never use this. -/ @[simp, nolint simp_nf, to_additive] lemma mul_right_symm_apply (a : G) : ((equiv.mul_right a).symm : G → G) = λ x, x * a⁻¹ := rfl @[to_additive] lemma _root_.group.mul_right_bijective (a : G) : function.bijective (* a) := (equiv.mul_right a).bijective variable (G) /-- Inversion on a `group` is a permutation of the underlying type. -/ @[to_additive "Negation on an `add_group` is a permutation of the underlying type.", simps apply {fully_applied := ff}] protected def inv : perm G := { to_fun := λa, a⁻¹, inv_fun := λa, a⁻¹, left_inv := assume a, inv_inv a, right_inv := assume a, inv_inv a } variable {G} @[simp, to_additive] lemma inv_symm : (equiv.inv G).symm = equiv.inv G := rfl /-- A version of `equiv.mul_left a b⁻¹` that is defeq to `a / b`. -/ @[to_additive /-" A version of `equiv.add_left a (-b)` that is defeq to `a - b`. "-/, simps] protected def div_left (a : G) : G ≃ G := { to_fun := λ b, a / b, inv_fun := λ b, b⁻¹ * a, left_inv := λ b, by simp [div_eq_mul_inv], right_inv := λ b, by simp [div_eq_mul_inv] } @[to_additive] lemma div_left_eq_inv_trans_mul_left (a : G) : equiv.div_left a = (equiv.inv G).trans (equiv.mul_left a) := ext $ λ _, div_eq_mul_inv _ _ /-- A version of `equiv.mul_right a⁻¹ b` that is defeq to `b / a`. -/ @[to_additive /-" A version of `equiv.add_right (-a) b` that is defeq to `b - a`. "-/, simps] protected def div_right (a : G) : G ≃ G := { to_fun := λ b, b / a, inv_fun := λ b, b * a, left_inv := λ b, by simp [div_eq_mul_inv], right_inv := λ b, by simp [div_eq_mul_inv] } @[to_additive] lemma div_right_eq_mul_right_inv (a : G) : equiv.div_right a = equiv.mul_right a⁻¹ := ext $ λ _, div_eq_mul_inv _ _ end group section group_with_zero variables [group_with_zero G] /-- Left multiplication by a nonzero element in a `group_with_zero` is a permutation of the underlying type. -/ @[simps {fully_applied := ff}] protected def mul_left₀ (a : G) (ha : a ≠ 0) : perm G := (units.mk0 a ha).mul_left lemma _root_.mul_left_bijective₀ (a : G) (ha : a ≠ 0) : function.bijective ((*) a : G → G) := (equiv.mul_left₀ a ha).bijective /-- Right multiplication by a nonzero element in a `group_with_zero` is a permutation of the underlying type. -/ @[simps {fully_applied := ff}] protected def mul_right₀ (a : G) (ha : a ≠ 0) : perm G := (units.mk0 a ha).mul_right lemma _root_.mul_right_bijective₀ (a : G) (ha : a ≠ 0) : function.bijective ((* a) : G → G) := (equiv.mul_right₀ a ha).bijective end group_with_zero end equiv /-- When the group is commutative, `equiv.inv` is a `mul_equiv`. There is a variant of this `mul_equiv.inv' G : G ≃* Gᵒᵖ` for the non-commutative case. -/ @[to_additive "When the `add_group` is commutative, `equiv.neg` is an `add_equiv`."] def mul_equiv.inv (G : Type*) [comm_group G] : G ≃* G := { to_fun := has_inv.inv, inv_fun := has_inv.inv, map_mul' := mul_inv, ..equiv.inv G} section type_tags /-- Reinterpret `G ≃+ H` as `multiplicative G ≃* multiplicative H`. -/ def add_equiv.to_multiplicative [add_zero_class G] [add_zero_class H] : (G ≃+ H) ≃ (multiplicative G ≃* multiplicative H) := { to_fun := λ f, ⟨f.to_add_monoid_hom.to_multiplicative, f.symm.to_add_monoid_hom.to_multiplicative, f.3, f.4, f.5⟩, inv_fun := λ f, ⟨f.to_monoid_hom, f.symm.to_monoid_hom, f.3, f.4, f.5⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, by { ext, refl, }, } /-- Reinterpret `G ≃* H` as `additive G ≃+ additive H`. -/ def mul_equiv.to_additive [mul_one_class G] [mul_one_class H] : (G ≃* H) ≃ (additive G ≃+ additive H) := { to_fun := λ f, ⟨f.to_monoid_hom.to_additive, f.symm.to_monoid_hom.to_additive, f.3, f.4, f.5⟩, inv_fun := λ f, ⟨f.to_add_monoid_hom, f.symm.to_add_monoid_hom, f.3, f.4, f.5⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, by { ext, refl, }, } /-- Reinterpret `additive G ≃+ H` as `G ≃* multiplicative H`. -/ def add_equiv.to_multiplicative' [mul_one_class G] [add_zero_class H] : (additive G ≃+ H) ≃ (G ≃* multiplicative H) := { to_fun := λ f, ⟨f.to_add_monoid_hom.to_multiplicative', f.symm.to_add_monoid_hom.to_multiplicative'', f.3, f.4, f.5⟩, inv_fun := λ f, ⟨f.to_monoid_hom, f.symm.to_monoid_hom, f.3, f.4, f.5⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, by { ext, refl, }, } /-- Reinterpret `G ≃* multiplicative H` as `additive G ≃+ H` as. -/ def mul_equiv.to_additive' [mul_one_class G] [add_zero_class H] : (G ≃* multiplicative H) ≃ (additive G ≃+ H) := add_equiv.to_multiplicative'.symm /-- Reinterpret `G ≃+ additive H` as `multiplicative G ≃* H`. -/ def add_equiv.to_multiplicative'' [add_zero_class G] [mul_one_class H] : (G ≃+ additive H) ≃ (multiplicative G ≃* H) := { to_fun := λ f, ⟨f.to_add_monoid_hom.to_multiplicative'', f.symm.to_add_monoid_hom.to_multiplicative', f.3, f.4, f.5⟩, inv_fun := λ f, ⟨f.to_monoid_hom, f.symm.to_monoid_hom, f.3, f.4, f.5⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, by { ext, refl, }, } /-- Reinterpret `multiplicative G ≃* H` as `G ≃+ additive H` as. -/ def mul_equiv.to_additive'' [add_zero_class G] [mul_one_class H] : (multiplicative G ≃* H) ≃ (G ≃+ additive H) := add_equiv.to_multiplicative''.symm end type_tags