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
249f6c1f289fa37da3468ff1b175474f023d25fd
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/algebra/equiv.lean
02524aa77f60d575d3f8f5d68a8cc8e26bad42ec
[ "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
21,501
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.hom /-! # Isomorphisms of `R`-algebras This file defines bundled isomorphisms of `R`-algebras. ## Main definitions * `alg_equiv R A B`: the type of `R`-algebra isomorphisms between `A` and `B`. ## Notations * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ open_locale big_operators universes u v w u₁ v₁ set_option old_structure_cmd true /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure alg_equiv (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) attribute [nolint doc_blame] alg_equiv.to_ring_equiv attribute [nolint doc_blame] alg_equiv.to_equiv attribute [nolint doc_blame] alg_equiv.to_add_equiv attribute [nolint doc_blame] alg_equiv.to_mul_equiv notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A' /-- `alg_equiv_class F R A B` states that `F` is a type of algebra structure preserving equivalences. You should extend this class when you extend `alg_equiv`. -/ class alg_equiv_class (F : Type*) (R A B : out_param Type*) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_equiv_class F A B := (commutes : ∀ (f : F) (r : R), f (algebra_map R A r) = algebra_map R B r) -- `R` becomes a metavariable but that's fine because it's an `out_param` attribute [nolint dangerous_instance] alg_equiv_class.to_ring_equiv_class namespace alg_equiv_class @[priority 100] -- See note [lower instance priority] instance to_alg_hom_class (F R A B : Type*) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] [h : alg_equiv_class F R A B] : alg_hom_class F R A B := { coe := coe_fn, coe_injective' := fun_like.coe_injective, map_zero := map_zero, map_one := map_one, .. h } @[priority 100] instance to_linear_equiv_class (F R A B : Type*) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] [h : alg_equiv_class F R A B] : linear_equiv_class F R A B := { map_smulₛₗ := λ f, map_smulₛₗ f, ..h } instance (F R A B : Type*) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] [h : alg_equiv_class F R A B] : has_coe_t F (A ≃ₐ[R] B) := { coe := λ f, { to_fun := f, inv_fun := equiv_like.inv f, commutes' := alg_hom_class.commutes f, .. (f : A ≃+* B) } } end alg_equiv_class namespace alg_equiv variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁} section semiring variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] variables (e : A₁ ≃ₐ[R] A₂) instance : alg_equiv_class (A₁ ≃ₐ[R] A₂) R A₁ A₂ := { coe := to_fun, inv := inv_fun, coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' }, map_add := map_add', map_mul := map_mul', commutes := commutes', left_inv := left_inv, right_inv := right_inv } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) (λ _, A₁ → A₂) := ⟨alg_equiv.to_fun⟩ @[simp, protected] lemma coe_coe {F : Type*} [alg_equiv_class F R A₁ A₂] (f : F) : ⇑(f : A₁ ≃ₐ[R] A₂) = f := rfl @[ext] lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} {x x' : A₁} : x = x' → f x = f x' := fun_like.congr_arg f protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := fun_like.congr_fun h x protected lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) := fun_like.coe_injective instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩ @[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} : ⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun := rfl @[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) : (⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl @[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl @[simp] lemma to_equiv_eq_coe : e.to_equiv = e := rfl @[simp] lemma to_ring_equiv_eq_coe : e.to_ring_equiv = e := rfl @[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl lemma coe_ring_equiv_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ ≃+* A₂)) := λ e₁ e₂ h, ext $ ring_equiv.congr_fun h protected lemma map_add : ∀ x y, e (x + y) = e x + e y := map_add e protected lemma map_zero : e 0 = 0 := map_zero e protected lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := map_mul e protected lemma map_one : e 1 = 1 := map_one e @[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r := e.commutes' @[simp] lemma map_smul (r : R) (x : A₁) : e (r • x) = r • e x := by simp only [algebra.smul_def, map_mul, commutes] lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∑ x in s, f x) = ∑ x in s, e (f x) := e.to_add_equiv.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum (λ i b, e (g i b)) := e.map_sum _ _ /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to_*_hom` projections. The `simp` normal form is to use the coercion of the `alg_hom_class.has_coe_t` instance. -/ def to_alg_hom : A₁ →ₐ[R] A₂ := { map_one' := e.map_one, map_zero' := e.map_zero, ..e } @[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl @[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e := rfl lemma coe_alg_hom_injective : function.injective (coe : (A₁ ≃ₐ[R] A₂) → (A₁ →ₐ[R] A₂)) := λ e₁ e₂ h, ext $ alg_hom.congr_fun h /-- The two paths coercion can take to a `ring_hom` are equivalent -/ lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) := rfl protected lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := map_pow _ protected lemma injective : function.injective e := equiv_like.injective e protected lemma surjective : function.surjective e := equiv_like.surjective e protected lemma bijective : function.bijective e := equiv_like.bijective e /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := {commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)} instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨refl⟩ @[simp] lemma refl_to_alg_hom : ↑(refl : A₁ ≃ₐ[R] A₁) = alg_hom.id R A₁ := rfl @[simp] lemma coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr, change _ = e _, rw e.commutes, }, ..e.to_ring_equiv.symm, } /-- See Note [custom simps projection] -/ def simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply) @[simp] lemma coe_apply_coe_coe_symm_apply {F : Type*} [alg_equiv_class F R A₁ A₂] (f : F) (x : A₂) : f ((f : A₁ ≃ₐ[R] A₂).symm x) = x := equiv_like.right_inv f x @[simp] lemma coe_coe_symm_apply_coe_apply {F : Type*} [alg_equiv_class F R A₁ A₂] (f : F) (x : A₁) : (f : A₁ ≃ₐ[R] A₂).symm (f x) = x := equiv_like.left_inv f x @[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl @[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e := by { ext, refl, } lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) : (⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) : (⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm = { to_fun := f', inv_fun := f, ..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl @[simp] theorem refl_symm : (alg_equiv.refl : A₁ ≃ₐ[R] A₁).symm = alg_equiv.refl := rfl --this should be a simp lemma but causes a lint timeout lemma to_ring_equiv_symm (f : A₁ ≃ₐ[R] A₁) : (f : A₁ ≃+* A₁).symm = f.symm := rfl @[simp] lemma symm_to_ring_equiv : (e.symm : A₂ ≃+* A₁) = (e : A₁ ≃+* A₂).symm := rfl /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'], ..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), } @[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp] lemma symm_trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₃) : (e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl @[simp] lemma coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ := by { ext, simp } @[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ := by { ext, simp } theorem left_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.left_inverse e.symm e := e.left_inv theorem right_inverse_symm (e : A₁ ≃ₐ[R] A₂) : function.right_inverse e.symm e := e.right_inv /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') := { to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom, inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom, left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp], simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] }, right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm], simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } } lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply], congr, exact (e₂.symm_apply_apply _).symm } @[simp] lemma arrow_congr_refl : arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) := by { ext, refl } @[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := by { ext, refl } @[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := by { ext, refl } /-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/ def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ := { to_fun := f, inv_fun := g, left_inv := alg_hom.ext_iff.1 h₂, right_inv := alg_hom.ext_iff.1 h₁, ..f } lemma coe_alg_hom_of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : ↑(of_alg_hom f g h₁ h₂) = f := alg_hom.ext $ λ _, rfl @[simp] lemma of_alg_hom_coe_alg_hom (f : A₁ ≃ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : of_alg_hom ↑f g h₁ h₂ = f := ext $ λ _, rfl lemma of_alg_hom_symm (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : (of_alg_hom f g h₁ h₂).symm = of_alg_hom g f h₂ h₁ := rfl /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ := { .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f } @[simp] lemma coe_of_bijective {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} : (alg_equiv.of_bijective f hf : A₁ → A₂) = f := rfl lemma of_bijective_apply {f : A₁ →ₐ[R] A₂} {hf : function.bijective f} (a : A₁) : (alg_equiv.of_bijective f hf) a = f a := rfl /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ @[simps apply] def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { to_fun := e, map_smul' := e.map_smul, inv_fun := e.symm, .. e } @[simp] lemma to_linear_equiv_refl : (alg_equiv.refl : A₁ ≃ₐ[R] A₁).to_linear_equiv = linear_equiv.refl R A₁ := rfl @[simp] lemma to_linear_equiv_symm (e : A₁ ≃ₐ[R] A₂) : e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl @[simp] lemma to_linear_equiv_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : (e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := rfl theorem to_linear_equiv_injective : function.injective (to_linear_equiv : _ → (A₁ ≃ₗ[R] A₂)) := λ e₁ e₂ h, ext $ linear_equiv.congr_fun h /-- Interpret an algebra equivalence as a linear map. -/ def to_linear_map : A₁ →ₗ[R] A₂ := e.to_alg_hom.to_linear_map @[simp] lemma to_alg_hom_to_linear_map : (e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_equiv_to_linear_map : e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl theorem to_linear_map_injective : function.injective (to_linear_map : _ → (A₁ →ₗ[R] A₂)) := λ e₁ e₂ h, ext $ linear_map.congr_fun h @[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl section of_linear_equiv variables (l : A₁ ≃ₗ[R] A₂) (map_mul : ∀ x y : A₁, l (x * y) = l x * l y) (commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r) /-- Upgrade a linear equivalence to an algebra equivalence, given that it distributes over multiplication and action of scalars. -/ @[simps apply] def of_linear_equiv : A₁ ≃ₐ[R] A₂ := { to_fun := l, inv_fun := l.symm, map_mul' := map_mul, commutes' := commutes, ..l } @[simp] lemma of_linear_equiv_symm : (of_linear_equiv l map_mul commutes).symm = of_linear_equiv l.symm ((of_linear_equiv l map_mul commutes).symm.map_mul) ((of_linear_equiv l map_mul commutes).symm.commutes) := rfl @[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) : of_linear_equiv e.to_linear_equiv map_mul commutes = e := by { ext, refl } @[simp] lemma to_linear_equiv_of_linear_equiv : to_linear_equiv (of_linear_equiv l map_mul commutes) = l := by { ext, refl } end of_linear_equiv section of_ring_equiv /-- Promotes a linear ring_equiv to an alg_equiv. -/ @[simps] def of_ring_equiv {f : A₁ ≃+* A₂} (hf : ∀ x, f (algebra_map R A₁ x) = algebra_map R A₂ x) : A₁ ≃ₐ[R] A₂ := { to_fun := f, inv_fun := f.symm, commutes' := hf, .. f } end of_ring_equiv @[simps mul one {attrs := []}] instance aut : group (A₁ ≃ₐ[R] A₁) := { mul := λ ϕ ψ, ψ.trans ϕ, mul_assoc := λ ϕ ψ χ, rfl, one := refl, one_mul := λ ϕ, ext $ λ x, rfl, mul_one := λ ϕ, ext $ λ x, rfl, inv := symm, mul_left_inv := λ ϕ, ext $ symm_apply_apply ϕ } @[simp] lemma one_apply (x : A₁) : (1 : A₁ ≃ₐ[R] A₁) x = x := rfl @[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl /-- An algebra isomorphism induces a group isomorphism between automorphism groups -/ @[simps apply] def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) := { to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ), inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm), left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] }, right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] }, map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } } @[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) := by { ext, refl } @[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl @[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) : (aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl /-- The tautological action by `A₁ ≃ₐ[R] A₁` on `A₁`. This generalizes `function.End.apply_mul_action`. -/ instance apply_mul_semiring_action : mul_semiring_action (A₁ ≃ₐ[R] A₁) A₁ := { smul := ($), smul_zero := alg_equiv.map_zero, smul_add := alg_equiv.map_add, smul_one := alg_equiv.map_one, smul_mul := alg_equiv.map_mul, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : A₁ ≃ₐ[R] A₁) (a : A₁) : f • a = f a := rfl instance apply_has_faithful_smul : has_faithful_smul (A₁ ≃ₐ[R] A₁) A₁ := ⟨λ _ _, alg_equiv.ext⟩ instance apply_smul_comm_class : smul_comm_class R (A₁ ≃ₐ[R] A₁) A₁ := { smul_comm := λ r e a, (e.map_smul r a).symm } instance apply_smul_comm_class' : smul_comm_class (A₁ ≃ₐ[R] A₁) R A₁ := { smul_comm := λ e r a, (e.map_smul r a) } @[simp] lemma algebra_map_eq_apply (e : A₁ ≃ₐ[R] A₂) {y : R} {x : A₁} : (algebra_map R A₂ y = e x) ↔ (algebra_map R A₁ y = x) := ⟨λ h, by simpa using e.symm.to_alg_hom.algebra_map_eq_apply h, λ h, e.to_alg_hom.algebra_map_eq_apply h⟩ end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∏ x in s, f x) = ∏ x in s, e (f x) := map_prod _ f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod (λ i a, e (g i a)) := map_finsupp_prod _ f g end comm_semiring section ring variables [comm_semiring R] [ring A₁] [ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) protected lemma map_neg (x) : e (-x) = -e x := map_neg e x protected lemma map_sub (x y) : e (x - y) = e x - e y := map_sub e x y end ring end alg_equiv namespace mul_semiring_action variables {M G : Type*} (R A : Type*) [comm_semiring R] [semiring A] [algebra R A] section variables [group G] [mul_semiring_action G A] [smul_comm_class G R A] /-- Each element of the group defines a algebra equivalence. This is a stronger version of `mul_semiring_action.to_ring_equiv` and `distrib_mul_action.to_linear_equiv`. -/ @[simps] def to_alg_equiv (g : G) : A ≃ₐ[R] A := { .. mul_semiring_action.to_ring_equiv _ _ g, .. mul_semiring_action.to_alg_hom R A g } theorem to_alg_equiv_injective [has_faithful_smul G A] : function.injective (mul_semiring_action.to_alg_equiv R A : G → A ≃ₐ[R] A) := λ m₁ m₂ h, eq_of_smul_eq_smul $ λ r, alg_equiv.ext_iff.1 h r end end mul_semiring_action
65ff5d76272ebc8053b4f712c70149fb7e9315a2
ac2987d8c7832fb4a87edb6bee26141facbb6fa0
/Mathlib/Data/Prod.lean
bf1d833b7a99f9ce13ce07f38b3a7a2fd4ff43c9
[ "Apache-2.0" ]
permissive
AurelienSaue/mathlib4
52204b9bd9d207c922fe0cf3397166728bb6c2e2
84271fe0875bafdaa88ac41f1b5a7c18151bd0d5
refs/heads/master
1,689,156,096,545
1,629,378,840,000
1,629,378,840,000
389,648,603
0
0
Apache-2.0
1,627,307,284,000
1,627,307,284,000
null
UTF-8
Lean
false
false
6,816
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Function import Mathlib.Logic.Basic import Mathlib.Logic.Function.Basic import Mathlib.Tactic.Basic /-! # Extra facts about `Prod` This file defines `Prod.swap : α × β → β × α` and proves various simple lemmas about `Prod`. -/ variable {α : Type _} {β : Type _} {γ : Type _} {δ : Type _} @[simp] lemma prod_map (f : α → γ) (g : β → δ) (p : α × β) : Prod.map f g p = (f p.1, g p.2) := by have : p = ⟨p.1, p.2⟩ := (Prod.ext _).symm rw [this] exact rfl namespace Prod @[simp] theorem «forall» {p : α × β → Prop} : (∀ x, p x) ↔ (∀ a b, p (a, b)) := ⟨λ h a b => h (a, b), λ h ⟨a, b⟩ => h a b⟩ @[simp] theorem «exists» {p : α × β → Prop} : (∃ x, p x) ↔ (∃ a b, p (a, b)) := ⟨λ ⟨⟨a, b⟩, h⟩ => ⟨a, b, h⟩, λ ⟨a, b, h⟩ => ⟨⟨a, b⟩, h⟩⟩ theorem forall' {p : α → β → Prop} : (∀ x : α × β, p x.1 x.2) ↔ ∀ a b, p a b := Prod.forall theorem exists' {p : α → β → Prop} : (∃ x : α × β, p x.1 x.2) ↔ ∃ a b, p a b := Prod.exists @[simp] lemma map_mk (f : α → γ) (g : β → δ) (a : α) (b : β) : map f g (a, b) = (f a, g b) := rfl lemma map_fst (f : α → γ) (g : β → δ) (p : α × β) : (map f g p).1 = f (p.1) := by simp lemma map_snd (f : α → γ) (g : β → δ) (p : α × β) : (map f g p).2 = g (p.2) := by simp lemma map_fst' (f : α → γ) (g : β → δ) : (Prod.fst ∘ map f g) = f ∘ Prod.fst := funext $ map_fst f g lemma map_snd' (f : α → γ) (g : β → δ) : (Prod.snd ∘ map f g) = g ∘ Prod.snd := funext $ map_snd f g /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions. -/ lemma map_comp_map {ε ζ : Type _} (f : α → β) (f' : γ → δ) (g : β → ε) (g' : δ → ζ) : Prod.map g g' ∘ Prod.map f f' = Prod.map (g ∘ f) (g' ∘ f') := funext (by intro x; simp) /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions, fully applied. -/ lemma map_map {ε ζ : Type _} (f : α → β) (f' : γ → δ) (g : β → ε) (g' : δ → ζ) (x : α × γ) : Prod.map g g' (Prod.map f f' x) = Prod.map (g ∘ f) (g' ∘ f') x := by simp @[simp] theorem mk.inj_iff {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ (a₁ = a₂ ∧ b₁ = b₂) := ⟨Prod.mk.inj, by intro hab; rw [hab.left, hab.right]⟩ lemma mk.inj_left {α β : Type _} (a : α) : Function.injective (Prod.mk a : β → α × β) := by intros b₁ b₂ h exact (Prod.mk.inj h).right lemma mk.inj_right {α β : Type _} (b : β) : Function.injective (λ a => Prod.mk a b : α → α × β) := by intros b₁ b₂ h exact (Prod.mk.inj h).left -- Port note: this lemma comes from lean3/library/init/data/prod.lean. @[simp] lemma mk.eta : ∀{p : α × β}, (p.1, p.2) = p | (a,b) => rfl lemma ext_iff {p q : α × β} : p = q ↔ p.1 = q.1 ∧ p.2 = q.2 := by rw [← @mk.eta _ _ p, ← @mk.eta _ _ q, mk.inj_iff] -- Port note: in mathlib this is named `ext`, but Lean4 has already defined that to be something -- with a slightly different signature. lemma ext' {α β} {p q : α × β} (h₁ : p.1 = q.1) (h₂ : p.2 = q.2) : p = q := ext_iff.2 ⟨h₁, h₂⟩ lemma map_def {f : α → γ} {g : β → δ} : Prod.map f g = λ (p : α × β) => (f p.1, g p.2) := funext (λ p => ext' (map_fst f g p) (map_snd f g p)) lemma id_prod : (λ (p : α × α) => (p.1, p.2)) = id := funext $ λ ⟨a, b⟩ => rfl lemma fst_surjective [h : Nonempty β] : Function.surjective (@fst α β) := λ x => h.elim $ λ y => ⟨⟨x, y⟩, rfl⟩ lemma snd_surjective [h : Nonempty α] : Function.surjective (@snd α β) := λ y => h.elim $ λ x => ⟨⟨x, y⟩, rfl⟩ lemma fst_injective [Subsingleton β] : Function.injective (@fst α β) := λ {x y} h => ext' h (Subsingleton.elim x.snd y.snd) lemma snd_injective [Subsingleton α] : Function.injective (@snd α β) := λ {x y} h => ext' (Subsingleton.elim _ _) h /-- Swap the factors of a product. `swap (a, b) = (b, a)` -/ def swap : α × β → β × α := λp => (p.2, p.1) @[simp] lemma swap_swap : ∀ x : α × β, swap (swap x) = x | ⟨a, b⟩ => rfl @[simp] lemma fst_swap {p : α × β} : (swap p).1 = p.2 := rfl @[simp] lemma snd_swap {p : α × β} : (swap p).2 = p.1 := rfl @[simp] lemma swap_prod_mk {a : α} {b : β} : swap (a, b) = (b, a) := rfl @[simp] lemma swap_swap_eq : swap ∘ swap = @id (α × β) := funext swap_swap @[simp] lemma swap_left_inverse : Function.left_inverse (@swap α β) swap := swap_swap @[simp] lemma swap_right_inverse : Function.right_inverse (@swap α β) swap := swap_swap lemma swap_injective : Function.injective (@swap α β) := swap_left_inverse.injective lemma swap_surjective : Function.surjective (@swap α β) := Function.right_inverse.surjective swap_left_inverse lemma swap_bijective : Function.bijective (@swap α β) := ⟨swap_injective, swap_surjective⟩ @[simp] lemma swap_inj {p q : α × β} : swap p = swap q ↔ p = q := swap_injective.eq_iff lemma eq_iff_fst_eq_snd_eq : ∀{p q : α × β}, p = q ↔ (p.1 = q.1 ∧ p.2 = q.2) | ⟨p₁, p₂⟩, ⟨q₁, q₂⟩ => by simp lemma fst_eq_iff : ∀ {p : α × β} {x : α}, p.1 = x ↔ p = (x, p.2) | ⟨a, b⟩, x => by simp lemma snd_eq_iff : ∀ {p : α × β} {x : β}, p.2 = x ↔ p = (p.1, x) | ⟨a, b⟩, x => by simp theorem lex_def (r : α → α → Prop) (s : β → β → Prop) {p q : α × β} : Prod.Lex r s p q ↔ r p.1 q.1 ∨ p.1 = q.1 ∧ s p.2 q.2 := ⟨(λ h => match h with | Lex.left b1 b2 h1 => by simp [h1] | Lex.right a h1 => by simp [h1] ), λ h => match p, q, h with | (a, b), (c, d), Or.inl h => Lex.left _ _ h | (a, b), (c, d), Or.inr ⟨e, h⟩ => by subst e; exact Lex.right _ h ⟩ instance Lex.decidable [DecidableEq α] (r : α → α → Prop) (s : β → β → Prop) [DecidableRel r] [DecidableRel s] : DecidableRel (Prod.Lex r s) := λ p q => decidable_of_decidable_of_iff (by inferInstance) (lex_def r s).symm end Prod open Function lemma Function.injective.prod_map {f : α → γ} {g : β → δ} (hf : injective f) (hg : injective g) : injective (Prod.map f g) := by intros x y h have h1 := (Prod.ext_iff.1 h).1 rw [Prod.map_fst, Prod.map_fst] at h1 have h2 := (Prod.ext_iff.1 h).2 rw [Prod.map_snd, Prod.map_snd] at h2 exact Prod.ext' (hf h1) (hg h2) lemma Function.surjective.prod_map {f : α → γ} {g : β → δ} (hf : surjective f) (hg : surjective g) : surjective (Prod.map f g) := λ p => let ⟨x, hx⟩ := hf p.1 let ⟨y, hy⟩ := hg p.2 ⟨(x, y), Prod.ext' hx hy⟩
9b63c1bc79fd210a9ee77cf87a4cb85e82cd987f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/995.lean
e2d2cefe2a79295f27141ded2047a85bfc291663
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
184
lean
example : ∀(x : Nat){h : x = x}, Nat := by intro x match x with | 0 => _ | n + 1 => _ example (x : Nat) : ∀{h : x = x}, Nat := by match x with | 0 => _ | n + 1 => _
aa6e3af5233577516403bfd5140bb0dcab87f3c6
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/rewriter12.lean
9b7cf535f7e87f237863825c7159d01253b54ad5
[ "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
272
lean
import data.nat open nat algebra constant f : nat → nat example (x y : nat) (H1 : (λ z, z + 0) x = y) : f x = f y := by rewrite [▸* at H1, add_zero at H1, H1] example (x y : nat) (H1 : (λ z, z + 0) x = y) : f x = f y := by rewrite [▸* at H1, add_zero at H1, H1]
01a799a213689421c0d80a2ef9db3ada4e2a20b4
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_ematch10.lean
0fa71972ac842cd5c05f6c494879ba91a48d4b42
[ "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
187
lean
attribute iff [reducible] set_option blast.strategy "ematch" definition lemma1 (p : nat → Prop) (a b c : nat) : p a → a = b → p b := by blast set_option pp.beta true print lemma1
6850ea01ace822f021b8570efa2dd9581293156c
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/order/atoms.lean
005c867c06171444e464c4dbca9f1c06184f36b2
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
4,354
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson. -/ import order.bounded_lattice import order.order_dual /-! # Atoms, Coatoms, and Simple Lattices This module defines atoms, which are minimal non-`⊥` elements in bounded lattices, simple lattices, which are lattices with only two elements, and related ideas. ## Main definitions * `is_atom a` indicates that the only element below `a` is `⊥`. * `is_coatom a` indicates that the only element above `a` is `⊤`. * `is_simple_lattice` indicates that a bounded lattice has only two elements, `⊥` and `⊤`. -/ variable {α : Type*} section atoms section is_atom variable [order_bot α] /-- An atom of an `order_bot` is an element with no other element between it and `⊥`, which is not `⊥`. -/ def is_atom (a : α) : Prop := a ≠ ⊥ ∧ (∀ b, b < a → b = ⊥) lemma eq_bot_or_eq_of_le_atom {a b : α} (ha : is_atom a) (hab : b ≤ a) : b = ⊥ ∨ b = a := or.imp_left (ha.2 b) (lt_or_eq_of_le hab) end is_atom section is_coatom variable [order_top α] /-- A coatom of an `order_top` is an element with no other element between it and `⊤`, which is not `⊤`. -/ def is_coatom (a : α) : Prop := a ≠ ⊤ ∧ (∀ b, a < b → b = ⊤) lemma eq_top_or_eq_of_coatom_le {a b : α} (ha : is_coatom a) (hab : a ≤ b) : b = ⊤ ∨ b = a := or.imp (ha.2 b) eq_comm.2 (lt_or_eq_of_le hab) end is_coatom section pairwise lemma is_atom.inf_eq_bot_of_ne [semilattice_inf_bot α] {a b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : a ⊓ b = ⊥ := or.elim (eq_bot_or_eq_of_le_atom ha inf_le_left) id (λ h1, or.elim (eq_bot_or_eq_of_le_atom hb inf_le_right) id (λ h2, false.rec _ (hab (le_antisymm (inf_eq_left.mp h1) (inf_eq_right.mp h2))))) lemma is_atom.disjoint_of_ne [semilattice_inf_bot α] {a b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : disjoint a b := disjoint_iff.mpr (is_atom.inf_eq_bot_of_ne ha hb hab) lemma is_coatom.sup_eq_top_of_ne [semilattice_sup_top α] {a b : α} (ha : is_coatom a) (hb : is_coatom b) (hab : a ≠ b) : a ⊔ b = ⊤ := or.elim (eq_top_or_eq_of_coatom_le ha le_sup_left) id (λ h1, or.elim (eq_top_or_eq_of_coatom_le hb le_sup_right) id (λ h2, false.rec _ (hab (le_antisymm (sup_eq_right.mp h2) (sup_eq_left.mp h1))))) end pairwise variables [bounded_lattice α] {a : α} lemma is_atom_iff_is_coatom_dual : is_atom a ↔ is_coatom (order_dual.to_dual a) := iff.refl _ lemma is_coatom_iff_is_atom_dual : is_coatom a ↔ is_atom (order_dual.to_dual a) := iff.refl _ end atoms /-- A lattice is simple iff it has only two elements, `⊥` and `⊤`. -/ class is_simple_lattice (α : Type*) [bounded_lattice α] extends nontrivial α : Prop := (eq_bot_or_eq_top : ∀ (a : α), a = ⊥ ∨ a = ⊤) export is_simple_lattice (eq_bot_or_eq_top) theorem is_simple_lattice_iff_is_simple_lattice_order_dual [bounded_lattice α] : is_simple_lattice α ↔ is_simple_lattice (order_dual α) := begin split; intro i; haveI := i, { exact { exists_pair_ne := @exists_pair_ne α _, eq_bot_or_eq_top := λ a, or.symm (eq_bot_or_eq_top ((order_dual.of_dual a)) : _ ∨ _) } }, { exact { exists_pair_ne := @exists_pair_ne (order_dual α) _, eq_bot_or_eq_top := λ a, or.symm (eq_bot_or_eq_top (order_dual.to_dual a)) } } end section is_simple_lattice variables [bounded_lattice α] [is_simple_lattice α] instance : is_simple_lattice (order_dual α) := is_simple_lattice_iff_is_simple_lattice_order_dual.1 (by apply_instance) @[simp] lemma is_atom_top : is_atom (⊤ : α) := ⟨top_ne_bot, λ a ha, or.resolve_right (eq_bot_or_eq_top a) (ne_of_lt ha)⟩ @[simp] lemma is_coatom_bot : is_coatom (⊥ : α) := is_coatom_iff_is_atom_dual.2 is_atom_top end is_simple_lattice theorem is_simple_lattice_iff_is_atom_top [bounded_lattice α] : is_simple_lattice α ↔ is_atom (⊤ : α) := ⟨λ h, @is_atom_top _ _ h, λ h, { exists_pair_ne := ⟨⊤, ⊥, h.1⟩, eq_bot_or_eq_top := λ a, ((eq_or_lt_of_le (@le_top _ _ a)).imp_right (h.2 a)).symm }⟩ theorem is_simple_lattice_iff_is_coatom_bot [bounded_lattice α] : is_simple_lattice α ↔ is_coatom (⊥ : α) := iff.trans is_simple_lattice_iff_is_simple_lattice_order_dual is_simple_lattice_iff_is_atom_top
268352589f5d075d74c4b3f887a0cb710126e257
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/tactic12.lean
faf9ef85d6e9ee4afdaee937cc8e84736508735f
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
285
lean
import standard using tactic theorem tst (a b : Prop) (H : ¬ a ∨ ¬ b) (Hb : b) : ¬ a ∧ b := by apply and_intro; apply not_intro; exact (assume Ha, or_elim H (assume Hna, absurd Ha Hna) (assume Hnb, absurd Hb Hnb)); assumption
108f5cdc447f58d87493b7f12798d678bf57f0a9
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/solutions/thursday/afternoon/category_theory/exercise3.lean
a28a8f0dee1332b817199fac759160ea631a48f4
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
1,977
lean
import category_theory.limits.shapes.zero import category_theory.full_subcategory import algebra.homology.chain_complex import data.int.basic /-! Let's give a quirky definition of a cochain complex in a category `C` with zero morphisms, as a functor `F` from `(ℤ, ≤)` to `C`, so that `∀ i, F.map (by tidy : i ≤ i+2) = 0`. Let's think of this as a full subcategory of all functors `(ℤ, ≤) ⥤ C`, and realise that natural transformations are exactly chain maps. Finally let's construct an equivalence of categories with the usual definition of chain cocomplex! -/ open category_theory open category_theory.limits -- Anytime we have a `[preorder α]`, we automatically get a `[category.{v} α]` instance, -- in which the morphisms `X ⟶ Y` are defined to be `ulift (plift X ≤ Y)`. -- (We need those annoying `ulift` and `plift` because `X ≤ Y` is a `Prop`, -- and the morphisms spaces of a category need to be in `Type v` for some `v`.) namespace exercise3 variables (C : Type) [category.{0} C] [has_zero_morphisms C] @[derive category] def complex : Type := { F : ℤ ⥤ C // ∀ i : ℤ, F.map (by tidy : i ⟶ i+2) = 0 } def functor : complex C ⥤ cochain_complex C := { obj := λ P, { X := P.1.obj, d := λ i, P.1.map (by tidy : i ⟶ i+1), d_squared' := sorry, }, map := λ P Q α, { f := λ i, α.app i, comm' := sorry, } } def long_map (P : cochain_complex C) (i j : ℤ) : P.X i ⟶ P.X j := if h₀ : j = i then eq_to_hom (by rw h₀) else if h₁ : j = i+1 then P.d i ≫ eq_to_hom (by {simp [h₁]}) else 0 def inverse : cochain_complex C ⥤ complex C := { obj := λ P, { val := { obj := λ i, P.X i, map := λ i j p, long_map C P i j, map_id' := sorry, map_comp' := sorry }, property := sorry, }, map := λ P Q f, { app := λ i, f.f i, naturality' := sorry, }, map_id' := sorry, map_comp' := sorry, } def exercise3 : complex C ≌ cochain_complex C := sorry end exercise3
f25833705d5ebc848b3cd33605f7af0b3432a1bc
ff5230333a701471f46c57e8c115a073ebaaa448
/library/data/stream.lean
783a243a60b53a0b8ef7bf3a134aaddf66f3d144
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
21,668
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 -/ open nat function option universes u v w def stream (α : Type u) := nat → α namespace stream variables {α : Type u} {β : Type v} {δ : Type w} def cons (a : α) (s : stream α) : stream α := λ i, match i with | 0 := a | succ n := s n end notation h :: t := cons h t @[reducible] def head (s : stream α) : α := s 0 def tail (s : stream α) : stream α := λ i, s (i+1) def drop (n : nat) (s : stream α) : stream α := λ i, s (i+n) @[reducible] def nth (n : nat) (s : stream α) : α := s n protected theorem eta (s : stream α) : head s :: tail s = s := funext (λ i, begin cases i; refl end) theorem nth_zero_cons (a : α) (s : stream α) : nth 0 (a :: s) = a := rfl theorem head_cons (a : α) (s : stream α) : head (a :: s) = a := rfl theorem tail_cons (a : α) (s : stream α) : tail (a :: s) = s := rfl theorem tail_drop (n : nat) (s : stream α) : tail (drop n s) = drop n (tail s) := funext (λ i, begin unfold tail drop, simp end) theorem nth_drop (n m : nat) (s : stream α) : nth n (drop m s) = nth (n+m) s := rfl theorem tail_eq_drop (s : stream α) : tail s = drop 1 s := rfl theorem drop_drop (n m : nat) (s : stream α) : drop n (drop m s) = drop (n+m) s := funext (λ i, begin unfold drop, rw add_assoc end) theorem nth_succ (n : nat) (s : stream α) : nth (succ n) s = nth n (tail s) := rfl theorem drop_succ (n : nat) (s : stream α) : drop (succ n) s = drop n (tail s) := rfl protected theorem ext {s₁ s₂ : stream α} : (∀ n, nth n s₁ = nth n s₂) → s₁ = s₂ := assume h, funext h def all (p : α → Prop) (s : stream α) := ∀ n, p (nth n s) def any (p : α → Prop) (s : stream α) := ∃ n, p (nth n s) theorem all_def (p : α → Prop) (s : stream α) : all p s = ∀ n, p (nth n s) := rfl theorem any_def (p : α → Prop) (s : stream α) : any p s = ∃ n, p (nth n s) := rfl protected def mem (a : α) (s : stream α) := any (λ b, a = b) s instance : has_mem α (stream α) := ⟨stream.mem⟩ theorem mem_cons (a : α) (s : stream α) : a ∈ (a::s) := exists.intro 0 rfl theorem mem_cons_of_mem {a : α} {s : stream α} (b : α) : a ∈ s → a ∈ b :: s := assume ⟨n, h⟩, exists.intro (succ n) (by rw [nth_succ, tail_cons, h]) theorem eq_or_mem_of_mem_cons {a b : α} {s : stream α} : a ∈ b::s → a = b ∨ a ∈ s := assume ⟨n, h⟩, begin cases n with n', {left, exact h}, {right, rw [nth_succ, tail_cons] at h, exact ⟨n', h⟩} end theorem mem_of_nth_eq {n : nat} {s : stream α} {a : α} : a = nth n s → a ∈ s := assume h, exists.intro n h section map variable (f : α → β) def map (s : stream α) : stream β := λ n, f (nth n s) theorem drop_map (n : nat) (s : stream α) : drop n (map f s) = map f (drop n s) := stream.ext (λ i, rfl) theorem nth_map (n : nat) (s : stream α) : nth n (map f s) = f (nth n s) := rfl theorem tail_map (s : stream α) : tail (map f s) = map f (tail s) := begin rw tail_eq_drop, refl end theorem head_map (s : stream α) : head (map f s) = f (head s) := rfl theorem map_eq (s : stream α) : map f s = f (head s) :: map f (tail s) := by rw [← stream.eta (map f s), tail_map, head_map] theorem map_cons (a : α) (s : stream α) : map f (a :: s) = f a :: map f s := begin rw [← stream.eta (map f (a :: s)), map_eq], refl end theorem map_id (s : stream α) : map id s = s := rfl theorem map_map (g : β → δ) (f : α → β) (s : stream α) : map g (map f s) = map (g ∘ f) s := rfl theorem map_tail (s : stream α) : map f (tail s) = tail (map f s) := rfl theorem mem_map {a : α} {s : stream α} : a ∈ s → f a ∈ map f s := assume ⟨n, h⟩, exists.intro n (by rw [nth_map, h]) theorem exists_of_mem_map {f} {b : β} {s : stream α} : b ∈ map f s → ∃ a, a ∈ s ∧ f a = b := assume ⟨n, h⟩, ⟨nth n s, ⟨n, rfl⟩, h.symm⟩ end map section zip variable (f : α → β → δ) def zip (s₁ : stream α) (s₂ : stream β) : stream δ := λ n, f (nth n s₁) (nth n s₂) theorem drop_zip (n : nat) (s₁ : stream α) (s₂ : stream β) : drop n (zip f s₁ s₂) = zip f (drop n s₁) (drop n s₂) := stream.ext (λ i, rfl) theorem nth_zip (n : nat) (s₁ : stream α) (s₂ : stream β) : nth n (zip f s₁ s₂) = f (nth n s₁) (nth n s₂) := rfl theorem head_zip (s₁ : stream α) (s₂ : stream β) : head (zip f s₁ s₂) = f (head s₁) (head s₂) := rfl theorem tail_zip (s₁ : stream α) (s₂ : stream β) : tail (zip f s₁ s₂) = zip f (tail s₁) (tail s₂) := rfl theorem zip_eq (s₁ : stream α) (s₂ : stream β) : zip f s₁ s₂ = f (head s₁) (head s₂) :: zip f (tail s₁) (tail s₂) := begin rw [← stream.eta (zip f s₁ s₂)], refl end end zip def const (a : α) : stream α := λ n, a theorem mem_const (a : α) : a ∈ const a := exists.intro 0 rfl theorem const_eq (a : α) : const a = a :: const a := begin apply stream.ext, intro n, cases n; refl end theorem tail_const (a : α) : tail (const a) = const a := suffices tail (a :: const a) = const a, by rwa [← const_eq] at this, rfl theorem map_const (f : α → β) (a : α) : map f (const a) = const (f a) := rfl theorem nth_const (n : nat) (a : α) : nth n (const a) = a := rfl theorem drop_const (n : nat) (a : α) : drop n (const a) = const a := stream.ext (λ i, rfl) def iterate (f : α → α) (a : α) : stream α := λ n, nat.rec_on n a (λ n r, f r) theorem head_iterate (f : α → α) (a : α) : head (iterate f a) = a := rfl theorem tail_iterate (f : α → α) (a : α) : tail (iterate f a) = iterate f (f a) := begin funext n, induction n with n' ih, {refl}, {unfold tail iterate, unfold tail iterate at ih, rw add_one at ih, dsimp at ih, rw add_one, dsimp, rw ih} end theorem iterate_eq (f : α → α) (a : α) : iterate f a = a :: iterate f (f a) := begin rw [← stream.eta (iterate f a)], rw tail_iterate, refl end theorem nth_zero_iterate (f : α → α) (a : α) : nth 0 (iterate f a) = a := rfl theorem nth_succ_iterate (n : nat) (f : α → α) (a : α) : nth (succ n) (iterate f a) = nth n (iterate f (f a)) := by rw [nth_succ, tail_iterate] section bisim variable (R : stream α → stream α → Prop) local infix ~ := R def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → head s₁ = head s₂ ∧ tail s₁ ~ tail s₂ theorem nth_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂} n, s₁ ~ s₂ → nth n s₁ = nth n s₂ ∧ drop (n+1) s₁ ~ drop (n+1) s₂ | s₁ s₂ 0 h := bisim h | s₁ s₂ (n+1) h := match bisim h with | ⟨h₁, trel⟩ := nth_of_bisim n trel end -- If two streams are bisimilar, then they are equal theorem eq_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂}, s₁ ~ s₂ → s₁ = s₂ := λ s₁ s₂ r, stream.ext (λ n, and.elim_left (nth_of_bisim R bisim n r)) end bisim theorem bisim_simple (s₁ s₂ : stream α) : head s₁ = head s₂ → s₁ = tail s₁ → s₂ = tail s₂ → s₁ = s₂ := assume hh ht₁ ht₂, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ s₁ = tail s₁ ∧ s₂ = tail s₂) (λ s₁ s₂ ⟨h₁, h₂, h₃⟩, begin constructor, exact h₁, rw [← h₂, ← h₃], repeat { constructor }; assumption end) (and.intro hh (and.intro ht₁ ht₂)) theorem coinduction {s₁ s₂ : stream α} : head s₁ = head s₂ → (∀ (β : Type u) (fr : stream α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂ := assume hh ht, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ ∀ (β : Type u) (fr : stream α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) (λ s₁ s₂ h, have h₁ : head s₁ = head s₂, from and.elim_left h, have h₂ : head (tail s₁) = head (tail s₂), from and.elim_right h α (@head α) h₁, have h₃ : ∀ (β : Type u) (fr : stream α → β), fr (tail s₁) = fr (tail s₂) → fr (tail (tail s₁)) = fr (tail (tail s₂)), from λ β fr, and.elim_right h β (λ s, fr (tail s)), and.intro h₁ (and.intro h₂ h₃)) (and.intro hh ht) theorem iterate_id (a : α) : iterate id a = const a := coinduction rfl (λ β fr ch, begin rw [tail_iterate, tail_const], exact ch end) local attribute [reducible] stream theorem map_iterate (f : α → α) (a : α) : iterate f (f a) = map f (iterate f a) := begin funext n, induction n with n' ih, {refl}, { unfold map iterate nth, dsimp, unfold map iterate nth at ih, dsimp at ih, rw ih } end section corec def corec (f : α → β) (g : α → α) : α → stream β := λ a, map f (iterate g a) def corec_on (a : α) (f : α → β) (g : α → α) : stream β := corec f g a theorem corec_def (f : α → β) (g : α → α) (a : α) : corec f g a = map f (iterate g a) := rfl theorem corec_eq (f : α → β) (g : α → α) (a : α) : corec f g a = f a :: corec f g (g a) := begin rw [corec_def, map_eq, head_iterate, tail_iterate], refl end theorem corec_id_id_eq_const (a : α) : corec id id a = const a := by rw [corec_def, map_id, iterate_id] theorem corec_id_f_eq_iterate (f : α → α) (a : α) : corec id f a = iterate f a := rfl end corec section corec' def corec' (f : α → β × α) : α → stream β := corec (prod.fst ∘ f) (prod.snd ∘ f) theorem corec'_eq (f : α → β × α) (a : α) : corec' f a = (f a).1 :: corec' f (f a).2 := corec_eq _ _ _ end corec' -- corec is also known as unfold def unfolds (g : α → β) (f : α → α) (a : α) : stream β := corec g f a theorem unfolds_eq (g : α → β) (f : α → α) (a : α) : unfolds g f a = g a :: unfolds g f (f a) := begin unfold unfolds, rw [corec_eq] end theorem nth_unfolds_head_tail : ∀ (n : nat) (s : stream α), nth n (unfolds head tail s) = nth n s := begin intro n, induction n with n' ih, {intro s, refl}, {intro s, rw [nth_succ, nth_succ, unfolds_eq, tail_cons, ih]} end theorem unfolds_head_eq : ∀ (s : stream α), unfolds head tail s = s := λ s, stream.ext (λ n, nth_unfolds_head_tail n s) def interleave (s₁ s₂ : stream α) : stream α := corec_on (s₁, s₂) (λ ⟨s₁, s₂⟩, head s₁) (λ ⟨s₁, s₂⟩, (s₂, tail s₁)) infix `⋈`:65 := interleave theorem interleave_eq (s₁ s₂ : stream α) : s₁ ⋈ s₂ = head s₁ :: head s₂ :: (tail s₁ ⋈ tail s₂) := begin unfold interleave corec_on, rw corec_eq, dsimp, rw corec_eq, refl end theorem tail_interleave (s₁ s₂ : stream α) : tail (s₁ ⋈ s₂) = s₂ ⋈ (tail s₁) := begin unfold interleave corec_on, rw corec_eq, refl end theorem interleave_tail_tail (s₁ s₂ : stream α) : tail s₁ ⋈ tail s₂ = tail (tail (s₁ ⋈ s₂)) := begin rw [interleave_eq s₁ s₂], refl end theorem nth_interleave_left : ∀ (n : nat) (s₁ s₂ : stream α), nth (2*n) (s₁ ⋈ s₂) = nth n s₁ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n))) (s₁ ⋈ s₂) = nth (succ n) s₁, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_left], refl end theorem nth_interleave_right : ∀ (n : nat) (s₁ s₂ : stream α), nth (2*n+1) (s₁ ⋈ s₂) = nth n s₂ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n+1))) (s₁ ⋈ s₂) = nth (succ n) s₂, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_right], refl end theorem mem_interleave_left {a : α} {s₁ : stream α} (s₂ : stream α) : a ∈ s₁ → a ∈ s₁ ⋈ s₂ := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_interleave_left]) theorem mem_interleave_right {a : α} {s₁ : stream α} (s₂ : stream α) : a ∈ s₂ → a ∈ s₁ ⋈ s₂ := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_interleave_right]) def even (s : stream α) : stream α := corec (λ s, head s) (λ s, tail (tail s)) s def odd (s : stream α) : stream α := even (tail s) theorem odd_eq (s : stream α) : odd s = even (tail s) := rfl theorem head_even (s : stream α) : head (even s) = head s := rfl theorem tail_even (s : stream α) : tail (even s) = even (tail (tail s)) := begin unfold even, rw corec_eq, refl end theorem even_cons_cons (a₁ a₂ : α) (s : stream α) : even (a₁ :: a₂ :: s) = a₁ :: even s := begin unfold even, rw corec_eq, refl end theorem even_tail (s : stream α) : even (tail s) = odd s := rfl theorem even_interleave (s₁ s₂ : stream α) : even (s₁ ⋈ s₂) = s₁ := eq_of_bisim (λ s₁' s₁, ∃ s₂, s₁' = even (s₁ ⋈ s₂)) (λ s₁' s₁ ⟨s₂, h₁⟩, begin rw h₁, constructor, {refl}, {exact ⟨tail s₂, by rw [interleave_eq, even_cons_cons, tail_cons]⟩} end) (exists.intro s₂ rfl) theorem interleave_even_odd (s₁ : stream α) : even s₁ ⋈ odd s₁ = s₁ := eq_of_bisim (λ s' s, s' = even s ⋈ odd s) (λ s' s (h : s' = even s ⋈ odd s), begin rw h, constructor, {refl}, {simp [odd_eq, odd_eq, tail_interleave, tail_even]} end) rfl theorem nth_even : ∀ (n : nat) (s : stream α), nth n (even s) = nth (2*n) s | 0 s := rfl | (succ n) s := begin change nth (succ n) (even s) = nth (succ (succ (2 * n))) s, rw [nth_succ, nth_succ, tail_even, nth_even], refl end theorem nth_odd : ∀ (n : nat) (s : stream α), nth n (odd s) = nth (2*n + 1) s := λ n s, begin rw [odd_eq, nth_even], refl end theorem mem_of_mem_even (a : α) (s : stream α) : a ∈ even s → a ∈ s := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_even]) theorem mem_of_mem_odd (a : α) (s : stream α) : a ∈ odd s → a ∈ s := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_odd]) def append_stream : list α → stream α → stream α | [] s := s | (list.cons a l) s := a :: append_stream l s theorem nil_append_stream (s : stream α) : append_stream [] s = s := rfl theorem cons_append_stream (a : α) (l : list α) (s : stream α) : append_stream (a::l) s = a :: append_stream l s := rfl infix `++ₛ`:65 := append_stream theorem append_append_stream : ∀ (l₁ l₂ : list α) (s : stream α), (l₁ ++ l₂) ++ₛ s = l₁ ++ₛ (l₂ ++ₛ s) | [] l₂ s := rfl | (list.cons a l₁) l₂ s := by rw [list.cons_append, cons_append_stream, cons_append_stream, append_append_stream] theorem map_append_stream (f : α → β) : ∀ (l : list α) (s : stream α), map f (l ++ₛ s) = list.map f l ++ₛ map f s | [] s := rfl | (list.cons a l) s := by rw [cons_append_stream, list.map_cons, map_cons, cons_append_stream, map_append_stream] theorem drop_append_stream : ∀ (l : list α) (s : stream α), drop l.length (l ++ₛ s) = s | [] s := by refl | (list.cons a l) s := by rw [list.length_cons, add_one, drop_succ, cons_append_stream, tail_cons, drop_append_stream] theorem append_stream_head_tail (s : stream α) : [head s] ++ₛ tail s = s := by rw [cons_append_stream, nil_append_stream, stream.eta] theorem mem_append_stream_right : ∀ {a : α} (l : list α) {s : stream α}, a ∈ s → a ∈ l ++ₛ s | a [] s h := h | a (list.cons b l) s h := have ih : a ∈ l ++ₛ s, from mem_append_stream_right l h, mem_cons_of_mem _ ih theorem mem_append_stream_left : ∀ {a : α} {l : list α} (s : stream α), a ∈ l → a ∈ l ++ₛ s | a [] s h := absurd h (list.not_mem_nil _) | a (list.cons b l) s h := or.elim (list.eq_or_mem_of_mem_cons h) (λ (aeqb : a = b), exists.intro 0 aeqb) (λ (ainl : a ∈ l), mem_cons_of_mem b (mem_append_stream_left s ainl)) def approx : nat → stream α → list α | 0 s := [] | (n+1) s := list.cons (head s) (approx n (tail s)) theorem approx_zero (s : stream α) : approx 0 s = [] := rfl theorem approx_succ (n : nat) (s : stream α) : approx (succ n) s = head s :: approx n (tail s) := rfl theorem nth_approx : ∀ (n : nat) (s : stream α), list.nth (approx (succ n) s) n = some (nth n s) | 0 s := rfl | (n+1) s := begin rw [approx_succ, add_one, list.nth, nth_approx], refl end theorem append_approx_drop : ∀ (n : nat) (s : stream α), append_stream (approx n s) (drop n s) = s := begin intro n, induction n with n' ih, {intro s, refl}, {intro s, rw [approx_succ, drop_succ, cons_append_stream, ih (tail s), stream.eta]} end -- Take theorem reduces a proof of equality of infinite streams to an -- induction over all their finite approximations. theorem take_theorem (s₁ s₂ : stream α) : (∀ (n : nat), approx n s₁ = approx n s₂) → s₁ = s₂ := begin intro h, apply stream.ext, intro n, induction n with n ih, { have aux := h 1, simp [approx] at aux, exact aux }, { have h₁ : some (nth (succ n) s₁) = some (nth (succ n) s₂), { rw [← nth_approx, ← nth_approx, h (succ (succ n))] }, injection h₁ } end -- auxiliary def for cycle corecursive def private def cycle_f : α × list α × α × list α → α | (v, _, _, _) := v -- auxiliary def for cycle corecursive def private def cycle_g : α × list α × α × list α → α × list α × α × list α | (v₁, [], v₀, l₀) := (v₀, l₀, v₀, l₀) | (v₁, list.cons v₂ l₂, v₀, l₀) := (v₂, l₂, v₀, l₀) private lemma cycle_g_cons (a : α) (a₁ : α) (l₁ : list α) (a₀ : α) (l₀ : list α) : cycle_g (a, a₁::l₁, a₀, l₀) = (a₁, l₁, a₀, l₀) := rfl def cycle : Π (l : list α), l ≠ [] → stream α | [] h := absurd rfl h | (list.cons a l) h := corec cycle_f cycle_g (a, l, a, l) theorem cycle_eq : ∀ (l : list α) (h : l ≠ []), cycle l h = l ++ₛ cycle l h | [] h := absurd rfl h | (list.cons a l) h := have gen : ∀ l' a', corec cycle_f cycle_g (a', l', a, l) = (a' :: l') ++ₛ corec cycle_f cycle_g (a, l, a, l), begin intro l', induction l' with a₁ l₁ ih, {intros, rw [corec_eq], refl}, {intros, rw [corec_eq, cycle_g_cons, ih a₁], refl} end, gen l a theorem mem_cycle {a : α} {l : list α} : ∀ (h : l ≠ []), a ∈ l → a ∈ cycle l h := assume h ainl, begin rw [cycle_eq], exact mem_append_stream_left _ ainl end theorem cycle_singleton (a : α) (h : [a] ≠ []) : cycle [a] h = const a := coinduction rfl (λ β fr ch, by rwa [cycle_eq, const_eq]) def tails (s : stream α) : stream (stream α) := corec id tail (tail s) theorem tails_eq (s : stream α) : tails s = tail s :: tails (tail s) := by unfold tails; rw [corec_eq]; refl theorem nth_tails : ∀ (n : nat) (s : stream α), nth n (tails s) = drop n (tail s) := begin intro n, induction n with n' ih, {intros, refl}, {intro s, rw [nth_succ, drop_succ, tails_eq, tail_cons, ih]} end theorem tails_eq_iterate (s : stream α) : tails s = iterate tail (tail s) := rfl def inits_core (l : list α) (s : stream α) : stream (list α) := corec_on (l, s) (λ ⟨a, b⟩, a) (λ p, match p with (l', s') := (l' ++ [head s'], tail s') end) def inits (s : stream α) : stream (list α) := inits_core [head s] (tail s) theorem inits_core_eq (l : list α) (s : stream α) : inits_core l s = l :: inits_core (l ++ [head s]) (tail s) := begin unfold inits_core corec_on, rw [corec_eq], refl end theorem tail_inits (s : stream α) : tail (inits s) = inits_core [head s, head (tail s)] (tail (tail s)) := begin unfold inits, rw inits_core_eq, refl end theorem inits_tail (s : stream α) : inits (tail s) = inits_core [head (tail s)] (tail (tail s)) := rfl theorem cons_nth_inits_core : ∀ (a : α) (n : nat) (l : list α) (s : stream α), a :: nth n (inits_core l s) = nth n (inits_core (a::l) s) := begin intros a n, induction n with n' ih, {intros, refl}, {intros l s, rw [nth_succ, inits_core_eq, tail_cons, ih, inits_core_eq (a::l) s], refl } end theorem nth_inits : ∀ (n : nat) (s : stream α), nth n (inits s) = approx (succ n) s := begin intro n, induction n with n' ih, {intros, refl}, {intros, rw [nth_succ, approx_succ, ← ih, tail_inits, inits_tail, cons_nth_inits_core]} end theorem inits_eq (s : stream α) : inits s = [head s] :: map (list.cons (head s)) (inits (tail s)) := begin apply stream.ext, intro n, cases n, {refl}, {rw [nth_inits, nth_succ, tail_cons, nth_map, nth_inits], refl} end theorem zip_inits_tails (s : stream α) : zip append_stream (inits s) (tails s) = const s := begin apply stream.ext, intro n, rw [nth_zip, nth_inits, nth_tails, nth_const, approx_succ, cons_append_stream, append_approx_drop, stream.eta] end def pure (a : α) : stream α := const a def apply (f : stream (α → β)) (s : stream α) : stream β := λ n, (nth n f) (nth n s) infix `⊛`:75 := apply -- input as \o* theorem identity (s : stream α) : pure id ⊛ s = s := rfl theorem composition (g : stream (β → δ)) (f : stream (α → β)) (s : stream α) : pure comp ⊛ g ⊛ f ⊛ s = g ⊛ (f ⊛ s) := rfl theorem homomorphism (f : α → β) (a : α) : pure f ⊛ pure a = pure (f a) := rfl theorem interchange (fs : stream (α → β)) (a : α) : fs ⊛ pure a = pure (λ f : α → β, f a) ⊛ fs := rfl theorem map_eq_apply (f : α → β) (s : stream α) : map f s = pure f ⊛ s := rfl def nats : stream nat := λ n, n theorem nth_nats (n : nat) : nth n nats = n := rfl theorem nats_eq : nats = 0 :: map succ nats := begin apply stream.ext, intro n, cases n, refl, rw [nth_succ], refl end end stream
ed8ab9b91231a3392b261a65e1a4a2c3aeb270c2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/counterexamples/phillips.lean
9ef4b12c2300fce190d134eefc3c2ddae0d4f99f
[ "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
29,252
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.hahn_banach.extension import measure_theory.integral.set_integral import measure_theory.measure.lebesgue.basic import topology.continuous_function.bounded /-! # A counterexample on Pettis integrability > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. There are several theories of integration for functions taking values in Banach spaces. Bochner integration, requiring approximation by simple functions, is the analogue of the one-dimensional theory. It is very well behaved, but only works for functions with second-countable range. For functions `f` taking values in a larger Banach space `B`, one can define the Dunford integral as follows. Assume that, for all continuous linear functional `φ`, the function `φ ∘ f` is measurable (we say that `f` is weakly measurable, or scalarly measurable) and integrable. Then `φ ↦ ∫ φ ∘ f` is continuous (by the closed graph theorem), and therefore defines an element of the bidual `B**`. This is the Dunford integral of `f`. This Dunford integral is not usable in practice as it does not belong to the right space. Let us say that a function is Pettis integrable if its Dunford integral belongs to the canonical image of `B` in `B**`. In this case, we define the Pettis integral as the Dunford integral inside `B`. This integral is very general, but not really usable to do analysis. This file illustrates this, by giving an example of a function with nice properties but which is *not* Pettis-integrable. This function: - is defined from `[0, 1]` to a complete Banach space; - is weakly measurable; - has norm everywhere bounded by `1` (in particular, its norm is integrable); - and yet it is not Pettis-integrable with respect to Lebesgue measure. This construction is due to [Ralph S. Phillips, *Integration in a convex linear topological space*][phillips1940], in Example 10.8. It requires the continuum hypothesis. The example is the following. Under the continuum hypothesis, one can find a subset of `ℝ²` which, along each vertical line, only misses a countable set of points, while it is countable along each horizontal line. This is due to Sierpinski, and formalized in `sierpinski_pathological_family`. (In fact, Sierpinski proves that the existence of such a set is equivalent to the continuum hypothesis). Let `B` be the set of all bounded functions on `ℝ` (we are really talking about everywhere defined functions here). Define `f : ℝ → B` by taking `f x` to be the characteristic function of the vertical slice at position `x` of Sierpinski's set. It is our counterexample. To show that it is weakly measurable, we should consider `φ ∘ f` where `φ` is an arbitrary continuous linear form on `B`. There is no reasonable classification of such linear forms (they can be very wild). But if one restricts such a linear form to characteristic functions, one gets a finitely additive signed "measure". Such a "measure" can be decomposed into a discrete part (supported on a countable set) and a continuous part (giving zero mass to countable sets). For all but countably many points, `f x` will not intersect the discrete support of `φ` thanks to the definition of the Sierpinski set. This implies that `φ ∘ f` is constant outside of a countable set, and equal to the total mass of the continuous part of `φ` there. In particular, it is measurable (and its integral is the total mass of the continuous part of `φ`). Assume that `f` has a Pettis integral `g`. For all continuous linear form `φ`, then `φ g` should be the total mass of the continuous part of `φ`. Taking for `φ` the evaluation at the point `x` (which has no continuous part), one gets `g x = 0`. Take then for `φ` the Lebesgue integral on `[0, 1]` (or rather an arbitrary extension of Lebesgue integration to all bounded functions, thanks to Hahn-Banach). Then `φ g` should be the total mass of the continuous part of `φ`, which is `1`. This contradicts the fact that `g = 0`, and concludes the proof that `f` has no Pettis integral. ## Implementation notes The space of all bounded functions is defined as the space of all bounded continuous functions on a discrete copy of the original type, as mathlib only contains the space of all bounded continuous functions (which is the useful one). -/ namespace counterexample universe u variables {α : Type u} open set bounded_continuous_function measure_theory open cardinal (aleph) open_locale cardinal bounded_continuous_function noncomputable theory /-- A copy of a type, endowed with the discrete topology -/ def discrete_copy (α : Type u) : Type u := α instance : topological_space (discrete_copy α) := ⊥ instance : discrete_topology (discrete_copy α) := ⟨rfl⟩ instance [inhabited α] : inhabited (discrete_copy α) := ⟨show α, from default⟩ namespace phillips_1940 /-! ### Extending the integral Thanks to Hahn-Banach, one can define a (non-canonical) continuous linear functional on the space of all bounded functions, coinciding with the integral on the integrable ones. -/ /-- The subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, used to apply Hahn-Banach theorem to construct an extension of the integral to all bounded functions. -/ def bounded_integrable_functions [measurable_space α] (μ : measure α) : subspace ℝ (discrete_copy α →ᵇ ℝ) := { carrier := {f | integrable f μ}, zero_mem' := integrable_zero _ _ _, add_mem' := λ f g hf hg, integrable.add hf hg, smul_mem' := λ c f hf, integrable.smul c hf } /-- The integral, as a continuous linear map on the subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, that we will extend through Hahn-Banach. -/ def bounded_integrable_functions_integral_clm [measurable_space α] (μ : measure α) [is_finite_measure μ] : bounded_integrable_functions μ →L[ℝ] ℝ := linear_map.mk_continuous { to_fun := λ f, ∫ x, f x ∂μ, map_add' := λ f g, integral_add f.2 g.2, map_smul' := λ c f, integral_smul _ _ } (μ univ).to_real begin assume f, rw mul_comm, apply norm_integral_le_of_norm_le_const, apply filter.eventually_of_forall, assume x, exact bounded_continuous_function.norm_coe_le_norm f x, end /-- Given a measure, there exists a continuous linear form on the space of all bounded functions (not necessarily measurable) that coincides with the integral on bounded measurable functions. -/ lemma exists_linear_extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : ∃ φ : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ, ∀ (f : discrete_copy α →ᵇ ℝ), integrable f μ → φ f = ∫ x, f x ∂μ := begin rcases exists_extension_norm_eq _ (bounded_integrable_functions_integral_clm μ) with ⟨φ, hφ⟩, exact ⟨φ, λ f hf, hφ.1 ⟨f, hf⟩⟩, end /-- An arbitrary extension of the integral to all bounded functions, as a continuous linear map. It is not at all canonical, and constructed using Hahn-Banach. -/ def _root_.measure_theory.measure.extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ := (exists_linear_extension_to_bounded_functions μ).some lemma extension_to_bounded_functions_apply [measurable_space α] (μ : measure α) [is_finite_measure μ] (f : discrete_copy α →ᵇ ℝ) (hf : integrable f μ) : μ.extension_to_bounded_functions f = ∫ x, f x ∂μ := (exists_linear_extension_to_bounded_functions μ).some_spec f hf /-! ### Additive measures on the space of all sets We define bounded finitely additive signed measures on the space of all subsets of a type `α`, and show that such an object can be split into a discrete part and a continuous part. -/ /-- A bounded signed finitely additive measure defined on *all* subsets of a type. -/ structure bounded_additive_measure (α : Type u) := (to_fun : set α → ℝ) (additive' : ∀ s t, disjoint s t → to_fun (s ∪ t) = to_fun s + to_fun t) (exists_bound : ∃ (C : ℝ), ∀ s, |to_fun s| ≤ C) instance : inhabited (bounded_additive_measure α) := ⟨{ to_fun := λ s, 0, additive' := λ s t hst, by simp, exists_bound := ⟨0, λ s, by simp⟩ }⟩ instance : has_coe_to_fun (bounded_additive_measure α) (λ _, set α → ℝ) := ⟨λ f, f.to_fun⟩ namespace bounded_additive_measure /-- A constant bounding the mass of any set for `f`. -/ def C (f : bounded_additive_measure α) := f.exists_bound.some lemma additive (f : bounded_additive_measure α) (s t : set α) (h : disjoint s t) : f (s ∪ t) = f s + f t := f.additive' s t h lemma abs_le_bound (f : bounded_additive_measure α) (s : set α) : |f s| ≤ f.C := f.exists_bound.some_spec s lemma le_bound (f : bounded_additive_measure α) (s : set α) : f s ≤ f.C := le_trans (le_abs_self _) (f.abs_le_bound s) @[simp] lemma empty (f : bounded_additive_measure α) : f ∅ = 0 := begin have : (∅ : set α) = ∅ ∪ ∅, by simp only [empty_union], apply_fun f at this, rwa [f.additive _ _ (empty_disjoint _), self_eq_add_left] at this, end instance : has_neg (bounded_additive_measure α) := ⟨λ f, { to_fun := λ s, - f s, additive' := λ s t hst, by simp only [f.additive s t hst, add_comm, neg_add_rev], exists_bound := ⟨f.C, λ s, by simp [f.abs_le_bound]⟩ }⟩ @[simp] lemma neg_apply (f : bounded_additive_measure α) (s : set α) : (-f) s = - (f s) := rfl /-- Restricting a bounded additive measure to a subset still gives a bounded additive measure. -/ def restrict (f : bounded_additive_measure α) (t : set α) : bounded_additive_measure α := { to_fun := λ s, f (t ∩ s), additive' := λ s s' h, begin rw [← f.additive (t ∩ s) (t ∩ s'), inter_union_distrib_left], exact h.mono (inter_subset_right _ _) (inter_subset_right _ _), end, exists_bound := ⟨f.C, λ s, f.abs_le_bound _⟩ } @[simp] lemma restrict_apply (f : bounded_additive_measure α) (s t : set α) : f.restrict s t = f (s ∩ t) := rfl /-- There is a maximal countable set of positive measure, in the sense that any countable set not intersecting it has nonpositive measure. Auxiliary lemma to prove `exists_discrete_support`. -/ lemma exists_discrete_support_nonpos (f : bounded_additive_measure α) : ∃ (s : set α), s.countable ∧ (∀ t : set α, t.countable → f (t \ s) ≤ 0) := begin /- The idea of the proof is to construct the desired set inductively, adding at each step a countable set with close to maximal measure among those points that have not already been chosen. Doing this countably many steps will be enough. Indeed, otherwise, a remaining set would have positive measure `ε`. This means that at each step the set we have added also had a large measure, say at least `ε / 2`. After `n` steps, the set we have constructed has therefore measure at least `n * ε / 2`. This is a contradiction since the measures have to remain uniformly bounded. We argue from the start by contradiction, as this means that our inductive construction will never be stuck, so we won't have to consider this case separately. In this proof, we use explicit coercions `↑s` for `s : A` as otherwise the system tries to find a `has_coe_to_fun` instance on `↥A`, which is too costly. -/ by_contra' h, -- We will formulate things in terms of the type of countable subsets of `α`, as this is more -- convenient to formalize the inductive construction. let A : set (set α) := {t | t.countable}, let empty : A := ⟨∅, countable_empty⟩, haveI : nonempty A := ⟨empty⟩, -- given a countable set `s`, one can find a set `t` in its complement with measure close to -- maximal. have : ∀ (s : A), ∃ (t : A), (∀ (u : A), f (↑u \ ↑s) ≤ 2 * f (↑t \ ↑s)), { assume s, have B : bdd_above (range (λ (u : A), f (↑u \ ↑s))), { refine ⟨f.C, λ x hx, _⟩, rcases hx with ⟨u, hu⟩, rw ← hu, exact f.le_bound _ }, let S := supr (λ (t : A), f (↑t \ ↑s)), have S_pos : 0 < S, { rcases h s.1 s.2 with ⟨t, t_count, ht⟩, apply ht.trans_le, let t' : A := ⟨t, t_count⟩, change f (↑t' \ ↑s) ≤ S, exact le_csupr B t' }, rcases exists_lt_of_lt_csupr (half_lt_self S_pos) with ⟨t, ht⟩, refine ⟨t, λ u, _⟩, calc f (↑u \ ↑s) ≤ S : le_csupr B _ ... = 2 * (S / 2) : by ring ... ≤ 2 * f (↑t \ ↑s) : mul_le_mul_of_nonneg_left ht.le (by norm_num) }, choose! F hF using this, -- iterate the above construction, by adding at each step a set with measure close to maximal in -- the complement of already chosen points. This is the set `s n` at step `n`. let G : A → A := λ u, ⟨(↑u : set α) ∪ ↑(F u), u.2.union (F u).2⟩, let s : ℕ → A := λ n, G^[n] empty, -- We will get a contradiction from the fact that there is a countable set `u` with positive -- measure in the complement of `⋃ n, s n`. rcases h (⋃ n, ↑(s n)) (countable_Union (λ n, (s n).2)) with ⟨t, t_count, ht⟩, let u : A := ⟨t \ ⋃ n, ↑(s n), t_count.mono (diff_subset _ _)⟩, set ε := f (↑u) with hε, have ε_pos : 0 < ε := ht, have I1 : ∀ n, ε / 2 ≤ f (↑(s (n+1)) \ ↑(s n)), { assume n, rw [div_le_iff' (show (0 : ℝ) < 2, by norm_num), hε], convert hF (s n) u using 3, { dsimp [u], ext x, simp only [not_exists, mem_Union, mem_diff], tauto }, { simp only [s, function.iterate_succ', subtype.coe_mk, union_diff_left] } }, have I2 : ∀ (n : ℕ), (n : ℝ) * (ε / 2) ≤ f (↑(s n)), { assume n, induction n with n IH, { simp only [s, bounded_additive_measure.empty, id.def, nat.cast_zero, zero_mul, function.iterate_zero, subtype.coe_mk], }, { have : (↑(s (n+1)) : set α) = (↑(s (n+1)) \ ↑(s n)) ∪ ↑(s n), by simp only [s, function.iterate_succ', union_comm, union_diff_self, subtype.coe_mk, union_diff_left], rw [nat.succ_eq_add_one, this, f.additive], swap, { exact disjoint_sdiff_self_left }, calc ((n + 1 : ℕ) : ℝ) * (ε / 2) = ε / 2 + n * (ε / 2) : by simp only [nat.cast_succ]; ring ... ≤ f (↑(s (n + 1 : ℕ)) \ ↑(s n)) + f (↑(s n)) : add_le_add (I1 n) IH } }, rcases exists_nat_gt (f.C / (ε / 2)) with ⟨n, hn⟩, have : (n : ℝ) ≤ f.C / (ε / 2), by { rw le_div_iff (half_pos ε_pos), exact (I2 n).trans (f.le_bound _) }, exact lt_irrefl _ (this.trans_lt hn) end lemma exists_discrete_support (f : bounded_additive_measure α) : ∃ s : set α, s.countable ∧ (∀ t : set α, t.countable → f (t \ s) = 0) := begin rcases f.exists_discrete_support_nonpos with ⟨s₁, s₁_count, h₁⟩, rcases (-f).exists_discrete_support_nonpos with ⟨s₂, s₂_count, h₂⟩, refine ⟨s₁ ∪ s₂, s₁_count.union s₂_count, λ t ht, le_antisymm _ _⟩, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₁, by rw [diff_diff, union_comm, union_assoc, union_self], rw this, exact h₁ _ (ht.mono (diff_subset _ _)) }, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₂, by rw [diff_diff, union_assoc, union_self], rw this, simp only [neg_nonpos, neg_apply] at h₂, exact h₂ _ (ht.mono (diff_subset _ _)) }, end /-- A countable set outside of which the measure gives zero mass to countable sets. We are not claiming this set is unique, but we make an arbitrary choice of such a set. -/ def discrete_support (f : bounded_additive_measure α) : set α := (exists_discrete_support f).some lemma countable_discrete_support (f : bounded_additive_measure α) : f.discrete_support.countable := (exists_discrete_support f).some_spec.1 lemma apply_countable (f : bounded_additive_measure α) (t : set α) (ht : t.countable) : f (t \ f.discrete_support) = 0 := (exists_discrete_support f).some_spec.2 t ht /-- The discrete part of a bounded additive measure, obtained by restricting the measure to its countable support. -/ def discrete_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict f.discrete_support /-- The continuous part of a bounded additive measure, giving zero measure to every countable set. -/ def continuous_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict (univ \ f.discrete_support) lemma eq_add_parts (f : bounded_additive_measure α) (s : set α) : f s = f.discrete_part s + f.continuous_part s := begin simp only [discrete_part, continuous_part, restrict_apply], rw [← f.additive, ← inter_distrib_right], { simp only [union_univ, union_diff_self, univ_inter] }, { have : disjoint f.discrete_support (univ \ f.discrete_support) := disjoint_sdiff_self_right, exact this.mono (inter_subset_left _ _) (inter_subset_left _ _) } end lemma discrete_part_apply (f : bounded_additive_measure α) (s : set α) : f.discrete_part s = f (f.discrete_support ∩ s) := rfl lemma continuous_part_apply_eq_zero_of_countable (f : bounded_additive_measure α) (s : set α) (hs : s.countable) : f.continuous_part s = 0 := begin simp [continuous_part], convert f.apply_countable s hs using 2, ext x, simp [and_comm] end lemma continuous_part_apply_diff (f : bounded_additive_measure α) (s t : set α) (hs : s.countable) : f.continuous_part (t \ s) = f.continuous_part t := begin conv_rhs { rw ← diff_union_inter t s }, rw [additive, self_eq_add_right], { exact continuous_part_apply_eq_zero_of_countable _ _ (hs.mono (inter_subset_right _ _)) }, { exact disjoint.mono_right (inter_subset_right _ _) disjoint_sdiff_self_left }, end end bounded_additive_measure open bounded_additive_measure section /-! ### Relationship between continuous functionals and finitely additive measures. -/ lemma norm_indicator_le_one (s : set α) (x : α) : ‖(indicator s (1 : α → ℝ)) x‖ ≤ 1 := by { simp only [indicator, pi.one_apply], split_ifs; norm_num } /-- A functional in the dual space of bounded functions gives rise to a bounded additive measure, by applying the functional to the indicator functions. -/ def _root_.continuous_linear_map.to_bounded_additive_measure [topological_space α] [discrete_topology α] (f : (α →ᵇ ℝ) →L[ℝ] ℝ) : bounded_additive_measure α := { to_fun := λ s, f (of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)), additive' := λ s t hst, begin have : of_normed_add_comm_group_discrete (indicator (s ∪ t) 1) 1 (norm_indicator_le_one _) = of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s) + of_normed_add_comm_group_discrete (indicator t 1) 1 (norm_indicator_le_one t), by { ext x, simp [indicator_union_of_disjoint hst], }, rw [this, f.map_add], end, exists_bound := ⟨‖f‖, λ s, begin have I : ‖of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)‖ ≤ 1, by apply norm_of_normed_add_comm_group_le _ zero_le_one, apply le_trans (f.le_op_norm _), simpa using mul_le_mul_of_nonneg_left I (norm_nonneg f), end⟩ } @[simp] lemma continuous_part_eval_clm_eq_zero [topological_space α] [discrete_topology α] (s : set α) (x : α) : (eval_clm ℝ x).to_bounded_additive_measure.continuous_part s = 0 := let f := (eval_clm ℝ x).to_bounded_additive_measure in calc f.continuous_part s = f.continuous_part (s \ {x}) : (continuous_part_apply_diff _ _ _ (countable_singleton x)).symm ... = f ((univ \ f.discrete_support) ∩ (s \ {x})) : rfl ... = indicator ((univ \ f.discrete_support) ∩ (s \ {x})) 1 x : rfl ... = 0 : by simp lemma to_functions_to_measure [measurable_space α] (μ : measure α) [is_finite_measure μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure s = (μ s).to_real := begin change μ.extension_to_bounded_functions (of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)) = (μ s).to_real, rw extension_to_bounded_functions_apply, { change ∫ x, s.indicator (λ y, (1 : ℝ)) x ∂μ = _, simp [integral_indicator hs] }, { change integrable (indicator s 1) μ, have : integrable (λ x, (1 : ℝ)) μ := integrable_const (1 : ℝ), apply this.mono' (measurable.indicator (@measurable_const _ _ _ _ (1 : ℝ)) hs).ae_strongly_measurable, apply filter.eventually_of_forall, exact norm_indicator_le_one _ } end lemma to_functions_to_measure_continuous_part [measurable_space α] [measurable_singleton_class α] (μ : measure α) [is_finite_measure μ] [has_no_atoms μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure.continuous_part s = (μ s).to_real := begin let f := μ.extension_to_bounded_functions.to_bounded_additive_measure, change f ((univ \ f.discrete_support) ∩ s) = (μ s).to_real, rw to_functions_to_measure, swap, { exact measurable_set.inter (measurable_set.univ.diff (countable.measurable_set f.countable_discrete_support)) hs }, congr' 1, rw [inter_comm, ← inter_diff_assoc, inter_univ], exact measure_diff_null (f.countable_discrete_support.measure_zero _) end end /-! ### A set in `ℝ²` large along verticals, small along horizontals We construct a subset of `ℝ²`, given as a family of sets, which is large along verticals (i.e., it only misses a countable set along each vertical) but small along horizontals (it is countable along horizontals). Such a set can not be measurable as it would contradict Fubini theorem. We need the continuum hypothesis to construct it. -/ theorem sierpinski_pathological_family (Hcont : #ℝ = aleph 1) : ∃ (f : ℝ → set ℝ), (∀ x, (univ \ f x).countable) ∧ (∀ y, {x : ℝ | y ∈ f x}.countable) := begin rcases cardinal.ord_eq ℝ with ⟨r, hr, H⟩, resetI, refine ⟨λ x, {y | r x y}, λ x, _, λ y, _⟩, { have : univ \ {y | r x y} = {y | r y x} ∪ {x}, { ext y, simp only [true_and, mem_univ, mem_set_of_eq, mem_insert_iff, union_singleton, mem_diff], rcases trichotomous_of r x y with h|rfl|h, { simp only [h, not_or_distrib, false_iff, not_true], split, { rintros rfl, exact irrefl_of r y h }, { exact asymm h } }, { simp only [true_or, eq_self_iff_true, iff_true], exact irrefl x }, { simp only [h, iff_true, or_true], exact asymm h } }, rw this, apply countable.union _ (countable_singleton _), rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r x H }, { rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r y H } end /-- A family of sets in `ℝ` which only miss countably many points, but such that any point is contained in only countably many of them. -/ def spf (Hcont : #ℝ = aleph 1) (x : ℝ) : set ℝ := (sierpinski_pathological_family Hcont).some x lemma countable_compl_spf (Hcont : #ℝ = aleph 1) (x : ℝ) : (univ \ spf Hcont x).countable := (sierpinski_pathological_family Hcont).some_spec.1 x lemma countable_spf_mem (Hcont : #ℝ = aleph 1) (y : ℝ) : {x | y ∈ spf Hcont x}.countable := (sierpinski_pathological_family Hcont).some_spec.2 y /-! ### A counterexample for the Pettis integral We construct a function `f` from `[0,1]` to a complete Banach space `B`, which is weakly measurable (i.e., for any continuous linear form `φ` on `B` the function `φ ∘ f` is measurable), bounded in norm (i.e., for all `x`, one has `‖f x‖ ≤ 1`), and still `f` has no Pettis integral. This construction, due to Phillips, requires the continuum hypothesis. We will take for `B` the space of all bounded functions on `ℝ`, with the supremum norm (no measure here, we are really talking of everywhere defined functions). And `f x` will be the characteristic function of a set which is large (it has countable complement), as in the Sierpinski pathological family. -/ /-- A family of bounded functions `f_x` from `ℝ` (seen with the discrete topology) to `ℝ` (in fact taking values in `{0, 1}`), indexed by a real parameter `x`, corresponding to the characteristic functions of the different fibers of the Sierpinski pathological family -/ def f (Hcont : #ℝ = aleph 1) (x : ℝ) : (discrete_copy ℝ →ᵇ ℝ) := of_normed_add_comm_group_discrete (indicator (spf Hcont x) 1) 1 (norm_indicator_le_one _) lemma apply_f_eq_continuous_part (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) (x : ℝ) (hx : φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x = ∅) : φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin set ψ := φ.to_bounded_additive_measure with hψ, have : φ (f Hcont x) = ψ (spf Hcont x) := rfl, have U : univ = spf Hcont x ∪ (univ \ spf Hcont x), by simp only [union_univ, union_diff_self], rw [this, eq_add_parts, discrete_part_apply, hx, ψ.empty, zero_add, U, ψ.continuous_part.additive _ _ disjoint_sdiff_self_right, ψ.continuous_part_apply_eq_zero_of_countable _ (countable_compl_spf Hcont x), add_zero], end lemma countable_ne (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)}.countable := begin have A : {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)} ⊆ {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅}, { assume x hx, contrapose! hx, simp only [not_not, mem_set_of_eq] at hx, simp [apply_f_eq_continuous_part Hcont φ x hx], }, have B : {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅} ⊆ ⋃ y ∈ φ.to_bounded_additive_measure.discrete_support, {x | y ∈ spf Hcont x}, { assume x hx, dsimp at hx, rw [← ne.def, ←nonempty_iff_ne_empty] at hx, simp only [exists_prop, mem_Union, mem_set_of_eq], exact hx }, apply countable.mono (subset.trans A B), exact countable.bUnion (countable_discrete_support _) (λ a ha, countable_spf_mem Hcont a), end lemma comp_ae_eq_const (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∀ᵐ x ∂(volume.restrict (Icc (0 : ℝ) 1)), φ.to_bounded_additive_measure.continuous_part univ = φ (f Hcont x) := begin apply ae_restrict_of_ae, refine measure_mono_null _ ((countable_ne Hcont φ).measure_zero _), assume x, simp only [imp_self, mem_set_of_eq, mem_compl_iff], end lemma integrable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : integrable_on (λ x, φ (f Hcont x)) (Icc 0 1) := begin have : integrable_on (λ x, φ.to_bounded_additive_measure.continuous_part univ) (Icc (0 : ℝ) 1) volume, by simp [integrable_on_const], apply integrable.congr this (comp_ae_eq_const Hcont φ), end lemma integral_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∫ x in Icc 0 1, φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin rw ← integral_congr_ae (comp_ae_eq_const Hcont φ), simp, end /-! The next few statements show that the function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` takes its values in a complete space, is scalarly measurable, is everywhere bounded by `1`, and still has no Pettis integral. -/ example : complete_space (discrete_copy ℝ →ᵇ ℝ) := by apply_instance /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is scalarly measurable. -/ lemma measurable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : measurable (λ x, φ (f Hcont x)) := begin have : measurable (λ x, φ.to_bounded_additive_measure.continuous_part univ) := measurable_const, refine this.measurable_of_countable_ne _, exact countable_ne Hcont φ, end /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is uniformly bounded by `1` in norm. -/ lemma norm_bound (Hcont : #ℝ = aleph 1) (x : ℝ) : ‖f Hcont x‖ ≤ 1 := norm_of_normed_add_comm_group_le _ zero_le_one _ /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` has no Pettis integral. -/ theorem no_pettis_integral (Hcont : #ℝ = aleph 1) : ¬ ∃ (g : discrete_copy ℝ →ᵇ ℝ), ∀ (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ), ∫ x in Icc 0 1, φ (f Hcont x) = φ g := begin rintros ⟨g, h⟩, simp only [integral_comp] at h, have : g = 0, { ext x, have : g x = eval_clm ℝ x g := rfl, rw [this, ← h], simp }, simp only [this, continuous_linear_map.map_zero] at h, specialize h (volume.restrict (Icc (0 : ℝ) 1)).extension_to_bounded_functions, simp_rw [to_functions_to_measure_continuous_part _ _ measurable_set.univ] at h, simpa using h, end end phillips_1940 end counterexample
14c57f9bf928881fad0a17a18b8a168df04ddb3c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/t10.lean
a215347ae62a0777d0f13e71969232103170887c
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
108
lean
set_option pp.colors true set_option pp.unicode false print options set_option pp.unicode true print options
90816df4bfc4bcb4637a347cb04c3e8cc30035a2
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/trim.lean
e41b661eaf1d9b7a2f957777ba596c8a497da60c
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
1,290
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .utils open monad expr tactic namespace super -- TODO(gabriel): rewrite using conversions meta def trim : expr → tactic expr | (app (lam n m d b) arg) := if ¬b.has_var then trim b else app <$> trim (lam n m d b) <*> trim arg | (app a b) := app <$> trim a <*> trim b | (lam n m d b) := do x ← mk_local' `x m d, b' ← trim (instantiate_var b x), return $ lam n m d (abstract_local b' x.local_uniq_name) | (elet n t v b) := if has_var b then do x ← mk_local_def `x t, b' ← trim (instantiate_var b x), return $ elet n t v (abstract_local b' x.local_uniq_name) else trim b | e := return e -- iterate trim until convergence meta def trim' : expr → tactic expr | e := do e' ← trim e, if e =ₐ e' then return e else trim' e' open tactic meta def with_trim {α} (tac : tactic α) : tactic α := do gs ← get_goals, match gs with | (g::gs) := do g' ← infer_type g >>= mk_meta_var, set_goals [g'], r ← tac, done, set_goals (g::gs), instantiate_mvars g' >>= trim' >>= exact, return r | [] := fail "no goal" end end super
3724d0181a4108ead67e3429a919673817ba1b59
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/Mbar/pseudo_normed_group.lean
c6104bd28fde79274f83f43ec4e609d96c9e71c2
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
800
lean
import pseudo_normed_group.with_Tinv import Mbar.Mbar_le /-! # Mbar_r(S) is a profinitely filtered pseudo-normed group with T⁻¹ This file constructs this instance. -/ universe u noncomputable theory open_locale big_operators nnreal variables {r' : ℝ≥0} {S : Type u} [fact (0 < r')] [fintype S] {c c₁ c₂ c₃ : ℝ≥0} namespace Mbar instance : profinitely_filtered_pseudo_normed_group_with_Tinv r' (Mbar r' S) := { Tinv := profinitely_filtered_pseudo_normed_group_hom.mk' Mbar.Tinv begin refine ⟨r'⁻¹, λ c, ⟨_, _⟩⟩, { intros x hx, exact Mbar.Tinv_mem_filtration hx }, { exact Mbar_le.continuous_Tinv _ _ _ _ } end, Tinv_mem_filtration := λ c x hx, Mbar.Tinv_mem_filtration hx, .. Mbar.profinitely_filtered_pseudo_normed_group } end Mbar #lint-
25dcc8c1463e81c47e0be3bd8cbbbba8bb585b64
5412d79aa1dc0b521605c38bef9f0d4557b5a29d
/stage0/src/Lean/Elab/MutualDef.lean
8d854b1b1accf3dea4e2e54ae4493e786c458561
[ "Apache-2.0" ]
permissive
smunix/lean4
a450ec0927dc1c74816a1bf2818bf8600c9fc9bf
3407202436c141e3243eafbecb4b8720599b970a
refs/heads/master
1,676,334,875,188
1,610,128,510,000
1,610,128,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,723
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.Closure import Lean.Meta.Check import Lean.Elab.Command import Lean.Elab.DefView import Lean.Elab.PreDefinition import Lean.Parser.Term namespace Lean.Elab open Lean.Parser.Term /- DefView after elaborating the header. -/ structure DefViewElabHeader where ref : Syntax modifiers : Modifiers kind : DefKind shortDeclName : Name declName : Name levelNames : List Name numParams : Nat type : Expr -- including the parameters valueStx : Syntax deriving Inhabited namespace Term open Meta private def checkModifiers (m₁ m₂ : Modifiers) : TermElabM Unit := do unless m₁.isUnsafe == m₂.isUnsafe do throwError "cannot mix unsafe and safe definitions" unless m₁.isNoncomputable == m₂.isNoncomputable do throwError "cannot mix computable and non-computable definitions" unless m₁.isPartial == m₂.isPartial do throwError "cannot mix partial and non-partial definitions" private def checkKinds (k₁ k₂ : DefKind) : TermElabM Unit := do unless k₁.isExample == k₂.isExample do throwError "cannot mix examples and definitions" -- Reason: we should discard examples unless k₁.isTheorem == k₂.isTheorem do throwError "cannot mix theorems and definitions" -- Reason: we will eventually elaborate theorems in `Task`s. private def check (prevHeaders : Array DefViewElabHeader) (newHeader : DefViewElabHeader) : TermElabM Unit := do if newHeader.kind.isTheorem && newHeader.modifiers.isUnsafe then throwError "'unsafe' theorems are not allowed" if newHeader.kind.isTheorem && newHeader.modifiers.isPartial then throwError "'partial' theorems are not allowed, 'partial' is a code generation directive" if newHeader.kind.isTheorem && newHeader.modifiers.isNoncomputable then throwError "'theorem' subsumes 'noncomputable', code is not generated for theorems" if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isUnsafe then throwError "'noncomputable unsafe' is not allowed" if newHeader.modifiers.isNoncomputable && newHeader.modifiers.isPartial then throwError "'noncomputable partial' is not allowed" if newHeader.modifiers.isPartial && newHeader.modifiers.isUnsafe then throwError "'unsafe' subsumes 'partial'" if h : 0 < prevHeaders.size then let firstHeader := prevHeaders.get ⟨0, h⟩ try unless newHeader.levelNames == firstHeader.levelNames do throwError "universe parameters mismatch" checkModifiers newHeader.modifiers firstHeader.modifiers checkKinds newHeader.kind firstHeader.kind catch | Exception.error ref msg => throw (Exception.error ref m!"invalid mutually recursive definitions, {msg}") | ex => throw ex else pure () private def registerFailedToInferDefTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit := registerCustomErrorIfMVar type ref "failed to infer definition type" private def elabFunType {α} (ref : Syntax) (xs : Array Expr) (view : DefView) (k : Array Expr → Expr → TermElabM α) : TermElabM α := do match view.type? with | some typeStx => elabTypeWithAutoBoundImplicit typeStx fun type => do synthesizeSyntheticMVarsNoPostponing let type ← instantiateMVars type registerFailedToInferDefTypeInfo type typeStx k xs (← mkForallFVars xs type) | none => let hole := mkHole ref let type ← elabType hole registerFailedToInferDefTypeInfo type ref k xs (← mkForallFVars xs type) def isAutoImplicit (fvarId : FVarId) : TermElabM Bool := return (← read).autoBoundImplicits.any fun x => x.fvarId! == fvarId private def elabHeaders (views : Array DefView) : TermElabM (Array DefViewElabHeader) := do let mut headers := #[] for view in views do let newHeader ← withRef view.ref do let ⟨shortDeclName, declName, levelNames⟩ ← expandDeclId (← getCurrNamespace) (← getLevelNames) view.declId view.modifiers applyAttributesAt declName view.modifiers.attrs AttributeApplicationTime.beforeElaboration withAutoBoundImplicitLocal <| withLevelNames levelNames <| elabBinders (catchAutoBoundImplicit := true) view.binders.getArgs fun xs => do let refForElabFunType := view.value elabFunType refForElabFunType xs view fun xs type => do let type ← mkForallFVars (← read).autoBoundImplicits.toArray type let xs ← addAutoBoundImplicits xs let levelNames ← getLevelNames let newHeader := { ref := view.ref, modifiers := view.modifiers, kind := view.kind, shortDeclName := shortDeclName, declName := declName, levelNames := levelNames, numParams := xs.size, type := type, valueStx := view.value : DefViewElabHeader } check headers newHeader pure newHeader headers := headers.push newHeader pure headers private partial def withFunLocalDecls {α} (headers : Array DefViewElabHeader) (k : Array Expr → TermElabM α) : TermElabM α := let rec loop (i : Nat) (fvars : Array Expr) := do if h : i < headers.size then let header := headers.get ⟨i, h⟩ withLocalDecl header.shortDeclName BinderInfo.auxDecl header.type fun fvar => loop (i+1) (fvars.push fvar) else k fvars loop 0 #[] private def expandWhereDeclsAsStructInst : Macro | `(whereDecls|where $[$decls:letRecDecl$[;]?]*) => do let letIdDecls ← decls.mapM fun stx => match stx with | `(letRecDecl|$attrs:attributes $decl:letDecl) => Macro.throwErrorAt stx "attributes are 'where' elements are currently not supported here" | `(letRecDecl|$decl:letPatDecl) => Macro.throwErrorAt stx "patterns are not allowed here" | `(letRecDecl|$decl:letEqnsDecl) => expandLetEqnsDecl decl | `(letRecDecl|$decl:letIdDecl) => pure decl | _ => unreachable! let structInstFields ← letIdDecls.mapM fun | stx@`(letIdDecl|$id:ident $[$binders]* $[: $ty?]? := $val) => withRef stx do let mut val := val if let some ty := ty? then val ← `(($val : $ty)) val ← `(fun $[$binders]* => $val:term) `(structInstField|$id:ident := $val) | _ => unreachable! `({ $[$structInstFields,]* }) | _ => unreachable! /- Recall that ``` def declValSimple := parser! " :=\n" >> termParser >> optional Term.whereDecls def declValEqns := parser! Term.matchAltsWhereDecls def declVal := declValSimple <|> declValEqns <|> Term.whereDecls ``` -/ private def declValToTerm (declVal : Syntax) : MacroM Syntax := withRef declVal do if declVal.isOfKind `Lean.Parser.Command.declValSimple then expandWhereDeclsOpt declVal[2] declVal[1] else if declVal.isOfKind `Lean.Parser.Command.declValEqns then expandMatchAltsWhereDecls declVal[0] else if declVal.isOfKind `Lean.Parser.Term.whereDecls then expandWhereDeclsAsStructInst declVal else Macro.throwErrorAt declVal "unexpected definition value" private def elabFunValues (headers : Array DefViewElabHeader) : TermElabM (Array Expr) := headers.mapM fun header => withDeclName header.declName $ withLevelNames header.levelNames do let valStx ← liftMacroM $ declValToTerm header.valueStx forallBoundedTelescope header.type header.numParams fun xs type => do let val ← elabTermEnsuringType valStx type mkLambdaFVars xs val private def collectUsed (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift) : StateRefT CollectFVars.State TermElabM Unit := do headers.forM fun header => collectUsedFVars header.type values.forM collectUsedFVars toLift.forM fun letRecToLift => do collectUsedFVars letRecToLift.type collectUsedFVars letRecToLift.val private def removeUnusedVars (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift) : TermElabM (LocalContext × LocalInstances × Array Expr) := do let (_, used) ← (collectUsed headers values toLift).run {} removeUnused vars used private def withUsed {α} (vars : Array Expr) (headers : Array DefViewElabHeader) (values : Array Expr) (toLift : List LetRecToLift) (k : Array Expr → TermElabM α) : TermElabM α := do let (lctx, localInsts, vars) ← removeUnusedVars vars headers values toLift withLCtx lctx localInsts $ k vars private def isExample (views : Array DefView) : Bool := views.any (·.kind.isExample) private def isTheorem (views : Array DefView) : Bool := views.any (·.kind.isTheorem) private def instantiateMVarsAtHeader (header : DefViewElabHeader) : TermElabM DefViewElabHeader := do let type ← instantiateMVars header.type pure { header with type := type } private def instantiateMVarsAtLetRecToLift (toLift : LetRecToLift) : TermElabM LetRecToLift := do let type ← instantiateMVars toLift.type let val ← instantiateMVars toLift.val pure { toLift with type := type, val := val } private def typeHasRecFun (type : Expr) (funFVars : Array Expr) (letRecsToLift : List LetRecToLift) : Option FVarId := let occ? := type.find? fun e => match e with | Expr.fvar fvarId _ => funFVars.contains e || letRecsToLift.any fun toLift => toLift.fvarId == fvarId | _ => false match occ? with | some (Expr.fvar fvarId _) => some fvarId | _ => none private def getFunName (fvarId : FVarId) (letRecsToLift : List LetRecToLift) : TermElabM Name := do match (← findLocalDecl? fvarId) with | some decl => pure decl.userName | none => /- Recall that the FVarId of nested let-recs are not in the current local context. -/ match letRecsToLift.findSome? fun toLift => if toLift.fvarId == fvarId then some toLift.shortDeclName else none with | none => throwError "unknown function" | some n => pure n /- Ensures that the of let-rec definition types do not contain functions being defined. In principle, this test can be improved. We could perform it after we separate the set of functions is strongly connected components. However, this extra complication doesn't seem worth it. -/ private def checkLetRecsToLiftTypes (funVars : Array Expr) (letRecsToLift : List LetRecToLift) : TermElabM Unit := letRecsToLift.forM fun toLift => match typeHasRecFun toLift.type funVars letRecsToLift with | none => pure () | some fvarId => do let fnName ← getFunName fvarId letRecsToLift throwErrorAt! toLift.ref "invalid type in 'let rec', it uses '{fnName}' which is being defined simultaneously" namespace MutualClosure /- A mapping from FVarId to Set of FVarIds. -/ abbrev UsedFVarsMap := NameMap NameSet /- Create the `UsedFVarsMap` mapping that takes the variable id for the mutually recursive functions being defined to the set of free variables in its definition. For `mainFVars`, this is just the set of section variables `sectionVars` used. For nested let-rec functions, we collect their free variables. Recall that a `let rec` expressions are encoded as follows in the elaborator. ```lean let rec f : A := t, g : B := s; body ``` is encoded as ```lean let f : A := ?m₁; let g : B := ?m₂; body ``` where `?m₁` and `?m₂` are synthetic opaque metavariables. That are assigned by this module. We may have nested `let rec`s. ```lean let rec f : A := let rec g : B := t; s; body ``` is encoded as ```lean let f : A := ?m₁; body ``` and the body of `f` is stored the field `val` of a `LetRecToLift`. For the example above, we would have a `LetRecToLift` containing: ``` { mvarId := m₁, val := `(let g : B := ?m₂; body) ... } ``` Note that `g` is not a free variable at `(let g : B := ?m₂; body)`. We recover the fact that `f` depends on `g` because it contains `m₂` -/ private def mkInitialUsedFVarsMap (mctx : MetavarContext) (sectionVars : Array Expr) (mainFVarIds : Array FVarId) (letRecsToLift : List LetRecToLift) : UsedFVarsMap := do let mut sectionVarSet := {} for var in sectionVars do sectionVarSet := sectionVarSet.insert var.fvarId! let mut usedFVarMap := {} for mainFVarId in mainFVarIds do usedFVarMap := usedFVarMap.insert mainFVarId sectionVarSet for toLift in letRecsToLift do let state := Lean.collectFVars {} toLift.val let state := Lean.collectFVars state toLift.type let mut set := state.fvarSet /- toLift.val may contain metavariables that are placeholders for nested let-recs. We should collect the fvarId for the associated let-rec because we need this information to compute the fixpoint later. -/ let mvarIds := (toLift.val.collectMVars {}).result for mvarId in mvarIds do match letRecsToLift.findSome? fun (toLift : LetRecToLift) => if toLift.mvarId == mctx.getDelayedRoot mvarId then some toLift.fvarId else none with | some fvarId => set := set.insert fvarId | none => pure () usedFVarMap := usedFVarMap.insert toLift.fvarId set pure usedFVarMap /- The let-recs may invoke each other. Example: ``` let rec f (x : Nat) := g x + y g : Nat → Nat | 0 => 1 | x+1 => f x + z ``` `y` is free variable in `f`, and `z` is a free variable in `g`. To close `f` and `g`, `y` and `z` must be in the closure of both. That is, we need to generate the top-level definitions. ``` def f (y z x : Nat) := g y z x + y def g (y z : Nat) : Nat → Nat | 0 => 1 | x+1 => f y z x + z ``` -/ namespace FixPoint structure State where usedFVarsMap : UsedFVarsMap := {} modified : Bool := false abbrev M := ReaderT (List FVarId) $ StateM State private def isModified : M Bool := do pure (← get).modified private def resetModified : M Unit := modify fun s => { s with modified := false } private def markModified : M Unit := modify fun s => { s with modified := true } private def getUsedFVarsMap : M UsedFVarsMap := do pure (← get).usedFVarsMap private def modifyUsedFVars (f : UsedFVarsMap → UsedFVarsMap) : M Unit := modify fun s => { s with usedFVarsMap := f s.usedFVarsMap } -- merge s₂ into s₁ private def merge (s₁ s₂ : NameSet) : M NameSet := s₂.foldM (init := s₁) fun s₁ k => do if s₁.contains k then pure s₁ else markModified pure $ s₁.insert k private def updateUsedVarsOf (fvarId : FVarId) : M Unit := do let usedFVarsMap ← getUsedFVarsMap match usedFVarsMap.find? fvarId with | none => pure () | some fvarIds => let fvarIdsNew ← fvarIds.foldM (init := fvarIds) fun fvarIdsNew fvarId' => if fvarId == fvarId' then pure fvarIdsNew else match usedFVarsMap.find? fvarId' with | none => pure fvarIdsNew /- We are being sloppy here `otherFVarIds` may contain free variables that are not in the context of the let-rec associated with fvarId. We filter these out-of-context free variables later. -/ | some otherFVarIds => merge fvarIdsNew otherFVarIds modifyUsedFVars fun usedFVars => usedFVars.insert fvarId fvarIdsNew private partial def fixpoint : Unit → M Unit | _ => do resetModified let letRecFVarIds ← read letRecFVarIds.forM updateUsedVarsOf if (← isModified) then fixpoint () def run (letRecFVarIds : List FVarId) (usedFVarsMap : UsedFVarsMap) : UsedFVarsMap := let (_, s) := ((fixpoint ()).run letRecFVarIds).run { usedFVarsMap := usedFVarsMap } s.usedFVarsMap end FixPoint abbrev FreeVarMap := NameMap (Array FVarId) private def mkFreeVarMap (mctx : MetavarContext) (sectionVars : Array Expr) (mainFVarIds : Array FVarId) (recFVarIds : Array FVarId) (letRecsToLift : List LetRecToLift) : FreeVarMap := do let usedFVarsMap := mkInitialUsedFVarsMap mctx sectionVars mainFVarIds letRecsToLift let letRecFVarIds := letRecsToLift.map fun toLift => toLift.fvarId let usedFVarsMap := FixPoint.run letRecFVarIds usedFVarsMap let mut freeVarMap := {} for toLift in letRecsToLift do let lctx := toLift.lctx let fvarIdsSet := (usedFVarsMap.find? toLift.fvarId).get! let fvarIds := fvarIdsSet.fold (init := #[]) fun fvarIds fvarId => if lctx.contains fvarId && !recFVarIds.contains fvarId then fvarIds.push fvarId else fvarIds freeVarMap := freeVarMap.insert toLift.fvarId fvarIds pure freeVarMap structure ClosureState where newLocalDecls : Array LocalDecl := #[] localDecls : Array LocalDecl := #[] newLetDecls : Array LocalDecl := #[] exprArgs : Array Expr := #[] private def pickMaxFVar? (lctx : LocalContext) (fvarIds : Array FVarId) : Option FVarId := fvarIds.getMax? fun fvarId₁ fvarId₂ => (lctx.get! fvarId₁).index < (lctx.get! fvarId₂).index private def preprocess (e : Expr) : TermElabM Expr := do let e ← instantiateMVars e -- which let-decls are dependent. We say a let-decl is dependent if its lambda abstraction is type incorrect. Meta.check e pure e /- Push free variables in `s` to `toProcess` if they are not already there. -/ private def pushNewVars (toProcess : Array FVarId) (s : CollectFVars.State) : Array FVarId := s.fvarSet.fold (init := toProcess) fun toProcess fvarId => if toProcess.contains fvarId then toProcess else toProcess.push fvarId private def pushLocalDecl (toProcess : Array FVarId) (fvarId : FVarId) (userName : Name) (type : Expr) (bi := BinderInfo.default) : StateRefT ClosureState TermElabM (Array FVarId) := do let type ← preprocess type modify fun s => { s with newLocalDecls := s.newLocalDecls.push $ LocalDecl.cdecl arbitrary fvarId userName type bi, exprArgs := s.exprArgs.push (mkFVar fvarId) } pure $ pushNewVars toProcess (collectFVars {} type) private partial def mkClosureForAux (toProcess : Array FVarId) : StateRefT ClosureState TermElabM Unit := do let lctx ← getLCtx match pickMaxFVar? lctx toProcess with | none => pure () | some fvarId => trace[Elab.definition.mkClosure]! "toProcess: {toProcess.map mkFVar}, maxVar: {mkFVar fvarId}" let toProcess := toProcess.erase fvarId let localDecl ← getLocalDecl fvarId match localDecl with | LocalDecl.cdecl _ _ userName type bi => let toProcess ← pushLocalDecl toProcess fvarId userName type bi mkClosureForAux toProcess | LocalDecl.ldecl _ _ userName type val _ => let zetaFVarIds ← getZetaFVarIds if !zetaFVarIds.contains fvarId then /- Non-dependent let-decl. See comment at src/Lean/Meta/Closure.lean -/ let toProcess ← pushLocalDecl toProcess fvarId userName type mkClosureForAux toProcess else /- Dependent let-decl. -/ let type ← preprocess type let val ← preprocess val modify fun s => { s with newLetDecls := s.newLetDecls.push $ LocalDecl.ldecl arbitrary fvarId userName type val false, /- We don't want to interleave let and lambda declarations in our closure. So, we expand any occurrences of fvarId at `newLocalDecls` and `localDecls` -/ newLocalDecls := s.newLocalDecls.map (replaceFVarIdAtLocalDecl fvarId val), localDecls := s.localDecls.map (replaceFVarIdAtLocalDecl fvarId val) } mkClosureForAux (pushNewVars toProcess (collectFVars (collectFVars {} type) val)) private partial def mkClosureFor (freeVars : Array FVarId) (localDecls : Array LocalDecl) : TermElabM ClosureState := do let (_, s) ← (mkClosureForAux freeVars).run { localDecls := localDecls } pure { s with newLocalDecls := s.newLocalDecls.reverse, newLetDecls := s.newLetDecls.reverse, exprArgs := s.exprArgs.reverse } structure LetRecClosure where localDecls : Array LocalDecl closed : Expr -- expression used to replace occurrences of the let-rec FVarId toLift : LetRecToLift private def mkLetRecClosureFor (toLift : LetRecToLift) (freeVars : Array FVarId) : TermElabM LetRecClosure := do let lctx := toLift.lctx withLCtx lctx toLift.localInstances do lambdaTelescope toLift.val fun xs val => do let type ← instantiateForall toLift.type xs let lctx ← getLCtx let s ← mkClosureFor freeVars $ xs.map fun x => lctx.get! x.fvarId! let type := Closure.mkForall s.localDecls $ Closure.mkForall s.newLetDecls type let val := Closure.mkLambda s.localDecls $ Closure.mkLambda s.newLetDecls val let c := mkAppN (Lean.mkConst toLift.declName) s.exprArgs assignExprMVar toLift.mvarId c pure ⟨s.newLocalDecls, c, { toLift with val := val, type := type }⟩ private def mkLetRecClosures (letRecsToLift : List LetRecToLift) (freeVarMap : FreeVarMap) : TermElabM (List LetRecClosure) := letRecsToLift.mapM fun toLift => mkLetRecClosureFor toLift (freeVarMap.find? toLift.fvarId).get! /- Mapping from FVarId of mutually recursive functions being defined to "closure" expression. -/ abbrev Replacement := NameMap Expr def insertReplacementForMainFns (r : Replacement) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) : Replacement := mainFVars.size.fold (init := r) fun i r => r.insert (mainFVars.get! i).fvarId! (mkAppN (Lean.mkConst (mainHeaders.get! i).declName) sectionVars) def insertReplacementForLetRecs (r : Replacement) (letRecClosures : List LetRecClosure) : Replacement := letRecClosures.foldl (init := r) fun r c => r.insert c.toLift.fvarId c.closed def Replacement.apply (r : Replacement) (e : Expr) : Expr := e.replace fun e => match e with | Expr.fvar fvarId _ => match r.find? fvarId with | some c => some c | _ => none | _ => none def pushMain (preDefs : Array PreDefinition) (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainVals : Array Expr) : TermElabM (Array PreDefinition) := mainHeaders.size.foldM (init := preDefs) fun i preDefs => do let header := mainHeaders[i] let val ← mkLambdaFVars sectionVars mainVals[i] let type ← mkForallFVars sectionVars header.type pure $ preDefs.push { kind := header.kind, declName := header.declName, lparams := [], -- we set it later modifiers := header.modifiers, type := type, value := val } def pushLetRecs (preDefs : Array PreDefinition) (letRecClosures : List LetRecClosure) (kind : DefKind) (modifiers : Modifiers) : Array PreDefinition := letRecClosures.foldl (init := preDefs) fun preDefs c => let type := Closure.mkForall c.localDecls c.toLift.type let val := Closure.mkLambda c.localDecls c.toLift.val preDefs.push { kind := kind, declName := c.toLift.declName, lparams := [], -- we set it later modifiers := { modifiers with attrs := c.toLift.attrs }, type := type, value := val } def getKindForLetRecs (mainHeaders : Array DefViewElabHeader) : DefKind := if mainHeaders.any fun h => h.kind.isTheorem then DefKind.«theorem» else DefKind.«def» def getModifiersForLetRecs (mainHeaders : Array DefViewElabHeader) : Modifiers := { isNoncomputable := mainHeaders.any fun h => h.modifiers.isNoncomputable, isPartial := mainHeaders.any fun h => h.modifiers.isPartial, isUnsafe := mainHeaders.any fun h => h.modifiers.isUnsafe } /- - `sectionVars`: The section variables used in the `mutual` block. - `mainHeaders`: The elaborated header of the top-level definitions being defined by the mutual block. - `mainFVars`: The auxiliary variables used to represent the top-level definitions being defined by the mutual block. - `mainVals`: The elaborated value for the top-level definitions - `letRecsToLift`: The let-rec's definitions that need to be lifted -/ def main (sectionVars : Array Expr) (mainHeaders : Array DefViewElabHeader) (mainFVars : Array Expr) (mainVals : Array Expr) (letRecsToLift : List LetRecToLift) : TermElabM (Array PreDefinition) := do -- Store in recFVarIds the fvarId of every function being defined by the mutual block. let mainFVarIds := mainFVars.map Expr.fvarId! let recFVarIds := (letRecsToLift.toArray.map fun toLift => toLift.fvarId) ++ mainFVarIds -- Compute the set of free variables (excluding `recFVarIds`) for each let-rec. let mctx ← getMCtx let freeVarMap := mkFreeVarMap mctx sectionVars mainFVarIds recFVarIds letRecsToLift resetZetaFVarIds withTrackingZeta do -- By checking `toLift.type` and `toLift.val` we populate `zetaFVarIds`. See comments at `src/Lean/Meta/Closure.lean`. letRecsToLift.forM fun toLift => withLCtx toLift.lctx toLift.localInstances do Meta.check toLift.type; Meta.check toLift.val let letRecClosures ← mkLetRecClosures letRecsToLift freeVarMap -- mkLetRecClosures assign metavariables that were placeholders for the lifted declarations. let mainVals ← mainVals.mapM (instantiateMVars ·) let mainHeaders ← mainHeaders.mapM instantiateMVarsAtHeader let letRecClosures ← letRecClosures.mapM fun closure => do pure { closure with toLift := (← instantiateMVarsAtLetRecToLift closure.toLift) } -- Replace fvarIds for functions being defined with closed terms let r := insertReplacementForMainFns {} sectionVars mainHeaders mainFVars let r := insertReplacementForLetRecs r letRecClosures let mainVals := mainVals.map r.apply let mainHeaders := mainHeaders.map fun h => { h with type := r.apply h.type } let letRecClosures := letRecClosures.map fun c => { c with toLift := { c.toLift with type := r.apply c.toLift.type, val := r.apply c.toLift.val } } let letRecKind := getKindForLetRecs mainHeaders let letRecMods := getModifiersForLetRecs mainHeaders pushMain (pushLetRecs #[] letRecClosures letRecKind letRecMods) sectionVars mainHeaders mainVals end MutualClosure private def getAllUserLevelNames (headers : Array DefViewElabHeader) : List Name := if h : 0 < headers.size then -- Recall that all top-level functions must have the same levels. See `check` method above (headers.get ⟨0, h⟩).levelNames else [] def elabMutualDef (vars : Array Expr) (views : Array DefView) : TermElabM Unit := do let scopeLevelNames ← getLevelNames let headers ← elabHeaders views let allUserLevelNames := getAllUserLevelNames headers withFunLocalDecls headers fun funFVars => do let values ← elabFunValues headers Term.synthesizeSyntheticMVarsNoPostponing if isExample views then pure () else let values ← values.mapM (instantiateMVars ·) let headers ← headers.mapM instantiateMVarsAtHeader let letRecsToLift ← getLetRecsToLift let letRecsToLift ← letRecsToLift.mapM instantiateMVarsAtLetRecToLift checkLetRecsToLiftTypes funFVars letRecsToLift withUsed vars headers values letRecsToLift fun vars => do let preDefs ← MutualClosure.main vars headers funFVars values letRecsToLift let preDefs ← levelMVarToParamPreDecls preDefs let preDefs ← instantiateMVarsAtPreDecls preDefs let preDefs ← fixLevelParams preDefs scopeLevelNames allUserLevelNames addPreDefinitions preDefs end Term namespace Command def elabMutualDef (ds : Array Syntax) : CommandElabM Unit := do let views ← ds.mapM fun d => do let modifiers ← elabModifiers d[0] mkDefView modifiers d[1] runTermElabM none fun vars => Term.elabMutualDef vars views end Command end Lean.Elab
45367eced608fe52f9e30d27ea4f7f09182704f1
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group/prod.lean
a0029e17dd12597e2bc6ae917a7b883273b2ecdc
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
15,542
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Yury Kudryashov -/ import algebra.group.opposite /-! # Monoid, group etc structures on `M × N` In this file we define one-binop (`monoid`, `group` etc) structures on `M × N`. We also prove trivial `simp` lemmas, and define the following operations on `monoid_hom`s: * `fst M N : M × N →* M`, `snd M N : M × N →* N`: projections `prod.fst` and `prod.snd` as `monoid_hom`s; * `inl M N : M →* M × N`, `inr M N : N →* M × N`: inclusions of first/second monoid into the product; * `f.prod g : `M →* N × P`: sends `x` to `(f x, g x)`; * `f.coprod g : M × N →* P`: sends `(x, y)` to `f x * g y`; * `f.prod_map g : M × N → M' × N'`: `prod.map f g` as a `monoid_hom`, sends `(x, y)` to `(f x, g y)`. ## Main declarations * `mul_mul_hom`/`mul_monoid_hom`/`mul_monoid_with_zero_hom`: Multiplication bundled as a multiplicative/monoid/monoid with zero homomorphism. * `div_monoid_hom`/`div_monoid_with_zero_hom`: Division bundled as a monoid/monoid with zero homomorphism. -/ variables {A : Type*} {B : Type*} {G : Type*} {H : Type*} {M : Type*} {N : Type*} {P : Type*} namespace prod @[to_additive] instance [has_mul M] [has_mul N] : has_mul (M × N) := ⟨λ p q, ⟨p.1 * q.1, p.2 * q.2⟩⟩ @[simp, to_additive] lemma fst_mul [has_mul M] [has_mul N] (p q : M × N) : (p * q).1 = p.1 * q.1 := rfl @[simp, to_additive] lemma snd_mul [has_mul M] [has_mul N] (p q : M × N) : (p * q).2 = p.2 * q.2 := rfl @[simp, to_additive] lemma mk_mul_mk [has_mul M] [has_mul N] (a₁ a₂ : M) (b₁ b₂ : N) : (a₁, b₁) * (a₂, b₂) = (a₁ * a₂, b₁ * b₂) := rfl @[to_additive] lemma mul_def [has_mul M] [has_mul N] (p q : M × N) : p * q = (p.1 * q.1, p.2 * q.2) := rfl @[to_additive] instance [has_one M] [has_one N] : has_one (M × N) := ⟨(1, 1)⟩ @[simp, to_additive] lemma fst_one [has_one M] [has_one N] : (1 : M × N).1 = 1 := rfl @[simp, to_additive] lemma snd_one [has_one M] [has_one N] : (1 : M × N).2 = 1 := rfl @[to_additive] lemma one_eq_mk [has_one M] [has_one N] : (1 : M × N) = (1, 1) := rfl @[simp, to_additive] lemma mk_eq_one [has_one M] [has_one N] {x : M} {y : N} : (x, y) = 1 ↔ x = 1 ∧ y = 1 := mk.inj_iff @[to_additive] lemma fst_mul_snd [mul_one_class M] [mul_one_class N] (p : M × N) : (p.fst, 1) * (1, p.snd) = p := ext (mul_one p.1) (one_mul p.2) @[to_additive] instance [has_inv M] [has_inv N] : has_inv (M × N) := ⟨λp, (p.1⁻¹, p.2⁻¹)⟩ @[simp, to_additive] lemma fst_inv [has_inv G] [has_inv H] (p : G × H) : (p⁻¹).1 = (p.1)⁻¹ := rfl @[simp, to_additive] lemma snd_inv [has_inv G] [has_inv H] (p : G × H) : (p⁻¹).2 = (p.2)⁻¹ := rfl @[simp, to_additive] lemma inv_mk [has_inv G] [has_inv H] (a : G) (b : H) : (a, b)⁻¹ = (a⁻¹, b⁻¹) := rfl @[to_additive] instance [has_div M] [has_div N] : has_div (M × N) := ⟨λ p q, ⟨p.1 / q.1, p.2 / q.2⟩⟩ @[simp] lemma fst_sub [add_group A] [add_group B] (a b : A × B) : (a - b).1 = a.1 - b.1 := rfl @[simp] lemma snd_sub [add_group A] [add_group B] (a b : A × B) : (a - b).2 = a.2 - b.2 := rfl @[simp] lemma mk_sub_mk [add_group A] [add_group B] (x₁ x₂ : A) (y₁ y₂ : B) : (x₁, y₁) - (x₂, y₂) = (x₁ - x₂, y₁ - y₂) := rfl instance [mul_zero_class M] [mul_zero_class N] : mul_zero_class (M × N) := { zero_mul := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨zero_mul _, zero_mul _⟩, mul_zero := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨mul_zero _, mul_zero _⟩, .. prod.has_zero, .. prod.has_mul } @[to_additive] instance [semigroup M] [semigroup N] : semigroup (M × N) := { mul_assoc := assume a b c, mk.inj_iff.mpr ⟨mul_assoc _ _ _, mul_assoc _ _ _⟩, .. prod.has_mul } instance [semigroup_with_zero M] [semigroup_with_zero N] : semigroup_with_zero (M × N) := { .. prod.mul_zero_class, .. prod.semigroup } @[to_additive] instance [mul_one_class M] [mul_one_class N] : mul_one_class (M × N) := { one_mul := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨one_mul _, one_mul _⟩, mul_one := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨mul_one _, mul_one _⟩, .. prod.has_mul, .. prod.has_one } @[to_additive] instance [monoid M] [monoid N] : monoid (M × N) := { npow := λ z a, ⟨monoid.npow z a.1, monoid.npow z a.2⟩, npow_zero' := λ z, ext (monoid.npow_zero' _) (monoid.npow_zero' _), npow_succ' := λ z a, ext (monoid.npow_succ' _ _) (monoid.npow_succ' _ _), .. prod.semigroup, .. prod.mul_one_class } @[to_additive] instance [div_inv_monoid G] [div_inv_monoid H] : div_inv_monoid (G × H) := { div_eq_mul_inv := λ a b, mk.inj_iff.mpr ⟨div_eq_mul_inv _ _, div_eq_mul_inv _ _⟩, zpow := λ z a, ⟨div_inv_monoid.zpow z a.1, div_inv_monoid.zpow z a.2⟩, zpow_zero' := λ z, ext (div_inv_monoid.zpow_zero' _) (div_inv_monoid.zpow_zero' _), zpow_succ' := λ z a, ext (div_inv_monoid.zpow_succ' _ _) (div_inv_monoid.zpow_succ' _ _), zpow_neg' := λ z a, ext (div_inv_monoid.zpow_neg' _ _) (div_inv_monoid.zpow_neg' _ _), .. prod.monoid, .. prod.has_inv, .. prod.has_div } @[to_additive] instance [group G] [group H] : group (G × H) := { mul_left_inv := assume a, mk.inj_iff.mpr ⟨mul_left_inv _, mul_left_inv _⟩, .. prod.div_inv_monoid } @[to_additive] instance [comm_semigroup G] [comm_semigroup H] : comm_semigroup (G × H) := { mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩, .. prod.semigroup } @[to_additive] instance [left_cancel_semigroup G] [left_cancel_semigroup H] : left_cancel_semigroup (G × H) := { mul_left_cancel := λ a b c h, prod.ext (mul_left_cancel (prod.ext_iff.1 h).1) (mul_left_cancel (prod.ext_iff.1 h).2), .. prod.semigroup } @[to_additive] instance [right_cancel_semigroup G] [right_cancel_semigroup H] : right_cancel_semigroup (G × H) := { mul_right_cancel := λ a b c h, prod.ext (mul_right_cancel (prod.ext_iff.1 h).1) (mul_right_cancel (prod.ext_iff.1 h).2), .. prod.semigroup } @[to_additive] instance [left_cancel_monoid M] [left_cancel_monoid N] : left_cancel_monoid (M × N) := { .. prod.left_cancel_semigroup, .. prod.monoid } @[to_additive] instance [right_cancel_monoid M] [right_cancel_monoid N] : right_cancel_monoid (M × N) := { .. prod.right_cancel_semigroup, .. prod.monoid } @[to_additive] instance [cancel_monoid M] [cancel_monoid N] : cancel_monoid (M × N) := { .. prod.right_cancel_monoid, .. prod.left_cancel_monoid } @[to_additive] instance [comm_monoid M] [comm_monoid N] : comm_monoid (M × N) := { .. prod.comm_semigroup, .. prod.monoid } @[to_additive] instance [cancel_comm_monoid M] [cancel_comm_monoid N] : cancel_comm_monoid (M × N) := { .. prod.left_cancel_monoid, .. prod.comm_monoid } instance [mul_zero_one_class M] [mul_zero_one_class N] : mul_zero_one_class (M × N) := { .. prod.mul_zero_class, .. prod.mul_one_class } instance [monoid_with_zero M] [monoid_with_zero N] : monoid_with_zero (M × N) := { .. prod.monoid, .. prod.mul_zero_one_class } instance [comm_monoid_with_zero M] [comm_monoid_with_zero N] : comm_monoid_with_zero (M × N) := { .. prod.comm_monoid, .. prod.monoid_with_zero } @[to_additive] instance [comm_group G] [comm_group H] : comm_group (G × H) := { .. prod.comm_semigroup, .. prod.group } end prod namespace monoid_hom variables (M N) [mul_one_class M] [mul_one_class N] /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `M`.-/ @[to_additive "Given additive monoids `A`, `B`, the natural projection homomorphism from `A × B` to `A`"] def fst : M × N →* M := ⟨prod.fst, rfl, λ _ _, rfl⟩ /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `N`.-/ @[to_additive "Given additive monoids `A`, `B`, the natural projection homomorphism from `A × B` to `B`"] def snd : M × N →* N := ⟨prod.snd, rfl, λ _ _, rfl⟩ /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `M` to `M × N`. -/ @[to_additive "Given additive monoids `A`, `B`, the natural inclusion homomorphism from `A` to `A × B`."] def inl : M →* M × N := ⟨λ x, (x, 1), rfl, λ _ _, prod.ext rfl (one_mul 1).symm⟩ /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `N` to `M × N`. -/ @[to_additive "Given additive monoids `A`, `B`, the natural inclusion homomorphism from `B` to `A × B`."] def inr : N →* M × N := ⟨λ y, (1, y), rfl, λ _ _, prod.ext (one_mul 1).symm rfl⟩ variables {M N} @[simp, to_additive] lemma coe_fst : ⇑(fst M N) = prod.fst := rfl @[simp, to_additive] lemma coe_snd : ⇑(snd M N) = prod.snd := rfl @[simp, to_additive] lemma inl_apply (x) : inl M N x = (x, 1) := rfl @[simp, to_additive] lemma inr_apply (y) : inr M N y = (1, y) := rfl @[simp, to_additive] lemma fst_comp_inl : (fst M N).comp (inl M N) = id M := rfl @[simp, to_additive] lemma snd_comp_inl : (snd M N).comp (inl M N) = 1 := rfl @[simp, to_additive] lemma fst_comp_inr : (fst M N).comp (inr M N) = 1 := rfl @[simp, to_additive] lemma snd_comp_inr : (snd M N).comp (inr M N) = id N := rfl section prod variable [mul_one_class P] /-- Combine two `monoid_hom`s `f : M →* N`, `g : M →* P` into `f.prod g : M →* N × P` given by `(f.prod g) x = (f x, g x)` -/ @[to_additive prod "Combine two `add_monoid_hom`s `f : M →+ N`, `g : M →+ P` into `f.prod g : M →+ N × P` given by `(f.prod g) x = (f x, g x)`"] protected def prod (f : M →* N) (g : M →* P) : M →* N × P := { to_fun := λ x, (f x, g x), map_one' := prod.ext f.map_one g.map_one, map_mul' := λ x y, prod.ext (f.map_mul x y) (g.map_mul x y) } @[simp, to_additive prod_apply] lemma prod_apply (f : M →* N) (g : M →* P) (x) : f.prod g x = (f x, g x) := rfl @[simp, to_additive fst_comp_prod] lemma fst_comp_prod (f : M →* N) (g : M →* P) : (fst N P).comp (f.prod g) = f := ext $ λ x, rfl @[simp, to_additive snd_comp_prod] lemma snd_comp_prod (f : M →* N) (g : M →* P) : (snd N P).comp (f.prod g) = g := ext $ λ x, rfl @[simp, to_additive prod_unique] lemma prod_unique (f : M →* N × P) : ((fst N P).comp f).prod ((snd N P).comp f) = f := ext $ λ x, by simp only [prod_apply, coe_fst, coe_snd, comp_apply, prod.mk.eta] end prod section prod_map variables {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] [mul_one_class P] (f : M →* M') (g : N →* N') /-- `prod.map` as a `monoid_hom`. -/ @[to_additive prod_map "`prod.map` as an `add_monoid_hom`"] def prod_map : M × N →* M' × N' := (f.comp (fst M N)).prod (g.comp (snd M N)) @[to_additive prod_map_def] lemma prod_map_def : prod_map f g = (f.comp (fst M N)).prod (g.comp (snd M N)) := rfl @[simp, to_additive coe_prod_map] lemma coe_prod_map : ⇑(prod_map f g) = prod.map f g := rfl @[to_additive prod_comp_prod_map] lemma prod_comp_prod_map (f : P →* M) (g : P →* N) (f' : M →* M') (g' : N →* N') : (f'.prod_map g').comp (f.prod g) = (f'.comp f).prod (g'.comp g) := rfl end prod_map section coprod variables [comm_monoid P] (f : M →* P) (g : N →* P) /-- Coproduct of two `monoid_hom`s with the same codomain: `f.coprod g (p : M × N) = f p.1 * g p.2`. -/ @[to_additive "Coproduct of two `add_monoid_hom`s with the same codomain: `f.coprod g (p : M × N) = f p.1 + g p.2`."] def coprod : M × N →* P := f.comp (fst M N) * g.comp (snd M N) @[simp, to_additive] lemma coprod_apply (p : M × N) : f.coprod g p = f p.1 * g p.2 := rfl @[simp, to_additive] lemma coprod_comp_inl : (f.coprod g).comp (inl M N) = f := ext $ λ x, by simp [coprod_apply] @[simp, to_additive] lemma coprod_comp_inr : (f.coprod g).comp (inr M N) = g := ext $ λ x, by simp [coprod_apply] @[simp, to_additive] lemma coprod_unique (f : M × N →* P) : (f.comp (inl M N)).coprod (f.comp (inr M N)) = f := ext $ λ x, by simp [coprod_apply, inl_apply, inr_apply, ← map_mul] @[simp, to_additive] lemma coprod_inl_inr {M N : Type*} [comm_monoid M] [comm_monoid N] : (inl M N).coprod (inr M N) = id (M × N) := coprod_unique (id $ M × N) lemma comp_coprod {Q : Type*} [comm_monoid Q] (h : P →* Q) (f : M →* P) (g : N →* P) : h.comp (f.coprod g) = (h.comp f).coprod (h.comp g) := ext $ λ x, by simp end coprod end monoid_hom namespace mul_equiv section variables {M N} [mul_one_class M] [mul_one_class N] /-- The equivalence between `M × N` and `N × M` given by swapping the components is multiplicative. -/ @[to_additive prod_comm "The equivalence between `M × N` and `N × M` given by swapping the components is additive."] def prod_comm : M × N ≃* N × M := { map_mul' := λ ⟨x₁, y₁⟩ ⟨x₂, y₂⟩, rfl, ..equiv.prod_comm M N } @[simp, to_additive coe_prod_comm] lemma coe_prod_comm : ⇑(prod_comm : M × N ≃* N × M) = prod.swap := rfl @[simp, to_additive coe_prod_comm_symm] lemma coe_prod_comm_symm : ⇑((prod_comm : M × N ≃* N × M).symm) = prod.swap := rfl end section variables {M N} [monoid M] [monoid N] /-- The monoid equivalence between units of a product of two monoids, and the product of the units of each monoid. -/ @[to_additive prod_add_units "The additive monoid equivalence between additive units of a product of two additive monoids, and the product of the additive units of each additive monoid."] def prod_units : (M × N)ˣ ≃* Mˣ × Nˣ := { to_fun := (units.map (monoid_hom.fst M N)).prod (units.map (monoid_hom.snd M N)), inv_fun := λ u, ⟨(u.1, u.2), (↑u.1⁻¹, ↑u.2⁻¹), by simp, by simp⟩, left_inv := λ u, by simp, right_inv := λ ⟨u₁, u₂⟩, by simp [units.map], map_mul' := monoid_hom.map_mul _ } end end mul_equiv section units open mul_opposite /-- Canonical homomorphism of monoids from `αˣ` into `α × αᵐᵒᵖ`. Used mainly to define the natural topology of `αˣ`. -/ def embed_product (α : Type*) [monoid α] : αˣ →* α × αᵐᵒᵖ := { to_fun := λ x, ⟨x, op ↑x⁻¹⟩, map_one' := by simp only [one_inv, eq_self_iff_true, units.coe_one, op_one, prod.mk_eq_one, and_self], map_mul' := λ x y, by simp only [mul_inv_rev, op_mul, units.coe_mul, prod.mk_mul_mk] } end units /-! ### Multiplication and division as homomorphisms -/ section bundled_mul_div variables {α : Type*} /-- Multiplication as a multiplicative homomorphism. -/ @[to_additive "Addition as an additive homomorphism.", simps] def mul_mul_hom [comm_semigroup α] : mul_hom (α × α) α := { to_fun := λ a, a.1 * a.2, map_mul' := λ a b, mul_mul_mul_comm _ _ _ _ } /-- Multiplication as a monoid homomorphism. -/ @[to_additive "Addition as an additive monoid homomorphism.", simps] def mul_monoid_hom [comm_monoid α] : α × α →* α := { map_one' := mul_one _, .. mul_mul_hom } /-- Multiplication as a multiplicative homomorphism with zero. -/ @[simps] def mul_monoid_with_zero_hom [comm_monoid_with_zero α] : α × α →*₀ α := { map_zero' := mul_zero _, .. mul_monoid_hom } /-- Division as a monoid homomorphism. -/ @[to_additive "Subtraction as an additive monoid homomorphism.", simps] def div_monoid_hom [comm_group α] : α × α →* α := { to_fun := λ a, a.1 / a.2, map_one' := div_one' _, map_mul' := λ a b, mul_div_comm' _ _ _ _ } /-- Division as a multiplicative homomorphism with zero. -/ @[simps] def div_monoid_with_zero_hom [comm_group_with_zero α] : α × α →*₀ α := { to_fun := λ a, a.1 / a.2, map_zero' := zero_div _, map_one' := div_one _, map_mul' := λ a b, (div_mul_div _ _ _ _).symm } end bundled_mul_div
2fe60a4624f56cbcc2de993925581cb7cad6c08a
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Hygiene.lean
00e9e35481b806424a6bc137d415799f8609f9c2
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
4,626
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Data.Name import Lean.Data.Options import Lean.Data.Format namespace Lean /- Remark: `MonadQuotation` class is part of the `Init` package and loaded by default since it is used in the builtin command `macro`. -/ structure Unhygienic.Context where ref : Syntax scope : MacroScope /-- Simplistic MonadQuotation that does not guarantee globally fresh names, that is, between different runs of this or other MonadQuotation implementations. It is only safe if the syntax quotations do not introduce bindings around antiquotations, and if references to globals are prefixed with `_root_.` (which is not allowed to refer to a local variable). `Unhygienic` can also be seen as a model implementation of `MonadQuotation` (since it is completely hygienic as long as it is "run" only once and can assume that there are no other implentations in use, as is the case for the elaboration monads that carry their macro scope state through the entire processing of a file). It uses the state monad to query and allocate the next macro scope, and uses the reader monad to store the stack of scopes corresponding to `withFreshMacroScope` calls. -/ abbrev Unhygienic := ReaderT Lean.Unhygienic.Context $ StateM MacroScope namespace Unhygienic instance : MonadQuotation Unhygienic where getRef := do (← read).ref withRef := fun ref => withReader ({ · with ref := ref }) getCurrMacroScope := do (← read).scope getMainModule := pure `UnhygienicMain withFreshMacroScope := fun x => do let fresh ← modifyGet fun n => (n, n + 1) withReader ({ · with scope := fresh}) x protected def run {α : Type} (x : Unhygienic α) : α := (x ⟨Syntax.missing, firstFrontendMacroScope⟩).run' (firstFrontendMacroScope+1) end Unhygienic private def mkInaccessibleUserNameAux (unicode : Bool) (name : Name) (idx : Nat) : Name := if unicode then if idx == 0 then name.appendAfter "✝" else name.appendAfter ("✝" ++ idx.toSuperscriptString) else name ++ Name.mkNum "_inaccessible" idx private def mkInaccessibleUserName (unicode : Bool) : Name → Name | Name.num p@(Name.str _ _ _) idx _ => mkInaccessibleUserNameAux unicode p idx | Name.num Name.anonymous idx _ => mkInaccessibleUserNameAux unicode Name.anonymous idx | Name.num p idx _ => if unicode then (mkInaccessibleUserName unicode p).appendAfter ("⁻" ++ idx.toSuperscriptString) else Name.mkNum (mkInaccessibleUserName unicode p) idx | n => n def sanitizeNamesDefault := true def getSanitizeNames (o : Options) : Bool:= o.get `pp.sanitizeNames sanitizeNamesDefault builtin_initialize registerOption `pp.sanitizeNames { defValue := sanitizeNamesDefault, group := "pp", descr := "add suffix '_{<idx>}' to shadowed/inaccessible variables when pretty printing" } structure NameSanitizerState where options : Options -- `x` ~> 2 if we're already using `x✝`, `x✝¹` nameStem2Idx : NameMap Nat := {} -- `x._hyg...` ~> `x✝` userName2Sanitized : NameMap Name := {} private partial def mkFreshInaccessibleUserName (userName : Name) (idx : Nat) : StateM NameSanitizerState Name := do let s ← get let userNameNew := mkInaccessibleUserName (Std.Format.getUnicode s.options) (Name.mkNum userName idx) if s.nameStem2Idx.contains userNameNew then mkFreshInaccessibleUserName userName (idx+1) else do modify fun s => { s with nameStem2Idx := s.nameStem2Idx.insert userName (idx+1) } pure userNameNew def sanitizeName (userName : Name) : StateM NameSanitizerState Name := do let stem := userName.eraseMacroScopes; let idx := (← get).nameStem2Idx.find? stem |>.getD 0 let san ← mkFreshInaccessibleUserName stem idx modify fun s => { s with userName2Sanitized := s.userName2Sanitized.insert userName san } pure san private partial def sanitizeSyntaxAux : Syntax → StateM NameSanitizerState Syntax | stx@(Syntax.ident _ _ n _) => do mkIdentFrom stx <$> match (← get).userName2Sanitized.find? n with | some n' => pure n' | none => if n.hasMacroScopes then sanitizeName n else pure n | Syntax.node k args => Syntax.node k <$> args.mapM sanitizeSyntaxAux | stx => pure stx def sanitizeSyntax (stx : Syntax) : StateM NameSanitizerState Syntax := do if getSanitizeNames (← get).options then sanitizeSyntaxAux stx else pure stx end Lean
e7a929204c1e0b9f0817a3520dcf64ab9616546b
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/library/init/meta/expr.lean
0a6b4d513677e406db2f436e4e6c0a1aee7a6ba0
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
11,713
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.level init.category.monad structure pos := (line : nat) (column : nat) instance : decidable_eq pos | ⟨l₁, c₁⟩ ⟨l₂, c₂⟩ := if h₁ : l₁ = l₂ then if h₂ : c₁ = c₂ then is_true (eq.rec_on h₁ (eq.rec_on h₂ rfl)) else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₂ h₂)) else is_false (λ contra, pos.no_confusion contra (λ e₁ e₂, absurd e₁ h₁)) meta instance : has_to_format pos := ⟨λ ⟨l, c⟩, "⟨" ++ l ++ ", " ++ c ++ "⟩"⟩ inductive binder_info | default | implicit | strict_implicit | inst_implicit | other instance : has_to_string binder_info := ⟨λ bi, match bi with | binder_info.default := "default" | binder_info.implicit := "implicit" | binder_info.strict_implicit := "strict_implicit" | binder_info.inst_implicit := "inst_implicit" | binder_info.other := "other" end⟩ meta constant macro_def : Type /- Reflect a C++ expr object. The VM replaces it with the C++ implementation. -/ meta inductive expr | var : nat → expr | sort : level → expr | const : name → list level → expr | mvar : name → expr → expr | local_const : name → name → binder_info → expr → expr | app : expr → expr → expr | lam : name → binder_info → expr → expr → expr | pi : name → binder_info → expr → expr → expr | elet : name → expr → expr → expr → expr | macro : macro_def → ∀ n, (fin n → expr) → expr /-- (reflected a) is a special opaque container for an `expr` representing `a`. It can only be obtained via type class inference, which will use the representation of `a` in the calling context. -/ meta constant {u} reflected {α : Type u} : α → Type u attribute [class] reflected meta constant {u} reflect {α : Type u} (a : α) [reflected a] : expr meta instance : inhabited expr := ⟨expr.sort level.zero⟩ meta constant expr.mk_macro (d : macro_def) : list expr → expr meta constant expr.macro_def_name (d : macro_def) : name meta def expr.mk_var (n : nat) : expr := expr.var n /- Choice macros are used to implement overloading. TODO(Leo): should we change it to pexpr? -/ meta constant expr.is_choice_macro : expr → bool /- Expressions can be annotated using the annotation macro. -/ meta constant expr.is_annotation : expr → option (name × expr) meta def expr.erase_annotations : expr → expr | e := match e.is_annotation with | some (_, a) := expr.erase_annotations a | none := e end -- Compares expressions, including binder names. meta constant expr.has_decidable_eq : decidable_eq expr attribute [instance] expr.has_decidable_eq -- Compares expressions while ignoring binder names. meta constant expr.alpha_eqv : expr → expr → bool notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt protected meta constant expr.to_string : expr → string meta instance : has_to_string expr := ⟨expr.to_string⟩ meta instance : has_to_format expr := ⟨λ e, e.to_string⟩ /- Coercion for letting users write (f a) instead of (expr.app f a) -/ meta instance : has_coe_to_fun expr := { F := λ e, expr → expr, coe := λ e, expr.app e } meta constant expr.hash : expr → nat -- Compares expressions, ignoring binder names, and sorting by hash. meta constant expr.lt : expr → expr → bool -- Compares expressions, ignoring binder names. meta constant expr.lex_lt : expr → expr → bool -- Compares expressions, ignoring binder names, and sorting by hash. meta def expr.cmp (a b : expr) : ordering := if expr.lt a b then ordering.lt else if a =ₐ b then ordering.eq else ordering.gt meta constant expr.fold {α : Type} : expr → α → (expr → nat → α → α) → α meta constant expr.replace : expr → (expr → nat → option expr) → expr meta constant expr.abstract_local : expr → name → expr meta constant expr.abstract_locals : expr → list name → expr meta def expr.abstract : expr → expr → expr | e (expr.local_const n m bi t) := e.abstract_local n | e _ := e meta constant expr.instantiate_univ_params : expr → list (name × level) → expr meta constant expr.instantiate_var : expr → expr → expr meta constant expr.instantiate_vars : expr → list expr → expr meta constant expr.subst : expr → expr → expr meta constant expr.has_var : expr → bool meta constant expr.has_var_idx : expr → nat → bool meta constant expr.has_local : expr → bool meta constant expr.has_meta_var : expr → bool meta constant expr.lift_vars : expr → nat → nat → expr meta constant expr.lower_vars : expr → nat → nat → expr /- (copy_pos_info src tgt) copy position information from src to tgt. -/ meta constant expr.copy_pos_info : expr → expr → expr meta constant expr.is_internal_cnstr : expr → option unsigned meta constant expr.get_nat_value : expr → option nat meta constant expr.collect_univ_params : expr → list name /-- `occurs e t` returns `tt` iff `e` occurs in `t` -/ meta constant expr.occurs : expr → expr → bool namespace expr open decidable -- Compares expressions, ignoring binder names, and sorting by hash. meta instance : has_ordering expr := ⟨ expr.cmp ⟩ meta def mk_true : expr := const `true [] meta def mk_false : expr := const `false [] /-- Returns the sorry macro with the given type. -/ meta constant mk_sorry (type : expr) : expr /-- Checks whether e is sorry, and returns its type. -/ meta constant is_sorry (e : expr) : option expr meta def instantiate_local (n : name) (s : expr) (e : expr) : expr := instantiate_var (abstract_local e n) s meta def instantiate_locals (s : list (name × expr)) (e : expr) : expr := instantiate_vars (abstract_locals e (list.reverse (list.map prod.fst s))) (list.map prod.snd s) meta def is_var : expr → bool | (var _) := tt | _ := ff meta def app_of_list : expr → list expr → expr | f [] := f | f (p::ps) := app_of_list (f p) ps meta def is_app : expr → bool | (app f a) := tt | e := ff meta def app_fn : expr → expr | (app f a) := f | a := a meta def app_arg : expr → expr | (app f a) := a | a := a meta def get_app_fn : expr → expr | (app f a) := get_app_fn f | a := a meta def get_app_num_args : expr → nat | (app f a) := get_app_num_args f + 1 | e := 0 meta def get_app_args_aux : list expr → expr → list expr | r (app f a) := get_app_args_aux (a::r) f | r e := r meta def get_app_args : expr → list expr := get_app_args_aux [] meta def mk_app : expr → list expr → expr | e [] := e | e (x::xs) := mk_app (e x) xs meta def ith_arg_aux : expr → nat → expr | (app f a) 0 := a | (app f a) (n+1) := ith_arg_aux f n | e _ := e meta def ith_arg (e : expr) (i : nat) : expr := ith_arg_aux e (get_app_num_args e - i - 1) meta def const_name : expr → name | (const n ls) := n | e := name.anonymous meta def is_constant : expr → bool | (const n ls) := tt | e := ff meta def is_local_constant : expr → bool | (local_const n m bi t) := tt | e := ff meta def local_uniq_name : expr → name | (local_const n m bi t) := n | e := name.anonymous meta def local_pp_name : expr → name | (local_const x n bi t) := n | e := name.anonymous meta def local_type : expr → expr | (local_const _ _ _ t) := t | e := e meta def is_constant_of : expr → name → bool | (const n₁ ls) n₂ := n₁ = n₂ | e n := ff meta def is_app_of (e : expr) (n : name) : bool := is_constant_of (get_app_fn e) n meta def is_napp_of (e : expr) (c : name) (n : nat) : bool := is_app_of e c ∧ get_app_num_args e = n meta def is_false : expr → bool | ```(false) := tt | _ := ff meta def is_not : expr → option expr | ```(not %%a) := some a | ```(%%a → false) := some a | e := none meta def is_and : expr → option (expr × expr) | ```(and %%α %%β) := some (α, β) | _ := none meta def is_or : expr → option (expr × expr) | ```(or %%α %%β) := some (α, β) | _ := none meta def is_eq : expr → option (expr × expr) | ```((%%a : %%_) = %%b) := some (a, b) | _ := none meta def is_ne : expr → option (expr × expr) | ```((%%a : %%_) ≠ %%b) := some (a, b) | _ := none meta def is_bin_arith_app (e : expr) (op : name) : option (expr × expr) := if is_napp_of e op 4 then some (app_arg (app_fn e), app_arg e) else none meta def is_lt (e : expr) : option (expr × expr) := is_bin_arith_app e ``has_lt.lt meta def is_gt (e : expr) : option (expr × expr) := is_bin_arith_app e ``gt meta def is_le (e : expr) : option (expr × expr) := is_bin_arith_app e ``has_le.le meta def is_ge (e : expr) : option (expr × expr) := is_bin_arith_app e ``ge meta def is_heq : expr → option (expr × expr × expr × expr) | ```(@heq %%α %%a %%β %%b) := some (α, a, β, b) | _ := none meta def is_pi : expr → bool | (pi _ _ _ _) := tt | e := ff meta def is_arrow : expr → bool | (pi _ _ _ b) := bnot (has_var b) | e := ff meta def is_let : expr → bool | (elet _ _ _ _) := tt | e := ff meta def binding_name : expr → name | (pi n _ _ _) := n | (lam n _ _ _) := n | e := name.anonymous meta def binding_info : expr → binder_info | (pi _ bi _ _) := bi | (lam _ bi _ _) := bi | e := binder_info.default meta def binding_domain : expr → expr | (pi _ _ d _) := d | (lam _ _ d _) := d | e := e meta def binding_body : expr → expr | (pi _ _ _ b) := b | (lam _ _ _ b) := b | e := e meta def imp (a b : expr) : expr := ```(%%a → %%b) meta def lambdas : list expr → expr → expr | (local_const uniq pp info t :: es) f := lam pp info t (abstract_local (lambdas es f) uniq) | _ f := f meta def pis : list expr → expr → expr | (local_const uniq pp info t :: es) f := pi pp info t (abstract_local (pis es f) uniq) | _ f := f open format private meta def p := λ xs, paren (format.join (list.intersperse " " xs)) private meta def macro_args_to_list_aux (n : nat) (args : fin n → expr) : Π (i : nat), i ≤ n → list expr | 0 _ := [] | (i+1) h := args ⟨i, h⟩ :: macro_args_to_list_aux i (nat.le_trans (nat.le_succ _) h) meta def macro_args_to_list (n : nat) (args : fin n → expr) : list expr := (macro_args_to_list_aux n args n (nat.le_refl _)).reverse meta def to_raw_fmt : expr → format | (var n) := p ["var", to_fmt n] | (sort l) := p ["sort", to_fmt l] | (const n ls) := p ["const", to_fmt n, to_fmt ls] | (mvar n t) := p ["mvar", to_fmt n, to_raw_fmt t] | (local_const n m bi t) := p ["local_const", to_fmt n, to_fmt m, to_raw_fmt t] | (app e f) := p ["app", to_raw_fmt e, to_raw_fmt f] | (lam n bi e t) := p ["lam", to_fmt n, to_string bi, to_raw_fmt e, to_raw_fmt t] | (pi n bi e t) := p ["pi", to_fmt n, to_string bi, to_raw_fmt e, to_raw_fmt t] | (elet n g e f) := p ["elet", to_fmt n, to_raw_fmt g, to_raw_fmt e, to_raw_fmt f] | (macro d n args) := sbracket (format.join (list.intersperse " " ("macro" :: to_fmt (macro_def_name d) :: list.map to_raw_fmt (macro_args_to_list n args)))) meta def mfold {α : Type} {m : Type → Type} [monad m] (e : expr) (a : α) (fn : expr → nat → α → m α) : m α := fold e (return a) (λ e n a, a >>= fn e n) end expr
debfae2957ba83f14d302b9ddbd367258d79391a
2eab05920d6eeb06665e1a6df77b3157354316ad
/test/lint.lean
e2c4531e61dfbd484c809f6b907a805a877af88a
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,586
lean
import tactic.lint import algebra.ring.basic def foo1 (n m : ℕ) : ℕ := n + 1 def foo2 (n m : ℕ) : m = m := by refl lemma foo3 (n m : ℕ) : ℕ := n - m lemma foo.foo (n m : ℕ) : n ≥ n := le_refl n instance bar.bar : has_add ℕ := by apply_instance -- we don't check the name of instances lemma foo.bar (ε > 0) : ε = ε := rfl -- >/≥ is allowed in binders (and in fact, in all hypotheses) /-- Test exception in `def_lemma` linter. -/ @[pattern] def my_exists_intro := @Exists.intro -- section -- local attribute [instance, priority 1001] classical.prop_decidable -- lemma foo4 : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) -- end open tactic meta def fold_over_with_cond {α} (l : list declaration) (tac : declaration → tactic (option α)) : tactic (list (declaration × α)) := l.mmap_filter $ λ d, option.map (λ x, (d, x)) <$> tac d run_cmd do let t := name × list ℕ, e ← get_env, let l := e.filter (λ d, e.in_current_file d.to_name ∧ ¬ d.is_auto_or_internal e), l2 ← fold_over_with_cond l (return ∘ check_unused_arguments), guard $ l2.length = 4, let l2 : list t := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ (⟨`foo1, [2]⟩ : t) ∈ l2, guard $ (⟨`foo2, [1]⟩ : t) ∈ l2, guard $ (⟨`foo.foo, [2]⟩ : t) ∈ l2, guard $ (⟨`foo.bar, [2]⟩ : t) ∈ l2, l2 ← fold_over_with_cond l linter.def_lemma.test, guard $ l2.length = 2, let l2 : list (name × _) := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo2, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo3, l3 ← fold_over_with_cond l linter.dup_namespace.test, guard $ l3.length = 1, guard $ ∃(x ∈ l3), (x : declaration × _).1.to_name = `foo.foo, l4 ← fold_over_with_cond l linter.ge_or_gt.test, guard $ l4.length = 1, guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo.foo, -- guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo4, (_, s) ← lint ff, guard $ "/- (slow tests skipped) -/\n".is_suffix_of s.to_string, (_, s2) ← lint tt, guard $ s.to_string ≠ s2.to_string, skip /- check customizability and nolint -/ meta def dummy_check (d : declaration) : tactic (option string) := return $ if d.to_name.last = "foo" then some "gotcha!" else none meta def linter.dummy_linter : linter := { test := dummy_check, auto_decls := ff, no_errors_found := "found nothing.", errors_found := "found something:" } @[nolint dummy_linter] def bar.foo : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) run_cmd do (_, s) ← lint tt lint_verbosity.medium [`linter.dummy_linter] tt, guard $ "/- found something: -/\n#check @foo.foo /- gotcha! -/\n".is_suffix_of s.to_string def incorrect_type_class_argument_test {α : Type} (x : α) [x = x] [decidable_eq α] [group α] : unit := () run_cmd do d ← get_decl `incorrect_type_class_argument_test, x ← linter.incorrect_type_class_argument.test d, guard $ x = some "These are not classes. argument 3: [_inst_1 : x = x]" section def impossible_instance_test {α β : Type} [add_group α] : has_add α := infer_instance local attribute [instance] impossible_instance_test run_cmd do d ← get_decl `impossible_instance_test, x ← linter.impossible_instance.test d, guard $ x = some "Impossible to infer argument 2: {β : Type}" def dangerous_instance_test {α β γ : Type} [ring α] [add_comm_group β] [has_coe α β] [has_inv γ] : has_add β := infer_instance local attribute [instance] dangerous_instance_test run_cmd do d ← get_decl `dangerous_instance_test, x ← linter.dangerous_instance.test d, guard $ x = some "The following arguments become metavariables. argument 1: {α : Type}, argument 3: {γ : Type}" end section def foo_has_mul {α} [has_mul α] : has_mul α := infer_instance local attribute [instance, priority 1] foo_has_mul run_cmd do d ← get_decl `foo_has_mul, some s ← fails_quickly 20 d, guard $ "type-class inference timed out".is_prefix_of s local attribute [instance, priority 10000] foo_has_mul run_cmd do d ← get_decl `foo_has_mul, some s ← fails_quickly 3000 d, guard $ "maximum class-instance resolution depth has been reached".is_prefix_of s end instance beta_redex_test {α} [monoid α] : (λ (X : Type), has_mul X) α := ⟨(*)⟩ run_cmd do d ← get_decl `beta_redex_test, x ← linter.instance_priority.test d, guard $ x = some "set priority below 1000" /- Test exception in `def_lemma` linter. -/ run_cmd do d ← get_decl `my_exists_intro, t ← linter.def_lemma.test d, guard $ t = none
5bad47fa2745cfe42a7bcc6abc279418f0892d2a
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Meta/KAbstract.lean
cff69caae19e656a49c0d7b1a533b2ab21c63a09
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,784
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Lean.Data.Occurrences import Init.Lean.HeadIndex import Init.Lean.Meta.ExprDefEq namespace Lean namespace Meta private partial def kabstractAux (occs : Occurrences) (p : Expr) (pHeadIdx : HeadIndex) (pNumArgs : Nat) : Expr → Nat → StateT Nat MetaM Expr | e, offset => let visitChildren : Unit → StateT Nat MetaM Expr := fun _ => match e with | Expr.app f a _ => do f ← kabstractAux f offset; a ← kabstractAux a offset; pure $ e.updateApp! f a | Expr.mdata _ b _ => do b ← kabstractAux b offset; pure $ e.updateMData! b | Expr.proj _ _ b _ => do b ← kabstractAux b offset; pure $ e.updateProj! b | Expr.letE _ t v b _ => do t ← kabstractAux t offset; v ← kabstractAux v offset; b ← kabstractAux b (offset+1); pure $ e.updateLet! t v b | Expr.lam _ d b _ => do d ← kabstractAux d offset; b ← kabstractAux b (offset+1); pure $ e.updateLambdaE! d b | Expr.forallE _ d b _ => do d ← kabstractAux d offset; b ← kabstractAux b (offset+1); pure $ e.updateForallE! d b | e => pure e; if e.hasLooseBVars then visitChildren () else if e.toHeadIndex == pHeadIdx && e.headNumArgs == pNumArgs then condM (liftM $ isDefEq e p) (do i ← get; set (i+1); if occs.contains i then pure (mkBVar offset) else visitChildren ()) (visitChildren ()) else visitChildren () def kabstract (e : Expr) (p : Expr) (occs : Occurrences := Occurrences.all) : MetaM Expr := (kabstractAux occs p p.toHeadIndex p.headNumArgs e 0).run' 1 end Meta end Lean
c0043c951580fb7eedddee064df33bd2aa28f076
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/euclidean_domain.lean
c8a290d8ffc0316705d4f698a0dc97cee660dd10
[ "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
3,644
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes -/ import algebra.gcd_monoid.basic import algebra.euclidean_domain.basic import ring_theory.ideal.basic import ring_theory.principal_ideal_domain /-! # Lemmas about Euclidean domains > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Various about Euclidean domains are proved; all of them seem to be true more generally for principal ideal domains, so these lemmas should probably be reproved in more generality and this file perhaps removed? ## Tags euclidean domain -/ noncomputable theory open_locale classical open euclidean_domain set ideal section gcd_monoid variables {R : Type*} [euclidean_domain R] [gcd_monoid R] {p q : R} lemma gcd_ne_zero_of_left (hp : p ≠ 0) : gcd_monoid.gcd p q ≠ 0 := λ h, hp $ eq_zero_of_zero_dvd (h ▸ gcd_dvd_left p q) lemma gcd_ne_zero_of_right (hp : q ≠ 0) : gcd_monoid.gcd p q ≠ 0 := λ h, hp $ eq_zero_of_zero_dvd (h ▸ gcd_dvd_right p q) lemma left_div_gcd_ne_zero {p q : R} (hp : p ≠ 0) : p / gcd_monoid.gcd p q ≠ 0 := begin obtain ⟨r, hr⟩ := gcd_monoid.gcd_dvd_left p q, obtain ⟨pq0, r0⟩ : gcd_monoid.gcd p q ≠ 0 ∧ r ≠ 0 := mul_ne_zero_iff.mp (hr ▸ hp), rw [hr, mul_comm, mul_div_cancel _ pq0] { occs := occurrences.pos [1] }, exact r0, end lemma right_div_gcd_ne_zero {p q : R} (hq : q ≠ 0) : q / gcd_monoid.gcd p q ≠ 0 := begin obtain ⟨r, hr⟩ := gcd_monoid.gcd_dvd_right p q, obtain ⟨pq0, r0⟩ : gcd_monoid.gcd p q ≠ 0 ∧ r ≠ 0 := mul_ne_zero_iff.mp (hr ▸ hq), rw [hr, mul_comm, mul_div_cancel _ pq0] { occs := occurrences.pos [1] }, exact r0, end lemma is_coprime_div_gcd_div_gcd (hq : q ≠ 0) : is_coprime (p / gcd_monoid.gcd p q) (q / gcd_monoid.gcd p q) := (gcd_is_unit_iff _ _).1 $ is_unit_gcd_of_eq_mul_gcd (euclidean_domain.mul_div_cancel' (gcd_ne_zero_of_right hq) $ gcd_dvd_left _ _).symm (euclidean_domain.mul_div_cancel' (gcd_ne_zero_of_right hq) $ gcd_dvd_right _ _).symm $ gcd_ne_zero_of_right hq end gcd_monoid namespace euclidean_domain /-- Create a `gcd_monoid` whose `gcd_monoid.gcd` matches `euclidean_domain.gcd`. -/ def gcd_monoid (R) [euclidean_domain R] : gcd_monoid R := { gcd := gcd, lcm := lcm, gcd_dvd_left := gcd_dvd_left, gcd_dvd_right := gcd_dvd_right, dvd_gcd := λ a b c, dvd_gcd, gcd_mul_lcm := λ a b, by rw euclidean_domain.gcd_mul_lcm, lcm_zero_left := lcm_zero_left, lcm_zero_right := lcm_zero_right } variables {α : Type*} [euclidean_domain α] [decidable_eq α] theorem span_gcd {α} [euclidean_domain α] (x y : α) : span ({gcd x y} : set α) = span ({x, y} : set α) := begin letI := euclidean_domain.gcd_monoid α, exact span_gcd x y, end theorem gcd_is_unit_iff {α} [euclidean_domain α] {x y : α} : is_unit (gcd x y) ↔ is_coprime x y := begin letI := euclidean_domain.gcd_monoid α, exact gcd_is_unit_iff x y, end -- this should be proved for UFDs surely? theorem is_coprime_of_dvd {α} [euclidean_domain α] {x y : α} (nonzero : ¬ (x = 0 ∧ y = 0)) (H : ∀ z ∈ nonunits α, z ≠ 0 → z ∣ x → ¬ z ∣ y) : is_coprime x y := begin letI := euclidean_domain.gcd_monoid α, exact is_coprime_of_dvd x y nonzero H, end -- this should be proved for UFDs surely? theorem dvd_or_coprime {α} [euclidean_domain α] (x y : α) (h : irreducible x) : x ∣ y ∨ is_coprime x y := begin letI := euclidean_domain.gcd_monoid α, exact dvd_or_coprime x y h, end end euclidean_domain
41db9034546bbab1a590789d2a118788e8d1c4d7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/number_theory/legendre_symbol/quadratic_char.lean
63f00b748e4c7f26c27791e45f8937e8c9682176
[ "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
18,635
lean
/- Copyright (c) 2022 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Stoll -/ import number_theory.legendre_symbol.zmod_char import field_theory.finite.basic import number_theory.legendre_symbol.gauss_sum /-! # Quadratic characters of finite fields This file defines the quadratic character on a finite field `F` and proves some basic statements about it. ## Tags quadratic character -/ /-! ### Definition of the quadratic character We define the quadratic character of a finite field `F` with values in ℤ. -/ section define /-- Define the quadratic character with values in ℤ on a monoid with zero `α`. It takes the value zero at zero; for non-zero argument `a : α`, it is `1` if `a` is a square, otherwise it is `-1`. This only deserves the name "character" when it is multiplicative, e.g., when `α` is a finite field. See `quadratic_char_fun_mul`. We will later define `quadratic_char` to be a multiplicative character of type `mul_char F ℤ`, when the domain is a finite field `F`. -/ def quadratic_char_fun (α : Type*) [monoid_with_zero α] [decidable_eq α] [decidable_pred (is_square : α → Prop)] (a : α) : ℤ := if a = 0 then 0 else if is_square a then 1 else -1 end define /-! ### Basic properties of the quadratic character We prove some properties of the quadratic character. We work with a finite field `F` here. The interesting case is when the characteristic of `F` is odd. -/ section quadratic_char open mul_char variables {F : Type*} [field F] [fintype F] [decidable_eq F] /-- Some basic API lemmas -/ lemma quadratic_char_fun_eq_zero_iff {a : F} : quadratic_char_fun F a = 0 ↔ a = 0 := begin simp only [quadratic_char_fun], by_cases ha : a = 0, { simp only [ha, eq_self_iff_true, if_true], }, { simp only [ha, if_false, iff_false], split_ifs; simp only [neg_eq_zero, one_ne_zero, not_false_iff], }, end @[simp] lemma quadratic_char_fun_zero : quadratic_char_fun F 0 = 0 := by simp only [quadratic_char_fun, eq_self_iff_true, if_true, id.def] @[simp] lemma quadratic_char_fun_one : quadratic_char_fun F 1 = 1 := by simp only [quadratic_char_fun, one_ne_zero, is_square_one, if_true, if_false, id.def] /-- If `ring_char F = 2`, then `quadratic_char_fun F` takes the value `1` on nonzero elements. -/ lemma quadratic_char_fun_eq_one_of_char_two (hF : ring_char F = 2) {a : F} (ha : a ≠ 0) : quadratic_char_fun F a = 1 := begin simp only [quadratic_char_fun, ha, if_false, ite_eq_left_iff], exact λ h, false.rec _ (h (finite_field.is_square_of_char_two hF a)) end /-- If `ring_char F` is odd, then `quadratic_char_fun F a` can be computed in terms of `a ^ (fintype.card F / 2)`. -/ lemma quadratic_char_fun_eq_pow_of_char_ne_two (hF : ring_char F ≠ 2) {a : F} (ha : a ≠ 0) : quadratic_char_fun F a = if a ^ (fintype.card F / 2) = 1 then 1 else -1 := begin simp only [quadratic_char_fun, ha, if_false], simp_rw finite_field.is_square_iff hF ha, end /-- The quadratic character is multiplicative. -/ lemma quadratic_char_fun_mul (a b : F) : quadratic_char_fun F (a * b) = quadratic_char_fun F a * quadratic_char_fun F b := begin by_cases ha : a = 0, { rw [ha, zero_mul, quadratic_char_fun_zero, zero_mul], }, -- now `a ≠ 0` by_cases hb : b = 0, { rw [hb, mul_zero, quadratic_char_fun_zero, mul_zero], }, -- now `a ≠ 0` and `b ≠ 0` have hab := mul_ne_zero ha hb, by_cases hF : ring_char F = 2, { -- case `ring_char F = 2` rw [quadratic_char_fun_eq_one_of_char_two hF ha, quadratic_char_fun_eq_one_of_char_two hF hb, quadratic_char_fun_eq_one_of_char_two hF hab, mul_one], }, { -- case of odd characteristic rw [quadratic_char_fun_eq_pow_of_char_ne_two hF ha, quadratic_char_fun_eq_pow_of_char_ne_two hF hb, quadratic_char_fun_eq_pow_of_char_ne_two hF hab, mul_pow], cases finite_field.pow_dichotomy hF hb with hb' hb', { simp only [hb', mul_one, eq_self_iff_true, if_true], }, { have h := ring.neg_one_ne_one_of_char_ne_two hF, -- `-1 ≠ 1` simp only [hb', h, mul_neg, mul_one, if_false, ite_mul, neg_mul], cases finite_field.pow_dichotomy hF ha with ha' ha'; simp only [ha', h, neg_neg, eq_self_iff_true, if_true, if_false], }, }, end variables (F) /-- The quadratic character as a multiplicative character. -/ @[simps] def quadratic_char : mul_char F ℤ := { to_fun := quadratic_char_fun F, map_one' := quadratic_char_fun_one, map_mul' := quadratic_char_fun_mul, map_nonunit' := λ a ha, by { rw of_not_not (mt ne.is_unit ha), exact quadratic_char_fun_zero, } } variables {F} /-- The value of the quadratic character on `a` is zero iff `a = 0`. -/ lemma quadratic_char_eq_zero_iff {a : F} : quadratic_char F a = 0 ↔ a = 0 := quadratic_char_fun_eq_zero_iff @[simp] lemma quadratic_char_zero : quadratic_char F 0 = 0 := by simp only [quadratic_char_apply, quadratic_char_fun_zero] /-- For nonzero `a : F`, `quadratic_char F a = 1 ↔ is_square a`. -/ lemma quadratic_char_one_iff_is_square {a : F} (ha : a ≠ 0) : quadratic_char F a = 1 ↔ is_square a := by simp only [quadratic_char_apply, quadratic_char_fun, ha, (dec_trivial : (-1 : ℤ) ≠ 1), if_false, ite_eq_left_iff, imp_false, not_not] /-- The quadratic character takes the value `1` on nonzero squares. -/ lemma quadratic_char_sq_one' {a : F} (ha : a ≠ 0) : quadratic_char F (a ^ 2) = 1 := by simp only [quadratic_char_fun, ha, pow_eq_zero_iff, nat.succ_pos', is_square_sq, if_true, if_false, quadratic_char_apply] /-- The square of the quadratic character on nonzero arguments is `1`. -/ lemma quadratic_char_sq_one {a : F} (ha : a ≠ 0) : (quadratic_char F a) ^ 2 = 1 := by rwa [pow_two, ← map_mul, ← pow_two, quadratic_char_sq_one'] /-- The quadratic character is `1` or `-1` on nonzero arguments. -/ lemma quadratic_char_dichotomy {a : F} (ha : a ≠ 0) : quadratic_char F a = 1 ∨ quadratic_char F a = -1 := sq_eq_one_iff.1 $ quadratic_char_sq_one ha /-- The quadratic character is `1` or `-1` on nonzero arguments. -/ lemma quadratic_char_eq_neg_one_iff_not_one {a : F} (ha : a ≠ 0) : quadratic_char F a = -1 ↔ ¬ quadratic_char F a = 1 := begin refine ⟨λ h, _, λ h₂, (or_iff_right h₂).mp (quadratic_char_dichotomy ha)⟩, rw h, norm_num, end /-- For `a : F`, `quadratic_char F a = -1 ↔ ¬ is_square a`. -/ lemma quadratic_char_neg_one_iff_not_is_square {a : F} : quadratic_char F a = -1 ↔ ¬ is_square a := begin by_cases ha : a = 0, { simp only [ha, is_square_zero, mul_char.map_zero, zero_eq_neg, one_ne_zero, not_true], }, { rw [quadratic_char_eq_neg_one_iff_not_one ha, quadratic_char_one_iff_is_square ha] }, end /-- If `F` has odd characteristic, then `quadratic_char F` takes the value `-1`. -/ lemma quadratic_char_exists_neg_one (hF : ring_char F ≠ 2) : ∃ a, quadratic_char F a = -1 := (finite_field.exists_nonsquare hF).imp $ λ b h₁, quadratic_char_neg_one_iff_not_is_square.mpr h₁ /-- If `ring_char F = 2`, then `quadratic_char F` takes the value `1` on nonzero elements. -/ lemma quadratic_char_eq_one_of_char_two (hF : ring_char F = 2) {a : F} (ha : a ≠ 0) : quadratic_char F a = 1 := quadratic_char_fun_eq_one_of_char_two hF ha /-- If `ring_char F` is odd, then `quadratic_char F a` can be computed in terms of `a ^ (fintype.card F / 2)`. -/ lemma quadratic_char_eq_pow_of_char_ne_two (hF : ring_char F ≠ 2) {a : F} (ha : a ≠ 0) : quadratic_char F a = if a ^ (fintype.card F / 2) = 1 then 1 else -1 := quadratic_char_fun_eq_pow_of_char_ne_two hF ha lemma quadratic_char_eq_pow_of_char_ne_two' (hF : ring_char F ≠ 2) (a : F) : (quadratic_char F a : F) = a ^ (fintype.card F / 2) := begin by_cases ha : a = 0, { have : 0 < fintype.card F / 2 := nat.div_pos fintype.one_lt_card two_pos, simp only [ha, zero_pow this, quadratic_char_apply, quadratic_char_zero, int.cast_zero], }, { rw [quadratic_char_eq_pow_of_char_ne_two hF ha], by_cases ha' : a ^ (fintype.card F / 2) = 1, { simp only [ha', eq_self_iff_true, if_true, int.cast_one], }, { have ha'' := or.resolve_left (finite_field.pow_dichotomy hF ha) ha', simp only [ha'', int.cast_ite, int.cast_one, int.cast_neg, ite_eq_right_iff], exact eq.symm, } } end variables (F) /-- The quadratic character is quadratic as a multiplicative character. -/ lemma quadratic_char_is_quadratic : (quadratic_char F).is_quadratic := begin intro a, by_cases ha : a = 0, { left, rw ha, exact quadratic_char_zero, }, { right, exact quadratic_char_dichotomy ha, }, end variables {F} /-- The quadratic character is nontrivial as a multiplicative character when the domain has odd characteristic. -/ lemma quadratic_char_is_nontrivial (hF : ring_char F ≠ 2) : (quadratic_char F).is_nontrivial := begin rcases quadratic_char_exists_neg_one hF with ⟨a, ha⟩, have hu : is_unit a := by { by_contra hf, rw map_nonunit _ hf at ha, norm_num at ha, }, refine ⟨hu.unit, (_ : quadratic_char F a ≠ 1)⟩, rw ha, norm_num, end /-- The number of solutions to `x^2 = a` is determined by the quadratic character. -/ lemma quadratic_char_card_sqrts (hF : ring_char F ≠ 2) (a : F) : ↑{x : F | x^2 = a}.to_finset.card = quadratic_char F a + 1 := begin -- we consider the cases `a = 0`, `a` is a nonzero square and `a` is a nonsquare in turn by_cases h₀ : a = 0, { simp only [h₀, pow_eq_zero_iff, nat.succ_pos', int.coe_nat_succ, int.coe_nat_zero, mul_char.map_zero, set.set_of_eq_eq_singleton, set.to_finset_card, set.card_singleton], }, { set s := {x : F | x^2 = a}.to_finset with hs, by_cases h : is_square a, { rw (quadratic_char_one_iff_is_square h₀).mpr h, rcases h with ⟨b, h⟩, rw [h, mul_self_eq_zero] at h₀, have h₁ : s = [b, -b].to_finset := by { ext x, simp only [finset.mem_filter, finset.mem_univ, true_and, list.to_finset_cons, list.to_finset_nil, insert_emptyc_eq, finset.mem_insert, finset.mem_singleton], rw ← pow_two at h, simp only [hs, set.mem_to_finset, set.mem_set_of_eq, h], split, { exact eq_or_eq_neg_of_sq_eq_sq _ _, }, { rintro (h₂ | h₂); rw h₂, simp only [neg_sq], }, }, norm_cast, rw [h₁, list.to_finset_cons, list.to_finset_cons, list.to_finset_nil], exact finset.card_doubleton (ne.symm (mt (ring.eq_self_iff_eq_zero_of_char_ne_two hF).mp h₀)), }, { rw quadratic_char_neg_one_iff_not_is_square.mpr h, simp only [int.coe_nat_eq_zero, finset.card_eq_zero, set.to_finset_card, fintype.card_of_finset, set.mem_set_of_eq, add_left_neg], ext x, simp only [iff_false, finset.mem_filter, finset.mem_univ, true_and, finset.not_mem_empty], rw is_square_iff_exists_sq at h, exact λ h', h ⟨_, h'.symm⟩, }, }, end open_locale big_operators /-- The sum over the values of the quadratic character is zero when the characteristic is odd. -/ lemma quadratic_char_sum_zero (hF : ring_char F ≠ 2) : ∑ (a : F), quadratic_char F a = 0 := is_nontrivial.sum_eq_zero (quadratic_char_is_nontrivial hF) end quadratic_char /-! ### Special values of the quadratic character We express `quadratic_char F (-1)` in terms of `χ₄`. -/ section special_values open zmod mul_char variables {F : Type} [field F] [fintype F] /-- The value of the quadratic character at `-1` -/ lemma quadratic_char_neg_one [decidable_eq F] (hF : ring_char F ≠ 2) : quadratic_char F (-1) = χ₄ (fintype.card F) := begin have h := quadratic_char_eq_pow_of_char_ne_two hF (neg_ne_zero.mpr one_ne_zero), rw [h, χ₄_eq_neg_one_pow (finite_field.odd_card_of_char_ne_two hF)], set n := fintype.card F / 2, cases (nat.even_or_odd n) with h₂ h₂, { simp only [even.neg_one_pow h₂, eq_self_iff_true, if_true], }, { simp only [odd.neg_one_pow h₂, ite_eq_right_iff], exact λ hf, false.rec (1 = -1) (ring.neg_one_ne_one_of_char_ne_two hF hf), }, end /-- `-1` is a square in `F` iff `#F` is not congruent to `3` mod `4`. -/ lemma finite_field.is_square_neg_one_iff : is_square (-1 : F) ↔ fintype.card F % 4 ≠ 3 := begin classical, -- suggested by the linter (instead of `[decidable_eq F]`) by_cases hF : ring_char F = 2, { simp only [finite_field.is_square_of_char_two hF, ne.def, true_iff], exact (λ hf, one_ne_zero $ (nat.odd_of_mod_four_eq_three hf).symm.trans $ finite_field.even_card_of_char_two hF) }, { have h₁ := finite_field.odd_card_of_char_ne_two hF, rw [← quadratic_char_one_iff_is_square (neg_ne_zero.mpr (@one_ne_zero F _ _)), quadratic_char_neg_one hF, χ₄_nat_eq_if_mod_four, h₁], simp only [nat.one_ne_zero, if_false, ite_eq_left_iff, ne.def, (dec_trivial : (-1 : ℤ) ≠ 1), imp_false, not_not], exact ⟨λ h, ne_of_eq_of_ne h (dec_trivial : 1 ≠ 3), or.resolve_right (nat.odd_mod_four_iff.mp h₁)⟩, }, end /-- The value of the quadratic character at `2` -/ lemma quadratic_char_two [decidable_eq F] (hF : ring_char F ≠ 2) : quadratic_char F 2 = χ₈ (fintype.card F) := is_quadratic.eq_of_eq_coe (quadratic_char_is_quadratic F) is_quadratic_χ₈ hF ((quadratic_char_eq_pow_of_char_ne_two' hF 2).trans (finite_field.two_pow_card hF)) /-- `2` is a square in `F` iff `#F` is not congruent to `3` or `5` mod `8`. -/ lemma finite_field.is_square_two_iff : is_square (2 : F) ↔ fintype.card F % 8 ≠ 3 ∧ fintype.card F % 8 ≠ 5 := begin classical, by_cases hF : ring_char F = 2, focus { have h := finite_field.even_card_of_char_two hF, simp only [finite_field.is_square_of_char_two hF, true_iff], }, rotate, focus { have h := finite_field.odd_card_of_char_ne_two hF, rw [← quadratic_char_one_iff_is_square (ring.two_ne_zero hF), quadratic_char_two hF, χ₈_nat_eq_if_mod_eight], simp only [h, nat.one_ne_zero, if_false, ite_eq_left_iff, ne.def, (dec_trivial : (-1 : ℤ) ≠ 1), imp_false, not_not], }, all_goals { rw [← nat.mod_mod_of_dvd _ (by norm_num : 2 ∣ 8)] at h, have h₁ := nat.mod_lt (fintype.card F) (dec_trivial : 0 < 8), revert h₁ h, generalize : fintype.card F % 8 = n, dec_trivial!, } end /-- The value of the quadratic character at `-2` -/ lemma quadratic_char_neg_two [decidable_eq F] (hF : ring_char F ≠ 2) : quadratic_char F (-2) = χ₈' (fintype.card F) := begin rw [(by norm_num : (-2 : F) = (-1) * 2), map_mul, χ₈'_eq_χ₄_mul_χ₈, quadratic_char_neg_one hF, quadratic_char_two hF, @cast_nat_cast _ (zmod 4) _ _ _ (by norm_num : 4 ∣ 8)], end /-- `-2` is a square in `F` iff `#F` is not congruent to `5` or `7` mod `8`. -/ lemma finite_field.is_square_neg_two_iff : is_square (-2 : F) ↔ fintype.card F % 8 ≠ 5 ∧ fintype.card F % 8 ≠ 7 := begin classical, by_cases hF : ring_char F = 2, focus { have h := finite_field.even_card_of_char_two hF, simp only [finite_field.is_square_of_char_two hF, true_iff], }, rotate, focus { have h := finite_field.odd_card_of_char_ne_two hF, rw [← quadratic_char_one_iff_is_square (neg_ne_zero.mpr (ring.two_ne_zero hF)), quadratic_char_neg_two hF, χ₈'_nat_eq_if_mod_eight], simp only [h, nat.one_ne_zero, if_false, ite_eq_left_iff, ne.def, (dec_trivial : (-1 : ℤ) ≠ 1), imp_false, not_not], }, all_goals { rw [← nat.mod_mod_of_dvd _ (by norm_num : 2 ∣ 8)] at h, have h₁ := nat.mod_lt (fintype.card F) (dec_trivial : 0 < 8), revert h₁ h, generalize : fintype.card F % 8 = n, dec_trivial! } end /-- The relation between the values of the quadratic character of one field `F` at the cardinality of another field `F'` and of the quadratic character of `F'` at the cardinality of `F`. -/ lemma quadratic_char_card_card [decidable_eq F] (hF : ring_char F ≠ 2) {F' : Type} [field F'] [fintype F'] [decidable_eq F'] (hF' : ring_char F' ≠ 2) (h : ring_char F' ≠ ring_char F) : quadratic_char F (fintype.card F') = quadratic_char F' (quadratic_char F (-1) * fintype.card F) := begin let χ := (quadratic_char F).ring_hom_comp (algebra_map ℤ F'), have hχ₁ : χ.is_nontrivial, { obtain ⟨a, ha⟩ := quadratic_char_exists_neg_one hF, have hu : is_unit a, { contrapose ha, exact ne_of_eq_of_ne (map_nonunit (quadratic_char F) ha) (mt zero_eq_neg.mp one_ne_zero), }, use hu.unit, simp only [is_unit.unit_spec, ring_hom_comp_apply, eq_int_cast, ne.def, ha], rw [int.cast_neg, int.cast_one], exact ring.neg_one_ne_one_of_char_ne_two hF', }, have hχ₂ : χ.is_quadratic := is_quadratic.comp (quadratic_char_is_quadratic F) _, have h := char.card_pow_card hχ₁ hχ₂ h hF', rw [← quadratic_char_eq_pow_of_char_ne_two' hF'] at h, exact (is_quadratic.eq_of_eq_coe (quadratic_char_is_quadratic F') (quadratic_char_is_quadratic F) hF' h).symm, end /-- The value of the quadratic character at an odd prime `p` different from `ring_char F`. -/ lemma quadratic_char_odd_prime [decidable_eq F] (hF : ring_char F ≠ 2) {p : ℕ} [fact p.prime] (hp₁ : p ≠ 2) (hp₂ : ring_char F ≠ p) : quadratic_char F p = quadratic_char (zmod p) (χ₄ (fintype.card F) * fintype.card F) := begin rw [← quadratic_char_neg_one hF], have h := quadratic_char_card_card hF (ne_of_eq_of_ne (ring_char_zmod_n p) hp₁) (ne_of_eq_of_ne (ring_char_zmod_n p) hp₂.symm), rwa [card p] at h, end /-- An odd prime `p` is a square in `F` iff the quadratic character of `zmod p` does not take the value `-1` on `χ₄(#F) * #F`. -/ lemma finite_field.is_square_odd_prime_iff (hF : ring_char F ≠ 2) {p : ℕ} [fact p.prime] (hp : p ≠ 2) : is_square (p : F) ↔ quadratic_char (zmod p) (χ₄ (fintype.card F) * fintype.card F) ≠ -1 := begin classical, by_cases hFp : ring_char F = p, { rw [show (p : F) = 0, by { rw ← hFp, exact ring_char.nat.cast_ring_char }], simp only [is_square_zero, ne.def, true_iff, map_mul], obtain ⟨n, _, hc⟩ := finite_field.card F (ring_char F), have hchar : ring_char F = ring_char (zmod p) := by {rw hFp, exact (ring_char_zmod_n p).symm}, conv {congr, to_lhs, congr, skip, rw [hc, nat.cast_pow, map_pow, hchar, map_ring_char], }, simp only [zero_pow n.pos, mul_zero, zero_eq_neg, one_ne_zero, not_false_iff], }, { rw [← iff.not_left (@quadratic_char_neg_one_iff_not_is_square F _ _ _ _), quadratic_char_odd_prime hF hp], exact hFp, }, end end special_values
2cadd1dcfcff9f594809d04cc85ac950dad5be3b
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/Mbar/basic.lean
9d33ef9aa3460db57659a2b4ea08981c781c4e78
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,459
lean
import data.real.basic import algebra.big_operators.basic import topology.algebra.infinite_sum import data.finset.basic import data.equiv.basic import analysis.normed_space.basic import analysis.specific_limits import data.equiv.basic import Mbar.bounded import pseudo_normed_group.basic import for_mathlib.tsum import for_mathlib.add_monoid_hom /-! ## $\overline{\mathcal{M}}_{r'}(S)$ This file contains a definition of ℳ-barᵣ'(S) as defined on p57 of Analytic.pdf . ## Implementation issues We model Tℤ[[T]] as functions ℕ → ℤ which vanish at 0. -/ universe u noncomputable theory open_locale big_operators nnreal -- move this lemma int.norm_def (n : ℤ) : ∥n∥ = abs n := rfl -- move this @[simp] lemma nnreal.coe_nat_abs (n : ℤ) : ↑n.nat_abs = nnnorm n := nnreal.eq $ calc ((n.nat_abs : ℝ≥0) : ℝ) = ↑(n.nat_abs : ℤ) : by simp only [int.cast_coe_nat, nnreal.coe_nat_cast] ... = abs n : by simp only [← int.abs_eq_nat_abs, int.cast_abs] ... = ∥n∥ : rfl section defs set_option old_structure_cmd true /-- `Mbar r' S` is the set of power series `F_s = ∑ a_{n,s}T^n ∈ Tℤ[[T]]` such that `∑_{n,s} |a_{n,s}|r'^n` converges -/ structure Mbar (r' : ℝ≥0) (S : Type u) [fintype S] := (to_fun : S → ℕ → ℤ) (coeff_zero' : ∀ s, to_fun s 0 = 0) (summable' : ∀ s, summable (λ n, (↑(to_fun s n).nat_abs * r' ^ n))) end defs variables {r' : ℝ≥0} {S : Type u} [fintype S] namespace Mbar instance has_coe_to_fun : has_coe_to_fun (Mbar r' S) := ⟨_, Mbar.to_fun⟩ @[simp] lemma coe_mk (x h₁ h₂) : ((⟨x, h₁, h₂⟩ : Mbar r' S) : S → ℕ → ℤ) = x := rfl @[simp] protected lemma coeff_zero (x : Mbar r' S) (s : S) : x s 0 = 0 := x.coeff_zero' s protected lemma summable (x : Mbar r' S) (s : S) : summable (λ n, (↑(x s n).nat_abs * r' ^ n)) := x.summable' s lemma summable_coe_real (x : Mbar r' S) (s : S) : summable (λ n, ∥x s n∥ * r' ^ n) := by simpa only [← nnreal.summable_coe, nnreal.coe_nat_cast, nnreal.coe_nat_abs, nnreal.coe_mul, nnreal.coe_pow] using x.summable s @[ext] lemma ext (x y : Mbar r' S) (h : ⇑x = y) : x = y := by { cases x, cases y, congr, exact h } lemma ext_iff (x y : Mbar r' S) : x = y ↔ (x : S → ℕ → ℤ) = y := ⟨congr_arg _, ext x y⟩ /-- The zero element of `Mbar r' S`, defined as the constant 0-function. -/ def zero : Mbar r' S := { to_fun := 0, coeff_zero' := λ s, rfl, summable' := λ s, by simpa using summable_zero } /-- Addition in `Mbar r' S`, defined as pointwise addition. -/ def add (F : Mbar r' S) (G : Mbar r' S) : Mbar r' S := { to_fun := F + G, coeff_zero' := λ s, by simp [F.coeff_zero s, G.coeff_zero s], summable' := begin intro s, apply nnreal.summable_of_le _ ((F.summable s).add (G.summable s)), intro n, rw [← add_mul, ← nat.cast_add], apply mul_le_mul_right', rw nat.cast_le, exact int.nat_abs_add_le _ _ end } /-- Subtraction in `Mbar r' S`, defined as pointwise subtraction. -/ def sub (F : Mbar r' S) (G : Mbar r' S) : Mbar r' S := { to_fun := F - G, coeff_zero' := λ s, by simp [F.coeff_zero s, G.coeff_zero s], summable' := begin intro s, apply nnreal.summable_of_le _ ((F.summable s).add (G.summable s)), intro n, rw [← add_mul, ← nat.cast_add], apply mul_le_mul_right', rw [nat.cast_le, sub_eq_add_neg, ← int.nat_abs_neg (G s n)], -- there should be an `int.nat_abs_sub_le` exact int.nat_abs_add_le _ _ end } /-- Negation in `Mbar r' S`, defined as pointwise negation. -/ def neg (F : Mbar r' S) : Mbar r' S := { to_fun := -F, coeff_zero' := λ s, by simp [F.coeff_zero s], summable' := begin intro s, convert F.summable s using 1, ext n, simp only [pi.neg_apply, int.nat_abs_neg] end } instance : has_zero (Mbar r' S) := ⟨zero⟩ instance : has_add (Mbar r' S) := ⟨add⟩ instance : has_sub (Mbar r' S) := ⟨sub⟩ instance : has_neg (Mbar r' S) := ⟨neg⟩ @[simp] lemma coe_zero : ⇑(0 : Mbar r' S) = 0 := rfl @[simp] lemma coe_add (F G : Mbar r' S) : ⇑(F + G : Mbar r' S) = F + G := rfl @[simp] lemma coe_sub (F G : Mbar r' S) : ⇑(F - G : Mbar r' S) = F - G := rfl @[simp] lemma coe_neg (F : Mbar r' S) : ⇑(-F : Mbar r' S) = -F := rfl instance : add_comm_group (Mbar r' S) := { zero := 0, add := (+), sub := has_sub.sub, neg := has_neg.neg, zero_add := by { intros, ext, simp only [coe_zero, zero_add, coe_add] }, add_zero := by { intros, ext, simp only [coe_zero, add_zero, coe_add] }, add_assoc := by { intros, ext, simp only [add_assoc, coe_add] }, add_left_neg := by { intros, ext, simp only [coe_add, coe_neg, coe_zero, add_left_neg] }, add_comm := by { intros, ext, simp only [coe_add, add_comm] }, sub_eq_add_neg := by { intros, ext, simp only [coe_sub, coe_add, coe_neg, sub_eq_add_neg] } } /-- The norm of `F : Mbar r' S` as nonnegative real number. It is defined as `∑ s, ∑' n, (↑(F s n).nat_abs * r' ^ n)`. -/ protected def nnnorm (F : Mbar r' S) : ℝ≥0 := ∑ s, ∑' n, (↑(F s n).nat_abs * r' ^ n) notation `∥`F`∥₊` := Mbar.nnnorm F lemma nnnorm_def (F : Mbar r' S) : ∥F∥₊ = ∑ s, ∑' n, (↑(F s n).nat_abs * r' ^ n) := rfl @[simp] lemma nnnorm_zero : ∥(0 : Mbar r' S)∥₊ = 0 := by simp only [nnnorm_def, Mbar.coe_zero, tsum_zero, nat.cast_zero, zero_mul, pi.zero_apply, zero_le', finset.sum_const_zero, int.nat_abs_zero] @[simp] lemma nnnorm_neg (F : Mbar r' S) : ∥-F∥₊ = ∥F∥₊ := by simp only [nnnorm_def, Mbar.coe_neg, pi.neg_apply, int.nat_abs_neg, int.nat_abs, coe_neg, set.mem_set_of_eq] lemma nnnorm_add_le (F₁ F₂ : Mbar r' S) : ∥F₁ + F₂∥₊ ≤ ∥F₁∥₊ + ∥F₂∥₊ := begin simp only [nnnorm_def, ← finset.sum_add_distrib], refine finset.sum_le_sum _, rintro s -, rw ← tsum_add (F₁.summable s) (F₂.summable s), refine tsum_le_tsum _ ((F₁ + F₂).summable _) ((F₁.summable s).add (F₂.summable s)), intro n, dsimp, rw [← add_mul, ← nat.cast_add], apply mul_le_mul_right', rw nat.cast_le, exact int.nat_abs_add_le _ _ end lemma nnnorm_sum_le {ι : Type*} (s : finset ι) (F : ι → Mbar r' S) : ∥∑ i in s, F i∥₊ ≤ ∑ i in s, ∥F i∥₊ := begin classical, apply finset.induction_on s; clear s, { simp only [finset.sum_empty, nnnorm_zero] }, intros i s his IH, simp only [finset.sum_insert his], exact (nnnorm_add_le _ _).trans (add_le_add le_rfl IH) end instance pseudo_normed_group : pseudo_normed_group (Mbar r' S) := { filtration := λ c, {F | ∥F∥₊ ≤ c}, filtration_mono := λ c₁ c₂ h F hF, le_trans (by exact hF) h, -- `by exact`, why?? zero_mem_filtration := λ c, by { dsimp, rw nnnorm_zero, apply zero_le' }, neg_mem_filtration := λ c F hF, by { dsimp, rwa nnnorm_neg }, add_mem_filtration := λ c₁ c₂ F₁ F₂ hF₁ hF₂, by exact le_trans (nnnorm_add_le _ _) (add_le_add hF₁ hF₂) } lemma mem_filtration_iff (x : Mbar r' S) (c : ℝ≥0) : x ∈ pseudo_normed_group.filtration (Mbar r' S) c ↔ ∥x∥₊ ≤ c := iff.rfl /-- The coercion from `Mbar r' S` to functions `S → ℕ → ℤ`. This is an additive group homomorphism. -/ def coe_hom : Mbar r' S →+ (S → ℕ → ℤ) := add_monoid_hom.mk' coe_fn $ coe_add @[simp] lemma coe_sum {ι : Type*} (s : finset ι) (F : ι → Mbar r' S) : ⇑(∑ i in s, F i) = ∑ i in s, (F i) := show coe_hom (∑ i in s, F i) = ∑ i in s, coe_hom (F i), from add_monoid_hom.map_sum _ _ _ @[simp] lemma coe_gsmul (n : ℤ) (F : Mbar r' S) : ⇑(n •ℤ F) = n •ℤ F := show coe_hom (n •ℤ F) = n •ℤ coe_hom F, from add_monoid_hom.map_gsmul _ _ _ @[simp] lemma coe_smul (n : ℤ) (F : Mbar r' S) : ⇑(n • F) = n • F := begin rw [← gsmul_eq_smul, coe_gsmul], ext, simp only [gsmul_int_int, function.gsmul_apply, pi.smul_apply, ← gsmul_eq_smul] end @[simp] lemma coe_nsmul (n : ℕ) (F : Mbar r' S) : ⇑(n •ℕ F) = n •ℕ F := coe_gsmul n F @[simp] lemma nnnorm_smul (N : ℤ) (F : Mbar r' S) : ∥N • F∥₊ = nnnorm N * ∥F∥₊ := begin simp only [nnnorm_def, finset.mul_sum], apply fintype.sum_congr, intro s, apply nnreal.eq, simp only [nnreal.coe_mul, nnreal.coe_tsum, ← tsum_mul_left, ← mul_assoc, nnreal.coe_nat_abs, coe_nnnorm, coe_smul, pi.smul_apply, int.norm_def, ← abs_mul, ← int.cast_mul], apply tsum_congr, intro n, simp only [← smul_eq_mul], congr /- evil congr, should be simp -/ end section Tinv /-- The action of T⁻¹. -/ def Tinv_aux {R : Type*} [has_zero R] : (ℕ → R) → ℕ → R := λ F n, if n = 0 then 0 else F (n + 1) @[simp] lemma Tinv_aux_zero {R : Type*} [has_zero R] (f : ℕ → R) : Tinv_aux f 0 = 0 := rfl @[simp] lemma Tinv_aux_ne_zero {R : Type*} [has_zero R] (f : ℕ → R) (i : ℕ) (hi : i ≠ 0) : Tinv_aux f i = f (i + 1) := if_neg hi @[simp] lemma Tinv_aux_succ {R : Type*} [has_zero R] (f : ℕ → R) (i : ℕ) : Tinv_aux f (i + 1) = f (i + 2) := if_neg (nat.succ_ne_zero i) lemma Tinv_aux_summable [h0r : fact (0 < r')] (F : Mbar r' S) (s : S) : summable (λ n, (↑(Tinv_aux (F s) n).nat_abs * r' ^ n)) := begin have : ∀ n:ℕ, r' ^ n = r' ^ n * r' * r'⁻¹, { intro, rw [mul_inv_cancel_right' (ne_of_gt h0r)] }, conv { congr, funext, rw [this, ← mul_assoc] }, apply summable.mul_right, refine nnreal.summable_of_le _ (nnreal.summable_nat_add _ (F.summable s) 1), rintro ⟨i⟩, { simp only [nat.cast_zero, zero_mul, Tinv_aux_zero, zero_le', int.nat_abs_zero] }, { simp only [Tinv_aux_succ, pow_add, mul_assoc, pow_one] } end /-- The `T⁻¹` action on `Mbar r' S`. This is defined, essentially, as a shift in `ℕ` (accounting for the restriction at 0). This is an additive group homomorphism. -/ def Tinv {r : ℝ≥0} {S : Type u} [fintype S] [h0r : fact (0 < r)] : Mbar r S →+ Mbar r S := add_monoid_hom.mk' (λ F, { to_fun := λ s, Tinv_aux (F s), coeff_zero' := λ s, rfl, summable' := Tinv_aux_summable F }) begin intros F G, ext s (_|n), { simp only [add_zero, pi.add_apply, Mbar.coeff_zero] }, { simp only [coe_mk, pi.add_apply, Tinv_aux_succ, coe_add] } end @[simp] lemma Tinv_zero [fact (0 < r')] (F : Mbar r' S) (s : S) : Tinv F s 0 = 0 := rfl @[simp] lemma Tinv_ne_zero [fact (0 < r')] (F : Mbar r' S) (s : S) (i : ℕ) (hi : i ≠ 0) : Tinv F s i = F s (i + 1) := if_neg hi @[simp] lemma Tinv_succ [fact (0 < r')] (F : Mbar r' S) (s : S) (i : ℕ) : Tinv F s (i + 1) = F s (i + 2) := Tinv_aux_succ (F s) i open pseudo_normed_group lemma Tinv_mem_filtration [h0r : fact (0 < r')] : Tinv ∈ filtration (Mbar r' S →+ Mbar r' S) (r'⁻¹) := begin intros c F hF, simp only [Tinv, add_monoid_hom.coe_mk'], change _ ≤ _ at hF, rw mul_comm, apply le_mul_inv_of_mul_le (ne_of_gt h0r), rw [nnnorm_def, finset.sum_mul], apply le_trans _ hF, apply finset.sum_le_sum, rintro s -, rw ← nnreal.tsum_mul_right, conv_rhs { rw [← sum_add_tsum_nat_add' 1 (F.summable s)] }, refine le_add_of_nonneg_of_le zero_le' _, apply tsum_le_tsum, { rintro (_|i), { simp only [nat.cast_zero, zero_mul, Mbar.coeff_zero, int.nat_abs_zero], exact zero_le' }, { simp only [Tinv_aux_succ, mul_assoc, coe_mk, pow_succ'] } }, { exact (Tinv_aux_summable F s).mul_right _ }, { exact (nnreal.summable_nat_add_iff 1).mpr (F.summable s) } end end Tinv end Mbar #lint- only unused_arguments def_lemma doc_blame
4d21612c35b8790194e4578c10be1c69c7a1cf48
367134ba5a65885e863bdc4507601606690974c1
/src/data/equiv/nat.lean
14b49b9f4a6678833db25b6caf38e1d4aae11d07
[ "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
1,980
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Additional facts about equiv and encodable using the pairing function on nat. -/ import data.nat.pairing import data.pnat.basic open nat namespace equiv /-- An equivalence between `ℕ × ℕ` and `ℕ`, using the `mkpair` and `unpair` functions in `data.nat.pairing`. -/ @[simp] def nat_prod_nat_equiv_nat : ℕ × ℕ ≃ ℕ := ⟨λ p, nat.mkpair p.1 p.2, nat.unpair, λ p, begin cases p, apply nat.unpair_mkpair end, nat.mkpair_unpair⟩ /-- An equivalence between `bool × ℕ` and `ℕ`, by mapping `(tt, x)` to `2 * x + 1` and `(ff, x)` to `2 * x`. -/ @[simp] def bool_prod_nat_equiv_nat : bool × ℕ ≃ ℕ := ⟨λ ⟨b, n⟩, bit b n, bodd_div2, λ ⟨b, n⟩, by simp [bool_prod_nat_equiv_nat._match_1, bodd_bit, div2_bit], λ n, by simp [bool_prod_nat_equiv_nat._match_1, bit_decomp]⟩ /-- An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(sum.inl x)` to `2 * x` and `(sum.inr x)` to `2 * x + 1`. -/ @[simp] def nat_sum_nat_equiv_nat : ℕ ⊕ ℕ ≃ ℕ := (bool_prod_equiv_sum ℕ).symm.trans bool_prod_nat_equiv_nat /-- An equivalence between `ℤ` and `ℕ`, through `ℤ ≃ ℕ ⊕ ℕ` and `ℕ ⊕ ℕ ≃ ℕ`. -/ def int_equiv_nat : ℤ ≃ ℕ := int_equiv_nat_sum_nat.trans nat_sum_nat_equiv_nat /-- An equivalence between `α × α` and `α`, given that there is an equivalence between `α` and `ℕ`. -/ def prod_equiv_of_equiv_nat {α : Sort*} (e : α ≃ ℕ) : α × α ≃ α := calc α × α ≃ ℕ × ℕ : prod_congr e e ... ≃ ℕ : nat_prod_nat_equiv_nat ... ≃ α : e.symm /-- An equivalence between `ℕ+` and `ℕ`, by mapping `x` in `ℕ+` to `x - 1` in `ℕ`. -/ def pnat_equiv_nat : ℕ+ ≃ ℕ := ⟨λ n, pred n.1, succ_pnat, λ ⟨n, h⟩, by { cases n, cases h, simp [succ_pnat, h] }, λ n, by simp [succ_pnat] ⟩ end equiv
720827fb6b877f4bf67b1086e034c17f4d559ac0
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/eval/time_expr_current.lean
78f34381d470accbcde85407073cf27cb5b8a183
[]
no_license
kevinsullivan/lang
d3e526ba363dc1ddf5ff1c2f36607d7f891806a7
e9d869bff94fb13ad9262222a6f3c4aafba82d5e
refs/heads/master
1,687,840,064,795
1,628,047,969,000
1,628,047,969,000
282,210,749
0
1
null
1,608,153,830,000
1,595,592,637,000
Lean
UTF-8
Lean
false
false
1,738
lean
import ..expressions.time_expr_current open lang.time namespace lang.time universes u variables {K : Type u} [field K] [inhabited K] def env.init_env (K : Type u) [field K] [inhabited K] : env := ⟨ (λf: fm K TIME, λsp, λv, ⟨mk_vectr sp 1⟩), (λf: fm K TIME, λsp, λv, ⟨mk_point sp 0⟩), (λf: fm K TIME, λsp1, λf2, λsp2, (λv, sp1.time_tr sp2)), (λv, time_std_frame K), (λf, (λv, mk_space K f)) ⟩ def eval.init_eval (K : Type u) [field K] [inhabited K] : eval := ⟨ (λf: fm K TIME, λsp, λenv_,λexpr_, ⟨mk_vectr sp 1⟩), (λf: fm K TIME, λsp, λenv_,λexpr_, ⟨mk_point sp 0⟩), (λf: fm K TIME, λsp1, λf2, λsp2, (λenv_,λexpr_, sp1.time_tr sp2 : transform_eval sp1 sp2)), (λenv_, λexpr_, time_std_frame K), (λf, λenv_, λexpr_, mk_space K f) ⟩ def static_env := env.init_env def static_eval := eval.init_eval def time_frame_expr.value (expr_ : time_frame_expr K) : fm K TIME := (static_eval K).frame (static_env K).frame expr_ def time_space_expr.value {f : fm K TIME} (expr_ : time_space_expr f) : spc K f := ((static_eval K).space f) ((static_env K).space f) expr_ def time_expr.value {f : fm K TIME} {sp : spc K f} (expr_ : time_expr sp) : time sp := ((static_eval K).time sp) ((static_env K).time sp) expr_ def duration_expr.value {f : fm K TIME} {sp : spc K f} (expr_ : duration_expr sp) : duration sp := ((static_eval K).duration sp) ((static_env K).duration sp) expr_ def transform_expr.value {f1 : fm K TIME} {sp1 : spc K f1} {f2 : fm K TIME} {sp2 : spc K f2} (expr_ : transform_expr sp1 sp2) : time_transform sp1 sp2 := ((static_eval K).transform sp1 sp2) ((static_env K).transform sp1 sp2) expr_ end lang.time
455024c5caa9da3561f3fdbb327f181ab28d5af5
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/logic/cast.lean
a08fbaf862f7f0e57d27f914f0da40b31c3d3788
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
5,059
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Casts and heterogeneous equality. See also init.datatypes and init.logic. -/ import logic.eq logic.quantifiers namespace heq universe variable u variables {A B C : Type.{u}} {a a' : A} {b b' : B} {c : C} theorem drec_on {C : Π {B : Type} (b : B), a == b → Type} (H₁ : a == b) (H₂ : C a (refl a)) : C b H₁ := heq.rec (λ H₁ : a == a, show C a H₁, from H₂) H₁ H₁ theorem to_cast_eq (H : a == b) : cast (type_eq_of_heq H) a = b := drec_on H (cast_eq _ _) end heq section universe variables u v variables {A A' B C : Type.{u}} {P P' : A → Type.{v}} {a a' : A} {b : B} theorem hcongr_fun {f : Π x, P x} {f' : Π x, P' x} (a : A) (H₁ : f == f') (H₂ : P = P') : f a == f' a := sorry /- begin cases H₂, cases H₁, reflexivity end -/ theorem hcongr {P' : A' → Type} {f : Π a, P a} {f' : Π a', P' a'} {a : A} {a' : A'} (Hf : f == f') (HP : P == P') (Ha : a == a') : f a == f' a' := sorry /- begin cases Ha, cases HP, cases Hf, reflexivity end -/ theorem hcongr_arg (f : Πx, P x) {a b : A} (H : a = b) : f a == f b := H ▸ (heq.refl (f a)) end section variables {A : Type} {B : A → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} variables {a a' : A} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} theorem hcongr_arg2 (f : Πa b, C a b) (Ha : a = a') (Hb : b == b') : f a b == f a' b' := hcongr (hcongr_arg f Ha) (hcongr_arg C Ha) Hb theorem hcongr_arg3 (f : Πa b c, D a b c) (Ha : a = a') (Hb : b == b') (Hc : c == c') : f a b c == f a' b' c' := hcongr (hcongr_arg2 f Ha Hb) (hcongr_arg2 D Ha Hb) Hc end section universe variables u v variables {A A' B C : Type.{u}} {P P' : A → Type.{v}} {a a' : A} {b : B} -- should H₁ be explicit (useful in e.g. hproof_irrel) theorem eq_rec_to_heq {H₁ : a = a'} {p : P a} {p' : P a'} (H₂ : eq.rec_on H₁ p = p') : p == p' := sorry -- by subst H₁; subst H₂ theorem cast_to_heq {H₁ : A = B} (H₂ : cast H₁ a = b) : a == b := eq_rec_to_heq H₂ theorem hproof_irrel {a b : Prop} (H : a = b) (H₁ : a) (H₂ : b) : H₁ == H₂ := eq_rec_to_heq (proof_irrel (cast H H₁) H₂) --TODO: generalize to eq.rec. This is a special case of rec_on_comp in eq.lean theorem cast_trans (Hab : A = B) (Hbc : B = C) (a : A) : cast Hbc (cast Hab a) = cast (eq.trans Hab Hbc) a := sorry -- by subst Hab theorem pi_eq (H : P = P') : (Π x, P x) = (Π x, P' x) := sorry -- by subst H theorem rec_on_app (H : P = P') (f : Π x, P x) (a : A) : eq.rec_on H f a == f a := sorry -- by subst H theorem rec_on_pull (H : P = P') (f : Π x, P x) (a : A) : eq.rec_on H f a = eq.rec_on (congr_fun H a) (f a) := eq_of_heq (calc eq.rec_on H f a == f a : rec_on_app H f a ... == eq.rec_on (congr_fun H a) (f a) : heq.symm (eq_rec_heq (congr_fun H a) (f a))) theorem cast_app (H : P = P') (f : Π x, P x) (a : A) : cast (pi_eq H) f a == f a := sorry -- by subst H end -- function extensionality wrt heterogeneous equality theorem hfunext {A : Type} {B : A → Type} {B' : A → Type} {f : Π x, B x} {g : Π x, B' x} (H : ∀ a, f a == g a) : f == g := cast_to_heq (funext (λ a, eq_of_heq (heq.trans (cast_app (funext (λ x, type_eq_of_heq (H x))) f a) (H a)))) section variables {A : Type} {B : A → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {E : Πa b c, D a b c → Type} {F : Type} variables {a a' : A} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} {d : D a b c} {d' : D a' b' c'} theorem hcongr_arg4 (f : Πa b c d, E a b c d) (Ha : a = a') (Hb : b == b') (Hc : c == c') (Hd : d == d') : f a b c d == f a' b' c' d' := hcongr (hcongr_arg3 f Ha Hb Hc) (hcongr_arg3 E Ha Hb Hc) Hd theorem dcongr_arg2 (f : Πa, B a → F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') : f a b = f a' b' := eq_of_heq (hcongr_arg2 f Ha (eq_rec_to_heq Hb)) theorem dcongr_arg3 (f : Πa b, C a b → F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') (Hc : cast (dcongr_arg2 C Ha Hb) c = c') : f a b c = f a' b' c' := eq_of_heq (hcongr_arg3 f Ha (eq_rec_to_heq Hb) (eq_rec_to_heq Hc)) theorem dcongr_arg4 (f : Πa b c, D a b c → F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') (Hc : cast (dcongr_arg2 C Ha Hb) c = c') (Hd : cast (dcongr_arg3 D Ha Hb Hc) d = d') : f a b c d = f a' b' c' d' := eq_of_heq (hcongr_arg4 f Ha (eq_rec_to_heq Hb) (eq_rec_to_heq Hc) (eq_rec_to_heq Hd)) -- mixed versions (we want them for example if C a' b' is a subsingleton, like a proposition. -- Then proving eq is easier than proving heq) theorem hdcongr_arg3 (f : Πa b, C a b → F) (Ha : a = a') (Hb : b == b') (Hc : cast (eq_of_heq (hcongr_arg2 C Ha Hb)) c = c') : f a b c = f a' b' c' := eq_of_heq (hcongr_arg3 f Ha Hb (eq_rec_to_heq Hc)) end
be29b74258611622127b4781910efcbb503ee7a6
d642a6b1261b2cbe691e53561ac777b924751b63
/src/algebra/group_power.lean
06485ebf569343383cbc181b960de7d920d4e234
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
25,719
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import algebra.group import data.int.basic data.list.basic universes u v variable {α : Type u} /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [monoid α] (a : α) : ℕ → α | 0 := 1 | (n+1) := a * monoid.pow n def add_monoid.smul [add_monoid α] (n : ℕ) (a : α) : α := @monoid.pow (multiplicative α) _ a n precedence `•`:70 localized "infix ` • ` := add_monoid.smul" in add_monoid @[priority 5] instance monoid.has_pow [monoid α] : has_pow α ℕ := ⟨monoid.pow⟩ /- monoid -/ section monoid variables [monoid α] {β : Type u} [add_monoid β] @[simp] theorem pow_zero (a : α) : a^0 = 1 := rfl @[simp] theorem add_monoid.zero_smul (a : β) : 0 • a = 0 := rfl theorem pow_succ (a : α) (n : ℕ) : a^(n+1) = a * a^n := rfl theorem succ_smul (a : β) (n : ℕ) : (n+1)•a = a + n•a := rfl @[simp] theorem pow_one (a : α) : a^1 = a := mul_one _ @[simp] theorem add_monoid.one_smul (a : β) : 1•a = a := add_zero _ theorem pow_mul_comm' (a : α) (n : ℕ) : a^n * a = a * a^n := by induction n with n ih; [rw [pow_zero, one_mul, mul_one], rw [pow_succ, mul_assoc, ih]] theorem smul_add_comm' : ∀ (a : β) (n : ℕ), n•a + a = a + n•a := @pow_mul_comm' (multiplicative β) _ theorem pow_succ' (a : α) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_smul' (a : β) (n : ℕ) : (n+1)•a = n•a + a := by rw [succ_smul, smul_add_comm'] theorem pow_two (a : α) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_smul (a : β) : 2•a = a + a := show a+(a+0)=a+a, by rw add_zero theorem pow_add (a : α) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl theorem add_monoid.add_smul : ∀ (a : β) (m n : ℕ), (m + n)•a = m•a + n•a := @pow_add (multiplicative β) _ @[simp] theorem one_pow (n : ℕ) : (1 : α)^n = (1:α) := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem add_monoid.smul_zero (n : ℕ) : n•(0 : β) = (0:β) := by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]] theorem pow_mul (a : α) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem add_monoid.mul_smul' : ∀ (a : β) (m n : ℕ), m * n • a = n•(m•a) := @pow_mul (multiplicative β) _ theorem pow_mul' (a : α) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem add_monoid.mul_smul (a : β) (m n : ℕ) : m * n • a = m•(n•a) := by rw [mul_comm, add_monoid.mul_smul'] @[simp] theorem add_monoid.smul_one [has_one β] : ∀ n : ℕ, n • (1 : β) = n := nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _) theorem pow_bit0 (a : α) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_smul (a : β) (n : ℕ) : bit0 n • a = n•a + n•a := add_monoid.add_smul _ _ _ theorem pow_bit1 (a : α) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_smul : ∀ (a : β) (n : ℕ), bit1 n • a = n•a + n•a + a := @pow_bit1 (multiplicative β) _ theorem pow_mul_comm (a : α) (m n : ℕ) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] theorem smul_add_comm : ∀ (a : β) (m n : ℕ), m•a + n•a = n•a + m•a := @pow_mul_comm (multiplicative β) _ @[simp] theorem list.prod_repeat (a : α) (n : ℕ) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp] theorem list.sum_repeat : ∀ (a : β) (n : ℕ), (list.repeat a n).sum = n • a := @list.prod_repeat (multiplicative β) _ @[simp] lemma units.coe_pow (u : units α) (n : ℕ) : ((u ^ n : units α) : α) = u ^ n := by induction n; simp [*, pow_succ] end monoid namespace is_monoid_hom variables {β : Type v} [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] theorem map_pow (a : α) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n | 0 := is_monoid_hom.map_one f | (nat.succ n) := by rw [pow_succ, is_monoid_hom.map_mul f, map_pow n]; refl end is_monoid_hom namespace is_add_monoid_hom variables {β : Type*} [add_monoid α] [add_monoid β] (f : α → β) [is_add_monoid_hom f] theorem map_smul (a : α) : ∀(n : ℕ), f (n • a) = n • (f a) | 0 := is_add_monoid_hom.map_zero f | (nat.succ n) := by rw [succ_smul, is_add_monoid_hom.map_add f, map_smul n]; refl end is_add_monoid_hom @[simp] theorem nat.pow_eq_pow (p q : ℕ) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]] @[simp] theorem nat.smul_eq_mul (m n : ℕ) : m • n = m * n := by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul], rw [succ_smul', ih, nat.succ_mul]] /- commutative monoid -/ section comm_monoid variables [comm_monoid α] {β : Type*} [add_comm_monoid β] theorem mul_pow (a b : α) (n : ℕ) : (a * b)^n = a^n * b^n := by induction n with n ih; [exact (mul_one _).symm, simp only [pow_succ, ih, mul_assoc, mul_left_comm]] theorem add_monoid.smul_add : ∀ (a b : β) (n : ℕ), n•(a + b) = n•a + n•b := @mul_pow (multiplicative β) _ instance pow.is_monoid_hom (n : ℕ) : is_monoid_hom ((^ n) : α → α) := { map_mul := λ _ _, mul_pow _ _ _, map_one := one_pow _ } instance add_monoid.smul.is_add_monoid_hom (n : ℕ) : is_add_monoid_hom (add_monoid.smul n : β → β) := { map_add := λ _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ } end comm_monoid section group variables [group α] {β : Type*} [add_group β] section nat @[simp] theorem inv_pow (a : α) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem add_monoid.neg_smul : ∀ (a : β) (n : ℕ), n•(-a) = -(n•a) := @inv_pow (multiplicative β) _ theorem pow_sub (a : α) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem add_monoid.smul_sub : ∀ (a : β) {m n : ℕ}, n ≤ m → (m - n)•a = m•a - n•a := @pow_sub (multiplicative β) _ theorem pow_inv_comm (a : α) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _) theorem add_monoid.smul_neg_comm : ∀ (a : β) (m n : ℕ), m•(-a) + n•a = n•a + m•(-a) := @pow_inv_comm (multiplicative β) _ end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : α) : ℤ → α | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ def gsmul (n : ℤ) (a : β) : β := @gpow (multiplicative β) _ a n @[priority 10] instance group.has_pow : has_pow α ℤ := ⟨gpow⟩ localized "infix ` • `:70 := gsmul" in add_group localized "infix ` •ℕ `:70 := add_monoid.smul" in smul localized "infix ` •ℤ `:70 := gsmul" in smul @[simp] theorem gpow_coe_nat (a : α) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : β) (n : ℕ) : (n:ℤ) • a = n •ℕ a := rfl @[simp] theorem gpow_of_nat (a : α) (n : ℕ) : a ^ of_nat n = a ^ n := rfl @[simp] theorem gsmul_of_nat (a : β) (n : ℕ) : of_nat n • a = n •ℕ a := rfl @[simp] theorem gpow_neg_succ (a : α) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ (a : β) (n : ℕ) : -[1+n] • a = - (n.succ •ℕ a) := rfl local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : α) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem zero_gsmul (a : β) : (0:ℤ) • a = 0 := rfl @[simp] theorem gpow_one (a : α) : a ^ (1:ℤ) = a := mul_one _ @[simp] theorem one_gsmul (a : β) : (1:ℤ) • a = a := add_zero _ @[simp] theorem one_gpow : ∀ (n : ℤ), (1 : α) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:α), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : ∀ (n : ℤ), n • (0 : β) = 0 := @one_gpow (multiplicative β) _ @[simp] theorem gpow_neg (a : α) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : ∀ (a : β) (n : ℤ), -n • a = -(n • a) := @gpow_neg (multiplicative β) _ theorem gpow_neg_one (x : α) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : β) : (-1:ℤ) • x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x theorem gsmul_one [has_one β] (n : ℤ) : n • (1 : β) = n := begin cases n, { rw [gsmul_of_nat, add_monoid.smul_one, int.cast_of_nat] }, { rw [gsmul_neg_succ, add_monoid.smul_one, int.cast_neg_succ_of_nat, nat.cast_succ] } end theorem inv_gpow (a : α) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) private lemma gpow_add_aux (a : α) (m n : nat) : a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] := or.elim (nat.lt_or_ge m (nat.succ n)) (assume h1 : m < succ n, have h2 : m ≤ n, from le_of_lt_succ h1, suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n], by rwa [of_nat_add_neg_succ_of_nat_of_lt h1], show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n], by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl) (assume : m ≥ succ n, suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : α) : ∀ (i j : ℤ), a ^ (i + j) = a ^ i * a ^ j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux, gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm] | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this, by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev] theorem add_gsmul : ∀ (a : β) (i j : ℤ), (i + j) • a = i • a + j • a := @gpow_add (multiplicative β) _ theorem gpow_add_one (a : α) (i : ℤ) : a ^ (i + 1) = a ^ i * a := by rw [gpow_add, gpow_one] theorem add_one_gsmul : ∀ (a : β) (i : ℤ), (i + 1) • a = i • a + a := @gpow_add_one (multiplicative β) _ theorem gpow_one_add (a : α) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : ∀ (a : β) (i : ℤ), (1 + i) • a = a + i • a := @gpow_one_add (multiplicative β) _ theorem gpow_mul_comm (a : α) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : β) (i j), i • a + j • a = j • a + i • a := @gpow_mul_comm (multiplicative β) _ theorem gpow_mul (a : α) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ) (n : ℕ) := pow_mul _ _ _ | (m : ℕ) -[1+ n] := (gpow_neg _ (m * succ n)).trans $ show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : ℕ) := (gpow_neg _ (succ m * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl | -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $ show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv] theorem gsmul_mul' : ∀ (a : β) (m n : ℤ), m * n • a = n • (m • a) := @gpow_mul (multiplicative β) _ theorem gpow_mul' (a : α) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : β) (m n : ℤ) : m * n • a = m • (n • a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : α) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : β) (n : ℤ) : bit0 n • a = n • a + n • a := gpow_add _ _ _ theorem gpow_bit1 (a : α) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : ∀ (a : β) (n : ℤ), bit1 n • a = n • a + n • a + a := @gpow_bit1 (multiplicative β) _ theorem gsmul_neg (a : β) (n : ℤ) : gsmul n (- a) = - gsmul n a := begin induction n using int.induction_on with z ih z ih, { simp }, { rw [add_comm] {occs := occurrences.pos [1]}, simp [add_gsmul, ih, -add_comm] }, { rw [sub_eq_add_neg, add_comm] {occs := occurrences.pos [1]}, simp [ih, add_gsmul, neg_gsmul, -add_comm] } end end group namespace is_group_hom variables {β : Type v} [group α] [group β] (f : α → β) [is_group_hom f] theorem map_pow (a : α) (n : ℕ) : f (a ^ n) = f a ^ n := is_monoid_hom.map_pow f a n theorem map_gpow (a : α) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; [exact is_group_hom.map_pow f _ _, exact (is_group_hom.map_inv f _).trans (congr_arg _ $ is_group_hom.map_pow f _ _)] end is_group_hom namespace is_add_group_hom variables {β : Type v} [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] theorem map_smul (a : α) (n : ℕ) : f (n • a) = n • f a := is_add_monoid_hom.map_smul f a n theorem map_gsmul (a : α) (n : ℤ) : f (gsmul n a) = gsmul n (f a) := @is_group_hom.map_gpow (multiplicative α) (multiplicative β) _ _ f _ a n end is_add_group_hom open_locale smul section comm_monoid variables [comm_group α] {β : Type*} [add_comm_group β] theorem mul_gpow (a b : α) : ∀ n:ℤ, (a * b)^n = a^n * b^n | (n : ℕ) := mul_pow a b n | -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm] theorem gsmul_add : ∀ (a b : β) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b := @mul_gpow (multiplicative β) _ theorem gsmul_sub : ∀ (a b : β) (n : ℤ), gsmul n (a - b) = gsmul n a - gsmul n b := by simp [gsmul_add, gsmul_neg] instance gpow.is_group_hom (n : ℤ) : is_group_hom ((^ n) : α → α) := { map_mul := λ _ _, mul_gpow _ _ n } instance gsmul.is_add_group_hom (n : ℤ) : is_add_group_hom (gsmul n : β → β) := { map_add := λ _ _, gsmul_add _ _ n } end comm_monoid section group @[instance] theorem is_add_group_hom.gsmul {α β} [add_group α] [add_comm_group β] (f : α → β) [is_add_group_hom f] (z : ℤ) : is_add_group_hom (λa, gsmul z (f a)) := { map_add := assume a b, by rw [is_add_hom.map_add f, gsmul_add] } end group @[simp] lemma with_bot.coe_smul [add_monoid α] (a : α) (n : ℕ) : ((add_monoid.smul n a : α) : with_bot α) = add_monoid.smul n a := by induction n; simp [*, succ_smul]; refl theorem add_monoid.smul_eq_mul' [semiring α] (a : α) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero], rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]] theorem add_monoid.smul_eq_mul [semiring α] (n : ℕ) (a : α) : n • a = n * a := by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm] theorem add_monoid.mul_smul_left [semiring α] (a b : α) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc] theorem add_monoid.mul_smul_assoc [semiring α] (a b : α) (n : ℕ) : n • (a * b) = n • a * b := by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc] lemma zero_pow [semiring α] : ∀ {n : ℕ}, 0 < n → (0 : α) ^ n = 0 | (n+1) _ := zero_mul _ @[simp, move_cast] theorem nat.cast_pow [semiring α] (n m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp, move_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]] theorem is_semiring_hom.map_pow {β} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] (x : α) (n : ℕ) : f (x ^ n) = f x ^ n := by induction n with n ih; [exact is_semiring_hom.map_one f, rw [pow_succ, pow_succ, is_semiring_hom.map_mul f, ih]] theorem neg_one_pow_eq_or {R} [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [comm_semiring α] (a : α) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩ theorem gsmul_eq_mul [ring α] (a : α) : ∀ n, n •ℤ a = n * a | (n : ℕ) := add_monoid.smul_eq_mul _ _ | -[1+ n] := show -(_•_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring α] (a : α) (n : ℤ) : n •ℤ a = a * n := by rw [gsmul_eq_mul, int.mul_cast_comm] theorem mul_gsmul_left [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp, move_cast] theorem int.cast_pow [ring α] (n : ℤ) (m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring α] {n : ℕ} : (-1 : α) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem sq_sub_sq [comm_ring α] (a b : α) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [pow_two, pow_two, mul_self_sub_mul_self] theorem pow_eq_zero [domain α] {x : α} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end theorem pow_ne_zero [domain α] {a : α} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h @[simp] theorem one_div_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by induction n with n ih; [exact (div_one _).symm, rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl @[simp] theorem division_ring.inv_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ := by simpa only [inv_eq_one_div] using one_div_pow ha n @[simp] theorem div_pow [field α] (a : α) {b : α} (hb : b ≠ 0) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, ← div_eq_mul_one_div] theorem add_monoid.smul_nonneg [ordered_comm_monoid α] {a : α} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n • a | 0 := le_refl _ | (n+1) := add_nonneg' H (add_monoid.smul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring α] (a : α) (n : ℕ) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring α] (n : ℕ) : abs ((-1 : α)^n) = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] lemma inv_pow' [discrete_field α] (a : α) (n : ℕ) : (a ^ n)⁻¹ = a⁻¹ ^ n := by induction n; simp [*, pow_succ, mul_inv', mul_comm] lemma pow_inv [division_ring α] (a : α) : ∀ n : ℕ, a ≠ 0 → (a^n)⁻¹ = (a⁻¹)^n | 0 ha := inv_one | (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha] namespace add_monoid variable [ordered_comm_monoid α] theorem smul_le_smul {a : α} {n m : ℕ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a := let ⟨k, hk⟩ := nat.le.dest h in calc n • a = n • a + 0 : (add_zero _).symm ... ≤ n • a + k • a : add_le_add_left' (smul_nonneg ha _) ... = m • a : by rw [← hk, add_smul] lemma smul_le_smul_of_le_right {a b : α} (hab : a ≤ b) : ∀ i : ℕ, i • a ≤ i • b | 0 := by simp | (k+1) := add_le_add' hab (smul_le_smul_of_le_right _) end add_monoid section linear_ordered_semiring variable [linear_ordered_semiring α] theorem pow_pos {a : α} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : α} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : α} {n : ℕ} (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_right_inj {x y : α} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : α} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) theorem one_add_mul_le_pow {a : α} (H : 0 ≤ a) : ∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := begin rw [pow_succ', succ_smul'], refine le_trans _ (mul_le_mul_of_nonneg_right (one_add_mul_le_pow n) (add_nonneg zero_le_one H)), rw [mul_add, mul_one, ← add_assoc, add_le_add_iff_left], simpa only [one_mul] using mul_le_mul_of_nonneg_right ((le_add_iff_nonneg_right 1).2 (add_monoid.smul_nonneg H n)) H end theorem pow_le_pow {a : α} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : α} {n m : ℕ} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≤ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : α} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : α} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h private lemma pow_lt_pow_of_lt_one_aux {a : α} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin simp only [add_zero], rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : α} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : α} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : α} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : α} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := le_refl (1 : α) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring α] (a : α) : 0 ≤ a ^ 2 := by rw pow_two; exact mul_self_nonneg _ theorem one_add_sub_mul_le_pow [linear_ordered_ring α] {a : α} (H : 1 ≤ a) (n : ℕ) : 1 + n • (a - 1) ≤ a ^ n := by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow (sub_nonneg.2 H) n namespace int lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] end int @[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma div_sq_cancel {α} [field α] {a : α} (ha : a ≠ 0) (b : α) : a^2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
62fbf4fc8244490d0ee2f181d4ba5ffb543f067c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/arthur1.lean
73f6d509591d6ce90f47650448860d65d3c458af
[ "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
15,765
lean
import Lean.Data.HashMap inductive NEList (α : Type) | uno : α → NEList α | cons : α → NEList α → NEList α def NEList.contains [BEq α] : NEList α → α → Bool | uno a, x => a == x | cons a as, x => a == x || as.contains x def NEList.noDup [BEq α] : NEList α → Bool | uno a => true | cons a as => ¬as.contains a && as.noDup @[specialize] def NEList.foldl (f : α → β → α) : (init : α) → NEList β → α | a, uno b => f a b | a, cons b l => foldl f (f a b) l @[specialize] def NEList.map (f : α → β) : NEList α → NEList β | uno a => uno (f a) | cons a as => cons (f a) (map f as) inductive Literal | bool : Bool → Literal | int : Int → Literal | float : Float → Literal | str : String → Literal inductive BinOp | add | mul | eq | ne | lt | le | gt | ge inductive UnOp | not mutual inductive Lambda | mk : (l : NEList String) → l.noDup → Program → Lambda inductive Expression | lit : Literal → Expression | var : String → Expression | lam : Lambda → Expression | list : List Literal → Expression | app : Expression → NEList Expression → Expression | unOp : UnOp → Expression → Expression | binOp : BinOp → Expression → Expression → Expression inductive Program | skip : Program | eval : Expression → Program | decl : String → Program → Program | seq : Program → Program → Program | fork : Expression → Program → Program → Program | loop : Expression → Program → Program | print : Expression → Program deriving Inhabited end inductive Value | nil : Value | lit : Literal → Value | list : List Literal → Value | lam : Lambda → Value deriving Inhabited abbrev Context := Lean.HashMap String Value inductive ErrorType | name | type | runTime def Literal.typeStr : Literal → String | bool _ => "bool" | int _ => "int" | float _ => "float" | str _ => "str" def removeRightmostZeros (s : String) : String := let rec aux (buff res : List Char) : List Char → List Char | [] => res.reverse | a :: as => if a != '0' then aux [] (a :: (buff ++ res)) as else aux (a :: buff) res as ⟨aux [] [] s.data⟩ protected def Literal.toString : Literal → String | bool b => toString b | int i => toString i | float f => removeRightmostZeros $ toString f | str s => s def Lambda.typeStr : Lambda → String | mk l .. => (l.foldl (init := "") fun acc _ => acc ++ "_ → ") ++ "_" def Value.typeStr : Value → String | nil => "nil" | lit l => l.typeStr | list _ => "list" | lam l => l.typeStr def Literal.eq : Literal → Literal → Bool | bool bₗ, bool bᵣ => bₗ == bᵣ | int iₗ, int iᵣ => iₗ == iᵣ | float fₗ, float fᵣ => fₗ == fᵣ | int iₗ, float fᵣ => (.ofInt iₗ) == fᵣ | float fₗ, int iᵣ => fₗ == (.ofInt iᵣ) | str sₗ, str sᵣ => sₗ == sᵣ | _ , _ => false def listLiteralEq : List Literal → List Literal → Bool | [], [] => true | a :: a' :: as, b :: b' :: bs => a.eq b && listLiteralEq (a' :: as) (b' :: bs) | _, _ => false def opError (app l r : String) : String := s!"I can't perform a '{app}' operation between '{l}' and '{r}'" def opError1 (app v : String) : String := s!"I can't perform a '{app}' operation on '{v}'" def Value.not : Value → Except String Value | lit $ .bool b => return lit $ .bool !b | v => throw $ opError1 "!" v.typeStr def Value.add : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return lit $ .bool $ bₗ || bᵣ | lit $ .int iₗ, lit $ .int iᵣ => return lit $ .int $ iₗ + iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return lit $ .float $ fₗ + fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return lit $ .float $ (.ofInt iₗ) + fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return lit $ .float $ fₗ + (.ofInt iᵣ) | lit $ .str sₗ, lit $ .str sᵣ => return lit $ .str $ sₗ ++ sᵣ | list lₗ, list lᵣ => return list $ lₗ ++ lᵣ | list l, lit r => return list $ l.concat r | l, r => throw $ opError "+" l.typeStr r.typeStr def Value.mul : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return .lit $ .bool $ bₗ && bᵣ | lit $ .int iₗ, lit $ .int iᵣ => return .lit $ .int $ iₗ * iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return .lit $ .float $ fₗ * fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return .lit $ .float $ (.ofInt iₗ) * fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return .lit $ .float $ fₗ * (.ofInt iᵣ) | l, r => throw $ opError "*" l.typeStr r.typeStr def Bool.toNat : Bool → Nat | false => 0 | true => 1 def Value.lt : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return lit $ .bool $ bₗ.toNat < bᵣ.toNat | lit $ .int iₗ, lit $ .int iᵣ => return lit $ .bool $ iₗ < iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return lit $ .bool $ fₗ < fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return lit $ .bool $ (.ofInt iₗ) < fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return lit $ .bool $ fₗ < (.ofInt iᵣ) | lit $ .str sₗ, lit $ .str sᵣ => return lit $ .bool $ sₗ < sᵣ | list lₗ, list lᵣ => return lit $ .bool $ lₗ.length < lᵣ.length | l, r => throw $ opError "<" l.typeStr r.typeStr def Value.le : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return lit $ .bool $ bₗ.toNat ≤ bᵣ.toNat | lit $ .int iₗ, lit $ .int iᵣ => return lit $ .bool $ iₗ ≤ iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return lit $ .bool $ fₗ ≤ fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return lit $ .bool $ (.ofInt iₗ) ≤ fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return lit $ .bool $ fₗ ≤ (.ofInt iᵣ) | lit $ .str sₗ, lit $ .str sᵣ => return lit $ .bool $ sₗ < sᵣ || sₗ == sᵣ | list lₗ, list lᵣ => return lit $ .bool $ lₗ.length ≤ lᵣ.length | l, r => throw $ opError "<=" l.typeStr r.typeStr def Value.gt : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return lit $ .bool $ bₗ.toNat > bᵣ.toNat | lit $ .int iₗ, lit $ .int iᵣ => return lit $ .bool $ iₗ > iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return lit $ .bool $ fₗ > fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return lit $ .bool $ (.ofInt iₗ) > fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return lit $ .bool $ fₗ > (.ofInt iᵣ) | lit $ .str sₗ, lit $ .str sᵣ => return lit $ .bool $ sₗ > sᵣ | list lₗ, list lᵣ => return lit $ .bool $ lₗ.length > lᵣ.length | l, r => throw $ opError ">" l.typeStr r.typeStr def Value.ge : Value → Value → Except String Value | lit $ .bool bₗ, lit $ .bool bᵣ => return lit $ .bool $ bₗ.toNat ≥ bᵣ.toNat | lit $ .int iₗ, lit $ .int iᵣ => return lit $ .bool $ iₗ ≥ iᵣ | lit $ .float fₗ, lit $ .float fᵣ => return lit $ .bool $ fₗ ≥ fᵣ | lit $ .int iₗ, lit $ .float fᵣ => return lit $ .bool $ (.ofInt iₗ) ≥ fᵣ | lit $ .float fₗ, lit $ .int iᵣ => return lit $ .bool $ fₗ ≥ (.ofInt iᵣ) | lit $ .str sₗ, lit $ .str sᵣ => return lit $ .bool $ sₗ > sᵣ || sₗ == sᵣ | list lₗ, list lᵣ => return lit $ .bool $ lₗ.length ≥ lᵣ.length | l, r => throw $ opError ">=" l.typeStr r.typeStr def Value.eq : Value → Value → Except String Value | nil, nil => return lit $ .bool true | lit lₗ, lit lᵣ => return lit $ .bool $ lₗ.eq lᵣ | list lₗ, list lᵣ => return lit $ .bool (listLiteralEq lₗ lᵣ) | lam .. , lam .. => throw "I can't compare functions" | _, _ => return lit $ .bool false def Value.ne : Value → Value → Except String Value | nil, nil => return lit $ .bool false | lit lₗ, lit lᵣ => return lit $ .bool $ !(lₗ.eq lᵣ) | list lₗ, list lᵣ => return lit $ .bool !(listLiteralEq lₗ lᵣ) | lam .., lam .. => throw "I can't compare functions" | _, _ => return lit $ .bool true def Value.unOp : Value → UnOp → Except String Value | v, .not => v.not def Value.binOp : Value → Value → BinOp → Except String Value | l, r, .add => l.add r | l, r, .mul => l.mul r | l, r, .lt => l.lt r | l, r, .le => l.le r | l, r, .gt => l.gt r | l, r, .ge => l.ge r | l, r, .eq => l.eq r | l, r, .ne => l.ne r def NEList.unfoldStrings (l : NEList String) : String := l.foldl (init := "") $ fun acc a => acc ++ s!" {a}" |>.trimLeft mutual partial def unfoldExpressions (es : NEList Expression) : String := (es.map exprToString).unfoldStrings partial def exprToString : Expression → String | .var n => n | .lit l => l.toString | .list l => toString $ l.map Literal.toString | .lam _ => "«function»" | .app e es => s!"({exprToString e} {unfoldExpressions es})" | .unOp .not e => s!"!{exprToString e}" | .binOp .add l r => s!"({exprToString l} + {exprToString r})" | .binOp .mul l r => s!"({exprToString l} * {exprToString r})" | .binOp .eq l r => s!"({exprToString l} = {exprToString r})" | .binOp .ne l r => s!"({exprToString l} != {exprToString r})" | .binOp .lt l r => s!"({exprToString l} < {exprToString r})" | .binOp .le l r => s!"({exprToString l} <= {exprToString r})" | .binOp .gt l r => s!"({exprToString l} > {exprToString r})" | .binOp .ge l r => s!"({exprToString l} >= {exprToString r})" end instance : ToString Expression := ⟨exprToString⟩ def valToString : Value → String | .nil => "«nil»" | .lit l => l.toString | .list l => toString $ l.map Literal.toString | .lam _ => "«function»" instance : ToString Value := ⟨valToString⟩ def consume (p : Program) : NEList String → NEList Expression → Option ((Option (NEList String)) × Program) | .cons n ns, .cons e es => consume (.seq (.decl n (.eval e)) p) ns es | .cons n ns, .uno e => some (some ns, .seq (.decl n (.eval e)) p) | .uno n, .uno e => some (none, .seq (.decl n (.eval e)) p) | .uno _, .cons .. => none theorem noDupOfConsumeNoDup (h : ns.noDup) (h' : consume p' ns es = some (some l, p)) : l.noDup = true := by induction ns generalizing p' es with | uno _ => cases es <;> cases h' | cons _ _ hi => simp [NEList.noDup] at h cases es with | uno _ => simp [consume] at h'; simp only [h.2, ← h'.1] | cons _ _ => exact hi h.2 h' inductive Continuation | exit : Continuation | seq : Program → Continuation → Continuation | decl : String → Continuation → Continuation | fork : Expression → Program → Program → Continuation → Continuation | loop : Expression → Program → Continuation → Continuation | unOp : UnOp → Expression → Continuation → Continuation | binOp₁ : BinOp → Expression → Continuation → Continuation | binOp₂ : BinOp → Value → Continuation → Continuation | app : Expression → NEList Expression → Continuation → Continuation | block : Context → Continuation → Continuation | print : Continuation → Continuation inductive State | ret : Value → Context → Continuation → State | prog : Program → Context → Continuation → State | expr : Expression → Context → Continuation → State | error : ErrorType → Context → String → State | done : Value → Context → State def cantEvalAsBool (e : Expression) (v : Value) : String := s!"I can't evaluate '{e}' as a 'bool' because it reduces to '{v}', of " ++ s!"type '{v.typeStr}'" def notFound (n : String) : String := s!"I can't find the definition of '{n}'" def notAFunction (e : Expression) (v : Value) : String := s!"I can't apply arguments to '{e}' because it evaluates to '{v}', of " ++ s!"type '{v.typeStr}'" def wrongNParameters (e : Expression) (allowed provided : Nat) : String := s!"I can't apply {provided} arguments to '{e}' because the maximum " ++ s!"allowed is {allowed}" def NEList.length : NEList α → Nat | uno _ => 1 | cons _ l => 1 + l.length def State.step : State → State | prog .skip ctx k => ret .nil ctx k | prog (.eval e) ctx k => expr e ctx k | prog (.seq p₁ p₂) ctx k => prog p₁ ctx (.seq p₂ k) | prog (.decl n p) ctx k => prog p ctx $ .block ctx (.decl n k) | prog (.fork e pT pF) ctx k => expr e ctx (.fork e pT pF k) | prog (.loop e p) ctx k => expr e ctx (.loop e p k) | prog (.print e) ctx k => expr e ctx (.print k) | expr (.lit l) ctx k => ret (.lit l) ctx k | expr (.list l) ctx k => ret (.list l) ctx k | expr (.var n) ctx k => match ctx[n] with | none => error .name ctx $ notFound n | some v => ret v ctx k | expr (.lam l) ctx k => ret (.lam l) ctx k | expr (.app e es) ctx k => expr e ctx (.app e es k) | expr (.unOp o e) ctx k => expr e ctx (.unOp o e k) | expr (.binOp o e₁ e₂) ctx k => expr e₁ ctx (.binOp₁ o e₂ k) | ret v ctx .exit => done v ctx | ret v ctx (.print k) => dbg_trace v; ret .nil ctx k | ret _ ctx (.seq p k) => prog p ctx k | ret v _ (.block ctx k) => ret v ctx k | ret v ctx (.app e es k) => match v with | .lam $ .mk ns h p => match h' : consume p ns es with | some (some l, p) => ret (.lam $ .mk l (noDupOfConsumeNoDup h h') p) ctx k | some (none, p) => prog p ctx (.block ctx k) | none => error .runTime ctx $ wrongNParameters e ns.length es.length | v => error .type ctx $ notAFunction e v | ret (.lit $ .bool true) ctx (.fork _ pT _ k) => prog pT ctx k | ret (.lit $ .bool false) ctx (.fork _ _ pF k) => prog pF ctx k | ret v ctx (.fork e ..) => error .type ctx $ cantEvalAsBool e v | ret (.lit $ .bool true) ctx (.loop e p k) => prog (.seq p (.loop e p)) ctx k | ret (.lit $ .bool false) ctx (.loop _ _ k) => ret .nil ctx k | ret v ctx (.loop e ..) => error .type ctx $ cantEvalAsBool e v | ret v ctx (.decl n k) => ret .nil (ctx.insert n v) k | ret v ctx (.unOp o e k) => match v.unOp o with | .error m => error .type ctx m | .ok v => ret v ctx k | ret v1 ctx (.binOp₁ o e2 k) => expr e2 ctx (.binOp₂ o v1 k) | ret v2 ctx (.binOp₂ o v1 k) => match v1.binOp v2 o with | .error m => error .type ctx m | .ok v => ret v ctx k | s@(error ..) => s | s@(done ..) => s def Context.equiv (cₗ cᵣ : Context) : Prop := ∀ n : String, cₗ[n] = cᵣ[n] def State.stepN : State → Nat → State | s, 0 => s | s, n + 1 => s.step.stepN n def State.reaches (s₁ s₂ : State) : Prop := ∃ n, s₁.stepN n = s₂ notation cₗ " ≃ " cᵣ:21 => Context.equiv cₗ cᵣ notation s₁ " ↠ " s₂ => State.reaches s₁ s₂ def State.ctx : State → Context | ret _ c _ => c | prog _ c _ => c | expr _ c _ => c | error _ c _ => c | done _ c => c theorem Context.equivSelf {c : Context} : c ≃ c := fun _ => rfl /- theorem State.skipStep (h : s = (prog .skip c k).step) : s.ctx ≃ c := by have : s.ctx = c := by rw [h, step, ctx] simp only [this, Context.equivSelf] -/ theorem State.skipClean : (prog .skip c .exit) ↠ (done .nil c) := ⟨2 , by simp only [stepN, step]⟩
23b43d70c4586740bfcedda331744b1d84a50778
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/punit.lean
a953c39ac8dc1a4c7a1d3268b6c69d584e0216d9
[ "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
3,463
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.functor.const import category_theory.discrete_category /-! # The category `discrete punit` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define `star : C ⥤ discrete punit` sending everything to `punit.star`, show that any two functors to `discrete punit` are naturally isomorphic, and construct the equivalence `(discrete punit ⥤ C) ≌ C`. -/ universes v u -- morphism levels before object levels. See note [category_theory universes]. namespace category_theory variables (C : Type u) [category.{v} C] namespace functor /-- The constant functor sending everything to `punit.star`. -/ @[simps] def star : C ⥤ discrete punit := (functor.const _).obj ⟨⟨⟩⟩ variable {C} /-- Any two functors to `discrete punit` are isomorphic. -/ @[simps] def punit_ext (F G : C ⥤ discrete punit) : F ≅ G := nat_iso.of_components (λ _, eq_to_iso dec_trivial) (λ _ _ _, dec_trivial) /-- Any two functors to `discrete punit` are *equal*. You probably want to use `punit_ext` instead of this. -/ lemma punit_ext' (F G : C ⥤ discrete punit) : F = G := functor.ext (λ _, dec_trivial) (λ _ _ _, dec_trivial) /-- The functor from `discrete punit` sending everything to the given object. -/ abbreviation from_punit (X : C) : discrete punit.{v+1} ⥤ C := (functor.const _).obj X /-- Functors from `discrete punit` are equivalent to the category itself. -/ @[simps] def equiv : (discrete punit ⥤ C) ≌ C := { functor := { obj := λ F, F.obj ⟨⟨⟩⟩, map := λ F G θ, θ.app ⟨⟨⟩⟩ }, inverse := functor.const _, unit_iso := begin apply nat_iso.of_components _ _, intro X, apply discrete.nat_iso, rintro ⟨⟨⟩⟩, apply iso.refl _, intros, ext ⟨⟨⟩⟩, simp, end, counit_iso := begin refine nat_iso.of_components iso.refl _, intros X Y f, dsimp, simp, -- See note [dsimp, simp]. end } end functor /-- A category being equivalent to `punit` is equivalent to it having a unique morphism between any two objects. (In fact, such a category is also a groupoid; see `groupoid.of_hom_unique`) -/ theorem equiv_punit_iff_unique : nonempty (C ≌ discrete punit) ↔ (nonempty C) ∧ (∀ x y : C, nonempty $ unique (x ⟶ y)) := begin split, { rintro ⟨h⟩, refine ⟨⟨h.inverse.obj ⟨⟨⟩⟩⟩, λ x y, nonempty.intro _⟩, apply (unique_of_subsingleton _), swap, { have hx : x ⟶ h.inverse.obj ⟨⟨⟩⟩ := by convert h.unit.app x, have hy : h.inverse.obj ⟨⟨⟩⟩ ⟶ y := by convert h.unit_inv.app y, exact hx ≫ hy, }, have : ∀ z, z = h.unit.app x ≫ (h.functor ⋙ h.inverse).map z ≫ h.unit_inv.app y, { intro z, simpa using congr_arg (≫ (h.unit_inv.app y)) (h.unit.naturality z), }, apply subsingleton.intro, intros a b, rw [this a, this b], simp only [functor.comp_map], congr, }, { rintro ⟨⟨p⟩, h⟩, haveI := λ x y, (h x y).some, refine nonempty.intro (category_theory.equivalence.mk ((functor.const _).obj ⟨⟨⟩⟩) ((functor.const _).obj p) _ (by apply functor.punit_ext)), exact nat_iso.of_components (λ _, { hom := default, inv := default }) (λ _ _ _, by tidy), }, end end category_theory
130629a0bfda72130697231eb363f919165a9117
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/InverseUnaryOperation.lean
1ede443063d13d19662ead547a87070b61c9cf82
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
5,747
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section InverseUnaryOperation structure InverseUnaryOperation (A : Type) : Type := (inv : (A → A)) open InverseUnaryOperation structure Sig (AS : Type) : Type := (invS : (AS → AS)) structure Product (A : Type) : Type := (invP : ((Prod A A) → (Prod A A))) structure Hom {A1 : Type} {A2 : Type} (In1 : (InverseUnaryOperation A1)) (In2 : (InverseUnaryOperation A2)) : Type := (hom : (A1 → A2)) (pres_inv : (∀ {x1 : A1} , (hom ((inv In1) x1)) = ((inv In2) (hom x1)))) structure RelInterp {A1 : Type} {A2 : Type} (In1 : (InverseUnaryOperation A1)) (In2 : (InverseUnaryOperation A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_inv : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((inv In1) x1) ((inv In2) y1))))) inductive InverseUnaryOperationTerm : Type | invL : (InverseUnaryOperationTerm → InverseUnaryOperationTerm) open InverseUnaryOperationTerm inductive ClInverseUnaryOperationTerm (A : Type) : Type | sing : (A → ClInverseUnaryOperationTerm) | invCl : (ClInverseUnaryOperationTerm → ClInverseUnaryOperationTerm) open ClInverseUnaryOperationTerm inductive OpInverseUnaryOperationTerm (n : ℕ) : Type | v : ((fin n) → OpInverseUnaryOperationTerm) | invOL : (OpInverseUnaryOperationTerm → OpInverseUnaryOperationTerm) open OpInverseUnaryOperationTerm inductive OpInverseUnaryOperationTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpInverseUnaryOperationTerm2) | sing2 : (A → OpInverseUnaryOperationTerm2) | invOL2 : (OpInverseUnaryOperationTerm2 → OpInverseUnaryOperationTerm2) open OpInverseUnaryOperationTerm2 def simplifyCl {A : Type} : ((ClInverseUnaryOperationTerm A) → (ClInverseUnaryOperationTerm A)) | (invCl x1) := (invCl (simplifyCl x1)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpInverseUnaryOperationTerm n) → (OpInverseUnaryOperationTerm n)) | (invOL x1) := (invOL (simplifyOpB x1)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpInverseUnaryOperationTerm2 n A) → (OpInverseUnaryOperationTerm2 n A)) | (invOL2 x1) := (invOL2 (simplifyOp x1)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((InverseUnaryOperation A) → (InverseUnaryOperationTerm → A)) | In (invL x1) := ((inv In) (evalB In x1)) def evalCl {A : Type} : ((InverseUnaryOperation A) → ((ClInverseUnaryOperationTerm A) → A)) | In (sing x1) := x1 | In (invCl x1) := ((inv In) (evalCl In x1)) def evalOpB {A : Type} {n : ℕ} : ((InverseUnaryOperation A) → ((vector A n) → ((OpInverseUnaryOperationTerm n) → A))) | In vars (v x1) := (nth vars x1) | In vars (invOL x1) := ((inv In) (evalOpB In vars x1)) def evalOp {A : Type} {n : ℕ} : ((InverseUnaryOperation A) → ((vector A n) → ((OpInverseUnaryOperationTerm2 n A) → A))) | In vars (v2 x1) := (nth vars x1) | In vars (sing2 x1) := x1 | In vars (invOL2 x1) := ((inv In) (evalOp In vars x1)) def inductionB {P : (InverseUnaryOperationTerm → Type)} : ((∀ (x1 : InverseUnaryOperationTerm) , ((P x1) → (P (invL x1)))) → (∀ (x : InverseUnaryOperationTerm) , (P x))) | pinvl (invL x1) := (pinvl _ (inductionB pinvl x1)) def inductionCl {A : Type} {P : ((ClInverseUnaryOperationTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 : (ClInverseUnaryOperationTerm A)) , ((P x1) → (P (invCl x1)))) → (∀ (x : (ClInverseUnaryOperationTerm A)) , (P x)))) | psing pinvcl (sing x1) := (psing x1) | psing pinvcl (invCl x1) := (pinvcl _ (inductionCl psing pinvcl x1)) def inductionOpB {n : ℕ} {P : ((OpInverseUnaryOperationTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 : (OpInverseUnaryOperationTerm n)) , ((P x1) → (P (invOL x1)))) → (∀ (x : (OpInverseUnaryOperationTerm n)) , (P x)))) | pv pinvol (v x1) := (pv x1) | pv pinvol (invOL x1) := (pinvol _ (inductionOpB pv pinvol x1)) def inductionOp {n : ℕ} {A : Type} {P : ((OpInverseUnaryOperationTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 : (OpInverseUnaryOperationTerm2 n A)) , ((P x1) → (P (invOL2 x1)))) → (∀ (x : (OpInverseUnaryOperationTerm2 n A)) , (P x))))) | pv2 psing2 pinvol2 (v2 x1) := (pv2 x1) | pv2 psing2 pinvol2 (sing2 x1) := (psing2 x1) | pv2 psing2 pinvol2 (invOL2 x1) := (pinvol2 _ (inductionOp pv2 psing2 pinvol2 x1)) def stageB : (InverseUnaryOperationTerm → (Staged InverseUnaryOperationTerm)) | (invL x1) := (stage1 invL (codeLift1 invL) (stageB x1)) def stageCl {A : Type} : ((ClInverseUnaryOperationTerm A) → (Staged (ClInverseUnaryOperationTerm A))) | (sing x1) := (Now (sing x1)) | (invCl x1) := (stage1 invCl (codeLift1 invCl) (stageCl x1)) def stageOpB {n : ℕ} : ((OpInverseUnaryOperationTerm n) → (Staged (OpInverseUnaryOperationTerm n))) | (v x1) := (const (code (v x1))) | (invOL x1) := (stage1 invOL (codeLift1 invOL) (stageOpB x1)) def stageOp {n : ℕ} {A : Type} : ((OpInverseUnaryOperationTerm2 n A) → (Staged (OpInverseUnaryOperationTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (invOL2 x1) := (stage1 invOL2 (codeLift1 invOL2) (stageOp x1)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (invT : ((Repr A) → (Repr A))) end InverseUnaryOperation
1b83551c184a03c74be20f86c67cea58379d6973
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/instances/ereal.lean
ba32823a51d9b2ba0b1777ecfcaf033d9ce30a96
[ "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
13,234
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import data.real.ereal import topology.algebra.order.monotone_continuity import topology.instances.ennreal /-! # Topological structure on `ereal` We endow `ereal` with the order topology, and prove basic properties of this topology. ## Main results * `coe : ℝ → ereal` is an open embedding * `coe : ℝ≥0∞ → ereal` is an embedding * The addition on `ereal` is continuous except at `(⊥, ⊤)` and at `(⊤, ⊥)`. * Negation is a homeomorphism on `ereal`. ## Implementation Most proofs are adapted from the corresponding proofs on `ℝ≥0∞`. -/ noncomputable theory open classical set filter metric topological_space open_locale classical topological_space ennreal nnreal big_operators filter variables {α : Type*} [topological_space α] namespace ereal instance : topological_space ereal := preorder.topology ereal instance : order_topology ereal := ⟨rfl⟩ instance : t2_space ereal := by apply_instance instance : second_countable_topology ereal := ⟨begin refine ⟨⋃ (q : ℚ), {{a : ereal | a < (q : ℝ)}, {a : ereal | ((q : ℝ) : ereal) < a}}, countable_Union (λ a, (countable_singleton _).insert _), _⟩, refine le_antisymm (le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt}) _, apply le_generate_from (λ s h, _), rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | a < (q : ℝ)}, {b | ((q : ℝ) : ereal) < b}, by { ext x, simpa only [hs, exists_prop, mem_Union] using lt_iff_exists_rat_btwn }, rw show s = ⋃q∈{q:ℚ | ((q : ℝ) : ereal) < a}, {b | b < ((q : ℝ) : ereal)}, by { ext x, simpa only [hs, and_comm, exists_prop, mem_Union] using lt_iff_exists_rat_btwn }]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, apply generate_open.basic, exact mem_Union.2 ⟨q, by simp⟩ }, end⟩ /-! ### Real coercion -/ lemma embedding_coe : embedding (coe : ℝ → ereal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ereal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ | a < ↑b}, { induction a using ereal.rec, { simp only [is_open_univ, bot_lt_coe, set_of_true] }, { simp only [ereal.coe_lt_coe_iff], exact is_open_Ioi }, { simp only [set_of_false, is_open_empty, not_top_lt] } }, show is_open {b : ℝ | ↑b < a}, { induction a using ereal.rec, { simp only [not_lt_bot, set_of_false, is_open_empty] }, { simp only [ereal.coe_lt_coe_iff], exact is_open_Iio }, { simp only [is_open_univ, coe_lt_top, set_of_true] } } }, { rw [@order_topology.topology_eq_generate_intervals ℝ _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, by simp only [imp_self, ereal.coe_eq_coe_iff]⟩ lemma open_embedding_coe : open_embedding (coe : ℝ → ereal) := ⟨embedding_coe, begin convert @is_open_Ioo ereal _ _ _ ⊥ ⊤, ext x, induction x using ereal.rec, { simp only [left_mem_Ioo, mem_range, coe_ne_bot, exists_false, not_false_iff] }, { simp only [mem_range_self, mem_Ioo, bot_lt_coe, coe_lt_top, and_self] }, { simp only [mem_range, right_mem_Ioo, exists_false, coe_ne_top] } end⟩ @[norm_cast] lemma tendsto_coe {α : Type*} {f : filter α} {m : α → ℝ} {a : ℝ} : tendsto (λ a, (m a : ereal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe.tendsto_nhds_iff.symm lemma _root_.continuous_coe_real_ereal : continuous (coe : ℝ → ereal) := embedding_coe.continuous lemma continuous_coe_iff {f : α → ℝ} : continuous (λa, (f a : ereal)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : ℝ} : 𝓝 (r : ereal) = (𝓝 r).map coe := (open_embedding_coe.map_nhds_eq r).symm lemma nhds_coe_coe {r p : ℝ} : 𝓝 ((r : ereal), (p : ereal)) = (𝓝 (r, p)).map (λp:ℝ × ℝ, (p.1, p.2)) := ((open_embedding_coe.prod open_embedding_coe).map_nhds_eq (r, p)).symm lemma tendsto_to_real {a : ereal} (ha : a ≠ ⊤) (h'a : a ≠ ⊥) : tendsto ereal.to_real (𝓝 a) (𝓝 a.to_real) := begin lift a to ℝ using and.intro ha h'a, rw [nhds_coe, tendsto_map'_iff], exact tendsto_id end lemma continuous_on_to_real : continuous_on ereal.to_real ({⊥, ⊤}ᶜ : set ereal) := λ a ha, continuous_at.continuous_within_at (tendsto_to_real (by { simp [not_or_distrib] at ha, exact ha.2 }) (by { simp [not_or_distrib] at ha, exact ha.1 })) /-- The set of finite `ereal` numbers is homeomorphic to `ℝ`. -/ def ne_bot_top_homeomorph_real : ({⊥, ⊤}ᶜ : set ereal) ≃ₜ ℝ := { continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_real, continuous_inv_fun := continuous_coe_real_ereal.subtype_mk _, .. ne_top_bot_equiv_real } /-! ### ennreal coercion -/ lemma embedding_coe_ennreal : embedding (coe : ℝ≥0∞ → ereal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ereal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ≥0∞ | a < ↑b}, { induction a using ereal.rec with x, { simp only [is_open_univ, bot_lt_coe_ennreal, set_of_true] }, { rcases le_or_lt 0 x with h|h, { have : (x : ereal) = ((id ⟨x, h⟩ : ℝ≥0) : ℝ≥0∞) := rfl, rw this, simp only [id.def, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Ioi, }, { have : ∀ (y : ℝ≥0∞), (x : ereal) < y := λ y, (ereal.coe_lt_coe_iff.2 h).trans_le (coe_ennreal_nonneg _), simp only [this, is_open_univ, set_of_true] } }, { simp only [set_of_false, is_open_empty, not_top_lt] } }, show is_open {b : ℝ≥0∞ | ↑b < a}, { induction a using ereal.rec with x, { simp only [not_lt_bot, set_of_false, is_open_empty] }, { rcases le_or_lt 0 x with h|h, { have : (x : ereal) = ((id ⟨x, h⟩ : ℝ≥0) : ℝ≥0∞) := rfl, rw this, simp only [id.def, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Iio, }, { convert is_open_empty, apply eq_empty_iff_forall_not_mem.2 (λ y hy, lt_irrefl (x : ereal) _), exact ((ereal.coe_lt_coe_iff.2 h).trans_le (coe_ennreal_nonneg y)).trans hy } }, { simp only [← coe_ennreal_top, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Iio } } }, { rw [@order_topology.topology_eq_generate_intervals ℝ≥0∞ _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, by simp only [imp_self, coe_ennreal_eq_coe_ennreal_iff]⟩ @[norm_cast] lemma tendsto_coe_ennreal {α : Type*} {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} : tendsto (λ a, (m a : ereal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe_ennreal.tendsto_nhds_iff.symm lemma _root_.continuous_coe_ennreal_ereal : continuous (coe : ℝ≥0∞ → ereal) := embedding_coe_ennreal.continuous lemma continuous_coe_ennreal_iff {f : α → ℝ≥0∞} : continuous (λa, (f a : ereal)) ↔ continuous f := embedding_coe_ennreal.continuous_iff.symm /-! ### Neighborhoods of infinity -/ lemma nhds_top : 𝓝 (⊤ : ereal) = ⨅ a ≠ ⊤, 𝓟 (Ioi a) := nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi] lemma nhds_top' : 𝓝 (⊤ : ereal) = ⨅ a : ℝ, 𝓟 (Ioi a) := begin rw [nhds_top], apply le_antisymm, { exact infi_mono' (λ x, ⟨x, by simp⟩) }, { refine le_infi (λ r, le_infi (λ hr, _)), induction r using ereal.rec, { exact (infi_le _ 0).trans (by simp) }, { exact infi_le _ _ }, { simpa using hr, } } end lemma mem_nhds_top_iff {s : set ereal} : s ∈ 𝓝 (⊤ : ereal) ↔ ∃ (y : ℝ), Ioi (y : ereal) ⊆ s := begin rw [nhds_top', mem_infi_of_directed], { refl }, exact λ x y, ⟨max x y, by simp [le_refl], by simp [le_refl]⟩, end lemma tendsto_nhds_top_iff_real {α : Type*} {m : α → ereal} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ x : ℝ, ∀ᶠ a in f, ↑x < m a := by simp only [nhds_top', mem_Ioi, tendsto_infi, tendsto_principal] lemma nhds_bot : 𝓝 (⊥ : ereal) = ⨅ a ≠ ⊥, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot] lemma nhds_bot' : 𝓝 (⊥ : ereal) = ⨅ a : ℝ, 𝓟 (Iio a) := begin rw [nhds_bot], apply le_antisymm, { exact infi_mono' (λ x, ⟨x, by simp⟩) }, { refine le_infi (λ r, le_infi (λ hr, _)), induction r using ereal.rec, { simpa using hr }, { exact infi_le _ _ }, { exact (infi_le _ 0).trans (by simp) } } end lemma mem_nhds_bot_iff {s : set ereal} : s ∈ 𝓝 (⊥ : ereal) ↔ ∃ (y : ℝ), Iio (y : ereal) ⊆ s := begin rw [nhds_bot', mem_infi_of_directed], { refl }, exact λ x y, ⟨min x y, by simp [le_refl], by simp [le_refl]⟩, end lemma tendsto_nhds_bot_iff_real {α : Type*} {m : α → ereal} {f : filter α} : tendsto m f (𝓝 ⊥) ↔ ∀ x : ℝ, ∀ᶠ a in f, m a < x := by simp only [nhds_bot', mem_Iio, tendsto_infi, tendsto_principal] /-! ### Continuity of addition -/ lemma continuous_at_add_coe_coe (a b :ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, b) := by simp only [continuous_at, nhds_coe_coe, ← coe_add, tendsto_map'_iff, (∘), tendsto_coe, tendsto_add] lemma continuous_at_add_top_coe (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊤, a) := begin simp only [continuous_at, tendsto_nhds_top_iff_real, top_add, nhds_prod_eq], assume r, rw eventually_prod_iff, refine ⟨λ z, ((r - (a - 1): ℝ) : ereal) < z, Ioi_mem_nhds (coe_lt_top _), λ z, ((a - 1 : ℝ) : ereal) < z, Ioi_mem_nhds (by simp [zero_lt_one]), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp, end lemma continuous_at_add_coe_top (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, ⊤) := begin change continuous_at ((λ (p : ereal × ereal), p.2 + p.1) ∘ prod.swap) (a, ⊤), apply continuous_at.comp _ continuous_swap.continuous_at, simp_rw add_comm, exact continuous_at_add_top_coe a end lemma continuous_at_add_top_top : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊤, ⊤) := begin simp only [continuous_at, tendsto_nhds_top_iff_real, top_add, nhds_prod_eq], assume r, rw eventually_prod_iff, refine ⟨λ z, (r : ereal) < z, Ioi_mem_nhds (coe_lt_top _), λ z, ((0 : ℝ) : ereal) < z, Ioi_mem_nhds (by simp [zero_lt_one]), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp, end lemma continuous_at_add_bot_coe (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊥, a) := begin simp only [continuous_at, tendsto_nhds_bot_iff_real, nhds_prod_eq, bot_add_coe], assume r, rw eventually_prod_iff, refine ⟨λ z, z < ((r - (a + 1): ℝ) : ereal), Iio_mem_nhds (bot_lt_coe _), λ z, z < ((a + 1 : ℝ) : ereal), Iio_mem_nhds (by simp [-coe_add, zero_lt_one]), λ x hx y hy, _⟩, convert add_lt_add hx hy, rw sub_add_cancel, end lemma continuous_at_add_coe_bot (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, ⊥) := begin change continuous_at ((λ (p : ereal × ereal), p.2 + p.1) ∘ prod.swap) (a, ⊥), apply continuous_at.comp _ continuous_swap.continuous_at, simp_rw add_comm, exact continuous_at_add_bot_coe a end lemma continuous_at_add_bot_bot : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊥, ⊥) := begin simp only [continuous_at, tendsto_nhds_bot_iff_real, nhds_prod_eq, bot_add_bot], assume r, rw eventually_prod_iff, refine ⟨λ z, z < r, Iio_mem_nhds (bot_lt_coe _), λ z, z < 0, Iio_mem_nhds (bot_lt_coe _), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp end /-- The addition on `ereal` is continuous except where it doesn't make sense (i.e., at `(⊥, ⊤)` and at `(⊤, ⊥)`). -/ lemma continuous_at_add {p : ereal × ereal} (h : p.1 ≠ ⊤ ∨ p.2 ≠ ⊥) (h' : p.1 ≠ ⊥ ∨ p.2 ≠ ⊤) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) p := begin rcases p with ⟨x, y⟩, induction x using ereal.rec; induction y using ereal.rec, { exact continuous_at_add_bot_bot }, { exact continuous_at_add_bot_coe _ }, { simpa using h' }, { exact continuous_at_add_coe_bot _ }, { exact continuous_at_add_coe_coe _ _ }, { exact continuous_at_add_coe_top _ }, { simpa using h }, { exact continuous_at_add_top_coe _ }, { exact continuous_at_add_top_top }, end /-! ### Negation-/ /-- Negation on `ereal` as a homeomorphism -/ def neg_homeo : ereal ≃ₜ ereal := neg_order_iso.to_homeomorph lemma continuous_neg : continuous (λ (x : ereal), -x) := neg_homeo.continuous end ereal
77381ab984c92c78985742610286e57f5e556413
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/squeeze.lean
d7044b1ddc7849003367128d724306e08b80b674
[ "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
14,733
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import control.traversable.basic import tactic.simpa open interactive interactive.types lean.parser private meta def loc.to_string_aux : option name → string | none := "⊢" | (some x) := to_string x /-- pretty print a `loc` -/ meta def loc.to_string : loc → string | (loc.ns []) := "" | (loc.ns [none]) := "" | (loc.ns ls) := string.join $ list.intersperse " " (" at" :: ls.map loc.to_string_aux) | loc.wildcard := " at *" /-- shift `pos` `n` columns to the left -/ meta def pos.move_left (p : pos) (n : ℕ) : pos := { line := p.line, column := p.column - n } namespace tactic open list /-- parse structure instance of the shape `{ field1 := value1, .. , field2 := value2 }` -/ meta def struct_inst : lean.parser pexpr := do tk "{", ls ← sep_by (skip_info (tk ",")) ( sum.inl <$> (tk ".." *> texpr) <|> sum.inr <$> (prod.mk <$> ident <* tk ":=" <*> texpr)), tk "}", let (srcs,fields) := partition_map id ls, let (names,values) := unzip fields, pure $ pexpr.mk_structure_instance { field_names := names, field_values := values, sources := srcs } /-- pretty print structure instance -/ meta def struct.to_tactic_format (e : pexpr) : tactic format := do r ← e.get_structure_instance_info, fs ← mzip_with (λ n v, do v ← to_expr v >>= pp, pure $ format!"{n} := {v}" ) r.field_names r.field_values, let ss := r.sources.map (λ s, format!" .. {s}"), let x : format := format.join $ list.intersperse ", " (fs ++ ss), pure format!" {{{x}}" /-- Attribute containing a table that accumulates multiple `squeeze_simp` suggestions -/ @[user_attribute] private meta def squeeze_loc_attr : user_attribute unit (option (list (pos × string × list simp_arg_type × string))) := { name := `_squeeze_loc, parser := fail "this attribute should not be used", descr := "table to accumulate multiple `squeeze_simp` suggestions" } /-- dummy declaration used as target of `squeeze_loc` attribute -/ def squeeze_loc_attr_carrier := () run_cmd squeeze_loc_attr.set ``squeeze_loc_attr_carrier none tt /-- Format a list of arguments for use with `simp` and friends. This omits the list entirely if it is empty. -/ meta def render_simp_arg_list : list simp_arg_type → tactic format | [] := pure "" | args := (++) " " <$> to_line_wrap_format <$> args.mmap pp /-- Emit a suggestion to the user. If inside a `squeeze_scope` block, the suggestions emitted through `mk_suggestion` will be aggregated so that every tactic that makes a suggestion can consider multiple execution of the same invocation. If `at_pos` is true, make the suggestion at `p` instead of the current position. -/ meta def mk_suggestion (p : pos) (pre post : string) (args : list simp_arg_type) (at_pos := ff) : tactic unit := do xs ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier, match xs with | none := do args ← render_simp_arg_list args, if at_pos then @scope_trace _ p.line p.column $ λ _, _root_.trace sformat!"{pre}{args}{post}" (pure () : tactic unit) else trace sformat!"{pre}{args}{post}" | some xs := do squeeze_loc_attr.set ``squeeze_loc_attr_carrier ((p,pre,args,post) :: xs) ff end local postfix `?`:9001 := optional /-- translate a `pexpr` into a `simp` configuration -/ meta def parse_config : option pexpr → tactic (simp_config_ext × format) | none := pure ({}, "") | (some cfg) := do e ← to_expr ``(%%cfg : simp_config_ext), fmt ← has_to_tactic_format.to_tactic_format cfg, prod.mk <$> eval_expr simp_config_ext e <*> struct.to_tactic_format cfg /-- translate a `pexpr` into a `dsimp` configuration -/ meta def parse_dsimp_config : option pexpr → tactic (dsimp_config × format) | none := pure ({}, "") | (some cfg) := do e ← to_expr ``(%%cfg : simp_config_ext), fmt ← has_to_tactic_format.to_tactic_format cfg, prod.mk <$> eval_expr dsimp_config e <*> struct.to_tactic_format cfg /-- `same_result proof tac` runs tactic `tac` and checks if the proof produced by `tac` is equivalent to `proof`. -/ meta def same_result (pr : proof_state) (tac : tactic unit) : tactic bool := do s ← get_proof_state_after tac, pure $ some pr = s private meta def filter_simp_set_aux (tac : bool → list simp_arg_type → tactic unit) (args : list simp_arg_type) (pr : proof_state) : list simp_arg_type → list simp_arg_type → list simp_arg_type → tactic (list simp_arg_type × list simp_arg_type) | [] ys ds := pure (ys.reverse, ds.reverse) | (x :: xs) ys ds := do b ← same_result pr (tac tt (args ++ xs ++ ys)), if b then filter_simp_set_aux xs ys (x:: ds) else filter_simp_set_aux xs (x :: ys) ds declare_trace squeeze.deleted /-- `filter_simp_set g call_simp user_args simp_args` returns `args'` such that, when calling `call_simp tt /- only -/ args'` on the goal `g` (`g` is a meta var) we end up in the same state as if we had called `call_simp ff (user_args ++ simp_args)` and removing any one element of `args'` changes the resulting proof. -/ meta def filter_simp_set (tac : bool → list simp_arg_type → tactic unit) (user_args simp_args : list simp_arg_type) : tactic (list simp_arg_type) := do some s ← get_proof_state_after (tac ff (user_args ++ simp_args)), (simp_args', _) ← filter_simp_set_aux tac user_args s simp_args [] [], (user_args', ds) ← filter_simp_set_aux tac simp_args' s user_args [] [], when (is_trace_enabled_for `squeeze.deleted = tt ∧ ¬ ds.empty) trace!"deleting provided arguments {ds}", pure (user_args' ++ simp_args') /-- make a `simp_arg_type` that references the name given as an argument -/ meta def name.to_simp_args (n : name) : tactic simp_arg_type := do e ← resolve_name' n, pure $ simp_arg_type.expr e /-- tactic combinator to create a `simp`-like tactic that minimizes its argument list. * `slow`: adds all rfl-lemmas from the environment to the initial list (this is a slower but more accurate strategy) * `no_dflt`: did the user use the `only` keyword? * `args`: list of `simp` arguments * `tac`: how to invoke the underlying `simp` tactic -/ meta def squeeze_simp_core (slow no_dflt : bool) (args : list simp_arg_type) (tac : Π (no_dflt : bool) (args : list simp_arg_type), tactic unit) (mk_suggestion : list simp_arg_type → tactic unit) : tactic unit := do v ← target >>= mk_meta_var, args ← if slow then do simp_set ← attribute.get_instances `simp, simp_set ← simp_set.mfilter $ has_attribute' `_refl_lemma, simp_set ← simp_set.mmap $ resolve_name' >=> pure ∘ simp_arg_type.expr, pure $ args ++ simp_set else pure args, g ← retrieve $ do { g ← main_goal, tac no_dflt args, instantiate_mvars g }, let vs := g.list_constant, vs ← vs.mfilter is_simp_lemma, vs ← vs.mmap strip_prefix, vs ← vs.to_list.mmap name.to_simp_args, with_local_goals' [v] (filter_simp_set tac args vs) >>= mk_suggestion, tac no_dflt args namespace interactive attribute [derive decidable_eq] simp_arg_type /-- Turn a `simp_arg_type` into a string. -/ meta instance simp_arg_type.has_to_string : has_to_string simp_arg_type := ⟨λ a, match a with | simp_arg_type.all_hyps := "*" | (simp_arg_type.except n) := "-" ++ to_string n | (simp_arg_type.expr e) := to_string e | (simp_arg_type.symm_expr e) := "←" ++ to_string e end⟩ /-- combinator meant to aggregate the suggestions issued by multiple calls of `squeeze_simp` (due, for instance, to `;`). Can be used as: ```lean example {α β} (xs ys : list α) (f : α → β) : (xs ++ ys.tail).map f = xs.map f ∧ (xs.tail.map f).length = xs.length := begin have : xs = ys, admit, squeeze_scope { split; squeeze_simp, -- `squeeze_simp` is run twice, the first one requires -- `list.map_append` and the second one -- `[list.length_map, list.length_tail]` -- prints only one message and combine the suggestions: -- > Try this: simp only [list.length_map, list.length_tail, list.map_append] squeeze_simp [this] -- `squeeze_simp` is run only once -- prints: -- > Try this: simp only [this] }, end ``` -/ meta def squeeze_scope (tac : itactic) : tactic unit := do none ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier | pure (), squeeze_loc_attr.set ``squeeze_loc_attr_carrier (some []) ff, finally tac $ do some xs ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier | fail "invalid state", let m := native.rb_lmap.of_list xs, squeeze_loc_attr.set ``squeeze_loc_attr_carrier none ff, m.to_list.reverse.mmap' $ λ ⟨p,suggs⟩, do { let ⟨pre,_,post⟩ := suggs.head, let suggs : list (list simp_arg_type) := suggs.map $ prod.fst ∘ prod.snd, mk_suggestion p pre post (suggs.foldl list.union []) tt, pure () } /-- `squeeze_simp`, `squeeze_simpa` and `squeeze_dsimp` perform the same task with the difference that `squeeze_simp` relates to `simp` while `squeeze_simpa` relates to `simpa` and `squeeze_dsimp` relates to `dsimp`. The following applies to `squeeze_simp`, `squeeze_simpa` and `squeeze_dsimp`. `squeeze_simp` behaves like `simp` (including all its arguments) and prints a `simp only` invocation to skip the search through the `simp` lemma list. For instance, the following is easily solved with `simp`: ```lean example : 0 + 1 = 1 + 0 := by simp ``` To guide the proof search and speed it up, we may replace `simp` with `squeeze_simp`: ```lean example : 0 + 1 = 1 + 0 := by squeeze_simp -- prints: -- Try this: simp only [add_zero, eq_self_iff_true, zero_add] ``` `squeeze_simp` suggests a replacement which we can use instead of `squeeze_simp`. ```lean example : 0 + 1 = 1 + 0 := by simp only [add_zero, eq_self_iff_true, zero_add] ``` `squeeze_simp only` prints nothing as it already skips the `simp` list. This tactic is useful for speeding up the compilation of a complete file. Steps: 1. search and replace ` simp` with ` squeeze_simp` (the space helps avoid the replacement of `simp` in `@[simp]`) throughout the file. 2. Starting at the beginning of the file, go to each printout in turn, copy the suggestion in place of `squeeze_simp`. 3. after all the suggestions were applied, search and replace `squeeze_simp` with `simp` to remove the occurrences of `squeeze_simp` that did not produce a suggestion. Known limitation(s): * in cases where `squeeze_simp` is used after a `;` (e.g. `cases x; squeeze_simp`), `squeeze_simp` will produce as many suggestions as the number of goals it is applied to. It is likely that none of the suggestion is a good replacement but they can all be combined by concatenating their list of lemmas. `squeeze_scope` can be used to combine the suggestions: `by squeeze_scope { cases x; squeeze_simp }` * sometimes, `simp` lemmas are also `_refl_lemma` and they can be used without appearing in the resulting proof. `squeeze_simp` won't know to try that lemma unless it is called as `squeeze_simp?` -/ meta def squeeze_simp (key : parse cur_pos) (slow_and_accurate : parse (tk "?")?) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_config cfg, squeeze_simp_core slow_and_accurate.is_some no_dflt hs (λ l_no_dft l_args, simp use_iota_eqn none l_no_dft l_args attr_names locat cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), loc := loc.to_string locat in mk_suggestion (key.move_left 1) sformat!"Try this: simp{use_iota_eqn} only" sformat!"{attrs}{loc}{c}" args) /-- see `squeeze_simp` -/ meta def squeeze_simpa (key : parse cur_pos) (slow_and_accurate : parse (tk "?")?) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (tgt : parse (tk "using" *> texpr)?) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_config cfg, tgt' ← traverse (λ t, do t ← to_expr t >>= pp, pure format!" using {t}") tgt, squeeze_simp_core slow_and_accurate.is_some no_dflt hs (λ l_no_dft l_args, simpa use_iota_eqn none l_no_dft l_args attr_names tgt cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), tgt' := tgt'.get_or_else "" in mk_suggestion (key.move_left 1) sformat!"Try this: simpa{use_iota_eqn} only" sformat!"{attrs}{tgt'}{c}" args) /-- `squeeze_dsimp` behaves like `dsimp` (including all its arguments) and prints a `dsimp only` invocation to skip the search through the `simp` lemma list. See the doc string of `squeeze_simp` for examples. -/ meta def squeeze_dsimp (key : parse cur_pos) (slow_and_accurate : parse (tk "?")?) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_dsimp_config cfg, squeeze_simp_core slow_and_accurate.is_some no_dflt hs (λ l_no_dft l_args, dsimp l_no_dft l_args attr_names locat cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), loc := loc.to_string locat in mk_suggestion (key.move_left 1) sformat!"Try this: dsimp{use_iota_eqn} only" sformat!"{attrs}{loc}{c}" args) end interactive end tactic open tactic.interactive add_tactic_doc { name := "squeeze_simp / squeeze_simpa / squeeze_dsimp / squeeze_scope", category := doc_category.tactic, decl_names := [``squeeze_simp, ``squeeze_dsimp, ``squeeze_simpa, ``squeeze_scope], tags := ["simplification", "Try this"], inherit_description_from := ``squeeze_simp }
e17f07e3d8663bc834e9d064394cecea05ef6581
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/string_imp2.lean
98c1b58014ce7cf046ff464b917d12c94e3b179a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,052
lean
def f (s : String) : String := s ++ " " ++ s def g (s : String) : String := s.push ' ' ++ s.push '-' def h (s : String) : String := let it₁ := s.mkIterator; let it₂ := it₁.next; it₁.remainingToString ++ "-" ++ it₂.remainingToString #eval "hello" ++ "hello" #eval f "hello" #eval (f "αβ").length #eval "hello".toList #eval "αβ".toList #eval "".toList #eval "αβγ".toList #eval "αβγ".mkIterator.1 #eval "αβγ".mkIterator.next.1 #eval "αβγ".mkIterator.next.next.1 #eval "αβγ".mkIterator.next.2 #eval "αβ".1 #eval "αβ".push 'a' #eval g "α" #eval "".mkIterator.curr #eval ("αβγ".mkIterator.setCurr 'a').toString #eval (("αβγ".mkIterator.setCurr 'a').next.setCurr 'b').toString #eval ((("αβγ".mkIterator.setCurr 'a').next.setCurr 'b').next.setCurr 'c').toString #eval ((("αβγ".mkIterator.setCurr 'a').next.setCurr 'b').prev.setCurr 'c').toString #eval ("abc".mkIterator.setCurr '0').toString #eval (("abc".mkIterator.setCurr '0').next.setCurr '1').toString #eval ((("abc".mkIterator.setCurr '0').next.setCurr '1').next.setCurr '2').toString #eval ((("abc".mkIterator.setCurr '0').next.setCurr '1').prev.setCurr '2').toString #eval ("abc".mkIterator.setCurr (Char.ofNat 955)).toString #eval h "abc" #eval "abc".mkIterator.remainingToString #eval ("a".push (Char.ofNat 0)) ++ "bb" #eval (("a".push (Char.ofNat 0)) ++ "αb").length #eval "".mkIterator.hasNext #eval "a".mkIterator.hasNext #eval "a".mkIterator.next.hasNext #eval "".mkIterator.hasPrev #eval "a".mkIterator.next.hasPrev #eval "αβ".mkIterator.next.hasPrev #eval "αβ".mkIterator.next.prev.hasPrev #eval "abc" == "abc" #eval "abc" == "abd" #eval "αβγ".drop 1 #eval "αβγ".takeRight 1 def ss : Substring := "0123abcdαβγδ".toSubstring #eval ss.drop 4 |>.takeRight 4 #eval ss.drop 4 |>.take 4 #eval ss.dropRight 4 |>.takeRight 4 def ssDots : Substring := "____abc.αβγ.123.____".toSubstring.extract 4 19 #eval ssDots.splitOn "." def ssHyphs : Substring := "____abc--αβγ--123--____".toSubstring.extract 4 22 #eval ssHyphs.splitOn "--"
d3d1ce0692f9ae12e9b54d96d5879c40df8a1ef8
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/data/complex/basic.lean
f91bb15b9fd89f6bbed4ad9bac7eb5022bf0a20f
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,621
lean
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Mario Carneiro -/ import data.real.sqrt open_locale big_operators /-! # The complex numbers The complex numbers are modelled as ℝ^2 in the obvious way. -/ /-! ### Definition and basic arithmmetic -/ /-- Complex numbers consist of two `real`s: a real part `re` and an imaginary part `im`. -/ structure complex : Type := (re : ℝ) (im : ℝ) notation `ℂ` := complex namespace complex noncomputable instance : decidable_eq ℂ := classical.dec_eq _ /-- The equivalence between the complex numbers and `ℝ × ℝ`. -/ def equiv_real_prod : ℂ ≃ (ℝ × ℝ) := { to_fun := λ z, ⟨z.re, z.im⟩, inv_fun := λ p, ⟨p.1, p.2⟩, left_inv := λ ⟨x, y⟩, rfl, right_inv := λ ⟨x, y⟩, rfl } @[simp] theorem equiv_real_prod_apply (z : ℂ) : equiv_real_prod z = (z.re, z.im) := rfl theorem equiv_real_prod_symm_re (x y : ℝ) : (equiv_real_prod.symm (x, y)).re = x := rfl theorem equiv_real_prod_symm_im (x y : ℝ) : (equiv_real_prod.symm (x, y)).im = y := rfl @[simp] theorem eta : ∀ z : ℂ, complex.mk z.re z.im = z | ⟨a, b⟩ := rfl @[ext] theorem ext : ∀ {z w : ℂ}, z.re = w.re → z.im = w.im → z = w | ⟨zr, zi⟩ ⟨_, _⟩ rfl rfl := rfl theorem ext_iff {z w : ℂ} : z = w ↔ z.re = w.re ∧ z.im = w.im := ⟨λ H, by simp [H], and.rec ext⟩ instance : has_coe ℝ ℂ := ⟨λ r, ⟨r, 0⟩⟩ @[simp, norm_cast] lemma of_real_re (r : ℝ) : (r : ℂ).re = r := rfl @[simp, norm_cast] lemma of_real_im (r : ℝ) : (r : ℂ).im = 0 := rfl @[simp, norm_cast] theorem of_real_inj {z w : ℝ} : (z : ℂ) = w ↔ z = w := ⟨congr_arg re, congr_arg _⟩ instance : has_zero ℂ := ⟨(0 : ℝ)⟩ instance : inhabited ℂ := ⟨0⟩ @[simp] lemma zero_re : (0 : ℂ).re = 0 := rfl @[simp] lemma zero_im : (0 : ℂ).im = 0 := rfl @[simp, norm_cast] lemma of_real_zero : ((0 : ℝ) : ℂ) = 0 := rfl @[simp] theorem of_real_eq_zero {z : ℝ} : (z : ℂ) = 0 ↔ z = 0 := of_real_inj theorem of_real_ne_zero {z : ℝ} : (z : ℂ) ≠ 0 ↔ z ≠ 0 := not_congr of_real_eq_zero instance : has_one ℂ := ⟨(1 : ℝ)⟩ @[simp] lemma one_re : (1 : ℂ).re = 1 := rfl @[simp] lemma one_im : (1 : ℂ).im = 0 := rfl @[simp, norm_cast] lemma of_real_one : ((1 : ℝ) : ℂ) = 1 := rfl instance : has_add ℂ := ⟨λ z w, ⟨z.re + w.re, z.im + w.im⟩⟩ @[simp] lemma add_re (z w : ℂ) : (z + w).re = z.re + w.re := rfl @[simp] lemma add_im (z w : ℂ) : (z + w).im = z.im + w.im := rfl @[simp] lemma bit0_re (z : ℂ) : (bit0 z).re = bit0 z.re := rfl @[simp] lemma bit1_re (z : ℂ) : (bit1 z).re = bit1 z.re := rfl @[simp] lemma bit0_im (z : ℂ) : (bit0 z).im = bit0 z.im := eq.refl _ @[simp] lemma bit1_im (z : ℂ) : (bit1 z).im = bit0 z.im := add_zero _ @[simp, norm_cast] lemma of_real_add (r s : ℝ) : ((r + s : ℝ) : ℂ) = r + s := ext_iff.2 $ by simp @[simp, norm_cast] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : ℂ) = bit0 r := ext_iff.2 $ by simp [bit0] @[simp, norm_cast] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : ℂ) = bit1 r := ext_iff.2 $ by simp [bit1] instance : has_neg ℂ := ⟨λ z, ⟨-z.re, -z.im⟩⟩ @[simp] lemma neg_re (z : ℂ) : (-z).re = -z.re := rfl @[simp] lemma neg_im (z : ℂ) : (-z).im = -z.im := rfl @[simp, norm_cast] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : ℂ) = -r := ext_iff.2 $ by simp instance : has_sub ℂ := ⟨λ z w, ⟨z.re - w.re, z.im - w.im⟩⟩ instance : has_mul ℂ := ⟨λ z w, ⟨z.re * w.re - z.im * w.im, z.re * w.im + z.im * w.re⟩⟩ @[simp] lemma mul_re (z w : ℂ) : (z * w).re = z.re * w.re - z.im * w.im := rfl @[simp] lemma mul_im (z w : ℂ) : (z * w).im = z.re * w.im + z.im * w.re := rfl @[simp, norm_cast] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : ℂ) = r * s := ext_iff.2 $ by simp lemma smul_re (r : ℝ) (z : ℂ) : (↑r * z).re = r * z.re := by simp lemma smul_im (r : ℝ) (z : ℂ) : (↑r * z).im = r * z.im := by simp lemma of_real_smul (r : ℝ) (z : ℂ) : (↑r * z) = ⟨r * z.re, r * z.im⟩ := ext (smul_re _ _) (smul_im _ _) /-! ### The imaginary unit, `I` -/ /-- The imaginary unit. -/ def I : ℂ := ⟨0, 1⟩ @[simp] lemma I_re : I.re = 0 := rfl @[simp] lemma I_im : I.im = 1 := rfl @[simp] lemma I_mul_I : I * I = -1 := ext_iff.2 $ by simp lemma I_mul (z : ℂ) : I * z = ⟨-z.im, z.re⟩ := ext_iff.2 $ by simp lemma I_ne_zero : (I : ℂ) ≠ 0 := mt (congr_arg im) zero_ne_one.symm lemma mk_eq_add_mul_I (a b : ℝ) : complex.mk a b = a + b * I := ext_iff.2 $ by simp @[simp] lemma re_add_im (z : ℂ) : (z.re : ℂ) + z.im * I = z := ext_iff.2 $ by simp /-! ### Commutative ring instance and lemmas -/ instance : comm_ring ℂ := by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, one := 1, mul := (*), sub_eq_add_neg := _, ..}; { intros, apply ext_iff.2; split; simp; ring } instance re.is_add_group_hom : is_add_group_hom complex.re := { map_add := complex.add_re } instance im.is_add_group_hom : is_add_group_hom complex.im := { map_add := complex.add_im } @[simp] lemma I_pow_bit0 (n : ℕ) : I ^ (bit0 n) = (-1) ^ n := by rw [pow_bit0', I_mul_I] @[simp] lemma I_pow_bit1 (n : ℕ) : I ^ (bit1 n) = (-1) ^ n * I := by rw [pow_bit1', I_mul_I] /-! ### Complex conjugation -/ /-- The complex conjugate. -/ def conj : ℂ →+* ℂ := begin refine_struct { to_fun := λ z : ℂ, (⟨z.re, -z.im⟩ : ℂ), .. }; { intros, ext; simp [add_comm], }, end @[simp] lemma conj_re (z : ℂ) : (conj z).re = z.re := rfl @[simp] lemma conj_im (z : ℂ) : (conj z).im = -z.im := rfl @[simp] lemma conj_of_real (r : ℝ) : conj r = r := ext_iff.2 $ by simp [conj] @[simp] lemma conj_I : conj I = -I := ext_iff.2 $ by simp @[simp] lemma conj_bit0 (z : ℂ) : conj (bit0 z) = bit0 (conj z) := ext_iff.2 $ by simp [bit0] @[simp] lemma conj_bit1 (z : ℂ) : conj (bit1 z) = bit1 (conj z) := ext_iff.2 $ by simp [bit0] @[simp] lemma conj_neg_I : conj (-I) = I := ext_iff.2 $ by simp @[simp] lemma conj_conj (z : ℂ) : conj (conj z) = z := ext_iff.2 $ by simp lemma conj_involutive : function.involutive conj := conj_conj lemma conj_bijective : function.bijective conj := conj_involutive.bijective lemma conj_inj {z w : ℂ} : conj z = conj w ↔ z = w := conj_bijective.1.eq_iff @[simp] lemma conj_eq_zero {z : ℂ} : conj z = 0 ↔ z = 0 := by simpa using @conj_inj z 0 lemma eq_conj_iff_real {z : ℂ} : conj z = z ↔ ∃ r : ℝ, z = r := ⟨λ h, ⟨z.re, ext rfl $ eq_zero_of_neg_eq (congr_arg im h)⟩, λ ⟨h, e⟩, by rw [e, conj_of_real]⟩ lemma eq_conj_iff_re {z : ℂ} : conj z = z ↔ (z.re : ℂ) = z := eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, λ h, ⟨_, h.symm⟩⟩ instance : star_ring ℂ := { star := λ z, conj z, star_involutive := λ z, by simp, star_mul := λ r s, by { ext; simp [mul_comm], }, star_add := by simp, } /-! ### Norm squared -/ /-- The norm squared function. -/ @[pp_nodot] def norm_sq : monoid_with_zero_hom ℂ ℝ := { to_fun := λ z, z.re * z.re + z.im * z.im, map_zero' := by simp, map_one' := by simp, map_mul' := λ z w, by { dsimp, ring } } lemma norm_sq_apply (z : ℂ) : norm_sq z = z.re * z.re + z.im * z.im := rfl @[simp] lemma norm_sq_of_real (r : ℝ) : norm_sq r = r * r := by simp [norm_sq] lemma norm_sq_zero : norm_sq 0 = 0 := norm_sq.map_zero lemma norm_sq_one : norm_sq 1 = 1 := norm_sq.map_one @[simp] lemma norm_sq_I : norm_sq I = 1 := by simp [norm_sq] lemma norm_sq_nonneg (z : ℂ) : 0 ≤ norm_sq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) lemma norm_sq_eq_zero {z : ℂ} : norm_sq z = 0 ↔ z = 0 := ⟨λ h, ext (eq_zero_of_mul_self_add_mul_self_eq_zero h) (eq_zero_of_mul_self_add_mul_self_eq_zero $ (add_comm _ _).trans h), λ h, h.symm ▸ norm_sq_zero⟩ @[simp] lemma norm_sq_pos {z : ℂ} : 0 < norm_sq z ↔ z ≠ 0 := (norm_sq_nonneg z).lt_iff_ne.trans $ not_congr (eq_comm.trans norm_sq_eq_zero) @[simp] lemma norm_sq_neg (z : ℂ) : norm_sq (-z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_conj (z : ℂ) : norm_sq (conj z) = norm_sq z := by simp [norm_sq] lemma norm_sq_mul (z w : ℂ) : norm_sq (z * w) = norm_sq z * norm_sq w := norm_sq.map_mul z w lemma norm_sq_add (z w : ℂ) : norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (z * conj w).re := by dsimp [norm_sq]; ring lemma re_sq_le_norm_sq (z : ℂ) : z.re * z.re ≤ norm_sq z := le_add_of_nonneg_right (mul_self_nonneg _) lemma im_sq_le_norm_sq (z : ℂ) : z.im * z.im ≤ norm_sq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : ℂ) : z * conj z = norm_sq z := ext_iff.2 $ by simp [norm_sq, mul_comm, sub_eq_neg_add, add_comm] theorem add_conj (z : ℂ) : z + conj z = (2 * z.re : ℝ) := ext_iff.2 $ by simp [two_mul] /-- The coercion `ℝ → ℂ` as a `ring_hom`. -/ def of_real : ℝ →+* ℂ := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩ @[simp] lemma of_real_eq_coe (r : ℝ) : of_real r = r := rfl @[simp] lemma I_sq : I ^ 2 = -1 := by rw [pow_two, I_mul_I] @[simp] lemma sub_re (z w : ℂ) : (z - w).re = z.re - w.re := rfl @[simp] lemma sub_im (z w : ℂ) : (z - w).im = z.im - w.im := rfl @[simp, norm_cast] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : ℂ) = r - s := ext_iff.2 $ by simp @[simp, norm_cast] lemma of_real_pow (r : ℝ) (n : ℕ) : ((r ^ n : ℝ) : ℂ) = r ^ n := by induction n; simp [*, of_real_mul, pow_succ] theorem sub_conj (z : ℂ) : z - conj z = (2 * z.im : ℝ) * I := ext_iff.2 $ by simp [two_mul, sub_eq_add_neg] lemma norm_sq_sub (z w : ℂ) : norm_sq (z - w) = norm_sq z + norm_sq w - 2 * (z * conj w).re := by rw [sub_eq_add_neg, norm_sq_add]; simp [-mul_re, add_comm, add_left_comm, sub_eq_add_neg] /-! ### Inversion -/ noncomputable instance : has_inv ℂ := ⟨λ z, conj z * ((norm_sq z)⁻¹:ℝ)⟩ theorem inv_def (z : ℂ) : z⁻¹ = conj z * ((norm_sq z)⁻¹:ℝ) := rfl @[simp] lemma inv_re (z : ℂ) : (z⁻¹).re = z.re / norm_sq z := by simp [inv_def, division_def] @[simp] lemma inv_im (z : ℂ) : (z⁻¹).im = -z.im / norm_sq z := by simp [inv_def, division_def] @[simp, norm_cast] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : ℂ) = r⁻¹ := ext_iff.2 $ by simp protected lemma inv_zero : (0⁻¹ : ℂ) = 0 := by rw [← of_real_zero, ← of_real_inv, inv_zero] protected theorem mul_inv_cancel {z : ℂ} (h : z ≠ 0) : z * z⁻¹ = 1 := by rw [inv_def, ← mul_assoc, mul_conj, ← of_real_mul, mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one] /-! ### Field instance and lemmas -/ noncomputable instance : field ℂ := { inv := has_inv.inv, exists_pair_ne := ⟨0, 1, mt (congr_arg re) zero_ne_one⟩, mul_inv_cancel := @complex.mul_inv_cancel, inv_zero := complex.inv_zero, ..complex.comm_ring } @[simp] lemma I_fpow_bit0 (n : ℤ) : I ^ (bit0 n) = (-1) ^ n := by rw [fpow_bit0', I_mul_I] @[simp] lemma I_fpow_bit1 (n : ℤ) : I ^ (bit1 n) = (-1) ^ n * I := by rw [fpow_bit1', I_mul_I] lemma div_re (z w : ℂ) : (z / w).re = z.re * w.re / norm_sq w + z.im * w.im / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg] lemma div_im (z w : ℂ) : (z / w).im = z.im * w.re / norm_sq w - z.re * w.im / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm] @[simp, norm_cast] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : ℂ) = r / s := of_real.map_div r s @[simp, norm_cast] lemma of_real_fpow (r : ℝ) (n : ℤ) : ((r ^ n : ℝ) : ℂ) = (r : ℂ) ^ n := of_real.map_fpow r n @[simp] lemma div_I (z : ℂ) : z / I = -(z * I) := (div_eq_iff_mul_eq I_ne_zero).2 $ by simp [mul_assoc] @[simp] lemma inv_I : I⁻¹ = -I := by simp [inv_eq_one_div] @[simp] lemma norm_sq_inv (z : ℂ) : norm_sq z⁻¹ = (norm_sq z)⁻¹ := norm_sq.map_inv' z @[simp] lemma norm_sq_div (z w : ℂ) : norm_sq (z / w) = norm_sq z / norm_sq w := norm_sq.map_div z w /-! ### Cast lemmas -/ @[simp, norm_cast] theorem of_real_nat_cast (n : ℕ) : ((n : ℝ) : ℂ) = n := of_real.map_nat_cast n @[simp, norm_cast] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] @[simp, norm_cast] lemma nat_cast_im (n : ℕ) : (n : ℂ).im = 0 := by rw [← of_real_nat_cast, of_real_im] @[simp, norm_cast] theorem of_real_int_cast (n : ℤ) : ((n : ℝ) : ℂ) = n := of_real.map_int_cast n @[simp, norm_cast] lemma int_cast_re (n : ℤ) : (n : ℂ).re = n := by rw [← of_real_int_cast, of_real_re] @[simp, norm_cast] lemma int_cast_im (n : ℤ) : (n : ℂ).im = 0 := by rw [← of_real_int_cast, of_real_im] @[simp, norm_cast] theorem of_real_rat_cast (n : ℚ) : ((n : ℝ) : ℂ) = n := of_real.map_rat_cast n @[simp, norm_cast] lemma rat_cast_re (q : ℚ) : (q : ℂ).re = q := by rw [← of_real_rat_cast, of_real_re] @[simp, norm_cast] lemma rat_cast_im (q : ℚ) : (q : ℂ).im = 0 := by rw [← of_real_rat_cast, of_real_im] /-! ### Characteristic zero -/ instance char_zero_complex : char_zero ℂ := char_zero_of_inj_zero $ λ n h, by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h /-- A complex number `z` plus its conjugate `conj z` is `2` times its real part. -/ theorem re_eq_add_conj (z : ℂ) : (z.re : ℂ) = (z + conj z) / 2 := by simp only [add_conj, of_real_mul, of_real_one, of_real_bit0, mul_div_cancel_left (z.re:ℂ) two_ne_zero'] /-- A complex number `z` minus its conjugate `conj z` is `2i` times its imaginary part. -/ theorem im_eq_sub_conj (z : ℂ) : (z.im : ℂ) = (z - conj(z))/(2 * I) := by simp only [sub_conj, of_real_mul, of_real_one, of_real_bit0, mul_right_comm, mul_div_cancel_left _ (mul_ne_zero two_ne_zero' I_ne_zero : 2 * I ≠ 0)] /-! ### Absolute value -/ /-- The complex absolute value function, defined as the square root of the norm squared. -/ @[pp_nodot] noncomputable def abs (z : ℂ) : ℝ := (norm_sq z).sqrt local notation `abs'` := _root_.abs @[simp, norm_cast] lemma abs_of_real (r : ℝ) : abs r = abs' r := by simp [abs, norm_sq_of_real, real.sqrt_mul_self_eq_abs] lemma abs_of_nonneg {r : ℝ} (h : 0 ≤ r) : abs r = r := (abs_of_real _).trans (abs_of_nonneg h) lemma abs_of_nat (n : ℕ) : complex.abs n = n := calc complex.abs n = complex.abs (n:ℝ) : by rw [of_real_nat_cast] ... = _ : abs_of_nonneg (nat.cast_nonneg n) lemma mul_self_abs (z : ℂ) : abs z * abs z = norm_sq z := real.mul_self_sqrt (norm_sq_nonneg _) @[simp] lemma abs_zero : abs 0 = 0 := by simp [abs] @[simp] lemma abs_one : abs 1 = 1 := by simp [abs] @[simp] lemma abs_I : abs I = 1 := by simp [abs] @[simp] lemma abs_two : abs 2 = 2 := calc abs 2 = abs (2 : ℝ) : by rw [of_real_bit0, of_real_one] ... = (2 : ℝ) : abs_of_nonneg (by norm_num) lemma abs_nonneg (z : ℂ) : 0 ≤ abs z := real.sqrt_nonneg _ @[simp] lemma abs_eq_zero {z : ℂ} : abs z = 0 ↔ z = 0 := (real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero lemma abs_ne_zero {z : ℂ} : abs z ≠ 0 ↔ z ≠ 0 := not_congr abs_eq_zero @[simp] lemma abs_conj (z : ℂ) : abs (conj z) = abs z := by simp [abs] @[simp] lemma abs_mul (z w : ℂ) : abs (z * w) = abs z * abs w := by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl lemma abs_re_le_abs (z : ℂ) : abs' z.re ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.re) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply re_sq_le_norm_sq lemma abs_im_le_abs (z : ℂ) : abs' z.im ≤ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg z.im) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply im_sq_le_norm_sq lemma re_le_abs (z : ℂ) : z.re ≤ abs z := (abs_le.1 (abs_re_le_abs _)).2 lemma im_le_abs (z : ℂ) : z.im ≤ abs z := (abs_le.1 (abs_im_le_abs _)).2 lemma abs_add (z w : ℂ) : abs (z + w) ≤ abs z + abs w := (mul_self_le_mul_self_iff (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $ begin rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs, add_right_comm, norm_sq_add, add_le_add_iff_left, mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)], simpa [-mul_re] using re_le_abs (z * conj w) end instance : is_absolute_value abs := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value @[simp] lemma abs_abs (z : ℂ) : abs' (abs z) = abs z := _root_.abs_of_nonneg (abs_nonneg _) @[simp] lemma abs_pos {z : ℂ} : 0 < abs z ↔ z ≠ 0 := abv_pos abs @[simp] lemma abs_neg : ∀ z, abs (-z) = abs z := abv_neg abs lemma abs_sub : ∀ z w, abs (z - w) = abs (w - z) := abv_sub abs lemma abs_sub_le : ∀ a b c, abs (a - c) ≤ abs (a - b) + abs (b - c) := abv_sub_le abs @[simp] theorem abs_inv : ∀ z, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs @[simp] theorem abs_div : ∀ z w, abs (z / w) = abs z / abs w := abv_div abs lemma abs_abs_sub_le_abs_sub : ∀ z w, abs' (abs z - abs w) ≤ abs (z - w) := abs_abv_sub_le_abv_sub abs lemma abs_le_abs_re_add_abs_im (z : ℂ) : abs z ≤ abs' z.re + abs' z.im := by simpa [re_add_im] using abs_add z.re (z.im * I) lemma abs_re_div_abs_le_one (z : ℂ) : abs' (z.re / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] } lemma abs_im_div_abs_le_one (z : ℂ) : abs' (z.im / z.abs) ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else by { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] } @[simp, norm_cast] lemma abs_cast_nat (n : ℕ) : abs (n : ℂ) = n := by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)] @[simp, norm_cast] lemma int_cast_abs (n : ℤ) : ↑(abs' n) = abs n := by rw [← of_real_int_cast, abs_of_real, int.cast_abs] lemma norm_sq_eq_abs (x : ℂ) : norm_sq x = abs x ^ 2 := by rw [abs, pow_two, real.mul_self_sqrt (norm_sq_nonneg _)] /-! ### Cauchy sequences -/ theorem is_cau_seq_re (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).re) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij) theorem is_cau_seq_im (f : cau_seq ℂ abs) : is_cau_seq abs' (λ n, (f n).im) := λ ε ε0, (f.cauchy ε0).imp $ λ i H j ij, lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij) /-- The real part of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_re (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_re f⟩ /-- The imaginary part of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_im (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_im f⟩ lemma is_cau_seq_abs {f : ℕ → ℂ} (hf : is_cau_seq abs f) : is_cau_seq abs' (abs ∘ f) := λ ε ε0, let ⟨i, hi⟩ := hf ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩ /-- The limit of a Cauchy sequence of complex numbers. -/ noncomputable def lim_aux (f : cau_seq ℂ abs) : ℂ := ⟨cau_seq.lim (cau_seq_re f), cau_seq.lim (cau_seq_im f)⟩ theorem equiv_lim_aux (f : cau_seq ℂ abs) : f ≈ cau_seq.const abs (lim_aux f) := λ ε ε0, (exists_forall_ge_and (cau_seq.equiv_lim ⟨_, is_cau_seq_re f⟩ _ (half_pos ε0)) (cau_seq.equiv_lim ⟨_, is_cau_seq_im f⟩ _ (half_pos ε0))).imp $ λ i H j ij, begin cases H _ ij with H₁ H₂, apply lt_of_le_of_lt (abs_le_abs_re_add_abs_im _), dsimp [lim_aux] at *, have := add_lt_add H₁ H₂, rwa add_halves at this, end noncomputable instance : cau_seq.is_complete ℂ abs := ⟨λ f, ⟨lim_aux f, equiv_lim_aux f⟩⟩ open cau_seq lemma lim_eq_lim_im_add_lim_re (f : cau_seq ℂ abs) : lim f = ↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I := lim_eq_of_equiv_const $ calc f ≈ _ : equiv_lim_aux f ... = cau_seq.const abs (↑(lim (cau_seq_re f)) + ↑(lim (cau_seq_im f)) * I) : cau_seq.ext (λ _, complex.ext (by simp [lim_aux, cau_seq_re]) (by simp [lim_aux, cau_seq_im])) lemma lim_re (f : cau_seq ℂ abs) : lim (cau_seq_re f) = (lim f).re := by rw [lim_eq_lim_im_add_lim_re]; simp lemma lim_im (f : cau_seq ℂ abs) : lim (cau_seq_im f) = (lim f).im := by rw [lim_eq_lim_im_add_lim_re]; simp lemma is_cau_seq_conj (f : cau_seq ℂ abs) : is_cau_seq abs (λ n, conj (f n)) := λ ε ε0, let ⟨i, hi⟩ := f.2 ε ε0 in ⟨i, λ j hj, by rw [← conj.map_sub, abs_conj]; exact hi j hj⟩ /-- The complex conjugate of a complex Cauchy sequence, as a complex Cauchy sequence. -/ noncomputable def cau_seq_conj (f : cau_seq ℂ abs) : cau_seq ℂ abs := ⟨_, is_cau_seq_conj f⟩ lemma lim_conj (f : cau_seq ℂ abs) : lim (cau_seq_conj f) = conj (lim f) := complex.ext (by simp [cau_seq_conj, (lim_re _).symm, cau_seq_re]) (by simp [cau_seq_conj, (lim_im _).symm, cau_seq_im, (lim_neg _).symm]; refl) /-- The absolute value of a complex Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_abs (f : cau_seq ℂ abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_abs f.2⟩ lemma lim_abs (f : cau_seq ℂ abs) : lim (cau_seq_abs f) = abs (lim f) := lim_eq_of_equiv_const (λ ε ε0, let ⟨i, hi⟩ := equiv_lim f ε ε0 in ⟨i, λ j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩) @[simp, norm_cast] lemma of_real_prod {α : Type*} (s : finset α) (f : α → ℝ) : ((∏ i in s, f i : ℝ) : ℂ) = ∏ i in s, (f i : ℂ) := ring_hom.map_prod of_real _ _ @[simp, norm_cast] lemma of_real_sum {α : Type*} (s : finset α) (f : α → ℝ) : ((∑ i in s, f i : ℝ) : ℂ) = ∑ i in s, (f i : ℂ) := ring_hom.map_sum of_real _ _ end complex
004556086c2f7844c213069b990bf8f0b87759a0
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Init/Control/StateRef.lean
e9c6da9620580b67440becfbc01f7f1740cda163
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
2,412
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich The State monad transformer using IO references. -/ prelude import Init.System.IO import Init.Control.State def StateRefT' (ω : Type) (σ : Type) (m : Type → Type) (α : Type) : Type := ReaderT (ST.Ref ω σ) m α /- Recall that `StateRefT` is a macro that infers `ω` from the `m`. -/ @[inline] def StateRefT'.run {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m (α × σ) := do let ref ← ST.mkRef s let a ← x ref let s ← ref.get pure (a, s) @[inline] def StateRefT'.run' {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m α := do let (a, _) ← x.run s pure a namespace StateRefT' variable {ω σ : Type} {m : Type → Type} {α : Type} @[inline] protected def lift (x : m α) : StateRefT' ω σ m α := fun _ => x instance [Monad m] : Monad (StateRefT' ω σ m) := inferInstanceAs (Monad (ReaderT _ _)) instance : MonadLift m (StateRefT' ω σ m) := ⟨StateRefT'.lift⟩ instance (σ m) [Monad m] : MonadFunctor m (StateRefT' ω σ m) := inferInstanceAs (MonadFunctor m (ReaderT _ _)) @[inline] protected def get [Monad m] [MonadLiftT (ST ω) m] : StateRefT' ω σ m σ := fun ref => ref.get @[inline] protected def set [Monad m] [MonadLiftT (ST ω) m] (s : σ) : StateRefT' ω σ m PUnit := fun ref => ref.set s @[inline] protected def modifyGet [Monad m] [MonadLiftT (ST ω) m] (f : σ → α × σ) : StateRefT' ω σ m α := fun ref => ref.modifyGet f instance [MonadLiftT (ST ω) m] [Monad m] : MonadStateOf σ (StateRefT' ω σ m) := { get := StateRefT'.get set := StateRefT'.set modifyGet := StateRefT'.modifyGet } instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (StateRefT' ω σ m) := { throw := StateRefT'.lift ∘ throwThe ε tryCatch := fun x c s => tryCatchThe ε (x s) (fun e => c e s) } end StateRefT' instance (ω σ : Type) (m : Type → Type) : MonadControl m (StateRefT' ω σ m) := inferInstanceAs (MonadControl m (ReaderT _ _)) instance {m : Type → Type} {ω σ : Type} [MonadFinally m] [Monad m] : MonadFinally (StateRefT' ω σ m) := inferInstanceAs (MonadFinally (ReaderT _ _))
06e2fae1565a6baaac5162c6eedd7fe24290623b
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/adjunction/limits.lean
686648ef0b180b673d8d584b396b171b54e789d4
[ "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
13,158
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin -/ import category_theory.adjunction.basic import category_theory.limits.creates /-! # Adjunctions and limits A left adjoint preserves colimits (`category_theory.adjunction.left_adjoint_preserves_colimits`), and a right adjoint preserves limits (`category_theory.adjunction.right_adjoint_preserves_limits`). Equivalences create and reflect (co)limits. (`category_theory.adjunction.is_equivalence_creates_limits`, `category_theory.adjunction.is_equivalence_creates_colimits`, `category_theory.adjunction.is_equivalence_reflects_limits`, `category_theory.adjunction.is_equivalence_reflects_colimits`,) In `category_theory.adjunction.cocones_iso` we show that when `F ⊣ G`, the functor associating to each `Y` the cocones over `K ⋙ F` with cone point `Y` is naturally isomorphic to the functor associating to each `Y` the cocones over `K` with cone point `G.obj Y`. -/ open opposite namespace category_theory.adjunction open category_theory open category_theory.functor open category_theory.limits universes v u v₁ v₂ v₀ u₁ u₂ section arbitrary_universe variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) include adj section preservation_colimits variables {J : Type u} [category.{v} J] (K : J ⥤ C) /-- The right adjoint of `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ def functoriality_right_adjoint : cocone (K ⋙ F) ⥤ cocone K := (cocones.functoriality _ G) ⋙ (cocones.precompose (K.right_unitor.inv ≫ (whisker_left K adj.unit) ≫ (associator _ _ _).inv)) local attribute [reducible] functoriality_right_adjoint /-- The unit for the adjunction for `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ @[simps] def functoriality_unit : 𝟭 (cocone K) ⟶ cocones.functoriality _ F ⋙ functoriality_right_adjoint adj K := { app := λ c, { hom := adj.unit.app c.X } } /-- The counit for the adjunction for `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ @[simps] def functoriality_counit : functoriality_right_adjoint adj K ⋙ cocones.functoriality _ F ⟶ 𝟭 (cocone (K ⋙ F)) := { app := λ c, { hom := adj.counit.app c.X } } /-- The functor `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)` is a left adjoint. -/ def functoriality_is_left_adjoint : is_left_adjoint (cocones.functoriality K F) := { right := functoriality_right_adjoint adj K, adj := mk_of_unit_counit { unit := functoriality_unit adj K, counit := functoriality_counit adj K } } /-- A left adjoint preserves colimits. See <https://stacks.math.columbia.edu/tag/0038>. -/ def left_adjoint_preserves_colimits : preserves_colimits_of_size.{v u} F := { preserves_colimits_of_shape := λ J 𝒥, { preserves_colimit := λ F, by exactI { preserves := λ c hc, is_colimit.iso_unique_cocone_morphism.inv (λ s, @equiv.unique _ _ (is_colimit.iso_unique_cocone_morphism.hom hc _) (((adj.functoriality_is_left_adjoint _).adj).hom_equiv _ _)) } } }. omit adj @[priority 100] -- see Note [lower instance priority] instance is_equivalence_preserves_colimits (E : C ⥤ D) [is_equivalence E] : preserves_colimits_of_size.{v u} E := left_adjoint_preserves_colimits E.adjunction @[priority 100] -- see Note [lower instance priority] instance is_equivalence_reflects_colimits (E : D ⥤ C) [is_equivalence E] : reflects_colimits_of_size.{v u} E := { reflects_colimits_of_shape := λ J 𝒥, by exactI { reflects_colimit := λ K, { reflects := λ c t, begin have l := (is_colimit_of_preserves E.inv t).map_cocone_equiv E.as_equivalence.unit_iso.symm, refine (((is_colimit.precompose_inv_equiv K.right_unitor _).symm) l).of_iso_colimit _, tidy, end } } } @[priority 100] -- see Note [lower instance priority] instance is_equivalence_creates_colimits (H : D ⥤ C) [is_equivalence H] : creates_colimits_of_size.{v u} H := { creates_colimits_of_shape := λ J 𝒥, by exactI { creates_colimit := λ F, { lifts := λ c t, { lifted_cocone := H.map_cocone_inv c, valid_lift := H.map_cocone_map_cocone_inv c } } } } -- verify the preserve_colimits instance works as expected: example (E : C ⥤ D) [is_equivalence E] (c : cocone K) (h : is_colimit c) : is_colimit (E.map_cocone c) := preserves_colimit.preserves h lemma has_colimit_comp_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimit K] : has_colimit (K ⋙ E) := has_colimit.mk { cocone := E.map_cocone (colimit.cocone K), is_colimit := preserves_colimit.preserves (colimit.is_colimit K) } lemma has_colimit_of_comp_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimit (K ⋙ E)] : has_colimit K := @has_colimit_of_iso _ _ _ _ (K ⋙ E ⋙ inv E) K (@has_colimit_comp_equivalence _ _ _ _ _ _ (K ⋙ E) (inv E) _ _) ((functor.right_unitor _).symm ≪≫ iso_whisker_left K (E.as_equivalence.unit_iso)) /-- Transport a `has_colimits_of_shape` instance across an equivalence. -/ lemma has_colimits_of_shape_of_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimits_of_shape J D] : has_colimits_of_shape J C := ⟨λ F, by exactI has_colimit_of_comp_equivalence F E⟩ /-- Transport a `has_colimits` instance across an equivalence. -/ lemma has_colimits_of_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimits_of_size.{v u} D] : has_colimits_of_size.{v u} C := ⟨λ J hJ, by { exactI has_colimits_of_shape_of_equivalence E }⟩ end preservation_colimits section preservation_limits variables {J : Type u} [category.{v} J] (K : J ⥤ D) /-- The left adjoint of `cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ def functoriality_left_adjoint : cone (K ⋙ G) ⥤ cone K := (cones.functoriality _ F) ⋙ (cones.postcompose ((associator _ _ _).hom ≫ (whisker_left K adj.counit) ≫ K.right_unitor.hom)) local attribute [reducible] functoriality_left_adjoint /-- The unit for the adjunction for`cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ @[simps] def functoriality_unit' : 𝟭 (cone (K ⋙ G)) ⟶ functoriality_left_adjoint adj K ⋙ cones.functoriality _ G := { app := λ c, { hom := adj.unit.app c.X, } } /-- The counit for the adjunction for`cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ @[simps] def functoriality_counit' : cones.functoriality _ G ⋙ functoriality_left_adjoint adj K ⟶ 𝟭 (cone K) := { app := λ c, { hom := adj.counit.app c.X, } } /-- The functor `cones.functoriality K G : cone K ⥤ cone (K ⋙ G)` is a right adjoint. -/ def functoriality_is_right_adjoint : is_right_adjoint (cones.functoriality K G) := { left := functoriality_left_adjoint adj K, adj := mk_of_unit_counit { unit := functoriality_unit' adj K, counit := functoriality_counit' adj K } } /-- A right adjoint preserves limits. See <https://stacks.math.columbia.edu/tag/0038>. -/ def right_adjoint_preserves_limits : preserves_limits_of_size.{v u} G := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ K, by exactI { preserves := λ c hc, is_limit.iso_unique_cone_morphism.inv (λ s, @equiv.unique _ _ (is_limit.iso_unique_cone_morphism.hom hc _) (((adj.functoriality_is_right_adjoint _).adj).hom_equiv _ _).symm) } } }. omit adj @[priority 100] -- see Note [lower instance priority] instance is_equivalence_preserves_limits (E : D ⥤ C) [is_equivalence E] : preserves_limits_of_size.{v u} E := right_adjoint_preserves_limits E.inv.adjunction @[priority 100] -- see Note [lower instance priority] instance is_equivalence_reflects_limits (E : D ⥤ C) [is_equivalence E] : reflects_limits_of_size.{v u} E := { reflects_limits_of_shape := λ J 𝒥, by exactI { reflects_limit := λ K, { reflects := λ c t, begin have := (is_limit_of_preserves E.inv t).map_cone_equiv E.as_equivalence.unit_iso.symm, refine (((is_limit.postcompose_hom_equiv K.left_unitor _).symm) this).of_iso_limit _, tidy, end } } } @[priority 100] -- see Note [lower instance priority] instance is_equivalence_creates_limits (H : D ⥤ C) [is_equivalence H] : creates_limits_of_size.{v u} H := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ F, { lifts := λ c t, { lifted_cone := H.map_cone_inv c, valid_lift := H.map_cone_map_cone_inv c } } } } -- verify the preserve_limits instance works as expected: example (E : D ⥤ C) [is_equivalence E] (c : cone K) [h : is_limit c] : is_limit (E.map_cone c) := preserves_limit.preserves h lemma has_limit_comp_equivalence (E : D ⥤ C) [is_equivalence E] [has_limit K] : has_limit (K ⋙ E) := has_limit.mk { cone := E.map_cone (limit.cone K), is_limit := preserves_limit.preserves (limit.is_limit K) } lemma has_limit_of_comp_equivalence (E : D ⥤ C) [is_equivalence E] [has_limit (K ⋙ E)] : has_limit K := @has_limit_of_iso _ _ _ _ (K ⋙ E ⋙ inv E) K (@has_limit_comp_equivalence _ _ _ _ _ _ (K ⋙ E) (inv E) _ _) ((iso_whisker_left K E.as_equivalence.unit_iso.symm) ≪≫ (functor.right_unitor _)) /-- Transport a `has_limits_of_shape` instance across an equivalence. -/ lemma has_limits_of_shape_of_equivalence (E : D ⥤ C) [is_equivalence E] [has_limits_of_shape J C] : has_limits_of_shape J D := ⟨λ F, by exactI has_limit_of_comp_equivalence F E⟩ /-- Transport a `has_limits` instance across an equivalence. -/ lemma has_limits_of_equivalence (E : D ⥤ C) [is_equivalence E] [has_limits_of_size.{v u} C] : has_limits_of_size.{v u} D := ⟨λ J hJ, by exactI has_limits_of_shape_of_equivalence E⟩ end preservation_limits /-- auxiliary construction for `cocones_iso` -/ @[simps] def cocones_iso_component_hom {J : Type u} [category.{v} J] {K : J ⥤ C} (Y : D) (t : ((cocones J D).obj (op (K ⋙ F))).obj Y) : (G ⋙ (cocones J C).obj (op K)).obj Y := { app := λ j, (adj.hom_equiv (K.obj j) Y) (t.app j), naturality' := λ j j' f, by { erw [← adj.hom_equiv_naturality_left, t.naturality], dsimp, simp } } /-- auxiliary construction for `cocones_iso` -/ @[simps] def cocones_iso_component_inv {J : Type u} [category.{v} J] {K : J ⥤ C} (Y : D) (t : (G ⋙ (cocones J C).obj (op K)).obj Y) : ((cocones J D).obj (op (K ⋙ F))).obj Y := { app := λ j, (adj.hom_equiv (K.obj j) Y).symm (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm, t.naturality], dsimp, simp end } /-- auxiliary construction for `cones_iso` -/ @[simps] def cones_iso_component_hom {J : Type u} [category.{v} J] {K : J ⥤ D} (X : Cᵒᵖ) (t : (functor.op F ⋙ (cones J D).obj K).obj X) : ((cones J C).obj (K ⋙ G)).obj X := { app := λ j, (adj.hom_equiv (unop X) (K.obj j)) (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_right, ← t.naturality, category.id_comp, category.id_comp], refl end } /-- auxiliary construction for `cones_iso` -/ @[simps] def cones_iso_component_inv {J : Type u} [category.{v} J] {K : J ⥤ D} (X : Cᵒᵖ) (t : ((cones J C).obj (K ⋙ G)).obj X) : (functor.op F ⋙ (cones J D).obj K).obj X := { app := λ j, (adj.hom_equiv (unop X) (K.obj j)).symm (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_right_symm, ← t.naturality, category.id_comp, category.id_comp] end } end arbitrary_universe variables {C : Type u₁} [category.{v₀} C] {D : Type u₂} [category.{v₀} D] {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) /-- When `F ⊣ G`, the functor associating to each `Y` the cocones over `K ⋙ F` with cone point `Y` is naturally isomorphic to the functor associating to each `Y` the cocones over `K` with cone point `G.obj Y`. -/ -- Note: this is natural in K, but we do not yet have the tools to formulate that. def cocones_iso {J : Type u} [category.{v} J] {K : J ⥤ C} : (cocones J D).obj (op (K ⋙ F)) ≅ G ⋙ (cocones J C).obj (op K) := nat_iso.of_components (λ Y, { hom := cocones_iso_component_hom adj Y, inv := cocones_iso_component_inv adj Y, }) (by tidy) -- Note: this is natural in K, but we do not yet have the tools to formulate that. /-- When `F ⊣ G`, the functor associating to each `X` the cones over `K` with cone point `F.op.obj X` is naturally isomorphic to the functor associating to each `X` the cones over `K ⋙ G` with cone point `X`. -/ def cones_iso {J : Type u} [category.{v} J] {K : J ⥤ D} : F.op ⋙ (cones J D).obj K ≅ (cones J C).obj (K ⋙ G) := nat_iso.of_components (λ X, { hom := cones_iso_component_hom adj X, inv := cones_iso_component_inv adj X, } ) (by tidy) end category_theory.adjunction
f5488d7f630bdfcd025b5a2adf18c263104b1ccf
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/hott/types/nat/basic.hlean
8b80bfbd6f5eab2cc4edfc20e4ce0f483fea8d63
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,326
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. (Ported from standard library file data.nat.basic on May 02, 2015) Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad Basic operations on the natural numbers. -/ import algebra.ring open core prod binary namespace nat /- a variant of add, defined by recursion on the first argument -/ definition addl (x y : ℕ) : ℕ := nat.rec y (λ n r, succ r) x infix ` ⊕ `:65 := addl definition addl_succ_right (n m : ℕ) : n ⊕ succ m = succ (n ⊕ m) := nat.rec_on n rfl (λ n₁ ih, calc succ n₁ ⊕ succ m = succ (n₁ ⊕ succ m) : rfl ... = succ (succ (n₁ ⊕ m)) : ih ... = succ (succ n₁ ⊕ m) : rfl) definition add_eq_addl (x : ℕ) : ∀y, x + y = x ⊕ y := nat.rec_on x (λ y, nat.rec_on y rfl (λ y₁ ih, calc zero + succ y₁ = succ (zero + y₁) : rfl ... = succ (zero ⊕ y₁) : {ih} ... = zero ⊕ (succ y₁) : rfl)) (λ x₁ ih₁ y, nat.rec_on y (calc succ x₁ + zero = succ (x₁ + zero) : rfl ... = succ (x₁ ⊕ zero) : {ih₁ zero} ... = succ x₁ ⊕ zero : rfl) (λ y₁ ih₂, calc succ x₁ + succ y₁ = succ (succ x₁ + y₁) : rfl ... = succ (succ x₁ ⊕ y₁) : {ih₂} ... = succ x₁ ⊕ succ y₁ : addl_succ_right)) /- successor and predecessor -/ definition succ_ne_zero (n : ℕ) : succ n ≠ 0 := by contradiction -- add_rewrite succ_ne_zero definition pred_zero : pred 0 = 0 := rfl definition pred_succ (n : ℕ) : pred (succ n) = n := rfl definition eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ⊎ n = succ (pred n) := nat.rec_on n (sum.inl rfl) (take m IH, sum.inr rfl) definition exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : Σk : ℕ, n = succ k := sigma.mk _ (sum_resolve_right !eq_zero_or_eq_succ_pred H) definition succ.inj {n m : ℕ} (H : succ n = succ m) : n = m := lift.down (nat.no_confusion H (λe, e)) definition succ_ne_self {n : ℕ} : succ n ≠ n := nat.rec_on n (take H : 1 = 0, have ne : 1 ≠ 0, from !succ_ne_zero, absurd H ne) (take k IH H, IH (succ.inj H)) definition discriminate {B : Type} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := have H : n = n → B, from nat.cases_on n H1 H2, H rfl definition two_step_induction_on {P : ℕ → Type} (a : ℕ) (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a := have stronger : P a × P (succ a), from nat.rec_on a (pair H1 H2) (take k IH, have IH1 : P k, from pr1 IH, have IH2 : P (succ k), from pr2 IH, pair IH2 (H3 k IH1 IH2)), pr1 stronger definition sub_induction {P : ℕ → ℕ → Type} (n m : ℕ) (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : P n m := have general : ∀m, P n m, from nat.rec_on n (take m : ℕ, H1 m) (take k : ℕ, assume IH : ∀m, P k m, take m : ℕ, nat.cases_on m (H2 k) (take l, (H3 k l (IH l)))), general m /- addition -/ definition add_zero (n : ℕ) : n + 0 = n := rfl definition add_succ (n m : ℕ) : n + succ m = succ (n + m) := rfl definition zero_add (n : ℕ) : 0 + n = n := nat.rec_on n !add_zero (take m IH, show 0 + succ m = succ m, from calc 0 + succ m = succ (0 + m) : add_succ ... = succ m : IH) definition succ_add (n m : ℕ) : (succ n) + m = succ (n + m) := nat.rec_on m (rfl) (take k IH, eq.ap succ IH) definition add.comm (n m : ℕ) : n + m = m + n := nat.rec_on m (!add_zero ⬝ !zero_add⁻¹) (take k IH, calc n + succ k = succ (n+k) : add_succ ... = succ (k + n) : IH ... = succ k + n : succ_add) definition succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := !succ_add ⬝ !add_succ⁻¹ definition add.assoc (n m k : ℕ) : (n + m) + k = n + (m + k) := nat.rec_on k (!add_zero ▸ !add_zero) (take l IH, calc (n + m) + succ l = succ ((n + m) + l) : add_succ ... = succ (n + (m + l)) : IH ... = n + succ (m + l) : add_succ ... = n + (m + succ l) : add_succ) definition add.left_comm (n m k : ℕ) : n + (m + k) = m + (n + k) := left_comm add.comm add.assoc n m k definition add.right_comm (n m k : ℕ) : n + m + k = n + k + m := right_comm add.comm add.assoc n m k theorem add.comm4 : Π {n m k l : ℕ}, n + m + (k + l) = n + k + (m + l) := comm4 add.comm add.assoc definition add.cancel_left {n m k : ℕ} : n + m = n + k → m = k := nat.rec_on n (take H : 0 + m = 0 + k, !zero_add⁻¹ ⬝ H ⬝ !zero_add) (take (n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k), have H2 : succ (n + m) = succ (n + k), from calc succ (n + m) = succ n + m : succ_add ... = succ n + k : H ... = succ (n + k) : succ_add, have H3 : n + m = n + k, from succ.inj H2, IH H3) definition add.cancel_right {n m k : ℕ} (H : n + m = k + m) : n = k := have H2 : m + n = m + k, from !add.comm ⬝ H ⬝ !add.comm, add.cancel_left H2 definition eq_zero_of_add_eq_zero_right {n m : ℕ} : n + m = 0 → n = 0 := nat.rec_on n (take (H : 0 + m = 0), rfl) (take k IH, assume H : succ k + m = 0, absurd (show succ (k + m) = 0, from calc succ (k + m) = succ k + m : succ_add ... = 0 : H) !succ_ne_zero) definition eq_zero_of_add_eq_zero_left {n m : ℕ} (H : n + m = 0) : m = 0 := eq_zero_of_add_eq_zero_right (!add.comm ⬝ H) definition eq_zero_and_eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 × m = 0 := pair (eq_zero_of_add_eq_zero_right H) (eq_zero_of_add_eq_zero_left H) definition add_one (n : ℕ) : n + 1 = succ n := !add_zero ▸ !add_succ definition one_add (n : ℕ) : 1 + n = succ n := !zero_add ▸ !succ_add /- multiplication -/ definition mul_zero (n : ℕ) : n * 0 = 0 := rfl definition mul_succ (n m : ℕ) : n * succ m = n * m + n := rfl -- commutativity, distributivity, associativity, identity definition zero_mul (n : ℕ) : 0 * n = 0 := nat.rec_on n !mul_zero (take m IH, !mul_succ ⬝ !add_zero ⬝ IH) definition succ_mul (n m : ℕ) : (succ n) * m = (n * m) + m := nat.rec_on m (!mul_zero ⬝ !mul_zero⁻¹ ⬝ !add_zero⁻¹) (take k IH, calc succ n * succ k = succ n * k + succ n : mul_succ ... = n * k + k + succ n : IH ... = n * k + (k + succ n) : add.assoc ... = n * k + (succ n + k) : add.comm ... = n * k + (n + succ k) : succ_add_eq_succ_add ... = n * k + n + succ k : add.assoc ... = n * succ k + succ k : mul_succ) definition mul.comm (n m : ℕ) : n * m = m * n := nat.rec_on m (!mul_zero ⬝ !zero_mul⁻¹) (take k IH, calc n * succ k = n * k + n : mul_succ ... = k * n + n : IH ... = (succ k) * n : succ_mul) definition mul.right_distrib (n m k : ℕ) : (n + m) * k = n * k + m * k := nat.rec_on k (calc (n + m) * 0 = 0 : mul_zero ... = 0 + 0 : add_zero ... = n * 0 + 0 : mul_zero ... = n * 0 + m * 0 : mul_zero) (take l IH, calc (n + m) * succ l = (n + m) * l + (n + m) : mul_succ ... = n * l + m * l + (n + m) : IH ... = n * l + m * l + n + m : add.assoc ... = n * l + n + m * l + m : add.right_comm ... = n * l + n + (m * l + m) : add.assoc ... = n * succ l + (m * l + m) : mul_succ ... = n * succ l + m * succ l : mul_succ) definition mul.left_distrib (n m k : ℕ) : n * (m + k) = n * m + n * k := calc n * (m + k) = (m + k) * n : mul.comm ... = m * n + k * n : mul.right_distrib ... = n * m + k * n : mul.comm ... = n * m + n * k : mul.comm definition mul.assoc (n m k : ℕ) : (n * m) * k = n * (m * k) := nat.rec_on k (calc (n * m) * 0 = n * (m * 0) : mul_zero) (take l IH, calc (n * m) * succ l = (n * m) * l + n * m : mul_succ ... = n * (m * l) + n * m : IH ... = n * (m * l + m) : mul.left_distrib ... = n * (m * succ l) : mul_succ) definition mul_one (n : ℕ) : n * 1 = n := calc n * 1 = n * 0 + n : mul_succ ... = 0 + n : mul_zero ... = n : zero_add definition one_mul (n : ℕ) : 1 * n = n := calc 1 * n = n * 1 : mul.comm ... = n : mul_one definition eq_zero_or_eq_zero_of_mul_eq_zero {n m : ℕ} : n * m = 0 → n = 0 ⊎ m = 0 := nat.cases_on n (assume H, sum.inl rfl) (take n', nat.cases_on m (assume H, sum.inr rfl) (take m', assume H : succ n' * succ m' = 0, absurd ((calc 0 = succ n' * succ m' : H ... = succ n' * m' + succ n' : mul_succ ... = succ (succ n' * m' + n') : add_succ)⁻¹) !succ_ne_zero)) section open [classes] algebra protected definition comm_semiring [instance] [reducible] : algebra.comm_semiring nat := ⦃algebra.comm_semiring, add := add, add_assoc := add.assoc, zero := zero, zero_add := zero_add, add_zero := add_zero, add_comm := add.comm, mul := mul, mul_assoc := mul.assoc, one := succ zero, one_mul := one_mul, mul_one := mul_one, left_distrib := mul.left_distrib, right_distrib := mul.right_distrib, zero_mul := zero_mul, mul_zero := mul_zero, mul_comm := mul.comm, is_hset_carrier := is_hset_of_decidable_eq⦄ end section port_algebra open [classes] algebra definition mul.left_comm : ∀a b c : ℕ, a * (b * c) = b * (a * c) := algebra.mul.left_comm definition mul.right_comm : ∀a b c : ℕ, (a * b) * c = (a * c) * b := algebra.mul.right_comm definition dvd (a b : ℕ) : Type₀ := algebra.dvd a b notation a ∣ b := dvd a b definition dvd.intro : ∀{a b c : ℕ} (H : a * c = b), a ∣ b := @algebra.dvd.intro _ _ definition dvd.intro_left : ∀{a b c : ℕ} (H : c * a = b), a ∣ b := @algebra.dvd.intro_left _ _ definition exists_eq_mul_right_of_dvd : ∀{a b : ℕ} (H : a ∣ b), Σc, b = a * c := @algebra.exists_eq_mul_right_of_dvd _ _ definition dvd.elim : ∀{P : Type} {a b : ℕ} (H₁ : a ∣ b) (H₂ : ∀c, b = a * c → P), P := @algebra.dvd.elim _ _ definition exists_eq_mul_left_of_dvd : ∀{a b : ℕ} (H : a ∣ b), Σc, b = c * a := @algebra.exists_eq_mul_left_of_dvd _ _ definition dvd.elim_left : ∀{P : Type} {a b : ℕ} (H₁ : a ∣ b) (H₂ : ∀c, b = c * a → P), P := @algebra.dvd.elim_left _ _ definition dvd.refl : ∀a : ℕ, a ∣ a := algebra.dvd.refl definition dvd.trans : ∀{a b c : ℕ}, a ∣ b → b ∣ c → a ∣ c := @algebra.dvd.trans _ _ definition eq_zero_of_zero_dvd : ∀{a : ℕ}, 0 ∣ a → a = 0 := @algebra.eq_zero_of_zero_dvd _ _ definition dvd_zero : ∀a : ℕ, a ∣ 0 := algebra.dvd_zero definition one_dvd : ∀a : ℕ, 1 ∣ a := algebra.one_dvd definition dvd_mul_right : ∀a b : ℕ, a ∣ a * b := algebra.dvd_mul_right definition dvd_mul_left : ∀a b : ℕ, a ∣ b * a := algebra.dvd_mul_left definition dvd_mul_of_dvd_left : ∀{a b : ℕ} (H : a ∣ b) (c : ℕ), a ∣ b * c := @algebra.dvd_mul_of_dvd_left _ _ definition dvd_mul_of_dvd_right : ∀{a b : ℕ} (H : a ∣ b) (c : ℕ), a ∣ c * b := @algebra.dvd_mul_of_dvd_right _ _ definition mul_dvd_mul : ∀{a b c d : ℕ}, a ∣ b → c ∣ d → a * c ∣ b * d := @algebra.mul_dvd_mul _ _ definition dvd_of_mul_right_dvd : ∀{a b c : ℕ}, a * b ∣ c → a ∣ c := @algebra.dvd_of_mul_right_dvd _ _ definition dvd_of_mul_left_dvd : ∀{a b c : ℕ}, a * b ∣ c → b ∣ c := @algebra.dvd_of_mul_left_dvd _ _ definition dvd_add : ∀{a b c : ℕ}, a ∣ b → a ∣ c → a ∣ b + c := @algebra.dvd_add _ _ end port_algebra end nat
f706e22310488e539272f9c025f8baccee07ece6
93f5d951583b87344f929a9ec18c4f603d920c4a
/src/print_formula.lean
a91a90f494badd165d877e6301bd92272cdc9d67
[ "Apache-2.0" ]
permissive
jesse-michael-han/flypitch
824a8c1955deb661943b02146d281932dad39302
b9a25b75f6a6c69bd2913c66a8e32fc67f196335
refs/heads/master
1,625,819,158,405
1,595,824,334,000
1,595,824,334,000
167,225,933
1
0
null
1,548,265,151,000
1,548,265,151,000
null
UTF-8
Lean
false
false
3,422
lean
/- Copyright (c) 2019 The Flypitch Project. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Tindall, Jesse Han, Floris van Doorn -/ import .zfc open fol ZFC_rel ZFC_func -- ugly but working (str_formula says it's not well-founded recursion, but it evaluates anyways) meta def str_preterm : ∀ n m : ℕ, ℕ → bounded_preterm L_ZFC n m → string | n m z &k := "x" ++ to_string(z - k) | n m z (bd_func emptyset) := "∅" | n m z (bd_func pr) := "pair" | n m z (bd_func ω) := "ω" | n m z (bd_func P) := "𝒫" | n m z (bd_func Union) := "⋃" | n m z (bd_app t₁ t₂) := (str_preterm _ _ z t₁) ++ "(" ++ (str_preterm _ _ z t₂) ++ ")" meta def str_term : ∀ n : ℕ, ℕ → bounded_term L_ZFC n → string := λ n, str_preterm n 0 -- | n m &k := "x" ++ to_string(m - k.val) -- | n m (bd_func emptyset) := "∅" -- | _ _ (bd_func ω) := "ω" -- | n m (bd_app t₁ t₂) := (str_term (n+1) m t₁) ++ (str_term 0 m t₂) meta def str_preformula : ∀ n m : ℕ, ℕ → bounded_preformula L_ZFC n m → string | _ _ _ (bd_falsum ) := "⊥" | n m z (bd_equal a b) := str_preterm n m z a ++ " = " ++ str_preterm n m z b | n m z (a ⟹ b) := str_preformula n m z a ++ " ⟹ " ++ str_preformula n m z b | n m z (bd_rel _) := "∈" | n m z (bd_apprel a b) := str_preformula n (m+1) z a ++ "(" ++ str_term n z b ++ ")" | n m z (∀' t) := "(∀x" ++ to_string(z+1) ++ "," ++ str_preformula (n+1) m (z+1) t ++ ")" meta def str_formula : ∀ {n : ℕ}, bounded_formula L_ZFC n → ℕ → string -- := λ f n, str_preformula _ _ 0 f | n ((f₁ ⟹ (f₂ ⟹ bd_falsum)) ⟹ bd_falsum) m:= "(" ++ str_formula f₁ m ++ " ∧ " ++ str_formula f₂ m ++ ")" | n ((f₁ ⟹ bd_falsum) ⟹ f₂) m := "(" ++ str_formula f₁ m ++ " ∨ " ++ str_formula f₂ m ++ ")" | n (bd_equal s1 s2) m := "(" ++ str_term n m s1 ++ " = " ++ str_term n m s2 ++ ")" | n ((∀' (f ⟹ bd_falsum)) ⟹ bd_falsum) m := "∃x" ++ to_string(m + 1) ++ "," ++ str_formula f (m+1) | n (∀' f) m := "(∀x"++ to_string(m + 1) ++ "," ++ (str_formula f (m+1) ) ++ ")" | _ bd_falsum _ := "⊥" | n (f ⟹ bd_falsum) m := "¬ " ++ str_formula f m | n (bd_apprel (bd_apprel (bd_rel ((ε : L_ZFC.relations 2))) a) b) m := str_preterm _ _ m a ++ " ∈ " ++ str_preterm _ _ m b | n (bd_apprel (f₁) f₂) m := str_preformula n 1 m f₁ ++ "(" ++ str_term n m f₂ ++ ")" | n (bd_imp a b) m := "(" ++ str_formula a m ++ " ⟹ " ++ str_formula b m ++ ")" meta def print_formula : ∀ {n : ℕ}, bounded_formula L_ZFC n → string := λ f, str_formula f ‹_› @[instance]meta def formula_to_string {n} : has_to_string (bounded_formula L_ZFC n) := ⟨print_formula⟩ meta def print_formula_list {n} (axms : list (bounded_formula L_ZFC n)) : tactic unit := mmap' (λ ax, tactic.trace $ to_string ax ++ "\n") axms section test /- ∀ x, ∀ y, x = y → ∀ z, z = x → z = y -/ meta def testsentence : sentence L_ZFC := ∀' ∀' (&1 ≃ &0 ⟹ ∀' (&0 ≃ &2 ⟹ &0 ≃ &1)) -- #eval print_formula testsentence -- #eval print_formula CH_f -- #eval print_formula axiom_of_powerset -- #eval print_formula axiom_of_emptyset -- #eval print_formula axiom_of_infinity -- -- "(∅ ∈ ω∧(∀x1,(x1 ∈ ω ⟹ ∃x2,(x2 ∈ ω∧x1 ∈ x2))))" -- #eval print_formula_list ([axiom_of_emptyset, axiom_of_pairing]) -- #eval print_formula axiom_of_regularity end test
b643b12d5a27d1fa37443e1cf7e66d8ac9be2e4c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/coeIssue1.lean
b40742a03c0af6c9e522fffe5d78965d7220df54
[ "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,269
lean
-- From @joehendrix -- The imul doesn't type check as Lean won't try to coerce from a reg (bv 64) to a expr (bv ?u) inductive MCType | bv : Nat → MCType open MCType inductive Reg : MCType → Type | rax (n : Nat) : Reg (bv n) inductive Expr : MCType → Type | r : ∀{tp:MCType}, Reg tp → Expr tp | sextC {s:Nat} (x : Expr (bv s)) (t:Nat) : Expr (bv t) instance reg_is_expr {tp:MCType} : Coe (Reg tp) (Expr tp) := ⟨Expr.r⟩ def bvmul {w:Nat} (x y : Expr (bv w)) : Expr (bv w) := x /- Remark: Joe's original example used the following definition. ``` def sext {s:Nat} (x : Expr (bv s)) (t:Nat) : Expr (bv t) := Expr.sextC x t ``` This definition is bad because the parameter `s` is unconstrained. Type class resolution gets stuck at ``` CoeT (Reg (bv 64)) (Reg.rax 64) (Expr (bv ?m_1)) ``` It would have to set `?m_1 := 64` which is not allowed since TC should not change external TC metavariables. I fixed the problem by changing the definition. Now, type inference will enforce that `?m_1` must be 64, and TC will be able to synthesize the instance. -/ def sext {s:Nat} (x : Expr (bv s)) (n:Nat) : Expr (bv (s+n)) := Expr.sextC x (s+n) open MCType variable {u:Nat} (e : Expr (bv 64)) #check (bvmul (sext (Reg.rax 64) 64) (sext e 64) : Expr (bv 128))
c611cab7da613a1e190b52d5fc802521e289ce66
e9dbaaae490bc072444e3021634bf73664003760
/src/Problems/2016/IMO_2016_P1.lean
7092bad03bee984c5cc46e12c17773d6b8904c7f
[ "Apache-2.0" ]
permissive
liaofei1128/geometry
566d8bfe095ce0c0113d36df90635306c60e975b
3dd128e4eec8008764bb94e18b932f9ffd66e6b3
refs/heads/master
1,678,996,510,399
1,581,454,543,000
1,583,337,839,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
591
lean
import Geo.Geo.Core namespace Geo open Angle Quadrilateral Seg def IMO_2016_P1 : Prop := ∀ (B C F : Point), Angle.isRight (Triangle.mk B C F).angles.A → -- Triangle B C F has a right angle at B ∀ (A D E: Point), on A (Line.mk C F) → cong ⟨F, A⟩ ⟨F, B⟩ → on F (Seg.mk A C) → cong ⟨D, A⟩ ⟨D, C⟩ → isBisector ⟨A, C⟩ ⟨D, A, B⟩ → cong ⟨E, A⟩ ⟨E, D⟩ → isBisector ⟨A, D⟩ ⟨E, A, C⟩ → let M := (Seg.mk C F).midp; ∀ (X : Point), parallelogram (Quadrilateral.mk A M X E) → allIntersect [Line.mk B D, Line.mk F X, Line.mk M E] end Geo
c72676c15fd5c8e15745ae464122cf4595227d4f
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/computability/halting.lean
96b598de662991efebb7b6b3fe94dfdd1b92e6a9
[ "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
14,173
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import computability.partrec_code /-! # Computability theory and the halting problem A universal partial recursive function, Rice's theorem, and the halting problem. ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open encodable denumerable namespace nat.partrec open computable part theorem merge' {f g} (hf : nat.partrec f) (hg : nat.partrec g) : ∃ h, nat.partrec h ∧ ∀ a, (∀ x ∈ h a, x ∈ f a ∨ x ∈ g a) ∧ ((h a).dom ↔ (f a).dom ∨ (g a).dom) := begin obtain ⟨cf, rfl⟩ := code.exists_code.1 hf, obtain ⟨cg, rfl⟩ := code.exists_code.1 hg, have : nat.partrec (λ n, (nat.rfind_opt (λ k, cf.evaln k n <|> cg.evaln k n))) := partrec.nat_iff.1 (partrec.rfind_opt $ primrec.option_orelse.to_comp.comp (code.evaln_prim.to_comp.comp $ (snd.pair (const cf)).pair fst) (code.evaln_prim.to_comp.comp $ (snd.pair (const cg)).pair fst)), refine ⟨_, this, λ n, _⟩, suffices, refine ⟨this, ⟨λ h, (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, _⟩⟩, { intro h, rw nat.rfind_opt_dom, simp only [dom_iff_mem, code.evaln_complete, option.mem_def] at h, obtain ⟨x, k, e⟩ | ⟨x, k, e⟩ := h, { refine ⟨k, x, _⟩, simp only [e, option.some_orelse, option.mem_def] }, { refine ⟨k, _⟩, cases cf.evaln k n with y, { exact ⟨x, by simp only [e, option.mem_def, option.none_orelse]⟩ }, { exact ⟨y, by simp only [option.some_orelse, option.mem_def]⟩ } } }, intros x h, obtain ⟨k, e⟩ := nat.rfind_opt_spec h, revert e, simp only [option.mem_def]; cases e' : cf.evaln k n with y; simp; intro, { exact or.inr (code.evaln_sound e) }, { subst y, exact or.inl (code.evaln_sound e') } end end nat.partrec namespace partrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] open computable part nat.partrec (code) nat.partrec.code theorem merge' {f g : α →. σ} (hf : partrec f) (hg : partrec g) : ∃ k : α →. σ, partrec k ∧ ∀ a, (∀ x ∈ k a, x ∈ f a ∨ x ∈ g a) ∧ ((k a).dom ↔ (f a).dom ∨ (g a).dom) := let ⟨k, hk, H⟩ := nat.partrec.merge' (bind_decode₂_iff.1 hf) (bind_decode₂_iff.1 hg) in begin let k' := λ a, (k (encode a)).bind (λ n, decode σ n), refine ⟨k', ((nat_iff.2 hk).comp computable.encode).bind (computable.decode.of_option.comp snd).to₂, λ a, _⟩, suffices, refine ⟨this, ⟨λ h, (this _ ⟨h, rfl⟩).imp Exists.fst Exists.fst, _⟩⟩, { intro h, rw bind_dom, have hk : (k (encode a)).dom := (H _).2.2 (by simpa only [encodek₂, bind_some, coe_some] using h), existsi hk, simp only [exists_prop, mem_map_iff, mem_coe, mem_bind_iff, option.mem_def] at H, obtain ⟨a', ha', y, hy, e⟩ | ⟨a', ha', y, hy, e⟩ := (H _).1 _ ⟨hk, rfl⟩; { simp only [e.symm, encodek] } }, intros x h', simp only [k', exists_prop, mem_coe, mem_bind_iff, option.mem_def] at h', obtain ⟨n, hn, hx⟩ := h', have := (H _).1 _ hn, simp [mem_decode₂, encode_injective.eq_iff] at this, obtain ⟨a', ha, rfl⟩ | ⟨a', ha, rfl⟩ := this; simp only [encodek] at hx; rw hx at ha, { exact or.inl ha }, exact or.inr ha end theorem merge {f g : α →. σ} (hf : partrec f) (hg : partrec g) (H : ∀ a (x ∈ f a) (y ∈ g a), x = y) : ∃ k : α →. σ, partrec k ∧ ∀ a x, x ∈ k a ↔ x ∈ f a ∨ x ∈ g a := let ⟨k, hk, K⟩ := merge' hf hg in ⟨k, hk, λ a x, ⟨(K _).1 _, λ h, begin have : (k a).dom := (K _).2.2 (h.imp Exists.fst Exists.fst), refine ⟨this, _⟩, cases h with h h; cases (K _).1 _ ⟨this, rfl⟩ with h' h', { exact mem_unique h' h }, { exact (H _ _ h _ h').symm }, { exact H _ _ h' _ h }, { exact mem_unique h' h } end⟩⟩ theorem cond {c : α → bool} {f : α →. σ} {g : α →. σ} (hc : computable c) (hf : partrec f) (hg : partrec g) : partrec (λ a, cond (c a) (f a) (g a)) := let ⟨cf, ef⟩ := exists_code.1 hf, ⟨cg, eg⟩ := exists_code.1 hg in ((eval_part.comp (computable.cond hc (const cf) (const cg)) computable.id).bind ((@computable.decode σ _).comp snd).of_option.to₂).of_eq $ λ a, by cases c a; simp [ef, eg, encodek] theorem sum_cases {f : α → β ⊕ γ} {g : α → β →. σ} {h : α → γ →. σ} (hf : computable f) (hg : partrec₂ g) (hh : partrec₂ h) : @partrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (sum_cases hf (const tt).to₂ (const ff).to₂) (sum_cases_left hf (option_some_iff.2 hg).to₂ (const option.none).to₂) (sum_cases_right hf (const option.none).to₂ (option_some_iff.2 hh).to₂)) .of_eq $ λ a, by cases f a; simp only [bool.cond_tt, bool.cond_ff] end partrec /-- A computable predicate is one whose indicator function is computable. -/ def computable_pred {α} [primcodable α] (p : α → Prop) := ∃ [D : decidable_pred p], by exactI computable (λ a, to_bool (p a)) /-- A recursively enumerable predicate is one which is the domain of a computable partial function. -/ def re_pred {α} [primcodable α] (p : α → Prop) := partrec (λ a, part.assert (p a) (λ _, part.some ())) theorem computable_pred.of_eq {α} [primcodable α] {p q : α → Prop} (hp : computable_pred p) (H : ∀ a, p a ↔ q a) : computable_pred q := (funext (λ a, propext (H a)) : p = q) ▸ hp namespace computable_pred variables {α : Type*} {σ : Type*} variables [primcodable α] [primcodable σ] open nat.partrec (code) nat.partrec.code computable theorem computable_iff {p : α → Prop} : computable_pred p ↔ ∃ f : α → bool, computable f ∧ p = λ a, f a := ⟨λ ⟨D, h⟩, by exactI ⟨_, h, funext $ λ a, propext (to_bool_iff _).symm⟩, by rintro ⟨f, h, rfl⟩; exact ⟨by apply_instance, by simpa using h⟩⟩ protected theorem not {p : α → Prop} (hp : computable_pred p) : computable_pred (λ a, ¬ p a) := by obtain ⟨f, hf, rfl⟩ := computable_iff.1 hp; exact ⟨by apply_instance, (cond hf (const ff) (const tt)).of_eq (λ n, by {dsimp, cases f n; refl})⟩ theorem to_re {p : α → Prop} (hp : computable_pred p) : re_pred p := begin obtain ⟨f, hf, rfl⟩ := computable_iff.1 hp, unfold re_pred, refine (partrec.cond hf (decidable.partrec.const' (part.some ())) partrec.none).of_eq (λ n, part.ext $ λ a, _), cases a, cases f n; simp end theorem rice (C : set (ℕ →. ℕ)) (h : computable_pred (λ c, eval c ∈ C)) {f g} (hf : nat.partrec f) (hg : nat.partrec g) (fC : f ∈ C) : g ∈ C := begin cases h with _ h, resetI, obtain ⟨c, e⟩ := fixed_point₂ (partrec.cond (h.comp fst) ((partrec.nat_iff.2 hg).comp snd).to₂ ((partrec.nat_iff.2 hf).comp snd).to₂).to₂, simp at e, by_cases H : eval c ∈ C, { simp only [H, if_true] at e, rwa ← e }, { simp only [H, if_false] at e, rw e at H, contradiction } end theorem rice₂ (C : set code) (H : ∀ cf cg, eval cf = eval cg → (cf ∈ C ↔ cg ∈ C)) : computable_pred (λ c, c ∈ C) ↔ C = ∅ ∨ C = set.univ := by classical; exact have hC : ∀ f, f ∈ C ↔ eval f ∈ eval '' C, from λ f, ⟨set.mem_image_of_mem _, λ ⟨g, hg, e⟩, (H _ _ e).1 hg⟩, ⟨λ h, or_iff_not_imp_left.2 $ λ C0, set.eq_univ_of_forall $ λ cg, let ⟨cf, fC⟩ := set.ne_empty_iff_nonempty.1 C0 in (hC _).2 $ rice (eval '' C) (h.of_eq hC) (partrec.nat_iff.1 $ eval_part.comp (const cf) computable.id) (partrec.nat_iff.1 $ eval_part.comp (const cg) computable.id) ((hC _).1 fC), λ h, by obtain rfl | rfl := h; simp [computable_pred, set.mem_empty_eq]; exact ⟨by apply_instance, computable.const _⟩⟩ theorem halting_problem (n) : ¬ computable_pred (λ c, (eval c n).dom) | h := rice {f | (f n).dom} h nat.partrec.zero nat.partrec.none trivial -- Post's theorem on the equivalence of r.e., co-r.e. sets and -- computable sets. The assumption that p is decidable is required -- unless we assume Markov's principle or LEM. @[nolint decidable_classical] theorem computable_iff_re_compl_re {p : α → Prop} [decidable_pred p] : computable_pred p ↔ re_pred p ∧ re_pred (λ a, ¬ p a) := ⟨λ h, ⟨h.to_re, h.not.to_re⟩, λ ⟨h₁, h₂⟩, ⟨‹_›, begin obtain ⟨k, pk, hk⟩ := partrec.merge (h₁.map (computable.const tt).to₂) (h₂.map (computable.const ff).to₂) _, { refine partrec.of_eq pk (λ n, part.eq_some_iff.2 _), rw hk, simp, apply decidable.em }, { intros a x hx y hy, simp at hx hy, cases hy.1 hx.1 } end⟩⟩ end computable_pred namespace nat open vector part /-- A simplified basis for `partrec`. -/ inductive partrec' : ∀ {n}, (vector ℕ n →. ℕ) → Prop | prim {n f} : @primrec' n f → @partrec' n f | comp {m n f} (g : fin n → vector ℕ m →. ℕ) : partrec' f → (∀ i, partrec' (g i)) → partrec' (λ v, m_of_fn (λ i, g i v) >>= f) | rfind {n} {f : vector ℕ (n+1) → ℕ} : @partrec' (n+1) f → partrec' (λ v, rfind (λ n, some (f (n ::ᵥ v) = 0))) end nat namespace nat.partrec' open vector partrec computable nat (partrec') nat.partrec' theorem to_part {n f} (pf : @partrec' n f) : partrec f := begin induction pf, case nat.partrec'.prim : n f hf { exact hf.to_prim.to_comp }, case nat.partrec'.comp : m n f g _ _ hf hg { exact (vector_m_of_fn (λ i, hg i)).bind (hf.comp snd) }, case nat.partrec'.rfind : n f _ hf { have := ((primrec.eq.comp primrec.id (primrec.const 0)).to_comp.comp (hf.comp (vector_cons.comp snd fst))).to₂.partrec₂, exact this.rfind }, end theorem of_eq {n} {f g : vector ℕ n →. ℕ} (hf : partrec' f) (H : ∀ i, f i = g i) : partrec' g := (funext H : f = g) ▸ hf theorem of_prim {n} {f : vector ℕ n → ℕ} (hf : primrec f) : @partrec' n f := prim (nat.primrec'.of_prim hf) theorem head {n : ℕ} : @partrec' n.succ (@head ℕ n) := prim nat.primrec'.head theorem tail {n f} (hf : @partrec' n f) : @partrec' n.succ (λ v, f v.tail) := (hf.comp _ (λ i, @prim _ _ $ nat.primrec'.nth i.succ)).of_eq $ λ v, by simp; rw [← of_fn_nth v.tail]; congr; funext i; simp protected theorem bind {n f g} (hf : @partrec' n f) (hg : @partrec' (n+1) g) : @partrec' n (λ v, (f v).bind (λ a, g (a ::ᵥ v))) := (@comp n (n+1) g (λ i, fin.cases f (λ i v, some (v.nth i)) i) hg (λ i, begin refine fin.cases _ (λ i, _) i; simp *, exact prim (nat.primrec'.nth _) end)).of_eq $ λ v, by simp [m_of_fn, part.bind_assoc, pure] protected theorem map {n f} {g : vector ℕ (n+1) → ℕ} (hf : @partrec' n f) (hg : @partrec' (n+1) g) : @partrec' n (λ v, (f v).map (λ a, g (a ::ᵥ v))) := by simp [(part.bind_some_eq_map _ _).symm]; exact hf.bind hg /-- Analogous to `nat.partrec'` for `ℕ`-valued functions, a predicate for partial recursive vector-valued functions.-/ def vec {n m} (f : vector ℕ n → vector ℕ m) := ∀ i, partrec' (λ v, (f v).nth i) theorem vec.prim {n m f} (hf : @nat.primrec'.vec n m f) : vec f := λ i, prim $ hf i protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0 protected theorem cons {n m} {f : vector ℕ n → ℕ} {g} (hf : @partrec' n f) (hg : @vec n m g) : vec (λ v, f v ::ᵥ g v) := λ i, fin.cases (by simp *) (λ i, by simp only [hg i, nth_cons_succ]) i theorem idv {n} : @vec n n id := vec.prim nat.primrec'.idv theorem comp' {n m f g} (hf : @partrec' m f) (hg : @vec n m g) : partrec' (λ v, f (g v)) := (hf.comp _ hg).of_eq $ λ v, by simp theorem comp₁ {n} (f : ℕ →. ℕ) {g : vector ℕ n → ℕ} (hf : @partrec' 1 (λ v, f v.head)) (hg : @partrec' n g) : @partrec' n (λ v, f (g v)) := by simpa using hf.comp' (partrec'.cons hg partrec'.nil) theorem rfind_opt {n} {f : vector ℕ (n+1) → ℕ} (hf : @partrec' (n+1) f) : @partrec' n (λ v, nat.rfind_opt (λ a, of_nat (option ℕ) (f (a ::ᵥ v)))) := ((rfind $ (of_prim (primrec.nat_sub.comp (primrec.const 1) primrec.vector_head)) .comp₁ (λ n, part.some (1 - n)) hf) .bind ((prim nat.primrec'.pred).comp₁ nat.pred hf)).of_eq $ λ v, part.ext $ λ b, begin simp [nat.rfind_opt, -nat.mem_rfind], refine exists_congr (λ a, (and_congr (iff_of_eq _) iff.rfl).trans (and_congr_right (λ h, _))), { congr; funext n, simp, cases f (n ::ᵥ v); simp [nat.succ_ne_zero]; refl }, { have := nat.rfind_spec h, simp at this, cases f (a ::ᵥ v) with c, {cases this}, rw [← option.some_inj, eq_comm], refl } end open nat.partrec.code theorem of_part : ∀ {n f}, partrec f → @partrec' n f := suffices ∀ f, nat.partrec f → @partrec' 1 (λ v, f v.head), from λ n f hf, begin let g, swap, exact (comp₁ g (this g hf) (prim nat.primrec'.encode)).of_eq (λ i, by dsimp only [g]; simp [encodek, part.map_id']), end, λ f hf, begin obtain ⟨c, rfl⟩ := exists_code.1 hf, simpa [eval_eq_rfind_opt] using (rfind_opt $ of_prim $ primrec.encode_iff.2 $ evaln_prim.comp $ (primrec.vector_head.pair (primrec.const c)).pair $ primrec.vector_head.comp primrec.vector_tail) end theorem part_iff {n f} : @partrec' n f ↔ partrec f := ⟨to_part, of_part⟩ theorem part_iff₁ {f : ℕ →. ℕ} : @partrec' 1 (λ v, f v.head) ↔ partrec f := part_iff.trans ⟨ λ h, (h.comp $ (primrec.vector_of_fn $ λ i, primrec.id).to_comp).of_eq (λ v, by simp only [id.def, head_of_fn]), λ h, h.comp vector_head⟩ theorem part_iff₂ {f : ℕ → ℕ →. ℕ} : @partrec' 2 (λ v, f v.head v.tail.head) ↔ partrec₂ f := part_iff.trans ⟨ λ h, (h.comp $ vector_cons.comp fst $ vector_cons.comp snd (const nil)).of_eq (λ v, by simp only [cons_head, cons_tail]), λ h, h.comp vector_head (vector_head.comp vector_tail)⟩ theorem vec_iff {m n f} : @vec m n f ↔ computable f := ⟨λ h, by simpa only [of_fn_nth] using vector_of_fn (λ i, to_part (h i)), λ h i, of_part $ vector_nth.comp h (const i)⟩ end nat.partrec'
8a6010c890e3dde88b77e3c4f8e6010f6223cf8e
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/order/priestley.lean
ac900eb4b1d1b606a7bdb225099dddee8cfd5274
[ "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
2,675
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.upper_lower import topology.separation /-! # Priestley spaces This file defines Priestley spaces. A Priestley space is an ordered compact topological space such that any two distinct points can be separated by a clopen upper set. ## Main declarations * `priestley_space`: Prop-valued mixin stating the Priestley separation axiom: Any two distinct points can be separated by a clopen upper set. ## Implementation notes We do not include compactness in the definition, so a Priestley space is to be declared as follows: `[preorder α] [topological_space α] [compact_space α] [priestley_space α]` ## References * [Wikipedia, *Priestley space*](https://en.wikipedia.org/wiki/Priestley_space) * [Davey, Priestley *Introduction to Lattices and Order*][davey_priestley] -/ open set variables {α : Type*} /-- A Priestley space is an ordered topological space such that any two distinct points can be separated by a clopen upper set. Compactness is often assumed, but we do not include it here. -/ class priestley_space (α : Type*) [preorder α] [topological_space α] := (priestley {x y : α} : ¬ x ≤ y → ∃ U : set α, is_clopen U ∧ is_upper_set U ∧ x ∈ U ∧ y ∉ U) variables [topological_space α] section preorder variables [preorder α] [priestley_space α] {x y : α} lemma exists_clopen_upper_of_not_le : ¬ x ≤ y → ∃ U : set α, is_clopen U ∧ is_upper_set U ∧ x ∈ U ∧ y ∉ U := priestley_space.priestley lemma exists_clopen_lower_of_not_le (h : ¬ x ≤ y) : ∃ U : set α, is_clopen U ∧ is_lower_set U ∧ x ∉ U ∧ y ∈ U := let ⟨U, hU, hU', hx, hy⟩ := exists_clopen_upper_of_not_le h in ⟨Uᶜ, hU.compl, hU'.compl, not_not.2 hx, hy⟩ end preorder section partial_order variables [partial_order α] [priestley_space α] {x y : α} lemma exists_clopen_upper_or_lower_of_ne (h : x ≠ y) : ∃ U : set α, is_clopen U ∧ (is_upper_set U ∨ is_lower_set U) ∧ x ∈ U ∧ y ∉ U := begin obtain (h | h) := h.not_le_or_not_le, { exact (exists_clopen_upper_of_not_le h).imp (λ U, and.imp_right $ and.imp_left or.inl) }, { obtain ⟨U, hU, hU', hy, hx⟩ := exists_clopen_lower_of_not_le h, exact ⟨U, hU, or.inr hU', hx, hy⟩ } end @[priority 100] -- See note [lower instance priority] instance priestley_space.to_t2_space : t2_space α := ⟨λ x y h, let ⟨U, hU, _, hx, hy⟩ := exists_clopen_upper_or_lower_of_ne h in ⟨U, Uᶜ, hU.is_open, hU.compl.is_open, hx, hy, inter_compl_self _⟩⟩ end partial_order
04f59b817f8178526cb27853b9646188bdd81f97
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/complete_lattice.lean
565b68593117fe6cbdbd22b6485a70649ceeb290
[ "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
56,974
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.bool.set import data.ulift import data.nat.basic import order.bounds /-! # Theory of complete lattices ## Main definitions * `Sup` and `Inf` are the supremum and the infimum of a set; * `supr (f : ι → α)` and `infi (f : ι → α)` are indexed supremum and infimum of a function, defined as `Sup` and `Inf` of the range of this function; * `class complete_lattice`: a bounded lattice such that `Sup s` is always the least upper boundary of `s` and `Inf s` is always the greatest lower boundary of `s`; * `class complete_linear_order`: a linear ordered complete lattice. ## Naming conventions In lemma names, * `Sup` is called `Sup` * `Inf` is called `Inf` * `⨆ i, s i` is called `supr` * `⨅ i, s i` is called `infi` * `⨆ i j, s i j` is called `supr₂`. This is a `supr` inside a `supr`. * `⨅ i j, s i j` is called `infi₂`. This is an `infi` inside an `infi`. * `⨆ i ∈ s, t i` is called `bsupr` for "bounded `supr`". This is the special case of `supr₂` where `j : i ∈ s`. * `⨅ i ∈ s, t i` is called `binfi` for "bounded `infi`". This is the special case of `infi₂` where `j : i ∈ s`. ## Notation * `⨆ i, f i` : `supr f`, the supremum of the range of `f`; * `⨅ i, f i` : `infi f`, the infimum of the range of `f`. -/ set_option old_structure_cmd true open function order_dual set variables {α β β₂ γ : Type*} {ι ι' : Sort*} {κ : ι → Sort*} {κ' : ι' → Sort*} /-- class for the `Sup` operator -/ class has_Sup (α : Type*) := (Sup : set α → α) /-- class for the `Inf` operator -/ class has_Inf (α : Type*) := (Inf : set α → α) export has_Sup (Sup) has_Inf (Inf) /-- Supremum of a set -/ add_decl_doc has_Sup.Sup /-- Infimum of a set -/ add_decl_doc has_Inf.Inf /-- Indexed supremum -/ def supr [has_Sup α] {ι} (s : ι → α) : α := Sup (range s) /-- Indexed infimum -/ def infi [has_Inf α] {ι} (s : ι → α) : α := Inf (range s) @[priority 50] instance has_Inf_to_nonempty (α) [has_Inf α] : nonempty α := ⟨Inf ∅⟩ @[priority 50] instance has_Sup_to_nonempty (α) [has_Sup α] : nonempty α := ⟨Sup ∅⟩ notation `⨆` binders `, ` r:(scoped f, supr f) := r notation `⨅` binders `, ` r:(scoped f, infi f) := r instance (α) [has_Inf α] : has_Sup αᵒᵈ := ⟨(Inf : set α → α)⟩ instance (α) [has_Sup α] : has_Inf αᵒᵈ := ⟨(Sup : set α → α)⟩ /-- Note that we rarely use `complete_semilattice_Sup` (in fact, any such object is always a `complete_lattice`, so it's usually best to start there). Nevertheless it is sometimes a useful intermediate step in constructions. -/ @[ancestor partial_order has_Sup] class complete_semilattice_Sup (α : Type*) extends partial_order α, has_Sup α := (le_Sup : ∀ s, ∀ a ∈ s, a ≤ Sup s) (Sup_le : ∀ s a, (∀ b ∈ s, b ≤ a) → Sup s ≤ a) section variables [complete_semilattice_Sup α] {s t : set α} {a b : α} @[ematch] theorem le_Sup : a ∈ s → a ≤ Sup s := complete_semilattice_Sup.le_Sup s a theorem Sup_le : (∀ b ∈ s, b ≤ a) → Sup s ≤ a := complete_semilattice_Sup.Sup_le s a lemma is_lub_Sup (s : set α) : is_lub s (Sup s) := ⟨λ x, le_Sup, λ x, Sup_le⟩ lemma is_lub.Sup_eq (h : is_lub s a) : Sup s = a := (is_lub_Sup s).unique h theorem le_Sup_of_le (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_Sup hb) theorem Sup_le_Sup (h : s ⊆ t) : Sup s ≤ Sup t := (is_lub_Sup s).mono (is_lub_Sup t) h @[simp] theorem Sup_le_iff : Sup s ≤ a ↔ ∀ b ∈ s, b ≤ a := is_lub_le_iff (is_lub_Sup s) lemma le_Sup_iff : a ≤ Sup s ↔ ∀ b ∈ upper_bounds s, a ≤ b := ⟨λ h b hb, le_trans h (Sup_le hb), λ hb, hb _ (λ x, le_Sup)⟩ lemma le_supr_iff {s : ι → α} : a ≤ supr s ↔ ∀ b, (∀ i, s i ≤ b) → a ≤ b := by simp [supr, le_Sup_iff, upper_bounds] theorem Sup_le_Sup_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, x ≤ y) : Sup s ≤ Sup t := le_Sup_iff.2 $ λ b hb, Sup_le $ λ a ha, let ⟨c, hct, hac⟩ := h a ha in hac.trans (hb hct) -- We will generalize this to conditionally complete lattices in `cSup_singleton`. theorem Sup_singleton {a : α} : Sup {a} = a := is_lub_singleton.Sup_eq end /-- Note that we rarely use `complete_semilattice_Inf` (in fact, any such object is always a `complete_lattice`, so it's usually best to start there). Nevertheless it is sometimes a useful intermediate step in constructions. -/ @[ancestor partial_order has_Inf] class complete_semilattice_Inf (α : Type*) extends partial_order α, has_Inf α := (Inf_le : ∀ s, ∀ a ∈ s, Inf s ≤ a) (le_Inf : ∀ s a, (∀ b ∈ s, a ≤ b) → a ≤ Inf s) section variables [complete_semilattice_Inf α] {s t : set α} {a b : α} @[ematch] theorem Inf_le : a ∈ s → Inf s ≤ a := complete_semilattice_Inf.Inf_le s a theorem le_Inf : (∀ b ∈ s, a ≤ b) → a ≤ Inf s := complete_semilattice_Inf.le_Inf s a lemma is_glb_Inf (s : set α) : is_glb s (Inf s) := ⟨λ a, Inf_le, λ a, le_Inf⟩ lemma is_glb.Inf_eq (h : is_glb s a) : Inf s = a := (is_glb_Inf s).unique h theorem Inf_le_of_le (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (Inf_le hb) h theorem Inf_le_Inf (h : s ⊆ t) : Inf t ≤ Inf s := (is_glb_Inf s).mono (is_glb_Inf t) h @[simp] theorem le_Inf_iff : a ≤ Inf s ↔ ∀ b ∈ s, a ≤ b := le_is_glb_iff (is_glb_Inf s) lemma Inf_le_iff : Inf s ≤ a ↔ ∀ b ∈ lower_bounds s, b ≤ a := ⟨λ h b hb, le_trans (le_Inf hb) h, λ hb, hb _ (λ x, Inf_le)⟩ lemma infi_le_iff {s : ι → α} : infi s ≤ a ↔ ∀ b, (∀ i, b ≤ s i) → b ≤ a := by simp [infi, Inf_le_iff, lower_bounds] theorem Inf_le_Inf_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, y ≤ x) : Inf t ≤ Inf s := le_of_forall_le begin simp only [le_Inf_iff], introv h₀ h₁, rcases h _ h₁ with ⟨y, hy, hy'⟩, solve_by_elim [le_trans _ hy'] end -- We will generalize this to conditionally complete lattices in `cInf_singleton`. theorem Inf_singleton {a : α} : Inf {a} = a := is_glb_singleton.Inf_eq end /-- A complete lattice is a bounded lattice which has suprema and infima for every subset. -/ @[protect_proj, ancestor lattice complete_semilattice_Sup complete_semilattice_Inf has_top has_bot] class complete_lattice (α : Type*) extends lattice α, complete_semilattice_Sup α, complete_semilattice_Inf α, has_top α, has_bot α := (le_top : ∀ x : α, x ≤ ⊤) (bot_le : ∀ x : α, ⊥ ≤ x) @[priority 100] -- see Note [lower instance priority] instance complete_lattice.to_bounded_order [h : complete_lattice α] : bounded_order α := { ..h } /-- Create a `complete_lattice` from a `partial_order` and `Inf` function that returns the greatest lower bound of a set. Usually this constructor provides poor definitional equalities. If other fields are known explicitly, they should be provided; for example, if `inf` is known explicitly, construct the `complete_lattice` instance as ``` instance : complete_lattice my_T := { inf := better_inf, le_inf := ..., inf_le_right := ..., inf_le_left := ... -- don't care to fix sup, Sup, bot, top ..complete_lattice_of_Inf my_T _ } ``` -/ def complete_lattice_of_Inf (α : Type*) [H1 : partial_order α] [H2 : has_Inf α] (is_glb_Inf : ∀ s : set α, is_glb s (Inf s)) : complete_lattice α := { bot := Inf univ, bot_le := λ x, (is_glb_Inf univ).1 trivial, top := Inf ∅, le_top := λ a, (is_glb_Inf ∅).2 $ by simp, sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, inf := λ a b, Inf {a, b}, le_inf := λ a b c hab hac, by { apply (is_glb_Inf _).2, simp [*] }, inf_le_right := λ a b, (is_glb_Inf _).1 $ mem_insert_of_mem _ $ mem_singleton _, inf_le_left := λ a b, (is_glb_Inf _).1 $ mem_insert _ _, sup_le := λ a b c hac hbc, (is_glb_Inf _).1 $ by simp [*], le_sup_left := λ a b, (is_glb_Inf _).2 $ λ x, and.left, le_sup_right := λ a b, (is_glb_Inf _).2 $ λ x, and.right, le_Inf := λ s a ha, (is_glb_Inf s).2 ha, Inf_le := λ s a ha, (is_glb_Inf s).1 ha, Sup := λ s, Inf (upper_bounds s), le_Sup := λ s a ha, (is_glb_Inf (upper_bounds s)).2 $ λ b hb, hb ha, Sup_le := λ s a ha, (is_glb_Inf (upper_bounds s)).1 ha, .. H1, .. H2 } /-- Any `complete_semilattice_Inf` is in fact a `complete_lattice`. Note that this construction has bad definitional properties: see the doc-string on `complete_lattice_of_Inf`. -/ def complete_lattice_of_complete_semilattice_Inf (α : Type*) [complete_semilattice_Inf α] : complete_lattice α := complete_lattice_of_Inf α (λ s, is_glb_Inf s) /-- Create a `complete_lattice` from a `partial_order` and `Sup` function that returns the least upper bound of a set. Usually this constructor provides poor definitional equalities. If other fields are known explicitly, they should be provided; for example, if `inf` is known explicitly, construct the `complete_lattice` instance as ``` instance : complete_lattice my_T := { inf := better_inf, le_inf := ..., inf_le_right := ..., inf_le_left := ... -- don't care to fix sup, Inf, bot, top ..complete_lattice_of_Sup my_T _ } ``` -/ def complete_lattice_of_Sup (α : Type*) [H1 : partial_order α] [H2 : has_Sup α] (is_lub_Sup : ∀ s : set α, is_lub s (Sup s)) : complete_lattice α := { top := Sup univ, le_top := λ x, (is_lub_Sup univ).1 trivial, bot := Sup ∅, bot_le := λ x, (is_lub_Sup ∅).2 $ by simp, sup := λ a b, Sup {a, b}, sup_le := λ a b c hac hbc, (is_lub_Sup _).2 (by simp [*]), le_sup_left := λ a b, (is_lub_Sup _).1 $ mem_insert _ _, le_sup_right := λ a b, (is_lub_Sup _).1 $ mem_insert_of_mem _ $ mem_singleton _, inf := λ a b, Sup {x | x ≤ a ∧ x ≤ b}, le_inf := λ a b c hab hac, (is_lub_Sup _).1 $ by simp [*], inf_le_left := λ a b, (is_lub_Sup _).2 (λ x, and.left), inf_le_right := λ a b, (is_lub_Sup _).2 (λ x, and.right), Inf := λ s, Sup (lower_bounds s), Sup_le := λ s a ha, (is_lub_Sup s).2 ha, le_Sup := λ s a ha, (is_lub_Sup s).1 ha, Inf_le := λ s a ha, (is_lub_Sup (lower_bounds s)).2 (λ b hb, hb ha), le_Inf := λ s a ha, (is_lub_Sup (lower_bounds s)).1 ha, .. H1, .. H2 } /-- Any `complete_semilattice_Sup` is in fact a `complete_lattice`. Note that this construction has bad definitional properties: see the doc-string on `complete_lattice_of_Sup`. -/ def complete_lattice_of_complete_semilattice_Sup (α : Type*) [complete_semilattice_Sup α] : complete_lattice α := complete_lattice_of_Sup α (λ s, is_lub_Sup s) /-- A complete linear order is a linear order whose lattice structure is complete. -/ class complete_linear_order (α : Type*) extends complete_lattice α, linear_order α renaming max → sup min → inf namespace order_dual variable (α) instance [complete_lattice α] : complete_lattice αᵒᵈ := { le_Sup := @complete_lattice.Inf_le α _, Sup_le := @complete_lattice.le_Inf α _, Inf_le := @complete_lattice.le_Sup α _, le_Inf := @complete_lattice.Sup_le α _, .. order_dual.lattice α, ..order_dual.has_Sup α, ..order_dual.has_Inf α, .. order_dual.bounded_order α } instance [complete_linear_order α] : complete_linear_order αᵒᵈ := { .. order_dual.complete_lattice α, .. order_dual.linear_order α } end order_dual open order_dual section variables [complete_lattice α] {s t : set α} {a b : α} @[simp] lemma to_dual_Sup (s : set α) : to_dual (Sup s) = Inf (of_dual ⁻¹' s) := rfl @[simp] lemma to_dual_Inf (s : set α) : to_dual (Inf s) = Sup (of_dual ⁻¹' s) := rfl @[simp] lemma of_dual_Sup (s : set αᵒᵈ) : of_dual (Sup s) = Inf (to_dual ⁻¹' s) := rfl @[simp] lemma of_dual_Inf (s : set αᵒᵈ) : of_dual (Inf s) = Sup (to_dual ⁻¹' s) := rfl @[simp] lemma to_dual_supr (f : ι → α) : to_dual (⨆ i, f i) = ⨅ i, to_dual (f i) := rfl @[simp] lemma to_dual_infi (f : ι → α) : to_dual (⨅ i, f i) = ⨆ i, to_dual (f i) := rfl @[simp] lemma of_dual_supr (f : ι → αᵒᵈ) : of_dual (⨆ i, f i) = ⨅ i, of_dual (f i) := rfl @[simp] lemma of_dual_infi (f : ι → αᵒᵈ) : of_dual (⨅ i, f i) = ⨆ i, of_dual (f i) := rfl theorem Inf_le_Sup (hs : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_Inf s) (is_lub_Sup s) hs theorem Sup_union {s t : set α} : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_Sup s).union (is_lub_Sup t)).Sup_eq theorem Inf_union {s t : set α} : Inf (s ∪ t) = Inf s ⊓ Inf t := ((is_glb_Inf s).union (is_glb_Inf t)).Inf_eq theorem Sup_inter_le {s t : set α} : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := Sup_le $ λ b hb, le_inf (le_Sup hb.1) (le_Sup hb.2) theorem le_Inf_inter {s t : set α} : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := @Sup_inter_le αᵒᵈ _ _ _ @[simp] theorem Sup_empty : Sup ∅ = (⊥ : α) := (@is_lub_empty α _ _).Sup_eq @[simp] theorem Inf_empty : Inf ∅ = (⊤ : α) := (@is_glb_empty α _ _).Inf_eq @[simp] theorem Sup_univ : Sup univ = (⊤ : α) := (@is_lub_univ α _ _).Sup_eq @[simp] theorem Inf_univ : Inf univ = (⊥ : α) := (@is_glb_univ α _ _).Inf_eq -- TODO(Jeremy): get this automatically @[simp] theorem Sup_insert {a : α} {s : set α} : Sup (insert a s) = a ⊔ Sup s := ((is_lub_Sup s).insert a).Sup_eq @[simp] theorem Inf_insert {a : α} {s : set α} : Inf (insert a s) = a ⊓ Inf s := ((is_glb_Inf s).insert a).Inf_eq theorem Sup_le_Sup_of_subset_insert_bot (h : s ⊆ insert ⊥ t) : Sup s ≤ Sup t := le_trans (Sup_le_Sup h) (le_of_eq (trans Sup_insert bot_sup_eq)) theorem Inf_le_Inf_of_subset_insert_top (h : s ⊆ insert ⊤ t) : Inf t ≤ Inf s := le_trans (le_of_eq (trans top_inf_eq.symm Inf_insert.symm)) (Inf_le_Inf h) @[simp] theorem Sup_diff_singleton_bot (s : set α) : Sup (s \ {⊥}) = Sup s := (Sup_le_Sup (diff_subset _ _)).antisymm $ Sup_le_Sup_of_subset_insert_bot $ subset_insert_diff_singleton _ _ @[simp] theorem Inf_diff_singleton_top (s : set α) : Inf (s \ {⊤}) = Inf s := @Sup_diff_singleton_bot αᵒᵈ _ s theorem Sup_pair {a b : α} : Sup {a, b} = a ⊔ b := (@is_lub_pair α _ a b).Sup_eq theorem Inf_pair {a b : α} : Inf {a, b} = a ⊓ b := (@is_glb_pair α _ a b).Inf_eq @[simp] lemma Sup_eq_bot : Sup s = ⊥ ↔ ∀ a ∈ s, a = ⊥ := ⟨λ h a ha, bot_unique $ h ▸ le_Sup ha, λ h, bot_unique $ Sup_le $ λ a ha, le_bot_iff.2 $ h a ha⟩ @[simp] lemma Inf_eq_top : Inf s = ⊤ ↔ ∀ a ∈ s, a = ⊤ := @Sup_eq_bot αᵒᵈ _ _ lemma eq_singleton_bot_of_Sup_eq_bot_of_nonempty {s : set α} (h_sup : Sup s = ⊥) (hne : s.nonempty) : s = {⊥} := by { rw set.eq_singleton_iff_nonempty_unique_mem, rw Sup_eq_bot at h_sup, exact ⟨hne, h_sup⟩, } lemma eq_singleton_top_of_Inf_eq_top_of_nonempty : Inf s = ⊤ → s.nonempty → s = {⊤} := @eq_singleton_bot_of_Sup_eq_bot_of_nonempty αᵒᵈ _ _ /--Introduction rule to prove that `b` is the supremum of `s`: it suffices to check that `b` is larger than all elements of `s`, and that this is not the case of any `w < b`. See `cSup_eq_of_forall_le_of_forall_lt_exists_gt` for a version in conditionally complete lattices. -/ theorem Sup_eq_of_forall_le_of_forall_lt_exists_gt (h₁ : ∀ a ∈ s, a ≤ b) (h₂ : ∀ w, w < b → ∃ a ∈ s, w < a) : Sup s = b := (Sup_le h₁).eq_of_not_lt $ λ h, let ⟨a, ha, ha'⟩ := h₂ _ h in ((le_Sup ha).trans_lt ha').false /--Introduction rule to prove that `b` is the infimum of `s`: it suffices to check that `b` is smaller than all elements of `s`, and that this is not the case of any `w > b`. See `cInf_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in conditionally complete lattices. -/ theorem Inf_eq_of_forall_ge_of_forall_gt_exists_lt : (∀ a ∈ s, b ≤ a) → (∀ w, b < w → ∃ a ∈ s, a < w) → Inf s = b := @Sup_eq_of_forall_le_of_forall_lt_exists_gt αᵒᵈ _ _ _ end section complete_linear_order variables [complete_linear_order α] {s t : set α} {a b : α} lemma lt_Sup_iff : b < Sup s ↔ ∃ a ∈ s, b < a := lt_is_lub_iff $ is_lub_Sup s lemma Inf_lt_iff : Inf s < b ↔ ∃ a ∈ s, a < b := is_glb_lt_iff $ is_glb_Inf s lemma Sup_eq_top : Sup s = ⊤ ↔ ∀ b < ⊤, ∃ a ∈ s, b < a := ⟨λ h b hb, lt_Sup_iff.1 $ hb.trans_eq h.symm, λ h, top_unique $ le_of_not_gt $ λ h', let ⟨a, ha, h⟩ := h _ h' in (h.trans_le $ le_Sup ha).false⟩ lemma Inf_eq_bot : Inf s = ⊥ ↔ ∀ b > ⊥, ∃ a ∈ s, a < b := @Sup_eq_top αᵒᵈ _ _ lemma lt_supr_iff {f : ι → α} : a < supr f ↔ ∃ i, a < f i := lt_Sup_iff.trans exists_range_iff lemma infi_lt_iff {f : ι → α} : infi f < a ↔ ∃ i, f i < a := Inf_lt_iff.trans exists_range_iff end complete_linear_order /- ### supr & infi -/ section has_Sup variables [has_Sup α] {f g : ι → α} lemma Sup_range : Sup (range f) = supr f := rfl lemma Sup_eq_supr' (s : set α) : Sup s = ⨆ a : s, a := by rw [supr, subtype.range_coe] lemma supr_congr (h : ∀ i, f i = g i) : (⨆ i, f i) = ⨆ i, g i := congr_arg _ $ funext h lemma function.surjective.supr_comp {f : ι → ι'} (hf : surjective f) (g : ι' → α) : (⨆ x, g (f x)) = ⨆ y, g y := by simp only [supr, hf.range_comp] lemma equiv.supr_comp {g : ι' → α} (e : ι ≃ ι') : (⨆ x, g (e x)) = ⨆ y, g y := e.surjective.supr_comp _ protected lemma function.surjective.supr_congr {g : ι' → α} (h : ι → ι') (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⨆ x, f x) = ⨆ y, g y := by { convert h1.supr_comp g, exact (funext h2).symm } protected lemma equiv.supr_congr {g : ι' → α} (e : ι ≃ ι') (h : ∀ x, g (e x) = f x) : (⨆ x, f x) = ⨆ y, g y := e.surjective.supr_congr _ h @[congr] lemma supr_congr_Prop {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀ x, f₁ (pq.mpr x) = f₂ x) : supr f₁ = supr f₂ := by { obtain rfl := propext pq, congr' with x, apply f } lemma supr_plift_up (f : plift ι → α) : (⨆ i, f (plift.up i)) = ⨆ i, f i := plift.up_surjective.supr_congr _ $ λ _, rfl lemma supr_plift_down (f : ι → α) : (⨆ i, f (plift.down i)) = ⨆ i, f i := plift.down_surjective.supr_congr _ $ λ _, rfl lemma supr_range' (g : β → α) (f : ι → β) : (⨆ b : range f, g b) = ⨆ i, g (f i) := by rw [supr, supr, ← image_eq_range, ← range_comp] lemma Sup_image' {s : set β} {f : β → α} : Sup (f '' s) = ⨆ a : s, f a := by rw [supr, image_eq_range] end has_Sup section has_Inf variables [has_Inf α] {f g : ι → α} lemma Inf_range : Inf (range f) = infi f := rfl lemma Inf_eq_infi' (s : set α) : Inf s = ⨅ a : s, a := @Sup_eq_supr' αᵒᵈ _ _ lemma infi_congr (h : ∀ i, f i = g i) : (⨅ i, f i) = ⨅ i, g i := congr_arg _ $ funext h lemma function.surjective.infi_comp {f : ι → ι'} (hf : surjective f) (g : ι' → α) : (⨅ x, g (f x)) = ⨅ y, g y := @function.surjective.supr_comp αᵒᵈ _ _ _ f hf g lemma equiv.infi_comp {g : ι' → α} (e : ι ≃ ι') : (⨅ x, g (e x)) = ⨅ y, g y := @equiv.supr_comp αᵒᵈ _ _ _ _ e protected lemma function.surjective.infi_congr {g : ι' → α} (h : ι → ι') (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⨅ x, f x) = ⨅ y, g y := @function.surjective.supr_congr αᵒᵈ _ _ _ _ _ h h1 h2 protected lemma equiv.infi_congr {g : ι' → α} (e : ι ≃ ι') (h : ∀ x, g (e x) = f x) : (⨅ x, f x) = ⨅ y, g y := @equiv.supr_congr αᵒᵈ _ _ _ _ _ e h @[congr]lemma infi_congr_Prop {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀ x, f₁ (pq.mpr x) = f₂ x) : infi f₁ = infi f₂ := @supr_congr_Prop αᵒᵈ _ p q f₁ f₂ pq f lemma infi_plift_up (f : plift ι → α) : (⨅ i, f (plift.up i)) = ⨅ i, f i := plift.up_surjective.infi_congr _ $ λ _, rfl lemma infi_plift_down (f : ι → α) : (⨅ i, f (plift.down i)) = ⨅ i, f i := plift.down_surjective.infi_congr _ $ λ _, rfl lemma infi_range' (g : β → α) (f : ι → β) : (⨅ b : range f, g b) = ⨅ i, g (f i) := @supr_range' αᵒᵈ _ _ _ _ _ lemma Inf_image' {s : set β} {f : β → α} : Inf (f '' s) = ⨅ a : s, f a := @Sup_image' αᵒᵈ _ _ _ _ end has_Inf section variables [complete_lattice α] {f g s t : ι → α} {a b : α} -- TODO: this declaration gives error when starting smt state --@[ematch] lemma le_supr (f : ι → α) (i : ι) : f i ≤ supr f := le_Sup ⟨i, rfl⟩ lemma infi_le (f : ι → α) (i : ι) : infi f ≤ f i := Inf_le ⟨i, rfl⟩ @[ematch] lemma le_supr' (f : ι → α) (i : ι) : (: f i ≤ supr f :) := le_Sup ⟨i, rfl⟩ @[ematch] lemma infi_le' (f : ι → α) (i : ι) : (: infi f ≤ f i :) := Inf_le ⟨i, rfl⟩ /- TODO: this version would be more powerful, but, alas, the pattern matcher doesn't accept it. @[ematch] lemma le_supr' (f : ι → α) (i : ι) : (: f i :) ≤ (: supr f :) := le_Sup ⟨i, rfl⟩ -/ lemma is_lub_supr : is_lub (range f) (⨆ j, f j) := is_lub_Sup _ lemma is_glb_infi : is_glb (range f) (⨅ j, f j) := is_glb_Inf _ lemma is_lub.supr_eq (h : is_lub (range f) a) : (⨆ j, f j) = a := h.Sup_eq lemma is_glb.infi_eq (h : is_glb (range f) a) : (⨅ j, f j) = a := h.Inf_eq lemma le_supr_of_le (i : ι) (h : a ≤ f i) : a ≤ supr f := h.trans $ le_supr _ i lemma infi_le_of_le (i : ι) (h : f i ≤ a) : infi f ≤ a := (infi_le _ i).trans h lemma le_supr₂ {f : Π i, κ i → α} (i : ι) (j : κ i) : f i j ≤ ⨆ i j, f i j := le_supr_of_le i $ le_supr (f i) j lemma infi₂_le {f : Π i, κ i → α} (i : ι) (j : κ i) : (⨅ i j, f i j) ≤ f i j := infi_le_of_le i $ infi_le (f i) j lemma le_supr₂_of_le {f : Π i, κ i → α} (i : ι) (j : κ i) (h : a ≤ f i j) : a ≤ ⨆ i j, f i j := h.trans $ le_supr₂ i j lemma infi₂_le_of_le {f : Π i, κ i → α} (i : ι) (j : κ i) (h : f i j ≤ a) : (⨅ i j, f i j) ≤ a := (infi₂_le i j).trans h lemma supr_le (h : ∀ i, f i ≤ a) : supr f ≤ a := Sup_le $ λ b ⟨i, eq⟩, eq ▸ h i lemma le_infi (h : ∀ i, a ≤ f i) : a ≤ infi f := le_Inf $ λ b ⟨i, eq⟩, eq ▸ h i lemma supr₂_le {f : Π i, κ i → α} (h : ∀ i j, f i j ≤ a) : (⨆ i j, f i j) ≤ a := supr_le $ λ i, supr_le $ h i lemma le_infi₂ {f : Π i, κ i → α} (h : ∀ i j, a ≤ f i j) : a ≤ ⨅ i j, f i j := le_infi $ λ i, le_infi $ h i lemma supr₂_le_supr (κ : ι → Sort*) (f : ι → α) : (⨆ i (j : κ i), f i) ≤ ⨆ i, f i := supr₂_le $ λ i j, le_supr f i lemma infi_le_infi₂ (κ : ι → Sort*) (f : ι → α) : (⨅ i, f i) ≤ ⨅ i (j : κ i), f i := le_infi₂ $ λ i j, infi_le f i lemma supr_mono (h : ∀ i, f i ≤ g i) : supr f ≤ supr g := supr_le $ λ i, le_supr_of_le i $ h i lemma infi_mono (h : ∀ i, f i ≤ g i) : infi f ≤ infi g := le_infi $ λ i, infi_le_of_le i $ h i lemma supr₂_mono {f g : Π i, κ i → α} (h : ∀ i j, f i j ≤ g i j) : (⨆ i j, f i j) ≤ ⨆ i j, g i j := supr_mono $ λ i, supr_mono $ h i lemma infi₂_mono {f g : Π i, κ i → α} (h : ∀ i j, f i j ≤ g i j) : (⨅ i j, f i j) ≤ ⨅ i j, g i j := infi_mono $ λ i, infi_mono $ h i lemma supr_mono' {g : ι' → α} (h : ∀ i, ∃ i', f i ≤ g i') : supr f ≤ supr g := supr_le $ λ i, exists.elim (h i) le_supr_of_le lemma infi_mono' {g : ι' → α} (h : ∀ i', ∃ i, f i ≤ g i') : infi f ≤ infi g := le_infi $ λ i', exists.elim (h i') infi_le_of_le lemma supr₂_mono' {f : Π i, κ i → α} {g : Π i', κ' i' → α} (h : ∀ i j, ∃ i' j', f i j ≤ g i' j') : (⨆ i j, f i j) ≤ ⨆ i j, g i j := supr₂_le $ λ i j, let ⟨i', j', h⟩ := h i j in le_supr₂_of_le i' j' h lemma infi₂_mono' {f : Π i, κ i → α} {g : Π i', κ' i' → α} (h : ∀ i j, ∃ i' j', f i' j' ≤ g i j) : (⨅ i j, f i j) ≤ ⨅ i j, g i j := le_infi₂ $ λ i j, let ⟨i', j', h⟩ := h i j in infi₂_le_of_le i' j' h lemma supr_const_mono (h : ι → ι') : (⨆ i : ι, a) ≤ ⨆ j : ι', a := supr_le $ le_supr _ ∘ h lemma infi_const_mono (h : ι' → ι) : (⨅ i : ι, a) ≤ ⨅ j : ι', a := le_infi $ infi_le _ ∘ h lemma supr_infi_le_infi_supr (f : ι → ι' → α) : (⨆ i, ⨅ j, f i j) ≤ (⨅ j, ⨆ i, f i j) := supr_le $ λ i, infi_mono $ λ j, le_supr _ i lemma bsupr_mono {p q : ι → Prop} (hpq : ∀ i, p i → q i) : (⨆ i (h : p i), f i) ≤ ⨆ i (h : q i), f i := supr_mono $ λ i, supr_const_mono (hpq i) lemma binfi_mono {p q : ι → Prop} (hpq : ∀ i, p i → q i) : (⨅ i (h : q i), f i) ≤ ⨅ i (h : p i), f i := infi_mono $ λ i, infi_const_mono (hpq i) @[simp] lemma supr_le_iff : supr f ≤ a ↔ ∀ i, f i ≤ a := (is_lub_le_iff is_lub_supr).trans forall_range_iff @[simp] lemma le_infi_iff : a ≤ infi f ↔ ∀ i, a ≤ f i := (le_is_glb_iff is_glb_infi).trans forall_range_iff @[simp] lemma supr₂_le_iff {f : Π i, κ i → α} : (⨆ i j, f i j) ≤ a ↔ ∀ i j, f i j ≤ a := by simp_rw supr_le_iff @[simp] lemma le_infi₂_iff {f : Π i, κ i → α} : a ≤ (⨅ i j, f i j) ↔ ∀ i j, a ≤ f i j := by simp_rw le_infi_iff lemma supr_lt_iff : supr f < a ↔ ∃ b, b < a ∧ ∀ i, f i ≤ b := ⟨λ h, ⟨supr f, h, le_supr f⟩, λ ⟨b, h, hb⟩, (supr_le hb).trans_lt h⟩ lemma lt_infi_iff : a < infi f ↔ ∃ b, a < b ∧ ∀ i, b ≤ f i := ⟨λ h, ⟨infi f, h, infi_le f⟩, λ ⟨b, h, hb⟩, h.trans_le $ le_infi hb⟩ lemma Sup_eq_supr {s : set α} : Sup s = ⨆ a ∈ s, a := le_antisymm (Sup_le le_supr₂) (supr₂_le $ λ b, le_Sup) lemma Inf_eq_infi {s : set α} : Inf s = ⨅ a ∈ s, a := @Sup_eq_supr αᵒᵈ _ _ lemma monotone.le_map_supr [complete_lattice β] {f : α → β} (hf : monotone f) : (⨆ i, f (s i)) ≤ f (supr s) := supr_le $ λ i, hf $ le_supr _ _ lemma antitone.le_map_infi [complete_lattice β] {f : α → β} (hf : antitone f) : (⨆ i, f (s i)) ≤ f (infi s) := hf.dual_left.le_map_supr lemma monotone.le_map_supr₂ [complete_lattice β] {f : α → β} (hf : monotone f) (s : Π i, κ i → α) : (⨆ i j, f (s i j)) ≤ f (⨆ i j, s i j) := supr₂_le $ λ i j, hf $ le_supr₂ _ _ lemma antitone.le_map_infi₂ [complete_lattice β] {f : α → β} (hf : antitone f) (s : Π i, κ i → α) : (⨆ i j, f (s i j)) ≤ f (⨅ i j, s i j) := hf.dual_left.le_map_supr₂ _ lemma monotone.le_map_Sup [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) : (⨆ a ∈ s, f a) ≤ f (Sup s) := by rw [Sup_eq_supr]; exact hf.le_map_supr₂ _ lemma antitone.le_map_Inf [complete_lattice β] {s : set α} {f : α → β} (hf : antitone f) : (⨆ a ∈ s, f a) ≤ f (Inf s) := hf.dual_left.le_map_Sup lemma order_iso.map_supr [complete_lattice β] (f : α ≃o β) (x : ι → α) : f (⨆ i, x i) = ⨆ i, f (x i) := eq_of_forall_ge_iff $ f.surjective.forall.2 $ λ x, by simp only [f.le_iff_le, supr_le_iff] lemma order_iso.map_infi [complete_lattice β] (f : α ≃o β) (x : ι → α) : f (⨅ i, x i) = ⨅ i, f (x i) := order_iso.map_supr f.dual _ lemma order_iso.map_Sup [complete_lattice β] (f : α ≃o β) (s : set α) : f (Sup s) = ⨆ a ∈ s, f a := by simp only [Sup_eq_supr, order_iso.map_supr] lemma order_iso.map_Inf [complete_lattice β] (f : α ≃o β) (s : set α) : f (Inf s) = ⨅ a ∈ s, f a := order_iso.map_Sup f.dual _ lemma supr_comp_le {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨆ x, f (g x)) ≤ ⨆ y, f y := supr_mono' $ λ x, ⟨_, le_rfl⟩ lemma le_infi_comp {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨅ y, f y) ≤ ⨅ x, f (g x) := infi_mono' $ λ x, ⟨_, le_rfl⟩ lemma monotone.supr_comp_eq [preorder β] {f : β → α} (hf : monotone f) {s : ι → β} (hs : ∀ x, ∃ i, x ≤ s i) : (⨆ x, f (s x)) = ⨆ y, f y := le_antisymm (supr_comp_le _ _) (supr_mono' $ λ x, (hs x).imp $ λ i hi, hf hi) lemma monotone.infi_comp_eq [preorder β] {f : β → α} (hf : monotone f) {s : ι → β} (hs : ∀ x, ∃ i, s i ≤ x) : (⨅ x, f (s x)) = ⨅ y, f y := le_antisymm (infi_mono' $ λ x, (hs x).imp $ λ i hi, hf hi) (le_infi_comp _ _) lemma antitone.map_supr_le [complete_lattice β] {f : α → β} (hf : antitone f) : f (supr s) ≤ ⨅ i, f (s i) := le_infi $ λ i, hf $ le_supr _ _ lemma monotone.map_infi_le [complete_lattice β] {f : α → β} (hf : monotone f) : f (infi s) ≤ (⨅ i, f (s i)) := hf.dual_left.map_supr_le lemma antitone.map_supr₂_le [complete_lattice β] {f : α → β} (hf : antitone f) (s : Π i, κ i → α) : f (⨆ i j, s i j) ≤ ⨅ i j, f (s i j) := hf.dual.le_map_infi₂ _ lemma monotone.map_infi₂_le [complete_lattice β] {f : α → β} (hf : monotone f) (s : Π i, κ i → α) : f (⨅ i j, s i j) ≤ ⨅ i j, f (s i j) := hf.dual.le_map_supr₂ _ lemma antitone.map_Sup_le [complete_lattice β] {s : set α} {f : α → β} (hf : antitone f) : f (Sup s) ≤ ⨅ a ∈ s, f a := by { rw Sup_eq_supr, exact hf.map_supr₂_le _ } lemma monotone.map_Inf_le [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) : f (Inf s) ≤ ⨅ a ∈ s, f a := hf.dual_left.map_Sup_le lemma supr_const_le : (⨆ i : ι, a) ≤ a := supr_le $ λ _, le_rfl lemma le_infi_const : a ≤ ⨅ i : ι, a := le_infi $ λ _, le_rfl /- We generalize this to conditionally complete lattices in `csupr_const` and `cinfi_const`. -/ theorem supr_const [nonempty ι] : (⨆ b : ι, a) = a := by rw [supr, range_const, Sup_singleton] theorem infi_const [nonempty ι] : (⨅ b : ι, a) = a := @supr_const αᵒᵈ _ _ a _ @[simp] lemma supr_bot : (⨆ i : ι, ⊥ : α) = ⊥ := bot_unique supr_const_le @[simp] lemma infi_top : (⨅ i : ι, ⊤ : α) = ⊤ := top_unique le_infi_const @[simp] lemma supr_eq_bot : supr s = ⊥ ↔ ∀ i, s i = ⊥ := Sup_eq_bot.trans forall_range_iff @[simp] lemma infi_eq_top : infi s = ⊤ ↔ ∀ i, s i = ⊤ := Inf_eq_top.trans forall_range_iff @[simp] lemma supr₂_eq_bot {f : Π i, κ i → α} : (⨆ i j, f i j) = ⊥ ↔ ∀ i j, f i j = ⊥ := by simp_rw supr_eq_bot @[simp] lemma infi₂_eq_top {f : Π i, κ i → α} : (⨅ i j, f i j) = ⊤ ↔ ∀ i j, f i j = ⊤ := by simp_rw infi_eq_top @[simp] lemma supr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := le_antisymm (supr_le $ λ h, le_rfl) (le_supr _ _) @[simp] lemma infi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := le_antisymm (infi_le _ _) (le_infi $ λ h, le_rfl) @[simp] lemma supr_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨆ h : p, f h) = ⊥ := le_antisymm (supr_le $ λ h, (hp h).elim) bot_le @[simp] lemma infi_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨅ h : p, f h) = ⊤ := le_antisymm le_top $ le_infi $ λ h, (hp h).elim /--Introduction rule to prove that `b` is the supremum of `f`: it suffices to check that `b` is larger than `f i` for all `i`, and that this is not the case of any `w<b`. See `csupr_eq_of_forall_le_of_forall_lt_exists_gt` for a version in conditionally complete lattices. -/ theorem supr_eq_of_forall_le_of_forall_lt_exists_gt {f : ι → α} (h₁ : ∀ i, f i ≤ b) (h₂ : ∀ w, w < b → (∃ i, w < f i)) : (⨆ (i : ι), f i) = b := Sup_eq_of_forall_le_of_forall_lt_exists_gt (forall_range_iff.mpr h₁) (λ w hw, exists_range_iff.mpr $ h₂ w hw) /--Introduction rule to prove that `b` is the infimum of `f`: it suffices to check that `b` is smaller than `f i` for all `i`, and that this is not the case of any `w>b`. See `cinfi_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in conditionally complete lattices. -/ theorem infi_eq_of_forall_ge_of_forall_gt_exists_lt : (∀ i, b ≤ f i) → (∀ w, b < w → ∃ i, f i < w) → (⨅ i, f i) = b := @supr_eq_of_forall_le_of_forall_lt_exists_gt αᵒᵈ _ _ _ _ lemma supr_eq_dif {p : Prop} [decidable p] (a : p → α) : (⨆ h : p, a h) = if h : p then a h else ⊥ := by by_cases p; simp [h] lemma supr_eq_if {p : Prop} [decidable p] (a : α) : (⨆ h : p, a) = if p then a else ⊥ := supr_eq_dif (λ _, a) lemma infi_eq_dif {p : Prop} [decidable p] (a : p → α) : (⨅ h : p, a h) = if h : p then a h else ⊤ := @supr_eq_dif αᵒᵈ _ _ _ _ lemma infi_eq_if {p : Prop} [decidable p] (a : α) : (⨅ h : p, a) = if p then a else ⊤ := infi_eq_dif (λ _, a) lemma supr_comm {f : ι → ι' → α} : (⨆ i j, f i j) = ⨆ j i, f i j := le_antisymm (supr_le $ λ i, supr_mono $ λ j, le_supr _ i) (supr_le $ λ j, supr_mono $ λ i, le_supr _ _) lemma infi_comm {f : ι → ι' → α} : (⨅ i j, f i j) = ⨅ j i, f i j := @supr_comm αᵒᵈ _ _ _ _ lemma supr₂_comm {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} (f : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → α) : (⨆ i₁ j₁ i₂ j₂, f i₁ j₁ i₂ j₂) = ⨆ i₂ j₂ i₁ j₁, f i₁ j₁ i₂ j₂ := by simp only [@supr_comm _ (κ₁ _), @supr_comm _ ι₁] lemma infi₂_comm {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*} (f : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → α) : (⨅ i₁ j₁ i₂ j₂, f i₁ j₁ i₂ j₂) = ⨅ i₂ j₂ i₁ j₁, f i₁ j₁ i₂ j₂ := by simp only [@infi_comm _ (κ₁ _), @infi_comm _ ι₁] /- TODO: this is strange. In the proof below, we get exactly the desired among the equalities, but close does not get it. begin apply @le_antisymm, simp, intros, begin [smt] ematch, ematch, ematch, trace_state, have := le_refl (f i_1 i), trace_state, close end end -/ @[simp] theorem supr_supr_eq_left {b : β} {f : Π x : β, x = b → α} : (⨆ x, ⨆ h : x = b, f x h) = f b rfl := (@le_supr₂ _ _ _ _ f b rfl).antisymm' (supr_le $ λ c, supr_le $ by { rintro rfl, refl }) @[simp] theorem infi_infi_eq_left {b : β} {f : Π x : β, x = b → α} : (⨅ x, ⨅ h : x = b, f x h) = f b rfl := @supr_supr_eq_left αᵒᵈ _ _ _ _ @[simp] theorem supr_supr_eq_right {b : β} {f : Π x : β, b = x → α} : (⨆ x, ⨆ h : b = x, f x h) = f b rfl := (le_supr₂ b rfl).antisymm' (supr₂_le $ λ c, by { rintro rfl, refl }) @[simp] theorem infi_infi_eq_right {b : β} {f : Π x : β, b = x → α} : (⨅ x, ⨅ h : b = x, f x h) = f b rfl := @supr_supr_eq_right αᵒᵈ _ _ _ _ attribute [ematch] le_refl theorem supr_subtype {p : ι → Prop} {f : subtype p → α} : supr f = (⨆ i (h : p i), f ⟨i, h⟩) := le_antisymm (supr_le $ λ ⟨i, h⟩, le_supr₂ i h) (supr₂_le $ λ i h, le_supr _ _) theorem infi_subtype : ∀ {p : ι → Prop} {f : subtype p → α}, infi f = (⨅ i (h : p i), f ⟨i, h⟩) := @supr_subtype αᵒᵈ _ _ lemma supr_subtype' {p : ι → Prop} {f : Π i, p i → α} : (⨆ i h, f i h) = ⨆ x : subtype p, f x x.property := (@supr_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma infi_subtype' {p : ι → Prop} {f : ∀ i, p i → α} : (⨅ i (h : p i), f i h) = (⨅ x : subtype p, f x x.property) := (@infi_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma supr_subtype'' {ι} (s : set ι) (f : ι → α) : (⨆ i : s, f i) = ⨆ (t : ι) (H : t ∈ s), f t := supr_subtype lemma infi_subtype'' {ι} (s : set ι) (f : ι → α) : (⨅ i : s, f i) = ⨅ (t : ι) (H : t ∈ s), f t := infi_subtype theorem supr_sup_eq : (⨆ x, f x ⊔ g x) = (⨆ x, f x) ⊔ (⨆ x, g x) := le_antisymm (supr_le $ λ i, sup_le_sup (le_supr _ _) $ le_supr _ _) (sup_le (supr_mono $ λ i, le_sup_left) $ supr_mono $ λ i, le_sup_right) theorem infi_inf_eq : (⨅ x, f x ⊓ g x) = (⨅ x, f x) ⊓ (⨅ x, g x) := @supr_sup_eq αᵒᵈ _ _ _ _ /- TODO: here is another example where more flexible pattern matching might help. begin apply @le_antisymm, safe, pose h := f a ⊓ g a, begin [smt] ematch, ematch end end -/ lemma supr_sup [nonempty ι] {f : ι → α} {a : α} : (⨆ x, f x) ⊔ a = ⨆ x, f x ⊔ a := by rw [supr_sup_eq, supr_const] lemma infi_inf [nonempty ι] {f : ι → α} {a : α} : (⨅ x, f x) ⊓ a = ⨅ x, f x ⊓ a := by rw [infi_inf_eq, infi_const] lemma sup_supr [nonempty ι] {f : ι → α} {a : α} : a ⊔ (⨆ x, f x) = ⨆ x, a ⊔ f x := by rw [supr_sup_eq, supr_const] lemma inf_infi [nonempty ι] {f : ι → α} {a : α} : a ⊓ (⨅ x, f x) = ⨅ x, a ⊓ f x := by rw [infi_inf_eq, infi_const] lemma binfi_inf {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) : (⨅ i (h : p i), f i h) ⊓ a = ⨅ i (h : p i), f i h ⊓ a := by haveI : nonempty {i // p i} := (let ⟨i, hi⟩ := h in ⟨⟨i, hi⟩⟩); rw [infi_subtype', infi_subtype', infi_inf] lemma inf_binfi {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) : a ⊓ (⨅ i (h : p i), f i h) = ⨅ i (h : p i), a ⊓ f i h := by simpa only [inf_comm] using binfi_inf h /-! ### `supr` and `infi` under `Prop` -/ @[simp] theorem supr_false {s : false → α} : supr s = ⊥ := le_antisymm (supr_le $ λ i, false.elim i) bot_le @[simp] theorem infi_false {s : false → α} : infi s = ⊤ := le_antisymm le_top (le_infi $ λ i, false.elim i) lemma supr_true {s : true → α} : supr s = s trivial := supr_pos trivial lemma infi_true {s : true → α} : infi s = s trivial := infi_pos trivial @[simp] lemma supr_exists {p : ι → Prop} {f : Exists p → α} : (⨆ x, f x) = ⨆ i h, f ⟨i, h⟩ := le_antisymm (supr_le $ λ ⟨i, h⟩, le_supr₂ i h) (supr₂_le $ λ i h, le_supr _ _) @[simp] lemma infi_exists {p : ι → Prop} {f : Exists p → α} : (⨅ x, f x) = ⨅ i h, f ⟨i, h⟩ := @supr_exists αᵒᵈ _ _ _ _ lemma supr_and {p q : Prop} {s : p ∧ q → α} : supr s = ⨆ h₁ h₂, s ⟨h₁, h₂⟩ := le_antisymm (supr_le $ λ ⟨i, h⟩, le_supr₂ i h) (supr₂_le $ λ i h, le_supr _ _) lemma infi_and {p q : Prop} {s : p ∧ q → α} : infi s = ⨅ h₁ h₂, s ⟨h₁, h₂⟩ := @supr_and αᵒᵈ _ _ _ _ /-- The symmetric case of `supr_and`, useful for rewriting into a supremum over a conjunction -/ lemma supr_and' {p q : Prop} {s : p → q → α} : (⨆ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨆ (h : p ∧ q), s h.1 h.2 := eq.symm supr_and /-- The symmetric case of `infi_and`, useful for rewriting into a infimum over a conjunction -/ lemma infi_and' {p q : Prop} {s : p → q → α} : (⨅ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨅ (h : p ∧ q), s h.1 h.2 := eq.symm infi_and theorem supr_or {p q : Prop} {s : p ∨ q → α} : (⨆ x, s x) = (⨆ i, s (or.inl i)) ⊔ (⨆ j, s (or.inr j)) := le_antisymm (supr_le $ λ i, match i with | or.inl i := le_sup_of_le_left $ le_supr _ i | or.inr j := le_sup_of_le_right $ le_supr _ j end) (sup_le (supr_comp_le _ _) (supr_comp_le _ _)) theorem infi_or {p q : Prop} {s : p ∨ q → α} : (⨅ x, s x) = (⨅ i, s (or.inl i)) ⊓ (⨅ j, s (or.inr j)) := @supr_or αᵒᵈ _ _ _ _ section variables (p : ι → Prop) [decidable_pred p] lemma supr_dite (f : Π i, p i → α) (g : Π i, ¬p i → α) : (⨆ i, if h : p i then f i h else g i h) = (⨆ i (h : p i), f i h) ⊔ (⨆ i (h : ¬ p i), g i h) := begin rw ←supr_sup_eq, congr' 1 with i, split_ifs with h; simp [h], end lemma infi_dite (f : Π i, p i → α) (g : Π i, ¬p i → α) : (⨅ i, if h : p i then f i h else g i h) = (⨅ i (h : p i), f i h) ⊓ (⨅ i (h : ¬ p i), g i h) := supr_dite p (show Π i, p i → αᵒᵈ, from f) g lemma supr_ite (f g : ι → α) : (⨆ i, if p i then f i else g i) = (⨆ i (h : p i), f i) ⊔ (⨆ i (h : ¬ p i), g i) := supr_dite _ _ _ lemma infi_ite (f g : ι → α) : (⨅ i, if p i then f i else g i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), g i) := infi_dite _ _ _ end lemma supr_range {g : β → α} {f : ι → β} : (⨆ b ∈ range f, g b) = ⨆ i, g (f i) := by rw [← supr_subtype'', supr_range'] lemma infi_range : ∀ {g : β → α} {f : ι → β}, (⨅ b ∈ range f, g b) = ⨅ i, g (f i) := @supr_range αᵒᵈ _ _ _ theorem Sup_image {s : set β} {f : β → α} : Sup (f '' s) = ⨆ a ∈ s, f a := by rw [← supr_subtype'', Sup_image'] theorem Inf_image {s : set β} {f : β → α} : Inf (f '' s) = ⨅ a ∈ s, f a := @Sup_image αᵒᵈ _ _ _ _ /- ### supr and infi under set constructions -/ theorem supr_emptyset {f : β → α} : (⨆ x ∈ (∅ : set β), f x) = ⊥ := by simp theorem infi_emptyset {f : β → α} : (⨅ x ∈ (∅ : set β), f x) = ⊤ := by simp theorem supr_univ {f : β → α} : (⨆ x ∈ (univ : set β), f x) = ⨆ x, f x := by simp theorem infi_univ {f : β → α} : (⨅ x ∈ (univ : set β), f x) = ⨅ x, f x := by simp theorem supr_union {f : β → α} {s t : set β} : (⨆ x ∈ s ∪ t, f x) = (⨆ x ∈ s, f x) ⊔ (⨆ x ∈ t, f x) := by simp_rw [mem_union, supr_or, supr_sup_eq] theorem infi_union {f : β → α} {s t : set β} : (⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) := @supr_union αᵒᵈ _ _ _ _ _ lemma supr_split (f : β → α) (p : β → Prop) : (⨆ i, f i) = (⨆ i (h : p i), f i) ⊔ (⨆ i (h : ¬ p i), f i) := by simpa [classical.em] using @supr_union _ _ _ f {i | p i} {i | ¬ p i} lemma infi_split : ∀ (f : β → α) (p : β → Prop), (⨅ i, f i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), f i) := @supr_split αᵒᵈ _ _ lemma supr_split_single (f : β → α) (i₀ : β) : (⨆ i, f i) = f i₀ ⊔ ⨆ i (h : i ≠ i₀), f i := by { convert supr_split _ _, simp } lemma infi_split_single (f : β → α) (i₀ : β) : (⨅ i, f i) = f i₀ ⊓ ⨅ i (h : i ≠ i₀), f i := @supr_split_single αᵒᵈ _ _ _ _ lemma supr_le_supr_of_subset {f : β → α} {s t : set β} : s ⊆ t → (⨆ x ∈ s, f x) ≤ ⨆ x ∈ t, f x := bsupr_mono lemma infi_le_infi_of_subset {f : β → α} {s t : set β} : s ⊆ t → (⨅ x ∈ t, f x) ≤ ⨅ x ∈ s, f x := binfi_mono theorem supr_insert {f : β → α} {s : set β} {b : β} : (⨆ x ∈ insert b s, f x) = f b ⊔ (⨆ x ∈ s, f x) := eq.trans supr_union $ congr_arg (λ x, x ⊔ (⨆ x ∈ s, f x)) supr_supr_eq_left theorem infi_insert {f : β → α} {s : set β} {b : β} : (⨅ x ∈ insert b s, f x) = f b ⊓ (⨅ x ∈ s, f x) := eq.trans infi_union $ congr_arg (λ x, x ⊓ (⨅ x ∈ s, f x)) infi_infi_eq_left theorem supr_singleton {f : β → α} {b : β} : (⨆ x ∈ (singleton b : set β), f x) = f b := by simp theorem infi_singleton {f : β → α} {b : β} : (⨅ x ∈ (singleton b : set β), f x) = f b := by simp theorem supr_pair {f : β → α} {a b : β} : (⨆ x ∈ ({a, b} : set β), f x) = f a ⊔ f b := by rw [supr_insert, supr_singleton] theorem infi_pair {f : β → α} {a b : β} : (⨅ x ∈ ({a, b} : set β), f x) = f a ⊓ f b := by rw [infi_insert, infi_singleton] lemma supr_image {γ} {f : β → γ} {g : γ → α} {t : set β} : (⨆ c ∈ f '' t, g c) = (⨆ b ∈ t, g (f b)) := by rw [← Sup_image, ← Sup_image, ← image_comp] lemma infi_image : ∀ {γ} {f : β → γ} {g : γ → α} {t : set β}, (⨅ c ∈ f '' t, g c) = (⨅ b ∈ t, g (f b)) := @supr_image αᵒᵈ _ _ theorem supr_extend_bot {e : ι → β} (he : injective e) (f : ι → α) : (⨆ j, extend e f ⊥ j) = ⨆ i, f i := begin rw supr_split _ (λ j, ∃ i, e i = j), simp [extend_apply he, extend_apply', @supr_comm _ β ι] { contextual := tt } end lemma infi_extend_top {e : ι → β} (he : injective e) (f : ι → α) : (⨅ j, extend e f ⊤ j) = infi f := @supr_extend_bot αᵒᵈ _ _ _ _ he _ /-! ### `supr` and `infi` under `Type` -/ theorem supr_of_empty' {α ι} [has_Sup α] [is_empty ι] (f : ι → α) : supr f = Sup (∅ : set α) := congr_arg Sup (range_eq_empty f) theorem infi_of_empty' {α ι} [has_Inf α] [is_empty ι] (f : ι → α) : infi f = Inf (∅ : set α) := congr_arg Inf (range_eq_empty f) theorem supr_of_empty [is_empty ι] (f : ι → α) : supr f = ⊥ := (supr_of_empty' f).trans Sup_empty theorem infi_of_empty [is_empty ι] (f : ι → α) : infi f = ⊤ := @supr_of_empty αᵒᵈ _ _ _ f lemma supr_bool_eq {f : bool → α} : (⨆b:bool, f b) = f tt ⊔ f ff := by rw [supr, bool.range_eq, Sup_pair, sup_comm] lemma infi_bool_eq {f : bool → α} : (⨅b:bool, f b) = f tt ⊓ f ff := @supr_bool_eq αᵒᵈ _ _ lemma sup_eq_supr (x y : α) : x ⊔ y = ⨆ b : bool, cond b x y := by rw [supr_bool_eq, bool.cond_tt, bool.cond_ff] lemma inf_eq_infi (x y : α) : x ⊓ y = ⨅ b : bool, cond b x y := @sup_eq_supr αᵒᵈ _ _ _ lemma is_glb_binfi {s : set β} {f : β → α} : is_glb (f '' s) (⨅ x ∈ s, f x) := by simpa only [range_comp, subtype.range_coe, infi_subtype'] using @is_glb_infi α s _ (f ∘ coe) lemma is_lub_bsupr {s : set β} {f : β → α} : is_lub (f '' s) (⨆ x ∈ s, f x) := by simpa only [range_comp, subtype.range_coe, supr_subtype'] using @is_lub_supr α s _ (f ∘ coe) theorem supr_sigma {p : β → Type*} {f : sigma p → α} : (⨆ x, f x) = ⨆ i j, f ⟨i, j⟩ := eq_of_forall_ge_iff $ λ c, by simp only [supr_le_iff, sigma.forall] theorem infi_sigma {p : β → Type*} {f : sigma p → α} : (⨅ x, f x) = ⨅ i j, f ⟨i, j⟩ := @supr_sigma αᵒᵈ _ _ _ _ theorem supr_prod {f : β × γ → α} : (⨆ x, f x) = ⨆ i j, f (i, j) := eq_of_forall_ge_iff $ λ c, by simp only [supr_le_iff, prod.forall] theorem infi_prod {f : β × γ → α} : (⨅ x, f x) = ⨅ i j, f (i, j) := @supr_prod αᵒᵈ _ _ _ _ lemma bsupr_prod {f : β × γ → α} {s : set β} {t : set γ} : (⨆ x ∈ s ×ˢ t, f x) = ⨆ (a ∈ s) (b ∈ t), f (a, b) := by { simp_rw [supr_prod, mem_prod, supr_and], exact supr_congr (λ _, supr_comm) } lemma binfi_prod {f : β × γ → α} {s : set β} {t : set γ} : (⨅ x ∈ s ×ˢ t, f x) = ⨅ (a ∈ s) (b ∈ t), f (a, b) := @bsupr_prod αᵒᵈ _ _ _ _ _ _ theorem supr_sum {f : β ⊕ γ → α} : (⨆ x, f x) = (⨆ i, f (sum.inl i)) ⊔ (⨆ j, f (sum.inr j)) := eq_of_forall_ge_iff $ λ c, by simp only [sup_le_iff, supr_le_iff, sum.forall] theorem infi_sum {f : β ⊕ γ → α} : (⨅ x, f x) = (⨅ i, f (sum.inl i)) ⊓ (⨅ j, f (sum.inr j)) := @supr_sum αᵒᵈ _ _ _ _ theorem supr_option (f : option β → α) : (⨆ o, f o) = f none ⊔ ⨆ b, f (option.some b) := eq_of_forall_ge_iff $ λ c, by simp only [supr_le_iff, sup_le_iff, option.forall] theorem infi_option (f : option β → α) : (⨅ o, f o) = f none ⊓ ⨅ b, f (option.some b) := @supr_option αᵒᵈ _ _ _ /-- A version of `supr_option` useful for rewriting right-to-left. -/ lemma supr_option_elim (a : α) (f : β → α) : (⨆ o : option β, o.elim a f) = a ⊔ ⨆ b, f b := by simp [supr_option] /-- A version of `infi_option` useful for rewriting right-to-left. -/ lemma infi_option_elim (a : α) (f : β → α) : (⨅ o : option β, o.elim a f) = a ⊓ ⨅ b, f b := @supr_option_elim αᵒᵈ _ _ _ _ /-- When taking the supremum of `f : ι → α`, the elements of `ι` on which `f` gives `⊥` can be dropped, without changing the result. -/ lemma supr_ne_bot_subtype (f : ι → α) : (⨆ i : {i // f i ≠ ⊥}, f i) = ⨆ i, f i := begin by_cases htriv : ∀ i, f i = ⊥, { simp only [supr_bot, (funext htriv : f = _)] }, refine (supr_comp_le f _).antisymm (supr_mono' $ λ i, _), by_cases hi : f i = ⊥, { rw hi, obtain ⟨i₀, hi₀⟩ := not_forall.mp htriv, exact ⟨⟨i₀, hi₀⟩, bot_le⟩ }, { exact ⟨⟨i, hi⟩, rfl.le⟩ }, end /-- When taking the infimum of `f : ι → α`, the elements of `ι` on which `f` gives `⊤` can be dropped, without changing the result. -/ lemma infi_ne_top_subtype (f : ι → α) : (⨅ i : {i // f i ≠ ⊤}, f i) = ⨅ i, f i := @supr_ne_bot_subtype αᵒᵈ ι _ f lemma Sup_image2 {f : β → γ → α} {s : set β} {t : set γ} : Sup (image2 f s t) = ⨆ (a ∈ s) (b ∈ t), f a b := by rw [←image_prod, Sup_image, bsupr_prod] lemma Inf_image2 {f : β → γ → α} {s : set β} {t : set γ} : Inf (image2 f s t) = ⨅ (a ∈ s) (b ∈ t), f a b := by rw [←image_prod, Inf_image, binfi_prod] /-! ### `supr` and `infi` under `ℕ` -/ lemma supr_ge_eq_supr_nat_add (u : ℕ → α) (n : ℕ) : (⨆ i ≥ n, u i) = ⨆ i, u (i + n) := begin apply le_antisymm; simp only [supr_le_iff], { exact λ i hi, le_Sup ⟨i - n, by { dsimp only, rw tsub_add_cancel_of_le hi }⟩ }, { exact λ i, le_Sup ⟨i + n, supr_pos (nat.le_add_left _ _)⟩ } end lemma infi_ge_eq_infi_nat_add (u : ℕ → α) (n : ℕ) : (⨅ i ≥ n, u i) = ⨅ i, u (i + n) := @supr_ge_eq_supr_nat_add αᵒᵈ _ _ _ lemma monotone.supr_nat_add {f : ℕ → α} (hf : monotone f) (k : ℕ) : (⨆ n, f (n + k)) = ⨆ n, f n := le_antisymm (supr_le $ λ i, le_supr _ (i + k)) $ supr_mono $ λ i, hf $ nat.le_add_right i k lemma antitone.infi_nat_add {f : ℕ → α} (hf : antitone f) (k : ℕ) : (⨅ n, f (n + k)) = ⨅ n, f n := hf.dual_right.supr_nat_add k @[simp] lemma supr_infi_ge_nat_add (f : ℕ → α) (k : ℕ) : (⨆ n, ⨅ i ≥ n, f (i + k)) = ⨆ n, ⨅ i ≥ n, f i := begin have hf : monotone (λ n, ⨅ i ≥ n, f i) := λ n m h, binfi_mono (λ i, h.trans), rw ←monotone.supr_nat_add hf k, { simp_rw [infi_ge_eq_infi_nat_add, ←nat.add_assoc], }, end @[simp] lemma infi_supr_ge_nat_add : ∀ (f : ℕ → α) (k : ℕ), (⨅ n, ⨆ i ≥ n, f (i + k)) = ⨅ n, ⨆ i ≥ n, f i := @supr_infi_ge_nat_add αᵒᵈ _ lemma sup_supr_nat_succ (u : ℕ → α) : u 0 ⊔ (⨆ i, u (i + 1)) = ⨆ i, u i := begin refine eq_of_forall_ge_iff (λ c, _), simp only [sup_le_iff, supr_le_iff], refine ⟨λ h, _, λ h, ⟨h _, λ i, h _⟩⟩, rintro (_|i), exacts [h.1, h.2 i] end lemma inf_infi_nat_succ (u : ℕ → α) : u 0 ⊓ (⨅ i, u (i + 1)) = ⨅ i, u i := @sup_supr_nat_succ αᵒᵈ _ u end section complete_linear_order variables [complete_linear_order α] lemma supr_eq_top (f : ι → α) : supr f = ⊤ ↔ ∀ b < ⊤, ∃ i, b < f i := by simp only [← Sup_range, Sup_eq_top, set.exists_range_iff] lemma infi_eq_bot (f : ι → α) : infi f = ⊥ ↔ ∀ b > ⊥, ∃ i, f i < b := by simp only [← Inf_range, Inf_eq_bot, set.exists_range_iff] end complete_linear_order /-! ### Instances -/ instance Prop.complete_lattice : complete_lattice Prop := { Sup := λ s, ∃ a ∈ s, a, le_Sup := λ s a h p, ⟨a, h, p⟩, Sup_le := λ s a h ⟨b, h', p⟩, h b h' p, Inf := λ s, ∀ a, a ∈ s → a, Inf_le := λ s a h p, p a h, le_Inf := λ s a h p b hb, h b hb p, .. Prop.bounded_order, .. Prop.distrib_lattice } noncomputable instance Prop.complete_linear_order : complete_linear_order Prop := { ..Prop.complete_lattice, ..Prop.linear_order } @[simp] lemma Sup_Prop_eq {s : set Prop} : Sup s = ∃ p ∈ s, p := rfl @[simp] lemma Inf_Prop_eq {s : set Prop} : Inf s = ∀ p ∈ s, p := rfl @[simp] lemma supr_Prop_eq {p : ι → Prop} : (⨆ i, p i) = ∃ i, p i := le_antisymm (λ ⟨q, ⟨i, (eq : p i = q)⟩, hq⟩, ⟨i, eq.symm ▸ hq⟩) (λ ⟨i, hi⟩, ⟨p i, ⟨i, rfl⟩, hi⟩) @[simp] lemma infi_Prop_eq {p : ι → Prop} : (⨅ i, p i) = ∀ i, p i := le_antisymm (λ h i, h _ ⟨i, rfl⟩ ) (λ h p ⟨i, eq⟩, eq ▸ h i) instance pi.has_Sup {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] : has_Sup (Π i, β i) := ⟨λ s i, ⨆ f : s, (f : Π i, β i) i⟩ instance pi.has_Inf {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] : has_Inf (Π i, β i) := ⟨λ s i, ⨅ f : s, (f : Π i, β i) i⟩ instance pi.complete_lattice {α : Type*} {β : α → Type*} [∀ i, complete_lattice (β i)] : complete_lattice (Π i, β i) := { Sup := Sup, Inf := Inf, le_Sup := λ s f hf i, le_supr (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩, Inf_le := λ s f hf i, infi_le (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩, Sup_le := λ s f hf i, supr_le $ λ g, hf g g.2 i, le_Inf := λ s f hf i, le_infi $ λ g, hf g g.2 i, .. pi.bounded_order, .. pi.lattice } lemma Sup_apply {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] {s : set (Π a, β a)} {a : α} : (Sup s) a = ⨆ f : s, (f : Π a, β a) a := rfl lemma Inf_apply {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] {s : set (Π a, β a)} {a : α} : Inf s a = ⨅ f : s, (f : Π a, β a) a := rfl @[simp] lemma supr_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Sup (β i)] {f : ι → Π a, β a} {a : α} : (⨆ i, f i) a = ⨆ i, f i a := by rw [supr, Sup_apply, supr, supr, ← image_eq_range (λ f : Π i, β i, f a) (range f), ← range_comp] @[simp] lemma infi_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Inf (β i)] {f : ι → Π a, β a} {a : α} : (⨅ i, f i) a = ⨅ i, f i a := @supr_apply α (λ i, (β i)ᵒᵈ) _ _ _ _ lemma unary_relation_Sup_iff {α : Type*} (s : set (α → Prop)) {a : α} : Sup s a ↔ ∃ r : α → Prop, r ∈ s ∧ r a := by { unfold Sup, simp [←eq_iff_iff] } lemma unary_relation_Inf_iff {α : Type*} (s : set (α → Prop)) {a : α} : Inf s a ↔ ∀ r : α → Prop, r ∈ s → r a := by { unfold Inf, simp [←eq_iff_iff] } lemma binary_relation_Sup_iff {α β : Type*} (s : set (α → β → Prop)) {a : α} {b : β} : Sup s a b ↔ ∃ r : α → β → Prop, r ∈ s ∧ r a b := by { unfold Sup, simp [←eq_iff_iff] } lemma binary_relation_Inf_iff {α β : Type*} (s : set (α → β → Prop)) {a : α} {b : β} : Inf s a b ↔ ∀ r : α → β → Prop, r ∈ s → r a b := by { unfold Inf, simp [←eq_iff_iff] } section complete_lattice variables [preorder α] [complete_lattice β] theorem monotone_Sup_of_monotone {s : set (α → β)} (m_s : ∀ f ∈ s, monotone f) : monotone (Sup s) := λ x y h, supr_mono $ λ f, m_s f f.2 h theorem monotone_Inf_of_monotone {s : set (α → β)} (m_s : ∀ f ∈ s, monotone f) : monotone (Inf s) := λ x y h, infi_mono $ λ f, m_s f f.2 h end complete_lattice namespace prod variables (α β) instance [has_Sup α] [has_Sup β] : has_Sup (α × β) := ⟨λ s, (Sup (prod.fst '' s), Sup (prod.snd '' s))⟩ instance [has_Inf α] [has_Inf β] : has_Inf (α × β) := ⟨λ s, (Inf (prod.fst '' s), Inf (prod.snd '' s))⟩ instance [complete_lattice α] [complete_lattice β] : complete_lattice (α × β) := { le_Sup := λ s p hab, ⟨le_Sup $ mem_image_of_mem _ hab, le_Sup $ mem_image_of_mem _ hab⟩, Sup_le := λ s p h, ⟨ Sup_le $ ball_image_of_ball $ λ p hp, (h p hp).1, Sup_le $ ball_image_of_ball $ λ p hp, (h p hp).2⟩, Inf_le := λ s p hab, ⟨Inf_le $ mem_image_of_mem _ hab, Inf_le $ mem_image_of_mem _ hab⟩, le_Inf := λ s p h, ⟨ le_Inf $ ball_image_of_ball $ λ p hp, (h p hp).1, le_Inf $ ball_image_of_ball $ λ p hp, (h p hp).2⟩, .. prod.lattice α β, .. prod.bounded_order α β, .. prod.has_Sup α β, .. prod.has_Inf α β } end prod section complete_lattice variables [complete_lattice α] {a : α} {s : set α} /-- This is a weaker version of `sup_Inf_eq` -/ lemma sup_Inf_le_infi_sup : a ⊔ Inf s ≤ ⨅ b ∈ s, a ⊔ b := le_infi₂ $ λ i h, sup_le_sup_left (Inf_le h) _ /-- This is a weaker version of `inf_Sup_eq` -/ lemma supr_inf_le_inf_Sup : (⨆ b ∈ s, a ⊓ b) ≤ a ⊓ Sup s := @sup_Inf_le_infi_sup αᵒᵈ _ _ _ /-- This is a weaker version of `Inf_sup_eq` -/ lemma Inf_sup_le_infi_sup : Inf s ⊔ a ≤ ⨅ b ∈ s, b ⊔ a := le_infi₂ $ λ i h, sup_le_sup_right (Inf_le h) _ /-- This is a weaker version of `Sup_inf_eq` -/ lemma supr_inf_le_Sup_inf : (⨆ b ∈ s, b ⊓ a) ≤ Sup s ⊓ a := @Inf_sup_le_infi_sup αᵒᵈ _ _ _ lemma le_supr_inf_supr (f g : ι → α) : (⨆ i, f i ⊓ g i) ≤ (⨆ i, f i) ⊓ (⨆ i, g i) := le_inf (supr_mono $ λ i, inf_le_left) (supr_mono $ λ i, inf_le_right) lemma infi_sup_infi_le (f g : ι → α) : (⨅ i, f i) ⊔ (⨅ i, g i) ≤ ⨅ i, f i ⊔ g i := @le_supr_inf_supr αᵒᵈ ι _ f g lemma disjoint_Sup_left {a : set α} {b : α} (d : disjoint (Sup a) b) {i} (hi : i ∈ a) : disjoint i b := (supr₂_le_iff.1 (supr_inf_le_Sup_inf.trans d) i hi : _) lemma disjoint_Sup_right {a : set α} {b : α} (d : disjoint b (Sup a)) {i} (hi : i ∈ a) : disjoint b i := (supr₂_le_iff.mp (supr_inf_le_inf_Sup.trans d) i hi : _) end complete_lattice /-- Pullback a `complete_lattice` along an injection. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.complete_lattice [has_sup α] [has_inf α] [has_Sup α] [has_Inf α] [has_top α] [has_bot α] [complete_lattice β] (f : α → β) (hf : function.injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_Sup : ∀ s, f (Sup s) = ⨆ a ∈ s, f a) (map_Inf : ∀ s, f (Inf s) = ⨅ a ∈ s, f a) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) : complete_lattice α := { Sup := Sup, le_Sup := λ s a h, (le_supr₂ a h).trans (map_Sup _).ge, Sup_le := λ s a h, (map_Sup _).trans_le $ supr₂_le h, Inf := Inf, Inf_le := λ s a h, (map_Inf _).trans_le $ infi₂_le a h, le_Inf := λ s a h, (le_infi₂ h).trans (map_Inf _).ge, -- we cannot use bounded_order.lift here as the `has_le` instance doesn't exist yet top := ⊤, le_top := λ a, (@le_top β _ _ _).trans map_top.ge, bot := ⊥, bot_le := λ a, map_bot.le.trans bot_le, ..hf.lattice f map_sup map_inf }
5890a52ec8684679c29a91771e494746f0dd96d8
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/formal/i_category/definitions.lean
9976cad0f8d6c33e768a72ea2e7ce3e630222eb2
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
2,861
lean
import homotopy_theory.formal.cofibrations.precofibration_category import homotopy_theory.formal.cylinder.definitions import homotopy_theory.formal.cylinder.hep import homotopy_theory.formal.weak_equivalences.definitions universes v u open category_theory local notation f ` ∘ `:80 g:80 := g ≫ f namespace homotopy_theory.cofibrations open homotopy_theory.cylinder /- An I-category [Baues, Algebraic homotopy, §I.3] is a precofibration category C equipped with a cylinder functor satisfying the following additional axioms. * C has an initial object and every object of C is cofibrant. From the axioms of a precofibration category, it follows that C admits coproducts. Because we will need these coproducts in order to state a later axiom, we assume that C already comes equipped with a choice of coproducts in order to avoid non-definitionally equal instances. * The cylinder functor I preserves pushouts by cofibrations and the initial object. * Cofibrations have the two-sided HEP with respect to the cylinder I. * The relative cylinder axiom: using the notation ∂I A = A ⊔ A, for each cofibration A → X, in the square ∂I A → I A ↓ ↓ ∂I X → I X, the induced map from the pushout to I X is again a cofibration. (The pushout exists because ∂I A → ∂I X is a cofibration.) * The cylinder I is equipped with an interchange structure. -/ variables (C : Type u) [category.{v} C] [has_initial_object.{v} C] [has_coproducts.{v} C] class I_category extends has_cylinder C, preserves_initial_object (I : C ↝ C), precofibration_category C, all_objects_cofibrant.{v} C, cylinder_has_interchange.{v} C := (I_preserves_pushout_by_cof : Π {a b a' b'} {f : a ⟶ b} {g : a ⟶ a'} {f' : a' ⟶ b'} {g' : b ⟶ b'}, is_cof f → Is_pushout f g g' f' → Is_pushout (I &> f) (I &> g) (I &> g') (I &> f')) (hep_cof : ∀ {a b} (j : a ⟶ b), is_cof j → two_sided_hep j) (relative_cylinder : ∀ {a b} (j : a ⟶ b) (hj : is_cof j), is_cof $ (pushout_by_cof (∂I &> j) (ii @> a) (cof_coprod hj hj)).is_pushout.induced (ii @> b) (I &> j) (ii.naturality _)) open precofibration_category open I_category variables {C} -- Alternate formulation of the relative cylinder axiom, using an -- arbitrary pushout rather than the one given by the precofibration -- category structure. lemma relative_cylinder' [I_category.{v} C] {a b : C} (j : a ⟶ b) (hj : is_cof j) {z} (ii' : ∂I.obj b ⟶ z) (jj' : I.obj a ⟶ z) (po : Is_pushout (∂I &> j) (ii @> a) ii' jj') : is_cof (po.induced (ii @> b) (I &> j) (ii.naturality _)) := let po' := (pushout_by_cof (∂I &> j) (ii @> a) (cof_coprod hj hj)).is_pushout in by convert cof_comp (cof_iso (pushout.unique po po')) (relative_cylinder j hj); apply po.uniqueness; rw ←category.assoc; simp; refl end homotopy_theory.cofibrations
88b5a403b23f8bca6bf86035274201334af104a9
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/linear_algebra/nonsingular_inverse.lean
eb128079149f9e5f1814b22b08245e50f264d0e1
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
15,603
lean
/- Copyright (c) 2019 Tim Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Tim Baanen. Inverses for nonsingular square matrices. -/ import algebra.associated import linear_algebra.determinant import tactic.linarith import tactic.ring_exp /-! # Nonsingular inverses In this file, we define an inverse for square matrices of invertible determinant. For matrices that are not square or not of full rank, there is a more general notion of pseudoinverses. Unfortunately, the definition of pseudoinverses is typically in terms of inverses of nonsingular matrices, so we need to define those first. The file also doesn't define a `has_inv` instance for `matrix` so that can be used for the pseudoinverse instead. The definition of inverse used in this file is the adjugate divided by the determinant. The adjugate is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the determinants of each minor of `A`. Instead of defining a minor to be `A` with column `i` and row `j` deleted, we replace the `i`th column of `A` with the `j`th basis vector; this has the same determinant as the minor but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will result in a multiplicative inverse to `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags matrix inverse, cramer, cramer's rule, adjugate -/ namespace matrix universes u v variables {n : Type u} [fintype n] [decidable_eq n] {α : Type v} open_locale matrix open equiv equiv.perm finset section update /-- Update, i.e. replace the `i`th column of matrix `A` with the values in `b`. -/ def update_column (A : matrix n n α) (i : n) (b : n → α) : matrix n n α := function.update A i b /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row (A : matrix n n α) (j : n) (b : n → α) : matrix n n α := λ i, function.update (A i) j (b i) variables {A : matrix n n α} {i j : n} {b : n → α} @[simp] lemma update_column_self : update_column A i b i = b := function.update_same i b A @[simp] lemma update_row_self : update_row A j b i j = b i := function.update_same j (b i) (A i) @[simp] lemma update_column_ne {i' : n} (i_ne : i' ≠ i) : update_column A i b i' = A i' := function.update_noteq i_ne b A @[simp] lemma update_row_ne {j' : n} (j_ne : j' ≠ j) : update_row A j b i j' = A i j' := function.update_noteq j_ne (b i) (A i) lemma update_column_val {i' : n} : update_column A i b i' j = if i' = i then b j else A i' j := begin by_cases i' = i, { rw [h, update_column_self, if_pos rfl] }, { rw [update_column_ne h, if_neg h] } end lemma update_row_val {j' : n} : update_row A j b i j' = if j' = j then b i else A i j' := begin by_cases j' = j, { rw [h, update_row_self, if_pos rfl] }, { rw [update_row_ne h, if_neg h] } end lemma update_column_transpose : update_column Aᵀ i b = (update_row A i b)ᵀ := begin ext i' j, rw [transpose_val, update_column_val, update_row_val], refl end end update section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables [comm_ring α] (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map` sends a square matrix `A` and vector `b` to the vector `x` such that `A ⬝ x = b`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := begin have : Π {f : n → n} {i : n} (x : n → α), finset.prod univ (λ (i' : n), (update_column A i x)ᵀ (f i') i') = finset.prod univ (λ (i' : n), if i' = i then x (f i') else A i' (f i')), { intros, congr, ext i', rw [transpose_val, update_column_val] }, split, { intros x y, repeat { rw [cramer_map, ←det_transpose, det] }, rw [←sum_add_distrib], congr, ext σ, rw [←mul_add ↑↑(sign σ)], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, prod_singleton, ←add_mul], refl }, { intros c x, repeat { rw [cramer_map, ←det_transpose, det] }, rw [smul_eq_mul, mul_sum], congr, ext σ, rw [←mul_assoc, mul_comm c, mul_assoc], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, mul_assoc], } end lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- The linear map of vectors associated to Cramer's rule. To help the elaborator, we need to make the type `α` an explicit argument to `cramer`. Otherwise, the coercion `⇑(cramer A) : (n → α) → (n → α)` gives an error because it fails to infer the type (even though `α` can be inferred from `A : matrix n n α`). -/ def cramer (α : Type v) [comm_ring α] (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer α A b i = (A.update_column i b).det := rfl /-- Applying Cramer's rule to a column of the matrix gives a scaled basis vector. -/ lemma cramer_column_self (i : n) : cramer α A (A i) = (λ j, if i = j then A.det else 0) := begin ext j, rw cramer_apply, by_cases i = j, { -- i = j: this entry should be `A.det` rw [if_pos h, ←h], congr, ext i', by_cases h : i' = i, { rw [h, update_column_self] }, { rw [update_column_ne h]} }, { -- i ≠ j: this entry should be 0 rw [if_neg h], apply det_zero_of_column_eq h, rw [update_column_self, update_column_ne], apply h } end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : s.sum (λ x, cramer α A (f x)) = cramer α A (s.sum f) := (linear_map.map_sum (cramer α A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : s.sum (λ x, cramer α A (λ j, f j x) i) = cramer α A (λ (j : n), s.sum (f j)) i := calc s.sum (λ x, cramer α A (λ j, f j x) i) = s.sum (λ x, cramer α A (λ j, f j x)) i : (pi.finset_sum_apply i s _).symm ... = cramer α A (λ (j : n), s.sum (f j)) i : by { rw [sum_cramer, cramer_apply], congr, ext j, apply pi.finset_sum_apply } end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring, while the `inv` section is specifically for invertible matrices. -/ variable [comm_ring α] /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking the determinant of minors, i.e. the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we define the minor as replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer α A (λ j, if i = j then 1 else 0) lemma adjugate_def (A : matrix n n α) : adjugate A = λ i, cramer α A (λ j, if i = j then 1 else 0) := rfl lemma adjugate_val (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_column j (λ j, if i = j then 1 else 0)).det := rfl lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_val, adjugate_val, adjugate_val, update_column_transpose, det_transpose], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'), rw [update_column_val, update_row_val], finish }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : univ.prod (λ (j' : n), update_row A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_row_self], exact if_neg h }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_column_self], apply if_neg, intro h', exact h ((symm_apply_eq σ).mp h'.symm) } end lemma mul_adjugate_val (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer α A (λ j, if k = j then A i k else 0) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.smul], congr, ext, rw [pi.smul_apply, smul_eq_mul, mul_boole], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_val, smul_val, one_val, mul_boole], calc univ.sum (λ (k : n), A i k * adjugate A k j) = univ.sum (λ (k : n), cramer α A (λ j, if k = j then A i k else 0) j) : by {congr, ext k, apply mul_adjugate_val A i j k} ... = cramer α A (λ j, univ.sum (λ (k : n), if k = j then A i k else 0)) j : sum_cramer_apply A univ (λ (j k : n), if k = j then A i k else 0) j ... = cramer α A (A i) j : by { rw [cramer_apply], congr, ext, rw [sum_ite_eq' univ x (A i), if_pos (mem_univ _)] } ... = if i = j then det A else 0 : by rw [cramer_column_self] end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] /-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`, used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels on the left of the equation `A.det * b = A.det ^ n`. -/ lemma det_adjugate_of_cancel {A : matrix n n α} (h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) : (adjugate A).det = A.det ^ (fintype.card n - 1) := h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm ... = A.det ^ fintype.card n : by simp [mul_adjugate]) lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin ext i j, have univ_eq_i := univ_eq_singleton_of_card_one i h, have univ_eq_j := univ_eq_singleton_of_card_one j h, have i_eq_j : i = j := singleton_inj.mp (by rw [←univ_eq_i, univ_eq_j]), have perm_eq : (univ : finset (perm n)) = finset.singleton 1 := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [adjugate_val, det, univ_eq_i, perm_eq, i_eq_j] end @[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'] end lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 := calc (adjugate A).det = A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb) ... = 1 : by rw [h, one_pow] /-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse. -/ lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩, by_cases card_lt_zero : fintype.card n ≤ 0, { have h : fintype.card n = 0 := by linarith, simp [det_eq_one_of_card_eq_zero h] }, have zero_lt_card : 0 < fintype.card n := by linarith, have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card, by_cases card_lt_one : fintype.card n ≤ 1, { have h : fintype.card n = 1 := by linarith, simp [h, adjugate_eq_one_of_card_eq_one h] }, have one_lt_card : 1 < fintype.card n := by linarith, have zero_lt_card_sub_one : 0 < fintype.card n - 1 := (nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card, apply det_adjugate_of_cancel, intros b hb, calc b = a * (det A ^ (fintype.card n - 1 + 1)) : by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card] ... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp ... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul] end end adjugate section inv /-! ### `inv` section Defines the matrix `nonsing_inv A` and proves it is the inverse matrix of a square matrix `A` as long as `det A` has a multiplicative inverse. -/ variables [comm_ring α] [has_inv α] /-- The inverse of a nonsingular matrix. This is not the most general possible definition, so we don't instantiate `has_inv` (yet). -/ def nonsing_inv (A : matrix n n α) : matrix n n α := (A.det)⁻¹ • adjugate A lemma nonsing_inv_val (A : matrix n n α) (i j : n) : A.nonsing_inv i j = (A.det)⁻¹ * adjugate A i j := rfl lemma transpose_nonsing_inv (A : matrix n n α) : (A.nonsing_inv)ᵀ = (Aᵀ).nonsing_inv := by {ext, simp [transpose_val, nonsing_inv_val, det_transpose, (adjugate_transpose A).symm]} /-- The `nonsing_inv` of `A` is a right inverse. -/ theorem mul_nonsing_inv (A : matrix n n α) (inv_mul_cancel : A.det⁻¹ * A.det = 1) : A ⬝ nonsing_inv A = 1 := by erw [mul_smul, mul_adjugate, smul_smul, inv_mul_cancel, one_smul] /-- The `nonsing_inv` of `A` is a left inverse. -/ theorem nonsing_inv_mul (A : matrix n n α) (inv_mul_cancel : A.det⁻¹ * A.det = 1) : nonsing_inv A ⬝ A = 1 := have inv_mul_cancel' : (det Aᵀ)⁻¹ * det Aᵀ = 1 := by {rw det_transpose, assumption}, calc nonsing_inv A ⬝ A = (Aᵀ ⬝ nonsing_inv (Aᵀ))ᵀ : by rw [transpose_mul, transpose_nonsing_inv, transpose_transpose] ... = 1ᵀ : by rw [mul_nonsing_inv Aᵀ inv_mul_cancel'] ... = 1 : transpose_one end inv end matrix
f4d110b0dacb2cad43d80a1d60702d4b9f84ff6b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/min_max.lean
a68845cba9a50752e9062447e41320f84b2560db
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
7,171
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import order.lattice /-! # `max` and `min` This file proves basic properties about maxima and minima on a `linear_order`. ## Tags min, max -/ universes u v variables {α : Type u} {β : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right section variables [linear_order α] [linear_order β] {f : α → β} {s : set α} {a b c d : α} -- translate from lattices to linear orders (sup → max, inf → min) @[simp] lemma le_min_iff : c ≤ min a b ↔ c ≤ a ∧ c ≤ b := le_inf_iff @[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := le_sup_iff @[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := inf_le_iff @[simp] lemma max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff @[simp] lemma lt_min_iff : a < min b c ↔ a < b ∧ a < c := lt_inf_iff @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := lt_sup_iff @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := inf_lt_iff @[simp] lemma max_lt_iff : max a b < c ↔ a < c ∧ b < c := sup_lt_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_of_le_left : a ≤ b → a ≤ max b c := le_sup_of_le_left lemma le_max_of_le_right : a ≤ c → a ≤ max b c := le_sup_of_le_right lemma lt_max_of_lt_left (h : a < b) : a < max b c := h.trans_le (le_max_left b c) lemma lt_max_of_lt_right (h : a < c) : a < max b c := h.trans_le (le_max_right b c) lemma min_le_of_left_le : a ≤ c → min a b ≤ c := inf_le_of_left_le lemma min_le_of_right_le : b ≤ c → min a b ≤ c := inf_le_of_right_le lemma min_lt_of_left_lt (h : a < c) : min a b < c := (min_le_left a b).trans_lt h lemma min_lt_of_right_lt (h : b < c) : min a b < c := (min_le_right a b).trans_lt h lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right lemma min_le_max : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b) @[simp] lemma min_eq_left_iff : min a b = a ↔ a ≤ b := inf_eq_left @[simp] lemma min_eq_right_iff : min a b = b ↔ b ≤ a := inf_eq_right @[simp] lemma max_eq_left_iff : max a b = a ↔ b ≤ a := sup_eq_left @[simp] lemma max_eq_right_iff : max a b = b ↔ a ≤ b := sup_eq_right /-- For elements `a` and `b` of a linear order, either `min a b = a` and `a ≤ b`, or `min a b = b` and `b < a`. Use cases on this lemma to automate linarith in inequalities -/ lemma min_cases (a b : α) : min a b = a ∧ a ≤ b ∨ min a b = b ∧ b < a := begin by_cases a ≤ b, { left, exact ⟨min_eq_left h, h⟩ }, { right, exact ⟨min_eq_right (le_of_lt (not_le.mp h)), (not_le.mp h)⟩ } end /-- For elements `a` and `b` of a linear order, either `max a b = a` and `b ≤ a`, or `max a b = b` and `a < b`. Use cases on this lemma to automate linarith in inequalities -/ lemma max_cases (a b : α) : max a b = a ∧ b ≤ a ∨ max a b = b ∧ a < b := @min_cases αᵒᵈ _ a b lemma min_eq_iff : min a b = c ↔ a = c ∧ a ≤ b ∨ b = c ∧ b ≤ a := begin split, { intro h, refine or.imp (λ h', _) (λ h', _) (le_total a b); exact ⟨by simpa [h'] using h, h'⟩ }, { rintro (⟨rfl, h⟩|⟨rfl, h⟩); simp [h] } end lemma max_eq_iff : max a b = c ↔ a = c ∧ b ≤ a ∨ b = c ∧ a ≤ b := @min_eq_iff αᵒᵈ _ a b c lemma min_lt_min_left_iff : min a c < min b c ↔ a < b ∧ a < c := by { simp_rw [lt_min_iff, min_lt_iff, or_iff_left (lt_irrefl _)], exact and_congr_left (λ h, or_iff_left_of_imp h.trans) } lemma min_lt_min_right_iff : min a b < min a c ↔ b < c ∧ b < a := by simp_rw [min_comm a, min_lt_min_left_iff] lemma max_lt_max_left_iff : max a c < max b c ↔ a < b ∧ c < b := @min_lt_min_left_iff αᵒᵈ _ _ _ _ lemma max_lt_max_right_iff : max a b < max a c ↔ b < c ∧ a < c := @min_lt_min_right_iff αᵒᵈ _ _ _ _ /-- An instance asserting that `max a a = a` -/ instance max_idem : is_idempotent α max := by apply_instance -- short-circuit type class inference /-- An instance asserting that `min a a = a` -/ instance min_idem : is_idempotent α min := by apply_instance -- short-circuit type class inference lemma min_lt_max : min a b < max a b ↔ a ≠ b := inf_lt_sup lemma max_lt_max (h₁ : a < c) (h₂ : b < d) : max a b < max c d := by simp [lt_max_iff, max_lt_iff, *] lemma min_lt_min (h₁ : a < c) (h₂ : b < d) : min a b < min c d := @max_lt_max αᵒᵈ _ _ _ _ _ h₁ h₂ theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma monotone_on.map_max (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp only [max_eq_right, max_eq_left, hf ha hb, hf hb ha, h] lemma monotone_on.map_min (hf : monotone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (min a b) = min (f a) (f b) := hf.dual.map_max ha hb lemma antitone_on.map_max (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (max a b) = min (f a) (f b) := hf.dual_right.map_max ha hb lemma antitone_on.map_min (hf : antitone_on f s) (ha : a ∈ s) (hb : b ∈ s) : f (min a b) = max (f a) (f b) := hf.dual.map_max ha hb lemma monotone.map_max (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma monotone.map_min (hf : monotone f) : f (min a b) = min (f a) (f b) := hf.dual.map_max lemma antitone.map_max (hf : antitone f) : f (max a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma antitone.map_min (hf : antitone f) : f (min a b) = max (f a) (f b) := hf.dual.map_max theorem min_choice (a b : α) : min a b = a ∨ min a b = b := by cases le_total a b; simp * theorem max_choice (a b : α) : max a b = a ∨ max a b = b := @min_choice αᵒᵈ _ a b lemma le_of_max_le_left {a b c : α} (h : max a b ≤ c) : a ≤ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : α} (h : max a b ≤ c) : b ≤ c := le_trans (le_max_right _ _) h lemma max_commutative : commutative (max : α → α → α) := max_comm lemma max_associative : associative (max : α → α → α) := max_assoc lemma max_left_commutative : left_commutative (max : α → α → α) := max_left_comm lemma min_commutative : commutative (min : α → α → α) := min_comm lemma min_associative : associative (min : α → α → α) := min_assoc lemma min_left_commutative : left_commutative (min : α → α → α) := min_left_comm end
533172eb697c1b788526eeef77d737f9e06ad1a4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/monoidal/rigid/functor_category.lean
61e3a4d4a5767c2fdc4cc154cee2ecc3b045b2c2
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,970
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.monoidal.rigid.basic import category_theory.monoidal.functor_category /-! # Functors from a groupoid into a right/left rigid category form a right/left rigid category. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. (Using the pointwise monoidal structure on the functor category.) -/ noncomputable theory open category_theory open category_theory.monoidal_category namespace category_theory.monoidal variables {C D : Type*} [groupoid C] [category D] [monoidal_category D] instance functor_has_right_dual [right_rigid_category D] (F : C ⥤ D) : has_right_dual F := { right_dual := { obj := λ X, (F.obj X)ᘁ, map := λ X Y f, (F.map (inv f))ᘁ, map_comp' := λ X Y Z f g, by { simp [comp_right_adjoint_mate], }, }, exact := { evaluation := { app := λ X, ε_ _ _, naturality' := λ X Y f, begin dsimp, rw [category.comp_id, functor.map_inv, ←id_tensor_comp_tensor_id, category.assoc, right_adjoint_mate_comp_evaluation, ←category.assoc, ←id_tensor_comp, is_iso.hom_inv_id, tensor_id, category.id_comp], end }, coevaluation := { app := λ X, η_ _ _, naturality' := λ X Y f, begin dsimp, rw [functor.map_inv, category.id_comp, ←id_tensor_comp_tensor_id, ←category.assoc, coevaluation_comp_right_adjoint_mate, category.assoc, ←comp_tensor_id, is_iso.inv_hom_id, tensor_id, category.comp_id], end, }, }, } instance right_rigid_functor_category [right_rigid_category D] : right_rigid_category (C ⥤ D) := {} instance functor_has_left_dual [left_rigid_category D] (F : C ⥤ D) : has_left_dual F := { left_dual := { obj := λ X, ᘁ(F.obj X), map := λ X Y f, ᘁ(F.map (inv f)), map_comp' := λ X Y Z f g, by { simp [comp_left_adjoint_mate], }, }, exact := { evaluation := { app := λ X, ε_ _ _, naturality' := λ X Y f, begin dsimp, rw [category.comp_id, functor.map_inv, ←tensor_id_comp_id_tensor, category.assoc, left_adjoint_mate_comp_evaluation, ←category.assoc, ←comp_tensor_id, is_iso.hom_inv_id, tensor_id, category.id_comp], end }, coevaluation := { app := λ X, η_ _ _, naturality' := λ X Y f, begin dsimp, rw [functor.map_inv, category.id_comp, ←tensor_id_comp_id_tensor, ←category.assoc, coevaluation_comp_left_adjoint_mate, category.assoc, ←id_tensor_comp, is_iso.inv_hom_id, tensor_id, category.comp_id], end, }, }, } instance left_rigid_functor_category [left_rigid_category D] : left_rigid_category (C ⥤ D) := {} instance rigid_functor_category [rigid_category D] : rigid_category (C ⥤ D) := {} end category_theory.monoidal
6b4f7683c2435f52db87afee47dcd73fddc793e3
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/counterexamples/canonically_ordered_comm_semiring_two_mul.lean
3b9ad504389daef8db31540fc0d829cb66a191e3
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,493
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.zmod.basic import ring_theory.subsemiring import algebra.order.monoid /-! A `canonically_ordered_comm_semiring` with two different elements `a` and `b` such that `a ≠ b` and `2 * a = 2 * b`. Thus, multiplication by a fixed non-zero element of a canonically ordered semiring need not be injective. In particular, multiplying by a strictly positive element need not be strictly monotone. Recall that a `canonically_ordered_comm_semiring` is a commutative semiring with a partial ordering that is "canonical" in the sense that the inequality `a ≤ b` holds if and only if there is a `c` such that `a + c = b`. There are several compatibility conditions among addition/multiplication and the order relation. The point of the counterexample is to show that monotonicity of multiplication cannot be strengthened to **strict** monotonicity. Reference: https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/canonically_ordered.20pathology -/ namespace from_Bhavik /-- Bhavik Mehta's example. There are only the initial definitions, but no proofs. The Type `K` is a canonically ordered commutative semiring with the property that `2 * (1/2) ≤ 2 * 1`, even though it is not true that `1/2 ≤ 1`, since `1/2` and `1` are not comparable. -/ @[derive [comm_semiring]] def K : Type := subsemiring.closure ({1.5} : set ℚ) instance : has_coe K ℚ := ⟨λ x, x.1⟩ instance inhabited_K : inhabited K := ⟨0⟩ instance : preorder K := { le := λ x y, x = y ∨ (x : ℚ) + 1 ≤ (y : ℚ), le_refl := λ x, or.inl rfl, le_trans := λ x y z xy yz, begin rcases xy with (rfl | _), { apply yz }, rcases yz with (rfl | _), { right, apply xy }, right, exact xy.trans (le_trans ((le_add_iff_nonneg_right _).mpr zero_le_one) yz) end } end from_Bhavik lemma mem_zmod_2 (a : zmod 2) : a = 0 ∨ a = 1 := begin rcases a with ⟨_ | _ | _ | _ | a_val, _ | ⟨_, _ | ⟨_, ⟨⟩⟩⟩⟩, { exact or.inl rfl }, { exact or.inr rfl }, end lemma add_self_zmod_2 (a : zmod 2) : a + a = 0 := begin rcases mem_zmod_2 a with rfl | rfl; refl, end namespace Nxzmod_2 variables {a b : ℕ × zmod 2} /-- The preorder relation on `ℕ × ℤ/2ℤ` where we only compare the first coordinate, except that we leave incomparable each pair of elements with the same first component. For instance, `∀ α, β ∈ ℤ/2ℤ`, the inequality `(1,α) ≤ (2,β)` holds, whereas, `∀ n ∈ ℤ`, the elements `(n,0)` and `(n,1)` are incomparable. -/ instance preN2 : partial_order (ℕ × zmod 2) := { le := λ x y, x = y ∨ x.1 < y.1, le_refl := λ a, or.inl rfl, le_trans := λ x y z xy yz, begin rcases xy with (rfl | _), { exact yz }, { rcases yz with (rfl | _), { exact or.inr xy}, { exact or.inr (xy.trans yz) } } end, le_antisymm := begin intros a b ab ba, cases ab with ab ab, { exact ab }, { cases ba with ba ba, { exact ba.symm }, { exact (nat.lt_asymm ab ba).elim } } end } instance csrN2 : comm_semiring (ℕ × zmod 2) := by apply_instance instance csrN2_1 : add_cancel_comm_monoid (ℕ × zmod 2) := { add_left_cancel := λ a b c h, (add_right_inj a).mp h, ..Nxzmod_2.csrN2 } /-- A strict inequality forces the first components to be different. -/ @[simp] lemma lt_def : a < b ↔ a.1 < b.1 := begin refine ⟨λ h, _, λ h, _⟩, { rcases h with ⟨(rfl | a1), h1⟩, { exact ((not_or_distrib.mp h1).1).elim rfl }, { exact a1 } }, refine ⟨or.inr h, not_or_distrib.mpr ⟨λ k, _, not_lt.mpr h.le⟩⟩, rw k at h, exact nat.lt_asymm h h end lemma add_left_cancel : ∀ (a b c : ℕ × zmod 2), a + b = a + c → b = c := λ a b c h, (add_right_inj a).mp h lemma add_le_add_left : ∀ (a b : ℕ × zmod 2), a ≤ b → ∀ (c : ℕ × zmod 2), c + a ≤ c + b := begin rintros a b (rfl | ab) c, { refl }, { exact or.inr (by simpa) } end lemma le_of_add_le_add_left : ∀ (a b c : ℕ × zmod 2), a + b ≤ a + c → b ≤ c := begin rintros a b c (bc | bc), { exact le_of_eq ((add_right_inj a).mp bc) }, { exact or.inr (by simpa using bc) } end lemma zero_le_one : (0 : ℕ × zmod 2) ≤ 1 := dec_trivial lemma mul_lt_mul_of_pos_left : ∀ (a b c : ℕ × zmod 2), a < b → 0 < c → c * a < c * b := λ a b c ab c0, lt_def.mpr ((mul_lt_mul_left (lt_def.mp c0)).mpr (lt_def.mp ab)) lemma mul_lt_mul_of_pos_right : ∀ (a b c : ℕ × zmod 2), a < b → 0 < c → a * c < b * c := λ a b c ab c0, lt_def.mpr ((mul_lt_mul_right (lt_def.mp c0)).mpr (lt_def.mp ab)) instance ocsN2 : ordered_comm_semiring (ℕ × zmod 2) := { add_le_add_left := add_le_add_left, le_of_add_le_add_left := le_of_add_le_add_left, zero_le_one := zero_le_one, mul_lt_mul_of_pos_left := mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := mul_lt_mul_of_pos_right, ..Nxzmod_2.csrN2_1, ..(infer_instance : partial_order (ℕ × zmod 2)), ..(infer_instance : comm_semiring (ℕ × zmod 2)) } end Nxzmod_2 namespace ex_L open Nxzmod_2 subtype /-- Initially, `L` was defined as the subsemiring closure of `(1,0)`. -/ def L : Type := { l : (ℕ × zmod 2) // l ≠ (0, 1) } instance zero : has_zero L := ⟨⟨(0, 0), dec_trivial⟩⟩ instance one : has_one L := ⟨⟨(1, 1), dec_trivial⟩⟩ instance inhabited : inhabited L := ⟨1⟩ lemma add_L {a b : ℕ × zmod 2} (ha : a ≠ (0, 1)) (hb : b ≠ (0, 1)) : a + b ≠ (0, 1) := begin rcases a with ⟨a, a2⟩, rcases b with ⟨b, b2⟩, cases b, { rcases mem_zmod_2 b2 with rfl | rfl, { simp [ha] }, { simpa only } }, { simp [(a + b).succ_ne_zero] } end lemma mul_L {a b : ℕ × zmod 2} (ha : a ≠ (0, 1)) (hb : b ≠ (0, 1)) : a * b ≠ (0, 1) := begin rcases a with ⟨a, a2⟩, rcases b with ⟨b, b2⟩, cases b, { rcases mem_zmod_2 b2 with rfl | rfl; rcases mem_zmod_2 a2 with rfl | rfl; -- while this looks like a non-terminal `simp`, it (almost) isn't: there is only one goal where -- it does not finish the proof and on that goal it asks to prove `false` simp, exact hb rfl }, cases a, { rcases mem_zmod_2 b2 with rfl | rfl; rcases mem_zmod_2 a2 with rfl | rfl; -- while this looks like a non-terminal `simp`, it (almost) isn't: there is only one goal where -- it does not finish the proof and on that goal it asks to prove `false` simp, exact ha rfl }, { simp [mul_ne_zero _ _, nat.succ_ne_zero _] } end instance has_add_L : has_add L := { add := λ ⟨a, ha⟩ ⟨b, hb⟩, ⟨a + b, add_L ha hb⟩ } instance : has_mul L := { mul := λ ⟨a, ha⟩ ⟨b, hb⟩, ⟨a * b, mul_L ha hb⟩ } instance : ordered_comm_semiring L := begin refine function.injective.ordered_comm_semiring _ subtype.coe_injective rfl rfl _ _; { refine λ x y, _, cases x, cases y, refl } end lemma bot_le : ∀ (a : L), 0 ≤ a := begin rintros ⟨⟨an, a2⟩, ha⟩, cases an, { rcases mem_zmod_2 a2 with (rfl | rfl), { refl, }, { exact (ha rfl).elim } }, { refine or.inr _, exact nat.succ_pos _ } end instance order_bot : order_bot L := { bot := 0, bot_le := bot_le, ..(infer_instance : partial_order L) } lemma le_iff_exists_add : ∀ (a b : L), a ≤ b ↔ ∃ (c : L), b = a + c := begin rintros ⟨⟨an, a2⟩, ha⟩ ⟨⟨bn, b2⟩, hb⟩, rw subtype.mk_le_mk, refine ⟨λ h, _, λ h, _⟩, { rcases h with ⟨rfl, rfl⟩ | h, { exact ⟨(0 : L), (add_zero _).symm⟩ }, { refine ⟨⟨⟨bn - an, b2 + a2⟩, _⟩, _⟩, { rw [ne.def, prod.mk.inj_iff, not_and_distrib], exact or.inl (ne_of_gt (tsub_pos_of_lt h)) }, { congr, { exact (add_tsub_cancel_of_le h.le).symm }, { change b2 = a2 + (b2 + a2), rw [add_comm b2, ← add_assoc, add_self_zmod_2, zero_add] } } } }, { rcases h with ⟨⟨⟨c, c2⟩, hc⟩, abc⟩, injection abc with abc, rw [prod.mk_add_mk, prod.mk.inj_iff] at abc, rcases abc with ⟨rfl, rfl⟩, cases c, { refine or.inl _, rw [ne.def, prod.mk.inj_iff, eq_self_iff_true, true_and] at hc, rcases mem_zmod_2 c2 with rfl | rfl, { rw [add_zero, add_zero] }, { exact (hc rfl).elim } }, { refine or.inr _, exact (lt_add_iff_pos_right _).mpr c.succ_pos } } end lemma eq_zero_or_eq_zero_of_mul_eq_zero : ∀ (a b : L), a * b = 0 → a = 0 ∨ b = 0 := begin rintros ⟨⟨a, a2⟩, ha⟩ ⟨⟨b, b2⟩, hb⟩ ab1, injection ab1 with ab, injection ab with abn ab2, rw mul_eq_zero at abn, rcases abn with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩, { refine or.inl _, rcases mem_zmod_2 a2 with rfl | rfl, { refl }, { exact (ha rfl).elim } }, { refine or.inr _, rcases mem_zmod_2 b2 with rfl | rfl, { refl }, { exact (hb rfl).elim } } end instance can : canonically_ordered_comm_semiring L := { le_iff_exists_add := le_iff_exists_add, eq_zero_or_eq_zero_of_mul_eq_zero := eq_zero_or_eq_zero_of_mul_eq_zero, ..(infer_instance : order_bot L), ..(infer_instance : ordered_comm_semiring L) } /-- The elements `(1,0)` and `(1,1)` of `L` are different, but their doubles coincide. -/ example : ∃ a b : L, a ≠ b ∧ 2 * a = 2 * b := begin refine ⟨⟨(1,0), by simp⟩, 1, λ (h : (⟨(1, 0), _⟩ : L) = ⟨⟨1, 1⟩, _⟩), _, rfl⟩, obtain (F : (0 : zmod 2) = 1) := congr_arg (λ j : L, j.1.2) h, cases F, end end ex_L
2ed2d975b2f737494e1601df6f50ec0ae07e8167
618003631150032a5676f229d13a079ac875ff77
/src/data/dlist/basic.lean
5e9f21c9359066196bbcbd15937d9518d4cf2afc
[ "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
282
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.dlist def dlist.join {α : Type*} : list (dlist α) → dlist α | [] := dlist.empty | (x :: xs) := x ++ dlist.join xs
b77a1b5b61c17bb88cdccbdd0ebad05974a2d38d
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/polynomial.lean
c6edd80c6dc567ba4b0dc738befce1a91b189aa3
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
107,587
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker Theory of univariate polynomials, represented as `add_monoid_algebra R ℕ`, where `R` is a commutative semiring. -/ import data.monoid_algebra import algebra.gcd_domain import ring_theory.euclidean_domain import ring_theory.multiplicity import tactic.ring_exp import deprecated.field noncomputable theory local attribute [instance, priority 100] classical.prop_decidable local attribute [instance, priority 10] is_semiring_hom.comp is_ring_hom.comp /-- `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`. -/ def polynomial (R : Type*) [comm_semiring R] := add_monoid_algebra R ℕ open finsupp finset add_monoid_algebra namespace polynomial universes u v w x y z variables {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} section comm_semiring variables [comm_semiring R] {p q r : polynomial R} instance : inhabited (polynomial R) := finsupp.inhabited instance : comm_semiring (polynomial R) := add_monoid_algebra.comm_semiring instance : has_scalar R (polynomial R) := add_monoid_algebra.has_scalar instance : semimodule R (polynomial R) := add_monoid_algebra.semimodule /-- the coercion turning a `polynomial` into the function which reports the coefficient of a given monomial `X^n` -/ def coeff_coe_to_fun : has_coe_to_fun (polynomial R) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun @[simp] lemma support_zero : (0 : polynomial R).support = ∅ := rfl /-- `monomial s a` is the monomial `a * X^s` -/ @[reducible] def monomial (n : ℕ) (a : R) : polynomial R := finsupp.single n a /-- `C a` is the constant polynomial `a`. -/ def C (a : R) : polynomial R := monomial 0 a /-- `X` is the polynomial variable (aka indeterminant). -/ def X : polynomial R := monomial 1 1 /-- coeff p n is the coefficient of X^n in p -/ def coeff (p : polynomial R) := p.to_fun @[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial R) = f := rfl instance [has_repr R] : has_repr (polynomial R) := ⟨λ p, if p = 0 then "0" else (p.support.sort (≤)).foldr (λ n a, a ++ (if a = "" then "" else " + ") ++ if n = 0 then "C (" ++ repr (coeff p n) ++ ")" else if n = 1 then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X" else if (coeff p n) = 1 then "X ^ " ++ repr n else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩ theorem ext_iff {p q : polynomial R} : p = q ↔ ∀ n, coeff p n = coeff q n := ⟨λ h n, h ▸ rfl, finsupp.ext⟩ @[ext] lemma ext {p q : polynomial R} : (∀ n, coeff p n = coeff q n) → p = q := (@ext_iff _ _ p q).2 /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : polynomial R) : with_bot ℕ := p.support.sup some lemma degree_lt_wf : well_founded (λp q : polynomial R, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial R) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to ℕ, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial R) : ℕ := (degree p).get_or_else 0 lemma single_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n | 0 := (mul_one _).symm | (n+1) := calc monomial (n + 1) a = monomial n a * X : by rw [X, single_mul_single, mul_one] ... = (C a * X^n) * X : by rw [single_eq_C_mul_X] ... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one] lemma sum_C_mul_X_eq (p : polynomial R) : p.sum (λn a, C a * X^n) = p := eq.trans (sum_congr rfl $ assume n hn, single_eq_C_mul_X.symm) (finsupp.sum_single _) @[elab_as_eliminator] protected lemma induction_on {M : polynomial R → Prop} (p : polynomial R) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_monomial : ∀(n : ℕ) (a : R), M (C a * X^n) → M (C a * X^(n+1))) : M p := have ∀{n:ℕ} {a}, M (C a * X^n), begin assume n a, induction n with n ih, { simp only [pow_zero, mul_one, h_C] }, { exact h_monomial _ _ ih } end, finsupp.induction p (suffices M (C 0), by { convert this, exact single_zero.symm, }, h_C 0) (assume n a p _ _ hp, suffices M (C a * X^n + p), by { convert this, exact single_eq_C_mul_X }, h_add _ _ this hp) @[simp] lemma C_0 : C (0 : R) = 0 := single_zero @[simp] lemma C_1 : C (1 : R) = 1 := rfl @[simp] lemma C_mul : C (a * b) = C a * C b := (@single_mul_single _ _ _ _ 0 0 a b).symm @[simp] lemma C_add : C (a + b) = C a + C b := finsupp.single_add instance C.is_semiring_hom : is_semiring_hom (C : R → polynomial R) := ⟨C_0, C_1, λ _ _, C_add, λ _ _, C_mul⟩ @[simp] lemma C_pow : C (a ^ n) = C a ^ n := is_semiring_hom.map_pow _ _ _ lemma nat_cast_eq_C (n : ℕ) : (n : polynomial R) = C n := ((ring_hom.of C).map_nat_cast n).symm section coeff lemma apply_eq_coeff : p n = coeff p n := rfl @[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial R) n = 0 := rfl lemma coeff_single : coeff (single n a) m = if n = m then a else 0 := by { dsimp [single, finsupp.single], congr } @[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 := coeff_single @[simp] lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl instance coeff.is_add_monoid_hom {n : ℕ} : is_add_monoid_hom (λ p : polynomial R, p.coeff n) := { map_add := λ p q, coeff_add p q n, map_zero := coeff_zero _ } lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 := by simp [coeff, eq_comm, C, monomial, single]; congr @[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_single @[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_single @[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_single lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_single lemma coeff_C_mul_X (x : R) (k n : ℕ) : coeff (C x * X^k : polynomial R) n = if n = k then x else 0 := by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr lemma coeff_sum [comm_semiring S] (n : ℕ) (f : ℕ → R → polynomial S) : coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) := finsupp.sum_apply @[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n := begin conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] }, rw [mul_def, C, sum_single_index], { simp [coeff_single, finsupp.mul_sum, coeff_sum], apply sum_congr rfl, assume i hi, by_cases i = n; simp [h] }, { simp [finsupp.sum] } end @[simp] lemma coeff_smul (p : polynomial R) (r : R) (n : ℕ) : coeff (r • p) n = r * coeff p n := finsupp.smul_apply lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f := ext $ λ n, coeff_C_mul f @[simp, priority 990] lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 := coeff_single @[simp] lemma coeff_X_pow (k n : ℕ) : coeff (X^k : polynomial R) n = if n = k then 1 else 0 := by simpa only [C_1, one_mul] using coeff_C_mul_X (1:R) k n lemma coeff_mul (p q : polynomial R) (n : ℕ) : coeff (p * q) n = (nat.antidiagonal n).sum (λ x, coeff p x.1 * coeff q x.2) := have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 ≠ 0 → a.1 + a.2 = n, from λ a ha, by_contradiction (λ h, absurd (eq.refl (0 : R)) (by rwa if_neg h at ha)), calc coeff (p * q) n = p.support.sum (λ a, q.support.sum (λ b, ite (a + b = n) (coeff p a * coeff q b) 0)) : by simp only [mul_def, coeff_sum, coeff_single]; refl ... = (p.support.product q.support).sum (λ v : ℕ × ℕ, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0) : by rw sum_product ... = (nat.antidiagonal n).sum (λ x, coeff p x.1 * coeff q x.2) : begin refine sum_bij_ne_zero (λ x _ _, x) (λ x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (λ _ _ _ _ _ _ h, h) (λ x h₁ h₂, ⟨x, _, _, rfl⟩) _, { rw [mem_product, mem_support_iff, mem_support_iff], exact ⟨ne_zero_of_mul_ne_zero_right h₂, ne_zero_of_mul_ne_zero_left h₂⟩ }, { rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] }, { intros x h hx, rw [if_pos (hite x hx)] } end theorem coeff_mul_X_pow (p : polynomial R) (n d : ℕ) : coeff (p * polynomial.X ^ n) (d + n) = coeff p d := begin rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim } end theorem coeff_mul_X (p : polynomial R) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n theorem mul_X_pow_eq_zero {p : polynomial R} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n) end coeff lemma C_inj : C a = C b ↔ a = b := ⟨λ h, coeff_C_zero.symm.trans (h.symm ▸ coeff_C_zero), congr_arg C⟩ section eval₂ variables [semiring S] variables (f : R → S) (x : S) open is_semiring_hom /-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring to the target and a value `x` for the variable in the target -/ def eval₂ (p : polynomial R) : S := p.sum (λ e a, f a * x ^ e) variables [is_semiring_hom f] @[simp] lemma eval₂_C : (C a).eval₂ f x = f a := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [pow_zero, mul_one] @[simp] lemma eval₂_X : X.eval₂ f x = x := (sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [map_one f, one_mul, pow_one] @[simp] lemma eval₂_zero : (0 : polynomial R).eval₂ f x = 0 := finsupp.sum_zero_index @[simp] lemma eval₂_add : (p + q).eval₂ f x = p.eval₂ f x + q.eval₂ f x := finsupp.sum_add_index (λ _, by rw [map_zero f, zero_mul]) (λ _ _ _, by rw [map_add f, add_mul]) @[simp] lemma eval₂_one : (1 : polynomial R).eval₂ f x = 1 := by rw [← C_1, eval₂_C, map_one f] instance eval₂.is_add_monoid_hom : is_add_monoid_hom (eval₂ f x) := { map_zero := eval₂_zero _ _, map_add := λ _ _, eval₂_add _ _ } end eval₂ section eval₂ variables [comm_semiring S] variables (f : R → S) [is_semiring_hom f] (x : S) open is_semiring_hom @[simp] lemma eval₂_mul : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := begin dunfold eval₂, rw [mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index], { apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index], { apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, map_mul f, pow_add], { simp only [mul_assoc, mul_left_comm] }, { rw [map_zero f, zero_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } }, { intro, rw [map_zero f, zero_mul] }, { intros, rw [map_add f, add_mul] } end instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f x) := ⟨eval₂_zero _ _, eval₂_one _ _, λ _ _, eval₂_add _ _, λ _ _, eval₂_mul _ _⟩ /-- `eval₂` as a `ring_hom` -/ def eval₂_ring_hom (f : R →+* S) (x) : polynomial R →+* S := ring_hom.of (eval₂ f x) @[simp] lemma coe_eval₂_ring_hom (f : R →+* S) (x) : ⇑(eval₂_ring_hom f x) = eval₂ f x := rfl lemma eval₂_pow (n : ℕ) : (p ^ n).eval₂ f x = p.eval₂ f x ^ n := map_pow _ _ _ lemma eval₂_sum (p : polynomial R) (g : ℕ → R → polynomial R) (x : S) : (p.sum g).eval₂ f x = p.sum (λ n a, (g n a).eval₂ f x) := finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f]) (by intros; simp [right_distrib, is_add_monoid_hom.map_add f]) end eval₂ section eval variable {x : R} /-- `eval x p` is the evaluation of the polynomial `p` at `x` -/ def eval : R → polynomial R → R := eval₂ id @[simp] lemma eval_C : (C a).eval x = a := eval₂_C _ _ @[simp] lemma eval_X : X.eval x = x := eval₂_X _ _ @[simp] lemma eval_zero : (0 : polynomial R).eval x = 0 := eval₂_zero _ _ @[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := eval₂_add _ _ @[simp] lemma eval_one : (1 : polynomial R).eval x = 1 := eval₂_one _ _ @[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := eval₂_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval x) := eval₂.is_semiring_hom _ _ @[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval x = p.eval x ^ n := eval₂_pow _ _ _ lemma eval_sum (p : polynomial R) (f : ℕ → R → polynomial R) (x : R) : (p.sum f).eval x = p.sum (λ n a, (f n a).eval x) := eval₂_sum _ _ _ _ lemma eval₂_hom [comm_semiring S] (f : R → S) [is_semiring_hom f] (x : R) : p.eval₂ f (f x) = f (p.eval x) := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, eval_pow, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) /-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/ def is_root (p : polynomial R) (a : R) : Prop := p.eval a = 0 instance [decidable_eq R] : decidable (is_root p a) := by unfold is_root; apply_instance @[simp] lemma is_root.def : is_root p a ↔ p.eval a = 0 := iff.rfl lemma root_mul_left_of_is_root (p : polynomial R) {q : polynomial R} : is_root q a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero] lemma root_mul_right_of_is_root {p : polynomial R} (q : polynomial R) : is_root p a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul] lemma coeff_zero_eq_eval_zero (p : polynomial R) : coeff p 0 = p.eval 0 := calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp ... = p.eval 0 : eq.symm $ finset.sum_eq_single _ (λ b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp) lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial R} (hp : p.coeff 0 = 0) : is_root p 0 := by rwa coeff_zero_eq_eval_zero at hp end eval section comp def comp (p q : polynomial R) : polynomial R := p.eval₂ C q lemma eval₂_comp [comm_semiring S] (f : R → S) [is_semiring_hom f] {x : S} : (p.comp q).eval₂ f x = p.eval₂ f (q.eval₂ f x) := show (p.sum (λ e a, C a * q ^ e)).eval₂ f x = p.eval₂ f (eval₂ f x q), by simp only [eval₂_mul, eval₂_C, eval₂_pow, eval₂_sum]; refl lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := eval₂_comp _ @[simp] lemma comp_X : p.comp X = p := begin refine ext (λ n, _), rw [comp, eval₂], conv in (C _ * _) { rw ← single_eq_C_mul_X }, rw finsupp.sum_single end @[simp] lemma X_comp : X.comp p = p := eval₂_X _ _ @[simp] lemma comp_C : p.comp (C a) = C (p.eval a) := begin dsimp [comp, eval₂, eval, finsupp.sum], rw [← p.support.sum_hom (@C R _)], apply finset.sum_congr rfl; simp end @[simp] lemma C_comp : (C a).comp p = C a := eval₂_C _ _ @[simp] lemma comp_zero : p.comp (0 : polynomial R) = C (p.eval 0) := by rw [← C_0, comp_C] @[simp] lemma zero_comp : comp (0 : polynomial R) p = 0 := by rw [← C_0, C_comp] @[simp] lemma comp_one : p.comp 1 = C (p.eval 1) := by rw [← C_1, comp_C] @[simp] lemma one_comp : comp (1 : polynomial R) p = 1 := by rw [← C_1, C_comp] instance : is_semiring_hom (λ q : polynomial R, q.comp p) := by unfold comp; apply_instance @[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := eval₂_add _ _ @[simp] lemma mul_comp : (p * q).comp r = p.comp r * q.comp r := eval₂_mul _ _ end comp /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial R) := leading_coeff p = (1 : R) lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq R] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) : leading_coeff p = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial R) = ⊥ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl @[simp] lemma degree_C (ha : a ≠ 0) : degree (C a) = (0 : with_bot ℕ) := show sup (ite (a = 0) ∅ {0}) some = 0, by rw if_neg ha; refl lemma degree_C_le : degree (C a) ≤ (0 : with_bot ℕ) := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial R) ≤ (0 : with_bot ℕ) := by rw [← C_1]; exact degree_C_le lemma degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), λ h, h.symm ▸ rfl⟩ lemma degree_eq_nat_degree (hp : p ≠ 0) : degree p = (nat_degree p : with_bot ℕ) := let ⟨n, hn⟩ := classical.not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : ℕ} (hn : n > 0) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : ℕ} (h : degree p = n) : nat_degree p = n := have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot ℕ) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≤ nat_degree p := begin by_cases hp : p = 0, { rw hp, exact bot_le }, rw [degree_eq_nat_degree hp], exact le_refl _ end lemma nat_degree_eq_of_degree_eq [comm_semiring S] {q : polynomial S} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : with_bot ℕ) ≤ degree p := show @has_le.le (with_bot ℕ) _ (some n : with_bot ℕ) (p.support.sup some : with_bot ℕ), from finset.le_sup (finsupp.mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma degree_le_degree (h : coeff q (nat_degree p) ≠ 0) : degree p ≤ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end @[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : ℕ) : nat_degree (n : polynomial R) = 0 := by simp [nat_cast_eq_C] @[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [← single_eq_C_mul_X, degree, support_single_ne_zero ha]; refl lemma degree_monomial_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := if h : a = 0 then by rw [h, C_0, zero_mul]; exact bot_le else le_of_eq (degree_monomial n h) lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : ℕ} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end -- TODO find a home (this file) @[simp] lemma finset_sum_coeff (s : finset ι) (f : ι → polynomial R) (n : ℕ) : coeff (s.sum f) n = s.sum (λ b, coeff (f b) n) := (s.sum_hom (λ q : polynomial R, q.coeff n)).symm -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial R) (n : ℕ) (I : decidable (n < 1 + nat_degree p)) : @ite (n < 1 + nat_degree p) I _ (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (λ w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum (p : polynomial R) : p = (range (p.nat_degree + 1)).sum (λ i, C (p.coeff i) * X^i) := begin ext n, simp only [add_comm, coeff_X_pow, coeff_C_mul, finset.mem_range, finset.sum_mul_boole, finset_sum_coeff, ite_le_nat_degree_coeff], end lemma monic.as_sum {p : polynomial R} (hp : p.monic) : p = X^(p.nat_degree) + ((finset.range p.nat_degree).sum $ λ i, C (p.coeff i) * X^i) := begin conv_lhs { rw [p.as_sum, finset.sum_range_succ] }, suffices : C (p.coeff p.nat_degree) = 1, { rw [this, one_mul] }, exact congr_arg C hp end section map variables [comm_semiring S] variables (f : R →+* S) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : polynomial R → polynomial S := eval₂ (C ∘ f) X instance is_semiring_hom_C_f : is_semiring_hom (C ∘ f) := is_semiring_hom.comp _ _ @[simp] lemma map_C : (C a).map f = C (f a) := eval₂_C _ _ @[simp] lemma map_X : X.map f = X := eval₂_X _ _ @[simp] lemma map_zero : (0 : polynomial R).map f = 0 := eval₂_zero _ _ @[simp] lemma map_add : (p + q).map f = p.map f + q.map f := eval₂_add _ _ @[simp] lemma map_one : (1 : polynomial R).map f = 1 := eval₂_one _ _ @[simp] lemma map_mul : (p * q).map f = p.map f * q.map f := eval₂_mul _ _ instance map.is_semiring_hom : is_semiring_hom (map f) := eval₂.is_semiring_hom _ _ @[simp] lemma map_pow (n : ℕ) : (p ^ n).map f = p.map f ^ n := eval₂_pow _ _ _ lemma coeff_map (n : ℕ) : coeff (p.map f) n = f (coeff p n) := begin rw [map, eval₂, coeff_sum], conv_rhs { rw [← sum_C_mul_X_eq p, coeff_sum, finsupp.sum, ← p.support.sum_hom f], }, refine finset.sum_congr rfl (λ x hx, _), simp [function.comp, coeff_C_mul_X, is_semiring_hom.map_mul f], split_ifs; simp [is_semiring_hom.map_zero f], end lemma map_map [comm_semiring T] (g : S →+* T) (p : polynomial R) : (p.map f).map g = p.map (g.comp f) := ext (by simp [coeff_map]) lemma eval₂_map [comm_semiring T] (g : S → T) [is_semiring_hom g] (x : T) : (p.map f).eval₂ g x = p.eval₂ (λ y, g (f y)) x := polynomial.induction_on p (by simp) (by simp [is_semiring_hom.map_add f] {contextual := tt}) (by simp [is_semiring_hom.map_mul f, is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt}) lemma eval_map (x : S) : (p.map f).eval x = p.eval₂ f x := eval₂_map _ _ _ @[simp] lemma map_id : p.map (ring_hom.id _) = p := by simp [polynomial.ext_iff, coeff_map] lemma mem_map_range {p : polynomial S} : p ∈ set.range (map f) ↔ ∀ n, p.coeff n ∈ (set.range f) := begin split, { rintro ⟨p, rfl⟩ n, rw coeff_map, exact set.mem_range_self _ }, { intro h, rw p.as_sum, apply is_add_submonoid.finset_sum_mem, intros i hi, rcases h i with ⟨c, hc⟩, use [C c * X^i], rw [map_mul, map_C, hc, map_pow, map_X] } end end map section variables [comm_semiring S] [comm_semiring T] variables (f : R →+* S) (g : S →+* T) (p) lemma hom_eval₂ (x : S) : g (p.eval₂ f x) = p.eval₂ (g ∘ f) (g x) := begin apply polynomial.induction_on p; clear p, { intros a, rw [eval₂_C, eval₂_C] }, { intros p q hp hq, simp only [hp, hq, eval₂_add, is_semiring_hom.map_add g] }, { intros n a ih, replace ih := congr_arg (λ y, y * g x) ih, simpa [pow_succ', is_semiring_hom.map_mul g, (mul_assoc _ _ _).symm, eval₂_C, eval₂_mul, eval₂_X] using ih } end end lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := begin refine ext (λ n, _), cases n, { simp }, { have : degree p < ↑(nat.succ n) := lt_of_le_of_lt h (with_bot.some_lt_some.2 (nat.succ_pos _)), rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt this] } end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h ▸ le_refl _) lemma degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, λ h, h.symm ▸ degree_C_le⟩ lemma degree_add_le (p q : polynomial R) : degree (p + q) ≤ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≤ (p.support ∪ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some ⊔ q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), λ h, h.symm ▸ leading_coeff_zero⟩ lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = ⊥ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma degree_add_eq_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := le_antisymm (max_eq_right_of_lt h ▸ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, zero_add], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [add_comm, degree_add_eq_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial R) (n : ℕ) : degree (p.erase n) ≤ degree p := by convert sup_mono (erase_subset _ _) lemma degree_erase_lt (hp : p ≠ 0) : degree (p.erase (nat_degree p)) < degree p := lt_of_le_of_ne (degree_erase_le _ _) $ (degree_eq_nat_degree hp).symm ▸ (by convert λ h, not_mem_erase _ _ (mem_of_max h)) lemma degree_sum_le (s : finset ι) (f : ι → polynomial R) : degree (s.sum f) ≤ s.sup (λ b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree ((insert a s).sum f) ≤ max (degree (f a)) (degree (s.sum f)) : by rw sum_insert has; exact degree_add_le _ _ ... ≤ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial R) : degree (p * q) ≤ degree p + degree q := calc degree (p * q) ≤ (p.support).sup (λi, degree (sum q (λj a, C (coeff p i * a) * X ^ (i + j)))) : by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _ ... ≤ p.support.sup (λi, q.support.sup (λj, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≤ degree p + degree q : begin refine finset.sup_le (λ a ha, finset.sup_le (λ b hb, le_trans (degree_monomial_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add' (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial R) : ∀ n, degree (p ^ n) ≤ add_monoid.smul n (degree p) | 0 := by rw [pow_zero, add_monoid.zero_smul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≤ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≤ _ : by rw succ_smul; exact add_le_add' (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : R) (n : ℕ) : leading_coeff (C a * X ^ n) = a := begin by_cases ha : a = 0, { simp only [ha, C_0, zero_mul, leading_coeff_zero] }, { rw [leading_coeff, nat_degree, degree_monomial _ ha, ← single_eq_C_mul_X], exact @finsupp.single_eq_same _ _ _ n a } end @[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a := suffices leading_coeff (C a * X^0) = a, by rwa [pow_zero, mul_one] at this, leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 := suffices leading_coeff (C (1:R) * X^1) = 1, by rwa [C_1, pow_one, one_mul] at this, leading_coeff_monomial 1 1 @[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 := suffices leading_coeff (C (1:R) * X^0) = 1, by rwa [C_1, pow_zero, mul_one] at this, leading_coeff_monomial 1 0 @[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _ lemma monic.ne_zero_of_zero_ne_one (h : (0:R) ≠ 1) {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { contrapose! h, rwa [h] at hp } lemma monic.ne_zero {R : Type*} [nonzero_comm_ring R] {p : polynomial R} (hp : p.monic) : p ≠ 0 := hp.ne_zero_of_zero_ne_one $ zero_ne_one lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q ≠ 0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = (nat.antidiagonal (nat_degree p + nat_degree q)).sum (λ x, coeff p x.1 * coeff q x.2) : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ h₂, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact h₂ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul_eq' (h : leading_coeff p * leading_coeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt _ h; exact λ hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q ≠ 0 := by refine mt _ h; exact λ hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma nat_degree_mul_eq' (h : leading_coeff p * leading_coeff q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, zero_mul]), have hq : q ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, mul_zero]), have hpq : p * q ≠ 0 := λ hpq, by rw [← coeff_mul_degree_add_degree, hpq, coeff_zero] at h; exact h rfl, option.some_inj.1 (show (nat_degree (p * q) : with_bot ℕ) = nat_degree p + nat_degree q, by rw [← degree_eq_nat_degree hpq, degree_mul_eq' h, degree_eq_nat_degree hp, degree_eq_nat_degree hq]) lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul_eq' h, coeff_mul_degree_add_degree], refl end lemma leading_coeff_pow' : leading_coeff p ^ n ≠ 0 → leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ λ n ih h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' h₂, ih h₁] lemma degree_pow_eq' : ∀ {n}, leading_coeff p ^ n ≠ 0 → degree (p ^ n) = add_monoid.smul n (degree p) | 0 := λ h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, add_monoid.zero_smul] | (n+1) := λ h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul_eq' h₂, succ_smul, degree_pow_eq' h₁] lemma nat_degree_pow_eq' {n : ℕ} (h : leading_coeff p ^ n ≠ 0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n ≠ 0, from λ hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot ℕ) = (n * nat_degree p : ℕ), by rw [← degree_eq_nat_degree hpn, degree_pow_eq' h, degree_eq_nat_degree hp0, ← with_bot.coe_smul]; simp @[simp] lemma leading_coeff_X_pow : ∀ n : ℕ, leading_coeff ((X : polynomial R) ^ n) = 1 | 0 := by simp | (n+1) := if h10 : (1 : R) = 0 then by rw [pow_succ, ← one_mul X, ← C_1, h10]; simp else have h : leading_coeff (X : polynomial R) * leading_coeff (X ^ n) ≠ 0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact h10, by rw [pow_succ, leading_coeff_mul' h, leading_coeff_X, leading_coeff_X_pow, one_mul] lemma nat_degree_comp_le : nat_degree (p.comp q) ≤ nat_degree p * nat_degree q := if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _ else with_bot.coe_le_coe.1 $ calc ↑(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm ... ≤ _ : degree_sum_le _ _ ... ≤ _ : sup_le (λ n hn, calc degree (C (coeff p n) * q ^ n) ≤ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _ ... ≤ nat_degree (C (coeff p n)) + add_monoid.smul n (degree q) : add_le_add' degree_le_nat_degree (degree_pow_le _ _) ... ≤ nat_degree (C (coeff p n)) + add_monoid.smul n (nat_degree q) : add_le_add_left' (add_monoid.smul_le_smul_of_le_right (@degree_le_nat_degree _ _ q) n) ... = (n * nat_degree q : ℕ) : by rw [nat_degree_C, with_bot.coe_zero, zero_add, ← with_bot.coe_smul, add_monoid.smul_eq_mul]; simp ... ≤ (nat_degree p * nat_degree q : ℕ) : with_bot.coe_le_coe.2 $ mul_le_mul_of_nonneg_right (le_nat_degree_of_ne_zero (finsupp.mem_support_iff.1 hn)) (nat.zero_le _)) lemma degree_map_le [comm_semiring S] (f : R →+* S) : degree (p.map f) ≤ degree p := if h : p.map f = 0 then by simp [h] else begin rw [degree_eq_nat_degree h], refine le_degree_of_ne_zero (mt (congr_arg f) _), rw [← coeff_map f, is_semiring_hom.map_zero f], exact mt leading_coeff_eq_zero.1 h end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) : (∀ p q : polynomial R, p = q) ∧ (∀ a b : R, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], λ a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma degree_map_eq_of_leading_coeff_ne_zero [comm_semiring S] (f : R →+* S) (hf : f (leading_coeff p) ≠ 0) : degree (p.map f) = degree p := le_antisymm (degree_map_le f) $ have hp0 : p ≠ 0, from λ hp0, by simpa [hp0, is_semiring_hom.map_zero f] using hf, begin rw [degree_eq_nat_degree hp0], refine le_degree_of_ne_zero _, rw [coeff_map], exact hf end lemma monic_map [comm_semiring S] (f : R →+* S) (hp : monic p) : monic (p.map f) := if h : (0 : S) = 1 then by haveI := subsingleton_of_zero_eq_one S h; exact subsingleton.elim _ _ else have f (leading_coeff p) ≠ 0, by rwa [show _ = _, from hp, is_semiring_hom.map_one f, ne.def, eq_comm], by erw [monic, leading_coeff, nat_degree_eq_of_degree_eq (degree_map_eq_of_leading_coeff_ne_zero f this), coeff_map, ← leading_coeff, show _ = _, from hp, is_semiring_hom.map_one f] lemma zero_le_degree_iff {p : polynomial R} : 0 ≤ degree p ↔ p ≠ 0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial @[simp] lemma coeff_mul_X_zero (p : polynomial R) : coeff (p * X) 0 = 0 := by rw [coeff_mul, nat.antidiagonal_zero]; simp only [polynomial.coeff_X_zero, finset.sum_singleton, mul_zero] lemma is_unit_C {x : R} : is_unit (C x) ↔ is_unit x := begin rw [is_unit_iff_dvd_one, is_unit_iff_dvd_one], split, { rintros ⟨g, hg⟩, replace hg := congr_arg (eval 0) hg, rw [eval_one, eval_mul, eval_C] at hg, exact ⟨g.eval 0, hg⟩ }, { rintros ⟨y, hy⟩, exact ⟨C y, by rw [← C_mul, ← hy, C_1]⟩ } end end comm_semiring instance subsingleton [subsingleton R] [comm_semiring R] : subsingleton (polynomial R) := ⟨λ _ _, ext (λ _, subsingleton.elim _ _)⟩ section comm_semiring variables [comm_semiring R] {p q r : polynomial R} lemma ne_zero_of_monic_of_zero_ne_one (hp : monic p) (h : (0 : R) ≠ 1) : p ≠ 0 := mt (congr_arg leading_coeff) $ by rw [monic.def.1 hp, leading_coeff_zero]; cc lemma eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (λ n, nat.cases_on n (by simp) (λ n, nat.cases_on n (by simp [coeff_C]) (λ m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm ℕ 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≤ 1, from h ▸ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) theorem degree_C_mul_X_pow_le (r : R) (n : ℕ) : degree (C r * X^n) ≤ n := begin rw [← single_eq_C_mul_X], refine finset.sup_le (λ b hb, _), rw list.eq_of_mem_singleton (finsupp.support_single_subset hb), exact le_refl _ end theorem degree_X_pow_le (n : ℕ) : degree (X^n : polynomial R) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le (1:R) n theorem degree_X_le : degree (X : polynomial R) ≤ 1 := by simpa only [C_1, one_mul, pow_one] using degree_C_mul_X_pow_le (1:R) 1 section injective open function variables [comm_semiring S] {f : R →+* S} (hf : function.injective f) include hf lemma degree_map_eq_of_injective (p : polynomial R) : degree (p.map f) = degree p := if h : p = 0 then by simp [h] else degree_map_eq_of_leading_coeff_ne_zero _ (by rw [← is_semiring_hom.map_zero f]; exact mt hf.eq_iff.1 (mt leading_coeff_eq_zero.1 h)) lemma degree_map' (p : polynomial R) : degree (p.map f) = degree p := p.degree_map_eq_of_injective hf lemma nat_degree_map' (p : polynomial R) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map' hf p) lemma map_injective : injective (map f) := λ p q h, ext $ λ m, hf $ begin rw ext_iff at h, specialize h m, rw [coeff_map f, coeff_map f] at h, exact h end lemma leading_coeff_of_injective (p : polynomial R) : leading_coeff (p.map f) = f (leading_coeff p) := begin delta leading_coeff, rw [coeff_map f, nat_degree_map' hf p] end lemma monic_of_injective {p : polynomial R} (hp : (p.map f).monic) : p.monic := begin apply hf, rw [← leading_coeff_of_injective hf, hp.leading_coeff, is_semiring_hom.map_one f] end end injective theorem monic_of_degree_le (n : ℕ) (H1 : degree p ≤ n) (H2 : coeff p n = 1) : monic p := decidable.by_cases (assume H : degree p < n, @subsingleton.elim _ (subsingleton_of_zero_eq_one R $ H2 ▸ (coeff_eq_zero_of_degree_lt H).symm) _ _) (assume H : ¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H]) theorem monic_X_pow_add {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) + p) := have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)), monic_of_degree_le (n+1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) theorem monic_X_add_C (x : R) : monic (X + C x) := pow_one (X : polynomial R) ▸ monic_X_pow_add degree_C_le theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≤ n) m (Hm : n < (m : with_bot ℕ)), decidable.of_not_not $ λ H4, have H1 : m ∉ f.support, from λ H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot ℕ) ≤ n)) Hm, H1 $ (finsupp.mem_support_to_fun f m).2 H4, λ H, finset.sup_le $ λ b Hb, decidable.of_not_not $ λ Hn, (finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem nat_degree_le_of_degree_le {p : polynomial R} {n : ℕ} (H : degree p ≤ n) : nat_degree p ≤ n := show option.get_or_else (degree p) 0 ≤ n, from match degree p, H with | none, H := zero_le _ | (some d), H := with_bot.coe_le_coe.1 H end theorem leading_coeff_mul_X_pow {p : polynomial R} {n : ℕ} : leading_coeff (p * X ^ n) = leading_coeff p := decidable.by_cases (assume H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (assume H : leading_coeff p ≠ 0, by rw [leading_coeff_mul', leading_coeff_X_pow, mul_one]; rwa [leading_coeff_X_pow, mul_one]) lemma monic_mul (hp : monic p) (hq : monic q) : monic (p * q) := if h0 : (0 : R) = 1 then by haveI := subsingleton_of_zero_eq_one _ h0; exact subsingleton.elim _ _ else have leading_coeff p * leading_coeff q ≠ 0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0], by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul] lemma monic_pow (hp : monic p) : ∀ (n : ℕ), monic (p ^ n) | 0 := monic_one | (n+1) := monic_mul hp (monic_pow n) lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot ℕ) < degree p) (hmp : monic p) (hq : q ≠ 0) : multiplicity.finite p q := have zn0 : (0 : R) ≠ 1, from λ h, by haveI := subsingleton_of_zero_eq_one _ h; exact hq (subsingleton.elim _ _), ⟨nat_degree q, λ ⟨r, hr⟩, have hp0 : p ≠ 0, from λ hp0, by simp [hp0] at hp; contradiction, have hr0 : r ≠ 0, from λ hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) ≠ 0, from hpn1.symm ▸ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r ≠ 0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hpn0 : p ^ (nat_degree q + 1) ≠ 0, from mt leading_coeff_eq_zero.2 $ by rw [leading_coeff_pow' hpn0', show _ = _, from hmp, one_pow]; exact zn0.symm, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul_eq' hpnr0, nat_degree_pow_eq' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right' (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section nonzero_comm_semiring variables [nonzero_comm_semiring R] {p q : polynomial R} instance : nonzero_comm_semiring (polynomial R) := { zero_ne_one := λ (h : (0 : polynomial R) = 1), @zero_ne_one R _ $ calc (0 : R) = eval 0 0 : eval_zero.symm ... = eval 0 1 : congr_arg _ h ... = 1 : eval_C, ..polynomial.comm_semiring } @[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot ℕ) := degree_C (show (1 : R) ≠ 0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial R) = 1 := begin unfold X degree monomial single finsupp.support, rw if_neg (zero_ne_one).symm, refl end lemma X_ne_zero : (X : polynomial R) ≠ 0 := mt (congr_arg (λ p, coeff p 1)) (by simp) @[simp] lemma degree_X_pow : ∀ (n : ℕ), degree ((X : polynomial R) ^ n) = n | 0 := by simp only [pow_zero, degree_one]; refl | (n+1) := have h : leading_coeff (X : polynomial R) * leading_coeff (X ^ n) ≠ 0, by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul]; exact zero_ne_one.symm, by rw [pow_succ, degree_mul_eq' h, degree_X, degree_X_pow, add_comm]; refl @[simp] lemma not_monic_zero : ¬monic (0 : polynomial R) := by simpa only [monic, leading_coeff_zero] using zero_ne_one lemma ne_zero_of_monic (h : monic p) : p ≠ 0 := λ h₁, @not_monic_zero R _ (h₁ ▸ h) end nonzero_comm_semiring section comm_semiring variables [comm_semiring R] {p q : polynomial R} /-- `dix_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`. It can be used in a semiring where the usual division algorithm is not possible -/ def div_X (p : polynomial R) : polynomial R := { to_fun := λ n, p.coeff (n + 1), support := ⟨(p.support.filter (> 0)).1.map (λ n, n - 1), multiset.nodup_map_on begin simp only [finset.mem_def.symm, finset.mem_erase, finset.mem_filter], assume x hx y hy hxy, rwa [← @add_right_cancel_iff _ _ 1, nat.sub_add_cancel hx.2, nat.sub_add_cancel hy.2] at hxy end (p.support.filter (> 0)).2⟩, mem_support_to_fun := λ n, suffices (∃ (a : ℕ), (¬coeff p a = 0 ∧ a > 0) ∧ a - 1 = n) ↔ ¬coeff p (n + 1) = 0, by simpa [finset.mem_def.symm, apply_eq_coeff], ⟨λ ⟨a, ha⟩, by rw [← ha.2, nat.sub_add_cancel ha.1.2]; exact ha.1.1, λ h, ⟨n + 1, ⟨h, nat.succ_pos _⟩, nat.succ_sub_one _⟩⟩ } lemma div_X_mul_X_add (p : polynomial R) : div_X p * X + C (p.coeff 0) = p := ext $ λ n, nat.cases_on n (by simp) (by simp [coeff_C, nat.succ_ne_zero, coeff_mul_X, div_X]) @[simp] lemma div_X_C (a : R) : div_X (C a) = 0 := ext $ λ n, by cases n; simp [div_X, coeff_C]; simp [coeff] lemma div_X_eq_zero_iff : div_X p = 0 ↔ p = C (p.coeff 0) := ⟨λ h, by simpa [eq_comm, h] using div_X_mul_X_add p, λ h, by rw [h, div_X_C]⟩ lemma div_X_add : div_X (p + q) = div_X p + div_X q := ext $ by simp [div_X] def nonzero_comm_semiring.of_polynomial_ne (h : p ≠ q) : nonzero_comm_semiring R := { zero_ne_one := λ h01 : 0 = 1, h $ by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero], ..show comm_semiring R, by apply_instance } lemma degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X ≠ 0, by simpa, by erw [degree_mul_eq' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma degree_div_X_lt (hp0 : p ≠ 0) : (div_X p).degree < p.degree := by letI := nonzero_comm_semiring.of_polynomial_ne hp0; exact calc (div_X p).degree < (div_X p * X + C (p.coeff 0)).degree : if h : degree p ≤ 0 then begin have h' : C (p.coeff 0) ≠ 0, by rwa [← eq_C_of_degree_le_zero h], rw [eq_C_of_degree_le_zero h, div_X_C, degree_zero, zero_mul, zero_add], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 $ by simp [h'])), end else have hXp0 : div_X p ≠ 0, by simpa [div_X_eq_zero_iff, -not_le, degree_le_zero_iff] using h, have leading_coeff (div_X p) * leading_coeff X ≠ 0, by simpa, have degree (C (p.coeff 0)) < degree (div_X p * X), from calc degree (C (p.coeff 0)) ≤ 0 : degree_C_le ... < 1 : dec_trivial ... = degree (X : polynomial R) : degree_X.symm ... ≤ degree (div_X p * X) : by rw [← zero_add (degree X), degree_mul_eq' this]; exact add_le_add' (by rw [zero_le_degree_iff, ne.def, div_X_eq_zero_iff]; exact λ h0, h (h0.symm ▸ degree_C_le)) (le_refl _), by rw [add_comm, degree_add_eq_of_degree_lt this]; exact degree_lt_degree_mul_X hXp0 ... = p.degree : by rw div_X_mul_X_add @[elab_as_eliminator] noncomputable def rec_on_horner {M : polynomial R → Sort*} : Π (p : polynomial R), M 0 → (Π p a, coeff p 0 = 0 → a ≠ 0 → M p → M (p + C a)) → (Π p, p ≠ 0 → M p → M (p * X)) → M p | p := λ M0 MC MX, if hp : p = 0 then eq.rec_on hp.symm M0 else have wf : degree (div_X p) < degree p, from degree_div_X_lt hp, by rw [← div_X_mul_X_add p] at *; exact if hcp0 : coeff p 0 = 0 then by rw [hcp0, C_0, add_zero]; exact MX _ (λ h : div_X p = 0, by simpa [h, hcp0] using hp) (rec_on_horner _ M0 MC MX) else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : div_X p = 0 then show M (div_X p * X), by rw [hpX0, zero_mul]; exact M0 else MX (div_X p) hpX0 (rec_on_horner _ M0 MC MX)) using_well_founded {dec_tac := tactic.assumption} @[elab_as_eliminator] lemma degree_pos_induction_on {P : polynomial R → Prop} (p : polynomial R) (h0 : 0 < degree p) (hC : ∀ {a}, a ≠ 0 → P (C a * X)) (hX : ∀ {p}, 0 < degree p → P p → P (p * X)) (hadd : ∀ {p} {a}, 0 < degree p → P p → P (p + C a)) : P p := rec_on_horner p (λ h, by rw degree_zero at h; exact absurd h dec_trivial) (λ p a _ _ ih h0, have 0 < degree p, from lt_of_not_ge (λ h, (not_lt_of_ge degree_C_le) $ by rwa [eq_C_of_degree_le_zero h, ← C_add] at h0), hadd this (ih this)) (λ p _ ih h0', if h0 : 0 < degree p then hX h0 (ih h0) else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at *; exact hC (λ h : coeff p 0 = 0, by simpa [h, nat.not_lt_zero] using h0')) h0 end comm_semiring section comm_ring variables [comm_ring R] {p q : polynomial R} instance : comm_ring (polynomial R) := add_monoid_algebra.comm_ring instance : module R (polynomial R) := add_monoid_algebra.module variable (R) def lcoeff (n : ℕ) : polynomial R →ₗ R := { to_fun := λ f, coeff f n, add := λ f g, coeff_add f g n, smul := λ r p, coeff_smul p r n } variable {R} @[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl instance C.is_ring_hom : is_ring_hom (@C R _) := by apply is_ring_hom.of_semiring lemma int_cast_eq_C (n : ℤ) : (n : polynomial R) = C n := ((ring_hom.of C).map_int_cast n).symm @[simp] lemma C_neg : C (-a) = -C a := is_ring_hom.map_neg C @[simp] lemma C_sub : C (a - b) = C a - C b := is_ring_hom.map_sub C instance eval₂.is_ring_hom {S} [comm_ring S] (f : R → S) [is_ring_hom f] {x : S} : is_ring_hom (eval₂ f x) := by apply is_ring_hom.of_semiring instance eval.is_ring_hom {x : R} : is_ring_hom (eval x) := eval₂.is_ring_hom _ instance map.is_ring_hom {S} [comm_ring S] (f : R →+* S) : is_ring_hom (map f) := eval₂.is_ring_hom (C ∘ f) @[simp] lemma map_sub {S} [comm_ring S] (f : R →+* S) : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg {S} [comm_ring S] (f : R →+* S) : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ @[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : ℤ) : nat_degree (n : polynomial R) = 0 := by simp [int_cast_eq_C] @[simp] lemma coeff_neg (p : polynomial R) (n : ℕ) : coeff (-p) n = -coeff p n := rfl @[simp] lemma coeff_sub (p q : polynomial R) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n := rfl @[simp] lemma eval₂_neg {S} [comm_ring S] (f : R → S) [is_ring_hom f] {x : S} : (-p).eval₂ f x = -p.eval₂ f x := is_ring_hom.map_neg _ @[simp] lemma eval₂_sub {S} [comm_ring S] (f : R → S) [is_ring_hom f] {x : S} : (p - q).eval₂ f x = p.eval₂ f x - q.eval₂ f x := is_ring_hom.map_sub _ @[simp] lemma eval_neg (p : polynomial R) (x : R) : (-p).eval x = -p.eval x := is_ring_hom.map_neg _ @[simp] lemma eval_sub (p q : polynomial R) (x : R) : (p - q).eval x = p.eval x - q.eval x := is_ring_hom.map_sub _ section aeval /-- `R[X]` is the generator of the category `R-Alg`. -/ instance polynomial (R : Type u) [comm_semiring R] : algebra R (polynomial R) := { commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (polynomial.C_mul' c p).symm, .. polynomial.semimodule, .. ring_hom.of polynomial.C } variables (R) (A) variables [comm_ring A] [algebra R A] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. -/ def aeval : polynomial R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _, ..eval₂_ring_hom (algebra_map R A) x } variables {R A} theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map R A) x p := rfl @[simp] lemma aeval_X : aeval R A x X = x := eval₂_X _ x @[simp] lemma aeval_C (r : R) : aeval R A x (C r) = algebra_map R A r := eval₂_C _ x theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map R A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, φ.map_mul, eval₂_mul (algebra_map R A), eval₂_X, ih] } end end aeval lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := finsupp.single_add_erase, have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := finsupp.single_add_erase, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv {to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]} ... ≤ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) ▸ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ lemma ne_zero_of_ne_zero_of_monic (hp : p ≠ 0) (hq : monic q) : q ≠ 0 | h := begin rw [h, monic.def, leading_coeff_zero] at hq, rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp, exact hp rfl end lemma div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p ≠ 0 := mt leading_coeff_eq_zero.1 h.2, have hpq : leading_coeff (C (leading_coeff p) * X ^ (nat_degree p - nat_degree q)) * leading_coeff q ≠ 0, by rwa [leading_coeff_monomial, monic.def.1 hq, mul_one], if h0 : p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q = 0 then h0.symm ▸ (lt_of_not_ge $ mt le_bot_iff.1 (mt degree_eq_bot.1 h.2)) else have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic h.2 hq, have hlt : nat_degree q ≤ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [degree_mul_eq' hpq, degree_monomial _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, nat.sub_add_cancel hlt]) h.2 (by rw [leading_coeff_mul' hpq, leading_coeff_monomial, monic.def.1 hq, mul_one]) noncomputable def div_mod_by_monic_aux : Π (p : polynomial R) {q : polynomial R}, monic q → polynomial R × polynomial R | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : polynomial R) : polynomial R := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : polynomial R) : polynomial R := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /ₘ ` : 70 := div_by_monic infixl ` %ₘ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt : ∀ (p : polynomial R) {q : polynomial R} (hq : monic q) (hq0 : q ≠ 0), degree (p %ₘ q) < degree q | p := λ q hq hq0, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %ₘ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq hq0, begin unfold mod_by_monic at this ⊢, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊢, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq0)), end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_eq_sub_mul_div : ∀ (p : polynomial R) {q : polynomial R} (hq : monic q), p %ₘ q = p - q * (p /ₘ q) | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : polynomial R) {q : polynomial R} (hq : monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) @[simp] lemma zero_mod_by_monic (p : polynomial R) : 0 %ₘ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : polynomial R) : 0 /ₘ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : polynomial R) : p %ₘ 0 = p := if h : monic (0 : polynomial R) then (subsingleton_of_monic_zero h).1 _ _ else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : polynomial R) : p /ₘ 0 = 0 := if h : monic (0 : polynomial R) then (subsingleton_of_monic_zero h).1 _ _ else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : polynomial R) (hq : ¬monic q) : p /ₘ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : polynomial R) (hq : ¬monic q) : p %ₘ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff (hq : monic q) (hq0 : q ≠ 0) : p %ₘ q = p ↔ degree p < degree q := ⟨λ h, h ▸ degree_mod_by_monic_lt _ hq hq0, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma div_by_monic_eq_zero_iff (hq : monic q) (hq0 : q ≠ 0) : p /ₘ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq hq0] at this, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := if hq0 : q = 0 then have ∀ (p : polynomial R), p = 0, from λ p, (@subsingleton_of_monic_zero R _ (hq0 ▸ hq)).1 _ _, by rw [this (p /ₘ q), this p, this q]; refl else have hdiv0 : p /ₘ q ≠ 0 := by rwa [(≠), div_by_monic_eq_zero_iff hq hq0, not_lt], have hlc : leading_coeff q * leading_coeff (p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul, (≠), leading_coeff_eq_zero], have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q : degree_mod_by_monic_lt _ hq hq0 ... ≤ _ : by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) : eq.symm (degree_mul_eq' hlc) ... = degree (p %ₘ q + q * (p /ₘ q)) : (degree_add_eq_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) lemma degree_div_by_monic_le (p q : polynomial R) : degree (p /ₘ q) ≤ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if h : degree q ≤ degree p then by rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm ▸ bot_le lemma degree_div_by_monic_lt (p : polynomial R) {q : polynomial R} (hq : monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := have hq0 : q ≠ 0 := ne_zero_of_ne_zero_of_monic hp0 hq, if hpq : degree p < degree q then begin rw [(div_by_monic_eq_zero_iff hq hq0).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_some _ end else begin rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq0, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq0) ▸ h0q)) end lemma div_mod_by_monic_unique {f g} (q r : polynomial R) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := if hg0 : g = 0 then by split; exact (subsingleton_of_monic_zero (hg0 ▸ hg : monic (0 : polynomial R))).1 _ _ else have h₁ : r - f %ₘ g = -g * (q - f /ₘ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm, sub_eq_add_neg, add_comm, add_left_comm, add_assoc]), have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)), by simp [h₁], have h₄ : degree (r - f %ₘ g) < degree g, from calc degree (r - f %ₘ g) ≤ max (degree r) (degree (-(f %ₘ g))) : degree_add_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, by rw degree_neg; exact degree_mod_by_monic_lt _ hg hg0⟩, have h₅ : q - (f /ₘ g) = 0, from by_contradiction (λ hqf, not_le_of_gt h₄ $ calc degree g ≤ degree g + degree (q - f /ₘ g) : by erw [degree_eq_nat_degree hg0, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %ₘ g) : by rw [h₂, degree_mul_eq']; simpa [monic.def.1 hg]), ⟨eq.symm $ eq_of_sub_eq_zero h₅, eq.symm $ eq_of_sub_eq_zero $ by simpa [h₅] using h₁⟩ lemma map_mod_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := if h01 : (0 : S) = 1 then by haveI := subsingleton_of_zero_eq_one S h01; exact ⟨subsingleton.elim _ _, subsingleton.elim _ _⟩ else have h01R : (0 : R) ≠ 1, from mt (congr_arg f) (by rwa [is_semiring_hom.map_one f, is_semiring_hom.map_zero f]), have map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q), from (div_mod_by_monic_unique ((p /ₘ q).map f) _ (monic_map f hq) ⟨eq.symm $ by rw [← map_mul, ← map_add, mod_by_monic_add_div _ hq], calc _ ≤ degree (p %ₘ q) : degree_map_le _ ... < degree q : degree_mod_by_monic_lt _ hq $ (ne_zero_of_monic_of_zero_ne_one hq h01R) ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, is_semiring_hom.map_one f]; exact ne.symm h01)⟩), ⟨this.1.symm, this.2.symm⟩ lemma map_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, λ h, if hq0 : q = 0 then by rw hq0 at hq; exact (subsingleton_of_monic_zero hq).1 _ _ else let ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h in by_contradiction (λ hpq0, have hmod : p %ₘ q = q * (r - p /ₘ q) := by rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr], have degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_mod_by_monic_lt _ hq hq0, have hrpq0 : leading_coeff (r - p /ₘ q) ≠ 0 := λ h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul], by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this; exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this))⟩ @[simp] lemma mod_by_monic_one (p : polynomial R) : p %ₘ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero monic_one).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : polynomial R) : p /ₘ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp lemma degree_pos_of_root (hp : p ≠ 0) (h : is_root p a) : 0 < degree p := lt_of_not_ge $ λ hlt, begin have := eq_C_of_degree_le_zero hlt, rw [is_root, this, eval_C] at h, exact hp (finsupp.ext (λ n, show coeff p n = 0, from nat.cases_on n h (λ _, coeff_eq_zero_of_degree_lt (lt_of_le_of_lt hlt (with_bot.coe_lt_coe.2 (nat.succ_pos _)))))), end theorem monic_X_sub_C (x : R) : monic (X - C x) := by simpa only [C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) - p) := monic_X_pow_add ((degree_neg p).symm ▸ H) theorem degree_mod_by_monic_le (p : polynomial R) {q : polynomial R} (hq : monic q) : degree (p %ₘ q) ≤ degree q := decidable.by_cases (assume H : q = 0, by rw [monic, H, leading_coeff_zero] at hq; have : (0:polynomial R) = 1 := (by rw [← C_0, ← C_1, hq]); rw [eq_zero_of_zero_eq_one _ this (p %ₘ q), eq_zero_of_zero_eq_one _ this q]; exact le_refl _) (assume H : q ≠ 0, le_of_lt $ degree_mod_by_monic_lt _ hq H) lemma root_X_sub_C : is_root (X - C a) b ↔ a = b := by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm] def nonzero_comm_ring.of_polynomial_ne (h : p ≠ q) : nonzero_comm_ring R := { zero := 0, one := 1, zero_ne_one := λ h01, h $ by rw [← one_mul p, ← one_mul q, ← C_1, ← h01, C_0, zero_mul, zero_mul], ..show comm_ring R, by apply_instance } end comm_ring section nonzero_comm_ring variables [nonzero_comm_ring R] {p q : polynomial R} instance : nonzero_comm_ring (polynomial R) := { ..polynomial.nonzero_comm_semiring, ..polynomial.comm_ring } @[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 := begin rw [sub_eq_add_neg, add_comm, ← @degree_X R], by_cases ha : a = 0, { simp only [ha, C_0, neg_zero, zero_add] }, exact degree_add_eq_of_degree_lt (by rw [degree_X, degree_neg, degree_C ha]; exact dec_trivial) end lemma degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : polynomial R) ^ n - C a) = n := have degree (-C a) < degree ((X : polynomial R) ^ n), from calc degree (-C a) ≤ 0 : by rw degree_neg; exact degree_C_le ... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [sub_eq_add_neg, add_comm, degree_add_eq_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : polynomial R) ^ n - C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) ≠ ⊥, by rw degree_X_pow_sub_C hn; exact dec_trivial) end nonzero_comm_ring section comm_ring variables [comm_ring R] {p q : polynomial R} @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : polynomial R) (a : R) : p %ₘ (X - C a) = C (p.eval a) := if h0 : (0 : R) = 1 then by letI := subsingleton_of_zero_eq_one R h0; exact subsingleton.elim _ _ else by letI : nonzero_comm_ring R := nonzero_comm_ring.of_ne h0; exact have h : (p %ₘ (X - C a)).eval a = p.eval a := by rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero], have degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_mod_by_monic_lt p (monic_X_sub_C a) ((degree_X_sub_C a).symm ▸ ne_zero_of_monic (monic_X_sub_C _)), have degree (p %ₘ (X - C a)) ≤ 0 := begin cases (degree (p %ₘ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } end, begin rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /ₘ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], λ h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, λ h, ⟨(p /ₘ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : polynomial R) : p %ₘ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] section multiplicity def decidable_dvd_monic (p : polynomial R) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : R) (h0 : p ≠ 0) : multiplicity.finite (X - C a) p := multiplicity_finite_of_degree_pos_of_monic (have (0 : R) ≠ 1, from (λ h, by haveI := subsingleton_of_zero_eq_one _ h; exact h0 (subsingleton.elim _ _)), by letI : nonzero_comm_ring R := { zero_ne_one := this, ..show comm_ring R, by apply_instance }; rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) h0 def root_multiplicity (a : R) (p : polynomial R) : ℕ := if h0 : p = 0 then 0 else let I : decidable_pred (λ n : ℕ, ¬(X - C a) ^ (n + 1) ∣ p) := λ n, @not.decidable _ (decidable_dvd_monic p (monic_pow (monic_X_sub_C a) (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : polynomial R) (a : R) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, roption.dom]; congr; funext; congr lemma pow_root_multiplicity_dvd (p : polynomial R) (a : R) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : polynomial R) (a : R) : p /ₘ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from monic_pow (monic_X_sub_C _) _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : polynomial R} (a : R) (hp : p ≠ 0) : (p /ₘ ((X - C a) ^ root_multiplicity a p)).eval a ≠ 0 := begin letI : nonzero_comm_ring R := nonzero_comm_ring.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot ℕ) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring section integral_domain variables [integral_domain R] {p q : polynomial R} @[simp] lemma degree_mul_eq : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul_eq' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow_eq (p : polynomial R) (n : ℕ) : degree (p ^ n) = add_monoid.smul n (degree p) := by induction n; [simp only [pow_zero, degree_one, add_monoid.zero_smul], simp only [*, pow_succ, succ_smul, degree_mul_eq]] @[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_pow (p : polynomial R) (n : ℕ) : leading_coeff (p ^ n) = leading_coeff p ^ n := by induction n; [simp only [pow_zero, leading_coeff_one], simp only [*, pow_succ, leading_coeff_mul]] instance : integral_domain (polynomial R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin have : leading_coeff 0 = leading_coeff a * leading_coeff b := h ▸ leading_coeff_mul a b, rw [leading_coeff_zero, eq_comm] at this, erw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], exact eq_zero_or_eq_zero_of_mul_eq_zero this end, ..polynomial.nonzero_comm_ring } lemma nat_degree_mul_eq (hp : p ≠ 0) (hq : q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul_eq] @[simp] lemma nat_degree_pow_eq (p : polynomial R) (n : ℕ) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow_eq' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := by rw [is_root, eval_mul] at h; exact eq_zero_or_eq_zero_of_mul_eq_zero h lemma degree_le_mul_left (p : polynomial R) (hq : q ≠ 0) : degree p ≤ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul_eq, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) lemma exists_finset_roots : ∀ {p : polynomial R} (hp : p ≠ 0), ∃ s : finset R, (s.card : with_bot ℕ) ≤ degree p ∧ ∀ x, x ∈ s ↔ is_root p x | p := λ hp, by haveI := classical.prop_decidable (∃ x, is_root p x); exact if h : ∃ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /ₘ (X - C x) ≠ 0 := λ h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /ₘ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm ▸ dec_trivial), let ⟨t, htd, htr⟩ := @exists_finset_roots (p /ₘ (X - C x)) hd0 in have hdeg : degree (X - C x) ≤ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /ₘ (X - C x) ≠ 0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨insert x t, calc (card (insert x t) : with_bot ℕ) ≤ card t + 1 : with_bot.coe_le_coe.2 $ finset.card_insert_le _ _ ... ≤ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add' (le_refl (1 : with_bot ℕ)) htd, begin assume y, rw [mem_insert, htr, eq_comm, ← root_X_sub_C], conv {to_rhs, rw ← mul_div_by_monic_eq_iff_is_root.2 hx}, exact ⟨λ h, or.cases_on h (root_mul_right_of_is_root _) (root_mul_left_of_is_root _), root_or_root_of_root_mul⟩ end⟩ else ⟨∅, (degree_eq_nat_degree hp).symm ▸ with_bot.coe_le_coe.2 (nat.zero_le _), by simpa only [not_mem_empty, false_iff, not_exists] using h⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a finset containing all the roots of `p` -/ noncomputable def roots (p : polynomial R) : finset R := if h : p = 0 then ∅ else classical.some (exists_finset_roots h) lemma card_roots (hp0 : p ≠ 0) : ((roots p).card : with_bot ℕ) ≤ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_finset_roots hp0)).1 end lemma card_roots' {p : polynomial R} (hp0 : p ≠ 0) : p.roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial R} {a : R} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot ℕ) ≤ degree p := calc ((p - C a).roots.card : with_bot ℕ) ≤ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial R} {a : R} (hp0 : 0 < degree p) : (p - C a).roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (λ h, by simp [*, lt_irrefl] at *))) @[simp] lemma mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ is_root p a := by unfold roots; rw dif_neg hp; exact (classical.some_spec (exists_finset_roots hp)).2 _ @[simp] lemma mem_roots_sub_C {p : polynomial R} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a ≠ 0, from mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) lemma card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : (roots ((X : polynomial R) ^ n - C a)).card ≤ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial R) ^ n - C a)).card : with_bot ℕ) ≤ degree ((X : polynomial R) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots {R : Type*} [integral_domain R] (n : ℕ) (a : R) : finset R := roots ((X : polynomial R) ^ n - C a) @[simp] lemma mem_nth_roots {R : Type*} [integral_domain R] {n : ℕ} (hn : 0 < n) {a x : R} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq] lemma card_nth_roots {R : Type*} [integral_domain R] (n : ℕ) (a : R) : (nth_roots n a).card ≤ n := if hn : n = 0 then if h : (X : polynomial R) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, card_empty] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C R _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q ≠ 0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (λ n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, eval₂, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q ≠ 0, from λ hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b ≠ 0, rwa [← apply_eq_coeff, ← finsupp.mem_support_iff], dsimp [apply_eq_coeff], refine coeff_eq_zero_of_degree_lt _, rw [degree_mul_eq, degree_C this, degree_pow_eq, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_smul, add_monoid.smul_eq_mul, with_bot.coe_lt_coe, nat.cast_id], exact (mul_lt_mul_right (nat.pos_of_ne_zero hqd0)).2 (lt_of_le_of_ne (with_bot.coe_le_coe.1 (by rw ← degree_eq_nat_degree hp0; exact le_sup hbs)) hbp) end (by rw [finsupp.mem_support_iff, apply_eq_coeff, ← leading_coeff, ne.def, leading_coeff_eq_zero, classical.not_not]; simp {contextual := tt}) ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow_eq], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≤ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q ≠ 0, from λ hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... ≠ 0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q ≠ 0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p ≠ 0, from λ hp0, by simpa [hp0] using hq, have hq0 : q ≠ 0, from λ hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial R) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul_eq hp0 hq0, eq_comm, _root_.add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial R)) : degree (u : polynomial R) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ @[simp] lemma nat_degree_coe_units (u : units (polynomial R)) : nat_degree (u : polynomial R) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) lemma coeff_coe_units_zero_ne_zero (u : units (polynomial R)) : coeff (u : polynomial R) 0 ≠ 0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.coe_ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : R} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.2 _ _ hg, this.elim (λ h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have h₂ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at h₂; exact absurd h₂ dec_trivial) (λ hgu, by rw [hg, degree_mul_eq, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) lemma prime_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : prime p := have p = X - C (- p.coeff 0), by simpa [hm.leading_coeff] using eq_X_add_C_of_degree_eq_one hp1, ⟨mt degree_eq_bot.2 $ hp1.symm ▸ dec_trivial, mt degree_eq_zero_of_is_unit (by simp [hp1]; exact dec_trivial), λ _ _, begin rw [this, dvd_iff_is_root, dvd_iff_is_root, dvd_iff_is_root, is_root, is_root, is_root, eval_mul, mul_eq_zero], exact id end⟩ lemma irreducible_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : irreducible p := irreducible_of_prime (prime_of_degree_eq_one_of_monic hp1 hm) end integral_domain section field variables [field R] {p q : polynomial R} instance : vector_space R (polynomial R) := finsupp.vector_space _ _ lemma is_unit_iff_degree_eq_zero : is_unit p ↔ degree p = 0 := ⟨degree_eq_zero_of_is_unit, λ h, have degree p ≤ 0, by simp [*, le_refl], have hc : coeff p 0 ≠ 0, from λ hc, by rw [eq_C_of_degree_le_zero this, hc] at h; simpa using h, is_unit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, begin conv in p { rw eq_C_of_degree_le_zero this }, rw [← C_mul, _root_.mul_inv_cancel hc, C_1] end⟩⟩ lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p ≠ 0) (hp : ¬is_unit p) : 0 < degree p := lt_of_not_ge (λ h, by rw [eq_C_of_degree_le_zero h] at hp0 hp; exact (hp $ is_unit.map' C $ is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))) lemma monic_mul_leading_coeff_inv (h : p ≠ 0) : monic (p * C (leading_coeff p)⁻¹) := by rw [monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel (show leading_coeff p ≠ 0, from mt leading_coeff_eq_zero.1 h)] lemma degree_mul_leading_coeff_inv (p : polynomial R) (h : q ≠ 0) : degree (p * C (leading_coeff q)⁻¹) = degree p := have h₁ : (leading_coeff q)⁻¹ ≠ 0 := inv_ne_zero (mt leading_coeff_eq_zero.1 h), by rw [degree_mul_eq, degree_C h₁, add_zero] def div (p q : polynomial R) := C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) def mod (p q : polynomial R) := p %ₘ (q * C (leading_coeff q)⁻¹) private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial R) : q * div p q + mod p q = p := if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add] else begin conv {to_rhs, rw ← mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)}, rw [div, mod, add_comm, mul_assoc] end private lemma remainder_lt_aux (p : polynomial R) (hq : q ≠ 0) : degree (mod p q) < degree q := by rw ← degree_mul_leading_coeff_inv q hq; exact degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq) (mul_ne_zero hq (mt leading_coeff_eq_zero.2 (by rw leading_coeff_C; exact inv_ne_zero (mt leading_coeff_eq_zero.1 hq)))) instance : has_div (polynomial R) := ⟨div⟩ instance : has_mod (polynomial R) := ⟨mod⟩ lemma div_def : p / q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) := rfl lemma mod_def : p % q = p %ₘ (q * C (leading_coeff q)⁻¹) := rfl lemma mod_by_monic_eq_mod (p : polynomial R) (hq : monic q) : p %ₘ q = p % q := show p %ₘ q = p %ₘ (q * C (leading_coeff q)⁻¹), by simp only [monic.def.1 hq, inv_one, mul_one, C_1] lemma div_by_monic_eq_div (p : polynomial R) (hq : monic q) : p /ₘ q = p / q := show p /ₘ q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)), by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one] lemma mod_X_sub_C_eq_C_eval (p : polynomial R) (a : R) : p % (X - C a) = C (p.eval a) := mod_by_monic_eq_mod p (monic_X_sub_C a) ▸ mod_by_monic_X_sub_C_eq_C_eval _ _ lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p ↔ is_root p a := div_by_monic_eq_div p (monic_X_sub_C a) ▸ mul_div_by_monic_eq_iff_is_root instance : euclidean_domain (polynomial R) := { quotient := (/), quotient_zero := by simp [div_def], remainder := (%), r := _, r_well_founded := degree_lt_wf, quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux, remainder_lt := λ p q hq, remainder_lt_aux _ hq, mul_left_not_lt := λ p q hq, not_lt_of_ge (degree_le_mul_left _ hq) } lemma mod_eq_self_iff (hq0 : q ≠ 0) : p % q = p ↔ degree p < degree q := ⟨λ h, h ▸ euclidean_domain.mod_lt _ hq0, λ h, have ¬degree (q * C (leading_coeff q)⁻¹) ≤ degree p := not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0, begin rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)], unfold div_mod_by_monic_aux, simp only [this, false_and, if_false] end⟩ lemma div_eq_zero_iff (hq0 : q ≠ 0) : p / q = 0 ↔ degree p < degree q := ⟨λ h, by have := euclidean_domain.div_add_mod p q; rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this, λ h, have hlt : degree p < degree (q * C (leading_coeff q)⁻¹), by rwa degree_mul_leading_coeff_inv q hq0, have hm : monic (q * C (leading_coeff q)⁻¹) := monic_mul_leading_coeff_inv hq0, by rw [div_def, (div_by_monic_eq_zero_iff hm (ne_zero_of_monic hm)).2 hlt, mul_zero]⟩ lemma degree_add_div (hq0 : q ≠ 0) (hpq : degree q ≤ degree p) : degree q + degree (p / q) = degree p := have degree (p % q) < degree (q * (p / q)) := calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0 ... ≤ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)), by conv {to_rhs, rw [← euclidean_domain.div_add_mod p q, add_comm, degree_add_eq_of_degree_lt this, degree_mul_eq]} lemma degree_div_le (p q : polynomial R) : degree (p / q) ≤ degree p := if hq : q = 0 then by simp [hq] else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq]; exact degree_div_by_monic_le _ _ lemma degree_div_lt (hp : p ≠ 0) (hq : 0 < degree q) : degree (p / q) < degree p := have hq0 : q ≠ 0, from λ hq0, by simpa [hq0] using hq, by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0]; exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp (by rw degree_mul_leading_coeff_inv _ hq0; exact hq) @[simp] lemma degree_map [field k] (p : polynomial R) (f : R →+* k) : degree (p.map f) = degree p := p.degree_map_eq_of_injective (is_ring_hom.injective f) @[simp] lemma nat_degree_map [field k] (f : R →+* k) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map _ f) @[simp] lemma leading_coeff_map [field k] (f : R →+* k) : leading_coeff (p.map f) = f (leading_coeff p) := by simp [leading_coeff, coeff_map f] lemma map_div [field k] (f : R →+* k) : (p / q).map f = p.map f / q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)]; simp [is_ring_hom.map_inv f, leading_coeff, coeff_map f] lemma map_mod [field k] (f : R →+* k) : (p % q).map f = p.map f % q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [mod_def, mod_def, leading_coeff_map f, ← is_ring_hom.map_inv f, ← map_C f, ← map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)] @[simp] lemma map_eq_zero [field k] (f : R →+* k) : p.map f = 0 ↔ p = 0 := by simp [polynomial.ext_iff, is_ring_hom.map_eq_zero f, coeff_map] lemma exists_root_of_degree_eq_one (h : degree p = 1) : ∃ x, is_root p x := ⟨-(p.coeff 0 / p.coeff 1), have p.coeff 1 ≠ 0, by rw ← nat_degree_eq_of_degree_eq_some h; exact mt leading_coeff_eq_zero.1 (λ h0, by simpa [h0] using h), by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p ≤ 1, by rw h; exact le_refl _)] }; simp [is_root, mul_div_cancel' _ this]⟩ lemma coeff_inv_units (u : units (polynomial R)) (n : ℕ) : ((↑u : polynomial R).coeff n)⁻¹ = ((↑u⁻¹ : polynomial R).coeff n) := begin rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹), coeff_C, coeff_C, inv_eq_one_div], split_ifs, { rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero, coeff_zero_eq_eval_zero, ← eval_mul, ← units.coe_mul, inv_mul_self]; simp }, { simp } end instance : normalization_domain (polynomial R) := { norm_unit := λ p, if hp0 : p = 0 then 1 else ⟨C p.leading_coeff⁻¹, C p.leading_coeff, by rw [← C_mul, inv_mul_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0, by rw [← C_mul, mul_inv_cancel, C_1]; exact mt leading_coeff_eq_zero.1 hp0,⟩, norm_unit_zero := dif_pos rfl, norm_unit_mul := λ p q hp0 hq0, begin rw [dif_neg hp0, dif_neg hq0, dif_neg (mul_ne_zero hp0 hq0)], apply units.ext, show C (leading_coeff (p * q))⁻¹ = C (leading_coeff p)⁻¹ * C (leading_coeff q)⁻¹, rw [leading_coeff_mul, mul_inv', C_mul, mul_comm] end, norm_unit_coe_units := λ u, have hu : degree ↑u⁻¹ = 0, from degree_eq_zero_of_is_unit ⟨u⁻¹, rfl⟩, begin apply units.ext, rw [dif_neg (units.coe_ne_zero u)], conv_rhs {rw eq_C_of_degree_eq_zero hu}, refine C_inj.2 _, rw [← nat_degree_eq_of_degree_eq_some hu, leading_coeff, coeff_inv_units], simp end, ..polynomial.integral_domain } lemma monic_normalize (hp0 : p ≠ 0) : monic (normalize p) := show leading_coeff (p * ↑(dite _ _ _)) = 1, by rw dif_neg hp0; exact monic_mul_leading_coeff_inv hp0 lemma coe_norm_unit (hp : p ≠ 0) : (norm_unit p : polynomial R) = C p.leading_coeff⁻¹ := show ↑(dite _ _ _) = C p.leading_coeff⁻¹, by rw dif_neg hp; refl @[simp] lemma degree_normalize : degree (normalize p) = degree p := if hp0 : p = 0 then by simp [hp0] else by rw [normalize, degree_mul_eq, degree_eq_zero_of_is_unit (is_unit_unit _), add_zero] lemma prime_of_degree_eq_one (hp1 : degree p = 1) : prime p := have prime (normalize p), from prime_of_degree_eq_one_of_monic (hp1 ▸ degree_normalize) (monic_normalize (λ hp0, absurd hp1 (hp0.symm ▸ by simp; exact dec_trivial))), prime_of_associated normalize_associated this lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p := irreducible_of_prime (prime_of_degree_eq_one hp1) end field section derivative variables [comm_semiring R] /-- `derivative p` is the formal derivative of the polynomial `p` -/ def derivative (p : polynomial R) : polynomial R := p.sum (λn a, C (a * n) * X^(n - 1)) lemma coeff_derivative (p : polynomial R) (n : ℕ) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) := begin rw [derivative], simp only [coeff_X_pow, coeff_sum, coeff_C_mul], rw [finsupp.sum, finset.sum_eq_single (n + 1), apply_eq_coeff], { rw [if_pos (nat.add_sub_cancel _ _).symm, mul_one, nat.cast_add, nat.cast_one] }, { assume b, cases b, { intros, rw [nat.cast_zero, mul_zero, zero_mul] }, { intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } }, { intro H, rw [not_mem_support_iff.1 H, zero_mul, zero_mul] } end @[simp] lemma derivative_zero : derivative (0 : polynomial R) = 0 := finsupp.sum_zero_index lemma derivative_monomial (a : R) (n : ℕ) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) := by rw [← single_eq_C_mul_X, ← single_eq_C_mul_X, derivative, sum_single_index, single_eq_C_mul_X]; simp only [zero_mul, C_0]; refl @[simp] lemma derivative_C {a : R} : derivative (C a) = 0 := suffices derivative (C a * X^0) = C (a * 0:R) * X ^ 0, by simpa only [mul_one, zero_mul, C_0, mul_zero, pow_zero], derivative_monomial a 0 @[simp] lemma derivative_X : derivative (X : polynomial R) = 1 := suffices derivative (C (1:R) * X^1) = C (1 * (1:ℕ)) * X ^ 0, by simpa only [mul_one, one_mul, C_1, pow_one, nat.cast_one, pow_zero], derivative_monomial 1 1 @[simp] lemma derivative_one : derivative (1 : polynomial R) = 0 := derivative_C @[simp] lemma derivative_add {f g : polynomial R} : derivative (f + g) = derivative f + derivative g := by refine finsupp.sum_add_index _ _; intros; simp only [add_mul, zero_mul, C_0, C_add, C_mul] /-- The formal derivative of polynomials, as additive homomorphism. -/ def derivative_hom (R : Type*) [comm_semiring R] : polynomial R →+ polynomial R := { to_fun := derivative, map_zero' := derivative_zero, map_add' := λ p q, derivative_add } @[simp] lemma derivative_neg {R : Type*} [comm_ring R] (f : polynomial R) : derivative (-f) = -derivative f := (derivative_hom R).map_neg f @[simp] lemma derivative_sub {R : Type*} [comm_ring R] (f g : polynomial R) : derivative (f - g) = derivative f - derivative g := (derivative_hom R).map_sub f g instance : is_add_monoid_hom (derivative : polynomial R → polynomial R) := (derivative_hom R).is_add_monoid_hom @[simp] lemma derivative_sum {s : finset ι} {f : ι → polynomial R} : derivative (s.sum f) = s.sum (λb, derivative (f b)) := (derivative_hom R).map_sum f s @[simp] lemma derivative_mul {f g : polynomial R} : derivative (f * g) = derivative f * g + f * derivative g := calc derivative (f * g) = f.sum (λn a, g.sum (λm b, C ((a * b) * (n + m : ℕ)) * X^((n + m) - 1))) : begin transitivity, exact derivative_sum, transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum }, apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm, transitivity, { apply congr_arg, exact single_eq_C_mul_X }, exact derivative_monomial _ _ end ... = f.sum (λn a, g.sum (λm b, (C (a * n) * X^(n - 1)) * (C b * X^m) + (C a * X^n) * (C (b * m) * X^(m - 1)))) : sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm, by simp only [nat.cast_add, mul_add, add_mul, C_add, C_mul]; cases n; simp only [nat.succ_sub_succ, pow_zero]; cases m; simp only [nat.cast_zero, C_0, nat.succ_sub_succ, zero_mul, mul_zero, nat.sub_zero, pow_zero, pow_add, one_mul, pow_succ, mul_comm, mul_left_comm] ... = derivative f * g + f * derivative g : begin conv { to_rhs, congr, { rw [← sum_C_mul_X_eq g] }, { rw [← sum_C_mul_X_eq f] } }, unfold derivative finsupp.sum, simp only [sum_add_distrib, finset.mul_sum, finset.sum_mul] end lemma derivative_eval (p : polynomial R) (x : R) : p.derivative.eval x = p.sum (λ n a, (a * n)*x^(n-1)) := by simp [derivative, eval_sum, eval_pow] @[simp] lemma derivative_smul (r : R) (p : polynomial R) : derivative (r • p) = r • derivative p := by { ext, simp only [coeff_derivative, mul_assoc, coeff_smul], } /-- The formal derivative of polynomials, as linear homomorphism. -/ def derivative_lhom (R : Type*) [comm_ring R] : polynomial R →ₗ[R] polynomial R := { to_fun := derivative, add := λ p q, derivative_add, smul := λ r p, derivative_smul r p } /-- If `f` is a polynomial over a field, and `a : K` satisfies `f' a ≠ 0`, then `f / (X - a)` is coprime with `X - a`. Note that we do not assume `f a = 0`, because `f / (X - a) = (f - f a) / (X - a)`. -/ lemma is_coprime_of_is_root_of_eval_derivative_ne_zero {K : Type*} [field K] (f : polynomial K) (a : K) (hf' : f.derivative.eval a ≠ 0) : ideal.is_coprime (X - C a : polynomial K) (f /ₘ (X - C a)) := begin refine or.resolve_left (dvd_or_coprime (X - C a) (f /ₘ (X - C a)) (irreducible_of_degree_eq_one (polynomial.degree_X_sub_C a))) _, contrapose! hf' with h, have key : (X - C a) * (f /ₘ (X - C a)) = f - (f %ₘ (X - C a)), { rw [eq_sub_iff_add_eq, ← eq_sub_iff_add_eq', mod_by_monic_eq_sub_mul_div], exact monic_X_sub_C a }, replace key := congr_arg derivative key, simp only [derivative_X, derivative_mul, one_mul, sub_zero, derivative_sub, mod_by_monic_X_sub_C_eq_C_eval, derivative_C] at key, have : (X - C a) ∣ derivative f := key ▸ (dvd_add h (dvd_mul_right _ _)), rw [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval] at this, rw [← C_inj, this, C_0], end end derivative section domain variables [integral_domain R] lemma mem_support_derivative [char_zero R] (p : polynomial R) (n : ℕ) : n ∈ (derivative p).support ↔ n + 1 ∈ p.support := suffices (¬(coeff p (n + 1) = 0 ∨ ((n + 1:ℕ) : R) = 0)) ↔ coeff p (n + 1) ≠ 0, by simpa only [coeff_derivative, apply_eq_coeff, mem_support_iff, ne.def, mul_eq_zero], by rw [nat.cast_eq_zero]; simp only [nat.succ_ne_zero, or_false] @[simp] lemma degree_derivative_eq [char_zero R] (p : polynomial R) (hp : 0 < nat_degree p) : degree (derivative p) = (nat_degree p - 1 : ℕ) := le_antisymm (le_trans (degree_sum_le _ _) $ sup_le $ assume n hn, have n ≤ nat_degree p, begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], { refine le_degree_of_ne_zero _, simpa only [mem_support_iff] using hn }, { assume h, simpa only [h, support_zero] using hn } end, le_trans (degree_monomial_le _ _) $ with_bot.coe_le_coe.2 $ nat.sub_le_sub_right this _) begin refine le_sup _, rw [mem_support_derivative, nat.sub_add_cancel, mem_support_iff], { show ¬ leading_coeff p = 0, rw [leading_coeff_eq_zero], assume h, rw [h, nat_degree_zero] at hp, exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), }, exact hp end end domain section identities /- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {R : Type*} [comm_semiring R] (x y : R) : ∀ (n : ℕ), {k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2} | 0 := ⟨0, by simp⟩ | 1 := ⟨0, by simp⟩ | (n+2) := begin cases pow_add_expansion (n+1) with z hz, existsi x*z + (n+1)*x^n+z*y, calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp ... = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz ... = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 : by { push_cast, ring_exp! } end variables [comm_ring R] private def poly_binom_aux1 (x y : R) (e : ℕ) (a : R) : {k : R // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} := begin existsi (pow_add_expansion x y e).val, congr, apply (pow_add_expansion _ _ _).property end private lemma poly_binom_aux2 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) := begin unfold eval eval₂, congr, ext, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * x^e) + f.sum (λ e a, (a * e * x^(e-1)) * y) + f.sum (λ e a, (a *(poly_binom_aux1 x y e a).val)*y^2) := by rw poly_binom_aux2; simp [left_distrib, finsupp.sum_add, mul_assoc] def binom_expansion (f : polynomial R) (x y : R) : {k : R // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} := begin existsi f.sum (λ e a, a *((poly_binom_aux1 x y e a).val)), rw poly_binom_aux3, congr, { rw derivative_eval, symmetry, apply finsupp.sum_mul }, { symmetry, apply finsupp.sum_mul } end def pow_sub_pow_factor (x y : R) : Π (i : ℕ), {z : R // x^i - y^i = z * (x - y)} | 0 := ⟨0, by simp⟩ | 1 := ⟨1, by simp⟩ | (k+2) := begin cases @pow_sub_pow_factor (k+1) with z hz, existsi z*x + y^(k+1), calc x ^ (k + 2) - y ^ (k + 2) = x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp ... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz ... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp end def eval_sub_factor (f : polynomial R) (x y : R) : {z : R // f.eval x - f.eval y = z * (x - y)} := begin refine ⟨f.sum (λ i r, r * (pow_sub_pow_factor x y i).val), _⟩, delta eval eval₂, rw ← finsupp.sum_sub, rw finsupp.sum_mul, delta finsupp.sum, congr, ext i r, dsimp, rw [mul_assoc, ←(pow_sub_pow_factor x y _).property, mul_sub], end end identities end polynomial namespace is_integral_domain variables {R : Type*} [comm_ring R] /-- Lift evidence that `is_integral_domain R` to `is_integral_domain (polynomial R)`. -/ lemma polynomial (h : is_integral_domain R) : is_integral_domain (polynomial R) := @integral_domain.to_is_integral_domain _ (@polynomial.integral_domain _ (h.to_integral_domain _)) end is_integral_domain
0e8462b6ba3a4997ca04571d3de740f8cf77e79e
ce89339993655da64b6ccb555c837ce6c10f9ef4
/bluejam/topprover/01.lean
9df2f48eca9b22b51dae8c6189ee8602d301a8b7
[]
no_license
zeptometer/LearnLean
ef32dc36a22119f18d843f548d0bb42f907bff5d
bb84d5dbe521127ba134d4dbf9559b294a80b9f7
refs/heads/master
1,625,710,824,322
1,601,382,570,000
1,601,382,570,000
195,228,870
2
0
null
null
null
null
UTF-8
Lean
false
false
74
lean
example : ∀ (n m o : ℕ), n + m + o = n + (m + o) := by intros; simp *
ea5de94ae71ae435def23fc07ffe1b9cb7a86165
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/equiv/encodable/basic.lean
1ed5010eeb8d6b4a2cc0a69b52429007717ec0ce
[ "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
17,827
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.equiv.nat import order.rel_iso import order.directed /-! # Encodable types This file defines encodable (constructively countable) types as a typeclass. This is used to provide explicit encode/decode functions from and to `ℕ`, with the information that those functions are inverses of each other. The difference with `denumerable` is that finite types are encodable. For infinite types, `encodable` and `denumerable` agree. ## Main declarations * `encodable α`: States that there exists an explicit encoding function `encode : α → ℕ` with a partial inverse `decode : ℕ → option α`. * `decode₂`: Version of `decode` that is equal to `none` outside of the range of `encode`. Useful as we do not require this in the definition of `decode`. * `ulower α`: Any encodable type has an equivalent type living in the lowest universe, namely a subtype of `ℕ`. `ulower α` finds it. ## Implementation notes The point of asking for an explicit partial inverse `decode : ℕ → option α` to `encode : α → ℕ` is to make the range of `encode` decidable even when the finiteness of `α` is not. -/ open option list nat function /-- Constructively countable type. Made from an explicit injection `encode : α → ℕ` and a partial inverse `decode : ℕ → option α`. Note that finite types *are* countable. See `denumerable` if you wish to enforce infiniteness. -/ class encodable (α : Type*) := (encode : α → ℕ) (decode [] : ℕ → option α) (encodek : ∀ a, decode (encode a) = some a) attribute [simp] encodable.encodek namespace encodable variables {α : Type*} {β : Type*} universe u theorem encode_injective [encodable α] : function.injective (@encode α _) | x y e := option.some.inj $ by rw [← encodek, e, encodek] lemma surjective_decode_iget (α : Type*) [encodable α] [inhabited α] : surjective (λ n, (encodable.decode α n).iget) := λ x, ⟨encodable.encode x, by simp_rw [encodable.encodek]⟩ /-- An encodable type has decidable equality. Not set as an instance because this is usually not the best way to infer decidability. -/ def decidable_eq_of_encodable (α) [encodable α] : decidable_eq α | a b := decidable_of_iff _ encode_injective.eq_iff /-- If `α` is encodable and there is an injection `f : β → α`, then `β` is encodable as well. -/ def of_left_injection [encodable α] (f : β → α) (finv : α → option β) (linv : ∀ b, finv (f b) = some b) : encodable β := ⟨λ b, encode (f b), λ n, (decode α n).bind finv, λ b, by simp [encodable.encodek, linv]⟩ /-- If `α` is encodable and `f : β → α` is invertible, then `β` is encodable as well. -/ def of_left_inverse [encodable α] (f : β → α) (finv : α → β) (linv : ∀ b, finv (f b) = b) : encodable β := of_left_injection f (some ∘ finv) (λ b, congr_arg some (linv b)) /-- Encodability is preserved by equivalence. -/ def of_equiv (α) [encodable α] (e : β ≃ α) : encodable β := of_left_inverse e e.symm e.left_inv @[simp] theorem encode_of_equiv {α β} [encodable α] (e : β ≃ α) (b : β) : @encode _ (of_equiv _ e) b = encode (e b) := rfl @[simp] theorem decode_of_equiv {α β} [encodable α] (e : β ≃ α) (n : ℕ) : @decode _ (of_equiv _ e) n = (decode α n).map e.symm := rfl instance nat : encodable ℕ := ⟨id, some, λ a, rfl⟩ @[simp] theorem encode_nat (n : ℕ) : encode n = n := rfl @[simp] theorem decode_nat (n : ℕ) : decode ℕ n = some n := rfl instance empty : encodable empty := ⟨λ a, a.rec _, λ n, none, λ a, a.rec _⟩ instance unit : encodable punit := ⟨λ_, zero, λ n, nat.cases_on n (some punit.star) (λ _, none), λ _, by simp⟩ @[simp] theorem encode_star : encode punit.star = 0 := rfl @[simp] theorem decode_unit_zero : decode punit 0 = some punit.star := rfl @[simp] theorem decode_unit_succ (n) : decode punit (succ n) = none := rfl /-- If `α` is encodable, then so is `option α`. -/ instance option {α : Type*} [h : encodable α] : encodable (option α) := ⟨λ o, option.cases_on o nat.zero (λ a, succ (encode a)), λ n, nat.cases_on n (some none) (λ m, (decode α m).map some), λ o, by cases o; dsimp; simp [encodek, nat.succ_ne_zero]⟩ @[simp] theorem encode_none [encodable α] : encode (@none α) = 0 := rfl @[simp] theorem encode_some [encodable α] (a : α) : encode (some a) = succ (encode a) := rfl @[simp] theorem decode_option_zero [encodable α] : decode (option α) 0 = some none := rfl @[simp] theorem decode_option_succ [encodable α] (n) : decode (option α) (succ n) = (decode α n).map some := rfl /-- Failsafe variant of `decode`. `decode₂ α n` returns the preimage of `n` under `encode` if it exists, and returns `none` if it doesn't. This requirement could be imposed directly on `decode` but is not to help make the definition easier to use. -/ def decode₂ (α) [encodable α] (n : ℕ) : option α := (decode α n).bind (option.guard (λ a, encode a = n)) theorem mem_decode₂' [encodable α] {n : ℕ} {a : α} : a ∈ decode₂ α n ↔ a ∈ decode α n ∧ encode a = n := by simp [decode₂]; exact ⟨λ ⟨_, h₁, rfl, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨_, h₁, rfl, h₂⟩⟩ theorem mem_decode₂ [encodable α] {n : ℕ} {a : α} : a ∈ decode₂ α n ↔ encode a = n := mem_decode₂'.trans (and_iff_right_of_imp $ λ e, e ▸ encodek _) theorem decode₂_ne_none_iff [encodable α] {n : ℕ} : decode₂ α n ≠ none ↔ n ∈ set.range (encode : α → ℕ) := by simp_rw [set.range, set.mem_set_of_eq, ne.def, option.eq_none_iff_forall_not_mem, encodable.mem_decode₂, not_forall, not_not] theorem decode₂_is_partial_inv [encodable α] : is_partial_inv encode (decode₂ α) := λ a n, mem_decode₂ theorem decode₂_inj [encodable α] {n : ℕ} {a₁ a₂ : α} (h₁ : a₁ ∈ decode₂ α n) (h₂ : a₂ ∈ decode₂ α n) : a₁ = a₂ := encode_injective $ (mem_decode₂.1 h₁).trans (mem_decode₂.1 h₂).symm theorem encodek₂ [encodable α] (a : α) : decode₂ α (encode a) = some a := mem_decode₂.2 rfl /-- The encoding function has decidable range. -/ def decidable_range_encode (α : Type*) [encodable α] : decidable_pred (∈ set.range (@encode α _)) := λ x, decidable_of_iff (option.is_some (decode₂ α x)) ⟨λ h, ⟨option.get h, by rw [← decode₂_is_partial_inv (option.get h), option.some_get]⟩, λ ⟨n, hn⟩, by rw [← hn, encodek₂]; exact rfl⟩ /-- An encodable type is equivalent to the range of its encoding function. -/ def equiv_range_encode (α : Type*) [encodable α] : α ≃ set.range (@encode α _) := { to_fun := λ a : α, ⟨encode a, set.mem_range_self _⟩, inv_fun := λ n, option.get (show is_some (decode₂ α n.1), by cases n.2 with x hx; rw [← hx, encodek₂]; exact rfl), left_inv := λ a, by dsimp; rw [← option.some_inj, option.some_get, encodek₂], right_inv := λ ⟨n, x, hx⟩, begin apply subtype.eq, dsimp, conv {to_rhs, rw ← hx}, rw [encode_injective.eq_iff, ← option.some_inj, option.some_get, ← hx, encodek₂], end } section sum variables [encodable α] [encodable β] /-- Explicit encoding function for the sum of two encodable types. -/ def encode_sum : α ⊕ β → ℕ | (sum.inl a) := bit0 $ encode a | (sum.inr b) := bit1 $ encode b /-- Explicit decoding function for the sum of two encodable types. -/ def decode_sum (n : ℕ) : option (α ⊕ β) := match bodd_div2 n with | (ff, m) := (decode α m).map sum.inl | (tt, m) := (decode β m).map sum.inr end /-- If `α` and `β` are encodable, then so is their sum. -/ instance sum : encodable (α ⊕ β) := ⟨encode_sum, decode_sum, λ s, by cases s; simp [encode_sum, decode_sum, encodek]; refl⟩ @[simp] theorem encode_inl (a : α) : @encode (α ⊕ β) _ (sum.inl a) = bit0 (encode a) := rfl @[simp] theorem encode_inr (b : β) : @encode (α ⊕ β) _ (sum.inr b) = bit1 (encode b) := rfl @[simp] theorem decode_sum_val (n : ℕ) : decode (α ⊕ β) n = decode_sum n := rfl end sum instance bool : encodable bool := of_equiv (unit ⊕ unit) equiv.bool_equiv_punit_sum_punit @[simp] theorem encode_tt : encode tt = 1 := rfl @[simp] theorem encode_ff : encode ff = 0 := rfl @[simp] theorem decode_zero : decode bool 0 = some ff := rfl @[simp] theorem decode_one : decode bool 1 = some tt := rfl theorem decode_ge_two (n) (h : 2 ≤ n) : decode bool n = none := begin suffices : decode_sum n = none, { change (decode_sum n).map _ = none, rw this, refl }, have : 1 ≤ div2 n, { rw [div2_val, nat.le_div_iff_mul_le], exacts [h, dec_trivial] }, cases exists_eq_succ_of_ne_zero (ne_of_gt this) with m e, simp [decode_sum]; cases bodd n; simp [decode_sum]; rw e; refl end section sigma variables {γ : α → Type*} [encodable α] [∀ a, encodable (γ a)] /-- Explicit encoding function for `sigma γ` -/ def encode_sigma : sigma γ → ℕ | ⟨a, b⟩ := mkpair (encode a) (encode b) /-- Explicit decoding function for `sigma γ` -/ def decode_sigma (n : ℕ) : option (sigma γ) := let (n₁, n₂) := unpair n in (decode α n₁).bind $ λ a, (decode (γ a) n₂).map $ sigma.mk a instance sigma : encodable (sigma γ) := ⟨encode_sigma, decode_sigma, λ ⟨a, b⟩, by simp [encode_sigma, decode_sigma, unpair_mkpair, encodek]⟩ @[simp] theorem decode_sigma_val (n : ℕ) : decode (sigma γ) n = (decode α n.unpair.1).bind (λ a, (decode (γ a) n.unpair.2).map $ sigma.mk a) := show decode_sigma._match_1 _ = _, by cases n.unpair; refl @[simp] theorem encode_sigma_val (a b) : @encode (sigma γ) _ ⟨a, b⟩ = mkpair (encode a) (encode b) := rfl end sigma section prod variables [encodable α] [encodable β] /-- If `α` and `β` are encodable, then so is their product. -/ instance prod : encodable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem decode_prod_val (n : ℕ) : decode (α × β) n = (decode α n.unpair.1).bind (λ a, (decode β n.unpair.2).map $ prod.mk a) := show (decode (sigma (λ _, β)) n).map (equiv.sigma_equiv_prod α β) = _, by simp; cases decode α n.unpair.1; simp; cases decode β n.unpair.2; refl @[simp] theorem encode_prod_val (a b) : @encode (α × β) _ (a, b) = mkpair (encode a) (encode b) := rfl end prod section subtype open subtype decidable variables {P : α → Prop} [encA : encodable α] [decP : decidable_pred P] include encA /-- Explicit encoding function for a decidable subtype of an encodable type -/ def encode_subtype : {a : α // P a} → ℕ | ⟨v, h⟩ := encode v include decP /-- Explicit decoding function for a decidable subtype of an encodable type -/ def decode_subtype (v : ℕ) : option {a : α // P a} := (decode α v).bind $ λ a, if h : P a then some ⟨a, h⟩ else none /-- A decidable subtype of an encodable type is encodable. -/ instance subtype : encodable {a : α // P a} := ⟨encode_subtype, decode_subtype, λ ⟨v, h⟩, by simp [encode_subtype, decode_subtype, encodek, h]⟩ lemma subtype.encode_eq (a : subtype P) : encode a = encode a.val := by cases a; refl end subtype instance fin (n) : encodable (fin n) := of_equiv _ (equiv.fin_equiv_subtype _) instance int : encodable ℤ := of_equiv _ equiv.int_equiv_nat instance pnat : encodable ℕ+ := of_equiv _ equiv.pnat_equiv_nat /-- The lift of an encodable type is encodable. -/ instance ulift [encodable α] : encodable (ulift α) := of_equiv _ equiv.ulift /-- The lift of an encodable type is encodable. -/ instance plift [encodable α] : encodable (plift α) := of_equiv _ equiv.plift /-- If `β` is encodable and there is an injection `f : α → β`, then `α` is encodable as well. -/ noncomputable def of_inj [encodable β] (f : α → β) (hf : injective f) : encodable α := of_left_injection f (partial_inv f) (λ x, (partial_inv_of_injective hf _ _).2 rfl) end encodable section ulower local attribute [instance, priority 100] encodable.decidable_range_encode /-- `ulower α : Type` is an equivalent type in the lowest universe, given `encodable α`. -/ @[derive decidable_eq, derive encodable] def ulower (α : Type*) [encodable α] : Type := set.range (encodable.encode : α → ℕ) end ulower namespace ulower variables (α : Type*) [encodable α] /-- The equivalence between the encodable type `α` and `ulower α : Type`. -/ def equiv : α ≃ ulower α := encodable.equiv_range_encode α variables {α} /-- Lowers an `a : α` into `ulower α`. -/ def down (a : α) : ulower α := equiv α a instance [inhabited α] : inhabited (ulower α) := ⟨down (default _)⟩ /-- Lifts an `a : ulower α` into `α`. -/ def up (a : ulower α) : α := (equiv α).symm a @[simp] lemma down_up {a : ulower α} : down a.up = a := equiv.right_inv _ _ @[simp] lemma up_down {a : α} : (down a).up = a := equiv.left_inv _ _ @[simp] lemma up_eq_up {a b : ulower α} : a.up = b.up ↔ a = b := equiv.apply_eq_iff_eq _ @[simp] lemma down_eq_down {a b : α} : down a = down b ↔ a = b := equiv.apply_eq_iff_eq _ @[ext] protected lemma ext {a b : ulower α} : a.up = b.up → a = b := up_eq_up.1 end ulower /- Choice function for encodable types and decidable predicates. We provide the following API choose {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] : (∃ x, p x) → α := choose_spec {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] (ex : ∃ x, p x) : p (choose ex) := -/ namespace encodable section find_a variables {α : Type*} (p : α → Prop) [encodable α] [decidable_pred p] private def good : option α → Prop | (some a) := p a | none := false private def decidable_good : decidable_pred (good p) | n := by cases n; unfold good; apply_instance local attribute [instance] decidable_good open encodable variable {p} /-- Constructive choice function for a decidable subtype of an encodable type. -/ def choose_x (h : ∃ x, p x) : {a : α // p a} := have ∃ n, good p (decode α n), from let ⟨w, pw⟩ := h in ⟨encode w, by simp [good, encodek, pw]⟩, match _, nat.find_spec this : ∀ o, good p o → {a // p a} with | some a, h := ⟨a, h⟩ end /-- Constructive choice function for a decidable predicate over an encodable type. -/ def choose (h : ∃ x, p x) : α := (choose_x h).1 lemma choose_spec (h : ∃ x, p x) : p (choose h) := (choose_x h).2 end find_a theorem axiom_of_choice {α : Type*} {β : α → Type*} {R : Π x, β x → Prop} [Π a, encodable (β a)] [∀ x y, decidable (R x y)] (H : ∀ x, ∃ y, R x y) : ∃ f : Π a, β a, ∀ x, R x (f x) := ⟨λ x, choose (H x), λ x, choose_spec (H x)⟩ theorem skolem {α : Type*} {β : α → Type*} {P : Π x, β x → Prop} [c : Π a, encodable (β a)] [d : ∀ x y, decidable (P x y)] : (∀ x, ∃ y, P x y) ↔ ∃ f : Π a, β a, (∀ x, P x (f x)) := ⟨axiom_of_choice, λ ⟨f, H⟩ x, ⟨_, H x⟩⟩ /- There is a total ordering on the elements of an encodable type, induced by the map to ℕ. -/ /-- The `encode` function, viewed as an embedding. -/ def encode' (α) [encodable α] : α ↪ ℕ := ⟨encodable.encode, encodable.encode_injective⟩ instance {α} [encodable α] : is_trans _ (encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_trans instance {α} [encodable α] : is_antisymm _ (encodable.encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_antisymm instance {α} [encodable α] : is_total _ (encodable.encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_total end encodable namespace directed open encodable variables {α : Type*} {β : Type*} [encodable α] [inhabited α] /-- Given a `directed r` function `f : α → β` defined on an encodable inhabited type, construct a noncomputable sequence such that `r (f (x n)) (f (x (n + 1)))` and `r (f a) (f (x (encode a + 1))`. -/ protected noncomputable def sequence {r : β → β → Prop} (f : α → β) (hf : directed r f) : ℕ → α | 0 := default α | (n + 1) := let p := sequence n in match decode α n with | none := classical.some (hf p p) | (some a) := classical.some (hf p a) end lemma sequence_mono_nat {r : β → β → Prop} {f : α → β} (hf : directed r f) (n : ℕ) : r (f (hf.sequence f n)) (f (hf.sequence f (n+1))) := begin dsimp [directed.sequence], generalize eq : hf.sequence f n = p, cases h : decode α n with a, { exact (classical.some_spec (hf p p)).1 }, { exact (classical.some_spec (hf p a)).1 } end lemma rel_sequence {r : β → β → Prop} {f : α → β} (hf : directed r f) (a : α) : r (f a) (f (hf.sequence f (encode a + 1))) := begin simp only [directed.sequence, encodek], exact (classical.some_spec (hf _ a)).2 end variables [preorder β] {f : α → β} (hf : directed (≤) f) lemma sequence_mono : monotone (f ∘ (hf.sequence f)) := monotone_nat_of_le_succ $ hf.sequence_mono_nat lemma le_sequence (a : α) : f a ≤ f (hf.sequence f (encode a + 1)) := hf.rel_sequence a end directed section quotient open encodable quotient variables {α : Type*} {s : setoid α} [@decidable_rel α (≈)] [encodable α] /-- Representative of an equivalence class. This is a computable version of `quot.out` for a setoid on an encodable type. -/ def quotient.rep (q : quotient s) : α := choose (exists_rep q) theorem quotient.rep_spec (q : quotient s) : ⟦q.rep⟧ = q := choose_spec (exists_rep q) /-- The quotient of an encodable space by a decidable equivalence relation is encodable. -/ def encodable_quotient : encodable (quotient s) := ⟨λ q, encode q.rep, λ n, quotient.mk <$> decode α n, by rintros ⟨l⟩; rw encodek; exact congr_arg some ⟦l⟧.rep_spec⟩ end quotient
ce2bf664260509482c1f89b10ce2635228ee1ae7
66a6486e19b71391cc438afee5f081a4257564ec
/homotopy/degree.hlean
7a69dbedc13c02e0d7a4eaffa9aad2b43fefbe26
[ "Apache-2.0" ]
permissive
spiceghello/Spectral
c8ccd1e32d4b6a9132ccee20fcba44b477cd0331
20023aa3de27c22ab9f9b4a177f5a1efdec2b19f
refs/heads/master
1,611,263,374,078
1,523,349,717,000
1,523,349,717,000
92,312,239
0
0
null
1,495,642,470,000
1,495,642,470,000
null
UTF-8
Lean
false
false
4,984
hlean
import homotopy.sphere2 ..move_to_lib open fin eq equiv group algebra sphere.ops pointed nat int trunc is_equiv function circle protected definition nat.eq_one_of_mul_eq_one {n : ℕ} (m : ℕ) (q : n * m = 1) : n = 1 := begin cases n with n, { exact empty.elim (succ_ne_zero 0 ((nat.zero_mul m)⁻¹ ⬝ q)⁻¹) }, { cases n with n, { reflexivity }, { apply empty.elim, cases m with m, { exact succ_ne_zero 0 q⁻¹ }, { apply nat.lt_irrefl 1, exact (calc 1 ≤ (m + 1) : succ_le_succ (nat.zero_le m) ... = 1 * (m + 1) : (nat.one_mul (m + 1))⁻¹ ... < (n + 2) * (m + 1) : nat.mul_lt_mul_of_pos_right (succ_le_succ (succ_le_succ (nat.zero_le n))) (zero_lt_succ m) ... = 1 : q) } } } end definition cases_of_nat_abs_eq {z : ℤ} (n : ℕ) (p : nat_abs z = n) : (z = of_nat n) ⊎ (z = - of_nat n) := begin cases p, apply by_cases_of_nat z, { intro n, apply sum.inl, reflexivity }, { intro n, apply sum.inr, exact ap int.neg (ap of_nat (nat_abs_neg n))⁻¹ } end definition eq_one_or_eq_neg_one_of_mul_eq_one {n : ℤ} (m : ℤ) (p : n * m = 1) : n = 1 ⊎ n = -1 := cases_of_nat_abs_eq 1 (nat.eq_one_of_mul_eq_one (nat_abs m) ((int.nat_abs_mul n m)⁻¹ ⬝ ap int.nat_abs p)) definition endomorphism_int_unbundled (f : ℤ → ℤ) [is_add_hom f] (n : ℤ) : f n = f 1 * n := begin induction n using rec_nat_on with n IH n IH, { refine respect_zero f ⬝ _, exact !mul_zero⁻¹ }, { refine respect_add f n 1 ⬝ _, rewrite IH, rewrite [↑int.succ, left_distrib], apply ap (λx, _ + x), exact !mul_one⁻¹}, { rewrite [neg_nat_succ], refine respect_add f (-n) (- 1) ⬝ _, rewrite [IH, ↑int.pred, mul_sub_left_distrib], apply ap (λx, _ + x), refine _ ⬝ ap neg !mul_one⁻¹, exact respect_neg f 1 } end namespace sphere /- TODO: define for unbased maps, define for S 0, clear sorry s prove stable under suspension -/ attribute fundamental_group_of_circle fg_carrier_equiv_int [constructor] attribute untrunc_of_is_trunc [unfold 4] definition surf_eq_loop : @surf 1 = circle.loop := sorry -- definition π2S2_surf : π2S2 (tr surf) = 1 :> ℤ := -- begin -- unfold [π2S2, chain_complex.LES_of_homotopy_groups], -- end -- check (pmap.to_fun -- (chain_complex.cc_to_fn -- (chain_complex.LES_of_homotopy_groups -- hopf.complex_phopf) -- (pair 1 2)) -- (tr surf)) -- eval (pmap.to_fun -- (chain_complex.cc_to_fn -- (chain_complex.LES_of_homotopy_groups -- hopf.complex_phopf) -- (pair 1 2)) -- (tr surf)) definition πnSn_surf (n : ℕ) : πnSn (n+1) (tr surf) = 1 :> ℤ := begin cases n with n IH, { refine ap (πnSn _ ∘ tr) surf_eq_loop ⬝ _, apply transport_code_loop }, { unfold [πnSn], exact sorry} end definition deg {n : ℕ} [H : is_succ n] (f : S n →* S n) : ℤ := by induction H with n; exact πnSn (n+1) (π→g[n+1] f (tr surf)) definition deg_id (n : ℕ) [H : is_succ n] : deg (pid (S n)) = (1 : ℤ) := by induction H with n; exact ap (πnSn (n+1)) (homotopy_group_functor_pid (succ n) (S (succ n)) (tr surf)) ⬝ πnSn_surf n definition deg_phomotopy {n : ℕ} [H : is_succ n] {f g : S n →* S n} (p : f ~* g) : deg f = deg g := begin induction H with n, exact ap (πnSn (n+1)) (homotopy_group_functor_phomotopy (succ n) p (tr surf)), end definition endomorphism_int (f : gℤ →g gℤ) (n : ℤ) : f n = f (1 : ℤ) *[ℤ] n := @endomorphism_int_unbundled f (homomorphism.addstruct f) n definition endomorphism_equiv_Z {X : Group} (e : X ≃g gℤ) {one : X} (p : e one = 1 :> ℤ) (φ : X →g X) (x : X) : e (φ x) = e (φ one) *[ℤ] e x := begin revert x, refine equiv_rect' (equiv_of_isomorphism e) _ _, intro n, refine endomorphism_int (e ∘g φ ∘g e⁻¹ᵍ) n ⬝ _, refine ap011 (@mul ℤ _) _ _, { esimp, apply ap (e ∘ φ), refine ap e⁻¹ᵍ p⁻¹ ⬝ _, exact to_left_inv (equiv_of_isomorphism e) one }, { symmetry, exact to_right_inv (equiv_of_isomorphism e) n} end definition deg_compose {n : ℕ} [H : is_succ n] (f g : S n →* S n) : deg (g ∘* f) = deg g *[ℤ] deg f := begin induction H with n, refine ap (πnSn (n+1)) (homotopy_group_functor_compose (succ n) g f (tr surf)) ⬝ _, apply endomorphism_equiv_Z !πnSn !πnSn_surf (π→g[n+1] g) end definition deg_equiv {n : ℕ} [H : is_succ n] (f : S n ≃* S n) : deg f = 1 ⊎ deg f = -1 := begin induction H with n, apply eq_one_or_eq_neg_one_of_mul_eq_one (deg f⁻¹ᵉ*), refine !deg_compose⁻¹ ⬝ _, refine deg_phomotopy (pright_inv f) ⬝ _, apply deg_id end end sphere
a90a20aab7d6aae0870e746310186826cb06afb5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/algebra/subalgebra.lean
95b39c3531f48f4419811e7ed95fe86d197d2ff7
[ "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
30,285
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.operations /-! # Subalgebras over Commutative Semiring In this file we define `subalgebra`s and the usual operations on them (`map`, `comap'`). More lemmas about `adjoin` can be found in `ring_theory.adjoin`. -/ universes u v w open_locale tensor_product big_operators set_option old_structure_cmd true /-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/ structure subalgebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v := (algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier) (zero_mem' := (algebra_map R A).map_zero ▸ algebra_map_mem' 0) (one_mem' := (algebra_map R A).map_one ▸ algebra_map_mem' 1) /-- Reinterpret a `subalgebra` as a `subsemiring`. -/ add_decl_doc subalgebra.to_subsemiring namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] include R instance : set_like (subalgebra R A) A := ⟨subalgebra.carrier, λ p q h, by cases p; cases q; congr'⟩ @[simp] lemma mem_carrier {s : subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h @[simp] lemma mem_to_subsemiring {S : subalgebra R A} {x} : x ∈ S.to_subsemiring ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_subsemiring (S : subalgebra R A) : (↑S.to_subsemiring : set A) = S := rfl /-- Copy of a subalgebra with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) : subalgebra R A := { carrier := s, add_mem' := hs.symm ▸ S.add_mem', mul_mem' := hs.symm ▸ S.mul_mem', algebra_map_mem' := hs.symm ▸ S.algebra_map_mem' } variables (S : subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≤ S.to_subsemiring := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) ⊆ S := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≤ S := S.range_subset theorem one_mem : (1 : A) ∈ S := S.to_subsemiring.one_mem theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := S.to_subsemiring.mul_mem hx hy theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := (algebra.smul_def r x).symm ▸ S.mul_mem (S.algebra_map_mem r) hx theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := S.to_subsemiring.pow_mem hx n theorem zero_mem : (0 : A) ∈ S := S.to_subsemiring.zero_mem theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := S.to_subsemiring.add_mem hx hy theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_one_smul R x ▸ S.smul_mem hx _ theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := by simpa only [sub_eq_add_neg] using S.add_mem hx (S.neg_mem hy) theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n • x ∈ S := S.to_subsemiring.nsmul_mem hx n theorem gsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : ∀ (n : ℤ), n • x ∈ S | (n : ℕ) := by { rw [gsmul_coe_nat], exact S.nsmul_mem hx n } | -[1+ n] := by { rw [gsmul_neg_succ_of_nat], exact S.neg_mem (S.nsmul_mem hx _) } theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S := S.to_subsemiring.coe_nat_mem n theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S := int.cases_on n (λ i, S.coe_nat_mem i) (λ i, S.neg_mem $ S.coe_nat_mem $ i + 1) theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := S.to_subsemiring.list_prod_mem h theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := S.to_subsemiring.list_sum_mem h theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S := S.to_subsemiring.multiset_prod_mem m h theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S := S.to_subsemiring.multiset_sum_mem m h theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S := S.to_subsemiring.prod_mem h theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∑ x in t, f x ∈ S := S.to_subsemiring.sum_mem h instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_add_submonoid (S : set A) := { zero_mem := S.zero_mem, add_mem := λ _ _, S.add_mem } instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_submonoid (S : set A) := { one_mem := S.one_mem, mul_mem := λ _ _, S.mul_mem } /-- A subalgebra over a ring is also a `subring`. -/ def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : subring A := { neg_mem' := λ _, S.neg_mem, .. S.to_subsemiring } @[simp] lemma mem_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} {x} : x ∈ S.to_subring ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : (↑S.to_subring : set A) = S := rfl instance {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S : set A) := { neg_mem := λ _, S.neg_mem } instance : inhabited S := ⟨(0 : S.to_subsemiring)⟩ section /-! `subalgebra`s inherit structure from their `subsemiring` / `semiring` coercions. -/ instance to_semiring {R A} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : semiring S := S.to_subsemiring.to_semiring instance to_comm_semiring {R A} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : comm_semiring S := S.to_subsemiring.to_comm_semiring instance to_ring {R A} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : ring S := S.to_subring.to_ring instance to_comm_ring {R A} [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := S.to_subring.to_comm_ring instance to_ordered_semiring {R A} [comm_semiring R] [ordered_semiring A] [algebra R A] (S : subalgebra R A) : ordered_semiring S := S.to_subsemiring.to_ordered_semiring instance to_ordered_comm_semiring {R A} [comm_semiring R] [ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) : ordered_comm_semiring S := S.to_subsemiring.to_ordered_comm_semiring instance to_ordered_ring {R A} [comm_ring R] [ordered_ring A] [algebra R A] (S : subalgebra R A) : ordered_ring S := S.to_subring.to_ordered_ring instance to_ordered_comm_ring {R A} [comm_ring R] [ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : ordered_comm_ring S := S.to_subring.to_ordered_comm_ring instance to_linear_ordered_semiring {R A} [comm_semiring R] [linear_ordered_semiring A] [algebra R A] (S : subalgebra R A) : linear_ordered_semiring S := S.to_subsemiring.to_linear_ordered_semiring /-! There is no `linear_ordered_comm_semiring`. -/ instance to_linear_ordered_ring {R A} [comm_ring R] [linear_ordered_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_ring S := S.to_subring.to_linear_ordered_ring instance to_linear_ordered_comm_ring {R A} [comm_ring R] [linear_ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_comm_ring S := S.to_subring.to_linear_ordered_comm_ring end instance algebra : algebra R S := { smul := λ (c:R) x, ⟨c • x.1, S.smul_mem x.2 c⟩, commutes' := λ c x, subtype.eq $ algebra.commutes _ _, smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _, .. (algebra_map R A).cod_srestrict S.to_subsemiring $ λ x, S.range_le ⟨x, rfl⟩ } instance to_algebra {R A B : Type*} [comm_semiring R] [comm_semiring A] [semiring B] [algebra R A] [algebra A B] (A₀ : subalgebra R A) : algebra A₀ B := algebra.of_subsemiring A₀.to_subsemiring instance nontrivial [nontrivial A] : nontrivial S := S.to_subsemiring.nontrivial instance no_zero_smul_divisors_bot [no_zero_smul_divisors R A] : no_zero_smul_divisors R S := ⟨λ c x h, have c = 0 ∨ (x : A) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ @[simp, norm_cast] lemma coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : S) : A) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : S) : A) = 1 := rfl @[simp, norm_cast] lemma coe_neg {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl @[simp, norm_cast] lemma coe_sub {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl @[simp, norm_cast] lemma coe_smul (r : R) (x : S) : (↑(r • x) : A) = r • ↑x := rfl @[simp, norm_cast] lemma coe_algebra_map (r : R) : ↑(algebra_map R S r) = algebra_map R A r := rfl @[simp, norm_cast] lemma coe_pow (x : S) (n : ℕ) : (↑(x^n) : A) = (↑x)^n := begin induction n with n ih, { simp, }, { simp [pow_succ, ih], }, end @[simp, norm_cast] lemma coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := (subtype.ext_iff.symm : (x : A) = (0 : S) ↔ x = 0) @[simp, norm_cast] lemma coe_eq_one {x : S} : (x : A) = 1 ↔ x = 1 := (subtype.ext_iff.symm : (x : A) = (1 : S) ↔ x = 1) -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := by refine_struct { to_fun := (coe : S → A) }; intros; refl @[simp] lemma coe_val : (S.val : S → A) = coe := rfl lemma val_apply (x : S) : S.val x = (x : A) := rfl /-- Convert a `subalgebra` to `submodule` -/ def to_submodule : submodule R A := { carrier := S, zero_mem' := (0:S).2, add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance to_submodule.is_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S.to_submodule : set A) := S.is_subring @[simp] lemma mem_to_submodule {x} : x ∈ S.to_submodule ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_submodule (S : subalgebra R A) : (↑S.to_submodule : set A) = S := rfl theorem to_submodule_injective : function.injective (to_submodule : subalgebra R A → submodule R A) := λ S T h, ext $ λ x, by rw [← mem_to_submodule, ← mem_to_submodule, h] theorem to_submodule_inj {S U : subalgebra R A} : S.to_submodule = U.to_submodule ↔ S = U := to_submodule_injective.eq_iff /-- As submodules, subalgebras are idempotent. -/ @[simp] theorem mul_self : S.to_submodule * S.to_submodule = S.to_submodule := begin apply le_antisymm, { rw submodule.mul_le, intros y hy z hz, exact mul_mem S hy hz }, { intros x hx1, rw ← mul_one x, exact submodule.mul_mem_mul hx1 (one_mem S) } end /-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal, we define it as a `linear_equiv` to avoid type equalities. -/ def to_submodule_equiv (S : subalgebra R A) : S.to_submodule ≃ₗ[R] S := linear_equiv.of_eq _ _ rfl /-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`, then `T` is an `R`-subalgebra of `A`. -/ def under {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { algebra_map_mem' := λ r, T.algebra_map_mem ⟨algebra_map R A r, S.algebra_map_mem r⟩, .. T } /-- Transport a subalgebra via an algebra homomorphism. -/ def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r), .. S.to_subsemiring.map (f : A →+* B) } lemma map_mono {S₁ S₂ : subalgebra R A} {f : A →ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := set.image_subset f lemma map_injective {S₁ S₂ : subalgebra R A} (f : A →ₐ[R] B) (hf : function.injective f) (ih : S₁.map f = S₂.map f) : S₁ = S₂ := ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ set_like.ext_iff.mp ih lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map S f ↔ ∃ x ∈ S, f x = y := subsemiring.mem_map /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap' (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A := { algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S, from (f.commutes r).symm ▸ S.algebra_map_mem r, .. S.to_subsemiring.comap (f : A →+* B) } theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} : map S f ≤ U ↔ S ≤ comap' U f := set.image_subset_iff @[simp] lemma mem_comap (S : subalgebra R B) (f : A →ₐ[R] B) (x : A) : x ∈ S.comap' f ↔ f x ∈ S := iff.rfl @[simp, norm_cast] lemma coe_comap (S : subalgebra R B) (f : A →ₐ[R] B) : (S.comap' f : set A) = f ⁻¹' (S : set B) := by { ext, simp, } instance no_zero_divisors {R A : Type*} [comm_ring R] [semiring A] [no_zero_divisors A] [algebra R A] (S : subalgebra R A) : no_zero_divisors S := S.to_subsemiring.no_zero_divisors instance no_zero_smul_divisors_top {R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] [no_zero_divisors A] (S : subalgebra R A) : no_zero_smul_divisors S A := ⟨λ c x h, have (c : A) = 0 ∨ x = 0, from eq_zero_or_eq_zero_of_mul_eq_zero h, this.imp_left (@subtype.ext_iff _ _ c 0).mpr⟩ instance integral_domain {R A : Type*} [comm_ring R] [integral_domain A] [algebra R A] (S : subalgebra R A) : integral_domain S := @subring.domain A _ S _ end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] variables (φ : A →ₐ[R] B) /-- Range of an `alg_hom` as a subalgebra. -/ protected def range (φ : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, ⟨algebra_map R A r, φ.commutes r⟩, .. φ.to_ring_hom.srange } @[simp] lemma mem_range (φ : A →ₐ[R] B) {y : B} : y ∈ φ.range ↔ ∃ x, φ x = y := ring_hom.mem_srange theorem mem_range_self (φ : A →ₐ[R] B) (x : A) : φ x ∈ φ.range := φ.mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (φ : A →ₐ[R] B) : (φ.range : set B) = set.range φ := by { ext, rw [set_like.mem_coe, mem_range], refl } /-- Restrict the codomain of an algebra homomorphism. -/ def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S := { commutes' := λ r, subtype.eq $ f.commutes r, .. ring_hom.cod_srestrict (f : A →+* B) S.to_subsemiring hf } @[simp] lemma val_comp_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : S.val.comp (f.cod_restrict S hf) = f := alg_hom.ext $ λ _, rfl @[simp] lemma coe_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) : ↑(f.cod_restrict S hf x) = f x := rfl theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : function.injective (f.cod_restrict S hf) ↔ function.injective f := ⟨λ H x y hxy, H $ subtype.eq hxy, λ H x y hxy, H (congr_arg subtype.val hxy : _)⟩ /-- Restrict the codomain of a alg_hom `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : A →ₐ[R] B) : A →ₐ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (ϕ ψ : A →ₐ[R] B) : subalgebra R A := { carrier := {a | ϕ a = ψ a}, add_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x + y) = ψ (x + y), rw [alg_hom.map_add, alg_hom.map_add, hx, hy] }, mul_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x * y) = ψ (x * y), rw [alg_hom.map_mul, alg_hom.map_mul, hx, hy] }, algebra_map_mem' := λ x, by { change ϕ (algebra_map R A x) = ψ (algebra_map R A x), rw [alg_hom.commutes, alg_hom.commutes] } } @[simp] lemma mem_equalizer (ϕ ψ : A →ₐ[R] B) (x : A) : x ∈ ϕ.equalizer ψ ↔ ϕ x = ψ x := iff.rfl end alg_hom namespace alg_equiv variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] /-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range. This is a computable alternative to `alg_equiv.of_injective`. -/ def of_left_inverse {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) : A ≃ₐ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.val, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := f.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : A) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Restrict an injective algebra homomorphism to an algebra isomorphism -/ noncomputable def of_injective (f : A →ₐ[R] B) (hf : function.injective f) : A ≃ₐ[R] f.range := of_left_inverse (classical.some_spec hf.has_left_inverse) @[simp] lemma of_injective_apply (f : A →ₐ[R] B) (hf : function.injective f) (x : A) : ↑(of_injective f hf x) = f x := rfl /-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/ noncomputable def of_injective_field {E F : Type*} [division_ring E] [semiring F] [nontrivial F] [algebra R E] [algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range := of_injective f f.to_ring_hom.injective end alg_equiv namespace algebra variables (R : Type u) {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : set A) : subalgebra R A := { algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩, .. subsemiring.closure (set.range (algebra_map R A) ∪ s) } variables {R} protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H, λ H, show subsemiring.closure (set.range (algebra_map R A) ∪ s) ≤ S.to_subsemiring, from subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, (adjoin R s).copy s $ le_antisymm (algebra.gc.le_u_l s) hs, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := λ _ _, set_like.coe_injective $ by { generalize_proofs h, exact h } } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi @[simp] lemma coe_top : (↑(⊤ : subalgebra R A) : set A) = set.univ := rfl @[simp] lemma mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := set.mem_univ x @[simp] lemma top_to_submodule : (⊤ : subalgebra R A).to_submodule = ⊤ := rfl @[simp] lemma top_to_subsemiring : (⊤ : subalgebra R A).to_subsemiring = ⊤ := rfl @[simp, norm_cast] lemma coe_inf (S T : subalgebra R A) : (↑(S ⊓ T) : set A) = S ∩ T := rfl @[simp] lemma mem_inf {S T : subalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl @[simp] lemma inf_to_submodule (S T : subalgebra R A) : (S ⊓ T).to_submodule = S.to_submodule ⊓ T.to_submodule := rfl @[simp] lemma inf_to_subsemiring (S T : subalgebra R A) : (S ⊓ T).to_subsemiring = S.to_subsemiring ⊓ T.to_subsemiring := rfl @[simp, norm_cast] lemma coe_Inf (S : set (subalgebra R A)) : (↑(Inf S) : set A) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subalgebra R A)} {x : A} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] lemma Inf_to_submodule (S : set (subalgebra R A)) : (Inf S).to_submodule = Inf (subalgebra.to_submodule '' S) := set_like.coe_injective $ by simp @[simp] lemma Inf_to_subsemiring (S : set (subalgebra R A)) : (Inf S).to_subsemiring = Inf (subalgebra.to_subsemiring '' S) := set_like.coe_injective $ by simp @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → subalgebra R A} : (↑(⨅ i, S i) : set A) = ⋂ i, S i := set.bInter_range lemma mem_infi {ι : Sort*} {S : ι → subalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp] lemma infi_to_submodule {ι : Sort*} (S : ι → subalgebra R A) : (⨅ i, S i).to_submodule = ⨅ i, (S i).to_submodule := set_like.coe_injective $ by simp instance : inhabited (subalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := suffices (of_id R A).range = (⊥ : subalgebra R A), by { rw [← this, ←set_like.mem_coe, alg_hom.coe_range], refl }, le_bot_iff.mp (λ x hx, subalgebra.range_le _ ((of_id R A).coe_range ▸ hx)) theorem to_submodule_bot : (⊥ : subalgebra R A).to_submodule = R ∙ 1 := by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] } @[simp] theorem coe_bot : ((⊥ : subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, algebra.mem_bot] theorem eq_top_iff {S : subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩ @[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊤ : subalgebra R A) f = f.range := subalgebra.ext $ λ x, ⟨λ ⟨y, _, hy⟩, ⟨y, hy⟩, λ ⟨y, hy⟩, ⟨y, algebra.mem_top, hy⟩⟩ @[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (⊥ : subalgebra R A) f = ⊥ := eq_bot_iff.2 $ λ x ⟨y, hy, hfy⟩, let ⟨r, hr⟩ := mem_bot.1 hy in subalgebra.range_le _ ⟨r, by rwa [← f.commutes, hr]⟩ @[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap' (⊤ : subalgebra R B) f = ⊤ := eq_top_iff.2 $ λ x, mem_top /-- `alg_hom` to `⊤ : subalgebra R A`. -/ def to_top : A →ₐ[R] (⊤ : subalgebra R A) := by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl theorem surjective_algebra_map_iff : function.surjective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, eq_bot_iff.2 $ λ y _, let ⟨x, hx⟩ := h y in hx ▸ subalgebra.algebra_map_mem _ _, λ h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩ theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A] [algebra R A] : function.bijective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, surjective_algebra_map_iff.1 h.2, λ h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) : (⊥ : subalgebra R A) ≃ₐ[R] R := alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _) ⟨λ x y hxy, h (congr_arg subtype.val hxy : _), λ ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] : (⊥ : subalgebra F R) ≃ₐ[F] F := bot_equiv_of_injective (ring_hom.injective _) /-- The top subalgebra is isomorphic to the field. -/ noncomputable def top_equiv : (⊤ : subalgebra R A) ≃ₐ[R] A := (alg_equiv.of_bijective to_top ⟨λ _ _, subtype.mk.inj, λ x, ⟨x.val, by { ext, refl }⟩⟩ : A ≃ₐ[R] (⊤ : subalgebra R A)).symm end algebra namespace subalgebra open algebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] variables (S : subalgebra R A) -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_subsingleton [subsingleton A] : subsingleton (subalgebra R A) := ⟨λ B C, ext (λ x, by { simp only [subsingleton.elim x 0, zero_mem] })⟩ /-- For performance reasons this is not an instance. If you need this instance, add ``` local attribute [instance] alg_hom.subsingleton subalgebra.subsingleton_of_subsingleton ``` in the section that needs it. -/ -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_hom.subsingleton [subsingleton (subalgebra R A)] : subsingleton (A →ₐ[R] B) := ⟨λ f g, alg_hom.ext $ λ a, have a ∈ (⊥ : subalgebra R A) := subsingleton.elim (⊤ : subalgebra R A) ⊥ ▸ mem_top, let ⟨x, hx⟩ := set.mem_range.mp (mem_bot.mp this) in hx ▸ (f.commutes _).trans (g.commutes _).symm⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_equiv.subsingleton_left [subsingleton (subalgebra R A)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (A →ₐ[R] B) := alg_hom.subsingleton, exact ⟨λ f g, alg_equiv.ext (λ x, alg_hom.ext_iff.mp (subsingleton.elim f.to_alg_hom g.to_alg_hom) x)⟩, end -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_equiv.subsingleton_right [subsingleton (subalgebra R B)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (B ≃ₐ[R] A) := alg_equiv.subsingleton_left, exact ⟨λ f g, eq.trans (alg_equiv.symm_symm _).symm (by rw [subsingleton.elim f.symm g.symm, alg_equiv.symm_symm])⟩ end lemma range_val : S.val.range = S := ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val instance : unique (subalgebra R R) := { uniq := begin intro S, refine le_antisymm (λ r hr, _) bot_le, simp only [set.mem_range, mem_bot, id.map_eq_self, exists_apply_eq_apply, default], end .. algebra.subalgebra.inhabited } /-- Two subalgebras that are equal are also equivalent as algebras. This is the `subalgebra` version of `linear_equiv.of_eq` and `equiv.set.of_eq`. -/ @[simps apply] def equiv_of_eq (S T : subalgebra R A) (h : S = T) : S ≃ₐ[R] T := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, map_mul' := λ _ _, rfl, commutes' := λ _, rfl, .. linear_equiv.of_eq _ _ (congr_arg to_submodule h) } @[simp] lemma equiv_of_eq_symm (S T : subalgebra R A) (h : S = T) : (equiv_of_eq S T h).symm = equiv_of_eq T S h.symm := rfl @[simp] lemma equiv_of_eq_rfl (S : subalgebra R A) : equiv_of_eq S S rfl = alg_equiv.refl := by { ext, refl } @[simp] lemma equiv_of_eq_trans (S T U : subalgebra R A) (hST : S = T) (hTU : T = U) : (equiv_of_eq S T hST).trans (equiv_of_eq T U hTU) = equiv_of_eq S U (trans hST hTU) := rfl section prod variables (S₁ : subalgebra R B) /-- The product of two subalgebras is a subalgebra. -/ def prod : subalgebra R (A × B) := { carrier := set.prod S S₁, algebra_map_mem' := λ r, ⟨algebra_map_mem _ _, algebra_map_mem _ _⟩, .. S.to_subsemiring.prod S₁.to_subsemiring } @[simp] lemma coe_prod : (prod S S₁ : set (A × B)) = set.prod S S₁ := rfl lemma prod_to_submodule : (S.prod S₁).to_submodule = S.to_submodule.prod S₁.to_submodule := rfl @[simp] lemma mem_prod {S : subalgebra R A} {S₁ : subalgebra R B} {x : A × B} : x ∈ prod S S₁ ↔ x.1 ∈ S ∧ x.2 ∈ S₁ := set.mem_prod @[simp] lemma prod_top : (prod ⊤ ⊤ : subalgebra R (A × B)) = ⊤ := by ext; simp lemma prod_mono {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} : S ≤ T → S₁ ≤ T₁ → prod S S₁ ≤ prod T T₁ := set.prod_mono @[simp] lemma prod_inf_prod {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} : S.prod S₁ ⊓ T.prod T₁ = (S ⊓ T).prod (S₁ ⊓ T₁) := set_like.coe_injective set.prod_inter_prod end prod end subalgebra section nat variables {R : Type*} [semiring R] /-- A subsemiring is a `ℕ`-subalgebra. -/ def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R := { algebra_map_mem' := λ i, S.coe_nat_mem i, .. S } @[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} : x ∈ subalgebra_of_subsemiring S ↔ x ∈ S := iff.rfl end nat section int variables {R : Type*} [ring R] /-- A subring is a `ℤ`-subalgebra. -/ def subalgebra_of_subring (S : subring R) : subalgebra ℤ R := { algebra_map_mem' := λ i, int.induction_on i S.zero_mem (λ i ih, S.add_mem ih S.one_mem) (λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one], exact S.sub_mem ih S.one_mem }), .. S } /-- A subset closed under the ring operations is a `ℤ`-subalgebra. -/ def subalgebra_of_is_subring (S : set R) [is_subring S] : subalgebra ℤ R := subalgebra_of_subring S.to_subring variables {S : Type*} [semiring S] @[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl @[simp] lemma mem_subalgebra_of_is_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_is_subring S ↔ x ∈ S := iff.rfl end int
46ceebf5c1f6b8e21c3bb2707b2da5c74798f9c0
6b2a480f27775cba4f3ae191b1c1387a29de586e
/group_rep1/Tools/to_sum.lean
fffc1d75f25e726311ff9dba32f3afc68a249999
[]
no_license
Or7ando/group_representation
a681de2e19d1930a1e1be573d6735a2f0b8356cb
9b576984f17764ebf26c8caa2a542d248f1b50d2
refs/heads/master
1,662,413,107,324
1,590,302,389,000
1,590,302,389,000
258,130,829
0
1
null
null
null
null
UTF-8
Lean
false
false
2,409
lean
import linear_algebra.basic import data.fintype.basic import algebra.big_operators import data.finset algebra.big_operators import linear_algebra.basic linear_algebra.finite_dimensional import algebra.module import data.equiv.basic universes u v w w' infix ` * ` := linear_map.comp ---- his there a standart notation ? open linear_map namespace TEST variables {G : Type u } [group G][fintype G] {R : Type v}[comm_ring R] {M : Type w} [add_comm_group M] [module R M] {M' : Type w'} [add_comm_group M'] [module R M'] (φ : G → (M →ₗ[R]M')) (f : M' →ₗ[R]M') (g : M →ₗ[R]M) variables (a : M →ₗ[R]M') (x : M) @[simp]lemma Sum_map (x: M) : f(finset.univ.sum (λ g, φ g x)) = finset.univ.sum (λ g, (f (φ g x))) := begin erw map_sum, --rw @map_sum R M' M' G _ _ _ _ _ f _ (λ g, φ g x), end @[simp]lemma Sum_apply (x : M) : (finset.sum finset.univ φ) x = (finset.sum finset.univ (λ g, φ g x)) := begin erw sum_apply, --finset.univ φ x, end @[simp]lemma Sum_comp_left : f * (finset.univ.sum φ) = finset.univ.sum (λ g, (f * (φ g ))) := begin ext,rw linear_map.comp_apply, rw Sum_apply, rw Sum_apply, rw Sum_map, exact rfl, end lemma Sum_comp_right : (finset.univ.sum φ) * g = finset.univ.sum (λ s, ((φ s) * g)) := begin ext, rw linear_map.comp_apply, erw sum_apply,erw sum_apply,exact rfl, end notation `Σ ` := finset.sum finset.univ #check (@finset.univ G) #check @map_sum R M' M' G _ _ _ _ _ f _ (λ g, φ g x) /- map_sum : ∀ {R : Type u_1} {M : Type u_2} {M₂ : Type u_3} {ι : Type u_4} [_inst_1 : ring R] [_inst_2 : add_comm_group M] [_inst_3 : add_comm_group M₂] [_inst_5 : module R M] [_inst_6 : module R M₂] (f : M →ₗ[R] M₂) {t : finset ι} {g : ι → M}, ⇑f (finset.sum t g) = finset.sum t (λ (i : ι), ⇑f (g i)) -/ end TEST namespace TEST2 def Sum {R : Type u}[add_comm_monoid R]{X : Type v}[fintype X](g : X → R) : R := finset.sum (finset.univ) g notation `Σ` := finset.sum (finset.univ) variables {G : Type u } [group G][fintype G] {R : Type v}[comm_ring R] {M : Type w} [add_comm_group M] [module R M] {M' : Type w'} [add_comm_group M'] [module R M'] (φ : G → (M →ₗ[R]M')) (f : M' →ₗ[R]M') lemma Sum_comp_left : f * (Σ φ) = Σ (λ g, (f * (φ g ))) := begin rw TEST.Sum_comp_left, end end TEST2
e57524fb2856dd1a4cce1104d5a36866e8e6c600
8e2026ac8a0660b5a490dfb895599fb445bb77a0
/library/init/meta/smt/interactive.lean
f5eaa9564088b80645450b9bc9e0fe1a12c41818
[ "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
10,191
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.smt.smt_tactic init.meta.interactive import init.meta.smt.rsimp namespace smt_tactic meta def save_info (p : pos) : smt_tactic unit := do (ss, ts) ← smt_tactic.read, tactic.save_info_thunk p (λ _, smt_state.to_format ss ts) meta def skip : smt_tactic unit := return () meta def solve_goals : smt_tactic unit := repeat close meta def step {α : Type} (tac : smt_tactic α) : smt_tactic unit := tac >> solve_goals meta def istep {α : Type} (line : nat) (col : nat) (tac : smt_tactic α) : smt_tactic unit := λ ss ts, (@scope_trace _ line col ((tac >> solve_goals) ss ts)).clamp_pos line col meta def execute (tac : smt_tactic unit) : tactic unit := using_smt tac meta def execute_with (cfg : smt_config) (tac : smt_tactic unit) : tactic unit := using_smt tac cfg namespace interactive open lean.parser open interactive open interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many meta def itactic : Type := smt_tactic unit meta def intros : parse ident* → smt_tactic unit | [] := smt_tactic.intros | hs := smt_tactic.intro_lst hs /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta def close : smt_tactic unit := smt_tactic.close /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. -/ meta def ematch : smt_tactic unit := smt_tactic.ematch meta def apply (q : parse texpr) : smt_tactic unit := tactic.interactive.apply q meta def fapply (q : parse texpr) : smt_tactic unit := tactic.interactive.fapply q meta def apply_instance : smt_tactic unit := tactic.apply_instance meta def change (q : parse texpr) : smt_tactic unit := tactic.interactive.change q meta def exact (q : parse texpr) : smt_tactic unit := tactic.interactive.exact q meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : smt_tactic unit := do e ← tactic.to_expr_strict q, smt_tactic.assert h e meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : smt_tactic unit := do e ← tactic.to_expr_strict q, smt_tactic.define h e meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : smt_tactic unit := do t ← tactic.to_expr_strict q₁, v ← tactic.to_expr_strict ``(%%q₂ : %%t), smt_tactic.assertv h t v meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : smt_tactic unit := do t ← tactic.to_expr_strict q₁, v ← tactic.to_expr_strict ``(%%q₂ : %%t), smt_tactic.definev h t v meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.note h p meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.pose h p meta def add_fact (q : parse texpr) : smt_tactic unit := do h ← tactic.get_unused_name `h none, p ← tactic.to_expr_strict q, smt_tactic.note h p meta def trace_state : smt_tactic unit := smt_tactic.trace_state meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def destruct (q : parse texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.destruct p meta def by_cases (q : parse texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.by_cases p meta def by_contradiction : smt_tactic unit := smt_tactic.by_contradiction meta def by_contra : smt_tactic unit := smt_tactic.by_contradiction open tactic (resolve_name transparency to_expr) private meta def report_invalid_em_lemma {α : Type} (n : name) : tactic α := fail ("invalid ematch lemma '" ++ to_string n ++ "'") private meta def add_lemma_name (md : transparency) (lhs_lemma : bool) (n : name) (ref : expr) : smt_tactic unit := do p ← resolve_name n, match p.to_raw_expr with | expr.const n _ := (add_ematch_lemma_from_decl_core md lhs_lemma n >> tactic.save_const_type_info n ref) <|> report_invalid_em_lemma n | _ := (do e ← to_expr p, add_ematch_lemma_core md lhs_lemma e >> try (tactic.save_type_info e ref)) <|> report_invalid_em_lemma n end private meta def add_lemma_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) : smt_tactic unit := let e := pexpr.to_raw_expr p in match e with | (expr.const c []) := add_lemma_name md lhs_lemma c e | (expr.local_const c _ _ _) := add_lemma_name md lhs_lemma c e | _ := do new_e ← to_expr p, add_ematch_lemma_core md lhs_lemma new_e end private meta def add_lemma_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → smt_tactic unit | [] := return () | (p::ps) := add_lemma_pexpr md lhs_lemma p >> add_lemma_pexprs ps meta def add_lemma (l : parse qexpr_list_or_texpr) : smt_tactic unit := add_lemma_pexprs reducible ff l meta def add_lhs_lemma (l : parse qexpr_list_or_texpr) : smt_tactic unit := add_lemma_pexprs reducible tt l private meta def add_eqn_lemmas_for_core (md : transparency) : list name → smt_tactic unit | [] := return () | (c::cs) := do p ← resolve_name c, match p.to_raw_expr with | expr.const n _ := add_ematch_eqn_lemmas_for_core md n >> add_eqn_lemmas_for_core cs | _ := fail $ "'" ++ to_string c ++ "' is not a constant" end meta def add_eqn_lemmas_for (ids : parse ident*) : smt_tactic unit := add_eqn_lemmas_for_core reducible ids meta def add_eqn_lemmas (ids : parse ident*) : smt_tactic unit := add_eqn_lemmas_for ids private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name) (hs : hinst_lemmas) (ref : expr) : smt_tactic hinst_lemmas := do p ← resolve_name n, match p.to_raw_expr with | expr.const n _ := (do h ← hinst_lemma.mk_from_decl_core md n lhs_lemma, tactic.save_const_type_info n ref, return $ hs.add h) <|> (do hs₁ ← mk_ematch_eqn_lemmas_for_core md n, tactic.save_const_type_info n ref, return $ hs.merge hs₁) <|> report_invalid_em_lemma n | _ := (do e ← to_expr p, h ← hinst_lemma.mk_core md e lhs_lemma, try (tactic.save_type_info e ref), return $ hs.add h) <|> report_invalid_em_lemma n end private meta def add_hinst_lemma_from_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas := let e := pexpr.to_raw_expr p in match e with | (expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs e | (expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs e | _ := do new_e ← to_expr p, h ← hinst_lemma.mk_core md new_e lhs_lemma, return $ hs.add h end private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → hinst_lemmas → smt_tactic hinst_lemmas | [] hs := return hs | (p::ps) hs := do hs₁ ← add_hinst_lemma_from_pexpr md lhs_lemma p hs, add_hinst_lemmas_from_pexprs ps hs₁ meta def ematch_using (l : parse qexpr_list_or_texpr) : smt_tactic unit := do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk, smt_tactic.ematch_using hs /-- Try the given tactic, and do nothing if it fails. -/ meta def try (t : itactic) : smt_tactic unit := smt_tactic.try t /-- Keep applying the given tactic until it fails. -/ meta def repeat (t : itactic) : smt_tactic unit := smt_tactic.repeat t /-- Apply the given tactic to all remaining goals. -/ meta def all_goals (t : itactic) : smt_tactic unit := smt_tactic.all_goals t meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : smt_tactic unit := slift (tactic.interactive.induction p rec_name ids) open tactic /-- Simplify the target type of the main goal. -/ meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit := tactic.interactive.simp hs attr_names ids [] cfg /-- Simplify the target type of the main goal using simplification lemmas and the current set of hypotheses. -/ meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit := tactic.interactive.simp_using_hs hs attr_names ids cfg meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : smt_tactic unit := simp_using_hs hs attr_names ids cfg meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : smt_tactic unit := tactic.interactive.dsimp es attr_names ids [] meta def rsimp : smt_tactic unit := do ccs ← to_cc_state, rsimp.rsimplify_goal ccs meta def add_simp_lemmas : smt_tactic unit := get_hinst_lemmas_for_attr `rsimp_attr >>= add_lemmas /-- Keep applying heuristic instantiation until the current goal is solved, or it fails. -/ meta def eblast : smt_tactic unit := smt_tactic.eblast /-- Keep applying heuristic instantiation using the given lemmas until the current goal is solved, or it fails. -/ meta def eblast_using (l : parse qexpr_list_or_texpr) : smt_tactic unit := do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk, smt_tactic.repeat (smt_tactic.ematch_using hs >> smt_tactic.try smt_tactic.close) meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : smt_tactic unit := do e ← to_expr p, guard (expr.alpha_eqv t e) meta def guard_target (p : parse texpr) : smt_tactic unit := do t ← target, guard_expr_eq t p end interactive end smt_tactic
3d27441ab9580986271fe74c70117a8eb36957e9
367134ba5a65885e863bdc4507601606690974c1
/src/measure_theory/haar_measure.lean
bd7f3b69d77ade163c712c97df3c29edd46633d6
[ "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
31,469
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 -/ import measure_theory.content import measure_theory.prod_group /-! # Haar measure In this file we prove the existence of Haar measure for a locally compact Hausdorff topological group. For the construction, we follow the write-up by Jonathan Gleason, *Existence and Uniqueness of Haar Measure*. This is essentially the same argument as in https://en.wikipedia.org/wiki/Haar_measure#A_construction_using_compact_subsets. We construct the Haar measure first on compact sets. For this we define `(K : U)` as the (smallest) number of left-translates of `U` are needed to cover `K` (`index` in the formalization). Then we define a function `h` on compact sets as `lim_U (K : U) / (K₀ : U)`, where `U` becomes a smaller and smaller open neighborhood of `1`, and `K₀` is a fixed compact set with nonempty interior. This function is `chaar` in the formalization, and we define the limit formally using Tychonoff's theorem. This function `h` forms a content, which we can extend to an outer measure `μ` (`haar_outer_measure`), and obtain the Haar measure from that (`haar_measure`). We normalize the Haar measure so that the measure of `K₀` is `1`. We show that for second countable spaces any left invariant Borel measure is a scalar multiple of the Haar measure. Note that `μ` need not coincide with `h` on compact sets, according to [halmos1950measure, ch. X, §53 p.233]. However, we know that `h(K)` lies between `μ(Kᵒ)` and `μ(K)`, where `ᵒ` denotes the interior. ## Main Declarations * `haar_measure`: the Haar measure on a locally compact Hausdorff group. This is a left invariant regular measure. It takes as argument a compact set of the group (with non-empty interior), and is normalized so that the measure of the given set is 1. * `haar_measure_self`: the Haar measure is normalized. * `is_left_invariant_haar_measure`: the Haar measure is left invariant. * `regular_haar_measure`: the Haar measure is a regular measure. ## References * Paul Halmos (1950), Measure Theory, §53 * Jonathan Gleason, Existence and Uniqueness of Haar Measure - Note: step 9, page 8 contains a mistake: the last defined `μ` does not extend the `μ` on compact sets, see Halmos (1950) p. 233, bottom of the page. This makes some other steps (like step 11) invalid. * https://en.wikipedia.org/wiki/Haar_measure -/ noncomputable theory open set has_inv function topological_space measurable_space open_locale nnreal classical ennreal variables {G : Type*} [group G] namespace measure_theory namespace measure /-! We put the internal functions in the construction of the Haar measure in a namespace, so that the chosen names don't clash with other declarations. We first define a couple of the functions before proving the properties (that require that `G` is a topological group). -/ namespace haar /-- The index or Haar covering number or ratio of `K` w.r.t. `V`, denoted `(K : V)`: it is the smallest number of (left) translates of `V` that is necessary to cover `K`. It is defined to be 0 if no finite number of translates cover `K`. -/ def index (K V : set G) : ℕ := Inf $ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V } lemma index_empty {V : set G} : index ∅ V = 0 := begin simp only [index, nat.Inf_eq_zero], left, use ∅, simp only [finset.card_empty, empty_subset, mem_set_of_eq, eq_self_iff_true, and_self], end variables [topological_space G] /-- `prehaar K₀ U K` is a weighted version of the index, defined as `(K : U)/(K₀ : U)`. In the applications `K₀` is compact with non-empty interior, `U` is open containing `1`, and `K` is any compact set. The argument `K` is a (bundled) compact set, so that we can consider `prehaar K₀ U` as an element of `haar_product` (below). -/ def prehaar (K₀ U : set G) (K : compacts G) : ℝ := (index K.1 U : ℝ) / index K₀ U lemma prehaar_empty (K₀ : positive_compacts G) {U : set G} : prehaar K₀.1 U ⊥ = 0 := by { simp only [prehaar, compacts.bot_val, index_empty, nat.cast_zero, zero_div] } lemma prehaar_nonneg (K₀ : positive_compacts G) {U : set G} (K : compacts G) : 0 ≤ prehaar K₀.1 U K := by apply div_nonneg; norm_cast; apply zero_le /-- `haar_product K₀` is the product of intervals `[0, (K : K₀)]`, for all compact sets `K`. For all `U`, we can show that `prehaar K₀ U ∈ haar_product K₀`. -/ def haar_product (K₀ : set G) : set (compacts G → ℝ) := pi univ (λ K, Icc 0 $ index K.1 K₀) @[simp] lemma mem_prehaar_empty {K₀ : set G} {f : compacts G → ℝ} : f ∈ haar_product K₀ ↔ ∀ K : compacts G, f K ∈ Icc (0 : ℝ) (index K.1 K₀) := by simp only [haar_product, pi, forall_prop_of_true, mem_univ, mem_set_of_eq] /-- The closure of the collection of elements of the form `prehaar K₀ U`, for `U` open neighbourhoods of `1`, contained in `V`. The closure is taken in the space `compacts G → ℝ`, with the topology of pointwise convergence. We show that the intersection of all these sets is nonempty, and the Haar measure on compact sets is defined to be an element in the closure of this intersection. -/ def cl_prehaar (K₀ : set G) (V : open_nhds_of (1 : G)) : set (compacts G → ℝ) := closure $ prehaar K₀ '' { U : set G | U ⊆ V.1 ∧ is_open U ∧ (1 : G) ∈ U } variables [topological_group G] /-! ### Lemmas about `index` -/ /-- If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties. -/ lemma index_defined {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ n : ℕ, n ∈ finset.card '' {t : finset G | K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V } := by { rcases compact_covered_by_mul_left_translates hK hV with ⟨t, ht⟩, exact ⟨t.card, t, ht, rfl⟩ } lemma index_elim {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ (t : finset G), K ⊆ (⋃ g ∈ t, (λ h, g * h) ⁻¹' V) ∧ finset.card t = index K V := by { have := nat.Inf_mem (index_defined hK hV), rwa [mem_image] at this } lemma le_index_mul (K₀ : positive_compacts G) (K : compacts G) {V : set G} (hV : (interior V).nonempty) : index K.1 V ≤ index K.1 K₀.1 * index K₀.1 V := begin rcases index_elim K.2 K₀.2.2 with ⟨s, h1s, h2s⟩, rcases index_elim K₀.2.1 hV with ⟨t, h1t, h2t⟩, rw [← h2s, ← h2t, mul_comm], refine le_trans _ finset.mul_card_le, apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], refine subset.trans h1s _, apply bUnion_subset, intros g₁ hg₁, rw preimage_subset_iff, intros g₂ hg₂, have := h1t hg₂, rcases this with ⟨_, ⟨g₃, rfl⟩, A, ⟨hg₃, rfl⟩, h2V⟩, rw [mem_preimage, ← mul_assoc] at h2V, exact mem_bUnion (finset.mul_mem_mul hg₃ hg₁) h2V end lemma index_pos (K : positive_compacts G) {V : set G} (hV : (interior V).nonempty) : 0 < index K.1 V := begin unfold index, rw [nat.Inf_def, nat.find_pos, mem_image], { rintro ⟨t, h1t, h2t⟩, rw [finset.card_eq_zero] at h2t, subst h2t, cases K.2.2 with g hg, show g ∈ (∅ : set G), convert h1t (interior_subset hg), symmetry, apply bUnion_empty }, { exact index_defined K.2.1 hV } end lemma index_mono {K K' V : set G} (hK' : is_compact K') (h : K ⊆ K') (hV : (interior V).nonempty) : index K V ≤ index K' V := begin rcases index_elim hK' hV with ⟨s, h1s, h2s⟩, apply nat.Inf_le, rw [mem_image], refine ⟨s, subset.trans h h1s, h2s⟩ end lemma index_union_le (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty) : index (K₁.1 ∪ K₂.1) V ≤ index K₁.1 V + index K₂.1 V := begin rcases index_elim K₁.2 hV with ⟨s, h1s, h2s⟩, rcases index_elim K₂.2 hV with ⟨t, h1t, h2t⟩, rw [← h2s, ← h2t], refine le_trans _ (finset.card_union_le _ _), apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], apply union_subset; refine subset.trans (by assumption) _; apply bUnion_subset_bUnion_left; intros g hg; simp only [mem_def] at hg; simp only [mem_def, multiset.mem_union, finset.union_val, hg, or_true, true_or] end lemma index_union_eq (K₁ K₂ : compacts G) {V : set G} (hV : (interior V).nonempty) (h : disjoint (K₁.1 * V⁻¹) (K₂.1 * V⁻¹)) : index (K₁.1 ∪ K₂.1) V = index K₁.1 V + index K₂.1 V := begin apply le_antisymm (index_union_le K₁ K₂ hV), rcases index_elim (K₁.2.union K₂.2) hV with ⟨s, h1s, h2s⟩, rw [← h2s], have : ∀ (K : set G) , K ⊆ (⋃ g ∈ s, (λ h, g * h) ⁻¹' V) → index K V ≤ (s.filter (λ g, ((λ (h : G), g * h) ⁻¹' V ∩ K).nonempty)).card, { intros K hK, apply nat.Inf_le, refine ⟨_, _, rfl⟩, rw [mem_set_of_eq], intros g hg, rcases hK hg with ⟨_, ⟨g₀, rfl⟩, _, ⟨h1g₀, rfl⟩, h2g₀⟩, simp only [mem_preimage] at h2g₀, simp only [mem_Union], use g₀, split, { simp only [finset.mem_filter, h1g₀, true_and], use g, simp only [hg, h2g₀, mem_inter_eq, mem_preimage, and_self] }, exact h2g₀ }, refine le_trans (add_le_add (this K₁.1 $ subset.trans (subset_union_left _ _) h1s) (this K₂.1 $ subset.trans (subset_union_right _ _) h1s)) _, rw [← finset.card_union_eq, finset.filter_union_right], exact s.card_filter_le _, apply finset.disjoint_filter.mpr, rintro g₁ h1g₁ ⟨g₂, h1g₂, h2g₂⟩ ⟨g₃, h1g₃, h2g₃⟩, simp only [mem_preimage] at h1g₃ h1g₂, apply @h g₁⁻¹, split; simp only [set.mem_inv, set.mem_mul, exists_exists_and_eq_and, exists_and_distrib_left], { refine ⟨_, h2g₂, (g₁ * g₂)⁻¹, _, _⟩, simp only [inv_inv, h1g₂], simp only [mul_inv_rev, mul_inv_cancel_left] }, { refine ⟨_, h2g₃, (g₁ * g₃)⁻¹, _, _⟩, simp only [inv_inv, h1g₃], simp only [mul_inv_rev, mul_inv_cancel_left] } end lemma mul_left_index_le {K : set G} (hK : is_compact K) {V : set G} (hV : (interior V).nonempty) (g : G) : index ((λ h, g * h) '' K) V ≤ index K V := begin rcases index_elim hK hV with ⟨s, h1s, h2s⟩, rw [← h2s], apply nat.Inf_le, rw [mem_image], refine ⟨s.map (equiv.mul_right g⁻¹).to_embedding, _, finset.card_map _⟩, { simp only [mem_set_of_eq], refine subset.trans (image_subset _ h1s) _, rintro _ ⟨g₁, ⟨_, ⟨g₂, rfl⟩, ⟨_, ⟨hg₂, rfl⟩, hg₁⟩⟩, rfl⟩, simp only [mem_preimage] at hg₁, simp only [exists_prop, mem_Union, finset.mem_map, equiv.coe_mul_right, exists_exists_and_eq_and, mem_preimage, equiv.to_embedding_apply], refine ⟨_, hg₂, _⟩, simp only [mul_assoc, hg₁, inv_mul_cancel_left] } end lemma is_left_invariant_index {K : set G} (hK : is_compact K) (g : G) {V : set G} (hV : (interior V).nonempty) : index ((λ h, g * h) '' K) V = index K V := begin refine le_antisymm (mul_left_index_le hK hV g) _, convert mul_left_index_le (hK.image $ continuous_mul_left g) hV g⁻¹, rw [image_image], symmetry, convert image_id' _, ext h, apply inv_mul_cancel_left end /-! ### Lemmas about `prehaar` -/ lemma prehaar_le_index (K₀ : positive_compacts G) {U : set G} (K : compacts G) (hU : (interior U).nonempty) : prehaar K₀.1 U K ≤ index K.1 K₀.1 := begin unfold prehaar, rw [div_le_iff]; norm_cast, { apply le_index_mul K₀ K hU }, { exact index_pos K₀ hU } end lemma prehaar_pos (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty) {K : set G} (h1K : is_compact K) (h2K : (interior K).nonempty) : 0 < prehaar K₀.1 U ⟨K, h1K⟩ := by { apply div_pos; norm_cast, apply index_pos ⟨K, h1K, h2K⟩ hU, exact index_pos K₀ hU } lemma prehaar_mono {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) {K₁ K₂ : compacts G} (h : K₁.1 ⊆ K₂.1) : prehaar K₀.1 U K₁ ≤ prehaar K₀.1 U K₂ := begin simp only [prehaar], rw [div_le_div_right], exact_mod_cast index_mono K₂.2 h hU, exact_mod_cast index_pos K₀ hU end lemma prehaar_self {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) : prehaar K₀.1 U ⟨K₀.1, K₀.2.1⟩ = 1 := by { simp only [prehaar], rw [div_self], apply ne_of_gt, exact_mod_cast index_pos K₀ hU } lemma prehaar_sup_le {K₀ : positive_compacts G} {U : set G} (K₁ K₂ : compacts G) (hU : (interior U).nonempty) : prehaar K₀.1 U (K₁ ⊔ K₂) ≤ prehaar K₀.1 U K₁ + prehaar K₀.1 U K₂ := begin simp only [prehaar], rw [div_add_div_same, div_le_div_right], exact_mod_cast index_union_le K₁ K₂ hU, exact_mod_cast index_pos K₀ hU end lemma prehaar_sup_eq {K₀ : positive_compacts G} {U : set G} {K₁ K₂ : compacts G} (hU : (interior U).nonempty) (h : disjoint (K₁.1 * U⁻¹) (K₂.1 * U⁻¹)) : prehaar K₀.1 U (K₁ ⊔ K₂) = prehaar K₀.1 U K₁ + prehaar K₀.1 U K₂ := by { simp only [prehaar], rw [div_add_div_same], congr', exact_mod_cast index_union_eq K₁ K₂ hU h } lemma is_left_invariant_prehaar {K₀ : positive_compacts G} {U : set G} (hU : (interior U).nonempty) (g : G) (K : compacts G) : prehaar K₀.1 U (K.map _ $ continuous_mul_left g) = prehaar K₀.1 U K := by simp only [prehaar, compacts.map_val, is_left_invariant_index K.2 _ hU] /-! ### Lemmas about `haar_product` -/ lemma prehaar_mem_haar_product (K₀ : positive_compacts G) {U : set G} (hU : (interior U).nonempty) : prehaar K₀.1 U ∈ haar_product K₀.1 := by { rintro ⟨K, hK⟩ h2K, rw [mem_Icc], exact ⟨prehaar_nonneg K₀ _, prehaar_le_index K₀ _ hU⟩ } lemma nonempty_Inter_cl_prehaar (K₀ : positive_compacts G) : (haar_product K₀.1 ∩ ⋂ (V : open_nhds_of (1 : G)), cl_prehaar K₀.1 V).nonempty := begin have : is_compact (haar_product K₀.1), { apply compact_univ_pi, intro K, apply compact_Icc }, refine this.inter_Inter_nonempty (cl_prehaar K₀.1) (λ s, is_closed_closure) (λ t, _), let V₀ := ⋂ (V ∈ t), (V : open_nhds_of 1).1, have h1V₀ : is_open V₀, { apply is_open_bInter, apply finite_mem_finset, rintro ⟨V, hV⟩ h2V, exact hV.1 }, have h2V₀ : (1 : G) ∈ V₀, { simp only [mem_Inter], rintro ⟨V, hV⟩ h2V, exact hV.2 }, refine ⟨prehaar K₀.1 V₀, _⟩, split, { apply prehaar_mem_haar_product K₀, use 1, rwa h1V₀.interior_eq }, { simp only [mem_Inter], rintro ⟨V, hV⟩ h2V, apply subset_closure, apply mem_image_of_mem, rw [mem_set_of_eq], exact ⟨subset.trans (Inter_subset _ ⟨V, hV⟩) (Inter_subset _ h2V), h1V₀, h2V₀⟩ }, end /-! ### Lemmas about `chaar` -/ /-- This is the "limit" of `prehaar K₀.1 U K` as `U` becomes a smaller and smaller open neighborhood of `(1 : G)`. More precisely, it is defined to be an arbitrary element in the intersection of all the sets `cl_prehaar K₀ V` in `haar_product K₀`. This is roughly equal to the Haar measure on compact sets, but it can differ slightly. We do know that `haar_measure K₀ (interior K.1) ≤ chaar K₀ K ≤ haar_measure K₀ K.1`. These inequalities are given by `measure_theory.measure.haar_outer_measure_le_echaar` and `measure_theory.measure.echaar_le_haar_outer_measure`. -/ def chaar (K₀ : positive_compacts G) (K : compacts G) : ℝ := classical.some (nonempty_Inter_cl_prehaar K₀) K lemma chaar_mem_haar_product (K₀ : positive_compacts G) : chaar K₀ ∈ haar_product K₀.1 := (classical.some_spec (nonempty_Inter_cl_prehaar K₀)).1 lemma chaar_mem_cl_prehaar (K₀ : positive_compacts G) (V : open_nhds_of (1 : G)) : chaar K₀ ∈ cl_prehaar K₀.1 V := by { have := (classical.some_spec (nonempty_Inter_cl_prehaar K₀)).2, rw [mem_Inter] at this, exact this V } lemma chaar_nonneg (K₀ : positive_compacts G) (K : compacts G) : 0 ≤ chaar K₀ K := by { have := chaar_mem_haar_product K₀ K (mem_univ _), rw mem_Icc at this, exact this.1 } lemma chaar_empty (K₀ : positive_compacts G) : chaar K₀ ⊥ = 0 := begin let eval : (compacts G → ℝ) → ℝ := λ f, f ⊥, have : continuous eval := continuous_apply ⊥, show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_empty }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end lemma chaar_self (K₀ : positive_compacts G) : chaar K₀ ⟨K₀.1, K₀.2.1⟩ = 1 := begin let eval : (compacts G → ℝ) → ℝ := λ f, f ⟨K₀.1, K₀.2.1⟩, have : continuous eval := continuous_apply _, show chaar K₀ ∈ eval ⁻¹' {(1 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, apply prehaar_self, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton } end lemma chaar_mono {K₀ : positive_compacts G} {K₁ K₂ : compacts G} (h : K₁.1 ⊆ K₂.1) : chaar K₀ K₁ ≤ chaar K₀ K₂ := begin let eval : (compacts G → ℝ) → ℝ := λ f, f K₂ - f K₁, have : continuous eval := (continuous_apply K₂).sub (continuous_apply K₁), rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)), apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg], apply prehaar_mono _ h, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_Ici }, end lemma chaar_sup_le {K₀ : positive_compacts G} (K₁ K₂ : compacts G) : chaar K₀ (K₁ ⊔ K₂) ≤ chaar K₀ K₁ + chaar K₀ K₂ := begin let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂), have : continuous eval := ((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub (continuous_apply (K₁ ⊔ K₂)), rw [← sub_nonneg], show chaar K₀ ∈ eval ⁻¹' (Ici (0 : ℝ)), apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, mem_Ici, eval, sub_nonneg], apply prehaar_sup_le, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_Ici }, end lemma chaar_sup_eq [t2_space G] {K₀ : positive_compacts G} {K₁ K₂ : compacts G} (h : disjoint K₁.1 K₂.1) : chaar K₀ (K₁ ⊔ K₂) = chaar K₀ K₁ + chaar K₀ K₂ := begin rcases compact_compact_separated K₁.2 K₂.2 (disjoint_iff.mp h) with ⟨U₁, U₂, h1U₁, h1U₂, h2U₁, h2U₂, hU⟩, rw [← disjoint_iff_inter_eq_empty] at hU, rcases compact_open_separated_mul K₁.2 h1U₁ h2U₁ with ⟨V₁, h1V₁, h2V₁, h3V₁⟩, rcases compact_open_separated_mul K₂.2 h1U₂ h2U₂ with ⟨V₂, h1V₂, h2V₂, h3V₂⟩, let eval : (compacts G → ℝ) → ℝ := λ f, f K₁ + f K₂ - f (K₁ ⊔ K₂), have : continuous eval := ((@continuous_add ℝ _ _ _).comp ((continuous_apply K₁).prod_mk (continuous_apply K₂))).sub (continuous_apply (K₁ ⊔ K₂)), rw [eq_comm, ← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, let V := V₁ ∩ V₂, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨V⁻¹, (is_open_inter h1V₁ h1V₂).preimage continuous_inv, by simp only [mem_inv, one_inv, h2V₁, h2V₂, V, mem_inter_eq, true_and]⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_preimage, eval, sub_eq_zero, mem_singleton_iff], rw [eq_comm], apply prehaar_sup_eq, { rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { refine disjoint_of_subset _ _ hU, { refine subset.trans (mul_subset_mul subset.rfl _) h3V₁, exact subset.trans (inv_subset.mpr h1U) (inter_subset_left _ _) }, { refine subset.trans (mul_subset_mul subset.rfl _) h3V₂, exact subset.trans (inv_subset.mpr h1U) (inter_subset_right _ _) }}}, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end lemma is_left_invariant_chaar {K₀ : positive_compacts G} (g : G) (K : compacts G) : chaar K₀ (K.map _ $ continuous_mul_left g) = chaar K₀ K := begin let eval : (compacts G → ℝ) → ℝ := λ f, f (K.map _ $ continuous_mul_left g) - f K, have : continuous eval := (continuous_apply (K.map _ _)).sub (continuous_apply K), rw [← sub_eq_zero], show chaar K₀ ∈ eval ⁻¹' {(0 : ℝ)}, apply mem_of_subset_of_mem _ (chaar_mem_cl_prehaar K₀ ⟨set.univ, is_open_univ, mem_univ _⟩), unfold cl_prehaar, rw is_closed.closure_subset_iff, { rintro _ ⟨U, ⟨h1U, h2U, h3U⟩, rfl⟩, simp only [mem_singleton_iff, mem_preimage, eval, sub_eq_zero], apply is_left_invariant_prehaar, rw h2U.interior_eq, exact ⟨1, h3U⟩ }, { apply continuous_iff_is_closed.mp this, exact is_closed_singleton }, end /-- The function `chaar` interpreted in `ℝ≥0∞` -/ @[reducible] def echaar (K₀ : positive_compacts G) (K : compacts G) : ℝ≥0∞ := show nnreal, from ⟨chaar K₀ K, chaar_nonneg _ _⟩ /-! We only prove the properties for `echaar` that we use at least twice below. -/ /-- The variant of `chaar_sup_le` for `echaar` -/ lemma echaar_sup_le {K₀ : positive_compacts G} (K₁ K₂ : compacts G) : echaar K₀ (K₁ ⊔ K₂) ≤ echaar K₀ K₁ + echaar K₀ K₂ := by { norm_cast, simp only [←nnreal.coe_le_coe, nnreal.coe_add, subtype.coe_mk, chaar_sup_le]} /-- The variant of `chaar_mono` for `echaar` -/ lemma echaar_mono {K₀ : positive_compacts G} ⦃K₁ K₂ : compacts G⦄ (h : K₁.1 ⊆ K₂.1) : echaar K₀ K₁ ≤ echaar K₀ K₂ := by { norm_cast, simp only [←nnreal.coe_le_coe, subtype.coe_mk, chaar_mono, h] } /-- The variant of `chaar_self` for `echaar` -/ lemma echaar_self {K₀ : positive_compacts G} : echaar K₀ ⟨K₀.1, K₀.2.1⟩ = 1 := by { simp_rw [← ennreal.coe_one, echaar, ennreal.coe_eq_coe, chaar_self], refl } /-- The variant of `is_left_invariant_chaar` for `echaar` -/ lemma is_left_invariant_echaar {K₀ : positive_compacts G} (g : G) (K : compacts G) : echaar K₀ (K.map _ $ continuous_mul_left g) = echaar K₀ K := by simpa only [ennreal.coe_eq_coe, ←nnreal.coe_eq] using is_left_invariant_chaar g K end haar open haar /-! ### The Haar outer measure -/ variables [topological_space G] [t2_space G] [topological_group G] /-- The Haar outer measure on `G`. It is not normalized, and is mainly used to construct `haar_measure`, which is a normalized measure. -/ def haar_outer_measure (K₀ : positive_compacts G) : outer_measure G := outer_measure.of_content (echaar K₀) $ by { rw echaar, norm_cast, rw [←nnreal.coe_eq, nnreal.coe_zero, subtype.coe_mk, chaar_empty] } lemma haar_outer_measure_eq_infi (K₀ : positive_compacts G) (A : set G) : haar_outer_measure K₀ A = ⨅ (U : set G) (hU : is_open U) (h : A ⊆ U), inner_content (echaar K₀) ⟨U, hU⟩ := outer_measure.of_content_eq_infi echaar_sup_le A lemma echaar_le_haar_outer_measure {K₀ : positive_compacts G} (K : compacts G) : echaar K₀ K ≤ haar_outer_measure K₀ K.1 := outer_measure.le_of_content_compacts echaar_sup_le K lemma haar_outer_measure_of_is_open {K₀ : positive_compacts G} (U : set G) (hU : is_open U) : haar_outer_measure K₀ U = inner_content (echaar K₀) ⟨U, hU⟩ := outer_measure.of_content_opens echaar_sup_le ⟨U, hU⟩ lemma haar_outer_measure_le_echaar {K₀ : positive_compacts G} {U : set G} (hU : is_open U) (K : compacts G) (h : U ⊆ K.1) : haar_outer_measure K₀ U ≤ echaar K₀ K := (outer_measure.of_content_le echaar_sup_le echaar_mono ⟨U, hU⟩ K h : _) lemma haar_outer_measure_exists_open {K₀ : positive_compacts G} {A : set G} (hA : haar_outer_measure K₀ A < ∞) {ε : ℝ≥0} (hε : 0 < ε) : ∃ U : opens G, A ⊆ U ∧ haar_outer_measure K₀ U ≤ haar_outer_measure K₀ A + ε := outer_measure.of_content_exists_open echaar_sup_le hA hε lemma haar_outer_measure_exists_compact {K₀ : positive_compacts G} {U : opens G} (hU : haar_outer_measure K₀ U < ∞) {ε : ℝ≥0} (hε : 0 < ε) : ∃ K : compacts G, K.1 ⊆ U ∧ haar_outer_measure K₀ U ≤ haar_outer_measure K₀ K.1 + ε := outer_measure.of_content_exists_compact echaar_sup_le hU hε lemma haar_outer_measure_caratheodory {K₀ : positive_compacts G} (A : set G) : (haar_outer_measure K₀).caratheodory.measurable_set' A ↔ ∀ (U : opens G), haar_outer_measure K₀ (U ∩ A) + haar_outer_measure K₀ (U \ A) ≤ haar_outer_measure K₀ U := outer_measure.of_content_caratheodory echaar_sup_le A lemma one_le_haar_outer_measure_self {K₀ : positive_compacts G} : 1 ≤ haar_outer_measure K₀ K₀.1 := begin rw [haar_outer_measure_eq_infi], refine le_binfi _, intros U hU, refine le_infi _, intros h2U, refine le_trans _ (le_bsupr ⟨K₀.1, K₀.2.1⟩ h2U), simp_rw [echaar_self, le_rfl] end lemma haar_outer_measure_pos_of_is_open {K₀ : positive_compacts G} {U : set G} (hU : is_open U) (h2U : U.nonempty) : 0 < haar_outer_measure K₀ U := outer_measure.of_content_pos_of_is_mul_left_invariant echaar_sup_le is_left_invariant_echaar ⟨K₀.1, K₀.2.1⟩ (by simp only [echaar_self, ennreal.zero_lt_one]) hU h2U lemma haar_outer_measure_self_pos {K₀ : positive_compacts G} : 0 < haar_outer_measure K₀ K₀.1 := (haar_outer_measure_pos_of_is_open is_open_interior K₀.2.2).trans_le ((haar_outer_measure K₀).mono interior_subset) lemma haar_outer_measure_lt_top_of_is_compact [locally_compact_space G] {K₀ : positive_compacts G} {K : set G} (hK : is_compact K) : haar_outer_measure K₀ K < ∞ := begin rcases exists_compact_superset hK with ⟨F, h1F, h2F⟩, refine ((haar_outer_measure K₀).mono h2F).trans_lt _, refine (haar_outer_measure_le_echaar is_open_interior ⟨F, h1F⟩ interior_subset).trans_lt ennreal.coe_lt_top end variables [S : measurable_space G] [borel_space G] include S lemma haar_caratheodory_measurable (K₀ : positive_compacts G) : S ≤ (haar_outer_measure K₀).caratheodory := begin rw [@borel_space.measurable_eq G _ _], refine generate_from_le _, intros U hU, rw haar_outer_measure_caratheodory, intro U', rw haar_outer_measure_of_is_open ((U' : set G) ∩ U) (is_open_inter U'.prop hU), simp only [inner_content, supr_subtype'], rw [opens.coe_mk], haveI : nonempty {L : compacts G // L.1 ⊆ U' ∩ U} := ⟨⟨⊥, empty_subset _⟩⟩, rw [ennreal.supr_add], refine supr_le _, rintro ⟨L, hL⟩, simp only [subset_inter_iff] at hL, have : ↑U' \ U ⊆ U' \ L.1 := diff_subset_diff_right hL.2, refine le_trans (add_le_add_left ((haar_outer_measure K₀).mono' this) _) _, rw haar_outer_measure_of_is_open (↑U' \ L.1) (is_open_diff U'.2 L.2.is_closed), simp only [inner_content, supr_subtype'], rw [opens.coe_mk], haveI : nonempty {M : compacts G // M.1 ⊆ ↑U' \ L.1} := ⟨⟨⊥, empty_subset _⟩⟩, rw [ennreal.add_supr], refine supr_le _, rintro ⟨M, hM⟩, simp only [subset_diff] at hM, have : (L ⊔ M).1 ⊆ U', { simp only [union_subset_iff, compacts.sup_val, hM, hL, and_self] }, rw haar_outer_measure_of_is_open ↑U' U'.2, refine le_trans (ge_of_eq _) (le_inner_content _ _ this), norm_cast, simp only [←nnreal.coe_eq, nnreal.coe_add, subtype.coe_mk], exact chaar_sup_eq hM.2.symm end /-! ### The Haar measure -/ /-- the Haar measure on `G`, scaled so that `haar_measure K₀ K₀ = 1`. -/ def haar_measure (K₀ : positive_compacts G) : measure G := (haar_outer_measure K₀ K₀.1)⁻¹ • (haar_outer_measure K₀).to_measure (haar_caratheodory_measurable K₀) lemma haar_measure_apply {K₀ : positive_compacts G} {s : set G} (hs : measurable_set s) : haar_measure K₀ s = haar_outer_measure K₀ s / haar_outer_measure K₀ K₀.1 := by { simp only [haar_measure, hs, div_eq_mul_inv, mul_comm, to_measure_apply, algebra.id.smul_eq_mul, pi.smul_apply, measure.coe_smul] } lemma is_mul_left_invariant_haar_measure (K₀ : positive_compacts G) : is_mul_left_invariant (haar_measure K₀) := begin intros g A hA, rw [haar_measure_apply hA, haar_measure_apply (measurable_mul_left g hA)], congr' 1, exact outer_measure.is_mul_left_invariant_of_content echaar_sup_le is_left_invariant_echaar g A end lemma haar_measure_self [locally_compact_space G] {K₀ : positive_compacts G} : haar_measure K₀ K₀.1 = 1 := begin rw [haar_measure_apply K₀.2.1.measurable_set, ennreal.div_self], { rw [← pos_iff_ne_zero], exact haar_outer_measure_self_pos }, { exact ne_of_lt (haar_outer_measure_lt_top_of_is_compact K₀.2.1) } end lemma haar_measure_pos_of_is_open [locally_compact_space G] {K₀ : positive_compacts G} {U : set G} (hU : is_open U) (h2U : U.nonempty) : 0 < haar_measure K₀ U := begin rw [haar_measure_apply hU.measurable_set, ennreal.div_pos_iff], refine ⟨_, ne_of_lt $ haar_outer_measure_lt_top_of_is_compact K₀.2.1⟩, rw [← pos_iff_ne_zero], apply haar_outer_measure_pos_of_is_open hU h2U end lemma regular_haar_measure [locally_compact_space G] {K₀ : positive_compacts G} : (haar_measure K₀).regular := begin apply measure.regular.smul, split, { intros K hK, rw [to_measure_apply _ _ hK.measurable_set], apply haar_outer_measure_lt_top_of_is_compact hK }, { intros A hA, rw [to_measure_apply _ _ hA, haar_outer_measure_eq_infi], refine binfi_le_binfi _, intros U hU, refine infi_le_infi _, intro h2U, rw [to_measure_apply _ _ hU.measurable_set, haar_outer_measure_of_is_open U hU], refl' }, { intros U hU, rw [to_measure_apply _ _ hU.measurable_set, haar_outer_measure_of_is_open U hU], dsimp only [inner_content], refine bsupr_le (λ K hK, _), refine le_supr_of_le K.1 _, refine le_supr_of_le K.2 _, refine le_supr_of_le hK _, rw [to_measure_apply _ _ K.2.measurable_set], apply echaar_le_haar_outer_measure }, { rw ennreal.inv_lt_top, apply haar_outer_measure_self_pos } end instance [locally_compact_space G] [separable_space G] (K₀ : positive_compacts G) : sigma_finite (haar_measure K₀) := regular_haar_measure.sigma_finite section unique variables [locally_compact_space G] [second_countable_topology G] {μ : measure G} [sigma_finite μ] /-- The Haar measure is unique up to scaling. More precisely: every σ-finite left invariant measure is a scalar multiple of the Haar measure. -/ theorem haar_measure_unique (hμ : is_mul_left_invariant μ) (K₀ : positive_compacts G) : μ = μ K₀.1 • haar_measure K₀ := begin ext1 s hs, have := measure_mul_measure_eq hμ (is_mul_left_invariant_haar_measure K₀) regular_haar_measure K₀.2.1 hs, rw [haar_measure_self, one_mul] at this, rw [← this (by norm_num), smul_apply], end theorem regular_of_left_invariant (hμ : is_mul_left_invariant μ) {K} (hK : is_compact K) (h2K : (interior K).nonempty) (hμK : μ K < ∞) : regular μ := begin rw [haar_measure_unique hμ ⟨K, hK, h2K⟩], exact regular.smul regular_haar_measure hμK end end unique end measure end measure_theory
153b8e5e8ee6c18b7d3d2e5e1367b2025f2de070
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/ring/basic.lean
318bae4e3e2262a574dacc01eacd910c86de01e0
[ "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
48,519
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, Floris van Doorn, Amelia Livingston, Yury Kudryashov, Neil Strickland -/ import algebra.divisibility import algebra.regular.basic /-! # Properties and homomorphisms of semirings and rings This file proves simple properties of semirings, rings and domains and their unit groups. It also defines bundled homomorphisms of semirings and rings. As with monoid and groups, we use the same structure `ring_hom a β`, a.k.a. `α →+* β`, for both homomorphism types. The unbundled homomorphisms are defined in `deprecated/ring`. They are deprecated and the plan is to slowly remove them from mathlib. ## Main definitions ring_hom, nonzero, domain, is_domain ## Notations →+* for bundled ring homs (also use for semiring 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 `semiring_hom` -- the idea is that `ring_hom` is used. The constructor for a `ring_hom` between semirings needs a proof of `map_zero`, `map_one` and `map_add` as well as `map_mul`; a separate constructor `ring_hom.mk'` will construct ring homs between rings from monoid homs given only a proof that addition is preserved. ## Tags `ring_hom`, `semiring_hom`, `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`, `is_domain`, `nonzero`, `units` -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x} set_option old_structure_cmd true open function /-! ### `distrib` class -/ /-- A typeclass stating that multiplication is left and right distributive over addition. -/ @[protect_proj, ancestor has_mul has_add] class distrib (R : Type*) extends has_mul R, has_add R := (left_distrib : ∀ a b c : R, a * (b + c) = (a * b) + (a * c)) (right_distrib : ∀ a b c : R, (a + b) * c = (a * c) + (b * c)) lemma left_distrib [distrib R] (a b c : R) : a * (b + c) = a * b + a * c := distrib.left_distrib a b c alias left_distrib ← mul_add lemma right_distrib [distrib R] (a b c : R) : (a + b) * c = a * c + b * c := distrib.right_distrib a b c alias right_distrib ← add_mul /-- Pullback a `distrib` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.distrib {S} [has_mul R] [has_add R] [distrib S] (f : R → S) (hf : injective f) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : distrib R := { mul := (*), add := (+), left_distrib := λ x y z, hf $ by simp only [*, left_distrib], right_distrib := λ x y z, hf $ by simp only [*, right_distrib] } /-- Pushforward a `distrib` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.distrib {S} [distrib R] [has_add S] [has_mul S] (f : R → S) (hf : surjective f) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : distrib S := { mul := (*), add := (+), left_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, left_distrib], right_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, right_distrib] } /-! ### Semirings -/ /-- A not-necessarily-unital, not-necessarily-associative semiring. -/ @[protect_proj, ancestor add_comm_monoid distrib mul_zero_class] class non_unital_non_assoc_semiring (α : Type u) extends add_comm_monoid α, distrib α, mul_zero_class α /-- An associative but not-necessarily unital semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring semigroup_with_zero] class non_unital_semiring (α : Type u) extends non_unital_non_assoc_semiring α, semigroup_with_zero α /-- A unital but not-necessarily-associative semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring mul_zero_one_class] class non_assoc_semiring (α : Type u) extends non_unital_non_assoc_semiring α, mul_zero_one_class α /-- A semiring is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero` instead of `monoid` and `mul_zero_class`. -/ @[protect_proj, ancestor non_unital_semiring non_assoc_semiring monoid_with_zero] class semiring (α : Type u) extends non_unital_semiring α, non_assoc_semiring α, monoid_with_zero α section injective_surjective_maps variables [has_zero β] [has_add β] [has_mul β] /-- Pullback a `non_unital_non_assoc_semiring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.non_unital_non_assoc_semiring {α : Type u} [non_unital_non_assoc_semiring α] (f : β → α) (hf : injective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_unital_non_assoc_semiring β := { .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } /-- Pullback a `non_unital_semiring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.non_unital_semiring {α : Type u} [non_unital_semiring α] (f : β → α) (hf : injective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_unital_semiring β := { .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.semigroup_with_zero f zero mul } /-- Pullback a `non_assoc_semiring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.non_assoc_semiring {α : Type u} [non_assoc_semiring α] [has_one β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_assoc_semiring β := { .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.mul_one_class f one mul } /-- Pullback a `semiring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.semiring {α : Type u} [semiring α] [has_one β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : semiring β := { .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } /-- Pushforward a `non_unital_non_assoc_semiring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.non_unital_non_assoc_semiring {α : Type u} [non_unital_non_assoc_semiring α] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_unital_non_assoc_semiring β := { .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } /-- Pushforward a `non_unital_semiring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.non_unital_semiring {α : Type u} [non_unital_semiring α] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_unital_semiring β := { .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.semigroup_with_zero f zero mul } /-- Pushforward a `non_assoc_semiring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.non_assoc_semiring {α : Type u} [non_assoc_semiring α] [has_one β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : non_assoc_semiring β := { .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.mul_one_class f one mul } /-- Pushforward a `semiring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.semiring {α : Type u} [semiring α] [has_one β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : semiring β := { .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } end injective_surjective_maps section semiring variables [semiring α] lemma one_add_one_eq_two : 1 + 1 = (2 : α) := by unfold bit0 theorem two_mul (n : α) : 2 * n = n + n := eq.trans (right_distrib 1 1 n) (by simp) lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d := by simp [right_distrib] theorem mul_two (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n := (two_mul _).symm @[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : a * (if P then b else c) = if P then a * b else a * c := by split_ifs; refl @[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : (if P then a else b) * c = if P then a * c else b * c := by split_ifs; refl -- We make `mul_ite` and `ite_mul` simp lemmas, -- but not `add_ite` or `ite_add`. -- The problem we're trying to avoid is dealing with -- summations of the form `∑ x in s, (f x + ite P 1 0)`, -- in which `add_ite` followed by `sum_ite` would needlessly slice up -- the `f x` terms according to whether `P` holds at `x`. -- There doesn't appear to be a corresponding difficulty so far with -- `mul_ite` and `ite_mul`. attribute [simp] mul_ite ite_mul @[simp] lemma mul_boole {α} [non_assoc_semiring α] (P : Prop) [decidable P] (a : α) : a * (if P then 1 else 0) = if P then a else 0 := by simp @[simp] lemma boole_mul {α} [non_assoc_semiring α] (P : Prop) [decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by simp lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = ite P a 0 * b := by { by_cases h : P; simp [h], } lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = a * ite P b 0 := by { by_cases h : P; simp [h], } /-- An element `a` of a semiring is even if there exists `k` such `a = 2*k`. -/ def even (a : α) : Prop := ∃ k, a = 2*k lemma even_iff_two_dvd {a : α} : even a ↔ 2 ∣ a := iff.rfl @[simp] lemma range_two_mul (α : Type*) [semiring α] : set.range (λ x : α, 2 * x) = {a | even a} := by { ext x, simp [even, eq_comm] } @[simp] lemma even_bit0 (a : α) : even (bit0 a) := ⟨a, by rw [bit0, two_mul]⟩ /-- An element `a` of a semiring is odd if there exists `k` such `a = 2*k + 1`. -/ def odd (a : α) : Prop := ∃ k, a = 2*k + 1 @[simp] lemma odd_bit1 (a : α) : odd (bit1 a) := ⟨a, by rw [bit1, bit0, two_mul]⟩ @[simp] lemma range_two_mul_add_one (α : Type*) [semiring α] : set.range (λ x : α, 2 * x + 1) = {a | odd a} := by { ext x, simp [odd, eq_comm] } theorem dvd_add {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c := dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he]))) end semiring namespace add_hom /-- Left multiplication by an element of a type with distributive multiplication is an `add_hom`. -/ @[simps { fully_applied := ff}] def mul_left {R : Type*} [distrib R] (r : R) : add_hom R R := ⟨(*) r, mul_add r⟩ /-- Left multiplication by an element of a type with distributive multiplication is an `add_hom`. -/ @[simps { fully_applied := ff}] def mul_right {R : Type*} [distrib R] (r : R) : add_hom R R := ⟨λ a, a * r, λ _ _, add_mul _ _ r⟩ end add_hom namespace add_monoid_hom /-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R := { to_fun := (*) r, map_zero' := mul_zero r, map_add' := mul_add r } @[simp] lemma coe_mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : ⇑(mul_left r) = (*) r := rfl /-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R := { to_fun := λ a, a * r, map_zero' := zero_mul r, map_add' := λ _ _, add_mul _ _ r } @[simp] lemma coe_mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : ⇑(mul_right r) = (* r) := rfl lemma mul_right_apply {R : Type*} [non_unital_non_assoc_semiring R] (a r : R) : mul_right r a = a * r := rfl end add_monoid_hom /-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too. This extends from both `monoid_hom` and `monoid_with_zero_hom` in order to put the fields in a sensible order, even though `monoid_with_zero_hom` already extends `monoid_hom`. -/ structure ring_hom (α : Type*) (β : Type*) [non_assoc_semiring α] [non_assoc_semiring β] extends α →* β, α →+ β, α →*₀ β infixr ` →+* `:25 := ring_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as a monoid with zero homomorphism `R →*₀ S`. The `simp`-normal form is `(f : R →*₀ S)`. -/ add_decl_doc ring_hom.to_monoid_with_zero_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as a monoid homomorphism `R →* S`. The `simp`-normal form is `(f : R →* S)`. -/ add_decl_doc ring_hom.to_monoid_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as an additive monoid homomorphism `R →+ S`. The `simp`-normal form is `(f : R →+ S)`. -/ add_decl_doc ring_hom.to_add_monoid_hom section ring_hom_class /-- `ring_hom_class F R S` states that `F` is a type of (semi)ring homomorphisms. You should extend this class when you extend `ring_hom`. This extends from both `monoid_hom_class` and `monoid_with_zero_hom_class` in order to put the fields in a sensible order, even though `monoid_with_zero_hom_class` already extends `monoid_hom_class`. -/ class ring_hom_class (F : Type*) (R S : out_param Type*) [non_assoc_semiring R] [non_assoc_semiring S] extends monoid_hom_class F R S, add_monoid_hom_class F R S, monoid_with_zero_hom_class F R S variables {F : Type*} [non_assoc_semiring α] [non_assoc_semiring β] [ring_hom_class F α β] /-- Ring homomorphisms preserve `bit0`. -/ @[simp] lemma map_bit0 (f : F) (a : α) : (f (bit0 a) : β) = bit0 (f a) := map_add _ _ _ /-- Ring homomorphisms preserve `bit1`. -/ @[simp] lemma map_bit1 (f : F) (a : α) : (f (bit1 a) : β) = bit1 (f a) := by simp [bit1] end ring_hom_class namespace ring_hom section coe /-! Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ variables {rα : non_assoc_semiring α} {rβ : non_assoc_semiring β} include rα rβ instance : ring_hom_class (α →+* β) α β := { coe := ring_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_add := ring_hom.map_add', map_zero := ring_hom.map_zero', map_mul := ring_hom.map_mul', map_one := ring_hom.map_one' } /-- Helper instance for when there's too many metavariables to apply `to_fun.to_coe_fn` directly. -/ instance : has_coe_to_fun (α →+* β) (λ _, α → β) := ⟨ring_hom.to_fun⟩ initialize_simps_projections ring_hom (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : α →+* β) : f.to_fun = f := rfl @[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl instance has_coe_monoid_hom : has_coe (α →+* β) (α →* β) := ⟨ring_hom.to_monoid_hom⟩ @[simp, norm_cast] lemma coe_monoid_hom (f : α →+* β) : ⇑(f : α →* β) = f := rfl @[simp] lemma to_monoid_hom_eq_coe (f : α →+* β) : f.to_monoid_hom = f := rfl @[simp] lemma to_monoid_with_zero_hom_eq_coe (f : α →+* β) : (f.to_monoid_with_zero_hom : α → β) = f := rfl @[simp] lemma coe_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) : ((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →* β) = ⟨f, h₁, h₂⟩ := rfl instance has_coe_add_monoid_hom : has_coe (α →+* β) (α →+ β) := ⟨ring_hom.to_add_monoid_hom⟩ @[simp, norm_cast] lemma coe_add_monoid_hom (f : α →+* β) : ⇑(f : α →+ β) = f := rfl @[simp] lemma to_add_monoid_hom_eq_coe (f : α →+* β) : f.to_add_monoid_hom = f := rfl @[simp] lemma coe_add_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) : ((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →+ β) = ⟨f, h₃, h₄⟩ := rfl end coe variables [rα : non_assoc_semiring α] [rβ : non_assoc_semiring β] section include rα rβ variables (f : α →+* β) {x y : α} {rα rβ} theorem congr_fun {f g : α →+* β} (h : f = g) (x : α) : f x = g x := fun_like.congr_fun h x theorem congr_arg (f : α →+* β) {x y : α} (h : x = y) : f x = f y := fun_like.congr_arg f h theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g := fun_like.coe_injective h @[ext] theorem ext ⦃f g : α →+* β⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext _ _ h theorem ext_iff {f g : α →+* β} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff @[simp] lemma mk_coe (f : α →+* β) (h₁ h₂ h₃ h₄) : ring_hom.mk f h₁ h₂ h₃ h₄ = f := ext $ λ _, rfl theorem coe_add_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →+ β)) := λ f g h, ext (λ x, add_monoid_hom.congr_fun h x) theorem coe_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →* β)) := λ f g h, ext (λ x, monoid_hom.congr_fun h x) /-- Ring homomorphisms map zero to zero. -/ protected lemma map_zero (f : α →+* β) : f 0 = 0 := map_zero f /-- Ring homomorphisms map one to one. -/ protected lemma map_one (f : α →+* β) : f 1 = 1 := map_one f /-- Ring homomorphisms preserve addition. -/ protected lemma map_add (f : α →+* β) (a b : α) : f (a + b) = f a + f b := map_add f a b /-- Ring homomorphisms preserve multiplication. -/ protected lemma map_mul (f : α →+* β) (a b : α) : f (a * b) = f a * f b := map_mul f a b /-- Ring homomorphisms preserve `bit0`. -/ protected lemma map_bit0 (f : α →+* β) (a : α) : f (bit0 a) = bit0 (f a) := map_add _ _ _ /-- Ring homomorphisms preserve `bit1`. -/ protected lemma map_bit1 (f : α →+* β) (a : α) : f (bit1 a) = bit1 (f a) := by simp [bit1] /-- `f : R →+* S` has a trivial codomain iff `f 1 = 0`. -/ lemma codomain_trivial_iff_map_one_eq_zero : (0 : β) = 1 ↔ f 1 = 0 := by rw [map_one, eq_comm] /-- `f : R →+* S` has a trivial codomain iff it has a trivial range. -/ lemma codomain_trivial_iff_range_trivial : (0 : β) = 1 ↔ (∀ x, f x = 0) := f.codomain_trivial_iff_map_one_eq_zero.trans ⟨λ h x, by rw [←mul_one x, map_mul, h, mul_zero], λ h, h 1⟩ /-- `f : R →+* S` has a trivial codomain iff its range is `{0}`. -/ lemma codomain_trivial_iff_range_eq_singleton_zero : (0 : β) = 1 ↔ set.range f = {0} := f.codomain_trivial_iff_range_trivial.trans ⟨ λ h, set.ext (λ y, ⟨λ ⟨x, hx⟩, by simp [←hx, h x], λ hy, ⟨0, by simpa using hy.symm⟩⟩), λ h x, set.mem_singleton_iff.mp (h ▸ set.mem_range_self x)⟩ /-- `f : R →+* S` doesn't map `1` to `0` if `S` is nontrivial -/ lemma map_one_ne_zero [nontrivial β] : f 1 ≠ 0 := mt f.codomain_trivial_iff_map_one_eq_zero.mpr zero_ne_one /-- If there is a homomorphism `f : R →+* S` and `S` is nontrivial, then `R` is nontrivial. -/ lemma domain_nontrivial [nontrivial β] : nontrivial α := ⟨⟨1, 0, mt (λ h, show f 1 = 0, by rw [h, map_zero]) f.map_one_ne_zero⟩⟩ end lemma is_unit_map [semiring α] [semiring β] (f : α →+* β) {a : α} (h : is_unit a) : is_unit (f a) := h.map f.to_monoid_hom /-- The identity ring homomorphism from a semiring to itself. -/ def id (α : Type*) [non_assoc_semiring α] : α →+* α := by refine {to_fun := id, ..}; intros; refl include rα instance : inhabited (α →+* α) := ⟨id α⟩ @[simp] lemma id_apply (x : α) : ring_hom.id α x = x := rfl @[simp] lemma coe_add_monoid_hom_id : (id α : α →+ α) = add_monoid_hom.id α := rfl @[simp] lemma coe_monoid_hom_id : (id α : α →* α) = monoid_hom.id α := rfl variable {rγ : non_assoc_semiring γ} include rβ rγ /-- Composition of ring homomorphisms is a ring homomorphism. -/ def comp (hnp : β →+* γ) (hmn : α →+* β) : α →+* γ := { to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_add' := λ x y, by simp, map_mul' := λ x y, by simp} /-- Composition of semiring homomorphisms is associative. -/ lemma comp_assoc {δ} {rδ: non_assoc_semiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl lemma comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x = (hnp (hmn x)) := rfl omit rγ @[simp] lemma comp_id (f : α →+* β) : f.comp (id α) = f := ext $ λ x, rfl @[simp] lemma id_comp (f : α →+* β) : (id β).comp f = f := ext $ λ x, rfl omit rβ instance : monoid (α →+* α) := { one := id α, mul := comp, mul_one := comp_id, one_mul := id_comp, mul_assoc := λ f g h, comp_assoc _ _ _ } lemma one_def : (1 : α →+* α) = id α := rfl @[simp] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl @[simp] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl include rβ rγ lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ring_hom.ext $ hf.forall.2 (ext_iff.1 h), λ h, h ▸ rfl⟩ lemma cancel_left {g : β →+* γ} {f₁ f₂ : α →+* β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ring_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩ omit rα rβ rγ end ring_hom section semiring variables [semiring α] {a : α} @[simp] theorem two_dvd_bit0 : 2 ∣ bit0 a := ⟨a, bit0_eq_two_mul _⟩ lemma ring_hom.map_dvd [semiring β] (f : α →+* β) {a b : α} : a ∣ b → f a ∣ f b := f.to_monoid_hom.map_dvd end semiring /-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor semiring comm_monoid] class comm_semiring (α : Type u) extends semiring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } section comm_semiring variables [comm_semiring α] [comm_semiring β] {a b c : α} /-- Pullback a `semiring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ] (f : γ → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semiring γ := { .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul } /-- Pushforward a `semiring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ] (f : α → γ) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semiring γ := { .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul } lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b := by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b] lemma has_dvd.dvd.linear_comb {d x y : α} (hdx : d ∣ x) (hdy : d ∣ y) (a b : α) : d ∣ (a * x + b * y) := dvd_add (hdx.mul_left a) (hdy.mul_left b) end comm_semiring /-! ### Rings -/ /-- A not-necessarily-unital, not-necessarily-associative ring. -/ @[protect_proj, ancestor add_comm_group non_unital_non_assoc_semiring] class non_unital_non_assoc_ring (α : Type u) extends add_comm_group α, non_unital_non_assoc_semiring α section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] /-- Pullback a `non_unital_non_assoc_ring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.non_unital_non_assoc_ring [has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : non_unital_non_assoc_ring β := { .. hf.add_comm_group f zero add neg sub, ..hf.mul_zero_class f zero mul, .. hf.distrib f add mul } /-- Pushforward a `non_unital_non_assoc_ring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.non_unital_non_assoc_ring [has_zero β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : non_unital_non_assoc_ring β := { .. hf.add_comm_group f zero add neg sub, .. hf.mul_zero_class f zero mul, .. hf.distrib f add mul } end non_unital_non_assoc_ring /-- A ring is a type with the following structures: additive commutative group (`add_comm_group`), multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a `semiring` with a negation operation making it an additive group. -/ @[protect_proj, ancestor add_comm_group monoid distrib] class ring (α : Type u) extends add_comm_group α, monoid α, distrib α section ring variables [ring α] {a b c d e : α} /- A (unital, associative) ring is a not-necessarily-unital, not-necessarily-associative ring -/ @[priority 100] -- see Note [lower instance priority] instance ring.to_non_unital_non_assoc_ring : non_unital_non_assoc_ring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic definitions are given in terms of semirings, but many applications use rings or fields. We increase a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that more specific instances are tried first. -/ @[priority 200] instance ring.to_semiring : semiring α := { ..‹ring α›, .. ring.to_non_unital_non_assoc_ring } /-- Pullback a `ring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : ring β := { .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul } /-- Pushforward a `ring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : ring β := { .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul } lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b := neg_eq_of_add_eq_zero begin rw [← right_distrib, add_right_neg, zero_mul] end lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b := neg_eq_of_add_eq_zero begin rw [← left_distrib, add_right_neg, mul_zero] end @[simp] lemma neg_mul_eq_neg_mul_symm (a b : α) : - a * b = - (a * b) := eq.symm (neg_mul_eq_neg_mul a b) @[simp] lemma mul_neg_eq_neg_mul_symm (a b : α) : a * - b = - (a * b) := eq.symm (neg_mul_eq_mul_neg a b) lemma neg_mul_neg (a b : α) : -a * -b = a * b := by simp lemma neg_mul_comm (a b : α) : -a * b = a * -b := by simp theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a := by simp lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c := by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c) alias mul_sub_left_distrib ← mul_sub lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c := by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c alias mul_sub_right_distrib ← sub_mul /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ lemma mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ lemma neg_one_mul (a : α) : -1 * a = -a := by simp /-- An iff statement following from right distributivity in rings and the definition of subtraction. -/ theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm] ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h, begin rw ← h, simp end) ... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end /-- A simplification of one side of an equation exploiting right distributivity in rings and the definition of subtraction. -/ theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end ... = d : begin rw h, simp [@add_sub_cancel α] end end ring namespace units variables [ring α] {a b : α} /-- Each element of the group of units of a ring has an additive inverse. -/ instance : has_neg αˣ := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩ /-- Representing an element of a ring's unit group as an element of the ring commutes with mapping this element to its additive inverse. -/ @[simp, norm_cast] protected theorem coe_neg (u : αˣ) : (↑-u : α) = -u := rfl @[simp, norm_cast] protected theorem coe_neg_one : ((-1 : αˣ) : α) = -1 := rfl /-- Mapping an element of a ring's unit group to its inverse commutes with mapping this element to its additive inverse. -/ @[simp] protected theorem neg_inv (u : αˣ) : (-u)⁻¹ = -u⁻¹ := rfl /-- An element of a ring's unit group equals the additive inverse of its additive inverse. -/ @[simp] protected theorem neg_neg (u : αˣ) : - -u = u := units.ext $ neg_neg _ /-- Multiplication of elements of a ring's unit group commutes with mapping the first argument to its additive inverse. -/ @[simp] protected theorem neg_mul (u₁ u₂ : αˣ) : -u₁ * u₂ = -(u₁ * u₂) := units.ext $ neg_mul_eq_neg_mul_symm _ _ /-- Multiplication of elements of a ring's unit group commutes with mapping the second argument to its additive inverse. -/ @[simp] protected theorem mul_neg (u₁ u₂ : αˣ) : u₁ * -u₂ = -(u₁ * u₂) := units.ext $ (neg_mul_eq_mul_neg _ _).symm /-- Multiplication of the additive inverses of two elements of a ring's unit group equals multiplication of the two original elements. -/ @[simp] protected theorem neg_mul_neg (u₁ u₂ : αˣ) : -u₁ * -u₂ = u₁ * u₂ := by simp /-- The additive inverse of an element of a ring's unit group equals the additive inverse of one times the original element. -/ protected theorem neg_eq_neg_one_mul (u : αˣ) : -u = -1 * u := by simp end units lemma is_unit.neg [ring α] {a : α} : is_unit a → is_unit (-a) | ⟨x, hx⟩ := hx ▸ (-x).is_unit lemma is_unit.neg_iff [ring α] (a : α) : is_unit (-a) ↔ is_unit a := ⟨λ h, neg_neg a ▸ h.neg, is_unit.neg⟩ lemma is_unit.sub_iff [ring α] {x y : α} : is_unit (x - y) ↔ is_unit (y - x) := (is_unit.neg_iff _).symm.trans $ neg_sub x y ▸ iff.rfl namespace ring_hom /-- Ring homomorphisms preserve additive inverse. -/ protected theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) := map_neg f x /-- Ring homomorphisms preserve subtraction. -/ protected theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) : f (x - y) = (f x) - (f y) := map_sub f x y /-- A ring homomorphism is injective iff its kernel is trivial. -/ theorem injective_iff {α β} [ring α] [non_assoc_semiring β] (f : α →+* β) : function.injective f ↔ (∀ a, f a = 0 → a = 0) := (f : α →+ β).injective_iff /-- A ring homomorphism is injective iff its kernel is trivial. -/ theorem injective_iff' {α β} [ring α] [non_assoc_semiring β] (f : α →+* β) : function.injective f ↔ (∀ a, f a = 0 ↔ a = 0) := (f : α →+ β).injective_iff' /-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/ def mk' {γ} [non_assoc_semiring α] [ring γ] (f : α →* γ) (map_add : ∀ a b : α, f (a + b) = f a + f b) : α →+* γ := { to_fun := f, .. add_monoid_hom.mk' f map_add, .. f } end ring_hom /-- A commutative ring is a `ring` with commutative multiplication. -/ @[protect_proj, ancestor ring comm_semigroup] class comm_ring (α : Type u) extends ring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } section ring variables [ring α] {a b c : α} theorem dvd_neg_of_dvd (h : a ∣ b) : (a ∣ -b) := dvd.elim h (assume c, assume : b = a * c, dvd.intro (-c) (by simp [this])) theorem dvd_of_dvd_neg (h : a ∣ -b) : (a ∣ b) := let t := dvd_neg_of_dvd h in by rwa neg_neg at t /-- An element a of a ring divides the additive inverse of an element b iff a divides b. -/ @[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ theorem neg_dvd_of_dvd (h : a ∣ b) : -a ∣ b := dvd.elim h (assume c, assume : b = a * c, dvd.intro (-c) (by simp [this])) theorem dvd_of_neg_dvd (h : -a ∣ b) : a ∣ b := let t := neg_dvd_of_dvd h in by rwa neg_neg at t /-- The additive inverse of an element a of a ring divides another element b iff a divides b. -/ @[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c := by { rw sub_eq_add_neg, exact dvd_add h₁ (dvd_neg_of_dvd h₂) } theorem dvd_add_iff_left (h : a ∣ c) : a ∣ b ↔ a ∣ b + c := ⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩ theorem dvd_add_iff_right (h : a ∣ b) : a ∣ c ↔ a ∣ b + c := by rw add_comm; exact dvd_add_iff_left h theorem two_dvd_bit1 : 2 ∣ bit1 a ↔ (2 : α) ∣ 1 := (dvd_add_iff_right (@two_dvd_bit0 _ _ a)).symm /-- If an element a divides another element c in a commutative ring, a divides the sum of another element b with c iff a divides b. -/ theorem dvd_add_left (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := (dvd_add_iff_left h).symm /-- If an element a divides another element b in a commutative ring, a divides the sum of b and another element c iff a divides c. -/ theorem dvd_add_right (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := (dvd_add_iff_right h).symm /-- An element a divides the sum a + b if and only if a divides b.-/ @[simp] lemma dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b := dvd_add_right (dvd_refl a) /-- An element a divides the sum b + a if and only if a divides b.-/ @[simp] lemma dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b := dvd_add_left (dvd_refl a) lemma dvd_iff_dvd_of_dvd_sub {a b c : α} (h : a ∣ (b - c)) : (a ∣ b ↔ a ∣ c) := begin split, { intro h', convert dvd_sub h' h, exact eq.symm (sub_sub_self b c) }, { intro h', convert dvd_add h h', exact eq_add_of_sub_eq rfl } end @[simp] theorem even_neg (a : α) : even (-a) ↔ even a := dvd_neg _ _ lemma odd.neg {a : α} (hp : odd a) : odd (-a) := begin obtain ⟨k, hk⟩ := hp, use -(k + 1), rw [mul_neg_eq_neg_mul_symm, mul_add, neg_add, add_assoc, two_mul (1 : α), neg_add, neg_add_cancel_right, ←neg_add, hk], end @[simp] lemma odd_neg (a : α) : odd (-a) ↔ odd a := ⟨λ h, neg_neg a ▸ h.neg, odd.neg⟩ end ring section comm_ring variables [comm_ring α] {a b c : α} /-- Pullback a `comm_ring` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : comm_ring β := { .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul } /-- Pushforward a `comm_ring` instance along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : comm_ring β := { .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul } local attribute [simp] add_assoc add_comm add_left_comm mul_comm /-- Representation of a difference of two squares in a commutative ring as a product. -/ theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) := by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel] lemma mul_self_sub_one (a : α) : a * a - 1 = (a + 1) * (a - 1) := by rw [← mul_self_sub_mul_self, mul_one] /-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with its roots. This particular version states that if we have a root `x` of a monic quadratic polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient and `x * y` is the `a_0` coefficient. -/ lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) : ∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c := begin have : c = -(x * x - b * x) := (neg_eq_of_add_eq_zero h).symm, have : c = x * (b - x), by subst this; simp [mul_sub, mul_comm], refine ⟨b - x, _, by simp, by rw this⟩, rw [this, sub_add, ← sub_mul, sub_self] end lemma dvd_mul_sub_mul {k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) : k ∣ a * x - b * y := begin convert dvd_add (hxy.mul_left a) (hab.mul_right y), rw [mul_sub_left_distrib, mul_sub_right_distrib], simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left], end end comm_ring lemma succ_ne_self [ring α] [nontrivial α] (a : α) : a + 1 ≠ a := λ h, one_ne_zero ((add_right_inj a).mp (by simp [h])) lemma pred_ne_self [ring α] [nontrivial α] (a : α) : a - 1 ≠ a := λ h, one_ne_zero (neg_injective ((add_right_inj a).mp (by simpa [sub_eq_add_neg] using h))) /-- Left `mul` by a `k : α` over `[ring α]` is injective, if `k` is not a zero divisor. The typeclass that restricts all terms of `α` to have this property is `no_zero_divisors`. -/ lemma is_left_regular_of_non_zero_divisor [ring α] (k : α) (h : ∀ (x : α), k * x = 0 → x = 0) : is_left_regular k := begin intros x y h', rw ←sub_eq_zero, refine h _ _, rw [mul_sub, sub_eq_zero, h'] end /-- Right `mul` by a `k : α` over `[ring α]` is injective, if `k` is not a zero divisor. The typeclass that restricts all terms of `α` to have this property is `no_zero_divisors`. -/ lemma is_right_regular_of_non_zero_divisor [ring α] (k : α) (h : ∀ (x : α), x * k = 0 → x = 0) : is_right_regular k := begin intros x y h', simp only at h', rw ←sub_eq_zero, refine h _ _, rw [sub_mul, sub_eq_zero, h'] end lemma is_regular_of_ne_zero' [ring α] [no_zero_divisors α] {k : α} (hk : k ≠ 0) : is_regular k := ⟨is_left_regular_of_non_zero_divisor k (λ x h, (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_left hk), is_right_regular_of_non_zero_divisor k (λ x h, (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_right hk)⟩ /-- A ring with no zero divisors is a cancel_monoid_with_zero. Note this is not an instance as it forms a typeclass loop. -/ @[reducible] def no_zero_divisors.to_cancel_monoid_with_zero [ring α] [no_zero_divisors α] : cancel_monoid_with_zero α := { mul_left_cancel_of_ne_zero := λ a b c ha, @is_regular.left _ _ _ (is_regular_of_ne_zero' ha) _ _, mul_right_cancel_of_ne_zero := λ a b c hb, @is_regular.right _ _ _ (is_regular_of_ne_zero' hb) _ _, .. (infer_instance : semiring α) } /-- A domain is a nontrivial ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. This is implemented as a mixin for `ring α`. To obtain an integral domain use `[comm_ring α] [is_domain α]`. -/ @[protect_proj] class is_domain (α : Type u) [ring α] extends no_zero_divisors α, nontrivial α : Prop section is_domain section ring variables [ring α] [is_domain α] @[priority 100] -- see Note [lower instance priority] instance is_domain.to_cancel_monoid_with_zero : cancel_monoid_with_zero α := no_zero_divisors.to_cancel_monoid_with_zero /-- Pullback an `is_domain` instance along an injective function. -/ protected theorem function.injective.is_domain [ring β] (f : β →+* α) (hf : injective f) : is_domain β := { .. pullback_nonzero f f.map_zero f.map_one, .. hf.no_zero_divisors f f.map_zero f.map_mul } end ring section comm_ring variables [comm_ring α] [is_domain α] @[priority 100] -- see Note [lower instance priority] instance is_domain.to_cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero α := { ..comm_semiring.to_comm_monoid_with_zero, ..is_domain.to_cancel_monoid_with_zero } lemma mul_self_eq_mul_self_iff {a b : α} : a * a = b * b ↔ a = b ∨ a = -b := by rw [← sub_eq_zero, mul_self_sub_mul_self, mul_eq_zero, or_comm, sub_eq_zero, add_eq_zero_iff_eq_neg] lemma mul_self_eq_one_iff {a : α} : a * a = 1 ↔ a = 1 ∨ a = -1 := by rw [← mul_self_eq_mul_self_iff, one_mul] /-- In the unit group of an integral domain, a unit is its own inverse iff the unit is one or one's additive inverse. -/ lemma units.inv_eq_self_iff (u : αˣ) : u⁻¹ = u ↔ u = 1 ∨ u = -1 := by { rw inv_eq_iff_mul_eq_one, simp only [units.ext_iff], push_cast, exact mul_self_eq_one_iff } /-- Makes a ring homomorphism from an additive group homomorphism from a commutative ring to an integral domain that commutes with self multiplication, assumes that two is nonzero and one is sent to one. -/ def add_monoid_hom.mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h : ∀ x, f (x * x) = f x * f x) (h_two : (2 : α) ≠ 0) (h_one : f 1 = 1) : β →+* α := { map_one' := h_one, map_mul' := begin intros x y, have hxy := h (x + y), rw [mul_add, add_mul, add_mul, f.map_add, f.map_add, f.map_add, f.map_add, h x, h y, add_mul, mul_add, mul_add, ← sub_eq_zero, add_comm, ← sub_sub, ← sub_sub, ← sub_sub, mul_comm y x, mul_comm (f y) (f x)] at hxy, simp only [add_assoc, add_sub_assoc, add_sub_cancel'_right] at hxy, rw [sub_sub, ← two_mul, ← add_sub_assoc, ← two_mul, ← mul_sub, mul_eq_zero, sub_eq_zero, or_iff_not_imp_left] at hxy, exact hxy h_two, end, ..f } @[simp] lemma add_monoid_hom.coe_fn_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h h_two h_one) : (f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β → α) = f := rfl @[simp] lemma add_monoid_hom.coe_add_monoid_hom_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h h_two h_one) : (f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β →+ α) = f := by {ext, simp} end comm_ring end is_domain namespace semiconj_by @[simp] lemma add_right [distrib R] {a x y x' y' : R} (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x + x') (y + y') := by simp only [semiconj_by, left_distrib, right_distrib, h.eq, h'.eq] @[simp] lemma add_left [distrib R] {a b x y : R} (ha : semiconj_by a x y) (hb : semiconj_by b x y) : semiconj_by (a + b) x y := by simp only [semiconj_by, left_distrib, right_distrib, ha.eq, hb.eq] variables [ring R] {a b x y x' y' : R} lemma neg_right (h : semiconj_by a x y) : semiconj_by a (-x) (-y) := by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm] @[simp] lemma neg_right_iff : semiconj_by a (-x) (-y) ↔ semiconj_by a x y := ⟨λ h, neg_neg x ▸ neg_neg y ▸ h.neg_right, semiconj_by.neg_right⟩ lemma neg_left (h : semiconj_by a x y) : semiconj_by (-a) x y := by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm] @[simp] lemma neg_left_iff : semiconj_by (-a) x y ↔ semiconj_by a x y := ⟨λ h, neg_neg a ▸ h.neg_left, semiconj_by.neg_left⟩ @[simp] lemma neg_one_right (a : R) : semiconj_by a (-1) (-1) := (one_right a).neg_right @[simp] lemma neg_one_left (x : R) : semiconj_by (-1) x x := (semiconj_by.one_left x).neg_left @[simp] lemma sub_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x - x') (y - y') := by simpa only [sub_eq_add_neg] using h.add_right h'.neg_right @[simp] lemma sub_left (ha : semiconj_by a x y) (hb : semiconj_by b x y) : semiconj_by (a - b) x y := by simpa only [sub_eq_add_neg] using ha.add_left hb.neg_left end semiconj_by namespace commute @[simp] theorem add_right [distrib R] {a b c : R} : commute a b → commute a c → commute a (b + c) := semiconj_by.add_right @[simp] theorem add_left [distrib R] {a b c : R} : commute a c → commute b c → commute (a + b) c := semiconj_by.add_left lemma bit0_right [distrib R] {x y : R} (h : commute x y) : commute x (bit0 y) := h.add_right h lemma bit0_left [distrib R] {x y : R} (h : commute x y) : commute (bit0 x) y := h.add_left h lemma bit1_right [semiring R] {x y : R} (h : commute x y) : commute x (bit1 y) := h.bit0_right.add_right (commute.one_right x) lemma bit1_left [semiring R] {x y : R} (h : commute x y) : commute (bit1 x) y := h.bit0_left.add_left (commute.one_left y) variables [ring R] {a b c : R} theorem neg_right : commute a b → commute a (- b) := semiconj_by.neg_right @[simp] theorem neg_right_iff : commute a (-b) ↔ commute a b := semiconj_by.neg_right_iff theorem neg_left : commute a b → commute (- a) b := semiconj_by.neg_left @[simp] theorem neg_left_iff : commute (-a) b ↔ commute a b := semiconj_by.neg_left_iff @[simp] theorem neg_one_right (a : R) : commute a (-1) := semiconj_by.neg_one_right a @[simp] theorem neg_one_left (a : R): commute (-1) a := semiconj_by.neg_one_left a @[simp] theorem sub_right : commute a b → commute a c → commute a (b - c) := semiconj_by.sub_right @[simp] theorem sub_left : commute a c → commute b c → commute (a - b) c := semiconj_by.sub_left end commute
8c695bab1f7aa9a9f37a583d408ad7646116d02f
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/algebra/lie/classical.lean
14d909dd9b5cd8acc2d0f22d5979c99677aa1bd9
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
13,394
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.invertible import data.matrix.basis import data.matrix.dmatrix import algebra.lie.abelian import linear_algebra.matrix.trace import algebra.lie.skew_adjoint /-! # Classical Lie algebras This file is the place to find definitions and basic properties of the classical Lie algebras: * Aₗ = sl(l+1) * Bₗ ≃ so(l+1, l) ≃ so(2l+1) * Cₗ = sp(l) * Dₗ ≃ so(l, l) ≃ so(2l) ## Main definitions * `lie_algebra.special_linear.sl` * `lie_algebra.symplectic.sp` * `lie_algebra.orthogonal.so` * `lie_algebra.orthogonal.so'` * `lie_algebra.orthogonal.so_indefinite_equiv` * `lie_algebra.orthogonal.type_D` * `lie_algebra.orthogonal.type_B` * `lie_algebra.orthogonal.type_D_equiv_so'` * `lie_algebra.orthogonal.type_B_equiv_so'` ## Implementation notes ### Matrices or endomorphisms Given a finite type and a commutative ring, the corresponding square matrices are equivalent to the endomorphisms of the corresponding finite-rank free module as Lie algebras, see `lie_equiv_matrix'`. We can thus define the classical Lie algebras as Lie subalgebras either of matrices or of endomorphisms. We have opted for the former. At the time of writing (August 2020) it is unclear which approach should be preferred so the choice should be assumed to be somewhat arbitrary. ### Diagonal quadratic form or diagonal Cartan subalgebra For the algebras of type `B` and `D`, there are two natural definitions. For example since the the `2l × 2l` matrix: $$ J = \left[\begin{array}{cc} 0_l & 1_l\\ 1_l & 0_l \end{array}\right] $$ defines a symmetric bilinear form equivalent to that defined by the identity matrix `I`, we can define the algebras of type `D` to be the Lie subalgebra of skew-adjoint matrices either for `J` or for `I`. Both definitions have their advantages (in particular the `J`-skew-adjoint matrices define a Lie algebra for which the diagonal matrices form a Cartan subalgebra) and so we provide both. We thus also provide equivalences `type_D_equiv_so'`, `so_indefinite_equiv` which show the two definitions are equivalent. Similarly for the algebras of type `B`. ## Tags classical lie algebra, special linear, symplectic, orthogonal -/ universes u₁ u₂ namespace lie_algebra open matrix open_locale matrix variables (n p q l : Type*) (R : Type u₂) variables [decidable_eq n] [decidable_eq p] [decidable_eq q] [decidable_eq l] variables [comm_ring R] @[simp] lemma matrix_trace_commutator_zero [fintype n] (X Y : matrix n n R) : matrix.trace ⁅X, Y⁆ = 0 := calc _ = matrix.trace (X ⬝ Y) - matrix.trace (Y ⬝ X) : trace_sub _ _ ... = matrix.trace (X ⬝ Y) - matrix.trace (X ⬝ Y) : congr_arg (λ x, _ - x) (matrix.trace_mul_comm Y X) ... = 0 : sub_self _ namespace special_linear /-- The special linear Lie algebra: square matrices of trace zero. -/ def sl [fintype n] : lie_subalgebra R (matrix n n R) := { lie_mem' := λ X Y _ _, linear_map.mem_ker.2 $ matrix_trace_commutator_zero _ _ _ _, ..linear_map.ker (matrix.trace_linear_map n R R) } lemma sl_bracket [fintype n] (A B : sl n R) : ⁅A, B⁆.val = A.val ⬝ B.val - B.val ⬝ A.val := rfl section elementary_basis variables {n} [fintype n] (i j : n) /-- When j ≠ i, the elementary matrices are elements of sl n R, in fact they are part of a natural basis of sl n R. -/ def Eb (h : j ≠ i) : sl n R := ⟨matrix.std_basis_matrix i j (1 : R), show matrix.std_basis_matrix i j (1 : R) ∈ linear_map.ker (matrix.trace_linear_map n R R), from matrix.std_basis_matrix.trace_zero i j (1 : R) h⟩ @[simp] lemma Eb_val (h : j ≠ i) : (Eb R i j h).val = matrix.std_basis_matrix i j 1 := rfl end elementary_basis lemma sl_non_abelian [fintype n] [nontrivial R] (h : 1 < fintype.card n) : ¬is_lie_abelian ↥(sl n R) := begin rcases fintype.exists_pair_of_one_lt_card h with ⟨j, i, hij⟩, let A := Eb R i j hij, let B := Eb R j i hij.symm, intros c, have c' : A.val ⬝ B.val = B.val ⬝ A.val, by { rw [← sub_eq_zero, ← sl_bracket, c.trivial], refl }, simpa [std_basis_matrix, matrix.mul_apply, hij] using congr_fun (congr_fun c' i) i, end end special_linear namespace symplectic /-- The matrix defining the canonical skew-symmetric bilinear form. -/ def J : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 (-1) 1 0 /-- The symplectic Lie algebra: skew-adjoint matrices with respect to the canonical skew-symmetric bilinear form. -/ def sp [fintype l] : lie_subalgebra R (matrix (l ⊕ l) (l ⊕ l) R) := skew_adjoint_matrices_lie_subalgebra (J l R) end symplectic namespace orthogonal /-- The definite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the identity matrix. -/ def so [fintype n] : lie_subalgebra R (matrix n n R) := skew_adjoint_matrices_lie_subalgebra (1 : matrix n n R) @[simp] lemma mem_so [fintype n] (A : matrix n n R) : A ∈ so n R ↔ Aᵀ = -A := begin erw mem_skew_adjoint_matrices_submodule, simp only [matrix.is_skew_adjoint, matrix.is_adjoint_pair, matrix.mul_one, matrix.one_mul], end /-- The indefinite diagonal matrix with `p` 1s and `q` -1s. -/ def indefinite_diagonal : matrix (p ⊕ q) (p ⊕ q) R := matrix.diagonal $ sum.elim (λ _, 1) (λ _, -1) /-- The indefinite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the indefinite diagonal matrix. -/ def so' [fintype p] [fintype q] : lie_subalgebra R (matrix (p ⊕ q) (p ⊕ q) R) := skew_adjoint_matrices_lie_subalgebra $ indefinite_diagonal p q R /-- A matrix for transforming the indefinite diagonal bilinear form into the definite one, provided the parameter `i` is a square root of -1. -/ def Pso (i : R) : matrix (p ⊕ q) (p ⊕ q) R := matrix.diagonal $ sum.elim (λ _, 1) (λ _, i) variables [fintype p] [fintype q] lemma Pso_inv {i : R} (hi : i*i = -1) : (Pso p q R i) * (Pso p q R (-i)) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end /-- There is a constructive inverse of `Pso p q R i`. -/ def invertible_Pso {i : R} (hi : i*i = -1) : invertible (Pso p q R i) := invertible_of_right_inverse _ _ (Pso_inv p q R hi) lemma indefinite_diagonal_transform {i : R} (hi : i*i = -1) : (Pso p q R i)ᵀ ⬝ (indefinite_diagonal p q R) ⬝ (Pso p q R i) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end /-- An equivalence between the indefinite and definite orthogonal Lie algebras, over a ring containing a square root of -1. -/ def so_indefinite_equiv {i : R} (hi : i*i = -1) : so' p q R ≃ₗ⁅R⁆ so (p ⊕ q) R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (indefinite_diagonal p q R) (Pso p q R i) (invertible_Pso p q R hi)).trans, apply lie_equiv.of_eq, ext A, rw indefinite_diagonal_transform p q R hi, refl, end lemma so_indefinite_equiv_apply {i : R} (hi : i*i = -1) (A : so' p q R) : (so_indefinite_equiv p q R hi A : matrix (p ⊕ q) (p ⊕ q) R) = (Pso p q R i)⁻¹ ⬝ (A : matrix (p ⊕ q) (p ⊕ q) R) ⬝ (Pso p q R i) := by erw [lie_equiv.trans_apply, lie_equiv.of_eq_apply, skew_adjoint_matrices_lie_subalgebra_equiv_apply] /-- A matrix defining a canonical even-rank symmetric bilinear form. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 0 1 ] [ 1 0 ] -/ def JD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 1 1 0 /-- The classical Lie algebra of type D as a Lie subalgebra of matrices associated to the matrix `JD`. -/ def type_D [fintype l] := skew_adjoint_matrices_lie_subalgebra (JD l R) /-- A matrix transforming the bilinear form defined by the matrix `JD` into a split-signature diagonal matrix. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 1 -1 ] [ 1 1 ] -/ def PD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 1 (-1) 1 1 /-- The split-signature diagonal matrix. -/ def S := indefinite_diagonal l l R lemma S_as_blocks : S l R = matrix.from_blocks 1 0 0 (-1) := begin rw [← matrix.diagonal_one, matrix.diagonal_neg, matrix.from_blocks_diagonal], refl, end lemma JD_transform [fintype l] : (PD l R)ᵀ ⬝ (JD l R) ⬝ (PD l R) = (2 : R) • (S l R) := begin have h : (PD l R)ᵀ ⬝ (JD l R) = matrix.from_blocks 1 1 1 (-1) := by { simp [PD, JD, matrix.from_blocks_transpose, matrix.from_blocks_multiply], }, erw [h, S_as_blocks, matrix.from_blocks_multiply, matrix.from_blocks_smul], congr; simp [two_smul], end lemma PD_inv [fintype l] [invertible (2 : R)] : (PD l R) * (⅟(2 : R) • (PD l R)ᵀ) = 1 := begin have h : ⅟(2 : R) • (1 : matrix l l R) + ⅟(2 : R) • 1 = 1 := by rw [← smul_add, ← (two_smul R _), smul_smul, inv_of_mul_self, one_smul], erw [matrix.from_blocks_transpose, matrix.from_blocks_smul, matrix.mul_eq_mul, matrix.from_blocks_multiply], simp [h], end instance invertible_PD [fintype l] [invertible (2 : R)] : invertible (PD l R) := invertible_of_right_inverse _ _ (PD_inv l R) /-- An equivalence between two possible definitions of the classical Lie algebra of type D. -/ def type_D_equiv_so' [fintype l] [invertible (2 : R)] : type_D l R ≃ₗ⁅R⁆ so' l l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JD l R) (PD l R) (by apply_instance)).trans, apply lie_equiv.of_eq, ext A, rw [JD_transform, ← coe_unit_of_invertible (2 : R), ←units.smul_def, lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], refl, end /-- A matrix defining a canonical odd-rank symmetric bilinear form. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 2 0 0 ] [ 0 0 1 ] [ 0 1 0 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def JB := matrix.from_blocks ((2 : R) • 1 : matrix unit unit R) 0 0 (JD l R) /-- The classical Lie algebra of type B as a Lie subalgebra of matrices associated to the matrix `JB`. -/ def type_B [fintype l] := skew_adjoint_matrices_lie_subalgebra(JB l R) /-- A matrix transforming the bilinear form defined by the matrix `JB` into an almost-split-signature diagonal matrix. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 1 0 0 ] [ 0 1 -1 ] [ 0 1 1 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def PB := matrix.from_blocks (1 : matrix unit unit R) 0 0 (PD l R) variable [fintype l] lemma PB_inv [invertible (2 : R)] : PB l R * matrix.from_blocks 1 0 0 (⅟(PD l R)) = 1 := begin rw [PB, matrix.mul_eq_mul, matrix.from_blocks_multiply, matrix.mul_inv_of_self], simp only [matrix.mul_zero, matrix.mul_one, matrix.zero_mul, zero_add, add_zero, matrix.from_blocks_one] end instance invertible_PB [invertible (2 : R)] : invertible (PB l R) := invertible_of_right_inverse _ _ (PB_inv l R) lemma JB_transform : (PB l R)ᵀ ⬝ (JB l R) ⬝ (PB l R) = (2 : R) • matrix.from_blocks 1 0 0 (S l R) := by simp [PB, JB, JD_transform, matrix.from_blocks_transpose, matrix.from_blocks_multiply, matrix.from_blocks_smul] lemma indefinite_diagonal_assoc : indefinite_diagonal (unit ⊕ l) l R = matrix.reindex_lie_equiv (equiv.sum_assoc unit l l).symm (matrix.from_blocks 1 0 0 (indefinite_diagonal l l R)) := begin ext i j, rcases i with ⟨⟨i₁ | i₂⟩ | i₃⟩; rcases j with ⟨⟨j₁ | j₂⟩ | j₃⟩; simp only [indefinite_diagonal, matrix.diagonal, equiv.sum_assoc_apply_inl_inl, matrix.reindex_lie_equiv_apply, matrix.submatrix_apply, equiv.symm_symm, matrix.reindex_apply, sum.elim_inl, if_true, eq_self_iff_true, matrix.one_apply_eq, matrix.from_blocks_apply₁₁, dmatrix.zero_apply, equiv.sum_assoc_apply_inl_inr, if_false, matrix.from_blocks_apply₁₂, matrix.from_blocks_apply₂₁, matrix.from_blocks_apply₂₂, equiv.sum_assoc_apply_inr, sum.elim_inr]; congr, end /-- An equivalence between two possible definitions of the classical Lie algebra of type B. -/ def type_B_equiv_so' [invertible (2 : R)] : type_B l R ≃ₗ⁅R⁆ so' (unit ⊕ l) l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JB l R) (PB l R) (by apply_instance)).trans, symmetry, apply (skew_adjoint_matrices_lie_subalgebra_equiv_transpose (indefinite_diagonal (unit ⊕ l) l R) (matrix.reindex_alg_equiv _ (equiv.sum_assoc punit l l)) (matrix.transpose_reindex _ _)).trans, apply lie_equiv.of_eq, ext A, rw [JB_transform, ← coe_unit_of_invertible (2 : R), ←units.smul_def, lie_subalgebra.mem_coe, lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], simpa [indefinite_diagonal_assoc], end end orthogonal end lie_algebra
82c4292bab770810df94e1aec12757a165e7d60c
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/geometry/manifold/times_cont_mdiff_map.lean
7cc5686be789ad79bf18d95b0cd710a77782c65a
[ "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
3,944
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.times_cont_mdiff import topology.continuous_function.basic /-! # Smooth bundled map In this file we define the type `times_cont_mdiff_map` of `n` times continuously differentiable bundled maps. -/ 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] {H' : Type*} [topological_space H'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') (M : Type*) [topological_space M] [charted_space H M] (M' : Type*) [topological_space M'] [charted_space 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''] [charted_space H'' M''] (n : with_top ℕ) /-- Bundled `n` times continuously differentiable maps. -/ @[protect_proj] structure times_cont_mdiff_map := (to_fun : M → M') (times_cont_mdiff_to_fun : times_cont_mdiff I I' n to_fun) /-- Bundled smooth maps. -/ @[reducible] def smooth_map := times_cont_mdiff_map I I' M M' ⊤ localized "notation `C^` n `⟮` I `, ` M `; ` I' `, ` M' `⟯` := times_cont_mdiff_map I I' M M' n" in manifold localized "notation `C^` n `⟮` I `, ` M `; ` k `⟯` := times_cont_mdiff_map I (model_with_corners_self k k) M k n" in manifold open_locale manifold namespace times_cont_mdiff_map variables {I} {I'} {M} {M'} {n} instance : has_coe_to_fun C^n⟮I, M; I', M'⟯ := ⟨_, times_cont_mdiff_map.to_fun⟩ instance : has_coe C^n⟮I, M; I', M'⟯ C(M, M') := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff_to_fun.continuous⟩⟩ attribute [to_additive_ignore_args 21] times_cont_mdiff_map times_cont_mdiff_map.has_coe_to_fun times_cont_mdiff_map.continuous_map.has_coe variables {f g : C^n⟮I, M; I', M'⟯} @[simp] lemma coe_fn_mk (f : M → M') (hf : times_cont_mdiff I I' n f) : (mk f hf : M → M') = f := rfl protected lemma times_cont_mdiff (f : C^n⟮I, M; I', M'⟯) : times_cont_mdiff I I' n f := f.times_cont_mdiff_to_fun protected lemma smooth (f : C^∞⟮I, M; I', M'⟯) : smooth I I' f := f.times_cont_mdiff_to_fun protected lemma mdifferentiable' (f : C^n⟮I, M; I', M'⟯) (hn : 1 ≤ n) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable hn protected lemma mdifferentiable (f : C^∞⟮I, M; I', M'⟯) : mdifferentiable I I' f := f.times_cont_mdiff.mdifferentiable le_top protected lemma mdifferentiable_at (f : C^∞⟮I, M; I', M'⟯) {x} : mdifferentiable_at I I' f x := f.mdifferentiable x lemma coe_inj ⦃f g : C^n⟮I, M; I', M'⟯⦄ (h : (f : M → M') = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext h /-- The identity as a smooth map. -/ def id : C^n⟮I, M; I, M⟯ := ⟨id, times_cont_mdiff_id⟩ /-- The composition of smooth maps, as a smooth map. -/ def comp (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) : C^n⟮I, M; I'', M''⟯ := { to_fun := λ a, f (g a), times_cont_mdiff_to_fun := f.times_cont_mdiff_to_fun.comp g.times_cont_mdiff_to_fun, } @[simp] lemma comp_apply (f : C^n⟮I', M'; I'', M''⟯) (g : C^n⟮I, M; I', M'⟯) (x : M) : f.comp g x = f (g x) := rfl instance [inhabited M'] : inhabited C^n⟮I, M; I', M'⟯ := ⟨⟨λ _, default _, times_cont_mdiff_const⟩⟩ /-- Constant map as a smooth map -/ def const (y : M') : C^n⟮I, M; I', M'⟯ := ⟨λ x, y, times_cont_mdiff_const⟩ end times_cont_mdiff_map instance continuous_linear_map.has_coe_to_times_cont_mdiff_map : has_coe (E →L[𝕜] E') C^n⟮𝓘(𝕜, E), E; 𝓘(𝕜, E'), E'⟯ := ⟨λ f, ⟨f.to_fun, f.times_cont_mdiff⟩⟩
fb810f53f87ac999bba5fcb8ff048eb34276a5c6
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Level.lean
e519e7efc32478d6e06eed5d6a11087ddd602f15
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,740
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Std.Data.HashMap import Std.Data.HashSet import Std.Data.PersistentHashMap import Std.Data.PersistentHashSet import Lean.Data.Name import Lean.Data.Format def Nat.imax (n m : Nat) : Nat := if m = 0 then 0 else Nat.max n m namespace Lean /-- Cached hash code, cached results, and other data for `Level`. hash : 32-bits hasMVar : 1-bit hasParam : 1-bit depth : 24-bits -/ def Level.Data := UInt64 instance : Inhabited Level.Data := inferInstanceAs (Inhabited UInt64) def Level.Data.hash (c : Level.Data) : USize := c.toUInt32.toUSize instance : BEq Level.Data := ⟨fun (a b : UInt64) => a == b⟩ def Level.Data.depth (c : Level.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Level.Data.hasMVar (c : Level.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Level.Data.hasParam (c : Level.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Level.mkData (h : USize) (depth : Nat) (hasMVar hasParam : Bool) : Level.Data := if depth > Nat.pow 2 24 - 1 then panic! "universe level depth is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasMVar.toUInt64.shiftLeft 32 + hasParam.toUInt64.shiftLeft 33 + depth.toUInt64.shiftLeft 40 r open Level inductive Level := | zero : Data → Level | succ : Level → Data → Level | max : Level → Level → Data → Level | imax : Level → Level → Data → Level | param : Name → Data → Level | mvar : Name → Data → Level namespace Level @[inline] def data : Level → Data | zero d => d | mvar _ d => d | param _ d => d | succ _ d => d | max _ _ d => d | imax _ _ d => d protected def hash (u : Level) : USize := u.data.hash instance : Hashable Level := ⟨Level.hash⟩ def depth (u : Level) : Nat := u.data.depth.toNat def hasMVar (u : Level) : Bool := u.data.hasMVar def hasParam (u : Level) : Bool := u.data.hasParam @[export lean_level_hash] def hashEx : Level → USize := Level.hash @[export lean_level_has_mvar] def hasMVarEx : Level → Bool := hasMVar @[export lean_level_has_param] def hasParamEx : Level → Bool := hasParam @[export lean_level_depth] def depthEx (u : Level) : UInt32 := u.data.depth end Level def levelZero := Level.zero $ mkData 2221 0 false false def mkLevelMVar (mvarId : Name) := Level.mvar mvarId $ mkData (mixHash 2237 $ hash mvarId) 0 true false def mkLevelParam (name : Name) := Level.param name $ mkData (mixHash 2239 $ hash name) 0 false true def mkLevelSucc (u : Level) := Level.succ u $ mkData (mixHash 2243 $ hash u) (u.depth + 1) u.hasMVar u.hasParam def mkLevelMax (u v : Level) := Level.max u v $ mkData (mixHash 2251 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def mkLevelIMax (u v : Level) := Level.imax u v $ mkData (mixHash 2267 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def levelOne := mkLevelSucc levelZero @[export lean_level_mk_zero] def mkLevelZeroEx : Unit → Level := fun _ => levelZero @[export lean_level_mk_succ] def mkLevelSuccEx : Level → Level := mkLevelSucc @[export lean_level_mk_mvar] def mkLevelMVarEx : Name → Level := mkLevelMVar @[export lean_level_mk_param] def mkLevelParamEx : Name → Level := mkLevelParam @[export lean_level_mk_max] def mkLevelMaxEx : Level → Level → Level := mkLevelMax @[export lean_level_mk_imax] def mkLevelIMaxEx : Level → Level → Level := mkLevelIMax namespace Level instance : Inhabited Level := ⟨levelZero⟩ def isZero : Level → Bool | zero _ => true | _ => false def isSucc : Level → Bool | succ .. => true | _ => false def isMax : Level → Bool | max .. => true | _ => false def isIMax : Level → Bool | imax .. => true | _ => false def isMaxIMax : Level → Bool | max .. => true | imax .. => true | _ => false def isParam : Level → Bool | param .. => true | _ => false def isMVar : Level → Bool | mvar .. => true | _ => false def mvarId! : Level → Name | mvar mvarId _ => mvarId | _ => panic! "metavariable expected" /-- If result is true, then forall assignments `A` which assigns all parameters and metavariables occuring in `l`, `l[A] != zero` -/ def isNeverZero : Level → Bool | zero _ => false | param .. => false | mvar .. => false | succ .. => true | max l₁ l₂ _ => isNeverZero l₁ || isNeverZero l₂ | imax l₁ l₂ _ => isNeverZero l₂ def ofNat : Nat → Level | 0 => levelZero | n+1 => mkLevelSucc (ofNat n) def addOffsetAux : Nat → Level → Level | 0, u => u | (n+1), u => addOffsetAux n (mkLevelSucc u) def addOffset (u : Level) (n : Nat) : Level := u.addOffsetAux n def isExplicit : Level → Bool | zero _ => true | succ u _ => !u.hasMVar && !u.hasParam && isExplicit u | _ => false def getOffsetAux : Level → Nat → Nat | succ u _, r => getOffsetAux u (r+1) | _, r => r def getOffset (lvl : Level) : Nat := getOffsetAux lvl 0 def getLevelOffset : Level → Level | succ u _ => getLevelOffset u | u => u def toNat (lvl : Level) : Option Nat := match lvl.getLevelOffset with | zero _ => lvl.getOffset | _ => none @[extern "lean_level_eq"] protected constant beq (a : @& Level) (b : @& Level) : Bool instance : BEq Level := ⟨Level.beq⟩ /-- `occurs u l` return `true` iff `u` occurs in `l`. -/ def occurs : Level → Level → Bool | u, v@(succ v₁ _) => u == v || occurs u v₁ | u, v@(max v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v@(imax v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v => u == v def ctorToNat : Level → Nat | zero .. => 0 | param .. => 1 | mvar .. => 2 | succ .. => 3 | max .. => 4 | imax .. => 5 /- TODO: use well founded recursion. -/ partial def normLtAux : Level → Nat → Level → Nat → Bool | succ l₁ _, k₁, l₂, k₂ => normLtAux l₁ (k₁+1) l₂ k₂ | l₁, k₁, succ l₂ _, k₂ => normLtAux l₁ k₁ l₂ (k₂+1) | l₁@(max l₁₁ l₁₂ _), k₁, l₂@(max l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | l₁@(imax l₁₁ l₁₂ _), k₁, l₂@(imax l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | param n₁ _, k₁, param n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.lt n₁ n₂ -- use Name.lt because it is lexicographical | mvar n₁ _, k₁, mvar n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.quickLt n₁ n₂ -- metavariables are temporary, the actual order doesn't matter | l₁, k₁, l₂, k₂ => if l₁ == l₂ then k₁ < k₂ else ctorToNat l₁ < ctorToNat l₂ /-- A total order on level expressions that has the following properties - `succ l` is an immediate successor of `l`. - `zero` is the minimal element. This total order is used in the normalization procedure. -/ def normLt (l₁ l₂ : Level) : Bool := normLtAux l₁ 0 l₂ 0 private def isAlreadyNormalizedCheap : Level → Bool | zero _ => true | param _ _ => true | mvar _ _ => true | succ u _ => isAlreadyNormalizedCheap u | _ => false /- Auxiliary function used at `normalize` -/ private def mkIMaxAux : Level → Level → Level | _, u@(zero _) => u | zero _, u => u | u₁, u₂ => if u₁ == u₂ then u₁ else mkLevelIMax u₁ u₂ /- Auxiliary function used at `normalize` -/ @[specialize] private partial def getMaxArgsAux (normalize : Level → Level) : Level → Bool → Array Level → Array Level | max l₁ l₂ _, alreadyNormalized, lvls => getMaxArgsAux normalize l₂ alreadyNormalized (getMaxArgsAux normalize l₁ alreadyNormalized lvls) | l, false, lvls => getMaxArgsAux normalize (normalize l) true lvls | l, true, lvls => lvls.push l private def accMax (result : Level) (prev : Level) (offset : Nat) : Level := if result.isZero then prev.addOffset offset else mkLevelMax result (prev.addOffset offset) /- Auxiliary function used at `normalize`. Remarks: - `lvls` are sorted using `normLt` - `extraK` is the outter offset of the `max` term. We will push it inside. - `i` is the current array index - `prev + prevK` is the "previous" level that has not been added to `result` yet. - `result` is the accumulator -/ private partial def mkMaxAux (lvls : Array Level) (extraK : Nat) (i : Nat) (prev : Level) (prevK : Nat) (result : Level) : Level := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ let curr := lvl.getLevelOffset let currK := lvl.getOffset if curr == prev then mkMaxAux lvls extraK (i+1) curr currK result else mkMaxAux lvls extraK (i+1) curr currK (accMax result prev (extraK + prevK)) else accMax result prev (extraK + prevK) /- Auxiliary function for `normalize`. It assumes `lvls` has been sorted using `normLt`. It finds the first position that is not an explicit universe. -/ private partial def skipExplicit (lvls : Array Level) (i : Nat) : Nat := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getLevelOffset.isZero then skipExplicit lvls (i+1) else i else i /- Auxiliary function for `normalize`. `maxExplicit` is the maximum explicit universe level at `lvls`. Return true if it finds a level with offset ≥ maxExplicit. `i` starts at the first non explict level. It assumes `lvls` has been sorted using `normLt`. -/ private partial def isExplicitSubsumedAux (lvls : Array Level) (maxExplicit : Nat) (i : Nat) : Bool := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getOffset ≥ maxExplicit then true else isExplicitSubsumedAux lvls maxExplicit (i+1) else false /- Auxiliary function for `normalize`. See `isExplicitSubsumedAux` -/ private def isExplicitSubsumed (lvls : Array Level) (firstNonExplicit : Nat) : Bool := if firstNonExplicit == 0 then false else let max := (lvls.get! (firstNonExplicit - 1)).getOffset; isExplicitSubsumedAux lvls max firstNonExplicit partial def normalize (l : Level) : Level := if isAlreadyNormalizedCheap l then l else let k := l.getOffset let u := l.getLevelOffset match u with | max l₁ l₂ _ => let lvls := getMaxArgsAux normalize l₁ false #[] let lvls := getMaxArgsAux normalize l₂ false lvls let lvls := lvls.qsort normLt let firstNonExplicit := skipExplicit lvls 0 let i := if isExplicitSubsumed lvls firstNonExplicit then firstNonExplicit else firstNonExplicit - 1 let lvl₁ := lvls[i] let prev := lvl₁.getLevelOffset let prevK := lvl₁.getOffset mkMaxAux lvls k (i+1) prev prevK levelZero | imax l₁ l₂ _ => if l₂.isNeverZero then addOffset (normalize (mkLevelMax l₁ l₂)) k else let l₁ := normalize l₁ let l₂ := normalize l₂ addOffset (mkIMaxAux l₁ l₂) k | _ => unreachable! /- Return true if `u` and `v` denote the same level. Check is currently incomplete. -/ def isEquiv (u v : Level) : Bool := u == v || u.normalize == v.normalize /-- Reduce (if possible) universe level by 1 -/ def dec : Level → Option Level | zero _ => none | param _ _ => none | mvar _ _ => none | succ l _ => l | max l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Remark: `mkLevelMax` in the following line is not a typo. If `dec l₂` succeeds, then `imax l₁ l₂` is equivalent to `max l₁ l₂`. -/ | imax l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Level to Format -/ namespace LevelToFormat inductive Result := | leaf : Format → Result | num : Nat → Result | offset : Result → Nat → Result | maxNode : List Result → Result | imaxNode : List Result → Result def Result.succ : Result → Result | Result.offset f k => Result.offset f (k+1) | Result.num k => Result.num (k+1) | f => Result.offset f 1 def Result.max : Result → Result → Result | f, Result.maxNode Fs => Result.maxNode (f::Fs) | f₁, f₂ => Result.maxNode [f₁, f₂] def Result.imax : Result → Result → Result | f, Result.imaxNode Fs => Result.imaxNode (f::Fs) | f₁, f₂ => Result.imaxNode [f₁, f₂] def parenIfFalse : Format → Bool → Format | f, true => f | f, false => f.paren @[specialize] private def formatLst (fmt : Result → Format) : List Result → Format | [] => Format.nil | r::rs => Format.line ++ fmt r ++ formatLst fmt rs partial def Result.format : Result → Bool → Format | Result.leaf f, _ => f | Result.num k, _ => toString k | Result.offset f 0, r => format f r | Result.offset f (k+1), r => let f' := format f false; parenIfFalse (f' ++ "+" ++ fmt (k+1)) r | Result.maxNode fs, r => parenIfFalse (Format.group $ "max" ++ formatLst (fun r => format r false) fs) r | Result.imaxNode fs, r => parenIfFalse (Format.group $ "imax" ++ formatLst (fun r => format r false) fs) r def toResult : Level → Result | zero _ => Result.num 0 | succ l _ => Result.succ (toResult l) | max l₁ l₂ _ => Result.max (toResult l₁) (toResult l₂) | imax l₁ l₂ _ => Result.imax (toResult l₁) (toResult l₂) | param n _ => Result.leaf (fmt n) | mvar n _ => let n := n.replacePrefix `_uniq `u; Result.leaf ("?" ++ fmt n) end LevelToFormat protected def format (l : Level) : Format := (LevelToFormat.toResult l).format true instance : ToFormat Level := ⟨Level.format⟩ instance : ToString Level := ⟨Format.pretty ∘ Level.format⟩ end Level /- Similar to `mkLevelMax`, but applies cheap simplifications -/ @[export lean_level_mk_max_simp] def mkLevelMax' (u v : Level) : Level := let subsumes (u v : Level) : Bool := if v.isExplicit && u.getOffset ≥ v.getOffset then true else match u with | Level.max u₁ u₂ _ => v == u₁ || v == u₂ | _ => false if u == v then u else if u.isZero then v else if v.isZero then u else if subsumes u v then u else if subsumes v u then v else if u.getLevelOffset == v.getLevelOffset then if u.getOffset ≥ v.getOffset then u else v else mkLevelMax u v /- Similar to `mkLevelIMax`, but applies cheap simplifications -/ @[export lean_level_mk_imax_simp] def mkLevelIMax' (u v : Level) : Level := if v.isNeverZero then mkLevelMax' u v else if v.isZero then v else if u.isZero then v else if u == v then u else mkLevelIMax u v namespace Level /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_level_update_succ"] def updateSucc (lvl : Level) (newLvl : Level) (h : lvl.isSucc = true) : Level := mkLevelSucc newLvl @[inline] def updateSucc! (lvl : Level) (newLvl : Level) : Level := match lvl with | succ lvl d => updateSucc (succ lvl d) newLvl rfl | _ => panic! "succ level expected" @[extern "lean_level_update_max"] def updateMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isMax = true) : Level := mkLevelMax' newLhs newRhs @[inline] def updateMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | max lhs rhs d => updateMax (max lhs rhs d) newLhs newRhs rfl | _ => panic! "max level expected" @[extern "lean_level_update_imax"] def updateIMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isIMax = true) : Level := mkLevelIMax' newLhs newRhs @[inline] def updateIMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | imax lhs rhs d => updateIMax (imax lhs rhs d) newLhs newRhs rfl | _ => panic! "imax level expected" def mkNaryMax : List Level → Level | [] => levelZero | [u] => u | u::us => mkLevelMax' u (mkNaryMax us) /- Level to Format -/ @[specialize] def instantiateParams (s : Name → Option Level) : Level → Level | u@(zero _) => u | u@(succ v _) => if u.hasParam then u.updateSucc! (instantiateParams s v) else u | u@(max v₁ v₂ _) => if u.hasParam then u.updateMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(imax v₁ v₂ _) => if u.hasParam then u.updateIMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(param n _) => match s n with | some u' => u' | none => u | u => u end Level open Std (HashMap HashSet PHashMap PHashSet) abbrev LevelMap (α : Type) := HashMap Level α abbrev PersistentLevelMap (α : Type) := PHashMap Level α abbrev LevelSet := HashSet Level abbrev PersistentLevelSet := PHashSet Level abbrev PLevelSet := PersistentLevelSet end Lean abbrev Nat.toLevel (n : Nat) : Lean.Level := Lean.Level.ofNat n
2cb1318221526703a985d988313f0abe1bf28dbd
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/field_theory/perfect_closure.lean
c2f09901e477a34e3649860cce367387e59a3df1
[ "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
17,380
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.char_p.basic import data.equiv.ring import algebra.group_with_zero.power import algebra.iterate_hom /-! # The perfect closure of a field -/ universes u v open function section defs variables (R : Type u) [comm_semiring R] (p : ℕ) [fact p.prime] [char_p R p] /-- A perfect ring is a ring of characteristic p that has p-th root. -/ class perfect_ring : Type u := (pth_root' : R → R) (frobenius_pth_root' : ∀ x, frobenius R p (pth_root' x) = x) (pth_root_frobenius' : ∀ x, pth_root' (frobenius R p x) = x) /-- Frobenius automorphism of a perfect ring. -/ def frobenius_equiv [perfect_ring R p] : R ≃+* R := { inv_fun := perfect_ring.pth_root' p, left_inv := perfect_ring.pth_root_frobenius', right_inv := perfect_ring.frobenius_pth_root', .. frobenius R p } /-- `p`-th root of an element in a `perfect_ring` as a `ring_hom`. -/ def pth_root [perfect_ring R p] : R →+* R := (frobenius_equiv R p).symm end defs section variables {R : Type u} [comm_semiring R] {S : Type v} [comm_semiring S] (f : R →* S) (g : R →+* S) {p : ℕ} [fact p.prime] [char_p R p] [perfect_ring R p] [char_p S p] [perfect_ring S p] @[simp] lemma coe_frobenius_equiv : ⇑(frobenius_equiv R p) = frobenius R p := rfl @[simp] lemma coe_frobenius_equiv_symm : ⇑(frobenius_equiv R p).symm = pth_root R p := rfl @[simp] theorem frobenius_pth_root (x : R) : frobenius R p (pth_root R p x) = x := (frobenius_equiv R p).apply_symm_apply x @[simp] theorem pth_root_pow_p (x : R) : pth_root R p x ^ p = x := frobenius_pth_root x @[simp] theorem pth_root_frobenius (x : R) : pth_root R p (frobenius R p x) = x := (frobenius_equiv R p).symm_apply_apply x @[simp] theorem pth_root_pow_p' (x : R) : pth_root R p (x ^ p) = x := pth_root_frobenius x theorem left_inverse_pth_root_frobenius : left_inverse (pth_root R p) (frobenius R p) := pth_root_frobenius theorem right_inverse_pth_root_frobenius : function.right_inverse (pth_root R p) (frobenius R p) := frobenius_pth_root theorem commute_frobenius_pth_root : function.commute (frobenius R p) (pth_root R p) := λ x, (frobenius_pth_root x).trans (pth_root_frobenius x).symm theorem eq_pth_root_iff {x y : R} : x = pth_root R p y ↔ frobenius R p x = y := (frobenius_equiv R p).to_equiv.eq_symm_apply theorem pth_root_eq_iff {x y : R} : pth_root R p x = y ↔ x = frobenius R p y := (frobenius_equiv R p).to_equiv.symm_apply_eq theorem monoid_hom.map_pth_root (x : R) : f (pth_root R p x) = pth_root S p (f x) := eq_pth_root_iff.2 $ by rw [← f.map_frobenius, frobenius_pth_root] theorem monoid_hom.map_iterate_pth_root (x : R) (n : ℕ) : f (pth_root R p^[n] x) = (pth_root S p^[n] (f x)) := semiconj.iterate_right f.map_pth_root n x theorem ring_hom.map_pth_root (x : R) : g (pth_root R p x) = pth_root S p (g x) := g.to_monoid_hom.map_pth_root x theorem ring_hom.map_iterate_pth_root (x : R) (n : ℕ) : g (pth_root R p^[n] x) = (pth_root S p^[n] (g x)) := g.to_monoid_hom.map_iterate_pth_root x n variables (p) lemma injective_pow_p {x y : R} (hxy : x ^ p = y ^ p) : x = y := left_inverse_pth_root_frobenius.injective hxy end section variables (K : Type u) [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p] /-- `perfect_closure K p` is the quotient by this relation. -/ @[mk_iff] inductive perfect_closure.r : (ℕ × K) → (ℕ × K) → Prop | intro : ∀ n x, perfect_closure.r (n, x) (n+1, frobenius K p x) /-- The perfect closure is the smallest extension that makes frobenius surjective. -/ def perfect_closure : Type u := quot (perfect_closure.r K p) end namespace perfect_closure variables (K : Type u) section ring variables [comm_ring K] (p : ℕ) [fact p.prime] [char_p K p] /-- Constructor for `perfect_closure`. -/ def mk (x : ℕ × K) : perfect_closure K p := quot.mk (r K p) x @[simp] lemma quot_mk_eq_mk (x : ℕ × K) : (quot.mk (r K p) x : perfect_closure K p) = mk K p x := rfl variables {K p} /-- Lift a function `ℕ × K → L` to a function on `perfect_closure K p`. -/ @[elab_as_eliminator] def lift_on {L : Type*} (x : perfect_closure K p) (f : ℕ × K → L) (hf : ∀ x y, r K p x y → f x = f y) : L := quot.lift_on x f hf @[simp] lemma lift_on_mk {L : Sort*} (f : ℕ × K → L) (hf : ∀ x y, r K p x y → f x = f y) (x : ℕ × K) : (mk K p x).lift_on f hf = f x := rfl @[elab_as_eliminator] lemma induction_on (x : perfect_closure K p) {q : perfect_closure K p → Prop} (h : ∀ x, q (mk K p x)) : q x := quot.induction_on x h variables (K p) private lemma mul_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) : mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) * ((frobenius K p)^[x1.1] y.2)) = mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) * ((frobenius K p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_mul, nat.succ_add]; apply r.intro end private lemma mul_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) : mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) * ((frobenius K p)^[x.1] y1.2)) = mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) * ((frobenius K p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_mul]; apply r.intro end instance : has_mul (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2))) (mul_aux_right K p x)) (λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y, mul_aux_left K p x1 x2 y H)⟩ @[simp] lemma mk_mul_mk (x y : ℕ × K) : mk K p x * mk K p y = mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) * ((frobenius K p)^[x.1] y.2)) := rfl instance : comm_monoid (perfect_closure K p) := { mul_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [add_assoc, mul_assoc, ring_hom.iterate_map_mul, ← iterate_add_apply, add_comm, add_left_comm], one := mk K p (0, 1), one_mul := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_one, iterate_zero_apply, one_mul, zero_add]), mul_one := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_one, iterate_zero_apply, mul_one, add_zero]), mul_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm, mul_comm])), .. (infer_instance : has_mul (perfect_closure K p)) } lemma one_def : (1 : perfect_closure K p) = mk K p (0, 1) := rfl instance : inhabited (perfect_closure K p) := ⟨1⟩ private lemma add_aux_left (x1 x2 y : ℕ × K) (H : r K p x1 x2) : mk K p (x1.1 + y.1, ((frobenius K p)^[y.1] x1.2) + ((frobenius K p)^[x1.1] y.2)) = mk K p (x2.1 + y.1, ((frobenius K p)^[y.1] x2.2) + ((frobenius K p)^[x2.1] y.2)) := match x1, x2, H with | _, _, r.intro n x := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_add, nat.succ_add]; apply r.intro end private lemma add_aux_right (x y1 y2 : ℕ × K) (H : r K p y1 y2) : mk K p (x.1 + y1.1, ((frobenius K p)^[y1.1] x.2) + ((frobenius K p)^[x.1] y1.2)) = mk K p (x.1 + y2.1, ((frobenius K p)^[y2.1] x.2) + ((frobenius K p)^[x.1] y2.2)) := match y1, y2, H with | _, _, r.intro n y := quot.sound $ by rw [← iterate_succ_apply, iterate_succ', iterate_succ', ← frobenius_add]; apply r.intro end instance : has_add (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.lift (λ y:ℕ×K, mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2))) (add_aux_right K p x)) (λ x1 x2 (H : r K p x1 x2), funext $ λ e, quot.induction_on e $ λ y, add_aux_left K p x1 x2 y H)⟩ @[simp] lemma mk_add_mk (x y : ℕ × K) : mk K p x + mk K p y = mk K p (x.1 + y.1, ((frobenius K p)^[y.1] x.2) + ((frobenius K p)^[x.1] y.2)) := rfl instance : has_neg (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, mk K p (x.1, -x.2)) (λ x y (H : r K p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by rw ← frobenius_neg; apply r.intro end)⟩ @[simp] lemma neg_mk (x : ℕ × K) : - mk K p x = mk K p (x.1, -x.2) := rfl instance : has_zero (perfect_closure K p) := ⟨mk K p (0, 0)⟩ lemma zero_def : (0 : perfect_closure K p) = mk K p (0, 0) := rfl theorem mk_zero (n : ℕ) : mk K p (n, 0) = 0 := by induction n with n ih; [refl, rw ← ih]; symmetry; apply quot.sound; have := r.intro n (0:K); rwa [frobenius_zero K p] at this theorem r.sound (m n : ℕ) (x y : K) (H : frobenius K p^[m] x = y) : mk K p (n, x) = mk K p (m + n, y) := by subst H; induction m with m ih; [simp only [zero_add, iterate_zero_apply], rw [ih, nat.succ_add, iterate_succ']]; apply quot.sound; apply r.intro instance : comm_ring (perfect_closure K p) := { add_assoc := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_add, ← iterate_add_apply, add_assoc, add_comm s _], zero := 0, zero_add := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, zero_add]), add_zero := λ e, quot.induction_on e (λ ⟨n, x⟩, congr_arg (quot.mk _) $ by simp only [ring_hom.iterate_map_zero, iterate_zero_apply, add_zero]), sub_eq_add_neg := λ a b, rfl, add_left_neg := λ e, by exact quot.induction_on e (λ ⟨n, x⟩, by simp only [quot_mk_eq_mk, neg_mk, mk_add_mk, ring_hom.iterate_map_neg, add_left_neg, mk_zero]), add_comm := λ e f, quot.induction_on e (λ ⟨m, x⟩, quot.induction_on f (λ ⟨n, y⟩, congr_arg (quot.mk _) $ by simp only [add_comm])), left_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm, add_left_comm]; apply r.sound; simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add, ← iterate_add_apply, mul_add, add_comm, add_left_comm], right_distrib := λ e f g, quot.induction_on e $ λ ⟨m, x⟩, quot.induction_on f $ λ ⟨n, y⟩, quot.induction_on g $ λ ⟨s, z⟩, show quot.mk _ _ = quot.mk _ _, by simp only [add_assoc, add_comm _ s, add_left_comm _ s]; apply r.sound; simp only [ring_hom.iterate_map_mul, ring_hom.iterate_map_add, ← iterate_add_apply, add_mul, add_comm, add_left_comm], .. (infer_instance : has_add (perfect_closure K p)), .. (infer_instance : has_neg (perfect_closure K p)), .. (infer_instance : comm_monoid (perfect_closure K p)) } theorem eq_iff' (x y : ℕ × K) : mk K p x = mk K p y ↔ ∃ z, (frobenius K p^[y.1 + z] x.2) = (frobenius K p^[x.1 + z] y.2) := begin split, { intro H, replace H := quot.exact _ H, induction H, case eqv_gen.rel : x y H { cases H with n x, exact ⟨0, rfl⟩ }, case eqv_gen.refl : H { exact ⟨0, rfl⟩ }, case eqv_gen.symm : x y H ih { cases ih with w ih, exact ⟨w, ih.symm⟩ }, case eqv_gen.trans : x y z H1 H2 ih1 ih2 { cases ih1 with z1 ih1, cases ih2 with z2 ih2, existsi z2+(y.1+z1), rw [← add_assoc, iterate_add_apply, ih1], rw [← iterate_add_apply, add_comm, iterate_add_apply, ih2], rw [← iterate_add_apply], simp only [add_comm, add_left_comm] } }, intro H, cases x with m x, cases y with n y, cases H with z H, dsimp only at H, rw [r.sound K p (n+z) m x _ rfl, r.sound K p (m+z) n y _ rfl, H], rw [add_assoc, add_comm, add_comm z] end theorem nat_cast (n x : ℕ) : (x : perfect_closure K p) = mk K p (n, x) := begin induction n with n ih, { induction x with x ih, {refl}, rw [nat.cast_succ, nat.cast_succ, ih], refl }, rw ih, apply quot.sound, conv {congr, skip, skip, rw ← frobenius_nat_cast K p x}, apply r.intro end theorem int_cast (x : ℤ) : (x : perfect_closure K p) = mk K p (0, x) := by induction x; simp only [int.cast_of_nat, int.cast_neg_succ_of_nat, nat_cast K p 0]; refl theorem nat_cast_eq_iff (x y : ℕ) : (x : perfect_closure K p) = y ↔ (x : K) = y := begin split; intro H, { rw [nat_cast K p 0, nat_cast K p 0, eq_iff'] at H, cases H with z H, simpa only [zero_add, iterate_fixed (frobenius_nat_cast K p _)] using H }, rw [nat_cast K p 0, nat_cast K p 0, H] end instance : char_p (perfect_closure K p) p := begin constructor, intro x, rw ← char_p.cast_eq_zero_iff K, rw [← nat.cast_zero, nat_cast_eq_iff, nat.cast_zero] end theorem frobenius_mk (x : ℕ × K) : (frobenius (perfect_closure K p) p : perfect_closure K p → perfect_closure K p) (mk K p x) = mk _ _ (x.1, x.2^p) := begin simp only [frobenius_def], cases x with n x, dsimp only, suffices : ∀ p':ℕ, mk K p (n, x) ^ p' = mk K p (n, x ^ p'), { apply this }, intro p, induction p with p ih, case nat.zero { apply r.sound, rw [(frobenius _ _).iterate_map_one, pow_zero] }, case nat.succ { rw [pow_succ, ih], symmetry, apply r.sound, simp only [pow_succ, (frobenius _ _).iterate_map_mul] } end /-- Embedding of `K` into `perfect_closure K p` -/ def of : K →+* perfect_closure K p := { to_fun := λ x, mk _ _ (0, x), map_one' := rfl, map_mul' := λ x y, rfl, map_zero' := rfl, map_add' := λ x y, rfl } lemma of_apply (x : K) : of K p x = mk _ _ (0, x) := rfl end ring theorem eq_iff [integral_domain K] (p : ℕ) [fact p.prime] [char_p K p] (x y : ℕ × K) : quot.mk (r K p) x = quot.mk (r K p) y ↔ (frobenius K p^[y.1] x.2) = (frobenius K p^[x.1] y.2) := (eq_iff' K p x y).trans ⟨λ ⟨z, H⟩, (frobenius_inj K p).iterate z $ by simpa only [add_comm, iterate_add] using H, λ H, ⟨0, H⟩⟩ section field variables [field K] (p : ℕ) [fact p.prime] [char_p K p] instance : has_inv (perfect_closure K p) := ⟨quot.lift (λ x:ℕ×K, quot.mk (r K p) (x.1, x.2⁻¹)) (λ x y (H : r K p x y), match x, y, H with | _, _, r.intro n x := quot.sound $ by { simp only [frobenius_def], rw ← inv_pow', apply r.intro } end)⟩ instance : field (perfect_closure K p) := { exists_pair_ne := ⟨0, 1, λ H, zero_ne_one ((eq_iff _ _ _ _).1 H)⟩, mul_inv_cancel := λ e, induction_on e $ λ ⟨m, x⟩ H, have _ := mt (eq_iff _ _ _ _).2 H, (eq_iff _ _ _ _).2 (by simp only [(frobenius _ _).iterate_map_one, (frobenius K p).iterate_map_zero, iterate_zero_apply, ← (frobenius _ p).iterate_map_mul] at this ⊢; rw [mul_inv_cancel this, (frobenius _ _).iterate_map_one]), inv_zero := congr_arg (quot.mk (r K p)) (by rw [inv_zero]), .. (infer_instance : has_inv (perfect_closure K p)), .. (infer_instance : comm_ring (perfect_closure K p)) } instance : perfect_ring (perfect_closure K p) p := { pth_root' := λ e, lift_on e (λ x, mk K p (x.1 + 1, x.2)) (λ x y H, match x, y, H with | _, _, r.intro n x := quot.sound (r.intro _ _) end), frobenius_pth_root' := λ e, induction_on e (λ ⟨n, x⟩, by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }), pth_root_frobenius' := λ e, induction_on e (λ ⟨n, x⟩, by { simp only [lift_on_mk, frobenius_mk], exact (quot.sound $ r.intro _ _).symm }) } theorem eq_pth_root (x : ℕ × K) : mk K p x = (pth_root (perfect_closure K p) p^[x.1] (of K p x.2)) := begin rcases x with ⟨m, x⟩, induction m with m ih, {refl}, rw [iterate_succ_apply', ← ih]; refl end /-- Given a field `K` of characteristic `p` and a perfect ring `L` of the same characteristic, any homomorphism `K →+* L` can be lifted to `perfect_closure K p`. -/ def lift (L : Type v) [comm_semiring L] [char_p L p] [perfect_ring L p] : (K →+* L) ≃ (perfect_closure K p →+* L) := begin have := left_inverse_pth_root_frobenius.iterate, refine_struct { .. }, field to_fun { intro f, refine_struct { .. }, field to_fun { refine λ e, lift_on e (λ x, pth_root L p^[x.1] (f x.2)) _, rintro a b ⟨n⟩, simp only [f.map_frobenius, iterate_succ_apply, pth_root_frobenius] }, field map_one' { exact f.map_one }, field map_zero' { exact f.map_zero }, field map_mul' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_mul_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_mul, ring_hom.map_mul], rw [iterate_add_apply, this _ _, add_comm, iterate_add_apply, this _ _] }, field map_add' { rintro ⟨x⟩ ⟨y⟩, simp only [quot_mk_eq_mk, lift_on_mk, mk_add_mk, ring_hom.map_iterate_frobenius, ring_hom.iterate_map_add, ring_hom.map_add], rw [iterate_add_apply, this _ _, add_comm x.1, iterate_add_apply, this _ _] } }, field inv_fun { exact λ f, f.comp (of K p) }, field left_inv { intro f, ext x, refl }, field right_inv { intro f, ext ⟨x⟩, simp only [ring_hom.coe_mk, quot_mk_eq_mk, ring_hom.comp_apply, lift_on_mk], rw [eq_pth_root, ring_hom.map_iterate_pth_root] } end end field end perfect_closure
b056a9906d16e9d97f39bc572f98a409ea5623db
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/star/basic.lean
b7c0c045a796f6e0110ceb9c2e2d49a990fa974c
[ "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
6,846
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.apply_fun import algebra.order.ring import algebra.opposites import algebra.big_operators.basic import data.equiv.ring_aut import data.equiv.mul_add_aut /-! # Star monoids and star rings We introduce the basic algebraic notions of star monoids, and star rings. Star algebras are introduced in `algebra.algebra.star`. These are implemented as "mixin" typeclasses, so to summon a star ring (for example) one needs to write `(R : Type) [ring R] [star_ring R]`. This avoids difficulties with diamond inheritance. For now we simply do not introduce notations, as different users are expected to feel strongly about the relative merits of `r^*`, `r†`, `rᘁ`, and so on. Our star rings are actually star semirings, but of course we can prove `star_neg : star (-r) = - star r` when the underlying semiring is a ring. -/ universes u v open opposite /-- Notation typeclass (with no default notation!) for an algebraic structure with a star operation. -/ class has_star (R : Type u) := (star : R → R) variables {R : Type u} /-- A star operation (e.g. complex conjugate). -/ def star [has_star R] (r : R) : R := has_star.star r /-- The opposite type carries the same star operation. -/ instance [has_star R] : has_star (Rᵒᵖ) := { star := λ r, op (star (r.unop)) } @[simp] lemma unop_star [has_star R] (r : Rᵒᵖ) : unop (star r) = star (unop r) := rfl @[simp] lemma op_star [has_star R] (r : R) : op (star r) = star (op r) := rfl /-- Typeclass for a star operation with is involutive. -/ class has_involutive_star (R : Type u) extends has_star R := (star_involutive : function.involutive star) export has_involutive_star (star_involutive) @[simp] lemma star_star [has_involutive_star R] (r : R) : star (star r) = r := star_involutive _ lemma star_injective [has_involutive_star R] : function.injective (star : R → R) := star_involutive.injective instance [has_involutive_star R] : has_involutive_star (Rᵒᵖ) := { star_involutive := λ r, unop_injective (star_star r.unop) } /-- A `*`-monoid is a monoid `R` with an involutive operations `star` so `star (r * s) = star s * star r`. -/ class star_monoid (R : Type u) [monoid R] extends has_involutive_star R := (star_mul : ∀ r s : R, star (r * s) = star s * star r) @[simp] lemma star_mul [monoid R] [star_monoid R] (r s : R) : star (r * s) = star s * star r := star_monoid.star_mul r s /-- `star` as an `mul_equiv` from `R` to `Rᵒᵖ` -/ @[simps apply] def star_mul_equiv [monoid R] [star_monoid R] : R ≃* Rᵒᵖ := { to_fun := λ x, opposite.op (star x), map_mul' := λ x y, (star_mul x y).symm ▸ (opposite.op_mul _ _), ..(has_involutive_star.star_involutive.to_equiv star).trans opposite.equiv_to_opposite} /-- `star` as a `mul_aut` for commutative `R`. -/ @[simps apply] def star_mul_aut [comm_monoid R] [star_monoid R] : mul_aut R := { to_fun := star, map_mul' := λ x y, (star_mul x y).trans (mul_comm _ _), ..(has_involutive_star.star_involutive.to_equiv star) } variables (R) @[simp] lemma star_one [monoid R] [star_monoid R] : star (1 : R) = 1 := begin have := congr_arg opposite.unop (star_mul_equiv : R ≃* Rᵒᵖ).map_one, rwa [star_mul_equiv_apply, opposite.unop_op, opposite.unop_one] at this, end variables {R} instance [monoid R] [star_monoid R] : star_monoid (Rᵒᵖ) := { star_mul := λ x y, unop_injective (star_mul y.unop x.unop) } /-- Any commutative monoid admits the trivial `*`-structure. See note [reducible non-instances]. -/ @[reducible] def star_monoid_of_comm {R : Type*} [comm_monoid R] : star_monoid R := { star := id, star_involutive := λ x, rfl, star_mul := mul_comm } section local attribute [instance] star_monoid_of_comm @[simp] lemma star_id_of_comm {R : Type*} [comm_semiring R] {x : R} : star x = x := rfl end /-- A `*`-ring `R` is a (semi)ring with an involutive `star` operation which is additive which makes `R` with its multiplicative structure into a `*`-monoid (i.e. `star (r * s) = star s * star r`). -/ class star_ring (R : Type u) [semiring R] extends star_monoid R := (star_add : ∀ r s : R, star (r + s) = star r + star s) @[simp] lemma star_add [semiring R] [star_ring R] (r s : R) : star (r + s) = star r + star s := star_ring.star_add r s /-- `star` as an `add_equiv` -/ @[simps apply] def star_add_equiv [semiring R] [star_ring R] : R ≃+ R := { to_fun := star, map_add' := star_add, ..(has_involutive_star.star_involutive.to_equiv star)} variables (R) @[simp] lemma star_zero [semiring R] [star_ring R] : star (0 : R) = 0 := (star_add_equiv : R ≃+ R).map_zero variables {R} instance [semiring R] [star_ring R] : star_ring (Rᵒᵖ) := { star_add := λ x y, unop_injective (star_add x.unop y.unop) } /-- `star` as an `ring_equiv` from `R` to `Rᵒᵖ` -/ @[simps apply] def star_ring_equiv [semiring R] [star_ring R] : R ≃+* Rᵒᵖ := { to_fun := λ x, opposite.op (star x), ..star_add_equiv.trans (opposite.op_add_equiv : R ≃+ Rᵒᵖ), ..star_mul_equiv} /-- `star` as a `ring_aut` for commutative `R`. -/ @[simps apply] def star_ring_aut [comm_semiring R] [star_ring R] : ring_aut R := { to_fun := star, ..star_add_equiv, ..star_mul_aut } section open_locale big_operators @[simp] lemma star_sum [semiring R] [star_ring R] {α : Type*} (s : finset α) (f : α → R): star (∑ x in s, f x) = ∑ x in s, star (f x) := (star_add_equiv : R ≃+ R).map_sum _ _ end @[simp] lemma star_neg [ring R] [star_ring R] (r : R) : star (-r) = - star r := (star_add_equiv : R ≃+ R).map_neg _ @[simp] lemma star_sub [ring R] [star_ring R] (r s : R) : star (r - s) = star r - star s := (star_add_equiv : R ≃+ R).map_sub _ _ @[simp] lemma star_bit0 [ring R] [star_ring R] (r : R) : star (bit0 r) = bit0 (star r) := by simp [bit0] @[simp] lemma star_bit1 [ring R] [star_ring R] (r : R) : star (bit1 r) = bit1 (star r) := by simp [bit1] /-- Any commutative semiring admits the trivial `*`-structure. See note [reducible non-instances]. -/ @[reducible] def star_ring_of_comm {R : Type*} [comm_semiring R] : star_ring R := { star := id, star_add := λ x y, rfl, ..star_monoid_of_comm } /-- An ordered `*`-ring is a ring which is both an ordered ring and a `*`-ring, and `0 ≤ star r * r` for every `r`. (In a Banach algebra, the natural ordering is given by the positive cone which is the closure of the sums of elements `star r * r`. This ordering makes the Banach algebra an ordered `*`-ring.) -/ class star_ordered_ring (R : Type u) [ordered_semiring R] extends star_ring R := (star_mul_self_nonneg : ∀ r : R, 0 ≤ star r * r) lemma star_mul_self_nonneg [ordered_semiring R] [star_ordered_ring R] {r : R} : 0 ≤ star r * r := star_ordered_ring.star_mul_self_nonneg r
fcd03ee8854a7b0a22d43ac302b85fca91b8a99f
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/group_theory/perm.lean
dc1c43d9486ff3590bb87ad26eeaa91b930a196a
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
29,533
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.fintype universes u v open equiv function fintype finset variables {α : Type u} {β : Type v} namespace equiv.perm def subtype_perm (f : perm α) {p : α → Prop} (h : ∀ x, p x ↔ p (f x)) : perm {x // p x} := ⟨λ x, ⟨f x, (h _).1 x.2⟩, λ x, ⟨f⁻¹ x, (h (f⁻¹ x)).2 $ by simpa using x.2⟩, λ _, by simp, λ _, by simp⟩ def of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : perm α := ⟨λ x, if h : p x then f ⟨x, h⟩ else x, λ x, if h : p x then f⁻¹ ⟨x, h⟩ else x, λ x, have h : ∀ h : p x, p (f ⟨x, h⟩), from λ h, (f ⟨x, h⟩).2, by simp; split_ifs at *; simp * at *, λ x, have h : ∀ h : p x, p (f⁻¹ ⟨x, h⟩), from λ h, (f⁻¹ ⟨x, h⟩).2, by simp; split_ifs at *; simp * at *⟩ instance of_subtype.is_group_hom {p : α → Prop} [decidable_pred p] : is_group_hom (@of_subtype α p _) := ⟨λ f g, equiv.ext _ _ $ λ x, begin rw [of_subtype, of_subtype, of_subtype], by_cases h : p x, { have h₁ : p (f (g ⟨x, h⟩)), from (f (g ⟨x, h⟩)).2, have h₂ : p (g ⟨x, h⟩), from (g ⟨x, h⟩).2, simp [h, h₁, h₂] }, { simp [h] } end⟩ lemma eq_inv_iff_eq {f : perm α} {x y : α} : x = f⁻¹ y ↔ f x = y := by conv {to_lhs, rw [← injective.eq_iff f.bijective.1, apply_inv_self]} lemma inv_eq_iff_eq {f : perm α} {x y : α} : f⁻¹ x = y ↔ x = f y := by rw [eq_comm, eq_inv_iff_eq, eq_comm] def is_cycle (f : perm α) := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y lemma exists_int_pow_eq_of_is_cycle {f : perm α} (hf : is_cycle f) {x y : α} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y := let ⟨g, hg⟩ := hf in let ⟨a, ha⟩ := hg.2 x hx in let ⟨b, hb⟩ := hg.2 y hy in ⟨b - a, by rw [← ha, ← mul_apply, ← gpow_add, sub_add_cancel, hb]⟩ lemma of_subtype_subtype_perm {f : perm α} {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : of_subtype (subtype_perm f h₁) = f := equiv.ext _ _ $ λ x, begin rw [of_subtype, subtype_perm], by_cases hx : p x, { simp [hx] }, { haveI := classical.prop_decidable, simp [hx, not_not.1 (mt (h₂ x) hx)] } end lemma of_subtype_apply_of_not_mem {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) {x : α} (hx : ¬ p x) : of_subtype f x = x := dif_neg hx lemma mem_iff_of_subtype_apply_mem {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) (x : α) : p x ↔ p ((of_subtype f : α → α) x) := if h : p x then by dsimp [of_subtype]; simpa [h] using (f ⟨x, h⟩).2 else by simp [h, of_subtype_apply_of_not_mem f h] @[simp] lemma subtype_perm_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : subtype_perm (of_subtype f) (mem_iff_of_subtype_apply_mem f) = f := equiv.ext _ _ $ λ ⟨x, hx⟩, by dsimp [subtype_perm, of_subtype]; simp [show p x, from hx] lemma pow_apply_eq_of_apply_apply_eq_self_nat {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 := or.inl rfl | (n+1) := (pow_apply_eq_of_apply_apply_eq_self_nat n).elim (λ h, or.inr (by rw [pow_succ, mul_apply, h])) (λ h, or.inl (by rw [pow_succ, mul_apply, h, hffx])) lemma pow_apply_eq_of_apply_apply_eq_self_int {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ i : ℤ, (f ^ i) x = x ∨ (f ^ i) x = f x | (n : ℕ) := pow_apply_eq_of_apply_apply_eq_self_nat hffx n | -[1+ n] := by rw [gpow_neg_succ, inv_eq_iff_eq, ← injective.eq_iff f.bijective.1, ← mul_apply, ← pow_succ, eq_comm, inv_eq_iff_eq, ← mul_apply, ← pow_succ', @eq_comm _ x, or.comm]; exact pow_apply_eq_of_apply_apply_eq_self_nat hffx _ variable [decidable_eq α] def support [fintype α] (f : perm α) := univ.filter (λ x, f x ≠ x) @[simp] lemma mem_support [fintype α] {f : perm α} {x : α} : x ∈ f.support ↔ f x ≠ x := by simp [support] def is_swap (f : perm α) := ∃ x y, x ≠ y ∧ f = swap x y lemma swap_mul_eq_mul_swap (f : perm α) (x y : α) : swap x y * f = f * swap (f⁻¹ x) (f⁻¹ y) := equiv.ext _ _ $ λ z, begin simp [mul_apply, swap_apply_def], split_ifs; simp [*, eq_inv_iff_eq] at * <|> cc end lemma mul_swap_eq_swap_mul (f : perm α) (x y : α) : f * swap x y = swap (f x) (f y) * f := by rw [swap_mul_eq_mul_swap, inv_apply_self, inv_apply_self] @[simp] lemma swap_mul_self (i j : α) : equiv.swap i j * equiv.swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_swap_apply (i j k : α) : equiv.swap i j (equiv.swap i j k) = k := equiv.swap_core_swap_core k i j lemma is_swap_of_subtype {p : α → Prop} [decidable_pred p] {f : perm (subtype p)} (h : is_swap f) : is_swap (of_subtype f) := let ⟨⟨x, hx⟩, ⟨y, hy⟩, hxy⟩ := h in ⟨x, y, by simp at hxy; tauto, equiv.ext _ _ $ λ z, begin rw [hxy.2, of_subtype], simp [swap_apply_def], split_ifs; cc <|> simp * at * end⟩ lemma support_swap_mul {f : perm α} {x : α} {y : α} (hy : (swap x (f x) * f) y ≠ y) : f y ≠ y ∧ y ≠ x := begin simp only [swap_apply_def, mul_apply, injective.eq_iff f.bijective.1] at *, by_cases h : f y = x, { split; intro; simp * at * }, { split_ifs at hy; cc } end def swap_factors_aux : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} | [] := λ f h, ⟨[], equiv.ext _ _ $ λ x, by rw [list.prod_nil]; exact eq.symm (not_not.1 (mt h (list.not_mem_nil _))), by simp⟩ | (x :: l) := λ f h, if hfx : x = f x then swap_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h hy)) else let m := swap_factors_aux l (swap x (f x) * f) (λ y hy, have f y ≠ y ∧ y ≠ x, from support_swap_mul hy, list.mem_of_ne_of_mem this.2 (h this.1)) in ⟨swap x (f x) :: m.1, by rw [list.prod_cons, m.2.1, ← mul_assoc, mul_def (swap x (f x)), swap_swap, ← one_def, one_mul], λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ h, ⟨x, f x, hfx, h⟩) (m.2.2 _)⟩ /-- `swap_factors` represents a permutation as a product of a list of transpositions. The representation is non unique and depends on the linear order structure. For types without linear order `trunc_swap_factors` can be used -/ def swap_factors [fintype α] [decidable_linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := swap_factors_aux ((@univ α _).sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) def trunc_swap_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (swap_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) lemma swap_mul_swap_mul_swap {x y z : α} (hwz: x ≠ y) (hxz : x ≠ z) : swap y z * swap x y * swap y z = swap z x := equiv.ext _ _ $ λ n, by simp only [swap_apply_def, mul_apply]; split_ifs; cc lemma is_conj_swap {w x y z : α} (hwx : w ≠ x) (hyz : y ≠ z) : is_conj (swap w x) (swap y z) := have h : ∀ {y z : α}, y ≠ z → w ≠ z → (swap w y * swap x z) * swap w x * (swap w y * swap x z)⁻¹ = swap y z := λ y z hyz hwz, by rw [mul_inv_rev, swap_inv, swap_inv, mul_assoc (swap w y), mul_assoc (swap w y), ← mul_assoc _ (swap x z), swap_mul_swap_mul_swap hwx hwz, ← mul_assoc, swap_mul_swap_mul_swap hwz.symm hyz.symm], if hwz : w = z then have hwy : w ≠ y, by cc, ⟨swap w z * swap x y, by rw [swap_comm y z, h hyz.symm hwy]⟩ else ⟨swap w y * swap x z, h hyz hwz⟩ /-- set of all pairs (⟨a, b⟩ : Σ a : fin n, fin n) such that b < a -/ def fin_pairs_lt (n : ℕ) : finset (Σ a : fin n, fin n) := (univ : finset (fin n)).sigma (λ a, (range a.1).attach_fin (λ m hm, lt_trans (mem_range.1 hm) a.2)) lemma mem_fin_pairs_lt {n : ℕ} {a : Σ a : fin n, fin n} : a ∈ fin_pairs_lt n ↔ a.2 < a.1 := by simp [fin_pairs_lt, fin.lt_def] def sign_aux {n : ℕ} (a : perm (fin n)) : units ℤ := (fin_pairs_lt n).prod (λ x, if a x.1 ≤ a x.2 then -1 else 1) @[simp] lemma sign_aux_one (n : ℕ) : sign_aux (1 : perm (fin n)) = 1 := begin unfold sign_aux, conv { to_rhs, rw ← @finset.prod_const_one _ (units ℤ) (fin_pairs_lt n) }, exact finset.prod_congr rfl (λ a ha, if_neg (not_le_of_gt (mem_fin_pairs_lt.1 ha))) end def sign_bij_aux {n : ℕ} (f : perm (fin n)) (a : Σ a : fin n, fin n) : Σ a : fin n, fin n := if hxa : f a.2 < f a.1 then ⟨f a.1, f a.2⟩ else ⟨f a.2, f a.1⟩ lemma sign_bij_aux_inj {n : ℕ} {f : perm (fin n)} : ∀ a b : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → b ∈ fin_pairs_lt n → sign_bij_aux f a = sign_bij_aux f b → a = b := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, begin unfold sign_bij_aux at h, rw mem_fin_pairs_lt at *, have : ¬b₁ < b₂ := not_lt_of_ge (le_of_lt hb), split_ifs at h; simp [*, injective.eq_iff f.bijective.1, sigma.mk.inj_eq] at * end lemma sign_bij_aux_surj {n : ℕ} {f : perm (fin n)} : ∀ a ∈ fin_pairs_lt n, ∃ b ∈ fin_pairs_lt n, a = sign_bij_aux f b := λ ⟨a₁, a₂⟩ ha, if hxa : f⁻¹ a₂ < f⁻¹ a₁ then ⟨⟨f⁻¹ a₁, f⁻¹ a₂⟩, mem_fin_pairs_lt.2 hxa, by dsimp [sign_bij_aux]; rw [apply_inv_self, apply_inv_self, dif_pos (mem_fin_pairs_lt.1 ha)]⟩ else ⟨⟨f⁻¹ a₂, f⁻¹ a₁⟩, mem_fin_pairs_lt.2 $ lt_of_le_of_ne (le_of_not_gt hxa) $ λ h, by simpa [mem_fin_pairs_lt, (f⁻¹).bijective.1 h, lt_irrefl] using ha, by dsimp [sign_bij_aux]; rw [apply_inv_self, apply_inv_self, dif_neg (not_lt_of_ge (le_of_lt (mem_fin_pairs_lt.1 ha)))]⟩ lemma sign_bij_aux_mem {n : ℕ} {f : perm (fin n)}: ∀ a : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → sign_bij_aux f a ∈ fin_pairs_lt n := λ ⟨a₁, a₂⟩ ha, begin unfold sign_bij_aux, split_ifs with h, { exact mem_fin_pairs_lt.2 h }, { exact mem_fin_pairs_lt.2 (lt_of_le_of_ne (le_of_not_gt h) (λ h, ne_of_lt (mem_fin_pairs_lt.1 ha) (f.bijective.1 h.symm))) } end @[simp] lemma sign_aux_inv {n : ℕ} (f : perm (fin n)) : sign_aux f⁻¹ = sign_aux f := prod_bij (λ a ha, sign_bij_aux f⁻¹ a) sign_bij_aux_mem (λ ⟨a, b⟩ hab, if h : f⁻¹ b < f⁻¹ a then by rw [sign_bij_aux, dif_pos h, if_neg (not_le_of_gt h), apply_inv_self, apply_inv_self, if_neg (not_le_of_gt $ mem_fin_pairs_lt.1 hab)] else by rw [sign_bij_aux, if_pos (le_of_not_gt h), dif_neg h, apply_inv_self, apply_inv_self, if_pos (le_of_lt $ mem_fin_pairs_lt.1 hab)]) sign_bij_aux_inj sign_bij_aux_surj lemma sign_aux_mul {n : ℕ} (f g : perm (fin n)) : sign_aux (f * g) = sign_aux f * sign_aux g := begin rw ← sign_aux_inv g, unfold sign_aux, rw ← prod_mul_distrib, refine prod_bij (λ a ha, sign_bij_aux g a) sign_bij_aux_mem _ sign_bij_aux_inj sign_bij_aux_surj, rintros ⟨a, b⟩ hab, rw [sign_bij_aux, mul_apply, mul_apply], rw mem_fin_pairs_lt at hab, by_cases h : g b < g a, { rw dif_pos h, simp [not_le_of_gt hab]; congr }, { rw [dif_neg h, inv_apply_self, inv_apply_self, if_pos (le_of_lt hab)], by_cases h₁ : f (g b) ≤ f (g a), { have : f (g b) ≠ f (g a), { rw [ne.def, injective.eq_iff f.bijective.1, injective.eq_iff g.bijective.1]; exact ne_of_lt hab }, rw [if_pos h₁, if_neg (not_le_of_gt (lt_of_le_of_ne h₁ this))], refl }, { rw [if_neg h₁, if_pos (le_of_lt (lt_of_not_ge h₁))], refl } } end instance sign_aux.is_group_hom {n : ℕ} : is_group_hom (@sign_aux n) := ⟨sign_aux_mul⟩ private lemma sign_aux_swap_zero_one {n : ℕ} (hn : 2 ≤ n) : sign_aux (swap (⟨0, lt_of_lt_of_le dec_trivial hn⟩ : fin n) ⟨1, lt_of_lt_of_le dec_trivial hn⟩) = -1 := let zero : fin n := ⟨0, lt_of_lt_of_le dec_trivial hn⟩ in let one : fin n := ⟨1, lt_of_lt_of_le dec_trivial hn⟩ in have hzo : zero < one := dec_trivial, show _ = (finset.singleton (⟨one, zero⟩ : Σ a : fin n, fin n)).prod (λ x : Σ a : fin n, fin n, if (equiv.swap zero one) x.1 ≤ swap zero one x.2 then (-1 : units ℤ) else 1), begin refine eq.symm (prod_subset (λ ⟨x₁, x₂⟩, by simp [mem_fin_pairs_lt, hzo] {contextual := tt}) (λ a ha₁ ha₂, _)), rcases a with ⟨⟨a₁, ha₁⟩, ⟨a₂, ha₂⟩⟩, replace ha₁ : a₂ < a₁ := mem_fin_pairs_lt.1 ha₁, simp only [swap_apply_def], have : ¬ 1 ≤ a₂ → a₂ = 0, from λ h, nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge h)), have : a₁ ≤ 1 → a₁ = 0 ∨ a₁ = 1, from nat.cases_on a₁ (λ _, or.inl rfl) (λ a₁, nat.cases_on a₁ (λ _, or.inr rfl) (λ _ h, absurd h dec_trivial)), split_ifs; simp [*, lt_irrefl, -not_lt, not_le.symm, -not_le, le_refl, fin.lt_def, fin.le_def, nat.zero_le, zero, one, iff.intro fin.veq_of_eq fin.eq_of_veq, nat.le_zero_iff] at *, end lemma sign_aux_swap : ∀ {n : ℕ} {x y : fin n} (hxy : x ≠ y), sign_aux (swap x y) = -1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := λ x y hxy, have h2n : 2 ≤ n + 2 := dec_trivial, by rw [← is_conj_iff_eq, ← sign_aux_swap_zero_one h2n]; exact is_group_hom.is_conj _ (is_conj_swap hxy dec_trivial) def sign_aux2 : list α → perm α → units ℤ | [] f := 1 | (x::l) f := if x = f x then sign_aux2 l f else -sign_aux2 l (swap x (f x) * f) lemma sign_aux_eq_sign_aux2 {n : ℕ} : ∀ (l : list α) (f : perm α) (e : α ≃ fin n) (h : ∀ x, f x ≠ x → x ∈ l), sign_aux ((e.symm.trans f).trans e) = sign_aux2 l f | [] f e h := have f = 1, from equiv.ext _ _ $ λ y, not_not.1 (mt (h y) (list.not_mem_nil _)), by rw [this, one_def, equiv.trans_refl, equiv.symm_trans, ← one_def, sign_aux_one, sign_aux2] | (x::l) f e h := begin rw sign_aux2, by_cases hfx : x = f x, { rw if_pos hfx, exact sign_aux_eq_sign_aux2 l f _ (λ y (hy : f y ≠ y), list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h y hy) ) }, { have hy : ∀ y : α, (swap x (f x) * f) y ≠ y → y ∈ l, from λ y hy, have f y ≠ y ∧ y ≠ x, from support_swap_mul hy, list.mem_of_ne_of_mem this.2 (h _ this.1), have : (e.symm.trans (swap x (f x) * f)).trans e = (swap (e x) (e (f x))) * (e.symm.trans f).trans e, from equiv.ext _ _ (λ z, by rw ← equiv.symm_trans_swap_trans; simp [mul_def]), have hefx : e x ≠ e (f x), from mt (injective.eq_iff e.bijective.1).1 hfx, rw [if_neg hfx, ← sign_aux_eq_sign_aux2 _ _ e hy, this, sign_aux_mul, sign_aux_swap hefx], simp } end def sign_aux3 [fintype α] (f : perm α) {s : multiset α} : (∀ x, x ∈ s) → units ℤ := quotient.hrec_on s (λ l h, sign_aux2 l f) (trunc.induction_on (equiv_fin α) (λ e l₁ l₂ h, function.hfunext (show (∀ x, x ∈ l₁) = ∀ x, x ∈ l₂, by simp [list.mem_of_perm h]) (λ h₁ h₂ _, by rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₂ _)]))) lemma sign_aux3_mul_and_swap [fintype α] (f g : perm α) (s : multiset α) (hs : ∀ x, x ∈ s) : sign_aux3 (f * g) hs = sign_aux3 f hs * sign_aux3 g hs ∧ ∀ x y, x ≠ y → sign_aux3 (swap x y) hs = -1 := let ⟨l, hl⟩ := quotient.exists_rep s in let ⟨e, _⟩ := trunc.exists_rep (equiv_fin α) in begin clear _let_match _let_match, subst hl, show sign_aux2 l (f * g) = sign_aux2 l f * sign_aux2 l g ∧ ∀ x y, x ≠ y → sign_aux2 l (swap x y) = -1, have hfg : (e.symm.trans (f * g)).trans e = (e.symm.trans f).trans e * (e.symm.trans g).trans e, from equiv.ext _ _ (λ h, by simp [mul_apply]), split, { rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), hfg, sign_aux_mul] }, { assume x y hxy, have hexy : e x ≠ e y, from mt (injective.eq_iff e.bijective.1).1 hxy, rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), equiv.symm_trans_swap_trans, sign_aux_swap hexy] } end /-- `sign` of a permutation returns the signature or parity of a permutation, `1` for even permutations, `-1` for odd permutations. It is the unique surjective group homomorphism from `perm α` to the group with two elements.-/ def sign [fintype α] (f : perm α) := sign_aux3 f mem_univ instance sign.is_group_hom [fintype α] : is_group_hom (@sign α _ _) := ⟨λ f g, (sign_aux3_mul_and_swap f g _ mem_univ).1⟩ section sign variable [fintype α] @[simp] lemma sign_mul (f g : perm α) : sign (f * g) = sign f * sign g := is_group_hom.mul sign _ _ @[simp] lemma sign_one : (sign (1 : perm α)) = 1 := is_group_hom.one sign @[simp] lemma sign_refl : sign (equiv.refl α) = 1 := is_group_hom.one sign @[simp] lemma sign_inv (f : perm α) : sign f⁻¹ = sign f := by rw [is_group_hom.inv sign, int.units_inv_eq_self]; apply_instance lemma sign_swap {x y : α} (h : x ≠ y) : sign (swap x y) = -1 := (sign_aux3_mul_and_swap 1 1 _ mem_univ).2 x y h @[simp] lemma sign_swap' {x y : α} : (swap x y).sign = if x = y then 1 else -1 := if H : x = y then by simp [H, swap_self] else by simp [sign_swap H, H] lemma sign_eq_of_is_swap {f : perm α} (h : is_swap f) : sign f = -1 := let ⟨x, y, hxy⟩ := h in hxy.2.symm ▸ sign_swap hxy.1 lemma sign_aux3_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) {s : multiset α} {t : multiset β} (hs : ∀ x, x ∈ s) (ht : ∀ x, x ∈ t) : sign_aux3 ((e.symm.trans f).trans e) ht = sign_aux3 f hs := quotient.induction_on₂ t s (λ l₁ l₂ h₁ h₂, show sign_aux2 _ _ = sign_aux2 _ _, from let n := trunc.out (equiv_fin β) in by rw [← sign_aux_eq_sign_aux2 _ _ n (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ (e.trans n) (λ _ _, h₂ _)]; exact congr_arg sign_aux (equiv.ext _ _ (λ x, by simp))) ht hs lemma sign_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) : sign ((e.symm.trans f).trans e) = sign f := sign_aux3_symm_trans_trans f e mem_univ mem_univ lemma sign_prod_list_swap {l : list (perm α)} (hl : ∀ g ∈ l, is_swap g) : sign l.prod = -1 ^ l.length := have h₁ : l.map sign = list.repeat (-1) l.length := list.eq_repeat.2 ⟨by simp, λ u hu, let ⟨g, hg⟩ := list.mem_map.1 hu in hg.2 ▸ sign_eq_of_is_swap (hl _ hg.1)⟩, by rw [← list.prod_repeat, ← h₁, ← is_group_hom.prod (@sign α _ _)] lemma eq_sign_of_surjective_hom {s : perm α → units ℤ} [is_group_hom s] (hs : surjective s) : s = sign := have ∀ {f}, is_swap f → s f = -1 := λ f ⟨x, y, hxy, hxy'⟩, hxy'.symm ▸ by_contradiction (λ h, have ∀ f, is_swap f → s f = 1 := λ f ⟨a, b, hab, hab'⟩, by rw [← is_conj_iff_eq, ← or.resolve_right (int.units_eq_one_or _) h, hab']; exact is_group_hom.is_conj _ (is_conj_swap hab hxy), let ⟨g, hg⟩ := hs (-1) in let ⟨l, hl⟩ := trunc.out (trunc_swap_factors g) in have ∀ a ∈ l.map s, a = (1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this _ (hl.2 _ hg.1), have s l.prod = 1, by rw [is_group_hom.prod s, list.eq_repeat'.2 this, list.prod_repeat, one_pow], by rw [hl.1, hg] at this; exact absurd this dec_trivial), funext $ λ f, let ⟨l, hl₁, hl₂⟩ := trunc.out (trunc_swap_factors f) in have hsl : ∀ a ∈ l.map s, a = (-1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this (hl₂ _ hg.1), by rw [← hl₁, is_group_hom.prod s, list.eq_repeat'.2 hsl, list.length_map, list.prod_repeat, sign_prod_list_swap hl₂] lemma sign_subtype_perm (f : perm α) {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : sign (subtype_perm f h₁) = sign f := let l := trunc.out (trunc_swap_factors (subtype_perm f h₁)) in have hl' : ∀ g' ∈ l.1.map of_subtype, is_swap g' := λ g' hg', let ⟨g, hg⟩ := list.mem_map.1 hg' in hg.2 ▸ is_swap_of_subtype (l.2.2 _ hg.1), have hl'₂ : (l.1.map of_subtype).prod = f, by rw [← is_group_hom.prod of_subtype l.1, l.2.1, of_subtype_subtype_perm _ h₂], by conv {congr, rw ← l.2.1, skip, rw ← hl'₂}; rw [sign_prod_list_swap l.2.2, sign_prod_list_swap hl', list.length_map] @[simp] lemma sign_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : sign (of_subtype f) = sign f := have ∀ x, of_subtype f x ≠ x → p x, from λ x, not_imp_comm.1 (of_subtype_apply_of_not_mem f), by conv {to_rhs, rw [← subtype_perm_of_subtype f, sign_subtype_perm _ _ this]} lemma sign_eq_sign_of_equiv [decidable_eq β] [fintype β] (f : perm α) (g : perm β) (e : α ≃ β) (h : ∀ x, e (f x) = g (e x)) : sign f = sign g := have hg : g = (e.symm.trans f).trans e, from equiv.ext _ _ $ by simp [h], by rw [hg, sign_symm_trans_trans] lemma sign_bij [decidable_eq β] [fintype β] {f : perm α} {g : perm β} (i : Π x : α, f x ≠ x → β) (h : ∀ x hx hx', i (f x) hx' = g (i x hx)) (hi : ∀ x₁ x₂ hx₁ hx₂, i x₁ hx₁ = i x₂ hx₂ → x₁ = x₂) (hg : ∀ y, g y ≠ y → ∃ x hx, i x hx = y) : sign f = sign g := calc sign f = sign (@subtype_perm _ f (λ x, f x ≠ x) (by simp)) : eq.symm (sign_subtype_perm _ _ (λ _, id)) ... = sign (@subtype_perm _ g (λ x, g x ≠ x) (by simp)) : sign_eq_sign_of_equiv _ _ (equiv.of_bijective (show function.bijective (λ x : {x // f x ≠ x}, (⟨i x.1 x.2, have f (f x) ≠ f x, from mt (λ h, f.bijective.1 h) x.2, by rw [← h _ x.2 this]; exact mt (hi _ _ this x.2) x.2⟩ : {y // g y ≠ y})), from ⟨λ ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq (hi _ _ _ _ (subtype.mk.inj h)), λ ⟨y, hy⟩, let ⟨x, hfx, hx⟩ := hg y hy in ⟨⟨x, hfx⟩, subtype.eq hx⟩⟩)) (λ ⟨x, _⟩, subtype.eq (h x _ _)) ... = sign g : sign_subtype_perm _ _ (λ _, id) end sign lemma is_cycle_swap {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) := ⟨y, by rwa swap_apply_right, λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a), if hya : y = a then ⟨0, hya⟩ else ⟨1, by rw [gpow_one, swap_apply_def]; split_ifs at *; cc⟩⟩ lemma is_cycle_inv {f : perm α} (hf : is_cycle f) : is_cycle (f⁻¹) := let ⟨x, hx⟩ := hf in ⟨x, by simp [eq_inv_iff_eq, inv_eq_iff_eq, *] at *; cc, λ y hy, let ⟨i, hi⟩ := hx.2 y (by simp [eq_inv_iff_eq, inv_eq_iff_eq, *] at *; cc) in ⟨-i, by rwa [gpow_neg, inv_gpow, inv_inv]⟩⟩ lemma is_cycle_swap_mul_aux₁ : ∀ (n : ℕ) {b x : α} {f : perm α} (hf : is_cycle f) (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | 0 := λ b x f hf hb h, ⟨0, h⟩ | (n+1 : ℕ) := λ b x f hf hb h, if hfbx : f x = b then ⟨0, hfbx⟩ else have f b ≠ b ∧ b ≠ x, from support_swap_mul hb, have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b, by rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx), ne.def, ← injective.eq_iff f.bijective.1, apply_inv_self]; exact this.1, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hf hb' (f.bijective.1 $ by rw [apply_inv_self]; rwa [pow_succ, mul_apply] at h) in ⟨i + 1, by rw [add_comm, gpow_add, mul_apply, hi, gpow_one, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne (support_swap_mul hb).2 (ne.symm hfbx)]⟩ lemma is_cycle_swap_mul_aux₂ : ∀ (n : ℤ) {b x : α} {f : perm α} (hf : is_cycle f) (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | (n : ℕ) := λ b x f hf, is_cycle_swap_mul_aux₁ n hf | -[1+ n] := λ b x f hf hb h, if hfbx : f⁻¹ x = b then ⟨-1, by rwa [gpow_neg, gpow_one, mul_inv_rev, mul_apply, swap_inv, swap_apply_right]⟩ else if hfbx' : f x = b then ⟨0, hfbx'⟩ else have f b ≠ b ∧ b ≠ x := support_swap_mul hb, have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b, by rw [mul_apply, swap_apply_def]; split_ifs; simp [inv_eq_iff_eq, eq_inv_iff_eq] at *; cc, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n (is_cycle_inv hf) hb (show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by rw [← gpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, gpow_neg_succ, ← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, gpow_coe_nat, ← pow_succ', ← pow_succ]) in have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left], ⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, gpow_add, gpow_one, gpow_neg, ← inv_gpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x, gpow_add, gpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩ lemma eq_swap_of_is_cycle_of_apply_apply_eq_self {f : perm α} (hf : is_cycle f) {x : α} (hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) := equiv.ext _ _ $ λ y, let ⟨z, hz⟩ := hf in let ⟨i, hi⟩ := hz.2 x hfx in if hyx : y = x then by simp [hyx] else if hfyx : y = f x then by simp [hfyx, hffx] else begin rw [swap_apply_of_ne_of_ne hyx hfyx], refine by_contradiction (λ hy, _), cases hz.2 y hy with j hj, rw [← sub_add_cancel j i, gpow_add, mul_apply, hi] at hj, cases pow_apply_eq_of_apply_apply_eq_self_int hffx (j - i) with hji hji, { rw [← hj, hji] at hyx, cc }, { rw [← hj, hji] at hfyx, cc } end lemma is_cycle_swap_mul {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) := ⟨f x, by simp only [swap_apply_def, mul_apply]; split_ifs; simp [injective.eq_iff f.bijective.1] at *; cc, λ y hy, let ⟨i, hi⟩ := exists_int_pow_eq_of_is_cycle hf hx (support_swap_mul hy).1 in have hi : (f ^ (i - 1)) (f x) = y, from calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [gpow_one, mul_apply] ... = y : by rwa [← gpow_add, sub_add_cancel], is_cycle_swap_mul_aux₂ (i - 1) hf hy hi⟩ lemma support_swap_mul_cycle [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hffx : f (f x) ≠ x) : (swap x (f x) * f).support = f.support.erase x := have hfx : f x ≠ x, from λ hfx, by simpa [hfx] using hffx, finset.ext.2 $ λ y, begin have h1 : swap x (f x) * f ≠ 1, from λ h1, hffx $ by rw [mul_eq_one_iff_inv_eq, swap_inv] at h1; rw ← h1; simp, have hfyxor : f y ≠ x ∨ f x ≠ y := not_and_distrib.1 (λ h, h1 $ by have := eq_swap_of_is_cycle_of_apply_apply_eq_self hf hfx (by rw [h.2, h.1]); rw [← this, this, mul_def, swap_swap, one_def]), rw [mem_support, mem_erase, mem_support], split, { assume h, refine not_or_distrib.1 (λ h₁, h₁.elim (λ hyx, by simpa [hyx, mul_apply] using h) _), assume hfy, have hyx : x ≠ y := λ h, by rw h at hfx; tauto, have hfyx : f x ≠ y := by rwa [← hfy, ne.def, injective.eq_iff f.bijective.1], simpa [mul_apply, hfy, swap_apply_of_ne_of_ne hyx.symm hfyx.symm] using h }, { assume h, simp [swap_apply_def], split_ifs; cc } end @[simp] lemma support_swap [fintype α] {x y : α} (hxy : x ≠ y) : (swap x y).support = {x, y} := finset.ext.2 $ λ a, by simp [swap_apply_def]; split_ifs; cc lemma card_support_swap [fintype α] {x y : α} (hxy : x ≠ y) : (swap x y).support.card = 2 := show (swap x y).support.card = finset.card ⟨x::y::0, by simp [hxy]⟩, from congr_arg card $ by rw [support_swap hxy]; simp [*, finset.ext]; cc lemma sign_cycle [fintype α] : ∀ {f : perm α} (hf : is_cycle f), sign f = -(-1 ^ f.support.card) | f := λ hf, let ⟨x, hx⟩ := hf in calc sign f = sign (swap x (f x) * (swap x (f x) * f)) : by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl] ... = -(-1 ^ f.support.card) : if h1 : f (f x) = x then have h : swap x (f x) * f = 1, by conv in (f) {rw eq_swap_of_is_cycle_of_apply_apply_eq_self hf hx.1 h1 }; simp [mul_def, one_def], by rw [sign_mul, sign_swap hx.1.symm, h, sign_one, eq_swap_of_is_cycle_of_apply_apply_eq_self hf hx.1 h1, card_support_swap hx.1.symm]; refl else have h : card (support (swap x (f x) * f)) + 1 = card (support f), by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_cycle hf h1, card_insert_of_not_mem (not_mem_erase _ _)], have wf : card (support (swap x (f x) * f)) < card (support f), from h ▸ nat.lt_succ_self _, by rw [sign_mul, sign_swap hx.1.symm, sign_cycle (is_cycle_swap_mul hf hx.1 h1), ← h]; simp [pow_add] using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} end equiv.perm lemma finset.prod_univ_perm [fintype α] [comm_monoid β] {f : α → β} (σ : perm α) : (univ : finset α).prod f = univ.prod (λ z, f (σ z)) := eq.symm $ prod_bij (λ z _, σ z) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ H, σ.bijective.1 H) (λ b _, ⟨σ⁻¹ b, mem_univ _, by simp⟩) lemma finset.sum_univ_perm [fintype α] [add_comm_monoid β] {f : α → β} (σ : perm α) : (univ : finset α).sum f = univ.sum (λ z, f (σ z)) := @finset.prod_univ_perm _ (multiplicative β) _ _ f σ attribute [to_additive finset.sum_univ_perm] finset.prod_univ_perm
5ccc02da29541f5ace083115314a13aef5c347b6
26ac254ecb57ffcb886ff709cf018390161a9225
/src/ring_theory/noetherian.lean
98b0fcf407567dfd390c1cd318b506d817ca5abe
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
23,027
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 import order.order_iso_nat /-! # 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₂, ht₁.1.union 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, ht.1.image _, 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, (htb.1.image _).union (htc.1.image _), 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, hs.insert x, _⟩, 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] [nontrivial R] [add_comm_group M] [module R M] [is_noetherian R M] {s : set M} (hs : linear_independent R (coe : 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, (coe ∘ f) '' {m | m ≤ n} ⊆ s, { rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | m ≤ a}) ≤ span R ((coe ∘ 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.coe_injective.comp f.injective), set.subset_def], exact ⟨λ hab x (hxa : x ≤ a), le_trans hxa hab, λ hx, hx a (le_refl a)⟩ }, exact ⟨⟨λ n, span R ((coe ∘ 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 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, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = 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) 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, hfm.mul 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
7ff0709d52cedfbeb5b46161b8c91f7062b8e28b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/basic_auto.lean
93d062db498e0ab08b52dc349bafbfe9409e7779
[]
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
157,536
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.order_functions import Mathlib.control.monad.basic import Mathlib.data.nat.choose.basic import Mathlib.order.rel_classes import Mathlib.PostPort universes u v w u_1 u_2 u_3 namespace Mathlib /-! # Basic properties of lists -/ namespace list protected instance nil.is_left_id {α : Type u} : is_left_id (List α) append [] := is_left_id.mk nil_append protected instance nil.is_right_id {α : Type u} : is_right_id (List α) append [] := is_right_id.mk append_nil protected instance has_append.append.is_associative {α : Type u} : is_associative (List α) append := is_associative.mk append_assoc theorem cons_ne_nil {α : Type u} (a : α) (l : List α) : a :: l ≠ [] := fun (ᾰ : a :: l = []) => eq.dcases_on ᾰ (fun (H_1 : [] = a :: l) => list.no_confusion H_1) (Eq.refl []) (HEq.refl ᾰ) theorem cons_ne_self {α : Type u} (a : α) (l : List α) : a :: l ≠ l := mt (congr_arg length) (nat.succ_ne_self (length l)) theorem head_eq_of_cons_eq {α : Type u} {h₁ : α} {h₂ : α} {t₁ : List α} {t₂ : List α} : h₁ :: t₁ = h₂ :: t₂ → h₁ = h₂ := fun (Peq : h₁ :: t₁ = h₂ :: t₂) => list.no_confusion Peq fun (Pheq : h₁ = h₂) (Pteq : t₁ = t₂) => Pheq theorem tail_eq_of_cons_eq {α : Type u} {h₁ : α} {h₂ : α} {t₁ : List α} {t₂ : List α} : h₁ :: t₁ = h₂ :: t₂ → t₁ = t₂ := fun (Peq : h₁ :: t₁ = h₂ :: t₂) => list.no_confusion Peq fun (Pheq : h₁ = h₂) (Pteq : t₁ = t₂) => Pteq @[simp] theorem cons_injective {α : Type u} {a : α} : function.injective (List.cons a) := fun (l₁ l₂ : List α) (Pe : a :: l₁ = a :: l₂) => tail_eq_of_cons_eq Pe theorem cons_inj {α : Type u} (a : α) {l : List α} {l' : List α} : a :: l = a :: l' ↔ l = l' := function.injective.eq_iff cons_injective theorem exists_cons_of_ne_nil {α : Type u} {l : List α} (h : l ≠ []) : ∃ (b : α), ∃ (L : List α), l = b :: L := sorry /-! ### mem -/ theorem mem_singleton_self {α : Type u} (a : α) : a ∈ [a] := mem_cons_self a [] theorem eq_of_mem_singleton {α : Type u} {a : α} {b : α} : a ∈ [b] → a = b := fun (this : a ∈ [b]) => or.elim (eq_or_mem_of_mem_cons this) (fun (this : a = b) => this) fun (this : a ∈ []) => absurd this (not_mem_nil a) @[simp] theorem mem_singleton {α : Type u} {a : α} {b : α} : a ∈ [b] ↔ a = b := { mp := eq_of_mem_singleton, mpr := Or.inl } theorem mem_of_mem_cons_of_mem {α : Type u} {a : α} {b : α} {l : List α} : a ∈ b :: l → b ∈ l → a ∈ l := sorry theorem eq_or_ne_mem_of_mem {α : Type u} {a : α} {b : α} {l : List α} (h : a ∈ b :: l) : a = b ∨ a ≠ b ∧ a ∈ l := classical.by_cases Or.inl fun (this : a ≠ b) => or.elim h Or.inl fun (h : list.mem a l) => Or.inr { left := this, right := h } theorem not_mem_append {α : Type u} {a : α} {s : List α} {t : List α} (h₁ : ¬a ∈ s) (h₂ : ¬a ∈ t) : ¬a ∈ s ++ t := mt (iff.mp mem_append) (iff.mpr not_or_distrib { left := h₁, right := h₂ }) theorem ne_nil_of_mem {α : Type u} {a : α} {l : List α} (h : a ∈ l) : l ≠ [] := id fun (e : l = []) => false.dcases_on (fun (h : a ∈ []) => False) (eq.mp (Eq._oldrec (Eq.refl (a ∈ l)) e) h) theorem mem_split {α : Type u} {a : α} {l : List α} (h : a ∈ l) : ∃ (s : List α), ∃ (t : List α), l = s ++ a :: t := sorry theorem mem_of_ne_of_mem {α : Type u} {a : α} {y : α} {l : List α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons h₂) (fun (e : a = y) => absurd e h₁) fun (r : a ∈ l) => r theorem ne_of_not_mem_cons {α : Type u} {a : α} {b : α} {l : List α} : ¬a ∈ b :: l → a ≠ b := fun (nin : ¬a ∈ b :: l) (aeqb : a = b) => absurd (Or.inl aeqb) nin theorem not_mem_of_not_mem_cons {α : Type u} {a : α} {b : α} {l : List α} : ¬a ∈ b :: l → ¬a ∈ l := fun (nin : ¬a ∈ b :: l) (nainl : a ∈ l) => absurd (Or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {α : Type u} {a : α} {y : α} {l : List α} : a ≠ y → ¬a ∈ l → ¬a ∈ y :: l := fun (p1 : a ≠ y) (p2 : ¬a ∈ l) => not.intro fun (Pain : a ∈ y :: l) => absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2) theorem ne_and_not_mem_of_not_mem_cons {α : Type u} {a : α} {y : α} {l : List α} : ¬a ∈ y :: l → a ≠ y ∧ ¬a ∈ l := fun (p : ¬a ∈ y :: l) => { left := ne_of_not_mem_cons p, right := not_mem_of_not_mem_cons p } theorem mem_map_of_mem {α : Type u} {β : Type v} (f : α → β) {a : α} {l : List α} (h : a ∈ l) : f a ∈ map f l := sorry theorem exists_of_mem_map {α : Type u} {β : Type v} {f : α → β} {b : β} {l : List α} (h : b ∈ map f l) : ∃ (a : α), a ∈ l ∧ f a = b := sorry @[simp] theorem mem_map {α : Type u} {β : Type v} {f : α → β} {b : β} {l : List α} : b ∈ map f l ↔ ∃ (a : α), a ∈ l ∧ f a = b := sorry theorem mem_map_of_injective {α : Type u} {β : Type v} {f : α → β} (H : function.injective f) {a : α} {l : List α} : f a ∈ map f l ↔ a ∈ l := sorry theorem forall_mem_map_iff {α : Type u} {β : Type v} {f : α → β} {l : List α} {P : β → Prop} : (∀ (i : β), i ∈ map f l → P i) ↔ ∀ (j : α), j ∈ l → P (f j) := sorry @[simp] theorem map_eq_nil {α : Type u} {β : Type v} {f : α → β} {l : List α} : map f l = [] ↔ l = [] := sorry @[simp] theorem mem_join {α : Type u} {a : α} {L : List (List α)} : a ∈ join L ↔ ∃ (l : List α), l ∈ L ∧ a ∈ l := sorry theorem exists_of_mem_join {α : Type u} {a : α} {L : List (List α)} : a ∈ join L → ∃ (l : List α), l ∈ L ∧ a ∈ l := iff.mp mem_join theorem mem_join_of_mem {α : Type u} {a : α} {L : List (List α)} {l : List α} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := iff.mpr mem_join (Exists.intro l { left := lL, right := al }) @[simp] theorem mem_bind {α : Type u} {β : Type v} {b : β} {l : List α} {f : α → List β} : b ∈ list.bind l f ↔ ∃ (a : α), ∃ (H : a ∈ l), b ∈ f a := sorry theorem exists_of_mem_bind {α : Type u} {β : Type v} {b : β} {l : List α} {f : α → List β} : b ∈ list.bind l f → ∃ (a : α), ∃ (H : a ∈ l), b ∈ f a := iff.mp mem_bind theorem mem_bind_of_mem {α : Type u} {β : Type v} {b : β} {l : List α} {f : α → List β} {a : α} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := iff.mpr mem_bind (Exists.intro a (Exists.intro al h)) theorem bind_map {α : Type u} {β : Type v} {γ : Type w} {g : α → List β} {f : β → γ} (l : List α) : map f (list.bind l g) = list.bind l fun (a : α) => map f (g a) := sorry /-! ### length -/ theorem length_eq_zero {α : Type u} {l : List α} : length l = 0 ↔ l = [] := { mp := eq_nil_of_length_eq_zero, mpr := fun (h : l = []) => Eq.symm h ▸ rfl } @[simp] theorem length_singleton {α : Type u} (a : α) : length [a] = 1 := rfl theorem length_pos_of_mem {α : Type u} {a : α} {l : List α} : a ∈ l → 0 < length l := sorry theorem exists_mem_of_length_pos {α : Type u} {l : List α} : 0 < length l → ∃ (a : α), a ∈ l := sorry theorem length_pos_iff_exists_mem {α : Type u} {l : List α} : 0 < length l ↔ ∃ (a : α), a ∈ l := sorry theorem ne_nil_of_length_pos {α : Type u} {l : List α} : 0 < length l → l ≠ [] := fun (h1 : 0 < length l) (h2 : l = []) => lt_irrefl 0 (iff.mpr length_eq_zero h2 ▸ h1) theorem length_pos_of_ne_nil {α : Type u} {l : List α} : l ≠ [] → 0 < length l := fun (h : l ≠ []) => iff.mpr pos_iff_ne_zero fun (h0 : length l = 0) => h (iff.mp length_eq_zero h0) theorem length_pos_iff_ne_nil {α : Type u} {l : List α} : 0 < length l ↔ l ≠ [] := { mp := ne_nil_of_length_pos, mpr := length_pos_of_ne_nil } theorem length_eq_one {α : Type u} {l : List α} : length l = 1 ↔ ∃ (a : α), l = [a] := sorry theorem exists_of_length_succ {α : Type u} {n : ℕ} (l : List α) : length l = n + 1 → ∃ (h : α), ∃ (t : List α), l = h :: t := sorry @[simp] theorem length_injective_iff {α : Type u} : function.injective length ↔ subsingleton α := sorry @[simp] theorem length_injective {α : Type u} [subsingleton α] : function.injective length := iff.mpr length_injective_iff _inst_1 /-! ### set-theoretic notation of lists -/ theorem empty_eq {α : Type u} : ∅ = [] := Eq.refl ∅ theorem singleton_eq {α : Type u} (x : α) : singleton x = [x] := rfl theorem insert_neg {α : Type u} [DecidableEq α] {x : α} {l : List α} (h : ¬x ∈ l) : insert x l = x :: l := if_neg h theorem insert_pos {α : Type u} [DecidableEq α] {x : α} {l : List α} (h : x ∈ l) : insert x l = l := if_pos h theorem doubleton_eq {α : Type u} [DecidableEq α] {x : α} {y : α} (h : x ≠ y) : insert x (singleton y) = [x, y] := sorry /-! ### bounded quantifiers over lists -/ theorem forall_mem_nil {α : Type u} (p : α → Prop) (x : α) (H : x ∈ []) : p x := false.dcases_on (fun (H : x ∈ []) => p x) H theorem forall_mem_cons {α : Type u} {p : α → Prop} {a : α} {l : List α} : (∀ (x : α), x ∈ a :: l → p x) ↔ p a ∧ ∀ (x : α), x ∈ l → p x := ball_cons theorem forall_mem_of_forall_mem_cons {α : Type u} {p : α → Prop} {a : α} {l : List α} (h : ∀ (x : α), x ∈ a :: l → p x) (x : α) (H : x ∈ l) : p x := and.right (iff.mp forall_mem_cons h) theorem forall_mem_singleton {α : Type u} {p : α → Prop} {a : α} : (∀ (x : α), x ∈ [a] → p x) ↔ p a := sorry theorem forall_mem_append {α : Type u} {p : α → Prop} {l₁ : List α} {l₂ : List α} : (∀ (x : α), x ∈ l₁ ++ l₂ → p x) ↔ (∀ (x : α), x ∈ l₁ → p x) ∧ ∀ (x : α), x ∈ l₂ → p x := sorry theorem not_exists_mem_nil {α : Type u} (p : α → Prop) : ¬∃ (x : α), ∃ (H : x ∈ []), p x := sorry theorem exists_mem_cons_of {α : Type u} {p : α → Prop} {a : α} (l : List α) (h : p a) : ∃ (x : α), ∃ (H : x ∈ a :: l), p x := bex.intro a (mem_cons_self a l) h theorem exists_mem_cons_of_exists {α : Type u} {p : α → Prop} {a : α} {l : List α} (h : ∃ (x : α), ∃ (H : x ∈ l), p x) : ∃ (x : α), ∃ (H : x ∈ a :: l), p x := bex.elim h fun (x : α) (xl : x ∈ l) (px : p x) => bex.intro x (mem_cons_of_mem a xl) px theorem or_exists_of_exists_mem_cons {α : Type u} {p : α → Prop} {a : α} {l : List α} (h : ∃ (x : α), ∃ (H : x ∈ a :: l), p x) : p a ∨ ∃ (x : α), ∃ (H : x ∈ l), p x := sorry theorem exists_mem_cons_iff {α : Type u} (p : α → Prop) (a : α) (l : List α) : (∃ (x : α), ∃ (H : x ∈ a :: l), p x) ↔ p a ∨ ∃ (x : α), ∃ (H : x ∈ l), p x := { mp := or_exists_of_exists_mem_cons, mpr := fun (h : p a ∨ ∃ (x : α), ∃ (H : x ∈ l), p x) => or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists } /-! ### list subset -/ theorem subset_def {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ ⊆ l₂ ↔ ∀ {a : α}, a ∈ l₁ → a ∈ l₂ := iff.rfl theorem subset_append_of_subset_left {α : Type u} (l : List α) (l₁ : List α) (l₂ : List α) : l ⊆ l₁ → l ⊆ l₁ ++ l₂ := fun (s : l ⊆ l₁) => subset.trans s (subset_append_left l₁ l₂) theorem subset_append_of_subset_right {α : Type u} (l : List α) (l₁ : List α) (l₂ : List α) : l ⊆ l₂ → l ⊆ l₁ ++ l₂ := fun (s : l ⊆ l₂) => subset.trans s (subset_append_right l₁ l₂) @[simp] theorem cons_subset {α : Type u} {a : α} {l : List α} {m : List α} : a :: l ⊆ m ↔ a ∈ m ∧ l ⊆ m := sorry theorem cons_subset_of_subset_of_mem {α : Type u} {a : α} {l : List α} {m : List α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a :: l ⊆ m := iff.mpr cons_subset { left := ainm, right := lsubm } theorem append_subset_of_subset_of_subset {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := fun (a : α) (h : a ∈ l₁ ++ l₂) => or.elim (iff.mp mem_append h) l₁subl l₂subl @[simp] theorem append_subset_iff {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} : l₁ ++ l₂ ⊆ l ↔ l₁ ⊆ l ∧ l₂ ⊆ l := sorry theorem eq_nil_of_subset_nil {α : Type u} {l : List α} : l ⊆ [] → l = [] := sorry theorem eq_nil_iff_forall_not_mem {α : Type u} {l : List α} : l = [] ↔ ∀ (a : α), ¬a ∈ l := (fun (this : l = [] ↔ l ⊆ []) => this) { mp := fun (e : l = []) => e ▸ subset.refl l, mpr := eq_nil_of_subset_nil } theorem map_subset {α : Type u} {β : Type v} {l₁ : List α} {l₂ : List α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ := sorry theorem map_subset_iff {α : Type u} {β : Type v} {l₁ : List α} {l₂ : List α} (f : α → β) (h : function.injective f) : map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ := sorry /-! ### append -/ theorem append_eq_has_append {α : Type u} {L₁ : List α} {L₂ : List α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl @[simp] theorem singleton_append {α : Type u} {x : α} {l : List α} : [x] ++ l = x :: l := rfl theorem append_ne_nil_of_ne_nil_left {α : Type u} (s : List α) (t : List α) : s ≠ [] → s ++ t ≠ [] := sorry theorem append_ne_nil_of_ne_nil_right {α : Type u} (s : List α) (t : List α) : t ≠ [] → s ++ t ≠ [] := sorry @[simp] theorem append_eq_nil {α : Type u} {p : List α} {q : List α} : p ++ q = [] ↔ p = [] ∧ q = [] := sorry @[simp] theorem nil_eq_append_iff {α : Type u} {a : List α} {b : List α} : [] = a ++ b ↔ a = [] ∧ b = [] := eq.mpr (id (Eq._oldrec (Eq.refl ([] = a ++ b ↔ a = [] ∧ b = [])) (propext eq_comm))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ++ b = [] ↔ a = [] ∧ b = [])) (propext append_eq_nil))) (iff.refl (a = [] ∧ b = []))) theorem append_eq_cons_iff {α : Type u} {a : List α} {b : List α} {c : List α} {x : α} : a ++ b = x :: c ↔ a = [] ∧ b = x :: c ∨ ∃ (a' : List α), a = x :: a' ∧ c = a' ++ b := sorry theorem cons_eq_append_iff {α : Type u} {a : List α} {b : List α} {c : List α} {x : α} : x :: c = a ++ b ↔ a = [] ∧ b = x :: c ∨ ∃ (a' : List α), a = x :: a' ∧ c = a' ++ b := sorry theorem append_eq_append_iff {α : Type u} {a : List α} {b : List α} {c : List α} {d : List α} : a ++ b = c ++ d ↔ (∃ (a' : List α), c = a ++ a' ∧ b = a' ++ d) ∨ ∃ (c' : List α), a = c ++ c' ∧ d = c' ++ b := sorry @[simp] theorem split_at_eq_take_drop {α : Type u} (n : ℕ) (l : List α) : split_at n l = (take n l, drop n l) := sorry @[simp] theorem take_append_drop {α : Type u} (n : ℕ) (l : List α) : take n l ++ drop n l = l := sorry -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} : s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂ := sorry theorem append_inj_right {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : t₁ = t₂ := and.right (append_inj h hl) theorem append_inj_left {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : s₁ = s₂ := and.left (append_inj h hl) theorem append_inj' {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ ∧ t₁ = t₂ := sorry theorem append_inj_right' {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : t₁ = t₂ := and.right (append_inj' h hl) theorem append_inj_left' {α : Type u} {s₁ : List α} {s₂ : List α} {t₁ : List α} {t₂ : List α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ := and.left (append_inj' h hl) theorem append_left_cancel {α : Type u} {s : List α} {t₁ : List α} {t₂ : List α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ := append_inj_right h rfl theorem append_right_cancel {α : Type u} {s₁ : List α} {s₂ : List α} {t : List α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ := append_inj_left' h rfl theorem append_right_injective {α : Type u} (s : List α) : function.injective fun (t : List α) => s ++ t := fun (t₁ t₂ : List α) => append_left_cancel theorem append_right_inj {α : Type u} {t₁ : List α} {t₂ : List α} (s : List α) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ := function.injective.eq_iff (append_right_injective s) theorem append_left_injective {α : Type u} (t : List α) : function.injective fun (s : List α) => s ++ t := fun (s₁ s₂ : List α) => append_right_cancel theorem append_left_inj {α : Type u} {s₁ : List α} {s₂ : List α} (t : List α) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ := function.injective.eq_iff (append_left_injective t) theorem map_eq_append_split {α : Type u} {β : Type v} {f : α → β} {l : List α} {s₁ : List β} {s₂ : List β} (h : map f l = s₁ ++ s₂) : ∃ (l₁ : List α), ∃ (l₂ : List α), l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ := sorry /-! ### repeat -/ @[simp] theorem repeat_succ {α : Type u} (a : α) (n : ℕ) : repeat a (n + 1) = a :: repeat a n := rfl theorem eq_of_mem_repeat {α : Type u} {a : α} {b : α} {n : ℕ} : b ∈ repeat a n → b = a := sorry theorem eq_repeat_of_mem {α : Type u} {a : α} {l : List α} : (∀ (b : α), b ∈ l → b = a) → l = repeat a (length l) := sorry theorem eq_repeat' {α : Type u} {a : α} {l : List α} : l = repeat a (length l) ↔ ∀ (b : α), b ∈ l → b = a := { mp := fun (h : l = repeat a (length l)) => Eq.symm h ▸ fun (b : α) => eq_of_mem_repeat, mpr := eq_repeat_of_mem } theorem eq_repeat {α : Type u} {a : α} {n : ℕ} {l : List α} : l = repeat a n ↔ length l = n ∧ ∀ (b : α), b ∈ l → b = a := sorry theorem repeat_add {α : Type u} (a : α) (m : ℕ) (n : ℕ) : repeat a (m + n) = repeat a m ++ repeat a n := sorry theorem repeat_subset_singleton {α : Type u} (a : α) (n : ℕ) : repeat a n ⊆ [a] := fun (b : α) (h : b ∈ repeat a n) => iff.mpr mem_singleton (eq_of_mem_repeat h) @[simp] theorem map_const {α : Type u} {β : Type v} (l : List α) (b : β) : map (function.const α b) l = repeat b (length l) := sorry theorem eq_of_mem_map_const {α : Type u} {β : Type v} {b₁ : β} {b₂ : β} {l : List α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := eq_of_mem_repeat (eq.mp (Eq._oldrec (Eq.refl (b₁ ∈ map (function.const α b₂) l)) (map_const l b₂)) h) @[simp] theorem map_repeat {α : Type u} {β : Type v} (f : α → β) (a : α) (n : ℕ) : map f (repeat a n) = repeat (f a) n := sorry @[simp] theorem tail_repeat {α : Type u} (a : α) (n : ℕ) : tail (repeat a n) = repeat a (Nat.pred n) := nat.cases_on n (Eq.refl (tail (repeat a 0))) fun (n : ℕ) => Eq.refl (tail (repeat a (Nat.succ n))) @[simp] theorem join_repeat_nil {α : Type u} (n : ℕ) : join (repeat [] n) = [] := sorry /-! ### pure -/ @[simp] theorem mem_pure {α : Type u_1} (x : α) (y : α) : x ∈ pure y ↔ x = y := sorry /-! ### bind -/ @[simp] theorem bind_eq_bind {α : Type u_1} {β : Type u_1} (f : α → List β) (l : List α) : l >>= f = list.bind l f := rfl @[simp] theorem bind_append {α : Type u} {β : Type v} (f : α → List β) (l₁ : List α) (l₂ : List α) : list.bind (l₁ ++ l₂) f = list.bind l₁ f ++ list.bind l₂ f := append_bind l₁ l₂ f @[simp] theorem bind_singleton {α : Type u} {β : Type v} (f : α → List β) (x : α) : list.bind [x] f = f x := append_nil (f x) /-! ### concat -/ theorem concat_nil {α : Type u} (a : α) : concat [] a = [a] := rfl theorem concat_cons {α : Type u} (a : α) (b : α) (l : List α) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_eq_append {α : Type u} (a : α) (l : List α) : concat l a = l ++ [a] := sorry theorem init_eq_of_concat_eq {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : concat l₁ a = concat l₂ a → l₁ = l₂ := sorry theorem last_eq_of_concat_eq {α : Type u} {a : α} {b : α} {l : List α} : concat l a = concat l b → a = b := sorry theorem concat_ne_nil {α : Type u} (a : α) (l : List α) : concat l a ≠ [] := sorry theorem concat_append {α : Type u} (a : α) (l₁ : List α) (l₂ : List α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ := sorry theorem length_concat {α : Type u} (a : α) (l : List α) : length (concat l a) = Nat.succ (length l) := sorry theorem append_concat {α : Type u} (a : α) (l₁ : List α) (l₂ : List α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a := sorry /-! ### reverse -/ @[simp] theorem reverse_nil {α : Type u} : reverse [] = [] := rfl @[simp] theorem reverse_cons {α : Type u} (a : α) (l : List α) : reverse (a :: l) = reverse l ++ [a] := sorry theorem reverse_core_eq {α : Type u} (l₁ : List α) (l₂ : List α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ := sorry theorem reverse_cons' {α : Type u} (a : α) (l : List α) : reverse (a :: l) = concat (reverse l) a := sorry @[simp] theorem reverse_singleton {α : Type u} (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_append {α : Type u} (s : List α) (t : List α) : reverse (s ++ t) = reverse t ++ reverse s := sorry theorem reverse_concat {α : Type u} (l : List α) (a : α) : reverse (concat l a) = a :: reverse l := sorry @[simp] theorem reverse_reverse {α : Type u} (l : List α) : reverse (reverse l) = l := sorry @[simp] theorem reverse_involutive {α : Type u} : function.involutive reverse := fun (l : List α) => reverse_reverse l @[simp] theorem reverse_injective {α : Type u} : function.injective reverse := function.involutive.injective reverse_involutive @[simp] theorem reverse_inj {α : Type u} {l₁ : List α} {l₂ : List α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ := function.injective.eq_iff reverse_injective @[simp] theorem reverse_eq_nil {α : Type u} {l : List α} : reverse l = [] ↔ l = [] := reverse_inj theorem concat_eq_reverse_cons {α : Type u} (a : α) (l : List α) : concat l a = reverse (a :: reverse l) := sorry @[simp] theorem length_reverse {α : Type u} (l : List α) : length (reverse l) = length l := sorry @[simp] theorem map_reverse {α : Type u} {β : Type v} (f : α → β) (l : List α) : map f (reverse l) = reverse (map f l) := sorry theorem map_reverse_core {α : Type u} {β : Type v} (f : α → β) (l₁ : List α) (l₂ : List α) : map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) := sorry @[simp] theorem mem_reverse {α : Type u} {a : α} {l : List α} : a ∈ reverse l ↔ a ∈ l := sorry @[simp] theorem reverse_repeat {α : Type u} (a : α) (n : ℕ) : reverse (repeat a n) = repeat a n := sorry /-! ### is_nil -/ theorem is_nil_iff_eq_nil {α : Type u} {l : List α} : ↥(is_nil l) ↔ l = [] := sorry /-! ### init -/ @[simp] theorem length_init {α : Type u} (l : List α) : length (init l) = length l - 1 := sorry /-! ### last -/ @[simp] theorem last_cons {α : Type u} {a : α} {l : List α} (h₁ : a :: l ≠ []) (h₂ : l ≠ []) : last (a :: l) h₁ = last l h₂ := sorry @[simp] theorem last_append {α : Type u} {a : α} (l : List α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a := sorry theorem last_concat {α : Type u} {a : α} (l : List α) (h : concat l a ≠ []) : last (concat l a) h = a := sorry @[simp] theorem last_singleton {α : Type u} (a : α) (h : [a] ≠ []) : last [a] h = a := rfl @[simp] theorem last_cons_cons {α : Type u} (a₁ : α) (a₂ : α) (l : List α) (h : a₁ :: a₂ :: l ≠ []) : last (a₁ :: a₂ :: l) h = last (a₂ :: l) (cons_ne_nil a₂ l) := rfl theorem init_append_last {α : Type u} {l : List α} (h : l ≠ []) : init l ++ [last l h] = l := sorry theorem last_congr {α : Type u} {l₁ : List α} {l₂ : List α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := Eq._oldrec (fun (h₁ : l₂ ≠ []) => Eq.refl (last l₂ h₁)) (Eq.symm h₃) h₁ theorem last_mem {α : Type u} {l : List α} (h : l ≠ []) : last l h ∈ l := sorry theorem last_repeat_succ (a : ℕ) (m : ℕ) : last (repeat a (Nat.succ m)) (ne_nil_of_length_eq_succ ((fun (this : length (repeat a (Nat.succ m)) = Nat.succ m) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (length (repeat a (Nat.succ m)) = Nat.succ m)) (length_repeat a (Nat.succ m)))) (Eq.refl (Nat.succ m))))) = a := sorry /-! ### last' -/ @[simp] theorem last'_is_none {α : Type u} {l : List α} : ↥(option.is_none (last' l)) ↔ l = [] := sorry @[simp] theorem last'_is_some {α : Type u} {l : List α} : ↥(option.is_some (last' l)) ↔ l ≠ [] := sorry theorem mem_last'_eq_last {α : Type u} {l : List α} {x : α} : x ∈ last' l → ∃ (h : l ≠ []), x = last l h := sorry theorem mem_of_mem_last' {α : Type u} {l : List α} {a : α} (ha : a ∈ last' l) : a ∈ l := sorry theorem init_append_last' {α : Type u} {l : List α} (a : α) (H : a ∈ last' l) : init l ++ [a] = l := sorry theorem ilast_eq_last' {α : Type u} [Inhabited α] (l : List α) : ilast l = option.iget (last' l) := sorry @[simp] theorem last'_append_cons {α : Type u} (l₁ : List α) (a : α) (l₂ : List α) : last' (l₁ ++ a :: l₂) = last' (a :: l₂) := sorry theorem last'_append_of_ne_nil {α : Type u} (l₁ : List α) {l₂ : List α} (hl₂ : l₂ ≠ []) : last' (l₁ ++ l₂) = last' l₂ := sorry /-! ### head(') and tail -/ theorem head_eq_head' {α : Type u} [Inhabited α] (l : List α) : head l = option.iget (head' l) := list.cases_on l (Eq.refl (head [])) fun (l_hd : α) (l_tl : List α) => Eq.refl (head (l_hd :: l_tl)) theorem mem_of_mem_head' {α : Type u} {x : α} {l : List α} : x ∈ head' l → x ∈ l := sorry @[simp] theorem head_cons {α : Type u} [Inhabited α] (a : α) (l : List α) : head (a :: l) = a := rfl @[simp] theorem tail_nil {α : Type u} : tail [] = [] := rfl @[simp] theorem tail_cons {α : Type u} (a : α) (l : List α) : tail (a :: l) = l := rfl @[simp] theorem head_append {α : Type u} [Inhabited α] (t : List α) {s : List α} (h : s ≠ []) : head (s ++ t) = head s := sorry theorem tail_append_singleton_of_ne_nil {α : Type u} {a : α} {l : List α} (h : l ≠ []) : tail (l ++ [a]) = tail l ++ [a] := sorry theorem cons_head'_tail {α : Type u} {l : List α} {a : α} (h : a ∈ head' l) : a :: tail l = l := sorry theorem head_mem_head' {α : Type u} [Inhabited α] {l : List α} (h : l ≠ []) : head l ∈ head' l := list.cases_on l (fun (h : [] ≠ []) => idRhs (head [] ∈ head' []) (absurd (Eq.refl []) h)) (fun (l_hd : α) (l_tl : List α) (h : l_hd :: l_tl ≠ []) => idRhs (head' (l_hd :: l_tl) = head' (l_hd :: l_tl)) rfl) h theorem cons_head_tail {α : Type u} [Inhabited α] {l : List α} (h : l ≠ []) : head l :: tail l = l := cons_head'_tail (head_mem_head' h) @[simp] theorem head'_map {α : Type u} {β : Type v} (f : α → β) (l : List α) : head' (map f l) = option.map f (head' l) := list.cases_on l (Eq.refl (head' (map f []))) fun (l_hd : α) (l_tl : List α) => Eq.refl (head' (map f (l_hd :: l_tl))) /-! ### Induction from the right -/ /-- Induction principle from the right for lists: if a property holds for the empty list, and for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def reverse_rec_on {α : Type u} {C : List α → Sort u_1} (l : List α) (H0 : C []) (H1 : (l : List α) → (a : α) → C l → C (l ++ [a])) : C l := eq.mpr sorry (List.rec H0 (fun (hd : α) (tl : List α) (ih : C (reverse tl)) => eq.mpr sorry (H1 (reverse tl) hd ih)) (reverse l)) /-- Bidirectional induction principle for lists: if a property holds for the empty list, the singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def bidirectional_rec {α : Type u} {C : List α → Sort u_1} (H0 : C []) (H1 : (a : α) → C [a]) (Hn : (a : α) → (l : List α) → (b : α) → C l → C (a :: (l ++ [b]))) (l : List α) : C l := sorry /-- Like `bidirectional_rec`, but with the list parameter placed first. -/ def bidirectional_rec_on {α : Type u} {C : List α → Sort u_1} (l : List α) (H0 : C []) (H1 : (a : α) → C [a]) (Hn : (a : α) → (l : List α) → (b : α) → C l → C (a :: (l ++ [b]))) : C l := bidirectional_rec H0 H1 Hn l /-! ### sublists -/ @[simp] theorem nil_sublist {α : Type u} (l : List α) : [] <+ l := sorry @[simp] theorem sublist.refl {α : Type u} (l : List α) : l <+ l := sorry theorem sublist.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ := sorry @[simp] theorem sublist_cons {α : Type u} (a : α) (l : List α) : l <+ a :: l := sublist.cons l l a (sublist.refl l) theorem sublist_of_cons_sublist {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : a :: l₁ <+ l₂ → l₁ <+ l₂ := sublist.trans (sublist_cons a l₁) theorem cons_sublist_cons {α : Type u} {l₁ : List α} {l₂ : List α} (a : α) (s : l₁ <+ l₂) : a :: l₁ <+ a :: l₂ := sublist.cons2 l₁ l₂ a s @[simp] theorem sublist_append_left {α : Type u} (l₁ : List α) (l₂ : List α) : l₁ <+ l₁ ++ l₂ := sorry @[simp] theorem sublist_append_right {α : Type u} (l₁ : List α) (l₂ : List α) : l₂ <+ l₁ ++ l₂ := sorry theorem sublist_cons_of_sublist {α : Type u} (a : α) {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → l₁ <+ a :: l₂ := sublist.cons l₁ l₂ a theorem sublist_append_of_sublist_left {α : Type u} {l : List α} {l₁ : List α} {l₂ : List α} (s : l <+ l₁) : l <+ l₁ ++ l₂ := sublist.trans s (sublist_append_left l₁ l₂) theorem sublist_append_of_sublist_right {α : Type u} {l : List α} {l₁ : List α} {l₂ : List α} (s : l <+ l₂) : l <+ l₁ ++ l₂ := sublist.trans s (sublist_append_right l₁ l₂) theorem sublist_of_cons_sublist_cons {α : Type u} {l₁ : List α} {l₂ : List α} {a : α} : a :: l₁ <+ a :: l₂ → l₁ <+ l₂ := sorry theorem cons_sublist_cons_iff {α : Type u} {l₁ : List α} {l₂ : List α} {a : α} : a :: l₁ <+ a :: l₂ ↔ l₁ <+ l₂ := { mp := sublist_of_cons_sublist_cons, mpr := cons_sublist_cons a } @[simp] theorem append_sublist_append_left {α : Type u} {l₁ : List α} {l₂ : List α} (l : List α) : l ++ l₁ <+ l ++ l₂ ↔ l₁ <+ l₂ := sorry theorem sublist.append_right {α : Type u} {l₁ : List α} {l₂ : List α} (h : l₁ <+ l₂) (l : List α) : l₁ ++ l <+ l₂ ++ l := sublist.drec (sublist.refl ([] ++ l)) (fun (h_l₁ h_l₂ : List α) (a : α) (h_ᾰ : h_l₁ <+ h_l₂) (ih : h_l₁ ++ l <+ h_l₂ ++ l) => sublist_cons_of_sublist a ih) (fun (h_l₁ h_l₂ : List α) (a : α) (h_ᾰ : h_l₁ <+ h_l₂) (ih : h_l₁ ++ l <+ h_l₂ ++ l) => cons_sublist_cons a ih) h theorem sublist_or_mem_of_sublist {α : Type u} {l : List α} {l₁ : List α} {l₂ : List α} {a : α} (h : l <+ l₁ ++ a :: l₂) : l <+ l₁ ++ l₂ ∨ a ∈ l := sorry theorem sublist.reverse {α : Type u} {l₁ : List α} {l₂ : List α} (h : l₁ <+ l₂) : reverse l₁ <+ reverse l₂ := sorry @[simp] theorem reverse_sublist_iff {α : Type u} {l₁ : List α} {l₂ : List α} : reverse l₁ <+ reverse l₂ ↔ l₁ <+ l₂ := { mp := fun (h : reverse l₁ <+ reverse l₂) => reverse_reverse l₁ ▸ reverse_reverse l₂ ▸ sublist.reverse h, mpr := sublist.reverse } @[simp] theorem append_sublist_append_right {α : Type u} {l₁ : List α} {l₂ : List α} (l : List α) : l₁ ++ l <+ l₂ ++ l ↔ l₁ <+ l₂ := sorry theorem sublist.append {α : Type u} {l₁ : List α} {l₂ : List α} {r₁ : List α} {r₂ : List α} (hl : l₁ <+ l₂) (hr : r₁ <+ r₂) : l₁ ++ r₁ <+ l₂ ++ r₂ := sublist.trans (sublist.append_right hl r₁) (iff.mpr (append_sublist_append_left l₂) hr) theorem sublist.subset {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → l₁ ⊆ l₂ := sorry theorem singleton_sublist {α : Type u} {a : α} {l : List α} : [a] <+ l ↔ a ∈ l := sorry theorem eq_nil_of_sublist_nil {α : Type u} {l : List α} (s : l <+ []) : l = [] := eq_nil_of_subset_nil (sublist.subset s) theorem repeat_sublist_repeat {α : Type u} (a : α) {m : ℕ} {n : ℕ} : repeat a m <+ repeat a n ↔ m ≤ n := sorry theorem eq_of_sublist_of_length_eq {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂ := sorry theorem eq_of_sublist_of_length_le {α : Type u} {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) : l₁ = l₂ := eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h) theorem sublist.antisymm {α : Type u} {l₁ : List α} {l₂ : List α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂) protected instance decidable_sublist {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : Decidable (l₁ <+ l₂) := sorry /-! ### index_of -/ @[simp] theorem index_of_nil {α : Type u} [DecidableEq α] (a : α) : index_of a [] = 0 := rfl theorem index_of_cons {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : index_of a (b :: l) = ite (a = b) 0 (Nat.succ (index_of a l)) := rfl theorem index_of_cons_eq {α : Type u} [DecidableEq α] {a : α} {b : α} (l : List α) : a = b → index_of a (b :: l) = 0 := fun (e : a = b) => if_pos e @[simp] theorem index_of_cons_self {α : Type u} [DecidableEq α] (a : α) (l : List α) : index_of a (a :: l) = 0 := index_of_cons_eq l rfl @[simp] theorem index_of_cons_ne {α : Type u} [DecidableEq α] {a : α} {b : α} (l : List α) : a ≠ b → index_of a (b :: l) = Nat.succ (index_of a l) := fun (n : a ≠ b) => if_neg n theorem index_of_eq_length {α : Type u} [DecidableEq α] {a : α} {l : List α} : index_of a l = length l ↔ ¬a ∈ l := sorry @[simp] theorem index_of_of_not_mem {α : Type u} [DecidableEq α] {l : List α} {a : α} : ¬a ∈ l → index_of a l = length l := iff.mpr index_of_eq_length theorem index_of_le_length {α : Type u} [DecidableEq α] {a : α} {l : List α} : index_of a l ≤ length l := sorry theorem index_of_lt_length {α : Type u} [DecidableEq α] {a : α} {l : List α} : index_of a l < length l ↔ a ∈ l := sorry /-! ### nth element -/ theorem nth_le_of_mem {α : Type u} {a : α} {l : List α} : a ∈ l → ∃ (n : ℕ), ∃ (h : n < length l), nth_le l n h = a := sorry theorem nth_le_nth {α : Type u} {l : List α} {n : ℕ} (h : n < length l) : nth l n = some (nth_le l n h) := sorry theorem nth_len_le {α : Type u} {l : List α} {n : ℕ} : length l ≤ n → nth l n = none := sorry theorem nth_eq_some {α : Type u} {l : List α} {n : ℕ} {a : α} : nth l n = some a ↔ ∃ (h : n < length l), nth_le l n h = a := sorry @[simp] theorem nth_eq_none_iff {α : Type u} {l : List α} {n : ℕ} : nth l n = none ↔ length l ≤ n := sorry theorem nth_of_mem {α : Type u} {a : α} {l : List α} (h : a ∈ l) : ∃ (n : ℕ), nth l n = some a := sorry theorem nth_le_mem {α : Type u} (l : List α) (n : ℕ) (h : n < length l) : nth_le l n h ∈ l := sorry theorem nth_mem {α : Type u} {l : List α} {n : ℕ} {a : α} (e : nth l n = some a) : a ∈ l := sorry theorem mem_iff_nth_le {α : Type u} {a : α} {l : List α} : a ∈ l ↔ ∃ (n : ℕ), ∃ (h : n < length l), nth_le l n h = a := sorry theorem mem_iff_nth {α : Type u} {a : α} {l : List α} : a ∈ l ↔ ∃ (n : ℕ), nth l n = some a := iff.trans mem_iff_nth_le (exists_congr fun (n : ℕ) => iff.symm nth_eq_some) theorem nth_zero {α : Type u} (l : List α) : nth l 0 = head' l := list.cases_on l (Eq.refl (nth [] 0)) fun (l_hd : α) (l_tl : List α) => Eq.refl (nth (l_hd :: l_tl) 0) theorem nth_injective {α : Type u} {xs : List α} {i : ℕ} {j : ℕ} (h₀ : i < length xs) (h₁ : nodup xs) (h₂ : nth xs i = nth xs j) : i = j := sorry @[simp] theorem nth_map {α : Type u} {β : Type v} (f : α → β) (l : List α) (n : ℕ) : nth (map f l) n = option.map f (nth l n) := sorry theorem nth_le_map {α : Type u} {β : Type v} (f : α → β) {l : List α} {n : ℕ} (H1 : n < length (map f l)) (H2 : n < length l) : nth_le (map f l) n H1 = f (nth_le l n H2) := sorry /-- A version of `nth_le_map` that can be used for rewriting. -/ theorem nth_le_map_rev {α : Type u} {β : Type v} (f : α → β) {l : List α} {n : ℕ} (H : n < length l) : f (nth_le l n H) = nth_le (map f l) n (Eq.symm (length_map f l) ▸ H) := Eq.symm (nth_le_map f (Eq.symm (length_map f l) ▸ H) H) @[simp] theorem nth_le_map' {α : Type u} {β : Type v} (f : α → β) {l : List α} {n : ℕ} (H : n < length (map f l)) : nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) := nth_le_map f H (length_map f l ▸ H) /-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as `hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make such a rewrite, with `rw (nth_le_of_eq h)`. -/ theorem nth_le_of_eq {α : Type u} {L : List α} {L' : List α} (h : L = L') {i : ℕ} (hi : i < length L) : nth_le L i hi = nth_le L' i (h ▸ hi) := sorry @[simp] theorem nth_le_singleton {α : Type u} (a : α) {n : ℕ} (hn : n < 1) : nth_le [a] n hn = a := (fun (hn0 : n = 0) => Eq._oldrec (fun (hn : 0 < 1) => Eq.refl (nth_le [a] 0 hn)) (Eq.symm hn0) hn) (iff.mp nat.le_zero_iff (nat.le_of_lt_succ hn)) theorem nth_le_zero {α : Type u} [Inhabited α] {L : List α} (h : 0 < length L) : nth_le L 0 h = head L := sorry theorem nth_le_append {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (hn₁ : n < length (l₁ ++ l₂)) (hn₂ : n < length l₁) : nth_le (l₁ ++ l₂) n hn₁ = nth_le l₁ n hn₂ := sorry theorem nth_le_append_right_aux {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (h₁ : length l₁ ≤ n) (h₂ : n < length (l₁ ++ l₂)) : n - length l₁ < length l₂ := sorry theorem nth_le_append_right {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (h₁ : length l₁ ≤ n) (h₂ : n < length (l₁ ++ l₂)) : nth_le (l₁ ++ l₂) n h₂ = nth_le l₂ (n - length l₁) (nth_le_append_right_aux h₁ h₂) := sorry @[simp] theorem nth_le_repeat {α : Type u} (a : α) {n : ℕ} {m : ℕ} (h : m < length (repeat a n)) : nth_le (repeat a n) m h = a := eq_of_mem_repeat (nth_le_mem (repeat a n) m h) theorem nth_append {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (hn : n < length l₁) : nth (l₁ ++ l₂) n = nth l₁ n := sorry theorem nth_append_right {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (hn : length l₁ ≤ n) : nth (l₁ ++ l₂) n = nth l₂ (n - length l₁) := sorry theorem last_eq_nth_le {α : Type u} (l : List α) (h : l ≠ []) : last l h = nth_le l (length l - 1) (nat.sub_lt (length_pos_of_ne_nil h) nat.one_pos) := sorry @[simp] theorem nth_concat_length {α : Type u} (l : List α) (a : α) : nth (l ++ [a]) (length l) = some a := sorry theorem ext {α : Type u} {l₁ : List α} {l₂ : List α} : (∀ (n : ℕ), nth l₁ n = nth l₂ n) → l₁ = l₂ := sorry theorem ext_le {α : Type u} {l₁ : List α} {l₂ : List α} (hl : length l₁ = length l₂) (h : ∀ (n : ℕ) (h₁ : n < length l₁) (h₂ : n < length l₂), nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ := sorry @[simp] theorem index_of_nth_le {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : index_of a l < length l) : nth_le l (index_of a l) h = a := sorry @[simp] theorem index_of_nth {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : nth l (index_of a l) = some a := sorry theorem nth_le_reverse_aux1 {α : Type u} (l : List α) (r : List α) (i : ℕ) (h1 : i + length l < length (reverse_core l r)) (h2 : i < length r) : nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 := sorry theorem index_of_inj {α : Type u} [DecidableEq α] {l : List α} {x : α} {y : α} (hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y := sorry theorem nth_le_reverse_aux2 {α : Type u} (l : List α) (r : List α) (i : ℕ) (h1 : length l - 1 - i < length (reverse_core l r)) (h2 : i < length l) : nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 := sorry @[simp] theorem nth_le_reverse {α : Type u} (l : List α) (i : ℕ) (h1 : length l - 1 - i < length (reverse l)) (h2 : i < length l) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 l [] i h1 h2 theorem eq_cons_of_length_one {α : Type u} {l : List α} (h : length l = 1) : l = [nth_le l 0 (Eq.symm h ▸ zero_lt_one)] := sorry theorem modify_nth_tail_modify_nth_tail {α : Type u} {f : List α → List α} {g : List α → List α} (m : ℕ) (n : ℕ) (l : List α) : modify_nth_tail g (m + n) (modify_nth_tail f n l) = modify_nth_tail (fun (l : List α) => modify_nth_tail g m (f l)) n l := sorry theorem modify_nth_tail_modify_nth_tail_le {α : Type u} {f : List α → List α} {g : List α → List α} (m : ℕ) (n : ℕ) (l : List α) (h : n ≤ m) : modify_nth_tail g m (modify_nth_tail f n l) = modify_nth_tail (fun (l : List α) => modify_nth_tail g (m - n) (f l)) n l := sorry theorem modify_nth_tail_modify_nth_tail_same {α : Type u} {f : List α → List α} {g : List α → List α} (n : ℕ) (l : List α) : modify_nth_tail g n (modify_nth_tail f n l) = modify_nth_tail (g ∘ f) n l := sorry theorem modify_nth_tail_id {α : Type u} (n : ℕ) (l : List α) : modify_nth_tail id n l = l := sorry theorem remove_nth_eq_nth_tail {α : Type u} (n : ℕ) (l : List α) : remove_nth l n = modify_nth_tail tail n l := sorry theorem update_nth_eq_modify_nth {α : Type u} (a : α) (n : ℕ) (l : List α) : update_nth l n a = modify_nth (fun (_x : α) => a) n l := sorry theorem modify_nth_eq_update_nth {α : Type u} (f : α → α) (n : ℕ) (l : List α) : modify_nth f n l = option.get_or_else ((fun (a : α) => update_nth l n (f a)) <$> nth l n) l := sorry theorem nth_modify_nth {α : Type u} (f : α → α) (n : ℕ) (l : List α) (m : ℕ) : nth (modify_nth f n l) m = (fun (a : α) => ite (n = m) (f a) a) <$> nth l m := sorry theorem modify_nth_tail_length {α : Type u} (f : List α → List α) (H : ∀ (l : List α), length (f l) = length l) (n : ℕ) (l : List α) : length (modify_nth_tail f n l) = length l := sorry @[simp] theorem modify_nth_length {α : Type u} (f : α → α) (n : ℕ) (l : List α) : length (modify_nth f n l) = length l := sorry @[simp] theorem update_nth_length {α : Type u} (l : List α) (n : ℕ) (a : α) : length (update_nth l n a) = length l := sorry @[simp] theorem nth_modify_nth_eq {α : Type u} (f : α → α) (n : ℕ) (l : List α) : nth (modify_nth f n l) n = f <$> nth l n := sorry @[simp] theorem nth_modify_nth_ne {α : Type u} (f : α → α) {m : ℕ} {n : ℕ} (l : List α) (h : m ≠ n) : nth (modify_nth f m l) n = nth l n := sorry theorem nth_update_nth_eq {α : Type u} (a : α) (n : ℕ) (l : List α) : nth (update_nth l n a) n = (fun (_x : α) => a) <$> nth l n := sorry theorem nth_update_nth_of_lt {α : Type u} (a : α) {n : ℕ} {l : List α} (h : n < length l) : nth (update_nth l n a) n = some a := eq.mpr (id (Eq._oldrec (Eq.refl (nth (update_nth l n a) n = some a)) (nth_update_nth_eq a n l))) (eq.mpr (id (Eq._oldrec (Eq.refl ((fun (_x : α) => a) <$> nth l n = some a)) (nth_le_nth h))) (Eq.refl ((fun (_x : α) => a) <$> some (nth_le l n h)))) theorem nth_update_nth_ne {α : Type u} (a : α) {m : ℕ} {n : ℕ} (l : List α) (h : m ≠ n) : nth (update_nth l m a) n = nth l n := sorry @[simp] theorem nth_le_update_nth_eq {α : Type u} (l : List α) (i : ℕ) (a : α) (h : i < length (update_nth l i a)) : nth_le (update_nth l i a) i h = a := sorry @[simp] theorem nth_le_update_nth_of_ne {α : Type u} {l : List α} {i : ℕ} {j : ℕ} (h : i ≠ j) (a : α) (hj : j < length (update_nth l i a)) : nth_le (update_nth l i a) j hj = nth_le l j (eq.mpr (id (Eq.refl (j < length l))) (eq.mp ((fun (ᾰ ᾰ_1 : ℕ) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ℕ) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg Less e_2) e_3) j j (Eq.refl j) (length (update_nth l i a)) (length l) (update_nth_length l i a)) hj)) := sorry theorem mem_or_eq_of_mem_update_nth {α : Type u} {l : List α} {n : ℕ} {a : α} {b : α} (h : a ∈ update_nth l n b) : a ∈ l ∨ a = b := sorry @[simp] theorem insert_nth_nil {α : Type u} (a : α) : insert_nth 0 a [] = [a] := rfl @[simp] theorem insert_nth_succ_nil {α : Type u} (n : ℕ) (a : α) : insert_nth (n + 1) a [] = [] := rfl theorem length_insert_nth {α : Type u} {a : α} (n : ℕ) (as : List α) : n ≤ length as → length (insert_nth n a as) = length as + 1 := sorry theorem remove_nth_insert_nth {α : Type u} {a : α} (n : ℕ) (l : List α) : remove_nth (insert_nth n a l) n = l := sorry theorem insert_nth_remove_nth_of_ge {α : Type u} {a : α} (n : ℕ) (m : ℕ) (as : List α) : n < length as → n ≤ m → insert_nth m a (remove_nth as n) = remove_nth (insert_nth (m + 1) a as) n := sorry theorem insert_nth_remove_nth_of_le {α : Type u} {a : α} (n : ℕ) (m : ℕ) (as : List α) : n < length as → m ≤ n → insert_nth m a (remove_nth as n) = remove_nth (insert_nth m a as) (n + 1) := sorry theorem insert_nth_comm {α : Type u} (a : α) (b : α) (i : ℕ) (j : ℕ) (l : List α) (h : i ≤ j) (hj : j ≤ length l) : insert_nth (j + 1) b (insert_nth i a l) = insert_nth i a (insert_nth j b l) := sorry theorem mem_insert_nth {α : Type u} {a : α} {b : α} {n : ℕ} {l : List α} (hi : n ≤ length l) : a ∈ insert_nth n b l ↔ a = b ∨ a ∈ l := sorry /-! ### map -/ @[simp] theorem map_nil {α : Type u} {β : Type v} (f : α → β) : map f [] = [] := rfl theorem map_eq_foldr {α : Type u} {β : Type v} (f : α → β) (l : List α) : map f l = foldr (fun (a : α) (bs : List β) => f a :: bs) [] l := sorry theorem map_congr {α : Type u} {β : Type v} {f : α → β} {g : α → β} {l : List α} : (∀ (x : α), x ∈ l → f x = g x) → map f l = map g l := sorry theorem map_eq_map_iff {α : Type u} {β : Type v} {f : α → β} {g : α → β} {l : List α} : map f l = map g l ↔ ∀ (x : α), x ∈ l → f x = g x := sorry theorem map_concat {α : Type u} {β : Type v} (f : α → β) (a : α) (l : List α) : map f (concat l a) = concat (map f l) (f a) := sorry theorem map_id' {α : Type u} {f : α → α} (h : ∀ (x : α), f x = x) (l : List α) : map f l = l := sorry theorem eq_nil_of_map_eq_nil {α : Type u} {β : Type v} {f : α → β} {l : List α} (h : map f l = []) : l = [] := eq_nil_of_length_eq_zero (eq.mpr (id (Eq._oldrec (Eq.refl (length l = 0)) (Eq.symm (length_map f l)))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (map f l) = 0)) h)) (Eq.refl (length [])))) @[simp] theorem map_join {α : Type u} {β : Type v} (f : α → β) (L : List (List α)) : map f (join L) = join (map (map f) L) := sorry theorem bind_ret_eq_map {α : Type u} {β : Type v} (f : α → β) (l : List α) : list.bind l (list.ret ∘ f) = map f l := sorry @[simp] theorem map_eq_map {α : Type u_1} {β : Type u_1} (f : α → β) (l : List α) : f <$> l = map f l := rfl @[simp] theorem map_tail {α : Type u} {β : Type v} (f : α → β) (l : List α) : map f (tail l) = tail (map f l) := list.cases_on l (Eq.refl (map f (tail []))) fun (l_hd : α) (l_tl : List α) => Eq.refl (map f (tail (l_hd :: l_tl))) @[simp] theorem map_injective_iff {α : Type u} {β : Type v} {f : α → β} : function.injective (map f) ↔ function.injective f := sorry /-- A single `list.map` of a composition of functions is equal to composing a `list.map` with another `list.map`, fully applied. This is the reverse direction of `list.map_map`. -/ theorem comp_map {α : Type u} {β : Type v} {γ : Type w} (h : β → γ) (g : α → β) (l : List α) : map (h ∘ g) l = map h (map g l) := Eq.symm (map_map h g l) /-- Composing a `list.map` with another `list.map` is equal to a single `list.map` of composed functions. -/ @[simp] theorem map_comp_map {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : α → β) : map g ∘ map f = map (g ∘ f) := sorry theorem map_filter_eq_foldr {α : Type u} {β : Type v} (f : α → β) (p : α → Prop) [decidable_pred p] (as : List α) : map f (filter p as) = foldr (fun (a : α) (bs : List β) => ite (p a) (f a :: bs) bs) [] as := sorry theorem last_map {α : Type u} {β : Type v} (f : α → β) {l : List α} (hl : l ≠ []) : last (map f l) (mt eq_nil_of_map_eq_nil hl) = f (last l hl) := sorry /-! ### map₂ -/ theorem nil_map₂ {α : Type u} {β : Type v} {γ : Type w} (f : α → β → γ) (l : List β) : map₂ f [] l = [] := list.cases_on l (Eq.refl (map₂ f [] [])) fun (l_hd : β) (l_tl : List β) => Eq.refl (map₂ f [] (l_hd :: l_tl)) theorem map₂_nil {α : Type u} {β : Type v} {γ : Type w} (f : α → β → γ) (l : List α) : map₂ f l [] = [] := list.cases_on l (Eq.refl (map₂ f [] [])) fun (l_hd : α) (l_tl : List α) => Eq.refl (map₂ f (l_hd :: l_tl) []) @[simp] theorem map₂_flip {α : Type u} {β : Type v} {γ : Type w} (f : α → β → γ) (as : List α) (bs : List β) : map₂ (flip f) bs as = map₂ f as bs := sorry /-! ### take, drop -/ @[simp] theorem take_zero {α : Type u} (l : List α) : take 0 l = [] := rfl @[simp] theorem take_nil {α : Type u} (n : ℕ) : take n [] = [] := nat.cases_on n (idRhs (take 0 [] = take 0 []) rfl) fun (n : ℕ) => idRhs (take (n + 1) [] = take (n + 1) []) rfl theorem take_cons {α : Type u} (n : ℕ) (a : α) (l : List α) : take (Nat.succ n) (a :: l) = a :: take n l := rfl @[simp] theorem take_length {α : Type u} (l : List α) : take (length l) l = l := sorry theorem take_all_of_le {α : Type u} {n : ℕ} {l : List α} : length l ≤ n → take n l = l := sorry @[simp] theorem take_left {α : Type u} (l₁ : List α) (l₂ : List α) : take (length l₁) (l₁ ++ l₂) = l₁ := sorry theorem take_left' {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (h : length l₁ = n) : take n (l₁ ++ l₂) = l₁ := eq.mpr (id (Eq._oldrec (Eq.refl (take n (l₁ ++ l₂) = l₁)) (Eq.symm h))) (take_left l₁ l₂) theorem take_take {α : Type u} (n : ℕ) (m : ℕ) (l : List α) : take n (take m l) = take (min n m) l := sorry theorem take_repeat {α : Type u} (a : α) (n : ℕ) (m : ℕ) : take n (repeat a m) = repeat a (min n m) := sorry theorem map_take {α : Type u_1} {β : Type u_2} (f : α → β) (L : List α) (i : ℕ) : map f (take i L) = take i (map f L) := sorry theorem take_append_of_le_length {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} : n ≤ length l₁ → take n (l₁ ++ l₂) = take n l₁ := sorry /-- Taking the first `l₁.length + i` elements in `l₁ ++ l₂` is the same as appending the first `i` elements of `l₂` to `l₁`. -/ theorem take_append {α : Type u} {l₁ : List α} {l₂ : List α} (i : ℕ) : take (length l₁ + i) (l₁ ++ l₂) = l₁ ++ take i l₂ := sorry /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the big list to the small list. -/ theorem nth_le_take {α : Type u} (L : List α) {i : ℕ} {j : ℕ} (hi : i < length L) (hj : i < j) : nth_le L i hi = nth_le (take j L) i (eq.mpr (id (Eq._oldrec (Eq.refl (i < length (take j L))) (length_take j L))) (lt_min hj hi)) := sorry /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the small list to the big list. -/ theorem nth_le_take' {α : Type u} (L : List α) {i : ℕ} {j : ℕ} (hi : i < length (take j L)) : nth_le (take j L) i hi = nth_le L i (lt_of_lt_of_le hi (eq.mpr (id (Eq.trans (Eq.trans (Eq.trans ((fun (ᾰ ᾰ_1 : ℕ) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ℕ) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg LessEq e_2) e_3) (length (take j L)) (min j (length L)) (length_take j L) (length L) (length L) (Eq.refl (length L))) (propext min_le_iff)) ((fun (a a_1 : Prop) (e_1 : a = a_1) (b b_1 : Prop) (e_2 : b = b_1) => congr (congr_arg Or e_1) e_2) (j ≤ length L) (j ≤ length L) (Eq.refl (j ≤ length L)) (length L ≤ length L) True (propext ((fun {α : Type} (a : α) => iff_true_intro (le_refl a)) (length L))))) (propext (or_true (j ≤ length L))))) trivial)) := sorry theorem nth_take {α : Type u} {l : List α} {n : ℕ} {m : ℕ} (h : m < n) : nth (take n l) m = nth l m := sorry @[simp] theorem nth_take_of_succ {α : Type u} {l : List α} {n : ℕ} : nth (take (n + 1) l) n = nth l n := nth_take (nat.lt_succ_self n) theorem take_succ {α : Type u} {l : List α} {n : ℕ} : take (n + 1) l = take n l ++ option.to_list (nth l n) := sorry @[simp] theorem drop_nil {α : Type u} (n : ℕ) : drop n [] = [] := nat.cases_on n (idRhs (drop 0 [] = drop 0 []) rfl) fun (n : ℕ) => idRhs (drop (n + 1) [] = drop (n + 1) []) rfl theorem mem_of_mem_drop {α : Type u_1} {n : ℕ} {l : List α} {x : α} (h : x ∈ drop n l) : x ∈ l := sorry @[simp] theorem drop_one {α : Type u} (l : List α) : drop 1 l = tail l := list.cases_on l (idRhs (drop 1 [] = drop 1 []) rfl) fun (l_hd : α) (l_tl : List α) => idRhs (drop 1 (l_hd :: l_tl) = drop 1 (l_hd :: l_tl)) rfl theorem drop_add {α : Type u} (m : ℕ) (n : ℕ) (l : List α) : drop (m + n) l = drop m (drop n l) := sorry @[simp] theorem drop_left {α : Type u} (l₁ : List α) (l₂ : List α) : drop (length l₁) (l₁ ++ l₂) = l₂ := sorry theorem drop_left' {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} (h : length l₁ = n) : drop n (l₁ ++ l₂) = l₂ := eq.mpr (id (Eq._oldrec (Eq.refl (drop n (l₁ ++ l₂) = l₂)) (Eq.symm h))) (drop_left l₁ l₂) theorem drop_eq_nth_le_cons {α : Type u} {n : ℕ} {l : List α} (h : n < length l) : drop n l = nth_le l n h :: drop (n + 1) l := sorry @[simp] theorem drop_length {α : Type u} (l : List α) : drop (length l) l = [] := sorry theorem drop_append_of_le_length {α : Type u} {l₁ : List α} {l₂ : List α} {n : ℕ} : n ≤ length l₁ → drop n (l₁ ++ l₂) = drop n l₁ ++ l₂ := sorry /-- Dropping the elements up to `l₁.length + i` in `l₁ + l₂` is the same as dropping the elements up to `i` in `l₂`. -/ theorem drop_append {α : Type u} {l₁ : List α} {l₂ : List α} (i : ℕ) : drop (length l₁ + i) (l₁ ++ l₂) = drop i l₂ := sorry /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/ theorem nth_le_drop {α : Type u} (L : List α) {i : ℕ} {j : ℕ} (h : i + j < length L) : nth_le L (i + j) h = nth_le (drop i L) j (eq.mpr (id (Eq.refl (j < length (drop i L)))) (eq.mp (Eq.trans ((fun (ᾰ ᾰ_1 : ℕ) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ℕ) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg Less e_2) e_3) (i + j) (i + j) (Eq.refl (i + j)) (length (take i L ++ drop i L)) (i + length (drop i L)) (Eq.trans (length_append (take i L) (drop i L)) ((fun (ᾰ ᾰ_1 : ℕ) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ℕ) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg Add.add e_2) e_3) (length (take i L)) i (Eq.trans (length_take i L) (min_eq_left (iff.mpr (iff_true_intro (le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h))) True.intro))) (length (drop i L)) (length (drop i L)) (Eq.refl (length (drop i L)))))) (propext (add_lt_add_iff_left i))) (eq.mp (Eq._oldrec (Eq.refl (i + j < length L)) (Eq.symm (take_append_drop i L))) h))) := sorry /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/ theorem nth_le_drop' {α : Type u} (L : List α) {i : ℕ} {j : ℕ} (h : j < length (drop i L)) : nth_le (drop i L) j h = nth_le L (i + j) (nat.add_lt_of_lt_sub_left (length_drop i L ▸ h)) := sorry @[simp] theorem drop_drop {α : Type u} (n : ℕ) (m : ℕ) (l : List α) : drop n (drop m l) = drop (n + m) l := sorry theorem drop_take {α : Type u} (m : ℕ) (n : ℕ) (l : List α) : drop m (take (m + n) l) = take n (drop m l) := sorry theorem map_drop {α : Type u_1} {β : Type u_2} (f : α → β) (L : List α) (i : ℕ) : map f (drop i L) = drop i (map f L) := sorry theorem modify_nth_tail_eq_take_drop {α : Type u} (f : List α → List α) (H : f [] = []) (n : ℕ) (l : List α) : modify_nth_tail f n l = take n l ++ f (drop n l) := sorry theorem modify_nth_eq_take_drop {α : Type u} (f : α → α) (n : ℕ) (l : List α) : modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop (modify_head f) rfl theorem modify_nth_eq_take_cons_drop {α : Type u} (f : α → α) {n : ℕ} {l : List α} (h : n < length l) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n + 1) l := sorry theorem update_nth_eq_take_cons_drop {α : Type u} (a : α) {n : ℕ} {l : List α} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n + 1) l := sorry theorem reverse_take {α : Type u_1} {xs : List α} (n : ℕ) (h : n ≤ length xs) : take n (reverse xs) = reverse (drop (length xs - n) xs) := sorry @[simp] theorem update_nth_eq_nil {α : Type u} (l : List α) (n : ℕ) (a : α) : update_nth l n a = [] ↔ l = [] := sorry @[simp] theorem take'_length {α : Type u} [Inhabited α] (n : ℕ) (l : List α) : length (take' n l) = n := sorry @[simp] theorem take'_nil {α : Type u} [Inhabited α] (n : ℕ) : take' n [] = repeat Inhabited.default n := sorry theorem take'_eq_take {α : Type u} [Inhabited α] {n : ℕ} {l : List α} : n ≤ length l → take' n l = take n l := sorry @[simp] theorem take'_left {α : Type u} [Inhabited α] (l₁ : List α) (l₂ : List α) : take' (length l₁) (l₁ ++ l₂) = l₁ := sorry theorem take'_left' {α : Type u} [Inhabited α] {l₁ : List α} {l₂ : List α} {n : ℕ} (h : length l₁ = n) : take' n (l₁ ++ l₂) = l₁ := eq.mpr (id (Eq._oldrec (Eq.refl (take' n (l₁ ++ l₂) = l₁)) (Eq.symm h))) (take'_left l₁ l₂) /-! ### foldl, foldr -/ theorem foldl_ext {α : Type u} {β : Type v} (f : α → β → α) (g : α → β → α) (a : α) {l : List β} (H : ∀ (a : α) (b : β), b ∈ l → f a b = g a b) : foldl f a l = foldl g a l := sorry theorem foldr_ext {α : Type u} {β : Type v} (f : α → β → β) (g : α → β → β) (b : β) {l : List α} (H : ∀ (a : α), a ∈ l → ∀ (b : β), f a b = g a b) : foldr f b l = foldr g b l := sorry @[simp] theorem foldl_nil {α : Type u} {β : Type v} (f : α → β → α) (a : α) : foldl f a [] = a := rfl @[simp] theorem foldl_cons {α : Type u} {β : Type v} (f : α → β → α) (a : α) (b : β) (l : List β) : foldl f a (b :: l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil {α : Type u} {β : Type v} (f : α → β → β) (b : β) : foldr f b [] = b := rfl @[simp] theorem foldr_cons {α : Type u} {β : Type v} (f : α → β → β) (b : β) (a : α) (l : List α) : foldr f b (a :: l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append {α : Type u} {β : Type v} (f : α → β → α) (a : α) (l₁ : List β) (l₂ : List β) : foldl f a (l₁ ++ l₂) = foldl f (foldl f a l₁) l₂ := sorry @[simp] theorem foldr_append {α : Type u} {β : Type v} (f : α → β → β) (b : β) (l₁ : List α) (l₂ : List α) : foldr f b (l₁ ++ l₂) = foldr f (foldr f b l₂) l₁ := sorry @[simp] theorem foldl_join {α : Type u} {β : Type v} (f : α → β → α) (a : α) (L : List (List β)) : foldl f a (join L) = foldl (foldl f) a L := sorry @[simp] theorem foldr_join {α : Type u} {β : Type v} (f : α → β → β) (b : β) (L : List (List α)) : foldr f b (join L) = foldr (fun (l : List α) (b : β) => foldr f b l) b L := sorry theorem foldl_reverse {α : Type u} {β : Type v} (f : α → β → α) (a : α) (l : List β) : foldl f a (reverse l) = foldr (fun (x : β) (y : α) => f y x) a l := sorry theorem foldr_reverse {α : Type u} {β : Type v} (f : α → β → β) (a : β) (l : List α) : foldr f a (reverse l) = foldl (fun (x : β) (y : α) => f y x) a l := sorry @[simp] theorem foldr_eta {α : Type u} (l : List α) : foldr List.cons [] l = l := sorry @[simp] theorem reverse_foldl {α : Type u} {l : List α} : reverse (foldl (fun (t : List α) (h : α) => h :: t) [] l) = l := sorry @[simp] theorem foldl_map {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : α → γ → α) (a : α) (l : List β) : foldl f a (map g l) = foldl (fun (x : α) (y : β) => f x (g y)) a l := sorry @[simp] theorem foldr_map {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : γ → α → α) (a : α) (l : List β) : foldr f a (map g l) = foldr (f ∘ g) a l := sorry theorem foldl_map' {α : Type u} {β : Type u} (g : α → β) (f : α → α → α) (f' : β → β → β) (a : α) (l : List α) (h : ∀ (x y : α), f' (g x) (g y) = g (f x y)) : foldl f' (g a) (map g l) = g (foldl f a l) := sorry theorem foldr_map' {α : Type u} {β : Type u} (g : α → β) (f : α → α → α) (f' : β → β → β) (a : α) (l : List α) (h : ∀ (x y : α), f' (g x) (g y) = g (f x y)) : foldr f' (g a) (map g l) = g (foldr f a l) := sorry theorem foldl_hom {α : Type u} {β : Type v} {γ : Type w} (l : List γ) (f : α → β) (op : α → γ → α) (op' : β → γ → β) (a : α) (h : ∀ (a : α) (x : γ), f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) := sorry theorem foldr_hom {α : Type u} {β : Type v} {γ : Type w} (l : List γ) (f : α → β) (op : γ → α → α) (op' : γ → β → β) (a : α) (h : ∀ (x : γ) (a : α), f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) := sorry theorem injective_foldl_comp {α : Type u_1} {l : List (α → α)} {f : α → α} (hl : ∀ (f : α → α), f ∈ l → function.injective f) (hf : function.injective f) : function.injective (foldl function.comp f l) := sorry /- scanl -/ theorem length_scanl {α : Type u} {β : Type v} {f : β → α → β} (a : β) (l : List α) : length (scanl f a l) = length l + 1 := sorry @[simp] theorem scanl_nil {α : Type u} {β : Type v} {f : β → α → β} (b : β) : scanl f b [] = [b] := rfl @[simp] theorem scanl_cons {α : Type u} {β : Type v} {f : β → α → β} {b : β} {a : α} {l : List α} : scanl f b (a :: l) = [b] ++ scanl f (f b a) l := sorry @[simp] theorem nth_zero_scanl {α : Type u} {β : Type v} {f : β → α → β} {b : β} {l : List α} : nth (scanl f b l) 0 = some b := sorry @[simp] theorem nth_le_zero_scanl {α : Type u} {β : Type v} {f : β → α → β} {b : β} {l : List α} {h : 0 < length (scanl f b l)} : nth_le (scanl f b l) 0 h = b := sorry theorem nth_succ_scanl {α : Type u} {β : Type v} {f : β → α → β} {b : β} {l : List α} {i : ℕ} : nth (scanl f b l) (i + 1) = option.bind (nth (scanl f b l) i) fun (x : β) => option.map (fun (y : α) => f x y) (nth l i) := sorry theorem nth_le_succ_scanl {α : Type u} {β : Type v} {f : β → α → β} {b : β} {l : List α} {i : ℕ} {h : i + 1 < length (scanl f b l)} : nth_le (scanl f b l) (i + 1) h = f (nth_le (scanl f b l) i (nat.lt_of_succ_lt h)) (nth_le l i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) := sorry /- scanr -/ @[simp] theorem scanr_nil {α : Type u} {β : Type v} (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons {α : Type u} {β : Type v} (f : α → β → β) (b : β) (a : α) (l : List α) : scanr_aux f b (a :: l) = (foldr f b (a :: l), scanr f b l) := sorry @[simp] theorem scanr_cons {α : Type u} {β : Type v} (f : α → β → β) (b : β) (a : α) (l : List α) : scanr f b (a :: l) = foldr f b (a :: l) :: scanr f b l := sorry -- foldl and foldr coincide when f is commutative and associative theorem foldl1_eq_foldr1 {α : Type u} {f : α → α → α} (hassoc : associative f) (a : α) (b : α) (l : List α) : foldl f a (l ++ [b]) = foldr f b (a :: l) := sorry theorem foldl_eq_of_comm_of_assoc {α : Type u} {f : α → α → α} (hcomm : commutative f) (hassoc : associative f) (a : α) (b : α) (l : List α) : foldl f a (b :: l) = f b (foldl f a l) := sorry theorem foldl_eq_foldr {α : Type u} {f : α → α → α} (hcomm : commutative f) (hassoc : associative f) (a : α) (l : List α) : foldl f a l = foldr f a l := sorry theorem foldl_eq_of_comm' {α : Type u} {β : Type v} {f : α → β → α} (hf : ∀ (a : α) (b c : β), f (f a b) c = f (f a c) b) (a : α) (b : β) (l : List β) : foldl f a (b :: l) = f (foldl f a l) b := sorry theorem foldl_eq_foldr' {α : Type u} {β : Type v} {f : α → β → α} (hf : ∀ (a : α) (b c : β), f (f a b) c = f (f a c) b) (a : α) (l : List β) : foldl f a l = foldr (flip f) a l := sorry theorem foldr_eq_of_comm' {α : Type u} {β : Type v} {f : α → β → β} (hf : ∀ (a b : α) (c : β), f a (f b c) = f b (f a c)) (a : β) (b : α) (l : List α) : foldr f a (b :: l) = foldr f (f b a) l := sorry theorem foldl_assoc {α : Type u} {op : α → α → α} [ha : is_associative α op] {l : List α} {a₁ : α} {a₂ : α} : foldl op (op a₁ a₂) l = op a₁ (foldl op a₂ l) := sorry theorem foldl_op_eq_op_foldr_assoc {α : Type u} {op : α → α → α} [ha : is_associative α op] {l : List α} {a₁ : α} {a₂ : α} : op (foldl op a₁ l) a₂ = op a₁ (foldr op a₂ l) := sorry theorem foldl_assoc_comm_cons {α : Type u} {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op] {l : List α} {a₁ : α} {a₂ : α} : foldl op a₂ (a₁ :: l) = op a₁ (foldl op a₂ l) := sorry /-! ### mfoldl, mfoldr, mmap -/ @[simp] theorem mfoldl_nil {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) {b : β} : mfoldl f b [] = pure b := rfl @[simp] theorem mfoldr_nil {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) {b : β} : mfoldr f b [] = pure b := rfl @[simp] theorem mfoldl_cons {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] {f : β → α → m β} {b : β} {a : α} {l : List α} : mfoldl f b (a :: l) = do let b' ← f b a mfoldl f b' l := rfl @[simp] theorem mfoldr_cons {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] {f : α → β → m β} {b : β} {a : α} {l : List α} : mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl theorem mfoldr_eq_foldr {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (b : β) (l : List α) : mfoldr f b l = foldr (fun (a : α) (mb : m β) => mb >>= f a) (pure b) l := sorry theorem mfoldl_eq_foldl {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] [is_lawful_monad m] (f : β → α → m β) (b : β) (l : List α) : mfoldl f b l = foldl (fun (mb : m β) (a : α) => do let b ← mb f b a) (pure b) l := sorry @[simp] theorem mfoldl_append {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] [is_lawful_monad m] {f : β → α → m β} {b : β} {l₁ : List α} {l₂ : List α} : mfoldl f b (l₁ ++ l₂) = do let x ← mfoldl f b l₁ mfoldl f x l₂ := sorry @[simp] theorem mfoldr_append {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] [is_lawful_monad m] {f : α → β → m β} {b : β} {l₁ : List α} {l₂ : List α} : mfoldr f b (l₁ ++ l₂) = do let x ← mfoldr f b l₂ mfoldr f x l₁ := sorry /-! ### prod and sum -/ -- list.sum was already defined in defs.lean, but we couldn't tag it with `to_additive` yet. @[simp] theorem sum_nil {α : Type u} [add_monoid α] : sum [] = 0 := rfl theorem sum_singleton {α : Type u} [add_monoid α] {a : α} : sum [a] = a := zero_add a @[simp] theorem prod_cons {α : Type u} [monoid α] {l : List α} {a : α} : prod (a :: l) = a * prod l := sorry @[simp] theorem sum_append {α : Type u} [add_monoid α] {l₁ : List α} {l₂ : List α} : sum (l₁ ++ l₂) = sum l₁ + sum l₂ := sorry @[simp] theorem sum_join {α : Type u} [add_monoid α] {l : List (List α)} : sum (join l) = sum (map sum l) := sorry theorem prod_ne_zero {R : Type u_1} [domain R] {L : List R} : (∀ (x : R), x ∈ L → x ≠ 0) → prod L ≠ 0 := sorry theorem prod_eq_foldr {α : Type u} [monoid α] {l : List α} : prod l = foldr Mul.mul 1 l := sorry theorem prod_hom_rel {α : Type u_1} {β : Type u_2} {γ : Type u_3} [monoid β] [monoid γ] (l : List α) {r : β → γ → Prop} {f : α → β} {g : α → γ} (h₁ : r 1 1) (h₂ : ∀ {a : α} {b : β} {c : γ}, r b c → r (f a * b) (g a * c)) : r (prod (map f l)) (prod (map g l)) := sorry theorem prod_hom {α : Type u} {β : Type v} [monoid α] [monoid β] (l : List α) (f : α →* β) : prod (map (⇑f) l) = coe_fn f (prod l) := sorry -- `to_additive` chokes on the next few lemmas, so we do them by hand below @[simp] theorem prod_take_mul_prod_drop {α : Type u} [monoid α] (L : List α) (i : ℕ) : prod (take i L) * prod (drop i L) = prod L := sorry @[simp] theorem prod_take_succ {α : Type u} [monoid α] (L : List α) (i : ℕ) (p : i < length L) : prod (take (i + 1) L) = prod (take i L) * nth_le L i p := sorry /-- A list with product not one must have positive length. -/ theorem length_pos_of_prod_ne_one {α : Type u} [monoid α] (L : List α) (h : prod L ≠ 1) : 0 < length L := sorry theorem prod_update_nth {α : Type u} [monoid α] (L : List α) (n : ℕ) (a : α) : prod (update_nth L n a) = prod (take n L) * ite (n < length L) a 1 * prod (drop (n + 1) L) := sorry /-- This is the `list.prod` version of `mul_inv_rev` -/ theorem sum_neg_reverse {α : Type u} [add_group α] (L : List α) : -sum L = sum (reverse (map (fun (x : α) => -x) L)) := sorry /-- A non-commutative variant of `list.prod_reverse` -/ theorem prod_reverse_noncomm {α : Type u} [group α] (L : List α) : prod (reverse L) = (prod (map (fun (x : α) => x⁻¹) L)⁻¹) := sorry /-- This is the `list.prod` version of `mul_inv` -/ theorem sum_neg {α : Type u} [add_comm_group α] (L : List α) : -sum L = sum (map (fun (x : α) => -x) L) := sorry @[simp] theorem sum_take_add_sum_drop {α : Type u} [add_monoid α] (L : List α) (i : ℕ) : sum (take i L) + sum (drop i L) = sum L := sorry @[simp] theorem sum_take_succ {α : Type u} [add_monoid α] (L : List α) (i : ℕ) (p : i < length L) : sum (take (i + 1) L) = sum (take i L) + nth_le L i p := sorry theorem eq_of_sum_take_eq {α : Type u} [add_left_cancel_monoid α] {L : List α} {L' : List α} (h : length L = length L') (h' : ∀ (i : ℕ), i ≤ length L → sum (take i L) = sum (take i L')) : L = L' := sorry theorem monotone_sum_take {α : Type u} [canonically_ordered_add_monoid α] (L : List α) : monotone fun (i : ℕ) => sum (take i L) := sorry theorem one_le_prod_of_one_le {α : Type u} [ordered_comm_monoid α] {l : List α} (hl₁ : ∀ (x : α), x ∈ l → 1 ≤ x) : 1 ≤ prod l := sorry theorem single_le_prod {α : Type u} [ordered_comm_monoid α] {l : List α} (hl₁ : ∀ (x : α), x ∈ l → 1 ≤ x) (x : α) (H : x ∈ l) : x ≤ prod l := sorry theorem all_zero_of_le_zero_le_of_sum_eq_zero {α : Type u} [ordered_add_comm_monoid α] {l : List α} (hl₁ : ∀ (x : α), x ∈ l → 0 ≤ x) (hl₂ : sum l = 0) (x : α) (H : x ∈ l) : x = 0 := le_antisymm (hl₂ ▸ single_le_sum hl₁ x hx) (hl₁ x hx) theorem sum_eq_zero_iff {α : Type u} [canonically_ordered_add_monoid α] (l : List α) : sum l = 0 ↔ ∀ (x : α), x ∈ l → x = 0 := sorry /-- A list with sum not zero must have positive length. -/ theorem length_pos_of_sum_ne_zero {α : Type u} [add_monoid α] (L : List α) (h : sum L ≠ 0) : 0 < length L := sorry /-- If all elements in a list are bounded below by `1`, then the length of the list is bounded by the sum of the elements. -/ theorem length_le_sum_of_one_le (L : List ℕ) (h : ∀ (i : ℕ), i ∈ L → 1 ≤ i) : length L ≤ sum L := sorry -- Now we tie those lemmas back to their multiplicative versions. /-- A list with positive sum must have positive length. -/ -- This is an easy consequence of `length_pos_of_sum_ne_zero`, but often useful in applications. theorem length_pos_of_sum_pos {α : Type u} [ordered_cancel_add_comm_monoid α] (L : List α) (h : 0 < sum L) : 0 < length L := length_pos_of_sum_ne_zero L (ne_of_gt h) @[simp] theorem prod_erase {α : Type u} [DecidableEq α] [comm_monoid α] {a : α} {l : List α} : a ∈ l → a * prod (list.erase l a) = prod l := sorry theorem dvd_prod {α : Type u} [comm_monoid α] {a : α} {l : List α} (ha : a ∈ l) : a ∣ prod l := sorry @[simp] theorem sum_const_nat (m : ℕ) (n : ℕ) : sum (repeat m n) = m * n := sorry theorem dvd_sum {α : Type u} [comm_semiring α] {a : α} {l : List α} (h : ∀ (x : α), x ∈ l → a ∣ x) : a ∣ sum l := sorry @[simp] theorem length_join {α : Type u} (L : List (List α)) : length (join L) = sum (map length L) := sorry @[simp] theorem length_bind {α : Type u} {β : Type v} (l : List α) (f : α → List β) : length (list.bind l f) = sum (map (length ∘ f) l) := sorry theorem exists_lt_of_sum_lt {α : Type u} {β : Type v} [linear_ordered_cancel_add_comm_monoid β] {l : List α} (f : α → β) (g : α → β) (h : sum (map f l) < sum (map g l)) : ∃ (x : α), ∃ (H : x ∈ l), f x < g x := sorry theorem exists_le_of_sum_le {α : Type u} {β : Type v} [linear_ordered_cancel_add_comm_monoid β] {l : List α} (hl : l ≠ []) (f : α → β) (g : α → β) (h : sum (map f l) ≤ sum (map g l)) : ∃ (x : α), ∃ (H : x ∈ l), f x ≤ g x := sorry -- Several lemmas about sum/head/tail for `list ℕ`. -- These are hard to generalize well, as they rely on the fact that `default ℕ = 0`. -- We'd like to state this as `L.head * L.tail.prod = L.prod`, -- but because `L.head` relies on an inhabited instances and -- returns a garbage value for the empty list, this is not possible. -- Instead we write the statement in terms of `(L.nth 0).get_or_else 1`, -- and below, restate the lemma just for `ℕ`. theorem head_mul_tail_prod' {α : Type u} [monoid α] (L : List α) : option.get_or_else (nth L 0) 1 * prod (tail L) = prod L := sorry theorem head_add_tail_sum (L : List ℕ) : head L + sum (tail L) = sum L := sorry theorem head_le_sum (L : List ℕ) : head L ≤ sum L := nat.le.intro (head_add_tail_sum L) theorem tail_sum (L : List ℕ) : sum (tail L) = sum L - head L := sorry @[simp] theorem alternating_prod_nil {G : Type u_1} [comm_group G] : alternating_prod [] = 1 := rfl @[simp] theorem alternating_sum_singleton {G : Type u_1} [add_comm_group G] (g : G) : alternating_sum [g] = g := rfl @[simp] theorem alternating_sum_cons_cons' {G : Type u_1} [add_comm_group G] (g : G) (h : G) (l : List G) : alternating_sum (g :: h :: l) = g + -h + alternating_sum l := rfl theorem alternating_sum_cons_cons {G : Type u_1} [add_comm_group G] (g : G) (h : G) (l : List G) : alternating_sum (g :: h :: l) = g - h + alternating_sum l := sorry /-! ### join -/ theorem join_eq_nil {α : Type u} {L : List (List α)} : join L = [] ↔ ∀ (l : List α), l ∈ L → l = [] := sorry @[simp] theorem join_append {α : Type u} (L₁ : List (List α)) (L₂ : List (List α)) : join (L₁ ++ L₂) = join L₁ ++ join L₂ := sorry theorem join_join {α : Type u} (l : List (List (List α))) : join (join l) = join (map join l) := sorry /-- In a join, taking the first elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join of the first `i` sublists. -/ theorem take_sum_join {α : Type u} (L : List (List α)) (i : ℕ) : take (sum (take i (map length L))) (join L) = join (take i L) := sorry /-- In a join, dropping all the elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/ theorem drop_sum_join {α : Type u} (L : List (List α)) (i : ℕ) : drop (sum (take i (map length L))) (join L) = join (drop i L) := sorry /-- Taking only the first `i+1` elements in a list, and then dropping the first `i` ones, one is left with a list of length `1` made of the `i`-th element of the original list. -/ theorem drop_take_succ_eq_cons_nth_le {α : Type u} (L : List α) {i : ℕ} (hi : i < length L) : drop i (take (i + 1) L) = [nth_le L i hi] := sorry /-- In a join of sublists, taking the slice between the indices `A` and `B - 1` gives back the original sublist of index `i` if `A` is the sum of the lenghts of sublists of index `< i`, and `B` is the sum of the lengths of sublists of index `≤ i`. -/ theorem drop_take_succ_join_eq_nth_le {α : Type u} (L : List (List α)) {i : ℕ} (hi : i < length L) : drop (sum (take i (map length L))) (take (sum (take (i + 1) (map length L))) (join L)) = nth_le L i hi := sorry /-- Auxiliary lemma to control elements in a join. -/ theorem sum_take_map_length_lt1 {α : Type u} (L : List (List α)) {i : ℕ} {j : ℕ} (hi : i < length L) (hj : j < length (nth_le L i hi)) : sum (take i (map length L)) + j < sum (take (i + 1) (map length L)) := sorry /-- Auxiliary lemma to control elements in a join. -/ theorem sum_take_map_length_lt2 {α : Type u} (L : List (List α)) {i : ℕ} {j : ℕ} (hi : i < length L) (hj : j < length (nth_le L i hi)) : sum (take i (map length L)) + j < length (join L) := sorry /-- The `n`-th element in a join of sublists is the `j`-th element of the `i`th sublist, where `n` can be obtained in terms of `i` and `j` by adding the lengths of all the sublists of index `< i`, and adding `j`. -/ theorem nth_le_join {α : Type u} (L : List (List α)) {i : ℕ} {j : ℕ} (hi : i < length L) (hj : j < length (nth_le L i hi)) : nth_le (join L) (sum (take i (map length L)) + j) (sum_take_map_length_lt2 L hi hj) = nth_le (nth_le L i hi) j hj := sorry /-- Two lists of sublists are equal iff their joins coincide, as well as the lengths of the sublists. -/ theorem eq_iff_join_eq {α : Type u} (L : List (List α)) (L' : List (List α)) : L = L' ↔ join L = join L' ∧ map length L = map length L' := sorry /-! ### lexicographic ordering -/ /-- Given a strict order `<` on `α`, the lexicographic strict order on `list α`, for which `[a0, ..., an] < [b0, ..., b_k]` if `a0 < b0` or `a0 = b0` and `[a1, ..., an] < [b1, ..., bk]`. The definition is given for any relation `r`, not only strict orders. -/ inductive lex {α : Type u} (r : α → α → Prop) : List α → List α → Prop where | nil : ∀ {a : α} {l : List α}, lex r [] (a :: l) | cons : ∀ {a : α} {l₁ l₂ : List α}, lex r l₁ l₂ → lex r (a :: l₁) (a :: l₂) | rel : ∀ {a₁ : α} {l₁ : List α} {a₂ : α} {l₂ : List α}, r a₁ a₂ → lex r (a₁ :: l₁) (a₂ :: l₂) namespace lex theorem cons_iff {α : Type u} {r : α → α → Prop} [is_irrefl α r] {a : α} {l₁ : List α} {l₂ : List α} : lex r (a :: l₁) (a :: l₂) ↔ lex r l₁ l₂ := sorry @[simp] theorem not_nil_right {α : Type u} (r : α → α → Prop) (l : List α) : ¬lex r l [] := sorry protected instance is_order_connected {α : Type u} (r : α → α → Prop) [is_order_connected α r] [is_trichotomous α r] : is_order_connected (List α) (lex r) := is_order_connected.mk fun (l₁ : List α) => sorry protected instance is_trichotomous {α : Type u} (r : α → α → Prop) [is_trichotomous α r] : is_trichotomous (List α) (lex r) := is_trichotomous.mk fun (l₁ : List α) => sorry protected instance is_asymm {α : Type u} (r : α → α → Prop) [is_asymm α r] : is_asymm (List α) (lex r) := is_asymm.mk fun (l₁ : List α) => sorry protected instance is_strict_total_order {α : Type u} (r : α → α → Prop) [is_strict_total_order' α r] : is_strict_total_order' (List α) (lex r) := is_strict_total_order'.mk protected instance decidable_rel {α : Type u} [DecidableEq α] (r : α → α → Prop) [DecidableRel r] : DecidableRel (lex r) := sorry theorem append_right {α : Type u} (r : α → α → Prop) {s₁ : List α} {s₂ : List α} (t : List α) : lex r s₁ s₂ → lex r s₁ (s₂ ++ t) := sorry theorem append_left {α : Type u} (R : α → α → Prop) {t₁ : List α} {t₂ : List α} (h : lex R t₁ t₂) (s : List α) : lex R (s ++ t₁) (s ++ t₂) := sorry theorem imp {α : Type u} {r : α → α → Prop} {s : α → α → Prop} (H : ∀ (a b : α), r a b → s a b) (l₁ : List α) (l₂ : List α) : lex r l₁ l₂ → lex s l₁ l₂ := sorry theorem to_ne {α : Type u} {l₁ : List α} {l₂ : List α} : lex ne l₁ l₂ → l₁ ≠ l₂ := sorry theorem ne_iff {α : Type u} {l₁ : List α} {l₂ : List α} (H : length l₁ ≤ length l₂) : lex ne l₁ l₂ ↔ l₁ ≠ l₂ := sorry end lex --Note: this overrides an instance in core lean protected instance has_lt' {α : Type u} [HasLess α] : HasLess (List α) := { Less := lex Less } theorem nil_lt_cons {α : Type u} [HasLess α] (a : α) (l : List α) : [] < a :: l := lex.nil protected instance linear_order {α : Type u} [linear_order α] : linear_order (List α) := linear_order_of_STO' (lex Less) --Note: this overrides an instance in core lean protected instance has_le' {α : Type u} [linear_order α] : HasLessEq (List α) := preorder.to_has_le (List α) /-! ### all & any -/ @[simp] theorem all_nil {α : Type u} (p : α → Bool) : all [] p = tt := rfl @[simp] theorem all_cons {α : Type u} (p : α → Bool) (a : α) (l : List α) : all (a :: l) p = p a && all l p := rfl theorem all_iff_forall {α : Type u} {p : α → Bool} {l : List α} : ↥(all l p) ↔ ∀ (a : α), a ∈ l → ↥(p a) := sorry theorem all_iff_forall_prop {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} : ↥(all l fun (a : α) => to_bool (p a)) ↔ ∀ (a : α), a ∈ l → p a := sorry @[simp] theorem any_nil {α : Type u} (p : α → Bool) : any [] p = false := rfl @[simp] theorem any_cons {α : Type u} (p : α → Bool) (a : α) (l : List α) : any (a :: l) p = p a || any l p := rfl theorem any_iff_exists {α : Type u} {p : α → Bool} {l : List α} : ↥(any l p) ↔ ∃ (a : α), ∃ (H : a ∈ l), ↥(p a) := sorry theorem any_iff_exists_prop {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} : ↥(any l fun (a : α) => to_bool (p a)) ↔ ∃ (a : α), ∃ (H : a ∈ l), p a := sorry theorem any_of_mem {α : Type u} {p : α → Bool} {a : α} {l : List α} (h₁ : a ∈ l) (h₂ : ↥(p a)) : ↥(any l p) := iff.mpr any_iff_exists (Exists.intro a (Exists.intro h₁ h₂)) protected instance decidable_forall_mem {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : Decidable (∀ (x : α), x ∈ l → p x) := decidable_of_iff ↥(all l fun (a : α) => to_bool (p a)) sorry protected instance decidable_exists_mem {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : Decidable (∃ (x : α), ∃ (H : x ∈ l), p x) := decidable_of_iff ↥(any l fun (a : α) => to_bool (p a)) sorry /-! ### map for partial functions -/ /-- Partial map. If `f : Π a, p a → β` is a partial function defined on `a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {α : Type u} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) (l : List α) : (∀ (a : α), a ∈ l → p a) → List β := sorry /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach {α : Type u} (l : List α) : List (Subtype fun (x : α) => x ∈ l) := pmap Subtype.mk l sorry theorem sizeof_lt_sizeof_of_mem {α : Type u} [SizeOf α] {x : α} {l : List α} (hx : x ∈ l) : sizeof x < sizeof l := sorry theorem pmap_eq_map {α : Type u} {β : Type v} (p : α → Prop) (f : α → β) (l : List α) (H : ∀ (a : α), a ∈ l → p a) : pmap (fun (a : α) (_x : p a) => f a) l H = map f l := sorry theorem pmap_congr {α : Type u} {β : Type v} {p : α → Prop} {q : α → Prop} {f : (a : α) → p a → β} {g : (a : α) → q a → β} (l : List α) {H₁ : ∀ (a : α), a ∈ l → p a} {H₂ : ∀ (a : α), a ∈ l → q a} (h : ∀ (a : α) (h₁ : p a) (h₂ : q a), f a h₁ = g a h₂) : pmap f l H₁ = pmap g l H₂ := sorry theorem map_pmap {α : Type u} {β : Type v} {γ : Type w} {p : α → Prop} (g : β → γ) (f : (a : α) → p a → β) (l : List α) (H : ∀ (a : α), a ∈ l → p a) : map g (pmap f l H) = pmap (fun (a : α) (h : p a) => g (f a h)) l H := sorry theorem pmap_map {α : Type u} {β : Type v} {γ : Type w} {p : β → Prop} (g : (b : β) → p b → γ) (f : α → β) (l : List α) (H : ∀ (a : β), a ∈ map f l → p a) : pmap g (map f l) H = pmap (fun (a : α) (h : p (f a)) => g (f a) h) l fun (a : α) (h : a ∈ l) => H (f a) (mem_map_of_mem f h) := sorry theorem pmap_eq_map_attach {α : Type u} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) (l : List α) (H : ∀ (a : α), a ∈ l → p a) : pmap f l H = map (fun (x : Subtype fun (x : α) => x ∈ l) => f (subtype.val x) (H (subtype.val x) (subtype.property x))) (attach l) := sorry theorem attach_map_val {α : Type u} (l : List α) : map subtype.val (attach l) = l := sorry @[simp] theorem mem_attach {α : Type u} (l : List α) (x : Subtype fun (x : α) => x ∈ l) : x ∈ attach l := sorry @[simp] theorem mem_pmap {α : Type u} {β : Type v} {p : α → Prop} {f : (a : α) → p a → β} {l : List α} {H : ∀ (a : α), a ∈ l → p a} {b : β} : b ∈ pmap f l H ↔ ∃ (a : α), ∃ (h : a ∈ l), f a (H a h) = b := sorry @[simp] theorem length_pmap {α : Type u} {β : Type v} {p : α → Prop} {f : (a : α) → p a → β} {l : List α} {H : ∀ (a : α), a ∈ l → p a} : length (pmap f l H) = length l := sorry @[simp] theorem length_attach {α : Type u} (L : List α) : length (attach L) = length L := length_pmap @[simp] theorem pmap_eq_nil {α : Type u} {β : Type v} {p : α → Prop} {f : (a : α) → p a → β} {l : List α} {H : ∀ (a : α), a ∈ l → p a} : pmap f l H = [] ↔ l = [] := eq.mpr (id (Eq._oldrec (Eq.refl (pmap f l H = [] ↔ l = [])) (Eq.symm (propext length_eq_zero)))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (pmap f l H) = 0 ↔ l = [])) length_pmap)) (eq.mpr (id (Eq._oldrec (Eq.refl (length l = 0 ↔ l = [])) (propext length_eq_zero))) (iff.refl (l = [])))) @[simp] theorem attach_eq_nil {α : Type u} (l : List α) : attach l = [] ↔ l = [] := pmap_eq_nil theorem last_pmap {α : Type u_1} {β : Type u_2} (p : α → Prop) (f : (a : α) → p a → β) (l : List α) (hl₁ : ∀ (a : α), a ∈ l → p a) (hl₂ : l ≠ []) : last (pmap f l hl₁) (mt (iff.mp pmap_eq_nil) hl₂) = f (last l hl₂) (hl₁ (last l hl₂) (last_mem hl₂)) := sorry theorem nth_pmap {α : Type u} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) {l : List α} (h : ∀ (a : α), a ∈ l → p a) (n : ℕ) : nth (pmap f l h) n = option.pmap f (nth l n) fun (x : α) (H : x ∈ nth l n) => h x (nth_mem H) := sorry theorem nth_le_pmap {α : Type u} {β : Type v} {p : α → Prop} (f : (a : α) → p a → β) {l : List α} (h : ∀ (a : α), a ∈ l → p a) {n : ℕ} (hn : n < length (pmap f l h)) : nth_le (pmap f l h) n hn = f (nth_le l n (length_pmap ▸ hn)) (h (nth_le l n (length_pmap ▸ hn)) (nth_le_mem l n (length_pmap ▸ hn))) := sorry /-! ### find -/ @[simp] theorem find_nil {α : Type u} (p : α → Prop) [decidable_pred p] : find p [] = none := rfl @[simp] theorem find_cons_of_pos {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} (l : List α) (h : p a) : find p (a :: l) = some a := if_pos h @[simp] theorem find_cons_of_neg {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} (l : List α) (h : ¬p a) : find p (a :: l) = find p l := if_neg h @[simp] theorem find_eq_none {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} : find p l = none ↔ ∀ (x : α), x ∈ l → ¬p x := sorry theorem find_some {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} {a : α} (H : find p l = some a) : p a := sorry @[simp] theorem find_mem {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} {a : α} (H : find p l = some a) : a ∈ l := sorry /-! ### lookmap -/ @[simp] theorem lookmap_nil {α : Type u} (f : α → Option α) : lookmap f [] = [] := rfl @[simp] theorem lookmap_cons_none {α : Type u} (f : α → Option α) {a : α} (l : List α) (h : f a = none) : lookmap f (a :: l) = a :: lookmap f l := sorry @[simp] theorem lookmap_cons_some {α : Type u} (f : α → Option α) {a : α} {b : α} (l : List α) (h : f a = some b) : lookmap f (a :: l) = b :: l := sorry theorem lookmap_some {α : Type u} (l : List α) : lookmap some l = l := list.cases_on l (idRhs (lookmap some [] = lookmap some []) rfl) fun (l_hd : α) (l_tl : List α) => idRhs (lookmap some (l_hd :: l_tl) = lookmap some (l_hd :: l_tl)) rfl theorem lookmap_none {α : Type u} (l : List α) : lookmap (fun (_x : α) => none) l = l := sorry theorem lookmap_congr {α : Type u} {f : α → Option α} {g : α → Option α} {l : List α} : (∀ (a : α), a ∈ l → f a = g a) → lookmap f l = lookmap g l := sorry theorem lookmap_of_forall_not {α : Type u} (f : α → Option α) {l : List α} (H : ∀ (a : α), a ∈ l → f a = none) : lookmap f l = l := Eq.trans (lookmap_congr H) (lookmap_none l) theorem lookmap_map_eq {α : Type u} {β : Type v} (f : α → Option α) (g : α → β) (h : ∀ (a b : α), b ∈ f a → g a = g b) (l : List α) : map g (lookmap f l) = map g l := sorry theorem lookmap_id' {α : Type u} (f : α → Option α) (h : ∀ (a b : α), b ∈ f a → a = b) (l : List α) : lookmap f l = l := eq.mpr (id (Eq._oldrec (Eq.refl (lookmap f l = l)) (Eq.symm (map_id (lookmap f l))))) (eq.mpr (id (Eq._oldrec (Eq.refl (map id (lookmap f l) = l)) (lookmap_map_eq f id h l))) (eq.mpr (id (Eq._oldrec (Eq.refl (map id l = l)) (map_id l))) (Eq.refl l))) theorem length_lookmap {α : Type u} (f : α → Option α) (l : List α) : length (lookmap f l) = length l := sorry /-! ### filter_map -/ @[simp] theorem filter_map_nil {α : Type u} {β : Type v} (f : α → Option β) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {α : Type u} {β : Type v} {f : α → Option β} (a : α) (l : List α) (h : f a = none) : filter_map f (a :: l) = filter_map f l := sorry @[simp] theorem filter_map_cons_some {α : Type u} {β : Type v} (f : α → Option β) (a : α) (l : List α) {b : β} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := sorry theorem filter_map_append {α : Type u_1} {β : Type u_2} (l : List α) (l' : List α) (f : α → Option β) : filter_map f (l ++ l') = filter_map f l ++ filter_map f l' := sorry theorem filter_map_eq_map {α : Type u} {β : Type v} (f : α → β) : filter_map (some ∘ f) = map f := sorry theorem filter_map_eq_filter {α : Type u} (p : α → Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := sorry theorem filter_map_filter_map {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β) (g : β → Option γ) (l : List α) : filter_map g (filter_map f l) = filter_map (fun (x : α) => option.bind (f x) g) l := sorry theorem map_filter_map {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β) (g : β → γ) (l : List α) : map g (filter_map f l) = filter_map (fun (x : α) => option.map g (f x)) l := sorry theorem filter_map_map {α : Type u} {β : Type v} {γ : Type w} (f : α → β) (g : β → Option γ) (l : List α) : filter_map g (map f l) = filter_map (g ∘ f) l := sorry theorem filter_filter_map {α : Type u} {β : Type v} (f : α → Option β) (p : β → Prop) [decidable_pred p] (l : List α) : filter p (filter_map f l) = filter_map (fun (x : α) => option.filter p (f x)) l := sorry theorem filter_map_filter {α : Type u} {β : Type v} (p : α → Prop) [decidable_pred p] (f : α → Option β) (l : List α) : filter_map f (filter p l) = filter_map (fun (x : α) => ite (p x) (f x) none) l := sorry @[simp] theorem filter_map_some {α : Type u} (l : List α) : filter_map some l = l := eq.mpr (id (Eq._oldrec (Eq.refl (filter_map some l = l)) (filter_map_eq_map fun (x : α) => x))) (map_id l) @[simp] theorem mem_filter_map {α : Type u} {β : Type v} (f : α → Option β) (l : List α) {b : β} : b ∈ filter_map f l ↔ ∃ (a : α), a ∈ l ∧ f a = some b := sorry theorem map_filter_map_of_inv {α : Type u} {β : Type v} (f : α → Option β) (g : β → α) (H : ∀ (x : α), option.map g (f x) = some x) (l : List α) : map g (filter_map f l) = l := sorry theorem sublist.filter_map {α : Type u} {β : Type v} (f : α → Option β) {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ := sorry theorem sublist.map {α : Type u} {β : Type v} (f : α → β) {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) : map f l₁ <+ map f l₂ := filter_map_eq_map f ▸ sublist.filter_map (some ∘ f) s /-! ### reduce_option -/ @[simp] theorem reduce_option_cons_of_some {α : Type u} (x : α) (l : List (Option α)) : reduce_option (some x :: l) = x :: reduce_option l := sorry @[simp] theorem reduce_option_cons_of_none {α : Type u} (l : List (Option α)) : reduce_option (none :: l) = reduce_option l := sorry @[simp] theorem reduce_option_nil {α : Type u} : reduce_option [] = [] := rfl @[simp] theorem reduce_option_map {α : Type u} {β : Type v} {l : List (Option α)} {f : α → β} : reduce_option (map (option.map f) l) = map f (reduce_option l) := sorry theorem reduce_option_append {α : Type u} (l : List (Option α)) (l' : List (Option α)) : reduce_option (l ++ l') = reduce_option l ++ reduce_option l' := filter_map_append l l' id theorem reduce_option_length_le {α : Type u} (l : List (Option α)) : length (reduce_option l) ≤ length l := sorry theorem reduce_option_length_eq_iff {α : Type u} {l : List (Option α)} : length (reduce_option l) = length l ↔ ∀ (x : Option α), x ∈ l → ↥(option.is_some x) := sorry theorem reduce_option_length_lt_iff {α : Type u} {l : List (Option α)} : length (reduce_option l) < length l ↔ none ∈ l := sorry theorem reduce_option_singleton {α : Type u} (x : Option α) : reduce_option [x] = option.to_list x := option.cases_on x (Eq.refl (reduce_option [none])) fun (x : α) => Eq.refl (reduce_option [some x]) theorem reduce_option_concat {α : Type u} (l : List (Option α)) (x : Option α) : reduce_option (concat l x) = reduce_option l ++ option.to_list x := sorry theorem reduce_option_concat_of_some {α : Type u} (l : List (Option α)) (x : α) : reduce_option (concat l (some x)) = concat (reduce_option l) x := sorry theorem reduce_option_mem_iff {α : Type u} {l : List (Option α)} {x : α} : x ∈ reduce_option l ↔ some x ∈ l := sorry theorem reduce_option_nth_iff {α : Type u} {l : List (Option α)} {x : α} : (∃ (i : ℕ), nth l i = some (some x)) ↔ ∃ (i : ℕ), nth (reduce_option l) i = some x := sorry /-! ### filter -/ theorem filter_eq_foldr {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : filter p l = foldr (fun (a : α) (out : List α) => ite (p a) (a :: out) out) [] l := sorry theorem filter_congr {α : Type u} {p : α → Prop} {q : α → Prop} [decidable_pred p] [decidable_pred q] {l : List α} : (∀ (x : α), x ∈ l → (p x ↔ q x)) → filter p l = filter q l := sorry @[simp] theorem filter_subset {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : filter p l ⊆ l := sublist.subset (filter_sublist l) theorem of_mem_filter {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} : a ∈ filter p l → p a := sorry theorem mem_of_mem_filter {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} : a ∈ l → p a → a ∈ filter p l := sorry @[simp] theorem mem_filter {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} : a ∈ filter p l ↔ a ∈ l ∧ p a := sorry theorem filter_eq_self {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} : filter p l = l ↔ ∀ (a : α), a ∈ l → p a := sorry theorem filter_eq_nil {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} : filter p l = [] ↔ ∀ (a : α), a ∈ l → ¬p a := sorry theorem filter_sublist_filter {α : Type u} (p : α → Prop) [decidable_pred p] {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ := filter_map_eq_filter p ▸ sublist.filter_map (option.guard p) s theorem filter_of_map {α : Type u} {β : Type v} (p : α → Prop) [decidable_pred p] (f : β → α) (l : List β) : filter p (map f l) = map f (filter (p ∘ f) l) := sorry @[simp] theorem filter_filter {α : Type u} (p : α → Prop) [decidable_pred p] (q : α → Prop) [decidable_pred q] (l : List α) : filter p (filter q l) = filter (fun (a : α) => p a ∧ q a) l := sorry @[simp] theorem filter_true {α : Type u} {h : decidable_pred fun (a : α) => True} (l : List α) : filter (fun (a : α) => True) l = l := sorry @[simp] theorem filter_false {α : Type u} {h : decidable_pred fun (a : α) => False} (l : List α) : filter (fun (a : α) => False) l = [] := sorry @[simp] theorem span_eq_take_drop {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : span p l = (take_while p l, drop_while p l) := sorry @[simp] theorem take_while_append_drop {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : take_while p l ++ drop_while p l = l := sorry @[simp] theorem countp_nil {α : Type u} (p : α → Prop) [decidable_pred p] : countp p [] = 0 := rfl @[simp] theorem countp_cons_of_pos {α : Type u} (p : α → Prop) [decidable_pred p] {a : α} (l : List α) (pa : p a) : countp p (a :: l) = countp p l + 1 := if_pos pa @[simp] theorem countp_cons_of_neg {α : Type u} (p : α → Prop) [decidable_pred p] {a : α} (l : List α) (pa : ¬p a) : countp p (a :: l) = countp p l := if_neg pa theorem countp_eq_length_filter {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : countp p l = length (filter p l) := sorry @[simp] theorem countp_append {α : Type u} (p : α → Prop) [decidable_pred p] (l₁ : List α) (l₂ : List α) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ := sorry theorem countp_pos {α : Type u} (p : α → Prop) [decidable_pred p] {l : List α} : 0 < countp p l ↔ ∃ (a : α), ∃ (H : a ∈ l), p a := sorry theorem countp_le_of_sublist {α : Type u} (p : α → Prop) [decidable_pred p] {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ := sorry @[simp] theorem countp_filter {α : Type u} (p : α → Prop) [decidable_pred p] {q : α → Prop} [decidable_pred q] (l : List α) : countp p (filter q l) = countp (fun (a : α) => p a ∧ q a) l := sorry /-! ### count -/ @[simp] theorem count_nil {α : Type u} [DecidableEq α] (a : α) : count a [] = 0 := rfl theorem count_cons {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : count a (b :: l) = ite (a = b) (Nat.succ (count a l)) (count a l) := rfl theorem count_cons' {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : count a (b :: l) = count a l + ite (a = b) 1 0 := sorry @[simp] theorem count_cons_self {α : Type u} [DecidableEq α] (a : α) (l : List α) : count a (a :: l) = Nat.succ (count a l) := if_pos rfl @[simp] theorem count_cons_of_ne {α : Type u} [DecidableEq α] {a : α} {b : α} (h : a ≠ b) (l : List α) : count a (b :: l) = count a l := if_neg h theorem count_tail {α : Type u} [DecidableEq α] (l : List α) (a : α) (h : 0 < length l) : count a (tail l) = count a l - ite (a = nth_le l 0 h) 1 0 := sorry theorem count_le_of_sublist {α : Type u} [DecidableEq α] (a : α) {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → count a l₁ ≤ count a l₂ := countp_le_of_sublist (Eq a) theorem count_le_count_cons {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : count a l ≤ count a (b :: l) := count_le_of_sublist a (sublist_cons b l) theorem count_singleton {α : Type u} [DecidableEq α] (a : α) : count a [a] = 1 := if_pos rfl @[simp] theorem count_append {α : Type u} [DecidableEq α] (a : α) (l₁ : List α) (l₂ : List α) : count a (l₁ ++ l₂) = count a l₁ + count a l₂ := countp_append (Eq a) theorem count_concat {α : Type u} [DecidableEq α] (a : α) (l : List α) : count a (concat l a) = Nat.succ (count a l) := sorry theorem count_pos {α : Type u} [DecidableEq α] {a : α} {l : List α} : 0 < count a l ↔ a ∈ l := sorry @[simp] theorem count_eq_zero_of_not_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : ¬a ∈ l) : count a l = 0 := by_contradiction fun (h' : ¬count a l = 0) => h (iff.mp count_pos (nat.pos_of_ne_zero h')) theorem not_mem_of_count_eq_zero {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : count a l = 0) : ¬a ∈ l := fun (h' : a ∈ l) => ne_of_gt (iff.mpr count_pos h') h @[simp] theorem count_repeat {α : Type u} [DecidableEq α] (a : α) (n : ℕ) : count a (repeat a n) = n := sorry theorem le_count_iff_repeat_sublist {α : Type u} [DecidableEq α] {a : α} {l : List α} {n : ℕ} : n ≤ count a l ↔ repeat a n <+ l := sorry theorem repeat_count_eq_of_count_eq_length {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : count a l = length l) : repeat a (count a l) = l := eq_of_sublist_of_length_eq (iff.mp le_count_iff_repeat_sublist (le_refl (count a l))) (Eq.trans (length_repeat a (count a l)) h) @[simp] theorem count_filter {α : Type u} [DecidableEq α] {p : α → Prop} [decidable_pred p] {a : α} {l : List α} (h : p a) : count a (filter p l) = count a l := sorry /-! ### prefix, suffix, infix -/ @[simp] theorem prefix_append {α : Type u} (l₁ : List α) (l₂ : List α) : l₁ <+: l₁ ++ l₂ := Exists.intro l₂ rfl @[simp] theorem suffix_append {α : Type u} (l₁ : List α) (l₂ : List α) : l₂ <:+ l₁ ++ l₂ := Exists.intro l₁ rfl theorem infix_append {α : Type u} (l₁ : List α) (l₂ : List α) (l₃ : List α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := Exists.intro l₁ (Exists.intro l₃ rfl) @[simp] theorem infix_append' {α : Type u} (l₁ : List α) (l₂ : List α) (l₃ : List α) : l₂ <:+: l₁ ++ (l₂ ++ l₃) := eq.mpr (id (Eq._oldrec (Eq.refl (l₂ <:+: l₁ ++ (l₂ ++ l₃))) (Eq.symm (append_assoc l₁ l₂ l₃)))) (infix_append l₁ l₂ l₃) theorem nil_prefix {α : Type u} (l : List α) : [] <+: l := Exists.intro l rfl theorem nil_suffix {α : Type u} (l : List α) : [] <:+ l := Exists.intro l (append_nil l) theorem prefix_refl {α : Type u} (l : List α) : l <+: l := Exists.intro [] (append_nil l) theorem suffix_refl {α : Type u} (l : List α) : l <:+ l := Exists.intro [] rfl @[simp] theorem suffix_cons {α : Type u} (a : α) (l : List α) : l <:+ a :: l := suffix_append [a] theorem prefix_concat {α : Type u} (a : α) (l : List α) : l <+: concat l a := sorry theorem infix_of_prefix {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+: l₂ → l₁ <:+: l₂ := sorry theorem infix_of_suffix {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <:+ l₂ → l₁ <:+: l₂ := sorry theorem infix_refl {α : Type u} (l : List α) : l <:+: l := infix_of_prefix (prefix_refl l) theorem nil_infix {α : Type u} (l : List α) : [] <:+: l := infix_of_prefix (nil_prefix l) theorem infix_cons {α : Type u} {L₁ : List α} {L₂ : List α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ := sorry theorem is_prefix.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} : l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃ := sorry theorem is_suffix.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} : l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃ := sorry theorem is_infix.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} : l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃ := sorry theorem sublist_of_infix {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <:+: l₂ → l₁ <+ l₂ := sorry theorem sublist_of_prefix {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+: l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_prefix theorem sublist_of_suffix {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <:+ l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_suffix theorem reverse_suffix {α : Type u} {l₁ : List α} {l₂ : List α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ := sorry theorem reverse_prefix {α : Type u} {l₁ : List α} {l₂ : List α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ := sorry theorem length_le_of_infix {α : Type u} {l₁ : List α} {l₂ : List α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ := length_le_of_sublist (sublist_of_infix s) theorem eq_nil_of_infix_nil {α : Type u} {l : List α} (s : l <:+: []) : l = [] := eq_nil_of_sublist_nil (sublist_of_infix s) theorem eq_nil_of_prefix_nil {α : Type u} {l : List α} (s : l <+: []) : l = [] := eq_nil_of_infix_nil (infix_of_prefix s) theorem eq_nil_of_suffix_nil {α : Type u} {l : List α} (s : l <:+ []) : l = [] := eq_nil_of_infix_nil (infix_of_suffix s) theorem infix_iff_prefix_suffix {α : Type u} (l₁ : List α) (l₂ : List α) : l₁ <:+: l₂ ↔ ∃ (t : List α), l₁ <+: t ∧ t <:+ l₂ := sorry theorem eq_of_infix_of_length_eq {α : Type u} {l₁ : List α} {l₂ : List α} (s : l₁ <:+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq (sublist_of_infix s) theorem eq_of_prefix_of_length_eq {α : Type u} {l₁ : List α} {l₂ : List α} (s : l₁ <+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq (sublist_of_prefix s) theorem eq_of_suffix_of_length_eq {α : Type u} {l₁ : List α} {l₂ : List α} (s : l₁ <:+ l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq (sublist_of_suffix s) theorem prefix_of_prefix_length_le {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} : l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂ := sorry theorem prefix_or_prefix_of_prefix {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ := or.imp (prefix_of_prefix_length_le h₁ h₂) (prefix_of_prefix_length_le h₂ h₁) (le_total (length l₁) (length l₂)) theorem suffix_of_suffix_length_le {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ := sorry theorem suffix_or_suffix_of_suffix {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ := or.imp (iff.mp reverse_prefix) (iff.mp reverse_prefix) (prefix_or_prefix_of_prefix (iff.mpr reverse_prefix h₁) (iff.mpr reverse_prefix h₂)) theorem infix_of_mem_join {α : Type u} {L : List (List α)} {l : List α} : l ∈ L → l <:+: join L := sorry theorem prefix_append_right_inj {α : Type u} {l₁ : List α} {l₂ : List α} (l : List α) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ := sorry theorem prefix_cons_inj {α : Type u} {l₁ : List α} {l₂ : List α} (a : α) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ := prefix_append_right_inj [a] theorem take_prefix {α : Type u} (n : ℕ) (l : List α) : take n l <+: l := Exists.intro (drop n l) (take_append_drop n l) theorem drop_suffix {α : Type u} (n : ℕ) (l : List α) : drop n l <:+ l := Exists.intro (take n l) (take_append_drop n l) theorem tail_suffix {α : Type u} (l : List α) : tail l <:+ l := eq.mpr (id (Eq._oldrec (Eq.refl (tail l <:+ l)) (Eq.symm (drop_one l)))) (drop_suffix 1 l) theorem tail_subset {α : Type u} (l : List α) : tail l ⊆ l := sublist.subset (sublist_of_suffix (tail_suffix l)) theorem prefix_iff_eq_append {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ := sorry theorem suffix_iff_eq_append {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ := sorry theorem prefix_iff_eq_take {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ := sorry theorem suffix_iff_eq_drop {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ := sorry protected instance decidable_prefix {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : Decidable (l₁ <+: l₂) := sorry -- Alternatively, use mem_tails protected instance decidable_suffix {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : Decidable (l₁ <:+ l₂) := sorry theorem prefix_take_le_iff {α : Type u} {L : List (List (Option α))} {m : ℕ} {n : ℕ} (hm : m < length L) : take m L <+: take n L ↔ m ≤ n := sorry theorem cons_prefix_iff {α : Type u} {l : List α} {l' : List α} {x : α} {y : α} : x :: l <+: y :: l' ↔ x = y ∧ l <+: l' := sorry theorem map_prefix {α : Type u} {β : Type v} {l : List α} {l' : List α} (f : α → β) (h : l <+: l') : map f l <+: map f l' := sorry theorem is_prefix.filter_map {α : Type u} {β : Type v} {l : List α} {l' : List α} (h : l <+: l') (f : α → Option β) : filter_map f l <+: filter_map f l' := sorry theorem is_prefix.reduce_option {α : Type u} {l : List (Option α)} {l' : List (Option α)} (h : l <+: l') : reduce_option l <+: reduce_option l' := is_prefix.filter_map h id @[simp] theorem mem_inits {α : Type u} (s : List α) (t : List α) : s ∈ inits t ↔ s <+: t := sorry @[simp] theorem mem_tails {α : Type u} (s : List α) (t : List α) : s ∈ tails t ↔ s <:+ t := sorry theorem inits_cons {α : Type u} (a : α) (l : List α) : inits (a :: l) = [] :: map (fun (t : List α) => a :: t) (inits l) := sorry theorem tails_cons {α : Type u} (a : α) (l : List α) : tails (a :: l) = (a :: l) :: tails l := sorry @[simp] theorem inits_append {α : Type u} (s : List α) (t : List α) : inits (s ++ t) = inits s ++ map (fun (l : List α) => s ++ l) (tail (inits t)) := sorry @[simp] theorem tails_append {α : Type u} (s : List α) (t : List α) : tails (s ++ t) = map (fun (l : List α) => l ++ t) (tails s) ++ tail (tails t) := sorry -- the lemma names `inits_eq_tails` and `tails_eq_inits` are like `sublists_eq_sublists'` theorem inits_eq_tails {α : Type u} (l : List α) : inits l = reverse (map reverse (tails (reverse l))) := sorry theorem tails_eq_inits {α : Type u} (l : List α) : tails l = reverse (map reverse (inits (reverse l))) := sorry theorem inits_reverse {α : Type u} (l : List α) : inits (reverse l) = reverse (map reverse (tails l)) := sorry theorem tails_reverse {α : Type u} (l : List α) : tails (reverse l) = reverse (map reverse (inits l)) := sorry theorem map_reverse_inits {α : Type u} (l : List α) : map reverse (inits l) = reverse (tails (reverse l)) := sorry theorem map_reverse_tails {α : Type u} (l : List α) : map reverse (tails l) = reverse (inits (reverse l)) := sorry protected instance decidable_infix {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : Decidable (l₁ <:+: l₂) := sorry /-! ### sublists -/ @[simp] theorem sublists'_nil {α : Type u} : sublists' [] = [[]] := rfl @[simp] theorem sublists'_singleton {α : Type u} (a : α) : sublists' [a] = [[], [a]] := rfl theorem map_sublists'_aux {α : Type u} {β : Type v} {γ : Type w} (g : List β → List γ) (l : List α) (f : List α → List β) (r : List (List β)) : map g (sublists'_aux l f r) = sublists'_aux l (g ∘ f) (map g r) := sorry theorem sublists'_aux_append {α : Type u} {β : Type v} (r' : List (List β)) (l : List α) (f : List α → List β) (r : List (List β)) : sublists'_aux l f (r ++ r') = sublists'_aux l f r ++ r' := sorry theorem sublists'_aux_eq_sublists' {α : Type u} {β : Type v} (l : List α) (f : List α → List β) (r : List (List β)) : sublists'_aux l f r = map f (sublists' l) ++ r := sorry @[simp] theorem sublists'_cons {α : Type u} (a : α) (l : List α) : sublists' (a :: l) = sublists' l ++ map (List.cons a) (sublists' l) := sorry @[simp] theorem mem_sublists' {α : Type u} {s : List α} {t : List α} : s ∈ sublists' t ↔ s <+ t := sorry @[simp] theorem length_sublists' {α : Type u} (l : List α) : length (sublists' l) = bit0 1 ^ length l := sorry @[simp] theorem sublists_nil {α : Type u} : sublists [] = [[]] := rfl @[simp] theorem sublists_singleton {α : Type u} (a : α) : sublists [a] = [[], [a]] := rfl theorem sublists_aux₁_eq_sublists_aux {α : Type u} {β : Type v} (l : List α) (f : List α → List β) : sublists_aux₁ l f = sublists_aux l fun (ys : List α) (r : List β) => f ys ++ r := sorry theorem sublists_aux_cons_eq_sublists_aux₁ {α : Type u} (l : List α) : sublists_aux l List.cons = sublists_aux₁ l fun (x : List α) => [x] := sorry theorem sublists_aux_eq_foldr.aux {α : Type u} {β : Type v} {a : α} {l : List α} (IH₁ : ∀ (f : List α → List β → List β), sublists_aux l f = foldr f [] (sublists_aux l List.cons)) (IH₂ : ∀ (f : List α → List (List α) → List (List α)), sublists_aux l f = foldr f [] (sublists_aux l List.cons)) (f : List α → List β → List β) : sublists_aux (a :: l) f = foldr f [] (sublists_aux (a :: l) List.cons) := sorry theorem sublists_aux_eq_foldr {α : Type u} {β : Type v} (l : List α) (f : List α → List β → List β) : sublists_aux l f = foldr f [] (sublists_aux l List.cons) := sorry theorem sublists_aux_cons_cons {α : Type u} (l : List α) (a : α) : sublists_aux (a :: l) List.cons = [a] :: foldr (fun (ys : List α) (r : List (List α)) => ys :: (a :: ys) :: r) [] (sublists_aux l List.cons) := sorry theorem sublists_aux₁_append {α : Type u} {β : Type v} (l₁ : List α) (l₂ : List α) (f : List α → List β) : sublists_aux₁ (l₁ ++ l₂) f = sublists_aux₁ l₁ f ++ sublists_aux₁ l₂ fun (x : List α) => f x ++ sublists_aux₁ l₁ (f ∘ fun (_x : List α) => _x ++ x) := sorry theorem sublists_aux₁_concat {α : Type u} {β : Type v} (l : List α) (a : α) (f : List α → List β) : sublists_aux₁ (l ++ [a]) f = sublists_aux₁ l f ++ f [a] ++ sublists_aux₁ l fun (x : List α) => f (x ++ [a]) := sorry theorem sublists_aux₁_bind {α : Type u} {β : Type v} {γ : Type w} (l : List α) (f : List α → List β) (g : β → List γ) : list.bind (sublists_aux₁ l f) g = sublists_aux₁ l fun (x : List α) => list.bind (f x) g := sorry theorem sublists_aux_cons_append {α : Type u} (l₁ : List α) (l₂ : List α) : sublists_aux (l₁ ++ l₂) List.cons = sublists_aux l₁ List.cons ++ do let x ← sublists_aux l₂ List.cons (fun (_x : List α) => _x ++ x) <$> sublists l₁ := sorry theorem sublists_append {α : Type u} (l₁ : List α) (l₂ : List α) : sublists (l₁ ++ l₂) = do let x ← sublists l₂ (fun (_x : List α) => _x ++ x) <$> sublists l₁ := sorry @[simp] theorem sublists_concat {α : Type u} (l : List α) (a : α) : sublists (l ++ [a]) = sublists l ++ map (fun (x : List α) => x ++ [a]) (sublists l) := sorry theorem sublists_reverse {α : Type u} (l : List α) : sublists (reverse l) = map reverse (sublists' l) := sorry theorem sublists_eq_sublists' {α : Type u} (l : List α) : sublists l = map reverse (sublists' (reverse l)) := sorry theorem sublists'_reverse {α : Type u} (l : List α) : sublists' (reverse l) = map reverse (sublists l) := sorry theorem sublists'_eq_sublists {α : Type u} (l : List α) : sublists' l = map reverse (sublists (reverse l)) := sorry theorem sublists_aux_ne_nil {α : Type u} (l : List α) : ¬[] ∈ sublists_aux l List.cons := sorry @[simp] theorem mem_sublists {α : Type u} {s : List α} {t : List α} : s ∈ sublists t ↔ s <+ t := sorry @[simp] theorem length_sublists {α : Type u} (l : List α) : length (sublists l) = bit0 1 ^ length l := sorry theorem map_ret_sublist_sublists {α : Type u} (l : List α) : map list.ret l <+ sublists l := sorry /-! ### sublists_len -/ /-- Auxiliary function to construct the list of all sublists of a given length. Given an integer `n`, a list `l`, a function `f` and an auxiliary list `L`, it returns the list made of of `f` applied to all sublists of `l` of length `n`, concatenated with `L`. -/ def sublists_len_aux {α : Type u_1} {β : Type u_2} : ℕ → List α → (List α → β) → List β → List β := sorry /-- The list of all sublists of a list `l` that are of length `n`. For instance, for `l = [0, 1, 2, 3]` and `n = 2`, one gets `[[2, 3], [1, 3], [1, 2], [0, 3], [0, 2], [0, 1]]`. -/ def sublists_len {α : Type u_1} (n : ℕ) (l : List α) : List (List α) := sublists_len_aux n l id [] theorem sublists_len_aux_append {α : Type u_1} {β : Type u_2} {γ : Type u_3} (n : ℕ) (l : List α) (f : List α → β) (g : β → γ) (r : List β) (s : List γ) : sublists_len_aux n l (g ∘ f) (map g r ++ s) = map g (sublists_len_aux n l f r) ++ s := sorry theorem sublists_len_aux_eq {α : Type u_1} {β : Type u_2} (l : List α) (n : ℕ) (f : List α → β) (r : List β) : sublists_len_aux n l f r = map f (sublists_len n l) ++ r := sorry theorem sublists_len_aux_zero {β : Type v} {α : Type u_1} (l : List α) (f : List α → β) (r : List β) : sublists_len_aux 0 l f r = f [] :: r := list.cases_on l (Eq.refl (sublists_len_aux 0 [] f r)) fun (l_hd : α) (l_tl : List α) => Eq.refl (sublists_len_aux 0 (l_hd :: l_tl) f r) @[simp] theorem sublists_len_zero {α : Type u_1} (l : List α) : sublists_len 0 l = [[]] := sublists_len_aux_zero l id [] @[simp] theorem sublists_len_succ_nil {α : Type u_1} (n : ℕ) : sublists_len (n + 1) [] = [] := rfl @[simp] theorem sublists_len_succ_cons {α : Type u_1} (n : ℕ) (a : α) (l : List α) : sublists_len (n + 1) (a :: l) = sublists_len (n + 1) l ++ map (List.cons a) (sublists_len n l) := sorry @[simp] theorem length_sublists_len {α : Type u_1} (n : ℕ) (l : List α) : length (sublists_len n l) = nat.choose (length l) n := sorry theorem sublists_len_sublist_sublists' {α : Type u_1} (n : ℕ) (l : List α) : sublists_len n l <+ sublists' l := sorry theorem sublists_len_sublist_of_sublist {α : Type u_1} (n : ℕ) {l₁ : List α} {l₂ : List α} (h : l₁ <+ l₂) : sublists_len n l₁ <+ sublists_len n l₂ := sorry theorem length_of_sublists_len {α : Type u_1} {n : ℕ} {l : List α} {l' : List α} : l' ∈ sublists_len n l → length l' = n := sorry theorem mem_sublists_len_self {α : Type u_1} {l : List α} {l' : List α} (h : l' <+ l) : l' ∈ sublists_len (length l') l := sorry @[simp] theorem mem_sublists_len {α : Type u_1} {n : ℕ} {l : List α} {l' : List α} : l' ∈ sublists_len n l ↔ l' <+ l ∧ length l' = n := sorry /-! ### permutations -/ @[simp] theorem permutations_aux_nil {α : Type u} (is : List α) : permutations_aux [] is = [] := sorry @[simp] theorem permutations_aux_cons {α : Type u} (t : α) (ts : List α) (is : List α) : permutations_aux (t :: ts) is = foldr (fun (y : List α) (r : List (List α)) => prod.snd (permutations_aux2 t ts r y id)) (permutations_aux ts (t :: is)) (permutations is) := sorry /-! ### insert -/ @[simp] theorem insert_nil {α : Type u} [DecidableEq α] (a : α) : insert a [] = [a] := rfl theorem insert.def {α : Type u} [DecidableEq α] (a : α) (l : List α) : insert a l = ite (a ∈ l) l (a :: l) := rfl @[simp] theorem insert_of_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : insert a l = l := sorry @[simp] theorem insert_of_not_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : ¬a ∈ l) : insert a l = a :: l := sorry @[simp] theorem mem_insert_iff {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} : a ∈ insert b l ↔ a = b ∨ a ∈ l := sorry @[simp] theorem suffix_insert {α : Type u} [DecidableEq α] (a : α) (l : List α) : l <:+ insert a l := sorry @[simp] theorem mem_insert_self {α : Type u} [DecidableEq α] (a : α) (l : List α) : a ∈ insert a l := iff.mpr mem_insert_iff (Or.inl rfl) theorem mem_insert_of_mem {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} (h : a ∈ l) : a ∈ insert b l := iff.mpr mem_insert_iff (Or.inr h) theorem eq_or_mem_of_mem_insert {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} (h : a ∈ insert b l) : a = b ∨ a ∈ l := iff.mp mem_insert_iff h @[simp] theorem length_insert_of_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : length (insert a l) = length l := eq.mpr (id (Eq._oldrec (Eq.refl (length (insert a l) = length l)) (insert_of_mem h))) (Eq.refl (length l)) @[simp] theorem length_insert_of_not_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : ¬a ∈ l) : length (insert a l) = length l + 1 := eq.mpr (id (Eq._oldrec (Eq.refl (length (insert a l) = length l + 1)) (insert_of_not_mem h))) (Eq.refl (length (a :: l))) /-! ### erasep -/ @[simp] theorem erasep_nil {α : Type u} {p : α → Prop} [decidable_pred p] : erasep p [] = [] := rfl theorem erasep_cons {α : Type u} {p : α → Prop} [decidable_pred p] (a : α) (l : List α) : erasep p (a :: l) = ite (p a) l (a :: erasep p l) := rfl @[simp] theorem erasep_cons_of_pos {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} (h : p a) : erasep p (a :: l) = l := sorry @[simp] theorem erasep_cons_of_neg {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} (h : ¬p a) : erasep p (a :: l) = a :: erasep p l := sorry theorem erasep_of_forall_not {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} (h : ∀ (a : α), a ∈ l → ¬p a) : erasep p l = l := sorry theorem exists_of_erasep {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} {a : α} (al : a ∈ l) (pa : p a) : ∃ (a : α), ∃ (l₁ : List α), ∃ (l₂ : List α), (∀ (b : α), b ∈ l₁ → ¬p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ erasep p l = l₁ ++ l₂ := sorry theorem exists_or_eq_self_of_erasep {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : erasep p l = l ∨ ∃ (a : α), ∃ (l₁ : List α), ∃ (l₂ : List α), (∀ (b : α), b ∈ l₁ → ¬p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ erasep p l = l₁ ++ l₂ := sorry @[simp] theorem length_erasep_of_mem {α : Type u} {p : α → Prop} [decidable_pred p] {l : List α} {a : α} (al : a ∈ l) (pa : p a) : length (erasep p l) = Nat.pred (length l) := sorry theorem erasep_append_left {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} (pa : p a) {l₁ : List α} (l₂ : List α) : a ∈ l₁ → erasep p (l₁ ++ l₂) = erasep p l₁ ++ l₂ := sorry theorem erasep_append_right {α : Type u} {p : α → Prop} [decidable_pred p] {l₁ : List α} (l₂ : List α) : (∀ (b : α), b ∈ l₁ → ¬p b) → erasep p (l₁ ++ l₂) = l₁ ++ erasep p l₂ := sorry theorem erasep_sublist {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : erasep p l <+ l := sorry theorem erasep_subset {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : erasep p l ⊆ l := sublist.subset (erasep_sublist l) theorem sublist.erasep {α : Type u} {p : α → Prop} [decidable_pred p] {l₁ : List α} {l₂ : List α} (s : l₁ <+ l₂) : erasep p l₁ <+ erasep p l₂ := sorry theorem mem_of_mem_erasep {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} : a ∈ erasep p l → a ∈ l := erasep_subset l @[simp] theorem mem_erasep_of_neg {α : Type u} {p : α → Prop} [decidable_pred p] {a : α} {l : List α} (pa : ¬p a) : a ∈ erasep p l ↔ a ∈ l := sorry theorem erasep_map {α : Type u} {β : Type v} {p : α → Prop} [decidable_pred p] (f : β → α) (l : List β) : erasep p (map f l) = map f (erasep (p ∘ f) l) := sorry @[simp] theorem extractp_eq_find_erasep {α : Type u} {p : α → Prop} [decidable_pred p] (l : List α) : extractp p l = (find p l, erasep p l) := sorry /-! ### erase -/ @[simp] theorem erase_nil {α : Type u} [DecidableEq α] (a : α) : list.erase [] a = [] := rfl theorem erase_cons {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : list.erase (b :: l) a = ite (b = a) l (b :: list.erase l a) := rfl @[simp] theorem erase_cons_head {α : Type u} [DecidableEq α] (a : α) (l : List α) : list.erase (a :: l) a = l := sorry @[simp] theorem erase_cons_tail {α : Type u} [DecidableEq α] {a : α} {b : α} (l : List α) (h : b ≠ a) : list.erase (b :: l) a = b :: list.erase l a := sorry theorem erase_eq_erasep {α : Type u} [DecidableEq α] (a : α) (l : List α) : list.erase l a = erasep (Eq a) l := sorry @[simp] theorem erase_of_not_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : ¬a ∈ l) : list.erase l a = l := sorry theorem exists_erase_eq {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : ∃ (l₁ : List α), ∃ (l₂ : List α), ¬a ∈ l₁ ∧ l = l₁ ++ a :: l₂ ∧ list.erase l a = l₁ ++ l₂ := sorry @[simp] theorem length_erase_of_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : a ∈ l) : length (list.erase l a) = Nat.pred (length l) := eq.mpr (id (Eq._oldrec (Eq.refl (length (list.erase l a) = Nat.pred (length l))) (erase_eq_erasep a l))) (length_erasep_of_mem h rfl) theorem erase_append_left {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} (l₂ : List α) (h : a ∈ l₁) : list.erase (l₁ ++ l₂) a = list.erase l₁ a ++ l₂ := sorry theorem erase_append_right {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} (l₂ : List α) (h : ¬a ∈ l₁) : list.erase (l₁ ++ l₂) a = l₁ ++ list.erase l₂ a := sorry theorem erase_sublist {α : Type u} [DecidableEq α] (a : α) (l : List α) : list.erase l a <+ l := eq.mpr (id (Eq._oldrec (Eq.refl (list.erase l a <+ l)) (erase_eq_erasep a l))) (erasep_sublist l) theorem erase_subset {α : Type u} [DecidableEq α] (a : α) (l : List α) : list.erase l a ⊆ l := sublist.subset (erase_sublist a l) theorem sublist.erase {α : Type u} [DecidableEq α] (a : α) {l₁ : List α} {l₂ : List α} (h : l₁ <+ l₂) : list.erase l₁ a <+ list.erase l₂ a := sorry theorem mem_of_mem_erase {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} : a ∈ list.erase l b → a ∈ l := erase_subset b l @[simp] theorem mem_erase_of_ne {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} (ab : a ≠ b) : a ∈ list.erase l b ↔ a ∈ l := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ list.erase l b ↔ a ∈ l)) (erase_eq_erasep b l))) (mem_erasep_of_neg (ne.symm ab)) theorem erase_comm {α : Type u} [DecidableEq α] (a : α) (b : α) (l : List α) : list.erase (list.erase l a) b = list.erase (list.erase l b) a := sorry theorem map_erase {α : Type u} {β : Type v} [DecidableEq α] [DecidableEq β] {f : α → β} (finj : function.injective f) {a : α} (l : List α) : map f (list.erase l a) = list.erase (map f l) (f a) := sorry theorem map_foldl_erase {α : Type u} {β : Type v} [DecidableEq α] [DecidableEq β] {f : α → β} (finj : function.injective f) {l₁ : List α} {l₂ : List α} : map f (foldl list.erase l₁ l₂) = foldl (fun (l : List β) (a : α) => list.erase l (f a)) (map f l₁) l₂ := sorry @[simp] theorem count_erase_self {α : Type u} [DecidableEq α] (a : α) (s : List α) : count a (list.erase s a) = Nat.pred (count a s) := sorry @[simp] theorem count_erase_of_ne {α : Type u} [DecidableEq α] {a : α} {b : α} (ab : a ≠ b) (s : List α) : count a (list.erase s b) = count a s := sorry /-! ### diff -/ @[simp] theorem diff_nil {α : Type u} [DecidableEq α] (l : List α) : list.diff l [] = l := rfl @[simp] theorem diff_cons {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) (a : α) : list.diff l₁ (a :: l₂) = list.diff (list.erase l₁ a) l₂ := sorry theorem diff_cons_right {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) (a : α) : list.diff l₁ (a :: l₂) = list.erase (list.diff l₁ l₂) a := sorry theorem diff_erase {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) (a : α) : list.erase (list.diff l₁ l₂) a = list.diff (list.erase l₁ a) l₂ := sorry @[simp] theorem nil_diff {α : Type u} [DecidableEq α] (l : List α) : list.diff [] l = [] := sorry theorem diff_eq_foldl {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : list.diff l₁ l₂ = foldl list.erase l₁ l₂ := sorry @[simp] theorem diff_append {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) (l₃ : List α) : list.diff l₁ (l₂ ++ l₃) = list.diff (list.diff l₁ l₂) l₃ := sorry @[simp] theorem map_diff {α : Type u} {β : Type v} [DecidableEq α] [DecidableEq β] {f : α → β} (finj : function.injective f) {l₁ : List α} {l₂ : List α} : map f (list.diff l₁ l₂) = list.diff (map f l₁) (map f l₂) := sorry theorem diff_sublist {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : list.diff l₁ l₂ <+ l₁ := sorry theorem diff_subset {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : list.diff l₁ l₂ ⊆ l₁ := sublist.subset (diff_sublist l₁ l₂) theorem mem_diff_of_mem {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} {l₂ : List α} : a ∈ l₁ → ¬a ∈ l₂ → a ∈ list.diff l₁ l₂ := sorry theorem sublist.diff_right {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} {l₃ : List α} : l₁ <+ l₂ → list.diff l₁ l₃ <+ list.diff l₂ l₃ := sorry theorem erase_diff_erase_sublist_of_sublist {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → list.diff (list.erase l₂ a) (list.erase l₁ a) <+ list.diff l₂ l₁ := sorry /-! ### enum -/ theorem length_enum_from {α : Type u} (n : ℕ) (l : List α) : length (enum_from n l) = length l := sorry theorem length_enum {α : Type u} (l : List α) : length (enum l) = length l := length_enum_from 0 @[simp] theorem enum_from_nth {α : Type u} (n : ℕ) (l : List α) (m : ℕ) : nth (enum_from n l) m = (fun (a : α) => (n + m, a)) <$> nth l m := sorry @[simp] theorem enum_nth {α : Type u} (l : List α) (n : ℕ) : nth (enum l) n = (fun (a : α) => (n, a)) <$> nth l n := sorry @[simp] theorem enum_from_map_snd {α : Type u} (n : ℕ) (l : List α) : map prod.snd (enum_from n l) = l := sorry @[simp] theorem enum_map_snd {α : Type u} (l : List α) : map prod.snd (enum l) = l := enum_from_map_snd 0 theorem mem_enum_from {α : Type u} {x : α} {i : ℕ} {j : ℕ} (xs : List α) : (i, x) ∈ enum_from j xs → j ≤ i ∧ i < j + length xs ∧ x ∈ xs := sorry /-! ### product -/ @[simp] theorem nil_product {α : Type u} {β : Type v} (l : List β) : product [] l = [] := rfl @[simp] theorem product_cons {α : Type u} {β : Type v} (a : α) (l₁ : List α) (l₂ : List β) : product (a :: l₁) l₂ = map (fun (b : β) => (a, b)) l₂ ++ product l₁ l₂ := rfl @[simp] theorem product_nil {α : Type u} {β : Type v} (l : List α) : product l [] = [] := sorry @[simp] theorem mem_product {α : Type u} {β : Type v} {l₁ : List α} {l₂ : List β} {a : α} {b : β} : (a, b) ∈ product l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ := sorry theorem length_product {α : Type u} {β : Type v} (l₁ : List α) (l₂ : List β) : length (product l₁ l₂) = length l₁ * length l₂ := sorry /-! ### sigma -/ @[simp] theorem nil_sigma {α : Type u} {σ : α → Type u_1} (l : (a : α) → List (σ a)) : list.sigma [] l = [] := rfl @[simp] theorem sigma_cons {α : Type u} {σ : α → Type u_1} (a : α) (l₁ : List α) (l₂ : (a : α) → List (σ a)) : list.sigma (a :: l₁) l₂ = map (sigma.mk a) (l₂ a) ++ list.sigma l₁ l₂ := rfl @[simp] theorem sigma_nil {α : Type u} {σ : α → Type u_1} (l : List α) : (list.sigma l fun (a : α) => []) = [] := sorry @[simp] theorem mem_sigma {α : Type u} {σ : α → Type u_1} {l₁ : List α} {l₂ : (a : α) → List (σ a)} {a : α} {b : σ a} : sigma.mk a b ∈ list.sigma l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ a := sorry theorem length_sigma {α : Type u} {σ : α → Type u_1} (l₁ : List α) (l₂ : (a : α) → List (σ a)) : length (list.sigma l₁ l₂) = sum (map (fun (a : α) => length (l₂ a)) l₁) := sorry /-! ### disjoint -/ theorem disjoint.symm {α : Type u} {l₁ : List α} {l₂ : List α} (d : disjoint l₁ l₂) : disjoint l₂ l₁ := fun {a : α} (ᾰ : a ∈ l₂) (ᾰ_1 : a ∈ l₁) => idRhs False (d ᾰ_1 ᾰ) theorem disjoint_comm {α : Type u} {l₁ : List α} {l₂ : List α} : disjoint l₁ l₂ ↔ disjoint l₂ l₁ := { mp := disjoint.symm, mpr := disjoint.symm } theorem disjoint_left {α : Type u} {l₁ : List α} {l₂ : List α} : disjoint l₁ l₂ ↔ ∀ {a : α}, a ∈ l₁ → ¬a ∈ l₂ := iff.rfl theorem disjoint_right {α : Type u} {l₁ : List α} {l₂ : List α} : disjoint l₁ l₂ ↔ ∀ {a : α}, a ∈ l₂ → ¬a ∈ l₁ := disjoint_comm theorem disjoint_iff_ne {α : Type u} {l₁ : List α} {l₂ : List α} : disjoint l₁ l₂ ↔ ∀ (a : α), a ∈ l₁ → ∀ (b : α), b ∈ l₂ → a ≠ b := sorry theorem disjoint_of_subset_left {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (ss : l₁ ⊆ l) (d : disjoint l l₂) : disjoint l₁ l₂ := fun {a : α} (ᾰ : a ∈ l₁) => idRhs (a ∈ l₂ → False) (d (ss ᾰ)) theorem disjoint_of_subset_right {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (ss : l₂ ⊆ l) (d : disjoint l₁ l) : disjoint l₁ l₂ := fun {a : α} (ᾰ : a ∈ l₁) (ᾰ_1 : a ∈ l₂) => idRhs False (d ᾰ (ss ᾰ_1)) theorem disjoint_of_disjoint_cons_left {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : disjoint (a :: l₁) l₂ → disjoint l₁ l₂ := disjoint_of_subset_left (subset_cons a l₁) theorem disjoint_of_disjoint_cons_right {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : disjoint l₁ (a :: l₂) → disjoint l₁ l₂ := disjoint_of_subset_right (subset_cons a l₂) @[simp] theorem disjoint_nil_left {α : Type u} (l : List α) : disjoint [] l := fun {a : α} => idRhs (a ∈ [] → a ∈ l → False) (not.elim (not_mem_nil a)) @[simp] theorem disjoint_nil_right {α : Type u} (l : List α) : disjoint l [] := eq.mpr (id (Eq._oldrec (Eq.refl (disjoint l [])) (propext disjoint_comm))) (disjoint_nil_left l) @[simp] theorem singleton_disjoint {α : Type u} {l : List α} {a : α} : disjoint [a] l ↔ ¬a ∈ l := sorry @[simp] theorem disjoint_singleton {α : Type u} {l : List α} {a : α} : disjoint l [a] ↔ ¬a ∈ l := sorry @[simp] theorem disjoint_append_left {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} : disjoint (l₁ ++ l₂) l ↔ disjoint l₁ l ∧ disjoint l₂ l := sorry @[simp] theorem disjoint_append_right {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} : disjoint l (l₁ ++ l₂) ↔ disjoint l l₁ ∧ disjoint l l₂ := sorry @[simp] theorem disjoint_cons_left {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : disjoint (a :: l₁) l₂ ↔ ¬a ∈ l₂ ∧ disjoint l₁ l₂ := sorry @[simp] theorem disjoint_cons_right {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : disjoint l₁ (a :: l₂) ↔ ¬a ∈ l₁ ∧ disjoint l₁ l₂ := sorry theorem disjoint_of_disjoint_append_left_left {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (d : disjoint (l₁ ++ l₂) l) : disjoint l₁ l := and.left (iff.mp disjoint_append_left d) theorem disjoint_of_disjoint_append_left_right {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (d : disjoint (l₁ ++ l₂) l) : disjoint l₂ l := and.right (iff.mp disjoint_append_left d) theorem disjoint_of_disjoint_append_right_left {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (d : disjoint l (l₁ ++ l₂)) : disjoint l l₁ := and.left (iff.mp disjoint_append_right d) theorem disjoint_of_disjoint_append_right_right {α : Type u} {l₁ : List α} {l₂ : List α} {l : List α} (d : disjoint l (l₁ ++ l₂)) : disjoint l l₂ := and.right (iff.mp disjoint_append_right d) theorem disjoint_take_drop {α : Type u} {l : List α} {m : ℕ} {n : ℕ} (hl : nodup l) (h : m ≤ n) : disjoint (take m l) (drop n l) := sorry /-! ### union -/ @[simp] theorem nil_union {α : Type u} [DecidableEq α] (l : List α) : [] ∪ l = l := rfl @[simp] theorem cons_union {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) (a : α) : a :: l₁ ∪ l₂ = insert a (l₁ ∪ l₂) := rfl @[simp] theorem mem_union {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} {a : α} : a ∈ l₁ ∪ l₂ ↔ a ∈ l₁ ∨ a ∈ l₂ := sorry theorem mem_union_left {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} (h : a ∈ l₁) (l₂ : List α) : a ∈ l₁ ∪ l₂ := iff.mpr mem_union (Or.inl h) theorem mem_union_right {α : Type u} [DecidableEq α] {a : α} (l₁ : List α) {l₂ : List α} (h : a ∈ l₂) : a ∈ l₁ ∪ l₂ := iff.mpr mem_union (Or.inr h) theorem sublist_suffix_of_union {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : ∃ (t : List α), t <+ l₁ ∧ t ++ l₂ = l₁ ∪ l₂ := sorry theorem suffix_union_right {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : l₂ <:+ l₁ ∪ l₂ := Exists.imp (fun (a : List α) => and.right) (sublist_suffix_of_union l₁ l₂) theorem union_sublist_append {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : l₁ ∪ l₂ <+ l₁ ++ l₂ := sorry theorem forall_mem_union {α : Type u} [DecidableEq α] {p : α → Prop} {l₁ : List α} {l₂ : List α} : (∀ (x : α), x ∈ l₁ ∪ l₂ → p x) ↔ (∀ (x : α), x ∈ l₁ → p x) ∧ ∀ (x : α), x ∈ l₂ → p x := sorry theorem forall_mem_of_forall_mem_union_left {α : Type u} [DecidableEq α] {p : α → Prop} {l₁ : List α} {l₂ : List α} (h : ∀ (x : α), x ∈ l₁ ∪ l₂ → p x) (x : α) (H : x ∈ l₁) : p x := and.left (iff.mp forall_mem_union h) theorem forall_mem_of_forall_mem_union_right {α : Type u} [DecidableEq α] {p : α → Prop} {l₁ : List α} {l₂ : List α} (h : ∀ (x : α), x ∈ l₁ ∪ l₂ → p x) (x : α) (H : x ∈ l₂) : p x := and.right (iff.mp forall_mem_union h) /-! ### inter -/ @[simp] theorem inter_nil {α : Type u} [DecidableEq α] (l : List α) : [] ∩ l = [] := rfl @[simp] theorem inter_cons_of_mem {α : Type u} [DecidableEq α] {a : α} (l₁ : List α) {l₂ : List α} (h : a ∈ l₂) : (a :: l₁) ∩ l₂ = a :: l₁ ∩ l₂ := if_pos h @[simp] theorem inter_cons_of_not_mem {α : Type u} [DecidableEq α] {a : α} (l₁ : List α) {l₂ : List α} (h : ¬a ∈ l₂) : (a :: l₁) ∩ l₂ = l₁ ∩ l₂ := if_neg h theorem mem_of_mem_inter_left {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₁ := mem_of_mem_filter theorem mem_of_mem_inter_right {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₂ := of_mem_filter theorem mem_inter_of_mem_of_mem {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} {a : α} : a ∈ l₁ → a ∈ l₂ → a ∈ l₁ ∩ l₂ := mem_filter_of_mem @[simp] theorem mem_inter {α : Type u} [DecidableEq α] {a : α} {l₁ : List α} {l₂ : List α} : a ∈ l₁ ∩ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ := mem_filter theorem inter_subset_left {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : l₁ ∩ l₂ ⊆ l₁ := filter_subset l₁ theorem inter_subset_right {α : Type u} [DecidableEq α] (l₁ : List α) (l₂ : List α) : l₁ ∩ l₂ ⊆ l₂ := fun (a : α) => mem_of_mem_inter_right theorem subset_inter {α : Type u} [DecidableEq α] {l : List α} {l₁ : List α} {l₂ : List α} (h₁ : l ⊆ l₁) (h₂ : l ⊆ l₂) : l ⊆ l₁ ∩ l₂ := fun (a : α) (h : a ∈ l) => iff.mpr mem_inter { left := h₁ h, right := h₂ h } theorem inter_eq_nil_iff_disjoint {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} : l₁ ∩ l₂ = [] ↔ disjoint l₁ l₂ := sorry theorem forall_mem_inter_of_forall_left {α : Type u} [DecidableEq α] {p : α → Prop} {l₁ : List α} (h : ∀ (x : α), x ∈ l₁ → p x) (l₂ : List α) (x : α) : x ∈ l₁ ∩ l₂ → p x := ball.imp_left (fun (x : α) => mem_of_mem_inter_left) h theorem forall_mem_inter_of_forall_right {α : Type u} [DecidableEq α] {p : α → Prop} (l₁ : List α) {l₂ : List α} (h : ∀ (x : α), x ∈ l₂ → p x) (x : α) : x ∈ l₁ ∩ l₂ → p x := ball.imp_left (fun (x : α) => mem_of_mem_inter_right) h @[simp] theorem inter_reverse {α : Type u} [DecidableEq α] {xs : List α} {ys : List α} : list.inter xs (reverse ys) = list.inter xs ys := sorry theorem choose_spec {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) (hp : ∃ (a : α), a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := subtype.property (choose_x p l hp) theorem choose_mem {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) (hp : ∃ (a : α), a ∈ l ∧ p a) : choose p l hp ∈ l := and.left (choose_spec p l hp) theorem choose_property {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) (hp : ∃ (a : α), a ∈ l ∧ p a) : p (choose p l hp) := and.right (choose_spec p l hp) /-! ### map₂_left' -/ -- The definitional equalities for `map₂_left'` can already be used by the -- simplifie because `map₂_left'` is marked `@[simp]`. @[simp] theorem map₂_left'_nil_right {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β → γ) (as : List α) : map₂_left' f as [] = (map (fun (a : α) => f a none) as, []) := list.cases_on as (Eq.refl (map₂_left' f [] [])) fun (as_hd : α) (as_tl : List α) => Eq.refl (map₂_left' f (as_hd :: as_tl) []) /-! ### map₂_right' -/ @[simp] theorem map₂_right'_nil_left {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (bs : List β) : map₂_right' f [] bs = (map (f none) bs, []) := list.cases_on bs (Eq.refl (map₂_right' f [] [])) fun (bs_hd : β) (bs_tl : List β) => Eq.refl (map₂_right' f [] (bs_hd :: bs_tl)) @[simp] theorem map₂_right'_nil_right {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (as : List α) : map₂_right' f as [] = ([], as) := rfl @[simp] theorem map₂_right'_nil_cons {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (b : β) (bs : List β) : map₂_right' f [] (b :: bs) = (f none b :: map (f none) bs, []) := rfl @[simp] theorem map₂_right'_cons_cons {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (a : α) (as : List α) (b : β) (bs : List β) : map₂_right' f (a :: as) (b :: bs) = let rec : List γ × List α := map₂_right' f as bs; (f (some a) b :: prod.fst rec, prod.snd rec) := rfl /-! ### zip_left' -/ @[simp] theorem zip_left'_nil_right {α : Type u} {β : Type v} (as : List α) : zip_left' as [] = (map (fun (a : α) => (a, none)) as, []) := list.cases_on as (Eq.refl (zip_left' [] [])) fun (as_hd : α) (as_tl : List α) => Eq.refl (zip_left' (as_hd :: as_tl) []) @[simp] theorem zip_left'_nil_left {α : Type u} {β : Type v} (bs : List β) : zip_left' [] bs = ([], bs) := rfl @[simp] theorem zip_left'_cons_nil {α : Type u} {β : Type v} (a : α) (as : List α) : zip_left' (a :: as) [] = ((a, none) :: map (fun (a : α) => (a, none)) as, []) := rfl @[simp] theorem zip_left'_cons_cons {α : Type u} {β : Type v} (a : α) (as : List α) (b : β) (bs : List β) : zip_left' (a :: as) (b :: bs) = let rec : List (α × Option β) × List β := zip_left' as bs; ((a, some b) :: prod.fst rec, prod.snd rec) := rfl /-! ### zip_right' -/ @[simp] theorem zip_right'_nil_left {α : Type u} {β : Type v} (bs : List β) : zip_right' [] bs = (map (fun (b : β) => (none, b)) bs, []) := list.cases_on bs (Eq.refl (zip_right' [] [])) fun (bs_hd : β) (bs_tl : List β) => Eq.refl (zip_right' [] (bs_hd :: bs_tl)) @[simp] theorem zip_right'_nil_right {α : Type u} {β : Type v} (as : List α) : zip_right' as [] = ([], as) := rfl @[simp] theorem zip_right'_nil_cons {α : Type u} {β : Type v} (b : β) (bs : List β) : zip_right' [] (b :: bs) = ((none, b) :: map (fun (b : β) => (none, b)) bs, []) := rfl @[simp] theorem zip_right'_cons_cons {α : Type u} {β : Type v} (a : α) (as : List α) (b : β) (bs : List β) : zip_right' (a :: as) (b :: bs) = let rec : List (Option α × β) × List α := zip_right' as bs; ((some a, b) :: prod.fst rec, prod.snd rec) := rfl /-! ### map₂_left -/ -- The definitional equalities for `map₂_left` can already be used by the -- simplifier because `map₂_left` is marked `@[simp]`. @[simp] theorem map₂_left_nil_right {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β → γ) (as : List α) : map₂_left f as [] = map (fun (a : α) => f a none) as := list.cases_on as (Eq.refl (map₂_left f [] [])) fun (as_hd : α) (as_tl : List α) => Eq.refl (map₂_left f (as_hd :: as_tl) []) theorem map₂_left_eq_map₂_left' {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β → γ) (as : List α) (bs : List β) : map₂_left f as bs = prod.fst (map₂_left' f as bs) := sorry theorem map₂_left_eq_map₂ {α : Type u} {β : Type v} {γ : Type w} (f : α → Option β → γ) (as : List α) (bs : List β) : length as ≤ length bs → map₂_left f as bs = map₂ (fun (a : α) (b : β) => f a (some b)) as bs := sorry /-! ### map₂_right -/ @[simp] theorem map₂_right_nil_left {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (bs : List β) : map₂_right f [] bs = map (f none) bs := list.cases_on bs (Eq.refl (map₂_right f [] [])) fun (bs_hd : β) (bs_tl : List β) => Eq.refl (map₂_right f [] (bs_hd :: bs_tl)) @[simp] theorem map₂_right_nil_right {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (as : List α) : map₂_right f as [] = [] := rfl @[simp] theorem map₂_right_nil_cons {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (b : β) (bs : List β) : map₂_right f [] (b :: bs) = f none b :: map (f none) bs := rfl @[simp] theorem map₂_right_cons_cons {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (a : α) (as : List α) (b : β) (bs : List β) : map₂_right f (a :: as) (b :: bs) = f (some a) b :: map₂_right f as bs := rfl theorem map₂_right_eq_map₂_right' {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (as : List α) (bs : List β) : map₂_right f as bs = prod.fst (map₂_right' f as bs) := sorry theorem map₂_right_eq_map₂ {α : Type u} {β : Type v} {γ : Type w} (f : Option α → β → γ) (as : List α) (bs : List β) (h : length bs ≤ length as) : map₂_right f as bs = map₂ (fun (a : α) (b : β) => f (some a) b) as bs := sorry /-! ### zip_left -/ @[simp] theorem zip_left_nil_right {α : Type u} {β : Type v} (as : List α) : zip_left as [] = map (fun (a : α) => (a, none)) as := list.cases_on as (Eq.refl (zip_left [] [])) fun (as_hd : α) (as_tl : List α) => Eq.refl (zip_left (as_hd :: as_tl) []) @[simp] theorem zip_left_nil_left {α : Type u} {β : Type v} (bs : List β) : zip_left [] bs = [] := rfl @[simp] theorem zip_left_cons_nil {α : Type u} {β : Type v} (a : α) (as : List α) : zip_left (a :: as) [] = (a, none) :: map (fun (a : α) => (a, none)) as := rfl @[simp] theorem zip_left_cons_cons {α : Type u} {β : Type v} (a : α) (as : List α) (b : β) (bs : List β) : zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs := rfl theorem zip_left_eq_zip_left' {α : Type u} {β : Type v} (as : List α) (bs : List β) : zip_left as bs = prod.fst (zip_left' as bs) := sorry /-! ### zip_right -/ @[simp] theorem zip_right_nil_left {α : Type u} {β : Type v} (bs : List β) : zip_right [] bs = map (fun (b : β) => (none, b)) bs := list.cases_on bs (Eq.refl (zip_right [] [])) fun (bs_hd : β) (bs_tl : List β) => Eq.refl (zip_right [] (bs_hd :: bs_tl)) @[simp] theorem zip_right_nil_right {α : Type u} {β : Type v} (as : List α) : zip_right as [] = [] := rfl @[simp] theorem zip_right_nil_cons {α : Type u} {β : Type v} (b : β) (bs : List β) : zip_right [] (b :: bs) = (none, b) :: map (fun (b : β) => (none, b)) bs := rfl @[simp] theorem zip_right_cons_cons {α : Type u} {β : Type v} (a : α) (as : List α) (b : β) (bs : List β) : zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs := rfl theorem zip_right_eq_zip_right' {α : Type u} {β : Type v} (as : List α) (bs : List β) : zip_right as bs = prod.fst (zip_right' as bs) := sorry /-! ### Miscellaneous lemmas -/ theorem ilast'_mem {α : Type u} (a : α) (l : List α) : ilast' a l ∈ a :: l := sorry @[simp] theorem nth_le_attach {α : Type u} (L : List α) (i : ℕ) (H : i < length (attach L)) : subtype.val (nth_le (attach L) i H) = nth_le L i (length_attach L ▸ H) := sorry end list theorem monoid_hom.map_list_prod {α : Type u_1} {β : Type u_2} [monoid α] [monoid β] (f : α →* β) (l : List α) : coe_fn f (list.prod l) = list.prod (list.map (⇑f) l) := Eq.symm (list.prod_hom l f) namespace list theorem sum_map_hom {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_monoid β] [add_monoid γ] (L : List α) (f : α → β) (g : β →+ γ) : sum (map (⇑g ∘ f) L) = coe_fn g (sum (map f L)) := sorry theorem sum_map_mul_left {α : Type u_1} [semiring α] {β : Type u_2} (L : List β) (f : β → α) (r : α) : sum (map (fun (b : β) => r * f b) L) = r * sum (map f L) := sum_map_hom L f (add_monoid_hom.mul_left r) theorem sum_map_mul_right {α : Type u_1} [semiring α] {β : Type u_2} (L : List β) (f : β → α) (r : α) : sum (map (fun (b : β) => f b * r) L) = sum (map f L) * r := sum_map_hom L f (add_monoid_hom.mul_right r) @[simp] theorem mem_map_swap {α : Type u} {β : Type v} (x : α) (y : β) (xs : List (α × β)) : (y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs := sorry theorem slice_eq {α : Type u_1} (xs : List α) (n : ℕ) (m : ℕ) : slice n m xs = take n xs ++ drop (n + m) xs := sorry theorem sizeof_slice_lt {α : Type u_1} [SizeOf α] (i : ℕ) (j : ℕ) (hj : 0 < j) (xs : List α) (hi : i < length xs) : sizeof (slice i j xs) < sizeof xs := sorry end Mathlib
69030ec107d9d6419efbb59e67edf7b3a66c8bd4
562dafcca9e8bf63ce6217723b51f32e89cdcc80
/src/super/clausifier.lean
3be4f631b31d461e40c71a094ebfb2612c226eec
[]
no_license
digama0/super
660801ef3edab2c046d6a01ba9bbce53e41523e8
976957fe46128e6ef057bc771f532c27cce5a910
refs/heads/master
1,606,987,814,510
1,498,621,778,000
1,498,621,778,000
95,626,306
0
0
null
1,498,621,692,000
1,498,621,692,000
null
UTF-8
Lean
false
false
10,779
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .clause_ops import .prover_state .misc_preprocessing open expr list tactic monad decidable universe u namespace super meta def try_option {a : Type u} (tac : tactic a) : tactic (option a) := lift some tac <|> return none private meta def normalize : expr → tactic expr | e := do e' ← whnf e reducible, args' ← monad.for e'.get_app_args normalize, return $ app_of_list e'.get_app_fn args' meta def inf_normalize_l (c : clause) : tactic (list clause) := on_first_left c $ λtype, do type' ← normalize type, guard $ type' ≠ type, h ← mk_local_def `h type', return [([h], h)] meta def inf_normalize_r (c : clause) : tactic (list clause) := on_first_right c $ λha, do a' ← normalize ha.local_type, guard $ a' ≠ ha.local_type, hna ← mk_local_def `hna (imp a' c.local_false), return [([hna], app hna ha)] meta def inf_false_l (c : clause) : tactic (list clause) := first $ do i ← list.range c.num_lits, if c.get_lit i = clause.literal.left `(false) then [return []] else [] meta def inf_false_r (c : clause) : tactic (list clause) := on_first_right c $ λhf, if hf.local_type = c.local_false then return [([], hf)] else match hf.local_type with | const ``false [] := do pr ← mk_app ``false.rec [c.local_false, hf], return [([], pr)] | _ := failed end meta def inf_true_l (c : clause) : tactic (list clause) := on_first_left c $ λt, match t with | (const ``true []) := return [([], const ``true.intro [])] | _ := failed end meta def inf_true_r (c : clause) : tactic (list clause) := first $ do i ← list.range c.num_lits, if c.get_lit i = clause.literal.right (const ``true []) then [return []] else [] meta def inf_not_l (c : clause) : tactic (list clause) := on_first_left c $ λtype, match type with | app (const ``not []) a := do hna ← mk_local_def `h (a.imp `(false)), return [([hna], hna)] | _ := failed end meta def inf_not_r (c : clause) : tactic (list clause) := on_first_right c $ λhna, match hna.local_type with | app (const ``not []) a := do hnna ← mk_local_def `h ((a.imp `(false)).imp c.local_false), return [([hnna], app hnna hna)] | _ := failed end meta def inf_and_l (c : clause) : tactic (list clause) := on_first_left c $ λab, match ab with | (app (app (const ``and []) a) b) := do ha ← mk_local_def `l a, hb ← mk_local_def `r b, pab ← mk_mapp ``and.intro [some a, some b, some ha, some hb], return [([ha, hb], pab)] | _ := failed end meta def inf_and_r (c : clause) : tactic (list clause) := on_first_right' c $ λhyp, do pa ← mk_mapp ``and.left [none, none, some hyp], pb ← mk_mapp ``and.right [none, none, some hyp], return [([], pa), ([], pb)] meta def inf_iff_l (c : clause) : tactic (list clause) := on_first_left c $ λab, match ab with | (app (app (const ``iff []) a) b) := do hab ← mk_local_def `l (imp a b), hba ← mk_local_def `r (imp b a), pab ← mk_mapp ``iff.intro [some a, some b, some hab, some hba], return [([hab, hba], pab)] | _ := failed end meta def inf_iff_r (c : clause) : tactic (list clause) := on_first_right' c $ λhyp, do pa ← mk_mapp ``iff.mp [none, none, some hyp], pb ← mk_mapp ``iff.mpr [none, none, some hyp], return [([], pa), ([], pb)] meta def inf_or_r (c : clause) : tactic (list clause) := on_first_right c $ λhab, match hab.local_type with | (app (app (const ``or []) a) b) := do hna ← mk_local_def `l (imp a c.local_false), hnb ← mk_local_def `r (imp b c.local_false), proof ← mk_app ``or.elim [a, b, c.local_false, hab, hna, hnb], return [([hna, hnb], proof)] | _ := failed end meta def inf_or_l (c : clause) : tactic (list clause) := on_first_left c $ λab, match ab with | (app (app (const ``or []) a) b) := do ha ← mk_local_def `l a, hb ← mk_local_def `l b, pa ← mk_mapp ``or.inl [some a, some b, some ha], pb ← mk_mapp ``or.inr [some a, some b, some hb], return [([ha], pa), ([hb], pb)] | _ := failed end meta def inf_all_r (c : clause) : tactic (list clause) := on_first_right' c $ λhallb, match hallb.local_type with | (pi n bi a b) := do ha ← mk_local_def `x a, return [([ha], app hallb ha)] | _ := failed end lemma imp_l {F a b} [decidable a] : ((a → b) → F) → ((a → F) → F) := λhabf haf, decidable.by_cases (assume ha : a, haf ha) (assume hna : ¬a, habf (take ha, absurd ha hna)) lemma imp_l' {F a b} [decidable F] : ((a → b) → F) → ((a → F) → F) := λhabf haf, decidable.by_cases (assume hf : F, hf) (assume hnf : ¬F, habf (take ha, absurd (haf ha) hnf)) lemma imp_l_c {F : Prop} {a b} : ((a → b) → F) → ((a → F) → F) := λhabf haf, classical.by_cases (assume hf : F, hf) (assume hnf : ¬F, habf (take ha, absurd (haf ha) hnf)) meta def inf_imp_l (c : clause) : tactic (list clause) := on_first_left_dn c $ λhnab, match hnab.local_type with | (pi _ _ (pi _ _ a b) _) := if b.has_var then failed else do hna ← mk_local_def `na (imp a c.local_false), pf ← first (do r ← [``super.imp_l, ``super.imp_l', ``super.imp_l_c], [mk_app r [hnab, hna]]), hb ← mk_local_def `b b, return [([hna], pf), ([hb], app hnab (lam `a binder_info.default a hb))] | _ := failed end meta def inf_ex_l (c : clause) : tactic (list clause) := on_first_left c $ λexp, match exp with | (app (app (const ``Exists [u]) dom) pred) := do hx ← mk_local_def `x dom, predx ← whnf $ app pred hx, hpx ← mk_local_def `hpx predx, return [([hx,hpx], app_of_list (const ``exists.intro [u]) [dom, pred, hx, hpx])] | _ := failed end lemma demorgan' {F a} {b : a → Prop} : ((∀x, b x) → F) → (((∃x, b x → F) → F) → F) := assume hab hnenb, classical.by_cases (assume h : ∃x, ¬b x, begin cases h with x, apply hnenb, existsi x, intros, contradiction end) (assume h : ¬∃x, ¬b x, hab (take x, classical.by_cases (assume bx : b x, bx) (assume nbx : ¬b x, have hf : false, { apply h, existsi x, assumption }, by contradiction))) meta def inf_all_l (c : clause) : tactic (list clause) := on_first_left_dn c $ λhnallb, match hnallb.local_type with | pi _ _ (pi n bi a b) _ := do enb ← mk_mapp ``Exists [none, some $ lam n binder_info.default a (imp b c.local_false)], hnenb ← mk_local_def `h (imp enb c.local_false), pr ← mk_app ``super.demorgan' [hnallb, hnenb], return [([hnenb], pr)] | _ := failed end meta def inf_ex_r (c : clause) : tactic (list clause) := do (qf, ctx) ← c.open_constn c.num_quants, skolemized ← on_first_right' qf $ λhexp, match hexp.local_type with | (app (app (const ``Exists [_]) d) p) := do sk_sym_name_pp ← get_unused_name `sk (some 1), inh_lc ← mk_local' `w binder_info.implicit d, sk_sym ← mk_local_def sk_sym_name_pp (pis (ctx ++ [inh_lc]) d), sk_p ← whnf_no_delta $ app p (app_of_list sk_sym (ctx ++ [inh_lc])), sk_ax ← mk_mapp ``Exists [some (local_type sk_sym), some (lambdas [sk_sym] (pis (ctx ++ [inh_lc]) (imp hexp.local_type sk_p)))], sk_ax_name ← get_unused_name `sk_axiom (some 1), assert sk_ax_name sk_ax, nonempt_of_inh ← mk_mapp ``nonempty.intro [some d, some inh_lc], eps ← mk_mapp ``classical.epsilon [some d, some nonempt_of_inh, some p], existsi (lambdas (ctx ++ [inh_lc]) eps), eps_spec ← mk_mapp ``classical.epsilon_spec [some d, some p], exact (lambdas (ctx ++ [inh_lc]) eps_spec), sk_ax_local ← get_local sk_ax_name, cases sk_ax_local [sk_sym_name_pp, sk_ax_name], sk_ax' ← get_local sk_ax_name, return [([inh_lc], app_of_list sk_ax' (ctx ++ [inh_lc, hexp]))] | _ := failed end, return $ skolemized.map (λs, s.close_constn ctx) meta def first_some {a : Type} : list (tactic (option a)) → tactic (option a) | [] := return none | (x::xs) := do xres ← x, match xres with some y := return (some y) | none := first_some xs end private meta def get_clauses_core' (rules : list (clause → tactic (list clause))) : list clause → tactic (list clause) | cs := lift list.join $ do for cs $ λc, do first $ rules.map (λr, r c >>= get_clauses_core') ++ [return [c]] meta def get_clauses_core (rules : list (clause → tactic (list clause))) (initial : list clause) : tactic (list clause) := do clauses ← get_clauses_core' rules initial, filter (λc, lift bnot $ is_taut c) $ list.nub_on clause.type clauses meta def clausification_rules_intuit : list (clause → tactic (list clause)) := [ inf_false_l, inf_false_r, inf_true_l, inf_true_r, inf_not_l, inf_not_r, inf_and_l, inf_and_r, inf_iff_l, inf_iff_r, inf_or_l, inf_or_r, inf_ex_l, inf_normalize_l, inf_normalize_r ] meta def clausification_rules_classical : list (clause → tactic (list clause)) := [ inf_false_l, inf_false_r, inf_true_l, inf_true_r, inf_not_l, inf_not_r, inf_and_l, inf_and_r, inf_iff_l, inf_iff_r, inf_or_l, inf_or_r, inf_imp_l, inf_all_r, inf_ex_l, inf_all_l, inf_ex_r, inf_normalize_l, inf_normalize_r ] meta def get_clauses_classical : list clause → tactic (list clause) := get_clauses_core clausification_rules_classical meta def get_clauses_intuit : list clause → tactic (list clause) := get_clauses_core clausification_rules_intuit meta def as_refutation : tactic unit := do repeat (do intro1, skip), tgt ← target, if tgt.is_constant || tgt.is_local_constant then skip else do local_false_name ← get_unused_name `F none, tgt_type ← infer_type tgt, definev local_false_name tgt_type tgt, local_false ← get_local local_false_name, target_name ← get_unused_name `target none, assertv target_name (imp tgt local_false) (lam `hf binder_info.default tgt $ mk_var 0), change local_false meta def clauses_of_context : tactic (list clause) := do local_false ← target, l ← local_context, monad.for l (clause.of_proof local_false) meta def clausify_pre := preprocessing_rule $ take new, lift list.join $ for new $ λ dc, do cs ← get_clauses_classical [dc.c], if cs.length ≤ 1 then return (for cs $ λ c, { dc with c := c }) else for cs (λc, mk_derived c dc.sc) -- @[super.inf] meta def clausification_inf : inf_decl := inf_decl.mk 0 $ λ given, list.foldr (<|>) (return ()) $ do r ← clausification_rules_classical, [do cs ← r given.c, cs' ← get_clauses_classical cs, for' cs' (λc, mk_derived c given.sc.sched_now >>= add_inferred), remove_redundant given.id []] end super
a97665c92694bae5645a013b1f8d8566a979ba6c
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/data/finsupp/basic.lean
816a0e308019549ee6dee4a1dad68b989074fc9b
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
78,763
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, Scott Morrison -/ import algebra.group.pi import algebra.big_operators.order import algebra.module.basic import group_theory.submonoid.basic import data.fintype.card import data.finset.preimage import data.multiset.antidiagonal /-! # Type of functions with finite support For any type `α` and a type `M` with zero, we define the type `finsupp α M` (notation: `α →₀ M`) of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere on `α` except on a finite set. Functions with finite support are used (at least) in the following parts of the library: * `monoid_algebra R M` and `add_monoid_algebra R M` are defined as `M →₀ R`; * polynomials and multivariate polynomials are defined as `add_monoid_algebra`s, hence they use `finsupp` under the hood; * the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to define linearly independent family `linear_independent`) is defined as a map `finsupp.total : (ι → M) → (ι →₀ R) →ₗ[R] M`. Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined in a different way in the library: * `multiset α ≃+ α →₀ ℕ`; * `free_abelian_group α ≃+ α →₀ ℤ`. Most of the theory assumes that the range is a commutative additive monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. Many constructions based on `α →₀ M` use `semireducible` type tags to avoid reusing unwanted type instances. E.g., `monoid_algebra`, `add_monoid_algebra`, and types based on these two have non-pointwise multiplication. ## Notations This file adds `α →₀ M` as a global notation for `finsupp α M`. We also use the following convention for `Type*` variables in this file * `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `finsupp` somewhere in the statement; * `ι` : an auxiliary index type; * `M`, `M'`, `N`, `P`: types with `has_zero` or `(add_)(comm_)monoid` structure; `M` is also used for a (semi)module over a (semi)ring. * `G`, `H`: groups (commutative or not, multiplicative or additive); * `R`, `S`: (semi)rings. ## TODO * This file is currently ~2K lines long, so possibly it should be splitted into smaller chunks; * Add the list of definitions and important lemmas to the module docstring. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `α →₀ β` as notation for `finsupp α β`. -/ noncomputable theory open_locale classical big_operators open finset variables {α β γ ι M M' N P G H R S : Type*} /-- `finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (M : Type*) [has_zero M] := (support : finset α) (to_fun : α → M) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero M] instance : has_coe_to_fun (α →₀ M) := ⟨λ _, α → M, to_fun⟩ @[simp] lemma coe_mk (f : α → M) (s : finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) : ⇑(⟨s, f, h⟩ : α →₀ M) = f := rfl instance : has_zero (α →₀ M) := ⟨⟨∅, (λ _, 0), λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma coe_zero : ⇑(0 : α →₀ M) = (λ _, (0:M)) := rfl lemma zero_apply {a : α} : (0 : α →₀ M) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ M).support = ∅ := rfl instance : inhabited (α →₀ M) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ M} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm lemma injective_coe_fn : function.injective (show (α →₀ M) → α → M, from coe_fn) | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin change f = g at h, subst h, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end @[ext] lemma ext {f g : α →₀ M} (h : ∀a, f a = g a) : f = g := injective_coe_fn (funext h) lemma ext_iff {f g : α →₀ M} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ lemma ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x := ⟨λ h, h ▸ ⟨rfl, λ _ _, rfl⟩, λ ⟨h₁, h₂⟩, ext $ λ a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, from not_mem_support_iff.1 h, have hg : g a = 0, by rwa [h₁, not_mem_support_iff] at h, by rw [hf, hg]⟩ @[simp] lemma support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ λ H, (finset.ext_iff.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ lemma card_support_eq_zero {f : α →₀ M} : card f.support = 0 ↔ f = 0 := by simp instance finsupp.decidable_eq [decidable_eq α] [decidable_eq M] : decidable_eq (α →₀ M) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ext_iff'.symm lemma finite_supp (f : α →₀ M) : set.finite {a | f a ≠ 0} := ⟨fintype.of_finset f.support (λ _, mem_support_iff)⟩ lemma support_subset_iff {s : set α} {f : α →₀ M} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, not_imp_comm) /-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ def equiv_fun_on_fintype [fintype α] : (α →₀ M) ≃ (α → M) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic /-! ### Declarations about `single` -/ section single variables [has_zero M] {a a' : α} {b : M} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : M) : α →₀ M := ⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : α →₀ M) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : α →₀ M) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ M) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : α →₀ M) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma single_injective (a : α) : function.injective (single a : M → α →₀ M) := assume b₁ b₂ eq, have (single a b₁ : α →₀ M) a = (single a b₂ : α →₀ M) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma mem_support_single (a a' : α) (b : M) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, λ ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ lemma eq_single_iff {f : α →₀ M} {a b} : f = single a b ↔ f.support ⊆ {a} ∧ f a = b := begin refine ⟨λ h, h.symm ▸ ⟨support_single_subset, single_eq_same⟩, _⟩, rintro ⟨h, rfl⟩, ext x, by_cases hx : a = x; simp only [hx, single_eq_same, single_eq_of_ne, ne.def, not_false_iff], exact not_mem_support_iff.1 (mt (λ hx, (mem_singleton.1 (h hx)).symm) hx) end lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : M) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (single_injective a₂).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := ⟨λ H, by simpa only [h, single_eq_single_iff, and_false, or_false, eq_self_iff_true, and_true] using H, λ H, by rw [H]⟩ lemma single_eq_zero : single a b = 0 ↔ b = 0 := ⟨λ h, by { rw ext_iff at h, simpa only [single_eq_same, zero_apply] using h a }, λ h, by rw [h, single_zero]⟩ lemma single_swap (a₁ a₂ : α) (b : M) : single a₁ b a₂ = single a₂ b a₁ := by simp only [single_apply]; ac_refl instance [nonempty α] [nontrivial M] : nontrivial (α →₀ M) := begin inhabit α, rcases exists_ne (0 : M) with ⟨x, hx⟩, exact nontrivial_of_ne (single (default α) x) 0 (mt single_eq_zero.1 hx) end lemma unique_single [unique α] (x : α →₀ M) : x = single (default α) (x (default α)) := ext $ unique.forall_iff.2 single_eq_same.symm lemma unique_ext [unique α] {f g : α →₀ M} (h : f (default α) = g (default α)) : f = g := ext $ λ a, by rwa [unique.eq_default a] lemma unique_ext_iff [unique α] {f g : α →₀ M} : f = g ↔ f (default α) = g (default α) := ⟨λ h, h ▸ rfl, unique_ext⟩ @[simp] lemma unique_single_eq_iff [unique α] {b' : M} : single a b = single a' b' ↔ b = b' := by rw [unique_ext_iff, unique.eq_default a, unique.eq_default a', single_eq_same, single_eq_same] lemma support_eq_singleton {f : α →₀ M} {a : α} : f.support = {a} ↔ f a ≠ 0 ∧ f = single a (f a) := ⟨λ h, ⟨mem_support_iff.1 $ h.symm ▸ finset.mem_singleton_self a, eq_single_iff.2 ⟨subset_of_eq h, rfl⟩⟩, λ h, h.2.symm ▸ support_single_ne_zero h.1⟩ lemma support_eq_singleton' {f : α →₀ M} {a : α} : f.support = {a} ↔ ∃ b ≠ 0, f = single a b := ⟨λ h, let h := support_eq_singleton.1 h in ⟨_, h.1, h.2⟩, λ ⟨b, hb, hf⟩, hf.symm ▸ support_single_ne_zero hb⟩ lemma card_support_eq_one {f : α →₀ M} : card f.support = 1 ↔ ∃ a, f a ≠ 0 ∧ f = single a (f a) := by simp only [card_eq_one, support_eq_singleton] lemma card_support_eq_one' {f : α →₀ M} : card f.support = 1 ↔ ∃ a (b ≠ 0), f = single a b := by simp only [card_eq_one, support_eq_singleton'] end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero M] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset α) (f : α → M) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ M := ⟨s.filter (λa, f a ≠ 0), f, by simpa⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → M} {hf a} : (on_finset s f hf : α →₀ M) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → M} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ _ @[simp] lemma mem_support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) {a : α} : a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 := by rw [finsupp.mem_support_iff, finsupp.on_finset_apply] lemma support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) : (finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) := rfl end on_finset /-! ### Declarations about `map_range` -/ section map_range variables [has_zero M] [has_zero N] /-- The composition of `f : M → N` and `g : α →₀ M` is `map_range f hf g : α →₀ N`, well-defined when `f 0 = 0`. -/ def map_range (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : M → N} {hf : f 0 = 0} : map_range f hf (0 : α →₀ M) = 0 := ext $ λ a, by simp only [hf, zero_apply, map_range_apply] lemma support_map_range {f : M → N} {hf : f 0 = 0} {g : α →₀ M} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : M → N} {hf : f 0 = 0} {a : α} {b : M} : map_range f hf (single a b) = single a (f b) := ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero M] [has_zero N] /-- Given `f : α ↪ β` and `v : α →₀ M`, `emb_domain f v : β →₀ M` is the finitely supported function whose value at `f a : β` is `v a`. For a `b : β` outside the range of `f`, it is zero. -/ def emb_domain (f : α ↪ β) (v : α →₀ M) : β →₀ M := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) }, { assume a₂, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end @[simp] lemma support_emb_domain (f : α ↪ β) (v : α →₀ M) : (emb_domain f v).support = v.support.map f := rfl @[simp] lemma emb_domain_zero (f : α ↪ β) : (emb_domain f 0 : β →₀ M) = 0 := rfl @[simp] lemma emb_domain_apply (f : α ↪ β) (v : α →₀ M) (a : α) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α → M) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α ↪ β) (v : α →₀ M) (a : β) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_injective (f : α ↪ β) : function.injective (emb_domain f : (α →₀ M) → (β →₀ M)) := λ l₁ l₂ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a) @[simp] lemma emb_domain_inj {f : α ↪ β} {l₁ l₂ : α →₀ M} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := (emb_domain_injective f).eq_iff @[simp] lemma emb_domain_eq_zero {f : α ↪ β} {l : α →₀ M} : emb_domain f l = 0 ↔ l = 0 := (emb_domain_injective f).eq_iff' $ emb_domain_zero f lemma emb_domain_map_range (f : α ↪ β) (g : M → N) (p : α →₀ M) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α →₀ M) (f : α ↪ β) (a : β) (b : M) (hb : b ≠ 0) (h : l.emb_domain f = single a b) : ∃ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = {a}, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hc₂, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.injective (hc₂.trans hfd)) } }, { exact hc₂ } end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero M] [has_zero N] [has_zero P] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : M → N → P) (hf : f 0 0 = 0) (g₁ : α →₀ M) (g₂ : α →₀ N) : (α →₀ P) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase variables [has_zero M] /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase (a : α) (f : α →₀ M) : α →₀ M := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase {a : α} {f : α →₀ M} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same {a : α} {f : α →₀ M} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne {a a' : α} {f : α →₀ M} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single {a : α} {b : M} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne {a a' : α} {b : M} (h : a ≠ a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end @[simp] lemma erase_zero (a : α) : erase a (0 : α →₀ M) = 0 := by rw [← support_eq_empty, support_erase, support_zero, erase_empty] end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ section sum_prod -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero M] [add_comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∑ a in f.support, g a (f a) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero M] [comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∏ a in f.support, g a (f a) variables [has_zero M] [has_zero M'] [comm_monoid N] @[to_additive] lemma prod_of_support_subset (f : α →₀ M) {s : finset α} (hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x in s, g x (f x) := finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx @[to_additive] lemma prod_fintype [fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g (λ x _, h x) @[simp, to_additive] lemma prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x in {a}, h x (single a b x) : prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero ... = h a b : by simp @[to_additive] lemma prod_map_range_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[simp, to_additive] lemma prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl @[to_additive] lemma prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := finset.prod_comm @[simp, to_additive] lemma prod_ite_eq (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma prod_pow [fintype α] (f : α →₀ ℕ) (g : α → N) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := f.prod_fintype _ $ λ a, pow_zero _ /-- If `g` maps a second argument of 0 to 1, then multiplying it over the result of `on_finset` is the same as multiplying it over the original `finset`. -/ @[to_additive "If `g` maps a second argument of 0 to 0, summing it over the result of `on_finset` is the same as summing it over the original `finset`."] lemma on_finset_prod {s : finset α} {f : α → M} {g : α → M → N} (hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) : (on_finset s f hf).prod g = ∏ a in s, g a (f a) := finset.prod_subset support_on_finset_subset $ by simp [*] { contextual := tt } end sum_prod /-! ### Additive monoid structure on `α →₀ M` -/ section add_monoid variables [add_monoid M] instance : has_add (α →₀ M) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ M} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ M} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq {g₁ g₂ : α →₀ M} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : M} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ M) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } /-- `finsupp.single` as an `add_monoid_hom`. See `finsupp.lsingle` for the stronger version as a linear map. -/ @[simps] def single_add_hom (a : α) : M →+ α →₀ M := ⟨single a, single_zero, λ _ _, single_add⟩ /-- Evaluation of a function `f : α →₀ M` at a point as an additive monoid homomorphism. See `finsupp.lapply` for the stronger version as a linear map. -/ @[simps apply] def apply_add_hom (a : α) : (α →₀ M) →+ M := ⟨λ g, g a, zero_apply, λ _ _, add_apply⟩ lemma single_add_erase (a : α) (f : α →₀ M) : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single (a : α) (f : α →₀ M) : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : α) (f f' : α →₀ M) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end @[simp] lemma add_closure_Union_range_single : add_submonoid.closure (⋃ a : α, set.range (single a : M → α →₀ M)) = ⊤ := top_unique $ λ x hx, finsupp.induction x (add_submonoid.zero_mem _) $ λ a b f ha hb hf, add_submonoid.add_mem _ (add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. -/ lemma add_hom_ext [add_monoid N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x y, f (single x y) = g (single x y)) : f = g := begin refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _), simp only [set.mem_Union, set.mem_range] at hf, rcases hf with ⟨x, y, rfl⟩, apply H end /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. We formulate this using equality of `add_monoid_hom`s so that `ext` tactic can apply a type-specific extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to verify `f (single a 1) = g (single a 1)`. -/ @[ext] lemma add_hom_ext' [add_monoid N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x, f.comp (single_add_hom x) = g.comp (single_add_hom x)) : f = g := add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x) lemma mul_hom_ext [monoid N] ⦃f g : multiplicative (α →₀ M) →* N⦄ (H : ∀ x y, f (multiplicative.of_add $ single x y) = g (multiplicative.of_add $ single x y)) : f = g := monoid_hom.ext $ add_monoid_hom.congr_fun $ @add_hom_ext α M (additive N) _ _ f.to_additive'' g.to_additive'' H @[ext] lemma mul_hom_ext' [monoid N] {f g : multiplicative (α →₀ M) →* N} (H : ∀ x, f.comp (single_add_hom x).to_multiplicative = g.comp (single_add_hom x).to_multiplicative) : f = g := mul_hom_ext $ λ x, monoid_hom.congr_fun (H x) lemma map_range_add [add_monoid N] {f : M → N} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ M) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := ext $ λ a, by simp only [hf', add_apply, map_range_apply] end add_monoid section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ g₂ : α →₀ ℕ} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma single_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end end nat_sub instance [add_comm_monoid M] : add_comm_monoid (α →₀ M) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group G] : add_group (α →₀ G) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } instance [add_comm_group G] : add_comm_group (α →₀ G) := { add_comm := add_comm, ..finsupp.add_group } lemma single_multiset_sum [add_comm_monoid M] (s : multiset M) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid M] (s : finset ι) (f : ι → M) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero M] [add_comm_monoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group G] [comm_monoid M] {g : α →₀ G} {h : α → G → M} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group G] {g : α →₀ G} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group G] {g₁ g₂ : α →₀ G} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group G] {f : α →₀ G} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) @[simp] lemma sum_apply [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (apply_add_hom a₂ : (β →₀ N) →+ _).map_sum _ _ lemma support_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → (β →₀ N)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀ c, f.sum (λ a b, g a b c) ≠ 0 → (∃ a, f a ≠ 0 ∧ ¬ (g a (f a)) c = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] @[simp] lemma sum_zero [has_zero M] [add_comm_monoid N] {f : α →₀ M} : f.sum (λa b, (0 : N)) = 0 := finset.sum_const_zero @[simp, to_additive] lemma prod_mul [has_zero M] [comm_monoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} : f.prod (λa b, h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ := finset.prod_mul_distrib @[simp, to_additive] lemma prod_inv [has_zero M] [comm_group G] {f : α →₀ M} {h : α → M → G} : f.prod (λa b, (h a b)⁻¹) = (f.prod h)⁻¹ := (((monoid_hom.id G)⁻¹).map_prod _ _).symm @[simp] lemma sum_sub [has_zero M] [add_comm_group G] {f : α →₀ M} {h₁ h₂ : α → M → G} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := finset.sum_sub_distrib @[to_additive] lemma prod_add_index [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have hf : f.prod h = ∏ a in f.support ∪ g.support, h a (f a), from f.prod_of_support_subset (subset_union_left _ _) _ $ λ a ha, h_zero a, have hg : g.prod h = ∏ a in f.support ∪ g.support, h a (g a), from g.prod_of_support_subset (subset_union_right _ _) _ $ λ a ha, h_zero a, have hfg : (f + g).prod h = ∏ a in f.support ∪ g.support, h a ((f + g) a), from (f + g).prod_of_support_subset support_add _ $ λ a ha, h_zero a, by simp only [*, add_apply, prod_mul_distrib] @[simp] lemma sum_add_index' [add_comm_monoid M] [add_comm_monoid N] {f g : α →₀ M} (h : α → M →+ N) : (f + g).sum (λ x, h x) = f.sum (λ x, h x) + g.sum (λ x, h x) := sum_add_index (λ a, (h a).map_zero) (λ a, (h a).map_add) @[simp] lemma prod_add_index' [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} (h : α → multiplicative M →* N) : (f + g).prod (λ a b, h a (multiplicative.of_add b)) = f.prod (λ a b, h a (multiplicative.of_add b)) * g.prod (λ a b, h a (multiplicative.of_add b)) := prod_add_index (λ a, (h a).map_one) (λ a, (h a).map_mul) /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)` and monoid homomorphisms `(α →₀ M) →+ N`. -/ def lift_add_hom [add_comm_monoid M] [add_comm_monoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) := { to_fun := λ F, { to_fun := λ f, f.sum (λ x, F x), map_zero' := finset.sum_empty, map_add' := λ _ _, sum_add_index (λ x, (F x).map_zero) (λ x, (F x).map_add) }, inv_fun := λ F x, F.comp $ single_add_hom x, left_inv := λ F, by { ext, simp }, right_inv := λ F, by { ext, simp }, map_add' := λ F G, by { ext, simp } } @[simp] lemma lift_add_hom_apply [add_comm_monoid M] [add_comm_monoid N] (F : α → M →+ N) (f : α →₀ M) : lift_add_hom F f = f.sum (λ x, F x) := rfl @[simp] lemma lift_add_hom_symm_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) : lift_add_hom.symm F x = F.comp (single_add_hom x) := rfl lemma lift_add_hom_symm_apply_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) (y : M) : lift_add_hom.symm F x y = F (single x y) := rfl @[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid M] : lift_add_hom (single_add_hom : α → M →+ α →₀ M) = add_monoid_hom.id _ := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma sum_single [add_comm_monoid M] (f : α →₀ M) : f.sum single = f := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f @[simp] lemma lift_add_hom_apply_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) (b : M) : lift_add_hom f (single a b) = f a b := sum_single_index (f a).map_zero @[simp] lemma lift_add_hom_comp_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) : (lift_add_hom f).comp (single_add_hom a) = f a := add_monoid_hom.ext $ λ b, lift_add_hom_apply_single f a b lemma comp_lift_add_hom [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] (g : N →+ P) (f : α → M →+ N) : g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) := lift_add_hom.symm_apply_eq.1 $ funext $ λ a, by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g @[to_additive] lemma prod_emb_domain [has_zero M] [comm_monoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} : (v.emb_domain f).prod g = v.prod (λ a b, g (f a) b) := begin rw [prod, prod, support_emb_domain, finset.prod_map], simp_rw emb_domain_apply, end @[to_additive] lemma prod_finset_sum_index [add_comm_monoid M] [comm_monoid N] {s : finset ι} {g : ι → α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid M] [add_comm_monoid N] [comm_monoid P] {f : α →₀ M} {g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid M] [add_comm_monoid N] (f : multiset (α →₀ M)) (h : α → M → N) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ M, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma multiset_map_sum [has_zero M] {f : α →₀ M} {m : β → γ} {h : α → M → multiset β} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (f.support.sum_hom _).symm lemma multiset_sum_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {h : α → M → multiset N} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (f.support.sum_hom multiset.sum).symm section map_range variables [add_comm_monoid M] [add_comm_monoid N] (f : M →+ N) /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ def map_range.add_monoid_hom : (α →₀ M) →+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero, map_add' := λ a b, map_range_add f.map_add _ _ } lemma map_range_multiset_sum (m : multiset (α →₀ M)) : map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum := (m.sum_hom (map_range.add_monoid_hom f)).symm lemma map_range_finset_sum (s : finset ι) (g : ι → (α →₀ M)) : map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid M] {v v₁ v₂ : α →₀ M} /-- Given `f : α → β` and `v : α →₀ M`, `map_domain f v : β →₀ M` is the finitely supported function whose value at `a : β` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α → β) (v : α →₀ M) : β →₀ M := v.sum $ λa, single (f a) lemma map_domain_apply {f : α → β} (hf : function.injective f) (x : α →₀ M) (a : α) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] } end lemma map_domain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → β} {g : β → γ} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : α → β} {a : α} {b : M} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → β} : map_domain f (0 : α →₀ M) = (0 : β →₀ M) := sum_zero_index lemma map_domain_congr {f g : α → β} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → β} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_finset_sum {f : α → β} {s : finset ι} {v : ι → α →₀ M} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_sum [has_zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_support {f : α → β} {s : α →₀ M} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (map_domain f s).prod h = s.prod (λa b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α ↪ β) (v : α →₀ M) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end @[to_additive] lemma prod_map_domain_index_inj [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (hf : function.injective f) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := by rw [←function.embedding.coe_fn_mk f hf, ←emb_domain_eq_map_domain, prod_emb_domain] lemma map_domain_injective {f : α → β} (hf : function.injective f) : function.injective (map_domain f : (α →₀ M) → (β →₀ M)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α` to `M` given by composing `l` with `f`. -/ def comap_domain [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α →₀ M := { support := l.support.preimage f hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain [has_zero M] [add_comm_monoid N] (f : α → β) (l : β →₀ M) (g : β → M → N) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp only [sum, comap_domain_apply, (∘)], simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ x, g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end comap_domain /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero M] (p : α → Prop) (f : α →₀ M) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ M) : α →₀ M := on_finset f.support (λa, if p a then f a else 0) $ λ a H, mem_support_iff.2 $ λ h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : α →₀ M).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : M} (h : p a) : (single a b).filter p = single a b := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos] }, { simp only [h', filter_apply_neg, not_false_iff], rw single_eq_of_ne, rintro rfl, exact h' h } end @[simp] lemma filter_single_of_neg {a : α} {b : M} (h : ¬ p a) : (single a b).filter p = 0 := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos, zero_apply], rw single_eq_of_ne, rintro rfl, exact h h' }, { simp only [h', finsupp.zero_apply, not_false_iff, filter_apply_neg] } end end has_zero lemma filter_pos_add_filter_neg [add_monoid M] (f : α →₀ M) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero M] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ M) : finset M := finset.image f f.support theorem mem_frange {f : α →₀ M} {y : M} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ M} : (0:M) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain section zero variables [has_zero M] {p : α → Prop} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ M) : (subtype p →₀ M) := ⟨f.support.subtype p, f ∘ coe, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : α →₀ M} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ M} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ M) = 0 := rfl lemma subtype_domain_eq_zero_iff' {f : α →₀ M} : f.subtype_domain p = 0 ↔ ∀ x, p x → f x = 0 := by simp_rw [← support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff] lemma subtype_domain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support , p x) : f.subtype_domain p = 0 ↔ f = 0 := subtype_domain_eq_zero_iff'.trans ⟨λ H, ext $ λ x, if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, λ H x _, by simp [H]⟩ @[to_additive] lemma prod_subtype_domain_index [comm_monoid N] {v : α →₀ M} {h : α → M → N} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid M] {p : α → Prop} {v v' : α →₀ M} @[simp] lemma subtype_domain_add {v v' : α →₀ M} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl instance subtype_domain.is_add_monoid_hom : is_add_monoid_hom (subtype_domain p : (α →₀ M) → subtype p →₀ M) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p := ext $ λ a, begin by_cases p a, { simp only [h, filter_apply_pos, add_apply] }, { simp only [h, add_zero, add_apply, not_false_iff, filter_apply_neg] } end instance filter.is_add_monoid_hom (p : α → Prop) : is_add_monoid_hom (filter p : (α →₀ M) → (α →₀ M)) := { map_zero := filter_zero p, map_add := λ x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid M] {p : α → Prop} lemma subtype_domain_sum {s : finset ι} {h : ι → α →₀ M} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := eq.symm (s.sum_hom _) lemma subtype_domain_finsupp_sum [has_zero N] {s : β →₀ N} {h : β → N → α →₀ M} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset ι) (f : ι → α →₀ M) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (s.sum_hom (filter p)).symm end comm_monoid section group variables [add_group G] {p : α → Prop} {v v' : α →₀ G} @[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. -/ def to_multiset (f : α →₀ ℕ) : multiset α := f.sum (λa n, n •ℕ {a}) lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, zero_nsmul _) (assume a b₁ b₂, add_nsmul _ _ _) lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n •ℕ {a} := by rw [to_multiset, sum_single_index]; apply zero_nsmul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ → multiset α) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_nsmul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid M] (f : M →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_disjoint] } end @[simp] lemma count_to_multiset (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n •ℕ {x} : multiset α).count a) : (f.support.sum_hom $ multiset.count a).symm ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_smul] ... = f.sum (λx n, n * (x ::ₘ 0 : multiset α).count a) : rfl ... = f a * (a ::ₘ 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] /-- Given `m : multiset α`, `of_multiset m` is the finitely supported function from `α` to `ℕ` given by the multiplicities of the elements of `α` in `m`. -/ def of_multiset (m : multiset α) : α →₀ ℕ := on_finset m.to_finset (λa, m.count a) $ λ a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply (m : multiset α) (a : α) : of_multiset m a = m.count a := rfl /-- `equiv_multiset` defines an `equiv` between finitely supported functions from `α` to `ℕ` and multisets on `α`. -/ def equiv_multiset : (α →₀ ℕ) ≃ (multiset α) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ λ a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ λ a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [add_comm_monoid M] {s : multiset (α →₀ M)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ M).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid M] {s : finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃ c ∈ s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ @[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) : i ∈ f.to_multiset ↔ i ∈ f.support := by rw [← multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff] end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry variables [add_comm_monoid M] [add_comm_monoid N] /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry (f : (α × β) →₀ M) : α →₀ (β →₀ M) := f.sum $ λp c, single p.1 (single p.2 c) lemma sum_curry_index (f : (α × β) →₀ M) (g : α → β → M → N) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `M`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/ protected def uncurry (f : α →₀ (β →₀ M)) : (α × β) →₀ M := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by currying and uncurrying. -/ def finsupp_prod_equiv : ((α × β) →₀ M) ≃ (α →₀ (β →₀ M)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry (f : α × β →₀ M) (p : α → Prop) : (f.filter (λa:α×β, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry (f : α × β →₀ M) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [group G] [mul_action G α] [add_comm_monoid M] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar G (α →₀ M) := { smul := λ g f, f.comap_domain (λ a, g⁻¹ • a) (λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action G (α →₀ M) := { one_smul := λ f, by { ext, dsimp [(•)], simp, }, mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action G (α →₀ M) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action G (G →₀ M) := @finsupp.comap_distrib_mul_action G M G _ (mul_action.regular G) _ @[simp] lemma comap_smul_single (g : G) (a : α) (b : M) : g • single a b = single (g • a) b := begin ext a', dsimp [(•)], by_cases h : g • a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : G) (f : α →₀ M) (a : α) : (g • f) a = f (g⁻¹ • a) := rfl end section instance [semiring R] [add_comm_monoid M] [semimodule R M] : has_scalar R (α →₀ M) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ variables (α M) /-! Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ @[simp] lemma smul_apply' {_:semiring R} [add_comm_monoid M] [semimodule R M] {a : α} {b : R} {v : α →₀ M} : (b • v) a = b • (v a) := rfl instance [semiring R] [add_comm_monoid M] [semimodule R M] : semimodule R (α →₀ M) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, zero_smul := λ x, ext $ λ _, zero_smul _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } variables {α M} {R} lemma support_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M] {b : R} {g : α →₀ M} : (b • g).support ⊆ g.support := λ a, by simp only [smul_apply', mem_support_iff, ne.def]; exact mt (λ h, h.symm ▸ smul_zero _) section variables {p : α → Prop} @[simp] lemma filter_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M] {b : R} {v : α →₀ M} : (b • v).filter p = b • v.filter p := ext $ λ a, begin by_cases p a, { simp only [h, smul_apply', filter_apply_pos] }, { simp only [h, smul_apply', not_false_iff, filter_apply_neg, smul_zero] } end end lemma map_domain_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M] {f : α → β} (b : R) (v : α →₀ M) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {_ : semiring R} [add_comm_monoid M] [semimodule R M] (c : R) (a : α) (b : M) : c • finsupp.single a b = finsupp.single a (c • b) := ext $ λ a', by by_cases a = a'; [{ subst h, simp only [smul_apply', single_eq_same] }, simp only [h, smul_apply', ne.def, not_false_iff, single_eq_of_ne, smul_zero]] @[simp] lemma smul_single' {_ : semiring R} (c : R) (a : α) (b : R) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ lemma smul_single_one [semiring R] (a : α) (b : R) : b • single a 1 = single a b := by rw [smul_single, smul_eq_mul, mul_one] end @[simp] lemma smul_apply [semiring R] {a : α} {b : R} {v : α →₀ R} : (b • v) a = b • (v a) := rfl lemma sum_smul_index [semiring R] [add_comm_monoid M] {g : α →₀ R} {b : R} {h : α → R → M} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 lemma sum_smul_index' [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid N] {g : α →₀ M} {b : R} {h : α → M → N} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi c, h i (b • c)) := finsupp.sum_map_range_index h0 section variables [semiring R] [semiring S] lemma sum_mul (b : S) (s : α →₀ R) {f : α → R → S} : (s.sum f) * b = s.sum (λ a c, (f a c) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : S) (s : α →₀ R) {f : α → R → S} : b * (s.sum f) = s.sum (λ a c, b * (f a c)) := by simp only [finsupp.sum, finset.mul_sum] instance unique_of_right [subsingleton R] : unique (α →₀ R) := { uniq := λ l, ext $ λ i, subsingleton.elim _ _, .. finsupp.inhabited } end /-- Given an `add_comm_monoid M` and `s : set α`, `restrict_support_equiv s M` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (M : Type*) [add_comm_monoid M] : {f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M):= begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid M` and `e : α ≃ β`, `dom_congr e` is the corresponding `equiv` between `α →₀ M` and `β →₀ M`. -/ protected def dom_congr [add_comm_monoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) := { to_fun := map_domain e, inv_fun := map_domain e.symm, left_inv := begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, right_inv := begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end, map_add' := λ a b, map_domain_add, } end finsupp @[to_additive] lemma mul_equiv.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N ≃* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma monoid_hom.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N →* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ lemma ring_hom.map_finsupp_sum [has_zero M] [semiring R] [semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) := h.map_sum _ _ lemma ring_hom.map_finsupp_prod [has_zero M] [comm_semiring R] [comm_semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ namespace finsupp /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero M] (l : (Σ i, αs i) →₀ M) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `M`. -/ def split (i : ι) : αs i →₀ M := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support : finset ι := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero N] (g : Π i, (αs i →₀ M) → N) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid N] (f : (Σ (i : ι), αs i) → M → N) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp (s : multiset α) : α →₀ ℕ := { support := s.to_finset, to_fun := λ a, s.count a, mem_support_to_fun := λ a, begin rw mem_to_finset, convert not_iff_not_of_iff (count_eq_zero.symm), rw not_not end } @[simp] lemma to_finsupp_support (s : multiset α) : s.to_finsupp.support = s.to_finset := rfl @[simp] lemma to_finsupp_apply (s : multiset α) (a : α) : s.to_finsupp a = s.count a := rfl @[simp] lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := finsupp.ext $ λ a, count_zero a @[simp] lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := finsupp.ext $ λ a, count_add a s t lemma to_finsupp_singleton (a : α) : to_finsupp {a} = finsupp.single a 1 := finsupp.ext $ λ b, if h : a = b then by rw [to_finsupp_apply, finsupp.single_apply, h, if_pos rfl, singleton_eq_singleton, count_singleton] else begin rw [to_finsupp_apply, finsupp.single_apply, if_neg h, count_eq_zero, singleton_eq_singleton, mem_singleton], rintro rfl, exact h rfl end namespace to_finsupp instance : is_add_monoid_hom (to_finsupp : multiset α → α →₀ ℕ) := { map_zero := to_finsupp_zero, map_add := to_finsupp_add } end to_finsupp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := ext.2 $ λ a, by rw [finsupp.count_to_multiset, to_finsupp_apply] end multiset /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp instance [preorder M] [has_zero M] : preorder (α →₀ M) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order M] [has_zero M] : partial_order (α →₀ M) := { le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid M] : add_left_cancel_semigroup (α →₀ M) := { add_left_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_left_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid M] : add_right_cancel_semigroup (α →₀ M) := { add_right_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_right_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid M] : ordered_cancel_add_comm_monoid (α →₀ M) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order, .. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup } lemma le_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f + g = 0 ↔ f = 0 ∧ g = 0 := begin split, { assume h, split, all_goals { ext s, suffices H : f s + g s = 0, { rw add_eq_zero_iff at H, cases H, assumption }, show (f + g) s = 0, rw h, refl } }, { rintro ⟨rfl, rfl⟩, rw add_zero } end attribute [simp] to_multiset_zero to_multiset_add @[simp] lemma to_multiset_to_finsupp (f : α →₀ ℕ) : f.to_multiset.to_finsupp = f := ext $ λ s, by rw [multiset.to_finsupp_apply, count_to_multiset] lemma to_multiset_strict_mono : strict_mono (@to_multiset α) := λ m n h, begin rw lt_iff_le_and_ne at h ⊢, cases h with h₁ h₂, split, { rw multiset.le_iff_count, intro s, erw [count_to_multiset m s, count_to_multiset], exact h₁ s }, { intro H, apply h₂, replace H := congr_arg multiset.to_finsupp H, simpa only [to_multiset_to_finsupp] using H } end lemma sum_id_lt_of_lt (m n : α →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (α) /-- The order on `σ →₀ ℕ` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (α →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf instance decidable_le : decidable_rel (@has_le.le (α →₀ ℕ) _) := λ m n, by rw le_iff; apply_instance variable {α} /-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of `s : α →₀ ℕ` consists of all pairs `(t₁, t₂) : (α →₀ ℕ) × (α →₀ ℕ)` such that `t₁ + t₂ = s`. The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/ def antidiagonal (f : α →₀ ℕ) : ((α →₀ ℕ) × (α →₀ ℕ)) →₀ ℕ := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp lemma mem_antidiagonal_support {f : α →₀ ℕ} {p : (α →₀ ℕ) × (α →₀ ℕ)} : p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f := begin erw [multiset.mem_to_finset, multiset.mem_map], split, { rintros ⟨⟨a, b⟩, h, rfl⟩, rw multiset.mem_antidiagonal at h, simpa only [to_multiset_to_finsupp, multiset.to_finsupp_add] using congr_arg multiset.to_finsupp h}, { intro h, refine ⟨⟨p.1.to_multiset, p.2.to_multiset⟩, _, _⟩, { simpa only [multiset.mem_antidiagonal, to_multiset_add] using congr_arg to_multiset h}, { rw [prod.map, to_multiset_to_finsupp, to_multiset_to_finsupp, prod.mk.eta] } } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : α →₀ ℕ) = single (0,0) 1 := by rw [← multiset.to_finsupp_singleton]; refl lemma swap_mem_antidiagonal_support {n : α →₀ ℕ} {f} (hf : f ∈ (antidiagonal n).support) : f.swap ∈ (antidiagonal n).support := by simpa only [mem_antidiagonal_support, add_comm, prod.swap] using hf /-- Let `n : α →₀ ℕ` be a finitely supported function. The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`, is a finite set. -/ lemma finite_le_nat (n : α →₀ ℕ) : set.finite {m | m ≤ n} := begin let I := {i // i ∈ n.support}, let k : ℕ := ∑ i in n.support, n i, let f : (α →₀ ℕ) → (I → fin (k + 1)) := λ m i, m i, have hf : ∀ m ≤ n, ∀ i, (f m i : ℕ) = m i, { intros m hm i, apply fin.coe_coe_of_lt, calc m i ≤ n i : hm i ... < k + 1 : nat.lt_succ_iff.mpr (single_le_sum (λ _ _, nat.zero_le _) i.2) }, have f_im : set.finite (f '' {m | m ≤ n}) := set.finite.of_fintype _, suffices f_inj : set.inj_on f {m | m ≤ n}, { exact set.finite_of_finite_image f_inj f_im }, intros m₁ h₁ m₂ h₂ h, ext i, by_cases hi : i ∈ n.support, { replace h := congr_fun h ⟨i, hi⟩, rwa [fin.ext_iff, hf m₁ h₁, hf m₂ h₂] at h }, { rw not_mem_support_iff at hi, specialize h₁ i, specialize h₂ i, rw [hi, nat.le_zero_iff] at h₁ h₂, rw [h₁, h₂] } end /-- Let `n : α →₀ ℕ` be a finitely supported function. The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`, but not equal to `n` everywhere, is a finite set. -/ lemma finite_lt_nat (n : α →₀ ℕ) : set.finite {m | m < n} := (finite_le_nat n).subset $ λ m, le_of_lt end finsupp
e62bb90212be6457806ae9c4b0f9852ce749d8ec
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/module/default.lean
ea1a9eeb45081d7fee293c68715080b49e071570
[ "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
323
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 algebra.module.basic import algebra.module.submodule.basic /-! # Default file for module This file imports `algebra.module.basic` and `algebra.module.submodule`. -/
7722b47aec14416a88b730b244531efb58f896c2
33340b3a23ca62ef3c8a7f6a2d4e14c07c6d3354
/lia/wf.lean
f4e2a106aefbbddf23791b2610b26de0bce7a1e3
[]
no_license
lclem/cooper
79554e72ced343c64fed24b2d892d24bf9447dfe
812afc6b158821f2e7dac9c91d3b6123c7a19faf
refs/heads/master
1,607,554,257,488
1,578,694,133,000
1,578,694,133,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,713
lean
import data.list.basic .sqe def atom.wf : atom → Prop | (atom.le i ks) := true | (atom.dvd d i ks) := d ≠ 0 | (atom.ndvd d i ks) := d ≠ 0 instance atom.dec_wf : decidable_pred atom.wf | (atom.le i ks) := decidable.is_true trivial | (atom.dvd d i ks) := begin simp [atom.wf], apply_instance end | (atom.ndvd d i ks) := begin simp [atom.wf], apply_instance end lemma normal_iff_divisor_nonzero : ∀ {a : atom}, a.wf ↔ divisor a ≠ 0 := begin intro a, cases a; simp [atom.wf, divisor] end def formula.wf : formula → Prop | ⊤' := true | ⊥' := true | (A' a) := atom.wf a | (p ∧' q) := formula.wf p ∧ formula.wf q | (p ∨' q) := formula.wf p ∧ formula.wf q | (¬' p) := formula.wf p | (∃' p) := formula.wf p lemma wf_and_o {p q : formula} : formula.wf p → formula.wf q → formula.wf (and_o p q) := begin intros hp hq, apply cases_and_o; try {assumption}, trivial, constructor; assumption end lemma wf_or_o {p q : formula} : formula.wf p → formula.wf q → formula.wf (or_o p q) := begin intros hp hq, apply cases_or_o; try {assumption}, trivial, constructor; assumption end def formula.wf_alt (p : formula) := ∀ a ∈ p.atoms, atom.wf a lemma wf_iff_wf_alt : ∀ {p : formula}, formula.wf p ↔ formula.wf_alt p | ⊤' := begin constructor; intro h, intros a ha, cases ha, trivial end | ⊥' := begin constructor; intro h, intros a ha, cases ha, trivial end | (A' a) := begin unfold formula.wf, unfold formula.wf_alt, unfold formula.atoms, apply iff.intro; intro h, intros a' ha', cases ha' with he he, subst he, apply h, cases he, apply h, apply or.inl rfl end | (p ∧' q) := begin unfold formula.wf, repeat {rewrite wf_iff_wf_alt}, apply iff.symm, apply list.forall_mem_append end | (p ∨' q) := begin unfold formula.wf, repeat {rewrite wf_iff_wf_alt}, apply iff.symm, apply list.forall_mem_append end | (¬' p) := begin unfold formula.wf, rewrite wf_iff_wf_alt, refl end | (∃' p) := by {simp [formula.wf, wf_iff_wf_alt], refl} lemma wf_disj : ∀ {ps : list (formula)}, (∀ p ∈ ps, formula.wf p) → formula.wf (disj ps) | [] _ := trivial | (p::ps) h := have hp : formula.wf p := h _ (or.inl rfl), have hps : formula.wf (disj ps) := wf_disj (list.forall_mem_of_forall_mem_cons h), begin simp [disj], apply cases_or_o; try {simp [formula.wf], constructor}; {trivial <|> assumption} end lemma wf_conj : ∀ {ps : list (formula)}, (∀ p ∈ ps, formula.wf p) → formula.wf (conj ps) | [] _ := trivial | (p::ps) h := have hp : formula.wf p := h _ (or.inl rfl), have hps : formula.wf (conj ps) := wf_conj (list.forall_mem_of_forall_mem_cons h), begin simp [conj], apply cases_and_o; try {simp [formula.wf], constructor}; {trivial <|> assumption} end lemma wf_conj_atom : ∀ {ps : list atom}, (∀ p ∈ ps, atom.wf p) → formula.wf (conj_atom ps) | [] h := trivial | (a::as) h := begin simp [conj_atom], apply wf_and_o, apply (h _ (or.inl rfl)), apply wf_conj_atom, apply list.forall_mem_of_forall_mem_cons h end instance formula.dec_wf : decidable_pred formula.wf | ⊤' := decidable.is_true trivial | ⊥' := decidable.is_true trivial | (A' a) := begin unfold formula.wf, apply_instance end | (p ∧' q) := begin unfold formula.wf, apply @and.decidable _ _ _ _; apply formula.dec_wf end | (p ∨' q) := begin unfold formula.wf, apply @and.decidable _ _ _ _; apply formula.dec_wf end | (¬' p) := begin unfold formula.wf, apply formula.dec_wf end | (∃' p) := begin unfold formula.wf, apply formula.dec_wf end
ef230eadd4d488f6afa0b06813ce27ff21345e44
439bc6c3e74a118aa51df633b8e1f24415804d86
/sheaf.lean
c5547944fbbf2a101cb39b2a2336c89b25e15343
[]
no_license
jcommelin/lt2019_slides
4ca498db02b5187c5778c21b985126d52d260696
3234cd92920d3d4321cc2cef78b48e5fa55be413
refs/heads/master
1,586,718,101,957
1,546,930,855,000
1,546,930,855,000
162,697,592
1
0
null
null
null
null
UTF-8
Lean
false
false
98
lean
def sheaf (X : Type u) [𝒳 : site X] := { F : presheaf X // nonempty (site.sheaf_condition F) }
90e3aaab649e9ff555adf2c25152deb94a9f535c
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/support.lean
5a0c50daad0e083a1c71cc89c5c0b31f574f1fde
[ "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
5,273
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.conditionally_complete_lattice import algebra.big_operators.basic import algebra.group.prod /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. -/ universes u v w x y open set open_locale big_operators namespace function variables {α : Type u} {β : Type v} {ι : Sort w} {A : Type x} {B : Type y} /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} lemma nmem_support [has_zero A] {f : α → A} {x : α} : x ∉ support f ↔ f x = 0 := not_not lemma mem_support [has_zero A] {f : α → A} {x : α} : x ∈ support f ↔ f x ≠ 0 := iff.rfl lemma support_subset_iff [has_zero A] {f : α → A} {s : set α} : support f ⊆ s ↔ ∀ x, f x ≠ 0 → x ∈ s := iff.rfl lemma support_subset_iff' [has_zero A] {f : α → A} {s : set α} : support f ⊆ s ↔ ∀ x ∉ s, f x = 0 := forall_congr $ λ x, by classical; exact not_imp_comm lemma support_binop_subset [has_zero A] (op : A → A → A) (op0 : op 0 0 = 0) (f g : α → A) : support (λ x, op (f x) (g x)) ⊆ support f ∪ support g := λ x hx, classical.by_cases (λ hf : f x = 0, or.inr $ λ hg, hx $ by simp only [hf, hg, op0]) or.inl lemma support_add [add_monoid A] (f g : α → A) : support (λ x, f x + g x) ⊆ support f ∪ support g := support_binop_subset (+) (zero_add _) f g @[simp] lemma support_neg [add_group A] (f : α → A) : support (λ x, -f x) = support f := set.ext $ λ x, not_congr neg_eq_zero lemma support_sub [add_group A] (f g : α → A) : support (λ x, f x - g x) ⊆ support f ∪ support g := support_binop_subset (has_sub.sub) (sub_self _) f g @[simp] lemma support_mul [mul_zero_class A] [no_zero_divisors A] (f g : α → A) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [support, ne.def, mul_eq_zero, mem_set_of_eq, mem_inter_iff, not_or_distrib] @[simp] lemma support_inv [division_ring A] (f : α → A) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [division_ring A] (f g : α → A) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] lemma support_sup [has_zero A] [semilattice_sup A] (f g : α → A) : support (λ x, (f x) ⊔ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊔) sup_idem f g lemma support_inf [has_zero A] [semilattice_inf A] (f g : α → A) : support (λ x, (f x) ⊓ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊓) inf_idem f g lemma support_max [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, max (f x) (g x)) ⊆ support f ∪ support g := support_sup f g lemma support_min [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, min (f x) (g x)) ⊆ support f ∪ support g := support_inf f g lemma support_supr [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨆ i, f i x) ⊆ ⋃ i, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, simp only [hx, csupr_const] end lemma support_infi [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨅ i, f i x) ⊆ ⋃ i, support (f i) := @support_supr _ _ (order_dual A) ⟨(0:A)⟩ _ _ f lemma support_sum [add_comm_monoid A] (s : finset α) (f : α → β → A) : support (λ x, ∑ i in s, f i x) ⊆ ⋃ i ∈ s, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, exact finset.sum_eq_zero hx end lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) := λ x hx, mem_bInter_iff.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma support_comp_subset [has_zero A] [has_zero B] {g : A → B} (hg : g 0 = 0) (f : α → A) : support (g ∘ f) ⊆ support f := λ x, mt $ λ h, by simp [(∘), *] lemma support_subset_comp [has_zero A] [has_zero B] {g : A → B} (hg : ∀ {x}, g x = 0 → x = 0) (f : α → A) : support f ⊆ support (g ∘ f) := λ x, mt hg lemma support_comp_eq [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (g ∘ f) = support f := set.ext $ λ x, not_congr hg lemma support_prod_mk [has_zero A] [has_zero B] (f : α → A) (g : α → B) : support (λ x, (f x, g x)) = support f ∪ support g := set.ext $ λ x, by simp only [support, not_and_distrib, mem_union_eq, mem_set_of_eq, prod.mk_eq_zero, ne.def] end function
316cb1360d05bdc961d883edb10180fdec8daf4f
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/indicator_function.lean
60fcf87d3fdbc826353f773251eefdb59a2d285e
[ "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
21,472
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.support /-! # Indicator function - `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise. - `mul_indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `1` otherwise. ## Implementation note In mathematics, an indicator function or a characteristic function is a function used to indicate membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0` otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`. ## Tags indicator, characteristic -/ noncomputable theory open_locale classical big_operators open function variables {α β ι M N : Type*} namespace set section has_one variables [has_one M] [has_one N] {s t : set α} {f g : α → M} {a : α} /-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/ def indicator {M} [has_zero M] (s : set α) (f : α → M) : α → M := λ x, if x ∈ s then f x else 0 /-- `mul_indicator s f a` is `f a` if `a ∈ s`, `1` otherwise. -/ @[to_additive] def mul_indicator (s : set α) (f : α → M) : α → M := λ x, if x ∈ s then f x else 1 @[simp, to_additive] lemma piecewise_eq_mul_indicator : s.piecewise f 1 = s.mul_indicator f := rfl @[to_additive] lemma mul_indicator_apply (s : set α) (f : α → M) (a : α) : mul_indicator s f a = if a ∈ s then f a else 1 := rfl @[simp, to_additive] lemma mul_indicator_of_mem (h : a ∈ s) (f : α → M) : mul_indicator s f a = f a := if_pos h @[simp, to_additive] lemma mul_indicator_of_not_mem (h : a ∉ s) (f : α → M) : mul_indicator s f a = 1 := if_neg h @[to_additive] lemma mul_indicator_eq_one_or_self (s : set α) (f : α → M) (a : α) : mul_indicator s f a = 1 ∨ mul_indicator s f a = f a := if h : a ∈ s then or.inr (mul_indicator_of_mem h f) else or.inl (mul_indicator_of_not_mem h f) @[simp, to_additive] lemma mul_indicator_apply_eq_self : s.mul_indicator f a = f a ↔ (a ∉ s → f a = 1) := ite_eq_left_iff.trans $ by rw [@eq_comm _ (f a)] @[simp, to_additive] lemma mul_indicator_eq_self : s.mul_indicator f = f ↔ mul_support f ⊆ s := by simp only [funext_iff, subset_def, mem_mul_support, mul_indicator_apply_eq_self, not_imp_comm] @[to_additive] lemma mul_indicator_eq_self_of_superset (h1 : s.mul_indicator f = f) (h2 : s ⊆ t) : t.mul_indicator f = f := by { rw mul_indicator_eq_self at h1 ⊢, exact subset.trans h1 h2 } @[simp, to_additive] lemma mul_indicator_apply_eq_one : mul_indicator s f a = 1 ↔ (a ∈ s → f a = 1) := ite_eq_right_iff @[simp, to_additive] lemma mul_indicator_eq_one : mul_indicator s f = (λ x, 1) ↔ disjoint (mul_support f) s := by simp only [funext_iff, mul_indicator_apply_eq_one, set.disjoint_left, mem_mul_support, not_imp_not] @[simp, to_additive] lemma mul_indicator_eq_one' : mul_indicator s f = 1 ↔ disjoint (mul_support f) s := mul_indicator_eq_one @[simp, to_additive] lemma mul_support_mul_indicator : function.mul_support (s.mul_indicator f) = s ∩ function.mul_support f := ext $ λ x, by simp [function.mem_mul_support, mul_indicator_apply_eq_one] /-- If a multiplicative indicator function is not equal to one at a point, then that point is in the set. -/ @[to_additive] lemma mem_of_mul_indicator_ne_one (h : mul_indicator s f a ≠ 1) : a ∈ s := not_imp_comm.1 (λ hn, mul_indicator_of_not_mem hn f) h @[to_additive] lemma eq_on_mul_indicator : eq_on (mul_indicator s f) f s := λ x hx, mul_indicator_of_mem hx f @[to_additive] lemma mul_support_mul_indicator_subset : mul_support (s.mul_indicator f) ⊆ s := λ x hx, hx.imp_symm (λ h, mul_indicator_of_not_mem h f) @[simp, to_additive] lemma mul_indicator_mul_support : mul_indicator (mul_support f) f = f := mul_indicator_eq_self.2 subset.rfl @[simp, to_additive] lemma mul_indicator_range_comp {ι : Sort*} (f : ι → α) (g : α → M) : mul_indicator (range f) g ∘ f = g ∘ f := piecewise_range_comp _ _ _ @[to_additive] lemma mul_indicator_congr (h : eq_on f g s) : mul_indicator s f = mul_indicator s g := funext $ λx, by { simp only [mul_indicator], split_ifs, { exact h h_1 }, refl } @[simp, to_additive] lemma mul_indicator_univ (f : α → M) : mul_indicator (univ : set α) f = f := mul_indicator_eq_self.2 $ subset_univ _ @[simp, to_additive] lemma mul_indicator_empty (f : α → M) : mul_indicator (∅ : set α) f = λa, 1 := mul_indicator_eq_one.2 $ disjoint_empty _ @[to_additive] lemma mul_indicator_empty' (f : α → M) : mul_indicator (∅ : set α) f = 1 := mul_indicator_empty f variable (M) @[simp, to_additive] lemma mul_indicator_one (s : set α) : mul_indicator s (λx, (1:M)) = λx, (1:M) := mul_indicator_eq_one.2 $ by simp only [mul_support_one, empty_disjoint] @[simp, to_additive] lemma mul_indicator_one' {s : set α} : s.mul_indicator (1 : α → M) = 1 := mul_indicator_one M s variable {M} @[to_additive] lemma mul_indicator_mul_indicator (s t : set α) (f : α → M) : mul_indicator s (mul_indicator t f) = mul_indicator (s ∩ t) f := funext $ λx, by { simp only [mul_indicator], split_ifs, repeat {simp * at * {contextual := tt}} } @[simp, to_additive] lemma mul_indicator_inter_mul_support (s : set α) (f : α → M) : mul_indicator (s ∩ mul_support f) f = mul_indicator s f := by rw [← mul_indicator_mul_indicator, mul_indicator_mul_support] @[to_additive] lemma comp_mul_indicator (h : M → β) (f : α → M) {s : set α} {x : α} : h (s.mul_indicator f x) = s.piecewise (h ∘ f) (const α (h 1)) x := s.apply_piecewise _ _ (λ _, h) @[to_additive] lemma mul_indicator_comp_right {s : set α} (f : β → α) {g : α → M} {x : β} : mul_indicator (f ⁻¹' s) (g ∘ f) x = mul_indicator s g (f x) := by { simp only [mul_indicator], split_ifs; refl } @[to_additive] lemma mul_indicator_comp_of_one {g : M → N} (hg : g 1 = 1) : mul_indicator s (g ∘ f) = g ∘ (mul_indicator s f) := begin funext, simp only [mul_indicator], split_ifs; simp [*] end @[to_additive] lemma comp_mul_indicator_const (c : M) (f : M → N) (hf : f 1 = 1) : (λ x, f (s.mul_indicator (λ x, c) x)) = s.mul_indicator (λ x, f c) := (mul_indicator_comp_of_one hf).symm @[to_additive] lemma mul_indicator_preimage (s : set α) (f : α → M) (B : set M) : (mul_indicator s f)⁻¹' B = s.ite (f ⁻¹' B) (1 ⁻¹' B) := piecewise_preimage s f 1 B @[to_additive] lemma mul_indicator_preimage_of_not_mem (s : set α) (f : α → M) {t : set M} (ht : (1:M) ∉ t) : (mul_indicator s f)⁻¹' t = f ⁻¹' t ∩ s := by simp [mul_indicator_preimage, pi.one_def, set.preimage_const_of_not_mem ht] @[to_additive] lemma mem_range_mul_indicator {r : M} {s : set α} {f : α → M} : r ∈ range (mul_indicator s f) ↔ (r = 1 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by simp [mul_indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm, @eq_comm _ r 1] @[to_additive] lemma mul_indicator_rel_mul_indicator {r : M → M → Prop} (h1 : r 1 1) (ha : a ∈ s → r (f a) (g a)) : r (mul_indicator s f a) (mul_indicator s g a) := by { simp only [mul_indicator], split_ifs with has has, exacts [ha has, h1] } end has_one section monoid variables [mul_one_class M] {s t : set α} {f g : α → M} {a : α} @[to_additive] lemma mul_indicator_union_mul_inter_apply (f : α → M) (s t : set α) (a : α) : mul_indicator (s ∪ t) f a * mul_indicator (s ∩ t) f a = mul_indicator s f a * mul_indicator t f a := by by_cases hs : a ∈ s; by_cases ht : a ∈ t; simp * @[to_additive] lemma mul_indicator_union_mul_inter (f : α → M) (s t : set α) : mul_indicator (s ∪ t) f * mul_indicator (s ∩ t) f = mul_indicator s f * mul_indicator t f := funext $ mul_indicator_union_mul_inter_apply f s t @[to_additive] lemma mul_indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → M) : mul_indicator (s ∪ t) f a = mul_indicator s f a * mul_indicator t f a := by rw [← mul_indicator_union_mul_inter_apply f s t, mul_indicator_of_not_mem h, mul_one] @[to_additive] lemma mul_indicator_union_of_disjoint (h : disjoint s t) (f : α → M) : mul_indicator (s ∪ t) f = λa, mul_indicator s f a * mul_indicator t f a := funext $ λa, mul_indicator_union_of_not_mem_inter (λ ha, h ha) _ @[to_additive] lemma mul_indicator_mul (s : set α) (f g : α → M) : mul_indicator s (λa, f a * g a) = λa, mul_indicator s f a * mul_indicator s g a := by { funext, simp only [mul_indicator], split_ifs, { refl }, rw mul_one } @[simp, to_additive] lemma mul_indicator_compl_mul_self_apply (s : set α) (f : α → M) (a : α) : mul_indicator sᶜ f a * mul_indicator s f a = f a := classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha]) @[simp, to_additive] lemma mul_indicator_compl_mul_self (s : set α) (f : α → M) : mul_indicator sᶜ f * mul_indicator s f = f := funext $ mul_indicator_compl_mul_self_apply s f @[simp, to_additive] lemma mul_indicator_self_mul_compl_apply (s : set α) (f : α → M) (a : α) : mul_indicator s f a * mul_indicator sᶜ f a = f a := classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha]) @[simp, to_additive] lemma mul_indicator_self_mul_compl (s : set α) (f : α → M) : mul_indicator s f * mul_indicator sᶜ f = f := funext $ mul_indicator_self_mul_compl_apply s f @[to_additive] lemma mul_indicator_mul_eq_left {f g : α → M} (h : disjoint (mul_support f) (mul_support g)) : (mul_support f).mul_indicator (f * g) = f := begin refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support, have : g x = 1, from nmem_mul_support.1 (disjoint_left.1 h hx), rw [pi.mul_apply, this, mul_one] end @[to_additive] lemma mul_indicator_mul_eq_right {f g : α → M} (h : disjoint (mul_support f) (mul_support g)) : (mul_support g).mul_indicator (f * g) = g := begin refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support, have : f x = 1, from nmem_mul_support.1 (disjoint_right.1 h hx), rw [pi.mul_apply, this, one_mul] end /-- `set.mul_indicator` as a `monoid_hom`. -/ @[to_additive "`set.indicator` as an `add_monoid_hom`."] def mul_indicator_hom {α} (M) [mul_one_class M] (s : set α) : (α → M) →* (α → M) := { to_fun := mul_indicator s, map_one' := mul_indicator_one M s, map_mul' := mul_indicator_mul s } end monoid section distrib_mul_action variables {A : Type*} [add_monoid A] [monoid M] [distrib_mul_action M A] lemma indicator_smul_apply (s : set α) (r : M) (f : α → A) (x : α) : indicator s (λ x, r • f x) x = r • indicator s f x := by { dunfold indicator, split_ifs, exacts [rfl, (smul_zero r).symm] } lemma indicator_smul (s : set α) (r : M) (f : α → A) : indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x := funext $ indicator_smul_apply s r f end distrib_mul_action section group variables {G : Type*} [group G] {s t : set α} {f g : α → G} {a : α} @[to_additive] lemma mul_indicator_inv' (s : set α) (f : α → G) : mul_indicator s (f⁻¹) = (mul_indicator s f)⁻¹ := (mul_indicator_hom G s).map_inv f @[to_additive] lemma mul_indicator_inv (s : set α) (f : α → G) : mul_indicator s (λa, (f a)⁻¹) = λa, (mul_indicator s f a)⁻¹ := mul_indicator_inv' s f lemma indicator_sub {G} [add_group G] (s : set α) (f g : α → G) : indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a := (indicator_hom G s).map_sub f g @[to_additive indicator_compl'] lemma mul_indicator_compl (s : set α) (f : α → G) : mul_indicator sᶜ f = f * (mul_indicator s f)⁻¹ := eq_mul_inv_of_mul_eq $ s.mul_indicator_compl_mul_self f lemma indicator_compl {G} [add_group G] (s : set α) (f : α → G) : indicator sᶜ f = f - indicator s f := by rw [sub_eq_add_neg, indicator_compl'] @[to_additive indicator_diff'] lemma mul_indicator_diff (h : s ⊆ t) (f : α → G) : mul_indicator (t \ s) f = mul_indicator t f * (mul_indicator s f)⁻¹ := eq_mul_inv_of_mul_eq $ by rw [pi.mul_def, ← mul_indicator_union_of_disjoint disjoint_diff.symm f, diff_union_self, union_eq_self_of_subset_right h] lemma indicator_diff {G : Type*} [add_group G] {s t : set α} (h : s ⊆ t) (f : α → G) : indicator (t \ s) f = indicator t f - indicator s f := by rw [indicator_diff' h, sub_eq_add_neg] end group section comm_monoid variables [comm_monoid M] /-- Consider a product of `g i (f i)` over a `finset`. Suppose `g` is a function such as `pow`, which maps a second argument of `1` to `1`. Then if `f` is replaced by the corresponding multiplicative indicator function, the `finset` may be replaced by a possibly larger `finset` without changing the value of the sum. -/ @[to_additive] lemma prod_mul_indicator_subset_of_eq_one [has_one N] (f : α → N) (g : α → N → M) {s t : finset α} (h : s ⊆ t) (hg : ∀ a, g a 1 = 1) : ∏ i in s, g i (f i) = ∏ i in t, g i (mul_indicator ↑s f i) := begin rw ← finset.prod_subset h _, { apply finset.prod_congr rfl, intros i hi, congr, symmetry, exact mul_indicator_of_mem hi _ }, { refine λ i hi hn, _, convert hg i, exact mul_indicator_of_not_mem hn _ } end /-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a function such as multiplication, which maps a second argument of 0 to 0. (A typical use case would be a weighted sum of `f i * h i` or `f i • h i`, where `f` gives the weights that are multiplied by some other function `h`.) Then if `f` is replaced by the corresponding indicator function, the `finset` may be replaced by a possibly larger `finset` without changing the value of the sum. -/ add_decl_doc set.sum_indicator_subset_of_eq_zero @[to_additive] lemma prod_mul_indicator_subset (f : α → M) {s t : finset α} (h : s ⊆ t) : ∏ i in s, f i = ∏ i in t, mul_indicator ↑s f i := prod_mul_indicator_subset_of_eq_one _ (λ a b, b) h (λ _, rfl) /-- Summing an indicator function over a possibly larger `finset` is the same as summing the original function over the original `finset`. -/ add_decl_doc sum_indicator_subset @[to_additive] lemma _root_.finset.prod_mul_indicator_eq_prod_filter (s : finset ι) (f : ι → α → M) (t : ι → set α) (g : ι → α) : ∏ i in s, mul_indicator (t i) (f i) (g i) = ∏ i in s.filter (λ i, g i ∈ t i), f i (g i) := begin refine (finset.prod_filter_mul_prod_filter_not s (λ i, g i ∈ t i) _).symm.trans _, refine eq.trans _ (mul_one _), exact congr_arg2 (*) (finset.prod_congr rfl $ λ x hx, mul_indicator_of_mem (finset.mem_filter.1 hx).2 _) (finset.prod_eq_one $ λ x hx, mul_indicator_of_not_mem (finset.mem_filter.1 hx).2 _) end @[to_additive] lemma mul_indicator_finset_prod (I : finset ι) (s : set α) (f : ι → α → M) : mul_indicator s (∏ i in I, f i) = ∏ i in I, mul_indicator s (f i) := (mul_indicator_hom M s).map_prod _ _ @[to_additive] lemma mul_indicator_finset_bUnion {ι} (I : finset ι) (s : ι → set α) {f : α → M} : (∀ (i ∈ I) (j ∈ I), i ≠ j → disjoint (s i) (s j)) → mul_indicator (⋃ i ∈ I, s i) f = λ a, ∏ i in I, mul_indicator (s i) f a := begin refine finset.induction_on I _ _, { intro h, funext, simp }, assume a I haI ih hI, funext, rw [finset.prod_insert haI, finset.set_bUnion_insert, mul_indicator_union_of_not_mem_inter, ih _], { assume i hi j hj hij, exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij }, simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and], assume hx a' ha', refine disjoint_left.1 (hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _) hx, exact (ne_of_mem_of_not_mem ha' haI).symm end end comm_monoid section mul_zero_class variables [mul_zero_class M] {s t : set α} {f g : α → M} {a : α} lemma indicator_mul (s : set α) (f g : α → M) : indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero } lemma indicator_mul_left (s : set α) (f g : α → M) : indicator s (λa, f a * g a) a = indicator s f a * g a := by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] } lemma indicator_mul_right (s : set α) (f g : α → M) : indicator s (λa, f a * g a) a = f a * indicator s g a := by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] } lemma inter_indicator_mul {t1 t2 : set α} (f g : α → M) (x : α) : (t1 ∩ t2).indicator (λ x, f x * g x) x = t1.indicator f x * t2.indicator g x := by { rw [← set.indicator_indicator], simp [indicator] } end mul_zero_class section monoid_with_zero variables [monoid_with_zero M] lemma indicator_prod_one {s : set α} {t : set β} {x : α} {y : β} : (s.prod t).indicator (1 : _ → M) (x, y) = s.indicator 1 x * t.indicator 1 y := by simp [indicator, ← ite_and] end monoid_with_zero section order variables [has_one M] [preorder M] {s t : set α} {f g : α → M} {a : α} {y : M} @[to_additive] lemma mul_indicator_apply_le' (hfg : a ∈ s → f a ≤ y) (hg : a ∉ s → 1 ≤ y) : mul_indicator s f a ≤ y := if ha : a ∈ s then by simpa [ha] using hfg ha else by simpa [ha] using hg ha @[to_additive] lemma mul_indicator_le' (hfg : ∀ a ∈ s, f a ≤ g a) (hg : ∀ a ∉ s, 1 ≤ g a) : mul_indicator s f ≤ g := λ a, mul_indicator_apply_le' (hfg _) (hg _) @[to_additive] lemma le_mul_indicator_apply {y} (hfg : a ∈ s → y ≤ g a) (hf : a ∉ s → y ≤ 1) : y ≤ mul_indicator s g a := @mul_indicator_apply_le' α (order_dual M) ‹_› _ _ _ _ _ hfg hf @[to_additive] lemma le_mul_indicator (hfg : ∀ a ∈ s, f a ≤ g a) (hf : ∀ a ∉ s, f a ≤ 1) : f ≤ mul_indicator s g := λ a, le_mul_indicator_apply (hfg _) (hf _) @[to_additive indicator_apply_nonneg] lemma one_le_mul_indicator_apply (h : a ∈ s → 1 ≤ f a) : 1 ≤ mul_indicator s f a := le_mul_indicator_apply h (λ _, le_rfl) @[to_additive indicator_nonneg] lemma one_le_mul_indicator (h : ∀ a ∈ s, 1 ≤ f a) (a : α) : 1 ≤ mul_indicator s f a := one_le_mul_indicator_apply (h a) @[to_additive] lemma mul_indicator_apply_le_one (h : a ∈ s → f a ≤ 1) : mul_indicator s f a ≤ 1 := mul_indicator_apply_le' h (λ _, le_rfl) @[to_additive] lemma mul_indicator_le_one (h : ∀ a ∈ s, f a ≤ 1) (a : α) : mul_indicator s f a ≤ 1 := mul_indicator_apply_le_one (h a) @[to_additive] lemma mul_indicator_le_mul_indicator (h : f a ≤ g a) : mul_indicator s f a ≤ mul_indicator s g a := mul_indicator_rel_mul_indicator (le_refl _) (λ _, h) attribute [mono] mul_indicator_le_mul_indicator indicator_le_indicator @[to_additive] lemma mul_indicator_le_mul_indicator_of_subset (h : s ⊆ t) (hf : ∀ a, 1 ≤ f a) (a : α) : mul_indicator s f a ≤ mul_indicator t f a := mul_indicator_apply_le' (λ ha, le_mul_indicator_apply (λ _, le_rfl) (λ hat, (hat $ h ha).elim)) (λ ha, one_le_mul_indicator_apply (λ _, hf _)) @[to_additive] lemma mul_indicator_le_self' (hf : ∀ x ∉ s, 1 ≤ f x) : mul_indicator s f ≤ f := mul_indicator_le' (λ _ _, le_refl _) hf @[to_additive] lemma mul_indicator_Union_apply {ι M} [complete_lattice M] [has_one M] (h1 : (⊥:M) = 1) (s : ι → set α) (f : α → M) (x : α) : mul_indicator (⋃ i, s i) f x = ⨆ i, mul_indicator (s i) f x := begin by_cases hx : x ∈ ⋃ i, s i, { rw [mul_indicator_of_mem hx], rw [mem_Union] at hx, refine le_antisymm _ (supr_le $ λ i, mul_indicator_le_self' (λ x hx, h1 ▸ bot_le) x), rcases hx with ⟨i, hi⟩, exact le_supr_of_le i (ge_of_eq $ mul_indicator_of_mem hi _) }, { rw [mul_indicator_of_not_mem hx], simp only [mem_Union, not_exists] at hx, simp [hx, ← h1] } end end order section canonically_ordered_monoid variables [canonically_ordered_monoid M] @[to_additive] lemma mul_indicator_le_self (s : set α) (f : α → M) : mul_indicator s f ≤ f := mul_indicator_le_self' $ λ _ _, one_le _ @[to_additive] lemma mul_indicator_apply_le {a : α} {s : set α} {f g : α → M} (hfg : a ∈ s → f a ≤ g a) : mul_indicator s f a ≤ g a := mul_indicator_apply_le' hfg $ λ _, one_le _ @[to_additive] lemma mul_indicator_le {s : set α} {f g : α → M} (hfg : ∀ a ∈ s, f a ≤ g a) : mul_indicator s f ≤ g := mul_indicator_le' hfg $ λ _ _, one_le _ end canonically_ordered_monoid lemma indicator_le_indicator_nonneg {β} [linear_order β] [has_zero β] (s : set α) (f : α → β) : s.indicator f ≤ {x | 0 ≤ f x}.indicator f := begin intro x, simp_rw indicator_apply, split_ifs, { exact le_rfl, }, { exact (not_le.mp h_1).le, }, { exact h_1, }, { exact le_rfl, }, end lemma indicator_nonpos_le_indicator {β} [linear_order β] [has_zero β] (s : set α) (f : α → β) : {x | f x ≤ 0}.indicator f ≤ s.indicator f := @indicator_le_indicator_nonneg α (order_dual β) _ _ s f end set @[to_additive] lemma monoid_hom.map_mul_indicator {M N : Type*} [monoid M] [monoid N] (f : M →* N) (s : set α) (g : α → M) (x : α) : f (s.mul_indicator g x) = s.mul_indicator (f ∘ g) x := congr_fun (set.mul_indicator_comp_of_one f.map_one).symm x
d6df4d895c4f23b46e5f4625bd6512b17cdc45da
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/fractional_ideal.lean
483495cd7600a3b3bf6c6567571286abca9c6bc9
[ "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
49,767
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Filippo A. E. Nuccio -/ import ring_theory.localization import ring_theory.noetherian import ring_theory.principal_ideal_domain import tactic.field_simp /-! # Fractional ideals This file defines fractional ideals of an integral domain and proves basic facts about them. ## Main definitions Let `S` be a submonoid of an integral domain `R`, `P` the localization of `R` at `S`, and `f` the natural ring hom from `R` to `P`. * `is_fractional` defines which `R`-submodules of `P` are fractional ideals * `fractional_ideal S P` is the type of fractional ideals in `P` * `has_coe_t (ideal R) (fractional_ideal S P)` instance * `comm_semiring (fractional_ideal S P)` instance: the typical ideal operations generalized to fractional ideals * `lattice (fractional_ideal S P)` instance * `map` is the pushforward of a fractional ideal along an algebra morphism Let `K` be the localization of `R` at `R⁰ = R \ {0}` (i.e. the field of fractions). * `fractional_ideal R⁰ K` is the type of fractional ideals in the field of fractions * `has_div (fractional_ideal R⁰ K)` instance: the ideal quotient `I / J` (typically written $I : J$, but a `:` operator cannot be defined) ## Main statements * `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone * `prod_one_self_div_eq` states that `1 / I` is the inverse of `I` if one exists * `is_noetherian` states that every fractional ideal of a noetherian integral domain is noetherian ## Implementation notes Fractional ideals are considered equal when they contain the same elements, independent of the denominator `a : R` such that `a I ⊆ R`. Thus, we define `fractional_ideal` to be the subtype of the predicate `is_fractional`, instead of having `fractional_ideal` be a structure of which `a` is a field. Most definitions in this file specialize operations from submodules to fractional ideals, proving that the result of this operation is fractional if the input is fractional. Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`, in order to re-use their respective proof terms. We can still use `simp` to show `↑I + ↑J = ↑(I + J)` and `↑⊥ = ↑0`. Many results in fact do not need that `P` is a localization, only that `P` is an `R`-algebra. We omit the `is_localization` parameter whenever this is practical. Similarly, we don't assume that the localization is a field until we need it to define ideal quotients. When this assumption is needed, we replace `S` with `R⁰`, making the localization a field. ## References * https://en.wikipedia.org/wiki/Fractional_ideal ## Tags fractional ideal, fractional ideals, invertible ideal -/ open is_localization open_locale pointwise open_locale non_zero_divisors section defs variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P] variables [algebra R P] variables (S) /-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/ def is_fractional (I : submodule R P) := ∃ a ∈ S, ∀ b ∈ I, is_integer R (a • b) variables (S P) /-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`. More precisely, let `P` be a localization of `R` at some submonoid `S`, then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`, such that there is a nonzero `a : R` with `a I ⊆ R`. -/ def fractional_ideal := {I : submodule R P // is_fractional S I} end defs namespace fractional_ideal open set open submodule variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P] variables [algebra R P] [loc : is_localization S P] /-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`. This coercion is typically called `coe_to_submodule` in lemma names (or `coe` when the coercion is clear from the context), not to be confused with `is_localization.coe_submodule : ideal R → submodule R P` (which we use to define `coe : ideal R → fractional_ideal S P`, referred to as `coe_ideal` in theorem names). -/ instance : has_coe (fractional_ideal S P) (submodule R P) := ⟨λ I, I.val⟩ protected lemma is_fractional (I : fractional_ideal S P) : is_fractional S (I : submodule R P) := I.prop section set_like instance : set_like (fractional_ideal S P) P := { coe := λ I, ↑(I : submodule R P), coe_injective' := set_like.coe_injective.comp subtype.coe_injective } @[simp] lemma mem_coe {I : fractional_ideal S P} {x : P} : x ∈ (I : submodule R P) ↔ x ∈ I := iff.rfl @[ext] lemma ext {I J : fractional_ideal S P} : (∀ x, x ∈ I ↔ x ∈ J) → I = J := set_like.ext /-- Copy of a `fractional_ideal` with a new underlying set equal to the old one. Useful to fix definitional equalities. -/ protected def copy (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) : fractional_ideal S P := ⟨submodule.copy p s hs, by { convert p.is_fractional, ext, simp only [hs], refl }⟩ end set_like @[simp] lemma val_eq_coe (I : fractional_ideal S P) : I.val = I := rfl @[simp, norm_cast] lemma coe_mk (I : submodule R P) (hI : is_fractional S I) : (subtype.mk I hI : submodule R P) = I := rfl lemma coe_to_submodule_injective : function.injective (coe : fractional_ideal S P → submodule R P) := subtype.coe_injective lemma is_fractional_of_le_one (I : submodule R P) (h : I ≤ 1) : is_fractional S I := begin use [1, S.one_mem], intros b hb, rw one_smul, obtain ⟨b', b'_mem, rfl⟩ := h hb, exact set.mem_range_self b', end lemma is_fractional_of_le {I : submodule R P} {J : fractional_ideal S P} (hIJ : I ≤ J) : is_fractional S I := begin obtain ⟨a, a_mem, ha⟩ := J.is_fractional, use [a, a_mem], intros b b_mem, exact ha b (hIJ b_mem) end /-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral. This is a bundled version of `is_localization.coe_submodule : ideal R → submodule R P`, which is not to be confused with the `coe : fractional_ideal S P → submodule R P`, also called `coe_to_submodule` in theorem names. This map is available as a ring hom, called `fractional_ideal.coe_ideal_hom`. -/ -- Is a `coe_t` rather than `coe` to speed up failing inference, see library note [use has_coe_t] instance coe_to_fractional_ideal : has_coe_t (ideal R) (fractional_ideal S P) := ⟨λ I, ⟨coe_submodule P I, is_fractional_of_le_one _ (by simpa using coe_submodule_mono P (le_top : I ≤ ⊤))⟩⟩ @[simp, norm_cast] lemma coe_coe_ideal (I : ideal R) : ((I : fractional_ideal S P) : submodule R P) = coe_submodule P I := rfl variables (S) @[simp] lemma mem_coe_ideal {x : P} {I : ideal R} : x ∈ (I : fractional_ideal S P) ↔ ∃ x', x' ∈ I ∧ algebra_map R P x' = x := mem_coe_submodule _ _ lemma mem_coe_ideal_of_mem {x : R} {I : ideal R} (hx : x ∈ I) : algebra_map R P x ∈ (I : fractional_ideal S P) := (mem_coe_ideal S).mpr ⟨x, hx, rfl⟩ lemma coe_ideal_le_coe_ideal' [is_localization S P] (h : S ≤ non_zero_divisors R) {I J : ideal R} : (I : fractional_ideal S P) ≤ J ↔ I ≤ J := coe_submodule_le_coe_submodule h @[simp] lemma coe_ideal_le_coe_ideal (K : Type*) [comm_ring K] [algebra R K] [is_fraction_ring R K] {I J : ideal R} : (I : fractional_ideal R⁰ K) ≤ J ↔ I ≤ J := is_fraction_ring.coe_submodule_le_coe_submodule instance : has_zero (fractional_ideal S P) := ⟨(0 : ideal R)⟩ @[simp] lemma mem_zero_iff {x : P} : x ∈ (0 : fractional_ideal S P) ↔ x = 0 := ⟨(λ ⟨x', x'_mem_zero, x'_eq_x⟩, have x'_eq_zero : x' = 0 := x'_mem_zero, by simp [x'_eq_x.symm, x'_eq_zero]), (λ hx, ⟨0, rfl, by simp [hx]⟩)⟩ variables {S} @[simp, norm_cast] lemma coe_zero : ↑(0 : fractional_ideal S P) = (⊥ : submodule R P) := submodule.ext $ λ _, mem_zero_iff S @[simp, norm_cast] lemma coe_to_fractional_ideal_bot : ((⊥ : ideal R) : fractional_ideal S P) = 0 := rfl variables (P) include loc @[simp] lemma exists_mem_to_map_eq {x : R} {I : ideal R} (h : S ≤ non_zero_divisors R) : (∃ x', x' ∈ I ∧ algebra_map R P x' = algebra_map R P x) ↔ x ∈ I := ⟨λ ⟨x', hx', eq⟩, is_localization.injective _ h eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩ variables {P} lemma coe_to_fractional_ideal_injective (h : S ≤ non_zero_divisors R) : function.injective (coe : ideal R → fractional_ideal S P) := λ I J heq, have ∀ (x : R), algebra_map R P x ∈ (I : fractional_ideal S P) ↔ algebra_map R P x ∈ (J : fractional_ideal S P) := λ x, heq ▸ iff.rfl, ideal.ext (by simpa only [mem_coe_ideal, exists_prop, exists_mem_to_map_eq P h] using this) lemma coe_to_fractional_ideal_eq_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) : (I : fractional_ideal S P) = 0 ↔ I = (⊥ : ideal R) := ⟨λ h, coe_to_fractional_ideal_injective hS h, λ h, by rw [h, coe_to_fractional_ideal_bot]⟩ lemma coe_to_fractional_ideal_ne_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) : (I : fractional_ideal S P) ≠ 0 ↔ I ≠ (⊥ : ideal R) := not_iff_not.mpr (coe_to_fractional_ideal_eq_zero hS) omit loc lemma coe_to_submodule_eq_bot {I : fractional_ideal S P} : (I : submodule R P) = ⊥ ↔ I = 0 := ⟨λ h, coe_to_submodule_injective (by simp [h]), λ h, by simp [h]⟩ lemma coe_to_submodule_ne_bot {I : fractional_ideal S P} : ↑I ≠ (⊥ : submodule R P) ↔ I ≠ 0 := not_iff_not.mpr coe_to_submodule_eq_bot instance : inhabited (fractional_ideal S P) := ⟨0⟩ instance : has_one (fractional_ideal S P) := ⟨(⊤ : ideal R)⟩ variables (S) @[simp, norm_cast] lemma coe_ideal_top : ((⊤ : ideal R) : fractional_ideal S P) = 1 := rfl lemma mem_one_iff {x : P} : x ∈ (1 : fractional_ideal S P) ↔ ∃ x' : R, algebra_map R P x' = x := iff.intro (λ ⟨x', _, h⟩, ⟨x', h⟩) (λ ⟨x', h⟩, ⟨x', ⟨⟩, h⟩) lemma coe_mem_one (x : R) : algebra_map R P x ∈ (1 : fractional_ideal S P) := (mem_one_iff S).mpr ⟨x, rfl⟩ lemma one_mem_one : (1 : P) ∈ (1 : fractional_ideal S P) := (mem_one_iff S).mpr ⟨1, ring_hom.map_one _⟩ variables {S} /-- `(1 : fractional_ideal S P)` is defined as the R-submodule `f(R) ≤ P`. However, this is not definitionally equal to `1 : submodule R P`, which is proved in the actual `simp` lemma `coe_one`. -/ lemma coe_one_eq_coe_submodule_top : ↑(1 : fractional_ideal S P) = coe_submodule P (⊤ : ideal R) := rfl @[simp, norm_cast] lemma coe_one : (↑(1 : fractional_ideal S P) : submodule R P) = 1 := by rw [coe_one_eq_coe_submodule_top, coe_submodule_top] section lattice /-! ### `lattice` section Defines the order on fractional ideals as inclusion of their underlying sets, and ports the lattice structure on submodules to fractional ideals. -/ @[simp] lemma coe_le_coe {I J : fractional_ideal S P} : (I : submodule R P) ≤ (J : submodule R P) ↔ I ≤ J := iff.rfl lemma zero_le (I : fractional_ideal S P) : 0 ≤ I := begin intros x hx, convert submodule.zero_mem _, simpa using hx end instance order_bot : order_bot (fractional_ideal S P) := { bot := 0, bot_le := zero_le } @[simp] lemma bot_eq_zero : (⊥ : fractional_ideal S P) = 0 := rfl @[simp] lemma le_zero_iff {I : fractional_ideal S P} : I ≤ 0 ↔ I = 0 := le_bot_iff lemma eq_zero_iff {I : fractional_ideal S P} : I = 0 ↔ (∀ x ∈ I, x = (0 : P)) := ⟨ (λ h x hx, by simpa [h, mem_zero_iff] using hx), (λ h, le_bot_iff.mp (λ x hx, (mem_zero_iff S).mpr (h x hx))) ⟩ lemma fractional_sup (I J : fractional_ideal S P) : is_fractional S (I ⊔ J : submodule R P) := begin rcases I.is_fractional with ⟨aI, haI, hI⟩, rcases J.is_fractional with ⟨aJ, haJ, hJ⟩, use aI * aJ, use S.mul_mem haI haJ, intros b hb, rcases mem_sup.mp hb with ⟨bI, hbI, bJ, hbJ, rfl⟩, rw smul_add, apply is_integer_add, { rw [mul_smul, smul_comm], exact is_integer_smul (hI bI hbI), }, { rw mul_smul, exact is_integer_smul (hJ bJ hbJ) } end lemma fractional_inf (I J : fractional_ideal S P) : is_fractional S (I ⊓ J : submodule R P) := begin rcases I.is_fractional with ⟨aI, haI, hI⟩, use aI, use haI, intros b hb, rcases mem_inf.mp hb with ⟨hbI, hbJ⟩, exact hI b hbI end instance lattice : lattice (fractional_ideal S P) := { inf := λ I J, ⟨I ⊓ J, fractional_inf I J⟩, sup := λ I J, ⟨I ⊔ J, fractional_sup I J⟩, inf_le_left := λ I J, show (I ⊓ J : submodule R P) ≤ I, from inf_le_left, inf_le_right := λ I J, show (I ⊓ J : submodule R P) ≤ J, from inf_le_right, le_inf := λ I J K hIJ hIK, show (I : submodule R P) ≤ J ⊓ K, from le_inf hIJ hIK, le_sup_left := λ I J, show (I : submodule R P) ≤ I ⊔ J, from le_sup_left, le_sup_right := λ I J, show (J : submodule R P) ≤ I ⊔ J, from le_sup_right, sup_le := λ I J K hIK hJK, show (I ⊔ J : submodule R P) ≤ K, from sup_le hIK hJK, ..set_like.partial_order } instance : semilattice_sup (fractional_ideal S P) := { ..fractional_ideal.lattice } end lattice section semiring instance : has_add (fractional_ideal S P) := ⟨(⊔)⟩ @[simp] lemma sup_eq_add (I J : fractional_ideal S P) : I ⊔ J = I + J := rfl @[simp, norm_cast] lemma coe_add (I J : fractional_ideal S P) : (↑(I + J) : submodule R P) = I + J := rfl @[simp, norm_cast] lemma coe_ideal_sup (I J : ideal R) : ↑(I ⊔ J) = (I + J : fractional_ideal S P) := coe_to_submodule_injective $ coe_submodule_sup _ _ _ lemma fractional_mul (I J : fractional_ideal S P) : is_fractional S (I * J : submodule R P) := begin rcases I with ⟨I, aI, haI, hI⟩, rcases J with ⟨J, aJ, haJ, hJ⟩, use aI * aJ, use S.mul_mem haI haJ, intros b hb, apply submodule.mul_induction_on hb, { intros m hm n hn, obtain ⟨n', hn'⟩ := hJ n hn, rw [mul_smul, mul_comm m, ← smul_mul_assoc, ← hn', ← algebra.smul_def], apply hI, exact submodule.smul_mem _ _ hm }, { intros x y hx hy, rw smul_add, apply is_integer_add hx hy }, end /-- `fractional_ideal.mul` is the product of two fractional ideals, used to define the `has_mul` instance. This is only an auxiliary definition: the preferred way of writing `I.mul J` is `I * J`. Elaborated terms involving `fractional_ideal` tend to grow quite large, so by making definitions irreducible, we hope to avoid deep unfolds. -/ @[irreducible] def mul (I J : fractional_ideal S P) : fractional_ideal S P := ⟨I * J, fractional_mul I J⟩ local attribute [semireducible] mul instance : has_mul (fractional_ideal S P) := ⟨λ I J, mul I J⟩ @[simp] lemma mul_eq_mul (I J : fractional_ideal S P) : mul I J = I * J := rfl @[simp, norm_cast] lemma coe_mul (I J : fractional_ideal S P) : (↑(I * J) : submodule R P) = I * J := rfl @[simp, norm_cast] lemma coe_ideal_mul (I J : ideal R) : (↑(I * J) : fractional_ideal S P) = I * J := coe_to_submodule_injective $ coe_submodule_mul _ _ _ lemma mul_left_mono (I : fractional_ideal S P) : monotone ((*) I) := λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul hx (h hy)) lemma mul_right_mono (I : fractional_ideal S P) : monotone (λ J, J * I) := λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul (h hx) hy) lemma mul_mem_mul {I J : fractional_ideal S P} {i j : P} (hi : i ∈ I) (hj : j ∈ J) : i * j ∈ I * J := submodule.mul_mem_mul hi hj lemma mul_le {I J K : fractional_ideal S P} : I * J ≤ K ↔ (∀ (i ∈ I) (j ∈ J), i * j ∈ K) := submodule.mul_le @[elab_as_eliminator] protected theorem mul_induction_on {I J : fractional_ideal S P} {C : P → Prop} {r : P} (hr : r ∈ I * J) (hm : ∀ (i ∈ I) (j ∈ J), C (i * j)) (ha : ∀ x y, C x → C y → C (x + y)) : C r := submodule.mul_induction_on hr hm ha instance comm_semiring : comm_semiring (fractional_ideal S P) := { add_assoc := λ I J K, sup_assoc, add_comm := λ I J, sup_comm, add_zero := λ I, sup_bot_eq, zero_add := λ I, bot_sup_eq, mul_assoc := λ I J K, coe_to_submodule_injective (submodule.mul_assoc _ _ _), mul_comm := λ I J, coe_to_submodule_injective (submodule.mul_comm _ _), mul_one := λ I, begin ext, split; intro h, { apply mul_le.mpr _ h, rintros x hx y ⟨y', y'_mem_R, rfl⟩, convert submodule.smul_mem _ y' hx, rw [mul_comm, eq_comm], exact algebra.smul_def y' x }, { have : x * 1 ∈ (I * 1) := mul_mem_mul h (one_mem_one _), rwa [mul_one] at this } end, one_mul := λ I, begin ext, split; intro h, { apply mul_le.mpr _ h, rintros x ⟨x', x'_mem_R, rfl⟩ y hy, convert submodule.smul_mem _ x' hy, rw eq_comm, exact algebra.smul_def x' y }, { have : 1 * x ∈ (1 * I) := mul_mem_mul (one_mem_one _) h, rwa one_mul at this } end, mul_zero := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx (λ x hx y hy, by simp [(mem_zero_iff S).mp hy]) (λ x y hx hy, by simp [hx, hy])), zero_mul := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx (λ x hx y hy, by simp [(mem_zero_iff S).mp hx]) (λ x y hx hy, by simp [hx, hy])), left_distrib := λ I J K, coe_to_submodule_injective (mul_add _ _ _), right_distrib := λ I J K, coe_to_submodule_injective (add_mul _ _ _), ..fractional_ideal.has_zero S, ..fractional_ideal.has_add, ..fractional_ideal.has_one, ..fractional_ideal.has_mul } section order lemma add_le_add_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) : J' + I ≤ J' + J := sup_le_sup_left hIJ J' lemma mul_le_mul_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) : J' * I ≤ J' * J := mul_le.mpr (λ k hk j hj, mul_mem_mul hk (hIJ hj)) lemma le_self_mul_self {I : fractional_ideal S P} (hI: 1 ≤ I) : I ≤ I * I := begin convert mul_left_mono I hI, exact (mul_one I).symm end lemma mul_self_le_self {I : fractional_ideal S P} (hI: I ≤ 1) : I * I ≤ I := begin convert mul_left_mono I hI, exact (mul_one I).symm end lemma coe_ideal_le_one {I : ideal R} : (I : fractional_ideal S P) ≤ 1 := λ x hx, let ⟨y, _, hy⟩ := (fractional_ideal.mem_coe_ideal S).mp hx in (fractional_ideal.mem_one_iff S).mpr ⟨y, hy⟩ lemma le_one_iff_exists_coe_ideal {J : fractional_ideal S P} : J ≤ (1 : fractional_ideal S P) ↔ ∃ (I : ideal R), ↑I = J := begin split, { intro hJ, refine ⟨⟨{x : R | algebra_map R P x ∈ J}, _, _, _⟩, _⟩, { rw [mem_set_of_eq, ring_hom.map_zero], exact J.val.zero_mem }, { intros a b ha hb, rw [mem_set_of_eq, ring_hom.map_add], exact J.val.add_mem ha hb }, { intros c x hx, rw [smul_eq_mul, mem_set_of_eq, ring_hom.map_mul, ← algebra.smul_def], exact J.val.smul_mem c hx }, { ext x, split, { rintros ⟨y, hy, eq_y⟩, rwa ← eq_y }, { intro hx, obtain ⟨y, eq_x⟩ := (fractional_ideal.mem_one_iff S).mp (hJ hx), rw ← eq_x at *, exact ⟨y, hx, rfl⟩ } } }, { rintro ⟨I, hI⟩, rw ← hI, apply coe_ideal_le_one }, end variables (S P) /-- `coe_ideal_hom (S : submonoid R) P` is `coe : ideal R → fractional_ideal S P` as a ring hom -/ @[simps] def coe_ideal_hom : ideal R →+* fractional_ideal S P := { to_fun := coe, map_add' := coe_ideal_sup, map_mul' := coe_ideal_mul, map_one' := by rw [ideal.one_eq_top, coe_ideal_top], map_zero' := coe_to_fractional_ideal_bot } end order variables {P' : Type*} [comm_ring P'] [algebra R P'] [loc' : is_localization S P'] variables {P'' : Type*} [comm_ring P''] [algebra R P''] [loc'' : is_localization S P''] lemma fractional_map (g : P →ₐ[R] P') (I : fractional_ideal S P) : is_fractional S (submodule.map g.to_linear_map I) := begin rcases I with ⟨I, a, a_nonzero, hI⟩, use [a, a_nonzero], intros b hb, obtain ⟨b', b'_mem, hb'⟩ := submodule.mem_map.mp hb, obtain ⟨x, hx⟩ := hI b' b'_mem, use x, erw [←g.commutes, hx, g.map_smul, hb'] end /-- `I.map g` is the pushforward of the fractional ideal `I` along the algebra morphism `g` -/ def map (g : P →ₐ[R] P') : fractional_ideal S P → fractional_ideal S P' := λ I, ⟨submodule.map g.to_linear_map I, fractional_map g I⟩ @[simp, norm_cast] lemma coe_map (g : P →ₐ[R] P') (I : fractional_ideal S P) : ↑(map g I) = submodule.map g.to_linear_map I := rfl @[simp] lemma mem_map {I : fractional_ideal S P} {g : P →ₐ[R] P'} {y : P'} : y ∈ I.map g ↔ ∃ x, x ∈ I ∧ g x = y := submodule.mem_map variables (I J : fractional_ideal S P) (g : P →ₐ[R] P') @[simp] lemma map_id : I.map (alg_hom.id _ _) = I := coe_to_submodule_injective (submodule.map_id I) @[simp] lemma map_comp (g' : P' →ₐ[R] P'') : I.map (g'.comp g) = (I.map g).map g' := coe_to_submodule_injective (submodule.map_comp g.to_linear_map g'.to_linear_map I) @[simp, norm_cast] lemma map_coe_ideal (I : ideal R) : (I : fractional_ideal S P).map g = I := begin ext x, simp only [mem_coe_ideal], split, { rintro ⟨_, ⟨y, hy, rfl⟩, rfl⟩, exact ⟨y, hy, (g.commutes y).symm⟩ }, { rintro ⟨y, hy, rfl⟩, exact ⟨_, ⟨y, hy, rfl⟩, g.commutes y⟩ }, end @[simp] lemma map_one : (1 : fractional_ideal S P).map g = 1 := map_coe_ideal g ⊤ @[simp] lemma map_zero : (0 : fractional_ideal S P).map g = 0 := map_coe_ideal g 0 @[simp] lemma map_add : (I + J).map g = I.map g + J.map g := coe_to_submodule_injective (submodule.map_sup _ _ _) @[simp] lemma map_mul : (I * J).map g = I.map g * J.map g := coe_to_submodule_injective (submodule.map_mul _ _ _) @[simp] lemma map_map_symm (g : P ≃ₐ[R] P') : (I.map (g : P →ₐ[R] P')).map (g.symm : P' →ₐ[R] P) = I := by rw [←map_comp, g.symm_comp, map_id] @[simp] lemma map_symm_map (I : fractional_ideal S P') (g : P ≃ₐ[R] P') : (I.map (g.symm : P' →ₐ[R] P)).map (g : P →ₐ[R] P') = I := by rw [←map_comp, g.comp_symm, map_id] lemma map_mem_map {f : P →ₐ[R] P'} (h : function.injective f) {x : P} {I : fractional_ideal S P} : f x ∈ map f I ↔ x ∈ I := mem_map.trans ⟨λ ⟨x', hx', x'_eq⟩, h x'_eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩ lemma map_injective (f : P →ₐ[R] P') (h : function.injective f) : function.injective (map f : fractional_ideal S P → fractional_ideal S P') := λ I J hIJ, fractional_ideal.ext (λ x, (fractional_ideal.map_mem_map h).symm.trans (hIJ.symm ▸ fractional_ideal.map_mem_map h)) /-- If `g` is an equivalence, `map g` is an isomorphism -/ def map_equiv (g : P ≃ₐ[R] P') : fractional_ideal S P ≃+* fractional_ideal S P' := { to_fun := map g, inv_fun := map g.symm, map_add' := λ I J, map_add I J _, map_mul' := λ I J, map_mul I J _, left_inv := λ I, by { rw [←map_comp, alg_equiv.symm_comp, map_id] }, right_inv := λ I, by { rw [←map_comp, alg_equiv.comp_symm, map_id] } } @[simp] lemma coe_fun_map_equiv (g : P ≃ₐ[R] P') : (map_equiv g : fractional_ideal S P → fractional_ideal S P') = map g := rfl @[simp] lemma map_equiv_apply (g : P ≃ₐ[R] P') (I : fractional_ideal S P) : map_equiv g I = map ↑g I := rfl @[simp] lemma map_equiv_symm (g : P ≃ₐ[R] P') : ((map_equiv g).symm : fractional_ideal S P' ≃+* _) = map_equiv g.symm := rfl @[simp] lemma map_equiv_refl : map_equiv alg_equiv.refl = ring_equiv.refl (fractional_ideal S P) := ring_equiv.ext (λ x, by simp) lemma is_fractional_span_iff {s : set P} : is_fractional S (span R s) ↔ ∃ a ∈ S, ∀ (b : P), b ∈ s → is_integer R (a • b) := ⟨λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, h b (subset_span hb)⟩, λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, span_induction hb h (by { rw smul_zero, exact is_integer_zero }) (λ x y hx hy, by { rw smul_add, exact is_integer_add hx hy }) (λ s x hx, by { rw smul_comm, exact is_integer_smul hx })⟩⟩ include loc lemma is_fractional_of_fg {I : submodule R P} (hI : I.fg) : is_fractional S I := begin rcases hI with ⟨I, rfl⟩, rcases exist_integer_multiples_of_finset S I with ⟨⟨s, hs1⟩, hs⟩, rw is_fractional_span_iff, exact ⟨s, hs1, hs⟩, end omit loc lemma mem_span_mul_finite_of_mem_mul {I J : fractional_ideal S P} {x : P} (hx : x ∈ I * J) : ∃ (T T' : finset P), (T : set P) ⊆ I ∧ (T' : set P) ⊆ J ∧ x ∈ span R (T * T' : set P) := submodule.mem_span_mul_finite_of_mem_mul (by simpa using mem_coe.mpr hx) variables (S) lemma coe_ideal_fg (inj : function.injective (algebra_map R P)) (I : ideal R) : fg ((I : fractional_ideal S P) : submodule R P) ↔ I.fg := coe_submodule_fg _ inj _ variables {S} lemma fg_unit (I : (fractional_ideal S P)ˣ) : fg (I : submodule R P) := begin have : (1 : P) ∈ (I * ↑I⁻¹ : fractional_ideal S P), { rw units.mul_inv, exact one_mem_one _ }, obtain ⟨T, T', hT, hT', one_mem⟩ := mem_span_mul_finite_of_mem_mul this, refine ⟨T, submodule.span_eq_of_le _ hT _⟩, rw [← one_mul ↑I, ← mul_one (span R ↑T)], conv_rhs { rw [← fractional_ideal.coe_one, ← units.mul_inv I, fractional_ideal.coe_mul, mul_comm ↑↑I, ← mul_assoc] }, refine submodule.mul_le_mul_left (le_trans _ (submodule.mul_le_mul_right (submodule.span_le.mpr hT'))), rwa [submodule.one_le, submodule.span_mul_span] end lemma fg_of_is_unit (I : fractional_ideal S P) (h : is_unit I) : fg (I : submodule R P) := by { rcases h with ⟨I, rfl⟩, exact fg_unit I } lemma _root_.ideal.fg_of_is_unit (inj : function.injective (algebra_map R P)) (I : ideal R) (h : is_unit (I : fractional_ideal S P)) : I.fg := by { rw ← coe_ideal_fg S inj I, exact fg_of_is_unit I h } variables (S P P') include loc loc' /-- `canonical_equiv f f'` is the canonical equivalence between the fractional ideals in `P` and in `P'` -/ @[irreducible] noncomputable def canonical_equiv : fractional_ideal S P ≃+* fractional_ideal S P' := map_equiv { commutes' := λ r, ring_equiv_of_ring_equiv_eq _ _, ..ring_equiv_of_ring_equiv P P' (ring_equiv.refl R) (show S.map _ = S, by rw [ring_equiv.to_monoid_hom_refl, submonoid.map_id]) } @[simp] lemma mem_canonical_equiv_apply {I : fractional_ideal S P} {x : P'} : x ∈ canonical_equiv S P P' I ↔ ∃ y ∈ I, is_localization.map P' (ring_hom.id R) (λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) (y : P) = x := begin rw [canonical_equiv, map_equiv_apply, mem_map], exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ end @[simp] lemma canonical_equiv_symm : (canonical_equiv S P P').symm = canonical_equiv S P' P := ring_equiv.ext $ λ I, set_like.ext_iff.mpr $ λ x, by { rw [mem_canonical_equiv_apply, canonical_equiv, map_equiv_symm, map_equiv, ring_equiv.coe_mk, mem_map], exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ } @[simp] lemma canonical_equiv_flip (I) : canonical_equiv S P P' (canonical_equiv S P' P I) = I := by rw [←canonical_equiv_symm, ring_equiv.symm_apply_apply] end semiring section is_fraction_ring /-! ### `is_fraction_ring` section This section concerns fractional ideals in the field of fractions, i.e. the type `fractional_ideal R⁰ K` where `is_fraction_ring R K`. -/ variables {K K' : Type*} [field K] [field K'] variables [algebra R K] [is_fraction_ring R K] [algebra R K'] [is_fraction_ring R K'] variables {I J : fractional_ideal R⁰ K} (h : K →ₐ[R] K') /-- Nonzero fractional ideals contain a nonzero integer. -/ lemma exists_ne_zero_mem_is_integer [nontrivial R] (hI : I ≠ 0) : ∃ x ≠ (0 : R), algebra_map R K x ∈ I := begin obtain ⟨y, y_mem, y_not_mem⟩ := set_like.exists_of_lt (by simpa only using bot_lt_iff_ne_bot.mpr hI), have y_ne_zero : y ≠ 0 := by simpa using y_not_mem, obtain ⟨z, ⟨x, hx⟩⟩ := exists_integer_multiple R⁰ y, refine ⟨x, _, _⟩, { rw [ne.def, ← @is_fraction_ring.to_map_eq_zero_iff R _ K, hx, algebra.smul_def], exact mul_ne_zero (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors z.2) y_ne_zero }, { rw hx, exact smul_mem _ _ y_mem } end lemma map_ne_zero [nontrivial R] (hI : I ≠ 0) : I.map h ≠ 0 := begin obtain ⟨x, x_ne_zero, hx⟩ := exists_ne_zero_mem_is_integer hI, contrapose! x_ne_zero with map_eq_zero, refine is_fraction_ring.to_map_eq_zero_iff.mp (eq_zero_iff.mp map_eq_zero _ (mem_map.mpr _)), exact ⟨algebra_map R K x, hx, h.commutes x⟩, end @[simp] lemma map_eq_zero_iff [nontrivial R] : I.map h = 0 ↔ I = 0 := ⟨imp_of_not_imp_not _ _ (map_ne_zero _), λ hI, hI.symm ▸ map_zero h⟩ lemma coe_ideal_injective : function.injective (coe : ideal R → fractional_ideal R⁰ K) := injective_of_le_imp_le _ (λ _ _, (coe_ideal_le_coe_ideal _).mp) @[simp] lemma coe_ideal_eq_zero_iff {I : ideal R} : (I : fractional_ideal R⁰ K) = 0 ↔ I = ⊥ := by { rw ← coe_to_fractional_ideal_bot, exact coe_ideal_injective.eq_iff } lemma coe_ideal_ne_zero_iff {I : ideal R} : (I : fractional_ideal R⁰ K) ≠ 0 ↔ I ≠ ⊥ := not_iff_not.mpr coe_ideal_eq_zero_iff lemma coe_ideal_ne_zero {I : ideal R} (hI : I ≠ ⊥) : (I : fractional_ideal R⁰ K) ≠ 0 := coe_ideal_ne_zero_iff.mpr hI end is_fraction_ring section quotient /-! ### `quotient` section This section defines the ideal quotient of fractional ideals. In this section we need that each non-zero `y : R` has an inverse in the localization, i.e. that the localization is a field. We satisfy this assumption by taking `S = non_zero_divisors R`, `R`'s localization at which is a field because `R` is a domain. -/ open_locale classical variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K] variables [algebra R₁ K] [frac : is_fraction_ring R₁ K] instance : nontrivial (fractional_ideal R₁⁰ K) := ⟨⟨0, 1, λ h, have this : (1 : K) ∈ (0 : fractional_ideal R₁⁰ K) := by { rw ← (algebra_map R₁ K).map_one, simpa only [h] using coe_mem_one R₁⁰ 1 }, one_ne_zero ((mem_zero_iff _).mp this)⟩⟩ lemma ne_zero_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : I ≠ 0 := λ hI, @zero_ne_one (fractional_ideal R₁⁰ K) _ _ (by { convert h, simp [hI], }) variables [is_domain R₁] include frac lemma fractional_div_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : is_fractional R₁⁰ (I / J : submodule R₁ K) := begin rcases I with ⟨I, aI, haI, hI⟩, rcases J with ⟨J, aJ, haJ, hJ⟩, obtain ⟨y, mem_J, not_mem_zero⟩ := set_like.exists_of_lt (by simpa only using bot_lt_iff_ne_bot.mpr h), obtain ⟨y', hy'⟩ := hJ y mem_J, use (aI * y'), split, { apply (non_zero_divisors R₁).mul_mem haI (mem_non_zero_divisors_iff_ne_zero.mpr _), intro y'_eq_zero, have : algebra_map R₁ K aJ * y = 0, { rw [← algebra.smul_def, ←hy', y'_eq_zero, ring_hom.map_zero] }, have y_zero := (mul_eq_zero.mp this).resolve_left (mt ((algebra_map R₁ K).injective_iff.1 (is_fraction_ring.injective _ _) _) (mem_non_zero_divisors_iff_ne_zero.mp haJ)), exact not_mem_zero ((mem_zero_iff R₁⁰).mpr y_zero) }, intros b hb, convert hI _ (hb _ (submodule.smul_mem _ aJ mem_J)) using 1, rw [← hy', mul_comm b, ← algebra.smul_def, mul_smul] end noncomputable instance fractional_ideal_has_div : has_div (fractional_ideal R₁⁰ K) := ⟨ λ I J, if h : J = 0 then 0 else ⟨I / J, fractional_div_of_nonzero h⟩ ⟩ variables {I J : fractional_ideal R₁⁰ K} [ J ≠ 0 ] @[simp] lemma div_zero {I : fractional_ideal R₁⁰ K} : I / 0 = 0 := dif_pos rfl lemma div_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : (I / J) = ⟨I / J, fractional_div_of_nonzero h⟩ := dif_neg h @[simp] lemma coe_div {I J : fractional_ideal R₁⁰ K} (hJ : J ≠ 0) : (↑(I / J) : submodule R₁ K) = ↑I / (↑J : submodule R₁ K) := begin unfold has_div.div, simp only [dif_neg hJ, coe_mk, val_eq_coe], end lemma mem_div_iff_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) {x} : x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I := by { rw div_nonzero h, exact submodule.mem_div_iff_forall_mul_mem } lemma mul_one_div_le_one {I : fractional_ideal R₁⁰ K} : I * (1 / I) ≤ 1 := begin by_cases hI : I = 0, { rw [hI, div_zero, mul_zero], exact zero_le 1 }, { rw [← coe_le_coe, coe_mul, coe_div hI, coe_one], apply submodule.mul_one_div_le_one }, end lemma le_self_mul_one_div {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) : I ≤ I * (1 / I) := begin by_cases hI_nz : I = 0, { rw [hI_nz, div_zero, mul_zero], exact zero_le 0 }, { rw [← coe_le_coe, coe_mul, coe_div hI_nz, coe_one], rw [← coe_le_coe, coe_one] at hI, exact submodule.le_self_mul_one_div hI }, end lemma le_div_iff_of_nonzero {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) : I ≤ J / J' ↔ ∀ (x ∈ I) (y ∈ J'), x * y ∈ J := ⟨ λ h x hx, (mem_div_iff_of_nonzero hJ').mp (h hx), λ h x hx, (mem_div_iff_of_nonzero hJ').mpr (h x hx) ⟩ lemma le_div_iff_mul_le {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) : I ≤ J / J' ↔ I * J' ≤ J := begin rw div_nonzero hJ', convert submodule.le_div_iff_mul_le using 1, rw [← coe_mul, coe_le_coe] end @[simp] lemma div_one {I : fractional_ideal R₁⁰ K} : I / 1 = I := begin rw [div_nonzero (@one_ne_zero (fractional_ideal R₁⁰ K) _ _)], ext, split; intro h, { simpa using mem_div_iff_forall_mul_mem.mp h 1 ((algebra_map R₁ K).map_one ▸ coe_mem_one R₁⁰ 1) }, { apply mem_div_iff_forall_mul_mem.mpr, rintros y ⟨y', _, rfl⟩, rw mul_comm, convert submodule.smul_mem _ y' h, exact (algebra.smul_def _ _).symm } end theorem eq_one_div_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : J = 1 / 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_div_self_cancel_iff {I : fractional_ideal R₁⁰ K} : I * (1 / I) = 1 ↔ ∃ J, I * J = 1 := ⟨λ h, ⟨(1 / I), h⟩, λ ⟨J, hJ⟩, by rwa [← eq_one_div_of_mul_eq_one I J hJ]⟩ variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K'] @[simp] lemma map_div (I J : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (I / J).map (h : K →ₐ[R₁] K') = I.map h / J.map h := begin by_cases H : J = 0, { rw [H, div_zero, map_zero, div_zero] }, { apply coe_to_submodule_injective, simp [div_nonzero H, div_nonzero (map_ne_zero _ H), submodule.map_div] } end @[simp] lemma map_one_div (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (1 / I).map (h : K →ₐ[R₁] K') = 1 / I.map h := by rw [map_div, map_one] end quotient section field variables {R₁ K L : Type*} [comm_ring R₁] [is_domain R₁] [field K] [field L] variables [algebra R₁ K] [is_fraction_ring R₁ K] [algebra K L] [is_fraction_ring K L] lemma eq_zero_or_one (I : fractional_ideal K⁰ L) : I = 0 ∨ I = 1 := begin rw or_iff_not_imp_left, intro hI, simp_rw [@set_like.ext_iff _ _ _ I 1, fractional_ideal.mem_one_iff], intro x, split, { intro x_mem, obtain ⟨n, d, rfl⟩ := is_localization.mk'_surjective K⁰ x, refine ⟨n / d, _⟩, rw [ring_hom.map_div, is_fraction_ring.mk'_eq_div] }, { rintro ⟨x, rfl⟩, obtain ⟨y, y_ne, y_mem⟩ := fractional_ideal.exists_ne_zero_mem_is_integer hI, rw [← div_mul_cancel x y_ne, ring_hom.map_mul, ← algebra.smul_def], exact submodule.smul_mem I _ y_mem } end lemma eq_zero_or_one_of_is_field (hF : is_field R₁) (I : fractional_ideal R₁⁰ K) : I = 0 ∨ I = 1 := begin letI : field R₁ := hF.to_field R₁, -- TODO can this be less ugly? exact @eq_zero_or_one R₁ K _ _ _ (by { unfreezingI {cases _inst_4}, convert _inst_9 }) I end end field section principal_ideal_ring variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K] variables [algebra R₁ K] [is_fraction_ring R₁ K] open_locale classical open submodule.is_principal include loc lemma is_fractional_span_singleton (x : P) : is_fractional S (span R {x} : submodule R P) := let ⟨a, ha⟩ := exists_integer_multiple S x in is_fractional_span_iff.mpr ⟨a, a.2, λ x' hx', (set.mem_singleton_iff.mp hx').symm ▸ ha⟩ variables (S) /-- `span_singleton x` is the fractional ideal generated by `x` if `0 ∉ S` -/ @[irreducible] def span_singleton (x : P) : fractional_ideal S P := ⟨span R {x}, is_fractional_span_singleton x⟩ local attribute [semireducible] span_singleton @[simp] lemma coe_span_singleton (x : P) : (span_singleton S x : submodule R P) = span R {x} := rfl @[simp] lemma mem_span_singleton {x y : P} : x ∈ span_singleton S y ↔ ∃ (z : R), z • y = x := submodule.mem_span_singleton lemma mem_span_singleton_self (x : P) : x ∈ span_singleton S x := (mem_span_singleton S).mpr ⟨1, one_smul _ _⟩ variables {S} lemma eq_span_singleton_of_principal (I : fractional_ideal S P) [is_principal (I : submodule R P)] : I = span_singleton S (generator (I : submodule R P)) := coe_to_submodule_injective (span_singleton_generator ↑I).symm lemma is_principal_iff (I : fractional_ideal S P) : is_principal (I : submodule R P) ↔ ∃ x, I = span_singleton S x := ⟨λ h, ⟨@generator _ _ _ _ _ ↑I h, @eq_span_singleton_of_principal _ _ _ _ _ _ _ I h⟩, λ ⟨x, hx⟩, { principal := ⟨x, trans (congr_arg _ hx) (coe_span_singleton _ x)⟩ } ⟩ @[simp] lemma span_singleton_zero : span_singleton S (0 : P) = 0 := by { ext, simp [submodule.mem_span_singleton, eq_comm] } lemma span_singleton_eq_zero_iff {y : P} : span_singleton S y = 0 ↔ y = 0 := ⟨λ h, span_eq_bot.mp (by simpa using congr_arg subtype.val h : span R {y} = ⊥) y (mem_singleton y), λ h, by simp [h] ⟩ lemma span_singleton_ne_zero_iff {y : P} : span_singleton S y ≠ 0 ↔ y ≠ 0 := not_congr span_singleton_eq_zero_iff @[simp] lemma span_singleton_one : span_singleton S (1 : P) = 1 := begin ext, refine (mem_span_singleton S).trans ((exists_congr _).trans (mem_one_iff S).symm), intro x', rw [algebra.smul_def, mul_one] end @[simp] lemma span_singleton_mul_span_singleton (x y : P) : span_singleton S x * span_singleton S y = span_singleton S (x * y) := begin apply coe_to_submodule_injective, simp only [coe_mul, coe_span_singleton, span_mul_span, singleton_mul_singleton], end @[simp] lemma coe_ideal_span_singleton (x : R) : (↑(ideal.span {x} : ideal R) : fractional_ideal S P) = span_singleton S (algebra_map R P x) := begin ext y, refine (mem_coe_ideal S).trans (iff.trans _ (mem_span_singleton S).symm), split, { rintros ⟨y', hy', rfl⟩, obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hy', use x', rw [smul_eq_mul, ring_hom.map_mul, algebra.smul_def] }, { rintros ⟨y', rfl⟩, refine ⟨y' * x, submodule.mem_span_singleton.mpr ⟨y', rfl⟩, _⟩, rw [ring_hom.map_mul, algebra.smul_def] } end @[simp] lemma canonical_equiv_span_singleton {P'} [comm_ring P'] [algebra R P'] [is_localization S P'] (x : P) : canonical_equiv S P P' (span_singleton S x) = span_singleton S (is_localization.map P' (ring_hom.id R) (λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) x) := begin apply set_like.ext_iff.mpr, intro y, split; intro h, { rw mem_span_singleton, obtain ⟨x', hx', rfl⟩ := (mem_canonical_equiv_apply _ _ _).mp h, obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp hx', use z, rw is_localization.map_smul, refl }, { rw mem_canonical_equiv_apply, obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp h, use z • x, use (mem_span_singleton _).mpr ⟨z, rfl⟩, simp [is_localization.map_smul] } end lemma mem_singleton_mul {x y : P} {I : fractional_ideal S P} : y ∈ span_singleton S x * I ↔ ∃ y' ∈ I, y = x * y' := begin split, { intro h, apply fractional_ideal.mul_induction_on h, { intros x' hx' y' hy', obtain ⟨a, ha⟩ := (mem_span_singleton S).mp hx', use [a • y', submodule.smul_mem I a hy'], rw [←ha, algebra.mul_smul_comm, algebra.smul_mul_assoc] }, { rintros _ _ ⟨y, hy, rfl⟩ ⟨y', hy', rfl⟩, exact ⟨y + y', submodule.add_mem I hy hy', (mul_add _ _ _).symm⟩ } }, { rintros ⟨y', hy', rfl⟩, exact mul_mem_mul ((mem_span_singleton S).mpr ⟨1, one_smul _ _⟩) hy' } end omit loc variables (K) lemma mk'_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {x y : R₁} (hy : y ∈ R₁⁰) : span_singleton R₁⁰ (is_localization.mk' K x ⟨y, hy⟩) * I = (J : fractional_ideal R₁⁰ K) ↔ ideal.span {x} * I = ideal.span {y} * J := begin have inj : function.injective (coe : ideal R₁ → fractional_ideal R₁⁰ K) := fractional_ideal.coe_ideal_injective, have : span_singleton R₁⁰ (is_localization.mk' _ (1 : R₁) ⟨y, hy⟩) * span_singleton R₁⁰ (algebra_map R₁ K y) = 1, { rw [span_singleton_mul_span_singleton, mul_comm, ← is_localization.mk'_eq_mul_mk'_one, is_localization.mk'_self, span_singleton_one] }, let y' : (fractional_ideal R₁⁰ K)ˣ := units.mk_of_mul_eq_one _ _ this, have coe_y' : ↑y' = span_singleton R₁⁰ (is_localization.mk' K (1 : R₁) ⟨y, hy⟩) := rfl, refine iff.trans _ (y'.mul_right_inj.trans inj.eq_iff), rw [coe_y', coe_ideal_mul, coe_ideal_span_singleton, coe_ideal_mul, coe_ideal_span_singleton, ←mul_assoc, span_singleton_mul_span_singleton, ←mul_assoc, span_singleton_mul_span_singleton, mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one, mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one, is_localization.mk'_self, span_singleton_one, one_mul], end variables {K} lemma span_singleton_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {z : K} : span_singleton R₁⁰ z * (I : fractional_ideal R₁⁰ K) = J ↔ ideal.span {((is_localization.sec R₁⁰ z).1 : R₁)} * I = ideal.span {(is_localization.sec R₁⁰ z).2} * J := -- `erw` to deal with the distinction between `y` and `⟨y.1, y.2⟩` by erw [← mk'_mul_coe_ideal_eq_coe_ideal K (is_localization.sec R₁⁰ z).2.prop, is_localization.mk'_sec K z] variables [is_domain R₁] lemma one_div_span_singleton (x : K) : 1 / span_singleton R₁⁰ x = span_singleton R₁⁰ (x⁻¹) := if h : x = 0 then by simp [h] else (eq_one_div_of_mul_eq_one _ _ (by simp [h])).symm @[simp] lemma div_span_singleton (J : fractional_ideal R₁⁰ K) (d : K) : J / span_singleton R₁⁰ d = span_singleton R₁⁰ (d⁻¹) * J := begin rw ← one_div_span_singleton, by_cases hd : d = 0, { simp only [hd, span_singleton_zero, div_zero, zero_mul] }, have h_spand : span_singleton R₁⁰ d ≠ 0 := mt span_singleton_eq_zero_iff.mp hd, apply le_antisymm, { intros x hx, rw [← mem_coe, coe_div h_spand, submodule.mem_div_iff_forall_mul_mem] at hx, specialize hx d (mem_span_singleton_self R₁⁰ d), have h_xd : x = d⁻¹ * (x * d), { field_simp }, rw [← mem_coe, coe_mul, one_div_span_singleton, h_xd], exact submodule.mul_mem_mul (mem_span_singleton_self R₁⁰ _) hx }, { rw [le_div_iff_mul_le h_spand, mul_assoc, mul_left_comm, one_div_span_singleton, span_singleton_mul_span_singleton, inv_mul_cancel hd, span_singleton_one, mul_one], exact le_refl J }, end lemma exists_eq_span_singleton_mul (I : fractional_ideal R₁⁰ K) : ∃ (a : R₁) (aI : ideal R₁), a ≠ 0 ∧ I = span_singleton R₁⁰ (algebra_map R₁ K a)⁻¹ * aI := begin obtain ⟨a_inv, nonzero, ha⟩ := I.is_fractional, have nonzero := mem_non_zero_divisors_iff_ne_zero.mp nonzero, have map_a_nonzero : algebra_map R₁ K a_inv ≠ 0 := mt is_fraction_ring.to_map_eq_zero_iff.mp nonzero, refine ⟨a_inv, submodule.comap (algebra.linear_map R₁ K) ↑(span_singleton R₁⁰ (algebra_map R₁ K a_inv) * I), nonzero, ext (λ x, iff.trans ⟨_, _⟩ mem_singleton_mul.symm)⟩, { intro hx, obtain ⟨x', hx'⟩ := ha x hx, rw algebra.smul_def at hx', refine ⟨algebra_map R₁ K x', (mem_coe_ideal _).mpr ⟨x', mem_singleton_mul.mpr _, rfl⟩, _⟩, { exact ⟨x, hx, hx'⟩ }, { rw [hx', ← mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } }, { rintros ⟨y, hy, rfl⟩, obtain ⟨x', hx', rfl⟩ := (mem_coe_ideal _).mp hy, obtain ⟨y', hy', hx'⟩ := mem_singleton_mul.mp hx', rw algebra.linear_map_apply at hx', rwa [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } end instance is_principal {R} [comm_ring R] [is_domain R] [is_principal_ideal_ring R] [algebra R K] [is_fraction_ring R K] (I : fractional_ideal R⁰ K) : (I : submodule R K).is_principal := begin obtain ⟨a, aI, -, ha⟩ := exists_eq_span_singleton_mul I, use (algebra_map R K a)⁻¹ * algebra_map R K (generator aI), suffices : I = span_singleton R⁰ ((algebra_map R K a)⁻¹ * algebra_map R K (generator aI)), { exact congr_arg subtype.val this }, conv_lhs { rw [ha, ←span_singleton_generator aI] }, rw [ideal.submodule_span_eq, coe_ideal_span_singleton (generator aI), span_singleton_mul_span_singleton] end include loc lemma le_span_singleton_mul_iff {x : P} {I J : fractional_ideal S P} : I ≤ span_singleton S x * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI := show (∀ {zI} (hzI : zI ∈ I), zI ∈ span_singleton _ x * J) ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI, by simp only [fractional_ideal.mem_singleton_mul, eq_comm] lemma span_singleton_mul_le_iff {x : P} {I J : fractional_ideal S P} : span_singleton _ x * I ≤ J ↔ ∀ z ∈ I, x * z ∈ J := begin simp only [fractional_ideal.mul_le, fractional_ideal.mem_singleton_mul, fractional_ideal.mem_span_singleton], split, { intros h zI hzI, exact h x ⟨1, one_smul _ _⟩ zI hzI }, { rintros h _ ⟨z, rfl⟩ zI hzI, rw [algebra.smul_mul_assoc], exact submodule.smul_mem J.1 _ (h zI hzI) }, end lemma eq_span_singleton_mul {x : P} {I J : fractional_ideal S P} : I = span_singleton _ x * J ↔ (∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI) ∧ ∀ z ∈ J, x * z ∈ I := by simp only [le_antisymm_iff, fractional_ideal.le_span_singleton_mul_iff, fractional_ideal.span_singleton_mul_le_iff] end principal_ideal_ring variables {R₁ : Type*} [comm_ring R₁] variables {K : Type*} [field K] [algebra R₁ K] [frac : is_fraction_ring R₁ K] local attribute [instance] classical.prop_decidable lemma is_noetherian_zero : is_noetherian R₁ (0 : fractional_ideal R₁⁰ K) := is_noetherian_submodule.mpr (λ I (hI : I ≤ (0 : fractional_ideal R₁⁰ K)), by { rw coe_zero at hI, rw le_bot_iff.mp hI, exact fg_bot }) lemma is_noetherian_iff {I : fractional_ideal R₁⁰ K} : is_noetherian R₁ I ↔ ∀ J ≤ I, (J : submodule R₁ K).fg := is_noetherian_submodule.trans ⟨λ h J hJ, h _ hJ, λ h J hJ, h ⟨J, is_fractional_of_le hJ⟩ hJ⟩ lemma is_noetherian_coe_to_fractional_ideal [_root_.is_noetherian_ring R₁] (I : ideal R₁) : is_noetherian R₁ (I : fractional_ideal R₁⁰ K) := begin rw is_noetherian_iff, intros J hJ, obtain ⟨J, rfl⟩ := le_one_iff_exists_coe_ideal.mp (le_trans hJ coe_ideal_le_one), exact (is_noetherian.noetherian J).map _, end include frac variables [is_domain R₁] lemma is_noetherian_span_singleton_inv_to_map_mul (x : R₁) {I : fractional_ideal R₁⁰ K} (hI : is_noetherian R₁ I) : is_noetherian R₁ (span_singleton R₁⁰ (algebra_map R₁ K x)⁻¹ * I : fractional_ideal R₁⁰ K) := begin by_cases hx : x = 0, { rw [hx, ring_hom.map_zero, _root_.inv_zero, span_singleton_zero, zero_mul], exact is_noetherian_zero }, have h_gx : algebra_map R₁ K x ≠ 0, from mt ((algebra_map R₁ K).injective_iff.mp (is_fraction_ring.injective _ _) x) hx, have h_spanx : span_singleton R₁⁰ (algebra_map R₁ K x) ≠ 0, from span_singleton_ne_zero_iff.mpr h_gx, rw is_noetherian_iff at ⊢ hI, intros J hJ, rw [← div_span_singleton, le_div_iff_mul_le h_spanx] at hJ, obtain ⟨s, hs⟩ := hI _ hJ, use s * {(algebra_map R₁ K x)⁻¹}, rw [finset.coe_mul, finset.coe_singleton, ← span_mul_span, hs, ← coe_span_singleton R₁⁰, ← coe_mul, mul_assoc, span_singleton_mul_span_singleton, mul_inv_cancel h_gx, span_singleton_one, mul_one], end /-- Every fractional ideal of a noetherian integral domain is noetherian. -/ theorem is_noetherian [_root_.is_noetherian_ring R₁] (I : fractional_ideal R₁⁰ K) : is_noetherian R₁ I := begin obtain ⟨d, J, h_nzd, rfl⟩ := exists_eq_span_singleton_mul I, apply is_noetherian_span_singleton_inv_to_map_mul, apply is_noetherian_coe_to_fractional_ideal, end section adjoin include loc omit frac variables {R P} (S) (x : P) (hx : is_integral R x) /-- `A[x]` is a fractional ideal for every integral `x`. -/ lemma is_fractional_adjoin_integral : is_fractional S (algebra.adjoin R ({x} : set P)).to_submodule := is_fractional_of_fg (fg_adjoin_singleton_of_integral x hx) /-- `fractional_ideal.adjoin_integral (S : submonoid R) x hx` is `R[x]` as a fractional ideal, where `hx` is a proof that `x : P` is integral over `R`. -/ @[simps] def adjoin_integral : fractional_ideal S P := ⟨_, is_fractional_adjoin_integral S x hx⟩ lemma mem_adjoin_integral_self : x ∈ adjoin_integral S x hx := algebra.subset_adjoin (set.mem_singleton x) end adjoin end fractional_ideal
cf91d9748a682171f018e21a9d7bb1a4ccaa2742
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/PreDefinition/Basic.lean
6681c7f5c3e03c95a08fb5c6a4d6496aef7dca49
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
6,642
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.Util.SCC import Lean.Meta.AbstractNestedProofs import Lean.Elab.Term import Lean.Elab.DefView import Lean.Elab.PreDefinition.MkInhabitant namespace Lean.Elab open Meta open Term /-- A (potentially recursive) definition. The elaborator converts it into Kernel definitions using many different strategies. -/ structure PreDefinition where ref : Syntax kind : DefKind levelParams : List Name modifiers : Modifiers declName : Name type : Expr value : Expr deriving Inhabited def instantiateMVarsAtPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do pure { preDef with type := (← instantiateMVars preDef.type), value := (← instantiateMVars preDef.value) } private def levelMVarToParamPreDeclsAux (preDefs : Array PreDefinition) : StateRefT Nat TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do pure { preDef with type := (← levelMVarToParam' preDef.type), value := (← levelMVarToParam' preDef.value) } def levelMVarToParamPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := (levelMVarToParamPreDeclsAux preDefs).run' 1 private def getLevelParamsPreDecls (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (List Name) := do let mut s : CollectLevelParams.State := {} for preDef in preDefs do s := collectLevelParams s preDef.type s := collectLevelParams s preDef.value match sortDeclLevelParams scopeLevelNames allUserLevelNames s.params with | Except.error msg => throwError msg | Except.ok levelParams => pure levelParams def fixLevelParams (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (Array PreDefinition) := do -- We used to use `shareCommon` here, but is was a bottleneck let levelParams ← getLevelParamsPreDecls preDefs scopeLevelNames allUserLevelNames let us := levelParams.map mkLevelParam let fixExpr (e : Expr) : Expr := e.replace fun c => match c with | Expr.const declName _ _ => if preDefs.any fun preDef => preDef.declName == declName then some $ Lean.mkConst declName us else none | _ => none pure $ preDefs.map fun preDef => { preDef with type := fixExpr preDef.type, value := fixExpr preDef.value, levelParams := levelParams } def applyAttributesOf (preDefs : Array PreDefinition) (applicationTime : AttributeApplicationTime) : TermElabM Unit := do for preDef in preDefs do applyAttributesAt preDef.declName preDef.modifiers.attrs applicationTime def abstractNestedProofs (preDef : PreDefinition) : MetaM PreDefinition := if preDef.kind.isTheorem || preDef.kind.isExample then pure preDef else do let value ← Meta.abstractNestedProofs preDef.declName preDef.value pure { preDef with value := value } /- Auxiliary method for (temporarily) adding pre definition as an axiom -/ def addAsAxiom (preDef : PreDefinition) : MetaM Unit := do withRef preDef.ref do addDecl <| Declaration.axiomDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, isUnsafe := preDef.modifiers.isUnsafe } private def shouldGenCodeFor (preDef : PreDefinition) : Bool := !preDef.kind.isTheorem && !preDef.modifiers.isNoncomputable private def addNonRecAux (preDef : PreDefinition) (compile : Bool) : TermElabM Unit := withRef preDef.ref do let preDef ← abstractNestedProofs preDef let env ← getEnv let decl := match preDef.kind with | DefKind.«theorem» => Declaration.thmDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value } | DefKind.«opaque» => Declaration.opaqueDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, isUnsafe := preDef.modifiers.isUnsafe } | DefKind.«abbrev» => Declaration.defnDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.«abbrev», safety := if preDef.modifiers.isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe } | _ => -- definitions and examples Declaration.defnDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.regular (getMaxHeight env preDef.value + 1), safety := if preDef.modifiers.isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe } addDecl decl applyAttributesOf #[preDef] AttributeApplicationTime.afterTypeChecking if compile && shouldGenCodeFor preDef then compileDecl decl applyAttributesOf #[preDef] AttributeApplicationTime.afterCompilation def addAndCompileNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef true def addNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef false def addAndCompileUnsafe (preDefs : Array PreDefinition) (safety := DefinitionSafety.unsafe) : TermElabM Unit := withRef preDefs[0].ref do let decl := Declaration.mutualDefnDecl $ preDefs.toList.map fun preDef => { name := preDef.declName levelParams := preDef.levelParams type := preDef.type value := preDef.value safety := safety hints := ReducibilityHints.opaque } addDecl decl applyAttributesOf preDefs AttributeApplicationTime.afterTypeChecking compileDecl decl applyAttributesOf preDefs AttributeApplicationTime.afterCompilation pure () def addAndCompilePartialRec (preDefs : Array PreDefinition) : TermElabM Unit := do if preDefs.all shouldGenCodeFor then addAndCompileUnsafe (safety := DefinitionSafety.partial) <| preDefs.map fun preDef => { preDef with declName := Compiler.mkUnsafeRecName preDef.declName, value := preDef.value.replace fun e => match e with | Expr.const declName us _ => if preDefs.any fun preDef => preDef.declName == declName then some $ mkConst (Compiler.mkUnsafeRecName declName) us else none | _ => none, modifiers := {} } end Lean.Elab
7fbd5240d5753e90cad54419bd6f114823e769c6
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/tactic/alias.lean
1863a7089dcd77ce28b708568e29536317b87a1e
[ "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
4,437
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro This file defines an alias command, which can be used to create copies of a theorem or definition with different names. Syntax: / -- doc string - / alias my_theorem ← alias1 alias2 ... This produces defs or theorems of the form: / -- doc string - / @[alias] theorem alias1 : <type of my_theorem> := my_theorem / -- doc string - / @[alias] theorem alias2 : <type of my_theorem> := my_theorem Iff alias syntax: alias A_iff_B ↔ B_of_A A_of_B alias A_iff_B ↔ .. This gets an existing biconditional theorem A_iff_B and produces the one-way implications B_of_A and A_of_B (with no change in implicit arguments). A blank _ can be used to avoid generating one direction. The .. notation attempts to generate the 'of'-names automatically when the input theorem has the form A_iff_B or A_iff_B_left etc. -/ import data.buffer.parser data.list.basic open lean.parser tactic interactive parser namespace tactic.alias @[user_attribute] meta def alias_attr : user_attribute := { name := `alias, descr := "This definition is an alias of another." } meta def alias_direct (d : declaration) (doc : string) (al : name) : tactic unit := do updateex_env $ λ env, env.add (match d.to_definition with | declaration.defn n ls t _ _ _ := declaration.defn al ls t (expr.const n (level.param <$> ls)) reducibility_hints.abbrev tt | declaration.thm n ls t _ := declaration.thm al ls t $ task.pure $ expr.const n (level.param <$> ls) | _ := undefined end), set_basic_attribute `alias al, add_doc_string al doc meta def mk_iff_mp_app (iffmp : name) : expr → (nat → expr) → tactic expr | (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (λ n, f (n+1) (expr.var n)) | `(%%a ↔ %%b) f := pure $ @expr.const tt iffmp [] a b (f 0) | _ f := fail "Target theorem must have the form `Π x y z, a ↔ b`" meta def alias_iff (d : declaration) (doc : string) (al : name) (iffmp : name) : tactic unit := (if al = `_ then skip else get_decl al >> skip) <|> do let ls := d.univ_params, let t := d.type, v ← mk_iff_mp_app iffmp t (λ_, expr.const d.to_name (level.param <$> ls)), t' ← infer_type v, updateex_env $ λ env, env.add (declaration.thm al ls t' $ task.pure v), set_basic_attribute `alias al, add_doc_string al doc meta def make_left_right : name → tactic (name × name) | (name.mk_string s p) := do let buf : char_buffer := s.to_char_buffer, sum.inr parts ← pure $ run (sep_by1 (ch '_') (many_char (sat (≠ '_')))) s.to_char_buffer, (left, _::right) ← pure $ parts.span (≠ "iff"), let pfx (a b : string) := a.to_list <+: b.to_list, (suffix', right') ← pure $ right.reverse.span (λ s, pfx "left" s ∨ pfx "right" s), let right := right'.reverse, let suffix := suffix'.reverse, pure (p <.> "_".intercalate (right ++ "of" :: left ++ suffix), p <.> "_".intercalate (left ++ "of" :: right ++ suffix)) | _ := failed @[user_command] meta def alias_cmd (meta_info : decl_meta_info) (_ : parse $ tk "alias") : lean.parser unit := do old ← ident, d ← (do old ← resolve_constant old, get_decl old) <|> fail ("declaration " ++ to_string old ++ " not found"), let doc := λ al : name, meta_info.doc_string.get_or_else $ "**Alias** of `" ++ to_string old ++ "`.", do { tk "←" <|> tk "<-", aliases ← many ident, ↑(aliases.mmap' $ λ al, alias_direct d (doc al) al) } <|> do { tk "↔" <|> tk "<->", (left, right) ← mcond ((tk "." *> tk "." >> pure tt) <|> pure ff) (make_left_right old <|> fail "invalid name for automatic name generation") (prod.mk <$> types.ident_ <*> types.ident_), alias_iff d (doc left) left `iff.mp, alias_iff d (doc right) right `iff.mpr } meta def get_lambda_body : expr → expr | (expr.lam _ _ _ b) := get_lambda_body b | a := a meta def get_alias_target (n : name) : tactic (option name) := do attr ← try_core (has_attribute `alias n), option.cases_on attr (pure none) $ λ_, do d ← get_decl n, let (head, args) := (get_lambda_body d.value).get_app_fn_args, let head := if head.is_constant_of `iff.mp ∨ head.is_constant_of `iff.mpr then expr.get_app_fn (head.ith_arg 2) else head, guardb $ head.is_constant, pure $ head.const_name end tactic.alias
f20e15fcc0da10a238ed0657096915558c679983
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/algebra/group_completion.lean
cccfe7a9ab6d847399828ef9db47bc2837bd018d
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
7,995
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import algebra.group.hom_instances import topology.uniform_space.completion import topology.algebra.uniform_group /-! # Completion of topological groups: This files endows the completion of a topological abelian group with a group structure. More precisely the instance `uniform_space.completion.add_group` builds an abelian group structure on the completion of an abelian group endowed with a compatible uniform structure. Then the instance `uniform_space.completion.uniform_add_group` proves this group structure is compatible with the completed uniform structure. The compatibility condition is `uniform_add_group`. ## Main declarations: Beyond the instances explained above (that don't have to be explicitly invoked), the main constructions deal with continuous group morphisms. * `add_monoid_hom.extension`: extends a continuous group morphism from `G` to a complete separated group `H` to `completion G`. * `add_monoid_hom.completion`: promotes a continuous group morphism from `G` to `H` into a continuous group morphism from `completion G` to `completion H`. -/ noncomputable theory universes u v section group open uniform_space Cauchy filter set variables {α : Type u} [uniform_space α] instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩ instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩ instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩ instance [has_sub α] : has_sub (completion α) := ⟨completion.map₂ has_sub.sub⟩ @[norm_cast] lemma uniform_space.completion.coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl end group namespace uniform_space.completion section uniform_add_group open uniform_space uniform_space.completion variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α] @[norm_cast] lemma coe_neg (a : α) : ((- a : α) : completion α) = - a := (map_coe uniform_continuous_neg a).symm @[norm_cast] lemma coe_sub (a b : α) : ((a - b : α) : completion α) = a - b := (map₂_coe_coe a b has_sub.sub uniform_continuous_sub).symm @[norm_cast] lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b := (map₂_coe_coe a b (+) uniform_continuous_add).symm instance : add_monoid (completion α) := { zero_add := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id) (assume a, show 0 + (a : completion α) = a, by rw_mod_cast zero_add), add_zero := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id) (assume a, show (a : completion α) + 0 = a, by rw_mod_cast add_zero), add_assoc := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_map₂ (continuous_map₂ continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd)) (continuous_map₂ continuous_fst (continuous_map₂ (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd)))) (assume a b c, show (a : completion α) + b + c = a + (b + c), by repeat { rw_mod_cast add_assoc }), .. completion.has_zero, .. completion.has_neg, ..completion.has_add, .. completion.has_sub } instance : sub_neg_monoid (completion α) := { sub_eq_add_neg := λ a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_fst (completion.continuous_map.comp continuous_snd))) (λ a b, by exact_mod_cast congr_arg coe (sub_eq_add_neg a b)), .. completion.add_monoid, .. completion.has_neg, .. completion.has_sub } instance : add_group (completion α) := { add_left_neg := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const) (assume a, show - (a : completion α) + a = 0, by { rw_mod_cast add_left_neg, refl }), .. completion.sub_neg_monoid } instance : uniform_add_group (completion α) := ⟨uniform_continuous_map₂ has_sub.sub⟩ /-- The map from a group to its completion as a group hom. -/ @[simps] def to_compl : α →+ completion α := { to_fun := coe, map_add' := coe_add, map_zero' := coe_zero } lemma continuous_to_compl : continuous (to_compl : α → completion α) := continuous_coe α variables {β : Type v} [uniform_space β] [add_group β] [uniform_add_group β] instance {α : Type u} [uniform_space α] [add_comm_group α] [uniform_add_group α] : add_comm_group (completion α) := { add_comm := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_snd continuous_fst)) (assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}), .. completion.add_group } end uniform_add_group end uniform_space.completion section add_monoid_hom variables {α β : Type*} [uniform_space α] [add_group α] [uniform_add_group α] [uniform_space β] [add_group β] [uniform_add_group β] open uniform_space uniform_space.completion /-- Extension to the completion of a continuous group hom. -/ def add_monoid_hom.extension [complete_space β] [separated_space β] (f : α →+ β) (hf : continuous f) : completion α →+ β := have hf : uniform_continuous f, from uniform_continuous_add_monoid_hom_of_continuous hf, { to_fun := completion.extension f, map_zero' := by rw [← coe_zero, extension_coe hf, f.map_zero], map_add' := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_extension.comp continuous_add) ((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd))) (λ a b, by rw_mod_cast [extension_coe hf, extension_coe hf, extension_coe hf, f.map_add]) } lemma add_monoid_hom.extension_coe [complete_space β] [separated_space β] (f : α →+ β) (hf : continuous f) (a : α) : f.extension hf a = f a := extension_coe (uniform_continuous_add_monoid_hom_of_continuous hf) a @[continuity] lemma add_monoid_hom.continuous_extension [complete_space β] [separated_space β] (f : α →+ β) (hf : continuous f) : continuous (f.extension hf) := continuous_extension /-- Completion of a continuous group hom, as a group hom. -/ def add_monoid_hom.completion (f : α →+ β) (hf : continuous f) : completion α →+ completion β := (to_compl.comp f).extension (continuous_to_compl.comp hf) @[continuity] lemma add_monoid_hom.continuous_completion (f : α →+ β) (hf : continuous f) : continuous (f.completion hf : completion α → completion β) := continuous_map lemma add_monoid_hom.completion_coe (f : α →+ β) (hf : continuous f) (a : α) : f.completion hf a = f a := map_coe (uniform_continuous_add_monoid_hom_of_continuous hf) a lemma add_monoid_hom.completion_zero : (0 : α →+ β).completion continuous_const = 0 := begin ext x, apply completion.induction_on x, { apply is_closed_eq ((0 : α →+ β).continuous_completion continuous_const), simp [continuous_const] }, { intro a, simp [(0 : α →+ β).completion_coe continuous_const, coe_zero] } end lemma add_monoid_hom.completion_add {γ : Type*} [add_comm_group γ] [uniform_space γ] [uniform_add_group γ] (f g : α →+ γ) (hf : continuous f) (hg : continuous g) : (f + g).completion (hf.add hg) = f.completion hf + g.completion hg := begin have hfg := hf.add hg, ext x, apply completion.induction_on x, { exact is_closed_eq ((f+g).continuous_completion hfg) ((f.continuous_completion hf).add (g.continuous_completion hg)) }, { intro a, simp [(f+g).completion_coe hfg, coe_add, f.completion_coe hf, g.completion_coe hg] } end end add_monoid_hom
9e73dac1df8aefe347b2573734e27c57a872ddde
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1575.lean
47e677127e99340f2d55a5331d16671e29b16f81
[ "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
143
lean
example [Subsingleton α] (p : α → Prop) : Subsingleton (Subtype p) := ⟨fun ⟨x, _⟩ ⟨y, _⟩ => Subsingleton.elim x y ▸ sorry⟩
795f489cd38e8d8d0b015bd85f27bf4a161b3987
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/field/defs.lean
58294ae15fda096e2504396053bd6b4467a0bbe4
[ "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
1,719
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Mario Carneiro, Floris van Doorn -/ import algebra.field.defs import algebra.order.ring.defs /-! # Linear ordered (semi)fields A linear ordered (semi)field is a (semi)field equipped with a linear order such that * addition respects the order: `a ≤ b → c + a ≤ c + b`; * multiplication of positives is positive: `0 < a → 0 < b → 0 < a * b`; * `0 < 1`. ## Main Definitions * `linear_ordered_semifield`: Typeclass for linear order semifields. * `linear_ordered_field`: Typeclass for linear ordered fields. ## Implementation details For olean caching reasons, this file is separate to the main file, `algebra.order.field.basic`. The lemmata are instead located there. -/ set_option old_structure_cmd true variables {α : Type*} /-- A linear ordered semifield is a field with a linear order respecting the operations. -/ @[protect_proj, ancestor linear_ordered_comm_semiring semifield] class linear_ordered_semifield (α : Type*) extends linear_ordered_comm_semiring α, semifield α /-- A linear ordered field is a field with a linear order respecting the operations. -/ @[protect_proj, ancestor linear_ordered_comm_ring field] class linear_ordered_field (α : Type*) extends linear_ordered_comm_ring α, field α @[priority 100] -- See note [lower instance priority] instance linear_ordered_field.to_linear_ordered_semifield [linear_ordered_field α] : linear_ordered_semifield α := { ..linear_ordered_ring.to_linear_ordered_semiring, ..‹linear_ordered_field α› } -- Guard against import creep. assert_not_exists monoid_hom
01ee3872ae6a6ae9c7a34d17fcebc3d9bc137bac
46125763b4dbf50619e8846a1371029346f4c3db
/src/category_theory/comma.lean
ca6ac3cfc95aa6ae2be0bb576732428cdbe69085
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
11,140
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import category_theory.isomorphism import category_theory.punit namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Type u₁} [𝒜 : category.{v₁} A] variables {B : Type u₂} [ℬ : category.{v₂} B] variables {T : Type u₃} [𝒯 : category.{v₃} T] include 𝒜 ℬ 𝒯 structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟶ R.obj right) variables {L : A ⥤ T} {R : B ⥤ T} @[ext] structure comma_morphism (X Y : comma L R) := (left : X.left ⟶ Y.left . obviously) (right : X.right ⟶ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w instance comma_category : category (comma L R) := { hom := comma_morphism, id := λ X, { left := 𝟙 X.left, right := 𝟙 X.right }, comp := λ X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R ⥤ A := { obj := λ X, X.left, map := λ _ _ f, f.left } def snd : comma L R ⥤ B := { obj := λ X, X.right, map := λ _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R := { app := λ X, X.hom } section variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T} def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R := { obj := λ X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) : (map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl end def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ := { obj := λ X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl end end end comma omit 𝒜 ℬ @[derive category] def over (X : T) := comma.{v₃ 0 v₃} (𝟭 T) ((functor.const punit).obj X) namespace over variables {X : T} @[ext] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp, reassoc] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ (functor.const punit).map f section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over @[derive category] def under (X : T) := comma.{0 v₃ v₃} ((functor.const punit).obj X) (𝟭 T) namespace under variables {X : T} @[ext] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ (functor.const punit).map f section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
92f658be56431d465ab8037f005b603739be48d7
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/platform.lean
9df49d81934a63160c4ab0554a0c84d9f803c7f5
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
325
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.core namespace System -- TODO: mark as opaque, the VM provides platform specific implementation def platform.nbits : Nat := 64 end System
5babfe7e1b3eb2a44e67f1b8e3cee6954442d531
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/multiplication/8.lean
bd8e47474462688f62bd3ded8a3f65c857d78907
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
138
lean
lemma mul_comm (a b : mynat) : a * b = b * a := induction b with n hn, rwa [mul_zero, zero_mul], rwa [mul_succ, succ_mul, hn], end
26e5ad8e0f188a4f1b3dd27be3317ebbfe9bc0c3
302b541ac2e998a523ae04da7673fd0932ded126
/tests/playground/binarytrees.lean
a0dd12d7d2b7ea5df0517598d0c569f341d17d8d
[]
no_license
mattweingarten/lambdapure
4aeff69e8e3b8e78ea3c0a2b9b61770ef5a689b1
f920a4ad78e6b1e3651f30bf8445c9105dfa03a8
refs/heads/master
1,680,665,168,790
1,618,420,180,000
1,618,420,180,000
310,816,264
2
1
null
null
null
null
UTF-8
Lean
false
false
1,801
lean
set_option trace.compiler.ir.init true inductive Tree | Nil | Node (l r : Tree) : Tree open Tree instance : Inhabited Tree := ⟨Nil⟩ -- This Function has an extra argument to suppress the -- common sub-expression elimination optimization partial def make' : UInt32 -> UInt32 -> Tree | n, d => if d = 0 then Node Nil Nil else Node (make' n (d - 1)) (make' (n + 1) (d - 1)) -- build a tree def make (d : UInt32) := make' d d def check : Tree → UInt32 | Nil => 0 | Node l r => 1 + check l + check r def minN := 4 def out (s) (n : Nat) (t : UInt32) : IO Unit := IO.println (s ++ " of depth " ++ toString n ++ "\t check: " ++ toString t) -- allocate and check lots of trees partial def sumT : UInt32 -> UInt32 -> UInt32 -> UInt32 | d, i, t => if i = 0 then t else let a := check (make d); sumT d (i-1) (t + a) -- generate many trees partial def depth : Nat -> Nat -> List (Nat × Nat × Task UInt32) | d, m => if d ≤ m then let n := 2 ^ (m - d + minN); (n, d, Task.mk (fun _ => sumT (UInt32.ofNat d) (UInt32.ofNat n) 0)) :: depth (d+2) m else [] def main : List String → IO UInt32 | [s] => do let n := s.toNat; let maxN := Nat.max (minN + 2) n; let stretchN := maxN + 1; -- stretch memory tree let c := check (make $ UInt32.ofNat stretchN); out "stretch tree" stretchN c; -- allocate a long lived tree let long := make $ UInt32.ofNat maxN; -- allocate, walk, and deallocate many bottom-up binary trees let vs := (depth minN maxN); -- `using` (parList $ evalTuple3 r0 r0 rseq) vs.mapM (fun ⟨m,d,i⟩ => out (toString m ++ "\t trees") d i.get); -- confirm the the long-lived binary tree still exists out "long lived tree" maxN (check long); pure 0 | _ => pure 1
e70d5ba472bfe266fbe9892ed0cce6e07435cb70
e30ff3aabdac29f8ea40ad76887544d0f9be9018
/ircbot/modules/print_date.lean
78f682aba83aa49e53da748fc143b1d80a9fd5d3
[]
no_license
forked-from-1kasper/leanbot
bdef0efa3e4d0eb75b06c1707fb4e35086bb57fa
c61c8c7fdad7b05877e0d232719ce23d2999557f
refs/heads/master
1,651,846,081,986
1,646,404,009,000
1,646,404,009,000
127,132,795
12
1
null
1,605,183,650,000
1,522,237,998,000
Lean
UTF-8
Lean
false
false
1,030
lean
import ircbot.types ircbot.effects ircbot.support ircbot.datetime open types effects support datetime namespace modules.print_date def print_date_func (d : date) (input : irc_text) : list irc_text := match input with | irc_text.parsed_normal { object := some ~nick!ident, type := message.privmsg, args := [subject], text := "\\date" } := let new_subject := if subject.front = '#' then subject else nick in let leading_zero (n : nat) := if n ≥ 10 then to_string n else "0" ++ (to_string n) in [privmsg new_subject $ sformat! ("It’s {leading_zero d.hour}:{leading_zero d.minute}, " ++ "{d.day} {to_string d.month}, {d.weekday} now.")] | _ := [] end def print_date_io (input : irc_text) : io (list irc_text) := do date ← get_date, pure $ match date with | some v := print_date_func v input | none := [] end def print_date : bot_function := { name := "print_date", syntax := "\\date", description := "Print current date.", func := print_date_io } end modules.print_date
4851c9ebd4c2a7eeee338a87c51a58f7ca7ab223
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/group_theory/p_group.lean
bf17caa9a7e4560bb42a1528b032f4eb47565c99
[ "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
9,380
lean
/- Copyright (c) 2018 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import group_theory.index import group_theory.perm.cycle_type import group_theory.quotient_group /-! # p-groups This file contains a proof that if `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α`. It also contains proofs of some corollaries of this lemma about existence of fixed points. -/ open_locale big_operators open fintype mul_action variables (p : ℕ) (G : Type*) [group G] /-- A p-group is a group in which every element has prime power order -/ def is_p_group : Prop := ∀ g : G, ∃ k : ℕ, g ^ (p ^ k) = 1 variables {p} {G} namespace is_p_group lemma iff_order_of [hp : fact p.prime] : is_p_group p G ↔ ∀ g : G, ∃ k : ℕ, order_of g = p ^ k := forall_congr (λ g, ⟨λ ⟨k, hk⟩, exists_imp_exists (by exact λ j, Exists.snd) ((nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hk)), exists_imp_exists (λ k hk, by rw [←hk, pow_order_of_eq_one])⟩) lemma of_card [fintype G] {n : ℕ} (hG : card G = p ^ n) : is_p_group p G := λ g, ⟨n, by rw [←hG, pow_card_eq_one]⟩ lemma of_bot : is_p_group p (⊥ : subgroup G) := of_card (subgroup.card_bot.trans (pow_zero p).symm) lemma iff_card [fact p.prime] [fintype G] : is_p_group p G ↔ ∃ n : ℕ, card G = p ^ n := begin have hG : 0 < card G := card_pos_iff.mpr has_one.nonempty, refine ⟨λ h, _, λ ⟨n, hn⟩, of_card hn⟩, suffices : ∀ q ∈ nat.factors (card G), q = p, { use (card G).factors.length, rw [←list.prod_repeat, ←list.eq_repeat_of_mem this, nat.prod_factors hG] }, intros q hq, obtain ⟨hq1, hq2⟩ := (nat.mem_factors hG).mp hq, haveI : fact q.prime := ⟨hq1⟩, obtain ⟨g, hg⟩ := equiv.perm.exists_prime_order_of_dvd_card q hq2, obtain ⟨k, hk⟩ := (iff_order_of.mp h) g, exact (hq1.pow_eq_iff.mp (hg.symm.trans hk).symm).1.symm, end section G_is_p_group variables (hG : is_p_group p G) include hG lemma of_injective {H : Type*} [group H] (ϕ : H →* G) (hϕ : function.injective ϕ) : is_p_group p H := begin simp_rw [is_p_group, ←hϕ.eq_iff, ϕ.map_pow, ϕ.map_one], exact λ h, hG (ϕ h), end lemma to_subgroup (H : subgroup G) : is_p_group p H := hG.of_injective H.subtype subtype.coe_injective lemma of_surjective {H : Type*} [group H] (ϕ : G →* H) (hϕ : function.surjective ϕ) : is_p_group p H := begin refine λ h, exists.elim (hϕ h) (λ g hg, exists_imp_exists (λ k hk, _) (hG g)), rw [←hg, ←ϕ.map_pow, hk, ϕ.map_one], end lemma to_quotient (H : subgroup G) [H.normal] : is_p_group p (quotient_group.quotient H) := hG.of_surjective (quotient_group.mk' H) quotient.surjective_quotient_mk' lemma of_equiv {H : Type*} [group H] (ϕ : G ≃* H) : is_p_group p H := hG.of_surjective ϕ.to_monoid_hom ϕ.surjective variables [hp : fact p.prime] include hp lemma index (H : subgroup G) [fintype (quotient_group.quotient H)] : ∃ n : ℕ, H.index = p ^ n := begin obtain ⟨n, hn⟩ := iff_card.mp (hG.to_quotient H.normal_core), obtain ⟨k, hk1, hk2⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ (H.normal_core.index_eq_card.trans hn)).mp (subgroup.index_dvd_of_le H.normal_core_le)), exact ⟨k, hk2⟩, end variables {α : Type*} [mul_action G α] lemma card_orbit (a : α) [fintype (orbit G a)] : ∃ n : ℕ, card (orbit G a) = p ^ n := begin let ϕ := orbit_equiv_quotient_stabilizer G a, haveI := fintype.of_equiv (orbit G a) ϕ, rw [card_congr ϕ, ←subgroup.index_eq_card], exact hG.index (stabilizer G a), end variables (α) [fintype α] [fintype (fixed_points G α)] /-- If `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α` -/ lemma card_modeq_card_fixed_points : card α ≡ card (fixed_points G α) [MOD p] := begin classical, calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (equiv.sigma_preimage_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = ∑ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a} : card_sigma _ ... ≡ ∑ a : fixed_points G α, 1 [MOD p] : _ ... = _ : by simp; refl, rw [←zmod.eq_iff_modeq_nat p, nat.cast_sum, nat.cast_sum], have key : ∀ x, card {y // (quotient.mk' y : quotient (orbit_rel G α)) = quotient.mk' x} = card (orbit G x) := λ x, by simp only [quotient.eq']; congr, refine eq.symm (finset.sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, finset.mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).mp a₂.2 a₁.1 (quotient.exact' h))) (λ b, quotient.induction_on' b (λ b _ hb, _)) (λ a ha _, by { rw [key, mem_fixed_points_iff_card_orbit_eq_one.mp a.2] })), obtain ⟨k, hk⟩ := hG.card_orbit b, have : k = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (pow_dvd_pow p) (by rwa [pow_one, ←hk, ←nat.modeq_zero_iff_dvd, ←zmod.eq_iff_modeq_nat, ←key])))), exact ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, pow_zero]⟩, finset.mem_univ _, (ne_of_eq_of_ne nat.cast_one one_ne_zero), rfl⟩, end /-- If a p-group acts on `α` and the cardinality of `α` is not a multiple of `p` then the action has a fixed point. -/ lemma nonempty_fixed_point_of_prime_not_dvd_card (hpα : ¬ p ∣ card α) : (fixed_points G α).nonempty := @set.nonempty_of_nonempty_subtype _ _ begin rw [←card_pos_iff, pos_iff_ne_zero], contrapose! hpα, rw [←nat.modeq_zero_iff_dvd, ←hpα], exact hG.card_modeq_card_fixed_points α, end /-- If a p-group acts on `α` and the cardinality of `α` is a multiple of `p`, and the action has one fixed point, then it has another fixed point. -/ lemma exists_fixed_point_of_prime_dvd_card_of_fixed_point (hpα : p ∣ card α) {a : α} (ha : a ∈ fixed_points G α) : ∃ b, b ∈ fixed_points G α ∧ a ≠ b := have hpf : p ∣ card (fixed_points G α) := nat.modeq_zero_iff_dvd.mp ((hG.card_modeq_card_fixed_points α).symm.trans hpα.modeq_zero_nat), have hα : 1 < card (fixed_points G α) := (fact.out p.prime).one_lt.trans_le (nat.le_of_dvd (card_pos_iff.2 ⟨⟨a, ha⟩⟩) hpf), let ⟨⟨b, hb⟩, hba⟩ := exists_ne_of_one_lt_card hα ⟨a, ha⟩ in ⟨b, hb, λ hab, hba (by simp_rw [hab])⟩ end G_is_p_group lemma to_le {H K : subgroup G} (hK : is_p_group p K) (hHK : H ≤ K) : is_p_group p H := hK.of_injective (subgroup.inclusion hHK) (λ a b h, subtype.ext (show _, from subtype.ext_iff.mp h)) lemma to_inf_left {H K : subgroup G} (hH : is_p_group p H) : is_p_group p (H ⊓ K : subgroup G) := hH.to_le inf_le_left lemma to_inf_right {H K : subgroup G} (hK : is_p_group p K) : is_p_group p (H ⊓ K : subgroup G) := hK.to_le inf_le_right lemma map {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : G →* K) : is_p_group p (H.map ϕ) := begin rw [←H.subtype_range, monoid_hom.map_range], exact hH.of_surjective (ϕ.restrict H).range_restrict (ϕ.restrict H).range_restrict_surjective, end lemma comap_of_ker_is_p_group {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : is_p_group p ϕ.ker) : is_p_group p (H.comap ϕ) := begin intro g, obtain ⟨j, hj⟩ := hH ⟨ϕ g.1, g.2⟩, rw [subtype.ext_iff, H.coe_pow, subtype.coe_mk, ←ϕ.map_pow] at hj, obtain ⟨k, hk⟩ := hϕ ⟨g.1 ^ p ^ j, hj⟩, rwa [subtype.ext_iff, ϕ.ker.coe_pow, subtype.coe_mk, ←pow_mul, ←pow_add] at hk, exact ⟨j + k, by rwa [subtype.ext_iff, (H.comap ϕ).coe_pow]⟩, end lemma comap_of_injective {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : function.injective ϕ) : is_p_group p (H.comap ϕ) := begin apply hH.comap_of_ker_is_p_group ϕ, rw ϕ.ker_eq_bot_iff.mpr hϕ, exact is_p_group.of_bot, end lemma to_sup_of_normal_right {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [K.normal] : is_p_group p (H ⊔ K : subgroup G) := begin rw [←quotient_group.ker_mk K, ←subgroup.comap_map_eq], apply (hH.map (quotient_group.mk' K)).comap_of_ker_is_p_group, rwa quotient_group.ker_mk, end lemma to_sup_of_normal_left {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [H.normal] : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right hK hH) lemma to_sup_of_normal_right' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : H ≤ K.normalizer) : is_p_group p (H ⊔ K : subgroup G) := let hHK' := to_sup_of_normal_right (hH.of_equiv (subgroup.comap_subtype_equiv_of_le hHK).symm) (hK.of_equiv (subgroup.comap_subtype_equiv_of_le subgroup.le_normalizer).symm) in ((congr_arg (λ H : subgroup K.normalizer, is_p_group p H) (subgroup.sup_subgroup_of_eq hHK subgroup.le_normalizer)).mp hHK').of_equiv (subgroup.comap_subtype_equiv_of_le (sup_le hHK subgroup.le_normalizer)) lemma to_sup_of_normal_left' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : K ≤ H.normalizer) : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right' hK hH hHK) end is_p_group
655b8086ddc81d3a29c68108aa11ea423f85f585
3ca6dd8bf23783927a6f1faedd16c92f0c96da9e
/lean/app.lean
e41ca7d5172d6be97b4fc46b98f8c74ec08575ed
[ "MIT" ]
permissive
anqurvanillapy/fpl
139a368655091c27b0ab78fa266ad9f74348ccb8
9576d5b76e6a868992dbe52930712ac67697bed2
refs/heads/master
1,623,285,455,999
1,616,136,159,000
1,616,136,159,000
128,802,535
1
0
null
null
null
null
UTF-8
Lean
false
false
77
lean
constants a b : Type constants f : a -> b constants x : a #check f x -- : b
b27b8312260cf74af750c9d7f1e3b9d263e94aaf
91493cf9df9eee2ed3f633c44e10d877cbda0a69
/src/mywork/lecture_1a.lean
0e86a1dc1d828241dcf2b9e61b863a4b61df9065
[]
no_license
edelzamora/cs2120f21
a32f6752dc4cf341bdf31b6f3fdd9ddbc903f83a
ba668ccc46998fa40a469bef273baba931fc0fad
refs/heads/main
1,689,854,534,035
1,630,525,878,000
1,630,525,878,000
399,944,123
0
0
null
null
null
null
UTF-8
Lean
false
false
10,010
lean
-- import definitions of real and rational numbers from mathlib import data.real.basic namespace cs2120 /- The first part of this lesson is in the file, lecture_1.md. Please start there then return here when you're done with that document. -/ /- NUMBER SYSTEMS -/ /- Mathematicians think about operations on many kinds of objects. In early mathematics, the objects are numbers. In later maths, they can be polynomials, matrices, functions, symmetries, or any manner of other "mathematical thingies". As we're going to see here, they can even be propositions and proofs. But let's start with something really simple. The number, 1. Ok, it's actually not that simple, because 1 can be interpreted as denoting a natural number, integer, real number, rational number, identity matrix, identity function, identity element of a group, or any manner (again) of "mathematical thingy". If Lean sees a bare numeral, 1, it interprets it as the natural number, 1. It is possible to force many other interpretations however, as the following examples show. As you read the code, remember the following. ℕ: Natural numbers. The non-negative whole numbers. {0, 1, 2, ...} ℤ: Integers: The negative and non-negative whole numbers. ℚ: Rationals: Ratios of an integer and a non-zero natural number. ℝ: Reals: Equivalence classes of convergent sequence of rationals. Irrational numbers: Real numbers not "isomorphic" to any rationals. Examples: Natural numbers: 0, 3, 11, 29 Integers: -29, 0, 3, 11 Rationals: 1/2, -3/4, 23/7 Reals: 0.000..., .333..., 3.1415... Irrationals: 3.1415..., e, sqrt 2 -/ def m := 1 -- 1 inferred to be a natural number (built into Lean) def n : ℕ := 1 -- 1 specified to be a natural number (non-negative whole number) def z : ℤ := 1 -- 1 as an integer (negative or non-negative whole number) def r : ℝ := 1.0 -- 1 as a real number (infinite decimal sequence) def q : ℚ := 1/1 -- 1 as a rational number (fraction) /- Each proceeding line of code has the following elements - def: a keyword, binds the given identifer to the given value - n, m, z, r, q: identifiers, a.k.a., variables or variable names - : T, where T is ℕ, ℤ, ℝ, or ℚ: specifies the *type* of the value - := 1.0: specifies the value, 1.0, to be bound to the identifier -/ /- The same definitions could be written as follows, allowing Lean to fill in type information that it can infer from the way in which the values are given. -/ def m' := 1 -- Lean assumes 1 is a natural number (built into Lean) def n' := (1 : ℕ) -- 1 as a natural number (non-negative whole number) def z' := (1 : ℤ) -- 1 as an integer (negative or non-negative whole number) def r' := (1.0 : ℝ) -- 1 as a real number (infinite decimal sequence) def q' := 1/1 -- Here Lean infers 1/1 is rational number (fraction) /- AXIOMS, PROPOSITIONS, PROOFS -/ /- Let's again talk about propositions and proofs involving "equality" propositions, such as the proposition that 1 = 1. We all *know* intuitively that 1 = 1, but how would you prove it, given that it's not an axiom of ordinary predicate logic? Without getting into the weeds, suffice it to say that the standard Lean Prover libraries define equality pretty much as we've discussed here: with an axiom in the form of a universal generaliztion: ∀ {T : Type} (t : T), t = t. In English, this says, "if you give me *any* Type, T, and any object, t, of that type, I will return you (and therefore there must be) a proof of the proposition, t = t; and the existence of this proof, in turn, justifies the *judgment* that the proposition, 1 = 1, is *true*. Let's take another look at the axiom that let's us *deduce* the *theorem* that 1 = 1. Here it is: ∀ {T : Type} (t : T), t = t. What that means is that if I choose any type, T, say T = ℕ, and any value of that type, say t = (1 : ℕ), then I should be able to apply the axiom to the argument values, ℕ and 1, to get back a *proof* of the proposition, 1 = 1. Indeed, that's just how it works, as the follow example shows formally (in Lean). -/ example : 1 = 1 := eq.refl 1 -- Lean inferns T = ℕ from 1 /- Yay! We just constructed a formal proof: a mathematical object that certifies that 1=1. It might not be super-impressive that Lean rejects "eq.refl 2" as a suitable proof (try it, and don't fail to read the entire error message when you hover your cursor over the red squiggle!); but the principle extends to commplex proofs of profound propositions. Nice: you've not only constructed a formal proof object but you have a "high assurance" check that the proof itself is correct, in that Lean actually accepts that it's correct. *That* is what Lean is really for: not just for formalizing mathematics and logic, but for checking that proofs *truly* prove what they claim to prove. -/ /- Of course, if formal proofs came without costs, we'd all be using them. The benefit of a *natural language* "proof description" (written in, say, English, but in a highly mathematical style) is that it's easier for people to follow, often because details can be elided on the assumption that the reader will know from context what is meant. In this case at hand, you could give a proof, of the proposition, 1 = 1, as follows: Proof: By the reflexive property of equality (applied to the particular value, 1). QED." If you and your audience both understand that you're *applying* the universal generalization given as an axiom to suitable values, then you can just leave out the parenthetical expression. How much detail to put in a proof description is a matter of style and of a willingness to make your readers fill in the missing details. The QED, by the way, is short for quod est demonstratum, Latin for "it is shown." It is a kind of traditional punctuation for natural language proof descriptions that signals that the proof is complete. The downside of using natural language proof descriptions in lieu of formal proof objects is that when things get complex, it can be nearly impossible to tell whether a proof in natural language is correct or not. In hard cases, it can require years of work by world experts to decide whether a proof is correct or faulty. In this class, we will insist, because all mathematicians do, that your propositions be fully formal, i.e., syntactically correct by the grammatical rules of predicate logic, as enforced by Lean. We will expose you to formal proofs to the extent that we believe that doing so will help you to understand how to write quasi-formal proof descriptions. Quasi-formal proofs are what most people use today, including instructors for follow-on CS courses. But there are now thriving communities in both mathematics and computer science that are aggressively pursuing the formalization, and the *computerization* of logic and proof. The community around Lean is most interested in formalizing mathematics for mathematicians. Other critically important applications of Lean and similar "proof assistants" arise in relation to the definition of programming languages, and in the formal (mathematical) specification and trustworthy and automated correctness checking of computer programs. -/ /- If you're getting the feeling that we are pointing you a little bit to a computational understand of what it means to construct or to use proofs, you're right. To make the point clearer, let's write our own proof-returning function@ We'll call it gimme_a_proof. It will take two arguments, a type, T, and a value, t, of that type; and it will return a proof of t = t, on the basis of which we can render the judgment that t = t is *true*. -/ def gimme_a_proof -- function name (T : Type) -- first argument (t : T) -- second argument : t = t -- return type := eq.refl t -- implementation /- Let's decode that. We're defining a function called gimme_a_proof that takes T and t as its arguments and promises to return a value of type t = t (a proof of this proposition). The way that it upholds this promise is by *applying* eq.refl to t, whatever it is, to construct a proof of t = t. That proof is the result and return value of this function. -/ /- Now that we've got this function defined, we can apply *it* to arguments to have it construct values that constitute proofs of t = t. If you hover over #reduce in the following Lean commands, Lean will report to you the results of applying the function to arguments of various numerical types. Remember when reading the outputs that "eq.refl x" *is* an object that serves as a proof of x = x -/ #reduce gimme_a_proof ℕ 9 #reduce gimme_a_proof bool tt #reduce gimme_a_proof ℚ 1 #reduce gimme_a_proof ℤ (-3) /- That wraps up this review (and extension) of our last lecture. Now for the quiz. -/ /-++++++++++ EXERCISES #1. Give a quasi-formal English language "proof" of the proposition that 2 = 2. Theorem: 2 = 2. Proof: [your answer here] -/ /-++++++++++ EXERCISE #2. Give, below, a formal statement and proof of the proposition, 2 = 2. (See above for a good example to follow!) -/ -- answer here /- EXERCISE #3. Identify what form of reasoning is being used in each of the following made-up stories. Just give a one-word answer for each. A. Every time the bell has rung, I've gotten a nugget. The bell just rung, so I'm gonna get a nugget! (Dogs usually say "gonna," by the way). answer: B. The "clone repo into container" command did nothing. That was clearly wrong. I search around on the World Wide Web and notice someone saying something about that VSCode command needed to have git installed. Ah ha, I thought. That could be it. I'll do the obvious experiment and install git and see if it works. (It did, by the way.) answer: C. It's true that it's raining, and it's true that the streets are wet, so it must be true that "it's raining *and* the streets are wet." answer: -/ end cs2120
0044feb508f897b3b32173898876965ef8e718c1
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/analytic/inverse.lean
60268c17f70fd0323c0208d0387869d60aff3cfa
[ "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
27,276
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.analytic.composition import tactic.congrm /-! # Inverse of analytic functions We construct the left and right inverse of a formal multilinear series with invertible linear term, we prove that they coincide and study their properties (notably convergence). ## Main statements * `p.left_inv i`: the formal left inverse of the formal multilinear series `p`, for `i : E ≃L[𝕜] F` which coincides with `p₁`. * `p.right_inv i`: the formal right inverse of the formal multilinear series `p`, for `i : E ≃L[𝕜] F` which coincides with `p₁`. * `p.left_inv_comp` says that `p.left_inv i` is indeed a left inverse to `p` when `p₁ = i`. * `p.right_inv_comp` says that `p.right_inv i` is indeed a right inverse to `p` when `p₁ = i`. * `p.left_inv_eq_right_inv`: the two inverses coincide. * `p.radius_right_inv_pos_of_radius_pos`: if a power series has a positive radius of convergence, then so does its inverse. -/ open_locale big_operators classical topology open finset filter namespace formal_multilinear_series variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] /-! ### The left inverse of a formal multilinear series -/ /-- The left inverse of a formal multilinear series, where the `n`-th term is defined inductively in terms of the previous ones to make sure that `(left_inv p i) ∘ p = id`. For this, the linear term `p₁` in `p` should be invertible. In the definition, `i` is a linear isomorphism that should coincide with `p₁`, so that one can use its inverse in the construction. The definition does not use that `i = p₁`, but proofs that the definition is well-behaved do. The `n`-th term in `q ∘ p` is `∑ qₖ (p_{j₁}, ..., p_{jₖ})` over `j₁ + ... + jₖ = n`. In this expression, `qₙ` appears only once, in `qₙ (p₁, ..., p₁)`. We adjust the definition so that this term compensates the rest of the sum, using `i⁻¹` as an inverse to `p₁`. These formulas only make sense when the constant term `p₀` vanishes. The definition we give is general, but it ignores the value of `p₀`. -/ noncomputable def left_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : formal_multilinear_series 𝕜 F E | 0 := 0 | 1 := (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm | (n+2) := - ∑ c : {c : composition (n+2) // c.length < n + 2}, have (c : composition (n+2)).length < n+2 := c.2, (left_inv (c : composition (n+2)).length).comp_along_composition (p.comp_continuous_linear_map i.symm) c @[simp] lemma left_inv_coeff_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.left_inv i 0 = 0 := by rw left_inv @[simp] lemma left_inv_coeff_one (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.left_inv i 1 = (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm := by rw left_inv /-- The left inverse does not depend on the zeroth coefficient of a formal multilinear series. -/ lemma left_inv_remove_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.remove_zero.left_inv i = p.left_inv i := begin ext1 n, induction n using nat.strong_rec' with n IH, cases n, { simp }, -- if one replaces `simp` with `refl`, the proof times out in the kernel. cases n, { simp }, -- TODO: why? simp only [left_inv, neg_inj], refine finset.sum_congr rfl (λ c cuniv, _), rcases c with ⟨c, hc⟩, ext v, dsimp, simp [IH _ hc], end /-- The left inverse to a formal multilinear series is indeed a left inverse, provided its linear term is invertible. -/ lemma left_inv_comp (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) : (left_inv p i).comp p = id 𝕜 E := begin ext n v, cases n, { simp only [left_inv, continuous_multilinear_map.zero_apply, id_apply_ne_one, ne.def, not_false_iff, zero_ne_one, comp_coeff_zero']}, cases n, { simp only [left_inv, comp_coeff_one, h, id_apply_one, continuous_linear_equiv.coe_apply, continuous_linear_equiv.symm_apply_apply, continuous_multilinear_curry_fin1_symm_apply] }, have A : (finset.univ : finset (composition (n+2))) = {c | composition.length c < n + 2}.to_finset ∪ {composition.ones (n+2)}, { refine subset.antisymm (λ c hc, _) (subset_univ _), by_cases h : c.length < n + 2, { simp [h] }, { simp [composition.eq_ones_iff_le_length.2 (not_lt.1 h)] } }, have B : disjoint ({c | composition.length c < n + 2} : set (composition (n + 2))).to_finset {composition.ones (n+2)}, by simp, have C : (p.left_inv i (composition.ones (n + 2)).length) (λ (j : fin (composition.ones n.succ.succ).length), p 1 (λ k, v ((fin.cast_le (composition.length_le _)) j))) = p.left_inv i (n+2) (λ (j : fin (n+2)), p 1 (λ k, v j)), { apply formal_multilinear_series.congr _ (composition.ones_length _) (λ j hj1 hj2, _), exact formal_multilinear_series.congr _ rfl (λ k hk1 hk2, by congr) }, have D : p.left_inv i (n+2) (λ (j : fin (n+2)), p 1 (λ k, v j)) = - ∑ (c : composition (n + 2)) in {c : composition (n + 2) | c.length < n + 2}.to_finset, (p.left_inv i c.length) (p.apply_composition c v), { simp only [left_inv, continuous_multilinear_map.neg_apply, neg_inj, continuous_multilinear_map.sum_apply], convert (sum_to_finset_eq_subtype (λ (c : composition (n+2)), c.length < n+2) (λ (c : composition (n+2)), (continuous_multilinear_map.comp_along_composition (p.comp_continuous_linear_map ↑(i.symm)) c (p.left_inv i c.length)) (λ (j : fin (n + 2)), p 1 (λ (k : fin 1), v j)))).symm.trans _, simp only [comp_continuous_linear_map_apply_composition, continuous_multilinear_map.comp_along_composition_apply], congr, ext c, congr, ext k, simp [h] }, simp [formal_multilinear_series.comp, show n + 2 ≠ 1, by dec_trivial, A, finset.sum_union B, apply_composition_ones, C, D, -set.to_finset_set_of], end /-! ### The right inverse of a formal multilinear series -/ /-- The right inverse of a formal multilinear series, where the `n`-th term is defined inductively in terms of the previous ones to make sure that `p ∘ (right_inv p i) = id`. For this, the linear term `p₁` in `p` should be invertible. In the definition, `i` is a linear isomorphism that should coincide with `p₁`, so that one can use its inverse in the construction. The definition does not use that `i = p₁`, but proofs that the definition is well-behaved do. The `n`-th term in `p ∘ q` is `∑ pₖ (q_{j₁}, ..., q_{jₖ})` over `j₁ + ... + jₖ = n`. In this expression, `qₙ` appears only once, in `p₁ (qₙ)`. We adjust the definition of `qₙ` so that this term compensates the rest of the sum, using `i⁻¹` as an inverse to `p₁`. These formulas only make sense when the constant term `p₀` vanishes. The definition we give is general, but it ignores the value of `p₀`. -/ noncomputable def right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : formal_multilinear_series 𝕜 F E | 0 := 0 | 1 := (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm | (n+2) := let q : formal_multilinear_series 𝕜 F E := λ k, if h : k < n + 2 then right_inv k else 0 in - (i.symm : F →L[𝕜] E).comp_continuous_multilinear_map ((p.comp q) (n+2)) @[simp] lemma right_inv_coeff_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.right_inv i 0 = 0 := by rw right_inv @[simp] lemma right_inv_coeff_one (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.right_inv i 1 = (continuous_multilinear_curry_fin1 𝕜 F E).symm i.symm := by rw right_inv /-- The right inverse does not depend on the zeroth coefficient of a formal multilinear series. -/ lemma right_inv_remove_zero (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) : p.remove_zero.right_inv i = p.right_inv i := begin ext1 n, induction n using nat.strong_rec' with n IH, rcases n with _|_|n, { simp only [right_inv_coeff_zero] }, { simp only [right_inv_coeff_one] }, simp only [right_inv, neg_inj], rw remove_zero_comp_of_pos _ _ (add_pos_of_nonneg_of_pos (n.zero_le) zero_lt_two), congr' 2 with k, by_cases hk : k < n+2; simp [hk, IH] end lemma comp_right_inv_aux1 {n : ℕ} (hn : 0 < n) (p : formal_multilinear_series 𝕜 E F) (q : formal_multilinear_series 𝕜 F E) (v : fin n → F) : p.comp q n v = (∑ (c : composition n) in {c : composition n | 1 < c.length}.to_finset, p c.length (q.apply_composition c v)) + p 1 (λ i, q n v) := begin have A : (finset.univ : finset (composition n)) = {c | 1 < composition.length c}.to_finset ∪ {composition.single n hn}, { refine subset.antisymm (λ c hc, _) (subset_univ _), by_cases h : 1 < c.length, { simp [h] }, { have : c.length = 1, by { refine (eq_iff_le_not_lt.2 ⟨ _, h⟩).symm, exact c.length_pos_of_pos hn }, rw ← composition.eq_single_iff_length hn at this, simp [this] } }, have B : disjoint ({c | 1 < composition.length c} : set (composition n)).to_finset {composition.single n hn}, by simp, have C : p (composition.single n hn).length (q.apply_composition (composition.single n hn) v) = p 1 (λ (i : fin 1), q n v), { apply p.congr (composition.single_length hn) (λ j hj1 hj2, _), simp [apply_composition_single] }, simp [formal_multilinear_series.comp, A, finset.sum_union B, C, -set.to_finset_set_of], end lemma comp_right_inv_aux2 (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (n : ℕ) (v : fin (n + 2) → F) : ∑ (c : composition (n + 2)) in {c : composition (n + 2) | 1 < c.length}.to_finset, p c.length (apply_composition (λ (k : ℕ), ite (k < n + 2) (p.right_inv i k) 0) c v) = ∑ (c : composition (n + 2)) in {c : composition (n + 2) | 1 < c.length}.to_finset, p c.length ((p.right_inv i).apply_composition c v) := begin have N : 0 < n + 2, by dec_trivial, refine sum_congr rfl (λ c hc, p.congr rfl (λ j hj1 hj2, _)), have : ∀ k, c.blocks_fun k < n + 2, { simp only [set.mem_to_finset, set.mem_set_of_eq] at hc, simp [← composition.ne_single_iff N, composition.eq_single_iff_length, ne_of_gt hc] }, simp [apply_composition, this], end /-- The right inverse to a formal multilinear series is indeed a right inverse, provided its linear term is invertible and its constant term vanishes. -/ lemma comp_right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) (h0 : p 0 = 0) : p.comp (right_inv p i) = id 𝕜 F := begin ext n v, cases n, { simp only [h0, continuous_multilinear_map.zero_apply, id_apply_ne_one, ne.def, not_false_iff, zero_ne_one, comp_coeff_zero']}, cases n, { simp only [comp_coeff_one, h, right_inv, continuous_linear_equiv.apply_symm_apply, id_apply_one, continuous_linear_equiv.coe_apply, continuous_multilinear_curry_fin1_symm_apply] }, have N : 0 < n+2, by dec_trivial, simp [comp_right_inv_aux1 N, h, right_inv, lt_irrefl n, show n + 2 ≠ 1, by dec_trivial, ← sub_eq_add_neg, sub_eq_zero, comp_right_inv_aux2, -set.to_finset_set_of], end lemma right_inv_coeff (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (n : ℕ) (hn : 2 ≤ n) : p.right_inv i n = - (i.symm : F →L[𝕜] E).comp_continuous_multilinear_map (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition n)), p.comp_along_composition (p.right_inv i) c) := begin cases n, { exact false.elim (zero_lt_two.not_le hn) }, cases n, { exact false.elim (one_lt_two.not_le hn) }, simp only [right_inv, neg_inj], congr' 1, ext v, have N : 0 < n + 2, by dec_trivial, have : (p 1) (λ (i : fin 1), 0) = 0 := continuous_multilinear_map.map_zero _, simp [comp_right_inv_aux1 N, lt_irrefl n, this, comp_right_inv_aux2, -set.to_finset_set_of], end /-! ### Coincidence of the left and the right inverse -/ private lemma left_inv_eq_right_inv_aux (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) (h0 : p 0 = 0) : left_inv p i = right_inv p i := calc left_inv p i = (left_inv p i).comp (id 𝕜 F) : by simp ... = (left_inv p i).comp (p.comp (right_inv p i)) : by rw comp_right_inv p i h h0 ... = ((left_inv p i).comp p).comp (right_inv p i) : by rw comp_assoc ... = (id 𝕜 E).comp (right_inv p i) : by rw left_inv_comp p i h ... = right_inv p i : by simp /-- The left inverse and the right inverse of a formal multilinear series coincide. This is not at all obvious from their definition, but it follows from uniqueness of inverses (which comes from the fact that composition is associative on formal multilinear series). -/ theorem left_inv_eq_right_inv (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (h : p 1 = (continuous_multilinear_curry_fin1 𝕜 E F).symm i) : left_inv p i = right_inv p i := calc left_inv p i = left_inv p.remove_zero i : by rw left_inv_remove_zero ... = right_inv p.remove_zero i : by { apply left_inv_eq_right_inv_aux; simp [h] } ... = right_inv p i : by rw right_inv_remove_zero /-! ### Convergence of the inverse of a power series Assume that `p` is a convergent multilinear series, and let `q` be its (left or right) inverse. Using the left-inverse formula gives $$ q_n = - (p_1)^{-n} \sum_{k=0}^{n-1} \sum_{i_1 + \dotsc + i_k = n} q_k (p_{i_1}, \dotsc, p_{i_k}). $$ Assume for simplicity that we are in dimension `1` and `p₁ = 1`. In the formula for `qₙ`, the term `q_{n-1}` appears with a multiplicity of `n-1` (choosing the index `i_j` for which `i_j = 2` while all the other indices are equal to `1`), which indicates that `qₙ` might grow like `n!`. This is bad for summability properties. It turns out that the right-inverse formula is better behaved, and should instead be used for this kind of estimate. It reads $$ q_n = - (p_1)^{-1} \sum_{k=2}^n \sum_{i_1 + \dotsc + i_k = n} p_k (q_{i_1}, \dotsc, q_{i_k}). $$ Here, `q_{n-1}` can only appear in the term with `k = 2`, and it only appears twice, so there is hope this formula can lead to an at most geometric behavior. Let `Qₙ = ‖qₙ‖`. Bounding `‖pₖ‖` with `C r^k` gives an inequality $$ Q_n ≤ C' \sum_{k=2}^n r^k \sum_{i_1 + \dotsc + i_k = n} Q_{i_1} \dotsm Q_{i_k}. $$ This formula is not enough to prove by naive induction on `n` a bound of the form `Qₙ ≤ D R^n`. However, assuming that the inequality above were an equality, one could get a formula for the generating series of the `Qₙ`: $$ \begin{align} Q(z) & := \sum Q_n z^n = Q_1 z + C' \sum_{2 \leq k \leq n} \sum_{i_1 + \dotsc + i_k = n} (r z^{i_1} Q_{i_1}) \dotsm (r z^{i_k} Q_{i_k}) \\ & = Q_1 z + C' \sum_{k = 2}^\infty (\sum_{i_1 \geq 1} r z^{i_1} Q_{i_1}) \dotsm (\sum_{i_k \geq 1} r z^{i_k} Q_{i_k}) \\ & = Q_1 z + C' \sum_{k = 2}^\infty (r Q(z))^k = Q_1 z + C' (r Q(z))^2 / (1 - r Q(z)). \end{align} $$ One can solve this formula explicitly. The solution is analytic in a neighborhood of `0` in `ℂ`, hence its coefficients grow at most geometrically (by a contour integral argument), and therefore the original `Qₙ`, which are bounded by these ones, are also at most geometric. This classical argument is not really satisfactory, as it requires an a priori bound on a complex analytic function. Another option would be to compute explicitly its terms (with binomial coefficients) to obtain an explicit geometric bound, but this would be very painful. Instead, we will use the above intuition, but in a slightly different form, with finite sums and an induction. I learnt this trick in [pöschel2017siegelsternberg]. Let $S_n = \sum_{k=1}^n Q_k a^k$ (where `a` is a positive real parameter to be chosen suitably small). The above computation but with finite sums shows that $$ S_n \leq Q_1 a + C' \sum_{k=2}^n (r S_{n-1})^k. $$ In particular, $S_n \leq Q_1 a + C' (r S_{n-1})^2 / (1- r S_{n-1})$. Assume that $S_{n-1} \leq K a$, where `K > Q₁` is fixed and `a` is small enough so that `r K a ≤ 1/2` (to control the denominator). Then this equation gives a bound $S_n \leq Q_1 a + 2 C' r^2 K^2 a^2$. If `a` is small enough, this is bounded by `K a` as the second term is quadratic in `a`, and therefore negligible. By induction, we deduce `Sₙ ≤ K a` for all `n`, which gives in particular the fact that `aⁿ Qₙ` remains bounded. -/ /-- First technical lemma to control the growth of coefficients of the inverse. Bound the explicit expression for `∑_{k<n+1} aᵏ Qₖ` in terms of a sum of powers of the same sum one step before, in a general abstract setup. -/ lemma radius_right_inv_pos_of_radius_pos_aux1 (n : ℕ) (p : ℕ → ℝ) (hp : ∀ k, 0 ≤ p k) {r a : ℝ} (hr : 0 ≤ r) (ha : 0 ≤ a) : ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, p (c.blocks_fun j)) ≤ ∑ j in Ico 2 (n + 1), r ^ j * (∑ k in Ico 1 n, a ^ k * p k) ^ j := calc ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, p (c.blocks_fun j)) = ∑ k in Ico 2 (n + 1), (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), ∏ j, r * (a ^ (c.blocks_fun j) * p (c.blocks_fun j))) : begin simp_rw [mul_sum], apply sum_congr rfl (λ k hk, _), apply sum_congr rfl (λ c hc, _), rw [prod_mul_distrib, prod_mul_distrib, prod_pow_eq_pow_sum, composition.sum_blocks_fun, prod_const, card_fin], ring, end ... ≤ ∑ d in comp_partial_sum_target 2 (n + 1) n, ∏ (j : fin d.2.length), r * (a ^ d.2.blocks_fun j * p (d.2.blocks_fun j)) : begin rw sum_sigma', refine sum_le_sum_of_subset_of_nonneg _ (λ x hx1 hx2, prod_nonneg (λ j hj, mul_nonneg hr (mul_nonneg (pow_nonneg ha _) (hp _)))), rintros ⟨k, c⟩ hd, simp only [set.mem_to_finset, mem_Ico, mem_sigma, set.mem_set_of_eq] at hd, simp only [mem_comp_partial_sum_target_iff], refine ⟨hd.2, c.length_le.trans_lt hd.1.2, λ j, _⟩, have : c ≠ composition.single k (zero_lt_two.trans_le hd.1.1), by simp [composition.eq_single_iff_length, ne_of_gt hd.2], rw composition.ne_single_iff at this, exact (this j).trans_le (nat.lt_succ_iff.mp hd.1.2) end ... = ∑ e in comp_partial_sum_source 2 (n+1) n, ∏ (j : fin e.1), r * (a ^ e.2 j * p (e.2 j)) : begin symmetry, apply comp_change_of_variables_sum, rintros ⟨k, blocks_fun⟩ H, have K : (comp_change_of_variables 2 (n + 1) n ⟨k, blocks_fun⟩ H).snd.length = k, by simp, congr' 2; try { rw K }, rw fin.heq_fun_iff K.symm, assume j, rw comp_change_of_variables_blocks_fun, end ... = ∑ j in Ico 2 (n+1), r ^ j * (∑ k in Ico 1 n, a ^ k * p k) ^ j : begin rw [comp_partial_sum_source, ← sum_sigma' (Ico 2 (n + 1)) (λ (k : ℕ), (fintype.pi_finset (λ (i : fin k), Ico 1 n) : finset (fin k → ℕ))) (λ n e, ∏ (j : fin n), r * (a ^ e j * p (e j)))], apply sum_congr rfl (λ j hj, _), simp only [← @multilinear_map.mk_pi_algebra_apply ℝ (fin j) _ ℝ], simp only [← multilinear_map.map_sum_finset (multilinear_map.mk_pi_algebra ℝ (fin j) ℝ) (λ k (m : ℕ), r * (a ^ m * p m))], simp only [multilinear_map.mk_pi_algebra_apply], dsimp, simp [prod_const, ← mul_sum, mul_pow], end /-- Second technical lemma to control the growth of coefficients of the inverse. Bound the explicit expression for `∑_{k<n+1} aᵏ Qₖ` in terms of a sum of powers of the same sum one step before, in the specific setup we are interesting in, by reducing to the general bound in `radius_right_inv_pos_of_radius_pos_aux1`. -/ lemma radius_right_inv_pos_of_radius_pos_aux2 {n : ℕ} (hn : 2 ≤ n + 1) (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) {r a C : ℝ} (hr : 0 ≤ r) (ha : 0 ≤ a) (hC : 0 ≤ C) (hp : ∀ n, ‖p n‖ ≤ C * r ^ n) : (∑ k in Ico 1 (n + 1), a ^ k * ‖p.right_inv i k‖) ≤ ‖(i.symm : F →L[𝕜] E)‖ * a + ‖(i.symm : F →L[𝕜] E)‖ * C * ∑ k in Ico 2 (n + 1), (r * ((∑ j in Ico 1 n, a ^ j * ‖p.right_inv i j‖))) ^ k := let I := ‖(i.symm : F →L[𝕜] E)‖ in calc ∑ k in Ico 1 (n + 1), a ^ k * ‖p.right_inv i k‖ = a * I + ∑ k in Ico 2 (n + 1), a ^ k * ‖p.right_inv i k‖ : by simp only [linear_isometry_equiv.norm_map, pow_one, right_inv_coeff_one, nat.Ico_succ_singleton, sum_singleton, ← sum_Ico_consecutive _ one_le_two hn] ... = a * I + ∑ k in Ico 2 (n + 1), a ^ k * ‖(i.symm : F →L[𝕜] E).comp_continuous_multilinear_map (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), p.comp_along_composition (p.right_inv i) c)‖ : begin congr' 1, apply sum_congr rfl (λ j hj, _), rw [right_inv_coeff _ _ _ (mem_Ico.1 hj).1, norm_neg], end ... ≤ a * ‖(i.symm : F →L[𝕜] E)‖ + ∑ k in Ico 2 (n + 1), a ^ k * (I * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), C * r ^ c.length * ∏ j, ‖p.right_inv i (c.blocks_fun j)‖)) : begin apply_rules [add_le_add, le_refl, sum_le_sum (λ j hj, _), mul_le_mul_of_nonneg_left, pow_nonneg, ha], apply (continuous_linear_map.norm_comp_continuous_multilinear_map_le _ _).trans, apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), apply (norm_sum_le _ _).trans, apply sum_le_sum (λ c hc, _), apply (comp_along_composition_norm _ _ _).trans, apply mul_le_mul_of_nonneg_right (hp _), exact prod_nonneg (λ j hj, norm_nonneg _), end ... = I * a + I * C * ∑ k in Ico 2 (n + 1), a ^ k * (∑ c in ({c | 1 < composition.length c}.to_finset : finset (composition k)), r ^ c.length * ∏ j, ‖p.right_inv i (c.blocks_fun j)‖) : begin simp_rw [mul_assoc C, ← mul_sum, ← mul_assoc, mul_comm _ (‖↑i.symm‖), mul_assoc, ← mul_sum, ← mul_assoc, mul_comm _ C, mul_assoc, ← mul_sum], ring, end ... ≤ I * a + I * C * ∑ k in Ico 2 (n+1), (r * ((∑ j in Ico 1 n, a ^ j * ‖p.right_inv i j‖))) ^ k : begin apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, norm_nonneg, hC, mul_nonneg], simp_rw [mul_pow], apply radius_right_inv_pos_of_radius_pos_aux1 n (λ k, ‖p.right_inv i k‖) (λ k, norm_nonneg _) hr ha, end /-- If a a formal multilinear series has a positive radius of convergence, then its right inverse also has a positive radius of convergence. -/ theorem radius_right_inv_pos_of_radius_pos (p : formal_multilinear_series 𝕜 E F) (i : E ≃L[𝕜] F) (hp : 0 < p.radius) : 0 < (p.right_inv i).radius := begin obtain ⟨C, r, Cpos, rpos, ple⟩ : ∃ C r (hC : 0 < C) (hr : 0 < r), ∀ (n : ℕ), ‖p n‖ ≤ C * r ^ n := le_mul_pow_of_radius_pos p hp, let I := ‖(i.symm : F →L[𝕜] E)‖, -- choose `a` small enough to make sure that `∑_{k ≤ n} aᵏ Qₖ` will be controllable by -- induction obtain ⟨a, apos, ha1, ha2⟩ : ∃ a (apos : 0 < a), (2 * I * C * r^2 * (I + 1) ^ 2 * a ≤ 1) ∧ (r * (I + 1) * a ≤ 1/2), { have : tendsto (λ a, 2 * I * C * r^2 * (I + 1) ^ 2 * a) (𝓝 0) (𝓝 (2 * I * C * r^2 * (I + 1) ^ 2 * 0)) := tendsto_const_nhds.mul tendsto_id, have A : ∀ᶠ a in 𝓝 0, 2 * I * C * r^2 * (I + 1) ^ 2 * a < 1, by { apply (tendsto_order.1 this).2, simp [zero_lt_one] }, have : tendsto (λ a, r * (I + 1) * a) (𝓝 0) (𝓝 (r * (I + 1) * 0)) := tendsto_const_nhds.mul tendsto_id, have B : ∀ᶠ a in 𝓝 0, r * (I + 1) * a < 1/2, by { apply (tendsto_order.1 this).2, simp [zero_lt_one] }, have C : ∀ᶠ a in 𝓝[>] (0 : ℝ), (0 : ℝ) < a, by { filter_upwards [self_mem_nhds_within] with _ ha using ha }, rcases (C.and ((A.and B).filter_mono inf_le_left)).exists with ⟨a, ha⟩, exact ⟨a, ha.1, ha.2.1.le, ha.2.2.le⟩ }, -- check by induction that the partial sums are suitably bounded, using the choice of `a` and the -- inductive control from Lemma `radius_right_inv_pos_of_radius_pos_aux2`. let S := λ n, ∑ k in Ico 1 n, a ^ k * ‖p.right_inv i k‖, have IRec : ∀ n, 1 ≤ n → S n ≤ (I + 1) * a, { apply nat.le_induction, { simp only [S], rw [Ico_eq_empty_of_le (le_refl 1), sum_empty], exact mul_nonneg (add_nonneg (norm_nonneg _) zero_le_one) apos.le }, { assume n one_le_n hn, have In : 2 ≤ n + 1, by linarith, have Snonneg : 0 ≤ S n := sum_nonneg (λ x hx, mul_nonneg (pow_nonneg apos.le _) (norm_nonneg _)), have rSn : r * S n ≤ 1/2 := calc r * S n ≤ r * ((I+1) * a) : mul_le_mul_of_nonneg_left hn rpos.le ... ≤ 1/2 : by rwa [← mul_assoc], calc S (n + 1) ≤ I * a + I * C * ∑ k in Ico 2 (n + 1), (r * S n)^k : radius_right_inv_pos_of_radius_pos_aux2 In p i rpos.le apos.le Cpos.le ple ... = I * a + I * C * (((r * S n) ^ 2 - (r * S n) ^ (n + 1)) / (1 - r * S n)) : by { rw geom_sum_Ico' _ In, exact ne_of_lt (rSn.trans_lt (by norm_num)) } ... ≤ I * a + I * C * ((r * S n) ^ 2 / (1/2)) : begin apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, Cpos.le], refine div_le_div (sq_nonneg _) _ (by norm_num) (by linarith), simp only [sub_le_self_iff], apply pow_nonneg (mul_nonneg rpos.le Snonneg), end ... = I * a + 2 * I * C * (r * S n) ^ 2 : by ring ... ≤ I * a + 2 * I * C * (r * ((I + 1) * a)) ^ 2 : by apply_rules [add_le_add, le_refl, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, Cpos.le, zero_le_two, pow_le_pow_of_le_left, rpos.le] ... = (I + 2 * I * C * r^2 * (I + 1) ^ 2 * a) * a : by ring ... ≤ (I + 1) * a : by apply_rules [mul_le_mul_of_nonneg_right, apos.le, add_le_add, le_refl] } }, -- conclude that all coefficients satisfy `aⁿ Qₙ ≤ (I + 1) a`. let a' : nnreal := ⟨a, apos.le⟩, suffices H : (a' : ennreal) ≤ (p.right_inv i).radius, by { apply lt_of_lt_of_le _ H, exact_mod_cast apos }, apply le_radius_of_bound _ ((I + 1) * a) (λ n, _), by_cases hn : n = 0, { have : ‖p.right_inv i n‖ = ‖p.right_inv i 0‖, by congr; try { rw hn }, simp only [this, norm_zero, zero_mul, right_inv_coeff_zero], apply_rules [mul_nonneg, add_nonneg, norm_nonneg, zero_le_one, apos.le] }, { have one_le_n : 1 ≤ n := bot_lt_iff_ne_bot.2 hn, calc ‖p.right_inv i n‖ * ↑a' ^ n = a ^ n * ‖p.right_inv i n‖ : mul_comm _ _ ... ≤ ∑ k in Ico 1 (n + 1), a ^ k * ‖p.right_inv i k‖ : begin have : ∀ k ∈ Ico 1 (n + 1), 0 ≤ a ^ k * ‖p.right_inv i k‖ := λ k hk, mul_nonneg (pow_nonneg apos.le _) (norm_nonneg _), exact single_le_sum this (by simp [one_le_n]), end ... ≤ (I + 1) * a : IRec (n + 1) (by dec_trivial) } end end formal_multilinear_series
62e005b0557139ffe8a5b68cba67197291201780
367134ba5a65885e863bdc4507601606690974c1
/src/combinatorics/colex.lean
f6de36b1365006e8a4d0a994781e3be2a0cc9ac6
[ "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
11,857
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Alena Gusakov -/ import data.finset import data.fintype.basic import algebra.geom_sum import tactic /-! # Colex We define the colex ordering for finite sets, and give a couple of important lemmas and properties relating to it. The colex ordering likes to avoid large values - it can be thought of on `finset ℕ` as the "binary" ordering. That is, order A based on `∑_{i ∈ A} 2^i`. It's defined here in a slightly more general way, requiring only `has_lt α` in the definition of colex on `finset α`. In the context of the Kruskal-Katona theorem, we are interested in particular on how colex behaves for sets of a fixed size. If the size is 3, colex on ℕ starts 123, 124, 134, 234, 125, 135, 235, 145, 245, 345, ... ## Main statements * `colex_hom`: strictly monotone functions preserve colex * Colex order properties - linearity, decidability and so on. * `forall_lt_of_colex_lt_of_forall_lt`: if A < B in colex, and everything in B is < t, then everything in A is < t. This confirms the idea that an enumeration under colex will exhaust all sets using elements < t before allowing t to be included. * `binary_iff`: colex for α = ℕ is the same as binary (this also proves binary expansions are unique) ## Notation We define `<` and `≤` to denote colex ordering, useful in particular when multiple orderings are available in context. ## Tags colex, colexicographic, binary ## References * https://github.com/b-mehta/maths-notes/blob/master/iii/mich/combinatorics.pdf ## Todo Show the subset ordering is a sub-relation of the colex ordering. -/ variable {α : Type*} open finset /-- We define this type synonym to refer to the colexicographic ordering on finsets rather than the natural subset ordering. -/ @[derive inhabited] def finset.colex (α) := finset α /-- A convenience constructor to turn a `finset α` into a `finset.colex α`, useful in order to use the colex ordering rather than the subset ordering. -/ def finset.to_colex {α} (s : finset α) : finset.colex α := s @[simp] lemma colex.eq_iff (A B : finset α) : A.to_colex = B.to_colex ↔ A = B := by refl /-- `A` is less than `B` in the colex ordering if the largest thing that's not in both sets is in B. In other words, max (A ▵ B) ∈ B (if the maximum exists). -/ instance [has_lt α] : has_lt (finset.colex α) := ⟨λ (A B : finset α), ∃ (k : α), (∀ {x}, k < x → (x ∈ A ↔ x ∈ B)) ∧ k ∉ A ∧ k ∈ B⟩ /-- We can define (≤) in the obvious way. -/ instance [has_lt α] : has_le (finset.colex α) := ⟨λ A B, A < B ∨ A = B⟩ lemma colex.lt_def [has_lt α] (A B : finset α) : A.to_colex < B.to_colex ↔ ∃ k, (∀ {x}, k < x → (x ∈ A ↔ x ∈ B)) ∧ k ∉ A ∧ k ∈ B := iff.rfl lemma colex.le_def [has_lt α] (A B : finset α) : A.to_colex ≤ B.to_colex ↔ A.to_colex < B.to_colex ∨ A = B := iff.rfl /-- If everything in A is less than k, we can bound the sum of powers. -/ lemma nat.sum_pow_two_lt {k : ℕ} {A : finset ℕ} (h₁ : ∀ {x}, x ∈ A → x < k) : A.sum (pow 2) < 2^k := begin apply lt_of_le_of_lt (sum_le_sum_of_subset (λ t, mem_range.2 ∘ h₁)), have z := geom_sum_mul_add 1 k, rw [geom_series, mul_one, one_add_one_eq_two] at z, rw ← z, apply nat.lt_succ_self, end namespace colex /-- Strictly monotone functions preserve the colex ordering. -/ lemma hom {β : Type*} [linear_order α] [decidable_eq β] [preorder β] {f : α → β} (h₁ : strict_mono f) (A B : finset α) : (A.image f).to_colex < (B.image f).to_colex ↔ A.to_colex < B.to_colex := begin simp only [colex.lt_def, not_exists, mem_image, exists_prop, not_and], split, { rintro ⟨k, z, q, k', _, rfl⟩, exact ⟨k', λ x hx, by simpa [h₁.injective.eq_iff] using z (h₁ hx), λ t, q _ t rfl, ‹k' ∈ B›⟩ }, rintro ⟨k, z, ka, _⟩, refine ⟨f k, λ x hx, _, _, k, ‹k ∈ B›, rfl⟩, { split, any_goals { rintro ⟨x', hx', rfl⟩, refine ⟨x', _, rfl⟩, rwa ← z _ <|> rwa z _, rwa strict_mono.lt_iff_lt h₁ at hx } }, { simp only [h₁.injective, function.injective.eq_iff], exact λ x hx, ne_of_mem_of_not_mem hx ka } end /-- A special case of `colex_hom` which is sometimes useful. -/ @[simp] lemma hom_fin {n : ℕ} (A B : finset (fin n)) : finset.to_colex (A.image (λ n, (n : ℕ))) < finset.to_colex (B.image (λ n, (n : ℕ))) ↔ finset.to_colex A < finset.to_colex B := colex.hom (λ x y k, k) _ _ instance [has_lt α] : is_irrefl (finset.colex α) (<) := ⟨λ A h, exists.elim h (λ _ ⟨_,a,b⟩, a b)⟩ @[trans] lemma lt_trans [linear_order α] {a b c : finset.colex α} : a < b → b < c → a < c := begin rintros ⟨k₁, k₁z, notinA, inB⟩ ⟨k₂, k₂z, notinB, inC⟩, cases lt_or_gt_of_ne (ne_of_mem_of_not_mem inB notinB), { refine ⟨k₂, _, by rwa k₁z h, inC⟩, intros x hx, rw ← k₂z hx, apply k₁z (trans h hx) }, { refine ⟨k₁, _, notinA, by rwa ← k₂z h⟩, intros x hx, rw k₁z hx, apply k₂z (trans h hx) } end @[trans] lemma le_trans [linear_order α] (a b c : finset.colex α) : a ≤ b → b ≤ c → a ≤ c := λ AB BC, AB.elim (λ k, BC.elim (λ t, or.inl (lt_trans k t)) (λ t, t ▸ AB)) (λ k, k.symm ▸ BC) instance [linear_order α] : is_trans (finset.colex α) (<) := ⟨λ _ _ _, colex.lt_trans⟩ instance [linear_order α] : is_asymm (finset.colex α) (<) := by apply_instance instance [linear_order α] : is_strict_order (finset.colex α) (<) := {} lemma lt_trichotomy [linear_order α] (A B : finset.colex α) : A < B ∨ A = B ∨ B < A := begin by_cases h₁ : (A = B), { tauto }, rcases (exists_max_image (A \ B ∪ B \ A) id _) with ⟨k, hk, z⟩, { simp only [mem_union, mem_sdiff] at hk, cases hk, { right, right, refine ⟨k, λ t th, _, hk.2, hk.1⟩, specialize z t, by_contra h₂, simp only [mem_union, mem_sdiff, id.def] at z, rw [not_iff, iff_iff_and_or_not_and_not, not_not, and_comm] at h₂, apply not_le_of_lt th (z h₂) }, { left, refine ⟨k, λ t th, _, hk.2, hk.1⟩, specialize z t, by_contra h₃, simp only [mem_union, mem_sdiff, id.def] at z, rw [not_iff, iff_iff_and_or_not_and_not, not_not, and_comm, or_comm] at h₃, apply not_le_of_lt th (z h₃) }, }, rw nonempty_iff_ne_empty, intro a, simp only [union_eq_empty_iff, sdiff_eq_empty_iff_subset] at a, apply h₁ (subset.antisymm a.1 a.2) end instance [linear_order α] : is_trichotomous (finset.colex α) (<) := ⟨lt_trichotomy⟩ -- It should be possible to do this computably but it doesn't seem to make any difference for now. noncomputable instance [linear_order α] : linear_order (finset.colex α) := { le_refl := λ A, or.inr rfl, le_trans := le_trans, le_antisymm := λ A B AB BA, AB.elim (λ k, BA.elim (λ t, (asymm k t).elim) (λ t, t.symm)) id, le_total := λ A B, (lt_trichotomy A B).elim3 (or.inl ∘ or.inl) (or.inl ∘ or.inr) (or.inr ∘ or.inl), decidable_le := classical.dec_rel _, ..finset.colex.has_le } instance [linear_order α] : is_incomp_trans (finset.colex α) (<) := begin constructor, rintros A B C ⟨nAB, nBA⟩ ⟨nBC, nCB⟩, have : A = B := ((lt_trichotomy A B).resolve_left nAB).resolve_right nBA, have : B = C := ((lt_trichotomy B C).resolve_left nBC).resolve_right nCB, rw [‹A = B›, ‹B = C›, and_self], apply irrefl end instance [linear_order α] : is_strict_weak_order (finset.colex α) (<) := {} instance [linear_order α] : is_strict_total_order (finset.colex α) (<) := {} /-- If {r} is less than or equal to s in the colexicographical sense, then s contains an element greater than or equal to r. -/ lemma mem_le_of_singleton_le [linear_order α] {r : α} {s : finset α}: ({r} : finset α).to_colex ≤ s.to_colex → ∃ x ∈ s, r ≤ x := begin intro h, rw colex.le_def at h, cases h with lt eq, { rw colex.lt_def at lt, rcases lt with ⟨k, hk, hi, hj⟩, by_cases hr : r ∈ s, { use r, tauto }, { contrapose! hk, simp only [mem_singleton], specialize hk k, use r, split, { apply hk, cc }, { simp, exact hr } } }, { rw ← eq, use r, simp only [true_and, eq_self_iff_true, mem_singleton] }, end /-- s.to_colex < finset.to_colex {r} iff all elements of s are less than r. -/ lemma lt_singleton_iff_mem_lt [linear_order α] {r : α} {s : finset α}: s.to_colex < finset.to_colex {r} ↔ ∀ x ∈ s, x < r := begin simp only [lt_def, mem_singleton, ← and_assoc, exists_eq_right], split, { rintro ⟨q, rs⟩ x hx, by_contra h, rw not_lt at h, cases lt_or_eq_of_le h with h₁ h₁, { rw q h₁ at hx, subst hx, apply lt_irrefl x h₁ }, { apply rs, rwa h₁ } }, { intro h, refine ⟨λ z hz, _, _⟩, { split, { intro hr, exfalso, apply lt_asymm (h _ hr) hz }, { rintro rfl, apply (lt_irrefl _ hz).elim } }, { intro rs, apply lt_irrefl _ (h _ rs) } } end /-- Colex is an extension of the base ordering on α. -/ lemma singleton_lt_iff_lt [linear_order α] {r s : α} : ({r} : finset α).to_colex < ({s} : finset α).to_colex ↔ r < s := begin rw colex.lt_def, simp only [mem_singleton, ← and_assoc, exists_eq_right], split, { rintro ⟨q, p⟩, apply lt_of_le_of_ne _ (ne.symm p), contrapose! p, rw (q p).1 rfl }, { intro a, exact ⟨λ z hz, iff_of_false (ne_of_gt (trans hz a)) (ne_of_gt hz), ne_of_gt a⟩ } end /-- If A is before B in colex, and everything in B is small, then everything in A is small. -/ lemma forall_lt_of_colex_lt_of_forall_lt [linear_order α] {A B : finset α} (t : α) (h₁ : A.to_colex < B.to_colex) (h₂ : ∀ x ∈ B, x < t) : ∀ x ∈ A, x < t := begin rw colex.lt_def at h₁, rcases h₁ with ⟨k, z, _, _⟩, intros x hx, apply lt_of_not_ge, intro a, refine not_lt_of_ge a (h₂ x _), rwa ← z, apply lt_of_lt_of_le (h₂ k ‹_›) a, end /-- Colex doesn't care if you remove the other set -/ @[simp] lemma sdiff_lt_sdiff_iff_lt [has_lt α] [decidable_eq α] (A B : finset α) : (A \ B).to_colex < (B \ A).to_colex ↔ A.to_colex < B.to_colex := begin rw [colex.lt_def, colex.lt_def], apply exists_congr, intro k, simp only [mem_sdiff, not_and, not_not], split, { rintro ⟨z, kAB, kB, kA⟩, refine ⟨_, kA, kB⟩, { intros x hx, specialize z hx, tauto } }, { rintro ⟨z, kA, kB⟩, refine ⟨_, λ _, kB, kB, kA⟩, intros x hx, rw z hx }, end /-- For subsets of ℕ, we can show that colex is equivalent to binary. -/ lemma sum_pow_two_lt_iff_lt (A B : finset ℕ) : A.sum (pow 2) < B.sum (pow 2) ↔ A.to_colex < B.to_colex := begin have z : ∀ (A B : finset ℕ), A.to_colex < B.to_colex → A.sum (pow 2) < B.sum (pow 2), { intros A B, rw [← sdiff_lt_sdiff_iff_lt, colex.lt_def], rintro ⟨k, z, kA, kB⟩, rw ← sdiff_union_inter A B, conv_rhs { rw ← sdiff_union_inter B A }, rw [sum_union (disjoint_sdiff_inter _ _), sum_union (disjoint_sdiff_inter _ _), inter_comm, add_lt_add_iff_right], apply lt_of_lt_of_le (@nat.sum_pow_two_lt k (A \ B) _), { apply single_le_sum (λ _ _, nat.zero_le _) kB }, intros x hx, apply lt_of_le_of_ne (le_of_not_lt (λ kx, _)), { apply (ne_of_mem_of_not_mem hx kA) }, specialize z kx, have := z.1 hx, rw mem_sdiff at this hx, exact hx.2 this.1 }, refine ⟨λ h, (lt_trichotomy A B).resolve_right (λ h₁, h₁.elim _ (not_lt_of_gt h ∘ z _ _)), z A B⟩, rintro rfl, apply irrefl _ h end end colex
a85ea5ee398ee4f8c7c3c4c515722a3738e1a957
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/topology/algebra/open_subgroup.lean
a572648a759d62d9823107b6e0c619783cd32c08
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
7,377
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 order.filter.lift import topology.opens import topology.algebra.ring open topological_space open_locale topological_space set_option old_structure_cmd true /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl attribute [norm_cast] mem_coe mem_coe_opens mem_coe_subgroup open_add_subgroup.mem_coe open_add_subgroup.mem_coe_opens open_add_subgroup.mem_coe_add_subgroup @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := λ U V h, by cases U; cases V; congr; assumption @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := mem_nhds_sets U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact (continuous_mul_right _) _ U.is_open }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] /-- The product of two open subgroups as an open subgroup of the product group. -/ @[to_additive "The product of two open subgroups as an open subgroup of the product group."] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G).prod (V : set H), is_open' := is_open_prod U.is_open V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := λ U V, { is_open' := is_open_inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, top := ⊤, le_top := λ U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl attribute [norm_cast] coe_inf coe_subset coe_subgroup_le open_add_subgroup.coe_inf open_add_subgroup.coe_subset open_add_subgroup.coe_add_subgroup_le end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, subgroup.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map.1 (this hg), replace hg : g ∈ H := subgroup.mem_coe.1 (mem_of_nhds hg), simp only [subgroup.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_sets_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ :set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
2bb1aaca8dc7b7452ef22515c5f034537f478b11
271e26e338b0c14544a889c31c30b39c989f2e0f
/src/Init/Data/UInt.lean
ebca55d79842ecb0a75cca3faf663319b9d7eac8
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,761
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Fin.Basic import Init.System.Platform open Nat def uint8Sz : Nat := 256 structure UInt8 := (val : Fin uint8Sz) @[extern "lean_uint8_of_nat"] def UInt8.ofNat (n : @& Nat) : UInt8 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt8 := UInt8.ofNat @[extern "lean_uint8_to_nat"] def UInt8.toNat (n : UInt8) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt8.add (a b : UInt8) : UInt8 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt8.sub (a b : UInt8) : UInt8 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt8.mul (a b : UInt8) : UInt8 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt8.div (a b : UInt8) : UInt8 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt8.mod (a b : UInt8) : UInt8 := ⟨a.val % b.val⟩ @[extern "lean_uint8_modn"] def UInt8.modn (a : UInt8) (n : @& Nat) : UInt8 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt8.land (a b : UInt8) : UInt8 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt8.lor (a b : UInt8) : UInt8 := ⟨Fin.lor a.val b.val⟩ def UInt8.lt (a b : UInt8) : Prop := a.val < b.val def UInt8.le (a b : UInt8) : Prop := a.val ≤ b.val instance : HasZero UInt8 := ⟨UInt8.ofNat 0⟩ instance : HasOne UInt8 := ⟨UInt8.ofNat 1⟩ instance : HasOfNat UInt8 := ⟨UInt8.ofNat⟩ instance : HasAdd UInt8 := ⟨UInt8.add⟩ instance : HasSub UInt8 := ⟨UInt8.sub⟩ instance : HasMul UInt8 := ⟨UInt8.mul⟩ instance : HasMod UInt8 := ⟨UInt8.mod⟩ instance : HasModN UInt8 := ⟨UInt8.modn⟩ instance : HasDiv UInt8 := ⟨UInt8.div⟩ instance : HasLess UInt8 := ⟨UInt8.lt⟩ instance : HasLessEq UInt8 := ⟨UInt8.le⟩ instance : Inhabited UInt8 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt8.decEq (a b : UInt8) : Decidable (a = b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt8.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt8.decLt (a b : UInt8) : Decidable (a < b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt8.decLe (a b : UInt8) : Decidable (a ≤ b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt8 := UInt8.decEq instance UInt8.hasDecidableLt (a b : UInt8) : Decidable (a < b) := UInt8.decLt a b instance UInt8.hasDecidableLe (a b : UInt8) : Decidable (a ≤ b) := UInt8.decLe a b def uint16Sz : Nat := 65536 structure UInt16 := (val : Fin uint16Sz) @[extern "lean_uint16_of_nat"] def UInt16.ofNat (n : @& Nat) : UInt16 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt16 := UInt16.ofNat @[extern "lean_uint16_to_nat"] def UInt16.toNat (n : UInt16) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt16.add (a b : UInt16) : UInt16 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt16.sub (a b : UInt16) : UInt16 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt16.mul (a b : UInt16) : UInt16 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt16.div (a b : UInt16) : UInt16 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt16.mod (a b : UInt16) : UInt16 := ⟨a.val % b.val⟩ @[extern "lean_uint16_modn"] def UInt16.modn (a : UInt16) (n : @& Nat) : UInt16 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt16.land (a b : UInt16) : UInt16 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt16.lor (a b : UInt16) : UInt16 := ⟨Fin.lor a.val b.val⟩ def UInt16.lt (a b : UInt16) : Prop := a.val < b.val def UInt16.le (a b : UInt16) : Prop := a.val ≤ b.val instance : HasZero UInt16 := ⟨UInt16.ofNat 0⟩ instance : HasOne UInt16 := ⟨UInt16.ofNat 1⟩ instance : HasOfNat UInt16 := ⟨UInt16.ofNat⟩ instance : HasAdd UInt16 := ⟨UInt16.add⟩ instance : HasSub UInt16 := ⟨UInt16.sub⟩ instance : HasMul UInt16 := ⟨UInt16.mul⟩ instance : HasMod UInt16 := ⟨UInt16.mod⟩ instance : HasModN UInt16 := ⟨UInt16.modn⟩ instance : HasDiv UInt16 := ⟨UInt16.div⟩ instance : HasLess UInt16 := ⟨UInt16.lt⟩ instance : HasLessEq UInt16 := ⟨UInt16.le⟩ instance : Inhabited UInt16 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt16.decEq (a b : UInt16) : Decidable (a = b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt16.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt16.decLt (a b : UInt16) : Decidable (a < b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt16.decLe (a b : UInt16) : Decidable (a ≤ b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt16 := UInt16.decEq instance UInt16.hasDecidableLt (a b : UInt16) : Decidable (a < b) := UInt16.decLt a b instance UInt16.hasDecidableLe (a b : UInt16) : Decidable (a ≤ b) := UInt16.decLe a b def uint32Sz : Nat := 4294967296 structure UInt32 := (val : Fin uint32Sz) @[extern "lean_uint32_of_nat"] def UInt32.ofNat (n : @& Nat) : UInt32 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt32 := UInt32.ofNat @[extern "lean_uint32_to_nat"] def UInt32.toNat (n : UInt32) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt32.add (a b : UInt32) : UInt32 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt32.sub (a b : UInt32) : UInt32 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt32.mul (a b : UInt32) : UInt32 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt32.div (a b : UInt32) : UInt32 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt32.mod (a b : UInt32) : UInt32 := ⟨a.val % b.val⟩ @[extern "lean_uint32_modn"] def UInt32.modn (a : UInt32) (n : @& Nat) : UInt32 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt32.land (a b : UInt32) : UInt32 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt32.lor (a b : UInt32) : UInt32 := ⟨Fin.lor a.val b.val⟩ def UInt32.lt (a b : UInt32) : Prop := a.val < b.val def UInt32.le (a b : UInt32) : Prop := a.val ≤ b.val instance : HasZero UInt32 := ⟨UInt32.ofNat 0⟩ instance : HasOne UInt32 := ⟨UInt32.ofNat 1⟩ instance : HasOfNat UInt32 := ⟨UInt32.ofNat⟩ instance : HasAdd UInt32 := ⟨UInt32.add⟩ instance : HasSub UInt32 := ⟨UInt32.sub⟩ instance : HasMul UInt32 := ⟨UInt32.mul⟩ instance : HasMod UInt32 := ⟨UInt32.mod⟩ instance : HasModN UInt32 := ⟨UInt32.modn⟩ instance : HasDiv UInt32 := ⟨UInt32.div⟩ instance : HasLess UInt32 := ⟨UInt32.lt⟩ instance : HasLessEq UInt32 := ⟨UInt32.le⟩ instance : Inhabited UInt32 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt32.decEq (a b : UInt32) : Decidable (a = b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt32.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt32.decLt (a b : UInt32) : Decidable (a < b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt32.decLe (a b : UInt32) : Decidable (a ≤ b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt32 := UInt32.decEq instance UInt32.hasDecidableLt (a b : UInt32) : Decidable (a < b) := UInt32.decLt a b instance UInt32.hasDecidableLe (a b : UInt32) : Decidable (a ≤ b) := UInt32.decLe a b def uint64Sz : Nat := 18446744073709551616 structure UInt64 := (val : Fin uint64Sz) @[extern "lean_uint64_of_nat"] def UInt64.ofNat (n : @& Nat) : UInt64 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt64 := UInt64.ofNat @[extern "lean_uint64_to_nat"] def UInt64.toNat (n : UInt64) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt64.add (a b : UInt64) : UInt64 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt64.sub (a b : UInt64) : UInt64 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt64.mul (a b : UInt64) : UInt64 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt64.div (a b : UInt64) : UInt64 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt64.mod (a b : UInt64) : UInt64 := ⟨a.val % b.val⟩ @[extern "lean_uint64_modn"] def UInt64.modn (a : UInt64) (n : @& Nat) : UInt64 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt64.land (a b : UInt64) : UInt64 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt64.lor (a b : UInt64) : UInt64 := ⟨Fin.lor a.val b.val⟩ def UInt64.lt (a b : UInt64) : Prop := a.val < b.val def UInt64.le (a b : UInt64) : Prop := a.val ≤ b.val @[extern c inline "((uint8_t)#1)"] def UInt64.toUInt8 (a : UInt64) : UInt8 := a.toNat.toUInt8 @[extern c inline "((uint16_t)#1)"] def UInt64.toUInt16 (a : UInt64) : UInt16 := a.toNat.toUInt16 @[extern c inline "((uint32_t)#1)"] def UInt64.toUInt32 (a : UInt64) : UInt32 := a.toNat.toUInt32 @[extern c inline "((uint64_t)#1)"] def UInt32.toUInt64 (a : UInt32) : UInt64 := a.toNat.toUInt64 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant UInt64.shiftLeft (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 @[extern c inline "#1 >> #2"] constant UInt64.shiftRight (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 instance : HasZero UInt64 := ⟨UInt64.ofNat 0⟩ instance : HasOne UInt64 := ⟨UInt64.ofNat 1⟩ instance : HasOfNat UInt64 := ⟨UInt64.ofNat⟩ instance : HasAdd UInt64 := ⟨UInt64.add⟩ instance : HasSub UInt64 := ⟨UInt64.sub⟩ instance : HasMul UInt64 := ⟨UInt64.mul⟩ instance : HasMod UInt64 := ⟨UInt64.mod⟩ instance : HasModN UInt64 := ⟨UInt64.modn⟩ instance : HasDiv UInt64 := ⟨UInt64.div⟩ instance : HasLess UInt64 := ⟨UInt64.lt⟩ instance : HasLessEq UInt64 := ⟨UInt64.le⟩ instance : Inhabited UInt64 := ⟨0⟩ @[extern c inline "(uint64_t)#1"] def Bool.toUInt64 (b : Bool) : UInt64 := if b then 1 else 0 @[extern c inline "#1 == #2"] def UInt64.decEq (a b : UInt64) : Decidable (a = b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt64.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt64.decLt (a b : UInt64) : Decidable (a < b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt64.decLe (a b : UInt64) : Decidable (a ≤ b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt64 := UInt64.decEq instance UInt64.hasDecidableLt (a b : UInt64) : Decidable (a < b) := UInt64.decLt a b instance UInt64.hasDecidableLe (a b : UInt64) : Decidable (a ≤ b) := UInt64.decLe a b def usizeSz : Nat := (2:Nat) ^ System.Platform.numBits structure USize := (val : Fin usizeSz) theorem usizeSzGt0 : usizeSz > 0 := Nat.posPowOfPos System.Platform.numBits (Nat.zeroLtSucc _) @[extern "lean_usize_of_nat"] def USize.ofNat (n : @& Nat) : USize := ⟨Fin.ofNat' n usizeSzGt0⟩ abbrev Nat.toUSize := USize.ofNat @[extern "lean_usize_to_nat"] def USize.toNat (n : USize) : Nat := n.val.val @[extern c inline "#1 + #2"] def USize.add (a b : USize) : USize := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def USize.sub (a b : USize) : USize := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def USize.mul (a b : USize) : USize := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def USize.div (a b : USize) : USize := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def USize.mod (a b : USize) : USize := ⟨a.val % b.val⟩ @[extern "lean_usize_modn"] def USize.modn (a : USize) (n : @& Nat) : USize := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def USize.land (a b : USize) : USize := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def USize.lor (a b : USize) : USize := ⟨Fin.lor a.val b.val⟩ @[extern c inline "#1"] def UInt32.toUSize (a : UInt32) : USize := a.toNat.toUSize @[extern c inline "((size_t)#1)"] def UInt64.toUSize (a : UInt64) : USize := a.toNat.toUSize @[extern c inline "(uint32_t)#1"] def USize.toUInt32 (a : USize) : UInt32 := a.toNat.toUInt32 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant USize.shiftLeft (a b : USize) : USize := (arbitrary Nat).toUSize @[extern c inline "#1 >> #2"] constant USize.shiftRight (a b : USize) : USize := (arbitrary Nat).toUSize def USize.lt (a b : USize) : Prop := a.val < b.val def USize.le (a b : USize) : Prop := a.val ≤ b.val instance : HasZero USize := ⟨USize.ofNat 0⟩ instance : HasOne USize := ⟨USize.ofNat 1⟩ instance : HasOfNat USize := ⟨USize.ofNat⟩ instance : HasAdd USize := ⟨USize.add⟩ instance : HasSub USize := ⟨USize.sub⟩ instance : HasMul USize := ⟨USize.mul⟩ instance : HasMod USize := ⟨USize.mod⟩ instance : HasModN USize := ⟨USize.modn⟩ instance : HasDiv USize := ⟨USize.div⟩ instance : HasLess USize := ⟨USize.lt⟩ instance : HasLessEq USize := ⟨USize.le⟩ instance : Inhabited USize := ⟨0⟩ @[extern c inline "#1 == #2"] def USize.decEq (a b : USize) : Decidable (a = b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => USize.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def USize.decLt (a b : USize) : Decidable (a < b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def USize.decLe (a b : USize) : Decidable (a ≤ b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq USize := USize.decEq instance USize.hasDecidableLt (a b : USize) : Decidable (a < b) := USize.decLt a b instance USize.hasDecidableLe (a b : USize) : Decidable (a ≤ b) := USize.decLe a b theorem USize.modnLt {m : Nat} : ∀ (u : USize), m > 0 → USize.toNat (u %ₙ m) < m | ⟨u⟩, h => Fin.modnLt u h
b5067ff6d4d2189937e4dca60cbdafec33dc492c
9028d228ac200bbefe3a711342514dd4e4458bff
/test/rcases.lean
a1ae6822133fd42466b5291c6f75e9c3f9c2b090
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,848
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.rcases universe u variables {α β γ : Type u} example (x : α × β × γ) : true := begin rcases x with ⟨a, b, c⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : γ, trivial } end example (x : α × β × γ) : true := begin rcases x with ⟨a, ⟨-, c⟩⟩, { guard_hyp a : α, success_if_fail { guard_hyp x_snd_fst : β }, guard_hyp c : γ, trivial } end example (x : (α × β) × γ) : true := begin rcases x with ⟨⟨a:α, b⟩, c⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : γ, trivial } end example : inhabited α × option β ⊕ γ → true := begin rintro (⟨⟨a⟩, _ | b⟩ | c), { guard_hyp a : α, trivial }, { guard_hyp a : α, guard_hyp b : β, trivial }, { guard_hyp c : γ, trivial } end example (x y : ℕ) (h : x = y) : true := begin rcases x with _|⟨⟩|z, { guard_hyp h : nat.zero = y, trivial }, { guard_hyp h : nat.succ nat.zero = y, trivial }, { guard_hyp z : ℕ, guard_hyp h : z.succ.succ = y, trivial }, end -- from equiv.sum_empty example (s : α ⊕ empty) : true := begin rcases s with _ | ⟨⟨⟩⟩, { guard_hyp s : α, trivial } end example : true := begin obtain ⟨n : ℕ, h : n = n, -⟩ : ∃ n : ℕ, n = n ∧ true, { existsi 0, simp }, guard_hyp n : ℕ, guard_hyp h : n = n, success_if_fail {assumption}, trivial end example : true := begin obtain : ∃ n : ℕ, n = n ∧ true, { existsi 0, simp }, trivial end example : true := begin obtain (h : true) | ⟨⟨⟩⟩ : true ∨ false, { left, trivial }, guard_hyp h : true, trivial end example : true := begin obtain h | ⟨⟨⟩⟩ : true ∨ false := or.inl trivial, guard_hyp h : true, trivial end example : true := begin obtain ⟨h, h2⟩ := and.intro trivial trivial, guard_hyp h : true, guard_hyp h2 : true, trivial end example : true := begin success_if_fail {obtain ⟨h, h2⟩}, trivial end example (x y : α × β) : true := begin rcases ⟨x, y⟩ with ⟨⟨a, b⟩, c, d⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : α, guard_hyp d : β, trivial } end example (x y : α ⊕ β) : true := begin obtain ⟨a|b, c|d⟩ := ⟨x, y⟩, { guard_hyp a : α, guard_hyp c : α, trivial }, { guard_hyp a : α, guard_hyp d : β, trivial }, { guard_hyp b : β, guard_hyp c : α, trivial }, { guard_hyp b : β, guard_hyp d : β, trivial }, end example {i j : ℕ} : (Σ' x, i ≤ x ∧ x ≤ j) → i ≤ j := begin intro h, rcases h' : h with ⟨x,h₀,h₁⟩, guard_hyp h' : h = ⟨x,h₀,h₁⟩, apply le_trans h₀ h₁, end protected def set.foo {α β} (s : set α) (t : set β) : set (α × β) := ∅ example {α} (V : set α) (w : true → ∃ p, p ∈ (V.foo V) ∩ (V.foo V)) : true := begin obtain ⟨a, h⟩ : ∃ p, p ∈ (V.foo V) ∩ (V.foo V) := w trivial, trivial, end example (n : ℕ) : true := begin obtain one_lt_n | n_le_one : 1 < n + 1 ∨ n + 1 ≤ 1 := nat.lt_or_ge 1 (n + 1), trivial, trivial, end example (n : ℕ) : true := begin obtain one_lt_n | (n_le_one : n + 1 ≤ 1) := nat.lt_or_ge 1 (n + 1), trivial, trivial, end example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true := begin rcases h with ⟨-, _⟩, (do lc ← tactic.local_context, guard lc.empty), trivial end example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true := begin rcases h with ⟨-, _, h⟩, (do lc ← tactic.local_context, guard (lc.length = 1)), guard_hyp h : 1 = 1, trivial end example (h : true ∨ true ∨ true) : true := begin rcases h with -|-|-, iterate 3 { (do lc ← tactic.local_context, guard lc.empty), trivial }, end
d7a15d9213fe61d5a36cce21d751812c53e4d6eb
4919181312c130f03eed71db8c80aa1cbd140256
/src/peano.lean
c78e5bad07a3f760ffb12e629bf0f95cb0ccf8d3
[]
no_license
williamdemeo/flypitch
62143800b790f0a0ffcd338c82b6bf3cd401552b
f73127c3ad36e6c2f074a26518dc333f07c1ab85
refs/heads/master
1,587,042,220,139
1,547,238,998,000
1,547,238,998,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,236
lean
import .fol tactic.tidy open fol local attribute [instance, priority 0] classical.prop_decidable --local attribute [instance] classical.prop_decidable local notation h :: t := dvector.cons h t local notation `[]` := dvector.nil local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l namespace peano section /-- Given a nat k and a 0-formula ψ, return ψ with ∀' applied k times to it --/ @[simp] def alls {L : Language} : Π n : ℕ, formula L → formula L --:= nat.iterate all n | 0 f := f | (n+1) f := ∀' alls n f @[simp]def bd_alls' {L : Language} : Π k n : ℕ, bounded_formula L (n + k) → bounded_formula L n | 0 n f := f | (k+1) n f := bd_alls' k n (∀' f) @[simp]def bf_k_plus_zero {L} {k} : bounded_formula L (0 + k) = bounded_formula L k := by {convert rfl, rw[zero_add]} @[simp] def bd_alls {L : Language} : Π n : ℕ, bounded_formula L n → sentence L | 0 f := f | (n+1) f := bd_alls n (∀' f) -- bd_alls' (n+1) 0 (f.cast_eqr (zero_add (n+1))) /-- generalization of bd_alls where we can apply less than n ∀'s--/ def bf_n_of_0_n {L : Language} {n} (ψ : bounded_formula L (0 + n)) : bounded_formula L n := by {convert ψ, rw[zero_add]} def bf_0_n_of_n {L : Language} {n} (ψ : bounded_formula L n) : bounded_formula L (0 + n) := by {convert ψ, apply zero_add} -- set_option pp.all true -- set_option pp.notation false @[simp]lemma mpr_lemma {α β: Type*} {p q : α = β} {x : β} : eq.mpr p x = eq.mpr q x := by refl @[simp]lemma mpr_lemma2 {L} {n m : ℕ} {h : m = n} {h' : bounded_formula L m = bounded_formula L n} {h'' : bounded_formula L (m+1) = bounded_formula L (n+1)} {f : bounded_formula L (n+1)} : ∀' eq.mpr h'' f = eq.mpr h' (∀'f) := by tidy -- lemma obvious3 {L : Language} {n} {ψ : bounded_formula L (n+1)} (p q : bounded_formula L (n - n) = bounded_formula L (n - n)) : eq.mpr p (bd_alls' n n (∀' ψ)) = eq.mpr q (bd_alls' n n (∀' ψ)) := -- by apply mpr_lemma -- refl also suffices here -- lemma obvious4 {L : Language} {n} {ψ : bounded_formula L (n+1)} (p : bounded_formula L (n - n) = bounded_formula L (n - n)) : -- (bd_alls' n n (∀' ψ)) = eq.mpr p (bd_alls' n n (∀' ψ)) := -- begin -- have : bd_alls' n n (∀' ψ) = eq.mpr rfl (bd_alls' n n (∀' ψ)), by refl, -- have := obvious3 rfl p, swap, exact ψ, cc -- end /- Obviously, bd_alls' n 0 ψ = bd_alls ψ -/ -- bd_alls n ψ = bd_alls' n 0 (by {convert ψ, apply zero_add}) --protected def cast_eqr {n m l} (h : n = m) (f : bounded_preformula L m l) : bounded_preformula L n l := --f.cast $ ge_of_eq h @[simp] lemma alls'_alls {L : Language} : Π n (ψ : bounded_formula L n), bd_alls n ψ = bd_alls' n 0 (ψ.cast_eq (zero_add n).symm) := by {intros n ψ, induction n, swap, simp[n_ih (∀' ψ)], tidy} -- @[simp] lemma alls'_alls {L : Language} : Π n (ψ : bounded_formula L n), bd_alls n ψ = bd_alls' n 0 (by {convert ψ, apply zero_add}) := -- begin -- intros n ψ, induction n with n ih generalizing ψ, refl, simp[ih (∀' ψ)] -- end @[simp]lemma alls'_all_commute {L : Language} {n} {k} {f : bounded_formula L (n+k+1)} : (bd_alls' k n (∀' f)) = ∀' bd_alls' k (n+1) (f.cast_eq (by simp))-- by {refine ∀' bd_alls' k (n+1) _, simp, exact f} := by {induction k; dsimp only [bounded_preformula.cast_eq], swap, simp[@k_ih (∀'f)], tidy} -- @[simp]lemma alls'_all_commute {L : Language} {n} {k} {f : bounded_formula L (n + k + 1)} : bd_alls' k n (∀' f) = ∀' bd_alls' k (n+1) (by {simp, exact f}) := -- begin -- induction k, refl, unfold bd_alls', rw[@k_ih (∀' f),<-mpr_lemma2], -- simp only [add_comm, eq_self_iff_true, add_right_inj, add_left_comm] -- end @[simp]lemma bd_alls'_substmax {L} {n} {f : bounded_formula L (n+1)} {t : closed_term L} : (bd_alls' n 1 (f.cast_eq (by simp)))[t /0] = (bd_alls' n 0 (substmax_bounded_formula (f.cast_eq (by simp)) t)) := by {induction n, {tidy}, have := @n_ih (∀' f), simp[bounded_preformula.cast_eq] at *, exact this} lemma realize_sentence_bd_alls {L} {n} {f : bounded_formula L n} {S : Structure L} : S ⊨ (bd_alls n f) ↔ (∀ xs : dvector S n, realize_bounded_formula xs f []) := begin induction n, {tidy, convert a, apply dvector.zero_eq}, {have := @n_ih (∀' f), simp[alls'_alls, alls'_all_commute] at this, cases this with this_mp this_mpr, split, intros H xs, cases xs, apply this_mp (by {convert H, tidy}), intro H, convert this_mpr (by {intros xs x, exact H (x :: xs)}), tidy} end @[simp] lemma alls_0 {L : Language} (ψ : formula L) : alls 0 ψ = ψ := by refl --lemma bd_alls_all_commute {L : Language} : Π n (f : bounded_formula L (n+1)), bd_alls n (∀'f) = ∀' (bd_alls n (f)) -- @[simp] def b_alls_1 {L : Language} {n : ℕ} {f : formula L} (hf : formula_below (n+1) f) : formula_below n $ alls 1 f := begin -- constructor, assumption -- end @[simp] lemma alls_all_commute {L : Language} (f : formula L) {k : ℕ} : (alls k ∀' f) = (∀' alls k f) := by {induction k, refl, dunfold alls, rw[k_ih]} @[simp] lemma alls_succ_k {L : Language} (f : formula L) {k : ℕ} : alls (k + 1) f = ∀' alls k f := by constructor -- @[simp] def b_alls_k {L : Language} {n : ℕ} {k: ℕ} : ∀ f : formula L, formula_below (n + k) f → formula_below n (alls k f) := -- begin -- induction k with k ih, -- intros f hf, exact hf, -- intros f hf, -- have H := alls_succ_k, -- have hf_rw : formula_below (n + nat.succ k) f → formula_below (n+k) ∀'f, by {apply b_alls_1}, -- let hf2 := hf_rw hf, -- have hooray := ih (∀'f) hf2, -- rw[alls_all_commute, <-H] at hooray, exact hooray --hooray!! -- end /- both b_subst and b_subst2 are consequences of formula_below_subst in fol.lean -/ -- def b_subst {L : Language} {n : ℕ} {f : formula L} (hf : formula_below (n+1) f) {t : term L} (ht : term_below 0 t) : formula_below n (f[t //0]) := -- begin -- have P := @formula_below_subst L 0 n 0 f begin rw[zero_add], exact hf end t, -- have t' : term_below n t, -- fapply term_below_of_le, -- exact 0, -- exact n.zero_le, -- exact ht, -- have Q := P t', -- simp only [fol.subst_formula, zero_add] at Q, assumption -- end -- def b_subst2 {L : Language} {n : ℕ} {f : formula L} (hf : formula_below n f) {t : term L} (ht : term_below n t) : formula_below n (f[t //0]) := -- begin -- have P := @formula_below_subst L 0 n 0 f begin rw[zero_add], fapply formula_below_of_le, exact n, simpa end t, -- have P' := P ht, simp only [fol.subst_formula, zero_add] at P', assumption -- end /- END LEMMAS -/ /- The language of PA -/ inductive peano_functions : ℕ → Type -- thanks Floris! | zero : peano_functions 0 | succ : peano_functions 1 | plus : peano_functions 2 | mult : peano_functions 2 def L_peano : Language := ⟨peano_functions, λ n, empty⟩ def L_peano_plus {n} (t₁ t₂ : bounded_term L_peano n) : bounded_term L_peano n := @bounded_term_of_function L_peano 2 n peano_functions.plus t₁ t₂ def L_peano_mult {n} (t₁ t₂ : bounded_term L_peano n) : bounded_term L_peano n := @bounded_term_of_function L_peano 2 n peano_functions.mult t₁ t₂ local infix ` +' `:100 := L_peano_plus local infix ` ×' `:150 := L_peano_mult def succ {n} : bounded_term L_peano n → bounded_term L_peano n := @bounded_term_of_function L_peano 1 n peano_functions.succ def zero {n} : bounded_term L_peano n := bd_const peano_functions.zero def one {n} : bounded_term L_peano n := succ zero /- For each k : ℕ, return the bounded_term of L_peano corresponding to k-/ @[reducible]def formal_nat {n}: Π k : ℕ, bounded_term L_peano n | 0 := zero | (k+1) := succ $ formal_nat k /- for all x, zero not equal to succ x -/ def p_zero_not_succ : sentence L_peano := ∀'(zero ≃ succ &0 ⟹ ⊥) @[reducible]def shallow_zero_not_succ : Prop := ∀ n : ℕ, 0 = nat.succ n → false def p_succ_inj : sentence L_peano := ∀' ∀'(succ &1 ≃ succ &0 ⟹ &1 ≃ &0) @[reducible]def shallow_succ_inj : Prop := ∀ x, ∀ y, nat.succ x = nat.succ y → x = y def p_zero_is_identity : sentence L_peano := ∀'(&0 +' zero ≃ &0) @[reducible]def shallow_zero_is_identity : Prop := ∀ x : ℕ, x + 0 = x /- ∀ x ∀ y, x + succ y = succ( x + y) -/ def p_succ_plus : sentence L_peano := ∀' ∀'(&1 +' succ &0 ≃ succ (&1 +' &0)) @[reducible]def shallow_succ_plus : Prop := ∀ x, ∀ y, x + nat.succ y = nat.succ(x + y) /- ∀ x, x ⬝ 0 = 0 -/ def p_zero_of_times_zero : sentence L_peano := ∀'(&0 ×' zero ≃ zero) @[reducible]def shallow_zero_of_times_zero : Prop := ∀ x : ℕ, x * 0 = 0 /- ∀ x y, (x ⬝ succ y = (x ⬝ y) + x -/ def p_times_succ : sentence L_peano := ∀' ∀' (&1 ×' succ &0 ≃ &1 ×' &0 +' &1) @[reducible]def shallow_times_succ : Prop := ∀ x : ℕ, ∀ y : ℕ, x * (y + 1) = (x * y) + x /- The induction schema instance at ψ is the following formula (up to the fixed ordering of variables given by the de Bruijn indexing): letting k+1 be the number of free vars of ψ: (k ∀'s)[(ψ(...,0) ∧ ∀' (ψ → ψ(...,S(x)))) → ∀' ψ] -/ def p_induction_schema {n : ℕ} (ψ : bounded_formula L_peano (n+1)) : sentence L_peano := bd_alls n (ψ[zero/0] ⊓ ∀' (ψ ⟹ (ψ ↑' 1 # 1)[succ &0/0]) ⟹ ∀' ψ) @[reducible]def shallow_induction_schema : Π P : set ℕ, Prop := λ P, (P(0) ∧ ∀ x, P x → P (nat.succ x)) → ∀ x, P x /- The theory of Peano arithmetic -/ def PA : Theory L_peano := {p_zero_not_succ, p_succ_inj, p_zero_is_identity, p_succ_plus, p_zero_of_times_zero, p_times_succ} ∪ ⋃ (n : ℕ), (λ(ψ : bounded_formula L_peano (n+1)), p_induction_schema ψ) '' set.univ @[reducible]def shallow_PA : set Prop := {shallow_zero_not_succ, shallow_succ_inj, shallow_zero_is_identity, shallow_succ_plus, shallow_zero_of_times_zero, shallow_times_succ} ∪ (shallow_induction_schema '' (set.univ)) def is_even : bounded_formula L_peano 1 := ∃' (&0 +' &0 ≃ &1) def L_peano_structure_of_nat : Structure L_peano := begin refine ⟨ℕ, _, _⟩, {intros n F, induction F, exact λv, 0, {intro v, cases v, exact nat.succ v_x}, {intro v, cases v, exact v_x + (v_xs.nth 0 $ by constructor)}, {intro v, cases v, exact v_x * (v_xs.nth 0 $ by constructor)},}, {intro v, intro R, cases R}, end local notation `ℕ'` := L_peano_structure_of_nat @[simp]lemma floris {L} {S : Structure L} : ↥S = S.carrier := by refl example : ℕ' ⊨ p_zero_not_succ := begin change ∀ x : ℕ', 0 = nat.succ x → false, intros x h, cases h end @[simp]lemma zero_is_zero : @realize_bounded_term L_peano ℕ' 0 [] 0 zero [] = nat.zero := by refl @[simp]lemma one_is_one : @realize_bounded_term L_peano ℕ' 0 [] 0 one [] = (nat.succ nat.zero) := by refl instance has_zero_sort_L_peano_structure_of_nat : has_zero ℕ' := ⟨nat.zero⟩ instance has_zero_L_peano_structure_of_nat : has_zero L_peano_structure_of_nat := ⟨nat.zero⟩ @[simp]lemma formal_nat_realize_term {n} : realize_closed_term ℕ' (formal_nat n) = n := by {induction n, refl, tidy} @[simp] lemma succ_realize_term {n} : realize_closed_term ℕ' (succ $ formal_nat n) = nat.succ n := begin dsimp[realize_closed_term, realize_bounded_term, succ, bounded_term_of_function], induction n, tidy end @[simp]lemma formal_nat_realize_formula {ψ : bounded_formula L_peano 1} (n) : realize_bounded_formula ([(n : ℕ')]) ψ [] ↔ ℕ' ⊨ ψ[(formal_nat n)/0] := begin induction n, all_goals{dsimp[formal_nat], simp[realize_subst_formula0]}, have := @formal_nat_realize_term 0, unfold formal_nat at this, rw[this] end @[simp]lemma nat_bd_all {ψ : bounded_formula L_peano 1} : ℕ' ⊨ ∀'ψ ↔ ∀(n : ℕ'), ℕ' ⊨ ψ[(formal_nat n)/0] := begin refine ⟨by {intros H n, induction n, all_goals{dsimp[formal_nat], rw[realize_subst_formula0], tidy}}, _⟩, intros H n, have := H n, induction n, all_goals{simp only [formal_nat_realize_formula], exact this} end lemma shallow_induction (P : set nat) : (P(0) ∧ ∀ x, P x → P (nat.succ x)) → ∀ x, P x := λ h, nat.rec h.1 h.2 -- @[simp]lemma subst0_subst0 {L} {n} {f : bounded_formula L (n+1)} {s₁} {s₂} : (f ↑' 1 # 1)[s₁ /0][s₂ /0] = f[s₁[s₂ /0] /0] := sorry -- this probably isn't true with careful lifting -- @[simp]lemma subst_succ_is_apply {n} {k} : (succ &0)[formal_nat n /0] = @formal_nat k (nat.succ n) := -- begin -- induction n, refl, symmetry, dsimp[formal_nat] at *, rw[<-n_ih], -- unfold succ bounded_term_of_function formal_nat, tidy, induction n_n, tidy -- end -- @[simp]lemma subst_term'_cancel {n} {k} : Π ψ : bounded_formula L_peano (k + 1), (ψ ↑' 1 # 1)[succ &0 /0][formal_nat n /0] = ψ[formal_nat (nat.succ n) /0] := by simp -- begin -- -- intros n ψ, unfold subst0_bounded_formula, tidy, -- simp[lift_subst_formula_cancel ψ.fst 0], -- -- sorry -- looks like here we need a lemma that generalizes lift_subst_formula_cancel to substitutions of terms, or something -- end ---- oops, i think this is already somewhere in fol.lean -- /-- Canonical extension of a dvector to a valuation --/ -- def val_of_dvector {α : Type*} [has_zero α] {n} (xs : dvector α n): ℕ' → α := -- begin -- intro k, -- by_cases nat.lt k n, -- exact xs.nth k h, -- exact 0 -- end -- @[reducible]def dvector.trunc {α : Type*} : ∀ {n m : nat} (h : n ≤ m) (xs : dvector α m), dvector α n -- | 0 0 _ xs := [] -- | 0 (m+1) _ xs := [] -- | (n+1) 0 _ xs := by {exfalso, cases _x} -- | (n+1) (m+1) h xs := begin cases xs, convert (xs_x::dvector.trunc _ xs_xs), tidy end -- @[simp]lemma dvector_trunc_n_n {α : Type*} {n : nat} {h : n ≤ n} {v : dvector α n} : dvector.trunc h v = v := by {induction v, tidy} -- @[simp]lemma dvector_trunc_0_n {α : Type*} {n : nat} {h : 0 ≤ n} {v : dvector α n} : dvector.trunc h v = [] := by {induction v, tidy} /-- Given a term t with ≤ n free variables, the realization of t only depends on the nth initial segment of the realizing dvector v. --/ -- lemma realize_closed_term_realize_irrel {L} {S : Structure L} {n n' : nat} {h : n' ≤ n} {t : bounded_term L n'} {v : dvector S n} : realize_bounded_term (dvector.trunc h v) t [] = realize_bounded_term v (t.cast h) [] := -- begin -- revert t, apply bounded_term.rec, {intro k, induction k, induction v, have : n' = 0, by {apply nat.eq_zero_of_le_zero, exact h}, subst this, {tidy}, sorry}, -- tidy, sorry -- end -- lemma realize_closed_term_realizer_irrel {L} {S : Structure L} {n} {n'} {h : n' ≤ n} {t : bounded_term L n'} {v : dvector S n} : realize_bounded_term (@dvector.trunc n' n h xs) (t.cast (by simp)) [] = realize_bounded_term [] t [] := -- begin -- induction n, -- {cases v, revert t, }, -- {sorry}, -- end -- lemma realize_bounded_formula_subst0_gen {L} {S : Structure L} {n l} (f : bounded_preformula L (n+1) l) {v : dvector S n} {xs : dvector S l} (t : bounded_term L n) : realize_bounded_formula v (f[(t.cast (by refl)) /0]) xs ↔ realize_bounded_formula ((realize_bounded_term v t [])::v) f xs := -- begin -- sorry -- end -- realization of a substitution of a bounded_term (n' + 1) at n in a bounded_formula (n'' + 1), where n + n' = n'', is the same as realization (insert S[t]) lemma asjh {L} {S : Structure L} {n n' n''} {h : n + (n'+1) + 1 = n'' + 1} {t : bounded_term L (n'+1)} {f : bounded_formula L (n''+1)} {v : dvector S (n + n' + 1)} : @realize_bounded_formula L S n 0 v (@subst_bounded_formula L n (n' + 1) (n'' + 1) 0 f t (by assumption) = @realize_bounded_formula L S (n+1) 0 (dvector.insert (realize_bounded_term begin end t)) @[simp]lemma subst_falsum {L} {n n' n''} {h : n + n' + 1 = n''} {t : bounded_term L n'} : bd_falsum[t // n // h] = bd_falsum := by tidy -- #reduce (ℕ')[ ([] : dvector ℕ' 0) /// (@zero 0)] -- #reduce (L_peano_structure_of_nat)[(p_zero_not_succ)] -- #reduce (L_peano_structure_of_nat)[(&0 ≃ zero : bounded_formula L_peano 1) ;; ([0] : dvector (ℕ') 1)] @[simp]lemma subst0_falsum {L} {n} {t : bounded_term L n} : bd_falsum[t /0] = bd_falsum := by {unfold subst0_bounded_formula, simpa only [subst_falsum]} -- looks like here we additionally need substitution notation for bounded_term @[simp]lemma subst_eq {L} {n n' n''} {h : n + n' + 1 = n''} {t₁ t₂ : bounded_term L n''} {t : bounded_term L n'} : (t₁ ≃ t₂)[t // n // h] = subst_bounded_term (t₁.cast_eq h.symm) t ≃ subst_bounded_term (t₂.cast_eq h.symm) t := by tidy -- TODO replace these with the fully expanded versions @[simp]lemma subst0_eq {L} {n} {t : bounded_term L n} {t₁ t₂ : bounded_term L (n+1)} : (t₁ ≃ t₂)[t /0] = (t₁[t /0] ≃ t₂[t /0]) := by {unfold subst0_bounded_formula, simpa only [subst_eq]} @[simp]lemma subst_imp {L} {n n' n''} {h : n + n' + 1 = n''} {t : bounded_term L n'} {f₁ f₂ : bounded_formula L (n'')} : (f₁ ⟹ f₂)[t // n // h] = (f₁[t // n // h] ⟹ f₂[t // n // h]) := by tidy @[simp]lemma subst0_imp {L} {n} {t : bounded_term L n} {f₁ f₂ : bounded_formula L (n+1)} : (f₁ ⟹ f₂)[t /0] = f₁[t /0] ⟹ f₂[t /0] := by {unfold subst0_bounded_formula, simpa only [subst_imp]} -- def hewwo : bounded_formula L_peano 1 := &0 ≃ zero -- #reduce @realize_bounded_formula L_peano L_peano_structure_of_nat _ _ [] ((hewwo[ (succ zero) // 0 // (by simp)]).cast (by {refl})) [] -- sanity check, works as expected #reduce @fol.subst_bounded_term L_peano 0 0 0 (&0) zero -- #reduce (&0 : bounded_term L_peano 1)[zero // 0] -- elaborator fails, don't know why -- this is the suspicious simp lemma--- don't want this -- @[simp]lemma subst0_all {L} {n} {t : closed_term L} {f : bounded_formula L (n+2)} : (∀' f)[(t.cast (by simp)) /0] = begin apply bd_all, fapply subst_bounded_formula, exact n+2, exact f, exact (t.cast (by simp)), repeat{constructor} end := -- begin -- sorry -- end @[simp]lemma subst_all {L} {n n' n''} {h : n + n' + 1 = n''} {t : closed_term L} {f : bounded_formula L (n'' + 1)} : (∀'f)[t.cast0 n' // n // (by {simp[h]})] = ∀'(f[(t.cast0 n' : bounded_term L n') // (n+1) // (by {tidy})]).cast_eq (by simp) := by tidy @[simp]lemma subst0_all {L} {n} {t : closed_term L} {f : bounded_formula L (n+2)} : ((∀'f)[t.cast (by simp) /0] : bounded_formula L n) = ∀'((f[t.cast0 n // 1 // (by {simp} : 1 + n + 1 = n + 2)]).cast_eq (by simp)) := by tidy -- {unfold subst0_bounded_formula, simp[@subst_all L n 0 (n+1) (by simp) t f], } lemma zero_of_lt_one (n : nat) (h : n < 1) : n = 0 := by {cases h, refl, cases nat.lt_of_succ_le h_a} @[simp]lemma realize_bounded_term_subst0 {L} {S : Structure L} {n} (s : bounded_term L (n+1)) {v : dvector S n} (t : closed_term L) : realize_bounded_term v (s[(t.cast (by simp)) /0]) [] = realize_bounded_term ((realize_closed_term S t)::v) s [] := begin revert s, refine bounded_term.rec1 _ _, {intro k, rcases k with ⟨k_val, k_H⟩, simp, induction n generalizing k_val, have := zero_of_lt_one k_val (by exact k_H), subst this, congr, {apply dvector.zero_eq}, {tidy}, cases nat.le_of_lt_succ k_H with H1 H2, swap, repeat{sorry} }, {sorry} end -- intros, induction n, rw[dvector.zero_eq v], -- {cases k, tidy, -- have := zero_of_lt_one k_val (by exact k_is_lt), subst this, -- congr, unfold subst0_bounded_term, tidy}, -- { -- -- cases v, have := @n_ih v_xs, sorry -- } lemma subst0_bounded_formula_bd_apps_rel {L} {n l} (f : bounded_preformula L (n+1) l) (t : closed_term L) (ts : dvector (bounded_term L (n+1)) l) : subst0_bounded_formula (bd_apps_rel f ts) (t.cast (by simp)) = bd_apps_rel (subst0_bounded_formula f (t.cast (by simp))) (ts.map $ λt', subst0_bounded_term t' (t.cast (by simp))) := begin induction ts generalizing f, refl, simp[bd_apps_rel, ts_ih (bd_apprel f ts_x)], congr, tidy end lemma rel_subst0_irrel {L : Language} {n l} {R : L.relations l} {t : bounded_term L n} : (bd_rel R)[t /0] = (bd_rel R) := by tidy -- lemma realize_bounded_formula_subst_insert {L} {S : Structure L} {n i} (f : bounded_formula L (n+1)) {v : dvector S n} (t : closed_term L) {h_i : i + 0 + 1 = n + 1}: realize_bounded_formula v (begin apply @subst_bounded_formula L i _ (n+1) _ f (by sorry), repeat{constructor} end) [] ↔ realize_bounded_formula (v.insert (realize_closed_term S t) i) f [] := -- begin -- sorry -- end -- maybe to finish this, need to generalize over all substitution indices---substitution at n corresponds to an insertion at n, and so on -- can finish by induction on vector length and use substmax lemma realize_bounded_formula_subst0_gen {L} {S : Structure L} {n} {m} {m'} {h_m : m + m' + 1 = n+1} (f : bounded_formula L (n+1)) {v : dvector S n} (t : closed_term L) : realize_bounded_formula v ((f[t.cast (zero_le m') // m // h_m]).cast_eq (by {tidy})) [] ↔ realize_bounded_formula (dvector.insert (realize_closed_term S t) (m+1) v) f [] := begin revert n f v, refine bounded_formula.rec1 _ _ _ _ _; intros, {simp, intro a, cases a}, {sorry}, -- this needs term version {sorry}, -- this needs version for bd_apps_rel {sorry}, {simp[*, subst_all],} end set_option pp.implicit true /-- realization of a subst0 is the realization with the substituted term prepended to the realizing vector --/ lemma realize_bounded_formula_subst0 {L} {S : Structure L} {n} (f : bounded_formula L (n+1)) {v : dvector S n} (t : closed_term L) : realize_bounded_formula v (f[(t.cast0 n) /0]) [] ↔ realize_bounded_formula ((realize_closed_term S t)::v) f [] := begin revert n f v, refine bounded_formula.rec1 _ _ _ _ _; intros, {simp}, {simp}, {rw[subst0_bounded_formula_bd_apps_rel], simp[realize_bounded_formula_bd_apps_rel, rel_subst0_irrel]}, {simp*}, {simp, apply forall_congr, clear ih, intro x, have := realize_bounded_formula_subst0_gen f t, tactic.rotate 1, exact S, exact 0, exact (n+1), {simp}, exact (x::v), simp at this, rw[<-this], conv {to_lhs, congr, skip, congr, congr, skip, simp[closed_preterm.cast0]}, apply iff_of_eq, congr' 2, simp, } end -- begin -- dsimp[closed_term, closed_preterm] at t, -- have := @realize_bounded_formula_subst0_gen L S n 0 f v [] (t.cast (by simp)), -- unfold realize_closed_term, rw[<-realize_closed_term_realize_irrel] at this, -- simp at this, split, -- -- rest of this lemma relies on rewriting the bounded_preterm.cast t, after which simp[this] should work -- {intro, apply this.mp, revert a, sorry}, -- {sorry}, -- end lemma realize_bounded_formula_subst0' {L} {S : Structure L} {n} (f : bounded_formula L (n+1)) {v : dvector S n} (t : bounded_term L 1) (x : S) : realize_bounded_formula (x :: v) ((f ↑' 1 # 1)[(t.cast (by simp)) /0]) [] ↔ realize_bounded_formula ((realize_bounded_term ([x] : dvector S 1) t []) :: v) f [] := begin revert f n v, refine bounded_formula.rec1 _ _ _ _ _; intros, {tidy}, {sorry}, -- this requires a version of this lemma for terms {sorry}, -- same issue as the corresponding case above {sorry}, -- this one should be easy, just need a lemma about commutation with bd_imp {sorry}, -- same issues as the corresponding case above end /- ℕ' satisfies PA induction schema -/ theorem PA_standard_model_induction {index : nat} {ψ : bounded_formula L_peano (index + 1)} : ℕ' ⊨ bd_alls index (ψ[zero /0] ⊓ ∀'(ψ ⟹ (ψ ↑' 1 # 1)[succ &0 /0]) ⟹ ∀' ψ) := begin rw[realize_sentence_bd_alls], intro xs, simp, intros H_zero H_ih, apply nat.rec, {apply (realize_bounded_formula_subst0 ψ zero).mp, apply H_zero}, {intros n H, apply (@realize_bounded_formula_subst0' _ _ _ ψ xs (succ &0) n).mp, exact H_ih n H} end def true_arithmetic := Th ℕ' lemma true_arithmetic_extends_PA : PA ⊆ true_arithmetic := begin intros f hf, cases hf with not_induct induct, swap, {rcases induct with ⟨induction_schemas, ⟨⟨index, h_eq⟩, ih_right⟩⟩, rw[h_eq] at ih_right, simp[set.range, set.image] at ih_right, rcases ih_right with ⟨ψ, h_ψ⟩, subst h_ψ, apply PA_standard_model_induction}, {repeat{cases not_induct}, tidy, contradiction} end lemma shallow_standard_model : ∀ ψ ∈ shallow_PA, ψ := begin intros x H, cases H, {repeat{cases H}, tidy, contradiction}, {simp[shallow_induction_schema] at H, rcases H with ⟨y, Hy⟩, subst Hy, exact nat.rec} end def PA_standard_model : Model PA := ⟨ℕ', true_arithmetic_extends_PA⟩ end end peano
504b3b800810e5a1f427d4a62cbaf782f4c186cc
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Data/Trie.lean
cc0d320810f88a8dc370ba57437f88e5c7172be0
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,742
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich, Leonardo de Moura Trie for tokenizing the Lean language -/ import Lean.Data.Format namespace Lean namespace Parser open Std (RBNode RBNode.leaf RBNode.singleton RBNode.find RBNode.insert) inductive Trie (α : Type) | Node : Option α → RBNode Char (fun _ => Trie α) → Trie α namespace Trie variables {α : Type} def empty : Trie α := ⟨none, RBNode.leaf⟩ instance : EmptyCollection (Trie α) := ⟨empty⟩ instance : Inhabited (Trie α) := ⟨Node none RBNode.leaf⟩ partial def insert (t : Trie α) (s : String) (val : α) : Trie α := let rec insertEmpty (i : String.Pos) : Trie α := match s.atEnd i with | true => Trie.Node (some val) RBNode.leaf | false => let c := s.get i let t := insertEmpty (s.next i) Trie.Node none (RBNode.singleton c t) let rec loop | Trie.Node v m, i => match s.atEnd i with | true => Trie.Node (some val) m -- overrides old value | false => let c := s.get i let i := s.next i let t := match RBNode.find Char.lt m c with | none => insertEmpty i | some t => loop t i Trie.Node v (RBNode.insert Char.lt m c t) loop t 0 partial def find? (t : Trie α) (s : String) : Option α := let rec loop | Trie.Node val m, i => match s.atEnd i with | true => val | false => let c := s.get i let i := s.next i match RBNode.find Char.lt m c with | none => none | some t => loop t i loop t 0 private def updtAcc (v : Option α) (i : String.Pos) (acc : String.Pos × Option α) : String.Pos × Option α := match v, acc with | some v, (j, w) => (i, some v) -- we pattern match on `acc` to enable memory reuse | none, acc => acc partial def matchPrefix (s : String) (t : Trie α) (i : String.Pos) : String.Pos × Option α := let rec loop | Trie.Node v m, i, acc => match s.atEnd i with | true => updtAcc v i acc | false => let acc := updtAcc v i acc let c := s.get i let i := s.next i match RBNode.find Char.lt m c with | some t => loop t i acc | none => acc loop t i (i, none) private partial def toStringAux {α : Type} : Trie α → List Format | Trie.Node val map => map.fold (fun Fs c t => format (repr c) :: (Format.group $ Format.nest 2 $ flip Format.joinSep Format.line $ toStringAux t) :: Fs) [] instance {α : Type} : ToString (Trie α) := ⟨fun t => (flip Format.joinSep Format.line $ toStringAux t).pretty⟩ end Trie end Parser end Lean
3eb6ae22e34faf003589bd21e0aa2bb577f7e2f0
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/fderiv_measurable.lean
51d4260b59aff35d1b262c612f0eb72d1a46f6ea
[ "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
40,962
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, Yury Kudryashov -/ import analysis.calculus.deriv import measure_theory.function.strongly_measurable.basic /-! # Derivative is measurable In this file we prove that the derivative of any function with complete codomain is a measurable function. Namely, we prove: * `measurable_set_of_differentiable_at`: the set `{x | differentiable_at 𝕜 f x}` is measurable; * `measurable_fderiv`: the function `fderiv 𝕜 f` is measurable; * `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `λ x, fderiv 𝕜 f x y` is measurable; * `measurable_deriv`: the function `deriv f` is measurable (for `f : 𝕜 → F`). We also show the same results for the right derivative on the real line (see `measurable_deriv_within_Ici` and ``measurable_deriv_within_Ioi`), following the same proof strategy. ## Implementation We give a proof that avoids second-countability issues, by expressing the differentiability set as a function of open sets in the following way. Define `A (L, r, ε)` to be the set of points where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the linear map `L`, up to `ε r`. It is an open set. Let also `B (L, r, s, ε) = A (L, r, ε) ∩ A (L, s, ε)`: we require that at two possibly different scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open. We claim that the differentiability set of `f` is exactly `D = ⋂ ε > 0, ⋃ δ > 0, ⋂ r, s < δ, ⋃ L, B (L, r, s, ε)`. In other words, for any `ε > 0`, we require that there is a size `δ` such that, for any two scales below this size, the function is well approximated by a linear map, common to the two scales. The set `⋃ L, B (L, r, s, ε)` is open, as a union of open sets. Converting the intersections and unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the differentiability set is measurable. To prove the claim, there are two inclusions. One is trivial: if the function is differentiable at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the differentiability exactly says that the map is well approximated by `L`). This is proved in `mem_A_of_differentiable` and `differentiable_set_subset_D`. For the other direction, the difficulty is that `L` in the union may depend on `ε, r, s`. The key point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to `A (L, r, ε)` and `A (L', r, ε)`, then `L` and `L'` have to be close on a shell, and thus `‖L - L'‖` is bounded by `ε` (see `norm_sub_le_of_mem_A`). Assume now `x ∈ D`. If one has two maps `L` and `L'` such that `x` belongs to `A (L, r, ε)` and to `A (L', r', ε')`, one deduces that `L` is close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a linear map `L₁` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `ε` (it exists as `x` belongs to `D`). Take also `L₂` that approximates `f` around `x` both at scales `r'` and `s` w.r.t. `ε'`. Then `L₁` is close to `L` (as they are close on a shell of radius `r`), and `L₂` is close to `L₁` (as they are close on a shell of radius `s`), and `L'` is close to `L₂` (as they are close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed. It follows that the different approximating linear maps that show up form a Cauchy sequence when `ε` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`. With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`. To show that the derivative itself is measurable, add in the definition of `B` and `D` a set `K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K` is exactly the set of points where `f` is differentiable with a derivative in `K`. ## Tags derivative, measurable function, Borel σ-algebra -/ noncomputable theory open set metric asymptotics filter continuous_linear_map open topological_space (second_countable_topology) measure_theory open_locale topology namespace continuous_linear_map variables {𝕜 E F : Type*} [nontrivially_normed_field 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] [normed_add_comm_group F] [normed_space 𝕜 F] lemma measurable_apply₂ [measurable_space E] [opens_measurable_space E] [second_countable_topology E] [second_countable_topology (E →L[𝕜] F)] [measurable_space F] [borel_space F] : measurable (λ p : (E →L[𝕜] F) × E, p.1 p.2) := is_bounded_bilinear_map_apply.continuous.measurable end continuous_linear_map section fderiv variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] variables {f : E → F} (K : set (E →L[𝕜] F)) namespace fderiv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `L`, up to an error `ε`. We tweak the definition to make sure that this is an open set.-/ def A (f : E → F) (L : E →L[𝕜] F) (r ε : ℝ) : set E := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ ball x r', ‖f z - f y - L (z-y)‖ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a continuous linear map `L` belonging to `K` (a given set of continuous linear maps) that approximates well the function `f` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : E → F) (K : set (E →L[𝕜] F)) (r s ε : ℝ) : set E := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : E → F) (K : set (E →L[𝕜] F)) : set E := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma is_open_A (L : E →L[𝕜] F) (r ε : ℝ) : is_open (A f L r ε) := begin rw metric.is_open_iff, rintros x ⟨r', r'_mem, hr'⟩, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between r'_mem.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le r'_mem.2)⟩, refine ⟨r' - s, by linarith, λ x' hx', ⟨s, this, _⟩⟩, have B : ball x' s ⊆ ball x r' := ball_subset (le_of_lt hx'), assume y hy z hz, exact hr' y (B hy) z (B hz) end lemma is_open_B {K : set (E →L[𝕜] F)} {r s ε : ℝ} : is_open (B f K r s ε) := by simp [B, is_open_Union, is_open.inter, is_open_A] lemma A_mono (L : E →L[𝕜] F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y hy z hz, (hr' y hy z hz).trans (mul_le_mul_of_nonneg_right h _)⟩, linarith [mem_ball.1 hy, r'r.2, @dist_nonneg _ _ y x], end lemma le_of_mem_A {r ε : ℝ} {L : E →L[𝕜] F} {x : E} (hx : x ∈ A f L r ε) {y z : E} (hy : y ∈ closed_ball x (r/2)) (hz : z ∈ closed_ball x (r/2)) : ‖f z - f y - L (z-y)‖ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, exact hr' _ ((mem_closed_ball.1 hy).trans_lt r'mem.1) _ ((mem_closed_ball.1 hz).trans_lt r'mem.1) end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : E} (hx : differentiable_at 𝕜 f x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (fderiv 𝕜 f x) r ε := begin have := hx.has_fderiv_at, simp only [has_fderiv_at, has_fderiv_at_filter, is_o_iff] at this, rcases eventually_nhds_iff_ball.1 (this (half_pos hε)) with ⟨R, R_pos, hR⟩, refine ⟨R, R_pos, λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_rfl⟩, refine ⟨r, this, λ y hy z hz, _⟩, calc ‖f z - f y - (fderiv 𝕜 f x) (z - y)‖ = ‖(f z - f x - (fderiv 𝕜 f x) (z - x)) - (f y - f x - (fderiv 𝕜 f x) (y - x))‖ : by { congr' 1, simp only [continuous_linear_map.map_sub], abel } ... ≤ ‖(f z - f x - (fderiv 𝕜 f x) (z - x))‖ + ‖f y - f x - (fderiv 𝕜 f x) (y - x)‖ : norm_sub_le _ _ ... ≤ ε / 2 * ‖z - x‖ + ε / 2 * ‖y - x‖ : add_le_add (hR _ (lt_trans (mem_ball.1 hz) hr.2)) (hR _ (lt_trans (mem_ball.1 hy) hr.2)) ... ≤ ε / 2 * r + ε / 2 * r : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hz)) (le_of_lt (half_pos hε))) (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hy)) (le_of_lt (half_pos hε))) ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {c : 𝕜} (hc : 1 < ‖c‖) {r ε : ℝ} (hε : 0 < ε) (hr : 0 < r) {x : E} {L₁ L₂ : E →L[𝕜] F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ‖L₁ - L₂‖ ≤ 4 * ‖c‖ * ε := begin have : 0 ≤ 4 * ‖c‖ * ε := mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4) (norm_nonneg _)) hε.le, refine op_norm_le_of_shell (half_pos hr) this hc _, assume y ley ylt, rw [div_div, div_le_iff' (mul_pos (by norm_num : (0 : ℝ) < 2) (zero_lt_one.trans hc))] at ley, calc ‖(L₁ - L₂) y‖ = ‖(f (x + y) - f x - L₂ ((x + y) - x)) - (f (x + y) - f x - L₁ ((x + y) - x))‖ : by simp ... ≤ ‖(f (x + y) - f x - L₂ ((x + y) - x))‖ + ‖(f (x + y) - f x - L₁ ((x + y) - x))‖ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le], } }, { apply le_of_mem_A h₁, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le] } }, end ... = 2 * ε * r : by ring ... ≤ 2 * ε * (2 * ‖c‖ * ‖y‖) : mul_le_mul_of_nonneg_left ley (mul_nonneg (by norm_num) hε.le) ... = 4 * ‖c‖ * ε * ‖y‖ : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_iff], refine ⟨n, λ p hp q hq, ⟨fderiv 𝕜 f x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set (E →L[𝕜] F)} (hK : is_complete K) : D f K ⊆ {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have cpos : 0 < ‖c‖ := lt_trans zero_lt_one hc, assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ‖L e p q - L e' p' q'‖ ≤ 12 * ‖c‖ * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ‖L e p q - L e p r‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A hc P P I1 I2 }, have J2 : ‖L e p r - L e' p' r‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A hc P P I1 (A_mono _ _ I I2) }, have J3 : ‖L e' p' r - L e' p' q'‖ ≤ 4 * ‖c‖ * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A hc P P (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ‖L e p q - L e' p' q'‖ = ‖(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')‖ : by { congr' 1, abel } ... ≤ ‖L e p q - L e p r‖ + ‖L e p r - L e' p' r‖ + ‖L e' p' r - L e' p' q'‖ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * ‖c‖ * (1/2)^e + 4 * ‖c‖ * (1/2)^e + 4 * ‖c‖ * (1/2)^e : by apply_rules [add_le_add] ... = 12 * ‖c‖ * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → (E →L[𝕜] F) := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw metric.cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / (12 * ‖c‖) := exists_pow_lt_of_lt_one (div_pos εpos (mul_pos (by norm_num) cpos)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ‖L0 e - L0 e'‖ ≤ 12 * ‖c‖ * (1/2)^e : M _ _ _ _ _ _ le_rfl le_rfl le_rfl le_rfl he' ... < 12 * ‖c‖ * (ε / (12 * ‖c‖)) : mul_lt_mul' le_rfl he (le_of_lt P) (mul_pos (by norm_num) cpos) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0), ne_of_gt cpos], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) le_rfl le_rfl).1) this, have Lf' : ∀ e p, n e ≤ p → ‖L e (n e) p - f'‖ ≤ 12 * ‖c‖ * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ le_rfl hp le_rfl le_rfl he'⟩ }, /- Let us show that `f` has derivative `f'` at `x`. -/ have : has_fderiv_at f f' x, { simp only [has_fderiv_at_iff_is_o_nhds_zero, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole ball of radius `(1/2)^(n e)`. -/ assume ε εpos, have pos : 0 < 4 + 12 * ‖c‖ := add_pos_of_pos_of_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _)), obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / (4 + 12 * ‖c‖) := exists_pow_lt_of_lt_one (div_pos εpos pos) (by norm_num), rw eventually_nhds_iff_ball, refine ⟨(1/2) ^ (n e + 1), P, λ y hy, _⟩, -- We need to show that `f (x + y) - f x - f' y` is small. For this, we will work at scale -- `k` where `k` is chosen with `‖y‖ ∼ 2 ^ (-k)`. by_cases y_pos : y = 0, {simp [y_pos] }, have yzero : 0 < ‖y‖ := norm_pos_iff.mpr y_pos, have y_lt : ‖y‖ < (1/2) ^ (n e + 1), by simpa using mem_ball_iff_norm.1 hy, have yone : ‖y‖ ≤ 1 := le_trans (y_lt.le) (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < ‖y‖ ∧ ‖y‖ ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_trans hk y_lt, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J1 : ‖f (x + y) - f x - L e (n e) m ((x + y) - x)‖ ≤ (1/2) ^ e * (1/2) ^ m, { apply le_of_mem_A (hn e (n e) m le_rfl m_ge).2.2, { simp only [mem_closed_ball, dist_self], exact div_nonneg (le_of_lt P) (zero_le_two) }, { simpa only [dist_eq_norm, add_sub_cancel', mem_closed_ball, pow_succ', mul_one_div] using h'k } }, have J2 : ‖f (x + y) - f x - L e (n e) m y‖ ≤ 4 * (1/2) ^ e * ‖y‖ := calc ‖f (x + y) - f x - L e (n e) m y‖ ≤ (1/2) ^ e * (1/2) ^ m : by simpa only [add_sub_cancel'] using J1 ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * ‖y‖ : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)), -- use the previous estimates to see that `f (x + y) - f x - f' y` is small. calc ‖f (x + y) - f x - f' y‖ = ‖(f (x + y) - f x - L e (n e) m y) + (L e (n e) m - f') y‖ : congr_arg _ (by simp) ... ≤ 4 * (1/2) ^ e * ‖y‖ + 12 * ‖c‖ * (1/2) ^ e * ‖y‖ : norm_add_le_of_le J2 ((le_op_norm _ _).trans (mul_le_mul_of_nonneg_right (Lf' _ _ m_ge) (norm_nonneg _))) ... = (4 + 12 * ‖c‖) * ‖y‖ * (1/2) ^ e : by ring ... ≤ (4 + 12 * ‖c‖) * ‖y‖ * (ε / (4 + 12 * ‖c‖)) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (add_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _))) (norm_nonneg _)) ... = ε * ‖y‖ : by { field_simp [ne_of_gt pos], ring } }, rw ← this.fderiv at f'K, exact ⟨this.differentiable_at, f'K⟩ end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end fderiv_measurable_aux open fderiv_measurable_aux variables [measurable_space E] [opens_measurable_space E] variables (𝕜 f) /-- The set of differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_at_of_is_complete {K : set (E →L[𝕜] F)} (hK : is_complete K) : measurable_set {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := by simp [differentiable_set_eq_D K hK, D, is_open_B.measurable_set, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_at : measurable_set {x | differentiable_at 𝕜 f x} := begin have : is_complete (univ : set (E →L[𝕜] F)) := complete_univ, convert measurable_set_of_differentiable_at_of_is_complete 𝕜 f this, simp end @[measurability] lemma measurable_fderiv : measurable (fderiv 𝕜 f) := begin refine measurable_of_is_closed (λ s hs, _), have : fderiv 𝕜 f ⁻¹' s = {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ s} ∪ ({x | ¬differentiable_at 𝕜 f x} ∩ {x | (0 : E →L[𝕜] F) ∈ s}) := set.ext (λ x, mem_preimage.trans fderiv_mem_iff), rw this, exact (measurable_set_of_differentiable_at_of_is_complete _ _ hs.is_complete).union ((measurable_set_of_differentiable_at _ _).compl.inter (measurable_set.const _)) end @[measurability] lemma measurable_fderiv_apply_const [measurable_space F] [borel_space F] (y : E) : measurable (λ x, fderiv 𝕜 f x y) := (continuous_linear_map.measurable_apply y).comp (measurable_fderiv 𝕜 f) variable {𝕜} @[measurability] lemma measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) : measurable (deriv f) := by simpa only [fderiv_deriv] using measurable_fderiv_apply_const 𝕜 f 1 lemma strongly_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [second_countable_topology F] (f : 𝕜 → F) : strongly_measurable (deriv f) := by { borelize F, exact (measurable_deriv f).strongly_measurable } lemma ae_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) (μ : measure 𝕜) : ae_measurable (deriv f) μ := (measurable_deriv f).ae_measurable lemma ae_strongly_measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [second_countable_topology F] (f : 𝕜 → F) (μ : measure 𝕜) : ae_strongly_measurable (deriv f) μ := (strongly_measurable_deriv f).ae_strongly_measurable end fderiv section right_deriv variables {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] variables {f : ℝ → F} (K : set F) namespace right_deriv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `h ↦ h • L`, up to an error `ε`. We tweak the definition to make sure that this is open on the right. -/ def A (f : ℝ → F) (L : F) (r ε : ℝ) : set ℝ := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ Icc x (x + r'), ‖f z - f y - (z-y) • L‖ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a vector `L` belonging to `K` (a given set of vectors) such that `h • L` approximates well `f (x + h)` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : ℝ → F) (K : set F) (r s ε : ℝ) : set ℝ := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : ℝ → F) (K : set F) : set ℝ := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma A_mem_nhds_within_Ioi {L : F} {r ε x : ℝ} (hx : x ∈ A f L r ε) : A f L r ε ∈ 𝓝[>] x := begin rcases hx with ⟨r', rr', hr'⟩, rw mem_nhds_within_Ioi_iff_exists_Ioo_subset, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between rr'.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le rr'.2)⟩, refine ⟨x + r' - s, by { simp only [mem_Ioi], linarith }, λ x' hx', ⟨s, this, _⟩⟩, have A : Icc x' (x' + s) ⊆ Icc x (x + r'), { apply Icc_subset_Icc hx'.1.le, linarith [hx'.2] }, assume y hy z hz, exact hr' y (A hy) z (A hz) end lemma B_mem_nhds_within_Ioi {K : set F} {r s ε x : ℝ} (hx : x ∈ B f K r s ε) : B f K r s ε ∈ 𝓝[>] x := begin obtain ⟨L, LK, hL₁, hL₂⟩ : ∃ (L : F), L ∈ K ∧ x ∈ A f L r ε ∧ x ∈ A f L s ε, by simpa only [B, mem_Union, mem_inter_iff, exists_prop] using hx, filter_upwards [A_mem_nhds_within_Ioi hL₁, A_mem_nhds_within_Ioi hL₂] with y hy₁ hy₂, simp only [B, mem_Union, mem_inter_iff, exists_prop], exact ⟨L, LK, hy₁, hy₂⟩ end lemma measurable_set_B {K : set F} {r s ε : ℝ} : measurable_set (B f K r s ε) := measurable_set_of_mem_nhds_within_Ioi (λ x hx, B_mem_nhds_within_Ioi hx) lemma A_mono (L : F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y hy z hz, (hr' y hy z hz).trans (mul_le_mul_of_nonneg_right h _)⟩, linarith [hy.1, hy.2, r'r.2], end lemma le_of_mem_A {r ε : ℝ} {L : F} {x : ℝ} (hx : x ∈ A f L r ε) {y z : ℝ} (hy : y ∈ Icc x (x + r/2)) (hz : z ∈ Icc x (x + r/2)) : ‖f z - f y - (z-y) • L‖ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, have A : x + r / 2 ≤ x + r', by linarith [r'mem.1], exact hr' _ ((Icc_subset_Icc le_rfl A) hy) _ ((Icc_subset_Icc le_rfl A) hz), end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : ℝ} (hx : differentiable_within_at ℝ f (Ici x) x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (deriv_within f (Ici x) x) r ε := begin have := hx.has_deriv_within_at, simp_rw [has_deriv_within_at_iff_is_o, is_o_iff] at this, rcases mem_nhds_within_Ici_iff_exists_Ico_subset.1 (this (half_pos hε)) with ⟨m, xm, hm⟩, refine ⟨m - x, by linarith [show x < m, from xm], λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_rfl⟩, refine ⟨r, this, λ y hy z hz, _⟩, calc ‖f z - f y - (z - y) • deriv_within f (Ici x) x‖ = ‖(f z - f x - (z - x) • deriv_within f (Ici x) x) - (f y - f x - (y - x) • deriv_within f (Ici x) x)‖ : by { congr' 1, simp only [sub_smul], abel } ... ≤ ‖f z - f x - (z - x) • deriv_within f (Ici x) x‖ + ‖f y - f x - (y - x) • deriv_within f (Ici x) x‖ : norm_sub_le _ _ ... ≤ ε / 2 * ‖z - x‖ + ε / 2 * ‖y - x‖ : add_le_add (hm ⟨hz.1, hz.2.trans_lt (by linarith [hr.2])⟩) (hm ⟨hy.1, hy.2.trans_lt (by linarith [hr.2])⟩) ... ≤ ε / 2 * r + ε / 2 * r : begin apply add_le_add, { apply mul_le_mul_of_nonneg_left _ (le_of_lt (half_pos hε)), rw [real.norm_of_nonneg]; linarith [hz.1, hz.2] }, { apply mul_le_mul_of_nonneg_left _ (le_of_lt (half_pos hε)), rw [real.norm_of_nonneg]; linarith [hy.1, hy.2] }, end ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {r x : ℝ} (hr : 0 < r) (ε : ℝ) {L₁ L₂ : F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ‖L₁ - L₂‖ ≤ 4 * ε := begin suffices H : ‖(r/2) • (L₁ - L₂)‖ ≤ (r / 2) * (4 * ε), by rwa [norm_smul, real.norm_of_nonneg (half_pos hr).le, mul_le_mul_left (half_pos hr)] at H, calc ‖(r/2) • (L₁ - L₂)‖ = ‖(f (x + r/2) - f x - (x + r/2 - x) • L₂) - (f (x + r/2) - f x - (x + r/2 - x) • L₁)‖ : by simp [smul_sub] ... ≤ ‖f (x + r/2) - f x - (x + r/2 - x) • L₂‖ + ‖f (x + r/2) - f x - (x + r/2 - x) • L₁‖ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂; simp [(half_pos hr).le] }, { apply le_of_mem_A h₁; simp [(half_pos hr).le] }, end ... = (r / 2) * (4 * ε) : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_iff], refine ⟨n, λ p hp q hq, ⟨deriv_within f (Ici x) x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set F} (hK : is_complete K) : D f K ⊆ {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ‖L e p q - L e' p' q'‖ ≤ 12 * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ‖L e p q - L e p r‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A P _ I1 I2 }, have J2 : ‖L e p r - L e' p' r‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A P _ I1 (A_mono _ _ I I2) }, have J3 : ‖L e' p' r - L e' p' q'‖ ≤ 4 * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A P _ (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ‖L e p q - L e' p' q'‖ = ‖(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')‖ : by { congr' 1, abel } ... ≤ ‖L e p q - L e p r‖ + ‖L e p r - L e' p' r‖ + ‖L e' p' r - L e' p' q'‖ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * (1/2)^e + 4 * (1/2)^e + 4 * (1/2)^e : by apply_rules [add_le_add] ... = 12 * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → F := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw metric.cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / 12 := exists_pow_lt_of_lt_one (div_pos εpos (by norm_num)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ‖L0 e - L0 e'‖ ≤ 12 * (1/2)^e : M _ _ _ _ _ _ le_rfl le_rfl le_rfl le_rfl he' ... < 12 * (ε / 12) : mul_lt_mul' le_rfl he (le_of_lt P) (by norm_num) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0)], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) le_rfl le_rfl).1) this, have Lf' : ∀ e p, n e ≤ p → ‖L e (n e) p - f'‖ ≤ 12 * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ le_rfl hp le_rfl le_rfl he'⟩ }, /- Let us show that `f` has right derivative `f'` at `x`. -/ have : has_deriv_within_at f f' (Ici x) x, { simp only [has_deriv_within_at_iff_is_o, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole interval of length `(1/2)^(n e)`. -/ assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / 16 := exists_pow_lt_of_lt_one (div_pos εpos (by norm_num)) (by norm_num), have xmem : x ∈ Ico x (x + (1/2)^(n e + 1)), by simp only [one_div, left_mem_Ico, lt_add_iff_pos_right, inv_pos, pow_pos, zero_lt_bit0, zero_lt_one], filter_upwards [Icc_mem_nhds_within_Ici xmem] with y hy, -- We need to show that `f y - f x - f' (y - x)` is small. For this, we will work at scale -- `k` where `k` is chosen with `‖y - x‖ ∼ 2 ^ (-k)`. rcases eq_or_lt_of_le hy.1 with rfl|xy, { simp only [sub_self, zero_smul, norm_zero, mul_zero]}, have yzero : 0 < y - x := sub_pos.2 xy, have y_le : y - x ≤ (1/2) ^ (n e + 1), by linarith [hy.2], have yone : y - x ≤ 1 := le_trans y_le (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < y - x ∧ y - x ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y - x` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_of_lt_of_le hk y_le, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J : ‖f y - f x - (y - x) • L e (n e) m‖ ≤ 4 * (1/2) ^ e * ‖y - x‖ := calc ‖f y - f x - (y - x) • L e (n e) m‖ ≤ (1/2) ^ e * (1/2) ^ m : begin apply le_of_mem_A (hn e (n e) m le_rfl m_ge).2.2, { simp only [one_div, inv_pow, left_mem_Icc, le_add_iff_nonneg_right], exact div_nonneg (inv_nonneg.2 (pow_nonneg zero_le_two _)) zero_le_two }, { simp only [pow_add, tsub_le_iff_left] at h'k, simpa only [hy.1, mem_Icc, true_and, one_div, pow_one] using h'k } end ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * (y - x) : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)) ... = 4 * (1/2) ^ e * ‖y - x‖ : by rw [real.norm_of_nonneg yzero.le], calc ‖f y - f x - (y - x) • f'‖ = ‖(f y - f x - (y - x) • L e (n e) m) + (y - x) • (L e (n e) m - f')‖ : by simp only [smul_sub, sub_add_sub_cancel] ... ≤ 4 * (1/2) ^ e * ‖y - x‖ + ‖y - x‖ * (12 * (1/2) ^ e) : norm_add_le_of_le J (by { rw [norm_smul], exact mul_le_mul_of_nonneg_left (Lf' _ _ m_ge) (norm_nonneg _) }) ... = 16 * ‖y - x‖ * (1/2) ^ e : by ring ... ≤ 16 * ‖y - x‖ * (ε / 16) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (by norm_num) (norm_nonneg _)) ... = ε * ‖y - x‖ : by ring }, rw ← this.deriv_within (unique_diff_on_Ici x x le_rfl) at f'K, exact ⟨this.differentiable_within_at, f'K⟩, end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end right_deriv_measurable_aux open right_deriv_measurable_aux variables (f) /-- The set of right differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ici_of_is_complete {K : set F} (hK : is_complete K) : measurable_set {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ K} := by simp [differentiable_set_eq_D K hK, D, measurable_set_B, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of right differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ici : measurable_set {x | differentiable_within_at ℝ f (Ici x) x} := begin have : is_complete (univ : set F) := complete_univ, convert measurable_set_of_differentiable_within_at_Ici_of_is_complete f this, simp end @[measurability] lemma measurable_deriv_within_Ici [measurable_space F] [borel_space F] : measurable (λ x, deriv_within f (Ici x) x) := begin refine measurable_of_is_closed (λ s hs, _), have : (λ x, deriv_within f (Ici x) x) ⁻¹' s = {x | differentiable_within_at ℝ f (Ici x) x ∧ deriv_within f (Ici x) x ∈ s} ∪ ({x | ¬differentiable_within_at ℝ f (Ici x) x} ∩ {x | (0 : F) ∈ s}) := set.ext (λ x, mem_preimage.trans deriv_within_mem_iff), rw this, exact (measurable_set_of_differentiable_within_at_Ici_of_is_complete _ hs.is_complete).union ((measurable_set_of_differentiable_within_at_Ici _).compl.inter (measurable_set.const _)) end lemma strongly_measurable_deriv_within_Ici [second_countable_topology F] : strongly_measurable (λ x, deriv_within f (Ici x) x) := by { borelize F, exact (measurable_deriv_within_Ici f).strongly_measurable } lemma ae_measurable_deriv_within_Ici [measurable_space F] [borel_space F] (μ : measure ℝ) : ae_measurable (λ x, deriv_within f (Ici x) x) μ := (measurable_deriv_within_Ici f).ae_measurable lemma ae_strongly_measurable_deriv_within_Ici [second_countable_topology F] (μ : measure ℝ) : ae_strongly_measurable (λ x, deriv_within f (Ici x) x) μ := (strongly_measurable_deriv_within_Ici f).ae_strongly_measurable /-- The set of right differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_within_at_Ioi : measurable_set {x | differentiable_within_at ℝ f (Ioi x) x} := by simpa [differentiable_within_at_Ioi_iff_Ici] using measurable_set_of_differentiable_within_at_Ici f @[measurability] lemma measurable_deriv_within_Ioi [measurable_space F] [borel_space F] : measurable (λ x, deriv_within f (Ioi x) x) := by simpa [deriv_within_Ioi_eq_Ici] using measurable_deriv_within_Ici f lemma strongly_measurable_deriv_within_Ioi [second_countable_topology F] : strongly_measurable (λ x, deriv_within f (Ioi x) x) := by { borelize F, exact (measurable_deriv_within_Ioi f).strongly_measurable } lemma ae_measurable_deriv_within_Ioi [measurable_space F] [borel_space F] (μ : measure ℝ) : ae_measurable (λ x, deriv_within f (Ioi x) x) μ := (measurable_deriv_within_Ioi f).ae_measurable lemma ae_strongly_measurable_deriv_within_Ioi [second_countable_topology F] (μ : measure ℝ) : ae_strongly_measurable (λ x, deriv_within f (Ioi x) x) μ := (strongly_measurable_deriv_within_Ioi f).ae_strongly_measurable end right_deriv
c39c937367636956cb443a7bdabb10fe1a4b3b7f
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/28.lean
a500cd070463fc64cff88b515c1596f054b56b5e
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,340
lean
structure bv (w : Nat) := (u:Unit) inductive val : Type | bv (w : Nat) : bv w → val inductive memtype : Type | ptr : Unit → memtype inductive instr : Type | load : memtype -> val -> instr | store : Unit -> Unit -> Unit -> instr structure sim (a:Type) := (runSim : ∀{z:Type}, (IO.Error → z) /- error continuation -/ → (a → z) /- normal continuation -/ → z) instance monad : Monad sim := { bind := λa b mx mf => sim.mk (λz kerr k => mx.runSim kerr (λx => (mf x).runSim kerr k)) , pure := λa x => sim.mk (λz _ k => k x) } instance monadExcept : MonadExcept IO.Error sim := { throw := λa err => sim.mk (λz kerr _k => kerr err) , catch := λa m handle => sim.mk (λz kerr k => let kerr' := λerr => (handle err).runSim kerr k; m.runSim kerr' k) }. def f : sim val := throw (IO.userError "ASDF") def g : sim Unit := throw (IO.userError "ASDF") def evalInstr : instr → sim (Option val) | instr.load mt pv => match mt, pv with | memtype.ptr _, val.bv 27 _ => throw (IO.userError "ASDF") | _, _ => throw (IO.userError "expected pointer value in load" ) | instr.store _ _ _ => do pv <- f; _ <- g; match pv with | val.bv 27 _ => throw (IO.userError "asdf") | _ => throw (IO.userError "expected pointer value in store" )
fec8f06b98aaa87f39f4375e117cfa92400a62b3
4727251e0cd73359b15b664c3170e5d754078599
/test/simp_rw.lean
5e874625d60c0c058f4addd164f87d24e0eb8a8a
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,611
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.nat.basic import data.set.basic import tactic.simp_rw /-! # Tests for `simp_rw` extensions -/ -- `simp_rw` can perform rewrites under binders: example : (λ (x y : ℕ), x + y) = (λ x y, y + x) := by simp_rw [add_comm] -- `simp_rw` can apply reverse rules: example (f : ℕ → ℕ) {a b c : ℕ} (ha : f b = a) (hc : f b = c) : a = c := by simp_rw [← ha, hc] -- `simp_rw` performs rewrites in the given order (`simp` fails on this example): example {α β : Type} {f : α → β} {t : set β} : (∀ s, f '' s ⊆ t) = ∀ s : set α, ∀ x ∈ s, x ∈ f ⁻¹' t := by simp_rw [set.image_subset_iff, set.subset_def] -- `simp_rw` applies rewrite rules multiple times: example (a b c d : ℕ) : a + (b + (c + d)) = ((d + c) + b) + a := by simp_rw [add_comm] -- `simp_rw` can also rewrite in assumptions: example (p : ℕ → Prop) (a b : ℕ) (h : p (a + b)) : p (b + a) := by {simp_rw [add_comm a b] at h, exact h} -- or explicitly rewrite at the goal: example (p : ℕ → Prop) (a b : ℕ) (h : p (a + b)) : p (b + a) := by {simp_rw [add_comm b a] at ⊢, exact h} -- or at multiple assumptions: example (p : ℕ → Prop) (a b : ℕ) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (b + a) := by {simp_rw [add_comm a b] at h₁ h₂, exact h₁ h₂} -- or everywhere: example (p : ℕ → Prop) (a b : ℕ) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (a + b) := by {simp_rw [add_comm a b] at *, exact h₁ h₂}
bb8b8b7e33c7726477f962619c9ba897ea132ff2
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/forest.lean
3215cc57895347327652ef647fd2ad501c600993
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,484
lean
import data.prod data.unit open prod inductive tree (A : Type) : Type := node : A → forest A → tree A with forest : Type := nil : forest A, cons : tree A → forest A → forest A namespace manual definition tree.below.{l₁ l₂} (A : Type.{l₁}) (C₁ : tree A → Type.{l₂}) (C₂ : forest A → Type.{l₂}) (t : tree A) : Type.{max 1 l₂} := @tree.rec_on A (λ t : tree A, Type.{max 1 l₂}) (λ t : forest A, Type.{max 1 l₂}) t (λ (a : A) (f : forest A) (r : Type.{max 1 l₂}), prod.{l₂ (max 1 l₂)} (C₂ f) r) unit.{max 1 l₂} (λ (t : tree A) (f : forest A) (rt : Type.{max 1 l₂}) (rf : Type.{max 1 l₂}), prod.{(max 1 l₂) (max 1 l₂)} (prod.{l₂ (max 1 l₂)} (C₁ t) rt) (prod.{l₂ (max 1 l₂)} (C₂ f) rf)) definition forest.below.{l₁ l₂} (A : Type.{l₁}) (C₁ : tree A → Type.{l₂}) (C₂ : forest A → Type.{l₂}) (f : forest A) : Type.{max 1 l₂} := @forest.rec_on A (λ t : tree A, Type.{max 1 l₂}) (λ t : forest A, Type.{max 1 l₂}) f (λ (a : A) (f : forest A) (r : Type.{max 1 l₂}), prod.{l₂ (max 1 l₂)} (C₂ f) r) unit.{max 1 l₂} (λ (t : tree A) (f : forest A) (rt : Type.{max 1 l₂}) (rf : Type.{max 1 l₂}), prod.{(max 1 l₂) (max 1 l₂)} (prod.{l₂ (max 1 l₂)} (C₁ t) rt) (prod.{l₂ (max 1 l₂)} (C₂ f) rf)) definition tree.brec_on.{l₁ l₂} (A : Type.{l₁}) (C₁ : tree A → Type.{l₂}) (C₂ : forest A → Type.{l₂}) (t : tree A) (F₁ : Π (t : tree A), tree.below A C₁ C₂ t → C₁ t) (F₂ : Π (f : forest A), forest.below A C₁ C₂ f → C₂ f) : C₁ t := have general : prod.{l₂ (max 1 l₂)} (C₁ t) (tree.below A C₁ C₂ t), from @tree.rec_on A (λ (t' : tree A), prod.{l₂ (max 1 l₂)} (C₁ t') (tree.below A C₁ C₂ t')) (λ (f' : forest A), prod.{l₂ (max 1 l₂)} (C₂ f') (forest.below A C₁ C₂ f')) t (λ (a : A) (f : forest A) (r : prod.{l₂ (max 1 l₂)} (C₂ f) (forest.below A C₁ C₂ f)), have b : tree.below A C₁ C₂ (tree.node a f), from r, have c : C₁ (tree.node a f), from F₁ (tree.node a f) b, prod.mk.{l₂ (max 1 l₂)} c b) (have b : forest.below A C₁ C₂ (forest.nil A), from unit.star.{max 1 l₂}, have c : C₂ (forest.nil A), from F₂ (forest.nil A) b, prod.mk.{l₂ (max 1 l₂)} c b) (λ (t : tree A) (f : forest A) (rt : prod.{l₂ (max 1 l₂)} (C₁ t) (tree.below A C₁ C₂ t)) (rf : prod.{l₂ (max 1 l₂)} (C₂ f) (forest.below A C₁ C₂ f)), have b : forest.below A C₁ C₂ (forest.cons t f), from prod.mk.{(max 1 l₂) (max 1 l₂)} rt rf, have c : C₂ (forest.cons t f), from F₂ (forest.cons t f) b, prod.mk.{l₂ (max 1 l₂)} c b), pr₁ general definition forest.brec_on.{l₁ l₂} (A : Type.{l₁}) (C₁ : tree A → Type.{l₂}) (C₂ : forest A → Type.{l₂}) (f : forest A) (F₁ : Π (t : tree A), tree.below A C₁ C₂ t → C₁ t) (F₂ : Π (f : forest A), forest.below A C₁ C₂ f → C₂ f) : C₂ f := have general : prod.{l₂ (max 1 l₂)} (C₂ f) (forest.below A C₁ C₂ f), from @forest.rec_on A (λ (t' : tree A), prod.{l₂ (max 1 l₂)} (C₁ t') (tree.below A C₁ C₂ t')) (λ (f' : forest A), prod.{l₂ (max 1 l₂)} (C₂ f') (forest.below A C₁ C₂ f')) f (λ (a : A) (f : forest A) (r : prod.{l₂ (max 1 l₂)} (C₂ f) (forest.below A C₁ C₂ f)), have b : tree.below A C₁ C₂ (tree.node a f), from r, have c : C₁ (tree.node a f), from F₁ (tree.node a f) b, prod.mk.{l₂ (max 1 l₂)} c b) (have b : forest.below A C₁ C₂ (forest.nil A), from unit.star.{max 1 l₂}, have c : C₂ (forest.nil A), from F₂ (forest.nil A) b, prod.mk.{l₂ (max 1 l₂)} c b) (λ (t : tree A) (f : forest A) (rt : prod.{l₂ (max 1 l₂)} (C₁ t) (tree.below A C₁ C₂ t)) (rf : prod.{l₂ (max 1 l₂)} (C₂ f) (forest.below A C₁ C₂ f)), have b : forest.below A C₁ C₂ (forest.cons t f), from prod.mk.{(max 1 l₂) (max 1 l₂)} rt rf, have c : C₂ (forest.cons t f), from F₂ (forest.cons t f) b, prod.mk.{l₂ (max 1 l₂)} c b), pr₁ general end manual
9cade9c68cbbc19f925efc33dfae191ecc428c4c
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/analysis/limits.lean
2c8303e6150f15fc28dca3d78a0b41c4f2852e7e
[ "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
8,120
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl A collection of limit properties. -/ import algebra.big_operators algebra.group_power analysis.metric_space analysis.of_nat analysis.topology.infinite_sum noncomputable theory open classical set finset function filter local attribute [instance] decidable_inhabited prop_decidable infix ` ^ ` := pow_nat section real lemma has_sum_of_absolute_convergence {f : ℕ → ℝ} (hf : ∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) : has_sum f := let f' := λs:finset ℕ, s.sum (λi, abs (f i)) in suffices cauchy (map (λs:finset ℕ, s.sum f) at_top), from complete_space.complete this, cauchy_iff.mpr $ and.intro (map_ne_bot at_top_ne_bot) $ assume s hs, let ⟨ε, hε, hsε⟩ := mem_uniformity_dist.mp hs, ⟨r, hr⟩ := hf in have hε' : {p : ℝ × ℝ | dist p.1 p.2 < ε / 2} ∈ (@uniformity ℝ _).sets, from mem_uniformity_dist.mpr ⟨ε / 2, div_pos_of_pos_of_pos hε two_pos, assume a b h, h⟩, have cauchy (at_top.map $ λn, f' (range n)), from cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hr, have ∃n, ∀{n'}, n ≤ n' → dist (f' (range n)) (f' (range n')) < ε / 2, by simp [cauchy_iff, mem_at_top_iff] at this; from let ⟨t, ht, u, hu⟩ := this _ hε' in ⟨u, assume n' hn, ht $ prod_mk_mem_set_prod_eq.mpr ⟨hu _ (le_refl _), hu _ hn⟩⟩, let ⟨n, hn⟩ := this in have ∀{s}, range n ⊆ s → abs ((s \ range n).sum f) < ε / 2, from assume s hs, let ⟨n', hn'⟩ := @exists_nat_subset_range s in have range n ⊆ range n', from subset.trans hs hn', have f'_nn : 0 ≤ f' (range n' \ range n), from zero_le_sum $ assume _ _, abs_nonneg _, calc abs ((s \ range n).sum f) ≤ f' (s \ range n) : abs_sum_le_sum_abs ... ≤ f' (range n' \ range n) : sum_le_sum_of_subset_of_nonneg (finset.sdiff_subset_sdiff hn' (finset.subset.refl _)) (assume _ _ _, abs_nonneg _) ... = abs (f' (range n' \ range n)) : (abs_of_nonneg f'_nn).symm ... = abs (f' (range n') - f' (range n)) : by simp [f', (sum_sdiff ‹range n ⊆ range n'›).symm] ... = abs (f' (range n) - f' (range n')) : abs_sub _ _ ... < ε / 2 : hn $ range_subset.mp this, have ∀{s t}, range n ⊆ s → range n ⊆ t → dist (s.sum f) (t.sum f) < ε, from assume s t hs ht, calc abs (s.sum f - t.sum f) = abs ((s \ range n).sum f + - (t \ range n).sum f) : by rw [←sum_sdiff hs, ←sum_sdiff ht]; simp ... ≤ abs ((s \ range n).sum f) + abs ((t \ range n).sum f) : le_trans (abs_add_le_abs_add_abs _ _) $ by rw [abs_neg]; exact le_refl _ ... < ε / 2 + ε / 2 : add_lt_add (this hs) (this ht) ... = ε : by rw [←add_div, add_self_div_two], ⟨(λs:finset ℕ, s.sum f) '' {s | range n ⊆ s}, image_mem_map $ mem_at_top (range n), assume ⟨a, b⟩ ⟨⟨t, ht, ha⟩, ⟨s, hs, hb⟩⟩, by simp at ha hb; exact ha ▸ hb ▸ hsε _ _ (this ht hs)⟩ lemma is_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} {r : ℝ} (hf : ∀n, 0 ≤ f n) : is_sum f r ↔ tendsto (λn, (range n).sum f) at_top (nhds r) := ⟨tendsto_sum_nat_of_is_sum, assume hr, have tendsto (λn, (range n).sum (λn, abs (f n))) at_top (nhds r), by simp [(λi, abs_of_nonneg (hf i)), hr], let ⟨p, h⟩ := has_sum_of_absolute_convergence ⟨r, this⟩ in have hp : tendsto (λn, (range n).sum f) at_top (nhds p), from tendsto_sum_nat_of_is_sum h, have p = r, from tendsto_nhds_unique at_top_ne_bot hp hr, this ▸ h⟩ end real lemma mul_add_one_le_pow {r : ℝ} (hr : 0 ≤ r) : ∀{n}, (of_nat n) * r + 1 ≤ (r + 1) ^ n | 0 := by simp; exact le_refl 1 | (n + 1) := let h : of_nat n ≥ (0 : ℝ) := @zero_le_of_nat ℝ _ n in calc of_nat (n + 1) * r + 1 ≤ (of_nat (n + 1) * r + 1) + r * r * of_nat n : le_add_of_le_of_nonneg (le_refl _) (mul_nonneg (mul_nonneg hr hr) h) ... = (r + 1) * (of_nat n * r + 1) : by simp [mul_add, add_mul] ... ≤ (r + 1) * (r + 1) ^ n : mul_le_mul (le_refl _) mul_add_one_le_pow (add_nonneg (mul_nonneg h hr) zero_le_one) (add_nonneg hr zero_le_one) lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi $ assume p, tendsto_principal $ let ⟨n, hn⟩ := @exists_lt_of_nat (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ of_nat n, from (@zero_le_of_nat ℝ _ n), have r - 1 > 0, from sub_lt_iff.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ of_nat n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ of_nat n * (r - 1) + 1 : le_add_of_le_of_nonneg (le_refl _) zero_le_one ... ≤ ((r - 1) + 1) ^ n : mul_add_one_le_pow $ le_of_lt this ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_iff.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_iff.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma map_succ_at_top_eq : map nat.succ at_top = at_top := le_antisymm (assume s hs, let ⟨b, hb⟩ := mem_at_top_iff.mp hs in mem_at_top_iff.mpr ⟨b, assume c hc, hb (c + 1) $ le_trans hc $ nat.le_succ _⟩) (assume s hs, let ⟨b, hb⟩ := mem_at_top_iff.mp hs in mem_at_top_iff.mpr ⟨b + 1, assume c, match c with | 0 := assume h, have 0 > 0, from lt_of_lt_of_le (lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one) h, (lt_irrefl 0 this).elim | (c+1) := assume h, hb _ (nat.le_of_succ_le_succ h) end⟩) lemma tendsto_comp_succ_at_top_iff {α : Type*} {f : ℕ → α} {x : filter α} : tendsto (λn, f (nat.succ n)) at_top x ↔ tendsto f at_top x := calc tendsto (f ∘ nat.succ) at_top x ↔ tendsto f (map nat.succ at_top) x : by simp [tendsto, map_map] ... ↔ _ : by rw [map_succ_at_top_eq] lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn, r^n) at_top (nhds 0) := by_cases (assume : r = 0, tendsto_comp_succ_at_top_iff.mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from tendsto_compose (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂) tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ assume a, by simp [*, pow_inv]) lemma sum_geometric' {r : ℝ} (h : r ≠ 0) : ∀{n}, (finset.range n).sum (λi, (r + 1) ^ i) = ((r + 1) ^ n - 1) / r | 0 := by simp [zero_div] | (n+1) := by simp [@sum_geometric' n, h, pow_succ, add_div_eq_mul_add_div, add_mul] lemma sum_geometric {r : ℝ} {n : ℕ} (h : r ≠ 1) : (range n).sum (λi, r ^ i) = (r ^ n - 1) / (r - 1) := calc (range n).sum (λi, r ^ i) = (range n).sum (λi, ((r - 1) + 1) ^ i) : by simp ... = (((r - 1) + 1) ^ n - 1) / (r - 1) : sum_geometric' $ by simp [sub_eq_iff_eq_add, -sub_eq_add_neg, h] ... = (r ^ n - 1) / (r - 1) : by simp lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg $ pow_nonneg h₁).mpr $ by simp [neg_inv, sum_geometric, div_eq_mul_inv, *] at *
72371125ae1547e3da66306a9b6a7fe4cc795eae
37a833c924892ee3ecb911484775a6d6ebb8984d
/src/category_theory/graphs/default.lean
3528557fc40dc3e05d93c7f8b89ea8ca6c7e6761
[]
no_license
silky/lean-category-theory
28126e80564a1f99e9c322d86b3f7d750da0afa1
0f029a2364975f56ac727d31d867a18c95c22fd8
refs/heads/master
1,589,555,811,646
1,554,673,665,000
1,554,673,665,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,985
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan and Scott Morrison import category_theory.tactics.obviously import tidy.auto_cast namespace category_theory.graphs universes u₁ v₁ u₂ v₂ class graph (vertices : Type u₁) := (edges : vertices → vertices → Type v₁) variable {C : Type u₁} variables {W X Y Z : C} variable [𝒞 : graph.{u₁ v₁} C] def edges : C → C → Type v₁ := @graph.edges.{u₁ v₁} C 𝒞 structure graph_hom (G : Type u₁) [graph.{u₁ v₁} G] (H : Type u₂) [graph.{u₂ v₂} H] := (onVertices : G → H) (onEdges : ∀ {X Y : G}, edges X Y → edges (onVertices X) (onVertices Y)) section variables {G : Type u₁} [𝒢 : graph.{u₁ v₁} G] {H : Type u₂} [ℋ : graph.{u₂ v₂} H] include 𝒢 ℋ @[extensionality] lemma graph_hom_pointwise_equal {p q : graph_hom G H} (vertexWitness : ∀ X : G, p.onVertices X = q.onVertices X) (edgeWitness : ∀ X Y : G, ∀ f : edges X Y, ⟬ p.onEdges f ⟭ = q.onEdges f) : p = q := begin induction p with p_onVertices p_onEdges, induction q with q_onVertices q_onEdges, have h_vertices : p_onVertices = q_onVertices, exact funext vertexWitness, subst h_vertices, have h_edges : @p_onEdges = @q_onEdges, apply funext, intro X, apply funext, intro Y, apply funext, intro f, exact edgeWitness X Y f, subst h_edges end end variables {G : Type u₁} [𝒢 : graph.{u₁ v₁} G] include 𝒢 inductive path : G → G → Type (max u₁ v₁) | nil : Π (h : G), path h h | cons : Π {h s t : G} (e : edges h s) (l : path s t), path h t def path.length : Π {s t : G}, path s t → ℕ | _ _ (path.nil _) := 0 | _ _ (@path.cons _ _ _ _ _ e l) := path.length l notation a :: b := path.cons a b notation `p[` l:(foldr `, ` (h t, path.cons h t) path.nil _ `]`) := l inductive path_of_paths : G → G → Type (max u₁ v₁) | nil : Π (h : G), path_of_paths h h | cons : Π {h s t : G} (e : path h s) (l : path_of_paths s t), path_of_paths h t notation a :: b := path_of_paths.cons a b notation `pp[` l:(foldr `, ` (h t, path_of_paths.cons h t) path_of_paths.nil _ `]`) := l -- The pattern matching trick used here was explained by Jeremy Avigad at https://groups.google.com/d/msg/lean-user/JqaI12tdk3g/F9MZDxkFDAAJ def concatenate_paths : Π {x y z : G}, path x y → path y z → path x z | ._ ._ _ (path.nil _) q := q | ._ ._ _ (@path.cons ._ _ _ _ _ e p') q := path.cons e (concatenate_paths p' q) @[simp] lemma concatenate_paths' {x' x y z : G} (e : edges x' x) (p : path x y) (q : path y z) : concatenate_paths (e :: p) q = e :: (concatenate_paths p q) := rfl def concatenate_path_of_paths : Π {x y : G}, path_of_paths x y → path x y | ._ ._ (path_of_paths.nil X) := path.nil X | ._ ._ (@path_of_paths.cons ._ _ _ _ _ e p') := concatenate_paths e (concatenate_path_of_paths p') end category_theory.graphs
b1dda821693b9e97b196a9db38d23e3a772734b7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/imp2.lean
75d488d441068022b348a2f4059f57651e200157
[ "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
110
lean
import data.num check (λ {A : Type.{1}} (a : A), a) (10:num) check (λ {A} a, a) 10 check (λ a, a) (10:num)