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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0a8ee14095befd132880cb7795a69106dd89596b | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/geo/src/group_objet/group_obj_def.lean | 91a734f423e14e72d9fc40564ef78b2b498c51d2 | [] | no_license | Or7ando/lean | cc003e6c41048eae7c34aa6bada51c9e9add9e66 | d41169cf4e416a0d42092fb6bdc14131cee9dd15 | refs/heads/master | 1,650,600,589,722 | 1,587,262,906,000 | 1,587,262,906,000 | 255,387,160 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,410 | lean | import category_theory.limits.limits
import category_theory.limits.shapes
import category_theory.yoneda
import category_theory.opposites
import category_theory.types
import category_theory.limits.types
run_cmd mk_simp_attr `PRODUCT ----- BOF BOF
meta def PRODUCT_CAT : tactic unit :=
`[ try {simp only with PRODUCT}]
run_cmd add_interactive [`PRODUCT_CAT]
universes v u
open category_theory
open category_theory.limits
open category_theory.category
open opposite
namespace lem
variables {C : Type u}
variables [𝒞 : category.{v} C]
variables [has_binary_products.{v} C][has_terminal.{v} C]
include 𝒞
attribute [PRODUCT] category.assoc category.id_comp category.comp_id
@[PRODUCT] lemma prod_left_def {X Y : C} : limit.π (pair X Y) walking_pair.left = limits.prod.fst := rfl
@[PRODUCT] lemma prod_right_def {X Y : C} : limit.π (pair X Y) walking_pair.right = limits.prod.snd := rfl
lemma prod.hom_ext {A X Y : C} {a b : A ⟶ X ⨯ Y} (h1 : a ≫ limits.prod.fst = b ≫ limits.prod.fst) (h2 : a ≫ limits.prod.snd = b ≫ limits.prod.snd) : a = b :=
begin
apply limit.hom_ext,
rintros (_ | _),
rw prod_left_def,
exact h1,
rw prod_right_def,
exact h2,
end
@[PRODUCT]lemma prod.lift_fst {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : prod.lift f g ≫ category_theory.limits.prod.fst = f :=
limit.lift_π (binary_fan.mk f g) _
@[PRODUCT]lemma prod.lift_snd {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : prod.lift f g ≫ category_theory.limits.prod.snd = g :=
limit.lift_π (binary_fan.mk f g) _
end lem
notation f ` ⊗ `:20 g :20 := category_theory.limits.prod.map f g ---- 20
notation `T`C :20 := (terminal C)
notation `T`X : 20 := (terminal.from X)
notation f ` | `:20 g :20 := prod.lift f g
notation `π1` := limits.prod.fst
notation `π2` := limits.prod.snd
variables {C : Type u}
variables [𝒞 : category.{v} C]
variables [has_binary_products.{v} C][has_terminal.{v} C]
include 𝒞
variables (X :C)
open lem
/-
π notation for projection
-/
@[PRODUCT]lemma ex_1 {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : ( f | g) ≫ π1 = f := prod.lift_fst f g
/-
we can type π : A ⨯ B ⟶ B if we need
-/
@[PRODUCT]lemma ex_2 {Y A B : C} (f : Y ⟶ A) (g : Y ⟶ B) : ( f | g) ≫ (π2 : A ⨯ B ⟶ B) = g := prod.lift_snd f g
@[PRODUCT]lemma ex_3 {A X Y : C} {a b : A ⟶ X ⨯ Y} (h1 : a ≫ π1 = b ≫ π1 ) (h2 : a ≫ π2 = b ≫ π2) : a = b := prod.hom_ext h1 h2
@[PRODUCT]lemma prod.left_composition{Z' Z A B : C}(h : Z' ⟶ Z)(f : Z ⟶ A)(g : Z ⟶ B) :
h ≫ (f | g) = (h ≫ f | h ≫ g) :=
begin
apply prod.hom_ext, --- Le right member is of the form ( | ) composition π1 π2
PRODUCT_CAT, PRODUCT_CAT, --- here assoc
-- rw assoc,
-- rw prod.lift_fst,
-- rw prod.lift_fst,
-- rw prod.lift_snd,
-- rw assoc,
-- rw prod.lift_snd,
end
@[PRODUCT]lemma prod.map_first{X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) ≫ (π1 : Y ⨯ W ⟶ Y) = π1 ≫ f := begin
exact limit.map_π (map_pair f g) walking_pair.left,
end
@[PRODUCT]lemma prod.map_second{X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) ≫ π2 = π2 ≫ g := begin
exact limit.map_π (map_pair f g) walking_pair.right,
end
@[PRODUCT]lemma prod.otimes_is_prod {X Y Z W : C}(f : X ⟶ Y)(g : Z ⟶ W) : (f ⊗ g) = ( π1 ≫ f | π2 ≫ g ) := begin
apply prod.hom_ext,
PRODUCT_CAT, PRODUCT_CAT,
-- rw prod.lift_fst,
-- rw prod.map_first,
-- rw prod.lift_snd,
-- rw prod.map_second,
end
-- notation π1`(`X `x` Y`)` := (limits.prod.fst : X⨯Y ⟶ X)
@[PRODUCT]lemma prod.map_ext{X Y Z W : C}(f1 f2 : X ⟶ Y)(g1 g2 : Z ⟶ W) : (f1 ⊗ g1) = (f2 ⊗ g2) →
(π1 : X ⨯ Z ⟶ X) ≫ f1 = (π1 : X ⨯ Z ⟶ X) ≫ f2 := λ certif, begin
iterate 2 {rw prod.otimes_is_prod at certif},
rw ← prod.map_first ( f1) (g1),
rw ← prod.map_first ( f2) (g2),
iterate 2 {rw prod.otimes_is_prod},
rw certif,
end
@[PRODUCT]lemma destruction {X Y Z : C} (f : Y ⟶ X) (g : X ⟶ Z ) :
(f | 𝟙 Y) ≫ (g ⊗ (𝟙 Y)) = (f ≫ g | 𝟙 Y) := begin
apply prod.hom_ext,
-- PRODUCT_CAT,PRODUCT_CAT, ---------------------- PROBLEME With the tatict HEEEEEEERRRRRRE
rw [prod.lift_fst],
rw assoc,
rw prod.map_first,
rw ← assoc, ----- ← assoc here Problem ?
rw prod.lift_fst,
tidy, -- super - power tidy
end
def Y (R : C)(A :C) := (yoneda.obj A).obj (op R)
def Y_ (R : C) {A B : C}(φ : A ⟶ B) := ((yoneda.map φ).app (op R) : Y R A ⟶ Y R B)
-- Good notation for yoneda stuff :
-- We fix V : C and we denote by
-- R[X] := yoneda.obj X).obj (op R) and φ : A ⟶ B (in C) R ⟦ φ ⟧ : R[A] → R[B] in type v
notation R`[`A`]`:20 := Y R A -- notation ??
notation R`<`φ`>` :20 := Y_ R φ --
def Yoneda_preserve_product (Y : C)(A B : C) :
Y[A ⨯ B] ≅ Y[A] ⨯ Y[B] :=
{ hom := prod.lift
(λ f, f ≫ π1)
(λ f, f ≫ π2),
inv := λ f : (Y ⟶ A) ⨯ (Y ⟶ B),
(prod.lift
((@category_theory.limits.prod.fst _ _ (Y ⟶ A) (Y ⟶ B) _ : ((Y ⟶ A) ⨯ (Y ⟶ B)) → (Y ⟶ A)) f)
((@category_theory.limits.prod.snd _ _ (Y ⟶ A) _ _ : ((Y ⟶ A) ⨯ (Y ⟶ B)) → (Y ⟶ B)) f : Y ⟶ B)),
hom_inv_id' := begin
ext f,
cases j,
{ simp, refl},
{ simp, refl}
end,
inv_hom_id' := begin
apply lem.prod.hom_ext,
{ rw assoc, rw lem.prod.lift_fst, obviously},
{ rw assoc, rw lem.prod.lift_snd, obviously}
end
}
--- Here it just sugar
@[PRODUCT]lemma yoneda_sugar.composition (R : C) {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : R < f ≫ g > =( R< f >) ≫ (R < g >)
:= begin
unfold Y_,
simp,
end
def yoneda_sugar.conv {R : C}{A : C}(g : R[A]) : R ⟶ A := g
def yoneda_sugar.prod (R : C)(A B : C) : R[A ⨯ B] ≅ R[A] ⨯ R[B] := begin
exact Yoneda_preserve_product R A B,
end
@[PRODUCT]lemma yoneda_sugar.prod.hom (R : C)(A B : C) :
(yoneda_sugar.prod R A B).hom = (R < (limits.prod.fst : A ⨯ B ⟶ A) > | R < (limits.prod.snd : A ⨯ B ⟶ B)> ) := rfl
@[PRODUCT]lemma yoneda_sugar.prod.first (R : C)(A B : C) :
(yoneda_sugar.prod R A B).hom ≫ limits.prod.fst = (R < limits.prod.fst >) :=
begin
exact rfl,
end
@[PRODUCT]lemma yoneda_sugar.prod.hom_inv (R : C)(A B : C) :
(yoneda_sugar.prod R A B).hom ≫ (yoneda_sugar.prod R A B).inv = 𝟙 (R[ A ⨯ B]) :=
(Yoneda_preserve_product R A B).hom_inv_id'
@[PRODUCT]lemma yoneda_sugar.prod.inv_hom (R : C)(A B : C) :
(yoneda_sugar.prod R A B).inv ≫ (yoneda_sugar.prod R A B).hom = 𝟙 ( R [A] ⨯ R[B]) :=
(Yoneda_preserve_product R A B).inv_hom_id'
@[PRODUCT]lemma yoneda_sugar.prod.second (R : C)(A B : C) :
(yoneda_sugar.prod R A B).hom ≫ limits.prod.snd = (R < limits.prod.snd >) := rfl
@[PRODUCT]lemma yoneda_sugar.id (R : C)(A : C) : R < 𝟙 A > = 𝟙 ( R [A] ) := begin
funext,
exact comp_id C g,
-- have T : ((yoneda.map (𝟙 A)).app (op R)) g = (g ≫ (𝟙 A)),
end
lemma yoneda_sugar_prod (R : C)(A B : C)(X :C)(f : X ⟶ A)(g : X ⟶ B) :
R < (f | g) > ≫ (yoneda_sugar.prod R A B).hom = (R < f > | R < g > ) := -- the ≫ is :/
begin
PRODUCT_CAT,
-- rw yoneda_sugar.prod.hom R A B,
-- rw prod.left_composition,
iterate 2 {rw ← yoneda_sugar.composition}, -- rw ← is the problem ?
rw lem.prod.lift_fst,
rw lem.prod.lift_snd,
end
@[PRODUCT]lemma yoneda_sugar_prod_inv (R : C)(A B : C)(X :C)(f : X ⟶ A)(g : X ⟶ B) :
R < (f | g) > = (R < f > | R < g > ) ≫ (yoneda_sugar.prod R A B).inv :=
begin
PRODUCT_CAT, -- noting
rw ← yoneda_sugar_prod,
rw assoc,
rw yoneda_sugar.prod.hom_inv,
exact rfl,
end
lemma yoneda_sugar.otimes (R : C){Y Z K :C}(f : X ⟶ Y )(g : Z ⟶ K) :
( R < (f ⊗ g) > ) = (yoneda_sugar.prod _ _ _).hom ≫ ((R<f>) ⊗ R<g>) ≫ (yoneda_sugar.prod _ _ _ ).inv := begin
PRODUCT_CAT,
-- iterate 2 {rw prod.otimes_is_prod},
-- rw yoneda_sugar.prod.hom,
-- iterate 1 {rw yoneda_sugar_prod_inv},
rw ← assoc,
rw prod.left_composition,
rw ← assoc,
rw prod.lift_fst,
rw ← assoc,
rw prod.lift_snd,
-- rw yoneda_sugar.composition,
-- rw yoneda_sugar.composition,
end
@[PRODUCT]lemma yonega_sugar.one_otimes (R :C)(X Y Z: C) (f : X ⟶ Y) :
(((yoneda_sugar.prod R Z X).inv) ≫ (R <(𝟙 Z ⊗ f ) > ) ≫ (yoneda_sugar.prod R Z Y).hom) = (𝟙 (R[Z]) ⊗ R < f >) := begin
rw yoneda_sugar.otimes,
iterate 3 {rw ← assoc},
rw yoneda_sugar.prod.inv_hom,
rw id_comp,
rw assoc,
rw yoneda_sugar.prod.inv_hom,
rw ← yoneda_sugar.id,
simp,
end
lemma yonega_sugar.one_otimes' (R :C)(X Y Z: C) (f : X ⟶ Y) :
( (R <(𝟙 Z ⊗ f ) > ) ≫ (yoneda_sugar.prod R Z Y).hom) = ((yoneda_sugar.prod R Z X).hom) ≫ (𝟙 (R[Z]) ⊗ R < f >) := begin
iterate 2{ rw yoneda_sugar.prod.hom},
rw prod.left_composition,
iterate 2{ rw ← yoneda_sugar.composition},
rw prod.map_first,
rw prod.map_second,
rw comp_id,
rw prod.otimes_is_prod,rw prod.left_composition,rw ← assoc,
rw prod.lift_fst,rw ← assoc,rw prod.lift_snd,rw comp_id,
rw yoneda_sugar.composition,
end
|
d7e3bdfa0209b19d03ea85bb59b49de2719dd8ef | bb31430994044506fa42fd667e2d556327e18dfe | /src/data/polynomial/basic.lean | 8b825846cbcb220245265da093899b5459539116 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 33,804 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import algebra.monoid_algebra.basic
/-!
# Theory of univariate polynomials
This file defines `polynomial R`, the type of univariate polynomials over the semiring `R`, builds
a semiring structure on it, and gives basic definitions that are expanded in other files in this
directory.
## Main definitions
* `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map.
* `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism.
* `X` is the polynomial `X`, i.e., `monomial 1 1`.
* `p.sum f` is `∑ n in p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied
to coefficients of the polynomial `p`.
* `p.erase n` is the polynomial `p` in which one removes the `c X^n` term.
There are often two natural variants of lemmas involving sums, depending on whether one acts on the
polynomials, or on the function. The naming convention is that one adds `index` when acting on
the polynomials. For instance,
* `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`;
* `sum_add` states that `p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g`.
* Notation to refer to `polynomial R`, as `R[X]` or `R[t]`.
## Implementation
Polynomials are defined using `add_monoid_algebra R ℕ`, where `R` is a semiring.
The variable `X` commutes with every polynomial `p`: lemma `X_mul` proves the identity
`X * p = p * X`. The relationship to `add_monoid_algebra R ℕ` is through a structure
to make polynomials irreducible from the point of view of the kernel. Most operations
are irreducible since Lean can not compute anyway with `add_monoid_algebra`. There are two
exceptions that we make semireducible:
* The zero polynomial, so that its coefficients are definitionally equal to `0`.
* The scalar action, to permit typeclass search to unfold it to resolve potential instance
diamonds.
The raw implementation of the equivalence between `R[X]` and `add_monoid_algebra R ℕ` is
done through `of_finsupp` and `to_finsupp` (or, equivalently, `rcases p` when `p` is a polynomial
gives an element `q` of `add_monoid_algebra R ℕ`, and conversely `⟨q⟩` gives back `p`). The
equivalence is also registered as a ring equiv in `polynomial.to_finsupp_iso`. These should
in general not be used once the basic API for polynomials is constructed.
-/
noncomputable theory
/-- `polynomial R` is the type of univariate polynomials over `R`.
Polynomials should be seen as (semi-)rings with the additional constructor `X`.
The embedding from `R` is called `C`. -/
structure polynomial (R : Type*) [semiring R] := of_finsupp ::
(to_finsupp : add_monoid_algebra R ℕ)
localized "notation (name := polynomial) R`[X]`:9000 := polynomial R" in polynomial
open add_monoid_algebra finsupp function
open_locale big_operators polynomial
namespace polynomial
universes u
variables {R : Type u} {a b : R} {m n : ℕ}
section semiring
variables [semiring R] {p q : R[X]}
lemma forall_iff_forall_finsupp (P : R[X] → Prop) :
(∀ p, P p) ↔ ∀ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ h q, h ⟨q⟩, λ h ⟨p⟩, h p⟩
lemma exists_iff_exists_finsupp (P : R[X] → Prop) :
(∃ p, P p) ↔ ∃ (q : add_monoid_algebra R ℕ), P ⟨q⟩ :=
⟨λ ⟨⟨p⟩, hp⟩, ⟨p, hp⟩, λ ⟨q, hq⟩, ⟨⟨q⟩, hq⟩ ⟩
@[simp] lemma eta (f : R[X]) : polynomial.of_finsupp f.to_finsupp = f := by cases f; refl
/-! ### Conversions to and from `add_monoid_algebra`
Since `R[X]` is not defeq to `add_monoid_algebra R ℕ`, but instead is a structure wrapping
it, we have to copy across all the arithmetic operators manually, along with the lemmas about how
they unfold around `polynomial.of_finsupp` and `polynomial.to_finsupp`.
-/
section add_monoid_algebra
@[irreducible] private def add : R[X] → R[X] → R[X]
| ⟨a⟩ ⟨b⟩ := ⟨a + b⟩
@[irreducible] private def neg {R : Type u} [ring R] : R[X] → R[X]
| ⟨a⟩ := ⟨-a⟩
@[irreducible] private def mul : R[X] → R[X] → R[X]
| ⟨a⟩ ⟨b⟩ := ⟨a * b⟩
instance : has_zero R[X] := ⟨⟨0⟩⟩
instance : has_one R[X] := ⟨⟨1⟩⟩
instance : has_add R[X] := ⟨add⟩
instance {R : Type u} [ring R] : has_neg R[X] := ⟨neg⟩
instance {R : Type u} [ring R] : has_sub R[X] := ⟨λ a b, a + -b⟩
instance : has_mul R[X] := ⟨mul⟩
instance {S : Type*} [smul_zero_class S R] : smul_zero_class S R[X] :=
{ smul := λ r p, ⟨r • p.to_finsupp⟩,
smul_zero := λ a, congr_arg of_finsupp (smul_zero a) }
@[priority 1] -- to avoid a bug in the `ring` tactic
instance has_pow : has_pow R[X] ℕ := { pow := λ p n, npow_rec n p }
@[simp] lemma of_finsupp_zero : (⟨0⟩ : R[X]) = 0 := rfl
@[simp] lemma of_finsupp_one : (⟨1⟩ : R[X]) = 1 := rfl
@[simp] lemma of_finsupp_add {a b} : (⟨a + b⟩ : R[X]) = ⟨a⟩ + ⟨b⟩ := show _ = add _ _, by rw add
@[simp] lemma of_finsupp_neg {R : Type u} [ring R] {a} : (⟨-a⟩ : R[X]) = -⟨a⟩ :=
show _ = neg _, by rw neg
@[simp] lemma of_finsupp_sub {R : Type u} [ring R] {a b} : (⟨a - b⟩ : R[X]) = ⟨a⟩ - ⟨b⟩ :=
by { rw [sub_eq_add_neg, of_finsupp_add, of_finsupp_neg], refl }
@[simp] lemma of_finsupp_mul (a b) : (⟨a * b⟩ : R[X]) = ⟨a⟩ * ⟨b⟩ := show _ = mul _ _, by rw mul
@[simp] lemma of_finsupp_smul {S : Type*} [smul_zero_class S R] (a : S) (b) :
(⟨a • b⟩ : R[X]) = (a • ⟨b⟩ : R[X]) := rfl
@[simp] lemma of_finsupp_pow (a) (n : ℕ) : (⟨a ^ n⟩ : R[X]) = ⟨a⟩ ^ n :=
begin
change _ = npow_rec n _,
induction n,
{ simp [npow_rec], } ,
{ simp [npow_rec, n_ih, pow_succ] }
end
@[simp] lemma to_finsupp_zero : (0 : R[X]).to_finsupp = 0 :=
rfl
@[simp] lemma to_finsupp_one : (1 : R[X]).to_finsupp = 1 := rfl
@[simp] lemma to_finsupp_add (a b : R[X]) : (a + b).to_finsupp = a.to_finsupp + b.to_finsupp :=
by { cases a, cases b, rw ←of_finsupp_add }
@[simp] lemma to_finsupp_neg {R : Type u} [ring R] (a : R[X]) : (-a).to_finsupp = -a.to_finsupp :=
by { cases a, rw ←of_finsupp_neg }
@[simp] lemma to_finsupp_sub {R : Type u} [ring R] (a b : R[X]) :
(a - b).to_finsupp = a.to_finsupp - b.to_finsupp :=
by { rw [sub_eq_add_neg, ←to_finsupp_neg, ←to_finsupp_add], refl }
@[simp] lemma to_finsupp_mul (a b : R[X]) : (a * b).to_finsupp = a.to_finsupp * b.to_finsupp :=
by { cases a, cases b, rw ←of_finsupp_mul }
@[simp] lemma to_finsupp_smul {S : Type*} [smul_zero_class S R] (a : S) (b : R[X]) :
(a • b).to_finsupp = a • b.to_finsupp := rfl
@[simp] lemma to_finsupp_pow (a : R[X]) (n : ℕ) : (a ^ n).to_finsupp = a.to_finsupp ^ n :=
by { cases a, rw ←of_finsupp_pow }
lemma _root_.is_smul_regular.polynomial {S : Type*} [monoid S] [distrib_mul_action S R] {a : S}
(ha : is_smul_regular R a) : is_smul_regular R[X] a
| ⟨x⟩ ⟨y⟩ h := congr_arg _ $ ha.finsupp (polynomial.of_finsupp.inj h)
lemma to_finsupp_injective : function.injective (to_finsupp : R[X] → add_monoid_algebra _ _) :=
λ ⟨x⟩ ⟨y⟩, congr_arg _
@[simp] lemma to_finsupp_inj {a b : R[X]} : a.to_finsupp = b.to_finsupp ↔ a = b :=
to_finsupp_injective.eq_iff
@[simp] lemma to_finsupp_eq_zero {a : R[X]} : a.to_finsupp = 0 ↔ a = 0 :=
by rw [←to_finsupp_zero, to_finsupp_inj]
@[simp] lemma to_finsupp_eq_one {a : R[X]} : a.to_finsupp = 1 ↔ a = 1 :=
by rw [←to_finsupp_one, to_finsupp_inj]
/-- A more convenient spelling of `polynomial.of_finsupp.inj_eq` in terms of `iff`. -/
lemma of_finsupp_inj {a b} : (⟨a⟩ : R[X]) = ⟨b⟩ ↔ a = b :=
iff_of_eq of_finsupp.inj_eq
@[simp] lemma of_finsupp_eq_zero {a} : (⟨a⟩ : R[X]) = 0 ↔ a = 0 :=
by rw [←of_finsupp_zero, of_finsupp_inj]
@[simp] lemma of_finsupp_eq_one {a} : (⟨a⟩ : R[X]) = 1 ↔ a = 1 :=
by rw [←of_finsupp_one, of_finsupp_inj]
instance : inhabited R[X] := ⟨0⟩
instance : has_nat_cast R[X] := ⟨λ n, polynomial.of_finsupp n⟩
instance : semiring R[X] :=
function.injective.semiring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul
(λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl)
instance {S} [monoid S] [distrib_mul_action S R] : distrib_mul_action S R[X] :=
function.injective.distrib_mul_action
⟨to_finsupp, to_finsupp_zero, to_finsupp_add⟩ to_finsupp_injective to_finsupp_smul
instance {S} [monoid S] [distrib_mul_action S R] [has_faithful_smul S R] :
has_faithful_smul S R[X] :=
{ eq_of_smul_eq_smul := λ s₁ s₂ h, eq_of_smul_eq_smul $ λ a : ℕ →₀ R, congr_arg to_finsupp (h ⟨a⟩) }
instance {S} [semiring S] [module S R] : module S R[X] :=
function.injective.module _
⟨to_finsupp, to_finsupp_zero, to_finsupp_add⟩ to_finsupp_injective to_finsupp_smul
instance {S₁ S₂} [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R] [distrib_mul_action S₂ R]
[smul_comm_class S₁ S₂ R] : smul_comm_class S₁ S₂ R[X] :=
⟨by { rintros _ _ ⟨⟩, simp_rw [←of_finsupp_smul, smul_comm] }⟩
instance {S₁ S₂} [has_smul S₁ S₂] [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R]
[distrib_mul_action S₂ R] [is_scalar_tower S₁ S₂ R] : is_scalar_tower S₁ S₂ R[X] :=
⟨by { rintros _ _ ⟨⟩, simp_rw [←of_finsupp_smul, smul_assoc] }⟩
instance is_scalar_tower_right {α K : Type*} [semiring K] [distrib_smul α K]
[is_scalar_tower α K K] : is_scalar_tower α K[X] K[X] :=
⟨by rintros _ ⟨⟩ ⟨⟩;
simp_rw [smul_eq_mul, ← of_finsupp_smul, ← of_finsupp_mul, ← of_finsupp_smul, smul_mul_assoc]⟩
instance {S} [monoid S] [distrib_mul_action S R] [distrib_mul_action Sᵐᵒᵖ R]
[is_central_scalar S R] : is_central_scalar S R[X] :=
⟨by { rintros _ ⟨⟩, simp_rw [←of_finsupp_smul, op_smul_eq_smul] }⟩
instance [subsingleton R] : unique R[X] :=
{ uniq := by { rintros ⟨x⟩, refine congr_arg of_finsupp _, simp },
.. polynomial.inhabited }
variable (R)
/-- Ring isomorphism between `R[X]` and `add_monoid_algebra R ℕ`. This is just an
implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/
@[simps apply symm_apply]
def to_finsupp_iso : R[X] ≃+* add_monoid_algebra R ℕ :=
{ to_fun := to_finsupp,
inv_fun := of_finsupp,
left_inv := λ ⟨p⟩, rfl,
right_inv := λ p, rfl,
map_mul' := to_finsupp_mul,
map_add' := to_finsupp_add }
end add_monoid_algebra
variable {R}
lemma of_finsupp_sum {ι : Type*} (s : finset ι) (f : ι → add_monoid_algebra R ℕ) :
(⟨∑ i in s, f i⟩ : R[X]) = ∑ i in s, ⟨f i⟩ :=
map_sum (to_finsupp_iso R).symm f s
lemma to_finsupp_sum {ι : Type*} (s : finset ι) (f : ι → R[X]) :
(∑ i in s, f i : R[X]).to_finsupp = ∑ i in s, (f i).to_finsupp :=
map_sum (to_finsupp_iso R) f s
/--
The set of all `n` such that `X^n` has a non-zero coefficient.
-/
@[simp]
def support : R[X] → finset ℕ
| ⟨p⟩ := p.support
@[simp] lemma support_of_finsupp (p) : support (⟨p⟩ : R[X]) = p.support :=
by rw support
@[simp] lemma support_zero : (0 : R[X]).support = ∅ :=
rfl
@[simp] lemma support_eq_empty : p.support = ∅ ↔ p = 0 :=
by { rcases p, simp [support] }
lemma card_support_eq_zero : p.support.card = 0 ↔ p = 0 :=
by simp
/-- `monomial s a` is the monomial `a * X^s` -/
def monomial (n : ℕ) : R →ₗ[R] R[X] :=
{ to_fun := λ t, ⟨finsupp.single n t⟩,
map_add' := by simp,
map_smul' := by simp [←of_finsupp_smul] }
@[simp] lemma to_finsupp_monomial (n : ℕ) (r : R) :
(monomial n r).to_finsupp = finsupp.single n r :=
by simp [monomial]
@[simp] lemma of_finsupp_single (n : ℕ) (r : R) :
(⟨finsupp.single n r⟩ : R[X]) = monomial n r :=
by simp [monomial]
@[simp]
lemma monomial_zero_right (n : ℕ) :
monomial n (0 : R) = 0 :=
(monomial n).map_zero
-- This is not a `simp` lemma as `monomial_zero_left` is more general.
lemma monomial_zero_one : monomial 0 (1 : R) = 1 := rfl
-- TODO: can't we just delete this one?
lemma monomial_add (n : ℕ) (r s : R) :
monomial n (r + s) = monomial n r + monomial n s :=
(monomial n).map_add _ _
lemma monomial_mul_monomial (n m : ℕ) (r s : R) :
monomial n r * monomial m s = monomial (n + m) (r * s) :=
to_finsupp_injective $
by simp only [to_finsupp_monomial, to_finsupp_mul, add_monoid_algebra.single_mul_single]
@[simp]
lemma monomial_pow (n : ℕ) (r : R) (k : ℕ) :
(monomial n r)^k = monomial (n*k) (r^k) :=
begin
induction k with k ih,
{ simp [pow_zero, monomial_zero_one], },
{ simp [pow_succ, ih, monomial_mul_monomial, nat.succ_eq_add_one, mul_add, add_comm] },
end
lemma smul_monomial {S} [monoid S] [distrib_mul_action S R] (a : S) (n : ℕ) (b : R) :
a • monomial n b = monomial n (a • b) :=
to_finsupp_injective $ by simp
lemma monomial_injective (n : ℕ) :
function.injective (monomial n : R → R[X]) :=
(to_finsupp_iso R).symm.injective.comp (single_injective n)
@[simp] lemma monomial_eq_zero_iff (t : R) (n : ℕ) :
monomial n t = 0 ↔ t = 0 :=
linear_map.map_eq_zero_iff _ (polynomial.monomial_injective n)
lemma support_add : (p + q).support ⊆ p.support ∪ q.support :=
begin
rcases p, rcases q,
simp only [←of_finsupp_add, support],
exact support_add
end
/--
`C a` is the constant polynomial `a`.
`C` is provided as a ring homomorphism.
-/
def C : R →+* R[X] :=
{ map_one' := by simp [monomial_zero_one],
map_mul' := by simp [monomial_mul_monomial],
map_zero' := by simp,
.. monomial 0 }
@[simp] lemma monomial_zero_left (a : R) : monomial 0 a = C a := rfl
@[simp] lemma to_finsupp_C (a : R) : (C a).to_finsupp = single 0 a := rfl
lemma C_0 : C (0 : R) = 0 := by simp
lemma C_1 : C (1 : R) = 1 := rfl
lemma C_mul : C (a * b) = C a * C b := C.map_mul a b
lemma C_add : C (a + b) = C a + C b := C.map_add a b
@[simp] lemma smul_C {S} [monoid S] [distrib_mul_action S R] (s : S) (r : R) :
s • C r = C (s • r) :=
smul_monomial _ _ r
@[simp] lemma C_bit0 : C (bit0 a) = bit0 (C a) := C_add
@[simp] lemma C_bit1 : C (bit1 a) = bit1 (C a) := by simp [bit1, C_bit0]
lemma C_pow : C (a ^ n) = C a ^ n := C.map_pow a n
@[simp]
lemma C_eq_nat_cast (n : ℕ) : C (n : R) = (n : R[X]) :=
map_nat_cast C n
@[simp] lemma C_mul_monomial : C a * monomial n b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, zero_add]
@[simp] lemma monomial_mul_C : monomial n a * C b = monomial n (a * b) :=
by simp only [←monomial_zero_left, monomial_mul_monomial, add_zero]
/-- `X` is the polynomial variable (aka indeterminate). -/
def X : R[X] := monomial 1 1
lemma monomial_one_one_eq_X : monomial 1 (1 : R) = X := rfl
lemma monomial_one_right_eq_X_pow (n : ℕ) : monomial n (1 : R) = X^n :=
begin
induction n with n ih,
{ simp [monomial_zero_one], },
{ rw [pow_succ, ←ih, ←monomial_one_one_eq_X, monomial_mul_monomial, add_comm, one_mul], }
end
@[simp] lemma to_finsupp_X : X.to_finsupp = finsupp.single 1 (1 : R) := rfl
/-- `X` commutes with everything, even when the coefficients are noncommutative. -/
lemma X_mul : X * p = p * X :=
begin
rcases p,
simp only [X, ←of_finsupp_single, ←of_finsupp_mul, linear_map.coe_mk],
ext,
simp [add_monoid_algebra.mul_apply, sum_single_index, add_comm],
end
lemma X_pow_mul {n : ℕ} : X^n * p = p * X^n :=
begin
induction n with n ih,
{ simp, },
{ conv_lhs { rw pow_succ', },
rw [mul_assoc, X_mul, ←mul_assoc, ih, mul_assoc, ←pow_succ'], }
end
/-- Prefer putting constants to the left of `X`.
This lemma is the loop-avoiding `simp` version of `polynomial.X_mul`. -/
@[simp] lemma X_mul_C (r : R) : X * C r = C r * X :=
X_mul
/-- Prefer putting constants to the left of `X ^ n`.
This lemma is the loop-avoiding `simp` version of `X_pow_mul`. -/
@[simp] lemma X_pow_mul_C (r : R) (n : ℕ) : X^n * C r = C r * X^n :=
X_pow_mul
lemma X_pow_mul_assoc {n : ℕ} : (p * X^n) * q = (p * q) * X^n :=
by rw [mul_assoc, X_pow_mul, ←mul_assoc]
/-- Prefer putting constants to the left of `X ^ n`.
This lemma is the loop-avoiding `simp` version of `X_pow_mul_assoc`. -/
@[simp] lemma X_pow_mul_assoc_C {n : ℕ} (r : R) : (p * X^n) * C r = p * C r * X^n :=
X_pow_mul_assoc
lemma commute_X (p : R[X]) : commute X p := X_mul
lemma commute_X_pow (p : R[X]) (n : ℕ) : commute (X ^ n) p := X_pow_mul
@[simp]
lemma monomial_mul_X (n : ℕ) (r : R) : monomial n r * X = monomial (n+1) r :=
by erw [monomial_mul_monomial, mul_one]
@[simp]
lemma monomial_mul_X_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r * X^k = monomial (n+k) r :=
begin
induction k with k ih,
{ simp, },
{ simp [ih, pow_succ', ←mul_assoc, add_assoc], },
end
@[simp]
lemma X_mul_monomial (n : ℕ) (r : R) : X * monomial n r = monomial (n+1) r :=
by rw [X_mul, monomial_mul_X]
@[simp]
lemma X_pow_mul_monomial (k n : ℕ) (r : R) : X^k * monomial n r = monomial (n+k) r :=
by rw [X_pow_mul, monomial_mul_X_pow]
/-- `coeff p n` (often denoted `p.coeff n`) is the coefficient of `X^n` in `p`. -/
@[simp] def coeff : R[X] → ℕ → R
| ⟨p⟩ := p
lemma coeff_injective : injective (coeff : R[X] → ℕ → R) :=
by { rintro ⟨p⟩ ⟨q⟩, simp only [coeff, fun_like.coe_fn_eq, imp_self] }
@[simp] lemma coeff_inj : p.coeff = q.coeff ↔ p = q := coeff_injective.eq_iff
lemma to_finsupp_apply (f : R[X]) (i) : f.to_finsupp i = f.coeff i := by cases f; refl
lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 :=
by { simp only [←of_finsupp_single, coeff, linear_map.coe_mk], rw finsupp.single_apply }
@[simp] lemma coeff_zero (n : ℕ) : coeff (0 : R[X]) n = 0 := rfl
@[simp] lemma coeff_one_zero : coeff (1 : R[X]) 0 = 1 :=
by { rw [← monomial_zero_one, coeff_monomial], simp }
@[simp] lemma coeff_X_one : coeff (X : R[X]) 1 = 1 := coeff_monomial
@[simp] lemma coeff_X_zero : coeff (X : R[X]) 0 = 0 := coeff_monomial
@[simp] lemma coeff_monomial_succ : coeff (monomial (n+1) a) 0 = 0 :=
by simp [coeff_monomial]
lemma coeff_X : coeff (X : R[X]) n = if 1 = n then 1 else 0 := coeff_monomial
lemma coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : R[X]) n = 0 :=
by rw [coeff_X, if_neg hn.symm]
@[simp] lemma mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 :=
by { rcases p, simp }
lemma not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 :=
by simp
lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 :=
by { convert coeff_monomial using 2, simp [eq_comm], }
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_monomial
lemma coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 :=
by rw [coeff_C, if_neg h]
lemma C_mul_X_pow_eq_monomial : ∀ {n : ℕ}, C a * X ^ n = monomial n a
| 0 := mul_one _
| (n+1) := by rw [pow_succ', ←mul_assoc, C_mul_X_pow_eq_monomial, X, monomial_mul_monomial, mul_one]
@[simp] lemma to_finsupp_C_mul_X_pow (a : R) (n : ℕ) :
(C a * X ^ n).to_finsupp = finsupp.single n a :=
by rw [C_mul_X_pow_eq_monomial, to_finsupp_monomial]
lemma C_mul_X_eq_monomial : C a * X = monomial 1 a := by rw [← C_mul_X_pow_eq_monomial, pow_one]
@[simp] lemma to_finsupp_C_mul_X (a : R) : (C a * X).to_finsupp = finsupp.single 1 a :=
by rw [C_mul_X_eq_monomial, to_finsupp_monomial]
lemma C_injective : injective (C : R → R[X]) := monomial_injective 0
@[simp] lemma C_inj : C a = C b ↔ a = b := C_injective.eq_iff
@[simp] lemma C_eq_zero : C a = 0 ↔ a = 0 := C_injective.eq_iff' (map_zero C)
lemma C_ne_zero : C a ≠ 0 ↔ a ≠ 0 := C_eq_zero.not
lemma subsingleton_iff_subsingleton :
subsingleton R[X] ↔ subsingleton R :=
⟨@injective.subsingleton _ _ _ C_injective, by { introI, apply_instance } ⟩
theorem nontrivial.of_polynomial_ne (h : p ≠ q) : nontrivial R :=
(subsingleton_or_nontrivial R).resolve_left $ λ hI, h $ by exactI subsingleton.elim _ _
lemma forall_eq_iff_forall_eq :
(∀ f g : R[X], f = g) ↔ (∀ a b : R, a = b) :=
by simpa only [← subsingleton_iff] using subsingleton_iff_subsingleton
theorem ext_iff {p q : R[X]} : p = q ↔ ∀ n, coeff p n = coeff q n :=
by { rcases p, rcases q, simp [coeff, finsupp.ext_iff] }
@[ext] lemma ext {p q : R[X]} : (∀ n, coeff p n = coeff q n) → p = q :=
ext_iff.2
/-- Monomials generate the additive monoid of polynomials. -/
lemma add_submonoid_closure_set_of_eq_monomial :
add_submonoid.closure {p : R[X] | ∃ n a, p = monomial n a} = ⊤ :=
begin
apply top_unique,
rw [← add_submonoid.map_equiv_top (to_finsupp_iso R).symm.to_add_equiv,
← finsupp.add_closure_set_of_eq_single, add_monoid_hom.map_mclosure],
refine add_submonoid.closure_mono (set.image_subset_iff.2 _),
rintro _ ⟨n, a, rfl⟩,
exact ⟨n, a, polynomial.of_finsupp_single _ _⟩,
end
lemma add_hom_ext {M : Type*} [add_monoid M] {f g : R[X] →+ M}
(h : ∀ n a, f (monomial n a) = g (monomial n a)) :
f = g :=
add_monoid_hom.eq_of_eq_on_mdense add_submonoid_closure_set_of_eq_monomial $
by { rintro p ⟨n, a, rfl⟩, exact h n a }
@[ext] lemma add_hom_ext' {M : Type*} [add_monoid M] {f g : R[X] →+ M}
(h : ∀ n, f.comp (monomial n).to_add_monoid_hom = g.comp (monomial n).to_add_monoid_hom) :
f = g :=
add_hom_ext (λ n, add_monoid_hom.congr_fun (h n))
@[ext] lemma lhom_ext' {M : Type*} [add_comm_monoid M] [module R M] {f g : R[X] →ₗ[R] M}
(h : ∀ n, f.comp (monomial n) = g.comp (monomial n)) :
f = g :=
linear_map.to_add_monoid_hom_injective $ add_hom_ext $ λ n, linear_map.congr_fun (h n)
-- this has the same content as the subsingleton
lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : R[X]) : p = 0 :=
by rw [←one_smul R p, ←h, zero_smul]
section fewnomials
lemma support_monomial (n) {a : R} (H : a ≠ 0) : (monomial n a).support = singleton n :=
by rw [←of_finsupp_single, support, finsupp.support_single_ne_zero _ H]
lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n :=
by { rw [←of_finsupp_single, support], exact finsupp.support_single_subset }
lemma support_C_mul_X {c : R} (h : c ≠ 0) : (C c * X).support = singleton 1 :=
by rw [C_mul_X_eq_monomial, support_monomial 1 h]
lemma support_C_mul_X' (c : R) : (C c * X).support ⊆ singleton 1 :=
by simpa only [C_mul_X_eq_monomial] using support_monomial' 1 c
lemma support_C_mul_X_pow (n : ℕ) {c : R} (h : c ≠ 0) : (C c * X ^ n).support = singleton n :=
by rw [C_mul_X_pow_eq_monomial, support_monomial n h]
lemma support_C_mul_X_pow' (n : ℕ) (c : R) : (C c * X ^ n).support ⊆ singleton n :=
by simpa only [C_mul_X_pow_eq_monomial] using support_monomial' n c
open finset
lemma support_binomial' (k m : ℕ) (x y : R) : (C x * X ^ k + C y * X ^ m).support ⊆ {k, m} :=
support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans
(singleton_subset_iff.mpr (mem_insert_self k {m}))) ((support_C_mul_X_pow' m y).trans
(singleton_subset_iff.mpr (mem_insert_of_mem (mem_singleton_self m)))))
lemma support_trinomial' (k m n : ℕ) (x y z : R) :
(C x * X ^ k + C y * X ^ m + C z * X ^ n).support ⊆ {k, m, n} :=
support_add.trans (union_subset (support_add.trans (union_subset ((support_C_mul_X_pow' k x).trans
(singleton_subset_iff.mpr (mem_insert_self k {m, n}))) ((support_C_mul_X_pow' m y).trans
(singleton_subset_iff.mpr (mem_insert_of_mem (mem_insert_self m {n}))))))
((support_C_mul_X_pow' n z).trans (singleton_subset_iff.mpr
(mem_insert_of_mem (mem_insert_of_mem (mem_singleton_self n))))))
end fewnomials
lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1 : R) :=
begin
induction n with n hn,
{ rw [pow_zero, monomial_zero_one] },
{ rw [pow_succ', hn, X, monomial_mul_monomial, one_mul] },
end
@[simp] lemma to_finsupp_X_pow (n : ℕ) : (X ^ n).to_finsupp = finsupp.single n (1 : R) :=
by rw [X_pow_eq_monomial, to_finsupp_monomial]
lemma smul_X_eq_monomial {n} : a • X ^ n = monomial n (a : R) :=
by rw [X_pow_eq_monomial, smul_monomial, smul_eq_mul, mul_one]
lemma support_X_pow (H : ¬(1 : R) = 0) (n : ℕ) : (X ^ n : R[X]).support = singleton n :=
begin
convert support_monomial n H,
exact X_pow_eq_monomial n,
end
lemma support_X_empty (H : (1 : R) = 0) : (X : R[X]).support = ∅ :=
by rw [X, H, monomial_zero_right, support_zero]
lemma support_X (H : ¬(1 : R) = 0) : (X : R[X]).support = singleton 1 :=
by rw [← pow_one X, support_X_pow H 1]
lemma monomial_left_inj {a : R} (ha : a ≠ 0) {i j : ℕ} : (monomial i a) = (monomial j a) ↔ i = j :=
by simp_rw [←of_finsupp_single, finsupp.single_left_inj ha]
lemma binomial_eq_binomial {k l m n : ℕ} {u v : R} (hu : u ≠ 0) (hv : v ≠ 0) :
C u * X ^ k + C v * X ^ l = C u * X ^ m + C v * X ^ n ↔
(k = m ∧ l = n) ∨ (u = v ∧ k = n ∧ l = m) ∨ (u + v = 0 ∧ k = l ∧ m = n) :=
begin
simp_rw [C_mul_X_pow_eq_monomial, ←to_finsupp_inj, to_finsupp_add, to_finsupp_monomial],
exact finsupp.single_add_single_eq_single_add_single hu hv,
end
lemma nat_cast_mul (n : ℕ) (p : R[X]) : (n : R[X]) * p = n • p :=
(nsmul_eq_mul _ _).symm
/-- Summing the values of a function applied to the coefficients of a polynomial -/
def sum {S : Type*} [add_comm_monoid S] (p : R[X]) (f : ℕ → R → S) : S :=
∑ n in p.support, f n (p.coeff n)
lemma sum_def {S : Type*} [add_comm_monoid S] (p : R[X]) (f : ℕ → R → S) :
p.sum f = ∑ n in p.support, f n (p.coeff n) := rfl
lemma sum_eq_of_subset {S : Type*} [add_comm_monoid S] (p : R[X])
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (s : finset ℕ) (hs : p.support ⊆ s) :
p.sum f = ∑ n in s, f n (p.coeff n) :=
begin
apply finset.sum_subset hs (λ n hn h'n, _),
rw not_mem_support_iff at h'n,
simp [h'n, hf]
end
/-- Expressing the product of two polynomials as a double sum. -/
lemma mul_eq_sum_sum :
p * q = ∑ i in p.support, q.sum (λ j a, (monomial (i + j)) (p.coeff i * a)) :=
begin
apply to_finsupp_injective,
rcases p, rcases q,
simp [support, sum, coeff, to_finsupp_sum],
refl
end
@[simp] lemma sum_zero_index {S : Type*} [add_comm_monoid S] (f : ℕ → R → S) :
(0 : R[X]).sum f = 0 :=
by simp [sum]
@[simp] lemma sum_monomial_index {S : Type*} [add_comm_monoid S]
(n : ℕ) (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) :
(monomial n a : R[X]).sum f = f n a :=
begin
by_cases h : a = 0,
{ simp [h, hf] },
{ simp [sum, support_monomial, h, coeff_monomial] }
end
@[simp] lemma sum_C_index {a} {β} [add_comm_monoid β] {f : ℕ → R → β} (h : f 0 0 = 0) :
(C a).sum f = f 0 a :=
sum_monomial_index 0 a f h
-- the assumption `hf` is only necessary when the ring is trivial
@[simp] lemma sum_X_index {S : Type*} [add_comm_monoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) :
(X : R[X]).sum f = f 1 1 :=
sum_monomial_index 1 1 f hf
lemma sum_add_index {S : Type*} [add_comm_monoid S] (p q : R[X])
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) :
(p + q).sum f = p.sum f + q.sum f :=
begin
rcases p, rcases q,
simp only [←of_finsupp_add, sum, support, coeff, pi.add_apply, coe_add],
exact finsupp.sum_add_index' hf h_add,
end
lemma sum_add' {S : Type*} [add_comm_monoid S] (p : R[X]) (f g : ℕ → R → S) :
p.sum (f + g) = p.sum f + p.sum g :=
by simp [sum_def, finset.sum_add_distrib]
lemma sum_add {S : Type*} [add_comm_monoid S] (p : R[X]) (f g : ℕ → R → S) :
p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g :=
sum_add' _ _ _
lemma sum_smul_index {S : Type*} [add_comm_monoid S] (p : R[X]) (b : R)
(f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum (λ n a, f n (b * a)) :=
begin
rcases p,
simpa [sum, support, coeff] using finsupp.sum_smul_index hf,
end
lemma sum_monomial_eq : ∀ p : R[X], p.sum (λ n a, monomial n a) = p
| ⟨p⟩ := (of_finsupp_sum _ _).symm.trans (congr_arg _ $ finsupp.sum_single _)
lemma sum_C_mul_X_pow_eq (p : R[X]) : p.sum (λ n a, C a * X ^ n) = p :=
by simp_rw [C_mul_X_pow_eq_monomial, sum_monomial_eq]
/-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/
@[irreducible] definition erase (n : ℕ) : R[X] → R[X]
| ⟨p⟩ := ⟨p.erase n⟩
@[simp] lemma to_finsupp_erase (p : R[X]) (n : ℕ) :
to_finsupp (p.erase n) = (p.to_finsupp).erase n :=
by { rcases p, simp only [erase] }
@[simp] lemma of_finsupp_erase (p : add_monoid_algebra R ℕ) (n : ℕ) :
(⟨p.erase n⟩ : R[X]) = (⟨p⟩ : R[X]).erase n :=
by { rcases p, simp only [erase] }
@[simp] lemma support_erase (p : R[X]) (n : ℕ) :
support (p.erase n) = (support p).erase n :=
by { rcases p, simp only [support, erase, support_erase] }
lemma monomial_add_erase (p : R[X]) (n : ℕ) : monomial n (coeff p n) + p.erase n = p :=
to_finsupp_injective $ begin
rcases p,
rw [to_finsupp_add, to_finsupp_monomial, to_finsupp_erase, coeff],
exact finsupp.single_add_erase _ _,
end
lemma coeff_erase (p : R[X]) (n i : ℕ) :
(p.erase n).coeff i = if i = n then 0 else p.coeff i :=
begin
rcases p,
simp only [erase, coeff],
convert rfl
end
@[simp] lemma erase_zero (n : ℕ) : (0 : R[X]).erase n = 0 :=
to_finsupp_injective $ by simp
@[simp] lemma erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 :=
to_finsupp_injective $ by simp
@[simp] lemma erase_same (p : R[X]) (n : ℕ) : coeff (p.erase n) n = 0 :=
by simp [coeff_erase]
@[simp] lemma erase_ne (p : R[X]) (n i : ℕ) (h : i ≠ n) :
coeff (p.erase n) i = coeff p i :=
by simp [coeff_erase, h]
section update
/-- Replace the coefficient of a `p : R[X]` at a given degree `n : ℕ`
by a given value `a : R`. If `a = 0`, this is equal to `p.erase n`
If `p.nat_degree < n` and `a ≠ 0`, this increases the degree to `n`. -/
def update (p : R[X]) (n : ℕ) (a : R) :
R[X] :=
polynomial.of_finsupp (p.to_finsupp.update n a)
lemma coeff_update (p : R[X]) (n : ℕ) (a : R) :
(p.update n a).coeff = function.update p.coeff n a :=
begin
ext,
cases p,
simp only [coeff, update, function.update_apply, coe_update],
end
lemma coeff_update_apply (p : R[X]) (n : ℕ) (a : R) (i : ℕ) :
(p.update n a).coeff i = if (i = n) then a else p.coeff i :=
by rw [coeff_update, function.update_apply]
@[simp] lemma coeff_update_same (p : R[X]) (n : ℕ) (a : R) :
(p.update n a).coeff n = a :=
by rw [p.coeff_update_apply, if_pos rfl]
lemma coeff_update_ne (p : R[X]) {n : ℕ} (a : R) {i : ℕ} (h : i ≠ n) :
(p.update n a).coeff i = p.coeff i :=
by rw [p.coeff_update_apply, if_neg h]
@[simp] lemma update_zero_eq_erase (p : R[X]) (n : ℕ) :
p.update n 0 = p.erase n :=
by { ext, rw [coeff_update_apply, coeff_erase] }
lemma support_update (p : R[X]) (n : ℕ) (a : R) [decidable (a = 0)] :
support (p.update n a) = if a = 0 then p.support.erase n else insert n p.support :=
by { cases p, simp only [support, update, support_update], congr }
lemma support_update_zero (p : R[X]) (n : ℕ) :
support (p.update n 0) = p.support.erase n :=
by rw [update_zero_eq_erase, support_erase]
lemma support_update_ne_zero (p : R[X]) (n : ℕ) {a : R} (ha : a ≠ 0) :
support (p.update n a) = insert n p.support :=
by classical; rw [support_update, if_neg ha]
end update
end semiring
section comm_semiring
variables [comm_semiring R]
instance : comm_semiring R[X] :=
function.injective.comm_semiring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul
(λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl)
end comm_semiring
section ring
variables [ring R]
instance : has_int_cast R[X] := ⟨λ n, of_finsupp n⟩
instance : ring R[X] :=
function.injective.ring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul to_finsupp_neg to_finsupp_sub
(λ _ _, to_finsupp_smul _ _) (λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl) (λ _, rfl)
@[simp] lemma coeff_neg (p : R[X]) (n : ℕ) : coeff (-p) n = -coeff p n :=
by { rcases p, rw [←of_finsupp_neg, coeff, coeff, finsupp.neg_apply] }
@[simp]
lemma coeff_sub (p q : R[X]) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n :=
by { rcases p, rcases q, rw [←of_finsupp_sub, coeff, coeff, coeff, finsupp.sub_apply] }
@[simp] lemma monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -(monomial n a) :=
by rw [eq_neg_iff_add_eq_zero, ←monomial_add, neg_add_self, monomial_zero_right]
@[simp] lemma support_neg {p : R[X]} : (-p).support = p.support :=
by { rcases p, rw [←of_finsupp_neg, support, support, finsupp.support_neg] }
@[simp] lemma C_eq_int_cast (n : ℤ) : C (n : R) = n := map_int_cast C n
end ring
instance [comm_ring R] : comm_ring R[X] :=
function.injective.comm_ring to_finsupp to_finsupp_injective
to_finsupp_zero to_finsupp_one to_finsupp_add to_finsupp_mul to_finsupp_neg to_finsupp_sub
(λ _ _, to_finsupp_smul _ _) (λ _ _, to_finsupp_smul _ _) to_finsupp_pow (λ _, rfl) (λ _, rfl)
section nonzero_semiring
variables [semiring R] [nontrivial R]
instance : nontrivial R[X] :=
begin
have h : nontrivial (add_monoid_algebra R ℕ) := by apply_instance,
rcases h.exists_pair_ne with ⟨x, y, hxy⟩,
refine ⟨⟨⟨x⟩, ⟨y⟩, _⟩⟩,
simp [hxy],
end
lemma X_ne_zero : (X : R[X]) ≠ 0 :=
mt (congr_arg (λ p, coeff p 1)) (by simp)
end nonzero_semiring
@[simp] lemma nontrivial_iff [semiring R] : nontrivial R[X] ↔ nontrivial R :=
⟨λ h, let ⟨r, s, hrs⟩ := @exists_pair_ne _ h in nontrivial.of_polynomial_ne hrs,
λ h, @polynomial.nontrivial _ _ h⟩
section repr
variables [semiring R]
open_locale classical
instance [has_repr R] : has_repr R[X] :=
⟨λ p, if p = 0 then "0"
else (p.support.sort (≤)).foldr
(λ n a, a ++ (if a = "" then "" else " + ") ++
if n = 0
then "C (" ++ repr (coeff p n) ++ ")"
else if n = 1
then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X"
else if (coeff p n) = 1 then "X ^ " ++ repr n
else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩
end repr
end polynomial
|
3350f6ca2528f7f7fad72efa2bd2059327ec2a1d | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/ring_theory/valuation/basic.lean | b4043074946f58a121d0ea33ab560562bc8abfa0 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 30,140 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Patrick Massot
-/
import algebra.order.with_zero
import algebra.punit_instances
import ring_theory.ideal.operations
/-!
# The basics of valuation theory.
The basic theory of valuations (non-archimedean norms) on a commutative ring,
following T. Wedhorn's unpublished notes “Adic Spaces” ([wedhorn_adic]).
The definition of a valuation we use here is Definition 1.22 of [wedhorn_adic].
A valuation on a ring `R` is a monoid homomorphism `v` to a linearly ordered
commutative monoid with zero, that in addition satisfies the following two axioms:
* `v 0 = 0`
* `∀ x y, v (x + y) ≤ max (v x) (v y)`
`valuation R Γ₀`is the type of valuations `R → Γ₀`, with a coercion to the underlying
function. If `v` is a valuation from `R` to `Γ₀` then the induced group
homomorphism `units(R) → Γ₀` is called `unit_map v`.
The equivalence "relation" `is_equiv v₁ v₂ : Prop` defined in 1.27 of [wedhorn_adic] is not strictly
speaking a relation, because `v₁ : valuation R Γ₁` and `v₂ : valuation R Γ₂` might
not have the same type. This corresponds in ZFC to the set-theoretic difficulty
that the class of all valuations (as `Γ₀` varies) on a ring `R` is not a set.
The "relation" is however reflexive, symmetric and transitive in the obvious
sense. Note that we use 1.27(iii) of [wedhorn_adic] as the definition of equivalence.
The support of a valuation `v : valuation R Γ₀` is `supp v`. If `J` is an ideal of `R`
with `h : J ⊆ supp v` then the induced valuation
on R / J = `ideal.quotient J` is `on_quot v h`.
## Main definitions
* `valuation R Γ₀`, the type of valuations on `R` with values in `Γ₀`
* `valuation.is_equiv`, the heterogeneous equivalence relation on valuations
* `valuation.supp`, the support of a valuation
* `add_valuation R Γ₀`, the type of additive valuations on `R` with values in a
linearly ordered additive commutative group with a top element, `Γ₀`.
## Implementation Details
`add_valuation R Γ₀` is implemented as `valuation R (multiplicative Γ₀)ᵒᵈ`.
## Notation
In the `discrete_valuation` locale:
* `ℕₘ₀` is a shorthand for `with_zero (multiplicative ℕ)`
* `ℤₘ₀` is a shorthand for `with_zero (multiplicative ℤ)`
## TODO
If ever someone extends `valuation`, we should fully comply to the `fun_like` by migrating the
boilerplate lemmas to `valuation_class`.
-/
open_locale classical big_operators
noncomputable theory
open function ideal
variables {K F R : Type*} [division_ring K]
section
variables (F R) (Γ₀ : Type*) [linear_ordered_comm_monoid_with_zero Γ₀] [ring R]
/-- The type of `Γ₀`-valued valuations on `R`.
When you extend this structure, make sure to extend `valuation_class`. -/
@[nolint has_nonempty_instance]
structure valuation extends R →*₀ Γ₀ :=
(map_add_le_max' : ∀ x y, to_fun (x + y) ≤ max (to_fun x) (to_fun y))
/-- `valuation_class F α β` states that `F` is a type of valuations.
You should also extend this typeclass when you extend `valuation`. -/
class valuation_class extends monoid_with_zero_hom_class F R Γ₀ :=
(map_add_le_max (f : F) (x y : R) : f (x + y) ≤ max (f x) (f y))
export valuation_class (map_add_le_max)
instance [valuation_class F R Γ₀] : has_coe_t F (valuation R Γ₀) :=
⟨λ f, { to_fun := f, map_one' := map_one f, map_zero' := map_zero f, map_mul' := map_mul f,
map_add_le_max' := map_add_le_max f }⟩
end
namespace valuation
variables {Γ₀ : Type*}
variables {Γ'₀ : Type*}
variables {Γ''₀ : Type*} [linear_ordered_comm_monoid_with_zero Γ''₀]
section basic
variables [ring R]
section monoid
variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀]
instance : valuation_class (valuation R Γ₀) R Γ₀ :=
{ coe := λ f, f.to_fun,
coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' },
map_mul := λ f, f.map_mul',
map_one := λ f, f.map_one',
map_zero := λ f, f.map_zero',
map_add_le_max := λ f, f.map_add_le_max' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (valuation R Γ₀) (λ _, R → Γ₀) := fun_like.has_coe_to_fun
@[simp] lemma to_fun_eq_coe (v : valuation R Γ₀) : v.to_fun = v := rfl
@[ext] lemma ext {v₁ v₂ : valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ := fun_like.ext _ _ h
variables (v : valuation R Γ₀) {x y z : R}
@[simp, norm_cast] lemma coe_coe : ⇑(v : R →*₀ Γ₀) = v := rfl
@[simp] lemma map_zero : v 0 = 0 := v.map_zero'
@[simp] lemma map_one : v 1 = 1 := v.map_one'
@[simp] lemma map_mul : ∀ x y, v (x * y) = v x * v y := v.map_mul'
@[simp] lemma map_add : ∀ x y, v (x + y) ≤ max (v x) (v y) := v.map_add_le_max'
lemma map_add_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x + y) ≤ g :=
le_trans (v.map_add x y) $ max_le hx hy
lemma map_add_lt {x y g} (hx : v x < g) (hy : v y < g) : v (x + y) < g :=
lt_of_le_of_lt (v.map_add x y) $ max_lt hx hy
lemma map_sum_le {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, v (f i) ≤ g) :
v (∑ i in s, f i) ≤ g :=
begin
refine finset.induction_on s
(λ _, trans_rel_right (≤) v.map_zero zero_le') (λ a s has ih hf, _) hf,
rw finset.forall_mem_insert at hf, rw finset.sum_insert has,
exact v.map_add_le hf.1 (ih hf.2)
end
lemma map_sum_lt {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ 0)
(hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g :=
begin
refine finset.induction_on s
(λ _, trans_rel_right (<) v.map_zero (zero_lt_iff.2 hg)) (λ a s has ih hf, _) hf,
rw finset.forall_mem_insert at hf, rw finset.sum_insert has,
exact v.map_add_lt hf.1 (ih hf.2)
end
lemma map_sum_lt' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : 0 < g)
(hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g :=
v.map_sum_lt (ne_of_gt hg) hf
@[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = (v x)^n :=
v.to_monoid_with_zero_hom.to_monoid_hom.map_pow
/-- Deprecated. Use `fun_like.ext_iff`. -/
lemma ext_iff {v₁ v₂ : valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r := fun_like.ext_iff
-- The following definition is not an instance, because we have more than one `v` on a given `R`.
-- In addition, type class inference would not be able to infer `v`.
/-- A valuation gives a preorder on the underlying ring. -/
def to_preorder : preorder R := preorder.lift v
/-- If `v` is a valuation on a division ring then `v(x) = 0` iff `x = 0`. -/
@[simp] lemma zero_iff [nontrivial Γ₀] (v : valuation K Γ₀) {x : K} :
v x = 0 ↔ x = 0 :=
v.to_monoid_with_zero_hom.map_eq_zero
lemma ne_zero_iff [nontrivial Γ₀] (v : valuation K Γ₀) {x : K} :
v x ≠ 0 ↔ x ≠ 0 :=
v.to_monoid_with_zero_hom.map_ne_zero
theorem unit_map_eq (u : Rˣ) :
(units.map (v : R →* Γ₀) u : Γ₀) = v u := rfl
/-- A ring homomorphism `S → R` induces a map `valuation R Γ₀ → valuation S Γ₀`. -/
def comap {S : Type*} [ring S] (f : S →+* R) (v : valuation R Γ₀) :
valuation S Γ₀ :=
{ to_fun := v ∘ f,
map_add_le_max' := λ x y, by simp only [comp_app, map_add, f.map_add],
.. v.to_monoid_with_zero_hom.comp f.to_monoid_with_zero_hom, }
@[simp]
lemma comap_apply {S : Type*} [ring S] (f : S →+* R) (v : valuation R Γ₀) (s : S) :
v.comap f s = v (f s) := rfl
@[simp] lemma comap_id : v.comap (ring_hom.id R) = v := ext $ λ r, rfl
lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) :
v.comap (g.comp f) = (v.comap g).comap f :=
ext $ λ r, rfl
/-- A `≤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map `valuation R Γ₀ → valuation R Γ'₀`.
-/
def map (f : Γ₀ →*₀ Γ'₀) (hf : monotone f) (v : valuation R Γ₀) :
valuation R Γ'₀ :=
{ to_fun := f ∘ v,
map_add_le_max' := λ r s,
calc f (v (r + s)) ≤ f (max (v r) (v s)) : hf (v.map_add r s)
... = max (f (v r)) (f (v s)) : hf.map_max,
.. monoid_with_zero_hom.comp f v.to_monoid_with_zero_hom }
/-- Two valuations on `R` are defined to be equivalent if they induce the same preorder on `R`. -/
def is_equiv (v₁ : valuation R Γ₀) (v₂ : valuation R Γ'₀) : Prop :=
∀ r s, v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s
end monoid
section group
variables [linear_ordered_comm_group_with_zero Γ₀] {R} {Γ₀} (v : valuation R Γ₀) {x y z : R}
@[simp] lemma map_neg (x : R) : v (-x) = v x :=
v.to_monoid_with_zero_hom.to_monoid_hom.map_neg x
lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) :=
v.to_monoid_with_zero_hom.to_monoid_hom.map_sub_swap x y
lemma map_sub (x y : R) : v (x - y) ≤ max (v x) (v y) :=
calc v (x - y) = v (x + -y) : by rw [sub_eq_add_neg]
... ≤ max (v x) (v $ -y) : v.map_add _ _
... = max (v x) (v y) : by rw map_neg
lemma map_sub_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x - y) ≤ g :=
begin
rw sub_eq_add_neg,
exact v.map_add_le hx (le_trans (le_of_eq (v.map_neg y)) hy)
end
lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = max (v x) (v y) :=
begin
suffices : ¬v (x + y) < max (v x) (v y),
from or_iff_not_imp_right.1 (le_iff_eq_or_lt.1 (v.map_add x y)) this,
intro h',
wlog vyx : v y < v x using x y,
{ apply lt_or_gt_of_ne h.symm },
{ rw max_eq_left_of_lt vyx at h',
apply lt_irrefl (v x),
calc v x = v ((x+y) - y) : by simp
... ≤ max (v $ x + y) (v y) : map_sub _ _ _
... < v x : max_lt h' vyx },
{ apply this h.symm,
rwa [add_comm, max_comm] at h' }
end
lemma map_add_eq_of_lt_right (h : v x < v y) : v (x + y) = v y :=
begin
convert v.map_add_of_distinct_val _,
{ symmetry, rw max_eq_right_iff, exact le_of_lt h },
{ exact ne_of_lt h }
end
lemma map_add_eq_of_lt_left (h : v y < v x) : v (x + y) = v x :=
begin
rw add_comm, exact map_add_eq_of_lt_right _ h,
end
lemma map_eq_of_sub_lt (h : v (y - x) < v x) : v y = v x :=
begin
have := valuation.map_add_of_distinct_val v (ne_of_gt h).symm,
rw max_eq_right (le_of_lt h) at this,
simpa using this
end
lemma map_one_add_of_lt (h : v x < 1) : v (1 + x) = 1 :=
begin
rw ← v.map_one at h,
simpa only [v.map_one] using v.map_add_eq_of_lt_left h
end
lemma map_one_sub_of_lt (h : v x < 1) : v (1 - x) = 1 :=
begin
rw [← v.map_one, ← v.map_neg] at h,
rw sub_eq_add_neg 1 x,
simpa only [v.map_one, v.map_neg] using v.map_add_eq_of_lt_left h
end
lemma one_lt_val_iff (v : valuation K Γ₀) {x : K} (h : x ≠ 0) :
1 < v x ↔ v x⁻¹ < 1 :=
by simpa using (inv_lt_inv₀ (v.ne_zero_iff.2 h) one_ne_zero).symm
/-- The subgroup of elements whose valuation is less than a certain unit.-/
def lt_add_subgroup (v : valuation R Γ₀) (γ : Γ₀ˣ) : add_subgroup R :=
{ carrier := {x | v x < γ},
zero_mem' := by { have h := units.ne_zero γ, contrapose! h, simpa using h },
add_mem' := λ x y x_in y_in, lt_of_le_of_lt (v.map_add x y) (max_lt x_in y_in),
neg_mem' := λ x x_in, by rwa [set.mem_set_of_eq, map_neg] }
end group
end basic -- end of section
namespace is_equiv
variables [ring R]
variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀]
variables {v : valuation R Γ₀}
variables {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} {v₃ : valuation R Γ''₀}
@[refl] lemma refl : v.is_equiv v :=
λ _ _, iff.refl _
@[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ :=
λ _ _, iff.symm (h _ _)
@[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ :=
λ _ _, iff.trans (h₁₂ _ _) (h₂₃ _ _)
lemma of_eq {v' : valuation R Γ₀} (h : v = v') : v.is_equiv v' :=
by { subst h }
lemma map {v' : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀) (hf : monotone f)
(inf : injective f) (h : v.is_equiv v') :
(v.map f hf).is_equiv (v'.map f hf) :=
let H : strict_mono f := hf.strict_mono_of_injective inf in
λ r s,
calc f (v r) ≤ f (v s) ↔ v r ≤ v s : by rw H.le_iff_le
... ↔ v' r ≤ v' s : h r s
... ↔ f (v' r) ≤ f (v' s) : by rw H.le_iff_le
/-- `comap` preserves equivalence. -/
lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) :
(v₁.comap f).is_equiv (v₂.comap f) :=
λ r s, h (f r) (f s)
lemma val_eq (h : v₁.is_equiv v₂) {r s : R} :
v₁ r = v₁ s ↔ v₂ r = v₂ s :=
by simpa only [le_antisymm_iff] using and_congr (h r s) (h s r)
lemma ne_zero (h : v₁.is_equiv v₂) {r : R} :
v₁ r ≠ 0 ↔ v₂ r ≠ 0 :=
begin
have : v₁ r ≠ v₁ 0 ↔ v₂ r ≠ v₂ 0 := not_iff_not_of_iff h.val_eq,
rwa [v₁.map_zero, v₂.map_zero] at this,
end
end is_equiv -- end of namespace
section
lemma is_equiv_of_map_strict_mono [linear_ordered_comm_monoid_with_zero Γ₀]
[linear_ordered_comm_monoid_with_zero Γ'₀] [ring R] {v : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀)
(H : strict_mono f) :
is_equiv (v.map f (H.monotone)) v :=
λ x y, ⟨H.le_iff_le.mp, λ h, H.monotone h⟩
lemma is_equiv_of_val_le_one [linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) (h : ∀ {x:K}, v x ≤ 1 ↔ v' x ≤ 1) :
v.is_equiv v' :=
begin
intros x y,
by_cases hy : y = 0, { simp [hy, zero_iff], },
rw show y = 1 * y, by rw one_mul,
rw [← (inv_mul_cancel_right₀ hy x)],
iterate 2 {rw [v.map_mul _ y, v'.map_mul _ y]},
rw [v.map_one, v'.map_one],
split; intro H,
{ apply mul_le_mul_right',
replace hy := v.ne_zero_iff.mpr hy,
replace H := le_of_le_mul_right hy H,
rwa h at H, },
{ apply mul_le_mul_right',
replace hy := v'.ne_zero_iff.mpr hy,
replace H := le_of_le_mul_right hy H,
rwa h, },
end
lemma is_equiv_iff_val_le_one
[linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) :
v.is_equiv v' ↔ ∀ {x : K}, v x ≤ 1 ↔ v' x ≤ 1 :=
⟨λ h x, by simpa using h x 1, is_equiv_of_val_le_one _ _⟩
lemma is_equiv_iff_val_eq_one
[linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) :
v.is_equiv v' ↔ ∀ {x : K}, v x = 1 ↔ v' x = 1 :=
begin
split,
{ intros h x,
simpa using @is_equiv.val_eq _ _ _ _ _ _ v v' h x 1 },
{ intros h, apply is_equiv_of_val_le_one, intros x,
split,
{ intros hx,
cases lt_or_eq_of_le hx with hx' hx',
{ have : v (1 + x) = 1,
{ rw ← v.map_one, apply map_add_eq_of_lt_left, simpa },
rw h at this,
rw (show x = (-1) + (1 + x), by simp),
refine le_trans (v'.map_add _ _) _,
simp [this] },
{ rw h at hx', exact le_of_eq hx' } },
{ intros hx,
cases lt_or_eq_of_le hx with hx' hx',
{ have : v' (1 + x) = 1,
{ rw ← v'.map_one, apply map_add_eq_of_lt_left, simpa },
rw ← h at this,
rw (show x = (-1) + (1 + x), by simp),
refine le_trans (v.map_add _ _) _,
simp [this] },
{ rw ← h at hx', exact le_of_eq hx' } } }
end
lemma is_equiv_iff_val_lt_one
[linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) :
v.is_equiv v' ↔ ∀ {x : K}, v x < 1 ↔ v' x < 1 :=
begin
split,
{ intros h x,
simp only [lt_iff_le_and_ne, and_congr ((is_equiv_iff_val_le_one _ _).1 h)
((is_equiv_iff_val_eq_one _ _).1 h).not] },
{ rw is_equiv_iff_val_eq_one,
intros h x,
by_cases hx : x = 0, { simp only [(zero_iff _).2 hx, zero_ne_one] },
split,
{ intro hh,
by_contra h_1,
cases ne_iff_lt_or_gt.1 h_1,
{ simpa [hh, lt_self_iff_false] using h.2 h_2 },
{ rw [← inv_one, eq_inv_iff_eq_inv, ← map_inv₀] at hh,
exact hh.le.not_lt (h.2 ((one_lt_val_iff v' hx).1 h_2)) } },
{ intro hh,
by_contra h_1,
cases ne_iff_lt_or_gt.1 h_1,
{ simpa [hh, lt_self_iff_false] using h.1 h_2 },
{ rw [← inv_one, eq_inv_iff_eq_inv, ← map_inv₀] at hh,
exact hh.le.not_lt (h.1 ((one_lt_val_iff v hx).1 h_2)) } } }
end
lemma is_equiv_iff_val_sub_one_lt_one
[linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) :
v.is_equiv v' ↔ ∀ {x : K}, v (x - 1) < 1 ↔ v' (x - 1) < 1 :=
begin
rw is_equiv_iff_val_lt_one,
exact (equiv.sub_right 1).surjective.forall
end
lemma is_equiv_tfae
[linear_ordered_comm_group_with_zero Γ₀]
[linear_ordered_comm_group_with_zero Γ'₀]
(v : valuation K Γ₀) (v' : valuation K Γ'₀) :
[v.is_equiv v',
∀ {x}, v x ≤ 1 ↔ v' x ≤ 1,
∀ {x}, v x = 1 ↔ v' x = 1,
∀ {x}, v x < 1 ↔ v' x < 1,
∀ {x}, v (x-1) < 1 ↔ v' (x-1) < 1].tfae :=
begin
tfae_have : 1 ↔ 2, { apply is_equiv_iff_val_le_one },
tfae_have : 1 ↔ 3, { apply is_equiv_iff_val_eq_one },
tfae_have : 1 ↔ 4, { apply is_equiv_iff_val_lt_one },
tfae_have : 1 ↔ 5, { apply is_equiv_iff_val_sub_one_lt_one },
tfae_finish
end
end
section supp
variables [comm_ring R]
variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀]
variables (v : valuation R Γ₀)
/-- The support of a valuation `v : R → Γ₀` is the ideal of `R` where `v` vanishes. -/
def supp : ideal R :=
{ carrier := {x | v x = 0},
zero_mem' := map_zero v,
add_mem' := λ x y hx hy, le_zero_iff.mp $
calc v (x + y) ≤ max (v x) (v y) : v.map_add x y
... ≤ 0 : max_le (le_zero_iff.mpr hx) (le_zero_iff.mpr hy),
smul_mem' := λ c x hx, calc v (c * x)
= v c * v x : map_mul v c x
... = v c * 0 : congr_arg _ hx
... = 0 : mul_zero _ }
@[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = 0 := iff.rfl
-- @[simp] lemma mem_supp_iff' (x : R) : x ∈ (supp v : set R) ↔ v x = 0 := iff.rfl
/-- The support of a valuation is a prime ideal. -/
instance [nontrivial Γ₀] [no_zero_divisors Γ₀] : ideal.is_prime (supp v) :=
⟨λ (h : v.supp = ⊤), one_ne_zero $ show (1 : Γ₀) = 0,
from calc 1 = v 1 : v.map_one.symm
... = 0 : show (1:R) ∈ supp v, by { rw h, trivial },
λ x y hxy, begin
show v x = 0 ∨ v y = 0,
change v (x * y) = 0 at hxy,
rw [v.map_mul x y] at hxy,
exact eq_zero_or_eq_zero_of_mul_eq_zero hxy
end⟩
lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a :=
begin
have aux : ∀ a s, v s = 0 → v (a + s) ≤ v a,
{ intros a' s' h', refine le_trans (v.map_add a' s') (max_le le_rfl _), simp [h'], },
apply le_antisymm (aux a s h),
calc v a = v (a + s + -s) : by simp
... ≤ v (a + s) : aux (a + s) (-s) (by rwa ←ideal.neg_mem_iff at h)
end
/-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function.
Note: it's just the function; the valuation is `on_quot hJ`. -/
def on_quot_val {J : ideal R} (hJ : J ≤ supp v) :
R ⧸ J → Γ₀ :=
λ q, quotient.lift_on' q v $ λ a b h,
calc v a = v (b + -(-a + b)) : by simp
... = v b :
v.map_add_supp b $ (ideal.neg_mem_iff _).2 $ hJ $ quotient_add_group.left_rel_apply.mp h
/-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/
def on_quot {J : ideal R} (hJ : J ≤ supp v) :
valuation (R ⧸ J) Γ₀ :=
{ to_fun := v.on_quot_val hJ,
map_zero' := v.map_zero,
map_one' := v.map_one,
map_mul' := λ xbar ybar, quotient.ind₂' v.map_mul xbar ybar,
map_add_le_max' := λ xbar ybar, quotient.ind₂' v.map_add xbar ybar }
@[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) :
(v.on_quot hJ).comap (ideal.quotient.mk J) = v :=
ext $ λ r, rfl
lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) :
supp (v.comap f) = ideal.comap f v.supp :=
ideal.ext $ λ x,
begin
rw [mem_supp_iff, ideal.mem_comap, mem_supp_iff],
refl,
end
lemma self_le_supp_comap (J : ideal R) (v : valuation (R ⧸ J) Γ₀) :
J ≤ (v.comap (ideal.quotient.mk J)).supp :=
by { rw [comap_supp, ← ideal.map_le_iff_le_comap], simp }
@[simp] lemma comap_on_quot_eq (J : ideal R) (v : valuation (R ⧸ J) Γ₀) :
(v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v :=
ext $ by { rintro ⟨x⟩, refl }
/-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/
lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) :
supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) :=
begin
apply le_antisymm,
{ rintro ⟨x⟩ hx,
apply ideal.subset_span,
exact ⟨x, hx, rfl⟩ },
{ rw ideal.map_le_iff_le_comap,
intros x hx, exact hx }
end
lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 :=
by { rw supp_quot, exact ideal.map_quotient_self _ }
end supp -- end of section
end valuation
section add_monoid
variables (R) [ring R] (Γ₀ : Type*) [linear_ordered_add_comm_monoid_with_top Γ₀]
/-- The type of `Γ₀`-valued additive valuations on `R`. -/
@[nolint has_nonempty_instance]
def add_valuation := valuation R (multiplicative Γ₀ᵒᵈ)
end add_monoid
namespace add_valuation
variables {Γ₀ : Type*} {Γ'₀ : Type*}
section basic
section monoid
variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀]
variables (R) (Γ₀) [ring R]
/-- A valuation is coerced to the underlying function `R → Γ₀`. -/
instance : has_coe_to_fun (add_valuation R Γ₀) (λ _, R → Γ₀) :=
{ coe := λ v, v.to_monoid_with_zero_hom.to_fun }
variables {R} {Γ₀} (v : add_valuation R Γ₀) {x y z : R}
section
variables (f : R → Γ₀) (h0 : f 0 = ⊤) (h1 : f 1 = 0)
variables (hadd : ∀ x y, min (f x) (f y) ≤ f (x + y)) (hmul : ∀ x y, f (x * y) = f x + f y)
/-- An alternate constructor of `add_valuation`, that doesn't reference `multiplicative Γ₀ᵒᵈ` -/
def of : add_valuation R Γ₀ :=
{ to_fun := f,
map_one' := h1,
map_zero' := h0,
map_add_le_max' := hadd,
map_mul' := hmul }
variables {h0} {h1} {hadd} {hmul} {r : R}
@[simp]
theorem of_apply : (of f h0 h1 hadd hmul) r = f r := rfl
/-- The `valuation` associated to an `add_valuation` (useful if the latter is constructed using
`add_valuation.of`). -/
def valuation : valuation R (multiplicative Γ₀ᵒᵈ) := v
@[simp] lemma valuation_apply (r : R) :
v.valuation r = multiplicative.of_add (order_dual.to_dual (v r)) := rfl
end
@[simp] lemma map_zero : v 0 = ⊤ := v.map_zero
@[simp] lemma map_one : v 1 = 0 := v.map_one
@[simp] lemma map_mul : ∀ x y, v (x * y) = v x + v y := v.map_mul
@[simp] lemma map_add : ∀ x y, min (v x) (v y) ≤ v (x + y) := v.map_add
lemma map_le_add {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x + y) := v.map_add_le hx hy
lemma map_lt_add {x y g} (hx : g < v x) (hy : g < v y) : g < v (x + y) := v.map_add_lt hx hy
lemma map_le_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, g ≤ v (f i)) :
g ≤ v (∑ i in s, f i) := v.map_sum_le hf
lemma map_lt_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ ⊤)
(hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt hg hf
lemma map_lt_sum' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g < ⊤)
(hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt' hg hf
@[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = n • (v x) := v.map_pow
@[ext] lemma ext {v₁ v₂ : add_valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ :=
valuation.ext h
lemma ext_iff {v₁ v₂ : add_valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r :=
valuation.ext_iff
-- The following definition is not an instance, because we have more than one `v` on a given `R`.
-- In addition, type class inference would not be able to infer `v`.
/-- A valuation gives a preorder on the underlying ring. -/
def to_preorder : preorder R := preorder.lift v
/-- If `v` is an additive valuation on a division ring then `v(x) = ⊤` iff `x = 0`. -/
@[simp] lemma top_iff [nontrivial Γ₀] (v : add_valuation K Γ₀) {x : K} :
v x = ⊤ ↔ x = 0 :=
v.zero_iff
lemma ne_top_iff [nontrivial Γ₀] (v : add_valuation K Γ₀) {x : K} :
v x ≠ ⊤ ↔ x ≠ 0 :=
v.ne_zero_iff
/-- A ring homomorphism `S → R` induces a map `add_valuation R Γ₀ → add_valuation S Γ₀`. -/
def comap {S : Type*} [ring S] (f : S →+* R) (v : add_valuation R Γ₀) :
add_valuation S Γ₀ :=
v.comap f
@[simp] lemma comap_id : v.comap (ring_hom.id R) = v := v.comap_id
lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) :
v.comap (g.comp f) = (v.comap g).comap f :=
v.comap_comp f g
/-- A `≤`-preserving, `⊤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map
`add_valuation R Γ₀ → add_valuation R Γ'₀`.
-/
def map (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f) (v : add_valuation R Γ₀) :
add_valuation R Γ'₀ :=
v.map
{ to_fun := f,
map_mul' := f.map_add,
map_one' := f.map_zero,
map_zero' := ht } (λ x y h, hf h)
/-- Two additive valuations on `R` are defined to be equivalent if they induce the same
preorder on `R`. -/
def is_equiv (v₁ : add_valuation R Γ₀) (v₂ : add_valuation R Γ'₀) : Prop :=
v₁.is_equiv v₂
end monoid
section group
variables [linear_ordered_add_comm_group_with_top Γ₀] [ring R] (v : add_valuation R Γ₀) {x y z : R}
@[simp] lemma map_inv (v : add_valuation K Γ₀) {x : K} :
v x⁻¹ = - (v x) :=
map_inv₀ v.valuation x
lemma map_units_inv (x : Rˣ) : v (x⁻¹ : Rˣ) = - (v x) :=
map_units_inv v.valuation x
@[simp] lemma map_neg (x : R) : v (-x) = v x :=
v.map_neg x
lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) :=
v.map_sub_swap x y
lemma map_sub (x y : R) : min (v x) (v y) ≤ v (x - y) :=
v.map_sub x y
lemma map_le_sub {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x - y) := v.map_sub_le hx hy
lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = min (v x) (v y) :=
v.map_add_of_distinct_val h
lemma map_eq_of_lt_sub (h : v x < v (y - x)) : v y = v x :=
v.map_eq_of_sub_lt h
end group
end basic
namespace is_equiv
variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀]
variables [ring R]
variables {Γ''₀ : Type*} [linear_ordered_add_comm_monoid_with_top Γ''₀]
variables {v : add_valuation R Γ₀}
variables {v₁ : add_valuation R Γ₀} {v₂ : add_valuation R Γ'₀} {v₃ : add_valuation R Γ''₀}
@[refl] lemma refl : v.is_equiv v := valuation.is_equiv.refl
@[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ := h.symm
@[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ :=
h₁₂.trans h₂₃
lemma of_eq {v' : add_valuation R Γ₀} (h : v = v') : v.is_equiv v' :=
valuation.is_equiv.of_eq h
lemma map {v' : add_valuation R Γ₀} (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f)
(inf : injective f) (h : v.is_equiv v') :
(v.map f ht hf).is_equiv (v'.map f ht hf) :=
h.map
{ to_fun := f,
map_mul' := f.map_add,
map_one' := f.map_zero,
map_zero' := ht } (λ x y h, hf h) inf
/-- `comap` preserves equivalence. -/
lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) :
(v₁.comap f).is_equiv (v₂.comap f) :=
h.comap f
lemma val_eq (h : v₁.is_equiv v₂) {r s : R} :
v₁ r = v₁ s ↔ v₂ r = v₂ s :=
h.val_eq
lemma ne_top (h : v₁.is_equiv v₂) {r : R} :
v₁ r ≠ ⊤ ↔ v₂ r ≠ ⊤ :=
h.ne_zero
end is_equiv
section supp
variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀]
variables [comm_ring R]
variables (v : add_valuation R Γ₀)
/-- The support of an additive valuation `v : R → Γ₀` is the ideal of `R` where `v x = ⊤` -/
def supp : ideal R := v.supp
@[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = ⊤ := v.mem_supp_iff x
lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a :=
v.map_add_supp a h
/-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function.
Note: it's just the function; the valuation is `on_quot hJ`. -/
def on_quot_val {J : ideal R} (hJ : J ≤ supp v) : (R ⧸ J) → Γ₀ := v.on_quot_val hJ
/-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/
def on_quot {J : ideal R} (hJ : J ≤ supp v) :
add_valuation (R ⧸ J) Γ₀ :=
v.on_quot hJ
@[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) :
(v.on_quot hJ).comap (ideal.quotient.mk J) = v :=
v.on_quot_comap_eq hJ
lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) :
supp (v.comap f) = ideal.comap f v.supp :=
v.comap_supp f
lemma self_le_supp_comap (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) :
J ≤ (v.comap (ideal.quotient.mk J)).supp :=
v.self_le_supp_comap J
@[simp] lemma comap_on_quot_eq (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) :
(v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v :=
v.comap_on_quot_eq J
/-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/
lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) :
supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) :=
v.supp_quot hJ
lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 :=
v.supp_quot_supp
end supp -- end of section
attribute [irreducible] add_valuation
end add_valuation
section valuation_notation
localized "notation `ℕₘ₀` := with_zero (multiplicative ℕ)" in discrete_valuation
localized "notation `ℤₘ₀` := with_zero (multiplicative ℤ)" in discrete_valuation
end valuation_notation
|
c4c9b1c54510ef021a36ac982d1194af1c159a99 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/preadditive/hom_orthogonal.lean | 3e8d5b54b5aa66f8e46c3fe34c3033e00a3011f7 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,486 | lean | /-
Copyright (c) 2022 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.linear.basic
import category_theory.preadditive.biproducts
import linear_algebra.matrix.invariant_basis_number
/-!
# Hom orthogonal families.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A family of objects in a category with zero morphisms is "hom orthogonal" if the only
morphism between distinct objects is the zero morphism.
We show that in any category with zero morphisms and finite biproducts,
a morphism between biproducts drawn from a hom orthogonal family `s : ι → C`
can be decomposed into a block diagonal matrix with entries in the endomorphism rings of the `s i`.
When the category is preadditive, this decomposition is an additive equivalence,
and intertwines composition and matrix multiplication.
When the category is `R`-linear, the decomposition is an `R`-linear equivalence.
If every object in the hom orthogonal family has an endomorphism ring with invariant basis number
(e.g. if each object in the family is simple, so its endomorphism ring is a division ring,
or otherwise if each endomorphism ring is commutative),
then decompositions of an object as a biproduct of the family have uniquely defined multiplicities.
We state this as:
```
lemma hom_orthogonal.equiv_of_iso (o : hom_orthogonal s) {f : α → ι} {g : β → ι}
(i : ⨁ (λ a, s (f a)) ≅ ⨁ (λ b, s (g b))) : ∃ e : α ≃ β, ∀ a, g (e a) = f a
```
This is preliminary to defining semisimple categories.
-/
open_locale classical matrix
open category_theory.limits
universes v u
namespace category_theory
variables {C : Type u} [category.{v} C]
/-- A family of objects is "hom orthogonal" if
there is at most one morphism between distinct objects.
(In a category with zero morphisms, that must be the zero morphism.) -/
def hom_orthogonal {ι : Type*} (s : ι → C) : Prop :=
∀ i j, i ≠ j → subsingleton (s i ⟶ s j)
namespace hom_orthogonal
variables {ι : Type*} {s : ι → C}
lemma eq_zero [has_zero_morphisms C] (o : hom_orthogonal s)
{i j : ι} (w : i ≠ j) (f : s i ⟶ s j) : f = 0 :=
by { haveI := o i j w, apply subsingleton.elim, }
section
variables [has_zero_morphisms C] [has_finite_biproducts C]
/-- Morphisms between two direct sums over a hom orthogonal family `s : ι → C`
are equivalent to block diagonal matrices,
with blocks indexed by `ι`,
and matrix entries in `i`-th block living in the endomorphisms of `s i`. -/
@[simps] noncomputable
def matrix_decomposition
(o : hom_orthogonal s) {α β : Type} [fintype α] [fintype β] {f : α → ι} {g : β → ι} :
(⨁ (λ a, s (f a)) ⟶ ⨁ (λ b, s (g b))) ≃
Π (i : ι), matrix (g ⁻¹' {i}) (f ⁻¹' {i}) (End (s i)) :=
{ to_fun := λ z i j k,
eq_to_hom (by { rcases k with ⟨k, ⟨⟩⟩, simp, }) ≫
biproduct.components z k j ≫ eq_to_hom (by { rcases j with ⟨j, ⟨⟩⟩, simp, }),
inv_fun := λ z, biproduct.matrix (λ j k, if h : f j = g k then
z (f j) ⟨k, by simp [h]⟩ ⟨j, by simp⟩ ≫ eq_to_hom (by simp [h])
else
0),
left_inv := λ z, begin
ext j k,
simp only [category.assoc, biproduct.lift_π, biproduct.ι_matrix],
split_ifs,
{ simp, refl, },
{ symmetry, apply o.eq_zero h, },
end,
right_inv := λ z, begin
ext i ⟨j, w⟩ ⟨k, ⟨⟩⟩,
simp only [set.mem_preimage, set.mem_singleton_iff],
simp [w.symm], refl,
end, }
end
section
variables [preadditive C] [has_finite_biproducts C]
/-- `hom_orthogonal.matrix_decomposition` as an additive equivalence. -/
@[simps] noncomputable
def matrix_decomposition_add_equiv
(o : hom_orthogonal s) {α β : Type} [fintype α] [fintype β] {f : α → ι} {g : β → ι} :
(⨁ (λ a, s (f a)) ⟶ ⨁ (λ b, s (g b))) ≃+
Π (i : ι), matrix (g ⁻¹' {i}) (f ⁻¹' {i}) (End (s i)) :=
{ map_add' := λ w z, by { ext, dsimp [biproduct.components], simp, },
..o.matrix_decomposition, }.
@[simp]
lemma matrix_decomposition_id
(o : hom_orthogonal s) {α : Type} [fintype α] {f : α → ι} (i : ι) :
o.matrix_decomposition (𝟙 (⨁ (λ a, s (f a)))) i = 1 :=
begin
ext ⟨b, ⟨⟩⟩ ⟨a⟩,
simp only [set.mem_preimage, set.mem_singleton_iff] at j_property,
simp only [category.comp_id, category.id_comp, category.assoc, End.one_def, eq_to_hom_refl,
matrix.one_apply, hom_orthogonal.matrix_decomposition_apply, biproduct.components],
split_ifs with h,
{ cases h, simp, },
{ convert comp_zero,
simpa using biproduct.ι_π_ne _ (ne.symm h), },
end
lemma matrix_decomposition_comp
(o : hom_orthogonal s)
{α β γ : Type} [fintype α] [fintype β] [fintype γ] {f : α → ι} {g : β → ι} {h : γ → ι}
(z : (⨁ (λ a, s (f a)) ⟶ ⨁ (λ b, s (g b)))) (w : (⨁ (λ b, s (g b)) ⟶ ⨁ (λ c, s (h c))))
(i : ι) :
o.matrix_decomposition (z ≫ w) i = o.matrix_decomposition w i ⬝ o.matrix_decomposition z i :=
begin
ext ⟨c, ⟨⟩⟩ ⟨a⟩,
simp only [set.mem_preimage, set.mem_singleton_iff] at j_property,
simp only [matrix.mul_apply, limits.biproduct.components,
hom_orthogonal.matrix_decomposition_apply,
category.comp_id, category.id_comp, category.assoc, End.mul_def,
eq_to_hom_refl, eq_to_hom_trans_assoc, finset.sum_congr],
conv_lhs { rw [←category.id_comp w, ←biproduct.total], },
simp only [preadditive.sum_comp, preadditive.comp_sum],
apply finset.sum_congr_set,
{ intros, simp, refl, },
{ intros b nm,
simp only [set.mem_preimage, set.mem_singleton_iff] at nm,
simp only [category.assoc],
convert comp_zero,
convert comp_zero,
convert comp_zero,
convert comp_zero,
apply o.eq_zero nm, },
end
section
variables {R : Type*} [semiring R] [linear R C]
/-- `hom_orthogonal.matrix_decomposition` as an `R`-linear equivalence. -/
@[simps] noncomputable
def matrix_decomposition_linear_equiv
(o : hom_orthogonal s)
{α β : Type} [fintype α] [fintype β] {f : α → ι} {g : β → ι} :
(⨁ (λ a, s (f a)) ⟶ ⨁ (λ b, s (g b))) ≃ₗ[R]
Π (i : ι), matrix (g ⁻¹' {i}) (f ⁻¹' {i}) (End (s i)) :=
{ map_smul' := λ w z, by { ext, dsimp [biproduct.components], simp, },
..o.matrix_decomposition_add_equiv, }
end
/-!
The hypothesis that `End (s i)` has invariant basis number is automatically satisfied
if `s i` is simple (as then `End (s i)` is a division ring).
-/
variables [∀ i, invariant_basis_number (End (s i))]
/--
Given a hom orthogonal family `s : ι → C`
for which each `End (s i)` is a ring with invariant basis number (e.g. if each `s i` is simple),
if two direct sums over `s` are isomorphic, then they have the same multiplicities.
-/
lemma equiv_of_iso (o : hom_orthogonal s)
{α β : Type} [fintype α] [fintype β] {f : α → ι} {g : β → ι}
(i : ⨁ (λ a, s (f a)) ≅ ⨁ (λ b, s (g b))) :
∃ e : α ≃ β, ∀ a, g (e a) = f a :=
begin
refine ⟨equiv.of_preimage_equiv _, λ a, equiv.of_preimage_equiv_map _ _⟩,
intro c,
apply nonempty.some,
apply cardinal.eq.1,
simp only [cardinal.mk_fintype, nat.cast_inj],
exact matrix.square_of_invertible
(o.matrix_decomposition i.inv c) (o.matrix_decomposition i.hom c)
(by { rw ←o.matrix_decomposition_comp, simp, })
(by { rw ←o.matrix_decomposition_comp, simp, })
end
end
end hom_orthogonal
end category_theory
|
53d29aaf6164f9054815c0d87ab11f30551a31b8 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/homotopy/sphere.hlean | b7dd1f3fe56dbb4bca39189652f215c625efba30 | [
"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 | 7,060 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Declaration of the n-spheres
-/
import .susp types.trunc
open eq nat susp bool is_trunc unit pointed
/-
We can define spheres with the following possible indices:
- trunc_index (defining S^-2 = S^-1 = empty)
- nat (forgetting that S^-1 = empty)
- nat, but counting wrong (S^0 = empty, S^1 = bool, ...)
- some new type "integers >= -1"
We choose the last option here.
-/
/- Sphere levels -/
inductive sphere_index : Type₀ :=
| minus_one : sphere_index
| succ : sphere_index → sphere_index
namespace trunc_index
definition sub_one [reducible] (n : sphere_index) : trunc_index :=
sphere_index.rec_on n -2 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
end trunc_index
namespace sphere_index
/-
notation for sphere_index is -1, 0, 1, ...
from 0 and up this comes from a coercion from num to sphere_index (via nat)
-/
definition has_zero_sphere_index [instance] [reducible] : has_zero sphere_index :=
has_zero.mk (succ minus_one)
definition has_one_sphere_index [instance] [reducible] : has_one sphere_index :=
has_one.mk (succ (succ minus_one))
postfix `.+1`:(max+1) := sphere_index.succ
postfix `.+2`:(max+1) := λ(n : sphere_index), (n .+1 .+1)
notation `-1` := minus_one
notation `ℕ₋₁` := sphere_index
definition add (n m : sphere_index) : sphere_index :=
sphere_index.rec_on m n (λ k l, l .+1)
definition leq (n m : sphere_index) : Type₀ :=
sphere_index.rec_on n (λm, unit) (λ n p m, sphere_index.rec_on m (λ p, empty) (λ m q p, p m) p) m
infix `+1+`:65 := sphere_index.add
definition has_le_sphere_index [instance] [reducible] : has_le sphere_index :=
has_le.mk leq
definition succ_le_succ {n m : sphere_index} (H : n ≤ m) : n.+1 ≤ m.+1 := proof H qed
definition le_of_succ_le_succ {n m : sphere_index} (H : n.+1 ≤ m.+1) : n ≤ m := proof H qed
definition minus_two_le (n : sphere_index) : -1 ≤ n := star
definition empty_of_succ_le_minus_two {n : sphere_index} (H : n .+1 ≤ -1) : empty := H
definition of_nat [coercion] [reducible] (n : nat) : sphere_index :=
(nat.rec_on n -1 (λ n k, k.+1)).+1
definition trunc_index_of_sphere_index [coercion] [reducible] (n : sphere_index) : trunc_index :=
(sphere_index.rec_on n -2 (λ n k, k.+1)).+1
definition sub_one [reducible] (n : ℕ) : sphere_index :=
nat.rec_on n -1 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
open trunc_index
definition sub_two_eq_sub_one_sub_one (n : ℕ) : n.-2 = n.-1.-1 :=
nat.rec_on n idp (λn p, ap trunc_index.succ p)
end sphere_index
open sphere_index equiv
definition sphere : sphere_index → Type₀
| -1 := empty
| n.+1 := susp (sphere n)
namespace sphere
definition base {n : ℕ} : sphere n := north
definition pointed_sphere [instance] [constructor] (n : ℕ) : pointed (sphere n) :=
pointed.mk base
definition Sphere [constructor] (n : ℕ) : Pointed := pointed.mk' (sphere n)
namespace ops
abbreviation S := sphere
notation `S.`:max := Sphere
end ops
open sphere.ops
definition equator (n : ℕ) : map₊ (S. n) (Ω (S. (succ n))) :=
pmap.mk (λa, merid a ⬝ (merid base)⁻¹) !con.right_inv
definition surf {n : ℕ} : Ω[n] S. n :=
nat.rec_on n (proof base qed)
(begin intro m s, refine cast _ (apn m (equator m) s),
exact ap Pointed.carrier !loop_space_succ_eq_in⁻¹ end)
definition bool_of_sphere : S 0 → bool :=
proof susp.rec ff tt (λx, empty.elim x) qed
definition sphere_of_bool : bool → S 0
| ff := proof north qed
| tt := proof south qed
definition sphere_equiv_bool : S 0 ≃ bool :=
equiv.MK bool_of_sphere
sphere_of_bool
(λb, match b with | tt := idp | ff := idp end)
(λx, proof susp.rec_on x idp idp (empty.rec _) qed)
definition sphere_eq_bool : S 0 = bool :=
ua sphere_equiv_bool
definition sphere_eq_bool_pointed : S. 0 = Bool :=
Pointed_eq sphere_equiv_bool idp
-- TODO: the commented-out part makes the forward function below "apn _ surf"
definition pmap_sphere (A : Pointed) (n : ℕ) : map₊ (S. n) A ≃ Ω[n] A :=
begin
-- fapply equiv_change_fun,
-- {
revert A, induction n with n IH: intro A,
{ rewrite [sphere_eq_bool_pointed], apply pmap_bool_equiv},
{ refine susp_adjoint_loop (S. n) A ⬝e !IH ⬝e _, rewrite [loop_space_succ_eq_in]}
-- },
-- { intro f, exact apn n f surf},
-- { revert A, induction n with n IH: intro A f,
-- { exact sorry},
-- { exact sorry}}
end
protected definition elim {n : ℕ} {P : Pointed} (p : Ω[n] P) : map₊ (S. n) P :=
to_inv !pmap_sphere p
-- definition elim_surf {n : ℕ} {P : Pointed} (p : Ω[n] P) : apn n (sphere.elim p) surf = p :=
-- begin
-- induction n with n IH,
-- { esimp [apn,surf,sphere.elim,pmap_sphere], apply sorry},
-- { apply sorry}
-- end
end sphere
open sphere sphere.ops
structure weakly_constant [class] {A B : Type} (f : A → B) := --move
(is_weakly_constant : Πa a', f a = f a')
abbreviation wconst := @weakly_constant.is_weakly_constant
namespace is_trunc
open trunc_index
variables {n : ℕ} {A : Type}
definition is_trunc_of_pmap_sphere_constant
(H : Π(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply iff.elim_right !is_trunc_iff_is_contr_loop,
intro a,
apply is_trunc_equiv_closed, apply pmap_sphere,
fapply is_contr.mk,
{ exact pmap.mk (λx, a) idp},
{ intro f, fapply pmap_eq,
{ intro x, esimp, refine !respect_pt⁻¹ ⬝ (!H ⬝ !H⁻¹)},
{ rewrite [▸*,con.right_inv,▸*,con.left_inv]}}
end
definition is_trunc_iff_map_sphere_constant
(H : Π(f : S n → A) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply is_trunc_of_pmap_sphere_constant,
intros, cases f with f p, esimp at *, apply H
end
definition pmap_sphere_constant_of_is_trunc' [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n) : f x = f base :=
begin
let H' := iff.elim_left (is_trunc_iff_is_contr_loop n A) H a,
note H'' := @is_trunc_equiv_closed_rev _ _ _ !pmap_sphere H',
assert p : (f = pmap.mk (λx, f base) (respect_pt f)),
apply is_hprop.elim,
exact ap10 (ap pmap.map p) x
end
definition pmap_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x y : S n) : f x = f y :=
let H := pmap_sphere_constant_of_is_trunc' a f in !H ⬝ !H⁻¹
definition map_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x y : S n) : f x = f y :=
pmap_sphere_constant_of_is_trunc (f base) (pmap.mk f idp) x y
definition map_sphere_constant_of_is_trunc_self [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x : S n) : map_sphere_constant_of_is_trunc f x x = idp :=
!con.right_inv
end is_trunc
|
716aba3185e7bb6965e2df5106852b1eb94883ed | 8e381650eb2c1c5361be64ff97e47d956bf2ab9f | /src/to_mathlib/localization/local_rings.lean | 5a5bf43bee55157cfa44ecf2322195ce80a9d55c | [] | no_license | alreadydone/lean-scheme | 04c51ab08eca7ccf6c21344d45d202780fa667af | 52d7624f57415eea27ed4dfa916cd94189221a1c | refs/heads/master | 1,599,418,221,423 | 1,562,248,559,000 | 1,562,248,559,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,913 | lean | /-
A ring with the localization property at a prime ideal is a local ring.
-/
import ring_theory.ideals
import ring_theory.localization
import to_mathlib.localization.localization_alt
universes u v
local attribute [instance] classical.prop_decidable
open localization_alt
variables {α : Type u} {β : Type v} [comm_ring α] [comm_ring β]
variables {f : α → β} [is_ring_hom f]
variables {P : ideal α} (HP : ideal.is_prime P)
variables (Hloc : is_localization_data (-P : set α) f)
include Hloc
lemma is_local_ring.of_is_localization_data_at_prime : is_local_ring β :=
begin
apply local_of_nonunits_ideal,
{ -- 0 ≠ 1.
intros HC,
rw ←is_ring_hom.map_one f at HC,
have Hone : (1 : α) ∈ ker f := HC.symm,
replace Hone := Hloc.ker_le Hone,
rcases Hone with ⟨⟨⟨u, ⟨v, HvnP⟩⟩, Huv⟩, Heq⟩,
dsimp at Huv,
dsimp at Heq,
rw Heq at Huv,
have Hzero : (0 : α) ∈ P := ideal.zero_mem P,
rw ←Huv at Hzero,
cases (HP.2 Hzero) with Hone Hv,
{ apply HP.1,
rw ideal.eq_top_iff_one,
exact Hone, },
{ change v ∉ P at HvnP,
exact (HvnP Hv), }, },
{ -- x and y units then x + y is a unit.
intros x y Hx Hy HC,
change ¬is_unit x at Hx,
change ¬is_unit y at Hy,
rcases is_unit_iff_exists_inv.1 HC with ⟨inv, Hinv⟩,
rcases Hloc.has_denom x with ⟨⟨⟨q₁, Hq₁⟩, p₁⟩, Hp₁q₁⟩,
rcases Hloc.has_denom y with ⟨⟨⟨q₂, Hq₂⟩, p₂⟩, Hp₂q₂⟩,
rcases Hloc.has_denom inv with ⟨⟨⟨q₃, Hq₃⟩, p₃⟩, Hp₃q₃⟩,
change q₁ ∉ P at Hq₁,
change q₂ ∉ P at Hq₂,
change q₃ ∉ P at Hq₃,
change f q₁ * x = f p₁ at Hp₁q₁,
change f q₂ * y = f p₂ at Hp₂q₂,
change f q₃ * inv = f p₃ at Hp₃q₃,
have Hp₁P : p₁ ∈ P,
by_contra Hp₁,
apply Hx,
rcases (Hloc.inverts ⟨p₁, Hp₁⟩) with ⟨w₁, Hw₁⟩,
change f p₁ * w₁ = 1 at Hw₁,
apply is_unit_of_mul_one x (f q₁ * w₁),
rw [←mul_assoc, mul_comm x, Hp₁q₁],
exact Hw₁,
have Hp₂P : p₂ ∈ P,
by_contra Hp₂,
apply Hy,
rcases (Hloc.inverts ⟨p₂, Hp₂⟩) with ⟨w₂, Hw₂⟩,
change f p₂ * w₂ = 1 at Hw₂,
apply is_unit_of_mul_one y (f q₂ * w₂),
rw [←mul_assoc, mul_comm y, Hp₂q₂],
exact Hw₂,
have Hp₃q₂p₁P : p₃ * q₂ * p₁ ∈ P := ideal.mul_mem_left P Hp₁P,
have Hp₃q₁p₂P : p₃ * q₁ * p₂ ∈ P := ideal.mul_mem_left P Hp₂P,
have Hmem := (ideal.neg_mem_iff P).2 (ideal.add_mem P Hp₃q₂p₁P Hp₃q₁p₂P),
have Hq₁q₂q₃nP : q₁ * q₂ * q₃ ∉ P,
intros HC,
cases (HP.2 HC) with HC₁₂ HC₃,
{ cases (HP.2 HC₁₂) with HC₁ HC₂,
{ exact (Hq₁ HC₁), },
{ exact (Hq₂ HC₂), } },
{ exact (Hq₃ HC₃), },
have Hzero : f (q₁ * q₂ * q₃ + -(p₃ * q₂ * p₁ + p₃ * q₁ * p₂)) = 0,
rw [←sub_eq_add_neg, is_ring_hom.map_sub f, sub_eq_zero, is_ring_hom.map_add f],
iterate 6 { rw is_ring_hom.map_mul f, },
iterate 2 { rw mul_assoc (f p₃), },
rw [←mul_add (f p₃), ←Hp₃q₃, mul_comm (f q₃ * inv), mul_comm (f q₃), ←mul_assoc _ inv],
rw [←Hp₁q₁, ←mul_assoc (f q₂), mul_comm (f q₂)],
rw [←Hp₂q₂, ←mul_assoc (f q₁)],
rw [←mul_add (f q₁ * f q₂), mul_assoc _ (x + y) inv, Hinv, mul_one],
rcases (Hloc.ker_le Hzero) with ⟨⟨⟨u, ⟨v, HvnP⟩⟩, Huv⟩, Helem⟩,
change v ∉ P at HvnP,
dsimp only [subtype.coe_mk] at Huv,
dsimp only [subtype.coe_mk] at Helem,
rw Helem at Huv; clear Helem,
cases (HP.mem_or_mem_of_mul_eq_zero Huv) with Helem HvP,
{ rw ideal.add_mem_iff_left P Hmem at Helem,
exact (Hq₁q₂q₃nP Helem), },
{ exact (HvnP HvP), } }
end
|
5fbf0f1bb76140e9f1f5e9c7ebb62b6835c527f6 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/tactic/omega/clause.lean | 97bc3cbc72163f6c41cc598b40433957cf1203f6 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 1,613 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
(([t₁,...tₘ],[s₁,...,sₙ]) : clause) encodes the constraints
0 = ⟦t₁⟧ ∧ ... ∧ 0 = ⟦tₘ⟧ ∧ 0 ≤ ⟦s₁⟧ ∧ ... ∧ 0 ≤ ⟦sₙ⟧, where
⟦t⟧ is the value of (t : term).
-/
import tactic.omega.term
namespace omega
@[reducible] def clause := (list term) × (list term)
namespace clause
def holds (v : nat → int) : clause → Prop
| (eqs,les) :=
( (∀ t : term, t ∈ eqs → 0 = term.val v t)
∧ (∀ t : term, t ∈ les → 0 ≤ term.val v t) )
def sat (c : clause) : Prop :=
∃ v : nat → int, holds v c
def unsat (c : clause) : Prop := ¬ c.sat
def append (c1 c2 : clause) : clause :=
(c1.fst ++ c2.fst, c1.snd ++ c2.snd)
def holds_append {v : nat → int} {c1 c2 : clause} :
holds v c1 → holds v c2 → holds v (append c1 c2) :=
begin
intros h1 h2,
cases c1 with eqs1 les1,
cases c2 with eqs2 les2,
cases h1, cases h2,
constructor; rw list.forall_mem_append;
constructor; assumption,
end
end clause
def clauses.sat (cs : list clause) : Prop :=
∃ c ∈ cs, clause.sat c
def clauses.unsat (cs : list clause) : Prop := ¬ clauses.sat cs
lemma clauses.unsat_nil : clauses.unsat [] :=
begin intro h1, rcases h1 with ⟨c,h1,h2⟩, cases h1 end
lemma clauses.unsat_cons (c : clause) (cs : list clause) :
clause.unsat c → clauses.unsat cs →
clauses.unsat (c::cs) | h1 h2 h3 :=
begin
unfold clauses.sat at h3,
rw list.exists_mem_cons_iff at h3,
cases h3; contradiction,
end
end omega
|
6cc9f9d2bf47a9f777c46a2f41c88e7c34fc20ca | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/category_theory/subobject.lean | bbaac7a8a2708d5d57732211144c6cc84cfa34cd | [
"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 | 37,179 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Scott Morrison
-/
import category_theory.opposites
import category_theory.full_subcategory
import category_theory.skeletal
import category_theory.currying
import category_theory.limits.lattice
import category_theory.limits.over
import category_theory.limits.shapes.images
import category_theory.limits.shapes.kernels
import category_theory.monad.adjunction
/-!
# The lattice of subobjects
We define `subobject X` as the quotient (by isomorphisms) of
`mono_over X := {f : over X // mono f.hom}`.
Here `mono_over X` is a thin category (a pair of objects has at most one morphism between them),
so we can think of it as a preorder. However as it is not skeletal, it is not a partial order.
There is a coercion from `subobject X` back to the ambient category `C`
(using choice to pick a representative), and for `P : subobject X`,
`P.arrow : (P : C) ⟶ X` is the inclusion morphism.
We provide
* `def pullback [has_pullbacks C] (f : X ⟶ Y) : subobject Y ⥤ subobject X`
* `def map (f : X ⟶ Y) [mono f] : subobject X ⥤ subobject Y`
* `def «exists» [has_images C] (f : X ⟶ Y) : subobject X ⥤ subobject Y`
(each first at the level of `mono_over`), and prove their basic properties and relationships.
We also provide the `semilattice_inf_top (subobject X)` instance when `[has_pullback C]`,
and the `semilattice_sup (subobject X)` instance when `[has_images C] [has_binary_coproducts C]`.
## Notes
This development originally appeared in Bhavik Mehta's "Topos theory for Lean" repository,
and was ported to mathlib by Scott Morrison.
### Implementation note
Currently we describe `pullback`, `map`, etc., as functors.
It may be better to just say that they are monotone functions,
and even avoid using categorical language entirely when describing `subobject X`.
(It's worth keeping this in mind in future use; it should be a relatively easy change here
if it looks preferable.)
### Relation to pseudoelements
There is a separate development of pseudoelements in `category_theory.abelian.pseudoelements`,
as a quotient (but not by isomorphism) of `over X`.
When a morphism `f` has an image, the image represents the same pseudoelement.
In a category with images `pseudoelements X` could be constructed as a quotient of `mono_over X`.
In fact, in an abelian category (I'm not sure in what generality beyond that),
`pseudoelements X` agrees with `subobject X`, but we haven't developed this in mathlib yet.
-/
universes v₁ v₂ u₁ u₂
noncomputable theory
namespace category_theory
open category_theory category_theory.category category_theory.limits
variables {C : Type u₁} [category.{v₁} C] {X Y Z : C}
variables {D : Type u₂} [category.{v₂} D]
/--
The category of monomorphisms into `X` as a full subcategory of the over category.
This isn't skeletal, so it's not a partial order.
Later we define `subobject X` as the quotient of this by isomorphisms.
-/
@[derive [category]]
def mono_over (X : C) := {f : over X // mono f.hom}
namespace mono_over
/-- Construct a `mono_over X`. -/
@[simps]
def mk' {X A : C} (f : A ⟶ X) [hf : mono f] : mono_over X := { val := over.mk f, property := hf }
/-- The inclusion from monomorphisms over X to morphisms over X. -/
def forget (X : C) : mono_over X ⥤ over X := full_subcategory_inclusion _
instance : has_coe (mono_over X) C :=
{ coe := λ Y, Y.val.left, }
@[simp]
lemma forget_obj_left {f} : ((forget X).obj f).left = (f : C) := rfl
/-- Convenience notation for the underlying arrow of a monomorphism over X. -/
abbreviation arrow (f : mono_over X) : _ ⟶ X := ((forget X).obj f).hom
@[simp] lemma mk'_arrow {X A : C} (f : A ⟶ X) [hf : mono f] : (mk' f).arrow = f := rfl
@[simp]
lemma forget_obj_hom {f} : ((forget X).obj f).hom = f.arrow := rfl
instance : full (forget X) := full_subcategory.full _
instance : faithful (forget X) := full_subcategory.faithful _
instance mono (f : mono_over X) : mono f.arrow := f.property
/-- The category of monomorphisms over X is a thin category,
which makes defining its skeleton easy. -/
instance is_thin {X : C} (f g : mono_over X) : subsingleton (f ⟶ g) :=
⟨begin
intros h₁ h₂,
ext1,
erw [← cancel_mono g.arrow, over.w h₁, over.w h₂],
end⟩
@[reassoc] lemma w {f g : mono_over X} (k : f ⟶ g) : k.left ≫ g.arrow = f.arrow := over.w _
/-- Convenience constructor for a morphism in monomorphisms over `X`. -/
abbreviation hom_mk {f g : mono_over X} (h : f.val.left ⟶ g.val.left) (w : h ≫ g.arrow = f.arrow) :
f ⟶ g :=
over.hom_mk h w
/-- Convenience constructor for an isomorphism in monomorphisms over `X`. -/
@[simps]
def iso_mk {f g : mono_over X} (h : f.val.left ≅ g.val.left) (w : h.hom ≫ g.arrow = f.arrow) :
f ≅ g :=
{ hom := hom_mk h.hom w,
inv := hom_mk h.inv (by rw [h.inv_comp_eq, w]) }
/--
Lift a functor between over categories to a functor between `mono_over` categories,
given suitable evidence that morphisms are taken to monomorphisms.
-/
@[simps]
def lift {Y : D} (F : over Y ⥤ over X)
(h : ∀ (f : mono_over Y), mono (F.obj ((mono_over.forget Y).obj f)).hom) :
mono_over Y ⥤ mono_over X :=
{ obj := λ f, ⟨_, h f⟩,
map := λ _ _ k, (mono_over.forget X).preimage ((mono_over.forget Y ⋙ F).map k), }
/--
Isomorphic functors `over Y ⥤ over X` lift to isomorphic functors `mono_over Y ⥤ mono_over X`.
-/
def lift_iso {Y : D} {F₁ F₂ : over Y ⥤ over X} (h₁ h₂) (i : F₁ ≅ F₂) :
lift F₁ h₁ ≅ lift F₂ h₂ :=
fully_faithful_cancel_right (mono_over.forget X) (iso_whisker_left (mono_over.forget Y) i)
/-- `mono_over.lift` commutes with composition of functors. -/
def lift_comp {X Z : C} {Y : D} (F : over X ⥤ over Y) (G : over Y ⥤ over Z) (h₁ h₂) :
lift F h₁ ⋙ lift G h₂ ≅ lift (F ⋙ G) (λ f, h₂ ⟨_, h₁ f⟩) :=
fully_faithful_cancel_right (mono_over.forget _) (iso.refl _)
/-- `mono_over.lift` preserves the identity functor. -/
def lift_id :
lift (𝟭 (over X)) (λ f, f.2) ≅ 𝟭 _ :=
fully_faithful_cancel_right (mono_over.forget _) (iso.refl _)
@[simp]
lemma lift_comm (F : over Y ⥤ over X)
(h : ∀ (f : mono_over Y), mono (F.obj ((mono_over.forget Y).obj f)).hom) :
lift F h ⋙ mono_over.forget X = mono_over.forget Y ⋙ F :=
rfl
/--
Monomorphisms over an object `f : over A` in an over category
are equivalent to monomorphisms over the source of `f`.
-/
def slice {A : C} {f : over A} (h₁ h₂) : mono_over f ≌ mono_over f.left :=
{ functor := mono_over.lift f.iterated_slice_equiv.functor h₁,
inverse := mono_over.lift f.iterated_slice_equiv.inverse h₂,
unit_iso := mono_over.lift_id.symm ≪≫
mono_over.lift_iso _ _ f.iterated_slice_equiv.unit_iso ≪≫
(mono_over.lift_comp _ _ _ _).symm,
counit_iso := mono_over.lift_comp _ _ _ _ ≪≫
mono_over.lift_iso _ _ f.iterated_slice_equiv.counit_iso ≪≫
mono_over.lift_id }
section pullback
variables [has_pullbacks C]
/-- When `C` has pullbacks, a morphism `f : X ⟶ Y` induces a functor `mono_over Y ⥤ mono_over X`,
by pulling back a monomorphism along `f`. -/
def pullback (f : X ⟶ Y) : mono_over Y ⥤ mono_over X :=
mono_over.lift (over.pullback f)
begin
intro g,
apply @pullback.snd_of_mono _ _ _ _ _ _ _ _ _,
change mono g.arrow,
apply_instance,
end
/-- pullback commutes with composition (up to a natural isomorphism) -/
def pullback_comp (f : X ⟶ Y) (g : Y ⟶ Z) : pullback (f ≫ g) ≅ pullback g ⋙ pullback f :=
lift_iso _ _ (over.pullback_comp _ _) ≪≫ (lift_comp _ _ _ _).symm
/-- pullback preserves the identity (up to a natural isomorphism) -/
def pullback_id : pullback (𝟙 X) ≅ 𝟭 _ :=
lift_iso _ _ over.pullback_id ≪≫ lift_id
@[simp] lemma pullback_obj_left (f : X ⟶ Y) (g : mono_over Y) :
(((pullback f).obj g) : C) = limits.pullback g.arrow f :=
rfl
@[simp] lemma pullback_obj_arrow (f : X ⟶ Y) (g : mono_over Y) :
((pullback f).obj g).arrow = pullback.snd :=
rfl
end pullback
section map
attribute [instance] mono_comp
/--
We can map monomorphisms over `X` to monomorphisms over `Y`
by post-composition with a monomorphism `f : X ⟶ Y`.
-/
def map (f : X ⟶ Y) [mono f] : mono_over X ⥤ mono_over Y :=
lift (over.map f)
(λ g, by apply mono_comp g.arrow f)
/-- `mono_over.map` commutes with composition (up to a natural isomorphism). -/
def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) [mono f] [mono g] :
map (f ≫ g) ≅ map f ⋙ map g :=
lift_iso _ _ (over.map_comp _ _) ≪≫ (lift_comp _ _ _ _).symm
/-- `mono_over.map` preserves the identity (up to a natural isomorphism). -/
def map_id : map (𝟙 X) ≅ 𝟭 _ :=
lift_iso _ _ over.map_id ≪≫ lift_id
@[simp] lemma map_obj_left (f : X ⟶ Y) [mono f] (g : mono_over X) :
(((map f).obj g) : C) = g.val.left :=
rfl
@[simp]
lemma map_obj_arrow (f : X ⟶ Y) [mono f] (g : mono_over X) :
((map f).obj g).arrow = g.arrow ≫ f :=
rfl
instance full_map (f : X ⟶ Y) [mono f] : full (map f) :=
{ preimage := λ g h e,
begin
refine hom_mk e.left _,
rw [← cancel_mono f, assoc],
apply w e,
end }
instance faithful_map (f : X ⟶ Y) [mono f] : faithful (map f) := {}.
/--
Isomorphic objects have equivalent `mono_over` categories.
-/
def map_iso {A B : C} (e : A ≅ B) : mono_over A ≌ mono_over B :=
{ functor := map e.hom,
inverse := map e.inv,
unit_iso := ((map_comp _ _).symm ≪≫ eq_to_iso (by simp) ≪≫ map_id).symm,
counit_iso := ((map_comp _ _).symm ≪≫ eq_to_iso (by simp) ≪≫ map_id) }
section
variable [has_pullbacks C]
/-- `map f` is left adjoint to `pullback f` when `f` is a monomorphism -/
def map_pullback_adj (f : X ⟶ Y) [mono f] : map f ⊣ pullback f :=
adjunction.restrict_fully_faithful
(forget X) (forget Y) (over.map_pullback_adj f) (iso.refl _) (iso.refl _)
/-- `mono_over.map f` followed by `mono_over.pullback f` is the identity. -/
def pullback_map_self (f : X ⟶ Y) [mono f] :
map f ⋙ pullback f ≅ 𝟭 _ :=
(as_iso (mono_over.map_pullback_adj f).unit).symm
end
end map
section image
variables (f : X ⟶ Y) [has_image f]
/--
The `mono_over Y` for the image inclusion for a morphism `f : X ⟶ Y`.
-/
def image_mono_over (f : X ⟶ Y) [has_image f] : mono_over Y := mono_over.mk' (image.ι f)
end image
section image
variables [has_images C]
/--
Taking the image of a morphism gives a functor `over X ⥤ mono_over X`.
-/
@[simps]
def image : over X ⥤ mono_over X :=
{ obj := λ f, image_mono_over f.hom,
map := λ f g k,
begin
apply (forget X).preimage _,
apply over.hom_mk _ _,
refine image.lift {I := image _, m := image.ι g.hom, e := k.left ≫ factor_thru_image g.hom},
apply image.lift_fac,
end }
/--
`mono_over.image : over X ⥤ mono_over X` is left adjoint to
`mono_over.forget : mono_over X ⥤ over X`
-/
def image_forget_adj : image ⊣ forget X :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ f g,
{ to_fun := λ k,
begin
apply over.hom_mk (factor_thru_image f.hom ≫ k.left) _,
change (factor_thru_image f.hom ≫ k.left) ≫ _ = f.hom,
rw [assoc, over.w k],
apply image.fac
end,
inv_fun := λ k,
begin
refine over.hom_mk _ _,
refine image.lift {I := g.val.left, m := g.arrow, e := k.left, fac' := over.w k},
apply image.lift_fac,
end,
left_inv := λ k, subsingleton.elim _ _,
right_inv := λ k,
begin
ext1,
change factor_thru_image _ ≫ image.lift _ = _,
rw [← cancel_mono g.arrow, assoc, image.lift_fac, image.fac f.hom],
exact (over.w k).symm,
end } }
instance : is_right_adjoint (forget X) :=
{ left := image, adj := image_forget_adj }
instance reflective : reflective (forget X) := {}.
/--
Forgetting that a monomorphism over `X` is a monomorphism, then taking its image,
is the identity functor.
-/
def forget_image : forget X ⋙ image ≅ 𝟭 (mono_over X) :=
as_iso (adjunction.counit image_forget_adj)
end image
section «exists»
variables [has_images C]
/--
In the case where `f` is not a monomorphism but `C` has images,
we can still take the "forward map" under it, which agrees with `mono_over.map f`.
-/
def «exists» (f : X ⟶ Y) : mono_over X ⥤ mono_over Y :=
forget _ ⋙ over.map f ⋙ image
instance faithful_exists (f : X ⟶ Y) : faithful («exists» f) := {}.
/--
When `f : X ⟶ Y` is a monomorphism, `exists f` agrees with `map f`.
-/
def exists_iso_map (f : X ⟶ Y) [mono f] : «exists» f ≅ map f :=
nat_iso.of_components
begin
intro Z,
suffices : (forget _).obj ((«exists» f).obj Z) ≅ (forget _).obj ((map f).obj Z),
apply preimage_iso this,
apply over.iso_mk _ _,
apply image_mono_iso_source (Z.arrow ≫ f),
apply image_mono_iso_source_hom_self,
end
begin
intros Z₁ Z₂ g,
ext1,
change image.lift ⟨_, _, _, _⟩ ≫ (image_mono_iso_source (Z₂.arrow ≫ f)).hom =
(image_mono_iso_source (Z₁.arrow ≫ f)).hom ≫ g.left,
rw [← cancel_mono (Z₂.arrow ≫ f), assoc, assoc, w_assoc g, image_mono_iso_source_hom_self,
image_mono_iso_source_hom_self],
apply image.lift_fac,
end
/-- `exists` is adjoint to `pullback` when images exist -/
def exists_pullback_adj (f : X ⟶ Y) [has_pullbacks C] : «exists» f ⊣ pullback f :=
adjunction.restrict_fully_faithful (forget X) (𝟭 _)
((over.map_pullback_adj f).comp _ _ image_forget_adj)
(iso.refl _)
(iso.refl _)
end «exists»
section has_top
instance {X : C} : has_top (mono_over X) :=
{ top := mk' (𝟙 _) }
instance {X : C} : inhabited (mono_over X) := ⟨⊤⟩
/-- The morphism to the top object in `mono_over X`. -/
def le_top (f : mono_over X) : f ⟶ ⊤ :=
hom_mk f.arrow (comp_id _)
@[simp] lemma top_left (X : C) : ((⊤ : mono_over X) : C) = X := rfl
@[simp] lemma top_arrow (X : C) : (⊤ : mono_over X).arrow = 𝟙 X := rfl
/-- `map f` sends `⊤ : mono_over X` to `⟨X, f⟩ : mono_over Y`. -/
def map_top (f : X ⟶ Y) [mono f] : (map f).obj ⊤ ≅ mk' f :=
iso_of_both_ways (hom_mk (𝟙 _) rfl) (hom_mk (𝟙 _) (by simp [id_comp f]))
section
variable [has_pullbacks C]
/-- The pullback of the top object in `mono_over Y`
is (isomorphic to) the top object in `mono_over X`. -/
def pullback_top (f : X ⟶ Y) : (pullback f).obj ⊤ ≅ ⊤ :=
iso_of_both_ways (le_top _) (hom_mk (pullback.lift f (𝟙 _) (by tidy)) (pullback.lift_snd _ _ _))
/-- There is a morphism from `⊤ : mono_over A` to the pullback of a monomorphism along itself;
as the category is thin this is an isomorphism. -/
def top_le_pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(⊤ : mono_over A) ⟶ (pullback f).obj (mk' f) :=
hom_mk _ (pullback.lift_snd _ _ rfl)
/-- The pullback of a monomorphism along itself is isomorphic to the top object. -/
def pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(pullback f).obj (mk' f) ≅ ⊤ :=
iso_of_both_ways (le_top _) (top_le_pullback_self _)
end
end has_top
section has_bot
variables [has_zero_morphisms C] [has_zero_object C]
local attribute [instance] has_zero_object.has_zero
instance {X : C} : has_bot (mono_over X) :=
{ bot := mk' (0 : 0 ⟶ X) }
@[simp] lemma bot_left (X : C) : ((⊥ : mono_over X) : C) = 0 := rfl
@[simp] lemma bot_arrow {X : C} : (⊥ : mono_over X).arrow = 0 :=
by ext
/-- The (unique) morphism from `⊥ : mono_over X` to any other `f : mono_over X`. -/
def bot_le {X : C} (f : mono_over X) : ⊥ ⟶ f :=
hom_mk 0 (by simp)
/-- `map f` sends `⊥ : mono_over X` to `⊥ : mono_over Y`. -/
def map_bot (f : X ⟶ Y) [mono f] : (map f).obj ⊥ ≅ ⊥ :=
iso_of_both_ways (hom_mk 0 (by simp)) (hom_mk (𝟙 _) (by simp [id_comp f]))
end has_bot
section inf
variables [has_pullbacks C]
/--
When `[has_pullbacks C]`, `mono_over A` has "intersections", functorial in both arguments.
As `mono_over A` is only a preorder, this doesn't satisfy the axioms of `semilattice_inf`,
but we reuse all the names from `semilattice_inf` because they will be used to construct
`semilattice_inf (subobject A)` shortly.
-/
@[simps]
def inf {A : C} : mono_over A ⥤ mono_over A ⥤ mono_over A :=
{ obj := λ f, pullback f.arrow ⋙ map f.arrow,
map := λ f₁ f₂ k,
{ app := λ g,
begin
apply hom_mk _ _,
apply pullback.lift pullback.fst (pullback.snd ≫ k.left) _,
rw [pullback.condition, assoc, w k],
dsimp,
rw [pullback.lift_snd_assoc, assoc, w k],
end } }.
/-- A morphism from the "infimum" of two objects in `mono_over A` to the first object. -/
def inf_le_left {A : C} (f g : mono_over A) :
(inf.obj f).obj g ⟶ f :=
hom_mk _ rfl
/-- A morphism from the "infimum" of two objects in `mono_over A` to the second object. -/
def inf_le_right {A : C} (f g : mono_over A) :
(inf.obj f).obj g ⟶ g :=
hom_mk _ pullback.condition
/-- A morphism version of the `le_inf` axiom. -/
def le_inf {A : C} (f g h : mono_over A) :
(h ⟶ f) → (h ⟶ g) → (h ⟶ (inf.obj f).obj g) :=
begin
intros k₁ k₂,
refine hom_mk (pullback.lift k₂.left k₁.left _) _,
rw [w k₁, w k₂],
erw [pullback.lift_snd_assoc, w k₁],
end
end inf
section sup
variables [has_images C] [has_binary_coproducts C]
/-- When `[has_images C] [has_binary_coproducts C]`, `mono_over A` has a `sup` construction,
which is functorial in both arguments,
and which on `subobject A` will induce a `semilattice_sup`. -/
def sup {A : C} : mono_over A ⥤ mono_over A ⥤ mono_over A :=
curry_obj ((forget A).prod (forget A) ⋙ uncurry.obj over.coprod ⋙ image)
/-- A morphism version of `le_sup_left`. -/
def le_sup_left {A : C} (f g : mono_over A) :
f ⟶ (sup.obj f).obj g :=
begin
refine hom_mk (coprod.inl ≫ factor_thru_image _) _,
erw [category.assoc, image.fac, coprod.inl_desc],
refl,
end
/-- A morphism version of `le_sup_right`. -/
def le_sup_right {A : C} (f g : mono_over A) :
g ⟶ (sup.obj f).obj g :=
begin
refine hom_mk (coprod.inr ≫ factor_thru_image _) _,
erw [category.assoc, image.fac, coprod.inr_desc],
refl,
end
/-- A morphism version of `sup_le`. -/
def sup_le {A : C} (f g h : mono_over A) :
(f ⟶ h) → (g ⟶ h) → ((sup.obj f).obj g ⟶ h) :=
begin
intros k₁ k₂,
refine hom_mk _ _,
apply image.lift ⟨_, h.arrow, coprod.desc k₁.left k₂.left, _⟩,
{ dsimp,
ext1,
{ simp [w k₁] },
{ simp [w k₂] } },
{ apply image.lift_fac }
end
end sup
end mono_over
/-!
We now construct the subobject lattice for `X : C`,
as the quotient by isomorphisms of `mono_over X`.
Since `mono_over X` is a thin category, we use `thin_skeleton` to take the quotient.
Essentially all the structure defined above on `mono_over X` descends to `subobject X`,
with morphisms becoming inequalities, and isomorphisms becoming equations.
-/
/--
The category of subobjects of `X : C`, defined as isomorphism classes of monomorphisms into `X`.
-/
@[derive [partial_order, category]]
def subobject (X : C) := thin_skeleton (mono_over X)
namespace subobject
/-- Convenience constructor for a subobject. -/
abbreviation mk {X A : C} (f : A ⟶ X) [mono f] : subobject X :=
(to_thin_skeleton _).obj (mono_over.mk' f)
/--
Use choice to pick a representative `mono_over X` for each `subobject X`.
-/
noncomputable
def representative {X : C} : subobject X ⥤ mono_over X :=
thin_skeleton.from_thin_skeleton _
/--
Starting with `A : mono_over X`, we can take its equivalence class in `subobject X`
then pick an arbitrary representative using `representative.obj`.
This is isomorphic (in `mono_over X`) to the original `A`.
-/
noncomputable
def representative_iso {X : C} (A : mono_over X) :
representative.obj ((to_thin_skeleton _).obj A) ≅ A :=
(thin_skeleton.from_thin_skeleton _).as_equivalence.counit_iso.app A
/--
Use choice to pick a representative underlying object in `C` for any `subobject X`.
Prefer to use the coercion `P : C` rather than explicitly writing `underlying.obj P`.
-/
noncomputable
def underlying {X : C} : subobject X ⥤ C :=
representative ⋙ mono_over.forget _ ⋙ over.forget _
instance : has_coe (subobject X) C :=
{ coe := λ Y, underlying.obj Y, }
@[simp] lemma underlying_as_coe {X : C} (P : subobject X) : underlying.obj P = P := rfl
/--
If we construct a `subobject Y` from an explicit `f : X ⟶ Y` with `[mono f]`,
then pick an arbitrary choice of underlying object `(subobject.mk f : C)` back in `C`,
it is isomorphic (in `C`) to the original `X`.
-/
noncomputable
def underlying_iso {X Y : C} (f : X ⟶ Y) [mono f] : (subobject.mk f : C) ≅ X :=
(mono_over.forget _ ⋙ over.forget _).map_iso (representative_iso (mono_over.mk' f))
/--
The morphism in `C` from the arbitrarily chosen underlying object to the ambient object.
-/
noncomputable
def arrow {X : C} (Y : subobject X) : (Y : C) ⟶ X :=
(representative.obj Y).val.hom
@[simp]
lemma underlying_arrow {X : C} {Y Z : subobject X} (f : Y ⟶ Z) :
underlying.map f ≫ arrow Z = arrow Y :=
over.w (representative.map f)
end subobject
namespace limits
section equalizer
variables (f g : X ⟶ Y) [has_equalizer f g]
/-- The equalizer of morphisms `f g : X ⟶ Y` as a `subobject X`. -/
def equalizer_subobject : subobject X :=
subobject.mk (equalizer.ι f g)
/-- The underlying object of `equalizer_subobject f g` is (up to isomorphism!)
the same as the chosen object `equalizer f g`. -/
def equalizer_subobject_iso : (equalizer_subobject f g : C) ≅ equalizer f g :=
subobject.underlying_iso (equalizer.ι f g)
lemma equalizer_subobject_arrow :
(equalizer_subobject f g).arrow = (equalizer_subobject_iso f g).hom ≫ equalizer.ι f g :=
(over.w (subobject.representative_iso (mono_over.mk' (equalizer.ι f g))).hom).symm
@[simp] lemma equalizer_subobject_arrow' :
(equalizer_subobject_iso f g).inv ≫ (equalizer_subobject f g).arrow = equalizer.ι f g :=
over.w (subobject.representative_iso (mono_over.mk' (equalizer.ι f g))).inv
lemma equalizer_subobject_arrow_comp :
(equalizer_subobject f g).arrow ≫ f = (equalizer_subobject f g).arrow ≫ g :=
by simp [equalizer_subobject_arrow, equalizer.condition]
end equalizer
section kernel
variables [has_zero_morphisms C] (f : X ⟶ Y) [has_kernel f]
/-- The kernel of a morphism `f : X ⟶ Y` as a `subobject X`. -/
def kernel_subobject : subobject X :=
subobject.mk (kernel.ι f)
/-- The underlying object of `kernel_subobject f` is (up to isomorphism!)
the same as the chosen object `kernel f`. -/
def kernel_subobject_iso :
(kernel_subobject f : C) ≅ kernel f :=
subobject.underlying_iso (kernel.ι f)
lemma kernel_subobject_arrow :
(kernel_subobject f).arrow = (kernel_subobject_iso f).hom ≫ kernel.ι f :=
(over.w (subobject.representative_iso (mono_over.mk' (kernel.ι f))).hom).symm
@[simp] lemma kernel_subobject_arrow' :
(kernel_subobject_iso f).inv ≫ (kernel_subobject f).arrow = kernel.ι f :=
over.w (subobject.representative_iso (mono_over.mk' (kernel.ι f))).inv
lemma kernel_subobject_arrow_comp :
(kernel_subobject f).arrow ≫ f = 0 :=
by simp [kernel_subobject_arrow, kernel.condition]
end kernel
section image
variables (f : X ⟶ Y) [has_image f]
/-- The image of a morphism `f g : X ⟶ Y` as a `subobject Y`. -/
def image_subobject : subobject Y :=
(to_thin_skeleton _).obj (mono_over.image_mono_over f)
/-- The underlying object of `image_subobject f` is (up to isomorphism!)
the same as the chosen object `image f`. -/
def image_subobject_iso :
(image_subobject f : C) ≅ image f :=
subobject.underlying_iso (image.ι f)
lemma image_subobject_arrow :
(image_subobject f).arrow = (image_subobject_iso f).hom ≫ image.ι f :=
(over.w (subobject.representative_iso (mono_over.mk' (image.ι f))).hom).symm
@[simp] lemma image_subobject_arrow' :
(image_subobject_iso f).inv ≫ (image_subobject f).arrow = image.ι f :=
over.w (subobject.representative_iso (mono_over.mk' (image.ι f))).inv
/-- A factorisation of `f : X ⟶ Y` through `image_subobject f`. -/
def factor_thru_image_subobject : X ⟶ image_subobject f :=
factor_thru_image f ≫ (image_subobject_iso f).inv
lemma image_subobject_arrow_comp :
factor_thru_image_subobject f ≫ (image_subobject f).arrow = f :=
by simp [factor_thru_image_subobject, image_subobject_arrow]
end image
end limits
namespace subobject
/-- Any functor `mono_over X ⥤ mono_over Y` descends to a functor
`subobject X ⥤ subobject Y`, because `mono_over Y` is thin. -/
def lower {Y : D} (F : mono_over X ⥤ mono_over Y) : subobject X ⥤ subobject Y :=
thin_skeleton.map F
/-- Isomorphic functors become equal when lowered to `subobject`.
(It's not as evil as usual to talk about equality between functors
because the categories are thin and skeletal.) -/
lemma lower_iso (F₁ F₂ : mono_over X ⥤ mono_over Y) (h : F₁ ≅ F₂) :
lower F₁ = lower F₂ :=
thin_skeleton.map_iso_eq h
/-- A ternary version of `subobject.lower`. -/
def lower₂ (F : mono_over X ⥤ mono_over Y ⥤ mono_over Z) :
subobject X ⥤ subobject Y ⥤ subobject Z :=
thin_skeleton.map₂ F
@[simp]
lemma lower_comm (F : mono_over Y ⥤ mono_over X) :
to_thin_skeleton _ ⋙ lower F = F ⋙ to_thin_skeleton _ :=
rfl
/-- An adjunction between `mono_over A` and `mono_over B` gives an adjunction
between `subobject A` and `subobject B`. -/
def lower_adjunction {A : C} {B : D}
{L : mono_over A ⥤ mono_over B} {R : mono_over B ⥤ mono_over A} (h : L ⊣ R) :
lower L ⊣ lower R :=
thin_skeleton.lower_adjunction _ _ h
/-- An equivalence between `mono_over A` and `mono_over B` gives an equivalence
between `subobject A` and `subobject B`. -/
@[simps]
def lower_equivalence {A : C} {B : D} (e : mono_over A ≌ mono_over B) : subobject A ≌ subobject B :=
{ functor := lower e.functor,
inverse := lower e.inverse,
unit_iso :=
begin
apply eq_to_iso,
convert thin_skeleton.map_iso_eq e.unit_iso,
{ exact thin_skeleton.map_id_eq.symm },
{ exact (thin_skeleton.map_comp_eq _ _).symm },
end,
counit_iso :=
begin
apply eq_to_iso,
convert thin_skeleton.map_iso_eq e.counit_iso,
{ exact (thin_skeleton.map_comp_eq _ _).symm },
{ exact thin_skeleton.map_id_eq.symm },
end }
section pullback
variables [has_pullbacks C]
/-- When `C` has pullbacks, a morphism `f : X ⟶ Y` induces a functor `subobject Y ⥤ subobject X`,
by pulling back a monomorphism along `f`. -/
def pullback (f : X ⟶ Y) : subobject Y ⥤ subobject X :=
lower (mono_over.pullback f)
lemma pullback_id (x : subobject X) : (pullback (𝟙 X)).obj x = x :=
begin
apply quotient.induction_on' x,
intro f,
apply quotient.sound,
exact ⟨mono_over.pullback_id.app f⟩,
end
lemma pullback_comp (f : X ⟶ Y) (g : Y ⟶ Z) (x : subobject Z) :
(pullback (f ≫ g)).obj x = (pullback f).obj ((pullback g).obj x) :=
begin
apply quotient.induction_on' x,
intro t,
apply quotient.sound,
refine ⟨(mono_over.pullback_comp _ _).app t⟩,
end
instance (f : X ⟶ Y) : faithful (pullback f) := {}
end pullback
section map
/--
We can map subobjects of `X` to subobjects of `Y`
by post-composition with a monomorphism `f : X ⟶ Y`.
-/
def map (f : X ⟶ Y) [mono f] : subobject X ⥤ subobject Y :=
lower (mono_over.map f)
lemma map_id (x : subobject X) : (map (𝟙 X)).obj x = x :=
begin
apply quotient.induction_on' x,
intro f,
apply quotient.sound,
exact ⟨mono_over.map_id.app f⟩,
end
lemma map_comp (f : X ⟶ Y) (g : Y ⟶ Z) [mono f] [mono g] (x : subobject X) :
(map (f ≫ g)).obj x = (map g).obj ((map f).obj x) :=
begin
apply quotient.induction_on' x,
intro t,
apply quotient.sound,
refine ⟨(mono_over.map_comp _ _).app t⟩,
end
/-- Isomorphic objects have equivalent subobject lattices. -/
def map_iso {A B : C} (e : A ≅ B) : subobject A ≌ subobject B :=
lower_equivalence (mono_over.map_iso e)
/-- In fact, there's a type level bijection between the subobjects of isomorphic objects,
which preserves the order. -/
-- @[simps] here generates a lemma `map_iso_to_order_iso_to_equiv_symm_apply`
-- whose left hand side is not in simp normal form.
def map_iso_to_order_iso (e : X ≅ Y) : subobject X ≃o subobject Y :=
{ to_fun := (map e.hom).obj,
inv_fun := (map e.inv).obj,
left_inv := λ g, by simp_rw [← map_comp, e.hom_inv_id, map_id],
right_inv := λ g, by simp_rw [← map_comp, e.inv_hom_id, map_id],
map_rel_iff' := λ A B, begin
dsimp, fsplit,
{ intro h,
apply_fun (map e.inv).obj at h,
simp_rw [← map_comp, e.hom_inv_id, map_id] at h,
exact h, },
{ intro h,
apply_fun (map e.hom).obj at h,
exact h, },
end }
@[simp] lemma map_iso_to_order_iso_apply (e : X ≅ Y) (P : subobject X) :
map_iso_to_order_iso e P = (map e.hom).obj P :=
rfl
@[simp] lemma map_iso_to_order_iso_symm_apply (e : X ≅ Y) (Q : subobject Y) :
(map_iso_to_order_iso e).symm Q = (map e.inv).obj Q :=
rfl
/-- `map f : subobject X ⥤ subobject Y` is
the left adjoint of `pullback f : subobject Y ⥤ subobject X`. -/
def map_pullback_adj [has_pullbacks C] (f : X ⟶ Y) [mono f] : map f ⊣ pullback f :=
lower_adjunction (mono_over.map_pullback_adj f)
@[simp]
lemma pullback_map_self [has_pullbacks C] (f : X ⟶ Y) [mono f] (g : subobject X) :
(pullback f).obj ((map f).obj g) = g :=
begin
revert g,
apply quotient.ind,
intro g',
apply quotient.sound,
exact ⟨(mono_over.pullback_map_self f).app _⟩,
end
lemma map_pullback [has_pullbacks C]
{X Y Z W : C} {f : X ⟶ Y} {g : X ⟶ Z} {h : Y ⟶ W} {k : Z ⟶ W} [mono h] [mono g]
(comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk f g comm)) (p : subobject Y) :
(map g).obj ((pullback f).obj p) = (pullback k).obj ((map h).obj p) :=
begin
revert p,
apply quotient.ind',
intro a,
apply quotient.sound,
apply thin_skeleton.equiv_of_both_ways,
{ refine mono_over.hom_mk (pullback.lift pullback.fst _ _) (pullback.lift_snd _ _ _),
change _ ≫ a.arrow ≫ h = (pullback.snd ≫ g) ≫ _,
rw [assoc, ← comm, pullback.condition_assoc] },
{ refine mono_over.hom_mk (pullback.lift pullback.fst
(pullback_cone.is_limit.lift' t (pullback.fst ≫ a.arrow) pullback.snd _).1
(pullback_cone.is_limit.lift' _ _ _ _).2.1.symm) _,
{ rw [← pullback.condition, assoc], refl },
{ dsimp, rw [pullback.lift_snd_assoc],
apply (pullback_cone.is_limit.lift' _ _ _ _).2.2 } }
end
end map
section «exists»
variables [has_images C]
/--
The functor from subobjects of `X` to subobjects of `Y` given by
sending the subobject `S` to its "image" under `f`, usually denoted $\exists_f$.
For instance, when `C` is the category of types,
viewing `subobject X` as `set X` this is just `set.image f`.
This functor is left adjoint to the `pullback f` functor (shown in `exists_pullback_adj`)
provided both are defined, and generalises the `map f` functor, again provided it is defined.
-/
def «exists» (f : X ⟶ Y) : subobject X ⥤ subobject Y :=
lower (mono_over.exists f)
/--
When `f : X ⟶ Y` is a monomorphism, `exists f` agrees with `map f`.
-/
lemma exists_iso_map (f : X ⟶ Y) [mono f] : «exists» f = map f :=
lower_iso _ _ (mono_over.exists_iso_map f)
/--
`exists f : subobject X ⥤ subobject Y` is
left adjoint to `pullback f : subobject Y ⥤ subobject X`.
-/
def exists_pullback_adj (f : X ⟶ Y) [has_pullbacks C] : «exists» f ⊣ pullback f :=
lower_adjunction (mono_over.exists_pullback_adj f)
end «exists»
section order_top
instance order_top {X : C} : order_top (subobject X) :=
{ top := quotient.mk' ⊤,
le_top :=
begin
refine quotient.ind' (λ f, _),
exact ⟨mono_over.le_top f⟩,
end,
..subobject.partial_order X}
instance {X : C} : inhabited (subobject X) := ⟨⊤⟩
lemma top_eq_id {B : C} : (⊤ : subobject B) = subobject.mk (𝟙 B) := rfl
lemma map_top (f : X ⟶ Y) [mono f] : (map f).obj ⊤ = quotient.mk' (mono_over.mk' f) :=
quotient.sound' ⟨mono_over.map_top f⟩
section
variables [has_pullbacks C]
lemma pullback_top (f : X ⟶ Y) : (pullback f).obj ⊤ = ⊤ :=
quotient.sound' ⟨mono_over.pullback_top f⟩
lemma pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(pullback f).obj (mk f) = ⊤ :=
quotient.sound' ⟨mono_over.pullback_self f⟩
end
end order_top
section order_bot
variables [has_zero_morphisms C] [has_zero_object C]
local attribute [instance] has_zero_object.has_zero
instance order_bot {X : C} : order_bot (subobject X) :=
{ bot := quotient.mk' ⊥,
bot_le :=
begin
refine quotient.ind' (λ f, _),
exact ⟨mono_over.bot_le f⟩,
end,
..subobject.partial_order X}
lemma bot_eq_zero {B : C} : (⊥ : subobject B) = subobject.mk (0 : 0 ⟶ B) := rfl
lemma map_bot (f : X ⟶ Y) [mono f] : (map f).obj ⊥ = ⊥ :=
quotient.sound' ⟨mono_over.map_bot f⟩
end order_bot
section functor
variable (C)
/-- Sending `X : C` to `subobject X` is a contravariant functor `Cᵒᵖ ⥤ Type`. -/
@[simps]
def functor [has_pullbacks C] : Cᵒᵖ ⥤ Type (max u₁ v₁) :=
{ obj := λ X, subobject X.unop,
map := λ X Y f, (pullback f.unop).obj,
map_id' := λ X, funext pullback_id,
map_comp' := λ X Y Z f g, funext (pullback_comp _ _) }
end functor
section semilattice_inf_top
variables [has_pullbacks C]
/-- The functorial infimum on `mono_over A` descends to an infimum on `subobject A`. -/
def inf {A : C} : subobject A ⥤ subobject A ⥤ subobject A :=
thin_skeleton.map₂ mono_over.inf
lemma inf_le_left {A : C} (f g : subobject A) :
(inf.obj f).obj g ≤ f :=
quotient.induction_on₂' f g (λ a b, ⟨mono_over.inf_le_left _ _⟩)
lemma inf_le_right {A : C} (f g : subobject A) :
(inf.obj f).obj g ≤ g :=
quotient.induction_on₂' f g (λ a b, ⟨mono_over.inf_le_right _ _⟩)
lemma le_inf {A : C} (h f g : subobject A) :
h ≤ f → h ≤ g → h ≤ (inf.obj f).obj g :=
quotient.induction_on₃' h f g
begin
rintros f g h ⟨k⟩ ⟨l⟩,
exact ⟨mono_over.le_inf _ _ _ k l⟩,
end
instance {B : C} : semilattice_inf_top (subobject B) :=
{ inf := λ m n, (inf.obj m).obj n,
inf_le_left := inf_le_left,
inf_le_right := inf_le_right,
le_inf := le_inf,
..subobject.order_top }
lemma inf_eq_map_pullback' {A : C} (f₁ : mono_over A) (f₂ : subobject A) :
(subobject.inf.obj (quotient.mk' f₁)).obj f₂ =
(subobject.map f₁.arrow).obj ((subobject.pullback f₁.arrow).obj f₂) :=
begin
apply quotient.induction_on' f₂,
intro f₂,
refl,
end
lemma inf_eq_map_pullback {A : C} (f₁ : mono_over A) (f₂ : subobject A) :
(quotient.mk' f₁ ⊓ f₂ : subobject A) = (map f₁.arrow).obj ((pullback f₁.arrow).obj f₂) :=
inf_eq_map_pullback' f₁ f₂
lemma prod_eq_inf {A : C} {f₁ f₂ : subobject A} [has_binary_product f₁ f₂] :
(f₁ ⨯ f₂) = f₁ ⊓ f₂ :=
le_antisymm
(_root_.le_inf
(le_of_hom limits.prod.fst)
(le_of_hom limits.prod.snd))
(le_of_hom
(prod.lift
(hom_of_le _root_.inf_le_left)
(hom_of_le _root_.inf_le_right)))
lemma inf_def {B : C} (m m' : subobject B) :
m ⊓ m' = (inf.obj m).obj m' := rfl
/-- `⊓` commutes with pullback. -/
lemma inf_pullback {X Y : C} (g : X ⟶ Y) (f₁ f₂) :
(pullback g).obj (f₁ ⊓ f₂) = (pullback g).obj f₁ ⊓ (pullback g).obj f₂ :=
begin
revert f₁,
apply quotient.ind',
intro f₁,
erw [inf_def, inf_def, inf_eq_map_pullback', inf_eq_map_pullback', ← pullback_comp,
← map_pullback pullback.condition (pullback_is_pullback f₁.arrow g),
← pullback_comp, pullback.condition],
refl,
end
/-- `⊓` commutes with map. -/
lemma inf_map {X Y : C} (g : Y ⟶ X) [mono g] (f₁ f₂) :
(map g).obj (f₁ ⊓ f₂) = (map g).obj f₁ ⊓ (map g).obj f₂ :=
begin
revert f₁,
apply quotient.ind',
intro f₁,
erw [inf_def, inf_def, inf_eq_map_pullback',
inf_eq_map_pullback', ← map_comp],
dsimp,
rw [pullback_comp, pullback_map_self],
end
end semilattice_inf_top
section semilattice_sup
variables [has_images C] [has_binary_coproducts C]
/-- The functorial supremum on `mono_over A` descends to an supremum on `subobject A`. -/
def sup {A : C} : subobject A ⥤ subobject A ⥤ subobject A :=
thin_skeleton.map₂ mono_over.sup
instance {B : C} : semilattice_sup (subobject B) :=
{ sup := λ m n, (sup.obj m).obj n,
le_sup_left := λ m n, quotient.induction_on₂' m n (λ a b, ⟨mono_over.le_sup_left _ _⟩),
le_sup_right := λ m n, quotient.induction_on₂' m n (λ a b, ⟨mono_over.le_sup_right _ _⟩),
sup_le := λ m n k, quotient.induction_on₃' m n k (λ a b c ⟨i⟩ ⟨j⟩, ⟨mono_over.sup_le _ _ _ i j⟩),
..subobject.partial_order B }
section
variables [has_zero_morphisms C] [has_zero_object C]
instance {B : C} : semilattice_sup_bot (subobject B) :=
{ ..subobject.order_bot,
..subobject.semilattice_sup }
end
end semilattice_sup
section lattice
variables [has_pullbacks C] [has_images C] [has_binary_coproducts C]
instance {B : C} : lattice (subobject B) :=
{ ..subobject.semilattice_inf_top,
..subobject.semilattice_sup }
variables [has_zero_morphisms C] [has_zero_object C]
instance {B : C} : bounded_lattice (subobject B) :=
{ ..subobject.semilattice_inf_top,
..subobject.semilattice_sup_bot }
end lattice
end subobject
end category_theory
|
05b1c68f2b839ed5a36276aeb932136bf5b96a99 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/group_theory/semidirect_product.lean | caa14a719ffa3a197447ba11fa3f8a78c6706a9a | [] | 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 | 22,419 | lean | /-
Copyright (c) 2020 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.equiv.mul_add_aut
import Mathlib.logic.function.basic
import Mathlib.group_theory.subgroup
import Mathlib.PostPort
universes u_1 u_2 l u_3 u_4 u_5
namespace Mathlib
/-!
# Semidirect product
This file defines semidirect products of groups, and the canonical maps in and out of the
semidirect product. The semidirect product of `N` and `G` given a hom `φ` from
`φ` from `G` to the automorphism group of `N` is the product of sets with the group
`⟨n₁, g₁⟩ * ⟨n₂, g₂⟩ = ⟨n₁ * φ g₁ n₂, g₁ * g₂⟩`
## Key definitions
There are two homs into the semidirect product `inl : N →* N ⋊[φ] G` and
`inr : G →* N ⋊[φ] G`, and `lift` can be used to define maps `N ⋊[φ] G →* H`
out of the semidirect product given maps `f₁ : N →* H` and `f₂ : G →* H` that satisfy the
condition `∀ n g, f₁ (φ g n) = f₂ g * f₁ n * f₂ g⁻¹`
## Notation
This file introduces the global notation `N ⋊[φ] G` for `semidirect_product N G φ`
## Tags
group, semidirect product
-/
/-- The semidirect product of groups `N` and `G`, given a map `φ` from `G` to the automorphism
group of `N`. It the product of sets with the group operation
`⟨n₁, g₁⟩ * ⟨n₂, g₂⟩ = ⟨n₁ * φ g₁ n₂, g₁ * g₂⟩` -/
structure semidirect_product (N : Type u_1) (G : Type u_2) [group N] [group G] (φ : G →* mul_aut N)
where
left : N
right : G
namespace semidirect_product
protected instance group {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : group (semidirect_product N G φ) :=
group.mk mul_aux mul_assoc_aux one_aux one_mul_aux mul_one_aux inv_aux
(div_inv_monoid.div._default mul_aux mul_assoc_aux one_aux one_mul_aux mul_one_aux inv_aux) mul_left_inv_aux
protected instance inhabited {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : Inhabited (semidirect_product N G φ) :=
{ default := 1 }
@[simp] theorem one_left {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : left 1 = 1 :=
rfl
@[simp] theorem one_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : right 1 = 1 :=
rfl
@[simp] theorem inv_left {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (a : semidirect_product N G φ) : left (a⁻¹) = coe_fn (coe_fn φ (right a⁻¹)) (left a⁻¹) :=
rfl
@[simp] theorem inv_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (a : semidirect_product N G φ) : right (a⁻¹) = (right a⁻¹) :=
rfl
@[simp] theorem mul_left {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (a : semidirect_product N G φ) (b : semidirect_product N G φ) : left (a * b) = left a * coe_fn (coe_fn φ (right a)) (left b) :=
rfl
@[simp] theorem mul_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (a : semidirect_product N G φ) (b : semidirect_product N G φ) : right (a * b) = right a * right b :=
rfl
/-- The canonical map `N →* N ⋊[φ] G` sending `n` to `⟨n, 1⟩` -/
def inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : N →* semidirect_product N G φ :=
monoid_hom.mk (fun (n : N) => mk n 1) sorry sorry
@[simp] theorem left_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (n : N) : left (coe_fn inl n) = n :=
rfl
@[simp] theorem right_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (n : N) : right (coe_fn inl n) = 1 :=
rfl
theorem inl_injective {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : function.injective ⇑inl :=
iff.mpr function.injective_iff_has_left_inverse (Exists.intro left left_inl)
@[simp] theorem inl_inj {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {n₁ : N} {n₂ : N} : coe_fn inl n₁ = coe_fn inl n₂ ↔ n₁ = n₂ :=
function.injective.eq_iff inl_injective
/-- The canonical map `G →* N ⋊[φ] G` sending `g` to `⟨1, g⟩` -/
def inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : G →* semidirect_product N G φ :=
monoid_hom.mk (fun (g : G) => mk 1 g) sorry sorry
@[simp] theorem left_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) : left (coe_fn inr g) = 1 :=
rfl
@[simp] theorem right_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) : right (coe_fn inr g) = g :=
rfl
theorem inr_injective {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : function.injective ⇑inr :=
iff.mpr function.injective_iff_has_left_inverse (Exists.intro right right_inr)
@[simp] theorem inr_inj {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {g₁ : G} {g₂ : G} : coe_fn inr g₁ = coe_fn inr g₂ ↔ g₁ = g₂ :=
function.injective.eq_iff inr_injective
theorem inl_aut {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) (n : N) : coe_fn inl (coe_fn (coe_fn φ g) n) = coe_fn inr g * coe_fn inl n * coe_fn inr (g⁻¹) := sorry
theorem inl_aut_inv {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) (n : N) : coe_fn inl (coe_fn (coe_fn φ g⁻¹) n) = coe_fn inr (g⁻¹) * coe_fn inl n * coe_fn inr g := sorry
@[simp] theorem mk_eq_inl_mul_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) (n : N) : mk n g = coe_fn inl n * coe_fn inr g := sorry
@[simp] theorem inl_left_mul_inr_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (x : semidirect_product N G φ) : coe_fn inl (left x) * coe_fn inr (right x) = x := sorry
/-- The canonical projection map `N ⋊[φ] G →* G`, as a group hom. -/
def right_hom {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : semidirect_product N G φ →* G :=
monoid_hom.mk right sorry sorry
@[simp] theorem right_hom_eq_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : ⇑right_hom = right :=
rfl
@[simp] theorem right_hom_comp_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : monoid_hom.comp right_hom inl = 1 := sorry
@[simp] theorem right_hom_comp_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : monoid_hom.comp right_hom inr = monoid_hom.id G := sorry
@[simp] theorem right_hom_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (n : N) : coe_fn right_hom (coe_fn inl n) = 1 := sorry
@[simp] theorem right_hom_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} (g : G) : coe_fn right_hom (coe_fn inr g) = g := sorry
theorem right_hom_surjective {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : function.surjective ⇑right_hom :=
iff.mpr function.surjective_iff_has_right_inverse (Exists.intro (⇑inr) right_hom_inr)
theorem range_inl_eq_ker_right_hom {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} : monoid_hom.range inl = monoid_hom.ker right_hom := sorry
/-- Define a group hom `N ⋊[φ] G →* H`, by defining maps `N →* H` and `G →* H` -/
def lift {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (f₁ : N →* H) (f₂ : G →* H) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn f₂ g))) f₁) : semidirect_product N G φ →* H :=
monoid_hom.mk (fun (a : semidirect_product N G φ) => coe_fn f₁ (left a) * coe_fn f₂ (right a)) sorry sorry
@[simp] theorem lift_inl {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (f₁ : N →* H) (f₂ : G →* H) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn f₂ g))) f₁) (n : N) : coe_fn (lift f₁ f₂ h) (coe_fn inl n) = coe_fn f₁ n := sorry
@[simp] theorem lift_comp_inl {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (f₁ : N →* H) (f₂ : G →* H) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn f₂ g))) f₁) : monoid_hom.comp (lift f₁ f₂ h) inl = f₁ := sorry
@[simp] theorem lift_inr {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (f₁ : N →* H) (f₂ : G →* H) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn f₂ g))) f₁) (g : G) : coe_fn (lift f₁ f₂ h) (coe_fn inr g) = coe_fn f₂ g := sorry
@[simp] theorem lift_comp_inr {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (f₁ : N →* H) (f₂ : G →* H) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn f₂ g))) f₁) : monoid_hom.comp (lift f₁ f₂ h) inr = f₂ := sorry
theorem lift_unique {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} (F : semidirect_product N G φ →* H) : F =
lift (monoid_hom.comp F inl) (monoid_hom.comp F inr)
fun (_x : G) =>
monoid_hom.ext
fun (x : N) =>
eq.mpr
(id
(Eq.trans
(Eq.trans
(Eq.trans
((fun (a a_1 : H) (e_1 : a = a_1) (ᾰ ᾰ_1 : H) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2)
(coe_fn (monoid_hom.comp (monoid_hom.comp F inl) (mul_equiv.to_monoid_hom (coe_fn φ _x))) x)
(coe_fn F (coe_fn inr _x) * coe_fn F (coe_fn inl x) * (coe_fn F (coe_fn inr _x)⁻¹))
(Eq.trans
(Eq.trans
(Eq.trans
(Eq.trans
(Eq.trans
(Eq.trans
(congr_fun
(monoid_hom.coe_comp (monoid_hom.comp F inl)
(mul_equiv.to_monoid_hom (coe_fn φ _x)))
x)
((fun (f f_1 : N → H) (e_1 : f = f_1) (g g_1 : N → N) (e_2 : g = g_1) (ᾰ ᾰ_1 : N)
(e_3 : ᾰ = ᾰ_1) => congr (congr (congr_arg function.comp e_1) e_2) e_3)
(⇑(monoid_hom.comp F inl)) (⇑F ∘ ⇑inl) (monoid_hom.coe_comp F inl)
(⇑(mul_equiv.to_monoid_hom (coe_fn φ _x))) (⇑(coe_fn φ _x))
(mul_equiv.coe_to_monoid_hom (coe_fn φ _x)) x x (Eq.refl x)))
(function.comp_app (⇑F ∘ ⇑inl) (⇑(coe_fn φ _x)) x))
(function.comp_app (⇑F) (⇑inl) (coe_fn (coe_fn φ _x) x)))
((fun (x x_1 : semidirect_product N G φ →* H) (e_1 : x = x_1)
(ᾰ ᾰ_1 : semidirect_product N G φ) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg coe_fn e_1) e_2)
F F (Eq.refl F) (coe_fn inl (coe_fn (coe_fn φ _x) x))
(coe_fn inr _x * coe_fn inl x * (coe_fn inr _x⁻¹))
(Eq.trans (inl_aut _x x)
((fun (ᾰ ᾰ_1 : semidirect_product N G φ) (e_2 : ᾰ = ᾰ_1)
(ᾰ_2 ᾰ_3 : semidirect_product N G φ) (e_3 : ᾰ_2 = ᾰ_3) =>
congr (congr_arg Mul.mul e_2) e_3)
(coe_fn inr _x * coe_fn inl x) (coe_fn inr _x * coe_fn inl x)
(Eq.refl (coe_fn inr _x * coe_fn inl x)) (coe_fn inr (_x⁻¹)) (coe_fn inr _x⁻¹)
(monoid_hom.map_inv inr _x)))))
(monoid_hom.map_mul_inv F (coe_fn inr _x * coe_fn inl x) (coe_fn inr _x)))
((fun (ᾰ ᾰ_1 : H) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : H) (e_3 : ᾰ_2 = ᾰ_3) =>
congr (congr_arg Mul.mul e_2) e_3)
(coe_fn F (coe_fn inr _x * coe_fn inl x)) (coe_fn F (coe_fn inr _x) * coe_fn F (coe_fn inl x))
(monoid_hom.map_mul F (coe_fn inr _x) (coe_fn inl x)) (coe_fn F (coe_fn inr _x)⁻¹)
(coe_fn F (coe_fn inr _x)⁻¹) (Eq.refl (coe_fn F (coe_fn inr _x)⁻¹))))
(coe_fn
(monoid_hom.comp
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn (monoid_hom.comp F inr) _x)))
(monoid_hom.comp F inl))
x)
(coe_fn F (coe_fn inr _x) * coe_fn F (coe_fn inl x) * (coe_fn F (coe_fn inr _x)⁻¹))
(Eq.trans
(Eq.trans
(Eq.trans
(Eq.trans
(Eq.trans
((fun (x x_1 : N →* H) (e_1 : x = x_1) (ᾰ ᾰ_1 : N) (e_2 : ᾰ = ᾰ_1) =>
congr (congr_arg coe_fn e_1) e_2)
(monoid_hom.comp
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn (monoid_hom.comp F inr) _x)))
(monoid_hom.comp F inl))
(monoid_hom.comp
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x))))
(monoid_hom.comp F inl))
((fun (hnp hnp_1 : H →* H) (e_1 : hnp = hnp_1) (hmn hmn_1 : N →* H)
(e_2 : hmn = hmn_1) => congr (congr_arg monoid_hom.comp e_1) e_2)
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn (monoid_hom.comp F inr) _x)))
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x))))
((fun (h h_1 : H ≃* H) (e_1 : h = h_1) => congr_arg mul_equiv.to_monoid_hom e_1)
(coe_fn mul_aut.conj (coe_fn (monoid_hom.comp F inr) _x))
(coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))
((fun (x x_1 : H →* mul_aut H) (e_1 : x = x_1) (ᾰ ᾰ_1 : H) (e_2 : ᾰ = ᾰ_1) =>
congr (congr_arg coe_fn e_1) e_2)
mul_aut.conj mul_aut.conj (Eq.refl mul_aut.conj)
(coe_fn (monoid_hom.comp F inr) _x) (coe_fn F (coe_fn inr _x))
(Eq.trans (congr_fun (monoid_hom.coe_comp F inr) _x)
(function.comp_app (⇑F) (⇑inr) _x))))
(monoid_hom.comp F inl) (monoid_hom.comp F inl) (Eq.refl (monoid_hom.comp F inl)))
x x (Eq.refl x))
(congr_fun
(monoid_hom.coe_comp
(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x))))
(monoid_hom.comp F inl))
x))
((fun (f f_1 : H → H) (e_1 : f = f_1) (g g_1 : N → H) (e_2 : g = g_1) (ᾰ ᾰ_1 : N)
(e_3 : ᾰ = ᾰ_1) => congr (congr (congr_arg function.comp e_1) e_2) e_3)
(⇑(mul_equiv.to_monoid_hom (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))))
(⇑(coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x))))
(mul_equiv.coe_to_monoid_hom (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x))))
(⇑(monoid_hom.comp F inl)) (⇑F ∘ ⇑inl) (monoid_hom.coe_comp F inl) x x (Eq.refl x)))
(function.comp_app (⇑(coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))) (⇑F ∘ ⇑inl) x))
((fun (x x_1 : H ≃* H) (e_1 : x = x_1) (ᾰ ᾰ_1 : H) (e_2 : ᾰ = ᾰ_1) =>
congr (congr_arg coe_fn e_1) e_2)
(coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))
(coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))
(Eq.refl (coe_fn mul_aut.conj (coe_fn F (coe_fn inr _x)))) (function.comp (⇑F) (⇑inl) x)
(coe_fn F (coe_fn inl x)) (function.comp_app (⇑F) (⇑inl) x)))
(mul_aut.conj_apply (coe_fn F (coe_fn inr _x)) (coe_fn F (coe_fn inl x)))))
(propext (mul_left_inj (coe_fn F (coe_fn inr _x)⁻¹))))
(propext (mul_left_inj (coe_fn F (coe_fn inl x)))))
(propext (eq_self_iff_true (coe_fn F (coe_fn inr _x))))))
trivial := sorry
/-- Two maps out of the semidirect product are equal if they're equal after composition
with both `inl` and `inr` -/
theorem hom_ext {N : Type u_1} {G : Type u_2} {H : Type u_3} [group N] [group G] [group H] {φ : G →* mul_aut N} {f : semidirect_product N G φ →* H} {g : semidirect_product N G φ →* H} (hl : monoid_hom.comp f inl = monoid_hom.comp g inl) (hr : monoid_hom.comp f inr = monoid_hom.comp g inr) : f = g := sorry
/-- Define a map from `N ⋊[φ] G` to `N₁ ⋊[φ₁] G₁` given maps `N →* N₁` and `G →* G₁` that
satisfy a commutativity condition `∀ n g, f₁ (φ g n) = φ₁ (f₂ g) (f₁ n)`. -/
def map {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) : semidirect_product N G φ →* semidirect_product N₁ G₁ φ₁ :=
monoid_hom.mk (fun (x : semidirect_product N G φ) => mk (coe_fn f₁ (left x)) (coe_fn f₂ (right x))) sorry sorry
@[simp] theorem map_left {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) (g : semidirect_product N G φ) : left (coe_fn (map f₁ f₂ h) g) = coe_fn f₁ (left g) :=
rfl
@[simp] theorem map_right {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) (g : semidirect_product N G φ) : right (coe_fn (map f₁ f₂ h) g) = coe_fn f₂ (right g) :=
rfl
@[simp] theorem right_hom_comp_map {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) : monoid_hom.comp right_hom (map f₁ f₂ h) = monoid_hom.comp f₂ right_hom :=
rfl
@[simp] theorem map_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) (n : N) : coe_fn (map f₁ f₂ h) (coe_fn inl n) = coe_fn inl (coe_fn f₁ n) := sorry
@[simp] theorem map_comp_inl {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) : monoid_hom.comp (map f₁ f₂ h) inl = monoid_hom.comp inl f₁ := sorry
@[simp] theorem map_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) (g : G) : coe_fn (map f₁ f₂ h) (coe_fn inr g) = coe_fn inr (coe_fn f₂ g) := sorry
@[simp] theorem map_comp_inr {N : Type u_1} {G : Type u_2} [group N] [group G] {φ : G →* mul_aut N} {N₁ : Type u_4} {G₁ : Type u_5} [group N₁] [group G₁] {φ₁ : G₁ →* mul_aut N₁} (f₁ : N →* N₁) (f₂ : G →* G₁) (h : ∀ (g : G),
monoid_hom.comp f₁ (mul_equiv.to_monoid_hom (coe_fn φ g)) =
monoid_hom.comp (mul_equiv.to_monoid_hom (coe_fn φ₁ (coe_fn f₂ g))) f₁) : monoid_hom.comp (map f₁ f₂ h) inr = monoid_hom.comp inr f₂ := sorry
|
b6b66118d14db6afa6ca5cdf7f66cd96e7bfb5d2 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/data/set_like/basic.lean | 95229ddf8ee2433b80b07cb25940555b355e018d | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,832 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import data.set.basic
import tactic.monotonicity.basic
/-!
# Typeclass for types with a set-like extensionality property
The `has_mem` typeclass is used to let terms of a type have elements.
Many instances of `has_mem` have a set-like extensionality property:
things are equal iff they have the same elements. The `set_like`
typeclass provides a unified interface to define a `has_mem` that is
extensional in this way.
The main use of `set_like` is for algebraic subobjects (such as
`submonoid` and `submodule`), whose non-proof data consists only of a
carrier set. In such a situation, the projection to the carrier set
is injective.
In general, a type `A` is `set_like` with elements of type `B` if it
has an injective map to `set B`. This module provides standard
boilerplate for every `set_like`: a `coe_sort`, a `coe` to set, a
`partial_order`, and various extensionality and simp lemmas.
A typical subobject should be declared as:
```
structure my_subobject (X : Type*) :=
(carrier : set X)
(op_mem : ∀ {x : X}, x ∈ carrier → sorry ∈ carrier)
namespace my_subobject
variables (X : Type*)
instance : set_like (my_subobject X) X :=
⟨sub_mul_action.carrier, λ p q h, by cases p; cases q; congr'⟩
@[simp] lemma mem_carrier {p : my_subobject X} : x ∈ p.carrier ↔ x ∈ (p : set X) := iff.rfl
@[ext] theorem ext {p q : my_subobject X} (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := set_like.ext h
/-- Copy of a `my_subobject` with a new `carrier` equal to the old one. Useful to fix definitional
equalities. See Note [range copy pattern]. -/
protected def copy (p : my_subobject X) (s : set X) (hs : s = ↑p) : my_subobject X :=
{ carrier := s,
op_mem' := hs.symm ▸ p.op_mem' }
@[simp] lemma coe_copy (p : my_subobject X) (s : set X) (hs : s = ↑p) :
(p.copy s hs : set X) = s := rfl
lemma copy_eq (p : my_subobject X) (s : set X) (hs : s = ↑p) : p.copy s hs = p :=
set_like.coe_injective hs
end my_subobject
```
An alternative to `set_like` could have been an extensional `has_mem` typeclass:
```
class has_ext_mem (α : out_param $ Type u) (β : Type v) extends has_mem α β :=
(ext_iff : ∀ {s t : β}, s = t ↔ ∀ (x : α), x ∈ s ↔ x ∈ t)
```
While this is equivalent, `set_like` conveniently uses a carrier set projection directly.
## Tags
subobjects
-/
/-- A class to indicate that there is a canonical injection between `A` and `set B`.
This has the effect of giving terms of `A` elements of type `B` (through a `has_mem`
instance) and a compatible coercion to `Type*` as a subtype.
Note: if `set_like.coe` is a projection, implementers should create a simp lemma such as
```
@[simp] lemma mem_carrier {p : my_subobject X} : x ∈ p.carrier ↔ x ∈ (p : set X) := iff.rfl
```
to normalize terms.
-/
@[protect_proj]
class set_like (A : Type*) (B : out_param $ Type*) :=
(coe : A → set B)
(coe_injective' : function.injective coe)
namespace set_like
variables {A : Type*} {B : Type*} [i : set_like A B]
include i
instance : has_coe_t A (set B) := ⟨set_like.coe⟩
@[priority 100]
instance : has_mem B A := ⟨λ x p, x ∈ (p : set B)⟩
-- `dangerous_instance` does not know that `B` is used only as an `out_param`
@[nolint dangerous_instance, priority 100]
instance : has_coe_to_sort A := ⟨_, λ p, {x : B // x ∈ p}⟩
variables (p q : A)
@[simp, norm_cast] theorem coe_sort_coe : ↥(p : set B) = p := rfl
variables {p q}
protected theorem «exists» {q : p → Prop} :
(∃ x, q x) ↔ (∃ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.exists
protected theorem «forall» {q : p → Prop} :
(∀ x, q x) ↔ (∀ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.forall
theorem coe_injective : function.injective (coe : A → set B) :=
λ x y h, set_like.coe_injective' h
@[simp, norm_cast] theorem coe_set_eq : (p : set B) = q ↔ p = q := coe_injective.eq_iff
theorem ext' (h : (p : set B) = q) : p = q := coe_injective h
theorem ext'_iff : p = q ↔ (p : set B) = q := coe_set_eq.symm
/-- Note: implementers of `set_like` must copy this lemma in order to tag it with `@[ext]`. -/
theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := coe_injective $ set.ext h
theorem ext_iff : p = q ↔ (∀ x, x ∈ p ↔ x ∈ q) := coe_injective.eq_iff.symm.trans set.ext_iff
@[simp] theorem mem_coe {x : B} : x ∈ (p : set B) ↔ x ∈ p := iff.rfl
@[simp, norm_cast] lemma coe_eq_coe {x y : p} : (x : B) = y ↔ x = y := subtype.ext_iff_val.symm
@[simp, norm_cast] lemma coe_mk (x : B) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : B) = x := rfl
@[simp] lemma coe_mem (x : p) : (x : B) ∈ p := x.2
@[simp] protected lemma eta (x : p) (hx : (x : B) ∈ p) : (⟨x, hx⟩ : p) = x := subtype.eta x hx
-- `dangerous_instance` does not know that `B` is used only as an `out_param`
@[nolint dangerous_instance, priority 100]
instance : partial_order A :=
{ le := λ H K, ∀ ⦃x⦄, x ∈ H → x ∈ K,
.. partial_order.lift (coe : A → set B) coe_injective }
lemma le_def {S T : A} : S ≤ T ↔ ∀ ⦃x : B⦄, x ∈ S → x ∈ T := iff.rfl
@[simp, norm_cast]
lemma coe_subset_coe {S T : A} : (S : set B) ⊆ T ↔ S ≤ T := iff.rfl
@[mono] lemma coe_mono : monotone (coe : A → set B) := λ a b, coe_subset_coe.mpr
@[simp, norm_cast]
lemma coe_ssubset_coe {S T : A} : (S : set B) ⊂ T ↔ S < T := iff.rfl
@[mono] lemma coe_strict_mono : strict_mono (coe : A → set B) := λ a b, coe_ssubset_coe.mpr
lemma not_le_iff_exists : ¬(p ≤ q) ↔ ∃ x ∈ p, x ∉ q := set.not_subset
lemma exists_of_lt : p < q → ∃ x ∈ q, x ∉ p := set.exists_of_ssubset
lemma lt_iff_le_and_exists : p < q ↔ p ≤ q ∧ ∃ x ∈ q, x ∉ p :=
by rw [lt_iff_le_not_le, not_le_iff_exists]
end set_like
|
2b7cc0edd749a6208f77105830719509c03d340a | a9fe717b93ccfa4b2e64faeb24f96dfefb390240 | /nat/form.lean | 17506b0d4b94cbd743b2378ed435739d093ea3c6 | [] | no_license | skbaek/omega | ab1f4a6daadfc8c855f14c39d9459ab841527141 | 715e384ed14e8eb177a326700066e7c98269e078 | refs/heads/master | 1,588,000,876,352 | 1,552,645,917,000 | 1,552,645,917,000 | 174,442,914 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,446 | lean | import .preterm ..simp_omega ..logic
namespace nat
@[derive has_reflect]
inductive form
| eq : preterm → preterm → form
| le : preterm → preterm → form
| not : form → form
| or : form → form → form
| and : form → form → form
notation x `=*` y := form.eq x y
notation x `≤*` y := form.le x y
notation `¬*` p := form.not p
notation p `∨*` q := form.or p q
notation p `∧*` q := form.and p q
-- | (t =* s) :=
-- | (t ≤* s) :=
-- | (¬* p) :=
-- | (p ∨* q) :=
-- | (p ∧* q) :=
def valuation.cons (i : nat) (v : nat → nat) : nat → nat
| 0 := i
| (k+1) := v k
notation i `::` v := valuation.cons i v
open tactic
namespace form
@[omega] def holds (v : nat → nat) : form → Prop
| (t =* s) := t.val v = s.val v
| (t ≤* s) := t.val v ≤ s.val v
| (¬* p) := ¬ p.holds
| (p ∨* q) := p.holds ∨ q.holds
| (p ∧* q) := p.holds ∧ q.holds
end form
@[omega] def uniclo (p : form) : (nat → nat) → nat → Prop
| v 0 := p.holds v
| v (k+1) := ∀ i : nat, uniclo (i::v) k
namespace form
def neg_free : form → Prop
| (t =* s) := true
| (t ≤* s) := true
| (p ∨* q) := neg_free p ∧ neg_free q
| (p ∧* q) := neg_free p ∧ neg_free q
| _ := false
def sub_free : form → Prop
| (t =* s) := t.sub_free ∧ s.sub_free
| (t ≤* s) := t.sub_free ∧ s.sub_free
| (¬* p) := p.sub_free
| (p ∨* q) := p.sub_free ∧ q.sub_free
| (p ∧* q) := p.sub_free ∧ q.sub_free
def fresh_idx : form → nat
| (t =* s) := max t.fresh_idx s.fresh_idx
| (t ≤* s) := max t.fresh_idx s.fresh_idx
| (¬* p) := p.fresh_idx
| (p ∨* q) := max p.fresh_idx q.fresh_idx
| (p ∧* q) := max p.fresh_idx q.fresh_idx
def holds_constant {v w : nat → nat} :
∀ p : form,
( (∀ x < p.fresh_idx, v x = w x) →
(p.holds v ↔ p.holds w) )
| (t =* s) h1 :=
begin
simp_omega, apply pred_mono_2;
apply preterm.val_constant;
intros x h2; apply h1 _ (lt_of_lt_of_le h2 _),
apply le_max_left, apply le_max_right
end
| (t ≤* s) h1 :=
begin
simp_omega, apply pred_mono_2;
apply preterm.val_constant;
intros x h2; apply h1 _ (lt_of_lt_of_le h2 _),
apply le_max_left, apply le_max_right
end
| (¬* p) h1 :=
begin
apply not_iff_not_of_iff,
apply holds_constant p h1
end
| (p ∨* q) h1 :=
begin
simp_omega, apply or_iff_or;
apply holds_constant;
intros x h2; apply h1 _ (lt_of_lt_of_le h2 _),
apply le_max_left, apply le_max_right
end
| (p ∧* q) h1 :=
begin
simp_omega, apply and_iff_and;
apply holds_constant;
intros x h2; apply h1 _ (lt_of_lt_of_le h2 _),
apply le_max_left, apply le_max_right
end
--def uniclo (p : form) : Prop :=
--uniclo_core p p.fresh_idx (λ _, 0)
def valid (p : form) : Prop :=
∀ v, holds v p
def sat (p : form) : Prop :=
∃ v, holds v p
def implies (p q : form) : Prop :=
∀ v, (holds v p → holds v q)
def equiv (p q : form) : Prop :=
∀ v, (holds v p ↔ holds v q)
lemma sat_of_implies_of_sat {p q} :
implies p q → sat p → sat q :=
begin intros h1 h2, apply exists_of_exists h1 h2 end
lemma sat_or {p q : form} :
sat (p ∨* q) ↔ sat p ∨ sat q :=
begin
constructor; intro h1,
{ cases h1 with v h1, cases h1 with h1 h1;
[left,right]; refine ⟨v,_⟩; assumption },
{ cases h1 with h1 h1; cases h1 with v h1;
refine ⟨v,_⟩; [left,right]; assumption }
end
def unsat (p : form) : Prop := ¬ sat p
def repr : form → string
| (t =* s) := "(" ++ t.repr ++ " = " ++ s.repr ++ ")"
| (t ≤* s) := "(" ++ t.repr ++ " ≤ " ++ s.repr ++ ")"
| (¬* p) := "¬" ++ p.repr
| (p ∨* q) := "(" ++ p.repr ++ " ∨ " ++ q.repr ++ ")"
| (p ∧* q) := "(" ++ p.repr ++ " ∧ " ++ q.repr ++ ")"
instance has_repr : has_repr form := ⟨repr⟩
meta instance has_to_format : has_to_format form := ⟨λ x, x.repr⟩
end form
lemma uniclo_of_valid {p : form} :
∀ {m v}, p.valid → uniclo p v m
| 0 v h1 := h1 _
| (m+1) v h1 := λ i, uniclo_of_valid h1
--lemma uniclo_of_valid {p : form} (h : p.valid) : p.uniclo :=
--uniclo_core_of_valid h
lemma valid_of_unsat_not {p : form} : (¬*p).unsat → p.valid :=
begin
simp only [form.sat, form.unsat, form.valid, form.holds],
rw classical.not_exists_not, intro h, assumption
end
open tactic
meta def form.induce (t : tactic unit := skip) : tactic unit :=
`[ intro p, induction p with t s t s p ih p q ihp ihq p q ihp ihq; t]
end nat
|
dc1a6c7e13f922fcd69dec38fa32e41a60f59980 | 7056dcccc8ffc1e6f9f533f3fed8ef16ff9fcc1e | /tests/lean/Delaborator.lean | 9816a74a056de75fe6712448bbfc452e302aadb8 | [
"Apache-2.0"
] | permissive | graydon/lean4 | d73f60d0aa60e64e8d15674538bb5921aad5a4d1 | ec9f4b579dc6b4a20407d2735f8ad91de29bfdb5 | refs/heads/master | 1,650,641,753,817 | 1,587,651,832,000 | 1,587,733,896,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,096 | lean | import Init.Lean
open Lean
open Lean.Elab
open Lean.Elab.Term
open Lean.Format
def check (stx : TermElabM Syntax) (optionsPerPos : OptionsPerPos := {}) : TermElabM Unit := do
stx ← stx;
opts ← getOptions;
e ← elabTermAndSynthesize stx none <* throwErrorIfErrors;
stx' ← liftMetaM stx $ delab e opts optionsPerPos;
dbgTrace $ toString stx';
e' ← elabTermAndSynthesize stx' none <* throwErrorIfErrors;
unlessM (isDefEq stx e e') $
throwError stx (fmt "failed to round-trip" ++ line ++ fmt e ++ line ++ fmt e')
-- #eval check `(?m) -- fails round-trip
#eval check `(Sort)
#eval check `(Type)
#eval check `(Type 0)
#eval check `(Type 1)
-- can't add a new universe variable inside a term...
#eval check `(Type _)
#eval check `(Type (_ + 2))
#eval check `(Nat)
#eval check `(List Nat)
#eval check `(id Nat)
section
set_option pp.explicit true
#eval check `(List Nat)
#eval check `(id Nat)
end
section
set_option pp.universes true
#eval check `(List Nat)
#eval check `(id Nat)
end
#eval check `(id (id Nat)) (RBMap.empty.insert 4 $ KVMap.empty.insert `pp.explicit true)
-- specify the expected type of `a` in a way that is not erased by the delaborator
def typeAs.{u} (α : Type u) (a : α) := ()
#eval check `(fun (a : Nat) => a)
#eval check `(fun (a b : Nat) => a)
#eval check `(fun (a : Nat) (b : Bool) => a)
#eval check `(fun {a b : Nat} => a)
-- implicit lambdas work as long as the expected type is preserved
#eval check `(typeAs ({α : Type} → (a : α) → α) (fun a => a))
section set_option pp.explicit true
#eval check `(fun {α : Type} [HasToString α] (a : α) => toString a)
end
#eval check `((α : Type) → α)
#eval check `((α β : Type) → α) -- group
#eval check `((α β : Type) → Type) -- don't group
#eval check `((α : Type) → (a : α) → α)
#eval check `((α : Type) → (a : α) → a = a)
#eval check `({α : Type} → α)
#eval check `({α : Type} → [HasToString α] → α)
-- TODO: hide `ofNat`
#eval check `(0)
#eval check `(1)
#eval check `(42)
#eval check `("hi")
#eval check `((1,2).fst)
#eval check `(1 < 2 || true)
|
79b1835d6410b236bd606378e6f381e8b7962dd7 | d751a70f46ed26dc0111a87f5bbe83e5c6648904 | /Code/src/inst/network/graph.lean | 35b0b3e8f4e35eb7ed6483b36225c0ce9464b00f | [] | no_license | marcusrossel/bachelors-thesis | 92cb12ae8436c10fbfab9bfe4929a0081e615b37 | d1ec2c2b5c3c6700a506f2e3cc93f1160e44b422 | refs/heads/main | 1,682,873,547,703 | 1,619,795,735,000 | 1,619,795,735,000 | 306,041,494 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,245 | lean | import lgraph
import inst.network.ids
open reactor
open reactor.ports
-- An edge in a reactor network graph connects reactors' ports.
structure inst.network.graph.edge :=
(src : port.id)
(dst : port.id)
-- Reactor network graph edges are directed.
instance inst.graph.lgraph_edge : lgraph.edge inst.network.graph.edge reactor.id :=
{ src := (λ e, e.src.rtr), dst := (λ e, e.dst.rtr) }
-- Cf. inst/primitives.lean
variables (υ : Type*) [decidable_eq υ]
-- An instantaneous reactor network graph is an L-graph of reactors, identified by reactor-IDs
-- and connected by the edges define above.
def inst.network.graph : Type* := lgraph reactor.id (reactor υ) inst.network.graph.edge
namespace inst
namespace network
namespace graph
variable {υ}
-- The reactor in a network graph, that is associated with a given reactor ID.
noncomputable def rtr (η : network.graph υ) (i : reactor.id) : reactor υ := η.data i
-- The reaction in a network graph, that is associated with a given reaction ID.
noncomputable def rcn (η : graph υ) (i : reaction.id) : reaction υ :=
(η.rtr i.rtr).reactions i.rcn
-- The port in a network graph, that is associated with a given role and port ID.
noncomputable def port (η : graph υ) (r : ports.role) (i : port.id) : option υ :=
(η.rtr i.rtr).port r i.prt
-- The dependencies of a given reaction for a given role, as proper `port.id`s.
noncomputable def deps (η : graph υ) (i : reaction.id) (r : ports.role) : finset port.id :=
((η.rcn i).deps r).image (port.id.mk i.rtr)
-- A port is part of a reaction's dependencies iff this holds at the reaction level.
lemma mem_deps_iff_mem_rcn_deps {η : graph υ} {i : reaction.id} {r : ports.role} {p : port.id} :
p ∈ (η.deps i r) ↔ i.rtr = p.rtr ∧ p.prt ∈ (η.rcn i).deps r :=
begin
unfold deps,
rw finset.mem_image,
split,
{
intro h,
obtain ⟨x, hₓ, hₚ⟩ := h,
simp [←hₚ, hₓ],
},
{
intro h,
rw h.left,
existsi p.prt,
existsi h.right,
ext ; refl
}
end
-- The edges in a network graph, that are connected to a given output port.
noncomputable def eₒ (η : graph υ) (p : port.id) : finset graph.edge :=
η.edges.filter (λ e : graph.edge, e.src = p)
-- An edge is in `eₒ p` if its source is `p`.
lemma mem_eₒ {η : graph υ} (p : port.id) {e : edge} : e ∈ η.eₒ p ↔ e ∈ η.edges ∧ e.src = p :=
by simp [eₒ, finset.mem_filter]
-- Reactor network graphs' equality is non-constructively decidable.
noncomputable instance dec_eq : decidable_eq graph.edge := classical.dec_eq _
-- A reactor is a member of a network graph if the graph contains an ID that maps to it.
instance rtr_mem : has_mem (reactor υ) (graph υ) := {mem := λ rtr η, ∃ i, η.data i = rtr}
-- Reactor network graphs are equivalent if they are structurally the same and only contain equivalent reactors.
instance equiv : has_equiv (graph υ) :=
⟨λ η η',
η.edges = η'.edges ∧
η.ids = η'.ids ∧
∀ i, (η.rtr i) ≈ (η'.rtr i)
⟩
-- Network graph equivalence is reflexive.
@[refl]
lemma equiv_refl (η : graph υ) : η ≈ η := by simp [(≈)]
-- Network graph equivalence is symmetric.
@[symm]
lemma equiv_symm {η η' : graph υ} (h : η ≈ η') : η' ≈ η :=
by { simp only [(≈)] at h ⊢, simp [h] }
-- Network graph equivalence is transitive.
@[trans]
lemma equiv_trans {η₁ η₂ η₃ : graph υ} (h₁₂ : η₁ ≈ η₂) (h₂₃ : η₂ ≈ η₃) : η₁ ≈ η₃ :=
by { simp [(≈)] at ⊢ h₁₂ h₂₃, simp [h₁₂, h₂₃] }
-- The proposition, that for all input ports (`i`) in `η` the number of edges that end in `i` is ≤ 1.
def has_unique_port_ins (η : graph υ) : Prop :=
∀ e e' : edge, (e ∈ η.edges) → (e' ∈ η.edges) → e ≠ e' → e.dst ≠ e'.dst
-- The property of having unique port ins only depends on a network graph's edges.
theorem eq_edges_unique_port_ins {η η' : graph υ} (hₑ : η.edges = η'.edges) (hᵤ : η.has_unique_port_ins) :
η'.has_unique_port_ins :=
begin
unfold has_unique_port_ins,
intros e e',
unfold has_unique_port_ins at hᵤ,
simp [(∈)] at hᵤ ⊢,
rw ←hₑ,
apply hᵤ
end
-- Updates the reactor associated with the given reactor ID.
noncomputable def update_reactor (η : graph υ) (i : reactor.id) (rtr : reactor υ) : graph υ :=
η.update_data i rtr
-- Updating a network graph with an equivalent reactor produces an equivalent network graph.
lemma update_reactor_equiv {η : graph υ} {i : reactor.id} {rtr : reactor υ} (h : η.rtr i ≈ rtr) :
(η.update_reactor i rtr) ≈ η :=
begin
simp only [update_reactor, lgraph.update_data, (≈), graph.rtr] at h ⊢,
repeat { split },
intro x,
by_cases hc : x = i,
{ rw hc, simp [function.update_same, h] },
simp [function.update_noteq hc, h]
end
-- Accessing the value of an updated reactor returns exactly the updated value.
@[simp]
lemma update_reactor_eq_rtr (η : graph υ) (i : reactor.id) (rtr' : reactor υ) :
(η.update_reactor i rtr').rtr i = rtr' :=
by simp [update_reactor, rtr]
-- Accessing the value of a non-updated reactor returns the same value as before.
@[simp]
lemma update_reactor_ne_rtr (η : graph υ) {i i' : reactor.id} (rtr' : reactor υ) (h : i ≠ i') :
(η.update_reactor i rtr').rtr i' = η.rtr i' :=
by simp [update_reactor, rtr, lgraph.update_data_ne η rtr' (ne.symm h)]
-- Updating the same reactor twice retains only the last update.
@[simp]
lemma update_reactor_same (η : graph υ) (i : reactor.id) (rtr rtr' : reactor υ) :
(η.update_reactor i rtr).update_reactor i rtr' = η.update_reactor i rtr' :=
by simp [update_reactor, lgraph.update_data]
-- Updating different reactors is commutative.
lemma update_reactor_comm (η : graph υ) {i i' : reactor.id} (rtr rtr' : reactor υ) (h : i ≠ i') :
(η.update_reactor i rtr).update_reactor i' rtr' = (η.update_reactor i' rtr').update_reactor i rtr :=
by simp [update_reactor, lgraph.update_data_comm _ _ _ h]
-- Relative reactor equality is retained when updating with a relatively equal reactor.
lemma update_reactor_eq_rel_to {η : graph υ} {i : reaction.id} {rtr' : reactor υ} (h : η.rtr i.rtr =i.rcn= rtr') :
(η.update_reactor i.rtr rtr').rtr i.rtr =i.rcn= η.rtr i.rtr :=
begin
simp only [update_reactor_eq_rtr],
exact reactor.eq_rel_to_symm h
end
-- Updates the port associated with the given role and port ID.
noncomputable def update_port (η : graph υ) (r : ports.role) (p : port.id) (v : option υ) : graph υ :=
η.update_reactor p.rtr ((η.rtr p.rtr).update r p.prt v)
-- Updating a port on one side of a reactor does not change the ports on the other side.
@[simp]
lemma update_port_opposite_eq (η : graph υ) (r : ports.role) (p : port.id) (v : option υ) :
(η.update_port r p v).port r.opposite = η.port r.opposite :=
begin
unfold update_port,
ext1,
cases r ; {
by_cases hc : x.rtr = p.rtr,
all_goals { simp only [ports.role.opposite, update, port, reactor.port] },
simp only [hc, update_reactor_eq_rtr],
rw update_reactor_ne_rtr η _ (ne.symm hc),
}
end
-- Updating different ports is commutative.
lemma update_port_comm (η : graph υ) (r : ports.role) {p p' : port.id} (v v' : option υ) (h : p ≠ p') :
(η.update_port r p v).update_port r p' v' = (η.update_port r p' v').update_port r p v :=
begin
unfold update_port,
by_cases hc : p.rtr = p'.rtr,
{
rw hc,
repeat { rw update_reactor_same },
unfold update_reactor rtr,
apply congr,
refl,
repeat { rw lgraph.update_data_same },
rw reactor.update_comm,
cases p,
cases p',
intro h',
apply_assumption,
ext,
assumption',
},
rw [update_reactor_comm _ _ _ hc, update_reactor_ne_rtr _ _ hc, update_reactor_ne_rtr _ _ (ne.symm hc)],
end
-- Updating a port in a network graph produces an equivalent network graph.
lemma update_port_equiv (η : graph υ) (r : ports.role) (p : port.id) (v : option υ) :
η ≈ (η.update_port r p v) :=
begin
unfold update_port,
have h, from reactor.update_equiv (η.rtr p.rtr) r p.prt v,
symmetry,
exact update_reactor_equiv (reactor.equiv_symm h)
end
-- Updating an input port that is not a dependency of a given reaction produces an equal reactor relative to that reaction.
lemma update_input_eq_rel_to {η : graph υ} {i : reaction.id} {p : ℕ} (v : option υ) (h : p ∉ (η.rcn i).dᵢ) :
(η.update_port role.input ⟨i.rtr, p⟩ v).rtr i.rtr =i.rcn= η.rtr i.rtr :=
begin
simp only [update_port, update_reactor, rtr, lgraph.update_data_same, rcn] at h ⊢,
exact reactor.eq_rel_to_symm (reactor.eq_rel_to.single (refl _) h)
end
-- Runs a given reaction within the context of its reactor.
-- That is, without propagating any outputs.
noncomputable def run_local (η : graph υ) (i : reaction.id) : graph υ :=
η.update_reactor i.rtr ((η.rtr i.rtr).run i.rcn)
-- Running a reaction locally does not change any other reactors in the network graph.
@[simp]
lemma run_local_is_local (η : graph υ) {iₙ : reaction.id} {iᵣ : reactor.id} (h : iₙ.rtr ≠ iᵣ) :
(η.run_local iₙ).rtr iᵣ = η.rtr iᵣ :=
by simp [run_local, update_reactor_ne_rtr, h]
-- Running a reaction locally produces an equivalent network graph.
lemma run_local_equiv (η : graph υ) (i : reaction.id) : η.run_local i ≈ η :=
begin
unfold run_local,
have h, from reactor.run_equiv (η.rtr i.rtr) i.rcn,
exact update_reactor_equiv (reactor.equiv_symm h)
end
-- Running a reaction locally produces retains relative equality for all other reactors.
lemma run_local_eq_rel_to (η : graph υ) {i i' : reaction.id} (h : i.rtr ≠ i'.rtr) :
(η.run_local i).rtr i'.rtr =i'.rcn= η.rtr i'.rtr :=
by { rw run_local_is_local η h, apply eq_rel_to_refl }
-- Running two reactions that live in different reactors locally is commutative.
lemma run_local_comm (η : graph υ) {i i' : reaction.id} (h : i.rtr ≠ i'.rtr) :
(η.run_local i).run_local i' = (η.run_local i').run_local i :=
by simp [run_local, update_reactor_ne_rtr _ _ h, update_reactor_ne_rtr _ _ (ne.symm h), update_reactor_comm _ _ _ h]
-- Output ports which are not in the output dependencies of a reaction, are not affected by running that reaction.
@[simp]
lemma run_local_output_eq {η : graph υ} {i : reaction.id} {p : port.id} (h : p ∉ η.deps i role.output) :
(η.run_local i).port role.output p = η.port role.output p :=
begin
unfold run_local,
unfold port,
unfold update_reactor rtr,
by_cases hc : i.rtr = p.rtr,
{
rw [hc, lgraph.update_data_same],
rw [mem_deps_iff_mem_rcn_deps, not_and, rcn, rtr, hc] at h,
exact reactor.run_out_not_dₒ_eq (h (refl _))
},
rw lgraph.update_data_ne _ _ (ne.symm hc)
end
-- Running a reaction locally, and updating an input port is commutative, if the input port is not part of the
-- reaction's input dependencies.
lemma run_local_update_input_comm {η : graph υ} {i : reaction.id} {p : port.id} (v : option υ) (h : p ∉ η.deps i role.input) :
(η.run_local i).update_port role.input p v = (η.update_port role.input p v).run_local i :=
begin
by_cases hc : p.rtr = i.rtr,
{
unfold update_port run_local,
rw hc,
repeat { rw [update_reactor_same, update_reactor_eq_rtr] },
rw run_update_input_comm,
rw [mem_deps_iff_mem_rcn_deps, not_and] at h,
exact h (eq.symm hc)
},
{
unfold update_port run_local,
rw update_reactor_ne_rtr _ _ hc,
rw update_reactor_ne_rtr _ _ (ne.symm hc),
exact update_reactor_comm _ _ _ (ne.symm hc)
}
end
-- Returns the index-diff of the ports (of a given role) of the same reactor in two different network graphs.
noncomputable def index_diff (η η' : graph υ) (i : reactor.id) (r : ports.role) : finset port.id :=
(((η.rtr i).prts r).index_diff ((η'.rtr i).prts r)).image (port.id.mk i)
-- The diff of running a reaction can only contain ports which are in the output dependencies of the reaction.
lemma index_diff_sub_dₒ (η : graph υ) (i : reaction.id) :
(η.run_local i).index_diff η i.rtr role.output ⊆ η.deps i role.output :=
begin
simp only [index_diff, deps, (⊆)],
intro x,
repeat { rw finset.mem_image },
intro h,
obtain ⟨a, hₐ, hₓ⟩ := h,
by_cases hc : i.rtr = x.rtr,
{
rw [run_local, hc, update_reactor_eq_rtr, prts, prts] at hₐ,
have hd, from run_out_diff_sub_dₒ (η.rtr x.rtr) i.rcn,
rw index_diff_comm at hd,
simp only [(⊆)] at hd,
replace hd := hd hₐ,
existsi a,
rw [rcn, hc],
existsi hd,
rw ←hc,
exact hₓ
},
{
exfalso,
simp only [←hₓ] at hc,
exact false_of_ne hc
}
end
-- Running a reaction in two network graphs that have relatively equal reactors for that reaction, produces the
-- same output index-diff for those reactors.
lemma run_local_index_diff_eq_rel_to {η η' : graph υ} {i : reaction.id} (h : η.rtr i.rtr =i.rcn= η'.rtr i.rtr) :
(η.run_local i).index_diff η i.rtr role.output = (η'.run_local i).index_diff η' i.rtr role.output :=
by simp [index_diff, prts, run_local, eq_rel_to_eq_output h, eq_rel_to_eq_output (run_eq_rel_to h)]
-- Returns a network graph, where the given reactor's ports are all set to `none`.
noncomputable def clear_reactor (η : graph υ) (i : reactor.id) : graph υ :=
η.update_reactor i {reactor .
input := (ports.empty υ (η.rtr i).input.length),
output := (ports.empty υ (η.rtr i).output.length),
.. (η.rtr i)
}
-- Clearing a reactor's ports, produces an equivalent reactor.
lemma clear_reactor_equiv (η : graph υ) (i : reactor.id) : η.clear_reactor i ≈ η :=
begin
simp only [clear_reactor, (≈)],
repeat { split },
intro x,
by_cases hc : i = x,
simp [hc],
simp [update_reactor_ne_rtr _ _ hc],
end
-- Returns a network graph, where all ports are set to `none`.
noncomputable def clear_all_ports (η : graph υ) : graph υ :=
η.ids.val.to_list.foldl clear_reactor η
-- Clearing all of a network graph's ports, produces an equivalent reactor.
lemma clear_all_ports_equiv (η : graph υ) : η.clear_all_ports ≈ η :=
begin
unfold clear_all_ports,
induction η.ids.val.to_list generalizing η,
case list.nil { simp },
case list.cons {
simp only [list.foldl_cons],
transitivity,
exact ih _,
exact clear_reactor_equiv _ _
}
end
-- Returns the network graph that you get by copying the ports of a given network graph (and role)
-- to a target graph. The first graph is the target.
noncomputable def copy_ports (η η' : graph υ) (ps : finset port.id) (r : ports.role) : graph υ :=
ps.val.to_list.foldl (λ ηₗ p, ηₗ.update_port r p (η'.port r p)) η
-- Copying ports, produces an equivalent reactor.
lemma copy_ports_equiv (η η' : graph υ) (ps : finset port.id) (r : ports.role) : η.copy_ports η' ps r ≈ η :=
begin
unfold copy_ports,
induction ps.val.to_list generalizing η,
case list.nil { simp },
case list.cons {
simp only [list.foldl_cons],
transitivity,
exact ih _,
exact equiv_symm (update_port_equiv _ _ _ _)
}
end
end graph
end network
end inst |
11f6a0e2691340847500229cd6d6b95a70c3ca20 | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/order/ring/with_top.lean | dc036fafb540ef24fa05f4855744cdcfd41b79be | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 10,400 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro
-/
import algebra.hom.ring
import algebra.order.monoid.with_top
import algebra.order.ring.canonical
/-! # Structures involving `*` and `0` on `with_top` and `with_bot`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The main results of this section are `with_top.canonically_ordered_comm_semiring` and
`with_bot.comm_monoid_with_zero`.
-/
variables {α : Type*}
namespace with_top
instance [nonempty α] : nontrivial (with_top α) := option.nontrivial
variable [decidable_eq α]
section has_mul
variables [has_zero α] [has_mul α]
instance : mul_zero_class (with_top α) :=
{ zero := 0,
mul := λ m n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)),
zero_mul := assume a, if_pos $ or.inl rfl,
mul_zero := assume a, if_pos $ or.inr rfl }
lemma mul_def {a b : with_top α} :
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
@[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ :=
by cases a; simp [mul_def, h]; refl
@[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ :=
by cases a; simp [mul_def, h]; refl
@[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ :=
top_mul top_ne_zero
end has_mul
section mul_zero_class
variables [mul_zero_class α]
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b :=
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
by { simp [*, mul_def], refl }
lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b))
| none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤,
by simp [hb]
| (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm
@[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) :=
begin
cases a; cases b; simp only [none_eq_top, some_eq_coe],
{ simp [← coe_mul] },
{ by_cases hb : b = 0; simp [hb] },
{ by_cases ha : a = 0; simp [ha] },
{ simp [← coe_mul] }
end
lemma mul_lt_top [preorder α] {a b : with_top α} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a * b < ⊤ :=
begin
lift a to α using ha,
lift b to α using hb,
simp only [← coe_mul, coe_lt_top]
end
@[simp] lemma untop'_zero_mul (a b : with_top α) : (a * b).untop' 0 = a.untop' 0 * b.untop' 0 :=
begin
by_cases ha : a = 0, { rw [ha, zero_mul, ← coe_zero, untop'_coe, zero_mul] },
by_cases hb : b = 0, { rw [hb, mul_zero, ← coe_zero, untop'_coe, mul_zero] },
induction a using with_top.rec_top_coe, { rw [top_mul hb, untop'_top, zero_mul] },
induction b using with_top.rec_top_coe, { rw [mul_top ha, untop'_top, mul_zero] },
rw [← coe_mul, untop'_coe, untop'_coe, untop'_coe]
end
end mul_zero_class
/-- `nontrivial α` is needed here as otherwise we have `1 * ⊤ = ⊤` but also `0 * ⊤ = 0`. -/
instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_top α) :=
{ mul := (*),
one := 1,
zero := 0,
one_mul := λ a, match a with
| ⊤ := mul_top (mt coe_eq_coe.1 one_ne_zero)
| (a : α) := by rw [← coe_one, ← coe_mul, one_mul]
end,
mul_one := λ a, match a with
| ⊤ := top_mul (mt coe_eq_coe.1 one_ne_zero)
| (a : α) := by rw [← coe_one, ← coe_mul, mul_one]
end,
.. with_top.mul_zero_class }
/-- A version of `with_top.map` for `monoid_with_zero_hom`s. -/
@[simps { fully_applied := ff }] protected def _root_.monoid_with_zero_hom.with_top_map
{R S : Type*} [mul_zero_one_class R] [decidable_eq R] [nontrivial R]
[mul_zero_one_class S] [decidable_eq S] [nontrivial S] (f : R →*₀ S) (hf : function.injective f) :
with_top R →*₀ with_top S :=
{ to_fun := with_top.map f,
map_mul' := λ x y,
begin
have : ∀ z, map f z = 0 ↔ z = 0,
from λ z, (option.map_injective hf).eq_iff' f.to_zero_hom.with_top_map.map_zero,
rcases eq_or_ne x 0 with rfl|hx, { simp },
rcases eq_or_ne y 0 with rfl|hy, { simp },
induction x using with_top.rec_top_coe, { simp [hy, this] },
induction y using with_top.rec_top_coe,
{ have : (f x : with_top S) ≠ 0, by simpa [hf.eq_iff' (map_zero f)] using hx,
simp [hx, this] },
simp [← coe_mul]
end,
.. f.to_zero_hom.with_top_map, .. f.to_monoid_hom.to_one_hom.with_top_map }
instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_top α) :=
⟨λ a b, by cases a; cases b; dsimp [mul_def]; split_ifs;
simp [*, none_eq_top, some_eq_coe, mul_eq_zero] at *⟩
instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_top α) :=
{ mul := (*),
zero := 0,
mul_assoc := λ a b c, begin
rcases eq_or_ne a 0 with rfl|ha, { simp only [zero_mul] },
rcases eq_or_ne b 0 with rfl|hb, { simp only [zero_mul, mul_zero] },
rcases eq_or_ne c 0 with rfl|hc, { simp only [mul_zero] },
induction a using with_top.rec_top_coe, { simp [hb, hc] },
induction b using with_top.rec_top_coe, { simp [ha, hc] },
induction c using with_top.rec_top_coe, { simp [ha, hb] },
simp only [← coe_mul, mul_assoc]
end,
.. with_top.mul_zero_class }
instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_top α) :=
{ .. with_top.mul_zero_one_class, .. with_top.semigroup_with_zero }
instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] :
comm_monoid_with_zero (with_top α) :=
{ mul := (*),
zero := 0,
mul_comm := λ a b,
by simp only [or_comm, mul_def, option.bind_comm a b, mul_comm],
.. with_top.monoid_with_zero }
variables [canonically_ordered_comm_semiring α]
private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c :=
begin
induction c using with_top.rec_top_coe,
{ by_cases ha : a = 0; simp [ha] },
{ by_cases hc : c = 0, { simp [hc] },
simp [mul_coe hc], cases a; cases b,
repeat { refl <|> exact congr_arg some (add_mul _ _ _) } }
end
/-- This instance requires `canonically_ordered_comm_semiring` as it is the smallest class
that derives from both `non_assoc_non_unital_semiring` and `canonically_ordered_add_monoid`, both
of which are required for distributivity. -/
instance [nontrivial α] : comm_semiring (with_top α) :=
{ right_distrib := distrib',
left_distrib := λ a b c, by { rw [mul_comm, distrib', mul_comm b, mul_comm c], refl },
.. with_top.add_comm_monoid_with_one, .. with_top.comm_monoid_with_zero }
instance [nontrivial α] : canonically_ordered_comm_semiring (with_top α) :=
{ .. with_top.comm_semiring,
.. with_top.canonically_ordered_add_monoid,
.. with_top.no_zero_divisors, }
/-- A version of `with_top.map` for `ring_hom`s. -/
@[simps { fully_applied := ff }] protected def _root_.ring_hom.with_top_map
{R S : Type*} [canonically_ordered_comm_semiring R] [decidable_eq R] [nontrivial R]
[canonically_ordered_comm_semiring S] [decidable_eq S] [nontrivial S]
(f : R →+* S) (hf : function.injective f) :
with_top R →+* with_top S :=
{ to_fun := with_top.map f,
.. f.to_monoid_with_zero_hom.with_top_map hf, .. f.to_add_monoid_hom.with_top_map }
end with_top
namespace with_bot
instance [nonempty α] : nontrivial (with_bot α) := option.nontrivial
variable [decidable_eq α]
section has_mul
variables [has_zero α] [has_mul α]
instance : mul_zero_class (with_bot α) :=
with_top.mul_zero_class
lemma mul_def {a b : with_bot α} :
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
@[simp] lemma mul_bot {a : with_bot α} (h : a ≠ 0) : a * ⊥ = ⊥ :=
with_top.mul_top h
@[simp] lemma bot_mul {a : with_bot α} (h : a ≠ 0) : ⊥ * a = ⊥ :=
with_top.top_mul h
@[simp] lemma bot_mul_bot : (⊥ * ⊥ : with_bot α) = ⊥ :=
with_top.top_mul_top
end has_mul
section mul_zero_class
variables [mul_zero_class α]
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_bot α) = a * b :=
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
by { simp [*, mul_def], refl }
lemma mul_coe {b : α} (hb : b ≠ 0) {a : with_bot α} : a * b = a.bind (λa:α, ↑(a * b)) :=
with_top.mul_coe hb
@[simp] lemma mul_eq_bot_iff {a b : with_bot α} : a * b = ⊥ ↔ (a ≠ 0 ∧ b = ⊥) ∨ (a = ⊥ ∧ b ≠ 0) :=
with_top.mul_eq_top_iff
lemma bot_lt_mul [preorder α] {a b : with_bot α} (ha : ⊥ < a) (hb : ⊥ < b) : ⊥ < a * b :=
begin
lift a to α using ne_bot_of_gt ha,
lift b to α using ne_bot_of_gt hb,
simp only [← coe_mul, bot_lt_coe],
end
end mul_zero_class
/-- `nontrivial α` is needed here as otherwise we have `1 * ⊥ = ⊥` but also `= 0 * ⊥ = 0`. -/
instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_bot α) :=
with_top.mul_zero_one_class
instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_bot α) :=
with_top.no_zero_divisors
instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_bot α) :=
with_top.semigroup_with_zero
instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_bot α) :=
with_top.monoid_with_zero
instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] :
comm_monoid_with_zero (with_bot α) :=
with_top.comm_monoid_with_zero
instance [canonically_ordered_comm_semiring α] [nontrivial α] : comm_semiring (with_bot α) :=
with_top.comm_semiring
instance [canonically_ordered_comm_semiring α] [nontrivial α] : pos_mul_mono (with_bot α) :=
pos_mul_mono_iff_covariant_pos.2 ⟨begin
rintros ⟨x, x0⟩ a b h, simp only [subtype.coe_mk],
lift x to α using x0.ne_bot,
induction a using with_bot.rec_bot_coe, { simp_rw [mul_bot x0.ne.symm, bot_le] },
induction b using with_bot.rec_bot_coe, { exact absurd h (bot_lt_coe a).not_le },
simp only [← coe_mul, coe_le_coe] at *,
exact mul_le_mul_left' h x,
end ⟩
instance [canonically_ordered_comm_semiring α] [nontrivial α] : mul_pos_mono (with_bot α) :=
pos_mul_mono_iff_mul_pos_mono.mp infer_instance
end with_bot
|
4060342cf349207252baabcb02fc2d0fc7d90861 | f4bff2062c030df03d65e8b69c88f79b63a359d8 | /src/game/order/level08.lean | 77fa1e9ac9bf55b073ab83a1a7f9eae42188eeae | [
"Apache-2.0"
] | permissive | adastra7470/real-number-game | 776606961f52db0eb824555ed2f8e16f92216ea3 | f9dcb7d9255a79b57e62038228a23346c2dc301b | refs/heads/master | 1,669,221,575,893 | 1,594,669,800,000 | 1,594,669,800,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 878 | lean | import game.order.level07
import data.real.irrational
open real
namespace xena -- hide
/-
# Chapter 2 : Order
## Level 8
Prove by example that there exist pairs of real numbers
$a$ and $b$ such that $a \in \mathbb{R} \setminus \mathbb{Q}$,
$b \in \mathbb{R} \setminus \mathbb{Q}$,
but their product $a \cdot b$ is a rational number, $(a \cdot b) \in \mathbb{Q}$.
You may use this result in the Lean mathlib library:
`irrational_sqrt_two : irrational (sqrt 2)
-/
/- Lemma
Not true that for any $a$, $b$, irrational numbers, the product is
also an irrational number.
-/
theorem not_prod_irrational :
¬ ( ∀ (a b : ℝ), irrational a → irrational b → irrational (a*b) ) :=
begin
intro H,
have H2 := H (sqrt 2) (sqrt 2),
have H3 := H2 irrational_sqrt_two irrational_sqrt_two,
apply H3,
existsi (2 : ℚ),
simp, norm_num, done
end
end xena -- hide
|
9f58bd05d3f7ed06c7fa972fd4880d7788e77825 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/pp_all.lean | d4ed191e2ccc06b76c0f1fb1371e43cac02babf4 | [
"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 | 138 | lean | --
open nat
variables {a : nat}
definition b : nat := 2
#check (λ x, x) a + b = 10
set_option pp.all true
#check (λ x, x) a + b = 10
|
3e12d280a7a639b6c0ce16b3755faed08ea53f72 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/number_theory/quadratic_reciprocity.lean | b3debc4dec4282d6c5d9c3e3089e1e04943ae0d1 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,984 | 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 field_theory.finite.basic
import data.zmod.basic
import data.nat.parity
/-!
# Quadratic reciprocity.
This file contains results about quadratic residues modulo a prime number.
The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the
interpretations in terms of existence of square roots depending on the congruence mod 4,
`exists_sq_eq_prime_iff_of_mod_four_eq_one`, and
`exists_sq_eq_prime_iff_of_mod_four_eq_three`.
Also proven are conditions for `-1` and `2` to be a square modulo a prime,
`exists_sq_eq_neg_one_iff_mod_four_ne_three` and
`exists_sq_eq_two_iff`
## Implementation notes
The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma
-/
open function finset nat finite_field zmod
open_locale big_operators nat
namespace zmod
variables (p q : ℕ) [fact p.prime] [fact q.prime]
/-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/
lemma euler_criterion_units (x : units (zmod p)) :
(∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 :=
begin
cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd,
{ substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim },
obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)),
obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_gpowers, apply hg },
split,
{ rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], },
{ subst x, assume h,
have key : 2 * (p / 2) ∣ n * (p / 2),
{ rw [← pow_mul] at h,
rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_gpowers hg],
apply order_of_dvd_of_pow_eq_one h },
have : 0 < p / 2 := nat.div_pos (fact.out (1 < p)) dec_trivial,
obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key,
refine ⟨g ^ m, _⟩,
rw [mul_comm, pow_mul], },
end
/-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/
lemma euler_criterion {a : zmod p} (ha : a ≠ 0) :
(∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 :=
begin
apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)),
simp only [units.ext_iff, sq, units.coe_mk0, units.coe_mul],
split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ },
{ rintro ⟨y, rfl⟩,
have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, },
refine ⟨units.mk0 y hy, _⟩, simp, }
end
lemma exists_sq_eq_neg_one_iff_mod_four_ne_three :
(∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 :=
begin
cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd,
{ substI p, exact dec_trivial },
haveI := fact.mk hp_odd,
have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero,
rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two],
cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd,
{ rw [p_half_even, pow_zero, eq_self_iff_true, true_iff],
contrapose! p_half_even with hp,
rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp],
exact dec_trivial },
{ rw [p_half_odd, pow_one,
iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not],
rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd,
rw [← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd,
have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial,
revert hp hp_odd p_half_odd,
generalize : p % 4 = k, dec_trivial! }
end
lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) :
a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 :=
begin
cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd,
{ substI p, revert a ha, exact dec_trivial },
rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd],
exact pow_card_sub_one_eq_one ha
end
/-- **Wilson's Lemma**: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/
@[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 :=
begin
refine
calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) :
by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast]
... = (∏ x : units (zmod p), x) : _
... = -1 : by simp_rw [← units.coe_hom_apply,
← (units.coe_hom (zmod p)).map_prod, prod_univ_units_id_eq_neg_one, units.coe_hom_apply,
units.coe_neg, units.coe_one],
have hp : 0 < p := (fact.out p.prime).pos,
symmetry,
refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _,
{ intros a ha,
rw [mem_Ico, ← nat.succ_sub hp, nat.succ_sub_one],
split,
{ apply nat.pos_of_ne_zero, rw ← @val_zero p,
assume h, apply units.ne_zero a (val_injective p h) },
{ exact val_lt _ } },
{ intros a ha, simp only [cast_id, nat_cast_val], },
{ intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h },
{ intros b hb,
rw [mem_Ico, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb,
refine ⟨units.mk0 b _, finset.mem_univ _, _⟩,
{ assume h, apply hb.1, apply_fun val at h,
simpa only [val_cast_of_lt hb.right, val_zero] using h },
{ simp only [val_cast_of_lt hb.right, units.coe_mk0], } }
end
@[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 :=
begin
conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (fact.out p.prime).pos] },
rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma]
end
end zmod
/-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value
of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set
of non zero natural numbers `x` such that `x ≤ p / 2` -/
lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id
(p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) :
(Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) =
(Ico 1 (p / 2).succ).1.map (λ a, a) :=
begin
have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2,
by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt},
have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p,
from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.1.pos dec_trivial),
have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x,
from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx),
have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ),
(a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ,
{ assume x hx,
simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff,
pos_iff_ne_zero, nat_abs_val_min_abs_le _], },
have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ),
∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs,
{ assume b hb,
refine ⟨(b / a : zmod p).val_min_abs.nat_abs, mem_Ico.mpr ⟨_, _⟩, _⟩,
{ apply nat.pos_of_ne_zero,
simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff,
val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] },
{ apply lt_succ_of_le, apply nat_abs_val_min_abs_le },
{ rw nat_cast_nat_abs_val_min_abs,
split_ifs,
{ erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb),
if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat], },
{ erw [mul_neg_eq_neg_mul_symm, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg,
val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2),
int.nat_abs_of_nat] } } },
exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _)
(λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl)
(inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj
end
private lemma gauss_lemma_aux₁ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)]
{a : ℕ} (hap : (a : zmod p) ≠ 0) :
(a^(p / 2) * (p / 2)! : zmod p) =
(-1)^((Ico 1 (p / 2).succ).filter
(λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! :=
calc (a ^ (p / 2) * (p / 2)! : zmod p) =
(∏ x in Ico 1 (p / 2).succ, a * x) :
by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_factorial,
prod_const, card_Ico, succ_sub_one]; simp
... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp
... = (∏ x in Ico 1 (p / 2).succ,
(if (a * x : zmod p).val ≤ p / 2 then 1 else -1) *
(a * x : zmod p).val_min_abs.nat_abs) :
prod_congr rfl $ λ _ _, begin
simp only [nat_cast_nat_abs_val_min_abs],
split_ifs; simp
end
... = (-1)^((Ico 1 (p / 2).succ).filter
(λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card *
(∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) :
have (∏ x in Ico 1 (p / 2).succ,
if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) =
(∏ x in (Ico 1 (p / 2).succ).filter
(λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1),
from prod_bij_ne_one (λ x _ _, x)
(λ x, by split_ifs; simp * at * {contextual := tt})
(λ _ _ _ _ _ _, id)
(λ b h _, ⟨b, by simp [-not_le, *] at *⟩)
(by intros; split_ifs at *; simp * at *),
by rw [prod_mul_distrib, this]; simp
... = (-1)^((Ico 1 (p / 2).succ).filter
(λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! :
by rw [← prod_nat_cast, finset.prod_eq_multiset_prod,
Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap,
← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial]
private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [fact (p % 2 = 1)]
{a : ℕ} (hap : (a : zmod p) ≠ 0) :
(a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter
(λ x : ℕ, p / 2 < (a * x : zmod p).val)).card :=
(mul_left_inj'
(show ((p / 2)! : zmod p) ≠ 0,
by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.1.dvd_factorial, not_le];
exact nat.div_lt_self hp.1.pos dec_trivial)).1 $
by simpa using gauss_lemma_aux₁ p hap
private lemma eisenstein_lemma_aux₁ (p : ℕ) [fact p.prime] [hp2 : fact (p % 2 = 1)]
{a : ℕ} (hap : (a : zmod p) ≠ 0) :
((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) =
((Ico 1 (p / 2).succ).filter
((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card +
∑ x in Ico 1 (p / 2).succ, x
+ (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) :=
have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2.1,
calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2)
= ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) :
by simp only [mod_add_div]
... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) +
(∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) :
by simp only [val_nat_cast];
simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, nat.cast_sum, hp2]
... = _ : congr_arg2 (+)
(calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2)
= ∑ x in Ico 1 (p / 2).succ,
((((a * x : zmod p).val_min_abs +
(if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) :
by simp only [(val_eq_ite_val_min_abs _).symm]; simp [nat.cast_sum]
... = ((Ico 1 (p / 2).succ).filter
(λ x : ℕ, p / 2 < (a * x : zmod p).val)).card +
((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) :
by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, nat.cast_sum], }
... = _ : by rw [finset.sum_eq_multiset_sum,
Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap,
← finset.sum_eq_multiset_sum];
simp [nat.cast_sum]) rfl
private lemma eisenstein_lemma_aux₂ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)]
{a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) :
((Ico 1 (p / 2).succ).filter
((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card
≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] :=
have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2,
(eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $
by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, nat.cast_sum,
add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc]
using eq.symm (eisenstein_lemma_aux₁ p hap)
lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b =
((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card :=
calc a / b = (Ico 1 (a / b).succ).card : by simp
... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card :
congr_arg _ $ finset.ext $ λ x,
have x * b ≤ a → x ≤ c,
from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc,
by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto
/-- The given sum is the number of integer points in the triangle formed by the diagonal of the
rectangle `(0, p/2) × (0, q/2)` -/
private lemma sum_Ico_eq_card_lt {p q : ℕ} :
∑ a in Ico 1 (p / 2).succ, (a * q) / p =
(((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card :=
if hp0 : p = 0 then by simp [hp0, finset.ext_iff]
else
calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p =
∑ a in Ico 1 (p / 2).succ,
((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card :
finset.sum_congr rfl $ λ x hx,
div_eq_filter_card (nat.pos_of_ne_zero hp0)
(calc x * q / p ≤ (p / 2) * q / p :
nat.div_le_div_right (mul_le_mul_of_nonneg_right
(le_of_lt_succ $ by finish)
(nat.zero_le _))
... ≤ _ : nat.div_mul_div_le_div _ _ _)
... = _ : by rw [← card_sigma];
exact card_congr (λ a _, ⟨a.1, a.2⟩)
(by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product]
{contextual := tt})
(λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq,
forall_true_iff] {contextual := tt})
(λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩,
by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma,
and_self, forall_true_iff, mem_product] {contextual := tt}⟩)
/-- Each of the sums in this lemma is the cardinality of the set integer points in each of the
two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them
gives the number of points in the rectangle. -/
private lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime]
(hq0 : (q : zmod p) ≠ 0) :
∑ a in Ico 1 (p / 2).succ, (a * q) / p +
∑ a in Ico 1 (q / 2).succ, (a * p) / q =
(p / 2) * (q / 2) :=
begin
have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter
(λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card =
(((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card :=
card_congr (λ x _, prod.swap x)
(λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product]
{contextual := tt})
(λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk,
forall_true_iff] {contextual := tt})
(λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self,
exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩),
have hdisj : disjoint
(((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q))
(((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)),
{ apply disjoint_filter.2 (λ x hx hpq hqp, _),
have hxp : x.1 < p, from lt_of_le_of_lt
(show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, mem_Ico, mem_product] at *; tauto)
(nat.div_lt_self hp.1.pos dec_trivial),
have : (x.1 : zmod p) = 0,
{ simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) },
apply_fun zmod.val at this,
rw [val_cast_of_lt hxp, val_zero] at this,
simpa only [this, nonpos_iff_eq_zero, mem_Ico, one_ne_zero, false_and, mem_product] using hx },
have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪
((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter
(λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) =
((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)),
from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q);
simp only [mem_union, mem_filter, mem_Ico, mem_product]; tauto),
rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion,
card_product],
simp only [card_Ico, tsub_zero, succ_sub_succ_eq_sub]
end
variables (p q : ℕ) [fact p.prime] [fact q.prime]
namespace zmod
/-- The Legendre symbol of `a` and `p` is an integer defined as
* `0` if `a` is `0` modulo `p`;
* `1` if `a ^ (p / 2)` is `1` modulo `p`
(by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”);
* `-1` otherwise.
-/
def legendre_sym (a p : ℕ) : ℤ :=
if (a : zmod p) = 0 then 0
else if (a : zmod p) ^ (p / 2) = 1 then 1
else -1
lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] :
(legendre_sym a p : zmod p) = (a ^ (p / 2)) :=
begin
rw legendre_sym,
by_cases ha : (a : zmod p) = 0,
{ simp only [if_pos, ha, zero_pow (nat.div_pos (hp.1.two_le) (succ_pos 1)), int.cast_zero] },
cases hp.1.eq_two_or_odd with hp2 hp_odd,
{ substI p,
generalize : (a : (zmod 2)) = b, revert b, dec_trivial, },
{ haveI := fact.mk hp_odd,
rw if_neg ha,
have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm,
cases pow_div_two_eq_neg_one_or_one p ha with h h,
{ rw [if_pos h, h, int.cast_one], },
{ rw [h, if_neg this, int.cast_neg, int.cast_one], } }
end
lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) :
legendre_sym a p = -1 ∨ legendre_sym a p = 1 :=
by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at *
lemma legendre_sym_eq_zero_iff (a p : ℕ) :
legendre_sym a p = 0 ↔ (a : zmod p) = 0 :=
begin
split,
{ classical, contrapose,
assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h,
all_goals { rw h, norm_num } },
{ assume ha, rw [legendre_sym, if_pos ha] }
end
/-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less
than `p/2` such that `(a * x) % p > p / 2` -/
lemma gauss_lemma {a : ℕ} [fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) :
legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter
(λ x : ℕ, p / 2 < (a * x : zmod p).val)).card :=
have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter
(λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p),
by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp,
begin
cases legendre_sym_eq_one_or_neg_one a p ha0;
cases neg_one_pow_eq_or ℤ ((Ico 1 (p / 2).succ).filter
(λ x : ℕ, p / 2 < (a * x : zmod p).val)).card;
simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at *
end
lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) :
legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) :=
begin
rw [euler_criterion p ha0, legendre_sym, if_neg ha0],
split_ifs,
{ simp only [h, eq_self_iff_true] },
finish -- this is quite slow. I'm actually surprised that it can close the goal at all!
end
lemma eisenstein_lemma [fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) :
legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p :=
by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two,
show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0]
/-- **Quadratic reciprocity theorem** -/
theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) :
legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) :=
have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm,
have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq,
by rw [eisenstein_lemma q hp1.1 hpq0, eisenstein_lemma p hq1.1 hqp0,
← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm]
-- move this
local attribute [instance]
lemma fact_prime_two : fact (nat.prime 2) := ⟨nat.prime_two⟩
lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) :=
have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1.1),
have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial)
(nat.div_le_self (p / 2) 2),
have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp,
have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x,
from λ x hx, have h2xp : 2 * x < p,
from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left
(le_of_lt_succ $ by finish) dec_trivial
... < _ : by conv_rhs {rw [← div_add_mod p 2, hp1.1]}; exact lt_succ_self _,
by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp],
have hdisj : disjoint
((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val))
((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)),
from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]),
have hunion :
((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪
((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) =
Ico 1 (p / 2).succ,
begin
rw [filter_union_right],
conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]},
exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm])
end,
begin
rw [gauss_lemma p (prime_ne_zero p 2 hp2),
neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)],
refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _),
rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add,
← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two,
← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard]
end
lemma exists_sq_eq_two_iff [hp1 : fact (p % 2 = 1)] :
(∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 :=
have hp2 : ((2 : ℕ) : zmod p) ≠ 0,
from prime_ne_zero p 2 (λ h, by simpa [h] using hp1.1),
have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm,
have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm,
begin
rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2,
legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial),
even_add, even_div, even_div],
have := nat.mod_lt p (show 0 < 8, from dec_trivial),
resetI, rw fact_iff at hp1,
revert this hp1,
erw [hpm4, hpm2],
generalize hm : p % 8 = m, unfreezingI {clear_dependent p},
dec_trivial!,
end
lemma exists_sq_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] :
(∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p :=
if hpq : p = q then by substI hpq else
have h1 : ((p / 2) * (q / 2)) % 2 = 0,
from (dvd_iff_mod_eq_zero _ _).1
(dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $
by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _),
begin
haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_one hp1⟩,
have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq),
have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq,
have := quadratic_reciprocity p q hpq,
rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym,
if_neg hqp0, if_neg hpq0] at this,
rw [euler_criterion q hpq0, euler_criterion p hqp0],
split_ifs at this; simp *; contradiction,
end
lemma exists_sq_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3)
(hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p :=
have h1 : ((p / 2) * (q / 2)) % 2 = 1,
from nat.odd_mul_odd
(by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl)
(by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl),
begin
haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_three hp3⟩,
haveI hq_odd : fact (q % 2 = 1) := ⟨odd_of_mod_four_eq_three hq3⟩,
have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq),
have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq,
have := quadratic_reciprocity p q hpq,
rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym,
if_neg hpq0, if_neg hqp0] at this,
rw [euler_criterion q hpq0, euler_criterion p hqp0],
split_ifs at this; simp *; contradiction
end
end zmod
|
b3b36af7f4e53947ec0fe631e3f5de12b4cdb5d3 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /order/galois_connection.lean | e405fffb4b9653a0c8e17f0e304fc5c83bfe6b72 | [
"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 | 10,481 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
Galois connections - order theoretic adjoints.
-/
import order.bounds
open function set lattice
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a a₁ a₂ : α} {b b₁ b₂ : β}
/-- A Galois connection is a pair of functions `l` and `u` satisfying
`l a ≤ b ↔ a ≤ u b`. They are closely connected to adjoint functors
in category theory. -/
def galois_connection [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀a b, l a ≤ b ↔ a ≤ u b
namespace galois_connection
section
variables [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u)
lemma monotone_intro (hu : monotone u) (hl : monotone l)
(hul : ∀ a, a ≤ u (l a)) (hlu : ∀ a, l (u a) ≤ a) : galois_connection l u :=
assume a b, ⟨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)⟩
include gc
lemma l_le {a : α} {b : β} : a ≤ u b → l a ≤ b :=
(gc _ _).mpr
lemma le_u {a : α} {b : β} : l a ≤ b → a ≤ u b :=
(gc _ _).mp
lemma le_u_l (a) : a ≤ u (l a) :=
gc.le_u $ le_refl _
lemma l_u_le (a) : l (u a) ≤ a :=
gc.l_le $ le_refl _
lemma monotone_u : monotone u :=
assume a b H, gc.le_u (le_trans (gc.l_u_le a) H)
lemma monotone_l : monotone l :=
assume a b H, gc.l_le (le_trans H (gc.le_u_l b))
lemma upper_bounds_l_image_subset {s : set α} : upper_bounds (l '' s) ⊆ u ⁻¹' upper_bounds s :=
assume b hb c, assume : c ∈ s, gc.le_u (hb _ (mem_image_of_mem _ ‹c ∈ s›))
lemma lower_bounds_u_image_subset {s : set β} : lower_bounds (u '' s) ⊆ l ⁻¹' lower_bounds s :=
assume a ha c, assume : c ∈ s, gc.l_le (ha _ (mem_image_of_mem _ ‹c ∈ s›))
lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) :=
⟨mem_upper_bounds_image gc.monotone_l $ and.elim_left ‹is_lub s a›,
assume b hb, gc.l_le $ and.elim_right ‹is_lub s a› _ $ gc.upper_bounds_l_image_subset hb⟩
lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) :=
⟨mem_lower_bounds_image gc.monotone_u $ and.elim_left ‹is_glb s b›,
assume a ha, gc.le_u $ and.elim_right ‹is_glb s b› _ $ gc.lower_bounds_u_image_subset ha⟩
lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) :=
⟨assume b, gc.l_le, assume b h, h _ $ gc.le_u_l _⟩
lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) :=
⟨assume b, gc.le_u, assume b h, h _ $ gc.l_u_le _⟩
end
section partial_order
variables [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_l_u_eq_u : u ∘ l ∘ u = u :=
funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _))
lemma l_u_l_eq_l : l ∘ u ∘ l = l :=
funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _)))
end partial_order
section order_top
variables [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_top : u ⊤ = ⊤ :=
eq_of_is_glb_of_is_glb (gc.is_glb_u_image is_glb_empty) $ by simp [is_glb_empty, image_empty]
end order_top
section order_bot
variables [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_bot : l ⊥ = ⊥ :=
eq_of_is_lub_of_is_lub (gc.is_lub_l_image is_lub_empty) $ by simp [is_lub_empty, image_empty]
end order_bot
section semilattice_sup
variables [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_sup : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ :=
have {l a₂, l a₁} = l '' {a₂, a₁}, by simp [image_insert_eq, image_singleton],
eq.symm $ is_lub_iff_sup_eq.mp $
by rw [this]; exact gc.is_lub_l_image (is_lub_insert_sup is_lub_singleton)
end semilattice_sup
section semilattice_inf
variables [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma u_inf : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ :=
have {u b₂, u b₁} = u '' {b₂, b₁}, by simp [image_insert_eq, image_singleton],
eq.symm $ is_glb_iff_inf_eq.mp $
by rw [this]; exact gc.is_glb_u_image (is_glb_insert_inf is_glb_singleton)
end semilattice_inf
section complete_lattice
variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_supr {f : ι → α} : l (supr f) = (⨆i, l (f i)) :=
eq.symm $ is_lub_iff_supr_eq.mp $ show is_lub (range (l ∘ f)) (l (supr f)),
by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image is_lub_Sup
lemma u_infi {f : ι → β} : u (infi f) = (⨅i, u (f i)) :=
eq.symm $ is_glb_iff_infi_eq.mp $ show is_glb (range (u ∘ f)) (u (infi f)),
by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image is_glb_Inf
end complete_lattice
section complete_lattice
variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u)
include gc
lemma l_Sup {s : set α} : l (Sup s) = (⨆a∈s, l a) :=
by simp [Sup_eq_supr, gc.l_supr]
lemma u_Inf {s : set β} : u (Inf s) = (⨅a∈s, u a) :=
by simp [Inf_eq_infi, gc.u_infi]
end complete_lattice
/- Constructing Galois connections -/
section constructions
protected lemma id [pα : preorder α] : @galois_connection α α pα pα id id :=
assume a b, iff.intro (λx, x) (λx, x)
protected lemma compose [preorder α] [preorder β] [preorder γ]
(l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β)
(gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) :
galois_connection (l2 ∘ l1) (u1 ∘ u2) :=
by intros a b; rw [gc2, gc1]
protected lemma dual [pα : preorder α] [pβ : preorder β]
(l : α → β) (u : β → α) (gc : galois_connection l u) :
@galois_connection (order_dual β) (order_dual α) _ _ u l :=
assume a b, (gc _ _).symm
protected lemma dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w}
[∀i, preorder (α i)] [∀i, preorder (β i)]
(l : Πi, α i → β i) (u : Πi, β i → α i) (gc : ∀i, galois_connection (l i) (u i)) :
@galois_connection (Π i, α i) (Π i, β i) _ _ (λa i, l i (a i)) (λb i, u i (b i)) :=
assume a b, forall_congr $ assume i, gc i (a i) (b i)
end constructions
end galois_connection
namespace nat
lemma galois_connection_mul_div {k : ℕ} (h : k > 0) : galois_connection (λn, n * k) (λn, n / k) :=
assume x y, (le_div_iff_mul_le x y h).symm
end nat
/-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive
choice function, to give better definitional equalities when lifting order structures. -/
structure galois_insertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) :=
(choice : Πx:α, u (l x) ≤ x → β)
(gc : galois_connection l u)
(le_l_u : ∀x, x ≤ l (u x))
(choice_eq : ∀a h, choice a h = l a)
/-- Lift the bottom along a Galois connection -/
def galois_connection.lift_order_bot {α β : Type*} [order_bot α] [partial_order β]
{l : α → β} {u : β → α} (gc : galois_connection l u) :
order_bot β :=
{ bot := l ⊥,
bot_le := assume b, gc.l_le $ bot_le,
.. ‹partial_order β› }
namespace galois_insertion
open lattice
variables [partial_order β] {l : α → β} {u : β → α}
lemma l_u_eq [preorder α] (gi : galois_insertion l u) (b : β) : l (u b) = b :=
le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _)
/-- Lift the suprema along a Galois insertion -/
def lift_semilattice_sup [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β :=
{ sup := λa b, l (u a ⊔ u b),
le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left,
le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right,
sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. ‹partial_order β› }
/-- Lift the infima along a Galois insertion -/
def lift_semilattice_inf [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β :=
{ inf := λa b, gi.choice (u a ⊓ u b) $
(le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)),
inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left,
inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right,
le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc,
le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. ‹partial_order β› }
/-- Lift the suprema and infima along a Galois insertion -/
def lift_lattice [lattice α] (gi : galois_insertion l u) : lattice β :=
{ .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf }
/-- Lift the top along a Galois insertion -/
def lift_order_top [order_top α] (gi : galois_insertion l u) : order_top β :=
{ top := gi.choice ⊤ $ le_top,
le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top),
.. ‹partial_order β› }
/-- Lift the top, bottom, suprema, and infima along a Galois insertion -/
def lift_bounded_lattice [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β :=
{ .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot }
/-- Lift all suprema and infima along a Galois insertion -/
def lift_complete_lattice [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β :=
{ Sup := λs, l (⨆ b∈s, u b),
Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb,
le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _,
Inf := λs, gi.choice (⨅ b∈s, u b) $ le_infi $ assume b, le_infi $ assume hb,
gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _,
Inf_le := by simp only [gi.choice_eq]; exact
assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _,
le_Inf := by simp only [gi.choice_eq]; exact
assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b,
show u a ≤ ⨅ (H : b ∈ s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb,
.. gi.lift_bounded_lattice }
end galois_insertion
|
34dd5454f0c375d47eef397931e29aac9258ebda | c777c32c8e484e195053731103c5e52af26a25d1 | /src/ring_theory/dedekind_domain/pid.lean | fd41b7a7737fbd574776fe42158867590be719ff | [
"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 | 12,579 | lean | /-
Copyright (c) 2023 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import ring_theory.dedekind_domain.dvr
import ring_theory.dedekind_domain.ideal
/-!
# Proving a Dedekind domain is a PID
This file contains some results that we can use to show all ideals in a Dedekind domain are
principal.
## Main results
* `ideal.is_principal.of_finite_maximals_of_is_unit`: an invertible ideal in a commutative ring
with finitely many maximal ideals, is a principal ideal.
* `is_principal_ideal_ring.of_finite_primes`: if a Dedekind domain has finitely many prime ideals,
it is a principal ideal domain.
-/
variables {R : Type*} [comm_ring R]
open ideal
open unique_factorization_monoid
open_locale big_operators
open_locale non_zero_divisors
open unique_factorization_monoid
/-- Let `P` be a prime ideal, `x ∈ P \ P²` and `x ∉ Q` for all prime ideals `Q ≠ P`.
Then `P` is generated by `x`. -/
lemma ideal.eq_span_singleton_of_mem_of_not_mem_sq_of_not_mem_prime_ne
{P : ideal R} (hP : P.is_prime) [is_domain R] [is_dedekind_domain R]
{x : R} (x_mem : x ∈ P) (hxP2 : x ∉ P^2)
(hxQ : ∀ (Q : ideal R), is_prime Q → Q ≠ P → x ∉ Q) :
P = ideal.span {x} :=
begin
letI := classical.dec_eq (ideal R),
have hx0 : x ≠ 0,
{ rintro rfl,
exact hxP2 (zero_mem _) },
by_cases hP0 : P = ⊥,
{ unfreezingI { subst hP0 },
simpa using hxP2 },
have hspan0 : span ({x} : set R) ≠ ⊥ := mt ideal.span_singleton_eq_bot.mp hx0,
have span_le := (ideal.span_singleton_le_iff_mem _).mpr x_mem,
refine associated_iff_eq.mp
((associated_iff_normalized_factors_eq_normalized_factors hP0 hspan0).mpr
(le_antisymm ((dvd_iff_normalized_factors_le_normalized_factors hP0 hspan0).mp _) _)),
{ rwa [ideal.dvd_iff_le, ideal.span_singleton_le_iff_mem] },
simp only [normalized_factors_irreducible ((ideal.prime_of_is_prime hP0 hP).irreducible),
normalize_eq, multiset.le_iff_count, multiset.count_singleton],
intros Q,
split_ifs with hQ,
{ unfreezingI { subst hQ },
refine (ideal.count_normalized_factors_eq _ _).le;
simp only [ideal.span_singleton_le_iff_mem, pow_one];
assumption },
by_cases hQp : is_prime Q,
{ resetI,
refine (ideal.count_normalized_factors_eq _ _).le;
simp only [ideal.span_singleton_le_iff_mem, pow_one, pow_zero, one_eq_top, submodule.mem_top],
exact hxQ _ hQp hQ },
{ exact (multiset.count_eq_zero.mpr (λ hQi, hQp (is_prime_of_prime (irreducible_iff_prime.mp
(irreducible_of_normalized_factor _ hQi))))).le }
end
lemma fractional_ideal.is_principal_of_unit_of_comap_mul_span_singleton_eq_top
{R A : Type*} [comm_ring R] [comm_ring A] [algebra R A] {S : submonoid R} [is_localization S A]
(I : (fractional_ideal S A)ˣ) {v : A} (hv : v ∈ (↑I⁻¹ : fractional_ideal S A))
(h : submodule.comap (algebra.linear_map R A) (I * submodule.span R {v}) = ⊤) :
submodule.is_principal (I : submodule R A) :=
begin
have hinv := I.mul_inv,
set J := submodule.comap (algebra.linear_map R A) (I * submodule.span R {v}),
have hJ : is_localization.coe_submodule A J = I * submodule.span R {v},
{ rw [subtype.ext_iff, fractional_ideal.coe_mul, fractional_ideal.coe_one] at hinv,
apply submodule.map_comap_eq_self,
rw [← submodule.one_eq_range, ← hinv],
exact submodule.mul_le_mul_right ((submodule.span_singleton_le_iff_mem _ _).2 hv) },
have : (1 : A) ∈ ↑I * submodule.span R {v},
{ rw [← hJ, h, is_localization.coe_submodule_top, submodule.mem_one],
exact ⟨1, (algebra_map R _).map_one⟩ },
obtain ⟨w, hw, hvw⟩ := submodule.mem_mul_span_singleton.1 this,
refine ⟨⟨w, _⟩⟩,
rw [← fractional_ideal.coe_span_singleton S, ← inv_inv I, eq_comm, coe_coe],
refine congr_arg coe (units.eq_inv_of_mul_eq_one_left (le_antisymm _ _)),
{ apply_instance },
{ conv_rhs { rw [← hinv, mul_comm] },
apply fractional_ideal.mul_le_mul_left (fractional_ideal.span_singleton_le_iff_mem.mpr hw) },
{ rw [fractional_ideal.one_le, ← hvw, mul_comm],
exact fractional_ideal.mul_mem_mul hv (fractional_ideal.mem_span_singleton_self _ _) }
end
/--
An invertible fractional ideal of a commutative ring with finitely many maximal ideals is principal.
https://math.stackexchange.com/a/95857 -/
theorem fractional_ideal.is_principal.of_finite_maximals_of_inv
{A : Type*} [comm_ring A] [algebra R A] {S : submonoid R} [is_localization S A]
(hS : S ≤ R⁰) (hf : {I : ideal R | I.is_maximal}.finite)
(I I' : fractional_ideal S A) (hinv : I * I' = 1) :
submodule.is_principal (I : submodule R A) :=
begin
have hinv' := hinv,
rw [subtype.ext_iff, fractional_ideal.coe_mul] at hinv,
let s := hf.to_finset,
haveI := classical.dec_eq (ideal R),
have coprime : ∀ (M ∈ s) (M' ∈ s.erase M), M ⊔ M' = ⊤,
{ simp_rw [finset.mem_erase, hf.mem_to_finset],
rintro M hM M' ⟨hne, hM'⟩,
exact ideal.is_maximal.coprime_of_ne hM hM' hne.symm },
have nle : ∀ M ∈ s, ¬ (⨅ M' ∈ s.erase M, M') ≤ M := λ M hM, left_lt_sup.1
((hf.mem_to_finset.1 hM).ne_top.lt_top.trans_eq (ideal.sup_infi_eq_top $ coprime M hM).symm),
have : ∀ M ∈ s, ∃ (a ∈ I) (b ∈ I'), a * b ∉ is_localization.coe_submodule A M,
{ intros M hM, by_contra' h,
obtain ⟨x, hx, hxM⟩ := set_like.exists_of_lt ((is_localization.coe_submodule_strict_mono
hS (hf.mem_to_finset.1 hM).ne_top.lt_top).trans_eq hinv.symm),
refine hxM (submodule.map₂_le.2 _ hx), exact h },
choose! a ha b hb hm using this,
choose! u hu hum using λ M hM, set_like.not_le_iff_exists.1 (nle M hM),
let v := ∑ M in s, u M • b M,
have hv : v ∈ I' := submodule.sum_mem _ (λ M hM, submodule.smul_mem _ _ $ hb M hM),
refine fractional_ideal.is_principal_of_unit_of_comap_mul_span_singleton_eq_top
(units.mk_of_mul_eq_one I I' hinv') hv (of_not_not $ λ h, _),
obtain ⟨M, hM, hJM⟩ := ideal.exists_le_maximal _ h,
replace hM := hf.mem_to_finset.2 hM,
have : ∀ (a ∈ I) (b ∈ I'), ∃ c, algebra_map R _ c = a * b,
{ intros a ha b hb, have hi := hinv.le,
obtain ⟨c, -, hc⟩ := hi (submodule.mul_mem_mul ha hb),
exact ⟨c, hc⟩ },
have hmem: a M * v ∈ is_localization.coe_submodule A M,
{ obtain ⟨c, hc⟩ := this _ (ha M hM) v hv,
refine is_localization.coe_submodule_mono _ hJM ⟨c, _, hc⟩,
have := submodule.mul_mem_mul (ha M hM) (submodule.mem_span_singleton_self v),
rwa ← hc at this },
simp_rw [finset.mul_sum, mul_smul_comm] at hmem,
rw [← s.add_sum_erase _ hM, submodule.add_mem_iff_left] at hmem,
{ refine hm M hM _,
obtain ⟨c, (hc : algebra_map R A c = a M * b M)⟩ := this _ (ha M hM) _ (hb M hM),
rw ← hc at hmem ⊢,
rw [algebra.smul_def, ← _root_.map_mul] at hmem,
obtain ⟨d, hdM, he⟩ := hmem,
rw is_localization.injective _ hS he at hdM,
exact submodule.mem_map_of_mem
(((hf.mem_to_finset.1 hM).is_prime.mem_or_mem hdM).resolve_left $ hum M hM) },
{ refine submodule.sum_mem _ (λ M' hM', _),
rw finset.mem_erase at hM',
obtain ⟨c, hc⟩ := this _ (ha M hM) _ (hb M' hM'.2),
rw [← hc, algebra.smul_def, ← _root_.map_mul],
specialize hu M' hM'.2,
simp_rw [ideal.mem_infi, finset.mem_erase] at hu,
exact submodule.mem_map_of_mem (M.mul_mem_right _ $ hu M ⟨hM'.1.symm, hM⟩) },
end
/-- An invertible ideal in a commutative ring with finitely many maximal ideals is principal.
https://math.stackexchange.com/a/95857 -/
theorem ideal.is_principal.of_finite_maximals_of_is_unit
(hf : {I : ideal R | I.is_maximal}.finite)
{I : ideal R} (hI : is_unit (I : fractional_ideal R⁰ (fraction_ring R))) :
I.is_principal :=
(is_localization.coe_submodule_is_principal _ le_rfl).mp
(fractional_ideal.is_principal.of_finite_maximals_of_inv le_rfl hf I
(↑(hI.unit⁻¹) : fractional_ideal R⁰ (fraction_ring R))
hI.unit.mul_inv)
/-- A Dedekind domain is a PID if its set of primes is finite. -/
theorem is_principal_ideal_ring.of_finite_primes [is_domain R] [is_dedekind_domain R]
(h : {I : ideal R | I.is_prime}.finite) :
is_principal_ideal_ring R :=
⟨λ I, begin
obtain rfl | hI := eq_or_ne I ⊥,
{ exact bot_is_principal },
apply ideal.is_principal.of_finite_maximals_of_is_unit,
{ apply h.subset, exact @ideal.is_maximal.is_prime _ _ },
{ exact is_unit_of_mul_eq_one _ _ (fractional_ideal.coe_ideal_mul_inv I hI) },
end⟩
variables [is_domain R] [is_dedekind_domain R]
variables (S : Type*) [comm_ring S] [is_domain S]
variables [algebra R S] [module.free R S] [module.finite R S]
variables (p : ideal R) (hp0 : p ≠ ⊥) [is_prime p]
variables {Sₚ : Type*} [comm_ring Sₚ] [algebra S Sₚ]
variables [is_localization (algebra.algebra_map_submonoid S p.prime_compl) Sₚ]
variables [algebra R Sₚ] [is_scalar_tower R S Sₚ]
/- The first hypothesis below follows from properties of the localization but is needed for the
second, so we leave it to the user to provide (automatically). -/
variables [is_domain Sₚ] [is_dedekind_domain Sₚ]
include S hp0
/-- If `p` is a prime in the Dedekind domain `R`, `S` an extension of `R` and `Sₚ` the localization
of `S` at `p`, then all primes in `Sₚ` are factors of the image of `p` in `Sₚ`. -/
lemma is_localization.over_prime.mem_normalized_factors_of_is_prime [decidable_eq (ideal Sₚ)]
{P : ideal Sₚ} (hP : is_prime P) (hP0 : P ≠ ⊥) :
P ∈ normalized_factors (ideal.map (algebra_map R Sₚ) p) :=
begin
have non_zero_div : algebra.algebra_map_submonoid S p.prime_compl ≤ S⁰ :=
map_le_non_zero_divisors_of_injective _ (no_zero_smul_divisors.algebra_map_injective _ _)
p.prime_compl_le_non_zero_divisors,
letI : algebra (localization.at_prime p) Sₚ := localization_algebra p.prime_compl S,
haveI : is_scalar_tower R (localization.at_prime p) Sₚ := is_scalar_tower.of_algebra_map_eq
(λ x, by erw [is_localization.map_eq, is_scalar_tower.algebra_map_apply R S]),
obtain ⟨pid, p', ⟨hp'0, hp'p⟩, hpu⟩ :=
(discrete_valuation_ring.iff_pid_with_one_nonzero_prime (localization.at_prime p)).mp
(is_localization.at_prime.discrete_valuation_ring_of_dedekind_domain R hp0 _),
have : local_ring.maximal_ideal (localization.at_prime p) ≠ ⊥,
{ rw submodule.ne_bot_iff at ⊢ hp0,
obtain ⟨x, x_mem, x_ne⟩ := hp0,
exact ⟨algebra_map _ _ x,
(is_localization.at_prime.to_map_mem_maximal_iff _ _ _).mpr x_mem,
is_localization.to_map_ne_zero_of_mem_non_zero_divisors _ p.prime_compl_le_non_zero_divisors
(mem_non_zero_divisors_of_ne_zero x_ne)⟩ },
rw [← multiset.singleton_le, ← normalize_eq P,
← normalized_factors_irreducible (ideal.prime_of_is_prime hP0 hP).irreducible,
← dvd_iff_normalized_factors_le_normalized_factors hP0, dvd_iff_le,
is_scalar_tower.algebra_map_eq R (localization.at_prime p) Sₚ, ← ideal.map_map,
localization.at_prime.map_eq_maximal_ideal, ideal.map_le_iff_le_comap,
hpu (local_ring.maximal_ideal _) ⟨this, _⟩, hpu (comap _ _) ⟨_, _⟩],
{ exact le_rfl },
{ have hRS : algebra.is_integral R S := is_integral_of_noetherian
(is_noetherian_of_fg_of_noetherian' module.finite.out),
exact mt (ideal.eq_bot_of_comap_eq_bot (is_integral_localization hRS)) hP0 },
{ exact ideal.comap_is_prime (algebra_map (localization.at_prime p) Sₚ) P },
{ exact (local_ring.maximal_ideal.is_maximal _).is_prime },
{ rw [ne.def, zero_eq_bot, ideal.map_eq_bot_iff_of_injective],
{ assumption },
rw is_scalar_tower.algebra_map_eq R S Sₚ,
exact (is_localization.injective Sₚ non_zero_div).comp
(no_zero_smul_divisors.algebra_map_injective _ _) },
end
/-- Let `p` be a prime in the Dedekind domain `R` and `S` be an integral extension of `R`,
then the localization `Sₚ` of `S` at `p` is a PID. -/
theorem is_dedekind_domain.is_principal_ideal_ring_localization_over_prime :
is_principal_ideal_ring Sₚ :=
begin
letI := classical.dec_eq (ideal Sₚ),
letI := classical.dec_pred (λ (P : ideal Sₚ), P.is_prime),
refine is_principal_ideal_ring.of_finite_primes
(set.finite.of_finset (finset.filter (λ P, P.is_prime)
({⊥} ∪ (normalized_factors (ideal.map (algebra_map R Sₚ) p)).to_finset))
(λ P, _)),
rw [finset.mem_filter, finset.mem_union, finset.mem_singleton, set.mem_set_of,
multiset.mem_to_finset],
exact and_iff_right_of_imp (λ hP, or_iff_not_imp_left.mpr
(is_localization.over_prime.mem_normalized_factors_of_is_prime S p hp0 hP))
end
|
d31572c83e43a52f9ee3e46f9e9c1e457270a135 | 94637389e03c919023691dcd05bd4411b1034aa5 | /src/zzz_junk/04_type_library_copy/01_unit.lean | cf4030f7abe5b40e36bde25765a5411e315a9988 | [] | no_license | kevinsullivan/complogic-s21 | 7c4eef2105abad899e46502270d9829d913e8afc | 99039501b770248c8ceb39890be5dfe129dc1082 | refs/heads/master | 1,682,985,669,944 | 1,621,126,241,000 | 1,621,126,241,000 | 335,706,272 | 0 | 38 | null | 1,618,325,669,000 | 1,612,374,118,000 | Lean | UTF-8 | Lean | false | false | 577 | lean | namespace hidden
/-
The unit type is a computational
data type inhabited by just one
constant, in lean called *star*.
A value of this type is available
at all times and doesn't carry any
information at all (wheras a value
of type bool carries one bit).
There's not much reason ever to
pass a unit value to a function,
as it conveys no useful information.
The place where the unit type is
of use is as the return type of a
function whose only purpose is to
produce a non-functional "effect"
(such as IO). More on that later.
-/
inductive unit : Type
| star
end hidden |
a5a6a1310a2d0aff6cd740b0e48849a7a5713538 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /archive/miu_language/basic.lean | 49c73d8be195a594b7d2d9a5f4f2b886fde460f3 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 6,035 | lean | /-
Copyright (c) 2020 Gihan Marasingha. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gihan Marasingha
-/
import tactic.linarith
/-!
# An MIU Decision Procedure in Lean
The [MIU formal system](https://en.wikipedia.org/wiki/MU_puzzle) was introduced by Douglas
Hofstadter in the first chapter of his 1979 book,
[Gödel, Escher, Bach](https://en.wikipedia.org/wiki/G%C3%B6del,_Escher,_Bach).
The system is defined by four rules of inference, one axiom, and an alphabet of three symbols:
`M`, `I`, and `U`.
Hofstadter's central question is: can the string `"MU"` be derived?
It transpires that there is a simple decision procedure for this system. A string is derivable if
and only if it starts with `M`, contains no other `M`s, and the number of `I`s in the string is
congruent to 1 or 2 modulo 3.
The principal aim of this project is to give a Lean proof that the derivability of a string is a
decidable predicate.
## The MIU System
In Hofstadter's description, an _atom_ is any one of `M`, `I` or `U`. A _string_ is a finite
sequence of zero or more symbols. To simplify notation, we write a sequence `[I,U,U,M]`,
for example, as `IUUM`.
The four rules of inference are:
1. xI → xIU,
2. Mx → Mxx,
3. xIIIy → xUy,
4. xUUy → xy,
where the notation α → β is to be interpreted as 'if α is derivable, then β is derivable'.
Additionally, he has an axiom:
* `MI` is derivable.
In Lean, it is natural to treat the rules of inference and the axiom on an equal footing via an
inductive data type `derivable` designed so that `derviable x` represents the notion that the string
`x` can be derived from the axiom by the rules of inference. The axiom is represented as a
nonrecursive constructor for `derivable`. This mirrors the translation of Peano's axiom '0 is a
natural number' into the nonrecursive constructor `zero` of the inductive type `nat`.
## References
* [Jeremy Avigad, Leonardo de Moura and Soonho Kong, _Theorem Proving in Lean_]
[avigad_moura_kong-2017]
* [Douglas R Hofstadter, _Gödel, Escher, Bach_][Hofstadter-1979]
## Tags
miu, derivable strings
-/
namespace miu
/-!
### Declarations and instance derivations for `miu_atom` and `miustr`
-/
/--
The atoms of MIU can be represented as an enumerated type in Lean.
-/
@[derive decidable_eq]
inductive miu_atom : Type
| M : miu_atom
| I : miu_atom
| U : miu_atom
/-!
The annotation `@[derive decidable_eq]` above assigns the attribute `derive` to `miu_atom`, through
which Lean automatically derives that `miu_atom` is an instance of `decidable_eq`. The use of
`derive` is crucial in this project and will lead to the automatic derivation of decidability.
-/
open miu_atom
/--
We show that the type `miu_atom` is inhabited, giving `M` (for no particular reason) as the default
element.
-/
instance miu_atom_inhabited : inhabited miu_atom :=
inhabited.mk M
/--
`miu_atom.repr` is the 'natural' function from `miu_atom` to `string`.
-/
def miu_atom.repr : miu_atom → string
| M := "M"
| I := "I"
| U := "U"
/--
Using `miu_atom.repr`, we prove that ``miu_atom` is an instance of `has_repr`.
-/
instance : has_repr miu_atom :=
⟨λ u, u.repr⟩
/--
For simplicity, an `miustr` is just a list of elements of type `miu_atom`.
-/
@[derive [has_append, has_mem miu_atom]]
def miustr := list miu_atom
/--
For display purposes, an `miustr` can be represented as a `string`.
-/
def miustr.mrepr : miustr → string
| [] := ""
| (c::cs) := c.repr ++ (miustr.mrepr cs)
instance miurepr : has_repr miustr :=
⟨λ u, u.mrepr⟩
/--
In the other direction, we set up a coercion from `string` to `miustr`.
-/
def lchar_to_miustr : (list char) → miustr
| [] := []
| (c::cs) :=
let ms := lchar_to_miustr cs in
match c with
| 'M' := M::ms
| 'I' := I::ms
| 'U' := U::ms
| _ := []
end
instance string_coe_miustr : has_coe string miustr :=
⟨λ st, lchar_to_miustr st.data ⟩
/-!
### Derivability
-/
/--
The inductive type `derivable` has five constructors. The nonrecursive constructor `mk` corresponds
to Hofstadter's axiom that `"MI"` is derivable. Each of the constructors `r1`, `r2`, `r3`, `r4`
corresponds to the one of Hofstadter's rules of inference.
-/
inductive derivable : miustr → Prop
| mk : derivable "MI"
| r1 {x} : derivable (x ++ [I]) → derivable (x ++ [I, U])
| r2 {x} : derivable (M :: x) → derivable (M :: x ++ x)
| r3 {x y} : derivable (x ++ [I, I, I] ++ y) → derivable (x ++ U :: y)
| r4 {x y} : derivable (x ++ [U, U] ++ y) → derivable (x ++ y)
/-!
### Rule usage examples
-/
example (h : derivable "UMI") : derivable "UMIU" :=
begin
change ("UMIU" : miustr) with [U,M] ++ [I,U],
exact derivable.r1 h, -- Rule 1
end
example (h : derivable "MIIU") : derivable "MIIUIIU" :=
begin
change ("MIIUIIU" : miustr) with M :: [I,I,U] ++ [I,I,U],
exact derivable.r2 h, -- Rule 2
end
example (h : derivable "UIUMIIIMMM") : derivable "UIUMUMMM" :=
begin
change ("UIUMUMMM" : miustr) with [U,I,U,M] ++ U :: [M,M,M],
exact derivable.r3 h, -- Rule 3
end
example (h : derivable "MIMIMUUIIM") : derivable "MIMIMIIM" :=
begin
change ("MIMIMIIM" : miustr) with [M,I,M,I,M] ++ [I,I,M],
exact derivable.r4 h, -- Rule 4
end
/-!
### Derivability examples
-/
private lemma MIU_der : derivable "MIU":=
begin
change ("MIU" :miustr) with [M] ++ [I,U],
apply derivable.r1, -- reduce to deriving "MI",
constructor, -- which is the base of the inductive construction.
end
example : derivable "MIUIU" :=
begin
change ("MIUIU" : miustr) with M :: [I,U] ++ [I,U],
exact derivable.r2 MIU_der, -- `"MIUIU"` can be derived as `"MIU"` can.
end
example : derivable "MUI" :=
begin
have h₂ : derivable "MII",
{ change ("MII" : miustr) with M :: [I] ++ [I],
exact derivable.r2 derivable.mk, },
have h₃ : derivable "MIIII",
{ change ("MIIII" : miustr) with M :: [I,I] ++ [I,I],
exact derivable.r2 h₂, },
change ("MUI" : miustr) with [M] ++ U :: [I],
exact derivable.r3 h₃, -- We prove our main goal using rule 3
end
end miu
|
6b340c24d7e743cee3dad68e24635940e498baee | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/vec_inv.lean | 681d64eb133691b103fac69454e0010fc4cf8b25 | [
"Apache-2.0"
] | permissive | tjiaqi/lean | 4634d729795c164664d10d093f3545287c76628f | d0ce4cf62f4246b0600c07e074d86e51f2195e30 | refs/heads/master | 1,622,323,796,480 | 1,422,643,069,000 | 1,422,643,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 732 | lean | import data.nat.basic data.empty data.prod
open nat eq.ops prod
inductive vector (T : Type) : ℕ → Type :=
nil {} : vector T 0,
cons : T → ∀{n}, vector T n → vector T (succ n)
set_option pp.metavar_args true
set_option pp.implicit true
set_option pp.notation false
namespace vector
variables {A B C : Type}
variables {n m : nat}
theorem z_cases_on {C : vector A 0 → Type} (v : vector A 0) (Hnil : C nil) : C v :=
begin
cases v,
apply Hnil
end
protected definition destruct (v : vector A (succ n)) {P : Π {n : nat}, vector A (succ n) → Type}
(H : Π {n : nat} (h : A) (t : vector A n), P (cons h t)) : P v :=
begin
cases v,
apply (H a a_1)
end
end vector
|
048aa1c9bb0d927e4c722e132a045c99b5efeb0b | 10834b3de906c60d1989336d7506a00ee5e52c08 | /library/init/data/int/order.lean | 6196afbb380826615abca03968dcf318818110ed | [
"Apache-2.0"
] | permissive | Jack-Pumpkinhead/lean | 16657b5ad7f5316f007842fa2426b7f91212eae8 | 81cabd7b5a8f789633639f5fba64b45d31e37259 | refs/heads/master | 1,621,358,983,310 | 1,585,070,616,000 | 1,585,070,616,000 | 251,335,905 | 0 | 0 | Apache-2.0 | 1,585,579,683,000 | 1,585,579,682,000 | null | UTF-8 | Lean | false | false | 11,895 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The order relation on the integers.
-/
prelude
import init.data.int.basic init.data.ordering.basic
local attribute [simp] sub_eq_add_neg
namespace int
private def nonneg (a : ℤ) : Prop := int.cases_on a (assume n, true) (assume n, false)
protected def le (a b : ℤ) : Prop := nonneg (b - a)
instance : has_le int := ⟨int.le⟩
protected def lt (a b : ℤ) : Prop := (a + 1) ≤ b
instance : has_lt int := ⟨int.lt⟩
private def decidable_nonneg (a : ℤ) : decidable (nonneg a) :=
int.cases_on a (assume a, decidable.true) (assume a, decidable.false)
instance decidable_le (a b : ℤ) : decidable (a ≤ b) := decidable_nonneg _
instance decidable_lt (a b : ℤ) : decidable (a < b) := decidable_nonneg _
lemma lt_iff_add_one_le (a b : ℤ) : a < b ↔ a + 1 ≤ b := iff.refl _
private lemma nonneg.elim {a : ℤ} : nonneg a → ∃ n : ℕ, a = n :=
int.cases_on a (assume n H, exists.intro n rfl) (assume n', false.elim)
private lemma nonneg_or_nonneg_neg (a : ℤ) : nonneg a ∨ nonneg (-a) :=
int.cases_on a (assume n, or.inl trivial) (assume n, or.inr trivial)
lemma le.intro_sub {a b : ℤ} {n : ℕ} (h : b - a = n) : a ≤ b :=
show nonneg (b - a), by rw h; trivial
lemma le.intro {a b : ℤ} {n : ℕ} (h : a + n = b) : a ≤ b :=
le.intro_sub (by rw [← h, add_comm]; simp)
lemma le.dest_sub {a b : ℤ} (h : a ≤ b) : ∃ n : ℕ, b - a = n := nonneg.elim h
lemma le.dest {a b : ℤ} (h : a ≤ b) : ∃ n : ℕ, a + n = b :=
match (le.dest_sub h) with
| ⟨n, h₁⟩ := exists.intro n begin rw [← h₁, add_comm], simp end
end
lemma le.elim {a b : ℤ} (h : a ≤ b) {P : Prop} (h' : ∀ n : ℕ, a + ↑n = b → P) : P :=
exists.elim (le.dest h) h'
protected lemma le_total (a b : ℤ) : a ≤ b ∨ b ≤ a :=
or.imp_right
(assume H : nonneg (-(b - a)),
have -(b - a) = a - b, by simp,
show nonneg (a - b), from this ▸ H)
(nonneg_or_nonneg_neg (b - a))
lemma coe_nat_le_coe_nat_of_le {m n : ℕ} (h : m ≤ n) : (↑m : ℤ) ≤ ↑n :=
match nat.le.dest h with
| ⟨k, (hk : m + k = n)⟩ := le.intro (begin rw [← hk], reflexivity end)
end
lemma le_of_coe_nat_le_coe_nat {m n : ℕ} (h : (↑m : ℤ) ≤ ↑n) : m ≤ n :=
le.elim h (assume k, assume hk : ↑m + ↑k = ↑n,
have m + k = n, from int.coe_nat_inj ((int.coe_nat_add m k).trans hk),
nat.le.intro this)
lemma coe_nat_le_coe_nat_iff (m n : ℕ) : (↑m : ℤ) ≤ ↑n ↔ m ≤ n :=
iff.intro le_of_coe_nat_le_coe_nat coe_nat_le_coe_nat_of_le
lemma coe_zero_le (n : ℕ) : 0 ≤ (↑n : ℤ) :=
coe_nat_le_coe_nat_of_le n.zero_le
lemma eq_coe_of_zero_le {a : ℤ} (h : 0 ≤ a) : ∃ n : ℕ, a = n :=
by have t := le.dest_sub h; simp at t; exact t
lemma eq_succ_of_zero_lt {a : ℤ} (h : 0 < a) : ∃ n : ℕ, a = n.succ :=
let ⟨n, (h : ↑(1+n) = a)⟩ := le.dest h in
⟨n, by rw add_comm at h; exact h.symm⟩
lemma lt_add_succ (a : ℤ) (n : ℕ) : a < a + ↑(nat.succ n) :=
le.intro (show a + 1 + n = a + nat.succ n, begin simp [int.coe_nat_eq, add_comm, add_left_comm], reflexivity end)
lemma lt.intro {a b : ℤ} {n : ℕ} (h : a + nat.succ n = b) : a < b :=
h ▸ lt_add_succ a n
lemma lt.dest {a b : ℤ} (h : a < b) : ∃ n : ℕ, a + ↑(nat.succ n) = b :=
le.elim h (assume n, assume hn : a + 1 + n = b,
exists.intro n begin rw [← hn, add_assoc, add_comm (1 : int)], reflexivity end)
lemma lt.elim {a b : ℤ} (h : a < b) {P : Prop} (h' : ∀ n : ℕ, a + ↑(nat.succ n) = b → P) : P :=
exists.elim (lt.dest h) h'
lemma coe_nat_lt_coe_nat_iff (n m : ℕ) : (↑n : ℤ) < ↑m ↔ n < m :=
begin rw [lt_iff_add_one_le, ← int.coe_nat_succ, coe_nat_le_coe_nat_iff], reflexivity end
lemma lt_of_coe_nat_lt_coe_nat {m n : ℕ} (h : (↑m : ℤ) < ↑n) : m < n :=
(coe_nat_lt_coe_nat_iff _ _).mp h
lemma coe_nat_lt_coe_nat_of_lt {m n : ℕ} (h : m < n) : (↑m : ℤ) < ↑n :=
(coe_nat_lt_coe_nat_iff _ _).mpr h
/- show that the integers form an ordered additive group -/
protected lemma le_refl (a : ℤ) : a ≤ a :=
le.intro (add_zero a)
protected lemma le_trans {a b c : ℤ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c :=
le.elim h₁ (assume n, assume hn : a + n = b,
le.elim h₂ (assume m, assume hm : b + m = c,
begin apply le.intro, rw [← hm, ← hn, add_assoc], reflexivity end))
protected lemma le_antisymm {a b : ℤ} (h₁ : a ≤ b) (h₂ : b ≤ a) : a = b :=
le.elim h₁ (assume n, assume hn : a + n = b,
le.elim h₂ (assume m, assume hm : b + m = a,
have a + ↑(n + m) = a + 0, by rw [int.coe_nat_add, ← add_assoc, hn, hm, add_zero a],
have (↑(n + m) : ℤ) = 0, from add_left_cancel this,
have n + m = 0, from int.coe_nat_inj this,
have n = 0, from nat.eq_zero_of_add_eq_zero_right this,
show a = b, begin rw [← hn, this, int.coe_nat_zero, add_zero a] end))
protected lemma lt_irrefl (a : ℤ) : ¬ a < a :=
assume : a < a,
lt.elim this (assume n, assume hn : a + nat.succ n = a,
have a + nat.succ n = a + 0, by rw [hn, add_zero],
have nat.succ n = 0, from int.coe_nat_inj (add_left_cancel this),
show false, from nat.succ_ne_zero _ this)
protected lemma ne_of_lt {a b : ℤ} (h : a < b) : a ≠ b :=
(assume : a = b, absurd (begin rewrite this at h, exact h end) (int.lt_irrefl b))
lemma le_of_lt {a b : ℤ} (h : a < b) : a ≤ b :=
lt.elim h (assume n, assume hn : a + nat.succ n = b, le.intro hn)
protected lemma lt_iff_le_and_ne (a b : ℤ) : a < b ↔ (a ≤ b ∧ a ≠ b) :=
iff.intro
(assume h, ⟨le_of_lt h, int.ne_of_lt h⟩)
(assume ⟨aleb, aneb⟩,
le.elim aleb (assume n, assume hn : a + n = b,
have n ≠ 0,
from (assume : n = 0, aneb begin rw [← hn, this, int.coe_nat_zero, add_zero] end),
have n = nat.succ (nat.pred n),
from eq.symm (nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero this)),
lt.intro (begin rewrite this at hn, exact hn end)))
lemma lt_succ (a : ℤ) : a < a + 1 :=
int.le_refl (a + 1)
protected lemma add_le_add_left {a b : ℤ} (h : a ≤ b) (c : ℤ) : c + a ≤ c + b :=
le.elim h (assume n, assume hn : a + n = b,
le.intro (show c + a + n = c + b, begin rw [add_assoc, hn] end))
protected lemma add_lt_add_left {a b : ℤ} (h : a < b) (c : ℤ) : c + a < c + b :=
iff.mpr (int.lt_iff_le_and_ne _ _)
(and.intro
(int.add_le_add_left (le_of_lt h) _)
(assume heq, int.lt_irrefl b begin rw add_left_cancel heq at h, exact h end))
protected lemma mul_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b :=
le.elim ha (assume n, assume hn,
le.elim hb (assume m, assume hm,
le.intro (show 0 + ↑n * ↑m = a * b, begin rw [← hn, ← hm], simp [zero_add] end)))
protected lemma mul_pos {a b : ℤ} (ha : 0 < a) (hb : 0 < b) : 0 < a * b :=
lt.elim ha (assume n, assume hn,
lt.elim hb (assume m, assume hm,
lt.intro (show 0 + ↑(nat.succ (nat.succ n * m + n)) = a * b,
begin rw [← hn, ← hm], simp [int.coe_nat_zero],
rw [← int.coe_nat_mul], simp [nat.mul_succ, nat.succ_add] end)))
protected lemma zero_lt_one : (0 : ℤ) < 1 := trivial
protected lemma lt_iff_le_not_le {a b : ℤ} : a < b ↔ (a ≤ b ∧ ¬ b ≤ a) :=
begin
simp [int.lt_iff_le_and_ne], split; intro h,
{ cases h with hab hn, split,
{ assumption },
{ intro hba, simp [int.le_antisymm hab hba] at *, contradiction } },
{ cases h with hab hn, split,
{ assumption },
{ intro h, simp [*] at * } }
end
instance : decidable_linear_ordered_comm_ring int :=
{ le := int.le,
le_refl := int.le_refl,
le_trans := @int.le_trans,
le_antisymm := @int.le_antisymm,
lt := int.lt,
lt_iff_le_not_le := @int.lt_iff_le_not_le,
add_le_add_left := @int.add_le_add_left,
add_lt_add_left := @int.add_lt_add_left,
zero_ne_one := int.zero_ne_one,
mul_nonneg := @int.mul_nonneg,
mul_pos := @int.mul_pos,
le_total := int.le_total,
zero_lt_one := int.zero_lt_one,
decidable_eq := int.decidable_eq,
decidable_le := int.decidable_le,
decidable_lt := int.decidable_lt,
..int.comm_ring }
instance : decidable_linear_ordered_comm_group int :=
by apply_instance
lemma eq_nat_abs_of_zero_le {a : ℤ} (h : 0 ≤ a) : a = nat_abs a :=
let ⟨n, e⟩ := eq_coe_of_zero_le h in by rw e; refl
lemma le_nat_abs {a : ℤ} : a ≤ nat_abs a :=
or.elim (le_total 0 a)
(λh, by rw eq_nat_abs_of_zero_le h; refl)
(λh, le_trans h (coe_zero_le _))
lemma neg_succ_lt_zero (n : ℕ) : -[1+ n] < 0 :=
lt_of_not_ge $ λ h, let ⟨m, h⟩ := eq_coe_of_zero_le h in by contradiction
lemma eq_neg_succ_of_lt_zero : ∀ {a : ℤ}, a < 0 → ∃ n : ℕ, a = -[1+ n]
| (n : ℕ) h := absurd h (not_lt_of_ge (coe_zero_le _))
| -[1+ n] h := ⟨n, rfl⟩
/- more facts specific to int -/
theorem of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n := trivial
theorem coe_succ_pos (n : nat) : (nat.succ n : ℤ) > 0 :=
coe_nat_lt_coe_nat_of_lt (nat.succ_pos _)
theorem exists_eq_neg_of_nat {a : ℤ} (H : a ≤ 0) : ∃n : ℕ, a = -n :=
let ⟨n, h⟩ := eq_coe_of_zero_le (neg_nonneg_of_nonpos H) in
⟨n, eq_neg_of_eq_neg h.symm⟩
theorem nat_abs_of_nonneg {a : ℤ} (H : a ≥ 0) : (nat_abs a : ℤ) = a :=
match a, eq_coe_of_zero_le H with ._, ⟨n, rfl⟩ := rfl end
theorem of_nat_nat_abs_of_nonpos {a : ℤ} (H : a ≤ 0) : (nat_abs a : ℤ) = -a :=
by rw [← nat_abs_neg, nat_abs_of_nonneg (neg_nonneg_of_nonpos H)]
theorem abs_eq_nat_abs : ∀ a : ℤ, abs a = nat_abs a
| (n : ℕ) := abs_of_nonneg $ coe_zero_le _
| -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _
theorem nat_abs_abs (a : ℤ) : nat_abs (abs a) = nat_abs a :=
by rw [abs_eq_nat_abs]; refl
theorem lt_of_add_one_le {a b : ℤ} (H : a + 1 ≤ b) : a < b := H
theorem add_one_le_of_lt {a b : ℤ} (H : a < b) : a + 1 ≤ b := H
theorem lt_add_one_of_le {a b : ℤ} (H : a ≤ b) : a < b + 1 :=
add_le_add_right H 1
theorem le_of_lt_add_one {a b : ℤ} (H : a < b + 1) : a ≤ b :=
le_of_add_le_add_right H
theorem sub_one_le_of_lt {a b : ℤ} (H : a ≤ b) : a - 1 < b :=
sub_right_lt_of_lt_add $ lt_add_one_of_le H
theorem lt_of_sub_one_le {a b : ℤ} (H : a - 1 < b) : a ≤ b :=
le_of_lt_add_one $ lt_add_of_sub_right_lt H
theorem le_sub_one_of_lt {a b : ℤ} (H : a < b) : a ≤ b - 1 :=
le_sub_right_of_add_le H
theorem lt_of_le_sub_one {a b : ℤ} (H : a ≤ b - 1) : a < b :=
add_le_of_le_sub_right H
theorem sign_of_succ (n : nat) : sign (nat.succ n) = 1 := rfl
theorem sign_eq_one_of_pos {a : ℤ} (h : 0 < a) : sign a = 1 :=
match a, eq_succ_of_zero_lt h with ._, ⟨n, rfl⟩ := rfl end
theorem sign_eq_neg_one_of_neg {a : ℤ} (h : a < 0) : sign a = -1 :=
match a, eq_neg_succ_of_lt_zero h with ._, ⟨n, rfl⟩ := rfl end
lemma eq_zero_of_sign_eq_zero : Π {a : ℤ}, sign a = 0 → a = 0
| 0 _ := rfl
theorem pos_of_sign_eq_one : ∀ {a : ℤ}, sign a = 1 → 0 < a
| (n+1:ℕ) _ := coe_nat_lt_coe_nat_of_lt (nat.succ_pos _)
theorem neg_of_sign_eq_neg_one : ∀ {a : ℤ}, sign a = -1 → a < 0
| (n+1:ℕ) h := match h with end
| 0 h := match h with end
| -[1+ n] _ := neg_succ_lt_zero _
theorem sign_eq_one_iff_pos (a : ℤ) : sign a = 1 ↔ 0 < a :=
⟨pos_of_sign_eq_one, sign_eq_one_of_pos⟩
theorem sign_eq_neg_one_iff_neg (a : ℤ) : sign a = -1 ↔ a < 0 :=
⟨neg_of_sign_eq_neg_one, sign_eq_neg_one_of_neg⟩
theorem sign_eq_zero_iff_zero (a : ℤ) : sign a = 0 ↔ a = 0 :=
⟨eq_zero_of_sign_eq_zero, λ h, by rw [h, sign_zero]⟩
theorem sign_mul_abs (a : ℤ) : sign a * abs a = a :=
by rw [abs_eq_nat_abs, sign_mul_nat_abs]
theorem eq_one_of_mul_eq_self_left {a b : ℤ} (Hpos : a ≠ 0) (H : b * a = a) : b = 1 :=
eq_of_mul_eq_mul_right Hpos (by rw [one_mul, H])
theorem eq_one_of_mul_eq_self_right {a b : ℤ} (Hpos : b ≠ 0) (H : b * a = b) : a = 1 :=
eq_of_mul_eq_mul_left Hpos (by rw [mul_one, H])
end int
|
4fa532aa17332c05be43efb6b45f048807f0ec1e | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Lean/Data/Options.lean | dbc41f4a2c9b6cbaecfb6d4cf310693034808c8e | [
"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",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 6,078 | lean | /-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich and Leonardo de Moura
-/
import Lean.ImportingFlag
import Lean.Data.KVMap
import Lean.Data.NameMap
namespace Lean
def Options := KVMap
def Options.empty : Options := {}
instance : Inhabited Options where
default := {}
instance : ToString Options := inferInstanceAs (ToString KVMap)
instance : ForIn m Options (Name × DataValue) := inferInstanceAs (ForIn _ KVMap _)
instance : BEq Options := inferInstanceAs (BEq KVMap)
structure OptionDecl where
declName : Name := by exact decl_name%
defValue : DataValue
group : String := ""
descr : String := ""
deriving Inhabited
def OptionDecls := NameMap OptionDecl
instance : Inhabited OptionDecls := ⟨({} : NameMap OptionDecl)⟩
private builtin_initialize optionDeclsRef : IO.Ref OptionDecls ← IO.mkRef (mkNameMap OptionDecl)
@[export lean_register_option]
def registerOption (name : Name) (decl : OptionDecl) : IO Unit := do
unless (← initializing) do
throw (IO.userError "failed to register option, options can only be registered during initialization")
let decls ← optionDeclsRef.get
if decls.contains name then
throw $ IO.userError s!"invalid option declaration '{name}', option already exists"
optionDeclsRef.set $ decls.insert name decl
def getOptionDecls : IO OptionDecls := optionDeclsRef.get
@[export lean_get_option_decls_array]
def getOptionDeclsArray : IO (Array (Name × OptionDecl)) := do
let decls ← getOptionDecls
pure $ decls.fold
(fun (r : Array (Name × OptionDecl)) k v => r.push (k, v))
#[]
def getOptionDecl (name : Name) : IO OptionDecl := do
let decls ← getOptionDecls
let (some decl) ← pure (decls.find? name) | throw $ IO.userError s!"unknown option '{name}'"
pure decl
def getOptionDefaultValue (name : Name) : IO DataValue := do
let decl ← getOptionDecl name
pure decl.defValue
def getOptionDescr (name : Name) : IO String := do
let decl ← getOptionDecl name
pure decl.descr
def setOptionFromString (opts : Options) (entry : String) : IO Options := do
let ps := (entry.splitOn "=").map String.trim
let [key, val] ← pure ps | throw $ IO.userError "invalid configuration option entry, it must be of the form '<key> = <value>'"
let key := Name.mkSimple key
let defValue ← getOptionDefaultValue key
match defValue with
| DataValue.ofString _ => pure $ opts.setString key val
| DataValue.ofBool _ =>
if key == `true then pure $ opts.setBool key true
else if key == `false then pure $ opts.setBool key false
else throw $ IO.userError s!"invalid Bool option value '{val}'"
| DataValue.ofName _ => pure $ opts.setName key val.toName
| DataValue.ofNat _ =>
match val.toNat? with
| none => throw (IO.userError s!"invalid Nat option value '{val}'")
| some v => pure $ opts.setNat key v
| DataValue.ofInt _ =>
match val.toInt? with
| none => throw (IO.userError s!"invalid Int option value '{val}'")
| some v => pure $ opts.setInt key v
| DataValue.ofSyntax _ => throw (IO.userError s!"invalid Syntax option value")
class MonadOptions (m : Type → Type) where
getOptions : m Options
export MonadOptions (getOptions)
instance [MonadLift m n] [MonadOptions m] : MonadOptions n where
getOptions := liftM (getOptions : m _)
variable [Monad m] [MonadOptions m]
def getBoolOption (k : Name) (defValue := false) : m Bool := do
let opts ← getOptions
return opts.getBool k defValue
def getNatOption (k : Name) (defValue := 0) : m Nat := do
let opts ← getOptions
return opts.getNat k defValue
class MonadWithOptions (m : Type → Type) where
withOptions (f : Options → Options) (x : m α) : m α
export MonadWithOptions (withOptions)
instance [MonadFunctor m n] [MonadWithOptions m] : MonadWithOptions n where
withOptions f x := monadMap (m := m) (withOptions f) x
/-! Remark: `_inPattern` is an internal option for communicating to the delaborator that
the term being delaborated should be treated as a pattern. -/
def withInPattern [MonadWithOptions m] (x : m α) : m α :=
withOptions (fun o => o.setBool `_inPattern true) x
def Options.getInPattern (o : Options) : Bool :=
o.getBool `_inPattern
/-- A strongly-typed reference to an option. -/
protected structure Option (α : Type) where
name : Name
defValue : α
deriving Inhabited
namespace Option
protected structure Decl (α : Type) where
defValue : α
group : String := ""
descr : String := ""
protected def get? [KVMap.Value α] (opts : Options) (opt : Lean.Option α) : Option α :=
opts.get? opt.name
protected def get [KVMap.Value α] (opts : Options) (opt : Lean.Option α) : α :=
opts.get opt.name opt.defValue
protected def set [KVMap.Value α] (opts : Options) (opt : Lean.Option α) (val : α) : Options :=
opts.set opt.name val
/-- Similar to `set`, but update `opts` only if it doesn't already contains an setting for `opt.name` -/
protected def setIfNotSet [KVMap.Value α] (opts : Options) (opt : Lean.Option α) (val : α) : Options :=
if opts.contains opt.name then opts else opt.set opts val
protected def register [KVMap.Value α] (name : Name) (decl : Lean.Option.Decl α) (ref : Name := by exact decl_name%) : IO (Lean.Option α) := do
registerOption name {
declName := ref
defValue := KVMap.Value.toDataValue decl.defValue
group := decl.group
descr := decl.descr
}
return { name := name, defValue := decl.defValue }
macro (name := registerBuiltinOption) doc?:(docComment)? "register_builtin_option" name:ident " : " type:term " := " decl:term : command =>
`($[$doc?]? builtin_initialize $name : Lean.Option $type ← Lean.Option.register $(quote name.getId) $decl)
macro (name := registerOption) doc?:(docComment)? "register_option" name:ident " : " type:term " := " decl:term : command =>
`($[$doc?]? initialize $name : Lean.Option $type ← Lean.Option.register $(quote name.getId) $decl)
end Option
end Lean
|
837c8f5d0bdba5a47bae28456b90d2a0220d12aa | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/order/filter/prod.lean | 7c93895fa46c37e72b957d3bb8721723298d72af | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 19,229 | lean | /-
Copyright (c) 2022 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johanes Hölzl, Patrick Massot, Yury Kudryashov, Kevin Wilson, Heather Macbeth
-/
import order.filter.basic
/-!
# Product and coproduct filters
In this file we define `filter.prod f g` (notation: `f ×ᶠ g`) and `filter.coprod f g`. The product
of two filters is the largest filter `l` such that `filter.tendsto prod.fst l f` and
`filter.tendsto prod.snd l g`.
## Implementation details
The product filter cannot be defined using the monad structure on filters. For example:
```lean
F := do {x ← seq, y ← top, return (x, y)}
G := do {y ← top, x ← seq, return (x, y)}
```
hence:
```lean
s ∈ F ↔ ∃ n, [n..∞] × univ ⊆ s
s ∈ G ↔ ∀ i:ℕ, ∃ n, [n..∞] × {i} ⊆ s
```
Now `⋃ i, [i..∞] × {i}` is in `G` but not in `F`.
As product filter we want to have `F` as result.
## Notations
* `f ×ᶠ g` : `filter.prod f g`, localized in `filter`.
-/
open set
open_locale filter
namespace filter
variables {α β γ δ : Type*} {ι : Sort*}
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
localized "infix ` ×ᶠ `:60 := filter.prod" in filter
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f) (ht : t ∈ g) : s ×ˢ t ∈ f ×ᶠ g :=
inter_mem_inf (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ ×ˢ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
{ rintro ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, rfl⟩,
exact ⟨s₁, hs₁, s₂, hs₂, λ p ⟨h, h'⟩, ⟨hts₁ h, hts₂ h'⟩⟩ },
{ rintro ⟨t₁, ht₁, t₂, ht₂, h⟩,
exact mem_inf_of_inter (preimage_mem_comap ht₁) (preimage_mem_comap ht₂) h }
end
@[simp] lemma prod_mem_prod_iff {s : set α} {t : set β} {f : filter α} {g : filter β}
[f.ne_bot] [g.ne_bot] :
s ×ˢ t ∈ f ×ᶠ g ↔ s ∈ f ∧ t ∈ g :=
⟨λ h, let ⟨s', hs', t', ht', H⟩ := mem_prod_iff.1 h in (prod_subset_prod_iff.1 H).elim
(λ ⟨hs's, ht't⟩, ⟨mem_of_superset hs' hs's, mem_of_superset ht' ht't⟩)
(λ h, h.elim
(λ hs'e, absurd hs'e (nonempty_of_mem hs').ne_empty)
(λ ht'e, absurd ht'e (nonempty_of_mem ht').ne_empty)),
λ h, prod_mem_prod h.1 h.2⟩
lemma mem_prod_principal {f : filter α} {s : set (α × β)} {t : set β}:
s ∈ f ×ᶠ 𝓟 t ↔ {a | ∀ b ∈ t, (a, b) ∈ s} ∈ f :=
begin
rw [← @exists_mem_subset_iff _ f, mem_prod_iff],
refine exists₂_congr (λ u u_in, ⟨_, λ h, ⟨t, mem_principal_self t, _⟩⟩),
{ rintros ⟨v, v_in, hv⟩ a a_in b b_in,
exact hv (mk_mem_prod a_in $ v_in b_in) },
{ rintro ⟨x, y⟩ ⟨hx, hy⟩,
exact h hx y hy }
end
lemma mem_prod_top {f : filter α} {s : set (α × β)} :
s ∈ f ×ᶠ (⊤ : filter β) ↔ {a | ∀ b, (a, b) ∈ s} ∈ f :=
begin
rw [← principal_univ, mem_prod_principal],
simp only [mem_univ, forall_true_left]
end
lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) :
comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) :=
by erw [comap_inf, filter.comap_comap, filter.comap_comap]
lemma prod_top {f : filter α} : f ×ᶠ (⊤ : filter β) = f.comap prod.fst :=
by rw [filter.prod, comap_top, inf_top_eq]
lemma sup_prod (f₁ f₂ : filter α) (g : filter β) : (f₁ ⊔ f₂) ×ᶠ g = (f₁ ×ᶠ g) ⊔ (f₂ ×ᶠ g) :=
by rw [filter.prod, comap_sup, inf_sup_right, ← filter.prod, ← filter.prod]
lemma prod_sup (f : filter α) (g₁ g₂ : filter β) : f ×ᶠ (g₁ ⊔ g₂) = (f ×ᶠ g₁) ⊔ (f ×ᶠ g₂) :=
by rw [filter.prod, comap_sup, inf_sup_left, ← filter.prod, ← filter.prod]
lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} :
(∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x)
(pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) :=
by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λ x, (m₁ x, m₂ x)) f (g ×ᶠ h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma tendsto_prod_swap {α1 α2 : Type*} {a1 : filter α1} {a2 : filter α2} :
tendsto (prod.swap : α1 × α2 → α2 × α1) (a1 ×ᶠ a2) (a2 ×ᶠ a1) :=
tendsto_snd.prod_mk tendsto_fst
lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 :=
tendsto_fst.eventually h
lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 :=
tendsto_snd.eventually h
lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x)
{lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) :
∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 :=
(ha.prod_inl lb).and (hb.prod_inr la)
lemma eventually_eq.prod_map {δ} {la : filter α} {fa ga : α → γ} (ha : fa =ᶠ[la] ga)
{lb : filter β} {fb gb : β → δ} (hb : fb =ᶠ[lb] gb) :
prod.map fa fb =ᶠ[la ×ᶠ lb] prod.map ga gb :=
(eventually.prod_mk ha hb).mono $ λ x h, prod.ext h.1 h.2
lemma eventually_le.prod_map {δ} [has_le γ] [has_le δ] {la : filter α} {fa ga : α → γ}
(ha : fa ≤ᶠ[la] ga) {lb : filter β} {fb gb : β → δ} (hb : fb ≤ᶠ[lb] gb) :
prod.map fa fb ≤ᶠ[la ×ᶠ lb] prod.map ga gb :=
eventually.prod_mk ha hb
lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop}
(h : ∀ᶠ x in la ×ᶠ lb, p x) :
∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) :=
begin
rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩,
exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb)
end
/-- A fact that is eventually true about all pairs `l ×ᶠ l` is eventually true about
all diagonal pairs `(i, i)` -/
lemma eventually.diag_of_prod {f : filter α} {p : α × α → Prop}
(h : ∀ᶠ i in f ×ᶠ f, p i) : (∀ᶠ i in f, p (i, i)) :=
begin
obtain ⟨t, ht, s, hs, hst⟩ := eventually_prod_iff.1 h,
apply (ht.and hs).mono (λ x hx, hst hx.1 hx.2),
end
lemma tendsto_diag : tendsto (λ i, (i, i)) f (f ×ᶠ f) :=
tendsto_iff_eventually.mpr (λ _ hpr, hpr.diag_of_prod)
lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}:
(⨅ i, f i) ×ᶠ g = (⨅ i, (f i) ×ᶠ g) :=
by { rw [filter.prod, comap_infi, infi_inf], simp only [filter.prod, eq_self_iff_true] }
lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} :
f ×ᶠ (⨅ i, g i) = (⨅ i, f ×ᶠ (g i)) :=
by { rw [filter.prod, comap_infi, inf_infi], simp only [filter.prod, eq_self_iff_true] }
@[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma {u v w x} prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
(comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λ p : β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf]
lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) :=
by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : f ×ᶠ g = map (λ p : β×α, (p.2, p.1)) (g ×ᶠ f) :=
by { rw [prod_comm', ← map_swap_eq_comap_swap], refl }
lemma prod_assoc (f : filter α) (g : filter β) (h : filter γ) :
map (equiv.prod_assoc α β γ) ((f ×ᶠ g) ×ᶠ h) = f ×ᶠ (g ×ᶠ h) :=
by simp_rw [← comap_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp,
equiv.prod_assoc_symm_apply]
theorem prod_assoc_symm (f : filter α) (g : filter β) (h : filter γ) :
map (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) = (f ×ᶠ g) ×ᶠ h :=
by simp_rw [map_equiv_symm, filter.prod, comap_inf, comap_comap, inf_assoc, function.comp,
equiv.prod_assoc_apply]
lemma tendsto_prod_assoc {f : filter α} {g : filter β} {h : filter γ} :
tendsto (equiv.prod_assoc α β γ) (f ×ᶠ g ×ᶠ h) (f ×ᶠ (g ×ᶠ h)) :=
(prod_assoc f g h).le
lemma tendsto_prod_assoc_symm {f : filter α} {g : filter β} {h : filter γ} :
tendsto (equiv.prod_assoc α β γ).symm (f ×ᶠ (g ×ᶠ h)) (f ×ᶠ g ×ᶠ h) :=
(prod_assoc_symm f g h).le
/-- A useful lemma when dealing with uniformities. -/
lemma map_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} :
map (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2))) ((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) =
(f ×ᶠ h) ×ᶠ (g ×ᶠ k) :=
by simp_rw [map_swap4_eq_comap, filter.prod, comap_inf, comap_comap, inf_assoc, inf_left_comm]
lemma tendsto_swap4_prod {f : filter α} {g : filter β} {h : filter γ} {k : filter δ} :
tendsto (λ p : (α × β) × (γ × δ), ((p.1.1, p.2.1), (p.1.2, p.2.2)))
((f ×ᶠ g) ×ᶠ (h ×ᶠ k)) ((f ×ᶠ h) ×ᶠ (g ×ᶠ k)) :=
map_swap4_prod.le
lemma {u v w x} prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
(map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
le_antisymm
(λ s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc (m₁ '' s₁) ×ˢ (m₂ '' s₂) = (λ p : α₁×α₂, (m₁ p.1, m₂ p.2)) '' s₁ ×ˢ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto.comp le_rfl tendsto_fst).prod_mk (tendsto.comp le_rfl tendsto_snd))
lemma prod_map_map_eq' {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*}
(f : α₁ → α₂) (g : β₁ → β₂) (F : filter α₁) (G : filter β₁) :
(map f F) ×ᶠ (map g G) = map (prod.map f g) (F ×ᶠ G) :=
prod_map_map_eq
lemma le_prod_map_fst_snd {f : filter (α × β)} : f ≤ map prod.fst f ×ᶠ map prod.snd f :=
le_inf le_comap_map le_comap_map
lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β}
{c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) :
tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) :=
begin
erw [tendsto, ← prod_map_map_eq],
exact filter.prod_mono hf hg,
end
protected lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f ×ᶠ g) = (f.map (λ a b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
intro s,
split,
exact λ ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, λ x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact λ ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, λ ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g :=
have h : _ := f.map_prod id g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
(f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm]
@[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
(𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (s ×ˢ t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal];
refl
@[simp] lemma pure_prod {a : α} {f : filter β} : pure a ×ᶠ f = map (prod.mk a) f :=
by rw [prod_eq, map_pure, pure_seq_eq_map]
lemma map_pure_prod (f : α → β → γ) (a : α) (B : filter β) :
filter.map (function.uncurry f) (pure a ×ᶠ B) = filter.map (f a) B :=
by { rw filter.pure_prod, refl }
@[simp] lemma prod_pure {f : filter α} {b : β} : f ×ᶠ pure b = map (λ a, (a, b)) f :=
by rw [prod_eq, seq_pure, map_map]
lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) :=
by simp
lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) :=
begin
split,
{ intro h,
rcases mem_prod_iff.1 (empty_mem_iff_bot.2 h) with ⟨s, hs, t, ht, hst⟩,
rw [subset_empty_iff, set.prod_eq_empty_iff] at hst,
cases hst with s_eq t_eq,
{ left, exact empty_mem_iff_bot.1 (s_eq ▸ hs) },
{ right, exact empty_mem_iff_bot.1 (t_eq ▸ ht) } },
{ rintro (rfl | rfl),
exact bot_prod,
exact prod_bot }
end
lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) :=
by simp only [ne_bot_iff, ne, prod_eq_bot, not_or_distrib]
lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) :
ne_bot (f ×ᶠ g) :=
prod_ne_bot.2 ⟨hf, hg⟩
instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] :
ne_bot (f ×ᶠ g) :=
hf.prod hg
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (x ×ᶠ y) z ↔
∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
lemma tendsto_prod_iff' {f : filter α} {g : filter β} {g' : filter γ}
{s : α → β × γ} :
tendsto s f (g ×ᶠ g') ↔ tendsto (λ n, (s n).1) f g ∧ tendsto (λ n, (s n).2) f g' :=
by { unfold filter.prod, simp only [tendsto_inf, tendsto_comap_iff, iff_self] }
end prod
/-! ### Coproducts of filters -/
section coprod
variables {f : filter α} {g : filter β}
/-- Coproduct of filters. -/
protected def coprod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊔ g.comap prod.snd
lemma mem_coprod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ f.coprod g ↔ ((∃ t₁ ∈ f, prod.fst ⁻¹' t₁ ⊆ s) ∧ (∃ t₂ ∈ g, prod.snd ⁻¹' t₂ ⊆ s)) :=
by simp [filter.coprod]
@[simp] lemma bot_coprod (l : filter β) : (⊥ : filter α).coprod l = comap prod.snd l :=
by simp [filter.coprod]
@[simp] lemma coprod_bot (l : filter α) : l.coprod (⊥ : filter β) = comap prod.fst l :=
by simp [filter.coprod]
lemma bot_coprod_bot : (⊥ : filter α).coprod (⊥ : filter β) = ⊥ := by simp
lemma compl_mem_coprod {s : set (α × β)} {la : filter α} {lb : filter β} :
sᶜ ∈ la.coprod lb ↔ (prod.fst '' s)ᶜ ∈ la ∧ (prod.snd '' s)ᶜ ∈ lb :=
by simp only [filter.coprod, mem_sup, compl_mem_comap]
@[mono] lemma coprod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁.coprod g₁ ≤ f₂.coprod g₂ :=
sup_le_sup (comap_mono hf) (comap_mono hg)
lemma coprod_ne_bot_iff : (f.coprod g).ne_bot ↔ f.ne_bot ∧ nonempty β ∨ nonempty α ∧ g.ne_bot :=
by simp [filter.coprod]
@[instance] lemma coprod_ne_bot_left [ne_bot f] [nonempty β] : (f.coprod g).ne_bot :=
coprod_ne_bot_iff.2 (or.inl ⟨‹_›, ‹_›⟩)
@[instance] lemma coprod_ne_bot_right [ne_bot g] [nonempty α] : (f.coprod g).ne_bot :=
coprod_ne_bot_iff.2 (or.inr ⟨‹_›, ‹_›⟩)
lemma principal_coprod_principal (s : set α) (t : set β) :
(𝓟 s).coprod (𝓟 t) = 𝓟 (sᶜ ×ˢ tᶜ)ᶜ :=
by rw [filter.coprod, comap_principal, comap_principal, sup_principal, set.prod_eq, compl_inter,
preimage_compl, preimage_compl, compl_compl, compl_compl]
-- this inequality can be strict; see `map_const_principal_coprod_map_id_principal` and
-- `map_prod_map_const_id_principal_coprod_principal` below.
lemma {u v w x} map_prod_map_coprod_le {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
map (prod.map m₁ m₂) (f₁.coprod f₂) ≤ (map m₁ f₁).coprod (map m₂ f₂) :=
begin
intros s,
simp only [mem_map, mem_coprod_iff],
rintro ⟨⟨u₁, hu₁, h₁⟩, u₂, hu₂, h₂⟩,
refine ⟨⟨m₁ ⁻¹' u₁, hu₁, λ _ hx, h₁ _⟩, ⟨m₂ ⁻¹' u₂, hu₂, λ _ hx, h₂ _⟩⟩; convert hx
end
/-- Characterization of the coproduct of the `filter.map`s of two principal filters `𝓟 {a}` and
`𝓟 {i}`, the first under the constant function `λ a, b` and the second under the identity function.
Together with the next lemma, `map_prod_map_const_id_principal_coprod_principal`, this provides an
example showing that the inequality in the lemma `map_prod_map_coprod_le` can be strict. -/
lemma map_const_principal_coprod_map_id_principal {α β ι : Type*} (a : α) (b : β) (i : ι) :
(map (λ _ : α, b) (𝓟 {a})).coprod (map id (𝓟 {i}))
= 𝓟 (({b} : set β) ×ˢ univ ∪ univ ×ˢ ({i} : set ι)) :=
by simp only [map_principal, filter.coprod, comap_principal, sup_principal, image_singleton,
image_id, prod_univ, univ_prod]
/-- Characterization of the `filter.map` of the coproduct of two principal filters `𝓟 {a}` and
`𝓟 {i}`, under the `prod.map` of two functions, respectively the constant function `λ a, b` and the
identity function. Together with the previous lemma,
`map_const_principal_coprod_map_id_principal`, this provides an example showing that the inequality
in the lemma `map_prod_map_coprod_le` can be strict. -/
lemma map_prod_map_const_id_principal_coprod_principal {α β ι : Type*} (a : α) (b : β) (i : ι) :
map (prod.map (λ _ : α, b) id) ((𝓟 {a}).coprod (𝓟 {i}))
= 𝓟 (({b} : set β) ×ˢ (univ : set ι)) :=
begin
rw [principal_coprod_principal, map_principal],
congr,
ext ⟨b', i'⟩,
split,
{ rintro ⟨⟨a'', i''⟩, h₁, h₂, h₃⟩,
simp },
{ rintro ⟨h₁, h₂⟩,
use (a, i'),
simpa using h₁.symm }
end
lemma tendsto.prod_map_coprod {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β}
{c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) :
tendsto (prod.map f g) (a.coprod b) (c.coprod d) :=
map_prod_map_coprod_le.trans (coprod_mono hf hg)
end coprod
end filter
|
e103bb34fef7819b972d6363311401edbbc967cd | 8e2026ac8a0660b5a490dfb895599fb445bb77a0 | /tests/lean/run/check_constants.lean | 9c1261d156d58f59a989acb1a874599661837b4d | [
"Apache-2.0"
] | permissive | pcmoritz/lean | 6a8575115a724af933678d829b4f791a0cb55beb | 35eba0107e4cc8a52778259bb5392300267bfc29 | refs/heads/master | 1,607,896,326,092 | 1,490,752,175,000 | 1,490,752,175,000 | 86,612,290 | 0 | 0 | null | 1,490,809,641,000 | 1,490,809,641,000 | null | UTF-8 | Lean | false | false | 14,639 | lean | -- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py
import smt system.io
open tactic
meta def script_check_id (n : name) : tactic unit :=
do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute")
run_cmd script_check_id `abs
run_cmd script_check_id `absurd
run_cmd script_check_id `acc.cases_on
run_cmd script_check_id `add
run_cmd script_check_id `add_comm_group
run_cmd script_check_id `add_comm_semigroup
run_cmd script_check_id `add_group
run_cmd script_check_id `add_monoid
run_cmd script_check_id `and
run_cmd script_check_id `and.elim_left
run_cmd script_check_id `and.elim_right
run_cmd script_check_id `and.intro
run_cmd script_check_id `andthen
run_cmd script_check_id `auto_param
run_cmd script_check_id `bit0
run_cmd script_check_id `bit1
run_cmd script_check_id `bool
run_cmd script_check_id `bool.ff
run_cmd script_check_id `bool.tt
run_cmd script_check_id `bind
run_cmd script_check_id `combinator.K
run_cmd script_check_id `caching_user_attribute
run_cmd script_check_id `cast
run_cmd script_check_id `cast_heq
run_cmd script_check_id `char
run_cmd script_check_id `char.of_nat
run_cmd script_check_id `char.of_nat_ne_of_ne
run_cmd script_check_id `classical.prop_decidable
run_cmd script_check_id `classical.type_decidable_eq
run_cmd script_check_id `coe
run_cmd script_check_id `coe_fn
run_cmd script_check_id `coe_sort
run_cmd script_check_id `coe_to_lift
run_cmd script_check_id `congr
run_cmd script_check_id `congr_arg
run_cmd script_check_id `congr_fun
run_cmd script_check_id `decidable
run_cmd script_check_id `decidable.to_bool
run_cmd script_check_id `distrib
run_cmd script_check_id `dite
run_cmd script_check_id `div
run_cmd script_check_id `id
run_cmd script_check_id `empty
run_cmd script_check_id `emptyc
run_cmd script_check_id `Exists
run_cmd script_check_id `eq
run_cmd script_check_id `eq.cases_on
run_cmd script_check_id `eq.drec
run_cmd script_check_id `eq.mp
run_cmd script_check_id `eq.mpr
run_cmd script_check_id `eq.rec
run_cmd script_check_id `eq.refl
run_cmd script_check_id `eq.subst
run_cmd script_check_id `eq.symm
run_cmd script_check_id `eq.trans
run_cmd script_check_id `eq_of_heq
run_cmd script_check_id `eq_rec_heq
run_cmd script_check_id `eq_true_intro
run_cmd script_check_id `eq_false_intro
run_cmd script_check_id `eq_self_iff_true
run_cmd script_check_id `expr
run_cmd script_check_id `expr.subst
run_cmd script_check_id `format
run_cmd script_check_id `false
run_cmd script_check_id `false_of_true_iff_false
run_cmd script_check_id `false_of_true_eq_false
run_cmd script_check_id `true_eq_false_of_false
run_cmd script_check_id `false.rec
run_cmd script_check_id `field
run_cmd script_check_id `fin.mk
run_cmd script_check_id `fin.ne_of_vne
run_cmd script_check_id `forall_congr
run_cmd script_check_id `forall_congr_eq
run_cmd script_check_id `forall_not_of_not_exists
run_cmd script_check_id `funext
run_cmd script_check_id `ge
run_cmd script_check_id `gt
run_cmd script_check_id `has_add
run_cmd script_check_id `has_bind.and_then
run_cmd script_check_id `has_bind.seq
run_cmd script_check_id `has_div
run_cmd script_check_id `has_mul
run_cmd script_check_id `has_inv
run_cmd script_check_id `has_le
run_cmd script_check_id `has_lt
run_cmd script_check_id `has_neg
run_cmd script_check_id `has_one
run_cmd script_check_id `has_one.one
run_cmd script_check_id `has_sizeof
run_cmd script_check_id `has_sizeof.mk
run_cmd script_check_id `has_sub
run_cmd script_check_id `has_to_format
run_cmd script_check_id `has_to_string
run_cmd script_check_id `has_zero
run_cmd script_check_id `has_zero.zero
run_cmd script_check_id `has_coe_t
run_cmd script_check_id `heq
run_cmd script_check_id `heq.refl
run_cmd script_check_id `heq.symm
run_cmd script_check_id `heq.trans
run_cmd script_check_id `heq_of_eq
run_cmd script_check_id `id_locked
run_cmd script_check_id `if_neg
run_cmd script_check_id `if_pos
run_cmd script_check_id `iff
run_cmd script_check_id `iff_false_intro
run_cmd script_check_id `iff.intro
run_cmd script_check_id `iff.mp
run_cmd script_check_id `iff.mpr
run_cmd script_check_id `iff.refl
run_cmd script_check_id `iff.symm
run_cmd script_check_id `iff.trans
run_cmd script_check_id `iff_true_intro
run_cmd script_check_id `imp_congr
run_cmd script_check_id `imp_congr_eq
run_cmd script_check_id `imp_congr_ctx
run_cmd script_check_id `imp_congr_ctx_eq
run_cmd script_check_id `implies
run_cmd script_check_id `implies_of_if_neg
run_cmd script_check_id `implies_of_if_pos
run_cmd script_check_id `inductive_compiler.tactic.prove_nested_inj
run_cmd script_check_id `inductive_compiler.tactic.prove_pack_inj
run_cmd script_check_id `insert
run_cmd script_check_id `int
run_cmd script_check_id `int.has_add
run_cmd script_check_id `int.has_mul
run_cmd script_check_id `int.has_sub
run_cmd script_check_id `int.has_div
run_cmd script_check_id `int.has_le
run_cmd script_check_id `int.has_lt
run_cmd script_check_id `int.has_neg
run_cmd script_check_id `int.has_mod
run_cmd script_check_id `int.bit0_nonneg
run_cmd script_check_id `int.bit1_nonneg
run_cmd script_check_id `int.one_nonneg
run_cmd script_check_id `int.zero_nonneg
run_cmd script_check_id `int.bit0_pos
run_cmd script_check_id `int.bit1_pos
run_cmd script_check_id `int.one_pos
run_cmd script_check_id `int.nat_abs_zero
run_cmd script_check_id `int.nat_abs_one
run_cmd script_check_id `int.nat_abs_bit0_step
run_cmd script_check_id `int.nat_abs_bit1_nonneg_step
run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case
run_cmd script_check_id `int.ne_neg_of_ne
run_cmd script_check_id `int.neg_ne_of_pos
run_cmd script_check_id `int.ne_neg_of_pos
run_cmd script_check_id `int.neg_ne_zero_of_ne
run_cmd script_check_id `int.zero_ne_neg_of_ne
run_cmd script_check_id `int.decidable_linear_ordered_comm_group
run_cmd script_check_id `interactive.param_desc
run_cmd script_check_id `interactive.parse
run_cmd script_check_id `inv
run_cmd script_check_id `io
run_cmd script_check_id `io.interface
run_cmd script_check_id `is_associative
run_cmd script_check_id `is_associative.assoc
run_cmd script_check_id `is_commutative
run_cmd script_check_id `is_commutative.comm
run_cmd script_check_id `ite
run_cmd script_check_id `left_distrib
run_cmd script_check_id `left_comm
run_cmd script_check_id `le
run_cmd script_check_id `le_refl
run_cmd script_check_id `linear_ordered_ring
run_cmd script_check_id `linear_ordered_semiring
run_cmd script_check_id `list
run_cmd script_check_id `list.nil
run_cmd script_check_id `list.cons
run_cmd script_check_id `lt
run_cmd script_check_id `match_failed
run_cmd script_check_id `mod
run_cmd script_check_id `monad
run_cmd script_check_id `monad_fail
run_cmd script_check_id `monoid
run_cmd script_check_id `mul
run_cmd script_check_id `mul_one
run_cmd script_check_id `mul_zero
run_cmd script_check_id `mul_zero_class
run_cmd script_check_id `name.anonymous
run_cmd script_check_id `name.mk_numeral
run_cmd script_check_id `name.mk_string
run_cmd script_check_id `nat
run_cmd script_check_id `nat.of_num
run_cmd script_check_id `nat.succ
run_cmd script_check_id `nat.zero
run_cmd script_check_id `nat.has_zero
run_cmd script_check_id `nat.has_one
run_cmd script_check_id `nat.has_add
run_cmd script_check_id `nat.add
run_cmd script_check_id `nat.cases_on
run_cmd script_check_id `nat.bit0_ne
run_cmd script_check_id `nat.bit0_ne_bit1
run_cmd script_check_id `nat.bit0_ne_zero
run_cmd script_check_id `nat.bit0_ne_one
run_cmd script_check_id `nat.bit1_ne
run_cmd script_check_id `nat.bit1_ne_bit0
run_cmd script_check_id `nat.bit1_ne_zero
run_cmd script_check_id `nat.bit1_ne_one
run_cmd script_check_id `nat.zero_ne_one
run_cmd script_check_id `nat.zero_ne_bit0
run_cmd script_check_id `nat.zero_ne_bit1
run_cmd script_check_id `nat.one_ne_zero
run_cmd script_check_id `nat.one_ne_bit0
run_cmd script_check_id `nat.one_ne_bit1
run_cmd script_check_id `nat.bit0_lt
run_cmd script_check_id `nat.bit1_lt
run_cmd script_check_id `nat.bit0_lt_bit1
run_cmd script_check_id `nat.bit1_lt_bit0
run_cmd script_check_id `nat.zero_lt_one
run_cmd script_check_id `nat.zero_lt_bit1
run_cmd script_check_id `nat.zero_lt_bit0
run_cmd script_check_id `nat.one_lt_bit0
run_cmd script_check_id `nat.one_lt_bit1
run_cmd script_check_id `nat.le_of_lt
run_cmd script_check_id `nat.le_refl
run_cmd script_check_id `ne
run_cmd script_check_id `neg
run_cmd script_check_id `neq_of_not_iff
run_cmd script_check_id `norm_num.add1
run_cmd script_check_id `norm_num.add1_bit0
run_cmd script_check_id `norm_num.add1_bit1_helper
run_cmd script_check_id `norm_num.add1_one
run_cmd script_check_id `norm_num.add1_zero
run_cmd script_check_id `norm_num.add_div_helper
run_cmd script_check_id `norm_num.bin_add_zero
run_cmd script_check_id `norm_num.bin_zero_add
run_cmd script_check_id `norm_num.bit0_add_bit0_helper
run_cmd script_check_id `norm_num.bit0_add_bit1_helper
run_cmd script_check_id `norm_num.bit0_add_one
run_cmd script_check_id `norm_num.bit1_add_bit0_helper
run_cmd script_check_id `norm_num.bit1_add_bit1_helper
run_cmd script_check_id `norm_num.bit1_add_one_helper
run_cmd script_check_id `norm_num.div_add_helper
run_cmd script_check_id `norm_num.div_eq_div_helper
run_cmd script_check_id `norm_num.div_helper
run_cmd script_check_id `norm_num.div_mul_helper
run_cmd script_check_id `norm_num.mk_cong
run_cmd script_check_id `norm_num.mul_bit0_helper
run_cmd script_check_id `norm_num.mul_bit1_helper
run_cmd script_check_id `norm_num.mul_div_helper
run_cmd script_check_id `norm_num.neg_add_neg_helper
run_cmd script_check_id `norm_num.neg_add_pos_helper1
run_cmd script_check_id `norm_num.neg_add_pos_helper2
run_cmd script_check_id `norm_num.neg_mul_neg_helper
run_cmd script_check_id `norm_num.neg_mul_pos_helper
run_cmd script_check_id `norm_num.neg_neg_helper
run_cmd script_check_id `norm_num.neg_zero_helper
run_cmd script_check_id `norm_num.nonneg_bit0_helper
run_cmd script_check_id `norm_num.nonneg_bit1_helper
run_cmd script_check_id `norm_num.nonzero_of_div_helper
run_cmd script_check_id `norm_num.nonzero_of_neg_helper
run_cmd script_check_id `norm_num.nonzero_of_pos_helper
run_cmd script_check_id `norm_num.one_add_bit0
run_cmd script_check_id `norm_num.one_add_bit1_helper
run_cmd script_check_id `norm_num.one_add_one
run_cmd script_check_id `norm_num.pos_add_neg_helper
run_cmd script_check_id `norm_num.pos_bit0_helper
run_cmd script_check_id `norm_num.pos_bit1_helper
run_cmd script_check_id `norm_num.pos_mul_neg_helper
run_cmd script_check_id `norm_num.sub_nat_zero_helper
run_cmd script_check_id `norm_num.sub_nat_pos_helper
run_cmd script_check_id `norm_num.subst_into_div
run_cmd script_check_id `norm_num.subst_into_prod
run_cmd script_check_id `norm_num.subst_into_subtr
run_cmd script_check_id `norm_num.subst_into_sum
run_cmd script_check_id `not
run_cmd script_check_id `not_of_iff_false
run_cmd script_check_id `not_of_eq_false
run_cmd script_check_id `num
run_cmd script_check_id `num.pos
run_cmd script_check_id `num.zero
run_cmd script_check_id `of_eq_true
run_cmd script_check_id `of_iff_true
run_cmd script_check_id `one
run_cmd script_check_id `opt_param
run_cmd script_check_id `or
run_cmd script_check_id `orelse
run_cmd script_check_id `out_param
run_cmd script_check_id `punit
run_cmd script_check_id `punit.star
run_cmd script_check_id `pos_num.bit0
run_cmd script_check_id `pos_num.bit1
run_cmd script_check_id `pos_num.one
run_cmd script_check_id `prod.mk
run_cmd script_check_id `pprod
run_cmd script_check_id `pprod.mk
run_cmd script_check_id `pprod.fst
run_cmd script_check_id `pprod.snd
run_cmd script_check_id `propext
run_cmd script_check_id `pexpr
run_cmd script_check_id `pexpr.subst
run_cmd script_check_id `to_pexpr
run_cmd script_check_id `quot.mk
run_cmd script_check_id `quot.lift
run_cmd script_check_id `real
run_cmd script_check_id `real.of_int
run_cmd script_check_id `real.to_int
run_cmd script_check_id `real.is_int
run_cmd script_check_id `real.has_neg
run_cmd script_check_id `real.has_div
run_cmd script_check_id `real.has_add
run_cmd script_check_id `real.has_mul
run_cmd script_check_id `real.has_sub
run_cmd script_check_id `real.has_lt
run_cmd script_check_id `real.has_le
run_cmd script_check_id `rfl
run_cmd script_check_id `right_distrib
run_cmd script_check_id `ring
run_cmd script_check_id `scope_trace
run_cmd script_check_id `set_of
run_cmd script_check_id `sep
run_cmd script_check_id `semiring
run_cmd script_check_id `sigma
run_cmd script_check_id `sigma.mk
run_cmd script_check_id `sigma.fst
run_cmd script_check_id `sigma.snd
run_cmd script_check_id `psigma
run_cmd script_check_id `psigma.cases_on
run_cmd script_check_id `psigma.mk
run_cmd script_check_id `singleton
run_cmd script_check_id `sizeof
run_cmd script_check_id `smt.array
run_cmd script_check_id `smt.select
run_cmd script_check_id `smt.store
run_cmd script_check_id `smt.prove
run_cmd script_check_id `string
run_cmd script_check_id `string.empty
run_cmd script_check_id `string.str
run_cmd script_check_id `string.empty_ne_str
run_cmd script_check_id `string.str_ne_empty
run_cmd script_check_id `string.str_ne_str_left
run_cmd script_check_id `string.str_ne_str_right
run_cmd script_check_id `sub
run_cmd script_check_id `subsingleton
run_cmd script_check_id `subsingleton.elim
run_cmd script_check_id `subsingleton.helim
run_cmd script_check_id `subtype
run_cmd script_check_id `subtype.mk
run_cmd script_check_id `subtype.val
run_cmd script_check_id `subtype.rec
run_cmd script_check_id `psum
run_cmd script_check_id `psum.cases_on
run_cmd script_check_id `psum.inl
run_cmd script_check_id `psum.inr
run_cmd script_check_id `tactic
run_cmd script_check_id `tactic.eval_expr
run_cmd script_check_id `tactic.try
run_cmd script_check_id `tactic.triv
run_cmd script_check_id `thunk
run_cmd script_check_id `to_fmt
run_cmd script_check_id `to_string
run_cmd script_check_id `trans_rel_left
run_cmd script_check_id `trans_rel_right
run_cmd script_check_id `true
run_cmd script_check_id `true.intro
run_cmd script_check_id `unification_hint
run_cmd script_check_id `unification_hint.mk
run_cmd script_check_id `unit
run_cmd script_check_id `unit.cases_on
run_cmd script_check_id `unit.star
run_cmd script_check_id `unsafe_monad_from_pure_bind
run_cmd script_check_id `user_attribute
run_cmd script_check_id `vm_monitor
run_cmd script_check_id `weak_order
run_cmd script_check_id `well_founded
run_cmd script_check_id `xor
run_cmd script_check_id `zero
run_cmd script_check_id `zero_le_one
run_cmd script_check_id `zero_lt_one
run_cmd script_check_id `zero_mul
|
3e6921fff34495edcf12485cf6fec81e057b66b3 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/topology/algebra/group.lean | 99e9f60e7a70898086754b6e04f0cc4342393e72 | [
"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 | 26,459 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import order.filter.pointwise
import group_theory.quotient_group
import topology.algebra.monoid
import topology.homeomorph
/-!
# Theory of topological groups
This file defines the following typeclasses:
* `topological_group`, `topological_add_group`: multiplicative and additive topological groups,
i.e., groups with continuous `(*)` and `(⁻¹)` / `(+)` and `(-)`;
* `has_continuous_sub G` means that `G` has a continuous subtraction operation.
There is an instance deducing `has_continuous_sub` from `topological_group` but we use a separate
typeclass because, e.g., `ℕ` and `ℝ≥0` have continuous subtraction but are not additive groups.
We also define `homeomorph` versions of several `equiv`s: `homeomorph.mul_left`,
`homeomorph.mul_right`, `homeomorph.inv`, and prove a few facts about neighbourhood filters in
groups.
## Tags
topological space, group, topological group
-/
open classical set filter topological_space function
open_locale classical topological_space filter
universes u v w x
variables {α : Type u} {β : Type v} {G : Type w} {H : Type x}
section continuous_mul_group
/-!
### Groups with continuous multiplication
In this section we prove a few statements about groups with continuous `(*)`.
-/
variables [topological_space G] [group G] [has_continuous_mul G]
/-- Multiplication from the left in a topological group as a homeomorphism. -/
@[to_additive "Addition from the left in a topological additive group as a homeomorphism."]
protected def homeomorph.mul_left (a : G) : G ≃ₜ G :=
{ continuous_to_fun := continuous_const.mul continuous_id,
continuous_inv_fun := continuous_const.mul continuous_id,
.. equiv.mul_left a }
@[simp, to_additive]
lemma homeomorph.coe_mul_left (a : G) : ⇑(homeomorph.mul_left a) = (*) a := rfl
@[to_additive]
lemma homeomorph.mul_left_symm (a : G) : (homeomorph.mul_left a).symm = homeomorph.mul_left a⁻¹ :=
by { ext, refl }
@[to_additive]
lemma is_open_map_mul_left (a : G) : is_open_map (λ x, a * x) :=
(homeomorph.mul_left a).is_open_map
@[to_additive]
lemma is_closed_map_mul_left (a : G) : is_closed_map (λ x, a * x) :=
(homeomorph.mul_left a).is_closed_map
/-- Multiplication from the right in a topological group as a homeomorphism. -/
@[to_additive "Addition from the right in a topological additive group as a homeomorphism."]
protected def homeomorph.mul_right (a : G) :
G ≃ₜ G :=
{ continuous_to_fun := continuous_id.mul continuous_const,
continuous_inv_fun := continuous_id.mul continuous_const,
.. equiv.mul_right a }
@[to_additive]
lemma is_open_map_mul_right (a : G) : is_open_map (λ x, x * a) :=
(homeomorph.mul_right a).is_open_map
@[to_additive]
lemma is_closed_map_mul_right (a : G) : is_closed_map (λ x, x * a) :=
(homeomorph.mul_right a).is_closed_map
@[to_additive]
lemma is_open_map_div_right (a : G) : is_open_map (λ x, x / a) :=
by simpa only [div_eq_mul_inv] using is_open_map_mul_right (a⁻¹)
@[to_additive]
lemma is_closed_map_div_right (a : G) : is_closed_map (λ x, x / a) :=
by simpa only [div_eq_mul_inv] using is_closed_map_mul_right (a⁻¹)
end continuous_mul_group
section topological_group
/-!
### Topological groups
A topological group is a group in which the multiplication and inversion operations are
continuous. Topological additive groups are defined in the same way. Equivalently, we can require
that the division operation `λ x y, x * y⁻¹` (resp., subtraction) is continuous.
-/
/-- A topological (additive) group is a group in which the addition and negation operations are
continuous. -/
class topological_add_group (G : Type u) [topological_space G] [add_group G]
extends has_continuous_add G : Prop :=
(continuous_neg : continuous (λa:G, -a))
/-- A topological group is a group in which the multiplication and inversion operations are
continuous. -/
@[to_additive]
class topological_group (G : Type*) [topological_space G] [group G]
extends has_continuous_mul G : Prop :=
(continuous_inv : continuous (has_inv.inv : G → G))
variables [topological_space G] [group G] [topological_group G]
export topological_group (continuous_inv)
export topological_add_group (continuous_neg)
@[to_additive]
lemma continuous_on_inv {s : set G} : continuous_on has_inv.inv s :=
continuous_inv.continuous_on
@[to_additive]
lemma continuous_within_at_inv {s : set G} {x : G} : continuous_within_at has_inv.inv s x :=
continuous_inv.continuous_within_at
@[to_additive]
lemma continuous_at_inv {x : G} : continuous_at has_inv.inv x :=
continuous_inv.continuous_at
@[to_additive]
lemma tendsto_inv (a : G) : tendsto has_inv.inv (𝓝 a) (𝓝 (a⁻¹)) :=
continuous_at_inv
/-- If a function converges to a value in a multiplicative topological group, then its inverse
converges to the inverse of this value. For the version in normed fields assuming additionally
that the limit is nonzero, use `tendsto.inv'`. -/
@[to_additive]
lemma filter.tendsto.inv {f : α → G} {l : filter α} {y : G} (h : tendsto f l (𝓝 y)) :
tendsto (λ x, (f x)⁻¹) l (𝓝 y⁻¹) :=
(continuous_inv.tendsto y).comp h
variables [topological_space α] {f : α → G} {s : set α} {x : α}
@[continuity, to_additive]
lemma continuous.inv (hf : continuous f) : continuous (λx, (f x)⁻¹) :=
continuous_inv.comp hf
attribute [continuity] continuous.neg -- TODO
@[to_additive]
lemma continuous_on.inv (hf : continuous_on f s) : continuous_on (λx, (f x)⁻¹) s :=
continuous_inv.comp_continuous_on hf
@[to_additive]
lemma continuous_within_at.inv (hf : continuous_within_at f s x) :
continuous_within_at (λ x, (f x)⁻¹) s x :=
hf.inv
@[instance, to_additive]
instance [topological_space H] [group H] [topological_group H] :
topological_group (G × H) :=
{ continuous_inv := continuous_inv.prod_map continuous_inv }
variable (G)
/-- Inversion in a topological group as a homeomorphism. -/
@[to_additive "Negation in a topological group as a homeomorphism."]
protected def homeomorph.inv : G ≃ₜ G :=
{ continuous_to_fun := continuous_inv,
continuous_inv_fun := continuous_inv,
.. equiv.inv G }
@[to_additive]
lemma nhds_one_symm : comap has_inv.inv (𝓝 (1 : G)) = 𝓝 (1 : G) :=
((homeomorph.inv G).comap_nhds_eq _).trans (congr_arg nhds one_inv)
/-- The map `(x, y) ↦ (x, xy)` as a homeomorphism. This is a shear mapping. -/
@[to_additive "The map `(x, y) ↦ (x, x + y)` as a homeomorphism.
This is a shear mapping."]
protected def homeomorph.shear_mul_right : G × G ≃ₜ G × G :=
{ continuous_to_fun := continuous_fst.prod_mk continuous_mul,
continuous_inv_fun := continuous_fst.prod_mk $ continuous_fst.inv.mul continuous_snd,
.. equiv.prod_shear (equiv.refl _) equiv.mul_left }
@[simp, to_additive]
lemma homeomorph.shear_mul_right_coe :
⇑(homeomorph.shear_mul_right G) = λ z : G × G, (z.1, z.1 * z.2) :=
rfl
@[simp, to_additive]
lemma homeomorph.shear_mul_right_symm_coe :
⇑(homeomorph.shear_mul_right G).symm = λ z : G × G, (z.1, z.1⁻¹ * z.2) :=
rfl
variable {G}
@[to_additive]
lemma inv_closure (s : set G) : (closure s)⁻¹ = closure s⁻¹ :=
(homeomorph.inv G).preimage_closure s
@[to_additive exists_nhds_half_neg]
lemma exists_nhds_split_inv {s : set G} (hs : s ∈ 𝓝 (1 : G)) :
∃ V ∈ 𝓝 (1 : G), ∀ (v ∈ V) (w ∈ V), v / w ∈ s :=
have ((λp : G × G, p.1 * p.2⁻¹) ⁻¹' s) ∈ 𝓝 ((1, 1) : G × G),
from continuous_at_fst.mul continuous_at_snd.inv (by simpa),
by simpa only [div_eq_mul_inv, nhds_prod_eq, mem_prod_self_iff, prod_subset_iff, mem_preimage]
using this
@[to_additive]
lemma nhds_translation_mul_inv (x : G) : comap (λ y : G, y * x⁻¹) (𝓝 1) = 𝓝 x :=
((homeomorph.mul_right x⁻¹).comap_nhds_eq 1).trans $ show 𝓝 (1 * x⁻¹⁻¹) = 𝓝 x, by simp
@[simp, to_additive] lemma map_mul_left_nhds (x y : G) : map ((*) x) (𝓝 y) = 𝓝 (x * y) :=
(homeomorph.mul_left x).map_nhds_eq y
@[to_additive] lemma map_mul_left_nhds_one (x : G) : map ((*) x) (𝓝 1) = 𝓝 x := by simp
@[to_additive]
lemma topological_group.ext {G : Type*} [group G] {t t' : topological_space G}
(tg : @topological_group G t _) (tg' : @topological_group G t' _)
(h : @nhds G t 1 = @nhds G t' 1) : t = t' :=
eq_of_nhds_eq_nhds $ λ x, by
rw [← @nhds_translation_mul_inv G t _ _ x , ← @nhds_translation_mul_inv G t' _ _ x , ← h]
@[to_additive]
lemma topological_group.of_nhds_aux {G : Type*} [group G] [topological_space G]
(hinv : tendsto (λ (x : G), x⁻¹) (𝓝 1) (𝓝 1))
(hleft : ∀ (x₀ : G), 𝓝 x₀ = map (λ (x : G), x₀ * x) (𝓝 1))
(hconj : ∀ (x₀ : G), map (λ (x : G), x₀ * x * x₀⁻¹) (𝓝 1) ≤ 𝓝 1) : continuous (λ x : G, x⁻¹) :=
begin
rw continuous_iff_continuous_at,
rintros x₀,
have key : (λ x, (x₀*x)⁻¹) = (λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹) ∘ (λ x, x⁻¹),
by {ext ; simp[mul_assoc] },
calc map (λ x, x⁻¹) (𝓝 x₀)
= map (λ x, x⁻¹) (map (λ x, x₀*x) $ 𝓝 1) : by rw hleft
... = map (λ x, (x₀*x)⁻¹) (𝓝 1) : by rw filter.map_map
... = map (((λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹)) ∘ (λ x, x⁻¹)) (𝓝 1) : by rw key
... = map ((λ x, x₀⁻¹*x) ∘ (λ x, x₀*x*x₀⁻¹)) _ : by rw ← filter.map_map
... ≤ map ((λ x, x₀⁻¹ * x) ∘ λ x, x₀ * x * x₀⁻¹) (𝓝 1) : map_mono hinv
... = map (λ x, x₀⁻¹ * x) (map (λ x, x₀ * x * x₀⁻¹) (𝓝 1)) : filter.map_map
... ≤ map (λ x, x₀⁻¹ * x) (𝓝 1) : map_mono (hconj x₀)
... = 𝓝 x₀⁻¹ : (hleft _).symm
end
@[to_additive]
lemma topological_group.of_nhds_one' {G : Type*} [group G] [topological_space G]
(hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1))
(hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1))
(hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1))
(hright : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x*x₀) (𝓝 1)) : topological_group G :=
begin
refine { continuous_mul := (has_continuous_mul.of_nhds_one hmul hleft hright).continuous_mul,
continuous_inv := topological_group.of_nhds_aux hinv hleft _ },
intros x₀,
suffices : map (λ (x : G), x₀ * x * x₀⁻¹) (𝓝 1) = 𝓝 1, by simp [this, le_refl],
rw [show (λ x, x₀ * x * x₀⁻¹) = (λ x, x₀ * x) ∘ λ x, x*x₀⁻¹, by {ext, simp [mul_assoc] },
← filter.map_map, ← hright, hleft x₀⁻¹, filter.map_map],
convert map_id,
ext,
simp
end
@[to_additive]
lemma topological_group.of_nhds_one {G : Type*} [group G] [topological_space G]
(hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1))
(hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1))
(hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1))
(hconj : ∀ x₀ : G, tendsto (λ x, x₀*x*x₀⁻¹) (𝓝 1) (𝓝 1)) : topological_group G :=
{ continuous_mul := begin
rw continuous_iff_continuous_at,
rintros ⟨x₀, y₀⟩,
have key : (λ (p : G × G), x₀ * p.1 * (y₀ * p.2)) =
((λ x, x₀*y₀*x) ∘ (uncurry (*)) ∘ (prod.map (λ x, y₀⁻¹*x*y₀) id)),
by { ext, simp [uncurry, prod.map, mul_assoc] },
specialize hconj y₀⁻¹, rw inv_inv at hconj,
calc map (λ (p : G × G), p.1 * p.2) (𝓝 (x₀, y₀))
= map (λ (p : G × G), p.1 * p.2) ((𝓝 x₀) ×ᶠ 𝓝 y₀)
: by rw nhds_prod_eq
... = map (λ (p : G × G), x₀ * p.1 * (y₀ * p.2)) ((𝓝 1) ×ᶠ (𝓝 1))
: by rw [hleft x₀, hleft y₀, prod_map_map_eq, filter.map_map]
... = map (((λ x, x₀*y₀*x) ∘ (uncurry (*))) ∘ (prod.map (λ x, y₀⁻¹*x*y₀) id))((𝓝 1) ×ᶠ (𝓝 1))
: by rw key
... = map ((λ x, x₀*y₀*x) ∘ (uncurry (*))) ((map (λ x, y₀⁻¹*x*y₀) $ 𝓝 1) ×ᶠ (𝓝 1))
: by rw [← filter.map_map, ← prod_map_map_eq', map_id]
... ≤ map ((λ x, x₀*y₀*x) ∘ (uncurry (*))) ((𝓝 1) ×ᶠ (𝓝 1))
: map_mono (filter.prod_mono hconj $ le_refl _)
... = map (λ x, x₀*y₀*x) (map (uncurry (*)) ((𝓝 1) ×ᶠ (𝓝 1))) : by rw filter.map_map
... ≤ map (λ x, x₀*y₀*x) (𝓝 1) : map_mono hmul
... = 𝓝 (x₀*y₀) : (hleft _).symm
end,
continuous_inv := topological_group.of_nhds_aux hinv hleft hconj}
@[to_additive]
lemma topological_group.of_comm_of_nhds_one {G : Type*} [comm_group G] [topological_space G]
(hmul : tendsto (uncurry ((*) : G → G → G)) ((𝓝 1) ×ᶠ 𝓝 1) (𝓝 1))
(hinv : tendsto (λ x : G, x⁻¹) (𝓝 1) (𝓝 1))
(hleft : ∀ x₀ : G, 𝓝 x₀ = map (λ x, x₀*x) (𝓝 1)) : topological_group G :=
topological_group.of_nhds_one hmul hinv hleft (by simpa using tendsto_id)
end topological_group
section quotient_topological_group
variables [topological_space G] [group G] [topological_group G] (N : subgroup G) (n : N.normal)
@[to_additive]
instance {G : Type*} [group G] [topological_space G] (N : subgroup G) :
topological_space (quotient_group.quotient N) :=
quotient.topological_space
open quotient_group
@[to_additive]
lemma quotient_group.is_open_map_coe : is_open_map (coe : G → quotient N) :=
begin
intros s s_op,
change is_open ((coe : G → quotient N) ⁻¹' (coe '' s)),
rw quotient_group.preimage_image_coe N s,
exact is_open_Union (λ n, is_open_map_mul_right n s s_op)
end
@[to_additive]
instance topological_group_quotient [N.normal] : topological_group (quotient N) :=
{ continuous_mul := begin
have cont : continuous ((coe : G → quotient N) ∘ (λ (p : G × G), p.fst * p.snd)) :=
continuous_quot_mk.comp continuous_mul,
have quot : quotient_map (λ p : G × G, ((p.1:quotient N), (p.2:quotient N))),
{ apply is_open_map.to_quotient_map,
{ exact (quotient_group.is_open_map_coe N).prod (quotient_group.is_open_map_coe N) },
{ exact continuous_quot_mk.prod_map continuous_quot_mk },
{ exact (surjective_quot_mk _).prod_map (surjective_quot_mk _) } },
exact (quotient_map.continuous_iff quot).2 cont,
end,
continuous_inv := begin
have : continuous ((coe : G → quotient N) ∘ (λ (a : G), a⁻¹)) :=
continuous_quot_mk.comp continuous_inv,
convert continuous_quotient_lift _ this,
end }
attribute [instance] topological_add_group_quotient
end quotient_topological_group
/-- A typeclass saying that `λ p : G × G, p.1 - p.2` is a continuous function. This property
automatically holds for topological additive groups but it also holds, e.g., for `ℝ≥0`. -/
class has_continuous_sub (G : Type*) [topological_space G] [has_sub G] : Prop :=
(continuous_sub : continuous (λ p : G × G, p.1 - p.2))
@[priority 100] -- see Note [lower instance priority]
instance topological_add_group.to_has_continuous_sub [topological_space G] [add_group G]
[topological_add_group G] :
has_continuous_sub G :=
⟨by { simp only [sub_eq_add_neg], exact continuous_fst.add continuous_snd.neg }⟩
export has_continuous_sub (continuous_sub)
section has_continuous_sub
variables [topological_space G] [has_sub G] [has_continuous_sub G]
lemma filter.tendsto.sub {f g : α → G} {l : filter α} {a b : G} (hf : tendsto f l (𝓝 a))
(hg : tendsto g l (𝓝 b)) :
tendsto (λx, f x - g x) l (𝓝 (a - b)) :=
(continuous_sub.tendsto (a, b)).comp (hf.prod_mk_nhds hg)
variables [topological_space α] {f g : α → G} {s : set α} {x : α}
@[continuity] lemma continuous.sub (hf : continuous f) (hg : continuous g) :
continuous (λ x, f x - g x) :=
continuous_sub.comp (hf.prod_mk hg : _)
lemma continuous_within_at.sub (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λ x, f x - g x) s x :=
hf.sub hg
lemma continuous_on.sub (hf : continuous_on f s) (hg : continuous_on g s) :
continuous_on (λx, f x - g x) s :=
λ x hx, (hf x hx).sub (hg x hx)
end has_continuous_sub
lemma nhds_translation [topological_space G] [add_group G] [topological_add_group G] (x : G) :
comap (λy:G, y - x) (𝓝 0) = 𝓝 x :=
by simpa only [sub_eq_add_neg] using nhds_translation_add_neg x
/-- additive group with a neighbourhood around 0.
Only used to construct a topology and uniform space.
This is currently only available for commutative groups, but it can be extended to
non-commutative groups too.
-/
class add_group_with_zero_nhd (G : Type u) extends add_comm_group G :=
(Z [] : filter G)
(zero_Z : pure 0 ≤ Z)
(sub_Z : tendsto (λp:G×G, p.1 - p.2) (Z ×ᶠ Z) Z)
namespace add_group_with_zero_nhd
variables (G) [add_group_with_zero_nhd G]
local notation `Z` := add_group_with_zero_nhd.Z
@[priority 100] -- see Note [lower instance priority]
instance : topological_space G :=
topological_space.mk_of_nhds $ λa, map (λx, x + a) (Z G)
variables {G}
lemma neg_Z : tendsto (λa:G, - a) (Z G) (Z G) :=
have tendsto (λa, (0:G)) (Z G) (Z G),
by refine le_trans (assume h, _) zero_Z; simp [univ_mem_sets'] {contextual := tt},
have tendsto (λa:G, 0 - a) (Z G) (Z G), from
sub_Z.comp (tendsto.prod_mk this tendsto_id),
by simpa
lemma add_Z : tendsto (λp:G×G, p.1 + p.2) (Z G ×ᶠ Z G) (Z G) :=
suffices tendsto (λp:G×G, p.1 - -p.2) (Z G ×ᶠ Z G) (Z G),
by simpa [sub_eq_add_neg],
sub_Z.comp (tendsto.prod_mk tendsto_fst (neg_Z.comp tendsto_snd))
lemma exists_Z_half {s : set G} (hs : s ∈ Z G) : ∃ V ∈ Z G, ∀ (v ∈ V) (w ∈ V), v + w ∈ s :=
begin
have : ((λa:G×G, a.1 + a.2) ⁻¹' s) ∈ Z G ×ᶠ Z G := add_Z (by simpa using hs),
rcases mem_prod_self_iff.1 this with ⟨V, H, H'⟩,
exact ⟨V, H, prod_subset_iff.1 H'⟩
end
lemma nhds_eq (a : G) : 𝓝 a = map (λx, x + a) (Z G) :=
topological_space.nhds_mk_of_nhds _ _
(assume a, calc pure a = map (λx, x + a) (pure 0) : by simp
... ≤ _ : map_mono zero_Z)
(assume b s hs,
let ⟨t, ht, eqt⟩ := exists_Z_half hs in
have t0 : (0:G) ∈ t, by simpa using zero_Z ht,
begin
refine ⟨(λx:G, x + b) '' t, image_mem_map ht, _, _⟩,
{ refine set.image_subset_iff.2 (assume b hbt, _),
simpa using eqt 0 t0 b hbt },
{ rintros _ ⟨c, hb, rfl⟩,
refine (Z G).sets_of_superset ht (assume x hxt, _),
simpa [add_assoc] using eqt _ hxt _ hb }
end)
lemma nhds_zero_eq_Z : 𝓝 0 = Z G := by simp [nhds_eq]; exact filter.map_id
@[priority 100] -- see Note [lower instance priority]
instance : has_continuous_add G :=
⟨ continuous_iff_continuous_at.2 $ assume ⟨a, b⟩,
begin
rw [continuous_at, nhds_prod_eq, nhds_eq, nhds_eq, nhds_eq, filter.prod_map_map_eq,
tendsto_map'_iff],
suffices : tendsto ((λx:G, (a + b) + x) ∘ (λp:G×G,p.1 + p.2)) (Z G ×ᶠ Z G)
(map (λx:G, (a + b) + x) (Z G)),
{ simpa [(∘), add_comm, add_left_comm] },
exact tendsto_map.comp add_Z
end ⟩
@[priority 100] -- see Note [lower instance priority]
instance : topological_add_group G :=
⟨continuous_iff_continuous_at.2 $ assume a,
begin
rw [continuous_at, nhds_eq, nhds_eq, tendsto_map'_iff],
suffices : tendsto ((λx:G, x - a) ∘ (λx:G, -x)) (Z G) (map (λx:G, x - a) (Z G)),
{ simpa [(∘), add_comm, sub_eq_add_neg] using this },
exact tendsto_map.comp neg_Z
end⟩
end add_group_with_zero_nhd
section filter_mul
section
variables [topological_space G] [group G] [topological_group G]
@[to_additive]
lemma is_open.mul_left {s t : set G} : is_open t → is_open (s * t) := λ ht,
begin
have : ∀a, is_open ((λ (x : G), a * x) '' t) :=
assume a, is_open_map_mul_left a t ht,
rw ← Union_mul_left_image,
exact is_open_Union (λa, is_open_Union $ λha, this _),
end
@[to_additive]
lemma is_open.mul_right {s t : set G} : is_open s → is_open (s * t) := λ hs,
begin
have : ∀a, is_open ((λ (x : G), x * a) '' s),
assume a, apply is_open_map_mul_right, exact hs,
rw ← Union_mul_right_image,
exact is_open_Union (λa, is_open_Union $ λha, this _),
end
variables (G)
lemma topological_group.t1_space (h : @is_closed G _ {1}) : t1_space G :=
⟨assume x, by { convert is_closed_map_mul_right x _ h, simp }⟩
lemma topological_group.regular_space [t1_space G] : regular_space G :=
⟨assume s a hs ha,
let f := λ p : G × G, p.1 * (p.2)⁻¹ in
have hf : continuous f := continuous_fst.mul continuous_snd.inv,
-- a ∈ -s implies f (a, 1) ∈ -s, and so (a, 1) ∈ f⁻¹' (-s);
-- and so can find t₁ t₂ open such that a ∈ t₁ × t₂ ⊆ f⁻¹' (-s)
let ⟨t₁, t₂, ht₁, ht₂, a_mem_t₁, one_mem_t₂, t_subset⟩ :=
is_open_prod_iff.1 ((is_open_compl_iff.2 hs).preimage hf) a (1:G) (by simpa [f]) in
begin
use [s * t₂, ht₂.mul_left, λ x hx, ⟨x, 1, hx, one_mem_t₂, mul_one _⟩],
apply inf_principal_eq_bot,
rw mem_nhds_sets_iff,
refine ⟨t₁, _, ht₁, a_mem_t₁⟩,
rintros x hx ⟨y, z, hy, hz, yz⟩,
have : x * z⁻¹ ∈ sᶜ := (prod_subset_iff.1 t_subset) x hx z hz,
have : x * z⁻¹ ∈ s, rw ← yz, simpa,
contradiction
end⟩
local attribute [instance] topological_group.regular_space
lemma topological_group.t2_space [t1_space G] : t2_space G := regular_space.t2_space G
end
section
/-! Some results about an open set containing the product of two sets in a topological group. -/
variables [topological_space G] [group G] [topological_group G]
/-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1`
such that `KV ⊆ U`. -/
@[to_additive "Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of
`0` such that `K + V ⊆ U`."]
lemma compact_open_separated_mul {K U : set G} (hK : is_compact K) (hU : is_open U) (hKU : K ⊆ U) :
∃ V : set G, is_open V ∧ (1 : G) ∈ V ∧ K * V ⊆ U :=
begin
let W : G → set G := λ x, (λ y, x * y) ⁻¹' U,
have h1W : ∀ x, is_open (W x) := λ x, hU.preimage (continuous_mul_left x),
have h2W : ∀ x ∈ K, (1 : G) ∈ W x := λ x hx, by simp only [mem_preimage, mul_one, hKU hx],
choose V hV using λ x : K, exists_open_nhds_one_mul_subset (mem_nhds_sets (h1W x) (h2W x.1 x.2)),
let X : K → set G := λ x, (λ y, (x : G)⁻¹ * y) ⁻¹' (V x),
obtain ⟨t, ht⟩ : ∃ t : finset ↥K, K ⊆ ⋃ i ∈ t, X i,
{ refine hK.elim_finite_subcover X (λ x, (hV x).1.preimage (continuous_mul_left x⁻¹)) _,
intros x hx, rw [mem_Union], use ⟨x, hx⟩, rw [mem_preimage], convert (hV _).2.1,
simp only [mul_left_inv, subtype.coe_mk] },
refine ⟨⋂ x ∈ t, V x, is_open_bInter (finite_mem_finset _) (λ x hx, (hV x).1), _, _⟩,
{ simp only [mem_Inter], intros x hx, exact (hV x).2.1 },
rintro _ ⟨x, y, hx, hy, rfl⟩, simp only [mem_Inter] at hy,
have := ht hx, simp only [mem_Union, mem_preimage] at this, rcases this with ⟨z, h1z, h2z⟩,
have : (z : G)⁻¹ * x * y ∈ W z := (hV z).2.2 (mul_mem_mul h2z (hy z h1z)),
rw [mem_preimage] at this, convert this using 1, simp only [mul_assoc, mul_inv_cancel_left]
end
/-- A compact set is covered by finitely many left multiplicative translates of a set
with non-empty interior. -/
@[to_additive "A compact set is covered by finitely many left additive translates of a set
with non-empty interior."]
lemma compact_covered_by_mul_left_translates {K V : set G} (hK : is_compact K)
(hV : (interior V).nonempty) : ∃ t : finset G, K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V :=
begin
obtain ⟨t, ht⟩ : ∃ t : finset G, K ⊆ ⋃ x ∈ t, interior (((*) x) ⁻¹' V),
{ refine hK.elim_finite_subcover (λ x, interior $ ((*) x) ⁻¹' V) (λ x, is_open_interior) _,
cases hV with g₀ hg₀,
refine λ g hg, mem_Union.2 ⟨g₀ * g⁻¹, _⟩,
refine preimage_interior_subset_interior_preimage (continuous_const.mul continuous_id) _,
rwa [mem_preimage, inv_mul_cancel_right] },
exact ⟨t, subset.trans ht $ bUnion_subset_bUnion_right $ λ g hg, interior_subset⟩
end
/-- Every locally compact separable topological group is σ-compact.
Note: this is not true if we drop the topological group hypothesis. -/
@[priority 100] instance separable_locally_compact_group.sigma_compact_space
[separable_space G] [locally_compact_space G] : sigma_compact_space G :=
begin
obtain ⟨L, hLc, hL1⟩ := exists_compact_mem_nhds (1 : G),
refine ⟨⟨λ n, (λ x, x * dense_seq G n) ⁻¹' L, _, _⟩⟩,
{ intro n, exact (homeomorph.mul_right _).compact_preimage.mpr hLc },
{ refine Union_eq_univ_iff.2 (λ x, _),
obtain ⟨_, ⟨n, rfl⟩, hn⟩ : (range (dense_seq G) ∩ (λ y, x * y) ⁻¹' L).nonempty,
{ rw [← (homeomorph.mul_left x).apply_symm_apply 1] at hL1,
exact (dense_range_dense_seq G).inter_nhds_nonempty
((homeomorph.mul_left x).continuous.continuous_at $ hL1) },
exact ⟨n, hn⟩ }
end
end
section
variables [topological_space G] [comm_group G] [topological_group G]
@[to_additive]
lemma nhds_mul (x y : G) : 𝓝 (x * y) = 𝓝 x * 𝓝 y :=
filter_eq $ set.ext $ assume s,
begin
rw [← nhds_translation_mul_inv x, ← nhds_translation_mul_inv y, ← nhds_translation_mul_inv (x*y)],
split,
{ rintros ⟨t, ht, ts⟩,
rcases exists_nhds_one_split ht with ⟨V, V1, h⟩,
refine ⟨(λa, a * x⁻¹) ⁻¹' V, (λa, a * y⁻¹) ⁻¹' V,
⟨V, V1, subset.refl _⟩, ⟨V, V1, subset.refl _⟩, _⟩,
rintros a ⟨v, w, v_mem, w_mem, rfl⟩,
apply ts,
simpa [mul_comm, mul_assoc, mul_left_comm] using h (v * x⁻¹) v_mem (w * y⁻¹) w_mem },
{ rintros ⟨a, c, ⟨b, hb, ba⟩, ⟨d, hd, dc⟩, ac⟩,
refine ⟨b ∩ d, inter_mem_sets hb hd, assume v, _⟩,
simp only [preimage_subset_iff, mul_inv_rev, mem_preimage] at *,
rintros ⟨vb, vd⟩,
refine ac ⟨v * y⁻¹, y, _, _, _⟩,
{ rw ← mul_assoc _ _ _ at vb, exact ba _ vb },
{ apply dc y, rw mul_right_inv, exact mem_of_nhds hd },
{ simp only [inv_mul_cancel_right] } }
end
@[to_additive]
lemma nhds_is_mul_hom : is_mul_hom (λx:G, 𝓝 x) := ⟨λ_ _, nhds_mul _ _⟩
end
end filter_mul
instance additive.topological_add_group {G} [h : topological_space G]
[group G] [topological_group G] : @topological_add_group (additive G) h _ :=
{ continuous_neg := @continuous_inv G _ _ _ }
instance multiplicative.topological_group {G} [h : topological_space G]
[add_group G] [topological_add_group G] : @topological_group (multiplicative G) h _ :=
{ continuous_inv := @continuous_neg G _ _ _ }
|
6577769af0740e7c5d96b9265d3d476b5ada75ef | 85a51a7a118db552510ddb311e53e7a8bba7b477 | /src/transfer_tactic.lean | 3863b40d715a0f1fda8e2808a64ba2d4a53a2fff | [] | no_license | ADedecker/nonstandard | f0c1cac7482bb0dd48d2f2eb092f5262ff0fa2dc | c32f5e1d87cc9e6410d66cf3080fd8c4a47cf5e4 | refs/heads/master | 1,686,549,196,023 | 1,626,129,788,000 | 1,626,129,788,000 | 382,594,533 | 3 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,640 | lean | import complements.filter_product
import tactic
import tactic.interactive
import data.real.hyperreal
open tactic expr
namespace tactic
section lift_lhs
private meta def forall_rule (l : expr) (α : expr) : tactic unit :=
do `(filter %%ι) ← infer_type l,
e ← to_expr ``(filter.germ.forall_iff_forall_lift_pred %%l),
rewrite_target e
private meta def exists_rule (l : expr) (α : expr) : tactic unit :=
do `(filter %%ι) ← infer_type l,
e ← to_expr ``(filter.germ.exists_iff_exists_lift_pred %%l),
rewrite_target e
meta def transfer_lift_lhs (tgt : expr) : tactic unit :=
do `(%%lhs ↔ %%rhs) ← (return tgt) | fail "Goal is not an equivalence (step 1)",
match rhs with
| `(∀ _ : (filter.germ %%l %%α), _) := forall_rule l α
| `(∀ _ : ℝ*, _) := forall_rule `(filter.hyperfilter ℕ : filter ℕ) `(ℝ)
| `(∃ _ : (filter.germ %%l %%α), _) := exists_rule l α
| `(∃ _ : ℝ*, _) := exists_rule `(filter.hyperfilter ℕ : filter ℕ) `(ℝ)
| _ := fail "No known pattern applicable (step 1)"
end
end lift_lhs
section congr
meta def transfer_congr (tgt : expr) : tactic unit :=
do `(%%lhs ↔ %%rhs) ← (return tgt) | fail "Goal is not an equivalence (step 2)",
match lhs with
| `(∀ _ : %%t, _) :=
(do t' ← infer_type t,
unify t' `(Prop),
refine ``(imp_congr _ _)) <|>
(do some (name, _, _) ← get_binder none tt lhs,
name ← get_unused_name name,
refine ``(forall_congr _),
intro name,
skip)
| `(∃ _ : %%t, _) :=
do --some (name, _, _) ← get_binder none tt lhs, TODO
name ← get_unused_name,
refine ``(exists_congr _),
intro name,
skip
| `(_ ∧ _) :=
do name ← get_unused_name,
refine ``(and_congr _ _)
| `(_ ∨ _) :=
do name ← get_unused_name,
refine ``(or_congr _ _)
| `(¬ _) :=
do name ← get_unused_name,
refine ``(not_congr _)
| `(_ = _) :=
do name ← get_unused_name,
refine ``(iff_of_eq (_root_.congr (=) _))
| _ := fail "No known pattern applicable (step 2)"
end
end congr
section push_lift
meta def transfer_push_lift (tgt : expr) : tactic unit :=
do `(filter.germ.lift_pred %%p %%x ↔ %%rhs) ← (return tgt) | fail "Goal is not an equivalence (step 3)",
match p with
| `(λ _, ∀ y, %%q) :=
(do e ← to_expr ``(filter.germ.lift_pred_forall_iff_forall_lift_pred'),
rewrite_target e) <|>
(do e ← to_expr ``(filter.germ.lift_pred_imp_iff_imp_lift_pred),
rewrite_target e)
| `(λ _, ¬ %%q) :=
do e ← to_expr ``(filter.germ.lift_pred_not_iff_not_lift_pred),
rewrite_target e
| `(λ _, ∃ y, %%q) :=
(do e ← to_expr ``(filter.germ.lift_pred_exists_iff_exists_lift_pred'),
rewrite_target e) <|>
(do e ← to_expr ``(filter.germ.lift_pred_exists_prop_iff_and_lift_pred),
e' ← to_expr ``(exists_prop),
rewrite_target e,
rewrite_target e')
| `(λ _, _ < _) :=
do e ← to_expr ``(filter.germ.lift_pred_lt_iff_lt_map),
rewrite_target e
| `(λ _, _ > _) :=
do e ← to_expr ``(filter.germ.lift_pred_lt_iff_lt_map),
rewrite_target e
| `(λ _, _ = _) :=
do e ← to_expr ``(filter.germ.lift_pred_eq_iff_eq_map),
rewrite_target e
| `(λ _, _ ≠ _) :=
do e ← to_expr ``(filter.germ.lift_pred_ne_iff_ne_map),
rewrite_target e
| `(λ _, _ ∧ _) :=
do e ← to_expr ``(filter.germ.lift_pred_and_iff_and_lift_pred),
rewrite_target e
| `(λ _, _ ∨ _) :=
do e ← to_expr ``(filter.germ.lift_pred_or_iff_or_lift_pred),
rewrite_target e
| _ := fail "No known pattern applicable (step 3)"
end
end push_lift
section induction
meta def transfer_induction (tgt : expr) : tactic unit :=
local_context >>= list.mmap' (λ x, try $
do t ← infer_type x,
match t with
| `(filter.germ _ _) := skip
| `(ℝ*) := skip
| _ := fail ()
end,
refine ``((%%x).induction_on _),
name ← get_unused_name,
intro name )
meta def transfer_close (tgt : expr) : tactic unit :=
transfer_induction tgt >> reflexivity
end induction
namespace interactive
setup_tactic_parser
meta def transfer_lift_lhs : tactic unit :=
target >>= tactic.transfer_lift_lhs
meta def transfer_congr : tactic unit :=
target >>= tactic.transfer_congr
meta def transfer_push_lift : tactic unit :=
target >>= tactic.transfer_push_lift
meta def transfer_induction : tactic unit :=
target >>= tactic.transfer_induction
meta def transfer_close : tactic unit :=
target >>= tactic.transfer_close
meta def transfer_step : tactic unit :=
transfer_close <|>
transfer_congr <|>
(transfer_push_lift >> try transfer_congr) <|>
(transfer_lift_lhs >> try transfer_congr)
meta def transfer : tactic unit :=
focus (repeat transfer_step)
end interactive
example (α ι : Type*) [preorder α] (l : ultrafilter ι) (a : α) :
(∀ x, a ≤ x) ↔ (∀ x : (l : filter ι).germ α, ↑a ≤ x) :=
by transfer
example (α ι : Type*) [preorder α] (l : ultrafilter ι) (a : α) :
(∀ x y : α, x = y) ↔ (∀ x y : (l : filter ι).germ α, x = y) :=
by transfer
open filter
example (l : ℝ) (u : ℕ → ℝ) :
(∀ ε > 0, ∃ N ≥ (1 : ℕ), ∀ n ≥ N, abs (u n - l) < ε) ↔
(∀ ε > 0, ∃ N ≥ (1 : (hyperfilter ℕ : filter ℕ).germ ℕ), ∀ n ≥ N, germ.map abs (germ.map u n - ↑l) < ε) :=
by transfer
end tactic |
17eaa79e5d1b1b394a25b81069342aaa8f774398 | 69bc7d0780be17e452d542a93f9599488f1c0c8e | /12-5-2019.lean | 8208b789ae2c8558879f31b5dc470e969277ac61 | [] | no_license | joek13/cs2102-notes | b7352285b1d1184fae25594f89f5926d74e6d7b4 | 25bb18788641b20af9cf3c429afe1da9b2f5eafb | refs/heads/master | 1,673,461,162,867 | 1,575,561,090,000 | 1,575,561,090,000 | 207,573,549 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,362 | lean | -- Notes 12/5/2019
/- Talking about logics
We don't have just one logic; we have many
- predicate logic
- the language of predicate logic: variables, functions,
- propositional logic
- some other logics (temporal logic)
- functions
- inductive types
types we have:
- true
- false
- a = b
- introduction rule: eq.refl
- P ∧ Q
- introduction rule: and.intro
- P ∨ Q
- introduction rule: or.intro_left / or.intro_right
- P → Q
- P ↔ Q
- introduction rule: iff.intro
- ¬P
- ∀ (p : P), Q
- ∃ (p: P), Q
-/
/-
Sample propositions:
If there's some P everyone likes, then everyone likes some P
-/
inductive Person : Type
inductive Likes : Person → Person → Prop
example : (∃ (p : Person), ∀ (q : Person), Likes q p) → ∀ (q : Person), (∃ (p : Person), Likes q p) :=
begin
assume someone_everyone_likes,
assume q,
apply exists.elim someone_everyone_likes,
assume a,
assume hypothesis,
have q_likes_a := hypothesis q,
apply exists.intro,
exact q_likes_a,
end
axiom f : false
example : (∃ (p : Person), ∀ (q : Person), Likes q p) → ∀ (q : Person), (∃ (p : Person), Likes q p) :=
begin
assume h,
assume p,
-- "identify a witness"
cases h with w pf,
apply exists.intro w _,
exact pf p
end
inductive jlist (α : Type) : Type
| nil : jlist
| cons (a : α) (t : jlist) : jlist
def len {α : Type} : jlist α → nat
| (jlist.nil α):= 0
| (jlist.cons a t) := nat.succ (len t)
example : (∃ (p : Person), ∀ (q : Person), Likes q p) → ∀ (q : Person), (∃ (p : Person), Likes q p) :=
begin
assume h,
assume p,
cases h with w pf,
apply exists.intro w (pf p)
end
-- "Proof of an exists is just a pair"
-- that pair is "a witness" and "a proof that witness satisfies p"
def even : nat → Prop := λ (q : nat), ∃ (n : nat), n * 2 = q
theorem two_even : even 2 :=
begin
exact exists.intro 1 (eq.refl _),
end
example : ∀ (b : bool), bor b tt = tt :=
begin
assume b,
cases b,
exact eq.refl tt,
exact eq.refl tt,
end
example : ∀ (n : ℕ), n = 0 ∨ n ≠ 0 :=
begin
assume n,
cases n with p z,
apply or.inl,
refl,
apply or.inr,
apply not.intro,
assume ridiculous,
cases ridiculous,
end
example : ∀ (n : ℕ), n = 0 ∨ n ≠ 0 := λ (n : nat), classical.em _ |
47a2bf9f5ec496d9179c720b2cfcb6925501279e | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/data/rat/cast.lean | 80288acadeaef08208209ac3ea5694641cdc7035 | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,826 | 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, Mario Carneiro
-/
import data.rat.order
import data.int.char_zero
/-!
# Casts for Rational Numbers
## Summary
We define the canonical injection from ℚ into an arbitrary division ring and prove various
casting lemmas showing the well-behavedness of this injection.
## Notations
- `/.` is infix notation for `rat.mk`.
## Tags
rat, rationals, field, ℚ, numerator, denominator, num, denom, cast, coercion, casting
-/
namespace rat
variable {α : Type*}
open_locale rat
section with_div_ring
variable [division_ring α]
/-- Construct the canonical injection from `ℚ` into an arbitrary
division ring. If the field has positive characteristic `p`,
we define `1 / p = 1 / 0 = 0` for consistency with our
division by zero convention. -/
-- see Note [coercion into rings]
@[priority 900] instance cast_coe : has_coe_t ℚ α := ⟨λ r, r.1 / r.2⟩
@[simp] theorem cast_of_int (n : ℤ) : (of_int n : α) = n :=
show (n / (1:ℕ) : α) = n, by rw [nat.cast_one, div_one]
@[simp, norm_cast] theorem cast_coe_int (n : ℤ) : ((n : ℚ) : α) = n :=
by rw [coe_int_eq_of_int, cast_of_int]
@[simp, norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℚ) : α) = n := cast_coe_int n
@[simp, norm_cast] theorem cast_zero : ((0 : ℚ) : α) = 0 :=
(cast_of_int _).trans int.cast_zero
@[simp, norm_cast] theorem cast_one : ((1 : ℚ) : α) = 1 :=
(cast_of_int _).trans int.cast_one
theorem cast_commute (r : ℚ) (a : α) : commute ↑r a :=
(r.1.cast_commute a).div_left (r.2.cast_commute a)
theorem cast_comm (r : ℚ) (a : α) : (r : α) * a = a * r :=
(cast_commute r a).eq
theorem commute_cast (a : α) (r : ℚ) : commute a r :=
(r.cast_commute a).symm
@[norm_cast] theorem cast_mk_of_ne_zero (a b : ℤ)
(b0 : (b:α) ≠ 0) : (a /. b : α) = a / b :=
begin
have b0' : b ≠ 0, { refine mt _ b0, simp {contextual := tt} },
cases e : a /. b with n d h c,
have d0 : (d:α) ≠ 0,
{ intro d0,
have dd := denom_dvd a b,
cases (show (d:ℤ) ∣ b, by rwa e at dd) with k ke,
have : (b:α) = (d:α) * (k:α), {rw [ke, int.cast_mul], refl},
rw [d0, zero_mul] at this, contradiction },
rw [num_denom'] at e,
have := congr_arg (coe : ℤ → α) ((mk_eq b0' $ ne_of_gt $ int.coe_nat_pos.2 h).1 e),
rw [int.cast_mul, int.cast_mul, int.cast_coe_nat] at this,
symmetry, change (a / b : α) = n / d,
rw [div_eq_mul_inv, eq_div_iff_mul_eq d0, mul_assoc, (d.commute_cast _).eq,
← mul_assoc, this, mul_assoc, mul_inv_cancel b0, mul_one]
end
@[norm_cast] theorem cast_add_of_ne_zero : ∀ {m n : ℚ},
(m.denom : α) ≠ 0 → (n.denom : α) ≠ 0 → ((m + n : ℚ) : α) = m + n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := λ (d₁0 : (d₁:α) ≠ 0) (d₂0 : (d₂:α) ≠ 0), begin
have d₁0' : (d₁:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₁0; exact d₁0 rfl),
have d₂0' : (d₂:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₂0; exact d₂0 rfl),
rw [num_denom', num_denom', add_def d₁0' d₂0'],
suffices : (n₁ * (d₂ * (d₂⁻¹ * d₁⁻¹)) +
n₂ * (d₁ * d₂⁻¹) * d₁⁻¹ : α) = n₁ * d₁⁻¹ + n₂ * d₂⁻¹,
{ rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero],
{ simpa [division_def, left_distrib, right_distrib, mul_inv_rev₀, d₁0, d₂0, mul_assoc] },
all_goals {simp [d₁0, d₂0]} },
rw [← mul_assoc (d₂:α), mul_inv_cancel d₂0, one_mul,
(nat.cast_commute _ _).eq], simp [d₁0, mul_assoc]
end
@[simp, norm_cast] theorem cast_neg : ∀ n, ((-n : ℚ) : α) = -n
| ⟨n, d, h, c⟩ := show (↑-n / d : α) = -(n / d),
by rw [div_eq_mul_inv, div_eq_mul_inv, int.cast_neg, neg_mul_eq_neg_mul]
@[norm_cast] theorem cast_sub_of_ne_zero {m n : ℚ}
(m0 : (m.denom : α) ≠ 0) (n0 : (n.denom : α) ≠ 0) : ((m - n : ℚ) : α) = m - n :=
have ((-n).denom : α) ≠ 0, by cases n; exact n0,
by simp [sub_eq_add_neg, (cast_add_of_ne_zero m0 this)]
@[norm_cast] theorem cast_mul_of_ne_zero : ∀ {m n : ℚ},
(m.denom : α) ≠ 0 → (n.denom : α) ≠ 0 → ((m * n : ℚ) : α) = m * n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := λ (d₁0 : (d₁:α) ≠ 0) (d₂0 : (d₂:α) ≠ 0), begin
have d₁0' : (d₁:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₁0; exact d₁0 rfl),
have d₂0' : (d₂:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d₂0; exact d₂0 rfl),
rw [num_denom', num_denom', mul_def d₁0' d₂0'],
suffices : (n₁ * ((n₂ * d₂⁻¹) * d₁⁻¹) : α) = n₁ * (d₁⁻¹ * (n₂ * d₂⁻¹)),
{ rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, cast_mk_of_ne_zero],
{ simpa [division_def, mul_inv_rev₀, d₁0, d₂0, mul_assoc] },
all_goals {simp [d₁0, d₂0]} },
rw [(d₁.commute_cast (_:α)).inv_right₀.eq]
end
@[norm_cast] theorem cast_inv_of_ne_zero : ∀ {n : ℚ},
(n.num : α) ≠ 0 → (n.denom : α) ≠ 0 → ((n⁻¹ : ℚ) : α) = n⁻¹
| ⟨n, d, h, c⟩ := λ (n0 : (n:α) ≠ 0) (d0 : (d:α) ≠ 0), begin
have n0' : (n:ℤ) ≠ 0 := λ e, by rw e at n0; exact n0 rfl,
have d0' : (d:ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (λ e, by rw e at d0; exact d0 rfl),
rw [num_denom', inv_def],
rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero, inv_div];
simp [n0, d0]
end
@[norm_cast] theorem cast_div_of_ne_zero {m n : ℚ} (md : (m.denom : α) ≠ 0)
(nn : (n.num : α) ≠ 0) (nd : (n.denom : α) ≠ 0) : ((m / n : ℚ) : α) = m / n :=
have (n⁻¹.denom : ℤ) ∣ n.num,
by conv in n⁻¹.denom { rw [←(@num_denom n), inv_def] };
apply denom_dvd,
have (n⁻¹.denom : α) = 0 → (n.num : α) = 0, from
λ h, let ⟨k, e⟩ := this in
by have := congr_arg (coe : ℤ → α) e;
rwa [int.cast_mul, int.cast_coe_nat, h, zero_mul] at this,
by rw [division_def, cast_mul_of_ne_zero md (mt this nn), cast_inv_of_ne_zero nn nd, division_def]
@[simp, norm_cast] theorem cast_inj [char_zero α] : ∀ {m n : ℚ}, (m : α) = n ↔ m = n
| ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := begin
refine ⟨λ h, _, congr_arg _⟩,
have d₁0 : d₁ ≠ 0 := ne_of_gt h₁,
have d₂0 : d₂ ≠ 0 := ne_of_gt h₂,
have d₁a : (d₁:α) ≠ 0 := nat.cast_ne_zero.2 d₁0,
have d₂a : (d₂:α) ≠ 0 := nat.cast_ne_zero.2 d₂0,
rw [num_denom', num_denom'] at h ⊢,
rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero] at h; simp [d₁0, d₂0] at h ⊢,
rwa [eq_div_iff_mul_eq d₂a, division_def, mul_assoc, (d₁.cast_commute (d₂:α)).inv_left₀.eq,
← mul_assoc, ← division_def, eq_comm, eq_div_iff_mul_eq d₁a, eq_comm,
← int.cast_coe_nat, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul,
int.cast_inj, ← mk_eq (int.coe_nat_ne_zero.2 d₁0) (int.coe_nat_ne_zero.2 d₂0)] at h
end
theorem cast_injective [char_zero α] : function.injective (coe : ℚ → α)
| m n := cast_inj.1
@[simp] theorem cast_eq_zero [char_zero α] {n : ℚ} : (n : α) = 0 ↔ n = 0 :=
by rw [← cast_zero, cast_inj]
theorem cast_ne_zero [char_zero α] {n : ℚ} : (n : α) ≠ 0 ↔ n ≠ 0 :=
not_congr cast_eq_zero
@[simp, norm_cast] theorem cast_add [char_zero α] (m n) :
((m + n : ℚ) : α) = m + n :=
cast_add_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_sub [char_zero α] (m n) :
((m - n : ℚ) : α) = m - n :=
cast_sub_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_mul [char_zero α] (m n) :
((m * n : ℚ) : α) = m * n :=
cast_mul_of_ne_zero (nat.cast_ne_zero.2 $ ne_of_gt m.pos) (nat.cast_ne_zero.2 $ ne_of_gt n.pos)
@[simp, norm_cast] theorem cast_bit0 [char_zero α] (n : ℚ) :
((bit0 n : ℚ) : α) = bit0 n :=
cast_add _ _
@[simp, norm_cast] theorem cast_bit1 [char_zero α] (n : ℚ) :
((bit1 n : ℚ) : α) = bit1 n :=
by rw [bit1, cast_add, cast_one, cast_bit0]; refl
variable (α)
/-- Coercion `ℚ → α` as a `ring_hom`. -/
def cast_hom [char_zero α] : ℚ →+* α := ⟨coe, cast_one, cast_mul, cast_zero, cast_add⟩
variable {α}
@[simp] lemma coe_cast_hom [char_zero α] : ⇑(cast_hom α) = coe := rfl
@[simp, norm_cast] theorem cast_inv [char_zero α] (n) : ((n⁻¹ : ℚ) : α) = n⁻¹ :=
(cast_hom α).map_inv _
@[simp, norm_cast] theorem cast_div [char_zero α] (m n) :
((m / n : ℚ) : α) = m / n :=
(cast_hom α).map_div _ _
@[norm_cast] theorem cast_mk [char_zero α] (a b : ℤ) : ((a /. b) : α) = a / b :=
by simp only [mk_eq_div, cast_div, cast_coe_int]
@[simp, norm_cast] theorem cast_pow [char_zero α] (q) (k : ℕ) :
((q ^ k : ℚ) : α) = q ^ k :=
(cast_hom α).map_pow q k
end with_div_ring
@[simp, norm_cast] theorem cast_nonneg [linear_ordered_field α] : ∀ {n : ℚ}, 0 ≤ (n : α) ↔ 0 ≤ n
| ⟨n, d, h, c⟩ :=
by { rw [num_denom', cast_mk, mk_eq_div, div_nonneg_iff, div_nonneg_iff], norm_cast }
@[simp, norm_cast] theorem cast_le [linear_ordered_field α] {m n : ℚ} : (m : α) ≤ n ↔ m ≤ n :=
by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg]
@[simp, norm_cast] theorem cast_lt [linear_ordered_field α] {m n : ℚ} : (m : α) < n ↔ m < n :=
by simpa [-cast_le] using not_congr (@cast_le α _ n m)
@[simp] theorem cast_nonpos [linear_ordered_field α] {n : ℚ} : (n : α) ≤ 0 ↔ n ≤ 0 :=
by rw [← cast_zero, cast_le]
@[simp] theorem cast_pos [linear_ordered_field α] {n : ℚ} : (0 : α) < n ↔ 0 < n :=
by rw [← cast_zero, cast_lt]
@[simp] theorem cast_lt_zero [linear_ordered_field α] {n : ℚ} : (n : α) < 0 ↔ n < 0 :=
by rw [← cast_zero, cast_lt]
@[simp, norm_cast] theorem cast_id : ∀ n : ℚ, ↑n = n
| ⟨n, d, h, c⟩ := by rw [num_denom', cast_mk, mk_eq_div]
@[simp, norm_cast] theorem cast_min [linear_ordered_field α] {a b : ℚ} :
(↑(min a b) : α) = min a b :=
by by_cases a ≤ b; simp [h, min_def]
@[simp, norm_cast] theorem cast_max [linear_ordered_field α] {a b : ℚ} :
(↑(max a b) : α) = max a b :=
by by_cases b ≤ a; simp [h, max_def]
@[simp, norm_cast] theorem cast_abs [linear_ordered_field α] {q : ℚ} :
((|q| : ℚ) : α) = |q| :=
by simp [abs_eq_max_neg]
end rat
open rat ring_hom
lemma ring_hom.eq_rat_cast {k} [division_ring k] (f : ℚ →+* k) (r : ℚ) : f r = r :=
calc f r = f (r.1 / r.2) : by rw [← int.cast_coe_nat, ← mk_eq_div, num_denom]
... = f r.1 / f r.2 : f.map_div _ _
... = r.1 / r.2 : by rw [map_nat_cast, map_int_cast]
-- This seems to be true for a `[char_p k]` too because `k'` must have the same characteristic
-- but the proof would be much longer
lemma ring_hom.map_rat_cast {k k'} [division_ring k] [char_zero k] [division_ring k']
(f : k →+* k') (r : ℚ) :
f r = r :=
(f.comp (cast_hom k)).eq_rat_cast r
lemma ring_hom.ext_rat {R : Type*} [semiring R] (f g : ℚ →+* R) : f = g :=
begin
ext r,
refine rat.num_denom_cases_on' r _,
intros a b b0,
let φ : ℤ →+* R := f.comp (int.cast_ring_hom ℚ),
let ψ : ℤ →+* R := g.comp (int.cast_ring_hom ℚ),
rw [rat.mk_eq_div, int.cast_coe_nat],
have b0' : (b:ℚ) ≠ 0 := nat.cast_ne_zero.2 b0,
have : ∀ n : ℤ, f n = g n := λ n, show φ n = ψ n, by rw [φ.ext_int ψ],
calc f (a * b⁻¹)
= f a * f b⁻¹ * (g (b:ℤ) * g b⁻¹) :
by rw [int.cast_coe_nat, ← g.map_mul, mul_inv_cancel b0', g.map_one, mul_one, f.map_mul]
... = g a * f b⁻¹ * (f (b:ℤ) * g b⁻¹) : by rw [this a, ← this b]
... = g (a * b⁻¹) :
by rw [int.cast_coe_nat, mul_assoc, ← mul_assoc (f b⁻¹),
← f.map_mul, inv_mul_cancel b0', f.map_one, one_mul, g.map_mul]
end
instance rat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℚ →+* R) :=
⟨ring_hom.ext_rat⟩
namespace monoid_with_zero_hom
variables {M : Type*} [group_with_zero M]
/-- If `f` and `g` agree on the integers then they are equal `φ`.
See note [partially-applied ext lemmas] for why `comp` is used here. -/
@[ext]
theorem ext_rat {f g : monoid_with_zero_hom ℚ M}
(same_on_int : f.comp (int.cast_ring_hom ℚ).to_monoid_with_zero_hom =
g.comp (int.cast_ring_hom ℚ).to_monoid_with_zero_hom) : f = g :=
begin
have same_on_int' : ∀ k : ℤ, f k = g k := congr_fun same_on_int,
ext x,
rw [← @rat.num_denom x, rat.mk_eq_div, f.map_div, g.map_div,
same_on_int' x.num, same_on_int' x.denom],
end
/-- Positive integer values of a morphism `φ` and its value on `-1` completely determine `φ`. -/
theorem ext_rat_on_pnat {f g : monoid_with_zero_hom ℚ M}
(same_on_neg_one : f (-1) = g (-1)) (same_on_pnat : ∀ n : ℕ, 0 < n → f n = g n) : f = g :=
ext_rat $ ext_int' (by simpa) ‹_›
end monoid_with_zero_hom
|
afaa9ec922037d71ade6ead6f7dcb3a81d0ec1e5 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/convex/gauge.lean | 66ad8807d7fff0a1746321fcdc85c323bf40b459 | [
"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 | 18,040 | 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 analysis.convex.star
import analysis.normed_space.pointwise
import analysis.seminorm
/-!
# The Minkowksi functional
This file defines the Minkowski functional, aka gauge.
The Minkowski functional of a set `s` is the function which associates each point to how much you
need to scale `s` for `x` to be inside it. When `s` is symmetric, convex and absorbent, its gauge is
a seminorm. Reciprocally, any seminorm arises as the gauge of some set, namely its unit ball. This
induces the equivalence of seminorms and locally convex topological vector spaces.
## Main declarations
For a real vector space,
* `gauge`: Aka Minkowksi functional. `gauge s x` is the least (actually, an infimum) `r` such
that `x ∈ r • s`.
* `gauge_seminorm`: The Minkowski functional as a seminorm, when `s` is symmetric, convex and
absorbent.
## References
* [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966]
## Tags
Minkowski functional, gauge
-/
open normed_field set
open_locale pointwise
noncomputable theory
variables {E : Type*}
section add_comm_group
variables [add_comm_group E] [module ℝ E]
/--The Minkowski functional. Given a set `s` in a real vector space, `gauge s` is the functional
which sends `x : E` to the smallest `r : ℝ` such that `x` is in `s` scaled by `r`. -/
def gauge (s : set E) (x : E) : ℝ := Inf {r : ℝ | 0 < r ∧ x ∈ r • s}
variables {s t : set E} {a : ℝ} {x : E}
lemma gauge_def : gauge s x = Inf {r ∈ set.Ioi 0 | x ∈ r • s} := rfl
/-- An alternative definition of the gauge using scalar multiplication on the element rather than on
the set. -/
lemma gauge_def' : gauge s x = Inf {r ∈ set.Ioi 0 | r⁻¹ • x ∈ s} :=
begin
unfold gauge,
congr' 1,
ext r,
exact and_congr_right (λ hr, mem_smul_set_iff_inv_smul_mem₀ hr.ne' _ _),
end
private lemma gauge_set_bdd_below : bdd_below {r : ℝ | 0 < r ∧ x ∈ r • s} := ⟨0, λ r hr, hr.1.le⟩
/-- If the given subset is `absorbent` then the set we take an infimum over in `gauge` is nonempty,
which is useful for proving many properties about the gauge. -/
lemma absorbent.gauge_set_nonempty (absorbs : absorbent ℝ s) :
{r : ℝ | 0 < r ∧ x ∈ r • s}.nonempty :=
let ⟨r, hr₁, hr₂⟩ := absorbs x in ⟨r, hr₁, hr₂ r (real.norm_of_nonneg hr₁.le).ge⟩
lemma gauge_mono (hs : absorbent ℝ s) (h : s ⊆ t) : gauge t ≤ gauge s :=
λ x, cInf_le_cInf gauge_set_bdd_below hs.gauge_set_nonempty $ λ r hr, ⟨hr.1, smul_set_mono h hr.2⟩
lemma exists_lt_of_gauge_lt (absorbs : absorbent ℝ s) (h : gauge s x < a) :
∃ b, 0 < b ∧ b < a ∧ x ∈ b • s :=
begin
obtain ⟨b, ⟨hb, hx⟩, hba⟩ := exists_lt_of_cInf_lt absorbs.gauge_set_nonempty h,
exact ⟨b, hb, hba, hx⟩,
end
/-- The gauge evaluated at `0` is always zero (mathematically this requires `0` to be in the set `s`
but, the real infimum of the empty set in Lean being defined as `0`, it holds unconditionally). -/
@[simp] lemma gauge_zero : gauge s 0 = 0 :=
begin
rw gauge_def',
by_cases (0 : E) ∈ s,
{ simp only [smul_zero, sep_true, h, cInf_Ioi] },
{ simp only [smul_zero, sep_false, h, real.Inf_empty] }
end
@[simp] lemma gauge_zero' : gauge (0 : set E) = 0 :=
begin
ext,
rw gauge_def',
obtain rfl | hx := eq_or_ne x 0,
{ simp only [cInf_Ioi, mem_zero, pi.zero_apply, eq_self_iff_true, sep_true, smul_zero] },
{ simp only [mem_zero, pi.zero_apply, inv_eq_zero, smul_eq_zero],
convert real.Inf_empty,
exact eq_empty_iff_forall_not_mem.2 (λ r hr, hr.2.elim (ne_of_gt hr.1) hx) }
end
@[simp] lemma gauge_empty : gauge (∅ : set E) = 0 :=
by { ext, simp only [gauge_def', real.Inf_empty, mem_empty_eq, pi.zero_apply, sep_false] }
lemma gauge_of_subset_zero (h : s ⊆ 0) : gauge s = 0 :=
by { obtain rfl | rfl := subset_singleton_iff_eq.1 h, exacts [gauge_empty, gauge_zero'] }
/-- The gauge is always nonnegative. -/
lemma gauge_nonneg (x : E) : 0 ≤ gauge s x := real.Inf_nonneg _ $ λ x hx, hx.1.le
lemma gauge_neg (symmetric : ∀ x ∈ s, -x ∈ s) (x : E) : gauge s (-x) = gauge s x :=
begin
have : ∀ x, -x ∈ s ↔ x ∈ s := λ x, ⟨λ h, by simpa using symmetric _ h, symmetric x⟩,
rw [gauge_def', gauge_def'],
simp_rw [smul_neg, this],
end
lemma gauge_le_of_mem (ha : 0 ≤ a) (hx : x ∈ a • s) : gauge s x ≤ a :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [mem_singleton_iff.1 (zero_smul_subset _ hx), gauge_zero] },
{ exact cInf_le gauge_set_bdd_below ⟨ha', hx⟩ }
end
lemma gauge_le_eq (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : absorbent ℝ s) (ha : 0 ≤ a) :
{x | gauge s x ≤ a} = ⋂ (r : ℝ) (H : a < r), r • s :=
begin
ext,
simp_rw [set.mem_Inter, set.mem_set_of_eq],
refine ⟨λ h r hr, _, λ h, le_of_forall_pos_lt_add (λ ε hε, _)⟩,
{ have hr' := ha.trans_lt hr,
rw mem_smul_set_iff_inv_smul_mem₀ hr'.ne',
obtain ⟨δ, δ_pos, hδr, hδ⟩ := exists_lt_of_gauge_lt hs₂ (h.trans_lt hr),
suffices : (r⁻¹ * δ) • δ⁻¹ • x ∈ s,
{ rwa [smul_smul, mul_inv_cancel_right₀ δ_pos.ne'] at this },
rw mem_smul_set_iff_inv_smul_mem₀ δ_pos.ne' at hδ,
refine hs₁.smul_mem_of_zero_mem hs₀ hδ
⟨mul_nonneg (inv_nonneg.2 hr'.le) δ_pos.le, _⟩,
rw [inv_mul_le_iff hr', mul_one],
exact hδr.le },
{ have hε' := (lt_add_iff_pos_right a).2 (half_pos hε),
exact (gauge_le_of_mem (ha.trans hε'.le) $ h _ hε').trans_lt
(add_lt_add_left (half_lt_self hε) _) }
end
lemma gauge_lt_eq' (absorbs : absorbent ℝ s) (a : ℝ) :
{x | gauge s x < a} = ⋃ (r : ℝ) (H : 0 < r) (H : r < a), r • s :=
begin
ext,
simp_rw [mem_set_of_eq, mem_Union, exists_prop],
exact ⟨exists_lt_of_gauge_lt absorbs,
λ ⟨r, hr₀, hr₁, hx⟩, (gauge_le_of_mem hr₀.le hx).trans_lt hr₁⟩,
end
lemma gauge_lt_eq (absorbs : absorbent ℝ s) (a : ℝ) :
{x | gauge s x < a} = ⋃ (r ∈ set.Ioo 0 (a : ℝ)), r • s :=
begin
ext,
simp_rw [mem_set_of_eq, mem_Union, exists_prop, mem_Ioo, and_assoc],
exact ⟨exists_lt_of_gauge_lt absorbs,
λ ⟨r, hr₀, hr₁, hx⟩, (gauge_le_of_mem hr₀.le hx).trans_lt hr₁⟩,
end
lemma gauge_lt_one_subset_self (hs : convex ℝ s) (h₀ : (0 : E) ∈ s) (absorbs : absorbent ℝ s) :
{x | gauge s x < 1} ⊆ s :=
begin
rw gauge_lt_eq absorbs,
refine set.Union₂_subset (λ r hr _, _),
rintro ⟨y, hy, rfl⟩,
exact hs.smul_mem_of_zero_mem h₀ hy (Ioo_subset_Icc_self hr),
end
lemma gauge_le_one_of_mem {x : E} (hx : x ∈ s) : gauge s x ≤ 1 :=
gauge_le_of_mem zero_le_one $ by rwa one_smul
lemma self_subset_gauge_le_one : s ⊆ {x | gauge s x ≤ 1} := λ x, gauge_le_one_of_mem
lemma convex.gauge_le (hs : convex ℝ s) (h₀ : (0 : E) ∈ s) (absorbs : absorbent ℝ s) (a : ℝ) :
convex ℝ {x | gauge s x ≤ a} :=
begin
by_cases ha : 0 ≤ a,
{ rw gauge_le_eq hs h₀ absorbs ha,
exact convex_Inter (λ i, convex_Inter (λ hi, hs.smul _)) },
{ convert convex_empty,
exact eq_empty_iff_forall_not_mem.2 (λ x hx, ha $ (gauge_nonneg _).trans hx) }
end
lemma balanced.star_convex (hs : balanced ℝ s) : star_convex ℝ 0 s :=
star_convex_zero_iff.2 $ λ x hx a ha₀ ha₁,
hs _ (by rwa real.norm_of_nonneg ha₀) (smul_mem_smul_set hx)
lemma le_gauge_of_not_mem (hs₀ : star_convex ℝ 0 s) (hs₂ : absorbs ℝ s {x}) (hx : x ∉ a • s) :
a ≤ gauge s x :=
begin
rw star_convex_zero_iff at hs₀,
obtain ⟨r, hr, h⟩ := hs₂,
refine le_cInf ⟨r, hr, singleton_subset_iff.1 $ h _ (real.norm_of_nonneg hr.le).ge⟩ _,
rintro b ⟨hb, x, hx', rfl⟩,
refine not_lt.1 (λ hba, hx _),
have ha := hb.trans hba,
refine ⟨(a⁻¹ * b) • x, hs₀ hx' (mul_nonneg (inv_nonneg.2 ha.le) hb.le) _, _⟩,
{ rw ←div_eq_inv_mul,
exact div_le_one_of_le hba.le ha.le },
{ rw [←mul_smul, mul_inv_cancel_left₀ ha.ne'] }
end
lemma one_le_gauge_of_not_mem (hs₁ : star_convex ℝ 0 s) (hs₂ : absorbs ℝ s {x}) (hx : x ∉ s) :
1 ≤ gauge s x :=
le_gauge_of_not_mem hs₁ hs₂ $ by rwa one_smul
section linear_ordered_field
variables {α : Type*} [linear_ordered_field α] [mul_action_with_zero α ℝ] [ordered_smul α ℝ]
lemma gauge_smul_of_nonneg [mul_action_with_zero α E] [is_scalar_tower α ℝ (set E)] {s : set E}
{a : α} (ha : 0 ≤ a) (x : E) :
gauge s (a • x) = a • gauge s x :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [zero_smul, gauge_zero, zero_smul] },
rw [gauge_def', gauge_def', ←real.Inf_smul_of_nonneg ha],
congr' 1,
ext r,
simp_rw [set.mem_smul_set, set.mem_sep_eq],
split,
{ rintro ⟨hr, hx⟩,
simp_rw mem_Ioi at ⊢ hr,
rw ←mem_smul_set_iff_inv_smul_mem₀ hr.ne' at hx,
have := smul_pos (inv_pos.2 ha') hr,
refine ⟨a⁻¹ • r, ⟨this, _⟩, smul_inv_smul₀ ha'.ne' _⟩,
rwa [←mem_smul_set_iff_inv_smul_mem₀ this.ne', smul_assoc,
mem_smul_set_iff_inv_smul_mem₀ (inv_ne_zero ha'.ne'), inv_inv] },
{ rintro ⟨r, ⟨hr, hx⟩, rfl⟩,
rw mem_Ioi at ⊢ hr,
rw ←mem_smul_set_iff_inv_smul_mem₀ hr.ne' at hx,
have := smul_pos ha' hr,
refine ⟨this, _⟩,
rw [←mem_smul_set_iff_inv_smul_mem₀ this.ne', smul_assoc],
exact smul_mem_smul_set hx }
end
/-- In textbooks, this is the homogeneity of the Minkowksi functional. -/
lemma gauge_smul [module α E] [is_scalar_tower α ℝ (set E)] {s : set E}
(symmetric : ∀ x ∈ s, -x ∈ s) (r : α) (x : E) :
gauge s (r • x) = abs r • gauge s x :=
begin
rw ←gauge_smul_of_nonneg (abs_nonneg r),
obtain h | h := abs_choice r,
{ rw h },
{ rw [h, neg_smul, gauge_neg symmetric] },
{ apply_instance }
end
lemma gauge_smul_left_of_nonneg [mul_action_with_zero α E] [smul_comm_class α ℝ ℝ]
[is_scalar_tower α ℝ ℝ] [is_scalar_tower α ℝ E] {s : set E} {a : α} (ha : 0 ≤ a) :
gauge (a • s) = a⁻¹ • gauge s :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [inv_zero, zero_smul, gauge_of_subset_zero (zero_smul_subset _)] },
ext,
rw [gauge_def', pi.smul_apply, gauge_def', ←real.Inf_smul_of_nonneg (inv_nonneg.2 ha)],
congr' 1,
ext r,
simp_rw [set.mem_smul_set, set.mem_sep_eq],
split,
{ rintro ⟨hr, y, hy, h⟩,
simp_rw [mem_Ioi] at ⊢ hr,
refine ⟨a • r, ⟨smul_pos ha' hr, _⟩, inv_smul_smul₀ ha'.ne' _⟩,
rwa [smul_inv₀, smul_assoc, ←h, inv_smul_smul₀ ha'.ne'] },
{ rintro ⟨r, ⟨hr, hx⟩, rfl⟩,
rw mem_Ioi at ⊢ hr,
have := smul_pos ha' hr,
refine ⟨smul_pos (inv_pos.2 ha') hr, r⁻¹ • x, hx, _⟩,
rw [smul_inv₀, smul_assoc, inv_inv] }
end
lemma gauge_smul_left [module α E] [smul_comm_class α ℝ ℝ] [is_scalar_tower α ℝ ℝ]
[is_scalar_tower α ℝ E] {s : set E} (symmetric : ∀ x ∈ s, -x ∈ s) (a : α) :
gauge (a • s) = |a|⁻¹ • gauge s :=
begin
rw ←gauge_smul_left_of_nonneg (abs_nonneg a),
obtain h | h := abs_choice a,
{ rw h },
{ rw [h, set.neg_smul_set, ←set.smul_set_neg],
congr,
ext y,
refine ⟨symmetric _, λ hy, _⟩,
rw ←neg_neg y,
exact symmetric _ hy },
{ apply_instance }
end
end linear_ordered_field
section topological_space
variables [topological_space E] [has_continuous_smul ℝ E]
lemma interior_subset_gauge_lt_one (s : set E) : interior s ⊆ {x | gauge s x < 1} :=
begin
intros x hx,
let f : ℝ → E := λ t, t • x,
have hf : continuous f,
{ continuity },
let s' := f ⁻¹' (interior s),
have hs' : is_open s' := hf.is_open_preimage _ is_open_interior,
have one_mem : (1 : ℝ) ∈ s',
{ simpa only [s', f, set.mem_preimage, one_smul] },
obtain ⟨ε, hε₀, hε⟩ := (metric.nhds_basis_closed_ball.1 _).1
(is_open_iff_mem_nhds.1 hs' 1 one_mem),
rw real.closed_ball_eq_Icc at hε,
have hε₁ : 0 < 1 + ε := hε₀.trans (lt_one_add ε),
have : (1 + ε)⁻¹ < 1,
{ rw inv_lt_one_iff,
right,
linarith },
refine (gauge_le_of_mem (inv_nonneg.2 hε₁.le) _).trans_lt this,
rw mem_inv_smul_set_iff₀ hε₁.ne',
exact interior_subset
(hε ⟨(sub_le_self _ hε₀.le).trans ((le_add_iff_nonneg_right _).2 hε₀.le), le_rfl⟩),
end
lemma gauge_lt_one_eq_self_of_open (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : is_open s) :
{x | gauge s x < 1} = s :=
begin
refine (gauge_lt_one_subset_self hs₁ ‹_› $ absorbent_nhds_zero $ hs₂.mem_nhds hs₀).antisymm _,
convert interior_subset_gauge_lt_one s,
exact hs₂.interior_eq.symm,
end
lemma gauge_lt_one_of_mem_of_open (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : is_open s)
{x : E} (hx : x ∈ s) :
gauge s x < 1 :=
by rwa ←gauge_lt_one_eq_self_of_open hs₁ hs₀ hs₂ at hx
lemma gauge_lt_of_mem_smul (x : E) (ε : ℝ) (hε : 0 < ε) (hs₀ : (0 : E) ∈ s)
(hs₁ : convex ℝ s) (hs₂ : is_open s) (hx : x ∈ ε • s) :
gauge s x < ε :=
begin
have : ε⁻¹ • x ∈ s,
{ rwa ←mem_smul_set_iff_inv_smul_mem₀ hε.ne' },
have h_gauge_lt := gauge_lt_one_of_mem_of_open hs₁ hs₀ hs₂ this,
rwa [gauge_smul_of_nonneg (inv_nonneg.2 hε.le), smul_eq_mul, inv_mul_lt_iff hε, mul_one]
at h_gauge_lt,
apply_instance
end
end topological_space
lemma gauge_add_le (hs : convex ℝ s) (absorbs : absorbent ℝ s) (x y : E) :
gauge s (x + y) ≤ gauge s x + gauge s y :=
begin
refine le_of_forall_pos_lt_add (λ ε hε, _),
obtain ⟨a, ha, ha', hx⟩ := exists_lt_of_gauge_lt absorbs
(lt_add_of_pos_right (gauge s x) (half_pos hε)),
obtain ⟨b, hb, hb', hy⟩ := exists_lt_of_gauge_lt absorbs
(lt_add_of_pos_right (gauge s y) (half_pos hε)),
rw mem_smul_set_iff_inv_smul_mem₀ ha.ne' at hx,
rw mem_smul_set_iff_inv_smul_mem₀ hb.ne' at hy,
suffices : gauge s (x + y) ≤ a + b,
{ linarith },
have hab : 0 < a + b := add_pos ha hb,
apply gauge_le_of_mem hab.le,
have := convex_iff_div.1 hs hx hy ha.le hb.le hab,
rwa [smul_smul, smul_smul, ←mul_div_right_comm, ←mul_div_right_comm, mul_inv_cancel ha.ne',
mul_inv_cancel hb.ne', ←smul_add, one_div, ←mem_smul_set_iff_inv_smul_mem₀ hab.ne'] at this,
end
/-- `gauge s` as a seminorm when `s` is symmetric, convex and absorbent. -/
@[simps] def gauge_seminorm (hs₀ : ∀ x ∈ s, -x ∈ s) (hs₁ : convex ℝ s) (hs₂ : absorbent ℝ s) :
seminorm ℝ E :=
{ to_fun := gauge s,
smul' := λ r x, by rw [gauge_smul hs₀, real.norm_eq_abs, smul_eq_mul]; apply_instance,
triangle' := gauge_add_le hs₁ hs₂ }
section gauge_seminorm
variables {hs₀ : ∀ x ∈ s, -x ∈ s} {hs₁ : convex ℝ s} {hs₂ : absorbent ℝ s}
section topological_space
variables [topological_space E] [has_continuous_smul ℝ E]
lemma gauge_seminorm_lt_one_of_open (hs : is_open s) {x : E} (hx : x ∈ s) :
gauge_seminorm hs₀ hs₁ hs₂ x < 1 :=
gauge_lt_one_of_mem_of_open hs₁ hs₂.zero_mem hs hx
end topological_space
end gauge_seminorm
/-- Any seminorm arises as the gauge of its unit ball. -/
@[simp] protected lemma seminorm.gauge_ball (p : seminorm ℝ E) : gauge (p.ball 0 1) = p :=
begin
ext,
obtain hp | hp := {r : ℝ | 0 < r ∧ x ∈ r • p.ball 0 1}.eq_empty_or_nonempty,
{ rw [gauge, hp, real.Inf_empty],
by_contra,
have hpx : 0 < p x := (p.nonneg x).lt_of_ne h,
have hpx₂ : 0 < 2 * p x := mul_pos zero_lt_two hpx,
refine hp.subset ⟨hpx₂, (2 * p x)⁻¹ • x, _, smul_inv_smul₀ hpx₂.ne' _⟩,
rw [p.mem_ball_zero, p.smul, real.norm_eq_abs, abs_of_pos (inv_pos.2 hpx₂), inv_mul_lt_iff hpx₂,
mul_one],
exact lt_mul_of_one_lt_left hpx one_lt_two },
refine is_glb.cInf_eq ⟨λ r, _, λ r hr, le_of_forall_pos_le_add $ λ ε hε, _⟩ hp,
{ rintro ⟨hr, y, hy, rfl⟩,
rw p.mem_ball_zero at hy,
rw [p.smul, real.norm_eq_abs, abs_of_pos hr],
exact mul_le_of_le_one_right hr.le hy.le },
{ have hpε : 0 < p x + ε := add_pos_of_nonneg_of_pos (p.nonneg _) hε,
refine hr ⟨hpε, (p x + ε)⁻¹ • x, _, smul_inv_smul₀ hpε.ne' _⟩,
rw [p.mem_ball_zero, p.smul, real.norm_eq_abs, abs_of_pos (inv_pos.2 hpε), inv_mul_lt_iff hpε,
mul_one],
exact lt_add_of_pos_right _ hε }
end
lemma seminorm.gauge_seminorm_ball (p : seminorm ℝ E) :
gauge_seminorm (λ x, p.symmetric_ball_zero 1) (p.convex_ball 0 1)
(p.absorbent_ball_zero zero_lt_one) = p := fun_like.coe_injective p.gauge_ball
end add_comm_group
section norm
variables [semi_normed_group E] [normed_space ℝ E] {s : set E} {r : ℝ} {x : E}
lemma gauge_unit_ball (x : E) : gauge (metric.ball (0 : E) 1) x = ∥x∥ :=
begin
obtain rfl | hx := eq_or_ne x 0,
{ rw [norm_zero, gauge_zero] },
refine (le_of_forall_pos_le_add $ λ ε hε, _).antisymm _,
{ have := add_pos_of_nonneg_of_pos (norm_nonneg x) hε,
refine gauge_le_of_mem this.le _,
rw [smul_ball this.ne', smul_zero, real.norm_of_nonneg this.le, mul_one, mem_ball_zero_iff],
exact lt_add_of_pos_right _ hε },
refine le_gauge_of_not_mem balanced_ball_zero.star_convex
(absorbent_ball_zero zero_lt_one).absorbs (λ h, _),
obtain hx' | hx' := eq_or_ne (∥x∥) 0,
{ rw hx' at h,
exact hx (zero_smul_subset _ h) },
{ rw [mem_smul_set_iff_inv_smul_mem₀ hx', mem_ball_zero_iff, norm_smul, norm_inv, norm_norm,
inv_mul_cancel hx'] at h,
exact lt_irrefl _ h }
end
lemma gauge_ball (hr : 0 < r) (x : E) : gauge (metric.ball (0 : E) r) x = ∥x∥ / r :=
begin
rw [←smul_unit_ball_of_pos hr, gauge_smul_left, pi.smul_apply, gauge_unit_ball, smul_eq_mul,
abs_of_nonneg hr.le, div_eq_inv_mul],
simp_rw [mem_ball_zero_iff, norm_neg],
exact λ _, id,
end
lemma mul_gauge_le_norm (hs : metric.ball (0 : E) r ⊆ s) : r * gauge s x ≤ ∥x∥ :=
begin
obtain hr | hr := le_or_lt r 0,
{ exact (mul_nonpos_of_nonpos_of_nonneg hr $ gauge_nonneg _).trans (norm_nonneg _) },
rw [mul_comm, ←le_div_iff hr, ←gauge_ball hr],
exact gauge_mono (absorbent_ball_zero hr) hs x,
end
end norm
|
e2ece76767847e35a667069e86112ab80a0aca37 | b00eb947a9c4141624aa8919e94ce6dcd249ed70 | /src/Init/System/IO.lean | 5bd8f8726aac603965dd260948b58df739a1d454 | [
"Apache-2.0"
] | permissive | gebner/lean4-old | a4129a041af2d4d12afb3a8d4deedabde727719b | ee51cdfaf63ee313c914d83264f91f414a0e3b6e | refs/heads/master | 1,683,628,606,745 | 1,622,651,300,000 | 1,622,654,405,000 | 142,608,821 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,060 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Luke Nelson, Jared Roesch, Leonardo de Moura, Sebastian Ullrich
-/
prelude
import Init.Control.EState
import Init.Control.Reader
import Init.Data.String
import Init.Data.ByteArray
import Init.System.IOError
import Init.System.FilePath
import Init.System.ST
import Init.Data.ToString.Macro
import Init.Data.Ord
open System
/-- Like https://hackage.haskell.org/package/ghc-Prim-0.5.2.0/docs/GHC-Prim.html#t:RealWorld.
Makes sure we never reorder `IO` operations.
TODO: mark opaque -/
def IO.RealWorld : Type := Unit
/- TODO(Leo): mark it as an opaque definition. Reason: prevent
functions defined in other modules from accessing `IO.RealWorld`.
We don't want action such as
```
def getWorld : IO (IO.RealWorld) := get
```
-/
def EIO (ε : Type) : Type → Type := EStateM ε IO.RealWorld
@[inline] def EIO.catchExceptions (x : EIO ε α) (h : ε → EIO Empty α) : EIO Empty α :=
fun s => match x s with
| EStateM.Result.ok a s => EStateM.Result.ok a s
| EStateM.Result.error ex s => h ex s
instance : Monad (EIO ε) := inferInstanceAs (Monad (EStateM ε IO.RealWorld))
instance : MonadFinally (EIO ε) := inferInstanceAs (MonadFinally (EStateM ε IO.RealWorld))
instance : MonadExceptOf ε (EIO ε) := inferInstanceAs (MonadExceptOf ε (EStateM ε IO.RealWorld))
instance : OrElse (EIO ε α) := ⟨MonadExcept.orelse⟩
instance [Inhabited ε] : Inhabited (EIO ε α) := inferInstanceAs (Inhabited (EStateM ε IO.RealWorld α))
open IO (Error) in
abbrev IO : Type → Type := EIO Error
@[inline] def EIO.toIO (f : ε → IO.Error) (x : EIO ε α) : IO α :=
x.adaptExcept f
@[inline] def EIO.toIO' (x : EIO ε α) : IO (Except ε α) :=
EIO.toIO (fun _ => unreachable!) (observing x)
@[inline] def IO.toEIO (f : IO.Error → ε) (x : IO α) : EIO ε α :=
x.adaptExcept f
/- After we inline `EState.run'`, the closed term `((), ())` is generated, where the second `()`
represents the "initial world". We don't want to cache this closed term. So, we disable
the "extract closed terms" optimization. -/
set_option compiler.extract_closed false in
@[inline] unsafe def unsafeEIO (fn : EIO ε α) : Except ε α :=
match fn.run () with
| EStateM.Result.ok a _ => Except.ok a
| EStateM.Result.error e _ => Except.error e
@[inline] unsafe def unsafeIO (fn : IO α) : Except IO.Error α :=
unsafeEIO fn
@[extern "lean_io_timeit"] constant timeit (msg : @& String) (fn : IO α) : IO α
@[extern "lean_io_allocprof"] constant allocprof (msg : @& String) (fn : IO α) : IO α
/- Programs can execute IO actions during initialization that occurs before
the `main` function is executed. The attribute `[init <action>]` specifies
which IO action is executed to set the value of an opaque constant.
The action `initializing` returns `true` iff it is invoked during initialization. -/
@[extern "lean_io_initializing"] constant IO.initializing : IO Bool
namespace IO
def ofExcept [ToString ε] (e : Except ε α) : IO α :=
match e with
| Except.ok a => pure a
| Except.error e => throw (IO.userError (toString e))
def lazyPure (fn : Unit → α) : IO α :=
pure (fn ())
/-- Monotonically increasing time since an unspecified past point in milliseconds. No relation to wall clock time. -/
@[extern "lean_io_mono_ms_now"] constant monoMsNow : IO Nat
def sleep (ms : UInt32) : IO Unit :=
-- TODO: add a proper primitive for IO.sleep
fun s => dbgSleep ms fun _ => EStateM.Result.ok () s
/--
Run `act` in a separate `Task`. This is similar to Haskell's [`unsafeInterleaveIO`](http://hackage.haskell.org/package/base-4.14.0.0/docs/System-IO-Unsafe.html#v:unsafeInterleaveIO),
except that the `Task` is started eagerly as usual. Thus pure accesses to the `Task` do not influence the impure `act`
computation.
Unlike with pure tasks created by `Task.mk`, tasks created by this function will be run even if the last reference
to the task is dropped. `act` should manually check for cancellation via `IO.checkCanceled` if it wants to react
to that. -/
@[extern "lean_io_as_task"]
constant asTask (act : IO α) (prio := Task.Priority.default) : IO (Task (Except IO.Error α))
/-- See `IO.asTask`. -/
@[extern "lean_io_map_task"]
constant mapTask (f : α → IO β) (t : Task α) (prio := Task.Priority.default) : IO (Task (Except IO.Error β))
/-- See `IO.asTask`. -/
@[extern "lean_io_bind_task"]
constant bindTask (t : Task α) (f : α → IO (Task (Except IO.Error β))) (prio := Task.Priority.default) : IO (Task (Except IO.Error β))
def mapTasks (f : List α → IO β) (tasks : List (Task α)) (prio := Task.Priority.default) : IO (Task (Except IO.Error β)) :=
go tasks []
where
go
| t::ts, as =>
IO.bindTask t (fun a => go ts (a :: as)) prio
| [], as => IO.asTask (f as.reverse) prio
/-- Check if the task's cancellation flag has been set by calling `IO.cancel` or dropping the last reference to the task. -/
@[extern "lean_io_check_canceled"] constant checkCanceled : IO Bool
/-- Request cooperative cancellation of the task. The task must explicitly call `IO.checkCanceled` to react to the cancellation. -/
@[extern "lean_io_cancel"] constant cancel : @& Task α → IO Unit
/-- Check if the task has finished execution, at which point calling `Task.get` will return immediately. -/
@[extern "lean_io_has_finished"] constant hasFinished : @& Task α → IO Bool
/-- Wait for the task to finish, then return its result. -/
@[extern "lean_io_wait"] constant wait : Task α → IO α
/-- Wait until any of the tasks in the given list has finished, then return its result. -/
@[extern "lean_io_wait_any"] constant waitAny : @& List (Task α) → IO α
/-- Helper method for implementing "deterministic" timeouts. It is the numbe of "small" memory allocations performed by the current execution thread. -/
@[extern "lean_io_get_num_heartbeats"] constant getNumHeartbeats : EIO ε Nat
inductive FS.Mode where
| read | write | readWrite | append
constant FS.Handle : Type := Unit
/--
A pure-Lean abstraction of POSIX streams. We use `Stream`s for the standard streams stdin/stdout/stderr so we can
capture output of `#eval` commands into memory. -/
structure FS.Stream where
isEof : IO Bool
flush : IO Unit
read : USize → IO ByteArray
write : ByteArray → IO Unit
getLine : IO String
putStr : String → IO Unit
namespace Prim
open FS
@[extern "lean_get_stdin"] constant getStdin : IO FS.Stream
@[extern "lean_get_stdout"] constant getStdout : IO FS.Stream
@[extern "lean_get_stderr"] constant getStderr : IO FS.Stream
@[extern "lean_get_set_stdin"] constant setStdin : FS.Stream → IO FS.Stream
@[extern "lean_get_set_stdout"] constant setStdout : FS.Stream → IO FS.Stream
@[extern "lean_get_set_stderr"] constant setStderr : FS.Stream → IO FS.Stream
@[specialize] partial def iterate (a : α) (f : α → IO (Sum α β)) : IO β := do
let v ← f a
match v with
| Sum.inl a => iterate a f
| Sum.inr b => pure b
-- @[export lean_fopen_flags]
def fopenFlags (m : FS.Mode) (b : Bool) : String :=
let mode :=
match m with
| FS.Mode.read => "r"
| FS.Mode.write => "w"
| FS.Mode.readWrite => "r+"
| FS.Mode.append => "a" ;
let bin := if b then "b" else "t"
mode ++ bin
@[extern "lean_io_prim_handle_mk"] constant Handle.mk (fn : @& FilePath) (mode : @& String) : IO Handle
@[extern "lean_io_prim_handle_is_eof"] constant Handle.isEof (h : @& Handle) : IO Bool
@[extern "lean_io_prim_handle_flush"] constant Handle.flush (h : @& Handle) : IO Unit
@[extern "lean_io_prim_handle_read"] constant Handle.read (h : @& Handle) (bytes : USize) : IO ByteArray
@[extern "lean_io_prim_handle_write"] constant Handle.write (h : @& Handle) (buffer : @& ByteArray) : IO Unit
@[extern "lean_io_prim_handle_get_line"] constant Handle.getLine (h : @& Handle) : IO String
@[extern "lean_io_prim_handle_put_str"] constant Handle.putStr (h : @& Handle) (s : @& String) : IO Unit
@[extern "lean_io_getenv"] constant getEnv (var : @& String) : IO (Option String)
@[extern "lean_io_realpath"] constant realPath (fname : FilePath) : IO FilePath
@[extern "lean_io_remove_file"] constant removeFile (fname : @& FilePath) : IO Unit
@[extern "lean_io_app_dir"] constant appPath : IO FilePath
@[extern "lean_io_current_dir"] constant currentDir : IO FilePath
end Prim
namespace FS
variable [Monad m] [MonadLiftT IO m]
def Handle.mk (fn : FilePath) (Mode : Mode) (bin : Bool := true) : m Handle :=
liftM (Prim.Handle.mk fn (Prim.fopenFlags Mode bin))
@[inline]
def withFile (fn : FilePath) (mode : Mode) (f : Handle → m α) : m α :=
Handle.mk fn mode >>= f
/-- returns whether the end of the file has been reached while reading a file.
`h.isEof` returns true /after/ the first attempt at reading past the end of `h`.
Once `h.isEof` is true, the reading `h` raises `IO.Error.eof`.
-/
def Handle.isEof : Handle → m Bool := liftM ∘ Prim.Handle.isEof
def Handle.flush : Handle → m Unit := liftM ∘ Prim.Handle.flush
def Handle.read (h : Handle) (bytes : Nat) : m ByteArray := liftM (Prim.Handle.read h (USize.ofNat bytes))
def Handle.write (h : Handle) (s : ByteArray) : m Unit := liftM (Prim.Handle.write h s)
def Handle.getLine : Handle → m String := liftM ∘ Prim.Handle.getLine
def Handle.putStr (h : Handle) (s : String) : m Unit :=
liftM <| Prim.Handle.putStr h s
def Handle.putStrLn (h : Handle) (s : String) : m Unit :=
h.putStr (s.push '\n')
partial def Handle.readBinToEnd (h : Handle) : m ByteArray := do
let rec loop (acc : ByteArray) : m ByteArray := do
if ← h.isEof then
return acc
else
let buf ← h.read 1024
loop (acc ++ buf)
loop ByteArray.empty
partial def Handle.readToEnd (h : Handle) : m String := do
let rec read (s : String) := do
let line ← h.getLine
if line.length == 0 then pure s else read (s ++ line)
read ""
def readBinFile (fname : FilePath) : m ByteArray := do
let h ← Handle.mk fname Mode.read true
h.readBinToEnd
def readFile (fname : FilePath) : m String := do
let h ← Handle.mk fname Mode.read false
h.readToEnd
partial def lines (fname : FilePath) : m (Array String) := do
let h ← Handle.mk fname Mode.read false
let rec read (lines : Array String) := do
let line ← h.getLine
if line.length == 0 then
pure lines
else if line.back == '\n' then
let line := line.dropRight 1
let line := if System.Platform.isWindows && line.back == '\x0d' then line.dropRight 1 else line
read <| lines.push line
else
pure <| lines.push line
read #[]
def writeBinFile (fname : FilePath) (content : ByteArray) : m Unit := do
let h ← Handle.mk fname Mode.write true
h.write content
def writeFile (fname : FilePath) (content : String) : m Unit := do
let h ← Handle.mk fname Mode.write false
h.putStr content
def Stream.putStrLn (strm : FS.Stream) (s : String) : m Unit :=
liftM (strm.putStr (s.push '\n'))
structure DirEntry where
root : FilePath
fileName : String
deriving Repr
def DirEntry.path (entry : DirEntry) : FilePath :=
entry.root / entry.fileName
inductive FileType where
| dir
| file
| symlink
| other
deriving Repr, BEq
structure SystemTime where
sec : Int
nsec : UInt32
deriving Repr, BEq, Ord, Inhabited
structure Metadata where
--permissions : ...
accessed : SystemTime
modified : SystemTime
byteSize : UInt64
type : FileType
deriving Repr
end FS
end IO
namespace System.FilePath
open IO
variable [Monad m] [MonadLiftT IO m]
@[extern "lean_io_read_dir"]
constant readDir : @& FilePath → IO (Array IO.FS.DirEntry)
@[extern "lean_io_metadata"]
constant metadata : @& FilePath → IO IO.FS.Metadata
def isDir (p : FilePath) : IO Bool :=
try
return (← p.metadata).type == IO.FS.FileType.dir
catch _ =>
return false
def pathExists (p : FilePath) : IO Bool :=
(p.metadata *> pure true) <|> pure false
end System.FilePath
namespace IO
section
variable [Monad m] [MonadLiftT IO m]
def getStdin : m FS.Stream := liftM Prim.getStdin
def getStdout : m FS.Stream := liftM Prim.getStdout
def getStderr : m FS.Stream := liftM Prim.getStderr
/-- Replaces the stdin stream of the current thread and returns its previous value. -/
def setStdin : FS.Stream → m FS.Stream := liftM ∘ Prim.setStdin
/-- Replaces the stdout stream of the current thread and returns its previous value. -/
def setStdout : FS.Stream → m FS.Stream := liftM ∘ Prim.setStdout
/-- Replaces the stderr stream of the current thread and returns its previous value. -/
def setStderr : FS.Stream → m FS.Stream := liftM ∘ Prim.setStderr
def withStdin [MonadFinally m] (h : FS.Stream) (x : m α) : m α := do
let prev ← setStdin h
try x finally discard <| setStdin prev
def withStdout [MonadFinally m] (h : FS.Stream) (x : m α) : m α := do
let prev ← setStdout h
try
x
finally
discard <| setStdout prev
def withStderr [MonadFinally m] (h : FS.Stream) (x : m α) : m α := do
let prev ← setStderr h
try x finally discard <| setStderr prev
def print [ToString α] (s : α) : IO Unit := do
let out ← getStdout
out.putStr <| toString s
def println [ToString α] (s : α) : IO Unit :=
print ((toString s).push '\n')
def eprint [ToString α] (s : α) : IO Unit := do
let out ← getStderr
liftM <| out.putStr <| toString s
def eprintln [ToString α] (s : α) : IO Unit :=
eprint <| toString s |>.push '\n'
@[export lean_io_eprintln]
private def eprintlnAux (s : String) : IO Unit :=
eprintln s
def getEnv : String → m (Option String) := liftM ∘ Prim.getEnv
def realPath : FilePath → m FilePath := liftM ∘ Prim.realPath
def removeFile : FilePath → m Unit := liftM ∘ Prim.removeFile
def appPath : m FilePath := liftM Prim.appPath
def appDir : m FilePath := do
let p ← appPath
let some p ← pure p.parent
| liftM (m := IO) <| throw <| IO.userError s!"System.IO.appDir: unexpected filename '{p}'"
realPath p
def currentDir : m FilePath := liftM Prim.currentDir
@[extern "lean_io_create_dir"]
constant createDir : @& FilePath → IO Unit
partial def createDirAll (p : FilePath) : IO Unit := do
if ← p.isDir then
return ()
if let some parent := p.parent then
createDirAll parent
try
createDir p
catch
| e =>
if ← p.isDir then
pure () -- I guess someone else was faster
else
throw e
end
namespace Process
inductive Stdio where
| piped
| inherit
| null
def Stdio.toHandleType : Stdio → Type
| Stdio.piped => FS.Handle
| Stdio.inherit => Unit
| Stdio.null => Unit
structure StdioConfig where
/- Configuration for the process' stdin handle. -/
stdin := Stdio.inherit
/- Configuration for the process' stdout handle. -/
stdout := Stdio.inherit
/- Configuration for the process' stderr handle. -/
stderr := Stdio.inherit
structure SpawnArgs extends StdioConfig where
/- Command name. -/
cmd : String
/- Arguments for the process -/
args : Array String := #[]
/- Working directory for the process. Inherit from current process if `none`. -/
cwd : Option FilePath := none
/- Add or remove environment variables for the process. -/
env : Array (String × Option String) := #[]
-- TODO(Sebastian): constructor must be private
structure Child (cfg : StdioConfig) where
stdin : cfg.stdin.toHandleType
stdout : cfg.stdout.toHandleType
stderr : cfg.stderr.toHandleType
@[extern "lean_io_process_spawn"] constant spawn (args : SpawnArgs) : IO (Child args.toStdioConfig)
@[extern "lean_io_process_child_wait"] constant Child.wait {cfg : @& StdioConfig} : @& Child cfg → IO UInt32
structure Output where
exitCode : UInt32
stdout : String
stderr : String
/-- Run process to completion and capture output. -/
def output (args : SpawnArgs) : IO Output := do
let child ← spawn { args with stdout := Stdio.piped, stderr := Stdio.piped }
let stdout ← IO.asTask child.stdout.readToEnd Task.Priority.dedicated
let stderr ← child.stderr.readToEnd
let exitCode ← child.wait
let stdout ← IO.ofExcept stdout.get
pure { exitCode := exitCode, stdout := stdout, stderr := stderr }
/-- Run process to completion and return stdout on success. -/
def run (args : SpawnArgs) : IO String := do
let out ← output args
if out.exitCode != 0 then
throw <| IO.userError <| "process '" ++ args.cmd ++ "' exited with code " ++ toString out.exitCode;
pure out.stdout
end Process
structure AccessRight where
read : Bool := false
write : Bool := false
execution : Bool := false
def AccessRight.flags (acc : AccessRight) : UInt32 :=
let r : UInt32 := if acc.read then 0x4 else 0
let w : UInt32 := if acc.write then 0x2 else 0
let x : UInt32 := if acc.execution then 0x1 else 0
r.lor <| w.lor x
structure FileRight where
user : AccessRight := {}
group : AccessRight := {}
other : AccessRight := {}
def FileRight.flags (acc : FileRight) : UInt32 :=
let u : UInt32 := acc.user.flags.shiftLeft 6
let g : UInt32 := acc.group.flags.shiftLeft 3
let o : UInt32 := acc.other.flags
u.lor <| g.lor o
@[extern "lean_chmod"] constant Prim.setAccessRights (filename : @& FilePath) (mode : UInt32) : IO Unit
def setAccessRights (filename : FilePath) (mode : FileRight) : IO Unit :=
Prim.setAccessRights filename mode.flags
/- References -/
abbrev Ref (α : Type) := ST.Ref IO.RealWorld α
instance : MonadLift (ST IO.RealWorld) (EIO ε) := ⟨fun x s =>
match x s with
| EStateM.Result.ok a s => EStateM.Result.ok a s
| EStateM.Result.error ex _ => nomatch ex⟩
def mkRef [Monad m] [MonadLiftT (ST IO.RealWorld) m] (a : α) : m (IO.Ref α) :=
ST.mkRef a
namespace FS
namespace Stream
@[export lean_stream_of_handle]
def ofHandle (h : Handle) : Stream := {
isEof := Prim.Handle.isEof h,
flush := Prim.Handle.flush h,
read := Prim.Handle.read h,
write := Prim.Handle.write h,
getLine := Prim.Handle.getLine h,
putStr := Prim.Handle.putStr h,
}
structure Buffer where
data : ByteArray := ByteArray.empty
pos : Nat := 0
def ofBuffer (r : Ref Buffer) : Stream := {
isEof := do let b ← r.get; pure <| b.pos >= b.data.size,
flush := pure (),
read := fun n => r.modifyGet fun b =>
let data := b.data.extract b.pos (b.pos + n.toNat)
(data, { b with pos := b.pos + data.size }),
write := fun data => r.modify fun b =>
-- set `exact` to `false` so that repeatedly writing to the stream does not impose quadratic run time
{ b with data := data.copySlice 0 b.data b.pos data.size false, pos := b.pos + data.size },
getLine := r.modifyGet fun b =>
let pos := match b.data.findIdx? (start := b.pos) fun u => u == 0 || u = '\n'.toNat.toUInt8 with
-- include '\n', but not '\0'
| some pos => if b.data.get! pos == 0 then pos else pos + 1
| none => b.data.size
(String.fromUTF8Unchecked <| b.data.extract b.pos pos, { b with pos := pos }),
putStr := fun s => r.modify fun b =>
let data := s.toUTF8
{ b with data := data.copySlice 0 b.data b.pos data.size false, pos := b.pos + data.size },
}
end Stream
/-- Run action with `stdin` emptied and `stdout+stderr` captured into a `String`. -/
def withIsolatedStreams (x : IO α) : IO (String × Except IO.Error α) := do
let bIn ← mkRef { : Stream.Buffer }
let bOut ← mkRef { : Stream.Buffer }
let r ← withStdin (Stream.ofBuffer bIn) <|
withStdout (Stream.ofBuffer bOut) <|
withStderr (Stream.ofBuffer bOut) <|
observing x
let bOut ← bOut.get
let out := String.fromUTF8Unchecked bOut.data
pure (out, r)
end FS
end IO
universe u
namespace Lean
/-- Typeclass used for presenting the output of an `#eval` command. -/
class Eval (α : Type u) where
-- We default `hideUnit` to `true`, but set it to `false` in the direct call from `#eval`
-- so that `()` output is hidden in chained instances such as for some `m Unit`.
-- We take `Unit → α` instead of `α` because ‵α` may contain effectful debugging primitives (e.g., `dbg_trace`)
eval : (Unit → α) → forall (hideUnit : optParam Bool true), IO Unit
instance [ToString α] : Eval α :=
⟨fun a _ => IO.println (toString (a ()))⟩
instance [Repr α] : Eval α :=
⟨fun a _ => IO.println (repr (a ()))⟩
instance : Eval Unit :=
⟨fun u hideUnit => if hideUnit then pure () else IO.println (repr (u ()))⟩
instance [Eval α] : Eval (IO α) :=
⟨fun x _ => do let a ← x (); Eval.eval (fun _ => a)⟩
@[noinline, nospecialize] def runEval [Eval α] (a : Unit → α) : IO (String × Except IO.Error Unit) :=
IO.FS.withIsolatedStreams (Eval.eval a false)
end Lean
syntax "println! " (interpolatedStr(term) <|> term) : term
macro_rules
| `(println! $msg) =>
if msg.getKind == Lean.interpolatedStrKind then
`((IO.println (s! $msg) : IO Unit))
else
`((IO.println $msg : IO Unit))
|
92a0bdcda5b8d0c8cbc60b5c1b2405bbc4c71e26 | 0402379982b540b88793299fdc0f0183ed6436df | /src/200316_cav.lean | 4d1bd8037dcbaf2fce913b35b569f0f29e5573da | [
"MIT"
] | permissive | teodorov/lean-scheduling | 12d0aa8471aea5216e49315181e3b5a113305ebc | 57e047e0b11f7d5b075ab512fb3d4f9cec4abf68 | refs/heads/master | 1,616,898,774,461 | 1,585,170,165,000 | 1,585,170,165,000 | 247,828,727 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,356 | lean | import data.set.finite data.stream
namespace gtr_cav
open set
--open byte
variables
{C : Type} -- configurations
{A : Type} -- actions
{R : Type} -- results, events, transition payload
{P : Type} -- atomic propositions
-- linear blocking semantic transition relation
structure iLSTR :=
(initial : C)
(actions : C → A)
(execute : C → A → option (R × C))
(one_action : ∀ c, ∃! a, actions c = a)
(determinist_exe: ∀ c a, ∃! e t, execute c a = some (e, t))
inductive sos_exe : @iLSTR C A R → C → C → Prop
notation ⟨ l, ρ₀ ⟩ ` -x→ ` ρ₁ := sos_exe l ρ₀ ρ₁
| step : ∀ (l:@iLSTR C A R) e ρ₀ ρ₁, l.execute ρ₀ (l.actions ρ₀) = some (e, ρ₁)
→ -----------------------------
⟨ l, ρ₀ ⟩ -x→ ρ₁
-- deterministic execution semantic transition relation
structure iDESTR :=
(initial : C)
(actions : C → set A)
(execute : C → A → R × C)
(determinist_exe : ∀ c a, ∃! e t, execute c a = (e, t))
-- blocking deterministic semantic transition relation
structure iBDSTR :=
(initial : C)
(actions : C → set A)
(execute : C → A → option (R × C))
-- general semantic transition relation
structure iSTR :=
(initial : set C)
(actions : C → set A)
(execute : C → A → set (R × C))
--(evaluate: C → A → R → C → bool)
def iDSTR2iSTR (l : @iDESTR C A R) : @iSTR C A R :=
⟨
singleton(l.initial),
l.actions,
λ c a, singleton(l.execute c a)
⟩
def iBDSTR2iSTR (l : @iBDSTR C A R) : @iSTR C A R :=
⟨
singleton(l.initial),
l.actions,
λ c a,
match (l.execute c a) with
| none := ∅
| some r := singleton(r)
end
⟩
def synchronous_product {C₁ C₂ A₁ A₂ R₁ R₂ L₁ L₂ : Type}
(lhs : @iSTR C₁ A₁ R₁)
(eval₁ : L₁ → C₁ → A₁ → R₁ → C₁ → bool)
(rhs : @iSTR C₂ A₂ R₂)
(eval₂ : L₂ → C₂ → A₂ → R₂ → C₂ → bool)
(mapping : set (L₁ × L₂))
: @iSTR (C₁×C₂) ((A₁ × A₂) × (R₁ × R₂) × (C₁ × C₂)) ( R₁ × R₂ ) :=
⟨
{ c | ∀ (c₁ ∈ lhs.initial) (c₂ ∈ rhs.initial), c = (c₁, c₂) },
λ c, { a |
match c with
| (c₁, c₂) :=
∀ (a₁ ∈ lhs.actions c₁) (a₂ ∈ rhs.actions c₂),
∀ (rc₁ ∈ lhs.execute c₁ a₁) (rc₂ ∈ rhs.execute c₂ a₂),
∃ m ∈ mapping,
match rc₁, rc₂, m with
| (r₁, t₁), (r₂, t₂), (l₁, l₂) :=
eval₁ l₁ c₁ a₁ r₁ t₁ = tt
∧ eval₂ l₂ c₂ a₂ r₂ t₂ = tt
→ --------------------------------
a = ((a₁, a₂), (r₁, r₂), (c₁, c₂))
end
end
},
λ c a, match a with | (_, rc) := singleton rc end
⟩
--For model-checking we need next stream with is_final
--High-level interface
class iExplicitNextStream :=
(initial : finset C)
(next : C → finset C)
(is_final : C → bool)
def iSTR2iExplicitNextStream
(l : @iSTR C A R) -- the semantic transition relation
(eval : C → bool) -- the configuration evaluation function, decide if a configuration is final
[fic : finite l.initial] -- a proof that the STR has finitely many initial states
[fia : ∀ c, finite (l.actions c)] -- a proof that for all configurations the STR has finitely many action
[fie : ∀ c a, finite (l.execute c a)] -- a proof that for all configuration and action the STR produces finitely many targets
: @iExplicitNextStream C :=
⟨
(@finite.to_finset _ l.initial fic),
λ c, (@finite.to_finset _ ({ x | ∀ a ∈ l.actions c, ∀ rc ∈ l.execute c a, x = (prod.snd rc)}) sorry),
eval
⟩
-- a statefull scheduler, carry state over the choice operation
-- it has some arbitrary initial state
structure scheduler {S : Type} :=
(initial : S)
(choice : S → C → set A → (S × A))
def stateless_scheduler := @scheduler unit
def scheduling_filter { S : Type}
(l : @iSTR C A R)
(p : @scheduler C A S)
: @iSTR (C × S) (S × A) R :=
⟨
{cs | ∀ c ∈ l.initial, cs = (c, p.initial)}, --- .initial
λ cs, singleton (p.choice (prod.snd cs) (prod.fst cs) (l.actions (prod.fst cs))), --.actions
λ cs sa,
let
r := l.execute (prod.fst cs) (prod.snd sa)
in
{x | ∀ ec ∈ r, x = ((prod.fst ec), ((prod.snd ec), (prod.snd cs)))} --.execute
⟩
--stateless filtering policies
structure filtering_policy :=
(apply : C → set A → set A)
(subset : ∀ c aSet, apply c aSet ⊆ aSet)
def filter
(l : @iSTR C A R)
(filter : @filtering_policy C A)
: @iSTR C A R :=
⟨
l.initial,
λ c, filter.apply c (l.actions c),
l.execute
⟩
def splitter -- named dispatcher previously
(actions : set A)
(selector : A → bool)
: set A × set A :=
(
{a ∈ actions | selector a = tt},
{a ∈ actions | selector a = ff}
)
def merger(a₁ a₂ : set A) : set A := a₁ ∪ a₂
end gtr_cav |
25489ca9f79f9e638ff7f37e80c75c67558b685e | c777c32c8e484e195053731103c5e52af26a25d1 | /src/linear_algebra/matrix/to_linear_equiv.lean | 18fe1120a77b271786e696df42ce4fc001540baf | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 7,163 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen
-/
import linear_algebra.finite_dimensional
import linear_algebra.matrix.general_linear_group
import linear_algebra.matrix.nondegenerate
import linear_algebra.matrix.nonsingular_inverse
import linear_algebra.matrix.to_lin
import ring_theory.localization.fraction_ring
import ring_theory.localization.integer
/-!
# Matrices and linear equivalences
This file gives the map `matrix.to_linear_equiv` from matrices with invertible determinant,
to linear equivs.
## Main definitions
* `matrix.to_linear_equiv`: a matrix with an invertible determinant forms a linear equiv
## Main results
* `matrix.exists_mul_vec_eq_zero_iff`: `M` maps some `v ≠ 0` to zero iff `det M = 0`
## Tags
matrix, linear_equiv, determinant, inverse
-/
namespace matrix
open linear_map
variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M]
variables {n : Type*} [fintype n]
section to_linear_equiv'
variables [decidable_eq n]
/-- An invertible matrix yields a linear equivalence from the free module to itself.
See `matrix.to_linear_equiv` for the same map on arbitrary modules.
-/
def to_linear_equiv' (P : matrix n n R) (h : invertible P) : (n → R) ≃ₗ[R] (n → R) :=
general_linear_group.general_linear_equiv _ _ $
matrix.general_linear_group.to_linear $ unit_of_invertible P
@[simp] lemma to_linear_equiv'_apply (P : matrix n n R) (h : invertible P) :
(↑(P.to_linear_equiv' h) : module.End R (n → R)) = P.to_lin' := rfl
@[simp] lemma to_linear_equiv'_symm_apply (P : matrix n n R) (h : invertible P) :
(↑(P.to_linear_equiv' h).symm : module.End R (n → R)) = (⅟P).to_lin' :=
rfl
end to_linear_equiv'
section to_linear_equiv
variables (b : basis n R M)
include b
/-- Given `hA : is_unit A.det` and `b : basis R b`, `A.to_linear_equiv b hA` is
the `linear_equiv` arising from `to_lin b b A`.
See `matrix.to_linear_equiv'` for this result on `n → R`.
-/
@[simps apply]
noncomputable def to_linear_equiv [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
M ≃ₗ[R] M :=
begin
refine
{ to_fun := to_lin b b A,
inv_fun := to_lin b b A⁻¹,
left_inv := λ x, _,
right_inv := λ x, _,
.. to_lin b b A };
rw ← linear_map.comp_apply;
simp only [← matrix.to_lin_mul b b b,
matrix.nonsing_inv_mul _ hA, matrix.mul_nonsing_inv _ hA,
to_lin_one, linear_map.id_apply]
end
lemma ker_to_lin_eq_bot [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
(to_lin b b A).ker = ⊥ :=
ker_eq_bot.mpr (to_linear_equiv b A hA).injective
lemma range_to_lin_eq_top [decidable_eq n] (A : matrix n n R) (hA : is_unit A.det) :
(to_lin b b A).range = ⊤ :=
range_eq_top.mpr (to_linear_equiv b A hA).surjective
end to_linear_equiv
section nondegenerate
open_locale matrix
/-- This holds for all integral domains (see `matrix.exists_mul_vec_eq_zero_iff`),
not just fields, but it's easier to prove it for the field of fractions first. -/
lemma exists_mul_vec_eq_zero_iff_aux {K : Type*} [decidable_eq n] [field K] {M : matrix n n K} :
(∃ (v ≠ 0), M.mul_vec v = 0) ↔ M.det = 0 :=
begin
split,
{ rintros ⟨v, hv, mul_eq⟩,
contrapose! hv,
exact eq_zero_of_mul_vec_eq_zero hv mul_eq },
{ contrapose!,
intros h,
have : function.injective M.to_lin',
{ simpa only [← linear_map.ker_eq_bot, ker_to_lin'_eq_bot_iff, not_imp_not] using h },
have : M ⬝ linear_map.to_matrix'
((linear_equiv.of_injective_endo M.to_lin' this).symm : (n → K) →ₗ[K] (n → K)) = 1,
{ refine matrix.to_lin'.injective (linear_map.ext $ λ v, _),
rw [matrix.to_lin'_mul, matrix.to_lin'_one, matrix.to_lin'_to_matrix', linear_map.comp_apply],
exact (linear_equiv.of_injective_endo M.to_lin' this).apply_symm_apply v },
exact matrix.det_ne_zero_of_right_inverse this }
end
lemma exists_mul_vec_eq_zero_iff' {A : Type*} (K : Type*) [decidable_eq n]
[comm_ring A] [nontrivial A]
[field K] [algebra A K] [is_fraction_ring A K]
{M : matrix n n A} :
(∃ (v ≠ 0), M.mul_vec v = 0) ↔ M.det = 0 :=
begin
have : (∃ (v ≠ 0), mul_vec ((algebra_map A K).map_matrix M) v = 0) ↔ _ :=
exists_mul_vec_eq_zero_iff_aux,
rw [← ring_hom.map_det, is_fraction_ring.to_map_eq_zero_iff] at this,
refine iff.trans _ this, split; rintro ⟨v, hv, mul_eq⟩,
{ refine ⟨λ i, algebra_map _ _ (v i), mt (λ h, funext $ λ i, _) hv, _⟩,
{ exact is_fraction_ring.to_map_eq_zero_iff.mp (congr_fun h i) },
{ ext i,
refine (ring_hom.map_mul_vec _ _ _ i).symm.trans _,
rw [mul_eq, pi.zero_apply, ring_hom.map_zero, pi.zero_apply] } },
{ letI := classical.dec_eq K,
obtain ⟨⟨b, hb⟩, ba_eq⟩ := is_localization.exist_integer_multiples_of_finset
(non_zero_divisors A) (finset.univ.image v),
choose f hf using ba_eq,
refine ⟨λ i, f _ (finset.mem_image.mpr ⟨i, finset.mem_univ i, rfl⟩),
mt (λ h, funext $ λ i, _) hv, _⟩,
{ have := congr_arg (algebra_map A K) (congr_fun h i),
rw [hf, subtype.coe_mk, pi.zero_apply, ring_hom.map_zero, algebra.smul_def,
mul_eq_zero, is_fraction_ring.to_map_eq_zero_iff] at this,
exact this.resolve_left (non_zero_divisors.ne_zero hb), },
{ ext i,
refine is_fraction_ring.injective A K _,
calc algebra_map A K (M.mul_vec (λ (i : n), f (v i) _) i)
= ((algebra_map A K).map_matrix M).mul_vec
(algebra_map _ K b • v) i : _
... = 0 : _
... = algebra_map A K 0 : (ring_hom.map_zero _).symm,
{ simp_rw [ring_hom.map_mul_vec, mul_vec, dot_product, function.comp_app, hf,
subtype.coe_mk, ring_hom.map_matrix_apply, pi.smul_apply, smul_eq_mul,
algebra.smul_def] },
{ rw [mul_vec_smul, mul_eq, pi.smul_apply, pi.zero_apply, smul_zero] } } },
end
lemma exists_mul_vec_eq_zero_iff {A : Type*} [decidable_eq n] [comm_ring A] [is_domain A]
{M : matrix n n A} :
(∃ (v ≠ 0), M.mul_vec v = 0) ↔ M.det = 0 :=
exists_mul_vec_eq_zero_iff' (fraction_ring A)
lemma exists_vec_mul_eq_zero_iff {A : Type*} [decidable_eq n] [comm_ring A] [is_domain A]
{M : matrix n n A} :
(∃ (v ≠ 0), M.vec_mul v = 0) ↔ M.det = 0 :=
by simpa only [← M.det_transpose, ← mul_vec_transpose] using exists_mul_vec_eq_zero_iff
theorem nondegenerate_iff_det_ne_zero {A : Type*} [decidable_eq n] [comm_ring A] [is_domain A]
{M : matrix n n A} :
nondegenerate M ↔ M.det ≠ 0 :=
begin
refine iff.trans _ (not_iff_not.mpr exists_vec_mul_eq_zero_iff),
simp only [not_exists],
split,
{ intros hM v hv hMv,
obtain ⟨w, hwMv⟩ := hM.exists_not_ortho_of_ne_zero hv,
simpa only [dot_product_mul_vec, hMv, zero_dot_product] using hwMv },
{ intros h v hv,
refine not_imp_not.mp (h v) (funext $ λ i, _),
simpa only [dot_product_mul_vec, dot_product_single, mul_one] using hv (pi.single i 1) }
end
alias nondegenerate_iff_det_ne_zero ↔ nondegenerate.det_ne_zero nondegenerate.of_det_ne_zero
end nondegenerate
end matrix
|
4993d9ae3512cc8743cd488e3290bd4de48cdfa8 | 7565ffb53cc64430691ce89265da0f944ee43051 | /hott/types/nat/hott.hlean | 874883dbcdf98f604bc9b0097605c0dc85c705d9 | [
"Apache-2.0"
] | permissive | EgbertRijke/lean2 | cacddba3d150f8b38688e044960a208bf851f90e | 519dcee739fbca5a4ab77d66db7652097b4604cd | refs/heads/master | 1,606,936,954,854 | 1,498,836,083,000 | 1,498,910,882,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,597 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Theorems about the natural numbers specific to HoTT
-/
import .order types.pointed .sub
open is_trunc unit empty eq equiv algebra pointed is_equiv equiv function
namespace nat
definition is_prop_le [instance] (n m : ℕ) : is_prop (n ≤ m) :=
begin
have lem : Π{n m : ℕ} (p : n ≤ m) (q : n = m), p = q ▸ le.refl n,
begin
intros, cases p,
{ have H' : q = idp, by apply is_set.elim,
cases H', reflexivity},
{ cases q, exfalso, apply not_succ_le_self a}
end,
apply is_prop.mk, intro H1 H2, induction H2,
{ apply lem},
{ cases H1,
{ exfalso, apply not_succ_le_self a},
{ exact ap le.step !v_0}},
end
definition is_prop_lt [instance] (n m : ℕ) : is_prop (n < m) := !is_prop_le
definition le_equiv_succ_le_succ (n m : ℕ) : (n ≤ m) ≃ (succ n ≤ succ m) :=
equiv_of_is_prop succ_le_succ le_of_succ_le_succ
definition le_succ_equiv_pred_le (n m : ℕ) : (n ≤ succ m) ≃ (pred n ≤ m) :=
equiv_of_is_prop pred_le_pred le_succ_of_pred_le
theorem lt_by_cases_lt {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P)
(H3 : a > b → P) (H : a < b) : lt.by_cases H1 H2 H3 = H1 H :=
begin
unfold lt.by_cases, induction (lt.trichotomy a b) with H' H',
{ esimp, exact ap H1 !is_prop.elim},
{ exfalso, cases H' with H' H', apply lt.irrefl, exact H' ▸ H, exact lt.asymm H H'}
end
theorem lt_by_cases_eq {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P)
(H3 : a > b → P) (H : a = b) : lt.by_cases H1 H2 H3 = H2 H :=
begin
unfold lt.by_cases, induction (lt.trichotomy a b) with H' H',
{ exfalso, apply lt.irrefl, exact H ▸ H'},
{ cases H' with H' H', esimp, exact ap H2 !is_prop.elim, exfalso, apply lt.irrefl, exact H ▸ H'}
end
theorem lt_by_cases_ge {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P)
(H3 : a > b → P) (H : a > b) : lt.by_cases H1 H2 H3 = H3 H :=
begin
unfold lt.by_cases, induction (lt.trichotomy a b) with H' H',
{ exfalso, exact lt.asymm H H'},
{ cases H' with H' H', exfalso, apply lt.irrefl, exact H' ▸ H, esimp, exact ap H3 !is_prop.elim}
end
theorem lt_ge_by_cases_lt {n m : ℕ} {P : Type} (H1 : n < m → P) (H2 : n ≥ m → P)
(H : n < m) : lt_ge_by_cases H1 H2 = H1 H :=
by apply lt_by_cases_lt
theorem lt_ge_by_cases_ge {n m : ℕ} {P : Type} (H1 : n < m → P) (H2 : n ≥ m → P)
(H : n ≥ m) : lt_ge_by_cases H1 H2 = H2 H :=
begin
unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H',
{ exfalso, apply lt.irrefl, exact lt_of_le_of_lt H H'},
{ cases H' with H' H'; all_goals (esimp; apply ap H2 !is_prop.elim)}
end
theorem lt_ge_by_cases_le {n m : ℕ} {P : Type} {H1 : n ≤ m → P} {H2 : n ≥ m → P}
(H : n ≤ m) (Heq : Π(p : n = m), H1 (le_of_eq p) = H2 (le_of_eq p⁻¹))
: lt_ge_by_cases (λH', H1 (le_of_lt H')) H2 = H1 H :=
begin
unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H',
{ esimp, apply ap H1 !is_prop.elim},
{ cases H' with H' H',
{ esimp, induction H', esimp, symmetry,
exact ap H1 !is_prop.elim ⬝ Heq idp ⬝ ap H2 !is_prop.elim},
{ exfalso, apply lt.irrefl, apply lt_of_le_of_lt H H'}}
end
protected definition code [reducible] [unfold 1 2] : ℕ → ℕ → Type₀
| code 0 0 := unit
| code 0 (succ m) := empty
| code (succ n) 0 := empty
| code (succ n) (succ m) := code n m
protected definition refl : Πn, nat.code n n
| refl 0 := star
| refl (succ n) := refl n
protected definition encode [unfold 3] {n m : ℕ} (p : n = m) : nat.code n m :=
p ▸ nat.refl n
protected definition decode : Π(n m : ℕ), nat.code n m → n = m
| decode 0 0 := λc, idp
| decode 0 (succ l) := λc, empty.elim c _
| decode (succ k) 0 := λc, empty.elim c _
| decode (succ k) (succ l) := λc, ap succ (decode k l c)
definition nat_eq_equiv (n m : ℕ) : (n = m) ≃ nat.code n m :=
equiv.MK nat.encode
!nat.decode
begin
revert m, induction n, all_goals (intro m;induction m;all_goals intro c),
all_goals try contradiction,
induction c, reflexivity,
xrewrite [↑nat.decode,-tr_compose,v_0],
end
begin
intro p, induction p, esimp, induction n,
reflexivity,
rewrite [↑nat.decode,↑nat.refl,v_0]
end
definition pointed_nat [instance] [constructor] : pointed ℕ :=
pointed.mk 0
open sigma sum
definition eq_even_or_eq_odd (n : ℕ) : (Σk, 2 * k = n) ⊎ (Σk, 2 * k + 1 = n) :=
begin
induction n with n IH,
{ exact inl ⟨0, idp⟩},
{ induction IH with H H: induction H with k p: induction p,
{ exact inr ⟨k, idp⟩},
{ refine inl ⟨k+1, idp⟩}}
end
definition rec_on_even_odd {P : ℕ → Type} (n : ℕ) (H : Πk, P (2 * k)) (H2 : Πk, P (2 * k + 1))
: P n :=
begin
cases eq_even_or_eq_odd n with v v: induction v with k p: induction p,
{ exact H k},
{ exact H2 k}
end
/- this inequality comes up a couple of times when using the freudenthal suspension theorem -/
definition add_mul_le_mul_add (n m k : ℕ) : n + (succ m) * k ≤ (succ m) * (n + k) :=
calc
n + (succ m) * k ≤ (m * n + n) + (succ m) * k : add_le_add_right !le_add_left _
... = (succ m) * n + (succ m) * k : by rewrite -succ_mul
... = (succ m) * (n + k) : !left_distrib⁻¹
/-
Some operations work only for successors. For example fin (succ n) has a 0 and a 1, but fin 0
doesn't. However, we want a bit more, because sometimes we want a zero of (fin a)
where a is either
- equal to a successor, but not definitionally a successor (e.g. (0 : fin (3 + n)))
- definitionally equal to a successor, but not in a way that type class inference can infer.
(e.g. (0 : fin 4). Note that 4 is bit0 (bit0 one), but (bit0 x) (defined as x + x),
is not always a successor)
To solve this we use an auxillary class `is_succ` which can solve whether a number is a
successor.
-/
inductive is_succ [class] : ℕ → Type :=
| mk : Π(n : ℕ), is_succ (succ n)
attribute is_succ.mk [instance]
definition is_succ_add_right [instance] [constructor] (n m : ℕ) [H : is_succ m] : is_succ (n+m) :=
by induction H with m; constructor
definition is_succ_add_left [instance] [constructor] (n m : ℕ) [H : is_succ n] : is_succ (n+m) :=
by induction H with n; cases m with m: constructor
definition is_succ_bit0 [constructor] (n : ℕ) [H : is_succ n] : is_succ (bit0 n) :=
by exact _
-- level 2 is useful for abelian homotopy groups, which only exist at level 2 and higher
inductive is_at_least_two [class] : ℕ → Type :=
| mk : Π(n : ℕ), is_at_least_two (succ (succ n))
attribute is_at_least_two.mk [instance]
definition is_at_least_two_add_right [instance] [constructor] (n m : ℕ) [H : is_at_least_two m] :
is_at_least_two (n+m) :=
by induction H with m; constructor
definition is_at_least_two_add_left [instance] [constructor] (n m : ℕ) [H : is_at_least_two n] :
is_at_least_two (n+m) :=
by induction H with n; cases m with m: try cases m with m: constructor
definition is_at_least_two_add_both [instance] [priority 900] [constructor] (n m : ℕ)
[H : is_succ n] [K : is_succ m] : is_at_least_two (n+m) :=
by induction H with n; induction K with m; cases m with m: constructor
definition is_at_least_two_bit0 [constructor] (n : ℕ) [H : is_succ n] : is_at_least_two (bit0 n) :=
by exact _
definition is_at_least_two_bit1 [constructor] (n : ℕ) [H : is_succ n] : is_at_least_two (bit1 n) :=
by exact _
/- some facts about iterate -/
definition iterate_succ {A : Type} (f : A → A) (n : ℕ) (x : A) :
f^[succ n] x = f^[n] (f x) :=
by induction n with n p; reflexivity; exact ap f p
lemma iterate_sub {A : Type} (f : A ≃ A) {n m : ℕ} (h : n ≥ m) (a : A) :
iterate f (n - m) a = iterate f n (iterate f⁻¹ m a) :=
begin
revert n h, induction m with m p: intro n h,
{ reflexivity },
{ cases n with n, exfalso, apply not_succ_le_zero _ h,
rewrite [succ_sub_succ], refine p n (le_of_succ_le_succ h) ⬝ _,
refine ap (f^[n]) _ ⬝ !iterate_succ⁻¹, exact !to_right_inv⁻¹ }
end
definition iterate_commute {A : Type} {f g : A → A} (n : ℕ) (h : f ∘ g ~ g ∘ f) :
iterate f n ∘ g ~ g ∘ iterate f n :=
by induction n with n IH; reflexivity; exact λx, ap f (IH x) ⬝ !h
definition iterate_equiv {A : Type} (f : A ≃ A) (n : ℕ) : A ≃ A :=
equiv.mk (iterate f n)
(by induction n with n IH; apply is_equiv_id; exact is_equiv_compose f (iterate f n))
definition iterate_inv {A : Type} (f : A ≃ A) (n : ℕ) :
(iterate_equiv f n)⁻¹ ~ iterate f⁻¹ n :=
begin
induction n with n p: intro a,
reflexivity,
exact p (f⁻¹ a) ⬝ !iterate_succ⁻¹
end
definition iterate_left_inv {A : Type} (f : A ≃ A) (n : ℕ) (a : A) : f⁻¹ᵉ^[n] (f^[n] a) = a :=
(iterate_inv f n (f^[n] a))⁻¹ ⬝ to_left_inv (iterate_equiv f n) a
definition iterate_right_inv {A : Type} (f : A ≃ A) (n : ℕ) (a : A) : f^[n] (f⁻¹ᵉ^[n] a) = a :=
ap (f^[n]) (iterate_inv f n a)⁻¹ ⬝ to_right_inv (iterate_equiv f n) a
end nat
|
72af1520b28fa5d750e2508c1bb6824f9ce1bb9e | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/continued_fractions/computation/translations.lean | 063ea8bcc899345fd0da30b0d632375272a5537a | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 10,501 | lean | /-
Copyright (c) 2020 Kevin Kappelmann. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import algebra.continued_fractions.computation.basic
import algebra.continued_fractions.translations
/-!
# Basic Translation Lemmas Between Structures Defined for Computing Continued Fractions
## Summary
This is a collection of simple lemmas between the different structures used for the computation
of continued fractions defined in `algebra.continued_fractions.computation.basic`. The file consists
of three sections:
1. Recurrences and inversion lemmas for `int_fract_pair.stream`: these lemmas give us inversion
rules and recurrences for the computation of the stream of integer and fractional parts of
a value.
2. Translation lemmas for the head term: these lemmas show us that the head term of the computed
continued fraction of a value `v` is `⌊v⌋` and how this head term is moved along the structures
used in the computation process.
3. Translation lemmas for the sequence: these lemmas show how the sequences of the involved
structures (`int_fract_pair.stream`, `int_fract_pair.seq1`, and
`generalized_continued_fraction.of`) are connected, i.e. how the values are moved along the
structures and the termination of one sequence implies the termination of another sequence.
## Main Theorems
- `succ_nth_stream_eq_some_iff` gives as a recurrence to compute the `n + 1`th value of the sequence
of integer and fractional parts of a value in case of non-termination.
- `succ_nth_stream_eq_none_iff` gives as a recurrence to compute the `n + 1`th value of the sequence
of integer and fractional parts of a value in case of termination.
- `nth_of_eq_some_of_succ_nth_int_fract_pair_stream` and
`nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero` show how the entries of the sequence
of the computed continued fraction can be obtained from the stream of integer and fractional
parts.
-/
namespace generalized_continued_fraction
open generalized_continued_fraction as gcf
/- Fix a discrete linear ordered floor field and a value `v`. -/
variables {K : Type*} [linear_ordered_field K] [floor_ring K] {v : K}
namespace int_fract_pair
/-!
### Recurrences and Inversion Lemmas for `int_fract_pair.stream`
Here we state some lemmas that give us inversion rules and recurrences for the computation of the
stream of integer and fractional parts of a value.
-/
variable {n : ℕ}
lemma stream_eq_none_of_fr_eq_zero {ifp_n : int_fract_pair K}
(stream_nth_eq : int_fract_pair.stream v n = some ifp_n) (nth_fr_eq_zero : ifp_n.fr = 0) :
int_fract_pair.stream v (n + 1) = none :=
begin
cases ifp_n with _ fr,
change fr = 0 at nth_fr_eq_zero,
simp [int_fract_pair.stream, stream_nth_eq, nth_fr_eq_zero]
end
/--
Gives a recurrence to compute the `n + 1`th value of the sequence of integer and fractional
parts of a value in case of termination.
-/
lemma succ_nth_stream_eq_none_iff : int_fract_pair.stream v (n + 1) = none
↔ (int_fract_pair.stream v n = none ∨ ∃ ifp, int_fract_pair.stream v n = some ifp ∧ ifp.fr = 0) :=
begin
cases stream_nth_eq : (int_fract_pair.stream v n) with ifp,
case option.none : { simp [stream_nth_eq, int_fract_pair.stream] },
case option.some :
{ cases ifp with _ fr,
cases decidable.em (fr = 0);
finish [int_fract_pair.stream] }
end
/--
Gives a recurrence to compute the `n + 1`th value of the sequence of integer and fractional
parts of a value in case of non-termination.
-/
lemma succ_nth_stream_eq_some_iff {ifp_succ_n : int_fract_pair K} :
int_fract_pair.stream v (n + 1) = some ifp_succ_n
↔ ∃ (ifp_n : int_fract_pair K), int_fract_pair.stream v n = some ifp_n
∧ ifp_n.fr ≠ 0
∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n :=
begin
split,
{ assume stream_succ_nth_eq,
have : int_fract_pair.stream v (n + 1) ≠ none, by simp [stream_succ_nth_eq],
have : ¬int_fract_pair.stream v n = none
∧ ¬(∃ ifp, int_fract_pair.stream v n = some ifp ∧ ifp.fr = 0), by
{ have not_none_not_fract_zero,
from (not_iff_not_of_iff succ_nth_stream_eq_none_iff).elim_left this,
exact (not_or_distrib.elim_left not_none_not_fract_zero) },
cases this with stream_nth_ne_none nth_fr_ne_zero,
replace nth_fr_ne_zero : ∀ ifp, int_fract_pair.stream v n = some ifp → ifp.fr ≠ 0, by
simpa using nth_fr_ne_zero,
obtain ⟨ifp_n, stream_nth_eq⟩ : ∃ ifp_n, int_fract_pair.stream v n = some ifp_n, from
option.ne_none_iff_exists'.mp stream_nth_ne_none,
existsi ifp_n,
have ifp_n_fr_ne_zero : ifp_n.fr ≠ 0, from nth_fr_ne_zero ifp_n stream_nth_eq,
cases ifp_n with _ ifp_n_fr,
suffices : int_fract_pair.of ifp_n_fr⁻¹ = ifp_succ_n,
by simpa [stream_nth_eq, ifp_n_fr_ne_zero],
simp only [int_fract_pair.stream, stream_nth_eq, ifp_n_fr_ne_zero, option.some_bind, if_false]
at stream_succ_nth_eq,
injection stream_succ_nth_eq },
{ rintro ⟨⟨_⟩, ifp_n_props⟩, finish [int_fract_pair.stream, ifp_n_props] }
end
lemma exists_succ_nth_stream_of_fr_zero {ifp_succ_n : int_fract_pair K}
(stream_succ_nth_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n)
(succ_nth_fr_eq_zero : ifp_succ_n.fr = 0) :
∃ ifp_n : int_fract_pair K, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr⁻¹ = ⌊ifp_n.fr⁻¹⌋ :=
begin
-- get the witness from `succ_nth_stream_eq_some_iff` and prove that it has the additional
-- properties
rcases (succ_nth_stream_eq_some_iff.elim_left stream_succ_nth_eq) with
⟨ifp_n, stream_nth_eq, nth_fr_ne_zero, _⟩,
existsi ifp_n,
cases ifp_n with _ ifp_n_fr,
suffices : ifp_n_fr⁻¹ = ⌊ifp_n_fr⁻¹⌋, by simpa [stream_nth_eq],
have : int_fract_pair.of ifp_n_fr⁻¹ = ifp_succ_n, by finish,
cases ifp_succ_n with _ ifp_succ_n_fr,
change ifp_succ_n_fr = 0 at succ_nth_fr_eq_zero,
have : fract ifp_n_fr⁻¹ = ifp_succ_n_fr, by injection this,
have : fract ifp_n_fr⁻¹ = 0, by rwa [succ_nth_fr_eq_zero] at this,
calc
ifp_n_fr⁻¹ = fract ifp_n_fr⁻¹ + ⌊ifp_n_fr⁻¹⌋ : by rw (fract_add_floor ifp_n_fr⁻¹)
... = ⌊ifp_n_fr⁻¹⌋ : by simp [‹fract ifp_n_fr⁻¹ = 0›]
end
end int_fract_pair
section head
/-!
### Translation of the Head Term
Here we state some lemmas that show us that the head term of the computed continued fraction of a
value `v` is `⌊v⌋` and how this head term is moved along the structures used in the computation
process.
-/
/-- The head term of the sequence with head of `v` is just the integer part of `v`. -/
@[simp]
lemma int_fract_pair.seq1_fst_eq_of : (int_fract_pair.seq1 v).fst = int_fract_pair.of v := rfl
lemma of_h_eq_int_fract_pair_seq1_fst_b : (gcf.of v).h = (int_fract_pair.seq1 v).fst.b :=
by { cases aux_seq_eq : (int_fract_pair.seq1 v), simp [gcf.of, aux_seq_eq] }
/-- The head term of the gcf of `v` is `⌊v⌋`. -/
@[simp]
lemma of_h_eq_floor : (gcf.of v).h = ⌊v⌋ :=
by simp [of_h_eq_int_fract_pair_seq1_fst_b, int_fract_pair.of]
end head
section sequence
/-!
### Translation of the Sequences
Here we state some lemmas that show how the sequences of the involved structures
(`int_fract_pair.stream`, `int_fract_pair.seq1`, and `generalized_continued_fraction.of`) are
connected, i.e. how the values are moved along the structures and how the termination of one
sequence implies the termination of another sequence.
-/
variable {n : ℕ}
lemma int_fract_pair.nth_seq1_eq_succ_nth_stream :
(int_fract_pair.seq1 v).snd.nth n = (int_fract_pair.stream v) (n + 1) := rfl
section termination
/-!
#### Translation of the Termination of the Sequences
Let's first show how the termination of one sequence implies the termination of another sequence.
-/
lemma of_terminated_at_iff_int_fract_pair_seq1_terminated_at :
(gcf.of v).terminated_at n ↔ (int_fract_pair.seq1 v).snd.terminated_at n :=
begin
rw [gcf.terminated_at_iff_s_none, gcf.of],
rcases (int_fract_pair.seq1 v) with ⟨head, ⟨st⟩⟩,
cases st_n_eq : st n;
simp [gcf.of, st_n_eq, seq.map, seq.nth, stream.map, seq.terminated_at, stream.nth]
end
lemma of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none :
(gcf.of v).terminated_at n ↔ int_fract_pair.stream v (n + 1) = none :=
by rw [of_terminated_at_iff_int_fract_pair_seq1_terminated_at, seq.terminated_at,
int_fract_pair.nth_seq1_eq_succ_nth_stream]
end termination
section values
/-!
#### Translation of the Values of the Sequence
Now let's show how the values of the sequences correspond to one another.
-/
lemma int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some {gp_n : gcf.pair K}
(s_nth_eq : (gcf.of v).s.nth n = some gp_n) :
∃ (ifp : int_fract_pair K), int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b :=
begin
obtain ⟨ifp, stream_succ_nth_eq, gp_n_eq⟩ :
∃ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ gcf.pair.mk 1 (ifp.b : K) = gp_n, by
{ unfold gcf.of int_fract_pair.seq1 at s_nth_eq,
rwa [seq.map_tail, seq.nth_tail, seq.map_nth, option.map_eq_some'] at s_nth_eq },
cases gp_n_eq,
injection gp_n_eq with _ ifp_b_eq_gp_n_b,
existsi ifp,
exact ⟨stream_succ_nth_eq, ifp_b_eq_gp_n_b⟩
end
/--
Shows how the entries of the sequence of the computed continued fraction can be obtained by the
integer parts of the stream of integer and fractional parts.
-/
lemma nth_of_eq_some_of_succ_nth_int_fract_pair_stream {ifp_succ_n : int_fract_pair K}
(stream_succ_nth_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) :
(gcf.of v).s.nth n = some ⟨1, ifp_succ_n.b⟩ :=
begin
unfold gcf.of int_fract_pair.seq1,
rw [seq.map_tail, seq.nth_tail, seq.map_nth],
simp [seq.nth, stream_succ_nth_eq]
end
/--
Shows how the entries of the sequence of the computed continued fraction can be obtained by the
fractional parts of the stream of integer and fractional parts.
-/
lemma nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero {ifp_n : int_fract_pair K}
(stream_nth_eq : int_fract_pair.stream v n = some ifp_n) (nth_fr_ne_zero : ifp_n.fr ≠ 0) :
(gcf.of v).s.nth n = some ⟨1, (int_fract_pair.of ifp_n.fr⁻¹).b⟩ :=
have int_fract_pair.stream v (n + 1) = some (int_fract_pair.of ifp_n.fr⁻¹), by
{ cases ifp_n, simp [int_fract_pair.stream, stream_nth_eq, nth_fr_ne_zero], refl },
nth_of_eq_some_of_succ_nth_int_fract_pair_stream this
end values
end sequence
end generalized_continued_fraction
|
5cf8d2d0fef8bfe9d02d83e129108dd8bbc986c6 | 19e80d9dcdc050ba32a44257170460f6eb2f174b | /bezouts2.lean | 290f30743973ac2fb0898f469933d1545e62fc35 | [] | no_license | petersonbill64/lean | d36d7d93d0f1e4e5618f1426d41567e33a0da6fc | 07b7b69e15785c75ebba6629be4fad92799fd7de | refs/heads/master | 1,609,627,927,456 | 1,438,537,533,000 | 1,438,537,533,000 | 40,084,227 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,668 | lean | import data.nat.div data.nat.sub data.int tools.fake_simplifier data.int logic
open nat int eq.ops well_founded decidable fake_simplifier prod
private definition pair_nat.lt : nat × nat → nat × nat → Prop := measure pr₂
private definition pair_nat.lt.wf : well_founded pair_nat.lt :=
intro_k (measure.wf pr₂) 20
local attribute pair_nat.lt.wf [instance]
local infixl `≺`:50 := pair_nat.lt
private definition gcd.lt.dec (x y₁ : nat) : (succ y₁, x mod succ y₁) ≺ (x, succ y₁) :=
mod_lt (succ_pos y₁)
/- extended gcd definitions and properties, and some utilities -/
definition egcd_rec_f (z : int) : int → int → int × int :=
(λ s t, (t, s - t * z))
definition egcdi.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → (int × int)) → (int × int) :=
prod.cases_on p₁ (λ x y, nat.cases_on y
( λ f, pair 1 0 )
( λ y₁ (f : Π p₂, p₂ ≺ (x, succ y₁) → (int × int)),
let bz := f (succ y₁, x mod succ y₁) !gcd.lt.dec in
prod.cases_on bz
(egcd_rec_f (x div succ y₁))))
definition egcdi (x y : nat) := fix egcdi.F (pair x y)
theorem egcdi_zero_right (x : nat) : egcdi x 0 = pair 1 0 :=
well_founded.fix_eq egcdi.F (x, 0)
theorem egcdi_zero_right_1 (x : nat) : pr₁ (egcdi x 0) = 1 := eq.subst (egcdi_zero_right x) rfl
theorem egcdi_zero_right_2 (x : nat) : pr₂ (egcdi x 0) = 0 := eq.subst (egcdi_zero_right x) rfl
theorem egcdi_succ (x y : nat) : egcdi x (succ y) = prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)) :=
well_founded.fix_eq egcdi.F (x, succ y)
theorem preq {A : Type} : (Π (p : A × A), p = (pr₁ p, pr₂ p))
| (a, b) := have H11 : a = pr₁ (a, b), from rfl,
have H12 : b = pr₂ (a, b), from rfl,
have H21 : pr₁ (a, b) = pr₁ (pr₁ (a, b), pr₂ (a, b)), from rfl,
have H22 : pr₂ (a, b) = pr₂ (pr₁ (a, b), pr₂ (a, b)), from rfl,
have H1 : pr₁ (a, b) = pr₁ (pr₁ (a, b), pr₂ (a, b)), from eq.trans H11 H21,
have H2 : pr₂ (a, b) = pr₂ (pr₁ (a, b), pr₂ (a, b)), from eq.trans H12 H22,
prod.equal H1 H2
theorem pc1 (z s t : int) : prod.cases_on (s, t) (egcd_rec_f z) = (t, s - t * z) :=
have H1 : prod.cases_on (s, t) (egcd_rec_f z) = egcd_rec_f z s t, from rfl,
have H2 : egcd_rec_f z s t = (t, s - t * z), from rfl,
eq.trans H1 H2
theorem pc2 (z : int) (p : int × int) : prod.cases_on p (egcd_rec_f z) = (pr₂ p, (pr₁ p) - (pr₂ p) * z) :=
have H1 : prod.cases_on (pr₁ p, pr₂ p) (egcd_rec_f z) = (pr₂ p, (pr₁ p) - (pr₂ p) * z), from pc1 z (pr₁ p) (pr₂ p),
eq.subst (preq p)⁻¹ H1
theorem egcdi_succ2 (x y : nat) : egcdi x (succ y) = (pr₂ (egcdi (succ y) (x mod succ y)), (pr₁ (egcdi (succ y) (x mod succ y))) - (pr₂ (egcdi (succ y) (x mod succ y))) * (x div succ y)) :=
have H1 : egcdi x (succ y) = prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)), from egcdi_succ x y,
have H2 : prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)) =
(pr₂ (egcdi (succ y) (x mod succ y)), int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))), from pc2 (x div succ y) (egcdi (succ y) (x mod succ y)),
eq.trans H1 H2
theorem int_rearrange (a b c : int) : a + (b - (a - c)) = b + c :=
calc a + (b - (a - c)) = a + (b + (int.neg (a - c))) : int.sub_eq_add_neg
... = a + (b + (c - a)) : int.neg_sub
... = a + (b + (c + (int.neg a))) : int.sub_eq_add_neg
... = a + (b + ((int.neg a) + c)) : int.add.comm
... = a + (((int.neg a) + c) + b) : int.add.comm
... = (a + ((int.neg a) + c)) + b : int.add.assoc
... = c + b : int.add_neg_cancel_left
... = b + c : int.add.comm
theorem sub_mod_eq_int_sub_mod (x y : nat) : x - (x mod (succ y)) = int.sub x (x mod (succ y)) :=
(int.of_nat_sub_of_nat mod_le)⁻¹
theorem divmul (x y : nat) : int.mul (x div y) y = x - (x mod y) :=
calc int.mul (x div y) y = nat.mul (nat.divide x y) y : int.of_nat_mul_of_nat
... = (x div y) * y + (x mod y) - (x mod y) : (nat.add_sub_cancel ((x div y) * y) (x mod y))⁻¹
... = x - (x mod y) : eq_div_mul_add_mod⁻¹
theorem egcdi_succ_first (x y : nat) : pr₁ (egcdi x (succ y)) = pr₂ (egcdi (succ y) (x mod succ y)) :=
calc pr₁ (egcdi x (succ y)) = pr₁ (pr₂ (egcdi (succ y) (x mod succ y)), (pr₁ (egcdi (succ y) (x mod succ y))) - ((pr₂ (egcdi (succ y) (x mod succ y))) * (x div succ y))) : egcdi_succ2 x y
... = pr₂ (egcdi (succ y) (x mod succ y)) : rfl
theorem egcdi_succ_second (x y : nat) : pr₂ (egcdi x (succ y)) = (pr₁ (egcdi (succ y) (x mod succ y))) - ((pr₂ (egcdi (succ y) (x mod succ y))) * (x div succ y)) :=
calc pr₂ (egcdi x (succ y)) = pr₂ (pr₂ (egcdi (succ y) (x mod succ y)), (pr₁ (egcdi (succ y) (x mod succ y))) - ((pr₂ (egcdi (succ y) (x mod succ y))) * (x div succ y))) : egcdi_succ2 x y
... = (pr₁ (egcdi (succ y) (x mod succ y))) - ((pr₂ (egcdi (succ y) (x mod succ y))) * (x div succ y)) : rfl
/- Inductive step
-/
theorem egcdi_ind (x y : nat) :
((pr₁ (egcdi (succ y) (x mod (succ y)))) * (succ y)) + ((pr₂ (egcdi (succ y) (x mod (succ y)))) * (x mod (succ y))) = gcd (succ y) (x mod (succ y)) →
((pr₁ (egcdi x (succ y))) * x) + ((pr₂ (egcdi x (succ y))) * (succ y)) = gcd x (succ y) :=
assume IH : ((pr₁ (egcdi (succ y) (x mod (succ y)))) * (succ y)) + ((pr₂ (egcdi (succ y) (x mod (succ y)))) * (x mod (succ y))) = gcd (succ y) (x mod (succ y)),
calc int.add (int.mul (pr₁ (egcdi x (succ y))) x) (int.mul (pr₂ (egcdi x (succ y))) (succ y)) = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.mul (pr₂ (egcdi x (succ y))) (succ y)) : egcdi_succ_first x y
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.mul (int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))) (succ y)) : egcdi_succ_second x y
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.sub
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y) ) (succ y) ) ) : int.mul_sub_right_distrib
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.sub
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (pr₂ (egcdi (succ y) (x mod succ y)))
(int.mul (x div succ y) (succ y))
)
) : int.mul.assoc
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.sub
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (pr₂ (egcdi (succ y) (x mod succ y)))
(x - (x mod (succ y)))
)
) : divmul x (succ y)
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.sub
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (pr₂ (egcdi (succ y) (x mod succ y)))
(int.sub x (x mod (succ y)))
)
) : sub_mod_eq_int_sub_mod x y
... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.sub
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.sub (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y)))
)
) : int.mul_sub_left_distrib
... = int.add (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y)))
: int_rearrange (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x)
(int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y))
(int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y)))
... = gcd (succ y) (x mod (succ y)) : IH
... = gcd x (succ y) : (gcd_rec x (succ y))⁻¹
/- Base case
-/
theorem egcdi_base (x : nat) : int.add (int.mul (pr₁ (egcdi x 0)) x) (int.mul (pr₂ (egcdi x 0)) 0) = gcd x 0 :=
calc int.add (int.mul (pr₁ (egcdi x 0)) x) (int.mul (pr₂ (egcdi x 0)) 0) = int.add (int.mul 1 x) (int.mul (pr₂ (egcdi x 0)) 0) : egcdi_zero_right_1
... = int.add (int.mul 1 x) (int.mul 0 0) : egcdi_zero_right_2
... = int.add (int.mul x 1) (int.mul 0 0) : int.mul.comm
... = int.add x (int.mul 0 0) : int.mul_one
... = int.add x 0 : int.mul_zero
... = x + 0 : int.of_nat_add_of_nat
... = x : nat.add_zero
... = gcd x 0 : gcd_zero_right
/- Conclusion
-/
private definition P (p : nat × nat) := (pr₁ (egcdi (pr₁ p) (pr₂ p))) * (pr₁ p) + (pr₂ (egcdi (pr₁ p) (pr₂ p))) * (pr₂ p) = gcd (pr₁ p) (pr₂ p)
private theorem Pbase (x : nat) : P (x, 0) := egcdi_base x
private theorem Prec (x y : nat) : P (succ y, x mod (succ y)) → P (x, succ y) := egcdi_ind x y
private definition P.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → P p₂) → P p₁ :=
prod.cases_on p₁ (λx y, nat.cases_on y
(λ f, Pbase x)
(λ y₁ (f : Πp₂, p₂ ≺ (x, succ y₁) → P p₂), (Prec x y₁) (f (succ y₁, x mod succ y₁) !gcd.lt.dec) ))
private definition Bezouts1 (x y : ℕ) : (pr₁ (egcdi x y)) * x + (pr₂ (egcdi x y)) * y = gcd x y := fix P.F (pair x y)
theorem BezoutsLemma : ∀ x y : ℕ, ∃ a b : ℤ, a * x + b * y = gcd x y :=
take x y : ℕ,
let a := pr₁ (egcdi x y),
b := pr₂ (egcdi x y)
in have H : a * x + b * y = gcd x y, from Bezouts1 x y,
exists.intro a (exists.intro b H)
check BezoutsLemma
|
77ed7eb1f87a7453ddd691eca5a9b0917c170118 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_1778.lean | d5e9f2712dbe60a4ce2f1d01443fb6f6fa341cfb | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 159 | lean | variables {α : Type*} [partial_order α]
variables x y z : α
#check x ≤ y
#check (le_refl x : x ≤ x)
#check (le_trans : x ≤ y → y ≤ z → x ≤ z) |
f15088676e52a42220f56f1874804b6d8597963e | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Lean/Elab/Calc.lean | 4f6da6e7b8e72f77b475a561979260ddc78222b0 | [
"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",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 3,243 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.App
namespace Lean.Elab.Term
open Meta
/--
Decompose `e` into `(r, a, b)`.
Remark: it assumes the last two arguments are explicit. -/
def getCalcRelation? (e : Expr) : MetaM (Option (Expr × Expr × Expr)) :=
if e.getAppNumArgs < 2 then
return none
else
return some (e.appFn!.appFn!, e.appFn!.appArg!, e.appArg!)
private def getRelUniv (r : Expr) : MetaM Level := do
let rType ← inferType r
forallTelescopeReducing rType fun _ sort => do
let .sort u ← whnf sort | throwError "unexpected relation type{indentExpr rType}"
return u
def mkCalcTrans (result resultType step stepType : Expr) : MetaM (Expr × Expr) := do
let some (r, a, b) ← getCalcRelation? resultType | unreachable!
let some (s, _, c) ← getCalcRelation? (← instantiateMVars stepType) | unreachable!
let u ← getRelUniv r
let v ← getRelUniv s
let (α, β, γ) := (← inferType a, ← inferType b, ← inferType c)
let (u_1, u_2, u_3) := (← getLevel α, ← getLevel β, ← getLevel γ)
let w ← mkFreshLevelMVar
let t ← mkFreshExprMVar (← mkArrow α (← mkArrow γ (mkSort w)))
let selfType := mkAppN (Lean.mkConst ``Trans [u, v, w, u_1, u_2, u_3]) #[α, β, γ, r, s, t]
match (← trySynthInstance selfType) with
| .some self =>
let result := mkAppN (Lean.mkConst ``Trans.trans [u, v, w, u_1, u_2, u_3]) #[α, β, γ, r, s, t, self, a, b, c, result, step]
let resultType := (← instantiateMVars (← inferType result)).headBeta
unless (← getCalcRelation? resultType).isSome do
throwError "invalid 'calc' step, step result is not a relation{indentExpr resultType}"
return (result, resultType)
| _ => throwError "invalid 'calc' step, failed to synthesize `Trans` instance{indentExpr selfType}"
/--
Elaborate `calc`-steps
-/
def elabCalcSteps (steps : Array Syntax) : TermElabM Expr := do
let mut proofs := #[]
let mut types := #[]
for step in steps do
let type ← elabType step[0]
let some (_, lhs, _) ← getCalcRelation? type |
throwErrorAt step[0] "invalid 'calc' step, relation expected{indentExpr type}"
if types.size > 0 then
let some (_, _, prevRhs) ← getCalcRelation? types.back | unreachable!
unless (← isDefEqGuarded lhs prevRhs) do
throwErrorAt step[0] "invalid 'calc' step, left-hand-side is {indentD m!"{lhs} : {← inferType lhs}"}\nprevious right-hand-side is{indentD m!"{prevRhs} : {← inferType prevRhs}"}"
types := types.push type
let proof ← elabTermEnsuringType step[2] type
synthesizeSyntheticMVars
proofs := proofs.push proof
let mut result := proofs[0]!
let mut resultType := types[0]!
for i in [1:proofs.size] do
(result, resultType) ← withRef steps[i]! <| mkCalcTrans result resultType proofs[i]! types[i]!
return result
/-- Elaborator for the `calc` term mode variant. -/
@[builtin_term_elab «calc»]
def elabCalc : TermElab := fun stx expectedType? => do
let steps := #[stx[1]] ++ stx[2].getArgs
let result ← elabCalcSteps steps
ensureHasType expectedType? result
|
21aed181d6c87e8c287226e2a5ec8d9f2b2a2589 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/run/cpdt_nano.lean | 37d314bcd5813167e449b85efea2bf38b5b610b1 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,051 | lean | import tools.mini_crush.nano_crush
/- "Proving in the Large" chapter of CPDT -/
inductive exp : Type
| Const (n : nat) : exp
| Plus (e1 e2 : exp) : exp
| Mult (e1 e2 : exp) : exp
open exp
def eeval : exp → nat
| (Const n) := n
| (Plus e1 e2) := eeval e1 + eeval e2
| (Mult e1 e2) := eeval e1 * eeval e2
def times (k : nat) : exp → exp
| (Const n) := Const (k * n)
| (Plus e1 e2) := Plus (times e1) (times e2)
| (Mult e1 e2) := Mult (times e1) e2
def reassoc : exp → exp
| (Const n) := (Const n)
| (Plus e1 e2) :=
let e1' := reassoc e1 in
let e2' := reassoc e2 in
match e2' with
| (Plus e21 e22) := Plus (Plus e1' e21) e22
| _ := Plus e1' e2'
end
| (Mult e1 e2) :=
let e1' := reassoc e1 in
let e2' := reassoc e2 in
match e2' with
| (Mult e21 e22) := Mult (Mult e1' e21) e22
| _ := Mult e1' e2'
end
attribute [simp] mul_add
theorem eeval_times (k e) : eeval (times k e) = k * eeval e :=
by nano_crush 0
theorem reassoc_correct (e) : eeval (reassoc e) = eeval e :=
by nano_crush 1
|
2c6891e6184c9fadf1ff3d3440f9de6ada0b2a47 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/algebra/big_operators/finprod.lean | 2796b6b5237353dac6b50c651e172f3d6fef8e0d | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 38,692 | lean | /-
Copyright (c) 2020 Kexing Ying and Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Kevin Buzzard, Yury Kudryashov
-/
import algebra.big_operators.order
import algebra.indicator_function
import data.set.pairwise
/-!
# Finite products and sums over types and sets
We define products and sums over types and subsets of types, with no finiteness hypotheses.
All infinite products and sums are defined to be junk values (i.e. one or zero).
This approach is sometimes easier to use than `finset.sum`,
when issues arise with `finset` and `fintype` being data.
## Main definitions
We use the following variables:
* `α`, `β` - types with no structure;
* `s`, `t` - sets
* `M`, `N` - additive or multiplicative commutative monoids
* `f`, `g` - functions
Definitions in this file:
* `finsum f : M` : the sum of `f x` as `x` ranges over the support of `f`, if it's finite.
Zero otherwise.
* `finprod f : M` : the product of `f x` as `x` ranges over the multiplicative support of `f`, if
it's finite. One otherwise.
## Notation
* `∑ᶠ i, f i` and `∑ᶠ i : α, f i` for `finsum f`
* `∏ᶠ i, f i` and `∏ᶠ i : α, f i` for `finprod f`
This notation works for functions `f : p → M`, where `p : Prop`, so the following works:
* `∑ᶠ i ∈ s, f i`, where `f : α → M`, `s : set α` : sum over the set `s`;
* `∑ᶠ n < 5, f n`, where `f : ℕ → M` : same as `f 0 + f 1 + f 2 + f 3 + f 4`;
* `∏ᶠ (n >= -2) (hn : n < 3), f n`, where `f : ℤ → M` : same as `f (-2) * f (-1) * f 0 * f 1 * f 2`.
## Implementation notes
`finsum` and `finprod` is "yet another way of doing finite sums and products in Lean". However
experiments in the wild (e.g. with matroids) indicate that it is a helpful approach in settings
where the user is not interested in computability and wants to do reasoning without running into
typeclass diamonds caused by the constructive finiteness used in definitions such as `finset` and
`fintype`. By sticking solely to `set.finite` we avoid these problems. We are aware that there are
other solutions but for beginner mathematicians this approach is easier in practice.
Another application is the construction of a partition of unity from a collection of “bump”
function. In this case the finite set depends on the point and it's convenient to have a definition
that does not mention the set explicitly.
The first arguments in all definitions and lemmas is the codomain of the function of the big
operator. This is necessary for the heuristic in `@[to_additive]`.
See the documentation of `to_additive.attr` for more information.
We did not add `is_finite (X : Type) : Prop`, because it is simply `nonempty (fintype X)`.
## Tags
finsum, finprod, finite sum, finite product
-/
open function set
/-!
### Definition and relation to `finset.sum` and `finset.prod`
-/
section sort
variables {M N : Type*} {α β ι : Sort*} [comm_monoid M] [comm_monoid N]
open_locale big_operators
section
/- Note: we use classical logic only for these definitions, to ensure that we do not write lemmas
with `classical.dec` in their statement. -/
open_locale classical
/-- Sum of `f x` as `x` ranges over the elements of the support of `f`, if it's finite. Zero
otherwise. -/
@[irreducible] noncomputable def finsum {M α} [add_comm_monoid M] (f : α → M) : M :=
if h : finite (support (f ∘ plift.down)) then ∑ i in h.to_finset, f i.down else 0
/-- Product of `f x` as `x` ranges over the elements of the multiplicative support of `f`, if it's
finite. One otherwise. -/
@[irreducible, to_additive]
noncomputable def finprod (f : α → M) : M :=
if h : finite (mul_support (f ∘ plift.down)) then ∏ i in h.to_finset, f i.down else 1
end
localized "notation `∑ᶠ` binders `, ` r:(scoped:67 f, finsum f) := r" in big_operators
localized "notation `∏ᶠ` binders `, ` r:(scoped:67 f, finprod f) := r" in big_operators
@[to_additive] lemma finprod_eq_prod_plift_of_mul_support_to_finset_subset
{f : α → M} (hf : finite (mul_support (f ∘ plift.down))) {s : finset (plift α)}
(hs : hf.to_finset ⊆ s) :
∏ᶠ i, f i = ∏ i in s, f i.down :=
begin
rw [finprod, dif_pos],
refine finset.prod_subset hs (λ x hx hxf, _),
rwa [hf.mem_to_finset, nmem_mul_support] at hxf
end
@[to_additive] lemma finprod_eq_prod_plift_of_mul_support_subset
{f : α → M} {s : finset (plift α)} (hs : mul_support (f ∘ plift.down) ⊆ s) :
∏ᶠ i, f i = ∏ i in s, f i.down :=
finprod_eq_prod_plift_of_mul_support_to_finset_subset
(s.finite_to_set.subset hs) $ λ x hx, by { rw finite.mem_to_finset at hx, exact hs hx }
@[simp, to_additive] lemma finprod_one : ∏ᶠ i : α, (1 : M) = 1 :=
begin
have : mul_support (λ x : plift α, (λ _, 1 : α → M) x.down) ⊆ (∅ : finset (plift α)),
from λ x h, h rfl,
rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_empty]
end
@[to_additive] lemma finprod_of_is_empty [is_empty α] (f : α → M) : ∏ᶠ i, f i = 1 :=
by { rw ← finprod_one, congr }
@[simp, to_additive] lemma finprod_false (f : false → M) : ∏ᶠ i, f i = 1 :=
finprod_of_is_empty _
@[to_additive] lemma finprod_eq_single (f : α → M) (a : α) (ha : ∀ x ≠ a, f x = 1) :
∏ᶠ x, f x = f a :=
begin
have : mul_support (f ∘ plift.down) ⊆ ({plift.up a} : finset (plift α)),
{ intro x, contrapose,
simpa [plift.eq_up_iff_down_eq] using ha x.down },
rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_singleton],
end
@[to_additive] lemma finprod_unique [unique α] (f : α → M) : ∏ᶠ i, f i = f (default α) :=
finprod_eq_single f (default α) $ λ x hx, (hx $ unique.eq_default _).elim
@[simp, to_additive] lemma finprod_true (f : true → M) : ∏ᶠ i, f i = f trivial :=
@finprod_unique M true _ ⟨⟨trivial⟩, λ _, rfl⟩ f
@[to_additive] lemma finprod_eq_dif {p : Prop} [decidable p] (f : p → M) :
∏ᶠ i, f i = if h : p then f h else 1 :=
begin
split_ifs,
{ haveI : unique p := ⟨⟨h⟩, λ _, rfl⟩, exact finprod_unique f },
{ haveI : is_empty p := ⟨h⟩, exact finprod_of_is_empty f }
end
@[to_additive] lemma finprod_eq_if {p : Prop} [decidable p] {x : M} :
∏ᶠ i : p, x = if p then x else 1 :=
finprod_eq_dif (λ _, x)
@[to_additive] lemma finprod_congr {f g : α → M} (h : ∀ x, f x = g x) :
finprod f = finprod g :=
congr_arg _ $ funext h
@[congr, to_additive] lemma finprod_congr_Prop {p q : Prop} {f : p → M} {g : q → M} (hpq : p = q)
(hfg : ∀ h : q, f (hpq.mpr h) = g h) :
finprod f = finprod g :=
by { subst q, exact finprod_congr hfg }
attribute [congr] finsum_congr_Prop
/-- To prove a property of a finite product, it suffices to prove that the property is
multiplicative and holds on multipliers. -/
@[to_additive] lemma finprod_induction {f : α → M} (p : M → Prop) (hp₀ : p 1)
(hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ i, p (f i)) :
p (∏ᶠ i, f i) :=
begin
rw finprod,
split_ifs,
exacts [finset.prod_induction _ _ hp₁ hp₀ (λ i hi, hp₂ _), hp₀]
end
/-- To prove a property of a finite sum, it suffices to prove that the property is
additive and holds on summands. -/
add_decl_doc finsum_induction
lemma finprod_nonneg {R : Type*} [ordered_comm_semiring R] {f : α → R} (hf : ∀ x, 0 ≤ f x) :
0 ≤ ∏ᶠ x, f x :=
finprod_induction (λ x, 0 ≤ x) zero_le_one (λ x y, mul_nonneg) hf
@[to_additive finsum_nonneg]
lemma one_le_finprod' {M : Type*} [ordered_comm_monoid M] {f : α → M} (hf : ∀ i, 1 ≤ f i) :
1 ≤ ∏ᶠ i, f i :=
finprod_induction _ le_rfl (λ _ _, one_le_mul) hf
@[to_additive] lemma monoid_hom.map_finprod_plift (f : M →* N) (g : α → M)
(h : finite (mul_support $ g ∘ plift.down)) :
f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) :=
begin
rw [finprod_eq_prod_plift_of_mul_support_subset h.coe_to_finset.ge,
finprod_eq_prod_plift_of_mul_support_subset, f.map_prod],
rw [h.coe_to_finset],
exact mul_support_comp_subset f.map_one (g ∘ plift.down)
end
@[to_additive] lemma monoid_hom.map_finprod_Prop {p : Prop} (f : M →* N) (g : p → M) :
f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) :=
f.map_finprod_plift g (finite.of_fintype _)
@[to_additive] lemma monoid_hom.map_finprod_of_preimage_one (f : M →* N)
(hf : ∀ x, f x = 1 → x = 1) (g : α → M) :
f (∏ᶠ i, g i) = ∏ᶠ i, f (g i) :=
begin
by_cases hg : (mul_support $ g ∘ plift.down).finite, { exact f.map_finprod_plift g hg },
rw [finprod, dif_neg, f.map_one, finprod, dif_neg],
exacts [infinite.mono (λ x hx, mt (hf (g x.down)) hx) hg, hg]
end
@[to_additive] lemma monoid_hom.map_finprod_of_injective (g : M →* N) (hg : injective g)
(f : α → M) :
g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.map_finprod_of_preimage_one (λ x, (hg.eq_iff' g.map_one).mp) f
@[to_additive] lemma mul_equiv.map_finprod (g : M ≃* N) (f : α → M) :
g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.to_monoid_hom.map_finprod_of_injective g.injective f
lemma finsum_smul {R M : Type*} [ring R] [add_comm_group M] [module R M]
[no_zero_smul_divisors R M] (f : ι → R) (x : M) :
(∑ᶠ i, f i) • x = (∑ᶠ i, (f i) • x) :=
begin
rcases eq_or_ne x 0 with rfl|hx, { simp },
exact ((smul_add_hom R M).flip x).map_finsum_of_injective (smul_left_injective R hx) _
end
lemma smul_finsum {R M : Type*} [ring R] [add_comm_group M] [module R M]
[no_zero_smul_divisors R M] (c : R) (f : ι → M) :
c • (∑ᶠ i, f i) = (∑ᶠ i, c • f i) :=
begin
rcases eq_or_ne c 0 with rfl|hc, { simp },
exact (smul_add_hom R M c).map_finsum_of_injective (smul_right_injective M hc) _
end
@[to_additive] lemma finprod_inv_distrib {G : Type*} [comm_group G] (f : α → G) :
∏ᶠ x, (f x)⁻¹ = (∏ᶠ x, f x)⁻¹ :=
((mul_equiv.inv G).map_finprod f).symm
end sort
section type
variables {α β ι M N : Type*} [comm_monoid M] [comm_monoid N]
open_locale big_operators
@[to_additive] lemma finprod_eq_mul_indicator_apply (s : set α)
(f : α → M) (a : α) :
∏ᶠ (h : a ∈ s), f a = mul_indicator s f a :=
by convert finprod_eq_if
@[simp, to_additive] lemma finprod_mem_mul_support (f : α → M) (a : α) :
∏ᶠ (h : f a ≠ 1), f a = f a :=
by rw [← mem_mul_support, finprod_eq_mul_indicator_apply, mul_indicator_mul_support]
@[to_additive] lemma finprod_mem_def (s : set α) (f : α → M) :
∏ᶠ a ∈ s, f a = ∏ᶠ a, mul_indicator s f a :=
finprod_congr $ finprod_eq_mul_indicator_apply s f
@[to_additive] lemma finprod_eq_prod_of_mul_support_subset (f : α → M) {s : finset α}
(h : mul_support f ⊆ s) :
∏ᶠ i, f i = ∏ i in s, f i :=
begin
have A : mul_support (f ∘ plift.down) = equiv.plift.symm '' mul_support f,
{ rw mul_support_comp_eq_preimage,
exact (equiv.plift.symm.image_eq_preimage _).symm },
have : mul_support (f ∘ plift.down) ⊆ s.map equiv.plift.symm.to_embedding,
{ rw [A, finset.coe_map], exact image_subset _ h },
rw [finprod_eq_prod_plift_of_mul_support_subset this],
simp
end
@[to_additive] lemma finprod_eq_prod_of_mul_support_to_finset_subset (f : α → M)
(hf : finite (mul_support f)) {s : finset α} (h : hf.to_finset ⊆ s) :
∏ᶠ i, f i = ∏ i in s, f i :=
finprod_eq_prod_of_mul_support_subset _ $ λ x hx, h $ hf.mem_to_finset.2 hx
@[to_additive] lemma finprod_def (f : α → M) [decidable (mul_support f).finite] :
∏ᶠ i : α, f i = if h : (mul_support f).finite then ∏ i in h.to_finset, f i else 1 :=
begin
split_ifs,
{ exact finprod_eq_prod_of_mul_support_to_finset_subset _ h (finset.subset.refl _) },
{ rw [finprod, dif_neg],
rw [mul_support_comp_eq_preimage],
exact mt (λ hf, hf.of_preimage equiv.plift.surjective) h}
end
@[to_additive] lemma finprod_of_infinite_mul_support {f : α → M} (hf : (mul_support f).infinite) :
∏ᶠ i, f i = 1 :=
by { classical, rw [finprod_def, dif_neg hf] }
@[to_additive] lemma finprod_eq_prod (f : α → M) (hf : (mul_support f).finite) :
∏ᶠ i : α, f i = ∏ i in hf.to_finset, f i :=
by { classical, rw [finprod_def, dif_pos hf] }
@[to_additive] lemma finprod_eq_prod_of_fintype [fintype α] (f : α → M) :
∏ᶠ i : α, f i = ∏ i, f i :=
finprod_eq_prod_of_mul_support_to_finset_subset _ (finite.of_fintype _) $ finset.subset_univ _
@[to_additive] lemma finprod_cond_eq_prod_of_cond_iff (f : α → M) {p : α → Prop} {t : finset α}
(h : ∀ {x}, f x ≠ 1 → (p x ↔ x ∈ t)) :
∏ᶠ i (hi : p i), f i = ∏ i in t, f i :=
begin
set s := {x | p x},
have : mul_support (s.mul_indicator f) ⊆ t,
{ rw [set.mul_support_mul_indicator], intros x hx, exact (h hx.2).1 hx.1 },
erw [finprod_mem_def, finprod_eq_prod_of_mul_support_subset _ this],
refine finset.prod_congr rfl (λ x hx, mul_indicator_apply_eq_self.2 $ λ hxs, _),
contrapose! hxs,
exact (h hxs).2 hx
end
@[to_additive] lemma finprod_mem_eq_prod_of_inter_mul_support_eq (f : α → M) {s : set α}
{t : finset α} (h : s ∩ mul_support f = t ∩ mul_support f) :
∏ᶠ i ∈ s, f i = ∏ i in t, f i :=
finprod_cond_eq_prod_of_cond_iff _ $ by simpa [set.ext_iff] using h
@[to_additive] lemma finprod_mem_eq_prod_of_subset (f : α → M) {s : set α} {t : finset α}
(h₁ : s ∩ mul_support f ⊆ t) (h₂ : ↑t ⊆ s) :
∏ᶠ i ∈ s, f i = ∏ i in t, f i :=
finprod_cond_eq_prod_of_cond_iff _ $ λ x hx, ⟨λ h, h₁ ⟨h, hx⟩, λ h, h₂ h⟩
@[to_additive] lemma finprod_mem_eq_prod (f : α → M) {s : set α}
(hf : (s ∩ mul_support f).finite) :
∏ᶠ i ∈ s, f i = ∏ i in hf.to_finset, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_assoc]
@[to_additive] lemma finprod_mem_eq_prod_filter (f : α → M) (s : set α) [decidable_pred (∈ s)]
(hf : (mul_support f).finite) :
∏ᶠ i ∈ s, f i = ∏ i in finset.filter (∈ s) hf.to_finset, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_comm, inter_left_comm]
@[to_additive] lemma finprod_mem_eq_to_finset_prod (f : α → M) (s : set α) [fintype s] :
∏ᶠ i ∈ s, f i = ∏ i in s.to_finset, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [coe_to_finset]
@[to_additive] lemma finprod_mem_eq_finite_to_finset_prod (f : α → M) {s : set α} (hs : s.finite) :
∏ᶠ i ∈ s, f i = ∏ i in hs.to_finset, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [hs.coe_to_finset]
@[to_additive] lemma finprod_mem_finset_eq_prod (f : α → M) (s : finset α) :
∏ᶠ i ∈ s, f i = ∏ i in s, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl
@[to_additive] lemma finprod_mem_coe_finset (f : α → M) (s : finset α) :
∏ᶠ i ∈ (s : set α), f i = ∏ i in s, f i :=
finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl
@[to_additive] lemma finprod_mem_eq_one_of_infinite {f : α → M} {s : set α}
(hs : (s ∩ mul_support f).infinite) : ∏ᶠ i ∈ s, f i = 1 :=
begin
rw finprod_mem_def,
apply finprod_of_infinite_mul_support,
rwa [← mul_support_mul_indicator] at hs
end
@[to_additive] lemma finprod_mem_inter_mul_support (f : α → M) (s : set α) :
∏ᶠ i ∈ (s ∩ mul_support f), f i = ∏ᶠ i ∈ s, f i :=
by rw [finprod_mem_def, finprod_mem_def, mul_indicator_inter_mul_support]
@[to_additive] lemma finprod_mem_inter_mul_support_eq (f : α → M) (s t : set α)
(h : s ∩ mul_support f = t ∩ mul_support f) :
∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i :=
by rw [← finprod_mem_inter_mul_support, h, finprod_mem_inter_mul_support]
@[to_additive] lemma finprod_mem_inter_mul_support_eq' (f : α → M) (s t : set α)
(h : ∀ x ∈ mul_support f, x ∈ s ↔ x ∈ t) :
∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i :=
begin
apply finprod_mem_inter_mul_support_eq,
ext x,
exact and_congr_left (h x)
end
@[to_additive] lemma finprod_mem_univ (f : α → M) : ∏ᶠ i ∈ @set.univ α, f i = ∏ᶠ i : α, f i :=
finprod_congr $ λ i, finprod_true _
variables {f g : α → M} {a b : α} {s t : set α}
@[to_additive] lemma finprod_mem_congr (h₀ : s = t) (h₁ : ∀ x ∈ t, f x = g x) :
∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, g i :=
h₀.symm ▸ (finprod_congr $ λ i, finprod_congr_Prop rfl (h₁ i))
/-!
### Distributivity w.r.t. addition, subtraction, and (scalar) multiplication
-/
/-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i * g i` equals
the product of `f i` multiplied by the product over `g i`. -/
@[to_additive] lemma finprod_mul_distrib (hf : (mul_support f).finite)
(hg : (mul_support g).finite) :
∏ᶠ i, (f i * g i) = (∏ᶠ i, f i) * ∏ᶠ i, g i :=
begin
classical,
rw [finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_union_left _ _),
finprod_eq_prod_of_mul_support_to_finset_subset _ hg (finset.subset_union_right _ _),
← finset.prod_mul_distrib],
refine finprod_eq_prod_of_mul_support_subset _ _,
simp [mul_support_mul]
end
/-- A more general version of `finprod_mem_mul_distrib` that requires `s ∩ mul_support f` and
`s ∩ mul_support g` instead of `s` to be finite. -/
@[to_additive] lemma finprod_mem_mul_distrib' (hf : (s ∩ mul_support f).finite)
(hg : (s ∩ mul_support g).finite) :
∏ᶠ i ∈ s, (f i * g i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i :=
begin
rw [← mul_support_mul_indicator] at hf hg,
simp only [finprod_mem_def, mul_indicator_mul, finprod_mul_distrib hf hg]
end
/-- The product of constant one over any set equals one. -/
@[to_additive] lemma finprod_mem_one (s : set α) : ∏ᶠ i ∈ s, (1 : M) = 1 := by simp
/-- If a function `f` equals one on a set `s`, then the product of `f i` over `i ∈ s` equals one. -/
@[to_additive] lemma finprod_mem_of_eq_on_one (hf : eq_on f 1 s) : ∏ᶠ i ∈ s, f i = 1 :=
by { rw ← finprod_mem_one s, exact finprod_mem_congr rfl hf }
/-- If the product of `f i` over `i ∈ s` is not equal to one, then there is some `x ∈ s`
such that `f x ≠ 1`. -/
@[to_additive] lemma exists_ne_one_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) :
∃ x ∈ s, f x ≠ 1 :=
begin
by_contra h', push_neg at h',
exact h (finprod_mem_of_eq_on_one h')
end
/-- Given a finite set `s`, the product of `f i * g i` over `i ∈ s` equals the product of `f i`
over `i ∈ s` times the product of `g i` over `i ∈ s`. -/
@[to_additive] lemma finprod_mem_mul_distrib (hs : s.finite) :
∏ᶠ i ∈ s, (f i * g i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i :=
finprod_mem_mul_distrib' (hs.inter_of_left _) (hs.inter_of_left _)
@[to_additive] lemma monoid_hom.map_finprod {f : α → M} (g : M →* N) (hf : (mul_support f).finite) :
g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) :=
g.map_finprod_plift f $ hf.preimage $ equiv.plift.injective.inj_on _
/-- A more general version of `monoid_hom.map_finprod_mem` that requires `s ∩ mul_support f` and
instead of `s` to be finite. -/
@[to_additive] lemma monoid_hom.map_finprod_mem' {f : α → M} (g : M →* N)
(h₀ : (s ∩ mul_support f).finite) :
g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, (g (f i)) :=
begin
rw [g.map_finprod],
{ simp only [g.map_finprod_Prop] },
{ simpa only [finprod_eq_mul_indicator_apply, mul_support_mul_indicator] }
end
/-- Given a monoid homomorphism `g : M →* N`, and a function `f : α → M`, the value of `g` at the
product of `f i` over `i ∈ s` equals the product of `(g ∘ f) i` over `s`. -/
@[to_additive] lemma monoid_hom.map_finprod_mem (f : α → M) (g : M →* N) (hs : s.finite) :
g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) :=
g.map_finprod_mem' (hs.inter_of_left _)
/-!
### `∏ᶠ x ∈ s, f x` and set operations
-/
/-- The product of any function over an empty set is one. -/
@[to_additive] lemma finprod_mem_empty : ∏ᶠ i ∈ (∅ : set α), f i = 1 := by simp
/-- A set `s` is not empty if the product of some function over `s` is not equal to one. -/
@[to_additive] lemma nonempty_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : s.nonempty :=
ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ finprod_mem_empty
/-- Given finite sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` times the product of
`f i` over `i ∈ s ∩ t` equals the product of `f i` over `i ∈ s` times the product of `f i`
over `i ∈ t`. -/
@[to_additive] lemma finprod_mem_union_inter (hs : s.finite) (ht : t.finite) :
(∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
begin
lift s to finset α using hs, lift t to finset α using ht,
classical,
rw [← finset.coe_union, ← finset.coe_inter],
simp only [finprod_mem_coe_finset, finset.prod_union_inter]
end
/-- A more general version of `finprod_mem_union_inter` that requires `s ∩ mul_support f` and
`t ∩ mul_support f` instead of `s` and `t` to be finite. -/
@[to_additive] lemma finprod_mem_union_inter'
(hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) :
(∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
begin
rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t,
← finprod_mem_union_inter hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support,
← finprod_mem_inter_mul_support f (s ∩ t)],
congr' 2,
rw [inter_left_comm, inter_assoc, inter_assoc, inter_self, inter_left_comm]
end
/-- A more general version of `finprod_mem_union` that requires `s ∩ mul_support f` and
`t ∩ mul_support f` instead of `s` and `t` to be finite. -/
@[to_additive] lemma finprod_mem_union' (hst : disjoint s t) (hs : (s ∩ mul_support f).finite)
(ht : (t ∩ mul_support f).finite) :
∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
by rw [← finprod_mem_union_inter' hs ht, disjoint_iff_inter_eq_empty.1 hst, finprod_mem_empty,
mul_one]
/-- Given two finite disjoint sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` equals the
product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/
@[to_additive] lemma finprod_mem_union (hst : disjoint s t) (hs : s.finite) (ht : t.finite) :
∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
finprod_mem_union' hst (hs.inter_of_left _) (ht.inter_of_left _)
/-- A more general version of `finprod_mem_union'` that requires `s ∩ mul_support f` and
`t ∩ mul_support f` instead of `s` and `t` to be disjoint -/
@[to_additive] lemma finprod_mem_union'' (hst : disjoint (s ∩ mul_support f) (t ∩ mul_support f))
(hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) :
∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i :=
by rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t,
← finprod_mem_union hst hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support]
/-- The product of `f i` over `i ∈ {a}` equals `f a`. -/
@[to_additive] lemma finprod_mem_singleton : ∏ᶠ i ∈ ({a} : set α), f i = f a :=
by rw [← finset.coe_singleton, finprod_mem_coe_finset, finset.prod_singleton]
@[simp, to_additive] lemma finprod_cond_eq_left : ∏ᶠ i = a, f i = f a :=
finprod_mem_singleton
@[simp, to_additive] lemma finprod_cond_eq_right : ∏ᶠ i (hi : a = i), f i = f a :=
by simp [@eq_comm _ a]
/-- A more general version of `finprod_mem_insert` that requires `s ∩ mul_support f` instead of
`s` to be finite. -/
@[to_additive] lemma finprod_mem_insert' (f : α → M) (h : a ∉ s)
(hs : (s ∩ mul_support f).finite) :
∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i :=
begin
rw [insert_eq, finprod_mem_union' _ _ hs, finprod_mem_singleton],
{ rwa disjoint_singleton_left },
{ exact (finite_singleton a).inter_of_left _ }
end
/-- Given a finite set `s` and an element `a ∉ s`, the product of `f i` over `i ∈ insert a s` equals
`f a` times the product of `f i` over `i ∈ s`. -/
@[to_additive] lemma finprod_mem_insert (f : α → M) (h : a ∉ s) (hs : s.finite) :
∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i :=
finprod_mem_insert' f h $ hs.inter_of_left _
/-- If `f a = 1` for all `a ∉ s`, then the product of `f i` over `i ∈ insert a s` equals the
product of `f i` over `i ∈ s`. -/
@[to_additive] lemma finprod_mem_insert_of_eq_one_if_not_mem (h : a ∉ s → f a = 1) :
∏ᶠ i ∈ (insert a s), f i = ∏ᶠ i ∈ s, f i :=
begin
refine finprod_mem_inter_mul_support_eq' _ _ _ (λ x hx, ⟨_, or.inr⟩),
rintro (rfl|hxs),
exacts [not_imp_comm.1 h hx, hxs]
end
/-- If `f a = 1`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over
`i ∈ s`. -/
@[to_additive] lemma finprod_mem_insert_one (h : f a = 1) :
∏ᶠ i ∈ (insert a s), f i = ∏ᶠ i ∈ s, f i :=
finprod_mem_insert_of_eq_one_if_not_mem (λ _, h)
/-- If the multiplicative support of `f` is finite, then for every `x` in the domain of `f`,
`f x` divides `finprod f`. -/
lemma finprod_mem_dvd {f : α → N} (a : α) (hf : finite (mul_support f)) :
f a ∣ finprod f :=
begin
by_cases ha : a ∈ mul_support f,
{ rw finprod_eq_prod_of_mul_support_to_finset_subset f hf (set.subset.refl _),
exact finset.dvd_prod_of_mem f ((finite.mem_to_finset hf).mpr ha) },
{ rw nmem_mul_support.mp ha,
exact one_dvd (finprod f) }
end
/-- The product of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a * f b`. -/
@[to_additive] lemma finprod_mem_pair (h : a ≠ b) : ∏ᶠ i ∈ ({a, b} : set α), f i = f a * f b :=
by { rw [finprod_mem_insert, finprod_mem_singleton], exacts [h, finite_singleton b] }
/-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s`
provided that `g` is injective on `s ∩ mul_support (f ∘ g)`. -/
@[to_additive] lemma finprod_mem_image' {s : set β} {g : β → α}
(hg : set.inj_on g (s ∩ mul_support (f ∘ g))) :
∏ᶠ i ∈ (g '' s), f i = ∏ᶠ j ∈ s, f (g j) :=
begin
classical,
by_cases hs : finite (s ∩ mul_support (f ∘ g)),
{ have hg : ∀ (x ∈ hs.to_finset) (y ∈ hs.to_finset), g x = g y → x = y,
by simpa only [hs.mem_to_finset],
rw [finprod_mem_eq_prod _ hs, ← finset.prod_image hg],
refine finprod_mem_eq_prod_of_inter_mul_support_eq f _,
rw [finset.coe_image, hs.coe_to_finset, ← image_inter_mul_support_eq, inter_assoc,
inter_self] },
{ rw [finprod_mem_eq_one_of_infinite hs, finprod_mem_eq_one_of_infinite],
rwa [image_inter_mul_support_eq, infinite_image_iff hg] }
end
/-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s`
provided that `g` is injective on `s`. -/
@[to_additive] lemma finprod_mem_image {β} {s : set β} {g : β → α} (hg : set.inj_on g s) :
∏ᶠ i ∈ (g '' s), f i = ∏ᶠ j ∈ s, f (g j) :=
finprod_mem_image' $ hg.mono $ inter_subset_left _ _
/-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i`
provided that `g` is injective on `mul_support (f ∘ g)`. -/
@[to_additive] lemma finprod_mem_range' {g : β → α} (hg : set.inj_on g (mul_support (f ∘ g))) :
∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) :=
begin
rw [← image_univ, finprod_mem_image', finprod_mem_univ],
rwa univ_inter
end
/-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i`
provided that `g` is injective. -/
@[to_additive] lemma finprod_mem_range {g : β → α} (hg : injective g) :
∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) :=
finprod_mem_range' (hg.inj_on _)
/-- The product of `f i` over `s : set α` is equal to the product of `g j` over `t : set β`
if there exists a function `e : α → β` such that `e` is bijective from `s` to `t` and for all
`x` in `s` we have `f x = g (e x)`. -/
@[to_additive] lemma finprod_mem_eq_of_bij_on {s : set α} {t : set β} {f : α → M} {g : β → M}
(e : α → β) (he₀ : set.bij_on e s t) (he₁ : ∀ x ∈ s, f x = g (e x)) :
∏ᶠ i ∈ s, f i = ∏ᶠ j ∈ t, g j :=
begin
rw [← set.bij_on.image_eq he₀, finprod_mem_image he₀.2.1],
exact finprod_mem_congr rfl he₁
end
@[to_additive] lemma finprod_set_coe_eq_finprod_mem (s : set α) : ∏ᶠ j : s, f j = ∏ᶠ i ∈ s, f i :=
begin
rw [← finprod_mem_range, subtype.range_coe],
exact subtype.coe_injective
end
@[to_additive] lemma finprod_subtype_eq_finprod_cond (p : α → Prop) :
∏ᶠ j : subtype p, f j = ∏ᶠ i (hi : p i), f i :=
finprod_set_coe_eq_finprod_mem {i | p i}
@[to_additive] lemma finprod_mem_inter_mul_diff' (t : set α) (h : (s ∩ mul_support f).finite) :
(∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i :=
begin
rw [← finprod_mem_union', inter_union_diff],
exacts [λ x hx, hx.2.2 hx.1.2, h.subset (λ x hx, ⟨hx.1.1, hx.2⟩),
h.subset (λ x hx, ⟨hx.1.1, hx.2⟩)],
end
@[to_additive] lemma finprod_mem_inter_mul_diff (t : set α) (h : s.finite) :
(∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i :=
finprod_mem_inter_mul_diff' _ $ h.inter_of_left _
/-- A more general version of `finprod_mem_mul_diff` that requires `t ∩ mul_support f` instead of
`t` to be finite. -/
@[to_additive] lemma finprod_mem_mul_diff' (hst : s ⊆ t) (ht : (t ∩ mul_support f).finite) :
(∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i :=
by rw [← finprod_mem_inter_mul_diff' _ ht, inter_eq_self_of_subset_right hst]
/-- Given a finite set `t` and a subset `s` of `t`, the product of `f i` over `i ∈ s`
times the product of `f i` over `t \ s` equals the product of `f i` over `i ∈ t`. -/
@[to_additive] lemma finprod_mem_mul_diff (hst : s ⊆ t) (ht : t.finite) :
(∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i :=
finprod_mem_mul_diff' hst (ht.inter_of_left _)
/-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type,
the product of `f a` over the union `⋃ i, t i` is equal to the product over all indexes `i`
of the products of `f a` over `a ∈ t i`. -/
@[to_additive] lemma finprod_mem_Union [fintype ι] {t : ι → set α}
(h : pairwise (disjoint on t)) (ht : ∀ i, (t i).finite) :
∏ᶠ a ∈ (⋃ i : ι, t i), f a = ∏ᶠ i, (∏ᶠ a ∈ t i, f a) :=
begin
lift t to ι → finset α using ht,
classical,
rw [← bUnion_univ, ← finset.coe_univ, ← finset.coe_bUnion,
finprod_mem_coe_finset, finset.prod_bUnion],
{ simp only [finprod_mem_coe_finset, finprod_eq_prod_of_fintype] },
{ exact λ x _ y _ hxy, finset.disjoint_iff_disjoint_coe.2 (h x y hxy) }
end
/-- Given a family of sets `t : ι → set α`, a finite set `I` in the index type such that all
sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then
the product of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the product over `i ∈ I`
of the products of `f a` over `a ∈ t i`. -/
@[to_additive] lemma finprod_mem_bUnion {I : set ι} {t : ι → set α}
(h : pairwise_on I (disjoint on t)) (hI : I.finite) (ht : ∀ i ∈ I, (t i).finite) :
∏ᶠ a ∈ ⋃ x ∈ I, t x, f a = ∏ᶠ i ∈ I, ∏ᶠ j ∈ t i, f j :=
begin
haveI := hI.fintype,
rw [bUnion_eq_Union, finprod_mem_Union, ← finprod_set_coe_eq_finprod_mem],
exacts [λ x y hxy, h x x.2 y y.2 (subtype.coe_injective.ne hxy), λ b, ht b b.2]
end
/-- If `t` is a finite set of pairwise disjoint finite sets, then the product of `f a`
over `a ∈ ⋃₀ t` is the product over `s ∈ t` of the products of `f a` over `a ∈ s`. -/
@[to_additive] lemma finprod_mem_sUnion {t : set (set α)} (h : pairwise_on t disjoint)
(ht₀ : t.finite) (ht₁ : ∀ x ∈ t, set.finite x):
∏ᶠ a ∈ ⋃₀ t, f a = ∏ᶠ s ∈ t, ∏ᶠ a ∈ s, f a :=
by rw [set.sUnion_eq_bUnion, finprod_mem_bUnion h ht₀ ht₁]
/-- If `s : set α` and `t : set β` are finite sets, then the product over `s` commutes
with the product over `t`. -/
@[to_additive] lemma finprod_mem_comm {s : set α} {t : set β}
(f : α → β → M) (hs : s.finite) (ht : t.finite) :
∏ᶠ i ∈ s, ∏ᶠ j ∈ t, f i j = ∏ᶠ j ∈ t, ∏ᶠ i ∈ s, f i j :=
begin
lift s to finset α using hs, lift t to finset β using ht,
simp only [finprod_mem_coe_finset],
exact finset.prod_comm
end
/-- To prove a property of a finite product, it suffices to prove that the property is
multiplicative and holds on multipliers. -/
@[to_additive] lemma finprod_mem_induction (p : M → Prop) (hp₀ : p 1)
(hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ x ∈ s, p $ f x) :
p (∏ᶠ i ∈ s, f i) :=
finprod_induction _ hp₀ hp₁ $ λ x, finprod_induction _ hp₀ hp₁ $ hp₂ x
lemma finprod_cond_nonneg {R : Type*} [ordered_comm_semiring R] {p : α → Prop} {f : α → R}
(hf : ∀ x, p x → 0 ≤ f x) :
0 ≤ ∏ᶠ x (h : p x), f x :=
finprod_nonneg $ λ x, finprod_nonneg $ hf x
@[to_additive]
lemma single_le_finprod {M : Type*} [ordered_comm_monoid M] (i : α) {f : α → M}
(hf : finite (mul_support f)) (h : ∀ j, 1 ≤ f j) :
f i ≤ ∏ᶠ j, f j :=
by classical;
calc f i ≤ ∏ j in insert i hf.to_finset, f j :
finset.single_le_prod' (λ j hj, h j) (finset.mem_insert_self _ _)
... = ∏ᶠ j, f j :
(finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_insert _ _)).symm
lemma finprod_eq_zero {M₀ : Type*} [comm_monoid_with_zero M₀] (f : α → M₀) (x : α)
(hx : f x = 0) (hf : finite (mul_support f)) :
∏ᶠ x, f x = 0 :=
begin
nontriviality,
rw [finprod_eq_prod f hf],
refine finset.prod_eq_zero (hf.mem_to_finset.2 _) hx,
simp [hx]
end
@[to_additive] lemma finprod_prod_comm (s : finset β) (f : α → β → M)
(h : ∀ b ∈ s, (mul_support (λ a, f a b)).finite) :
∏ᶠ a : α, ∏ b in s, f a b = ∏ b in s, ∏ᶠ a : α, f a b :=
begin
have hU : mul_support (λ a, ∏ b in s, f a b) ⊆
(s.finite_to_set.bUnion (λ b hb, h b (finset.mem_coe.1 hb))).to_finset,
{ rw finite.coe_to_finset,
intros x hx,
simp only [exists_prop, mem_Union, ne.def, mem_mul_support, finset.mem_coe],
contrapose! hx,
rw [mem_mul_support, not_not, finset.prod_congr rfl hx, finset.prod_const_one] },
rw [finprod_eq_prod_of_mul_support_subset _ hU, finset.prod_comm],
refine finset.prod_congr rfl (λ b hb, (finprod_eq_prod_of_mul_support_subset _ _).symm),
intros a ha,
simp only [finite.coe_to_finset, mem_Union],
exact ⟨b, hb, ha⟩
end
@[to_additive] lemma prod_finprod_comm (s : finset α) (f : α → β → M)
(h : ∀ a ∈ s, (mul_support (f a)).finite) :
∏ a in s, ∏ᶠ b : β, f a b = ∏ᶠ b : β, ∏ a in s, f a b :=
(finprod_prod_comm s (λ b a, f a b) h).symm
lemma mul_finsum {R : Type*} [semiring R] (f : α → R) (r : R)
(h : (function.support f).finite) :
r * ∑ᶠ a : α, f a = ∑ᶠ a : α, r * f a :=
(add_monoid_hom.mul_left r).map_finsum h
lemma finsum_mul {R : Type*} [semiring R] (f : α → R) (r : R)
(h : (function.support f).finite) :
(∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r :=
(add_monoid_hom.mul_right r).map_finsum h
@[to_additive] lemma finset.mul_support_of_fiberwise_prod_subset_image [decidable_eq β]
(s : finset α) (f : α → M) (g : α → β) :
mul_support (λ b, (s.filter (λ a, g a = b)).prod f) ⊆ s.image g :=
begin
simp only [finset.coe_image, set.mem_image, finset.mem_coe, function.support_subset_iff],
intros b h,
suffices : (s.filter (λ (a : α), g a = b)).nonempty,
{ simpa only [s.fiber_nonempty_iff_mem_image g b, finset.mem_image, exists_prop], },
exact finset.nonempty_of_prod_ne_one h,
end
/-- Note that `b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd` iff `(a, b) ∈ s` so we can
simplify the right hand side of this lemma. However the form stated here is more useful for
iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/
@[to_additive] lemma finprod_mem_finset_product' [decidable_eq α] [decidable_eq β]
(s : finset (α × β)) (f : α × β → M) :
∏ᶠ ab (h : ab ∈ s), f ab =
∏ᶠ a b (h : b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd), f (a, b) :=
begin
have : ∀ a, ∏ (i : β) in (s.filter (λ ab, prod.fst ab = a)).image prod.snd, f (a, i) =
(finset.filter (λ ab, prod.fst ab = a) s).prod f,
{ intros a, apply finset.prod_bij (λ b _, (a, b)); finish, },
rw finprod_mem_finset_eq_prod,
simp_rw [finprod_mem_finset_eq_prod, this],
rw [finprod_eq_prod_of_mul_support_subset _
(s.mul_support_of_fiberwise_prod_subset_image f prod.fst),
← finset.prod_fiberwise_of_maps_to _ f],
finish,
end
/-- See also `finprod_mem_finset_product'`. -/
@[to_additive] lemma finprod_mem_finset_product (s : finset (α × β)) (f : α × β → M) :
∏ᶠ ab (h : ab ∈ s), f ab = ∏ᶠ a b (h : (a, b) ∈ s), f (a, b) :=
by { classical, rw finprod_mem_finset_product', simp, }
@[to_additive] lemma finprod_mem_finset_product₃ {γ : Type*}
(s : finset (α × β × γ)) (f : α × β × γ → M) :
∏ᶠ abc (h : abc ∈ s), f abc = ∏ᶠ a b c (h : (a, b, c) ∈ s), f (a, b, c) :=
by { classical, rw finprod_mem_finset_product', simp_rw finprod_mem_finset_product', simp, }
@[to_additive] lemma finprod_curry (f : α × β → M) (hf : (mul_support f).finite) :
∏ᶠ ab, f ab = ∏ᶠ a b, f (a, b) :=
begin
have h₁ : ∀ a, ∏ᶠ (h : a ∈ hf.to_finset), f a = f a, { simp, },
have h₂ : ∏ᶠ a, f a = ∏ᶠ a (h : a ∈ hf.to_finset), f a, { simp, },
simp_rw [h₂, finprod_mem_finset_product, h₁],
end
@[to_additive] lemma finprod_curry₃ {γ : Type*} (f : α × β × γ → M) (h : (mul_support f).finite) :
∏ᶠ abc, f abc = ∏ᶠ a b c, f (a, b, c) :=
by { rw finprod_curry f h, congr, ext a, rw finprod_curry, simp [h], }
@[to_additive]
lemma finprod_dmem {s : set α} [decidable_pred (∈ s)] (f : (Π (a : α), a ∈ s → M)) :
∏ᶠ (a : α) (h : a ∈ s), f a h = ∏ᶠ (a : α) (h : a ∈ s), if h' : a ∈ s then f a h' else 1 :=
finprod_congr (λ a, finprod_congr (λ ha, (dif_pos ha).symm))
@[to_additive]
lemma finprod_emb_domain' {f : α → β} (hf : function.injective f)
[decidable_pred (∈ set.range f)] (g : α → M) :
∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a :=
begin
simp_rw [← finprod_eq_dif],
rw [finprod_dmem, finprod_mem_range hf, finprod_congr (λ a, _)],
rw [dif_pos (set.mem_range_self a), hf (classical.some_spec (set.mem_range_self a))]
end
@[to_additive]
lemma finprod_emb_domain (f : α ↪ β) [decidable_pred (∈ set.range f)] (g : α → M) :
∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a :=
finprod_emb_domain' f.injective g
end type
|
42d30ca934ec9de9d37938df4277b9226d785744 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/idempotents/basic.lean | a19ef233af3e8fb4e253b4b31eeac907661964a5 | [
"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 | 7,613 | lean | /-
Copyright (c) 2022 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou
-/
import category_theory.abelian.basic
/-!
# Idempotent complete categories
In this file, we define the notion of idempotent complete categories
(also known as Karoubian categories, or pseudoabelian in the case of
preadditive categories).
## Main definitions
- `is_idempotent_complete C` expresses that `C` is idempotent complete, i.e.
all idempotents in `C` split. Other characterisations of idempotent completeness are given
by `is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent` and
`is_idempotent_complete_iff_idempotents_have_kernels`.
- `is_idempotent_complete_of_abelian` expresses that abelian categories are
idempotent complete.
- `is_idempotent_complete_iff_of_equivalence` expresses that if two categories `C` and `D`
are equivalent, then `C` is idempotent complete iff `D` is.
- `is_idempotent_complete_iff_opposite` expresses that `Cᵒᵖ` is idempotent complete
iff `C` is.
## References
* [Stacks: Karoubian categories] https://stacks.math.columbia.edu/tag/09SF
-/
open category_theory
open category_theory.category
open category_theory.limits
open category_theory.preadditive
open opposite
namespace category_theory
variables (C : Type*) [category C]
/-- A category is idempotent complete iff all idempotent endomorphisms `p`
split as a composition `p = e ≫ i` with `i ≫ e = 𝟙 _` -/
class is_idempotent_complete : Prop :=
(idempotents_split : ∀ (X : C) (p : X ⟶ X), p ≫ p = p →
∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p)
namespace idempotents
/-- A category is idempotent complete iff for all idempotent endomorphisms,
the equalizer of the identity and this idempotent exists. -/
lemma is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent :
is_idempotent_complete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → has_equalizer (𝟙 X) p :=
begin
split,
{ introI,
intros X p hp,
rcases is_idempotent_complete.idempotents_split X p hp with ⟨Y, i, e, ⟨h₁, h₂⟩⟩,
exact ⟨nonempty.intro
{ cone := fork.of_ι i
(show i ≫ 𝟙 X = i ≫ p, by rw [comp_id, ← h₂, ← assoc, h₁, id_comp]),
is_limit := begin
apply fork.is_limit.mk',
intro s,
refine ⟨s.ι ≫ e, _⟩,
split,
{ erw [assoc, h₂, ← limits.fork.condition s, comp_id], },
{ intros m hm,
rw fork.ι_of_ι at hm,
rw [← hm],
simp only [← hm, assoc, h₁],
exact (comp_id m).symm }
end }⟩, },
{ intro h,
refine ⟨_⟩,
intros X p hp,
haveI := h X p hp,
use equalizer (𝟙 X) p,
use equalizer.ι (𝟙 X) p,
use equalizer.lift p (show p ≫ 𝟙 X = p ≫ p, by rw [hp, comp_id]),
split,
{ ext,
rw [assoc, equalizer.lift_ι, id_comp],
conv { to_rhs, erw [← comp_id (equalizer.ι (𝟙 X) p)], },
exact (limits.fork.condition (equalizer.fork (𝟙 X) p)).symm, },
{ rw [equalizer.lift_ι], }, }
end
variables {C}
/-- In a preadditive category, when `p : X ⟶ X` is idempotent,
then `𝟙 X - p` is also idempotent. -/
lemma idem_of_id_sub_idem [preadditive C]
{X : C} (p : X ⟶ X) (hp : p ≫ p = p) :
(𝟙 _ - p) ≫ (𝟙 _ - p) = (𝟙 _ - p) :=
by simp only [comp_sub, sub_comp, id_comp, comp_id, hp, sub_self, sub_zero]
variables (C)
/-- A preadditive category is pseudoabelian iff all idempotent endomorphisms have a kernel. -/
lemma is_idempotent_complete_iff_idempotents_have_kernels [preadditive C] :
is_idempotent_complete C ↔ ∀ (X : C) (p : X ⟶ X), p ≫ p = p → has_kernel p :=
begin
rw is_idempotent_complete_iff_has_equalizer_of_id_and_idempotent,
split,
{ intros h X p hp,
haveI := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp),
convert has_kernel_of_has_equalizer (𝟙 X) (𝟙 X - p),
rw [sub_sub_cancel], },
{ intros h X p hp,
haveI : has_kernel (𝟙 _ - p) := h X (𝟙 _ - p) (idem_of_id_sub_idem p hp),
apply preadditive.has_equalizer_of_has_kernel, },
end
/-- An abelian category is idempotent complete. -/
@[priority 100]
instance is_idempotent_complete_of_abelian (D : Type*) [category D] [abelian D] :
is_idempotent_complete D :=
by { rw is_idempotent_complete_iff_idempotents_have_kernels, intros, apply_instance, }
variables {C}
lemma split_imp_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X')
(hpp' : p ≫ φ.hom = φ.hom ≫ p')
(h : ∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) :
(∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p') :=
begin
rcases h with ⟨Y, i, e, ⟨h₁, h₂⟩⟩,
use [Y, i ≫ φ.hom, φ.inv ≫ e],
split,
{ slice_lhs 2 3 { rw φ.hom_inv_id, },
rw [id_comp, h₁], },
{ slice_lhs 2 3 { rw h₂, },
rw [hpp', ← assoc, φ.inv_hom_id, id_comp], }
end
lemma split_iff_of_iso {X X' : C} (φ : X ≅ X') (p : X ⟶ X) (p' : X' ⟶ X')
(hpp' : p ≫ φ.hom = φ.hom ≫ p') :
(∃ (Y : C) (i : Y ⟶ X) (e : X ⟶ Y), i ≫ e = 𝟙 Y ∧ e ≫ i = p) ↔
(∃ (Y' : C) (i' : Y' ⟶ X') (e' : X' ⟶ Y'), i' ≫ e' = 𝟙 Y' ∧ e' ≫ i' = p') :=
begin
split,
{ exact split_imp_of_iso φ p p' hpp', },
{ apply split_imp_of_iso φ.symm p' p,
rw [← comp_id p, ← φ.hom_inv_id],
slice_rhs 2 3 { rw hpp', },
slice_rhs 1 2 { erw φ.inv_hom_id, },
simpa only [id_comp], },
end
lemma equivalence.is_idempotent_complete {D : Type*} [category D] (ε : C ≌ D)
(h : is_idempotent_complete C) : is_idempotent_complete D :=
begin
refine ⟨_⟩,
intros X' p hp,
let φ := ε.counit_iso.symm.app X',
erw split_iff_of_iso φ p (φ.inv ≫ p ≫ φ.hom)
(by { slice_rhs 1 2 { rw φ.hom_inv_id, }, rw id_comp,}),
rcases is_idempotent_complete.idempotents_split (ε.inverse.obj X') (ε.inverse.map p)
(by rw [← ε.inverse.map_comp, hp]) with ⟨Y, i, e, ⟨h₁,h₂⟩⟩,
use [ε.functor.obj Y, ε.functor.map i, ε.functor.map e],
split,
{ rw [← ε.functor.map_comp, h₁, ε.functor.map_id], },
{ simpa only [← ε.functor.map_comp, h₂, equivalence.fun_inv_map], },
end
/-- If `C` and `D` are equivalent categories, that `C` is idempotent complete iff `D` is. -/
lemma is_idempotent_complete_iff_of_equivalence {D : Type*} [category D] (ε : C ≌ D) :
is_idempotent_complete C ↔ is_idempotent_complete D :=
begin
split,
{ exact equivalence.is_idempotent_complete ε, },
{ exact equivalence.is_idempotent_complete ε.symm, },
end
lemma is_idempotent_complete_of_is_idempotent_complete_opposite
(h : is_idempotent_complete Cᵒᵖ) : is_idempotent_complete C :=
begin
refine ⟨_⟩,
intros X p hp,
rcases is_idempotent_complete.idempotents_split (op X) p.op
(by rw [← op_comp, hp]) with ⟨Y, i, e, ⟨h₁, h₂⟩⟩,
use [Y.unop, e.unop, i.unop],
split,
{ simpa only [← unop_comp, h₁], },
{ simpa only [← unop_comp, h₂], },
end
lemma is_idempotent_complete_iff_opposite :
is_idempotent_complete Cᵒᵖ ↔ is_idempotent_complete C :=
begin
split,
{ exact is_idempotent_complete_of_is_idempotent_complete_opposite, },
{ intro h,
apply is_idempotent_complete_of_is_idempotent_complete_opposite,
rw is_idempotent_complete_iff_of_equivalence (op_op_equivalence C),
exact h, },
end
instance [is_idempotent_complete C] : is_idempotent_complete (Cᵒᵖ) :=
by rwa is_idempotent_complete_iff_opposite
end idempotents
end category_theory
|
779e707de1e0993f5d94795f8ba88a2e1a0e18d6 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/print_ax3.lean | 753e702c756d8e7d6a916aae7b5d5fa0c5283d27 | [
"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 | 244 | lean | open subtype
theorem foo1 : 0 = (0:num) :=
rfl
theorem foo2 : 0 = (0:num) :=
rfl
theorem foo3 : 0 = (0:num) :=
foo2
definition foo4 : 0 = (0:num) :=
eq.trans foo2 foo1
print axioms foo4
print "------"
print axioms
print "------"
print foo3
|
1cfb8378903971a0a0ce21064b8f5efe255d3174 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/lean/run/structInst.lean | 07704114612ebb78ad6dc972e50df57cf6a77467 | [
"Apache-2.0"
] | permissive | walterhu1015/lean4 | b2c71b688975177402758924eaa513475ed6ce72 | 2214d81e84646a905d0b20b032c89caf89c737ad | refs/heads/master | 1,671,342,096,906 | 1,599,695,985,000 | 1,599,695,985,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,025 | lean | namespace Ex1
structure A :=
(x : Nat)
structure B extends A :=
(y : Nat := x + 2) (x := y + 1)
structure C extends B :=
(z : Nat) (x := z + 10)
end Ex1
namespace Ex2
structure A :=
(x : Nat) (y : Nat)
structure B extends A :=
(z : Nat := x + 1) (y := z + x)
end Ex2
namespace Ex3
structure A :=
(x : Nat)
structure B extends A :=
(y : Nat := x + 2) (x := y + 1)
structure C extends B :=
(z : Nat := 2*y) (x := z + 2) (y := z + 3)
end Ex3
namespace Ex4
structure A :=
(x : Nat)
structure B extends A :=
(y : Nat := x + 1) (x := y + 1)
structure C extends B :=
(z : Nat := 2*y) (x := z + 3)
end Ex4
new_frontend
namespace Ex1
#check { y := 1 : B }
#check { z := 1 : C }
end Ex1
namespace Ex2
#check { x := 1 : B }
end Ex2
namespace Ex3
#check { x := 1 : C }
#check { y := 1 : C }
#check { z := 1 : C }
end Ex3
namespace Ex4
#check { x := 1 : C } -- works
#check { y := 1 : C } -- works
#check { z := 1 : C } -- works
#check { z := 1, x := 2 : C } -- works
#check { y := 1 : B } -- works
end Ex4
|
1efff624902973d3138b35b166c4fa965112a2d6 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/setoid/basic.lean | 4e69c1aae666e8e3a60e8a0a0b3dca3b043d8f55 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 18,664 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston, Bryan Gin-ge Chen
-/
import logic.relation
import order.galois_connection
/-!
# Equivalence relations
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the complete lattice of equivalence relations on a type, results about the
inductively defined equivalence closure of a binary relation, and the analogues of some isomorphism
theorems for quotients of arbitrary types.
## Implementation notes
The function `rel` and lemmas ending in ' make it easier to talk about different
equivalence relations on the same type.
The complete lattice instance for equivalence relations could have been defined by lifting
the Galois insertion of equivalence relations on α into binary relations on α, and then using
`complete_lattice.copy` to define a complete lattice instance with more appropriate
definitional equalities (a similar example is `filter.complete_lattice` in
`order/filter/basic.lean`). This does not save space, however, and is less clear.
Partitions are not defined as a separate structure here; users are encouraged to
reason about them using the existing `setoid` and its infrastructure.
## Tags
setoid, equivalence, iseqv, relation, equivalence relation
-/
variables {α : Type*} {β : Type*}
/-- A version of `setoid.r` that takes the equivalence relation as an explicit argument. -/
def setoid.rel (r : setoid α) : α → α → Prop := @setoid.r _ r
instance setoid.decidable_rel (r : setoid α) [h : decidable_rel r.r] : decidable_rel r.rel := h
/-- A version of `quotient.eq'` compatible with `setoid.rel`, to make rewriting possible. -/
lemma quotient.eq_rel {r : setoid α} {x y} :
(quotient.mk' x : quotient r) = quotient.mk' y ↔ r.rel x y := quotient.eq
namespace setoid
@[ext] lemma ext' {r s : setoid α} (H : ∀ a b, r.rel a b ↔ s.rel a b) :
r = s := ext H
lemma ext_iff {r s : setoid α} : r = s ↔ ∀ a b, r.rel a b ↔ s.rel a b :=
⟨λ h a b, h ▸ iff.rfl, ext'⟩
/-- Two equivalence relations are equal iff their underlying binary operations are equal. -/
theorem eq_iff_rel_eq {r₁ r₂ : setoid α} : r₁ = r₂ ↔ r₁.rel = r₂.rel :=
⟨λ h, h ▸ rfl, λ h, setoid.ext' $ λ x y, h ▸ iff.rfl⟩
/-- Defining `≤` for equivalence relations. -/
instance : has_le (setoid α) := ⟨λ r s, ∀ ⦃x y⦄, r.rel x y → s.rel x y⟩
theorem le_def {r s : setoid α} : r ≤ s ↔ ∀ {x y}, r.rel x y → s.rel x y := iff.rfl
@[refl] lemma refl' (r : setoid α) (x) : r.rel x x := r.2.1 x
@[symm] lemma symm' (r : setoid α) : ∀ {x y}, r.rel x y → r.rel y x := λ _ _ h, r.2.2.1 h
@[trans] lemma trans' (r : setoid α) : ∀ {x y z}, r.rel x y → r.rel y z → r.rel x z :=
λ _ _ _ hx, r.2.2.2 hx
lemma comm' (s : setoid α) {x y} : s.rel x y ↔ s.rel y x :=
⟨s.symm', s.symm'⟩
/-- The kernel of a function is an equivalence relation. -/
def ker (f : α → β) : setoid α :=
⟨(=) on f, eq_equivalence.comap f⟩
/-- The kernel of the quotient map induced by an equivalence relation r equals r. -/
@[simp] lemma ker_mk_eq (r : setoid α) : ker (@quotient.mk _ r) = r :=
ext' $ λ x y, quotient.eq
lemma ker_apply_mk_out {f : α → β} (a : α) :
f (by haveI := setoid.ker f; exact ⟦a⟧.out) = f a :=
@quotient.mk_out _ (setoid.ker f) a
lemma ker_apply_mk_out' {f : α → β} (a : α) :
f ((quotient.mk' a : quotient $ setoid.ker f).out') = f a :=
@quotient.mk_out' _ (setoid.ker f) a
lemma ker_def {f : α → β} {x y : α} : (ker f).rel x y ↔ f x = f y := iff.rfl
/-- Given types `α`, `β`, the product of two equivalence relations `r` on `α` and `s` on `β`:
`(x₁, x₂), (y₁, y₂) ∈ α × β` are related by `r.prod s` iff `x₁` is related to `y₁`
by `r` and `x₂` is related to `y₂` by `s`. -/
protected def prod (r : setoid α) (s : setoid β) : setoid (α × β) :=
{ r := λ x y, r.rel x.1 y.1 ∧ s.rel x.2 y.2,
iseqv := ⟨λ x, ⟨r.refl' x.1, s.refl' x.2⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩ }
/-- The infimum of two equivalence relations. -/
instance : has_inf (setoid α) :=
⟨λ r s, ⟨λ x y, r.rel x y ∧ s.rel x y, ⟨λ x, ⟨r.refl' x, s.refl' x⟩,
λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩,
λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩⟩⟩
/-- The infimum of 2 equivalence relations r and s is the same relation as the infimum
of the underlying binary operations. -/
lemma inf_def {r s : setoid α} : (r ⊓ s).rel = r.rel ⊓ s.rel := rfl
theorem inf_iff_and {r s : setoid α} {x y} :
(r ⊓ s).rel x y ↔ r.rel x y ∧ s.rel x y := iff.rfl
/-- The infimum of a set of equivalence relations. -/
instance : has_Inf (setoid α) :=
⟨λ S, ⟨λ x y, ∀ r ∈ S, rel r x y,
⟨λ x r hr, r.refl' x, λ _ _ h r hr, r.symm' $ h r hr,
λ _ _ _ h1 h2 r hr, r.trans' (h1 r hr) $ h2 r hr⟩⟩⟩
/-- The underlying binary operation of the infimum of a set of equivalence relations
is the infimum of the set's image under the map to the underlying binary operation. -/
theorem Inf_def {s : set (setoid α)} : (Inf s).rel = Inf (rel '' s) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
instance : partial_order (setoid α) :=
{ le := (≤),
lt := λ r s, r ≤ s ∧ ¬s ≤ r,
le_refl := λ _ _ _, id,
le_trans := λ _ _ _ hr hs _ _ h, hs $ hr h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ r s h1 h2, setoid.ext' $ λ x y, ⟨λ h, h1 h, λ h, h2 h⟩ }
/-- The complete lattice of equivalence relations on a type, with bottom element `=`
and top element the trivial equivalence relation. -/
instance complete_lattice : complete_lattice (setoid α) :=
{ inf := has_inf.inf,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ h1 h2 _ _ h, ⟨h1 h, h2 h⟩,
top := ⟨λ _ _, true, ⟨λ _, trivial, λ _ _ h, h, λ _ _ _ h1 h2, h1⟩⟩,
le_top := λ _ _ _ _, trivial,
bot := ⟨(=), ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h1 h2, h1.trans h2⟩⟩,
bot_le := λ r x y h, h ▸ r.2.1 x,
.. complete_lattice_of_Inf (setoid α) $ assume s,
⟨λ r hr x y h, h _ hr, λ r hr x y h r' hr', hr hr' h⟩ }
@[simp]
lemma top_def : (⊤ : setoid α).rel = ⊤ := rfl
@[simp]
lemma bot_def : (⊥ : setoid α).rel = (=) := rfl
lemma eq_top_iff {s : setoid α} : s = (⊤ : setoid α) ↔ ∀ x y : α, s.rel x y :=
by simp [eq_top_iff, setoid.le_def, setoid.top_def, pi.top_apply]
/-- The inductively defined equivalence closure of a binary relation r is the infimum
of the set of all equivalence relations containing r. -/
theorem eqv_gen_eq (r : α → α → Prop) :
eqv_gen.setoid r = Inf {s : setoid α | ∀ ⦃x y⦄, r x y → s.rel x y} :=
le_antisymm
(λ _ _ H, eqv_gen.rec (λ _ _ h _ hs, hs h) (refl' _)
(λ _ _ _, symm' _) (λ _ _ _ _ _, trans' _) H)
(Inf_le $ λ _ _ h, eqv_gen.rel _ _ h)
/-- The supremum of two equivalence relations r and s is the equivalence closure of the binary
relation `x is related to y by r or s`. -/
lemma sup_eq_eqv_gen (r s : setoid α) :
r ⊔ s = eqv_gen.setoid (λ x y, r.rel x y ∨ s.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of 2 equivalence relations r and s is the equivalence closure of the
supremum of the underlying binary operations. -/
lemma sup_def {r s : setoid α} : r ⊔ s = eqv_gen.setoid (r.rel ⊔ s.rel) :=
by rw sup_eq_eqv_gen; refl
/-- The supremum of a set S of equivalence relations is the equivalence closure of the binary
relation `there exists r ∈ S relating x and y`. -/
lemma Sup_eq_eqv_gen (S : set (setoid α)) :
Sup S = eqv_gen.setoid (λ x y, ∃ r : setoid α, r ∈ S ∧ r.rel x y) :=
begin
rw eqv_gen_eq,
apply congr_arg Inf,
simp only [upper_bounds, le_def, and_imp, exists_imp_distrib],
ext,
exact ⟨λ H x y r hr, H hr, λ H r hr x y, H r hr⟩
end
/-- The supremum of a set of equivalence relations is the equivalence closure of the
supremum of the set's image under the map to the underlying binary operation. -/
lemma Sup_def {s : set (setoid α)} : Sup s = eqv_gen.setoid (Sup (rel '' s)) :=
begin
rw [Sup_eq_eqv_gen, Sup_image],
congr' with x y,
simp only [supr_apply, supr_Prop_eq, exists_prop]
end
/-- The equivalence closure of an equivalence relation r is r. -/
@[simp] lemma eqv_gen_of_setoid (r : setoid α) : eqv_gen.setoid r.r = r :=
le_antisymm (by rw eqv_gen_eq; exact Inf_le (λ _ _, id)) eqv_gen.rel
/-- Equivalence closure is idempotent. -/
@[simp] lemma eqv_gen_idem (r : α → α → Prop) :
eqv_gen.setoid (eqv_gen.setoid r).rel = eqv_gen.setoid r :=
eqv_gen_of_setoid _
/-- The equivalence closure of a binary relation r is contained in any equivalence
relation containing r. -/
theorem eqv_gen_le {r : α → α → Prop} {s : setoid α} (h : ∀ x y, r x y → s.rel x y) :
eqv_gen.setoid r ≤ s :=
by rw eqv_gen_eq; exact Inf_le h
/-- Equivalence closure of binary relations is monotone. -/
theorem eqv_gen_mono {r s : α → α → Prop} (h : ∀ x y, r x y → s x y) :
eqv_gen.setoid r ≤ eqv_gen.setoid s :=
eqv_gen_le $ λ _ _ hr, eqv_gen.rel _ _ $ h _ _ hr
/-- There is a Galois insertion of equivalence relations on α into binary relations
on α, with equivalence closure the lower adjoint. -/
def gi : @galois_insertion (α → α → Prop) (setoid α) _ _ eqv_gen.setoid rel :=
{ choice := λ r h, eqv_gen.setoid r,
gc := λ r s, ⟨λ H _ _ h, H $ eqv_gen.rel _ _ h, λ H, eqv_gen_of_setoid s ▸ eqv_gen_mono H⟩,
le_l_u := λ x, (eqv_gen_of_setoid x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
open function
/-- A function from α to β is injective iff its kernel is the bottom element of the complete lattice
of equivalence relations on α. -/
theorem injective_iff_ker_bot (f : α → β) :
injective f ↔ ker f = ⊥ :=
(@eq_bot_iff (setoid α) _ _ (ker f)).symm
/-- The elements related to x ∈ α by the kernel of f are those in the preimage of f(x) under f. -/
lemma ker_iff_mem_preimage {f : α → β} {x y} : (ker f).rel x y ↔ x ∈ f ⁻¹' {f y} :=
iff.rfl
/-- Equivalence between functions `α → β` such that `r x y → f x = f y` and functions
`quotient r → β`. -/
def lift_equiv (r : setoid α) : {f : α → β // r ≤ ker f} ≃ (quotient r → β) :=
{ to_fun := λ f, quotient.lift (f : α → β) f.2,
inv_fun := λ f, ⟨f ∘ quotient.mk, λ x y h, by simp [ker_def, quotient.sound h]⟩,
left_inv := λ ⟨f, hf⟩, subtype.eq $ funext $ λ x, rfl,
right_inv := λ f, funext $ λ x, quotient.induction_on' x $ λ x, rfl }
/-- The uniqueness part of the universal property for quotients of an arbitrary type. -/
theorem lift_unique {r : setoid α} {f : α → β} (H : r ≤ ker f) (g : quotient r → β)
(Hg : f = g ∘ quotient.mk) : quotient.lift f H = g :=
begin
ext ⟨x⟩,
erw [quotient.lift_mk f H, Hg],
refl
end
/-- Given a map f from α to β, the natural map from the quotient of α by the kernel of f is
injective. -/
lemma ker_lift_injective (f : α → β) : injective (@quotient.lift _ _ (ker f) f (λ _ _ h, h)) :=
λ x y, quotient.induction_on₂' x y $ λ a b h, quotient.sound' h
/-- Given a map f from α to β, the kernel of f is the unique equivalence relation on α whose
induced map from the quotient of α to β is injective. -/
lemma ker_eq_lift_of_injective {r : setoid α} (f : α → β) (H : ∀ x y, r.rel x y → f x = f y)
(h : injective (quotient.lift f H)) : ker f = r :=
le_antisymm
(λ x y hk, quotient.exact $ h $ show quotient.lift f H ⟦x⟧ = quotient.lift f H ⟦y⟧, from hk)
H
variables (r : setoid α) (f : α → β)
/-- The first isomorphism theorem for sets: the quotient of α by the kernel of a function f
bijects with f's image. -/
noncomputable def quotient_ker_equiv_range :
quotient (ker f) ≃ set.range f :=
equiv.of_bijective (@quotient.lift _ (set.range f) (ker f)
(λ x, ⟨f x, set.mem_range_self x⟩) $ λ _ _ h, subtype.ext_val h)
⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨@quotient.mk _ (ker f) z, by rw quotient.lift_mk; exact subtype.ext_iff_val.2 hz⟩⟩
/-- If `f` has a computable right-inverse, then the quotient by its kernel is equivalent to its
domain. -/
@[simps]
def quotient_ker_equiv_of_right_inverse (g : β → α) (hf : function.right_inverse g f) :
quotient (ker f) ≃ β :=
{ to_fun := λ a, quotient.lift_on' a f $ λ _ _, id,
inv_fun := λ b, quotient.mk' (g b),
left_inv := λ a, quotient.induction_on' a $ λ a, quotient.sound' $ by exact hf (f a),
right_inv := hf }
/-- The quotient of α by the kernel of a surjective function f bijects with f's codomain.
If a specific right-inverse of `f` is known, `setoid.quotient_ker_equiv_of_right_inverse` can be
definitionally more useful. -/
noncomputable def quotient_ker_equiv_of_surjective (hf : surjective f) :
quotient (ker f) ≃ β :=
quotient_ker_equiv_of_right_inverse _ (function.surj_inv hf) (right_inverse_surj_inv hf)
variables {r f}
/-- Given a function `f : α → β` and equivalence relation `r` on `α`, the equivalence
closure of the relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are
related to the elements of `f⁻¹(y)` by `r`.' -/
def map (r : setoid α) (f : α → β) : setoid β :=
eqv_gen.setoid $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b
/-- Given a surjective function f whose kernel is contained in an equivalence relation r, the
equivalence relation on f's codomain defined by x ≈ y ↔ the elements of f⁻¹(x) are related to
the elements of f⁻¹(y) by r. -/
def map_of_surjective (r) (f : α → β) (h : ker f ≤ r) (hf : surjective f) :
setoid β :=
⟨λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b,
⟨λ x, let ⟨y, hy⟩ := hf x in ⟨y, y, hy, hy, r.refl' y⟩,
λ _ _ ⟨x, y, hx, hy, h⟩, ⟨y, x, hy, hx, r.symm' h⟩,
λ _ _ _ ⟨x, y, hx, hy, h₁⟩ ⟨y', z, hy', hz, h₂⟩,
⟨x, z, hx, hz, r.trans' h₁ $ r.trans' (h $ by rwa ←hy' at hy) h₂⟩⟩⟩
/-- A special case of the equivalence closure of an equivalence relation r equalling r. -/
lemma map_of_surjective_eq_map (h : ker f ≤ r) (hf : surjective f) :
map r f = map_of_surjective r f h hf :=
by rw ←eqv_gen_of_setoid (map_of_surjective r f h hf); refl
/-- Given a function `f : α → β`, an equivalence relation `r` on `β` induces an equivalence
relation on `α` defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `r`'.
See note [reducible non-instances]. -/
@[reducible]
def comap (f : α → β) (r : setoid β) : setoid α :=
⟨r.rel on f, r.iseqv.comap _⟩
lemma comap_rel (f : α → β) (r : setoid β) (x y : α) : (comap f r).rel x y ↔ r.rel (f x) (f y) :=
iff.rfl
/-- Given a map `f : N → M` and an equivalence relation `r` on `β`, the equivalence relation
induced on `α` by `f` equals the kernel of `r`'s quotient map composed with `f`. -/
lemma comap_eq {f : α → β} {r : setoid β} : comap f r = ker (@quotient.mk _ r ∘ f) :=
ext $ λ x y, show _ ↔ ⟦_⟧ = ⟦_⟧, by rw quotient.eq; refl
/-- The second isomorphism theorem for sets. -/
noncomputable def comap_quotient_equiv (f : α → β) (r : setoid β) :
quotient (comap f r) ≃ set.range (@quotient.mk _ r ∘ f) :=
(quotient.congr_right $ ext_iff.1 comap_eq).trans $ quotient_ker_equiv_range $ quotient.mk ∘ f
variables (r f)
/-- The third isomorphism theorem for sets. -/
def quotient_quotient_equiv_quotient (s : setoid α) (h : r ≤ s) :
quotient (ker (quot.map_right h)) ≃ quotient s :=
{ to_fun := λ x, quotient.lift_on' x (λ w, quotient.lift_on' w (@quotient.mk _ s) $
λ x y H, quotient.sound $ h H) $ λ x y, quotient.induction_on₂' x y $ λ w z H,
show @quot.mk _ _ _ = @quot.mk _ _ _, from H,
inv_fun := λ x, quotient.lift_on' x
(λ w, @quotient.mk _ (ker $ quot.map_right h) $ @quotient.mk _ r w) $
λ x y H, quotient.sound' $ show @quot.mk _ _ _ = @quot.mk _ _ _, from quotient.sound H,
left_inv := λ x, quotient.induction_on' x $ λ y, quotient.induction_on' y $
λ w, by show ⟦_⟧ = _; refl,
right_inv := λ x, quotient.induction_on' x $ λ y, by show ⟦_⟧ = _; refl }
variables {r f}
open quotient
/-- Given an equivalence relation `r` on `α`, the order-preserving bijection between the set of
equivalence relations containing `r` and the equivalence relations on the quotient of `α` by `r`. -/
def correspondence (r : setoid α) : {s // r ≤ s} ≃o setoid (quotient r) :=
{ to_fun := λ s, map_of_surjective s.1 quotient.mk ((ker_mk_eq r).symm ▸ s.2) exists_rep,
inv_fun := λ s, ⟨comap quotient.mk' s, λ x y h, by rw [comap_rel, eq_rel.2 h]⟩,
left_inv := λ s, subtype.ext_iff_val.2 $ ext' $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
s.1.trans' (s.1.symm' $ s.2 $ eq_rel.1 hx) $ s.1.trans' H $ s.2 $ eq_rel.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ s, let Hm : ker quotient.mk' ≤ comap quotient.mk' s :=
λ x y h, by rw [comap_rel, (@eq_rel _ r x y).2 ((ker_mk_eq r) ▸ h)] in
ext' $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
quotient.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
map_rel_iff' := λ s t, ⟨λ h x y hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨x, y, rfl, rfl, hs⟩ in
t.1.trans' (t.1.symm' $ t.2 $ eq_rel.1 hx) $ t.1.trans' ht $ t.2 $ eq_rel.1 hy,
λ h x y hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩⟩ }
end setoid
@[simp]
lemma quotient.subsingleton_iff {s : setoid α} :
subsingleton (quotient s) ↔ s = ⊤ :=
begin
simp only [subsingleton_iff, eq_top_iff, setoid.le_def, setoid.top_def,
pi.top_apply, forall_const],
refine (surjective_quotient_mk _).forall.trans (forall_congr $ λ a, _),
refine (surjective_quotient_mk _).forall.trans (forall_congr $ λ b, _),
exact quotient.eq',
end
lemma quot.subsingleton_iff (r : α → α → Prop) : subsingleton (quot r) ↔ eqv_gen r = ⊤ :=
begin
simp only [subsingleton_iff, _root_.eq_top_iff, pi.le_def, pi.top_apply, forall_const],
refine (surjective_quot_mk _).forall.trans (forall_congr $ λ a, _),
refine (surjective_quot_mk _).forall.trans (forall_congr $ λ b, _),
rw quot.eq,
simp only [forall_const, le_Prop_eq],
end
|
8afb4812faf0a6547976a33da119a9dfabbbcc0b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/sheaves/functors.lean | 8693dff4d83a37f13202198af8defe70961ea79b | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 2,407 | lean | /-
Copyright (c) 2021 Junyan Xu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Junyan Xu
-/
import topology.sheaves.sheaf_condition.pairwise_intersections
/-!
# functors between categories of sheaves
Show that the pushforward of a sheaf is a sheaf, and define
the pushforward functor from the category of C-valued sheaves
on X to that of sheaves on Y, given a continuous map between
topological spaces X and Y.
TODO: pullback for presheaves and sheaves
-/
noncomputable theory
universes w v u
open category_theory
open category_theory.limits
open topological_space
variables {C : Type u} [category.{v} C]
variables {X Y : Top.{w}} (f : X ⟶ Y)
variables ⦃ι : Type w⦄ {U : ι → opens Y}
namespace Top
namespace presheaf.sheaf_condition_pairwise_intersections
lemma map_diagram :
pairwise.diagram U ⋙ opens.map f = pairwise.diagram ((opens.map f).obj ∘ U) :=
begin
apply functor.hext,
abstract obj_eq {intro i, cases i; refl},
intros i j g, apply subsingleton.helim,
iterate 2 {rw map_diagram.obj_eq},
end
lemma map_cocone : (opens.map f).map_cocone (pairwise.cocone U)
== pairwise.cocone ((opens.map f).obj ∘ U) :=
begin
unfold functor.map_cocone cocones.functoriality, dsimp, congr,
iterate 2 {rw map_diagram, rw opens.map_supr},
apply subsingleton.helim, rw [map_diagram, opens.map_supr],
apply proof_irrel_heq,
end
theorem pushforward_sheaf_of_sheaf {F : presheaf C X}
(h : F.is_sheaf_pairwise_intersections) :
(f _* F).is_sheaf_pairwise_intersections :=
λ ι U, begin
convert h ((opens.map f).obj ∘ U) using 2,
rw ← map_diagram, refl,
change F.map_cone ((opens.map f).map_cocone _).op == _,
congr, iterate 2 {rw map_diagram}, apply map_cocone,
end
end presheaf.sheaf_condition_pairwise_intersections
namespace sheaf
open presheaf
/--
The pushforward of a sheaf (by a continuous map) is a sheaf.
-/
theorem pushforward_sheaf_of_sheaf
{F : X.presheaf C} (h : F.is_sheaf) : (f _* F).is_sheaf :=
by rw is_sheaf_iff_is_sheaf_pairwise_intersections at h ⊢;
exact sheaf_condition_pairwise_intersections.pushforward_sheaf_of_sheaf f h
/--
The pushforward functor.
-/
def pushforward (f : X ⟶ Y) : X.sheaf C ⥤ Y.sheaf C :=
{ obj := λ ℱ, ⟨f _* ℱ.1, pushforward_sheaf_of_sheaf f ℱ.2⟩,
map := λ _ _ g, ⟨pushforward_map f g.1⟩ }
end sheaf
end Top
|
66956e95ee87a580989c2ded5ce6bb760c09632d | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/order/filter/indicator_function.lean | 51463c6965df05a56520e14b84e6d1409f0d57c3 | [
"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 | 3,216 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov
-/
import algebra.indicator_function
import order.filter.at_top_bot
/-!
# Indicator function and filters
Properties of indicator functions involving `=ᶠ` and `≤ᶠ`.
## Tags
indicator, characteristic, filter
-/
variables {α β M E : Type*}
open set filter classical
open_locale filter classical
section has_zero
variables [has_zero M] {s t : set α} {f g : α → M} {a : α} {l : filter α}
lemma indicator_eventually_eq (hf : f =ᶠ[l ⊓ 𝓟 s] g) (hs : s =ᶠ[l] t) :
indicator s f =ᶠ[l] indicator t g :=
(eventually_inf_principal.1 hf).mp $ hs.mem_iff.mono $ λ x hst hfg,
by_cases (λ hxs : x ∈ s, by simp only [*, hst.1 hxs, indicator_of_mem])
(λ hxs, by simp only [indicator_of_not_mem hxs, indicator_of_not_mem (mt hst.2 hxs)])
end has_zero
section add_monoid
variables [add_monoid M] {s t : set α} {f g : α → M} {a : α} {l : filter α}
lemma indicator_union_eventually_eq (h : ∀ᶠ a in l, a ∉ s ∩ t) :
indicator (s ∪ t) f =ᶠ[l] indicator s f + indicator t f :=
h.mono $ λ a ha, indicator_union_of_not_mem_inter ha _
end add_monoid
section order
variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α} {l : filter α}
lemma indicator_eventually_le_indicator (h : f ≤ᶠ[l ⊓ 𝓟 s] g) :
indicator s f ≤ᶠ[l] indicator s g :=
(eventually_inf_principal.1 h).mono $ assume a h,
indicator_rel_indicator (le_refl _) h
end order
lemma monotone.tendsto_indicator {ι} [preorder ι] [has_zero β]
(s : ι → set α) (hs : monotone s) (f : α → β) (a : α) :
tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (⋃ i, s i) f a) :=
begin
by_cases h : ∃i, a ∈ s i,
{ rcases h with ⟨i, hi⟩,
refine tendsto_pure.2 ((eventually_ge_at_top i).mono $ assume n hn, _),
rw [indicator_of_mem (hs hn hi) _, indicator_of_mem ((subset_Union _ _) hi) _] },
{ rw [not_exists] at h,
simp only [indicator_of_not_mem (h _)],
convert tendsto_const_pure,
apply indicator_of_not_mem, simpa only [not_exists, mem_Union] }
end
lemma antitone.tendsto_indicator {ι} [preorder ι] [has_zero β]
(s : ι → set α) (hs : antitone s) (f : α → β) (a : α) :
tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (⋂ i, s i) f a) :=
begin
by_cases h : ∃i, a ∉ s i,
{ rcases h with ⟨i, hi⟩,
refine tendsto_pure.2 ((eventually_ge_at_top i).mono $ assume n hn, _),
rw [indicator_of_not_mem _ _, indicator_of_not_mem _ _],
{ simp only [mem_Inter, not_forall], exact ⟨i, hi⟩ },
{ assume h, have := hs hn h, contradiction } },
{ push_neg at h,
simp only [indicator_of_mem, h, (mem_Inter.2 h), tendsto_const_pure] }
end
lemma tendsto_indicator_bUnion_finset {ι} [has_zero β] (s : ι → set α) (f : α → β) (a : α) :
tendsto (λ (n : finset ι), indicator (⋃i∈n, s i) f a) at_top (pure $ indicator (Union s) f a) :=
begin
rw Union_eq_Union_finset s,
refine monotone.tendsto_indicator (λ n : finset ι, ⋃ i ∈ n, s i) _ f a,
exact λ t₁ t₂, bUnion_subset_bUnion_left
end
|
82a0ae0bf9f356472390491c039435ce726cc21d | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/coe8.lean | 2e56be1fccddfd2e7b8f6486b17b8f6edf5579e5 | [
"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 | 369 | lean | import logic
namespace experiment
constant nat : Type.{1}
constant int : Type.{1}
constant of_nat : nat → int
attribute of_nat [coercion]
constant nat_add : nat → nat → nat
constant int_add : int → int → int
infixl `+` := int_add
infixl `+` := nat_add
print "================"
constant tst (n m : nat) : @eq int (of_nat n + of_nat m) (n + m)
check tst
exit
|
5e1b91792d5e57879522a1286d1c1235ac695fcd | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/injectionsIssue.lean | 49b27955641047fac101f58055f933f3883faab1 | [
"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 | 314 | lean | inductive Vector (α : Type u): Nat → Type u where
| nil : Vector α 0
| cons (head : α) (tail : Vector α n) : Vector α (n+1)
namespace Vector
def nth : Vector α n → Fin n → α
| cons x xs, ⟨0, _⟩ => x
| cons x xs, ⟨k+1, h⟩ => xs.nth ⟨k, Nat.le_of_succ_le_succ h⟩
attribute [simp] nth
|
2887f8666b6b21926411267cc4afcc6bf7432112 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/number_theory/arithmetic_function.lean | 967b2ddb6d76ae023828976a4df79de249aab3a9 | [
"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 | 34,760 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import algebra.big_operators.ring
import number_theory.divisors
import algebra.squarefree
import algebra.invertible
import data.nat.factorization
/-!
# Arithmetic Functions and Dirichlet Convolution
This file defines arithmetic functions, which are functions from `ℕ` to a specified type that map 0
to 0. In the literature, they are often instead defined as functions from `ℕ+`. These arithmetic
functions are endowed with a multiplication, given by Dirichlet convolution, and pointwise addition,
to form the Dirichlet ring.
## Main Definitions
* `arithmetic_function R` consists of functions `f : ℕ → R` such that `f 0 = 0`.
* An arithmetic function `f` `is_multiplicative` when `x.coprime y → f (x * y) = f x * f y`.
* The pointwise operations `pmul` and `ppow` differ from the multiplication
and power instances on `arithmetic_function R`, which use Dirichlet multiplication.
* `ζ` is the arithmetic function such that `ζ x = 1` for `0 < x`.
* `σ k` is the arithmetic function such that `σ k x = ∑ y in divisors x, y ^ k` for `0 < x`.
* `pow k` is the arithmetic function such that `pow k x = x ^ k` for `0 < x`.
* `id` is the identity arithmetic function on `ℕ`.
* `ω n` is the number of distinct prime factors of `n`.
* `Ω n` is the number of prime factors of `n` counted with multiplicity.
* `μ` is the Möbius function (spelled `moebius` in code).
## Main Results
* Several forms of Möbius inversion:
* `sum_eq_iff_sum_mul_moebius_eq` for functions to a `comm_ring`
* `sum_eq_iff_sum_smul_moebius_eq` for functions to an `add_comm_group`
* `prod_eq_iff_prod_pow_moebius_eq` for functions to a `comm_group`
* `prod_eq_iff_prod_pow_moebius_eq_of_nonzero` for functions to a `comm_group_with_zero`
## Notation
The arithmetic functions `ζ` and `σ` have Greek letter names, which are localized notation in
the namespace `arithmetic_function`.
## Tags
arithmetic functions, dirichlet convolution, divisors
-/
open finset
open_locale big_operators
namespace nat
variable (R : Type*)
/-- An arithmetic function is a function from `ℕ` that maps 0 to 0. In the literature, they are
often instead defined as functions from `ℕ+`. Multiplication on `arithmetic_functions` is by
Dirichlet convolution. -/
@[derive [has_zero, inhabited]]
def arithmetic_function [has_zero R] := zero_hom ℕ R
variable {R}
namespace arithmetic_function
section has_zero
variable [has_zero R]
instance : has_coe_to_fun (arithmetic_function R) (λ _, ℕ → R) := zero_hom.has_coe_to_fun
@[simp] lemma to_fun_eq (f : arithmetic_function R) : f.to_fun = f := rfl
@[simp]
lemma map_zero {f : arithmetic_function R} : f 0 = 0 :=
zero_hom.map_zero' f
theorem coe_inj {f g : arithmetic_function R} : (f : ℕ → R) = g ↔ f = g :=
⟨λ h, zero_hom.coe_inj h, λ h, h ▸ rfl⟩
@[simp]
lemma zero_apply {x : ℕ} : (0 : arithmetic_function R) x = 0 :=
zero_hom.zero_apply x
@[ext] theorem ext ⦃f g : arithmetic_function R⦄ (h : ∀ x, f x = g x) : f = g :=
zero_hom.ext h
theorem ext_iff {f g : arithmetic_function R} : f = g ↔ ∀ x, f x = g x :=
zero_hom.ext_iff
section has_one
variable [has_one R]
instance : has_one (arithmetic_function R) := ⟨⟨λ x, ite (x = 1) 1 0, rfl⟩⟩
@[simp]
lemma one_one : (1 : arithmetic_function R) 1 = 1 := rfl
@[simp]
lemma one_apply_ne {x : ℕ} (h : x ≠ 1) : (1 : arithmetic_function R) x = 0 := if_neg h
end has_one
end has_zero
instance nat_coe [has_zero R] [has_one R] [has_add R] :
has_coe (arithmetic_function ℕ) (arithmetic_function R) :=
⟨λ f, ⟨↑(f : ℕ → ℕ), by { transitivity ↑(f 0), refl, simp }⟩⟩
@[simp]
lemma nat_coe_nat (f : arithmetic_function ℕ) :
(↑f : arithmetic_function ℕ) = f :=
ext $ λ _, cast_id _
@[simp]
lemma nat_coe_apply [has_zero R] [has_one R] [has_add R] {f : arithmetic_function ℕ} {x : ℕ} :
(f : arithmetic_function R) x = f x := rfl
instance int_coe [has_zero R] [has_one R] [has_add R] [has_neg R] :
has_coe (arithmetic_function ℤ) (arithmetic_function R) :=
⟨λ f, ⟨↑(f : ℕ → ℤ), by { transitivity ↑(f 0), refl, simp }⟩⟩
@[simp]
lemma int_coe_int (f : arithmetic_function ℤ) :
(↑f : arithmetic_function ℤ) = f :=
ext $ λ _, int.cast_id _
@[simp]
lemma int_coe_apply [has_zero R] [has_one R] [has_add R] [has_neg R]
{f : arithmetic_function ℤ} {x : ℕ} :
(f : arithmetic_function R) x = f x := rfl
@[simp]
lemma coe_coe [has_zero R] [has_one R] [has_add R] [has_neg R] {f : arithmetic_function ℕ} :
((f : arithmetic_function ℤ) : arithmetic_function R) = f :=
by { ext, simp, }
section add_monoid
variable [add_monoid R]
instance : has_add (arithmetic_function R) := ⟨λ f g, ⟨λ n, f n + g n, by simp⟩⟩
@[simp]
lemma add_apply {f g : arithmetic_function R} {n : ℕ} : (f + g) n = f n + g n := rfl
instance : add_monoid (arithmetic_function R) :=
{ add_assoc := λ _ _ _, ext (λ _, add_assoc _ _ _),
zero_add := λ _, ext (λ _, zero_add _),
add_zero := λ _, ext (λ _, add_zero _),
.. arithmetic_function.has_zero R,
.. arithmetic_function.has_add }
end add_monoid
instance [add_comm_monoid R] : add_comm_monoid (arithmetic_function R) :=
{ add_comm := λ _ _, ext (λ _, add_comm _ _),
.. arithmetic_function.add_monoid }
instance [add_group R] : add_group (arithmetic_function R) :=
{ neg := λ f, ⟨λ n, - f n, by simp⟩,
add_left_neg := λ _, ext (λ _, add_left_neg _),
.. arithmetic_function.add_monoid }
instance [add_comm_group R] : add_comm_group (arithmetic_function R) :=
{ .. arithmetic_function.add_comm_monoid,
.. arithmetic_function.add_group }
section has_scalar
variables {M : Type*} [has_zero R] [add_comm_monoid M] [has_scalar R M]
/-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function
such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/
instance : has_scalar (arithmetic_function R) (arithmetic_function M) :=
⟨λ f g, ⟨λ n, ∑ x in divisors_antidiagonal n, f x.fst • g x.snd, by simp⟩⟩
@[simp]
lemma smul_apply {f : arithmetic_function R} {g : arithmetic_function M} {n : ℕ} :
(f • g) n = ∑ x in divisors_antidiagonal n, f x.fst • g x.snd := rfl
end has_scalar
/-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function
such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/
instance [semiring R] : has_mul (arithmetic_function R) := ⟨(•)⟩
@[simp]
lemma mul_apply [semiring R] {f g : arithmetic_function R} {n : ℕ} :
(f * g) n = ∑ x in divisors_antidiagonal n, f x.fst * g x.snd := rfl
section module
variables {M : Type*} [semiring R] [add_comm_monoid M] [module R M]
lemma mul_smul' (f g : arithmetic_function R) (h : arithmetic_function M) :
(f * g) • h = f • g • h :=
begin
ext n,
simp only [mul_apply, smul_apply, sum_smul, mul_smul, smul_sum, finset.sum_sigma'],
apply finset.sum_bij,
swap 5,
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, exact ⟨(k, l*j), (l, j)⟩ },
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H,
simp only [finset.mem_sigma, mem_divisors_antidiagonal] at H ⊢,
rcases H with ⟨⟨rfl, n0⟩, rfl, i0⟩,
refine ⟨⟨(mul_assoc _ _ _).symm, n0⟩, rfl, _⟩,
rw mul_ne_zero_iff at *,
exact ⟨i0.2, n0.2⟩, },
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, simp only [mul_assoc] },
{ rintros ⟨⟨a,b⟩, ⟨c,d⟩⟩ ⟨⟨i,j⟩, ⟨k,l⟩⟩ H₁ H₂,
simp only [finset.mem_sigma, mem_divisors_antidiagonal,
and_imp, prod.mk.inj_iff, add_comm, heq_iff_eq] at H₁ H₂ ⊢,
rintros rfl h2 rfl rfl,
exact ⟨⟨eq.trans H₁.2.1.symm H₂.2.1, rfl⟩, rfl, rfl⟩ },
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, refine ⟨⟨(i*k, l), (i, k)⟩, _, _⟩,
{ simp only [finset.mem_sigma, mem_divisors_antidiagonal] at H ⊢,
rcases H with ⟨⟨rfl, n0⟩, rfl, j0⟩,
refine ⟨⟨mul_assoc _ _ _, n0⟩, rfl, _⟩,
rw mul_ne_zero_iff at *,
exact ⟨n0.1, j0.1⟩ },
{ simp only [true_and, mem_divisors_antidiagonal, and_true, prod.mk.inj_iff, eq_self_iff_true,
ne.def, mem_sigma, heq_iff_eq] at H ⊢,
rw H.2.1 } }
end
lemma one_smul' (b : arithmetic_function M) :
(1 : arithmetic_function R) • b = b :=
begin
ext,
rw smul_apply,
by_cases x0 : x = 0, {simp [x0]},
have h : {(1,x)} ⊆ divisors_antidiagonal x := by simp [x0],
rw ← sum_subset h, {simp},
intros y ymem ynmem,
have y1ne : y.fst ≠ 1,
{ intro con,
simp only [con, mem_divisors_antidiagonal, one_mul, ne.def] at ymem,
simp only [mem_singleton, prod.ext_iff] at ynmem,
tauto },
simp [y1ne],
end
end module
section semiring
variable [semiring R]
instance : monoid (arithmetic_function R) :=
{ one_mul := one_smul',
mul_one := λ f,
begin
ext,
rw mul_apply,
by_cases x0 : x = 0, {simp [x0]},
have h : {(x,1)} ⊆ divisors_antidiagonal x := by simp [x0],
rw ← sum_subset h, {simp},
intros y ymem ynmem,
have y2ne : y.snd ≠ 1,
{ intro con,
simp only [con, mem_divisors_antidiagonal, mul_one, ne.def] at ymem,
simp only [mem_singleton, prod.ext_iff] at ynmem,
tauto },
simp [y2ne],
end,
mul_assoc := mul_smul',
.. arithmetic_function.has_one,
.. arithmetic_function.has_mul }
instance : semiring (arithmetic_function R) :=
{ zero_mul := λ f, by { ext, simp only [mul_apply, zero_mul, sum_const_zero, zero_apply] },
mul_zero := λ f, by { ext, simp only [mul_apply, sum_const_zero, mul_zero, zero_apply] },
left_distrib := λ a b c, by { ext, simp only [←sum_add_distrib, mul_add, mul_apply, add_apply] },
right_distrib := λ a b c, by { ext, simp only [←sum_add_distrib, add_mul, mul_apply, add_apply] },
.. arithmetic_function.has_zero R,
.. arithmetic_function.has_mul,
.. arithmetic_function.has_add,
.. arithmetic_function.add_comm_monoid,
.. arithmetic_function.monoid }
end semiring
instance [comm_semiring R] : comm_semiring (arithmetic_function R) :=
{ mul_comm := λ f g, by { ext,
rw [mul_apply, ← map_swap_divisors_antidiagonal, sum_map],
simp [mul_comm] },
.. arithmetic_function.semiring }
instance [comm_ring R] : comm_ring (arithmetic_function R) :=
{ .. arithmetic_function.add_comm_group,
.. arithmetic_function.comm_semiring }
instance {M : Type*} [semiring R] [add_comm_monoid M] [module R M] :
module (arithmetic_function R) (arithmetic_function M) :=
{ one_smul := one_smul',
mul_smul := mul_smul',
smul_add := λ r x y, by { ext, simp only [sum_add_distrib, smul_add, smul_apply, add_apply] },
smul_zero := λ r, by { ext, simp only [smul_apply, sum_const_zero, smul_zero, zero_apply] },
add_smul := λ r s x, by { ext, simp only [add_smul, sum_add_distrib, smul_apply, add_apply] },
zero_smul := λ r, by { ext, simp only [smul_apply, sum_const_zero, zero_smul, zero_apply] }, }
section zeta
/-- `ζ 0 = 0`, otherwise `ζ x = 1`. The Dirichlet Series is the Riemann ζ. -/
def zeta : arithmetic_function ℕ :=
⟨λ x, ite (x = 0) 0 1, rfl⟩
localized "notation `ζ` := nat.arithmetic_function.zeta" in arithmetic_function
@[simp]
lemma zeta_apply {x : ℕ} : ζ x = if (x = 0) then 0 else 1 := rfl
lemma zeta_apply_ne {x : ℕ} (h : x ≠ 0) : ζ x = 1 := if_neg h
@[simp]
theorem coe_zeta_mul_apply [semiring R] {f : arithmetic_function R} {x : ℕ} :
(↑ζ * f) x = ∑ i in divisors x, f i :=
begin
rw mul_apply,
transitivity ∑ i in divisors_antidiagonal x, f i.snd,
{ apply sum_congr rfl,
intros i hi,
rcases mem_divisors_antidiagonal.1 hi with ⟨rfl, h⟩,
rw [nat_coe_apply, zeta_apply_ne (left_ne_zero_of_mul h), cast_one, one_mul] },
{ apply sum_bij (λ i h, prod.snd i),
{ rintros ⟨a, b⟩ h, simp [snd_mem_divisors_of_mem_antidiagonal h] },
{ rintros ⟨a, b⟩ h, refl },
{ rintros ⟨a1, b1⟩ ⟨a2, b2⟩ h1 h2 h,
dsimp at h,
rw h at *,
rw mem_divisors_antidiagonal at *,
ext, swap, {refl},
simp only [prod.fst, prod.snd] at *,
apply nat.eq_of_mul_eq_mul_right _ (eq.trans h1.1 h2.1.symm),
rcases h1 with ⟨rfl, h⟩,
apply nat.pos_of_ne_zero (right_ne_zero_of_mul h) },
{ intros a ha,
rcases mem_divisors.1 ha with ⟨⟨b, rfl⟩, ne0⟩,
use (b, a),
simp [ne0, mul_comm] } }
end
theorem coe_zeta_smul_apply {M : Type*} [comm_ring R] [add_comm_group M] [module R M]
{f : arithmetic_function M} {x : ℕ} :
((↑ζ : arithmetic_function R) • f) x = ∑ i in divisors x, f i :=
begin
rw smul_apply,
transitivity ∑ i in divisors_antidiagonal x, f i.snd,
{ apply sum_congr rfl,
intros i hi,
rcases mem_divisors_antidiagonal.1 hi with ⟨rfl, h⟩,
rw [nat_coe_apply, zeta_apply_ne (left_ne_zero_of_mul h), cast_one, one_smul] },
{ apply sum_bij (λ i h, prod.snd i),
{ rintros ⟨a, b⟩ h, simp [snd_mem_divisors_of_mem_antidiagonal h] },
{ rintros ⟨a, b⟩ h, refl },
{ rintros ⟨a1, b1⟩ ⟨a2, b2⟩ h1 h2 h,
dsimp at h,
rw h at *,
rw mem_divisors_antidiagonal at *,
ext, swap, {refl},
simp only [prod.fst, prod.snd] at *,
apply nat.eq_of_mul_eq_mul_right _ (eq.trans h1.1 h2.1.symm),
rcases h1 with ⟨rfl, h⟩,
apply nat.pos_of_ne_zero (right_ne_zero_of_mul h) },
{ intros a ha,
rcases mem_divisors.1 ha with ⟨⟨b, rfl⟩, ne0⟩,
use (b, a),
simp [ne0, mul_comm] } }
end
@[simp]
theorem coe_mul_zeta_apply [semiring R] {f : arithmetic_function R} {x : ℕ} :
(f * ζ) x = ∑ i in divisors x, f i :=
begin
apply mul_opposite.op_injective,
rw [op_sum],
convert @coe_zeta_mul_apply Rᵐᵒᵖ _ { to_fun := mul_opposite.op ∘ f, map_zero' := by simp} x,
rw [mul_apply, mul_apply, op_sum],
conv_lhs { rw ← map_swap_divisors_antidiagonal, },
rw sum_map,
apply sum_congr rfl,
intros y hy,
by_cases h1 : y.fst = 0,
{ simp [function.comp_apply, h1] },
{ simp only [h1, mul_one, one_mul, prod.fst_swap, function.embedding.coe_fn_mk, prod.snd_swap,
if_false, zeta_apply, zero_hom.coe_mk, nat_coe_apply, cast_one] }
end
theorem zeta_mul_apply {f : arithmetic_function ℕ} {x : ℕ} :
(ζ * f) x = ∑ i in divisors x, f i :=
by rw [← nat_coe_nat ζ, coe_zeta_mul_apply]
theorem mul_zeta_apply {f : arithmetic_function ℕ} {x : ℕ} :
(f * ζ) x = ∑ i in divisors x, f i :=
by rw [← nat_coe_nat ζ, coe_mul_zeta_apply]
end zeta
open_locale arithmetic_function
section pmul
/-- This is the pointwise product of `arithmetic_function`s. -/
def pmul [mul_zero_class R] (f g : arithmetic_function R) :
arithmetic_function R :=
⟨λ x, f x * g x, by simp⟩
@[simp]
lemma pmul_apply [mul_zero_class R] {f g : arithmetic_function R} {x : ℕ} :
f.pmul g x = f x * g x := rfl
lemma pmul_comm [comm_monoid_with_zero R] (f g : arithmetic_function R) :
f.pmul g = g.pmul f :=
by { ext, simp [mul_comm] }
section non_assoc_semiring
variable [non_assoc_semiring R]
@[simp]
lemma pmul_zeta (f : arithmetic_function R) : f.pmul ↑ζ = f :=
begin
ext x,
cases x;
simp [nat.succ_ne_zero],
end
@[simp]
lemma zeta_pmul (f : arithmetic_function R) : (ζ : arithmetic_function R).pmul f = f :=
begin
ext x,
cases x;
simp [nat.succ_ne_zero],
end
end non_assoc_semiring
variables [semiring R]
/-- This is the pointwise power of `arithmetic_function`s. -/
def ppow (f : arithmetic_function R) (k : ℕ) :
arithmetic_function R :=
if h0 : k = 0 then ζ else ⟨λ x, (f x) ^ k,
by { rw [map_zero], exact zero_pow (nat.pos_of_ne_zero h0) }⟩
@[simp]
lemma ppow_zero {f : arithmetic_function R} : f.ppow 0 = ζ :=
by rw [ppow, dif_pos rfl]
@[simp]
lemma ppow_apply {f : arithmetic_function R} {k x : ℕ} (kpos : 0 < k) :
f.ppow k x = (f x) ^ k :=
by { rw [ppow, dif_neg (ne_of_gt kpos)], refl }
lemma ppow_succ {f : arithmetic_function R} {k : ℕ} :
f.ppow (k + 1) = f.pmul (f.ppow k) :=
begin
ext x,
rw [ppow_apply (nat.succ_pos k), pow_succ],
induction k; simp,
end
lemma ppow_succ' {f : arithmetic_function R} {k : ℕ} {kpos : 0 < k} :
f.ppow (k + 1) = (f.ppow k).pmul f :=
begin
ext x,
rw [ppow_apply (nat.succ_pos k), pow_succ'],
induction k; simp,
end
end pmul
/-- Multiplicative functions -/
def is_multiplicative [monoid_with_zero R] (f : arithmetic_function R) : Prop :=
f 1 = 1 ∧ (∀ {m n : ℕ}, m.coprime n → f (m * n) = f m * f n)
namespace is_multiplicative
section monoid_with_zero
variable [monoid_with_zero R]
@[simp]
lemma map_one {f : arithmetic_function R} (h : f.is_multiplicative) : f 1 = 1 :=
h.1
@[simp]
lemma map_mul_of_coprime {f : arithmetic_function R} (hf : f.is_multiplicative)
{m n : ℕ} (h : m.coprime n) : f (m * n) = f m * f n :=
hf.2 h
end monoid_with_zero
lemma nat_cast {f : arithmetic_function ℕ} [semiring R] (h : f.is_multiplicative) :
is_multiplicative (f : arithmetic_function R) :=
⟨by simp [h], λ m n cop, by simp [cop, h]⟩
lemma int_cast {f : arithmetic_function ℤ} [ring R] (h : f.is_multiplicative) :
is_multiplicative (f : arithmetic_function R) :=
⟨by simp [h], λ m n cop, by simp [cop, h]⟩
lemma mul [comm_semiring R] {f g : arithmetic_function R}
(hf : f.is_multiplicative) (hg : g.is_multiplicative) :
is_multiplicative (f * g) :=
⟨by { simp [hf, hg], }, begin
simp only [mul_apply],
intros m n cop,
rw sum_mul_sum,
symmetry,
apply sum_bij (λ (x : (ℕ × ℕ) × ℕ × ℕ) h, (x.1.1 * x.2.1, x.1.2 * x.2.2)),
{ rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ h,
simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h,
rcases h with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩,
simp only [mem_divisors_antidiagonal, nat.mul_eq_zero, ne.def],
split, {ring},
rw nat.mul_eq_zero at *,
apply not_or ha hb },
{ rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ h,
simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h,
rcases h with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩,
dsimp only,
rw [hf.map_mul_of_coprime cop.coprime_mul_right.coprime_mul_right_right,
hg.map_mul_of_coprime cop.coprime_mul_left.coprime_mul_left_right],
ring, },
{ rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ ⟨⟨c1, c2⟩, ⟨d1, d2⟩⟩ hab hcd h,
simp only [mem_divisors_antidiagonal, ne.def, mem_product] at hab,
rcases hab with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩,
simp only [mem_divisors_antidiagonal, ne.def, mem_product] at hcd,
simp only [prod.mk.inj_iff] at h,
ext; dsimp only,
{ transitivity nat.gcd (a1 * a2) (a1 * b1),
{ rw [nat.gcd_mul_left, cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] },
{ rw [← hcd.1.1, ← hcd.2.1] at cop,
rw [← hcd.1.1, h.1, nat.gcd_mul_left,
cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] } },
{ transitivity nat.gcd (a1 * a2) (a2 * b2),
{ rw [mul_comm, nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one,
mul_one] },
{ rw [← hcd.1.1, ← hcd.2.1] at cop,
rw [← hcd.1.1, h.2, mul_comm, nat.gcd_mul_left,
cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one, mul_one] } },
{ transitivity nat.gcd (b1 * b2) (a1 * b1),
{ rw [mul_comm, nat.gcd_mul_right,
cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, one_mul] },
{ rw [← hcd.1.1, ← hcd.2.1] at cop,
rw [← hcd.2.1, h.1, mul_comm c1 d1, nat.gcd_mul_left,
cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, mul_one] } },
{ transitivity nat.gcd (b1 * b2) (a2 * b2),
{ rw [nat.gcd_mul_right,
cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] },
{ rw [← hcd.1.1, ← hcd.2.1] at cop,
rw [← hcd.2.1, h.2, nat.gcd_mul_right,
cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] } } },
{ rintros ⟨b1, b2⟩ h,
simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h,
use ((b1.gcd m, b2.gcd m), (b1.gcd n, b2.gcd n)),
simp only [exists_prop, prod.mk.inj_iff, ne.def, mem_product, mem_divisors_antidiagonal],
rw [← cop.gcd_mul _, ← cop.gcd_mul _, ← h.1, nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop h.1,
nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop.symm _],
{ rw [nat.mul_eq_zero, decidable.not_or_iff_and_not] at h, simp [h.2.1, h.2.2] },
rw [mul_comm n m, h.1] }
end⟩
lemma pmul [comm_semiring R] {f g : arithmetic_function R}
(hf : f.is_multiplicative) (hg : g.is_multiplicative) :
is_multiplicative (f.pmul g) :=
⟨by { simp [hf, hg], }, λ m n cop, begin
simp only [pmul_apply, hf.map_mul_of_coprime cop, hg.map_mul_of_coprime cop],
ring,
end⟩
/-- For any multiplicative function `f` and any `n > 0`,
we can evaluate `f n` by evaluating `f` at `p ^ k` over the factorization of `n` -/
lemma multiplicative_factorization [comm_monoid_with_zero R] (f : arithmetic_function R)
(hf : f.is_multiplicative) :
∀ {n : ℕ}, n ≠ 0 → f n = n.factorization.prod (λ p k, f (p ^ k)) :=
λ n hn, multiplicative_factorization f hf.2 hf.1 hn
/-- A recapitulation of the definition of multiplicative that is simpler for proofs -/
lemma iff_ne_zero [monoid_with_zero R] {f : arithmetic_function R} :
is_multiplicative f ↔
f 1 = 1 ∧ (∀ {m n : ℕ}, m ≠ 0 → n ≠ 0 → m.coprime n → f (m * n) = f m * f n) :=
begin
refine and_congr_right' (forall₂_congr (λ m n, ⟨λ h _ _, h, λ h hmn, _⟩)),
rcases eq_or_ne m 0 with rfl | hm,
{ simp },
rcases eq_or_ne n 0 with rfl | hn,
{ simp },
exact h hm hn hmn,
end
/-- Two multiplicative functions `f` and `g` are equal if and only if
they agree on prime powers -/
lemma eq_iff_eq_on_prime_powers [comm_monoid_with_zero R]
(f : arithmetic_function R) (hf : f.is_multiplicative)
(g : arithmetic_function R) (hg : g.is_multiplicative) :
f = g ↔ ∀ (p i : ℕ), nat.prime p → f (p ^ i) = g (p ^ i) :=
begin
split,
{ intros h p i _, rw [h] },
intros h,
ext n,
by_cases hn : n = 0,
{ rw [hn, arithmetic_function.map_zero, arithmetic_function.map_zero] },
rw [multiplicative_factorization f hf hn, multiplicative_factorization g hg hn],
refine finset.prod_congr rfl _,
simp only [support_factorization, list.mem_to_finset],
intros p hp,
exact h p _ (nat.prime_of_mem_factors hp),
end
end is_multiplicative
section special_functions
/-- The identity on `ℕ` as an `arithmetic_function`. -/
def id : arithmetic_function ℕ := ⟨id, rfl⟩
@[simp]
lemma id_apply {x : ℕ} : id x = x := rfl
/-- `pow k n = n ^ k`, except `pow 0 0 = 0`. -/
def pow (k : ℕ) : arithmetic_function ℕ := id.ppow k
@[simp]
lemma pow_apply {k n : ℕ} : pow k n = if (k = 0 ∧ n = 0) then 0 else n ^ k :=
begin
cases k,
{ simp [pow] },
simp [pow, (ne_of_lt (nat.succ_pos k)).symm],
end
/-- `σ k n` is the sum of the `k`th powers of the divisors of `n` -/
def sigma (k : ℕ) : arithmetic_function ℕ :=
⟨λ n, ∑ d in divisors n, d ^ k, by simp⟩
localized "notation `σ` := nat.arithmetic_function.sigma" in arithmetic_function
@[simp]
lemma sigma_apply {k n : ℕ} : σ k n = ∑ d in divisors n, d ^ k := rfl
lemma sigma_one_apply {n : ℕ} : σ 1 n = ∑ d in divisors n, d := by simp
lemma zeta_mul_pow_eq_sigma {k : ℕ} : ζ * pow k = σ k :=
begin
ext,
rw [sigma, zeta_mul_apply],
apply sum_congr rfl,
intros x hx,
rw [pow_apply, if_neg (not_and_of_not_right _ _)],
contrapose! hx,
simp [hx],
end
lemma is_multiplicative_zeta : is_multiplicative ζ :=
⟨by simp, λ m n cop, begin
cases m, {simp},
cases n, {simp},
simp [nat.succ_ne_zero]
end⟩
lemma is_multiplicative_id : is_multiplicative arithmetic_function.id :=
⟨rfl, λ _ _ _, rfl⟩
lemma is_multiplicative.ppow [comm_semiring R] {f : arithmetic_function R}
(hf : f.is_multiplicative) {k : ℕ} :
is_multiplicative (f.ppow k) :=
begin
induction k with k hi,
{ exact is_multiplicative_zeta.nat_cast },
{ rw ppow_succ,
apply hf.pmul hi },
end
lemma is_multiplicative_pow {k : ℕ} : is_multiplicative (pow k) :=
is_multiplicative_id.ppow
lemma is_multiplicative_sigma {k : ℕ} :
is_multiplicative (sigma k) :=
begin
rw [← zeta_mul_pow_eq_sigma],
apply ((is_multiplicative_zeta).mul is_multiplicative_pow)
end
/-- `Ω n` is the number of prime factors of `n`. -/
def card_factors : arithmetic_function ℕ :=
⟨λ n, n.factors.length, by simp⟩
localized "notation `Ω` := nat.arithmetic_function.card_factors" in arithmetic_function
lemma card_factors_apply {n : ℕ} :
Ω n = n.factors.length := rfl
@[simp]
lemma card_factors_one : Ω 1 = 0 := by simp [card_factors]
lemma card_factors_eq_one_iff_prime {n : ℕ} :
Ω n = 1 ↔ n.prime :=
begin
refine ⟨λ h, _, λ h, list.length_eq_one.2 ⟨n, factors_prime h⟩⟩,
cases n,
{ contrapose! h,
simp },
rcases list.length_eq_one.1 h with ⟨x, hx⟩,
rw [← prod_factors n.succ_ne_zero, hx, list.prod_singleton],
apply prime_of_mem_factors,
rw [hx, list.mem_singleton]
end
lemma card_factors_mul {m n : ℕ} (m0 : m ≠ 0) (n0 : n ≠ 0) :
Ω (m * n) = Ω m + Ω n :=
by rw [card_factors_apply, card_factors_apply, card_factors_apply, ← multiset.coe_card,
← factors_eq, unique_factorization_monoid.normalized_factors_mul m0 n0, factors_eq, factors_eq,
multiset.card_add, multiset.coe_card, multiset.coe_card]
lemma card_factors_multiset_prod {s : multiset ℕ} (h0 : s.prod ≠ 0) :
Ω s.prod = (multiset.map Ω s).sum :=
begin
revert h0,
apply s.induction_on, by simp,
intros a t h h0,
rw [multiset.prod_cons, mul_ne_zero_iff] at h0,
simp [h0, card_factors_mul, h],
end
/-- `ω n` is the number of distinct prime factors of `n`. -/
def card_distinct_factors : arithmetic_function ℕ :=
⟨λ n, n.factors.dedup.length, by simp⟩
localized "notation `ω` := nat.arithmetic_function.card_distinct_factors" in arithmetic_function
lemma card_distinct_factors_zero : ω 0 = 0 := by simp
lemma card_distinct_factors_apply {n : ℕ} :
ω n = n.factors.dedup.length := rfl
lemma card_distinct_factors_eq_card_factors_iff_squarefree {n : ℕ} (h0 : n ≠ 0) :
ω n = Ω n ↔ squarefree n :=
begin
rw [squarefree_iff_nodup_factors h0, card_distinct_factors_apply],
split; intro h,
{ rw ← list.eq_of_sublist_of_length_eq n.factors.dedup_sublist h,
apply list.nodup_dedup },
{ rw h.dedup,
refl }
end
/-- `μ` is the Möbius function. If `n` is squarefree with an even number of distinct prime factors,
`μ n = 1`. If `n` is squarefree with an odd number of distinct prime factors, `μ n = -1`.
If `n` is not squarefree, `μ n = 0`. -/
def moebius : arithmetic_function ℤ :=
⟨λ n, if squarefree n then (-1) ^ (card_factors n) else 0, by simp⟩
localized "notation `μ` := nat.arithmetic_function.moebius" in arithmetic_function
@[simp]
lemma moebius_apply_of_squarefree {n : ℕ} (h : squarefree n): μ n = (-1) ^ (card_factors n) :=
if_pos h
@[simp]
lemma moebius_eq_zero_of_not_squarefree {n : ℕ} (h : ¬ squarefree n): μ n = 0 := if_neg h
lemma moebius_ne_zero_iff_squarefree {n : ℕ} : μ n ≠ 0 ↔ squarefree n :=
begin
split; intro h,
{ contrapose! h,
simp [h] },
{ simp [h, pow_ne_zero] }
end
lemma moebius_ne_zero_iff_eq_or {n : ℕ} : μ n ≠ 0 ↔ μ n = 1 ∨ μ n = -1 :=
begin
split; intro h,
{ rw moebius_ne_zero_iff_squarefree at h,
rw moebius_apply_of_squarefree h,
apply neg_one_pow_eq_or },
{ rcases h with h | h; simp [h] }
end
lemma is_multiplicative_moebius : is_multiplicative μ :=
begin
rw is_multiplicative.iff_ne_zero,
refine ⟨by simp, λ n m hn hm hnm, _⟩,
simp only [moebius, zero_hom.coe_mk, squarefree_mul hnm, ite_and, card_factors_mul hn hm],
rw [pow_add, mul_comm, ite_mul_zero_left, ite_mul_zero_right, mul_comm],
end
open unique_factorization_monoid
@[simp] lemma coe_moebius_mul_coe_zeta [ring R] : (μ * ζ : arithmetic_function R) = 1 :=
begin
ext x,
cases x,
{ simp only [divisors_zero, sum_empty, ne.def, not_false_iff, coe_mul_zeta_apply,
zero_ne_one, one_apply_ne] },
cases x,
{ simp only [moebius_apply_of_squarefree, card_factors_one, squarefree_one, divisors_one,
int.cast_one, sum_singleton, coe_mul_zeta_apply, one_one, int_coe_apply, pow_zero] },
rw [coe_mul_zeta_apply, one_apply_ne (ne_of_gt (succ_lt_succ (nat.succ_pos _)))],
simp_rw [int_coe_apply],
rw [←int.cast_sum, ← sum_filter_ne_zero],
convert int.cast_zero,
simp only [moebius_ne_zero_iff_squarefree],
suffices :
∑ (y : finset ℕ) in
(unique_factorization_monoid.normalized_factors x.succ.succ).to_finset.powerset,
ite (squarefree y.val.prod) ((-1:ℤ) ^ Ω y.val.prod) 0 = 0,
{ have h : ∑ i in _, ite (squarefree i) ((-1:ℤ) ^ Ω i) 0 = _ :=
(sum_divisors_filter_squarefree (nat.succ_ne_zero _)),
exact (eq.trans (by congr') h).trans this },
apply eq.trans (sum_congr rfl _) (sum_powerset_neg_one_pow_card_of_nonempty _),
{ intros y hy,
rw [finset.mem_powerset, ← finset.val_le_iff, multiset.to_finset_val] at hy,
have h : unique_factorization_monoid.normalized_factors y.val.prod = y.val,
{ apply factors_multiset_prod_of_irreducible,
intros z hz,
apply irreducible_of_normalized_factor _ (multiset.subset_of_le
(le_trans hy (multiset.dedup_le _)) hz) },
rw [if_pos],
{ rw [card_factors_apply, ← multiset.coe_card, ← factors_eq, h, finset.card] },
rw [unique_factorization_monoid.squarefree_iff_nodup_normalized_factors, h],
{ apply y.nodup },
rw [ne.def, multiset.prod_eq_zero_iff],
intro con,
rw ← h at con,
exact not_irreducible_zero (irreducible_of_normalized_factor 0 con) },
{ rw finset.nonempty,
rcases wf_dvd_monoid.exists_irreducible_factor _ (nat.succ_ne_zero _) with ⟨i, hi⟩,
{ rcases exists_mem_normalized_factors_of_dvd (nat.succ_ne_zero _) hi.1 hi.2 with ⟨j, hj, hj2⟩,
use j,
apply multiset.mem_to_finset.2 hj },
rw nat.is_unit_iff,
norm_num },
end
@[simp] lemma coe_zeta_mul_coe_moebius [comm_ring R] : (ζ * μ : arithmetic_function R) = 1 :=
by rw [mul_comm, coe_moebius_mul_coe_zeta]
@[simp] lemma moebius_mul_coe_zeta : (μ * ζ : arithmetic_function ℤ) = 1 :=
by rw [← int_coe_int μ, coe_moebius_mul_coe_zeta]
@[simp] lemma coe_zeta_mul_moebius : (ζ * μ : arithmetic_function ℤ) = 1 :=
by rw [← int_coe_int μ, coe_zeta_mul_coe_moebius]
section comm_ring
variable [comm_ring R]
instance : invertible (ζ : arithmetic_function R) :=
{ inv_of := μ,
inv_of_mul_self := coe_moebius_mul_coe_zeta,
mul_inv_of_self := coe_zeta_mul_coe_moebius}
/-- A unit in `arithmetic_function R` that evaluates to `ζ`, with inverse `μ`. -/
def zeta_unit : (arithmetic_function R)ˣ :=
⟨ζ, μ, coe_zeta_mul_coe_moebius, coe_moebius_mul_coe_zeta⟩
@[simp]
lemma coe_zeta_unit :
((zeta_unit : (arithmetic_function R)ˣ) : arithmetic_function R) = ζ := rfl
@[simp]
lemma inv_zeta_unit :
((zeta_unit⁻¹ : (arithmetic_function R)ˣ) : arithmetic_function R) = μ := rfl
end comm_ring
/-- Möbius inversion for functions to an `add_comm_group`. -/
theorem sum_eq_iff_sum_smul_moebius_eq
[add_comm_group R] {f g : ℕ → R} :
(∀ (n : ℕ), 0 < n → ∑ i in (n.divisors), f i = g n) ↔
∀ (n : ℕ), 0 < n → ∑ (x : ℕ × ℕ) in n.divisors_antidiagonal, μ x.fst • g x.snd = f n :=
begin
let f' : arithmetic_function R := ⟨λ x, if x = 0 then 0 else f x, if_pos rfl⟩,
let g' : arithmetic_function R := ⟨λ x, if x = 0 then 0 else g x, if_pos rfl⟩,
transitivity (ζ : arithmetic_function ℤ) • f' = g',
{ rw ext_iff,
apply forall_congr,
intro n,
cases n, { simp },
rw coe_zeta_smul_apply,
simp only [n.succ_ne_zero, forall_prop_of_true, succ_pos', if_false, zero_hom.coe_mk],
rw sum_congr rfl (λ x hx, _),
rw (if_neg (ne_of_gt (nat.pos_of_mem_divisors hx))) },
transitivity μ • g' = f',
{ split; intro h,
{ rw [← h, ← mul_smul, moebius_mul_coe_zeta, one_smul] },
{ rw [← h, ← mul_smul, coe_zeta_mul_moebius, one_smul] } },
{ rw ext_iff,
apply forall_congr,
intro n,
cases n, { simp },
simp only [n.succ_ne_zero, forall_prop_of_true, succ_pos', smul_apply,
if_false, zero_hom.coe_mk],
rw sum_congr rfl (λ x hx, _),
rw (if_neg (ne_of_gt (nat.pos_of_mem_divisors (snd_mem_divisors_of_mem_antidiagonal hx)))) },
end
/-- Möbius inversion for functions to a `ring`. -/
theorem sum_eq_iff_sum_mul_moebius_eq [ring R] {f g : ℕ → R} :
(∀ (n : ℕ), 0 < n → ∑ i in (n.divisors), f i = g n) ↔
∀ (n : ℕ), 0 < n → ∑ (x : ℕ × ℕ) in n.divisors_antidiagonal, (μ x.fst : R) * g x.snd = f n :=
begin
rw sum_eq_iff_sum_smul_moebius_eq,
apply forall_congr,
refine λ a, imp_congr_right (λ _, (sum_congr rfl $ λ x hx, _).congr_left),
rw [zsmul_eq_mul],
end
/-- Möbius inversion for functions to a `comm_group`. -/
theorem prod_eq_iff_prod_pow_moebius_eq [comm_group R] {f g : ℕ → R} :
(∀ (n : ℕ), 0 < n → ∏ i in (n.divisors), f i = g n) ↔
∀ (n : ℕ), 0 < n → ∏ (x : ℕ × ℕ) in n.divisors_antidiagonal, g x.snd ^ (μ x.fst) = f n :=
@sum_eq_iff_sum_smul_moebius_eq (additive R) _ _ _
/-- Möbius inversion for functions to a `comm_group_with_zero`. -/
theorem prod_eq_iff_prod_pow_moebius_eq_of_nonzero [comm_group_with_zero R] {f g : ℕ → R}
(hf : ∀ (n : ℕ), 0 < n → f n ≠ 0) (hg : ∀ (n : ℕ), 0 < n → g n ≠ 0) :
(∀ (n : ℕ), 0 < n → ∏ i in (n.divisors), f i = g n) ↔
∀ (n : ℕ), 0 < n → ∏ (x : ℕ × ℕ) in n.divisors_antidiagonal, g x.snd ^ (μ x.fst) = f n :=
begin
refine iff.trans (iff.trans (forall_congr (λ n, _)) (@prod_eq_iff_prod_pow_moebius_eq Rˣ _
(λ n, if h : 0 < n then units.mk0 (f n) (hf n h) else 1)
(λ n, if h : 0 < n then units.mk0 (g n) (hg n h) else 1))) (forall_congr (λ n, _));
refine imp_congr_right (λ hn, _),
{ dsimp,
rw [dif_pos hn, ← units.eq_iff, ← units.coe_hom_apply, monoid_hom.map_prod, units.coe_mk0,
prod_congr rfl _],
intros x hx,
rw [dif_pos (nat.pos_of_mem_divisors hx), units.coe_hom_apply, units.coe_mk0] },
{ dsimp,
rw [dif_pos hn, ← units.eq_iff, ← units.coe_hom_apply, monoid_hom.map_prod, units.coe_mk0,
prod_congr rfl _],
intros x hx,
rw [dif_pos (nat.pos_of_mem_divisors (nat.snd_mem_divisors_of_mem_antidiagonal hx)),
units.coe_hom_apply, units.coe_zpow₀, units.coe_mk0] }
end
end special_functions
end arithmetic_function
end nat
|
0ce5c18d46a72306a10c3040aec3ed197879be32 | c777c32c8e484e195053731103c5e52af26a25d1 | /archive/imo/imo1981_q3.lean | 4b40612e5a87397076f1c1d1c178babcf8eb3520 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 6,876 | lean | /-
Copyright (c) 2020 Kevin Lacker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Lacker
-/
import data.nat.fib
import tactic.linarith
/-!
# IMO 1981 Q3
Determine the maximum value of `m ^ 2 + n ^ 2`, where `m` and `n` are integers in
`{1, 2, ..., 1981}` and `(n ^ 2 - m * n - m ^ 2) ^ 2 = 1`.
The trick to this problem is that `m` and `n` have to be consecutive Fibonacci numbers,
because you can reduce any solution to a smaller one using the Fibonacci recurrence.
-/
/-
First, define the problem in terms of finding the maximum of a set.
We first generalize the problem to `{1, 2, ..., N}` and specialize to `N = 1981` at the very end.
-/
open int nat set
namespace imo1981_q3
variable (N : ℕ) -- N = 1981
@[mk_iff] structure problem_predicate (m n : ℤ) : Prop :=
(m_range : m ∈ Ioc 0 (N : ℤ))
(n_range : n ∈ Ioc 0 (N : ℤ))
(eq_one : (n ^ 2 - m * n - m ^ 2) ^ 2 = 1)
def specified_set : set ℤ :=
{k : ℤ | ∃ m : ℤ, ∃ n : ℤ, k = m ^ 2 + n ^ 2 ∧ problem_predicate N m n}
/-
We want to reduce every solution to a smaller solution. Specifically,
we show that when `(m, n)` is a solution, `(n - m, m)` is also a solution,
except for the base case of `(1, 1)`.
-/
namespace problem_predicate
variable {N}
lemma m_le_n {m n : ℤ} (h1 : problem_predicate N m n) : m ≤ n :=
begin
by_contradiction h2,
have h3 : 1 = (n * (n - m) - m ^ 2) ^ 2,
{ calc 1 = (n ^ 2 - m * n - m ^ 2) ^ 2 : h1.eq_one.symm
... = (n * (n - m) - m ^ 2) ^ 2 : by ring },
have h4 : n * (n - m) - m ^ 2 < -1, by nlinarith [h1.n_range.left],
have h5 : 1 < (n * (n - m) - m ^ 2) ^ 2, by nlinarith,
exact h5.ne h3
end
lemma eq_imp_1 {n : ℤ} (h1 : problem_predicate N n n) : n = 1 :=
begin
have : n * (n * (n * n)) = 1,
{ calc _ = (n ^ 2 - n * n - n ^ 2) ^ 2 : by simp [sq, mul_assoc]
... = 1 : h1.eq_one },
exact eq_one_of_mul_eq_one_right h1.m_range.left.le this,
end
lemma reduction {m n : ℤ} (h1 : problem_predicate N m n) (h2 : 1 < n) :
problem_predicate N (n - m) m :=
begin
obtain (rfl : m = n) | (h3 : m < n) := h1.m_le_n.eq_or_lt,
{ have h4 : m = 1, from h1.eq_imp_1,
exact absurd h4.symm h2.ne },
refine_struct { n_range := h1.m_range, .. },
-- m_range:
{ have h5 : 0 < n - m, from sub_pos.mpr h3,
have h6 : n - m < N,
{ calc _ < n : sub_lt_self n h1.m_range.left
... ≤ N : h1.n_range.right },
exact ⟨h5, h6.le⟩ },
-- eq_one:
{ calc _ = (n ^ 2 - m * n - m ^ 2) ^ 2 : by ring
... = 1 : h1.eq_one },
end
end problem_predicate
/-
It will be convenient to have the lemmas above in their natural number form.
Most of these can be proved with the `norm_cast` family of tactics.
-/
def nat_predicate (m n : ℕ) : Prop := problem_predicate N ↑m ↑n
namespace nat_predicate
variable {N}
lemma m_le_n {m n : ℕ} (h1 : nat_predicate N m n) : m ≤ n :=
by exact_mod_cast h1.m_le_n
lemma eq_imp_1 {n : ℕ} (h1 : nat_predicate N n n) : n = 1 :=
by exact_mod_cast h1.eq_imp_1
lemma reduction {m n : ℕ} (h1 : nat_predicate N m n) (h2 : 1 < n) :
nat_predicate N (n - m) m :=
have m ≤ n, from h1.m_le_n,
by exact_mod_cast h1.reduction (by exact_mod_cast h2)
lemma n_pos {m n : ℕ} (h1 : nat_predicate N m n) : 0 < n :=
by exact_mod_cast h1.n_range.left
lemma m_pos {m n : ℕ} (h1 : nat_predicate N m n) : 0 < m :=
by exact_mod_cast h1.m_range.left
lemma n_le_N {m n : ℕ} (h1 : nat_predicate N m n) : n ≤ N :=
by exact_mod_cast h1.n_range.right
/-
Now we can use induction to show that solutions must be Fibonacci numbers.
-/
lemma imp_fib {n : ℕ} : ∀ m : ℕ, nat_predicate N m n →
∃ k : ℕ, m = fib k ∧ n = fib (k + 1) :=
begin
apply nat.strong_induction_on n _,
intros n h1 m h2,
have h3 : m ≤ n, from h2.m_le_n,
obtain (rfl : 1 = n) | (h4 : 1 < n) := (succ_le_iff.mpr h2.n_pos).eq_or_lt,
{ use 1,
have h5 : 1 ≤ m, from succ_le_iff.mpr h2.m_pos,
simpa [fib_one, fib_two] using (h3.antisymm h5 : m = 1) },
{ obtain (rfl : m = n) | (h6 : m < n) := h3.eq_or_lt,
{ exact absurd h2.eq_imp_1 (ne_of_gt h4) },
{ have h7 : nat_predicate N (n - m) m, from h2.reduction h4,
obtain ⟨k : ℕ, hnm : n - m = fib k, rfl : m = fib (k+1)⟩ := h1 m h6 (n - m) h7,
use [k + 1, rfl],
rw [fib_add_two, ← hnm, tsub_add_cancel_of_le h3] } }
end
end nat_predicate
/-
Next, we prove that if `N < fib K + fib (K+1)`, then the largest `m` and `n`
satisfying `nat_predicate m n N` are `fib K` and `fib (K+1)`, respectively.
-/
variables {K : ℕ} (HK : N < fib K + fib (K+1)) {N}
include HK
lemma m_n_bounds {m n : ℕ} (h1 : nat_predicate N m n) : m ≤ fib K ∧ n ≤ fib (K+1) :=
begin
obtain ⟨k : ℕ, hm : m = fib k, hn : n = fib (k+1)⟩ := h1.imp_fib m,
by_cases h2 : k < K + 1,
{ have h3 : k ≤ K, from lt_succ_iff.mp h2,
split,
{ calc m = fib k : hm
... ≤ fib K : fib_mono h3, },
{ have h6 : k + 1 ≤ K + 1, from succ_le_succ h3,
calc n = fib (k+1) : hn
... ≤ fib (K+1) : fib_mono h6 } },
{ have h7 : N < n,
{ have h8 : K + 2 ≤ k + 1, from succ_le_succ (not_lt.mp h2),
rw ← fib_add_two at HK,
calc N < fib (K+2) : HK
... ≤ fib (k+1) : fib_mono h8
... = n : hn.symm, },
have h9 : n ≤ N, from h1.n_le_N,
exact absurd h7 h9.not_lt }
end
/-
We spell out the consequences of this result for `specified_set N` here.
-/
variables {M : ℕ} (HM : M = (fib K) ^ 2 + (fib (K+1)) ^ 2)
include HM
lemma k_bound {m n : ℤ} (h1 : problem_predicate N m n) : m ^ 2 + n ^ 2 ≤ M :=
begin
have h2 : 0 ≤ m, from h1.m_range.left.le,
have h3 : 0 ≤ n, from h1.n_range.left.le,
rw [← nat_abs_of_nonneg h2, ← nat_abs_of_nonneg h3] at h1, clear h2 h3,
obtain ⟨h4 : m.nat_abs ≤ fib K, h5 : n.nat_abs ≤ fib (K+1)⟩ := m_n_bounds HK h1,
have h6 : m ^ 2 ≤ (fib K) ^ 2, from nat_abs_le_iff_sq_le.mp h4,
have h7 : n ^ 2 ≤ (fib (K+1)) ^ 2, from nat_abs_le_iff_sq_le.mp h5,
linarith
end
lemma solution_bound : ∀ {k : ℤ}, k ∈ specified_set N → k ≤ M
| _ ⟨_, _, rfl, h⟩ := k_bound HK HM h
theorem solution_greatest (H : problem_predicate N (fib K) (fib (K + 1))) :
is_greatest (specified_set N) M :=
⟨⟨fib K, fib (K+1), by simp [HM], H⟩, λ k h, solution_bound HK HM h⟩
end imo1981_q3
open imo1981_q3
/-
Now we just have to demonstrate that 987 and 1597 are in fact the largest Fibonacci
numbers in this range, and thus provide the maximum of `specified_set`.
-/
theorem imo1981_q3 : is_greatest (specified_set 1981) 3524578 :=
begin
have := λ h, @solution_greatest 1981 16 h 3524578,
simp only [show fib (16:ℕ) = 987 ∧ fib (16+1:ℕ) = 1597,
by norm_num [fib_add_two]] at this,
apply_mod_cast this; norm_num [problem_predicate_iff],
end
|
96dec9fd123988eabca691da0954a8477aeac55b | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/group_theory/monoid_localization.lean | 65f6860c977b979d500dc07cd14709b0cb3af5b1 | [
"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 | 54,914 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston
-/
import group_theory.congruence
import group_theory.submonoid
import algebra.group.units
import algebra.punit_instances
/-!
# Localizations of commutative monoids
Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so
we can generalize localizations to commutative monoids.
We characterize the localization of a commutative monoid `M` at a submonoid `S` up to
isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a
monoid homomorphism `f : M →* N` satisfying 3 properties:
1. For all `y ∈ S`, `f y` is a unit;
2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`;
3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`.
Given such a localization map `f : M →* N`, we can define the surjection
`localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and
`localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps
elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids
`P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism
`g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations,
`localization_map.map`, from `N` to `Q`.
We treat the special case of localizing away from an element in the sections `away_map` and `away`.
We also define the quotient of `M × S` by the unique congruence relation (equivalence relation
preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S`
satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s`
whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard
localization relation.
This defines the localization as a quotient type, `localization`, but the majority of
subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps
which satisfy the characteristic predicate.
## Implementation notes
In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one
structure with an isomorphic one; one way around this is to isolate a predicate characterizing
a structure up to isomorphism, and reason about things that satisfy the predicate.
The infimum form of the localization congruence relation is chosen as 'canonical' here, since it
shortens some proofs.
To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for
this structure.
To reason about the localization as a quotient type, use `mk_eq_monoid_of_mk'` and associated
lemmas. These show the quotient map `mk : M → S → localization S` equals the
surjection `localization_map.mk'` induced by the map
`monoid_of : localization_map S (localization S)` (where `of` establishes the
localization as a quotient type satisfies the characteristic predicate). The lemma
`mk_eq_monoid_of_mk'` hence gives you access to the results in the rest of the file, which are
about the `localization_map.mk'` induced by any localization map.
## Tags
localization, monoid localization, quotient monoid, congruence relation, characteristic predicate,
commutative monoid
-/
set_option old_structure_cmd true
namespace add_submonoid
variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N]
/-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_inhabited_instance] structure localization_map
extends add_monoid_hom M N :=
(map_add_units' : ∀ y : S, is_add_unit (to_fun y))
(surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1)
(eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c)
/-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/
add_decl_doc localization_map.to_add_monoid_hom
end add_submonoid
variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N]
{P : Type*} [comm_monoid P]
namespace submonoid
/-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N`
satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/
@[nolint has_inhabited_instance] structure localization_map
extends monoid_hom M N :=
(map_units' : ∀ y : S, is_unit (to_fun y))
(surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1)
(eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c)
attribute [to_additive add_submonoid.localization_map] submonoid.localization_map
attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom]
submonoid.localization_map.to_monoid_hom
/-- The monoid hom underlying a `localization_map`. -/
add_decl_doc localization_map.to_monoid_hom
end submonoid
namespace localization
run_cmd to_additive.map_namespace `localization `add_localization
/-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose
quotient is the localization of `M` at `S`, defined as the unique congruence relation on
`M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`,
`(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies
`(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/
@[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S`
an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique
congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where
for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies
`(x₁, y₁) ∼ (x₂, y₂)` by `s`."]
def r (S : submonoid M) : con (M × S) :=
Inf {c | ∀ y : S, c 1 (y, y)}
/-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a
submonoid of `M`, whose quotient is the localization of `M` at `S`. -/
@[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and
`S` a submonoid of `M`, whose quotient is the localization of `M` at `S`."]
def r' : con (M × S) :=
begin
refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c,
iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩,
.. },
{ rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use b.2 * t₁ * t₂,
simp only [submonoid.coe_mul],
calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl
... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl }
... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } },
{ rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩,
use t₁ * t₂,
calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) :
by ac_refl
... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } }
end
/-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed
equivalently as an infimum (see `localization.r`) or explicitly
(see `localization.r'`). -/
@[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a
submonoid can be expressed equivalently as an infimum (see `add_localization.r`) or
explicitly (see `add_localization.r'`)."]
theorem r_eq_r' : r S = r' S :=
le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $
le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩,
begin
rw [← mul_one (p, q), ← mul_one y],
refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _,
convert b.symm (b.mul (b.refl y) (H (q * t))) using 1,
rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc],
refl
end
variables {S}
@[to_additive]
lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c :=
by rw r_eq_r' S; refl
end localization
/-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/
@[to_additive add_localization "The localization of an `add_comm_monoid` at one
of its submonoids (as a quotient type)."]
def localization := (localization.r S).quotient
namespace localization
@[to_additive] instance inhabited :
inhabited (localization S) :=
con.quotient.inhabited
@[to_additive] instance : comm_monoid (localization S) :=
(r S).comm_monoid
variables {S}
/-- Given a `comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence
class of `(x, y)` in the localization of `M` at `S`. -/
@[to_additive "Given an `add_comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to
the equivalence class of `(x, y)` in the localization of `M` at `S`."]
def mk (x : M) (y : S) : localization S := (r S).mk' (x, y)
@[elab_as_eliminator, to_additive]
theorem ind {p : localization S → Prop}
(H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x :=
by rcases x; convert H x; exact prod.mk.eta.symm
@[elab_as_eliminator, to_additive]
theorem induction_on {p : localization S → Prop} (x)
(H : ∀ (y : M × S), p (mk y.1 y.2)) : p x := ind H x
@[elab_as_eliminator, to_additive]
theorem induction_on₂ {p : localization S → localization S → Prop} (x y)
(H : ∀ (x y : M × S), p (mk x.1 x.2) (mk y.1 y.2)) : p x y :=
induction_on x $ λ x, induction_on y $ H x
@[elab_as_eliminator, to_additive]
theorem induction_on₃
{p : localization S → localization S → localization S → Prop} (x y z)
(H : ∀ (x y z : M × S), p (mk x.1 x.2) (mk y.1 y.2) (mk z.1 z.2)) : p x y z :=
induction_on₂ x y $ λ x y, induction_on z $ H x y
@[to_additive] lemma one_rel (y : S) : r S 1 (y, y) := λ b hb, hb y
@[to_additive] theorem r_of_eq {x y : M × S} (h : y.1 * x.2 = x.1 * y.2) : r S x y :=
r_iff_exists.2 ⟨1, by rw h⟩
end localization
variables {S N}
namespace monoid_hom
/-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/
@[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic
predicate."]
def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y))
(H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) :
submonoid.localization_map S N :=
{ map_units' := H1,
surj' := H2,
eq_iff_exists' := H3,
.. f }
end monoid_hom
namespace submonoid
namespace localization_map
/-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/
@[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."]
abbreviation to_map (f : localization_map S N) := f.to_monoid_hom
@[to_additive, ext] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) :
f = g :=
by cases f; cases g; simp only; exact funext h
attribute [ext] add_submonoid.localization_map.ext
@[to_additive] lemma ext_iff {f g : localization_map S N} :
f = g ↔ ∀ x, f.to_map x = g.to_map x :=
⟨λ h x, h ▸ rfl, ext⟩
@[to_additive] lemma to_map_injective :
function.injective (@localization_map.to_map _ _ S N _) :=
λ _ _ h, ext $ monoid_hom.ext_iff.1 h
@[to_additive] lemma map_units (f : localization_map S N) (y : S) :
is_unit (f.to_map y) := f.4 y
@[to_additive] lemma surj (f : localization_map S N) (z : N) :
∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.5 z
@[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} :
f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.6 x y
/-- Given a localization map `f : M →* N`, a section function sending `z : N` to some
`(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/
@[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N`
to some `(x, y) : M × S` such that `f x - f y = z`."]
noncomputable def sec (f : localization_map S N) (z : N) : M × S :=
classical.some $ f.surj z
@[to_additive] lemma sec_spec {f : localization_map S N} (z : N) :
z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 :=
classical.some_spec $ f.surj z
@[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) :
f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z :=
by rw [mul_comm, sec_spec]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all
`w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."]
lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z ↔ w = f y * z :=
by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _;
exact (is_unit.coe_lift_right (f.mrestrict S) h _).symm
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all
`w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."]
lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y))
(y : S) (w z) : z = w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ ↔ z * f y = w :=
by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that
`f(S) ⊆ units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have
`f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/
@[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have
`f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."]
lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} :
f x₁ * ↑(is_unit.lift_right (f.mrestrict S) h y₁)⁻¹ =
f x₂ * ↑(is_unit.lift_right (f.mrestrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) :=
by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂,
f.map_mul, f.map_mul]
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all
`y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."]
lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z}
(h : (is_unit.lift_right (f.mrestrict S) hf y)⁻¹ = (is_unit.lift_right (f.mrestrict S) hf z)⁻¹) :
f y = f z :=
by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h];
convert units.inv_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) hf _).symm
/-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all
`y ∈ S`, `(f y)⁻¹` is unique. -/
@[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that
`f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."]
lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S}
{z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z :=
by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H]
variables (f : localization_map S N)
@[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) :
f.to_map x = f.to_map y :=
begin
rw [f.to_map.map_mul, f.to_map.map_mul] at h,
cases f.map_units c with u hu,
rw ←hu at h,
exact (units.mul_right_inj u).1 h,
end
@[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) :
f.to_map x = f.to_map y :=
f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h]
/-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to
`f x * (f y)⁻¹`. -/
@[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S`
to `f x - f y`."]
noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N :=
f.to_map x * ↑(is_unit.lift_right (f.to_map.mrestrict S) f.map_units y)⁻¹
@[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) :
f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ :=
(mul_inv_left f.map_units _ _ _).2 $
show _ = _ * (_ * _ * (_ * _)), by
rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc,
mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units,
submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul];
ac_refl
@[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x :=
by rw [mk', monoid_hom.map_one]; exact mul_one _
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if
`x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/
@[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N`
we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."]
lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z :=
show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z :=
⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩
@[to_additive] lemma mk'_spec (x) (y : S) :
f.mk' x y * f.to_map y = f.to_map x :=
show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm]
@[to_additive] lemma mk'_spec' (x) (y : S) :
f.to_map y * f.mk' x y = f.to_map x :=
by rw [mul_comm, mk'_spec]
@[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} :
z = f.mk' x y ↔ z * f.to_map y = f.to_map x :=
⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩
@[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} :
f.mk' x y = z ↔ f.to_map x = z * f.to_map y :=
by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm]
@[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} :
f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) :=
⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc,
mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul],
λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc,
←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩
@[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c :=
f.mk'_eq_iff_eq.trans $ f.eq_iff_exists
@[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} :
f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) :=
by rw [f.eq, localization.r_iff_exists]
@[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} :
f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y :=
f.eq_iff_exists.trans g.eq_iff_exists.symm
@[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂}
{y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ :=
f.eq'.trans g.eq'.symm
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`,
if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S`
such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M`
and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists
`c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."]
lemma exists_of_sec_mk' (x) (y : S) :
∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c :=
f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm
@[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) :
f.mk' a₁ a₂ = f.mk' b₁ b₂ :=
f.mk'_eq_iff_eq.2 $ H ▸ rfl
@[simp, to_additive] lemma mk'_self' (y : S) :
f.mk' (y : M) y = 1 :=
show _ * _ = _, by rw [mul_inv_left, mul_one]
@[simp, to_additive] lemma mk'_self (x) (H : x ∈ S) :
f.mk' x ⟨x, H⟩ = 1 :=
by convert mk'_self' _ _; refl
@[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y :=
by rw [←mk'_one, ←mk'_mul, one_mul]
@[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) :
f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y :=
by rw [mul_comm, mul_mk'_eq_mk'_of_mul]
@[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) :
f.to_map x * f.mk' 1 y = f.mk' x y :=
by rw [mul_mk'_eq_mk'_of_mul, mul_one]
@[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) :
f.mk' (x * y) y = f.to_map x :=
by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self', mul_one]
@[to_additive] lemma mk'_mul_cancel_left (x) (y : S) :
f.mk' ((y : M) * x) y = f.to_map x :=
by rw [mul_comm, mk'_mul_cancel_right]
@[to_additive] lemma is_unit_comp (j : N →* P) (y : S) :
is_unit (j.comp f.to_map y) :=
⟨units.map j $ is_unit.lift_right (f.to_map.mrestrict S) f.map_units y,
show j _ = j _, from congr_arg j $
(is_unit.coe_lift_right (f.to_map.mrestrict S) f.map_units _)⟩
variables {g : M →* P}
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y`
for all `x y : M`."]
lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) :
g x = g y :=
begin
obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h,
rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.mrestrict S) hg c],
show _ * (g c * _) = _,
rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm],
end
/-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids
`S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies
`k (g x) = k (g y)`. -/
@[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for
submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y`
implies `k (g x) = k (g y)`."]
lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q]
(hg : ∀ y : S, g y ∈ T) (k : localization_map T Q)
{x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) :=
f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h
variables (hg : ∀ y : S, is_unit (g y))
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from
`N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that
`z = f x * (f y)⁻¹`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism
induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that
`z = f x - f y`."]
noncomputable def lift : N →* P :=
{ to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.mrestrict S) hg (f.sec z).2)⁻¹,
map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg
(by rw [←sec_spec, one_mul]),
map_mul' := λ x y,
begin
rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg,
mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg],
repeat { rw ←g.map_mul },
exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl)
end }
variables {S g}
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s
`g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from
`N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map
of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism
induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."]
lemma lift_mk' (x y) :
f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.mrestrict S) hg y)⁻¹ :=
(mul_inv hg).2 $ f.eq_of_eq hg $ by
rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm]
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have
`f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all
`z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma lift_spec (z v) :
f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v :=
mul_inv_left hg _ _ v
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have
`f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all
`z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such
that `z + f y = f x`."]
lemma lift_spec_mul (z w v) :
f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v :=
begin
rw mul_comm,
show _ * (_ * _) = _ ↔ _,
rw [←mul_assoc, mul_inv_left hg, mul_comm],
end
@[to_additive] lemma lift_mk'_spec (x v) (y : S) :
f.lift hg (f.mk' x y) = v ↔ g x = g y * v :=
by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have
`f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we
have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."]
lemma lift_mul_right (z) :
f.lift hg z * g (f.sec z).2 = g (f.sec z).1 :=
show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one]
/-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map
`g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have
`g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if
an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we
have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."]
lemma lift_mul_left (z) :
g (f.sec z).2 * f.lift hg z = g (f.sec z).1 :=
by rw [mul_comm, lift_mul_right]
@[simp, to_additive] lemma lift_eq (x : M) :
f.lift hg (f.to_map x) = g x :=
by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul])
@[to_additive] lemma lift_eq_iff {x y : M × S} :
f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) :=
by rw [lift_mk', lift_mk', mul_inv hg]
@[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g :=
by ext; exact f.lift_eq hg _
@[simp, to_additive] lemma lift_of_comp (j : N →* P) :
f.lift (f.is_unit_comp j) = j :=
begin
ext,
rw lift_spec,
show j _ = j _ * _,
erw [←j.map_mul, sec_spec'],
end
@[to_additive] lemma epic_of_localization_map {j k : N →* P}
(h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k :=
begin
rw [←f.lift_of_comp j, ←f.lift_of_comp k],
congr' 1 with x, exact h x,
end
@[to_additive] lemma lift_unique {j : N →* P}
(hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j :=
begin
ext,
rw [lift_spec, ←hj, ←hj, ←j.map_mul],
apply congr_arg,
rw ←sec_spec',
end
@[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x :=
monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x
/-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`,
the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P`
induced by `k`. -/
@[simp, to_additive] lemma lift_left_inverse {k : localization_map S P} (z : N) :
k.lift f.map_units (f.lift k.map_units z) = z :=
begin
rw lift_spec,
cases f.surj z with x hx,
conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx},
rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul],
apply k.eq_of_eq f.map_units,
rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul],
repeat { rw ←k.to_map.map_mul },
apply f.eq_of_eq k.map_units,
repeat { rw f.to_map.map_mul },
rw [sec_spec', ←hx],
ac_refl,
end
@[to_additive] lemma lift_surjective_iff :
function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 :=
begin
split,
{ intros H v,
obtain ⟨z, hz⟩ := H v,
obtain ⟨x, hx⟩ := f.surj z,
use x,
rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)],
erw [is_unit.mul_lift_right_inv (g.mrestrict S) hg, mul_one] },
{ intros H v,
obtain ⟨x, hx⟩ := H v,
use f.mk' x.1 x.2,
rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] }
end
@[to_additive] lemma lift_injective_iff :
function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y :=
begin
split,
{ intros H x y,
split,
{ exact f.eq_of_eq hg },
{ intro h,
rw [←f.lift_eq hg, ←f.lift_eq hg] at h,
exact H h }},
{ intros H z w h,
obtain ⟨x, hx⟩ := f.surj z,
obtain ⟨y, hy⟩ := f.surj w,
rw [←f.mk'_sec z, ←f.mk'_sec w],
exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) }
end
variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q]
(k : localization_map T Q)
/-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have
`g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the
localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and
`T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such
that `z = f x * (f y)⁻¹`. -/
@[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids
`S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M`
at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps
for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are
such that `z = f x - f y`."]
noncomputable def map : N →* Q :=
@lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩
variables {k}
@[to_additive] lemma map_eq (x) :
f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x
@[simp, to_additive] lemma map_comp :
(f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩
@[to_additive] lemma map_mk' (x) (y : S) :
f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ :=
begin
rw [map, lift_mk', mul_inv_left],
{ show k.to_map (g x) = k.to_map (g y) * _,
rw mul_mk'_eq_mk'_of_mul,
exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm },
end
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
`u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all
`z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such
that `z + f y = f x`."]
lemma map_spec (z u) :
f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u :=
f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then
for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma map_mul_right (z) :
f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) :=
f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _
/-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a
`comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`,
we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that
`z * f y = f x`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively,
if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all
`z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that
`z + f y = f x`."]
lemma map_mul_left (z) :
k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) :=
by rw [mul_comm, f.map_mul_right]
@[simp, to_additive] lemma map_id (z : N) :
f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z :=
f.lift_id z
/-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition
of the induced maps equals the map of localizations induced by `l ∘ g`. -/
@[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations,
the composition of the induced maps equals the map of localizations induced by `l ∘ g`."]
lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R]
(j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) :
(k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j :=
begin
ext z,
show j.to_map _ * _ = j.to_map (l _) * _,
{ rw [mul_inv_left, ←mul_assoc, mul_inv_right],
show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _,
rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul],
exact k.comp_eq_of_eq hl j
(by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) },
end
/-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition
of the induced maps equals the map of localizations induced by `l ∘ g`. -/
@[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations,
the composition of the induced maps equals the map of localizations induced by `l ∘ g`."]
lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R]
(j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) :
k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x :=
by rw ←f.map_comp_map hy j hl; refl
section away_map
variables (x : M)
/-- Given `x : M`, the type of `comm_monoid` homomorphisms `f : M →* N` such that `N`
is isomorphic to the localization of `M` at the submonoid generated by `x`. -/
@[reducible, to_additive "Given `x : M`, the type of `add_comm_monoid` homomorphisms `f : M →+ N`
such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`."]
def away_map (N' : Type*) [comm_monoid N'] :=
localization_map (powers x) N'
variables (F : away_map x N)
/-- Given `x : M` and a localization map `F : M →* N` away from `x`, `inv_self` is `(F x)⁻¹`. -/
noncomputable def away_map.inv_self : N :=
F.mk' 1 ⟨x, mem_powers _⟩
/-- Given `x : M`, a localization map `F : M →* N` away from `x`, and a map of `comm_monoid`s
`g : M →* P` such that `g x` is invertible, the homomorphism induced from `N` to `P` sending
`z : N` to `g y * (g x)⁻ⁿ`, where `y : M, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/
noncomputable def away_map.lift (hg : is_unit (g x)) : N →* P :=
F.lift $ λ y, show is_unit (g y.1),
begin
obtain ⟨n, hn⟩ := y.2,
rw [←hn, g.map_pow],
exact is_unit.pow n hg,
end
@[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : M) :
F.lift x hg (F.to_map a) = g a := lift_eq _ _ _
@[simp] lemma away_map.lift_comp (hg : is_unit (g x)) :
(F.lift x hg).comp F.to_map = g := lift_comp _ _
/-- Given `x y : M` and localization maps `F : M →* N, G : M →* P` away from `x` and `x * y`
respectively, the homomorphism induced from `N` to `P`. -/
noncomputable def away_to_away_right (y : M) (G : away_map (x * y) P) : N →* P :=
F.lift x $ show is_unit (G.to_map x), from
is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, mem_powers _⟩) $
by rw [mul_mk'_eq_mk'_of_mul, mk'_self]
end away_map
end localization_map
end submonoid
namespace add_submonoid
namespace localization_map
section away_map
variables {A : Type*} [add_comm_monoid A] (x : A) {B : Type*}
[add_comm_monoid B] (F : away_map x B) {C : Type*} [add_comm_monoid C] {g : A →+ C}
/-- Given `x : A` and a localization map `F : A →+ B` away from `x`, `neg_self` is `- (F x)`. -/
noncomputable def away_map.neg_self : B :=
F.mk' 0 ⟨x, mem_multiples _⟩
/-- Given `x : A`, a localization map `F : A →+ B` away from `x`, and a map of `add_comm_monoid`s
`g : A →+ C` such that `g x` is invertible, the homomorphism induced from `B` to `C` sending
`z : B` to `g y - n • g x`, where `y : A, n : ℕ` are such that `z = F y - n • F x`. -/
noncomputable def away_map.lift (hg : is_add_unit (g x)) : B →+ C :=
F.lift $ λ y, show is_add_unit (g y.1),
begin
obtain ⟨n, hn⟩ := y.2,
rw ← hn,
dsimp,
rw [g.map_nsmul],
exact is_add_unit.map (nsmul_add_monoid_hom n) hg,
end
@[simp] lemma away_map.lift_eq (hg : is_add_unit (g x)) (a : A) :
F.lift x hg (F.to_map a) = g a := lift_eq _ _ _
@[simp] lemma away_map.lift_comp (hg : is_add_unit (g x)) :
(F.lift x hg).comp F.to_map = g := lift_comp _ _
/-- Given `x y : A` and localization maps `F : A →+ B, G : A →+ C` away from `x` and `x + y`
respectively, the homomorphism induced from `B` to `C`. -/
noncomputable def away_to_away_right (y : A) (G : away_map (x + y) C) : B →+ C :=
F.lift x $ show is_add_unit (G.to_map x), from
is_add_unit_of_add_eq_zero (G.to_map x) (G.mk' y ⟨x + y, mem_multiples _⟩) $
by rw [add_mk'_eq_mk'_of_add, mk'_self]
end away_map
end localization_map
end add_submonoid
namespace submonoid
namespace localization_map
variables (f : S.localization_map N) {g : M →* P} (hg : ∀ (y : S), is_unit (g y))
{T : submonoid P} {Q : Type*} [comm_monoid Q]
/-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an
isomorphism of `N` and `P`. -/
@[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`,
we get an isomorphism of `N` and `R`."]
noncomputable def mul_equiv_of_localizations
(k : localization_map S P) : N ≃* P :=
⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse,
k.lift_left_inverse, monoid_hom.map_mul _⟩
@[simp, to_additive] lemma mul_equiv_of_localizations_apply
{k : localization_map S P} {x} :
f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl
@[simp, to_additive] lemma mul_equiv_of_localizations_symm_apply
{k : localization_map S P} {x} :
(f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl
@[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations
{k : localization_map S P} :
(k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl
/-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism
of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/
@[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an
isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."]
def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P :=
(k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y)
(λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in
let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩)
(λ x y, k.apply_eq_iff_eq.trans f.eq_iff_exists)
@[simp, to_additive] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) :
(f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl
@[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} :
(f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl
@[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) :
k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x :=
k.symm_apply_apply (f.to_map x)
@[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) :
k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x :=
k.apply_symm_apply (f.to_map x)
@[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} :
(f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y :=
k.to_equiv.eq_symm_apply.symm
@[to_additive add_equiv_of_localizations_right_inv]
lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) :
f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k :=
to_map_injective $ f.lift_comp k.map_units
@[to_additive add_equiv_of_localizations_right_inv_apply, simp]
lemma mul_equiv_of_localizations_right_inv_apply
{k : localization_map S P} {x} :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x :=
ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x
@[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) :
f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k :=
mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom
@[simp, to_additive] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) :
f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x :=
by rw mul_equiv_of_localizations_left_inv
@[simp, to_additive] lemma of_mul_equiv_of_localizations_id :
f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f :=
by ext; refl
@[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} :
(f.of_mul_equiv_of_localizations (k.trans j)).to_map =
j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map :=
by ext; refl
/-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization
map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k`
is a localization map for `T`. -/
@[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is
a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that
`k(T) = S`, `f ∘ k` is a localization map for `T`."]
def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) :
localization_map T N :=
let H' : S.comap k.to_monoid_hom = T :=
H ▸ (set_like.coe_injective $ T.1.preimage_image_eq k.to_equiv.injective) in
(f.to_map.comp k.to_monoid_hom).to_localization_map
(λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩)
(λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in
let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩),
show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩)
(λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists;
exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in
⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc;
exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩,
by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩)
@[simp, to_additive] lemma of_mul_equiv_of_dom_apply
{k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl
@[to_additive] lemma of_mul_equiv_of_dom_eq
{k : P ≃* M} (H : T.map k.to_monoid_hom = S) :
(f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl
@[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M}
(H : T.map k.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x :=
congr_arg f.to_map $ k.apply_symm_apply x
@[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P}
(H : T.map k.symm.to_monoid_hom = S) (x) :
(f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x :=
congr_arg f.to_map $ k.symm_apply_apply x
/-- A special case of `f ∘ id = f`, `f` a localization map. -/
@[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."]
lemma of_mul_equiv_of_dom_id :
f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from
submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f :=
by ext; refl
/-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an
isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations
`N ≃* U`. -/
@[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively,
an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of
localizations `N ≃+ U`."]
noncomputable def mul_equiv_of_mul_equiv
(k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
N ≃* Q :=
f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
f.mul_equiv_of_mul_equiv k H x =
f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl
@[to_additive] lemma mul_equiv_of_mul_equiv_eq_map
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
(f.mul_equiv_of_mul_equiv k H).to_monoid_hom =
f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q}
{j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) :=
f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _
@[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q}
{j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) :
f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ :=
f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _
@[simp, to_additive] lemma of_mul_equiv_of_mul_equiv_apply
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) :=
ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x
@[to_additive] lemma of_mul_equiv_of_mul_equiv
{k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) :
(f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map =
k.to_map.comp j.to_monoid_hom :=
monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H
end localization_map
end submonoid
namespace localization
variables (S)
/-- Natural hom sending `x : M`, `M` a `comm_monoid`, to the equivalence class of
`(x, 1)` in the localization of `M` at a submonoid. -/
@[to_additive "Natural homomorphism sending `x : M`, `M` an `add_comm_monoid`, to the equivalence
class of `(x, 0)` in the localization of `M` at a submonoid."]
def monoid_of : submonoid.localization_map S (localization S) :=
{ map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, (r S).eq.2 $
show r S (_, 1 * y) 1, by simpa using (r S).symm (one_rel y)⟩,
surj' := λ z, induction_on z $ λ x, ⟨x, (r S).eq.2 $
show r S (x.1 * x.2, x.2 * 1) (x.1, 1), by
rw [mul_comm x.2, ←mul_one (x.1, (1 : S))];
exact (r S).mul ((r S).refl (x.1, 1)) ((r S).symm $ one_rel x.2)⟩,
eq_iff_exists' := λ x y, (r S).eq.trans $ r_iff_exists.trans $
show (∃ (c : S), x * 1 * c = y * 1 * c) ↔ _, by rw [mul_one, mul_one],
..(r S).mk'.comp $ monoid_hom.inl M S }
variables {S}
@[to_additive] lemma mk_one_eq_monoid_of_mk (x) : mk x 1 = (monoid_of S).to_map x := rfl
@[to_additive] lemma mk_eq_monoid_of_mk'_apply (x y) : mk x y = (monoid_of S).mk' x y :=
show _ = _ * _, from (submonoid.localization_map.mul_inv_right (monoid_of S).map_units _ _ _).2 $
begin
rw [←mk_one_eq_monoid_of_mk, ←mk_one_eq_monoid_of_mk,
show mk x y * mk y 1 = mk (x * y) (1 * y), by rw mul_comm 1 y; refl,
show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]],
exact (con.eq _).2 (con.symm _ $ (localization.r S).mul
(con.refl _ (x, 1)) $ one_rel _),
end
@[simp, to_additive] lemma mk_eq_monoid_of_mk' : mk = (monoid_of S).mk' :=
funext $ λ _, funext $ λ _, mk_eq_monoid_of_mk'_apply _ _
variables (f : submonoid.localization_map S N)
/-- Given a localization map `f : M →* N` for a submonoid `S`, we get an isomorphism between
the localization of `M` at `S` as a quotient type and `N`. -/
@[to_additive "Given a localization map `f : M →+ N` for a submonoid `S`, we get an isomorphism
between the localization of `M` at `S` as a quotient type and `N`."]
noncomputable def mul_equiv_of_quotient (f : submonoid.localization_map S N) :
localization S ≃* N :=
(monoid_of S).mul_equiv_of_localizations f
variables {f}
@[simp, to_additive] lemma mul_equiv_of_quotient_apply (x) :
mul_equiv_of_quotient f x = (monoid_of S).lift f.map_units x := rfl
@[simp, to_additive] lemma mul_equiv_of_quotient_mk' (x y) :
mul_equiv_of_quotient f ((monoid_of S).mk' x y) = f.mk' x y :=
(monoid_of S).lift_mk' _ _ _
@[to_additive] lemma mul_equiv_of_quotient_mk (x y) :
mul_equiv_of_quotient f (mk x y) = f.mk' x y :=
by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_mk' _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_monoid_of (x) :
mul_equiv_of_quotient f ((monoid_of S).to_map x) = f.to_map x :=
(monoid_of S).lift_eq _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_symm_mk' (x y) :
(mul_equiv_of_quotient f).symm (f.mk' x y) = (monoid_of S).mk' x y :=
f.lift_mk' _ _ _
@[to_additive] lemma mul_equiv_of_quotient_symm_mk (x y) :
(mul_equiv_of_quotient f).symm (f.mk' x y) = mk x y :=
by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_symm_mk' _ _
@[simp, to_additive] lemma mul_equiv_of_quotient_symm_monoid_of (x) :
(mul_equiv_of_quotient f).symm (f.to_map x) = (monoid_of S).to_map x :=
f.lift_eq _ _
section away
variables (x : M)
/-- Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient. -/
@[reducible, to_additive "Given `x : M`, the localization of `M` at the submonoid generated
by `x`, as a quotient."]
def away := localization (submonoid.powers x)
/-- Given `x : M`, `inv_self` is `x⁻¹` in the localization (as a quotient type) of `M` at the
submonoid generated by `x`. -/
@[to_additive "Given `x : M`, `neg_self` is `-x` in the localization (as a quotient type) of `M`
at the submonoid generated by `x`."]
def away.inv_self : away x :=
mk 1 ⟨x, submonoid.mem_powers _⟩
/-- Given `x : M`, the natural hom sending `y : M`, `M` a `comm_monoid`, to the equivalence class
of `(y, 1)` in the localization of `M` at the submonoid generated by `x`. -/
@[reducible, to_additive "Given `x : M`, the natural hom sending `y : M`, `M` an `add_comm_monoid`,
to the equivalence class of `(y, 0)` in the localization of `M` at the submonoid
generated by `x`."]
def away.monoid_of : submonoid.localization_map.away_map x (away x) :=
monoid_of (submonoid.powers x)
@[simp, to_additive] lemma away.mk_eq_monoid_of_mk' : mk = (away.monoid_of x).mk' :=
mk_eq_monoid_of_mk'
/-- Given `x : M` and a localization map `f : M →* N` away from `x`, we get an isomorphism between
the localization of `M` at the submonoid generated by `x` as a quotient type and `N`. -/
@[to_additive "Given `x : M` and a localization map `f : M →+ N` away from `x`, we get an
isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type
and `N`."]
noncomputable def away.mul_equiv_of_quotient (f : submonoid.localization_map.away_map x N) :
away x ≃* N :=
mul_equiv_of_quotient f
end away
end localization
|
48a897cd5bf96d9cafb20a025ddbbdc638f007c6 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/defeq_simplifier1.lean | 03cf70104f7f7fdaeeb0e83e14c7443179deed16 | [
"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 | 1,534 | lean | universe l
constants (A : Type.{l})
definition q (x : A) : A := x
definition h (x : A) : A := q x
definition g (x y : A) := h y
definition f (x y z : A) := g (g x y) z
definition d (x y z w : A) := f (f x y z) (f y z w) (f x w z)
definition h.def [defeq] (x : A) : h x = q x := rfl
definition g.def [defeq] (x y : A) : g x y = h y := rfl
definition f.def [defeq] (x y z : A) : f x y z = g (g x y) z := rfl
definition d.def [defeq] (x y z w : A) : d x y z w = f (f x y z) (f y z w) (f x w z) := rfl
#defeq_simplify env λ x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z))
#defeq_simplify env Π x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z)) = (λ x, x) w
set_option defeq_simplify.exhaustive false
#defeq_simplify env λ x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z))
#defeq_simplify env Π x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z)) = (λ x, x) w
set_option defeq_simplify.top_down true
#defeq_simplify env λ x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z))
#defeq_simplify env Π x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z)) = (λ x, x) w
attribute q [reducible]
set_option defeq_simplify.exhaustive true
set_option defeq_simplify.top_down false
#defeq_simplify env λ x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z))
#defeq_simplify env Π x y z w, (λ x, d x) (g (f x y z) (f z y x)) (g x z) (f y z w) (q (g x z)) = (λ x, x) w
|
287a08df0547c78dbebe4a2ebc21268a725d17d7 | 618003631150032a5676f229d13a079ac875ff77 | /src/geometry/manifold/mfderiv.lean | 9e83eb37f7ee3f1de68f644428c3a31fc11196b1 | [
"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 | 67,201 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import geometry.manifold.basic_smooth_bundle
/-!
# The derivative of functions between smooth manifolds
Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with
corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the
derivative of the function at a point, within a set or along the whole space, mimicking the API
for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and
"f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`).
## Main definitions
* `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have
at most one derivative. This technical condition is important when we define
`mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative,
and many properties will fail (for instance the chain rule). This is analogous to
`unique_diff_on 𝕜 s` in a vector space.
Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API.
* `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent
space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`.
* `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map
from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable
within `s`, this is `0`.
* `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`.
* `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s`
at `x`.
* `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`.
* `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative
within `s` at `x`.
* `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`.
* `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere.
* `bundle_mfderiv I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the
tangent bundle of `M'`.
We also establish results on the differential of the identity, constant functions, charts, extended
charts. For functions between vector spaces, we show that the usual notions and the manifold notions
coincide.
## Implementation notes
The tangent bundle is constructed using the machinery of topological fiber bundles, for which one
can define bundled morphisms and construct canonically maps from the total space of one bundle to
the total space of another one. One could use this mechanism to construct directly the derivative
of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map
is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the
details of the definition of the total space of a fiber bundle constructed from core, to cook up a
suitable definition of the derivative. It is the following: at each point, we have a preferred chart
(used to identify the fiber above the point with the model vector space in fiber bundles). Then one
should read the function using these preferred charts at `x` and `f x`, and take the derivative
of `f` in these charts.
Due to the fact that we are working in a model with corners, with an additional embedding `I` of the
model space `H` in the model vector space `E`, the charts taking values in `E` are not the original
charts of the manifold, but those ones composed with `I`, called extended charts. We
define `written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts.
Then the manifold derivative of `f`, at `x`, is just the usual derivative of
`written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x) x`.
There is a subtelty with respect to continuity: if the function is not continuous, then the image
of a small open set around `x` will not be contained in the source of the preferred chart around
`f x`, which means that when reading `f` in the chart one is losing some information. To avoid this,
we include continuity in the definition of differentiablity (which is reasonable since with any
definition, differentiability implies continuity).
*Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose
that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its
restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property
`mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined
(what values would one choose for vectors that are transverse to `N`?), which can create issues down
the road. The problem here is that knowing the value of `f` along `N` does not determine the
differential of `f` in all directions. This is in contrast to the case where `N` would be an open
subset, or a submanifold with boundary of maximal dimension, where this issue does not appear.
The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists,
and is an assumption in most statements requiring a form of uniqueness.
On a vector space, the manifold derivative and the usual derivative are equal. This means in
particular that they live on the same space, i.e., the tangent space is defeq to the original vector
space. To get this property is a motivation for our definition of the tangent space as a single
copy of the vector space, instead of more usual definitions such as the space of derivations, or
the space of equivalence classes of smooth curves in the manifold.
## Notations
For the composition of local homeomorphisms and local equivs, we use respectively ` ≫ₕ` and ` ≫`.
## Tags
Derivative, manifold
-/
noncomputable theory
open_locale classical topological_space
open set
local infixr ` ≫ₕ `:100 := local_homeomorph.trans
local infixr ` ≫ `:100 := local_equiv.trans
universe u
section derivatives_definitions
/-!
### Derivative of maps between manifolds
The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear
map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined
the tangent space using one specific chart, the formula for the derivative is written in terms of
this specific chart.
We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold".
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [manifold H M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [manifold H' M']
/-- Predicate ensuring that, at a point and within a set, a function can have at most one
derivative. This is expressed using the preferred chart at the considered point. -/
def unique_mdiff_within_at (s : set M) (x : M) :=
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/
def unique_mdiff_on (s : set M) :=
∀x∈s, unique_mdiff_within_at I s x
/-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative
of `f` will just be the derivative of this conjugated function. -/
def written_in_ext_chart_at (x : M) (f : M → M') : E → E' :=
(ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm
/-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds
has a derivative at the point `x` within the set `s`.
This is a generalization of `differentiable_within_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) :=
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds
has a derivative at the point `x`.
This is a generalization of `differentiable_at` to manifolds.
We require continuity in the definition, as otherwise points close to `x` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def mdifferentiable_at (f : M → M') (x : M) :=
continuous_at f x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I)
((ext_chart_at I x) x)
/-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds
has a derivative within `s` at all points of `s`.
This is a generalization of `differentiable_on` to manifolds. -/
def mdifferentiable_on (f : M → M') (s : set M) :=
∀x ∈ s, mdifferentiable_within_at I I' f s x
/-- `mdifferentiable I I' f` indicates that the function `f` between manifolds
has a derivative everywhere.
This is a generalization of `differentiable` to manifolds. -/
def mdifferentiable (f : M → M') :=
∀x, mdifferentiable_at I I' f x
/-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/
def local_homeomorph.mdifferentiable (f : local_homeomorph M M') :=
(mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
/-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds
has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`).
The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`.
We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_within_at (f : M → M') (s : set M) (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_within_at f s x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f'
((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x)
/-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds
has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear
map from the tangent space at `x` to the tangent space at `f x`.
We require continuity in the definition, as otherwise points close to `x` `s` could be sent by
`f` outside of the chart domain around `f x`. Then the chart could do anything to the image points,
and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while
this would not mean anything relevant. -/
def has_mfderiv_at (f : M → M') (x : M)
(f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) :=
continuous_at f x ∧
has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I)
((ext_chart_at I x) x)
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the
derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the
tangent space at `f x`. -/
def mfderiv_within (f : M → M') (s : set M) (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if h : mdifferentiable_within_at I I' f s x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) : _)
else 0
/-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of
`f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/
def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) :=
if h : mdifferentiable_at I I' f x then
(fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I)
((ext_chart_at I x) x) : _)
else 0
/-- The derivative within a set, as a map between the tangent bundles -/
def bundle_mfderiv_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
/-- The derivative, as a map between the tangent bundles -/
def bundle_mfderiv (f : M → M') : tangent_bundle I M → tangent_bundle I' M' :=
λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩
end derivatives_definitions
section derivatives_properties
/-! ### Unique differentiability sets in manifolds -/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [manifold H M] --
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [manifold H' M']
{E'' : Type*} [normed_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [manifold H'' M'']
{f f₀ f₁ : M → M'}
{x : M}
{s t : set M}
{g : M' → M''}
{u : set M'}
lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x :=
begin
unfold unique_mdiff_within_at,
simp only [preimage_univ, univ_inter],
exact I.unique_diff _ (mem_range_self _)
end
variable {I}
lemma unique_mdiff_within_at_iff {s : set M} {x : M} :
unique_mdiff_within_at I s x ↔
unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target)
((ext_chart_at I x) x) :=
begin
apply unique_diff_within_at_congr,
rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_target_eq]
end
lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) :
unique_mdiff_within_at I t x :=
unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _)
lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ nhds_within x s) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq],
exact unique_diff_within_at.inter' hs (ext_chart_preimage_mem_nhds_within I x ht)
end
lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) :
unique_mdiff_within_at I (s ∩ t) x :=
begin
rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq],
exact unique_diff_within_at.inter hs (ext_chart_preimage_mem_nhds I x ht)
end
lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x :=
begin
have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (mem_nhds_sets hs xs),
rwa univ_inter at this
end
lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) : unique_mdiff_on I (s ∩ t) :=
λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (mem_nhds_sets ht hx.2)
lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s :=
λx hx, is_open.unique_mdiff_within_at hx hs
/- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are
necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we
want to include them or omit them when necessary. -/
variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M']
[I''s : smooth_manifold_with_corners I'' M'']
{f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)}
{g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))}
/-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/
theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' :=
U.eq h.2 h₁.2
theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s)
(h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' :=
unique_mdiff_within_at.eq (U _ hx) h h₁
/-!
### General lemmas on derivatives of functions between manifolds
We mimick the API for functions between vector spaces
-/
lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} :
mdifferentiable_within_at I I' f s x ↔
continuous_within_at f s x ∧
differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f)
((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) :=
begin
refine and_congr iff.rfl (exists_congr $ λ f', _),
rw [inter_comm],
simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_target_eq]
end
include Is I's
lemma mfderiv_within_zero_of_not_mdifferentiable_within_at
(h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 :=
by simp [mfderiv_within, h]
lemma mfderiv_zero_of_not_mdifferentiable_at
(h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 :=
by simp [mfderiv, h]
theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) :
has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_within_at.mono h.1 hst,
has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
theorem has_mfderiv_at.has_mfderiv_within_at
(h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' :=
⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩
lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') :
mdifferentiable_within_at I I' f s x :=
⟨h.1, ⟨f', h.2⟩⟩
lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') :
mdifferentiable_at I I' f x :=
⟨h.1, ⟨f', h.2⟩⟩
@[simp] lemma has_mfderiv_within_at_univ :
has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' :=
by simp [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ]
theorem has_mfderiv_at_unique
(h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' :=
begin
rw ← has_mfderiv_within_at_univ at h₀ h₁,
exact (unique_mdiff_within_at_univ I).eq h₀ h₁
end
lemma has_mfderiv_within_at_inter' (h : t ∈ nhds_within x s) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq,
has_fderiv_within_at_inter', continuous_within_at_inter' h],
exact ext_chart_preimage_mem_nhds_within I x h,
end
lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) :
has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' :=
begin
rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq,
has_fderiv_within_at_inter, continuous_within_at_inter h],
exact ext_chart_preimage_mem_nhds I x h,
end
lemma has_mfderiv_within_at.union
(hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') :
has_mfderiv_within_at I I' f (s ∪ t) x f' :=
begin
split,
{ exact continuous_within_at.union hs.1 ht.1 },
{ convert has_fderiv_within_at.union hs.2 ht.2,
simp [union_inter_distrib_right] }
end
lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f')
(ht : s ∈ nhds_within x t) : has_mfderiv_within_at I I' f t x f' :=
(has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _))
lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f') (hs : s ∈ 𝓝 x) :
has_mfderiv_at I I' f x f' :=
by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h
lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) :
has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) :=
begin
refine ⟨h.1, _⟩,
simp [mfderiv_within, h],
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) :
(mfderiv_within I I' f s x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I)
((ext_chart_at I x) x) :=
by simp [mfderiv_within, h]
lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) :
has_mfderiv_at I I' f x (mfderiv I I' f x) :=
begin
refine ⟨h.1, _⟩,
simp [mfderiv, h],
exact differentiable_within_at.has_fderiv_within_at h.2
end
lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) :
(mfderiv I I' f x) =
fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) :=
by simp [mfderiv, h]
lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') :
mfderiv I I' f x = f' :=
by { ext, rw has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at }
lemma has_mfderiv_within_at.mfderiv_within
(h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = f' :=
by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at }
lemma mdifferentiable.mfderiv_within
(h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f s x = mfderiv I I' f x :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact h.has_mfderiv_at.has_mfderiv_within_at
end
lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x)
(h : mdifferentiable_within_at I I' f t x) :
mfderiv_within I I' f s x = mfderiv_within I I' f t x :=
((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs
omit Is I's
lemma mdifferentiable_within_at.mono (hst : s ⊆ t)
(h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x :=
⟨ continuous_within_at.mono h.1 hst,
differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩
lemma mdifferentiable_within_at_univ :
mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x :=
by simp [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ]
lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq,
differentiable_within_at_inter, continuous_within_at_inter ht],
exact ext_chart_preimage_mem_nhds I x ht
end
lemma mdifferentiable_within_at_inter' (ht : t ∈ nhds_within x s) :
mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x :=
begin
rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq,
differentiable_within_at_inter', continuous_within_at_inter' ht],
exact ext_chart_preimage_mem_nhds_within I x ht
end
lemma mdifferentiable_at.mdifferentiable_within_at
(h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x :=
mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h)
lemma mdifferentiable_within_at.mdifferentiable_at
(h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h,
end
lemma mdifferentiable_on.mono
(h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s :=
λx hx, (h x (st hx)).mono st
lemma mdifferentiable_on_univ :
mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f :=
by { simp [mdifferentiable_on, mdifferentiable_within_at_univ], refl }
lemma mdifferentiable.mdifferentiable_on
(h : mdifferentiable I I' f) : mdifferentiable_on I I' f s :=
(mdifferentiable_on_univ.2 h).mono (subset_univ _)
lemma mdifferentiable_on_of_locally_mdifferentiable_on
(h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) : mdifferentiable_on I I' f s :=
begin
assume x xs,
rcases h x xs with ⟨t, t_open, xt, ht⟩,
exact (mdifferentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩)
end
include Is I's
@[simp] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f :=
begin
ext x : 1,
simp [mfderiv_within, mfderiv],
erw mdifferentiable_within_at_univ
end
lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) :
mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x :=
by erw [mfderiv_within, mfderiv_within, ext_chart_preimage_inter_eq,
mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_preimage_mem_nhds I x ht) hs]
omit Is I's
/-! ### Deriving continuity from differentiability on manifolds -/
theorem has_mfderiv_within_at.continuous_within_at
(h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x :=
h.1
theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') :
continuous_at f x :=
h.1
lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) :
continuous_within_at f s x :=
h.1
lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x :=
h.1
lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s :=
λx hx, (h x hx).continuous_within_at
lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f :=
continuous_iff_continuous_at.2 $ λx, (h x).continuous_at
include Is I's
lemma bundle_mfderiv_within_subset {p : tangent_bundle I M}
(st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) :
bundle_mfderiv_within I I' f s p = bundle_mfderiv_within I I' f t p :=
by { simp [bundle_mfderiv_within], rw mfderiv_within_subset st hs h }
lemma bundle_mfderiv_within_univ :
bundle_mfderiv_within I I' f univ = bundle_mfderiv I I' f :=
by { ext p : 1, simp [bundle_mfderiv_within, bundle_mfderiv] }
lemma bundle_mfderiv_within_eq_bundle_mfderiv {p : tangent_bundle I M}
(hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) :
bundle_mfderiv_within I I' f s p = bundle_mfderiv I I' f p :=
begin
rw ← mdifferentiable_within_at_univ at h,
rw ← bundle_mfderiv_within_univ,
exact bundle_mfderiv_within_subset (subset_univ _) hs h,
end
@[simp] lemma bundle_mfderiv_within_tangent_bundle_proj {p : tangent_bundle I M} :
tangent_bundle.proj I' M' (bundle_mfderiv_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl
@[simp] lemma bundle_mfderiv_within_proj {p : tangent_bundle I M} :
(bundle_mfderiv_within I I' f s p).1 = f p.1 := rfl
@[simp] lemma bundle_mfderiv_tangent_bundle_proj {p : tangent_bundle I M} :
tangent_bundle.proj I' M' (bundle_mfderiv I I' f p) = f (tangent_bundle.proj I M p) := rfl
@[simp] lemma bundle_mfderiv_proj {p : tangent_bundle I M} :
(bundle_mfderiv I I' f p).1 = f p.1 := rfl
omit Is I's
/-! ### Congruence lemmas for derivatives on manifolds -/
lemma has_mfderiv_within_at.congr_of_mem_nhds_within (h : has_mfderiv_within_at I I' f s x f')
(h₁ : ∀ᶠ y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' :=
begin
refine ⟨continuous_within_at.congr_of_mem_nhds_within h.1 h₁ hx, _⟩,
apply has_fderiv_within_at.congr_of_mem_nhds_within h.2,
{ have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈
nhds_within ((ext_chart_at I x) x) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) :=
ext_chart_preimage_mem_nhds_within I x h₁,
apply filter.mem_sets_of_superset this (λy, _),
simp [written_in_ext_chart_at, hx] {contextual := tt} },
{ simp [written_in_ext_chart_at, hx] },
end
lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f')
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
has_mfderiv_within_at I I' f₁ t x f' :=
(h.mono h₁).congr_of_mem_nhds_within (filter.mem_inf_sets_of_right ht) hx
lemma has_mfderiv_at.congr_of_mem_nhds (h : has_mfderiv_at I I' f x f')
(h₁ : ∀ᶠ y in 𝓝 x, f₁ y = f y) : has_mfderiv_at I I' f₁ x f' :=
begin
erw ← has_mfderiv_within_at_univ at ⊢ h,
apply h.congr_of_mem_nhds_within _ (mem_of_nhds h₁ : _),
rwa nhds_within_univ
end
include Is I's
lemma mdifferentiable_within_at.congr_of_mem_nhds_within
(h : mdifferentiable_within_at I I' f s x) (h₁ : ∀ᶠ y in nhds_within x s, f₁ y = f y)
(hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(h.has_mfderiv_within_at.congr_of_mem_nhds_within h₁ hx).mdifferentiable_within_at
variables (I I')
lemma mdifferentiable_within_at_congr_of_mem_nhds_within
(h₁ : ∀ᶠ y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) :
mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x :=
begin
split,
{ assume h,
apply h.congr_of_mem_nhds_within h₁ hx },
{ assume h,
apply h.congr_of_mem_nhds_within _ hx.symm,
apply h₁.mono,
intro y,
apply eq.symm }
end
variables {I I'}
lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_within_at I I' f₁ t x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at
lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x)
(ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx (subset.refl _)).mdifferentiable_within_at
lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x)
(h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t :=
λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁
lemma mdifferentiable_at.congr_of_mem_nhds (h : mdifferentiable_at I I' f x)
(hL : ∀ᶠ y in 𝓝 x, f₁ y = f y) : mdifferentiable_at I I' f₁ x :=
((h.has_mfderiv_at).congr_of_mem_nhds hL).mdifferentiable_at
lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x)
(hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) :
mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) :=
(has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt
lemma mfderiv_within_congr_of_mem_nhds_within (hs : unique_mdiff_within_at I s x)
(hL : ∀ᶠ y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) :
mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) :=
begin
by_cases h : mdifferentiable_within_at I I' f s x,
{ exact ((h.has_mfderiv_within_at).congr_of_mem_nhds_within hL hx).mfderiv_within hs },
{ unfold mfderiv_within,
rw [dif_neg h, dif_neg],
rwa ← mdifferentiable_within_at_congr_of_mem_nhds_within I I' hL hx }
end
lemma mfderiv_congr_of_mem_nhds (hL : ∀ᶠ y in 𝓝 x, f₁ y = f y) :
mfderiv I I' f₁ x = (mfderiv I I' f x : _) :=
begin
have A : f₁ x = f x := (mem_of_nhds hL : _),
rw [← mfderiv_within_univ, ← mfderiv_within_univ],
rw ← nhds_within_univ at hL,
exact mfderiv_within_congr_of_mem_nhds_within (unique_mdiff_within_at_univ I) hL A
end
/-! ### Composition lemmas -/
omit Is I's
lemma written_in_ext_chart_comp (h : continuous_within_at f s x) :
{y | written_in_ext_chart_at I I'' x (g ∘ f) y
= ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y}
∈ nhds_within ((ext_chart_at I x) x) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) :=
begin
apply @filter.mem_sets_of_superset _ _
((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _
(ext_chart_preimage_mem_nhds_within I x (h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
assume y hy,
simp only [ext_chart_at, written_in_ext_chart_at, mem_set_of_eq, function.comp_app,
local_equiv.coe_trans, local_equiv.coe_trans_symm, local_homeomorph.coe_coe_symm,
local_homeomorph.coe_coe, mem_univ, local_equiv.left_inv, model_with_corners.source_eq],
rw (chart_at H' (f x)).left_inv,
simpa [ext_chart_at_source] using hy
end
variable (x)
include Is I's I''s
theorem has_mfderiv_within_at.comp
(hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f')
(hst : s ⊆ f ⁻¹' u) :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩,
have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘
(written_in_ext_chart_at I I' x f))
(continuous_linear_map.comp g' f' : E →L[𝕜] E'')
((ext_chart_at I x).symm ⁻¹' s ∩ range (I))
((ext_chart_at I x) x),
{ have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source)
∈ nhds_within ((ext_chart_at I x) x) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) :=
(ext_chart_preimage_mem_nhds_within I x
(hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))),
unfold has_mfderiv_within_at at *,
rw [← has_fderiv_within_at_inter' this, ← ext_chart_preimage_inter_eq] at hf ⊢,
have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x)
= (ext_chart_at I' (f x)) (f x),
by simp [written_in_ext_chart_at, local_equiv.left_inv, mem_chart_source],
rw ← this at hg,
apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _,
assume y hy,
simp [ext_chart_at_coe_symm, local_equiv.trans_source, -mem_range, ext_chart_at_source] at hy,
have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1,
simp [written_in_ext_chart_at, ext_chart_at_coe_symm, -mem_range, hy, this, mem_range_self,
ext_chart_at_coe] },
apply A.congr_of_mem_nhds_within (written_in_ext_chart_comp hf.1),
simp [written_in_ext_chart_at, ext_chart_at, local_equiv.left_inv, mem_chart_source]
end
/-- The chain rule. -/
theorem has_mfderiv_at.comp
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') :
has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
theorem has_mfderiv_at.comp_has_mfderiv_within_at
(hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') :
has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') :=
begin
rw ← has_mfderiv_within_at_univ at *,
exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ
end
lemma mdifferentiable_within_at.comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x :=
begin
rcases hf.2 with ⟨f', hf'⟩,
have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩,
rcases hg.2 with ⟨g', hg'⟩,
have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩,
exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at
end
lemma mdifferentiable_at.comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mdifferentiable_at I I'' (g ∘ f) x :=
(hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at
lemma mfderiv_within_comp
(hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x)
(h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I'' (g ∘ f) s x = (mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) :=
begin
apply has_mfderiv_within_at.mfderiv_within _ hxs,
exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h
end
lemma mfderiv_comp
(hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) :
mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) :=
begin
apply has_mfderiv_at.mfderiv,
exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at
end
lemma mdifferentiable_on.comp
(hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) :
mdifferentiable_on I I'' (g ∘ f) s :=
λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st
lemma mdifferentiable.comp
(hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) :=
λx, mdifferentiable_at.comp x (hg (f x)) (hf x)
lemma bundle_mfderiv_within_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1)
(h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) :
bundle_mfderiv_within I I'' (g ∘ f) s p =
bundle_mfderiv_within I' I'' g u (bundle_mfderiv_within I I' f s p) :=
begin
simp [bundle_mfderiv_within],
rw mfderiv_within_comp p.1 hg hf h hps,
refl
end
lemma bundle_mfderiv_comp_at (p : tangent_bundle I M)
(hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) :
bundle_mfderiv I I'' (g ∘ f) p = bundle_mfderiv I' I'' g (bundle_mfderiv I I' f p) :=
begin
rcases p with ⟨x, v⟩,
simp [bundle_mfderiv],
rw mfderiv_comp x hg hf,
refl
end
lemma bundle_mfderiv_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) :
bundle_mfderiv I I'' (g ∘ f) = (bundle_mfderiv I' I'' g) ∘ (bundle_mfderiv I I' f) :=
by { ext p : 1, exact bundle_mfderiv_comp_at _ (hg _) (hf _) }
end derivatives_properties
section specific_functions
/-! ### Differentiability of specific functions -/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [manifold H M] [smooth_manifold_with_corners I M]
{s : set M} {x : M}
section id
/-! #### Identity -/
lemma has_mfderiv_at_id (x : M) :
has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
refine ⟨continuous_id.continuous_at, _⟩,
have : ∀ᶠ y in nhds_within ((ext_chart_at I x) x) (range (I)),
((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y,
{ apply filter.mem_sets_of_superset (ext_chart_at_target_mem_nhds_within I x),
assume y hy,
simp [(ext_chart_at I x).right_inv hy] },
apply has_fderiv_within_at.congr_of_mem_nhds_within (has_fderiv_within_at_id _ _) this,
simp [(ext_chart_at I x).left_inv, mem_ext_chart_source I x]
end
theorem has_mfderiv_within_at_id (s : set M) (x : M) :
has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
(has_mfderiv_at_id I x).has_mfderiv_within_at
lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x :=
(has_mfderiv_at_id I x).mdifferentiable_at
lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x :=
(mdifferentiable_at_id I).mdifferentiable_within_at
lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) :=
λx, mdifferentiable_at_id I
lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s :=
(mdifferentiable_id I).mdifferentiable_on
@[simp] lemma mfderiv_id :
mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_id I x)
lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) :=
begin
rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs,
exact mfderiv_id I
end
end id
section const
/-! #### Constants -/
variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
{M' : Type*} [topological_space M'] [manifold H' M'] [smooth_manifold_with_corners I' M']
{c : M'}
lemma has_mfderiv_at_const (c : M') (x : M) :
has_mfderiv_at I I' (λy : M, c) x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
begin
refine ⟨continuous_const.continuous_at, _⟩,
have : (ext_chart_at I' c) ∘ (λ (y : M), c) ∘ (ext_chart_at I x).symm =
(λy, (ext_chart_at I' c) c) := rfl,
rw [written_in_ext_chart_at, this],
apply has_fderiv_within_at_const
end
theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) :
has_mfderiv_within_at I I' (λy : M, c) s x
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
(has_mfderiv_at_const I I' c x).has_mfderiv_within_at
lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x :=
(has_mfderiv_at_const I I' c x).mdifferentiable_at
lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x :=
(mdifferentiable_at_const I I').mdifferentiable_within_at
lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) :=
λx, mdifferentiable_at_const I I'
lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s :=
(mdifferentiable_const I I').mdifferentiable_on
@[simp] lemma mfderiv_const : mfderiv I I' (λy : M, c) x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x)
lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) :
mfderiv_within I I' (λy : M, c) s x =
(0 : tangent_space I x →L[𝕜] tangent_space I' c) :=
begin
rw mdifferentiable.mfderiv_within (mdifferentiable_at_const I I') hxs,
{ exact mfderiv_const I I' },
{ apply_instance }
end
end const
section model_with_corners
/-! #### Model with corners -/
lemma model_with_corners.mdifferentiable :
mdifferentiable I (model_with_corners_self 𝕜 E) I :=
begin
simp only [mdifferentiable, mdifferentiable_at, written_in_ext_chart_at, ext_chart_at,
local_equiv.refl_coe, chart_at_model_space_eq, function.comp.left_id,
local_homeomorph.refl_coe, id.def, ext_chart_model_space_eq_id, local_equiv.refl_trans,
model_with_corners.to_local_equiv_coe_symm, local_homeomorph.refl_local_equiv,
model_with_corners.to_local_equiv_coe],
assume x,
refine ⟨I.continuous.continuous_at, _⟩,
have : differentiable_within_at 𝕜 id (range I) (I x) :=
differentiable_at_id.differentiable_within_at,
apply this.congr,
{ simp [model_with_corners.right_inv] {contextual := tt} },
{ simp [model_with_corners.left_inv] }
end
lemma model_with_corners.mdifferentiable_on_symm :
mdifferentiable_on (model_with_corners_self 𝕜 E) I I.symm (range I) :=
begin
simp only [mdifferentiable_on, mdifferentiable_within_at, written_in_ext_chart_at, ext_chart_at,
local_equiv.refl_trans, local_equiv.refl_coe, preimage_id, id.def, inter_univ,
model_with_corners_self_local_equiv, range_id, function.comp.right_id, chart_at_model_space_eq,
local_homeomorph.refl_local_equiv, local_equiv.refl_symm,
model_with_corners_self_coe, model_with_corners.to_local_equiv_coe],
assume x hx,
refine ⟨I.continuous_symm.continuous_at.continuous_within_at, _⟩,
have : differentiable_within_at 𝕜 id (range I) x := differentiable_at_id.differentiable_within_at,
apply this.congr,
{ simp [model_with_corners.right_inv] {contextual := tt} },
{ simp [model_with_corners.right_inv, hx] }
end
end model_with_corners
section charts
variable {e : local_homeomorph M H}
lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I e x :=
begin
refine ⟨(e.continuous_on x hx).continuous_at (mem_nhds_sets e.open_source hx), _⟩,
have mem : I ((chart_at H x : M → H) x) ∈
I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I,
by simp [hx, local_equiv.trans_source, -mem_range, mem_range_self],
have : (chart_at H x).symm.trans e ∈ times_cont_diff_groupoid ⊤ I :=
has_groupoid.compatible _ (chart_mem_atlas H x) h,
have A : times_cont_diff_on 𝕜 ⊤
(I ∘ ((chart_at H x).symm.trans e) ∘ I.symm)
(I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) :=
this.1,
have B := A.differentiable_on (by simp) (I ((chart_at H x : M → H) x)) mem,
simp only [local_homeomorph.coe_trans, local_homeomorph.trans_to_local_equiv,
local_homeomorph.symm_to_local_equiv] at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa [written_in_ext_chart_at, ext_chart_at] },
{ apply mem_nhds_sets (I.continuous_symm _ (local_homeomorph.open_source _)) mem.1 }
end
lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) :
mdifferentiable_on I I e e.source :=
λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at
lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) :
mdifferentiable_at I I e.symm x :=
begin
refine ⟨(e.continuous_on_symm x hx).continuous_at (mem_nhds_sets e.open_target hx), _⟩,
have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I),
by simp only [local_equiv.trans_source, mem_preimage, mem_inter_eq, model_with_corners.left_inv,
hx, true_and, mem_range_self, mem_chart_source, local_homeomorph.coe_coe_symm,
local_homeomorph.trans_to_local_equiv, local_equiv.symm_source,
local_homeomorph.symm_to_local_equiv],
have : e.symm.trans (chart_at H (e.symm x)) ∈ times_cont_diff_groupoid ⊤ I :=
has_groupoid.compatible _ h (chart_mem_atlas H _),
have A : times_cont_diff_on 𝕜 ⊤
(I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm)
(I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) :=
this.1,
have B := A.differentiable_on (by simp) (I x) mem,
simp only [local_homeomorph.coe_trans, local_homeomorph.trans_to_local_equiv,
local_homeomorph.symm_to_local_equiv] at B,
rw [inter_comm, differentiable_within_at_inter] at B,
{ simpa [written_in_ext_chart_at, ext_chart_at] },
{ apply (mem_nhds_sets (I.continuous_symm _ (local_homeomorph.open_source _)) mem.1) }
end
lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) :
mdifferentiable_on I I e.symm e.target :=
λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at
lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I :=
⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩
lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I :=
mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)
/-- The derivative of the chart at a base point is the chart of the tangent bundle. -/
lemma bundle_mfderiv_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) :
bundle_mfderiv I I (chart_at H p.1) q = (chart_at (H × E) p : tangent_bundle I M → H × E) q :=
begin
dsimp [bundle_mfderiv],
rw mdifferentiable_at.mfderiv,
{ refl },
{ exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h }
end
/-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the
tangent bundle. -/
lemma bundle_mfderiv_chart_symm {p : tangent_bundle I M} {q : H × E}
(h : q.1 ∈ (chart_at H p.1).target) :
bundle_mfderiv I I (chart_at H p.1).symm q =
((chart_at (H × E) p).symm : H × E → tangent_bundle I M) q :=
begin
dsimp only [bundle_mfderiv],
rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h),
-- a trivial instance is needed after the rewrite, handle it right now.
rotate, { apply_instance },
dsimp [written_in_ext_chart_at, ext_chart_at, chart_at, manifold.chart_at,
basic_smooth_bundle_core.chart, basic_smooth_bundle_core.to_topological_fiber_bundle_core,
topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv',
tangent_bundle_core],
rw local_homeomorph.right_inv,
exact h
end
end charts
end specific_functions
section mfderiv_fderiv
/-!
### Relations between vector space derivative and manifold derivative
The manifold derivative `mfderiv`, when considered on the model vector space with its trivial
manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove
this and related statements.
-/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{f : E → E'} {s : set E} {x : E}
lemma unique_mdiff_within_at_iff_unique_diff_within_at :
unique_mdiff_within_at (model_with_corners_self 𝕜 E) s x ↔ unique_diff_within_at 𝕜 s x :=
by simp [unique_mdiff_within_at]
lemma unique_mdiff_on_iff_unique_diff_on :
unique_mdiff_on (model_with_corners_self 𝕜 E) s ↔ unique_diff_on 𝕜 s :=
by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at]
@[simp] lemma written_in_ext_chart_model_space :
written_in_ext_chart_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') x f = f :=
by { ext y, simp [written_in_ext_chart_at] }
/-- For maps between vector spaces, mdifferentiable_within_at and fdifferentiable_within_at coincide -/
theorem mdifferentiable_within_at_iff_differentiable_within_at :
mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x
↔ differentiable_within_at 𝕜 f s x :=
begin
simp [mdifferentiable_within_at],
exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩
end
/-- For maps between vector spaces, mdifferentiable_at and differentiable_at coincide -/
theorem mdifferentiable_at_iff_differentiable_at :
mdifferentiable_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x
↔ differentiable_at 𝕜 f x :=
begin
simp [mdifferentiable_at, differentiable_within_at_univ],
exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩
end
/-- For maps between vector spaces, mdifferentiable_on and differentiable_on coincide -/
theorem mdifferentiable_on_iff_differentiable_on :
mdifferentiable_on (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s
↔ differentiable_on 𝕜 f s :=
by simp [mdifferentiable_on, differentiable_on, mdifferentiable_within_at_iff_differentiable_within_at]
/-- For maps between vector spaces, mdifferentiable and differentiable coincide -/
theorem mdifferentiable_iff_differentiable :
mdifferentiable (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f
↔ differentiable 𝕜 f :=
by simp [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at]
/-- For maps between vector spaces, mfderiv_within and fderiv_within coincide -/
theorem mfderiv_within_eq_fderiv_within :
mfderiv_within (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x
= fderiv_within 𝕜 f s x :=
begin
by_cases h : mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x,
{ simp [mfderiv_within, h] },
{ simp [mfderiv_within, h],
rw [mdifferentiable_within_at_iff_differentiable_within_at,
differentiable_within_at] at h,
change ¬(∃(f' : tangent_space (model_with_corners_self 𝕜 E) x →L[𝕜]
tangent_space (model_with_corners_self 𝕜 E') (f x)),
has_fderiv_within_at f f' s x) at h,
simp [fderiv_within, h] }
end
/-- For maps between vector spaces, mfderiv and fderiv coincide -/
theorem mfderiv_eq_fderiv :
mfderiv (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x = fderiv 𝕜 f x :=
begin
rw [← mfderiv_within_univ, ← fderiv_within_univ],
exact mfderiv_within_eq_fderiv_within
end
end mfderiv_fderiv
/-! ### Differentiable local homeomorphisms -/
namespace local_homeomorph.mdifferentiable
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [manifold H M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [manifold H' M']
{E'' : Type*} [normed_group E''] [normed_space 𝕜 E'']
{H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''}
{M'' : Type*} [topological_space M''] [manifold H'' M'']
{e : local_homeomorph M M'} (he : e.mdifferentiable I I')
{e' : local_homeomorph M' M''}
include he
lemma symm : e.symm.mdifferentiable I' I :=
⟨he.2, he.1⟩
protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) :
mdifferentiable_at I I' e x :=
(he.1 x hx).mdifferentiable_at (mem_nhds_sets e.open_source hx)
lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) :
mdifferentiable_at I' I e.symm x :=
(he.2 x hx).mdifferentiable_at (mem_nhds_sets e.open_target hx)
variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M']
[smooth_manifold_with_corners I'' M'']
lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) :
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) =
continuous_linear_map.id 𝕜 (tangent_space I x) :=
begin
have : (mfderiv I I (e.symm ∘ e) x) =
(mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) :=
mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx),
rw ← this,
have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I,
rw ← this,
apply mfderiv_congr_of_mem_nhds,
have : e.source ∈ 𝓝 x := mem_nhds_sets e.open_source hx,
apply filter.mem_sets_of_superset this,
assume p hp,
simp [e.left_inv, hp]
end
lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) :
(mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) =
continuous_linear_map.id 𝕜 (tangent_space I' x) :=
he.symm.symm_comp_deriv hx
/-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence
between the tangent spaces at `x` and `e x`. -/
protected def mfderiv {x : M} (hx : x ∈ e.source) :
tangent_space I x ≃L[𝕜] tangent_space I' (e x) :=
{ inv_fun := (mfderiv I' I e.symm (e x)),
continuous_to_fun := (mfderiv I I' e x).cont,
continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont,
left_inv := λy, begin
have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl,
conv_rhs { rw [← this, ← he.symm_comp_deriv hx] },
refl
end,
right_inv := λy, begin
have : (continuous_linear_map.id 𝕜 _ :
tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl,
conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] },
rw e.left_inv hx,
refl
end,
.. mfderiv I I' e x }
lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) :
range (mfderiv I I' e x) = univ :=
(he.mfderiv hx).to_linear_equiv.to_equiv.range_eq_univ
lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' :=
begin
split,
{ assume x hx,
simp [local_equiv.trans_source] at hx,
exact ((he'.mdifferentiable_at hx.2).comp _
(he.mdifferentiable_at hx.1)).mdifferentiable_within_at },
{ assume x hx,
simp [local_equiv.trans_target] at hx,
exact ((he.symm.mdifferentiable_at hx.2).comp _
(he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at }
end
end local_homeomorph.mdifferentiable
/-! ### Unique derivative sets in manifolds -/
section unique_mdiff
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{M : Type*} [topological_space M] [manifold H M] [smooth_manifold_with_corners I M]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M' : Type*} [topological_space M'] [manifold H' M']
{s : set M}
/-- If a set has the unique differential property, then its image under a local
diffeomorphism also has the unique differential property. -/
lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M']
(hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') :
unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) :=
begin
/- Start from a point `x` in the image, and let `z` be its preimage. Then the unique
derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique
derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that
the composition of these two charts with `e` is a local diffeomorphism in vector spaces,
and therefore preserves the unique differential property thanks to lemma
`has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto
derivative preserves the unique derivative property.-/
assume x hx,
let z := e.symm x,
have z_source : z ∈ e.source, by simp [hx.1, local_equiv.map_target],
have zx : e z = x, by simp [z, hx.1],
let F := ext_chart_at I z,
-- the unique derivative property at `z` is expressed through its preferred chart, that we call `F`.
have B : unique_diff_within_at 𝕜
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z),
{ have : unique_mdiff_within_at I s z := hs _ hx.2,
have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z,
{ apply mem_nhds_sets,
apply e.continuous_on.preimage_open_of_open e.open_source (ext_chart_at_open_source I' x),
simp [z_source, zx] },
have := this.inter S,
rw [unique_mdiff_within_at_iff] at this,
exact this },
-- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and
-- of `e`
let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x),
-- `G` is differentiable
have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I',
{ have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z),
have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x),
exact A.symm.trans (he.trans B) },
have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source,
by simp [local_equiv.trans_source, local_equiv.map_source, z_source, zx],
have A : differentiable_within_at 𝕜 G (range I) (F z),
{ refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _;
simp [G, written_in_ext_chart_at, ext_chart_at, F] },
-- let `G'` be its derivative
let G' := fderiv_within 𝕜 G (range I) (F z),
have D₁ : has_fderiv_within_at G G' (range I) (F z) :=
A.has_fderiv_within_at,
have D₂ : has_fderiv_within_at G G'
(F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z),
{ apply D₁.mono,
refine subset.trans (inter_subset_right _ _) _,
simp [F, ext_chart_at, local_equiv.trans_target] },
-- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition
-- of the two charts and of `e`.
have C₁ : range (G' : E → E') = univ,
{ have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)) ((chart_at H z : M → H) z),
by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl },
rw this,
exact Diff.range_mfderiv_eq_univ Mmem },
have C₂ : closure (range (G' : E → E')) = univ, by rw [C₁, closure_univ],
-- key step: thanks to what we have proved about it, `G` preserves the unique derivative property
have key : unique_diff_within_at 𝕜
(G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target))
(G (F z)) := D₂.unique_diff_within_at B C₂,
have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp [hx.1] },
rw this at key,
apply key.mono,
show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆
(ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩
range (I'),
rw image_subset_iff,
rintros p ⟨⟨hp₁, ⟨hp₂, hp₄⟩⟩, hp₃⟩,
simp [G, local_equiv.map_source, hp₂, hp₁, mem_preimage.1 hp₄, -mem_range, mem_range_self],
exact mem_range_self _
end
/-- If a set in a manifold has the unique derivative property, then its pullback by any extended
chart, in the vector space, also has the unique derivative property. -/
lemma unique_mdiff_on.unique_diff_on (hs : unique_mdiff_on I s) (x : M) :
unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) :=
begin
-- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e`
-- the local chart at `x`.
assume z hz,
simp [ext_chart_at, local_equiv.trans_target, -mem_range] at hz,
have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _,
have T := (hs.unique_mdiff_on_preimage this) (I.symm z),
simp only [ext_chart_at, hz.left.left, hz.left.right, hz.right, local_equiv.trans_target,
unique_mdiff_within_at, local_equiv.refl_trans, forall_prop_of_true, model_with_corners.target,
mem_inter_eq, preimage_inter, mem_preimage, chart_at_model_space_eq,
local_homeomorph.refl_local_equiv, and_self, model_with_corners.right_inv,
local_equiv.coe_trans_symm, model_with_corners.to_local_equiv_coe_symm,
local_homeomorph.coe_coe_symm, model_with_corners.to_local_equiv_coe] at ⊢ T,
convert T using 1,
rw @preimage_comp _ _ _ _ (chart_at H x).symm,
-- it remains to show that `(a ∩ b) ∩ c` = `(b ∩ c) ∩ a`, which finish can do but very slowly
ext p,
split;
{ assume hp, simp at hp, simp [hp] }
end
/-- When considering functions between manifolds, this statement shows up often. It entails
the unique differential of the pullback in extended charts of the set where the function can
be read in the charts. -/
lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M')
{f : M → M'} (hf : continuous_on f s) :
unique_diff_on 𝕜 ((ext_chart_at I x).target
∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) :=
begin
have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source),
{ assume z hz,
apply (hs z hz.1).inter',
apply (hf z hz.1).preimage_mem_nhds_within,
exact mem_nhds_sets (ext_chart_at_open_source I' y) hz.2 },
exact this.unique_diff_on _
end
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
(Z : basic_smooth_bundle_core I M F)
/-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with
unique differential in the basis also has unique differential. -/
lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) :
unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F))
(Z.to_topological_fiber_bundle_core.proj ⁻¹' s) :=
begin
/- Using a chart (and the fact that unique differentiability is invariant under charts), we
reduce the situation to the model space, where we can use the fact that products respect
unique differentiability. -/
assume p hp,
replace hp : p.fst ∈ s, by simpa using hp,
let e₀ := chart_at H p.1,
let e := chart_at (H × F) p,
-- It suffices to prove unique differentiability in a chart
suffices h : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F))
(e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)),
{ have A : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F)) (e.symm.target ∩
e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s))),
{ apply h.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm,
apply_instance },
have : p ∈ e.symm.target ∩
e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)),
by simp [e, hp],
apply (A _ this).mono,
assume q hq,
simp [e, local_homeomorph.left_inv _ hq.1] at hq,
simp [hq] },
-- rewrite the relevant set in the chart as a direct product
have : (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target ∩
(λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.symm ⁻¹' (prod.fst ⁻¹' s)) ∩
range (λ (p : H × F), (I p.1, p.snd))
= set.prod (I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) univ,
{ ext q,
split;
{ assume hq,
simp [-mem_range, mem_range_self, prod_range_univ_eq.symm] at hq,
simp [-mem_range, mem_range_self, hq, prod_range_univ_eq.symm] } },
assume q hq,
replace hq : q.1 ∈ (chart_at H p.1).target ∧ ((chart_at H p.1).symm : H → M) q.1 ∈ s,
by simpa using hq,
simp only [unique_mdiff_within_at, ext_chart_at, model_with_corners.prod, local_equiv.refl_trans,
topological_fiber_bundle_core.proj, id.def, preimage_inter, chart_at_model_space_eq,
local_homeomorph.refl_local_equiv, this, model_with_corners.mk_coe, local_equiv.coe_mk,
model_with_corners_self_coe, local_equiv.coe_symm_mk, model_with_corners_self_coe_symm],
-- apply unique differentiability of products to conclude
apply unique_diff_on.prod _ unique_diff_on_univ,
{ simp [-mem_range, mem_range_self, hq] },
{ assume x hx,
have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s),
{ apply hs.unique_mdiff_on_preimage,
exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)),
apply_instance },
simp [unique_mdiff_on, unique_mdiff_within_at, ext_chart_at] at A,
have B := A (I.symm x) hx.1.1 hx.1.2,
rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B }
end
lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s):
unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) :=
hs.smooth_bundle_preimage _
end unique_mdiff
|
bc0e260159fdba82776148cebe595cc2cccb6f7b | acc85b4be2c618b11fc7cb3005521ae6858a8d07 | /algebra/group.lean | c020c11954ff18d1a2e8fccfae3e76bff1fcfe45 | [
"Apache-2.0"
] | permissive | linpingchuan/mathlib | d49990b236574df2a45d9919ba43c923f693d341 | 5ad8020f67eb13896a41cc7691d072c9331b1f76 | refs/heads/master | 1,626,019,377,808 | 1,508,048,784,000 | 1,508,048,784,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,026 | 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
Various multiplicative and additive structures.
-/
universe variable u
variable {α : Type u}
@[simp] theorem mul_left_inj [left_cancel_semigroup α] {a b c : α} : a * b = a * c ↔ b = c :=
⟨mul_left_cancel, congr_arg _⟩
@[simp] theorem mul_right_inj [right_cancel_semigroup α] {a b c : α} : b * a = c * a ↔ b = c :=
⟨mul_right_cancel, congr_arg _⟩
section group
variables [group α] {a b c : α}
@[simp] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b :=
⟨λ h, by rw ← inv_inv a; simp [h], congr_arg _⟩
theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b :=
inv_inj'.1
@[simp] 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] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 :=
by rw [← @inv_inj' _ _ a 1, one_inv]
@[simp] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 :=
not_congr inv_eq_one
theorem left_inverse_inv (α) [group α] :
function.left_inverse (λ a : α, a⁻¹) (λ a, a⁻¹) :=
assume a, inv_inv a
attribute [simp] mul_inv_cancel_left mul_inv_cancel_right
theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ :=
⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩
theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a :=
by rw [eq_comm, @eq_comm _ _ a, eq_inv_iff_eq_inv]
theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ :=
have a * b = b⁻¹ * b ↔ a = b⁻¹, from mul_right_inj,
by rwa mul_left_inv at this
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]
@[simp] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b :=
⟨λ h, by simp [h], λ h, by simp [h.symm]⟩
@[simp] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c :=
⟨λ h, by simp [h], λ h, by simp [h.symm]⟩
@[simp] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c :=
⟨λ h, by simp [h.symm], λ h, by simp [h]⟩
@[simp] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b :=
⟨λ h, by simp [h.symm], λ h, by simp [h]⟩
theorem mul_inv_eq_one {a b : α} : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inv]
end group
/- transport versions to additive -/
run_cmd transport_multiplicative_to_additive [
/- map operations -/
(`has_mul.mul, `has_add.add), (`has_one.one, `has_zero.zero), (`has_inv.inv, `has_neg.neg),
(`has_mul, `has_add), (`has_inv, `has_neg),
/- map structures -/
(`group, `add_group),
(`left_cancel_semigroup, `add_left_cancel_semigroup),
(`right_cancel_semigroup, `add_right_cancel_semigroup),
/- map instances -/
(`semigroup.to_has_mul, `add_semigroup.to_has_add),
(`monoid.to_has_one, `add_monoid.to_has_zero),
(`group.to_has_inv, `add_group.to_has_neg),
(`monoid.to_semigroup, `add_monoid.to_add_semigroup),
(`group.to_monoid, `add_group.to_add_monoid),
(`left_cancel_semigroup.to_semigroup, `add_left_cancel_semigroup.to_add_semigroup),
(`right_cancel_semigroup.to_semigroup, `add_right_cancel_semigroup.to_add_semigroup),
/- map lemmas -/
(`mul_left_inv, `add_left_neg),
(`mul_left_cancel, `add_left_cancel),
(`mul_right_cancel, `add_right_cancel),
(`inv_mul_cancel_left, `neg_add_cancel_left),
(`inv_mul_cancel_right, `neg_add_cancel_right),
(`inv_inv, `neg_neg),
(`mul_inv_cancel_left, `add_neg_cancel_left),
(`mul_inv_cancel_right, `add_neg_cancel_right),
(`group.to_right_cancel_semigroup, `add_group.to_right_cancel_add_semigroup),
(`eq_inv_of_eq_inv, `eq_neg_of_eq_neg),
(`one_inv, `neg_zero),
/- new lemmas -/
(`mul_left_inj, `add_left_inj),
(`mul_right_inj, `add_right_inj),
(`inv_inj', `neg_inj'),
(`inv_eq_one, `neg_eq_zero),
(`inv_ne_one, `neg_ne_zero),
(`left_inverse_inv, `left_inverse_neg),
(`eq_inv_iff_eq_inv, `eq_neg_iff_eq_neg),
(`inv_eq_iff_inv_eq, `neg_eq_iff_neg_eq),
(`mul_eq_one_iff_eq_inv, `add_eq_zero_iff_eq_neg),
(`mul_eq_one_iff_inv_eq, `add_eq_zero_iff_neg_eq),
(`eq_mul_inv_iff_mul_eq, `eq_add_neg_iff_add_eq),
(`eq_inv_mul_iff_mul_eq, `eq_neg_add_iff_add_eq),
(`inv_mul_eq_iff_eq_mul, `neg_add_eq_iff_eq_add),
(`mul_inv_eq_iff_eq_mul, `add_neg_eq_iff_eq_add),
(`mul_inv_eq_one, `add_neg_eq_zero)]
section add_group
variables [add_group α] {a b c : α}
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 - b) + (b - c) = a - c :=
by simp
lemma sub_sub_sub_cancel_right (a b c : α) : (a - c) - (b - c) = a - b :=
by simp
theorem sub_eq_zero : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, λ h, by simp [h]⟩
theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b :=
not_congr sub_eq_zero
@[simp] theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b :=
by split; intro h; simp [h]
theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b :=
by split; intro h; simp * at *
theorem eq_iff_eq_of_sub_eq_sub {a b c d : α} (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 : α) : function.left_inverse (λ x, x - c) (λ x, x + c) :=
assume x, add_sub_cancel x c
theorem left_inverse_add_left_sub (c : α) : function.left_inverse (λ x, x + c) (λ x, x - c) :=
assume x, sub_add_cancel x c
theorem left_inverse_add_right_neg_add (c : α) :
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 : α) :
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 b c : α}
lemma sub_eq_neg_add (a b : α) : a - b = -b + a :=
by simp
theorem neg_add' (a b : α) : -(a + b) = -a - b := neg_add a b
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]
lemma add_sub_cancel' (a b : α) : a + b - a = b :=
by simp
lemma add_sub_cancel'_right (a b : α) : a + (b - a) = b :=
by rw [← add_sub_assoc, add_sub_cancel']
lemma sub_sub_swap (a b c : α) : a - b - c = a - c - b :=
by simp
lemma sub_sub_sub_cancel_left (a b c : α) : (c - a) - (c - b) = b - a :=
by simp
end add_comm_group
section ordered_comm_group
variables [ordered_comm_group α]
theorem le_sub_iff_add_le {a b c : α} : a ≤ b - c ↔ a + c ≤ b :=
by rw [add_comm]; exact ⟨add_le_of_le_sub_left, le_sub_left_of_add_le⟩
theorem sub_le_iff_le_add {a b c : α} : a - c ≤ b ↔ a ≤ b + c :=
by rw [add_comm]; exact ⟨le_add_of_sub_left_le, sub_left_le_of_le_add⟩
end ordered_comm_group
|
ee6b41d3bb62daf6e76a889af86c3ec4de8b2888 | d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6 | /Papyrus/IR/TypeBases.lean | cb46323cb972cd070a108810996a8d603bb0ec81 | [
"Apache-2.0"
] | permissive | xubaiw/lean4-papyrus | c3fbbf8ba162eb5f210155ae4e20feb2d32c8182 | 02e82973a5badda26fc0f9fd15b3d37e2eb309e0 | refs/heads/master | 1,691,425,756,824 | 1,632,122,825,000 | 1,632,123,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,353 | lean | import Papyrus.IR.TypeID
import Papyrus.IR.AddressSpace
namespace Papyrus
--------------------------------------------------------------------------------
-- # IntegerType
--------------------------------------------------------------------------------
/--
A pure representation of an LLVM
[IntegerType](https://llvm.org/doxygen/classllvm_1_1IntegerType.html).
-/
structure IntegerType where
/-- The width in bits of integers of this type. -/
bitWidth : UInt32
deriving BEq, Repr
/-- An integer type of the given precision. -/
def integerType (numBits : UInt32) : IntegerType :=
IntegerType.mk numBits
namespace IntegerType
/-- The type ID of an integer type (i.e., `integer`). -/
def typeID (self : IntegerType) := TypeID.integer
/-- An integer type twice as wide as this type. -/
def extendedType (self : IntegerType) :=
integerType (self.bitWidth <<< 1)
/--
A 64-bit mask with ones set for all the bits of this type
(or just every bit, if this type's bit width is greater than 64).
-/
def bitMask (self : IntegerType) : UInt64 :=
~~~(0 : UInt64) >>> (64 - self.bitWidth.toUInt64)
/--
A `UInt64` with just the most significant bit of this type set
(the sign bit, if the value is treated as a signed number).
-/
def signBit (self : IntegerType) : UInt64 :=
(1 : UInt64) <<< (self.bitWidth.toUInt64 - 1)
/--
A bit mask with ones set for all the bits of this type.
For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
-/
def mask (self : IntegerType) : Nat :=
(1 <<< self.bitWidth.toNat) - 1
end IntegerType
-- ## Specializations
/-- A 1-bit integer type (e.g., a `bool`). -/
abbrev int1Type := integerType 1
/-- An 8-bit integer type (e.g., a `byte` or `char`). -/
abbrev int8Type := integerType 8
/-- A 16-bit integer type (e.g., a `short`). -/
abbrev int16Type := integerType 16
/-- A 32-bit integer type (e.g., a `long`). -/
abbrev int32Type := integerType 32
/-- A 64-bit integer type (e.g., a `long long`). -/
abbrev int64Type := integerType 64
/-- A 128-bit integer type. -/
abbrev int128Type := integerType 128
--------------------------------------------------------------------------------
-- # BaseFunctionType
--------------------------------------------------------------------------------
/-- Base type of `FunctionType`. Used to avoid mutual recursion with `Type`. -/
structure BaseFunctionType (t) where
/-- The type of return values of functions of this type. -/
returnType : t
/-- The types of parameters of functions of this type. -/
parameterTypes : Array t
/-- Do functions of this type accept a variable number of arguments (e.g., `printf`). -/
isVarArg := false
deriving BEq, Repr
/-- The type ID of a function type (i.e., `function`). -/
def BaseFunctionType.typeID (self : BaseFunctionType t) := TypeID.function
--------------------------------------------------------------------------------
-- # BasePointerType
--------------------------------------------------------------------------------
/-- Base type of `PointerType`. Used to avoid mutual recursion with `Type`. -/
structure BasePointerType.{u} (t : Type u) where
/-- The type pointed to by pointers of this type. -/
pointeeType : t
/-- The address space pointers of this type belongs to. -/
addressSpace := AddressSpace.default
deriving BEq, Repr
/-- The type ID of a pointer type (i.e., `pointer`). -/
def BasePointerType.typeID (self : BasePointerType t) := TypeID.pointer
--------------------------------------------------------------------------------
-- # BaseStructType
--------------------------------------------------------------------------------
/-- Base type of `StructTypeBody`. Used to avoid mutual recursion with `Type`. -/
structure BaseStructTypeBody (t) where
/-- The types of the elements of structs of this type. -/
elementTypes : Array t
/--
If true, structs of this type are packed
(i.e., no padding of elements is allowed).
-/
isPacked : Bool
deriving BEq, Repr
/-- Base type of `StructType`. Used to avoid mutual recursion with `Type`. -/
inductive BaseStructType (t)
| literal (body : BaseStructTypeBody t)
| identified (name : String) (body? : Option (BaseStructTypeBody t))
deriving BEq, Repr
namespace BaseStructType
@[matchPattern]
def opaque (name : String) : BaseStructType t :=
identified name none
@[matchPattern]
def complete (name : String) (body : BaseStructTypeBody t): BaseStructType t :=
identified name (some body)
/-- The type ID of a struct type (i.e., `struct`). -/
def typeID (self : BaseStructType t) := TypeID.struct
/-- Is the struct type is literal? -/
def isLiteral : (self : BaseStructType t) → Bool
| literal _ => true
| _ => false
/-- Does this struct type have a non-empty name. -/
def hasName : (self : BaseStructType t) → Bool
| literal _ => false
| identified name _ => name.bsize != 0
/-- The name of this string (or the empty string if literal). -/
def name : (self : BaseStructType t) → String
| literal _ => ""
| identified name _ => name
/-- The name of this string (or none if literal). -/
def name? : (self : BaseStructType t) → Option String
| literal _ => none
| identified name _ => some name
/-- Change/set the name of the struct type. -/
def withName (name : String)
: (self : BaseStructType t) → BaseStructType t
| literal body => identified name body
| identified _ body? => identified name body?
/-- Is the struct type is opaque? -/
def isOpaque : (self : BaseStructType t) → Bool
| opaque _ => true
| _ => false
/-- Does this struct type have a body (i.e., is it not opaque)? -/
def hasBody : (self : BaseStructType t) → Bool
| opaque _ => false
| _ => true
/-- The body of the struct (or none if opaque). -/
def body? : (self : BaseStructType t) → Option (BaseStructTypeBody t)
| literal body => some body
| identified _ body? => body?
/-- Change/set the body of the struct type. -/
def withBody (body : BaseStructTypeBody t)
: (self : BaseStructType t) → BaseStructType t
| literal _ => literal body
| identified name _ => identified name (some body)
/-- The element types of the struct (or the empty array if opaque). -/
def elementTypes : (self : BaseStructType t) → Array t
| literal body => body.elementTypes
| complete _ body => body.elementTypes
| opaque _ => #[]
/-- The element types of the struct (if they are defined). -/
def elementTypes? : (self : BaseStructType t) → Option (Array t)
| literal body => some body.elementTypes
| identified _ body? => body?.map (·.elementTypes)
/-- Is the struct type is non-opaque and packed? -/
def isPacked : (self : BaseStructType t) → Bool
| literal ⟨_, packed⟩ => packed
| complete _ ⟨_, packed⟩ => packed
| opaque _ => false
end BaseStructType
--------------------------------------------------------------------------------
-- # BaseArrayType
--------------------------------------------------------------------------------
/-- Base type of `ArrayType`. Used to avoid mutual recursion with `Type`. -/
structure BaseArrayType.{u} (t : Type u) where
/-- The type of elements of arrays of this type. -/
elementType : t
/-- The size of arrays of this type. -/
size : UInt64
deriving BEq, Repr
/-- The type ID of an array type (i.e., `array`). -/
def BaseArrayType.typeID (self : BaseArrayType t) := TypeID.array
--------------------------------------------------------------------------------
-- # Base Vector Types
--------------------------------------------------------------------------------
-- ## Abstract Base
/-- The abstract parent of both fixed-length and scalable SIMD vector types. -/
structure BaseAbstractVectorType.{u} (t : Type u) where
/-- The type of elements of vectors of this type. -/
elementType : t
/-- The minimum size of vectors of this type. -/
minSize : UInt32
deriving BEq, Repr
-- ## BaseVectorType
/-- Base type of `VectorType`. Used to avoid mutual recursion with `Type`. -/
structure BaseVectorType (t) extends BaseAbstractVectorType t where
isScalable : Bool
deriving BEq, Repr
/-- The type ID of this SIMD vector type (i.e., `fixedVector` or `scalableVector`). -/
def BaseVectorType.typeID (self : BaseVectorType t) :=
if self.isScalable then TypeID.scalableVector else TypeID.fixedVector
-- ## BaseFixedVectorType
/-- Base type of `FixedVectorType`. Used to avoid mutual recursion with `Type`. -/
structure BaseFixedVectorType (t) extends BaseAbstractVectorType t
deriving BEq, Repr
instance : Coe (BaseFixedVectorType t) (BaseVectorType t) where
coe v := ⟨v.toBaseAbstractVectorType, false⟩
/-- The type ID of a fixed-length SIMD vector type (i.e., `fixedVector`). -/
def BaseFixedVectorType.typeID (self : BaseFixedVectorType t) := TypeID.fixedVector
/-- The size of this vector (i.e., exactly its `minSize`). -/
def BaseFixedVectorType.size (self : BaseFixedVectorType t) := self.minSize
-- ## BaseScalableVectorType
/-- Base type of `ScalableVectorType`. Used to avoid mutual recursion with `Type`. -/
structure BaseScalableVectorType (t) extends BaseAbstractVectorType t
deriving BEq, Repr
instance : Coe (BaseScalableVectorType t) (BaseVectorType t) where
coe v := ⟨v.toBaseAbstractVectorType, true⟩
/-- The type ID of an scalable SIMD vector type (i.e., `scalableVector`). -/
def BaseScalableVectorType.typeID (self : BaseScalableVectorType t) :=
TypeID.scalableVector
|
3fd7226443de3706fd24bbcc824598c6011f9f18 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/uni_bug1.lean | 7e574804342cefb3c39c064b4612d7dd0c13bb2a | [
"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 | 175 | lean | --
open nat prod
constant R : nat → nat → Prop
constant f (a b : nat) (H : R a b) : nat
axiom Rtrue (a b : nat) : R a b
check f 1 0 (Rtrue (fst (prod.mk 1 (0:nat))) 0)
|
8a39ab427c9ca8312ffe92233507704bae27e772 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/homology/additive.lean | 6fd20d33f676d612e716cf94e6cd4c73376b6599 | [
"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 | 8,810 | 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 algebra.homology.homology
import algebra.homology.single
import category_theory.preadditive.additive_functor
/-!
# Homology is an additive functor
When `V` is preadditive, `homological_complex V c` is also preadditive,
and `homology_functor` is additive.
TODO: similarly for `R`-linear.
-/
universes v u
open_locale classical
noncomputable theory
open category_theory category_theory.limits homological_complex
variables {ι : Type*}
variables {V : Type u} [category.{v} V] [preadditive V]
variables {c : complex_shape ι} {C D E : homological_complex V c}
variables (f g : C ⟶ D) (h k : D ⟶ E) (i : ι)
namespace homological_complex
instance : has_zero (C ⟶ D) := ⟨{ f := λ i, 0 }⟩
instance : has_add (C ⟶ D) := ⟨λ f g, { f := λ i, f.f i + g.f i, }⟩
instance : has_neg (C ⟶ D) := ⟨λ f, { f := λ i, -(f.f i), }⟩
instance : has_sub (C ⟶ D) := ⟨λ f g, { f := λ i, f.f i - g.f i, }⟩
@[simp] lemma zero_f_apply (i : ι) : (0 : C ⟶ D).f i = 0 := rfl
@[simp] lemma add_f_apply (f g : C ⟶ D) (i : ι) : (f + g).f i = f.f i + g.f i := rfl
@[simp] lemma neg_f_apply (f : C ⟶ D) (i : ι) : (-f).f i = -(f.f i) := rfl
@[simp] lemma sub_f_apply (f g : C ⟶ D) (i : ι) : (f - g).f i = f.f i - g.f i := rfl
/- TODO(jmc/Scott): the instance below doesn't have the correct defeq for `nsmul` and `zsmul`.
We should generalize `function.injective.add_comm_group` and friends.
For the `R`-linear version, it will be very convenient to have
a good definition of `nsmul` and `zsmul` that matches `smul`. -/
instance : add_comm_group (C ⟶ D) :=
function.injective.add_comm_group hom.f
homological_complex.hom_f_injective (by tidy) (by tidy) (by tidy) (by tidy)
instance : preadditive (homological_complex V c) := {}
/-- The `i`-th component of a chain map, as an additive map from chain maps to morphisms. -/
@[simps]
def hom.f_add_monoid_hom {C₁ C₂ : homological_complex V c} (i : ι) :
(C₁ ⟶ C₂) →+ (C₁.X i ⟶ C₂.X i) :=
add_monoid_hom.mk' (λ f, hom.f f i) (λ _ _, rfl)
end homological_complex
namespace homological_complex
instance eval_additive (i : ι) : (eval V c i).additive := {}
variables [has_zero_object V]
instance cycles_additive [has_equalizers V] : (cycles_functor V c i).additive := {}
variables [has_images V] [has_image_maps V]
instance boundaries_additive : (boundaries_functor V c i).additive := {}
variables [has_equalizers V] [has_cokernels V]
instance homology_additive : (homology_functor V c i).additive :=
{ map_zero' := λ C D, begin
dsimp [homology_functor],
ext,
simp only [limits.cokernel.π_desc, limits.comp_zero, homology.π_map],
convert zero_comp,
ext,
simp,
end,
map_add' := λ C D f g, begin
dsimp [homology_functor],
ext,
simp only [homology.π_map, preadditive.comp_add, ←preadditive.add_comp],
congr,
ext, simp,
end }
end homological_complex
namespace category_theory
variables {W : Type*} [category W] [preadditive W]
/--
An additive functor induces a functor between homological complexes.
This is sometimes called the "prolongation".
-/
@[simps]
def functor.map_homological_complex (F : V ⥤ W) [F.additive] (c : complex_shape ι) :
homological_complex V c ⥤ homological_complex W c :=
{ obj := λ C,
{ X := λ i, F.obj (C.X i),
d := λ i j, F.map (C.d i j),
shape' := λ i j w, by rw [C.shape _ _ w, F.map_zero],
d_comp_d' := λ i j k _ _, by rw [←F.map_comp, C.d_comp_d, F.map_zero], },
map := λ C D f,
{ f := λ i, F.map (f.f i),
comm' := λ i j h, by { dsimp, rw [←F.map_comp, ←F.map_comp, f.comm], }, }, }.
instance functor.map_homogical_complex_additive
(F : V ⥤ W) [F.additive] (c : complex_shape ι) : (F.map_homological_complex c).additive := {}
/--
A natural transformation between functors induces a natural transformation
between those functors applied to homological complexes.
-/
@[simps]
def nat_trans.map_homological_complex {F G : V ⥤ W} [F.additive] [G.additive]
(α : F ⟶ G) (c : complex_shape ι) : F.map_homological_complex c ⟶ G.map_homological_complex c :=
{ app := λ C, { f := λ i, α.app _, }, }
@[simp] lemma nat_trans.map_homological_complex_id (c : complex_shape ι) (F : V ⥤ W) [F.additive] :
nat_trans.map_homological_complex (𝟙 F) c = 𝟙 (F.map_homological_complex c) :=
by tidy
@[simp] lemma nat_trans.map_homological_complex_comp (c : complex_shape ι)
{F G H : V ⥤ W} [F.additive] [G.additive] [H.additive]
(α : F ⟶ G) (β : G ⟶ H):
nat_trans.map_homological_complex (α ≫ β) c =
nat_trans.map_homological_complex α c ≫ nat_trans.map_homological_complex β c :=
by tidy
@[simp, reassoc] lemma nat_trans.map_homological_complex_naturality {c : complex_shape ι}
{F G : V ⥤ W} [F.additive] [G.additive] (α : F ⟶ G) {C D : homological_complex V c} (f : C ⟶ D) :
(F.map_homological_complex c).map f ≫ (nat_trans.map_homological_complex α c).app D =
(nat_trans.map_homological_complex α c).app C ≫ (G.map_homological_complex c).map f :=
by tidy
end category_theory
variables [has_zero_object V] {W : Type*} [category W] [preadditive W] [has_zero_object W]
namespace homological_complex
/--
Turning an object into a complex supported at `j` then applying a functor is
the same as applying the functor then forming the complex.
-/
def single_map_homological_complex (F : V ⥤ W) [F.additive] (c : complex_shape ι) (j : ι):
single V c j ⋙ F.map_homological_complex _ ≅ F ⋙ single W c j :=
nat_iso.of_components (λ X,
{ hom := { f := λ i, if h : i = j then
eq_to_hom (by simp [h])
else
0, },
inv := { f := λ i, if h : i = j then
eq_to_hom (by simp [h])
else
0, },
hom_inv_id' := begin
ext i,
dsimp,
split_ifs with h,
{ simp [h] },
{ rw [zero_comp, if_neg h],
exact (zero_of_source_iso_zero _ F.map_zero_object).symm, },
end,
inv_hom_id' := begin
ext i,
dsimp,
split_ifs with h,
{ simp [h] },
{ rw [zero_comp, if_neg h],
simp, },
end, })
(λ X Y f, begin
ext i,
dsimp,
split_ifs with h; simp [h],
end).
variables (F : V ⥤ W) [functor.additive F] (c)
@[simp] lemma single_map_homological_complex_hom_app_self (j : ι) (X : V) :
((single_map_homological_complex F c j).hom.app X).f j = eq_to_hom (by simp) :=
by simp [single_map_homological_complex]
@[simp] lemma single_map_homological_complex_hom_app_ne
{i j : ι} (h : i ≠ j) (X : V) :
((single_map_homological_complex F c j).hom.app X).f i = 0 :=
by simp [single_map_homological_complex, h]
@[simp] lemma single_map_homological_complex_inv_app_self (j : ι) (X : V) :
((single_map_homological_complex F c j).inv.app X).f j = eq_to_hom (by simp) :=
by simp [single_map_homological_complex]
@[simp] lemma single_map_homological_complex_inv_app_ne
{i j : ι} (h : i ≠ j) (X : V):
((single_map_homological_complex F c j).inv.app X).f i = 0 :=
by simp [single_map_homological_complex, h]
end homological_complex
namespace chain_complex
-- TODO: dualize to cochain complexes
/--
Turning an object into a chain complex supported at zero then applying a functor is
the same as applying the functor then forming the complex.
-/
def single₀_map_homological_complex (F : V ⥤ W) [F.additive] :
single₀ V ⋙ F.map_homological_complex _ ≅ F ⋙ single₀ W :=
nat_iso.of_components (λ X,
{ hom := { f := λ i, match i with
| 0 := 𝟙 _
| (i+1) := F.map_zero_object.hom
end, },
inv := { f := λ i, match i with
| 0 := 𝟙 _
| (i+1) := F.map_zero_object.inv
end, },
hom_inv_id' := begin
ext (_|i),
{ unfold_aux, simp, },
{ unfold_aux,
dsimp,
simp only [comp_f, id_f, zero_comp],
exact (zero_of_source_iso_zero _ F.map_zero_object).symm, }
end,
inv_hom_id' := by { ext (_|i); { unfold_aux, dsimp, simp, }, }, })
(λ X Y f, by { ext (_|i); { unfold_aux, dsimp, simp, }, }).
@[simp] lemma single₀_map_homological_complex_hom_app_zero (F : V ⥤ W) [F.additive] (X : V) :
((single₀_map_homological_complex F).hom.app X).f 0 = 𝟙 _ := rfl
@[simp] lemma single₀_map_homological_complex_hom_app_succ
(F : V ⥤ W) [F.additive] (X : V) (n : ℕ) :
((single₀_map_homological_complex F).hom.app X).f (n+1) = 0 := rfl
@[simp] lemma single₀_map_homological_complex_inv_app_zero (F : V ⥤ W) [F.additive] (X : V) :
((single₀_map_homological_complex F).inv.app X).f 0 = 𝟙 _ := rfl
@[simp] lemma single₀_map_homological_complex_inv_app_succ
(F : V ⥤ W) [F.additive] (X : V) (n : ℕ) :
((single₀_map_homological_complex F).inv.app X).f (n+1) = 0 := rfl
end chain_complex
|
494cb85acd9472bb27d7abef2c73a6cff7da6eb9 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/topology/algebra/mul_action.lean | 5c2a751c2f099ec750f6fe437ff90bf431df96dd | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 10,016 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import topology.algebra.monoid
import algebra.module.prod
import topology.homeomorph
/-!
# Continuous monoid action
In this file we define class `has_continuous_smul`. We say `has_continuous_smul M α` if `M` acts on
`α` and the map `(c, x) ↦ c • x` is continuous on `M × α`. We reuse this class for topological
(semi)modules, vector spaces and algebras.
## Main definitions
* `has_continuous_smul M α` : typeclass saying that the map `(c, x) ↦ c • x` is continuous
on `M × α`;
* `homeomorph.smul_of_ne_zero`: if a group with zero `G₀` (e.g., a field) acts on `α` and `c : G₀`
is a nonzero element of `G₀`, then scalar multiplication by `c` is a homeomorphism of `α`;
* `homeomorph.smul`: scalar multiplication by an element of a group `G` acting on `α`
is a homeomorphism of `α`.
* `units.has_continuous_smul`: scalar multiplication by `units M` is continuous when scalar
multiplication by `M` is continuous. This allows `homeomorph.smul` to be used with on monoids
with `G = units M`.
## Main results
Besides homeomorphisms mentioned above, in this file we provide lemmas like `continuous.smul`
or `filter.tendsto.smul` that provide dot-syntax access to `continuous_smul`.
-/
open_locale topological_space
open filter
/-- Class `has_continuous_smul M α` says that the scalar multiplication `(•) : M → α → α`
is continuous in both arguments. We use the same class for all kinds of multiplicative actions,
including (semi)modules and algebras. -/
class has_continuous_smul (M α : Type*) [has_scalar M α]
[topological_space M] [topological_space α] : Prop :=
(continuous_smul : continuous (λp : M × α, p.1 • p.2))
export has_continuous_smul (continuous_smul)
variables {M α β : Type*} [topological_space M] [topological_space α]
section has_scalar
variables [has_scalar M α] [has_continuous_smul M α]
lemma filter.tendsto.smul {f : β → M} {g : β → α} {l : filter β} {c : M} {a : α}
(hf : tendsto f l (𝓝 c)) (hg : tendsto g l (𝓝 a)) :
tendsto (λ x, f x • g x) l (𝓝 $ c • a) :=
(continuous_smul.tendsto _).comp (hf.prod_mk_nhds hg)
lemma filter.tendsto.const_smul {f : β → α} {l : filter β} {a : α} (hf : tendsto f l (𝓝 a))
(c : M) :
tendsto (λ x, c • f x) l (𝓝 (c • a)) :=
tendsto_const_nhds.smul hf
lemma filter.tendsto.smul_const {f : β → M} {l : filter β} {c : M}
(hf : tendsto f l (𝓝 c)) (a : α) :
tendsto (λ x, (f x) • a) l (𝓝 (c • a)) :=
hf.smul tendsto_const_nhds
variables [topological_space β] {f : β → M} {g : β → α} {b : β} {s : set β}
lemma continuous_within_at.smul (hf : continuous_within_at f s b)
(hg : continuous_within_at g s b) :
continuous_within_at (λ x, f x • g x) s b :=
hf.smul hg
lemma continuous_within_at.const_smul (hg : continuous_within_at g s b) (c : M) :
continuous_within_at (λ x, c • g x) s b :=
hg.const_smul c
lemma continuous_at.smul (hf : continuous_at f b) (hg : continuous_at g b) :
continuous_at (λ x, f x • g x) b :=
hf.smul hg
lemma continuous_at.const_smul (hg : continuous_at g b) (c : M) :
continuous_at (λ x, c • g x) b :=
hg.const_smul c
lemma continuous_on.smul (hf : continuous_on f s) (hg : continuous_on g s) :
continuous_on (λ x, f x • g x) s :=
λ x hx, (hf x hx).smul (hg x hx)
lemma continuous_on.const_smul (hg : continuous_on g s) (c : M) :
continuous_on (λ x, c • g x) s :=
λ x hx, (hg x hx).const_smul c
@[continuity]
lemma continuous.smul (hf : continuous f) (hg : continuous g) :
continuous (λ x, f x • g x) :=
continuous_smul.comp (hf.prod_mk hg)
lemma continuous.const_smul (hg : continuous g) (c : M) :
continuous (λ x, c • g x) :=
continuous_smul.comp (continuous_const.prod_mk hg)
end has_scalar
section monoid
variables [monoid M] [mul_action M α] [has_continuous_smul M α]
instance units.has_continuous_smul : has_continuous_smul (units M) α :=
{ continuous_smul :=
show continuous ((λ p : M × α, p.fst • p.snd) ∘ (λ p : units M × α, (p.1, p.2))),
from continuous_smul.comp ((units.continuous_coe.comp continuous_fst).prod_mk continuous_snd) }
end monoid
section group
variables {G : Type*} [topological_space G] [group G] [mul_action G α]
[has_continuous_smul G α]
lemma tendsto_const_smul_iff {f : β → α} {l : filter β} {a : α} (c : G) :
tendsto (λ x, c • f x) l (𝓝 $ c • a) ↔ tendsto f l (𝓝 a) :=
⟨λ h, by simpa only [inv_smul_smul] using h.const_smul c⁻¹,
λ h, h.const_smul _⟩
variables [topological_space β] {f : β → α} {b : β} {s : set β}
lemma continuous_within_at_const_smul_iff (c : G) :
continuous_within_at (λ x, c • f x) s b ↔ continuous_within_at f s b :=
tendsto_const_smul_iff c
lemma continuous_on_const_smul_iff (c : G) :
continuous_on (λ x, c • f x) s ↔ continuous_on f s :=
forall_congr $ λ b, forall_congr $ λ hb, continuous_within_at_const_smul_iff c
lemma continuous_at_const_smul_iff (c : G) :
continuous_at (λ x, c • f x) b ↔ continuous_at f b :=
tendsto_const_smul_iff c
lemma continuous_const_smul_iff (c : G) :
continuous (λ x, c • f x) ↔ continuous f :=
by simp only [continuous_iff_continuous_at, continuous_at_const_smul_iff]
/-- Scalar multiplication by a unit of a monoid `M` acting on `α` is a homeomorphism from `α`
to itself. -/
protected def homeomorph.smul (c : G) : α ≃ₜ α :=
{ to_equiv := mul_action.to_perm_hom G α c,
continuous_to_fun := continuous_id.const_smul _,
continuous_inv_fun := continuous_id.const_smul _ }
lemma is_open_map_smul (c : G) : is_open_map (λ x : α, c • x) :=
(homeomorph.smul c).is_open_map
lemma is_closed_map_smul (c : G) : is_closed_map (λ x : α, c • x) :=
(homeomorph.smul c).is_closed_map
end group
section group_with_zero
variables {G₀ : Type*} [topological_space G₀] [group_with_zero G₀] [mul_action G₀ α]
[has_continuous_smul G₀ α]
lemma tendsto_const_smul_iff' {f : β → α} {l : filter β} {a : α} {c : G₀} (hc : c ≠ 0) :
tendsto (λ x, c • f x) l (𝓝 $ c • a) ↔ tendsto f l (𝓝 a) :=
tendsto_const_smul_iff (units.mk0 c hc)
variables [topological_space β] {f : β → α} {b : β} {c : G₀} {s : set β}
lemma continuous_within_at_const_smul_iff' (hc : c ≠ 0) :
continuous_within_at (λ x, c • f x) s b ↔ continuous_within_at f s b :=
tendsto_const_smul_iff (units.mk0 c hc)
lemma continuous_on_const_smul_iff' (hc : c ≠ 0) :
continuous_on (λ x, c • f x) s ↔ continuous_on f s :=
continuous_on_const_smul_iff (units.mk0 c hc)
lemma continuous_at_const_smul_iff' (hc : c ≠ 0) :
continuous_at (λ x, c • f x) b ↔ continuous_at f b :=
continuous_at_const_smul_iff (units.mk0 c hc)
lemma continuous_const_smul_iff' (hc : c ≠ 0) :
continuous (λ x, c • f x) ↔ continuous f :=
continuous_const_smul_iff (units.mk0 c hc)
/-- Scalar multiplication by a non-zero element of a group with zero acting on `α` is a
homeomorphism from `α` onto itself. -/
protected def homeomorph.smul_of_ne_zero (c : G₀) (hc : c ≠ 0) : α ≃ₜ α :=
homeomorph.smul (units.mk0 c hc)
lemma is_open_map_smul' {c : G₀} (hc : c ≠ 0) : is_open_map (λ x : α, c • x) :=
(homeomorph.smul_of_ne_zero c hc).is_open_map
/-- `smul` is a closed map in the second argument.
The lemma that `smul` is a closed map in the first argument (for a normed space over a complete
normed field) is `is_closed_map_smul_left` in `analysis.normed_space.finite_dimension`. -/
lemma is_closed_map_smul' {c : G₀} (hc : c ≠ 0) : is_closed_map (λ x : α, c • x) :=
(homeomorph.smul_of_ne_zero c hc).is_closed_map
end group_with_zero
namespace is_unit
variables [monoid M] [mul_action M α] [has_continuous_smul M α]
lemma tendsto_const_smul_iff {f : β → α} {l : filter β} {a : α} {c : M} (hc : is_unit c) :
tendsto (λ x, c • f x) l (𝓝 $ c • a) ↔ tendsto f l (𝓝 a) :=
let ⟨u, hu⟩ := hc in hu ▸ tendsto_const_smul_iff u
variables [topological_space β] {f : β → α} {b : β} {c : M} {s : set β}
lemma continuous_within_at_const_smul_iff (hc : is_unit c) :
continuous_within_at (λ x, c • f x) s b ↔ continuous_within_at f s b :=
let ⟨u, hu⟩ := hc in hu ▸ continuous_within_at_const_smul_iff u
lemma continuous_on_const_smul_iff (hc : is_unit c) :
continuous_on (λ x, c • f x) s ↔ continuous_on f s :=
let ⟨u, hu⟩ := hc in hu ▸ continuous_on_const_smul_iff u
lemma continuous_at_const_smul_iff (hc : is_unit c) :
continuous_at (λ x, c • f x) b ↔ continuous_at f b :=
let ⟨u, hu⟩ := hc in hu ▸ continuous_at_const_smul_iff u
lemma continuous_const_smul_iff (hc : is_unit c) :
continuous (λ x, c • f x) ↔ continuous f :=
let ⟨u, hu⟩ := hc in hu ▸ continuous_const_smul_iff u
lemma is_open_map_smul (hc : is_unit c) : is_open_map (λ x : α, c • x) :=
let ⟨u, hu⟩ := hc in hu ▸ is_open_map_smul u
lemma is_closed_map_smul (hc : is_unit c) : is_closed_map (λ x : α, c • x) :=
let ⟨u, hu⟩ := hc in hu ▸ is_closed_map_smul u
end is_unit
instance has_continuous_mul.has_continuous_smul {M : Type*} [monoid M]
[topological_space M] [has_continuous_mul M] :
has_continuous_smul M M :=
⟨continuous_mul⟩
instance [topological_space β] [has_scalar M α] [has_scalar M β] [has_continuous_smul M α]
[has_continuous_smul M β] :
has_continuous_smul M (α × β) :=
⟨(continuous_fst.smul (continuous_fst.comp continuous_snd)).prod_mk
(continuous_fst.smul (continuous_snd.comp continuous_snd))⟩
instance {ι : Type*} {γ : ι → Type}
[∀ i, topological_space (γ i)] [Π i, has_scalar M (γ i)] [∀ i, has_continuous_smul M (γ i)] :
has_continuous_smul M (Π i, γ i) :=
⟨continuous_pi $ λ i,
(continuous_fst.smul continuous_snd).comp $
continuous_fst.prod_mk ((continuous_apply i).comp continuous_snd)⟩
|
8274cc7b5237fdcde0f70cf96ddbfbb732a85bb1 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /library/init/data/list/qsort.lean | d736e759d8f73ca6c3e2c3157565009263124e8c | [
"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,628 | 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
-/
prelude
import init.data.list.lemmas init.wf
namespace list
-- Note: we can't use the equation compiler here because
-- init.meta.well_founded_tactics uses this file
def qsort.F {α} (lt : α → α → bool) : Π (x : list α),
(Π (y : list α), length y < length x → list α) → list α
| [] IH := []
| (h::t) IH := begin
ginduction partition (λ x, lt h x = tt) t with e large small,
have : length small < length (h::t) ∧ length large < length (h::t),
{ rw partition_eq_filter_filter at e,
injection e,
subst large, subst small,
constructor;
exact nat.succ_le_succ (length_le_of_sublist (filter_sublist _)) },
exact IH small this.left ++ h :: IH large this.right
end
/- 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 {α} (lt : α → α → bool) : list α → list α :=
well_founded.fix (inv_image.wf length nat.lt_wf) (qsort.F lt)
@[simp] theorem qsort_nil {α} (lt : α → α → bool) : qsort lt [] = [] :=
by rw [qsort, well_founded.fix_eq, qsort.F]
@[simp] theorem qsort_cons {α} (lt : α → α → bool) (h t) : qsort lt (h::t) =
let (large, small) := partition (λ x, lt h x = tt) t in
qsort lt small ++ h :: qsort lt large :=
begin
rw [qsort, well_founded.fix_eq, qsort.F],
ginduction partition (λ x, lt h x = tt) t with e large small,
simp [e], rw [e]
end
end list
|
1e373c547f8512f9a6ae700713cd9902cadc85cb | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/seq/seq.lean | c7cec9dffc823d1a51ca9ba196f200fb1972c88c | [
"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,534 | 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 tactic.basic
import data.list.basic data.stream data.lazy_list data.seq.computation logic.basic
universes u v w
/-
coinductive seq (α : Type u) : Type u
| nil : seq α
| cons : α → seq α → seq α
-/
/--
A stream `s : option α` is a sequence if `s.nth n = none` implies `s.nth (n + 1) = none`.
-/
def stream.is_seq {α : Type u} (s : stream (option α)) : Prop :=
∀ {n : ℕ}, s n = none → s (n + 1) = none
/-- `seq α` is the type of possibly infinite lists (referred here as sequences).
It is encoded as an infinite stream of options such that if `f n = none`, then
`f m = none` for all `m ≥ n`. -/
def seq (α : Type u) : Type u := { f : stream (option α) // f.is_seq }
/-- `seq1 α` is the type of nonempty sequences. -/
def seq1 (α) := α × seq α
namespace seq
variables {α : Type u} {β : Type v} {γ : Type w}
/-- The empty sequence -/
def nil : seq α := ⟨stream.const none, λn h, rfl⟩
instance : inhabited (seq α) := ⟨nil⟩
/-- Prepend an element to a sequence -/
def cons (a : α) : seq α → seq α
| ⟨f, al⟩ := ⟨some a :: f, λn h, by {cases n with n, contradiction, exact al h}⟩
/-- Get the nth element of a sequence (if it exists) -/
def nth : seq α → ℕ → option α := subtype.val
/-- A sequence has terminated at position `n` if the value at position `n` equals `none`. -/
def terminated_at (s : seq α) (n : ℕ) : Prop := s.nth n = none
/-- It is decidable whether a sequence terminates at a given position. -/
instance terminated_at_decidable (s : seq α) (n : ℕ) : decidable (s.terminated_at n) :=
if p : s.nth n = none then is_true p
else is_false (assume h, by contradiction)
/-- A sequence terminates if there is some position `n` at which it has terminated. -/
def terminates (s : seq α) : Prop := ∃ (n : ℕ), s.terminated_at n
/-- Functorial action of the functor `option (α × _)` -/
@[simp] def omap (f : β → γ) : option (α × β) → option (α × γ)
| none := none
| (some (a, b)) := some (a, f b)
/-- Get the first element of a sequence -/
def head (s : seq α) : option α := nth s 0
/-- Get the tail of a sequence (or `nil` if the sequence is `nil`) -/
def tail : seq α → seq α
| ⟨f, al⟩ := ⟨f.tail, λ n, al⟩
protected def mem (a : α) (s : seq α) := some a ∈ s.1
instance : has_mem α (seq α) :=
⟨seq.mem⟩
theorem le_stable (s : seq α) {m n} (h : m ≤ n) :
s.1 m = none → s.1 n = none :=
by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]}
/-- If a sequence terminated at position `n`, it also terminated at `m ≥ n `. -/
lemma terminated_stable {s : seq α} {m n : ℕ} (m_le_n : m ≤ n)
(terminated_at_m : s.terminated_at m) :
s.terminated_at n :=
le_stable s m_le_n terminated_at_m
/--
If `s.nth n = some aₙ` for some value `aₙ`, then there is also some value `aₘ` such
that `s.nth = some aₘ` for `m ≤ n`.
-/
lemma ge_stable (s : seq α) {aₙ : α} {n m : ℕ} (m_le_n : m ≤ n)
(s_nth_eq_some : s.nth n = some aₙ) :
∃ (aₘ : α), s.nth m = some aₘ :=
have s.nth n ≠ none, by simp [s_nth_eq_some],
have s.nth m ≠ none, from mt (s.le_stable m_le_n) this,
with_one.ne_one_iff_exists.elim_left this
theorem not_mem_nil (a : α) : a ∉ @nil α :=
λ ⟨n, (h : some a = none)⟩, by injection h
theorem mem_cons (a : α) : ∀ (s : seq α), a ∈ cons a s
| ⟨f, al⟩ := stream.mem_cons (some a) _
theorem mem_cons_of_mem (y : α) {a : α} : ∀ {s : seq α}, a ∈ s → a ∈ cons y s
| ⟨f, al⟩ := stream.mem_cons_of_mem (some y)
theorem eq_or_mem_of_mem_cons {a b : α} : ∀ {s : seq α}, a ∈ cons b s → a = b ∨ a ∈ s
| ⟨f, al⟩ h := (stream.eq_or_mem_of_mem_cons h).imp_left (λh, by injection h)
@[simp] theorem mem_cons_iff {a b : α} {s : seq α} : a ∈ cons b s ↔ a = b ∨ a ∈ s :=
⟨eq_or_mem_of_mem_cons, λo, by cases o with e m;
[{rw e, apply mem_cons}, exact mem_cons_of_mem _ m]⟩
/-- Destructor for a sequence, resulting in either `none` (for `nil`) or
`some (a, s)` (for `cons a s`). -/
def destruct (s : seq α) : option (seq1 α) :=
(λa', (a', s.tail)) <$> nth s 0
theorem destruct_eq_nil {s : seq α} : destruct s = none → s = nil :=
begin
dsimp [destruct],
induction f0 : nth s 0; intro h,
{ apply subtype.eq,
funext n,
induction n with n IH, exacts [f0, s.2 IH] },
{ contradiction }
end
theorem destruct_eq_cons {s : seq α} {a s'} : destruct s = some (a, s') → s = cons a s' :=
begin
dsimp [destruct],
induction f0 : nth s 0 with a'; intro h,
{ contradiction },
{ unfold functor.map at h,
cases s with f al,
injections with _ h1 h2,
rw ←h2, apply subtype.eq, dsimp [tail, cons],
rw h1 at f0, rw ←f0,
exact (stream.eta f).symm }
end
@[simp] theorem destruct_nil : destruct (nil : seq α) = none := rfl
@[simp] theorem destruct_cons (a : α) : ∀ s, destruct (cons a s) = some (a, s)
| ⟨f, al⟩ := begin
unfold cons destruct functor.map,
apply congr_arg (λ s, some (a, s)),
apply subtype.eq, dsimp [tail], rw [stream.tail_cons]
end
theorem head_eq_destruct (s : seq α) : head s = prod.fst <$> destruct s :=
by unfold destruct head; cases nth s 0; refl
@[simp] theorem head_nil : head (nil : seq α) = none := rfl
@[simp] theorem head_cons (a : α) (s) : head (cons a s) = some a :=
by rw [head_eq_destruct, destruct_cons]; refl
@[simp] theorem tail_nil : tail (nil : seq α) = nil := rfl
@[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s :=
by cases s with f al; apply subtype.eq; dsimp [tail, cons]; rw [stream.tail_cons]
def cases_on {C : seq α → Sort v} (s : seq α)
(h1 : C nil) (h2 : ∀ x s, C (cons x s)) : C s := begin
induction H : destruct s with v v,
{ rw destruct_eq_nil H, apply h1 },
{ cases v with a s', rw destruct_eq_cons H, apply h2 }
end
theorem mem_rec_on {C : seq α → Prop} {a s} (M : a ∈ s)
(h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) : C s :=
begin
cases M with k e, unfold stream.nth at e,
induction k with k IH generalizing s,
{ have TH : s = cons a (tail s),
{ apply destruct_eq_cons,
unfold destruct nth functor.map, rw ←e, refl },
rw TH, apply h1 _ _ (or.inl rfl) },
revert e, apply s.cases_on _ (λ b s', _); intro e,
{ injection e },
{ have h_eq : (cons b s').val (nat.succ k) = s'.val k, { cases s'; refl },
rw [h_eq] at e,
apply h1 _ _ (or.inr (IH e)) }
end
def corec.F (f : β → option (α × β)) : option β → option α × option β
| none := (none, none)
| (some b) := match f b with none := (none, none) | some (a, b') := (some a, some b') end
/-- Corecursor for `seq α` as a coinductive type. Iterates `f` to produce new elements
of the sequence until `none` is obtained. -/
def corec (f : β → option (α × β)) (b : β) : seq α :=
begin
refine ⟨stream.corec' (corec.F f) (some b), λn h, _⟩,
rw stream.corec'_eq,
change stream.corec' (corec.F f) (corec.F f (some b)).2 n = none,
revert h, generalize : some b = o, revert o,
induction n with n IH; intro o,
{ change (corec.F f o).1 = none → (corec.F f (corec.F f o).2).1 = none,
cases o with b; intro h, { refl },
dsimp [corec.F] at h, dsimp [corec.F],
cases f b with s, { refl },
{ cases s with a b', contradiction } },
{ rw [stream.corec'_eq (corec.F f) (corec.F f o).2,
stream.corec'_eq (corec.F f) o],
exact IH (corec.F f o).2 }
end
@[simp] theorem corec_eq (f : β → option (α × β)) (b : β) :
destruct (corec f b) = omap (corec f) (f b) :=
begin
dsimp [corec, destruct, nth],
change stream.corec' (corec.F f) (some b) 0 with (corec.F f (some b)).1,
unfold functor.map, dsimp [corec.F],
induction h : f b with s, { refl },
cases s with a b', dsimp [corec.F],
apply congr_arg (λ b', some (a, b')),
apply subtype.eq,
dsimp [corec, tail],
rw [stream.corec'_eq, stream.tail_cons],
dsimp [corec.F], rw h, refl
end
/-- Embed a list as a sequence -/
def of_list (l : list α) : seq α :=
⟨list.nth l, λn h, begin
induction l with a l IH generalizing n, refl,
dsimp [list.nth], cases n with n; dsimp [list.nth] at h,
{ contradiction },
{ apply IH _ h }
end⟩
instance coe_list : has_coe (list α) (seq α) := ⟨of_list⟩
section bisim
variable (R : seq α → seq α → Prop)
local infix ~ := R
def bisim_o : option (seq1 α) → option (seq1 α) → Prop
| none none := true
| (some (a, s)) (some (a', s')) := a = a' ∧ R s s'
| _ _ := false
attribute [simp] bisim_o
def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂)
-- If two streams are bisimilar, then they are equal
theorem eq_of_bisim (bisim : is_bisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ :=
begin
apply subtype.eq,
apply stream.eq_of_bisim (λx y, ∃ s s' : seq α, s.1 = x ∧ s'.1 = y ∧ R s s'),
dsimp [stream.is_bisimulation],
intros t₁ t₂ e,
exact match t₁, t₂, e with ._, ._, ⟨s, s', rfl, rfl, r⟩ :=
suffices head s = head s' ∧ R (tail s) (tail s'), from
and.imp id (λr, ⟨tail s, tail s',
by cases s; refl, by cases s'; refl, r⟩) this,
begin
have := bisim r, revert r this,
apply cases_on s _ _; intros; apply cases_on s' _ _; intros; intros r this,
{ constructor, refl, assumption },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_cons, destruct_cons] at this,
rw [head_cons, head_cons, tail_cons, tail_cons],
cases this with h1 h2,
constructor, rw h1, exact h2 }
end
end,
exact ⟨s₁, s₂, rfl, rfl, r⟩
end
end bisim
theorem coinduction : ∀ {s₁ s₂ : seq α}, head s₁ = head s₂ →
(∀ (β : Type u) (fr : seq α → β),
fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ hh ht :=
subtype.eq (stream.coinduction hh (λ β fr, ht β (λs, fr s.1)))
theorem coinduction2 (s) (f g : seq α → seq β)
(H : ∀ s, bisim_o (λ (s1 s2 : seq β), ∃ (s : seq α), s1 = f s ∧ s2 = g s)
(destruct (f s)) (destruct (g s)))
: f s = g s :=
begin
refine eq_of_bisim (λ s1 s2, ∃ s, s1 = f s ∧ s2 = g s) _ ⟨s, rfl, rfl⟩,
intros s1 s2 h, rcases h with ⟨s, h1, h2⟩,
rw [h1, h2], apply H
end
/-- Embed an infinite stream as a sequence -/
def of_stream (s : stream α) : seq α :=
⟨s.map some, λn h, by contradiction⟩
instance coe_stream : has_coe (stream α) (seq α) := ⟨of_stream⟩
/-- Embed a `lazy_list α` as a sequence. Note that even though this
is non-meta, it will produce infinite sequences if used with
cyclic `lazy_list`s created by meta constructions. -/
def of_lazy_list : lazy_list α → seq α :=
corec (λl, match l with
| lazy_list.nil := none
| lazy_list.cons a l' := some (a, l' ())
end)
instance coe_lazy_list : has_coe (lazy_list α) (seq α) := ⟨of_lazy_list⟩
/-- Translate a sequence into a `lazy_list`. Since `lazy_list` and `list`
are isomorphic as non-meta types, this function is necessarily meta. -/
meta def to_lazy_list : seq α → lazy_list α | s :=
match destruct s with
| none := lazy_list.nil
| some (a, s') := lazy_list.cons a (to_lazy_list s')
end
/-- Translate a sequence to a list. This function will run forever if
run on an infinite sequence. -/
meta def force_to_list (s : seq α) : list α := (to_lazy_list s).to_list
/-- The sequence of natural numbers some 0, some 1, ... -/
def nats : seq ℕ := stream.nats
@[simp]
lemma nats_nth (n : ℕ) : nats.nth n = some n := rfl
/-- Append two sequences. If `s₁` is infinite, then `s₁ ++ s₂ = s₁`,
otherwise it puts `s₂` at the location of the `nil` in `s₁`. -/
def append (s₁ s₂ : seq α) : seq α :=
@corec α (seq α × seq α) (λ⟨s₁, s₂⟩,
match destruct s₁ with
| none := omap (λs₂, (nil, s₂)) (destruct s₂)
| some (a, s₁') := some (a, s₁', s₂)
end) (s₁, s₂)
/-- Map a function over a sequence. -/
def map (f : α → β) : seq α → seq β | ⟨s, al⟩ :=
⟨s.map (option.map f),
λn, begin
dsimp [stream.map, stream.nth],
induction e : s n; intro,
{ rw al e, assumption }, { contradiction }
end⟩
/-- Flatten a sequence of sequences. (It is required that the
sequences be nonempty to ensure productivity; in the case
of an infinite sequence of `nil`, the first element is never
generated.) -/
def join : seq (seq1 α) → seq α :=
corec (λS, match destruct S with
| none := none
| some ((a, s), S') := some (a, match destruct s with
| none := S'
| some s' := cons s' S'
end)
end)
/-- Remove the first `n` elements from the sequence. -/
def drop (s : seq α) : ℕ → seq α
| 0 := s
| (n+1) := tail (drop n)
attribute [simp] drop
/-- Take the first `n` elements of the sequence (producing a list) -/
def take : ℕ → seq α → list α
| 0 s := []
| (n+1) s := match destruct s with
| none := []
| some (x, r) := list.cons x (take n r)
end
/-- Split a sequence at `n`, producing a finite initial segment
and an infinite tail. -/
def split_at : ℕ → seq α → list α × seq α
| 0 s := ([], s)
| (n+1) s := match destruct s with
| none := ([], nil)
| some (x, s') := let (l, r) := split_at n s' in (list.cons x l, r)
end
section zip_with
/-- Combine two sequences with a function -/
def zip_with (f : α → β → γ) : seq α → seq β → seq γ
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ := ⟨λn,
match f₁ n, f₂ n with
| some a, some b := some (f a b)
| _, _ := none
end,
λn, begin
induction h1 : f₁ n,
{ intro H, simp only [(a₁ h1)], refl },
induction h2 : f₂ n; dsimp [seq.zip_with._match_1]; intro H,
{ rw (a₂ h2), cases f₁ (n + 1); refl },
{ rw [h1, h2] at H, contradiction }
end⟩
variables {s : seq α} {s' : seq β} {n : ℕ}
lemma zip_with_nth_some {a : α} {b : β} (s_nth_eq_some : s.nth n = some a)
(s_nth_eq_some' : s'.nth n = some b) (f : α → β → γ) :
(zip_with f s s').nth n = some (f a b) :=
begin
cases s with st,
have : st n = some a, from s_nth_eq_some,
cases s' with st',
have : st' n = some b, from s_nth_eq_some',
simp only [zip_with, seq.nth, *]
end
lemma zip_with_nth_none (s_nth_eq_none : s.nth n = none) (f : α → β → γ) :
(zip_with f s s').nth n = none :=
begin
cases s with st,
have : st n = none, from s_nth_eq_none,
cases s' with st',
cases st'_nth_eq : st' n;
simp only [zip_with, seq.nth, *]
end
lemma zip_with_nth_none' (s'_nth_eq_none : s'.nth n = none) (f : α → β → γ) :
(zip_with f s s').nth n = none :=
begin
cases s' with st',
have : st' n = none, from s'_nth_eq_none,
cases s with st,
cases st_nth_eq : st n;
simp only [zip_with, seq.nth, *]
end
end zip_with
/-- Pair two sequences into a sequence of pairs -/
def zip : seq α → seq β → seq (α × β) := zip_with prod.mk
/-- Separate a sequence of pairs into two sequences -/
def unzip (s : seq (α × β)) : seq α × seq β := (map prod.fst s, map prod.snd s)
/-- Convert a sequence which is known to terminate into a list -/
def to_list (s : seq α) (h : ∃ n, ¬ (nth s n).is_some) : list α :=
take (nat.find h) s
/-- Convert a sequence which is known not to terminate into a stream -/
def to_stream (s : seq α) (h : ∀ n, (nth s n).is_some) : stream α :=
λn, option.get (h n)
/-- Convert a sequence into either a list or a stream depending on whether
it is finite or infinite. (Without decidability of the infiniteness predicate,
this is not constructively possible.) -/
def to_list_or_stream (s : seq α) [decidable (∃ n, ¬ (nth s n).is_some)] :
list α ⊕ stream α :=
if h : ∃ n, ¬ (nth s n).is_some
then sum.inl (to_list s h)
else sum.inr (to_stream s (λn, decidable.by_contradiction (λ hn, h ⟨n, hn⟩)))
@[simp] theorem nil_append (s : seq α) : append nil s = s :=
begin
apply coinduction2, intro s,
dsimp [append], rw [corec_eq],
dsimp [append], apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) :=
destruct_eq_cons $ begin
dsimp [append], rw [corec_eq],
dsimp [append], rw [destruct_cons],
dsimp [append], refl
end
@[simp] theorem append_nil (s : seq α) : append s nil = s :=
begin
apply coinduction2 s, intro s,
apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [cons_append, destruct_cons, destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem append_assoc (s t u : seq α) :
append (append s t) u = append s (append t u) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t u,
s1 = append (append s t) u ∧ s2 = append s (append t u)),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, u, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ apply cases_on u; simp,
{ intros x u, refine ⟨nil, nil, u, _, _⟩; simp } },
{ intros x t, refine ⟨nil, t, u, _, _⟩; simp } },
{ intros x s, exact ⟨s, t, u, rfl, rfl⟩ }
end end },
{ exact ⟨s, t, u, rfl, rfl⟩ }
end
@[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl
@[simp] theorem map_cons (f : α → β) (a) : ∀ s, map f (cons a s) = cons (f a) (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [cons, map]; rw stream.map_cons; refl
@[simp] theorem map_id : ∀ (s : seq α), map id s = s
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw [option.map_id, stream.map_id]; refl
end
@[simp] theorem map_tail (f : α → β) : ∀ s, map f (tail s) = tail (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [tail, map]; rw stream.map_tail; refl
theorem map_comp (f : α → β) (g : β → γ) : ∀ (s : seq α), map (g ∘ f) s = map g (map f s)
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw stream.map_map,
apply congr_arg (λ f : _ → option γ, stream.map f s),
funext x, cases x with x; refl
end
@[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t,
s1 = map f (append s t) ∧ s2 = append (map f s) (map f t)) _ ⟨s, t, rfl, rfl⟩,
intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ intros x t, refine ⟨nil, t, _, _⟩; simp } },
{ intros x s, refine ⟨s, t, rfl, rfl⟩ }
end end
end
@[simp] theorem map_nth (f : α → β) : ∀ s n, nth (map f s) n = (nth s n).map f
| ⟨s, al⟩ n := rfl
instance : functor seq := {map := @map}
instance : is_lawful_functor seq :=
{ id_map := @map_id, comp_map := @map_comp }
@[simp] theorem join_nil : join nil = (nil : seq α) := destruct_eq_nil rfl
@[simp] theorem join_cons_nil (a : α) (S) :
join (cons (a, nil) S) = cons a (join S) :=
destruct_eq_cons $ by simp [join]
@[simp] theorem join_cons_cons (a b : α) (s S) :
join (cons (a, cons b s) S) = cons a (join (cons (b, s) S)) :=
destruct_eq_cons $ by simp [join]
@[simp, priority 990] theorem join_cons (a : α) (s S) :
join (cons (a, s) S) = cons a (append s (join S)) :=
begin
apply eq_of_bisim (λs1 s2, s1 = s2 ∨
∃ a s S, s1 = join (cons (a, s) S) ∧
s2 = cons a (append s (join S))) _ (or.inr ⟨a, s, S, rfl, rfl⟩),
intros s1 s2 h,
exact match s1, s2, h with
| _, _, (or.inl $ eq.refl s) := begin
apply cases_on s, { trivial },
{ intros x s, rw [destruct_cons], exact ⟨rfl, or.inl rfl⟩ }
end
| ._, ._, (or.inr ⟨a, s, S, rfl, rfl⟩) := begin
apply cases_on s,
{ simp },
{ intros x s, simp, refine or.inr ⟨x, s, S, rfl, rfl⟩ }
end
end
end
@[simp] theorem join_append (S T : seq (seq1 α)) :
join (append S T) = append (join S) (join T) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s S T,
s1 = append s (join (append S T)) ∧
s2 = append s (append (join S) (join T))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ apply cases_on T, { simp },
{ intros s T, cases s with a s; simp,
refine ⟨s, nil, T, _, _⟩; simp } },
{ intros s S, cases s with a s; simp,
exact ⟨s, S, T, rfl, rfl⟩ } },
{ intros x s, exact ⟨s, S, T, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, T, _, _⟩; simp }
end
@[simp] theorem of_list_nil : of_list [] = (nil : seq α) := rfl
@[simp] theorem of_list_cons (a : α) (l) :
of_list (a :: l) = cons a (of_list l) :=
begin
apply subtype.eq, simp [of_list, cons],
funext n, cases n; simp [list.nth, stream.cons]
end
@[simp] theorem of_stream_cons (a : α) (s) :
of_stream (a :: s) = cons a (of_stream s) :=
by apply subtype.eq; simp [of_stream, cons]; rw stream.map_cons
@[simp] theorem of_list_append (l l' : list α) :
of_list (l ++ l') = append (of_list l) (of_list l') :=
by induction l; simp [*]
@[simp] theorem of_stream_append (l : list α) (s : stream α) :
of_stream (l ++ₛ s) = append (of_list l) (of_stream s) :=
by induction l; simp [*, stream.nil_append_stream, stream.cons_append_stream]
/-- Convert a sequence into a list, embedded in a computation to allow for
the possibility of infinite sequences (in which case the computation
never returns anything). -/
def to_list' {α} (s : seq α) : computation (list α) :=
@computation.corec (list α) (list α × seq α) (λ⟨l, s⟩,
match destruct s with
| none := sum.inl l.reverse
| some (a, s') := sum.inr (a::l, s')
end) ([], s)
theorem dropn_add (s : seq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n
| 0 := rfl
| (n+1) := congr_arg tail (dropn_add n)
theorem dropn_tail (s : seq α) (n) : drop (tail s) n = drop s (n + 1) :=
by rw add_comm; symmetry; apply dropn_add
theorem nth_tail : ∀ (s : seq α) n, nth (tail s) n = nth s (n + 1)
| ⟨f, al⟩ n := rfl
@[ext]
protected lemma ext (s s': seq α) (hyp : ∀ (n : ℕ), s.nth n = s'.nth n) : s = s' :=
begin
let ext := (λ (s s' : seq α), ∀ n, s.nth n = s'.nth n),
apply seq.eq_of_bisim ext _ hyp,
-- we have to show that ext is a bisimulation
clear hyp s s',
assume s s' (hyp : ext s s'),
unfold seq.destruct,
rw (hyp 0),
cases (s'.nth 0),
{ simp [seq.bisim_o] }, -- option.none
{ -- option.some
suffices : ext s.tail s'.tail, by simpa,
assume n,
simp only [seq.nth_tail _ n, (hyp $ n + 1)] }
end
@[simp] theorem head_dropn (s : seq α) (n) : head (drop s n) = nth s n :=
begin
induction n with n IH generalizing s, { refl },
rw [nat.succ_eq_add_one, ←nth_tail, ←dropn_tail], apply IH
end
theorem mem_map (f : α → β) {a : α} : ∀ {s : seq α}, a ∈ s → f a ∈ map f s
| ⟨g, al⟩ := stream.mem_map (option.map f)
theorem exists_of_mem_map {f} {b : β} : ∀ {s : seq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b
| ⟨g, al⟩ h := let ⟨o, om, oe⟩ := stream.exists_of_mem_map h in
by cases o with a; injection oe with h'; exact ⟨a, om, h'⟩
theorem of_mem_append {s₁ s₂ : seq α} {a : α} (h : a ∈ append s₁ s₂) : a ∈ s₁ ∨ a ∈ s₂ :=
begin
have := h, revert this,
generalize e : append s₁ s₂ = ss, intro h, revert s₁,
apply mem_rec_on h _,
intros b s' o s₁,
apply s₁.cases_on _ (λ c t₁, _); intros m e;
have := congr_arg destruct e,
{ apply or.inr, simpa using m },
{ cases (show a = c ∨ a ∈ append t₁ s₂, by simpa using m) with e' m,
{ rw e', exact or.inl (mem_cons _ _) },
{ cases (show c = b ∧ append t₁ s₂ = s', by simpa) with i1 i2,
cases o with e' IH,
{ simp [i1, e'] },
{ exact or.imp_left (mem_cons_of_mem _) (IH m i2) } } }
end
theorem mem_append_left {s₁ s₂ : seq α} {a : α} (h : a ∈ s₁) : a ∈ append s₁ s₂ :=
by apply mem_rec_on h; intros; simp [*]
end seq
namespace seq1
variables {α : Type u} {β : Type v} {γ : Type w}
open seq
/-- Convert a `seq1` to a sequence. -/
def to_seq : seq1 α → seq α
| (a, s) := cons a s
instance coe_seq : has_coe (seq1 α) (seq α) := ⟨to_seq⟩
/-- Map a function on a `seq1` -/
def map (f : α → β) : seq1 α → seq1 β
| (a, s) := (f a, seq.map f s)
theorem map_id : ∀ (s : seq1 α), map id s = s | ⟨a, s⟩ := by simp [map]
/-- Flatten a nonempty sequence of nonempty sequences -/
def join : seq1 (seq1 α) → seq1 α
| ((a, s), S) := match destruct s with
| none := (a, seq.join S)
| some s' := (a, seq.join (cons s' S))
end
@[simp] theorem join_nil (a : α) (S) : join ((a, nil), S) = (a, seq.join S) := rfl
@[simp] theorem join_cons (a b : α) (s S) :
join ((a, cons b s), S) = (a, seq.join (cons (b, s) S)) :=
by dsimp [join]; rw [destruct_cons]; refl
/-- The `return` operator for the `seq1` monad,
which produces a singleton sequence. -/
def ret (a : α) : seq1 α := (a, nil)
instance [inhabited α] : inhabited (seq1 α) := ⟨ret (default _)⟩
/-- The `bind` operator for the `seq1` monad,
which maps `f` on each element of `s` and appends the results together.
(Not all of `s` may be evaluated, because the first few elements of `s`
may already produce an infinite result.) -/
def bind (s : seq1 α) (f : α → seq1 β) : seq1 β :=
join (map f s)
@[simp] theorem join_map_ret (s : seq α) : seq.join (seq.map ret s) = s :=
by apply coinduction2 s; intro s; apply cases_on s; simp [ret]
@[simp] theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s
| ⟨a, s⟩ := begin
dsimp [bind, map], change (λx, ret (f x)) with (ret ∘ f),
rw [map_comp], simp [function.comp, ret]
end
@[simp] theorem ret_bind (a : α) (f : α → seq1 β) : bind (ret a) f = f a :=
begin
simp [ret, bind, map],
cases f a with a s,
apply cases_on s; intros; simp
end
@[simp] theorem map_join' (f : α → β) (S) :
seq.map f (seq.join S) = seq.join (seq.map (map f) S) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s S, s1 = append s (seq.map f (seq.join S)) ∧
s2 = append s (seq.join (seq.map (map f) S))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ intros x S, cases x with a s; simp [map],
exact ⟨_, _, rfl, rfl⟩ } },
{ intros x s, refine ⟨s, S, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, _, _⟩; simp }
end
@[simp] theorem map_join (f : α → β) : ∀ S, map f (join S) = join (map (map f) S)
| ((a, s), S) := by apply cases_on s; intros; simp [map]
@[simp] theorem join_join (SS : seq (seq1 (seq1 α))) :
seq.join (seq.join SS) = seq.join (seq.map join SS) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s SS, s1 = seq.append s (seq.join (seq.join SS)) ∧
s2 = seq.append s (seq.join (seq.map join SS))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, SS, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on SS; simp,
{ intros S SS, cases S with s S; cases s with x s; simp [map],
apply cases_on s; simp,
{ exact ⟨_, _, rfl, rfl⟩ },
{ intros x s,
refine ⟨cons x (append s (seq.join S)), SS, _, _⟩; simp } } },
{ intros x s, exact ⟨s, SS, rfl, rfl⟩ }
end end },
{ refine ⟨nil, SS, _, _⟩; simp }
end
@[simp] theorem bind_assoc (s : seq1 α) (f : α → seq1 β) (g : β → seq1 γ) :
bind (bind s f) g = bind s (λ (x : α), bind (f x) g) :=
begin
cases s with a s,
simp [bind, map],
rw [←map_comp],
change (λ x, join (map g (f x))) with (join ∘ ((map g) ∘ f)),
rw [map_comp _ join],
generalize : seq.map (map g ∘ f) s = SS,
rcases map g (f a) with ⟨⟨a, s⟩, S⟩,
apply cases_on s; intros; apply cases_on S; intros; simp,
{ cases x with x t, apply cases_on t; intros; simp },
{ cases x_1 with y t; simp }
end
instance : monad seq1 :=
{ map := @map,
pure := @ret,
bind := @bind }
instance : is_lawful_monad seq1 :=
{ id_map := @map_id,
bind_pure_comp_eq_map := @bind_ret,
pure_bind := @ret_bind,
bind_assoc := @bind_assoc }
end seq1
|
b5577b81ed2cfba7bc05f302b35948fb65ff64ef | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/group/hom.lean | b320c58ab8c5652f50bb1e1db457990c6011ef66 | [
"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 | 38,069 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes,
Johannes Hölzl, Yury Kudryashov
-/
import algebra.group.commute
import algebra.group_with_zero.defs
/-!
# monoid and group homomorphisms
This file defines the bundled structures for monoid and group homomorphisms. Namely, we define
`monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp.,
additive) monoids or groups.
We also define coercion to a function, and usual operations: composition, identity homomorphism,
pointwise multiplication and pointwise inversion.
This file also defines the lesser-used (and notation-less) homomorphism types which are used as
building blocks for other homomorphisms:
* `zero_hom`
* `one_hom`
* `add_hom`
* `mul_hom`
* `monoid_with_zero_hom`
## Notations
* `→*` for bundled monoid homs (also use for group homs)
* `→+` for bundled add_monoid homs (also use for add_group homs)
## implementation notes
There's a coercion from bundled homs to fun, and the canonical
notation is to use the bundled hom as a function via this coercion.
There is no `group_hom` -- the idea is that `monoid_hom` is used.
The constructor for `monoid_hom` needs a proof of `map_one` as well
as `map_mul`; a separate constructor `monoid_hom.mk'` will construct
group homs (i.e. monoid homs between groups) given only a proof
that multiplication is preserved,
Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the
instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they
can be inferred from the type it is faster to use this method than to use type class inference.
Historically this file also included definitions of unbundled homomorphism classes; they were
deprecated and moved to `deprecated/group`.
## Tags
monoid_hom, add_monoid_hom
-/
variables {M : Type*} {N : Type*} {P : Type*} -- monoids
{G : Type*} {H : Type*} -- groups
-- for easy multiple inheritance
set_option old_structure_cmd true
/-- Homomorphism that preserves zero -/
structure zero_hom (M : Type*) (N : Type*) [has_zero M] [has_zero N] :=
(to_fun : M → N)
(map_zero' : to_fun 0 = 0)
/-- Homomorphism that preserves addition -/
structure add_hom (M : Type*) (N : Type*) [has_add M] [has_add N] :=
(to_fun : M → N)
(map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y)
/-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/
@[ancestor zero_hom add_hom]
structure add_monoid_hom (M : Type*) (N : Type*) [add_zero_class M] [add_zero_class N]
extends zero_hom M N, add_hom M N
attribute [nolint doc_blame] add_monoid_hom.to_add_hom
attribute [nolint doc_blame] add_monoid_hom.to_zero_hom
infixr ` →+ `:25 := add_monoid_hom
/-- Homomorphism that preserves one -/
@[to_additive]
structure one_hom (M : Type*) (N : Type*) [has_one M] [has_one N] :=
(to_fun : M → N)
(map_one' : to_fun 1 = 1)
/-- Homomorphism that preserves multiplication -/
@[to_additive]
structure mul_hom (M : Type*) (N : Type*) [has_mul M] [has_mul N] :=
(to_fun : M → N)
(map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y)
/-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/
@[ancestor one_hom mul_hom, to_additive]
structure monoid_hom (M : Type*) (N : Type*) [mul_one_class M] [mul_one_class N]
extends one_hom M N, mul_hom M N
/-- Bundled monoid with zero homomorphisms; use this for bundled group with zero homomorphisms
too. -/
@[ancestor zero_hom monoid_hom]
structure monoid_with_zero_hom (M : Type*) (N : Type*) [mul_zero_one_class M] [mul_zero_one_class N]
extends zero_hom M N, monoid_hom M N
attribute [nolint doc_blame] monoid_hom.to_mul_hom
attribute [nolint doc_blame] monoid_hom.to_one_hom
attribute [nolint doc_blame] monoid_with_zero_hom.to_monoid_hom
attribute [nolint doc_blame] monoid_with_zero_hom.to_zero_hom
infixr ` →* `:25 := monoid_hom
-- completely uninteresting lemmas about coercion to function, that all homs need
section coes
/-! Bundled morphisms can be down-cast to weaker bundlings -/
@[to_additive]
instance monoid_hom.has_coe_to_one_hom {mM : mul_one_class M} {mN : mul_one_class N} :
has_coe (M →* N) (one_hom M N) := ⟨monoid_hom.to_one_hom⟩
@[to_additive]
instance monoid_hom.has_coe_to_mul_hom {mM : mul_one_class M} {mN : mul_one_class N} :
has_coe (M →* N) (mul_hom M N) := ⟨monoid_hom.to_mul_hom⟩
instance monoid_with_zero_hom.has_coe_to_monoid_hom
{mM : mul_zero_one_class M} {mN : mul_zero_one_class N} :
has_coe (monoid_with_zero_hom M N) (M →* N) := ⟨monoid_with_zero_hom.to_monoid_hom⟩
instance monoid_with_zero_hom.has_coe_to_zero_hom
{mM : mul_zero_one_class M} {mN : mul_zero_one_class N} :
has_coe (monoid_with_zero_hom M N) (zero_hom M N) := ⟨monoid_with_zero_hom.to_zero_hom⟩
/-! The simp-normal form of morphism coercion is `f.to_..._hom`. This choice is primarily because
this is the way things were before the above coercions were introduced. Bundled morphisms defined
elsewhere in Mathlib may choose `↑f` as their simp-normal form instead. -/
@[simp, to_additive]
lemma monoid_hom.coe_eq_to_one_hom {mM : mul_one_class M} {mN : mul_one_class N} (f : M →* N) :
(f : one_hom M N) = f.to_one_hom := rfl
@[simp, to_additive]
lemma monoid_hom.coe_eq_to_mul_hom {mM : mul_one_class M} {mN : mul_one_class N} (f : M →* N) :
(f : mul_hom M N) = f.to_mul_hom := rfl
@[simp]
lemma monoid_with_zero_hom.coe_eq_to_monoid_hom
{mM : mul_zero_one_class M} {mN : mul_zero_one_class N} (f : monoid_with_zero_hom M N) :
(f : M →* N) = f.to_monoid_hom := rfl
@[simp]
lemma monoid_with_zero_hom.coe_eq_to_zero_hom
{mM : mul_zero_one_class M} {mN : mul_zero_one_class N} (f : monoid_with_zero_hom M N) :
(f : zero_hom M N) = f.to_zero_hom := rfl
@[to_additive]
instance {mM : has_one M} {mN : has_one N} : has_coe_to_fun (one_hom M N) (λ _, M → N) :=
⟨one_hom.to_fun⟩
@[to_additive]
instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (mul_hom M N) (λ _, M → N) :=
⟨mul_hom.to_fun⟩
@[to_additive]
instance {mM : mul_one_class M} {mN : mul_one_class N} : has_coe_to_fun (M →* N) (λ _, M → N) :=
⟨monoid_hom.to_fun⟩
instance {mM : mul_zero_one_class M} {mN : mul_zero_one_class N} :
has_coe_to_fun (monoid_with_zero_hom M N) (λ _, M → N) :=
⟨monoid_with_zero_hom.to_fun⟩
-- these must come after the coe_to_fun definitions
initialize_simps_projections zero_hom (to_fun → apply)
initialize_simps_projections add_hom (to_fun → apply)
initialize_simps_projections add_monoid_hom (to_fun → apply)
initialize_simps_projections one_hom (to_fun → apply)
initialize_simps_projections mul_hom (to_fun → apply)
initialize_simps_projections monoid_hom (to_fun → apply)
initialize_simps_projections monoid_with_zero_hom (to_fun → apply)
@[simp, to_additive]
lemma one_hom.to_fun_eq_coe [has_one M] [has_one N] (f : one_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma mul_hom.to_fun_eq_coe [has_mul M] [has_mul N] (f : mul_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_fun_eq_coe [mul_one_class M] [mul_one_class N]
(f : M →* N) : f.to_fun = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_fun_eq_coe [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) : f.to_fun = f := rfl
@[simp, to_additive]
lemma one_hom.coe_mk [has_one M] [has_one N]
(f : M → N) (h1) : ⇑(one_hom.mk f h1) = f := rfl
@[simp, to_additive]
lemma mul_hom.coe_mk [has_mul M] [has_mul N]
(f : M → N) (hmul) : ⇑(mul_hom.mk f hmul) = f := rfl
@[simp, to_additive]
lemma monoid_hom.coe_mk [mul_one_class M] [mul_one_class N]
(f : M → N) (h1 hmul) : ⇑(monoid_hom.mk f h1 hmul) = f := rfl
@[simp]
lemma monoid_with_zero_hom.coe_mk [mul_zero_one_class M] [mul_zero_one_class N]
(f : M → N) (h0 h1 hmul) : ⇑(monoid_with_zero_hom.mk f h0 h1 hmul) = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_one_hom_coe [mul_one_class M] [mul_one_class N] (f : M →* N) :
(f.to_one_hom : M → N) = f := rfl
@[simp, to_additive]
lemma monoid_hom.to_mul_hom_coe [mul_one_class M] [mul_one_class N] (f : M →* N) :
(f.to_mul_hom : M → N) = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_zero_hom_coe [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) :
(f.to_zero_hom : M → N) = f := rfl
@[simp]
lemma monoid_with_zero_hom.to_monoid_hom_coe [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) :
(f.to_monoid_hom : M → N) = f := rfl
@[to_additive]
theorem one_hom.congr_fun [has_one M] [has_one N]
{f g : one_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : one_hom M N, h x) h
@[to_additive]
theorem mul_hom.congr_fun [has_mul M] [has_mul N]
{f g : mul_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : mul_hom M N, h x) h
@[to_additive]
theorem monoid_hom.congr_fun [mul_one_class M] [mul_one_class N]
{f g : M →* N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : M →* N, h x) h
theorem monoid_with_zero_hom.congr_fun [mul_zero_one_class M] [mul_zero_one_class N]
{f g : monoid_with_zero_hom M N} (h : f = g) (x : M) : f x = g x :=
congr_arg (λ h : monoid_with_zero_hom M N, h x) h
@[to_additive]
theorem one_hom.congr_arg [has_one M] [has_one N]
(f : one_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
theorem mul_hom.congr_arg [has_mul M] [has_mul N]
(f : mul_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
theorem monoid_hom.congr_arg [mul_one_class M] [mul_one_class N]
(f : M →* N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
theorem monoid_with_zero_hom.congr_arg [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) {x y : M} (h : x = y) : f x = f y :=
congr_arg (λ x : M, f x) h
@[to_additive]
lemma one_hom.coe_inj [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[to_additive]
lemma mul_hom.coe_inj [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[to_additive]
lemma monoid_hom.coe_inj [mul_one_class M] [mul_one_class N]
⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
lemma monoid_with_zero_hom.coe_inj [mul_zero_one_class M] [mul_zero_one_class N]
⦃f g : monoid_with_zero_hom M N⦄ (h : (f : M → N) = g) : f = g :=
by cases f; cases g; cases h; refl
@[ext, to_additive]
lemma one_hom.ext [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
one_hom.coe_inj (funext h)
@[ext, to_additive]
lemma mul_hom.ext [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
mul_hom.coe_inj (funext h)
@[ext, to_additive]
lemma monoid_hom.ext [mul_one_class M] [mul_one_class N]
⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g :=
monoid_hom.coe_inj (funext h)
@[ext]
lemma monoid_with_zero_hom.ext [mul_zero_one_class M] [mul_zero_one_class N]
⦃f g : monoid_with_zero_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
monoid_with_zero_hom.coe_inj (funext h)
@[to_additive]
lemma one_hom.ext_iff [has_one M] [has_one N] {f g : one_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, one_hom.ext h⟩
@[to_additive]
lemma mul_hom.ext_iff [has_mul M] [has_mul N] {f g : mul_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, mul_hom.ext h⟩
@[to_additive]
lemma monoid_hom.ext_iff [mul_one_class M] [mul_one_class N]
{f g : M →* N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, monoid_hom.ext h⟩
lemma monoid_with_zero_hom.ext_iff [mul_zero_one_class M] [mul_zero_one_class N]
{f g : monoid_with_zero_hom M N} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, monoid_with_zero_hom.ext h⟩
@[simp, to_additive]
lemma one_hom.mk_coe [has_one M] [has_one N]
(f : one_hom M N) (h1) : one_hom.mk f h1 = f :=
one_hom.ext $ λ _, rfl
@[simp, to_additive]
lemma mul_hom.mk_coe [has_mul M] [has_mul N]
(f : mul_hom M N) (hmul) : mul_hom.mk f hmul = f :=
mul_hom.ext $ λ _, rfl
@[simp, to_additive]
lemma monoid_hom.mk_coe [mul_one_class M] [mul_one_class N]
(f : M →* N) (h1 hmul) : monoid_hom.mk f h1 hmul = f :=
monoid_hom.ext $ λ _, rfl
@[simp]
lemma monoid_with_zero_hom.mk_coe [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) (h0 h1 hmul) : monoid_with_zero_hom.mk f h0 h1 hmul = f :=
monoid_with_zero_hom.ext $ λ _, rfl
end coes
@[simp, to_additive]
lemma one_hom.map_one [has_one M] [has_one N] (f : one_hom M N) : f 1 = 1 := f.map_one'
/-- If `f` is a monoid homomorphism then `f 1 = 1`. -/
@[simp, to_additive]
lemma monoid_hom.map_one [mul_one_class M] [mul_one_class N] (f : M →* N) : f 1 = 1 := f.map_one'
@[simp]
lemma monoid_with_zero_hom.map_one [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) : f 1 = 1 := f.map_one'
/-- If `f` is an additive monoid homomorphism then `f 0 = 0`. -/
add_decl_doc add_monoid_hom.map_zero
@[simp]
lemma monoid_with_zero_hom.map_zero [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) : f 0 = 0 := f.map_zero'
@[simp, to_additive]
lemma mul_hom.map_mul [has_mul M] [has_mul N]
(f : mul_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
/-- If `f` is a monoid homomorphism then `f (a * b) = f a * f b`. -/
@[simp, to_additive]
lemma monoid_hom.map_mul [mul_one_class M] [mul_one_class N]
(f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
@[simp]
lemma monoid_with_zero_hom.map_mul [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b
/-- If `f` is an additive monoid homomorphism then `f (a + b) = f a + f b`. -/
add_decl_doc add_monoid_hom.map_add
namespace monoid_hom
variables {mM : mul_one_class M} {mN : mul_one_class N} {mP : mul_one_class P}
variables [group G] [comm_group H]
include mM mN
@[to_additive]
lemma map_mul_eq_one (f : M →* N) {a b : M} (h : a * b = 1) : f a * f b = 1 :=
by rw [← f.map_mul, h, f.map_one]
/-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a right inverse,
then `f x` has a right inverse too. For elements invertible on both sides see `is_unit.map`. -/
@[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has
a right inverse, then `f x` has a right inverse too."]
lemma map_exists_right_inv (f : M →* N) {x : M} (hx : ∃ y, x * y = 1) :
∃ y, f x * y = 1 :=
let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩
/-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a left inverse,
then `f x` has a left inverse too. For elements invertible on both sides see `is_unit.map`. -/
@[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has
a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see
`is_add_unit.map`."]
lemma map_exists_left_inv (f : M →* N) {x : M} (hx : ∃ y, y * x = 1) :
∃ y, y * f x = 1 :=
let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩
end monoid_hom
/-- Inversion on a commutative group, considered as a monoid homomorphism. -/
@[to_additive "Inversion on a commutative additive group, considered as an additive
monoid homomorphism."]
def comm_group.inv_monoid_hom {G : Type*} [comm_group G] : G →* G :=
{ to_fun := has_inv.inv,
map_one' := one_inv,
map_mul' := mul_inv }
/-- The identity map from a type with 1 to itself. -/
@[to_additive, simps]
def one_hom.id (M : Type*) [has_one M] : one_hom M M :=
{ to_fun := λ x, x, map_one' := rfl, }
/-- The identity map from a type with multiplication to itself. -/
@[to_additive, simps]
def mul_hom.id (M : Type*) [has_mul M] : mul_hom M M :=
{ to_fun := λ x, x, map_mul' := λ _ _, rfl, }
/-- The identity map from a monoid to itself. -/
@[to_additive, simps]
def monoid_hom.id (M : Type*) [mul_one_class M] : M →* M :=
{ to_fun := λ x, x, map_one' := rfl, map_mul' := λ _ _, rfl, }
/-- The identity map from a monoid_with_zero to itself. -/
@[simps]
def monoid_with_zero_hom.id (M : Type*) [mul_zero_one_class M] : monoid_with_zero_hom M M :=
{ to_fun := λ x, x, map_zero' := rfl, map_one' := rfl, map_mul' := λ _ _, rfl, }
/-- The identity map from an type with zero to itself. -/
add_decl_doc zero_hom.id
/-- The identity map from an type with addition to itself. -/
add_decl_doc add_hom.id
/-- The identity map from an additive monoid to itself. -/
add_decl_doc add_monoid_hom.id
/-- Composition of `one_hom`s as a `one_hom`. -/
@[to_additive]
def one_hom.comp [has_one M] [has_one N] [has_one P]
(hnp : one_hom N P) (hmn : one_hom M N) : one_hom M P :=
{ to_fun := hnp ∘ hmn, map_one' := by simp, }
/-- Composition of `mul_hom`s as a `mul_hom`. -/
@[to_additive]
def mul_hom.comp [has_mul M] [has_mul N] [has_mul P]
(hnp : mul_hom N P) (hmn : mul_hom M N) : mul_hom M P :=
{ to_fun := hnp ∘ hmn, map_mul' := by simp, }
/-- Composition of monoid morphisms as a monoid morphism. -/
@[to_additive]
def monoid_hom.comp [mul_one_class M] [mul_one_class N] [mul_one_class P]
(hnp : N →* P) (hmn : M →* N) : M →* P :=
{ to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp, }
/-- Composition of `monoid_with_zero_hom`s as a `monoid_with_zero_hom`. -/
def monoid_with_zero_hom.comp [mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P]
(hnp : monoid_with_zero_hom N P) (hmn : monoid_with_zero_hom M N) : monoid_with_zero_hom M P :=
{ to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_mul' := by simp, }
/-- Composition of `zero_hom`s as a `zero_hom`. -/
add_decl_doc zero_hom.comp
/-- Composition of `add_hom`s as a `add_hom`. -/
add_decl_doc add_hom.comp
/-- Composition of additive monoid morphisms as an additive monoid morphism. -/
add_decl_doc add_monoid_hom.comp
@[simp, to_additive] lemma one_hom.coe_comp [has_one M] [has_one N] [has_one P]
(g : one_hom N P) (f : one_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp, to_additive] lemma mul_hom.coe_comp [has_mul M] [has_mul N] [has_mul P]
(g : mul_hom N P) (f : mul_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp, to_additive] lemma monoid_hom.coe_comp [mul_one_class M] [mul_one_class N] [mul_one_class P]
(g : N →* P) (f : M →* N) :
⇑(g.comp f) = g ∘ f := rfl
@[simp] lemma monoid_with_zero_hom.coe_comp [mul_zero_one_class M] [mul_zero_one_class N]
[mul_zero_one_class P]
(g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) :
⇑(g.comp f) = g ∘ f := rfl
@[to_additive] lemma one_hom.comp_apply [has_one M] [has_one N] [has_one P]
(g : one_hom N P) (f : one_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
@[to_additive] lemma mul_hom.comp_apply [has_mul M] [has_mul N] [has_mul P]
(g : mul_hom N P) (f : mul_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
@[to_additive] lemma monoid_hom.comp_apply [mul_one_class M] [mul_one_class N] [mul_one_class P]
(g : N →* P) (f : M →* N) (x : M) :
g.comp f x = g (f x) := rfl
lemma monoid_with_zero_hom.comp_apply
[mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P]
(g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) (x : M) :
g.comp f x = g (f x) := rfl
/-- Composition of monoid homomorphisms is associative. -/
@[to_additive] lemma one_hom.comp_assoc {Q : Type*} [has_one M] [has_one N] [has_one P] [has_one Q]
(f : one_hom M N) (g : one_hom N P) (h : one_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive] lemma mul_hom.comp_assoc {Q : Type*} [has_mul M] [has_mul N] [has_mul P] [has_mul Q]
(f : mul_hom M N) (g : mul_hom N P) (h : mul_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive] lemma monoid_hom.comp_assoc {Q : Type*}
[mul_one_class M] [mul_one_class N] [mul_one_class P] [mul_one_class Q]
(f : M →* N) (g : N →* P) (h : P →* Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
lemma monoid_with_zero_hom.comp_assoc {Q : Type*}
[mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P] [mul_zero_one_class Q]
(f : monoid_with_zero_hom M N) (g : monoid_with_zero_hom N P) (h : monoid_with_zero_hom P Q) :
(h.comp g).comp f = h.comp (g.comp f) := rfl
@[to_additive]
lemma one_hom.cancel_right [has_one M] [has_one N] [has_one P]
{g₁ g₂ : one_hom N P} {f : one_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, one_hom.ext $ (forall_iff_forall_surj hf).1 (one_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
@[to_additive]
lemma mul_hom.cancel_right [has_mul M] [has_mul N] [has_mul P]
{g₁ g₂ : mul_hom N P} {f : mul_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, mul_hom.ext $ (forall_iff_forall_surj hf).1 (mul_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
@[to_additive]
lemma monoid_hom.cancel_right
[mul_one_class M] [mul_one_class N] [mul_one_class P]
{g₁ g₂ : N →* P} {f : M →* N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, monoid_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_hom.ext_iff.1 h), λ h, h ▸ rfl⟩
lemma monoid_with_zero_hom.cancel_right
[mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P]
{g₁ g₂ : monoid_with_zero_hom N P} {f : monoid_with_zero_hom M N} (hf : function.surjective f) :
g₁.comp f = g₂.comp f ↔ g₁ = g₂ :=
⟨λ h, monoid_with_zero_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_with_zero_hom.ext_iff.1 h),
λ h, h ▸ rfl⟩
@[to_additive]
lemma one_hom.cancel_left [has_one M] [has_one N] [has_one P]
{g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply],
λ h, h ▸ rfl⟩
@[to_additive]
lemma mul_hom.cancel_left [has_mul M] [has_mul N] [has_mul P]
{g : mul_hom N P} {f₁ f₂ : mul_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, mul_hom.ext $ λ x, hg $ by rw [← mul_hom.comp_apply, h, mul_hom.comp_apply],
λ h, h ▸ rfl⟩
@[to_additive]
lemma monoid_hom.cancel_left [mul_one_class M] [mul_one_class N] [mul_one_class P]
{g : N →* P} {f₁ f₂ : M →* N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, monoid_hom.ext $ λ x, hg $ by rw [← monoid_hom.comp_apply, h, monoid_hom.comp_apply],
λ h, h ▸ rfl⟩
lemma monoid_with_zero_hom.cancel_left
[mul_zero_one_class M] [mul_zero_one_class N] [mul_zero_one_class P]
{g : monoid_with_zero_hom N P} {f₁ f₂ : monoid_with_zero_hom M N} (hg : function.injective g) :
g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ :=
⟨λ h, monoid_with_zero_hom.ext $ λ x, hg $ by rw [
← monoid_with_zero_hom.comp_apply, h, monoid_with_zero_hom.comp_apply],
λ h, h ▸ rfl⟩
@[to_additive]
lemma monoid_hom.to_one_hom_injective [mul_one_class M] [mul_one_class N] :
function.injective (monoid_hom.to_one_hom : (M →* N) → one_hom M N) :=
λ f g h, monoid_hom.ext $ one_hom.ext_iff.mp h
@[to_additive]
lemma monoid_hom.to_mul_hom_injective [mul_one_class M] [mul_one_class N] :
function.injective (monoid_hom.to_mul_hom : (M →* N) → mul_hom M N) :=
λ f g h, monoid_hom.ext $ mul_hom.ext_iff.mp h
lemma monoid_with_zero_hom.to_monoid_hom_injective [monoid_with_zero M] [monoid_with_zero N] :
function.injective (monoid_with_zero_hom.to_monoid_hom : monoid_with_zero_hom M N → M →* N) :=
λ f g h, monoid_with_zero_hom.ext $ monoid_hom.ext_iff.mp h
lemma monoid_with_zero_hom.to_zero_hom_injective [monoid_with_zero M] [monoid_with_zero N] :
function.injective (monoid_with_zero_hom.to_zero_hom : monoid_with_zero_hom M N → zero_hom M N) :=
λ f g h, monoid_with_zero_hom.ext $ zero_hom.ext_iff.mp h
@[simp, to_additive] lemma one_hom.comp_id [has_one M] [has_one N]
(f : one_hom M N) : f.comp (one_hom.id M) = f := one_hom.ext $ λ x, rfl
@[simp, to_additive] lemma mul_hom.comp_id [has_mul M] [has_mul N]
(f : mul_hom M N) : f.comp (mul_hom.id M) = f := mul_hom.ext $ λ x, rfl
@[simp, to_additive] lemma monoid_hom.comp_id [mul_one_class M] [mul_one_class N]
(f : M →* N) : f.comp (monoid_hom.id M) = f := monoid_hom.ext $ λ x, rfl
@[simp] lemma monoid_with_zero_hom.comp_id [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) : f.comp (monoid_with_zero_hom.id M) = f :=
monoid_with_zero_hom.ext $ λ x, rfl
@[simp, to_additive] lemma one_hom.id_comp [has_one M] [has_one N]
(f : one_hom M N) : (one_hom.id N).comp f = f := one_hom.ext $ λ x, rfl
@[simp, to_additive] lemma mul_hom.id_comp [has_mul M] [has_mul N]
(f : mul_hom M N) : (mul_hom.id N).comp f = f := mul_hom.ext $ λ x, rfl
@[simp, to_additive] lemma monoid_hom.id_comp [mul_one_class M] [mul_one_class N]
(f : M →* N) : (monoid_hom.id N).comp f = f := monoid_hom.ext $ λ x, rfl
@[simp] lemma monoid_with_zero_hom.id_comp [mul_zero_one_class M] [mul_zero_one_class N]
(f : monoid_with_zero_hom M N) : (monoid_with_zero_hom.id N).comp f = f :=
monoid_with_zero_hom.ext $ λ x, rfl
@[simp, to_additive add_monoid_hom.map_nsmul]
theorem monoid_hom.map_pow [monoid M] [monoid N] (f : M →* N) (a : M) :
∀(n : ℕ), f (a ^ n) = (f a) ^ n
| 0 := by rw [pow_zero, pow_zero, f.map_one]
| (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow]
@[to_additive]
theorem monoid_hom.map_zpow' [div_inv_monoid M] [div_inv_monoid N] (f : M →* N)
(hf : ∀ x, f (x⁻¹) = (f x)⁻¹) (a : M) :
∀ n : ℤ, f (a ^ n) = (f a) ^ n
| (n : ℕ) := by rw [zpow_coe_nat, f.map_pow, zpow_coe_nat]
| -[1+n] := by rw [zpow_neg_succ_of_nat, hf, f.map_pow, ← zpow_neg_succ_of_nat]
@[to_additive]
theorem monoid_hom.map_div' [div_inv_monoid M] [div_inv_monoid N] (f : M →* N)
(hf : ∀ x, f (x⁻¹) = (f x)⁻¹) (a b : M) : f (a / b) = f a / f b :=
by rw [div_eq_mul_inv, div_eq_mul_inv, f.map_mul, hf]
section End
namespace monoid
variables (M) [mul_one_class M]
/-- The monoid of endomorphisms. -/
protected def End := M →* M
namespace End
instance : monoid (monoid.End M) :=
{ mul := monoid_hom.comp,
one := monoid_hom.id M,
mul_assoc := λ _ _ _, monoid_hom.comp_assoc _ _ _,
mul_one := monoid_hom.comp_id,
one_mul := monoid_hom.id_comp }
instance : inhabited (monoid.End M) := ⟨1⟩
instance : has_coe_to_fun (monoid.End M) (λ _, M → M) := ⟨monoid_hom.to_fun⟩
end End
@[simp] lemma coe_one : ((1 : monoid.End M) : M → M) = id := rfl
@[simp] lemma coe_mul (f g) : ((f * g : monoid.End M) : M → M) = f ∘ g := rfl
end monoid
namespace add_monoid
variables (A : Type*) [add_zero_class A]
/-- The monoid of endomorphisms. -/
protected def End := A →+ A
namespace End
instance : monoid (add_monoid.End A) :=
{ mul := add_monoid_hom.comp,
one := add_monoid_hom.id A,
mul_assoc := λ _ _ _, add_monoid_hom.comp_assoc _ _ _,
mul_one := add_monoid_hom.comp_id,
one_mul := add_monoid_hom.id_comp }
instance : inhabited (add_monoid.End A) := ⟨1⟩
instance : has_coe_to_fun (add_monoid.End A) (λ _, A → A) := ⟨add_monoid_hom.to_fun⟩
end End
@[simp] lemma coe_one : ((1 : add_monoid.End A) : A → A) = id := rfl
@[simp] lemma coe_mul (f g) : ((f * g : add_monoid.End A) : A → A) = f ∘ g := rfl
end add_monoid
end End
/-- `1` is the homomorphism sending all elements to `1`. -/
@[to_additive]
instance [has_one M] [has_one N] : has_one (one_hom M N) := ⟨⟨λ _, 1, rfl⟩⟩
/-- `1` is the multiplicative homomorphism sending all elements to `1`. -/
@[to_additive]
instance [has_mul M] [mul_one_class N] : has_one (mul_hom M N) :=
⟨⟨λ _, 1, λ _ _, (one_mul 1).symm⟩⟩
/-- `1` is the monoid homomorphism sending all elements to `1`. -/
@[to_additive]
instance [mul_one_class M] [mul_one_class N] : has_one (M →* N) :=
⟨⟨λ _, 1, rfl, λ _ _, (one_mul 1).symm⟩⟩
/-- `0` is the homomorphism sending all elements to `0`. -/
add_decl_doc zero_hom.has_zero
/-- `0` is the additive homomorphism sending all elements to `0`. -/
add_decl_doc add_hom.has_zero
/-- `0` is the additive monoid homomorphism sending all elements to `0`. -/
add_decl_doc add_monoid_hom.has_zero
@[simp, to_additive] lemma one_hom.one_apply [has_one M] [has_one N]
(x : M) : (1 : one_hom M N) x = 1 := rfl
@[simp, to_additive] lemma monoid_hom.one_apply [mul_one_class M] [mul_one_class N]
(x : M) : (1 : M →* N) x = 1 := rfl
@[simp, to_additive] lemma one_hom.one_comp [has_one M] [has_one N] [has_one P] (f : one_hom M N) :
(1 : one_hom N P).comp f = 1 := rfl
@[simp, to_additive] lemma one_hom.comp_one [has_one M] [has_one N] [has_one P] (f : one_hom N P) :
f.comp (1 : one_hom M N) = 1 :=
by { ext, simp only [one_hom.map_one, one_hom.coe_comp, function.comp_app, one_hom.one_apply] }
@[to_additive]
instance [has_one M] [has_one N] : inhabited (one_hom M N) := ⟨1⟩
@[to_additive]
instance [has_mul M] [mul_one_class N] : inhabited (mul_hom M N) := ⟨1⟩
@[to_additive]
instance [mul_one_class M] [mul_one_class N] : inhabited (M →* N) := ⟨1⟩
-- unlike the other homs, `monoid_with_zero_hom` does not have a `1` or `0`
instance [mul_zero_one_class M] : inhabited (monoid_with_zero_hom M M) :=
⟨monoid_with_zero_hom.id M⟩
namespace monoid_hom
variables [mM : mul_one_class M] [mN : mul_one_class N] [mP : mul_one_class P]
variables [group G] [comm_group H]
/-- Given two monoid morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid morphism
sending `x` to `f x * g x`. -/
@[to_additive]
instance {M N} {mM : mul_one_class M} [comm_monoid N] : has_mul (M →* N) :=
⟨λ f g,
{ to_fun := λ m, f m * g m,
map_one' := show f 1 * g 1 = 1, by simp,
map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y),
rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end }⟩
/-- Given two additive monoid morphisms `f`, `g` to an additive commutative monoid, `f + g` is the
additive monoid morphism sending `x` to `f x + g x`. -/
add_decl_doc add_monoid_hom.has_add
@[simp, to_additive] lemma mul_apply {M N} {mM : mul_one_class M} {mN : comm_monoid N}
(f g : M →* N) (x : M) :
(f * g) x = f x * g x := rfl
@[simp, to_additive] lemma one_comp [mul_one_class M] [mul_one_class N] [mul_one_class P]
(f : M →* N) : (1 : N →* P).comp f = 1 := rfl
@[simp, to_additive] lemma comp_one [mul_one_class M] [mul_one_class N] [mul_one_class P]
(f : N →* P) : f.comp (1 : M →* N) = 1 :=
by { ext, simp only [map_one, coe_comp, function.comp_app, one_apply] }
@[to_additive] lemma mul_comp [mul_one_class M] [comm_monoid N] [comm_monoid P]
(g₁ g₂ : N →* P) (f : M →* N) :
(g₁ * g₂).comp f = g₁.comp f * g₂.comp f := rfl
@[to_additive] lemma comp_mul [mul_one_class M] [comm_monoid N] [comm_monoid P]
(g : N →* P) (f₁ f₂ : M →* N) :
g.comp (f₁ * f₂) = g.comp f₁ * g.comp f₂ :=
by { ext, simp only [mul_apply, function.comp_app, map_mul, coe_comp] }
/-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/
@[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`,
then they are equal at `-x`." ]
lemma eq_on_inv {G} [group G] [monoid M] {f g : G →* M} {x : G} (h : f x = g x) :
f x⁻¹ = g x⁻¹ :=
left_inv_eq_right_inv (f.map_mul_eq_one $ inv_mul_self x) $
h.symm ▸ g.map_mul_eq_one $ mul_inv_self x
/-- Group homomorphisms preserve inverse. -/
@[simp, to_additive]
theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ :=
eq_inv_of_mul_eq_one $ f.map_mul_eq_one $ inv_mul_self g
/-- Group homomorphisms preserve integer power. -/
@[simp, to_additive /-" Additive group homomorphisms preserve integer scaling. "-/]
theorem map_zpow {G H} [group G] [group H] (f : G →* H) (g : G) (n : ℤ) : f (g ^ n) = (f g) ^ n :=
f.map_zpow' f.map_inv g n
/-- Group homomorphisms preserve division. -/
@[simp, to_additive]
theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) :
f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv]
/-- Group homomorphisms preserve division. -/
@[simp, to_additive /-" Additive group homomorphisms preserve subtraction. "-/]
theorem map_div {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g / h) = (f g) / (f h) :=
f.map_div' f.map_inv g h
/-- A homomorphism from a group to a monoid is injective iff its kernel is trivial.
For the iff statement on the triviality of the kernel, see `monoid_hom.injective_iff'`. -/
@[to_additive /-" A homomorphism from an additive group to an additive monoid is injective iff
its kernel is trivial. For the iff statement on the triviality of the kernel,
see `add_monoid_hom.injective_iff'`. "-/]
lemma injective_iff {G H} [group G] [mul_one_class H] (f : G →* H) :
function.injective f ↔ (∀ a, f a = 1 → a = 1) :=
⟨λ h x hfx, h $ hfx.trans f.map_one.symm,
λ h x y hxy, mul_inv_eq_one.1 $ h _ $ by rw [f.map_mul, hxy, ← f.map_mul, mul_inv_self, f.map_one]⟩
/-- A homomorphism from a group to a monoid is injective iff its kernel is trivial,
stated as an iff on the triviality of the kernel.
For the implication, see `monoid_hom.injective_iff`. -/
@[to_additive /-" A homomorphism from an additive group to an additive monoid is injective iff
its kernel is trivial, stated as an iff on the triviality of the kernel. For the implication, see
`add_monoid_hom.injective_iff`. "-/]
lemma injective_iff' {G H} [group G] [mul_one_class H] (f : G →* H) :
function.injective f ↔ (∀ a, f a = 1 ↔ a = 1) :=
f.injective_iff.trans $ forall_congr $ λ a, ⟨λ h, ⟨h, λ H, H.symm ▸ f.map_one⟩, iff.mp⟩
include mM
/-- Makes a group homomorphism from a proof that the map preserves multiplication. -/
@[to_additive "Makes an additive group homomorphism from a proof that the map preserves addition.",
simps {fully_applied := ff}]
def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G :=
{ to_fun := f,
map_mul' := map_mul,
map_one' := mul_left_eq_self.1 $ by rw [←map_mul, mul_one] }
omit mM
/-- Makes a group homomorphism from a proof that the map preserves right division `λ x y, x * y⁻¹`.
See also `monoid_hom.of_map_div` for a version using `λ x y, x / y`.
-/
@[to_additive "Makes an additive group homomorphism from a proof that the map preserves
the operation `λ a b, a + -b`. See also `add_monoid_hom.of_map_sub` for a version using
`λ a b, a - b`."]
def of_map_mul_inv {H : Type*} [group H] (f : G → H)
(map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) :
G →* H :=
mk' f $ λ x y,
calc f (x * y) = f x * (f $ 1 * 1⁻¹ * y⁻¹)⁻¹ : by simp only [one_mul, one_inv, ← map_div, inv_inv]
... = f x * f y : by { simp only [map_div], simp only [mul_right_inv, one_mul, inv_inv] }
@[simp, to_additive] lemma coe_of_map_mul_inv {H : Type*} [group H] (f : G → H)
(map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) :
⇑(of_map_mul_inv f map_div) = f :=
rfl
/-- Define a morphism of additive groups given a map which respects ratios. -/
@[to_additive /-"Define a morphism of additive groups given a map which respects difference."-/]
def of_map_div {H : Type*} [group H] (f : G → H) (hf : ∀ x y, f (x / y) = f x / f y) : G →* H :=
of_map_mul_inv f (by simpa only [div_eq_mul_inv] using hf)
@[simp, to_additive]
lemma coe_of_map_div {H : Type*} [group H] (f : G → H) (hf : ∀ x y, f (x / y) = f x / f y) :
⇑(of_map_div f hf) = f :=
rfl
/-- If `f` is a monoid homomorphism to a commutative group, then `f⁻¹` is the homomorphism sending
`x` to `(f x)⁻¹`. -/
@[to_additive]
instance {M G} [mul_one_class M] [comm_group G] : has_inv (M →* G) :=
⟨λ f, mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul]⟩
/-- If `f` is an additive monoid homomorphism to an additive commutative group, then `-f` is the
homomorphism sending `x` to `-(f x)`. -/
add_decl_doc add_monoid_hom.has_neg
@[simp, to_additive] lemma inv_apply {M G} {mM : mul_one_class M} {gG : comm_group G}
(f : M →* G) (x : M) :
f⁻¹ x = (f x)⁻¹ := rfl
@[simp, to_additive] lemma inv_comp {M N A} {mM : mul_one_class M} {gN : mul_one_class N}
{gA : comm_group A} (φ : N →* A) (ψ : M →* N) : φ⁻¹.comp ψ = (φ.comp ψ)⁻¹ :=
by { ext, simp only [function.comp_app, inv_apply, coe_comp] }
@[simp, to_additive] lemma comp_inv {M A B} {mM : mul_one_class M} {mA : comm_group A}
{mB : comm_group B} (φ : A →* B) (ψ : M →* A) : φ.comp ψ⁻¹ = (φ.comp ψ)⁻¹ :=
by { ext, simp only [function.comp_app, inv_apply, map_inv, coe_comp] }
/-- If `f` and `g` are monoid homomorphisms to a commutative group, then `f / g` is the homomorphism
sending `x` to `(f x) / (g x)`. -/
@[to_additive]
instance {M G} [mul_one_class M] [comm_group G] : has_div (M →* G) :=
⟨λ f g, mk' (λ x, f x / g x) $ λ a b,
by simp [div_eq_mul_inv, mul_assoc, mul_left_comm, mul_comm]⟩
/-- If `f` and `g` are monoid homomorphisms to an additive commutative group, then `f - g`
is the homomorphism sending `x` to `(f x) - (g x)`. -/
add_decl_doc add_monoid_hom.has_sub
@[simp, to_additive] lemma div_apply {M G} {mM : mul_one_class M} {gG : comm_group G}
(f g : M →* G) (x : M) :
(f / g) x = f x / g x := rfl
end monoid_hom
section commute
variables [mul_one_class M] [mul_one_class N] {a x y : M}
@[simp, to_additive]
protected lemma semiconj_by.map (h : semiconj_by a x y) (f : M →* N) :
semiconj_by (f a) (f x) (f y) :=
by simpa only [semiconj_by, f.map_mul] using congr_arg f h
@[simp, to_additive]
protected lemma commute.map (h : commute x y) (f : M →* N) : commute (f x) (f y) := h.map f
end commute
|
df9b18d84f8e9c333b939c95fedc1f76d644956b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/algebra/homology/short_exact/preadditive.lean | 5fc1314c04831716447c875f2c5b35244798cd33 | [
"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 | 10,608 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Andrew Yang
-/
import algebra.homology.exact
import category_theory.preadditive.additive_functor
/-!
# Short exact sequences, and splittings.
`short_exact f g` is the proposition that `0 ⟶ A -f⟶ B -g⟶ C ⟶ 0` is an exact sequence.
We define when a short exact sequence is left-split, right-split, and split.
## See also
In `algebra.homology.short_exact.abelian` we show that in an abelian category
a left-split short exact sequences admits a splitting.
-/
noncomputable theory
open category_theory category_theory.limits category_theory.preadditive
variables {𝒜 : Type*} [category 𝒜]
namespace category_theory
variables {A B C A' B' C' : 𝒜} (f : A ⟶ B) (g : B ⟶ C) (f' : A' ⟶ B') (g' : B' ⟶ C')
section has_zero_morphisms
variables [has_zero_morphisms 𝒜] [has_kernels 𝒜] [has_images 𝒜]
/-- If `f : A ⟶ B` and `g : B ⟶ C` then `short_exact f g` is the proposition saying
the resulting diagram `0 ⟶ A ⟶ B ⟶ C ⟶ 0` is an exact sequence. -/
structure short_exact : Prop :=
[mono : mono f]
[epi : epi g]
(exact : exact f g)
/-- An exact sequence `A -f⟶ B -g⟶ C` is *left split*
if there exists a morphism `φ : B ⟶ A` such that `f ≫ φ = 𝟙 A` and `g` is epi.
Such a sequence is automatically short exact (i.e., `f` is mono). -/
structure left_split : Prop :=
(left_split : ∃ φ : B ⟶ A, f ≫ φ = 𝟙 A)
[epi : epi g]
(exact : exact f g)
lemma left_split.short_exact {f : A ⟶ B} {g : B ⟶ C} (h : left_split f g) : short_exact f g :=
{ mono :=
begin
obtain ⟨φ, hφ⟩ := h.left_split,
haveI : mono (f ≫ φ) := by { rw hφ, apply_instance },
exact mono_of_mono f φ,
end,
epi := h.epi,
exact := h.exact }
/-- An exact sequence `A -f⟶ B -g⟶ C` is *right split*
if there exists a morphism `φ : C ⟶ B` such that `f ≫ φ = 𝟙 A` and `f` is mono.
Such a sequence is automatically short exact (i.e., `g` is epi). -/
structure right_split : Prop :=
(right_split : ∃ χ : C ⟶ B, χ ≫ g = 𝟙 C)
[mono : mono f]
(exact : exact f g)
lemma right_split.short_exact {f : A ⟶ B} {g : B ⟶ C} (h : right_split f g) : short_exact f g :=
{ epi :=
begin
obtain ⟨χ, hχ⟩ := h.right_split,
haveI : epi (χ ≫ g) := by { rw hχ, apply_instance },
exact epi_of_epi χ g,
end,
mono := h.mono,
exact := h.exact }
end has_zero_morphisms
section preadditive
variables [preadditive 𝒜]
/-- An exact sequence `A -f⟶ B -g⟶ C` is *split* if there exist
`φ : B ⟶ A` and `χ : C ⟶ B` such that:
* `f ≫ φ = 𝟙 A`
* `χ ≫ g = 𝟙 C`
* `f ≫ g = 0`
* `χ ≫ φ = 0`
* `φ ≫ f + g ≫ χ = 𝟙 B`
Such a sequence is automatically short exact (i.e., `f` is mono and `g` is epi). -/
structure split : Prop :=
(split : ∃ (φ : B ⟶ A) (χ : C ⟶ B),
f ≫ φ = 𝟙 A ∧ χ ≫ g = 𝟙 C ∧ f ≫ g = 0 ∧ χ ≫ φ = 0 ∧ φ ≫ f + g ≫ χ = 𝟙 B)
variables [has_kernels 𝒜] [has_images 𝒜]
lemma exact_of_split {A B C : 𝒜} {f : A ⟶ B} {g : B ⟶ C} {χ : C ⟶ B} {φ : B ⟶ A}
(hfg : f ≫ g = 0) (H : φ ≫ f + g ≫ χ = 𝟙 B) : exact f g :=
{ w := hfg,
epi :=
begin
let ψ : (kernel_subobject g : 𝒜) ⟶ image_subobject f :=
subobject.arrow _ ≫ φ ≫ factor_thru_image_subobject f,
suffices : ψ ≫ image_to_kernel f g hfg = 𝟙 _,
{ convert epi_of_epi ψ _, rw this, apply_instance },
rw ← cancel_mono (subobject.arrow _), swap, { apply_instance },
simp only [image_to_kernel_arrow, image_subobject_arrow_comp, category.id_comp, category.assoc],
calc (kernel_subobject g).arrow ≫ φ ≫ f
= (kernel_subobject g).arrow ≫ 𝟙 B : _
... = (kernel_subobject g).arrow : category.comp_id _,
rw [← H, preadditive.comp_add],
simp only [add_zero, zero_comp, kernel_subobject_arrow_comp_assoc],
end }
section
variables {f g}
lemma split.exact (h : split f g) : exact f g :=
by { obtain ⟨φ, χ, -, -, h1, -, h2⟩ := h, exact exact_of_split h1 h2 }
lemma split.left_split (h : split f g) : left_split f g :=
{ left_split := by { obtain ⟨φ, χ, h1, -⟩ := h, exact ⟨φ, h1⟩, },
epi := begin
obtain ⟨φ, χ, -, h2, -⟩ := h,
have : epi (χ ≫ g), { rw h2, apply_instance },
exactI epi_of_epi χ g,
end,
exact := h.exact }
lemma split.right_split (h : split f g) : right_split f g :=
{ right_split := by { obtain ⟨φ, χ, -, h1, -⟩ := h, exact ⟨χ, h1⟩, },
mono := begin
obtain ⟨φ, χ, h1, -⟩ := h,
have : mono (f ≫ φ), { rw h1, apply_instance },
exactI mono_of_mono f φ,
end,
exact := h.exact }
lemma split.short_exact (h : split f g) : short_exact f g :=
h.left_split.short_exact
end
lemma split.map {𝒜 ℬ : Type*} [category 𝒜] [preadditive 𝒜] [category ℬ] [preadditive ℬ]
(F : 𝒜 ⥤ ℬ) [functor.additive F] {A B C : 𝒜} {f : A ⟶ B} {g : B ⟶ C} (h : split f g) :
split (F.map f) (F.map g) :=
begin
obtain ⟨φ, χ, h1, h2, h3, h4, h5⟩ := h,
refine ⟨⟨F.map φ, F.map χ, _⟩⟩,
simp only [← F.map_comp, ← F.map_id, ← F.map_add, F.map_zero, *, eq_self_iff_true, and_true],
end
/-- The sequence `A ⟶ A ⊞ B ⟶ B` is exact. -/
lemma exact_inl_snd [has_binary_biproducts 𝒜] (A B : 𝒜) :
exact (biprod.inl : A ⟶ A ⊞ B) biprod.snd :=
exact_of_split biprod.inl_snd biprod.total
/-- The sequence `B ⟶ A ⊞ B ⟶ A` is exact. -/
lemma exact_inr_fst [has_binary_biproducts 𝒜] (A B : 𝒜) :
exact (biprod.inr : B ⟶ A ⊞ B) biprod.fst :=
exact_of_split biprod.inr_fst ((add_comm _ _).trans biprod.total)
end preadditive
/-- A *splitting* of a sequence `A -f⟶ B -g⟶ C` is an isomorphism
to the short exact sequence `0 ⟶ A ⟶ A ⊞ C ⟶ C ⟶ 0` such that
the vertical maps on the left and the right are the identity. -/
@[nolint has_inhabited_instance]
structure splitting [has_zero_morphisms 𝒜] [has_binary_biproducts 𝒜] :=
(iso : B ≅ A ⊞ C)
(comp_iso_eq_inl : f ≫ iso.hom = biprod.inl)
(iso_comp_snd_eq : iso.hom ≫ biprod.snd = g)
variables {f g}
namespace splitting
section has_zero_morphisms
variables [has_zero_morphisms 𝒜] [has_binary_biproducts 𝒜]
attribute [simp, reassoc] comp_iso_eq_inl iso_comp_snd_eq
variables (h : splitting f g)
@[simp, reassoc] lemma inl_comp_iso_eq : biprod.inl ≫ h.iso.inv = f :=
by rw [iso.comp_inv_eq, h.comp_iso_eq_inl]
@[simp, reassoc] lemma iso_comp_eq_snd : h.iso.inv ≫ g = biprod.snd :=
by rw [iso.inv_comp_eq, h.iso_comp_snd_eq]
/-- If `h` is a splitting of `A -f⟶ B -g⟶ C`,
then `h.section : C ⟶ B` is the morphism satisfying `h.section ≫ g = 𝟙 C`. -/
def _root_.category_theory.splitting.section : C ⟶ B := biprod.inr ≫ h.iso.inv
/-- If `h` is a splitting of `A -f⟶ B -g⟶ C`,
then `h.retraction : B ⟶ A` is the morphism satisfying `f ≫ h.retraction = 𝟙 A`. -/
def retraction : B ⟶ A := h.iso.hom ≫ biprod.fst
@[simp, reassoc] lemma section_π : h.section ≫ g = 𝟙 C := by { delta splitting.section, simp }
@[simp, reassoc] lemma ι_retraction : f ≫ h.retraction = 𝟙 A := by { delta retraction, simp }
@[simp, reassoc] lemma section_retraction : h.section ≫ h.retraction = 0 :=
by { delta splitting.section retraction, simp }
/-- The retraction in a splitting is a split mono. -/
protected def split_mono : split_mono f := ⟨h.retraction, by simp⟩
/-- The section in a splitting is a split epi. -/
protected def split_epi : split_epi g := ⟨h.section, by simp⟩
@[simp, reassoc] lemma inr_iso_inv : biprod.inr ≫ h.iso.inv = h.section := rfl
@[simp, reassoc] lemma iso_hom_fst : h.iso.hom ≫ biprod.fst = h.retraction := rfl
/-- A short exact sequence of the form `X -f⟶ Y -0⟶ Z` where `f` is an iso and `Z` is zero
has a splitting. -/
def splitting_of_is_iso_zero {X Y Z : 𝒜} (f : X ⟶ Y) [is_iso f] (hZ : is_zero Z) :
splitting f (0 : Y ⟶ Z) :=
⟨(as_iso f).symm ≪≫ iso_biprod_zero hZ, by simp [hZ.eq_of_tgt _ 0], by simp⟩
include h
protected lemma mono : mono f :=
begin
apply mono_of_mono _ h.retraction,
rw h.ι_retraction,
apply_instance
end
protected lemma epi : epi g :=
begin
apply_with (epi_of_epi h.section) { instances := ff },
rw h.section_π,
apply_instance
end
instance : mono h.section :=
by { delta splitting.section, apply_instance }
instance : epi h.retraction :=
by { delta retraction, apply epi_comp }
end has_zero_morphisms
section preadditive
variables [preadditive 𝒜] [has_binary_biproducts 𝒜]
variables (h : splitting f g)
lemma split_add : h.retraction ≫ f + g ≫ h.section = 𝟙 _ :=
begin
delta splitting.section retraction,
rw [← cancel_mono h.iso.hom, ← cancel_epi h.iso.inv],
simp only [category.comp_id, category.id_comp, category.assoc,
iso.inv_hom_id_assoc, iso.inv_hom_id, limits.biprod.total,
preadditive.comp_add, preadditive.add_comp,
splitting.comp_iso_eq_inl, splitting.iso_comp_eq_snd_assoc]
end
@[reassoc]
lemma retraction_ι_eq_id_sub :
h.retraction ≫ f = 𝟙 _ - g ≫ h.section :=
eq_sub_iff_add_eq.mpr h.split_add
@[reassoc]
lemma π_section_eq_id_sub :
g ≫ h.section = 𝟙 _ - h.retraction ≫ f :=
eq_sub_iff_add_eq.mpr ((add_comm _ _).trans h.split_add)
lemma splittings_comm (h h' : splitting f g) :
h'.section ≫ h.retraction = - h.section ≫ h'.retraction :=
begin
haveI := h.mono,
rw ← cancel_mono f,
simp [retraction_ι_eq_id_sub],
end
include h
lemma split : split f g :=
begin
let φ := h.iso.hom ≫ biprod.fst,
let χ := biprod.inr ≫ h.iso.inv,
refine ⟨⟨h.retraction, h.section, h.ι_retraction, h.section_π, _,
h.section_retraction, h.split_add⟩⟩,
rw [← h.inl_comp_iso_eq, category.assoc, h.iso_comp_eq_snd, biprod.inl_snd],
end
@[reassoc] lemma comp_eq_zero : f ≫ g = 0 :=
h.split.1.some_spec.some_spec.2.2.1
variables [has_kernels 𝒜] [has_images 𝒜] [has_zero_object 𝒜] [has_cokernels 𝒜]
protected lemma exact : exact f g :=
begin
rw exact_iff_exact_of_iso f g (biprod.inl : A ⟶ A ⊞ C) (biprod.snd : A ⊞ C ⟶ C) _ _ _,
{ exact exact_inl_snd _ _ },
{ refine arrow.iso_mk (iso.refl _) h.iso _,
simp only [iso.refl_hom, arrow.mk_hom, category.id_comp, comp_iso_eq_inl], },
{ refine arrow.iso_mk h.iso (iso.refl _) _,
dsimp, simp, },
{ refl }
end
protected
lemma short_exact : short_exact f g :=
{ mono := h.mono, epi := h.epi, exact := h.exact }
end preadditive
end splitting
end category_theory
|
3041a2a980843c752072e8fc0e5233f8f3624911 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/ring_theory/unique_factorization_domain.lean | 752b80f6fb62532644ced3c75771321dd5e40afd | [
"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 | 69,279 | 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, Jens Wagemaker, Aaron Anderson
-/
import algebra.big_operators.associated
import algebra.gcd_monoid.basic
import data.finsupp.multiset
import ring_theory.noetherian
import ring_theory.multiplicity
/-!
# Unique factorization
## Main Definitions
* `wf_dvd_monoid` holds for `monoid`s for which a strict divisibility relation is
well-founded.
* `unique_factorization_monoid` holds for `wf_dvd_monoid`s where
`irreducible` is equivalent to `prime`
## To do
* set up the complete lattice structure on `factor_set`.
-/
variables {α : Type*}
local infix ` ~ᵤ ` : 50 := associated
/-- Well-foundedness of the strict version of |, which is equivalent to the descending chain
condition on divisibility and to the ascending chain condition on
principal ideals in an integral domain.
-/
class wf_dvd_monoid (α : Type*) [comm_monoid_with_zero α] : Prop :=
(well_founded_dvd_not_unit : well_founded (@dvd_not_unit α _))
export wf_dvd_monoid (well_founded_dvd_not_unit)
@[priority 100] -- see Note [lower instance priority]
instance is_noetherian_ring.wf_dvd_monoid [comm_ring α] [is_domain α] [is_noetherian_ring α] :
wf_dvd_monoid α :=
⟨by { convert inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _),
ext,
exact ideal.span_singleton_lt_span_singleton.symm }⟩
namespace wf_dvd_monoid
variables [comm_monoid_with_zero α]
open associates nat
theorem of_wf_dvd_monoid_associates (h : wf_dvd_monoid (associates α)): wf_dvd_monoid α :=
⟨begin
haveI := h,
refine (surjective.well_founded_iff mk_surjective _).2 well_founded_dvd_not_unit,
intros, rw mk_dvd_not_unit_mk_iff
end⟩
variables [wf_dvd_monoid α]
instance wf_dvd_monoid_associates : wf_dvd_monoid (associates α) :=
⟨begin
refine (surjective.well_founded_iff mk_surjective _).1 well_founded_dvd_not_unit,
intros, rw mk_dvd_not_unit_mk_iff
end⟩
theorem well_founded_associates : well_founded ((<) : associates α → associates α → Prop) :=
subrelation.wf (λ x y, dvd_not_unit_of_lt) well_founded_dvd_not_unit
local attribute [elab_as_eliminator] well_founded.fix
lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) :
∃ i, irreducible i ∧ i ∣ a :=
let ⟨b, hs, hr⟩ := well_founded_dvd_not_unit.has_min {b | b ∣ a ∧ ¬ is_unit b} ⟨a, dvd_rfl, ha⟩ in
⟨b, ⟨hs.2, λ c d he, let h := dvd_trans ⟨d, he⟩ hs.1 in or_iff_not_imp_left.2 $
λ hc, of_not_not $ λ hd, hr c ⟨h, hc⟩ ⟨ne_zero_of_dvd_ne_zero ha0 h, d, hd, he⟩⟩, hs.1⟩
@[elab_as_eliminator] lemma induction_on_irreducible {P : α → Prop} (a : α)
(h0 : P 0) (hu : ∀ u : α, is_unit u → P u)
(hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) :
P a :=
by haveI := classical.dec; exact
well_founded_dvd_not_unit.fix
(λ a ih, if ha0 : a = 0 then ha0.substr h0
else if hau : is_unit a then hu a hau
else let ⟨i, hii, b, hb⟩ := exists_irreducible_factor hau ha0,
hb0 : b ≠ 0 := ne_zero_of_dvd_ne_zero ha0 ⟨i, mul_comm i b ▸ hb⟩ in
hb.symm ▸ hi b i hb0 hii $ ih b ⟨hb0, i, hii.1, mul_comm i b ▸ hb⟩)
a
lemma exists_factors (a : α) : a ≠ 0 →
∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ associated f.prod a :=
induction_on_irreducible a
(λ h, (h rfl).elim)
(λ u hu _, ⟨0, λ _ h, h.elim, hu.unit, one_mul _⟩)
(λ a i ha0 hi ih _,
let ⟨s, hs⟩ := ih ha0 in
⟨i ::ₘ s, λ b H, (multiset.mem_cons.1 H).elim (λ h, h.symm ▸ hi) (hs.1 b),
by { rw s.prod_cons i, exact hs.2.mul_left i }⟩)
lemma not_unit_iff_exists_factors_eq (a : α) (hn0 : a ≠ 0) :
¬ is_unit a ↔ ∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ f.prod = a ∧ f ≠ ∅ :=
⟨λ hnu, begin
obtain ⟨f, hi, u, rfl⟩ := exists_factors a hn0,
obtain ⟨b, h⟩ := multiset.exists_mem_of_ne_zero (λ h : f = 0, hnu $ by simp [h]),
classical, refine ⟨(f.erase b).cons (b * u), λ a ha, _, _, multiset.cons_ne_zero⟩,
{ obtain (rfl|ha) := multiset.mem_cons.1 ha,
exacts [associated.irreducible ⟨u,rfl⟩ (hi b h), hi a (multiset.mem_of_mem_erase ha)] },
{ rw [multiset.prod_cons, mul_comm b, mul_assoc, multiset.prod_erase h, mul_comm] },
end,
λ ⟨f, hi, he, hne⟩, let ⟨b, h⟩ := multiset.exists_mem_of_ne_zero hne in
not_is_unit_of_not_is_unit_dvd (hi b h).not_unit $ he ▸ multiset.dvd_prod h⟩
end wf_dvd_monoid
theorem wf_dvd_monoid.of_well_founded_associates [cancel_comm_monoid_with_zero α]
(h : well_founded ((<) : associates α → associates α → Prop)) : wf_dvd_monoid α :=
wf_dvd_monoid.of_wf_dvd_monoid_associates
⟨by { convert h, ext, exact associates.dvd_not_unit_iff_lt }⟩
theorem wf_dvd_monoid.iff_well_founded_associates [cancel_comm_monoid_with_zero α] :
wf_dvd_monoid α ↔ well_founded ((<) : associates α → associates α → Prop) :=
⟨by apply wf_dvd_monoid.well_founded_associates, wf_dvd_monoid.of_well_founded_associates⟩
section prio
set_option default_priority 100 -- see Note [default priority]
/-- unique factorization monoids.
These are defined as `cancel_comm_monoid_with_zero`s with well-founded strict divisibility
relations, but this is equivalent to more familiar definitions:
Each element (except zero) is uniquely represented as a multiset of irreducible factors.
Uniqueness is only up to associated elements.
Each element (except zero) is non-uniquely represented as a multiset
of prime factors.
To define a UFD using the definition in terms of multisets
of irreducible factors, use the definition `of_exists_unique_irreducible_factors`
To define a UFD using the definition in terms of multisets
of prime factors, use the definition `of_exists_prime_factors`
-/
class unique_factorization_monoid (α : Type*) [cancel_comm_monoid_with_zero α]
extends wf_dvd_monoid α : Prop :=
(irreducible_iff_prime : ∀ {a : α}, irreducible a ↔ prime a)
/-- Can't be an instance because it would cause a loop `ufm → wf_dvd_monoid → ufm → ...`. -/
@[reducible] lemma ufm_of_gcd_of_wf_dvd_monoid [cancel_comm_monoid_with_zero α]
[wf_dvd_monoid α] [gcd_monoid α] : unique_factorization_monoid α :=
{ irreducible_iff_prime := λ _, gcd_monoid.irreducible_iff_prime
.. ‹wf_dvd_monoid α› }
instance associates.ufm [cancel_comm_monoid_with_zero α]
[unique_factorization_monoid α] : unique_factorization_monoid (associates α) :=
{ irreducible_iff_prime := by { rw ← associates.irreducible_iff_prime_iff,
apply unique_factorization_monoid.irreducible_iff_prime, }
.. (wf_dvd_monoid.wf_dvd_monoid_associates : wf_dvd_monoid (associates α)) }
end prio
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
theorem exists_prime_factors (a : α) : a ≠ 0 →
∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a :=
by { simp_rw ← unique_factorization_monoid.irreducible_iff_prime,
apply wf_dvd_monoid.exists_factors a }
@[elab_as_eliminator] lemma induction_on_prime {P : α → Prop}
(a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x)
(h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a :=
begin
simp_rw ← unique_factorization_monoid.irreducible_iff_prime at h₃,
exact wf_dvd_monoid.induction_on_irreducible a h₁ h₂ h₃,
end
end unique_factorization_monoid
lemma prime_factors_unique [cancel_comm_monoid_with_zero α] : ∀ {f g : multiset α},
(∀ x ∈ f, prime x) → (∀ x ∈ g, prime x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g :=
by haveI := classical.dec_eq α; exact
λ f, multiset.induction_on f
(λ g _ hg h,
multiset.rel_zero_left.2 $
multiset.eq_zero_of_forall_not_mem $ λ x hx,
have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm,
(hg x hx).not_unit $ is_unit_iff_dvd_one.2 $
(multiset.dvd_prod hx).trans (is_unit_iff_dvd_one.1 this))
(λ p f ih g hf hg hfg,
let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod
(hf p (by simp)) (λ q hq, hg _ hq) $
hfg.dvd_iff_dvd_right.1
(show p ∣ (p ::ₘ f).prod, by simp) in
begin
rw ← multiset.cons_erase hbg,
exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq]))
(λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq))
(associated.of_mul_left
(by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb
(hf p (by simp)).ne_zero)),
end)
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
lemma factors_unique {f g : multiset α} (hf : ∀ x ∈ f, irreducible x) (hg : ∀ x ∈ g, irreducible x)
(h : f.prod ~ᵤ g.prod) : multiset.rel associated f g :=
prime_factors_unique
(λ x hx, irreducible_iff_prime.mp (hf x hx))
(λ x hx, irreducible_iff_prime.mp (hg x hx))
h
end unique_factorization_monoid
/-- If an irreducible has a prime factorization,
then it is an associate of one of its prime factors. -/
lemma prime_factors_irreducible [cancel_comm_monoid_with_zero α] {a : α} {f : multiset α}
(ha : irreducible a) (pfa : (∀ b ∈ f, prime b) ∧ f.prod ~ᵤ a) :
∃ p, a ~ᵤ p ∧ f = {p} :=
begin
haveI := classical.dec_eq α,
refine multiset.induction_on f (λ h, (ha.not_unit
(associated_one_iff_is_unit.1 (associated.symm h))).elim) _ pfa.2 pfa.1,
rintros p s _ ⟨u, hu⟩ hs,
use p,
have hs0 : s = 0,
{ by_contra hs0,
obtain ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0,
apply (hs q (by simp [hq])).2.1,
refine (ha.is_unit_or_is_unit (_ : _ = ((p * ↑u) * (s.erase q).prod) * _)).resolve_left _,
{ rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons, multiset.cons_erase hq, ← hu,
mul_comm, mul_comm p _, mul_assoc],
simp, },
apply mt is_unit_of_mul_is_unit_left (mt is_unit_of_mul_is_unit_left _),
apply (hs p (multiset.mem_cons_self _ _)).2.1 },
simp only [mul_one, multiset.prod_cons, multiset.prod_zero, hs0] at *,
exact ⟨associated.symm ⟨u, hu⟩, rfl⟩,
end
section exists_prime_factors
variables [cancel_comm_monoid_with_zero α]
variables (pf : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a)
include pf
lemma wf_dvd_monoid.of_exists_prime_factors : wf_dvd_monoid α :=
⟨begin
classical,
refine rel_hom_class.well_founded
(rel_hom.mk _ _ : (dvd_not_unit : α → α → Prop) →r ((<) : with_top ℕ → with_top ℕ → Prop))
(with_top.well_founded_lt nat.lt_wf),
{ intro a,
by_cases h : a = 0, { exact ⊤ },
exact (classical.some (pf a h)).card },
rintros a b ⟨ane0, ⟨c, hc, b_eq⟩⟩,
rw dif_neg ane0,
by_cases h : b = 0, { simp [h, lt_top_iff_ne_top] },
rw [dif_neg h, with_top.coe_lt_coe],
have cne0 : c ≠ 0, { refine mt (λ con, _) h, rw [b_eq, con, mul_zero] },
calc multiset.card (classical.some (pf a ane0))
< _ + multiset.card (classical.some (pf c cne0)) :
lt_add_of_pos_right _ (multiset.card_pos.mpr (λ con, hc (associated_one_iff_is_unit.mp _)))
... = multiset.card (classical.some (pf a ane0) + classical.some (pf c cne0)) :
(multiset.card_add _ _).symm
... = multiset.card (classical.some (pf b h)) :
multiset.card_eq_card_of_rel (prime_factors_unique _ (classical.some_spec (pf _ h)).1 _),
{ convert (classical.some_spec (pf c cne0)).2.symm,
rw [con, multiset.prod_zero] },
{ intros x hadd,
rw multiset.mem_add at hadd,
cases hadd; apply (classical.some_spec (pf _ _)).1 _ hadd },
{ rw multiset.prod_add,
transitivity a * c,
{ apply associated.mul_mul; apply (classical.some_spec (pf _ _)).2 },
{ rw ← b_eq,
apply (classical.some_spec (pf _ _)).2.symm, } }
end⟩
lemma irreducible_iff_prime_of_exists_prime_factors {p : α} : irreducible p ↔ prime p :=
begin
by_cases hp0 : p = 0,
{ simp [hp0] },
refine ⟨λ h, _, prime.irreducible⟩,
obtain ⟨f, hf⟩ := pf p hp0,
obtain ⟨q, hq, rfl⟩ := prime_factors_irreducible h hf,
rw hq.prime_iff,
exact hf.1 q (multiset.mem_singleton_self _)
end
theorem unique_factorization_monoid.of_exists_prime_factors :
unique_factorization_monoid α :=
{ irreducible_iff_prime := λ _, irreducible_iff_prime_of_exists_prime_factors pf,
.. wf_dvd_monoid.of_exists_prime_factors pf }
end exists_prime_factors
theorem unique_factorization_monoid.iff_exists_prime_factors [cancel_comm_monoid_with_zero α] :
unique_factorization_monoid α ↔
(∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) :=
⟨λ h, @unique_factorization_monoid.exists_prime_factors _ _ h,
unique_factorization_monoid.of_exists_prime_factors⟩
section
variables {β : Type*} [cancel_comm_monoid_with_zero α] [cancel_comm_monoid_with_zero β]
lemma mul_equiv.unique_factorization_monoid (e : α ≃* β)
(hα : unique_factorization_monoid α) : unique_factorization_monoid β :=
begin
rw unique_factorization_monoid.iff_exists_prime_factors at hα ⊢, intros a ha,
obtain ⟨w,hp,u,h⟩ := hα (e.symm a) (λ h, ha $ by { convert ← map_zero e, simp [← h] }),
exact ⟨ w.map e,
λ b hb, let ⟨c,hc,he⟩ := multiset.mem_map.1 hb in he ▸ e.prime_iff.1 (hp c hc),
units.map e.to_monoid_hom u,
by { erw [multiset.prod_hom, ← e.map_mul, h], simp } ⟩,
end
lemma mul_equiv.unique_factorization_monoid_iff (e : α ≃* β) :
unique_factorization_monoid α ↔ unique_factorization_monoid β :=
⟨ e.unique_factorization_monoid, e.symm.unique_factorization_monoid ⟩
end
theorem irreducible_iff_prime_of_exists_unique_irreducible_factors [cancel_comm_monoid_with_zero α]
(eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a)
(uif : ∀ (f g : multiset α),
(∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g)
(p : α) : irreducible p ↔ prime p :=
⟨by letI := classical.dec_eq α; exact λ hpi,
⟨hpi.ne_zero, hpi.1,
λ a b ⟨x, hx⟩,
if hab0 : a * b = 0
then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim
(λ ha0, by simp [ha0])
(λ hb0, by simp [hb0])
else
have hx0 : x ≠ 0, from λ hx0, by simp * at *,
have ha0 : a ≠ 0, from left_ne_zero_of_mul hab0,
have hb0 : b ≠ 0, from right_ne_zero_of_mul hab0,
begin
cases eif x hx0 with fx hfx,
cases eif a ha0 with fa hfa,
cases eif b hb0 with fb hfb,
have h : multiset.rel associated (p ::ₘ fx) (fa + fb),
{ apply uif,
{ exact λ i hi, (multiset.mem_cons.1 hi).elim (λ hip, hip.symm ▸ hpi) (hfx.1 _), },
{ exact λ i hi, (multiset.mem_add.1 hi).elim (hfa.1 _) (hfb.1 _), },
calc multiset.prod (p ::ₘ fx)
~ᵤ a * b : by rw [hx, multiset.prod_cons];
exact hfx.2.mul_left _
... ~ᵤ (fa).prod * (fb).prod :
hfa.2.symm.mul_mul hfb.2.symm
... = _ : by rw multiset.prod_add, },
exact let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem h
(multiset.mem_cons_self p _) in
(multiset.mem_add.1 hqf).elim
(λ hqa, or.inl $ hq.dvd_iff_dvd_left.2 $
hfa.2.dvd_iff_dvd_right.1
(multiset.dvd_prod hqa))
(λ hqb, or.inr $ hq.dvd_iff_dvd_left.2 $
hfb.2.dvd_iff_dvd_right.1
(multiset.dvd_prod hqb))
end⟩, prime.irreducible⟩
theorem unique_factorization_monoid.of_exists_unique_irreducible_factors
[cancel_comm_monoid_with_zero α]
(eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a)
(uif : ∀ (f g : multiset α),
(∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod →
multiset.rel associated f g) :
unique_factorization_monoid α :=
unique_factorization_monoid.of_exists_prime_factors (by
{ convert eif,
simp_rw irreducible_iff_prime_of_exists_unique_irreducible_factors eif uif })
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [decidable_eq α]
variables [unique_factorization_monoid α]
/-- Noncomputably determines the multiset of prime factors. -/
noncomputable def factors (a : α) : multiset α := if h : a = 0 then 0 else
classical.some (unique_factorization_monoid.exists_prime_factors a h)
theorem factors_prod {a : α} (ane0 : a ≠ 0) : associated (factors a).prod a :=
begin
rw [factors, dif_neg ane0],
exact (classical.some_spec (exists_prime_factors a ane0)).2
end
lemma ne_zero_of_mem_factors {p a : α} (h : p ∈ factors a) : a ≠ 0 :=
begin
intro ha,
rw [factors, dif_pos ha] at h,
exact multiset.not_mem_zero _ h
end
lemma dvd_of_mem_factors {p a : α} (h : p ∈ factors a) : p ∣ a :=
dvd_trans (multiset.dvd_prod h) (associated.dvd (factors_prod (ne_zero_of_mem_factors h)))
theorem prime_of_factor {a : α} (x : α) (hx : x ∈ factors a) : prime x :=
begin
have ane0 := ne_zero_of_mem_factors hx,
rw [factors, dif_neg ane0] at hx,
exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 x hx,
end
theorem irreducible_of_factor {a : α} : ∀ (x : α), x ∈ factors a → irreducible x :=
λ x h, (prime_of_factor x h).irreducible
@[simp] lemma factors_zero : factors (0 : α) = 0 :=
by simp [factors]
@[simp] lemma factors_one : factors (1 : α) = 0 :=
begin
nontriviality α using [factors],
rw ← multiset.rel_zero_right,
refine factors_unique irreducible_of_factor (λ x hx, (multiset.not_mem_zero x hx).elim) _,
rw multiset.prod_zero,
exact factors_prod one_ne_zero,
end
lemma exists_mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a →
∃ q ∈ factors a, p ~ᵤ q :=
λ ⟨b, hb⟩,
have hb0 : b ≠ 0, from λ hb0, by simp * at *,
have multiset.rel associated (p ::ₘ factors b) (factors a),
from factors_unique
(λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_factor _))
irreducible_of_factor
(associated.symm $ calc multiset.prod (factors a) ~ᵤ a : factors_prod ha0
... = p * b : hb
... ~ᵤ multiset.prod (p ::ₘ factors b) :
by rw multiset.prod_cons; exact (factors_prod hb0).symm.mul_left _),
multiset.exists_mem_of_rel_of_mem this (by simp)
lemma factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
multiset.rel associated (factors (x * y)) (factors x + factors y) :=
begin
refine factors_unique irreducible_of_factor
(λ a ha, (multiset.mem_add.mp ha).by_cases (irreducible_of_factor _) (irreducible_of_factor _))
((factors_prod (mul_ne_zero hx hy)).trans _),
rw multiset.prod_add,
exact (associated.mul_mul (factors_prod hx) (factors_prod hy)).symm,
end
lemma factors_pow {x : α} (n : ℕ) :
multiset.rel associated (factors (x ^ n)) (n • factors x) :=
begin
induction n with n ih,
{ simp },
by_cases h0 : x = 0,
{ simp [h0, zero_pow n.succ_pos, smul_zero] },
rw [pow_succ, succ_nsmul],
refine multiset.rel.trans _ (factors_mul h0 (pow_ne_zero n h0)) _,
refine multiset.rel.add _ ih,
exact multiset.rel_refl_of_refl_on (λ y hy, associated.refl _),
end
end unique_factorization_monoid
namespace unique_factorization_monoid
variables [cancel_comm_monoid_with_zero α] [decidable_eq α] [normalization_monoid α]
variables [unique_factorization_monoid α]
/-- Noncomputably determines the multiset of prime factors. -/
noncomputable def normalized_factors (a : α) : multiset α :=
multiset.map normalize $ factors a
/-- An arbitrary choice of factors of `x : M` is exactly the (unique) normalized set of factors,
if `M` has a trivial group of units. -/
@[simp] lemma factors_eq_normalized_factors {M : Type*} [cancel_comm_monoid_with_zero M]
[decidable_eq M] [unique_factorization_monoid M] [unique (Mˣ)] (x : M) :
factors x = normalized_factors x :=
begin
unfold normalized_factors,
convert (multiset.map_id (factors x)).symm,
ext p,
exact normalize_eq p
end
theorem normalized_factors_prod {a : α} (ane0 : a ≠ 0) : associated (normalized_factors a).prod a :=
begin
rw [normalized_factors, factors, dif_neg ane0],
refine associated.trans _ (classical.some_spec (exists_prime_factors a ane0)).2,
rw [← associates.mk_eq_mk_iff_associated, ← associates.prod_mk, ← associates.prod_mk,
multiset.map_map],
congr' 2,
ext,
rw [function.comp_apply, associates.mk_normalize],
end
theorem prime_of_normalized_factor {a : α} : ∀ (x : α), x ∈ normalized_factors a → prime x :=
begin
rw [normalized_factors, factors],
split_ifs with ane0, { simp },
intros x hx, rcases multiset.mem_map.1 hx with ⟨y, ⟨hy, rfl⟩⟩,
rw (normalize_associated _).prime_iff,
exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 y hy,
end
theorem irreducible_of_normalized_factor {a : α} :
∀ (x : α), x ∈ normalized_factors a → irreducible x :=
λ x h, (prime_of_normalized_factor x h).irreducible
theorem normalize_normalized_factor {a : α} :
∀ (x : α), x ∈ normalized_factors a → normalize x = x :=
begin
rw [normalized_factors, factors],
split_ifs with h, { simp },
intros x hx,
obtain ⟨y, hy, rfl⟩ := multiset.mem_map.1 hx,
apply normalize_idem
end
lemma normalized_factors_irreducible {a : α} (ha : irreducible a) :
normalized_factors a = {normalize a} :=
begin
obtain ⟨p, a_assoc, hp⟩ := prime_factors_irreducible ha
⟨prime_of_normalized_factor, normalized_factors_prod ha.ne_zero⟩,
have p_mem : p ∈ normalized_factors a,
{ rw hp, exact multiset.mem_singleton_self _ },
convert hp,
rwa [← normalize_normalized_factor p p_mem, normalize_eq_normalize_iff, dvd_dvd_iff_associated]
end
lemma exists_mem_normalized_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a →
∃ q ∈ normalized_factors a, p ~ᵤ q :=
λ ⟨b, hb⟩,
have hb0 : b ≠ 0, from λ hb0, by simp * at *,
have multiset.rel associated (p ::ₘ normalized_factors b) (normalized_factors a),
from factors_unique
(λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp)
(irreducible_of_normalized_factor _))
irreducible_of_normalized_factor
(associated.symm $ calc multiset.prod (normalized_factors a) ~ᵤ a : normalized_factors_prod ha0
... = p * b : hb
... ~ᵤ multiset.prod (p ::ₘ normalized_factors b) :
by rw multiset.prod_cons; exact (normalized_factors_prod hb0).symm.mul_left _),
multiset.exists_mem_of_rel_of_mem this (by simp)
@[simp] lemma normalized_factors_zero : normalized_factors (0 : α) = 0 :=
by simp [normalized_factors, factors]
@[simp] lemma normalized_factors_one : normalized_factors (1 : α) = 0 :=
begin
nontriviality α using [normalized_factors, factors],
rw ← multiset.rel_zero_right,
apply factors_unique irreducible_of_normalized_factor,
{ intros x hx,
exfalso,
apply multiset.not_mem_zero x hx },
{ simp [normalized_factors_prod (@one_ne_zero α _ _)] },
apply_instance
end
@[simp] lemma normalized_factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
normalized_factors (x * y) = normalized_factors x + normalized_factors y :=
begin
have h : (normalize : α → α) = associates.out ∘ associates.mk,
{ ext, rw [function.comp_apply, associates.out_mk], },
rw [← multiset.map_id' (normalized_factors (x * y)), ← multiset.map_id' (normalized_factors x),
← multiset.map_id' (normalized_factors y), ← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_congr rfl normalize_normalized_factor,
← multiset.map_add, h, ← multiset.map_map associates.out, eq_comm,
← multiset.map_map associates.out],
refine congr rfl _,
apply multiset.map_mk_eq_map_mk_of_rel,
apply factors_unique,
{ intros x hx,
rcases multiset.mem_add.1 hx with hx | hx;
exact irreducible_of_normalized_factor x hx },
{ exact irreducible_of_normalized_factor },
{ rw multiset.prod_add,
exact ((normalized_factors_prod hx).mul_mul (normalized_factors_prod hy)).trans
(normalized_factors_prod (mul_ne_zero hx hy)).symm }
end
@[simp] lemma normalized_factors_pow {x : α} (n : ℕ) :
normalized_factors (x ^ n) = n • normalized_factors x :=
begin
induction n with n ih,
{ simp },
by_cases h0 : x = 0,
{ simp [h0, zero_pow n.succ_pos, smul_zero] },
rw [pow_succ, succ_nsmul, normalized_factors_mul h0 (pow_ne_zero _ h0), ih],
end
theorem _root_.irreducible.normalized_factors_pow {p : α} (hp : irreducible p) (k : ℕ) :
normalized_factors (p ^ k) = multiset.repeat (normalize p) k :=
by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton]
lemma dvd_iff_normalized_factors_le_normalized_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) :
x ∣ y ↔ normalized_factors x ≤ normalized_factors y :=
begin
split,
{ rintro ⟨c, rfl⟩,
simp [hx, right_ne_zero_of_mul hy] },
{ rw [← (normalized_factors_prod hx).dvd_iff_dvd_left,
← (normalized_factors_prod hy).dvd_iff_dvd_right],
apply multiset.prod_dvd_prod_of_le }
end
theorem normalized_factors_of_irreducible_pow {p : α} (hp : irreducible p) (k : ℕ) :
normalized_factors (p ^ k) = multiset.repeat (normalize p) k :=
by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton]
lemma zero_not_mem_normalized_factors (x : α) : (0 : α) ∉ normalized_factors x :=
λ h, prime.ne_zero (prime_of_normalized_factor _ h) rfl
lemma dvd_of_mem_normalized_factors {a p : α} (H : p ∈ normalized_factors a) : p ∣ a :=
begin
by_cases hcases : a = 0,
{ rw hcases,
exact dvd_zero p },
{ exact dvd_trans (multiset.dvd_prod H) (associated.dvd (normalized_factors_prod hcases)) },
end
lemma exists_associated_prime_pow_of_unique_normalized_factor {p r : α}
(h : ∀ {m}, m ∈ normalized_factors r → m = p) (hr : r ≠ 0) : ∃ (i : ℕ), associated (p ^ i) r :=
begin
use (normalized_factors r).card,
have := unique_factorization_monoid.normalized_factors_prod hr,
rwa [multiset.eq_repeat_of_mem (λ b, h), multiset.prod_repeat] at this
end
end unique_factorization_monoid
namespace unique_factorization_monoid
open_locale classical
open multiset associates
noncomputable theory
variables [cancel_comm_monoid_with_zero α] [nontrivial α] [unique_factorization_monoid α]
/-- Noncomputably defines a `normalization_monoid` structure on a `unique_factorization_monoid`. -/
protected def normalization_monoid : normalization_monoid α :=
normalization_monoid_of_monoid_hom_right_inverse
{ to_fun := λ a : associates α, if a = 0 then 0 else ((normalized_factors a).map
(classical.some mk_surjective.has_right_inverse : associates α → α)).prod,
map_one' := by simp,
map_mul' := λ x y, by
{ by_cases hx : x = 0, { simp [hx] },
by_cases hy : y = 0, { simp [hy] },
simp [hx, hy] } } begin
intro x,
dsimp,
by_cases hx : x = 0, { simp [hx] },
have h : associates.mk_monoid_hom ∘ (classical.some mk_surjective.has_right_inverse) =
(id : associates α → associates α),
{ ext x,
rw [function.comp_apply, mk_monoid_hom_apply,
classical.some_spec mk_surjective.has_right_inverse x],
refl },
rw [if_neg hx, ← mk_monoid_hom_apply, monoid_hom.map_multiset_prod, map_map, h, map_id,
← associated_iff_eq],
apply normalized_factors_prod hx
end
instance : inhabited (normalization_monoid α) := ⟨unique_factorization_monoid.normalization_monoid⟩
end unique_factorization_monoid
namespace unique_factorization_monoid
variables {R : Type*} [cancel_comm_monoid_with_zero R] [unique_factorization_monoid R]
lemma no_factors_of_no_prime_factors {a b : R} (ha : a ≠ 0)
(h : (∀ {d}, d ∣ a → d ∣ b → ¬ prime d)) : ∀ {d}, d ∣ a → d ∣ b → is_unit d :=
λ d, induction_on_prime d
(by { simp only [zero_dvd_iff], intros, contradiction })
(λ x hx _ _, hx)
(λ d q hp hq ih dvd_a dvd_b,
absurd hq (h (dvd_of_mul_right_dvd dvd_a) (dvd_of_mul_right_dvd dvd_b)))
/-- Euclid's lemma: if `a ∣ b * c` and `a` and `c` have no common prime factors, `a ∣ b`.
Compare `is_coprime.dvd_of_dvd_mul_left`. -/
lemma dvd_of_dvd_mul_left_of_no_prime_factors {a b c : R} (ha : a ≠ 0) :
(∀ {d}, d ∣ a → d ∣ c → ¬ prime d) → a ∣ b * c → a ∣ b :=
begin
refine induction_on_prime c _ _ _,
{ intro no_factors,
simp only [dvd_zero, mul_zero, forall_prop_of_true],
haveI := classical.prop_decidable,
exact is_unit_iff_forall_dvd.mp
(no_factors_of_no_prime_factors ha @no_factors (dvd_refl a) (dvd_zero a)) _ },
{ rintros _ ⟨x, rfl⟩ _ a_dvd_bx,
apply units.dvd_mul_right.mp a_dvd_bx },
{ intros c p hc hp ih no_factors a_dvd_bpc,
apply ih (λ q dvd_a dvd_c hq, no_factors dvd_a (dvd_c.mul_left _) hq),
rw mul_left_comm at a_dvd_bpc,
refine or.resolve_left (hp.left_dvd_or_dvd_right_of_dvd_mul a_dvd_bpc) (λ h, _),
exact no_factors h (dvd_mul_right p c) hp }
end
/-- Euclid's lemma: if `a ∣ b * c` and `a` and `b` have no common prime factors, `a ∣ c`.
Compare `is_coprime.dvd_of_dvd_mul_right`. -/
lemma dvd_of_dvd_mul_right_of_no_prime_factors {a b c : R} (ha : a ≠ 0)
(no_factors : ∀ {d}, d ∣ a → d ∣ b → ¬ prime d) : a ∣ b * c → a ∣ c :=
by simpa [mul_comm b c] using dvd_of_dvd_mul_left_of_no_prime_factors ha @no_factors
/-- If `a ≠ 0, b` are elements of a unique factorization domain, then dividing
out their common factor `c'` gives `a'` and `b'` with no factors in common. -/
lemma exists_reduced_factors : ∀ (a ≠ (0 : R)) b,
∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b :=
begin
haveI := classical.prop_decidable,
intros a,
refine induction_on_prime a _ _ _,
{ intros, contradiction },
{ intros a a_unit a_ne_zero b,
use [a, b, 1],
split,
{ intros p p_dvd_a _,
exact is_unit_of_dvd_unit p_dvd_a a_unit },
{ simp } },
{ intros a p a_ne_zero p_prime ih_a pa_ne_zero b,
by_cases p ∣ b,
{ rcases h with ⟨b, rfl⟩,
obtain ⟨a', b', c', no_factor, ha', hb'⟩ := ih_a a_ne_zero b,
refine ⟨a', b', p * c', @no_factor, _, _⟩,
{ rw [mul_assoc, ha'] },
{ rw [mul_assoc, hb'] } },
{ obtain ⟨a', b', c', coprime, rfl, rfl⟩ := ih_a a_ne_zero b,
refine ⟨p * a', b', c', _, mul_left_comm _ _ _, rfl⟩,
intros q q_dvd_pa' q_dvd_b',
cases p_prime.left_dvd_or_dvd_right_of_dvd_mul q_dvd_pa' with p_dvd_q q_dvd_a',
{ have : p ∣ c' * b' := dvd_mul_of_dvd_right (p_dvd_q.trans q_dvd_b') _,
contradiction },
exact coprime q_dvd_a' q_dvd_b' } }
end
lemma exists_reduced_factors' (a b : R) (hb : b ≠ 0) :
∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b :=
let ⟨b', a', c', no_factor, hb, ha⟩ := exists_reduced_factors b hb a
in ⟨a', b', c', λ _ hpb hpa, no_factor hpa hpb, ha, hb⟩
section multiplicity
variables [nontrivial R] [normalization_monoid R] [decidable_eq R]
variables [dec_dvd : decidable_rel (has_dvd.dvd : R → R → Prop)]
open multiplicity multiset
include dec_dvd
lemma le_multiplicity_iff_repeat_le_normalized_factors {a b : R} {n : ℕ}
(ha : irreducible a) (hb : b ≠ 0) :
↑n ≤ multiplicity a b ↔ repeat (normalize a) n ≤ normalized_factors b :=
begin
rw ← pow_dvd_iff_le_multiplicity,
revert b,
induction n with n ih, { simp },
intros b hb,
split,
{ rintro ⟨c, rfl⟩,
rw [ne.def, pow_succ, mul_assoc, mul_eq_zero, decidable.not_or_iff_and_not] at hb,
rw [pow_succ, mul_assoc, normalized_factors_mul hb.1 hb.2, repeat_succ,
normalized_factors_irreducible ha, singleton_add, cons_le_cons_iff, ← ih hb.2],
apply dvd.intro _ rfl },
{ rw [multiset.le_iff_exists_add],
rintro ⟨u, hu⟩,
rw [← (normalized_factors_prod hb).dvd_iff_dvd_right, hu, prod_add, prod_repeat],
exact (associated.pow_pow $ associated_normalize a).dvd.trans (dvd.intro u.prod rfl) }
end
/-- The multiplicity of an irreducible factor of a nonzero element is exactly the number of times
the normalized factor occurs in the `normalized_factors`.
See also `count_normalized_factors_eq` which expands the definition of `multiplicity`
to produce a specification for `count (normalized_factors _) _`..
-/
lemma multiplicity_eq_count_normalized_factors {a b : R} (ha : irreducible a) (hb : b ≠ 0) :
multiplicity a b = (normalized_factors b).count (normalize a) :=
begin
apply le_antisymm,
{ apply part_enat.le_of_lt_add_one,
rw [← nat.cast_one, ← nat.cast_add, lt_iff_not_ge, ge_iff_le,
le_multiplicity_iff_repeat_le_normalized_factors ha hb, ← le_count_iff_repeat_le],
simp },
rw [le_multiplicity_iff_repeat_le_normalized_factors ha hb, ← le_count_iff_repeat_le],
end
omit dec_dvd
/-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by
the number of times it divides `x`.
See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`.
-/
lemma count_normalized_factors_eq {p x : R} (hp : irreducible p) (hnorm : normalize p = p) {n : ℕ}
(hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) :
(normalized_factors x).count p = n :=
begin
letI : decidable_rel ((∣) : R → R → Prop) := λ _ _, classical.prop_decidable _,
by_cases hx0 : x = 0,
{ simp [hx0] at hlt, contradiction },
rw [← part_enat.coe_inj],
convert (multiplicity_eq_count_normalized_factors hp hx0).symm,
{ exact hnorm.symm },
exact (multiplicity.eq_coe_iff.mpr ⟨hle, hlt⟩).symm
end
/-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by
the number of times it divides `x`. This is a slightly more general version of
`unique_factorization_monoid.count_normalized_factors_eq` that allows `p = 0`.
See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`.
-/
lemma count_normalized_factors_eq' {p x : R} (hp : p = 0 ∨ irreducible p) (hnorm : normalize p = p)
{n : ℕ} (hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) :
(normalized_factors x).count p = n :=
begin
rcases hp with rfl|hp,
{ cases n,
{ exact count_eq_zero.2 (zero_not_mem_normalized_factors _) },
{ rw [zero_pow (nat.succ_pos _)] at hle hlt,
exact absurd hle hlt } },
{ exact count_normalized_factors_eq hp hnorm hle hlt }
end
end multiplicity
end unique_factorization_monoid
namespace associates
open unique_factorization_monoid associated multiset
variables [cancel_comm_monoid_with_zero α]
/-- `factor_set α` representation elements of unique factorization domain as multisets.
`multiset α` produced by `normalized_factors` are only unique up to associated elements, while the
multisets in `factor_set α` are unique by equality and restricted to irreducible elements. This
gives us a representation of each element as a unique multisets (or the added ⊤ for 0), which has a
complete lattice struture. Infimum is the greatest common divisor and supremum is the least common
multiple.
-/
@[reducible] def {u} factor_set (α : Type u) [cancel_comm_monoid_with_zero α] :
Type u :=
with_top (multiset { a : associates α // irreducible a })
local attribute [instance] associated.setoid
theorem factor_set.coe_add {a b : multiset { a : associates α // irreducible a }} :
(↑(a + b) : factor_set α) = a + b :=
by norm_cast
lemma factor_set.sup_add_inf_eq_add [decidable_eq (associates α)] :
∀(a b : factor_set α), a ⊔ b + a ⊓ b = a + b
| none b := show ⊤ ⊔ b + ⊤ ⊓ b = ⊤ + b, by simp
| a none := show a ⊔ ⊤ + a ⊓ ⊤ = a + ⊤, by simp
| (some a) (some b) := show (a : factor_set α) ⊔ b + a ⊓ b = a + b, from
begin
rw [← with_top.coe_sup, ← with_top.coe_inf, ← with_top.coe_add, ← with_top.coe_add,
with_top.coe_eq_coe],
exact multiset.union_add_inter _ _
end
/-- Evaluates the product of a `factor_set` to be the product of the corresponding multiset,
or `0` if there is none. -/
def factor_set.prod : factor_set α → associates α
| none := 0
| (some s) := (s.map coe).prod
@[simp] theorem prod_top : (⊤ : factor_set α).prod = 0 := rfl
@[simp] theorem prod_coe {s : multiset { a : associates α // irreducible a }} :
(s : factor_set α).prod = (s.map coe).prod :=
rfl
@[simp] theorem prod_add : ∀(a b : factor_set α), (a + b).prod = a.prod * b.prod
| none b := show (⊤ + b).prod = (⊤:factor_set α).prod * b.prod, by simp
| a none := show (a + ⊤).prod = a.prod * (⊤:factor_set α).prod, by simp
| (some a) (some b) :=
show (↑a + ↑b:factor_set α).prod = (↑a:factor_set α).prod * (↑b:factor_set α).prod,
by rw [← factor_set.coe_add, prod_coe, prod_coe, prod_coe, multiset.map_add, multiset.prod_add]
theorem prod_mono : ∀{a b : factor_set α}, a ≤ b → a.prod ≤ b.prod
| none b h := have b = ⊤, from top_unique h, by rw [this, prod_top]; exact le_rfl
| a none h := show a.prod ≤ (⊤ : factor_set α).prod, by simp; exact le_top
| (some a) (some b) h := prod_le_prod $ multiset.map_le_map $ with_top.coe_le_coe.1 $ h
theorem factor_set.prod_eq_zero_iff [nontrivial α] (p : factor_set α) :
p.prod = 0 ↔ p = ⊤ :=
begin
induction p using with_top.rec_top_coe,
{ simp only [iff_self, eq_self_iff_true, associates.prod_top] },
simp only [prod_coe, with_top.coe_ne_top, iff_false, prod_eq_zero_iff, multiset.mem_map],
rintro ⟨⟨a, ha⟩, -, eq⟩,
rw [subtype.coe_mk] at eq,
exact ha.ne_zero eq,
end
/-- `bcount p s` is the multiplicity of `p` in the factor_set `s` (with bundled `p`)-/
def bcount [decidable_eq (associates α)] (p : {a : associates α // irreducible a}) :
factor_set α → ℕ
| none := 0
| (some s) := s.count p
variables [dec_irr : Π (p : associates α), decidable (irreducible p)]
include dec_irr
/-- `count p s` is the multiplicity of the irreducible `p` in the factor_set `s`.
If `p` is not irreducible, `count p s` is defined to be `0`. -/
def count [decidable_eq (associates α)] (p : associates α) :
factor_set α → ℕ :=
if hp : irreducible p then bcount ⟨p, hp⟩ else 0
@[simp] lemma count_some [decidable_eq (associates α)] {p : associates α} (hp : irreducible p)
(s : multiset _) : count p (some s) = s.count ⟨p, hp⟩:=
by { dunfold count, split_ifs, refl }
@[simp] lemma count_zero [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) :
count p (0 : factor_set α) = 0 :=
by { dunfold count, split_ifs, refl }
lemma count_reducible [decidable_eq (associates α)] {p : associates α} (hp : ¬ irreducible p) :
count p = 0 := dif_neg hp
omit dec_irr
/-- membership in a factor_set (bundled version) -/
def bfactor_set_mem : {a : associates α // irreducible a} → (factor_set α) → Prop
| _ ⊤ := true
| p (some l) := p ∈ l
include dec_irr
/-- `factor_set_mem p s` is the predicate that the irreducible `p` is a member of
`s : factor_set α`.
If `p` is not irreducible, `p` is not a member of any `factor_set`. -/
def factor_set_mem (p : associates α) (s : factor_set α) : Prop :=
if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false
instance : has_mem (associates α) (factor_set α) := ⟨factor_set_mem⟩
@[simp] lemma factor_set_mem_eq_mem (p : associates α) (s : factor_set α) :
factor_set_mem p s = (p ∈ s) := rfl
lemma mem_factor_set_top {p : associates α} {hp : irreducible p} :
p ∈ (⊤ : factor_set α) :=
begin
dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, exact trivial
end
lemma mem_factor_set_some {p : associates α} {hp : irreducible p}
{l : multiset {a : associates α // irreducible a }} :
p ∈ (l : factor_set α) ↔ subtype.mk p hp ∈ l :=
begin
dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, refl
end
lemma reducible_not_mem_factor_set {p : associates α} (hp : ¬ irreducible p)
(s : factor_set α) : ¬ p ∈ s :=
λ (h : if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false),
by rwa [dif_neg hp] at h
omit dec_irr
variable [unique_factorization_monoid α]
theorem unique' {p q : multiset (associates α)} :
(∀a∈p, irreducible a) → (∀a∈q, irreducible a) → p.prod = q.prod → p = q :=
begin
apply multiset.induction_on_multiset_quot p,
apply multiset.induction_on_multiset_quot q,
assume s t hs ht eq,
refine multiset.map_mk_eq_map_mk_of_rel (unique_factorization_monoid.factors_unique _ _ _),
{ exact assume a ha, ((irreducible_mk _).1 $ hs _ $ multiset.mem_map_of_mem _ ha) },
{ exact assume a ha, ((irreducible_mk _).1 $ ht _ $ multiset.mem_map_of_mem _ ha) },
simpa [quot_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated] using eq
end
theorem factor_set.unique [nontrivial α] {p q : factor_set α} (h : p.prod = q.prod) : p = q :=
begin
induction p using with_top.rec_top_coe;
induction q using with_top.rec_top_coe,
{ refl },
{ rw [eq_comm, ←factor_set.prod_eq_zero_iff, ←h, associates.prod_top] },
{ rw [←factor_set.prod_eq_zero_iff, h, associates.prod_top] },
{ congr' 1,
rw ←multiset.map_eq_map subtype.coe_injective,
apply unique' _ _ h;
{ intros a ha,
obtain ⟨⟨a', irred⟩, -, rfl⟩ := multiset.mem_map.mp ha,
rwa [subtype.coe_mk] } },
end
theorem prod_le_prod_iff_le [nontrivial α] {p q : multiset (associates α)}
(hp : ∀a∈p, irreducible a) (hq : ∀a∈q, irreducible a) :
p.prod ≤ q.prod ↔ p ≤ q :=
iff.intro
begin
classical,
rintros ⟨c, eqc⟩,
refine multiset.le_iff_exists_add.2 ⟨factors c, unique' hq (λ x hx, _) _⟩,
{ obtain h|h := multiset.mem_add.1 hx,
{ exact hp x h },
{ exact irreducible_of_factor _ h } },
{ rw [eqc, multiset.prod_add],
congr,
refine associated_iff_eq.mp (factors_prod (λ hc, _)).symm,
refine not_irreducible_zero (hq _ _),
rw [←prod_eq_zero_iff, eqc, hc, mul_zero] }
end
prod_le_prod
variables [dec : decidable_eq α] [dec' : decidable_eq (associates α)]
include dec
/-- This returns the multiset of irreducible factors as a `factor_set`,
a multiset of irreducible associates `with_top`. -/
noncomputable def factors' (a : α) :
multiset { a : associates α // irreducible a } :=
(factors a).pmap (λa ha, ⟨associates.mk a, (irreducible_mk _).2 ha⟩)
(irreducible_of_factor)
@[simp] theorem map_subtype_coe_factors' {a : α} :
(factors' a).map coe = (factors a).map associates.mk :=
by simp [factors', multiset.map_pmap, multiset.pmap_eq_map]
theorem factors'_cong {a b : α} (h : a ~ᵤ b) :
factors' a = factors' b :=
begin
obtain rfl|hb := eq_or_ne b 0,
{ rw associated_zero_iff_eq_zero at h, rw h },
have ha : a ≠ 0,
{ contrapose! hb with ha,
rw [←associated_zero_iff_eq_zero, ←ha],
exact h.symm },
rw [←multiset.map_eq_map subtype.coe_injective, map_subtype_coe_factors',
map_subtype_coe_factors', ←rel_associated_iff_map_eq_map],
exact factors_unique irreducible_of_factor irreducible_of_factor
((factors_prod ha).trans $ h.trans $ (factors_prod hb).symm),
end
include dec'
/-- This returns the multiset of irreducible factors of an associate as a `factor_set`,
a multiset of irreducible associates `with_top`. -/
noncomputable def factors (a : associates α) :
factor_set α :=
begin
refine (if h : a = 0 then ⊤ else
quotient.hrec_on a (λx h, some $ factors' x) _ h),
assume a b hab,
apply function.hfunext,
{ have : a ~ᵤ 0 ↔ b ~ᵤ 0, from
iff.intro (assume ha0, hab.symm.trans ha0) (assume hb0, hab.trans hb0),
simp only [associated_zero_iff_eq_zero] at this,
simp only [quotient_mk_eq_mk, this, mk_eq_zero] },
exact (assume ha hb eq, heq_of_eq $ congr_arg some $ factors'_cong hab)
end
@[simp] theorem factors_0 : (0 : associates α).factors = ⊤ :=
dif_pos rfl
@[simp] theorem factors_mk (a : α) (h : a ≠ 0) :
(associates.mk a).factors = factors' a :=
by { classical, apply dif_neg, apply (mt mk_eq_zero.1 h) }
@[simp]
theorem factors_prod (a : associates α) : a.factors.prod = a :=
quotient.induction_on a $ assume a, decidable.by_cases
(assume : associates.mk a = 0, by simp [quotient_mk_eq_mk, this])
(assume : associates.mk a ≠ 0,
have a ≠ 0, by simp * at *,
by simp [this, quotient_mk_eq_mk, prod_mk,
mk_eq_mk_iff_associated.2 (factors_prod this)])
theorem prod_factors [nontrivial α] (s : factor_set α) : s.prod.factors = s :=
factor_set.unique $ factors_prod _
@[nontriviality] lemma factors_subsingleton [subsingleton α] {a : associates α} :
a.factors = option.none :=
by { convert factors_0; apply_instance }
lemma factors_eq_none_iff_zero {a : associates α} :
a.factors = option.none ↔ a = 0 :=
begin
nontriviality α,
exact ⟨λ h, by rwa [← factors_prod a, factor_set.prod_eq_zero_iff], λ h, h.symm ▸ factors_0⟩
end
lemma factors_eq_some_iff_ne_zero {a : associates α} :
(∃ (s : multiset {p : associates α // irreducible p}), a.factors = some s) ↔ a ≠ 0 :=
by rw [← option.is_some_iff_exists, ← option.ne_none_iff_is_some, ne.def, ne.def,
factors_eq_none_iff_zero]
theorem eq_of_factors_eq_factors {a b : associates α} (h : a.factors = b.factors) : a = b :=
have a.factors.prod = b.factors.prod, by rw h,
by rwa [factors_prod, factors_prod] at this
omit dec dec'
theorem eq_of_prod_eq_prod [nontrivial α] {a b : factor_set α} (h : a.prod = b.prod) : a = b :=
begin
classical,
have : a.prod.factors = b.prod.factors, by rw h,
rwa [prod_factors, prod_factors] at this
end
include dec dec' dec_irr
theorem eq_factors_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(h : ∀ (p : associates α) (hp : irreducible p), p.count a.factors = p.count b.factors) :
a.factors = b.factors :=
begin
obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha,
obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb,
rw [h_sa, h_sb] at h ⊢,
rw option.some_inj,
have h_count : ∀ (p : associates α) (hp : irreducible p), sa.count ⟨p, hp⟩ = sb.count ⟨p, hp⟩,
{ intros p hp, rw [← count_some, ← count_some, h p hp] },
apply multiset.to_finsupp.injective,
ext ⟨p, hp⟩,
rw [multiset.to_finsupp_apply, multiset.to_finsupp_apply, h_count p hp]
end
theorem eq_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(h : ∀ (p : associates α), irreducible p → p.count a.factors = p.count b.factors) : a = b :=
eq_of_factors_eq_factors (eq_factors_of_eq_counts ha hb h)
lemma count_le_count_of_factors_le {a b p : associates α} (hb : b ≠ 0)
(hp : irreducible p) (h : a.factors ≤ b.factors) : p.count a.factors ≤ p.count b.factors :=
begin
by_cases ha : a = 0,
{ simp [*] at *, },
obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha,
obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb,
rw [h_sa, h_sb] at h ⊢,
rw [count_some hp, count_some hp], rw with_top.some_le_some at h,
exact multiset.count_le_of_le _ h
end
omit dec_irr
@[simp] theorem factors_mul (a b : associates α) :
(a * b).factors = a.factors + b.factors :=
begin
casesI subsingleton_or_nontrivial α,
{ simp [subsingleton.elim a 0], },
refine (eq_of_prod_eq_prod (eq_of_factors_eq_factors _)),
rw [prod_add, factors_prod, factors_prod, factors_prod],
end
theorem factors_mono : ∀{a b : associates α}, a ≤ b → a.factors ≤ b.factors
| s t ⟨d, rfl⟩ := by rw [factors_mul] ; exact le_add_of_nonneg_right bot_le
theorem factors_le {a b : associates α} : a.factors ≤ b.factors ↔ a ≤ b :=
iff.intro
(assume h, have a.factors.prod ≤ b.factors.prod, from prod_mono h,
by rwa [factors_prod, factors_prod] at this)
factors_mono
include dec_irr
lemma count_le_count_of_le {a b p : associates α} (hb : b ≠ 0)
(hp : irreducible p) (h : a ≤ b) : p.count a.factors ≤ p.count b.factors :=
count_le_count_of_factors_le hb hp $ factors_mono h
omit dec dec' dec_irr
theorem prod_le [nontrivial α] {a b : factor_set α} : a.prod ≤ b.prod ↔ a ≤ b :=
begin
classical,
exact iff.intro
(assume h, have a.prod.factors ≤ b.prod.factors, from factors_mono h,
by rwa [prod_factors, prod_factors] at this)
prod_mono
end
include dec dec'
noncomputable instance : has_sup (associates α) := ⟨λa b, (a.factors ⊔ b.factors).prod⟩
noncomputable instance : has_inf (associates α) := ⟨λa b, (a.factors ⊓ b.factors).prod⟩
noncomputable instance : lattice (associates α) :=
{ sup := (⊔),
inf := (⊓),
sup_le :=
assume a b c hac hbc, factors_prod c ▸ prod_mono (sup_le (factors_mono hac) (factors_mono hbc)),
le_sup_left := assume a b,
le_trans (le_of_eq (factors_prod a).symm) $ prod_mono $ le_sup_left,
le_sup_right := assume a b,
le_trans (le_of_eq (factors_prod b).symm) $ prod_mono $ le_sup_right,
le_inf :=
assume a b c hac hbc, factors_prod a ▸ prod_mono (le_inf (factors_mono hac) (factors_mono hbc)),
inf_le_left := assume a b,
le_trans (prod_mono inf_le_left) (le_of_eq (factors_prod a)),
inf_le_right := assume a b,
le_trans (prod_mono inf_le_right) (le_of_eq (factors_prod b)),
.. associates.partial_order }
lemma sup_mul_inf (a b : associates α) : (a ⊔ b) * (a ⊓ b) = a * b :=
show (a.factors ⊔ b.factors).prod * (a.factors ⊓ b.factors).prod = a * b,
begin
nontriviality α,
refine eq_of_factors_eq_factors _,
rw [← prod_add, prod_factors, factors_mul, factor_set.sup_add_inf_eq_add]
end
include dec_irr
lemma dvd_of_mem_factors {a p : associates α} {hp : irreducible p}
(hm : p ∈ factors a) : p ∣ a :=
begin
by_cases ha0 : a = 0, { rw ha0, exact dvd_zero p },
obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha0,
rw [← associates.factors_prod a],
rw [← ha', factors_mk a0 nza] at hm ⊢,
erw prod_coe,
apply multiset.dvd_prod, apply multiset.mem_map.mpr,
exact ⟨⟨p, hp⟩, mem_factor_set_some.mp hm, rfl⟩
end
omit dec'
lemma dvd_of_mem_factors' {a : α} {p : associates α} {hp : irreducible p} {hz : a ≠ 0}
(h_mem : subtype.mk p hp ∈ factors' a) : p ∣ associates.mk a :=
by { haveI := classical.dec_eq (associates α),
apply @dvd_of_mem_factors _ _ _ _ _ _ _ _ hp,
rw factors_mk _ hz,
apply mem_factor_set_some.2 h_mem }
omit dec_irr
lemma mem_factors'_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) :
subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a :=
begin
obtain ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd ha0 hp hd,
apply multiset.mem_pmap.mpr, use q, use hq,
exact subtype.eq (eq.symm (mk_eq_mk_iff_associated.mpr hpq))
end
include dec_irr
lemma mem_factors'_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a ↔ p ∣ a :=
begin
split,
{ rw ← mk_dvd_mk, apply dvd_of_mem_factors', apply ha0 },
{ apply mem_factors'_of_dvd ha0 }
end
include dec'
lemma mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) :
(associates.mk p) ∈ factors (associates.mk a) :=
begin
rw factors_mk _ ha0, exact mem_factor_set_some.mpr (mem_factors'_of_dvd ha0 hp hd)
end
lemma mem_factors_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
(associates.mk p) ∈ factors (associates.mk a) ↔ p ∣ a :=
begin
split,
{ rw ← mk_dvd_mk, apply dvd_of_mem_factors, exact (irreducible_mk p).mpr hp },
{ apply mem_factors_of_dvd ha0 hp }
end
lemma exists_prime_dvd_of_not_inf_one {a b : α}
(ha : a ≠ 0) (hb : b ≠ 0) (h : (associates.mk a) ⊓ (associates.mk b) ≠ 1) :
∃ (p : α), prime p ∧ p ∣ a ∧ p ∣ b :=
begin
have hz : (factors (associates.mk a)) ⊓ (factors (associates.mk b)) ≠ 0,
{ contrapose! h with hf,
change ((factors (associates.mk a)) ⊓ (factors (associates.mk b))).prod = 1,
rw hf,
exact multiset.prod_zero },
rw [factors_mk a ha, factors_mk b hb, ← with_top.coe_inf] at hz,
obtain ⟨⟨p0, p0_irr⟩, p0_mem⟩ := multiset.exists_mem_of_ne_zero ((mt with_top.coe_eq_coe.mpr) hz),
rw multiset.inf_eq_inter at p0_mem,
obtain ⟨p, rfl⟩ : ∃ p, associates.mk p = p0 := quot.exists_rep p0,
refine ⟨p, _, _, _⟩,
{ rw [← irreducible_iff_prime, ← irreducible_mk],
exact p0_irr },
{ apply dvd_of_mk_le_mk,
apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).left,
apply ha, },
{ apply dvd_of_mk_le_mk,
apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).right,
apply hb }
end
theorem coprime_iff_inf_one {a b : α} (ha0 : a ≠ 0) (hb0 : b ≠ 0) :
(associates.mk a) ⊓ (associates.mk b) = 1 ↔ ∀ {d : α}, d ∣ a → d ∣ b → ¬ prime d :=
begin
split,
{ intros hg p ha hb hp,
refine ((associates.prime_mk _).mpr hp).not_unit (is_unit_of_dvd_one _ _),
rw ← hg,
exact le_inf (mk_le_mk_of_dvd ha) (mk_le_mk_of_dvd hb) },
{ contrapose,
intros hg hc,
obtain ⟨p, hp, hpa, hpb⟩ := exists_prime_dvd_of_not_inf_one ha0 hb0 hg,
exact hc hpa hpb hp }
end
omit dec_irr
theorem factors_self [nontrivial α] {p : associates α} (hp : irreducible p) :
p.factors = some ({⟨p, hp⟩}) :=
eq_of_prod_eq_prod (by rw [factors_prod, factor_set.prod, map_singleton, prod_singleton,
subtype.coe_mk])
theorem factors_prime_pow [nontrivial α] {p : associates α} (hp : irreducible p)
(k : ℕ) : factors (p ^ k) = some (multiset.repeat ⟨p, hp⟩ k) :=
eq_of_prod_eq_prod (by rw [associates.factors_prod, factor_set.prod, multiset.map_repeat,
multiset.prod_repeat, subtype.coe_mk])
include dec_irr
theorem prime_pow_dvd_iff_le [nontrivial α] {m p : associates α} (h₁ : m ≠ 0)
(h₂ : irreducible p) {k : ℕ} : p ^ k ≤ m ↔ k ≤ count p m.factors :=
begin
obtain ⟨a, nz, rfl⟩ := associates.exists_non_zero_rep h₁,
rw [factors_mk _ nz, ← with_top.some_eq_coe, count_some, multiset.le_count_iff_repeat_le,
← factors_le, factors_prime_pow h₂, factors_mk _ nz],
exact with_top.coe_le_coe
end
theorem le_of_count_ne_zero {m p : associates α} (h0 : m ≠ 0)
(hp : irreducible p) : count p m.factors ≠ 0 → p ≤ m :=
begin
nontriviality α,
rw [← pos_iff_ne_zero],
intro h,
rw [← pow_one p],
apply (prime_pow_dvd_iff_le h0 hp).2,
simpa only
end
theorem count_ne_zero_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) :
(associates.mk p).count (associates.mk a).factors ≠ 0 ↔ p ∣ a :=
begin
nontriviality α,
rw ← associates.mk_le_mk_iff_dvd_iff,
refine ⟨λ h, associates.le_of_count_ne_zero (associates.mk_ne_zero.mpr ha0)
((associates.irreducible_mk p).mpr hp) h, λ h, _⟩,
{ rw [← pow_one (associates.mk p), associates.prime_pow_dvd_iff_le
(associates.mk_ne_zero.mpr ha0) ((associates.irreducible_mk p).mpr hp)] at h,
exact (zero_lt_one.trans_le h).ne' }
end
theorem count_self [nontrivial α] {p : associates α} (hp : irreducible p) :
p.count p.factors = 1 :=
by simp [factors_self hp, associates.count_some hp]
lemma count_eq_zero_of_ne {p q : associates α} (hp : irreducible p) (hq : irreducible q)
(h : p ≠ q) : p.count q.factors = 0 :=
not_ne_iff.mp $ λ h', h $ associated_iff_eq.mp $ hp.associated_of_dvd hq $
by { nontriviality α, exact le_of_count_ne_zero hq.ne_zero hp h' }
theorem count_mul {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0)
{p : associates α} (hp : irreducible p) :
count p (factors (a * b)) = count p a.factors + count p b.factors :=
begin
obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha,
obtain ⟨b0, nzb, hb'⟩ := exists_non_zero_rep hb,
rw [factors_mul, ← ha', ← hb', factors_mk a0 nza, factors_mk b0 nzb, ← factor_set.coe_add,
← with_top.some_eq_coe, ← with_top.some_eq_coe, ← with_top.some_eq_coe, count_some hp,
multiset.count_add, count_some hp, count_some hp]
end
theorem count_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α}
(hb : b ≠ 0)
(hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {p : associates α} (hp : irreducible p) :
count p a.factors = 0 ∨ count p b.factors = 0 :=
begin
rw [or_iff_not_imp_left, ← ne.def],
intro hca,
contrapose! hab with hcb,
exact ⟨p, le_of_count_ne_zero ha hp hca, le_of_count_ne_zero hb hp hcb,
(irreducible_iff_prime.mp hp)⟩,
end
theorem count_mul_of_coprime {a : associates α} {b : associates α}
(hb : b ≠ 0)
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) :
count p a.factors = 0 ∨ count p a.factors = count p (a * b).factors :=
begin
by_cases ha : a = 0,
{ simp [ha], },
cases count_of_coprime ha hb hab hp with hz hb0, { tauto },
apply or.intro_right,
rw [count_mul ha hb hp, hb0, add_zero]
end
theorem count_mul_of_coprime' {a b : associates α}
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) :
count p (a * b).factors = count p a.factors
∨ count p (a * b).factors = count p b.factors :=
begin
by_cases ha : a = 0, { simp [ha], },
by_cases hb : b = 0, { simp [hb], },
rw [count_mul ha hb hp],
cases count_of_coprime ha hb hab hp with ha0 hb0,
{ apply or.intro_right, rw [ha0, zero_add] },
{ apply or.intro_left, rw [hb0, add_zero] }
end
theorem dvd_count_of_dvd_count_mul {a b : associates α} (hb : b ≠ 0)
{p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d)
{k : ℕ} (habk : k ∣ count p (a * b).factors) : k ∣ count p a.factors :=
begin
by_cases ha : a = 0, { simpa [*] using habk, },
cases count_of_coprime ha hb hab hp with hz h,
{ rw hz, exact dvd_zero k },
{ rw [count_mul ha hb hp, h] at habk, exact habk }
end
omit dec_irr
@[simp] lemma factors_one [nontrivial α] : factors (1 : associates α) = 0 :=
begin
apply eq_of_prod_eq_prod,
rw associates.factors_prod,
exact multiset.prod_zero,
end
@[simp] theorem pow_factors [nontrivial α] {a : associates α} {k : ℕ} :
(a ^ k).factors = k • a.factors :=
begin
induction k with n h,
{ rw [zero_nsmul, pow_zero], exact factors_one },
{ rw [pow_succ, succ_nsmul, factors_mul, h] }
end
include dec_irr
lemma count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α}
(hp : irreducible p)
(k : ℕ) : count p (a ^ k).factors = k * count p a.factors :=
begin
induction k with n h,
{ rw [pow_zero, factors_one, zero_mul, count_zero hp] },
{ rw [pow_succ, count_mul ha (pow_ne_zero _ ha) hp, h, nat.succ_eq_add_one], ring }
end
theorem dvd_count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α}
(hp : irreducible p)
(k : ℕ) : k ∣ count p (a ^ k).factors := by { rw count_pow ha hp, apply dvd_mul_right }
theorem is_pow_of_dvd_count [nontrivial α] {a : associates α} (ha : a ≠ 0) {k : ℕ}
(hk : ∀ (p : associates α) (hp : irreducible p), k ∣ count p a.factors) :
∃ (b : associates α), a = b ^ k :=
begin
obtain ⟨a0, hz, rfl⟩ := exists_non_zero_rep ha,
rw [factors_mk a0 hz] at hk,
have hk' : ∀ p, p ∈ (factors' a0) → k ∣ (factors' a0).count p,
{ rintros p -,
have pp : p = ⟨p.val, p.2⟩, { simp only [subtype.coe_eta, subtype.val_eq_coe] },
rw [pp, ← count_some p.2], exact hk p.val p.2 },
obtain ⟨u, hu⟩ := multiset.exists_smul_of_dvd_count _ hk',
use (u : factor_set α).prod,
apply eq_of_factors_eq_factors,
rw [pow_factors, prod_factors, factors_mk a0 hz, ← with_top.some_eq_coe, hu],
exact with_bot.coe_nsmul u k
end
/-- The only divisors of prime powers are prime powers. See `eq_pow_find_of_dvd_irreducible_pow`
for an explicit expression as a p-power (without using `count`). -/
theorem eq_pow_count_factors_of_dvd_pow {p a : associates α} (hp : irreducible p)
{n : ℕ} (h : a ∣ p ^ n) : a = p ^ p.count a.factors :=
begin
nontriviality α,
have hph := pow_ne_zero n hp.ne_zero,
have ha := ne_zero_of_dvd_ne_zero hph h,
apply eq_of_eq_counts ha (pow_ne_zero _ hp.ne_zero),
have eq_zero_of_ne : ∀ (q : associates α), irreducible q → q ≠ p → _ = 0 :=
λ q hq h', nat.eq_zero_of_le_zero $ by
{ convert count_le_count_of_le hph hq h, symmetry,
rw [count_pow hp.ne_zero hq, count_eq_zero_of_ne hq hp h', mul_zero] },
intros q hq,
rw count_pow hp.ne_zero hq,
by_cases h : q = p,
{ rw [h, count_self hp, mul_one] },
{ rw [count_eq_zero_of_ne hq hp h, mul_zero, eq_zero_of_ne q hq h] }
end
lemma count_factors_eq_find_of_dvd_pow {a p : associates α} (hp : irreducible p)
[∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : nat.find ⟨n, h⟩ = p.count a.factors :=
begin
apply le_antisymm,
{ refine nat.find_le ⟨1, _⟩, rw mul_one, symmetry, exact eq_pow_count_factors_of_dvd_pow hp h },
{ have hph := pow_ne_zero (nat.find ⟨n, h⟩) hp.ne_zero,
casesI (subsingleton_or_nontrivial α) with hα hα,
{ simpa using hph, },
convert count_le_count_of_le hph hp (nat.find_spec ⟨n, h⟩),
rw [count_pow hp.ne_zero hp, count_self hp, mul_one] }
end
omit dec
omit dec_irr
omit dec'
theorem eq_pow_of_mul_eq_pow [nontrivial α] {a b c : associates α} (ha : a ≠ 0) (hb : b ≠ 0)
(hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (h : a * b = c ^ k) :
∃ (d : associates α), a = d ^ k :=
begin
classical,
by_cases hk0 : k = 0,
{ use 1,
rw [hk0, pow_zero] at h ⊢,
apply (mul_eq_one_iff.1 h).1 },
{ refine is_pow_of_dvd_count ha _,
intros p hp,
apply dvd_count_of_dvd_count_mul hb hp hab,
rw h,
apply dvd_count_pow _ hp,
rintros rfl,
rw zero_pow' _ hk0 at h,
cases mul_eq_zero.mp h; contradiction }
end
/-- The only divisors of prime powers are prime powers. -/
theorem eq_pow_find_of_dvd_irreducible_pow {a p : associates α} (hp : irreducible p)
[∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : a = p ^ nat.find ⟨n, h⟩ :=
by { classical, rw [count_factors_eq_find_of_dvd_pow hp, ← eq_pow_count_factors_of_dvd_pow hp h] }
end associates
section
open associates unique_factorization_monoid
lemma associates.quot_out {α : Type*} [comm_monoid α] (a : associates α):
associates.mk (quot.out (a)) = a :=
by rw [←quot_mk_eq_mk, quot.out_eq]
/-- `to_gcd_monoid` constructs a GCD monoid out of a unique factorization domain. -/
noncomputable def unique_factorization_monoid.to_gcd_monoid
(α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
[decidable_eq (associates α)] [decidable_eq α] : gcd_monoid α :=
{ gcd := λa b, quot.out (associates.mk a ⊓ associates.mk b : associates α),
lcm := λa b, quot.out (associates.mk a ⊔ associates.mk b : associates α),
gcd_dvd_left := λ a b, by
{ rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le],
exact inf_le_left },
gcd_dvd_right := λ a b, by
{ rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le],
exact inf_le_right },
dvd_gcd := λ a b c hac hab, by
{ rw [←mk_dvd_mk, (associates.mk c ⊓ associates.mk b).quot_out, dvd_eq_le,
le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff],
exact ⟨hac, hab⟩ },
lcm_zero_left := λ a, by
{ have : associates.mk (0 : α) = ⊤ := rfl,
rw [this, top_sup_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated,
←associated_iff_eq, associates.quot_out] },
lcm_zero_right := λ a, by
{ have : associates.mk (0 : α) = ⊤ := rfl,
rw [this, sup_top_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated,
←associated_iff_eq, associates.quot_out] },
gcd_mul_lcm := λ a b, by
{ rw [←mk_eq_mk_iff_associated, ←associates.mk_mul_mk, ←associated_iff_eq, associates.quot_out,
associates.quot_out, mul_comm, sup_mul_inf, associates.mk_mul_mk] } }
/-- `to_normalized_gcd_monoid` constructs a GCD monoid out of a normalization on a
unique factorization domain. -/
noncomputable def unique_factorization_monoid.to_normalized_gcd_monoid
(α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
[normalization_monoid α] [decidable_eq (associates α)] [decidable_eq α] :
normalized_gcd_monoid α :=
{ gcd := λa b, (associates.mk a ⊓ associates.mk b).out,
lcm := λa b, (associates.mk a ⊔ associates.mk b).out,
gcd_dvd_left := assume a b, (out_dvd_iff a (associates.mk a ⊓ associates.mk b)).2 $ inf_le_left,
gcd_dvd_right := assume a b, (out_dvd_iff b (associates.mk a ⊓ associates.mk b)).2 $ inf_le_right,
dvd_gcd := assume a b c hac hab, show a ∣ (associates.mk c ⊓ associates.mk b).out,
by rw [dvd_out_iff, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]; exact ⟨hac, hab⟩,
lcm_zero_left := assume a, show (⊤ ⊔ associates.mk a).out = 0, by simp,
lcm_zero_right := assume a, show (associates.mk a ⊔ ⊤).out = 0, by simp,
gcd_mul_lcm := assume a b, by
{ rw [← out_mul, mul_comm, sup_mul_inf, mk_mul_mk, out_mk],
exact normalize_associated (a * b) },
normalize_gcd := assume a b, by convert normalize_out _,
normalize_lcm := assume a b, by convert normalize_out _,
.. ‹normalization_monoid α› }
end
namespace unique_factorization_monoid
/-- If `y` is a nonzero element of a unique factorization monoid with finitely
many units (e.g. `ℤ`, `ideal (ring_of_integers K)`), it has finitely many divisors. -/
noncomputable def fintype_subtype_dvd {M : Type*} [cancel_comm_monoid_with_zero M]
[unique_factorization_monoid M] [fintype Mˣ]
(y : M) (hy : y ≠ 0) :
fintype {x // x ∣ y} :=
begin
haveI : nontrivial M := ⟨⟨y, 0, hy⟩⟩,
haveI : normalization_monoid M := unique_factorization_monoid.normalization_monoid,
haveI := classical.dec_eq M,
haveI := classical.dec_eq (associates M),
-- We'll show `λ (u : Mˣ) (f ⊆ factors y) → u * Π f` is injective
-- and has image exactly the divisors of `y`.
refine fintype.of_finset
(((normalized_factors y).powerset.to_finset.product (finset.univ : finset Mˣ)).image
(λ s, (s.snd : M) * s.fst.prod))
(λ x, _),
simp only [exists_prop, finset.mem_image, finset.mem_product, finset.mem_univ, and_true,
multiset.mem_to_finset, multiset.mem_powerset, exists_eq_right, multiset.mem_map],
split,
{ rintros ⟨s, hs, rfl⟩,
have prod_s_ne : s.fst.prod ≠ 0,
{ intro hz,
apply hy (eq_zero_of_zero_dvd _),
have hz := (@multiset.prod_eq_zero_iff M _ _ _ s.fst).mp hz,
rw ← (normalized_factors_prod hy).dvd_iff_dvd_right,
exact multiset.dvd_prod (multiset.mem_of_le hs hz) },
show (s.snd : M) * s.fst.prod ∣ y,
rw [(unit_associated_one.mul_right s.fst.prod).dvd_iff_dvd_left, one_mul,
← (normalized_factors_prod hy).dvd_iff_dvd_right],
exact multiset.prod_dvd_prod_of_le hs },
{ rintro (h : x ∣ y),
have hx : x ≠ 0, { refine mt (λ hx, _) hy, rwa [hx, zero_dvd_iff] at h },
obtain ⟨u, hu⟩ := normalized_factors_prod hx,
refine ⟨⟨normalized_factors x, u⟩, _, (mul_comm _ _).trans hu⟩,
exact (dvd_iff_normalized_factors_le_normalized_factors hx hy).mp h }
end
end unique_factorization_monoid
section finsupp
variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α]
variables [normalization_monoid α] [decidable_eq α]
open unique_factorization_monoid
/-- This returns the multiset of irreducible factors as a `finsupp` -/
noncomputable def factorization (n : α) : α →₀ ℕ := (normalized_factors n).to_finsupp
lemma factorization_eq_count {n p : α} :
factorization n p = multiset.count p (normalized_factors n) :=
by simp [factorization]
@[simp] lemma factorization_zero : factorization (0 : α) = 0 := by simp [factorization]
@[simp] lemma factorization_one : factorization (1 : α) = 0 := by simp [factorization]
/-- The support of `factorization n` is exactly the finset of normalized factors -/
@[simp] lemma support_factorization {n : α} :
(factorization n).support = (normalized_factors n).to_finset :=
by simp [factorization, multiset.to_finsupp_support]
/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
@[simp] lemma factorization_mul {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) :
factorization (a * b) = factorization a + factorization b :=
by simp [factorization, normalized_factors_mul ha hb]
/-- For any `p`, the power of `p` in `x^n` is `n` times the power in `x` -/
lemma factorization_pow {x : α} {n : ℕ} :
factorization (x^n) = n • factorization x :=
by { ext, simp [factorization] }
lemma associated_of_factorization_eq (a b: α) (ha: a ≠ 0) (hb: b ≠ 0)
(h: factorization a = factorization b) : associated a b :=
begin
simp only [factorization, add_equiv.apply_eq_iff_eq] at h,
have ha' := normalized_factors_prod ha,
rw h at ha',
exact associated.trans ha'.symm (normalized_factors_prod hb),
end
end finsupp
|
7e12336ca00141c4783a9df38a20574bc6b18acf | dc253be9829b840f15d96d986e0c13520b085033 | /algebra/exactness.hlean | b90bdd90680534e87060e6f5de655a7fee97d3aa | [
"Apache-2.0"
] | permissive | cmu-phil/Spectral | 4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea | 3b078f5f1de251637decf04bd3fc8aa01930a6b3 | refs/heads/master | 1,685,119,195,535 | 1,684,169,772,000 | 1,684,169,772,000 | 46,450,197 | 42 | 13 | null | 1,505,516,767,000 | 1,447,883,921,000 | Lean | UTF-8 | Lean | false | false | 11,031 | hlean | /-
Copyright (c) 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
Short exact sequences
-/
import homotopy.chain_complex eq2 .quotient_group
open pointed is_trunc equiv is_equiv eq algebra group trunc function fiber sigma property
structure is_exact_t {A B : Type} {C : Type*} (f : A → B) (g : B → C) :=
( im_in_ker : Π(a:A), g (f a) = pt)
( ker_in_im : Π(b:B), (g b = pt) → fiber f b)
structure is_exact {A B : Type} {C : Type*} (f : A → B) (g : B → C) :=
( im_in_ker : Π(a:A), g (f a) = pt)
( ker_in_im : Π(b:B), (g b = pt) → image f b)
namespace algebra
definition is_exact_g {A B C : Group} (f : A →g B) (g : B →g C) :=
is_exact f g
definition is_exact_ag {A B C : AbGroup} (f : A →g B) (g : B →g C) :=
is_exact f g
definition is_exact_g.mk {A B C : Group} {f : A →g B} {g : B →g C}
(H₁ : Πa, g (f a) = 1) (H₂ : Πb, g b = 1 → image f b) : is_exact_g f g :=
is_exact.mk H₁ H₂
definition is_exact.im_in_ker2 {A B : Type} {C : Set*} {f : A → B} {g : B → C} (H : is_exact f g)
{b : B} (h : image f b) : g b = pt :=
begin
induction h with a p, exact ap g p⁻¹ ⬝ is_exact.im_in_ker H a
end
definition is_exact_homotopy {A B : Type} {C : Type*} {f f' : A → B} {g g' : B → C}
(p : f ~ f') (q : g ~ g') (H : is_exact f g) : is_exact f' g' :=
begin
induction p using homotopy.rec_on_idp,
induction q using homotopy.rec_on_idp,
exact H
end
definition is_exact_trunc_functor {A B : Type} {C : Type*} {f : A → B} {g : B → C}
(H : is_exact_t f g) : @is_exact _ _ (ptrunc 0 C) (trunc_functor 0 f) (trunc_functor 0 g) :=
begin
constructor,
{ intro a, esimp, induction a with a,
exact ap tr (is_exact_t.im_in_ker H a) },
{ intro b p, induction b with b, note q := !tr_eq_tr_equiv p, induction q with q,
induction is_exact_t.ker_in_im H b q with a r,
exact image.mk (tr a) (ap tr r) }
end
definition is_contr_middle_of_is_exact {A B : Type} {C : Type*} {f : A → B} {g : B → C}
(H : is_exact f g) [is_contr A] [is_set B] [is_contr C] : is_contr B :=
begin
apply is_contr.mk (f pt),
intro b,
induction is_exact.ker_in_im H b !is_prop.elim,
exact ap f !is_prop.elim ⬝ p
end
definition is_surjective_of_is_exact_of_is_contr {A B : Type} {C : Type*} {f : A → B} {g : B → C}
(H : is_exact f g) [is_contr C] : is_surjective f :=
λb, is_exact.ker_in_im H b !is_prop.elim
definition is_embedding_of_is_exact_g {A B C : Group} {g : B →g C} {f : A →g B}
(gf : is_exact_g f g) [is_contr A] : is_embedding g :=
begin
apply to_is_embedding_homomorphism, intro a p,
induction is_exact.ker_in_im gf a p with x q,
exact q⁻¹ ⬝ ap f !is_prop.elim ⬝ to_respect_one f
end
definition map_left_of_is_exact {G₃' G₃ G₂ : Type} {G₁ : Type*}
{g : G₃ → G₂} {g' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f)
(Hg' : is_embedding g') : G₃ → G₃' :=
begin
intro a,
have fiber g' (g a),
begin
have is_prop (fiber g' (g a)), from !is_prop_fiber_of_is_embedding,
induction is_exact.ker_in_im H2 (g a) (is_exact.im_in_ker H1 a) with a' p,
exact fiber.mk a' p
end,
exact point this
end
definition map_left_of_is_exact_compute {G₃' G₃ G₂ : Type} {G₁ : Type*}
{g : G₃ → G₂} {g' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f)
(Hg' : is_embedding g') (a : G₃) : g' (map_left_of_is_exact H1 H2 Hg' a) = g a :=
@point_eq _ _ g' _ _
definition map_left_of_is_exact_compose {G₃'' G₃' G₃ G₂ : Type} {G₁ : Type*}
{g : G₃ → G₂} {g' : G₃' → G₂} {g'' : G₃'' → G₂} {f : G₂ → G₁}
(H1 : is_exact g f) (H2 : is_exact g' f) (H3 : is_exact g'' f)
(Hg' : is_embedding g') (Hg'' : is_embedding g'') (a : G₃) :
map_left_of_is_exact H2 H3 Hg'' (map_left_of_is_exact H1 H2 Hg' a) =
map_left_of_is_exact H1 H3 Hg'' a :=
begin
refine @is_injective_of_is_embedding _ _ g'' _ _ _ _,
refine !map_left_of_is_exact_compute ⬝ _ ⬝ !map_left_of_is_exact_compute⁻¹,
exact map_left_of_is_exact_compute H1 H2 Hg' a
end
definition map_left_of_is_exact_id {G₃ G₂ : Type} {G₁ : Type*}
{g : G₃ → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (Hg : is_embedding g) (a : G₃) :
map_left_of_is_exact H1 H1 Hg a = a :=
begin
refine @is_injective_of_is_embedding _ _ g _ _ _ _,
exact map_left_of_is_exact_compute H1 H1 Hg a
end
definition map_left_of_is_exact_homotopy {G₃' G₃ G₂ : Type} {G₁ : Type*}
{g : G₃ → G₂} {g' g'' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f)
(H3 : is_exact g'' f) (Hg' : is_embedding g') (Hg'' : is_embedding g'') (p : g' ~ g'') :
map_left_of_is_exact H1 H2 Hg' ~ map_left_of_is_exact H1 H3 Hg'' :=
begin
intro a,
refine @is_injective_of_is_embedding _ _ g' _ _ _ _,
exact !map_left_of_is_exact_compute ⬝ (!p ⬝ !map_left_of_is_exact_compute)⁻¹,
end
definition homomorphism_left_of_is_exact_g {G₃' G₃ G₂ G₁ : Group}
{g : G₃ →g G₂} {g' : G₃' →g G₂} {f : G₂ →g G₁} (H1 : is_exact_g g f) (H2 : is_exact_g g' f)
(Hg' : is_embedding g') : G₃ →g G₃' :=
begin
apply homomorphism.mk (map_left_of_is_exact H1 H2 Hg'),
{ intro a a', refine @is_injective_of_is_embedding _ _ g' _ _ _ _,
exact !point_eq ⬝ to_respect_mul g a a' ⬝
(to_respect_mul g' _ _ ⬝ ap011 mul !point_eq !point_eq)⁻¹ }
end
definition isomorphism_left_of_is_exact_g {G₃' G₃ G₂ G₁ : Group}
{g : G₃ →g G₂} {g' : G₃' →g G₂} {f : G₂ →g G₁} (H1 : is_exact g f) (H2 : is_exact g' f)
(Hg : is_embedding g) (Hg' : is_embedding g') : G₃ ≃g G₃' :=
begin
fapply isomorphism.mk, exact homomorphism_left_of_is_exact_g H1 H2 Hg',
fapply adjointify, exact homomorphism_left_of_is_exact_g H2 H1 Hg,
{ intro a, refine @is_injective_of_is_embedding _ _ g' _ _ _ _,
refine map_left_of_is_exact_compute H1 H2 Hg' _ ⬝ map_left_of_is_exact_compute H2 H1 Hg a },
{ intro a, refine @is_injective_of_is_embedding _ _ g _ _ _ _,
refine map_left_of_is_exact_compute H2 H1 Hg _ ⬝ map_left_of_is_exact_compute H1 H2 Hg' a },
end
definition is_exact_incl_of_subgroup {G H : Group} (f : G →g H) :
is_exact (incl_of_subgroup (kernel f)) f :=
begin
apply is_exact.mk,
{ intro x, cases x with x p, exact p },
{ intro x p, exact image.mk ⟨x, p⟩ idp }
end
definition isomorphism_kernel_of_is_exact {G₄ G₃ G₂ G₁ : Group}
{h : G₄ →g G₃} {g : G₃ →g G₂} {f : G₂ →g G₁} (H1 : is_exact h g) (H2 : is_exact g f)
(HG : is_contr G₄) : G₃ ≃g Kernel f :=
isomorphism_left_of_is_exact_g H2 (is_exact_incl_of_subgroup f)
(is_embedding_of_is_exact_g H1) (is_embedding_incl_of_subgroup _)
section chain_complex
open succ_str chain_complex
definition is_exact_of_is_exact_at {N : succ_str} {A : chain_complex N} {n : N}
(H : is_exact_at A n) : is_exact (cc_to_fn A (S n)) (cc_to_fn A n) :=
is_exact.mk (cc_is_chain_complex A n) H
end chain_complex
structure is_short_exact {A B : Type} {C : Type*} (f : A → B) (g : B → C) :=
(is_emb : is_embedding f)
(im_in_ker : Π(a:A), g (f a) = pt)
(ker_in_im : Π(b:B), (g b = pt) → image f b)
(is_surj : is_surjective g)
structure is_short_exact_t {A B : Type} {C : Type*} (f : A → B) (g : B → C) :=
(is_emb : is_embedding f)
(im_in_ker : Π(a:A), g (f a) = pt)
(ker_in_im : Π(b:B), (g b = pt) → fiber f b)
(is_surj : is_split_surjective g)
lemma is_short_exact_of_is_exact {X A B C Y : Group}
(k : X →g A) (f : A →g B) (g : B →g C) (l : C →g Y)
(hX : is_contr X) (hY : is_contr Y)
(kf : is_exact_g k f) (fg : is_exact_g f g) (gl : is_exact_g g l) : is_short_exact f g :=
begin
constructor,
{ exact is_embedding_of_is_exact_g kf },
{ exact is_exact.im_in_ker fg },
{ exact is_exact.ker_in_im fg },
{ intro c, exact is_exact.ker_in_im gl c !is_prop.elim },
end
lemma is_short_exact_equiv {A B A' B' : Type} {C C' : Type*}
{f' : A' → B'} {g' : B' → C'} (f : A → B) (g : B → C)
(eA : A ≃ A') (eB : B ≃ B') (eC : C ≃* C')
(h₁ : hsquare f f' eA eB) (h₂ : hsquare g g' eB eC)
(H : is_short_exact f' g') : is_short_exact f g :=
begin
constructor,
{ apply is_embedding_homotopy_closed_rev (homotopy_top_of_hsquare h₁),
apply is_embedding_compose, apply is_embedding_of_is_equiv,
apply is_embedding_compose, apply is_short_exact.is_emb H, apply is_embedding_of_is_equiv },
{ intro a, refine homotopy_top_of_hsquare' (hhconcat h₁ h₂) a ⬝ _,
refine ap eC⁻¹ _ ⬝ respect_pt eC⁻¹ᵉ*, exact is_short_exact.im_in_ker H (eA a) },
{ intro b p, note q := eq_of_inv_eq ((homotopy_top_of_hsquare' h₂ b)⁻¹ ⬝ p) ⬝ respect_pt eC,
induction is_short_exact.ker_in_im H (eB b) q with a' r,
apply image.mk (eA⁻¹ a'),
exact inj eB ((homotopy_top_of_hsquare h₁⁻¹ʰᵗʸᵛ a')⁻¹ ⬝ r) },
{ apply is_surjective_homotopy_closed_rev (homotopy_top_of_hsquare' h₂),
apply is_surjective_compose, apply is_surjective_of_is_equiv,
apply is_surjective_compose, apply is_short_exact.is_surj H, apply is_surjective_of_is_equiv }
end
lemma is_exact_of_is_short_exact {A B : Type} {C : Type*}
{f : A → B} {g : B → C} (H : is_short_exact f g) : is_exact f g :=
begin
constructor,
{ exact is_short_exact.im_in_ker H },
{ exact is_short_exact.ker_in_im H }
end
lemma is_equiv_left_of_is_short_exact {A B C : Group} {f : A →g B} {g : B →g C}
(H : is_short_exact f g) (HC : is_contr C) : is_equiv f :=
begin
apply is_equiv_of_is_surjective_of_is_embedding,
exact is_short_exact.is_emb H,
apply is_surjective_of_is_exact_of_is_contr, exact is_exact_of_is_short_exact H
end
lemma is_equiv_right_of_is_short_exact {A B C : Group} {f : A →g B} {g : B →g C}
(H : is_short_exact f g) (HA : is_contr A) : is_equiv g :=
begin
apply is_equiv_of_is_surjective_of_is_embedding,
apply is_embedding_of_is_exact_g, exact is_exact_of_is_short_exact H,
exact is_short_exact.is_surj H
end
definition is_contr_right_of_is_short_exact {A B : Type} {C : Type*} {f : A → B} {g : B → C}
(H : is_short_exact f g) (HB : is_contr B) (HC : is_set C) : is_contr C :=
is_contr_of_is_surjective g (is_short_exact.is_surj H) HB HC
definition is_contr_left_of_is_short_exact {A B : Type} {C : Type*} {f : A → B} {g : B → C}
(H : is_short_exact f g) (HB : is_contr B) (a₀ : A) : is_contr A :=
is_contr_of_is_embedding f (is_short_exact.is_emb H) _ a₀
definition is_short_exact_normal_subgroup {G : Group} (S : property G) [is_normal_subgroup G S] :
is_short_exact (incl_of_subgroup S) (qg_map S) :=
begin
fconstructor,
{ exact is_embedding_incl_of_subgroup S },
{ intro a, fapply qg_map_eq_one, induction a with b p, exact p },
{ intro b p, fapply image.mk,
{ apply sigma.mk b, fapply rel_of_qg_map_eq_one, exact p },
reflexivity },
{ exact is_surjective_qg_map S },
end
end algebra
|
756681364f685c12a4b283ea97a46b0ff44cf011 | 856e2e1615a12f95b551ed48fa5b03b245abba44 | /src/computability/problem.lean | a08e62a6e7a0615f85b8b1dd66dbf951edfa9a3f | [
"Apache-2.0"
] | permissive | pimsp/mathlib | 8b77e1ccfab21703ba8fbe65988c7de7765aa0e5 | 913318ca9d6979686996e8d9b5ebf7e74aae1c63 | refs/heads/master | 1,669,812,465,182 | 1,597,133,610,000 | 1,597,133,610,000 | 281,890,685 | 1 | 0 | null | 1,595,491,577,000 | 1,595,491,576,000 | null | UTF-8 | Lean | false | false | 9,954 | lean | import tactic
import computability.primrec
import computability.partrec
import computability.reduce
import computability.tm_to_partrec
import computability.turing_machine
import data.zmod.basic
import data.equiv.list
import data.list.basic
import .encode_alphabet
import .list_computable
open function
-- Some option construction
namespace option
def domain_add_option { α β : Type* } : ( α → option β ) → ( option α → option β ) := begin
intros f a,
cases a,
use none,
use f a,
end
@[simp]
lemma domain_add_option_of_some { α β : Type* } ( f : α → option β ) ( a : α ) : ( domain_add_option f ) ( some a ) = f a :=
begin
trivial,
end
end option
-- Problem namespace
namespace problem
open encoding
structure problem (α : Type) extends fin_encoding α:=
(yesinstance : α → Prop)
namespace examples
def is_even : problem ℕ := {
yesinstance := λ n, n%2 = 0,
..fin_encoding_nat_Γ₀₁
}
#check is_even.to_encoding.Γ
def is_odd : problem ℕ := {
yesinstance := λ n, n%2 = 1,
..fin_encoding_nat_Γ₀₁
}
end examples
def partrec {α σ} [encodable α] [encodable σ]
(f : α →. σ) := nat.partrec (λ n,
roption.bind (encodable.decode α n) (λ a, (f a).map encodable.encode))
def computable {α σ} [encodable α] [encodable σ] (f : α → σ) := partrec (f : α →. σ)
def many_one_reducible_partrec {α β : Type} [encodable α] [encodable β] (P : problem α) (Q : problem β) :=
∃ f : α → β, computable f ∧ ∀ a, P.yesinstance a ↔ Q.yesinstance (f a)
attribute [class] fin_encoding
def many_one_reducible_tm_aux {α β : Type} (ea : fin_encoding α) (eb : fin_encoding β) (P : problem α) (Q : problem β) :=
∃ f : α → β, @computable_by_tm_2 α β ea eb f ∧ ∀ a, P.yesinstance a ↔ Q.yesinstance (f a)
def many_one_reducible_tm {α β : Type} (P : problem α) (Q : problem β) :=
@many_one_reducible_tm_aux α β P.to_fin_encoding Q.to_fin_encoding P Q
infix ` ≤₀ `:1000 := many_one_reducible_partrec
infix ` ≤tm `:1000 := many_one_reducible_tm
--TODO: give a theorem many_one_reducible_partrec → many_one_reducible_tm (under the right conditions of course)
lemma is_even_red_to_is_odd: examples.is_even ≤tm examples.is_odd := begin
use nat.succ,
split,
sorry,
intro a,
change a ≡ 0 [MOD 2] ↔ a+1≡1 [MOD 2],
repeat {rw ← zmod.nat_coe_eq_nat_coe_iff},
simp,
end
lemma is_odd_red_to_is_even: examples.is_odd ≤tm examples.is_even := begin
use nat.succ,
split,
sorry,
intro a,
change a ≡ 1 [MOD 2] ↔ a+1≡0 [MOD 2],
repeat {rw ← zmod.nat_coe_eq_nat_coe_iff},
split, {
intro h,
simp [h],
ring,
},
intro h,
apply add_right_cancel,
simp at h,
rw h,
ring,
end
#check @computable_by_tm_2
#check problem _
@[refl]
lemma many_one_reducible.refl {α : Type} (P : problem α) :
P ≤tm P := ⟨@id α, (@id_computable α P.to_fin_encoding), by simp⟩
theorem many_one_reducible.trans {α β γ : Type} {P : problem α} {Q : problem β} {R : problem γ} : P ≤tm Q → Q ≤tm R → P ≤tm R
:= sorry
-- | ⟨f, c₁, h₁⟩ ⟨g, c₂, h₂⟩ := ⟨g ∘ f, c₂.comp c₁,
-- λ a, ⟨λ h, by rwa [←h₂, ←h₁], λ h, by rwa [h₁, h₂]⟩⟩
@[derive inhabited]
inductive propositional_formula (α : Type*)
| atom (a:α) : propositional_formula
| conj (a b: propositional_formula) : propositional_formula
| disj (a b: propositional_formula) : propositional_formula
| not (a:propositional_formula) : propositional_formula
-- Postfix territory
namespace propositional_formula
def to_postfix : (propositional_formula ℕ) → list ℕ
| (atom a) := [a+3]
| (conj a b) := (to_postfix a) ++ (to_postfix b) ++ [1]
| (disj a b) := (to_postfix a) ++ (to_postfix b) ++ [2]
| (not a) := (to_postfix a) ++ [0]
def from_postfix' : list ℕ → list ( propositional_formula ℕ ) → list ℕ × list ( propositional_formula ℕ )
| [] [f] := ([],[f])
| [] m := ([],[])
| (0::l) (f::m) := from_postfix' l ((not f)::m)
| (0::l) nil := ([],[])
| (1::l) (f::(g::m)) := from_postfix' l ((conj g f)::m)
| (1::l) m := ([],[])
| (2::l) (f::(g::m)) := from_postfix' l ((disj g f)::m)
| (2::l) m := ([],[])
| ((b+3)::l) m := from_postfix' l ((atom b)::m)
def from_postfix : list ℕ → option ( propositional_formula ℕ ) := λ l, list.head' ( from_postfix' l [] ).2
lemma from_to_postfix_id_aux (φ:propositional_formula ℕ) : ∀ l₁:list ℕ, ∀ l₂:list (propositional_formula ℕ), from_postfix' ( φ.to_postfix ++ l₁ ) l₂ = from_postfix' l₁ ( φ :: l₂ ) :=
begin
induction φ, {
intros l₁ l₂,
trivial,
}, {
intros l₁ l₂,
specialize φ_ih_a (φ_b.to_postfix ++ (1 :: l₁) ) l₂,
specialize φ_ih_b (1 :: l₁) (φ_a :: l₂),
calc from_postfix' ((φ_a.conj φ_b).to_postfix ++ l₁) l₂
= from_postfix' ( φ_a.to_postfix ++ φ_b.to_postfix ++ [1] ++ l₁ ) l₂: by trivial
... = from_postfix' ( φ_a.to_postfix ++ ( φ_b.to_postfix ++ ( 1 :: l₁ ) ) ) l₂: by simp[list.append_assoc]
... = from_postfix' ( φ_b.to_postfix ++ (1 :: l₁) ) (φ_a :: l₂) : φ_ih_a
... = from_postfix' ( 1 :: l₁ ) ( φ_b :: φ_a :: l₂) : φ_ih_b
... = from_postfix' l₁ (φ_a.conj φ_b :: l₂) : by trivial,
}, {
intros l₁ l₂,
specialize φ_ih_a (φ_b.to_postfix ++ (2 :: l₁) ) l₂,
specialize φ_ih_b (2 :: l₁) (φ_a :: l₂),
calc from_postfix' ((φ_a.disj φ_b).to_postfix ++ l₁) l₂
= from_postfix' ( φ_a.to_postfix ++ φ_b.to_postfix ++ [2] ++ l₁ ) l₂: by trivial
... = from_postfix' ( φ_a.to_postfix ++ ( φ_b.to_postfix ++ ( 2 :: l₁ ) ) ) l₂: by simp[list.append_assoc]
... = from_postfix' ( φ_b.to_postfix ++ (2 :: l₁) ) (φ_a :: l₂) : φ_ih_a
... = from_postfix' ( 2 :: l₁ ) ( φ_b :: φ_a :: l₂) : φ_ih_b
... = from_postfix' l₁ (φ_a.disj φ_b :: l₂) : by trivial,
}, {
intros l₁ l₂,
specialize φ_ih (0 :: l₁) l₂,
calc from_postfix' ( (φ_a.not).to_postfix ++ l₁) l₂
= from_postfix' ( φ_a.to_postfix ++ [0] ++ l₁ ) l₂: by trivial
... = from_postfix' ( φ_a.to_postfix ++ ( 0 :: l₁ ) ) l₂: by simp[list.append_assoc]
... = from_postfix' ( 0 :: l₁ ) ( φ_a :: l₂) : φ_ih
... = from_postfix' l₁ (φ_a.not :: l₂) : by trivial,
},
end
end propositional_formula
namespace encodable
open propositional_formula
instance propositional_formula_nat : encodable (propositional_formula ℕ) := {
encode := encodable.list.encode ∘ to_postfix,
decode := ( option.domain_add_option from_postfix ) ∘ encodable.list.decode,
encodek := begin
intro φ,
repeat {rw comp_app},
let el := encodable.list.encodek,
specialize el φ.to_postfix,
rw el,
simp,
clear el,
change list.head' ( from_postfix' (to_postfix φ) [] ).2 = some φ,
let from_to_postfix_id := from_to_postfix_id_aux φ [] [],
simp at from_to_postfix_id,
rw from_to_postfix_id,
trivial,
end,
}
end encodable
-- End postfix territory
namespace propositional_formula
def eval {α : Type*} (f : α → Prop) : (propositional_formula α) → Prop
| (atom a) := f a
| (conj a b) := (eval a) ∧ (eval b)
| (disj a b) := (eval a) ∨ (eval b)
| (not a) := ¬(eval a)
-- propositional_formula.eval a = run tm_van_Daan (encode a)
-- note that singleton a = a is also a rec_list (but not a list)
-- inductive rec_list (α : Type*)
-- | nil : rec_list
-- | singleton (a:α) : rec_list
-- | singlelist (a : rec_list) : rec_list
-- | cons (hd : rec_list) (tl : rec_list) : rec_list -- think of this as [hd] concatenated with tl
-- notation h :: t := rec_list.cons h t
-- notation `[` l:(foldr `, ` (h t, rec_list.cons h t) rec_list.nil `]`) := l
-- -- def encode_to_list {α : Type*} [h: primcodable α]: (propositional_formula α) → list ℕ
-- def encode_to_list : (propositional_formula ℕ) → rec_list ℕ
-- | (atom a) := rec_list.singleton a
-- | (conj a b) := [(encode_to_list a),rec_list.singleton 0,(encode_to_list b)]
-- | (disj a b) := [(encode_to_list a),rec_list.singleton 1,(encode_to_list b)]
-- | (not a) := [rec_list.singleton 0,(encode_to_list a)]
-- def decode_from_list : rec_list ℕ → option (propositional_formula ℕ)
-- | rec_list.nil : begin
end propositional_formula
def is_satisfiable {α : Type*} (p : propositional_formula α) : Prop :=
∃ f : α → Prop, p.eval f
def sat : problem (propositional_formula ℕ) :=
{ yesinstance := λ p, is_satisfiable p }
-- namespace tm0
-- section
-- parameters (Λ : Type*) [inhabited Λ]
-- def machine := turing.TM0.machine Γ₀₁ Λ
-- def list_to_list_blank {Γ : Type} [inhabited Γ] (L : list Γ) : turing.list_blank Γ :=
-- @quotient.mk (list Γ) (turing.blank_rel.setoid Γ) L
-- def run_tm0 {α : Type} [encodable α] (tm : machine) (a : α) : roption (turing.list_blank Γ₀₁) :=
-- turing.TM0.eval tm (encode_nat (encodable.encode a))
-- def solved_by_turing_machine_0 {α : Type} [encodable α] (P : problem α) (tm : machine) : Prop := (λ (a : α), run_tm0 tm a = roption.some (list_to_list_blank [Γ₀₁.bit1])) = P.yesinstance ∧ ∀ (a : α), run_tm0 tm a ≠ roption.none
-- end
-- end tm0
-- structure turing_machine_0 :=
-- (Λ : Type*)
-- [Λ_inhabited : inhabited Λ]
-- (M : tm0.machine Λ)
-- def solvable_by_turing_machine_0 {α : Type*} [encodable α] (P : problem α) : Prop :=
-- ∃ (tm : turing_machine_0), @tm0.solved_by_turing_machine_0 tm.Λ tm.Λ_inhabited _ _ P tm.M
def prototypical_problem : problem bool :=
{ yesinstance := λ b, b = tt,
..encoding_bool_Γ₀₁}
def is_in_P {α : Type} (P : problem α) : Prop := P ≤tm prototypical_problem
def leq_P_to_in_P {α β : Type} (P : problem α) (Q : problem β) (hpq: P ≤tm Q) (hq : is_in_P Q) : is_in_P P :=
many_one_reducible.trans hpq hq
def prototypical_problem_in_P : is_in_P prototypical_problem := many_one_reducible.refl _
end problem
|
5264b53d35c323ebc60f9447b4b8f420c7389ec3 | 92b50235facfbc08dfe7f334827d47281471333b | /library/data/finset/comb.lean | d7ff2e4b909d45de583f96bb58db09909109ffdb | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 12,242 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Jeremy Avigad
Combinators for finite sets.
-/
import data.finset.basic logic.identities
open list quot subtype decidable perm function
namespace finset
/- image (corresponds to map on list) -/
section image
variables {A B : Type}
variable [h : decidable_eq B]
include h
definition image (f : A → B) (s : finset A) : finset B :=
quot.lift_on s
(λ l, to_finset (list.map f (elt_of l)))
(λ l₁ l₂ p, quot.sound (perm_erase_dup_of_perm (perm_map _ p)))
theorem image_empty (f : A → B) : image f ∅ = ∅ :=
rfl
theorem mem_image_of_mem (f : A → B) {s : finset A} {a : A} : a ∈ s → f a ∈ image f s :=
quot.induction_on s (take l, assume H : a ∈ elt_of l, mem_to_finset (mem_map f H))
theorem mem_image_of_mem_of_eq {f : A → B} {s : finset A} {a : A} {b : B}
(H1 : a ∈ s) (H2 : f a = b) :
b ∈ image f s :=
eq.subst H2 (mem_image_of_mem f H1)
theorem exists_of_mem_image {f : A → B} {s : finset A} {b : B} :
b ∈ image f s → ∃a, a ∈ s ∧ f a = b :=
quot.induction_on s
(take l, assume H : b ∈ erase_dup (list.map f (elt_of l)),
exists_of_mem_map (mem_of_mem_erase_dup H))
theorem mem_image_iff (f : A → B) {s : finset A} {y : B} : y ∈ image f s ↔ ∃x, x ∈ s ∧ f x = y :=
iff.intro exists_of_mem_image
(assume H,
obtain x (H₁ : x ∈ s) (H₂ : f x = y), from H,
mem_image_of_mem_of_eq H₁ H₂)
theorem mem_image_eq (f : A → B) {s : finset A} {y : B} : y ∈ image f s = ∃x, x ∈ s ∧ f x = y :=
propext (mem_image_iff f)
theorem mem_image_of_mem_image_of_subset {f : A → B} {s t : finset A} {y : B}
(H1 : y ∈ image f s) (H2 : s ⊆ t) : y ∈ image f t :=
obtain x (H3: x ∈ s) (H4 : f x = y), from exists_of_mem_image H1,
have H5 : x ∈ t, from mem_of_subset_of_mem H2 H3,
show y ∈ image f t, from mem_image_of_mem_of_eq H5 H4
theorem image_insert [h' : decidable_eq A] (f : A → B) (s : finset A) (a : A) :
image f (insert a s) = insert (f a) (image f s) :=
ext (take y, iff.intro
(assume H : y ∈ image f (insert a s),
obtain x (H1l : x ∈ insert a s) (H1r :f x = y), from exists_of_mem_image H,
have H2 : x = a ∨ x ∈ s, from eq_or_mem_of_mem_insert H1l,
or.elim H2
(assume H3 : x = a,
have H4 : f a = y, from eq.subst H3 H1r,
show y ∈ insert (f a) (image f s), from eq.subst H4 !mem_insert)
(assume H3 : x ∈ s,
have H5 : f x ∈ image f s, from mem_image_of_mem f H3,
show y ∈ insert (f a) (image f s), from eq.subst H1r (mem_insert_of_mem _ H5)))
(assume H : y ∈ insert (f a) (image f s),
have H1 : y = f a ∨ y ∈ image f s, from eq_or_mem_of_mem_insert H,
or.elim H1
(assume H2 : y = f a,
have H3 : f a ∈ image f (insert a s), from mem_image_of_mem f !mem_insert,
show y ∈ image f (insert a s), from eq.subst (eq.symm H2) H3)
(assume H2 : y ∈ image f s,
show y ∈ image f (insert a s), from mem_image_of_mem_image_of_subset H2 !subset_insert)))
lemma image_compose {C : Type} [deceqC : decidable_eq C] {f : B → C} {g : A → B} {s : finset A} :
image (f∘g) s = image f (image g s) :=
ext (take z, iff.intro
(assume Hz : z ∈ image (f∘g) s,
obtain x (Hx : x ∈ s) (Hgfx : f (g x) = z), from exists_of_mem_image Hz,
by rewrite -Hgfx; apply mem_image_of_mem _ (mem_image_of_mem _ Hx))
(assume Hz : z ∈ image f (image g s),
obtain y (Hy : y ∈ image g s) (Hfy : f y = z), from exists_of_mem_image Hz,
obtain x (Hx : x ∈ s) (Hgx : g x = y), from exists_of_mem_image Hy,
mem_image_of_mem_of_eq Hx (by esimp; rewrite [Hgx, Hfy])))
end image
/- filter and set-builder notation -/
section filter
variables {A : Type} [deceq : decidable_eq A]
include deceq
variables (p : A → Prop) [decp : decidable_pred p] (s : finset A) {x : A}
include decp
definition filter : finset A :=
quot.lift_on s
(λl, to_finset_of_nodup
(list.filter p (subtype.elt_of l))
(list.nodup_filter p (subtype.has_property l)))
(λ l₁ l₂ u, quot.sound (perm.perm_filter u))
notation `{` binders ∈ s `|` r:(scoped:1 p, filter p s) `}` := r
theorem filter_empty : filter p ∅ = ∅ := rfl
variables {p s}
theorem of_mem_filter : x ∈ filter p s → p x :=
quot.induction_on s (take l, list.of_mem_filter)
theorem mem_of_mem_filter : x ∈ filter p s → x ∈ s :=
quot.induction_on s (take l, list.mem_of_mem_filter)
theorem mem_filter_of_mem {x : A} : x ∈ s → p x → x ∈ filter p s :=
quot.induction_on s (take l, list.mem_filter_of_mem)
variables (p s)
theorem mem_filter_iff : x ∈ filter p s ↔ x ∈ s ∧ p x :=
iff.intro
(assume H, and.intro (mem_of_mem_filter H) (of_mem_filter H))
(assume H, mem_filter_of_mem (and.left H) (and.right H))
theorem mem_filter_eq : x ∈ filter p s = (x ∈ s ∧ p x) :=
propext !mem_filter_iff
end filter
/- set difference -/
section diff
variables {A : Type} [deceq : decidable_eq A]
include deceq
definition diff (s t : finset A) : finset A := {x ∈ s | x ∉ t}
infix `\`:70 := diff
theorem mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∈ s :=
mem_of_mem_filter H
theorem not_mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∉ t :=
of_mem_filter H
theorem mem_diff {s t : finset A} {x : A} (H1 : x ∈ s) (H2 : x ∉ t) : x ∈ s \ t :=
mem_filter_of_mem H1 H2
theorem mem_diff_iff (s t : finset A) (x : A) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t :=
iff.intro
(assume H, and.intro (mem_of_mem_diff H) (not_mem_of_mem_diff H))
(assume H, mem_diff (and.left H) (and.right H))
theorem mem_diff_eq (s t : finset A) (x : A) : x ∈ s \ t = (x ∈ s ∧ x ∉ t) :=
propext !mem_diff_iff
theorem union_diff_cancel {s t : finset A} (H : s ⊆ t) : s ∪ (t \ s) = t :=
ext (take x, iff.intro
(assume H1 : x ∈ s ∪ (t \ s),
or.elim (mem_or_mem_of_mem_union H1)
(assume H2 : x ∈ s, mem_of_subset_of_mem H H2)
(assume H2 : x ∈ t \ s, mem_of_mem_diff H2))
(assume H1 : x ∈ t,
decidable.by_cases
(assume H2 : x ∈ s, mem_union_left _ H2)
(assume H2 : x ∉ s, mem_union_right _ (mem_diff H1 H2))))
theorem diff_union_cancel {s t : finset A} (H : s ⊆ t) : (t \ s) ∪ s = t :=
eq.subst !union.comm (!union_diff_cancel H)
end diff
/- all -/
section all
variables {A : Type}
definition all (s : finset A) (p : A → Prop) : Prop :=
quot.lift_on s
(λ l, all (elt_of l) p)
(λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !and.left_comm) p true)
theorem all_empty (p : A → Prop) : all ∅ p = true :=
rfl
theorem of_mem_of_all {p : A → Prop} {a : A} {s : finset A} : a ∈ s → all s p → p a :=
quot.induction_on s (λ l i h, list.of_mem_of_all i h)
theorem forall_of_all {p : A → Prop} {s : finset A} (H : all s p) : ∀{a}, a ∈ s → p a :=
λ a H', of_mem_of_all H' H
theorem all_of_forall {p : A → Prop} {s : finset A} : (∀a, a ∈ s → p a) → all s p :=
quot.induction_on s (λ l H, list.all_of_forall H)
theorem all_iff_forall (p : A → Prop) (s : finset A) : all s p ↔ (∀a, a ∈ s → p a) :=
iff.intro forall_of_all all_of_forall
definition decidable_all [instance] (p : A → Prop) [h : decidable_pred p] (s : finset A) :
decidable (all s p) :=
quot.rec_on_subsingleton s (λ l, list.decidable_all p (elt_of l))
theorem all_implies {p q : A → Prop} {s : finset A} : all s p → (∀ x, p x → q x) → all s q :=
quot.induction_on s (λ l h₁ h₂, list.all_implies h₁ h₂)
variable [h : decidable_eq A]
include h
theorem all_union {p : A → Prop} {s₁ s₂ : finset A} : all s₁ p → all s₂ p → all (s₁ ∪ s₂) p :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a₁ a₂, all_union a₁ a₂)
theorem all_of_all_union_left {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₁ p :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_left a)
theorem all_of_all_union_right {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₂ p :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_right a)
theorem all_insert_of_all {p : A → Prop} {a : A} {s : finset A} : p a → all s p → all (insert a s) p :=
quot.induction_on s (λ l h₁ h₂, list.all_insert_of_all h₁ h₂)
theorem all_erase_of_all {p : A → Prop} (a : A) {s : finset A}: all s p → all (erase a s) p :=
quot.induction_on s (λ l h, list.all_erase_of_all a h)
theorem all_inter_of_all_left {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₁ p → all (s₁ ∩ s₂) p :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_left _ h)
theorem all_inter_of_all_right {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₂ p → all (s₁ ∩ s₂) p :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_right _ h)
theorem subset_iff_all (s t : finset A) : s ⊆ t ↔ all s (λ x, x ∈ t) :=
iff.intro
(assume H : s ⊆ t, all_of_forall (take x, assume H1, mem_of_subset_of_mem H H1))
(assume H : all s (λ x, x ∈ t), subset_of_forall (take x, assume H1 : x ∈ s, of_mem_of_all H1 H))
definition decidable_subset [instance] (s t : finset A) : decidable (s ⊆ t) :=
decidable_of_decidable_of_iff _ (iff.symm !subset_iff_all)
end all
/- any -/
section any
variables {A : Type}
definition any (s : finset A) (p : A → Prop) : Prop :=
quot.lift_on s
(λ l, any (elt_of l) p)
(λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !or.left_comm) p false)
theorem any_empty (p : A → Prop) : any ∅ p = false := rfl
theorem exists_of_any {p : A → Prop} {s : finset A} : any s p → ∃a, a ∈ s ∧ p a :=
quot.induction_on s (λ l H, list.exists_of_any H)
theorem any_of_mem {p : A → Prop} {s : finset A} {a : A} : a ∈ s → p a → any s p :=
quot.induction_on s (λ l H1 H2, list.any_of_mem H1 H2)
theorem any_of_exists {p : A → Prop} {s : finset A} (H : ∃a, a ∈ s ∧ p a) : any s p :=
obtain a H₁ H₂, from H,
any_of_mem H₁ H₂
theorem any_iff_exists (p : A → Prop) (s : finset A) : any s p ↔ (∃a, a ∈ s ∧ p a) :=
iff.intro exists_of_any any_of_exists
theorem any_of_insert [h : decidable_eq A] {p : A → Prop} (s : finset A) {a : A} (H : p a) :
any (insert a s) p :=
any_of_mem (mem_insert a s) H
theorem any_of_insert_right [h : decidable_eq A] {p : A → Prop} {s : finset A} (a : A) (H : any s p) :
any (insert a s) p :=
obtain b (H₁ : b ∈ s) (H₂ : p b), from exists_of_any H,
any_of_mem (mem_insert_of_mem a H₁) H₂
definition decidable_any [instance] (p : A → Prop) [h : decidable_pred p] (s : finset A) :
decidable (any s p) :=
quot.rec_on_subsingleton s (λ l, list.decidable_any p (elt_of l))
end any
section product
variables {A B : Type}
definition product (s₁ : finset A) (s₂ : finset B) : finset (A × B) :=
quot.lift_on₂ s₁ s₂
(λ l₁ l₂,
to_finset_of_nodup (product (elt_of l₁) (elt_of l₂))
(nodup_product (has_property l₁) (has_property l₂)))
(λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_product p₁ p₂))
infix * := product
theorem empty_product (s : finset B) : @empty A * s = ∅ :=
quot.induction_on s (λ l, rfl)
theorem mem_product {a : A} {b : B} {s₁ : finset A} {s₂ : finset B}
: a ∈ s₁ → b ∈ s₂ → (a, b) ∈ s₁ * s₂ :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i₁ i₂, list.mem_product i₁ i₂)
theorem mem_of_mem_product_left {a : A} {b : B} {s₁ : finset A} {s₂ : finset B}
: (a, b) ∈ s₁ * s₂ → a ∈ s₁ :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_left i)
theorem mem_of_mem_product_right {a : A} {b : B} {s₁ : finset A} {s₂ : finset B}
: (a, b) ∈ s₁ * s₂ → b ∈ s₂ :=
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_right i)
theorem product_empty (s : finset A) : s * @empty B = ∅ :=
ext (λ p,
match p with
| (a, b) := iff.intro
(λ i, absurd (mem_of_mem_product_right i) !not_mem_empty)
(λ i, absurd i !not_mem_empty)
end)
end product
end finset
|
24af1928aab0a3dc4df54845811d47a448326c47 | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/algebra/opposites.lean | 2c033b9a2f05745317c5be15edd46d4bdf48cc63 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 7,131 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Opposites.
-/
import data.opposite
import algebra.field
namespace opposite
universes u
variables (α : Type u)
instance [has_add α] : has_add (opposite α) :=
{ add := λ x y, op (unop x + unop y) }
instance [add_semigroup α] : add_semigroup (opposite α) :=
{ add_assoc := λ x y z, unop_injective $ add_assoc (unop x) (unop y) (unop z),
.. opposite.has_add α }
instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup (opposite α) :=
{ add_left_cancel := λ x y z H, unop_injective $ add_left_cancel $ op_injective H,
.. opposite.add_semigroup α }
instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup (opposite α) :=
{ add_right_cancel := λ x y z H, unop_injective $ add_right_cancel $ op_injective H,
.. opposite.add_semigroup α }
instance [add_comm_semigroup α] : add_comm_semigroup (opposite α) :=
{ add_comm := λ x y, unop_injective $ add_comm (unop x) (unop y),
.. opposite.add_semigroup α }
instance [has_zero α] : has_zero (opposite α) :=
{ zero := op 0 }
instance [nontrivial α] : nontrivial (opposite α) :=
let ⟨x, y, h⟩ := exists_pair_ne α in nontrivial_of_ne (op x) (op y) (op_injective.ne h)
section
local attribute [reducible] opposite
@[simp] lemma unop_eq_zero_iff [has_zero α] (a : αᵒᵖ) : a.unop = (0 : α) ↔ a = (0 : αᵒᵖ) :=
iff.refl _
@[simp] lemma op_eq_zero_iff [has_zero α] (a : α) : op a = (0 : αᵒᵖ) ↔ a = (0 : α) :=
iff.refl _
end
instance [add_monoid α] : add_monoid (opposite α) :=
{ zero_add := λ x, unop_injective $ zero_add $ unop x,
add_zero := λ x, unop_injective $ add_zero $ unop x,
.. opposite.add_semigroup α, .. opposite.has_zero α }
instance [add_comm_monoid α] : add_comm_monoid (opposite α) :=
{ .. opposite.add_monoid α, .. opposite.add_comm_semigroup α }
instance [has_neg α] : has_neg (opposite α) :=
{ neg := λ x, op $ -(unop x) }
instance [add_group α] : add_group (opposite α) :=
{ add_left_neg := λ x, unop_injective $ add_left_neg $ unop x,
.. opposite.add_monoid α, .. opposite.has_neg α }
instance [add_comm_group α] : add_comm_group (opposite α) :=
{ .. opposite.add_group α, .. opposite.add_comm_monoid α }
instance [has_mul α] : has_mul (opposite α) :=
{ mul := λ x y, op (unop y * unop x) }
instance [semigroup α] : semigroup (opposite α) :=
{ mul_assoc := λ x y z, unop_injective $ eq.symm $ mul_assoc (unop z) (unop y) (unop x),
.. opposite.has_mul α }
instance [right_cancel_semigroup α] : left_cancel_semigroup (opposite α) :=
{ mul_left_cancel := λ x y z H, unop_injective $ mul_right_cancel $ op_injective H,
.. opposite.semigroup α }
instance [left_cancel_semigroup α] : right_cancel_semigroup (opposite α) :=
{ mul_right_cancel := λ x y z H, unop_injective $ mul_left_cancel $ op_injective H,
.. opposite.semigroup α }
instance [comm_semigroup α] : comm_semigroup (opposite α) :=
{ mul_comm := λ x y, unop_injective $ mul_comm (unop y) (unop x),
.. opposite.semigroup α }
instance [has_one α] : has_one (opposite α) :=
{ one := op 1 }
section
local attribute [reducible] opposite
@[simp] lemma unop_eq_one_iff [has_one α] (a : αᵒᵖ) : a.unop = 1 ↔ a = 1 :=
iff.refl _
@[simp] lemma op_eq_one_iff [has_one α] (a : α) : op a = 1 ↔ a = 1 :=
iff.refl _
end
instance [monoid α] : monoid (opposite α) :=
{ one_mul := λ x, unop_injective $ mul_one $ unop x,
mul_one := λ x, unop_injective $ one_mul $ unop x,
.. opposite.semigroup α, .. opposite.has_one α }
instance [comm_monoid α] : comm_monoid (opposite α) :=
{ .. opposite.monoid α, .. opposite.comm_semigroup α }
instance [has_inv α] : has_inv (opposite α) :=
{ inv := λ x, op $ (unop x)⁻¹ }
instance [group α] : group (opposite α) :=
{ mul_left_inv := λ x, unop_injective $ mul_inv_self $ unop x,
.. opposite.monoid α, .. opposite.has_inv α }
instance [comm_group α] : comm_group (opposite α) :=
{ .. opposite.group α, .. opposite.comm_monoid α }
instance [distrib α] : distrib (opposite α) :=
{ left_distrib := λ x y z, unop_injective $ add_mul (unop y) (unop z) (unop x),
right_distrib := λ x y z, unop_injective $ mul_add (unop z) (unop x) (unop y),
.. opposite.has_add α, .. opposite.has_mul α }
instance [semiring α] : semiring (opposite α) :=
{ zero_mul := λ x, unop_injective $ mul_zero $ unop x,
mul_zero := λ x, unop_injective $ zero_mul $ unop x,
.. opposite.add_comm_monoid α, .. opposite.monoid α, .. opposite.distrib α }
instance [ring α] : ring (opposite α) :=
{ .. opposite.add_comm_group α, .. opposite.monoid α, .. opposite.semiring α }
instance [comm_ring α] : comm_ring (opposite α) :=
{ .. opposite.ring α, .. opposite.comm_semigroup α }
instance [integral_domain α] : integral_domain (opposite α) :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ x y (H : op (_ * _) = op (0:α)),
or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ op_injective H)
(λ hy, or.inr $ unop_injective $ hy) (λ hx, or.inl $ unop_injective $ hx),
.. opposite.comm_ring α, .. opposite.nontrivial α }
instance [field α] : field (opposite α) :=
{ mul_inv_cancel := λ x hx, unop_injective $ inv_mul_cancel $ λ hx', hx $ unop_injective hx',
inv_zero := unop_injective inv_zero,
.. opposite.comm_ring α, .. opposite.has_inv α, .. opposite.nontrivial α }
@[simp] lemma op_zero [has_zero α] : op (0 : α) = 0 := rfl
@[simp] lemma unop_zero [has_zero α] : unop (0 : αᵒᵖ) = 0 := rfl
@[simp] lemma op_one [has_one α] : op (1 : α) = 1 := rfl
@[simp] lemma unop_one [has_one α] : unop (1 : αᵒᵖ) = 1 := rfl
variable {α}
@[simp] lemma op_add [has_add α] (x y : α) : op (x + y) = op x + op y := rfl
@[simp] lemma unop_add [has_add α] (x y : αᵒᵖ) : unop (x + y) = unop x + unop y := rfl
@[simp] lemma op_neg [has_neg α] (x : α) : op (-x) = -op x := rfl
@[simp] lemma unop_neg [has_neg α] (x : αᵒᵖ) : unop (-x) = -unop x := rfl
@[simp] lemma op_mul [has_mul α] (x y : α) : op (x * y) = op y * op x := rfl
@[simp] lemma unop_mul [has_mul α] (x y : αᵒᵖ) : unop (x * y) = unop y * unop x := rfl
@[simp] lemma op_inv [has_inv α] (x : α) : op (x⁻¹) = (op x)⁻¹ := rfl
@[simp] lemma unop_inv [has_inv α] (x : αᵒᵖ) : unop (x⁻¹) = (unop x)⁻¹ := rfl
@[simp] lemma op_sub [add_group α] (x y : α) : op (x - y) = op x - op y := rfl
@[simp] lemma unop_sub [add_group α] (x y : αᵒᵖ) : unop (x - y) = unop x - unop y := rfl
/-- The function `op` is a homomorphism of additive commutative monoids. -/
def op_add_hom [add_comm_monoid α] : α →+ αᵒᵖ := ⟨op, op_zero α, op_add⟩
/-- The function `unop` is a homomorphism of additive commutative monoids. -/
def unop_add_hom [add_comm_monoid α] : αᵒᵖ →+ α := ⟨unop, unop_zero α, unop_add⟩
@[simp] lemma coe_op_add_hom [add_comm_monoid α] : (op_add_hom : α → αᵒᵖ) = op := rfl
@[simp] lemma coe_unop_add_hom [add_comm_monoid α] : (unop_add_hom : αᵒᵖ → α) = unop := rfl
end opposite
|
6f5820099d4ccc4648ad9e43d703e762311bab43 | d9ed0fce1c218297bcba93e046cb4e79c83c3af8 | /library/init/propext.lean | cef8d18894d98674c7e3bf691d70a7593523e302 | [
"Apache-2.0"
] | permissive | leodemoura/lean_clone | 005c63aa892a6492f2d4741ee3c2cb07a6be9d7f | cc077554b584d39bab55c360bc12a6fe7957afe6 | refs/heads/master | 1,610,506,475,484 | 1,482,348,354,000 | 1,482,348,543,000 | 77,091,586 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 766 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import init.logic
constant propext {a b : Prop} : (a ↔ b) → a = b
/- Additional congruence lemmas. -/
universe variables u v
lemma forall_congr_eq {a : Type u} {p q : a → Prop} (h : ∀ x, p x = q x) : (∀ x, p x) = ∀ x, q x :=
propext (forall_congr (λ a, (h a)^.to_iff))
lemma imp_congr_eq {a b c d : Prop} (h₁ : a = c) (h₂ : b = d) : (a → b) = (c → d) :=
propext (imp_congr h₁^.to_iff h₂^.to_iff)
lemma imp_congr_ctx_eq {a b c d : Prop} (h₁ : a = c) (h₂ : c → (b = d)) : (a → b) = (c → d) :=
propext (imp_congr_ctx h₁^.to_iff (λ hc, (h₂ hc)^.to_iff))
|
70ecd6dd04da7c3cf1dc3395042f2eed310dbe3d | 367134ba5a65885e863bdc4507601606690974c1 | /src/geometry/manifold/algebra/smooth_functions.lean | 77cfd2f3e937e6a8c6918cc37aa13976ba49649b | [
"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 | 6,973 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri
-/
import geometry.manifold.algebra.structures
import geometry.manifold.times_cont_mdiff_map
/-!
# Algebraic structures over smooth functions
In this file, we define instances of algebraic structures over smooth functions.
-/
noncomputable theory
open_locale manifold
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{N : Type*} [topological_space N] [charted_space H N]
namespace smooth_map
@[to_additive]
instance has_mul {G : Type*} [has_mul G] [topological_space G] [charted_space H' G]
[has_smooth_mul I' G] :
has_mul C^∞⟮I, N; I', G⟯ :=
⟨λ f g, ⟨f * g, f.smooth.mul g.smooth⟩⟩
@[to_additive]
instance has_one {G : Type*} [monoid G] [topological_space G] [charted_space H' G] :
has_one C^∞⟮I, N; I', G⟯ :=
⟨times_cont_mdiff_map.const (1 : G)⟩
end smooth_map
section group_structure
/-!
### Group structure
In this section we show that smooth functions valued in a Lie group inherit a group structure
under pointwise multiplication.
-/
@[to_additive]
instance smooth_map_semigroup {G : Type*} [semigroup G] [topological_space G]
[charted_space H' G] [has_smooth_mul I' G] :
semigroup C^∞⟮I, N; I', G⟯ :=
{ mul_assoc := λ a b c, by ext; exact mul_assoc _ _ _,
..smooth_map.has_mul}
@[to_additive]
instance smooth_map_monoid {G : Type*} [monoid G] [topological_space G]
[charted_space H' G] [has_smooth_mul I' G] :
monoid C^∞⟮I, N; I', G⟯ :=
{ one_mul := λ a, by ext; exact one_mul _,
mul_one := λ a, by ext; exact mul_one _,
..smooth_map_semigroup,
..smooth_map.has_one }
@[to_additive]
instance smooth_map_comm_monoid {G : Type*} [comm_monoid G] [topological_space G]
[charted_space H' G] [has_smooth_mul I' G] :
comm_monoid C^∞⟮I, N; I', G⟯ :=
{ mul_comm := λ a b, by ext; exact mul_comm _ _,
..smooth_map_monoid,
..smooth_map.has_one }
@[to_additive]
instance smooth_map_group {G : Type*} [group G] [topological_space G]
[charted_space H' G] [lie_group I' G] :
group C^∞⟮I, N; I', G⟯ :=
{ inv := λ f, ⟨λ x, (f x)⁻¹, f.smooth.inv⟩,
mul_left_inv := λ a, by ext; exact mul_left_inv _,
.. smooth_map_monoid }
@[to_additive]
instance smooth_map_comm_group {G : Type*} [comm_group G] [topological_space G]
[charted_space H' G] [lie_group I' G] :
comm_group C^∞⟮I, N; I', G⟯ :=
{ ..smooth_map_group,
..smooth_map_comm_monoid }
end group_structure
section ring_structure
/-!
### Ring stucture
In this section we show that smooth functions valued in a smooth ring `R` inherit a ring structure
under pointwise multiplication.
-/
instance smooth_map_semiring {R : Type*} [semiring R] [topological_space R]
[charted_space H' R] [smooth_semiring I' R] :
semiring C^∞⟮I, N; I', R⟯ :=
{ left_distrib := λ a b c, by ext; exact left_distrib _ _ _,
right_distrib := λ a b c, by ext; exact right_distrib _ _ _,
zero_mul := λ a, by ext; exact zero_mul _,
mul_zero := λ a, by ext; exact mul_zero _,
..smooth_map_add_comm_monoid,
..smooth_map_monoid }
instance smooth_map_ring {R : Type*} [ring R] [topological_space R]
[charted_space H' R] [smooth_ring I' R] :
ring C^∞⟮I, N; I', R⟯ :=
{ ..smooth_map_semiring,
..smooth_map_add_comm_group, }
instance smooth_map_comm_ring {R : Type*} [comm_ring R] [topological_space R]
[charted_space H' R] [smooth_ring I' R] :
comm_ring C^∞⟮I, N; I', R⟯ :=
{ ..smooth_map_semiring,
..smooth_map_add_comm_group,
..smooth_map_comm_monoid,}
end ring_structure
section semimodule_structure
/-!
### Semiodule stucture
In this section we show that smooth functions valued in a vector space `M` over a normed
field `𝕜` inherit a vector space structure.
-/
instance smooth_map_has_scalar
{V : Type*} [normed_group V] [normed_space 𝕜 V] :
has_scalar 𝕜 C^∞⟮I, N; 𝓘(𝕜, V), V⟯ :=
⟨λ r f, ⟨r • f, smooth_const.smul f.smooth⟩⟩
instance smooth_map_semimodule
{V : Type*} [normed_group V] [normed_space 𝕜 V] :
vector_space 𝕜 C^∞⟮I, N; 𝓘(𝕜, V), V⟯ :=
semimodule.of_core $
{ smul := (•),
smul_add := λ c f g, by ext x; exact smul_add c (f x) (g x),
add_smul := λ c₁ c₂ f, by ext x; exact add_smul c₁ c₂ (f x),
mul_smul := λ c₁ c₂ f, by ext x; exact mul_smul c₁ c₂ (f x),
one_smul := λ f, by ext x; exact one_smul 𝕜 (f x), }
end semimodule_structure
section algebra_structure
/-!
### Algebra structure
In this section we show that smooth functions valued in a normed algebra `A` over a normed field `𝕜`
inherit an algebra structure.
-/
variables {A : Type*} [normed_ring A] [normed_algebra 𝕜 A] [smooth_ring 𝓘(𝕜, A) A]
/-- Smooth constant functions as a `ring_hom`. -/
def smooth_map.C : 𝕜 →+* C^∞⟮I, N; 𝓘(𝕜, A), A⟯ :=
{ to_fun := λ c : 𝕜, ⟨λ x, ((algebra_map 𝕜 A) c), smooth_const⟩,
map_one' := by ext x; exact (algebra_map 𝕜 A).map_one,
map_mul' := λ c₁ c₂, by ext x; exact (algebra_map 𝕜 A).map_mul _ _,
map_zero' := by ext x; exact (algebra_map 𝕜 A).map_zero,
map_add' := λ c₁ c₂, by ext x; exact (algebra_map 𝕜 A).map_add _ _ }
instance : algebra 𝕜 C^∞⟮I, N; 𝓘(𝕜, A), A⟯ :=
{ smul := λ r f,
⟨r • f, smooth_const.smul f.smooth⟩,
to_ring_hom := smooth_map.C,
commutes' := λ c f, by ext x; exact algebra.commutes' _ _,
smul_def' := λ c f, by ext x; exact algebra.smul_def' _ _,
..smooth_map_semiring }
end algebra_structure
section module_over_continuous_functions
/-!
### Structure as module over scalar functions
If `V` is a module over `𝕜`, then we show that the space of smooth functions from `N` to `V`
is naturally a vector space over the ring of smooth functions from `N` to `𝕜`. -/
instance smooth_map_has_scalar'
{V : Type*} [normed_group V] [normed_space 𝕜 V] :
has_scalar C^∞⟮I, N; 𝓘(𝕜), 𝕜⟯ C^∞⟮I, N; 𝓘(𝕜, V), V⟯ :=
⟨λ f g, ⟨λ x, (f x) • (g x), (smooth.smul f.2 g.2)⟩⟩
instance smooth_map_module'
{V : Type*} [normed_group V] [normed_space 𝕜 V] :
semimodule C^∞⟮I, N; 𝓘(𝕜), 𝕜⟯ C^∞⟮I, N; 𝓘(𝕜, V), V⟯ :=
{ smul := (•),
smul_add := λ c f g, by ext x; exact smul_add (c x) (f x) (g x),
add_smul := λ c₁ c₂ f, by ext x; exact add_smul (c₁ x) (c₂ x) (f x),
mul_smul := λ c₁ c₂ f, by ext x; exact mul_smul (c₁ x) (c₂ x) (f x),
one_smul := λ f, by ext x; exact one_smul 𝕜 (f x),
zero_smul := λ f, by ext x; exact zero_smul _ _,
smul_zero := λ r, by ext x; exact smul_zero _, }
end module_over_continuous_functions
|
b9e134609ca51a8a6572da663a1c4a64445d1dc8 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/702.lean | c25b64f3e2477621e2d11fb40c1c1004b82a7d32 | [
"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 | 94 | lean | definition bar := bool
example (b : bar) : bool :=
begin
rewrite [↓bar],
assumption
end
|
63b1fc997939a260476369e28407c90911bf558e | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/number_theory/padics/padic_norm.lean | e44df9e92c04e716b8ea67e737be1a895948cfba | [
"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 | 12,877 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import number_theory.padics.padic_val
/-!
# p-adic norm
This file defines the p-adic norm on ℚ.
The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and
denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate
assumptions on p.
The valuation induces a norm on ℚ. This norm is a nonarchimedean absolute value.
It takes values in {0} ∪ {1/p^k | k ∈ ℤ}.
## Notations
This file uses the local notation `/.` for `rat.mk`.
## Implementation notes
Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically
by taking `[fact (prime p)]` as a type class argument.
## References
* [F. Q. Gouvêa, *p-adic numbers*][gouvea1997]
* [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]
* <https://en.wikipedia.org/wiki/P-adic_number>
## Tags
p-adic, p adic, padic, norm, valuation
-/
/--
If `q ≠ 0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`.
If `q = 0`, the p-adic norm of `q` is 0.
-/
def padic_norm (p : ℕ) (q : ℚ) : ℚ :=
if q = 0 then 0 else (↑p : ℚ) ^ (-(padic_val_rat p q))
namespace padic_norm
open padic_val_rat
variables {p : ℕ}
/-- Unfolds the definition of the p-adic norm of `q` when `q ≠ 0`. -/
@[simp] protected lemma eq_zpow_of_nonzero {q : ℚ} (hq : q ≠ 0) :
padic_norm p q = p ^ (-(padic_val_rat p q)) :=
by simp [hq, padic_norm]
/-- The p-adic norm is nonnegative. -/
protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q :=
if hq : q = 0 then by simp [hq, padic_norm]
else
begin
unfold padic_norm; split_ifs,
apply zpow_nonneg,
exact_mod_cast nat.zero_le _
end
/-- The p-adic norm of 0 is 0. -/
@[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm]
/-- The p-adic norm of 1 is 1. -/
@[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm]
/--
The p-adic norm of `p` is `1/p` if `p > 1`.
See also `padic_norm.padic_norm_p_of_prime` for a version that assumes `p` is prime.
-/
lemma padic_norm_p (hp : 1 < p) : padic_norm p p = 1 / p :=
by simp [padic_norm, (pos_of_gt hp).ne', padic_val_nat.self hp]
/--
The p-adic norm of `p` is `1/p` if `p` is prime.
See also `padic_norm.padic_norm_p` for a version that assumes `1 < p`.
-/
@[simp] lemma padic_norm_p_of_prime [fact p.prime] : padic_norm p p = 1 / p :=
padic_norm_p $ nat.prime.one_lt (fact.out _)
/-- The p-adic norm of `q` is `1` if `q` is prime and not equal to `p`. -/
lemma padic_norm_of_prime_of_ne {q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime]
(neq : p ≠ q) : padic_norm p q = 1 :=
begin
have p : padic_val_rat p q = 0,
{ exact_mod_cast @padic_val_nat_primes p q p_prime q_prime neq },
simp [padic_norm, p, q_prime.1.1, q_prime.1.ne_zero],
end
/--
The p-adic norm of `p` is less than 1 if `1 < p`.
See also `padic_norm.padic_norm_p_lt_one_of_prime` for a version assuming `prime p`.
-/
lemma padic_norm_p_lt_one (hp : 1 < p) : padic_norm p p < 1 :=
begin
rw [padic_norm_p hp, div_lt_iff, one_mul],
{ exact_mod_cast hp },
{ exact_mod_cast zero_lt_one.trans hp },
end
/--
The p-adic norm of `p` is less than 1 if `p` is prime.
See also `padic_norm.padic_norm_p_lt_one` for a version assuming `1 < p`.
-/
lemma padic_norm_p_lt_one_of_prime [fact p.prime] : padic_norm p p < 1 :=
padic_norm_p_lt_one $ nat.prime.one_lt (fact.out _)
/-- `padic_norm p q` takes discrete values `p ^ -z` for `z : ℤ`. -/
protected theorem values_discrete {q : ℚ} (hq : q ≠ 0) : ∃ z : ℤ, padic_norm p q = p ^ (-z) :=
⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩
/-- `padic_norm p` is symmetric. -/
@[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q :=
if hq : q = 0 then by simp [hq]
else by simp [padic_norm, hq]
variable [hp : fact p.prime]
include hp
/-- If `q ≠ 0`, then `padic_norm p q ≠ 0`. -/
protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 :=
begin
rw padic_norm.eq_zpow_of_nonzero hq,
apply zpow_ne_zero_of_ne_zero,
exact_mod_cast ne_of_gt hp.1.pos
end
/-- If the p-adic norm of `q` is 0, then `q` is 0. -/
lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 :=
begin
apply by_contradiction, intro hq,
unfold padic_norm at h, rw if_neg hq at h,
apply absurd h,
apply zpow_ne_zero_of_ne_zero,
exact_mod_cast hp.1.ne_zero
end
/-- The p-adic norm is multiplicative. -/
@[simp] protected theorem mul (q r : ℚ) : padic_norm p (q*r) = padic_norm p q * padic_norm p r :=
if hq : q = 0 then
by simp [hq]
else if hr : r = 0 then
by simp [hr]
else
have q*r ≠ 0, from mul_ne_zero hq hr,
have (p : ℚ) ≠ 0, by simp [hp.1.ne_zero],
by simp [padic_norm, *, padic_val_rat.mul, zpow_add₀ this, mul_comm]
/-- The p-adic norm respects division. -/
@[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r :=
if hr : r = 0 then by simp [hr] else
eq_div_of_mul_eq (padic_norm.nonzero hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr])
/-- The p-adic norm of an integer is at most 1. -/
protected theorem of_int (z : ℤ) : padic_norm p ↑z ≤ 1 :=
if hz : z = 0 then by simp [hz, zero_le_one] else
begin
unfold padic_norm,
rw [if_neg _],
{ refine zpow_le_one_of_nonpos _ _,
{ exact_mod_cast le_of_lt hp.1.one_lt },
{ rw [padic_val_rat.of_int, neg_nonpos],
norm_cast, simp }},
exact_mod_cast hz,
end
private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _,
have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _,
if hq : q = 0 then
by simp [hq, max_eq_right hnrp, le_max_right]
else if hr : r = 0 then
by simp [hr, max_eq_left hnqp, le_max_left]
else if hqr : q + r = 0 then
le_trans (by simpa [hqr] using hnqp) (le_max_left _ _)
else
begin
unfold padic_norm, split_ifs,
apply le_max_iff.2,
left,
apply zpow_le_of_le,
{ exact_mod_cast le_of_lt hp.1.one_lt },
{ apply neg_le_neg,
have : padic_val_rat p q =
min (padic_val_rat p q) (padic_val_rat p r),
from (min_eq_left h).symm,
rw this,
apply min_le_padic_val_rat_add; assumption }
end
/--
The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and
the norm of `q`.
-/
protected theorem nonarchimedean {q r : ℚ} :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r],
exact nonarchimedean_aux hle
end
/--
The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p`
plus the norm of `q`.
-/
theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r :=
calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean
... ≤ padic_norm p q + padic_norm p r :
max_le_add_of_nonneg (padic_norm.nonneg _) (padic_norm.nonneg _)
/--
The p-adic norm of a difference is at most the max of each component. Restates the archimedean
property of the p-adic norm.
-/
protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) :=
by rw [sub_eq_add_neg, ←padic_norm.neg r]; apply padic_norm.nonarchimedean
/--
If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of
the norms of `q` and `r`.
-/
lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) :
padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r],
have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm,
have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc
padic_norm p q = padic_norm p (q + r - r) : by congr; ring
... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean
... = max (padic_norm p (q + r)) (padic_norm p r) : by simp,
have hnge : padic_norm p r ≤ padic_norm p (q + r),
{ apply le_of_not_gt,
intro hgt,
rw max_eq_right_of_lt hgt at this,
apply not_lt_of_ge this,
assumption },
have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this,
apply _root_.le_antisymm,
{ apply padic_norm.nonarchimedean },
{ rwa max_eq_left_of_lt hlt }
end
/--
The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle
inequality.
-/
instance : is_absolute_value (padic_norm p) :=
{ abv_nonneg := padic_norm.nonneg,
abv_eq_zero := λ _, ⟨zero_of_padic_norm_eq_zero, λ hx, by simpa only [hx]⟩,
abv_add := padic_norm.triangle_ineq,
abv_mul := padic_norm.mul }
lemma dvd_iff_norm_le {n : ℕ} {z : ℤ} : ↑(p ^ n) ∣ z ↔ padic_norm p z ≤ p ^ (-n : ℤ) :=
begin
unfold padic_norm, split_ifs with hz,
{ norm_cast at hz,
have : 0 ≤ (p^n : ℚ), {apply pow_nonneg, exact_mod_cast le_of_lt hp.1.pos },
simp [hz, this] },
{ rw [zpow_le_iff_le, neg_le_neg_iff, padic_val_rat.of_int,
padic_val_int.of_ne_one_ne_zero hp.1.ne_one _],
{ norm_cast,
rw [← part_enat.coe_le_coe, part_enat.coe_get, ← multiplicity.pow_dvd_iff_le_multiplicity],
simp },
{ exact_mod_cast hz },
{ exact_mod_cast hp.1.one_lt } }
end
/-- The `p`-adic norm of an integer `m` is one iff `p` doesn't divide `m`. -/
lemma int_eq_one_iff (m : ℤ) : padic_norm p m = 1 ↔ ¬ (p : ℤ) ∣ m :=
begin
nth_rewrite 1 ← pow_one p,
simp only [dvd_iff_norm_le, int.cast_coe_nat, nat.cast_one, zpow_neg, zpow_one, not_le],
split,
{ intro h,
rw [h, inv_lt_one_iff_of_pos];
norm_cast,
{ exact nat.prime.one_lt (fact.out _), },
{ exact nat.prime.pos (fact.out _), }, },
{ simp only [padic_norm],
split_ifs,
{ rw [inv_lt_zero, ← nat.cast_zero, nat.cast_lt],
intro h, exact (nat.not_lt_zero p h).elim, },
{ have : 1 < (p : ℚ) := by norm_cast; exact (nat.prime.one_lt (fact.out _ : nat.prime p)),
rw [← zpow_neg_one, zpow_lt_iff_lt this],
have : 0 ≤ padic_val_rat p m, simp only [of_int, nat.cast_nonneg],
intro h,
rw [← zpow_zero (p : ℚ), zpow_inj];
linarith, } },
end
lemma int_lt_one_iff (m : ℤ) : padic_norm p m < 1 ↔ (p : ℤ) ∣ m :=
begin
rw [← not_iff_not, ← int_eq_one_iff, eq_iff_le_not_lt],
simp only [padic_norm.of_int, true_and],
end
lemma of_nat (m : ℕ) : padic_norm p m ≤ 1 := padic_norm.of_int (m : ℤ)
/-- The `p`-adic norm of a natural `m` is one iff `p` doesn't divide `m`. -/
lemma nat_eq_one_iff (m : ℕ) : padic_norm p m = 1 ↔ ¬ p ∣ m :=
by simp only [←int.coe_nat_dvd, ←int_eq_one_iff, int.cast_coe_nat]
lemma nat_lt_one_iff (m : ℕ) : padic_norm p m < 1 ↔ p ∣ m :=
by simp only [←int.coe_nat_dvd, ←int_lt_one_iff, int.cast_coe_nat]
open_locale big_operators
lemma sum_lt {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} :
s.nonempty → (∀ i ∈ s, padic_norm p (F i) < t) → padic_norm p (∑ i in s, F i) < t :=
begin
classical,
refine s.induction_on (by { rintro ⟨-, ⟨⟩⟩, }) _,
rintro a S haS IH - ht,
by_cases hs : S.nonempty,
{ rw finset.sum_insert haS,
exact lt_of_le_of_lt padic_norm.nonarchimedean (max_lt
(ht a (finset.mem_insert_self a S))
(IH hs (λ b hb, ht b (finset.mem_insert_of_mem hb)))), },
{ simp * at *, },
end
lemma sum_le {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} :
s.nonempty → (∀ i ∈ s, padic_norm p (F i) ≤ t) → padic_norm p (∑ i in s, F i) ≤ t :=
begin
classical,
refine s.induction_on (by { rintro ⟨-, ⟨⟩⟩, }) _,
rintro a S haS IH - ht,
by_cases hs : S.nonempty,
{ rw finset.sum_insert haS,
exact padic_norm.nonarchimedean.trans (max_le
(ht a (finset.mem_insert_self a S))
(IH hs (λ b hb, ht b (finset.mem_insert_of_mem hb)))), },
{ simp * at *, },
end
lemma sum_lt' {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} (hF : ∀ i ∈ s, padic_norm p (F i) < t)
(ht : 0 < t) : padic_norm p (∑ i in s, F i) < t :=
begin
obtain rfl | hs := finset.eq_empty_or_nonempty s,
{ simp [ht], },
{ exact sum_lt hs hF, },
end
lemma sum_le' {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} (hF : ∀ i ∈ s, padic_norm p (F i) ≤ t)
(ht : 0 ≤ t) : padic_norm p (∑ i in s, F i) ≤ t :=
begin
obtain rfl | hs := finset.eq_empty_or_nonempty s,
{ simp [ht], },
{ exact sum_le hs hF, },
end
end padic_norm
|
bddfc34a678f0e4db1e3c17023550a10d282d871 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /12_Axioms.org.3.lean | a21508ecfbb2d4f8d97f35ff5f6d0e27eb81c9a1 | [] | 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 | 176 | lean | import standard
namespace hide
-- BEGIN
check @funext
-- ∀ {A : Type} {B : A → Type} {f₁ f₂ : Π x : A, B x}, (∀ x, f₁ x = f₂ x) → f₁ = f₂
-- END
end hide
|
cb7641a2ca538eba85acc1d2e8ff6dc0c152f70c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/symmetrized.lean | f4e94a886498fabdb9409ac13b77d5b55c25dc25 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 10,024 | lean | /-
Copyright (c) 2021 Christopher Hoskin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Christopher Hoskin
-/
import algebra.jordan.basic
import algebra.module.basic
/-!
# Symmetrized algebra
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A commutative multiplication on a real or complex space can be constructed from any multiplication
by "symmetrization" i.e
$$
a \circ b = \frac{1}{2}(ab + ba)
$$
We provide the symmetrized version of a type `α` as `sym_alg α`, with notation `αˢʸᵐ`.
## Implementation notes
The approach taken here is inspired by algebra.opposites. We use Oxford Spellings
(IETF en-GB-oxendict).
## References
* [Hanche-Olsen and Størmer, Jordan Operator Algebras][hancheolsenstormer1984]
-/
open function
/--
The symmetrized algebra has the same underlying space as the original algebra.
-/
def sym_alg (α : Type*) : Type* := α
postfix `ˢʸᵐ`:std.prec.max_plus := sym_alg
namespace sym_alg
variables {α : Type*}
/-- The element of `sym_alg α` that represents `a : α`. -/
@[pattern,pp_nodot]
def sym : α ≃ αˢʸᵐ := equiv.refl _
/-- The element of `α` represented by `x : αˢʸᵐ`. -/
@[pp_nodot]
def unsym : αˢʸᵐ ≃ α := equiv.refl _
@[simp] lemma unsym_sym (a : α) : unsym (sym a) = a := rfl
@[simp] lemma sym_unsym (a : α) : sym (unsym a) = a := rfl
@[simp] lemma sym_comp_unsym : (sym : α → αˢʸᵐ) ∘ unsym = id := rfl
@[simp] lemma unsym_comp_sym : (unsym : αˢʸᵐ → α) ∘ sym = id := rfl
@[simp] lemma sym_symm : (@sym α).symm = unsym := rfl
@[simp] lemma unsym_symm : (@unsym α).symm = sym := rfl
lemma sym_bijective : bijective (sym : α → αˢʸᵐ) := sym.bijective
lemma unsym_bijective : bijective (unsym : αˢʸᵐ → α) := unsym.symm.bijective
lemma sym_injective : injective (sym : α → αˢʸᵐ) := sym.injective
lemma sym_surjective : surjective (sym : α → αˢʸᵐ) := sym.surjective
lemma unsym_injective : injective (unsym : αˢʸᵐ → α) := unsym.injective
lemma unsym_surjective : surjective (unsym : αˢʸᵐ → α) := unsym.surjective
@[simp] lemma sym_inj {a b : α} : sym a = sym b ↔ a = b := sym_injective.eq_iff
@[simp] lemma unsym_inj {a b : αˢʸᵐ} : unsym a = unsym b ↔ a = b := unsym_injective.eq_iff
instance [nontrivial α] : nontrivial αˢʸᵐ := sym_injective.nontrivial
instance [inhabited α] : inhabited αˢʸᵐ := ⟨sym default⟩
instance [subsingleton α] : subsingleton αˢʸᵐ := unsym_injective.subsingleton
instance [unique α] : unique αˢʸᵐ := unique.mk' _
instance [is_empty α] : is_empty αˢʸᵐ := function.is_empty unsym
@[to_additive]
instance [has_one α] : has_one αˢʸᵐ := { one := sym 1 }
instance [has_add α] : has_add αˢʸᵐ :=
{ add := λ a b, sym (unsym a + unsym b) }
instance [has_sub α] : has_sub αˢʸᵐ := { sub := λ a b, sym (unsym a - unsym b) }
instance [has_neg α] : has_neg αˢʸᵐ :=
{ neg := λ a, sym (-unsym a) }
/- Introduce the symmetrized multiplication-/
instance [has_add α] [has_mul α] [has_one α] [invertible (2 : α)] : has_mul (αˢʸᵐ) :=
{ mul := λ a b, sym (⅟2 * (unsym a * unsym b + unsym b * unsym a)) }
@[to_additive] instance [has_inv α] : has_inv αˢʸᵐ :=
{ inv := λ a, sym $ (unsym a)⁻¹ }
instance (R : Type*) [has_smul R α] : has_smul R αˢʸᵐ :=
{ smul := λ r a, sym (r • unsym a) }
@[simp, to_additive] lemma sym_one [has_one α] : sym (1 : α) = 1 := rfl
@[simp, to_additive] lemma unsym_one [has_one α] : unsym (1 : αˢʸᵐ) = 1 := rfl
@[simp] lemma sym_add [has_add α] (a b : α) : sym (a + b) = sym a + sym b := rfl
@[simp] lemma unsym_add [has_add α] (a b : αˢʸᵐ) : unsym (a + b) = unsym a + unsym b := rfl
@[simp] lemma sym_sub [has_sub α] (a b : α) : sym (a - b) = sym a - sym b := rfl
@[simp] lemma unsym_sub [has_sub α] (a b : αˢʸᵐ) : unsym (a - b) = unsym a - unsym b := rfl
@[simp] lemma sym_neg [has_neg α] (a : α) : sym (-a) = -sym a := rfl
@[simp] lemma unsym_neg [has_neg α] (a : αˢʸᵐ) : unsym (-a) = -unsym a := rfl
lemma mul_def [has_add α] [has_mul α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
a * b = sym (⅟2*(unsym a * unsym b + unsym b * unsym a)) := by refl
lemma unsym_mul [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
unsym (a * b) = ⅟2*(unsym a * unsym b + unsym b * unsym a) := by refl
lemma sym_mul_sym [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a b : α) :
sym a * sym b = sym (⅟2*(a * b + b * a)) :=
rfl
@[simp, to_additive] lemma sym_inv [has_inv α] (a : α) : sym (a⁻¹) = (sym a)⁻¹ := rfl
@[simp, to_additive] lemma unsym_inv [has_inv α] (a : αˢʸᵐ) : unsym (a⁻¹) = (unsym a)⁻¹ := rfl
@[simp] lemma sym_smul {R : Type*} [has_smul R α] (c : R) (a : α) : sym (c • a) = c • sym a := rfl
@[simp] lemma unsym_smul {R : Type*} [has_smul R α] (c : R) (a : αˢʸᵐ) :
unsym (c • a) = c • unsym a := rfl
@[simp, to_additive] lemma unsym_eq_one_iff [has_one α] (a : αˢʸᵐ) : a.unsym = 1 ↔ a = 1 :=
unsym_injective.eq_iff' rfl
@[simp, to_additive] lemma sym_eq_one_iff [has_one α] (a : α) : sym a = 1 ↔ a = 1 :=
sym_injective.eq_iff' rfl
@[to_additive] lemma unsym_ne_one_iff [has_one α] (a : αˢʸᵐ) : a.unsym ≠ (1 : α) ↔ a ≠ (1 : αˢʸᵐ) :=
not_congr $ unsym_eq_one_iff a
@[to_additive] lemma sym_ne_one_iff [has_one α] (a : α) : sym a ≠ (1 : αˢʸᵐ) ↔ a ≠ (1 : α) :=
not_congr $ sym_eq_one_iff a
instance [add_comm_semigroup α] : add_comm_semigroup (αˢʸᵐ) :=
unsym_injective.add_comm_semigroup _ unsym_add
instance [add_monoid α] : add_monoid (αˢʸᵐ) :=
unsym_injective.add_monoid _ unsym_zero unsym_add (λ _ _, rfl)
instance [add_group α] : add_group (αˢʸᵐ) :=
unsym_injective.add_group _ unsym_zero
unsym_add unsym_neg unsym_sub (λ _ _, rfl) (λ _ _, rfl)
instance [add_comm_monoid α] : add_comm_monoid (αˢʸᵐ) :=
{ ..sym_alg.add_comm_semigroup, ..sym_alg.add_monoid }
instance [add_comm_group α] : add_comm_group (αˢʸᵐ) :=
{ ..sym_alg.add_comm_monoid, ..sym_alg.add_group }
instance {R : Type*} [semiring R] [add_comm_monoid α] [module R α] : module R αˢʸᵐ :=
function.injective.module R ⟨unsym, unsym_zero, unsym_add⟩ unsym_injective unsym_smul
instance [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a : α) [invertible a] :
invertible (sym a) :=
{ inv_of := sym (⅟a),
inv_of_mul_self := begin
rw [sym_mul_sym, mul_inv_of_self, inv_of_mul_self, ←bit0, inv_of_mul_self, sym_one],
end,
mul_inv_of_self := begin
rw [sym_mul_sym, mul_inv_of_self, inv_of_mul_self, ←bit0, inv_of_mul_self, sym_one],
end }
@[simp] lemma inv_of_sym [has_mul α] [has_add α] [has_one α] [invertible (2 : α)] (a : α)
[invertible a] : ⅟(sym a) = sym (⅟a) := rfl
instance [semiring α] [invertible (2 : α)] : non_assoc_semiring (αˢʸᵐ) :=
{ one := 1,
mul := (*),
zero := (0),
zero_mul := λ _, by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero],
mul_zero := λ _, by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero],
mul_one := λ _, by rw [mul_def, unsym_one, mul_one, one_mul, ←two_mul, inv_of_mul_self_assoc,
sym_unsym],
one_mul := λ _, by rw [mul_def, unsym_one, mul_one, one_mul, ←two_mul, inv_of_mul_self_assoc,
sym_unsym],
left_distrib := λ a b c, match a, b, c with
| sym a, sym b, sym c := begin
rw [sym_mul_sym, sym_mul_sym, ←sym_add, sym_mul_sym, ←sym_add, mul_add a, add_mul _ _ a,
add_add_add_comm, mul_add],
end
end,
right_distrib := λ a b c, match a, b, c with
| sym a, sym b, sym c := begin
rw [sym_mul_sym, sym_mul_sym, ←sym_add, sym_mul_sym, ←sym_add, mul_add c, add_mul _ _ c,
add_add_add_comm, mul_add],
end
end,
..sym_alg.add_comm_monoid, }
/-- The symmetrization of a real (unital, associative) algebra is a non-associative ring. -/
instance [ring α] [invertible (2 : α)] : non_assoc_ring (αˢʸᵐ) :=
{ ..sym_alg.non_assoc_semiring,
..sym_alg.add_comm_group, }
/-! The squaring operation coincides for both multiplications -/
lemma unsym_mul_self [semiring α] [invertible (2 : α)] (a : αˢʸᵐ) :
unsym (a*a) = unsym a * unsym a :=
by rw [mul_def, unsym_sym, ←two_mul, inv_of_mul_self_assoc]
lemma sym_mul_self [semiring α] [invertible (2 : α)] (a : α) : sym (a*a) = sym a * sym a :=
by rw [sym_mul_sym, ←two_mul, inv_of_mul_self_assoc]
lemma mul_comm [has_mul α] [add_comm_semigroup α] [has_one α] [invertible (2 : α)] (a b : αˢʸᵐ) :
a * b = b * a :=
by rw [mul_def, mul_def, add_comm]
instance [ring α] [invertible (2 : α)] : is_comm_jordan αˢʸᵐ :=
{ mul_comm := sym_alg.mul_comm,
lmul_comm_rmul_rmul := λ a b, begin
-- Rearrange LHS
have commute_half_left := λ a : α, (commute.one_left a).bit0_left.inv_of_left.eq,
rw [mul_def, mul_def a b, unsym_sym, ← mul_assoc, ← commute_half_left (unsym (a*a)), mul_assoc,
mul_assoc, ← mul_add, ← mul_assoc, add_mul, mul_add (unsym (a * a)), ← add_assoc, ← mul_assoc,
← mul_assoc],
-- Rearrange RHS
nth_rewrite_rhs 0 [mul_def],
nth_rewrite_rhs 0 [mul_def],
nth_rewrite_rhs 2 [mul_def],
rw [unsym_sym, sym_inj, ← mul_assoc, ← commute_half_left (unsym a), mul_assoc (⅟2) (unsym a),
mul_assoc (⅟2) _ (unsym a), ← mul_add, ← mul_assoc],
nth_rewrite_rhs 0 mul_add (unsym a),
rw [add_mul, ← add_assoc, ← mul_assoc, ← mul_assoc],
rw unsym_mul_self,
rw [← mul_assoc, ← mul_assoc, ← mul_assoc, ← mul_assoc, ← sub_eq_zero, ← mul_sub],
convert mul_zero (⅟(2:α) * ⅟(2:α)),
rw [add_sub_add_right_eq_sub, add_assoc, add_assoc, add_sub_add_left_eq_sub, add_comm,
add_sub_add_right_eq_sub, sub_eq_zero],
end }
end sym_alg
|
36f69ba97965757fd454741090b5b09a34e069b3 | a047a4718edfa935d17231e9e6ecec8c7b701e05 | /src/ring_theory/noetherian.lean | 31bee5d19fba50ca2ce5cdf32de69c1b0cba14f4 | [
"Apache-2.0"
] | permissive | utensil-contrib/mathlib | bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767 | b91909e77e219098a2f8cc031f89d595fe274bd2 | refs/heads/master | 1,668,048,976,965 | 1,592,442,701,000 | 1,592,442,701,000 | 273,197,855 | 0 | 0 | null | 1,592,472,812,000 | 1,592,472,811,000 | null | UTF-8 | Lean | false | false | 23,150 | lean | /-
Copyright (c) 2018 Mario Carneiro and Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kevin Buzzard
-/
import ring_theory.ideal_operations
import linear_algebra.basis
/-!
# Noetherian rings and modules
The following are equivalent for a module M over a ring R:
1. Every increasing chain of submodule M₁ ⊆ M₂ ⊆ M₃ ⊆ ⋯ eventually stabilises.
2. Every submodule is finitely generated.
A module satisfying these equivalent conditions is said to be a *Noetherian* R-module.
A ring is a *Noetherian ring* if it is Noetherian as a module over itself.
## Main definitions
Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`.
* `fg N : Prop` is the assertion that `N` is finitely generated as an `R`-module.
* `is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module. It is a class,
implemented as the predicate that all `R`-submodules of `M` are finitely generated.
## Main statements
* `exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` is Nakayama's lemma, in the following form:
if N is a finitely generated submodule of an ambient R-module M and I is an ideal of R
such that N ⊆ IN, then there exists r ∈ 1 + I such that rN = 0.
* `is_noetherian_iff_well_founded` is the theorem that an R-module M is Noetherian iff
`>` is well-founded on `submodule R M`.
Note that the Hilbert basis theorem, that if a commutative ring R is Noetherian then so is R[X],
is proved in `ring_theory.polynomial`.
## References
* [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald]
## Tags
Noetherian, noetherian, Noetherian ring, Noetherian module, noetherian ring, noetherian module
-/
open set
open_locale big_operators
namespace submodule
variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M]
/-- A submodule of `M` is finitely generated if it is the span of a finite subset of `M`. -/
def fg (N : submodule R M) : Prop := ∃ S : finset M, submodule.span R ↑S = N
theorem fg_def {N : submodule R M} :
N.fg ↔ ∃ S : set M, finite S ∧ span R S = N :=
⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin
rintro ⟨t', h, rfl⟩,
rcases finite.exists_finset_coe h with ⟨t, rfl⟩,
exact ⟨t, rfl⟩
end⟩
/-- Nakayama's Lemma. Atiyah-Macdonald 2.5, Eisenbud 4.7, Matsumura 2.2, Stacks 00DV -/
theorem exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul {R : Type*} [comm_ring R]
{M : Type*} [add_comm_group M] [module R M]
(I : ideal R) (N : submodule R M) (hn : N.fg) (hin : N ≤ I • N) :
∃ r : R, r - 1 ∈ I ∧ ∀ n ∈ N, r • n = (0 : M) :=
begin
rw fg_def at hn, rcases hn with ⟨s, hfs, hs⟩,
have : ∃ r : R, r - 1 ∈ I ∧ N ≤ (I • span R s).comap (linear_map.lsmul R M r) ∧ s ⊆ N,
{ refine ⟨1, _, _, _⟩,
{ rw sub_self, exact I.zero_mem },
{ rw [hs], intros n hn, rw [mem_comap], change (1:R) • n ∈ I • N, rw one_smul, exact hin hn },
{ rw [← span_le, hs], exact le_refl N } },
clear hin hs, revert this,
refine set.finite.dinduction_on hfs (λ H, _) (λ i s his hfs ih H, _),
{ rcases H with ⟨r, hr1, hrn, hs⟩, refine ⟨r, hr1, λ n hn, _⟩, specialize hrn hn,
rwa [mem_comap, span_empty, smul_bot, mem_bot] at hrn },
apply ih, rcases H with ⟨r, hr1, hrn, hs⟩,
rw [← set.singleton_union, span_union, smul_sup] at hrn,
rw [set.insert_subset] at hs,
have : ∃ c : R, c - 1 ∈ I ∧ c • i ∈ I • span R s,
{ specialize hrn hs.1, rw [mem_comap, mem_sup] at hrn,
rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • i at hyz,
rw mem_smul_span_singleton at hy, rcases hy with ⟨c, hci, rfl⟩,
use r-c, split,
{ rw [sub_right_comm], exact I.sub_mem hr1 hci },
{ rw [sub_smul, ← hyz, add_sub_cancel'], exact hz } },
rcases this with ⟨c, hc1, hci⟩, refine ⟨c * r, _, _, hs.2⟩,
{ rw [← ideal.quotient.eq, ideal.quotient.mk_one] at hr1 hc1 ⊢,
rw [ideal.quotient.mk_mul, hc1, hr1, mul_one] },
{ intros n hn, specialize hrn hn, rw [mem_comap, mem_sup] at hrn,
rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • n at hyz,
rw mem_smul_span_singleton at hy, rcases hy with ⟨d, hdi, rfl⟩,
change _ • _ ∈ I • span R s,
rw [mul_smul, ← hyz, smul_add, smul_smul, mul_comm, mul_smul],
exact add_mem _ (smul_mem _ _ hci) (smul_mem _ _ hz) }
end
theorem fg_bot : (⊥ : submodule R M).fg :=
⟨∅, by rw [finset.coe_empty, span_empty]⟩
theorem fg_sup {N₁ N₂ : submodule R M}
(hN₁ : N₁.fg) (hN₂ : N₂.fg) : (N₁ ⊔ N₂).fg :=
let ⟨t₁, ht₁⟩ := fg_def.1 hN₁, ⟨t₂, ht₂⟩ := fg_def.1 hN₂ in
fg_def.2 ⟨t₁ ∪ t₂, finite_union ht₁.1 ht₂.1, by rw [span_union, ht₁.2, ht₂.2]⟩
variables {P : Type*} [add_comm_group P] [module R P]
variables {f : M →ₗ[R] P}
theorem fg_map {N : submodule R M} (hs : N.fg) : (N.map f).fg :=
let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, finite_image _ ht.1, by rw [span_image, ht.2]⟩
theorem fg_prod {sb : submodule R M} {sc : submodule R P}
(hsb : sb.fg) (hsc : sc.fg) : (sb.prod sc).fg :=
let ⟨tb, htb⟩ := fg_def.1 hsb, ⟨tc, htc⟩ := fg_def.1 hsc in
fg_def.2 ⟨prod.inl '' tb ∪ prod.inr '' tc,
finite_union (finite_image _ htb.1) (finite_image _ htc.1),
by rw [linear_map.span_inl_union_inr, htb.2, htc.2]⟩
variable (f)
/-- If 0 → M' → M → M'' → 0 is exact and M' and M'' are
finitely generated then so is M. -/
theorem fg_of_fg_map_of_fg_inf_ker {s : submodule R M}
(hs1 : (s.map f).fg) (hs2 : (s ⊓ f.ker).fg) : s.fg :=
begin
haveI := classical.dec_eq R, haveI := classical.dec_eq M, haveI := classical.dec_eq P,
cases hs1 with t1 ht1, cases hs2 with t2 ht2,
have : ∀ y ∈ t1, ∃ x ∈ s, f x = y,
{ intros y hy,
have : y ∈ map f s, { rw ← ht1, exact subset_span hy },
rcases mem_map.1 this with ⟨x, hx1, hx2⟩,
exact ⟨x, hx1, hx2⟩ },
have : ∃ g : P → M, ∀ y ∈ t1, g y ∈ s ∧ f (g y) = y,
{ choose g hg1 hg2,
existsi λ y, if H : y ∈ t1 then g y H else 0,
intros y H, split,
{ simp only [dif_pos H], apply hg1 },
{ simp only [dif_pos H], apply hg2 } },
cases this with g hg, clear this,
existsi t1.image g ∪ t2,
rw [finset.coe_union, span_union, finset.coe_image],
apply le_antisymm,
{ refine sup_le (span_le.2 $ image_subset_iff.2 _) (span_le.2 _),
{ intros y hy, exact (hg y hy).1 },
{ intros x hx, have := subset_span hx,
rw ht2 at this,
exact this.1 } },
intros x hx,
have : f x ∈ map f s, { rw mem_map, exact ⟨x, hx, rfl⟩ },
rw [← ht1,← set.image_id ↑t1, finsupp.mem_span_iff_total] at this,
rcases this with ⟨l, hl1, hl2⟩,
refine mem_sup.2 ⟨(finsupp.total M M R id).to_fun ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l), _,
x - finsupp.total M M R id ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l),
_, add_sub_cancel'_right _ _⟩,
{ rw [← set.image_id (g '' ↑t1), finsupp.mem_span_iff_total], refine ⟨_, _, rfl⟩,
haveI : inhabited P := ⟨0⟩,
rw [← finsupp.lmap_domain_supported _ _ g, mem_map],
refine ⟨l, hl1, _⟩,
refl, },
rw [ht2, mem_inf], split,
{ apply s.sub_mem hx,
rw [finsupp.total_apply, finsupp.lmap_domain_apply, finsupp.sum_map_domain_index],
refine s.sum_mem _,
{ intros y hy, exact s.smul_mem _ (hg y (hl1 hy)).1 },
{ exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } },
{ rw [linear_map.mem_ker, f.map_sub, ← hl2],
rw [finsupp.total_apply, finsupp.total_apply, finsupp.lmap_domain_apply],
rw [finsupp.sum_map_domain_index, finsupp.sum, finsupp.sum, f.map_sum],
rw sub_eq_zero,
refine finset.sum_congr rfl (λ y hy, _),
unfold id,
rw [f.map_smul, (hg y (hl1 hy)).2],
{ exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } }
end
end submodule
/--
`is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module,
implemented as the predicate that all `R`-submodules of `M` are finitely generated.
-/
class is_noetherian (R M) [ring R] [add_comm_group M] [module R M] : Prop :=
(noetherian : ∀ (s : submodule R M), s.fg)
section
variables {R : Type*} {M : Type*} {P : Type*}
variables [ring R] [add_comm_group M] [add_comm_group P]
variables [module R M] [module R P]
open is_noetherian
include R
theorem is_noetherian_submodule {N : submodule R M} :
is_noetherian R N ↔ ∀ s : submodule R M, s ≤ N → s.fg :=
⟨λ ⟨hn⟩, λ s hs, have s ≤ N.subtype.range, from (N.range_subtype).symm ▸ hs,
linear_map.map_comap_eq_self this ▸ submodule.fg_map (hn _),
λ h, ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker N.subtype (h _ $ submodule.map_subtype_le _ _) $
by rw [submodule.ker_subtype, inf_bot_eq]; exact submodule.fg_bot⟩⟩
theorem is_noetherian_submodule_left {N : submodule R M} :
is_noetherian R N ↔ ∀ s : submodule R M, (N ⊓ s).fg :=
is_noetherian_submodule.trans
⟨λ H s, H _ inf_le_left, λ H s hs, (inf_of_le_right hs) ▸ H _⟩
theorem is_noetherian_submodule_right {N : submodule R M} :
is_noetherian R N ↔ ∀ s : submodule R M, (s ⊓ N).fg :=
is_noetherian_submodule.trans
⟨λ H s, H _ inf_le_right, λ H s hs, (inf_of_le_left hs) ▸ H _⟩
variable (M)
theorem is_noetherian_of_surjective (f : M →ₗ[R] P) (hf : f.range = ⊤)
[is_noetherian R M] : is_noetherian R P :=
⟨λ s, have (s.comap f).map f = s, from linear_map.map_comap_eq_self $ hf.symm ▸ le_top,
this ▸ submodule.fg_map $ noetherian _⟩
variable {M}
theorem is_noetherian_of_linear_equiv (f : M ≃ₗ[R] P)
[is_noetherian R M] : is_noetherian R P :=
is_noetherian_of_surjective _ f.to_linear_map f.range
instance is_noetherian_prod [is_noetherian R M]
[is_noetherian R P] : is_noetherian R (M × P) :=
⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker (linear_map.snd R M P) (noetherian _) $
have s ⊓ linear_map.ker (linear_map.snd R M P) ≤ linear_map.range (linear_map.inl R M P),
from λ x ⟨hx1, hx2⟩, ⟨x.1, trivial, prod.ext rfl $ eq.symm $ linear_map.mem_ker.1 hx2⟩,
linear_map.map_comap_eq_self this ▸ submodule.fg_map (noetherian _)⟩
instance is_noetherian_pi {R ι : Type*} {M : ι → Type*} [ring R]
[Π i, add_comm_group (M i)] [Π i, module R (M i)] [fintype ι]
[∀ i, is_noetherian R (M i)] : is_noetherian R (Π i, M i) :=
begin
haveI := classical.dec_eq ι,
suffices : ∀ s : finset ι, is_noetherian R (Π i : (↑s : set ι), M i),
{ letI := this finset.univ,
refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _
⟨_, _, _, _, _, _⟩ (this finset.univ),
{ exact λ f i, f ⟨i, finset.mem_univ _⟩ },
{ intros, ext, refl },
{ intros, ext, refl },
{ exact λ f i, f i.1 },
{ intro, ext ⟨⟩, refl },
{ intro, ext i, refl } },
intro s,
induction s using finset.induction with a s has ih,
{ split, intro s, convert submodule.fg_bot, apply eq_bot_iff.2,
intros x hx, refine (submodule.mem_bot R).2 _, ext i, cases i.2 },
refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _
⟨_, _, _, _, _, _⟩ (@is_noetherian_prod _ (M a) _ _ _ _ _ _ _ ih),
{ exact λ f i, or.by_cases (finset.mem_insert.1 i.2)
(λ h : i.1 = a, show M i.1, from (eq.rec_on h.symm f.1))
(λ h : i.1 ∈ s, show M i.1, from f.2 ⟨i.1, h⟩) },
{ intros f g, ext i, unfold or.by_cases, cases i with i hi,
rcases finset.mem_insert.1 hi with rfl | h,
{ change _ = _ + _, simp only [dif_pos], refl },
{ change _ = _ + _, have : ¬i = a, { rintro rfl, exact has h },
simp only [dif_neg this, dif_pos h], refl } },
{ intros c f, ext i, unfold or.by_cases, cases i with i hi,
rcases finset.mem_insert.1 hi with rfl | h,
{ change _ = c • _, simp only [dif_pos], refl },
{ change _ = c • _, have : ¬i = a, { rintro rfl, exact has h },
simp only [dif_neg this, dif_pos h], refl } },
{ exact λ f, (f ⟨a, finset.mem_insert_self _ _⟩, λ i, f ⟨i.1, finset.mem_insert_of_mem i.2⟩) },
{ intro f, apply prod.ext,
{ simp only [or.by_cases, dif_pos] },
{ ext ⟨i, his⟩,
have : ¬i = a, { rintro rfl, exact has his },
dsimp only [or.by_cases], change i ∈ s at his,
rw [dif_neg this, dif_pos his] } },
{ intro f, ext ⟨i, hi⟩,
rcases finset.mem_insert.1 hi with rfl | h,
{ simp only [or.by_cases, dif_pos], refl },
{ have : ¬i = a, { rintro rfl, exact has h },
simp only [or.by_cases, dif_neg this, dif_pos h], refl } }
end
end
open is_noetherian submodule function
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem is_noetherian_iff_well_founded
{R M} [ring R] [add_comm_group M] [module R M] :
is_noetherian R M ↔ well_founded ((>) : submodule R M → submodule R M → Prop) :=
⟨λ h, begin
apply order_embedding.well_founded_iff_no_descending_seq.2,
swap, { apply is_strict_order.swap },
rintro ⟨⟨N, hN⟩⟩,
let Q := ⨆ n, N n,
resetI,
rcases submodule.fg_def.1 (noetherian Q) with ⟨t, h₁, h₂⟩,
have hN' : ∀ {a b}, a ≤ b → N a ≤ N b :=
λ a b, (strict_mono.le_iff_le (λ _ _, hN.1)).2,
have : t ⊆ ⋃ i, (N i : set M),
{ rw [← submodule.coe_supr_of_directed N _],
{ show t ⊆ Q, rw ← h₂,
apply submodule.subset_span },
{ exact λ i j, ⟨max i j,
hN' (le_max_left _ _),
hN' (le_max_right _ _)⟩ } },
simp [subset_def] at this,
choose f hf using show ∀ x : t, ∃ (i : ℕ), x.1 ∈ N i, { simpa },
cases h₁ with h₁,
let A := finset.sup (@finset.univ t h₁) f,
have : Q ≤ N A,
{ rw ← h₂, apply submodule.span_le.2,
exact λ x h, hN' (finset.le_sup (@finset.mem_univ t h₁ _))
(hf ⟨x, h⟩) },
exact not_le_of_lt (hN.1 (nat.lt_succ_self A))
(le_trans (le_supr _ _) this)
end,
begin
assume h, split, assume N,
suffices : ∀ P ≤ N, ∃ s, finite s ∧ P ⊔ submodule.span R s = N,
{ rcases this ⊥ bot_le with ⟨s, hs, e⟩,
exact submodule.fg_def.2 ⟨s, hs, by simpa using e⟩ },
refine λ P, h.induction P _, intros P IH PN,
letI := classical.dec,
by_cases h : ∀ x, x ∈ N → x ∈ P,
{ cases le_antisymm PN h, exact ⟨∅, by simp⟩ },
{ simp [not_forall] at h,
rcases h with ⟨x, h, h₂⟩,
have : ¬P ⊔ submodule.span R {x} ≤ P,
{ intro hn, apply h₂,
have := le_trans le_sup_right hn,
exact submodule.span_le.1 this (mem_singleton x) },
rcases IH (P ⊔ submodule.span R {x})
⟨@le_sup_left _ _ P _, this⟩
(sup_le PN (submodule.span_le.2 (by simpa))) with ⟨s, hs, hs₂⟩,
refine ⟨insert x s, finite_insert _ hs, _⟩,
rw [← hs₂, sup_assoc, ← submodule.span_union], simp }
end⟩
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma well_founded_submodule_gt (R M) [ring R] [add_comm_group M] [module R M] :
∀ [is_noetherian R M], well_founded ((>) : submodule R M → submodule R M → Prop) :=
is_noetherian_iff_well_founded.mp
lemma finite_of_linear_independent {R M} [comm_ring R] [nonzero R] [add_comm_group M] [module R M]
[is_noetherian R M] {s : set M} (hs : linear_independent R (subtype.val : s → M)) : s.finite :=
begin
refine classical.by_contradiction (λ hf, order_embedding.well_founded_iff_no_descending_seq.1
(well_founded_submodule_gt R M) ⟨_⟩),
have f : ℕ ↪ s, from @infinite.nat_embedding s ⟨λ f, hf ⟨f⟩⟩,
have : ∀ n, (subtype.val ∘ f) '' {m | m ≤ n} ⊆ s,
{ rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 },
have : ∀ a b : ℕ, a ≤ b ↔
span R ((subtype.val ∘ f) '' {m | m ≤ a}) ≤ span R ((subtype.val ∘ f) '' {m | m ≤ b}),
{ assume a b,
rw [span_le_span_iff zero_ne_one hs (this a) (this b),
set.image_subset_image_iff (subtype.val_injective.comp f.inj),
set.subset_def],
exact ⟨λ hab x (hxa : x ≤ a), le_trans hxa hab, λ hx, hx a (le_refl a)⟩ },
exact ⟨⟨λ n, span R ((subtype.val ∘ f) '' {m | m ≤ n}),
λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩,
by dsimp [gt]; simp only [lt_iff_le_not_le, (this _ _).symm]; tauto⟩
end
/--
A ring is Noetherian if it is Noetherian as a module over itself,
i.e. all its ideals are finitely generated.
-/
@[class] def is_noetherian_ring (R) [ring R] : Prop := is_noetherian R R
instance is_noetherian_ring.to_is_noetherian {R : Type*} [ring R] :
∀ [is_noetherian_ring R], is_noetherian R R := id
@[priority 80] -- see Note [lower instance priority]
instance ring.is_noetherian_of_fintype (R M) [fintype M] [ring R] [add_comm_group M] [module R M] :
is_noetherian R M :=
by letI := classical.dec; exact
⟨assume s, ⟨to_finset s, by rw [set.coe_to_finset, submodule.span_eq]⟩⟩
theorem ring.is_noetherian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_noetherian_ring R :=
by haveI := subsingleton_of_zero_eq_one R h01;
haveI := fintype.of_subsingleton (0:R);
exact ring.is_noetherian_of_fintype _ _
theorem is_noetherian_of_submodule_of_noetherian (R M) [ring R] [add_comm_group M] [module R M]
(N : submodule R M) (h : is_noetherian R M) : is_noetherian R N :=
begin
rw is_noetherian_iff_well_founded at h ⊢,
convert order_embedding.well_founded (order_embedding.rsymm
(submodule.map_subtype.lt_order_embedding N)) h
end
theorem is_noetherian_of_quotient_of_noetherian (R) [ring R] (M) [add_comm_group M] [module R M]
(N : submodule R M) (h : is_noetherian R M) : is_noetherian R N.quotient :=
begin
rw is_noetherian_iff_well_founded at h ⊢,
convert order_embedding.well_founded (order_embedding.rsymm
(submodule.comap_mkq.lt_order_embedding N)) h
end
theorem is_noetherian_of_fg_of_noetherian {R M} [ring R] [add_comm_group M] [module R M]
(N : submodule R M) [is_noetherian_ring R] (hN : N.fg) : is_noetherian R N :=
let ⟨s, hs⟩ := hN in
begin
haveI := classical.dec_eq M,
haveI := classical.dec_eq R,
letI : is_noetherian R R := by apply_instance,
have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx,
refine @@is_noetherian_of_surjective ((↑s : set M) → R) _ _ _ (pi.semimodule _ _ _)
_ _ _ is_noetherian_pi,
{ fapply linear_map.mk,
{ exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ },
{ intros f g, apply subtype.eq,
change ∑ i in s.attach, (f i + g i) • _ = _,
simp only [add_smul, finset.sum_add_distrib], refl },
{ intros c f, apply subtype.eq,
change ∑ i in s.attach, (c • f i) • _ = _,
simp only [smul_eq_mul, mul_smul],
exact finset.smul_sum.symm } },
rw linear_map.range_eq_top,
rintro ⟨n, hn⟩, change n ∈ N at hn,
rw [← hs, ← set.image_id ↑s, finsupp.mem_span_iff_total] at hn,
rcases hn with ⟨l, hl1, hl2⟩,
refine ⟨λ x, l x.1, subtype.eq _⟩,
change ∑ i in s.attach, l i.1 • i.1 = n,
rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2,
finsupp.total_apply, finsupp.sum, eq_comm],
refine finset.sum_subset hl1 (λ x _ hx, _),
rw [finsupp.not_mem_support_iff.1 hx, zero_smul]
end
/-- In a module over a noetherian ring, the submodule generated by finitely many vectors is
noetherian. -/
theorem is_noetherian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M]
[is_noetherian_ring R] {A : set M} (hA : finite A) : is_noetherian R (submodule.span R A) :=
is_noetherian_of_fg_of_noetherian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩)
theorem is_noetherian_ring_of_surjective (R) [comm_ring R] (S) [comm_ring S]
(f : R →+* S) (hf : function.surjective f)
[H : is_noetherian_ring R] : is_noetherian_ring S :=
begin
unfold is_noetherian_ring at H ⊢,
rw is_noetherian_iff_well_founded at H ⊢,
convert order_embedding.well_founded (order_embedding.rsymm
(ideal.lt_order_embedding_of_surjective f hf)) H
end
instance is_noetherian_ring_range {R} [comm_ring R] {S} [comm_ring S] (f : R →+* S)
[is_noetherian_ring R] : is_noetherian_ring (set.range f) :=
is_noetherian_ring_of_surjective R (set.range f) (f.cod_restrict (set.range f) set.mem_range_self)
set.surjective_onto_range
theorem is_noetherian_ring_of_ring_equiv (R) [comm_ring R] {S} [comm_ring S]
(f : R ≃+* S) [is_noetherian_ring R] : is_noetherian_ring S :=
is_noetherian_ring_of_surjective R S f.to_ring_hom f.to_equiv.surjective
namespace is_noetherian_ring
variables {R : Type*} [integral_domain R] [is_noetherian_ring R]
open associates nat
local attribute [elab_as_eliminator] well_founded.fix
lemma well_founded_dvd_not_unit : well_founded (λ a b : R, a ≠ 0 ∧ ∃ x, ¬is_unit x ∧ b = a * x) :=
by simp only [ideal.span_singleton_lt_span_singleton.symm];
exact inv_image.wf (λ a, ideal.span ({a} : set R)) (well_founded_submodule_gt _ _)
lemma exists_irreducible_factor {a : R} (ha : ¬ is_unit a) (ha0 : a ≠ 0) :
∃ i, irreducible i ∧ i ∣ a :=
(irreducible_or_factor a ha).elim (λ hai, ⟨a, hai, dvd_refl _⟩)
(well_founded.fix
well_founded_dvd_not_unit
(λ a ih ha ha0 ⟨x, y, hx, hy, hxy⟩,
have hx0 : x ≠ 0, from λ hx0, ha0 (by rw [← hxy, hx0, zero_mul]),
(irreducible_or_factor x hx).elim
(λ hxi, ⟨x, hxi, hxy ▸ by simp⟩)
(λ hxf, let ⟨i, hi⟩ := ih x ⟨hx0, y, hy, hxy.symm⟩ hx hx0 hxf in
⟨i, hi.1, dvd.trans hi.2 (hxy ▸ by simp)⟩)) a ha ha0)
@[elab_as_eliminator] lemma irreducible_induction_on {P : R → Prop} (a : R)
(h0 : P 0) (hu : ∀ u : R, is_unit u → P u)
(hi : ∀ a i : R, a ≠ 0 → irreducible i → P a → P (i * a)) :
P a :=
by haveI := classical.dec; exact
well_founded.fix well_founded_dvd_not_unit
(λ a ih, if ha0 : a = 0 then ha0.symm ▸ h0
else if hau : is_unit a then hu a hau
else let ⟨i, hii, ⟨b, hb⟩⟩ := exists_irreducible_factor hau ha0 in
have hb0 : b ≠ 0, from λ hb0, by simp * at *,
hb.symm ▸ hi _ _ hb0 hii (ih _ ⟨hb0, i,
hii.1, by rw [hb, mul_comm]⟩))
a
lemma exists_factors (a : R) : a ≠ 0 →
∃f : multiset R, (∀b ∈ f, irreducible b) ∧ associated a f.prod :=
is_noetherian_ring.irreducible_induction_on a
(λ h, (h rfl).elim)
(λ u hu _, ⟨0, by simp [associated_one_iff_is_unit, hu]⟩)
(λ a i ha0 hii ih hia0,
let ⟨s, hs⟩ := ih ha0 in
⟨i::s, ⟨by clear _let_match; finish,
by rw multiset.prod_cons;
exact associated_mul_mul (by refl) hs.2⟩⟩)
end is_noetherian_ring
namespace submodule
variables {R : Type*} {A : Type*} [comm_ring R] [ring A] [algebra R A]
variables (M N : submodule R A)
local attribute [instance] set.pointwise_mul_semiring
theorem fg_mul (hm : M.fg) (hn : N.fg) : (M * N).fg :=
let ⟨m, hfm, hm⟩ := fg_def.1 hm, ⟨n, hfn, hn⟩ := fg_def.1 hn in
fg_def.2 ⟨m * n, set.pointwise_mul_finite hfm hfn, span_mul_span R m n ▸ hm ▸ hn ▸ rfl⟩
lemma fg_pow (h : M.fg) (n : ℕ) : (M ^ n).fg :=
nat.rec_on n
(⟨{1}, by simp [one_eq_span]⟩)
(λ n ih, by simpa [pow_succ] using fg_mul _ _ h ih)
end submodule
|
558c5545dea149985ff8f4d833ebf6035e575557 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/tactic/lint/type_classes.lean | 9dc66800beccf53a2ba07d0aa07aeee0908d39ae | [
"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 | 23,285 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner
-/
import tactic.lint.basic
/-!
# Linters about type classes
This file defines several linters checking the correct usage of type classes
and the appropriate definition of instances:
* `instance_priority` ensures that blanket instances have low priority.
* `has_inhabited_instances` checks that every type has an `inhabited` instance.
* `impossible_instance` checks that there are no instances which can never apply.
* `incorrect_type_class_argument` checks that only type classes are used in
instance-implicit arguments.
* `dangerous_instance` checks for instances that generate subproblems with metavariables.
* `fails_quickly` checks that type class resolution finishes quickly.
* `class_structure` checks that every `class` is a structure, i.e. `@[class] def` is forbidden.
* `has_coe_variable` checks that there is no instance of type `has_coe α t`.
* `inhabited_nonempty` checks whether `[inhabited α]` arguments could be generalized
to `[nonempty α]`.
* `decidable_classical` checks propositions for `[decidable_... p]` hypotheses that are not used
in the statement, and could thus be removed by using `classical` in the proof.
* `linter.has_coe_to_fun` checks whether necessary `has_coe_to_fun` instances are declared.
* `linter.check_reducibility` checks whether non-instances with a class as type are reducible.
-/
open tactic
/-- Pretty prints a list of arguments of a declaration. Assumes `l` is a list of argument positions
and binders (or any other element that can be pretty printed).
`l` can be obtained e.g. by applying `list.indexes_values` to a list obtained by
`get_pi_binders`. -/
meta def print_arguments {α} [has_to_tactic_format α] (l : list (ℕ × α)) : tactic string := do
fs ← l.mmap (λ ⟨n, b⟩, (λ s, to_fmt "argument " ++ to_fmt (n+1) ++ ": " ++ s) <$> pp b),
return $ fs.to_string_aux tt
/-- checks whether an instance that always applies has priority ≥ 1000. -/
private meta def instance_priority (d : declaration) : tactic (option string) := do
let nm := d.to_name,
b ← is_instance nm,
/- return `none` if `d` is not an instance -/
if ¬ b then return none else do
(is_persistent, prio) ← has_attribute `instance nm,
/- return `none` if `d` is has low priority -/
if prio < 1000 then return none else do
(_, tp) ← open_pis d.type,
tp ← whnf tp transparency.none,
let (fn, args) := tp.get_app_fn_args,
cls ← get_decl fn.const_name,
let (pi_args, _) := cls.type.pi_binders,
guard (args.length = pi_args.length),
/- List all the arguments of the class that block type-class inference from firing
(if they are metavariables). These are all the arguments except instance-arguments and
out-params. -/
let relevant_args := (args.zip pi_args).filter_map $ λ⟨e, ⟨_, info, tp⟩⟩,
if info = binder_info.inst_implicit ∨ tp.get_app_fn.is_constant_of `out_param
then none else some e,
let always_applies := relevant_args.all expr.is_local_constant ∧ relevant_args.nodup,
if always_applies then return $ some "set priority below 1000" else return none
/--
There are places where typeclass arguments are specified with implicit `{}` brackets instead of
the usual `[]` brackets. This is done when the instances can be inferred because they are implicit
arguments to the type of one of the other arguments. When they can be inferred from these other
arguments, it is faster to use this method than to use type class inference.
For example, when writing lemmas about `(f : α →+* β)`, it is faster to specify the fact that `α`
and `β` are `semiring`s as `{rα : semiring α} {rβ : semiring β}` rather than the usual
`[semiring α] [semiring β]`.
-/
library_note "implicit instance arguments"
/--
Certain instances always apply during type-class resolution. For example, the instance
`add_comm_group.to_add_group {α} [add_comm_group α] : add_group α` applies to all type-class
resolution problems of the form `add_group _`, and type-class inference will then do an
exhaustive search to find a commutative group. These instances take a long time to fail.
Other instances will only apply if the goal has a certain shape. For example
`int.add_group : add_group ℤ` or
`add_group.prod {α β} [add_group α] [add_group β] : add_group (α × β)`. Usually these instances
will fail quickly, and when they apply, they are almost the desired instance.
For this reason, we want the instances of the second type (that only apply in specific cases) to
always have higher priority than the instances of the first type (that always apply).
See also #1561.
Therefore, if we create an instance that always applies, we set the priority of these instances to
100 (or something similar, which is below the default value of 1000).
-/
library_note "lower instance priority"
/-- A linter object for checking instance priorities of instances that always apply.
This is in the default linter set. -/
@[linter] meta def linter.instance_priority : linter :=
{ test := instance_priority,
no_errors_found := "All instance priorities are good.",
errors_found := "DANGEROUS INSTANCE PRIORITIES.
The following instances always apply, and therefore should have a priority < 1000.
If you don't know what priority to choose, use priority 100.
See note [lower instance priority] for instructions to change the priority.",
auto_decls := tt }
/-- Reports declarations of types that do not have an associated `inhabited` instance. -/
private meta def has_inhabited_instance (d : declaration) : tactic (option string) := do
tt ← pure d.is_trusted | pure none,
ff ← has_attribute' `reducible d.to_name | pure none,
ff ← has_attribute' `class d.to_name | pure none,
(_, ty) ← open_pis d.type,
ty ← whnf ty,
if ty = `(Prop) then pure none else do
`(Sort _) ← whnf ty | pure none,
insts ← attribute.get_instances `instance,
insts_tys ← insts.mmap $ λ i, expr.pi_codomain <$> declaration.type <$> get_decl i,
let inhabited_insts := insts_tys.filter (λ i,
i.app_fn.const_name = ``inhabited ∨ i.app_fn.const_name = `unique),
let inhabited_tys := inhabited_insts.map (λ i, i.app_arg.get_app_fn.const_name),
if d.to_name ∈ inhabited_tys then
pure none
else
pure "inhabited instance missing"
/-- A linter for missing `inhabited` instances. -/
@[linter]
meta def linter.has_inhabited_instance : linter :=
{ test := has_inhabited_instance,
auto_decls := ff,
no_errors_found := "No types have missing inhabited instances.",
errors_found := "TYPES ARE MISSING INHABITED INSTANCES:",
is_fast := ff }
attribute [nolint has_inhabited_instance] pempty
/-- Checks whether an instance can never be applied. -/
private meta def impossible_instance (d : declaration) : tactic (option string) := do
tt ← is_instance d.to_name | return none,
(binders, _) ← get_pi_binders_nondep d.type,
let bad_arguments := binders.filter $ λ nb, nb.2.info ≠ binder_info.inst_implicit,
_ :: _ ← return bad_arguments | return none,
(λ s, some $ "Impossible to infer " ++ s) <$> print_arguments bad_arguments
/-- A linter object for `impossible_instance`. -/
@[linter] meta def linter.impossible_instance : linter :=
{ test := impossible_instance,
auto_decls := tt,
no_errors_found := "All instances are applicable.",
errors_found := "IMPOSSIBLE INSTANCES FOUND.
These instances have an argument that cannot be found during type-class resolution, and " ++
"therefore can never succeed. Either mark the arguments with square brackets (if it is a " ++
"class), or don't make it an instance." }
/-- Checks whether an instance can never be applied. -/
private meta def incorrect_type_class_argument (d : declaration) : tactic (option string) := do
(binders, _) ← get_pi_binders d.type,
let instance_arguments := binders.indexes_values $
λ b : binder, b.info = binder_info.inst_implicit,
/- the head of the type should either unfold to a class, or be a local constant.
A local constant is allowed, because that could be a class when applied to the
proper arguments. -/
bad_arguments ← instance_arguments.mfilter (λ ⟨_, b⟩, do
(_, head) ← open_pis b.type,
if head.get_app_fn.is_local_constant then return ff else do
bnot <$> is_class head),
_ :: _ ← return bad_arguments | return none,
(λ s, some $ "These are not classes. " ++ s) <$> print_arguments bad_arguments
/-- A linter object for `incorrect_type_class_argument`. -/
@[linter] meta def linter.incorrect_type_class_argument : linter :=
{ test := incorrect_type_class_argument,
auto_decls := tt,
no_errors_found := "All declarations have correct type-class arguments.",
errors_found := "INCORRECT TYPE-CLASS ARGUMENTS.
Some declarations have non-classes between [square brackets]:" }
/-- Checks whether an instance is dangerous: it creates a new type-class problem with metavariable
arguments. -/
private meta def dangerous_instance (d : declaration) : tactic (option string) := do
tt ← is_instance d.to_name | return none,
(local_constants, target) ← open_pis d.type,
let instance_arguments := local_constants.indexes_values $
λ e : expr, e.local_binding_info = binder_info.inst_implicit,
let bad_arguments := local_constants.indexes_values $ λ x,
!target.has_local_constant x &&
(x.local_binding_info ≠ binder_info.inst_implicit) &&
instance_arguments.any (λ nb, nb.2.local_type.has_local_constant x),
let bad_arguments : list (ℕ × binder) := bad_arguments.map $ λ ⟨n, e⟩, ⟨n, e.to_binder⟩,
_ :: _ ← return bad_arguments | return none,
(λ s, some $ "The following arguments become metavariables. " ++ s) <$>
print_arguments bad_arguments
/-- A linter object for `dangerous_instance`. -/
@[linter] meta def linter.dangerous_instance : linter :=
{ test := dangerous_instance,
no_errors_found := "No dangerous instances.",
errors_found := "DANGEROUS INSTANCES FOUND.\nThese instances are recursive, and create a new " ++
"type-class problem which will have metavariables.
Possible solution: remove the instance attribute or make it a local instance instead.
Currently this linter does not check whether the metavariables only occur in arguments marked " ++
"with `out_param`, in which case this linter gives a false positive.",
auto_decls := tt }
/-- Auxilliary definition for `find_nondep` -/
meta def find_nondep_aux : list expr → expr_set → tactic expr_set
| [] r := return r
| (h::hs) r :=
do type ← infer_type h,
find_nondep_aux hs $ r.union type.list_local_consts'
/-- Finds all hypotheses that don't occur in the target or other hypotheses. -/
meta def find_nondep : tactic (list expr) := do
ctx ← local_context,
tgt ← target,
lconsts ← find_nondep_aux ctx tgt.list_local_consts',
return $ ctx.filter $ λ e, !lconsts.contains e
/--
Tests whether type-class inference search will end quickly on certain unsolvable
type-class problems. This is to detect loops or very slow searches, which are problematic
(recall that normal type-class search often creates unsolvable subproblems, which have to fail
quickly for type-class inference to perform well.
We create these type-class problems by taking an instance, and removing the last hypothesis that
doesn't appear in the goal (or a later hypothesis). Note: this argument is necessarily an
instance-implicit argument if it passes the `linter.incorrect_type_class_argument`.
This tactic succeeds if `mk_instance` succeeds quickly or fails quickly with the error
message that it cannot find an instance. It fails if the tactic takes too long, or if any other
error message is raised (usually a maximum depth in the search).
-/
meta def fails_quickly (max_steps : ℕ) (d : declaration) : tactic (option string) := retrieve $ do
tt ← is_instance d.to_name | return none,
let e := d.type,
g ← mk_meta_var e,
set_goals [g],
intros,
l@(_::_) ← find_nondep | return none, -- if all arguments occur in the goal, this instance is ok
clear l.ilast,
reset_instance_cache,
state ← read,
let state_msg := "\nState:\n" ++ to_string state,
tgt ← target >>= instantiate_mvars,
sum.inr msg ← retrieve_or_report_error $ tactic.try_for max_steps $ mk_instance tgt |
return none, /- it's ok if type-class inference can find an instance with fewer hypotheses.
This happens a lot for `has_sizeof` and `has_well_founded`, but can also happen if there is a
noncomputable instance with fewer assumptions. -/
return $ if "tactic.mk_instance failed to generate instance for".is_prefix_of msg then none else
some $ (++ state_msg) $
if msg = "try_for tactic failed, timeout" then "type-class inference timed out" else msg
/--
A linter object for `fails_quickly`.
We currently set the number of steps in the type-class search pretty high.
Some instances take quite some time to fail, and we seem to run against the caching issue in
https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/odd.20repeated.20type.20class.20search
-/
@[linter] meta def linter.fails_quickly : linter :=
{ test := fails_quickly 10000,
auto_decls := tt,
no_errors_found := "No type-class searches timed out.",
errors_found := "TYPE CLASS SEARCHES TIMED OUT.
The following instances are part of a loop, or an excessively long search.
It is common that the loop occurs in a different class than the one flagged below,
but usually an instance that is part of the loop is also flagged.
To debug:
(1) run `scripts/mk_all.sh` and create a file with `import all` and
`set_option trace.class_instances true`
(2) Recreate the state shown in the error message. You can do this easily by copying the type of
the instance (the output of `#check @my_instance`), turning this into an example and removing the
last argument in square brackets. Prove the example using `by apply_instance`.
For example, if `additive.topological_add_group` raises an error, run
```
example {G : Type*} [topological_space G] [group G] : topological_add_group (additive G) :=
by apply_instance
```
(3) What error do you get?
(3a) If the error is \"tactic.mk_instance failed to generate instance\",
there might be nothing wrong. But it might take unreasonably long for the type-class inference to
fail. Check the trace to see if type-class inference takes any unnecessary long unexpected turns.
If not, feel free to increase the value in the definition of the linter `fails_quickly`.
(3b) If the error is \"maximum class-instance resolution depth has been reached\" there is almost
certainly a loop in the type-class inference. Find which instance causes the type-class inference to
go astray, and fix that instance.",
is_fast := ff }
/-- Checks that all uses of the `@[class]` attribute apply to structures or inductive types.
This is future-proofing for lean 4, which no longer supports `@[class] def`. -/
private meta def class_structure (n : name) : tactic (option string) := do
is_class ← has_attribute' `class n,
if is_class then do
env ← get_env,
pure $ if env.is_inductive n then none else
"is a non-structure or inductive type marked @[class]"
else pure none
/-- A linter object for `class_structure`. -/
@[linter] meta def linter.class_structure : linter :=
{ test := λ d, class_structure d.to_name,
auto_decls := tt,
no_errors_found := "All classes are structures.",
errors_found := "USE OF @[class] def IS DISALLOWED:" }
/--
Tests whether there is no instance of type `has_coe α t` where `α` is a variable,
or `has_coe t α` where `α` does not occur in `t`.
See note [use has_coe_t].
-/
private meta def has_coe_variable (d : declaration) : tactic (option string) := do
tt ← is_instance d.to_name | return none,
`(has_coe %%a %%b) ← return d.type.pi_codomain | return none,
if a.is_var then
return $ some $ "illegal instance, first argument is variable"
else if b.is_var ∧ ¬ b.occurs a then
return $ some $ "illegal instance, second argument is variable not occurring in first argument"
else
return none
/-- A linter object for `has_coe_variable`. -/
@[linter] meta def linter.has_coe_variable : linter :=
{ test := has_coe_variable,
auto_decls := tt,
no_errors_found := "No invalid `has_coe` instances.",
errors_found := "INVALID `has_coe` INSTANCES.
Make the following declarations instances of the class `has_coe_t` instead of `has_coe`." }
/-- Checks whether a declaration is prop-valued and takes an `inhabited _` argument that is unused
elsewhere in the type. In this case, that argument can be replaced with `nonempty _`. -/
private meta def inhabited_nonempty (d : declaration) : tactic (option string) :=
do tt ← is_prop d.type | return none,
(binders, _) ← get_pi_binders_nondep d.type,
let inhd_binders := binders.filter $ λ pr, pr.2.type.is_app_of `inhabited,
if inhd_binders.length = 0 then return none
else (λ s, some $ "The following `inhabited` instances should be `nonempty`. " ++ s) <$>
print_arguments inhd_binders
/-- A linter object for `inhabited_nonempty`. -/
@[linter] meta def linter.inhabited_nonempty : linter :=
{ test := inhabited_nonempty,
auto_decls := ff,
no_errors_found := "No uses of `inhabited` arguments should be replaced with `nonempty`.",
errors_found := "USES OF `inhabited` SHOULD BE REPLACED WITH `nonempty`." }
/-- Checks whether a declaration is `Prop`-valued and takes a `decidable* _`
hypothesis that is unused lsewhere in the type.
In this case, that hypothesis can be replaced with `classical` in the proof.
Theorems in the `decidable` namespace are exempt from the check. -/
private meta def decidable_classical (d : declaration) : tactic (option string) :=
do tt ← is_prop d.type | return none,
ff ← pure $ (`decidable).is_prefix_of d.to_name | return none,
(binders, _) ← get_pi_binders_nondep d.type,
let deceq_binders := binders.filter $ λ pr, pr.2.type.is_app_of `decidable_eq
∨ pr.2.type.is_app_of `decidable_pred ∨ pr.2.type.is_app_of `decidable_rel
∨ pr.2.type.is_app_of `decidable,
if deceq_binders.length = 0 then return none
else (λ s, some $ "The following `decidable` hypotheses should be replaced with
`classical` in the proof. " ++ s) <$>
print_arguments deceq_binders
/-- A linter object for `decidable_classical`. -/
@[linter] meta def linter.decidable_classical : linter :=
{ test := decidable_classical,
auto_decls := ff,
no_errors_found := "No uses of `decidable` arguments should be replaced with `classical`.",
errors_found := "USES OF `decidable` SHOULD BE REPLACED WITH `classical` IN THE PROOF." }
/- The file `logic/basic.lean` emphasizes the differences between what holds under classical
and non-classical logic. It makes little sense to make all these lemmas classical, so we add them
to the list of lemmas which are not checked by the linter `decidable_classical`. -/
attribute [nolint decidable_classical] dec_em dec_em' not.decidable_imp_symm
private meta def has_coe_to_fun_linter (d : declaration) : tactic (option string) :=
retrieve $ do
tt ← return d.is_trusted | pure none,
mk_meta_var d.type >>= set_goals ∘ pure,
args ← unfreezing intros,
expr.sort _ ← target | pure none,
let ty : expr := (expr.const d.to_name d.univ_levels).mk_app args,
some coe_fn_inst ←
try_core $ to_expr ``(_root_.has_coe_to_fun %%ty _) >>= mk_instance | pure none,
set_bool_option `pp.all true,
some trans_inst@(expr.app (expr.app _ trans_inst_1) trans_inst_2) ←
try_core $ to_expr ``(@_root_.coe_fn_trans %%ty _ _ _ _) | pure none,
tt ← succeeds $ unify trans_inst coe_fn_inst transparency.reducible | pure none,
set_bool_option `pp.all true,
trans_inst_1 ← pp trans_inst_1,
trans_inst_2 ← pp trans_inst_2,
pure $ format.to_string $
"`has_coe_to_fun` instance is definitionally equal to a transitive instance composed of: " ++
trans_inst_1.group.indent 2 ++
format.line ++ "and" ++
trans_inst_2.group.indent 2
/-- Linter that checks whether `has_coe_to_fun` instances comply with Note [function coercion]. -/
@[linter] meta def linter.has_coe_to_fun : linter :=
{ test := has_coe_to_fun_linter,
auto_decls := tt,
no_errors_found := "has_coe_to_fun is used correctly",
errors_found := "INVALID/MISSING `has_coe_to_fun` instances.
You should add a `has_coe_to_fun` instance for the following types.
See Note [function coercion]." }
/--
Checks whether an instance contains a semireducible non-instance with a class as
type in its value. We add some restrictions to get not too many false positives:
* We only consider classes with an `add` or `mul` field, since those classes are most likely to
occur as a field to another class, and be an extension of another class.
* We only consider instances of type-valued classes and non-instances that are definitions.
* We currently ignore declarations `foo` that have a `foo._main` declaration. We could look inside,
or at the generated equation lemmas, but it's unlikely that there are many problematic instances
defined using the equation compiler.
-/
meta def check_reducible_non_instances (d : declaration) : tactic (option string) := do
tt ← is_instance d.to_name | return none,
ff ← is_prop d.type | return none,
env ← get_env,
-- We only check if the class of the instance contains an `add` or a `mul` field.
let cls := d.type.pi_codomain.get_app_fn.const_name,
some constrs ← return $ env.structure_fields cls | return none,
tt ← return $ constrs.mem `add || constrs.mem `mul | return none,
l ← d.value.list_constant.mfilter $ λ nm, do {
d ← env.get nm,
ff ← is_instance nm | return ff,
tt ← is_class d.type | return ff,
tt ← return d.is_definition | return ff,
-- We only check if the class of the non-instance contains an `add` or a `mul` field.
let cls := d.type.pi_codomain.get_app_fn.const_name,
some constrs ← return $ env.structure_fields cls | return ff,
tt ← return $ constrs.mem `add || constrs.mem `mul | return ff,
ff ← has_attribute' `reducible nm | return ff,
return tt },
if l.empty then return none else
-- we currently ignore declarations that have a `foo._main` declaration.
if l.to_list = [d.to_name ++ `_main] then return none else
return $ some $ "This instance contains the declarations " ++ to_string l.to_list ++
", which are semireducible non-instances."
/-- A linter that checks whether an instance contains a semireducible non-instance. -/
@[linter]
meta def linter.check_reducibility : linter :=
{ test := check_reducible_non_instances,
auto_decls := ff,
no_errors_found :=
"All non-instances are reducible.",
errors_found := "THE FOLLOWING INSTANCES MIGHT NOT REDUCE.
These instances contain one or more declarations that are not instances and are also not marked
`@[reducible]`. This means that type-class inference cannot unfold these declarations, " ++
"which might mean that type-class inference cannot infer that two instances are definitionally " ++
"equal. This can cause unexpected errors when this class occurs " ++
"as an *argument* to a type-class problem. See note [reducible non-instances].",
is_fast := tt }
|
7f2c7679f42bfc4bcff3326d8ddaec4e8761a0d5 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run_cmd_type.lean | 2852cfeea97a0810b07e6bd7c8be7c76d2498500 | [
"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 | 64 | lean | meta def foo: lean.parser unit :=
do
return ()
run_cmd foo
|
66aaa18a5081220d1967b5786a654b80d6bf9de9 | e514e8b939af519a1d5e9b30a850769d058df4e9 | /src/lib_unused/list.lean | e3be5aaecd176f1222dd7a53e206f4a470f23ee0 | [] | no_license | semorrison/lean-rewrite-search | dca317c5a52e170fb6ffc87c5ab767afb5e3e51a | e804b8f2753366b8957be839908230ee73f9e89f | refs/heads/master | 1,624,051,754,485 | 1,614,160,817,000 | 1,614,160,817,000 | 162,660,605 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 295 | lean | -- import data.option.defs
-- import data.buffer
-- universes u v w
-- namespace list
-- def min {α : Type u} (l : list α) [has_le α] [@decidable_rel α has_le.le] : option α :=
-- l.foldl (λ o a, match o with | none := some a | some b := if b ≤ a then b else a end) none
-- end list
|
fd3ef844a9c46b2292355a8394d745659bc164a4 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/simple.lean | eb096162ebe784cd9b3f768ae02f0bfccca3e620 | [] | 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,870 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel, Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.limits.shapes.zero
import Mathlib.category_theory.limits.shapes.kernels
import Mathlib.category_theory.abelian.basic
import Mathlib.PostPort
universes v u l
namespace Mathlib
namespace category_theory
/-- An object is simple if monomorphisms into it are (exclusively) either isomorphisms or zero. -/
-- This is a constructive definition, from which we can extract an inverse for `f` given `f ≠ 0`.
-- We show below that although it contains data, it is a subsingleton.
class simple {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C)
where
mono_is_iso_equiv_nonzero : {Y : C} → (f : Y ⟶ X) → [_inst_3 : mono f] → is_iso f ≃ f ≠ 0
theorem simple.ext {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {a : simple X} {b : simple X} : a = b := sorry
protected instance subsingleton_simple {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C) : subsingleton (simple X) :=
subsingleton.intro simple.ext
/-- A nonzero monomorphism to a simple object is an isomorphism. -/
def is_iso_of_mono_of_nonzero {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : f ≠ 0) : is_iso f :=
coe_fn (equiv.symm (simple.mono_is_iso_equiv_nonzero f)) w
theorem kernel_zero_of_nonzero_from_simple {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [limits.has_kernel f] (w : f ≠ 0) : limits.kernel.ι f = 0 :=
decidable.by_contradiction fun (h : ¬limits.kernel.ι f = 0) => w (limits.eq_zero_of_epi_kernel f)
theorem mono_to_simple_zero_of_not_iso {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : is_iso f → False) : f = 0 :=
decidable.by_contradiction fun (h : ¬f = 0) => w (is_iso_of_mono_of_nonzero h)
theorem id_nonzero {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C) [simple X] : 𝟙 ≠ 0 :=
coe_fn (simple.mono_is_iso_equiv_nonzero 𝟙) (is_iso.id X)
/-- We don't want the definition of 'simple' to include the zero object, so we check that here. -/
theorem zero_not_simple {C : Type u} [category C] [limits.has_zero_morphisms C] [limits.has_zero_object C] [simple 0] : False :=
coe_fn (simple.mono_is_iso_equiv_nonzero 0) (is_iso.mk 0) rfl
-- We next make the dual arguments, but for this we must be in an abelian category.
/-- In an abelian category, an object satisfying the dual of the definition of a simple object is
simple. -/
def simple_of_cosimple {C : Type u} [category C] [abelian C] (X : C) (h : {Z : C} → (f : X ⟶ Z) → [_inst_3 : epi f] → is_iso f ≃ f ≠ 0) : simple X :=
simple.mk
fun (Y : C) (f : Y ⟶ X) (I : mono f) =>
equiv_of_subsingleton_of_subsingleton sorry fun (hf : f ≠ 0) => abelian.is_iso_of_mono_of_epi f
/-- A nonzero epimorphism from a simple object is an isomorphism. -/
def is_iso_of_epi_of_nonzero {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : f ≠ 0) : is_iso f :=
abelian.is_iso_of_mono_of_epi f
theorem cokernel_zero_of_nonzero_to_simple {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [limits.has_cokernel f] (w : f ≠ 0) : limits.cokernel.π f = 0 :=
decidable.by_contradiction fun (h : ¬limits.cokernel.π f = 0) => w (limits.eq_zero_of_mono_cokernel f)
theorem epi_from_simple_zero_of_not_iso {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : is_iso f → False) : f = 0 :=
decidable.by_contradiction fun (h : ¬f = 0) => w (is_iso_of_epi_of_nonzero h)
|
ee114ff83841f9db59c8539251e93685440e85ad | 626e312b5c1cb2d88fca108f5933076012633192 | /src/field_theory/fixed.lean | b78c448b6785344dc621d4546fed7be2590a53e3 | [
"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 | 14,503 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import algebra.polynomial.group_ring_action
import field_theory.normal
import field_theory.separable
import field_theory.tower
import ring_theory.polynomial
/-!
# Fixed field under a group action.
This is the basis of the Fundamental Theorem of Galois Theory.
Given a (finite) group `G` that acts on a field `F`, we define `fixed_points G F`,
the subfield consisting of elements of `F` fixed_points by every element of `G`.
This subfield is then normal and separable, and in addition (TODO) if `G` acts faithfully on `F`
then `finrank (fixed_points G F) F = fintype.card G`.
## Main Definitions
- `fixed_points G F`, the subfield consisting of elements of `F` fixed_points by every element of
`G`, where `G` is a group that acts on `F`.
-/
noncomputable theory
open_locale classical big_operators
open mul_action finset finite_dimensional
universes u v w
variables {M : Type u} [monoid M]
variables (G : Type u) [group G]
variables (F : Type v) [field F] [mul_semiring_action M F] [mul_semiring_action G F] (m : M)
/-- The subfield of F fixed by the field endomorphism `m`. -/
def fixed_by.subfield : subfield F :=
{ carrier := fixed_by M F m,
zero_mem' := smul_zero m,
add_mem' := λ x y hx hy, (smul_add m x y).trans $ congr_arg2 _ hx hy,
neg_mem' := λ x hx, (smul_neg m x).trans $ congr_arg _ hx,
one_mem' := smul_one m,
mul_mem' := λ x y hx hy, (smul_mul' m x y).trans $ congr_arg2 _ hx hy,
inv_mem' := λ x hx, (smul_inv' F m x).trans $ congr_arg _ hx }
section invariant_subfields
variables (M) {F}
/-- A typeclass for subrings invariant under a `mul_semiring_action`. -/
class is_invariant_subfield (S : subfield F) : Prop :=
(smul_mem : ∀ (m : M) {x : F}, x ∈ S → m • x ∈ S)
variable (S : subfield F)
instance is_invariant_subfield.to_mul_semiring_action [is_invariant_subfield M S] :
mul_semiring_action M S :=
{ smul := λ m x, ⟨m • x, is_invariant_subfield.smul_mem m x.2⟩,
one_smul := λ s, subtype.eq $ one_smul M s,
mul_smul := λ m₁ m₂ s, subtype.eq $ mul_smul m₁ m₂ s,
smul_add := λ m s₁ s₂, subtype.eq $ smul_add m s₁ s₂,
smul_zero := λ m, subtype.eq $ smul_zero m,
smul_one := λ m, subtype.eq $ smul_one m,
smul_mul := λ m s₁ s₂, subtype.eq $ smul_mul' m s₁ s₂ }
instance [is_invariant_subfield M S] : is_invariant_subring M (S.to_subring) :=
{ smul_mem := is_invariant_subfield.smul_mem }
end invariant_subfields
namespace fixed_points
variable (M)
-- we use `subfield.copy` so that the underlying set is `fixed_points M F`
/-- The subfield of fixed points by a monoid action. -/
def subfield : subfield F :=
subfield.copy (⨅ (m : M), fixed_by.subfield F m) (fixed_points M F)
(by { ext z, simp [fixed_points, fixed_by.subfield, infi, subfield.mem_Inf] })
instance : is_invariant_subfield M (fixed_points.subfield M F) :=
{ smul_mem := λ g x hx g', by rw [hx, hx] }
instance : smul_comm_class M (fixed_points.subfield M F) F :=
{ smul_comm := λ m f f', show m • (↑f * f') = f * (m • f'), by rw [smul_mul', f.prop m] }
instance smul_comm_class' : smul_comm_class (fixed_points.subfield M F) M F :=
smul_comm_class.symm _ _ _
@[simp] theorem smul (m : M) (x : fixed_points.subfield M F) : m • x = x :=
subtype.eq $ x.2 m
-- Why is this so slow?
@[simp] theorem smul_polynomial (m : M) (p : polynomial (fixed_points.subfield M F)) : m • p = p :=
polynomial.induction_on p
(λ x, by rw [polynomial.smul_C, smul])
(λ p q ihp ihq, by rw [smul_add, ihp, ihq])
(λ n x ih, by rw [smul_mul', polynomial.smul_C, smul, smul_pow, polynomial.smul_X])
instance : algebra (fixed_points.subfield M F) F :=
algebra.of_subring (fixed_points.subfield M F).to_subring
theorem coe_algebra_map :
algebra_map (fixed_points.subfield M F) F = subfield.subtype (fixed_points.subfield M F) :=
rfl
lemma linear_independent_smul_of_linear_independent {s : finset F} :
linear_independent (fixed_points.subfield G F) (λ i : (s : set F), (i : F)) →
linear_independent F (λ i : (s : set F), mul_action.to_fun G F i) :=
begin
haveI : is_empty ((∅ : finset F) : set F) := ⟨subtype.prop⟩,
refine finset.induction_on s (λ _, linear_independent_empty_type)
(λ a s has ih hs, _),
rw coe_insert at hs ⊢,
rw linear_independent_insert (mt mem_coe.1 has) at hs,
rw linear_independent_insert' (mt mem_coe.1 has), refine ⟨ih hs.1, λ ha, _⟩,
rw finsupp.mem_span_image_iff_total at ha, rcases ha with ⟨l, hl, hla⟩,
rw [finsupp.total_apply_of_mem_supported F hl] at hla,
suffices : ∀ i ∈ s, l i ∈ fixed_points.subfield G F,
{ replace hla := (sum_apply _ _ (λ i, l i • to_fun G F i)).symm.trans (congr_fun hla 1),
simp_rw [pi.smul_apply, to_fun_apply, one_smul] at hla,
refine hs.2 (hla ▸ submodule.sum_mem _ (λ c hcs, _)),
change (⟨l c, this c hcs⟩ : fixed_points.subfield G F) • c ∈ _,
exact submodule.smul_mem _ _ (submodule.subset_span $ mem_coe.2 hcs) },
intros i his g,
refine eq_of_sub_eq_zero (linear_independent_iff'.1 (ih hs.1) s.attach (λ i, g • l i - l i) _
⟨i, his⟩ (mem_attach _ _) : _),
refine (@sum_attach _ _ s _ (λ i, (g • l i - l i) • (to_fun G F) i)).trans _, ext g', dsimp only,
conv_lhs { rw sum_apply, congr, skip, funext, rw [pi.smul_apply, sub_smul, smul_eq_mul] },
rw [sum_sub_distrib, pi.zero_apply, sub_eq_zero],
conv_lhs { congr, skip, funext,
rw [to_fun_apply, ← mul_inv_cancel_left g g', mul_smul, ← smul_mul', ← to_fun_apply _ x] },
show ∑ x in s, g • (λ y, l y • to_fun G F y) x (g⁻¹ * g') =
∑ x in s, (λ y, l y • to_fun G F y) x g',
rw [← smul_sum, ← sum_apply _ _ (λ y, l y • to_fun G F y),
← sum_apply _ _ (λ y, l y • to_fun G F y)], dsimp only,
rw [hla, to_fun_apply, to_fun_apply, smul_smul, mul_inv_cancel_left]
end
variables [fintype G] (x : F)
/-- `minpoly G F x` is the minimal polynomial of `(x : F)` over `fixed_points G F`. -/
def minpoly : polynomial (fixed_points.subfield G F) :=
(prod_X_sub_smul G F x).to_subring (fixed_points.subfield G F).to_subring $ λ c hc g,
let ⟨n, hc0, hn⟩ := polynomial.mem_frange_iff.1 hc in hn.symm ▸ prod_X_sub_smul.coeff G F x g n
namespace minpoly
theorem monic : (minpoly G F x).monic :=
by { simp only [minpoly, polynomial.monic_to_subring], exact prod_X_sub_smul.monic G F x }
theorem eval₂ : polynomial.eval₂ (subring.subtype $ (fixed_points.subfield G F).to_subring) x
(minpoly G F x) = 0 :=
begin
rw [← prod_X_sub_smul.eval G F x, polynomial.eval₂_eq_eval_map],
simp only [minpoly, polynomial.map_to_subring],
end
theorem eval₂' :
polynomial.eval₂ (subfield.subtype $ (fixed_points.subfield G F)) x (minpoly G F x) = 0 :=
eval₂ G F x
theorem ne_one :
minpoly G F x ≠ (1 : polynomial (fixed_points.subfield G F)) :=
λ H, have _ := eval₂ G F x,
(one_ne_zero : (1 : F) ≠ 0) $ by rwa [H, polynomial.eval₂_one] at this
theorem of_eval₂ (f : polynomial (fixed_points.subfield G F))
(hf : polynomial.eval₂ (subfield.subtype $ fixed_points.subfield G F) x f = 0) :
minpoly G F x ∣ f :=
begin
erw [← polynomial.map_dvd_map' (subfield.subtype $ fixed_points.subfield G F),
minpoly, polynomial.map_to_subring _ (subfield G F).to_subring, prod_X_sub_smul],
refine fintype.prod_dvd_of_coprime
(polynomial.pairwise_coprime_X_sub $ mul_action.injective_of_quotient_stabilizer G x)
(λ y, quotient_group.induction_on y $ λ g, _),
rw [polynomial.dvd_iff_is_root, polynomial.is_root.def, mul_action.of_quotient_stabilizer_mk,
polynomial.eval_smul',
← subfield.to_subring.subtype_eq_subtype,
← is_invariant_subring.coe_subtype_hom' G (fixed_points.subfield G F).to_subring,
← mul_semiring_action_hom.coe_polynomial, ← mul_semiring_action_hom.map_smul,
smul_polynomial, mul_semiring_action_hom.coe_polynomial,
is_invariant_subring.coe_subtype_hom', polynomial.eval_map,
subfield.to_subring.subtype_eq_subtype, hf, smul_zero]
end
/- Why is this so slow? -/
theorem irreducible_aux (f g : polynomial (fixed_points.subfield G F))
(hf : f.monic) (hg : g.monic) (hfg : f * g = minpoly G F x) :
f = 1 ∨ g = 1 :=
begin
have hf2 : f ∣ minpoly G F x,
{ rw ← hfg, exact dvd_mul_right _ _ },
have hg2 : g ∣ minpoly G F x,
{ rw ← hfg, exact dvd_mul_left _ _ },
have := eval₂ G F x,
rw [← hfg, polynomial.eval₂_mul, mul_eq_zero] at this,
cases this,
{ right,
have hf3 : f = minpoly G F x,
{ exact polynomial.eq_of_monic_of_associated hf (monic G F x)
(associated_of_dvd_dvd hf2 $ @of_eval₂ G _ F _ _ _ x f this) },
rwa [← mul_one (minpoly G F x), hf3,
mul_right_inj' (monic G F x).ne_zero] at hfg },
{ left,
have hg3 : g = minpoly G F x,
{ exact polynomial.eq_of_monic_of_associated hg (monic G F x)
(associated_of_dvd_dvd hg2 $ @of_eval₂ G _ F _ _ _ x g this) },
rwa [← one_mul (minpoly G F x), hg3,
mul_left_inj' (monic G F x).ne_zero] at hfg }
end
theorem irreducible : irreducible (minpoly G F x) :=
(polynomial.irreducible_of_monic (monic G F x) (ne_one G F x)).2 (irreducible_aux G F x)
end minpoly
theorem is_integral : is_integral (fixed_points.subfield G F) x :=
⟨minpoly G F x, minpoly.monic G F x, minpoly.eval₂ G F x⟩
theorem minpoly_eq_minpoly :
minpoly G F x = _root_.minpoly (fixed_points.subfield G F) x :=
minpoly.unique' (minpoly.irreducible G F x)
(minpoly.eval₂ G F x) (minpoly.monic G F x)
instance normal : normal (fixed_points.subfield G F) F :=
⟨λ x, is_integral G F x, λ x, (polynomial.splits_id_iff_splits _).1 $
by { rw [← minpoly_eq_minpoly, minpoly,
coe_algebra_map, ← subfield.to_subring.subtype_eq_subtype,
polynomial.map_to_subring _ (fixed_points.subfield G F).to_subring, prod_X_sub_smul],
exact polynomial.splits_prod _ (λ _ _, polynomial.splits_X_sub_C _) }⟩
instance separable : is_separable (fixed_points.subfield G F) F :=
⟨λ x, is_integral G F x,
λ x, by {
-- this was a plain rw when we were using unbundled subrings
erw [← minpoly_eq_minpoly,
← polynomial.separable_map (fixed_points.subfield G F).subtype,
minpoly, polynomial.map_to_subring _ ((subfield G F).to_subring) ],
exact polynomial.separable_prod_X_sub_C_iff.2 (injective_of_quotient_stabilizer G x) }⟩
lemma dim_le_card : module.rank (fixed_points.subfield G F) F ≤ fintype.card G :=
begin
refine dim_le (λ s hs, cardinal.nat_cast_le.1 _),
rw [← @dim_fun' F G, ← cardinal.lift_nat_cast.{v (max u v)},
cardinal.finset_card, ← cardinal.lift_id (module.rank F (G → F))],
exact cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max u v)}
(linear_independent_smul_of_linear_independent G F hs)
end
instance : finite_dimensional (fixed_points.subfield G F) F :=
is_noetherian.iff_dim_lt_omega.2 $
lt_of_le_of_lt (dim_le_card G F) (cardinal.nat_lt_omega _)
lemma finrank_le_card : finrank (fixed_points.subfield G F) F ≤ fintype.card G :=
begin
rw [← cardinal.nat_cast_le, finrank_eq_dim],
apply dim_le_card,
end
end fixed_points
lemma linear_independent_to_linear_map (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [integral_domain A] [algebra R A] [integral_domain B] [algebra R B] :
linear_independent B (alg_hom.to_linear_map : (A →ₐ[R] B) → (A →ₗ[R] B)) :=
have linear_independent B (linear_map.lto_fun R A B ∘ alg_hom.to_linear_map),
from ((linear_independent_monoid_hom A B).comp
(coe : (A →ₐ[R] B) → (A →* B))
(λ f g hfg, alg_hom.ext $ monoid_hom.ext_iff.1 hfg) : _),
this.of_comp _
lemma cardinal_mk_alg_hom (K : Type u) (V : Type v) (W : Type w)
[field K] [field V] [algebra K V] [finite_dimensional K V]
[field W] [algebra K W] [finite_dimensional K W] :
cardinal.mk (V →ₐ[K] W) ≤ finrank W (V →ₗ[K] W) :=
cardinal_mk_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V W
noncomputable instance alg_hom.fintype (K : Type u) (V : Type v) (W : Type w)
[field K] [field V] [algebra K V] [finite_dimensional K V]
[field W] [algebra K W] [finite_dimensional K W] :
fintype (V →ₐ[K] W) :=
classical.choice $ cardinal.lt_omega_iff_fintype.1 $
lt_of_le_of_lt (cardinal_mk_alg_hom K V W) (cardinal.nat_lt_omega _)
noncomputable instance alg_equiv.fintype (K : Type u) (V : Type v)
[field K] [field V] [algebra K V] [finite_dimensional K V] :
fintype (V ≃ₐ[K] V) :=
fintype.of_equiv (V →ₐ[K] V) (alg_equiv_equiv_alg_hom K V).symm
lemma finrank_alg_hom (K : Type u) (V : Type v)
[field K] [field V] [algebra K V] [finite_dimensional K V] :
fintype.card (V →ₐ[K] V) ≤ finrank V (V →ₗ[K] V) :=
fintype_card_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V V
namespace fixed_points
theorem finrank_eq_card (G : Type u) (F : Type v) [group G] [field F]
[fintype G] [mul_semiring_action G F] [has_faithful_scalar G F] :
finrank (fixed_points.subfield G F) F = fintype.card G :=
le_antisymm (fixed_points.finrank_le_card G F) $
calc fintype.card G
≤ fintype.card (F →ₐ[fixed_points.subfield G F] F) :
fintype.card_le_of_injective _ (mul_semiring_action.to_alg_hom_injective _ F)
... ≤ finrank F (F →ₗ[fixed_points.subfield G F] F) : finrank_alg_hom (fixed_points G F) F
... = finrank (fixed_points.subfield G F) F : finrank_linear_map' _ _ _
/-- `mul_semiring_action.to_alg_hom` is bijective. -/
theorem to_alg_hom_bijective (G : Type u) (F : Type v) [group G] [field F]
[fintype G] [mul_semiring_action G F] [has_faithful_scalar G F] :
function.bijective (mul_semiring_action.to_alg_hom _ _ : G → F →ₐ[subfield G F] F) :=
begin
rw fintype.bijective_iff_injective_and_card,
split,
{ exact mul_semiring_action.to_alg_hom_injective _ F },
{ apply le_antisymm,
{ exact fintype.card_le_of_injective _ (mul_semiring_action.to_alg_hom_injective _ F) },
{ rw ← finrank_eq_card G F,
exact has_le.le.trans_eq (finrank_alg_hom _ F) (finrank_linear_map' _ _ _) } },
end
/-- Bijection between G and algebra homomorphisms that fix the fixed points -/
def to_alg_hom_equiv (G : Type u) (F : Type v) [group G] [field F]
[fintype G] [mul_semiring_action G F] [has_faithful_scalar G F] :
G ≃ (F →ₐ[fixed_points.subfield G F] F) :=
equiv.of_bijective _ (to_alg_hom_bijective G F)
end fixed_points
|
6cc8e06501a2d810b38d2f8bc4f25c9e0e32a2e5 | c65da2ef2a10991ca5f329be68b231f8f5aed210 | /src/exercises_sources/friday/topology.lean | 90c0ac09373e7dc3db35469449cb3dbf2bd6f4a3 | [] | no_license | arademaker/lftcm2020 | 5d6aa964837cfea82a98d32b6e2a0b9a6c97dfdc | e83aab9d2c514c4fccfb6d6043200f1bdc7b3841 | refs/heads/master | 1,672,633,876,208 | 1,602,728,317,000 | 1,602,728,317,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,136 | lean | import topology.metric_space.basic
open_locale classical filter topological_space
namespace lftcm
open filter set
/-!
# Filters
## Definition of filters
-/
def principal {α : Type*} (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := begin
sorry
end,
sets_of_superset := begin
sorry
end,
inter_sets := begin
sorry
end}
def at_top : filter ℕ :=
{ sets := {s | ∃ a, ∀ b, a ≤ b → b ∈ s},
univ_sets := begin
sorry
end,
sets_of_superset := begin
sorry
end,
inter_sets := begin
sorry
end}
-- The next exercise is slightly more tricky, you should probably keep it for later
def nhds (x : ℝ) : filter ℝ :=
{ sets := {s | ∃ ε > 0, Ioo (x - ε) (x + ε) ⊆ s},
univ_sets := begin
sorry
end,
sets_of_superset := begin
sorry
end,
inter_sets := begin
sorry
end}
/-
The filter axiom are also available as standalone lemmas where the filter argument is implicit
Compare
-/
#check @filter.sets_of_superset
#check @mem_sets_of_superset
-- And analogously:
#check @inter_mem_sets
/-!
## Definition of "tends to"
-/
-- We'll practive using tendsto by reproving the composition lemma `tendsto.comp` from mathlib
-- Let's first use the concrete definition recorded by `tendsto_def`
#check @tendsto_def
#check @preimage_comp
example {α β γ : Type*} {A : filter α} {B : filter β} {C : filter γ} {f : α → β} {g : β → γ}
(hf : tendsto f A B) (hg : tendsto g B C) : tendsto (g ∘ f) A C :=
begin
sorry
end
-- Now let's get functorial (same statement as above, different proof packaging).
example {α β γ : Type*} {A : filter α} {B : filter β} {C : filter γ} {f : α → β} {g : β → γ}
(hf : tendsto f A B) (hg : tendsto g B C) : tendsto (g ∘ f) A C :=
begin
calc
map (g ∘ f) A = map g (map f A) : sorry
... ≤ map g B : sorry
... ≤ C : sorry,
end
/-
Let's now focus on the pull-back operation `filter.comap` which takes `f : X → Y`
and a filter `G` on `Y` and returns a filter on `X`.
-/
#check @mem_comap_sets -- this is by definition, the proof is `iff.rfl`
-- It also help to record a special case of one implication:
#check @preimage_mem_comap
-- The following exercise, which reproves `comap_ne_bot_iff` can start using
#check @forall_sets_nonempty_iff_ne_bot
example {α β : Type*} {f : filter β} {m : α → β} :
(comap m f).ne_bot ↔ ∀ t ∈ f, ∃ a, m a ∈ t :=
begin
sorry
end
/-!
## Properties holding eventually
-/
/--
The next exercise only needs the definition of filters and the fact that
`∀ᶠ x in f, p x` is a notation for `{x | p x} ∈ f`.
It is called `eventually_and` in mathlib, and won't be needed below.
For instance, applied to `α = ℕ` and the `at_top` filter above, it says
that, given two predicates `p` and `q` on natural numbers,
p n and q n for n large enough if and only if p n holds for n large enough
and q n holds for n large enough.
-/
example {α : Type*} {p q : α → Prop} {f : filter α} :
(∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) :=
begin
sorry
end
/-!
## Topological spaces
-/
section
-- This is how we can talk about two topological spaces X and Y
variables {X Y : Type*} [topological_space X] [topological_space Y]
/-
Given a topological space `X` and some `A : set X`, we have the usual zoo of predicates
`is_open A`, `is_closed A`, `is_connected A`, `is_compact A` (and some more)
There are also additional type classes referring to properties of `X` itself,
like `compact_space X` or `connected_space X`
-/
/-- We can talk about continuous functions from `X` to `Y` -/
example (f : X → Y) : continuous f ↔ ∀ V, is_open V → is_open (f ⁻¹' V) := iff.rfl
/- Each point `x` of a topological space has a neighborhood filter `𝓝 x`
made of sets containing an open set containing `x`.
It is always a proper filter, as recorded by `nhds_ne_bot`
Asking for continuity is the same as asking for continuity at each point
the right-hand side below is known as `continuous_at f x` -/
example (f : X → Y) : continuous f ↔ ∀ x, tendsto f (𝓝 x) (𝓝 (f x)) := continuous_iff_continuous_at
/- The topological structure also brings operations on sets.
To each `A : set X`, we can associate `closure A`, `interior A` and `frontier A`.
We'll focus on `closure A`. It is defined as the intersection of closed sets containing `A`
but we can characterize it in terms of neighborhoods. The most concrete version is
`mem_closure_iff_nhds : a ∈ closure A ↔ ∀ B ∈ 𝓝 a, (B ∩ A).nonempty`
We'll pratice by reproving the slightly more abstract `mem_closure_iff_comap_ne_bot`.
First let's review sets and subtypes. Fix a type `X` and recall
that `A : set X` is not a type a priori, but Lean coerces automatically when needed to the
type `↥A` whose terms are build of a term `x : X` and a proof of `x ∈ A`.
In the other direction, inhabitants of `↥A` can be coerced to `X` automatically.
This inclusion coercion map is called `coe : A → X` and `coe a` is also denoted by `↑a`.
Now assume `X` is a topological space, and let's understand the closure of A in terms
of `coe` and the neighborhood filter.
In the next exercise, you can use `simp_rw` instead of `rw` to rewrite inside a quantifier
-/
#check nonempty_inter_iff_exists_right
example {A : set X} {x : X} :
x ∈ closure A ↔ (comap (coe : A → X) (𝓝 x)).ne_bot :=
begin
sorry
end
/-
In elementary contexts, the main property of `closure A` is that a converging sequence
`u : ℕ → X` such that `∀ n, u n ∈ A` has its limit in `closure A`.
Note we don't need all the full sequence to be in
`A`, it's enough to ask it for `n` large enough, ie. `∀ᶠ n in at_top, u n ∈ A`.
Also there is no reason to use sequences only, we can use any map and any source filter.
We hence have the important
`mem_closure_of_tendsto` : ∀ {f : β → X} {F : filter β} {a : X}
{A : set X}, F ≠ ⊥ → tendsto f F (𝓝 a) → (∀ᶠ x in F, f x ∈ A) → a ∈ closure A
If `A` is known to be closed then we can replace `closure A` by `A`, this is
`is_closed.mem_of_tendsto`.
-/
/-
We need one last piece of filter technology: bases. By definition, each neighborhood of a point
`x` contains an *open* neighborhood of `x`.
Hence we can often restrict our attention to such neighborhoods.
The general definition recording such a situation is:
`has_basis` (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop :=
(mem_iff' : ∀ t, t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t)
You can now inspect three examples of how bases allow to restrict attention to certain elements
of a filter.
-/
#check @has_basis.mem_iff
#check @has_basis.tendsto_left_iff
#check @has_basis.tendsto_right_iff
-- We'll use the following bases:
#check @nhds_basis_opens'
#check @closed_nhds_basis
/--
Our main goal is now to prove the basic theorem which allows extension by continuity.
From Bourbaki's general topology book, I.8.5, Theorem 1 (taking only the non-trivial implication):
Let `X` be a topological space, `A` a dense subset of `X`, `f : A → Y` a mapping of `A` into a
regular space `Y`. If, for each `x` in `X`, `f(y)` tends to a limit in `Y` when `y` tends to `x`
while remaining in `A` then there exists a continuous extension `φ` of `f` to `X`.
The regularity assumption on `Y` ensures that each point of `Y` has a basis of *closed*
neighborhoods, this is `closed_nhds_basis`.
It also ensures that `Y` is Hausdorff so limits in `Y` are unique, this is `tendsto_nhds_unique`.
mathlib contains a refinement of the above lemma, `dense_inducing.continuous_at_extend`,
but we'll stick to Bourbaki's version here.
Remember that, given `A : set X`, `↥A` is the subtype associated to `A`, and Lean will automatically
insert that funny up arrow when needed. And the (inclusion) coercion map is `coe : A → X`.
The assumption "tends to `x` while remaining in `A`" corresponds to the pull-back filter
`comap coe (𝓝 x)`.
Let's prove first an auxilliary lemma, extracted to simplify the context
(in particular we don't need Y to be a topological space here).
-/
lemma aux {X Y A : Type*} [topological_space X] {c : A → X} {f : A → Y} {x : X} {F : filter Y}
(h : tendsto f (comap c (𝓝 x)) F) {V' : set Y} (V'_in : V' ∈ F) :
∃ V ∈ 𝓝 x, is_open V ∧ c ⁻¹' V ⊆ f ⁻¹' V' :=
begin
sorry
end
/--
Let's now turn to the main proof of the extension by continuity theorem.
When Lean needs a topology on `↥A` it will use the induced topology, thanks to the instance
`subtype.topological_space`.
This all happens automatically. The only relevant lemma is
`nhds_induced coe : ∀ a : ↥A, 𝓝 a = comap coe (𝓝 ↑a)`
(this is actually a general lemma about induced topologies).
The proof outline is:
The main assumption and the axiom of choice give a function `φ` such that
`∀ x, tendsto f (comap coe $ 𝓝 x) (𝓝 (φ x))`
(because `Y` is Hausdorff, `φ` is entirely determined, but we won't need that until we try to
prove that `φ` indeed extends `f`).
Let's first prove `φ` is continuous. Fix any `x : X`.
Since `Y` is regular, it suffices to check that for every *closed* neighborhood
`V'` of `φ x`, `φ ⁻¹' V' ∈ 𝓝 x`.
The limit assumption gives (through the auxilliary lemma above)
some `V ∈ 𝓝 x` such `is_open V ∧ coe ⁻¹' V ⊆ f ⁻¹' V'`.
Since `V ∈ 𝓝 x`, it suffices to prove `V ⊆ φ ⁻¹' V'`, ie `∀ y ∈ V, φ y ∈ V'`.
Let's fix `y` in `V`. Because `V` is *open*, it is a neighborhood of `y`.
In particular `coe ⁻¹' V ∈ comap coe (𝓝 y)` and a fortiori `f ⁻¹' V' ∈ comap coe (𝓝 y)`.
In addition `comap coe $ 𝓝 y ≠ ⊥` because `A` is dense.
Because we know `tendsto f (comap coe $ 𝓝 y) (𝓝 (φ y))` this implies
`φ y ∈ closure V'` and, since `V'` is closed, we have proved `φ y ∈ V'`.
It remains to prove that `φ` extends `f`. This is were continuity of `f` enters the discussion,
together with the fact that `Y` is Hausdorff.
-/
example [regular_space Y] {A : set X} (hA : ∀ x, x ∈ closure A)
{f : A → Y} (f_cont : continuous f)
(hf : ∀ x : X, ∃ c : Y, tendsto f (comap coe $ 𝓝 x) $ 𝓝 c) :
∃ φ : X → Y, continuous φ ∧ ∀ a : A, φ a = f a :=
begin
sorry
end
end
/-!
## Metric spaces
-/
/--
We now leave general topology and turn to metric spaces. The distance function is denoted by `dist`.
A slight difficulty here is that, as in Bourbaki, many results you may expect
to see stated for metric spaces are stated for uniform spaces, a more general notion that also
includes topological groups. In this tutorial we will avoid uniform spaces for simplicity.
We will prove that continuous functions from a compact metric space to a
metric space are uniformly continuous. mathlib has a much more general
version (about functions between uniform spaces...).
The lemma `metric.uniform_continuous_iff` allows to translate the general definition
of uniform continuity to the ε-δ definition that works for metric spaces only.
So let's fix `ε > 0` and start looking for `δ`.
We will deduce Heine-Cantor from the fact that a real value continuous function
on a nonempty compact set reaches its infimum. There are several ways to state that,
but here we recommend `is_compact.exists_forall_le`.
Let `φ : X × X → ℝ := λ p, dist (f p.1) (f p.2)` and let `K := { p : X × X | ε ≤ φ p }`.
Observe `φ` is continuous by assumption on `f` and using `continuous_dist`.
And `K` is closed using `is_closed_le` hence compact since `X` is compact.
Then we discuss two possibilities using `eq_empty_or_nonempty`.
If `K` is empty then we are clearly done (we can set `δ = 1` for instance).
So let's assume `K` is not empty, and choose `(x₀, x₁)` attaining the infimum
of `φ` on `K`. We can then set `δ = dist x₀ x₁` and check everything works.
-/
example {X : Type*} [metric_space X] [compact_space X] {Y : Type*} [metric_space Y]
{f : X → Y} (hf : continuous f) : uniform_continuous f :=
begin
sorry
end
end lftcm
|
6700dbb6d252b43b082261a0f65ee5c2dbef4c42 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/notation4.lean | e96a2ae03e79720b9c292ac6db2550b3f03c9bdf | [
"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 | 346 | lean | --
open sigma
inductive List (T : Type) : Type
| nil {} : List
| cons : T → List → List
open List
notation (name := cons2) h :: t := cons h t
notation (name := list2) `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
#check ∃ (A : Type) (x y : A), x = y
#check ∃ (x : nat), x = 0
#check Σ' (x : nat), x = 10
#check Σ (A : Type), List A
|
59dc59903f2bc19083b993b04a67a7a814e76b6b | 5756a081670ba9c1d1d3fca7bd47cb4e31beae66 | /Mathport/Syntax.lean | 1ac082a6ccbdf394570c682dc16a1e247eae77e3 | [
"Apache-2.0"
] | permissive | leanprover-community/mathport | 2c9bdc8292168febf59799efdc5451dbf0450d4a | 13051f68064f7638970d39a8fecaede68ffbf9e1 | refs/heads/master | 1,693,841,364,079 | 1,693,813,111,000 | 1,693,813,111,000 | 379,357,010 | 27 | 10 | Apache-2.0 | 1,691,309,132,000 | 1,624,384,521,000 | Lean | UTF-8 | Lean | false | false | 3,804 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Daniel Selsam
-/
import Mathport.Bridge.Config
import Mathport.Syntax.AST3
import Mathport.Syntax.Parse
import Mathport.Syntax.Translate
namespace Mathport
open Lean Lean.Elab.Command
open Syntax
def synport1 (config : Config) (path : Path) (imports : Array Name) : CommandElabM Unit := do
let pcfg := config.pathConfig
let (ast3, _) ← parseAST3 (path.toLean3 pcfg ".ast.json") false
let imports := if ast3.prelude.isNone then imports.filter (!(`init).isPrefixOf ·) else imports
-- HACK: replace the imports with the ones from .tlean until lean#811 lands
let ast3 := { ast3 with «import» := imports.map (#[.dummy ·]) }
let ⟨fmt, _⟩ ← AST3toData4 path ast3 config
IO.FS.writeFile (path.toLean4src pcfg) (fmt.pretty 100)
open Lean Lean.Elab Lean.Elab.Term Lean.Elab.Tactic
open Lean.Parser Lean.PrettyPrinter
-- #eval show CoreM Unit from do if false then
-- initSearchPath s!"{← Lean.getLibDir}:./Lib4:build"
-- let pcfg : Path.Config := { outRoot := "", packages := {} }
-- let mods := [`Mathlib.Tactic.Localized]
-- -- let s ← IO.FS.readFile "/home/mario/Documents/lean/lean/library/test.ast.json"
-- -- let s ← IO.FS.readFile "/home/mario/Documents/lean/mathport/PreData/Lean3/init/data/int/order.ast.json"
-- let s ← IO.FS.readFile "/home/mario/Documents/lean/mathport/PreData/Mathlib/tactic/localized.ast.json"
-- let json ← Json.parse s
-- let raw@⟨ast, file, level, expr, _, _⟩ ← fromJson? json (α := Parse.RawAST3)
-- let (⟨prel, imp, commands, inot, icmd⟩, _) ← raw.build false
-- let level := Parse.buildLevels level
-- let expr := Parse.buildExprs level expr
-- let commands := ast[ast[file].get!.children'[2]].get!.children'
-- let cmdCtx := { fileName := "<input>", fileMap := dummyFileMap }
-- let env ← getEnv
-- withImportModulesConst (mods.map fun n => { module := n : Import }) {} 0 $ λ env => do
-- let mut opts : Options := {}
-- -- opts := opts.setBool `trace.PrettyPrinter.parenthesize true
-- -- opts := opts.setBool `trace.PrettyPrinter.format true
-- let s := Elab.Command.mkState env {} opts
-- let mut i := 0
-- for c in commands[i:] do
-- println! "cmd[{i}]"; i := i + 1
-- -- println! (repr (← Parse.getNode c |>.run ast expr)).group ++ "\n"
-- -- println! (repr (← Parse.getCommand c |>.run ast expr).kind).group ++ "\n"
-- let res ← Parse.getCommand c |>.run ast expr
-- Elab.Command.CommandElabM.toIO (ctx := cmdCtx) (s := s) do
-- let ⟨fmt, _⟩ ← Mathport.AST3toData4 ⟨none, #[], #[res], inot, icmd⟩ pcfg
-- println! "{fmt}"
-- printTraces
-- #eval show CoreM Unit from do
-- let pcfg : Path.Config := { outRoot := "", packages := {} }
-- let mut opts : Options := {}
-- let s := Elab.Command.mkState (← getEnv) {} opts
-- let stx ← Translate.Tactic.trFunext
-- |>.run #[Spanned.dummy (AST3.Param.parse arbitrary #[Spanned.dummy (AST3.VMCall.ident `x), Spanned.dummy (AST3.VMCall.ident `y)])]
-- |>.run #[] #[] { outRoot := "", packages := {} }
-- |>.toIO { fileName := "<input>", fileMap := dummyFileMap } s
-- println! "{stx}"
-- #eval show CoreM Unit from do
-- let ⟨ast⟩ ← parseAST3 "/home/mario/Documents/lean/lean/library/init/logic.ast.json"
-- let ⟨stx, _⟩ ← match AST3toData4 ⟨ast[290:292].toArray⟩ with
-- | Except.ok e => e
-- | Except.error e => throwError "{e}"
-- -- let stx := stx[1][0]
-- println! "{stx[1]}\n\n"
-- let stx ← parenthesize Parser.Module.module.parenthesizer stx
-- println! "{stx}\n\n"
-- let fmt ← format Parser.Module.module.formatter stx
-- println! "{fmt}"
|
ec62a993956f4f22cda9e2e858fe737e178c6875 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/direct_sum/ring.lean | cca2fd311b35e6dbeac2fa4d5c66bb33b1909c03 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 22,917 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.graded_monoid
import algebra.direct_sum.basic
/-!
# Additively-graded multiplicative structures on `⨁ i, A i`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This module provides a set of heterogeneous typeclasses for defining a multiplicative structure
over `⨁ i, A i` such that `(*) : A i → A j → A (i + j)`; that is to say, `A` forms an
additively-graded ring. The typeclasses are:
* `direct_sum.gnon_unital_non_assoc_semiring A`
* `direct_sum.gsemiring A`
* `direct_sum.gring A`
* `direct_sum.gcomm_semiring A`
* `direct_sum.gcomm_ring A`
Respectively, these imbue the external direct sum `⨁ i, A i` with:
* `direct_sum.non_unital_non_assoc_semiring`, `direct_sum.non_unital_non_assoc_ring`
* `direct_sum.semiring`
* `direct_sum.ring`
* `direct_sum.comm_semiring`
* `direct_sum.comm_ring`
the base ring `A 0` with:
* `direct_sum.grade_zero.non_unital_non_assoc_semiring`,
`direct_sum.grade_zero.non_unital_non_assoc_ring`
* `direct_sum.grade_zero.semiring`
* `direct_sum.grade_zero.ring`
* `direct_sum.grade_zero.comm_semiring`
* `direct_sum.grade_zero.comm_ring`
and the `i`th grade `A i` with `A 0`-actions (`•`) defined as left-multiplication:
* `direct_sum.grade_zero.has_smul (A 0)`, `direct_sum.grade_zero.smul_with_zero (A 0)`
* `direct_sum.grade_zero.module (A 0)`
* (nothing)
* (nothing)
* (nothing)
Note that in the presence of these instances, `⨁ i, A i` itself inherits an `A 0`-action.
`direct_sum.of_zero_ring_hom : A 0 →+* ⨁ i, A i` provides `direct_sum.of A 0` as a ring
homomorphism.
`direct_sum.to_semiring` extends `direct_sum.to_add_monoid` to produce a `ring_hom`.
## Direct sums of subobjects
Additionally, this module provides helper functions to construct `gsemiring` and `gcomm_semiring`
instances for:
* `A : ι → submonoid S`:
`direct_sum.gsemiring.of_add_submonoids`, `direct_sum.gcomm_semiring.of_add_submonoids`.
* `A : ι → subgroup S`:
`direct_sum.gsemiring.of_add_subgroups`, `direct_sum.gcomm_semiring.of_add_subgroups`.
* `A : ι → submodule S`:
`direct_sum.gsemiring.of_submodules`, `direct_sum.gcomm_semiring.of_submodules`.
If `complete_lattice.independent (set.range A)`, these provide a gradation of `⨆ i, A i`, and the
mapping `⨁ i, A i →+ ⨆ i, A i` can be obtained as
`direct_sum.to_monoid (λ i, add_submonoid.inclusion $ le_supr A i)`.
## tags
graded ring, filtered ring, direct sum, add_submonoid
-/
set_option old_structure_cmd true
variables {ι : Type*} [decidable_eq ι]
namespace direct_sum
open_locale direct_sum
/-! ### Typeclasses -/
section defs
variables (A : ι → Type*)
/-- A graded version of `non_unital_non_assoc_semiring`. -/
class gnon_unital_non_assoc_semiring [has_add ι] [Π i, add_comm_monoid (A i)] extends
graded_monoid.ghas_mul A :=
(mul_zero : ∀ {i j} (a : A i), mul a (0 : A j) = 0)
(zero_mul : ∀ {i j} (b : A j), mul (0 : A i) b = 0)
(mul_add : ∀ {i j} (a : A i) (b c : A j), mul a (b + c) = mul a b + mul a c)
(add_mul : ∀ {i j} (a b : A i) (c : A j), mul (a + b) c = mul a c + mul b c)
end defs
section defs
variables (A : ι → Type*)
/-- A graded version of `semiring`. -/
class gsemiring [add_monoid ι] [Π i, add_comm_monoid (A i)] extends
gnon_unital_non_assoc_semiring A, graded_monoid.gmonoid A :=
(nat_cast : ℕ → A 0)
(nat_cast_zero : nat_cast 0 = 0)
(nat_cast_succ : ∀ n : ℕ, nat_cast (n + 1) = nat_cast n + graded_monoid.ghas_one.one)
/-- A graded version of `comm_semiring`. -/
class gcomm_semiring [add_comm_monoid ι] [Π i, add_comm_monoid (A i)] extends
gsemiring A, graded_monoid.gcomm_monoid A
/-- A graded version of `ring`. -/
class gring [add_monoid ι] [Π i, add_comm_group (A i)] extends gsemiring A :=
(int_cast : ℤ → A 0)
(int_cast_of_nat : ∀ n : ℕ, int_cast n = nat_cast n)
(int_cast_neg_succ_of_nat : ∀ n : ℕ, int_cast (-(n+1 : ℕ)) = -nat_cast (n+1 : ℕ))
/-- A graded version of `comm_ring`. -/
class gcomm_ring [add_comm_monoid ι] [Π i, add_comm_group (A i)] extends
gring A, gcomm_semiring A
end defs
lemma of_eq_of_graded_monoid_eq {A : ι → Type*} [Π (i : ι), add_comm_monoid (A i)]
{i j : ι} {a : A i} {b : A j} (h : graded_monoid.mk i a = graded_monoid.mk j b) :
direct_sum.of A i a = direct_sum.of A j b :=
dfinsupp.single_eq_of_sigma_eq h
variables (A : ι → Type*)
/-! ### Instances for `⨁ i, A i` -/
section one
variables [has_zero ι] [graded_monoid.ghas_one A] [Π i, add_comm_monoid (A i)]
instance : has_one (⨁ i, A i) :=
{ one := direct_sum.of (λ i, A i) 0 graded_monoid.ghas_one.one }
end one
section mul
variables [has_add ι] [Π i, add_comm_monoid (A i)] [gnon_unital_non_assoc_semiring A]
open add_monoid_hom (flip_apply coe_comp comp_hom_apply_apply)
/-- The piecewise multiplication from the `has_mul` instance, as a bundled homomorphism. -/
@[simps]
def gmul_hom {i j} : A i →+ A j →+ A (i + j) :=
{ to_fun := λ a,
{ to_fun := λ b, graded_monoid.ghas_mul.mul a b,
map_zero' := gnon_unital_non_assoc_semiring.mul_zero _,
map_add' := gnon_unital_non_assoc_semiring.mul_add _ },
map_zero' := add_monoid_hom.ext $ λ a, gnon_unital_non_assoc_semiring.zero_mul a,
map_add' := λ a₁ a₂, add_monoid_hom.ext $ λ b, gnon_unital_non_assoc_semiring.add_mul _ _ _}
/-- The multiplication from the `has_mul` instance, as a bundled homomorphism. -/
def mul_hom : (⨁ i, A i) →+ (⨁ i, A i) →+ ⨁ i, A i :=
direct_sum.to_add_monoid $ λ i,
add_monoid_hom.flip $ direct_sum.to_add_monoid $ λ j, add_monoid_hom.flip $
(direct_sum.of A _).comp_hom.comp $ gmul_hom A
instance : non_unital_non_assoc_semiring (⨁ i, A i) :=
{ mul := λ a b, mul_hom A a b,
zero := 0,
add := (+),
zero_mul := λ a, by simp only [add_monoid_hom.map_zero, add_monoid_hom.zero_apply],
mul_zero := λ a, by simp only [add_monoid_hom.map_zero],
left_distrib := λ a b c, by simp only [add_monoid_hom.map_add],
right_distrib := λ a b c, by simp only [add_monoid_hom.map_add, add_monoid_hom.add_apply],
.. direct_sum.add_comm_monoid _ _}
variables {A}
lemma mul_hom_of_of {i j} (a : A i) (b : A j) :
mul_hom A (of _ i a) (of _ j b) = of _ (i + j) (graded_monoid.ghas_mul.mul a b) :=
begin
unfold mul_hom,
rw [to_add_monoid_of, flip_apply, to_add_monoid_of, flip_apply, coe_comp, function.comp_app,
comp_hom_apply_apply, coe_comp, function.comp_app, gmul_hom_apply_apply],
end
lemma of_mul_of {i j} (a : A i) (b : A j) :
of _ i a * of _ j b = of _ (i + j) (graded_monoid.ghas_mul.mul a b) :=
mul_hom_of_of a b
end mul
section semiring
variables [Π i, add_comm_monoid (A i)] [add_monoid ι] [gsemiring A]
open add_monoid_hom (flip_hom coe_comp comp_hom_apply_apply flip_apply flip_hom_apply)
private lemma one_mul (x : ⨁ i, A i) : 1 * x = x :=
suffices mul_hom A 1 = add_monoid_hom.id (⨁ i, A i),
from add_monoid_hom.congr_fun this x,
begin
apply add_hom_ext, intros i xi,
unfold has_one.one,
rw mul_hom_of_of,
exact of_eq_of_graded_monoid_eq (one_mul $ graded_monoid.mk i xi),
end
private lemma mul_one (x : ⨁ i, A i) : x * 1 = x :=
suffices (mul_hom A).flip 1 = add_monoid_hom.id (⨁ i, A i),
from add_monoid_hom.congr_fun this x,
begin
apply add_hom_ext, intros i xi,
unfold has_one.one,
rw [flip_apply, mul_hom_of_of],
exact of_eq_of_graded_monoid_eq (mul_one $ graded_monoid.mk i xi),
end
private lemma mul_assoc (a b c : ⨁ i, A i) : a * b * c = a * (b * c) :=
suffices (mul_hom A).comp_hom.comp (mul_hom A) -- `λ a b c, a * b * c` as a bundled hom
= (add_monoid_hom.comp_hom flip_hom $ -- `λ a b c, a * (b * c)` as a bundled hom
(mul_hom A).flip.comp_hom.comp (mul_hom A)).flip,
from add_monoid_hom.congr_fun (add_monoid_hom.congr_fun (add_monoid_hom.congr_fun this a) b) c,
begin
ext ai ax bi bx ci cx : 6,
dsimp only [coe_comp, function.comp_app, comp_hom_apply_apply, flip_apply, flip_hom_apply],
rw [mul_hom_of_of, mul_hom_of_of, mul_hom_of_of, mul_hom_of_of],
exact of_eq_of_graded_monoid_eq (mul_assoc (graded_monoid.mk ai ax) ⟨bi, bx⟩ ⟨ci, cx⟩),
end
/-- The `semiring` structure derived from `gsemiring A`. -/
instance semiring : semiring (⨁ i, A i) :=
{ one := 1,
mul := (*),
zero := 0,
add := (+),
one_mul := one_mul A,
mul_one := mul_one A,
mul_assoc := mul_assoc A,
nat_cast := λ n, of _ _ (gsemiring.nat_cast n),
nat_cast_zero := by rw [gsemiring.nat_cast_zero, map_zero],
nat_cast_succ := λ n, by { rw [gsemiring.nat_cast_succ, map_add], refl },
..direct_sum.non_unital_non_assoc_semiring _, }
lemma of_pow {i} (a : A i) (n : ℕ) :
of _ i a ^ n = of _ (n • i) (graded_monoid.gmonoid.gnpow _ a) :=
begin
induction n with n,
{ exact of_eq_of_graded_monoid_eq (pow_zero $ graded_monoid.mk _ a).symm, },
{ rw [pow_succ, n_ih, of_mul_of],
exact of_eq_of_graded_monoid_eq (pow_succ (graded_monoid.mk _ a) n).symm, },
end
lemma of_list_dprod {α} (l : list α) (fι : α → ι) (fA : Π a, A (fι a)) :
of A _ (l.dprod fι fA) = (l.map $ λ a, of A (fι a) (fA a)).prod :=
begin
induction l,
{ simp only [list.map_nil, list.prod_nil, list.dprod_nil],
refl },
{ simp only [list.map_cons, list.prod_cons, list.dprod_cons, ←l_ih, direct_sum.of_mul_of],
refl },
end
lemma list_prod_of_fn_of_eq_dprod (n : ℕ) (fι : fin n → ι) (fA : Π a, A (fι a)) :
(list.of_fn $ λ a, of A (fι a) (fA a)).prod = of A _ ((list.fin_range n).dprod fι fA) :=
by rw [list.of_fn_eq_map, of_list_dprod]
open_locale big_operators
lemma mul_eq_dfinsupp_sum [Π (i : ι) (x : A i), decidable (x ≠ 0)] (a a' : ⨁ i, A i) :
a * a' = a.sum (λ i ai, a'.sum $ λ j aj, direct_sum.of _ _ $ graded_monoid.ghas_mul.mul ai aj) :=
begin
change mul_hom _ a a' = _,
simpa only [mul_hom, to_add_monoid, dfinsupp.lift_add_hom_apply, dfinsupp.sum_add_hom_apply,
add_monoid_hom.dfinsupp_sum_apply, flip_apply, add_monoid_hom.dfinsupp_sum_add_hom_apply],
end
/-- A heavily unfolded version of the definition of multiplication -/
lemma mul_eq_sum_support_ghas_mul
[Π (i : ι) (x : A i), decidable (x ≠ 0)] (a a' : ⨁ i, A i) :
a * a' =
∑ ij in dfinsupp.support a ×ˢ dfinsupp.support a',
direct_sum.of _ _ (graded_monoid.ghas_mul.mul (a ij.fst) (a' ij.snd)) :=
by simp only [mul_eq_dfinsupp_sum, dfinsupp.sum, finset.sum_product]
end semiring
section comm_semiring
variables [Π i, add_comm_monoid (A i)] [add_comm_monoid ι] [gcomm_semiring A]
private lemma mul_comm (a b : ⨁ i, A i) : a * b = b * a :=
suffices mul_hom A = (mul_hom A).flip,
from add_monoid_hom.congr_fun (add_monoid_hom.congr_fun this a) b,
begin
apply add_hom_ext, intros ai ax, apply add_hom_ext, intros bi bx,
rw [add_monoid_hom.flip_apply, mul_hom_of_of, mul_hom_of_of],
exact of_eq_of_graded_monoid_eq (gcomm_semiring.mul_comm ⟨ai, ax⟩ ⟨bi, bx⟩),
end
/-- The `comm_semiring` structure derived from `gcomm_semiring A`. -/
instance comm_semiring : comm_semiring (⨁ i, A i) :=
{ one := 1,
mul := (*),
zero := 0,
add := (+),
mul_comm := mul_comm A,
..direct_sum.semiring _, }
end comm_semiring
section non_unital_non_assoc_ring
variables [Π i, add_comm_group (A i)] [has_add ι] [gnon_unital_non_assoc_semiring A]
/-- The `ring` derived from `gsemiring A`. -/
instance non_assoc_ring : non_unital_non_assoc_ring (⨁ i, A i) :=
{ mul := (*),
zero := 0,
add := (+),
neg := has_neg.neg,
..(direct_sum.non_unital_non_assoc_semiring _),
..(direct_sum.add_comm_group _), }
end non_unital_non_assoc_ring
section ring
variables [Π i, add_comm_group (A i)] [add_monoid ι] [gring A]
/-- The `ring` derived from `gsemiring A`. -/
instance ring : ring (⨁ i, A i) :=
{ one := 1,
mul := (*),
zero := 0,
add := (+),
neg := has_neg.neg,
int_cast := λ z, of _ _ (gring.int_cast z),
int_cast_of_nat := λ z, congr_arg _ $ gring.int_cast_of_nat _,
int_cast_neg_succ_of_nat := λ z,
(congr_arg _ $ gring.int_cast_neg_succ_of_nat _).trans (map_neg _ _),
..(direct_sum.semiring _),
..(direct_sum.add_comm_group _), }
end ring
section comm_ring
variables [Π i, add_comm_group (A i)] [add_comm_monoid ι] [gcomm_ring A]
/-- The `comm_ring` derived from `gcomm_semiring A`. -/
instance comm_ring : comm_ring (⨁ i, A i) :=
{ one := 1,
mul := (*),
zero := 0,
add := (+),
neg := has_neg.neg,
..(direct_sum.ring _),
..(direct_sum.comm_semiring _), }
end comm_ring
/-! ### Instances for `A 0`
The various `g*` instances are enough to promote the `add_comm_monoid (A 0)` structure to various
types of multiplicative structure.
-/
section grade_zero
section one
variables [has_zero ι] [graded_monoid.ghas_one A] [Π i, add_comm_monoid (A i)]
@[simp] lemma of_zero_one : of _ 0 (1 : A 0) = 1 := rfl
end one
section mul
variables [add_zero_class ι] [Π i, add_comm_monoid (A i)] [gnon_unital_non_assoc_semiring A]
@[simp] lemma of_zero_smul {i} (a : A 0) (b : A i) : of _ _ (a • b) = of _ _ a * of _ _ b :=
(of_eq_of_graded_monoid_eq (graded_monoid.mk_zero_smul a b)).trans (of_mul_of _ _).symm
@[simp] lemma of_zero_mul (a b : A 0) : of _ 0 (a * b) = of _ 0 a * of _ 0 b:=
of_zero_smul A a b
instance grade_zero.non_unital_non_assoc_semiring : non_unital_non_assoc_semiring (A 0) :=
function.injective.non_unital_non_assoc_semiring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of A 0).map_add (of_zero_mul A) (λ x n, dfinsupp.single_smul n x)
instance grade_zero.smul_with_zero (i : ι) : smul_with_zero (A 0) (A i) :=
begin
letI := smul_with_zero.comp_hom (⨁ i, A i) (of A 0).to_zero_hom,
refine dfinsupp.single_injective.smul_with_zero (of A i).to_zero_hom (of_zero_smul A),
end
end mul
section semiring
variables [Π i, add_comm_monoid (A i)] [add_monoid ι] [gsemiring A]
@[simp] lemma of_zero_pow (a : A 0) : ∀ n : ℕ, of _ 0 (a ^ n) = of _ 0 a ^ n
| 0 := by rw [pow_zero, pow_zero, direct_sum.of_zero_one]
| (n + 1) := by rw [pow_succ, pow_succ, of_zero_mul, of_zero_pow]
instance : has_nat_cast (A 0) := ⟨gsemiring.nat_cast⟩
@[simp] lemma of_nat_cast (n : ℕ) : of A 0 n = n :=
rfl
/-- The `semiring` structure derived from `gsemiring A`. -/
instance grade_zero.semiring : semiring (A 0) :=
function.injective.semiring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of_zero_one A) (of A 0).map_add (of_zero_mul A)
(of A 0).map_nsmul (λ x n, of_zero_pow _ _ _) (of_nat_cast A)
/-- `of A 0` is a `ring_hom`, using the `direct_sum.grade_zero.semiring` structure. -/
def of_zero_ring_hom : A 0 →+* (⨁ i, A i) :=
{ map_one' := of_zero_one A, map_mul' := of_zero_mul A, ..(of _ 0) }
/-- Each grade `A i` derives a `A 0`-module structure from `gsemiring A`. Note that this results
in an overall `module (A 0) (⨁ i, A i)` structure via `direct_sum.module`.
-/
instance grade_zero.module {i} : module (A 0) (A i) :=
begin
letI := module.comp_hom (⨁ i, A i) (of_zero_ring_hom A),
exact dfinsupp.single_injective.module (A 0) (of A i) (λ a, of_zero_smul A a),
end
end semiring
section comm_semiring
variables [Π i, add_comm_monoid (A i)] [add_comm_monoid ι] [gcomm_semiring A]
/-- The `comm_semiring` structure derived from `gcomm_semiring A`. -/
instance grade_zero.comm_semiring : comm_semiring (A 0) :=
function.injective.comm_semiring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of_zero_one A) (of A 0).map_add (of_zero_mul A)
(λ x n, dfinsupp.single_smul n x) (λ x n, of_zero_pow _ _ _) (of_nat_cast A)
end comm_semiring
section ring
variables [Π i, add_comm_group (A i)] [add_zero_class ι] [gnon_unital_non_assoc_semiring A]
/-- The `non_unital_non_assoc_ring` derived from `gnon_unital_non_assoc_semiring A`. -/
instance grade_zero.non_unital_non_assoc_ring : non_unital_non_assoc_ring (A 0) :=
function.injective.non_unital_non_assoc_ring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of A 0).map_add (of_zero_mul A)
(of A 0).map_neg (of A 0).map_sub
(λ x n, begin
letI : Π i, distrib_mul_action ℕ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end)
(λ x n, begin
letI : Π i, distrib_mul_action ℤ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end)
end ring
section ring
variables [Π i, add_comm_group (A i)] [add_monoid ι] [gring A]
instance : has_int_cast (A 0) := ⟨gring.int_cast⟩
@[simp] lemma of_int_cast (n : ℤ) : of A 0 n = n :=
rfl
/-- The `ring` derived from `gsemiring A`. -/
instance grade_zero.ring : ring (A 0) :=
function.injective.ring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of_zero_one A) (of A 0).map_add (of_zero_mul A)
(of A 0).map_neg (of A 0).map_sub
(λ x n, begin
letI : Π i, distrib_mul_action ℕ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end)
(λ x n, begin
letI : Π i, distrib_mul_action ℤ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end) (λ x n, of_zero_pow _ _ _)
(of_nat_cast A) (of_int_cast A)
end ring
section comm_ring
variables [Π i, add_comm_group (A i)] [add_comm_monoid ι] [gcomm_ring A]
/-- The `comm_ring` derived from `gcomm_semiring A`. -/
instance grade_zero.comm_ring : comm_ring (A 0) :=
function.injective.comm_ring (of A 0) dfinsupp.single_injective
(of A 0).map_zero (of_zero_one A) (of A 0).map_add (of_zero_mul A)
(of A 0).map_neg (of A 0).map_sub
(λ x n, begin
letI : Π i, distrib_mul_action ℕ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end)
(λ x n, begin
letI : Π i, distrib_mul_action ℤ (A i) := λ i, infer_instance,
exact dfinsupp.single_smul n x
end) (λ x n, of_zero_pow _ _ _)
(of_nat_cast A) (of_int_cast A)
end comm_ring
end grade_zero
section to_semiring
variables {R : Type*} [Π i, add_comm_monoid (A i)] [add_monoid ι] [gsemiring A] [semiring R]
variables {A}
/-- If two ring homomorphisms from `⨁ i, A i` are equal on each `of A i y`,
then they are equal.
See note [partially-applied ext lemmas]. -/
@[ext]
lemma ring_hom_ext' ⦃F G : (⨁ i, A i) →+* R⦄
(h : ∀ i, (↑F : _ →+ R).comp (of A i) = (↑G : _ →+ R).comp (of A i)) : F = G :=
ring_hom.coe_add_monoid_hom_injective $ direct_sum.add_hom_ext' h
/-- Two `ring_hom`s out of a direct sum are equal if they agree on the generators. -/
lemma ring_hom_ext ⦃f g : (⨁ i, A i) →+* R⦄ (h : ∀ i x, f (of A i x) = g (of A i x)) :
f = g :=
ring_hom_ext' $ λ i, add_monoid_hom.ext $ h i
/-- A family of `add_monoid_hom`s preserving `direct_sum.ghas_one.one` and `direct_sum.ghas_mul.mul`
describes a `ring_hom`s on `⨁ i, A i`. This is a stronger version of `direct_sum.to_monoid`.
Of particular interest is the case when `A i` are bundled subojects, `f` is the family of
coercions such as `add_submonoid.subtype (A i)`, and the `[gsemiring A]` structure originates from
`direct_sum.gsemiring.of_add_submonoids`, in which case the proofs about `ghas_one` and `ghas_mul`
can be discharged by `rfl`. -/
@[simps]
def to_semiring
(f : Π i, A i →+ R) (hone : f _ (graded_monoid.ghas_one.one) = 1)
(hmul : ∀ {i j} (ai : A i) (aj : A j), f _ (graded_monoid.ghas_mul.mul ai aj) = f _ ai * f _ aj) :
(⨁ i, A i) →+* R :=
{ to_fun := to_add_monoid f,
map_one' := begin
change (to_add_monoid f) (of _ 0 _) = 1,
rw to_add_monoid_of,
exact hone
end,
map_mul' := begin
rw (to_add_monoid f).map_mul_iff,
ext xi xv yi yv : 4,
show to_add_monoid f (of A xi xv * of A yi yv) =
to_add_monoid f (of A xi xv) * to_add_monoid f (of A yi yv),
rw [of_mul_of, to_add_monoid_of, to_add_monoid_of, to_add_monoid_of],
exact hmul _ _,
end,
.. to_add_monoid f}
@[simp] lemma to_semiring_of (f : Π i, A i →+ R) (hone hmul) (i : ι) (x : A i) :
to_semiring f hone hmul (of _ i x) = f _ x :=
to_add_monoid_of f i x
@[simp] lemma to_semiring_coe_add_monoid_hom (f : Π i, A i →+ R) (hone hmul):
(to_semiring f hone hmul : (⨁ i, A i) →+ R) = to_add_monoid f := rfl
/-- Families of `add_monoid_hom`s preserving `direct_sum.ghas_one.one` and `direct_sum.ghas_mul.mul`
are isomorphic to `ring_hom`s on `⨁ i, A i`. This is a stronger version of `dfinsupp.lift_add_hom`.
-/
@[simps]
def lift_ring_hom :
{f : Π {i}, A i →+ R //
f (graded_monoid.ghas_one.one) = 1 ∧
∀ {i j} (ai : A i) (aj : A j), f (graded_monoid.ghas_mul.mul ai aj) = f ai * f aj} ≃
((⨁ i, A i) →+* R) :=
{ to_fun := λ f, to_semiring (λ _, f.1) f.2.1 (λ _ _, f.2.2),
inv_fun := λ F,
⟨λ i, (F : (⨁ i, A i) →+ R).comp (of _ i), begin
simp only [add_monoid_hom.comp_apply, ring_hom.coe_add_monoid_hom],
rw ←F.map_one,
refl
end, λ i j ai aj, begin
simp only [add_monoid_hom.comp_apply, ring_hom.coe_add_monoid_hom],
rw [←F.map_mul, of_mul_of],
end⟩,
left_inv := λ f, begin
ext xi xv,
exact to_add_monoid_of (λ _, f.1) xi xv,
end,
right_inv := λ F, begin
apply ring_hom.coe_add_monoid_hom_injective,
ext xi xv,
simp only [ring_hom.coe_add_monoid_hom_mk,
direct_sum.to_add_monoid_of,
add_monoid_hom.mk_coe,
add_monoid_hom.comp_apply, to_semiring_coe_add_monoid_hom],
end}
end to_semiring
end direct_sum
/-! ### Concrete instances -/
section uniform
variables (ι)
/-- A direct sum of copies of a `semiring` inherits the multiplication structure. -/
instance non_unital_non_assoc_semiring.direct_sum_gnon_unital_non_assoc_semiring
{R : Type*} [add_monoid ι] [non_unital_non_assoc_semiring R] :
direct_sum.gnon_unital_non_assoc_semiring (λ i : ι, R) :=
{ mul_zero := λ i j, mul_zero,
zero_mul := λ i j, zero_mul,
mul_add := λ i j, mul_add,
add_mul := λ i j, add_mul,
..has_mul.ghas_mul ι }
/-- A direct sum of copies of a `semiring` inherits the multiplication structure. -/
instance semiring.direct_sum_gsemiring {R : Type*} [add_monoid ι] [semiring R] :
direct_sum.gsemiring (λ i : ι, R) :=
{ nat_cast := λ n, n,
nat_cast_zero := nat.cast_zero,
nat_cast_succ := nat.cast_succ,
..non_unital_non_assoc_semiring.direct_sum_gnon_unital_non_assoc_semiring ι,
..monoid.gmonoid ι }
open_locale direct_sum
-- To check `has_mul.ghas_mul_mul` matches
example {R : Type*} [add_monoid ι] [semiring R] (i j : ι) (a b : R) :
(direct_sum.of _ i a * direct_sum.of _ j b : ⨁ i, R) = direct_sum.of _ (i + j) (by exact a * b) :=
by rw [direct_sum.of_mul_of, has_mul.ghas_mul_mul]
/-- A direct sum of copies of a `comm_semiring` inherits the commutative multiplication structure.
-/
instance comm_semiring.direct_sum_gcomm_semiring {R : Type*} [add_comm_monoid ι] [comm_semiring R] :
direct_sum.gcomm_semiring (λ i : ι, R) :=
{ ..comm_monoid.gcomm_monoid ι, ..semiring.direct_sum_gsemiring ι }
end uniform
|
41120979118fa369c42d4ce5a6c1a003503d11f4 | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/dedekind_domain/ideal.lean | 9adff7d3d09cfff60baf189d276b03533b7f8f71 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 63,859 | lean | /-
Copyright (c) 2020 Kenji Nakagawa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio
-/
import algebra.algebra.subalgebra.pointwise
import algebraic_geometry.prime_spectrum.maximal
import algebraic_geometry.prime_spectrum.noetherian
import order.hom.basic
import ring_theory.dedekind_domain.basic
import ring_theory.fractional_ideal
import ring_theory.principal_ideal_domain
import ring_theory.chain_of_divisors
/-!
# Dedekind domains and ideals
In this file, we show a ring is a Dedekind domain iff all fractional ideals are invertible.
Then we prove some results on the unique factorization monoid structure of the ideals.
## Main definitions
- `is_dedekind_domain_inv` alternatively defines a Dedekind domain as an integral domain where
every nonzero fractional ideal is invertible.
- `is_dedekind_domain_inv_iff` shows that this does note depend on the choice of field of
fractions.
- `is_dedekind_domain.height_one_spectrum` defines the type of nonzero prime ideals of `R`.
## Main results:
- `is_dedekind_domain_iff_is_dedekind_domain_inv`
- `ideal.unique_factorization_monoid`
## Implementation notes
The definitions that involve a field of fractions choose a canonical field of fractions,
but are independent of that choice. The `..._iff` lemmas express this independence.
Often, definitions assume that Dedekind domains are not fields. We found it more practical
to add a `(h : ¬ is_field A)` assumption whenever this is explicitly needed.
## References
* [D. Marcus, *Number Fields*][marcus1977number]
* [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic]
* [J. Neukirch, *Algebraic Number Theory*][Neukirch1992]
## Tags
dedekind domain, dedekind ring
-/
variables (R A K : Type*) [comm_ring R] [comm_ring A] [field K]
open_locale non_zero_divisors polynomial
variables [is_domain A]
section inverse
namespace fractional_ideal
variables {R₁ : Type*} [comm_ring R₁] [is_domain R₁] [algebra R₁ K] [is_fraction_ring R₁ K]
variables {I J : fractional_ideal R₁⁰ K}
noncomputable instance : has_inv (fractional_ideal R₁⁰ K) := ⟨λ I, 1 / I⟩
lemma inv_eq : I⁻¹ = 1 / I := rfl
lemma inv_zero' : (0 : fractional_ideal R₁⁰ K)⁻¹ = 0 := div_zero
lemma inv_nonzero {J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
J⁻¹ = ⟨(1 : fractional_ideal R₁⁰ K) / J, fractional_div_of_nonzero h⟩ := div_nonzero _
lemma coe_inv_of_nonzero {J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
(↑J⁻¹ : submodule R₁ K) = is_localization.coe_submodule K ⊤ / J :=
by { rwa inv_nonzero _, refl, assumption }
variables {K}
lemma mem_inv_iff (hI : I ≠ 0) {x : K} : x ∈ I⁻¹ ↔ ∀ y ∈ I, x * y ∈ (1 : fractional_ideal R₁⁰ K) :=
mem_div_iff_of_nonzero hI
lemma inv_anti_mono (hI : I ≠ 0) (hJ : J ≠ 0) (hIJ : I ≤ J) : J⁻¹ ≤ I⁻¹ :=
λ x, by { simp only [mem_inv_iff hI, mem_inv_iff hJ], exact λ h y hy, h y (hIJ hy) }
lemma le_self_mul_inv {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) :
I ≤ I * I⁻¹ :=
le_self_mul_one_div hI
variables (K)
lemma coe_ideal_le_self_mul_inv (I : ideal R₁) : (I : fractional_ideal R₁⁰ K) ≤ I * I⁻¹ :=
le_self_mul_inv coe_ideal_le_one
/-- `I⁻¹` is the inverse of `I` if `I` has an inverse. -/
theorem right_inverse_eq (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : J = I⁻¹ :=
begin
have hI : I ≠ 0 := ne_zero_of_mul_eq_one I J h,
suffices h' : I * (1 / I) = 1,
{ exact (congr_arg units.inv $
@units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) },
apply le_antisymm,
{ apply mul_le.mpr _,
intros x hx y hy,
rw mul_comm,
exact (mem_div_iff_of_nonzero hI).mp hy x hx },
rw ← h,
apply mul_left_mono I,
apply (le_div_iff_of_nonzero hI).mpr _,
intros y hy x hx,
rw mul_comm,
exact mul_mem_mul hx hy
end
theorem mul_inv_cancel_iff {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ ∃ J, I * J = 1 :=
⟨λ h, ⟨I⁻¹, h⟩, λ ⟨J, hJ⟩, by rwa ← right_inverse_eq K I J hJ⟩
lemma mul_inv_cancel_iff_is_unit {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ is_unit I :=
(mul_inv_cancel_iff K).trans is_unit_iff_exists_inv.symm
variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K']
@[simp] lemma map_inv (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') :
(I⁻¹).map (h : K →ₐ[R₁] K') = (I.map h)⁻¹ :=
by rw [inv_eq, map_div, map_one, inv_eq]
open submodule submodule.is_principal
@[simp] lemma span_singleton_inv (x : K) : (span_singleton R₁⁰ x)⁻¹ = span_singleton _ x⁻¹ :=
one_div_span_singleton x
@[simp] lemma span_singleton_div_span_singleton (x y : K) :
span_singleton R₁⁰ x / span_singleton R₁⁰ y = span_singleton R₁⁰ (x / y) :=
by rw [div_span_singleton, mul_comm, span_singleton_mul_span_singleton, div_eq_mul_inv]
lemma span_singleton_div_self {x : K} (hx : x ≠ 0) :
span_singleton R₁⁰ x / span_singleton R₁⁰ x = 1 :=
by rw [span_singleton_div_span_singleton, div_self hx, span_singleton_one]
lemma coe_ideal_span_singleton_div_self {x : R₁} (hx : x ≠ 0) :
(ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K) / ideal.span ({x} : set R₁) = 1 :=
by rw [coe_ideal_span_singleton, span_singleton_div_self K $
(map_ne_zero_iff _ $ no_zero_smul_divisors.algebra_map_injective R₁ K).mpr hx]
lemma span_singleton_mul_inv {x : K} (hx : x ≠ 0) :
span_singleton R₁⁰ x * (span_singleton R₁⁰ x)⁻¹ = 1 :=
by rw [span_singleton_inv, span_singleton_mul_span_singleton, mul_inv_cancel hx, span_singleton_one]
lemma coe_ideal_span_singleton_mul_inv {x : R₁} (hx : x ≠ 0) :
(ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K) * (ideal.span ({x} : set R₁))⁻¹ = 1 :=
by rw [coe_ideal_span_singleton, span_singleton_mul_inv K $
(map_ne_zero_iff _ $ no_zero_smul_divisors.algebra_map_injective R₁ K).mpr hx]
lemma span_singleton_inv_mul {x : K} (hx : x ≠ 0) :
(span_singleton R₁⁰ x)⁻¹ * span_singleton R₁⁰ x = 1 :=
by rw [mul_comm, span_singleton_mul_inv K hx]
lemma coe_ideal_span_singleton_inv_mul {x : R₁} (hx : x ≠ 0) :
(ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K)⁻¹ * ideal.span ({x} : set R₁) = 1 :=
by rw [mul_comm, coe_ideal_span_singleton_mul_inv K hx]
lemma mul_generator_self_inv {R₁ : Type*} [comm_ring R₁] [algebra R₁ K] [is_localization R₁⁰ K]
(I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) :
I * span_singleton _ (generator (I : submodule R₁ K))⁻¹ = 1 :=
begin
-- Rewrite only the `I` that appears alone.
conv_lhs { congr, rw eq_span_singleton_of_principal I },
rw [span_singleton_mul_span_singleton, mul_inv_cancel, span_singleton_one],
intro generator_I_eq_zero,
apply h,
rw [eq_span_singleton_of_principal I, generator_I_eq_zero, span_singleton_zero]
end
lemma invertible_of_principal (I : fractional_ideal R₁⁰ K)
[submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) : I * I⁻¹ = 1 :=
(mul_div_self_cancel_iff).mpr
⟨span_singleton _ (generator (I : submodule R₁ K))⁻¹, mul_generator_self_inv _ I h⟩
lemma invertible_iff_generator_nonzero (I : fractional_ideal R₁⁰ K)
[submodule.is_principal (I : submodule R₁ K)] :
I * I⁻¹ = 1 ↔ generator (I : submodule R₁ K) ≠ 0 :=
begin
split,
{ intros hI hg,
apply ne_zero_of_mul_eq_one _ _ hI,
rw [eq_span_singleton_of_principal I, hg, span_singleton_zero] },
{ intro hg,
apply invertible_of_principal,
rw [eq_span_singleton_of_principal I],
intro hI,
have := mem_span_singleton_self _ (generator (I : submodule R₁ K)),
rw [hI, mem_zero_iff] at this,
contradiction }
end
lemma is_principal_inv (I : fractional_ideal R₁⁰ K)
[submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) :
submodule.is_principal (I⁻¹).1 :=
begin
rw [val_eq_coe, is_principal_iff],
use (generator (I : submodule R₁ K))⁻¹,
have hI : I * span_singleton _ ((generator (I : submodule R₁ K))⁻¹) = 1,
apply mul_generator_self_inv _ I h,
exact (right_inverse_eq _ I (span_singleton _ ((generator (I : submodule R₁ K))⁻¹)) hI).symm
end
noncomputable instance : inv_one_class (fractional_ideal R₁⁰ K) :=
{ inv_one := div_one,
..fractional_ideal.has_one,
..fractional_ideal.has_inv K }
end fractional_ideal
/--
A Dedekind domain is an integral domain such that every fractional ideal has an inverse.
This is equivalent to `is_dedekind_domain`.
In particular we provide a `fractional_ideal.comm_group_with_zero` instance,
assuming `is_dedekind_domain A`, which implies `is_dedekind_domain_inv`. For **integral** ideals,
`is_dedekind_domain`(`_inv`) implies only `ideal.cancel_comm_monoid_with_zero`.
-/
def is_dedekind_domain_inv : Prop :=
∀ I ≠ (⊥ : fractional_ideal A⁰ (fraction_ring A)), I * I⁻¹ = 1
open fractional_ideal
variables {R A K}
lemma is_dedekind_domain_inv_iff [algebra A K] [is_fraction_ring A K] :
is_dedekind_domain_inv A ↔ (∀ I ≠ (⊥ : fractional_ideal A⁰ K), I * I⁻¹ = 1) :=
begin
let h := map_equiv (fraction_ring.alg_equiv A K),
refine h.to_equiv.forall_congr (λ I, _),
rw ← h.to_equiv.apply_eq_iff_eq,
simp [is_dedekind_domain_inv, show ⇑h.to_equiv = h, from rfl],
end
lemma fractional_ideal.adjoin_integral_eq_one_of_is_unit [algebra A K] [is_fraction_ring A K]
(x : K) (hx : is_integral A x) (hI : is_unit (adjoin_integral A⁰ x hx)) :
adjoin_integral A⁰ x hx = 1 :=
begin
set I := adjoin_integral A⁰ x hx,
have mul_self : I * I = I,
{ apply coe_to_submodule_injective, simp },
convert congr_arg (* I⁻¹) mul_self;
simp only [(mul_inv_cancel_iff_is_unit K).mpr hI, mul_assoc, mul_one],
end
namespace is_dedekind_domain_inv
variables [algebra A K] [is_fraction_ring A K] (h : is_dedekind_domain_inv A)
include h
lemma mul_inv_eq_one {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : I * I⁻¹ = 1 :=
is_dedekind_domain_inv_iff.mp h I hI
lemma inv_mul_eq_one {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : I⁻¹ * I = 1 :=
(mul_comm _ _).trans (h.mul_inv_eq_one hI)
protected lemma is_unit {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : is_unit I :=
is_unit_of_mul_eq_one _ _ (h.mul_inv_eq_one hI)
lemma is_noetherian_ring : is_noetherian_ring A :=
begin
refine is_noetherian_ring_iff.mpr ⟨λ (I : ideal A), _⟩,
by_cases hI : I = ⊥,
{ rw hI, apply submodule.fg_bot },
have hI : (I : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := coe_ideal_ne_zero.mpr hI,
exact I.fg_of_is_unit (is_fraction_ring.injective A (fraction_ring A)) (h.is_unit hI)
end
lemma integrally_closed : is_integrally_closed A :=
begin
-- It suffices to show that for integral `x`,
-- `A[x]` (which is a fractional ideal) is in fact equal to `A`.
refine ⟨λ x hx, _⟩,
rw [← set.mem_range, ← algebra.mem_bot, ← subalgebra.mem_to_submodule, algebra.to_submodule_bot,
← coe_span_singleton A⁰ (1 : fraction_ring A), span_singleton_one,
← fractional_ideal.adjoin_integral_eq_one_of_is_unit x hx (h.is_unit _)],
{ exact mem_adjoin_integral_self A⁰ x hx },
{ exact λ h, one_ne_zero (eq_zero_iff.mp h 1 (subalgebra.one_mem _)) },
end
open ring
lemma dimension_le_one : dimension_le_one A :=
begin
-- We're going to show that `P` is maximal because any (maximal) ideal `M`
-- that is strictly larger would be `⊤`.
rintros P P_ne hP,
refine ideal.is_maximal_def.mpr ⟨hP.ne_top, λ M hM, _⟩,
-- We may assume `P` and `M` (as fractional ideals) are nonzero.
have P'_ne : (P : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := coe_ideal_ne_zero.mpr P_ne,
have M'_ne : (M : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 :=
coe_ideal_ne_zero.mpr (lt_of_le_of_lt bot_le hM).ne',
-- In particular, we'll show `M⁻¹ * P ≤ P`
suffices : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≤ P,
{ rw [eq_top_iff, ← coe_ideal_le_coe_ideal (fraction_ring A), coe_ideal_top],
calc (1 : fractional_ideal A⁰ (fraction_ring A)) = _ * _ * _ : _
... ≤ _ * _ : mul_right_mono (P⁻¹ * M : fractional_ideal A⁰ (fraction_ring A)) this
... = M : _,
{ rw [mul_assoc, ← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul, h.inv_mul_eq_one M'_ne] },
{ rw [← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul] },
{ apply_instance } },
-- Suppose we have `x ∈ M⁻¹ * P`, then in fact `x = algebra_map _ _ y` for some `y`.
intros x hx,
have le_one : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≤ 1,
{ rw [← h.inv_mul_eq_one M'_ne],
exact mul_left_mono _ ((coe_ideal_le_coe_ideal (fraction_ring A)).mpr hM.le) },
obtain ⟨y, hy, rfl⟩ := (mem_coe_ideal _).mp (le_one hx),
-- Since `M` is strictly greater than `P`, let `z ∈ M \ P`.
obtain ⟨z, hzM, hzp⟩ := set_like.exists_of_lt hM,
-- We have `z * y ∈ M * (M⁻¹ * P) = P`.
have zy_mem := mul_mem_mul (mem_coe_ideal_of_mem A⁰ hzM) hx,
rw [← ring_hom.map_mul, ← mul_assoc, h.mul_inv_eq_one M'_ne, one_mul] at zy_mem,
obtain ⟨zy, hzy, zy_eq⟩ := (mem_coe_ideal A⁰).mp zy_mem,
rw is_fraction_ring.injective A (fraction_ring A) zy_eq at hzy,
-- But `P` is a prime ideal, so `z ∉ P` implies `y ∈ P`, as desired.
exact mem_coe_ideal_of_mem A⁰ (or.resolve_left (hP.mem_or_mem hzy) hzp)
end
/-- Showing one side of the equivalence between the definitions
`is_dedekind_domain_inv` and `is_dedekind_domain` of Dedekind domains. -/
theorem is_dedekind_domain : is_dedekind_domain A :=
⟨h.is_noetherian_ring, h.dimension_le_one, h.integrally_closed⟩
end is_dedekind_domain_inv
variables [algebra A K] [is_fraction_ring A K]
/-- Specialization of `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` to Dedekind domains:
Let `I : ideal A` be a nonzero ideal, where `A` is a Dedekind domain that is not a field.
Then `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` states we can find a product of prime
ideals that is contained within `I`. This lemma extends that result by making the product minimal:
let `M` be a maximal ideal that contains `I`, then the product including `M` is contained within `I`
and the product excluding `M` is not contained within `I`. -/
lemma exists_multiset_prod_cons_le_and_prod_not_le [is_dedekind_domain A]
(hNF : ¬ is_field A) {I M : ideal A} (hI0 : I ≠ ⊥) (hIM : I ≤ M) [hM : M.is_maximal] :
∃ (Z : multiset (prime_spectrum A)),
(M ::ₘ (Z.map prime_spectrum.as_ideal)).prod ≤ I ∧
¬ (multiset.prod (Z.map prime_spectrum.as_ideal) ≤ I) :=
begin
-- Let `Z` be a minimal set of prime ideals such that their product is contained in `J`.
obtain ⟨Z₀, hZ₀⟩ := prime_spectrum.exists_prime_spectrum_prod_le_and_ne_bot_of_domain hNF hI0,
obtain ⟨Z, ⟨hZI, hprodZ⟩, h_eraseZ⟩ := multiset.well_founded_lt.has_min
(λ Z, (Z.map prime_spectrum.as_ideal).prod ≤ I ∧ (Z.map prime_spectrum.as_ideal).prod ≠ ⊥)
⟨Z₀, hZ₀⟩,
have hZM : multiset.prod (Z.map prime_spectrum.as_ideal) ≤ M := le_trans hZI hIM,
have hZ0 : Z ≠ 0, { rintro rfl, simpa [hM.ne_top] using hZM },
obtain ⟨_, hPZ', hPM⟩ := (hM.is_prime.multiset_prod_le (mt multiset.map_eq_zero.mp hZ0)).mp hZM,
-- Then in fact there is a `P ∈ Z` with `P ≤ M`.
obtain ⟨P, hPZ, rfl⟩ := multiset.mem_map.mp hPZ',
classical,
have := multiset.map_erase prime_spectrum.as_ideal prime_spectrum.ext P Z,
obtain ⟨hP0, hZP0⟩ : P.as_ideal ≠ ⊥ ∧ ((Z.erase P).map prime_spectrum.as_ideal).prod ≠ ⊥,
{ rwa [ne.def, ← multiset.cons_erase hPZ', multiset.prod_cons, ideal.mul_eq_bot,
not_or_distrib, ← this] at hprodZ },
-- By maximality of `P` and `M`, we have that `P ≤ M` implies `P = M`.
have hPM' := (is_dedekind_domain.dimension_le_one _ hP0 P.is_prime).eq_of_le hM.ne_top hPM,
substI hPM',
-- By minimality of `Z`, erasing `P` from `Z` is exactly what we need.
refine ⟨Z.erase P, _, _⟩,
{ convert hZI,
rw [this, multiset.cons_erase hPZ'] },
{ refine λ h, h_eraseZ (Z.erase P) ⟨h, _⟩ (multiset.erase_lt.mpr hPZ),
exact hZP0 }
end
namespace fractional_ideal
open ideal
lemma exists_not_mem_one_of_ne_bot [is_dedekind_domain A]
(hNF : ¬ is_field A) {I : ideal A} (hI0 : I ≠ ⊥) (hI1 : I ≠ ⊤) :
∃ x : K, x ∈ (I⁻¹ : fractional_ideal A⁰ K) ∧ x ∉ (1 : fractional_ideal A⁰ K) :=
begin
-- WLOG, let `I` be maximal.
suffices : ∀ {M : ideal A} (hM : M.is_maximal),
∃ x : K, x ∈ (M⁻¹ : fractional_ideal A⁰ K) ∧ x ∉ (1 : fractional_ideal A⁰ K),
{ obtain ⟨M, hM, hIM⟩ : ∃ (M : ideal A), is_maximal M ∧ I ≤ M := ideal.exists_le_maximal I hI1,
resetI,
have hM0 := (M.bot_lt_of_maximal hNF).ne',
obtain ⟨x, hxM, hx1⟩ := this hM,
refine ⟨x, inv_anti_mono _ _ ((coe_ideal_le_coe_ideal _).mpr hIM) hxM, hx1⟩;
rw coe_ideal_ne_zero; assumption },
-- Let `a` be a nonzero element of `M` and `J` the ideal generated by `a`.
intros M hM,
resetI,
obtain ⟨⟨a, haM⟩, ha0⟩ := submodule.nonzero_mem_of_bot_lt (M.bot_lt_of_maximal hNF),
replace ha0 : a ≠ 0 := subtype.coe_injective.ne ha0,
let J : ideal A := ideal.span {a},
have hJ0 : J ≠ ⊥ := mt ideal.span_singleton_eq_bot.mp ha0,
have hJM : J ≤ M := ideal.span_le.mpr (set.singleton_subset_iff.mpr haM),
have hM0 : ⊥ < M := M.bot_lt_of_maximal hNF,
-- Then we can find a product of prime (hence maximal) ideals contained in `J`,
-- such that removing element `M` from the product is not contained in `J`.
obtain ⟨Z, hle, hnle⟩ := exists_multiset_prod_cons_le_and_prod_not_le hNF hJ0 hJM,
-- Choose an element `b` of the product that is not in `J`.
obtain ⟨b, hbZ, hbJ⟩ := set_like.not_le_iff_exists.mp hnle,
have hnz_fa : algebra_map A K a ≠ 0 :=
mt ((injective_iff_map_eq_zero _).mp (is_fraction_ring.injective A K) a) ha0,
have hb0 : algebra_map A K b ≠ 0 :=
mt ((injective_iff_map_eq_zero _).mp (is_fraction_ring.injective A K) b)
(λ h, hbJ $ h.symm ▸ J.zero_mem),
-- Then `b a⁻¹ : K` is in `M⁻¹` but not in `1`.
refine ⟨algebra_map A K b * (algebra_map A K a)⁻¹, (mem_inv_iff _).mpr _, _⟩,
{ exact coe_ideal_ne_zero.mpr hM0.ne' },
{ rintro y₀ hy₀,
obtain ⟨y, h_Iy, rfl⟩ := (mem_coe_ideal _).mp hy₀,
rw [mul_comm, ← mul_assoc, ← ring_hom.map_mul],
have h_yb : y * b ∈ J,
{ apply hle,
rw multiset.prod_cons,
exact submodule.smul_mem_smul h_Iy hbZ },
rw ideal.mem_span_singleton' at h_yb,
rcases h_yb with ⟨c, hc⟩,
rw [← hc, ring_hom.map_mul, mul_assoc, mul_inv_cancel hnz_fa, mul_one],
apply coe_mem_one },
{ refine mt (mem_one_iff _).mp _,
rintros ⟨x', h₂_abs⟩,
rw [← div_eq_mul_inv, eq_div_iff_mul_eq hnz_fa, ← ring_hom.map_mul] at h₂_abs,
have := ideal.mem_span_singleton'.mpr ⟨x', is_fraction_ring.injective A K h₂_abs⟩,
contradiction },
end
lemma one_mem_inv_coe_ideal {I : ideal A} (hI : I ≠ ⊥) :
(1 : K) ∈ (I : fractional_ideal A⁰ K)⁻¹ :=
begin
rw mem_inv_iff (coe_ideal_ne_zero.mpr hI),
intros y hy,
rw one_mul,
exact coe_ideal_le_one hy,
assumption
end
lemma mul_inv_cancel_of_le_one [h : is_dedekind_domain A]
{I : ideal A} (hI0 : I ≠ ⊥) (hI : ((I * I⁻¹)⁻¹ : fractional_ideal A⁰ K) ≤ 1) :
(I * I⁻¹ : fractional_ideal A⁰ K) = 1 :=
begin
-- Handle a few trivial cases.
by_cases hI1 : I = ⊤,
{ rw [hI1, coe_ideal_top, one_mul, inv_one] },
by_cases hNF : is_field A,
{ letI := hNF.to_field, rcases hI1 (I.eq_bot_or_top.resolve_left hI0) },
-- We'll show a contradiction with `exists_not_mem_one_of_ne_bot`:
-- `J⁻¹ = (I * I⁻¹)⁻¹` cannot have an element `x ∉ 1`, so it must equal `1`.
obtain ⟨J, hJ⟩ : ∃ (J : ideal A), (J : fractional_ideal A⁰ K) = I * I⁻¹ :=
le_one_iff_exists_coe_ideal.mp mul_one_div_le_one,
by_cases hJ0 : J = ⊥,
{ subst hJ0,
refine absurd _ hI0,
rw [eq_bot_iff, ← coe_ideal_le_coe_ideal K, hJ],
exact coe_ideal_le_self_mul_inv K I,
apply_instance },
by_cases hJ1 : J = ⊤,
{ rw [← hJ, hJ1, coe_ideal_top] },
obtain ⟨x, hx, hx1⟩ : ∃ (x : K),
x ∈ (J : fractional_ideal A⁰ K)⁻¹ ∧ x ∉ (1 : fractional_ideal A⁰ K) :=
exists_not_mem_one_of_ne_bot hNF hJ0 hJ1,
contrapose! hx1 with h_abs,
rw hJ at hx,
exact hI hx,
end
/-- Nonzero integral ideals in a Dedekind domain are invertible.
We will use this to show that nonzero fractional ideals are invertible,
and finally conclude that fractional ideals in a Dedekind domain form a group with zero.
-/
lemma coe_ideal_mul_inv [h : is_dedekind_domain A] (I : ideal A) (hI0 : I ≠ ⊥) :
(I * I⁻¹ : fractional_ideal A⁰ K) = 1 :=
begin
-- We'll show `1 ≤ J⁻¹ = (I * I⁻¹)⁻¹ ≤ 1`.
apply mul_inv_cancel_of_le_one hI0,
by_cases hJ0 : (I * I⁻¹ : fractional_ideal A⁰ K) = 0,
{ rw [hJ0, inv_zero'], exact zero_le _ },
intros x hx,
-- In particular, we'll show all `x ∈ J⁻¹` are integral.
suffices : x ∈ integral_closure A K,
{ rwa [is_integrally_closed.integral_closure_eq_bot, algebra.mem_bot, set.mem_range,
← mem_one_iff] at this;
assumption },
-- For that, we'll find a subalgebra that is f.g. as a module and contains `x`.
-- `A` is a noetherian ring, so we just need to find a subalgebra between `{x}` and `I⁻¹`.
rw mem_integral_closure_iff_mem_fg,
have x_mul_mem : ∀ b ∈ (I⁻¹ : fractional_ideal A⁰ K), x * b ∈ (I⁻¹ : fractional_ideal A⁰ K),
{ intros b hb,
rw mem_inv_iff at ⊢ hx,
swap, { exact coe_ideal_ne_zero.mpr hI0 },
swap, { exact hJ0 },
simp only [mul_assoc, mul_comm b] at ⊢ hx,
intros y hy,
exact hx _ (mul_mem_mul hy hb) },
-- It turns out the subalgebra consisting of all `p(x)` for `p : A[X]` works.
refine ⟨alg_hom.range (polynomial.aeval x : A[X] →ₐ[A] K),
is_noetherian_submodule.mp (is_noetherian I⁻¹) _ (λ y hy, _),
⟨polynomial.X, polynomial.aeval_X x⟩⟩,
obtain ⟨p, rfl⟩ := (alg_hom.mem_range _).mp hy,
rw polynomial.aeval_eq_sum_range,
refine submodule.sum_mem _ (λ i hi, submodule.smul_mem _ _ _),
clear hi,
induction i with i ih,
{ rw pow_zero, exact one_mem_inv_coe_ideal hI0 },
{ show x ^ i.succ ∈ (I⁻¹ : fractional_ideal A⁰ K),
rw pow_succ, exact x_mul_mem _ ih },
end
/-- Nonzero fractional ideals in a Dedekind domain are units.
This is also available as `_root_.mul_inv_cancel`, using the
`comm_group_with_zero` instance defined below.
-/
protected theorem mul_inv_cancel [is_dedekind_domain A]
{I : fractional_ideal A⁰ K} (hne : I ≠ 0) : I * I⁻¹ = 1 :=
begin
obtain ⟨a, J, ha, hJ⟩ :
∃ (a : A) (aI : ideal A), a ≠ 0 ∧ I = span_singleton A⁰ (algebra_map _ _ a)⁻¹ * aI :=
exists_eq_span_singleton_mul I,
suffices h₂ : I * (span_singleton A⁰ (algebra_map _ _ a) * J⁻¹) = 1,
{ rw mul_inv_cancel_iff,
exact ⟨span_singleton A⁰ (algebra_map _ _ a) * J⁻¹, h₂⟩ },
subst hJ,
rw [mul_assoc, mul_left_comm (J : fractional_ideal A⁰ K), coe_ideal_mul_inv, mul_one,
span_singleton_mul_span_singleton, inv_mul_cancel, span_singleton_one],
{ exact mt ((injective_iff_map_eq_zero (algebra_map A K)).mp
(is_fraction_ring.injective A K) _) ha },
{ exact coe_ideal_ne_zero.mp (right_ne_zero_of_mul hne) }
end
lemma mul_right_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K}
(hJ : J ≠ 0) : ∀ {I I'}, I * J ≤ I' * J ↔ I ≤ I' :=
begin
intros I I',
split,
{ intros h, convert mul_right_mono J⁻¹ h;
rw [mul_assoc, fractional_ideal.mul_inv_cancel hJ, mul_one] },
{ exact λ h, mul_right_mono J h }
end
lemma mul_left_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K}
(hJ : J ≠ 0) {I I'} : J * I ≤ J * I' ↔ I ≤ I' :=
by convert mul_right_le_iff hJ using 1; simp only [mul_comm]
lemma mul_right_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K}
(hI : I ≠ 0) : strict_mono (* I) :=
strict_mono_of_le_iff_le (λ _ _, (mul_right_le_iff hI).symm)
lemma mul_left_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K}
(hI : I ≠ 0) : strict_mono ((*) I) :=
strict_mono_of_le_iff_le (λ _ _, (mul_left_le_iff hI).symm)
/--
This is also available as `_root_.div_eq_mul_inv`, using the
`comm_group_with_zero` instance defined below.
-/
protected lemma div_eq_mul_inv [is_dedekind_domain A] (I J : fractional_ideal A⁰ K) :
I / J = I * J⁻¹ :=
begin
by_cases hJ : J = 0,
{ rw [hJ, div_zero, inv_zero', mul_zero] },
refine le_antisymm ((mul_right_le_iff hJ).mp _) ((le_div_iff_mul_le hJ).mpr _),
{ rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one, mul_le],
intros x hx y hy,
rw [mem_div_iff_of_nonzero hJ] at hx,
exact hx y hy },
rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one],
exact le_refl I
end
end fractional_ideal
/-- `is_dedekind_domain` and `is_dedekind_domain_inv` are equivalent ways
to express that an integral domain is a Dedekind domain. -/
theorem is_dedekind_domain_iff_is_dedekind_domain_inv :
is_dedekind_domain A ↔ is_dedekind_domain_inv A :=
⟨λ h I hI, by exactI fractional_ideal.mul_inv_cancel hI, λ h, h.is_dedekind_domain⟩
end inverse
section is_dedekind_domain
variables {R A} [is_dedekind_domain A] [algebra A K] [is_fraction_ring A K]
open fractional_ideal
open ideal
noncomputable instance fractional_ideal.semifield :
semifield (fractional_ideal A⁰ K) :=
{ inv := λ I, I⁻¹,
inv_zero := inv_zero' _,
div := (/),
div_eq_mul_inv := fractional_ideal.div_eq_mul_inv,
mul_inv_cancel := λ I, fractional_ideal.mul_inv_cancel,
.. fractional_ideal.comm_semiring, .. coe_ideal_injective.nontrivial }
/-- Fractional ideals have cancellative multiplication in a Dedekind domain.
Although this instance is a direct consequence of the instance
`fractional_ideal.comm_group_with_zero`, we define this instance to provide
a computable alternative.
-/
instance fractional_ideal.cancel_comm_monoid_with_zero :
cancel_comm_monoid_with_zero (fractional_ideal A⁰ K) :=
{ .. fractional_ideal.comm_semiring, -- Project out the computable fields first.
.. (by apply_instance : cancel_comm_monoid_with_zero (fractional_ideal A⁰ K)) }
instance ideal.cancel_comm_monoid_with_zero :
cancel_comm_monoid_with_zero (ideal A) :=
{ .. ideal.comm_semiring,
.. function.injective.cancel_comm_monoid_with_zero (coe_ideal_hom A⁰ (fraction_ring A))
coe_ideal_injective (ring_hom.map_zero _) (ring_hom.map_one _) (ring_hom.map_mul _)
(ring_hom.map_pow _) }
instance ideal.is_domain :
is_domain (ideal A) :=
{ .. (infer_instance : is_cancel_mul_zero _), .. ideal.nontrivial }
/-- For ideals in a Dedekind domain, to divide is to contain. -/
lemma ideal.dvd_iff_le {I J : ideal A} : (I ∣ J) ↔ J ≤ I :=
⟨ideal.le_of_dvd,
λ h, begin
by_cases hI : I = ⊥,
{ have hJ : J = ⊥, { rwa [hI, ← eq_bot_iff] at h },
rw [hI, hJ] },
have hI' : (I : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := coe_ideal_ne_zero.mpr hI,
have : (I : fractional_ideal A⁰ (fraction_ring A))⁻¹ * J ≤ 1 := le_trans
(mul_left_mono (↑I)⁻¹ ((coe_ideal_le_coe_ideal _).mpr h))
(le_of_eq (inv_mul_cancel hI')),
obtain ⟨H, hH⟩ := le_one_iff_exists_coe_ideal.mp this,
use H,
refine coe_ideal_injective
(show (J : fractional_ideal A⁰ (fraction_ring A)) = ↑(I * H), from _),
rw [coe_ideal_mul, hH, ← mul_assoc, mul_inv_cancel hI', one_mul]
end⟩
lemma ideal.dvd_not_unit_iff_lt {I J : ideal A} :
dvd_not_unit I J ↔ J < I :=
⟨λ ⟨hI, H, hunit, hmul⟩, lt_of_le_of_ne (ideal.dvd_iff_le.mp ⟨H, hmul⟩)
(mt (λ h, have H = 1, from mul_left_cancel₀ hI (by rw [← hmul, h, mul_one]),
show is_unit H, from this.symm ▸ is_unit_one) hunit),
λ h, dvd_not_unit_of_dvd_of_not_dvd (ideal.dvd_iff_le.mpr (le_of_lt h))
(mt ideal.dvd_iff_le.mp (not_le_of_lt h))⟩
instance : wf_dvd_monoid (ideal A) :=
{ well_founded_dvd_not_unit :=
have well_founded ((>) : ideal A → ideal A → Prop) :=
is_noetherian_iff_well_founded.mp
(is_noetherian_ring_iff.mp is_dedekind_domain.is_noetherian_ring),
by { convert this, ext, rw ideal.dvd_not_unit_iff_lt } }
instance ideal.unique_factorization_monoid :
unique_factorization_monoid (ideal A) :=
{ irreducible_iff_prime := λ P,
⟨λ hirr, ⟨hirr.ne_zero, hirr.not_unit, λ I J, begin
have : P.is_maximal,
{ refine ⟨⟨mt ideal.is_unit_iff.mpr hirr.not_unit, _⟩⟩,
intros J hJ,
obtain ⟨J_ne, H, hunit, P_eq⟩ := ideal.dvd_not_unit_iff_lt.mpr hJ,
exact ideal.is_unit_iff.mp ((hirr.is_unit_or_is_unit P_eq).resolve_right hunit) },
rw [ideal.dvd_iff_le, ideal.dvd_iff_le, ideal.dvd_iff_le,
set_like.le_def, set_like.le_def, set_like.le_def],
contrapose!,
rintros ⟨⟨x, x_mem, x_not_mem⟩, ⟨y, y_mem, y_not_mem⟩⟩,
exact ⟨x * y, ideal.mul_mem_mul x_mem y_mem,
mt this.is_prime.mem_or_mem (not_or x_not_mem y_not_mem)⟩,
end⟩,
prime.irreducible⟩,
.. ideal.wf_dvd_monoid }
instance ideal.normalization_monoid : normalization_monoid (ideal A) :=
normalization_monoid_of_unique_units
@[simp] lemma ideal.dvd_span_singleton {I : ideal A} {x : A} :
I ∣ ideal.span {x} ↔ x ∈ I :=
ideal.dvd_iff_le.trans (ideal.span_le.trans set.singleton_subset_iff)
lemma ideal.is_prime_of_prime {P : ideal A} (h : prime P) : is_prime P :=
begin
refine ⟨_, λ x y hxy, _⟩,
{ unfreezingI { rintro rfl },
rw ← ideal.one_eq_top at h,
exact h.not_unit is_unit_one },
{ simp only [← ideal.dvd_span_singleton, ← ideal.span_singleton_mul_span_singleton] at ⊢ hxy,
exact h.dvd_or_dvd hxy }
end
theorem ideal.prime_of_is_prime {P : ideal A} (hP : P ≠ ⊥) (h : is_prime P) : prime P :=
begin
refine ⟨hP, mt ideal.is_unit_iff.mp h.ne_top, λ I J hIJ, _⟩,
simpa only [ideal.dvd_iff_le] using (h.mul_le.mp (ideal.le_of_dvd hIJ)),
end
/-- In a Dedekind domain, the (nonzero) prime elements of the monoid with zero `ideal A`
are exactly the prime ideals. -/
theorem ideal.prime_iff_is_prime {P : ideal A} (hP : P ≠ ⊥) :
prime P ↔ is_prime P :=
⟨ideal.is_prime_of_prime, ideal.prime_of_is_prime hP⟩
/-- In a Dedekind domain, the the prime ideals are the zero ideal together with the prime elements
of the monoid with zero `ideal A`. -/
theorem ideal.is_prime_iff_bot_or_prime {P : ideal A} :
is_prime P ↔ P = ⊥ ∨ prime P :=
⟨λ hp, (eq_or_ne P ⊥).imp_right $ λ hp0, (ideal.prime_of_is_prime hp0 hp),
λ hp, hp.elim (λ h, h.symm ▸ ideal.bot_prime) ideal.is_prime_of_prime⟩
lemma ideal.strict_anti_pow (I : ideal A) (hI0 : I ≠ ⊥) (hI1 : I ≠ ⊤) :
strict_anti ((^) I : ℕ → ideal A) :=
strict_anti_nat_of_succ_lt $ λ e, ideal.dvd_not_unit_iff_lt.mp
⟨pow_ne_zero _ hI0, I, mt is_unit_iff.mp hI1, pow_succ' I e⟩
lemma ideal.pow_lt_self (I : ideal A) (hI0 : I ≠ ⊥) (hI1 : I ≠ ⊤) (e : ℕ) (he : 2 ≤ e) : I^e < I :=
by convert I.strict_anti_pow hI0 hI1 he; rw pow_one
lemma ideal.exists_mem_pow_not_mem_pow_succ (I : ideal A) (hI0 : I ≠ ⊥) (hI1 : I ≠ ⊤) (e : ℕ) :
∃ x ∈ I^e, x ∉ I^(e+1) :=
set_like.exists_of_lt (I.strict_anti_pow hI0 hI1 e.lt_succ_self)
open unique_factorization_monoid
lemma ideal.eq_prime_pow_of_succ_lt_of_le {P I : ideal A} [P_prime : P.is_prime] (hP : P ≠ ⊥)
{i : ℕ} (hlt : P ^ (i + 1) < I) (hle : I ≤ P ^ i) :
I = P ^ i :=
begin
letI := classical.dec_eq (ideal A),
refine le_antisymm hle _,
have P_prime' := ideal.prime_of_is_prime hP P_prime,
have : I ≠ ⊥ := (lt_of_le_of_lt bot_le hlt).ne',
have := pow_ne_zero i hP,
have := pow_ne_zero (i + 1) hP,
rw [← ideal.dvd_not_unit_iff_lt, dvd_not_unit_iff_normalized_factors_lt_normalized_factors,
normalized_factors_pow, normalized_factors_irreducible P_prime'.irreducible,
multiset.nsmul_singleton, multiset.lt_repeat_succ]
at hlt,
rw [← ideal.dvd_iff_le, dvd_iff_normalized_factors_le_normalized_factors, normalized_factors_pow,
normalized_factors_irreducible P_prime'.irreducible, multiset.nsmul_singleton],
all_goals { assumption }
end
lemma ideal.pow_succ_lt_pow {P : ideal A} [P_prime : P.is_prime] (hP : P ≠ ⊥)
(i : ℕ) :
P ^ (i + 1) < P ^ i :=
lt_of_le_of_ne (ideal.pow_le_pow (nat.le_succ _))
(mt (pow_eq_pow_iff hP (mt ideal.is_unit_iff.mp P_prime.ne_top)).mp i.succ_ne_self)
lemma associates.le_singleton_iff (x : A) (n : ℕ) (I : ideal A) :
associates.mk I^n ≤ associates.mk (ideal.span {x}) ↔ x ∈ I^n :=
begin
rw [← associates.dvd_eq_le, ← associates.mk_pow, associates.mk_dvd_mk, ideal.dvd_span_singleton],
end
open fractional_ideal
variables {A K}
/-- Strengthening of `is_localization.exist_integer_multiples`:
Let `J ≠ ⊤` be an ideal in a Dedekind domain `A`, and `f ≠ 0` a finite collection
of elements of `K = Frac(A)`, then we can multiply the elements of `f` by some `a : K`
to find a collection of elements of `A` that is not completely contained in `J`. -/
lemma ideal.exist_integer_multiples_not_mem
{J : ideal A} (hJ : J ≠ ⊤) {ι : Type*} (s : finset ι) (f : ι → K)
{j} (hjs : j ∈ s) (hjf : f j ≠ 0) :
∃ a : K, (∀ i ∈ s, is_localization.is_integer A (a * f i)) ∧
∃ i ∈ s, (a * f i) ∉ (J : fractional_ideal A⁰ K) :=
begin
-- Consider the fractional ideal `I` spanned by the `f`s.
let I : fractional_ideal A⁰ K := span_finset A s f,
have hI0 : I ≠ 0 := span_finset_ne_zero.mpr ⟨j, hjs, hjf⟩,
-- We claim the multiplier `a` we're looking for is in `I⁻¹ \ (J / I)`.
suffices : ↑J / I < I⁻¹,
{ obtain ⟨_, a, hI, hpI⟩ := set_like.lt_iff_le_and_exists.mp this,
rw mem_inv_iff hI0 at hI,
refine ⟨a, λ i hi, _, _⟩,
-- By definition, `a ∈ I⁻¹` multiplies elements of `I` into elements of `1`,
-- in other words, `a * f i` is an integer.
{ exact (mem_one_iff _).mp (hI (f i)
(submodule.subset_span (set.mem_image_of_mem f hi))) },
{ contrapose! hpI,
-- And if all `a`-multiples of `I` are an element of `J`,
-- then `a` is actually an element of `J / I`, contradiction.
refine (mem_div_iff_of_nonzero hI0).mpr (λ y hy, submodule.span_induction hy _ _ _ _),
{ rintros _ ⟨i, hi, rfl⟩, exact hpI i hi },
{ rw mul_zero, exact submodule.zero_mem _ },
{ intros x y hx hy, rw mul_add, exact submodule.add_mem _ hx hy },
{ intros b x hx, rw mul_smul_comm, exact submodule.smul_mem _ b hx } } },
-- To show the inclusion of `J / I` into `I⁻¹ = 1 / I`, note that `J < I`.
calc ↑J / I = ↑J * I⁻¹ : div_eq_mul_inv ↑J I
... < 1 * I⁻¹ : mul_right_strict_mono (inv_ne_zero hI0) _
... = I⁻¹ : one_mul _,
{ rw [← coe_ideal_top],
-- And multiplying by `I⁻¹` is indeed strictly monotone.
exact strict_mono_of_le_iff_le (λ _ _, (coe_ideal_le_coe_ideal K).symm)
(lt_top_iff_ne_top.mpr hJ) },
end
section gcd
namespace ideal
/-! ### GCD and LCM of ideals in a Dedekind domain
We show that the gcd of two ideals in a Dedekind domain is just their supremum,
and the lcm is their infimum, and use this to instantiate `normalized_gcd_monoid (ideal A)`.
-/
@[simp] lemma sup_mul_inf (I J : ideal A) : (I ⊔ J) * (I ⊓ J) = I * J :=
begin
letI := classical.dec_eq (ideal A),
letI := classical.dec_eq (associates (ideal A)),
letI := unique_factorization_monoid.to_normalized_gcd_monoid (ideal A),
have hgcd : gcd I J = I ⊔ J,
{ rw [gcd_eq_normalize _ _, normalize_eq],
{ rw [dvd_iff_le, sup_le_iff, ← dvd_iff_le, ← dvd_iff_le],
exact ⟨gcd_dvd_left _ _, gcd_dvd_right _ _⟩ },
{ rw [dvd_gcd_iff, dvd_iff_le, dvd_iff_le],
simp } },
have hlcm : lcm I J = I ⊓ J,
{ rw [lcm_eq_normalize _ _, normalize_eq],
{ rw [lcm_dvd_iff, dvd_iff_le, dvd_iff_le],
simp },
{ rw [dvd_iff_le, le_inf_iff, ← dvd_iff_le, ← dvd_iff_le],
exact ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩ } },
rw [← hgcd, ← hlcm, associated_iff_eq.mp (gcd_mul_lcm _ _)],
apply_instance
end
/-- Ideals in a Dedekind domain have gcd and lcm operators that (trivially) are compatible with
the normalization operator. -/
instance : normalized_gcd_monoid (ideal A) :=
{ gcd := (⊔),
gcd_dvd_left := λ _ _, by simpa only [dvd_iff_le] using le_sup_left,
gcd_dvd_right := λ _ _, by simpa only [dvd_iff_le] using le_sup_right,
dvd_gcd := λ _ _ _, by simpa only [dvd_iff_le] using sup_le,
lcm := (⊓),
lcm_zero_left := λ _, by simp only [zero_eq_bot, bot_inf_eq],
lcm_zero_right := λ _, by simp only [zero_eq_bot, inf_bot_eq],
gcd_mul_lcm := λ _ _, by rw [associated_iff_eq, sup_mul_inf],
normalize_gcd := λ _ _, normalize_eq _,
normalize_lcm := λ _ _, normalize_eq _,
.. ideal.normalization_monoid }
-- In fact, any lawful gcd and lcm would equal sup and inf respectively.
@[simp] lemma gcd_eq_sup (I J : ideal A) : gcd I J = I ⊔ J := rfl
@[simp]
lemma lcm_eq_inf (I J : ideal A) : lcm I J = I ⊓ J := rfl
lemma inf_eq_mul_of_coprime {I J : ideal A} (coprime : I ⊔ J = ⊤) :
I ⊓ J = I * J :=
by rw [← associated_iff_eq.mp (gcd_mul_lcm I J), lcm_eq_inf I J, gcd_eq_sup, coprime, top_mul]
end ideal
end gcd
end is_dedekind_domain
section is_dedekind_domain
variables {T : Type*} [comm_ring T] [is_domain T] [is_dedekind_domain T] {I J : ideal T}
open_locale classical
open multiset unique_factorization_monoid ideal
lemma prod_normalized_factors_eq_self (hI : I ≠ ⊥) : (normalized_factors I).prod = I :=
associated_iff_eq.1 (normalized_factors_prod hI)
lemma count_le_of_ideal_ge {I J : ideal T} (h : I ≤ J) (hI : I ≠ ⊥) (K : ideal T) :
count K (normalized_factors J) ≤ count K (normalized_factors I) :=
le_iff_count.1 ((dvd_iff_normalized_factors_le_normalized_factors (ne_bot_of_le_ne_bot hI h) hI).1
(dvd_iff_le.2 h)) _
lemma sup_eq_prod_inf_factors (hI : I ≠ ⊥) (hJ : J ≠ ⊥) :
I ⊔ J = (normalized_factors I ∩ normalized_factors J).prod :=
begin
have H : normalized_factors (normalized_factors I ∩ normalized_factors J).prod =
normalized_factors I ∩ normalized_factors J,
{ apply normalized_factors_prod_of_prime,
intros p hp,
rw mem_inter at hp,
exact prime_of_normalized_factor p hp.left },
have := (multiset.prod_ne_zero_of_prime (normalized_factors I ∩ normalized_factors J)
(λ _ h, prime_of_normalized_factor _ (multiset.mem_inter.1 h).1)),
apply le_antisymm,
{ rw [sup_le_iff, ← dvd_iff_le, ← dvd_iff_le],
split,
{ rw [dvd_iff_normalized_factors_le_normalized_factors this hI, H],
exact inf_le_left },
{ rw [dvd_iff_normalized_factors_le_normalized_factors this hJ, H],
exact inf_le_right } },
{ rw [← dvd_iff_le, dvd_iff_normalized_factors_le_normalized_factors,
normalized_factors_prod_of_prime, le_iff_count],
{ intro a,
rw multiset.count_inter,
exact le_min (count_le_of_ideal_ge le_sup_left hI a)
(count_le_of_ideal_ge le_sup_right hJ a) },
{ intros p hp,
rw mem_inter at hp,
exact prime_of_normalized_factor p hp.left },
{ exact ne_bot_of_le_ne_bot hI le_sup_left },
{ exact this } },
end
lemma irreducible_pow_sup (hI : I ≠ ⊥) (hJ : irreducible J) (n : ℕ) :
J^n ⊔ I = J^(min ((normalized_factors I).count J) n) :=
by rw [sup_eq_prod_inf_factors (pow_ne_zero n hJ.ne_zero) hI, ← inf_eq_inter,
normalized_factors_of_irreducible_pow hJ, normalize_eq J, repeat_inf, prod_repeat]
lemma irreducible_pow_sup_of_le (hJ : irreducible J) (n : ℕ)
(hn : ↑n ≤ multiplicity J I) : J^n ⊔ I = J^n :=
begin
by_cases hI : I = ⊥,
{ simp [*] at *, },
rw [irreducible_pow_sup hI hJ, min_eq_right],
rwa [multiplicity_eq_count_normalized_factors hJ hI, part_enat.coe_le_coe, normalize_eq J] at hn
end
lemma irreducible_pow_sup_of_ge (hI : I ≠ ⊥) (hJ : irreducible J) (n : ℕ)
(hn : multiplicity J I ≤ n) : J^n ⊔ I = J ^ (multiplicity J I).get (part_enat.dom_of_le_coe hn) :=
begin
rw [irreducible_pow_sup hI hJ, min_eq_left],
congr,
{ rw [← part_enat.coe_inj, part_enat.coe_get, multiplicity_eq_count_normalized_factors hJ hI,
normalize_eq J] },
{ rwa [multiplicity_eq_count_normalized_factors hJ hI, part_enat.coe_le_coe, normalize_eq J]
at hn }
end
end is_dedekind_domain
/-!
### Height one spectrum of a Dedekind domain
If `R` is a Dedekind domain of Krull dimension 1, the maximal ideals of `R` are exactly its nonzero
prime ideals.
We define `height_one_spectrum` and provide lemmas to recover the facts that prime ideals of height
one are prime and irreducible.
-/
namespace is_dedekind_domain
variables [is_domain R] [is_dedekind_domain R]
/-- The height one prime spectrum of a Dedekind domain `R` is the type of nonzero prime ideals of
`R`. Note that this equals the maximal spectrum if `R` has Krull dimension 1. -/
@[ext, nolint has_nonempty_instance unused_arguments]
structure height_one_spectrum :=
(as_ideal : ideal R)
(is_prime : as_ideal.is_prime)
(ne_bot : as_ideal ≠ ⊥)
attribute [instance] height_one_spectrum.is_prime
variables (v : height_one_spectrum R) {R}
namespace height_one_spectrum
instance is_maximal : v.as_ideal.is_maximal := dimension_le_one v.as_ideal v.ne_bot v.is_prime
lemma prime : prime v.as_ideal := ideal.prime_of_is_prime v.ne_bot v.is_prime
lemma irreducible : irreducible v.as_ideal :=
unique_factorization_monoid.irreducible_iff_prime.mpr v.prime
lemma associates_irreducible : _root_.irreducible $ associates.mk v.as_ideal :=
(associates.irreducible_mk _).mpr v.irreducible
/-- An equivalence between the height one and maximal spectra for rings of Krull dimension 1. -/
def equiv_maximal_spectrum (hR : ¬is_field R) : height_one_spectrum R ≃ maximal_spectrum R :=
{ to_fun := λ v, ⟨v.as_ideal, dimension_le_one v.as_ideal v.ne_bot v.is_prime⟩,
inv_fun := λ v,
⟨v.as_ideal, v.is_maximal.is_prime, ring.ne_bot_of_is_maximal_of_not_is_field v.is_maximal hR⟩,
left_inv := λ ⟨_, _, _⟩, rfl,
right_inv := λ ⟨_, _⟩, rfl }
variables (R K)
/-- A Dedekind domain is equal to the intersection of its localizations at all its height one
non-zero prime ideals viewed as subalgebras of its field of fractions. -/
theorem infi_localization_eq_bot [algebra R K] [hK : is_fraction_ring R K] :
(⨅ v : height_one_spectrum R,
localization.subalgebra.of_field K _ v.as_ideal.prime_compl_le_non_zero_divisors) = ⊥ :=
begin
ext x,
rw [algebra.mem_infi],
split,
by_cases hR : is_field R,
{ rcases function.bijective_iff_has_inverse.mp
(is_field.localization_map_bijective (flip non_zero_divisors.ne_zero rfl : 0 ∉ R⁰) hR)
with ⟨algebra_map_inv, _, algebra_map_right_inv⟩,
exact λ _, algebra.mem_bot.mpr ⟨algebra_map_inv x, algebra_map_right_inv x⟩,
exact hK },
all_goals { rw [← maximal_spectrum.infi_localization_eq_bot, algebra.mem_infi] },
{ exact λ hx ⟨v, hv⟩, hx ((equiv_maximal_spectrum hR).symm ⟨v, hv⟩) },
{ exact λ hx ⟨v, hv, hbot⟩, hx ⟨v, dimension_le_one v hbot hv⟩ }
end
end height_one_spectrum
end is_dedekind_domain
section
open ideal
variables {R} {A} [is_dedekind_domain A] {I : ideal R} {J : ideal A}
/-- The map from ideals of `R` dividing `I` to the ideals of `A` dividing `J` induced by
a homomorphism `f : R/I →+* A/J` -/
@[simps]
def ideal_factors_fun_of_quot_hom {f : R ⧸ I →+* A ⧸ J} (hf : function.surjective f ) :
{p : ideal R | p ∣ I} →o {p : ideal A | p ∣ J} :=
{ to_fun := λ X, ⟨comap J^.quotient.mk (map f (map I^.quotient.mk X)),
begin
have : (J^.quotient.mk).ker ≤ comap J^.quotient.mk (map f (map I^.quotient.mk X)),
{ exact ker_le_comap J^.quotient.mk },
rw mk_ker at this,
exact dvd_iff_le.mpr this,
end ⟩,
monotone' :=
begin
rintros ⟨X, hX⟩ ⟨Y, hY⟩ h,
rw [← subtype.coe_le_coe, subtype.coe_mk, subtype.coe_mk] at h ⊢,
rw [subtype.coe_mk, comap_le_comap_iff_of_surjective J^.quotient.mk quotient.mk_surjective,
map_le_iff_le_comap, subtype.coe_mk, comap_map_of_surjective _ hf (map I^.quotient.mk Y)],
suffices : map I^.quotient.mk X ≤ map I^.quotient.mk Y,
{ exact le_sup_of_le_left this },
rwa [map_le_iff_le_comap, comap_map_of_surjective I^.quotient.mk quotient.mk_surjective,
← ring_hom.ker_eq_comap_bot, mk_ker, sup_eq_left.mpr $ le_of_dvd hY],
end }
@[simp]
lemma ideal_factors_fun_of_quot_hom_id :
ideal_factors_fun_of_quot_hom (ring_hom.id (A ⧸ J)).is_surjective = order_hom.id :=
order_hom.ext _ _ (funext $ λ X, by simp only [ideal_factors_fun_of_quot_hom, map_id,
order_hom.coe_fun_mk, order_hom.id_coe, id.def, comap_map_of_surjective J^.quotient.mk
quotient.mk_surjective, ← ring_hom.ker_eq_comap_bot J^.quotient.mk, mk_ker, sup_eq_left.mpr
(dvd_iff_le.mp X.prop), subtype.coe_eta] )
variables {B : Type*} [comm_ring B] [is_domain B] [is_dedekind_domain B] {L : ideal B}
lemma ideal_factors_fun_of_quot_hom_comp {f : R ⧸ I →+* A ⧸ J} {g : A ⧸ J →+* B ⧸ L}
(hf : function.surjective f) (hg : function.surjective g) :
(ideal_factors_fun_of_quot_hom hg).comp (ideal_factors_fun_of_quot_hom hf)
= ideal_factors_fun_of_quot_hom (show function.surjective (g.comp f), from hg.comp hf) :=
begin
refine order_hom.ext _ _ (funext $ λ x, _),
rw [ideal_factors_fun_of_quot_hom, ideal_factors_fun_of_quot_hom, order_hom.comp_coe,
order_hom.coe_fun_mk, order_hom.coe_fun_mk, function.comp_app,
ideal_factors_fun_of_quot_hom, order_hom.coe_fun_mk, subtype.mk_eq_mk, subtype.coe_mk,
map_comap_of_surjective J^.quotient.mk quotient.mk_surjective, map_map],
end
variables [is_domain R] [is_dedekind_domain R] (f : R ⧸ I ≃+* A ⧸ J)
/-- The bijection between ideals of `R` dividing `I` and the ideals of `A` dividing `J` induced by
an isomorphism `f : R/I ≅ A/J`. -/
@[simps]
def ideal_factors_equiv_of_quot_equiv : {p : ideal R | p ∣ I} ≃o {p : ideal A | p ∣ J} :=
order_iso.of_hom_inv
(ideal_factors_fun_of_quot_hom (show function.surjective
(f : R ⧸I →+* A ⧸ J), from f.surjective))
(ideal_factors_fun_of_quot_hom (show function.surjective
(f.symm : A ⧸J →+* R ⧸ I), from f.symm.surjective))
(by simp only [← ideal_factors_fun_of_quot_hom_id, order_hom.coe_eq, order_hom.coe_eq,
ideal_factors_fun_of_quot_hom_comp, ← ring_equiv.to_ring_hom_eq_coe,
← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_trans, ring_equiv.symm_trans_self,
ring_equiv.to_ring_hom_refl])
(by simp only [← ideal_factors_fun_of_quot_hom_id, order_hom.coe_eq, order_hom.coe_eq,
ideal_factors_fun_of_quot_hom_comp, ← ring_equiv.to_ring_hom_eq_coe,
← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_trans, ring_equiv.self_trans_symm,
ring_equiv.to_ring_hom_refl])
lemma ideal_factors_equiv_of_quot_equiv_symm :
(ideal_factors_equiv_of_quot_equiv f).symm = ideal_factors_equiv_of_quot_equiv f.symm := rfl
lemma ideal_factors_equiv_of_quot_equiv_is_dvd_iso {L M : ideal R} (hL : L ∣ I) (hM : M ∣ I) :
(ideal_factors_equiv_of_quot_equiv f ⟨L, hL⟩ : ideal A) ∣
ideal_factors_equiv_of_quot_equiv f ⟨M, hM⟩ ↔ L ∣ M :=
begin
suffices : ideal_factors_equiv_of_quot_equiv f ⟨M, hM⟩ ≤
ideal_factors_equiv_of_quot_equiv f ⟨L, hL⟩ ↔ (⟨M, hM⟩ : {p : ideal R | p ∣ I}) ≤ ⟨L, hL⟩,
{ rw [dvd_iff_le, dvd_iff_le, subtype.coe_le_coe, this, subtype.mk_le_mk] },
exact (ideal_factors_equiv_of_quot_equiv f).le_iff_le,
end
open unique_factorization_monoid
variables [decidable_eq (ideal R)] [decidable_eq (ideal A)]
lemma ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors
(hJ : J ≠ ⊥) {L : ideal R} (hL : L ∈ normalized_factors I) :
↑(ideal_factors_equiv_of_quot_equiv f
⟨L, dvd_of_mem_normalized_factors hL⟩) ∈ normalized_factors J :=
begin
by_cases hI : I = ⊥,
{ exfalso,
rw [hI, bot_eq_zero, normalized_factors_zero, ← multiset.empty_eq_zero] at hL,
exact hL, },
{ apply mem_normalized_factors_factor_dvd_iso_of_mem_normalized_factors hI hJ hL _,
rintros ⟨l, hl⟩ ⟨l', hl'⟩,
rw [subtype.coe_mk, subtype.coe_mk],
apply ideal_factors_equiv_of_quot_equiv_is_dvd_iso f }
end
/-- The bijection between the sets of normalized factors of I and J induced by a ring
isomorphism `f : R/I ≅ A/J`. -/
@[simps apply]
def normalized_factors_equiv_of_quot_equiv (hI : I ≠ ⊥) (hJ : J ≠ ⊥) :
{L : ideal R | L ∈ normalized_factors I } ≃ {M : ideal A | M ∈ normalized_factors J } :=
{ to_fun := λ j, ⟨ideal_factors_equiv_of_quot_equiv f ⟨↑j, dvd_of_mem_normalized_factors j.prop⟩,
ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors f hJ j.prop⟩,
inv_fun := λ j, ⟨(ideal_factors_equiv_of_quot_equiv f).symm
⟨↑j, dvd_of_mem_normalized_factors j.prop⟩, by { rw ideal_factors_equiv_of_quot_equiv_symm,
exact ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors
f.symm hI j.prop} ⟩,
left_inv := λ ⟨j, hj⟩, by simp,
right_inv := λ ⟨j, hj⟩, by simp }
@[simp]
lemma normalized_factors_equiv_of_quot_equiv_symm (hI : I ≠ ⊥) (hJ : J ≠ ⊥) :
(normalized_factors_equiv_of_quot_equiv f hI hJ).symm =
normalized_factors_equiv_of_quot_equiv f.symm hJ hI :=
rfl
variable [decidable_rel ((∣) : ideal R → ideal R → Prop)]
variable [decidable_rel ((∣) : ideal A → ideal A → Prop)]
/-- The map `normalized_factors_equiv_of_quot_equiv` preserves multiplicities. -/
lemma normalized_factors_equiv_of_quot_equiv_multiplicity_eq_multiplicity (hI : I ≠ ⊥) (hJ : J ≠ ⊥)
(L : ideal R) (hL : L ∈ normalized_factors I) :
multiplicity ↑(normalized_factors_equiv_of_quot_equiv f hI hJ ⟨L, hL⟩) J = multiplicity L I :=
begin
rw [normalized_factors_equiv_of_quot_equiv, equiv.coe_fn_mk, subtype.coe_mk],
exact multiplicity_factor_dvd_iso_eq_multiplicity_of_mem_normalized_factor hI hJ hL
(λ ⟨l, hl⟩ ⟨l', hl'⟩, ideal_factors_equiv_of_quot_equiv_is_dvd_iso f hl hl'),
end
end
section chinese_remainder
open ideal unique_factorization_monoid
open_locale big_operators
variables {R}
lemma ring.dimension_le_one.prime_le_prime_iff_eq (h : ring.dimension_le_one R)
{P Q : ideal R} [hP : P.is_prime] [hQ : Q.is_prime] (hP0 : P ≠ ⊥) :
P ≤ Q ↔ P = Q :=
⟨(h P hP0 hP).eq_of_le hQ.ne_top, eq.le⟩
lemma ideal.coprime_of_no_prime_ge {I J : ideal R} (h : ∀ P, I ≤ P → J ≤ P → ¬ is_prime P) :
I ⊔ J = ⊤ :=
begin
by_contra hIJ,
obtain ⟨P, hP, hIJ⟩ := ideal.exists_le_maximal _ hIJ,
exact h P (le_trans le_sup_left hIJ) (le_trans le_sup_right hIJ) hP.is_prime
end
section dedekind_domain
variables {R} [is_domain R] [is_dedekind_domain R]
lemma ideal.is_prime.mul_mem_pow (I : ideal R) [hI : I.is_prime] {a b : R} {n : ℕ}
(h : a * b ∈ I^n) : a ∈ I ∨ b ∈ I^n :=
begin
cases n, { simp },
by_cases hI0 : I = ⊥, { simpa [pow_succ, hI0] using h },
simp only [← submodule.span_singleton_le_iff_mem, ideal.submodule_span_eq, ← ideal.dvd_iff_le,
← ideal.span_singleton_mul_span_singleton] at h ⊢,
by_cases ha : I ∣ span {a},
{ exact or.inl ha },
rw mul_comm at h,
exact or.inr (prime.pow_dvd_of_dvd_mul_right ((ideal.prime_iff_is_prime hI0).mpr hI) _ ha h),
end
section
open_locale classical
lemma ideal.count_normalized_factors_eq {p x : ideal R} [hp : p.is_prime] {n : ℕ}
(hle : x ≤ p^n) (hlt : ¬ (x ≤ p^(n+1))) :
(normalized_factors x).count p = n :=
count_normalized_factors_eq'
((ideal.is_prime_iff_bot_or_prime.mp hp).imp_right prime.irreducible)
(by { haveI : unique (ideal R)ˣ := ideal.unique_units, apply normalize_eq })
(by convert ideal.dvd_iff_le.mpr hle) (by convert mt ideal.le_of_dvd hlt)
/- Warning: even though a pure term-mode proof typechecks (the `by convert` can simply be
removed), it's slower to the point of a possible timeout. -/
end
lemma ideal.le_mul_of_no_prime_factors
{I J K : ideal R} (coprime : ∀ P, J ≤ P → K ≤ P → ¬ is_prime P) (hJ : I ≤ J) (hK : I ≤ K) :
I ≤ J * K :=
begin
simp only [← ideal.dvd_iff_le] at coprime hJ hK ⊢,
by_cases hJ0 : J = 0,
{ simpa only [hJ0, zero_mul] using hJ },
obtain ⟨I', rfl⟩ := hK,
rw mul_comm,
exact mul_dvd_mul_left K
(unique_factorization_monoid.dvd_of_dvd_mul_right_of_no_prime_factors hJ0
(λ P hPJ hPK, mt ideal.is_prime_of_prime (coprime P hPJ hPK))
hJ)
end
lemma ideal.le_of_pow_le_prime {I P : ideal R} [hP : P.is_prime] {n : ℕ} (h : I^n ≤ P) : I ≤ P :=
begin
by_cases hP0 : P = ⊥,
{ simp only [hP0, le_bot_iff] at ⊢ h,
exact pow_eq_zero h },
rw ← ideal.dvd_iff_le at ⊢ h,
exact ((ideal.prime_iff_is_prime hP0).mpr hP).dvd_of_dvd_pow h
end
lemma ideal.pow_le_prime_iff {I P : ideal R} [hP : P.is_prime] {n : ℕ} (hn : n ≠ 0) :
I^n ≤ P ↔ I ≤ P :=
⟨ideal.le_of_pow_le_prime, λ h, trans (ideal.pow_le_self hn) h⟩
lemma ideal.prod_le_prime {ι : Type*} {s : finset ι} {f : ι → ideal R} {P : ideal R}
[hP : P.is_prime] :
∏ i in s, f i ≤ P ↔ ∃ i ∈ s, f i ≤ P :=
begin
by_cases hP0 : P = ⊥,
{ simp only [hP0, le_bot_iff],
rw [← ideal.zero_eq_bot, finset.prod_eq_zero_iff] },
simp only [← ideal.dvd_iff_le],
exact ((ideal.prime_iff_is_prime hP0).mpr hP).dvd_finset_prod_iff _
end
/-- The intersection of distinct prime powers in a Dedekind domain is the product of these
prime powers. -/
lemma is_dedekind_domain.inf_prime_pow_eq_prod {ι : Type*}
(s : finset ι) (f : ι → ideal R) (e : ι → ℕ)
(prime : ∀ i ∈ s, prime (f i)) (coprime : ∀ i j ∈ s, i ≠ j → f i ≠ f j) :
s.inf (λ i, f i ^ e i) = ∏ i in s, f i ^ e i :=
begin
letI := classical.dec_eq ι,
revert prime coprime,
refine s.induction _ _,
{ simp },
intros a s ha ih prime coprime,
specialize ih (λ i hi, prime i (finset.mem_insert_of_mem hi))
(λ i hi j hj, coprime i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj)),
rw [finset.inf_insert, finset.prod_insert ha, ih],
refine le_antisymm (ideal.le_mul_of_no_prime_factors _ inf_le_left inf_le_right) ideal.mul_le_inf,
intros P hPa hPs hPp,
haveI := hPp,
obtain ⟨b, hb, hPb⟩ := ideal.prod_le_prime.mp hPs,
haveI := ideal.is_prime_of_prime (prime a (finset.mem_insert_self a s)),
haveI := ideal.is_prime_of_prime (prime b (finset.mem_insert_of_mem hb)),
refine coprime a (finset.mem_insert_self a s) b (finset.mem_insert_of_mem hb) _
(((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq _).mp
(ideal.le_of_pow_le_prime hPa)).trans
((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq _).mp
(ideal.le_of_pow_le_prime hPb)).symm),
{ unfreezingI { rintro rfl }, contradiction },
{ exact (prime a (finset.mem_insert_self a s)).ne_zero },
{ exact (prime b (finset.mem_insert_of_mem hb)).ne_zero },
end
/-- **Chinese remainder theorem** for a Dedekind domain: if the ideal `I` factors as
`∏ i, P i ^ e i`, then `R ⧸ I` factors as `Π i, R ⧸ (P i ^ e i)`. -/
noncomputable def is_dedekind_domain.quotient_equiv_pi_of_prod_eq {ι : Type*} [fintype ι]
(I : ideal R) (P : ι → ideal R) (e : ι → ℕ)
(prime : ∀ i, prime (P i)) (coprime : ∀ i j, i ≠ j → P i ≠ P j) (prod_eq : (∏ i, P i ^ e i) = I) :
R ⧸ I ≃+* Π i, R ⧸ (P i ^ e i) :=
(ideal.quot_equiv_of_eq (by { simp only [← prod_eq, finset.inf_eq_infi, finset.mem_univ, cinfi_pos,
← is_dedekind_domain.inf_prime_pow_eq_prod _ _ _ (λ i _, prime i) (λ i _ j _, coprime i j)] }))
.trans $
ideal.quotient_inf_ring_equiv_pi_quotient _ (λ i j hij, ideal.coprime_of_no_prime_ge (begin
intros P hPi hPj hPp,
haveI := hPp,
haveI := ideal.is_prime_of_prime (prime i), haveI := ideal.is_prime_of_prime (prime j),
exact coprime i j hij
(((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq (prime i).ne_zero).mp
(ideal.le_of_pow_le_prime hPi)).trans
((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq (prime j).ne_zero).mp
(ideal.le_of_pow_le_prime hPj)).symm)
end))
open_locale classical
/-- **Chinese remainder theorem** for a Dedekind domain: `R ⧸ I` factors as `Π i, R ⧸ (P i ^ e i)`,
where `P i` ranges over the prime factors of `I` and `e i` over the multiplicities. -/
noncomputable def is_dedekind_domain.quotient_equiv_pi_factors {I : ideal R} (hI : I ≠ ⊥) :
R ⧸ I ≃+* Π (P : (factors I).to_finset), R ⧸ ((P : ideal R) ^ (factors I).count P) :=
is_dedekind_domain.quotient_equiv_pi_of_prod_eq _ _ _
(λ (P : (factors I).to_finset), prime_of_factor _ (multiset.mem_to_finset.mp P.prop))
(λ i j hij, subtype.coe_injective.ne hij)
(calc ∏ (P : (factors I).to_finset), (P : ideal R) ^ (factors I).count (P : ideal R)
= ∏ P in (factors I).to_finset, P ^ (factors I).count P
: (factors I).to_finset.prod_coe_sort (λ P, P ^ (factors I).count P)
... = ((factors I).map (λ P, P)).prod : (finset.prod_multiset_map_count (factors I) id).symm
... = (factors I).prod : by rw multiset.map_id'
... = I : (@associated_iff_eq (ideal R) _ ideal.unique_units _ _).mp (factors_prod hI))
@[simp] lemma is_dedekind_domain.quotient_equiv_pi_factors_mk {I : ideal R} (hI : I ≠ ⊥)
(x : R) : is_dedekind_domain.quotient_equiv_pi_factors hI (ideal.quotient.mk I x) =
λ P, ideal.quotient.mk _ x :=
rfl
/-- **Chinese remainder theorem**, specialized to two ideals. -/
noncomputable def ideal.quotient_mul_equiv_quotient_prod (I J : ideal R)
(coprime : I ⊔ J = ⊤) :
(R ⧸ (I * J)) ≃+* (R ⧸ I) × R ⧸ J :=
ring_equiv.trans
(ideal.quot_equiv_of_eq (inf_eq_mul_of_coprime coprime).symm)
(ideal.quotient_inf_equiv_quotient_prod I J coprime)
end dedekind_domain
end chinese_remainder
section PID
open multiplicity unique_factorization_monoid ideal
variables {R} [is_domain R] [is_principal_ideal_ring R]
lemma span_singleton_dvd_span_singleton_iff_dvd {a b : R} :
(ideal.span {a}) ∣ (ideal.span ({b} : set R)) ↔ a ∣ b :=
⟨λ h, mem_span_singleton.mp (dvd_iff_le.mp h (mem_span_singleton.mpr (dvd_refl b))),
λ h, dvd_iff_le.mpr (λ d hd, mem_span_singleton.mpr (dvd_trans h (mem_span_singleton.mp hd)))⟩
lemma singleton_span_mem_normalized_factors_of_mem_normalized_factors [normalization_monoid R]
[decidable_eq R] [decidable_eq (ideal R)] {a b : R} (ha : a ∈ normalized_factors b) :
ideal.span ({a} : set R) ∈ normalized_factors (ideal.span ({b} : set R)) :=
begin
by_cases hb : b = 0,
{ rw [ideal.span_singleton_eq_bot.mpr hb, bot_eq_zero, normalized_factors_zero],
rw [hb, normalized_factors_zero] at ha,
simpa only [multiset.not_mem_zero] },
{ suffices : prime (ideal.span ({a} : set R)),
{ obtain ⟨c, hc, hc'⟩ := exists_mem_normalized_factors_of_dvd _ this.irreducible
(dvd_iff_le.mpr (span_singleton_le_span_singleton.mpr (dvd_of_mem_normalized_factors ha))),
rwa associated_iff_eq.mp hc',
{ by_contra,
exact hb (span_singleton_eq_bot.mp h) } },
rw prime_iff_is_prime,
exact (span_singleton_prime (prime_of_normalized_factor a ha).ne_zero).mpr
(prime_of_normalized_factor a ha),
by_contra,
exact (prime_of_normalized_factor a ha).ne_zero (span_singleton_eq_bot.mp h) },
end
lemma multiplicity_eq_multiplicity_span [decidable_rel ((∣) : R → R → Prop)]
[decidable_rel ((∣) : ideal R → ideal R → Prop)] {a b : R} :
multiplicity (ideal.span {a}) (ideal.span ({b} : set R)) = multiplicity a b :=
begin
by_cases h : finite a b,
{ rw ← part_enat.coe_get (finite_iff_dom.mp h),
refine (multiplicity.unique
(show (ideal.span {a})^(((multiplicity a b).get h)) ∣ (ideal.span {b}), from _) _).symm ;
rw [ideal.span_singleton_pow, span_singleton_dvd_span_singleton_iff_dvd],
exact pow_multiplicity_dvd h ,
{ exact multiplicity.is_greatest ((part_enat.lt_coe_iff _ _).mpr (exists.intro
(finite_iff_dom.mp h) (nat.lt_succ_self _))) } },
{ suffices : ¬ (finite (ideal.span ({a} : set R)) (ideal.span ({b} : set R))),
{ rw [finite_iff_dom, part_enat.not_dom_iff_eq_top] at h this,
rw [h, this] },
refine not_finite_iff_forall.mpr (λ n, by {rw [ideal.span_singleton_pow,
span_singleton_dvd_span_singleton_iff_dvd], exact not_finite_iff_forall.mp h n }) }
end
variables [decidable_eq R] [decidable_eq (ideal R)] [normalization_monoid R]
/-- The bijection between the (normalized) prime factors of `r` and the (normalized) prime factors
of `span {r}` -/
@[simps]
noncomputable def normalized_factors_equiv_span_normalized_factors {r : R} (hr : r ≠ 0) :
{d : R | d ∈ normalized_factors r} ≃
{I : ideal R | I ∈ normalized_factors (ideal.span ({r} : set R))} :=
equiv.of_bijective
(λ d, ⟨ideal.span {↑d}, singleton_span_mem_normalized_factors_of_mem_normalized_factors d.prop⟩)
begin
split,
{ rintros ⟨a, ha⟩ ⟨b, hb⟩ h,
rw [subtype.mk_eq_mk, ideal.span_singleton_eq_span_singleton, subtype.coe_mk,
subtype.coe_mk] at h,
exact subtype.mk_eq_mk.mpr (mem_normalized_factors_eq_of_associated ha hb h) },
{ rintros ⟨i, hi⟩,
letI : i.is_principal := infer_instance,
letI : i.is_prime := is_prime_of_prime (prime_of_normalized_factor i hi),
obtain ⟨a, ha, ha'⟩ := exists_mem_normalized_factors_of_dvd hr
(submodule.is_principal.prime_generator_of_is_prime i
(prime_of_normalized_factor i hi).ne_zero).irreducible _,
{ use ⟨a, ha⟩,
simp only [subtype.coe_mk, subtype.mk_eq_mk, ← span_singleton_eq_span_singleton.mpr ha',
ideal.span_singleton_generator] },
{exact (submodule.is_principal.mem_iff_generator_dvd i).mp (((show ideal.span {r} ≤ i, from
dvd_iff_le.mp (dvd_of_mem_normalized_factors hi))) (mem_span_singleton.mpr (dvd_refl r))) } }
end
variables [decidable_rel ((∣) : R → R → Prop)] [decidable_rel ((∣) : ideal R → ideal R → Prop)]
/-- The bijection `normalized_factors_equiv_span_normalized_factors` between the set of prime
factors of `r` and the set of prime factors of the ideal `⟨r⟩` preserves multiplicities. -/
lemma multiplicity_normalized_factors_equiv_span_normalized_factors_eq_multiplicity {r d: R}
(hr : r ≠ 0) (hd : d ∈ normalized_factors r) :
multiplicity d r =
multiplicity (normalized_factors_equiv_span_normalized_factors hr ⟨d, hd⟩ : ideal R)
(ideal.span {r}) :=
by simp only [normalized_factors_equiv_span_normalized_factors, multiplicity_eq_multiplicity_span,
subtype.coe_mk, equiv.of_bijective_apply]
/-- The bijection `normalized_factors_equiv_span_normalized_factors.symm` between the set of prime
factors of the ideal `⟨r⟩` and the set of prime factors of `r` preserves multiplicities. -/
lemma multiplicity_normalized_factors_equiv_span_normalized_factors_symm_eq_multiplicity
{r : R} (hr : r ≠ 0) (I : {I : ideal R | I ∈ normalized_factors (ideal.span ({r} : set R))}) :
multiplicity ((normalized_factors_equiv_span_normalized_factors hr).symm I : R) r =
multiplicity (I : ideal R) (ideal.span {r}) :=
begin
obtain ⟨x, hx⟩ := (normalized_factors_equiv_span_normalized_factors hr).surjective I,
obtain ⟨a, ha⟩ := x,
rw [hx.symm, equiv.symm_apply_apply, subtype.coe_mk,
multiplicity_normalized_factors_equiv_span_normalized_factors_eq_multiplicity hr ha, hx],
end
end PID
|
510354081b529783d174a0a02543f053dc0a3240 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/data/option/defs.lean | 59f684ecfdfa786f63f56189061e32fc41935764 | [
"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 | 6,178 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Extra definitions on option.
-/
namespace option
variables {α : Type*} {β : Type*}
attribute [inline] option.is_some option.is_none
/-- An elimination principle for `option`. It is a nondependent version of `option.rec_on`. -/
@[simp] protected def elim : option α → β → (α → β) → β
| (some x) y f := f x
| none y f := y
instance has_mem : has_mem α (option α) := ⟨λ a b, b = some a⟩
@[simp] theorem mem_def {a : α} {b : option α} : a ∈ b ↔ b = some a :=
iff.rfl
theorem is_none_iff_eq_none {o : option α} : o.is_none = tt ↔ o = none :=
⟨option.eq_none_of_is_none, λ e, e.symm ▸ rfl⟩
theorem some_inj {a b : α} : some a = some b ↔ a = b := by simp
/--
`o = none` is decidable even if the wrapped type does not have decidable equality.
This is not an instance because it is not definitionally equal to `option.decidable_eq`.
Try to use `o.is_none` or `o.is_some` instead.
-/
@[inline]
def decidable_eq_none {o : option α} : decidable (o = none) :=
decidable_of_decidable_of_iff (bool.decidable_eq _ _) is_none_iff_eq_none
instance decidable_forall_mem {p : α → Prop} [decidable_pred p] :
∀ o : option α, decidable (∀ a ∈ o, p a)
| none := is_true (by simp [false_implies_iff])
| (some a) := if h : p a
then is_true $ λ o e, some_inj.1 e ▸ h
else is_false $ mt (λ H, H _ rfl) h
instance decidable_exists_mem {p : α → Prop} [decidable_pred p] :
∀ o : option α, decidable (∃ a ∈ o, p a)
| none := is_false (λ ⟨a, ⟨h, _⟩⟩, by cases h)
| (some a) := if h : p a
then is_true $ ⟨_, rfl, h⟩
else is_false $ λ ⟨_, ⟨rfl, hn⟩⟩, h hn
/-- inhabited `get` function. Returns `a` if the input is `some a`,
otherwise returns `default`. -/
@[reducible] def iget [inhabited α] : option α → α
| (some x) := x
| none := default α
@[simp] theorem iget_some [inhabited α] {a : α} : (some a).iget = a := rfl
/-- `guard p a` returns `some a` if `p a` holds, otherwise `none`. -/
def guard (p : α → Prop) [decidable_pred p] (a : α) : option α :=
if p a then some a else none
/-- `filter p o` returns `some a` if `o` is `some a`
and `p a` holds, otherwise `none`. -/
def filter (p : α → Prop) [decidable_pred p] (o : option α) : option α :=
o.bind (guard p)
def to_list : option α → list α
| none := []
| (some a) := [a]
@[simp] theorem mem_to_list {a : α} {o : option α} : a ∈ to_list o ↔ a ∈ o :=
by cases o; simp [to_list, eq_comm]
def lift_or_get (f : α → α → α) : option α → option α → option α
| none none := none
| (some a) none := some a -- get a
| none (some b) := some b -- get b
| (some a) (some b) := some (f a b) -- lift f
instance lift_or_get_comm (f : α → α → α) [h : is_commutative α f] :
is_commutative (option α) (lift_or_get f) :=
⟨λ a b, by cases a; cases b; simp [lift_or_get, h.comm]⟩
instance lift_or_get_assoc (f : α → α → α) [h : is_associative α f] :
is_associative (option α) (lift_or_get f) :=
⟨λ a b c, by cases a; cases b; cases c; simp [lift_or_get, h.assoc]⟩
instance lift_or_get_idem (f : α → α → α) [h : is_idempotent α f] :
is_idempotent (option α) (lift_or_get f) :=
⟨λ a, by cases a; simp [lift_or_get, h.idempotent]⟩
instance lift_or_get_is_left_id (f : α → α → α) :
is_left_id (option α) (lift_or_get f) none :=
⟨λ a, by cases a; simp [lift_or_get]⟩
instance lift_or_get_is_right_id (f : α → α → α) :
is_right_id (option α) (lift_or_get f) none :=
⟨λ a, by cases a; simp [lift_or_get]⟩
inductive rel (r : α → β → Prop) : option α → option β → Prop
| some {a b} : r a b → rel (some a) (some b)
| none : rel none none
/-- Partial bind. If for some `x : option α`, `f : Π (a : α), a ∈ x → option β` is a
partial function defined on `a : α` giving an `option β`, where `some a = x`,
then `pbind x f h` is essentially the same as `bind x f`
but is defined only when all `x = some a`, using the proof to apply `f`. -/
@[simp] def pbind : Π (x : option α), (Π (a : α), a ∈ x → option β) → option β
| none _ := none
| (some a) f := f a rfl
/-- Partial map. If `f : Π a, p a → β` is a partial function defined on
`a : α` satisfying `p`, then `pmap f x h` is essentially the same as `map f x`
but is defined only when all members of `x` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {p : α → Prop} (f : Π (a : α), p a → β) :
Π x : option α, (∀ a ∈ x, p a) → option β
| none _ := none
| (some a) H := some (f a (H a (mem_def.mpr rfl)))
/--
Flatten an `option` of `option`, a specialization of `mjoin`.
-/
@[simp] def join : option (option α) → option α :=
λ x, bind x id
protected def {u v} traverse {F : Type u → Type v} [applicative F] {α β : Type*} (f : α → F β) :
option α → F (option β)
| none := pure none
| (some x) := some <$> f x
/- By analogy with `monad.sequence` in `init/category/combinators.lean`. -/
/-- If you maybe have a monadic computation in a `[monad m]` which produces a term of type `α`, then
there is a naturally associated way to always perform a computation in `m` which maybe produces a
result. -/
def {u v} maybe {m : Type u → Type v} [monad m] {α : Type u} : option (m α) → m (option α)
| none := return none
| (some fn) := some <$> fn
/-- Map a monadic function `f : α → m β` over an `o : option α`, maybe producing a result. -/
def {u v w} mmap {m : Type u → Type v} [monad m] {α : Type w} {β : Type u} (f : α → m β)
(o : option α) : m (option β) := (o.map f).maybe
/--
A monadic analogue of `option.elim`.
-/
def melim {α β : Type*} {m : Type* → Type*} [monad m] (x : m (option α)) (y : m β) (z : α → m β) :
m β :=
x >>= λ o, option.elim o y z
/--
A monadic analogue of `option.get_or_else`.
-/
def mget_or_else {α : Type*} {m : Type* → Type*} [monad m] (x : m (option α)) (y : m α) : m α :=
melim x y pure
end option
|
690cbaf68af092ff63e1a653a884e12a0a46735f | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/isDefEqOffsetBug.lean | 5a6a0f200d015a5689f462e88b4521ec06d1bf96 | [
"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 | 683 | lean | class Zero (α : Type u) where
zero : α
export Zero (zero)
instance [Zero α] : OfNat α (nat_lit 0) where
ofNat := zero
class AddGroup (α : Type u) extends Add α, Zero α, Neg α where
addAssoc : {a b c : α} → a + b + c = a + (b + c)
zeroAdd : {a : α} → 0 + a = a
addZero : {a : α} → a + 0 = a
negAdd : {a : α} → -a + a = 0
open AddGroup
theorem negZero [AddGroup α] : -(0 : α) = 0 := by
rw [←addZero (a := -(0 : α)), negAdd]
theorem subZero [AddGroup α] {a : α} : a + -(0 : α) = a := by
rw [← addZero (a := a)]
rw [addAssoc]
rw [negZero]
rw [addZero]
theorem shouldFail [AddGroup α] : ((0 : α) + 0) = 0 :=
rfl -- Error
|
e3f6cdcc041346856d655f441342c31056c21c66 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/new_proj_notation.lean | 2805e97149838c23c1f33cf2ec6470e6a67f3116 | [
"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 | 92 | lean | variable x : list nat
#check x^.map (+1)
#check x^.foldl (+) 0
#eval [1, 2, 3]^.map (+3)
|
31af6672034275f35d6dd06249c9152b66552a72 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /archive/imo/imo1988_q6.lean | 5599d30145a81c51f34c563b144eb25dd88b8ece | [
"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 | 13,837 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.rat.basic
import order.well_founded
import tactic.linarith
import tactic.omega
/-!
# IMO1988 Q6 and constant descent Vieta jumping
Question 6 of IMO1988 is somewhat (in)famous. Several expert problem solvers
could not tackle the question within the given time limit.
The problem lead to the introduction of a new proof technique,
so called “Vieta jumping”.
In this file we formalise constant descent Vieta jumping,
and apply this to prove Q6 of IMO1988.
To illustrate the technique, we also prove a similar result.
-/
-- open_locale classical
local attribute [instance] classical.prop_decidable
local attribute [simp] pow_two
/-- Constant descent Vieta jumping.
This proof technique allows one to prove an arbitrary proposition `claim`,
by running a descent argument on a hyperbola `H` in the first quadrant of the plane,
under the following conditions:
* `h₀` : There exists an integral point `(x,y)` on the hyperbola `H`.
* `H_symm` : The hyperbola has a symmetry along the diagonal in the plane.
* `H_zero` : If an integral point `(x,0)` lies on the hyperbola `H`, then `claim` is true.
* `H_diag` : If an integral point `(x,x)` lies on the hyperbola `H`, then `claim` is true.
* `H_desc` : If `(x,y)` is an integral point on the hyperbola `H`,
with `x < y` then there exists a “smaller” point on `H`: a point `(x',y')` with `x' < y' ≤ x`.
For reasons of usability, the hyperbola `H` is implemented as an arbitrary predicate.
(In question 6 of IMO1988, where this proof technique was first developped,
the predicate `claim` would be `∃ (d : ℕ), d ^ 2 = k` for some natural number `k`,
and the predicate `H` would be `λ a b, a * a + b * b = (a * b + 1) * k`.)
To ensure that the predicate `H` actually describes a hyperbola,
the user must provide arguments `B` and `C` that are used as coefficients for a quadratic equation.
Finally, `H_quad` is the proof obligation that the quadratic equation
`(y:ℤ) * y - B x * y + C x = 0`
describes the same hyperbola as the predicate `H`.
For extra flexibility, one must provide a predicate `base` on the integral points in the plane.
In the descent step `H_desc` this will give the user the additional assumption that
the point `(x,y)` does not lie in this base locus.
The user must provide a proof that the proposition `claim` is true
if there exists an integral point `(x,y)` on the hyperbola `H` that lies in the base locus.
If such a base locus is not necessary, once can simply let it be `λ x y, false`.
-/
lemma constant_descent_vieta_jumping (x y : ℕ) {claim : Prop} {H : ℕ → ℕ → Prop}
(h₀ : H x y) (B : ℕ → ℤ) (C : ℕ → ℤ) (base : ℕ → ℕ → Prop)
(H_quad : ∀ {x y}, H x y ↔ (y:ℤ) * y - B x * y + C x = 0) (H_symm : ∀ {x y}, H x y ↔ H y x)
(H_zero : ∀ {x}, H x 0 → claim) (H_diag : ∀ {x}, H x x → claim)
(H_desc : ∀ {x y}, 0 < x → x < y → ¬base x y → H x y →
∀ y', y' * y' - B x * y' + C x = 0 → y' = B x - y → y' * y = C x → 0 ≤ y' ∧ y' ≤ x)
(H_base : ∀ {x y}, H x y → base x y → claim) :
claim :=
begin
-- First of all, we may assume that x ≤ y.
-- We justify this using H_symm.
wlog hxy : x ≤ y, swap, { rw H_symm at h₀, solve_by_elim },
-- In fact, we can easily deal with the case x = y.
by_cases x_eq_y : x = y, {subst x_eq_y, exact H_diag h₀},
-- Hence we may assume that x < y.
replace hxy : x < y := lt_of_le_of_ne hxy x_eq_y, clear x_eq_y,
-- Consider the upper branch of the hyperbola defined by H.
let upper_branch : set (ℕ × ℕ) := {p | H p.1 p.2 ∧ p.1 < p.2},
-- Note that the point p = (x,y) lies on the upper branch.
let p : ℕ × ℕ := ⟨x,y⟩,
have hp : p ∈ upper_branch := ⟨h₀, hxy⟩,
-- We also consider the exceptional set of solutions (a,b) that satisfy
-- a = 0 or a = b or B a = b or B a = b + a or that lie in the base locus.
let exceptional : set (ℕ × ℕ) :=
{p | H p.1 p.2 ∧ (base p.1 p.2 ∨ p.1 = 0 ∨ p.1 = p.2 ∨ B p.1 = p.2 ∨ B p.1 = p.2 + p.1) },
-- Let S be the projection of the upper branch on to the y-axis
-- after removing the exceptional locus.
let S : set ℕ := prod.snd '' (upper_branch \ exceptional),
-- The strategy is to show that the exceptional locus in nonempty
-- by running a descent argument that starts with the given point p = (x,y).
-- Our assumptions ensure that we can then prove the claim.
suffices exc : exceptional.nonempty,
{ -- Suppose that there exists an element in the exceptional locus.
simp [exceptional, -add_comm, set.nonempty] at exc,
-- Let (a,b) be such an element, and consider all the possible cases.
rcases exc with ⟨a, b, hH, hb⟩, rcases hb with _|rfl|rfl|hB|hB,
-- The first three cases are rather easy to solve.
{ solve_by_elim },
{ rw H_symm at hH, solve_by_elim },
{ solve_by_elim },
-- The final two cases are very similar.
all_goals {
-- Consider the quadratic equation that (a,b) satisfies.
rw H_quad at hH,
-- We find the other root of the equation, and Vieta's formulas.
rcases Vieta_formula_quadratic hH with ⟨c, h_root, hV₁, hV₂⟩,
-- By substitutions we find that b = 0 or b = a.
simp [hB] at hV₁, subst hV₁,
rw [← int.coe_nat_zero] at *,
rw ← H_quad at h_root,
-- And hence we are done by H_zero and H_diag.
solve_by_elim } },
-- To finish the main proof, we need to show that the exceptional locus is nonempty.
-- So we assume that the exceptional locus is empty, and work towards dering a contradiction.
rw ← set.ne_empty_iff_nonempty,
assume exceptional_empty,
-- Observe that S is nonempty.
have S_nonempty : S.nonempty,
{ -- It contains the image of p.
use p.2,
apply set.mem_image_of_mem,
-- After all, we assumed that the exceptional locus is empty.
rwa [exceptional_empty, set.diff_empty], },
-- We are now set for an infinite descent argument.
-- Let m be the smallest element of the nonempty set S.
let m : ℕ := well_founded.min nat.lt_wf S S_nonempty,
have m_mem : m ∈ S := well_founded.min_mem nat.lt_wf S S_nonempty,
have m_min : ∀ k ∈ S, ¬ k < m := λ k hk, well_founded.not_lt_min nat.lt_wf S S_nonempty hk,
-- It suffices to show that there is point (a,b) with b ∈ S and b < m.
suffices hp' : ∃ p' : ℕ × ℕ, p'.2 ∈ S ∧ p'.2 < m,
{ rcases hp' with ⟨p', p'_mem, p'_small⟩, solve_by_elim },
-- Let (m_x, m_y) be a point on the upper branch that projects to m ∈ S
-- and that does not lie in the exceptional locus.
rcases m_mem with ⟨⟨mx, my⟩, ⟨⟨hHm, mx_lt_my⟩, h_base⟩, m_eq⟩,
-- This means that m_y = m,
-- and the conditions H(m_x, m_y) and m_x < m_y are satisfied.
simp [exceptional, hHm] at mx_lt_my h_base m_eq,
push_neg at h_base,
-- Finally, it also means that (m_x, m_y) does not lie in the base locus,
-- that m_x ≠ 0, m_x ≠ m_y, B(m_x) ≠ m_y, and B(m_x) ≠ m_x + m_y.
rcases h_base with ⟨h_base, hmx, hm_diag, hm_B₁, hm_B₂⟩,
replace hmx : 0 < mx := nat.pos_iff_ne_zero.mpr hmx,
-- Consider the quadratic equation that (m_x, m_y) satisfies.
have h_quad := hHm, rw H_quad at h_quad,
-- We find the other root of the equation, and Vieta's formulas.
rcases Vieta_formula_quadratic h_quad with ⟨c, h_root, hV₁, hV₂⟩,
-- No we rewrite Vietas formulas a bit, and apply the descent step.
replace hV₁ : c = B mx - my := eq_sub_of_add_eq' hV₁,
rw mul_comm at hV₂,
have Hc := H_desc hmx mx_lt_my h_base hHm c h_root hV₁ hV₂,
-- This means that we may assume that c ≥ 0 and c ≤ m_x.
cases Hc with c_nonneg c_lt,
-- In other words, c is a natural number.
lift c to ℕ using c_nonneg,
-- Recall that we are trying find a point (a,b) such that b ∈ S and b < m.
-- We claim that p' = (c, m_x) does the job.
let p' : ℕ × ℕ := ⟨c, mx⟩,
use p',
-- The second condition is rather easy to check, so we do that first.
split, swap,
{ rwa m_eq at mx_lt_my },
-- Now we need to show that p' projects onto S. In other words, that c ∈ S.
-- We do that, by showing that it lies in the upper branch
-- (which is sufficient, because we assumed that the exceptional locus is empty).
apply set.mem_image_of_mem,
rw [exceptional_empty, set.diff_empty],
-- Now we are ready to prove that p' = (c, m_x) lies on the upper branch.
-- We need to check two conditions: H(c, m_x) and c < m_x.
split; dsimp only,
{ -- The first condition is not so hard. After all, c is the other root of the quadratic equation.
rw [H_symm, H_quad],
simpa using h_root, },
{ -- For the second condition, we note that it suffices to check that c ≠ m_x.
suffices hc : c ≠ mx,
{ refine lt_of_le_of_ne _ hc,
exact_mod_cast c_lt, },
-- However, recall that B(m_x) ≠ m_x + m_y.
-- If c = m_x, we can prove B(m_x) = m_x + m_y.
contrapose! hm_B₂, subst c,
simp [hV₁], }
-- Hence p' = (c, m_x) lies on the upper branch, and we are done.
end
/--Question 6 of IMO1988. If a and b are two natural numbers
such that a*b+1 divides a^2 + b^2, show that their quotient is a perfect square.-/
lemma imo1988_q6 {a b : ℕ} (h : (a*b+1) ∣ a^2 + b^2) :
∃ d, d^2 = (a^2 + b^2)/(a*b + 1) :=
begin
rcases h with ⟨k, hk⟩,
rw [hk, nat.mul_div_cancel_left _ (nat.succ_pos (a*b))],
simp only [pow_two] at hk,
apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x - k) (λ x y, false);
clear hk a b,
{ -- We will now show that the fibers of the solution set are described by a quadratic equation.
intros x y, dsimp only,
rw [← int.coe_nat_inj', ← sub_eq_zero],
apply eq_iff_eq_cancel_right.2,
norm_cast,
simp, ring, },
{ -- Show that the solution set is symmetric in a and b.
intros x y, simp [add_comm (x*x), mul_comm x], },
{ -- Show that the claim is true if b = 0.
suffices : ∀ a, a * a = k → ∃ d, d * d = k, by simpa,
rintros x rfl, use x },
{ -- Show that the claim is true if a = b.
intros x hx,
suffices : k ≤ 1,
{ rw [nat.le_add_one_iff, nat.le_zero_iff] at this,
rcases this with rfl|rfl,
{ use 0, simp },
{ use 1, simp } },
contrapose! hx with k_lt_one,
apply ne_of_lt,
calc x*x + x*x = x*x * 2 : by rw mul_two
... ≤ x*x * k : nat.mul_le_mul_left (x*x) k_lt_one
... < (x*x + 1) * k : by linarith },
{ -- Show the descent step.
intros x y hx x_lt_y hxky h z h_root hV₁ hV₀,
split,
{ dsimp [-sub_eq_add_neg] at *,
have hpos : z*z + x*x > 0,
{ apply add_pos_of_nonneg_of_pos,
{ apply mul_self_nonneg },
{ apply mul_pos; exact_mod_cast hx }, },
have hzx : z*z + x*x = (z * x + 1) * k,
{ rw [← sub_eq_zero, ← h_root],
ring, },
rw hzx at hpos,
replace hpos : z * x + 1 > 0 := pos_of_mul_pos_right hpos (int.coe_zero_le k),
replace hpos : z * x ≥ 0 := int.le_of_lt_add_one hpos,
apply nonneg_of_mul_nonneg_right hpos (by exact_mod_cast hx), },
{ contrapose! hV₀ with x_lt_z,
apply ne_of_gt,
calc z * y > x*x : by apply mul_lt_mul'; linarith
... ≥ x*x - k : sub_le_self _ (int.coe_zero_le k) }, },
{ -- There is no base case in this application of Vieta jumping.
simp },
end
/-
The following example illustrates the use of constant descent Vieta jumping
in the presence of a non-trivial base case.
-/
example {a b : ℕ} (h : a*b ∣ a^2 + b^2 + 1) :
3*a*b = a^2 + b^2 + 1 :=
begin
rcases h with ⟨k, hk⟩,
suffices : k = 3, { simp * at *, ring, },
simp only [pow_two] at hk,
apply constant_descent_vieta_jumping a b hk (λ x, k * x) (λ x, x*x + 1) (λ x y, x ≤ 1);
clear hk a b,
{ -- We will now show that the fibers of the solution set are described by a quadratic equation.
intros x y, dsimp only,
rw [← int.coe_nat_inj', ← sub_eq_zero],
apply eq_iff_eq_cancel_right.2,
simp, ring, },
{ -- Show that the solution set is symmetric in a and b.
cc },
{ -- Show that the claim is true if b = 0.
simp },
{ -- Show that the claim is true if a = b.
intros x hx,
have x_sq_dvd : x*x ∣ x*x*k := dvd_mul_right (x*x) k,
rw ← hx at x_sq_dvd,
obtain ⟨y, hy⟩ : x * x ∣ 1 := by simpa only [nat.dvd_add_self_left, add_assoc] using x_sq_dvd,
obtain ⟨rfl,rfl⟩ : x = 1 ∧ y = 1 := by simpa [nat.mul_eq_one_iff] using hy.symm,
simpa using hx.symm, },
{ -- Show the descent step.
intros x y x_lt_y hx h_base h z h_root hV₁ hV₀,
split,
{ have zy_pos : z * y ≥ 0,
{ rw hV₀, exact_mod_cast (nat.zero_le _) },
apply nonneg_of_mul_nonneg_right zy_pos,
linarith },
{ contrapose! hV₀ with x_lt_z,
apply ne_of_gt,
push_neg at h_base,
calc z * y > x * y : by apply mul_lt_mul_of_pos_right; linarith
... ≥ x * (x + 1) : by apply mul_le_mul; linarith
... > x * x + 1 :
begin
rw [mul_add, mul_one],
apply add_lt_add_left,
assumption_mod_cast
end, } },
{ -- Show the base case.
intros x y h h_base,
obtain rfl|rfl : x = 0 ∨ x = 1 := by rwa [nat.le_add_one_iff, nat.le_zero_iff] at h_base,
{ simpa using h, },
{ simp only [mul_one, one_mul, add_comm, zero_add] at h,
have y_dvd : y ∣ y * k := dvd_mul_right y k,
rw [← h, ← add_assoc, nat.dvd_add_left (dvd_mul_left y y)] at y_dvd,
obtain rfl|rfl : y = 1 ∨ y = 2 := nat.prime_two.2 y y_dvd,
all_goals { ring at h, omega } } }
end
|
ec88ec79ce63bfe1b2f1d8f76fb746eb2be9e832 | 91b8df3b248df89472cc0b753fbe2bac750aefea | /experiments/lean/src/ddl/parsing.lean | e8db34f3faf0f836a5e54e1452d17480178ad31a | [
"Apache-2.0",
"LicenseRef-scancode-unknown-license-reference"
] | permissive | yeslogic/fathom | eabe5c4112d3b4d5ec9096a57bb502254ddbdf15 | 3960a9466150d392c2cb103c5cb5fcffa0200814 | refs/heads/main | 1,685,349,769,736 | 1,675,998,621,000 | 1,675,998,621,000 | 28,993,871 | 214 | 11 | Apache-2.0 | 1,694,044,276,000 | 1,420,764,938,000 | Rust | UTF-8 | Lean | false | false | 218 | lean | import ddl.binary
namespace ddl
-- FIXME: constrain `kt.embed` to be `Type 0`
def parse {ℓ α} [decidable_eq ℓ] : Π (kt : binary.kinded_type ℓ α), list bool → /- kt.embed -/ sorry :=
sorry
end ddl
|
73d55e12d9926681295a9245135a7743a532e63c | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/measure_theory/integral/interval_integral.lean | 1397c54b61a15ebb1c88b54b2affefb061411670 | [
"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 | 137,628 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov, Patrick Massot, Sébastien Gouëzel
-/
import analysis.normed_space.dual
import data.set.intervals.disjoint
import measure_theory.measure.haar_lebesgue
import analysis.calculus.extend_deriv
import measure_theory.function.locally_integrable
import measure_theory.integral.set_integral
import measure_theory.integral.vitali_caratheodory
import analysis.calculus.fderiv_measurable
/-!
# Integral over an interval
In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and
`-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and several versions of the
[fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus).
Recall that its first version states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative
`(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`,
and its second version states that, if `f` has an integrable derivative on `[a, b]`, then
`∫ x in a..b, f' x = f b - f a`.
## Main statements
### FTC-1 for Lebesgue measure
We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow
the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`.
They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`.
Let us explain the meaning of each part of the name:
* `_strict` means that the theorem is about strict differentiability;
* `f` means that the theorem is about differentiability in both endpoints; incompatible with
`_right|_left`;
* `_within` means that the theorem is about one-sided derivatives, see below for details;
* `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit
almost surely as `x` tends to `a` and/or `b`;
* `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left)
endpoint.
We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named
`(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the
name.
### One-sided derivatives
Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a
derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends
to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and
corresponding filters `la`, `lb` are given in the following table.
| `s` | `la` | `t` | `lb` |
| ------- | ---- | --- | ---- |
| `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` |
| `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` |
| `{a}` | `⊥` | `{b}` | `⊥` |
| `univ` | `𝓝 a` | `univ` | `𝓝 b` |
We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way
we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered
by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and
`integral_has_fderiv_at_of_tendsto_ae`). Similarly,
`integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the
same typeclass to find an appropriate filter.
### FTC for a locally finite measure
Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for
any measure. The most general of them,
`measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')`
be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be
an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at
`la'` and `lb'`, respectively, then
`∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ +
o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while
`ub` and `vb` tend to `lb`.
### FTC-2 and corollaries
We use FTC-1 to prove several versions of FTC-2 for the Lebesgue measure, using a similar naming
scheme as for the versions of FTC-1. They include:
* `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` - most general version, for functions
with a right derivative
* `interval_integral.integral_eq_sub_of_has_deriv_at'` - version for functions with a derivative on
an open set
* `interval_integral.integral_deriv_eq_sub'` - version that is easiest to use when computing the
integral of a specific function
We then derive additional integration techniques from FTC-2:
* `interval_integral.integral_mul_deriv_eq_deriv_mul` - integration by parts
* `interval_integral.integral_comp_mul_deriv''` - integration by substitution
Many applications of these theorems can be found in the file `analysis.special_functions.integrals`.
Note that the assumptions of FTC-2 are formulated in the form that `f'` is integrable. To use it in
a context with the stronger assumption that `f'` is continuous, one can use
`continuous_on.interval_integrable` or `continuous_on.integrable_on_Icc` or
`continuous_on.integrable_on_interval`.
## Implementation notes
### Avoiding `if`, `min`, and `max`
In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as
`integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these
intervals is empty and the other coincides with `set.interval_oc a b = set.Ioc (min a b) (max a b)`.
Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`.
Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result.
This way some properties can be translated from integrals over sets without dealing with
the cases `a ≤ b` and `b ≤ a` separately.
### Choice of the interval
We use integral over `set.interval_oc a b = set.Ioc (min a b) (max a b)` instead of one of the other
three possible intervals with the same endpoints for two reasons:
* this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever
`f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom
at `b`; this rules out `set.Ioo` and `set.Icc` intervals;
* with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals
the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the
[cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)
of `μ`.
### `FTC_filter` class
As explained above, many theorems in this file rely on the typeclass
`FTC_filter (a : ℝ) (l l' : filter ℝ)` to avoid code duplication. This typeclass combines four
assumptions:
- `pure a ≤ l`;
- `l' ≤ 𝓝 a`;
- `l'` has a basis of measurable sets;
- if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included
in `s`.
This typeclass has the following “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[≥] a, 𝓝[>] a)`,
`(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`.
Furthermore, we have the following instances that are equal to the previously mentioned instances:
`(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`.
While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure,
it becomes important in the versions of FTC about any locally finite measure if this measure has an
atom at one of the endpoints.
### Combining one-sided and two-sided derivatives
There are some `FTC_filter` instances where the fact that it is one-sided or
two-sided depends on the point, namely `(x, 𝓝[Icc a b] x, 𝓝[Icc a b] x)`
(resp. `(x, 𝓝[[a, b]] x, 𝓝[[a, b]] x)`, where `[a, b] = set.interval a b`),
with `x ∈ Icc a b` (resp. `x ∈ [a, b]`).
This results in a two-sided derivatives for `x ∈ Ioo a b` and one-sided derivatives for
`x ∈ {a, b}`. Other instances could be added when needed (in that case, one also needs to add
instances for `filter.is_measurably_generated` and `filter.tendsto_Ixx_class`).
## Tags
integral, fundamental theorem of calculus, FTC-1, FTC-2, change of variables in integrals
-/
noncomputable theory
open topological_space (second_countable_topology)
open measure_theory set classical filter function
open_locale classical topological_space filter ennreal big_operators interval nnreal
variables {ι 𝕜 E F : Type*} [normed_add_comm_group E]
/-!
### Integrability at an interval
-/
/-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered
interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these
intervals is always empty, so this property is equivalent to `f` being integrable on
`(min a b, max a b]`. -/
def interval_integrable (f : ℝ → E) (μ : measure ℝ) (a b : ℝ) :=
integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ
section
variables {f : ℝ → E} {a b : ℝ} {μ : measure ℝ}
/-- A function is interval integrable with respect to a given measure `μ` on `a..b` if and
only if it is integrable on `interval_oc a b` with respect to `μ`. This is an equivalent
definition of `interval_integrable`. -/
lemma interval_integrable_iff : interval_integrable f μ a b ↔ integrable_on f (Ι a b) μ :=
by rw [interval_oc_eq_union, integrable_on_union, interval_integrable]
/-- If a function is interval integrable with respect to a given measure `μ` on `a..b` then
it is integrable on `interval_oc a b` with respect to `μ`. -/
lemma interval_integrable.def (h : interval_integrable f μ a b) : integrable_on f (Ι a b) μ :=
interval_integrable_iff.mp h
lemma interval_integrable_iff_integrable_Ioc_of_le (hab : a ≤ b) :
interval_integrable f μ a b ↔ integrable_on f (Ioc a b) μ :=
by rw [interval_integrable_iff, interval_oc_of_le hab]
lemma integrable_on_Icc_iff_integrable_on_Ioc'
{E : Type*} [normed_add_comm_group E] {f : ℝ → E} (ha : μ {a} ≠ ∞) :
integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ :=
begin
cases le_or_lt a b with hab hab,
{ have : Icc a b = Icc a a ∪ Ioc a b := (Icc_union_Ioc_eq_Icc le_rfl hab).symm,
rw [this, integrable_on_union],
simp [ha.lt_top] },
{ simp [hab, hab.le] },
end
lemma integrable_on_Icc_iff_integrable_on_Ioc
{E : Type*}[normed_add_comm_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} :
integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ :=
integrable_on_Icc_iff_integrable_on_Ioc' (by simp)
lemma integrable_on_Ioc_iff_integrable_on_Ioo'
{E : Type*} [normed_add_comm_group E]
{f : ℝ → E} {a b : ℝ} (hb : μ {b} ≠ ∞) :
integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ :=
begin
cases lt_or_le a b with hab hab,
{ have : Ioc a b = Ioo a b ∪ Icc b b := (Ioo_union_Icc_eq_Ioc hab le_rfl).symm,
rw [this, integrable_on_union],
simp [hb.lt_top] },
{ simp [hab] },
end
lemma integrable_on_Ioc_iff_integrable_on_Ioo
{E : Type*} [normed_add_comm_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} :
integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ :=
integrable_on_Ioc_iff_integrable_on_Ioo' (by simp)
lemma integrable_on_Icc_iff_integrable_on_Ioo
{E : Type*} [normed_add_comm_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} :
integrable_on f (Icc a b) μ ↔ integrable_on f (Ioo a b) μ :=
by rw [integrable_on_Icc_iff_integrable_on_Ioc, integrable_on_Ioc_iff_integrable_on_Ioo]
lemma interval_integrable_iff' [has_no_atoms μ] :
interval_integrable f μ a b ↔ integrable_on f (interval a b) μ :=
by rw [interval_integrable_iff, interval, interval_oc, integrable_on_Icc_iff_integrable_on_Ioc]
lemma interval_integrable_iff_integrable_Icc_of_le {E : Type*} [normed_add_comm_group E]
{f : ℝ → E} {a b : ℝ} (hab : a ≤ b) {μ : measure ℝ} [has_no_atoms μ] :
interval_integrable f μ a b ↔ integrable_on f (Icc a b) μ :=
by rw [interval_integrable_iff_integrable_Ioc_of_le hab, integrable_on_Icc_iff_integrable_on_Ioc]
lemma integrable_on_Ici_iff_integrable_on_Ioi'
{E : Type*} [normed_add_comm_group E] {f : ℝ → E} (ha : μ {a} ≠ ∞) :
integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ :=
begin
have : Ici a = Icc a a ∪ Ioi a := (Icc_union_Ioi_eq_Ici le_rfl).symm,
rw [this, integrable_on_union],
simp [ha.lt_top]
end
lemma integrable_on_Ici_iff_integrable_on_Ioi
{E : Type*} [normed_add_comm_group E] [has_no_atoms μ] {f : ℝ → E} :
integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ :=
integrable_on_Ici_iff_integrable_on_Ioi' (by simp)
/-- If a function is integrable with respect to a given measure `μ` then it is interval integrable
with respect to `μ` on `interval a b`. -/
lemma measure_theory.integrable.interval_integrable (hf : integrable f μ) :
interval_integrable f μ a b :=
⟨hf.integrable_on, hf.integrable_on⟩
lemma measure_theory.integrable_on.interval_integrable (hf : integrable_on f [a, b] μ) :
interval_integrable f μ a b :=
⟨measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_interval),
measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_interval')⟩
lemma interval_integrable_const_iff {c : E} :
interval_integrable (λ _, c) μ a b ↔ c = 0 ∨ μ (Ι a b) < ∞ :=
by simp only [interval_integrable_iff, integrable_on_const]
@[simp] lemma interval_integrable_const [is_locally_finite_measure μ] {c : E} :
interval_integrable (λ _, c) μ a b :=
interval_integrable_const_iff.2 $ or.inr measure_Ioc_lt_top
end
namespace interval_integrable
section
variables {f : ℝ → E} {a b c d : ℝ} {μ ν : measure ℝ}
@[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a :=
h.symm
@[refl] lemma refl : interval_integrable f μ a a :=
by split; simp
@[trans] lemma trans {a b c : ℝ} (hab : interval_integrable f μ a b)
(hbc : interval_integrable f μ b c) : interval_integrable f μ a c :=
⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc,
(hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩
lemma trans_iterate_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n)
(hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) :
interval_integrable f μ (a m) (a n) :=
begin
revert hint,
refine nat.le_induction _ _ n hmn,
{ simp },
{ assume p hp IH h,
exact (IH (λ k hk, h k (Ico_subset_Ico_right p.le_succ hk))).trans (h p (by simp [hp])) }
end
lemma trans_iterate {a : ℕ → ℝ} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) :
interval_integrable f μ (a 0) (a n) :=
trans_iterate_Ico bot_le (λ k hk, hint k hk.2)
lemma neg (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b :=
⟨h.1.neg, h.2.neg⟩
lemma norm (h : interval_integrable f μ a b) :
interval_integrable (λ x, ∥f x∥) μ a b :=
⟨h.1.norm, h.2.norm⟩
lemma abs {f : ℝ → ℝ} (h : interval_integrable f μ a b) :
interval_integrable (λ x, |f x|) μ a b :=
h.norm
lemma mono (hf : interval_integrable f ν a b) (h1 : [c, d] ⊆ [a, b]) (h2 : μ ≤ ν) :
interval_integrable f μ c d :=
interval_integrable_iff.mpr $ hf.def.mono
(interval_oc_subset_interval_oc_of_interval_subset_interval h1) h2
lemma mono_set (hf : interval_integrable f μ a b) (h : [c, d] ⊆ [a, b]) :
interval_integrable f μ c d :=
hf.mono h rfl.le
lemma mono_measure (hf : interval_integrable f ν a b) (h : μ ≤ ν) :
interval_integrable f μ a b :=
hf.mono rfl.subset h
lemma mono_set_ae (hf : interval_integrable f μ a b) (h : Ι c d ≤ᵐ[μ] Ι a b) :
interval_integrable f μ c d :=
interval_integrable_iff.mpr $ hf.def.mono_set_ae h
lemma mono_fun [normed_add_comm_group F] {g : ℝ → F}
(hf : interval_integrable f μ a b) (hgm : ae_strongly_measurable g (μ.restrict (Ι a b)))
(hle : (λ x, ∥g x∥) ≤ᵐ[μ.restrict (Ι a b)] (λ x, ∥f x∥)) : interval_integrable g μ a b :=
interval_integrable_iff.2 $ hf.def.integrable.mono hgm hle
lemma mono_fun' {g : ℝ → ℝ} (hg : interval_integrable g μ a b)
(hfm : ae_strongly_measurable f (μ.restrict (Ι a b)))
(hle : (λ x, ∥f x∥) ≤ᵐ[μ.restrict (Ι a b)] g) : interval_integrable f μ a b :=
interval_integrable_iff.2 $ hg.def.integrable.mono' hfm hle
protected lemma ae_strongly_measurable (h : interval_integrable f μ a b) :
ae_strongly_measurable f (μ.restrict (Ioc a b)):=
h.1.ae_strongly_measurable
protected lemma ae_strongly_measurable' (h : interval_integrable f μ a b) :
ae_strongly_measurable f (μ.restrict (Ioc b a)):=
h.2.ae_strongly_measurable
end
variables {f g : ℝ → E} {a b : ℝ} {μ : measure ℝ}
lemma smul [normed_field 𝕜] [normed_space 𝕜 E]
{f : ℝ → E} {a b : ℝ} {μ : measure ℝ} (h : interval_integrable f μ a b) (r : 𝕜) :
interval_integrable (r • f) μ a b :=
⟨h.1.smul r, h.2.smul r⟩
@[simp] lemma add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) :
interval_integrable (λ x, f x + g x) μ a b :=
⟨hf.1.add hg.1, hf.2.add hg.2⟩
@[simp] lemma sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) :
interval_integrable (λ x, f x - g x) μ a b :=
⟨hf.1.sub hg.1, hf.2.sub hg.2⟩
lemma sum (s : finset ι) {f : ι → ℝ → E} (h : ∀ i ∈ s, interval_integrable (f i) μ a b) :
interval_integrable (∑ i in s, f i) μ a b :=
⟨integrable_finset_sum' s (λ i hi, (h i hi).1), integrable_finset_sum' s (λ i hi, (h i hi).2)⟩
lemma mul_continuous_on {f g : ℝ → ℝ}
(hf : interval_integrable f μ a b) (hg : continuous_on g [a, b]) :
interval_integrable (λ x, f x * g x) μ a b :=
begin
rw interval_integrable_iff at hf ⊢,
exact hf.mul_continuous_on_of_subset hg measurable_set_Ioc is_compact_interval Ioc_subset_Icc_self
end
lemma continuous_on_mul {f g : ℝ → ℝ} (hf : interval_integrable f μ a b)
(hg : continuous_on g [a, b]) :
interval_integrable (λ x, g x * f x) μ a b :=
by simpa [mul_comm] using hf.mul_continuous_on hg
lemma comp_mul_left (hf : interval_integrable f volume a b) (c : ℝ) :
interval_integrable (λ x, f (c * x)) volume (a / c) (b / c) :=
begin
rcases eq_or_ne c 0 with hc|hc, { rw hc, simp },
rw interval_integrable_iff' at hf ⊢,
have A : measurable_embedding (λ x, x * c⁻¹) :=
(homeomorph.mul_right₀ _ (inv_ne_zero hc)).closed_embedding.measurable_embedding,
rw [←real.smul_map_volume_mul_right (inv_ne_zero hc), integrable_on, measure.restrict_smul,
integrable_smul_measure (by simpa : ennreal.of_real (|c⁻¹|) ≠ 0) ennreal.of_real_ne_top,
←integrable_on, measurable_embedding.integrable_on_map_iff A],
convert hf using 1,
{ ext, simp only [comp_app], congr' 1, field_simp, ring },
{ rw preimage_mul_const_interval (inv_ne_zero hc), field_simp [hc] },
end
lemma iff_comp_neg :
interval_integrable f volume a b ↔ interval_integrable (λ x, f (-x)) volume (-a) (-b) :=
begin
split, all_goals { intro hf, convert comp_mul_left hf (-1), simp, field_simp, field_simp },
end
end interval_integrable
section
variables {μ : measure ℝ} [is_locally_finite_measure μ]
lemma continuous_on.interval_integrable {u : ℝ → E} {a b : ℝ}
(hu : continuous_on u (interval a b)) : interval_integrable u μ a b :=
(continuous_on.integrable_on_Icc hu).interval_integrable
lemma continuous_on.interval_integrable_of_Icc {u : ℝ → E} {a b : ℝ} (h : a ≤ b)
(hu : continuous_on u (Icc a b)) : interval_integrable u μ a b :=
continuous_on.interval_integrable ((interval_of_le h).symm ▸ hu)
/-- A continuous function on `ℝ` is `interval_integrable` with respect to any locally finite measure
`ν` on ℝ. -/
lemma continuous.interval_integrable {u : ℝ → E} (hu : continuous u) (a b : ℝ) :
interval_integrable u μ a b :=
hu.continuous_on.interval_integrable
end
section
variables {μ : measure ℝ} [is_locally_finite_measure μ] [conditionally_complete_linear_order E]
[order_topology E] [second_countable_topology E]
lemma monotone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone_on u (interval a b)) :
interval_integrable u μ a b :=
begin
rw interval_integrable_iff,
exact (hu.integrable_on_compact is_compact_interval).mono_set Ioc_subset_Icc_self,
end
lemma antitone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone_on u (interval a b)) :
interval_integrable u μ a b :=
hu.dual_right.interval_integrable
lemma monotone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone u) :
interval_integrable u μ a b :=
(hu.monotone_on _).interval_integrable
lemma antitone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone u) :
interval_integrable u μ a b :=
(hu.antitone_on _).interval_integrable
end
/-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'`
eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`.
Suppose that `f : ℝ → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on
`u..v` provided that both `u` and `v` tend to `l`.
Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so
`apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and
`tendsto_Ixx_class Ioc ?m_1 l'`. -/
lemma filter.tendsto.eventually_interval_integrable_ae {f : ℝ → E} {μ : measure ℝ}
{l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ)
[tendsto_Ixx_class Ioc l l'] [is_measurably_generated l']
(hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
{u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) :
∀ᶠ t in lt, interval_integrable f μ (u t) (v t) :=
have _ := (hf.integrable_at_filter_ae hfm hμ).eventually,
((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this
/-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'`
eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`.
Suppose that `f : ℝ → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v`
provided that both `u` and `v` tend to `l`.
Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so
`apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and
`tendsto_Ixx_class Ioc ?m_1 l'`. -/
lemma filter.tendsto.eventually_interval_integrable {f : ℝ → E} {μ : measure ℝ}
{l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ)
[tendsto_Ixx_class Ioc l l'] [is_measurably_generated l']
(hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c))
{u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) :
∀ᶠ t in lt, interval_integrable f μ (u t) (v t) :=
(hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv
/-!
### Interval integral: definition and basic properties
In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`
and prove some basic properties.
-/
variables [complete_space E] [normed_space ℝ E]
/-- The interval integral `∫ x in a..b, f x ∂μ` is defined
as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals
`∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/
def interval_integral (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) :=
∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ
notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ
notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r
namespace interval_integral
section basic
variables {a b : ℝ} {f g : ℝ → E} {μ : measure ℝ}
@[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 :=
by simp [interval_integral]
lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ :=
by simp [interval_integral, h]
@[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 :=
sub_self _
lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ :=
by simp only [interval_integral, neg_sub]
lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ :=
by simp only [integral_symm b, integral_of_le h]
lemma interval_integral_eq_integral_interval_oc (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) :
∫ x in a..b, f x ∂μ = (if a ≤ b then 1 else -1 : ℝ) • ∫ x in Ι a b, f x ∂μ :=
begin
split_ifs with h,
{ simp only [integral_of_le h, interval_oc_of_le h, one_smul] },
{ simp only [integral_of_ge (not_le.1 h).le, interval_oc_of_lt (not_le.1 h), neg_one_smul] }
end
lemma integral_cases (f : ℝ → E) (a b) :
∫ x in a..b, f x ∂μ ∈ ({∫ x in Ι a b, f x ∂μ, -∫ x in Ι a b, f x ∂μ} : set E) :=
by { rw interval_integral_eq_integral_interval_oc, split_ifs; simp }
lemma integral_undef (h : ¬ interval_integrable f μ a b) :
∫ x in a..b, f x ∂μ = 0 :=
by cases le_total a b with hab hab;
simp only [integral_of_le, integral_of_ge, hab, neg_eq_zero];
refine integral_undef (not_imp_not.mpr integrable.integrable_on' _);
simpa [hab] using not_and_distrib.mp h
lemma integral_non_ae_strongly_measurable
(hf : ¬ ae_strongly_measurable f (μ.restrict (Ι a b))) :
∫ x in a..b, f x ∂μ = 0 :=
by rw [interval_integral_eq_integral_interval_oc, integral_non_ae_strongly_measurable hf, smul_zero]
lemma integral_non_ae_strongly_measurable_of_le (h : a ≤ b)
(hf : ¬ ae_strongly_measurable f (μ.restrict (Ioc a b))) :
∫ x in a..b, f x ∂μ = 0 :=
integral_non_ae_strongly_measurable $ by rwa [interval_oc_of_le h]
lemma norm_integral_min_max (f : ℝ → E) :
∥∫ x in min a b..max a b, f x ∂μ∥ = ∥∫ x in a..b, f x ∂μ∥ :=
by cases le_total a b; simp [*, integral_symm a b]
lemma norm_integral_eq_norm_integral_Ioc (f : ℝ → E) :
∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ι a b, f x ∂μ∥ :=
by rw [← norm_integral_min_max, integral_of_le min_le_max, interval_oc]
lemma abs_integral_eq_abs_integral_interval_oc (f : ℝ → ℝ) :
|∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| :=
norm_integral_eq_norm_integral_Ioc f
lemma norm_integral_le_integral_norm_Ioc :
∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in Ι a b, ∥f x∥ ∂μ :=
calc ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ι a b, f x ∂μ∥ :
norm_integral_eq_norm_integral_Ioc f
... ≤ ∫ x in Ι a b, ∥f x∥ ∂μ :
norm_integral_le_integral_norm f
lemma norm_integral_le_abs_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ |∫ x in a..b, ∥f x∥ ∂μ| :=
begin
simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc],
exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _)
end
lemma norm_integral_le_integral_norm (h : a ≤ b) :
∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in a..b, ∥f x∥ ∂μ :=
norm_integral_le_integral_norm_Ioc.trans_eq $ by rw [interval_oc_of_le h, integral_of_le h]
lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E}
(h : ∀ᵐ x, x ∈ Ι a b → ∥f x∥ ≤ C) :
∥∫ x in a..b, f x∥ ≤ C * |b - a| :=
begin
rw [norm_integral_eq_norm_integral_Ioc],
convert norm_set_integral_le_of_norm_le_const_ae'' _ measurable_set_Ioc h,
{ rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] },
{ simp only [real.volume_Ioc, ennreal.of_real_lt_top] },
end
lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E}
(h : ∀ x ∈ Ι a b, ∥f x∥ ≤ C) :
∥∫ x in a..b, f x∥ ≤ C * |b - a| :=
norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h
@[simp] lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) :
∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ :=
by simp only [interval_integral_eq_integral_interval_oc, integral_add hf.def hg.def, smul_add]
lemma integral_finset_sum {ι} {s : finset ι} {f : ι → ℝ → E}
(h : ∀ i ∈ s, interval_integrable (f i) μ a b) :
∫ x in a..b, ∑ i in s, f i x ∂μ = ∑ i in s, ∫ x in a..b, f i x ∂μ :=
by simp only [interval_integral_eq_integral_interval_oc,
integral_finset_sum s (λ i hi, (h i hi).def), finset.smul_sum]
@[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ :=
by { simp only [interval_integral, integral_neg], abel }
@[simp] lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) :
∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ :=
by simpa only [sub_eq_add_neg] using (integral_add hf hg.neg).trans (congr_arg _ integral_neg)
@[simp] lemma integral_smul {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E]
[smul_comm_class ℝ 𝕜 E]
(r : 𝕜) (f : ℝ → E) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ :=
by simp only [interval_integral, integral_smul, smul_sub]
@[simp] lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : ℝ → 𝕜) (c : E) :
∫ x in a..b, f x • c ∂μ = (∫ x in a..b, f x ∂μ) • c :=
by simp only [interval_integral_eq_integral_interval_oc, integral_smul_const, smul_assoc]
@[simp] lemma integral_const_mul {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) :
∫ x in a..b, r * f x ∂μ = r * ∫ x in a..b, f x ∂μ :=
integral_smul r f
@[simp] lemma integral_mul_const {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) :
∫ x in a..b, f x * r ∂μ = ∫ x in a..b, f x ∂μ * r :=
by simpa only [mul_comm r] using integral_const_mul r f
@[simp] lemma integral_div {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) :
∫ x in a..b, f x / r ∂μ = ∫ x in a..b, f x ∂μ / r :=
by simpa only [div_eq_mul_inv] using integral_mul_const r⁻¹ f
lemma integral_const' (c : E) :
∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c :=
by simp only [interval_integral, set_integral_const, sub_smul]
@[simp] lemma integral_const (c : E) : ∫ x in a..b, c = (b - a) • c :=
by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b,
max_zero_sub_eq_self]
lemma integral_smul_measure (c : ℝ≥0∞) :
∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ :=
by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub]
variables [normed_add_comm_group F] [complete_space F] [normed_space ℝ F]
lemma _root_.continuous_linear_map.interval_integral_comp_comm
(L : E →L[ℝ] F) (hf : interval_integrable f μ a b) :
∫ x in a..b, L (f x) ∂μ = L (∫ x in a..b, f x ∂μ) :=
begin
rw [interval_integral, interval_integral, L.integral_comp_comm, L.integral_comp_comm, L.map_sub],
exacts [hf.2, hf.1]
end
end basic
section comp
variables {a b c d : ℝ} (f : ℝ → E)
@[simp] lemma integral_comp_mul_right (hc : c ≠ 0) :
∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x :=
begin
have A : measurable_embedding (λ x, x * c) :=
(homeomorph.mul_right₀ c hc).closed_embedding.measurable_embedding,
conv_rhs { rw [← real.smul_map_volume_mul_right hc] },
simp_rw [integral_smul_measure, interval_integral, A.set_integral_map,
ennreal.to_real_of_real (abs_nonneg c)],
cases hc.lt_or_lt,
{ simp [h, mul_div_cancel, hc, abs_of_neg, measure.restrict_congr_set Ico_ae_eq_Ioc] },
{ simp [h, mul_div_cancel, hc, abs_of_pos] }
end
@[simp] lemma smul_integral_comp_mul_right (c) :
c • ∫ x in a..b, f (x * c) = ∫ x in a*c..b*c, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_mul_left (hc : c ≠ 0) :
∫ x in a..b, f (c * x) = c⁻¹ • ∫ x in c*a..c*b, f x :=
by simpa only [mul_comm c] using integral_comp_mul_right f hc
@[simp] lemma smul_integral_comp_mul_left (c) :
c • ∫ x in a..b, f (c * x) = ∫ x in c*a..c*b, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_div (hc : c ≠ 0) :
∫ x in a..b, f (x / c) = c • ∫ x in a/c..b/c, f x :=
by simpa only [inv_inv] using integral_comp_mul_right f (inv_ne_zero hc)
@[simp] lemma inv_smul_integral_comp_div (c) :
c⁻¹ • ∫ x in a..b, f (x / c) = ∫ x in a/c..b/c, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_add_right (d) :
∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x :=
have A : measurable_embedding (λ x, x + d) :=
(homeomorph.add_right d).closed_embedding.measurable_embedding,
calc ∫ x in a..b, f (x + d)
= ∫ x in a+d..b+d, f x ∂(measure.map (λ x, x + d) volume)
: by simp [interval_integral, A.set_integral_map]
... = ∫ x in a+d..b+d, f x : by rw [map_add_right_eq_self]
@[simp] lemma integral_comp_add_left (d) :
∫ x in a..b, f (d + x) = ∫ x in d+a..d+b, f x :=
by simpa only [add_comm] using integral_comp_add_right f d
@[simp] lemma integral_comp_mul_add (hc : c ≠ 0) (d) :
∫ x in a..b, f (c * x + d) = c⁻¹ • ∫ x in c*a+d..c*b+d, f x :=
by rw [← integral_comp_add_right, ← integral_comp_mul_left _ hc]
@[simp] lemma smul_integral_comp_mul_add (c d) :
c • ∫ x in a..b, f (c * x + d) = ∫ x in c*a+d..c*b+d, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_add_mul (hc : c ≠ 0) (d) :
∫ x in a..b, f (d + c * x) = c⁻¹ • ∫ x in d+c*a..d+c*b, f x :=
by rw [← integral_comp_add_left, ← integral_comp_mul_left _ hc]
@[simp] lemma smul_integral_comp_add_mul (c d) :
c • ∫ x in a..b, f (d + c * x) = ∫ x in d+c*a..d+c*b, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_div_add (hc : c ≠ 0) (d) :
∫ x in a..b, f (x / c + d) = c • ∫ x in a/c+d..b/c+d, f x :=
by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_add f (inv_ne_zero hc) d
@[simp] lemma inv_smul_integral_comp_div_add (c d) :
c⁻¹ • ∫ x in a..b, f (x / c + d) = ∫ x in a/c+d..b/c+d, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_add_div (hc : c ≠ 0) (d) :
∫ x in a..b, f (d + x / c) = c • ∫ x in d+a/c..d+b/c, f x :=
by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_add_mul f (inv_ne_zero hc) d
@[simp] lemma inv_smul_integral_comp_add_div (c d) :
c⁻¹ • ∫ x in a..b, f (d + x / c) = ∫ x in d+a/c..d+b/c, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_mul_sub (hc : c ≠ 0) (d) :
∫ x in a..b, f (c * x - d) = c⁻¹ • ∫ x in c*a-d..c*b-d, f x :=
by simpa only [sub_eq_add_neg] using integral_comp_mul_add f hc (-d)
@[simp] lemma smul_integral_comp_mul_sub (c d) :
c • ∫ x in a..b, f (c * x - d) = ∫ x in c*a-d..c*b-d, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_sub_mul (hc : c ≠ 0) (d) :
∫ x in a..b, f (d - c * x) = c⁻¹ • ∫ x in d-c*b..d-c*a, f x :=
begin
simp only [sub_eq_add_neg, neg_mul_eq_neg_mul],
rw [integral_comp_add_mul f (neg_ne_zero.mpr hc) d, integral_symm],
simp only [inv_neg, smul_neg, neg_neg, neg_smul],
end
@[simp] lemma smul_integral_comp_sub_mul (c d) :
c • ∫ x in a..b, f (d - c * x) = ∫ x in d-c*b..d-c*a, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_div_sub (hc : c ≠ 0) (d) :
∫ x in a..b, f (x / c - d) = c • ∫ x in a/c-d..b/c-d, f x :=
by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_sub f (inv_ne_zero hc) d
@[simp] lemma inv_smul_integral_comp_div_sub (c d) :
c⁻¹ • ∫ x in a..b, f (x / c - d) = ∫ x in a/c-d..b/c-d, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_sub_div (hc : c ≠ 0) (d) :
∫ x in a..b, f (d - x / c) = c • ∫ x in d-b/c..d-a/c, f x :=
by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_sub_mul f (inv_ne_zero hc) d
@[simp] lemma inv_smul_integral_comp_sub_div (c d) :
c⁻¹ • ∫ x in a..b, f (d - x / c) = ∫ x in d-b/c..d-a/c, f x :=
by by_cases hc : c = 0; simp [hc]
@[simp] lemma integral_comp_sub_right (d) :
∫ x in a..b, f (x - d) = ∫ x in a-d..b-d, f x :=
by simpa only [sub_eq_add_neg] using integral_comp_add_right f (-d)
@[simp] lemma integral_comp_sub_left (d) :
∫ x in a..b, f (d - x) = ∫ x in d-b..d-a, f x :=
by simpa only [one_mul, one_smul, inv_one] using integral_comp_sub_mul f one_ne_zero d
@[simp] lemma integral_comp_neg : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x :=
by simpa only [zero_sub] using integral_comp_sub_left f 0
end comp
/-!
### Integral is an additive function of the interval
In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ`
as well as a few other identities trivially equivalent to this one. We also prove that
`∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`.
-/
section order_closed_topology
variables {a b c d : ℝ} {f g : ℝ → E} {μ : measure ℝ}
/-- If two functions are equal in the relevant interval, their interval integrals are also equal. -/
lemma integral_congr {a b : ℝ} (h : eq_on f g [a, b]) :
∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ :=
by cases le_total a b with hab hab; simpa [hab, integral_of_le, integral_of_ge]
using set_integral_congr measurable_set_Ioc (h.mono Ioc_subset_Icc_self)
lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b)
(hbc : interval_integrable f μ b c) :
∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 :=
begin
have hac := hab.trans hbc,
simp only [interval_integral, sub_add_sub_comm, sub_eq_zero],
iterate 4 { rw ← integral_union },
{ suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this,
rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle,
min_left_comm, max_left_comm] },
all_goals { simp [*, measurable_set.union, measurable_set_Ioc, Ioc_disjoint_Ioc_same,
Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] }
end
lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b)
(hbc : interval_integrable f μ b c) :
∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ :=
by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc]
lemma sum_integral_adjacent_intervals_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n)
(hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) :
∑ (k : ℕ) in finset.Ico m n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a m)..(a n), f x ∂μ :=
begin
revert hint,
refine nat.le_induction _ _ n hmn,
{ simp },
{ assume p hmp IH h,
rw [finset.sum_Ico_succ_top hmp, IH, integral_add_adjacent_intervals],
{ apply interval_integrable.trans_iterate_Ico hmp (λ k hk, h k _),
exact (Ico_subset_Ico le_rfl (nat.le_succ _)) hk },
{ apply h,
simp [hmp] },
{ assume k hk,
exact h _ (Ico_subset_Ico_right p.le_succ hk) } }
end
lemma sum_integral_adjacent_intervals {a : ℕ → ℝ} {n : ℕ}
(hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) :
∑ (k : ℕ) in finset.range n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a 0)..(a n), f x ∂μ :=
begin
rw ← nat.Ico_zero_eq_range,
exact sum_integral_adjacent_intervals_Ico (zero_le n) (λ k hk, hint k hk.2),
end
lemma integral_interval_sub_left (hab : interval_integrable f μ a b)
(hac : interval_integrable f μ a c) :
∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ :=
sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab)
lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b)
(hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) :
∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ :=
by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm,
integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm]
lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b)
(hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) :
∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ :=
by simp only [sub_eq_add_neg, ← integral_symm,
integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)]
lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b)
(hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) :
∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ :=
by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c,
sub_neg_eq_add, sub_eq_neg_add], }
lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) :
∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ :=
begin
wlog hab : a ≤ b using [a b] tactic.skip,
{ rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc le_rfl),
Iic_union_Ioc_eq_Iic hab],
exacts [measurable_set_Ioc, ha, hb.mono_set (λ _, and.right)] },
{ intros ha hb,
rw [integral_symm, ← this hb ha, neg_sub] }
end
/-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/
lemma integral_const_of_cdf [is_finite_measure μ] (c : E) :
∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c :=
begin
simp only [sub_smul, ← set_integral_const],
refine (integral_Iic_sub_Iic _ _).symm;
simp only [integrable_on_const, measure_lt_top, or_true]
end
lemma integral_eq_integral_of_support_subset {a b} (h : support f ⊆ Ioc a b) :
∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ :=
begin
cases le_total a b with hab hab,
{ rw [integral_of_le hab, ← integral_indicator measurable_set_Ioc, indicator_eq_self.2 h];
apply_instance },
{ rw [Ioc_eq_empty hab.not_lt, subset_empty_iff, support_eq_empty_iff] at h,
simp [h] }
end
lemma integral_congr_ae' (h : ∀ᵐ x ∂μ, x ∈ Ioc a b → f x = g x)
(h' : ∀ᵐ x ∂μ, x ∈ Ioc b a → f x = g x) :
∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ :=
by simp only [interval_integral, set_integral_congr_ae (measurable_set_Ioc) h,
set_integral_congr_ae (measurable_set_Ioc) h']
lemma integral_congr_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = g x) :
∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ :=
integral_congr_ae' (ae_interval_oc_iff.mp h).1 (ae_interval_oc_iff.mp h).2
lemma integral_zero_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = 0) :
∫ x in a..b, f x ∂μ = 0 :=
calc ∫ x in a..b, f x ∂μ = ∫ x in a..b, 0 ∂μ : integral_congr_ae h
... = 0 : integral_zero
lemma integral_indicator {a₁ a₂ a₃ : ℝ} (h : a₂ ∈ Icc a₁ a₃) :
∫ x in a₁..a₃, indicator {x | x ≤ a₂} f x ∂ μ = ∫ x in a₁..a₂, f x ∂ μ :=
begin
have : {x | x ≤ a₂} ∩ Ioc a₁ a₃ = Ioc a₁ a₂, from Iic_inter_Ioc_of_le h.2,
rw [integral_of_le h.1, integral_of_le (h.1.trans h.2),
integral_indicator, measure.restrict_restrict, this],
exact measurable_set_Iic,
all_goals { apply measurable_set_Iic },
end
/-- Lebesgue dominated convergence theorem for filters with a countable basis -/
lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι}
[l.is_countably_generated] {F : ι → ℝ → E} (bound : ℝ → ℝ)
(hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) (μ.restrict (Ι a b)))
(h_bound : ∀ᶠ n in l, ∀ᵐ x ∂μ, x ∈ Ι a b → ∥F n x∥ ≤ bound x)
(bound_integrable : interval_integrable bound μ a b)
(h_lim : ∀ᵐ x ∂μ, x ∈ Ι a b → tendsto (λ n, F n x) l (𝓝 (f x))) :
tendsto (λn, ∫ x in a..b, F n x ∂μ) l (𝓝 $ ∫ x in a..b, f x ∂μ) :=
begin
simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc,
← ae_restrict_iff' measurable_set_interval_oc] at *,
exact tendsto_const_nhds.smul
(tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_lim)
end
/-- Lebesgue dominated convergence theorem for series. -/
lemma has_sum_integral_of_dominated_convergence {ι} [countable ι]
{F : ι → ℝ → E} (bound : ι → ℝ → ℝ)
(hF_meas : ∀ n, ae_strongly_measurable (F n) (μ.restrict (Ι a b)))
(h_bound : ∀ n, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F n t∥ ≤ bound n t)
(bound_summable : ∀ᵐ t ∂μ, t ∈ Ι a b → summable (λ n, bound n t))
(bound_integrable : interval_integrable (λ t, ∑' n, bound n t) μ a b)
(h_lim : ∀ᵐ t ∂μ, t ∈ Ι a b → has_sum (λ n, F n t) (f t)) :
has_sum (λn, ∫ t in a..b, F n t ∂μ) (∫ t in a..b, f t ∂μ) :=
begin
simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc,
← ae_restrict_iff' measurable_set_interval_oc] at *,
exact (has_sum_integral_of_dominated_convergence bound hF_meas h_bound bound_summable
bound_integrable h_lim).const_smul
end
open topological_space
variables {X : Type*} [topological_space X] [first_countable_topology X]
/-- Continuity of interval integral with respect to a parameter, at a point within a set.
Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a
neighborhood of `x₀` within `s` and at `x₀`, and assume it is bounded by a function integrable
on `[a, b]` independent of `x` in a neighborhood of `x₀` within `s`. If `(λ x, F x t)`
is continuous at `x₀` within `s` for almost every `t` in `[a, b]`
then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/
lemma continuous_within_at_of_dominated_interval
{F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ} {s : set X}
(hF_meas : ∀ᶠ x in 𝓝[s] x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b))
(h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t)
(bound_integrable : interval_integrable bound μ a b)
(h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_within_at (λ x, F x t) s x₀) :
continuous_within_at (λ x, ∫ t in a..b, F x t ∂μ) s x₀ :=
tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont
/-- Continuity of interval integral with respect to a parameter at a point.
Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a
neighborhood of `x₀`, and assume it is bounded by a function integrable on
`[a, b]` independent of `x` in a neighborhood of `x₀`. If `(λ x, F x t)`
is continuous at `x₀` for almost every `t` in `[a, b]`
then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/
lemma continuous_at_of_dominated_interval
{F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ}
(hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b))
(h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t)
(bound_integrable : interval_integrable bound μ a b)
(h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_at (λ x, F x t) x₀) :
continuous_at (λ x, ∫ t in a..b, F x t ∂μ) x₀ :=
tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont
/-- Continuity of interval integral with respect to a parameter.
Given `F : X → ℝ → E`, assume each `F x` is ae-measurable on `[a, b]`,
and assume it is bounded by a function integrable on `[a, b]` independent of `x`.
If `(λ x, F x t)` is continuous for almost every `t` in `[a, b]`
then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/
lemma continuous_of_dominated_interval {F : X → ℝ → E} {bound : ℝ → ℝ} {a b : ℝ}
(hF_meas : ∀ x, ae_strongly_measurable (F x) $ μ.restrict $ Ι a b)
(h_bound : ∀ x, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t)
(bound_integrable : interval_integrable bound μ a b)
(h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous (λ x, F x t)) :
continuous (λ x, ∫ t in a..b, F x t ∂μ) :=
continuous_iff_continuous_at.mpr (λ x₀, continuous_at_of_dominated_interval
(eventually_of_forall hF_meas) (eventually_of_forall h_bound) bound_integrable $ h_cont.mono $
λ x himp hx, (himp hx).continuous_at)
end order_closed_topology
section continuous_primitive
open topological_space
variables {a b b₀ b₁ b₂ : ℝ} {μ : measure ℝ} {f g : ℝ → E}
lemma continuous_within_at_primitive (hb₀ : μ {b₀} = 0)
(h_int : interval_integrable f μ (min a b₁) (max a b₂)) :
continuous_within_at (λ b, ∫ x in a .. b, f x ∂ μ) (Icc b₁ b₂) b₀ :=
begin
by_cases h₀ : b₀ ∈ Icc b₁ b₂,
{ have h₁₂ : b₁ ≤ b₂ := h₀.1.trans h₀.2,
have min₁₂ : min b₁ b₂ = b₁ := min_eq_left h₁₂,
have h_int' : ∀ {x}, x ∈ Icc b₁ b₂ → interval_integrable f μ b₁ x,
{ rintros x ⟨h₁, h₂⟩,
apply h_int.mono_set,
apply interval_subset_interval,
{ exact ⟨min_le_of_left_le (min_le_right a b₁),
h₁.trans (h₂.trans $ le_max_of_le_right $ le_max_right _ _)⟩ },
{ exact ⟨min_le_of_left_le $ (min_le_right _ _).trans h₁,
le_max_of_le_right $ h₂.trans $ le_max_right _ _⟩ } },
have : ∀ b ∈ Icc b₁ b₂, ∫ x in a..b, f x ∂μ = ∫ x in a..b₁, f x ∂μ + ∫ x in b₁..b, f x ∂μ,
{ rintros b ⟨h₁, h₂⟩,
rw ← integral_add_adjacent_intervals _ (h_int' ⟨h₁, h₂⟩),
apply h_int.mono_set,
apply interval_subset_interval,
{ exact ⟨min_le_of_left_le (min_le_left a b₁), le_max_of_le_right (le_max_left _ _)⟩ },
{ exact ⟨min_le_of_left_le (min_le_right _ _),
le_max_of_le_right (h₁.trans $ h₂.trans (le_max_right a b₂))⟩ } },
apply continuous_within_at.congr _ this (this _ h₀), clear this,
refine continuous_within_at_const.add _,
have : (λ b, ∫ x in b₁..b, f x ∂μ) =ᶠ[𝓝[Icc b₁ b₂] b₀]
λ b, ∫ x in b₁..b₂, indicator {x | x ≤ b} f x ∂ μ,
{ apply eventually_eq_of_mem self_mem_nhds_within,
exact λ b b_in, (integral_indicator b_in).symm },
apply continuous_within_at.congr_of_eventually_eq _ this (integral_indicator h₀).symm,
have : interval_integrable (λ x, ∥f x∥) μ b₁ b₂,
from interval_integrable.norm (h_int' $ right_mem_Icc.mpr h₁₂),
refine continuous_within_at_of_dominated_interval _ _ this _ ; clear this,
{ apply eventually.mono (self_mem_nhds_within),
intros x hx,
erw [ae_strongly_measurable_indicator_iff, measure.restrict_restrict, Iic_inter_Ioc_of_le],
{ rw min₁₂,
exact (h_int' hx).1.ae_strongly_measurable },
{ exact le_max_of_le_right hx.2 },
exacts [measurable_set_Iic, measurable_set_Iic] },
{ refine eventually_of_forall (λ x, eventually_of_forall (λ t, _)),
dsimp [indicator],
split_ifs ; simp },
{ have : ∀ᵐ t ∂μ, t < b₀ ∨ b₀ < t,
{ apply eventually.mono (compl_mem_ae_iff.mpr hb₀),
intros x hx,
exact ne.lt_or_lt hx },
apply this.mono,
rintros x₀ (hx₀ | hx₀) -,
{ have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = f x₀,
{ apply mem_nhds_within_of_mem_nhds,
apply eventually.mono (Ioi_mem_nhds hx₀),
intros x hx,
simp [hx.le] },
apply continuous_within_at_const.congr_of_eventually_eq this,
simp [hx₀.le] },
{ have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = 0,
{ apply mem_nhds_within_of_mem_nhds,
apply eventually.mono (Iio_mem_nhds hx₀),
intros x hx,
simp [hx] },
apply continuous_within_at_const.congr_of_eventually_eq this,
simp [hx₀] } } },
{ apply continuous_within_at_of_not_mem_closure,
rwa [closure_Icc] }
end
lemma continuous_on_primitive [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) :
continuous_on (λ x, ∫ t in Ioc a x, f t ∂ μ) (Icc a b) :=
begin
by_cases h : a ≤ b,
{ have : ∀ x ∈ Icc a b, ∫ t in Ioc a x, f t ∂μ = ∫ t in a..x, f t ∂μ,
{ intros x x_in,
simp_rw [← interval_oc_of_le h, integral_of_le x_in.1] },
rw continuous_on_congr this,
intros x₀ hx₀,
refine continuous_within_at_primitive (measure_singleton x₀) _,
simp only [interval_integrable_iff_integrable_Ioc_of_le, min_eq_left, max_eq_right, h],
exact h_int.mono Ioc_subset_Icc_self le_rfl },
{ rw Icc_eq_empty h,
exact continuous_on_empty _ },
end
lemma continuous_on_primitive_Icc [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) :
continuous_on (λ x, ∫ t in Icc a x, f t ∂ μ) (Icc a b) :=
begin
rw show (λ x, ∫ t in Icc a x, f t ∂μ) = λ x, ∫ t in Ioc a x, f t ∂μ,
by { ext x, exact integral_Icc_eq_integral_Ioc },
exact continuous_on_primitive h_int
end
/-- Note: this assumes that `f` is `interval_integrable`, in contrast to some other lemmas here. -/
lemma continuous_on_primitive_interval' [has_no_atoms μ]
(h_int : interval_integrable f μ b₁ b₂) (ha : a ∈ [b₁, b₂]) :
continuous_on (λ b, ∫ x in a..b, f x ∂ μ) [b₁, b₂] :=
begin
intros b₀ hb₀,
refine continuous_within_at_primitive (measure_singleton _) _,
rw [min_eq_right ha.1, max_eq_right ha.2],
simpa [interval_integrable_iff, interval_oc] using h_int,
end
lemma continuous_on_primitive_interval [has_no_atoms μ]
(h_int : integrable_on f (interval a b) μ) :
continuous_on (λ x, ∫ t in a..x, f t ∂ μ) (interval a b) :=
continuous_on_primitive_interval' h_int.interval_integrable left_mem_interval
lemma continuous_on_primitive_interval_left [has_no_atoms μ]
(h_int : integrable_on f (interval a b) μ) :
continuous_on (λ x, ∫ t in x..b, f t ∂ μ) (interval a b) :=
begin
rw interval_swap a b at h_int ⊢,
simp only [integral_symm b],
exact (continuous_on_primitive_interval h_int).neg,
end
variables [has_no_atoms μ]
lemma continuous_primitive (h_int : ∀ a b, interval_integrable f μ a b) (a : ℝ) :
continuous (λ b, ∫ x in a..b, f x ∂ μ) :=
begin
rw continuous_iff_continuous_at,
intro b₀,
cases exists_lt b₀ with b₁ hb₁,
cases exists_gt b₀ with b₂ hb₂,
apply continuous_within_at.continuous_at _ (Icc_mem_nhds hb₁ hb₂),
exact continuous_within_at_primitive (measure_singleton b₀) (h_int _ _)
end
lemma _root_.measure_theory.integrable.continuous_primitive (h_int : integrable f μ) (a : ℝ) :
continuous (λ b, ∫ x in a..b, f x ∂ μ) :=
continuous_primitive (λ _ _, h_int.interval_integrable) a
end continuous_primitive
section
variables {f g : ℝ → ℝ} {a b : ℝ} {μ : measure ℝ}
lemma integral_eq_zero_iff_of_le_of_nonneg_ae (hab : a ≤ b)
(hf : 0 ≤ᵐ[μ.restrict (Ioc a b)] f) (hfi : interval_integrable f μ a b) :
∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b)] 0 :=
by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1]
lemma integral_eq_zero_iff_of_nonneg_ae
(hf : 0 ≤ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f μ a b) :
∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] 0 :=
begin
cases le_total a b with hab hab;
simp only [Ioc_eq_empty hab.not_lt, empty_union, union_empty] at hf ⊢,
{ exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi },
{ rw [integral_symm, neg_eq_zero, integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm] }
end
/-- If `f` is nonnegative and integrable on the unordered interval `set.interval_oc a b`, then its
integral over `a..b` is positive if and only if `a < b` and the measure of
`function.support f ∩ set.Ioc a b` is positive. -/
lemma integral_pos_iff_support_of_nonneg_ae'
(hf : 0 ≤ᵐ[μ.restrict (Ι a b)] f) (hfi : interval_integrable f μ a b) :
0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) :=
begin
cases lt_or_le a b with hab hba,
{ rw interval_oc_of_le hab.le at hf,
simp only [hab, true_and, integral_of_le hab.le,
set_integral_pos_iff_support_of_nonneg_ae hf hfi.1] },
{ suffices : ∫ x in a..b, f x ∂μ ≤ 0, by simp only [this.not_lt, hba.not_lt, false_and],
rw [integral_of_ge hba, neg_nonpos],
rw [interval_oc_swap, interval_oc_of_le hba] at hf,
exact integral_nonneg_of_ae hf }
end
/-- If `f` is nonnegative a.e.-everywhere and it is integrable on the unordered interval
`set.interval_oc a b`, then its integral over `a..b` is positive if and only if `a < b` and the
measure of `function.support f ∩ set.Ioc a b` is positive. -/
lemma integral_pos_iff_support_of_nonneg_ae (hf : 0 ≤ᵐ[μ] f) (hfi : interval_integrable f μ a b) :
0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) :=
integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi
/-- If `f : ℝ → ℝ` is strictly positive and integrable on `(a, b]` for real numbers `a < b`, then
its integral over `a..b` is strictly positive. -/
lemma interval_integral_pos_of_pos {f : ℝ → ℝ} {a b : ℝ}
(hfi : interval_integrable f measure_space.volume a b) (h : ∀ x, 0 < f x) (hab : a < b) :
0 < ∫ x in a..b, f x :=
begin
have hsupp : support f = univ := eq_univ_iff_forall.mpr (λ t, (h t).ne.symm),
replace h₀ : 0 ≤ᵐ[volume] f := eventually_of_forall (λ x, (h x).le),
rw integral_pos_iff_support_of_nonneg_ae h₀ hfi,
exact ⟨hab, by simp [hsupp, hab]⟩,
end
/-- If `f` and `g` are two functions that are interval integrable on `a..b`, `a ≤ b`,
`f x ≤ g x` for a.e. `x ∈ set.Ioc a b`, and `f x < g x` on a subset of `set.Ioc a b`
of nonzero measure, then `∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ`. -/
lemma integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero (hab : a ≤ b)
(hfi : interval_integrable f μ a b) (hgi : interval_integrable g μ a b)
(hle : f ≤ᵐ[μ.restrict (Ioc a b)] g) (hlt : μ.restrict (Ioc a b) {x | f x < g x} ≠ 0) :
∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ :=
begin
rw [← sub_pos, ← integral_sub hgi hfi, integral_of_le hab,
measure_theory.integral_pos_iff_support_of_nonneg_ae],
{ refine pos_iff_ne_zero.2 (mt (measure_mono_null _) hlt),
exact λ x hx, (sub_pos.2 hx).ne' },
exacts [hle.mono (λ x, sub_nonneg.2), hgi.1.sub hfi.1]
end
/-- If `f` and `g` are continuous on `[a, b]`, `a < b`, `f x ≤ g x` on this interval, and
`f c < g c` at some point `c ∈ [a, b]`, then `∫ x in a..b, f x < ∫ x in a..b, g x`. -/
lemma integral_lt_integral_of_continuous_on_of_le_of_exists_lt {f g : ℝ → ℝ} {a b : ℝ}
(hab : a < b) (hfc : continuous_on f (Icc a b)) (hgc : continuous_on g (Icc a b))
(hle : ∀ x ∈ Ioc a b, f x ≤ g x) (hlt : ∃ c ∈ Icc a b, f c < g c) :
∫ x in a..b, f x < ∫ x in a..b, g x :=
begin
refine integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero hab.le
(hfc.interval_integrable_of_Icc hab.le) (hgc.interval_integrable_of_Icc hab.le)
((ae_restrict_mem measurable_set_Ioc).mono hle) _,
contrapose! hlt,
have h_eq : f =ᵐ[volume.restrict (Ioc a b)] g,
{ simp only [← not_le, ← ae_iff] at hlt,
exact eventually_le.antisymm ((ae_restrict_iff' measurable_set_Ioc).2 $
eventually_of_forall hle) hlt },
simp only [measure.restrict_congr_set Ioc_ae_eq_Icc] at h_eq,
exact λ c hc, (measure.eq_on_Icc_of_ae_eq volume hab.ne h_eq hfc hgc hc).ge
end
lemma integral_nonneg_of_ae_restrict (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Icc a b)] f) :
0 ≤ (∫ u in a..b, f u ∂μ) :=
let H := ae_restrict_of_ae_restrict_of_subset Ioc_subset_Icc_self hf in
by simpa only [integral_of_le hab] using set_integral_nonneg_of_ae_restrict H
lemma integral_nonneg_of_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ] f) :
0 ≤ (∫ u in a..b, f u ∂μ) :=
integral_nonneg_of_ae_restrict hab $ ae_restrict_of_ae hf
lemma integral_nonneg_of_forall (hab : a ≤ b) (hf : ∀ u, 0 ≤ f u) :
0 ≤ (∫ u in a..b, f u ∂μ) :=
integral_nonneg_of_ae hab $ eventually_of_forall hf
lemma integral_nonneg (hab : a ≤ b) (hf : ∀ u, u ∈ Icc a b → 0 ≤ f u) :
0 ≤ (∫ u in a..b, f u ∂μ) :=
integral_nonneg_of_ae_restrict hab $ (ae_restrict_iff' measurable_set_Icc).mpr $ ae_of_all μ hf
lemma abs_integral_le_integral_abs (hab : a ≤ b) :
|∫ x in a..b, f x ∂μ| ≤ ∫ x in a..b, |f x| ∂μ :=
by simpa only [← real.norm_eq_abs] using norm_integral_le_integral_norm hab
section mono
variables (hab : a ≤ b) (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b)
include hab hf hg
lemma integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict (Icc a b)] g) :
∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ :=
let H := h.filter_mono $ ae_mono $ measure.restrict_mono Ioc_subset_Icc_self $ le_refl μ in
by simpa only [integral_of_le hab] using set_integral_mono_ae_restrict hf.1 hg.1 H
lemma integral_mono_ae (h : f ≤ᵐ[μ] g) :
∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ :=
by simpa only [integral_of_le hab] using set_integral_mono_ae hf.1 hg.1 h
lemma integral_mono_on (h : ∀ x ∈ Icc a b, f x ≤ g x) :
∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ :=
let H := λ x hx, h x $ Ioc_subset_Icc_self hx in
by simpa only [integral_of_le hab] using set_integral_mono_on hf.1 hg.1 measurable_set_Ioc H
lemma integral_mono (h : f ≤ g) :
∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ :=
integral_mono_ae hab hf hg $ ae_of_all _ h
omit hg hab
lemma integral_mono_interval {c d} (hca : c ≤ a) (hab : a ≤ b) (hbd : b ≤ d)
(hf : 0 ≤ᵐ[μ.restrict (Ioc c d)] f) (hfi : interval_integrable f μ c d):
∫ x in a..b, f x ∂μ ≤ ∫ x in c..d, f x ∂μ :=
begin
rw [integral_of_le hab, integral_of_le (hca.trans (hab.trans hbd))],
exact set_integral_mono_set hfi.1 hf (Ioc_subset_Ioc hca hbd).eventually_le
end
lemma abs_integral_mono_interval {c d } (h : Ι a b ⊆ Ι c d)
(hf : 0 ≤ᵐ[μ.restrict (Ι c d)] f) (hfi : interval_integrable f μ c d) :
|∫ x in a..b, f x ∂μ| ≤ |∫ x in c..d, f x ∂μ| :=
have hf' : 0 ≤ᵐ[μ.restrict (Ι a b)] f, from ae_mono (measure.restrict_mono h le_rfl) hf,
calc |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| : abs_integral_eq_abs_integral_interval_oc f
... = ∫ x in Ι a b, f x ∂μ : abs_of_nonneg (measure_theory.integral_nonneg_of_ae hf')
... ≤ ∫ x in Ι c d, f x ∂μ : set_integral_mono_set hfi.def hf h.eventually_le
... ≤ |∫ x in Ι c d, f x ∂μ| : le_abs_self _
... = |∫ x in c..d, f x ∂μ| : (abs_integral_eq_abs_integral_interval_oc f).symm
end mono
end
/-!
### Fundamental theorem of calculus, part 1, for any measure
In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integrals
w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by
`FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`,
`(a, 𝓝[≥] a, 𝓝[>] a)`, `(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances
that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and
`(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar
cases. Lean can automatically find both `a` and `l'` based on `l`.
The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen
as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict
differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that
is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three
directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any
locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes
only that `f` has finite limits almost surely at `a` and `b`.
Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of
`FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f`
has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then
`∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ +
o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)`
as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`.
This theorem is formulated with integral of constants instead of measures in the right hand sides
for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is
possible to write better `simp` lemmas for these integrals, see `integral_const` and
`integral_const_of_cdf`.
In the next subsection we apply this theorem to prove various theorems about differentiability
of the integral w.r.t. Lebesgue measure. -/
/-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate
theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. -/
class FTC_filter (a : out_param ℝ) (outer : filter ℝ) (inner : out_param $ filter ℝ)
extends tendsto_Ixx_class Ioc outer inner : Prop :=
(pure_le : pure a ≤ outer)
(le_nhds : inner ≤ 𝓝 a)
[meas_gen : is_measurably_generated inner]
/- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that
`FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`.
-/
attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class
namespace FTC_filter
instance pure (a : ℝ) : FTC_filter a (pure a) ⊥ :=
{ pure_le := le_rfl,
le_nhds := bot_le }
instance nhds_within_singleton (a : ℝ) : FTC_filter a (𝓝[{a}] a) ⊥ :=
by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance }
lemma finite_at_inner {a : ℝ} (l : filter ℝ) {l'} [h : FTC_filter a l l']
{μ : measure ℝ} [is_locally_finite_measure μ] :
μ.finite_at_filter l' :=
(μ.finite_at_nhds a).filter_mono h.le_nhds
instance nhds (a : ℝ) : FTC_filter a (𝓝 a) (𝓝 a) :=
{ pure_le := pure_le_nhds a,
le_nhds := le_rfl }
instance nhds_univ (a : ℝ) : FTC_filter a (𝓝[univ] a) (𝓝 a) :=
by { rw nhds_within_univ, apply_instance }
instance nhds_left (a : ℝ) : FTC_filter a (𝓝[≤] a) (𝓝[≤] a) :=
{ pure_le := pure_le_nhds_within right_mem_Iic,
le_nhds := inf_le_left }
instance nhds_right (a : ℝ) : FTC_filter a (𝓝[≥] a) (𝓝[>] a) :=
{ pure_le := pure_le_nhds_within left_mem_Ici,
le_nhds := inf_le_left }
instance nhds_Icc {x a b : ℝ} [h : fact (x ∈ Icc a b)] :
FTC_filter x (𝓝[Icc a b] x) (𝓝[Icc a b] x) :=
{ pure_le := pure_le_nhds_within h.out,
le_nhds := inf_le_left }
instance nhds_interval {x a b : ℝ} [h : fact (x ∈ [a, b])] :
FTC_filter x (𝓝[[a, b]] x) (𝓝[[a, b]] x) :=
by { haveI : fact (x ∈ set.Icc (min a b) (max a b)) := h, exact FTC_filter.nhds_Icc }
end FTC_filter
open asymptotics
section
variables {f : ℝ → E} {a b : ℝ} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι}
{μ : measure ℝ} {u v ua va ub vb : ι → ℝ}
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`.
If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure
finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both
`u` and `v` tend to `l`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming
`[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`,
`𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version.
The primed version also works, e.g., for `l = l' = at_top`.
We use integrals of constants instead of measures because this way it is easier to formulate
a statement that works in both cases `u ≤ v` and `v ≤ u`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae'
[is_measurably_generated l'] [tendsto_Ixx_class Ioc l l']
(hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
(hl : μ.finite_at_filter l')
(hu : tendsto u lt l) (hv : tendsto v lt l) :
(λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) :=
begin
have A := hf.integral_sub_linear_is_o_ae hfm hl (hu.Ioc hv),
have B := hf.integral_sub_linear_is_o_ae hfm hl (hv.Ioc hu),
simp only [integral_const'],
convert (A.trans_le _).sub (B.trans_le _),
{ ext t,
simp_rw [interval_integral, sub_smul],
abel },
all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] }
end
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`.
If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure
finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both
`u` and `v` tend to `l` so that `u ≤ v`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming
`[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`,
`𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version.
The primed version also works, e.g., for `l = l' = at_top`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'
[is_measurably_generated l'] [tendsto_Ixx_class Ioc l l']
(hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
(hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) :
(λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt]
(λ t, (μ $ Ioc (u t) (v t)).to_real) :=
(measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr'
(huv.mono $ λ x hx, by simp [integral_const', hx])
(huv.mono $ λ x hx, by simp [integral_const', hx])
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`.
If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure
finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both
`u` and `v` tend to `l` so that `v ≤ u`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming
`[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`,
`𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version.
The primed version also works, e.g., for `l = l' = at_top`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'
[is_measurably_generated l'] [tendsto_Ixx_class Ioc l l']
(hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
(hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) :
(λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt]
(λ t, (μ $ Ioc (v t) (u t)).to_real) :=
(measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $
λ t, by simp [integral_symm (u t), add_comm]
section
variables [is_locally_finite_measure μ] [FTC_filter a l l']
include a
local attribute [instance] FTC_filter.meas_gen
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure.
If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then
`∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for
`l = l' = at_top`.
We use integrals of constants instead of measures because this way it is easier to formulate
a statement that works in both cases `u ≤ v` and `v ≤ u`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : strongly_measurable_at_filter f l' μ)
(hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) :
(λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) :=
measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure.
If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then
`∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works,
e.g., for `l = l' = at_top`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le
(hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
(hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) :
(λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt]
(λ t, (μ $ Ioc (u t) (v t)).to_real) :=
measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l)
hu hv huv
/-- Fundamental theorem of calculus-1, local version for any measure.
Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure.
If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then
`∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`.
See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works,
e.g., for `l = l' = at_top`. -/
lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge
(hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c))
(hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) :
(λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt]
(λ t, (μ $ Ioc (v t) (u t)).to_real) :=
measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l)
hu hv huv
end
local attribute [instance] FTC_filter.meas_gen
variables [FTC_filter a la la'] [FTC_filter b lb lb'] [is_locally_finite_measure μ]
/-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite
measure.
Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s
around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite
limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively.
Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ =
∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ +
o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)`
as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`.
-/
lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae
(hab : interval_integrable f μ a b)
(hmeas_a : strongly_measurable_at_filter f la' μ)
(hmeas_b : strongly_measurable_at_filter f lb' μ)
(ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb))
(hua : tendsto ua lt la) (hva : tendsto va lt la)
(hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) :
(λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) -
(∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) =o[lt]
(λ t, ∥∫ x in ua t..va t, (1:ℝ) ∂μ∥ + ∥∫ x in ub t..vb t, (1:ℝ) ∂μ∥) :=
begin
refine
((measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_a ha_lim hua hva).neg_left.add_add
(measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_b hb_lim hub hvb)).congr'
_ eventually_eq.rfl,
have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) :=
ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) hua hva,
have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) :=
ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la)
(tendsto_const_pure.mono_right FTC_filter.pure_le) hua,
have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) :=
hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) hub hvb,
have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) :=
hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb)
(tendsto_const_pure.mono_right FTC_filter.pure_le) hub,
filter_upwards [A, A', B, B'] with _ ua_va a_ua ub_vb b_ub,
rw [← integral_interval_sub_interval_comm'],
{ dsimp only [], abel, },
exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua]
end
/-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite
measure.
Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s
around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`.
Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)`
as `u` and `v` tend to `lb`.
-/
lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right
(hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f lb' μ)
(hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) :
(λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt]
(λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) :=
by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae
hab strongly_measurable_at_bot hmeas ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left)
hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv
/-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite
measure.
Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s
around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`.
Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)`
as `u` and `v` tend to `la`.
-/
lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left
(hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f la' μ)
(hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) :
(λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) =o[lt]
(λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) :=
by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae
hab hmeas strongly_measurable_at_bot hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left)
hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure
end
/-!
### Fundamental theorem of calculus-1 for Lebesgue measure
In this section we restate theorems from the previous section for Lebesgue measure.
In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)`
at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`.
-/
variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι}
{a b z : ℝ} {u v ua ub va vb : ι → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb']
/-!
#### Auxiliary `is_o` statements
In this section we prove several lemmas that can be interpreted as strict differentiability of
`(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because
we have no definition of `has_strict_(f)deriv_at_filter` in the library.
-/
/-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at
`l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then
`∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/
lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l']
(hfm : strongly_measurable_at_filter f l') (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c))
{u v : ι → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) :
(λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) =o[lt] (v - u) :=
by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv
/-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints.
If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around
`a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb`
almost surely at `la'` and `lb'`, respectively, then
`(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca +
o(∥va - ua∥ + ∥vb - ub∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`.
This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this
definition. -/
lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae
(hab : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f la') (hmeas_b : strongly_measurable_at_filter f lb')
(ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb))
(hua : tendsto ua lt la) (hva : tendsto va lt la)
(hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) :
(λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) -
((vb t - ub t) • cb - (va t - ua t) • ca)) =o[lt] (λ t, ∥va t - ua t∥ + ∥vb t - ub t∥) :=
by simpa [integral_const]
using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas_a hmeas_b
ha_lim hb_lim hua hva hub hvb
/-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints.
If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair
around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then
`(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `lb`.
This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/
lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right
(hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f lb')
(hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) :
(λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) =o[lt] (v - u) :=
by simpa only [integral_const, smul_eq_mul, mul_one] using
measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hmeas hf hu hv
/-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints.
If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair
around `a`, and `f` has a finite limit `c` almost surely at `la'`, then
`(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `la`.
This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/
lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left
(hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f la')
(hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) :
(λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) =o[lt] (v - u) :=
by simpa only [integral_const, smul_eq_mul, mul_one] using
measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hmeas hf hu hv
open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub)
/-!
#### Strict differentiability
In this section we prove that for a measurable function `f` integrable on `a..b`,
* `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has
derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability
provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`,
respectively;
* `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has
derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability
provided that `f` is continuous at `a` and `b`;
* `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has
derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c`
almost surely as `x` tends to `b`;
* `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at
`b` in the sense of strict differentiability provided that `f` is continuous at `b`;
* `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has
derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c`
almost surely as `x` tends to `a`;
* `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at
`a` in the sense of strict differentiability provided that `f` is continuous at `a`.
-/
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite
limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then
`(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`
in the sense of strict differentiability. -/
lemma integral_has_strict_fderiv_at_of_tendsto_ae
(hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) :
has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) :=
begin
have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb
((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b)))
((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b)))
((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b)))
((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))),
refine (this.congr_left _).trans_is_O _,
{ intro x, simp [sub_smul] },
{ exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left }
end
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca`
at `(a, b)` in the sense of strict differentiability. -/
lemma integral_has_strict_fderiv_at
(hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : continuous_at f a) (hb : continuous_at f b) :
has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) :=
integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b
(ha.mono_left inf_le_left) (hb.mono_left inf_le_left)
/-- **First Fundamental Theorem of Calculus**: if `f : ℝ → E` is integrable on `a..b` and `f x` has
a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in
the sense of strict differentiability. -/
lemma integral_has_strict_deriv_at_of_tendsto_ae_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b :=
integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb continuous_at_snd
continuous_at_fst
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict
differentiability. -/
lemma integral_has_strict_deriv_at_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b :=
integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left)
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense
of strict differentiability. -/
lemma integral_has_strict_deriv_at_of_tendsto_ae_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a :=
by simpa only [← integral_symm]
using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm hmeas ha).neg
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict
differentiability. -/
lemma integral_has_strict_deriv_at_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a :=
by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm hmeas ha).neg
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is continuous, then `u ↦ ∫ x in a..u, f x`
has derivative `f b` at `b` in the sense of strict differentiability. -/
lemma _root_.continuous.integral_has_strict_deriv_at {f : ℝ → E} (hf : continuous f) (a b : ℝ) :
has_strict_deriv_at (λ u, ∫ (x : ℝ) in a..u, f x) (f b) b :=
integral_has_strict_deriv_at_right (hf.interval_integrable _ _)
(hf.strongly_measurable_at_filter _ _) hf.continuous_at
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is continuous, then the derivative
of `u ↦ ∫ x in a..u, f x` at `b` is `f b`. -/
lemma _root_.continuous.deriv_integral (f : ℝ → E) (hf : continuous f) (a b : ℝ) :
deriv (λ u, ∫ (x : ℝ) in a..u, f x) b = f b :=
(hf.integral_has_strict_deriv_at a b).has_deriv_at.deriv
/-!
#### Fréchet differentiability
In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`,
`has_deriv_at`, `fderiv`, and `deriv`.
-/
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite
limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then
`(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/
lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) :
has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) :=
(integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).has_fderiv_at
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca`
at `(a, b)`. -/
lemma integral_has_fderiv_at (hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : continuous_at f a) (hb : continuous_at f b) :
has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) :=
(integral_has_strict_fderiv_at hf hmeas_a hmeas_b ha hb).has_fderiv_at
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite
limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv`
derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/
lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) :
fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) =
(snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca :=
(integral_has_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦
v • cb - u • ca`. -/
lemma fderiv_integral (hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f (𝓝 a))
(hmeas_b : strongly_measurable_at_filter f (𝓝 b))
(ha : continuous_at f a) (hb : continuous_at f b) :
fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) =
(snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) :=
(integral_has_fderiv_at hf hmeas_a hmeas_b ha hb).fderiv
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/
lemma integral_has_deriv_at_of_tendsto_ae_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b :=
(integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas hb).has_deriv_at
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/
lemma integral_has_deriv_at_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b :=
(integral_has_strict_deriv_at_right hf hmeas hb).has_deriv_at
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite
limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/
lemma deriv_integral_of_tendsto_ae_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c :=
(integral_has_deriv_at_of_tendsto_ae_right hf hmeas hb).deriv
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/
lemma deriv_integral_right
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b))
(hb : continuous_at f b) :
deriv (λ u, ∫ x in a..u, f x) b = f b :=
(integral_has_deriv_at_right hf hmeas hb).deriv
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/
lemma integral_has_deriv_at_of_tendsto_ae_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a :=
(integral_has_strict_deriv_at_of_tendsto_ae_left hf hmeas ha).has_deriv_at
/-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/
lemma integral_has_deriv_at_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(ha : continuous_at f a) :
has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a :=
(integral_has_strict_deriv_at_left hf hmeas ha).has_deriv_at
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite
limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/
lemma deriv_integral_of_tendsto_ae_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c :=
(integral_has_deriv_at_of_tendsto_ae_left hf hmeas hb).deriv
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous
at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/
lemma deriv_integral_left
(hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a))
(hb : continuous_at f a) :
deriv (λ u, ∫ x in u..b, f x) a = -f a :=
(integral_has_deriv_at_left hf hmeas hb).deriv
/-!
#### One-sided derivatives
-/
/-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x`
has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where
`s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca`
and `cb` almost surely at the filters `la` and `lb` from the following table.
| `s` | `la` | `t` | `lb` |
| ------- | ---- | --- | ---- |
| `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` |
| `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` |
| `{a}` | `⊥` | `{b}` | `⊥` |
| `univ` | `𝓝 a` | `univ` | `𝓝 b` |
-/
lemma integral_has_fderiv_within_at_of_tendsto_ae
(hf : interval_integrable f volume a b)
{s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb]
(hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb)
(ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) :
has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s ×ˢ t) (a, b) :=
begin
rw [has_fderiv_within_at, nhds_within_prod_eq],
have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb
(tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst
(tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd,
refine (this.congr_left _).trans_is_O _,
{ intro x, simp [sub_smul] },
{ exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left }
end
/-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x`
has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where
`s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to
`f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption
is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`.
| `s` | `la` | `t` | `lb` |
| ------- | ---- | --- | ---- |
| `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` |
| `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` |
| `{a}` | `⊥` | `{b}` | `⊥` |
| `univ` | `𝓝 a` | `univ` | `𝓝 b` |
-/
lemma integral_has_fderiv_within_at
(hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb)
{s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb]
(ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) :
has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x)
((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s ×ˢ t) (a, b) :=
integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left)
(hb.mono_left inf_le_left)
/-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where
`s ∈ {Iic a, Ici a, univ}`. -/
meta def unique_diff_within_at_Ici_Iic_univ : tactic unit :=
`[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic,
left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]]
/-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}`
and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters
`la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s ×ˢ t)`
is equal to `(u, v) ↦ u • cb - v • ca`.
| `s` | `la` | `t` | `lb` |
| ------- | ---- | --- | ---- |
| `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` |
| `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` |
| `{a}` | `⊥` | `{b}` | `⊥` |
| `univ` | `𝓝 a` | `univ` | `𝓝 b` |
-/
lemma fderiv_within_integral_of_tendsto_ae
(hf : interval_integrable f volume a b)
(hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb)
{s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb]
(ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb))
(hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ)
(ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) :
fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s ×ˢ t) (a, b) =
((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) :=
(integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv_within $ hs.prod ht
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely as `x` tends to `b` from the right or from the left,
then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/
lemma integral_has_deriv_within_at_of_tendsto_ae_right
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) :
has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b :=
integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb
(tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous
from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right)
derivative `f b` at `b`. -/
lemma integral_has_deriv_within_at_right
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) :
has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b :=
integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left)
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right
(resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/
lemma deriv_within_integral_of_tendsto_ae_right
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)]
(hmeas: strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c))
(hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) :
deriv_within (λ u, ∫ x in a..u, f x) s b = c :=
(integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas hb).deriv_within hs
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous
on the right or on the left at `b`, then the right (resp., left) derivative of
`u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/
lemma deriv_within_integral_right
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b)
(hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) :
deriv_within (λ u, ∫ x in a..u, f x) s b = f b :=
(integral_has_deriv_within_at_right hf hmeas hb).deriv_within hs
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely as `x` tends to `a` from the right or from the left,
then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/
lemma integral_has_deriv_within_at_of_tendsto_ae_left
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] a))
(ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) :
has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a :=
by { simp only [integral_symm b],
exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm hmeas ha).neg }
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous
from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right)
derivative `-f a` at `a`. -/
lemma integral_has_deriv_within_at_left
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) :
has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a :=
integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas (ha.mono_left inf_le_left)
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite
limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right
(resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/
lemma deriv_within_integral_of_tendsto_ae_left
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c))
(hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) :
deriv_within (λ u, ∫ x in u..b, f x) s a = -c :=
(integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas ha).deriv_within hs
/-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous
on the right or on the left at `a`, then the right (resp., left) derivative of
`u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/
lemma deriv_within_integral_left
(hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)]
(hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a)
(hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) :
deriv_within (λ u, ∫ x in u..b, f x) s a = -f a :=
(integral_has_deriv_within_at_left hf hmeas ha).deriv_within hs
/-- The integral of a continuous function is differentiable on a real set `s`. -/
theorem differentiable_on_integral_of_continuous {s : set ℝ}
(hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) :
differentiable_on ℝ (λ u, ∫ x in a..u, f x) s :=
λ y hy, (integral_has_deriv_at_right (hintg y hy)
hcont.ae_strongly_measurable.strongly_measurable_at_filter
hcont.continuous_at) .differentiable_at.differentiable_within_at
/-!
### Fundamental theorem of calculus, part 2
This section contains theorems pertaining to FTC-2 for interval integrals, i.e., the assertion
that `∫ x in a..b, f' x = f b - f a` under suitable assumptions.
The most classical version of this theorem assumes that `f'` is continuous. However, this is
unnecessarily strong: the result holds if `f'` is just integrable. We prove the strong version,
following [Rudin, *Real and Complex Analysis* (Theorem 7.21)][rudin2006real]. The proof is first
given for real-valued functions, and then deduced for functions with a general target space. For
a real-valued function `g`, it suffices to show that `g b - g a ≤ (∫ x in a..b, g' x) + ε` for all
positive `ε`. To prove this, choose a lower-semicontinuous function `G'` with `g' < G'` and with
integral close to that of `g'` (its existence is guaranteed by the Vitali-Carathéodory theorem).
It satisfies `g t - g a ≤ ∫ x in a..t, G' x` for all `t ∈ [a, b]`: this inequality holds at `a`,
and if it holds at `t` then it holds for `u` close to `t` on its right, as the left hand side
increases by `g u - g t ∼ (u -t) g' t`, while the right hand side increases by
`∫ x in t..u, G' x` which is roughly at least `∫ x in t..u, G' t = (u - t) G' t`, by lower
semicontinuity. As `g' t < G' t`, this gives the conclusion. One can therefore push progressively
this inequality to the right until the point `b`, where it gives the desired conclusion.
-/
variables {g' g φ : ℝ → ℝ}
/-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has
`g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable.
Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`.
We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and
`φ` is integrable (even if `g'` is not known to be integrable).
Version assuming that `g` is differentiable on `[a, b)`. -/
lemma sub_le_integral_of_has_deriv_right_of_le_Ico (hab : a ≤ b) (hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ico a b, has_deriv_within_at g (g' x) (Ioi x) x)
(φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ico a b, g' x ≤ φ x) :
g b - g a ≤ ∫ y in a..b, φ y :=
begin
refine le_of_forall_pos_le_add (λ ε εpos, _),
-- Bound from above `g'` by a lower-semicontinuous function `G'`.
rcases exists_lt_lower_semicontinuous_integral_lt φ φint εpos with
⟨G', f_lt_G', G'cont, G'int, G'lt_top, hG'⟩,
-- we will show by "induction" that `g t - g a ≤ ∫ u in a..t, G' u` for all `t ∈ [a, b]`.
set s := {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real} ∩ Icc a b,
-- the set `s` of points where this property holds is closed.
have s_closed : is_closed s,
{ have : continuous_on (λ t, (g t - g a, ∫ u in a..t, (G' u).to_real)) (Icc a b),
{ rw ← interval_of_le hab at G'int ⊢ hcont,
exact (hcont.sub continuous_on_const).prod (continuous_on_primitive_interval G'int) },
simp only [s, inter_comm],
exact this.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' },
have main : Icc a b ⊆ {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real },
{ -- to show that the set `s` is all `[a, b]`, it suffices to show that any point `t` in `s`
-- with `t < b` admits another point in `s` slightly to its right
-- (this is a sort of real induction).
apply s_closed.Icc_subset_of_forall_exists_gt
(by simp only [integral_same, mem_set_of_eq, sub_self]) (λ t ht v t_lt_v, _),
obtain ⟨y, g'_lt_y', y_lt_G'⟩ : ∃ (y : ℝ), (g' t : ereal) < y ∧ (y : ereal) < G' t :=
ereal.lt_iff_exists_real_btwn.1 ((ereal.coe_le_coe_iff.2 (hφg t ht.2)).trans_lt (f_lt_G' t)),
-- bound from below the increase of `∫ x in a..u, G' x` on the right of `t`, using the lower
-- semicontinuity of `G'`.
have I1 : ∀ᶠ u in 𝓝[>] t, (u - t) * y ≤ ∫ w in t..u, (G' w).to_real,
{ have B : ∀ᶠ u in 𝓝 t, (y : ereal) < G' u :=
G'cont.lower_semicontinuous_at _ _ y_lt_G',
rcases mem_nhds_iff_exists_Ioo_subset.1 B with ⟨m, M, ⟨hm, hM⟩, H⟩,
have : Ioo t (min M b) ∈ 𝓝[>] t := mem_nhds_within_Ioi_iff_exists_Ioo_subset.2
⟨min M b, by simp only [hM, ht.right.right, lt_min_iff, mem_Ioi, and_self], subset.refl _⟩,
filter_upwards [this] with u hu,
have I : Icc t u ⊆ Icc a b := Icc_subset_Icc ht.2.1 (hu.2.le.trans (min_le_right _ _)),
calc (u - t) * y = ∫ v in Icc t u, y :
by simp only [hu.left.le, measure_theory.integral_const, algebra.id.smul_eq_mul, sub_nonneg,
measurable_set.univ, real.volume_Icc, measure.restrict_apply, univ_inter,
ennreal.to_real_of_real]
... ≤ ∫ w in t..u, (G' w).to_real :
begin
rw [interval_integral.integral_of_le hu.1.le, ← integral_Icc_eq_integral_Ioc],
apply set_integral_mono_ae_restrict,
{ simp only [integrable_on_const, real.volume_Icc, ennreal.of_real_lt_top, or_true] },
{ exact integrable_on.mono_set G'int I },
{ have C1 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), G' x < ∞ :=
ae_mono (measure.restrict_mono I le_rfl) G'lt_top,
have C2 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), x ∈ Icc t u :=
ae_restrict_mem measurable_set_Icc,
filter_upwards [C1, C2] with x G'x hx,
apply ereal.coe_le_coe_iff.1,
have : x ∈ Ioo m M, by simp only [hm.trans_le hx.left,
(hx.right.trans_lt hu.right).trans_le (min_le_left M b), mem_Ioo, and_self],
convert le_of_lt (H this),
exact ereal.coe_to_real G'x.ne (ne_bot_of_gt (f_lt_G' x)) }
end },
-- bound from above the increase of `g u - g a` on the right of `t`, using the derivative at `t`
have I2 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ (u - t) * y,
{ have g'_lt_y : g' t < y := ereal.coe_lt_coe_iff.1 g'_lt_y',
filter_upwards [(hderiv t ⟨ht.2.1, ht.2.2⟩).limsup_slope_le'
(not_mem_Ioi.2 le_rfl) g'_lt_y, self_mem_nhds_within] with u hu t_lt_u,
have := mul_le_mul_of_nonneg_left hu.le (sub_pos.2 t_lt_u).le,
rwa [← smul_eq_mul, sub_smul_slope] at this },
-- combine the previous two bounds to show that `g u - g a` increases less quickly than
-- `∫ x in a..u, G' x`.
have I3 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ ∫ w in t..u, (G' w).to_real,
{ filter_upwards [I1, I2] with u hu1 hu2 using hu2.trans hu1, },
have I4 : ∀ᶠ u in 𝓝[>] t, u ∈ Ioc t (min v b),
{ refine mem_nhds_within_Ioi_iff_exists_Ioc_subset.2 ⟨min v b, _, subset.refl _⟩,
simp only [lt_min_iff, mem_Ioi],
exact ⟨t_lt_v, ht.2.2⟩ },
-- choose a point `x` slightly to the right of `t` which satisfies the above bound
rcases (I3.and I4).exists with ⟨x, hx, h'x⟩,
-- we check that it belongs to `s`, essentially by construction
refine ⟨x, _, Ioc_subset_Ioc le_rfl (min_le_left _ _) h'x⟩,
calc g x - g a = (g t - g a) + (g x - g t) : by abel
... ≤ (∫ w in a..t, (G' w).to_real) + ∫ w in t..x, (G' w).to_real : add_le_add ht.1 hx
... = ∫ w in a..x, (G' w).to_real :
begin
apply integral_add_adjacent_intervals,
{ rw interval_integrable_iff_integrable_Ioc_of_le ht.2.1,
exact integrable_on.mono_set G'int
(Ioc_subset_Icc_self.trans (Icc_subset_Icc le_rfl ht.2.2.le)) },
{ rw interval_integrable_iff_integrable_Ioc_of_le h'x.1.le,
apply integrable_on.mono_set G'int,
refine Ioc_subset_Icc_self.trans (Icc_subset_Icc ht.2.1 (h'x.2.trans (min_le_right _ _))) }
end },
-- now that we know that `s` contains `[a, b]`, we get the desired result by applying this to `b`.
calc g b - g a ≤ ∫ y in a..b, (G' y).to_real : main (right_mem_Icc.2 hab)
... ≤ (∫ y in a..b, φ y) + ε :
begin
convert hG'.le;
{ rw interval_integral.integral_of_le hab,
simp only [integral_Icc_eq_integral_Ioc', real.volume_singleton] },
end
end
/-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has
`g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable.
Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`.
We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and
`φ` is integrable (even if `g'` is not known to be integrable).
Version assuming that `g` is differentiable on `(a, b)`. -/
lemma sub_le_integral_of_has_deriv_right_of_le (hab : a ≤ b)
(hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x)
(φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, g' x ≤ φ x) :
g b - g a ≤ ∫ y in a..b, φ y :=
begin
-- This follows from the version on a closed-open interval (applied to `[t, b)` for `t` close to
-- `a`) and a continuity argument.
obtain rfl|a_lt_b := hab.eq_or_lt, { simp },
set s := {t | g b - g t ≤ ∫ u in t..b, φ u} ∩ Icc a b,
have s_closed : is_closed s,
{ have : continuous_on (λ t, (g b - g t, ∫ u in t..b, φ u)) (Icc a b),
{ rw ← interval_of_le hab at ⊢ hcont φint,
exact (continuous_on_const.sub hcont).prod (continuous_on_primitive_interval_left φint) },
simp only [s, inter_comm],
exact this.preimage_closed_of_closed is_closed_Icc is_closed_le_prod, },
have A : closure (Ioc a b) ⊆ s,
{ apply s_closed.closure_subset_iff.2,
assume t ht,
refine ⟨_, ⟨ht.1.le, ht.2⟩⟩,
exact sub_le_integral_of_has_deriv_right_of_le_Ico ht.2
(hcont.mono (Icc_subset_Icc ht.1.le le_rfl))
(λ x hx, hderiv x ⟨ht.1.trans_le hx.1, hx.2⟩)
(φint.mono_set (Icc_subset_Icc ht.1.le le_rfl))
(λ x hx, hφg x ⟨ht.1.trans_le hx.1, hx.2⟩) },
rw closure_Ioc a_lt_b.ne at A,
exact (A (left_mem_Icc.2 hab)).1,
end
/-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. -/
lemma integral_le_sub_of_has_deriv_right_of_le (hab : a ≤ b)
(hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x)
(φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, φ x ≤ g' x) :
∫ y in a..b, φ y ≤ g b - g a :=
begin
rw ← neg_le_neg_iff,
convert sub_le_integral_of_has_deriv_right_of_le hab hcont.neg (λ x hx, (hderiv x hx).neg)
φint.neg (λ x hx, neg_le_neg (hφg x hx)),
{ abel },
{ simp only [← integral_neg], refl },
end
/-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`: real version -/
lemma integral_eq_sub_of_has_deriv_right_of_le_real (hab : a ≤ b)
(hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x)
(g'int : integrable_on g' (Icc a b)) :
∫ y in a..b, g' y = g b - g a :=
le_antisymm
(integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl))
(sub_le_integral_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl))
variable {f' : ℝ → E}
/-- **Fundamental theorem of calculus-2**: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`)
and has a right derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`,
then `∫ y in a..b, f' y` equals `f b - f a`. -/
theorem integral_eq_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at f (f' x) (Ioi x) x)
(f'int : interval_integrable f' volume a b) :
∫ y in a..b, f' y = f b - f a :=
begin
refine (normed_space.eq_iff_forall_dual_eq ℝ).2 (λ g, _),
rw [← g.interval_integral_comp_comm f'int, g.map_sub],
exact integral_eq_sub_of_has_deriv_right_of_le_real hab (g.continuous.comp_continuous_on hcont)
(λ x hx, g.has_fderiv_at.comp_has_deriv_within_at x (hderiv x hx))
(g.integrable_comp ((interval_integrable_iff_integrable_Icc_of_le hab).1 f'int))
end
/-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and
has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is integrable on `[a, b]` then
`∫ y in a..b, f' y` equals `f b - f a`. -/
theorem integral_eq_sub_of_has_deriv_right (hcont : continuous_on f (interval a b))
(hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x)
(hint : interval_integrable f' volume a b) :
∫ y in a..b, f' y = f b - f a :=
begin
cases le_total a b with hab hab,
{ simp only [interval_of_le, min_eq_left, max_eq_right, hab] at hcont hderiv hint,
apply integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint },
{ simp only [interval_of_ge, min_eq_right, max_eq_left, hab] at hcont hderiv,
rw [integral_symm, integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint.symm,
neg_sub] }
end
/-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and
has a derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`, then
`∫ y in a..b, f' y` equals `f b - f a`. -/
theorem integral_eq_sub_of_has_deriv_at_of_le (hab : a ≤ b)
(hcont : continuous_on f (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) :
∫ y in a..b, f' y = f b - f a :=
integral_eq_sub_of_has_deriv_right_of_le hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) hint
/-- Fundamental theorem of calculus-2: If `f : ℝ → E` has a derivative at `f' x` for all `x` in
`[a, b]` and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/
theorem integral_eq_sub_of_has_deriv_at
(hderiv : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(hint : interval_integrable f' volume a b) :
∫ y in a..b, f' y = f b - f a :=
integral_eq_sub_of_has_deriv_right (has_deriv_at.continuous_on hderiv)
(λ x hx, (hderiv _ (mem_Icc_of_Ioo hx)).has_deriv_within_at) hint
theorem integral_eq_sub_of_has_deriv_at_of_tendsto (hab : a < b) {fa fb}
(hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b)
(ha : tendsto f (𝓝[>] a) (𝓝 fa)) (hb : tendsto f (𝓝[<] b) (𝓝 fb)) :
∫ y in a..b, f' y = fb - fa :=
begin
set F : ℝ → E := update (update f a fa) b fb,
have Fderiv : ∀ x ∈ Ioo a b, has_deriv_at F (f' x) x,
{ refine λ x hx, (hderiv x hx).congr_of_eventually_eq _,
filter_upwards [Ioo_mem_nhds hx.1 hx.2] with _ hy, simp only [F],
rw [update_noteq hy.2.ne, update_noteq hy.1.ne'], },
have hcont : continuous_on F (Icc a b),
{ rw [continuous_on_update_iff, continuous_on_update_iff, Icc_diff_right, Ico_diff_left],
refine ⟨⟨λ z hz, (hderiv z hz).continuous_at.continuous_within_at, _⟩, _⟩,
{ exact λ _, ha.mono_left (nhds_within_mono _ Ioo_subset_Ioi_self) },
{ rintro -,
refine (hb.congr' _).mono_left (nhds_within_mono _ Ico_subset_Iio_self),
filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 hab)]
with _ hz using (update_noteq hz.1.ne' _ _).symm } },
simpa [F, hab.ne, hab.ne'] using integral_eq_sub_of_has_deriv_at_of_le hab.le hcont Fderiv hint
end
/-- Fundamental theorem of calculus-2: If `f : ℝ → E` is differentiable at every `x` in `[a, b]` and
its derivative is integrable on `[a, b]`, then `∫ y in a..b, deriv f y` equals `f b - f a`. -/
theorem integral_deriv_eq_sub (hderiv : ∀ x ∈ interval a b, differentiable_at ℝ f x)
(hint : interval_integrable (deriv f) volume a b) :
∫ y in a..b, deriv f y = f b - f a :=
integral_eq_sub_of_has_deriv_at (λ x hx, (hderiv x hx).has_deriv_at) hint
theorem integral_deriv_eq_sub' (f) (hderiv : deriv f = f')
(hdiff : ∀ x ∈ interval a b, differentiable_at ℝ f x)
(hcont : continuous_on f' (interval a b)) :
∫ y in a..b, f' y = f b - f a :=
begin
rw [← hderiv, integral_deriv_eq_sub hdiff],
rw hderiv,
exact hcont.interval_integrable
end
/-!
### Automatic integrability for nonnegative derivatives
-/
/-- When the right derivative of a function is nonnegative, then it is automatically integrable. -/
lemma integrable_on_deriv_right_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x)
(g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) :
integrable_on g' (Ioc a b) :=
begin
rw integrable_on_Ioc_iff_integrable_on_Ioo,
have meas_g' : ae_measurable g' (volume.restrict (Ioo a b)),
{ apply (ae_measurable_deriv_within_Ioi g _).congr,
refine (ae_restrict_mem measurable_set_Ioo).mono (λ x hx, _),
exact (hderiv x hx).deriv_within (unique_diff_within_at_Ioi _) },
suffices H : ∫⁻ x in Ioo a b, ∥g' x∥₊ ≤ ennreal.of_real (g b - g a),
from ⟨meas_g'.ae_strongly_measurable, H.trans_lt ennreal.of_real_lt_top⟩,
by_contra' H,
obtain ⟨f, fle, fint, hf⟩ :
∃ (f : simple_func ℝ ℝ≥0), (∀ x, f x ≤ ∥g' x∥₊) ∧ ∫⁻ (x : ℝ) in Ioo a b, f x < ∞
∧ ennreal.of_real (g b - g a) < ∫⁻ (x : ℝ) in Ioo a b, f x :=
exists_lt_lintegral_simple_func_of_lt_lintegral H,
let F : ℝ → ℝ := coe ∘ f,
have intF : integrable_on F (Ioo a b),
{ refine ⟨f.measurable.coe_nnreal_real.ae_strongly_measurable, _⟩,
simpa only [has_finite_integral, nnreal.nnnorm_eq] using fint },
have A : ∫⁻ (x : ℝ) in Ioo a b, f x = ennreal.of_real (∫ x in Ioo a b, F x) :=
lintegral_coe_eq_integral _ intF,
rw A at hf,
have B : ∫ (x : ℝ) in Ioo a b, F x ≤ g b - g a,
{ rw [← integral_Ioc_eq_integral_Ioo, ← interval_integral.integral_of_le hab],
apply integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv _ (λ x hx, _),
{ rwa integrable_on_Icc_iff_integrable_on_Ioo },
{ convert nnreal.coe_le_coe.2 (fle x),
simp only [real.norm_of_nonneg (g'pos x hx), coe_nnnorm] } },
exact lt_irrefl _ (hf.trans_le (ennreal.of_real_le_of_real B)),
end
/-- When the derivative of a function is nonnegative, then it is automatically integrable,
Ioc version. -/
lemma integrable_on_deriv_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b))
(hderiv : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x)
(g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) :
integrable_on g' (Ioc a b) :=
integrable_on_deriv_right_of_nonneg hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) g'pos
/-- When the derivative of a function is nonnegative, then it is automatically integrable,
interval version. -/
theorem interval_integrable_deriv_of_nonneg (hcont : continuous_on g (interval a b))
(hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_at g (g' x) x)
(hpos : ∀ x ∈ Ioo (min a b) (max a b), 0 ≤ g' x) :
interval_integrable g' volume a b :=
begin
cases le_total a b with hab hab,
{ simp only [interval_of_le, min_eq_left, max_eq_right, hab, interval_integrable,
hab, Ioc_eq_empty_of_le, integrable_on_empty, and_true] at hcont hderiv hpos ⊢,
exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos, },
{ simp only [interval_of_ge, min_eq_right, max_eq_left, hab, interval_integrable,
Ioc_eq_empty_of_le, integrable_on_empty, true_and] at hcont hderiv hpos ⊢,
exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos }
end
/-!
### Integration by parts
-/
theorem integral_deriv_mul_eq_sub {u v u' v' : ℝ → ℝ}
(hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x)
(hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x)
(hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) :
∫ x in a..b, u' x * v x + u x * v' x = u b * v b - u a * v a :=
integral_eq_sub_of_has_deriv_at (λ x hx, (hu x hx).mul (hv x hx)) $
(hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add
(hv'.continuous_on_mul ((has_deriv_at.continuous_on hu)))
theorem integral_mul_deriv_eq_deriv_mul {u v u' v' : ℝ → ℝ}
(hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x)
(hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x)
(hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) :
∫ x in a..b, u x * v' x = u b * v b - u a * v a - ∫ x in a..b, v x * u' x :=
begin
rw [← integral_deriv_mul_eq_sub hu hv hu' hv', ← integral_sub],
{ exact integral_congr (λ x hx, by simp only [mul_comm, add_sub_cancel']) },
{ exact ((hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add
(hv'.continuous_on_mul (has_deriv_at.continuous_on hu))) },
{ exact hu'.continuous_on_mul (has_deriv_at.continuous_on hv) },
end
/-!
### Integration by substitution / Change of variables
-/
section smul
/--
Change of variables, general form. If `f` is continuous on `[a, b]` and has
continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can
substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`.
We could potentially slightly weaken the conditions, by not requiring that `f'` and `g` are
continuous on the endpoints of these intervals, but in that case we need to additionally assume that
the functions are integrable on that interval.
-/
theorem integral_comp_smul_deriv'' {f f' : ℝ → ℝ} {g : ℝ → E}
(hf : continuous_on f [a, b])
(hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x)
(hf' : continuous_on f' [a, b])
(hg : continuous_on g (f '' [a, b])) :
∫ x in a..b, f' x • (g ∘ f) x= ∫ u in f a..f b, g u :=
begin
have h_cont : continuous_on (λ u, ∫ t in f a..f u, g t) [a, b],
{ rw [hf.image_interval] at hg,
refine (continuous_on_primitive_interval' hg.interval_integrable _).comp hf _,
{ rw ← hf.image_interval, exact mem_image_of_mem f left_mem_interval },
{ rw ← hf.image_interval, exact maps_to_image _ _ } },
have h_der : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at
(λ u, ∫ t in f a..f u, g t) (f' x • ((g ∘ f) x)) (Ioi x) x,
{ intros x hx,
let I := [Inf (f '' [a, b]), Sup (f '' [a, b])],
have hI : f '' [a, b] = I := hf.image_interval,
have h2x : f x ∈ I, { rw [← hI], exact mem_image_of_mem f (Ioo_subset_Icc_self hx) },
have h2g : interval_integrable g volume (f a) (f x),
{ refine (hg.mono $ _).interval_integrable,
exact hf.surj_on_interval left_mem_interval (Ioo_subset_Icc_self hx) },
rw [hI] at hg,
have h3g : strongly_measurable_at_filter g (𝓝[I] f x) volume :=
hg.strongly_measurable_at_filter_nhds_within measurable_set_Icc (f x),
haveI : fact (f x ∈ I) := ⟨h2x⟩,
have : has_deriv_within_at (λ u, ∫ x in f a..u, g x) (g (f x)) I (f x) :=
integral_has_deriv_within_at_right h2g h3g (hg (f x) h2x),
refine (this.scomp x ((hff' x hx).Ioo_of_Ioi hx.2) _).Ioi_of_Ioo hx.2,
rw ← hI,
exact (maps_to_image _ _).mono (Ioo_subset_Icc_self.trans $ Icc_subset_Icc_left hx.1.le)
subset.rfl },
have h_int : interval_integrable (λ (x : ℝ), f' x • (g ∘ f) x) volume a b :=
(hf'.smul (hg.comp hf $ subset_preimage_image f _)).interval_integrable,
simp_rw [integral_eq_sub_of_has_deriv_right h_cont h_der h_int, integral_same, sub_zero],
end
/--
Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`,
and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get
`∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`.
Compared to `interval_integral.integral_comp_smul_deriv` we only require that `g` is continuous on
`f '' [a, b]`.
-/
theorem integral_comp_smul_deriv' {f f' : ℝ → ℝ} {g : ℝ → E}
(h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(h' : continuous_on f' (interval a b)) (hg : continuous_on g (f '' [a, b])) :
∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x :=
integral_comp_smul_deriv'' (λ x hx, (h x hx).continuous_at.continuous_within_at)
(λ x hx, (h x $ Ioo_subset_Icc_self hx).has_deriv_within_at) h' hg
/--
Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`,
and `g` is continuous, then we can substitute `u = f x` to get
`∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`.
-/
theorem integral_comp_smul_deriv {f f' : ℝ → ℝ} {g : ℝ → E}
(h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(h' : continuous_on f' (interval a b)) (hg : continuous g) :
∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x :=
integral_comp_smul_deriv' h h' hg.continuous_on
theorem integral_deriv_comp_smul_deriv' {f f' : ℝ → ℝ} {g g' : ℝ → E}
(hf : continuous_on f [a, b])
(hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x)
(hf' : continuous_on f' [a, b])
(hg : continuous_on g [f a, f b])
(hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x)
(hg' : continuous_on g' (f '' [a, b])) :
∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a :=
begin
rw [integral_comp_smul_deriv'' hf hff' hf' hg',
integral_eq_sub_of_has_deriv_right hg hgg' (hg'.mono _).interval_integrable],
exact intermediate_value_interval hf
end
theorem integral_deriv_comp_smul_deriv {f f' : ℝ → ℝ} {g g' : ℝ → E}
(hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(hg : ∀ x ∈ interval a b, has_deriv_at g (g' (f x)) (f x))
(hf' : continuous_on f' (interval a b)) (hg' : continuous g') :
∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a :=
integral_eq_sub_of_has_deriv_at (λ x hx, (hg x hx).scomp x $ hf x hx)
(hf'.smul (hg'.comp_continuous_on $ has_deriv_at.continuous_on hf)).interval_integrable
end smul
section mul
/--
Change of variables, general form for scalar functions. If `f` is continuous on `[a, b]` and has
continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can
substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`.
-/
theorem integral_comp_mul_deriv'' {f f' g : ℝ → ℝ}
(hf : continuous_on f [a, b])
(hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x)
(hf' : continuous_on f' [a, b])
(hg : continuous_on g (f '' [a, b])) :
∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u :=
by simpa [mul_comm] using integral_comp_smul_deriv'' hf hff' hf' hg
/--
Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`,
and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get
`∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`.
Compared to `interval_integral.integral_comp_mul_deriv` we only require that `g` is continuous on
`f '' [a, b]`.
-/
theorem integral_comp_mul_deriv' {f f' g : ℝ → ℝ}
(h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(h' : continuous_on f' (interval a b)) (hg : continuous_on g (f '' [a, b])) :
∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x :=
by simpa [mul_comm] using integral_comp_smul_deriv' h h' hg
/--
Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`,
and `g` is continuous, then we can substitute `u = f x` to get
`∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`.
-/
theorem integral_comp_mul_deriv {f f' g : ℝ → ℝ}
(h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(h' : continuous_on f' (interval a b)) (hg : continuous g) :
∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x :=
integral_comp_mul_deriv' h h' hg.continuous_on
theorem integral_deriv_comp_mul_deriv' {f f' g g' : ℝ → ℝ}
(hf : continuous_on f [a, b])
(hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x)
(hf' : continuous_on f' [a, b])
(hg : continuous_on g [f a, f b])
(hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x)
(hg' : continuous_on g' (f '' [a, b])) :
∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a :=
by simpa [mul_comm] using integral_deriv_comp_smul_deriv' hf hff' hf' hg hgg' hg'
theorem integral_deriv_comp_mul_deriv {f f' g g' : ℝ → ℝ}
(hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x)
(hg : ∀ x ∈ interval a b, has_deriv_at g (g' (f x)) (f x))
(hf' : continuous_on f' (interval a b)) (hg' : continuous g') :
∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a :=
by simpa [mul_comm] using integral_deriv_comp_smul_deriv hf hg hf' hg'
end mul
end interval_integral
|
8c3f886db68a8999886457e483006655b00a4fe2 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1127.lean | 1c9d8d1ad3d9d6b8ef45c04d98edf4a03888333f | [
"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 | 513 | lean | example: p ∧ q := by first | apply And.intro <;> fail | sorry
variable (p q : Prop)
example (hp : p) : p := by
try trivial -- succeeds, as expected
example : p := by
try trivial -- fails quietly, as expected
admit
example (hp : p) (hq : q) : p ∧ q := by
try trivial -- succeeds, as expected
example (hp : p) : p ∧ q := by
try trivial
admit
example (hq : p) : p ∧ q := by
try trivial
admit
example : p ∧ q := by
try trivial -- fails quietly
admit -- splits goals p and q
|
7ac15049cd021193ba6bf0850483726b77516fbe | 29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f | /Exercises 8/31_homework_sheet.lean | 3dff282f75ffabecf72c18cca84865e20ecea556 | [] | no_license | KjellZijlemaker/Logical_Verification_VU | ced0ba95316a30e3c94ba8eebd58ea004fa6f53b | 4578b93bf1615466996157bb333c84122b201d99 | refs/heads/master | 1,585,966,086,108 | 1,549,187,704,000 | 1,549,187,704,000 | 155,690,284 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,589 | lean | /- Homework 3.1: Program Semantics — Operational Semantics -/
attribute [pattern] or.intro_left or.intro_right
/- Question 1: Semantics of regular expressions
Regular expression are a very popular tool for software development. Often, when textual input needs
to be analyzed it is matched against a regular expression. In this homework, we define the syntax of
regular expressions and what it means that a regular expression matches a string.
We define `regex` to represent the following grammar:
R ::= c — `char`: accepts one character `c`
| ∅ — `nothing`: accepts nothing
| ε — `empty`: accepts the empty string
| R ⬝ R — `concat`: accepts the concatenation of two regexes
| R + R — `alt`: accepts either of two regexes
| R* — `star`: accept arbitrary many repetitions of a regex
Notice the rough correspondence with a WHILE language:
`char` ~ assignment
(`nothing` ~ failing assertion)
`empty` ~ `skip`
`concat` ~ sequential composition
`alt` ~ conditional statement
`star` ~ while loop -/
@[derive decidable_eq]
inductive regex : Type
| char : char → regex
| nothing : regex
| empty : regex
| concat : regex → regex → regex
| alt : regex → regex → regex
| star : regex → regex
/- `accept r s`: the regular expression `r` accepts the string `s` -/
inductive accept : regex → list char → Prop
/- accept one character -/
| char (c : char) :
accept (regex.char c) [c]
/- accept the empty string -/
| empty :
accept regex.empty []
/- accept two concatenated regexes -/
| concat {r₁ r₂ : regex} (s₁ s₂ : list char) (h₁ : accept r₁ s₁) (h₂ : accept r₂ s₂) :
accept (regex.concat r₁ r₂) (s₁ ++ s₂)
/- accept the left alternative -/
| alt_left {r₁ r₂ : regex} (s : list char) (h : accept r₁ s) :
accept (regex.alt r₁ r₂) s
/- accept the right alternative -/
| alt_right {r₁ r₂ : regex} (s : list char) (h : accept r₂ s) :
accept (regex.alt r₁ r₂) s
/- accepts the empty string; this is the base case of `R*` -/
| star_base {r : regex} : accept (regex.star r) []
/- accepts `R` followed again by `R*`; this is the induction step of `R*` -/
| star_step {r : regex} (s s' : list char) (h₁ : accept r s) (h₂ : accept (regex.star r) s') :
accept (regex.star r) (s ++ s')
/- 1.1. Explain why there is no rule for `nothing`. -/
/- Answer: There is no input nor output. So it would be weird to have a rule for nothing, as there is nothing.. -/
/- 1.2. Prove the following inversion rules.
These proofs are very similar to the inversion rules in the lecture and in Question 2.1 of the
exercise. -/
variables {s s₁ s₂ : list char} {r r₁ r₂ : regex} {c : char}
@[simp] lemma accept_char : accept (regex.char c) s ↔ s = [c] :=
begin
apply iff.intro,
intro h,
cases h,
trivial,
intro s,
cases s,
exact accept.char c
end
@[simp] lemma accept_nothing : ¬ accept regex.nothing s:=
begin
intro s,
cases s
end
@[simp] lemma accept_empty : accept regex.empty s ↔ s = [] :=
begin
apply iff.intro,
intro h,
cases h,
trivial,
intro h,
cases h,
exact accept.empty
end
@[simp] lemma accept_concat :
accept (regex.concat r₁ r₂) s ↔ (∃s₁ s₂, accept r₁ s₁ ∧ accept r₂ s₂ ∧ s = s₁ ++ s₂) :=
begin
apply iff.intro,
intro h,
cases h,
apply exists.intro h_s₁,
apply exists.intro h_s₂,
apply and.intro,
assumption,
apply and.intro,
assumption,
end
@[simp] lemma accept_alt :
accept (regex.alt r₁ r₂) s ↔ (accept r₁ s ∨ accept r₂ s) :=
begin
apply iff.intro,
intro a,
cases a,
cases a_h,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inl,
assumption,
apply or.inr,
assumption,
intro b,
cases b,
exact accept.alt_left s b ,
exact accept.alt_right s b
end
lemma accept_star :
accept (regex.star r) s ↔
(s = [] ∨ (∃s₁ s₂, accept r s₁ ∧ accept (regex.star r) s₂ ∧ s = s₁ ++ s₂)) :=
begin
apply iff.intro,
intro acc,
cases s,
simp,
repeat{simp[accept.star_base, accept.star_step]},
repeat {apply exists.intro s_tl},
apply and.intro,
cases s_tl,
cases r,
exact accept.empty, /-Does not work?-/
end
/- 1.3 **optional**. Prove a more sophisticated version of `accept_star`.
The previous rule `accept_star` has the problem that in the induction step, the accepted string for
`r` could be empty. Now we want to **enforce** that the it is not empty, _without loss of
generality_.
*Hint*: In contrast to the other inversion rules, you now need to perform an induction. But the
arguments in our induction hypothesis `accept (regex.star r) (c :: s)` are not variables. So you
will need to generalize them. You can use `cases` to cope with the parts where the regex you handle
is not of the form `regex.star r`. In the `accept.star_step` case, you might need to split on the
string first, and then use `cases` on the second generalized equality. -/
lemma accept_star_cons :
accept (regex.star r) (c :: s) →
∃s₁ s₂ : list char, accept r (c :: s₁) ∧ accept (regex.star r) s₂ ∧ s = s₁ ++ s₂ :=
sorry
/- Question 2 **optional**: Equivalence and matching of regular expressions -/
/- We can prove equivalence between regular expressions, just like between programs. Two
regular expressions are equivalent if they accept the same set of strings. -/
def regex_equiv (r₁ r₂ : regex) : Prop :=
∀s, accept r₁ s ↔ accept r₂ s
local infix ` ≈ ` := regex_equiv
/- Program equivalence is a equivalence relation, i.e. it is reflexive, symmetric, and
transitive. -/
@[refl] lemma regex_equiv.refl :
r ≈ r :=
assume s, by refl
@[symm] lemma regex_equiv.symm :
r₁ ≈ r₂ → r₂ ≈ r₁ :=
assume h s, (h s).symm
@[trans] lemma regex_equiv.trans {r₃} (h₁₂ : r₁ ≈ r₂) (h₂₃ : r₂ ≈ r₃) :
r₁ ≈ r₃ :=
assume s, iff.trans (h₁₂ s) (h₂₃ s)
/- 2.1 **optional**. Prove the following regular expression equivalences. -/
lemma concat_empty_left : regex.concat regex.empty r ≈ r :=
sorry
/- **Hint**: Below, you need to rewrite at some point `x ++ [] = x` (either
using `rw` or `simp`). Depending on your approach you may be required to
introduce an intermediate goal. Remember `simp [...] at h` or `rw [...] at h`
allows you to rewrite a hypothesis. -/
lemma concat_empty_right : regex.concat r regex.empty ≈ r :=
sorry
lemma alt_idem : regex.alt r r ≈ r :=
sorry
lemma star_unfold : regex.star r ≈ regex.alt regex.empty (regex.concat r (regex.star r)) :=
sorry
/- **Hint**: For the next proof, you will probably need induction. -/
lemma star_congr_aux (hr : r₁ ≈ r₂) : accept (regex.star r₁) s → accept (regex.star r₂) s :=
sorry
/- **Hint**: For the next proof, you will probably need `star_congr_aux` and `regex.symm`. -/
lemma star_congr (hr : r₁ ≈ r₂) : regex.star r₁ ≈ regex.star r₂ :=
sorry
/- The `match_regex` function below matches a regular expression using Brzozowski derivatives. See
https://en.wikipedia.org/wiki/Brzozowski_derivative for details. -/
@[simp] def accepts_empty : regex → bool
| (regex.char c) := ff
| regex.nothing := ff
| regex.empty := tt
| (regex.concat r₁ r₂) := accepts_empty r₁ && accepts_empty r₂
| (regex.alt r₁ r₂) := accepts_empty r₁ || accepts_empty r₂
| (regex.star r) := tt
lemma accepts_empty_iff : ∀r : regex, accepts_empty r = tt ↔ accept r []
| (regex.char c) := by simp
| regex.nothing := by simp
| regex.empty := by simp
| (regex.concat r₁ r₂) :=
begin
simp [accepts_empty_iff r₁, accepts_empty_iff r₂],
exact iff.intro
(assume ⟨h₁, h₂⟩, ⟨[], [], h₁, h₂, rfl⟩)
(assume h, match h with ⟨[], [], h₁, h₂, rfl⟩ := ⟨h₁, h₂⟩ end)
end
| (regex.alt r₁ r₂) := by simp [accepts_empty_iff r₁, accepts_empty_iff r₂]
| (regex.star r) := by simp; constructor
@[simp] def deriv : regex → char → regex
| (regex.char c') c := if c = c' then regex.empty else regex.nothing
| regex.nothing _ := regex.nothing
| regex.empty _ := regex.nothing
| (regex.concat r₁ r₂) c :=
if accepts_empty r₁ = tt
then regex.alt (regex.concat (deriv r₁ c) r₂) (deriv r₂ c)
else regex.concat (deriv r₁ c) r₂
| (regex.alt r₁ r₂) c := regex.alt (deriv r₁ c) (deriv r₂ c)
| (regex.star r) c := regex.concat (deriv r c) (regex.star r)
def match_regex : regex → list char → bool
| r [] := accepts_empty r
| r (c :: s) := match_regex (deriv r c) s
/- 2.2 **optional**. Fill in the `sorry` placeholders below. -/
lemma accept_deriv : ∀r : regex, ∀c s, accept (deriv r c) s ↔ accept r (c :: s)
| (regex.char c') c s :=
sorry
| regex.nothing c s :=
sorry
| regex.empty c s :=
sorry
| (regex.concat r₁ r₂) c s :=
begin
by_cases h₁ : accepts_empty r₁ = tt;
simp [h₁, accept_deriv r₂, accept_deriv r₁];
simp [accepts_empty_iff] at h₁,
sorry, -- in one direction, you will need to make a case distinction on the string
sorry
end
| (regex.alt r₁ r₂) c s := by simp [accept_deriv r₂, accept_deriv r₁]
| (regex.star r) c s :=
begin
rw [accept_star],
sorry -- for one direction, you will probably need `accept_star_cons`
end
|
4bb1082d8c19442be2cfc0d5f3aa5a6a1ace3f14 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/inner_product_space/gram_schmidt_ortho.lean | 12eff1b6066b1f5a2ce4fe10078e32f29e567691 | [
"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 | 11,764 | lean | /-
Copyright (c) 2022 Jiale Miao. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jiale Miao, Kevin Buzzard, Alexander Bentkamp
-/
import analysis.inner_product_space.projection
import order.well_founded_set
import analysis.inner_product_space.pi_L2
/-!
# Gram-Schmidt Orthogonalization and Orthonormalization
In this file we introduce Gram-Schmidt Orthogonalization and Orthonormalization.
The Gram-Schmidt process takes a set of vectors as input
and outputs a set of orthogonal vectors which have the same span.
## Main results
- `gram_schmidt` : the Gram-Schmidt process
- `gram_schmidt_orthogonal` :
`gram_schmidt` produces an orthogonal system of vectors.
- `span_gram_schmidt` :
`gram_schmidt` preserves span of vectors.
- `gram_schmidt_ne_zero` :
If the input vectors of `gram_schmidt` are linearly independent,
then the output vectors are non-zero.
- `gram_schmidt_basis` :
The basis produced by the Gram-Schmidt process when given a basis as input.
- `gram_schmidt_normed` :
the normalized `gram_schmidt` (i.e each vector in `gram_schmidt_normed` has unit length.)
- `gram_schmidt_orthornormal` :
`gram_schmidt_normed` produces an orthornormal system of vectors.
## TODO
Construct a version with an orthonormal basis from Gram-Schmidt process.
-/
open_locale big_operators
open finset
variables (𝕜 : Type*) {E : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E]
variables {ι : Type*} [linear_order ι] [locally_finite_order_bot ι] [is_well_order ι (<)]
local attribute [instance] is_well_order.to_has_well_founded
local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y
/-- The Gram-Schmidt process takes a set of vectors as input
and outputs a set of orthogonal vectors which have the same span. -/
noncomputable def gram_schmidt (f : ι → E) : ι → E
| n := f n - ∑ i : Iio n, orthogonal_projection (𝕜 ∙ gram_schmidt i) (f n)
using_well_founded { dec_tac := `[exact mem_Iio.1 i.2] }
/-- This lemma uses `∑ i in` instead of `∑ i :`.-/
lemma gram_schmidt_def (f : ι → E) (n : ι):
gram_schmidt 𝕜 f n = f n - ∑ i in Iio n,
orthogonal_projection (𝕜 ∙ gram_schmidt 𝕜 f i) (f n) :=
by { rw [←sum_attach, attach_eq_univ, gram_schmidt], refl }
lemma gram_schmidt_def' (f : ι → E) (n : ι):
f n = gram_schmidt 𝕜 f n + ∑ i in Iio n,
orthogonal_projection (𝕜 ∙ gram_schmidt 𝕜 f i) (f n) :=
by rw [gram_schmidt_def, sub_add_cancel]
@[simp] lemma gram_schmidt_zero {ι : Type*} [linear_order ι] [locally_finite_order ι]
[order_bot ι] [is_well_order ι (<)] (f : ι → E) : gram_schmidt 𝕜 f ⊥ = f ⊥ :=
by rw [gram_schmidt_def, Iio_eq_Ico, finset.Ico_self, finset.sum_empty, sub_zero]
/-- **Gram-Schmidt Orthogonalisation**:
`gram_schmidt` produces an orthogonal system of vectors. -/
theorem gram_schmidt_orthogonal (f : ι → E) {a b : ι} (h₀ : a ≠ b) :
⟪gram_schmidt 𝕜 f a, gram_schmidt 𝕜 f b⟫ = 0 :=
begin
suffices : ∀ a b : ι, a < b → ⟪gram_schmidt 𝕜 f a, gram_schmidt 𝕜 f b⟫ = 0,
{ cases h₀.lt_or_lt with ha hb,
{ exact this _ _ ha, },
{ rw inner_eq_zero_sym,
exact this _ _ hb, }, },
clear h₀ a b,
intros a b h₀,
revert a,
apply well_founded.induction (@is_well_founded.wf ι (<) _) b,
intros b ih a h₀,
simp only [gram_schmidt_def 𝕜 f b, inner_sub_right, inner_sum,
orthogonal_projection_singleton, inner_smul_right],
rw finset.sum_eq_single_of_mem a (finset.mem_Iio.mpr h₀),
{ by_cases h : gram_schmidt 𝕜 f a = 0,
{ simp only [h, inner_zero_left, zero_div, zero_mul, sub_zero], },
{ rw [← inner_self_eq_norm_sq_to_K, div_mul_cancel, sub_self],
rwa [ne.def, inner_self_eq_zero], }, },
simp_intros i hi hia only [finset.mem_range],
simp only [mul_eq_zero, div_eq_zero_iff, inner_self_eq_zero],
right,
cases hia.lt_or_lt with hia₁ hia₂,
{ rw inner_eq_zero_sym,
exact ih a h₀ i hia₁ },
{ exact ih i (mem_Iio.1 hi) a hia₂ }
end
/-- This is another version of `gram_schmidt_orthogonal` using `pairwise` instead. -/
theorem gram_schmidt_pairwise_orthogonal (f : ι → E) :
pairwise (λ a b, ⟪gram_schmidt 𝕜 f a, gram_schmidt 𝕜 f b⟫ = 0) :=
λ a b, gram_schmidt_orthogonal 𝕜 f
open submodule set order
lemma mem_span_gram_schmidt (f : ι → E) {i j : ι} (hij : i ≤ j) :
f i ∈ span 𝕜 (gram_schmidt 𝕜 f '' Iic j) :=
begin
rw [gram_schmidt_def' 𝕜 f i],
simp_rw orthogonal_projection_singleton,
exact submodule.add_mem _ (subset_span $ mem_image_of_mem _ hij)
(submodule.sum_mem _ $ λ k hk, smul_mem (span 𝕜 (gram_schmidt 𝕜 f '' Iic j)) _ $
subset_span $ mem_image_of_mem (gram_schmidt 𝕜 f) $ (finset.mem_Iio.1 hk).le.trans hij),
end
lemma gram_schmidt_mem_span (f : ι → E) :
∀ {j i}, i ≤ j → gram_schmidt 𝕜 f i ∈ span 𝕜 (f '' Iic j)
| j := λ i hij,
begin
rw [gram_schmidt_def 𝕜 f i],
simp_rw orthogonal_projection_singleton,
refine submodule.sub_mem _ (subset_span (mem_image_of_mem _ hij))
(submodule.sum_mem _ $ λ k hk, _),
let hkj : k < j := (finset.mem_Iio.1 hk).trans_le hij,
exact smul_mem _ _ (span_mono (image_subset f $ Iic_subset_Iic.2 hkj.le) $
gram_schmidt_mem_span le_rfl),
end
using_well_founded { dec_tac := `[assumption] }
lemma span_gram_schmidt_Iic (f : ι → E) (c : ι) :
span 𝕜 (gram_schmidt 𝕜 f '' Iic c) = span 𝕜 (f '' Iic c) :=
span_eq_span (set.image_subset_iff.2 $ λ i, gram_schmidt_mem_span _ _) $
set.image_subset_iff.2 $ λ i, mem_span_gram_schmidt _ _
lemma span_gram_schmidt_Iio (f : ι → E) (c : ι) :
span 𝕜 (gram_schmidt 𝕜 f '' Iio c) = span 𝕜 (f '' Iio c) :=
span_eq_span
(set.image_subset_iff.2 $ λ i hi, span_mono (image_subset _ $ Iic_subset_Iio.2 hi) $
gram_schmidt_mem_span _ _ le_rfl) $
set.image_subset_iff.2 $ λ i hi, span_mono (image_subset _ $ Iic_subset_Iio.2 hi) $
mem_span_gram_schmidt _ _ le_rfl
/-- `gram_schmidt` preserves span of vectors. -/
lemma span_gram_schmidt (f : ι → E) : span 𝕜 (range (gram_schmidt 𝕜 f)) = span 𝕜 (range f) :=
span_eq_span (range_subset_iff.2 $ λ i, span_mono (image_subset_range _ _) $
gram_schmidt_mem_span _ _ le_rfl) $
range_subset_iff.2 $ λ i, span_mono (image_subset_range _ _) $ mem_span_gram_schmidt _ _ le_rfl
lemma gram_schmidt_ne_zero_coe
(f : ι → E) (n : ι) (h₀ : linear_independent 𝕜 (f ∘ (coe : set.Iic n → ι))) :
gram_schmidt 𝕜 f n ≠ 0 :=
begin
by_contra h,
have h₁ : f n ∈ span 𝕜 (f '' Iio n),
{ rw [← span_gram_schmidt_Iio 𝕜 f n, gram_schmidt_def' _ f, h, zero_add],
apply submodule.sum_mem _ _,
simp_intros a ha only [finset.mem_Ico],
simp only [set.mem_image, set.mem_Iio, orthogonal_projection_singleton],
apply submodule.smul_mem _ _ _,
rw finset.mem_Iio at ha,
refine subset_span ⟨a, ha, by refl⟩ },
have h₂ : (f ∘ (coe : set.Iic n → ι)) ⟨n, le_refl n⟩
∈ span 𝕜 (f ∘ (coe : set.Iic n → ι) '' Iio ⟨n, le_refl n⟩),
{ rw [image_comp],
convert h₁ using 3,
ext i,
simpa using @le_of_lt _ _ i n },
apply linear_independent.not_mem_span_image h₀ _ h₂,
simp only [set.mem_Iio, lt_self_iff_false, not_false_iff]
end
/-- If the input vectors of `gram_schmidt` are linearly independent,
then the output vectors are non-zero. -/
lemma gram_schmidt_ne_zero (f : ι → E) (n : ι) (h₀ : linear_independent 𝕜 f) :
gram_schmidt 𝕜 f n ≠ 0 :=
gram_schmidt_ne_zero_coe _ _ _ (linear_independent.comp h₀ _ subtype.coe_injective)
/-- `gram_schmidt` produces a triangular matrix of vectors when given a basis. -/
lemma gram_schmidt_triangular {i j : ι} (hij : i < j) (b : basis ι 𝕜 E) :
b.repr (gram_schmidt 𝕜 b i) j = 0 :=
begin
have : gram_schmidt 𝕜 b i ∈ span 𝕜 (gram_schmidt 𝕜 b '' set.Iio j),
from subset_span ((set.mem_image _ _ _).2 ⟨i, hij, rfl⟩),
have : gram_schmidt 𝕜 b i ∈ span 𝕜 (b '' set.Iio j),
by rwa [← span_gram_schmidt_Iio 𝕜 b j],
have : ↑(((b.repr) (gram_schmidt 𝕜 b i)).support) ⊆ set.Iio j,
from basis.repr_support_subset_of_mem_span b (set.Iio j) this,
exact (finsupp.mem_supported' _ _).1
((finsupp.mem_supported 𝕜 _).2 this) j (not_mem_Iio.2 (le_refl j)),
end
/-- `gram_schmidt` produces linearly independent vectors when given linearly independent vectors. -/
lemma gram_schmidt_linear_independent (f : ι → E) (h₀ : linear_independent 𝕜 f) :
linear_independent 𝕜 (gram_schmidt 𝕜 f) :=
linear_independent_of_ne_zero_of_inner_eq_zero
(λ i, gram_schmidt_ne_zero _ _ _ h₀) (λ i j, gram_schmidt_orthogonal 𝕜 f)
/-- When given a basis, `gram_schmidt` produces a basis. -/
noncomputable def gram_schmidt_basis (b : basis ι 𝕜 E) : basis ι 𝕜 E :=
basis.mk
(gram_schmidt_linear_independent 𝕜 b b.linear_independent)
((span_gram_schmidt 𝕜 b).trans b.span_eq).ge
lemma coe_gram_schmidt_basis (b : basis ι 𝕜 E) :
(gram_schmidt_basis 𝕜 b : ι → E) = gram_schmidt 𝕜 b := basis.coe_mk _ _
/-- the normalized `gram_schmidt`
(i.e each vector in `gram_schmidt_normed` has unit length.) -/
noncomputable def gram_schmidt_normed (f : ι → E) (n : ι) : E :=
(∥gram_schmidt 𝕜 f n∥ : 𝕜)⁻¹ • (gram_schmidt 𝕜 f n)
lemma gram_schmidt_normed_unit_length_coe
(f : ι → E) (n : ι) (h₀ : linear_independent 𝕜 (f ∘ (coe : set.Iic n → ι))) :
∥gram_schmidt_normed 𝕜 f n∥ = 1 :=
by simp only [gram_schmidt_ne_zero_coe 𝕜 f n h₀,
gram_schmidt_normed, norm_smul_inv_norm, ne.def, not_false_iff]
lemma gram_schmidt_normed_unit_length (f : ι → E) (n : ι) (h₀ : linear_independent 𝕜 f) :
∥gram_schmidt_normed 𝕜 f n∥ = 1 :=
gram_schmidt_normed_unit_length_coe _ _ _ (linear_independent.comp h₀ _ subtype.coe_injective)
/-- **Gram-Schmidt Orthonormalization**:
`gram_schmidt_normed` produces an orthornormal system of vectors. -/
theorem gram_schmidt_orthonormal (f : ι → E) (h₀ : linear_independent 𝕜 f) :
orthonormal 𝕜 (gram_schmidt_normed 𝕜 f) :=
begin
unfold orthonormal,
split,
{ simp only [gram_schmidt_normed_unit_length, h₀, eq_self_iff_true, implies_true_iff], },
{ intros i j hij,
simp only [gram_schmidt_normed, inner_smul_left, inner_smul_right, is_R_or_C.conj_inv,
is_R_or_C.conj_of_real, mul_eq_zero, inv_eq_zero, is_R_or_C.of_real_eq_zero, norm_eq_zero],
repeat { right },
exact gram_schmidt_orthogonal 𝕜 f hij }
end
lemma span_gram_schmidt_normed (f : ι → E) (s : set ι) :
span 𝕜 (gram_schmidt_normed 𝕜 f '' s) = span 𝕜 (gram_schmidt 𝕜 f '' s) :=
begin
refine span_eq_span (set.image_subset_iff.2 $ λ i hi, smul_mem _ _ $ subset_span $
mem_image_of_mem _ hi)
(set.image_subset_iff.2 $ λ i hi, span_mono (image_subset _ $ singleton_subset_set_iff.2 hi) _),
simp only [coe_singleton, set.image_singleton],
by_cases h : gram_schmidt 𝕜 f i = 0,
{ simp [h] },
{ refine mem_span_singleton.2 ⟨∥gram_schmidt 𝕜 f i∥, smul_inv_smul₀ _ _⟩,
exact_mod_cast (norm_ne_zero_iff.2 h) }
end
lemma span_gram_schmidt_normed_range (f : ι → E) :
span 𝕜 (range (gram_schmidt_normed 𝕜 f)) = span 𝕜 (range (gram_schmidt 𝕜 f)) :=
by simpa only [image_univ.symm] using span_gram_schmidt_normed 𝕜 f univ
/-- When given a basis, `gram_schmidt_normed` produces an orthonormal basis. -/
noncomputable def gram_schmidt_orthonormal_basis [fintype ι] (b : basis ι 𝕜 E) :
orthonormal_basis ι 𝕜 E :=
orthonormal_basis.mk
(gram_schmidt_orthonormal 𝕜 b b.linear_independent)
(((span_gram_schmidt_normed_range 𝕜 b).trans (span_gram_schmidt 𝕜 b)).trans b.span_eq).ge
|
bdf97736d223ba3abb9d5acfb24766bd253ecdcb | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Util/OccursCheck.lean | d6b33264843e4c5cf9950c3505103af09e62f494 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,597 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.MetavarContext
namespace Lean
/--
Return true if `e` does **not** contain `mvarId` directly or indirectly
This function considers assigments and delayed assignments. -/
partial def occursCheck [Monad m] [MonadMCtx m] (mvarId : MVarId) (e : Expr) : m Bool := do
if !e.hasExprMVar then
return true
else
match (← visit e |>.run |>.run {}) with
| (.ok .., _) => return true
| (.error .., _) => return false
where
visitMVar (mvarId' : MVarId) : ExceptT Unit (StateT ExprSet m) Unit := do
if mvarId == mvarId' then
throw () -- found
else
match (← getExprMVarAssignment? mvarId') with
| some v => visit v
| none =>
match (← getDelayedMVarAssignment? mvarId') with
| some d => visitMVar d.mvarIdPending
| none => return ()
visit (e : Expr) : ExceptT Unit (StateT ExprSet m) Unit := do
if !e.hasExprMVar then
return ()
else if (← get).contains e then
return ()
else
modify fun s => s.insert e
match e with
| Expr.proj _ _ s => visit s
| Expr.forallE _ d b _ => visit d; visit b
| Expr.lam _ d b _ => visit d; visit b
| Expr.letE _ t v b _ => visit t; visit v; visit b
| Expr.mdata _ b => visit b
| Expr.app f a => visit f; visit a
| Expr.mvar mvarId => visitMVar mvarId
| _ => return ()
end Lean
|
8956fd6b0c3f686c1d4c4ab6c8033231793aa9c9 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/category_theory/limits/shapes/kernels.lean | 3b1e8d9a27b89762eae41784804bead10c186a46 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 25,521 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Markus Himmel
-/
import category_theory.limits.shapes.zero
/-!
# Kernels and cokernels
In a category with zero morphisms, the kernel of a morphism `f : X ⟶ Y` is the equalizer of `f`
and `0 : X ⟶ Y`. (Similarly the cokernel is the coequalizer.)
The basic definitions are
* `kernel : (X ⟶ Y) → C`
* `kernel.ι : kernel f ⟶ X`
* `kernel.condition : kernel.ι f ≫ f = 0` and
* `kernel.lift (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f` (as well as the dual versions)
## Main statements
Besides the definition and lifts, we prove
* `kernel.ι_zero_is_iso`: a kernel map of a zero morphism is an isomorphism
* `kernel.eq_zero_of_epi_kernel`: if `kernel.ι f` is an epimorphism, then `f = 0`
* `kernel.of_mono`: the kernel of a monomorphism is the zero object
* `kernel.lift_mono`: the lift of a monomorphism `k : W ⟶ X` such that `k ≫ f = 0`
is still a monomorphism
* `kernel.is_limit_cone_zero_cone`: if our category has a zero object, then the map from the zero
obect is a kernel map of any monomorphism
* `kernel.ι_of_zero`: `kernel.ι (0 : X ⟶ Y)` is an isomorphism
and the corresponding dual statements.
## Future work
* TODO: connect this with existing working in the group theory and ring theory libraries.
## Implementation notes
As with the other special shapes in the limits library, all the definitions here are given as
`abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about
general limits can be used.
## References
* [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2]
-/
universes v u
open category_theory
open category_theory.limits.walking_parallel_pair
namespace category_theory.limits
variables {C : Type u} [category.{v} C]
variables [has_zero_morphisms C]
/-- A morphism `f` has a kernel if the functor `parallel_pair f 0` has a limit. -/
abbreviation has_kernel {X Y : C} (f : X ⟶ Y) : Type (max u v) := has_limit (parallel_pair f 0)
/-- A morphism `f` has a cokernel if the functor `parallel_pair f 0` has a colimit. -/
abbreviation has_cokernel {X Y : C} (f : X ⟶ Y) : Type (max u v) := has_colimit (parallel_pair f 0)
variables {X Y : C} (f : X ⟶ Y)
section
/-- A kernel fork is just a fork where the second morphism is a zero morphism. -/
abbreviation kernel_fork := fork f 0
variables {f}
@[simp, reassoc] lemma kernel_fork.condition (s : kernel_fork f) : fork.ι s ≫ f = 0 :=
by erw [fork.condition, has_zero_morphisms.comp_zero]
@[simp] lemma kernel_fork.app_one (s : kernel_fork f) : s.π.app one = 0 :=
by rw [←fork.app_zero_left, kernel_fork.condition]
/-- A morphism `ι` satisfying `ι ≫ f = 0` determines a kernel fork over `f`. -/
abbreviation kernel_fork.of_ι {Z : C} (ι : Z ⟶ X) (w : ι ≫ f = 0) : kernel_fork f :=
fork.of_ι ι $ by rw [w, has_zero_morphisms.comp_zero]
@[simp] lemma kernel_fork.ι_of_ι {X Y P : C} (f : X ⟶ Y) (ι : P ⟶ X) (w : ι ≫ f = 0) :
fork.ι (kernel_fork.of_ι ι w) = ι := rfl
/-- If `s` is a limit kernel fork and `k : W ⟶ X` satisfies ``k ≫ f = 0`, then there is some
`l : W ⟶ s.X` such that `l ≫ fork.ι s = k`. -/
def kernel_fork.is_limit.lift' {s : kernel_fork f} (hs : is_limit s) {W : C} (k : W ⟶ X)
(h : k ≫ f = 0) : {l : W ⟶ s.X // l ≫ fork.ι s = k} :=
⟨hs.lift $ kernel_fork.of_ι _ h, hs.fac _ _⟩
/-- This is a slightly more convenient method to verify that a kernel fork is a limit cone. It
only asks for a proof of facts that carry any mathematical content -/
def is_limit_aux (t : kernel_fork f)
(lift : Π (s : kernel_fork f), s.X ⟶ t.X)
(fac : ∀ (s : kernel_fork f), lift s ≫ t.ι = s.ι)
(uniq : ∀ (s : kernel_fork f) (m : s.X ⟶ t.X) (w : m ≫ t.ι = s.ι), m = lift s) :
is_limit t :=
{ lift := lift,
fac' := λ s j, by { cases j, { exact fac s, }, { simp, }, },
uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.zero), }
/--
This is a more convenient formulation to show that a `kernel_fork` constructed using
`kernel_fork.of_ι` is a limit cone.
-/
def is_limit.of_ι {W : C} (g : W ⟶ X) (eq : g ≫ f = 0)
(lift : Π {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), W' ⟶ W)
(fac : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), lift g' eq' ≫ g = g')
(uniq : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0) (m : W' ⟶ W) (w : m ≫ g = g'), m = lift g' eq') :
is_limit (kernel_fork.of_ι g eq) :=
is_limit_aux _ (λ s, lift s.ι s.condition) (λ s, fac s.ι s.condition) (λ s, uniq s.ι s.condition)
end
section
variables [has_kernel f]
/-- The kernel of a morphism, expressed as the equalizer with the 0 morphism. -/
abbreviation kernel : C := equalizer f 0
/-- The map from `kernel f` into the source of `f`. -/
abbreviation kernel.ι : kernel f ⟶ X := equalizer.ι f 0
@[simp, reassoc] lemma kernel.condition : kernel.ι f ≫ f = 0 :=
kernel_fork.condition _
/-- Given any morphism `k : W ⟶ X` satisfying `k ≫ f = 0`, `k` factors through `kernel.ι f`
via `kernel.lift : W ⟶ kernel f`. -/
abbreviation kernel.lift {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f :=
limit.lift (parallel_pair f 0) (kernel_fork.of_ι k h)
@[simp, reassoc]
lemma kernel.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : kernel.lift f k h ≫ kernel.ι f = k :=
limit.lift_π _ _
@[simp]
lemma kernel.lift_zero {W : C} {h} : kernel.lift f (0 : W ⟶ X) h = 0 :=
by { ext, simp, }
instance kernel.lift_mono {W : C} (k : W ⟶ X) (h : k ≫ f = 0) [mono k] : mono (kernel.lift f k h) :=
⟨λ Z g g' w,
begin
replace w := w =≫ kernel.ι f,
simp only [category.assoc, kernel.lift_ι] at w,
exact (cancel_mono k).1 w,
end⟩
/-- Any morphism `k : W ⟶ X` satisfying `k ≫ f = 0` induces a morphism `l : W ⟶ kernel f` such that
`l ≫ kernel.ι f = k`. -/
def kernel.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : {l : W ⟶ kernel f // l ≫ kernel.ι f = k} :=
⟨kernel.lift f k h, kernel.lift_ι _ _ _⟩
/-- Every kernel of the zero morphism is an isomorphism -/
instance kernel.ι_zero_is_iso [has_kernel (0 : X ⟶ Y)] :
is_iso (kernel.ι (0 : X ⟶ Y)) :=
equalizer.ι_of_self _
lemma eq_zero_of_epi_kernel [epi (kernel.ι f)] : f = 0 :=
(cancel_epi (kernel.ι f)).1 (by simp)
/-- The kernel of a zero morphism is isomorphic to the source. -/
def kernel_zero_iso_source [has_kernel (0 : X ⟶ Y)] : kernel (0 : X ⟶ Y) ≅ X :=
equalizer.iso_source_of_self 0
@[simp] lemma kernel_zero_iso_source_hom [has_kernel (0 : X ⟶ Y)] :
kernel_zero_iso_source.hom = kernel.ι (0 : X ⟶ Y) := rfl
@[simp] lemma kernel_zero_iso_source_inv [has_kernel (0 : X ⟶ Y)] :
kernel_zero_iso_source.inv = kernel.lift (0 : X ⟶ Y) (𝟙 X) (by simp) := rfl
/-- If two morphisms are known to be equal, then their kernels are isomorphic. -/
def kernel_iso_of_eq {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) :
kernel f ≅ kernel g :=
has_limit.iso_of_nat_iso (by simp[h])
@[simp]
lemma kernel_iso_of_eq_refl {h : f = f} : kernel_iso_of_eq h = iso.refl (kernel f) :=
by { ext, simp [kernel_iso_of_eq], }
@[simp]
lemma kernel_iso_of_eq_trans {f g h : X ⟶ Y} [has_kernel f] [has_kernel g] [has_kernel h]
(w₁ : f = g) (w₂ : g = h) :
kernel_iso_of_eq w₁ ≪≫ kernel_iso_of_eq w₂ = kernel_iso_of_eq (w₁.trans w₂) :=
by { unfreezingI { induction w₁, induction w₂, }, ext, simp [kernel_iso_of_eq], }
variables {f}
lemma kernel_not_epi_of_nonzero (w : f ≠ 0) : ¬epi (kernel.ι f) :=
λ I, by exactI w (eq_zero_of_epi_kernel f)
lemma kernel_not_iso_of_nonzero (w : f ≠ 0) : (is_iso (kernel.ι f)) → false :=
λ I, kernel_not_epi_of_nonzero w $ by { resetI, apply_instance }
/--
When `g` is an isomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `f`.
-/
@[simps]
def kernel_comp_is_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [has_kernel f] [is_iso g] :
kernel (f ≫ g) ≅ kernel f :=
{ hom := kernel.lift _ (kernel.ι _) (by { rw [←cancel_mono g], simp, }),
inv := kernel.lift _ (kernel.ι _) (by simp), }
lemma kernel_comp_is_iso_hom_comp_kernel_ι {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [has_kernel f] [is_iso g] :
(kernel_comp_is_iso f g).hom ≫ kernel.ι f = kernel.ι (f ≫ g) :=
by simp
lemma kernel_comp_is_iso_inv_comp_kernel_ι {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [has_kernel f] [is_iso g] :
(kernel_comp_is_iso f g).inv ≫ kernel.ι (f ≫ g) = kernel.ι f :=
by simp
/--
When `f` is an isomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `g`.
-/
@[simps]
def kernel_is_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [is_iso f] [has_kernel g] :
kernel (f ≫ g) ≅ kernel g :=
{ hom := kernel.lift _ (kernel.ι _ ≫ f) (by simp),
inv := kernel.lift _ (kernel.ι _ ≫ inv f) (by simp), }
lemma kernel_is_iso_comp_hom_comp_kernel_ι {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [is_iso f] [has_kernel g] :
(kernel_is_iso_comp f g).hom ≫ kernel.ι g = kernel.ι (f ≫ g) ≫ f :=
by simp
lemma kernel_is_iso_comp_inv_comp_kernel_ι {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_kernel (f ≫ g)] [is_iso f] [has_kernel g] :
(kernel_is_iso_comp f g).inv ≫ kernel.ι (f ≫ g) = kernel.ι g ≫ (inv f) :=
by simp
end
section has_zero_object
variables [has_zero_object C]
local attribute [instance] has_zero_object.has_zero
/-- The morphism from the zero object determines a cone on a kernel diagram -/
def kernel.zero_cone : cone (parallel_pair f 0) :=
{ X := 0,
π := { app := λ j, 0 }}
/-- The map from the zero object is a kernel of a monomorphism -/
def kernel.is_limit_cone_zero_cone [mono f] : is_limit (kernel.zero_cone f) :=
fork.is_limit.mk _ (λ s, 0)
(λ s, by { erw has_zero_morphisms.zero_comp,
convert (zero_of_comp_mono f _).symm,
exact kernel_fork.condition _ })
(λ _ _ _, zero_of_to_zero _)
/-- The kernel of a monomorphism is isomorphic to the zero object -/
def kernel.of_mono [has_kernel f] [mono f] : kernel f ≅ 0 :=
functor.map_iso (cones.forget _) $ is_limit.unique_up_to_iso
(limit.is_limit (parallel_pair f 0)) (kernel.is_limit_cone_zero_cone f)
/-- The kernel morphism of a monomorphism is a zero morphism -/
lemma kernel.ι_of_mono [has_kernel f] [mono f] : kernel.ι f = 0 :=
zero_of_source_iso_zero _ (kernel.of_mono f)
end has_zero_object
section transport
/-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then any kernel of `f` is a kernel of `l`.-/
def is_kernel.of_comp_iso {Z : C} (l : X ⟶ Z) (i : Z ≅ Y) (h : l ≫ i.hom = f)
{s : kernel_fork f} (hs : is_limit s) : is_limit (kernel_fork.of_ι (fork.ι s) $
show fork.ι s ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) :=
fork.is_limit.mk _
(λ s, hs.lift $ kernel_fork.of_ι (fork.ι s) $ by simp [←h])
(λ s, by simp)
(λ s m h, by { apply fork.is_limit.hom_ext hs, simpa using h walking_parallel_pair.zero })
/-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then the kernel of `f` is a kernel of `l`.-/
def kernel.of_comp_iso [has_kernel f]
{Z : C} (l : X ⟶ Z) (i : Z ≅ Y) (h : l ≫ i.hom = f) :
is_limit (kernel_fork.of_ι (kernel.ι f) $
show kernel.ι f ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) :=
is_kernel.of_comp_iso f l i h $ limit.is_limit _
/-- If `s` is any limit kernel cone over `f` and if `i` is an isomorphism such that
`i.hom ≫ s.ι = l`, then `l` is a kernel of `f`. -/
def is_kernel.iso_kernel {Z : C} (l : Z ⟶ X) {s : kernel_fork f} (hs : is_limit s)
(i : Z ≅ s.X) (h : i.hom ≫ fork.ι s = l) : is_limit (kernel_fork.of_ι l $
show l ≫ f = 0, by simp [←h]) :=
is_limit.of_iso_limit hs $ cones.ext i.symm $ λ j,
by { cases j, { exact (iso.eq_inv_comp i).2 h }, { simp } }
/-- If `i` is an isomorphism such that `i.hom ≫ kernel.ι f = l`, then `l` is a kernel of `f`. -/
def kernel.iso_kernel [has_kernel f]
{Z : C} (l : Z ⟶ X) (i : Z ≅ kernel f) (h : i.hom ≫ kernel.ι f = l) :
is_limit (kernel_fork.of_ι l $ by simp [←h]) :=
is_kernel.iso_kernel f l (limit.is_limit _) i h
end transport
section
variables (X Y)
/-- The kernel morphism of a zero morphism is an isomorphism -/
def kernel.ι_of_zero [has_kernel (0 : X ⟶ Y)] : is_iso (kernel.ι (0 : X ⟶ Y)) :=
equalizer.ι_of_self _
end
section
/-- A cokernel cofork is just a cofork where the second morphism is a zero morphism. -/
abbreviation cokernel_cofork := cofork f 0
variables {f}
@[simp, reassoc] lemma cokernel_cofork.condition (s : cokernel_cofork f) : f ≫ cofork.π s = 0 :=
by rw [cofork.condition, has_zero_morphisms.zero_comp]
@[simp] lemma cokernel_cofork.app_zero (s : cokernel_cofork f) : s.ι.app zero = 0 :=
by rw [←cofork.left_app_one, cokernel_cofork.condition]
/-- A morphism `π` satisfying `f ≫ π = 0` determines a cokernel cofork on `f`. -/
abbreviation cokernel_cofork.of_π {Z : C} (π : Y ⟶ Z) (w : f ≫ π = 0) : cokernel_cofork f :=
cofork.of_π π $ by rw [w, has_zero_morphisms.zero_comp]
@[simp] lemma cokernel_cofork.π_of_π {X Y P : C} (f : X ⟶ Y) (π : Y ⟶ P) (w : f ≫ π = 0) :
cofork.π (cokernel_cofork.of_π π w) = π := rfl
/-- If `s` is a colimit cokernel cofork, then every `k : Y ⟶ W` satisfying `f ≫ k = 0` induces
`l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/
def cokernel_cofork.is_colimit.desc' {s : cokernel_cofork f} (hs : is_colimit s) {W : C} (k : Y ⟶ W)
(h : f ≫ k = 0) : {l : s.X ⟶ W // cofork.π s ≫ l = k} :=
⟨hs.desc $ cokernel_cofork.of_π _ h, hs.fac _ _⟩
/--
This is a slightly more convenient method to verify that a cokernel cofork is a colimit cocone.
It only asks for a proof of facts that carry any mathematical content -/
def is_colimit_aux (t : cokernel_cofork f)
(desc : Π (s : cokernel_cofork f), t.X ⟶ s.X)
(fac : ∀ (s : cokernel_cofork f), t.π ≫ desc s = s.π)
(uniq : ∀ (s : cokernel_cofork f) (m : t.X ⟶ s.X) (w : t.π ≫ m = s.π), m = desc s) :
is_colimit t :=
{ desc := desc,
fac' := λ s j, by { cases j, { simp, }, { exact fac s, }, },
uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.one), }
/--
This is a more convenient formulation to show that a `cokernel_cofork` constructed using
`cokernel_cofork.of_π` is a limit cone.
-/
def is_colimit.of_π {Z : C} (g : Y ⟶ Z) (eq : f ≫ g = 0)
(desc : Π {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), Z ⟶ Z')
(fac : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), g ≫ desc g' eq' = g')
(uniq : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0) (m : Z ⟶ Z') (w : g ≫ m = g'), m = desc g' eq') :
is_colimit (cokernel_cofork.of_π g eq) :=
is_colimit_aux _ (λ s, desc s.π s.condition) (λ s, fac s.π s.condition) (λ s, uniq s.π s.condition)
end
section
variables [has_cokernel f]
/-- The cokernel of a morphism, expressed as the coequalizer with the 0 morphism. -/
abbreviation cokernel : C := coequalizer f 0
/-- The map from the target of `f` to `cokernel f`. -/
abbreviation cokernel.π : Y ⟶ cokernel f := coequalizer.π f 0
@[simp, reassoc] lemma cokernel.condition : f ≫ cokernel.π f = 0 :=
cokernel_cofork.condition _
/-- Given any morphism `k : Y ⟶ W` such that `f ≫ k = 0`, `k` factors through `cokernel.π f`
via `cokernel.desc : cokernel f ⟶ W`. -/
abbreviation cokernel.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : cokernel f ⟶ W :=
colimit.desc (parallel_pair f 0) (cokernel_cofork.of_π k h)
@[simp, reassoc]
lemma cokernel.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) :
cokernel.π f ≫ cokernel.desc f k h = k :=
colimit.ι_desc _ _
@[simp]
lemma cokernel.desc_zero {W : C} {h} : cokernel.desc f (0 : Y ⟶ W) h = 0 :=
by { ext, simp, }
instance cokernel.desc_epi {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) [epi k] : epi (cokernel.desc f k h) :=
⟨λ Z g g' w,
begin
replace w := cokernel.π f ≫= w,
simp only [cokernel.π_desc_assoc] at w,
exact (cancel_epi k).1 w,
end⟩
/-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = 0` induces `l : cokernel f ⟶ W` such that
`cokernel.π f ≫ l = k`. -/
def cokernel.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) :
{l : cokernel f ⟶ W // cokernel.π f ≫ l = k} :=
⟨cokernel.desc f k h, cokernel.π_desc _ _ _⟩
/-- The cokernel of the zero morphism is an isomorphism -/
instance cokernel.π_zero_is_iso [has_colimit (parallel_pair (0 : X ⟶ Y) 0)] :
is_iso (cokernel.π (0 : X ⟶ Y)) :=
coequalizer.π_of_self _
lemma eq_zero_of_mono_cokernel [mono (cokernel.π f)] : f = 0 :=
(cancel_mono (cokernel.π f)).1 (by simp)
/-- The cokernel of a zero morphism is isomorphic to the target. -/
def cokernel_zero_iso_target [has_cokernel (0 : X ⟶ Y)] : cokernel (0 : X ⟶ Y) ≅ Y :=
coequalizer.iso_target_of_self 0
@[simp] lemma cokernel_zero_iso_target_hom [has_cokernel (0 : X ⟶ Y)] :
cokernel_zero_iso_target.hom = cokernel.desc (0 : X ⟶ Y) (𝟙 Y) (by simp) := rfl
@[simp] lemma cokernel_zero_iso_target_inv [has_cokernel (0 : X ⟶ Y)] :
cokernel_zero_iso_target.inv = cokernel.π (0 : X ⟶ Y) := rfl
/-- If two morphisms are known to be equal, then their cokernels are isomorphic. -/
def cokernel_iso_of_eq {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) :
cokernel f ≅ cokernel g :=
has_colimit.iso_of_nat_iso (by simp[h])
@[simp]
lemma cokernel_iso_of_eq_refl {h : f = f} : cokernel_iso_of_eq h = iso.refl (cokernel f) :=
by { ext, simp [cokernel_iso_of_eq], }
@[simp]
lemma cokernel_iso_of_eq_trans {f g h : X ⟶ Y} [has_cokernel f] [has_cokernel g] [has_cokernel h]
(w₁ : f = g) (w₂ : g = h) :
cokernel_iso_of_eq w₁ ≪≫ cokernel_iso_of_eq w₂ = cokernel_iso_of_eq (w₁.trans w₂) :=
by { unfreezingI { induction w₁, induction w₂, }, ext, simp [cokernel_iso_of_eq], }
variables {f}
lemma cokernel_not_mono_of_nonzero (w : f ≠ 0) : ¬mono (cokernel.π f) :=
λ I, by exactI w (eq_zero_of_mono_cokernel f)
lemma cokernel_not_iso_of_nonzero (w : f ≠ 0) : (is_iso (cokernel.π f)) → false :=
λ I, cokernel_not_mono_of_nonzero w $ by { resetI, apply_instance }
/--
When `g` is an isomorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `f`.
-/
@[simps]
def cokernel_comp_is_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] :
cokernel (f ≫ g) ≅ cokernel f :=
{ hom := cokernel.desc _ (inv g ≫ cokernel.π f) (by simp),
inv := cokernel.desc _ (g ≫ cokernel.π (f ≫ g)) (by rw [←category.assoc, cokernel.condition]), }
lemma cokernel_π_comp_cokernel_comp_is_iso_hom {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] :
cokernel.π (f ≫ g) ≫ (cokernel_comp_is_iso f g).hom = inv g ≫ cokernel.π f :=
by simp
lemma cokernel_π_comp_cokernel_comp_is_iso_inv {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] :
cokernel.π f ≫ (cokernel_comp_is_iso f g).inv = g ≫ cokernel.π (f ≫ g) :=
by simp
/--
When `f` is an isomorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `g`.
-/
@[simps]
def cokernel_is_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] :
cokernel (f ≫ g) ≅ cokernel g :=
{ hom := cokernel.desc _ (cokernel.π g) (by simp),
inv := cokernel.desc _ (cokernel.π (f ≫ g)) (by { rw [←cancel_epi f, ←category.assoc], simp, }), }
lemma cokernel_π_comp_cokernel_is_iso_comp_hom {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] :
cokernel.π (f ≫ g) ≫ (cokernel_is_iso_comp f g).hom = cokernel.π g :=
by simp
lemma cokernel_π_comp_cokernel_is_iso_comp_inv {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z)
[has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] :
cokernel.π g ≫ (cokernel_is_iso_comp f g).inv = cokernel.π (f ≫ g) :=
by simp
end
section has_zero_object
variables [has_zero_object C]
local attribute [instance] has_zero_object.has_zero
/-- The morphism to the zero object determines a cocone on a cokernel diagram -/
def cokernel.zero_cocone : cocone (parallel_pair f 0) :=
{ X := 0,
ι := { app := λ j, 0 } }
/-- The morphism to the zero object is a cokernel of an epimorphism -/
def cokernel.is_colimit_cocone_zero_cocone [epi f] : is_colimit (cokernel.zero_cocone f) :=
cofork.is_colimit.mk _ (λ s, 0)
(λ s, by { erw has_zero_morphisms.zero_comp,
convert (zero_of_epi_comp f _).symm,
exact cokernel_cofork.condition _ })
(λ _ _ _, zero_of_from_zero _)
/-- The cokernel of an epimorphism is isomorphic to the zero object -/
def cokernel.of_epi [has_cokernel f] [epi f] : cokernel f ≅ 0 :=
functor.map_iso (cocones.forget _) $ is_colimit.unique_up_to_iso
(colimit.is_colimit (parallel_pair f 0)) (cokernel.is_colimit_cocone_zero_cocone f)
/-- The cokernel morphism of an epimorphism is a zero morphism -/
lemma cokernel.π_of_epi [has_cokernel f] [epi f] : cokernel.π f = 0 :=
zero_of_target_iso_zero _ (cokernel.of_epi f)
end has_zero_object
section has_image
/--
The cokernel of the image inclusion of a morphism `f` is isomorphic to the cokernel of `f`.
(This result requires that the factorisation through the image is an epimorphism.
This holds in any category with equalizers.)
-/
@[simps]
def cokernel_image_ι {X Y : C} (f : X ⟶ Y)
[has_image f] [has_cokernel (image.ι f)] [has_cokernel f] [epi (factor_thru_image f)] :
cokernel (image.ι f) ≅ cokernel f :=
{ hom := cokernel.desc _ (cokernel.π f)
begin
have w := cokernel.condition f,
conv at w { to_lhs, congr, rw ←image.fac f, },
rw [←has_zero_morphisms.comp_zero (limits.factor_thru_image f), category.assoc, cancel_epi] at w,
exact w,
end,
inv := cokernel.desc _ (cokernel.π _)
begin
conv { to_lhs, congr, rw ←image.fac f, },
rw [category.assoc, cokernel.condition, has_zero_morphisms.comp_zero],
end, }
end has_image
section
variables (X Y)
/-- The cokernel of a zero morphism is an isomorphism -/
def cokernel.π_of_zero [has_cokernel (0 : X ⟶ Y)] :
is_iso (cokernel.π (0 : X ⟶ Y)) :=
coequalizer.π_of_self _
end
section has_zero_object
variables [has_zero_object C]
local attribute [instance] has_zero_object.has_zero
/-- The kernel of the cokernel of an epimorphism is an isomorphism -/
instance kernel.of_cokernel_of_epi [has_cokernel f]
[has_kernel (cokernel.π f)] [epi f] : is_iso (kernel.ι (cokernel.π f)) :=
equalizer.ι_of_eq $ cokernel.π_of_epi f
/-- The cokernel of the kernel of a monomorphism is an isomorphism -/
instance cokernel.of_kernel_of_mono [has_kernel f]
[has_cokernel (kernel.ι f)] [mono f] : is_iso (cokernel.π (kernel.ι f)) :=
coequalizer.π_of_eq $ kernel.ι_of_mono f
end has_zero_object
section transport
/-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then any cokernel of `f` is a cokernel of
`l`. -/
def is_cokernel.of_iso_comp {Z : C} (l : Z ⟶ Y) (i : X ≅ Z) (h : i.hom ≫ l = f)
{s : cokernel_cofork f} (hs : is_colimit s) : is_colimit (cokernel_cofork.of_π (cofork.π s) $
show l ≫ cofork.π s = 0, by simp [i.eq_inv_comp.2 h]) :=
cofork.is_colimit.mk _
(λ s, hs.desc $ cokernel_cofork.of_π (cofork.π s) $ by simp [←h])
(λ s, by simp)
(λ s m h, by { apply cofork.is_colimit.hom_ext hs, simpa using h walking_parallel_pair.one })
/-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then the cokernel of `f` is a cokernel of
`l`. -/
def cokernel.of_iso_comp [has_cokernel f]
{Z : C} (l : Z ⟶ Y) (i : X ≅ Z) (h : i.hom ≫ l = f) :
is_colimit (cokernel_cofork.of_π (cokernel.π f) $
show l ≫ cokernel.π f = 0, by simp [i.eq_inv_comp.2 h]) :=
is_cokernel.of_iso_comp f l i h $ colimit.is_colimit _
/-- If `s` is any colimit cokernel cocone over `f` and `i` is an isomorphism such that
`s.π ≫ i.hom = l`, then `l` is a cokernel of `f`. -/
def is_cokernel.cokernel_iso {Z : C} (l : Y ⟶ Z) {s : cokernel_cofork f} (hs : is_colimit s)
(i : s.X ≅ Z) (h : cofork.π s ≫ i.hom = l) : is_colimit (cokernel_cofork.of_π l $
show f ≫ l = 0, by simp [←h]) :=
is_colimit.of_iso_colimit hs $ cocones.ext i $ λ j, by { cases j, { simp }, { exact h } }
/-- If `i` is an isomorphism such that `cokernel.π f ≫ i.hom = l`, then `l` is a cokernel of `f`. -/
def cokernel.cokernel_iso [has_cokernel f]
{Z : C} (l : Y ⟶ Z) (i : cokernel f ≅ Z) (h : cokernel.π f ≫ i.hom = l) :
is_colimit (cokernel_cofork.of_π l $ by simp [←h]) :=
is_cokernel.cokernel_iso f l (colimit.is_colimit _) i h
end transport
end category_theory.limits
namespace category_theory.limits
variables (C : Type u) [category.{v} C]
variables [has_zero_morphisms C]
/-- `has_kernels` represents a choice of kernel for every morphism -/
class has_kernels :=
(has_limit : Π {X Y : C} (f : X ⟶ Y), has_kernel f)
/-- `has_cokernels` represents a choice of cokernel for every morphism -/
class has_cokernels :=
(has_colimit : Π {X Y : C} (f : X ⟶ Y), has_cokernel f)
attribute [instance, priority 100] has_kernels.has_limit has_cokernels.has_colimit
@[priority 100]
instance has_kernels_of_has_equalizers [has_equalizers C] : has_kernels C :=
{ has_limit := infer_instance }
@[priority 100]
instance has_cokernels_of_has_coequalizers [has_coequalizers C] : has_cokernels C :=
{ has_colimit := infer_instance }
end category_theory.limits
|
d7cc4f0434625277c0334197b7bcab17fbcd0ea2 | 26b8b0964ca8e1c2e203585ba5940f83fe05e48a | /test/edit_distance.lean | c97476a5f29798eef6c0dc432ab19092e94a8229 | [] | no_license | jcommelin/lean-tidy | ef3cd32a3804221d93f0dff9e180bb2c52f4b143 | 9cecf497e90db64b5ea140ad6ae1603976dcd402 | refs/heads/master | 1,585,129,919,276 | 1,533,512,680,000 | 1,533,512,680,000 | 143,677,361 | 0 | 0 | null | 1,616,803,481,000 | 1,533,530,576,000 | Lean | UTF-8 | Lean | false | false | 558 | lean | import tidy.edit_distance
open edit_distance_progress
#eval edit_distance' [1,2,3,4] [1,2,3,4]
def f0 : partial_edit_distance_data ℕ := ⟨ 0, [7,8,9], [1,2,3,4,5] ⟩
def p0 : edit_distance_progress [7,8,9] [7,8,19,8,9] := at_least [7,8,9] [7,8,19,8,9] 0 f0
meta def p1 := update_edit_distance p0
meta def p2 := update_edit_distance p1
meta def p3 := update_edit_distance p2
meta def p4 := update_edit_distance p3
meta def p5 := update_edit_distance p4
#eval p0.to_string
#eval p1.to_string
#eval p2.to_string
#eval p3.to_string
#eval p4.to_string
|
ac9593e282ca5bdd12c6bb2d0a3cda704e4673fe | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/algebra/lie/weights.lean | 3ecb736a7312ab0e3ed8238749654a98dcc65ac4 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,191 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.nilpotent
import algebra.lie.tensor_product
import algebra.lie.character
import algebra.lie.cartan_subalgebra
import linear_algebra.eigenspace
import ring_theory.tensor_product
/-!
# Weights and roots of Lie modules and Lie algebras
Just as a key tool when studying the behaviour of a linear operator is to decompose the space on
which it acts into a sum of (generalised) eigenspaces, a key tool when studying a representation `M`
of Lie algebra `L` is to decompose `M` into a sum of simultaneous eigenspaces of `x` as `x` ranges
over `L`. These simultaneous generalised eigenspaces are known as the weight spaces of `M`.
When `L` is nilpotent, it follows from the binomial theorem that weight spaces are Lie submodules.
Even when `L` is not nilpotent, it may be useful to study its representations by restricting them
to a nilpotent subalgebra (e.g., a Cartan subalgebra). In the particular case when we view `L` as a
module over itself via the adjoint action, the weight spaces of `L` restricted to a nilpotent
subalgebra are known as root spaces.
Basic definitions and properties of the above ideas are provided in this file.
## Main definitions
* `lie_module.weight_space`
* `lie_module.is_weight`
* `lie_algebra.root_space`
* `lie_algebra.is_root`
* `lie_algebra.root_space_weight_space_product`
* `lie_algebra.root_space_product`
## References
* [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 7--9*](bourbaki1975b)
## Tags
lie character, eigenvalue, eigenspace, weight, weight vector, root, root vector
-/
universes u v w w₁ w₂ w₃
variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L]
variables (H : lie_subalgebra R L) [lie_algebra.is_nilpotent R H]
variables (M : Type w) [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
namespace lie_module
open lie_algebra
open tensor_product
open tensor_product.lie_module
open_locale big_operators
open_locale tensor_product
/-- Given a Lie module `M` over a Lie algebra `L`, the pre-weight space of `M` with respect to a
map `χ : L → R` is the simultaneous generalized eigenspace of the action of all `x : L` on `M`,
with eigenvalues `χ x`.
See also `lie_module.weight_space`. -/
def pre_weight_space (χ : L → R) : submodule R M :=
⨅ (x : L), (to_endomorphism R L M x).maximal_generalized_eigenspace (χ x)
lemma mem_pre_weight_space (χ : L → R) (m : M) :
m ∈ pre_weight_space M χ ↔ ∀ x, ∃ (k : ℕ), ((to_endomorphism R L M x - (χ x) • 1)^k) m = 0 :=
by simp [pre_weight_space, -linear_map.pow_apply]
variables (L)
/-- See also `bourbaki1975b` Chapter VII §1.1, Proposition 2 (ii). -/
protected lemma weight_vector_multiplication (M₁ : Type w₁) (M₂ : Type w₂) (M₃ : Type w₃)
[add_comm_group M₁] [module R M₁] [lie_ring_module L M₁] [lie_module R L M₁]
[add_comm_group M₂] [module R M₂] [lie_ring_module L M₂] [lie_module R L M₂]
[add_comm_group M₃] [module R M₃] [lie_ring_module L M₃] [lie_module R L M₃]
(g : M₁ ⊗[R] M₂ →ₗ⁅R,L⁆ M₃) (χ₁ χ₂ : L → R) :
((g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp
(map_incl (pre_weight_space M₁ χ₁) (pre_weight_space M₂ χ₂))).range ≤
pre_weight_space M₃ (χ₁ + χ₂) :=
begin
/- Unpack the statement of the goal. -/
intros m₃,
simp only [lie_module_hom.coe_to_linear_map, pi.add_apply, function.comp_app,
mem_pre_weight_space, linear_map.coe_comp, tensor_product.map_incl, exists_imp_distrib,
linear_map.mem_range],
rintros t rfl x,
/- Set up some notation. -/
let F : module.End R M₃ := (to_endomorphism R L M₃ x) - (χ₁ x + χ₂ x) • 1,
change ∃ k, (F^k) (g _) = 0,
/- The goal is linear in `t` so use induction to reduce to the case that `t` is a pure tensor. -/
apply t.induction_on,
{ use 0, simp only [linear_map.map_zero, lie_module_hom.map_zero], },
swap,
{ rintros t₁ t₂ ⟨k₁, hk₁⟩ ⟨k₂, hk₂⟩, use max k₁ k₂,
simp only [lie_module_hom.map_add, linear_map.map_add,
linear_map.pow_map_zero_of_le (le_max_left k₁ k₂) hk₁,
linear_map.pow_map_zero_of_le (le_max_right k₁ k₂) hk₂, add_zero], },
/- Now the main argument: pure tensors. -/
rintros ⟨m₁, hm₁⟩ ⟨m₂, hm₂⟩,
change ∃ k, (F^k) ((g : M₁ ⊗[R] M₂ →ₗ[R] M₃) (m₁ ⊗ₜ m₂)) = 0,
/- Eliminate `g` from the picture. -/
let f₁ : module.End R (M₁ ⊗[R] M₂) := (to_endomorphism R L M₁ x - (χ₁ x) • 1).rtensor M₂,
let f₂ : module.End R (M₁ ⊗[R] M₂) := (to_endomorphism R L M₂ x - (χ₂ x) • 1).ltensor M₁,
have h_comm_square : F ∘ₗ ↑g = (g : M₁ ⊗[R] M₂ →ₗ[R] M₃).comp (f₁ + f₂),
{ ext m₁ m₂, simp only [← g.map_lie x (m₁ ⊗ₜ m₂), add_smul, sub_tmul, tmul_sub, smul_tmul,
lie_tmul_right, tmul_smul, to_endomorphism_apply_apply, lie_module_hom.map_smul,
linear_map.one_apply, lie_module_hom.coe_to_linear_map, linear_map.smul_apply,
function.comp_app, linear_map.coe_comp, linear_map.rtensor_tmul, lie_module_hom.map_add,
linear_map.add_apply, lie_module_hom.map_sub, linear_map.sub_apply, linear_map.ltensor_tmul,
algebra_tensor_module.curry_apply, curry_apply, linear_map.to_fun_eq_coe,
linear_map.coe_restrict_scalars_eq_coe], abel, },
suffices : ∃ k, ((f₁ + f₂)^k) (m₁ ⊗ₜ m₂) = 0,
{ obtain ⟨k, hk⟩ := this, use k,
rw [← linear_map.comp_apply, linear_map.commute_pow_left_of_commute h_comm_square,
linear_map.comp_apply, hk, linear_map.map_zero], },
/- Unpack the information we have about `m₁`, `m₂`. -/
simp only [mem_pre_weight_space] at hm₁ hm₂,
obtain ⟨k₁, hk₁⟩ := hm₁ x,
obtain ⟨k₂, hk₂⟩ := hm₂ x,
have hf₁ : (f₁^k₁) (m₁ ⊗ₜ m₂) = 0,
{ simp only [hk₁, zero_tmul, linear_map.rtensor_tmul, linear_map.rtensor_pow], },
have hf₂ : (f₂^k₂) (m₁ ⊗ₜ m₂) = 0,
{ simp only [hk₂, tmul_zero, linear_map.ltensor_tmul, linear_map.ltensor_pow], },
/- It's now just an application of the binomial theorem. -/
use k₁ + k₂ - 1,
have hf_comm : commute f₁ f₂,
{ ext m₁ m₂, simp only [linear_map.mul_apply, linear_map.rtensor_tmul, linear_map.ltensor_tmul,
algebra_tensor_module.curry_apply, linear_map.to_fun_eq_coe, linear_map.ltensor_tmul,
curry_apply, linear_map.coe_restrict_scalars_eq_coe], },
rw hf_comm.add_pow',
simp only [tensor_product.map_incl, submodule.subtype_apply, finset.sum_apply,
submodule.coe_mk, linear_map.coe_fn_sum, tensor_product.map_tmul, linear_map.smul_apply],
/- The required sum is zero because each individual term is zero. -/
apply finset.sum_eq_zero,
rintros ⟨i, j⟩ hij,
/- Eliminate the binomial coefficients from the picture. -/
suffices : (f₁^i * f₂^j) (m₁ ⊗ₜ m₂) = 0, { rw this, apply smul_zero, },
/- Finish off with appropriate case analysis. -/
cases nat.le_or_le_of_add_eq_add_pred (finset.nat.mem_antidiagonal.mp hij) with hi hj,
{ rw [(hf_comm.pow_pow i j).eq, linear_map.mul_apply, linear_map.pow_map_zero_of_le hi hf₁,
linear_map.map_zero], },
{ rw [linear_map.mul_apply, linear_map.pow_map_zero_of_le hj hf₂, linear_map.map_zero], },
end
variables {L M}
lemma lie_mem_pre_weight_space_of_mem_pre_weight_space {χ₁ χ₂ : L → R} {x : L} {m : M}
(hx : x ∈ pre_weight_space L χ₁) (hm : m ∈ pre_weight_space M χ₂) :
⁅x, m⁆ ∈ pre_weight_space M (χ₁ + χ₂) :=
begin
apply lie_module.weight_vector_multiplication L L M M (to_module_hom R L M) χ₁ χ₂,
simp only [lie_module_hom.coe_to_linear_map, function.comp_app, linear_map.coe_comp,
tensor_product.map_incl, linear_map.mem_range],
use [⟨x, hx⟩ ⊗ₜ ⟨m, hm⟩],
simp only [submodule.subtype_apply, to_module_hom_apply, tensor_product.map_tmul],
refl,
end
variables (M)
/-- If a Lie algebra is nilpotent, then pre-weight spaces are Lie submodules. -/
def weight_space [lie_algebra.is_nilpotent R L] (χ : L → R) : lie_submodule R L M :=
{ lie_mem := λ x m hm,
begin
rw ← zero_add χ,
refine lie_mem_pre_weight_space_of_mem_pre_weight_space _ hm,
suffices : pre_weight_space L (0 : L → R) = ⊤, { simp only [this, submodule.mem_top], },
exact lie_algebra.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L,
end,
.. pre_weight_space M χ }
lemma mem_weight_space [lie_algebra.is_nilpotent R L] (χ : L → R) (m : M) :
m ∈ weight_space M χ ↔ m ∈ pre_weight_space M χ :=
iff.rfl
/-- See also the more useful form `lie_module.zero_weight_space_eq_top_of_nilpotent`. -/
@[simp] lemma zero_weight_space_eq_top_of_nilpotent'
[lie_algebra.is_nilpotent R L] [is_nilpotent R L M] :
weight_space M (0 : L → R) = ⊤ :=
begin
rw [← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule],
exact infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L M,
end
lemma coe_weight_space_of_top [lie_algebra.is_nilpotent R L] (χ : L → R) :
(weight_space M (χ ∘ (⊤ : lie_subalgebra R L).incl) : submodule R M) = weight_space M χ :=
begin
ext m,
simp only [weight_space, lie_submodule.coe_to_submodule_mk, lie_subalgebra.coe_bracket_of_module,
function.comp_app, mem_pre_weight_space],
split; intros h x,
{ obtain ⟨k, hk⟩ := h ⟨x, set.mem_univ x⟩, use k, exact hk, },
{ obtain ⟨k, hk⟩ := h x, use k, exact hk, },
end
@[simp] lemma zero_weight_space_eq_top_of_nilpotent
[lie_algebra.is_nilpotent R L] [is_nilpotent R L M] :
weight_space M (0 : (⊤ : lie_subalgebra R L) → R) = ⊤ :=
begin
/- We use `coe_weight_space_of_top` as a trick to circumvent the fact that we don't (yet) know
`is_nilpotent R (⊤ : lie_subalgebra R L) M` is equivalent to `is_nilpotent R L M`. -/
have h₀ : (0 : L → R) ∘ (⊤ : lie_subalgebra R L).incl = 0, { ext, refl, },
rw [← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule, ← h₀,
coe_weight_space_of_top, ← infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L M],
refl,
end
/-- Given a Lie module `M` of a Lie algebra `L`, a weight of `M` with respect to a nilpotent
subalgebra `H ⊆ L` is a Lie character whose corresponding weight space is non-empty. -/
def is_weight (χ : lie_character R H) : Prop := weight_space M χ ≠ ⊥
/-- For a non-trivial nilpotent Lie module over a nilpotent Lie algebra, the zero character is a
weight with respect to the `⊤` Lie subalgebra. -/
lemma is_weight_zero_of_nilpotent
[nontrivial M] [lie_algebra.is_nilpotent R L] [is_nilpotent R L M] :
is_weight (⊤ : lie_subalgebra R L) M 0 :=
by { rw [is_weight, lie_hom.coe_zero, zero_weight_space_eq_top_of_nilpotent], exact top_ne_bot, }
end lie_module
namespace lie_algebra
open_locale tensor_product
open tensor_product.lie_module
open lie_module
/-- Given a nilpotent Lie subalgebra `H ⊆ L`, the root space of a map `χ : H → R` is the weight
space of `L` regarded as a module of `H` via the adjoint action. -/
abbreviation root_space (χ : H → R) : lie_submodule R H L := weight_space L χ
@[simp] lemma zero_root_space_eq_top_of_nilpotent [h : is_nilpotent R L] :
root_space (⊤ : lie_subalgebra R L) 0 = ⊤ :=
zero_weight_space_eq_top_of_nilpotent L
/-- A root of a Lie algebra `L` with respect to a nilpotent subalgebra `H ⊆ L` is a weight of `L`,
regarded as a module of `H` via the adjoint action. -/
abbreviation is_root := is_weight H L
@[simp] lemma root_space_comap_eq_weight_space (χ : H → R) :
(root_space H χ).comap H.incl' = weight_space H χ :=
begin
ext x,
let f : H → module.End R L := λ y, to_endomorphism R H L y - (χ y) • 1,
let g : H → module.End R H := λ y, to_endomorphism R H H y - (χ y) • 1,
suffices : (∀ (y : H), ∃ (k : ℕ), ((f y)^k).comp (H.incl : H →ₗ[R] L) x = 0) ↔
∀ (y : H), ∃ (k : ℕ), (H.incl : H →ₗ[R] L).comp ((g y)^k) x = 0,
{ simp only [lie_hom.coe_to_linear_map, lie_subalgebra.coe_incl, function.comp_app,
linear_map.coe_comp, submodule.coe_eq_zero] at this,
simp only [mem_weight_space, mem_pre_weight_space,
lie_subalgebra.coe_incl', lie_submodule.mem_comap, this], },
have hfg : ∀ (y : H), (f y).comp (H.incl : H →ₗ[R] L) = (H.incl : H →ₗ[R] L).comp (g y),
{ rintros ⟨y, hz⟩, ext ⟨z, hz⟩,
simp only [submodule.coe_sub, to_endomorphism_apply_apply, lie_hom.coe_to_linear_map,
linear_map.one_apply, lie_subalgebra.coe_incl, lie_subalgebra.coe_bracket_of_module,
lie_subalgebra.coe_bracket, linear_map.smul_apply, function.comp_app,
submodule.coe_smul_of_tower, linear_map.coe_comp, linear_map.sub_apply], },
simp_rw [linear_map.commute_pow_left_of_commute (hfg _)],
end
variables {H M}
lemma lie_mem_weight_space_of_mem_weight_space {χ₁ χ₂ : H → R} {x : L} {m : M}
(hx : x ∈ root_space H χ₁) (hm : m ∈ weight_space M χ₂) : ⁅x, m⁆ ∈ weight_space M (χ₁ + χ₂) :=
begin
apply lie_module.weight_vector_multiplication
H L M M ((to_module_hom R L M).restrict_lie H) χ₁ χ₂,
simp only [lie_module_hom.coe_to_linear_map, function.comp_app, linear_map.coe_comp,
tensor_product.map_incl, linear_map.mem_range],
use [⟨x, hx⟩ ⊗ₜ ⟨m, hm⟩],
simp only [submodule.subtype_apply, to_module_hom_apply, submodule.coe_mk,
lie_module_hom.coe_restrict_lie, tensor_product.map_tmul],
end
variables (R L H M)
/-- Given a nilpotent Lie subalgebra `H ⊆ L` together with `χ₁ χ₂ : H → R`, there is a natural
`R`-bilinear product of root vectors and weight vectors, compatible with the actions of `H`. -/
def root_space_weight_space_product (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) :
(root_space H χ₁) ⊗[R] (weight_space M χ₂) →ₗ⁅R,H⁆ weight_space M χ₃ :=
lift_lie R H (root_space H χ₁) (weight_space M χ₂) (weight_space M χ₃)
{ to_fun := λ x,
{ to_fun :=
λ m, ⟨⁅(x : L), (m : M)⁆,
hχ ▸ (lie_mem_weight_space_of_mem_weight_space x.property m.property) ⟩,
map_add' := λ m n, by { simp only [lie_submodule.coe_add, lie_add], refl, },
map_smul' := λ t m, by { conv_lhs { congr, rw [lie_submodule.coe_smul, lie_smul], }, refl, }, },
map_add' := λ x y, by ext m; rw [linear_map.add_apply, linear_map.coe_mk, linear_map.coe_mk,
linear_map.coe_mk, subtype.coe_mk, lie_submodule.coe_add, lie_submodule.coe_add, add_lie,
subtype.coe_mk, subtype.coe_mk],
map_smul' := λ t x, by ext m; rw [linear_map.smul_apply, linear_map.coe_mk, linear_map.coe_mk,
subtype.coe_mk, lie_submodule.coe_smul, smul_lie, lie_submodule.coe_smul, subtype.coe_mk],
map_lie' := λ x y, by ext m; rw [lie_hom.lie_apply, lie_submodule.coe_sub, linear_map.coe_mk,
linear_map.coe_mk, subtype.coe_mk, subtype.coe_mk, lie_submodule.coe_bracket,
lie_submodule.coe_bracket, subtype.coe_mk, lie_subalgebra.coe_bracket_of_module,
lie_subalgebra.coe_bracket_of_module, lie_submodule.coe_bracket,
lie_subalgebra.coe_bracket_of_module, lie_lie], }
@[simp] lemma coe_root_space_weight_space_product_tmul
(χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) (x : root_space H χ₁) (m : weight_space M χ₂) :
(root_space_weight_space_product R L H M χ₁ χ₂ χ₃ hχ (x ⊗ₜ m) : M) = ⁅(x : L), (m : M)⁆ :=
by simp only [root_space_weight_space_product, lift_apply, lie_module_hom.coe_to_linear_map,
coe_lift_lie_eq_lift_coe, submodule.coe_mk, linear_map.coe_mk, lie_module_hom.coe_mk]
/-- Given a nilpotent Lie subalgebra `H ⊆ L` together with `χ₁ χ₂ : H → R`, there is a natural
`R`-bilinear product of root vectors, compatible with the actions of `H`. -/
def root_space_product (χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) :
(root_space H χ₁) ⊗[R] (root_space H χ₂) →ₗ⁅R,H⁆ root_space H χ₃ :=
root_space_weight_space_product R L H L χ₁ χ₂ χ₃ hχ
@[simp] lemma root_space_product_def :
root_space_product R L H = root_space_weight_space_product R L H L :=
rfl
lemma root_space_product_tmul
(χ₁ χ₂ χ₃ : H → R) (hχ : χ₁ + χ₂ = χ₃) (x : root_space H χ₁) (y : root_space H χ₂) :
(root_space_product R L H χ₁ χ₂ χ₃ hχ (x ⊗ₜ y) : L) = ⁅(x : L), (y : L)⁆ :=
by simp only [root_space_product_def, coe_root_space_weight_space_product_tmul]
/-- Given a nilpotent Lie subalgebra `H ⊆ L`, the root space of the zero map `0 : H → R` is a Lie
subalgebra of `L`. -/
def zero_root_subalgebra : lie_subalgebra R L :=
{ lie_mem' := λ x y hx hy, by
{ let xy : (root_space H 0) ⊗[R] (root_space H 0) := ⟨x, hx⟩ ⊗ₜ ⟨y, hy⟩,
suffices : (root_space_product R L H 0 0 0 (add_zero 0) xy : L) ∈ root_space H 0,
{ rwa [root_space_product_tmul, subtype.coe_mk, subtype.coe_mk] at this, },
exact (root_space_product R L H 0 0 0 (add_zero 0) xy).property, },
.. (root_space H 0 : submodule R L) }
@[simp] lemma coe_zero_root_subalgebra :
(zero_root_subalgebra R L H : submodule R L) = root_space H 0 :=
rfl
lemma mem_zero_root_subalgebra (x : L) :
x ∈ zero_root_subalgebra R L H ↔ ∀ (y : H), ∃ (k : ℕ), ((to_endomorphism R H L y)^k) x = 0 :=
by simp only [zero_root_subalgebra, mem_weight_space, mem_pre_weight_space, pi.zero_apply, sub_zero,
set_like.mem_coe, zero_smul, lie_submodule.mem_coe_submodule, submodule.mem_carrier,
lie_subalgebra.mem_mk_iff]
lemma to_lie_submodule_le_root_space_zero : H.to_lie_submodule ≤ root_space H 0 :=
begin
intros x hx,
simp only [lie_subalgebra.mem_to_lie_submodule] at hx,
simp only [mem_weight_space, mem_pre_weight_space, pi.zero_apply, sub_zero, zero_smul],
intros y,
unfreezingI { obtain ⟨k, hk⟩ := (infer_instance : is_nilpotent R H) },
use k,
let f : module.End R H := to_endomorphism R H H y,
let g : module.End R L := to_endomorphism R H L y,
have hfg : g.comp (H : submodule R L).subtype = (H : submodule R L).subtype.comp f,
{ ext z, simp only [to_endomorphism_apply_apply, submodule.subtype_apply,
lie_subalgebra.coe_bracket_of_module, lie_subalgebra.coe_bracket, function.comp_app,
linear_map.coe_comp], },
change (g^k).comp (H : submodule R L).subtype ⟨x, hx⟩ = 0,
rw linear_map.commute_pow_left_of_commute hfg k,
have h := iterate_to_endomorphism_mem_lower_central_series R H H y ⟨x, hx⟩ k,
rw [hk, lie_submodule.mem_bot] at h,
simp only [submodule.subtype_apply, function.comp_app, linear_map.pow_apply, linear_map.coe_comp,
submodule.coe_eq_zero],
exact h,
end
lemma le_zero_root_subalgebra : H ≤ zero_root_subalgebra R L H :=
begin
rw [← lie_subalgebra.coe_submodule_le_coe_submodule, ← H.coe_to_lie_submodule,
coe_zero_root_subalgebra, lie_submodule.coe_submodule_le_coe_submodule],
exact to_lie_submodule_le_root_space_zero R L H,
end
@[simp] lemma zero_root_subalgebra_normalizer_eq_self :
(zero_root_subalgebra R L H).normalizer = zero_root_subalgebra R L H :=
begin
refine le_antisymm _ (lie_subalgebra.le_normalizer _),
intros x hx,
rw lie_subalgebra.mem_normalizer_iff at hx,
rw mem_zero_root_subalgebra,
rintros ⟨y, hy⟩,
specialize hx y (le_zero_root_subalgebra R L H hy),
rw mem_zero_root_subalgebra at hx,
obtain ⟨k, hk⟩ := hx ⟨y, hy⟩,
rw [← lie_skew, linear_map.map_neg, neg_eq_zero] at hk,
use k + 1,
rw [linear_map.iterate_succ, linear_map.coe_comp, function.comp_app, to_endomorphism_apply_apply,
lie_subalgebra.coe_bracket_of_module, submodule.coe_mk, hk],
end
/-- In finite dimensions over a field (and possibly more generally) Engel's theorem shows that
the converse of this is also true, i.e.,
`zero_root_subalgebra R L H = H ↔ lie_subalgebra.is_cartan_subalgebra H`. -/
lemma zero_root_subalgebra_is_cartan_of_eq (h : zero_root_subalgebra R L H = H) :
lie_subalgebra.is_cartan_subalgebra H :=
{ nilpotent := infer_instance,
self_normalizing := by { rw ← h, exact zero_root_subalgebra_normalizer_eq_self R L H, } }
end lie_algebra
namespace lie_module
open lie_algebra
variables {R L H}
/-- A priori, weight spaces are Lie submodules over the Lie subalgebra `H` used to define them.
However they are naturally Lie submodules over the (in general larger) Lie subalgebra
`zero_root_subalgebra R L H`. Even though it is often the case that
`zero_root_subalgebra R L H = H`, it is likely to be useful to have the flexibility not to have
to invoke this equality (as well as to work more generally). -/
def weight_space' (χ : H → R) : lie_submodule R (zero_root_subalgebra R L H) M :=
{ lie_mem := λ x m hm, by
{ have hx : (x : L) ∈ root_space H 0,
{ rw [← lie_submodule.mem_coe_submodule, ← coe_zero_root_subalgebra], exact x.property, },
rw ← zero_add χ,
exact lie_mem_weight_space_of_mem_weight_space hx hm, },
.. (weight_space M χ : submodule R M) }
@[simp] lemma coe_weight_space' (χ : H → R) :
(weight_space' M χ : submodule R M) = weight_space M χ :=
rfl
end lie_module
|
f5a28321b9ce608e45189344785463039b06b8c4 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/field_theory/separable.lean | c1a9a1c93009cb50409733aab3e9268bfc0a8ca4 | [
"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 | 30,407 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import algebra.polynomial.big_operators
import algebra.squarefree
import field_theory.minpoly
import field_theory.splitting_field
/-!
# Separable polynomials
We define a polynomial to be separable if it is coprime with its derivative. We prove basic
properties about separable polynomials here.
## Main definitions
* `polynomial.separable f`: a polynomial `f` is separable iff it is coprime with its derivative.
* `polynomial.expand R p f`: expand the polynomial `f` with coefficients in a
commutative semiring `R` by a factor of p, so `expand R p (∑ aₙ xⁿ)` is `∑ aₙ xⁿᵖ`.
* `polynomial.contract p f`: the opposite of `expand`, so it sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`.
-/
universes u v w
open_locale classical big_operators
open finset
namespace polynomial
section comm_semiring
variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S]
/-- A polynomial is separable iff it is coprime with its derivative. -/
def separable (f : polynomial R) : Prop :=
is_coprime f f.derivative
lemma separable_def (f : polynomial R) :
f.separable ↔ is_coprime f f.derivative :=
iff.rfl
lemma separable_def' (f : polynomial R) :
f.separable ↔ ∃ a b : polynomial R, a * f + b * f.derivative = 1 :=
iff.rfl
lemma not_separable_zero [nontrivial R] : ¬ separable (0 : polynomial R) :=
begin
rintro ⟨x, y, h⟩,
simpa only [derivative_zero, mul_zero, add_zero, zero_ne_one] using h,
end
lemma separable_one : (1 : polynomial R).separable :=
is_coprime_one_left
@[nontriviality] lemma separable_of_subsingleton [subsingleton R] (f : polynomial R) :
f.separable := by simp [separable]
lemma separable_X_add_C (a : R) : (X + C a).separable :=
by { rw [separable_def, derivative_add, derivative_X, derivative_C, add_zero],
exact is_coprime_one_right }
lemma separable_X : (X : polynomial R).separable :=
by { rw [separable_def, derivative_X], exact is_coprime_one_right }
lemma separable_C (r : R) : (C r).separable ↔ is_unit r :=
by rw [separable_def, derivative_C, is_coprime_zero_right, is_unit_C]
lemma separable.of_mul_left {f g : polynomial R} (h : (f * g).separable) : f.separable :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_left (is_coprime.of_add_mul_left_right this)
end
lemma separable.of_mul_right {f g : polynomial R} (h : (f * g).separable) : g.separable :=
by { rw mul_comm at h, exact h.of_mul_left }
lemma separable.of_dvd {f g : polynomial R} (hf : f.separable) (hfg : g ∣ f) : g.separable :=
by { rcases hfg with ⟨f', rfl⟩, exact separable.of_mul_left hf }
lemma separable_gcd_left {F : Type*} [field F] {f : polynomial F}
(hf : f.separable) (g : polynomial F) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hf (euclidean_domain.gcd_dvd_left f g)
lemma separable_gcd_right {F : Type*} [field F] {g : polynomial F}
(f : polynomial F) (hg : g.separable) : (euclidean_domain.gcd f g).separable :=
separable.of_dvd hg (euclidean_domain.gcd_dvd_right f g)
lemma separable.is_coprime {f g : polynomial R} (h : (f * g).separable) : is_coprime f g :=
begin
have := h.of_mul_left_left, rw derivative_mul at this,
exact is_coprime.of_mul_right_right (is_coprime.of_add_mul_left_right this)
end
theorem separable.of_pow' {f : polynomial R} :
∀ {n : ℕ} (h : (f ^ n).separable), is_unit f ∨ (f.separable ∧ n = 1) ∨ n = 0
| 0 := λ h, or.inr $ or.inr rfl
| 1 := λ h, or.inr $ or.inl ⟨pow_one f ▸ h, rfl⟩
| (n+2) := λ h, by { rw [pow_succ, pow_succ] at h,
exact or.inl (is_coprime_self.1 h.is_coprime.of_mul_right_left) }
theorem separable.of_pow {f : polynomial R} (hf : ¬is_unit f) {n : ℕ} (hn : n ≠ 0)
(hfs : (f ^ n).separable) : f.separable ∧ n = 1 :=
(hfs.of_pow'.resolve_left hf).resolve_right hn
theorem separable.map {p : polynomial R} (h : p.separable) {f : R →+* S} : (p.map f).separable :=
let ⟨a, b, H⟩ := h in ⟨a.map f, b.map f,
by rw [derivative_map, ← map_mul, ← map_mul, ← map_add, H, map_one]⟩
variables (R) (p q : ℕ)
/-- Expand the polynomial by a factor of p, so `∑ aₙ xⁿ` becomes `∑ aₙ xⁿᵖ`. -/
noncomputable def expand : polynomial R →ₐ[R] polynomial R :=
{ commutes' := λ r, eval₂_C _ _,
.. (eval₂_ring_hom C (X ^ p) : polynomial R →+* polynomial R) }
lemma coe_expand : (expand R p : polynomial R → polynomial R) = eval₂ C (X ^ p) := rfl
variables {R}
lemma expand_eq_sum {f : polynomial R} :
expand R p f = f.sum (λ e a, C a * (X ^ p) ^ e) :=
by { dsimp [expand, eval₂], refl, }
@[simp] lemma expand_C (r : R) : expand R p (C r) = C r := eval₂_C _ _
@[simp] lemma expand_X : expand R p X = X ^ p := eval₂_X _ _
@[simp] lemma expand_monomial (r : R) : expand R p (monomial q r) = monomial (q * p) r :=
by simp_rw [monomial_eq_smul_X, alg_hom.map_smul, alg_hom.map_pow, expand_X, mul_comm, pow_mul]
theorem expand_expand (f : polynomial R) : expand R p (expand R q f) = expand R (p * q) f :=
polynomial.induction_on f (λ r, by simp_rw expand_C)
(λ f g ihf ihg, by simp_rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by simp_rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X,
alg_hom.map_pow, expand_X, pow_mul])
theorem expand_mul (f : polynomial R) : expand R (p * q) f = expand R p (expand R q f) :=
(expand_expand p q f).symm
@[simp] theorem expand_zero (f : polynomial R) : expand R 0 f = C (eval 1 f) :=
by simp [expand]
@[simp] theorem expand_one (f : polynomial R) : expand R 1 f = f :=
polynomial.induction_on f
(λ r, by rw expand_C)
(λ f g ihf ihg, by rw [alg_hom.map_add, ihf, ihg])
(λ n r ih, by rw [alg_hom.map_mul, expand_C, alg_hom.map_pow, expand_X, pow_one])
theorem expand_pow (f : polynomial R) : expand R (p ^ q) f = (expand R p ^[q] f) :=
nat.rec_on q (by rw [pow_zero, expand_one, function.iterate_zero, id]) $ λ n ih,
by rw [function.iterate_succ_apply', pow_succ, expand_mul, ih]
theorem derivative_expand (f : polynomial R) :
(expand R p f).derivative = expand R p f.derivative * (p * X ^ (p - 1)) :=
by rw [coe_expand, derivative_eval₂_C, derivative_pow, derivative_X, mul_one]
theorem coeff_expand {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff n = if p ∣ n then f.coeff (n / p) else 0 :=
begin
simp only [expand_eq_sum],
simp_rw [coeff_sum, ← pow_mul, C_mul_X_pow_eq_monomial, coeff_monomial, sum],
split_ifs with h,
{ rw [finset.sum_eq_single (n/p), nat.mul_div_cancel' h, if_pos rfl],
{ intros b hb1 hb2, rw if_neg, intro hb3, apply hb2, rw [← hb3, nat.mul_div_cancel_left b hp] },
{ intro hn, rw not_mem_support_iff.1 hn, split_ifs; refl } },
{ rw finset.sum_eq_zero, intros k hk, rw if_neg, exact λ hkn, h ⟨k, hkn.symm⟩, },
end
@[simp] theorem coeff_expand_mul {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (n * p) = f.coeff n :=
by rw [coeff_expand hp, if_pos (dvd_mul_left _ _), nat.mul_div_cancel _ hp]
@[simp] theorem coeff_expand_mul' {p : ℕ} (hp : 0 < p) (f : polynomial R) (n : ℕ) :
(expand R p f).coeff (p * n) = f.coeff n :=
by rw [mul_comm, coeff_expand_mul hp]
theorem expand_inj {p : ℕ} (hp : 0 < p) {f g : polynomial R} :
expand R p f = expand R p g ↔ f = g :=
⟨λ H, ext $ λ n, by rw [← coeff_expand_mul hp, H, coeff_expand_mul hp], congr_arg _⟩
theorem expand_eq_zero {p : ℕ} (hp : 0 < p) {f : polynomial R} : expand R p f = 0 ↔ f = 0 :=
by rw [← (expand R p).map_zero, expand_inj hp, alg_hom.map_zero]
theorem expand_eq_C {p : ℕ} (hp : 0 < p) {f : polynomial R} {r : R} :
expand R p f = C r ↔ f = C r :=
by rw [← expand_C, expand_inj hp, expand_C]
theorem nat_degree_expand (p : ℕ) (f : polynomial R) :
(expand R p f).nat_degree = f.nat_degree * p :=
begin
cases p.eq_zero_or_pos with hp hp,
{ rw [hp, coe_expand, pow_zero, mul_zero, ← C_1, eval₂_hom, nat_degree_C] },
by_cases hf : f = 0,
{ rw [hf, alg_hom.map_zero, nat_degree_zero, zero_mul] },
have hf1 : expand R p f ≠ 0 := mt (expand_eq_zero hp).1 hf,
rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree hf1],
refine le_antisymm ((degree_le_iff_coeff_zero _ _).2 $ λ n hn, _) _,
{ rw coeff_expand hp, split_ifs with hpn,
{ rw coeff_eq_zero_of_nat_degree_lt, contrapose! hn,
rw [with_bot.coe_le_coe, ← nat.div_mul_cancel hpn], exact nat.mul_le_mul_right p hn },
{ refl } },
{ refine le_degree_of_ne_zero _,
rw [coeff_expand_mul hp, ← leading_coeff], exact mt leading_coeff_eq_zero.1 hf }
end
lemma monic.expand {p : ℕ} {f : polynomial R} (hp : 0 < p) (h : f.monic) : (expand R p f).monic :=
begin
rw [monic.def, leading_coeff, nat_degree_expand, coeff_expand hp],
simp [hp, h],
end
theorem map_expand {p : ℕ} {f : R →+* S} {q : polynomial R} :
map f (expand R p q) = expand S p (map f q) :=
begin
by_cases hp : p = 0,
{ simp [hp] },
ext,
rw [coeff_map, coeff_expand (nat.pos_of_ne_zero hp), coeff_expand (nat.pos_of_ne_zero hp)],
split_ifs; simp,
end
/-- Expansion is injective. -/
lemma expand_injective {n : ℕ} (hn : 0 < n) :
function.injective (expand R n) :=
λ g g' h, begin
ext,
have h' : (expand R n g).coeff (n * n_1) = (expand R n g').coeff (n * n_1) :=
begin
apply polynomial.ext_iff.1,
exact h,
end,
rw [polynomial.coeff_expand hn g (n * n_1), polynomial.coeff_expand hn g' (n * n_1)] at h',
simp only [if_true, dvd_mul_right] at h',
rw (nat.mul_div_right n_1 hn) at h',
exact h',
end
@[simp]
lemma expand_eval (p : ℕ) (P : polynomial R) (r : R) : eval r (expand R p P) = eval (r ^ p) P :=
begin
refine polynomial.induction_on P (λ a, by simp) (λ f g hf hg, _) (λ n a h, by simp),
rw [alg_hom.map_add, eval_add, eval_add, hf, hg]
end
lemma is_unit_of_self_mul_dvd_separable {p q : polynomial R}
(hp : p.separable) (hq : q * q ∣ p) : is_unit q :=
begin
obtain ⟨p, rfl⟩ := hq,
apply is_coprime_self.mp,
have : is_coprime (q * (q * p)) (q * (q.derivative * p + q.derivative * p + q * p.derivative)),
{ simp only [← mul_assoc, mul_add],
convert hp,
rw [derivative_mul, derivative_mul],
ring },
exact is_coprime.of_mul_right_left (is_coprime.of_mul_left_left this)
end
/-- The opposite of `expand`: sends `∑ aₙ xⁿᵖ` to `∑ aₙ xⁿ`. -/
noncomputable def contract (p : ℕ) (f : polynomial R) : polynomial R :=
∑ n in range (f.nat_degree + 1), monomial n (f.coeff (n * p))
theorem coeff_contract {p : ℕ} (hp : p ≠ 0) (f : polynomial R) (n : ℕ) :
(contract p f).coeff n = f.coeff (n * p) :=
begin
simp only [contract, coeff_monomial, sum_ite_eq', finset_sum_coeff, mem_range, not_lt,
ite_eq_left_iff],
assume hn,
apply (coeff_eq_zero_of_nat_degree_lt _).symm,
calc f.nat_degree < f.nat_degree + 1 : nat.lt_succ_self _
... ≤ n * 1 : by simpa only [mul_one] using hn
... ≤ n * p : mul_le_mul_of_nonneg_left (show 1 ≤ p, from hp.bot_lt) (zero_le n)
end
theorem contract_expand {f : polynomial R} (hp : p ≠ 0) : contract p (expand R p f) = f :=
begin
ext,
simp [coeff_contract hp, coeff_expand hp.bot_lt, nat.mul_div_cancel _ hp.bot_lt]
end
section char_p
variable [char_p R p]
theorem expand_contract [no_zero_divisors R] {f : polynomial R} (hf : f.derivative = 0)
(hp : p ≠ 0) : expand R p (contract p f) = f :=
begin
ext n,
rw [coeff_expand hp.bot_lt, coeff_contract hp],
split_ifs with h,
{ rw nat.div_mul_cancel h },
{ cases n,
{ exact absurd (dvd_zero p) h },
have := coeff_derivative f n,
rw [hf, coeff_zero, zero_eq_mul] at this,
cases this,
{ rw this },
rw [← nat.cast_succ, char_p.cast_eq_zero_iff R p] at this,
exact absurd this h }
end
variable [hp : fact p.prime]
include hp
theorem expand_char (f : polynomial R) : map (frobenius R p) (expand R p f) = f ^ p :=
begin
refine f.induction_on' (λ a b ha hb, _) (λ n a, _),
{ rw [alg_hom.map_add, map_add, ha, hb, add_pow_char], },
{ rw [expand_monomial, map_monomial, monomial_eq_C_mul_X, monomial_eq_C_mul_X,
mul_pow, ← C.map_pow, frobenius_def],
ring_exp }
end
theorem map_expand_pow_char (f : polynomial R) (n : ℕ) :
map ((frobenius R p) ^ n) (expand R (p ^ n) f) = f ^ (p ^ n) :=
begin
induction n,
{ simp [ring_hom.one_def] },
symmetry,
rw [pow_succ', pow_mul, ← n_ih, ← expand_char, pow_succ, ring_hom.mul_def,
← map_map, mul_comm, expand_mul, ← map_expand]
end
end char_p
lemma multiplicity_le_one_of_separable {p q : polynomial R} (hq : ¬ is_unit q)
(hsep : separable p) : multiplicity q p ≤ 1 :=
begin
contrapose! hq,
apply is_unit_of_self_mul_dvd_separable hsep,
rw ← sq,
apply multiplicity.pow_dvd_of_le_multiplicity,
simpa only [nat.cast_one, nat.cast_bit0] using enat.add_one_le_of_lt hq
end
lemma separable.squarefree {p : polynomial R} (hsep : separable p) : squarefree p :=
begin
rw multiplicity.squarefree_iff_multiplicity_le_one p,
intro f,
by_cases hunit : is_unit f,
{ exact or.inr hunit },
exact or.inl (multiplicity_le_one_of_separable hunit hsep)
end
end comm_semiring
section comm_ring
variables {R : Type u} [comm_ring R]
lemma separable_X_sub_C {x : R} : separable (X - C x) :=
by simpa only [sub_eq_add_neg, C_neg] using separable_X_add_C (-x)
lemma separable.mul {f g : polynomial R} (hf : f.separable) (hg : g.separable)
(h : is_coprime f g) : (f * g).separable :=
by { rw [separable_def, derivative_mul], exact ((hf.mul_right h).add_mul_left_right _).mul_left
((h.symm.mul_right hg).mul_add_right_right _) }
lemma separable_prod' {ι : Sort*} {f : ι → polynomial R} {s : finset ι} :
(∀x∈s, ∀y∈s, x ≠ y → is_coprime (f x) (f y)) → (∀x∈s, (f x).separable) →
(∏ x in s, f x).separable :=
finset.induction_on s (λ _ _, separable_one) $ λ a s has ih h1 h2, begin
simp_rw [finset.forall_mem_insert, forall_and_distrib] at h1 h2, rw prod_insert has,
exact h2.1.mul (ih h1.2.2 h2.2) (is_coprime.prod_right $ λ i his, h1.1.2 i his $
ne.symm $ ne_of_mem_of_not_mem his has)
end
lemma separable_prod {ι : Sort*} [fintype ι] {f : ι → polynomial R}
(h1 : pairwise (is_coprime on f)) (h2 : ∀ x, (f x).separable) : (∏ x, f x).separable :=
separable_prod' (λ x hx y hy hxy, h1 x y hxy) (λ x hx, h2 x)
lemma separable.inj_of_prod_X_sub_C [nontrivial R] {ι : Sort*} {f : ι → R} {s : finset ι}
(hfs : (∏ i in s, (X - C (f i))).separable)
{x y : ι} (hx : x ∈ s) (hy : y ∈ s) (hfxy : f x = f y) : x = y :=
begin
by_contra hxy,
rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase_of_ne_of_mem (ne.symm hxy) hy),
prod_insert (not_mem_erase _ _), ← mul_assoc, hfxy, ← sq] at hfs,
cases (hfs.of_mul_left.of_pow (by exact not_is_unit_X_sub_C _) two_ne_zero).2
end
lemma separable.injective_of_prod_X_sub_C [nontrivial R] {ι : Sort*} [fintype ι] {f : ι → R}
(hfs : (∏ i, (X - C (f i))).separable) : function.injective f :=
λ x y hfxy, hfs.inj_of_prod_X_sub_C (mem_univ _) (mem_univ _) hfxy
lemma nodup_of_separable_prod [nontrivial R] {s : multiset R}
(hs : separable (multiset.map (λ a, X - C a) s).prod) : s.nodup :=
begin
rw multiset.nodup_iff_ne_cons_cons,
rintros a t rfl,
refine not_is_unit_X_sub_C a (is_unit_of_self_mul_dvd_separable hs _),
simpa only [multiset.map_cons, multiset.prod_cons] using mul_dvd_mul_left _ (dvd_mul_right _ _)
end
/--If `is_unit n` in a `comm_ring R`, then `X ^ n - u` is separable for any unit `u`. -/
lemma separable_X_pow_sub_C_unit {n : ℕ} (u : Rˣ) (hn : is_unit (n : R)) :
separable (X ^ n - C (u : R)) :=
begin
nontriviality R,
rcases n.eq_zero_or_pos with rfl | hpos,
{ simpa using hn },
apply (separable_def' (X ^ n - C (u : R))).2,
obtain ⟨n', hn'⟩ := hn.exists_left_inv,
refine ⟨-C ↑u⁻¹, C ↑u⁻¹ * C n' * X, _⟩,
rw [derivative_sub, derivative_C, sub_zero, derivative_pow X n, derivative_X, mul_one],
calc - C ↑u⁻¹ * (X ^ n - C ↑u) + C ↑u⁻¹ * C n' * X * (↑n * X ^ (n - 1))
= C (↑u⁻¹ * ↑ u) - C ↑u⁻¹ * X^n + C ↑ u ⁻¹ * C (n' * ↑n) * (X * X ^ (n - 1)) :
by { simp only [C.map_mul, C_eq_nat_cast], ring }
... = 1 : by simp only [units.inv_mul, hn', C.map_one, mul_one, ← pow_succ,
nat.sub_add_cancel (show 1 ≤ n, from hpos), sub_add_cancel]
end
lemma root_multiplicity_le_one_of_separable [nontrivial R] {p : polynomial R}
(hsep : separable p) (x : R) : root_multiplicity x p ≤ 1 :=
begin
by_cases hp : p = 0,
{ simp [hp], },
rw [root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_le_coe, enat.coe_get, nat.cast_one],
exact multiplicity_le_one_of_separable (not_is_unit_X_sub_C _) hsep
end
end comm_ring
section is_domain
variables (R : Type u) [comm_ring R] [is_domain R]
theorem is_local_ring_hom_expand {p : ℕ} (hp : 0 < p) :
is_local_ring_hom (↑(expand R p) : polynomial R →+* polynomial R) :=
begin
refine ⟨λ f hf1, _⟩, rw ← coe_fn_coe_base at hf1,
have hf2 := eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf1),
rw [coeff_expand hp, if_pos (dvd_zero _), p.zero_div] at hf2,
rw [hf2, is_unit_C] at hf1, rw expand_eq_C hp at hf2, rwa [hf2, is_unit_C]
end
variable {R}
theorem of_irreducible_expand {p : ℕ} (hp : p ≠ 0) {f : polynomial R}
(hf : irreducible (expand R p f)) : irreducible f :=
@@of_irreducible_map _ _ _ (is_local_ring_hom_expand R hp.bot_lt) hf
theorem of_irreducible_expand_pow {p : ℕ} (hp : p ≠ 0) {f : polynomial R} {n : ℕ} :
irreducible (expand R (p ^ n) f) → irreducible f :=
nat.rec_on n (λ hf, by rwa [pow_zero, expand_one] at hf) $ λ n ih hf,
ih $ of_irreducible_expand hp $ by { rw pow_succ at hf, rwa [expand_expand] }
lemma count_roots_le_one {p : polynomial R} (hsep : separable p) (x : R) :
p.roots.count x ≤ 1 :=
begin
rw count_roots p,
exact root_multiplicity_le_one_of_separable hsep x
end
lemma nodup_roots {p : polynomial R} (hsep : separable p) : p.roots.nodup :=
multiset.nodup_iff_count_le_one.mpr (count_roots_le_one hsep)
end is_domain
section field
variables {F : Type u} [field F] {K : Type v} [field K]
theorem separable_iff_derivative_ne_zero {f : polynomial F} (hf : irreducible f) :
f.separable ↔ f.derivative ≠ 0 :=
⟨λ h1 h2, hf.not_unit $ is_coprime_zero_right.1 $ h2 ▸ h1,
λ h, euclidean_domain.is_coprime_of_dvd (mt and.right h) $ λ g hg1 hg2 ⟨p, hg3⟩ hg4,
let ⟨u, hu⟩ := (hf.is_unit_or_is_unit hg3).resolve_left hg1 in
have f ∣ f.derivative, by { conv_lhs { rw [hg3, ← hu] }, rwa units.mul_right_dvd },
not_lt_of_le (nat_degree_le_of_dvd this h) $ nat_degree_derivative_lt h⟩
theorem separable_map (f : F →+* K) {p : polynomial F} : (p.map f).separable ↔ p.separable :=
by simp_rw [separable_def, derivative_map, is_coprime_map]
lemma separable_prod_X_sub_C_iff' {ι : Sort*} {f : ι → F} {s : finset ι} :
(∏ i in s, (X - C (f i))).separable ↔ (∀ (x ∈ s) (y ∈ s), f x = f y → x = y) :=
⟨λ hfs x hx y hy hfxy, hfs.inj_of_prod_X_sub_C hx hy hfxy,
λ H, by { rw ← prod_attach, exact separable_prod' (λ x hx y hy hxy,
@pairwise_coprime_X_sub _ _ { x // x ∈ s } (λ x, f x)
(λ x y hxy, subtype.eq $ H x.1 x.2 y.1 y.2 hxy) _ _ hxy)
(λ _ _, separable_X_sub_C) }⟩
lemma separable_prod_X_sub_C_iff {ι : Sort*} [fintype ι] {f : ι → F} :
(∏ i, (X - C (f i))).separable ↔ function.injective f :=
separable_prod_X_sub_C_iff'.trans $ by simp_rw [mem_univ, true_implies_iff, function.injective]
section char_p
variables (p : ℕ) [HF : char_p F p]
include HF
theorem separable_or {f : polynomial F} (hf : irreducible f) : f.separable ∨
¬f.separable ∧ ∃ g : polynomial F, irreducible g ∧ expand F p g = f :=
if H : f.derivative = 0 then
begin
unfreezingI { rcases p.eq_zero_or_pos with rfl | hp },
{ haveI := char_p.char_p_to_char_zero F,
have := nat_degree_eq_zero_of_derivative_eq_zero H,
have := (nat_degree_pos_iff_degree_pos.mpr $ degree_pos_of_irreducible hf).ne',
contradiction },
haveI := is_local_ring_hom_expand F hp,
exact or.inr
⟨by rw [separable_iff_derivative_ne_zero hf, not_not, H],
contract p f,
of_irreducible_map ↑(expand F p) (by rwa ← expand_contract p H hp.ne' at hf),
expand_contract p H hp.ne'⟩
end
else or.inl $ (separable_iff_derivative_ne_zero hf).2 H
theorem exists_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hp : p ≠ 0) :
∃ (n : ℕ) (g : polynomial F), g.separable ∧ expand F (p ^ n) g = f :=
begin
replace hp : p.prime := (char_p.char_is_prime_or_zero F p).resolve_right hp,
unfreezingI
{ induction hn : f.nat_degree using nat.strong_induction_on with N ih generalizing f },
rcases separable_or p hf with h | ⟨h1, g, hg, hgf⟩,
{ refine ⟨0, f, h, _⟩, rw [pow_zero, expand_one] },
{ cases N with N,
{ rw [nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff] at hn,
rw [hn, separable_C, is_unit_iff_ne_zero, not_not] at h1,
have hf0 : f ≠ 0 := hf.ne_zero,
rw [h1, C_0] at hn, exact absurd hn hf0 },
have hg1 : g.nat_degree * p = N.succ,
{ rwa [← nat_degree_expand, hgf] },
have hg2 : g.nat_degree ≠ 0,
{ intro this, rw [this, zero_mul] at hg1, cases hg1 },
have hg3 : g.nat_degree < N.succ,
{ rw [← mul_one g.nat_degree, ← hg1],
exact nat.mul_lt_mul_of_pos_left hp.one_lt hg2.bot_lt },
rcases ih _ hg3 hg rfl with ⟨n, g, hg4, rfl⟩, refine ⟨n+1, g, hg4, _⟩,
rw [← hgf, expand_expand, pow_succ] }
end
theorem is_unit_or_eq_zero_of_separable_expand {f : polynomial F} (n : ℕ) (hp : 0 < p)
(hf : (expand F (p ^ n) f).separable) : is_unit f ∨ n = 0 :=
begin
rw or_iff_not_imp_right,
rintro hn : n ≠ 0,
have hf2 : (expand F (p ^ n) f).derivative = 0,
{ by rw [derivative_expand, nat.cast_pow, char_p.cast_eq_zero,
zero_pow hn.bot_lt, zero_mul, mul_zero] },
rw [separable_def, hf2, is_coprime_zero_right, is_unit_iff] at hf,
rcases hf with ⟨r, hr, hrf⟩,
rw [eq_comm, expand_eq_C (pow_pos hp _)] at hrf,
rwa [hrf, is_unit_C]
end
theorem unique_separable_of_irreducible {f : polynomial F} (hf : irreducible f) (hp : 0 < p)
(n₁ : ℕ) (g₁ : polynomial F) (hg₁ : g₁.separable) (hgf₁ : expand F (p ^ n₁) g₁ = f)
(n₂ : ℕ) (g₂ : polynomial F) (hg₂ : g₂.separable) (hgf₂ : expand F (p ^ n₂) g₂ = f) :
n₁ = n₂ ∧ g₁ = g₂ :=
begin
revert g₁ g₂,
wlog hn : n₁ ≤ n₂ := le_total n₁ n₂ using [n₁ n₂, n₂ n₁],
have hf0 : f ≠ 0 := hf.ne_zero,
unfreezingI { intros, rw le_iff_exists_add at hn, rcases hn with ⟨k, rfl⟩,
rw [← hgf₁, pow_add, expand_mul, expand_inj (pow_pos hp n₁)] at hgf₂, subst hgf₂,
subst hgf₁,
rcases is_unit_or_eq_zero_of_separable_expand p k hp hg₁ with h | rfl,
{ rw is_unit_iff at h, rcases h with ⟨r, hr, rfl⟩,
simp_rw expand_C at hf, exact absurd (is_unit_C.2 hr) hf.1 },
{ rw [add_zero, pow_zero, expand_one], split; refl } },
obtain ⟨hn, hg⟩ := this g₂ g₁ hg₂ hgf₂ hg₁ hgf₁,
exact ⟨hn.symm, hg.symm⟩
end
end char_p
/--If `n ≠ 0` in `F`, then ` X ^ n - a` is separable for any `a ≠ 0`. -/
lemma separable_X_pow_sub_C {n : ℕ} (a : F) (hn : (n : F) ≠ 0) (ha : a ≠ 0) :
separable (X ^ n - C a) :=
separable_X_pow_sub_C_unit (units.mk0 a ha) (is_unit.mk0 n hn)
-- this can possibly be strengthened to making `separable_X_pow_sub_C_unit` a
-- bi-implication, but it is nontrivial!
/-- In a field `F`, `X ^ n - 1` is separable iff `↑n ≠ 0`. -/
lemma X_pow_sub_one_separable_iff {n : ℕ} :
(X ^ n - 1 : polynomial F).separable ↔ (n : F) ≠ 0 :=
begin
refine ⟨_, λ h, separable_X_pow_sub_C_unit 1 (is_unit.mk0 ↑n h)⟩,
rw [separable_def', derivative_sub, derivative_X_pow, derivative_one, sub_zero],
-- Suppose `(n : F) = 0`, then the derivative is `0`, so `X ^ n - 1` is a unit, contradiction.
rintro (h : is_coprime _ _) hn',
rw [← C_eq_nat_cast, hn', C.map_zero, zero_mul, is_coprime_zero_right] at h,
have := not_is_unit_X_pow_sub_one F n,
contradiction
end
section splits
lemma card_root_set_eq_nat_degree [algebra F K] {p : polynomial F} (hsep : p.separable)
(hsplit : splits (algebra_map F K) p) : fintype.card (p.root_set K) = p.nat_degree :=
begin
simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe],
rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots hsplit],
exact nodup_roots hsep.map,
end
variable {i : F →+* K}
lemma eq_X_sub_C_of_separable_of_root_eq {x : F} {h : polynomial F}
(h_sep : h.separable) (h_root : h.eval x = 0) (h_splits : splits i h)
(h_roots : ∀ y ∈ (h.map i).roots, y = i x) : h = (C (leading_coeff h)) * (X - C x) :=
begin
have h_ne_zero : h ≠ 0 := by { rintro rfl, exact not_separable_zero h_sep },
apply polynomial.eq_X_sub_C_of_splits_of_single_root i h_splits,
apply finset.mk.inj,
{ change _ = {i x},
rw finset.eq_singleton_iff_unique_mem,
split,
{ apply finset.mem_mk.mpr,
rw mem_roots (show h.map i ≠ 0, by exact map_ne_zero h_ne_zero),
rw [is_root.def,←eval₂_eq_eval_map,eval₂_hom,h_root],
exact ring_hom.map_zero i },
{ exact h_roots } },
{ exact nodup_roots (separable.map h_sep) },
end
lemma exists_finset_of_splits
(i : F →+* K) {f : polynomial F} (sep : separable f) (sp : splits i f) :
∃ (s : finset K), f.map i =
C (i f.leading_coeff) * (s.prod (λ a : K, (X : polynomial K) - C a)) :=
begin
classical,
obtain ⟨s, h⟩ := exists_multiset_of_splits i sp,
use s.to_finset,
rw [h, finset.prod_eq_multiset_prod, ←multiset.to_finset_eq],
apply nodup_of_separable_prod,
apply separable.of_mul_right,
rw ←h,
exact sep.map,
end
end splits
theorem _root_.irreducible.separable [char_zero F] {f : polynomial F}
(hf : irreducible f) : f.separable :=
begin
rw [separable_iff_derivative_ne_zero hf, ne, ← degree_eq_bot, degree_derivative_eq],
{ rintro ⟨⟩ },
rw [pos_iff_ne_zero, ne, nat_degree_eq_zero_iff_degree_le_zero, degree_le_zero_iff],
refine λ hf1, hf.not_unit _,
rw [hf1, is_unit_C, is_unit_iff_ne_zero],
intro hf2,
rw [hf2, C_0] at hf1,
exact absurd hf1 hf.ne_zero
end
end field
end polynomial
open polynomial
section comm_ring
variables (F K : Type*) [comm_ring F] [ring K] [algebra F K]
-- TODO: refactor to allow transcendental extensions?
-- See: https://en.wikipedia.org/wiki/Separable_extension#Separability_of_transcendental_extensions
-- Note that right now a Galois extension (class `is_galois`) is defined to be an extension which
-- is separable and normal, so if the definition of separable changes here at some point
-- to allow non-algebraic extensions, then the definition of `is_galois` must also be changed.
/-- Typeclass for separable field extension: `K` is a separable field extension of `F` iff
the minimal polynomial of every `x : K` is separable.
We define this for general (commutative) rings and only assume `F` and `K` are fields if this
is needed for a proof.
-/
class is_separable : Prop :=
(is_integral' (x : K) : is_integral F x)
(separable' (x : K) : (minpoly F x).separable)
variables (F) {K}
theorem is_separable.is_integral [is_separable F K] :
∀ x : K, is_integral F x := is_separable.is_integral'
theorem is_separable.separable [is_separable F K] :
∀ x : K, (minpoly F x).separable := is_separable.separable'
variables {F K}
theorem is_separable_iff : is_separable F K ↔ ∀ x : K, is_integral F x ∧ (minpoly F x).separable :=
⟨λ h x, ⟨@@is_separable.is_integral F _ _ _ h x, @@is_separable.separable F _ _ _ h x⟩,
λ h, ⟨λ x, (h x).1, λ x, (h x).2⟩⟩
end comm_ring
instance is_separable_self (F : Type*) [field F] : is_separable F F :=
⟨λ x, is_integral_algebra_map, λ x, by { rw minpoly.eq_X_sub_C', exact separable_X_sub_C }⟩
/-- A finite field extension in characteristic 0 is separable. -/
@[priority 100] -- See note [lower instance priority]
instance is_separable.of_finite (F K : Type*) [field F] [field K] [algebra F K]
[finite_dimensional F K] [char_zero F] : is_separable F K :=
have ∀ (x : K), is_integral F x,
from λ x, algebra.is_integral_of_finite _ _ _,
⟨this, λ x, (minpoly.irreducible (this x)).separable⟩
section is_separable_tower
variables (F K E : Type*) [field F] [field K] [field E] [algebra F K] [algebra F E]
[algebra K E] [is_scalar_tower F K E]
lemma is_separable_tower_top_of_is_separable [is_separable F E] : is_separable K E :=
⟨λ x, is_integral_of_is_scalar_tower x (is_separable.is_integral F x),
λ x, (is_separable.separable F x).map.of_dvd (minpoly.dvd_map_of_is_scalar_tower _ _ _)⟩
lemma is_separable_tower_bot_of_is_separable [h : is_separable F E] : is_separable F K :=
is_separable_iff.2 $ λ x, begin
refine (is_separable_iff.1 h (algebra_map K E x)).imp
is_integral_tower_bot_of_is_integral_field (λ hs, _),
obtain ⟨q, hq⟩ := minpoly.dvd F x
(is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero_field
(minpoly.aeval F ((algebra_map K E) x))),
rw hq at hs,
exact hs.of_mul_left
end
variables {E}
lemma is_separable.of_alg_hom (E' : Type*) [field E'] [algebra F E']
(f : E →ₐ[F] E') [is_separable F E'] : is_separable F E :=
begin
letI : algebra E E' := ring_hom.to_algebra f.to_ring_hom,
haveI : is_scalar_tower F E E' := is_scalar_tower.of_algebra_map_eq (λ x, (f.commutes x).symm),
exact is_separable_tower_bot_of_is_separable F E E',
end
end is_separable_tower
section card_alg_hom
variables {R S T : Type*} [comm_ring S]
variables {K L F : Type*} [field K] [field L] [field F]
variables [algebra K S] [algebra K L]
lemma alg_hom.card_of_power_basis (pb : power_basis K S) (h_sep : (minpoly K pb.gen).separable)
(h_splits : (minpoly K pb.gen).splits (algebra_map K L)) :
@fintype.card (S →ₐ[K] L) (power_basis.alg_hom.fintype pb) = pb.dim :=
begin
let s := ((minpoly K pb.gen).map (algebra_map K L)).roots.to_finset,
have H := λ x, multiset.mem_to_finset,
rw [fintype.card_congr pb.lift_equiv', fintype.card_of_subtype s H,
← pb.nat_degree_minpoly, nat_degree_eq_card_roots h_splits, multiset.to_finset_card_of_nodup],
exact nodup_roots ((separable_map (algebra_map K L)).mpr h_sep)
end
end card_alg_hom
|
6c7f4f1389dc80fe49faa8673cb5c1e533918116 | b328e8ebb2ba923140e5137c83f09fa59516b793 | /stage0/src/Lean/Meta/Tactic/Induction.lean | d60cf0fee1dd0d21c213c594666fe47d0b92c8f8 | [
"Apache-2.0"
] | permissive | DrMaxis/lean4 | a781bcc095511687c56ab060e816fd948553e162 | 5a02c4facc0658aad627cfdcc3db203eac0cb544 | refs/heads/master | 1,677,051,517,055 | 1,611,876,226,000 | 1,611,876,226,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,548 | 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.RecursorInfo
import Lean.Meta.SynthInstance
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.FVarSubst
namespace Lean.Meta
private partial def getTargetArity : Expr → Nat
| Expr.mdata _ b _ => getTargetArity b
| Expr.forallE _ _ b _ => getTargetArity b + 1
| e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0
private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) → Expr → MetaM Expr
| [], recursor => pure recursor
| some pos :: rest, recursor =>
if h : pos < majorTypeArgs.size then
addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get ⟨pos, h⟩))
else
throwTacticEx `induction mvarId "ill-formed recursor"
| none :: rest, recursor => do
let recursorType ← inferType recursor
let recursorType ← whnfForall recursorType
match recursorType with
| Expr.forallE _ d _ _ => do
let param ← try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter"
addRecParams mvarId majorTypeArgs rest (mkApp recursor param)
| _ =>
throwTacticEx `induction mvarId "ill-formed recursor"
structure InductionSubgoal where
mvarId : MVarId
fields : Array Expr := #[]
subst : FVarSubst := {}
deriving Inhabited
private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do
let type ← whnfForall type
match type with
| Expr.forallE _ _ b _ => pure $ b.instantiate1 x
| _ => throwTacticEx `induction mvarId "ill-formed recursor"
private partial def finalize
(mvarId : MVarId) (givenNames : Array (List Name)) (recursorInfo : RecursorInfo)
(reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr)
: MetaM (Array InductionSubgoal) := do
let target ← getMVarType mvarId
let initialArity := getTargetArity target
let recursorType ← inferType recursor
let numMinors := recursorInfo.produceMotive.length
let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do
let recursorType ← whnfForall recursorType
if recursorType.isForall && pos < recursorInfo.numArgs then
if pos == recursorInfo.firstIndexPos then
let (recursor, recursorType) ← indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do
let recursor := mkApp recursor index
let recursorType ← getTypeBody mvarId recursorType index
pure (recursor, recursorType)
let recursor := mkApp recursor major
let recursorType ← getTypeBody mvarId recursorType major
loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals
else
-- consume motive
let tag ← getMVarTag mvarId
if minorIdx ≥ numMinors then throwTacticEx `induction mvarId "ill-formed recursor"
match recursorType with
| Expr.forallE n d b c =>
let d := d.headBeta
-- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise
if c.binderInfo.isInstImplicit && givenNames.isEmpty then
match (← synthInstance? d) with
| some inst =>
let recursor := mkApp recursor inst
let recursorType ← getTypeBody mvarId recursorType inst
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals
| none => do
-- Add newSubgoal if type class resolution failed
let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType ← getTypeBody mvarId recursorType mvar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! })
else
let arity := getTargetArity d
if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor"
let nparams := arity - initialArity -- number of fields due to minor premise
let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted
let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else []
let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType ← getTypeBody mvarId recursorType mvar
-- Try to clear major premise from new goal
let mvarId' ← tryClear mvar.mvarId! major.fvarId!
let (fields, mvarId') ← introN mvarId' nparams minorGivenNames
let (extra, mvarId') ← introNP mvarId' nextra
let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) =>
if i < indices.size + 1 then subst
else
let revertedFVarId := reverted[i]
let newFVarId := extra[i - indices.size - 1]
subst.insert revertedFVarId (mkFVar newFVarId)
let fields := fields.map mkFVar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst })
| _ => unreachable!
else
unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor"
assignExprMVar mvarId recursor
pure subgoals
loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[]
private def throwUnexpectedMajorType {α} (mvarId : MVarId) (majorType : Expr) : MetaM α :=
throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}"
def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array (List Name) := #[]) (useUnusedNames := false) :
MetaM (Array InductionSubgoal) :=
withMVarContext mvarId do
checkNotAssigned mvarId `induction
let majorLocalDecl ← getLocalDecl majorFVarId
let recursorInfo ← mkRecursorInfo recursorName
let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun _ majorTypeArgs => do
recursorInfo.paramsPos.forM fun paramPos? => do
match paramPos? with
| none => pure ()
| some paramPos => if paramPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let mctx ← getMCtx
let indices ← recursorInfo.indicesPos.toArray.mapM fun idxPos => do
if idxPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let idx := majorTypeArgs.get! idxPos
unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}"
majorTypeArgs.size.forM fun i => do
let arg := majorTypeArgs[i]
if i != idxPos && arg == idx then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}"
if i < idxPos && mctx.exprDependsOn arg idx.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}"
-- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`.
-- Note that if `arg` is not a variable, we will fail anyway when we visit it.
if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then
let idxDecl ← getLocalDecl idx.fvarId!
if mctx.localDeclDependsOn idxDecl arg.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}"
pure idx
let target ← getMVarType mvarId
if !recursorInfo.depElim && mctx.exprDependsOn target majorFVarId then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise"
-- Revert indices and major premise preserving variable order
let (reverted, mvarId) ← revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true
-- Re-introduce indices and major
let (indices', mvarId) ← introNP mvarId indices.size
let (majorFVarId', mvarId) ← intro1P mvarId
-- Create FVarSubst with indices
let baseSubst := do
let mut subst : FVarSubst := {}
let mut i := 0
for index in indices do
subst := subst.insert index.fvarId! (mkFVar indices'[i])
i := i + 1
pure subst
trace[Meta.Tactic.induction]! "after revert&intro\n{MessageData.ofGoal mvarId}"
-- Update indices and major
let indices := indices'.map mkFVar
let majorFVarId := majorFVarId'
let major := mkFVar majorFVarId
withMVarContext mvarId do
let target ← getMVarType mvarId
let targetLevel ← getLevel target
let targetLevel ← normalizeLevel targetLevel
let majorLocalDecl ← getLocalDecl majorFVarId
let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun majorTypeFn majorTypeArgs => do
match majorTypeFn with
| Expr.const majorTypeFnName majorTypeFnLevels _ => do
let majorTypeFnLevels := majorTypeFnLevels.toArray
let (recursorLevels, foundTargetLevel) ← recursorInfo.univLevelPos.foldlM (init := (#[], false))
fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do
match univPos with
| RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true)
| RecursorUnivLevelPos.majorType idx =>
if idx ≥ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor"
pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel)
if !foundTargetLevel && !targetLevel.isZero then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop"
let recursor := mkConst recursorName recursorLevels.toList
let recursor ← addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor
-- Compute motive
let motive := target
let motive ← if recursorInfo.depElim then mkLambdaFVars #[major] motive else pure motive
let motive ← mkLambdaFVars indices motive
let recursor := mkApp recursor motive
finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor
| _ =>
throwTacticEx `induction mvarId "major premise is not of the form (C ...)"
builtin_initialize registerTraceClass `Meta.Tactic.induction
end Lean.Meta
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.